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