| Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2021 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 "HidlSensorHalWrapper.h" | 
|  | 18 | #include "android/hardware/sensors/2.0/types.h" | 
|  | 19 | #include "android/hardware/sensors/2.1/ISensorsCallback.h" | 
|  | 20 | #include "android/hardware/sensors/2.1/types.h" | 
|  | 21 | #include "convertV2_1.h" | 
|  | 22 |  | 
|  | 23 | #include <android-base/logging.h> | 
|  | 24 |  | 
|  | 25 | using android::hardware::hidl_vec; | 
|  | 26 | using android::hardware::sensors::V1_0::RateLevel; | 
|  | 27 | using android::hardware::sensors::V1_0::Result; | 
|  | 28 | using android::hardware::sensors::V1_0::SharedMemFormat; | 
|  | 29 | using android::hardware::sensors::V1_0::SharedMemInfo; | 
|  | 30 | using android::hardware::sensors::V1_0::SharedMemType; | 
|  | 31 | using android::hardware::sensors::V2_0::EventQueueFlagBits; | 
|  | 32 | using android::hardware::sensors::V2_0::WakeLockQueueFlagBits; | 
|  | 33 | using android::hardware::sensors::V2_1::Event; | 
|  | 34 | using android::hardware::sensors::V2_1::ISensorsCallback; | 
|  | 35 | using android::hardware::sensors::V2_1::implementation::convertFromSensorEvent; | 
|  | 36 | using android::hardware::sensors::V2_1::implementation::convertToNewEvents; | 
|  | 37 | using android::hardware::sensors::V2_1::implementation::convertToNewSensorInfos; | 
|  | 38 | using android::hardware::sensors::V2_1::implementation::convertToSensor; | 
|  | 39 | using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV1_0; | 
|  | 40 | using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV2_0; | 
|  | 41 | using android::hardware::sensors::V2_1::implementation::ISensorsWrapperV2_1; | 
|  | 42 |  | 
|  | 43 | namespace android { | 
|  | 44 |  | 
|  | 45 | namespace { | 
|  | 46 |  | 
|  | 47 | status_t statusFromResult(Result result) { | 
|  | 48 | switch (result) { | 
|  | 49 | case Result::OK: | 
|  | 50 | return OK; | 
|  | 51 | case Result::BAD_VALUE: | 
|  | 52 | return BAD_VALUE; | 
|  | 53 | case Result::PERMISSION_DENIED: | 
|  | 54 | return PERMISSION_DENIED; | 
|  | 55 | case Result::INVALID_OPERATION: | 
|  | 56 | return INVALID_OPERATION; | 
|  | 57 | case Result::NO_MEMORY: | 
|  | 58 | return NO_MEMORY; | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | template <typename EnumType> | 
|  | 63 | constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) { | 
|  | 64 | return static_cast<typename std::underlying_type<EnumType>::type>(value); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | enum EventQueueFlagBitsInternal : uint32_t { | 
|  | 68 | INTERNAL_WAKE = 1 << 16, | 
|  | 69 | }; | 
|  | 70 |  | 
|  | 71 | } // anonymous namespace | 
|  | 72 |  | 
|  | 73 | void SensorsHalDeathReceiver::serviceDied( | 
|  | 74 | uint64_t /* cookie */, const wp<::android::hidl::base::V1_0::IBase>& /* service */) { | 
|  | 75 | ALOGW("Sensors HAL died, attempting to reconnect."); | 
|  | 76 | mHidlSensorHalWrapper->prepareForReconnect(); | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | struct SensorsCallback : public ISensorsCallback { | 
|  | 80 | using Result = ::android::hardware::sensors::V1_0::Result; | 
|  | 81 | using SensorInfo = ::android::hardware::sensors::V2_1::SensorInfo; | 
|  | 82 |  | 
|  | 83 | SensorsCallback(ISensorHalWrapper::SensorDeviceCallback* sensorDeviceCallback) { | 
|  | 84 | mSensorDeviceCallback = sensorDeviceCallback; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | Return<void> onDynamicSensorsConnected_2_1( | 
|  | 88 | const hidl_vec<SensorInfo>& dynamicSensorsAdded) override { | 
|  | 89 | std::vector<sensor_t> sensors; | 
|  | 90 | for (const android::hardware::sensors::V2_1::SensorInfo& info : dynamicSensorsAdded) { | 
|  | 91 | sensor_t sensor; | 
|  | 92 | convertToSensor(info, &sensor); | 
|  | 93 | sensors.push_back(sensor); | 
|  | 94 | } | 
|  | 95 |  | 
|  | 96 | mSensorDeviceCallback->onDynamicSensorsConnected(sensors); | 
|  | 97 | return Return<void>(); | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | Return<void> onDynamicSensorsConnected( | 
|  | 101 | const hidl_vec<android::hardware::sensors::V1_0::SensorInfo>& dynamicSensorsAdded) | 
|  | 102 | override { | 
|  | 103 | return onDynamicSensorsConnected_2_1(convertToNewSensorInfos(dynamicSensorsAdded)); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | Return<void> onDynamicSensorsDisconnected( | 
|  | 107 | const hidl_vec<int32_t>& dynamicSensorHandlesRemoved) override { | 
|  | 108 | mSensorDeviceCallback->onDynamicSensorsDisconnected(dynamicSensorHandlesRemoved); | 
|  | 109 | return Return<void>(); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | private: | 
|  | 113 | ISensorHalWrapper::SensorDeviceCallback* mSensorDeviceCallback; | 
|  | 114 | }; | 
|  | 115 |  | 
|  | 116 | bool HidlSensorHalWrapper::supportsPolling() { | 
|  | 117 | return mSensors->supportsPolling(); | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | bool HidlSensorHalWrapper::supportsMessageQueues() { | 
|  | 121 | return mSensors->supportsMessageQueues(); | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | bool HidlSensorHalWrapper::connect(SensorDeviceCallback* callback) { | 
|  | 125 | mSensorDeviceCallback = callback; | 
|  | 126 | bool ret = connectHidlService(); | 
|  | 127 | if (mEventQueueFlag != nullptr) { | 
|  | 128 | mEventQueueFlag->wake(asBaseType(INTERNAL_WAKE)); | 
|  | 129 | } | 
|  | 130 | return ret; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | void HidlSensorHalWrapper::prepareForReconnect() { | 
|  | 134 | mReconnecting = true; | 
|  | 135 | if (mEventQueueFlag != nullptr) { | 
|  | 136 | mEventQueueFlag->wake(asBaseType(INTERNAL_WAKE)); | 
|  | 137 | } | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | ssize_t HidlSensorHalWrapper::poll(sensors_event_t* buffer, size_t count) { | 
|  | 141 | ssize_t err; | 
|  | 142 | int numHidlTransportErrors = 0; | 
|  | 143 | bool hidlTransportError = false; | 
|  | 144 |  | 
|  | 145 | do { | 
|  | 146 | auto ret = mSensors->poll( | 
|  | 147 | count, [&](auto result, const auto& events, const auto& dynamicSensorsAdded) { | 
|  | 148 | if (result == Result::OK) { | 
|  | 149 | convertToSensorEventsAndQuantize(convertToNewEvents(events), | 
|  | 150 | convertToNewSensorInfos( | 
|  | 151 | dynamicSensorsAdded), | 
|  | 152 | buffer); | 
|  | 153 | err = (ssize_t)events.size(); | 
|  | 154 | } else { | 
|  | 155 | err = statusFromResult(result); | 
|  | 156 | } | 
|  | 157 | }); | 
|  | 158 |  | 
|  | 159 | if (ret.isOk()) { | 
|  | 160 | hidlTransportError = false; | 
|  | 161 | } else { | 
|  | 162 | hidlTransportError = true; | 
|  | 163 | numHidlTransportErrors++; | 
|  | 164 | if (numHidlTransportErrors > 50) { | 
|  | 165 | // Log error and bail | 
|  | 166 | ALOGE("Max Hidl transport errors this cycle : %d", numHidlTransportErrors); | 
|  | 167 | handleHidlDeath(ret.description()); | 
|  | 168 | } else { | 
|  | 169 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 | } while (hidlTransportError); | 
|  | 173 |  | 
|  | 174 | if (numHidlTransportErrors > 0) { | 
|  | 175 | ALOGE("Saw %d Hidl transport failures", numHidlTransportErrors); | 
|  | 176 | HidlTransportErrorLog errLog(time(nullptr), numHidlTransportErrors); | 
|  | 177 | mHidlTransportErrors.add(errLog); | 
|  | 178 | mTotalHidlTransportErrors++; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | return err; | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | ssize_t HidlSensorHalWrapper::pollFmq(sensors_event_t* buffer, size_t maxNumEventsToRead) { | 
|  | 185 | ssize_t eventsRead = 0; | 
|  | 186 | size_t availableEvents = mSensors->getEventQueue()->availableToRead(); | 
|  | 187 |  | 
|  | 188 | if (availableEvents == 0) { | 
|  | 189 | uint32_t eventFlagState = 0; | 
|  | 190 |  | 
|  | 191 | // Wait for events to become available. This is necessary so that the Event FMQ's read() is | 
|  | 192 | // able to be called with the correct number of events to read. If the specified number of | 
|  | 193 | // events is not available, then read() would return no events, possibly introducing | 
|  | 194 | // additional latency in delivering events to applications. | 
|  | 195 | if (mEventQueueFlag != nullptr) { | 
|  | 196 | mEventQueueFlag->wait(asBaseType(EventQueueFlagBits::READ_AND_PROCESS) | | 
|  | 197 | asBaseType(INTERNAL_WAKE), | 
|  | 198 | &eventFlagState); | 
|  | 199 | } | 
|  | 200 | availableEvents = mSensors->getEventQueue()->availableToRead(); | 
|  | 201 |  | 
|  | 202 | if ((eventFlagState & asBaseType(INTERNAL_WAKE)) && mReconnecting) { | 
|  | 203 | ALOGD("Event FMQ internal wake, returning from poll with no events"); | 
|  | 204 | return DEAD_OBJECT; | 
|  | 205 | } | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | size_t eventsToRead = std::min({availableEvents, maxNumEventsToRead, mEventBuffer.size()}); | 
|  | 209 | if (eventsToRead > 0) { | 
|  | 210 | if (mSensors->getEventQueue()->read(mEventBuffer.data(), eventsToRead)) { | 
|  | 211 | // Notify the Sensors HAL that sensor events have been read. This is required to support | 
|  | 212 | // the use of writeBlocking by the Sensors HAL. | 
|  | 213 | if (mEventQueueFlag != nullptr) { | 
|  | 214 | mEventQueueFlag->wake(asBaseType(EventQueueFlagBits::EVENTS_READ)); | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | for (size_t i = 0; i < eventsToRead; i++) { | 
|  | 218 | convertToSensorEvent(mEventBuffer[i], &buffer[i]); | 
|  | 219 | android::SensorDeviceUtils::quantizeSensorEventValues(&buffer[i], | 
|  | 220 | getResolutionForSensor( | 
|  | 221 | buffer[i].sensor)); | 
|  | 222 | } | 
|  | 223 | eventsRead = eventsToRead; | 
|  | 224 | } else { | 
|  | 225 | ALOGW("Failed to read %zu events, currently %zu events available", eventsToRead, | 
|  | 226 | availableEvents); | 
|  | 227 | } | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | return eventsRead; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | std::vector<sensor_t> HidlSensorHalWrapper::getSensorsList() { | 
|  | 234 | std::vector<sensor_t> sensorsFound; | 
|  | 235 | if (mSensors != nullptr) { | 
|  | 236 | checkReturn(mSensors->getSensorsList([&](const auto& list) { | 
|  | 237 | for (size_t i = 0; i < list.size(); i++) { | 
|  | 238 | sensor_t sensor; | 
|  | 239 | convertToSensor(list[i], &sensor); | 
|  | 240 | sensorsFound.push_back(sensor); | 
|  | 241 |  | 
|  | 242 | // Only disable all sensors on HAL 1.0 since HAL 2.0 | 
|  | 243 | // handles this in its initialize method | 
|  | 244 | if (!mSensors->supportsMessageQueues()) { | 
|  | 245 | checkReturn(mSensors->activate(list[i].sensorHandle, 0 /* enabled */)); | 
|  | 246 | } | 
|  | 247 | } | 
|  | 248 | })); | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | return sensorsFound; | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | status_t HidlSensorHalWrapper::setOperationMode(SensorService::Mode mode) { | 
|  | 255 | if (mSensors == nullptr) return NO_INIT; | 
|  | 256 | return checkReturnAndGetStatus( | 
|  | 257 | mSensors->setOperationMode(static_cast<hardware::sensors::V1_0::OperationMode>(mode))); | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | status_t HidlSensorHalWrapper::activate(int32_t sensorHandle, bool enabled) { | 
|  | 261 | if (mSensors == nullptr) return NO_INIT; | 
|  | 262 | return checkReturnAndGetStatus(mSensors->activate(sensorHandle, enabled)); | 
|  | 263 | } | 
|  | 264 |  | 
|  | 265 | status_t HidlSensorHalWrapper::batch(int32_t sensorHandle, int64_t samplingPeriodNs, | 
|  | 266 | int64_t maxReportLatencyNs) { | 
|  | 267 | if (mSensors == nullptr) return NO_INIT; | 
|  | 268 | return checkReturnAndGetStatus( | 
|  | 269 | mSensors->batch(sensorHandle, samplingPeriodNs, maxReportLatencyNs)); | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | status_t HidlSensorHalWrapper::flush(int32_t sensorHandle) { | 
|  | 273 | if (mSensors == nullptr) return NO_INIT; | 
|  | 274 | return checkReturnAndGetStatus(mSensors->flush(sensorHandle)); | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | status_t HidlSensorHalWrapper::injectSensorData(const sensors_event_t* event) { | 
|  | 278 | if (mSensors == nullptr) return NO_INIT; | 
|  | 279 |  | 
|  | 280 | Event ev; | 
|  | 281 | convertFromSensorEvent(*event, &ev); | 
|  | 282 | return checkReturnAndGetStatus(mSensors->injectSensorData(ev)); | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | status_t HidlSensorHalWrapper::registerDirectChannel(const sensors_direct_mem_t* memory, | 
|  | 286 | int32_t* /*channelHandle*/) { | 
|  | 287 | if (mSensors == nullptr) return NO_INIT; | 
|  | 288 |  | 
|  | 289 | SharedMemType type; | 
|  | 290 | switch (memory->type) { | 
|  | 291 | case SENSOR_DIRECT_MEM_TYPE_ASHMEM: | 
|  | 292 | type = SharedMemType::ASHMEM; | 
|  | 293 | break; | 
|  | 294 | case SENSOR_DIRECT_MEM_TYPE_GRALLOC: | 
|  | 295 | type = SharedMemType::GRALLOC; | 
|  | 296 | break; | 
|  | 297 | default: | 
|  | 298 | return BAD_VALUE; | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | SharedMemFormat format; | 
|  | 302 | if (memory->format != SENSOR_DIRECT_FMT_SENSORS_EVENT) { | 
|  | 303 | return BAD_VALUE; | 
|  | 304 | } | 
|  | 305 | format = SharedMemFormat::SENSORS_EVENT; | 
|  | 306 |  | 
|  | 307 | SharedMemInfo mem = { | 
|  | 308 | .type = type, | 
|  | 309 | .format = format, | 
|  | 310 | .size = static_cast<uint32_t>(memory->size), | 
|  | 311 | .memoryHandle = memory->handle, | 
|  | 312 | }; | 
|  | 313 |  | 
|  | 314 | status_t ret; | 
|  | 315 | checkReturn(mSensors->registerDirectChannel(mem, [&ret](auto result, auto channelHandle) { | 
|  | 316 | if (result == Result::OK) { | 
|  | 317 | ret = channelHandle; | 
|  | 318 | } else { | 
|  | 319 | ret = statusFromResult(result); | 
|  | 320 | } | 
|  | 321 | })); | 
|  | 322 | return ret; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | status_t HidlSensorHalWrapper::unregisterDirectChannel(int32_t channelHandle) { | 
|  | 326 | if (mSensors == nullptr) return NO_INIT; | 
|  | 327 | return checkReturnAndGetStatus(mSensors->unregisterDirectChannel(channelHandle)); | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | status_t HidlSensorHalWrapper::configureDirectChannel(int32_t sensorHandle, int32_t channelHandle, | 
|  | 331 | const struct sensors_direct_cfg_t* config) { | 
|  | 332 | if (mSensors == nullptr) return NO_INIT; | 
|  | 333 |  | 
|  | 334 | RateLevel rate; | 
|  | 335 | switch (config->rate_level) { | 
|  | 336 | case SENSOR_DIRECT_RATE_STOP: | 
|  | 337 | rate = RateLevel::STOP; | 
|  | 338 | break; | 
|  | 339 | case SENSOR_DIRECT_RATE_NORMAL: | 
|  | 340 | rate = RateLevel::NORMAL; | 
|  | 341 | break; | 
|  | 342 | case SENSOR_DIRECT_RATE_FAST: | 
|  | 343 | rate = RateLevel::FAST; | 
|  | 344 | break; | 
|  | 345 | case SENSOR_DIRECT_RATE_VERY_FAST: | 
|  | 346 | rate = RateLevel::VERY_FAST; | 
|  | 347 | break; | 
|  | 348 | default: | 
|  | 349 | return BAD_VALUE; | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | status_t ret; | 
|  | 353 | checkReturn(mSensors->configDirectReport(sensorHandle, channelHandle, rate, | 
|  | 354 | [&ret, rate](auto result, auto token) { | 
|  | 355 | if (rate == RateLevel::STOP) { | 
|  | 356 | ret = statusFromResult(result); | 
|  | 357 | } else { | 
|  | 358 | if (result == Result::OK) { | 
|  | 359 | ret = token; | 
|  | 360 | } else { | 
|  | 361 | ret = statusFromResult(result); | 
|  | 362 | } | 
|  | 363 | } | 
|  | 364 | })); | 
|  | 365 |  | 
|  | 366 | return ret; | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | void HidlSensorHalWrapper::writeWakeLockHandled(uint32_t count) { | 
|  | 370 | if (mWakeLockQueue->write(&count)) { | 
|  | 371 | mWakeLockQueueFlag->wake(asBaseType(WakeLockQueueFlagBits::DATA_WRITTEN)); | 
|  | 372 | } else { | 
|  | 373 | ALOGW("Failed to write wake lock handled"); | 
|  | 374 | } | 
|  | 375 | } | 
|  | 376 |  | 
| Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 377 | status_t HidlSensorHalWrapper::checkReturnAndGetStatus(const hardware::Return<Result>& ret) { | 
|  | 378 | checkReturn(ret); | 
|  | 379 | return (!ret.isOk()) ? DEAD_OBJECT : statusFromResult(ret); | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | void HidlSensorHalWrapper::handleHidlDeath(const std::string& detail) { | 
|  | 383 | if (!mSensors->supportsMessageQueues()) { | 
|  | 384 | // restart is the only option at present. | 
|  | 385 | LOG_ALWAYS_FATAL("Abort due to ISensors hidl service failure, detail: %s.", detail.c_str()); | 
|  | 386 | } else { | 
|  | 387 | ALOGD("ISensors HAL died, death recipient will attempt reconnect"); | 
|  | 388 | } | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 | bool HidlSensorHalWrapper::connectHidlService() { | 
|  | 392 | HalConnectionStatus status = connectHidlServiceV2_1(); | 
|  | 393 | if (status == HalConnectionStatus::DOES_NOT_EXIST) { | 
|  | 394 | status = connectHidlServiceV2_0(); | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 | if (status == HalConnectionStatus::DOES_NOT_EXIST) { | 
|  | 398 | status = connectHidlServiceV1_0(); | 
|  | 399 | } | 
|  | 400 | return (status == HalConnectionStatus::CONNECTED); | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | ISensorHalWrapper::HalConnectionStatus HidlSensorHalWrapper::connectHidlServiceV1_0() { | 
|  | 404 | // SensorDevice will wait for HAL service to start if HAL is declared in device manifest. | 
|  | 405 | size_t retry = 10; | 
|  | 406 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; | 
|  | 407 |  | 
|  | 408 | while (retry-- > 0) { | 
|  | 409 | sp<android::hardware::sensors::V1_0::ISensors> sensors = | 
|  | 410 | android::hardware::sensors::V1_0::ISensors::getService(); | 
|  | 411 | if (sensors == nullptr) { | 
|  | 412 | // no sensor hidl service found | 
|  | 413 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; | 
|  | 414 | break; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | mSensors = new ISensorsWrapperV1_0(sensors); | 
|  | 418 | mRestartWaiter->reset(); | 
|  | 419 | // Poke ISensor service. If it has lingering connection from previous generation of | 
|  | 420 | // system server, it will kill itself. There is no intention to handle the poll result, | 
|  | 421 | // which will be done since the size is 0. | 
|  | 422 | if (mSensors->poll(0, [](auto, const auto&, const auto&) {}).isOk()) { | 
|  | 423 | // ok to continue | 
|  | 424 | connectionStatus = HalConnectionStatus::CONNECTED; | 
|  | 425 | break; | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | // hidl service is restarting, pointer is invalid. | 
|  | 429 | mSensors = nullptr; | 
|  | 430 | connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT; | 
|  | 431 | ALOGI("%s unsuccessful, remaining retry %zu.", __FUNCTION__, retry); | 
|  | 432 | mRestartWaiter->wait(); | 
|  | 433 | } | 
|  | 434 |  | 
|  | 435 | return connectionStatus; | 
|  | 436 | } | 
|  | 437 |  | 
|  | 438 | ISensorHalWrapper::HalConnectionStatus HidlSensorHalWrapper::connectHidlServiceV2_0() { | 
|  | 439 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; | 
|  | 440 | sp<android::hardware::sensors::V2_0::ISensors> sensors = | 
|  | 441 | android::hardware::sensors::V2_0::ISensors::getService(); | 
|  | 442 |  | 
|  | 443 | if (sensors == nullptr) { | 
|  | 444 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; | 
|  | 445 | } else { | 
|  | 446 | mSensors = new ISensorsWrapperV2_0(sensors); | 
|  | 447 | connectionStatus = initializeHidlServiceV2_X(); | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | return connectionStatus; | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | ISensorHalWrapper::HalConnectionStatus HidlSensorHalWrapper::connectHidlServiceV2_1() { | 
|  | 454 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; | 
|  | 455 | sp<android::hardware::sensors::V2_1::ISensors> sensors = | 
|  | 456 | android::hardware::sensors::V2_1::ISensors::getService(); | 
|  | 457 |  | 
|  | 458 | if (sensors == nullptr) { | 
|  | 459 | connectionStatus = HalConnectionStatus::DOES_NOT_EXIST; | 
|  | 460 | } else { | 
|  | 461 | mSensors = new ISensorsWrapperV2_1(sensors); | 
|  | 462 | connectionStatus = initializeHidlServiceV2_X(); | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | return connectionStatus; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | ISensorHalWrapper::HalConnectionStatus HidlSensorHalWrapper::initializeHidlServiceV2_X() { | 
|  | 469 | HalConnectionStatus connectionStatus = HalConnectionStatus::UNKNOWN; | 
|  | 470 |  | 
|  | 471 | mWakeLockQueue = | 
|  | 472 | std::make_unique<WakeLockQueue>(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT, | 
|  | 473 | true /* configureEventFlagWord */); | 
|  | 474 |  | 
|  | 475 | hardware::EventFlag::deleteEventFlag(&mEventQueueFlag); | 
|  | 476 | hardware::EventFlag::createEventFlag(mSensors->getEventQueue()->getEventFlagWord(), | 
|  | 477 | &mEventQueueFlag); | 
|  | 478 |  | 
|  | 479 | hardware::EventFlag::deleteEventFlag(&mWakeLockQueueFlag); | 
|  | 480 | hardware::EventFlag::createEventFlag(mWakeLockQueue->getEventFlagWord(), &mWakeLockQueueFlag); | 
|  | 481 |  | 
|  | 482 | CHECK(mSensors != nullptr && mWakeLockQueue != nullptr && mEventQueueFlag != nullptr && | 
|  | 483 | mWakeLockQueueFlag != nullptr); | 
|  | 484 |  | 
|  | 485 | mCallback = new SensorsCallback(mSensorDeviceCallback); | 
|  | 486 | status_t status = | 
|  | 487 | checkReturnAndGetStatus(mSensors->initialize(*mWakeLockQueue->getDesc(), mCallback)); | 
|  | 488 |  | 
|  | 489 | if (status != NO_ERROR) { | 
|  | 490 | connectionStatus = HalConnectionStatus::FAILED_TO_CONNECT; | 
|  | 491 | ALOGE("Failed to initialize Sensors HAL (%s)", strerror(-status)); | 
|  | 492 | } else { | 
|  | 493 | connectionStatus = HalConnectionStatus::CONNECTED; | 
|  | 494 | mSensorsHalDeathReceiver = new SensorsHalDeathReceiver(this); | 
|  | 495 | mSensors->linkToDeath(mSensorsHalDeathReceiver, 0 /* cookie */); | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | return connectionStatus; | 
|  | 499 | } | 
|  | 500 |  | 
|  | 501 | void HidlSensorHalWrapper::convertToSensorEvent(const Event& src, sensors_event_t* dst) { | 
|  | 502 | android::hardware::sensors::V2_1::implementation::convertToSensorEvent(src, dst); | 
|  | 503 |  | 
|  | 504 | if (src.sensorType == android::hardware::sensors::V2_1::SensorType::DYNAMIC_SENSOR_META) { | 
|  | 505 | const hardware::sensors::V1_0::DynamicSensorInfo& dyn = src.u.dynamic; | 
|  | 506 |  | 
|  | 507 | dst->dynamic_sensor_meta.connected = dyn.connected; | 
|  | 508 | dst->dynamic_sensor_meta.handle = dyn.sensorHandle; | 
|  | 509 | if (dyn.connected) { | 
|  | 510 | std::unique_lock<std::mutex> lock(mDynamicSensorsMutex); | 
|  | 511 | // Give MAX_DYN_SENSOR_WAIT_SEC for onDynamicSensorsConnected to be invoked since it | 
|  | 512 | // can be received out of order from this event due to a bug in the HIDL spec that | 
|  | 513 | // marks it as oneway. | 
|  | 514 | auto it = mConnectedDynamicSensors.find(dyn.sensorHandle); | 
|  | 515 | if (it == mConnectedDynamicSensors.end()) { | 
|  | 516 | mDynamicSensorsCv.wait_for(lock, MAX_DYN_SENSOR_WAIT, [&, dyn] { | 
|  | 517 | return mConnectedDynamicSensors.find(dyn.sensorHandle) != | 
|  | 518 | mConnectedDynamicSensors.end(); | 
|  | 519 | }); | 
|  | 520 | it = mConnectedDynamicSensors.find(dyn.sensorHandle); | 
|  | 521 | CHECK(it != mConnectedDynamicSensors.end()); | 
|  | 522 | } | 
|  | 523 |  | 
|  | 524 | dst->dynamic_sensor_meta.sensor = &it->second; | 
|  | 525 |  | 
|  | 526 | memcpy(dst->dynamic_sensor_meta.uuid, dyn.uuid.data(), | 
|  | 527 | sizeof(dst->dynamic_sensor_meta.uuid)); | 
|  | 528 | } | 
|  | 529 | } | 
|  | 530 | } | 
|  | 531 |  | 
|  | 532 | void HidlSensorHalWrapper::convertToSensorEventsAndQuantize( | 
|  | 533 | const hidl_vec<Event>& src, const hidl_vec<SensorInfo>& dynamicSensorsAdded, | 
|  | 534 | sensors_event_t* dst) { | 
|  | 535 | if (dynamicSensorsAdded.size() > 0 && mCallback != nullptr) { | 
|  | 536 | mCallback->onDynamicSensorsConnected_2_1(dynamicSensorsAdded); | 
|  | 537 | } | 
|  | 538 |  | 
|  | 539 | for (size_t i = 0; i < src.size(); ++i) { | 
|  | 540 | android::hardware::sensors::V2_1::implementation::convertToSensorEvent(src[i], &dst[i]); | 
|  | 541 | android::SensorDeviceUtils::quantizeSensorEventValues(&dst[i], | 
|  | 542 | getResolutionForSensor( | 
|  | 543 | dst[i].sensor)); | 
|  | 544 | } | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | float HidlSensorHalWrapper::getResolutionForSensor(int sensorHandle) { | 
|  | 548 | for (size_t i = 0; i < mSensorList.size(); i++) { | 
|  | 549 | if (sensorHandle == mSensorList[i].handle) { | 
|  | 550 | return mSensorList[i].resolution; | 
|  | 551 | } | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | auto it = mConnectedDynamicSensors.find(sensorHandle); | 
|  | 555 | if (it != mConnectedDynamicSensors.end()) { | 
|  | 556 | return it->second.resolution; | 
|  | 557 | } | 
|  | 558 |  | 
|  | 559 | return 0; | 
|  | 560 | } | 
|  | 561 |  | 
|  | 562 | } // namespace android |