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 "camera_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 MEDIA_INFO_LOG("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 MEDIA_INFO_LOG("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 MEDIA_INFO_LOG("unload motion plugin.");
46 CHECK_RETURN(g_handle == nullptr);
47 dlclose(g_handle);
48 g_handle = nullptr;
49 }
50
SubscribeCallback(int32_t motionType,OnMotionChangedPtr callback)51 __attribute__((no_sanitize("cfi"))) bool SubscribeCallback(int32_t motionType, OnMotionChangedPtr callback)
52 {
53 CHECK_RETURN_RET_ELOG(callback == nullptr, false, "callback is nullptr");
54 CHECK_RETURN_RET_ELOG(g_handle == nullptr, false, "g_handle is nullptr");
55 MotionSubscribeCallbackPtr func = (MotionSubscribeCallbackPtr)(dlsym(g_handle, "MotionSubscribeCallback"));
56 const char* dlsymError = dlerror();
57 if (dlsymError) {
58 MEDIA_INFO_LOG("dlsym error: %{public}s", dlsymError);
59 return false;
60 }
61 return func(motionType, callback);
62 }
63
UnsubscribeCallback(int32_t motionType,OnMotionChangedPtr callback)64 __attribute__((no_sanitize("cfi"))) bool UnsubscribeCallback(int32_t motionType, OnMotionChangedPtr callback)
65 {
66 CHECK_RETURN_RET_ELOG(callback == nullptr, false, "callback is nullptr");
67 CHECK_RETURN_RET_ELOG(g_handle == nullptr, false, "g_handle is nullptr");
68 MotionUnsubscribeCallbackPtr func =
69 (MotionUnsubscribeCallbackPtr)(dlsym(g_handle, "MotionUnsubscribeCallback"));
70 const char* dlsymError = dlerror();
71 if (dlsymError) {
72 MEDIA_ERR_LOG("dlsym error: %{public}s", dlsymError);
73 return false;
74 }
75 return func(motionType, callback);
76 }
77 }
78 }
79