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