blob: d481ed276631d9b335ae0b80e14f5984fef839bf [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
Brian Stack6c49e6f2018-09-24 15:44:32 -070046class SensorDevice : public Singleton<SensorDevice>,
47 public android::hardware::sensors::V2_0::ISensorsCallback,
48 public SensorServiceUtil::Dumpable {
Peng Xu6a2d3a02015-12-21 12:00:23 -080049public:
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070050 class HidlTransportErrorLog {
51 public:
52
53 HidlTransportErrorLog() {
54 mTs = 0;
55 mCount = 0;
56 }
57
58 HidlTransportErrorLog(time_t ts, int count) {
59 mTs = ts;
60 mCount = count;
61 }
62
63 String8 toString() const {
64 String8 result;
65 struct tm *timeInfo = localtime(&mTs);
66 result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min,
67 timeInfo->tm_sec, mCount);
68 return result;
69 }
70
71 private:
72 time_t mTs; // timestamp of the error
73 int mCount; // number of transport errors observed
74 };
75
Brian Stacka28e9212018-09-19 15:20:30 -070076 ~SensorDevice();
77
Peng Xu6a2d3a02015-12-21 12:00:23 -080078 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070079
Peng Xu6a2d3a02015-12-21 12:00:23 -080080 void handleDynamicSensorConnection(int handle, bool connected);
81 status_t initCheck() const;
82 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070083
Peng Xu6a2d3a02015-12-21 12:00:23 -080084 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070085
Peng Xu6a2d3a02015-12-21 12:00:23 -080086 status_t activate(void* ident, int handle, int enabled);
87 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
88 int64_t maxBatchReportLatencyNs);
89 // Call batch with timeout zero instead of calling setDelay() for newer devices.
90 status_t setDelay(void* ident, int handle, int64_t ns);
91 status_t flush(void* ident, int handle);
92 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070093
Peng Xu53632542017-01-23 20:06:27 -080094 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070095 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
96 void unregisterDirectChannel(int32_t channelHandle);
97 int32_t configureDirectChannel(int32_t sensorHandle,
98 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
99
Peng Xu6a2d3a02015-12-21 12:00:23 -0800100 void disableAllSensors();
101 void enableAllSensors();
102 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700103
Peng Xu6a2d3a02015-12-21 12:00:23 -0800104 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -0700105 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -0800106
Brian Stack6c49e6f2018-09-24 15:44:32 -0700107 using Result = ::android::hardware::sensors::V1_0::Result;
108 hardware::Return<void> onDynamicSensorsConnected(
109 const hardware::hidl_vec<hardware::sensors::V1_0::SensorInfo> &dynamicSensorsAdded) override;
110 hardware::Return<void> onDynamicSensorsDisconnected(
111 const hardware::hidl_vec<int32_t> &dynamicSensorHandlesRemoved) override;
112
Peng Xu6a2d3a02015-12-21 12:00:23 -0800113 // Dumpable
114 virtual std::string dump() const;
115private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800116 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800117
Brian Stack12f4d322018-09-14 16:18:59 -0700118 sp<SensorServiceUtil::ISensorsWrapper> mSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700119 Vector<sensor_t> mSensorList;
Peng Xu2bec6232016-07-01 17:13:10 -0700120 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700121
Aravind Akella724d91d2013-06-27 12:04:23 -0700122 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
123 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800124 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700125
126 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
127 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
128 struct BatchParams {
Peng Xu2bec6232016-07-01 17:13:10 -0700129 nsecs_t mTSample, mTBatch;
130 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
131 BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700132 bool operator != (const BatchParams& other) {
Peng Xu2bec6232016-07-01 17:13:10 -0700133 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
134 }
135 // Merge another parameter with this one. The updated mTSample will be the min of the two.
136 // The update mTBatch will be the min of original mTBatch and the apparent batch period
137 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
138 void merge(const BatchParams &other) {
139 mTSample = std::min(mTSample, other.mTSample);
140 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
Aravind Akella724d91d2013-06-27 12:04:23 -0700141 }
142 };
143
144 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
145 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
146 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
147 // mode request is batch(... timeout > 0 ...) followed by activate().
148 // Info is a per-sensor data structure which contains the batch parameters for each client that
149 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800150 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700151 BatchParams bestBatchParams;
152 // Key is the unique identifier(ident) for each client, value is the batch parameters
153 // requested by the client.
154 KeyedVector<void*, BatchParams> batchParams;
155
Aravind Akella724d91d2013-06-27 12:04:23 -0700156 // Sets batch parameters for this ident. Returns error if this ident is not already present
157 // in the KeyedVector above.
158 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
159 int64_t maxBatchReportLatencyNs);
160 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
161 void selectBatchParams();
162 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
163 // the removed ident. If index >=0, ident is present and successfully removed.
164 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800165
166 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800167 };
168 DefaultKeyedVector<int, Info> mActivationCount;
169
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700170 // Keep track of any hidl transport failures
171 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
172 int mTotalHidlTransportErrors;
173
Aravind Akella4949c502015-02-11 15:54:35 -0800174 // Use this vector to determine which client is activated or deactivated.
175 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800176 SensorDevice();
Peng Xua8fad9b2017-03-17 12:20:27 -0700177 bool connectHidlService();
Brian Stack979887b2018-09-19 15:27:48 -0700178
179 enum HalConnectionStatus {
180 CONNECTED, // Successfully connected to the HAL
181 DOES_NOT_EXIST, // Could not find the HAL
182 FAILED_TO_CONNECT, // Found the HAL but failed to connect/initialize
183 UNKNOWN,
184 };
185 HalConnectionStatus connectHidlServiceV1_0();
186 HalConnectionStatus connectHidlServiceV2_0();
Aravind Akella4949c502015-02-11 15:54:35 -0800187
Brian Stacka28e9212018-09-19 15:20:30 -0700188 ssize_t pollHal(sensors_event_t* buffer, size_t count);
189 ssize_t pollFmq(sensors_event_t* buffer, size_t count);
190
Peng Xu3889e6e2017-03-02 19:10:38 -0800191 static void handleHidlDeath(const std::string &detail);
192 template<typename T>
193 static Return<T> checkReturn(Return<T> &&ret) {
194 if (!ret.isOk()) {
195 handleHidlDeath(ret.description());
196 }
197 return std::move(ret);
198 }
Peng Xu1a00e2d2017-09-27 23:08:30 -0700199 //TODO(b/67425500): remove waiter after bug is resolved.
200 sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter;
Peng Xu3889e6e2017-03-02 19:10:38 -0800201
Aravind Akella4949c502015-02-11 15:54:35 -0800202 bool isClientDisabled(void* ident);
203 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700204
Andreas Huber99fdbb52016-10-10 13:22:58 -0700205 using Event = hardware::sensors::V1_0::Event;
206 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
207
208 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
209
210 void convertToSensorEvents(
211 const hardware::hidl_vec<Event> &src,
212 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
213 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800214
215 bool mIsDirectReportSupported;
Brian Stack979887b2018-09-19 15:27:48 -0700216
217 typedef hardware::MessageQueue<Event, hardware::kSynchronizedReadWrite> EventMessageQueue;
218 typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue;
219 std::unique_ptr<EventMessageQueue> mEventQueue;
220 std::unique_ptr<WakeLockQueue> mWakeLockQueue;
Brian Stacka28e9212018-09-19 15:20:30 -0700221
222 hardware::EventFlag* mEventQueueFlag;
223
224 std::array<Event, SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer;
Mathias Agopianf001c922010-11-11 17:58:51 -0800225};
226
227// ---------------------------------------------------------------------------
228}; // namespace android
229
230#endif // ANDROID_SENSOR_DEVICE_H