blob: fd6cee67ea914dde2a54a35a28b5e53b8d1ac809 [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 Xu6a2d3a02015-12-21 12:00:23 -080020#include "SensorServiceUtils.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080021
Mathias Agopian801ea092017-03-06 15:05:04 -080022#include <sensor/Sensor.h>
Steven Morelandd15c0302016-12-20 11:14:50 -080023#include <stdint.h>
24#include <sys/types.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080025#include <utils/KeyedVector.h>
26#include <utils/Singleton.h>
27#include <utils/String8.h>
28
Peng Xue36e3472016-11-03 11:57:10 -070029#include <string>
Peng Xu2bec6232016-07-01 17:13:10 -070030#include <unordered_map>
31#include <algorithm> //std::max std::min
Andreas Huber99fdbb52016-10-10 13:22:58 -070032
33#include "android/hardware/sensors/1.0/ISensors.h"
Andreas Huber99fdbb52016-10-10 13:22:58 -070034
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070035#include "RingBuffer.h"
36
Mathias Agopianf001c922010-11-11 17:58:51 -080037// ---------------------------------------------------------------------------
38
39namespace android {
Andreas Huber99fdbb52016-10-10 13:22:58 -070040
Mathias Agopianf001c922010-11-11 17:58:51 -080041// ---------------------------------------------------------------------------
Peng Xu6a2d3a02015-12-21 12:00:23 -080042using SensorServiceUtil::Dumpable;
Peng Xu3889e6e2017-03-02 19:10:38 -080043using hardware::Return;
Mathias Agopianf001c922010-11-11 17:58:51 -080044
Peng Xu6a2d3a02015-12-21 12:00:23 -080045class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
46public:
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070047
48 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
Peng Xu6a2d3a02015-12-21 12:00:23 -080074 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070075
Peng Xu6a2d3a02015-12-21 12:00:23 -080076 void handleDynamicSensorConnection(int handle, bool connected);
77 status_t initCheck() const;
78 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070079
Peng Xu6a2d3a02015-12-21 12:00:23 -080080 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070081
Peng Xu6a2d3a02015-12-21 12:00:23 -080082 status_t activate(void* ident, int handle, int enabled);
83 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
84 int64_t maxBatchReportLatencyNs);
85 // Call batch with timeout zero instead of calling setDelay() for newer devices.
86 status_t setDelay(void* ident, int handle, int64_t ns);
87 status_t flush(void* ident, int handle);
88 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070089
Peng Xu53632542017-01-23 20:06:27 -080090 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070091 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
92 void unregisterDirectChannel(int32_t channelHandle);
93 int32_t configureDirectChannel(int32_t sensorHandle,
94 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
95
Peng Xu6a2d3a02015-12-21 12:00:23 -080096 void disableAllSensors();
97 void enableAllSensors();
98 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -070099
Peng Xu6a2d3a02015-12-21 12:00:23 -0800100 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -0700101 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -0800102
103 // Dumpable
104 virtual std::string dump() const;
105private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800106 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800107
Andreas Huber99fdbb52016-10-10 13:22:58 -0700108 sp<android::hardware::sensors::V1_0::ISensors> mSensors;
109 Vector<sensor_t> mSensorList;
Peng Xu2bec6232016-07-01 17:13:10 -0700110 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700111
Aravind Akella724d91d2013-06-27 12:04:23 -0700112 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
113 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800114 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700115
116 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
117 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
118 struct BatchParams {
Peng Xu2bec6232016-07-01 17:13:10 -0700119 nsecs_t mTSample, mTBatch;
120 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
121 BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700122 bool operator != (const BatchParams& other) {
Peng Xu2bec6232016-07-01 17:13:10 -0700123 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
124 }
125 // Merge another parameter with this one. The updated mTSample will be the min of the two.
126 // The update mTBatch will be the min of original mTBatch and the apparent batch period
127 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
128 void merge(const BatchParams &other) {
129 mTSample = std::min(mTSample, other.mTSample);
130 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
Aravind Akella724d91d2013-06-27 12:04:23 -0700131 }
132 };
133
134 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
135 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
136 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
137 // mode request is batch(... timeout > 0 ...) followed by activate().
138 // Info is a per-sensor data structure which contains the batch parameters for each client that
139 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800140 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700141 BatchParams bestBatchParams;
142 // Key is the unique identifier(ident) for each client, value is the batch parameters
143 // requested by the client.
144 KeyedVector<void*, BatchParams> batchParams;
145
Aravind Akella724d91d2013-06-27 12:04:23 -0700146 // Sets batch parameters for this ident. Returns error if this ident is not already present
147 // in the KeyedVector above.
148 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
149 int64_t maxBatchReportLatencyNs);
150 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
151 void selectBatchParams();
152 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
153 // the removed ident. If index >=0, ident is present and successfully removed.
154 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800155
156 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800157 };
158 DefaultKeyedVector<int, Info> mActivationCount;
159
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700160 // Keep track of any hidl transport failures
161 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
162 int mTotalHidlTransportErrors;
163
Aravind Akella4949c502015-02-11 15:54:35 -0800164 // Use this vector to determine which client is activated or deactivated.
165 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800166 SensorDevice();
Peng Xua8fad9b2017-03-17 12:20:27 -0700167 bool connectHidlService();
Aravind Akella4949c502015-02-11 15:54:35 -0800168
Peng Xu3889e6e2017-03-02 19:10:38 -0800169 static void handleHidlDeath(const std::string &detail);
170 template<typename T>
171 static Return<T> checkReturn(Return<T> &&ret) {
172 if (!ret.isOk()) {
173 handleHidlDeath(ret.description());
174 }
175 return std::move(ret);
176 }
177
Aravind Akella4949c502015-02-11 15:54:35 -0800178 bool isClientDisabled(void* ident);
179 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700180
Andreas Huber99fdbb52016-10-10 13:22:58 -0700181 using Event = hardware::sensors::V1_0::Event;
182 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
183
184 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
185
186 void convertToSensorEvents(
187 const hardware::hidl_vec<Event> &src,
188 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
189 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800190
191 bool mIsDirectReportSupported;
Mathias Agopianf001c922010-11-11 17:58:51 -0800192};
193
194// ---------------------------------------------------------------------------
195}; // namespace android
196
197#endif // ANDROID_SENSOR_DEVICE_H