| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.hdmi; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
|
| 21 |
import java.lang.annotation.Retention; |
| 22 |
import java.lang.annotation.RetentionPolicy; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public final class HdmiUtils { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
static final int TARGET_NOT_UNDER_LOCAL_DEVICE = -1; |
| 34 |
static final int TARGET_SAME_PHYSICAL_ADDRESS = 0; |
| 35 |
|
| 36 |
private HdmiUtils() { } |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static int getLocalPortFromPhysicalAddress( |
| 57 |
int targetPhysicalAddress, int myPhysicalAddress) { |
| 58 |
if (myPhysicalAddress == targetPhysicalAddress) { |
| 59 |
return TARGET_SAME_PHYSICAL_ADDRESS; |
| 60 |
} |
| 61 |
|
| 62 |
int mask = 0xF000; |
| 63 |
int finalMask = 0xF000; |
| 64 |
int maskedAddress = myPhysicalAddress; |
| 65 |
|
| 66 |
while (maskedAddress != 0) { |
| 67 |
maskedAddress = myPhysicalAddress & mask; |
| 68 |
finalMask |= mask; |
| 69 |
mask >>= 4; |
| 70 |
} |
| 71 |
|
| 72 |
int portAddress = targetPhysicalAddress & finalMask; |
| 73 |
if ((portAddress & (finalMask << 4)) != myPhysicalAddress) { |
| 74 |
return TARGET_NOT_UNDER_LOCAL_DEVICE; |
| 75 |
} |
| 76 |
|
| 77 |
mask <<= 4; |
| 78 |
int port = portAddress & mask; |
| 79 |
while ((port >> 4) != 0) { |
| 80 |
port >>= 4; |
| 81 |
} |
| 82 |
return port; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@Retention(RetentionPolicy.SOURCE) |
| 89 |
@IntDef({HDMI_RELATIVE_POSITION_UNKNOWN, HDMI_RELATIVE_POSITION_DIRECTLY_BELOW, |
| 90 |
HDMI_RELATIVE_POSITION_BELOW, HDMI_RELATIVE_POSITION_SAME, |
| 91 |
HDMI_RELATIVE_POSITION_DIRECTLY_ABOVE, HDMI_RELATIVE_POSITION_ABOVE, |
| 92 |
HDMI_RELATIVE_POSITION_SIBLING, HDMI_RELATIVE_POSITION_DIFFERENT_BRANCH}) |
| 93 |
public @interface HdmiAddressRelativePosition {} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final int HDMI_RELATIVE_POSITION_UNKNOWN = 0; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public static final int HDMI_RELATIVE_POSITION_DIRECTLY_BELOW = 1; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static final int HDMI_RELATIVE_POSITION_BELOW = 2; |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static final int HDMI_RELATIVE_POSITION_SAME = 3; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static final int HDMI_RELATIVE_POSITION_DIRECTLY_ABOVE = 4; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public static final int HDMI_RELATIVE_POSITION_ABOVE = 5; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public static final int HDMI_RELATIVE_POSITION_SIBLING = 6; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public static final int HDMI_RELATIVE_POSITION_DIFFERENT_BRANCH = 7; |
| 142 |
|
| 143 |
private static final int NPOS = -1; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static boolean isValidPhysicalAddress(int address) { |
| 152 |
if (address < 0 || address >= 0xFFFF) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
int mask = 0xF000; |
| 156 |
boolean hasZero = false; |
| 157 |
for (int i = 0; i < 4; i++) { |
| 158 |
if ((address & mask) == 0) { |
| 159 |
hasZero = true; |
| 160 |
} else if (hasZero) { |
| 161 |
|
| 162 |
|
| 163 |
return false; |
| 164 |
} |
| 165 |
mask >>= 4; |
| 166 |
} |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
@HdmiAddressRelativePosition |
| 175 |
public static int getHdmiAddressRelativePosition(int src, int dest) { |
| 176 |
if (src == 0xFFFF || dest == 0xFFFF) { |
| 177 |
|
| 178 |
return HDMI_RELATIVE_POSITION_UNKNOWN; |
| 179 |
} |
| 180 |
try { |
| 181 |
int firstDiffPos = physicalAddressFirstDifferentDigitPos(src, dest); |
| 182 |
if (firstDiffPos == NPOS) { |
| 183 |
return HDMI_RELATIVE_POSITION_SAME; |
| 184 |
} |
| 185 |
int mask = (0xF000 >> (firstDiffPos * 4)); |
| 186 |
int nextPos = firstDiffPos + 1; |
| 187 |
if ((src & mask) == 0) { |
| 188 |
|
| 189 |
if (nextPos == 4) { |
| 190 |
|
| 191 |
return HDMI_RELATIVE_POSITION_DIRECTLY_ABOVE; |
| 192 |
} |
| 193 |
if (((0xF000 >> (nextPos * 4)) & dest) == 0) { |
| 194 |
|
| 195 |
return HDMI_RELATIVE_POSITION_DIRECTLY_ABOVE; |
| 196 |
} |
| 197 |
return HDMI_RELATIVE_POSITION_ABOVE; |
| 198 |
} |
| 199 |
|
| 200 |
if ((dest & mask) == 0) { |
| 201 |
|
| 202 |
if (nextPos == 4) { |
| 203 |
|
| 204 |
return HDMI_RELATIVE_POSITION_DIRECTLY_BELOW; |
| 205 |
} |
| 206 |
if (((0xF000 >> (nextPos * 4)) & src) == 0) { |
| 207 |
|
| 208 |
return HDMI_RELATIVE_POSITION_DIRECTLY_BELOW; |
| 209 |
} |
| 210 |
return HDMI_RELATIVE_POSITION_BELOW; |
| 211 |
} |
| 212 |
if (nextPos == 4) { |
| 213 |
|
| 214 |
return HDMI_RELATIVE_POSITION_SIBLING; |
| 215 |
} |
| 216 |
if (((0xF000 >> (nextPos * 4)) & src) == 0 && ((0xF000 >> (nextPos * 4)) & dest) == 0) { |
| 217 |
return HDMI_RELATIVE_POSITION_SIBLING; |
| 218 |
} |
| 219 |
return HDMI_RELATIVE_POSITION_DIFFERENT_BRANCH; |
| 220 |
} catch (IllegalArgumentException e) { |
| 221 |
|
| 222 |
return HDMI_RELATIVE_POSITION_UNKNOWN; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
private static int physicalAddressFirstDifferentDigitPos(int address1, int address2) |
| 227 |
throws IllegalArgumentException { |
| 228 |
if (!isValidPhysicalAddress(address1)) { |
| 229 |
throw new IllegalArgumentException(address1 + " is not a valid address."); |
| 230 |
} |
| 231 |
if (!isValidPhysicalAddress(address2)) { |
| 232 |
throw new IllegalArgumentException(address2 + " is not a valid address."); |
| 233 |
} |
| 234 |
int mask = 0xF000; |
| 235 |
for (int i = 0; i < 4; i++) { |
| 236 |
if ((address1 & mask) != (address2 & mask)) { |
| 237 |
return i; |
| 238 |
} |
| 239 |
mask = mask >> 4; |
| 240 |
} |
| 241 |
return NPOS; |
| 242 |
} |
| 243 |
} |