SensorDevice.h revision e36e34731cbe77a49aa5e7d687dde041d83d0370
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 <gui/Sensor.h> 23#include <utils/KeyedVector.h> 24#include <utils/Singleton.h> 25#include <utils/String8.h> 26 27#include <stdint.h> 28#include <sys/types.h> 29#include <string> 30 31#ifdef ENABLE_TREBLE 32#include <map> 33 34#include "android/hardware/sensors/1.0/ISensors.h" 35#endif 36 37// --------------------------------------------------------------------------- 38 39namespace android { 40 41// --------------------------------------------------------------------------- 42using SensorServiceUtil::Dumpable; 43 44class SensorDevice : public Singleton<SensorDevice>, public Dumpable { 45public: 46 ssize_t getSensorList(sensor_t const** list); 47 48 void handleDynamicSensorConnection(int handle, bool connected); 49 status_t initCheck() const; 50 int getHalDeviceVersion() const; 51 52 ssize_t poll(sensors_event_t* buffer, size_t count); 53 54 status_t activate(void* ident, int handle, int enabled); 55 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, 56 int64_t maxBatchReportLatencyNs); 57 // Call batch with timeout zero instead of calling setDelay() for newer devices. 58 status_t setDelay(void* ident, int handle, int64_t ns); 59 status_t flush(void* ident, int handle); 60 status_t setMode(uint32_t mode); 61 62 int32_t registerDirectChannel(const sensors_direct_mem_t *memory); 63 void unregisterDirectChannel(int32_t channelHandle); 64 int32_t configureDirectChannel(int32_t sensorHandle, 65 int32_t channelHandle, const struct sensors_direct_cfg_t *config); 66 67 void disableAllSensors(); 68 void enableAllSensors(); 69 void autoDisable(void *ident, int handle); 70 71 status_t injectSensorData(const sensors_event_t *event); 72 void notifyConnectionDestroyed(void *ident); 73 74 // Dumpable 75 virtual std::string dump() const; 76private: 77 friend class Singleton<SensorDevice>; 78#ifdef ENABLE_TREBLE 79 sp<android::hardware::sensors::V1_0::ISensors> mSensors; 80 Vector<sensor_t> mSensorList; 81 std::map<int32_t, sensor_t*> mConnectedDynamicSensors; 82#else 83 sensors_poll_device_1_t* mSensorDevice; 84 struct sensors_module_t* mSensorModule; 85#endif 86 87 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz 88 mutable Mutex mLock; // protect mActivationCount[].batchParams 89 // fixed-size array after construction 90 91 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from 92 // batch call. For continous mode clients, maxBatchReportLatency is set to zero. 93 struct BatchParams { 94 // TODO: Get rid of flags parameter everywhere. 95 int flags; 96 nsecs_t batchDelay, batchTimeout; 97 BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {} 98 BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay), 99 batchTimeout(timeout) { } 100 bool operator != (const BatchParams& other) { 101 return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout || 102 other.flags != flags; 103 } 104 }; 105 106 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in 107 // bestBatchParams. For every batch() call corresponding params are stored in batchParams 108 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch 109 // mode request is batch(... timeout > 0 ...) followed by activate(). 110 // Info is a per-sensor data structure which contains the batch parameters for each client that 111 // has registered for this sensor. 112 struct Info { 113 BatchParams bestBatchParams; 114 // Key is the unique identifier(ident) for each client, value is the batch parameters 115 // requested by the client. 116 KeyedVector<void*, BatchParams> batchParams; 117 118 Info() : bestBatchParams(0, -1, -1) {} 119 // Sets batch parameters for this ident. Returns error if this ident is not already present 120 // in the KeyedVector above. 121 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs, 122 int64_t maxBatchReportLatencyNs); 123 // Finds the optimal parameters for batching and stores them in bestBatchParams variable. 124 void selectBatchParams(); 125 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of 126 // the removed ident. If index >=0, ident is present and successfully removed. 127 ssize_t removeBatchParamsForIdent(void* ident); 128 129 int numActiveClients(); 130 }; 131 DefaultKeyedVector<int, Info> mActivationCount; 132 133 // Use this vector to determine which client is activated or deactivated. 134 SortedVector<void *> mDisabledClients; 135 SensorDevice(); 136 137 bool isClientDisabled(void* ident); 138 bool isClientDisabledLocked(void* ident); 139 140#ifdef ENABLE_TREBLE 141 using Event = hardware::sensors::V1_0::Event; 142 using SensorInfo = hardware::sensors::V1_0::SensorInfo; 143 144 void convertToSensorEvent(const Event &src, sensors_event_t *dst); 145 146 void convertToSensorEvents( 147 const hardware::hidl_vec<Event> &src, 148 const hardware::hidl_vec<SensorInfo> &dynamicSensorsAdded, 149 sensors_event_t *dst); 150#endif // ENABLE_TREBLE 151}; 152 153// --------------------------------------------------------------------------- 154}; // namespace android 155 156#endif // ANDROID_SENSOR_DEVICE_H 157