blob: 52a6380ebdf3ad7dcadcce5902ed48392f345c17 [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"
32
33namespace android {
34namespace {
35
36using byte_view = std::basic_string_view<uint8_t>;
37
Marin Shalamanov7a9ba302020-03-02 17:49:16 +010038constexpr size_t kEdidBlockSize = 128;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070039constexpr size_t kEdidHeaderLength = 5;
40
Dominik Laskowski075d3172018-05-24 15:50:06 -070041constexpr uint16_t kFallbackEdidManufacturerId = 0;
42constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu;
43
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070044std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) {
45 if (view.size() < kEdidHeaderLength || view[0] || view[1] || view[2] || view[4]) {
46 return {};
47 }
48
49 return view[3];
50}
51
52std::string_view parseEdidText(const byte_view& view) {
53 std::string_view text(reinterpret_cast<const char*>(view.data()), view.size());
54 text = text.substr(0, text.find('\n'));
55
56 if (!std::all_of(text.begin(), text.end(), ::isprint)) {
57 ALOGW("Invalid EDID: ASCII text is not printable.");
58 return {};
59 }
60
61 return text;
62}
63
64// Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001.
65template <size_t I>
66char getPnpLetter(uint16_t id) {
67 static_assert(I < 3);
68 const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1;
69 return letter < 'A' || letter > 'Z' ? '\0' : letter;
70}
71
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020072DeviceProductInfo buildDeviceProductInfo(const Edid& edid) {
73 DeviceProductInfo info;
Marin Shalamanov359a7e72020-02-17 17:03:07 +010074 info.name.assign(edid.displayName);
75 info.productId = std::to_string(edid.productId);
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020076 info.manufacturerPnpId = edid.pnpId;
77
78 constexpr uint8_t kModelYearFlag = 0xff;
79 constexpr uint32_t kYearOffset = 1990;
80
81 const auto year = edid.manufactureOrModelYear + kYearOffset;
82 if (edid.manufactureWeek == kModelYearFlag) {
83 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
84 } else if (edid.manufactureWeek == 0) {
85 DeviceProductInfo::ManufactureYear date;
86 date.year = year;
87 info.manufactureOrModelDate = date;
88 } else {
89 DeviceProductInfo::ManufactureWeekAndYear date;
90 date.year = year;
91 date.week = edid.manufactureWeek;
92 info.manufactureOrModelDate = date;
93 }
94
Marin Shalamanov896e6302020-04-06 16:11:25 +020095 if (edid.cea861Block && edid.cea861Block->hdmiVendorDataBlock) {
96 const auto& address = edid.cea861Block->hdmiVendorDataBlock->physicalAddress;
97 info.relativeAddress = {address.a, address.b, address.c, address.d};
98 }
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020099 return info;
100}
101
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100102Cea861ExtensionBlock parseCea861Block(const byte_view& block) {
103 Cea861ExtensionBlock cea861Block;
104
105 constexpr size_t kRevisionNumberOffset = 1;
106 cea861Block.revisionNumber = block[kRevisionNumberOffset];
107
108 constexpr size_t kDetailedTimingDescriptorsOffset = 2;
109 const size_t dtdStart =
110 std::min(kEdidBlockSize, static_cast<size_t>(block[kDetailedTimingDescriptorsOffset]));
111
112 // Parse data blocks.
113 for (size_t dataBlockOffset = 4; dataBlockOffset < dtdStart;) {
114 const uint8_t header = block[dataBlockOffset];
115 const uint8_t tag = header >> 5;
116 const size_t bodyLength = header & 0b11111;
117 constexpr size_t kDataBlockHeaderSize = 1;
118 const size_t dataBlockSize = bodyLength + kDataBlockHeaderSize;
119
120 if (block.size() < dataBlockOffset + dataBlockSize) {
121 ALOGW("Invalid EDID: CEA 861 data block is truncated.");
122 break;
123 }
124
125 const byte_view dataBlock(block.data() + dataBlockOffset, dataBlockSize);
126 constexpr uint8_t kVendorSpecificDataBlockTag = 0x3;
127
128 if (tag == kVendorSpecificDataBlockTag) {
129 const uint32_t ieeeRegistrationId =
130 dataBlock[1] | (dataBlock[2] << 8) | (dataBlock[3] << 16);
131 constexpr uint32_t kHdmiIeeeRegistrationId = 0xc03;
132
133 if (ieeeRegistrationId == kHdmiIeeeRegistrationId) {
134 const uint8_t a = dataBlock[4] >> 4;
135 const uint8_t b = dataBlock[4] & 0b1111;
136 const uint8_t c = dataBlock[5] >> 4;
137 const uint8_t d = dataBlock[5] & 0b1111;
138 cea861Block.hdmiVendorDataBlock =
139 HdmiVendorDataBlock{.physicalAddress = HdmiPhysicalAddress{a, b, c, d}};
140 } else {
141 ALOGV("Ignoring vendor specific data block for vendor with IEEE OUI %x",
142 ieeeRegistrationId);
143 }
144 } else {
145 ALOGV("Ignoring CEA-861 data block with tag %x", tag);
146 }
147 dataBlockOffset += bodyLength + kDataBlockHeaderSize;
148 }
149
150 return cea861Block;
151}
152
Dominik Laskowski34157762018-10-31 13:07:19 -0700153} // namespace
154
155uint16_t DisplayId::manufacturerId() const {
156 return static_cast<uint16_t>(value >> 40);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700157}
158
Dominik Laskowski17337962020-03-02 15:51:15 -0800159DisplayId DisplayId::fromEdid(uint8_t port, uint16_t manufacturerId, uint32_t modelHash) {
160 return {(static_cast<Type>(manufacturerId) << 40) | (static_cast<Type>(modelHash) << 8) | port};
Dominik Laskowski34157762018-10-31 13:07:19 -0700161}
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700162
163bool isEdid(const DisplayIdentificationData& data) {
164 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
165 return data.size() >= sizeof(kMagic) &&
166 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
167}
168
169std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100170 if (edid.size() < kEdidBlockSize) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700171 ALOGW("Invalid EDID: structure is truncated.");
172 // Attempt parsing even if EDID is malformed.
173 } else {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100174 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kEdidBlockSize,
175 static_cast<uint8_t>(0)),
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700176 "Invalid EDID: structure does not checksum.");
177 }
178
179 constexpr size_t kManufacturerOffset = 8;
180 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
181 ALOGE("Invalid EDID: manufacturer ID is truncated.");
182 return {};
183 }
184
185 // Plug and play ID encoded as big-endian 16-bit value.
186 const uint16_t manufacturerId =
187 (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1];
188
189 const auto pnpId = getPnpId(manufacturerId);
190 if (!pnpId) {
191 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
192 return {};
193 }
194
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200195 constexpr size_t kProductIdOffset = 10;
196 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
197 ALOGE("Invalid EDID: product ID is truncated.");
198 return {};
199 }
200 const uint16_t productId = edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8);
201
202 constexpr size_t kManufactureWeekOffset = 16;
203 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
204 ALOGE("Invalid EDID: manufacture week is truncated.");
205 return {};
206 }
207 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
208 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
209 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
210
211 constexpr size_t kManufactureYearOffset = 17;
212 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
213 ALOGE("Invalid EDID: manufacture year is truncated.");
214 return {};
215 }
216 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
217 ALOGW_IF(manufactureOrModelYear <= 0xf,
218 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
219
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700220 constexpr size_t kDescriptorOffset = 54;
221 if (edid.size() < kDescriptorOffset) {
222 ALOGE("Invalid EDID: descriptors are missing.");
223 return {};
224 }
225
226 byte_view view(edid.data(), edid.size());
227 view.remove_prefix(kDescriptorOffset);
228
229 std::string_view displayName;
230 std::string_view serialNumber;
231 std::string_view asciiText;
232
233 constexpr size_t kDescriptorCount = 4;
234 constexpr size_t kDescriptorLength = 18;
235
236 for (size_t i = 0; i < kDescriptorCount; i++) {
237 if (view.size() < kDescriptorLength) {
238 break;
239 }
240
241 if (const auto type = getEdidDescriptorType(view)) {
242 byte_view descriptor(view.data(), kDescriptorLength);
243 descriptor.remove_prefix(kEdidHeaderLength);
244
245 switch (*type) {
246 case 0xfc:
247 displayName = parseEdidText(descriptor);
248 break;
249 case 0xfe:
250 asciiText = parseEdidText(descriptor);
251 break;
252 case 0xff:
253 serialNumber = parseEdidText(descriptor);
254 break;
255 }
256 }
257
258 view.remove_prefix(kDescriptorLength);
259 }
260
Dominik Laskowski17337962020-03-02 15:51:15 -0800261 std::string_view modelString = displayName;
262
263 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700264 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800265 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700266 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800267 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700268 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800269 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700270 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800271 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700272 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
273 return {};
274 }
275
Dominik Laskowski17337962020-03-02 15:51:15 -0800276 // Hash model string instead of using product code or (integer) serial number, since the latter
277 // have been observed to change on some displays with multiple inputs.
278 const auto modelHash = static_cast<uint32_t>(std::hash<std::string_view>()(modelString));
279
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100280 // Parse extension blocks.
281 std::optional<Cea861ExtensionBlock> cea861Block;
282 if (edid.size() < kEdidBlockSize) {
283 ALOGW("Invalid EDID: block 0 is truncated.");
284 } else {
285 constexpr size_t kNumExtensionsOffset = 126;
286 const size_t numExtensions = edid[kNumExtensionsOffset];
287 view = byte_view(edid.data(), edid.size());
288 for (size_t blockNumber = 1; blockNumber <= numExtensions; blockNumber++) {
289 view.remove_prefix(kEdidBlockSize);
290 if (view.size() < kEdidBlockSize) {
291 ALOGW("Invalid EDID: block %zu is truncated.", blockNumber);
292 break;
293 }
294
295 const byte_view block(view.data(), kEdidBlockSize);
296 ALOGW_IF(std::accumulate(block.begin(), block.end(), static_cast<uint8_t>(0)),
297 "Invalid EDID: block %zu does not checksum.", blockNumber);
298 const uint8_t tag = block[0];
299
300 constexpr uint8_t kCea861BlockTag = 0x2;
301 if (tag == kCea861BlockTag) {
302 cea861Block = parseCea861Block(block);
303 } else {
304 ALOGV("Ignoring block number %zu with tag %x.", blockNumber, tag);
305 }
306 }
307 }
308
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200309 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200310 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800311 .pnpId = *pnpId,
312 .modelHash = modelHash,
313 .displayName = displayName,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100314 .manufactureOrModelYear = manufactureOrModelYear,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200315 .manufactureWeek = manufactureWeek,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100316 .cea861Block = cea861Block};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700317}
318
319std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
320 const char a = getPnpLetter<0>(manufacturerId);
321 const char b = getPnpLetter<1>(manufacturerId);
322 const char c = getPnpLetter<2>(manufacturerId);
323 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
324}
325
Dominik Laskowski34157762018-10-31 13:07:19 -0700326std::optional<PnpId> getPnpId(DisplayId displayId) {
327 return getPnpId(displayId.manufacturerId());
328}
329
Dominik Laskowski075d3172018-05-24 15:50:06 -0700330std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
331 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700332 if (!isEdid(data)) {
333 ALOGE("Display identification data has unknown format.");
334 return {};
335 }
336
337 const auto edid = parseEdid(data);
338 if (!edid) {
339 return {};
340 }
341
Dominik Laskowski17337962020-03-02 15:51:15 -0800342 const auto displayId = DisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
343 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200344 .name = std::string(edid->displayName),
345 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700346}
347
348DisplayId getFallbackDisplayId(uint8_t port) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700349 return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700350}
351
352DisplayId getVirtualDisplayId(uint32_t id) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700353 return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700354}
355
356} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800357
358// TODO(b/129481165): remove the #pragma below and fix conversion issues
359#pragma clang diagnostic pop // ignored "-Wconversion"