| 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" | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 22 | #include "SensorsWrapper.h" | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 23 |  | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 24 | #include <fmq/MessageQueue.h> | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 25 | #include <sensor/SensorEventQueue.h> | 
| Mathias Agopian | 801ea09 | 2017-03-06 15:05:04 -0800 | [diff] [blame] | 26 | #include <sensor/Sensor.h> | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 27 | #include <stdint.h> | 
 | 28 | #include <sys/types.h> | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 29 | #include <utils/KeyedVector.h> | 
 | 30 | #include <utils/Singleton.h> | 
 | 31 | #include <utils/String8.h> | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 32 | #include <utils/Timers.h> | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 33 |  | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 34 | #include <string> | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 35 | #include <unordered_map> | 
 | 36 | #include <algorithm> //std::max std::min | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 37 |  | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 38 | #include "RingBuffer.h" | 
 | 39 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 40 | // --------------------------------------------------------------------------- | 
 | 41 |  | 
 | 42 | namespace android { | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 43 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 44 | // --------------------------------------------------------------------------- | 
| Brian Stack | 574cda3 | 2018-10-01 11:18:51 -0700 | [diff] [blame] | 45 | class SensorsHalDeathReceivier : public android::hardware::hidl_death_recipient { | 
 | 46 |     virtual void serviceDied(uint64_t cookie, | 
 | 47 |                              const wp<::android::hidl::base::V1_0::IBase>& service) override; | 
 | 48 | }; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 49 |  | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 50 | class SensorDevice : public Singleton<SensorDevice>, | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 51 |                      public SensorServiceUtil::Dumpable { | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 52 | public: | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 53 |     class HidlTransportErrorLog { | 
 | 54 |      public: | 
 | 55 |  | 
 | 56 |         HidlTransportErrorLog() { | 
 | 57 |             mTs = 0; | 
 | 58 |             mCount = 0; | 
 | 59 |         } | 
 | 60 |  | 
 | 61 |         HidlTransportErrorLog(time_t ts, int count) { | 
 | 62 |             mTs = ts; | 
 | 63 |             mCount = count; | 
 | 64 |         } | 
 | 65 |  | 
 | 66 |         String8 toString() const { | 
 | 67 |             String8 result; | 
 | 68 |             struct tm *timeInfo = localtime(&mTs); | 
 | 69 |             result.appendFormat("%02d:%02d:%02d :: %d", timeInfo->tm_hour, timeInfo->tm_min, | 
 | 70 |                                 timeInfo->tm_sec, mCount); | 
 | 71 |             return result; | 
 | 72 |         } | 
 | 73 |  | 
 | 74 |     private: | 
 | 75 |         time_t mTs; // timestamp of the error | 
 | 76 |         int mCount;   // number of transport errors observed | 
 | 77 |     }; | 
 | 78 |  | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 79 |     ~SensorDevice(); | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 80 |     void prepareForReconnect(); | 
 | 81 |     void reconnect(); | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 82 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 83 |     ssize_t getSensorList(sensor_t const** list); | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 84 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 85 |     void handleDynamicSensorConnection(int handle, bool connected); | 
 | 86 |     status_t initCheck() const; | 
 | 87 |     int getHalDeviceVersion() const; | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 88 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 89 |     ssize_t poll(sensors_event_t* buffer, size_t count); | 
| Brian Stack | b7bfc0f | 2018-09-25 09:41:16 -0700 | [diff] [blame] | 90 |     void writeWakeLockHandled(uint32_t count); | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 91 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 92 |     status_t activate(void* ident, int handle, int enabled); | 
 | 93 |     status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, | 
 | 94 |                    int64_t maxBatchReportLatencyNs); | 
 | 95 |     // Call batch with timeout zero instead of calling setDelay() for newer devices. | 
 | 96 |     status_t setDelay(void* ident, int handle, int64_t ns); | 
 | 97 |     status_t flush(void* ident, int handle); | 
 | 98 |     status_t setMode(uint32_t mode); | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 99 |  | 
| Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 100 |     bool isDirectReportSupported() const; | 
| Peng Xu | e36e347 | 2016-11-03 11:57:10 -0700 | [diff] [blame] | 101 |     int32_t registerDirectChannel(const sensors_direct_mem_t *memory); | 
 | 102 |     void unregisterDirectChannel(int32_t channelHandle); | 
 | 103 |     int32_t configureDirectChannel(int32_t sensorHandle, | 
 | 104 |             int32_t channelHandle, const struct sensors_direct_cfg_t *config); | 
 | 105 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 106 |     void disableAllSensors(); | 
 | 107 |     void enableAllSensors(); | 
 | 108 |     void autoDisable(void *ident, int handle); | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 109 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 110 |     status_t injectSensorData(const sensors_event_t *event); | 
| Peng Xu | 4f707f8 | 2016-09-26 11:28:32 -0700 | [diff] [blame] | 111 |     void notifyConnectionDestroyed(void *ident); | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 112 |  | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 113 |     using Result = ::android::hardware::sensors::V1_0::Result; | 
 | 114 |     hardware::Return<void> onDynamicSensorsConnected( | 
| Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 115 |             const hardware::hidl_vec<hardware::sensors::V1_0::SensorInfo> &dynamicSensorsAdded); | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 116 |     hardware::Return<void> onDynamicSensorsDisconnected( | 
| Brian Stack | bbab1ea | 2018-10-01 10:49:07 -0700 | [diff] [blame] | 117 |             const hardware::hidl_vec<int32_t> &dynamicSensorHandlesRemoved); | 
| Brian Stack | 6c49e6f | 2018-09-24 15:44:32 -0700 | [diff] [blame] | 118 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 119 |     bool isReconnecting() const { | 
 | 120 |         return mReconnecting; | 
 | 121 |     } | 
 | 122 |  | 
| Peng Xu | 6a2d3a0 | 2015-12-21 12:00:23 -0800 | [diff] [blame] | 123 |     // Dumpable | 
 | 124 |     virtual std::string dump() const; | 
 | 125 | private: | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 126 |     friend class Singleton<SensorDevice>; | 
| Steven Moreland | d15c030 | 2016-12-20 11:14:50 -0800 | [diff] [blame] | 127 |  | 
| Brian Stack | 12f4d32 | 2018-09-14 16:18:59 -0700 | [diff] [blame] | 128 |     sp<SensorServiceUtil::ISensorsWrapper> mSensors; | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 129 |     Vector<sensor_t> mSensorList; | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 130 |     std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors; | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 131 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 132 |     static const nsecs_t MINIMUM_EVENTS_PERIOD =   1000000; // 1000 Hz | 
 | 133 |     mutable Mutex mLock; // protect mActivationCount[].batchParams | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 134 |     // fixed-size array after construction | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 135 |  | 
 | 136 |     // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from | 
 | 137 |     // batch call. For continous mode clients, maxBatchReportLatency is set to zero. | 
 | 138 |     struct BatchParams { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 139 |       nsecs_t mTSample, mTBatch; | 
 | 140 |       BatchParams() : mTSample(INT64_MAX), mTBatch(INT64_MAX) {} | 
 | 141 |       BatchParams(nsecs_t tSample, nsecs_t tBatch): mTSample(tSample), mTBatch(tBatch) {} | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 142 |       bool operator != (const BatchParams& other) { | 
| Peng Xu | 2bec623 | 2016-07-01 17:13:10 -0700 | [diff] [blame] | 143 |           return !(mTSample == other.mTSample && mTBatch == other.mTBatch); | 
 | 144 |       } | 
 | 145 |       // Merge another parameter with this one. The updated mTSample will be the min of the two. | 
 | 146 |       // The update mTBatch will be the min of original mTBatch and the apparent batch period | 
 | 147 |       // of the other. the apparent batch is the maximum of mTBatch and mTSample, | 
 | 148 |       void merge(const BatchParams &other) { | 
 | 149 |           mTSample = std::min(mTSample, other.mTSample); | 
 | 150 |           mTBatch = std::min(mTBatch, std::max(other.mTBatch, other.mTSample)); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 151 |       } | 
 | 152 |     }; | 
 | 153 |  | 
 | 154 |     // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in | 
 | 155 |     // bestBatchParams. For every batch() call corresponding params are stored in batchParams | 
 | 156 |     // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch | 
 | 157 |     // mode request is batch(... timeout > 0 ...) followed by activate(). | 
 | 158 |     // Info is a per-sensor data structure which contains the batch parameters for each client that | 
 | 159 |     // has registered for this sensor. | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 160 |     struct Info { | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 161 |         BatchParams bestBatchParams; | 
 | 162 |         // Key is the unique identifier(ident) for each client, value is the batch parameters | 
 | 163 |         // requested by the client. | 
 | 164 |         KeyedVector<void*, BatchParams> batchParams; | 
 | 165 |  | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 166 |         // Sets batch parameters for this ident. Returns error if this ident is not already present | 
 | 167 |         // in the KeyedVector above. | 
 | 168 |         status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs, | 
 | 169 |                                         int64_t maxBatchReportLatencyNs); | 
 | 170 |         // Finds the optimal parameters for batching and stores them in bestBatchParams variable. | 
 | 171 |         void selectBatchParams(); | 
 | 172 |         // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of | 
 | 173 |         // the removed ident. If index >=0, ident is present and successfully removed. | 
 | 174 |         ssize_t removeBatchParamsForIdent(void* ident); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 175 |  | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 176 |         int numActiveClients() const; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 177 |     }; | 
 | 178 |     DefaultKeyedVector<int, Info> mActivationCount; | 
 | 179 |  | 
