blob: 3a1ffb25e34ad0ff6d84595116eead38a5de4f4d [file] [log] [blame]
Mathias Agopianfc328812010-07-14 23:41:37 -07001/*
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 Akella56ae4262014-07-10 16:01:10 -070027#include <utils/AndroidThreads.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070028#include <utils/RefBase.h>
Aravind Akella56ae4262014-07-10 16:01:10 -070029#include <utils/Looper.h>
Aravind Akella4949c502015-02-11 15:54:35 -080030#include <utils/String8.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070031
32#include <binder/BinderService.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070033
34#include <gui/Sensor.h>
Mathias Agopianb3989272011-10-20 18:42:02 -070035#include <gui/BitTube.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070036#include <gui/ISensorServer.h>
37#include <gui/ISensorEventConnection.h>
38
Mathias Agopianf001c922010-11-11 17:58:51 -080039#include "SensorInterface.h"
40
Bernhard Rosenkränzer72952ef2014-11-17 21:03:39 +010041#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 Agopianfc328812010-07-14 23:41:37 -070048// ---------------------------------------------------------------------------
49
Mathias Agopiana1b7db92011-05-27 16:23:58 -070050#define DEBUG_CONNECTIONS false
Aravind Akella56ae4262014-07-10 16:01:10 -070051// 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 Akella4c8b9512013-09-05 17:03:38 -070054#define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024
Mathias Agopiana1b7db92011-05-27 16:23:58 -070055
Aravind Akella444f2672015-05-07 12:40:52 -070056#define CIRCULAR_BUF_SIZE 10
Aravind Akella18d6d512015-06-18 14:18:28 -070057#define SENSOR_REGISTRATIONS_BUF_SIZE 20
Aravind Akella444f2672015-05-07 12:40:52 -070058
Mathias Agopianfc328812010-07-14 23:41:37 -070059struct sensors_poll_device_t;
60struct sensors_module_t;
61
62namespace android {
63// ---------------------------------------------------------------------------
64
65class SensorService :
66 public BinderService<SensorService>,
67 public BnSensorServer,
68 protected Thread
69{
Mathias Agopianb6df7d02013-05-09 14:53:35 -070070 friend class BinderService<SensorService>;
Mathias Agopianfc328812010-07-14 23:41:37 -070071
Aravind Akella4949c502015-02-11 15:54:35 -080072 enum Mode {
73 // The regular operating mode where any application can register/unregister/call flush on
74 // sensors.
75 NORMAL = 0,
Aravind Akellaa9e6cc32015-04-16 18:57:31 -070076 // 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 Akella4949c502015-02-11 15:54:35 -080086 // 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 Akellaa9e6cc32015-04-16 18:57:31 -070092 // these apps can register/unregister/call flush() on sensors. If SensorService switches to
Aravind Akella4949c502015-02-11 15:54:35 -080093 // 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 Akellaa9e6cc32015-04-16 18:57:31 -070098 RESTRICTED = 2
99
100 // State Transitions supported.
101 // RESTRICTED <--- NORMAL ---> DATA_INJECTION
102 // ---> <---
Aravind Akella841a5922015-06-29 12:37:48 -0700103
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 Akella4949c502015-02-11 15:54:35 -0800115 };
116
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700117 static const char* WAKE_LOCK_NAME;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700118
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700119 static char const* getServiceName() ANDROID_API { return "sensorservice"; }
120 SensorService() ANDROID_API;
Mathias Agopianfc328812010-07-14 23:41:37 -0700121 virtual ~SensorService();
122
123 virtual void onFirstRef();
124
125 // Thread interface
126 virtual bool threadLoop();
127
128 // ISensorServer interface
Svetoslavb412f6e2015-04-29 16:50:41 -0700129 virtual Vector<Sensor> getSensorList(const String16& opPackageName);
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700130 virtual sp<ISensorEventConnection> createSensorEventConnection(const String8& packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -0700131 int requestedMode, const String16& opPackageName);
Aravind Akella841a5922015-06-29 12:37:48 -0700132 virtual int isDataInjectionEnabled();
Jani Suonperad4db70a2015-10-09 11:45:57 +0300133 virtual status_t setSensorPhysicalData(const char* physicaldata);
Mathias Agopianfc328812010-07-14 23:41:37 -0700134 virtual status_t dump(int fd, const Vector<String16>& args);
135
Aravind Akella56ae4262014-07-10 16:01:10 -0700136 class SensorEventConnection : public BnSensorEventConnection, public LooperCallback {
137 friend class SensorService;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700138 virtual ~SensorEventConnection();
139 virtual void onFirstRef();
Mathias Agopianb3989272011-10-20 18:42:02 -0700140 virtual sp<BitTube> getSensorChannel() const;
Aravind Akella724d91d2013-06-27 12:04:23 -0700141 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
142 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
143 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
Aravind Akella701166d2013-10-08 14:59:26 -0700144 virtual status_t flush();
Aravind Akella4c8b9512013-09-05 17:03:38 -0700145 // Count the number of flush complete events which are about to be dropped in the buffer.
146 // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be
147 // sent separately before the next batch of events.
Aravind Akella0ec20662014-09-14 17:29:48 -0700148 void countFlushCompleteEventsLocked(sensors_event_t const* scratch, int numEventsDropped);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700149
Aravind Akella6c2664a2014-08-13 12:24:50 -0700150 // Check if there are any wake up events in the buffer. If yes, return the index of the
151 // first wake_up sensor event in the buffer else return -1. This wake_up sensor event will
152 // have the flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have
153 // the wake_up flag set. SOCK_SEQPACKET ensures that either the entire packet is read or
154 // dropped.
155 int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count);
156
157 // Send pending flush_complete events. There may have been flush_complete_events that are
158 // dropped which need to be sent separately before other events. On older HALs (1_0) this
159 // method emulates the behavior of flush().
160 void sendPendingFlushEventsLocked();
Aravind Akella56ae4262014-07-10 16:01:10 -0700161
162 // Writes events from mEventCache to the socket.
Aravind Akellab4373ac2014-10-29 17:55:20 -0700163 void writeToSocketFromCache();
Aravind Akella56ae4262014-07-10 16:01:10 -0700164
165 // Compute the approximate cache size from the FIFO sizes of various sensors registered for
166 // this connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be
167 // shared amongst wake-up sensors and non-wake up sensors.
168 int computeMaxCacheSizeLocked() const;
169
Aravind Akella6c2664a2014-08-13 12:24:50 -0700170 // When more sensors register, the maximum cache size desired may change. Compute max cache
171 // size, reallocate memory and copy over events from the older cache.
172 void reAllocateCacheLocked(sensors_event_t const* scratch, int count);
173
Aravind Akella56ae4262014-07-10 16:01:10 -0700174 // LooperCallback method. If there is data to read on this fd, it is an ack from the
175 // app that it has read events from a wake up sensor, decrement mWakeLockRefCount.
176 // If this fd is available for writing send the data from the cache.
177 virtual int handleEvent(int fd, int events, void* data);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800178
Aravind Akella8a969552014-09-28 17:52:41 -0700179 // Increment mPendingFlushEventsToSend for the given sensor handle.
180 void incrementPendingFlushCount(int32_t handle);
181
182 // Add or remove the file descriptor associated with the BitTube to the looper. If mDead is
183 // set to true or there are no more sensors for this connection, the file descriptor is
184 // removed if it has been previously added to the Looper. Depending on the state of the
185 // connection FD may be added to the Looper. The flags to set are determined by the internal
186 // state of the connection. FDs are added to the looper when wake-up sensors are registered
187 // (to poll for acknowledgements) and when write fails on the socket when there are too many
Aravind Akella8a969552014-09-28 17:52:41 -0700188 // error and the other end hangs up or when this client unregisters for this connection.
189 void updateLooperRegistration(const sp<Looper>& looper);
190 void updateLooperRegistrationLocked(const sp<Looper>& looper);
191
Mathias Agopianfc328812010-07-14 23:41:37 -0700192 sp<SensorService> const mService;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700193 sp<BitTube> mChannel;
Mathias Agopian5307d172012-09-18 17:02:43 -0700194 uid_t mUid;
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800195 mutable Mutex mConnectionLock;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800196 // Number of events from wake up sensors which are still pending and haven't been delivered
197 // to the corresponding application. It is incremented by one unit for each write to the
198 // socket.
Aravind Akella8a969552014-09-28 17:52:41 -0700199 uint32_t mWakeLockRefCount;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700200
Aravind Akella8a969552014-09-28 17:52:41 -0700201 // If this flag is set to true, it means that the file descriptor associated with the
202 // BitTube has been added to the Looper in SensorService. This flag is typically set when
203 // this connection has wake-up sensors associated with it or when write has failed on this
204 // connection and we're storing some events in the cache.
205 bool mHasLooperCallbacks;
206 // If there are any errors associated with the Looper this flag is set to true and
207 // mWakeLockRefCount is reset to zero. needsWakeLock method will always return false, if
208 // this flag is set.
209 bool mDead;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700210
211 bool mDataInjectionMode;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700212 struct FlushInfo {
213 // The number of flush complete events dropped for this sensor is stored here.
214 // They are sent separately before the next batch of events.
215 int mPendingFlushEventsToSend;
216 // Every activate is preceded by a flush. Only after the first flush complete is
217 // received, the events for the sensor are sent on that *connection*.
218 bool mFirstFlushPending;
Aravind Akella6c2664a2014-08-13 12:24:50 -0700219 FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
Aravind Akella4c8b9512013-09-05 17:03:38 -0700220 };
221 // protected by SensorService::mLock. Key for this vector is the sensor handle.
222 KeyedVector<int, FlushInfo> mSensorInfo;
Aravind Akella56ae4262014-07-10 16:01:10 -0700223 sensors_event_t *mEventCache;
224 int mCacheSize, mMaxCacheSize;
Aravind Akella4949c502015-02-11 15:54:35 -0800225 String8 mPackageName;
Svetoslavb412f6e2015-04-29 16:50:41 -0700226 const String16 mOpPackageName;
Aravind Akella56ae4262014-07-10 16:01:10 -0700227#if DEBUG_CONNECTIONS
228 int mEventsReceived, mEventsSent, mEventsSentFromCache;
Aravind Akellae74baf62014-08-21 12:28:35 -0700229 int mTotalAcksNeeded, mTotalAcksReceived;
Aravind Akella56ae4262014-07-10 16:01:10 -0700230#endif
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700231
Mathias Agopianfc328812010-07-14 23:41:37 -0700232 public:
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700233 SensorEventConnection(const sp<SensorService>& service, uid_t uid, String8 packageName,
Svetoslavb412f6e2015-04-29 16:50:41 -0700234 bool isDataInjectionMode, const String16& opPackageName);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700235
Aravind Akella0ec20662014-09-14 17:29:48 -0700236 status_t sendEvents(sensors_event_t const* buffer, size_t count,
Aravind Akella8493b792014-09-08 15:45:47 -0700237 sensors_event_t* scratch,
238 SensorEventConnection const * const * mapFlushEventsToConnections = NULL);
Mathias Agopianfc328812010-07-14 23:41:37 -0700239 bool hasSensor(int32_t handle) const;
240 bool hasAnySensor() const;
Aravind Akella8493b792014-09-08 15:45:47 -0700241 bool hasOneShotSensors() const;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700242 bool addSensor(int32_t handle);
243 bool removeSensor(int32_t handle);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700244 void setFirstFlushPending(int32_t handle, bool value);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700245 void dump(String8& result);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800246 bool needsWakeLock();
Aravind Akellab4373ac2014-10-29 17:55:20 -0700247 void resetWakeLockRefCount();
Aravind Akella4949c502015-02-11 15:54:35 -0800248 String8 getPackageName() const;
Mathias Agopian5307d172012-09-18 17:02:43 -0700249
250 uid_t getUid() const { return mUid; }
Mathias Agopianfc328812010-07-14 23:41:37 -0700251 };
252
253 class SensorRecord {
254 SortedVector< wp<SensorEventConnection> > mConnections;
Aravind Akella6c2664a2014-08-13 12:24:50 -0700255 // A queue of all flush() calls made on this sensor. Flush complete events will be
256 // sent in this order.
257 Vector< wp<SensorEventConnection> > mPendingFlushConnections;
Mathias Agopianfc328812010-07-14 23:41:37 -0700258 public:
259 SensorRecord(const sp<SensorEventConnection>& connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700260 bool addConnection(const sp<SensorEventConnection>& connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700261 bool removeConnection(const wp<SensorEventConnection>& connection);
262 size_t getNumConnections() const { return mConnections.size(); }
Aravind Akella6c2664a2014-08-13 12:24:50 -0700263
264 void addPendingFlushConnection(const sp<SensorEventConnection>& connection);
265 void removeFirstPendingFlushConnection();
266 SensorEventConnection * getFirstPendingFlushConnection();
Aravind Akella4949c502015-02-11 15:54:35 -0800267 void clearAllPendingFlushConnections();
Mathias Agopianfc328812010-07-14 23:41:37 -0700268 };
269
Aravind Akella56ae4262014-07-10 16:01:10 -0700270 class SensorEventAckReceiver : public Thread {
271 sp<SensorService> const mService;
272 public:
273 virtual bool threadLoop();
274 SensorEventAckReceiver(const sp<SensorService>& service): mService(service) {}
275 };
276
Aravind Akella444f2672015-05-07 12:40:52 -0700277 // sensor_event_t with only the data and the timestamp.
278 struct TrimmedSensorEvent {
279 union {
280 float *mData;
281 uint64_t mStepCounter;
282 };
283 // Timestamp from the sensor_event.
284 int64_t mTimestamp;
285 // HH:MM:SS local time at which this sensor event is read at SensorService. Useful
286 // for debugging.
287 int32_t mHour, mMin, mSec;
288
Aravind Akella18d6d512015-06-18 14:18:28 -0700289 TrimmedSensorEvent(int sensorType);
290 static bool isSentinel(const TrimmedSensorEvent& event);
Aravind Akella444f2672015-05-07 12:40:52 -0700291
292 ~TrimmedSensorEvent() {
293 delete [] mData;
294 }
295 };
296
297 // A circular buffer of TrimmedSensorEvents. The size of this buffer is typically 10. The
298 // last N events generated from the sensor are stored in this buffer. The buffer is NOT
299 // cleared when the sensor unregisters and as a result one may see very old data in the
300 // dumpsys output but this is WAI.
301 class CircularBuffer {
302 int mNextInd;
303 int mSensorType;
Aravind Akella18d6d512015-06-18 14:18:28 -0700304 int mBufSize;
Aravind Akella444f2672015-05-07 12:40:52 -0700305 TrimmedSensorEvent ** mTrimmedSensorEventArr;
306 public:
307 CircularBuffer(int sensor_event_type);
308 void addEvent(const sensors_event_t& sensor_event);
309 void printBuffer(String8& buffer) const;
310 bool populateLastEvent(sensors_event_t *event);
311 ~CircularBuffer();
312 };
313
Aravind Akella18d6d512015-06-18 14:18:28 -0700314 struct SensorRegistrationInfo {
315 int32_t mSensorHandle;
316 String8 mPackageName;
317 bool mActivated;
318 int32_t mSamplingRateUs;
319 int32_t mMaxReportLatencyUs;
320 int32_t mHour, mMin, mSec;
321
322 SensorRegistrationInfo() : mPackageName() {
323 mSensorHandle = mSamplingRateUs = mMaxReportLatencyUs = INT32_MIN;
324 mHour = mMin = mSec = INT32_MIN;
325 mActivated = false;
326 }
327
328 static bool isSentinel(const SensorRegistrationInfo& info) {
329 return (info.mHour == INT32_MIN && info.mMin == INT32_MIN && info.mSec == INT32_MIN);
330 }
331 };
332
Aravind Akella444f2672015-05-07 12:40:52 -0700333 static int getNumEventsForSensorType(int sensor_event_type);
Mathias Agopianf001c922010-11-11 17:58:51 -0800334 String8 getSensorName(int handle) const;
Aravind Akellab4099e72013-10-15 15:43:10 -0700335 bool isVirtualSensor(int handle) const;
Aravind Akella70018042014-04-07 22:52:37 +0000336 Sensor getSensorFromHandle(int handle) const;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800337 bool isWakeUpSensor(int type) const;
Aravind Akella0ec20662014-09-14 17:29:48 -0700338 void recordLastValueLocked(sensors_event_t const* buffer, size_t count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800339 static void sortEventBuffer(sensors_event_t* buffer, size_t count);
Mathias Agopian03193062013-05-10 19:32:39 -0700340 Sensor registerSensor(SensorInterface* sensor);
341 Sensor registerVirtualSensor(SensorInterface* sensor);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700342 status_t cleanupWithoutDisable(
343 const sp<SensorEventConnection>& connection, int handle);
344 status_t cleanupWithoutDisableLocked(
345 const sp<SensorEventConnection>& connection, int handle);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800346 void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700347 sensors_event_t const* buffer, const int count);
Svetoslavb412f6e2015-04-29 16:50:41 -0700348 static bool canAccessSensor(const Sensor& sensor, const char* operation,
349 const String16& opPackageName);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800350 // SensorService acquires a partial wakelock for delivering events from wake up sensors. This
351 // method checks whether all the events from these wake up sensors have been delivered to the
352 // corresponding applications, if yes the wakelock is released.
353 void checkWakeLockState();
354 void checkWakeLockStateLocked();
Aravind Akellab4373ac2014-10-29 17:55:20 -0700355 bool isWakeLockAcquired();
Aravind Akella9a844cf2014-02-11 18:58:52 -0800356 bool isWakeUpSensorEvent(const sensors_event_t& event) const;
Aravind Akella56ae4262014-07-10 16:01:10 -0700357
Aravind Akella6c2664a2014-08-13 12:24:50 -0700358 SensorRecord * getSensorRecord(int handle);
359
Aravind Akella56ae4262014-07-10 16:01:10 -0700360 sp<Looper> getLooper() const;
361
Aravind Akellab4373ac2014-10-29 17:55:20 -0700362 // Reset mWakeLockRefCounts for all SensorEventConnections to zero. This may happen if
363 // SensorService did not receive any acknowledgements from apps which have registered for
364 // wake_up sensors.
365 void resetAllWakeLockRefCounts();
366
367 // Acquire or release wake_lock. If wake_lock is acquired, set the timeout in the looper to
368 // 5 seconds and wake the looper.
369 void setWakeLockAcquiredLocked(bool acquire);
370
371 // Send events from the event cache for this particular connection.
372 void sendEventsFromCache(const sp<SensorEventConnection>& connection);
373
374 // Promote all weak referecences in mActiveConnections vector to strong references and add them
375 // to the output vector.
376 void populateActiveConnections(SortedVector< sp<SensorEventConnection> >* activeConnections);
377
Aravind Akella4949c502015-02-11 15:54:35 -0800378 // If SensorService is operating in RESTRICTED mode, only select whitelisted packages are
379 // allowed to register for or call flush on sensors. Typically only cts test packages are
380 // allowed.
381 bool isWhiteListedPackage(const String8& packageName);
382
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700383 // Reset the state of SensorService to NORMAL mode.
384 status_t resetToNormalMode();
385 status_t resetToNormalModeLocked();
386
Mathias Agopianfc328812010-07-14 23:41:37 -0700387 // constants
388 Vector<Sensor> mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700389 Vector<Sensor> mUserSensorListDebug;
Mathias Agopian010e4222011-06-08 20:06:50 -0700390 Vector<Sensor> mUserSensorList;
Mathias Agopianf001c922010-11-11 17:58:51 -0800391 DefaultKeyedVector<int, SensorInterface*> mSensorMap;
392 Vector<SensorInterface *> mVirtualSensorList;
Mathias Agopian50df2952010-07-19 19:09:10 -0700393 status_t mInitCheck;
Aravind Akella5466c3d2014-08-22 16:11:10 -0700394 // Socket buffersize used to initialize BitTube. This size depends on whether batching is
395 // supported or not.
Aravind Akella8a969552014-09-28 17:52:41 -0700396 uint32_t mSocketBufferSize;
Aravind Akella56ae4262014-07-10 16:01:10 -0700397 sp<Looper> mLooper;
Aravind Akellab4373ac2014-10-29 17:55:20 -0700398 sp<SensorEventAckReceiver> mAckReceiver;
Mathias Agopianfc328812010-07-14 23:41:37 -0700399
400 // protected by mLock
401 mutable Mutex mLock;
Mathias Agopianfc328812010-07-14 23:41:37 -0700402 DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
Mathias Agopianf001c922010-11-11 17:58:51 -0800403 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
Mathias Agopianfc328812010-07-14 23:41:37 -0700404 SortedVector< wp<SensorEventConnection> > mActiveConnections;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800405 bool mWakeLockAcquired;
Aravind Akella8493b792014-09-08 15:45:47 -0700406 sensors_event_t *mSensorEventBuffer, *mSensorEventScratch;
407 SensorEventConnection const **mMapFlushEventsToConnections;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700408 Mode mCurrentOperatingMode;
Aravind Akella841a5922015-06-29 12:37:48 -0700409 // This packagaName is set when SensorService is in RESTRICTED or DATA_INJECTION mode. Only
410 // applications with this packageName are allowed to activate/deactivate or call flush on
411 // sensors. To run CTS this is can be set to ".cts." and only CTS tests will get access to
412 // sensors.
413 String8 mWhiteListedPackage;
Aravind Akella8493b792014-09-08 15:45:47 -0700414
Mathias Agopian3560fb22010-07-22 21:24:39 -0700415 // The size of this vector is constant, only the items are mutable
Aravind Akella444f2672015-05-07 12:40:52 -0700416 KeyedVector<int32_t, CircularBuffer *> mLastEventSeen;
Mathias Agopian3560fb22010-07-22 21:24:39 -0700417
Aravind Akella18d6d512015-06-18 14:18:28 -0700418 int mNextSensorRegIndex;
419 Vector<SensorRegistrationInfo> mLastNSensorRegistrations;
Mathias Agopianfc328812010-07-14 23:41:37 -0700420public:
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800421 void cleanupConnection(SensorEventConnection* connection);
Aravind Akella724d91d2013-06-27 12:04:23 -0700422 status_t enable(const sp<SensorEventConnection>& connection, int handle,
Svetoslavb412f6e2015-04-29 16:50:41 -0700423 nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags,
424 const String16& opPackageName);
Mathias Agopianfc328812010-07-14 23:41:37 -0700425 status_t disable(const sp<SensorEventConnection>& connection, int handle);
Svetoslavb412f6e2015-04-29 16:50:41 -0700426 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns,
427 const String16& opPackageName);
428 status_t flushSensor(const sp<SensorEventConnection>& connection,
429 const String16& opPackageName);
Mathias Agopianfc328812010-07-14 23:41:37 -0700430};
431
432// ---------------------------------------------------------------------------
433}; // namespace android
434
435#endif // ANDROID_SENSOR_SERVICE_H