blob: 5fd56b8b5de41284eb37c0f376ef9977f9ff1812 [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>
27#include <utils/RefBase.h>
28
29#include <binder/BinderService.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070030
31#include <gui/Sensor.h>
Mathias Agopianb3989272011-10-20 18:42:02 -070032#include <gui/BitTube.h>
Mathias Agopianfc328812010-07-14 23:41:37 -070033#include <gui/ISensorServer.h>
34#include <gui/ISensorEventConnection.h>
35
Mathias Agopianf001c922010-11-11 17:58:51 -080036#include "SensorInterface.h"
37
Mathias Agopianfc328812010-07-14 23:41:37 -070038// ---------------------------------------------------------------------------
39
Mathias Agopiana1b7db92011-05-27 16:23:58 -070040#define DEBUG_CONNECTIONS false
Aravind Akella4c8b9512013-09-05 17:03:38 -070041// Max size is 1 MB which is enough to accept a batch of about 10k events.
42#define MAX_SOCKET_BUFFER_SIZE_BATCHED 1024 * 1024
43#define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024
Aravind Akella9a844cf2014-02-11 18:58:52 -080044#define WAKE_UP_SENSOR_EVENT_NEEDS_ACK (1 << 31)
Mathias Agopiana1b7db92011-05-27 16:23:58 -070045
Mathias Agopianfc328812010-07-14 23:41:37 -070046struct sensors_poll_device_t;
47struct sensors_module_t;
48
49namespace android {
50// ---------------------------------------------------------------------------
51
52class SensorService :
53 public BinderService<SensorService>,
54 public BnSensorServer,
55 protected Thread
56{
Mathias Agopianb6df7d02013-05-09 14:53:35 -070057 friend class BinderService<SensorService>;
Mathias Agopianfc328812010-07-14 23:41:37 -070058
Mathias Agopianb6df7d02013-05-09 14:53:35 -070059 static const char* WAKE_LOCK_NAME;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070060
Mathias Agopianb6df7d02013-05-09 14:53:35 -070061 static char const* getServiceName() ANDROID_API { return "sensorservice"; }
62 SensorService() ANDROID_API;
Mathias Agopianfc328812010-07-14 23:41:37 -070063 virtual ~SensorService();
64
65 virtual void onFirstRef();
66
67 // Thread interface
68 virtual bool threadLoop();
69
70 // ISensorServer interface
71 virtual Vector<Sensor> getSensorList();
72 virtual sp<ISensorEventConnection> createSensorEventConnection();
73 virtual status_t dump(int fd, const Vector<String16>& args);
74
Mathias Agopianfc328812010-07-14 23:41:37 -070075 class SensorEventConnection : public BnSensorEventConnection {
Mathias Agopian7c1c5312010-07-21 15:59:50 -070076 virtual ~SensorEventConnection();
77 virtual void onFirstRef();
Mathias Agopianb3989272011-10-20 18:42:02 -070078 virtual sp<BitTube> getSensorChannel() const;
Aravind Akella724d91d2013-06-27 12:04:23 -070079 virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
80 nsecs_t maxBatchReportLatencyNs, int reservedFlags);
81 virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
Aravind Akella701166d2013-10-08 14:59:26 -070082 virtual status_t flush();
Aravind Akella9a844cf2014-02-11 18:58:52 -080083 void decreaseWakeLockRefCount();
Aravind Akella4c8b9512013-09-05 17:03:38 -070084 // Count the number of flush complete events which are about to be dropped in the buffer.
85 // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be
86 // sent separately before the next batch of events.
Aravind Akellac551eac2013-10-14 17:04:42 -070087 void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070088
Aravind Akella9a844cf2014-02-11 18:58:52 -080089 // Check if there are any wake up events in the buffer. If yes, increment the ref count.
90 // Increment it by exactly one unit for each packet sent on the socket. SOCK_SEQPACKET for
91 // the socket ensures that either the entire packet is read or dropped.
92 // Return 1 if mWakeLockRefCount has been incremented, zero if not.
93 int countWakeUpSensorEventsLocked(sensors_event_t* scratch, const int count);
94
Mathias Agopianfc328812010-07-14 23:41:37 -070095 sp<SensorService> const mService;
Aravind Akella4c8b9512013-09-05 17:03:38 -070096 sp<BitTube> mChannel;
Mathias Agopian5307d172012-09-18 17:02:43 -070097 uid_t mUid;
Mathias Agopian71d7a5c2010-11-14 20:55:25 -080098 mutable Mutex mConnectionLock;
Aravind Akella9a844cf2014-02-11 18:58:52 -080099 // Number of events from wake up sensors which are still pending and haven't been delivered
100 // to the corresponding application. It is incremented by one unit for each write to the
101 // socket.
102 int mWakeLockRefCount;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700103
Aravind Akella4c8b9512013-09-05 17:03:38 -0700104 struct FlushInfo {
105 // The number of flush complete events dropped for this sensor is stored here.
106 // They are sent separately before the next batch of events.
107 int mPendingFlushEventsToSend;
108 // Every activate is preceded by a flush. Only after the first flush complete is
109 // received, the events for the sensor are sent on that *connection*.
110 bool mFirstFlushPending;
111 FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
112 };
113 // protected by SensorService::mLock. Key for this vector is the sensor handle.
114 KeyedVector<int, FlushInfo> mSensorInfo;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700115
Mathias Agopianfc328812010-07-14 23:41:37 -0700116 public:
Mathias Agopian5307d172012-09-18 17:02:43 -0700117 SensorEventConnection(const sp<SensorService>& service, uid_t uid);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700118
Mathias Agopiancf510012010-07-22 16:18:10 -0700119 status_t sendEvents(sensors_event_t const* buffer, size_t count,
Aravind Akella9a844cf2014-02-11 18:58:52 -0800120 sensors_event_t* scratch);
Mathias Agopianfc328812010-07-14 23:41:37 -0700121 bool hasSensor(int32_t handle) const;
122 bool hasAnySensor() const;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700123 bool addSensor(int32_t handle);
124 bool removeSensor(int32_t handle);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700125 void setFirstFlushPending(int32_t handle, bool value);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700126 void dump(String8& result);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800127 bool needsWakeLock();
Mathias Agopian5307d172012-09-18 17:02:43 -0700128
129 uid_t getUid() const { return mUid; }
Mathias Agopianfc328812010-07-14 23:41:37 -0700130 };
131
132 class SensorRecord {
133 SortedVector< wp<SensorEventConnection> > mConnections;
134 public:
135 SensorRecord(const sp<SensorEventConnection>& connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700136 bool addConnection(const sp<SensorEventConnection>& connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700137 bool removeConnection(const wp<SensorEventConnection>& connection);
138 size_t getNumConnections() const { return mConnections.size(); }
139 };
140
Mathias Agopianf001c922010-11-11 17:58:51 -0800141 String8 getSensorName(int handle) const;
Aravind Akellab4099e72013-10-15 15:43:10 -0700142 bool isVirtualSensor(int handle) const;
Aravind Akella70018042014-04-07 22:52:37 +0000143 Sensor getSensorFromHandle(int handle) const;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800144 bool isWakeUpSensor(int type) const;
145 void recordLastValueLocked(const sensors_event_t* buffer, size_t count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800146 static void sortEventBuffer(sensors_event_t* buffer, size_t count);
Mathias Agopian03193062013-05-10 19:32:39 -0700147 Sensor registerSensor(SensorInterface* sensor);
148 Sensor registerVirtualSensor(SensorInterface* sensor);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700149 status_t cleanupWithoutDisable(
150 const sp<SensorEventConnection>& connection, int handle);
151 status_t cleanupWithoutDisableLocked(
152 const sp<SensorEventConnection>& connection, int handle);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800153 void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700154 sensors_event_t const* buffer, const int count);
Aravind Akella70018042014-04-07 22:52:37 +0000155 static bool canAccessSensor(const Sensor& sensor);
156 static bool verifyCanAccessSensor(const Sensor& sensor, const char* operation);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800157 // SensorService acquires a partial wakelock for delivering events from wake up sensors. This
158 // method checks whether all the events from these wake up sensors have been delivered to the
159 // corresponding applications, if yes the wakelock is released.
160 void checkWakeLockState();
161 void checkWakeLockStateLocked();
162 bool isWakeUpSensorEvent(const sensors_event_t& event) const;
Mathias Agopianfc328812010-07-14 23:41:37 -0700163 // constants
164 Vector<Sensor> mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700165 Vector<Sensor> mUserSensorListDebug;
Mathias Agopian010e4222011-06-08 20:06:50 -0700166 Vector<Sensor> mUserSensorList;
Mathias Agopianf001c922010-11-11 17:58:51 -0800167 DefaultKeyedVector<int, SensorInterface*> mSensorMap;
168 Vector<SensorInterface *> mVirtualSensorList;
Mathias Agopian50df2952010-07-19 19:09:10 -0700169 status_t mInitCheck;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700170 size_t mSocketBufferSize;
Mathias Agopianfc328812010-07-14 23:41:37 -0700171
172 // protected by mLock
173 mutable Mutex mLock;
Mathias Agopianfc328812010-07-14 23:41:37 -0700174 DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
Mathias Agopianf001c922010-11-11 17:58:51 -0800175 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
Mathias Agopianfc328812010-07-14 23:41:37 -0700176 SortedVector< wp<SensorEventConnection> > mActiveConnections;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800177 bool mWakeLockAcquired;
Mathias Agopianfc328812010-07-14 23:41:37 -0700178
Mathias Agopian3560fb22010-07-22 21:24:39 -0700179 // The size of this vector is constant, only the items are mutable
180 KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
181
Mathias Agopianfc328812010-07-14 23:41:37 -0700182public:
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800183 void cleanupConnection(SensorEventConnection* connection);
Aravind Akella724d91d2013-06-27 12:04:23 -0700184 status_t enable(const sp<SensorEventConnection>& connection, int handle,
185 nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700186 status_t disable(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700187 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
Aravind Akella724d91d2013-06-27 12:04:23 -0700188 status_t flushSensor(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700189};
190
191// ---------------------------------------------------------------------------
192}; // namespace android
193
194#endif // ANDROID_SENSOR_SERVICE_H