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