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 | |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 20 | #include "SensorDeviceUtils.h" |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 21 | #include "SensorServiceUtils.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 22 | |
Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 23 | #include <sensor/Sensor.h> |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 24 | #include <stdint.h> |
| 25 | #include <sys/types.h> |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 26 | #include <utils/KeyedVector.h> |
| 27 | #include <utils/Singleton.h> |
| 28 | #include <utils/String8.h> |
| 29 | |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 30 | #include <string> |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 31 | #include <unordered_map> |
| 32 | #include <algorithm> //std::max std::min |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 33 | |
| 34 | #include "android/hardware/sensors/1.0/ISensors.h" |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 35 | |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 36 | #include "RingBuffer.h" |
| 37 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 38 | // --------------------------------------------------------------------------- |
| 39 | |
| 40 | namespace android { |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 41 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 42 | // --------------------------------------------------------------------------- |
| 43 | |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 44 | class SensorDevice : public Singleton<SensorDevice>, public SensorServiceUtil::Dumpable { |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 45 | public: |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 46 | class HidlTransportErrorLog { |
| 47 | public: |
| 48 | |
| 49 | HidlTransportErrorLog() { |
| 50 | mTs = 0; |
| 51 | mCount = 0; |
| 52 | } |
| 53 | |
| 54 | HidlTransportErrorLog(time_t ts, int count) { |
| 55 | mTs = ts; |
| 56 | mCount = count; |
| 57 | } |
| 58 | |
| 59 | String8 toString() const { |
| 60 | String8 result; |
| 61 | struct tm *timeInfo = localtime(&mTs); |
| 62 | result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min, |
| 63 | timeInfo->tm_sec, mCount); |
| 64 | return result; |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | time_t mTs; // timestamp of the error |
| 69 | int mCount; // number of transport errors observed |
| 70 | }; |
| 71 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 72 | ssize_t getSensorList(sensor_t const** list); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 73 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 74 | void handleDynamicSensorConnection(int handle, bool connected); |
| 75 | status_t initCheck() const; |
| 76 | int getHalDeviceVersion() const; |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 77 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 78 | ssize_t poll(sensors_event_t* buffer, size_t count); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 79 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 80 | status_t activate(void* ident, int handle, int enabled); |
| 81 | status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, |
| 82 | int64_t maxBatchReportLatencyNs); |
| 83 | // Call batch with timeout zero instead of calling setDelay() for newer devices. |
| 84 | status_t setDelay(void* ident, int handle, int64_t ns); |
| 85 | status_t flush(void* ident, int handle); |
| 86 | status_t setMode(uint32_t mode); |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 87 | |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 88 | bool isDirectReportSupported() const; |
Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 89 | int32_t registerDirectChannel(const sensors_direct_mem_t *memory); |
| 90 | void unregisterDirectChannel(int32_t channelHandle); |
| 91 | int32_t configureDirectChannel(int32_t sensorHandle, |
| 92 | int32_t channelHandle, const struct sensors_direct_cfg_t *config); |
| 93 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 94 | void disableAllSensors(); |
| 95 | void enableAllSensors(); |
| 96 | void autoDisable(void *ident, int handle); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 97 | |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 98 | status_t injectSensorData(const sensors_event_t *event); |
Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 99 | void notifyConnectionDestroyed(void *ident); |
Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 100 | |
| 101 | // Dumpable |
| 102 | virtual std::string dump() const; |
| 103 | private: |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 104 | friend class Singleton<SensorDevice>; |
Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 105 | |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 106 | sp<hardware::sensors::V1_0::ISensors> mSensors; |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 107 | Vector<sensor_t> mSensorList; |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 108 | std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors; |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 109 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 110 | static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz |
| 111 | mutable Mutex mLock; // protect mActivationCount[].batchParams |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 112 | // fixed-size array after construction |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 113 | |
| 114 | // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from |
| 115 | // batch call. For continous mode clients, maxBatchReportLatency is set to zero. |
| 116 | struct BatchParams { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 117 | nsecs_t mTSample, mTBatch; |
| 118 | BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {} |
| 119 | BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {} |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 120 | bool operator != (const BatchParams& other) { |
Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 121 | return !(mTSample == other.mTSample && mTBatch == other.mTBatch); |
| 122 | } |
| 123 | // Merge another parameter with this one. The updated mTSample will be the min of the two. |
| 124 | // The update mTBatch will be the min of original mTBatch and the apparent batch period |
| 125 | // of the other. the apparent batch is the maximum of mTBatch and mTSample, |
| 126 | void merge(const BatchParams &other) { |
| 127 | mTSample = std::min(mTSample, other.mTSample); |
| 128 | mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample)); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 129 | } |
| 130 | }; |
| 131 | |
| 132 | // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in |
| 133 | // bestBatchParams. For every batch() call corresponding params are stored in batchParams |
| 134 | // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch |
| 135 | // mode request is batch(... timeout > 0 ...) followed by activate(). |
| 136 | // Info is a per-sensor data structure which contains the batch parameters for each client that |
| 137 | // has registered for this sensor. |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 138 | struct Info { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 139 | BatchParams bestBatchParams; |
| 140 | // Key is the unique identifier(ident) for each client, value is the batch parameters |
| 141 | // requested by the client. |
| 142 | KeyedVector<void*, BatchParams> batchParams; |
| 143 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 144 | // Sets batch parameters for this ident. Returns error if this ident is not already present |
| 145 | // in the KeyedVector above. |
| 146 | status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs, |
| 147 | int64_t maxBatchReportLatencyNs); |
| 148 | // Finds the optimal parameters for batching and stores them in bestBatchParams variable. |
| 149 | void selectBatchParams(); |
| 150 | // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of |
| 151 | // the removed ident. If index >=0, ident is present and successfully removed. |
| 152 | ssize_t removeBatchParamsForIdent(void* ident); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 153 | |
| 154 | int numActiveClients(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 155 | }; |
| 156 | DefaultKeyedVector<int, Info> mActivationCount; |
| 157 | |
Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 158 | // Keep track of any hidl transport failures |
| 159 | SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors; |
| 160 | int mTotalHidlTransportErrors; |
| 161 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 162 | // Use this vector to determine which client is activated or deactivated. |
| 163 | SortedVector<void *> mDisabledClients; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 164 | SensorDevice(); |
Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 165 | bool connectHidlService(); |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 166 | |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 167 | static void handleHidlDeath(const std::string &detail); |
| 168 | template<typename T> |
| 169 | static Return<T> checkReturn(Return<T> &&ret) { |
| 170 | if (!ret.isOk()) { |
| 171 | handleHidlDeath(ret.description()); |
| 172 | } |
| 173 | return std::move(ret); |
| 174 | } |
Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 175 | //TODO(b/67425500): remove waiter after bug is resolved. |
| 176 | sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter; |
Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 177 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 178 | bool isClientDisabled(void* ident); |
| 179 | bool isClientDisabledLocked(void* ident); |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 180 | |
Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 181 | using Event = hardware::sensors::V1_0::Event; |
| 182 | using SensorInfo = hardware::sensors::V1_0::SensorInfo; |
| 183 | |
| 184 | void convertToSensorEvent(const Event &src, sensors_event_t *dst); |
| 185 | |
| 186 | void convertToSensorEvents( |
| 187 | const hardware::hidl_vec<Event> &src, |
| 188 | const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded, |
| 189 | sensors_event_t *dst); |
Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 190 | |
| 191 | bool mIsDirectReportSupported; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 192 | }; |
| 193 | |
| 194 | // --------------------------------------------------------------------------- |
| 195 | }; // namespace android |
| 196 | |
| 197 | #endif // ANDROID_SENSOR_DEVICE_H |