blob: 83c2b2e78911d40b6aa78bd765388418bff7b28c [file] [log] [blame]
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#undef LOG_TAG
18#define LOG_TAG "DisplayIdentification"
19
20#include <algorithm>
21#include <cctype>
22#include <numeric>
23#include <optional>
24
25#include <log/log.h>
26
27#include "DisplayIdentification.h"
Jason Macnak4afe8572021-07-16 13:57:41 -070028#include "Hash.h"
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070029
30namespace android {
31namespace {
32
33using byte_view = std::basic_string_view<uint8_t>;
34
Marin Shalamanov7a9ba302020-03-02 17:49:16 +010035constexpr size_t kEdidBlockSize = 128;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070036constexpr size_t kEdidHeaderLength = 5;
37
Dominik Laskowski075d3172018-05-24 15:50:06 -070038constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu;
39
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070040std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) {
41 if (view.size() < kEdidHeaderLength || view[0] || view[1] || view[2] || view[4]) {
42 return {};
43 }
44
45 return view[3];
46}
47
48std::string_view parseEdidText(const byte_view& view) {
49 std::string_view text(reinterpret_cast<const char*>(view.data()), view.size());
50 text = text.substr(0, text.find('\n'));
51
52 if (!std::all_of(text.begin(), text.end(), ::isprint)) {
53 ALOGW("Invalid EDID: ASCII text is not printable.");
54 return {};
55 }
56
57 return text;
58}
59
60// Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001.
61template <size_t I>
62char getPnpLetter(uint16_t id) {
63 static_assert(I < 3);
64 const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1;
65 return letter < 'A' || letter > 'Z' ? '\0' : letter;
66}
67
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020068DeviceProductInfo buildDeviceProductInfo(const Edid& edid) {
69 DeviceProductInfo info;
Marin Shalamanov359a7e72020-02-17 17:03:07 +010070 info.name.assign(edid.displayName);
71 info.productId = std::to_string(edid.productId);
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020072 info.manufacturerPnpId = edid.pnpId;
73
74 constexpr uint8_t kModelYearFlag = 0xff;
75 constexpr uint32_t kYearOffset = 1990;
76
77 const auto year = edid.manufactureOrModelYear + kYearOffset;
78 if (edid.manufactureWeek == kModelYearFlag) {
79 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
80 } else if (edid.manufactureWeek == 0) {
81 DeviceProductInfo::ManufactureYear date;
82 date.year = year;
83 info.manufactureOrModelDate = date;
84 } else {
85 DeviceProductInfo::ManufactureWeekAndYear date;
86 date.year = year;
87 date.week = edid.manufactureWeek;
88 info.manufactureOrModelDate = date;
89 }
90
Marin Shalamanov896e6302020-04-06 16:11:25 +020091 if (edid.cea861Block && edid.cea861Block->hdmiVendorDataBlock) {
92 const auto& address = edid.cea861Block->hdmiVendorDataBlock->physicalAddress;
93 info.relativeAddress = {address.a, address.b, address.c, address.d};
94 }
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020095 return info;
96}
97
Marin Shalamanov7a9ba302020-03-02 17:49:16 +010098Cea861ExtensionBlock parseCea861Block(const byte_view& block) {
99 Cea861ExtensionBlock cea861Block;
100
101 constexpr size_t kRevisionNumberOffset = 1;
102 cea861Block.revisionNumber = block[kRevisionNumberOffset];
103
104 constexpr size_t kDetailedTimingDescriptorsOffset = 2;
105 const size_t dtdStart =
106 std::min(kEdidBlockSize, static_cast<size_t>(block[kDetailedTimingDescriptorsOffset]));
107
108 // Parse data blocks.
109 for (size_t dataBlockOffset = 4; dataBlockOffset < dtdStart;) {
110 const uint8_t header = block[dataBlockOffset];
111 const uint8_t tag = header >> 5;
112 const size_t bodyLength = header & 0b11111;
113 constexpr size_t kDataBlockHeaderSize = 1;
114 const size_t dataBlockSize = bodyLength + kDataBlockHeaderSize;
115
116 if (block.size() < dataBlockOffset + dataBlockSize) {
117 ALOGW("Invalid EDID: CEA 861 data block is truncated.");
118 break;
119 }
120
121 const byte_view dataBlock(block.data() + dataBlockOffset, dataBlockSize);
122 constexpr uint8_t kVendorSpecificDataBlockTag = 0x3;
123
124 if (tag == kVendorSpecificDataBlockTag) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200125 const uint32_t ieeeRegistrationId = static_cast<uint32_t>(
126 dataBlock[1] | (dataBlock[2] << 8) | (dataBlock[3] << 16));
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100127 constexpr uint32_t kHdmiIeeeRegistrationId = 0xc03;
128
129 if (ieeeRegistrationId == kHdmiIeeeRegistrationId) {
130 const uint8_t a = dataBlock[4] >> 4;
131 const uint8_t b = dataBlock[4] & 0b1111;
132 const uint8_t c = dataBlock[5] >> 4;
133 const uint8_t d = dataBlock[5] & 0b1111;
134 cea861Block.hdmiVendorDataBlock =
135 HdmiVendorDataBlock{.physicalAddress = HdmiPhysicalAddress{a, b, c, d}};
136 } else {
137 ALOGV("Ignoring vendor specific data block for vendor with IEEE OUI %x",
138 ieeeRegistrationId);
139 }
140 } else {
141 ALOGV("Ignoring CEA-861 data block with tag %x", tag);
142 }
143 dataBlockOffset += bodyLength + kDataBlockHeaderSize;
144 }
145
146 return cea861Block;
147}
148
Dominik Laskowski34157762018-10-31 13:07:19 -0700149} // namespace
150
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700151bool isEdid(const DisplayIdentificationData& data) {
152 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
153 return data.size() >= sizeof(kMagic) &&
154 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
155}
156
157std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100158 if (edid.size() < kEdidBlockSize) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700159 ALOGW("Invalid EDID: structure is truncated.");
160 // Attempt parsing even if EDID is malformed.
161 } else {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100162 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kEdidBlockSize,
163 static_cast<uint8_t>(0)),
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700164 "Invalid EDID: structure does not checksum.");
165 }
166
167 constexpr size_t kManufacturerOffset = 8;
168 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
169 ALOGE("Invalid EDID: manufacturer ID is truncated.");
170 return {};
171 }
172
173 // Plug and play ID encoded as big-endian 16-bit value.
174 const uint16_t manufacturerId =
Marin Shalamanova524a092020-07-27 21:39:55 +0200175 static_cast<uint16_t>((edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1]);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700176
177 const auto pnpId = getPnpId(manufacturerId);
178 if (!pnpId) {
179 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
180 return {};
181 }
182
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200183 constexpr size_t kProductIdOffset = 10;
184 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
185 ALOGE("Invalid EDID: product ID is truncated.");
186 return {};
187 }
Marin Shalamanova524a092020-07-27 21:39:55 +0200188 const uint16_t productId =
189 static_cast<uint16_t>(edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8));
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200190
191 constexpr size_t kManufactureWeekOffset = 16;
192 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
193 ALOGE("Invalid EDID: manufacture week is truncated.");
194 return {};
195 }
196 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
197 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
198 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
199
200 constexpr size_t kManufactureYearOffset = 17;
201 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
202 ALOGE("Invalid EDID: manufacture year is truncated.");
203 return {};
204 }
205 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
206 ALOGW_IF(manufactureOrModelYear <= 0xf,
207 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
208
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700209 constexpr size_t kDescriptorOffset = 54;
210 if (edid.size() < kDescriptorOffset) {
211 ALOGE("Invalid EDID: descriptors are missing.");
212 return {};
213 }
214
215 byte_view view(edid.data(), edid.size());
216 view.remove_prefix(kDescriptorOffset);
217
218 std::string_view displayName;
219 std::string_view serialNumber;
220 std::string_view asciiText;
221
222 constexpr size_t kDescriptorCount = 4;
223 constexpr size_t kDescriptorLength = 18;
224
225 for (size_t i = 0; i < kDescriptorCount; i++) {
226 if (view.size() < kDescriptorLength) {
227 break;
228 }
229
230 if (const auto type = getEdidDescriptorType(view)) {
231 byte_view descriptor(view.data(), kDescriptorLength);
232 descriptor.remove_prefix(kEdidHeaderLength);
233
234 switch (*type) {
235 case 0xfc:
236 displayName = parseEdidText(descriptor);
237 break;
238 case 0xfe:
239 asciiText = parseEdidText(descriptor);
240 break;
241 case 0xff:
242 serialNumber = parseEdidText(descriptor);
243 break;
244 }
245 }
246
247 view.remove_prefix(kDescriptorLength);
248 }
249
Dominik Laskowski17337962020-03-02 15:51:15 -0800250 std::string_view modelString = displayName;
251
252 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700253 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800254 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700255 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800256 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700257 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800258 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700259 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800260 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700261 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
262 return {};
263 }
264
Dominik Laskowski17337962020-03-02 15:51:15 -0800265 // Hash model string instead of using product code or (integer) serial number, since the latter
Jason Macnak4afe8572021-07-16 13:57:41 -0700266 // have been observed to change on some displays with multiple inputs. Use a stable hash instead
267 // of std::hash which is only required to be same within a single execution of a program.
268 const uint32_t modelHash = static_cast<uint32_t>(cityHash64Len0To16(modelString));
Dominik Laskowski17337962020-03-02 15:51:15 -0800269
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100270 // Parse extension blocks.
271 std::optional<Cea861ExtensionBlock> cea861Block;
272 if (edid.size() < kEdidBlockSize) {
273 ALOGW("Invalid EDID: block 0 is truncated.");
274 } else {
275 constexpr size_t kNumExtensionsOffset = 126;
276 const size_t numExtensions = edid[kNumExtensionsOffset];
277 view = byte_view(edid.data(), edid.size());
278 for (size_t blockNumber = 1; blockNumber <= numExtensions; blockNumber++) {
279 view.remove_prefix(kEdidBlockSize);
280 if (view.size() < kEdidBlockSize) {
281 ALOGW("Invalid EDID: block %zu is truncated.", blockNumber);
282 break;
283 }
284
285 const byte_view block(view.data(), kEdidBlockSize);
286 ALOGW_IF(std::accumulate(block.begin(), block.end(), static_cast<uint8_t>(0)),
287 "Invalid EDID: block %zu does not checksum.", blockNumber);
288 const uint8_t tag = block[0];
289
290 constexpr uint8_t kCea861BlockTag = 0x2;
291 if (tag == kCea861BlockTag) {
292 cea861Block = parseCea861Block(block);
293 } else {
294 ALOGV("Ignoring block number %zu with tag %x.", blockNumber, tag);
295 }
296 }
297 }
298
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200299 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200300 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800301 .pnpId = *pnpId,
302 .modelHash = modelHash,
303 .displayName = displayName,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100304 .manufactureOrModelYear = manufactureOrModelYear,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200305 .manufactureWeek = manufactureWeek,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100306 .cea861Block = cea861Block};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700307}
308
309std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
310 const char a = getPnpLetter<0>(manufacturerId);
311 const char b = getPnpLetter<1>(manufacturerId);
312 const char c = getPnpLetter<2>(manufacturerId);
313 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
314}
315
Marin Shalamanova524a092020-07-27 21:39:55 +0200316std::optional<PnpId> getPnpId(PhysicalDisplayId displayId) {
317 return getPnpId(displayId.getManufacturerId());
Dominik Laskowski34157762018-10-31 13:07:19 -0700318}
319
Dominik Laskowski075d3172018-05-24 15:50:06 -0700320std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
321 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700322 if (!isEdid(data)) {
323 ALOGE("Display identification data has unknown format.");
324 return {};
325 }
326
327 const auto edid = parseEdid(data);
328 if (!edid) {
329 return {};
330 }
331
Marin Shalamanova524a092020-07-27 21:39:55 +0200332 const auto displayId = PhysicalDisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
Dominik Laskowski17337962020-03-02 15:51:15 -0800333 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200334 .name = std::string(edid->displayName),
335 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700336}
337
Marin Shalamanova524a092020-07-27 21:39:55 +0200338PhysicalDisplayId getVirtualDisplayId(uint32_t id) {
339 return PhysicalDisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700340}
341
342} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800343