blob: f24f3142f1fc7c97294638e43f7fb7d8e3a2e3a7 [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 Laskowski17337962020-03-02 15:51:15 -0800107DisplayId DisplayId::fromEdid(uint8_t port, uint16_t manufacturerId, uint32_t modelHash) {
108 return {(static_cast<Type>(manufacturerId) << 40) | (static_cast<Type>(modelHash) << 8) | port};
Dominik Laskowski34157762018-10-31 13:07:19 -0700109}
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700110
111bool isEdid(const DisplayIdentificationData& data) {
112 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
113 return data.size() >= sizeof(kMagic) &&
114 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
115}
116
117std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
118 constexpr size_t kMinLength = 128;
119 if (edid.size() < kMinLength) {
120 ALOGW("Invalid EDID: structure is truncated.");
121 // Attempt parsing even if EDID is malformed.
122 } else {
123 ALOGW_IF(edid[126] != 0, "EDID extensions are currently unsupported.");
124 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kMinLength, static_cast<uint8_t>(0)),
125 "Invalid EDID: structure does not checksum.");
126 }
127
128 constexpr size_t kManufacturerOffset = 8;
129 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
130 ALOGE("Invalid EDID: manufacturer ID is truncated.");
131 return {};
132 }
133
134 // Plug and play ID encoded as big-endian 16-bit value.
135 const uint16_t manufacturerId =
136 (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1];
137
138 const auto pnpId = getPnpId(manufacturerId);
139 if (!pnpId) {
140 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
141 return {};
142 }
143
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200144 constexpr size_t kProductIdOffset = 10;
145 if (edid.size() < kProductIdOffset + sizeof(uint16_t)) {
146 ALOGE("Invalid EDID: product ID is truncated.");
147 return {};
148 }
149 const uint16_t productId = edid[kProductIdOffset] | (edid[kProductIdOffset + 1] << 8);
150
151 constexpr size_t kManufactureWeekOffset = 16;
152 if (edid.size() < kManufactureWeekOffset + sizeof(uint8_t)) {
153 ALOGE("Invalid EDID: manufacture week is truncated.");
154 return {};
155 }
156 const uint8_t manufactureWeek = edid[kManufactureWeekOffset];
157 ALOGW_IF(0x37 <= manufactureWeek && manufactureWeek <= 0xfe,
158 "Invalid EDID: week of manufacture cannot be in the range [0x37, 0xfe].");
159
160 constexpr size_t kManufactureYearOffset = 17;
161 if (edid.size() < kManufactureYearOffset + sizeof(uint8_t)) {
162 ALOGE("Invalid EDID: manufacture year is truncated.");
163 return {};
164 }
165 const uint8_t manufactureOrModelYear = edid[kManufactureYearOffset];
166 ALOGW_IF(manufactureOrModelYear <= 0xf,
167 "Invalid EDID: model year or manufacture year cannot be in the range [0x0, 0xf].");
168
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700169 constexpr size_t kDescriptorOffset = 54;
170 if (edid.size() < kDescriptorOffset) {
171 ALOGE("Invalid EDID: descriptors are missing.");
172 return {};
173 }
174
175 byte_view view(edid.data(), edid.size());
176 view.remove_prefix(kDescriptorOffset);
177
178 std::string_view displayName;
179 std::string_view serialNumber;
180 std::string_view asciiText;
181
182 constexpr size_t kDescriptorCount = 4;
183 constexpr size_t kDescriptorLength = 18;
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200184 static_assert(kDescriptorLength - kEdidHeaderLength < DeviceProductInfo::TEXT_BUFFER_SIZE);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700185
186 for (size_t i = 0; i < kDescriptorCount; i++) {
187 if (view.size() < kDescriptorLength) {
188 break;
189 }
190
191 if (const auto type = getEdidDescriptorType(view)) {
192 byte_view descriptor(view.data(), kDescriptorLength);
193 descriptor.remove_prefix(kEdidHeaderLength);
194
195 switch (*type) {
196 case 0xfc:
197 displayName = parseEdidText(descriptor);
198 break;
199 case 0xfe:
200 asciiText = parseEdidText(descriptor);
201 break;
202 case 0xff:
203 serialNumber = parseEdidText(descriptor);
204 break;
205 }
206 }
207
208 view.remove_prefix(kDescriptorLength);
209 }
210
Dominik Laskowski17337962020-03-02 15:51:15 -0800211 std::string_view modelString = displayName;
212
213 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700214 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800215 modelString = serialNumber;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700216 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800217 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700218 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
Dominik Laskowski17337962020-03-02 15:51:15 -0800219 modelString = asciiText;
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700220 }
Dominik Laskowski17337962020-03-02 15:51:15 -0800221 if (modelString.empty()) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700222 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
223 return {};
224 }
225
Dominik Laskowski17337962020-03-02 15:51:15 -0800226 // Hash model string instead of using product code or (integer) serial number, since the latter
227 // have been observed to change on some displays with multiple inputs.
228 const auto modelHash = static_cast<uint32_t>(std::hash<std::string_view>()(modelString));
229
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200230 return Edid{.manufacturerId = manufacturerId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200231 .productId = productId,
Dominik Laskowski17337962020-03-02 15:51:15 -0800232 .pnpId = *pnpId,
233 .modelHash = modelHash,
234 .displayName = displayName,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200235 .manufactureWeek = manufactureWeek,
236 .manufactureOrModelYear = manufactureOrModelYear};
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700237}
238
239std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
240 const char a = getPnpLetter<0>(manufacturerId);
241 const char b = getPnpLetter<1>(manufacturerId);
242 const char c = getPnpLetter<2>(manufacturerId);
243 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
244}
245
Dominik Laskowski34157762018-10-31 13:07:19 -0700246std::optional<PnpId> getPnpId(DisplayId displayId) {
247 return getPnpId(displayId.manufacturerId());
248}
249
Dominik Laskowski075d3172018-05-24 15:50:06 -0700250std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
251 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700252 if (!isEdid(data)) {
253 ALOGE("Display identification data has unknown format.");
254 return {};
255 }
256
257 const auto edid = parseEdid(data);
258 if (!edid) {
259 return {};
260 }
261
Dominik Laskowski17337962020-03-02 15:51:15 -0800262 const auto displayId = DisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash);
263 return DisplayIdentificationInfo{.id = displayId,
Marin Shalamanovf5de90d2019-10-08 10:57:25 +0200264 .name = std::string(edid->displayName),
265 .deviceProductInfo = buildDeviceProductInfo(*edid)};
Dominik Laskowski075d3172018-05-24 15:50:06 -0700266}
267
268DisplayId getFallbackDisplayId(uint8_t port) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700269 return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700270}
271
272DisplayId getVirtualDisplayId(uint32_t id) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700273 return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700274}
275
276} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800277
278// TODO(b/129481165): remove the #pragma below and fix conversion issues
279#pragma clang diagnostic pop // ignored "-Wconversion"