Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1 | /* |
| 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 Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame^] | 20 | #include "HidlSensorHalWrapper.h" |
| 21 | #include "ISensorHalWrapper.h" |
| 22 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 23 | #include "ISensorsWrapper.h" |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 24 | #include "SensorDeviceUtils.h" |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 25 | #include "SensorService.h" |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 26 | #include "SensorServiceUtils.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 27 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 28 | #include <sensor/Sensor.h> |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 29 | #include <sensor/SensorEventQueue.h> |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 30 | #include <stdint.h> |
| 31 | #include <sys/types.h> |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 32 | #include <utils/KeyedVector.h> |
| 33 | #include <utils/Singleton.h> |
| 34 | #include <utils/String8.h> |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 35 | #include <utils/Timers.h> |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 36 | |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 37 | #include <algorithm> //std::max std::min |
Arthur Ishiguro | 4f1dd8a | 2021-11-12 17:21:24 +0000 | [diff] [blame] | 38 | #include <string> |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 39 | #include <unordered_map> |
Arthur Ishiguro | 4f1dd8a | 2021-11-12 17:21:24 +0000 | [diff] [blame] | 40 | #include <vector> |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 41 | |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 42 | #include "RingBuffer.h" |
| 43 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 44 | // --------------------------------------------------------------------------- |
| 45 | |
| 46 | namespace android { |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 47 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 48 | // --------------------------------------------------------------------------- |
| 49 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame^] | 50 | class SensorDevice : public Singleton<SensorDevice>, |
| 51 | public SensorServiceUtil::Dumpable, |
| 52 | public ISensorHalWrapper::SensorDeviceCallback { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 53 | public: |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 54 | ~SensorDevice(); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 55 | void prepareForReconnect(); |
| 56 | void reconnect(); |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 57 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 58 | ssize_t getSensorList(sensor_t const** list); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 59 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 60 | void handleDynamicSensorConnection(int handle, bool connected); |
| 61 | status_t initCheck() const; |
| 62 | int getHalDeviceVersion() const; |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 63 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 64 | ssize_t poll(sensors_event_t* buffer, size_t count); |
Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 65 | void writeWakeLockHandled(uint32_t count); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 66 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 67 | status_t activate(void* ident, int handle, int enabled); |
| 68 | status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 69 | int64_t maxBatchReportLatencyNs); |
| 70 | // Call batch with timeout zero instead of calling setDelay() for newer devices. |
| 71 | status_t setDelay(void* ident, int handle, int64_t ns); |
| 72 | status_t flush(void* ident, int handle); |
| 73 | status_t setMode(uint32_t mode); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 74 | |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 75 | bool isDirectReportSupported() const; |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 76 | int32_t registerDirectChannel(const sensors_direct_mem_t* memory); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 77 | void unregisterDirectChannel(int32_t channelHandle); |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 78 | int32_t configureDirectChannel(int32_t sensorHandle, int32_t channelHandle, |
| 79 | const struct sensors_direct_cfg_t* config); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 80 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 81 | void disableAllSensors(); |
| 82 | void enableAllSensors(); |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 83 | void autoDisable(void* ident, int handle); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 84 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 85 | status_t injectSensorData(const sensors_event_t* event); |
| 86 | void notifyConnectionDestroyed(void* ident); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 87 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame^] | 88 | // SensorDeviceCallback |
| 89 | virtual void onDynamicSensorsConnected( |
| 90 | const std::vector<sensor_t>& dynamicSensorsAdded) override; |
| 91 | virtual void onDynamicSensorsDisconnected( |
| 92 | const std::vector<int32_t>& dynamicSensorHandlesRemoved) override; |
Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 93 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 94 | void setUidStateForConnection(void* ident, SensorService::UidState state); |
| 95 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame^] | 96 | bool isReconnecting() const { return mHalWrapper->mReconnecting; } |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 97 | |
Brian Stack | bce04d7 | 2019-03-21 10:54:10 -0700 | [diff] [blame] | 98 | bool isSensorActive(int handle) const; |
| 99 | |
Anh Pham | 5198c99 | 2021-02-10 14:15:30 +0100 | [diff] [blame] | 100 | // To update the BatchParams of a SensorEventConnection when the mic toggle changes its state |
| 101 | // while the Sensors Off toggle is on. |
| 102 | void onMicSensorAccessChanged(void* ident, int handle, nsecs_t samplingPeriodNs); |
| 103 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 104 | // Dumpable |
Mike Ma | 2474386 | 2020-01-29 00:36:55 -0800 | [diff] [blame] | 105 | virtual std::string dump() const override; |
| 106 | virtual void dump(util::ProtoOutputStream* proto) const override; |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 107 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 108 | private: |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 109 | friend class Singleton<SensorDevice>; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 110 | |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame^] | 111 | std::unique_ptr<ISensorHalWrapper> mHalWrapper; |
| 112 | |
Arthur Ishiguro | 4f1dd8a | 2021-11-12 17:21:24 +0000 | [diff] [blame] | 113 | std::vector<sensor_t> mSensorList; |
Arthur Ishiguro | 71e78ac | 2021-11-14 01:18:47 +0000 | [diff] [blame] | 114 | std::unordered_map<int32_t, sensor_t> mConnectedDynamicSensors; |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 115 | |
Anthony Stange | fb4e33a | 2021-09-29 15:47:53 +0000 | [diff] [blame] | 116 | // A bug in the Sensors HIDL spec which marks onDynamicSensorsConnected as oneway causes dynamic |
| 117 | // meta events and onDynamicSensorsConnected to be received out of order. This mutex + CV are |
| 118 | // used to block meta event processing until onDynamicSensorsConnected is received to simplify |
| 119 | // HAL implementations. |
| 120 | std::mutex mDynamicSensorsMutex; |
| 121 | std::condition_variable mDynamicSensorsCv; |
Anthony Stange | fb4e33a | 2021-09-29 15:47:53 +0000 | [diff] [blame] | 122 | |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 123 | static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz |
| 124 | mutable Mutex mLock; // protect mActivationCount[].batchParams |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 125 | // fixed-size array after construction |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 126 | |
| 127 | // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from |
| 128 | // batch call. For continous mode clients, maxBatchReportLatency is set to zero. |
| 129 | struct BatchParams { |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 130 | nsecs_t mTSample, mTBatch; |
| 131 | BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {} |
| 132 | BatchParams(nsecs_t tSample, nsecs_t tBatch) : mTSample(tSample), mTBatch(tBatch) {} |
| 133 | bool operator!=(const BatchParams& other) { |
| 134 | return !(mTSample == other.mTSample && mTBatch == other.mTBatch); |
| 135 | } |
| 136 | // Merge another parameter with this one. The updated mTSample will be the min of the two. |
| 137 | // The update mTBatch will be the min of original mTBatch and the apparent batch period |
| 138 | // of the other. the apparent batch is the maximum of mTBatch and mTSample, |
| 139 | void merge(const BatchParams& other) { |
| 140 | mTSample = std::min(mTSample, other.mTSample); |
| 141 | mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample)); |
| 142 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in |
| 146 | // bestBatchParams. For every batch() call corresponding params are stored in batchParams |
| 147 | // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch |
| 148 | // mode request is batch(... timeout > 0 ...) followed by activate(). |
| 149 | // Info is a per-sensor data structure which contains the batch parameters for each client that |
| 150 | // has registered for this sensor. |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 151 | struct Info { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 152 | BatchParams bestBatchParams; |
| 153 | // Key is the unique identifier(ident) for each client, value is the batch parameters |
| 154 | // requested by the client. |
| 155 | KeyedVector<void*, BatchParams> batchParams; |
| 156 | |
Brian Stack | 0c305fe | 2019-04-09 12:49:24 -0700 | [diff] [blame] | 157 | // Flag to track if the sensor is active |
| 158 | bool isActive = false; |
| 159 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 160 | // 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 Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 169 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 170 | bool hasBatchParamsForIdent(void* ident) const { |
| 171 | return batchParams.indexOfKey(ident) >= 0; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @return The number of active clients of this sensor. |
| 176 | */ |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 177 | int numActiveClients() const; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 178 | }; |
| 179 | DefaultKeyedVector<int, Info> mActivationCount; |
| 180 | |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 181 | int mTotalHidlTransportErrors; |
| 182 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 183 | /** |
| 184 | * Enums describing the reason why a client was disabled. |
| 185 | */ |
| 186 | enum DisabledReason : uint8_t { |
| 187 | // UID becomes idle (e.g. app goes to background). |
| 188 | DISABLED_REASON_UID_IDLE = 0, |
| 189 | |
| 190 | // Sensors are restricted for all clients. |
| 191 | DISABLED_REASON_SERVICE_RESTRICTED, |
| 192 | DISABLED_REASON_MAX, |
| 193 | }; |
| 194 | |
| 195 | static_assert(DisabledReason::DISABLED_REASON_MAX < sizeof(uint8_t) * CHAR_BIT); |
| 196 | |
| 197 | // Use this map to determine which client is activated or deactivated. |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 198 | std::unordered_map<void*, uint8_t> mDisabledClients; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 199 | |
| 200 | void addDisabledReasonForIdentLocked(void* ident, DisabledReason reason); |
| 201 | void removeDisabledReasonForIdentLocked(void* ident, DisabledReason reason); |
| 202 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 203 | SensorDevice(); |
Arthur Ishiguro | 24804dc | 2021-11-12 17:17:09 +0000 | [diff] [blame^] | 204 | bool connectHalService(); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 205 | void initializeSensorList(); |
| 206 | void reactivateSensors(const DefaultKeyedVector<int, Info>& previousActivations); |
Arthur Ishiguro | 4f1dd8a | 2021-11-12 17:21:24 +0000 | [diff] [blame] | 207 | static bool sensorHandlesChanged(const std::vector<sensor_t>& oldSensorList, |
| 208 | const std::vector<sensor_t>& newSensorList); |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 209 | static bool sensorIsEquivalent(const sensor_t& prevSensor, const sensor_t& newSensor); |
Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 210 | |
Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 211 | status_t activateLocked(void* ident, int handle, int enabled); |
| 212 | status_t batchLocked(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 213 | int64_t maxBatchReportLatencyNs); |
Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 214 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 215 | status_t updateBatchParamsLocked(int handle, Info& info); |
| 216 | status_t doActivateHardwareLocked(int handle, bool enable); |
| 217 | |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 218 | bool isClientDisabled(void* ident) const; |
| 219 | bool isClientDisabledLocked(void* ident) const; |
Arthur Ishiguro | d6b9e21 | 2021-11-14 01:20:55 +0000 | [diff] [blame] | 220 | std::vector<void*> getDisabledClientsLocked() const; |
Arthur Ishiguro | 539c27c | 2020-04-13 09:47:59 -0700 | [diff] [blame] | 221 | |
| 222 | bool clientHasNoAccessLocked(void* ident) const; |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 223 | |
Anthony Stange | 1292486 | 2020-03-20 10:46:15 -0400 | [diff] [blame] | 224 | float getResolutionForSensor(int sensorHandle); |
| 225 | |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 226 | bool mIsDirectReportSupported; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 227 | }; |
| 228 | |
| 229 | // --------------------------------------------------------------------------- |
| 230 | }; // namespace android |
| 231 | |
| 232 | #endif // ANDROID_SENSOR_DEVICE_H |