1 /* 2 * Copyright (C) 2019 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 "V2_0/SubHal.h" 20 #include "V2_1/SubHal.h" 21 22 #include "IHalProxyCallbackWrapper.h" 23 #include "Sensor.h" 24 25 #include <vector> 26 27 namespace android { 28 namespace hardware { 29 namespace sensors { 30 namespace V2_1 { 31 namespace subhal { 32 namespace implementation { 33 34 using ::android::hardware::sensors::V1_0::OperationMode; 35 using ::android::hardware::sensors::V1_0::Result; 36 37 /** 38 * Implementation of a ISensorsSubHal that can be used to test the implementation of multihal 2.0. 39 * See the README file for more details on how this class can be used for testing. 40 */ 41 class ISensorsSubHalBase : public ISensorsEventCallback { 42 protected: 43 using Event = ::android::hardware::sensors::V2_1::Event; 44 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel; 45 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo; 46 47 public: 48 ISensorsSubHalBase(); 49 50 Return<void> getSensorsList(V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb); 51 Return<Result> injectSensorData(const Event& event); 52 Return<Result> initialize(std::unique_ptr<IHalProxyCallbackWrapperBase>& halProxyCallback); 53 54 // Methods from ::android::hardware::sensors::V2_0::ISensors follow. 55 virtual Return<Result> setOperationMode(OperationMode mode); 56 getOperationMode()57 OperationMode getOperationMode() const { return mCurrentOperationMode; } 58 59 Return<Result> activate(int32_t sensorHandle, bool enabled); 60 61 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, 62 int64_t maxReportLatencyNs); 63 64 Return<Result> flush(int32_t sensorHandle); 65 66 Return<void> registerDirectChannel(const SharedMemInfo& mem, 67 V2_0::ISensors::registerDirectChannel_cb _hidl_cb); 68 69 Return<Result> unregisterDirectChannel(int32_t channelHandle); 70 71 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, 72 V2_0::ISensors::configDirectReport_cb _hidl_cb); 73 74 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args); 75 76 // Methods from ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal follow. getName()77 const std::string getName() { 78 #ifdef SUB_HAL_NAME 79 return SUB_HAL_NAME; 80 #else // SUB_HAL_NAME 81 return "FakeSubHal"; 82 #endif // SUB_HAL_NAME 83 } 84 85 // Method from ISensorsEventCallback. 86 void postEvents(const std::vector<Event>& events, bool wakeup) override; 87 88 protected: 89 template <class SensorType> AddSensor()90 void AddSensor() { 91 std::shared_ptr<SensorType> sensor = 92 std::make_shared<SensorType>(mNextHandle++ /* sensorHandle */, this /* callback */); 93 mSensors[sensor->getSensorInfo().sensorHandle] = sensor; 94 } 95 96 /** 97 * A map of the available sensors 98 */ 99 std::map<int32_t, std::shared_ptr<Sensor>> mSensors; 100 101 /** 102 * Callback used to communicate to the HalProxy when dynamic sensors are connected / 103 * disconnected, sensor events need to be sent to the framework, and when a wakelock should be 104 * acquired. 105 */ 106 std::unique_ptr<IHalProxyCallbackWrapperBase> mCallback; 107 108 private: 109 /** 110 * The current operation mode of the multihal framework. Ensures that all subhals are set to 111 * the same operation mode. 112 */ 113 OperationMode mCurrentOperationMode = OperationMode::NORMAL; 114 115 /** 116 * The next available sensor handle 117 */ 118 int32_t mNextHandle; 119 }; 120 121 template <class SubHalClass> 122 class SensorsSubHalBase : public ISensorsSubHalBase, public SubHalClass { 123 public: setOperationMode(OperationMode mode)124 Return<Result> setOperationMode(OperationMode mode) override { 125 return ISensorsSubHalBase::setOperationMode(mode); 126 } 127 activate(int32_t sensorHandle,bool enabled)128 Return<Result> activate(int32_t sensorHandle, bool enabled) override { 129 return ISensorsSubHalBase::activate(sensorHandle, enabled); 130 } 131 batch(int32_t sensorHandle,int64_t samplingPeriodNs,int64_t maxReportLatencyNs)132 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, 133 int64_t maxReportLatencyNs) override { 134 return ISensorsSubHalBase::batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs); 135 } 136 flush(int32_t sensorHandle)137 Return<Result> flush(int32_t sensorHandle) override { 138 return ISensorsSubHalBase::flush(sensorHandle); 139 } 140 registerDirectChannel(const SharedMemInfo & mem,V2_0::ISensors::registerDirectChannel_cb _hidl_cb)141 Return<void> registerDirectChannel(const SharedMemInfo& mem, 142 V2_0::ISensors::registerDirectChannel_cb _hidl_cb) override { 143 return ISensorsSubHalBase::registerDirectChannel(mem, _hidl_cb); 144 } 145 unregisterDirectChannel(int32_t channelHandle)146 Return<Result> unregisterDirectChannel(int32_t channelHandle) override { 147 return ISensorsSubHalBase::unregisterDirectChannel(channelHandle); 148 } 149 configDirectReport(int32_t sensorHandle,int32_t channelHandle,RateLevel rate,V2_0::ISensors::configDirectReport_cb _hidl_cb)150 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, 151 V2_0::ISensors::configDirectReport_cb _hidl_cb) override { 152 return ISensorsSubHalBase::configDirectReport(sensorHandle, channelHandle, rate, _hidl_cb); 153 } 154 debug(const hidl_handle & fd,const hidl_vec<hidl_string> & args)155 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override { 156 return ISensorsSubHalBase::debug(fd, args); 157 } 158 getName()159 const std::string getName() override { return ISensorsSubHalBase::getName(); } 160 }; 161 162 class SensorsSubHalV2_0 : public SensorsSubHalBase<V2_0::implementation::ISensorsSubHal> { 163 public: getSensorsList(V2_0::ISensors::getSensorsList_cb _hidl_cb)164 virtual Return<void> getSensorsList(V2_0::ISensors::getSensorsList_cb _hidl_cb) override { 165 return ISensorsSubHalBase::getSensorsList([&](const auto& list) { 166 _hidl_cb(V2_1::implementation::convertToOldSensorInfos(list)); 167 }); 168 } 169 injectSensorData(const V1_0::Event & event)170 Return<Result> injectSensorData(const V1_0::Event& event) override { 171 return ISensorsSubHalBase::injectSensorData(V2_1::implementation::convertToNewEvent(event)); 172 } 173 initialize(const sp<V2_0::implementation::IHalProxyCallback> & halProxyCallback)174 Return<Result> initialize( 175 const sp<V2_0::implementation::IHalProxyCallback>& halProxyCallback) override { 176 std::unique_ptr<IHalProxyCallbackWrapperBase> wrapper = 177 std::make_unique<HalProxyCallbackWrapperV2_0>(halProxyCallback); 178 return ISensorsSubHalBase::initialize(wrapper); 179 } 180 }; 181 182 class SensorsSubHalV2_1 : public SensorsSubHalBase<V2_1::implementation::ISensorsSubHal> { 183 public: getSensorsList_2_1(V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb)184 Return<void> getSensorsList_2_1(V2_1::ISensors::getSensorsList_2_1_cb _hidl_cb) override { 185 return ISensorsSubHalBase::getSensorsList(_hidl_cb); 186 } 187 injectSensorData_2_1(const V2_1::Event & event)188 Return<Result> injectSensorData_2_1(const V2_1::Event& event) override { 189 return ISensorsSubHalBase::injectSensorData(event); 190 } 191 initialize(const sp<V2_1::implementation::IHalProxyCallback> & halProxyCallback)192 Return<Result> initialize( 193 const sp<V2_1::implementation::IHalProxyCallback>& halProxyCallback) override { 194 std::unique_ptr<IHalProxyCallbackWrapperBase> wrapper = 195 std::make_unique<HalProxyCallbackWrapperV2_1>(halProxyCallback); 196 return ISensorsSubHalBase::initialize(wrapper); 197 } 198 }; 199 200 // SubHal that has continuous sensors for testing purposes. 201 template <class SubHalVersion> 202 class ContinuousSensorsSubHal : public SubHalVersion { 203 public: ContinuousSensorsSubHal()204 ContinuousSensorsSubHal() { 205 ISensorsSubHalBase::AddSensor<AccelSensor>(); 206 ISensorsSubHalBase::AddSensor<GyroSensor>(); 207 ISensorsSubHalBase::AddSensor<MagnetometerSensor>(); 208 ISensorsSubHalBase::AddSensor<PressureSensor>(); 209 } 210 }; 211 212 // SubHal that has on-change sensors for testing purposes. 213 template <class SubHalVersion> 214 class OnChangeSensorsSubHal : public SubHalVersion { 215 public: OnChangeSensorsSubHal()216 OnChangeSensorsSubHal() { 217 ISensorsSubHalBase::AddSensor<AmbientTempSensor>(); 218 ISensorsSubHalBase::AddSensor<LightSensor>(); 219 ISensorsSubHalBase::AddSensor<ProximitySensor>(); 220 ISensorsSubHalBase::AddSensor<RelativeHumiditySensor>(); 221 } 222 }; 223 224 // SubHal that has both continuous and on-change sensors for testing purposes. 225 template <class SubHalVersion> 226 class AllSensorsSubHal : public SubHalVersion { 227 public: AllSensorsSubHal()228 AllSensorsSubHal() { 229 ISensorsSubHalBase::AddSensor<AccelSensor>(); 230 ISensorsSubHalBase::AddSensor<GyroSensor>(); 231 ISensorsSubHalBase::AddSensor<MagnetometerSensor>(); 232 ISensorsSubHalBase::AddSensor<PressureSensor>(); 233 ISensorsSubHalBase::AddSensor<AmbientTempSensor>(); 234 ISensorsSubHalBase::AddSensor<LightSensor>(); 235 ISensorsSubHalBase::AddSensor<ProximitySensor>(); 236 ISensorsSubHalBase::AddSensor<RelativeHumiditySensor>(); 237 } 238 }; 239 240 class SetOperationModeFailingSensorsSubHal : public AllSensorsSubHal<SensorsSubHalV2_0> { 241 public: 242 Return<Result> setOperationMode(OperationMode mode) override; 243 }; 244 245 class AllSupportDirectChannelSensorsSubHal : public AllSensorsSubHal<SensorsSubHalV2_0> { 246 public: 247 Return<void> getSensorsList(V2_0::ISensors::getSensorsList_cb _hidl_cb) override; 248 }; 249 250 class DoesNotSupportDirectChannelSensorsSubHal : public AllSensorsSubHal<SensorsSubHalV2_0> { 251 public: 252 Return<void> getSensorsList(V2_0::ISensors::getSensorsList_cb _hidl_cb) override; 253 }; 254 255 class AddAndRemoveDynamicSensorsSubHal : public AllSensorsSubHal<SensorsSubHalV2_0> { 256 public: 257 void addDynamicSensors(const std::vector<SensorInfo>& sensorsAdded); 258 void removeDynamicSensors(const std::vector<int32_t>& sensorHandlesAdded); 259 }; 260 261 } // namespace implementation 262 } // namespace subhal 263 } // namespace V2_1 264 } // namespace sensors 265 } // namespace hardware 266 } // namespace android 267