blob: 595a62754e5031670d11df9c754e9085c2ffd60c [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
Mathias Agopian801ea092017-03-06 15:05:04 -080024#include <sensor/Sensor.h>
Steven Morelandd15c0302016-12-20 11:14:50 -080025#include <stdint.h>
26#include <sys/types.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080027#include <utils/KeyedVector.h>
28#include <utils/Singleton.h>
29#include <utils/String8.h>
30
Peng Xue36e3472016-11-03 11:57:10 -070031#include <string>
Peng Xu2bec6232016-07-01 17:13:10 -070032#include <unordered_map>
33#include <algorithm> //std::max std::min
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// ---------------------------------------------------------------------------
42
Peng Xu1a00e2d2017-09-27 23:08:30 -070043class SensorDevice : public Singleton<SensorDevice>, public SensorServiceUtil::Dumpable {
Peng Xu6a2d3a02015-12-21 12:00:23 -080044public:
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070045 class HidlTransportErrorLog {
46 public:
47
48 HidlTransportErrorLog() {
49 mTs = 0;
50 mCount = 0;
51 }
52
53 HidlTransportErrorLog(time_t ts, int count) {
54 mTs = ts;
55 mCount = count;
56 }
57
58 String8 toString() const {
59 String8 result;
60 struct tm *timeInfo = localtime(&mTs);
61 result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min,
62 timeInfo->tm_sec, mCount);
63 return result;
64 }
65
66 private:
67 time_t mTs; // timestamp of the error
68 int mCount; // number of transport errors observed
69 };
70
Peng Xu6a2d3a02015-12-21 12:00:23 -080071 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070072
Peng Xu6a2d3a02015-12-21 12:00:23 -080073 void handleDynamicSensorConnection(int handle, bool connected);
74 status_t initCheck() const;
75 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070076
Peng Xu6a2d3a02015-12-21 12:00:23 -080077 ssize_t poll(sensors_event_t* buffer, size_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070078
Peng Xu6a2d3a02015-12-21 12:00:23 -080079 status_t activate(void* ident, int handle, int enabled);
80 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
81 int64_t maxBatchReportLatencyNs);
82 // Call batch with timeout zero instead of calling setDelay() for newer devices.
83 status_t setDelay(void* ident, int handle, int64_t ns);
84 status_t flush(void* ident, int handle);
85 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070086
Peng Xu53632542017-01-23 20:06:27 -080087 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070088 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
89 void unregisterDirectChannel(int32_t channelHandle);
90 int32_t configureDirectChannel(int32_t sensorHandle,
91 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
92
Peng Xu6a2d3a02015-12-21 12:00:23 -080093 void disableAllSensors();
94 void enableAllSensors();
95 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -070096
Peng Xu6a2d3a02015-12-21 12:00:23 -080097 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -070098 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -080099
100 // Dumpable
101 virtual std::string dump() const;
102private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800103 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800104
Brian Stack12f4d322018-09-14 16:18:59 -0700105 sp<SensorServiceUtil::ISensorsWrapper> mSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700106 Vector<sensor_t> mSensorList;
Peng Xu2bec6232016-07-01 17:13:10 -0700107 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700108
Aravind Akella724d91d2013-06-27 12:04:23 -0700109 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
110 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800111 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700112
113 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
114 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
115 struct BatchParams {
Peng Xu2bec6232016-07-01 17:13:10 -0700116 nsecs_t mTSample, mTBatch;
117 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
118 BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700119 bool operator != (const BatchParams& other) {
Peng Xu2bec6232016-07-01 17:13:10 -0700120 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
121 }
122 // Merge another parameter with this one. The updated mTSample will be the min of the two.
123 // The update mTBatch will be the min of original mTBatch and the apparent batch period
124 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
125 void merge(const BatchParams &other) {
126 mTSample = std::min(mTSample, other.mTSample);
127 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
Aravind Akella724d91d2013-06-27 12:04:23 -0700128 }
129 };
130
131 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
132 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
133 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
134 // mode request is batch(... timeout > 0 ...) followed by activate().
135 // Info is a per-sensor data structure which contains the batch parameters for each client that
136 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800137 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700138 BatchParams bestBatchParams;
139 // Key is the unique identifier(ident) for each client, value is the batch parameters
140 // requested by the client.
141 KeyedVector<void*, BatchParams> batchParams;
142
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();
Peng Xua8fad9b2017-03-17 12:20:27 -0700164 bool connectHidlService();
Brian Stack12f4d322018-09-14 16:18:59 -0700165 bool connectHidlServiceV1_0();
166 bool connectHidlServiceV2_0();
Aravind Akella4949c502015-02-11 15:54:35 -0800167
Peng Xu3889e6e2017-03-02 19:10:38 -0800168 static void handleHidlDeath(const std::string &detail);
169 template<typename T>
170 static Return<T> checkReturn(Return<T> &&ret) {
171 if (!ret.isOk()) {
172 handleHidlDeath(ret.description());
173 }
174 return std::move(ret);
175 }
Peng Xu1a00e2d2017-09-27 23:08:30 -0700176 //TODO(b/67425500): remove waiter after bug is resolved.
177 sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter;
Peng Xu3889e6e2017-03-02 19:10:38 -0800178
Aravind Akella4949c502015-02-11 15:54:35 -0800179 bool isClientDisabled(void* ident);
180 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700181
Andreas Huber99fdbb52016-10-10 13:22:58 -0700182 using Event = hardware::sensors::V1_0::Event;
183 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
184
185 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
186
187 void convertToSensorEvents(
188 const hardware::hidl_vec<Event> &src,
189 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
190 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800191
192 bool mIsDirectReportSupported;
Mathias Agopianf001c922010-11-11 17:58:51 -0800193};
194
195// ---------------------------------------------------------------------------
196}; // namespace android
197
198#endif // ANDROID_SENSOR_DEVICE_H