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_1 { 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<V2_0::ISensorCallback> &callbackObj); 45 void ReportDataCbUnRegister(int groupId, int serviceId, const sptr<V2_0::ISensorCallback> &callbackObj); 46 void ReportDataCbOneWay(int groupId, int serviceId); 47 void SetSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval); 48 void SetSdcSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval); 49 void GetSensorBestConfig(int sensorId, int64_t &samplingInterval, int64_t &reportInterval); 50 void EraseSdcSensorBestConfig(int sensorId); 51 bool IsUpadateSensorState(int sensorId, int serviceId, bool isOpen); 52 static bool IsNotNeedReportData(SensorClientInfo &sensorClientInfo, const int32_t &sensorId, 53 const int32_t &serviceId); 54 std::string ReportEachClient(const V2_0::HdfSensorEvents& event); 55 bool GetClients(int groupId, std::unordered_map<int32_t, SensorClientInfo> &client); 56 std::set<int32_t> GetServiceIds(int32_t &sensorId); 57 bool GetBestSensorConfigMap(std::unordered_map<int32_t, struct BestSensorConfig> &map); 58 bool IsClientsEmpty(int groupId); 59 bool IsNoSensorUsed(); 60 std::unordered_map<int32_t, std::set<int32_t>> GetSensorUsed(); 61 bool IsNeedOpenSensor(int sensorId, int serviceId); 62 bool IsNeedCloseSensor(int sensorId, int serviceId); 63 bool IsExistSdcSensorEnable(int sensorId); 64 void OpenSensor(int sensorId, int serviceId); 65 void UpdateSensorConfig(int sensorId, int64_t samplingInterval, int64_t reportInterval); 66 void UpdateSdcSensorConfig(int sensorId, int64_t samplingInterval, int64_t reportInterval); 67 int GetServiceId(int groupId, const sptr<V2_0::ISensorCallback> &callbackObj); 68 static SensorClientsManager* GetInstance(); 69 std::mutex clientsMutex_; 70 std::mutex sensorUsedMutex_; 71 std::mutex sensorConfigMutex_; 72 std::mutex sdcSensorConfigMutex_; 73 std::mutex sensorInfoMutex_; 74 std::mutex sensorsDataPackMutex_; 75 struct SensorInfoId { 76 int32_t sensorId = 0; 77 int32_t serviceId = 0; 78 }; 79 void SetClientSenSorConfig(int32_t sensorId, int32_t serviceId, int64_t samplingInterval, int64_t &reportInterval); 80 static bool IsSensorContinues(int32_t sensorId); 81 void UpdateClientPeriodCount(int sensorId, int64_t samplingInterval, int64_t reportInterval); 82 void CopySensorInfo(std::vector<HdfSensorInformation> &info, bool cFlag); 83 void GetEventData(struct SensorsDataPack &dataPack); 84 void CopyEventData(const struct HdfSensorEvents event); 85 void ReSetSensorPrintTime(int32_t sensorId); 86 bool IsSensorNeedPrint(int32_t sensorId); 87 void HdiReportData(const sptr<V2_0::ISensorCallback> &callbackObj, const V2_0::HdfSensorEvents& event, 88 std::string &result, const bool &oneway, SensorInfoId sensorInfoId); 89 private: 90 SensorClientsManager(); 91 static std::mutex instanceMutex_; 92 std::unordered_map<int32_t, std::unordered_map<int, SensorClientInfo>> clients_; 93 std::unordered_map<int32_t, std::set<int32_t>> sensorUsed_; 94 std::unordered_map<int32_t, struct BestSensorConfig> sensorConfig_; 95 std::unordered_map<int32_t, struct BestSensorConfig> sdcSensorConfig_; 96 std::unordered_map<int32_t, int32_t> sensorPrintTimes_; 97 std::mutex sensorPrintTimesMutex_; 98 std::vector<HdfSensorInformation> sensorInfo_; 99 SensorsDataPack listDump_ = {0}; 100 }; 101 102 struct BestSensorConfig { 103 int64_t samplingInterval; 104 int64_t reportInterval; 105 }; 106 107 } // V2_1 108 } // Sensor 109 } // HDI 110 } // OHOS 111 112 #endif // HDI_SENSOR_MANAGER_H