1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "screen_edid_parse.h"
17
18 namespace OHOS {
19 namespace Rosen {
20 namespace {
21 constexpr uint32_t CHECK_INDEX_BEGIN = 0x8;
22 constexpr uint32_t CHECK_INDEX_END = 0x11;
23 constexpr int32_t CHECK_CODE_INVALID = -1;
24 constexpr uint32_t SLEEP_TIME_US = 10000;
25 }
26
27 static void *g_libHandle = nullptr;
28
LoadEdidPlugin(void)29 bool LoadEdidPlugin(void)
30 {
31 if (g_libHandle != nullptr) {
32 TLOGW(WmsLogTag::DMS, "edid plugin has already exists.");
33 return true;
34 }
35 int32_t cnt = 0;
36 int32_t retryTimes = 3;
37 do {
38 cnt++;
39 g_libHandle = dlopen(EDID_PARSE_SO_PATH.c_str(), RTLD_LAZY);
40 TLOGI(WmsLogTag::DMS, "dlopen %{public}s, retry cnt: %{public}d", EDID_PARSE_SO_PATH.c_str(), cnt);
41 if (g_libHandle == nullptr) {
42 TLOGE(WmsLogTag::DMS, "dlopen failed: %{public}s", dlerror());
43 }
44 usleep(SLEEP_TIME_US);
45 } while (!g_libHandle && cnt < retryTimes);
46 return g_libHandle != nullptr;
47 }
48
UnloadEdidPlugin(void)49 void UnloadEdidPlugin(void)
50 {
51 TLOGI(WmsLogTag::DMS, "unload edid plugin.");
52 if (g_libHandle != nullptr) {
53 dlclose(g_libHandle);
54 g_libHandle = nullptr;
55 }
56 }
57
GetEdid(ScreenId screenId,struct BaseEdid & edid)58 bool GetEdid(ScreenId screenId, struct BaseEdid &edid)
59 {
60 std::vector<uint8_t> edidData;
61 uint8_t outPort;
62 int getEdidFromRS = RSInterfaces::GetInstance().GetDisplayIdentificationData(screenId, outPort, edidData);
63 if (getEdidFromRS != 0) {
64 TLOGE(WmsLogTag::DMS, "get EDID from RS failed.");
65 return false;
66 }
67 uint32_t edidSize = static_cast<uint32_t>(edidData.size());
68 int32_t checkCode = GetEdidCheckCode(edidData);
69 TLOGW(WmsLogTag::DMS, "EDID data size: %{public}u, check code: %{public}d",
70 edidSize, checkCode);
71 if (!LoadEdidPlugin()) {
72 TLOGE(WmsLogTag::DMS, "dlopen failed.");
73 return false;
74 }
75 ParseEdidFunc ParseBaseEdid = (ParseEdidFunc)(dlsym(g_libHandle, "ParseBaseEdid"));
76 if (ParseBaseEdid == nullptr) {
77 TLOGE(WmsLogTag::DMS, "ParseBaseEdid null.");
78 UnloadEdidPlugin();
79 return false;
80 }
81 int32_t getEdid = ParseBaseEdid(edidData.data(), edidSize, &edid);
82 if (getEdid != 0) {
83 TLOGE(WmsLogTag::DMS, "parse EDID failed.");
84 return false;
85 }
86 return true;
87 }
88
GetEdidCheckCode(const std::vector<uint8_t> & edidData)89 int32_t GetEdidCheckCode(const std::vector<uint8_t>& edidData)
90 {
91 int32_t checkCode = 0;
92 if (edidData.size() <= CHECK_INDEX_END) {
93 TLOGE(WmsLogTag::DMS, "EDID data size not enough!");
94 return CHECK_CODE_INVALID;
95 }
96 for (uint32_t i = CHECK_INDEX_BEGIN; i <= CHECK_INDEX_END; i++) {
97 checkCode += edidData[i];
98 }
99 return checkCode;
100 }
101 }
102 }