| Ashutosh Joshi | 96b12d8 | 2017-03-15 16:27:12 -0700 | [diff] [blame] | 180 |     // Keep track of any hidl transport failures | 
 | 181 |     SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors; | 
 | 182 |     int mTotalHidlTransportErrors; | 
 | 183 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 184 |     // Use this vector to determine which client is activated or deactivated. | 
 | 185 |     SortedVector<void *> mDisabledClients; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 186 |     SensorDevice(); | 
| Peng Xu | a8fad9b | 2017-03-17 12:20:27 -0700 | [diff] [blame] | 187 |     bool connectHidlService(); | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 188 |     void initializeSensorList(); | 
 | 189 |     void reactivateSensors(const DefaultKeyedVector<int, Info>& previousActivations); | 
 | 190 |     static bool sensorHandlesChanged(const Vector<sensor_t>& oldSensorList, | 
 | 191 |                                      const Vector<sensor_t>& newSensorList); | 
 | 192 |     static bool sensorIsEquivalent(const sensor_t& prevSensor, const sensor_t& newSensor); | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 193 |  | 
 | 194 |     enum HalConnectionStatus { | 
 | 195 |         CONNECTED, // Successfully connected to the HAL | 
 | 196 |         DOES_NOT_EXIST, // Could not find the HAL | 
 | 197 |         FAILED_TO_CONNECT, // Found the HAL but failed to connect/initialize | 
 | 198 |         UNKNOWN, | 
 | 199 |     }; | 
 | 200 |     HalConnectionStatus connectHidlServiceV1_0(); | 
 | 201 |     HalConnectionStatus connectHidlServiceV2_0(); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 202 |  | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 203 |     ssize_t pollHal(sensors_event_t* buffer, size_t count); | 
 | 204 |     ssize_t pollFmq(sensors_event_t* buffer, size_t count); | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 205 |     status_t activateLocked(void* ident, int handle, int enabled); | 
 | 206 |     status_t batchLocked(void* ident, int handle, int flags, int64_t samplingPeriodNs, | 
 | 207 |                          int64_t maxBatchReportLatencyNs); | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 208 |  | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 209 |     static void handleHidlDeath(const std::string &detail); | 
 | 210 |     template<typename T> | 
 | 211 |     static Return<T> checkReturn(Return<T> &&ret) { | 
 | 212 |         if (!ret.isOk()) { | 
 | 213 |             handleHidlDeath(ret.description()); | 
 | 214 |         } | 
 | 215 |         return std::move(ret); | 
 | 216 |     } | 
