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 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 19 | #include "SubHal.h" |
| 20 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 21 | #include <android/hardware/sensors/2.0/types.h> |
| 22 | |
Stan Rokita | d0cd57d | 2019-09-17 15:52:51 -0700 | [diff] [blame] | 23 | #include "hardware_legacy/power.h" |
| 24 | |
Stan Rokita | 2879067 | 2019-08-20 14:32:15 -0700 | [diff] [blame] | 25 | #include <dlfcn.h> |
| 26 | |
| 27 | #include <fstream> |
| 28 | #include <functional> |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 29 | #include <thread> |
Stan Rokita | 2879067 | 2019-08-20 14:32:15 -0700 | [diff] [blame] | 30 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace sensors { |
| 34 | namespace V2_0 { |
| 35 | namespace implementation { |
| 36 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 37 | using ::android::hardware::sensors::V2_0::EventQueueFlagBits; |
| 38 | |
Stan Rokita | 2879067 | 2019-08-20 14:32:15 -0700 | [diff] [blame] | 39 | typedef ISensorsSubHal*(SensorsHalGetSubHalFunc)(uint32_t*); |
| 40 | |
Stan Rokita | e93fdf9 | 2019-09-24 11:58:51 -0700 | [diff] [blame^] | 41 | /** |
| 42 | * Set the subhal index as first byte of sensor handle and return this modified version. |
| 43 | * |
| 44 | * @param sensorHandle The sensor handle to modify. |
| 45 | * @param subHalIndex The index in the hal proxy of the sub hal this sensor belongs to. |
| 46 | * |
| 47 | * @return The modified sensor handle. |
| 48 | */ |
| 49 | uint32_t setSubHalIndex(uint32_t sensorHandle, size_t subHalIndex) { |
| 50 | return sensorHandle | (subHalIndex << 24); |
| 51 | } |
| 52 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 53 | HalProxy::HalProxy() { |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 54 | const char* kMultiHalConfigFile = "/vendor/etc/sensors/hals.conf"; |
| 55 | initializeSubHalListFromConfigFile(kMultiHalConfigFile); |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 56 | initializeSubHalCallbacksAndSensorList(); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Anthony Stange | aacbf94 | 2019-08-30 15:21:34 -0400 | [diff] [blame] | 59 | HalProxy::HalProxy(std::vector<ISensorsSubHal*>& subHalList) : mSubHalList(subHalList) { |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 60 | initializeSubHalCallbacksAndSensorList(); |
Anthony Stange | aacbf94 | 2019-08-30 15:21:34 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 63 | HalProxy::~HalProxy() { |
Stan Rokita | 5971426 | 2019-09-20 10:55:30 -0700 | [diff] [blame] | 64 | { |
| 65 | std::lock_guard<std::mutex> lockGuard(mEventQueueWriteMutex); |
| 66 | mPendingWritesRun = false; |
| 67 | mEventQueueWriteCV.notify_one(); |
| 68 | } |
| 69 | if (mPendingWritesThread.joinable()) { |
| 70 | mPendingWritesThread.join(); |
| 71 | } |
| 72 | // TODO: Cleanup wakeup thread once it is implemented |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 75 | Return<void> HalProxy::getSensorsList(getSensorsList_cb _hidl_cb) { |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 76 | std::vector<SensorInfo> sensors; |
| 77 | for (const auto& iter : mSensors) { |
| 78 | sensors.push_back(iter.second); |
| 79 | } |
| 80 | _hidl_cb(sensors); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 81 | return Void(); |
| 82 | } |
| 83 | |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 84 | Return<Result> HalProxy::setOperationMode(OperationMode mode) { |
| 85 | Result result = Result::OK; |
| 86 | size_t subHalIndex; |
| 87 | for (subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { |
| 88 | ISensorsSubHal* subHal = mSubHalList[subHalIndex]; |
| 89 | result = subHal->setOperationMode(mode); |
| 90 | if (result != Result::OK) { |
| 91 | ALOGE("setOperationMode failed for SubHal: %s", subHal->getName().c_str()); |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | if (result != Result::OK) { |
| 96 | // Reset the subhal operation modes that have been flipped |
| 97 | for (size_t i = 0; i < subHalIndex; i++) { |
| 98 | ISensorsSubHal* subHal = mSubHalList[i]; |
| 99 | subHal->setOperationMode(mCurrentOperationMode); |
| 100 | } |
| 101 | } else { |
| 102 | mCurrentOperationMode = mode; |
| 103 | } |
| 104 | return result; |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Stan Rokita | 4b4c7b7 | 2019-09-10 15:07:59 -0700 | [diff] [blame] | 107 | Return<Result> HalProxy::activate(int32_t sensorHandle, bool enabled) { |
| 108 | return getSubHalForSensorHandle(sensorHandle) |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 109 | ->activate(clearSubHalIndex(sensorHandle), enabled); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | Return<Result> HalProxy::initialize( |
| 113 | const ::android::hardware::MQDescriptorSync<Event>& eventQueueDescriptor, |
| 114 | const ::android::hardware::MQDescriptorSync<uint32_t>& wakeLockDescriptor, |
| 115 | const sp<ISensorsCallback>& sensorsCallback) { |
| 116 | Result result = Result::OK; |
| 117 | |
| 118 | // TODO: clean up sensor requests, if not already done elsewhere through a death recipient, and |
| 119 | // clean up any other resources that exist (FMQs, flags, threads, etc.) |
| 120 | |
| 121 | mDynamicSensorsCallback = sensorsCallback; |
| 122 | |
| 123 | // Create the Event FMQ from the eventQueueDescriptor. Reset the read/write positions. |
| 124 | mEventQueue = |
| 125 | std::make_unique<EventMessageQueue>(eventQueueDescriptor, true /* resetPointers */); |
| 126 | |
| 127 | // Create the EventFlag that is used to signal to the framework that sensor events have been |
| 128 | // written to the Event FMQ |
| 129 | if (EventFlag::createEventFlag(mEventQueue->getEventFlagWord(), &mEventQueueFlag) != OK) { |
| 130 | result = Result::BAD_VALUE; |
| 131 | } |
| 132 | |
| 133 | // Create the Wake Lock FMQ that is used by the framework to communicate whenever WAKE_UP |
| 134 | // events have been successfully read and handled by the framework. |
| 135 | mWakeLockQueue = |
| 136 | std::make_unique<WakeLockMessageQueue>(wakeLockDescriptor, true /* resetPointers */); |
| 137 | |
| 138 | if (!mDynamicSensorsCallback || !mEventQueue || !mWakeLockQueue || mEventQueueFlag == nullptr) { |
| 139 | result = Result::BAD_VALUE; |
| 140 | } |
| 141 | |
Stan Rokita | 5971426 | 2019-09-20 10:55:30 -0700 | [diff] [blame] | 142 | mPendingWritesThread = std::thread(startPendingWritesThread, this); |
| 143 | // TODO: start threads to read wake locks. |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 144 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 145 | for (size_t i = 0; i < mSubHalList.size(); i++) { |
| 146 | auto subHal = mSubHalList[i]; |
| 147 | const auto& subHalCallback = mSubHalCallbacks[i]; |
| 148 | Result currRes = subHal->initialize(subHalCallback); |
| 149 | if (currRes != Result::OK) { |
| 150 | result = currRes; |
| 151 | ALOGE("Subhal '%s' failed to initialize.", subHal->getName().c_str()); |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 156 | return result; |
| 157 | } |
| 158 | |
Stan Rokita | 4b4c7b7 | 2019-09-10 15:07:59 -0700 | [diff] [blame] | 159 | Return<Result> HalProxy::batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
| 160 | int64_t maxReportLatencyNs) { |
| 161 | return getSubHalForSensorHandle(sensorHandle) |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 162 | ->batch(clearSubHalIndex(sensorHandle), samplingPeriodNs, maxReportLatencyNs); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Stan Rokita | 4b4c7b7 | 2019-09-10 15:07:59 -0700 | [diff] [blame] | 165 | Return<Result> HalProxy::flush(int32_t sensorHandle) { |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 166 | return getSubHalForSensorHandle(sensorHandle)->flush(clearSubHalIndex(sensorHandle)); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Stan Rokita | 83e4370 | 2019-09-24 10:16:08 -0700 | [diff] [blame] | 169 | Return<Result> HalProxy::injectSensorData(const Event& event) { |
| 170 | Result result = Result::OK; |
| 171 | if (mCurrentOperationMode == OperationMode::NORMAL && |
| 172 | event.sensorType != V1_0::SensorType::ADDITIONAL_INFO) { |
| 173 | ALOGE("An event with type != ADDITIONAL_INFO passed to injectSensorData while operation" |
| 174 | " mode was NORMAL."); |
| 175 | result = Result::BAD_VALUE; |
| 176 | } |
| 177 | if (result == Result::OK) { |
| 178 | Event subHalEvent = event; |
| 179 | subHalEvent.sensorHandle = clearSubHalIndex(event.sensorHandle); |
| 180 | result = getSubHalForSensorHandle(event.sensorHandle)->injectSensorData(subHalEvent); |
| 181 | } |
| 182 | return result; |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Stan Rokita | db23aa8 | 2019-09-24 11:08:27 -0700 | [diff] [blame] | 185 | Return<void> HalProxy::registerDirectChannel(const SharedMemInfo& mem, |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 186 | registerDirectChannel_cb _hidl_cb) { |
Stan Rokita | db23aa8 | 2019-09-24 11:08:27 -0700 | [diff] [blame] | 187 | if (mDirectChannelSubHal == nullptr) { |
| 188 | _hidl_cb(Result::INVALID_OPERATION, -1 /* channelHandle */); |
| 189 | } else { |
| 190 | mDirectChannelSubHal->registerDirectChannel(mem, _hidl_cb); |
| 191 | } |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 192 | return Return<void>(); |
| 193 | } |
| 194 | |
Stan Rokita | db23aa8 | 2019-09-24 11:08:27 -0700 | [diff] [blame] | 195 | Return<Result> HalProxy::unregisterDirectChannel(int32_t channelHandle) { |
| 196 | Result result; |
| 197 | if (mDirectChannelSubHal == nullptr) { |
| 198 | result = Result::INVALID_OPERATION; |
| 199 | } else { |
| 200 | result = mDirectChannelSubHal->unregisterDirectChannel(channelHandle); |
| 201 | } |
| 202 | return result; |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Stan Rokita | db23aa8 | 2019-09-24 11:08:27 -0700 | [diff] [blame] | 205 | Return<void> HalProxy::configDirectReport(int32_t sensorHandle, int32_t channelHandle, |
| 206 | RateLevel rate, configDirectReport_cb _hidl_cb) { |
| 207 | if (mDirectChannelSubHal == nullptr) { |
| 208 | _hidl_cb(Result::INVALID_OPERATION, -1 /* reportToken */); |
| 209 | } else { |
| 210 | mDirectChannelSubHal->configDirectReport(clearSubHalIndex(sensorHandle), channelHandle, |
| 211 | rate, _hidl_cb); |
| 212 | } |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 213 | return Return<void>(); |
| 214 | } |
| 215 | |
| 216 | Return<void> HalProxy::debug(const hidl_handle& /* fd */, const hidl_vec<hidl_string>& /* args */) { |
| 217 | // TODO: output debug information |
| 218 | return Return<void>(); |
| 219 | } |
| 220 | |
Stan Rokita | e93fdf9 | 2019-09-24 11:58:51 -0700 | [diff] [blame^] | 221 | Return<void> HalProxy::onDynamicSensorsConnected(const hidl_vec<SensorInfo>& dynamicSensorsAdded, |
| 222 | int32_t subHalIndex) { |
| 223 | std::vector<SensorInfo> sensors; |
| 224 | { |
| 225 | std::lock_guard<std::mutex> lock(mDynamicSensorsMutex); |
| 226 | for (SensorInfo sensor : dynamicSensorsAdded) { |
| 227 | if (!subHalIndexIsClear(sensor.sensorHandle)) { |
| 228 | ALOGE("Dynamic sensor added %s had sensorHandle with first byte not 0.", |
| 229 | sensor.name.c_str()); |
| 230 | } else { |
| 231 | sensor.sensorHandle = setSubHalIndex(sensor.sensorHandle, subHalIndex); |
| 232 | mDynamicSensors[sensor.sensorHandle] = sensor; |
| 233 | sensors.push_back(sensor); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | mDynamicSensorsCallback->onDynamicSensorsConnected(sensors); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 238 | return Return<void>(); |
| 239 | } |
| 240 | |
| 241 | Return<void> HalProxy::onDynamicSensorsDisconnected( |
Stan Rokita | e93fdf9 | 2019-09-24 11:58:51 -0700 | [diff] [blame^] | 242 | const hidl_vec<int32_t>& dynamicSensorHandlesRemoved, int32_t subHalIndex) { |
| 243 | // TODO: Block this call until all pending events are flushed from queue |
| 244 | std::vector<int32_t> sensorHandles; |
| 245 | { |
| 246 | std::lock_guard<std::mutex> lock(mDynamicSensorsMutex); |
| 247 | for (int32_t sensorHandle : dynamicSensorHandlesRemoved) { |
| 248 | if (!subHalIndexIsClear(sensorHandle)) { |
| 249 | ALOGE("Dynamic sensorHandle removed had first byte not 0."); |
| 250 | } else { |
| 251 | sensorHandle = setSubHalIndex(sensorHandle, subHalIndex); |
| 252 | if (mDynamicSensors.find(sensorHandle) != mDynamicSensors.end()) { |
| 253 | mDynamicSensors.erase(sensorHandle); |
| 254 | sensorHandles.push_back(sensorHandle); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | mDynamicSensorsCallback->onDynamicSensorsDisconnected(sensorHandles); |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 260 | return Return<void>(); |
| 261 | } |
| 262 | |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 263 | void HalProxy::initializeSubHalListFromConfigFile(const char* configFileName) { |
| 264 | std::ifstream subHalConfigStream(configFileName); |
| 265 | if (!subHalConfigStream) { |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 266 | ALOGE("Failed to load subHal config file: %s", configFileName); |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 267 | } else { |
| 268 | std::string subHalLibraryFile; |
| 269 | while (subHalConfigStream >> subHalLibraryFile) { |
| 270 | void* handle = dlopen(subHalLibraryFile.c_str(), RTLD_NOW); |
| 271 | if (handle == nullptr) { |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 272 | ALOGE("dlopen failed for library: %s", subHalLibraryFile.c_str()); |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 273 | } else { |
| 274 | SensorsHalGetSubHalFunc* sensorsHalGetSubHalPtr = |
| 275 | (SensorsHalGetSubHalFunc*)dlsym(handle, "sensorsHalGetSubHal"); |
| 276 | if (sensorsHalGetSubHalPtr == nullptr) { |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 277 | ALOGE("Failed to locate sensorsHalGetSubHal function for library: %s", |
| 278 | subHalLibraryFile.c_str()); |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 279 | } else { |
| 280 | std::function<SensorsHalGetSubHalFunc> sensorsHalGetSubHal = |
| 281 | *sensorsHalGetSubHalPtr; |
| 282 | uint32_t version; |
| 283 | ISensorsSubHal* subHal = sensorsHalGetSubHal(&version); |
| 284 | if (version != SUB_HAL_2_0_VERSION) { |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 285 | ALOGE("SubHal version was not 2.0 for library: %s", |
| 286 | subHalLibraryFile.c_str()); |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 287 | } else { |
| 288 | ALOGV("Loaded SubHal from library: %s", subHalLibraryFile.c_str()); |
| 289 | mSubHalList.push_back(subHal); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 297 | void HalProxy::initializeSubHalCallbacks() { |
| 298 | for (size_t subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { |
| 299 | sp<IHalProxyCallback> callback = new HalProxyCallback(this, subHalIndex); |
| 300 | mSubHalCallbacks.push_back(callback); |
| 301 | } |
| 302 | } |
| 303 | |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 304 | void HalProxy::initializeSensorList() { |
| 305 | for (size_t subHalIndex = 0; subHalIndex < mSubHalList.size(); subHalIndex++) { |
| 306 | ISensorsSubHal* subHal = mSubHalList[subHalIndex]; |
| 307 | auto result = subHal->getSensorsList([&](const auto& list) { |
| 308 | for (SensorInfo sensor : list) { |
Stan Rokita | e93fdf9 | 2019-09-24 11:58:51 -0700 | [diff] [blame^] | 309 | if (!subHalIndexIsClear(sensor.sensorHandle)) { |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 310 | ALOGE("SubHal sensorHandle's first byte was not 0"); |
| 311 | } else { |
| 312 | ALOGV("Loaded sensor: %s", sensor.name.c_str()); |
| 313 | sensor.sensorHandle |= (subHalIndex << 24); |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 314 | setDirectChannelFlags(&sensor, subHal); |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 315 | mSensors[sensor.sensorHandle] = sensor; |
Stan Rokita | dc7a8e7 | 2019-08-23 12:35:40 -0700 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | }); |
| 319 | if (!result.isOk()) { |
| 320 | ALOGE("getSensorsList call failed for SubHal: %s", subHal->getName().c_str()); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 325 | void HalProxy::initializeSubHalCallbacksAndSensorList() { |
| 326 | initializeSubHalCallbacks(); |
| 327 | initializeSensorList(); |
| 328 | } |
| 329 | |
Stan Rokita | 5971426 | 2019-09-20 10:55:30 -0700 | [diff] [blame] | 330 | void HalProxy::startPendingWritesThread(HalProxy* halProxy) { |
| 331 | halProxy->handlePendingWrites(); |
| 332 | } |
| 333 | |
| 334 | void HalProxy::handlePendingWrites() { |
| 335 | // TODO: Find a way to optimize locking strategy maybe using two mutexes instead of one. |
| 336 | std::unique_lock<std::mutex> lock(mEventQueueWriteMutex); |
| 337 | while (mPendingWritesRun) { |
| 338 | mEventQueueWriteCV.wait( |
| 339 | lock, [&] { return !mPendingWriteEventsQueue.empty() || !mPendingWritesRun; }); |
| 340 | if (!mPendingWriteEventsQueue.empty() && mPendingWritesRun) { |
| 341 | std::vector<Event>& pendingWriteEvents = mPendingWriteEventsQueue.front(); |
| 342 | size_t eventQueueSize = mEventQueue->getQuantumCount(); |
| 343 | size_t numToWrite = std::min(pendingWriteEvents.size(), eventQueueSize); |
| 344 | lock.unlock(); |
| 345 | // TODO: Find a way to interrup writeBlocking if the thread should exit |
| 346 | // so we don't have to wait for timeout on framework restarts. |
| 347 | if (!mEventQueue->writeBlocking( |
| 348 | pendingWriteEvents.data(), numToWrite, |
| 349 | static_cast<uint32_t>(EventQueueFlagBits::EVENTS_READ), |
| 350 | static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS), |
| 351 | kWakelockTimeoutNs, mEventQueueFlag)) { |
| 352 | ALOGE("Dropping %zu events after blockingWrite failed.", numToWrite); |
| 353 | } else { |
| 354 | mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS)); |
| 355 | } |
| 356 | lock.lock(); |
| 357 | if (pendingWriteEvents.size() > eventQueueSize) { |
| 358 | // TODO: Check if this erase operation is too inefficient. It will copy all the |
| 359 | // events ahead of it down to fill gap off array at front after the erase. |
| 360 | pendingWriteEvents.erase(pendingWriteEvents.begin(), |
| 361 | pendingWriteEvents.begin() + eventQueueSize); |
| 362 | } else { |
| 363 | mPendingWriteEventsQueue.pop(); |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 369 | void HalProxy::postEventsToMessageQueue(const std::vector<Event>& events) { |
Stan Rokita | 5971426 | 2019-09-20 10:55:30 -0700 | [diff] [blame] | 370 | size_t numToWrite = 0; |
| 371 | std::lock_guard<std::mutex> lock(mEventQueueWriteMutex); |
| 372 | if (mPendingWriteEventsQueue.empty()) { |
| 373 | numToWrite = std::min(events.size(), mEventQueue->availableToWrite()); |
| 374 | if (numToWrite > 0) { |
| 375 | if (mEventQueue->write(events.data(), numToWrite)) { |
| 376 | // TODO: While loop if mEventQueue->avaiableToWrite > 0 to possibly fit in more |
| 377 | // writes immediately |
| 378 | mEventQueueFlag->wake(static_cast<uint32_t>(EventQueueFlagBits::READ_AND_PROCESS)); |
| 379 | } else { |
| 380 | numToWrite = 0; |
| 381 | } |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | if (numToWrite < events.size()) { |
Stan Rokita | 5971426 | 2019-09-20 10:55:30 -0700 | [diff] [blame] | 385 | // TODO: Bound the mPendingWriteEventsQueue so that we do not trigger OOMs if framework |
| 386 | // stalls |
| 387 | mPendingWriteEventsQueue.push( |
| 388 | std::vector<Event>(events.begin() + numToWrite, events.end())); |
| 389 | mEventQueueWriteCV.notify_one(); |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
Stan Rokita | d0cd57d | 2019-09-17 15:52:51 -0700 | [diff] [blame] | 393 | // TODO: Implement the wakelock timeout in these next two methods. Also pass in the subhal |
| 394 | // index for better tracking. |
| 395 | |
| 396 | void HalProxy::incrementRefCountAndMaybeAcquireWakelock() { |
| 397 | std::lock_guard<std::mutex> lockGuard(mWakelockRefCountMutex); |
| 398 | if (mWakelockRefCount == 0) { |
| 399 | acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLockName); |
| 400 | } |
| 401 | mWakelockRefCount++; |
| 402 | } |
| 403 | |
| 404 | void HalProxy::decrementRefCountAndMaybeReleaseWakelock() { |
| 405 | std::lock_guard<std::mutex> lockGuard(mWakelockRefCountMutex); |
| 406 | mWakelockRefCount--; |
| 407 | if (mWakelockRefCount == 0) { |
| 408 | release_wake_lock(kWakeLockName); |
| 409 | } |
| 410 | } |
| 411 | |
Stan Rokita | 7a72354 | 2019-08-29 15:47:50 -0700 | [diff] [blame] | 412 | void HalProxy::setDirectChannelFlags(SensorInfo* sensorInfo, ISensorsSubHal* subHal) { |
| 413 | bool sensorSupportsDirectChannel = |
| 414 | (sensorInfo->flags & (V1_0::SensorFlagBits::MASK_DIRECT_REPORT | |
| 415 | V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL)) != 0; |
| 416 | if (mDirectChannelSubHal == nullptr && sensorSupportsDirectChannel) { |
| 417 | mDirectChannelSubHal = subHal; |
| 418 | } else if (mDirectChannelSubHal != nullptr && subHal != mDirectChannelSubHal) { |
| 419 | // disable direct channel capability for sensors in subHals that are not |
| 420 | // the only one we will enable |
| 421 | sensorInfo->flags &= ~(V1_0::SensorFlagBits::MASK_DIRECT_REPORT | |
| 422 | V1_0::SensorFlagBits::MASK_DIRECT_CHANNEL); |
| 423 | } |
| 424 | } |
| 425 | |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 426 | ISensorsSubHal* HalProxy::getSubHalForSensorHandle(uint32_t sensorHandle) { |
| 427 | return mSubHalList[static_cast<size_t>(sensorHandle >> 24)]; |
| 428 | } |
| 429 | |
Stan Rokita | f97a3f3 | 2019-09-13 09:58:47 -0700 | [diff] [blame] | 430 | uint32_t HalProxy::clearSubHalIndex(uint32_t sensorHandle) { |
| 431 | return sensorHandle & (~kSensorHandleSubHalIndexMask); |
Stan Rokita | 1638531 | 2019-09-10 14:54:36 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Stan Rokita | e93fdf9 | 2019-09-24 11:58:51 -0700 | [diff] [blame^] | 434 | bool HalProxy::subHalIndexIsClear(uint32_t sensorHandle) { |
| 435 | return (sensorHandle & kSensorHandleSubHalIndexMask) == 0; |
| 436 | } |
| 437 | |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 438 | void HalProxyCallback::postEvents(const std::vector<Event>& events, ScopedWakelock wakelock) { |
| 439 | (void)wakelock; |
| 440 | size_t numWakeupEvents; |
| 441 | std::vector<Event> processedEvents = processEvents(events, &numWakeupEvents); |
| 442 | if (numWakeupEvents > 0) { |
| 443 | ALOG_ASSERT(wakelock.isLocked(), |
| 444 | "Wakeup events posted while wakelock unlocked for subhal" |
| 445 | " w/ index %zu.", |
| 446 | mSubHalIndex); |
| 447 | } else { |
| 448 | ALOG_ASSERT(!wakelock.isLocked(), |
| 449 | "No Wakeup events posted but wakelock locked for subhal" |
| 450 | " w/ index %zu.", |
| 451 | mSubHalIndex); |
| 452 | } |
| 453 | |
| 454 | mHalProxy->postEventsToMessageQueue(processedEvents); |
| 455 | } |
| 456 | |
| 457 | ScopedWakelock HalProxyCallback::createScopedWakelock(bool lock) { |
Stan Rokita | d0cd57d | 2019-09-17 15:52:51 -0700 | [diff] [blame] | 458 | ScopedWakelock wakelock(mHalProxy, lock); |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 459 | return wakelock; |
| 460 | } |
| 461 | |
| 462 | std::vector<Event> HalProxyCallback::processEvents(const std::vector<Event>& events, |
| 463 | size_t* numWakeupEvents) const { |
| 464 | std::vector<Event> eventsOut; |
| 465 | *numWakeupEvents = 0; |
| 466 | for (Event event : events) { |
Stan Rokita | e93fdf9 | 2019-09-24 11:58:51 -0700 | [diff] [blame^] | 467 | event.sensorHandle = setSubHalIndex(event.sensorHandle, mSubHalIndex); |
Stan Rokita | 537c027 | 2019-09-13 10:36:07 -0700 | [diff] [blame] | 468 | eventsOut.push_back(event); |
| 469 | if ((mHalProxy->getSensorInfo(event.sensorHandle).flags & V1_0::SensorFlagBits::WAKE_UP) != |
| 470 | 0) { |
| 471 | (*numWakeupEvents)++; |
| 472 | } |
| 473 | } |
| 474 | return eventsOut; |
| 475 | } |
| 476 | |
Anthony Stange | a689f8a | 2019-07-30 11:35:48 -0400 | [diff] [blame] | 477 | } // namespace implementation |
| 478 | } // namespace V2_0 |
| 479 | } // namespace sensors |
| 480 | } // namespace hardware |
| 481 | } // namespace android |