blob: 63897cc102bae58b5aec0d15f1bfa1814acf667e [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
38constexpr size_t kEdidHeaderLength = 5;
39
Dominik Laskowski075d3172018-05-24 15:50:06 -070040constexpr uint16_t kFallbackEdidManufacturerId = 0;
41constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu;
42
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070043std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) {
44 if (view.size() < kEdidHeaderLength || view[0] || view[1] || view[2] || view[4]) {
45 return {};
46 }
47
48 return view[3];
49}
50
51std::string_view parseEdidText(const byte_view& view) {
52 std::string_view text(reinterpret_cast<const char*>(view.data()), view.size());
53 text = text.substr(0, text.find('\n'));
54
55 if (!std::all_of(text.begin(), text.end(), ::isprint)) {
56 ALOGW("Invalid EDID: ASCII text is not printable.");
57 return {};
58 }
59
60 return text;
61}
62
63// Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001.
64template <size_t I>
65char getPnpLetter(uint16_t id) {
66 static_assert(I < 3);
67 const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1;
68 return letter < 'A' || letter > 'Z' ? '\0' : letter;
69}
70
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020071DeviceProductInfo buildDeviceProductInfo(const Edid& edid) {
72 DeviceProductInfo info;
Marin Shalamanov359a7e72020-02-17 17:03:07 +010073 info.name.assign(edid.displayName);
74 info.productId = std::to_string(edid.productId);
Marin Shalamanovf5de90d2019-10-08 10:57:25 +020075 info.manufacturerPnpId = edid.pnpId;
76
77 constexpr uint8_t kModelYearFlag = 0xff;
78 constexpr uint32_t kYearOffset = 1990;
79
80 const auto year = edid.manufactureOrModelYear + kYearOffset;
81 if (edid.manufactureWeek == kModelYearFlag) {
82 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
83 } else if (edid.manufactureWeek == 0) {
84 DeviceProductInfo::ManufactureYear date;
85 date.year = year;
86 info.manufactureOrModelDate = date;
87 } else {
88 DeviceProductInfo::ManufactureWeekAndYear date;
89 date.year = year;
90 date.week = edid.manufactureWeek;
91 info.manufactureOrModelDate = date;
92 }
93
94 return info;
95}
96
Dominik Laskowski34157762018-10-31 13:07:19 -070097} // namespace
98
99uint16_t DisplayId::manufacturerId() const {
100 return static_cast<uint16_t>(value >> 40);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700101}
102
Dominik Laskowski17337962020-03-02 15:51:15 -0800103DisplayId DisplayId::fromEdid(uint8_t port, uint16_t manufacturerId, uint32_t modelHash) {
104 return {(static_cast<Type>(manufacturerId) << 40) | (static_cast<Type>(modelHash) << 8) | port};
Dominik Laskowski34157762018-10-31 13:07:19 -0700105}
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700106
107bool isEdid(const DisplayIdentificationData& data) {
108 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
109 return data.size() >= sizeof(kMagic) &&
110 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
111}
112
113std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
114 constexpr size_t kMinLength = 128;
115 if (edid.size() < kMinLength) {
116 ALOGW("Invalid EDID: structure is truncated.");
117 // Attempt parsing even if EDID is malformed.
118 } else {
119 ALOGW_IF(edid[126] != 0, "EDID extensions are currently unsupported.");
120 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kMinLength, static_cast<uint8_t>(0)),
121 "Invalid EDID: structure does not checksum.");
122 }
123
124 constexpr size_t kManufacturerOffset = 8;
125 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
126 ALOGE("Invalid EDID: manufacturer ID is truncated.");
127 return {};
128 }
129
130 // Plug and play ID encoded as big-endian 16-bit value.
131 const uint16_t manufacturerId =
132 (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1];
133
134 const auto pnpId = getPnpId(manufacturerId);
135 if (!pnpId) {
136 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
137 return {};
138 }
139
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200140 constexpr size_t kProductIdOffset = 10;
141 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
142 ALOGE("Invalid EDID: product ID is truncated.");
143 return {};
144 }
145 const uint16_t productId = edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8);
146
147 constexpr size_t kManufactureWeekOffset = 16;
148 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
149 ALOGE("Invalid EDID: manufacture week is truncated.");
150 return {};
151 }
152 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
153 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
154 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
155
156 constexpr size_t kManufactureYearOffset = 17;
157 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
158 ALOGE("Invalid EDID: manufacture year is truncated.");
159 return {};
160 }
161 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
162 ALOGW_IF(manufactureOrModelYear <= 0xf,
163 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
164
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700165 constexpr size_t kDescriptorOffset = 54;
166 if (edid.size() < kDescriptorOffset) {
167 ALOGE("Invalid EDID: descriptors are missing.");
168 return {};
169 }
170
171 byte_view view(edid.data(), edid.size());
172 view.remove_prefix(kDescriptorOffset);
173
174 std::string_view displayName;
175 std::string_view serialNumber;
176 std::string_view asciiText;
177
178 constexpr size_t kDescriptorCount = 4;
179 constexpr size_t kDescriptorLength = 18;
180
181 for (size_t i = 0; i < kDescriptorCount; i++) {
182 if (view.size() < kDescriptorLength) {
183 break;
184 }
185
186 if (const auto type = getEdidDescriptorType(view)) {
187 byte_view descriptor(view.data(), kDescriptorLength);
188 descriptor.remove_prefix(kEdidHeaderLength);
189
190 switch (*type) {
191 case 0xfc:
192 displayName = parseEdidText(descriptor);
193 break;
194 case 0xfe:
195 asciiText = parseEdidText(descriptor);
196 break;
197 case 0xff:
198 serialNumber = parseEdidText(descriptor);
199 break;
200 }
201 }
202
203 view.remove_prefix(kDescriptorLength);
204 }
205
Dominik Laskowski17337962020-03-02 15:51:15 -0800206 std::string_view modelString = displayName;
207
208 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700209 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800210 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700211 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800212 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700213 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800214 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700215 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800216 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700217 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
218 return {};
219 }
220
Dominik Laskowski17337962020-03-02 15:51:15 -0800221 // Hash model string instead of using product code or (integer) serial number, since the latter
222 // have been observed to change on some displays with multiple inputs.
223 const auto modelHash = static_cast<uint32_t>(std::hash<std::string_view>()(modelString));
224
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200225 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200226 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800227 .pnpId = *pnpId,
228 .modelHash = modelHash,
229 .displayName = displayName,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200230 .manufactureWeek = manufactureWeek,
231 .manufactureOrModelYear = manufactureOrModelYear};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700232}
233
234std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
235 const char a = getPnpLetter<0>(manufacturerId);
236 const char b = getPnpLetter<1>(manufacturerId);
237 const char c = getPnpLetter<2>(manufacturerId);
238 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
239}
240
Dominik Laskowski34157762018-10-31 13:07:19 -0700241std::optional<PnpId> getPnpId(DisplayId displayId) {
242 return getPnpId(displayId.manufacturerId());
243}
244
Dominik Laskowski075d3172018-05-24 15:50:06 -0700245std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
246 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700247 if (!isEdid(data)) {
248 ALOGE("Display identification data has unknown format.");
249 return {};
250 }
251
252 const auto edid = parseEdid(data);
253 if (!edid) {
254 return {};
255 }
256
Dominik Laskowski17337962020-03-02 15:51:15 -0800257 const auto displayId = DisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
258 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200259 .name = std::string(edid->displayName),
260 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700261}
262
263DisplayId getFallbackDisplayId(uint8_t port) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700264 return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700265}
266
267DisplayId getVirtualDisplayId(uint32_t id) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700268 return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700269}
270
271} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800272
273// TODO(b/129481165): remove the #pragma below and fix conversion issues
274#pragma clang diagnostic pop // ignored "-Wconversion"