1bae4d13cSopenharmony_ci/*
2bae4d13cSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3bae4d13cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bae4d13cSopenharmony_ci * you may not use this file except in compliance with the License.
5bae4d13cSopenharmony_ci * You may obtain a copy of the License at
6bae4d13cSopenharmony_ci *
7bae4d13cSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bae4d13cSopenharmony_ci *
9bae4d13cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bae4d13cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bae4d13cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bae4d13cSopenharmony_ci * See the License for the specific language governing permissions and
13bae4d13cSopenharmony_ci * limitations under the License.
14bae4d13cSopenharmony_ci */
15bae4d13cSopenharmony_ci
16bae4d13cSopenharmony_ci#ifndef CLIENT_INFO_H
17bae4d13cSopenharmony_ci#define CLIENT_INFO_H
18bae4d13cSopenharmony_ci
19bae4d13cSopenharmony_ci#include <map>
20bae4d13cSopenharmony_ci#include <queue>
21bae4d13cSopenharmony_ci#include <set>
22bae4d13cSopenharmony_ci#include <unordered_map>
23bae4d13cSopenharmony_ci#include <unordered_set>
24bae4d13cSopenharmony_ci#include <vector>
25bae4d13cSopenharmony_ci
26bae4d13cSopenharmony_ci#include "refbase.h"
27bae4d13cSopenharmony_ci#include "singleton.h"
28bae4d13cSopenharmony_ci
29bae4d13cSopenharmony_ci#include "accesstoken_kit.h"
30bae4d13cSopenharmony_ci#include "iremote_object.h"
31bae4d13cSopenharmony_ci#include "nocopyable.h"
32bae4d13cSopenharmony_ci
33bae4d13cSopenharmony_ci#include "app_thread_info.h"
34bae4d13cSopenharmony_ci#include "sensor_basic_data_channel.h"
35bae4d13cSopenharmony_ci#include "sensor_basic_info.h"
36bae4d13cSopenharmony_ci#include "sensor_channel_info.h"
37bae4d13cSopenharmony_ci#include "sensor_data_event.h"
38bae4d13cSopenharmony_ci
39bae4d13cSopenharmony_cinamespace OHOS {
40bae4d13cSopenharmony_cinamespace Sensors {
41bae4d13cSopenharmony_ciusing Security::AccessToken::AccessTokenID;
42bae4d13cSopenharmony_ciclass ClientInfo : public Singleton<ClientInfo> {
43bae4d13cSopenharmony_cipublic:
44bae4d13cSopenharmony_ci    ClientInfo() = default;
45bae4d13cSopenharmony_ci    virtual ~ClientInfo() = default;
46bae4d13cSopenharmony_ci    bool GetSensorState(int32_t sensorId);
47bae4d13cSopenharmony_ci    SensorBasicInfo GetBestSensorInfo(int32_t sensorId);
48bae4d13cSopenharmony_ci    bool OnlyCurPidSensorEnabled(int32_t sensorId, int32_t pid);
49bae4d13cSopenharmony_ci    std::vector<sptr<SensorBasicDataChannel>> GetSensorChannel(int32_t sensorId);
50bae4d13cSopenharmony_ci    std::vector<sptr<SensorBasicDataChannel>> GetSensorChannelByUid(int32_t uid);
51bae4d13cSopenharmony_ci    sptr<SensorBasicDataChannel> GetSensorChannelByPid(int32_t pid);
52bae4d13cSopenharmony_ci    bool UpdateSensorInfo(int32_t sensorId, int32_t pid, const SensorBasicInfo &sensorInfo);
53bae4d13cSopenharmony_ci    void RemoveSubscriber(int32_t sensorId, uint32_t pid);
54bae4d13cSopenharmony_ci    bool UpdateSensorChannel(int32_t pid, const sptr<SensorBasicDataChannel> &channel);
55bae4d13cSopenharmony_ci    bool UpdateAppThreadInfo(int32_t pid, int32_t uid, AccessTokenID callerToken);
56bae4d13cSopenharmony_ci    void ClearSensorInfo(int32_t sensorId);
57bae4d13cSopenharmony_ci    void ClearCurPidSensorInfo(int32_t sensorId, int32_t pid);
58bae4d13cSopenharmony_ci    bool DestroySensorChannel(int32_t pid);
59bae4d13cSopenharmony_ci    void DestroyAppThreadInfo(int32_t pid);
60bae4d13cSopenharmony_ci    SensorBasicInfo GetCurPidSensorInfo(int32_t sensorId, int32_t pid);
61bae4d13cSopenharmony_ci    uint64_t ComputeBestPeriodCount(int32_t sensorId, sptr<SensorBasicDataChannel> &channel);
62bae4d13cSopenharmony_ci    uint64_t ComputeBestFifoCount(int32_t sensorId, sptr<SensorBasicDataChannel> &channel);
63bae4d13cSopenharmony_ci    int32_t GetStoreEvent(int32_t sensorId, SensorData &data);
64bae4d13cSopenharmony_ci    void StoreEvent(const SensorData &data);
65bae4d13cSopenharmony_ci    void ClearEvent();
66bae4d13cSopenharmony_ci    AppThreadInfo GetAppInfoByChannel(const sptr<SensorBasicDataChannel> &channel);
67bae4d13cSopenharmony_ci    bool SaveClientPid(const sptr<IRemoteObject> &sensorClient, int32_t pid);
68bae4d13cSopenharmony_ci    int32_t FindClientPid(const sptr<IRemoteObject> &sensorClient);
69bae4d13cSopenharmony_ci    void DestroyClientPid(const sptr<IRemoteObject> &sensorClient);
70bae4d13cSopenharmony_ci    std::vector<int32_t> GetSensorIdByPid(int32_t pid);
71bae4d13cSopenharmony_ci    void GetSensorChannelInfo(std::vector<SensorChannelInfo> &channelInfo);
72bae4d13cSopenharmony_ci    void UpdateCmd(int32_t sensorId, int32_t uid, int32_t cmdType);
73bae4d13cSopenharmony_ci    void DestroyCmd(int32_t uid);
74bae4d13cSopenharmony_ci    void UpdateDataQueue(int32_t sensorId, SensorData &data);
75bae4d13cSopenharmony_ci    std::unordered_map<int32_t, std::queue<SensorData>> GetDumpQueue();
76bae4d13cSopenharmony_ci    void ClearDataQueue(int32_t sensorId);
77bae4d13cSopenharmony_ci    int32_t GetUidByPid(int32_t pid);
78bae4d13cSopenharmony_ci    AccessTokenID GetTokenIdByPid(int32_t pid);
79bae4d13cSopenharmony_ci    int32_t AddActiveInfoCBPid(int32_t pid);
80bae4d13cSopenharmony_ci    int32_t DelActiveInfoCBPid(int32_t pid);
81bae4d13cSopenharmony_ci    std::vector<int32_t> GetActiveInfoCBPid();
82bae4d13cSopenharmony_ci    bool CallingService(int32_t pid);
83bae4d13cSopenharmony_ci    int32_t GetPidByTokenId(AccessTokenID tokenId);
84bae4d13cSopenharmony_ci    void UpdatePermState(int32_t pid, int32_t sensorId, bool state);
85bae4d13cSopenharmony_ci    void ChangeSensorPerm(AccessTokenID tokenId, const std::string &permName, bool state);
86bae4d13cSopenharmony_ci
87bae4d13cSopenharmony_ciprivate:
88bae4d13cSopenharmony_ci    DISALLOW_COPY_AND_MOVE(ClientInfo);
89bae4d13cSopenharmony_ci    std::vector<int32_t> GetCmdList(int32_t sensorId, int32_t uid);
90bae4d13cSopenharmony_ci    std::mutex clientMutex_;
91bae4d13cSopenharmony_ci    std::mutex channelMutex_;
92bae4d13cSopenharmony_ci    std::mutex eventMutex_;
93bae4d13cSopenharmony_ci    std::mutex uidMutex_;
94bae4d13cSopenharmony_ci    std::mutex clientPidMutex_;
95bae4d13cSopenharmony_ci    std::mutex cmdMutex_;
96bae4d13cSopenharmony_ci    std::mutex dataQueueMutex_;
97bae4d13cSopenharmony_ci    std::unordered_map<int32_t, std::unordered_map<int32_t, SensorBasicInfo>> clientMap_;
98bae4d13cSopenharmony_ci    std::unordered_map<int32_t, sptr<SensorBasicDataChannel>> channelMap_;
99bae4d13cSopenharmony_ci    std::unordered_map<int32_t, SensorData> storedEvent_;
100bae4d13cSopenharmony_ci    std::unordered_map<int32_t, AppThreadInfo> appThreadInfoMap_;
101bae4d13cSopenharmony_ci    std::map<sptr<IRemoteObject>, int32_t> clientPidMap_;
102bae4d13cSopenharmony_ci    std::unordered_map<int32_t, std::unordered_map<int32_t, std::vector<int32_t>>> cmdMap_;
103bae4d13cSopenharmony_ci    std::unordered_map<int32_t, std::queue<SensorData>> dumpQueue_;
104bae4d13cSopenharmony_ci    std::mutex activeInfoCBPidMutex_;
105bae4d13cSopenharmony_ci    std::unordered_set<int32_t> activeInfoCBPidSet_;
106bae4d13cSopenharmony_ci    static std::unordered_map<std::string, std::set<int32_t>> userGrantPermMap_;
107bae4d13cSopenharmony_ci};
108bae4d13cSopenharmony_ci} // namespace Sensors
109bae4d13cSopenharmony_ci} // namespace OHOS
110bae4d13cSopenharmony_ci#endif // CLIENT_INFO_H
111