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 "sensor_hdi_connection.h"
16
17 #ifdef BUILD_VARIANT_ENG
18 #include "compatible_connection.h"
19 #endif // BUILD_VARIANT_ENG
20
21 #include "hdi_connection.h"
22 #ifdef HIVIEWDFX_HITRACE_ENABLE
23 #include "hitrace_meter.h"
24 #endif // HIVIEWDFX_HITRACE_ENABLE
25 #include "sensor_errors.h"
26
27 #undef LOG_TAG
28 #define LOG_TAG "SensorHdiConnection"
29 std::mutex OHOS::Sensors::ISensorHdiConnection::dataMutex_;
30 std::condition_variable OHOS::Sensors::ISensorHdiConnection::dataCondition_;
31 std::atomic<bool> OHOS::Sensors::ISensorHdiConnection::dataReady_ = false;
32
33 namespace OHOS {
34 namespace Sensors {
35 using namespace OHOS::HiviewDFX;
36 namespace {
37 #ifdef BUILD_VARIANT_ENG
38 constexpr float MAX_RANGE = 9999.0;
39 constexpr float POWER = 20.0;
40 constexpr float RESOLITION = 0.000001;
41 constexpr float MIN_SAMPLE_PERIOD_NS = 100000000;
42 constexpr float MAX_SAMPLE_PERIOD_NS = 1000000000;
43 const std::string VERSION_NAME = "1.0.1";
44 std::unordered_set<int32_t> g_supportMockSensors = {
45 SENSOR_TYPE_ID_COLOR,
46 SENSOR_TYPE_ID_SAR,
47 SENSOR_TYPE_ID_HEADPOSTURE,
48 SENSOR_TYPE_ID_PROXIMITY1
49 };
50 constexpr int32_t IS_LOCAL_DEVICE = 1;
51 constexpr int32_t DEFAULT_SENSORID = 0;
52 constexpr int32_t DEFAULT_LOCATION = 1;
53 static int32_t localDeviceId_ = -1;
54 #endif // BUILD_VARIANT_ENG
55 constexpr int32_t HDI_DISABLE_SENSOR_TIMEOUT = -23;
56 } // namespace
57
ConnectHdi()58 int32_t SensorHdiConnection::ConnectHdi()
59 {
60 iSensorHdiConnection_ = std::make_unique<HdiConnection>();
61 int32_t ret = ConnectHdiService();
62 if (ret != ERR_OK) {
63 SEN_HILOGE("Connect hdi service failed, try to connect compatible connection, ret:%{public}d", ret);
64 #ifdef BUILD_VARIANT_ENG
65 iSensorHdiConnection_ = std::make_unique<CompatibleConnection>();
66 ret = ConnectHdiService();
67 if (ret != ERR_OK) {
68 SEN_HILOGE("Connect compatible connection failed, ret:%{public}d", ret);
69 return ret;
70 }
71 hdiConnectionStatus_ = false;
72 } else {
73 hdiConnectionStatus_ = true;
74 }
75 if (hdiConnectionStatus_ && !FindAllInSensorSet(g_supportMockSensors)) {
76 SEN_HILOGD("SensorList not contain all mock sensors, connect mock sensors compatible connection");
77 ret = ConnectCompatibleHdi();
78 if (ret != ERR_OK) {
79 SEN_HILOGE("Connect mock sensors compatible connection failed, ret:%{public}d", ret);
80 }
81 #endif // BUILD_VARIANT_ENG
82 return ret;
83 }
84 return ERR_OK;
85 }
86
ConnectHdiService()87 int32_t SensorHdiConnection::ConnectHdiService()
88 {
89 int32_t ret = iSensorHdiConnection_->ConnectHdi();
90 if (ret != ERR_OK) {
91 SEN_HILOGE("Connect hdi service failed");
92 return CONNECT_SENSOR_HDF_ERR;
93 }
94 std::lock_guard<std::mutex> sensorLock(sensorMutex_);
95 ret = iSensorHdiConnection_->GetSensorList(sensorList_);
96 if (ret != ERR_OK) {
97 SEN_HILOGE("Get sensor list failed");
98 return GET_SENSOR_LIST_ERR;
99 }
100 for (const auto &sensor : sensorList_) {
101 sensorSet_.insert(sensor.GetSensorTypeId());
102 }
103 return ERR_OK;
104 }
105
106 #ifdef BUILD_VARIANT_ENG
ConnectCompatibleHdi()107 int32_t SensorHdiConnection::ConnectCompatibleHdi()
108 {
109 if (iSensorCompatibleHdiConnection_ == nullptr) {
110 iSensorCompatibleHdiConnection_ = std::make_unique<CompatibleConnection>();
111 }
112 int32_t ret = iSensorCompatibleHdiConnection_->ConnectHdi();
113 if (ret != ERR_OK) {
114 SEN_HILOGE("Connect hdi compatible service failed");
115 return CONNECT_SENSOR_HDF_ERR;
116 }
117 return ERR_OK;
118 }
119
FindAllInSensorSet(const std::unordered_set<int32_t> & sensors)120 bool SensorHdiConnection::FindAllInSensorSet(const std::unordered_set<int32_t> &sensors)
121 {
122 int32_t count = 0;
123 std::lock_guard<std::mutex> sensorLock(sensorMutex_);
124 for (const auto &sensorType : sensors) {
125 if (sensorSet_.find(sensorType) == sensorSet_.end()) {
126 mockSet_.insert(sensorType);
127 count++;
128 }
129 }
130 return count == 0 ? true : false;
131 }
132
FindOneInMockSet(int32_t sensorType)133 bool SensorHdiConnection::FindOneInMockSet(int32_t sensorType)
134 {
135 std::lock_guard<std::mutex> sensorLock(sensorMutex_);
136 return mockSet_.find(sensorType) != mockSet_.end();
137 }
138
GenerateColorSensor()139 Sensor SensorHdiConnection::GenerateColorSensor()
140 {
141 Sensor sensorColor;
142 sensorColor.SetSensorId(DEFAULT_SENSORID);
143 sensorColor.SetSensorTypeId(SENSOR_TYPE_ID_COLOR);
144 sensorColor.SetDeviceId(localDeviceId_);
145 sensorColor.SetLocation(DEFAULT_LOCATION);
146 sensorColor.SetFirmwareVersion(VERSION_NAME);
147 sensorColor.SetHardwareVersion(VERSION_NAME);
148 sensorColor.SetMaxRange(MAX_RANGE);
149 sensorColor.SetSensorName("sensor_color");
150 sensorColor.SetVendorName("default_color");
151 sensorColor.SetResolution(RESOLITION);
152 sensorColor.SetPower(POWER);
153 sensorColor.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS);
154 sensorColor.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS);
155 return sensorColor;
156 }
157
GenerateSarSensor()158 Sensor SensorHdiConnection::GenerateSarSensor()
159 {
160 Sensor sensorSar;
161 sensorSar.SetSensorId(DEFAULT_SENSORID);
162 sensorSar.SetSensorTypeId(SENSOR_TYPE_ID_SAR);
163 sensorSar.SetDeviceId(localDeviceId_);
164 sensorSar.SetLocation(DEFAULT_LOCATION);
165 sensorSar.SetFirmwareVersion(VERSION_NAME);
166 sensorSar.SetHardwareVersion(VERSION_NAME);
167 sensorSar.SetMaxRange(MAX_RANGE);
168 sensorSar.SetSensorName("sensor_sar");
169 sensorSar.SetVendorName("default_sar");
170 sensorSar.SetResolution(RESOLITION);
171 sensorSar.SetPower(POWER);
172 sensorSar.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS);
173 sensorSar.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS);
174 return sensorSar;
175 }
176
GenerateHeadPostureSensor()177 Sensor SensorHdiConnection::GenerateHeadPostureSensor()
178 {
179 Sensor sensorHeadPosture;
180 sensorHeadPosture.SetSensorId(DEFAULT_SENSORID);
181 sensorHeadPosture.SetSensorTypeId(SENSOR_TYPE_ID_HEADPOSTURE);
182 sensorHeadPosture.SetDeviceId(localDeviceId_);
183 sensorHeadPosture.SetLocation(DEFAULT_LOCATION);
184 sensorHeadPosture.SetFirmwareVersion(VERSION_NAME);
185 sensorHeadPosture.SetHardwareVersion(VERSION_NAME);
186 sensorHeadPosture.SetMaxRange(MAX_RANGE);
187 sensorHeadPosture.SetSensorName("sensor_headPosture");
188 sensorHeadPosture.SetVendorName("default_headPosture");
189 sensorHeadPosture.SetResolution(RESOLITION);
190 sensorHeadPosture.SetPower(POWER);
191 sensorHeadPosture.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS);
192 sensorHeadPosture.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS);
193 return sensorHeadPosture;
194 }
195
GenerateProximitySensor()196 Sensor SensorHdiConnection::GenerateProximitySensor()
197 {
198 Sensor sensorProximity;
199 sensorProximity.SetSensorId(DEFAULT_SENSORID);
200 sensorProximity.SetSensorTypeId(SENSOR_TYPE_ID_PROXIMITY1);
201 sensorProximity.SetDeviceId(localDeviceId_);
202 sensorProximity.SetLocation(DEFAULT_LOCATION);
203 sensorProximity.SetFirmwareVersion(VERSION_NAME);
204 sensorProximity.SetHardwareVersion(VERSION_NAME);
205 sensorProximity.SetMaxRange(MAX_RANGE);
206 sensorProximity.SetSensorName("sensor_proximity1");
207 sensorProximity.SetVendorName("default_proximity1");
208 sensorProximity.SetResolution(RESOLITION);
209 sensorProximity.SetPower(POWER);
210 sensorProximity.SetMinSamplePeriodNs(MIN_SAMPLE_PERIOD_NS);
211 sensorProximity.SetMaxSamplePeriodNs(MAX_SAMPLE_PERIOD_NS);
212 return sensorProximity;
213 }
214 #endif // BUILD_VARIANT_ENG
215
GetSensorList(std::vector<Sensor> & sensorList)216 int32_t SensorHdiConnection::GetSensorList(std::vector<Sensor> &sensorList)
217 {
218 CHKPR(iSensorHdiConnection_, GET_SENSOR_LIST_ERR);
219 std::lock_guard<std::mutex> sensorLock(sensorMutex_);
220 if (sensorList_.empty()) {
221 if (iSensorHdiConnection_->GetSensorList(sensorList_) != ERR_OK) {
222 SEN_HILOGW("Get sensor list failed");
223 }
224 }
225 sensorList.assign(sensorList_.begin(), sensorList_.end());
226 #ifdef BUILD_VARIANT_ENG
227 if (!hdiConnectionStatus_) {
228 return ERR_OK;
229 }
230 for (const auto& sensor : sensorList) {
231 if (sensor.GetLocation() == IS_LOCAL_DEVICE) {
232 localDeviceId_ = sensor.GetDeviceId();
233 }
234 }
235 for (const auto &sensorType : mockSet_) {
236 switch (sensorType) {
237 case SENSOR_TYPE_ID_COLOR:
238 sensorList.push_back(GenerateColorSensor());
239 break;
240 case SENSOR_TYPE_ID_SAR:
241 sensorList.push_back(GenerateSarSensor());
242 break;
243 case SENSOR_TYPE_ID_HEADPOSTURE:
244 sensorList.push_back(GenerateHeadPostureSensor());
245 break;
246 case SENSOR_TYPE_ID_PROXIMITY1:
247 sensorList.push_back(GenerateProximitySensor());
248 break;
249 default:
250 break;
251 }
252 }
253 #endif // BUILD_VARIANT_ENG
254 return ERR_OK;
255 }
256
EnableSensor(const SensorDescription & sensorDesc)257 int32_t SensorHdiConnection::EnableSensor(const SensorDescription &sensorDesc)
258 {
259 #ifdef HIVIEWDFX_HITRACE_ENABLE
260 StartTrace(HITRACE_TAG_SENSORS, "EnableSensor");
261 #endif // HIVIEWDFX_HITRACE_ENABLE
262 int32_t ret = ENABLE_SENSOR_ERR;
263 #ifdef BUILD_VARIANT_ENG
264 if (FindOneInMockSet(sensorDesc.sensorType)) {
265 CHKPR(iSensorCompatibleHdiConnection_, ENABLE_SENSOR_ERR);
266 ret = iSensorCompatibleHdiConnection_->EnableSensor(sensorDesc);
267 #ifdef HIVIEWDFX_HITRACE_ENABLE
268 FinishTrace(HITRACE_TAG_SENSORS);
269 #endif // HIVIEWDFX_HITRACE_ENABLE
270 if (ret != ERR_OK) {
271 SEN_HILOGE(
272 "Enable failed in compatible, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
273 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
274 return ENABLE_SENSOR_ERR;
275 }
276 return ret;
277 }
278 #endif // BUILD_VARIANT_ENG
279 CHKPR(iSensorHdiConnection_, ENABLE_SENSOR_ERR);
280 ret = iSensorHdiConnection_->EnableSensor(sensorDesc);
281 #ifdef HIVIEWDFX_HITRACE_ENABLE
282 FinishTrace(HITRACE_TAG_SENSORS);
283 #endif // HIVIEWDFX_HITRACE_ENABLE
284 if (ret != ERR_OK) {
285 SEN_HILOGI("Enable failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
286 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
287 return ENABLE_SENSOR_ERR;
288 }
289 return ret;
290 };
291
DisableSensor(const SensorDescription & sensorDesc)292 int32_t SensorHdiConnection::DisableSensor(const SensorDescription &sensorDesc)
293 {
294 #ifdef HIVIEWDFX_HITRACE_ENABLE
295 StartTrace(HITRACE_TAG_SENSORS, "DisableSensor");
296 #endif // HIVIEWDFX_HITRACE_ENABLE
297 int32_t ret = DISABLE_SENSOR_ERR;
298 #ifdef BUILD_VARIANT_ENG
299 if (FindOneInMockSet(sensorDesc.sensorType)) {
300 CHKPR(iSensorCompatibleHdiConnection_, DISABLE_SENSOR_ERR);
301 ret = iSensorCompatibleHdiConnection_->DisableSensor(sensorDesc);
302 #ifdef HIVIEWDFX_HITRACE_ENABLE
303 FinishTrace(HITRACE_TAG_SENSORS);
304 #endif // HIVIEWDFX_HITRACE_ENABLE
305 if (ret != ERR_OK) {
306 SEN_HILOGE(
307 "Disable failed in compatible, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
308 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
309 return DISABLE_SENSOR_ERR;
310 }
311 return ret;
312 }
313 #endif // BUILD_VARIANT_ENG
314 CHKPR(iSensorHdiConnection_, DISABLE_SENSOR_ERR);
315 ret = iSensorHdiConnection_->DisableSensor(sensorDesc);
316 #ifdef HIVIEWDFX_HITRACE_ENABLE
317 FinishTrace(HITRACE_TAG_SENSORS);
318 #endif // HIVIEWDFX_HITRACE_ENABLE
319 if ((ret != ERR_OK) && (ret != HDI_DISABLE_SENSOR_TIMEOUT)) {
320 SEN_HILOGI("Disable sensor failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
321 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
322 return DISABLE_SENSOR_ERR;
323 }
324 if (ret == HDI_DISABLE_SENSOR_TIMEOUT) {
325 SEN_HILOGI("Hdi DisableSensor timeout, ret:%{public}d", ret);
326 }
327 return ERR_OK;
328 }
329
SetBatch(const SensorDescription & sensorDesc,int64_t samplingInterval,int64_t reportInterval)330 int32_t SensorHdiConnection::SetBatch(const SensorDescription &sensorDesc, int64_t samplingInterval,
331 int64_t reportInterval)
332 {
333 #ifdef HIVIEWDFX_HITRACE_ENABLE
334 StartTrace(HITRACE_TAG_SENSORS, "SetBatch");
335 #endif // HIVIEWDFX_HITRACE_ENABLE
336 int32_t ret = SET_SENSOR_CONFIG_ERR;
337 #ifdef BUILD_VARIANT_ENG
338 if (FindOneInMockSet(sensorDesc.sensorType)) {
339 CHKPR(iSensorCompatibleHdiConnection_, SET_SENSOR_CONFIG_ERR);
340 ret = iSensorCompatibleHdiConnection_->SetBatch(sensorDesc, samplingInterval, reportInterval);
341 #ifdef HIVIEWDFX_HITRACE_ENABLE
342 FinishTrace(HITRACE_TAG_SENSORS);
343 #endif // HIVIEWDFX_HITRACE_ENABLE
344 if (ret != ERR_OK) {
345 SEN_HILOGI(
346 "Set batch failed in compatible, deviceIndex:%{public}d, sensortype:%{public}d, sensorId:%{public}d",
347 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
348 return SET_SENSOR_CONFIG_ERR;
349 }
350 return ret;
351 }
352 #endif // BUILD_VARIANT_ENG
353 CHKPR(iSensorHdiConnection_, SET_SENSOR_CONFIG_ERR);
354 ret = iSensorHdiConnection_->SetBatch(sensorDesc, samplingInterval, reportInterval);
355 #ifdef HIVIEWDFX_HITRACE_ENABLE
356 FinishTrace(HITRACE_TAG_SENSORS);
357 #endif // HIVIEWDFX_HITRACE_ENABLE
358 if (ret != ERR_OK) {
359 SEN_HILOGI("Set batch failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
360 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
361 return SET_SENSOR_CONFIG_ERR;
362 }
363 return ret;
364 }
365
SetMode(const SensorDescription & sensorDesc,int32_t mode)366 int32_t SensorHdiConnection::SetMode(const SensorDescription &sensorDesc, int32_t mode)
367 {
368 #ifdef HIVIEWDFX_HITRACE_ENABLE
369 StartTrace(HITRACE_TAG_SENSORS, "SetMode");
370 #endif // HIVIEWDFX_HITRACE_ENABLE
371 int32_t ret = SET_SENSOR_MODE_ERR;
372 #ifdef BUILD_VARIANT_ENG
373 if (FindOneInMockSet(sensorDesc.sensorType)) {
374 CHKPR(iSensorCompatibleHdiConnection_, SET_SENSOR_MODE_ERR);
375 ret = iSensorCompatibleHdiConnection_->SetMode(sensorDesc, mode);
376 #ifdef HIVIEWDFX_HITRACE_ENABLE
377 FinishTrace(HITRACE_TAG_SENSORS);
378 #endif // HIVIEWDFX_HITRACE_ENABLE
379 if (ret != ERR_OK) {
380 SEN_HILOGI("Set mode failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
381 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
382 return SET_SENSOR_MODE_ERR;
383 }
384 return ret;
385 }
386 #endif // BUILD_VARIANT_ENG
387 CHKPR(iSensorHdiConnection_, SET_SENSOR_MODE_ERR);
388 ret = iSensorHdiConnection_->SetMode(sensorDesc, mode);
389 #ifdef HIVIEWDFX_HITRACE_ENABLE
390 FinishTrace(HITRACE_TAG_SENSORS);
391 #endif // HIVIEWDFX_HITRACE_ENABLE
392 if (ret != ERR_OK) {
393 SEN_HILOGI("Set mode failed, deviceIndex:%{public}d, sensortypeId:%{public}d, sensorId:%{public}d",
394 sensorDesc.deviceId, sensorDesc.sensorType, sensorDesc.sensorId);
395 return SET_SENSOR_MODE_ERR;
396 }
397 return ret;
398 }
399
RegisterDataReport(ReportDataCb cb,sptr<ReportDataCallback> reportDataCallback)400 int32_t SensorHdiConnection::RegisterDataReport(ReportDataCb cb, sptr<ReportDataCallback> reportDataCallback)
401 {
402 #ifdef HIVIEWDFX_HITRACE_ENABLE
403 StartTrace(HITRACE_TAG_SENSORS, "RegisterDataReport");
404 #endif // HIVIEWDFX_HITRACE_ENABLE
405 CHKPR(iSensorHdiConnection_, REGIST_CALLBACK_ERR);
406 int32_t ret = iSensorHdiConnection_->RegisterDataReport(cb, reportDataCallback);
407 if (ret != ERR_OK) {
408 SEN_HILOGE("Registe dataReport failed");
409 return REGIST_CALLBACK_ERR;
410 }
411 #ifdef BUILD_VARIANT_ENG
412 if (iSensorCompatibleHdiConnection_ != nullptr) {
413 ret = iSensorCompatibleHdiConnection_->RegisterDataReport(cb, reportDataCallback);
414 if (ret != ERR_OK) {
415 SEN_HILOGE("Registe dataReport failed in compatible");
416 return REGIST_CALLBACK_ERR;
417 }
418 }
419 #endif // BUILD_VARIANT_ENG
420 #ifdef HIVIEWDFX_HITRACE_ENABLE
421 FinishTrace(HITRACE_TAG_SENSORS);
422 #endif // HIVIEWDFX_HITRACE_ENABLE
423 return ret;
424 }
425
DestroyHdiConnection()426 int32_t SensorHdiConnection::DestroyHdiConnection()
427 {
428 CHKPR(iSensorHdiConnection_, DEVICE_ERR);
429 int32_t ret = iSensorHdiConnection_->DestroyHdiConnection();
430 if (ret != ERR_OK) {
431 SEN_HILOGE("Destroy hdi connection failed");
432 return DEVICE_ERR;
433 }
434 #ifdef BUILD_VARIANT_ENG
435 if (iSensorCompatibleHdiConnection_ != nullptr) {
436 ret = iSensorCompatibleHdiConnection_->DestroyHdiConnection();
437 if (ret != ERR_OK) {
438 SEN_HILOGE("Destroy hdi connection failed in compatible");
439 }
440 return DEVICE_ERR;
441 }
442 #endif // BUILD_VARIANT_ENG
443 return ret;
444 }
445
UpdateSensorList(std::vector<Sensor> & singleDevSensors)446 void SensorHdiConnection::UpdateSensorList(std::vector<Sensor> &singleDevSensors)
447 {
448 CALL_LOG_ENTER;
449 for (const auto& newSensor : singleDevSensors) {
450 bool found = false;
451 for (const auto& oldSensor : sensorList_) {
452 if (oldSensor.GetDeviceId() == newSensor.GetDeviceId() &&
453 oldSensor.GetSensorId() == newSensor.GetSensorId() &&
454 oldSensor.GetSensorTypeId() == newSensor.GetSensorTypeId()) {
455 found = true;
456 break;
457 }
458 }
459 if (!found) {
460 SEN_HILOGD("Sensor not found in sensorList_");
461 sensorList_.push_back(newSensor);
462 }
463 }
464 return;
465 }
466
GetSensorListByDevice(int32_t deviceId,std::vector<Sensor> & singleDevSensors)467 int32_t SensorHdiConnection::GetSensorListByDevice(int32_t deviceId, std::vector<Sensor> &singleDevSensors)
468 {
469 CALL_LOG_ENTER;
470 CHKPR(iSensorHdiConnection_, GET_SENSOR_LIST_ERR);
471 std::lock_guard<std::mutex> sensorLock(sensorMutex_);
472 if (iSensorHdiConnection_->GetSensorListByDevice(deviceId, singleDevSensors) != ERR_OK) {
473 SEN_HILOGW("Get sensor list by device failed");
474 }
475 if (singleDevSensors.empty()) {
476 SEN_HILOGW("Get sensor list is empty");
477 return ERR_OK;
478 }
479 UpdateSensorList(singleDevSensors);
480 #ifdef BUILD_VARIANT_ENG
481 if (singleDevSensors[0].GetLocation() == IS_LOCAL_DEVICE) {
482 if (!hdiConnectionStatus_) {
483 return ERR_OK;
484 }
485 localDeviceId_ = singleDevSensors[0].GetDeviceId();
486 for (const auto &sensorType : mockSet_) {
487 switch (sensorType) {
488 case SENSOR_TYPE_ID_COLOR:
489 singleDevSensors.push_back(GenerateColorSensor());
490 break;
491 case SENSOR_TYPE_ID_SAR:
492 singleDevSensors.push_back(GenerateSarSensor());
493 break;
494 case SENSOR_TYPE_ID_HEADPOSTURE:
495 singleDevSensors.push_back(GenerateHeadPostureSensor());
496 break;
497 case SENSOR_TYPE_ID_PROXIMITY1:
498 singleDevSensors.push_back(GenerateProximitySensor());
499 break;
500 default:
501 break;
502 }
503 }
504 }
505 #endif // BUILD_VARIANT_ENG
506 return ERR_OK;
507 }
508
RegSensorPlugCallback(DevicePlugCallback cb)509 int32_t SensorHdiConnection::RegSensorPlugCallback(DevicePlugCallback cb)
510 {
511 #ifdef HIVIEWDFX_HITRACE_ENABLE
512 StartTrace(HITRACE_TAG_SENSORS, "RegSensorPlugCallback");
513 #endif // HIVIEWDFX_HITRACE_ENABLE
514 CHKPR(iSensorHdiConnection_, REGIST_CALLBACK_ERR);
515 int32_t ret = iSensorHdiConnection_->RegSensorPlugCallback(cb);
516 if (ret != ERR_OK) {
517 SEN_HILOGE("Registe sensor plug callback failed");
518 return REGIST_CALLBACK_ERR;
519 }
520 #ifdef BUILD_VARIANT_ENG
521 if (iSensorCompatibleHdiConnection_ != nullptr) {
522 ret = iSensorCompatibleHdiConnection_->RegSensorPlugCallback(cb);
523 if (ret != ERR_OK) {
524 SEN_HILOGE("Registe sensor plug callback failed in compatible");
525 return REGIST_CALLBACK_ERR;
526 }
527 }
528 #endif // BUILD_VARIANT_ENG
529 #ifdef HIVIEWDFX_HITRACE_ENABLE
530 FinishTrace(HITRACE_TAG_SENSORS);
531 #endif // HIVIEWDFX_HITRACE_ENABLE
532 return ret;
533 }
534
GetSensorPlugCb()535 DevicePlugCallback SensorHdiConnection::GetSensorPlugCb()
536 {
537 return NULL;
538 }
539
PlugEraseSensorData(const SensorPlugInfo & info)540 bool SensorHdiConnection::PlugEraseSensorData(const SensorPlugInfo &info)
541 {
542 CALL_LOG_ENTER;
543 std::lock_guard<std::mutex> sensorLock(sensorMutex_);
544 if (sensorList_.empty()) {
545 SEN_HILOGE("sensorList_ cannot be empty");
546 return false;
547 }
548 auto it = std::find_if(sensorList_.begin(), sensorList_.end(), [&](const Sensor& sensor) {
549 return sensor.GetDeviceId() == info.deviceSensorInfo.deviceId &&
550 sensor.GetSensorTypeId() == info.deviceSensorInfo.sensorType &&
551 sensor.GetSensorId() == info.deviceSensorInfo.sensorId;
552 });
553 if (it != sensorList_.end()) {
554 sensorList_.erase(it);
555 return true;
556 }
557 SEN_HILOGD("sensorList_ cannot find the sensor");
558 return true;
559 }
560 } // namespace Sensors
561 } // namespace OHOS
562