blob: 24d03c63c265e017130c1ebaa4c385badb5125a7 [file] [log] [blame]
Mathias Agopianf001c922010-11-11 17:58:51 -08001/*
2 * Copyright (C) 2010 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_SENSOR_DEVICE_H
18#define ANDROID_SENSOR_DEVICE_H
19
Peng Xu1a00e2d2017-09-27 23:08:30 -070020#include "SensorDeviceUtils.h"
Peng Xu6a2d3a02015-12-21 12:00:23 -080021#include "SensorServiceUtils.h"
Anthony Stangee38a1412020-02-13 21:28:37 -050022#include "ISensorsWrapper.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080023
Brian Stack979887b2018-09-19 15:27:48 -070024#include <fmq/MessageQueue.h>
Brian Stacka28e9212018-09-19 15:20:30 -070025#include <sensor/SensorEventQueue.h>
Mathias Agopian801ea092017-03-06 15:05:04 -080026#include <sensor/Sensor.h>
Steven Morelandd15c0302016-12-20 11:14:50 -080027#include <stdint.h>
28#include <sys/types.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080029#include <utils/KeyedVector.h>
30#include <utils/Singleton.h>
31#include <utils/String8.h>
Brian Stacka28e9212018-09-19 15:20:30 -070032#include <utils/Timers.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080033
Peng Xue36e3472016-11-03 11:57:10 -070034#include <string>
Peng Xu2bec6232016-07-01 17:13:10 -070035#include <unordered_map>
36#include <algorithm> //std::max std::min
Andreas Huber99fdbb52016-10-10 13:22:58 -070037
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070038#include "RingBuffer.h"
39
Mathias Agopianf001c922010-11-11 17:58:51 -080040// ---------------------------------------------------------------------------
41
42namespace android {
Andreas Huber99fdbb52016-10-10 13:22:58 -070043
Mathias Agopianf001c922010-11-11 17:58:51 -080044// ---------------------------------------------------------------------------
Brian Stack574cda32018-10-01 11:18:51 -070045class SensorsHalDeathReceivier : public android::hardware::hidl_death_recipient {
46 virtual void serviceDied(uint64_t cookie,
47 const wp<::android::hidl::base::V1_0::IBase>& service) override;
48};
Mathias Agopianf001c922010-11-11 17:58:51 -080049
Brian Stack6c49e6f2018-09-24 15:44:32 -070050class SensorDevice : public Singleton<SensorDevice>,
Brian Stack6c49e6f2018-09-24 15:44:32 -070051 public SensorServiceUtil::Dumpable {
Peng Xu6a2d3a02015-12-21 12:00:23 -080052public:
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070053 class HidlTransportErrorLog {
54 public:
55
56 HidlTransportErrorLog() {
57 mTs = 0;
58 mCount = 0;
59 }
60
61 HidlTransportErrorLog(time_t ts, int count) {
62 mTs = ts;
63 mCount = count;
64 }
65
66 String8 toString() const {
67 String8 result;
68 struct tm *timeInfo = localtime(&mTs);
69 result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min,
70 timeInfo->tm_sec, mCount);
71 return result;
72 }
73
74 private:
75 time_t mTs; // timestamp of the error
76 int mCount; // number of transport errors observed
77 };
78
Brian Stacka28e9212018-09-19 15:20:30 -070079 ~SensorDevice();
Brian Stack156da082018-10-01 15:58:53 -070080 void prepareForReconnect();
81 void reconnect();
Brian Stacka28e9212018-09-19 15:20:30 -070082
Peng Xu6a2d3a02015-12-21 12:00:23 -080083 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070084
Peng Xu6a2d3a02015-12-21 12:00:23 -080085 void handleDynamicSensorConnection(int handle, bool connected);
86 status_t initCheck() const;
87 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070088
Peng Xu6a2d3a02015-12-21 12:00:23 -080089 ssize_t poll(sensors_event_t* buffer, size_t count);
Brian Stackb7bfc0f2018-09-25 09:41:16 -070090 void writeWakeLockHandled(uint32_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070091
Peng Xu6a2d3a02015-12-21 12:00:23 -080092 status_t activate(void* ident, int handle, int enabled);
93 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
94 int64_t maxBatchReportLatencyNs);
95 // Call batch with timeout zero instead of calling setDelay() for newer devices.
96 status_t setDelay(void* ident, int handle, int64_t ns);
97 status_t flush(void* ident, int handle);
98 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070099
Peng Xu53632542017-01-23 20:06:27 -0800100 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -0700101 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
102 void unregisterDirectChannel(int32_t channelHandle);
103 int32_t configureDirectChannel(int32_t sensorHandle,
104 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
105
Peng Xu6a2d3a02015-12-21 12:00:23 -0800106 void disableAllSensors();
107 void enableAllSensors();
108 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700109
Peng Xu6a2d3a02015-12-21 12:00:23 -0800110 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -0700111 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -0800112
Brian Stack6c49e6f2018-09-24 15:44:32 -0700113 using Result = ::android::hardware::sensors::V1_0::Result;
114 hardware::Return<void> onDynamicSensorsConnected(
Anthony Stangee38a1412020-02-13 21:28:37 -0500115 const hardware::hidl_vec<hardware::sensors::V2_1::SensorInfo> &dynamicSensorsAdded);
Brian Stack6c49e6f2018-09-24 15:44:32 -0700116 hardware::Return<void> onDynamicSensorsDisconnected(
Brian Stackbbab1ea2018-10-01 10:49:07 -0700117 const hardware::hidl_vec<int32_t> &dynamicSensorHandlesRemoved);
Brian Stack6c49e6f2018-09-24 15:44:32 -0700118
Brian Stack156da082018-10-01 15:58:53 -0700119 bool isReconnecting() const {
120 return mReconnecting;
121 }
122
Brian Stackbce04d72019-03-21 10:54:10 -0700123 bool isSensorActive(int handle) const;
124
Peng Xu6a2d3a02015-12-21 12:00:23 -0800125 // Dumpable
Mike Ma24743862020-01-29 00:36:55 -0800126 virtual std::string dump() const override;
127 virtual void dump(util::ProtoOutputStream* proto) const override;
Peng Xu6a2d3a02015-12-21 12:00:23 -0800128private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800129 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800130
Anthony Stangee38a1412020-02-13 21:28:37 -0500131 sp<::android::hardware::sensors::V2_1::implementation::ISensorsWrapperBase> mSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700132 Vector<sensor_t> mSensorList;
Peng Xu2bec6232016-07-01 17:13:10 -0700133 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700134
Aravind Akella724d91d2013-06-27 12:04:23 -0700135 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
136 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800137 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700138
139 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
140 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
141 struct BatchParams {
Peng Xu2bec6232016-07-01 17:13:10 -0700142 nsecs_t mTSample, mTBatch;
143 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
144 BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700145 bool operator != (const BatchParams& other) {
Peng Xu2bec6232016-07-01 17:13:10 -0700146 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
147 }
148 // Merge another parameter with this one. The updated mTSample will be the min of the two.
149 // The update mTBatch will be the min of original mTBatch and the apparent batch period
150 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
151 void merge(const BatchParams &other) {
152 mTSample = std::min(mTSample, other.mTSample);
153 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
Aravind Akella724d91d2013-06-27 12:04:23 -0700154 }
155 };
156
157 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
158 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
159 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
160 // mode request is batch(... timeout > 0 ...) followed by activate().
161 // Info is a per-sensor data structure which contains the batch parameters for each client that
162 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800163 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700164 BatchParams bestBatchParams;
165 // Key is the unique identifier(ident) for each client, value is the batch parameters
166 // requested by the client.
167 KeyedVector<void*, BatchParams> batchParams;
168
Brian Stack0c305fe2019-04-09 12:49:24 -0700169 // Flag to track if the sensor is active
170 bool isActive = false;
171
Aravind Akella724d91d2013-06-27 12:04:23 -0700172 // Sets batch parameters for this ident. Returns error if this ident is not already present
173 // in the KeyedVector above.
174 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
175 int64_t maxBatchReportLatencyNs);
176 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
177 void selectBatchParams();
178 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
179 // the removed ident. If index >=0, ident is present and successfully removed.
180 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800181
Brian Stack156da082018-10-01 15:58:53 -0700182 int numActiveClients() const;
Mathias Agopianf001c922010-11-11 17:58:51 -0800183 };
184 DefaultKeyedVector<int, Info> mActivationCount;
185
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700186 // Keep track of any hidl transport failures
187 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
188 int mTotalHidlTransportErrors;
189
Aravind Akella4949c502015-02-11 15:54:35 -0800190 // Use this vector to determine which client is activated or deactivated.
191 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800192 SensorDevice();
Peng Xua8fad9b2017-03-17 12:20:27 -0700193 bool connectHidlService();
Brian Stack156da082018-10-01 15:58:53 -0700194 void initializeSensorList();
195 void reactivateSensors(const DefaultKeyedVector<int, Info>& previousActivations);
196 static bool sensorHandlesChanged(const Vector<sensor_t>& oldSensorList,
197 const Vector<sensor_t>& newSensorList);
198 static bool sensorIsEquivalent(const sensor_t& prevSensor, const sensor_t& newSensor);
Brian Stack979887b2018-09-19 15:27:48 -0700199
200 enum HalConnectionStatus {
201 CONNECTED, // Successfully connected to the HAL
202 DOES_NOT_EXIST, // Could not find the HAL
203 FAILED_TO_CONNECT, // Found the HAL but failed to connect/initialize
204 UNKNOWN,
205 };
206 HalConnectionStatus connectHidlServiceV1_0();
207 HalConnectionStatus connectHidlServiceV2_0();
Anthony Stangee38a1412020-02-13 21:28:37 -0500208 HalConnectionStatus connectHidlServiceV2_1();
209 HalConnectionStatus initializeHidlServiceV2_X();
Aravind Akella4949c502015-02-11 15:54:35 -0800210
Brian Stacka28e9212018-09-19 15:20:30 -0700211 ssize_t pollHal(sensors_event_t* buffer, size_t count);
212 ssize_t pollFmq(sensors_event_t* buffer, size_t count);
Brian Stack156da082018-10-01 15:58:53 -0700213 status_t activateLocked(void* ident, int handle, int enabled);
214 status_t batchLocked(void* ident, int handle, int flags, int64_t samplingPeriodNs,
215 int64_t maxBatchReportLatencyNs);
Brian Stacka28e9212018-09-19 15:20:30 -0700216
Brian Duddie6d2e3752019-05-30 09:52:28 -0700217 void handleHidlDeath(const std::string &detail);
Peng Xu3889e6e2017-03-02 19:10:38 -0800218 template<typename T>
Brian Duddie6d2e3752019-05-30 09:52:28 -0700219 void checkReturn(const Return<T>& ret) {
Peng Xu3889e6e2017-03-02 19:10:38 -0800220 if (!ret.isOk()) {
221 handleHidlDeath(ret.description());
222 }
Peng Xu3889e6e2017-03-02 19:10:38 -0800223 }
Brian Duddie6d2e3752019-05-30 09:52:28 -0700224 status_t checkReturnAndGetStatus(const Return<Result>& ret);
Peng Xu1a00e2d2017-09-27 23:08:30 -0700225 //TODO(b/67425500): remove waiter after bug is resolved.
226 sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter;
Peng Xu3889e6e2017-03-02 19:10:38 -0800227
Aravind Akella4949c502015-02-11 15:54:35 -0800228 bool isClientDisabled(void* ident);
229 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700230
Anthony Stangee38a1412020-02-13 21:28:37 -0500231 using Event = hardware::sensors::V2_1::Event;
232 using SensorInfo = hardware::sensors::V2_1::SensorInfo;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700233
234 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
235
Anthony Stange9b466b72020-04-20 22:37:08 +0000236 void convertToSensorEvents(
Andreas Huber99fdbb52016-10-10 13:22:58 -0700237 const hardware::hidl_vec<Event> &src,
238 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
239 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800240
241 bool mIsDirectReportSupported;
Brian Stack979887b2018-09-19 15:27:48 -0700242
Brian Stack979887b2018-09-19 15:27:48 -0700243 typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue;
Brian Stack979887b2018-09-19 15:27:48 -0700244 std::unique_ptr<WakeLockQueue> mWakeLockQueue;
Brian Stacka28e9212018-09-19 15:20:30 -0700245
246 hardware::EventFlag* mEventQueueFlag;
Brian Stacka24e7d42019-01-08 12:56:09 -0800247 hardware::EventFlag* mWakeLockQueueFlag;
Brian Stacka28e9212018-09-19 15:20:30 -0700248
249 std::array<Event, SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer;
Brian Stack574cda32018-10-01 11:18:51 -0700250
251 sp<SensorsHalDeathReceivier> mSensorsHalDeathReceiver;
Brian Stack156da082018-10-01 15:58:53 -0700252 std::atomic_bool mReconnecting;
Mathias Agopianf001c922010-11-11 17:58:51 -0800253};
254
255// ---------------------------------------------------------------------------
256}; // namespace android
257
258#endif // ANDROID_SENSOR_DEVICE_H