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 "motion_plugin.h"
17
18 #include "sensor_log.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "MotionPlugin"
22
23 namespace OHOS {
24 namespace Sensors {
25 namespace {
26 constexpr uint32_t SLEEP_TIME_US = 10000;
27 }
28
29 static void *g_handle = nullptr;
30
LoadMotionSensor(void)31 bool LoadMotionSensor(void)
32 {
33 SEN_HILOGI("Load motion plugin in");
34 if (g_handle != nullptr) {
35 SEN_HILOGW("Motion plugin has already exits");
36 return true;
37 }
38 int32_t cnt = 0;
39 int32_t retryTimes = 3;
40 do {
41 cnt++;
42 g_handle = dlopen(PLUGIN_SO_PATH.c_str(), RTLD_LAZY);
43 SEN_HILOGI("dlopen %{public}s, retry cnt: %{public}d", PLUGIN_SO_PATH.c_str(), cnt);
44 usleep(SLEEP_TIME_US);
45 } while (!g_handle && cnt < retryTimes);
46 return (g_handle != nullptr);
47 }
48
UnloadMotionSensor(void)49 void UnloadMotionSensor(void)
50 {
51 SEN_HILOGI("Unload motion plugin in");
52 if (g_handle != nullptr) {
53 dlclose(g_handle);
54 g_handle = nullptr;
55 }
56 }
57
MotionTransformIfRequired(const std::string & pkName,uint32_t state,SensorData * sensorData)58 __attribute__((no_sanitize("cfi"))) void MotionTransformIfRequired(const std::string& pkName,
59 uint32_t state, SensorData* sensorData)
60 {
61 if (g_handle == nullptr) {
62 return;
63 }
64 MotionTransformIfRequiredPtr func = (MotionTransformIfRequiredPtr)(dlsym(g_handle, "TransformIfRequired"));
65 if (func == nullptr) {
66 SEN_HILOGE("func is nullptr");
67 return;
68 }
69 const char* dlsymError = dlerror();
70 if (dlsymError) {
71 SEN_HILOGE("dlsym error: %{public}s", dlsymError);
72 return;
73 }
74 return func(pkName, state, sensorData);
75 }
76 }
77 }