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 #ifndef CLIENT_INFO_H
17 #define CLIENT_INFO_H
18 
19 #include <map>
20 #include <queue>
21 #include <set>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <vector>
25 
26 #include "refbase.h"
27 #include "singleton.h"
28 
29 #include "accesstoken_kit.h"
30 #include "iremote_object.h"
31 #include "nocopyable.h"
32 
33 #include "app_thread_info.h"
34 #include "sensor_basic_data_channel.h"
35 #include "sensor_basic_info.h"
36 #include "sensor_channel_info.h"
37 #include "sensor_data_event.h"
38 
39 namespace OHOS {
40 namespace Sensors {
41 using Security::AccessToken::AccessTokenID;
42 class ClientInfo : public Singleton<ClientInfo> {
43 public:
44     ClientInfo() = default;
45     virtual ~ClientInfo() = default;
46     bool GetSensorState(int32_t sensorId);
47     SensorBasicInfo GetBestSensorInfo(int32_t sensorId);
48     bool OnlyCurPidSensorEnabled(int32_t sensorId, int32_t pid);
49     std::vector<sptr<SensorBasicDataChannel>> GetSensorChannel(int32_t sensorId);
50     std::vector<sptr<SensorBasicDataChannel>> GetSensorChannelByUid(int32_t uid);
51     sptr<SensorBasicDataChannel> GetSensorChannelByPid(int32_t pid);
52     bool UpdateSensorInfo(int32_t sensorId, int32_t pid, const SensorBasicInfo &sensorInfo);
53     void RemoveSubscriber(int32_t sensorId, uint32_t pid);
54     bool UpdateSensorChannel(int32_t pid, const sptr<SensorBasicDataChannel> &channel);
55     bool UpdateAppThreadInfo(int32_t pid, int32_t uid, AccessTokenID callerToken);
56     void ClearSensorInfo(int32_t sensorId);
57     void ClearCurPidSensorInfo(int32_t sensorId, int32_t pid);
58     bool DestroySensorChannel(int32_t pid);
59     void DestroyAppThreadInfo(int32_t pid);
60     SensorBasicInfo GetCurPidSensorInfo(int32_t sensorId, int32_t pid);
61     uint64_t ComputeBestPeriodCount(int32_t sensorId, sptr<SensorBasicDataChannel> &channel);
62     uint64_t ComputeBestFifoCount(int32_t sensorId, sptr<SensorBasicDataChannel> &channel);
63     int32_t GetStoreEvent(int32_t sensorId, SensorData &data);
64     void StoreEvent(const SensorData &data);
65     void ClearEvent();
66     AppThreadInfo GetAppInfoByChannel(const sptr<SensorBasicDataChannel> &channel);
67     bool SaveClientPid(const sptr<IRemoteObject> &sensorClient, int32_t pid);
68     int32_t FindClientPid(const sptr<IRemoteObject> &sensorClient);
69     void DestroyClientPid(const sptr<IRemoteObject> &sensorClient);
70     std::vector<int32_t> GetSensorIdByPid(int32_t pid);
71     void GetSensorChannelInfo(std::vector<SensorChannelInfo> &channelInfo);
72     void UpdateCmd(int32_t sensorId, int32_t uid, int32_t cmdType);
73     void DestroyCmd(int32_t uid);
74     void UpdateDataQueue(int32_t sensorId, SensorData &data);
75     std::unordered_map<int32_t, std::queue<SensorData>> GetDumpQueue();
76     void ClearDataQueue(int32_t sensorId);
77     int32_t GetUidByPid(int32_t pid);
78     AccessTokenID GetTokenIdByPid(int32_t pid);
79     int32_t AddActiveInfoCBPid(int32_t pid);
80     int32_t DelActiveInfoCBPid(int32_t pid);
81     std::vector<int32_t> GetActiveInfoCBPid();
82     bool CallingService(int32_t pid);
83     int32_t GetPidByTokenId(AccessTokenID tokenId);
84     void UpdatePermState(int32_t pid, int32_t sensorId, bool state);
85     void ChangeSensorPerm(AccessTokenID tokenId, const std::string &permName, bool state);
86 
87 private:
88     DISALLOW_COPY_AND_MOVE(ClientInfo);
89     std::vector<int32_t> GetCmdList(int32_t sensorId, int32_t uid);
90     std::mutex clientMutex_;
91     std::mutex channelMutex_;
92     std::mutex eventMutex_;
93     std::mutex uidMutex_;
94     std::mutex clientPidMutex_;
95     std::mutex cmdMutex_;
96     std::mutex dataQueueMutex_;
97     std::unordered_map<int32_t, std::unordered_map<int32_t, SensorBasicInfo>> clientMap_;
98     std::unordered_map<int32_t, sptr<SensorBasicDataChannel>> channelMap_;
99     std::unordered_map<int32_t, SensorData> storedEvent_;
100     std::unordered_map<int32_t, AppThreadInfo> appThreadInfoMap_;
101     std::map<sptr<IRemoteObject>, int32_t> clientPidMap_;
102     std::unordered_map<int32_t, std::unordered_map<int32_t, std::vector<int32_t>>> cmdMap_;
103     std::unordered_map<int32_t, std::queue<SensorData>> dumpQueue_;
104     std::mutex activeInfoCBPidMutex_;
105     std::unordered_set<int32_t> activeInfoCBPidSet_;
106     static std::unordered_map<std::string, std::set<int32_t>> userGrantPermMap_;
107 };
108 } // namespace Sensors
109 } // namespace OHOS
110 #endif // CLIENT_INFO_H
111