• 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 #include "telephony_log_wrapper.h"
19 
20 using namespace OHOS::Telephony;
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 constexpr uint32_t SLEEP_TIME_US = 10000;
26 }
27 
28 static void *g_handle = nullptr;
29 
LoadMotionSensor(void)30 bool LoadMotionSensor(void)
31 {
32     TELEPHONY_LOGI("LoadMotionSensor enter.");
33     if (g_handle != nullptr) {
34         TELEPHONY_LOGE("motion plugin has already exist.");
35         return true;
36     }
37     int32_t cnt = 0;
38     int32_t retryTimes = 3;
39     do {
40         cnt++;
41         g_handle = dlopen(PLUGIN_SO_PATH.c_str(), RTLD_LAZY);
42         TELEPHONY_LOGI("dlopen %{public}s, retry cnt: %{public}d", PLUGIN_SO_PATH.c_str(), cnt);
43         usleep(SLEEP_TIME_US);
44     } while (!g_handle && cnt < retryTimes);
45     return g_handle != nullptr;
46 }
47 
UnloadMotionSensor(void)48 void UnloadMotionSensor(void)
49 {
50     TELEPHONY_LOGI("unload motion plugin.");
51     if (g_handle != nullptr) {
52         dlclose(g_handle);
53         g_handle = nullptr;
54     }
55 }
56 
SubscribeCallback(int32_t motionType,OnMotionChangedPtr callback)57 __attribute__((no_sanitize("cfi"))) bool SubscribeCallback(int32_t motionType, OnMotionChangedPtr callback)
58 {
59     TELEPHONY_LOGI("RegisterCallback enter. motionType = %{public}d", motionType);
60     if (callback == nullptr) {
61         TELEPHONY_LOGE("callback is nullptr");
62         return false;
63     }
64     if (g_handle == nullptr) {
65         TELEPHONY_LOGE("g_handle is nullptr");
66         return false;
67     }
68     MotionSubscribeCallbackPtr func = (MotionSubscribeCallbackPtr)(dlsym(g_handle, "MotionSubscribeCallback"));
69     if (func == nullptr) {
70         TELEPHONY_LOGE("func is nullptr, dlsym error: %{public}s", dlerror());
71         return false;
72     }
73     return func(motionType, callback);
74 }
75 
UnsubscribeCallback(int32_t motionType,OnMotionChangedPtr callback)76 __attribute__((no_sanitize("cfi"))) bool UnsubscribeCallback(int32_t motionType, OnMotionChangedPtr callback)
77 {
78     TELEPHONY_LOGI("RegisterCallback enter.");
79     if (callback == nullptr) {
80         TELEPHONY_LOGE("callback is nullptr");
81         return false;
82     }
83     if (g_handle == nullptr) {
84         TELEPHONY_LOGE("g_handle is nullptr");
85         return false;
86     }
87     MotionUnsubscribeCallbackPtr func = (MotionUnsubscribeCallbackPtr)(dlsym(g_handle, "MotionUnsubscribeCallback"));
88     const char *dlsymError = dlerror();
89     if (dlsymError) {
90         TELEPHONY_LOGE("dlsym error: %{public}s", dlsymError);
91         return false;
92     }
93     return func(motionType, callback);
94 }
95 } // namespace Rosen
96 } // namespace OHOS