• 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 #include "hdi_connection.h"
16 
17 #include <map>
18 #include <thread>
19 
20 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
21 #include "hisysevent.h"
22 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
23 #include "iproxy_broker.h"
24 #include "v3_0/isensor_interface.h"
25 
26 #include "sensor_agent_type.h"
27 #include "sensor_errors.h"
28 #include "sensor_event_callback.h"
29 #include "sensor_plug_callback.h"
30 #include "sensor_xcollie.h"
31 
32 #undef LOG_TAG
33 #define LOG_TAG "HdiConnection"
34 
35 namespace OHOS {
36 namespace Sensors {
37 using namespace OHOS::HiviewDFX;
38 using OHOS::HDI::Sensor::V3_0::ISensorInterface;
39 using OHOS::HDI::Sensor::V3_0::ISensorCallback;
40 using OHOS::HDI::Sensor::V3_0::HdfSensorInformation;
41 using OHOS::HDI::Sensor::V3_0::ISensorPlugCallback;
42 namespace {
43 sptr<ISensorInterface> g_sensorInterface = nullptr;
44 sptr<ISensorCallback> g_eventCallback = nullptr;
45 sptr<ISensorPlugCallback> g_plugCallback = nullptr;
46 std::map<SensorDescription, SensorBasicInfo> g_sensorBasicInfoMap;
47 std::mutex g_sensorBasicInfoMutex;
48 std::mutex g_sensorInterfaceMutex;
49 constexpr int32_t DEFAULT_GROUP_ID = 0;
50 constexpr int32_t GET_HDI_SERVICE_COUNT = 25;
51 constexpr uint32_t WAIT_MS = 200;
52 constexpr int32_t HEADPOSTURE_FIFO_COUNT = 5;
53 }  // namespace
54 
55 ReportDataCb HdiConnection::reportDataCb_ = nullptr;
56 DevicePlugCallback HdiConnection::reportPlugDataCb_ = nullptr;
57 sptr<ReportDataCallback> HdiConnection::reportDataCallback_ = nullptr;
58 
ConnectHdi()59 int32_t HdiConnection::ConnectHdi()
60 {
61     CALL_LOG_ENTER;
62     int32_t retry = 0;
63     while (retry < GET_HDI_SERVICE_COUNT) {
64         std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
65         g_sensorInterface = ISensorInterface::Get();
66         if (g_sensorInterface != nullptr) {
67             SEN_HILOGI("Connect v3_0 hdi success");
68             g_eventCallback = new (std::nothrow) SensorEventCallback();
69             CHKPR(g_eventCallback, ERR_NO_INIT);
70             g_plugCallback = new (std::nothrow) SensorPlugCallback();
71             CHKPR(g_plugCallback, ERR_NO_INIT);
72             RegisterHdiDeathRecipient();
73             return ERR_OK;
74         }
75         retry++;
76         SEN_HILOGW("Connect hdi service failed, retry:%{public}d", retry);
77         std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_MS));
78     }
79 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
80     HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
81         HiSysEvent::EventType::FAULT, "PKG_NAME", "ConnectHdi", "ERROR_CODE", CONNECT_SENSOR_HDF_ERR);
82 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
83     SEN_HILOGE("Connect V3_0 hdi failed");
84     return ERR_NO_INIT;
85 }
86 
GetSensorList(std::vector<Sensor> & sensorList)87 int32_t HdiConnection::GetSensorList(std::vector<Sensor> &sensorList)
88 {
89     CALL_LOG_ENTER;
90     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
91     CHKPR(g_sensorInterface, ERR_NO_INIT);
92     std::vector<HdfSensorInformation> sensorInfos;
93     SensorXcollie sensorXcollie("HdiConnection:GetSensorList", XCOLLIE_TIMEOUT_5S);
94     int32_t ret = g_sensorInterface->GetAllSensorInfo(sensorInfos);
95     if (ret != 0) {
96 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
97         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
98             HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorList", "ERROR_CODE", ret);
99 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
100         SEN_HILOGE("Get sensor list failed");
101         return ret;
102     }
103     size_t count = sensorInfos.size();
104     if (count > MAX_SENSOR_COUNT) {
105         SEN_HILOGD("SensorInfos size:%{public}zu", count);
106         count = MAX_SENSOR_COUNT;
107     }
108     for (size_t i = 0; i < count; i++) {
109         Sensor sensor;
110         sensor.SetDeviceId(sensorInfos[i].deviceSensorInfo.deviceId);
111         sensor.SetSensorId(sensorInfos[i].deviceSensorInfo.sensorId);
112         sensor.SetSensorTypeId(sensorInfos[i].deviceSensorInfo.sensorType);
113         sensor.SetLocation(sensorInfos[i].deviceSensorInfo.location);
114         sensor.SetFirmwareVersion(sensorInfos[i].firmwareVersion);
115         sensor.SetHardwareVersion(sensorInfos[i].hardwareVersion);
116         sensor.SetMaxRange(sensorInfos[i].maxRange);
117         sensor.SetSensorName(sensorInfos[i].sensorName);
118         sensor.SetVendorName(sensorInfos[i].vendorName);
119         sensor.SetResolution(sensorInfos[i].accuracy);
120         sensor.SetPower(sensorInfos[i].power);
121         sensor.SetMinSamplePeriodNs(sensorInfos[i].minDelay);
122         sensor.SetMaxSamplePeriodNs(sensorInfos[i].maxDelay);
123         if (sensorInfos[i].deviceSensorInfo.sensorType == SENSOR_TYPE_ID_HEADPOSTURE) {
124             sensor.SetFifoMaxEventCount(HEADPOSTURE_FIFO_COUNT);
125         }
126         sensorList.push_back(sensor);
127     }
128     return ERR_OK;
129 }
130 
EnableSensor(const SensorDescription & sensorDesc)131 int32_t HdiConnection::EnableSensor(const SensorDescription &sensorDesc)
132 {
133     SEN_HILOGI("In, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
134         sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
135     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
136     CHKPR(g_sensorInterface, ERR_NO_INIT);
137     SensorXcollie sensorXcollie("HdiConnection:EnableSensor", XCOLLIE_TIMEOUT_5S);
138     int32_t ret = g_sensorInterface->Enable({sensorDesc.deviceId, sensorDesc.sensorType,
139         sensorDesc.sensorId, sensorDesc.location});
140     if (ret != 0) {
141 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
142         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
143             HiSysEvent::EventType::FAULT, "PKG_NAME", "EnableSensor", "ERROR_CODE", ret);
144 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
145         SEN_HILOGE("Connect v3_0 hdi failed");
146         return ret;
147     }
148     SetSensorBasicInfoState(sensorDesc, true);
149     SEN_HILOGI("Done, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
150         sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
151     return ERR_OK;
152 }
153 
DisableSensor(const SensorDescription & sensorDesc)154 int32_t HdiConnection::DisableSensor(const SensorDescription &sensorDesc)
155 {
156     SEN_HILOGI("In, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
157         sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
158     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
159     CHKPR(g_sensorInterface, ERR_NO_INIT);
160     SensorXcollie sensorXcollie("HdiConnection:DisableSensor", XCOLLIE_TIMEOUT_5S);
161     int32_t ret = g_sensorInterface->Disable({sensorDesc.deviceId, sensorDesc.sensorType,
162         sensorDesc.sensorId, sensorDesc.location});
163     if (ret != 0) {
164 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
165         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
166             HiSysEvent::EventType::FAULT, "PKG_NAME", "DisableSensor", "ERROR_CODE", ret);
167 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
168         SEN_HILOGE("Disable is failed");
169         return ret;
170     }
171     DeleteSensorBasicInfoState(sensorDesc);
172     SEN_HILOGI("Done, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
173         sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
174     return ERR_OK;
175 }
176 
SetBatch(const SensorDescription & sensorDesc,int64_t samplingInterval,int64_t reportInterval)177 int32_t HdiConnection::SetBatch(const SensorDescription &sensorDesc, int64_t samplingInterval, int64_t reportInterval)
178 {
179     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
180     CHKPR(g_sensorInterface, ERR_NO_INIT);
181     SensorXcollie sensorXcollie("HdiConnection:SetBatch", XCOLLIE_TIMEOUT_5S);
182     int32_t ret = g_sensorInterface->SetBatch({sensorDesc.deviceId, sensorDesc.sensorType,
183         sensorDesc.sensorId, sensorDesc.location}, samplingInterval, reportInterval);
184     if (ret != 0) {
185 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
186         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
187             HiSysEvent::EventType::FAULT, "PKG_NAME", "SetBatch", "ERROR_CODE", ret);
188 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
189         SEN_HILOGE("SetBatch is failed");
190         return ret;
191     }
192     UpdateSensorBasicInfo(sensorDesc, samplingInterval, reportInterval);
193     return ERR_OK;
194 }
195 
SetMode(const SensorDescription & sensorDesc,int32_t mode)196 int32_t HdiConnection::SetMode(const SensorDescription &sensorDesc, int32_t mode)
197 {
198     CALL_LOG_ENTER;
199     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
200     CHKPR(g_sensorInterface, ERR_NO_INIT);
201     SensorXcollie sensorXcollie("HdiConnection:SetMode", XCOLLIE_TIMEOUT_5S);
202     int32_t ret = g_sensorInterface->SetMode({sensorDesc.deviceId, sensorDesc.sensorType,
203         sensorDesc.sensorId, sensorDesc.location}, mode);
204     if (ret != 0) {
205 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
206         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
207             HiSysEvent::EventType::FAULT, "PKG_NAME", "SetMode", "ERROR_CODE", ret);
208 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
209         SEN_HILOGE("SetMode is failed");
210         return ret;
211     }
212     return ERR_OK;
213 }
214 
RegisterDataReport(ReportDataCb cb,sptr<ReportDataCallback> reportDataCallback)215 int32_t HdiConnection::RegisterDataReport(ReportDataCb cb, sptr<ReportDataCallback> reportDataCallback)
216 {
217     CALL_LOG_ENTER;
218     CHKPR(reportDataCallback, ERR_NO_INIT);
219     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
220     CHKPR(g_sensorInterface, ERR_NO_INIT);
221     SensorXcollie sensorXcollie("HdiConnection:RegisterDataReport", XCOLLIE_TIMEOUT_5S);
222     int32_t ret = g_sensorInterface->RegisterAsync(0, g_eventCallback);
223     if (ret != 0) {
224 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
225         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
226             HiSysEvent::EventType::FAULT, "PKG_NAME", "RegisterDataReport", "ERROR_CODE", ret);
227 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
228         SEN_HILOGE("RegisterAsync is failed");
229         return ret;
230     }
231     reportDataCb_ = cb;
232     reportDataCallback_ = reportDataCallback;
233     return ERR_OK;
234 }
235 
DestroyHdiConnection()236 int32_t HdiConnection::DestroyHdiConnection()
237 {
238     CALL_LOG_ENTER;
239     {
240         std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
241         CHKPR(g_sensorInterface, ERR_NO_INIT);
242         int32_t ret = 0;
243         {
244             SensorXcollie unregisterAsyncXcollie("HdiConnection:UnregisterAsync", XCOLLIE_TIMEOUT_5S);
245             ret = g_sensorInterface->UnregisterAsync(DEFAULT_GROUP_ID, g_eventCallback);
246             if (ret != ERR_OK) {
247 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
248                 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
249                     HiSysEvent::EventType::FAULT, "PKG_NAME", "DestroyHdiConnection", "ERROR_CODE", ret);
250 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
251                 SEN_HILOGE("UnregisterAsync is failed");
252                 return ret;
253             }
254         }
255         SensorXcollie unRegSensorPlugCallBackXcollie("HdiConnection:UnRegSensorPlugCallBack", XCOLLIE_TIMEOUT_5S);
256         ret = g_sensorInterface->UnRegSensorPlugCallBack(g_plugCallback);
257         if (ret != ERR_OK) {
258 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
259             HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
260                 HiSysEvent::EventType::FAULT, "PKG_NAME", "UnRegSensorPlugCallback", "ERROR_CODE", ret);
261 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
262             SEN_HILOGE("UnRegSensorPlugCallback is failed");
263             return ret;
264         }
265         g_plugCallback = nullptr;
266         g_eventCallback = nullptr;
267     }
268     UnregisterHdiDeathRecipient();
269     return ERR_OK;
270 }
271 
RegSensorPlugCallback(DevicePlugCallback cb)272 int32_t HdiConnection::RegSensorPlugCallback(DevicePlugCallback cb)
273 {
274     CALL_LOG_ENTER;
275     CHKPR(cb, ERR_NO_INIT);
276     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
277     CHKPR(g_sensorInterface, ERR_NO_INIT);
278     reportPlugDataCb_ = cb;
279     SensorXcollie sensorXcollie("HdiConnection:RegSensorPlugCallback", XCOLLIE_TIMEOUT_5S);
280     int32_t ret = g_sensorInterface->RegSensorPlugCallBack(g_plugCallback);
281     if (ret != 0) {
282 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
283         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
284             HiSysEvent::EventType::FAULT, "PKG_NAME", "RegSensorPlugCallback", "ERROR_CODE", ret);
285 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
286         SEN_HILOGE("RegSensorPlugCallback is failed");
287         return ret;
288     }
289     return ERR_OK;
290 }
291 
GetSensorPlugCb()292 DevicePlugCallback HdiConnection::GetSensorPlugCb()
293 {
294     if (reportPlugDataCb_ == nullptr) {
295         SEN_HILOGE("reportPlugDataCb_ cannot be null");
296     }
297     return reportPlugDataCb_;
298 }
299 
GetReportDataCb()300 ReportDataCb HdiConnection::GetReportDataCb()
301 {
302     if (reportDataCb_ == nullptr) {
303         SEN_HILOGE("reportDataCb_ cannot be null");
304     }
305     return reportDataCb_;
306 }
307 
GetReportDataCallback()308 sptr<ReportDataCallback> HdiConnection::GetReportDataCallback()
309 {
310     if (reportDataCallback_ == nullptr) {
311         SEN_HILOGE("reportDataCallback_ cannot be null");
312     }
313     return reportDataCallback_;
314 }
315 
UpdateSensorBasicInfo(const SensorDescription & sensorDesc,int64_t samplingPeriodNs,int64_t maxReportDelayNs)316 void HdiConnection::UpdateSensorBasicInfo(const SensorDescription &sensorDesc, int64_t samplingPeriodNs,
317     int64_t maxReportDelayNs)
318 {
319     std::lock_guard<std::mutex> sensorInfoLock(g_sensorBasicInfoMutex);
320     SensorBasicInfo sensorBasicInfo;
321     sensorBasicInfo.SetSamplingPeriodNs(samplingPeriodNs);
322     sensorBasicInfo.SetMaxReportDelayNs(maxReportDelayNs);
323     auto it = g_sensorBasicInfoMap.find(sensorDesc);
324     if (it != g_sensorBasicInfoMap.end()) {
325         if (g_sensorBasicInfoMap[sensorDesc].GetSensorState()) {
326             sensorBasicInfo.SetSensorState(true);
327         }
328     }
329     g_sensorBasicInfoMap[sensorDesc] = sensorBasicInfo;
330 }
331 
SetSensorBasicInfoState(const SensorDescription & sensorDesc,bool state)332 void HdiConnection::SetSensorBasicInfoState(const SensorDescription &sensorDesc, bool state)
333 {
334     SEN_HILOGI("In, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
335         sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
336     std::lock_guard<std::mutex> sensorInfoLock(g_sensorBasicInfoMutex);
337     auto it = g_sensorBasicInfoMap.find(sensorDesc);
338     if (it == g_sensorBasicInfoMap.end()) {
339         SEN_HILOGW("Should set batch first");
340         return;
341     }
342     g_sensorBasicInfoMap[sensorDesc].SetSensorState(state);
343     SEN_HILOGI("Done, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
344         sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
345 }
346 
DeleteSensorBasicInfoState(const SensorDescription & sensorDesc)347 void HdiConnection::DeleteSensorBasicInfoState(const SensorDescription &sensorDesc)
348 {
349     SEN_HILOGI("In, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
350         sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
351     std::lock_guard<std::mutex> sensorInfoLock(g_sensorBasicInfoMutex);
352     auto it = g_sensorBasicInfoMap.find(sensorDesc);
353     if (it != g_sensorBasicInfoMap.end()) {
354         g_sensorBasicInfoMap.erase(sensorDesc);
355     }
356     SEN_HILOGI("Done,deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
357         sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
358 }
359 
RegisterHdiDeathRecipient()360 void HdiConnection::RegisterHdiDeathRecipient()
361 {
362     CALL_LOG_ENTER;
363     CHKPV(g_sensorInterface);
364     if (hdiDeathObserver_ == nullptr) {
365         hdiDeathObserver_ = new (std::nothrow) DeathRecipientTemplate(*const_cast<HdiConnection *>(this));
366         CHKPV(hdiDeathObserver_);
367     }
368     SensorXcollie sensorXcollie("HdiConnection:RegisterHdiDeathRecipient", XCOLLIE_TIMEOUT_5S);
369     OHOS::HDI::hdi_objcast<ISensorInterface>(g_sensorInterface)->AddDeathRecipient(hdiDeathObserver_);
370 }
371 
UnregisterHdiDeathRecipient()372 void HdiConnection::UnregisterHdiDeathRecipient()
373 {
374     CALL_LOG_ENTER;
375     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
376     CHKPV(g_sensorInterface);
377     CHKPV(hdiDeathObserver_);
378     SensorXcollie sensorXcollie("HdiConnection:UnregisterHdiDeathRecipient", XCOLLIE_TIMEOUT_5S);
379     OHOS::HDI::hdi_objcast<ISensorInterface>(g_sensorInterface)->RemoveDeathRecipient(hdiDeathObserver_);
380 }
381 
ProcessDeathObserver(const wptr<IRemoteObject> & object)382 void HdiConnection::ProcessDeathObserver(const wptr<IRemoteObject> &object)
383 {
384     CALL_LOG_ENTER;
385     sptr<IRemoteObject> hdiService = object.promote();
386     CHKPV(hdiService);
387     CHKPV(hdiDeathObserver_);
388     {
389         std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
390         hdiService->RemoveDeathRecipient(hdiDeathObserver_);
391         g_eventCallback = nullptr;
392     }
393     Reconnect();
394 }
395 
ReEnableSensor()396 void HdiConnection::ReEnableSensor()
397 {
398     std::lock_guard<std::mutex> sensorInfoLock(g_sensorBasicInfoMutex);
399     for (const auto &sensorInfo: g_sensorBasicInfoMap) {
400         SensorBasicInfo info = sensorInfo.second;
401         if (!info.GetSensorState()) {
402             SEN_HILOGE("deviceIndex:%{public}d, sensortype:%{public}d, sensorId:%{public}d don't need enable sensor",
403                 sensorInfo.first.deviceId, sensorInfo.first.sensorType, sensorInfo.first.sensorId);
404             continue;
405         }
406         int32_t ret = 0;
407         {
408             SensorXcollie setBatchXcollie("HdiConnection:Reconnect:SetBatch", XCOLLIE_TIMEOUT_5S);
409             ret = g_sensorInterface->SetBatch({sensorInfo.first.deviceId, sensorInfo.first.sensorType,
410                 sensorInfo.first.sensorId, sensorInfo.first.location},
411                 info.GetSamplingPeriodNs(), info.GetMaxReportDelayNs());
412             if (ret != ERR_OK) {
413                 SEN_HILOGE("deviceIndex:%{public}d, sensortype:%{public}d, sensorId:%{public}d set batch fail,"
414                     "err:%{public}d", sensorInfo.first.deviceId, sensorInfo.first.sensorType, sensorInfo.first.sensorId,
415                     ret);
416                 continue;
417             }
418         }
419         SensorXcollie enableXcollie("HdiConnection:Reconnect:ReEnable", XCOLLIE_TIMEOUT_5S);
420         ret = g_sensorInterface->Enable({sensorInfo.first.deviceId, sensorInfo.first.sensorType,
421             sensorInfo.first.sensorId, sensorInfo.first.location});
422         if (ret != ERR_OK) {
423             SEN_HILOGE("Enable fail, deviceIndex:%{public}d, sensortype:%{public}d, sensorId:%{public}d,"
424                 "err:%{public}d", sensorInfo.first.deviceId, sensorInfo.first.sensorType, sensorInfo.first.sensorId,
425                 ret);
426         }
427     }
428 }
429 
Reconnect()430 void HdiConnection::Reconnect()
431 {
432     CALL_LOG_ENTER;
433     int32_t ret = ConnectHdi();
434     if (ret != 0) {
435         SEN_HILOGE("Failed to get an instance of hdi service");
436         return;
437     }
438     {
439         std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
440         {
441             SensorXcollie registerXcollie("HdiConnection:Reconnect:RegisterAsync", XCOLLIE_TIMEOUT_5S);
442             ret = g_sensorInterface->RegisterAsync(DEFAULT_GROUP_ID, g_eventCallback);
443             if (ret != ERR_OK) {
444                 SEN_HILOGE("RegisterAsync callback fail");
445                 return;
446             }
447         }
448         SensorXcollie regSensorPlugCallBackXcollie("HdiConnection:Reconnect:RegSensorPlugCallBack", XCOLLIE_TIMEOUT_5S);
449         ret = g_sensorInterface->RegSensorPlugCallBack(g_plugCallback);
450         if (ret != ERR_OK) {
451             SEN_HILOGE("RegisterAsync plug callback fail");
452             return;
453         }
454     }
455     std::vector<Sensor> sensorList;
456     ret = GetSensorList(sensorList);
457     if (ret != ERR_OK) {
458         SEN_HILOGE("Get sensor list fail");
459         return;
460     }
461     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
462     ReEnableSensor();
463 }
464 
GetSensorListByDevice(int32_t deviceId,std::vector<Sensor> & singleDevSensors)465 int32_t HdiConnection::GetSensorListByDevice(int32_t deviceId, std::vector<Sensor> &singleDevSensors)
466 {
467     CALL_LOG_ENTER;
468     std::lock_guard<std::mutex> sensorInterfaceLock(g_sensorInterfaceMutex);
469     CHKPR(g_sensorInterface, ERR_NO_INIT);
470     std::vector<HdfSensorInformation> sensorInfos;
471     SensorXcollie sensorXcollie("HdiConnection:GetSensorListByDevice", XCOLLIE_TIMEOUT_5S);
472     int32_t ret = g_sensorInterface->GetDeviceSensorInfo(deviceId, sensorInfos);
473     if (ret != 0) {
474 #ifdef HIVIEWDFX_HISYSEVENT_ENABLE
475         HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::SENSOR, "HDF_SERVICE_EXCEPTION",
476             HiSysEvent::EventType::FAULT, "PKG_NAME", "GetSensorListByDevice", "ERROR_CODE", ret);
477 #endif // HIVIEWDFX_HISYSEVENT_ENABLE
478         SEN_HILOGE("Get sensor list by device failed");
479         return ret;
480     }
481     size_t count = sensorInfos.size();
482     if (count > MAX_SENSOR_COUNT) {
483         SEN_HILOGD("SensorInfos size:%{public}zu", count);
484         count = MAX_SENSOR_COUNT;
485     }
486     for (size_t i = 0; i < count; i++) {
487         Sensor sensor;
488         sensor.SetDeviceId(sensorInfos[i].deviceSensorInfo.deviceId);
489         sensor.SetSensorId(sensorInfos[i].deviceSensorInfo.sensorId);
490         sensor.SetSensorTypeId(sensorInfos[i].deviceSensorInfo.sensorType);
491         sensor.SetLocation(sensorInfos[i].deviceSensorInfo.location);
492         sensor.SetFirmwareVersion(sensorInfos[i].firmwareVersion);
493         sensor.SetHardwareVersion(sensorInfos[i].hardwareVersion);
494         sensor.SetMaxRange(sensorInfos[i].maxRange);
495         sensor.SetSensorName(sensorInfos[i].sensorName);
496         sensor.SetVendorName(sensorInfos[i].vendorName);
497         sensor.SetResolution(sensorInfos[i].accuracy);
498         sensor.SetPower(sensorInfos[i].power);
499         sensor.SetMinSamplePeriodNs(sensorInfos[i].minDelay);
500         sensor.SetMaxSamplePeriodNs(sensorInfos[i].maxDelay);
501         if (sensorInfos[i].deviceSensorInfo.sensorType == SENSOR_TYPE_ID_HEADPOSTURE) {
502             sensor.SetFifoMaxEventCount(HEADPOSTURE_FIFO_COUNT);
503         }
504         singleDevSensors.push_back(sensor);
505     }
506     return ERR_OK;
507 }
508 } // namespace Sensors
509 } // namespace OHOS
510