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 OHOS_HDI_SENSOR_V2_0_SENSORINTERFACEPROXY_H 17#define OHOS_HDI_SENSOR_V2_0_SENSORINTERFACEPROXY_H 18 19#include "v2_0/isensor_interface.h" 20#include <unistd.h> 21#include <iproxy_broker.h> 22 23namespace OHOS { 24namespace HDI { 25namespace Sensor { 26namespace V2_0 { 27 28class SensorInterfaceProxy : public IProxyBroker<OHOS::HDI::Sensor::V2_0::ISensorInterface> { 29public: 30 class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient { 31 public: 32 IServiceManagerDeathRecipient(wptr<OHOS::HDI::Sensor::V2_0::SensorInterfaceProxy> proxy) : proxy_(proxy) {} 33 ~IServiceManagerDeathRecipient() override = default; 34 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 35 { 36 int32_t result = HDF_FAILURE; 37 const int sleepInterval = 500000; 38 const int waitTimes = 10; 39 int currentTime = waitTimes; 40 do { 41 usleep(sleepInterval); 42 auto proxy = proxy_.promote(); 43 if (proxy != nullptr) { 44 result = OHOS::HDI::Sensor::V2_0::SensorInterfaceProxy::Reconnect(proxy); 45 } 46 --currentTime; 47 } while (result != HDF_SUCCESS && currentTime >0); 48 } 49 private: 50 wptr<OHOS::HDI::Sensor::V2_0::SensorInterfaceProxy> proxy_; 51 }; 52 53 explicit SensorInterfaceProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Sensor::V2_0::ISensorInterface>(remote) { 54 reconnectRemote_ = nullptr; 55 servMgr_ = nullptr; 56 deathRecipient_ = nullptr; 57 isReconnected_ = false; 58 } 59 virtual ~SensorInterfaceProxy() { 60 if (servMgr_ != nullptr && deathRecipient_ != nullptr) { 61 servMgr_->RemoveDeathRecipient(deathRecipient_); 62 } 63 } 64 65 inline bool IsProxy() override 66 { 67 return true; 68 } 69 70 int32_t GetAllSensorInfo(std::vector<OHOS::HDI::Sensor::V2_0::HdfSensorInformation>& info) override; 71 72 int32_t Enable(int32_t sensorId) override; 73 74 int32_t Disable(int32_t sensorId) override; 75 76 int32_t SetBatch(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval) override; 77 78 int32_t SetMode(int32_t sensorId, int32_t mode) override; 79 80 int32_t SetOption(int32_t sensorId, uint32_t option) override; 81 82 int32_t Register(int32_t groupId, const sptr<OHOS::HDI::Sensor::V2_0::ISensorCallback>& callbackObj) override; 83 84 int32_t Unregister(int32_t groupId, const sptr<OHOS::HDI::Sensor::V2_0::ISensorCallback>& callbackObj) override; 85 86 int32_t ReadData(int32_t sensorId, std::vector<OHOS::HDI::Sensor::V2_0::HdfSensorEvents>& event) override; 87 88 int32_t SetSdcSensor(int32_t sensorId, bool enabled, int32_t rateLevel) override; 89 90 int32_t GetSdcSensorInfo(std::vector<OHOS::HDI::Sensor::V2_0::SdcSensorInfo>& sdcSensorInfo) override; 91 92 int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override; 93 94 static int32_t GetAllSensorInfo_(std::vector<OHOS::HDI::Sensor::V2_0::HdfSensorInformation>& info, 95 const sptr<IRemoteObject> remote); 96 97 static int32_t Enable_(int32_t sensorId, const sptr<IRemoteObject> remote); 98 99 static int32_t Disable_(int32_t sensorId, const sptr<IRemoteObject> remote); 100 101 static int32_t SetBatch_(int32_t sensorId, int64_t samplingInterval, int64_t reportInterval, 102 const sptr<IRemoteObject> remote); 103 104 static int32_t SetMode_(int32_t sensorId, int32_t mode, const sptr<IRemoteObject> remote); 105 106 static int32_t SetOption_(int32_t sensorId, uint32_t option, const sptr<IRemoteObject> remote); 107 108 static int32_t Register_(int32_t groupId, const sptr<OHOS::HDI::Sensor::V2_0::ISensorCallback>& callbackObj, 109 const sptr<IRemoteObject> remote); 110 111 static int32_t Unregister_(int32_t groupId, const sptr<OHOS::HDI::Sensor::V2_0::ISensorCallback>& callbackObj, 112 const sptr<IRemoteObject> remote); 113 114 static int32_t ReadData_(int32_t sensorId, std::vector<OHOS::HDI::Sensor::V2_0::HdfSensorEvents>& event, 115 const sptr<IRemoteObject> remote); 116 117 static int32_t SetSdcSensor_(int32_t sensorId, bool enabled, int32_t rateLevel, const sptr<IRemoteObject> remote); 118 119 static int32_t GetSdcSensorInfo_(std::vector<OHOS::HDI::Sensor::V2_0::SdcSensorInfo>& sdcSensorInfo, 120 const sptr<IRemoteObject> remote); 121 122 static int32_t GetVersion_(uint32_t& majorVer, uint32_t& minorVer, const sptr<IRemoteObject> remote); 123 124 static int32_t Reconnect(sptr<OHOS::HDI::Sensor::V2_0::SensorInterfaceProxy> proxy); 125 126 sptr<IRemoteObject> GetCurrentRemote() { 127 return isReconnected_ ? reconnectRemote_ : Remote(); 128 } 129 130 bool isReconnected_; 131 std::string serviceName_; 132 sptr<IRemoteObject> servMgr_; 133 sptr<OHOS::HDI::Sensor::V2_0::SensorInterfaceProxy::IServiceManagerDeathRecipient> deathRecipient_; 134 sptr<IRemoteObject> reconnectRemote_; 135private: 136 static inline BrokerDelegator<OHOS::HDI::Sensor::V2_0::SensorInterfaceProxy> delegator_; 137}; 138 139} // V2_0 140} // Sensor 141} // HDI 142} // OHOS 143 144#endif // OHOS_HDI_SENSOR_V2_0_SENSORINTERFACEPROXY_H 145 146