/* * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef SENSOR_SERVICE_H #define SENSOR_SERVICE_H #include "system_ability.h" #include "death_recipient_template.h" #include "sensor_delayed_sp_singleton.h" #include "sensor_power_policy.h" #include "sensor_service_stub.h" #include "stream_server.h" #ifdef HDF_DRIVERS_INTERFACE_SENSOR #include "sensor_hdi_connection.h" #endif // HDF_DRIVERS_INTERFACE_SENSOR namespace OHOS { namespace Sensors { enum class SensorServiceState { STATE_STOPPED, STATE_RUNNING, }; class SensorService : public SystemAbility, public StreamServer, public SensorServiceStub { DECLARE_SYSTEM_ABILITY(SensorService) SENSOR_DECLARE_DELAYED_SP_SINGLETON(SensorService); public: void OnDump() override; void OnStart() override; void OnStop() override; int Dump(int fd, const std::vector &args) override; ErrCode EnableSensor(const SensorDescriptionIPC &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; ErrCode DisableSensor(const SensorDescriptionIPC &sensorDesc) override; ErrCode GetSensorList(std::vector &sensorList) override; ErrCode GetSensorListByDevice(int32_t deviceId, std::vector &sensorList) override; ErrCode TransferDataChannel(int32_t sendFd, const sptr &sensorClient) override; ErrCode DestroySensorChannel(const sptr &sensorClient) override; void ProcessDeathObserver(const wptr &object); ErrCode SuspendSensors(int32_t pid) override; ErrCode ResumeSensors(int32_t pid) override; ErrCode GetActiveInfoList(int32_t pid, std::vector &activeInfoList) override; ErrCode CreateSocketChannel(const sptr &sensorClient, int32_t &clientFd) override; ErrCode DestroySocketChannel(const sptr &sensorClient) override; ErrCode EnableActiveInfoCB() override; ErrCode DisableActiveInfoCB() override; ErrCode ResetSensors() override; ErrCode SetDeviceStatus(uint32_t deviceStatus) override; ErrCode TransferClientRemoteObject(const sptr &sensorClient) override; ErrCode DestroyClientRemoteObject(const sptr &sensorClient) override; private: DISALLOW_COPY_AND_MOVE(SensorService); std::vector GetSensorList(); std::vector GetSensorListByDevice(int32_t deviceId); void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; ErrCode CheckAuthAndParameter(const SensorDescription &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs); void ReportPlugEventCallback(const SensorPlugInfo &sensorPlugInfo); ErrCode SensorReportEvent(const SensorDescription &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs, int32_t pid); class PermStateChangeCb : public Security::AccessToken::PermStateChangeCallbackCustomize { public: PermStateChangeCb(const Security::AccessToken::PermStateChangeScope &scope, sptr server) : PermStateChangeCallbackCustomize(scope), server_(server) {} void PermStateChangeCallback(PermStateChangeInfo &result) override; private: sptr server_ = nullptr; }; void RegisterClientDeathRecipient(sptr sensorClient, int32_t pid); void UnregisterClientDeathRecipient(sptr sensorClient); bool InitSensorPolicy(); void ReportOnChangeData(const SensorDescription &sensorDesc); void ReportSensorSysEvent(int32_t sensorType, bool enable, int32_t pid, int64_t samplingPeriodNs = 0, int64_t maxReportDelayNs = 0); ErrCode DisableSensor(const SensorDescription &sensorDesc, int32_t pid); bool RegisterPermCallback(int32_t sensorType); void UnregisterPermCallback(); bool CheckSensorId(const SensorDescription &sensorDesc); void ReportActiveInfo(const SensorDescription &sensorDesc, int32_t pid); bool IsSystemServiceCalling(); bool IsSystemCalling(); bool IsNeedLoadMotionLib(); void SetCritical(); SensorServiceState state_; std::mutex serviceLock_; std::mutex sensorsMutex_; std::mutex sensorMapMutex_; std::vector sensors_; std::unordered_map sensorMap_; #ifdef HDF_DRIVERS_INTERFACE_SENSOR bool InitInterface(); bool InitDataCallback(); bool InitSensorList(); bool InitPlugCallback(); SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); sptr sensorDataProcesser_ = nullptr; sptr reportDataCallback_ = nullptr; #endif // HDF_DRIVERS_INTERFACE_SENSOR ClientInfo &clientInfo_ = ClientInfo::GetInstance(); SensorManager &sensorManager_ = SensorManager::GetInstance(); std::mutex uidLock_; // death recipient of sensor client std::mutex clientDeathObserverMutex_; sptr clientDeathObserver_ = nullptr; std::shared_ptr permStateChangeCb_ = nullptr; ErrCode SaveSubscriber(const SensorDescription &sensorDesc, int64_t samplingPeriodNs, int64_t maxReportDelayNs); std::atomic_bool isReportActiveInfo_ = false; static std::atomic_bool isAccessTokenServiceActive_; static std::atomic_bool isMemoryMgrServiceActive_; static std::atomic_bool isCritical_; }; #define POWER_POLICY SensorPowerPolicy::GetInstance() } // namespace Sensors } // namespace OHOS #endif // SENSOR_SERVICE_H