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