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_uhdf_log.h"
17 #include "sensor_clients_manager.h"
18 #include <cinttypes>
19 #include <iproxy_broker.h>
20
21 #define HDF_LOG_TAG uhdf_sensor_clients_manager
22
23 namespace OHOS {
24 namespace HDI {
25 namespace Sensor {
26 namespace V2_1 {
27
28 namespace {
29 const std::vector<int32_t> continuesSensor = {HDF_SENSOR_TYPE_ACCELEROMETER, HDF_SENSOR_TYPE_GYROSCOPE,
30 HDF_SENSOR_TYPE_MAGNETIC_FIELD, HDF_SENSOR_TYPE_SAR,
31 HDF_SENSOR_TYPE_ORIENTATION, HDF_SENSOR_TYPE_GRAVITY,
32 HDF_SENSOR_TYPE_LINEAR_ACCELERATION, HDF_SENSOR_TYPE_ROTATION_VECTOR,
33 HDF_SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED,
34 HDF_SENSOR_TYPE_GAME_ROTATION_VECTOR,
35 HDF_SENSOR_TYPE_GYROSCOPE_UNCALIBRATED, HDF_SENSOR_TYPE_DROP_DETECT,
36 HDF_SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR,
37 HDF_SENSOR_TYPE_ACCELEROMETER_UNCALIBRATED,
38 HDF_SENSOR_TYPE_BAROMETER};
39 constexpr int64_t ERROR_INTERVAL = 0;
40 constexpr int64_t STOP_INTERVAL = 0;
41 constexpr int32_t INIT_CUR_COUNT = 0;
42 constexpr int32_t ZERO_PRINT_TIME = 0;
43 constexpr int32_t MAX_PRINT_TIME = 10;
44 constexpr int64_t INIT_REPORT_COUNT = 1;
45 constexpr int REPORT_INTERVAL = 0;
46 constexpr int ERROR_RETURN_VALUE = 32;
47 }
48
49 std::mutex SensorClientsManager::instanceMutex_;
50
SensorClientsManager()51 SensorClientsManager::SensorClientsManager()
52 {
53 }
54
~SensorClientsManager()55 SensorClientsManager::~SensorClientsManager()
56 {
57 clients_.clear();
58 sensorUsed_.clear();
59 sensorConfig_.clear();
60 sdcSensorConfig_.clear();
61 }
62
CopySensorInfo(std::vector<HdfSensorInformation> & info,bool cFlag)63 void SensorClientsManager::CopySensorInfo(std::vector<HdfSensorInformation> &info, bool cFlag)
64 {
65 std::unique_lock<std::mutex> lock(sensorInfoMutex_);
66 if (!cFlag) {
67 info = sensorInfo_;
68 return;
69 }
70 sensorInfo_ = info;
71 return;
72 }
73
GetEventData(struct SensorsDataPack & dataPack)74 void SensorClientsManager::GetEventData(struct SensorsDataPack &dataPack)
75 {
76 std::unique_lock<std::mutex> lock(sensorsDataPackMutex_);
77 dataPack = listDump_;
78 return;
79 }
80
CopyEventData(const struct HdfSensorEvents event)81 void SensorClientsManager::CopyEventData(const struct HdfSensorEvents event)
82 {
83 std::unique_lock<std::mutex> lock(sensorsDataPackMutex_);
84 if (event.data.empty()) {
85 HDF_LOGE("%{public}s: event data is empty!", __func__);
86 return;
87 }
88
89 if (listDump_.count == MAX_DUMP_DATA_SIZE) {
90 listDump_.listDumpArray[listDump_.pos++] = event;
91 if (listDump_.pos == MAX_DUMP_DATA_SIZE) {
92 listDump_.pos = 0;
93 }
94 } else {
95 listDump_.listDumpArray[listDump_.count] = event;
96 listDump_.count++;
97 }
98 return;
99 }
100
GetServiceId(int groupId,const sptr<V2_0::ISensorCallback> & callbackObj)101 int SensorClientsManager::GetServiceId(int groupId, const sptr<V2_0::ISensorCallback> &callbackObj)
102 {
103 SENSOR_TRACE_PID;
104 std::unique_lock<std::mutex> lock(clientsMutex_);
105 for (auto &iter : clients_[groupId]) {
106 if (iter.second.GetReportDataCb() == callbackObj) {
107 return iter.first;
108 }
109 }
110 return HDF_FAILURE;
111 }
112
ReportDataCbRegister(int groupId,int serviceId,const sptr<V2_0::ISensorCallback> & callbackObj)113 void SensorClientsManager::ReportDataCbRegister(int groupId, int serviceId,
114 const sptr<V2_0::ISensorCallback> &callbackObj)
115 {
116 SENSOR_TRACE_PID;
117 std::unique_lock<std::mutex> lock(clientsMutex_);
118 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
119 if (callbackObj == nullptr) {
120 HDF_LOGE("%{public}s: the callback of service %{public}d is null", __func__, serviceId);
121 return;
122 }
123 clients_[groupId].emplace(serviceId, callbackObj);
124 HDF_LOGD("%{public}s: service %{public}d insert the callback", __func__, serviceId);
125 return;
126 }
127
128 auto it = clients_[groupId].find(serviceId);
129 it -> second.SetReportDataCb(callbackObj);
130 HDF_LOGD("%{public}s: service %{public}d update the callback", __func__, serviceId);
131
132 return;
133 }
134
ReportDataCbUnRegister(int groupId,int serviceId,const sptr<V2_0::ISensorCallback> & callbackObj)135 void SensorClientsManager::ReportDataCbUnRegister(int groupId, int serviceId,
136 const sptr<V2_0::ISensorCallback> &callbackObj)
137 {
138 SENSOR_TRACE_PID;
139 std::unique_lock<std::mutex> lock(clientsMutex_);
140 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
141 HDF_LOGD("%{public}s: service %{public}d already UnRegister", __func__, serviceId);
142 return;
143 }
144
145 auto it = clients_[groupId].find(serviceId);
146 clients_[groupId].erase(it);
147 HDF_LOGD("%{public}s: service: %{public}d, UnRegisterCB Success", __func__, serviceId);
148 return;
149 }
150
ReportDataCbOneWay(int groupId,int serviceId)151 void SensorClientsManager::ReportDataCbOneWay(int groupId, int serviceId)
152 {
153 SENSOR_TRACE_PID;
154 HDF_LOGI("%{public}s: service: %{public}d", __func__, serviceId);
155 std::unique_lock<std::mutex> lock(clientsMutex_);
156 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
157 HDF_LOGD("%{public}s: service %{public}d already UnRegister", __func__, serviceId);
158 return;
159 }
160
161 auto it = clients_[groupId].find(serviceId);
162 it->second.oneway = true;
163 HDF_LOGI("%{public}s: service: %{public}d set oneway = true", __func__, serviceId);
164 return;
165 }
166
UpdateSensorConfig(int sensorId,int64_t samplingInterval,int64_t reportInterval)167 void SensorClientsManager::UpdateSensorConfig(int sensorId, int64_t samplingInterval, int64_t reportInterval)
168 {
169 SENSOR_TRACE_PID;
170 std::unique_lock<std::mutex> lock(sensorConfigMutex_);
171 auto it = sensorConfig_.find(sensorId);
172 if (it != sensorConfig_.end()) {
173 it->second.samplingInterval = samplingInterval <= it->second.samplingInterval ? samplingInterval
174 : it->second.samplingInterval;
175 it->second.reportInterval = reportInterval <= it->second.reportInterval ? reportInterval
176 : it->second.reportInterval;
177 } else {
178 BestSensorConfig config = {samplingInterval, reportInterval};
179 sensorConfig_.emplace(sensorId, config);
180 }
181 }
182
UpdateSdcSensorConfig(int sensorId,int64_t samplingInterval,int64_t reportInterval)183 void SensorClientsManager::UpdateSdcSensorConfig(int sensorId, int64_t samplingInterval, int64_t reportInterval)
184 {
185 SENSOR_TRACE_PID;
186 std::unique_lock<std::mutex> lock(sdcSensorConfigMutex_);
187 auto it = sdcSensorConfig_.find(sensorId);
188 if (it != sdcSensorConfig_.end()) {
189 it->second.samplingInterval = samplingInterval <= it->second.samplingInterval ? samplingInterval
190 : it->second.samplingInterval;
191 it->second.reportInterval = reportInterval <= it->second.reportInterval ? reportInterval
192 : it->second.reportInterval;
193 } else {
194 BestSensorConfig config = {samplingInterval, reportInterval};
195 sdcSensorConfig_.emplace(sensorId, config);
196 }
197 }
198
UpdateClientPeriodCount(int sensorId,int64_t samplingInterval,int64_t reportInterval)199 void SensorClientsManager::UpdateClientPeriodCount(int sensorId, int64_t samplingInterval, int64_t reportInterval)
200 {
201 SENSOR_TRACE_PID;
202 HDF_LOGD("%{public}s: sensorId is %{public}d, samplingInterval is [%{public}" PRId64 "],"
203 "reportInterval is [%{public}" PRId64 "]", __func__, sensorId,
204 samplingInterval, reportInterval);
205 std::unique_lock<std::mutex> lock(clientsMutex_);
206 if (samplingInterval <= ERROR_INTERVAL || reportInterval < ERROR_INTERVAL) {
207 HDF_LOGE("%{public}s: samplingInterval or reportInterval error", __func__);
208 return;
209 }
210 int32_t groupId = HDF_TRADITIONAL_SENSOR_TYPE;
211 if (clients_.find(groupId) == clients_.end() || clients_[groupId].empty()) {
212 return;
213 }
214 std::string result = "";
215 for (auto &entry : clients_[groupId]) {
216 auto &client = entry.second;
217 if (client.curCountMap_.find(sensorId) == client.curCountMap_.end() ||
218 HdfRemoteGetCallingPid() == entry.first) {
219 client.curCountMap_[sensorId] = INIT_CUR_COUNT;
220 }
221 if (client.sensorConfigMap_.find(sensorId) != client.sensorConfigMap_.end()) {
222 int32_t periodCount = client.sensorConfigMap_.find(sensorId)->second.samplingInterval / samplingInterval;
223 result += " serviceId=" + std::to_string(entry.first) + ", sensorId=" + std::to_string(sensorId) +
224 ", periodCount=" + std::to_string(client.sensorConfigMap_.find(sensorId)->second.samplingInterval)
225 + "/" + std::to_string(samplingInterval) + "=" + std::to_string(periodCount);
226 client.periodCountMap_[sensorId] = periodCount;
227 }
228 }
229 HDF_LOGI("%{public}s: %{public}s", __func__, result.c_str());
230 }
231
SetSensorBestConfig(int sensorId,int64_t & samplingInterval,int64_t & reportInterval)232 void SensorClientsManager::SetSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval)
233 {
234 SENSOR_TRACE_PID;
235 std::unique_lock<std::mutex> lock(sensorConfigMutex_);
236 auto it = sensorConfig_.find(sensorId);
237 if (it == sensorConfig_.end()) {
238 HDF_LOGD("%{public}s: sensor: %{public}d is enabled first time", __func__, sensorId);
239 return;
240 }
241
242 samplingInterval = samplingInterval < it->second.samplingInterval ? samplingInterval : it->second.samplingInterval;
243 reportInterval = reportInterval < it->second.reportInterval ? reportInterval : it->second.reportInterval;
244 HDF_LOGD("%{public}s: sensorId is %{public}d, after SetSensorBestConfig, samplingInterval is %{public}s, "
245 "reportInterval is %{public}s", __func__, sensorId, std::to_string(samplingInterval).c_str(),
246 std::to_string(reportInterval).c_str());
247 return;
248 }
249
SetSdcSensorBestConfig(int sensorId,int64_t & samplingInterval,int64_t & reportInterval)250 void SensorClientsManager::SetSdcSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval)
251 {
252 SENSOR_TRACE_PID;
253 std::unique_lock<std::mutex> lock(sdcSensorConfigMutex_);
254 auto it = sdcSensorConfig_.find(sensorId);
255 if (it == sdcSensorConfig_.end()) {
256 HDF_LOGD("%{public}s: sensor: %{public}d is enabled by sdc first time", __func__, sensorId);
257 return;
258 }
259
260 samplingInterval = samplingInterval < it->second.samplingInterval ? samplingInterval : it->second.samplingInterval;
261 reportInterval = reportInterval < it->second.reportInterval ? reportInterval : it->second.reportInterval;
262 HDF_LOGD("%{public}s: sensorId is %{public}d, after SetSdcSensorBestConfig, samplingInterval is %{public}s, "
263 "reportInterval is %{public}s", __func__, sensorId, std::to_string(samplingInterval).c_str(),
264 std::to_string(reportInterval).c_str());
265 return;
266 }
267
268
GetSensorBestConfig(int sensorId,int64_t & samplingInterval,int64_t & reportInterval)269 void SensorClientsManager::GetSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval)
270 {
271 SENSOR_TRACE_PID;
272 std::unique_lock<std::mutex> lock(sensorConfigMutex_);
273 auto it = sensorConfig_.find(sensorId);
274 if (it == sensorConfig_.end()) {
275 samplingInterval = STOP_INTERVAL;
276 reportInterval = STOP_INTERVAL;
277 HDF_LOGD("%{public}s: sensor: %{public}d has no best config", __func__, sensorId);
278 return;
279 }
280
281 samplingInterval = it->second.samplingInterval;
282 reportInterval = it->second.reportInterval;
283 HDF_LOGD("%{public}s: sensorId is %{public}d, after GetSensorBestConfig, samplingInterval is %{public}s, "
284 "reportInterval is %{public}s", __func__, sensorId, std::to_string(samplingInterval).c_str(),
285 std::to_string(reportInterval).c_str());
286 return;
287 }
288
EraseSdcSensorBestConfig(int sensorId)289 void SensorClientsManager::EraseSdcSensorBestConfig(int sensorId)
290 {
291 SENSOR_TRACE_PID;
292 std::unique_lock<std::mutex> lock(sdcSensorConfigMutex_);
293 auto it = sdcSensorConfig_.find(sensorId);
294 if (it == sdcSensorConfig_.end()) {
295 HDF_LOGD("%{public}s: sensor: %{public}d sdcSensorBestConfig not exist, not need erase", __func__, sensorId);
296 return;
297 }
298 sdcSensorConfig_.erase(it);
299 HDF_LOGD("%{public}s: sensor: %{public}d config has been erase from sdcSensorConfig_", __func__, sensorId);
300 return;
301 }
302
OpenSensor(int sensorId,int serviceId)303 void SensorClientsManager::OpenSensor(int sensorId, int serviceId)
304 {
305 SENSOR_TRACE_PID;
306 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
307 std::set<int> service = {serviceId};
308 sensorUsed_.emplace(sensorId, service);
309 HDF_LOGD("%{public}s: service: %{public}d enabled sensor %{public}d", __func__, serviceId, sensorId);
310 }
311
IsNeedOpenSensor(int sensorId,int serviceId)312 bool SensorClientsManager::IsNeedOpenSensor(int sensorId, int serviceId)
313 {
314 SENSOR_TRACE_PID;
315 auto it = sensorUsed_.find(sensorId);
316 if (it == sensorUsed_.end()) {
317 HDF_LOGD("%{public}s: sensor %{public}d is enabled by service: %{public}d", __func__, sensorId, serviceId);
318 return true;
319 }
320 auto service = sensorUsed_[sensorId].find(serviceId);
321 if (service == sensorUsed_[sensorId].end()) {
322 sensorUsed_[sensorId].insert(serviceId);
323 HDF_LOGD("%{public}s: service: %{public}d enabled sensor %{public}d", __func__, serviceId, sensorId);
324 }
325 return false;
326 }
327
IsNeedCloseSensor(int sensorId,int serviceId)328 bool SensorClientsManager::IsNeedCloseSensor(int sensorId, int serviceId)
329 {
330 SENSOR_TRACE_PID;
331 auto it = sensorUsed_.find(sensorId);
332 if (it == sensorUsed_.end()) {
333 HDF_LOGD("%{public}s: sensor %{public}d has been disabled or not support", __func__, sensorId);
334 return true;
335 }
336 sensorUsed_[sensorId].erase(serviceId);
337 if (sensorUsed_[sensorId].empty()) {
338 sensorUsed_.erase(sensorId);
339 sensorConfig_.erase(sensorId);
340 HDF_LOGD("%{public}s: disabled sensor %{public}d", __func__, sensorId);
341 return true;
342 }
343 for (auto sid : sensorUsed_[sensorId]) {
344 HDF_LOGD("%{public}s: sensor %{public}d also is enable by service %{public}d", __func__, sensorId, sid);
345 }
346 return false;
347 }
348
IsExistSdcSensorEnable(int sensorId)349 bool SensorClientsManager::IsExistSdcSensorEnable(int sensorId)
350 {
351 SENSOR_TRACE_PID;
352 std::unique_lock<std::mutex> lock(sdcSensorConfigMutex_);
353 auto it = sdcSensorConfig_.find(sensorId);
354 if (it == sdcSensorConfig_.end()) {
355 return false;
356 }
357 HDF_LOGE("%{public}s: sensor %{public}d has been enabled by sdc service %{public}d", __func__, sensorId, it->first);
358 return true;
359 }
360
IsUpadateSensorState(int sensorId,int serviceId,bool isOpen)361 bool SensorClientsManager::IsUpadateSensorState(int sensorId, int serviceId, bool isOpen)
362 {
363 SENSOR_TRACE_PID;
364 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
365 if (isOpen && IsNeedOpenSensor(sensorId, serviceId)) {
366 return true;
367 }
368 if (!isOpen && IsNeedCloseSensor(sensorId, serviceId)) {
369 return true;
370 }
371 return false;
372 }
373
IsClientsEmpty(int groupId)374 bool SensorClientsManager::IsClientsEmpty(int groupId)
375 {
376 SENSOR_TRACE_PID;
377 std::unique_lock<std::mutex> lock(clientsMutex_);
378 if (clients_.find(groupId) == clients_.end() || clients_[groupId].empty()) {
379 return true;
380 }
381 return false;
382 }
383
IsNoSensorUsed()384 bool SensorClientsManager::IsNoSensorUsed()
385 {
386 SENSOR_TRACE_PID;
387 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
388 for (auto it = sensorUsed_.begin(); it != sensorUsed_.end(); ++it) {
389 if (!it->second.empty()) {
390 return false;
391 }
392 }
393 return true;
394 }
395
GetClients(int groupId,std::unordered_map<int32_t,SensorClientInfo> & client)396 bool SensorClientsManager::GetClients(int groupId, std::unordered_map<int32_t, SensorClientInfo> &client)
397 {
398 SENSOR_TRACE_PID;
399 std::unique_lock<std::mutex> lock(clientsMutex_);
400 auto it = clients_.find(groupId);
401 if (it == clients_.end() || it->second.empty()) {
402 return false;
403 }
404 client = it->second;
405 return true;
406 }
407
GetBestSensorConfigMap(std::unordered_map<int32_t,struct BestSensorConfig> & map)408 bool SensorClientsManager::GetBestSensorConfigMap(std::unordered_map<int32_t, struct BestSensorConfig> &map)
409 {
410 SENSOR_TRACE_PID;
411 std::unique_lock<std::mutex> lock(sensorConfigMutex_);
412 map = sensorConfig_;
413 return true;
414 }
415
SetClientSenSorConfig(int32_t sensorId,int32_t serviceId,int64_t samplingInterval,int64_t & reportInterval)416 void SensorClientsManager::SetClientSenSorConfig(int32_t sensorId, int32_t serviceId, int64_t samplingInterval,
417 int64_t &reportInterval)
418 {
419 SENSOR_TRACE_PID;
420 std::unique_lock<std::mutex> lock(clientsMutex_);
421 HDF_LOGD("%{public}s: service %{public}d enter the SetClientSenSorConfig function, sensorId is %{public}d, "
422 "samplingInterval is %{public}s, reportInterval is %{public}s", __func__, serviceId, sensorId,
423 std::to_string(samplingInterval).c_str(), std::to_string(reportInterval).c_str());
424
425 int32_t groupId = HDF_TRADITIONAL_SENSOR_TYPE;
426 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
427 HDF_LOGE("%{public}s: service %{public}d already UnRegister", __func__, serviceId);
428 return;
429 }
430
431 auto &client = clients_[groupId].find(serviceId)->second;
432 SensorConfig sensorConfig = {samplingInterval, reportInterval};
433 client.sensorConfigMap_[sensorId] = sensorConfig;
434 }
435
IsSensorContinues(int32_t sensorId)436 bool SensorClientsManager::IsSensorContinues(int32_t sensorId)
437 {
438 return std::find(continuesSensor.begin(), continuesSensor.end(), sensorId) != continuesSensor.end();
439 }
440
IsNotNeedReportData(SensorClientInfo & sensorClientInfo,const int32_t & sensorId,const int32_t & serviceId)441 bool SensorClientsManager::IsNotNeedReportData(SensorClientInfo &sensorClientInfo, const int32_t &sensorId,
442 const int32_t &serviceId)
443 {
444 SENSOR_TRACE;
445 if (!SensorClientsManager::IsSensorContinues(sensorId)) {
446 return false;
447 }
448 if (sensorClientInfo.periodCountMap_.find(sensorId) == sensorClientInfo.periodCountMap_.end()) {
449 return false;
450 }
451 bool result = true;
452 sensorClientInfo.PrintClientMapInfo(serviceId, sensorId);
453 if (sensorClientInfo.curCountMap_[sensorId] == 0) {
454 result = false;
455 }
456 sensorClientInfo.curCountMap_[sensorId]++;
457 if (sensorClientInfo.curCountMap_[sensorId] >= sensorClientInfo.periodCountMap_[sensorId]) {
458 sensorClientInfo.curCountMap_[sensorId] = 0;
459 }
460 return result;
461 }
462
GetServiceIds(int32_t & sensorId)463 std::set<int32_t> SensorClientsManager::GetServiceIds(int32_t &sensorId)
464 {
465 SENSOR_TRACE;
466 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
467 if (sensorUsed_.find(sensorId) == sensorUsed_.end()) {
468 HDF_LOGD("%{public}s sensor %{public}d is not enabled by anyone", __func__, sensorId);
469 return std::set<int32_t>();
470 }
471 return sensorUsed_.find(sensorId)->second;
472 }
473
ReportEachClient(const V2_0::HdfSensorEvents & event)474 std::string SensorClientsManager::ReportEachClient(const V2_0::HdfSensorEvents& event)
475 {
476 SENSOR_TRACE;
477 std::string result = "services=";
478 int32_t sensorId = event.sensorId;
479 const std::set<int32_t> services = GetServiceIds(sensorId);
480 int32_t groupId = HDF_TRADITIONAL_SENSOR_TYPE;
481 static struct SensorInfoId sensorInfoId;
482 {
483 std::unique_lock<std::mutex> lock(clientsMutex_);
484 if (clients_.find(groupId) == clients_.end() || clients_.find(groupId)->second.empty()) {
485 HDF_LOGE("%{public}s groupId %{public}d is not enabled by anyone", __func__, sensorId);
486 return result;
487 }
488 }
489 for (auto it = services.begin(); it != services.end(); ++it) {
490 int32_t serviceId = *it;
491 sptr<V2_0::ISensorCallback> callback;
492 bool oneway = false;
493 {
494 std::unique_lock<std::mutex> lock(clientsMutex_);
495 if (clients_.find(groupId)->second.find(serviceId) == clients_.find(groupId)->second.end()) {
496 continue;
497 }
498 SensorClientInfo &sensorClientInfo = clients_.find(groupId)->second.find(serviceId)->second;
499 if (IsNotNeedReportData(sensorClientInfo, sensorId, serviceId)) {
500 continue;
501 }
502 callback = sensorClientInfo.GetReportDataCb();
503 oneway = sensorClientInfo.oneway;
504 if (callback == nullptr) {
505 HDF_LOGD("%{public}s the callback of %{public}d is nullptr", __func__, serviceId);
506 continue;
507 }
508 }
509 HITRACE_METER_FMT(HITRACE_TAG_HDF, "%s: serviceId %d, sensorId %d", __func__, serviceId, event.sensorId);
510 sensorInfoId.sensorId = event.sensorId;
511 sensorInfoId.serviceId = serviceId;
512 HdiReportData(callback, event, result, oneway, sensorInfoId);
513 }
514 return result;
515 }
516
HdiReportData(const sptr<V2_0::ISensorCallback> & callback,const V2_0::HdfSensorEvents & event,std::string & result,const bool & oneway,SensorInfoId sensorInfoId)517 void SensorClientsManager::HdiReportData(const sptr<V2_0::ISensorCallback> &callback,
518 const V2_0::HdfSensorEvents& event, std::string &result, const bool &oneway, SensorInfoId sensorInfoId)
519 {
520 int32_t ret = HDF_SUCCESS;
521 if (oneway) {
522 const sptr<IRemoteObject> &remote = OHOS::HDI::hdi_objcast(callback);
523 const sptr<V2_1::ISensorCallback> &cb = OHOS::HDI::hdi_facecast<V2_1::ISensorCallback>(remote);
524 std::vector<OHOS::HDI::Sensor::V2_0::HdfSensorEvents> eventsVec;
525 eventsVec.push_back(std::move(event));
526 cb->OnDataEventAsync(eventsVec);
527 } else {
528 ret = callback->OnDataEvent(event);
529 }
530 if (ret != HDF_SUCCESS) {
531 HDF_LOGD("%{public}s Sensor OnDataEvent failed, error code is %{public}d", __func__, ret);
532 if (ret == ERROR_RETURN_VALUE) {
533 DeleteService(sensorInfoId.sensorId, sensorInfoId.serviceId);
534 }
535 } else {
536 static std::unordered_map<int32_t, std::unordered_map<int32_t, int64_t>> sensorReportCountMap;
537 auto it = sensorReportCountMap[sensorInfoId.sensorId].find(sensorInfoId.serviceId);
538 int64_t reportCount = INIT_REPORT_COUNT;
539 if (it == sensorReportCountMap[sensorInfoId.sensorId].end()) {
540 sensorReportCountMap[sensorInfoId.sensorId][sensorInfoId.serviceId] = INIT_REPORT_COUNT;
541 } else {
542 it->second++;
543 reportCount = it->second;
544 }
545 result += std::to_string(sensorInfoId.serviceId) + "-" + std::to_string(reportCount) + " ";
546 }
547 }
548
GetSensorUsed()549 std::unordered_map<int32_t, std::set<int32_t>> SensorClientsManager::GetSensorUsed()
550 {
551 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
552 return sensorUsed_;
553 }
554
DeleteService(int32_t sensorId,int32_t serviceId)555 void SensorClientsManager::DeleteService(int32_t sensorId, int32_t serviceId)
556 {
557 HDF_LOGD("%{public}s Delete service: %{public}d, sensorId: %{public}d", __func__, serviceId, sensorId);
558 int32_t ret = HDF_SUCCESS;
559 int32_t groupId = HDF_TRADITIONAL_SENSOR_TYPE;
560 bool needDisableSensor = false;
561 {
562 std::unique_lock<std::mutex> lock(sensorUsedMutex_);
563 auto it = sensorUsed_.find(sensorId);
564 if (it != sensorUsed_.end()) {
565 sensorUsed_[sensorId].erase(serviceId);
566 if (sensorUsed_[sensorId].empty()) {
567 sensorUsed_.erase(sensorId);
568 sensorConfig_.erase(sensorId);
569 HDF_LOGD("%{public}s: disabled sensor %{public}d", __func__, sensorId);
570 needDisableSensor = true;
571 }
572 }
573 for (auto sid : sensorUsed_[sensorId]) {
574 HDF_LOGD("%{public}s: sensor %{public}d also is enabled by service %{public}d", __func__, sensorId, sid);
575 }
576 }
577 if (needDisableSensor) {
578 if (sensorVdiImpl_ == nullptr) {
579 HDF_LOGE("%{public}s: sensorVdiImpl_ is nullptr", __func__);
580 return;
581 }
582 if (IsExistSdcSensorEnable(sensorId)) {
583 SENSOR_TRACE_START("sensorVdiImpl_->SetSaBatch");
584 ret = sensorVdiImpl_->SetSaBatch(sensorId, REPORT_INTERVAL, REPORT_INTERVAL);
585 SENSOR_TRACE_FINISH;
586 if (ret != HDF_SUCCESS) {
587 HDF_LOGE("%{public}s: SetSaBatch failed, error code is %{public}d, sensorId = %{public}d,"
588 "serviceId = %{public}d", __func__, ret, sensorId, serviceId);
589 }
590 } else {
591 SENSOR_TRACE_START("sensorVdiImpl_->Disable");
592 ret = sensorVdiImpl_->Disable(sensorId);
593 SENSOR_TRACE_FINISH;
594 if (ret != HDF_SUCCESS) {
595 HDF_LOGE("%{public}s: Disable failed, error code is %{public}d, sensorId = %{public}d,"
596 "serviceId = %{public}d", __func__, ret, sensorId, serviceId);
597 }
598 }
599 }
600 EraseClients(groupId, serviceId);
601 }
602
EraseClients(int32_t groupId,int32_t serviceId)603 void SensorClientsManager::EraseClients(int32_t groupId, int32_t serviceId)
604 {
605 std::unique_lock<std::mutex> lock(clientsMutex_);
606 if (clients_.find(groupId) == clients_.end() || clients_[groupId].find(serviceId) == clients_[groupId].end()) {
607 HDF_LOGE("%{public}s: service: %{public}d already UnRegister", __func__, serviceId);
608 return;
609 }
610
611 auto it = clients_[groupId].find(serviceId);
612 clients_[groupId].erase(it);
613 HDF_LOGI("%{public}s: service: %{public}d UnRegisterCB Success", __func__, serviceId);
614 }
615
SetSensorVdiImpl(OHOS::HDI::Sensor::V1_1::ISensorInterfaceVdi * sensorVdiImpl)616 void SensorClientsManager::SetSensorVdiImpl(OHOS::HDI::Sensor::V1_1::ISensorInterfaceVdi *sensorVdiImpl)
617 {
618 sensorVdiImpl_ = sensorVdiImpl;
619 }
620
ReSetSensorPrintTime(int32_t sensorId)621 void SensorClientsManager::ReSetSensorPrintTime(int32_t sensorId)
622 {
623 SENSOR_TRACE;
624 std::unique_lock<std::mutex> lock(sensorPrintTimesMutex_);
625 sensorPrintTimes_[sensorId] = ZERO_PRINT_TIME;
626 }
627
IsSensorNeedPrint(int32_t sensorId)628 bool SensorClientsManager::IsSensorNeedPrint(int32_t sensorId)
629 {
630 SENSOR_TRACE;
631 std::unique_lock<std::mutex> lock(sensorPrintTimesMutex_);
632 auto it = sensorPrintTimes_.find(sensorId);
633 if (it == sensorPrintTimes_.end() || it->second > MAX_PRINT_TIME) {
634 return false;
635 }
636 it->second++;
637 return true;
638 }
639
GetInstance()640 SensorClientsManager* SensorClientsManager::GetInstance()
641 {
642 static SensorClientsManager *instance = new SensorClientsManager();
643 return instance;
644 }
645
646 } // V2_1
647 } // Sensor
648 } // HDI
649 } // OHOS