1 /*
2 * Copyright (c) 2021-2022 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 "client_info.h"
17
18 #include <mutex>
19
20 #include "permission_util.h"
21 #include "securec.h"
22 #include "sensor_hdi_connection.h"
23 #include "sensor_manager.h"
24 #include "sensors_errors.h"
25
26 namespace OHOS {
27 namespace Sensors {
28 using namespace OHOS::HiviewDFX;
29
30 namespace {
31 constexpr HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "ClientInfo" };
32 constexpr uint32_t INVALID_SENSOR_ID = -1;
33 constexpr int32_t INVALID_PID = -1;
34 constexpr int32_t INVALID_UID = -1;
35 constexpr int32_t MIN_MAP_SIZE = 0;
36 constexpr uint32_t NO_STROE_EVENT = -2;
37 constexpr uint32_t MAX_SUPPORT_CHANNEL = 200;
38 constexpr uint32_t MAX_DUMP_DATA_SIZE = 10;
39 constexpr uint32_t HEART_RATE_SENSOR_ID = 83886336;
40 } // namespace
41
GetSensorState(uint32_t sensorId)42 bool ClientInfo::GetSensorState(uint32_t sensorId)
43 {
44 CALL_LOG_ENTER;
45 if (sensorId == INVALID_SENSOR_ID) {
46 SEN_HILOGE("sensorId is invalid");
47 return false;
48 }
49 std::lock_guard<std::mutex> clientLock(clientMutex_);
50 auto it = clientMap_.find(sensorId);
51 if (it == clientMap_.end()) {
52 SEN_HILOGE("cannot find sensorId:%{public}u", sensorId);
53 return false;
54 }
55 for (const auto &pidIt : it->second) {
56 if (pidIt.second.GetSensorState() == true) {
57 return true;
58 }
59 }
60 SEN_HILOGE("cannot find sensorinfo, sensorId:%{public}u", sensorId);
61 return false;
62 }
63
GetBestSensorInfo(uint32_t sensorId)64 SensorBasicInfo ClientInfo::GetBestSensorInfo(uint32_t sensorId)
65 {
66 int64_t minSamplingPeriodNs = LLONG_MAX;
67 int64_t minReportDelayNs = LLONG_MAX;
68 SensorBasicInfo sensorInfo;
69 sensorInfo.SetSamplingPeriodNs(minSamplingPeriodNs);
70 sensorInfo.SetMaxReportDelayNs(minReportDelayNs);
71 if (sensorId == INVALID_SENSOR_ID) {
72 SEN_HILOGE("sensorId is invalid");
73 return sensorInfo;
74 }
75
76 std::lock_guard<std::mutex> clientLock(clientMutex_);
77 auto it = clientMap_.find(sensorId);
78 if (it == clientMap_.end()) {
79 SEN_HILOGE("cannot find sensorId:%{public}u", sensorId);
80 return sensorInfo;
81 }
82 for (const auto &pidIt : it->second) {
83 int64_t curSamplingPeriodNs = pidIt.second.GetSamplingPeriodNs();
84 int64_t curReportDelayNs = pidIt.second.GetMaxReportDelayNs();
85 minSamplingPeriodNs = (curSamplingPeriodNs < minSamplingPeriodNs) ? curSamplingPeriodNs : minSamplingPeriodNs;
86 minReportDelayNs = (curReportDelayNs < minReportDelayNs) ? curReportDelayNs : minReportDelayNs;
87 }
88 sensorInfo.SetSamplingPeriodNs(minSamplingPeriodNs);
89 sensorInfo.SetMaxReportDelayNs(minReportDelayNs);
90 return sensorInfo;
91 }
92
OnlyCurPidSensorEnabled(uint32_t sensorId,int32_t pid)93 bool ClientInfo::OnlyCurPidSensorEnabled(uint32_t sensorId, int32_t pid)
94 {
95 CALL_LOG_ENTER;
96 if ((sensorId == INVALID_SENSOR_ID) || (pid <= INVALID_PID)) {
97 SEN_HILOGE("sensorId or pid is invalid");
98 return false;
99 }
100 std::lock_guard<std::mutex> clientLock(clientMutex_);
101 auto it = clientMap_.find(sensorId);
102 if (it == clientMap_.end()) {
103 SEN_HILOGE("cannot find sensorId:%{public}u", sensorId);
104 return false;
105 }
106 bool ret = false;
107 for (const auto &pidIt : it->second) {
108 if (!pidIt.second.GetSensorState()) {
109 continue;
110 }
111 if (pidIt.first != pid) {
112 return false;
113 }
114 ret = true;
115 }
116 return ret;
117 }
118
UpdateAppThreadInfo(int32_t pid,int32_t uid,AccessTokenID callerToken)119 bool ClientInfo::UpdateAppThreadInfo(int32_t pid, int32_t uid, AccessTokenID callerToken)
120 {
121 CALL_LOG_ENTER;
122 if ((uid == INVALID_UID) || (pid <= INVALID_PID)) {
123 SEN_HILOGE("uid or pid is invalid");
124 return false;
125 }
126 std::lock_guard<std::mutex> uidLock(uidMutex_);
127 AppThreadInfo appThreadInfo(pid, uid, callerToken);
128 auto appThreadInfoItr = appThreadInfoMap_.find(pid);
129 if (appThreadInfoItr == appThreadInfoMap_.end()) {
130 if (appThreadInfoMap_.size() == MAX_SUPPORT_CHANNEL) {
131 SEN_HILOGE("max support channel size is %{public}u", MAX_SUPPORT_CHANNEL);
132 return false;
133 }
134 auto ret = appThreadInfoMap_.insert(std::make_pair(pid, appThreadInfo));
135 return ret.second;
136 }
137 appThreadInfoMap_[pid] = appThreadInfo;
138 return true;
139 }
140
DestroyAppThreadInfo(int32_t pid)141 void ClientInfo::DestroyAppThreadInfo(int32_t pid)
142 {
143 CALL_LOG_ENTER;
144 if (pid == INVALID_PID) {
145 SEN_HILOGE("pid is invalid");
146 return;
147 }
148 std::lock_guard<std::mutex> uidLock(uidMutex_);
149 auto appThreadInfoItr = appThreadInfoMap_.find(pid);
150 if (appThreadInfoItr == appThreadInfoMap_.end()) {
151 SEN_HILOGD("uid not exist, no need to destroy it");
152 return;
153 }
154 appThreadInfoMap_.erase(appThreadInfoItr);
155 }
156
GetSensorChannelByUid(int32_t uid)157 std::vector<sptr<SensorBasicDataChannel>> ClientInfo::GetSensorChannelByUid(int32_t uid)
158 {
159 CALL_LOG_ENTER;
160 if (uid == INVALID_UID) {
161 SEN_HILOGE("uid is invalid");
162 return {};
163 }
164 std::vector<sptr<SensorBasicDataChannel>> sensorChannel;
165 std::lock_guard<std::mutex> uidLock(uidMutex_);
166 for (const auto &appThreadInfoIt : appThreadInfoMap_) {
167 if (uid != appThreadInfoIt.second.uid) {
168 continue;
169 }
170 std::lock_guard<std::mutex> channelLock(channelMutex_);
171 auto channelIt = channelMap_.find(appThreadInfoIt.first);
172 if (channelIt == channelMap_.end()) {
173 continue;
174 }
175 sensorChannel.push_back(channelIt->second);
176 }
177 return sensorChannel;
178 }
179
GetSensorChannelByPid(int32_t pid)180 sptr<SensorBasicDataChannel> ClientInfo::GetSensorChannelByPid(int32_t pid)
181 {
182 CALL_LOG_ENTER;
183 if (pid == INVALID_PID) {
184 SEN_HILOGE("pid is invalid");
185 return nullptr;
186 }
187 std::lock_guard<std::mutex> channelLock(channelMutex_);
188 auto channelIt = channelMap_.find(pid);
189 if (channelIt == channelMap_.end()) {
190 SEN_HILOGE("there is no channel belong to the pid");
191 return nullptr;
192 }
193 return channelIt->second;
194 }
195
GetSensorChannel(uint32_t sensorId)196 std::vector<sptr<SensorBasicDataChannel>> ClientInfo::GetSensorChannel(uint32_t sensorId)
197 {
198 if (sensorId == INVALID_SENSOR_ID) {
199 SEN_HILOGE("sensorId is invalid");
200 return {};
201 }
202 std::lock_guard<std::mutex> clientLock(clientMutex_);
203 auto clientIt = clientMap_.find(sensorId);
204 if (clientIt == clientMap_.end()) {
205 SEN_HILOGD("there is no channel belong to sensorId:%{public}u", sensorId);
206 return {};
207 }
208 std::vector<sptr<SensorBasicDataChannel>> sensorChannel;
209 for (const auto &sensorInfoIt : clientIt->second) {
210 std::lock_guard<std::mutex> channelLock(channelMutex_);
211 auto channelIt = channelMap_.find(sensorInfoIt.first);
212 if (channelIt == channelMap_.end()) {
213 continue;
214 }
215 sensorChannel.push_back(channelIt->second);
216 }
217 return sensorChannel;
218 }
219
UpdateSensorInfo(uint32_t sensorId,int32_t pid,const SensorBasicInfo & sensorInfo)220 bool ClientInfo::UpdateSensorInfo(uint32_t sensorId, int32_t pid, const SensorBasicInfo &sensorInfo)
221 {
222 CALL_LOG_ENTER;
223 if ((sensorId == INVALID_SENSOR_ID) || (pid <= INVALID_PID) || (!sensorInfo.GetSensorState())) {
224 SEN_HILOGE("params are invalid");
225 return false;
226 }
227 std::lock_guard<std::mutex> clientLock(clientMutex_);
228 auto it = clientMap_.find(sensorId);
229 if (it == clientMap_.end()) {
230 std::unordered_map<int32_t, SensorBasicInfo> pidMap;
231 auto pidRet = pidMap.insert(std::make_pair(pid, sensorInfo));
232 auto clientRet = clientMap_.insert(std::make_pair(sensorId, pidMap));
233 return pidRet.second && clientRet.second;
234 }
235 auto pidIt = it->second.find(pid);
236 if (pidIt == it->second.end()) {
237 auto ret = it->second.insert(std::make_pair(pid, sensorInfo));
238 return ret.second;
239 }
240 it->second[pid] = sensorInfo;
241 return true;
242 }
243
RemoveSubscriber(uint32_t sensorId,uint32_t pid)244 void ClientInfo::RemoveSubscriber(uint32_t sensorId, uint32_t pid)
245 {
246 std::lock_guard<std::mutex> clientLock(clientMutex_);
247 auto it = clientMap_.find(sensorId);
248 if (it == clientMap_.end()) {
249 SEN_HILOGW("sensorId not exist");
250 return;
251 }
252 auto pidIt = it->second.find(pid);
253 if (pidIt != it->second.end()) {
254 it->second.erase(pidIt);
255 }
256 }
257
UpdateSensorChannel(int32_t pid,const sptr<SensorBasicDataChannel> & channel)258 bool ClientInfo::UpdateSensorChannel(int32_t pid, const sptr<SensorBasicDataChannel> &channel)
259 {
260 CALL_LOG_ENTER;
261 CHKPR(channel, false);
262 if (pid <= INVALID_PID) {
263 SEN_HILOGE("pid or channel is invalid or channel cannot be null");
264 return false;
265 }
266 std::lock_guard<std::mutex> channelLock(channelMutex_);
267 auto it = channelMap_.find(pid);
268 if (it == channelMap_.end()) {
269 if (channelMap_.size() == MAX_SUPPORT_CHANNEL) {
270 SEN_HILOGE("max support channel size:%{public}u", MAX_SUPPORT_CHANNEL);
271 return false;
272 }
273 auto ret = channelMap_.insert(std::make_pair(pid, channel));
274 SEN_HILOGD("ret.second:%{public}d", ret.second);
275 return ret.second;
276 }
277 channelMap_[pid] = channel;
278 return true;
279 }
280
ClearSensorInfo(uint32_t sensorId)281 void ClientInfo::ClearSensorInfo(uint32_t sensorId)
282 {
283 CALL_LOG_ENTER;
284 if (sensorId == INVALID_SENSOR_ID) {
285 SEN_HILOGE("sensorId is invalid");
286 return;
287 }
288 std::lock_guard<std::mutex> clientLock(clientMutex_);
289 auto it = clientMap_.find(sensorId);
290 if (it == clientMap_.end()) {
291 SEN_HILOGD("sensorId not exist, no need to clear it");
292 return;
293 }
294 clientMap_.erase(it);
295 }
296
ClearCurPidSensorInfo(uint32_t sensorId,int32_t pid)297 void ClientInfo::ClearCurPidSensorInfo(uint32_t sensorId, int32_t pid)
298 {
299 CALL_LOG_ENTER;
300 if ((sensorId == INVALID_SENSOR_ID) || (pid <= INVALID_PID)) {
301 SEN_HILOGE("sensorId or pid is invalid");
302 return;
303 }
304 std::lock_guard<std::mutex> clientLock(clientMutex_);
305 auto it = clientMap_.find(sensorId);
306 if (it == clientMap_.end()) {
307 SEN_HILOGD("sensorId not exist, no need to clear it");
308 return;
309 }
310 auto pidIt = it->second.find(pid);
311 if (pidIt == it->second.end()) {
312 SEN_HILOGD("pid not exist, no need to clear it");
313 return;
314 }
315 pidIt = it->second.erase(pidIt);
316 if (it->second.size() == MIN_MAP_SIZE) {
317 it = clientMap_.erase(it);
318 }
319 }
320
DestroySensorChannel(int32_t pid)321 bool ClientInfo::DestroySensorChannel(int32_t pid)
322 {
323 CALL_LOG_ENTER;
324 if (pid <= INVALID_PID) {
325 SEN_HILOGE("pid is invalid");
326 return false;
327 }
328 std::lock_guard<std::mutex> clientLock(clientMutex_);
329 for (auto it = clientMap_.begin(); it != clientMap_.end();) {
330 auto pidIt = it->second.find(pid);
331 if (pidIt == it->second.end()) {
332 it++;
333 continue;
334 }
335 pidIt = it->second.erase(pidIt);
336 if (it->second.size() != MIN_MAP_SIZE) {
337 it++;
338 continue;
339 }
340 it = clientMap_.erase(it);
341 }
342 DestroyAppThreadInfo(pid);
343 std::lock_guard<std::mutex> channelLock(channelMutex_);
344 auto it = channelMap_.find(pid);
345 if (it == channelMap_.end()) {
346 SEN_HILOGD("there is no channel belong to pid, no need to destroy");
347 return true;
348 }
349 it = channelMap_.erase(it);
350 return true;
351 }
352
GetCurPidSensorInfo(uint32_t sensorId,int32_t pid)353 SensorBasicInfo ClientInfo::GetCurPidSensorInfo(uint32_t sensorId, int32_t pid)
354 {
355 int64_t minSamplingPeriodNs = LLONG_MAX;
356 int64_t minReportDelayNs = LLONG_MAX;
357 SensorBasicInfo sensorInfo;
358 sensorInfo.SetSamplingPeriodNs(minSamplingPeriodNs);
359 sensorInfo.SetMaxReportDelayNs(minReportDelayNs);
360 if ((sensorId == INVALID_SENSOR_ID) || (pid <= INVALID_PID)) {
361 SEN_HILOGE("sensorId or channel is invalid");
362 return sensorInfo;
363 }
364 std::lock_guard<std::mutex> clientLock(clientMutex_);
365 auto it = clientMap_.find(sensorId);
366 if (it == clientMap_.end()) {
367 SEN_HILOGE("cannot find sensorId:%{public}u", sensorId);
368 return sensorInfo;
369 }
370 auto pidIt = it->second.find(pid);
371 if (pidIt == it->second.end()) {
372 SEN_HILOGE("cannot find pid:%{public}d", pid);
373 return sensorInfo;
374 }
375 sensorInfo.SetSamplingPeriodNs(pidIt->second.GetSamplingPeriodNs());
376 sensorInfo.SetMaxReportDelayNs(pidIt->second.GetMaxReportDelayNs());
377 return sensorInfo;
378 }
379
ComputeBestPeriodCount(uint32_t sensorId,sptr<SensorBasicDataChannel> & channel)380 uint64_t ClientInfo::ComputeBestPeriodCount(uint32_t sensorId, sptr<SensorBasicDataChannel> &channel)
381 {
382 if (sensorId == INVALID_SENSOR_ID || channel == nullptr) {
383 SEN_HILOGE("sensorId is invalid or channel cannot be null");
384 return 0UL;
385 }
386 int32_t pid = INVALID_PID;
387 {
388 std::lock_guard<std::mutex> channelLock(channelMutex_);
389 for (const auto &channelIt : channelMap_) {
390 if (channelIt.second == channel) {
391 pid = channelIt.first;
392 }
393 }
394 }
395 int64_t bestSamplingPeriod = GetBestSensorInfo(sensorId).GetSamplingPeriodNs();
396 int64_t curSamplingPeriod = GetCurPidSensorInfo(sensorId, pid).GetSamplingPeriodNs();
397 if (bestSamplingPeriod == 0L) {
398 SEN_HILOGE("best Sensor Sampling Period is 0");
399 return 0UL;
400 }
401 int64_t ret = curSamplingPeriod / bestSamplingPeriod;
402 return (ret <= 0L) ? 0UL : ret;
403 }
404
ComputeBestFifoCount(uint32_t sensorId,sptr<SensorBasicDataChannel> & channel)405 uint64_t ClientInfo::ComputeBestFifoCount(uint32_t sensorId, sptr<SensorBasicDataChannel> &channel)
406 {
407 if (channel == nullptr || sensorId == INVALID_SENSOR_ID) {
408 SEN_HILOGE("sensorId is invalid or channel cannot be null");
409 return 0UL;
410 }
411 int32_t pid = INVALID_PID;
412 {
413 std::lock_guard<std::mutex> channelLock(channelMutex_);
414 for (const auto &channelIt : channelMap_) {
415 if (channelIt.second == channel) {
416 pid = channelIt.first;
417 }
418 }
419 }
420 int64_t curReportDelay = GetCurPidSensorInfo(sensorId, pid).GetMaxReportDelayNs();
421 int64_t curSamplingPeriod = GetCurPidSensorInfo(sensorId, pid).GetSamplingPeriodNs();
422 if (curSamplingPeriod == 0L) {
423 SEN_HILOGE("best sensor fifo count is 0");
424 return 0UL;
425 }
426 int64_t ret = curReportDelay / curSamplingPeriod;
427 return (ret <= 0L) ? 0UL : ret;
428 }
429
GetStoreEvent(int32_t sensorId,SensorEvent & event)430 int32_t ClientInfo::GetStoreEvent(int32_t sensorId, SensorEvent &event)
431 {
432 std::lock_guard<std::mutex> lock(eventMutex_);
433 auto storedEvent = storedEvent_.find(sensorId);
434 if (storedEvent != storedEvent_.end()) {
435 errno_t ret = memcpy_s(&event, sizeof(SensorEvent), &storedEvent->second, sizeof(SensorEvent));
436 if (ret != EOK) {
437 SEN_HILOGE("memcpy_s failed, sensorId:%{public}d", sensorId);
438 return ret;
439 }
440 return ERR_OK;
441 }
442
443 SEN_HILOGE("can't get store event, sensorId:%{public}u", sensorId);
444 return NO_STROE_EVENT;
445 }
446
StoreEvent(const SensorEvent & event)447 void ClientInfo::StoreEvent(const SensorEvent &event)
448 {
449 bool foundSensor = false;
450 SensorEvent storedEvent;
451 auto sensorHdiConnection = &SensorHdiConnection::GetInstance();
452 if (sensorHdiConnection == nullptr) {
453 SEN_HILOGE("sensorHdiConnection cannot be null");
454 return;
455 }
456 std::vector<Sensor> sensors;
457 int32_t ret = sensorHdiConnection->GetSensorList(sensors);
458 if (ret != 0) {
459 SEN_HILOGE("GetSensorList is failed");
460 return;
461 }
462 errno_t retVal = memcpy_s(&storedEvent, sizeof(storedEvent), &event, sizeof(event));
463 if (retVal != EOK) {
464 SEN_HILOGE("memcpy_s is failed");
465 return;
466 }
467 for (size_t i = 0; i < sensors.size(); i++) {
468 if ((int32_t)(sensors[i].GetSensorId()) == storedEvent.sensorTypeId) {
469 foundSensor = true;
470 break;
471 }
472 }
473
474 if (foundSensor) {
475 std::lock_guard<std::mutex> lock(eventMutex_);
476 storedEvent_[storedEvent.sensorTypeId] = storedEvent;
477 }
478 }
479
SaveClientPid(const sptr<IRemoteObject> & sensorClient,int32_t pid)480 bool ClientInfo::SaveClientPid(const sptr<IRemoteObject> &sensorClient, int32_t pid)
481 {
482 CALL_LOG_ENTER;
483 CHKPF(sensorClient);
484 std::lock_guard<std::mutex> lock(clientPidMutex_);
485 auto it = clientPidMap_.find(sensorClient);
486 if (it == clientPidMap_.end()) {
487 clientPidMap_.insert(std::make_pair(sensorClient, pid));
488 return true;
489 }
490 clientPidMap_.insert(std::make_pair(sensorClient, pid));
491 return true;
492 }
493
FindClientPid(const sptr<IRemoteObject> & sensorClient)494 int32_t ClientInfo::FindClientPid(const sptr<IRemoteObject> &sensorClient)
495 {
496 CALL_LOG_ENTER;
497 CHKPR(sensorClient, INVALID_PID);
498 std::lock_guard<std::mutex> lock(clientPidMutex_);
499 auto it = clientPidMap_.find(sensorClient);
500 if (it == clientPidMap_.end()) {
501 SEN_HILOGE("cannot find client pid");
502 return INVALID_PID;
503 }
504 return it->second;
505 }
506
DestroyClientPid(const sptr<IRemoteObject> & sensorClient)507 void ClientInfo::DestroyClientPid(const sptr<IRemoteObject> &sensorClient)
508 {
509 CALL_LOG_ENTER;
510 CHKPV(sensorClient);
511 std::lock_guard<std::mutex> lock(clientPidMutex_);
512 auto it = clientPidMap_.find(sensorClient);
513 if (it == clientPidMap_.end()) {
514 SEN_HILOGE("cannot find client pid");
515 return;
516 }
517 clientPidMap_.erase(it);
518 }
519
ClearEvent()520 void ClientInfo::ClearEvent()
521 {
522 std::lock_guard<std::mutex> lock(eventMutex_);
523 storedEvent_.clear();
524 }
525
GetSensorIdByPid(int32_t pid)526 std::vector<uint32_t> ClientInfo::GetSensorIdByPid(int32_t pid)
527 {
528 CALL_LOG_ENTER;
529 std::vector<uint32_t> sensorIdVec;
530 std::lock_guard<std::mutex> clientLock(clientMutex_);
531 for (const auto &itClientMap : clientMap_) {
532 auto it = itClientMap.second.find(pid);
533 if (it != itClientMap.second.end()) {
534 sensorIdVec.push_back(itClientMap.first);
535 }
536 }
537 return sensorIdVec;
538 }
539
GetAppInfoByChannel(const sptr<SensorBasicDataChannel> & channel)540 AppThreadInfo ClientInfo::GetAppInfoByChannel(const sptr<SensorBasicDataChannel> &channel)
541 {
542 CALL_LOG_ENTER;
543 AppThreadInfo appThreadInfo;
544 {
545 std::lock_guard<std::mutex> channelLock(channelMutex_);
546 for (auto channelIt = channelMap_.begin(); channelIt != channelMap_.end(); channelIt++) {
547 if (channelIt->second == channel) {
548 appThreadInfo.pid = channelIt->first;
549 }
550 }
551 }
552 {
553 std::lock_guard<std::mutex> uidLock(uidMutex_);
554 auto it = appThreadInfoMap_.find(appThreadInfo.pid);
555 if (it != appThreadInfoMap_.end()) {
556 appThreadInfo.uid = it->second.uid;
557 appThreadInfo.callerToken = it->second.callerToken;
558 }
559 }
560 return appThreadInfo;
561 }
562
GetSensorChannelInfo(std::vector<SensorChannelInfo> & channelInfo)563 void ClientInfo::GetSensorChannelInfo(std::vector<SensorChannelInfo> &channelInfo)
564 {
565 CALL_LOG_ENTER;
566 std::lock_guard<std::mutex> clientLock(clientMutex_);
567 for (const auto &sensorIt : clientMap_) {
568 for (const auto &pidIt : sensorIt.second) {
569 int32_t pid = pidIt.first;
570 int32_t uid = GetUidByPid(pid);
571 if (uid == INVALID_UID) {
572 SEN_HILOGW("uid is invalid, uid:%{public}d", uid);
573 continue;
574 }
575 SensorChannelInfo channel;
576 channel.SetUid(uid);
577 channel.SetSensorId(sensorIt.first);
578 std::string packageName;
579 SensorManager::GetInstance().GetPackageName(GetTokenIdByPid(pid), packageName);
580 channel.SetPackageName(packageName);
581 int64_t samplingPeriodNs = pidIt.second.GetSamplingPeriodNs();
582 int64_t maxReportDelayNs = pidIt.second.GetMaxReportDelayNs();
583 channel.SetSamplingPeriodNs(samplingPeriodNs);
584 uint32_t fifoCount = (samplingPeriodNs == 0) ? 0 : (uint32_t)(maxReportDelayNs / samplingPeriodNs);
585 channel.SetFifoCount(fifoCount);
586 channel.SetCmdType(GetCmdList(sensorIt.first, uid));
587 channelInfo.push_back(channel);
588 }
589 }
590 }
591
GetUidByPid(int32_t pid)592 int32_t ClientInfo::GetUidByPid(int32_t pid)
593 {
594 std::lock_guard<std::mutex> uidLock(uidMutex_);
595 auto appThreadInfoIt = appThreadInfoMap_.find(pid);
596 if (appThreadInfoIt == appThreadInfoMap_.end()) {
597 return INVALID_UID;
598 }
599 return appThreadInfoIt->second.uid;
600 }
601
GetTokenIdByPid(int32_t pid)602 AccessTokenID ClientInfo::GetTokenIdByPid(int32_t pid)
603 {
604 std::lock_guard<std::mutex> uidLock(uidMutex_);
605 auto appThreadInfoIt = appThreadInfoMap_.find(pid);
606 if (appThreadInfoIt == appThreadInfoMap_.end()) {
607 return INVALID_UID;
608 }
609 return appThreadInfoIt->second.callerToken;
610 }
611
UpdateCmd(uint32_t sensorId,int32_t uid,int32_t cmdType)612 void ClientInfo::UpdateCmd(uint32_t sensorId, int32_t uid, int32_t cmdType)
613 {
614 std::lock_guard<std::mutex> cmdLock(cmdMutex_);
615 auto cmdIt = cmdMap_.find(sensorId);
616 if (cmdIt == cmdMap_.end()) {
617 std::unordered_map<int32_t, std::vector<int32_t>> cmds;
618 std::vector<int32_t> tmp;
619 tmp.push_back(cmdType);
620 cmds.insert(std::make_pair(uid, tmp));
621 cmdMap_.insert(std::make_pair(sensorId, cmds));
622 return;
623 }
624 auto tmpIt = cmdIt->second.find(uid);
625 if (tmpIt == cmdIt->second.end()) {
626 std::vector<int32_t> tmp;
627 tmp.push_back(cmdType);
628 cmdIt->second.insert(std::make_pair(uid, tmp));
629 return;
630 }
631 auto tmp = tmpIt->second;
632 tmp.push_back(cmdType);
633 cmdIt->second.insert(std::make_pair(uid, tmp));
634 }
635
DestroyCmd(int32_t uid)636 void ClientInfo::DestroyCmd(int32_t uid)
637 {
638 std::lock_guard<std::mutex> cmdLock(cmdMutex_);
639 cmdMap_.erase(uid);
640 }
641
GetCmdList(uint32_t sensorId,int32_t uid)642 std::vector<int32_t> ClientInfo::GetCmdList(uint32_t sensorId, int32_t uid)
643 {
644 std::lock_guard<std::mutex> cmdLock(cmdMutex_);
645 auto cmdIt = cmdMap_.find(sensorId);
646 if (cmdIt == cmdMap_.end()) {
647 return {};
648 }
649 auto uidIt = cmdIt->second.find(uid);
650 if (uidIt == cmdIt->second.end()) {
651 return {};
652 }
653 return uidIt->second;
654 }
655
UpdateDataQueue(int32_t sensorId,SensorEvent & event)656 void ClientInfo::UpdateDataQueue(int32_t sensorId, SensorEvent &event)
657 {
658 if (sensorId == HEART_RATE_SENSOR_ID) {
659 return;
660 }
661 std::lock_guard<std::mutex> queueLock(dataQueueMutex_);
662 TransferSensorEvents transferEvent = {
663 .sensorTypeId = event.sensorTypeId,
664 .version = event.version,
665 .timestamp = event.timestamp,
666 .option = event.option,
667 .mode = event.mode,
668 .dataLen = event.dataLen
669 };
670 if (memcpy_s(transferEvent.data, event.dataLen, event.data, event.dataLen) != EOK) {
671 SEN_HILOGE("Copy data failed");
672 return;
673 }
674 auto it = dumpQueue_.find(sensorId);
675 if (it == dumpQueue_.end()) {
676 std::queue<TransferSensorEvents> q;
677 q.push(transferEvent);
678 dumpQueue_.insert(std::make_pair(sensorId, q));
679 return;
680 }
681 it->second.push(transferEvent);
682 if (it->second.size() > MAX_DUMP_DATA_SIZE) {
683 it->second.pop();
684 }
685 }
686
GetDumpQueue()687 std::unordered_map<uint32_t, std::queue<TransferSensorEvents>> ClientInfo::GetDumpQueue()
688 {
689 return dumpQueue_;
690 }
691
ClearDataQueue(int32_t sensorId)692 void ClientInfo::ClearDataQueue(int32_t sensorId)
693 {
694 std::lock_guard<std::mutex> queueLock(dataQueueMutex_);
695 auto it = dumpQueue_.find(sensorId);
696 if (it != dumpQueue_.end()) {
697 dumpQueue_.erase(it);
698 }
699 }
700 } // namespace Sensors
701 } // namespace OHOS
702