blob: 20f3fd2ec1322d861e7f4b9bb1e364190c6613da [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
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -080017// TODO(b/129481165): remove the #pragma below and fix conversion issues
18#pragma clang diagnostic push
19#pragma clang diagnostic ignored "-Wconversion"
20
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070021#undef LOG_TAG
22#define LOG_TAG "DisplayIdentification"
23
24#include <algorithm>
25#include <cctype>
26#include <numeric>
27#include <optional>
28
29#include <log/log.h>
30
31#include "DisplayIdentification.h"
Jason Macnak072d2a02021-07-16 13:57:41 -070032#include "Hash.h"
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070033
34namespace android {
35namespace {
36
37using byte_view = std::basic_string_view<uint8_t>;
38
Marin Shalamanovf1274d42020-03-02 17:49:16 +010039constexpr size_t kEdidBlockSize = 128;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070040constexpr size_t kEdidHeaderLength = 5;
41
Dominik Laskowski075d3172018-05-24 15:50:06 -070042constexpr uint16_t kFallbackEdidManufacturerId = 0;
43constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu;
44
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070045std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) {
46 if (view.size() < kEdidHeaderLength || view[0] || view[1] || view[2] || view[4]) {
47 return {};
48 }
49
50 return view[3];
51}
52
53std::string_view parseEdidText(const byte_view& view) {
54 std::string_view text(reinterpret_cast<const char*>(view.data()), view.size());
55 text = text.substr(0, text.find('\n'));
56
57 if (!std::all_of(text.begin(), text.end(), ::isprint)) {
58 ALOGW("Invalid EDID: ASCII text is not printable.");
59 return {};
60 }
61
62 return text;
63}
64
65// Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001.
66template <size_t I>
67char getPnpLetter(uint16_t id) {
68 static_assert(I < 3);
69 const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1;
70 return letter < 'A' || letter > 'Z' ? '\0' : letter;
71}
72
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020073DeviceProductInfo buildDeviceProductInfo(const Edid& edid) {
74 DeviceProductInfo info;
75 std::copy(edid.displayName.begin(), edid.displayName.end(), info.name.begin());
76 info.name[edid.displayName.size()] = '\0';
77
78 const auto productId = std::to_string(edid.productId);
79 std::copy(productId.begin(), productId.end(), info.productId.begin());
80 info.productId[productId.size()] = '\0';
81 info.manufacturerPnpId = edid.pnpId;
82
83 constexpr uint8_t kModelYearFlag = 0xff;
84 constexpr uint32_t kYearOffset = 1990;
85
86 const auto year = edid.manufactureOrModelYear + kYearOffset;
87 if (edid.manufactureWeek == kModelYearFlag) {
88 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
89 } else if (edid.manufactureWeek == 0) {
90 DeviceProductInfo::ManufactureYear date;
91 date.year = year;
92 info.manufactureOrModelDate = date;
93 } else {
94 DeviceProductInfo::ManufactureWeekAndYear date;
95 date.year = year;
96 date.week = edid.manufactureWeek;
97 info.manufactureOrModelDate = date;
98 }
99
Marin Shalamanovdac1acb2020-04-06 16:11:25 +0200100 if (edid.cea861Block && edid.cea861Block->hdmiVendorDataBlock) {
101 const auto& address = edid.cea861Block->hdmiVendorDataBlock->physicalAddress;
102 info.relativeAddress = {address.a, address.b, address.c, address.d};
103 } else {
104 info.relativeAddress = DeviceProductInfo::NO_RELATIVE_ADDRESS;
105 }
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200106 return info;
107}
108
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100109Cea861ExtensionBlock parseCea861Block(const byte_view& block) {
110 Cea861ExtensionBlock cea861Block;
111
112 constexpr size_t kRevisionNumberOffset = 1;
113 cea861Block.revisionNumber = block[kRevisionNumberOffset];
114
115 constexpr size_t kDetailedTimingDescriptorsOffset = 2;
116 const size_t dtdStart =
117 std::min(kEdidBlockSize, static_cast<size_t>(block[kDetailedTimingDescriptorsOffset]));
118
119 // Parse data blocks.
120 for (size_t dataBlockOffset = 4; dataBlockOffset < dtdStart;) {
121 const uint8_t header = block[dataBlockOffset];
122 const uint8_t tag = header >> 5;
123 const size_t bodyLength = header & 0b11111;
124 constexpr size_t kDataBlockHeaderSize = 1;
125 const size_t dataBlockSize = bodyLength + kDataBlockHeaderSize;
126
127 if (block.size() < dataBlockOffset + dataBlockSize) {
128 ALOGW("Invalid EDID: CEA 861 data block is truncated.");
129 break;
130 }
131
132 const byte_view dataBlock(block.data() + dataBlockOffset, dataBlockSize);
133 constexpr uint8_t kVendorSpecificDataBlockTag = 0x3;
134
135 if (tag == kVendorSpecificDataBlockTag) {
136 const uint32_t ieeeRegistrationId =
137 dataBlock[1] | (dataBlock[2] << 8) | (dataBlock[3] << 16);
138 constexpr uint32_t kHdmiIeeeRegistrationId = 0xc03;
139
140 if (ieeeRegistrationId == kHdmiIeeeRegistrationId) {
141 const uint8_t a = dataBlock[4] >> 4;
142 const uint8_t b = dataBlock[4] & 0b1111;
143 const uint8_t c = dataBlock[5] >> 4;
144 const uint8_t d = dataBlock[5] & 0b1111;
145 cea861Block.hdmiVendorDataBlock =
146 HdmiVendorDataBlock{.physicalAddress = HdmiPhysicalAddress{a, b, c, d}};
147 } else {
148 ALOGV("Ignoring vendor specific data block for vendor with IEEE OUI %x",
149 ieeeRegistrationId);
150 }
151 } else {
152 ALOGV("Ignoring CEA-861 data block with tag %x", tag);
153 }
154 dataBlockOffset += bodyLength + kDataBlockHeaderSize;
155 }
156
157 return cea861Block;
158}
159
Dominik Laskowski34157762018-10-31 13:07:19 -0700160} // namespace
161
162uint16_t DisplayId::manufacturerId() const {
163 return static_cast<uint16_t>(value >> 40);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700164}
165
Dominik Laskowski17337962020-03-02 15:51:15 -0800166DisplayId DisplayId::fromEdid(uint8_t port, uint16_t manufacturerId, uint32_t modelHash) {
167 return {(static_cast<Type>(manufacturerId) << 40) | (static_cast<Type>(modelHash) << 8) | port};
Dominik Laskowski34157762018-10-31 13:07:19 -0700168}
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700169
170bool isEdid(const DisplayIdentificationData& data) {
171 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
172 return data.size() >= sizeof(kMagic) &&
173 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
174}
175
176std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100177 if (edid.size() < kEdidBlockSize) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700178 ALOGW("Invalid EDID: structure is truncated.");
179 // Attempt parsing even if EDID is malformed.
180 } else {
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100181 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kEdidBlockSize,
182 static_cast<uint8_t>(0)),
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700183 "Invalid EDID: structure does not checksum.");
184 }
185
186 constexpr size_t kManufacturerOffset = 8;
187 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
188 ALOGE("Invalid EDID: manufacturer ID is truncated.");
189 return {};
190 }
191
192 // Plug and play ID encoded as big-endian 16-bit value.
193 const uint16_t manufacturerId =
194 (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1];
195
196 const auto pnpId = getPnpId(manufacturerId);
197 if (!pnpId) {
198 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
199 return {};
200 }
201
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200202 constexpr size_t kProductIdOffset = 10;
203 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
204 ALOGE("Invalid EDID: product ID is truncated.");
205 return {};
206 }
207 const uint16_t productId = edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8);
208
209 constexpr size_t kManufactureWeekOffset = 16;
210 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
211 ALOGE("Invalid EDID: manufacture week is truncated.");
212 return {};
213 }
214 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
215 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
216 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
217
218 constexpr size_t kManufactureYearOffset = 17;
219 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
220 ALOGE("Invalid EDID: manufacture year is truncated.");
221 return {};
222 }
223 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
224 ALOGW_IF(manufactureOrModelYear <= 0xf,
225 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
226
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700227 constexpr size_t kDescriptorOffset = 54;
228 if (edid.size() < kDescriptorOffset) {
229 ALOGE("Invalid EDID: descriptors are missing.");
230 return {};
231 }
232
233 byte_view view(edid.data(), edid.size());
234 view.remove_prefix(kDescriptorOffset);
235
236 std::string_view displayName;
237 std::string_view serialNumber;
238 std::string_view asciiText;
239
240 constexpr size_t kDescriptorCount = 4;
241 constexpr size_t kDescriptorLength = 18;
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200242 static_assert(kDescriptorLength - kEdidHeaderLength < DeviceProductInfo::TEXT_BUFFER_SIZE);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700243
244 for (size_t i = 0; i < kDescriptorCount; i++) {
245 if (view.size() < kDescriptorLength) {
246 break;
247 }
248
249 if (const auto type = getEdidDescriptorType(view)) {
250 byte_view descriptor(view.data(), kDescriptorLength);
251 descriptor.remove_prefix(kEdidHeaderLength);
252
253 switch (*type) {
254 case 0xfc:
255 displayName = parseEdidText(descriptor);
256 break;
257 case 0xfe:
258 asciiText = parseEdidText(descriptor);
259 break;
260 case 0xff:
261 serialNumber = parseEdidText(descriptor);
262 break;
263 }
264 }
265
266 view.remove_prefix(kDescriptorLength);
267 }
268
Dominik Laskowski17337962020-03-02 15:51:15 -0800269 std::string_view modelString = displayName;
270
271 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700272 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800273 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700274 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800275 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700276 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800277 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700278 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800279 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700280 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
281 return {};
282 }
283
Dominik Laskowski17337962020-03-02 15:51:15 -0800284 // Hash model string instead of using product code or (integer) serial number, since the latter
Jason Macnak072d2a02021-07-16 13:57:41 -0700285 // have been observed to change on some displays with multiple inputs. Use a stable hash instead
286 // of std::hash which is only required to be same within a single execution of a program.
287 const uint32_t modelHash = static_cast<uint32_t>(cityHash64Len0To16(modelString));
Dominik Laskowski17337962020-03-02 15:51:15 -0800288
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100289 // Parse extension blocks.
290 std::optional<Cea861ExtensionBlock> cea861Block;
291 if (edid.size() < kEdidBlockSize) {
292 ALOGW("Invalid EDID: block 0 is truncated.");
293 } else {
294 constexpr size_t kNumExtensionsOffset = 126;
295 const size_t numExtensions = edid[kNumExtensionsOffset];
296 view = byte_view(edid.data(), edid.size());
297 for (size_t blockNumber = 1; blockNumber <= numExtensions; blockNumber++) {
298 view.remove_prefix(kEdidBlockSize);
299 if (view.size() < kEdidBlockSize) {
300 ALOGW("Invalid EDID: block %zu is truncated.", blockNumber);
301 break;
302 }
303
304 const byte_view block(view.data(), kEdidBlockSize);
305 ALOGW_IF(std::accumulate(block.begin(), block.end(), static_cast<uint8_t>(0)),
306 "Invalid EDID: block %zu does not checksum.", blockNumber);
307 const uint8_t tag = block[0];
308
309 constexpr uint8_t kCea861BlockTag = 0x2;
310 if (tag == kCea861BlockTag) {
311 cea861Block = parseCea861Block(block);
312 } else {
313 ALOGV("Ignoring block number %zu with tag %x.", blockNumber, tag);
314 }
315 }
316 }
317
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200318 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200319 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800320 .pnpId = *pnpId,
321 .modelHash = modelHash,
322 .displayName = displayName,
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100323 .manufactureOrModelYear = manufactureOrModelYear,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200324 .manufactureWeek = manufactureWeek,
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100325 .cea861Block = cea861Block};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700326}
327
328std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
329 const char a = getPnpLetter<0>(manufacturerId);
330 const char b = getPnpLetter<1>(manufacturerId);
331 const char c = getPnpLetter<2>(manufacturerId);
332 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
333}
334
Dominik Laskowski34157762018-10-31 13:07:19 -0700335std::optional<PnpId> getPnpId(DisplayId displayId) {
336 return getPnpId(displayId.manufacturerId());
337}
338
Dominik Laskowski075d3172018-05-24 15:50:06 -0700339std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
340 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700341 if (!isEdid(data)) {
342 ALOGE("Display identification data has unknown format.");
343 return {};
344 }
345
346 const auto edid = parseEdid(data);
347 if (!edid) {
348 return {};
349 }
350
Dominik Laskowski17337962020-03-02 15:51:15 -0800351 const auto displayId = DisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
352 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200353 .name = std::string(edid->displayName),
354 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700355}
356
357DisplayId getFallbackDisplayId(uint8_t port) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700358 return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700359}
360
361DisplayId getVirtualDisplayId(uint32_t id) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700362 return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700363}
364
365} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800366
367// TODO(b/129481165): remove the #pragma below and fix conversion issues
368#pragma clang diagnostic pop // ignored "-Wconversion"