blob: 98209bb9e4718f2a119b309a7554d48c0d696ae2 [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"
28
29namespace android {
30namespace {
31
32using byte_view = std::basic_string_view<uint8_t>;
33
Marin Shalamanov7a9ba302020-03-02 17:49:16 +010034constexpr size_t kEdidBlockSize = 128;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070035constexpr size_t kEdidHeaderLength = 5;
36
Dominik Laskowski075d3172018-05-24 15:50:06 -070037constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu;
38
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070039std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) {
40 if (view.size() < kEdidHeaderLength || view[0] || view[1] || view[2] || view[4]) {
41 return {};
42 }
43
44 return view[3];
45}
46
47std::string_view parseEdidText(const byte_view& view) {
48 std::string_view text(reinterpret_cast<const char*>(view.data()), view.size());
49 text = text.substr(0, text.find('\n'));
50
51 if (!std::all_of(text.begin(), text.end(), ::isprint)) {
52 ALOGW("Invalid EDID: ASCII text is not printable.");
53 return {};
54 }
55
56 return text;
57}
58
59// Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001.
60template <size_t I>
61char getPnpLetter(uint16_t id) {
62 static_assert(I < 3);
63 const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1;
64 return letter < 'A' || letter > 'Z' ? '\0' : letter;
65}
66
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020067DeviceProductInfo buildDeviceProductInfo(const Edid& edid) {
68 DeviceProductInfo info;
Marin Shalamanov359a7e72020-02-17 17:03:07 +010069 info.name.assign(edid.displayName);
70 info.productId = std::to_string(edid.productId);
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020071 info.manufacturerPnpId = edid.pnpId;
72
73 constexpr uint8_t kModelYearFlag = 0xff;
74 constexpr uint32_t kYearOffset = 1990;
75
76 const auto year = edid.manufactureOrModelYear + kYearOffset;
77 if (edid.manufactureWeek == kModelYearFlag) {
78 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
79 } else if (edid.manufactureWeek == 0) {
80 DeviceProductInfo::ManufactureYear date;
81 date.year = year;
82 info.manufactureOrModelDate = date;
83 } else {
84 DeviceProductInfo::ManufactureWeekAndYear date;
85 date.year = year;
86 date.week = edid.manufactureWeek;
87 info.manufactureOrModelDate = date;
88 }
89
Marin Shalamanov896e6302020-04-06 16:11:25 +020090 if (edid.cea861Block && edid.cea861Block->hdmiVendorDataBlock) {
91 const auto& address = edid.cea861Block->hdmiVendorDataBlock->physicalAddress;
92 info.relativeAddress = {address.a, address.b, address.c, address.d};
93 }
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020094 return info;
95}
96
Marin Shalamanov7a9ba302020-03-02 17:49:16 +010097Cea861ExtensionBlock parseCea861Block(const byte_view& block) {
98 Cea861ExtensionBlock cea861Block;
99
100 constexpr size_t kRevisionNumberOffset = 1;
101 cea861Block.revisionNumber = block[kRevisionNumberOffset];
102
103 constexpr size_t kDetailedTimingDescriptorsOffset = 2;
104 const size_t dtdStart =
105 std::min(kEdidBlockSize, static_cast<size_t>(block[kDetailedTimingDescriptorsOffset]));
106
107 // Parse data blocks.
108 for (size_t dataBlockOffset = 4; dataBlockOffset < dtdStart;) {
109 const uint8_t header = block[dataBlockOffset];
110 const uint8_t tag = header >> 5;
111 const size_t bodyLength = header & 0b11111;
112 constexpr size_t kDataBlockHeaderSize = 1;
113 const size_t dataBlockSize = bodyLength + kDataBlockHeaderSize;
114
115 if (block.size() < dataBlockOffset + dataBlockSize) {
116 ALOGW("Invalid EDID: CEA 861 data block is truncated.");
117 break;
118 }
119
120 const byte_view dataBlock(block.data() + dataBlockOffset, dataBlockSize);
121 constexpr uint8_t kVendorSpecificDataBlockTag = 0x3;
122
123 if (tag == kVendorSpecificDataBlockTag) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200124 const uint32_t ieeeRegistrationId = static_cast<uint32_t>(
125 dataBlock[1] | (dataBlock[2] << 8) | (dataBlock[3] << 16));
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100126 constexpr uint32_t kHdmiIeeeRegistrationId = 0xc03;
127
128 if (ieeeRegistrationId == kHdmiIeeeRegistrationId) {
129 const uint8_t a = dataBlock[4] >> 4;
130 const uint8_t b = dataBlock[4] & 0b1111;
131 const uint8_t c = dataBlock[5] >> 4;
132 const uint8_t d = dataBlock[5] & 0b1111;
133 cea861Block.hdmiVendorDataBlock =
134 HdmiVendorDataBlock{.physicalAddress = HdmiPhysicalAddress{a, b, c, d}};
135 } else {
136 ALOGV("Ignoring vendor specific data block for vendor with IEEE OUI %x",
137 ieeeRegistrationId);
138 }
139 } else {
140 ALOGV("Ignoring CEA-861 data block with tag %x", tag);
141 }
142 dataBlockOffset += bodyLength + kDataBlockHeaderSize;
143 }
144
145 return cea861Block;
146}
147
Dominik Laskowski34157762018-10-31 13:07:19 -0700148} // namespace
149
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700150bool isEdid(const DisplayIdentificationData& data) {
151 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
152 return data.size() >= sizeof(kMagic) &&
153 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
154}
155
156std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100157 if (edid.size() < kEdidBlockSize) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700158 ALOGW("Invalid EDID: structure is truncated.");
159 // Attempt parsing even if EDID is malformed.
160 } else {
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100161 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kEdidBlockSize,
162 static_cast<uint8_t>(0)),
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700163 "Invalid EDID: structure does not checksum.");
164 }
165
166 constexpr size_t kManufacturerOffset = 8;
167 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
168 ALOGE("Invalid EDID: manufacturer ID is truncated.");
169 return {};
170 }
171
172 // Plug and play ID encoded as big-endian 16-bit value.
173 const uint16_t manufacturerId =
Marin Shalamanova524a092020-07-27 21:39:55 +0200174 static_cast<uint16_t>((edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1]);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700175
176 const auto pnpId = getPnpId(manufacturerId);
177 if (!pnpId) {
178 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
179 return {};
180 }
181
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200182 constexpr size_t kProductIdOffset = 10;
183 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
184 ALOGE("Invalid EDID: product ID is truncated.");
185 return {};
186 }
Marin Shalamanova524a092020-07-27 21:39:55 +0200187 const uint16_t productId =
188 static_cast<uint16_t>(edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8));
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200189
190 constexpr size_t kManufactureWeekOffset = 16;
191 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
192 ALOGE("Invalid EDID: manufacture week is truncated.");
193 return {};
194 }
195 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
196 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
197 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
198
199 constexpr size_t kManufactureYearOffset = 17;
200 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
201 ALOGE("Invalid EDID: manufacture year is truncated.");
202 return {};
203 }
204 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
205 ALOGW_IF(manufactureOrModelYear <= 0xf,
206 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
207
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700208 constexpr size_t kDescriptorOffset = 54;
209 if (edid.size() < kDescriptorOffset) {
210 ALOGE("Invalid EDID: descriptors are missing.");
211 return {};
212 }
213
214 byte_view view(edid.data(), edid.size());
215 view.remove_prefix(kDescriptorOffset);
216
217 std::string_view displayName;
218 std::string_view serialNumber;
219 std::string_view asciiText;
220
221 constexpr size_t kDescriptorCount = 4;
222 constexpr size_t kDescriptorLength = 18;
223
224 for (size_t i = 0; i < kDescriptorCount; i++) {
225 if (view.size() < kDescriptorLength) {
226 break;
227 }
228
229 if (const auto type = getEdidDescriptorType(view)) {
230 byte_view descriptor(view.data(), kDescriptorLength);
231 descriptor.remove_prefix(kEdidHeaderLength);
232
233 switch (*type) {
234 case 0xfc:
235 displayName = parseEdidText(descriptor);
236 break;
237 case 0xfe:
238 asciiText = parseEdidText(descriptor);
239 break;
240 case 0xff:
241 serialNumber = parseEdidText(descriptor);
242 break;
243 }
244 }
245
246 view.remove_prefix(kDescriptorLength);
247 }
248
Dominik Laskowski17337962020-03-02 15:51:15 -0800249 std::string_view modelString = displayName;
250
251 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700252 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800253 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700254 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800255 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700256 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800257 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700258 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800259 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700260 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
261 return {};
262 }
263
Dominik Laskowski17337962020-03-02 15:51:15 -0800264 // Hash model string instead of using product code or (integer) serial number, since the latter
265 // have been observed to change on some displays with multiple inputs.
266 const auto modelHash = static_cast<uint32_t>(std::hash<std::string_view>()(modelString));
267
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100268 // Parse extension blocks.
269 std::optional<Cea861ExtensionBlock> cea861Block;
270 if (edid.size() < kEdidBlockSize) {
271 ALOGW("Invalid EDID: block 0 is truncated.");
272 } else {
273 constexpr size_t kNumExtensionsOffset = 126;
274 const size_t numExtensions = edid[kNumExtensionsOffset];
275 view = byte_view(edid.data(), edid.size());
276 for (size_t blockNumber = 1; blockNumber <= numExtensions; blockNumber++) {
277 view.remove_prefix(kEdidBlockSize);
278 if (view.size() < kEdidBlockSize) {
279 ALOGW("Invalid EDID: block %zu is truncated.", blockNumber);
280 break;
281 }
282
283 const byte_view block(view.data(), kEdidBlockSize);
284 ALOGW_IF(std::accumulate(block.begin(), block.end(), static_cast<uint8_t>(0)),
285 "Invalid EDID: block %zu does not checksum.", blockNumber);
286 const uint8_t tag = block[0];
287
288 constexpr uint8_t kCea861BlockTag = 0x2;
289 if (tag == kCea861BlockTag) {
290 cea861Block = parseCea861Block(block);
291 } else {
292 ALOGV("Ignoring block number %zu with tag %x.", blockNumber, tag);
293 }
294 }
295 }
296
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200297 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200298 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800299 .pnpId = *pnpId,
300 .modelHash = modelHash,
301 .displayName = displayName,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100302 .manufactureOrModelYear = manufactureOrModelYear,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200303 .manufactureWeek = manufactureWeek,
Marin Shalamanov7a9ba302020-03-02 17:49:16 +0100304 .cea861Block = cea861Block};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700305}
306
307std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
308 const char a = getPnpLetter<0>(manufacturerId);
309 const char b = getPnpLetter<1>(manufacturerId);
310 const char c = getPnpLetter<2>(manufacturerId);
311 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
312}
313
Marin Shalamanova524a092020-07-27 21:39:55 +0200314std::optional<PnpId> getPnpId(PhysicalDisplayId displayId) {
315 return getPnpId(displayId.getManufacturerId());
Dominik Laskowski34157762018-10-31 13:07:19 -0700316}
317
Dominik Laskowski075d3172018-05-24 15:50:06 -0700318std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
319 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700320 if (!isEdid(data)) {
321 ALOGE("Display identification data has unknown format.");
322 return {};
323 }
324
325 const auto edid = parseEdid(data);
326 if (!edid) {
327 return {};
328 }
329
Marin Shalamanova524a092020-07-27 21:39:55 +0200330 const auto displayId = PhysicalDisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
Dominik Laskowski17337962020-03-02 15:51:15 -0800331 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200332 .name = std::string(edid->displayName),
333 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700334}
335
Marin Shalamanova524a092020-07-27 21:39:55 +0200336PhysicalDisplayId getVirtualDisplayId(uint32_t id) {
337 return PhysicalDisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700338}
339
340} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800341