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