1/* 2 * Copyright (c) 2021-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 SENSOR_SERVICE_H 17#define SENSOR_SERVICE_H 18 19#include <mutex> 20#include <thread> 21#include <unordered_map> 22 23#include "nocopyable.h" 24#include "system_ability.h" 25 26#include "client_info.h" 27#include "death_recipient_template.h" 28#include "sensor_data_event.h" 29#include "sensor_delayed_sp_singleton.h" 30#include "sensor_manager.h" 31#include "sensor_power_policy.h" 32#include "sensor_service_stub.h" 33#include "stream_server.h" 34#ifdef HDF_DRIVERS_INTERFACE_SENSOR 35#include "sensor_hdi_connection.h" 36#endif // HDF_DRIVERS_INTERFACE_SENSOR 37 38namespace OHOS { 39namespace Sensors { 40enum class SensorServiceState { 41 STATE_STOPPED, 42 STATE_RUNNING, 43}; 44 45class SensorService : public SystemAbility, public StreamServer, public SensorServiceStub { 46 DECLARE_SYSTEM_ABILITY(SensorService) 47 SENSOR_DECLARE_DELAYED_SP_SINGLETON(SensorService); 48 49public: 50 void OnDump() override; 51 void OnStart() override; 52 void OnStop() override; 53 int Dump(int fd, const std::vector<std::u16string> &args) override; 54 ErrCode EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; 55 ErrCode DisableSensor(int32_t sensorId) override; 56 std::vector<Sensor> GetSensorList() override; 57 ErrCode TransferDataChannel(const sptr<SensorBasicDataChannel> &sensorBasicDataChannel, 58 const sptr<IRemoteObject> &sensorClient) override; 59 ErrCode DestroySensorChannel(sptr<IRemoteObject> sensorClient) override; 60 void ProcessDeathObserver(const wptr<IRemoteObject> &object); 61 ErrCode SuspendSensors(int32_t pid) override; 62 ErrCode ResumeSensors(int32_t pid) override; 63 ErrCode GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList) override; 64 ErrCode CreateSocketChannel(sptr<IRemoteObject> sensorClient, int32_t &clientFd) override; 65 ErrCode DestroySocketChannel(sptr<IRemoteObject> sensorClient) override; 66 ErrCode EnableActiveInfoCB() override; 67 ErrCode DisableActiveInfoCB() override; 68 ErrCode ResetSensors() override; 69 70private: 71 DISALLOW_COPY_AND_MOVE(SensorService); 72 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 73 bool CheckParameter(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); 74 75 class PermStateChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize { 76 public: 77 PermStateChangeCb(const Security::AccessToken::PermStateChangeScope &scope, 78 sptr<SensorService> server) : PermStateChangeCallbackCustomize(scope), server_(server) {} 79 void PermStateChangeCallback(PermStateChangeInfo &result) override; 80 81 private: 82 sptr<SensorService> server_ = nullptr; 83 }; 84 85 void RegisterClientDeathRecipient(sptr<IRemoteObject> sensorClient, int32_t pid); 86 void UnregisterClientDeathRecipient(sptr<IRemoteObject> sensorClient); 87 bool InitSensorPolicy(); 88 void ReportOnChangeData(int32_t sensorId); 89 void ReportSensorSysEvent(int32_t sensorId, bool enable, int32_t pid); 90 ErrCode DisableSensor(int32_t sensorId, int32_t pid); 91 bool RegisterPermCallback(int32_t sensorId); 92 void UnregisterPermCallback(); 93 void ReportActiveInfo(int32_t sensorId, int32_t pid); 94 bool CheckSensorId(int32_t sensorId); 95 SensorServiceState state_; 96 std::mutex serviceLock_; 97 std::mutex sensorsMutex_; 98 std::mutex sensorMapMutex_; 99 std::vector<Sensor> sensors_; 100 std::unordered_map<int32_t, Sensor> sensorMap_; 101#ifdef HDF_DRIVERS_INTERFACE_SENSOR 102 bool InitInterface(); 103 bool InitDataCallback(); 104 bool InitSensorList(); 105 SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); 106 sptr<SensorDataProcesser> sensorDataProcesser_ = nullptr; 107 sptr<ReportDataCallback> reportDataCallback_ = nullptr; 108#endif // HDF_DRIVERS_INTERFACE_SENSOR 109 ClientInfo &clientInfo_ = ClientInfo::GetInstance(); 110 SensorManager &sensorManager_ = SensorManager::GetInstance(); 111 std::mutex uidLock_; 112 // death recipient of sensor client 113 std::mutex clientDeathObserverMutex_; 114 sptr<IRemoteObject::DeathRecipient> clientDeathObserver_ = nullptr; 115 std::shared_ptr<PermStateChangeCb> permStateChangeCb_ = nullptr; 116 ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); 117 std::atomic_bool isReportActiveInfo_ = false; 118}; 119 120#define POWER_POLICY SensorPowerPolicy::GetInstance() 121} // namespace Sensors 122} // namespace OHOS 123#endif // SENSOR_SERVICE_H 124