blob: 2da60b65e3b6395a13e4c7f27b5924d31755e4f8 [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// ---------------------------------------------------------------------------
Brian Stack574cda32018-10-01 11:18:51 -070045class SensorsHalDeathReceivier : public android::hardware::hidl_death_recipient {
46 virtual void serviceDied(uint64_t cookie,
47 const wp<::android::hidl::base::V1_0::IBase>& service) override;
48};
Mathias Agopianf001c922010-11-11 17:58:51 -080049
Brian Stack6c49e6f2018-09-24 15:44:32 -070050class SensorDevice : public Singleton<SensorDevice>,
Brian Stack6c49e6f2018-09-24 15:44:32 -070051 public SensorServiceUtil::Dumpable {
Peng Xu6a2d3a02015-12-21 12:00:23 -080052public:
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070053 class HidlTransportErrorLog {
54 public:
55
56 HidlTransportErrorLog() {
57 mTs = 0;
58 mCount = 0;
59 }
60
61 HidlTransportErrorLog(time_t ts, int count) {
62 mTs = ts;
63 mCount = count;
64 }
65
66 String8 toString() const {
67 String8 result;
68 struct tm *timeInfo = localtime(&mTs);
69 result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min,
70 timeInfo->tm_sec, mCount);
71 return result;
72 }
73
74 private:
75 time_t mTs; // timestamp of the error
76 int mCount; // number of transport errors observed
77 };
78
Brian Stacka28e9212018-09-19 15:20:30 -070079 ~SensorDevice();
80
Peng Xu6a2d3a02015-12-21 12:00:23 -080081 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070082
Peng Xu6a2d3a02015-12-21 12:00:23 -080083 void handleDynamicSensorConnection(int handle, bool connected);
84 status_t initCheck() const;
85 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070086
Peng Xu6a2d3a02015-12-21 12:00:23 -080087 ssize_t poll(sensors_event_t* buffer, size_t count);
Brian Stackb7bfc0f2018-09-25 09:41:16 -070088 void writeWakeLockHandled(uint32_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070089
Peng Xu6a2d3a02015-12-21 12:00:23 -080090 status_t activate(void* ident, int handle, int enabled);
91 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
92 int64_t maxBatchReportLatencyNs);
93 // Call batch with timeout zero instead of calling setDelay() for newer devices.
94 status_t setDelay(void* ident, int handle, int64_t ns);
95 status_t flush(void* ident, int handle);
96 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070097
Peng Xu53632542017-01-23 20:06:27 -080098 bool isDirectReportSupported() const;
Peng Xue36e3472016-11-03 11:57:10 -070099 int32_t registerDirectChannel(const sensors_direct_mem_t *memory);
100 void unregisterDirectChannel(int32_t channelHandle);
101 int32_t configureDirectChannel(int32_t sensorHandle,
102 int32_t channelHandle, const struct sensors_direct_cfg_t *config);
103
Peng Xu6a2d3a02015-12-21 12:00:23 -0800104 void disableAllSensors();
105 void enableAllSensors();
106 void autoDisable(void *ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700107
Peng Xu6a2d3a02015-12-21 12:00:23 -0800108 status_t injectSensorData(const sensors_event_t *event);
Peng Xu4f707f82016-09-26 11:28:32 -0700109 void notifyConnectionDestroyed(void *ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -0800110
Brian Stack6c49e6f2018-09-24 15:44:32 -0700111 using Result = ::android::hardware::sensors::V1_0::Result;
112 hardware::Return<void> onDynamicSensorsConnected(
Brian Stackbbab1ea2018-10-01 10:49:07 -0700113 const hardware::hidl_vec<hardware::sensors::V1_0::SensorInfo> &dynamicSensorsAdded);
Brian Stack6c49e6f2018-09-24 15:44:32 -0700114 hardware::Return<void> onDynamicSensorsDisconnected(
Brian Stackbbab1ea2018-10-01 10:49:07 -0700115 const hardware::hidl_vec<int32_t> &dynamicSensorHandlesRemoved);
Brian Stack6c49e6f2018-09-24 15:44:32 -0700116
Peng Xu6a2d3a02015-12-21 12:00:23 -0800117 // Dumpable
118 virtual std::string dump() const;
119private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800120 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800121
Brian Stack12f4d322018-09-14 16:18:59 -0700122 sp<SensorServiceUtil::ISensorsWrapper> mSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700123 Vector<sensor_t> mSensorList;
Peng Xu2bec6232016-07-01 17:13:10 -0700124 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700125
Aravind Akella724d91d2013-06-27 12:04:23 -0700126 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
127 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800128 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700129
130 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
131 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
132 struct BatchParams {
Peng Xu2bec6232016-07-01 17:13:10 -0700133 nsecs_t mTSample, mTBatch;
134 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
135 BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {}
Aravind Akella724d91d2013-06-27 12:04:23 -0700136 bool operator != (const BatchParams& other) {
Peng Xu2bec6232016-07-01 17:13:10 -0700137 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
138 }
139 // Merge another parameter with this one. The updated mTSample will be the min of the two.
140 // The update mTBatch will be the min of original mTBatch and the apparent batch period
141 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
142 void merge(const BatchParams &other) {
143 mTSample = std::min(mTSample, other.mTSample);
144 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
Aravind Akella724d91d2013-06-27 12:04:23 -0700145 }
146 };
147
148 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
149 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
150 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
151 // mode request is batch(... timeout > 0 ...) followed by activate().
152 // Info is a per-sensor data structure which contains the batch parameters for each client that
153 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800154 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700155 BatchParams bestBatchParams;
156 // Key is the unique identifier(ident) for each client, value is the batch parameters
157 // requested by the client.
158 KeyedVector<void*, BatchParams> batchParams;
159
Aravind Akella724d91d2013-06-27 12:04:23 -0700160 // Sets batch parameters for this ident. Returns error if this ident is not already present
161 // in the KeyedVector above.
162 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
163 int64_t maxBatchReportLatencyNs);
164 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
165 void selectBatchParams();
166 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
167 // the removed ident. If index >=0, ident is present and successfully removed.
168 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800169
170 int numActiveClients();
Mathias Agopianf001c922010-11-11 17:58:51 -0800171 };
172 DefaultKeyedVector<int, Info> mActivationCount;
173
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700174 // Keep track of any hidl transport failures
175 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors;
176 int mTotalHidlTransportErrors;
177
Aravind Akella4949c502015-02-11 15:54:35 -0800178 // Use this vector to determine which client is activated or deactivated.
179 SortedVector<void *> mDisabledClients;
Mathias Agopianf001c922010-11-11 17:58:51 -0800180 SensorDevice();
Peng Xua8fad9b2017-03-17 12:20:27 -0700181 bool connectHidlService();
Brian Stack979887b2018-09-19 15:27:48 -0700182
183 enum HalConnectionStatus {
184 CONNECTED, // Successfully connected to the HAL
185 DOES_NOT_EXIST, // Could not find the HAL
186 FAILED_TO_CONNECT, // Found the HAL but failed to connect/initialize
187 UNKNOWN,
188 };
189 HalConnectionStatus connectHidlServiceV1_0();
190 HalConnectionStatus connectHidlServiceV2_0();
Aravind Akella4949c502015-02-11 15:54:35 -0800191
Brian Stacka28e9212018-09-19 15:20:30 -0700192 ssize_t pollHal(sensors_event_t* buffer, size_t count);
193 ssize_t pollFmq(sensors_event_t* buffer, size_t count);
194
Peng Xu3889e6e2017-03-02 19:10:38 -0800195 static void handleHidlDeath(const std::string &detail);
196 template<typename T>
197 static Return<T> checkReturn(Return<T> &&ret) {
198 if (!ret.isOk()) {
199 handleHidlDeath(ret.description());
200 }
201 return std::move(ret);
202 }
Peng Xu1a00e2d2017-09-27 23:08:30 -0700203 //TODO(b/67425500): remove waiter after bug is resolved.
204 sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter;
Peng Xu3889e6e2017-03-02 19:10:38 -0800205
Aravind Akella4949c502015-02-11 15:54:35 -0800206 bool isClientDisabled(void* ident);
207 bool isClientDisabledLocked(void* ident);
Andreas Huber99fdbb52016-10-10 13:22:58 -0700208
Andreas Huber99fdbb52016-10-10 13:22:58 -0700209 using Event = hardware::sensors::V1_0::Event;
210 using SensorInfo = hardware::sensors::V1_0::SensorInfo;
211
212 void convertToSensorEvent(const Event &src, sensors_event_t *dst);
213
214 void convertToSensorEvents(
215 const hardware::hidl_vec<Event> &src,
216 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded,
217 sensors_event_t *dst);
Peng Xu53632542017-01-23 20:06:27 -0800218
219 bool mIsDirectReportSupported;
Brian Stack979887b2018-09-19 15:27:48 -0700220
221 typedef hardware::MessageQueue<Event, hardware::kSynchronizedReadWrite> EventMessageQueue;
222 typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue;
223 std::unique_ptr<EventMessageQueue> mEventQueue;
224 std::unique_ptr<WakeLockQueue> mWakeLockQueue;
Brian Stacka28e9212018-09-19 15:20:30 -0700225
226 hardware::EventFlag* mEventQueueFlag;
227
228 std::array<Event, SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer;
Brian Stack574cda32018-10-01 11:18:51 -0700229
230 sp<SensorsHalDeathReceivier> mSensorsHalDeathReceiver;
Mathias Agopianf001c922010-11-11 17:58:51 -0800231};
232
233// ---------------------------------------------------------------------------
234}; // namespace android
235
236#endif // ANDROID_SENSOR_DEVICE_H