1 /* 2 * Copyright (c) 2021 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 SENSOR_MANAGER_H 17 #define SENSOR_MANAGER_H 18 19 #include <thread> 20 21 #ifdef HDF_DRIVERS_INTERFACE_SENSOR 22 #include "sensor_data_processer.h" 23 #include "sensor_hdi_connection.h" 24 #else 25 #include "sensor.h" 26 #endif // HDF_DRIVERS_INTERFACE_SENSOR 27 28 namespace OHOS { 29 namespace Sensors { 30 using namespace Security::AccessToken; 31 class SensorManager : public Singleton<SensorManager> { 32 public: 33 #ifdef HDF_DRIVERS_INTERFACE_SENSOR 34 void InitSensorMap(const std::unordered_map<int32_t, Sensor> &sensorMap, sptr<SensorDataProcesser> dataProcesser, 35 sptr<ReportDataCallback> dataCallback); 36 bool SetBestSensorParams(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); 37 bool ResetBestSensorParams(int32_t sensorId); 38 void StartDataReportThread(); 39 #else 40 void InitSensorMap(const std::unordered_map<int32_t, Sensor> &sensorMap); 41 #endif // HDF_DRIVERS_INTERFACE_SENSOR 42 bool SaveSubscriber(int32_t sensorId, uint32_t pid, int64_t samplingPeriodNs, int64_t maxReportDelayNs); 43 SensorBasicInfo GetSensorInfo(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); 44 bool IsOtherClientUsingSensor(int32_t sensorId, int32_t clientPid); 45 ErrCode AfterDisableSensor(int32_t sensorId); 46 void GetPackageName(AccessTokenID tokenId, std::string &packageName, bool isAccessTokenServiceActive = false); 47 48 private: 49 #ifdef HDF_DRIVERS_INTERFACE_SENSOR 50 SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); 51 std::thread dataThread_; 52 sptr<SensorDataProcesser> sensorDataProcesser_ = nullptr; 53 sptr<ReportDataCallback> reportDataCallback_ = nullptr; 54 #endif // HDF_DRIVERS_INTERFACE_SENSOR 55 ClientInfo &clientInfo_ = ClientInfo::GetInstance(); 56 std::unordered_map<int32_t, Sensor> sensorMap_; 57 std::mutex sensorMapMutex_; 58 }; 59 } // namespace Sensors 60 } // namespace OHOS 61 #endif // SENSOR_MANAGER_H 62