blob: 9aaef6501d5e0a4d638151d92babb13e5382c0c1 [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;
73 std::copy(edid.displayName.begin(), edid.displayName.end(), info.name.begin());
74 info.name[edid.displayName.size()] = '\0';
75
76 const auto productId = std::to_string(edid.productId);
77 std::copy(productId.begin(), productId.end(), info.productId.begin());
78 info.productId[productId.size()] = '\0';
79 info.manufacturerPnpId = edid.pnpId;
80
81 constexpr uint8_t kModelYearFlag = 0xff;
82 constexpr uint32_t kYearOffset = 1990;
83
84 const auto year = edid.manufactureOrModelYear + kYearOffset;
85 if (edid.manufactureWeek == kModelYearFlag) {
86 info.manufactureOrModelDate = DeviceProductInfo::ModelYear{.year = year};
87 } else if (edid.manufactureWeek == 0) {
88 DeviceProductInfo::ManufactureYear date;
89 date.year = year;
90 info.manufactureOrModelDate = date;
91 } else {
92 DeviceProductInfo::ManufactureWeekAndYear date;
93 date.year = year;
94 date.week = edid.manufactureWeek;
95 info.manufactureOrModelDate = date;
96 }
97
98 return info;
99}
100
Dominik Laskowski34157762018-10-31 13:07:19 -0700101} // namespace
102
103uint16_t DisplayId::manufacturerId() const {
104 return static_cast<uint16_t>(value >> 40);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700105}
106
Dominik Laskowski34157762018-10-31 13:07:19 -0700107DisplayId DisplayId::fromEdid(uint8_t port, uint16_t manufacturerId, uint32_t displayNameHash) {
108 return {(static_cast<Type>(manufacturerId) << 40) | (static_cast<Type>(displayNameHash) << 8) |
109 port};
110}
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700111
112bool isEdid(const DisplayIdentificationData& data) {
113 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
114 return data.size() >= sizeof(kMagic) &&
115 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
116}
117
118std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
119 constexpr size_t kMinLength = 128;
120 if (edid.size() < kMinLength) {
121 ALOGW("Invalid EDID: structure is truncated.");
122 // Attempt parsing even if EDID is malformed.
123 } else {
124 ALOGW_IF(edid[126] != 0, "EDID extensions are currently unsupported.");
125 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kMinLength, static_cast<uint8_t>(0)),
126 "Invalid EDID: structure does not checksum.");
127 }
128
129 constexpr size_t kManufacturerOffset = 8;
130 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
131 ALOGE("Invalid EDID: manufacturer ID is truncated.");
132 return {};
133 }
134
135 // Plug and play ID encoded as big-endian 16-bit value.
136 const uint16_t manufacturerId =
137 (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1];
138
139 const auto pnpId = getPnpId(manufacturerId);
140 if (!pnpId) {
141 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
142 return {};
143 }
144
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200145 constexpr size_t kProductIdOffset = 10;
146 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
147 ALOGE("Invalid EDID: product ID is truncated.");
148 return {};
149 }
150 const uint16_t productId = edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8);
151
152 constexpr size_t kManufactureWeekOffset = 16;
153 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
154 ALOGE("Invalid EDID: manufacture week is truncated.");
155 return {};
156 }
157 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
158 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
159 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
160
161 constexpr size_t kManufactureYearOffset = 17;
162 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
163 ALOGE("Invalid EDID: manufacture year is truncated.");
164 return {};
165 }
166 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
167 ALOGW_IF(manufactureOrModelYear <= 0xf,
168 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
169
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700170 constexpr size_t kDescriptorOffset = 54;
171 if (edid.size() < kDescriptorOffset) {
172 ALOGE("Invalid EDID: descriptors are missing.");
173 return {};
174 }
175
176 byte_view view(edid.data(), edid.size());
177 view.remove_prefix(kDescriptorOffset);
178
179 std::string_view displayName;
180 std::string_view serialNumber;
181 std::string_view asciiText;
182
183 constexpr size_t kDescriptorCount = 4;
184 constexpr size_t kDescriptorLength = 18;
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200185 static_assert(kDescriptorLength - kEdidHeaderLength < DeviceProductInfo::TEXT_BUFFER_SIZE);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700186
187 for (size_t i = 0; i < kDescriptorCount; i++) {
188 if (view.size() < kDescriptorLength) {
189 break;
190 }
191
192 if (const auto type = getEdidDescriptorType(view)) {
193 byte_view descriptor(view.data(), kDescriptorLength);
194 descriptor.remove_prefix(kEdidHeaderLength);
195
196 switch (*type) {
197 case 0xfc:
198 displayName = parseEdidText(descriptor);
199 break;
200 case 0xfe:
201 asciiText = parseEdidText(descriptor);
202 break;
203 case 0xff:
204 serialNumber = parseEdidText(descriptor);
205 break;
206 }
207 }
208
209 view.remove_prefix(kDescriptorLength);
210 }
211
212 if (displayName.empty()) {
213 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
214 displayName = serialNumber;
215 }
216 if (displayName.empty()) {
217 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
218 displayName = asciiText;
219 }
220 if (displayName.empty()) {
221 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
222 return {};
223 }
224
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200225 return Edid{.manufacturerId = manufacturerId,
226 .pnpId = *pnpId,
227 .displayName = displayName,
228 .productId = productId,
229 .manufactureWeek = manufactureWeek,
230 .manufactureOrModelYear = manufactureOrModelYear};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700231}
232
233std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
234 const char a = getPnpLetter<0>(manufacturerId);
235 const char b = getPnpLetter<1>(manufacturerId);
236 const char c = getPnpLetter<2>(manufacturerId);
237 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
238}
239
Dominik Laskowski34157762018-10-31 13:07:19 -0700240std::optional<PnpId> getPnpId(DisplayId displayId) {
241 return getPnpId(displayId.manufacturerId());
242}
243
Dominik Laskowski075d3172018-05-24 15:50:06 -0700244std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
245 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700246 if (!isEdid(data)) {
247 ALOGE("Display identification data has unknown format.");
248 return {};
249 }
250
251 const auto edid = parseEdid(data);
252 if (!edid) {
253 return {};
254 }
255
256 // Hash display name instead of using product code or serial number, since the latter have been
257 // observed to change on some displays with multiple inputs.
258 const auto hash = static_cast<uint32_t>(std::hash<std::string_view>()(edid->displayName));
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200259 return DisplayIdentificationInfo{.id = DisplayId::fromEdid(port, edid->manufacturerId, hash),
260 .name = std::string(edid->displayName),
261 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700262}
263
264DisplayId getFallbackDisplayId(uint8_t port) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700265 return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700266}
267
268DisplayId getVirtualDisplayId(uint32_t id) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700269 return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700270}
271
272} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800273
274// TODO(b/129481165): remove the #pragma below and fix conversion issues
275#pragma clang diagnostic pop // ignored "-Wconversion"