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 | #include "HalProxy.h" |
| 18 | |
| 19 | #include <android/hardware/sensors/2.0/types.h> |
| 20 | |
Stan Rokita | 2879067 | 2019-08-20 14:32:15 -0700 | [diff] [blame] | 21 | #include <dlfcn.h> |
| 22 | |
| 23 | #include <fstream> |
| 24 | #include <functional> |
| 25 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace sensors { |
| 29 | namespace V2_0 { |
| 30 | namespace implementation { |
| 31 | |
Stan Rokita | 2879067 | 2019-08-20 14:32:15 -0700 | [diff] [blame] | 32 | typedef ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*); |
| 33 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 34 | // TODO: Use this wake lock name as the prefix to all sensors HAL wake locks acquired. |
| 35 | // constexpr const char* kWakeLockName = "SensorsHAL_WAKEUP"; |
| 36 | |
| 37 | // TODO: Use the following class as a starting point for implementing the full HalProxyCallback |
| 38 | // along with being inspiration for how to implement the ScopedWakelock class. |
| 39 | /** |
| 40 | * Callback class used to provide the HalProxy with the index of which subHal is invoking |
| 41 | */ |
| 42 | class SensorsCallbackProxy : public ISensorsCallback { |
| 43 | public: |
| 44 | SensorsCallbackProxy(wp<HalProxy>& halProxy, int32_t subHalIndex) |
| 45 | : mHalProxy(halProxy), mSubHalIndex(subHalIndex) {} |
| 46 | |
| 47 | Return<void> onDynamicSensorsConnected( |
| 48 | const hidl_vec<SensorInfo>& dynamicSensorsAdded) override { |
| 49 | sp<HalProxy> halProxy(mHalProxy.promote()); |
| 50 | if (halProxy != nullptr) { |
| 51 | return halProxy->onDynamicSensorsConnected(dynamicSensorsAdded, mSubHalIndex); |
| 52 | } |
| 53 | return Return<void>(); |
| 54 | } |
| 55 | |
| 56 | Return<void> onDynamicSensorsDisconnected( |
| 57 | const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override { |
| 58 | sp<HalProxy> halProxy(mHalProxy.promote()); |
| 59 | if (halProxy != nullptr) { |
| 60 | return halProxy->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved, |
| 61 | mSubHalIndex); |
| 62 | } |
| 63 | return Return<void>(); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | wp<HalProxy>& mHalProxy; |
| 68 | int32_t mSubHalIndex; |
| 69 | }; |
| 70 | |
| 71 | HalProxy::HalProxy() { |
Stan Rokita | 2879067 | 2019-08-20 14:32:15 -0700 | [diff] [blame] | 72 | const char* kMultiHalConfigFilePath = "/vendor/etc/sensors/hals.conf"; |
| 73 | std::ifstream subHalConfigStream(kMultiHalConfigFilePath); |
| 74 | if (!subHalConfigStream) { |
| 75 | LOG_FATAL("Failed to load subHal config file: %s", kMultiHalConfigFilePath); |
| 76 | } else { |
| 77 | std::string subHalLibraryFile; |
| 78 | while (subHalConfigStream >> subHalLibraryFile) { |
| 79 | void* handle = dlopen(subHalLibraryFile.c_str(), RTLD_NOW); |
| 80 | if (handle == nullptr) { |
| 81 | LOG_FATAL("dlopen failed for library: %s", subHalLibraryFile.c_str()); |
| 82 | } else { |
| 83 | SensorsHalGetSubHalFunc* sensorsHalGetSubHalPtr = |
| 84 | (SensorsHalGetSubHalFunc*)dlsym(handle, "sensorsHalGetSubHal"); |
| 85 | if (sensorsHalGetSubHalPtr == nullptr) { |
| 86 | LOG_FATAL("Failed to locate sensorsHalGetSubHal function for library: %s", |
| 87 | subHalLibraryFile.c_str()); |
| 88 | } else { |
| 89 | std::function<SensorsHalGetSubHalFunc> sensorsHalGetSubHal = |
| 90 | *sensorsHalGetSubHalPtr; |
| 91 | uint32_t version; |
| 92 | ISensorsSubHal* subHal = sensorsHalGetSubHal(&version); |
| 93 | if (version != SUB_HAL_2_0_VERSION) { |
| 94 | LOG_FATAL("SubHal version was not 2.0 for library: %s", |
| 95 | subHalLibraryFile.c_str()); |
| 96 | } else { |
| 97 | ALOGI("Loaded SubHal from library: %s", subHalLibraryFile.c_str()); |
| 98 | mSubHalList.push_back(subHal); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | // TODO: Discover sensors |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Anthony Stange | aacbf94 | 2019-08-30 15:21:34 -0400 | [diff] [blame^] | 107 | HalProxy::HalProxy(std::vector<ISensorsSubHal*>& subHalList) : mSubHalList(subHalList) { |
| 108 | // TODO: Perform the same steps as the empty constructor. |
| 109 | } |
| 110 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 111 | HalProxy::~HalProxy() { |
| 112 | // TODO: Join any running threads and clean up FMQs and any other allocated |
| 113 | // state. |
| 114 | } |
| 115 | |
| 116 | Return<void> HalProxy::getSensorsList(getSensorsList_cb /* _hidl_cb */) { |
| 117 | // TODO: Output sensors list created as part of HalProxy(). |
| 118 | return Void(); |
| 119 | } |
| 120 | |
| 121 | Return<Result> HalProxy::setOperationMode(OperationMode /* mode */) { |
| 122 | // TODO: Proxy API call to all sub-HALs and return appropriate result. |
| 123 | return Result::INVALID_OPERATION; |
| 124 | } |
| 125 | |
| 126 | Return<Result> HalProxy::activate(int32_t /* sensorHandle */, bool /* enabled */) { |
| 127 | // TODO: Proxy API call to appropriate sub-HAL. |
| 128 | return Result::INVALID_OPERATION; |
| 129 | } |
| 130 | |
| 131 | Return<Result> HalProxy::initialize( |
| 132 | const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor, |
| 133 | const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, |
| 134 | const sp<ISensorsCallback>& sensorsCallback) { |
| 135 | Result result = Result::OK; |
| 136 | |
| 137 | // TODO: clean up sensor requests, if not already done elsewhere through a death recipient, and |
| 138 | // clean up any other resources that exist (FMQs, flags, threads, etc.) |
| 139 | |
| 140 | mDynamicSensorsCallback = sensorsCallback; |
| 141 | |
| 142 | // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. |
| 143 | mEventQueue = |
| 144 | std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */); |
| 145 | |
| 146 | // Create the EventFlag that is used to signal to the framework that sensor events have been |
| 147 | // written to the Event FMQ |
| 148 | if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) { |
| 149 | result = Result::BAD_VALUE; |
| 150 | } |
| 151 | |
| 152 | // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP |
| 153 | // events have been successfully read and handled by the framework. |
| 154 | mWakeLockQueue = |
| 155 | std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */); |
| 156 | |
| 157 | if (!mDynamicSensorsCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) { |
| 158 | result = Result::BAD_VALUE; |
| 159 | } |
| 160 | |
| 161 | // TODO: start threads to read wake locks and process events from sub HALs. |
| 162 | |
| 163 | return result; |
| 164 | } |
| 165 | |
| 166 | Return<Result> HalProxy::batch(int32_t /* sensorHandle */, int64_t /* samplingPeriodNs */, |
| 167 | int64_t /* maxReportLatencyNs */) { |
| 168 | // TODO: Proxy API call to appropriate sub-HAL. |
| 169 | return Result::INVALID_OPERATION; |
| 170 | } |
| 171 | |
| 172 | Return<Result> HalProxy::flush(int32_t /* sensorHandle */) { |
| 173 | // TODO: Proxy API call to appropriate sub-HAL. |
| 174 | return Result::INVALID_OPERATION; |
| 175 | } |
| 176 | |
| 177 | Return<Result> HalProxy::injectSensorData(const Event& /* event */) { |
| 178 | // TODO: Proxy API call to appropriate sub-HAL. |
| 179 | return Result::INVALID_OPERATION; |
| 180 | } |
| 181 | |
| 182 | Return<void> HalProxy::registerDirectChannel(const SharedMemInfo& /* mem */, |
| 183 | registerDirectChannel_cb _hidl_cb) { |
| 184 | // TODO: During init, discover the first sub-HAL in the config that has sensors with direct |
| 185 | // channel support, if any, and proxy the API call there. |
| 186 | _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */); |
| 187 | return Return<void>(); |
| 188 | } |
| 189 | |
| 190 | Return<Result> HalProxy::unregisterDirectChannel(int32_t /* channelHandle */) { |
| 191 | // TODO: During init, discover the first sub-HAL in the config that has sensors with direct |
| 192 | // channel support, if any, and proxy the API call there. |
| 193 | return Result::INVALID_OPERATION; |
| 194 | } |
| 195 | |
| 196 | Return<void> HalProxy::configDirectReport(int32_t /* sensorHandle */, int32_t /* channelHandle */, |
| 197 | RateLevel /* rate */, configDirectReport_cb _hidl_cb) { |
| 198 | // TODO: During init, discover the first sub-HAL in the config that has sensors with direct |
| 199 | // channel support, if any, and proxy the API call there. |
| 200 | _hidl_cb(Result::INVALID_OPERATION, 0 /* reportToken */); |
| 201 | return Return<void>(); |
| 202 | } |
| 203 | |
| 204 | Return<void> HalProxy::debug(const hidl_handle& /* fd */, const hidl_vec<hidl_string>& /* args */) { |
| 205 | // TODO: output debug information |
| 206 | return Return<void>(); |
| 207 | } |
| 208 | |
| 209 | Return<void> HalProxy::onDynamicSensorsConnected( |
| 210 | const hidl_vec<SensorInfo>& /* dynamicSensorsAdded */, int32_t /* subHalIndex */) { |
| 211 | // TODO: Map the SensorInfo to the global list and then invoke the framework's callback. |
| 212 | return Return<void>(); |
| 213 | } |
| 214 | |
| 215 | Return<void> HalProxy::onDynamicSensorsDisconnected( |
| 216 | const hidl_vec<int32_t>& /* dynamicSensorHandlesRemoved */, int32_t /* subHalIndex */) { |
| 217 | // TODO: Unmap the SensorInfo from the global list and then invoke the framework's callback. |
| 218 | return Return<void>(); |
| 219 | } |
| 220 | |
| 221 | } // namespace implementation |
| 222 | } // namespace V2_0 |
| 223 | } // namespace sensors |
| 224 | } // namespace hardware |
| 225 | } // namespace android |