blob: b6d904f9f9a00d4f152965b291dd8bed7b7dfbca [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 Shalamanovf1274d42020-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;
74 std::copy(edid.displayName.begin(), edid.displayName.end(), info.name.begin());
75 info.name[edid.displayName.size()] = '\0';
76
77 const auto productId = std::to_string(edid.productId);
78 std::copy(productId.begin(), productId.end(), info.productId.begin());
79 info.productId[productId.size()] = '\0';
80 info.manufacturerPnpId = edid.pnpId;
81
82 constexpr uint8_t kModelYearFlag = 0xff;
83 constexpr uint32_t kYearOffset = 1990;
84
85 const auto year = edid.manufactureOrModelYear + kYearOffset;
86 if (edid.manufactureWeek == kModelYearFlag) {
87 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
88 } else if (edid.manufactureWeek == 0) {
89 DeviceProductInfo::ManufactureYear date;
90 date.year = year;
91 info.manufactureOrModelDate = date;
92 } else {
93 DeviceProductInfo::ManufactureWeekAndYear date;
94 date.year = year;
95 date.week = edid.manufactureWeek;
96 info.manufactureOrModelDate = date;
97 }
98
99 return info;
100}
101
Marin Shalamanovf1274d42020-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 Shalamanovf1274d42020-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 Shalamanovf1274d42020-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;
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200235 static_assert(kDescriptorLength - kEdidHeaderLength < DeviceProductInfo::TEXT_BUFFER_SIZE);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700236
237 for (size_t i = 0; i < kDescriptorCount; i++) {
238 if (view.size() < kDescriptorLength) {
239 break;
240 }
241
242 if (const auto type = getEdidDescriptorType(view)) {
243 byte_view descriptor(view.data(), kDescriptorLength);
244 descriptor.remove_prefix(kEdidHeaderLength);
245
246 switch (*type) {
247 case 0xfc:
248 displayName = parseEdidText(descriptor);
249 break;
250 case 0xfe:
251 asciiText = parseEdidText(descriptor);
252 break;
253 case 0xff:
254 serialNumber = parseEdidText(descriptor);
255 break;
256 }
257 }
258
259 view.remove_prefix(kDescriptorLength);
260 }
261
Dominik Laskowski17337962020-03-02 15:51:15 -0800262 std::string_view modelString = displayName;
263
264 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700265 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800266 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700267 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800268 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700269 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800270 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700271 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800272 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700273 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
274 return {};
275 }
276
Dominik Laskowski17337962020-03-02 15:51:15 -0800277 // Hash model string instead of using product code or (integer) serial number, since the latter
278 // have been observed to change on some displays with multiple inputs.
279 const auto modelHash = static_cast<uint32_t>(std::hash<std::string_view>()(modelString));
280
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100281 // Parse extension blocks.
282 std::optional<Cea861ExtensionBlock> cea861Block;
283 if (edid.size() < kEdidBlockSize) {
284 ALOGW("Invalid EDID: block 0 is truncated.");
285 } else {
286 constexpr size_t kNumExtensionsOffset = 126;
287 const size_t numExtensions = edid[kNumExtensionsOffset];
288 view = byte_view(edid.data(), edid.size());
289 for (size_t blockNumber = 1; blockNumber <= numExtensions; blockNumber++) {
290 view.remove_prefix(kEdidBlockSize);
291 if (view.size() < kEdidBlockSize) {
292 ALOGW("Invalid EDID: block %zu is truncated.", blockNumber);
293 break;
294 }
295
296 const byte_view block(view.data(), kEdidBlockSize);
297 ALOGW_IF(std::accumulate(block.begin(), block.end(), static_cast<uint8_t>(0)),
298 "Invalid EDID: block %zu does not checksum.", blockNumber);
299 const uint8_t tag = block[0];
300
301 constexpr uint8_t kCea861BlockTag = 0x2;
302 if (tag == kCea861BlockTag) {
303 cea861Block = parseCea861Block(block);
304 } else {
305 ALOGV("Ignoring block number %zu with tag %x.", blockNumber, tag);
306 }
307 }
308 }
309
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200310 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200311 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800312 .pnpId = *pnpId,
313 .modelHash = modelHash,
314 .displayName = displayName,
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100315 .manufactureOrModelYear = manufactureOrModelYear,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200316 .manufactureWeek = manufactureWeek,
Marin Shalamanovf1274d42020-03-02 17:49:16 +0100317 .cea861Block = cea861Block};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700318}
319
320std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
321 const char a = getPnpLetter<0>(manufacturerId);
322 const char b = getPnpLetter<1>(manufacturerId);
323 const char c = getPnpLetter<2>(manufacturerId);
324 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
325}
326
Dominik Laskowski34157762018-10-31 13:07:19 -0700327std::optional<PnpId> getPnpId(DisplayId displayId) {
328 return getPnpId(displayId.manufacturerId());
329}
330
Dominik Laskowski075d3172018-05-24 15:50:06 -0700331std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
332 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700333 if (!isEdid(data)) {
334 ALOGE("Display identification data has unknown format.");
335 return {};
336 }
337
338 const auto edid = parseEdid(data);
339 if (!edid) {
340 return {};
341 }
342
Dominik Laskowski17337962020-03-02 15:51:15 -0800343 const auto displayId = DisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
344 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200345 .name = std::string(edid->displayName),
346 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700347}
348
349DisplayId getFallbackDisplayId(uint8_t port) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700350 return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700351}
352
353DisplayId getVirtualDisplayId(uint32_t id) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700354 return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700355}
356
357} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800358
359// TODO(b/129481165): remove the #pragma below and fix conversion issues
360#pragma clang diagnostic pop // ignored "-Wconversion"