DisplayIdentification.cpp 6.5 KB

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