blob: 277081f2301c5f652249af5d48835d823942dafc [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
Dominik Laskowski34157762018-10-31 13:07:19 -070071} // namespace
72
73uint16_t DisplayId::manufacturerId() const {
74 return static_cast<uint16_t>(value >> 40);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070075}
76
Dominik Laskowski34157762018-10-31 13:07:19 -070077DisplayId DisplayId::fromEdid(uint8_t port, uint16_t manufacturerId, uint32_t displayNameHash) {
78 return {(static_cast<Type>(manufacturerId) << 40) | (static_cast<Type>(displayNameHash) << 8) |
79 port};
80}
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -070081
82bool isEdid(const DisplayIdentificationData& data) {
83 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
84 return data.size() >= sizeof(kMagic) &&
85 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
86}
87
88std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
89 constexpr size_t kMinLength = 128;
90 if (edid.size() < kMinLength) {
91 ALOGW("Invalid EDID: structure is truncated.");
92 // Attempt parsing even if EDID is malformed.
93 } else {
94 ALOGW_IF(edid[126] != 0, "EDID extensions are currently unsupported.");
95 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kMinLength, static_cast<uint8_t>(0)),
96 "Invalid EDID: structure does not checksum.");
97 }
98
99 constexpr size_t kManufacturerOffset = 8;
100 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
101 ALOGE("Invalid EDID: manufacturer ID is truncated.");
102 return {};
103 }
104
105 // Plug and play ID encoded as big-endian 16-bit value.
106 const uint16_t manufacturerId =
107 (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1];
108
109 const auto pnpId = getPnpId(manufacturerId);
110 if (!pnpId) {
111 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
112 return {};
113 }
114
115 constexpr size_t kDescriptorOffset = 54;
116 if (edid.size() < kDescriptorOffset) {
117 ALOGE("Invalid EDID: descriptors are missing.");
118 return {};
119 }
120
121 byte_view view(edid.data(), edid.size());
122 view.remove_prefix(kDescriptorOffset);
123
124 std::string_view displayName;
125 std::string_view serialNumber;
126 std::string_view asciiText;
127
128 constexpr size_t kDescriptorCount = 4;
129 constexpr size_t kDescriptorLength = 18;
130
131 for (size_t i = 0; i < kDescriptorCount; i++) {
132 if (view.size() < kDescriptorLength) {
133 break;
134 }
135
136 if (const auto type = getEdidDescriptorType(view)) {
137 byte_view descriptor(view.data(), kDescriptorLength);
138 descriptor.remove_prefix(kEdidHeaderLength);
139
140 switch (*type) {
141 case 0xfc:
142 displayName = parseEdidText(descriptor);
143 break;
144 case 0xfe:
145 asciiText = parseEdidText(descriptor);
146 break;
147 case 0xff:
148 serialNumber = parseEdidText(descriptor);
149 break;
150 }
151 }
152
153 view.remove_prefix(kDescriptorLength);
154 }
155
156 if (displayName.empty()) {
157 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
158 displayName = serialNumber;
159 }
160 if (displayName.empty()) {
161 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
162 displayName = asciiText;
163 }
164 if (displayName.empty()) {
165 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
166 return {};
167 }
168
169 return Edid{manufacturerId, *pnpId, displayName};
170}
171
172std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
173 const char a = getPnpLetter<0>(manufacturerId);
174 const char b = getPnpLetter<1>(manufacturerId);
175 const char c = getPnpLetter<2>(manufacturerId);
176 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
177}
178
Dominik Laskowski34157762018-10-31 13:07:19 -0700179std::optional<PnpId> getPnpId(DisplayId displayId) {
180 return getPnpId(displayId.manufacturerId());
181}
182
Dominik Laskowski075d3172018-05-24 15:50:06 -0700183std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData(
184 uint8_t port, const DisplayIdentificationData& data) {
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700185 if (!isEdid(data)) {
186 ALOGE("Display identification data has unknown format.");
187 return {};
188 }
189
190 const auto edid = parseEdid(data);
191 if (!edid) {
192 return {};
193 }
194
195 // Hash display name instead of using product code or serial number, since the latter have been
196 // observed to change on some displays with multiple inputs.
197 const auto hash = static_cast<uint32_t>(std::hash<std::string_view>()(edid->displayName));
Dominik Laskowski34157762018-10-31 13:07:19 -0700198 return DisplayIdentificationInfo{DisplayId::fromEdid(port, edid->manufacturerId, hash),
Dominik Laskowski075d3172018-05-24 15:50:06 -0700199 std::string(edid->displayName)};
200}
201
202DisplayId getFallbackDisplayId(uint8_t port) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700203 return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0);
Dominik Laskowski075d3172018-05-24 15:50:06 -0700204}
205
206DisplayId getVirtualDisplayId(uint32_t id) {
Dominik Laskowski34157762018-10-31 13:07:19 -0700207 return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id);
Dominik Laskowskie9ef7c42018-03-12 19:34:30 -0700208}
209
210} // namespace android
Ady Abrahamb0dbdaa2020-01-06 16:19:42 -0800211
212// TODO(b/129481165): remove the #pragma below and fix conversion issues
213#pragma clang diagnostic pop // ignored "-Wconversion"