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