| Peng Xu | 1a00e2d | 2017-09-27 23:08:30 -0700 | [diff] [blame] | 217 |     //TODO(b/67425500): remove waiter after bug is resolved. | 
 | 218 |     sp<SensorDeviceUtils::HidlServiceRegistrationWaiter> mRestartWaiter; | 
| Peng Xu | 3889e6e | 2017-03-02 19:10:38 -0800 | [diff] [blame] | 219 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 220 |     bool isClientDisabled(void* ident); | 
 | 221 |     bool isClientDisabledLocked(void* ident); | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 222 |  | 
| Andreas Huber | 99fdbb5 | 2016-10-10 13:22:58 -0700 | [diff] [blame] | 223 |     using Event = hardware::sensors::V1_0::Event; | 
 | 224 |     using SensorInfo = hardware::sensors::V1_0::SensorInfo; | 
 | 225 |  | 
 | 226 |     void convertToSensorEvent(const Event &src, sensors_event_t *dst); | 
 | 227 |  | 
 | 228 |     void convertToSensorEvents( | 
 | 229 |             const hardware::hidl_vec<Event> &src, | 
 | 230 |             const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded, | 
 | 231 |             sensors_event_t *dst); | 
| Peng Xu | 5363254 | 2017-01-23 20:06:27 -0800 | [diff] [blame] | 232 |  | 
 | 233 |     bool mIsDirectReportSupported; | 
| Brian Stack | 979887b | 2018-09-19 15:27:48 -0700 | [diff] [blame] | 234 |  | 
 | 235 |     typedef hardware::MessageQueue<Event, hardware::kSynchronizedReadWrite> EventMessageQueue; | 
 | 236 |     typedef hardware::MessageQueue<uint32_t, hardware::kSynchronizedReadWrite> WakeLockQueue; | 
 | 237 |     std::unique_ptr<EventMessageQueue> mEventQueue; | 
 | 238 |     std::unique_ptr<WakeLockQueue> mWakeLockQueue; | 
| Brian Stack | a28e921 | 2018-09-19 15:20:30 -0700 | [diff] [blame] | 239 |  | 
 | 240 |     hardware::EventFlag* mEventQueueFlag; | 
 | 241 |  | 
 | 242 |     std::array<Event, SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT> mEventBuffer; | 
| Brian Stack | 574cda3 | 2018-10-01 11:18:51 -0700 | [diff] [blame] | 243 |  | 
 | 244 |     sp<SensorsHalDeathReceivier> mSensorsHalDeathReceiver; | 
| Brian Stack | 156da08 | 2018-10-01 15:58:53 -0700 | [diff] [blame] | 245 |     std::atomic_bool mReconnecting; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 246 | }; | 
 | 247 |  | 
 | 248 | // --------------------------------------------------------------------------- | 
 | 249 | }; // namespace android | 
 | 250 |  | 
 | 251 | #endif // ANDROID_SENSOR_DEVICE_H |