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