• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_sensor_plugin.h"
17 
18 namespace OHOS {
19 namespace Rosen {
20 namespace {
21     constexpr uint32_t SLEEP_TIME_US = 10000;
22 }
23 
24 static void *g_handle = nullptr;
25 
LoadMotionSensor(void)26 bool LoadMotionSensor(void)
27 {
28     if (g_handle != nullptr) {
29         TLOGW(WmsLogTag::DMS, "motion 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_SO_PATH.c_str(), RTLD_LAZY);
37         TLOGI(WmsLogTag::DMS, "dlopen %{public}s, retry cnt: %{public}d", PLUGIN_SO_PATH.c_str(), cnt);
38         usleep(SLEEP_TIME_US);
39     } while (!g_handle && cnt < retryTimes);
40     return g_handle != nullptr;
41 }
42 
UnloadMotionSensor(void)43 void UnloadMotionSensor(void)
44 {
45     TLOGI(WmsLogTag::DMS, "unload motion plugin.");
46     if (g_handle != nullptr) {
47         dlclose(g_handle);
48         g_handle = nullptr;
49     }
50 }
51 
SubscribeCallback(int32_t motionType,OnMotionChangedPtr callback)52 __attribute__((no_sanitize("cfi"))) bool SubscribeCallback(int32_t motionType, OnMotionChangedPtr callback)
53 {
54     if (callback == nullptr) {
55         TLOGE(WmsLogTag::DMS, "callback is nullptr");
56         return false;
57     }
58     if (g_handle == nullptr) {
59         TLOGE(WmsLogTag::DMS, "g_handle is nullptr");
60         return false;
61     }
62     MotionSubscribeCallbackPtr func = (MotionSubscribeCallbackPtr)(dlsym(g_handle, "MotionSubscribeCallback"));
63     const char* dlsymError = dlerror();
64     if  (dlsymError) {
65         TLOGE(WmsLogTag::DMS, "dlsym error: %{public}s", dlsymError);
66         return false;
67     }
68     return func(motionType, callback);
69 }
70 
UnsubscribeCallback(int32_t motionType,OnMotionChangedPtr callback)71 __attribute__((no_sanitize("cfi"))) bool UnsubscribeCallback(int32_t motionType, OnMotionChangedPtr callback)
72 {
73     if (callback == nullptr) {
74         TLOGE(WmsLogTag::DMS, "callback is nullptr");
75         return false;
76     }
77     if (g_handle == nullptr) {
78         TLOGE(WmsLogTag::DMS, "g_handle is nullptr");
79         return false;
80     }
81     MotionUnsubscribeCallbackPtr func =
82         (MotionUnsubscribeCallbackPtr)(dlsym(g_handle, "MotionUnsubscribeCallback"));
83     const char* dlsymError = dlerror();
84     if  (dlsymError) {
85         TLOGE(WmsLogTag::DMS, "dlsym error: %{public}s", dlsymError);
86         return false;
87     }
88     return func(motionType, callback);
89 }
90 }
91 }