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 20 #include "SensorServiceUtils.h" 21 22 #include <sensor/Sensor.h> 23 #include <stdint.h> 24 #include <sys/types.h> 25 #include <utils/KeyedVector.h> 26 #include <utils/Singleton.h> 27 #include <utils/String8.h> 28 29 #include <string> 30 #include <unordered_map> 31 #include <algorithm> //std::max std::min 32 33 #include "android/hardware/sensors/1.0/ISensors.h" 34 35 #include "RingBuffer.h" 36 37 // --------------------------------------------------------------------------- 38 39 namespace android { 40 41 // --------------------------------------------------------------------------- 42 using SensorServiceUtil::Dumpable; 43 using hardware::Return; 44 45 class SensorDevice : public Singleton<SensorDevice>, public Dumpable { 46 public: 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 74 ssize_t getSensorList(sensor_t const** list); 75 76 void handleDynamicSensorConnection(int handle, bool connected); 77 status_t initCheck() const; 78 int getHalDeviceVersion() const; 79 80 ssize_t poll(sensors_event_t* buffer, size_t count); 81 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); 89 90 bool isDirectReportSupported() const; 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 96 void disableAllSensors(); 97 void enableAllSensors(); 98 void autoDisable(void *ident, int handle); 99 100 status_t injectSensorData(const sensors_event_t *event); 101 void notifyConnectionDestroyed(void *ident); 102 103 // Dumpable 104 virtual std::string dump() const; 105 private: 106 friend class Singleton<SensorDevice>; 107 108 sp<android::hardware::sensors::V1_0::ISensors> mSensors; 109 Vector<sensor_t> mSensorList; 110 std::unordered_map<int32_t, sensor_t*> mConnectedDynamicSensors; 111 112 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz 113 mutable Mutex mLock; // protect mActivationCount[].batchParams 114 // fixed-size array after construction 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 { 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) {} 122 bool operator != (const BatchParams& other) { 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)); 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. 140 struct Info { 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 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); 155 156 int numActiveClients(); 157 }; 158 DefaultKeyedVector<int, Info> mActivationCount; 159 160 // Keep track of any hidl transport failures 161 SensorServiceUtil::RingBuffer<HidlTransportErrorLog> mHidlTransportErrors; 162 int mTotalHidlTransportErrors; 163 164 // Use this vector to determine which client is activated or deactivated. 165 SortedVector<void *> mDisabledClients; 166 SensorDevice(); 167 bool connectHidlService(); 168 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 178 bool isClientDisabled(void* ident); 179 bool isClientDisabledLocked(void* ident); 180 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); 190 191 bool mIsDirectReportSupported; 192 }; 193 194 // --------------------------------------------------------------------------- 195 }; // namespace android 196 197 #endif // ANDROID_SENSOR_DEVICE_H 198