1 /*
2  * Copyright (c) 2023 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 HDI_SENSOR_MANAGER_H
17 #define HDI_SENSOR_MANAGER_H
18 
19 #include <unordered_map>
20 #include <vector>
21 #include <set>
22 #include <hdf_remote_service.h>
23 #include "v2_0/isensor_interface.h"
24 #include "isensor_interface_vdi.h"
25 #include "sensor_client_info.h"
26 #include "sensor_trace.h"
27 
28 namespace OHOS {
29 namespace HDI {
30 namespace Sensor {
31 namespace V2_0 {
32 
33 constexpr uint32_t MAX_DUMP_DATA_SIZE = 10;
34 
35 struct SensorsDataPack {
36     int32_t count;
37     int32_t pos;
38     struct HdfSensorEvents listDumpArray[MAX_DUMP_DATA_SIZE];
39 };
40 
41 class SensorClientsManager {
42 public:
43     ~SensorClientsManager();
44     void ReportDataCbRegister(int groupId, int serviceId, const sptr<ISensorCallback> &callbackObj);
45     void ReportDataCbUnRegister(int groupId, int serviceId, const sptr<ISensorCallback> &callbackObj);
46     void SetSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval);
47     void SetSdcSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval);
48     void GetSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval);
49     void EraseSdcSensorBestConfig(int sensorId);
50     bool IsUpadateSensorState(int sensorId, int serviceId, bool isOpen);
51     static bool IsNotNeedReportData(SensorClientInfo &sensorClientInfo, const int32_t &sensorId,
52                                     const int32_t &serviceId);
53     std::string ReportEachClient(const V2_0::HdfSensorEvents& event);
54     bool GetClients(int groupId, std::unordered_map<int32_t, SensorClientInfo> &client);
55     std::set<int32_t> GetServiceIds(int32_t &sensorId);
56     bool GetBestSensorConfigMap(std::unordered_map<int32_t, struct BestSensorConfig> &map);
57     bool IsClientsEmpty(int groupId);
58     bool IsNoSensorUsed();
59     std::unordered_map<int32_t, std::set<int32_t>> GetSensorUsed();
60     bool IsNeedOpenSensor(int sensorId, int serviceId);
61     bool IsNeedCloseSensor(int sensorId, int serviceId);
62     bool IsExistSdcSensorEnable(int sensorId);
63     void OpenSensor(int sensorId, int serviceId);
64     void UpdateSensorConfig(int sensorId, int64_t samplingInterval, int64_t reportInterval);
65     void UpdateSdcSensorConfig(int sensorId, int64_t samplingInterval, int64_t reportInterval);
66     int GetServiceId(int groupId, const sptr<ISensorCallback> &callbackObj);
67     static SensorClientsManager* GetInstance();
68     std::mutex clientsMutex_;
69     std::mutex sensorUsedMutex_;
70     std::mutex sensorConfigMutex_;
71     std::mutex sdcSensorConfigMutex_;
72     std::mutex sensorInfoMutex_;
73     std::mutex sensorsDataPackMutex_;
74     void SetClientSenSorConfig(int32_t sensorId, int32_t serviceId, int64_t samplingInterval, int64_t &reportInterval);
75     static bool IsSensorContinues(int32_t sensorId);
76     void UpdateClientPeriodCount(int sensorId, int64_t samplingInterval, int64_t reportInterval);
77     void CopySensorInfo(std::vector<HdfSensorInformation> &info, bool cFlag);
78     void GetEventData(struct SensorsDataPack &dataPack);
79     void CopyEventData(const struct HdfSensorEvents event);
80 private:
81     SensorClientsManager();
82     static std::mutex instanceMutex_;
83     std::unordered_map<int32_t, std::unordered_map<int, SensorClientInfo>> clients_;
84     std::unordered_map<int32_t, std::set<int32_t>> sensorUsed_;
85     std::unordered_map<int32_t, struct BestSensorConfig> sensorConfig_;
86     std::unordered_map<int32_t, struct BestSensorConfig> sdcSensorConfig_;
87     std::vector<HdfSensorInformation> sensorInfo_;
88     SensorsDataPack listDump_ = {0};
89 };
90 
91 struct BestSensorConfig {
92     int64_t samplingInterval;
93     int64_t reportInterval;
94 };
95 
96 } // V2_0
97 } // Sensor
98 } // HDI
99 } // OHOS
100 
101 #endif // HDI_SENSOR_MANAGER_H