| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 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> | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 27 | #include <utils/AndroidThreads.h> | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 28 | #include <utils/RefBase.h> | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 29 | #include <utils/Looper.h> | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 30 | #include <utils/String8.h> | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | #include <binder/BinderService.h> | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | #include <gui/Sensor.h> | 
| Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 35 | #include <gui/BitTube.h> | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 36 | #include <gui/ISensorServer.h> | 
|  | 37 | #include <gui/ISensorEventConnection.h> | 
|  | 38 |  | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 39 | #include "SensorInterface.h" | 
|  | 40 |  | 
| Bernhard Rosenkränzer | 72952ef | 2014-11-17 21:03:39 +0100 | [diff] [blame] | 41 | #if __clang__ | 
|  | 42 | // Clang warns about SensorEventConnection::dump hiding BBinder::dump | 
|  | 43 | // The cause isn't fixable without changing the API, so let's tell clang | 
|  | 44 | // this is indeed intentional. | 
|  | 45 | #pragma clang diagnostic ignored "-Woverloaded-virtual" | 
|  | 46 | #endif | 
|  | 47 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 48 | // --------------------------------------------------------------------------- | 
|  | 49 |  | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 50 | #define DEBUG_CONNECTIONS   false | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 51 | // Max size is 100 KB which is enough to accept a batch of about 1000 events. | 
|  | 52 | #define MAX_SOCKET_BUFFER_SIZE_BATCHED 100 * 1024 | 
|  | 53 | // For older HALs which don't support batching, use a smaller socket buffer size. | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 54 | #define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024 | 
| Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 55 |  | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 56 | #define CIRCULAR_BUF_SIZE 10 | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 57 | #define SENSOR_REGISTRATIONS_BUF_SIZE 20 | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 58 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 59 | struct sensors_poll_device_t; | 
|  | 60 | struct sensors_module_t; | 
|  | 61 |  | 
|  | 62 | namespace android { | 
|  | 63 | // --------------------------------------------------------------------------- | 
|  | 64 |  | 
|  | 65 | class SensorService : | 
|  | 66 | public BinderService<SensorService>, | 
|  | 67 | public BnSensorServer, | 
|  | 68 | protected Thread | 
|  | 69 | { | 
| Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 70 | friend class BinderService<SensorService>; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 71 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 72 | enum Mode { | 
|  | 73 | // The regular operating mode where any application can register/unregister/call flush on | 
|  | 74 | // sensors. | 
|  | 75 | NORMAL = 0, | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 76 | // This mode is only used for testing purposes. Not all HALs support this mode. In this | 
|  | 77 | // mode, the HAL ignores the sensor data provided by physical sensors and accepts the data | 
|  | 78 | // that is injected from the SensorService as if it were the real sensor data. This mode | 
|  | 79 | // is primarily used for testing various algorithms like vendor provided SensorFusion, | 
|  | 80 | // Step Counter and Step Detector etc. Typically in this mode, there will be a client | 
|  | 81 | // (a SensorEventConnection) which will be injecting sensor data into the HAL. Normal apps | 
|  | 82 | // can unregister and register for any sensor that supports injection. Registering to sensors | 
|  | 83 | // that do not support injection will give an error. | 
|  | 84 | // TODO(aakella) : Allow exactly one client to inject sensor data at a time. | 
|  | 85 | DATA_INJECTION = 1, | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 86 | // This mode is used only for testing sensors. Each sensor can be tested in isolation with | 
|  | 87 | // the required sampling_rate and maxReportLatency parameters without having to think about | 
|  | 88 | // the data rates requested by other applications. End user devices are always expected to be | 
|  | 89 | // in NORMAL mode. When this mode is first activated, all active sensors from all connections | 
|  | 90 | // are disabled. Calling flush() will return an error. In this mode, only the requests from | 
|  | 91 | // selected apps whose package names are whitelisted are allowed (typically CTS apps).  Only | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 92 | // these apps can register/unregister/call flush() on sensors. If SensorService switches to | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 93 | // NORMAL mode again, all sensors that were previously registered to are activated with the | 
|  | 94 | // corresponding paramaters if the application hasn't unregistered for sensors in the mean | 
|  | 95 | // time. | 
|  | 96 | // NOTE: Non whitelisted app whose sensors were previously deactivated may still receive | 
|  | 97 | // events if a whitelisted app requests data from the same sensor. | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 98 | RESTRICTED = 2 | 
|  | 99 |  | 
|  | 100 | // State Transitions supported. | 
|  | 101 | //     RESTRICTED   <---  NORMAL   ---> DATA_INJECTION | 
|  | 102 | //                  --->           <--- | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | // Shell commands to switch modes in SensorService. | 
|  | 105 | // 1) Put SensorService in RESTRICTED mode with packageName .cts. If it is already in | 
|  | 106 | // restricted mode it is treated as a NO_OP (and packageName is NOT changed). | 
|  | 107 | // $ adb shell dumpsys sensorservice restrict .cts. | 
|  | 108 | // | 
|  | 109 | // 2) Put SensorService in DATA_INJECTION mode with packageName .xts. If it is already in | 
|  | 110 | // data_injection mode it is treated as a NO_OP (and packageName is NOT changed). | 
|  | 111 | // $ adb shell dumpsys sensorservice data_injection .xts. | 
|  | 112 | // | 
|  | 113 | // 3) Reset sensorservice back to NORMAL mode. | 
|  | 114 | // $ adb shell dumpsys sensorservice enable | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 115 | }; | 
|  | 116 |  | 
| Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 117 | static const char* WAKE_LOCK_NAME; | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 118 |  | 
| Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 119 | static char const* getServiceName() ANDROID_API { return "sensorservice"; } | 
|  | 120 | SensorService() ANDROID_API; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 121 | virtual ~SensorService(); | 
|  | 122 |  | 
|  | 123 | virtual void onFirstRef(); | 
|  | 124 |  | 
|  | 125 | // Thread interface | 
|  | 126 | virtual bool threadLoop(); | 
|  | 127 |  | 
|  | 128 | // ISensorServer interface | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 129 | virtual Vector<Sensor> getSensorList(const String16& opPackageName); | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 130 | virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 131 | int requestedMode, const String16& opPackageName); | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 132 | virtual int isDataInjectionEnabled(); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 133 | virtual status_t dump(int fd, const Vector<String16>& args); | 
|  | 134 |  | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 135 | class SensorEventConnection : public BnSensorEventConnection, public LooperCallback { | 
|  | 136 | friend class SensorService; | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 137 | virtual ~SensorEventConnection(); | 
|  | 138 | virtual void onFirstRef(); | 
| Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 139 | virtual sp<BitTube> getSensorChannel() const; | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 140 | virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs, | 
|  | 141 | nsecs_t maxBatchReportLatencyNs, int reservedFlags); | 
|  | 142 | virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs); | 
| Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 143 | virtual status_t flush(); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 144 | // Count the number of flush complete events which are about to be dropped in the buffer. | 
|  | 145 | // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be | 
|  | 146 | // sent separately before the next batch of events. | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 147 | void countFlushCompleteEventsLocked(sensors_event_t const* scratch, int numEventsDropped); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 148 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 149 | // Check if there are any wake up events in the buffer. If yes, return the index of the | 
|  | 150 | // first wake_up sensor event in the buffer else return -1. This wake_up sensor event will | 
|  | 151 | // have the flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have | 
|  | 152 | // the wake_up flag set. SOCK_SEQPACKET ensures that either the entire packet is read or | 
|  | 153 | // dropped. | 
|  | 154 | int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count); | 
|  | 155 |  | 
|  | 156 | // Send pending flush_complete events. There may have been flush_complete_events that are | 
|  | 157 | // dropped which need to be sent separately before other events. On older HALs (1_0) this | 
|  | 158 | // method emulates the behavior of flush(). | 
|  | 159 | void sendPendingFlushEventsLocked(); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 160 |  | 
|  | 161 | // Writes events from mEventCache to the socket. | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 162 | void writeToSocketFromCache(); | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 163 |  | 
|  | 164 | // Compute the approximate cache size from the FIFO sizes of various sensors registered for | 
|  | 165 | // this connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be | 
|  | 166 | // shared amongst wake-up sensors and non-wake up sensors. | 
|  | 167 | int computeMaxCacheSizeLocked() const; | 
|  | 168 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 169 | // When more sensors register, the maximum cache size desired may change. Compute max cache | 
|  | 170 | // size, reallocate memory and copy over events from the older cache. | 
|  | 171 | void reAllocateCacheLocked(sensors_event_t const* scratch, int count); | 
|  | 172 |  | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 173 | // LooperCallback method. If there is data to read on this fd, it is an ack from the | 
|  | 174 | // app that it has read events from a wake up sensor, decrement mWakeLockRefCount. | 
|  | 175 | // If this fd is available for writing send the data from the cache. | 
|  | 176 | virtual int handleEvent(int fd, int events, void* data); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 177 |  | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 178 | // Increment mPendingFlushEventsToSend for the given sensor handle. | 
|  | 179 | void incrementPendingFlushCount(int32_t handle); | 
|  | 180 |  | 
|  | 181 | // Add or remove the file descriptor associated with the BitTube to the looper. If mDead is | 
|  | 182 | // set to true or there are no more sensors for this connection, the file descriptor is | 
|  | 183 | // removed if it has been previously added to the Looper. Depending on the state of the | 
|  | 184 | // connection FD may be added to the Looper. The flags to set are determined by the internal | 
|  | 185 | // state of the connection. FDs are added to the looper when wake-up sensors are registered | 
|  | 186 | // (to poll for acknowledgements) and when write fails on the socket when there are too many | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 187 | // error and the other end hangs up or when this client unregisters for this connection. | 
|  | 188 | void updateLooperRegistration(const sp<Looper>& looper); | 
|  | 189 | void updateLooperRegistrationLocked(const sp<Looper>& looper); | 
|  | 190 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 191 | sp<SensorService> const mService; | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 192 | sp<BitTube> mChannel; | 
| Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 193 | uid_t mUid; | 
| Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 194 | mutable Mutex mConnectionLock; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 195 | // Number of events from wake up sensors which are still pending and haven't been delivered | 
|  | 196 | // to the corresponding application. It is incremented by one unit for each write to the | 
|  | 197 | // socket. | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 198 | uint32_t mWakeLockRefCount; | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 199 |  | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 200 | // If this flag is set to true, it means that the file descriptor associated with the | 
|  | 201 | // BitTube has been added to the Looper in SensorService. This flag is typically set when | 
|  | 202 | // this connection has wake-up sensors associated with it or when write has failed on this | 
|  | 203 | // connection and we're storing some events in the cache. | 
|  | 204 | bool mHasLooperCallbacks; | 
|  | 205 | // If there are any errors associated with the Looper this flag is set to true and | 
|  | 206 | // mWakeLockRefCount is reset to zero. needsWakeLock method will always return false, if | 
|  | 207 | // this flag is set. | 
|  | 208 | bool mDead; | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 209 |  | 
|  | 210 | bool mDataInjectionMode; | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 211 | struct FlushInfo { | 
|  | 212 | // The number of flush complete events dropped for this sensor is stored here. | 
|  | 213 | // They are sent separately before the next batch of events. | 
|  | 214 | int mPendingFlushEventsToSend; | 
|  | 215 | // Every activate is preceded by a flush. Only after the first flush complete is | 
|  | 216 | // received, the events for the sensor are sent on that *connection*. | 
|  | 217 | bool mFirstFlushPending; | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 218 | FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {} | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 219 | }; | 
|  | 220 | // protected by SensorService::mLock. Key for this vector is the sensor handle. | 
|  | 221 | KeyedVector<int, FlushInfo> mSensorInfo; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 222 | sensors_event_t *mEventCache; | 
|  | 223 | int mCacheSize, mMaxCacheSize; | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 224 | String8 mPackageName; | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 225 | const String16 mOpPackageName; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 226 | #if DEBUG_CONNECTIONS | 
|  | 227 | int mEventsReceived, mEventsSent, mEventsSentFromCache; | 
| Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 228 | int mTotalAcksNeeded, mTotalAcksReceived; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 229 | #endif | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 230 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 231 | public: | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 232 | SensorEventConnection(const sp<SensorService>& service, uid_t uid, String8 packageName, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 233 | bool isDataInjectionMode, const String16& opPackageName); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 234 |  | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 235 | status_t sendEvents(sensors_event_t const* buffer, size_t count, | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 236 | sensors_event_t* scratch, | 
|  | 237 | SensorEventConnection const * const * mapFlushEventsToConnections = NULL); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 238 | bool hasSensor(int32_t handle) const; | 
|  | 239 | bool hasAnySensor() const; | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 240 | bool hasOneShotSensors() const; | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 241 | bool addSensor(int32_t handle); | 
|  | 242 | bool removeSensor(int32_t handle); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 243 | void setFirstFlushPending(int32_t handle, bool value); | 
| Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 244 | void dump(String8& result); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 245 | bool needsWakeLock(); | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 246 | void resetWakeLockRefCount(); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 247 | String8 getPackageName() const; | 
| Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 248 |  | 
|  | 249 | uid_t getUid() const { return mUid; } | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 250 | }; | 
|  | 251 |  | 
|  | 252 | class SensorRecord { | 
|  | 253 | SortedVector< wp<SensorEventConnection> > mConnections; | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 254 | // A queue of all flush() calls made on this sensor. Flush complete events will be | 
|  | 255 | // sent in this order. | 
|  | 256 | Vector< wp<SensorEventConnection> > mPendingFlushConnections; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 257 | public: | 
|  | 258 | SensorRecord(const sp<SensorEventConnection>& connection); | 
| Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 259 | bool addConnection(const sp<SensorEventConnection>& connection); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 260 | bool removeConnection(const wp<SensorEventConnection>& connection); | 
|  | 261 | size_t getNumConnections() const { return mConnections.size(); } | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 262 |  | 
|  | 263 | void addPendingFlushConnection(const sp<SensorEventConnection>& connection); | 
|  | 264 | void removeFirstPendingFlushConnection(); | 
|  | 265 | SensorEventConnection * getFirstPendingFlushConnection(); | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 266 | void clearAllPendingFlushConnections(); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 267 | }; | 
|  | 268 |  | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 269 | class SensorEventAckReceiver : public Thread { | 
|  | 270 | sp<SensorService> const mService; | 
|  | 271 | public: | 
|  | 272 | virtual bool threadLoop(); | 
|  | 273 | SensorEventAckReceiver(const sp<SensorService>& service): mService(service) {} | 
|  | 274 | }; | 
|  | 275 |  | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 276 | // sensor_event_t with only the data and the timestamp. | 
|  | 277 | struct TrimmedSensorEvent { | 
|  | 278 | union { | 
|  | 279 | float *mData; | 
|  | 280 | uint64_t mStepCounter; | 
|  | 281 | }; | 
|  | 282 | // Timestamp from the sensor_event. | 
|  | 283 | int64_t mTimestamp; | 
|  | 284 | // HH:MM:SS local time at which this sensor event is read at SensorService. Useful | 
|  | 285 | // for debugging. | 
|  | 286 | int32_t mHour, mMin, mSec; | 
|  | 287 |  | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 288 | TrimmedSensorEvent(int sensorType); | 
|  | 289 | static bool isSentinel(const TrimmedSensorEvent& event); | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 290 |  | 
|  | 291 | ~TrimmedSensorEvent() { | 
|  | 292 | delete [] mData; | 
|  | 293 | } | 
|  | 294 | }; | 
|  | 295 |  | 
|  | 296 | // A circular buffer of TrimmedSensorEvents. The size of this buffer is typically 10. The | 
|  | 297 | // last N events generated from the sensor are stored in this buffer. The buffer is NOT | 
|  | 298 | // cleared when the sensor unregisters and as a result one may see very old data in the | 
|  | 299 | // dumpsys output but this is WAI. | 
|  | 300 | class CircularBuffer { | 
|  | 301 | int mNextInd; | 
|  | 302 | int mSensorType; | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 303 | int mBufSize; | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 304 | TrimmedSensorEvent ** mTrimmedSensorEventArr; | 
|  | 305 | public: | 
|  | 306 | CircularBuffer(int sensor_event_type); | 
|  | 307 | void addEvent(const sensors_event_t& sensor_event); | 
|  | 308 | void printBuffer(String8& buffer) const; | 
|  | 309 | bool populateLastEvent(sensors_event_t *event); | 
|  | 310 | ~CircularBuffer(); | 
|  | 311 | }; | 
|  | 312 |  | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 313 | struct SensorRegistrationInfo { | 
|  | 314 | int32_t mSensorHandle; | 
|  | 315 | String8 mPackageName; | 
|  | 316 | bool mActivated; | 
|  | 317 | int32_t mSamplingRateUs; | 
|  | 318 | int32_t mMaxReportLatencyUs; | 
|  | 319 | int32_t mHour, mMin, mSec; | 
|  | 320 |  | 
|  | 321 | SensorRegistrationInfo() : mPackageName() { | 
|  | 322 | mSensorHandle = mSamplingRateUs = mMaxReportLatencyUs = INT32_MIN; | 
|  | 323 | mHour = mMin = mSec = INT32_MIN; | 
|  | 324 | mActivated = false; | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | static bool isSentinel(const SensorRegistrationInfo& info) { | 
|  | 328 | return (info.mHour == INT32_MIN && info.mMin == INT32_MIN && info.mSec == INT32_MIN); | 
|  | 329 | } | 
|  | 330 | }; | 
|  | 331 |  | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 332 | static int getNumEventsForSensorType(int sensor_event_type); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 333 | String8 getSensorName(int handle) const; | 
| Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 334 | bool isVirtualSensor(int handle) const; | 
| Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 335 | Sensor getSensorFromHandle(int handle) const; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 336 | bool isWakeUpSensor(int type) const; | 
| Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 337 | void recordLastValueLocked(sensors_event_t const* buffer, size_t count); | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 338 | static void sortEventBuffer(sensors_event_t* buffer, size_t count); | 
| Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 339 | Sensor registerSensor(SensorInterface* sensor); | 
|  | 340 | Sensor registerVirtualSensor(SensorInterface* sensor); | 
| Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 341 | status_t cleanupWithoutDisable( | 
|  | 342 | const sp<SensorEventConnection>& connection, int handle); | 
|  | 343 | status_t cleanupWithoutDisableLocked( | 
|  | 344 | const sp<SensorEventConnection>& connection, int handle); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 345 | void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, | 
| Mathias Agopian | b6df7d0 | 2013-05-09 14:53:35 -0700 | [diff] [blame] | 346 | sensors_event_t const* buffer, const int count); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 347 | static bool canAccessSensor(const Sensor& sensor, const char* operation, | 
|  | 348 | const String16& opPackageName); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 349 | // SensorService acquires a partial wakelock for delivering events from wake up sensors. This | 
|  | 350 | // method checks whether all the events from these wake up sensors have been delivered to the | 
|  | 351 | // corresponding applications, if yes the wakelock is released. | 
|  | 352 | void checkWakeLockState(); | 
|  | 353 | void checkWakeLockStateLocked(); | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 354 | bool isWakeLockAcquired(); | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 355 | bool isWakeUpSensorEvent(const sensors_event_t& event) const; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 356 |  | 
| Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 357 | SensorRecord * getSensorRecord(int handle); | 
|  | 358 |  | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 359 | sp<Looper> getLooper() const; | 
|  | 360 |  | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 361 | // Reset mWakeLockRefCounts for all SensorEventConnections to zero. This may happen if | 
|  | 362 | // SensorService did not receive any acknowledgements from apps which have registered for | 
|  | 363 | // wake_up sensors. | 
|  | 364 | void resetAllWakeLockRefCounts(); | 
|  | 365 |  | 
|  | 366 | // Acquire or release wake_lock. If wake_lock is acquired, set the timeout in the looper to | 
|  | 367 | // 5 seconds and wake the looper. | 
|  | 368 | void setWakeLockAcquiredLocked(bool acquire); | 
|  | 369 |  | 
|  | 370 | // Send events from the event cache for this particular connection. | 
|  | 371 | void sendEventsFromCache(const sp<SensorEventConnection>& connection); | 
|  | 372 |  | 
|  | 373 | // Promote all weak referecences in mActiveConnections vector to strong references and add them | 
|  | 374 | // to the output vector. | 
|  | 375 | void populateActiveConnections(SortedVector< sp<SensorEventConnection> >* activeConnections); | 
|  | 376 |  | 
| Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 377 | // If SensorService is operating in RESTRICTED mode, only select whitelisted packages are | 
|  | 378 | // allowed to register for or call flush on sensors. Typically only cts test packages are | 
|  | 379 | // allowed. | 
|  | 380 | bool isWhiteListedPackage(const String8& packageName); | 
|  | 381 |  | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 382 | // Reset the state of SensorService to NORMAL mode. | 
|  | 383 | status_t resetToNormalMode(); | 
|  | 384 | status_t resetToNormalModeLocked(); | 
|  | 385 |  | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 386 | // constants | 
|  | 387 | Vector<Sensor> mSensorList; | 
| Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 388 | Vector<Sensor> mUserSensorListDebug; | 
| Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 389 | Vector<Sensor> mUserSensorList; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 390 | DefaultKeyedVector<int, SensorInterface*> mSensorMap; | 
|  | 391 | Vector<SensorInterface *> mVirtualSensorList; | 
| Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 392 | status_t mInitCheck; | 
| Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 393 | // Socket buffersize used to initialize BitTube. This size depends on whether batching is | 
|  | 394 | // supported or not. | 
| Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 395 | uint32_t mSocketBufferSize; | 
| Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 396 | sp<Looper> mLooper; | 
| Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 397 | sp<SensorEventAckReceiver> mAckReceiver; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 398 |  | 
|  | 399 | // protected by mLock | 
|  | 400 | mutable Mutex mLock; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 401 | DefaultKeyedVector<int, SensorRecord*> mActiveSensors; | 
| Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 402 | DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 403 | SortedVector< wp<SensorEventConnection> > mActiveConnections; | 
| Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 404 | bool mWakeLockAcquired; | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 405 | sensors_event_t *mSensorEventBuffer, *mSensorEventScratch; | 
|  | 406 | SensorEventConnection const **mMapFlushEventsToConnections; | 
| Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 407 | Mode mCurrentOperatingMode; | 
| Aravind Akella | 841a592 | 2015-06-29 12:37:48 -0700 | [diff] [blame] | 408 | // This packagaName is set when SensorService is in RESTRICTED or DATA_INJECTION mode. Only | 
|  | 409 | // applications with this packageName are allowed to activate/deactivate or call flush on | 
|  | 410 | // sensors. To run CTS this is can be set to ".cts." and only CTS tests will get access to | 
|  | 411 | // sensors. | 
|  | 412 | String8 mWhiteListedPackage; | 
| Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 413 |  | 
| Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 414 | // The size of this vector is constant, only the items are mutable | 
| Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 415 | KeyedVector<int32_t, CircularBuffer *> mLastEventSeen; | 
| Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 416 |  | 
| Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 417 | int mNextSensorRegIndex; | 
|  | 418 | Vector<SensorRegistrationInfo> mLastNSensorRegistrations; | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 419 | public: | 
| Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 420 | void cleanupConnection(SensorEventConnection* connection); | 
| Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 421 | status_t enable(const sp<SensorEventConnection>& connection, int handle, | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 422 | nsecs_t samplingPeriodNs,  nsecs_t maxBatchReportLatencyNs, int reservedFlags, | 
|  | 423 | const String16& opPackageName); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 424 | status_t disable(const sp<SensorEventConnection>& connection, int handle); | 
| Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 425 | status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns, | 
|  | 426 | const String16& opPackageName); | 
|  | 427 | status_t flushSensor(const sp<SensorEventConnection>& connection, | 
|  | 428 | const String16& opPackageName); | 
| Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 429 | }; | 
|  | 430 |  | 
|  | 431 | // --------------------------------------------------------------------------- | 
|  | 432 | }; // namespace android | 
|  | 433 |  | 
|  | 434 | #endif // ANDROID_SENSOR_SERVICE_H |