• 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 "sensor_uhdf_log.h"
20 #include "hitrace_meter.h"
21 #include "sensor_type.h"
22 #include "sensor_callback_vdi.h"
23 #include <hdf_remote_service.h>
24 #include "callback_death_recipient.h"
25 
26 constexpr int DISABLE_SENSOR = 0;
27 constexpr int REPORT_INTERVAL = 0;
28 constexpr int UNREGISTER_SENSOR = 0;
29 constexpr int REGISTER_SENSOR = 1;
30 constexpr int ENABLE_SENSOR = 1;
31 constexpr int COMMON_REPORT_FREQUENCY = 1000000000;
32 enum BatchSeniorMode {
33         SA = 0,
34         SDC = 1
35 };
36 
37 #define HDF_LOG_TAG uhdf_sensor_service
38 
39 namespace OHOS {
40 namespace HDI {
41 namespace Sensor {
42 namespace V2_0 {
43 namespace {
44     constexpr int32_t CALLBACK_CTOUNT_THRESHOLD = 1;
45     using CallBackDeathRecipientMap = std::unordered_map<IRemoteObject *, sptr<CallBackDeathRecipient>>;
46     CallBackDeathRecipientMap g_callBackDeathRecipientMap;
47     std::mutex g_mutex;
48 }
49 
SensorIfService()50 SensorIfService::SensorIfService()
51 {
52     int32_t ret = GetSensorVdiImpl();
53     if (ret != HDF_SUCCESS) {
54         HDF_LOGE("%{public}s: get sensor vdi instance failed", __func__);
55     }
56 }
57 
~SensorIfService()58 SensorIfService::~SensorIfService()
59 {
60     if (vdi_ != nullptr) {
61         HdfCloseVdi(vdi_);
62     }
63     RemoveDeathNotice(TRADITIONAL_SENSOR_TYPE);
64     RemoveDeathNotice(MEDICAL_SENSOR_TYPE);
65 }
66 
GetSensorVdiImpl()67 int32_t SensorIfService::GetSensorVdiImpl()
68 {
69     struct OHOS::HDI::Sensor::V1_1::WrapperSensorVdi *wrapperSensorVdi = nullptr;
70     uint32_t version = 0;
71     vdi_ = HdfLoadVdi(HDI_SENSOR_VDI_LIBNAME);
72     if (vdi_ == nullptr || vdi_->vdiBase == nullptr) {
73         HDF_LOGE("%{public}s: load sensor vdi failed", __func__);
74         return HDF_FAILURE;
75     }
76 
77     version = HdfGetVdiVersion(vdi_);
78     if (version != 1) {
79         HDF_LOGE("%{public}s: get sensor vdi version failed", __func__);
80         return HDF_FAILURE;
81     }
82 
83     wrapperSensorVdi = reinterpret_cast<struct OHOS::HDI::Sensor::V1_1::WrapperSensorVdi *>(vdi_->vdiBase);
84     sensorVdiImpl_ = wrapperSensorVdi->sensorModule;
85     if (sensorVdiImpl_ == nullptr) {
86         HDF_LOGE("%{public}s: get sensor impl failed", __func__);
87         return HDF_FAILURE;
88     }
89 
90     return HDF_SUCCESS;
91 }
92 
Init()93 int32_t SensorIfService::Init()
94 {
95     if (sensorVdiImpl_ == nullptr) {
96         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
97         return HDF_FAILURE;
98     }
99     int32_t ret = sensorVdiImpl_->Init();
100     if (ret != SENSOR_SUCCESS) {
101         HDF_LOGE("%{public}s Init failed, error code is %{public}d", __func__, ret);
102     }
103 
104     return ret;
105 }
106 
GetSensorCb(int32_t groupId,const sptr<ISensorCallback> & callbackObj,bool cbFlag)107 sptr<SensorCallbackVdi> SensorIfService::GetSensorCb(int32_t groupId, const sptr<ISensorCallback> &callbackObj,
108     bool cbFlag)
109 {
110     if (groupId == TRADITIONAL_SENSOR_TYPE) {
111         if (cbFlag) {
112             traditionalCb = new SensorCallbackVdi(callbackObj);
113         }
114         return traditionalCb;
115     }
116     if (cbFlag) {
117         medicalCb = new SensorCallbackVdi(callbackObj);
118     }
119     return medicalCb;
120 }
121 
GetAllSensorInfo(std::vector<HdfSensorInformation> & info)122 int32_t SensorIfService::GetAllSensorInfo(std::vector<HdfSensorInformation> &info)
123 {
124     HDF_LOGD("%{public}s: Enter the GetAllSensorInfo function.", __func__);
125     if (sensorVdiImpl_ == nullptr) {
126         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
127         return HDF_FAILURE;
128     }
129 
130     std::vector<OHOS::HDI::Sensor::V1_1::HdfSensorInformationVdi> sensorInfoVdi = {};
131     StartTrace(HITRACE_TAG_HDF, "GetAllSensorInfo");
132     int32_t ret = sensorVdiImpl_->GetAllSensorInfo(sensorInfoVdi);
133     if (ret != SENSOR_SUCCESS) {
134         HDF_LOGE("%{public}s GetAllSensors failed, error code is %{public}d", __func__, ret);
135         return ret;
136     }
137     FinishTrace(HITRACE_TAG_HDF);
138 
139     if (sensorInfoVdi.empty()) {
140         HDF_LOGE("%{public}s no sensor info in list", __func__);
141         return HDF_FAILURE;
142     }
143 
144     for (const auto &it : sensorInfoVdi) {
145         struct HdfSensorInformation sensorInfo = {};
146         sensorInfo.sensorName = it.sensorName;
147         sensorInfo.vendorName = it.vendorName;
148         sensorInfo.firmwareVersion = it.firmwareVersion;
149         sensorInfo.hardwareVersion = it.hardwareVersion;
150         sensorInfo.sensorTypeId = it.sensorTypeId;
151         sensorInfo.sensorId = it.sensorId;
152         sensorInfo.maxRange = it.maxRange;
153         sensorInfo.accuracy = it.accuracy;
154         sensorInfo.power = it.power;
155         sensorInfo.minDelay = it.minDelay;
156         sensorInfo.maxDelay = it.maxDelay;
157         sensorInfo.fifoMaxEventCount = it.fifoMaxEventCount;
158         info.push_back(std::move(sensorInfo));
159     }
160 
161     return HDF_SUCCESS;
162 }
163 
Enable(int32_t sensorId)164 int32_t SensorIfService::Enable(int32_t sensorId)
165 {
166     uint32_t serviceId = static_cast<uint32_t>(HdfRemoteGetCallingPid());
167     HDF_LOGD("%{public}s:Enter the Enable function, sensorId %{public}d, service %{public}d",
168              __func__, sensorId, serviceId);
169     if (!SensorClientsManager::GetInstance()->IsUpadateSensorState(sensorId, serviceId, ENABLE_SENSOR)) {
170         return HDF_SUCCESS;
171     }
172 
173     if (sensorVdiImpl_ == nullptr) {
174         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
175         return HDF_FAILURE;
176     }
177 
178     StartTrace(HITRACE_TAG_HDF, "Enable");
179     int32_t ret = sensorVdiImpl_->Enable(sensorId);
180     if (ret != SENSOR_SUCCESS) {
181         HDF_LOGE("%{public}s Enable failed, error code is %{public}d", __func__, ret);
182     } else {
183         SensorClientsManager::GetInstance()->OpenSensor(sensorId, serviceId);
184     }
185     FinishTrace(HITRACE_TAG_HDF);
186 
187     return ret;
188 }
189 
Disable(int32_t sensorId)190 int32_t SensorIfService::Disable(int32_t sensorId)
191 {
192     uint32_t serviceId = static_cast<uint32_t>(HdfRemoteGetCallingPid());
193     HDF_LOGD("%{public}s:Enter the Disable function, sensorId %{public}d, service %{public}d",
194              __func__, sensorId, serviceId);
195     if (!SensorClientsManager::GetInstance()->IsUpadateSensorState(sensorId, serviceId, DISABLE_SENSOR)) {
196         HDF_LOGE("%{public}s There are still some services enable", __func__);
197         return HDF_SUCCESS;
198     }
199 
200     if (sensorVdiImpl_ == nullptr) {
201         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
202         return HDF_FAILURE;
203     }
204 
205     int32_t ret;
206     if (SensorClientsManager::GetInstance()->IsExistSdcSensorEnable(sensorId)) {
207         ret = sensorVdiImpl_->SetSaBatch(sensorId, REPORT_INTERVAL, REPORT_INTERVAL);
208         if (ret != SENSOR_SUCCESS) {
209             HDF_LOGE("%{public}s SetBatchSenior SA failed, error code is %{public}d", __func__, ret);
210             return ret;
211         }
212         return HDF_SUCCESS;
213     }
214 
215     StartTrace(HITRACE_TAG_HDF, "Disable");
216     ret = sensorVdiImpl_->Disable(sensorId);
217     if (ret != SENSOR_SUCCESS) {
218         HDF_LOGE("%{public}s Disable failed, error code is %{public}d", __func__, ret);
219     }
220     FinishTrace(HITRACE_TAG_HDF);
221 
222     return ret;
223 }
224 
SetBatch(int32_t sensorId,int64_t samplingInterval,int64_t reportInterval)225 int32_t SensorIfService::SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval)
226 {
227     HDF_LOGD("%{public}s: sensorId is %{public}d, samplingInterval is [%{public}" PRId64 "], \
228         reportInterval is [%{public}" PRId64 "].", __func__, sensorId, samplingInterval, reportInterval);
229     uint32_t serviceId = static_cast<uint32_t>(HdfRemoteGetCallingPid());
230 
231     StartTrace(HITRACE_TAG_HDF, "SetBatch");
232     int32_t ret = SetBatchSenior(serviceId, sensorId, SA, samplingInterval, reportInterval);
233     if (ret != SENSOR_SUCCESS) {
234         HDF_LOGE("%{public}s SetBatch failed, error code is %{public}d", __func__, ret);
235     }
236     FinishTrace(HITRACE_TAG_HDF);
237 
238     return ret;
239 }
240 
SetBatchSenior(int32_t serviceId,int32_t sensorId,int32_t mode,int64_t samplingInterval,int64_t reportInterval)241 int32_t SensorIfService::SetBatchSenior(int32_t serviceId, int32_t sensorId, int32_t mode, int64_t samplingInterval,
242                                         int64_t reportInterval)
243 {
244     HDF_LOGD("%{public}s: serviceId is %{public}d, sensorId is %{public}d, mode is %{public}d, samplingInterval is "
245              "[%{public}" PRId64 "], reportInterval is [%{public}" PRId64 "].", __func__, serviceId, sensorId, mode,
246              samplingInterval, reportInterval);
247     if (sensorVdiImpl_ == nullptr) {
248         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
249         return HDF_FAILURE;
250     }
251     StartTrace(HITRACE_TAG_HDF, "SetBatchSenior");
252     SensorClientsManager::GetInstance()->SetClientSenSorConfig(sensorId, serviceId, samplingInterval, reportInterval);
253 
254     int64_t saSamplingInterval = samplingInterval;
255     int64_t saReportInterval = reportInterval;
256     int64_t sdcSamplingInterval = samplingInterval;
257     int64_t sdcReportInterval = reportInterval;
258 
259     SensorClientsManager::GetInstance()->SetSensorBestConfig(sensorId, saSamplingInterval, saReportInterval);
260     SensorClientsManager::GetInstance()->SetSdcSensorBestConfig(sensorId, sdcSamplingInterval, sdcReportInterval);
261 
262     samplingInterval = saSamplingInterval < sdcSamplingInterval ? saSamplingInterval : sdcSamplingInterval;
263     reportInterval = saReportInterval < sdcReportInterval ? saReportInterval : sdcReportInterval;
264 
265     int32_t ret = sensorVdiImpl_->SetBatch(sensorId, samplingInterval, reportInterval);
266     if (ret != SENSOR_SUCCESS) {
267         HDF_LOGE("%{public}s SetBatch failed, error code is %{public}d", __func__, ret);
268         return ret;
269     }
270     if (mode == SA) {
271         SensorClientsManager::GetInstance()->UpdateSensorConfig(sensorId, saSamplingInterval, saReportInterval);
272         SensorClientsManager::GetInstance()->UpdateClientPeriodCount(sensorId, saSamplingInterval, saReportInterval);
273     }
274     if (mode == SDC) {
275         SensorClientsManager::GetInstance()->UpdateSdcSensorConfig(sensorId, sdcSamplingInterval, sdcReportInterval);
276     }
277     SensorClientsManager::GetInstance()->GetSensorBestConfig(sensorId, samplingInterval, reportInterval);
278     ret = sensorVdiImpl_->SetSaBatch(sensorId, samplingInterval, samplingInterval);
279     if (ret != SENSOR_SUCCESS) {
280         HDF_LOGE("%{public}s SetBatch failed, error code is %{public}d", __func__, ret);
281     }
282     FinishTrace(HITRACE_TAG_HDF);
283 
284     return ret;
285 }
286 
SetMode(int32_t sensorId,int32_t mode)287 int32_t SensorIfService::SetMode(int32_t sensorId, int32_t mode)
288 {
289     HDF_LOGD("%{public}s: Enter the SetMode function, sensorId is %{public}d, mode is %{public}d",
290         __func__, sensorId, mode);
291     if (sensorVdiImpl_ == nullptr) {
292         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
293         return HDF_FAILURE;
294     }
295 
296     StartTrace(HITRACE_TAG_HDF, "SetMode");
297     int32_t ret = sensorVdiImpl_->SetMode(sensorId, mode);
298     if (ret != SENSOR_SUCCESS) {
299         HDF_LOGE("%{public}s SetMode failed, error code is %{public}d", __func__, ret);
300     }
301     FinishTrace(HITRACE_TAG_HDF);
302 
303     return ret;
304 }
305 
SetOption(int32_t sensorId,uint32_t option)306 int32_t SensorIfService::SetOption(int32_t sensorId, uint32_t option)
307 {
308     HDF_LOGD("%{public}s: Enter the SetOption function, sensorId is %{public}d, option is %{public}u",
309         __func__, sensorId, option);
310     if (sensorVdiImpl_ == nullptr) {
311         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
312         return HDF_FAILURE;
313     }
314 
315     StartTrace(HITRACE_TAG_HDF, "SetOption");
316     int32_t ret = sensorVdiImpl_->SetOption(sensorId, option);
317     if (ret != SENSOR_SUCCESS) {
318         HDF_LOGE("%{public}s SetOption failed, error code is %{public}d", __func__, ret);
319     }
320     FinishTrace(HITRACE_TAG_HDF);
321 
322     return ret;
323 }
324 
Register(int32_t groupId,const sptr<ISensorCallback> & callbackObj)325 int32_t SensorIfService::Register(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
326 {
327     int32_t ret = HDF_SUCCESS;
328     uint32_t serviceId = static_cast<uint32_t>(HdfRemoteGetCallingPid());
329     HDF_LOGD("%{public}s:Enter the Register function, groupId %{public}d, service %{public}d",
330         __func__, groupId, serviceId);
331     std::lock_guard<std::mutex> lock(g_mutex);
332     int32_t result = AddCallbackMap(groupId, callbackObj);
333     if (result !=SENSOR_SUCCESS) {
334         HDF_LOGE("%{public}s: AddCallbackMap failed groupId[%{public}d]", __func__, groupId);
335     }
336     if (SensorClientsManager::GetInstance()->IsClientsEmpty(groupId)) {
337         if (sensorVdiImpl_ == nullptr) {
338             HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
339             return HDF_FAILURE;
340         }
341         StartTrace(HITRACE_TAG_HDF, "Register");
342         sptr<SensorCallbackVdi> sensorCb = GetSensorCb(groupId, callbackObj, REGISTER_SENSOR);
343         if (sensorCb == nullptr) {
344             HDF_LOGE("%{public}s: get sensorcb fail, groupId[%{public}d]", __func__, groupId);
345             return HDF_FAILURE;
346         }
347         ret = sensorVdiImpl_->Register(groupId, sensorCb);
348         if (ret != SENSOR_SUCCESS) {
349             HDF_LOGE("%{public}s Register failed, error code is %{public}d", __func__, ret);
350             int32_t removeResult = RemoveSensorDeathRecipient(callbackObj);
351             if (removeResult != SENSOR_SUCCESS) {
352                 HDF_LOGE("%{public}s: callback RemoveSensorDeathRecipient fail, groupId[%{public}d]",
353                     __func__, groupId);
354             }
355         } else {
356             SensorClientsManager::GetInstance()->ReportDataCbRegister(groupId, serviceId, callbackObj);
357         }
358         FinishTrace(HITRACE_TAG_HDF);
359     } else {
360         SensorClientsManager::GetInstance()->ReportDataCbRegister(groupId, serviceId, callbackObj);
361     }
362     return ret;
363 }
364 
Unregister(int32_t groupId,const sptr<ISensorCallback> & callbackObj)365 int32_t SensorIfService::Unregister(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
366 {
367     uint32_t serviceId = static_cast<uint32_t>(HdfRemoteGetCallingPid());
368     HDF_LOGD("%{public}s:Enter the Unregister function, groupId %{public}d, service %{public}d",
369         __func__, groupId, serviceId);
370     std::lock_guard<std::mutex> lock(g_mutex);
371     int32_t result = RemoveCallbackMap(groupId, serviceId, callbackObj);
372     if (result !=SENSOR_SUCCESS) {
373         HDF_LOGE("%{public}s: RemoveCallbackMap failed groupId[%{public}d]", __func__, groupId);
374     }
375     SensorClientsManager::GetInstance()->ReportDataCbUnRegister(groupId, serviceId, callbackObj);
376     if (!SensorClientsManager::GetInstance()->IsClientsEmpty(groupId)) {
377         HDF_LOGD("%{public}s: clients is not empty, do not unregister", __func__);
378         return HDF_SUCCESS;
379     }
380 
381     if (sensorVdiImpl_ == nullptr) {
382         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
383         return HDF_FAILURE;
384     }
385 
386     StartTrace(HITRACE_TAG_HDF, "Unregister");
387     sptr<SensorCallbackVdi> sensorCb = GetSensorCb(groupId, callbackObj, UNREGISTER_SENSOR);
388     if (sensorCb == nullptr) {
389         HDF_LOGE("%{public}s: get sensorcb fail, groupId[%{public}d]", __func__, groupId);
390         return HDF_FAILURE;
391     }
392     int32_t ret = sensorVdiImpl_->Unregister(groupId, sensorCb);
393     if (ret != SENSOR_SUCCESS) {
394         HDF_LOGE("%{public}s: Unregister failed, error code is %{public}d", __func__, ret);
395     }
396     FinishTrace(HITRACE_TAG_HDF);
397 
398     return ret;
399 }
400 
AddCallbackMap(int32_t groupId,const sptr<ISensorCallback> & callbackObj)401 int32_t SensorIfService::AddCallbackMap(int32_t groupId, const sptr<ISensorCallback> &callbackObj)
402 {
403     auto groupCallBackIter = callbackMap.find(groupId);
404     if (groupCallBackIter != callbackMap.end()) {
405         auto callBackIter =
406             find_if(callbackMap[groupId].begin(), callbackMap[groupId].end(),
407             [&callbackObj](const sptr<ISensorCallback> &callbackRegistered) {
408                 const sptr<IRemoteObject> &lhs = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackObj);
409                 const sptr<IRemoteObject> &rhs = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackRegistered);
410                 return lhs == rhs;
411             });
412         if (callBackIter == callbackMap[groupId].end()) {
413             int32_t addResult = AddSensorDeathRecipient(callbackObj);
414             if (addResult != SENSOR_SUCCESS) {
415                 return HDF_FAILURE;
416             }
417             callbackMap[groupId].push_back(callbackObj);
418         }
419     } else {
420         int32_t addResult = AddSensorDeathRecipient(callbackObj);
421         if (addResult != SENSOR_SUCCESS) {
422             return HDF_FAILURE;
423         }
424         std::vector<sptr<ISensorCallback>> remoteVec;
425         remoteVec.push_back(callbackObj);
426         callbackMap[groupId] = remoteVec;
427     }
428     return SENSOR_SUCCESS;
429 }
430 
RemoveCallbackMap(int32_t groupId,int serviceId,const sptr<ISensorCallback> & callbackObj)431 int32_t SensorIfService::RemoveCallbackMap(int32_t groupId, int serviceId, const sptr<ISensorCallback> &callbackObj)
432 {
433     auto groupIdCallBackIter = callbackMap.find(groupId);
434     if (groupIdCallBackIter == callbackMap.end()) {
435         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
436         return HDF_FAILURE;
437     }
438     auto callBackIter =
439         find_if(callbackMap[groupId].begin(), callbackMap[groupId].end(),
440         [&callbackObj](const sptr<ISensorCallback> &callbackRegistered) {
441             const sptr<IRemoteObject> &lhs = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackObj);
442             const sptr<IRemoteObject> &rhs = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackRegistered);
443             return lhs == rhs;
444         });
445     if (callBackIter == callbackMap[groupId].end()) {
446         HDF_LOGE("%{public}s: groupId [%{public}d] callbackObj not registered", __func__, groupId);
447         return HDF_FAILURE;
448     }
449     int32_t removeResult = RemoveSensorDeathRecipient(*callBackIter);
450     if (removeResult != SENSOR_SUCCESS) {
451         HDF_LOGE("%{public}s: last callback RemoveSensorDeathRecipient fail, groupId[%{public}d]", __func__, groupId);
452     }
453     if (callbackMap[groupId].size() > CALLBACK_CTOUNT_THRESHOLD) {
454         callbackMap[groupId].erase(callBackIter);
455     } else {
456         callbackMap.erase(groupId);
457     }
458     std::unordered_map<int, std::set<int>> sensorEnabled = SensorClientsManager::GetInstance()->GetSensorUsed();
459     for (auto iter : sensorEnabled) {
460         if (iter.second.find(serviceId) == iter.second.end()) {
461             continue;
462         }
463         if (!SensorClientsManager::GetInstance()->IsUpadateSensorState(iter.first, serviceId, DISABLE_SENSOR)) {
464             continue;
465         }
466         std::unordered_map<int, std::set<int>> sensorUsed = SensorClientsManager::GetInstance()->GetSensorUsed();
467         if (sensorUsed.find(iter.first) == sensorUsed.end()) {
468             if (sensorVdiImpl_ == nullptr) {
469                 HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
470                 return HDF_FAILURE;
471             }
472             int32_t ret = sensorVdiImpl_->Disable(iter.first);
473             if (ret != SENSOR_SUCCESS) {
474                 HDF_LOGE("%{public}s Disable failed, error code is %{public}d", __func__, ret);
475             }
476         }
477     }
478     return SENSOR_SUCCESS;
479 }
480 
AddSensorDeathRecipient(const sptr<ISensorCallback> & callbackObj)481 int32_t SensorIfService::AddSensorDeathRecipient(const sptr<ISensorCallback> &callbackObj)
482 {
483     sptr<CallBackDeathRecipient> callBackDeathRecipient = new CallBackDeathRecipient(this);
484     if (callBackDeathRecipient == nullptr) {
485         HDF_LOGE("%{public}s: new CallBackDeathRecipient fail", __func__);
486         return HDF_FAILURE;
487     }
488     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackObj);
489     bool result = remote->AddDeathRecipient(callBackDeathRecipient);
490     if (!result) {
491         HDF_LOGE("%{public}s: AddDeathRecipient fail", __func__);
492         return HDF_FAILURE;
493     }
494     g_callBackDeathRecipientMap[remote.GetRefPtr()] = callBackDeathRecipient;
495     return SENSOR_SUCCESS;
496 }
497 
RemoveSensorDeathRecipient(const sptr<ISensorCallback> & callbackObj)498 int32_t SensorIfService::RemoveSensorDeathRecipient(const sptr<ISensorCallback> &callbackObj)
499 {
500     const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<ISensorCallback>(callbackObj);
501     auto callBackDeathRecipientIter = g_callBackDeathRecipientMap.find(remote.GetRefPtr());
502     if (callBackDeathRecipientIter == g_callBackDeathRecipientMap.end()) {
503         HDF_LOGE("%{public}s: not find recipient", __func__);
504         return HDF_FAILURE;
505     }
506     bool result = remote->RemoveDeathRecipient(callBackDeathRecipientIter->second);
507     g_callBackDeathRecipientMap.erase(callBackDeathRecipientIter);
508     if (!result) {
509         HDF_LOGE("%{public}s: RemoveDeathRecipient fail", __func__);
510         return HDF_FAILURE;
511     }
512     return SENSOR_SUCCESS;
513 }
514 
OnRemoteDied(const wptr<IRemoteObject> & object)515 void SensorIfService::OnRemoteDied(const wptr<IRemoteObject> &object)
516 {
517     std::lock_guard<std::mutex> lock(g_mutex);
518     sptr<IRemoteObject> callbackObject = object.promote();
519     if (callbackObject == nullptr) {
520         return;
521     }
522 
523     for (int32_t groupId = TRADITIONAL_SENSOR_TYPE; groupId < SENSOR_GROUP_TYPE_MAX; groupId++) {
524         auto groupIdIter = callbackMap.find(groupId);
525         if (groupIdIter == callbackMap.end()) {
526             continue;
527         }
528         auto callBackIter =
529         find_if(callbackMap[groupId].begin(), callbackMap[groupId].end(),
530         [&callbackObject](const sptr<ISensorCallback> &callbackRegistered) {
531             return callbackObject.GetRefPtr() ==
532                 (OHOS::HDI::hdi_objcast<ISensorCallback>(callbackRegistered)).GetRefPtr();
533         });
534         if (callBackIter != callbackMap[groupId].end()) {
535             int32_t serviceId = SensorClientsManager::GetInstance()->GetServiceId(groupId, *callBackIter);
536             if (serviceId == HDF_FAILURE) {
537                 HDF_LOGE("%{public}s: GetServiceId failed", __func__);
538             }
539             int32_t ret = RemoveCallbackMap(groupId, serviceId, *callBackIter);
540             if (ret != SENSOR_SUCCESS) {
541                 HDF_LOGE("%{public}s: Unregister failed groupId[%{public}d]", __func__, groupId);
542             }
543             if (!SensorClientsManager::GetInstance()->IsClientsEmpty(groupId)) {
544                 HDF_LOGD("%{public}s: clients is not empty, do not unregister", __func__);
545                 continue;
546             }
547             if (sensorVdiImpl_ == nullptr) {
548                 HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
549                 continue;
550             }
551             sptr<SensorCallbackVdi> sensorCb = GetSensorCb(groupId, *callBackIter, UNREGISTER_SENSOR);
552             if (sensorCb == nullptr) {
553                 HDF_LOGE("%{public}s: get sensorcb fail, groupId[%{public}d]", __func__, groupId);
554                 continue;
555             }
556             ret = sensorVdiImpl_->Unregister(groupId, sensorCb);
557             if (ret != SENSOR_SUCCESS) {
558                 HDF_LOGE("%{public}s: Unregister failed, error code is %{public}d", __func__, ret);
559             }
560         }
561     }
562 }
563 
RemoveDeathNotice(int32_t sensorType)564 void  SensorIfService::RemoveDeathNotice(int32_t sensorType)
565 {
566     auto iter = callbackMap.find(sensorType);
567     if (iter != callbackMap.end()) {
568         return;
569     }
570     for (auto callback : callbackMap[sensorType]) {
571         const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast<ISensorCallback>(callback);
572         auto recipientIter = g_callBackDeathRecipientMap.find(remote.GetRefPtr());
573         if (recipientIter != g_callBackDeathRecipientMap.end()) {
574             bool removeResult = remote->RemoveDeathRecipient(recipientIter->second);
575             if (!removeResult) {
576                 HDF_LOGE("%{public}s: sensor destroyed, callback RemoveSensorDeathRecipient fail", __func__);
577             }
578         }
579     }
580 }
581 
ReadData(int32_t sensorId,std::vector<HdfSensorEvents> & event)582 int32_t SensorIfService::ReadData(int32_t sensorId, std::vector<HdfSensorEvents> &event)
583 {
584     HDF_LOGD("%{public}s: Enter the ReadData function", __func__);
585     if (sensorVdiImpl_ == nullptr) {
586         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
587         return HDF_FAILURE;
588     }
589 
590     return HDF_SUCCESS;
591 }
592 
SetSdcSensor(int32_t sensorId,bool enabled,int32_t rateLevel)593 int32_t SensorIfService::SetSdcSensor(int32_t sensorId, bool enabled, int32_t rateLevel)
594 {
595     HDF_LOGD("%{public}s: Enter the SetSdcSensor function, sensorId is %{public}d, enabled is %{public}u, \
596              rateLevel is %{public}u", __func__, sensorId, enabled, rateLevel);
597     if (sensorVdiImpl_ == nullptr) {
598         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
599         return HDF_FAILURE;
600     }
601     StartTrace(HITRACE_TAG_HDF, "SetSdcSensor");
602     int32_t ret;
603     int64_t samplingInterval = rateLevel == REPORT_INTERVAL ? REPORT_INTERVAL : COMMON_REPORT_FREQUENCY / rateLevel;
604     int64_t reportInterval = REPORT_INTERVAL;
605     uint32_t serviceId = static_cast<uint32_t>(HdfRemoteGetCallingPid());
606     if (enabled) {
607         ret = SetBatchSenior(serviceId, sensorId, SDC, samplingInterval, reportInterval);
608         if (ret != SENSOR_SUCCESS) {
609             HDF_LOGE("%{public}s SetBatchSenior SDC failed, error code is %{public}d", __func__, ret);
610             return ret;
611         }
612         ret = sensorVdiImpl_->Enable(sensorId);
613         if (ret != SENSOR_SUCCESS) {
614             HDF_LOGE("%{public}s Enable failed, error code is %{public}d", __func__, ret);
615             return ret;
616         }
617     } else {
618         SensorClientsManager::GetInstance()->EraseSdcSensorBestConfig(sensorId);
619         ret = Disable(sensorId);
620         if (ret != SENSOR_SUCCESS) {
621             HDF_LOGE("%{public}s Disable failed, error code is %{public}d", __func__, ret);
622             return ret;
623         }
624         SensorClientsManager::GetInstance()->GetSensorBestConfig(sensorId, samplingInterval, reportInterval);
625         ret = sensorVdiImpl_->SetSaBatch(sensorId, samplingInterval, samplingInterval);
626         if (ret != SENSOR_SUCCESS) {
627             HDF_LOGE("%{public}s SetSaBatch failed, error code is %{public}d", __func__, ret);
628             return ret;
629         }
630     }
631     FinishTrace(HITRACE_TAG_HDF);
632     return ret;
633 }
634 
GetSdcSensorInfo(std::vector<SdcSensorInfo> & sdcSensorInfo)635 int32_t SensorIfService::GetSdcSensorInfo(std::vector<SdcSensorInfo>& sdcSensorInfo)
636 {
637     HDF_LOGD("%{public}s: Enter the GetSdcSensorInfo function", __func__);
638     if (sensorVdiImpl_ == nullptr) {
639         HDF_LOGE("%{public}s: get sensor vdi impl failed", __func__);
640         return HDF_FAILURE;
641     }
642 
643     std::vector<OHOS::HDI::Sensor::V1_1::SdcSensorInfoVdi> sdcSensorInfoVdi;
644     StartTrace(HITRACE_TAG_HDF, "GetSdcSensorInfo");
645     int32_t ret = sensorVdiImpl_->GetSdcSensorInfo(sdcSensorInfoVdi);
646     if (ret != SENSOR_SUCCESS) {
647         HDF_LOGE("%{public}s GetSdcSensorInfo failed, error code is %{public}d", __func__, ret);
648     }
649     FinishTrace(HITRACE_TAG_HDF);
650 
651     for (auto infoVdi : sdcSensorInfoVdi) {
652         SdcSensorInfo info;
653         info.offset = infoVdi.offset;
654         info.sensorId = infoVdi.sensorId;
655         info.ddrSize = infoVdi.ddrSize;
656         info.minRateLevel = infoVdi.minRateLevel;
657         info.maxRateLevel = infoVdi.maxRateLevel;
658         info.memAddr = infoVdi.memAddr;
659         info.reserved = infoVdi.reserved;
660         sdcSensorInfo.push_back(std::move(info));
661     }
662 
663     return ret;
664 }
665 
SensorInterfaceImplGetInstance(void)666 extern "C" ISensorInterface *SensorInterfaceImplGetInstance(void)
667 {
668     SensorIfService *impl = new (std::nothrow) SensorIfService();
669     if (impl == nullptr) {
670         return nullptr;
671     }
672 
673     int32_t ret = impl->Init();
674     if (ret != HDF_SUCCESS) {
675         HDF_LOGE("%{public}s: service init failed, error code is %{public}d", __func__, ret);
676         delete impl;
677         return nullptr;
678     }
679 
680     return impl;
681 }
682 } // namespace V2_0
683 } // namespace Sensor
684 } // namespace HDI
685 } // namespace OHOS