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_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_agent_type.h" 29 #include "sensor_hdi_connection.h" 30 #include "sensor_manager.h" 31 #include "sensor_service_stub.h" 32 33 namespace OHOS { 34 namespace Sensors { 35 enum class SensorServiceState { 36 STATE_STOPPED, 37 STATE_RUNNING, 38 }; 39 40 class SensorService : public SystemAbility, public SensorServiceStub { 41 DECLARE_SYSTEM_ABILITY(SensorService) 42 43 public: 44 explicit SensorService(int32_t systemAbilityId, bool runOnCreate = false); 45 virtual ~SensorService() = default; 46 void OnDump() override; 47 void OnStart() override; 48 void OnStop() override; 49 int Dump(int fd, const std::vector<std::u16string> &args) override; 50 ErrCode EnableSensor(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs) override; 51 ErrCode DisableSensor(uint32_t sensorId) override; 52 std::vector<Sensor> GetSensorList() override; 53 ErrCode TransferDataChannel(const sptr<SensorBasicDataChannel> &sensorBasicDataChannel, 54 const sptr<IRemoteObject> &sensorClient) override; 55 ErrCode DestroySensorChannel(sptr<IRemoteObject> sensorClient) override; 56 void ProcessDeathObserver(const wptr<IRemoteObject> &object); 57 58 private: 59 DISALLOW_COPY_AND_MOVE(SensorService); 60 void RegisterClientDeathRecipient(sptr<IRemoteObject> sensorClient, int32_t pid); 61 void UnregisterClientDeathRecipient(sptr<IRemoteObject> sensorClient); 62 bool InitInterface(); 63 bool InitDataCallback(); 64 bool InitSensorList(); 65 bool InitSensorPolicy(); 66 void ReportOnChangeData(uint32_t sensorId); 67 void ReportSensorSysEvent(uint32_t sensorId, bool enable, int32_t pid); 68 ErrCode DisableSensor(uint32_t sensorId, int32_t pid); 69 SensorServiceState state_; 70 std::mutex serviceLock_; 71 std::mutex sensorsMutex_; 72 std::mutex sensorMapMutex_; 73 std::vector<Sensor> sensors_; 74 std::unordered_map<uint32_t, Sensor> sensorMap_; 75 SensorHdiConnection &sensorHdiConnection_ = SensorHdiConnection::GetInstance(); 76 ClientInfo &clientInfo_ = ClientInfo::GetInstance(); 77 SensorManager &sensorManager_ = SensorManager::GetInstance(); 78 sptr<SensorDataProcesser> sensorDataProcesser_; 79 sptr<ReportDataCallback> reportDataCallback_; 80 std::mutex uidLock_; 81 // death recipient of sensor client 82 sptr<IRemoteObject::DeathRecipient> clientDeathObserver_; 83 ErrCode SaveSubscriber(uint32_t sensorId, int64_t samplingPeriodNs, int64_t maxReportDelayNs); 84 }; 85 } // namespace Sensors 86 } // namespace OHOS 87 #endif // SENSOR_SERVICE_H 88