blob: 6542c6acf9746c5b5cfa6662a1aaa97a1f16ddf8 [file] [log] [blame]
Arthur Ishiguro24804dc2021-11-12 17:17:09 +00001/*
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
28namespace android {
29
30using android::hardware::sensors::V1_0::Result;
31using android::hardware::sensors::V2_1::Event;
32using android::hardware::sensors::V2_1::SensorInfo;
33
34class HidlTransportErrorLog {
35public:
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
54private:
55 time_t mTs; // timestamp of the error
56 int mCount; // number of transport errors observed
57};
58
59class SensorsHalDeathReceiver : public android::hardware::hidl_death_recipient {
60public:
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
66private:
67 ISensorHalWrapper* mHidlSensorHalWrapper;
68};
69
70class HidlSensorHalWrapper : public ISensorHalWrapper {
71public:
72 HidlSensorHalWrapper()
73 : mHidlTransportErrors(20),
74 mRestartWaiter(new SensorDeviceUtils::HidlServiceRegistrationWaiter()),
75 mEventQueueFlag(nullptr),
76 mWakeLockQueueFlag(nullptr) {}
77
78 ~HidlSensorHalWrapper() override {
79 if (mEventQueueFlag != nullptr) {
80 hardware::EventFlag::deleteEventFlag(&mEventQueueFlag);
81 mEventQueueFlag = nullptr;
82 }
83 if (mWakeLockQueueFlag != nullptr) {
84 hardware::EventFlag::deleteEventFlag(&mWakeLockQueueFlag);
85 mWakeLockQueueFlag = nullptr;
86 }
87 }
88 virtual bool connect(SensorDeviceCallback* callback) override;
89
90 virtual void prepareForReconnect() override;
91
92 virtual bool supportsPolling() override;
93
94 virtual bool supportsMessageQueues() override;
95
96 virtual ssize_t poll(sensors_event_t* buffer, size_t count) override;
97
98 virtual ssize_t pollFmq(sensors_event_t* buffer, size_t count) override;
99
100 virtual std::vector<sensor_t> getSensorsList() override;
101
102 virtual status_t setOperationMode(SensorService::Mode mode) override;
103
104 virtual status_t activate(int32_t sensorHandle, bool enabled) override;
105
106 virtual status_t batch(int32_t sensorHandle, int64_t samplingPeriodNs,
107 int64_t maxReportLatencyNs) override;
108
109 virtual status_t flush(int32_t sensorHandle) override;
110
111 virtual status_t injectSensorData(const sensors_event_t* event) override;
112
113 virtual status_t registerDirectChannel(const sensors_direct_mem_t* memory,
114 int32_t* channelHandle) override;
115
116 virtual status_t unregisterDirectChannel(int32_t channelHandle) override;
117
118 virtual status_t configureDirectChannel(int32_t sensorHandle, int32_t channelHandle,
119 const struct sensors_direct_cfg_t* config) override;
120
121 virtual void onDynamicSensorsConnected(
122 const std::vector<sensor_t>& dynamicSensorsAdded) override;
123
124 virtual void onDynamicSensorsDisconnected(
125 const std::vector<int32_t>& dynamicSensorHandlesRemoved) override;
126
127 virtual void writeWakeLockHandled(uint32_t count) override;
128
129private:
130 sp<::android::hardware::sensors::V2_1::implementation::ISensorsWrapperBase> mSensors;
131 sp<::android::hardware::sensors::V2_1::ISensorsCallback> mCallback;
132 std::vector<sensor_t> mSensorList;
133 std::unordered_map<int32_t, sensor_t> mConnectedDynamicSensors;
134
135 std::mutex mDynamicSensorsMutex;
136 std::condition_variable mDynamicSensorsCv;
137 static constexpr std::chrono::seconds MAX_DYN_SENSOR_WAIT{5};
138
139 // Keep track of any hidl transport failures
140 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
141 int mTotalHidlTransportErrors;
142
143 SensorDeviceCallback* mSensorDeviceCallback = nullptr;
144
145 // TODO(b/67425500): remove waiter after bug is resolved.
146 sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter;
147
148 template <typename T>
149 void checkReturn(const hardware::Return<T>& ret) {
150 if (!ret.isOk()) {
151 handleHidlDeath(ret.description());
152 }
153 }
154
155 status_t checkReturnAndGetStatus(const hardware::Return<Result>& ret);
156
157 void handleHidlDeath(const std::string& detail);
158
159 void convertToSensorEvent(const Event& src, sensors_event_t* dst);
160
161 void convertToSensorEventsAndQuantize(const hardware::hidl_vec<Event>& src,
162 const hardware::hidl_vec<SensorInfo>& dynamicSensorsAdded,
163 sensors_event_t* dst);
164
165 bool connectHidlService();
166
167 HalConnectionStatus connectHidlServiceV1_0();
168 HalConnectionStatus connectHidlServiceV2_0();
169 HalConnectionStatus connectHidlServiceV2_1();
170 HalConnectionStatus initializeHidlServiceV2_X();
171
172 typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue;
173 std::unique_ptr<WakeLockQueue> mWakeLockQueue;
174
175 float getResolutionForSensor(int sensorHandle);
176
177 hardware::EventFlag* mEventQueueFlag;
178 hardware::EventFlag* mWakeLockQueueFlag;
179
180 std::array<Event, SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer;
181
182 sp<SensorsHalDeathReceiver> mSensorsHalDeathReceiver;
183};
184
185} // namespace android
186
187#endif // ANDROID_HIDL_SENSOR_HAL_WRAPPER_H