SensorService.h revision 47e96014fe6576a0ae7b02e7a2cb2275e549bbc9
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#include <utils/String8.h> 31 32#include <binder/BinderService.h> 33 34#include <gui/Sensor.h> 35#include <gui/BitTube.h> 36#include <gui/ISensorServer.h> 37#include <gui/ISensorEventConnection.h> 38 39#include "SensorInterface.h" 40 41#if __clang__ 42// Clang warns about SensorEventConnection::dump hiding BBinder::dump. The cause isn't fixable 43// without changing the API, so let's tell clang this is indeed intentional. 44#pragma clang diagnostic ignored "-Woverloaded-virtual" 45#endif 46 47// --------------------------------------------------------------------------- 48#define IGNORE_HARDWARE_FUSION false 49#define DEBUG_CONNECTIONS false 50// Max size is 100 KB which is enough to accept a batch of about 1000 events. 51#define MAX_SOCKET_BUFFER_SIZE_BATCHED 100 * 1024 52// For older HALs which don't support batching, use a smaller socket buffer size. 53#define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024 54 55#define SENSOR_REGISTRATIONS_BUF_SIZE 20 56 57namespace android { 58// --------------------------------------------------------------------------- 59 60class SensorService : 61 public BinderService<SensorService>, 62 public BnSensorServer, 63 protected Thread 64{ 65 // nested class/struct for internal use 66 class SensorEventConnection; 67 68public: 69 void cleanupConnection(SensorEventConnection* connection); 70 71 status_t enable(const sp<SensorEventConnection>& connection, int handle, 72 nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags, 73 const String16& opPackageName); 74 75 status_t disable(const sp<SensorEventConnection>& connection, int handle); 76 77 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns, 78 const String16& opPackageName); 79 80 status_t flushSensor(const sp<SensorEventConnection>& connection, 81 const String16& opPackageName); 82 83private: 84 friend class BinderService<SensorService>; 85 86 // nested class/struct for internal use 87 class SensorRecord; 88 class SensorEventAckReceiver; 89 struct TrimmedSensorEvent; 90 class MostRecentEventLogger; 91 struct SensorRegistrationInfo; 92 93 enum Mode { 94 // The regular operating mode where any application can register/unregister/call flush on 95 // sensors. 96 NORMAL = 0, 97 // This mode is only used for testing purposes. Not all HALs support this mode. In this mode, 98 // the HAL ignores the sensor data provided by physical sensors and accepts the data that is 99 // injected from the SensorService as if it were the real sensor data. This mode is primarily 100 // used for testing various algorithms like vendor provided SensorFusion, Step Counter and 101 // Step Detector etc. Typically in this mode, there will be a client (a 102 // SensorEventConnection) which will be injecting sensor data into the HAL. Normal apps can 103 // unregister and register for any sensor that supports injection. Registering to sensors 104 // that do not support injection will give an error. TODO(aakella) : Allow exactly one 105 // client to inject sensor data at a time. 106 DATA_INJECTION = 1, 107 // This mode is used only for testing sensors. Each sensor can be tested in isolation with 108 // the required sampling_rate and maxReportLatency parameters without having to think about 109 // the data rates requested by other applications. End user devices are always expected to be 110 // in NORMAL mode. When this mode is first activated, all active sensors from all connections 111 // are disabled. Calling flush() will return an error. In this mode, only the requests from 112 // selected apps whose package names are whitelisted are allowed (typically CTS apps). Only 113 // these apps can register/unregister/call flush() on sensors. If SensorService switches to 114 // NORMAL mode again, all sensors that were previously registered to are activated with the 115 // corresponding paramaters if the application hasn't unregistered for sensors in the mean 116 // time. NOTE: Non whitelisted app whose sensors were previously deactivated may still 117 // receive events if a whitelisted app requests data from the same sensor. 118 RESTRICTED = 2 119 120 // State Transitions supported. 121 // RESTRICTED <--- NORMAL ---> DATA_INJECTION 122 // ---> <--- 123 124 // Shell commands to switch modes in SensorService. 125 // 1) Put SensorService in RESTRICTED mode with packageName .cts. If it is already in 126 // restricted mode it is treated as a NO_OP (and packageName is NOT changed). 127 // 128 // $ adb shell dumpsys sensorservice restrict .cts. 129 // 130 // 2) Put SensorService in DATA_INJECTION mode with packageName .xts. If it is already in 131 // data_injection mode it is treated as a NO_OP (and packageName is NOT changed). 132 // 133 // $ adb shell dumpsys sensorservice data_injection .xts. 134 // 135 // 3) Reset sensorservice back to NORMAL mode. 136 // $ adb shell dumpsys sensorservice enable 137 }; 138 139 static const char* WAKE_LOCK_NAME; 140 141 static char const* getServiceName() ANDROID_API { return "sensorservice"; } 142 SensorService() ANDROID_API; 143 virtual ~SensorService(); 144 145 virtual void onFirstRef(); 146 147 // Thread interface 148 virtual bool threadLoop(); 149 150 // ISensorServer interface 151 virtual Vector<Sensor> getSensorList(const String16& opPackageName); 152 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName); 153 virtual sp<ISensorEventConnection> createSensorEventConnection( 154 const String8& packageName, 155 int requestedMode, const String16& opPackageName); 156 virtual int isDataInjectionEnabled(); 157 virtual status_t dump(int fd, const Vector<String16>& args); 158 159 160 static int getNumEventsForSensorType(int sensor_event_type); 161 String8 getSensorName(int handle) const; 162 bool isVirtualSensor(int handle) const; 163 SensorInterface* getSensorInterfaceFromHandle(int handle) const; 164 Sensor getSensorFromHandle(int handle) const; 165 bool isWakeUpSensor(int type) const; 166 void recordLastValueLocked(sensors_event_t const* buffer, size_t count); 167 static void sortEventBuffer(sensors_event_t* buffer, size_t count); 168 Sensor registerSensor(SensorInterface* sensor); 169 Sensor registerVirtualSensor(SensorInterface* sensor); 170 Sensor registerDynamicSensor(SensorInterface* sensor); 171 bool unregisterDynamicSensor(int handle); 172 status_t cleanupWithoutDisable(const sp<SensorEventConnection>& connection, int handle); 173 status_t cleanupWithoutDisableLocked(const sp<SensorEventConnection>& connection, int handle); 174 void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, 175 sensors_event_t const* buffer, const int count); 176 static bool canAccessSensor(const Sensor& sensor, const char* operation, 177 const String16& opPackageName); 178 // SensorService acquires a partial wakelock for delivering events from wake up sensors. This 179 // method checks whether all the events from these wake up sensors have been delivered to the 180 // corresponding applications, if yes the wakelock is released. 181 void checkWakeLockState(); 182 void checkWakeLockStateLocked(); 183 bool isWakeLockAcquired(); 184 bool isWakeUpSensorEvent(const sensors_event_t& event) const; 185 bool isNewHandle(int handle); 186 187 SensorRecord * getSensorRecord(int handle); 188 189 sp<Looper> getLooper() const; 190 191 // Reset mWakeLockRefCounts for all SensorEventConnections to zero. This may happen if 192 // SensorService did not receive any acknowledgements from apps which have registered for 193 // wake_up sensors. 194 void resetAllWakeLockRefCounts(); 195 196 // Acquire or release wake_lock. If wake_lock is acquired, set the timeout in the looper to 5 197 // seconds and wake the looper. 198 void setWakeLockAcquiredLocked(bool acquire); 199 200 // Send events from the event cache for this particular connection. 201 void sendEventsFromCache(const sp<SensorEventConnection>& connection); 202 203 // Promote all weak referecences in mActiveConnections vector to strong references and add them 204 // to the output vector. 205 void populateActiveConnections( SortedVector< sp<SensorEventConnection> >* activeConnections); 206 207 // If SensorService is operating in RESTRICTED mode, only select whitelisted packages are 208 // allowed to register for or call flush on sensors. Typically only cts test packages are 209 // allowed. 210 bool isWhiteListedPackage(const String8& packageName); 211 212 // Reset the state of SensorService to NORMAL mode. 213 status_t resetToNormalMode(); 214 status_t resetToNormalModeLocked(); 215 216 // lists and maps 217 mutable Mutex mSensorsLock; 218 Vector<Sensor> mSensorList; 219 Vector<Sensor> mUserSensorListDebug; 220 Vector<Sensor> mUserSensorList; 221 Vector<Sensor> mDynamicSensorList; 222 DefaultKeyedVector<int, SensorInterface*> mSensorMap; 223 Vector<SensorInterface *> mVirtualSensorList; 224 Vector<int> mUsedHandleList; 225 status_t mInitCheck; 226 227 // Socket buffersize used to initialize BitTube. This size depends on whether batching is 228 // supported or not. 229 uint32_t mSocketBufferSize; sp<Looper> mLooper; sp<SensorEventAckReceiver> mAckReceiver; 230 231 // protected by mLock 232 mutable Mutex mLock; 233 DefaultKeyedVector<int, SensorRecord*> mActiveSensors; 234 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; 235 SortedVector< wp<SensorEventConnection> > mActiveConnections; 236 bool mWakeLockAcquired; 237 sensors_event_t *mSensorEventBuffer, *mSensorEventScratch; 238 SensorEventConnection const **mMapFlushEventsToConnections; 239 Mode mCurrentOperatingMode; 240 241 // This packagaName is set when SensorService is in RESTRICTED or DATA_INJECTION mode. Only 242 // applications with this packageName are allowed to activate/deactivate or call flush on 243 // sensors. To run CTS this is can be set to ".cts." and only CTS tests will get access to 244 // sensors. 245 String8 mWhiteListedPackage; 246 247 // The size of this vector is constant, only the items are mutable 248 KeyedVector<int32_t, MostRecentEventLogger *> mLastEventSeen; 249 250 int mNextSensorRegIndex; 251 Vector<SensorRegistrationInfo> mLastNSensorRegistrations; 252}; 253 254} // namespace android 255#endif // ANDROID_SENSOR_SERVICE_H 256