1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include "HalProxyCallback.h" 20 #include "V2_0/SubHal.h" 21 #include "V2_1/SubHal.h" 22 23 #include "android/hardware/sensors/1.0/ISensors.h" 24 #include "android/hardware/sensors/1.0/types.h" 25 #include "android/hardware/sensors/2.0/ISensors.h" 26 #include "android/hardware/sensors/2.0/ISensorsCallback.h" 27 #include "android/hardware/sensors/2.1/ISensors.h" 28 #include "android/hardware/sensors/2.1/ISensorsCallback.h" 29 #include "android/hardware/sensors/2.1/types.h" 30 31 #include <utils/LightRefBase.h> 32 33 #include <cassert> 34 35 namespace android { 36 namespace hardware { 37 namespace sensors { 38 namespace V2_1 { 39 namespace implementation { 40 41 /** 42 * The following subHal wrapper classes abstract away common functionality across V2.0 and V2.1 43 * subHal interfaces. Much of the logic is common between the two versions and this allows users of 44 * the classes to only care about the type used at initialization and then interact with either 45 * version of the subHal interface without worrying about the type. 46 */ 47 class ISubHalWrapperBase { 48 protected: 49 using Event = ::android::hardware::sensors::V2_1::Event; 50 using OperationMode = ::android::hardware::sensors::V1_0::OperationMode; 51 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel; 52 using Result = ::android::hardware::sensors::V1_0::Result; 53 using SensorInfo = ::android::hardware::sensors::V2_1::SensorInfo; 54 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo; 55 56 public: ~ISubHalWrapperBase()57 virtual ~ISubHalWrapperBase() {} 58 59 virtual bool supportsNewEvents() = 0; 60 61 virtual Return<Result> initialize(V2_0::implementation::ISubHalCallback* callback, 62 V2_0::implementation::IScopedWakelockRefCounter* refCounter, 63 int32_t subHalIndex) = 0; 64 65 virtual Return<void> getSensorsList( 66 ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) = 0; 67 68 virtual Return<Result> setOperationMode(OperationMode mode) = 0; 69 70 virtual Return<Result> activate(int32_t sensorHandle, bool enabled) = 0; 71 72 virtual Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, 73 int64_t maxReportLatencyNs) = 0; 74 75 virtual Return<Result> flush(int32_t sensorHandle) = 0; 76 77 virtual Return<Result> injectSensorData(const Event& event) = 0; 78 79 virtual Return<void> registerDirectChannel(const SharedMemInfo& mem, 80 ISensors::registerDirectChannel_cb _hidl_cb) = 0; 81 82 virtual Return<Result> unregisterDirectChannel(int32_t channelHandle) = 0; 83 84 virtual Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, 85 RateLevel rate, 86 ISensors::configDirectReport_cb _hidl_cb) = 0; 87 88 virtual Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) = 0; 89 90 virtual const std::string getName() = 0; 91 }; 92 93 template <typename T> 94 class SubHalWrapperBase : public ISubHalWrapperBase { 95 public: SubHalWrapperBase(T * subHal)96 SubHalWrapperBase(T* subHal) : mSubHal(subHal){}; 97 supportsNewEvents()98 virtual bool supportsNewEvents() override { return false; } 99 getSensorsList(::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb)100 virtual Return<void> getSensorsList( 101 ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override { 102 return mSubHal->getSensorsList( 103 [&](const auto& list) { _hidl_cb(convertToNewSensorInfos(list)); }); 104 } 105 setOperationMode(OperationMode mode)106 Return<Result> setOperationMode(OperationMode mode) override { 107 return mSubHal->setOperationMode(mode); 108 } 109 activate(int32_t sensorHandle,bool enabled)110 Return<Result> activate(int32_t sensorHandle, bool enabled) override { 111 return mSubHal->activate(sensorHandle, enabled); 112 } 113 batch(int32_t sensorHandle,int64_t samplingPeriodNs,int64_t maxReportLatencyNs)114 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, 115 int64_t maxReportLatencyNs) override { 116 return mSubHal->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs); 117 } 118 flush(int32_t sensorHandle)119 Return<Result> flush(int32_t sensorHandle) override { return mSubHal->flush(sensorHandle); } 120 injectSensorData(const Event & event)121 virtual Return<Result> injectSensorData(const Event& event) override { 122 return mSubHal->injectSensorData(convertToOldEvent(event)); 123 } 124 registerDirectChannel(const SharedMemInfo & mem,ISensors::registerDirectChannel_cb _hidl_cb)125 Return<void> registerDirectChannel(const SharedMemInfo& mem, 126 ISensors::registerDirectChannel_cb _hidl_cb) override { 127 return mSubHal->registerDirectChannel(mem, _hidl_cb); 128 } 129 unregisterDirectChannel(int32_t channelHandle)130 Return<Result> unregisterDirectChannel(int32_t channelHandle) override { 131 return mSubHal->unregisterDirectChannel(channelHandle); 132 } 133 configDirectReport(int32_t sensorHandle,int32_t channelHandle,RateLevel rate,ISensors::configDirectReport_cb _hidl_cb)134 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, 135 ISensors::configDirectReport_cb _hidl_cb) override { 136 return mSubHal->configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb); 137 } 138 debug(const hidl_handle & fd,const hidl_vec<hidl_string> & args)139 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override { 140 return mSubHal->debug(fd, args); 141 } 142 getName()143 const std::string getName() override { return mSubHal->getName(); } 144 145 protected: 146 T* mSubHal; 147 }; 148 149 class SubHalWrapperV2_0 : public SubHalWrapperBase<V2_0::implementation::ISensorsSubHal> { 150 public: SubHalWrapperV2_0(V2_0::implementation::ISensorsSubHal * subHal)151 SubHalWrapperV2_0(V2_0::implementation::ISensorsSubHal* subHal) : SubHalWrapperBase(subHal){}; 152 initialize(V2_0::implementation::ISubHalCallback * callback,V2_0::implementation::IScopedWakelockRefCounter * refCounter,int32_t subHalIndex)153 Return<Result> initialize(V2_0::implementation::ISubHalCallback* callback, 154 V2_0::implementation::IScopedWakelockRefCounter* refCounter, 155 int32_t subHalIndex) override { 156 return mSubHal->initialize( 157 new V2_0::implementation::HalProxyCallbackV2_0(callback, refCounter, subHalIndex)); 158 } 159 }; 160 161 class SubHalWrapperV2_1 : public SubHalWrapperBase<V2_1::implementation::ISensorsSubHal> { 162 public: SubHalWrapperV2_1(V2_1::implementation::ISensorsSubHal * subHal)163 SubHalWrapperV2_1(V2_1::implementation::ISensorsSubHal* subHal) : SubHalWrapperBase(subHal) {} 164 supportsNewEvents()165 bool supportsNewEvents() override { return true; } 166 getSensorsList(::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb)167 virtual Return<void> getSensorsList( 168 ::android::hardware::sensors::V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override { 169 return mSubHal->getSensorsList_2_1([&](const auto& list) { _hidl_cb(list); }); 170 } 171 injectSensorData(const Event & event)172 virtual Return<Result> injectSensorData(const Event& event) override { 173 return mSubHal->injectSensorData_2_1(event); 174 } 175 initialize(V2_0::implementation::ISubHalCallback * callback,V2_0::implementation::IScopedWakelockRefCounter * refCounter,int32_t subHalIndex)176 Return<Result> initialize(V2_0::implementation::ISubHalCallback* callback, 177 V2_0::implementation::IScopedWakelockRefCounter* refCounter, 178 int32_t subHalIndex) override { 179 return mSubHal->initialize( 180 new V2_0::implementation::HalProxyCallbackV2_1(callback, refCounter, subHalIndex)); 181 } 182 }; 183 184 } // namespace implementation 185 } // namespace V2_1 186 } // namespace sensors 187 } // namespace hardware 188 } // namespace android 189