• 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 "sensor_if_service.h"
17 #include <refbase.h>
18 #include <cinttypes>
19 #include "hdf_log.h"
20 #include "hitrace_meter.h"
21 #include "sensor_type.h"
22 #include "sensor_callback_vdi.h"
23 
24 #define HDF_LOG_TAG uhdf_sensor_service
25 
26 namespace OHOS {
27 namespace HDI {
28 namespace Sensor {
29 namespace V1_1 {
SensorIfService()30 SensorIfService::SensorIfService()
31 {
32     int32_t ret = GetSensorVdiImpl();
33     if (ret != HDF_SUCCESS) {
34         HDF_LOGE("%{public}s: get sensor vdi instance failed", __func__);
35     }
36 }
37 
~SensorIfService()38 SensorIfService::~SensorIfService()
39 {
40     if (vdi_ != nullptr) {
41         HdfCloseVdi(vdi_);
42     }
43 }
44 
GetSensorVdiImpl()45 int32_t SensorIfService::GetSensorVdiImpl()
46 {
47     struct WrapperSensorVdi *wrapperSensorVdi = nullptr;
48     uint32_t version = 0;
49     vdi_ = HdfLoadVdi(HDI_SENSOR_VDI_LIBNAME);
50     if (vdi_ == nullptr || vdi_->vdiBase == nullptr) {
51         HDF_LOGE("%{public}s: load sensor vdi failed", __func__);
52         return HDF_FAILURE;
53     }
54 
55     version = HdfGetVdiVersion(vdi_);
56     if (version != 1) {
57         HDF_LOGE("%{public}s: get sensor vdi version failed", __func__);
58         return HDF_FAILURE;
59     }
60 
61     wrapperSensorVdi = reinterpret_cast<struct WrapperSensorVdi *>(vdi_->vdiBase);
62     sensorVdiImpl_ = wrapperSensorVdi->sensorModule;
63     if (sensorVdiImpl_ == nullptr) {
64         HDF_LOGE("%{public}s: get sensor impl failed", __func__);
65         return HDF_FAILURE;
66     }
67 
68     return HDF_SUCCESS;
69 }
70 
Init()71 int32_t SensorIfService::Init()
72 {
73     if (sensorVdiImpl_ == nullptr) {
74         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
75         return HDF_FAILURE;
76     }
77     int32_t ret = sensorVdiImpl_->Init();
78     if (ret != SENSOR_SUCCESS) {
79         HDF_LOGE("%{public}s Init failed, error code is %{public}d", __func__, ret);
80     }
81 
82     return ret;
83 }
84 
GetAllSensorInfo(std::vector<HdfSensorInformation> & info)85 int32_t SensorIfService::GetAllSensorInfo(std::vector<HdfSensorInformation> &info)
86 {
87     HDF_LOGI("%{public}s: Enter the GetAllSensorInfo function.", __func__);
88     if (sensorVdiImpl_ == nullptr) {
89         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
90         return HDF_FAILURE;
91     }
92 
93     std::vector<HdfSensorInformationVdi> sensorInfoVdi = {};
94     StartTrace(HITRACE_TAG_HDF, "GetAllSensorInfo");
95     int32_t ret = sensorVdiImpl_->GetAllSensorInfo(sensorInfoVdi);
96     if (ret != SENSOR_SUCCESS) {
97         HDF_LOGE("%{public}s GetAllSensors failed, error code is %{public}d", __func__, ret);
98         return ret;
99     }
100     FinishTrace(HITRACE_TAG_HDF);
101 
102     if (sensorInfoVdi.empty()) {
103         HDF_LOGE("%{public}s no sensor info in list", __func__);
104         return HDF_FAILURE;
105     }
106 
107     for (const auto &it : sensorInfoVdi) {
108         struct HdfSensorInformation sensorInfo = {};
109         sensorInfo.sensorName = it.sensorName;
110         sensorInfo.vendorName = it.vendorName;
111         sensorInfo.firmwareVersion = it.firmwareVersion;
112         sensorInfo.hardwareVersion = it.hardwareVersion;
113         sensorInfo.sensorTypeId = it.sensorTypeId;
114         sensorInfo.sensorId = it.sensorId;
115         sensorInfo.maxRange = it.maxRange;
116         sensorInfo.accuracy = it.accuracy;
117         sensorInfo.power = it.power;
118         sensorInfo.minDelay = it.minDelay;
119         sensorInfo.maxDelay = it.maxDelay;
120         info.push_back(std::move(sensorInfo));
121     }
122 
123     return HDF_SUCCESS;
124 }
125 
Enable(int32_t sensorId)126 int32_t SensorIfService::Enable(int32_t sensorId)
127 {
128     HDF_LOGI("%{public}s: Enter the Enable function, sensorId is %{public}d", __func__, sensorId);
129     if (sensorVdiImpl_ == nullptr) {
130         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
131         return HDF_FAILURE;
132     }
133 
134     StartTrace(HITRACE_TAG_HDF, "Enable");
135     int32_t ret = sensorVdiImpl_->Enable(sensorId);
136     if (ret != SENSOR_SUCCESS) {
137         HDF_LOGE("%{public}s Enable failed, error code is %{public}d", __func__, ret);
138     }
139     FinishTrace(HITRACE_TAG_HDF);
140 
141     return ret;
142 }
143 
Disable(int32_t sensorId)144 int32_t SensorIfService::Disable(int32_t sensorId)
145 {
146     HDF_LOGI("%{public}s: Enter the Disable function, sensorId is %{public}d", __func__, sensorId);
147     if (sensorVdiImpl_ == nullptr) {
148         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
149         return HDF_FAILURE;
150     }
151 
152     StartTrace(HITRACE_TAG_HDF, "Disable");
153     int32_t ret = sensorVdiImpl_->Disable(sensorId);
154     if (ret != SENSOR_SUCCESS) {
155         HDF_LOGE("%{public}s Disable failed, error code is %{public}d", __func__, ret);
156     }
157     FinishTrace(HITRACE_TAG_HDF);
158 
159     return ret;
160 }
161 
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)162 int32_t SensorIfService::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
163 {
164     HDF_LOGI("%{public}s: sensorId is %{public}d, samplingInterval is [%{public}" PRId64 "], \
165         reportInterval is [%{public}" PRId64 "].", __func__, sensorId, samplingInterval, reportInterval);
166     if (sensorVdiImpl_ == nullptr) {
167         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
168         return HDF_FAILURE;
169     }
170 
171     StartTrace(HITRACE_TAG_HDF, "SetBatch");
172     int32_t ret = sensorVdiImpl_->SetBatch(sensorId, samplingInterval, reportInterval);
173     if (ret != SENSOR_SUCCESS) {
174         HDF_LOGE("%{public}s SetBatch failed, error code is %{public}d", __func__, ret);
175     }
176     FinishTrace(HITRACE_TAG_HDF);
177 
178     return ret;
179 }
180 
SetMode(int32_t sensorId,int32_t mode)181 int32_t SensorIfService::SetMode(int32_t sensorId, int32_t mode)
182 {
183     HDF_LOGI("%{public}s: Enter the SetMode function, sensorId is %{public}d, mode is %{public}d",
184         __func__, sensorId, mode);
185     if (sensorVdiImpl_ == nullptr) {
186         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
187         return HDF_FAILURE;
188     }
189 
190     StartTrace(HITRACE_TAG_HDF, "SetMode");
191     int32_t ret = sensorVdiImpl_->SetMode(sensorId, mode);
192     if (ret != SENSOR_SUCCESS) {
193         HDF_LOGE("%{public}s SetMode failed, error code is %{public}d", __func__, ret);
194     }
195     FinishTrace(HITRACE_TAG_HDF);
196 
197     return ret;
198 }
199 
SetOption(int32_t sensorId,uint32_t option)200 int32_t SensorIfService::SetOption(int32_t sensorId, uint32_t option)
201 {
202     HDF_LOGI("%{public}s: Enter the SetOption function, sensorId is %{public}d, option is %{public}u",
203         __func__, sensorId, option);
204     if (sensorVdiImpl_ == nullptr) {
205         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
206         return HDF_FAILURE;
207     }
208 
209     StartTrace(HITRACE_TAG_HDF, "SetOption");
210     int32_t ret = sensorVdiImpl_->SetOption(sensorId, option);
211     if (ret != SENSOR_SUCCESS) {
212         HDF_LOGE("%{public}s SetOption failed, error code is %{public}d", __func__, ret);
213     }
214     FinishTrace(HITRACE_TAG_HDF);
215 
216     return ret;
217 }
218 
Register(int32_t groupId,const sptr<ISensorCallback> & callbackObj)219 int32_t SensorIfService::Register(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
220 {
221     HDF_LOGI("%{public}s: Enter the Register function, groupId is %{public}d", __func__, groupId);
222     if (sensorVdiImpl_ == nullptr) {
223         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
224         return HDF_FAILURE;
225     }
226 
227     StartTrace(HITRACE_TAG_HDF, "Register");
228     sptr<SensorCallbackVdi> sensorCb = new SensorCallbackVdi(callbackObj);
229     int32_t ret = sensorVdiImpl_->Register(groupId, sensorCb);
230     if (ret != SENSOR_SUCCESS) {
231         HDF_LOGE("%{public}s Register failed, error code is %{public}d", __func__, ret);
232     }
233     FinishTrace(HITRACE_TAG_HDF);
234 
235     return ret;
236 }
237 
Unregister(int32_t groupId,const sptr<ISensorCallback> & callbackObj)238 int32_t SensorIfService::Unregister(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
239 {
240     HDF_LOGI("%{public}s: Enter the Unregister function, groupId is %{public}d", __func__, groupId);
241     if (sensorVdiImpl_ == nullptr) {
242         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
243         return HDF_FAILURE;
244     }
245 
246     StartTrace(HITRACE_TAG_HDF, "Unregister");
247     sptr<SensorCallbackVdi> sensorCb = new SensorCallbackVdi(callbackObj);
248     int32_t ret = sensorVdiImpl_->Unregister(groupId, sensorCb);
249     if (ret != SENSOR_SUCCESS) {
250         HDF_LOGE("%{public}s: Unregister failed, error code is %{public}d", __func__, ret);
251     }
252     FinishTrace(HITRACE_TAG_HDF);
253 
254     return ret;
255 }
256 
ReadData(int32_t sensorId,std::vector<HdfSensorEvents> & event)257 int32_t SensorIfService::ReadData(int32_t sensorId, std::vector<HdfSensorEvents> &event)
258 {
259     HDF_LOGI("%{public}s: Enter the ReadData function", __func__);
260     if (sensorVdiImpl_ == nullptr) {
261         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
262         return HDF_FAILURE;
263     }
264 
265     return HDF_SUCCESS;
266 }
267 
SensorInterfaceImplGetInstance(void)268 extern "C" ISensorInterface *SensorInterfaceImplGetInstance(void)
269 {
270     SensorIfService *impl = new (std::nothrow) SensorIfService();
271     if (impl == nullptr) {
272         return nullptr;
273     }
274 
275     int32_t ret = impl->Init();
276     if (ret != HDF_SUCCESS) {
277         HDF_LOGE("%{public}s: service init failed, error code is %{public}d", __func__, ret);
278         delete impl;
279         return nullptr;
280     }
281 
282     return impl;
283 }
284 } // namespace V1_1
285 } // namespace Sensor
286 } // namespace HDI
287 } // namespace OHOS
288