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