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