• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "screen_aod_plugin.h"
16 
17 namespace OHOS {
18 namespace Rosen {
19 namespace {
20     constexpr uint32_t SLEEP_TIME_US = 10000;
21 }
22 
23 static void *g_handle = nullptr;
24 using IsInAodFunc = bool (*)();
25 
LoadAodLib(void)26 bool LoadAodLib(void)
27 {
28     if (g_handle != nullptr) {
29         TLOGW(WmsLogTag::DMS, "aod plugin has already exits.");
30         return true;
31     }
32     int32_t cnt = 0;
33     int32_t retryTimes = 3;
34     do {
35         cnt++;
36         g_handle = dlopen(PLUGIN_AOD_SO_PATH.c_str(), RTLD_LAZY);
37         TLOGI(WmsLogTag::DMS, "dlopen %{public}s, retry cnt: %{public}d", PLUGIN_AOD_SO_PATH.c_str(), cnt);
38         usleep(SLEEP_TIME_US);
39     } while (!g_handle && cnt < retryTimes);
40     return g_handle != nullptr;
41 }
42 
UnloadAodLib(void)43 void UnloadAodLib(void)
44 {
45     TLOGI(WmsLogTag::DMS, "unload aod plugin.");
46     if (g_handle != nullptr) {
47         dlclose(g_handle);
48         g_handle = nullptr;
49     }
50 }
51 
IsInAod()52 __attribute__((no_sanitize("cfi"))) bool IsInAod()
53 {
54     if (g_handle == nullptr) {
55         TLOGE(WmsLogTag::DMS, "g_handle is nullptr");
56         return false;
57     }
58     IsInAodFunc func = (IsInAodFunc)(dlsym(g_handle, "IsInAod"));
59     const char* dlsymError = dlerror();
60     if  (dlsymError) {
61         TLOGE(WmsLogTag::DMS, "dlsym error: %{public}s", dlsymError);
62         return false;
63     }
64     return func();
65 }
66 }
67 }