blob: dcc413863e38782dd5a4984d45ad8fdfe180ced4 [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
34constexpr size_t kEdidHeaderLength = 5;
35
36std::optional<uint8_t> getEdidDescriptorType(const byte_view& view) {
37 if (view.size() < kEdidHeaderLength || view[0] || view[1] || view[2] || view[4]) {
38 return {};
39 }
40
41 return view[3];
42}
43
44std::string_view parseEdidText(const byte_view& view) {
45 std::string_view text(reinterpret_cast<const char*>(view.data()), view.size());
46 text = text.substr(0, text.find('\n'));
47
48 if (!std::all_of(text.begin(), text.end(), ::isprint)) {
49 ALOGW("Invalid EDID: ASCII text is not printable.");
50 return {};
51 }
52
53 return text;
54}
55
56// Big-endian 16-bit value encodes three 5-bit letters where A is 0b00001.
57template <size_t I>
58char getPnpLetter(uint16_t id) {
59 static_assert(I < 3);
60 const char letter = 'A' + (static_cast<uint8_t>(id >> ((2 - I) * 5)) & 0b00011111) - 1;
61 return letter < 'A' || letter > 'Z' ? '\0' : letter;
62}
63
64DisplayId getEdidDisplayId(uint8_t port, uint16_t manufacturerId, uint32_t displayNameHash) {
65 return (static_cast<DisplayId>(manufacturerId) << 40) |
66 (static_cast<DisplayId>(displayNameHash) << 8) | port;
67}
68
69} // namespace
70
71bool isEdid(const DisplayIdentificationData& data) {
72 const uint8_t kMagic[] = {0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0};
73 return data.size() >= sizeof(kMagic) &&
74 std::equal(std::begin(kMagic), std::end(kMagic), data.begin());
75}
76
77std::optional<Edid> parseEdid(const DisplayIdentificationData& edid) {
78 constexpr size_t kMinLength = 128;
79 if (edid.size() < kMinLength) {
80 ALOGW("Invalid EDID: structure is truncated.");
81 // Attempt parsing even if EDID is malformed.
82 } else {
83 ALOGW_IF(edid[126] != 0, "EDID extensions are currently unsupported.");
84 ALOGW_IF(std::accumulate(edid.begin(), edid.begin() + kMinLength, static_cast<uint8_t>(0)),
85 "Invalid EDID: structure does not checksum.");
86 }
87
88 constexpr size_t kManufacturerOffset = 8;
89 if (edid.size() < kManufacturerOffset + sizeof(uint16_t)) {
90 ALOGE("Invalid EDID: manufacturer ID is truncated.");
91 return {};
92 }
93
94 // Plug and play ID encoded as big-endian 16-bit value.
95 const uint16_t manufacturerId =
96 (edid[kManufacturerOffset] << 8) | edid[kManufacturerOffset + 1];
97
98 const auto pnpId = getPnpId(manufacturerId);
99 if (!pnpId) {
100 ALOGE("Invalid EDID: manufacturer ID is not a valid PnP ID.");
101 return {};
102 }
103
104 constexpr size_t kDescriptorOffset = 54;
105 if (edid.size() < kDescriptorOffset) {
106 ALOGE("Invalid EDID: descriptors are missing.");
107 return {};
108 }
109
110 byte_view view(edid.data(), edid.size());
111 view.remove_prefix(kDescriptorOffset);
112
113 std::string_view displayName;
114 std::string_view serialNumber;
115 std::string_view asciiText;
116
117 constexpr size_t kDescriptorCount = 4;
118 constexpr size_t kDescriptorLength = 18;
119
120 for (size_t i = 0; i < kDescriptorCount; i++) {
121 if (view.size() < kDescriptorLength) {
122 break;
123 }
124
125 if (const auto type = getEdidDescriptorType(view)) {
126 byte_view descriptor(view.data(), kDescriptorLength);
127 descriptor.remove_prefix(kEdidHeaderLength);
128
129 switch (*type) {
130 case 0xfc:
131 displayName = parseEdidText(descriptor);
132 break;
133 case 0xfe:
134 asciiText = parseEdidText(descriptor);
135 break;
136 case 0xff:
137 serialNumber = parseEdidText(descriptor);
138 break;
139 }
140 }
141
142 view.remove_prefix(kDescriptorLength);
143 }
144
145 if (displayName.empty()) {
146 ALOGW("Invalid EDID: falling back to serial number due to missing display name.");
147 displayName = serialNumber;
148 }
149 if (displayName.empty()) {
150 ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number.");
151 displayName = asciiText;
152 }
153 if (displayName.empty()) {
154 ALOGE("Invalid EDID: display name and fallback descriptors are missing.");
155 return {};
156 }
157
158 return Edid{manufacturerId, *pnpId, displayName};
159}
160
161std::optional<PnpId> getPnpId(uint16_t manufacturerId) {
162 const char a = getPnpLetter<0>(manufacturerId);
163 const char b = getPnpLetter<1>(manufacturerId);
164 const char c = getPnpLetter<2>(manufacturerId);
165 return a && b && c ? std::make_optional(PnpId{a, b, c}) : std::nullopt;
166}
167
168std::optional<DisplayId> generateDisplayId(uint8_t port, const DisplayIdentificationData& data) {
169 if (!isEdid(data)) {
170 ALOGE("Display identification data has unknown format.");
171 return {};
172 }
173
174 const auto edid = parseEdid(data);
175 if (!edid) {
176 return {};
177 }
178
179 // Hash display name instead of using product code or serial number, since the latter have been
180 // observed to change on some displays with multiple inputs.
181 const auto hash = static_cast<uint32_t>(std::hash<std::string_view>()(edid->displayName));
182 return getEdidDisplayId(port, edid->manufacturerId, hash);
183}
184
185} // namespace android