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