blob: 1b2d908fb2bab0c391ac722984a00dd97bb8a2fa [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>
Mathias Agopian801ea092017-03-06 15:05:04 -080025#include <sensor/Sensor.h>
Steven Morelandd15c0302016-12-20 11:14:50 -080026#include <stdint.h>
27#include <sys/types.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080028#include <utils/KeyedVector.h>
29#include <utils/Singleton.h>
30#include <utils/String8.h>
31
Peng Xue36e3472016-11-03 11:57:10 -070032#include <string>
Peng Xu2bec6232016-07-01 17:13:10 -070033#include <unordered_map>
34#include <algorithm> //std::max std::min
Andreas Huber99fdbb52016-10-10 13:22:58 -070035
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070036#include "RingBuffer.h"
37
Mathias Agopianf001c922010-11-11 17:58:51 -080038// ---------------------------------------------------------------------------
39
40namespace android {
Andreas Huber99fdbb52016-10-10 13:22:58 -070041
Mathias Agopianf001c922010-11-11 17:58:51 -080042// ---------------------------------------------------------------------------
43
Peng Xu1a00e2d2017-09-27 23:08:30 -070044class SensorDevice : public Singleton<SensorDevice>, public SensorServiceUtil::Dumpable {
Peng Xu6a2d3a02015-12-21 12:00:23 -080045public:
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070046 class HidlTransportErrorLog {
47 public:
48
49 HidlTransportErrorLog() {
50 mTs = 0;
51 mCount = 0;
52 }
53
54 HidlTransportErrorLog(time_t ts, int count) {
55 mTs = ts;
56 mCount = count;
57 }
58
59 String8 toString() const {
60 String8 result;
61 struct tm *timeInfo = localtime(&mTs);
62 result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min,
63 timeInfo->tm_sec, mCount);
64 return result;
65 }
66
67 private:
68 time_t mTs; // timestamp of the error
69 int mCount; // number of transport errors observed
70 };
71
Peng Xu6a2d3a02015-12-21 12:00:23 -080072 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070073
Peng Xu6a2d3a02015-12-21 12:00:23 -080074 void handleDynamicSensorConnection(int handle, bool connected);
75 status_t initCheck() const;
76 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070077
Peng Xu6a2d3a02015-12-21 12:00:23 -080078 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070079
Peng Xu6a2d3a02015-12-21 12:00:23 -080080 status_t activate(void* ident, int handle, int enabled);
81 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
82 int64_t maxBatchReportLatencyNs);
83 // Call batch with timeout zero instead of calling setDelay() for newer devices.
84 status_t setDelay(void* ident, int handle, int64_t ns);
85 status_t flush(void* ident, int handle);
86 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070087
Peng Xu53632542017-01-23 20:06:27 -080088 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070089 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
90 void unregisterDirectChannel(int32_t channelHandle);
91 int32_t configureDirectChannel(int32_t sensorHandle,
92 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
93
Peng Xu6a2d3a02015-12-21 12:00:23 -080094 void disableAllSensors();
95 void enableAllSensors();
96 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -070097
Peng Xu6a2d3a02015-12-21 12:00:23 -080098 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -070099 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -0800100
101 // Dumpable
102 virtual std::string dump() const;
103private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800104 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800105
Brian Stack12f4d322018-09-14 16:18:59 -0700106 sp<SensorServiceUtil::ISensorsWrapper> mSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700107 Vector<sensor_t> mSensorList;
Peng Xu2bec6232016-07-01 17:13:10 -0700108 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700109
Aravind Akella724d91d2013-06-27 12:04:23 -0700110 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
111 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800112 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700113
114 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
115 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
116 struct BatchParams {
Peng Xu2bec6232016-07-01 17:13:10 -0700117 nsecs_t mTSample, mTBatch;
118 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
119 BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700120 bool operator != (const BatchParams& other) {
Peng Xu2bec6232016-07-01 17:13:10 -0700121 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
122 }
123 // Merge another parameter with this one. The updated mTSample will be the min of the two.
124 // The update mTBatch will be the min of original mTBatch and the apparent batch period
125 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
126 void merge(const BatchParams &other) {
127 mTSample = std::min(mTSample, other.mTSample);
128 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
Aravind Akella724d91d2013-06-27 12:04:23 -0700129 }
130 };
131
132 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
133 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
134 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
135 // mode request is batch(... timeout > 0 ...) followed by activate().
136 // Info is a per-sensor data structure which contains the batch parameters for each client that
137 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800138 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700139 BatchParams bestBatchParams;
140 // Key is the unique identifier(ident) for each client, value is the batch parameters
141 // requested by the client.
142 KeyedVector<void*, BatchParams> batchParams;
143
Aravind Akella724d91d2013-06-27 12:04:23 -0700144 // Sets batch parameters for this ident. Returns error if this ident is not already present
145 // in the KeyedVector above.
146 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
147 int64_t maxBatchReportLatencyNs);
148 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
149 void selectBatchParams();
150 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
151 // the removed ident. If index >=0, ident is present and successfully removed.
152 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800153
154 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800155 };
156 DefaultKeyedVector<int, Info> mActivationCount;
157
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700158 // Keep track of any hidl transport failures
159 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
160 int mTotalHidlTransportErrors;
161
Aravind Akella4949c502015-02-11 15:54:35 -0800162 // Use this vector to determine which client is activated or deactivated.
163 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800164 SensorDevice();
Peng Xua8fad9b2017-03-17 12:20:27 -0700165 bool connectHidlService();
Brian Stack979887b2018-09-19 15:27:48 -0700166
167 enum HalConnectionStatus {
168 CONNECTED, // Successfully connected to the HAL
169 DOES_NOT_EXIST, // Could not find the HAL
170 FAILED_TO_CONNECT, // Found the HAL but failed to connect/initialize
171 UNKNOWN,
172 };
173 HalConnectionStatus connectHidlServiceV1_0();
174 HalConnectionStatus connectHidlServiceV2_0();
Aravind Akella4949c502015-02-11 15:54:35 -0800175
Peng Xu3889e6e2017-03-02 19:10:38 -0800176 static void handleHidlDeath(const std::string &detail);
177 template<typename T>
178 static Return<T> checkReturn(Return<T> &&ret) {
179 if (!ret.isOk()) {
180 handleHidlDeath(ret.description());
181 }
182 return std::move(ret);
183 }
Peng Xu1a00e2d2017-09-27 23:08:30 -0700184 //TODO(b/67425500): remove waiter after bug is resolved.
185 sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter;
Peng Xu3889e6e2017-03-02 19:10:38 -0800186
Aravind Akella4949c502015-02-11 15:54:35 -0800187 bool isClientDisabled(void* ident);
188 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700189
Andreas Huber99fdbb52016-10-10 13:22:58 -0700190 using Event = hardware::sensors::V1_0::Event;
191 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
192
193 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
194
195 void convertToSensorEvents(
196 const hardware::hidl_vec<Event> &src,
197 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
198 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800199
200 bool mIsDirectReportSupported;
Brian Stack979887b2018-09-19 15:27:48 -0700201
202 typedef hardware::MessageQueue<Event, hardware::kSynchronizedReadWrite> EventMessageQueue;
203 typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue;
204 std::unique_ptr<EventMessageQueue> mEventQueue;
205 std::unique_ptr<WakeLockQueue> mWakeLockQueue;
Mathias Agopianf001c922010-11-11 17:58:51 -0800206};
207
208// ---------------------------------------------------------------------------
209}; // namespace android
210
211#endif // ANDROID_SENSOR_DEVICE_H