blob: 410531be6e7a11a8d489a842ddd1a871e748eb8f [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>
Andreas Huber99fdbb52016-10-10 13:22:58 -070030#include <map>
31
32#include "android/hardware/sensors/1.0/ISensors.h"
Andreas Huber99fdbb52016-10-10 13:22:58 -070033
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070034#include "RingBuffer.h"
35
Mathias Agopianf001c922010-11-11 17:58:51 -080036// ---------------------------------------------------------------------------
37
38namespace android {
Andreas Huber99fdbb52016-10-10 13:22:58 -070039
Mathias Agopianf001c922010-11-11 17:58:51 -080040// ---------------------------------------------------------------------------
Peng Xu6a2d3a02015-12-21 12:00:23 -080041using SensorServiceUtil::Dumpable;
Peng Xu3889e6e2017-03-02 19:10:38 -080042using hardware::Return;
Mathias Agopianf001c922010-11-11 17:58:51 -080043
Peng Xu6a2d3a02015-12-21 12:00:23 -080044class SensorDevice : public Singleton<SensorDevice>, public Dumpable {
45public:
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070046
47 class HidlTransportErrorLog {
48 public:
49
50 HidlTransportErrorLog() {
51 mTs = 0;
52 mCount = 0;
53 }
54
55 HidlTransportErrorLog(time_t ts, int count) {
56 mTs = ts;
57 mCount = count;
58 }
59
60 String8 toString() const {
61 String8 result;
62 struct tm *timeInfo = localtime(&mTs);
63 result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min,
64 timeInfo->tm_sec, mCount);
65 return result;
66 }
67
68 private:
69 time_t mTs; // timestamp of the error
70 int mCount; // number of transport errors observed
71 };
72
Peng Xu6a2d3a02015-12-21 12:00:23 -080073 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070074
Peng Xu6a2d3a02015-12-21 12:00:23 -080075 void handleDynamicSensorConnection(int handle, bool connected);
76 status_t initCheck() const;
77 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070078
Peng Xu6a2d3a02015-12-21 12:00:23 -080079 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070080
Peng Xu6a2d3a02015-12-21 12:00:23 -080081 status_t activate(void* ident, int handle, int enabled);
82 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
83 int64_t maxBatchReportLatencyNs);
84 // Call batch with timeout zero instead of calling setDelay() for newer devices.
85 status_t setDelay(void* ident, int handle, int64_t ns);
86 status_t flush(void* ident, int handle);
87 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070088
Peng Xu53632542017-01-23 20:06:27 -080089 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070090 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
91 void unregisterDirectChannel(int32_t channelHandle);
92 int32_t configureDirectChannel(int32_t sensorHandle,
93 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
94
Peng Xu6a2d3a02015-12-21 12:00:23 -080095 void disableAllSensors();
96 void enableAllSensors();
97 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -070098
Peng Xu6a2d3a02015-12-21 12:00:23 -080099 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -0700100 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -0800101
102 // Dumpable
103 virtual std::string dump() const;
104private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800105 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800106
Andreas Huber99fdbb52016-10-10 13:22:58 -0700107 sp<android::hardware::sensors::V1_0::ISensors> mSensors;
108 Vector<sensor_t> mSensorList;
109 std::map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700110
Aravind Akella724d91d2013-06-27 12:04:23 -0700111 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
112 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800113 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700114
115 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
116 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
117 struct BatchParams {
Aravind Akella4949c502015-02-11 15:54:35 -0800118 // TODO: Get rid of flags parameter everywhere.
Aravind Akella724d91d2013-06-27 12:04:23 -0700119 int flags;
120 nsecs_t batchDelay, batchTimeout;
121 BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {}
122 BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay),
123 batchTimeout(timeout) { }
124 bool operator != (const BatchParams& other) {
125 return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout ||
126 other.flags != flags;
127 }
128 };
129
130 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
131 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
132 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
133 // mode request is batch(... timeout > 0 ...) followed by activate().
134 // Info is a per-sensor data structure which contains the batch parameters for each client that
135 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800136 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700137 BatchParams bestBatchParams;
138 // Key is the unique identifier(ident) for each client, value is the batch parameters
139 // requested by the client.
140 KeyedVector<void*, BatchParams> batchParams;
141
Aravind Akella4949c502015-02-11 15:54:35 -0800142 Info() : bestBatchParams(0, -1, -1) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700143 // Sets batch parameters for this ident. Returns error if this ident is not already present
144 // in the KeyedVector above.
145 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
146 int64_t maxBatchReportLatencyNs);
147 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
148 void selectBatchParams();
149 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
150 // the removed ident. If index >=0, ident is present and successfully removed.
151 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800152
153 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800154 };
155 DefaultKeyedVector<int, Info> mActivationCount;
156
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700157 // Keep track of any hidl transport failures
158 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
159 int mTotalHidlTransportErrors;
160
Aravind Akella4949c502015-02-11 15:54:35 -0800161 // Use this vector to determine which client is activated or deactivated.
162 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800163 SensorDevice();
Aravind Akella4949c502015-02-11 15:54:35 -0800164
Peng Xu3889e6e2017-03-02 19:10:38 -0800165 static void handleHidlDeath(const std::string &detail);
166 template<typename T>
167 static Return<T> checkReturn(Return<T> &&ret) {
168 if (!ret.isOk()) {
169 handleHidlDeath(ret.description());
170 }
171 return std::move(ret);
172 }
173
Aravind Akella4949c502015-02-11 15:54:35 -0800174 bool isClientDisabled(void* ident);
175 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700176
Andreas Huber99fdbb52016-10-10 13:22:58 -0700177 using Event = hardware::sensors::V1_0::Event;
178 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
179
180 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
181
182 void convertToSensorEvents(
183 const hardware::hidl_vec<Event> &src,
184 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
185 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800186
187 bool mIsDirectReportSupported;
Mathias Agopianf001c922010-11-11 17:58:51 -0800188};
189
190// ---------------------------------------------------------------------------
191}; // namespace android
192
193#endif // ANDROID_SENSOR_DEVICE_H