blob: 0887f28b5a4a5dc76b299c7804bae1a5706f1693 [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>
Mathias Agopianfc328812010-07-14 23:41:37 -070030
31#include <binder/BinderService.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070032
33#include <gui/Sensor.h>
Mathias Agopianb3989272011-10-20 18:42:02 -070034#include <gui/BitTube.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070035#include <gui/ISensorServer.h>
36#include <gui/ISensorEventConnection.h>
37
Mathias Agopianf001c922010-11-11 17:58:51 -080038#include "SensorInterface.h"
39
Mathias Agopianfc328812010-07-14 23:41:37 -070040// ---------------------------------------------------------------------------
41
Mathias Agopiana1b7db92011-05-27 16:23:58 -070042#define DEBUG_CONNECTIONS false
Aravind Akella56ae4262014-07-10 16:01:10 -070043// 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.
Aravind Akella4c8b9512013-09-05 17:03:38 -070046#define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024
Mathias Agopiana1b7db92011-05-27 16:23:58 -070047
Aravind Akella9e3adfc2014-09-03 15:48:05 -070048// Flags for sensors_event_t.flag. Using only the most significant two bits for flags.
49// MSB is to invalidate a sensor_event (typically a flush_complete_event) so that
50// it won't be used by other connections.
51// MSB 2nd bit is used to indicate whether the event needs to be acknowledged or not.
52// This is typically used for WAKE_UP sensors. WAKE_UP_SENSOR_EVENT_NEEDS_ACK is defined
53// in SensorEveneQueue.h
54#define SENSOR_EVENT_INVALID_FLAG (1U << 31)
55
Mathias Agopianfc328812010-07-14 23:41:37 -070056struct sensors_poll_device_t;
57struct sensors_module_t;
58
59namespace android {
60// ---------------------------------------------------------------------------
61
62class SensorService :
63 public BinderService<SensorService>,
64 public BnSensorServer,
65 protected Thread
66{
Mathias Agopianb6df7d02013-05-09 14:53:35 -070067 friend class BinderService<SensorService>;
Mathias Agopianfc328812010-07-14 23:41:37 -070068
Mathias Agopianb6df7d02013-05-09 14:53:35 -070069 static const char* WAKE_LOCK_NAME;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070070
Mathias Agopianb6df7d02013-05-09 14:53:35 -070071 static char const* getServiceName() ANDROID_API { return "sensorservice"; }
72 SensorService() ANDROID_API;
Mathias Agopianfc328812010-07-14 23:41:37 -070073 virtual ~SensorService();
74
75 virtual void onFirstRef();
76
77 // Thread interface
78 virtual bool threadLoop();
79
80 // ISensorServer interface
81 virtual Vector<Sensor> getSensorList();
82 virtual sp<ISensorEventConnection> createSensorEventConnection();
83 virtual status_t dump(int fd, const Vector<String16>& args);
84
Aravind Akella56ae4262014-07-10 16:01:10 -070085 class SensorEventConnection : public BnSensorEventConnection, public LooperCallback {
86 friend class SensorService;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070087 virtual ~SensorEventConnection();
88 virtual void onFirstRef();
Mathias Agopianb3989272011-10-20 18:42:02 -070089 virtual sp<BitTube> getSensorChannel() const;
Aravind Akella724d91d2013-06-27 12:04:23 -070090 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
91 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
92 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
Aravind Akella701166d2013-10-08 14:59:26 -070093 virtual status_t flush();
Aravind Akella4c8b9512013-09-05 17:03:38 -070094 // Count the number of flush complete events which are about to be dropped in the buffer.
95 // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be
96 // sent separately before the next batch of events.
Aravind Akellac551eac2013-10-14 17:04:42 -070097 void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070098
Aravind Akella6c2664a2014-08-13 12:24:50 -070099 // Check if there are any wake up events in the buffer. If yes, return the index of the
100 // first wake_up sensor event in the buffer else return -1. This wake_up sensor event will
101 // have the flag WAKE_UP_SENSOR_EVENT_NEEDS_ACK set. Exactly one event per packet will have
102 // the wake_up flag set. SOCK_SEQPACKET ensures that either the entire packet is read or
103 // dropped.
104 int findWakeUpSensorEventLocked(sensors_event_t const* scratch, int count);
105
106 // Send pending flush_complete events. There may have been flush_complete_events that are
107 // dropped which need to be sent separately before other events. On older HALs (1_0) this
108 // method emulates the behavior of flush().
109 void sendPendingFlushEventsLocked();
Aravind Akella56ae4262014-07-10 16:01:10 -0700110
111 // Writes events from mEventCache to the socket.
112 void writeToSocketFromCacheLocked();
113
114 // Compute the approximate cache size from the FIFO sizes of various sensors registered for
115 // this connection. Wake up and non-wake up sensors have separate FIFOs but FIFO may be
116 // shared amongst wake-up sensors and non-wake up sensors.
117 int computeMaxCacheSizeLocked() const;
118
Aravind Akella6c2664a2014-08-13 12:24:50 -0700119 // When more sensors register, the maximum cache size desired may change. Compute max cache
120 // size, reallocate memory and copy over events from the older cache.
121 void reAllocateCacheLocked(sensors_event_t const* scratch, int count);
122
Aravind Akella56ae4262014-07-10 16:01:10 -0700123 // LooperCallback method. If there is data to read on this fd, it is an ack from the
124 // app that it has read events from a wake up sensor, decrement mWakeLockRefCount.
125 // If this fd is available for writing send the data from the cache.
126 virtual int handleEvent(int fd, int events, void* data);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800127
Mathias Agopianfc328812010-07-14 23:41:37 -0700128 sp<SensorService> const mService;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700129 sp<BitTube> mChannel;
Mathias Agopian5307d172012-09-18 17:02:43 -0700130 uid_t mUid;
Mathias Agopian71d7a5c2010-11-14 20:55:25 -0800131 mutable Mutex mConnectionLock;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800132 // Number of events from wake up sensors which are still pending and haven't been delivered
133 // to the corresponding application. It is incremented by one unit for each write to the
134 // socket.
135 int mWakeLockRefCount;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700136
Aravind Akella4c8b9512013-09-05 17:03:38 -0700137 struct FlushInfo {
138 // The number of flush complete events dropped for this sensor is stored here.
139 // They are sent separately before the next batch of events.
140 int mPendingFlushEventsToSend;
141 // Every activate is preceded by a flush. Only after the first flush complete is
142 // received, the events for the sensor are sent on that *connection*.
143 bool mFirstFlushPending;
Aravind Akella6c2664a2014-08-13 12:24:50 -0700144 FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
Aravind Akella4c8b9512013-09-05 17:03:38 -0700145 };
146 // protected by SensorService::mLock. Key for this vector is the sensor handle.
147 KeyedVector<int, FlushInfo> mSensorInfo;
Aravind Akella56ae4262014-07-10 16:01:10 -0700148 sensors_event_t *mEventCache;
149 int mCacheSize, mMaxCacheSize;
150
151#if DEBUG_CONNECTIONS
152 int mEventsReceived, mEventsSent, mEventsSentFromCache;
Aravind Akellae74baf62014-08-21 12:28:35 -0700153 int mTotalAcksNeeded, mTotalAcksReceived;
Aravind Akella56ae4262014-07-10 16:01:10 -0700154#endif
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700155
Mathias Agopianfc328812010-07-14 23:41:37 -0700156 public:
Mathias Agopian5307d172012-09-18 17:02:43 -0700157 SensorEventConnection(const sp<SensorService>& service, uid_t uid);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700158
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700159 status_t sendEvents(sensors_event_t* buffer, size_t count,
Aravind Akella9a844cf2014-02-11 18:58:52 -0800160 sensors_event_t* scratch);
Mathias Agopianfc328812010-07-14 23:41:37 -0700161 bool hasSensor(int32_t handle) const;
162 bool hasAnySensor() const;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700163 bool addSensor(int32_t handle);
164 bool removeSensor(int32_t handle);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700165 void setFirstFlushPending(int32_t handle, bool value);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700166 void dump(String8& result);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800167 bool needsWakeLock();
Mathias Agopian5307d172012-09-18 17:02:43 -0700168
169 uid_t getUid() const { return mUid; }
Mathias Agopianfc328812010-07-14 23:41:37 -0700170 };
171
172 class SensorRecord {
173 SortedVector< wp<SensorEventConnection> > mConnections;
Aravind Akella6c2664a2014-08-13 12:24:50 -0700174 // A queue of all flush() calls made on this sensor. Flush complete events will be
175 // sent in this order.
176 Vector< wp<SensorEventConnection> > mPendingFlushConnections;
Mathias Agopianfc328812010-07-14 23:41:37 -0700177 public:
178 SensorRecord(const sp<SensorEventConnection>& connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700179 bool addConnection(const sp<SensorEventConnection>& connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700180 bool removeConnection(const wp<SensorEventConnection>& connection);
181 size_t getNumConnections() const { return mConnections.size(); }
Aravind Akella6c2664a2014-08-13 12:24:50 -0700182
183 void addPendingFlushConnection(const sp<SensorEventConnection>& connection);
184 void removeFirstPendingFlushConnection();
185 SensorEventConnection * getFirstPendingFlushConnection();
Mathias Agopianfc328812010-07-14 23:41:37 -0700186 };
187
Aravind Akella56ae4262014-07-10 16:01:10 -0700188 class SensorEventAckReceiver : public Thread {
189 sp<SensorService> const mService;
190 public:
191 virtual bool threadLoop();
192 SensorEventAckReceiver(const sp<SensorService>& service): mService(service) {}
193 };
194
Mathias Agopianf001c922010-11-11 17:58:51 -0800195 String8 getSensorName(int handle) const;
Aravind Akellab4099e72013-10-15 15:43:10 -0700196 bool isVirtualSensor(int handle) const;
Aravind Akella70018042014-04-07 22:52:37 +0000197 Sensor getSensorFromHandle(int handle) const;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800198 bool isWakeUpSensor(int type) const;
199 void recordLastValueLocked(const sensors_event_t* buffer, size_t count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800200 static void sortEventBuffer(sensors_event_t* buffer, size_t count);
Mathias Agopian03193062013-05-10 19:32:39 -0700201 Sensor registerSensor(SensorInterface* sensor);
202 Sensor registerVirtualSensor(SensorInterface* sensor);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700203 status_t cleanupWithoutDisable(
204 const sp<SensorEventConnection>& connection, int handle);
205 status_t cleanupWithoutDisableLocked(
206 const sp<SensorEventConnection>& connection, int handle);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800207 void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700208 sensors_event_t const* buffer, const int count);
Aravind Akella70018042014-04-07 22:52:37 +0000209 static bool canAccessSensor(const Sensor& sensor);
210 static bool verifyCanAccessSensor(const Sensor& sensor, const char* operation);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800211 // SensorService acquires a partial wakelock for delivering events from wake up sensors. This
212 // method checks whether all the events from these wake up sensors have been delivered to the
213 // corresponding applications, if yes the wakelock is released.
214 void checkWakeLockState();
215 void checkWakeLockStateLocked();
216 bool isWakeUpSensorEvent(const sensors_event_t& event) const;
Aravind Akella56ae4262014-07-10 16:01:10 -0700217
Aravind Akella6c2664a2014-08-13 12:24:50 -0700218 SensorRecord * getSensorRecord(int handle);
219
Aravind Akella56ae4262014-07-10 16:01:10 -0700220 sp<Looper> getLooper() const;
221
Mathias Agopianfc328812010-07-14 23:41:37 -0700222 // constants
223 Vector<Sensor> mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700224 Vector<Sensor> mUserSensorListDebug;
Mathias Agopian010e4222011-06-08 20:06:50 -0700225 Vector<Sensor> mUserSensorList;
Mathias Agopianf001c922010-11-11 17:58:51 -0800226 DefaultKeyedVector<int, SensorInterface*> mSensorMap;
227 Vector<SensorInterface *> mVirtualSensorList;
Mathias Agopian50df2952010-07-19 19:09:10 -0700228 status_t mInitCheck;
Aravind Akella5466c3d2014-08-22 16:11:10 -0700229 // Socket buffersize used to initialize BitTube. This size depends on whether batching is
230 // supported or not.
Aravind Akella4c8b9512013-09-05 17:03:38 -0700231 size_t mSocketBufferSize;
Aravind Akella56ae4262014-07-10 16:01:10 -0700232 sp<Looper> mLooper;
Mathias Agopianfc328812010-07-14 23:41:37 -0700233
234 // protected by mLock
235 mutable Mutex mLock;
Mathias Agopianfc328812010-07-14 23:41:37 -0700236 DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
Mathias Agopianf001c922010-11-11 17:58:51 -0800237 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
Mathias Agopianfc328812010-07-14 23:41:37 -0700238 SortedVector< wp<SensorEventConnection> > mActiveConnections;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800239 bool mWakeLockAcquired;
Mathias Agopian3560fb22010-07-22 21:24:39 -0700240 // The size of this vector is constant, only the items are mutable
241 KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
242
Mathias Agopianfc328812010-07-14 23:41:37 -0700243public:
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800244 void cleanupConnection(SensorEventConnection* connection);
Aravind Akella724d91d2013-06-27 12:04:23 -0700245 status_t enable(const sp<SensorEventConnection>& connection, int handle,
246 nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700247 status_t disable(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700248 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
Aravind Akella9e3adfc2014-09-03 15:48:05 -0700249 status_t flushSensor(const sp<SensorEventConnection>& connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700250};
251
252// ---------------------------------------------------------------------------
253}; // namespace android
254
255#endif // ANDROID_SENSOR_SERVICE_H