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