blob: 2caebf9adeb0b22b73c9ef36417f1e655cef0445 [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"
Brian Stack12f4d322018-09-14 16:18:59 -070022#include "SensorsWrapper.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// ---------------------------------------------------------------------------
45
Peng Xu1a00e2d2017-09-27 23:08:30 -070046class SensorDevice : public Singleton<SensorDevice>, public SensorServiceUtil::Dumpable {
Peng Xu6a2d3a02015-12-21 12:00:23 -080047public:
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070048 class HidlTransportErrorLog {
49 public:
50
51 HidlTransportErrorLog() {
52 mTs = 0;
53 mCount = 0;
54 }
55
56 HidlTransportErrorLog(time_t ts, int count) {
57 mTs = ts;
58 mCount = count;
59 }
60
61 String8 toString() const {
62 String8 result;
63 struct tm *timeInfo = localtime(&mTs);
64 result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min,
65 timeInfo->tm_sec, mCount);
66 return result;
67 }
68
69 private:
70 time_t mTs; // timestamp of the error
71 int mCount; // number of transport errors observed
72 };
73
Brian Stacka28e9212018-09-19 15:20:30 -070074 ~SensorDevice();
75
Peng Xu6a2d3a02015-12-21 12:00:23 -080076 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070077
Peng Xu6a2d3a02015-12-21 12:00:23 -080078 void handleDynamicSensorConnection(int handle, bool connected);
79 status_t initCheck() const;
80 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070081
Peng Xu6a2d3a02015-12-21 12:00:23 -080082 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070083
Peng Xu6a2d3a02015-12-21 12:00:23 -080084 status_t activate(void* ident, int handle, int enabled);
85 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
86 int64_t maxBatchReportLatencyNs);
87 // Call batch with timeout zero instead of calling setDelay() for newer devices.
88 status_t setDelay(void* ident, int handle, int64_t ns);
89 status_t flush(void* ident, int handle);
90 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070091
Peng Xu53632542017-01-23 20:06:27 -080092 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070093 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
94 void unregisterDirectChannel(int32_t channelHandle);
95 int32_t configureDirectChannel(int32_t sensorHandle,
96 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
97
Peng Xu6a2d3a02015-12-21 12:00:23 -080098 void disableAllSensors();
99 void enableAllSensors();
100 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700101
Peng Xu6a2d3a02015-12-21 12:00:23 -0800102 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -0700103 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -0800104
105 // Dumpable
106 virtual std::string dump() const;
107private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800108 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800109
Brian Stack12f4d322018-09-14 16:18:59 -0700110 sp<SensorServiceUtil::ISensorsWrapper> mSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700111 Vector<sensor_t> mSensorList;
Peng Xu2bec6232016-07-01 17:13:10 -0700112 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700113
Aravind Akella724d91d2013-06-27 12:04:23 -0700114 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
115 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800116 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700117
118 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
119 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
120 struct BatchParams {
Peng Xu2bec6232016-07-01 17:13:10 -0700121 nsecs_t mTSample, mTBatch;
122 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
123 BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700124 bool operator != (const BatchParams& other) {
Peng Xu2bec6232016-07-01 17:13:10 -0700125 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
126 }
127 // Merge another parameter with this one. The updated mTSample will be the min of the two.
128 // The update mTBatch will be the min of original mTBatch and the apparent batch period
129 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
130 void merge(const BatchParams &other) {
131 mTSample = std::min(mTSample, other.mTSample);
132 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
Aravind Akella724d91d2013-06-27 12:04:23 -0700133 }
134 };
135
136 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
137 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
138 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
139 // mode request is batch(... timeout > 0 ...) followed by activate().
140 // Info is a per-sensor data structure which contains the batch parameters for each client that
141 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800142 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700143 BatchParams bestBatchParams;
144 // Key is the unique identifier(ident) for each client, value is the batch parameters
145 // requested by the client.
146 KeyedVector<void*, BatchParams> batchParams;
147
Aravind Akella724d91d2013-06-27 12:04:23 -0700148 // Sets batch parameters for this ident. Returns error if this ident is not already present
149 // in the KeyedVector above.
150 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
151 int64_t maxBatchReportLatencyNs);
152 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
153 void selectBatchParams();
154 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
155 // the removed ident. If index >=0, ident is present and successfully removed.
156 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800157
158 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800159 };
160 DefaultKeyedVector<int, Info> mActivationCount;
161
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700162 // Keep track of any hidl transport failures
163 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
164 int mTotalHidlTransportErrors;
165
Aravind Akella4949c502015-02-11 15:54:35 -0800166 // Use this vector to determine which client is activated or deactivated.
167 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800168 SensorDevice();
Peng Xua8fad9b2017-03-17 12:20:27 -0700169 bool connectHidlService();
Brian Stack979887b2018-09-19 15:27:48 -0700170
171 enum HalConnectionStatus {
172 CONNECTED, // Successfully connected to the HAL
173 DOES_NOT_EXIST, // Could not find the HAL
174 FAILED_TO_CONNECT, // Found the HAL but failed to connect/initialize
175 UNKNOWN,
176 };
177 HalConnectionStatus connectHidlServiceV1_0();
178 HalConnectionStatus connectHidlServiceV2_0();
Aravind Akella4949c502015-02-11 15:54:35 -0800179
Brian Stacka28e9212018-09-19 15:20:30 -0700180 ssize_t pollHal(sensors_event_t* buffer, size_t count);
181 ssize_t pollFmq(sensors_event_t* buffer, size_t count);
182
Peng Xu3889e6e2017-03-02 19:10:38 -0800183 static void handleHidlDeath(const std::string &detail);
184 template<typename T>
185 static Return<T> checkReturn(Return<T> &&ret) {
186 if (!ret.isOk()) {
187 handleHidlDeath(ret.description());
188 }
189 return std::move(ret);
190 }
Peng Xu1a00e2d2017-09-27 23:08:30 -0700191 //TODO(b/67425500): remove waiter after bug is resolved.
192 sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter;
Peng Xu3889e6e2017-03-02 19:10:38 -0800193
Aravind Akella4949c502015-02-11 15:54:35 -0800194 bool isClientDisabled(void* ident);
195 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700196
Andreas Huber99fdbb52016-10-10 13:22:58 -0700197 using Event = hardware::sensors::V1_0::Event;
198 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
199
200 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
201
202 void convertToSensorEvents(
203 const hardware::hidl_vec<Event> &src,
204 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
205 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800206
207 bool mIsDirectReportSupported;
Brian Stack979887b2018-09-19 15:27:48 -0700208
209 typedef hardware::MessageQueue<Event, hardware::kSynchronizedReadWrite> EventMessageQueue;
210 typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue;
211 std::unique_ptr<EventMessageQueue> mEventQueue;
212 std::unique_ptr<WakeLockQueue> mWakeLockQueue;
Brian Stacka28e9212018-09-19 15:20:30 -0700213
214 hardware::EventFlag* mEventQueueFlag;
215
216 std::array<Event, SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800217};
218
219// ---------------------------------------------------------------------------
220}; // namespace android
221
222#endif // ANDROID_SENSOR_DEVICE_H