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