Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 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 <android/hardware/sensors/2.0/ISensors.h> |
| 22 | #include <fmq/MessageQueue.h> |
| 23 | #include <hardware_legacy/power.h> |
| 24 | #include <hidl/MQDescriptor.h> |
| 25 | #include <hidl/Status.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace sensors { |
| 30 | namespace V2_0 { |
| 31 | namespace implementation { |
| 32 | |
| 33 | using ::android::sp; |
| 34 | using ::android::hardware::EventFlag; |
| 35 | using ::android::hardware::hidl_string; |
| 36 | using ::android::hardware::hidl_vec; |
| 37 | using ::android::hardware::MessageQueue; |
| 38 | using ::android::hardware::MQDescriptor; |
| 39 | using ::android::hardware::Return; |
| 40 | using ::android::hardware::Void; |
| 41 | |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 42 | class HalProxy : public ISensors { |
| 43 | public: |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 44 | using Event = ::android::hardware::sensors::V1_0::Event; |
| 45 | using OperationMode = ::android::hardware::sensors::V1_0::OperationMode; |
| 46 | using RateLevel = ::android::hardware::sensors::V1_0::RateLevel; |
| 47 | using Result = ::android::hardware::sensors::V1_0::Result; |
| 48 | using SharedMemInfo = ::android::hardware::sensors::V1_0::SharedMemInfo; |
| 49 | |
Anthony Stange | aacbf94 | 2019-08-30 15:21:34 -0400 | [diff] [blame] | 50 | explicit HalProxy(); |
| 51 | // Test only constructor. |
| 52 | explicit HalProxy(std::vector<ISensorsSubHal*>& subHalList); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 53 | ~HalProxy(); |
| 54 | |
| 55 | // Methods from ::android::hardware::sensors::V2_0::ISensors follow. |
| 56 | Return<void> getSensorsList(getSensorsList_cb _hidl_cb) override; |
| 57 | |
| 58 | Return<Result> setOperationMode(OperationMode mode) override; |
| 59 | |
| 60 | Return<Result> activate(int32_t sensorHandle, bool enabled) override; |
| 61 | |
| 62 | Return<Result> initialize( |
| 63 | const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor, |
| 64 | const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, |
| 65 | const sp<ISensorsCallback>& sensorsCallback) override; |
| 66 | |
| 67 | Return<Result> batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
| 68 | int64_t maxReportLatencyNs) override; |
| 69 | |
| 70 | Return<Result> flush(int32_t sensorHandle) override; |
| 71 | |
| 72 | Return<Result> injectSensorData(const Event& event) override; |
| 73 | |
| 74 | Return<void> registerDirectChannel(const SharedMemInfo& mem, |
| 75 | registerDirectChannel_cb _hidl_cb) override; |
| 76 | |
| 77 | Return<Result> unregisterDirectChannel(int32_t channelHandle) override; |
| 78 | |
| 79 | Return<void> configDirectReport(int32_t sensorHandle, int32_t channelHandle, RateLevel rate, |
| 80 | configDirectReport_cb _hidl_cb) override; |
| 81 | |
| 82 | Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override; |
| 83 | |
Anthony Stange | aacbf94 | 2019-08-30 15:21:34 -0400 | [diff] [blame] | 84 | // Below methods from ::android::hardware::sensors::V2_0::ISensorsCallback with a minor change |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 85 | // to pass in the sub-HAL index. While the above methods are invoked from the sensors framework |
| 86 | // via the binder, these methods are invoked from a callback provided to sub-HALs inside the |
| 87 | // same process as the HalProxy, but potentially running on different threads. |
| 88 | Return<void> onDynamicSensorsConnected(const hidl_vec<SensorInfo>& dynamicSensorsAdded, |
| 89 | int32_t subHalIndex); |
| 90 | |
| 91 | Return<void> onDynamicSensorsDisconnected(const hidl_vec<int32_t>& dynamicSensorHandlesRemoved, |
| 92 | int32_t subHalIndex); |
| 93 | |
| 94 | private: |
| 95 | using EventMessageQueue = MessageQueue<Event, kSynchronizedReadWrite>; |
| 96 | using WakeLockMessageQueue = MessageQueue<uint32_t, kSynchronizedReadWrite>; |
| 97 | |
| 98 | /** |
| 99 | * The Event FMQ where sensor events are written |
| 100 | */ |
| 101 | std::unique_ptr<EventMessageQueue> mEventQueue; |
| 102 | |
| 103 | /** |
| 104 | * The Wake Lock FMQ that is read to determine when the framework has handled WAKE_UP events |
| 105 | */ |
| 106 | std::unique_ptr<WakeLockMessageQueue> mWakeLockQueue; |
| 107 | |
| 108 | /** |
| 109 | * Event Flag to signal to the framework when sensor events are available to be read |
| 110 | */ |
| 111 | EventFlag* mEventQueueFlag; |
| 112 | |
| 113 | /** |
| 114 | * Callback to the sensors framework to inform it that new sensors have been added or removed. |
| 115 | */ |
| 116 | sp<ISensorsCallback> mDynamicSensorsCallback; |
Stan Rokita | 2879067 | 2019-08-20 14:32:15 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * SubHal object pointers that have been saved from vendor dynamic libraries. |
| 120 | */ |
| 121 | std::vector<ISensorsSubHal*> mSubHalList; |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 122 | |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 123 | /** |
| 124 | * List of SensorInfo objects that contains the sensor info from subhals as |
| 125 | * well as the modified sensor handle for the framework. |
| 126 | * |
| 127 | * The subhal index is encoded in the first byte of the sensor handle and |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame^] | 128 | * the remaining bytes are generated by the subhal to identify the sensor. |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 129 | */ |
| 130 | std::vector<SensorInfo> mSensorList; |
| 131 | |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame^] | 132 | //! The current operation mode for all subhals. |
| 133 | OperationMode mCurrentOperationMode = OperationMode::NORMAL; |
| 134 | |
| 135 | //! The single subHal that supports directChannel reporting. |
| 136 | ISensorsSubHal* mDirectChannelSubHal = nullptr; |
| 137 | |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 138 | /** |
| 139 | * Initialize the list of SubHal objects in mSubHalList by reading from dynamic libraries |
| 140 | * listed in a config file. |
| 141 | */ |
| 142 | void initializeSubHalListFromConfigFile(const char* configFileName); |
| 143 | |
| 144 | /** |
| 145 | * Initialize the list of SensorInfo objects in mSensorList by getting sensors from each |
| 146 | * subhal. |
| 147 | */ |
| 148 | void initializeSensorList(); |
| 149 | |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame^] | 150 | /** |
| 151 | * Clear direct channel flags if the HalProxy has already chosen a subhal as its direct channel |
| 152 | * subhal. Set the directChannelSubHal pointer to the subHal passed in if this is the first |
| 153 | * direct channel enabled sensor seen. |
| 154 | * |
| 155 | * @param sensorInfo The SensorInfo object that may be altered to have direct channel support |
| 156 | * disabled. |
| 157 | * @param subHal The subhal pointer that the current sensorInfo object came from. |
| 158 | */ |
| 159 | void setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal); |
| 160 | |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 161 | /* |
| 162 | * Get the subhal pointer which can be found by indexing into the mSubHalList vector |
| 163 | * using the index from the first byte of sensorHandle. |
| 164 | * |
| 165 | * @param sensorHandle The handle used to identify a sensor in one of the subhals. |
| 166 | */ |
| 167 | ISensorsSubHal* getSubHalForSensorHandle(uint32_t sensorHandle); |
| 168 | |
| 169 | /* |
| 170 | * Zero out the first (most significant) byte in a number. Used in modifying the sensor handles |
| 171 | * before passing them to subhals. |
| 172 | * |
| 173 | * @param num The uint32_t number to work with. |
| 174 | * |
| 175 | * @return The modified version of num param. |
| 176 | */ |
| 177 | static uint32_t zeroOutFirstByte(uint32_t num); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | } // namespace implementation |
| 181 | } // namespace V2_0 |
| 182 | } // namespace sensors |
| 183 | } // namespace hardware |
| 184 | } // namespace android |