• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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     bool GetSensorState(uint32_t sensorId);
45     SensorBasicInfo GetBestSensorInfo(uint32_t sensorId);
46     bool OnlyCurPidSensorEnabled(uint32_t sensorId, int32_t pid);
47     std::vector<sptr<SensorBasicDataChannel>> GetSensorChannel(uint32_t sensorId);
48     std::vector<sptr<SensorBasicDataChannel>> GetSensorChannelByUid(int32_t uid);
49     sptr<SensorBasicDataChannel> GetSensorChannelByPid(int32_t pid);
50     bool UpdateSensorInfo(uint32_t sensorId, int32_t pid, const SensorBasicInfo &sensorInfo);
51     void RemoveSubscriber(uint32_t sensorId, uint32_t pid);
52     bool UpdateSensorChannel(int32_t pid, const sptr<SensorBasicDataChannel> &channel);
53     bool UpdateAppThreadInfo(int32_t pid, int32_t uid, AccessTokenID callerToken);
54     void ClearSensorInfo(uint32_t sensorId);
55     void ClearCurPidSensorInfo(uint32_t sensorId, int32_t pid);
56     bool DestroySensorChannel(int32_t pid);
57     void DestroyAppThreadInfo(int32_t pid);
58     SensorBasicInfo GetCurPidSensorInfo(uint32_t sensorId, int32_t pid);
59     uint64_t ComputeBestPeriodCount(uint32_t sensorId, sptr<SensorBasicDataChannel> &channel);
60     uint64_t ComputeBestFifoCount(uint32_t sensorId, sptr<SensorBasicDataChannel> &channel);
61     int32_t GetStoreEvent(int32_t sensorId, SensorEvent &event);
62     void StoreEvent(const SensorEvent &event);
63     void ClearEvent();
64     AppThreadInfo GetAppInfoByChannel(const sptr<SensorBasicDataChannel> &channel);
65     bool SaveClientPid(const sptr<IRemoteObject> &sensorClient, int32_t pid);
66     int32_t FindClientPid(const sptr<IRemoteObject> &sensorClient);
67     void DestroyClientPid(const sptr<IRemoteObject> &sensorClient);
68     std::vector<uint32_t> GetSensorIdByPid(int32_t pid);
69     void GetSensorChannelInfo(std::vector<SensorChannelInfo> &channelInfo);
70     void UpdateCmd(uint32_t sensorId, int32_t uid, int32_t cmdType);
71     void DestroyCmd(int32_t uid);
72     void UpdateDataQueue(int32_t sensorId, SensorEvent &event);
73     std::unordered_map<uint32_t, std::queue<TransferSensorEvents>> GetDumpQueue();
74     void ClearDataQueue(int32_t sensorId);
75     int32_t GetUidByPid(int32_t pid);
76     AccessTokenID GetTokenIdByPid(int32_t pid);
77 
78 private:
79     DISALLOW_COPY_AND_MOVE(ClientInfo);
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, 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<TransferSensorEvents>> dumpQueue_;
95 };
96 }  // namespace Sensors
97 }  // namespace OHOS
98 #endif  // CLIENT_INFO_H
99