• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_impl.h"
17 #include <hdf_base.h>
18 #include <hdf_log.h>
19 
20 #define HDF_LOG_TAG    hdf_sensor_dal
21 
22 namespace OHOS {
23 namespace HDI {
24 namespace Sensor {
25 namespace V1_0 {
26 namespace {
27     constexpr int32_t CALLBACK_CTOUNT_THRESHOLD = 1;
28     using GroupIdCallBackMap = std::unordered_map<int32_t, std::vector<sptr<ISensorCallback>>>;
29     GroupIdCallBackMap g_groupIdCallBackMap;
30     std::mutex g_mutex;
31 }
32 
TradtionalSensorDataCallback(const struct SensorEvents * event)33 int32_t TradtionalSensorDataCallback(const struct SensorEvents *event)
34 {
35     if (event == nullptr || event->data == nullptr) {
36         HDF_LOGE("%{public}s failed, event or event.data is nullptr", __func__);
37         return SENSOR_FAILURE;
38     }
39 
40     std::lock_guard<std::mutex> lock(g_mutex);
41     auto groupCallBackIter = g_groupIdCallBackMap.find(TRADITIONAL_SENSOR_TYPE);
42     if (groupCallBackIter == g_groupIdCallBackMap.end()) {
43         return SENSOR_SUCCESS;
44     }
45 
46     HdfSensorEvents hdfSensorEvents;
47     hdfSensorEvents.sensorId = event->sensorId;
48     hdfSensorEvents.version = event->version;
49     hdfSensorEvents.timestamp = event->timestamp;
50     hdfSensorEvents.option = event->option;
51     hdfSensorEvents.mode = event->mode;
52     hdfSensorEvents.dataLen = event->dataLen;
53     uint32_t len = event->dataLen;
54     uint8_t *tmp = event->data;
55 
56     while (len--) {
57         hdfSensorEvents.data.push_back(*tmp);
58         tmp++;
59     }
60 
61     for (auto callBack : g_groupIdCallBackMap[TRADITIONAL_SENSOR_TYPE]) {
62         callBack->OnDataEvent(hdfSensorEvents);
63     }
64 
65     return SENSOR_SUCCESS;
66 }
67 
MedicalSensorDataCallback(const struct SensorEvents * event)68 int32_t MedicalSensorDataCallback(const struct SensorEvents *event)
69 {
70     if (event == nullptr || event->data == nullptr) {
71         HDF_LOGE("%{public}s failed, event or event.data is nullptr", __func__);
72         return SENSOR_FAILURE;
73     }
74 
75     std::lock_guard<std::mutex> lock(g_mutex);
76     auto groupCallBackIter = g_groupIdCallBackMap.find(MEDICAL_SENSOR_TYPE);
77     if (groupCallBackIter == g_groupIdCallBackMap.end()) {
78         return SENSOR_SUCCESS;
79     }
80 
81     HdfSensorEvents hdfSensorEvents;
82     hdfSensorEvents.sensorId = event->sensorId;
83     hdfSensorEvents.version = event->version;
84     hdfSensorEvents.timestamp = event->timestamp;
85     hdfSensorEvents.option = event->option;
86     hdfSensorEvents.mode = event->mode;
87     hdfSensorEvents.dataLen = event->dataLen;
88     uint32_t len = event->dataLen;
89     uint8_t *tmp = event->data;
90 
91     while (len--) {
92         hdfSensorEvents.data.push_back(*tmp);
93         tmp++;
94     }
95 
96     for (auto callBack : g_groupIdCallBackMap[MEDICAL_SENSOR_TYPE]) {
97         callBack->OnDataEvent(hdfSensorEvents);
98     }
99 
100     return SENSOR_SUCCESS;
101 }
102 
Init()103 void SensorImpl::Init()
104 {
105     sensorInterface = NewSensorInterfaceInstance();
106     if (sensorInterface == NULL) {
107         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
108     }
109 }
110 
GetAllSensorInfo(std::vector<HdfSensorInformation> & info)111 int32_t SensorImpl::GetAllSensorInfo(std::vector<HdfSensorInformation>& info)
112 {
113     if (sensorInterface == NULL || sensorInterface->GetAllSensors == NULL) {
114         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
115         return HDF_FAILURE;
116     }
117 
118     struct SensorInformation *sensorInfo = nullptr;
119     struct SensorInformation *tmp = nullptr;
120     int32_t count = 0;
121 
122     int32_t ret = sensorInterface->GetAllSensors(&sensorInfo, &count);
123     if (ret != SENSOR_SUCCESS) {
124         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
125         return ret;
126     }
127 
128     if (count <= 0) {
129         HDF_LOGE("%{public}s failed, count<=0", __func__);
130         return HDF_FAILURE;
131     }
132 
133     tmp = sensorInfo;
134     while (count--) {
135         HdfSensorInformation hdfSensorInfo;
136         std::string sensorName(tmp->sensorName);
137         hdfSensorInfo.sensorName = sensorName;
138         std::string vendorName(tmp->vendorName);
139         hdfSensorInfo.vendorName = vendorName;
140         std::string firmwareVersion(tmp->firmwareVersion);
141         hdfSensorInfo.firmwareVersion = firmwareVersion;
142         std::string hardwareVersion(tmp->hardwareVersion);
143         hdfSensorInfo.hardwareVersion = hardwareVersion;
144         hdfSensorInfo.sensorTypeId = tmp->sensorTypeId;
145         hdfSensorInfo.sensorId = tmp->sensorId;
146         hdfSensorInfo.maxRange = tmp->maxRange;
147         hdfSensorInfo.accuracy = tmp->accuracy;
148         hdfSensorInfo.power = tmp->power;
149         info.push_back(std::move(hdfSensorInfo));
150         tmp++;
151     }
152 
153     return HDF_SUCCESS;
154 }
155 
Enable(int32_t sensorId)156 int32_t SensorImpl::Enable(int32_t sensorId)
157 {
158     if (sensorInterface == NULL || sensorInterface->Enable == NULL) {
159         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
160         return HDF_FAILURE;
161     }
162 
163     int32_t ret = sensorInterface->Enable(sensorId);
164     if (ret != SENSOR_SUCCESS) {
165         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
166     }
167 
168     return ret;
169 }
170 
Disable(int32_t sensorId)171 int32_t SensorImpl::Disable(int32_t sensorId)
172 {
173     if (sensorInterface == NULL || sensorInterface->Disable == NULL) {
174         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
175         return HDF_FAILURE;
176     }
177 
178     int32_t ret = sensorInterface->Disable(sensorId);
179     if (ret != SENSOR_SUCCESS) {
180         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
181     }
182 
183     return ret;
184 }
185 
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)186 int32_t SensorImpl::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
187 {
188     if (sensorInterface == NULL || sensorInterface->SetBatch == NULL) {
189         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
190         return HDF_FAILURE;
191     }
192 
193     int32_t ret = sensorInterface->SetBatch(sensorId, samplingInterval, reportInterval);
194     if (ret != SENSOR_SUCCESS) {
195         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
196     }
197 
198     return ret;
199 }
200 
SetMode(int32_t sensorId,int32_t mode)201 int32_t SensorImpl::SetMode(int32_t sensorId, int32_t mode)
202 {
203     if (sensorInterface == NULL || sensorInterface->SetMode == NULL) {
204         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
205         return HDF_FAILURE;
206     }
207 
208     int32_t ret = sensorInterface->SetMode(sensorId, mode);
209     if (ret != SENSOR_SUCCESS) {
210         HDF_LOGE("%{public}s SetMode failed, error code is %{public}d", __func__, ret);
211     }
212 
213     return ret;
214 }
215 
SetOption(int32_t sensorId,uint32_t option)216 int32_t SensorImpl::SetOption(int32_t sensorId, uint32_t option)
217 {
218     if (sensorInterface == NULL || sensorInterface->SetOption == NULL) {
219         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
220         return HDF_FAILURE;
221     }
222 
223     int32_t ret = sensorInterface->SetOption(sensorId, option);
224     if (ret != SENSOR_SUCCESS) {
225         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
226     }
227 
228     return ret;
229 }
230 
Register(int32_t groupId,const sptr<ISensorCallback> & callbackObj)231 int32_t SensorImpl::Register(int32_t groupId, const sptr<ISensorCallback>& callbackObj)
232 {
233     if (sensorInterface == NULL || sensorInterface->Register == NULL) {
234         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
235         return HDF_FAILURE;
236     }
237 
238     if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
239         HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
240         return SENSOR_INVALID_PARAM;
241     }
242 
243     std::lock_guard<std::mutex> lock(g_mutex);
244     auto groupCallBackIter = g_groupIdCallBackMap.find(groupId);
245     if (groupCallBackIter != g_groupIdCallBackMap.end()) {
246         auto callBackIter =
247             find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
248             [callbackObj](const sptr<ISensorCallback> &callbackRegistered) {
249                 return callbackObj->AsObject().GetRefPtr() == callbackRegistered->AsObject().GetRefPtr();
250             });
251         if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
252             g_groupIdCallBackMap[groupId].push_back(callbackObj);
253         }
254         return SENSOR_SUCCESS;
255     }
256 
257     int32_t ret = HDF_FAILURE;
258     if (groupId == TRADITIONAL_SENSOR_TYPE) {
259         ret = sensorInterface->Register(groupId, TradtionalSensorDataCallback);
260     } else if (groupId == MEDICAL_SENSOR_TYPE) {
261         ret = sensorInterface->Register(groupId, MedicalSensorDataCallback);
262     }
263 
264     if (ret != SENSOR_SUCCESS) {
265         HDF_LOGE("%{public}s failed, ret[%{public}d]", __func__, ret);
266         return ret;
267     }
268 
269     std::vector<sptr<ISensorCallback>> remoteVec;
270     remoteVec.push_back(callbackObj);
271     g_groupIdCallBackMap[groupId] = remoteVec;
272 
273     return ret;
274 }
275 
Unregister(int32_t groupId,const sptr<ISensorCallback> & callbackObj)276 int32_t SensorImpl::Unregister(int32_t groupId, const sptr<ISensorCallback>& callbackObj)
277 {
278     if (sensorInterface == NULL || sensorInterface->Unregister == NULL) {
279         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
280         return HDF_FAILURE;
281     }
282 
283     if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
284         HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
285         return SENSOR_INVALID_PARAM;
286     }
287 
288     std::lock_guard<std::mutex> lock(g_mutex);
289     auto groupIdCallBackIter = g_groupIdCallBackMap.find(groupId);
290     if (groupIdCallBackIter == g_groupIdCallBackMap.end()) {
291         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
292         return HDF_FAILURE;
293     }
294 
295     auto callBackIter =
296         find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
297         [callbackObj](const sptr<ISensorCallback> &callbackRegistered) {
298             return callbackObj->AsObject().GetRefPtr() == callbackRegistered->AsObject().GetRefPtr();
299         });
300     if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
301         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
302         return HDF_FAILURE;
303     }
304 
305     /**
306      * when there is only one item in the vector,can call the Unregister function.
307      * when there is more than one item in the vector, only need to remove the  callbackObj
308      * from the vector
309      */
310     if (g_groupIdCallBackMap[groupId].size() > CALLBACK_CTOUNT_THRESHOLD) {
311         g_groupIdCallBackMap[groupId].erase(callBackIter);
312         return SENSOR_SUCCESS;
313     }
314 
315     int32_t ret = HDF_FAILURE;
316     if (groupId == TRADITIONAL_SENSOR_TYPE) {
317         ret = sensorInterface->Unregister(groupId, TradtionalSensorDataCallback);
318     } else if (groupId == MEDICAL_SENSOR_TYPE) {
319         ret = sensorInterface->Unregister(groupId, MedicalSensorDataCallback);
320     }
321 
322     if (ret != SENSOR_SUCCESS) {
323         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
324         return ret;
325     }
326     g_groupIdCallBackMap.erase(groupId);
327 
328     return ret;
329 }
330 } // V1_0
331 } // Sensor
332 } // HDI
333 } // OHOS