SensorService.h revision 56ae42613c91f6a6fb0dc3f626daa24666fd18c2
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_SERVICE_H 18#define ANDROID_SENSOR_SERVICE_H 19 20#include <stdint.h> 21#include <sys/types.h> 22 23#include <utils/Vector.h> 24#include <utils/SortedVector.h> 25#include <utils/KeyedVector.h> 26#include <utils/threads.h> 27#include <utils/AndroidThreads.h> 28#include <utils/RefBase.h> 29#include <utils/Looper.h> 30 31#include <binder/BinderService.h> 32 33#include <gui/Sensor.h> 34#include <gui/BitTube.h> 35#include <gui/ISensorServer.h> 36#include <gui/ISensorEventConnection.h> 37 38#include "SensorInterface.h" 39 40// --------------------------------------------------------------------------- 41 42#define DEBUG_CONNECTIONS false 43// Max size is 100 KB which is enough to accept a batch of about 1000 events. 44#define MAX_SOCKET_BUFFER_SIZE_BATCHED 100 * 1024 45// For older HALs which don't support batching, use a smaller socket buffer size. 46#define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024 47#define WAKE_UP_SENSOR_EVENT_NEEDS_ACK (1U << 31) 48 49struct sensors_poll_device_t; 50struct sensors_module_t; 51 52namespace android { 53// --------------------------------------------------------------------------- 54 55class SensorService : 56 public BinderService<SensorService>, 57 public BnSensorServer, 58 protected Thread 59{ 60 friend class BinderService<SensorService>; 61 62 static const char* WAKE_LOCK_NAME; 63 64 static char const* getServiceName() ANDROID_API { return "sensorservice"; } 65 SensorService() ANDROID_API; 66 virtual ~SensorService(); 67 68 virtual void onFirstRef(); 69 70 // Thread interface 71 virtual bool threadLoop(); 72 73 // ISensorServer interface 74 virtual Vector<Sensor> getSensorList(); 75 virtual sp<ISensorEventConnection> createSensorEventConnection(); 76 virtual status_t dump(int fd, const Vector<String16>& args); 77 78 class SensorEventConnection : public BnSensorEventConnection, public LooperCallback { 79 friend class SensorService; 80 virtual ~SensorEventConnection(); 81 virtual void onFirstRef(); 82 virtual sp<BitTube> getSensorChannel() const; 83 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, 84 nsecs_t maxBatchReportLatencyNs, int reservedFlags); 85 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs); 86 virtual status_t flush(); 87 // Count the number of flush complete events which are about to be dropped in the buffer. 88 // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be 89 // sent separately before the next batch of events. 90 void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped); 91 92 // Check if there are any wake up events in the buffer. If yes, increment the ref count. 93 // Increment it by exactly one unit for each packet sent on the socket. SOCK_SEQPACKET for 94 // the socket ensures that either the entire packet is read or dropped. 95 // Return 1 if mWakeLockRefCount has been incremented, zero if not. 96 int countWakeUpSensorEventsLocked(sensors_event_t* scratch, int count); 97 98 // Writes events from mEventCache to the socket. 99 void writeToSocketFromCacheLocked(); 100 101 // Compute the approximate cache size from the FIFO sizes of various sensors registered for 102 // this connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be 103 // shared amongst wake-up sensors and non-wake up sensors. 104 int computeMaxCacheSizeLocked() const; 105 106 // LooperCallback method. If there is data to read on this fd, it is an ack from the 107 // app that it has read events from a wake up sensor, decrement mWakeLockRefCount. 108 // If this fd is available for writing send the data from the cache. 109 virtual int handleEvent(int fd, int events, void* data); 110 111 sp<SensorService> const mService; 112 sp<BitTube> mChannel; 113 uid_t mUid; 114 mutable Mutex mConnectionLock; 115 // Number of events from wake up sensors which are still pending and haven't been delivered 116 // to the corresponding application. It is incremented by one unit for each write to the 117 // socket. 118 int mWakeLockRefCount; 119 120 struct FlushInfo { 121 // The number of flush complete events dropped for this sensor is stored here. 122 // They are sent separately before the next batch of events. 123 int mPendingFlushEventsToSend; 124 // Every activate is preceded by a flush. Only after the first flush complete is 125 // received, the events for the sensor are sent on that *connection*. 126 bool mFirstFlushPending; 127 // Number of time flush() was called on this connection. This is incremented every time 128 // flush() is called and decremented when flush_complete_event is received. 129 int mNumFlushCalls; 130 FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false), 131 mNumFlushCalls(0) {} 132 }; 133 // protected by SensorService::mLock. Key for this vector is the sensor handle. 134 KeyedVector<int, FlushInfo> mSensorInfo; 135 sensors_event_t *mEventCache; 136 int mCacheSize, mMaxCacheSize; 137 138#if DEBUG_CONNECTIONS 139 int mEventsReceived, mEventsSent, mEventsSentFromCache; 140#endif 141 142 public: 143 SensorEventConnection(const sp<SensorService>& service, uid_t uid); 144 145 status_t sendEvents(sensors_event_t const* buffer, size_t count, 146 sensors_event_t* scratch); 147 bool hasSensor(int32_t handle) const; 148 bool hasAnySensor() const; 149 bool addSensor(int32_t handle); 150 bool removeSensor(int32_t handle); 151 void setFirstFlushPending(int32_t handle, bool value); 152 void dump(String8& result); 153 bool needsWakeLock(); 154 155 uid_t getUid() const { return mUid; } 156 }; 157 158 class SensorRecord { 159 SortedVector< wp<SensorEventConnection> > mConnections; 160 public: 161 SensorRecord(const sp<SensorEventConnection>& connection); 162 bool addConnection(const sp<SensorEventConnection>& connection); 163 bool removeConnection(const wp<SensorEventConnection>& connection); 164 size_t getNumConnections() const { return mConnections.size(); } 165 }; 166 167 class SensorEventAckReceiver : public Thread { 168 sp<SensorService> const mService; 169 public: 170 virtual bool threadLoop(); 171 SensorEventAckReceiver(const sp<SensorService>& service): mService(service) {} 172 }; 173 174 String8 getSensorName(int handle) const; 175 bool isVirtualSensor(int handle) const; 176 Sensor getSensorFromHandle(int handle) const; 177 bool isWakeUpSensor(int type) const; 178 void recordLastValueLocked(const sensors_event_t* buffer, size_t count); 179 static void sortEventBuffer(sensors_event_t* buffer, size_t count); 180 Sensor registerSensor(SensorInterface* sensor); 181 Sensor registerVirtualSensor(SensorInterface* sensor); 182 status_t cleanupWithoutDisable( 183 const sp<SensorEventConnection>& connection, int handle); 184 status_t cleanupWithoutDisableLocked( 185 const sp<SensorEventConnection>& connection, int handle); 186 void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, 187 sensors_event_t const* buffer, const int count); 188 static bool canAccessSensor(const Sensor& sensor); 189 static bool verifyCanAccessSensor(const Sensor& sensor, const char* operation); 190 // SensorService acquires a partial wakelock for delivering events from wake up sensors. This 191 // method checks whether all the events from these wake up sensors have been delivered to the 192 // corresponding applications, if yes the wakelock is released. 193 void checkWakeLockState(); 194 void checkWakeLockStateLocked(); 195 bool isWakeUpSensorEvent(const sensors_event_t& event) const; 196 197 sp<Looper> getLooper() const; 198 199 // constants 200 Vector<Sensor> mSensorList; 201 Vector<Sensor> mUserSensorListDebug; 202 Vector<Sensor> mUserSensorList; 203 DefaultKeyedVector<int, SensorInterface*> mSensorMap; 204 Vector<SensorInterface *> mVirtualSensorList; 205 status_t mInitCheck; 206 size_t mSocketBufferSize; 207 sp<Looper> mLooper; 208 209 // protected by mLock 210 mutable Mutex mLock; 211 DefaultKeyedVector<int, SensorRecord*> mActiveSensors; 212 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; 213 SortedVector< wp<SensorEventConnection> > mActiveConnections; 214 bool mWakeLockAcquired; 215 216 // The size of this vector is constant, only the items are mutable 217 KeyedVector<int32_t, sensors_event_t> mLastEventSeen; 218 219public: 220 void cleanupConnection(SensorEventConnection* connection); 221 status_t enable(const sp<SensorEventConnection>& connection, int handle, 222 nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags); 223 status_t disable(const sp<SensorEventConnection>& connection, int handle); 224 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns); 225 status_t flushSensor(const sp<SensorEventConnection>& connection, int handle); 226}; 227 228// --------------------------------------------------------------------------- 229}; // namespace android 230 231#endif // ANDROID_SENSOR_SERVICE_H 232