1 /*
2 * Copyright (c) 2023 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_if_service.h"
17 #include <hdf_base.h>
18 #include "hitrace_meter.h"
19
20 #define HDF_LOG_TAG "uhdf_motion_service"
21 #define HDF_MOTION_TYPE_SECTION 1000
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Motion {
26 namespace V1_1 {
MotionIfService()27 MotionIfService::MotionIfService()
28 {
29 int32_t ret = GetMotionVdiImpl();
30 if (ret != HDF_SUCCESS) {
31 HDF_LOGE("%{public}s get motion vdi impl failed!", __func__);
32 }
33 }
34
~MotionIfService()35 MotionIfService::~MotionIfService()
36 {
37 if (vdi_ != nullptr) {
38 HdfCloseVdi(vdi_);
39 }
40 }
41
GetMotionVdiImpl()42 int32_t MotionIfService::GetMotionVdiImpl()
43 {
44 struct WrapperMotionVdi *wrapperMotionVdi = nullptr;
45 uint32_t version = 0;
46 vdi_ = HdfLoadVdi(HDI_MOTION_VDI_LIBNAME);
47 if (vdi_ == nullptr || vdi_->vdiBase == nullptr) {
48 HDF_LOGE("%{public}s load motion vdi failed!", __func__);
49 return HDF_FAILURE;
50 }
51
52 version = HdfGetVdiVersion(vdi_);
53 if (version != 1) {
54 HDF_LOGE("%{public}s get motion vdi version failed!", __func__);
55 return HDF_FAILURE;
56 }
57
58 wrapperMotionVdi = reinterpret_cast<struct WrapperMotionVdi *>(vdi_->vdiBase);
59 motionVdiImpl_ = wrapperMotionVdi->motionModule;
60 if (motionVdiImpl_ == nullptr) {
61 HDF_LOGE("%{public}s get motion impl failed!", __func__);
62 return HDF_FAILURE;
63 }
64
65 return HDF_SUCCESS;
66 }
Init()67 int32_t MotionIfService::Init()
68 {
69 if (motionVdiImpl_ == nullptr) {
70 HDF_LOGE("%{public}s get motion vdi version failed!", __func__);
71 return HDF_FAILURE;
72 }
73
74 int32_t ret = motionVdiImpl_->InitMotion();
75 if (ret != HDF_SUCCESS) {
76 HDF_LOGE("%{public}s impl init failed,error code is %{public}d", __func__, ret);
77 }
78
79 return ret;
80 }
81
EnableMotion(int32_t motionType)82 int32_t MotionIfService::EnableMotion(int32_t motionType)
83 {
84 if (motionType <= HDF_MOTION_TYPE_SECTION) {
85 HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType);
86 }
87
88 if (motionVdiImpl_ == nullptr) {
89 HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
90 return HDF_FAILURE;
91 }
92
93 int32_t checkResult = CheckMotionType(motionType);
94 if (checkResult != HDF_SUCCESS) {
95 return checkResult;
96 }
97
98 StartTrace(HITRACE_TAG_HDF, "EnableMotion");
99 int32_t ret = motionVdiImpl_->EnableMotion(motionType);
100 if (ret != HDF_SUCCESS) {
101 HDF_LOGE("%{public}s: Enable failed, error code is %{public}d", __func__, ret);
102 }
103 FinishTrace(HITRACE_TAG_HDF);
104
105 return ret;
106 }
107
DisableMotion(int32_t motionType)108 int32_t MotionIfService::DisableMotion(int32_t motionType)
109 {
110 if (motionType <= HDF_MOTION_TYPE_SECTION) {
111 HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType);
112 }
113
114 if (motionVdiImpl_ == nullptr) {
115 HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
116 return HDF_FAILURE;
117 }
118
119 int32_t checkResult = CheckMotionType(motionType);
120 if (checkResult != HDF_SUCCESS) {
121 return checkResult;
122 }
123
124 StartTrace(HITRACE_TAG_HDF, "DisableMotion");
125 int32_t ret = motionVdiImpl_->DisableMotion(motionType);
126 if (ret != HDF_SUCCESS) {
127 HDF_LOGE("%{public}s: Disable failed, error code is %{public}d", __func__, ret);
128 }
129 FinishTrace(HITRACE_TAG_HDF);
130
131 return ret;
132 }
133
Register(const sptr<IMotionCallback> & callbackObj)134 int32_t MotionIfService::Register(const sptr<IMotionCallback> &callbackObj)
135 {
136 HDF_LOGI("%{public}s", __func__);
137 if (motionVdiImpl_ == nullptr) {
138 HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
139 return HDF_FAILURE;
140 }
141
142 StartTrace(HITRACE_TAG_HDF, "Register");
143 sptr<MotionCallbackVdi> motionCb = new MotionCallbackVdi(callbackObj);
144 int32_t ret = motionVdiImpl_->RegisterMotionCallback(motionCb);
145 if (ret != HDF_SUCCESS) {
146 HDF_LOGE("%{public}s: Register failed, error code is %{public}d", __func__, ret);
147 }
148 FinishTrace(HITRACE_TAG_HDF);
149
150 return ret;
151 }
152
Unregister(const sptr<IMotionCallback> & callbackObj)153 int32_t MotionIfService::Unregister(const sptr<IMotionCallback> &callbackObj)
154 {
155 HDF_LOGI("%{public}s", __func__);
156 if (motionVdiImpl_ == nullptr) {
157 HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
158 return HDF_FAILURE;
159 }
160
161 StartTrace(HITRACE_TAG_HDF, "Unregister");
162 sptr<MotionCallbackVdi> motionCb = new MotionCallbackVdi(callbackObj);
163 int32_t ret = motionVdiImpl_->UnregisterMotionCallback(motionCb);
164 if (ret != HDF_SUCCESS) {
165 HDF_LOGE("%{public}s: Unregister failed, Unregistererror code is %{public}d", __func__, ret);
166 }
167 FinishTrace(HITRACE_TAG_HDF);
168
169 return ret;
170 }
171
SetMotionConfig(int32_t motionType,const std::vector<uint8_t> & data)172 int32_t MotionIfService::SetMotionConfig(int32_t motionType, const std::vector<uint8_t>& data)
173 {
174 HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType);
175 if (motionVdiImpl_ == nullptr) {
176 HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
177 return HDF_FAILURE;
178 }
179
180 int32_t checkResult = CheckMotionType(motionType);
181 if (checkResult != HDF_SUCCESS) {
182 return checkResult;
183 }
184
185 StartTrace(HITRACE_TAG_HDF, "SetMotionConfig");
186 int32_t ret = motionVdiImpl_->SetMotionConfig(motionType, data);
187 if (ret != HDF_SUCCESS) {
188 HDF_LOGE("%{public}s: SetMotionConfig failed, error code is %{public}d", __func__, ret);
189 }
190 FinishTrace(HITRACE_TAG_HDF);
191
192 return ret;
193 }
194
CheckMotionType(int32_t motionType)195 int32_t MotionIfService::CheckMotionType(int32_t motionType)
196 {
197 if ((motionType > HDF_MOTION_TYPE_SECTION)) {
198 return HDF_SUCCESS;
199 }
200 if (motionType >= HDF_MOTION_TYPE_PICKUP && motionType < HDF_MOTION_TYPE_MAX) {
201 return HDF_SUCCESS;
202 }
203 return HDF_ERR_INVALID_PARAM;
204 }
205
MotionInterfaceImplGetInstance(void)206 extern "C" IMotionInterface *MotionInterfaceImplGetInstance(void)
207 {
208 MotionIfService *impl = new (std::nothrow) MotionIfService();
209 if (impl == nullptr) {
210 return nullptr;
211 }
212
213 int32_t ret = impl->Init();
214 if (ret != HDF_SUCCESS) {
215 HDF_LOGE("%{public}s service init failed, error code is %{public}d", __func__, ret);
216 delete impl;
217 return nullptr;
218 }
219
220 return impl;
221 }
222 } // V1_1
223 } //Motion
224 } //HDI
225 } //OHOS
226