blob: 1dc2dd36aedfc17f974bcf7bc76b64f8ac16df47 [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
Mathias Agopiana1b7db92011-05-27 16:23:58 -070044
Mathias Agopianfc328812010-07-14 23:41:37 -070045struct sensors_poll_device_t;
46struct sensors_module_t;
47
48namespace android {
49// ---------------------------------------------------------------------------
50
51class SensorService :
52 public BinderService<SensorService>,
53 public BnSensorServer,
54 protected Thread
55{
Mathias Agopianb6df7d02013-05-09 14:53:35 -070056 friend class BinderService<SensorService>;
Mathias Agopianfc328812010-07-14 23:41:37 -070057
Mathias Agopianb6df7d02013-05-09 14:53:35 -070058 static const char* WAKE_LOCK_NAME;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070059
Mathias Agopianb6df7d02013-05-09 14:53:35 -070060 static char const* getServiceName() ANDROID_API { return "sensorservice"; }
61 SensorService() ANDROID_API;
Mathias Agopianfc328812010-07-14 23:41:37 -070062 virtual ~SensorService();
63
64 virtual void onFirstRef();
65
66 // Thread interface
67 virtual bool threadLoop();
68
69 // ISensorServer interface
70 virtual Vector<Sensor> getSensorList();
71 virtual sp<ISensorEventConnection> createSensorEventConnection();
72 virtual status_t dump(int fd, const Vector<String16>& args);
73
74
75 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 Akella4c8b9512013-09-05 17:03:38 -070083 // Count the number of flush complete events which are about to be dropped in the buffer.
84 // Increment mPendingFlushEventsToSend in mSensorInfo. These flush complete events will be
85 // sent separately before the next batch of events.
Aravind Akellac551eac2013-10-14 17:04:42 -070086 void countFlushCompleteEventsLocked(sensors_event_t* scratch, int numEventsDropped);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070087
Mathias Agopianfc328812010-07-14 23:41:37 -070088 sp<SensorService> const mService;
Aravind Akella4c8b9512013-09-05 17:03:38 -070089 sp<BitTube> mChannel;
Mathias Agopian5307d172012-09-18 17:02:43 -070090 uid_t mUid;
Mathias Agopian71d7a5c2010-11-14 20:55:25 -080091 mutable Mutex mConnectionLock;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070092
Aravind Akella4c8b9512013-09-05 17:03:38 -070093 struct FlushInfo {
94 // The number of flush complete events dropped for this sensor is stored here.
95 // They are sent separately before the next batch of events.
96 int mPendingFlushEventsToSend;
97 // Every activate is preceded by a flush. Only after the first flush complete is
98 // received, the events for the sensor are sent on that *connection*.
99 bool mFirstFlushPending;
100 FlushInfo() : mPendingFlushEventsToSend(0), mFirstFlushPending(false) {}
101 };
102 // protected by SensorService::mLock. Key for this vector is the sensor handle.
103 KeyedVector<int, FlushInfo> mSensorInfo;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700104
Mathias Agopianfc328812010-07-14 23:41:37 -0700105 public:
Mathias Agopian5307d172012-09-18 17:02:43 -0700106 SensorEventConnection(const sp<SensorService>& service, uid_t uid);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700107
Mathias Agopiancf510012010-07-22 16:18:10 -0700108 status_t sendEvents(sensors_event_t const* buffer, size_t count,
Mathias Agopian3560fb22010-07-22 21:24:39 -0700109 sensors_event_t* scratch = NULL);
Mathias Agopianfc328812010-07-14 23:41:37 -0700110 bool hasSensor(int32_t handle) const;
111 bool hasAnySensor() const;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700112 bool addSensor(int32_t handle);
113 bool removeSensor(int32_t handle);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700114 void setFirstFlushPending(int32_t handle, bool value);
Aravind Akella4c8b9512013-09-05 17:03:38 -0700115 void dump(String8& result);
Mathias Agopian5307d172012-09-18 17:02:43 -0700116
117 uid_t getUid() const { return mUid; }
Mathias Agopianfc328812010-07-14 23:41:37 -0700118 };
119
120 class SensorRecord {
121 SortedVector< wp<SensorEventConnection> > mConnections;
122 public:
123 SensorRecord(const sp<SensorEventConnection>& connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700124 bool addConnection(const sp<SensorEventConnection>& connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700125 bool removeConnection(const wp<SensorEventConnection>& connection);
126 size_t getNumConnections() const { return mConnections.size(); }
127 };
128
129 SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
Mathias Agopianf001c922010-11-11 17:58:51 -0800130 DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
Mathias Agopianfc328812010-07-14 23:41:37 -0700131
Mathias Agopianf001c922010-11-11 17:58:51 -0800132 String8 getSensorName(int handle) const;
Aravind Akellab4099e72013-10-15 15:43:10 -0700133 bool isVirtualSensor(int handle) const;
Aravind Akella4b847042014-03-03 19:02:46 -0800134 void recordLastValue(const sensors_event_t* buffer, size_t count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800135 static void sortEventBuffer(sensors_event_t* buffer, size_t count);
Mathias Agopian03193062013-05-10 19:32:39 -0700136 Sensor registerSensor(SensorInterface* sensor);
137 Sensor registerVirtualSensor(SensorInterface* sensor);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700138 status_t cleanupWithoutDisable(
139 const sp<SensorEventConnection>& connection, int handle);
140 status_t cleanupWithoutDisableLocked(
141 const sp<SensorEventConnection>& connection, int handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700142 void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700143 sensors_event_t const* buffer, const int count);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800144
Mathias Agopianfc328812010-07-14 23:41:37 -0700145 // constants
146 Vector<Sensor> mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700147 Vector<Sensor> mUserSensorListDebug;
Mathias Agopian010e4222011-06-08 20:06:50 -0700148 Vector<Sensor> mUserSensorList;
Mathias Agopianf001c922010-11-11 17:58:51 -0800149 DefaultKeyedVector<int, SensorInterface*> mSensorMap;
150 Vector<SensorInterface *> mVirtualSensorList;
Mathias Agopian50df2952010-07-19 19:09:10 -0700151 status_t mInitCheck;
Aravind Akella4c8b9512013-09-05 17:03:38 -0700152 size_t mSocketBufferSize;
Mathias Agopianfc328812010-07-14 23:41:37 -0700153
154 // protected by mLock
155 mutable Mutex mLock;
Mathias Agopianfc328812010-07-14 23:41:37 -0700156 DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
Mathias Agopianf001c922010-11-11 17:58:51 -0800157 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
Mathias Agopianfc328812010-07-14 23:41:37 -0700158 SortedVector< wp<SensorEventConnection> > mActiveConnections;
159
Mathias Agopian3560fb22010-07-22 21:24:39 -0700160 // The size of this vector is constant, only the items are mutable
161 KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
162
Mathias Agopianfc328812010-07-14 23:41:37 -0700163public:
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800164 void cleanupConnection(SensorEventConnection* connection);
Aravind Akella724d91d2013-06-27 12:04:23 -0700165 status_t enable(const sp<SensorEventConnection>& connection, int handle,
166 nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags);
Mathias Agopianfc328812010-07-14 23:41:37 -0700167 status_t disable(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700168 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
Aravind Akella724d91d2013-06-27 12:04:23 -0700169 status_t flushSensor(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopianfc328812010-07-14 23:41:37 -0700170};
171
172// ---------------------------------------------------------------------------
173}; // namespace android
174
175#endif // ANDROID_SENSOR_SERVICE_H