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     HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType);
85     if (motionVdiImpl_ == nullptr) {
86         HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
87         return HDF_FAILURE;
88     }
89 
90     int32_t checkResult = CheckMotionType(motionType);
91     if (checkResult != HDF_SUCCESS) {
92         return checkResult;
93     }
94 
95     StartTrace(HITRACE_TAG_HDF, "EnableMotion");
96     int32_t ret = motionVdiImpl_->EnableMotion(motionType);
97     if (ret != HDF_SUCCESS) {
98         HDF_LOGE("%{public}s: Enable failed, error code is %{public}d", __func__, ret);
99     }
100     FinishTrace(HITRACE_TAG_HDF);
101 
102     return ret;
103 }
104 
DisableMotion(int32_t motionType)105 int32_t MotionIfService::DisableMotion(int32_t motionType)
106 {
107     HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType);
108     if (motionVdiImpl_ == nullptr) {
109         HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
110         return HDF_FAILURE;
111     }
112 
113     int32_t checkResult = CheckMotionType(motionType);
114     if (checkResult != HDF_SUCCESS) {
115         return checkResult;
116     }
117 
118     StartTrace(HITRACE_TAG_HDF, "DisableMotion");
119     int32_t ret = motionVdiImpl_->DisableMotion(motionType);
120     if (ret != HDF_SUCCESS) {
121         HDF_LOGE("%{public}s: Disable failed, error code is %{public}d", __func__, ret);
122     }
123     FinishTrace(HITRACE_TAG_HDF);
124 
125     return ret;
126 }
127 
Register(const sptr<IMotionCallback> & callbackObj)128 int32_t MotionIfService::Register(const sptr<IMotionCallback> &callbackObj)
129 {
130     HDF_LOGI("%{public}s", __func__);
131     if (motionVdiImpl_ == nullptr) {
132         HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
133         return HDF_FAILURE;
134     }
135 
136     StartTrace(HITRACE_TAG_HDF, "Register");
137     sptr<MotionCallbackVdi> motionCb = new MotionCallbackVdi(callbackObj);
138     int32_t ret = motionVdiImpl_->RegisterMotionCallback(motionCb);
139     if (ret != HDF_SUCCESS) {
140         HDF_LOGE("%{public}s: Register failed, error code is %{public}d", __func__, ret);
141     }
142     FinishTrace(HITRACE_TAG_HDF);
143 
144     return ret;
145 }
146 
Unregister(const sptr<IMotionCallback> & callbackObj)147 int32_t MotionIfService::Unregister(const sptr<IMotionCallback> &callbackObj)
148 {
149     HDF_LOGI("%{public}s", __func__);
150     if (motionVdiImpl_ == nullptr) {
151         HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
152         return HDF_FAILURE;
153     }
154 
155     StartTrace(HITRACE_TAG_HDF, "Unregister");
156     sptr<MotionCallbackVdi> motionCb = new MotionCallbackVdi(callbackObj);
157     int32_t ret = motionVdiImpl_->UnregisterMotionCallback(motionCb);
158     if (ret != HDF_SUCCESS) {
159         HDF_LOGE("%{public}s: Unregister failed, Unregistererror code is %{public}d", __func__, ret);
160     }
161     FinishTrace(HITRACE_TAG_HDF);
162 
163     return ret;
164 }
165 
SetMotionConfig(int32_t motionType,const std::vector<uint8_t> & data)166 int32_t MotionIfService::SetMotionConfig(int32_t motionType, const std::vector<uint8_t>& data)
167 {
168     HDF_LOGI("%{public}s: motionType is %{public}d", __func__, motionType);
169     if (motionVdiImpl_ == nullptr) {
170         HDF_LOGE("%{public}s motionVdiImpl_ is nullptr", __func__);
171         return HDF_FAILURE;
172     }
173 
174     int32_t checkResult = CheckMotionType(motionType);
175     if (checkResult != HDF_SUCCESS) {
176         return checkResult;
177     }
178 
179     StartTrace(HITRACE_TAG_HDF, "SetMotionConfig");
180     int32_t ret = motionVdiImpl_->SetMotionConfig(motionType, data);
181     if (ret != HDF_SUCCESS) {
182         HDF_LOGE("%{public}s: SetMotionConfig failed, error code is %{public}d", __func__, ret);
183     }
184     FinishTrace(HITRACE_TAG_HDF);
185 
186     return ret;
187 }
188 
CheckMotionType(int32_t motionType)189 int32_t MotionIfService::CheckMotionType(int32_t motionType)
190 {
191     if ((motionType > HDF_MOTION_TYPE_SECTION)) {
192         return HDF_SUCCESS;
193     }
194     if (motionType >= HDF_MOTION_TYPE_PICKUP && motionType < HDF_MOTION_TYPE_MAX) {
195         return HDF_SUCCESS;
196     }
197     return HDF_ERR_INVALID_PARAM;
198 }
199 
MotionInterfaceImplGetInstance(void)200 extern "C" IMotionInterface *MotionInterfaceImplGetInstance(void)
201 {
202     MotionIfService *impl = new (std::nothrow) MotionIfService();
203     if (impl == nullptr) {
204         return nullptr;
205     }
206 
207     int32_t ret = impl->Init();
208     if (ret != HDF_SUCCESS) {
209         HDF_LOGE("%{public}s service init failed, error code is %{public}d", __func__, ret);
210         delete impl;
211         return nullptr;
212     }
213 
214     return impl;
215 }
216 } // V1_1
217 } //Motion
218 } //HDI
219 } //OHOS
220