Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 17 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 18 | #pragma clang diagnostic push |
| 19 | #pragma clang diagnostic ignored "-Wconversion" |
| 20 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 21 | #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 | |
| 33 | namespace android { |
| 34 | namespace { |
| 35 | |
| 36 | using byte_view = std::basic_string_view<uint8_t>; |
| 37 | |
| 38 | constexpr size_t kEdidHeaderLength = 5; |
| 39 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 40 | constexpr uint16_t kFallbackEdidManufacturerId = 0; |
| 41 | constexpr uint16_t kVirtualEdidManufacturerId = 0xffffu; |
| 42 | |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 43 | std::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 | |
| 51 | std::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. |
| 64 | template <size_t I> |
| 65 | char 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 Shalamanov | f5de90d | 2019-10-08 10:57:25 +0200 | [diff] [blame] | 71 | DeviceProductInfo 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 Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 101 | } // namespace |
| 102 | |
| 103 | uint16_t DisplayId::manufacturerId() const { |
| 104 | return static_cast<uint16_t>(value >> 40); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Dominik Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 107 | DisplayId 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 Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 109 | } |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 110 | |
| 111 | bool 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 | |
| 117 | std::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 Shalamanov | f5de90d | 2019-10-08 10:57:25 +0200 | [diff] [blame] | 144 | 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 Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 169 | 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 Shalamanov | f5de90d | 2019-10-08 10:57:25 +0200 | [diff] [blame] | 184 | static_assert(kDescriptorLength - kEdidHeaderLength < DeviceProductInfo::TEXT_BUFFER_SIZE); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 185 | |
| 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 Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 211 | std::string_view modelString = displayName; |
| 212 | |
| 213 | if (modelString.empty()) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 214 | ALOGW("Invalid EDID: falling back to serial number due to missing display name."); |
Dominik Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 215 | modelString = serialNumber; |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 216 | } |
Dominik Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 217 | if (modelString.empty()) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 218 | ALOGW("Invalid EDID: falling back to ASCII text due to missing serial number."); |
Dominik Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 219 | modelString = asciiText; |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 220 | } |
Dominik Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 221 | if (modelString.empty()) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 222 | ALOGE("Invalid EDID: display name and fallback descriptors are missing."); |
| 223 | return {}; |
| 224 | } |
| 225 | |
Dominik Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 226 | // 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 Shalamanov | f5de90d | 2019-10-08 10:57:25 +0200 | [diff] [blame] | 230 | return Edid{.manufacturerId = manufacturerId, |
Marin Shalamanov | f5de90d | 2019-10-08 10:57:25 +0200 | [diff] [blame] | 231 | .productId = productId, |
Dominik Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 232 | .pnpId = *pnpId, |
| 233 | .modelHash = modelHash, |
| 234 | .displayName = displayName, |
Marin Shalamanov | f5de90d | 2019-10-08 10:57:25 +0200 | [diff] [blame] | 235 | .manufactureWeek = manufactureWeek, |
| 236 | .manufactureOrModelYear = manufactureOrModelYear}; |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | std::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 Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 246 | std::optional<PnpId> getPnpId(DisplayId displayId) { |
| 247 | return getPnpId(displayId.manufacturerId()); |
| 248 | } |
| 249 | |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 250 | std::optional<DisplayIdentificationInfo> parseDisplayIdentificationData( |
| 251 | uint8_t port, const DisplayIdentificationData& data) { |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 252 | 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 Laskowski | 1733796 | 2020-03-02 15:51:15 -0800 | [diff] [blame^] | 262 | const auto displayId = DisplayId::fromEdid(port, edid->manufacturerId, edid->modelHash); |
| 263 | return DisplayIdentificationInfo{.id = displayId, |
Marin Shalamanov | f5de90d | 2019-10-08 10:57:25 +0200 | [diff] [blame] | 264 | .name = std::string(edid->displayName), |
| 265 | .deviceProductInfo = buildDeviceProductInfo(*edid)}; |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | DisplayId getFallbackDisplayId(uint8_t port) { |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 269 | return DisplayId::fromEdid(port, kFallbackEdidManufacturerId, 0); |
Dominik Laskowski | 075d317 | 2018-05-24 15:50:06 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | DisplayId getVirtualDisplayId(uint32_t id) { |
Dominik Laskowski | 3415776 | 2018-10-31 13:07:19 -0700 | [diff] [blame] | 273 | return DisplayId::fromEdid(0, kVirtualEdidManufacturerId, id); |
Dominik Laskowski | e9ef7c4 | 2018-03-12 19:34:30 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | } // namespace android |
Ady Abraham | b0dbdaa | 2020-01-06 16:19:42 -0800 | [diff] [blame] | 277 | |
| 278 | // TODO(b/129481165): remove the #pragma below and fix conversion issues |
| 279 | #pragma clang diagnostic pop // ignored "-Wconversion" |