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 "SubHal.h" 20 21 #include "Sensor.h" 22 23 #include <vector> 24 25 namespace android { 26 namespace hardware { 27 namespace sensors { 28 namespace V2_0 { 29 namespace subhal { 30 namespace implementation { 31 32 using ::android::hardware::sensors::V1_0::OperationMode; 33 using ::android::hardware::sensors::V1_0::Result; 34 using ::android::hardware::sensors::V2_0::implementation::IHalProxyCallback; 35 36 /** 37 * Implementation of a ISensorsSubHal that can be used to test the implementation of multihal 2.0. 38 * See the README file for more details on how this class can be used for testing. 39 */ 40 class SensorsSubHal : public ISensorsSubHal, public ISensorsEventCallback { 41 using Event = ::android::hardware::sensors::V1_0::Event; 42 using RateLevel = ::android::hardware::sensors::V1_0::RateLevel; 43 using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo; 44 45 public: 46 SensorsSubHal(); 47 48 // Methods from ::android::hardware::sensors::V2_0::ISensors follow. 49 virtual Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override; 50 51 virtual Return<Result> setOperationMode(OperationMode mode) override; 52 getOperationMode()53 OperationMode getOperationMode() const { return mCurrentOperationMode; } 54 55 Return<Result> activate(int32_t sensorHandle, bool enabled) override; 56 57 Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, 58 int64_t maxReportLatencyNs) override; 59 60 Return<Result> flush(int32_t sensorHandle) override; 61 62 Return<Result> injectSensorData(const Event& event) override; 63 64 Return<void> registerDirectChannel(const SharedMemInfo& mem, 65 registerDirectChannel_cb _hidl_cb) override; 66 67 Return<Result> unregisterDirectChannel(int32_t channelHandle) override; 68 69 Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, 70 configDirectReport_cb _hidl_cb) override; 71 72 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override; 73 74 // Methods from ::android::hardware::sensors::V2_0::implementation::ISensorsSubHal follow. getName()75 const std::string getName() override { 76 #ifdef SUB_HAL_NAME 77 return SUB_HAL_NAME; 78 #else // SUB_HAL_NAME 79 return "FakeSubHal"; 80 #endif // SUB_HAL_NAME 81 } 82 83 Return<Result> initialize(const sp<IHalProxyCallback>& halProxyCallback) override; 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 sp<IHalProxyCallback> 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 // SubHal that has continuous sensors for testing purposes. 122 class ContinuousSensorsSubHal : public SensorsSubHal { 123 public: 124 ContinuousSensorsSubHal(); 125 }; 126 127 // SubHal that has on-change sensors for testing purposes. 128 class OnChangeSensorsSubHal : public SensorsSubHal { 129 public: 130 OnChangeSensorsSubHal(); 131 }; 132 133 // SubHal that has both continuous and on-change sensors for testing purposes. 134 class AllSensorsSubHal : public SensorsSubHal { 135 public: 136 AllSensorsSubHal(); 137 }; 138 139 class SetOperationModeFailingSensorsSubHal : public AllSensorsSubHal { 140 public: 141 Return<Result> setOperationMode(OperationMode mode) override; 142 }; 143 144 class AllSupportDirectChannelSensorsSubHal : public AllSensorsSubHal { 145 public: 146 Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override; 147 }; 148 149 class DoesNotSupportDirectChannelSensorsSubHal : public AllSensorsSubHal { 150 public: 151 Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override; 152 }; 153 154 class AddAndRemoveDynamicSensorsSubHal : public AllSensorsSubHal { 155 public: 156 void addDynamicSensors(const std::vector<SensorInfo>& sensorsAdded); 157 void removeDynamicSensors(const std::vector<int32_t>& sensorHandlesAdded); 158 }; 159 160 } // namespace implementation 161 } // namespace subhal 162 } // namespace V2_0 163 } // namespace sensors 164 } // namespace hardware 165 } // namespace android 166