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