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