blob: f127c0f8cd211d25b84568a7d41ca5345259b64f [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
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000020#include "HidlSensorHalWrapper.h"
21#include "ISensorHalWrapper.h"
22
Arthur Ishigurod6b9e212021-11-14 01:20:55 +000023#include "ISensorsWrapper.h"
Peng Xu1a00e2d2017-09-27 23:08:30 -070024#include "SensorDeviceUtils.h"
Arthur Ishiguro539c27c2020-04-13 09:47:59 -070025#include "SensorService.h"
Peng Xu6a2d3a02015-12-21 12:00:23 -080026#include "SensorServiceUtils.h"
Mathias Agopianf001c922010-11-11 17:58:51 -080027
Mathias Agopian801ea092017-03-06 15:05:04 -080028#include <sensor/Sensor.h>
Arthur Ishigurod6b9e212021-11-14 01:20:55 +000029#include <sensor/SensorEventQueue.h>
Steven Morelandd15c0302016-12-20 11:14:50 -080030#include <stdint.h>
31#include <sys/types.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080032#include <utils/KeyedVector.h>
33#include <utils/Singleton.h>
34#include <utils/String8.h>
Brian Stacka28e9212018-09-19 15:20:30 -070035#include <utils/Timers.h>
Mathias Agopianf001c922010-11-11 17:58:51 -080036
Peng Xu2bec6232016-07-01 17:13:10 -070037#include <algorithm> //std::max std::min
Mark Wheatley8f285d92023-07-07 20:07:18 +000038#include <condition_variable>
39#include <mutex>
40#include <queue>
Arthur Ishiguro4f1dd8a2021-11-12 17:21:24 +000041#include <string>
Arthur Ishigurod6b9e212021-11-14 01:20:55 +000042#include <unordered_map>
Arthur Ishiguro4f1dd8a2021-11-12 17:21:24 +000043#include <vector>
Andreas Huber99fdbb52016-10-10 13:22:58 -070044
Ashutosh Joshi96b12d82017-03-15 16:27:12 -070045#include "RingBuffer.h"
46
Mathias Agopianf001c922010-11-11 17:58:51 -080047// ---------------------------------------------------------------------------
48
49namespace android {
Andreas Huber99fdbb52016-10-10 13:22:58 -070050
Mathias Agopianf001c922010-11-11 17:58:51 -080051// ---------------------------------------------------------------------------
52
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000053class SensorDevice : public Singleton<SensorDevice>,
54 public SensorServiceUtil::Dumpable,
55 public ISensorHalWrapper::SensorDeviceCallback {
Peng Xu6a2d3a02015-12-21 12:00:23 -080056public:
Brian Stacka28e9212018-09-19 15:20:30 -070057 ~SensorDevice();
Brian Stack156da082018-10-01 15:58:53 -070058 void prepareForReconnect();
59 void reconnect();
Brian Stacka28e9212018-09-19 15:20:30 -070060
Peng Xu6a2d3a02015-12-21 12:00:23 -080061 ssize_t getSensorList(sensor_t const** list);
Andreas Huber99fdbb52016-10-10 13:22:58 -070062
Peng Xu6a2d3a02015-12-21 12:00:23 -080063 void handleDynamicSensorConnection(int handle, bool connected);
64 status_t initCheck() const;
65 int getHalDeviceVersion() const;
Andreas Huber99fdbb52016-10-10 13:22:58 -070066
Peng Xu6a2d3a02015-12-21 12:00:23 -080067 ssize_t poll(sensors_event_t* buffer, size_t count);
Brian Stackb7bfc0f2018-09-25 09:41:16 -070068 void writeWakeLockHandled(uint32_t count);
Andreas Huber99fdbb52016-10-10 13:22:58 -070069
Peng Xu6a2d3a02015-12-21 12:00:23 -080070 status_t activate(void* ident, int handle, int enabled);
71 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs,
72 int64_t maxBatchReportLatencyNs);
73 // Call batch with timeout zero instead of calling setDelay() for newer devices.
74 status_t setDelay(void* ident, int handle, int64_t ns);
75 status_t flush(void* ident, int handle);
76 status_t setMode(uint32_t mode);
Peng Xue36e3472016-11-03 11:57:10 -070077
Peng Xu53632542017-01-23 20:06:27 -080078 bool isDirectReportSupported() const;
Arthur Ishigurod6b9e212021-11-14 01:20:55 +000079 int32_t registerDirectChannel(const sensors_direct_mem_t* memory);
Peng Xue36e3472016-11-03 11:57:10 -070080 void unregisterDirectChannel(int32_t channelHandle);
Arthur Ishigurod6b9e212021-11-14 01:20:55 +000081 int32_t configureDirectChannel(int32_t sensorHandle, int32_t channelHandle,
82 const struct sensors_direct_cfg_t* config);
Peng Xue36e3472016-11-03 11:57:10 -070083
Peng Xu6a2d3a02015-12-21 12:00:23 -080084 void disableAllSensors();
85 void enableAllSensors();
Arthur Ishigurod6b9e212021-11-14 01:20:55 +000086 void autoDisable(void* ident, int handle);
Andreas Huber99fdbb52016-10-10 13:22:58 -070087
Arthur Ishigurod6b9e212021-11-14 01:20:55 +000088 status_t injectSensorData(const sensors_event_t* event);
89 void notifyConnectionDestroyed(void* ident);
Peng Xu6a2d3a02015-12-21 12:00:23 -080090
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000091 // SensorDeviceCallback
92 virtual void onDynamicSensorsConnected(
93 const std::vector<sensor_t>& dynamicSensorsAdded) override;
94 virtual void onDynamicSensorsDisconnected(
95 const std::vector<int32_t>& dynamicSensorHandlesRemoved) override;
Brian Stack6c49e6f2018-09-24 15:44:32 -070096
Arthur Ishiguro539c27c2020-04-13 09:47:59 -070097 void setUidStateForConnection(void* ident, SensorService::UidState state);
98
Arthur Ishiguro24804dc2021-11-12 17:17:09 +000099 bool isReconnecting() const { return mHalWrapper->mReconnecting; }
Brian Stack156da082018-10-01 15:58:53 -0700100
Brian Stackbce04d72019-03-21 10:54:10 -0700101 bool isSensorActive(int handle) const;
102
Anh Pham5198c992021-02-10 14:15:30 +0100103 // To update the BatchParams of a SensorEventConnection when the mic toggle changes its state
104 // while the Sensors Off toggle is on.
105 void onMicSensorAccessChanged(void* ident, int handle, nsecs_t samplingPeriodNs);
106
Peng Xu6a2d3a02015-12-21 12:00:23 -0800107 // Dumpable
Mike Ma24743862020-01-29 00:36:55 -0800108 virtual std::string dump() const override;
109 virtual void dump(util::ProtoOutputStream* proto) const override;
Arthur Ishigurod6b9e212021-11-14 01:20:55 +0000110
Peng Xu6a2d3a02015-12-21 12:00:23 -0800111private:
Mathias Agopianf001c922010-11-11 17:58:51 -0800112 friend class Singleton<SensorDevice>;
Steven Morelandd15c0302016-12-20 11:14:50 -0800113
Arthur Ishiguro24804dc2021-11-12 17:17:09 +0000114 std::unique_ptr<ISensorHalWrapper> mHalWrapper;
115
Arthur Ishiguro4f1dd8a2021-11-12 17:21:24 +0000116 std::vector<sensor_t> mSensorList;
Arthur Ishiguro71e78ac2021-11-14 01:18:47 +0000117 std::unordered_map<int32_t, sensor_t> mConnectedDynamicSensors;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700118
Anthony Stangefb4e33a2021-09-29 15:47:53 +0000119 // A bug in the Sensors HIDL spec which marks onDynamicSensorsConnected as oneway causes dynamic
120 // meta events and onDynamicSensorsConnected to be received out of order. This mutex + CV are
121 // used to block meta event processing until onDynamicSensorsConnected is received to simplify
122 // HAL implementations.
123 std::mutex mDynamicSensorsMutex;
124 std::condition_variable mDynamicSensorsCv;
Arthur Ishiguro65b59812021-12-26 19:09:54 +0000125 static constexpr std::chrono::seconds MAX_DYN_SENSOR_WAIT{5};
Anthony Stangefb4e33a2021-09-29 15:47:53 +0000126
Arthur Ishigurod6b9e212021-11-14 01:20:55 +0000127 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
128 mutable Mutex mLock; // protect mActivationCount[].batchParams
Mathias Agopianf001c922010-11-11 17:58:51 -0800129 // fixed-size array after construction
Aravind Akella724d91d2013-06-27 12:04:23 -0700130
131 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from
132 // batch call. For continous mode clients, maxBatchReportLatency is set to zero.
133 struct BatchParams {
Arthur Ishigurod6b9e212021-11-14 01:20:55 +0000134 nsecs_t mTSample, mTBatch;
135 BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {}
136 BatchParams(nsecs_t tSample, nsecs_t tBatch) : mTSample(tSample), mTBatch(tBatch) {}
137 bool operator!=(const BatchParams& other) {
138 return !(mTSample == other.mTSample && mTBatch == other.mTBatch);
139 }
140 // Merge another parameter with this one. The updated mTSample will be the min of the two.
141 // The update mTBatch will be the min of original mTBatch and the apparent batch period
142 // of the other. the apparent batch is the maximum of mTBatch and mTSample,
143 void merge(const BatchParams& other) {
144 mTSample = std::min(mTSample, other.mTSample);
145 mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample));
146 }
Aravind Akella724d91d2013-06-27 12:04:23 -0700147 };
148
149 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in
150 // bestBatchParams. For every batch() call corresponding params are stored in batchParams
151 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch
152 // mode request is batch(... timeout > 0 ...) followed by activate().
153 // Info is a per-sensor data structure which contains the batch parameters for each client that
154 // has registered for this sensor.
Mathias Agopianf001c922010-11-11 17:58:51 -0800155 struct Info {
Aravind Akella724d91d2013-06-27 12:04:23 -0700156 BatchParams bestBatchParams;
157 // Key is the unique identifier(ident) for each client, value is the batch parameters
158 // requested by the client.
159 KeyedVector<void*, BatchParams> batchParams;
160
Brian Stack0c305fe2019-04-09 12:49:24 -0700161 // Flag to track if the sensor is active
162 bool isActive = false;
163
Aravind Akella724d91d2013-06-27 12:04:23 -0700164 // Sets batch parameters for this ident. Returns error if this ident is not already present
165 // in the KeyedVector above.
166 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs,
167 int64_t maxBatchReportLatencyNs);
168 // Finds the optimal parameters for batching and stores them in bestBatchParams variable.
169 void selectBatchParams();
170 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of
171 // the removed ident. If index >=0, ident is present and successfully removed.
172 ssize_t removeBatchParamsForIdent(void* ident);
Aravind Akella4949c502015-02-11 15:54:35 -0800173
Arthur Ishiguro539c27c2020-04-13 09:47:59 -0700174 bool hasBatchParamsForIdent(void* ident) const {
175 return batchParams.indexOfKey(ident) >= 0;
176 }
177
178 /**
179 * @return The number of active clients of this sensor.
180 */
Brian Stack156da082018-10-01 15:58:53 -0700181 int numActiveClients() const;
Mathias Agopianf001c922010-11-11 17:58:51 -0800182 };
183 DefaultKeyedVector<int, Info> mActivationCount;
184
Ashutosh Joshi96b12d82017-03-15 16:27:12 -0700185 int mTotalHidlTransportErrors;
186
Arthur Ishiguro539c27c2020-04-13 09:47:59 -0700187 /**
188 * Enums describing the reason why a client was disabled.
189 */
190 enum DisabledReason : uint8_t {
191 // UID becomes idle (e.g. app goes to background).
192 DISABLED_REASON_UID_IDLE = 0,
193
194 // Sensors are restricted for all clients.
195 DISABLED_REASON_SERVICE_RESTRICTED,
196 DISABLED_REASON_MAX,
197 };
198
199 static_assert(DisabledReason::DISABLED_REASON_MAX < sizeof(uint8_t) * CHAR_BIT);
200
201 // Use this map to determine which client is activated or deactivated.
Arthur Ishigurod6b9e212021-11-14 01:20:55 +0000202 std::unordered_map<void*, uint8_t> mDisabledClients;
Arthur Ishiguro539c27c2020-04-13 09:47:59 -0700203
204 void addDisabledReasonForIdentLocked(void* ident, DisabledReason reason);
205 void removeDisabledReasonForIdentLocked(void* ident, DisabledReason reason);
206
Mathias Agopianf001c922010-11-11 17:58:51 -0800207 SensorDevice();
Arthur Ishiguro24804dc2021-11-12 17:17:09 +0000208 bool connectHalService();
Brian Stack156da082018-10-01 15:58:53 -0700209 void initializeSensorList();
210 void reactivateSensors(const DefaultKeyedVector<int, Info>& previousActivations);
Arthur Ishiguro4f1dd8a2021-11-12 17:21:24 +0000211 static bool sensorHandlesChanged(const std::vector<sensor_t>& oldSensorList,
212 const std::vector<sensor_t>& newSensorList);
Brian Stack156da082018-10-01 15:58:53 -0700213 static bool sensorIsEquivalent(const sensor_t& prevSensor, const sensor_t& newSensor);
Brian Stack979887b2018-09-19 15:27:48 -0700214
Brian Stack156da082018-10-01 15:58:53 -0700215 status_t activateLocked(void* ident, int handle, int enabled);
216 status_t batchLocked(void* ident, int handle, int flags, int64_t samplingPeriodNs,
217 int64_t maxBatchReportLatencyNs);
Brian Stacka28e9212018-09-19 15:20:30 -0700218
Arthur Ishiguro539c27c2020-04-13 09:47:59 -0700219 status_t updateBatchParamsLocked(int handle, Info& info);
220 status_t doActivateHardwareLocked(int handle, bool enable);
221
Arthur Ishiguro539c27c2020-04-13 09:47:59 -0700222 bool isClientDisabled(void* ident) const;
223 bool isClientDisabledLocked(void* ident) const;
Arthur Ishigurod6b9e212021-11-14 01:20:55 +0000224 std::vector<void*> getDisabledClientsLocked() const;
Arthur Ishiguro539c27c2020-04-13 09:47:59 -0700225
226 bool clientHasNoAccessLocked(void* ident) const;
Andreas Huber99fdbb52016-10-10 13:22:58 -0700227
Anthony Stange12924862020-03-20 10:46:15 -0400228 float getResolutionForSensor(int sensorHandle);
229
Peng Xu53632542017-01-23 20:06:27 -0800230 bool mIsDirectReportSupported;
Mark Wheatley8f285d92023-07-07 20:07:18 +0000231
232 std::mutex mHalBypassLock;
233 std::condition_variable mHalBypassCV;
234 std::queue<sensors_event_t> mHalBypassInjectedEventQueue;
235 ssize_t getHalBypassInjectedEvents(sensors_event_t* buffer, size_t count);
236 bool mInHalBypassMode;
Mathias Agopianf001c922010-11-11 17:58:51 -0800237};
238
239// ---------------------------------------------------------------------------
240}; // namespace android
241
242#endif // ANDROID_SENSOR_DEVICE_H