• 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 <unordered_map>
18 #include <mutex>
19 #include <iproxy_broker.h>
20 #include <hdf_base.h>
21 #include <hdf_log.h>
22 #include "callback_death_recipient.h"
23 
24 #define HDF_LOG_TAG uhdf_sensor
25 
26 namespace OHOS {
27 namespace HDI {
28 namespace Sensor {
29 namespace V1_0 {
30 namespace {
31     constexpr int32_t CALLBACK_CTOUNT_THRESHOLD = 1;
32     using GroupIdCallBackMap = std::unordered_map<int32_t, std::vector<sptr<ISensorCallback>>>;
33     using CallBackDeathRecipientMap = std::unordered_map<IRemoteObject *, sptr<CallBackDeathRecipient>>;
34     GroupIdCallBackMap g_groupIdCallBackMap;
35     CallBackDeathRecipientMap g_callBackDeathRecipientMap;
36     std::mutex g_mutex;
37 } // namespace
38 
SensorInterfaceImplGetInstance(void)39 extern "C" ISensorInterface *SensorInterfaceImplGetInstance(void)
40 {
41     using OHOS::HDI::Sensor::V1_0::SensorImpl;
42     SensorImpl *service = new (std::nothrow) SensorImpl();
43     if (service == nullptr) {
44         return nullptr;
45     }
46 
47     service->Init();
48     return service;
49 }
50 
ReportSensorEventsData(int32_t sensorType,const struct SensorEvents * event)51 int32_t ReportSensorEventsData(int32_t sensorType, const struct SensorEvents *event)
52 {
53     std::lock_guard<std::mutex> lock(g_mutex);
54     auto groupCallBackIter = g_groupIdCallBackMap.find(sensorType);
55     if (groupCallBackIter == g_groupIdCallBackMap.end()) {
56         return SENSOR_SUCCESS;
57     }
58 
59     HdfSensorEvents hdfSensorEvents;
60     hdfSensorEvents.sensorId = event->sensorId;
61     hdfSensorEvents.version = event->version;
62     hdfSensorEvents.timestamp = event->timestamp;
63     hdfSensorEvents.option = event->option;
64     hdfSensorEvents.mode = event->mode;
65     hdfSensorEvents.dataLen = event->dataLen;
66     uint32_t len = event->dataLen;
67     uint8_t *tmp = event->data;
68 
69     while ((len--) != 0) {
70         hdfSensorEvents.data.push_back(*tmp);
71         tmp++;
72     }
73 
74     for (auto callBack : g_groupIdCallBackMap[sensorType]) {
75         callBack->OnDataEvent(hdfSensorEvents);
76     }
77 
78     return SENSOR_SUCCESS;
79 }
80 
TradtionalSensorDataCallback(const struct SensorEvents * event)81 int32_t TradtionalSensorDataCallback(const struct SensorEvents *event)
82 {
83     if (event == nullptr || event->data == nullptr) {
84         HDF_LOGE("%{public}s failed, event or event.data is nullptr", __func__);
85         return SENSOR_FAILURE;
86     }
87 
88     (void)ReportSensorEventsData(TRADITIONAL_SENSOR_TYPE, event);
89 
90     return SENSOR_SUCCESS;
91 }
92 
MedicalSensorDataCallback(const struct SensorEvents * event)93 int32_t MedicalSensorDataCallback(const struct SensorEvents *event)
94 {
95     if (event == nullptr || event->data == nullptr) {
96         HDF_LOGE("%{public}s failed, event or event.data is nullptr", __func__);
97         return SENSOR_FAILURE;
98     }
99 
100     (void)ReportSensorEventsData(MEDICAL_SENSOR_TYPE, event);
101 
102     return SENSOR_SUCCESS;
103 }
104 
RemoveDeathNotice(int32_t sensorType)105 void  SensorImpl::RemoveDeathNotice(int32_t sensorType)
106 {
107     auto iter = g_groupIdCallBackMap.find(sensorType);
108     if (iter != g_groupIdCallBackMap.end()) {
109         for (auto callback : g_groupIdCallBackMap[sensorType]) {
110             const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<ISensorCallback>(callback);
111             auto recipientIter = g_callBackDeathRecipientMap.find(remote.GetRefPtr());
112             if (recipientIter != g_callBackDeathRecipientMap.end()) {
113                 bool removeResult = remote->RemoveDeathRecipient(recipientIter->second);
114                 if (!removeResult) {
115                     HDF_LOGE("%{public}s: sensor destroyed, callback RemoveSensorDeathRecipient fail", __func__);
116                 }
117             }
118         }
119     }
120 }
121 
~SensorImpl()122 SensorImpl::~SensorImpl()
123 {
124     std::lock_guard<std::mutex> lock(g_mutex);
125 
126     RemoveDeathNotice(TRADITIONAL_SENSOR_TYPE);
127     RemoveDeathNotice(MEDICAL_SENSOR_TYPE);
128 
129     FreeSensorInterfaceInstance();
130 }
131 
Init()132 void SensorImpl::Init()
133 {
134     sensorInterface = NewSensorInterfaceInstance();
135     if (sensorInterface == nullptr) {
136         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
137     }
138 }
139 
GetAllSensorInfo(std::vector<HdfSensorInformation> & info)140 int32_t SensorImpl::GetAllSensorInfo(std::vector<HdfSensorInformation> &info)
141 {
142     if (sensorInterface == nullptr || sensorInterface->GetAllSensors == nullptr) {
143         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
144         return HDF_FAILURE;
145     }
146 
147     struct SensorInformation *sensorInfo = nullptr;
148     struct SensorInformation *tmp = nullptr;
149     int32_t count = 0;
150 
151     int32_t ret = sensorInterface->GetAllSensors(&sensorInfo, &count);
152     if (ret != SENSOR_SUCCESS) {
153         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
154         return ret;
155     }
156 
157     if (count <= 0) {
158         HDF_LOGE("%{public}s failed, count<=0", __func__);
159         return HDF_FAILURE;
160     }
161 
162     tmp = sensorInfo;
163     while (count--) {
164         HdfSensorInformation hdfSensorInfo;
165         std::string sensorName(tmp->sensorName);
166         hdfSensorInfo.sensorName = sensorName;
167         std::string vendorName(tmp->vendorName);
168         hdfSensorInfo.vendorName = vendorName;
169         std::string firmwareVersion(tmp->firmwareVersion);
170         hdfSensorInfo.firmwareVersion = firmwareVersion;
171         std::string hardwareVersion(tmp->hardwareVersion);
172         hdfSensorInfo.hardwareVersion = hardwareVersion;
173         hdfSensorInfo.sensorTypeId = tmp->sensorTypeId;
174         hdfSensorInfo.sensorId = tmp->sensorId;
175         hdfSensorInfo.maxRange = tmp->maxRange;
176         hdfSensorInfo.accuracy = tmp->accuracy;
177         hdfSensorInfo.power = tmp->power;
178         hdfSensorInfo.minDelay = tmp->minDelay;
179         hdfSensorInfo.maxDelay = tmp->maxDelay;
180         info.push_back(std::move(hdfSensorInfo));
181         tmp++;
182     }
183 
184     return HDF_SUCCESS;
185 }
186 
Enable(int32_t sensorId)187 int32_t SensorImpl::Enable(int32_t sensorId)
188 {
189     if (sensorInterface == nullptr || sensorInterface->Enable == nullptr) {
190         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
191         return HDF_FAILURE;
192     }
193 
194     int32_t ret = sensorInterface->Enable(sensorId);
195     if (ret != SENSOR_SUCCESS) {
196         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
197     }
198 
199     return ret;
200 }
201 
Disable(int32_t sensorId)202 int32_t SensorImpl::Disable(int32_t sensorId)
203 {
204     if (sensorInterface == nullptr || sensorInterface->Disable == nullptr) {
205         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
206         return HDF_FAILURE;
207     }
208 
209     int32_t ret = sensorInterface->Disable(sensorId);
210     if (ret != SENSOR_SUCCESS) {
211         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
212     }
213 
214     return ret;
215 }
216 
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)217 int32_t SensorImpl::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
218 {
219     if (sensorInterface == nullptr || sensorInterface->SetBatch == nullptr) {
220         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
221         return HDF_FAILURE;
222     }
223 
224     int32_t ret = sensorInterface->SetBatch(sensorId, samplingInterval, reportInterval);
225     if (ret != SENSOR_SUCCESS) {
226         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
227     }
228 
229     return ret;
230 }
231 
SetMode(int32_t sensorId,int32_t mode)232 int32_t SensorImpl::SetMode(int32_t sensorId, int32_t mode)
233 {
234     HDF_LOGI("%{public}s SetMode fail, sensorId %{public}d, mode %{public}d", __func__, sensorId, mode);
235     if (sensorInterface == nullptr || sensorInterface->SetMode == nullptr) {
236         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
237         return HDF_FAILURE;
238     }
239 
240     int32_t ret = sensorInterface->SetMode(sensorId, mode);
241     if (ret != SENSOR_SUCCESS) {
242         HDF_LOGE("%{public}s SetMode failed, error code is %{public}d", __func__, ret);
243     }
244 
245     return ret;
246 }
247 
SetOption(int32_t sensorId,uint32_t option)248 int32_t SensorImpl::SetOption(int32_t sensorId, uint32_t option)
249 {
250     if (sensorInterface == nullptr || sensorInterface->SetOption == nullptr) {
251         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
252         return HDF_FAILURE;
253     }
254 
255     int32_t ret = sensorInterface->SetOption(sensorId, option);
256     if (ret != SENSOR_SUCCESS) {
257         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
258     }
259 
260     return ret;
261 }
262 
Register(int32_t groupId,const sptr<ISensorCallback> & callbackObj)263 int32_t SensorImpl::Register(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
264 {
265     if (sensorInterface == nullptr || sensorInterface->Register == nullptr) {
266         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
267         return HDF_FAILURE;
268     }
269 
270     if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
271         HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
272         return SENSOR_INVALID_PARAM;
273     }
274 
275     std::lock_guard<std::mutex> lock(g_mutex);
276     auto groupCallBackIter = g_groupIdCallBackMap.find(groupId);
277     if (groupCallBackIter != g_groupIdCallBackMap.end()) {
278         auto callBackIter =
279             find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
280             [&callbackObj](const sptr<ISensorCallback> &callbackRegistered) {
281                 const sptr<IRemoteObject> &lhs = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackObj);
282                 const sptr<IRemoteObject> &rhs = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackRegistered);
283                 return lhs == rhs;
284             });
285         if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
286             int32_t addResult = AddSensorDeathRecipient(callbackObj);
287             if (addResult != SENSOR_SUCCESS) {
288                 return HDF_FAILURE;
289             }
290             g_groupIdCallBackMap[groupId].push_back(callbackObj);
291         }
292         return SENSOR_SUCCESS;
293     }
294 
295     int32_t addResult = AddSensorDeathRecipient(callbackObj);
296     if (addResult != SENSOR_SUCCESS) {
297         return HDF_FAILURE;
298     }
299     int32_t ret = HDF_FAILURE;
300     if (groupId == TRADITIONAL_SENSOR_TYPE) {
301         ret = sensorInterface->Register(groupId, TradtionalSensorDataCallback);
302     } else if (groupId == MEDICAL_SENSOR_TYPE) {
303         ret = sensorInterface->Register(groupId, MedicalSensorDataCallback);
304     }
305     if (ret != SENSOR_SUCCESS) {
306         int32_t removeResult = RemoveSensorDeathRecipient(callbackObj);
307         if (removeResult != SENSOR_SUCCESS) {
308             HDF_LOGE("%{public}s: callback RemoveSensorDeathRecipient fail, groupId[%{public}d]", __func__, groupId);
309         }
310         return ret;
311     }
312     std::vector<sptr<ISensorCallback>> remoteVec;
313     remoteVec.push_back(callbackObj);
314     g_groupIdCallBackMap[groupId] = remoteVec;
315     return ret;
316 }
317 
Unregister(int32_t groupId,const sptr<ISensorCallback> & callbackObj)318 int32_t SensorImpl::Unregister(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
319 {
320     std::lock_guard<std::mutex> lock(g_mutex);
321     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackObj);
322     int32_t ret = UnregisterImpl(groupId, remote.GetRefPtr());
323     if (ret != SENSOR_SUCCESS) {
324         HDF_LOGE("%{public}s: Unregister failed groupId[%{public}d]", __func__, groupId);
325     }
326     return ret;
327 }
328 
UnregisterImpl(int32_t groupId,IRemoteObject * callbackObj)329 int32_t SensorImpl::UnregisterImpl(int32_t groupId, IRemoteObject *callbackObj)
330 {
331     if (sensorInterface == nullptr || sensorInterface->Unregister == nullptr) {
332         HDF_LOGE("%{public}s: get sensor Module instance failed", __func__);
333         return HDF_FAILURE;
334     }
335 
336     if (groupId < TRADITIONAL_SENSOR_TYPE || groupId > MEDICAL_SENSOR_TYPE) {
337         HDF_LOGE("%{public}s: groupId [%{public}d] out of range", __func__, groupId);
338         return SENSOR_INVALID_PARAM;
339     }
340 
341     auto groupIdCallBackIter = g_groupIdCallBackMap.find(groupId);
342     if (groupIdCallBackIter == g_groupIdCallBackMap.end()) {
343         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
344         return HDF_FAILURE;
345     }
346 
347     auto callBackIter =
348         find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
349         [&callbackObj](const sptr<ISensorCallback> &callbackRegistered) {
350             return callbackObj == OHOS::HDI::hdi_objcast<ISensorCallback>(callbackRegistered).GetRefPtr();
351         });
352     if (callBackIter == g_groupIdCallBackMap[groupId].end()) {
353         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
354         return HDF_FAILURE;
355     }
356 
357     /**
358      * when there is only one item in the vector,can call the Unregister function.
359      * when there is more than one item in the vector, only need to remove the  callbackObj
360      * from the vector
361      */
362     if (g_groupIdCallBackMap[groupId].size() > CALLBACK_CTOUNT_THRESHOLD) {
363         int32_t removeResult = RemoveSensorDeathRecipient(*callBackIter);
364         if (removeResult != SENSOR_SUCCESS) {
365             HDF_LOGE("%{public}s: callback RemoveSensorDeathRecipient fail, groupId[%{public}d]", __func__, groupId);
366         }
367         g_groupIdCallBackMap[groupId].erase(callBackIter);
368         return SENSOR_SUCCESS;
369     }
370 
371     int32_t ret = HDF_FAILURE;
372     if (groupId == TRADITIONAL_SENSOR_TYPE) {
373         ret = sensorInterface->Unregister(groupId, TradtionalSensorDataCallback);
374     } else if (groupId == MEDICAL_SENSOR_TYPE) {
375         ret = sensorInterface->Unregister(groupId, MedicalSensorDataCallback);
376     }
377 
378     if (ret != SENSOR_SUCCESS) {
379         HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
380         return ret;
381     }
382 
383     int32_t removeResult = RemoveSensorDeathRecipient(*callBackIter);
384     if (removeResult != SENSOR_SUCCESS) {
385         HDF_LOGE("%{public}s: last callback RemoveSensorDeathRecipient fail, groupId[%{public}d]", __func__, groupId);
386     }
387     g_groupIdCallBackMap.erase(groupId);
388 
389     return ret;
390 }
391 
AddSensorDeathRecipient(const sptr<ISensorCallback> & callbackObj)392 int32_t SensorImpl::AddSensorDeathRecipient(const sptr<ISensorCallback> &callbackObj)
393 {
394     sptr<CallBackDeathRecipient> callBackDeathRecipient = new CallBackDeathRecipient(this);
395     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackObj);
396     bool result = remote->AddDeathRecipient(callBackDeathRecipient);
397     if (!result) {
398         HDF_LOGE("%{public}s: AddDeathRecipient fail", __func__);
399         return HDF_FAILURE;
400     }
401     g_callBackDeathRecipientMap[remote.GetRefPtr()] = callBackDeathRecipient;
402     return SENSOR_SUCCESS;
403 }
404 
RemoveSensorDeathRecipient(const sptr<ISensorCallback> & callbackObj)405 int32_t SensorImpl::RemoveSensorDeathRecipient(const sptr<ISensorCallback> &callbackObj)
406 {
407     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackObj);
408     auto callBackDeathRecipientIter = g_callBackDeathRecipientMap.find(remote.GetRefPtr());
409     if (callBackDeathRecipientIter == g_callBackDeathRecipientMap.end()) {
410         HDF_LOGE("%{public}s: not find recipient", __func__);
411         return HDF_FAILURE;
412     }
413     bool result = remote->RemoveDeathRecipient(callBackDeathRecipientIter->second);
414     g_callBackDeathRecipientMap.erase(callBackDeathRecipientIter);
415     if (!result) {
416         HDF_LOGE("%{public}s: RemoveDeathRecipient fail", __func__);
417         return HDF_FAILURE;
418     }
419     return SENSOR_SUCCESS;
420 }
421 
OnRemoteDied(const wptr<IRemoteObject> & object)422 void SensorImpl::OnRemoteDied(const wptr<IRemoteObject> &object)
423 {
424     HDF_LOGI("%{public}s: sensor OnRemoteDied enter", __func__);
425     std::lock_guard<std::mutex> lock(g_mutex);
426     sptr<IRemoteObject> callbackObject = object.promote();
427     if (callbackObject == nullptr) {
428         return;
429     }
430 
431     int32_t groupId = TRADITIONAL_SENSOR_TYPE;
432     auto traditionIter = g_groupIdCallBackMap.find(groupId);
433     if (traditionIter != g_groupIdCallBackMap.end()) {
434         auto callBackIter =
435         find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
436         [&callbackObject](const sptr<ISensorCallback> &callbackRegistered) {
437             return callbackObject.GetRefPtr() ==
438                 OHOS::HDI::hdi_objcast<ISensorCallback>(callbackRegistered).GetRefPtr();
439         });
440         if (callBackIter != g_groupIdCallBackMap[groupId].end()) {
441             int32_t ret = UnregisterImpl(groupId, callbackObject.GetRefPtr());
442             if (ret != SENSOR_SUCCESS) {
443                 HDF_LOGE("%{public}s: traditional Unregister failed groupId[%{public}d]", __func__, groupId);
444             }
445         }
446     }
447 
448     groupId = MEDICAL_SENSOR_TYPE;
449     auto medicalIter = g_groupIdCallBackMap.find(groupId);
450     if (medicalIter != g_groupIdCallBackMap.end()) {
451         auto callBackIter =
452         find_if(g_groupIdCallBackMap[groupId].begin(), g_groupIdCallBackMap[groupId].end(),
453         [&callbackObject](const sptr<ISensorCallback> &callbackRegistered) {
454             return callbackObject.GetRefPtr() ==
455                 OHOS::HDI::hdi_objcast<ISensorCallback>(callbackRegistered).GetRefPtr();
456         });
457         if (callBackIter != g_groupIdCallBackMap[groupId].end()) {
458             int32_t ret = UnregisterImpl(groupId, callbackObject.GetRefPtr());
459             if (ret != SENSOR_SUCCESS) {
460                 HDF_LOGE("%{public}s: medical Unregister failed groupId[%{public}d]", __func__, groupId);
461             }
462         }
463     }
464 }
465 } // namespace V1_0
466 } // namespace Sensor
467 } // namespace HDI
468 } // namespace OHOS
469