1bae4d13cSopenharmony_ci/* 2bae4d13cSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3bae4d13cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bae4d13cSopenharmony_ci * you may not use this file except in compliance with the License. 5bae4d13cSopenharmony_ci * You may obtain a copy of the License at 6bae4d13cSopenharmony_ci * 7bae4d13cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bae4d13cSopenharmony_ci * 9bae4d13cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bae4d13cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bae4d13cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bae4d13cSopenharmony_ci * See the License for the specific language governing permissions and 13bae4d13cSopenharmony_ci * limitations under the License. 14bae4d13cSopenharmony_ci */ 15bae4d13cSopenharmony_ci 16bae4d13cSopenharmony_ci#ifndef SENSOR_SERVICE_H 17bae4d13cSopenharmony_ci#define SENSOR_SERVICE_H 18bae4d13cSopenharmony_ci 19bae4d13cSopenharmony_ci#include <mutex> 20bae4d13cSopenharmony_ci#include <thread> 21bae4d13cSopenharmony_ci#include <unordered_map> 22bae4d13cSopenharmony_ci 23bae4d13cSopenharmony_ci#include "nocopyable.h" 24bae4d13cSopenharmony_ci#include "system_ability.h" 25bae4d13cSopenharmony_ci 26bae4d13cSopenharmony_ci#include "client_info.h" 27bae4d13cSopenharmony_ci#include "death_recipient_template.h" 28bae4d13cSopenharmony_ci#include "sensor_data_event.h" 29bae4d13cSopenharmony_ci#include "sensor_delayed_sp_singleton.h" 30bae4d13cSopenharmony_ci#include "sensor_manager.h" 31bae4d13cSopenharmony_ci#include "sensor_power_policy.h" 32bae4d13cSopenharmony_ci#include "sensor_service_stub.h" 33bae4d13cSopenharmony_ci#include "stream_server.h" 34bae4d13cSopenharmony_ci#ifdef HDF_DRIVERS_INTERFACE_SENSOR 35bae4d13cSopenharmony_ci#include "sensor_hdi_connection.h" 36bae4d13cSopenharmony_ci#endif // HDF_DRIVERS_INTERFACE_SENSOR 37bae4d13cSopenharmony_ci 38bae4d13cSopenharmony_cinamespace OHOS { 39bae4d13cSopenharmony_cinamespace Sensors { 40bae4d13cSopenharmony_cienum class SensorServiceState { 41bae4d13cSopenharmony_ci STATE_STOPPED, 42bae4d13cSopenharmony_ci STATE_RUNNING, 43bae4d13cSopenharmony_ci}; 44bae4d13cSopenharmony_ci 45bae4d13cSopenharmony_ciclass SensorService : public SystemAbility, public StreamServer, public SensorServiceStub { 46bae4d13cSopenharmony_ci DECLARE_SYSTEM_ABILITY(SensorService) 47bae4d13cSopenharmony_ci SENSOR_DECLARE_DELAYED_SP_SINGLETON(SensorService); 48bae4d13cSopenharmony_ci 49bae4d13cSopenharmony_cipublic: 50bae4d13cSopenharmony_ci void OnDump() override; 51bae4d13cSopenharmony_ci void OnStart() override; 52bae4d13cSopenharmony_ci void OnStop() override; 53bae4d13cSopenharmony_ci int Dump(int fd, const std::vector<std::u16string> &args) override; 54bae4d13cSopenharmony_ci ErrCode EnableSensor(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; 55bae4d13cSopenharmony_ci ErrCode DisableSensor(int32_t sensorId) override; 56bae4d13cSopenharmony_ci std::vector<Sensor> GetSensorList() override; 57bae4d13cSopenharmony_ci ErrCode TransferDataChannel(const sptr<SensorBasicDataChannel> &sensorBasicDataChannel, 58bae4d13cSopenharmony_ci const sptr<IRemoteObject> &sensorClient) override; 59bae4d13cSopenharmony_ci ErrCode DestroySensorChannel(sptr<IRemoteObject> sensorClient) override; 60bae4d13cSopenharmony_ci void ProcessDeathObserver(const wptr<IRemoteObject> &object); 61bae4d13cSopenharmony_ci ErrCode SuspendSensors(int32_t pid) override; 62bae4d13cSopenharmony_ci ErrCode ResumeSensors(int32_t pid) override; 63bae4d13cSopenharmony_ci ErrCode GetActiveInfoList(int32_t pid, std::vector<ActiveInfo> &activeInfoList) override; 64bae4d13cSopenharmony_ci ErrCode CreateSocketChannel(sptr<IRemoteObject> sensorClient, int32_t &clientFd) override; 65bae4d13cSopenharmony_ci ErrCode DestroySocketChannel(sptr<IRemoteObject> sensorClient) override; 66bae4d13cSopenharmony_ci ErrCode EnableActiveInfoCB() override; 67bae4d13cSopenharmony_ci ErrCode DisableActiveInfoCB() override; 68bae4d13cSopenharmony_ci ErrCode ResetSensors() override; 69bae4d13cSopenharmony_ci 70bae4d13cSopenharmony_ciprivate: 71bae4d13cSopenharmony_ci DISALLOW_COPY_AND_MOVE(SensorService); 72bae4d13cSopenharmony_ci void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 73bae4d13cSopenharmony_ci bool CheckParameter(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); 74bae4d13cSopenharmony_ci 75bae4d13cSopenharmony_ci class PermStateChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize { 76bae4d13cSopenharmony_ci public: 77bae4d13cSopenharmony_ci PermStateChangeCb(const Security::AccessToken::PermStateChangeScope &scope, 78bae4d13cSopenharmony_ci sptr<SensorService> server) : PermStateChangeCallbackCustomize(scope), server_(server) {} 79bae4d13cSopenharmony_ci void PermStateChangeCallback(PermStateChangeInfo &result) override; 80bae4d13cSopenharmony_ci 81bae4d13cSopenharmony_ci private: 82bae4d13cSopenharmony_ci sptr<SensorService> server_ = nullptr; 83bae4d13cSopenharmony_ci }; 84bae4d13cSopenharmony_ci 85bae4d13cSopenharmony_ci void RegisterClientDeathRecipient(sptr<IRemoteObject> sensorClient, int32_t pid); 86bae4d13cSopenharmony_ci void UnregisterClientDeathRecipient(sptr<IRemoteObject> sensorClient); 87bae4d13cSopenharmony_ci bool InitSensorPolicy(); 88bae4d13cSopenharmony_ci void ReportOnChangeData(int32_t sensorId); 89bae4d13cSopenharmony_ci void ReportSensorSysEvent(int32_t sensorId, bool enable, int32_t pid); 90bae4d13cSopenharmony_ci ErrCode DisableSensor(int32_t sensorId, int32_t pid); 91bae4d13cSopenharmony_ci bool RegisterPermCallback(int32_t sensorId); 92bae4d13cSopenharmony_ci void UnregisterPermCallback(); 93bae4d13cSopenharmony_ci void ReportActiveInfo(int32_t sensorId, int32_t pid); 94bae4d13cSopenharmony_ci bool CheckSensorId(int32_t sensorId); 95bae4d13cSopenharmony_ci SensorServiceState state_; 96bae4d13cSopenharmony_ci std::mutex serviceLock_; 97bae4d13cSopenharmony_ci std::mutex sensorsMutex_; 98bae4d13cSopenharmony_ci std::mutex sensorMapMutex_; 99bae4d13cSopenharmony_ci std::vector<Sensor> sensors_; 100bae4d13cSopenharmony_ci std::unordered_map<int32_t, Sensor> sensorMap_; 101bae4d13cSopenharmony_ci#ifdef HDF_DRIVERS_INTERFACE_SENSOR 102bae4d13cSopenharmony_ci bool InitInterface(); 103bae4d13cSopenharmony_ci bool InitDataCallback(); 104bae4d13cSopenharmony_ci bool InitSensorList(); 105bae4d13cSopenharmony_ci SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); 106bae4d13cSopenharmony_ci sptr<SensorDataProcesser> sensorDataProcesser_ = nullptr; 107bae4d13cSopenharmony_ci sptr<ReportDataCallback> reportDataCallback_ = nullptr; 108bae4d13cSopenharmony_ci#endif // HDF_DRIVERS_INTERFACE_SENSOR 109bae4d13cSopenharmony_ci ClientInfo &clientInfo_ = ClientInfo::GetInstance(); 110bae4d13cSopenharmony_ci SensorManager &sensorManager_ = SensorManager::GetInstance(); 111bae4d13cSopenharmony_ci std::mutex uidLock_; 112bae4d13cSopenharmony_ci // death recipient of sensor client 113bae4d13cSopenharmony_ci std::mutex clientDeathObserverMutex_; 114bae4d13cSopenharmony_ci sptr<IRemoteObject::DeathRecipient> clientDeathObserver_ = nullptr; 115bae4d13cSopenharmony_ci std::shared_ptr<PermStateChangeCb> permStateChangeCb_ = nullptr; 116bae4d13cSopenharmony_ci ErrCode SaveSubscriber(int32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); 117bae4d13cSopenharmony_ci std::atomic_bool isReportActiveInfo_ = false; 118bae4d13cSopenharmony_ci}; 119bae4d13cSopenharmony_ci 120bae4d13cSopenharmony_ci#define POWER_POLICY SensorPowerPolicy::GetInstance() 121bae4d13cSopenharmony_ci} // namespace Sensors 122bae4d13cSopenharmony_ci} // namespace OHOS 123bae4d13cSopenharmony_ci#endif // SENSOR_SERVICE_H 124