blob: f9281a7c992eafb2636ded3799dbeeee260faa8a [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
95 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) {
125 const uint32_t ieeeRegistrationId =
126 dataBlock[1] | (dataBlock[2] << 8) | (dataBlock[3] << 16);
127 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
151uint16_t DisplayId::manufacturerId() const {
152 return static_cast<uint16_t>(value >> 40);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700153}
154
Dominik Laskowski17337962020-03-02 15:51:15 -0800155DisplayId DisplayId::fromEdid(uint8_t port, uint16_t manufacturerId, uint32_t modelHash) {
156 return {(static_cast<Type>(manufacturerId) << 40) | (static_cast<Type>(modelHash) << 8) | port};
Dominik Laskowski34157762018-10-31 13:07:19 -0700157}
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700158
159bool isEdid(const DisplayIdentificationData& data) {
160 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
161 return data.size() >= sizeof(kMagic) &&
162 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
163}
164
165std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100166 if (edid.size() < kEdidBlockSize) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700167 ALOGW("Invalid EDID: structure is truncated.");
168 // Attempt parsing even if EDID is malformed.
169 } else {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100170 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kEdidBlockSize,
171 static_cast<uint8_t>(0)),
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700172 "Invalid EDID: structure does not checksum.");
173 }
174
175 constexpr size_t kManufacturerOffset = 8;
176 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
177 ALOGE("Invalid EDID: manufacturer ID is truncated.");
178 return {};
179 }
180
181 // Plug and play ID encoded as big-endian 16-bit value.
182 const uint16_t manufacturerId =
183 (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1];
184
185 const auto pnpId = getPnpId(manufacturerId);
186 if (!pnpId) {
187 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
188 return {};
189 }
190
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200191 constexpr size_t kProductIdOffset = 10;
192 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
193 ALOGE("Invalid EDID: product ID is truncated.");
194 return {};
195 }
196 const uint16_t productId = edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8);
197
198 constexpr size_t kManufactureWeekOffset = 16;
199 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
200 ALOGE("Invalid EDID: manufacture week is truncated.");
201 return {};
202 }
203 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
204 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
205 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
206
207 constexpr size_t kManufactureYearOffset = 17;
208 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
209 ALOGE("Invalid EDID: manufacture year is truncated.");
210 return {};
211 }
212 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
213 ALOGW_IF(manufactureOrModelYear <= 0xf,
214 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
215
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700216 constexpr size_t kDescriptorOffset = 54;
217 if (edid.size() < kDescriptorOffset) {
218 ALOGE("Invalid EDID: descriptors are missing.");
219 return {};
220 }
221
222 byte_view view(edid.data(), edid.size());
223 view.remove_prefix(kDescriptorOffset);
224
225 std::string_view displayName;
226 std::string_view serialNumber;
227 std::string_view asciiText;
228
229 constexpr size_t kDescriptorCount = 4;
230 constexpr size_t kDescriptorLength = 18;
231
232 for (size_t i = 0; i < kDescriptorCount; i++) {
233 if (view.size() < kDescriptorLength) {
234 break;
235 }
236
237 if (const auto type = getEdidDescriptorType(view)) {
238 byte_view descriptor(view.data(), kDescriptorLength);
239 descriptor.remove_prefix(kEdidHeaderLength);
240
241 switch (*type) {
242 case 0xfc:
243 displayName = parseEdidText(descriptor);
244 break;
245 case 0xfe:
246 asciiText = parseEdidText(descriptor);
247 break;
248 case 0xff:
249 serialNumber = parseEdidText(descriptor);
250 break;
251 }
252 }
253
254 view.remove_prefix(kDescriptorLength);
255 }
256
Dominik Laskowski17337962020-03-02 15:51:15 -0800257 std::string_view modelString = displayName;
258
259 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700260 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800261 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700262 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800263 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700264 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800265 modelString = asciiText;
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 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
269 return {};
270 }
271
Dominik Laskowski17337962020-03-02 15:51:15 -0800272 // Hash model string instead of using product code or (integer) serial number, since the latter
273 // have been observed to change on some displays with multiple inputs.
274 const auto modelHash = static_cast<uint32_t>(std::hash<std::string_view>()(modelString));
275
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100276 // Parse extension blocks.
277 std::optional<Cea861ExtensionBlock> cea861Block;
278 if (edid.size() < kEdidBlockSize) {
279 ALOGW("Invalid EDID: block 0 is truncated.");
280 } else {
281 constexpr size_t kNumExtensionsOffset = 126;
282 const size_t numExtensions = edid[kNumExtensionsOffset];
283 view = byte_view(edid.data(), edid.size());
284 for (size_t blockNumber = 1; blockNumber <= numExtensions; blockNumber++) {
285 view.remove_prefix(kEdidBlockSize);
286 if (view.size() < kEdidBlockSize) {
287 ALOGW("Invalid EDID: block %zu is truncated.", blockNumber);
288 break;
289 }
290
291 const byte_view block(view.data(), kEdidBlockSize);
292 ALOGW_IF(std::accumulate(block.begin(), block.end(), static_cast<uint8_t>(0)),
293 "Invalid EDID: block %zu does not checksum.", blockNumber);
294 const uint8_t tag = block[0];
295
296 constexpr uint8_t kCea861BlockTag = 0x2;
297 if (tag == kCea861BlockTag) {
298 cea861Block = parseCea861Block(block);
299 } else {
300 ALOGV("Ignoring block number %zu with tag %x.", blockNumber, tag);
301 }
302 }
303 }
304
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200305 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200306 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800307 .pnpId = *pnpId,
308 .modelHash = modelHash,
309 .displayName = displayName,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100310 .manufactureOrModelYear = manufactureOrModelYear,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200311 .manufactureWeek = manufactureWeek,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100312 .cea861Block = cea861Block};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700313}
314
315std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
316 const char a = getPnpLetter<0>(manufacturerId);
317 const char b = getPnpLetter<1>(manufacturerId);
318 const char c = getPnpLetter<2>(manufacturerId);
319 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
320}
321
Dominik Laskowski34157762018-10-31 13:07:19 -0700322std::optional<PnpId> getPnpId(DisplayId displayId) {
323 return getPnpId(displayId.manufacturerId());
324}
325
Dominik Laskowski075d3172018-05-24 15:50:06 -0700326std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
327 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700328 if (!isEdid(data)) {
329 ALOGE("Display identification data has unknown format.");
330 return {};
331 }
332
333 const auto edid = parseEdid(data);
334 if (!edid) {
335 return {};
336 }
337
Dominik Laskowski17337962020-03-02 15:51:15 -0800338 const auto displayId = DisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
339 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200340 .name = std::string(edid->displayName),
341 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700342}
343
344DisplayId getFallbackDisplayId(uint8_t port) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700345 return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700346}
347
348DisplayId getVirtualDisplayId(uint32_t id) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700349 return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700350}
351
352} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800353
354// TODO(b/129481165): remove the #pragma below and fix conversion issues
355#pragma clang diagnostic pop // ignored "-Wconversion"