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 "v3_0/isensor_interface.h" 24 #include "sensor_client_info.h" 25 #include "sensor_trace.h" 26 #include "v1_0/isensor_interface_vdi.h" 27 namespace OHOS { 28 namespace HDI { 29 namespace Sensor { 30 namespace V3_0 { 31 32 constexpr uint32_t MAX_DUMP_DATA_SIZE = 10; 33 34 struct SensorsDataPack { 35 int32_t count; 36 int32_t pos; 37 struct HdfSensorEvents listDumpArray[MAX_DUMP_DATA_SIZE]; 38 }; 39 40 class SensorClientsManager { 41 public: 42 ~SensorClientsManager(); 43 void ReportDataCbRegister(int groupId, int serviceId, const sptr<V3_0::ISensorCallback> &callbackObj, bool oneway); 44 void ReportDataCbUnRegister(int groupId, int serviceId, const sptr<V3_0::ISensorCallback> &callbackObj); 45 void ReportDataCbOneWay(int groupId, int serviceId); 46 void SetSensorBestConfig(SensorHandle sensorHandle, int64_t &samplingInterval, int64_t &reportInterval); 47 void SetSdcSensorBestConfig(SensorHandle sensorHandle, int64_t &samplingInterval, int64_t &reportInterval); 48 void GetSensorBestConfig(SensorHandle sensorHandle, int64_t &samplingInterval, int64_t &reportInterval); 49 int64_t GetSensorBestSamplingInterval(SensorHandle sensorHandle); 50 void EraseSdcSensorBestConfig(SensorHandle sensorHandle); 51 void EraseSensorBestConfig(SensorHandle sensorHandle); 52 bool IsUpadateSensorState(SensorHandle sensorHandle, int serviceId, bool isOpen); 53 static bool IsNotNeedReportData(SensorClientInfo &sensorClientInfo, const SensorHandle sensorHandle, 54 const int32_t &serviceId); 55 std::string ReportEachClient(const V3_0::HdfSensorEvents& event); 56 bool GetClients(int groupId, std::unordered_map<int32_t, SensorClientInfo> &client); 57 std::set<int32_t> GetServiceIds(SensorHandle sensorHandle); 58 bool GetBestSensorConfigMap(std::unordered_map<SensorHandle, struct BestSensorConfig> &map); 59 bool IsClientsEmpty(int groupId); 60 bool IsNoSensorUsed(); 61 std::unordered_map<SensorHandle, std::set<int32_t>> GetSensorUsed(); 62 bool IsNeedOpenSensor(SensorHandle sensorHandle, int serviceId); 63 bool IsNeedCloseSensor(SensorHandle sensorHandle, int serviceId); 64 bool IsExistSdcSensorEnable(SensorHandle sensorHandle); 65 void OpenSensor(SensorHandle sensorHandle, int serviceId); 66 void UpdateSensorConfig(SensorHandle sensorHandle, int64_t samplingInterval, int64_t reportInterval); 67 void UpdateSdcSensorConfig(SensorHandle sensorHandle, int64_t samplingInterval, int64_t reportInterval); 68 int GetServiceId(int groupId, const sptr<IRemoteObject> &iRemoteObject); 69 static SensorClientsManager* GetInstance(); 70 std::mutex clientsMutex_; 71 std::mutex sensorUsedMutex_; 72 std::mutex sensorConfigMutex_; 73 std::mutex sdcSensorConfigMutex_; 74 std::mutex sensorInfoMutex_; 75 std::mutex sensorsDataPackMutex_; 76 struct SensorInfoId { 77 SensorHandle sensorHandle = {}; 78 int32_t serviceId = 0; 79 bool oneway; 80 int32_t callbackVersion; 81 }; 82 void SetClientSenSorConfig(SensorHandle sensorHandle, int32_t serviceId, int64_t samplingInterval, 83 int64_t &reportInterval); 84 static bool IsSensorContinues(SensorHandle sensorHandle); 85 void UpdateClientPeriodCount(SensorHandle sensorHandle, int64_t samplingInterval, int64_t reportInterval); 86 void CopySensorInfo(std::vector<V3_0::HdfSensorInformation> &info, bool cFlag); 87 void GetEventData(struct SensorsDataPack &dataPack); 88 void CopyEventData(const struct HdfSensorEvents event); 89 void ReSetSensorPrintTime(SensorHandle sensorHandle); 90 bool IsSensorNeedPrint(SensorHandle sensorHandle); 91 void HdiReportData(const sptr<V3_0::ISensorCallback> &callbackObj, const V3_0::HdfSensorEvents& event, 92 std::string &result, SensorInfoId sensorInfoId); 93 static std::unordered_map<SensorHandle, std::unordered_map<int32_t, int64_t>> sensorReportCountMap; 94 private: 95 SensorClientsManager(); 96 static std::mutex instanceMutex_; 97 std::unordered_map<int32_t, std::unordered_map<int, SensorClientInfo>> clients_; 98 std::unordered_map<SensorHandle, std::set<int32_t>> sensorUsed_; 99 std::unordered_map<SensorHandle, struct BestSensorConfig> sensorConfig_; 100 std::unordered_map<SensorHandle, struct BestSensorConfig> sdcSensorConfig_; 101 std::unordered_map<SensorHandle, int32_t> sensorPrintTimes_; 102 std::mutex sensorPrintTimesMutex_; 103 std::vector<V3_0::HdfSensorInformation> sensorInfo_; 104 SensorsDataPack listDump_ = {0}; 105 }; 106 107 struct BestSensorConfig { 108 int64_t samplingInterval; 109 int64_t reportInterval; 110 }; 111 112 } // V3_0 113 } // Sensor 114 } // HDI 115 } // OHOS 116 117 #endif // HDI_SENSOR_MANAGER_H