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 | #ifndef ANDROID_HIDL_SENSOR_HAL_WRAPPER_H |
| 18 | #define ANDROID_HIDL_SENSOR_HAL_WRAPPER_H |
| 19 | |
| 20 | #include <sensor/SensorEventQueue.h> |
| 21 | #include <utils/Singleton.h> |
| 22 | |
| 23 | #include "ISensorHalWrapper.h" |
| 24 | |
| 25 | #include "ISensorsWrapper.h" |
| 26 | #include "SensorDeviceUtils.h" |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | using android::hardware::sensors::V1_0::Result; |
| 31 | using android::hardware::sensors::V2_1::Event; |
| 32 | using android::hardware::sensors::V2_1::SensorInfo; |
| 33 | |
| 34 | class HidlTransportErrorLog { |
| 35 | public: |
| 36 | HidlTransportErrorLog() { |
| 37 | mTs = 0; |
| 38 | mCount = 0; |
| 39 | } |
| 40 | |
| 41 | HidlTransportErrorLog(time_t ts, int count) { |
| 42 | mTs = ts; |
| 43 | mCount = count; |
| 44 | } |
| 45 | |
| 46 | String8 toString() const { |
| 47 | String8 result; |
| 48 | struct tm* timeInfo = localtime(&mTs); |
| 49 | result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min, |
| 50 | timeInfo->tm_sec, mCount); |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | time_t mTs; // timestamp of the error |
| 56 | int mCount; // number of transport errors observed |
| 57 | }; |
| 58 | |
| 59 | class SensorsHalDeathReceiver : public android::hardware::hidl_death_recipient { |
| 60 | public: |
| 61 | SensorsHalDeathReceiver(ISensorHalWrapper* wrapper) : mHidlSensorHalWrapper(wrapper) {} |
| 62 | |
| 63 | virtual void serviceDied(uint64_t cookie, |
| 64 | const wp<::android::hidl::base::V1_0::IBase>& service) override; |
| 65 | |
| 66 | private: |
| 67 | ISensorHalWrapper* mHidlSensorHalWrapper; |
| 68 | }; |
| 69 | |
| 70 | class HidlSensorHalWrapper : public ISensorHalWrapper { |
| 71 | public: |
| 72 | HidlSensorHalWrapper() |
| 73 | : mHidlTransportErrors(20), |
Greg Kaiser | cda575a | 2021-12-15 07:48:15 -0800 | [diff] [blame] | 74 | mTotalHidlTransportErrors(0), |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 75 | mRestartWaiter(new SensorDeviceUtils::HidlServiceRegistrationWaiter()), |
| 76 | mEventQueueFlag(nullptr), |
| 77 | mWakeLockQueueFlag(nullptr) {} |
| 78 | |
| 79 | ~HidlSensorHalWrapper() override { |
| 80 | if (mEventQueueFlag != nullptr) { |
| 81 | hardware::EventFlag::deleteEventFlag(&mEventQueueFlag); |
| 82 | mEventQueueFlag = nullptr; |
| 83 | } |
| 84 | if (mWakeLockQueueFlag != nullptr) { |
| 85 | hardware::EventFlag::deleteEventFlag(&mWakeLockQueueFlag); |
| 86 | mWakeLockQueueFlag = nullptr; |
| 87 | } |
| 88 | } |
| 89 | virtual bool connect(SensorDeviceCallback* callback) override; |
| 90 | |
| 91 | virtual void prepareForReconnect() override; |
| 92 | |
| 93 | virtual bool supportsPolling() override; |
| 94 | |
| 95 | virtual bool supportsMessageQueues() override; |
| 96 | |
| 97 | virtual ssize_t poll(sensors_event_t* buffer, size_t count) override; |
| 98 | |
| 99 | virtual ssize_t pollFmq(sensors_event_t* buffer, size_t count) override; |
| 100 | |
| 101 | virtual std::vector<sensor_t> getSensorsList() override; |
| 102 | |
| 103 | virtual status_t setOperationMode(SensorService::Mode mode) override; |
| 104 | |
| 105 | virtual status_t activate(int32_t sensorHandle, bool enabled) override; |
| 106 | |
| 107 | virtual status_t batch(int32_t sensorHandle, int64_t samplingPeriodNs, |
| 108 | int64_t maxReportLatencyNs) override; |
| 109 | |
| 110 | virtual status_t flush(int32_t sensorHandle) override; |
| 111 | |
| 112 | virtual status_t injectSensorData(const sensors_event_t* event) override; |
| 113 | |
| 114 | virtual status_t registerDirectChannel(const sensors_direct_mem_t* memory, |
| 115 | int32_t* channelHandle) override; |
| 116 | |
| 117 | virtual status_t unregisterDirectChannel(int32_t channelHandle) override; |
| 118 | |
| 119 | virtual status_t configureDirectChannel(int32_t sensorHandle, int32_t channelHandle, |
| 120 | const struct sensors_direct_cfg_t* config) override; |
| 121 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 122 | virtual void writeWakeLockHandled(uint32_t count) override; |
| 123 | |
| 124 | private: |
| 125 | sp<::android::hardware::sensors::V2_1::implementation::ISensorsWrapperBase> mSensors; |
| 126 | sp<::android::hardware::sensors::V2_1::ISensorsCallback> mCallback; |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 127 | |
| 128 | // Keep track of any hidl transport failures |
| 129 | SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors; |
| 130 | int mTotalHidlTransportErrors; |
| 131 | |
| 132 | SensorDeviceCallback* mSensorDeviceCallback = nullptr; |
| 133 | |
| 134 | // TODO(b/67425500): remove waiter after bug is resolved. |
| 135 | sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter; |
| 136 | |
| 137 | template <typename T> |
| 138 | void checkReturn(const hardware::Return<T>& ret) { |
| 139 | if (!ret.isOk()) { |
| 140 | handleHidlDeath(ret.description()); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | status_t checkReturnAndGetStatus(const hardware::Return<Result>& ret); |
| 145 | |
| 146 | void handleHidlDeath(const std::string& detail); |
| 147 | |
| 148 | void convertToSensorEvent(const Event& src, sensors_event_t* dst); |
| 149 | |
Arthur Ishiguro | 65b5981 | 2021-12-26 19:09:54 +0000 | [diff] [blame^] | 150 | void convertToSensorEvents(const hardware::hidl_vec<Event>& src, |
| 151 | const hardware::hidl_vec<SensorInfo>& dynamicSensorsAdded, |
| 152 | sensors_event_t* dst); |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 153 | |
| 154 | bool connectHidlService(); |
| 155 | |
| 156 | HalConnectionStatus connectHidlServiceV1_0(); |
| 157 | HalConnectionStatus connectHidlServiceV2_0(); |
| 158 | HalConnectionStatus connectHidlServiceV2_1(); |
| 159 | HalConnectionStatus initializeHidlServiceV2_X(); |
| 160 | |
| 161 | typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue; |
| 162 | std::unique_ptr<WakeLockQueue> mWakeLockQueue; |
| 163 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame] | 164 | hardware::EventFlag* mEventQueueFlag; |
| 165 | hardware::EventFlag* mWakeLockQueueFlag; |
| 166 | |
| 167 | std::array<Event, SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer; |
| 168 | |
| 169 | sp<SensorsHalDeathReceiver> mSensorsHalDeathReceiver; |
| 170 | }; |
| 171 | |
| 172 | } // namespace android |
| 173 | |
| 174 | #endif // ANDROID_HIDL_SENSOR_HAL_WRAPPER_H |