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 30 // --------------------------------------------------------------------------- 31 32 namespace android { 33 // --------------------------------------------------------------------------- 34 using SensorServiceUtil::Dumpable; 35 36 class SensorDevice : public Singleton<SensorDevice>, public Dumpable { 37 public: 38 ssize_t getSensorList(sensor_t const** list); 39 void handleDynamicSensorConnection(int handle, bool connected); 40 status_t initCheck() const; 41 int getHalDeviceVersion() const; 42 ssize_t poll(sensors_event_t* buffer, size_t count); 43 status_t activate(void* ident, int handle, int enabled); 44 status_t batch(void* ident, int handle, int flags, int64_t samplingPeriodNs, 45 int64_t maxBatchReportLatencyNs); 46 // Call batch with timeout zero instead of calling setDelay() for newer devices. 47 status_t setDelay(void* ident, int handle, int64_t ns); 48 status_t flush(void* ident, int handle); 49 status_t setMode(uint32_t mode); 50 void disableAllSensors(); 51 void enableAllSensors(); 52 void autoDisable(void *ident, int handle); 53 status_t injectSensorData(const sensors_event_t *event); 54 55 // Dumpable 56 virtual std::string dump() const; 57 private: 58 friend class Singleton<SensorDevice>; 59 sensors_poll_device_1_t* mSensorDevice; 60 struct sensors_module_t* mSensorModule; 61 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz 62 mutable Mutex mLock; // protect mActivationCount[].batchParams 63 // fixed-size array after construction 64 65 // Struct to store all the parameters(samplingPeriod, maxBatchReportLatency and flags) from 66 // batch call. For continous mode clients, maxBatchReportLatency is set to zero. 67 struct BatchParams { 68 // TODO: Get rid of flags parameter everywhere. 69 int flags; 70 nsecs_t batchDelay, batchTimeout; BatchParamsBatchParams71 BatchParams() : flags(0), batchDelay(0), batchTimeout(0) {} BatchParamsBatchParams72 BatchParams(int flag, nsecs_t delay, nsecs_t timeout): flags(flag), batchDelay(delay), 73 batchTimeout(timeout) { } 74 bool operator != (const BatchParams& other) { 75 return other.batchDelay != batchDelay || other.batchTimeout != batchTimeout || 76 other.flags != flags; 77 } 78 }; 79 80 // Store batch parameters in the KeyedVector and the optimal batch_rate and timeout in 81 // bestBatchParams. For every batch() call corresponding params are stored in batchParams 82 // vector. A continuous mode request is batch(... timeout=0 ..) followed by activate(). A batch 83 // mode request is batch(... timeout > 0 ...) followed by activate(). 84 // Info is a per-sensor data structure which contains the batch parameters for each client that 85 // has registered for this sensor. 86 struct Info { 87 BatchParams bestBatchParams; 88 // Key is the unique identifier(ident) for each client, value is the batch parameters 89 // requested by the client. 90 KeyedVector<void*, BatchParams> batchParams; 91 InfoInfo92 Info() : bestBatchParams(0, -1, -1) {} 93 // Sets batch parameters for this ident. Returns error if this ident is not already present 94 // in the KeyedVector above. 95 status_t setBatchParamsForIdent(void* ident, int flags, int64_t samplingPeriodNs, 96 int64_t maxBatchReportLatencyNs); 97 // Finds the optimal parameters for batching and stores them in bestBatchParams variable. 98 void selectBatchParams(); 99 // Removes batchParams for an ident and re-computes bestBatchParams. Returns the index of 100 // the removed ident. If index >=0, ident is present and successfully removed. 101 ssize_t removeBatchParamsForIdent(void* ident); 102 103 int numActiveClients(); 104 }; 105 DefaultKeyedVector<int, Info> mActivationCount; 106 107 // Use this vector to determine which client is activated or deactivated. 108 SortedVector<void *> mDisabledClients; 109 SensorDevice(); 110 111 bool isClientDisabled(void* ident); 112 bool isClientDisabledLocked(void* ident); 113 }; 114 115 // --------------------------------------------------------------------------- 116 }; // namespace android 117 118 #endif // ANDROID_SENSOR_DEVICE_H 119