blob: da97286cd22a08246f235ae09daef55ca9335c82 [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__
Peng Xueb4d6282015-12-10 18:02:41 -080042// Clang warns about SensorEventConnection::dump hiding BBinder::dump. The cause isn't fixable
43// without changing the API, so let's tell clang this is indeed intentional.
Bernhard Rosenkränzer72952ef2014-11-17 21:03:39 +010044#pragma clang diagnostic ignored "-Woverloaded-virtual"
45#endif
46
Mathias Agopianfc328812010-07-14 23:41:37 -070047// ---------------------------------------------------------------------------
Peng Xuf66684a2015-07-23 11:41:53 -070048#define IGNORE_HARDWARE_FUSION false
Mathias Agopiana1b7db92011-05-27 16:23:58 -070049#define DEBUG_CONNECTIONS false
Aravind Akella56ae4262014-07-10 16:01:10 -070050// Max size is 100 KB which is enough to accept a batch of about 1000 events.
51#define MAX_SOCKET_BUFFER_SIZE_BATCHED 100 * 1024
52// For older HALs which don't support batching, use a smaller socket buffer size.
Aravind Akella4c8b9512013-09-05 17:03:38 -070053#define SOCKET_BUFFER_SIZE_NON_BATCHED 4 * 1024
Mathias Agopiana1b7db92011-05-27 16:23:58 -070054
Aravind Akella18d6d512015-06-18 14:18:28 -070055#define SENSOR_REGISTRATIONS_BUF_SIZE 20
Aravind Akella444f2672015-05-07 12:40:52 -070056
Mathias Agopianfc328812010-07-14 23:41:37 -070057namespace android {
58// ---------------------------------------------------------------------------
59
60class SensorService :
61 public BinderService<SensorService>,
62 public BnSensorServer,
63 protected Thread
64{
Peng Xueb4d6282015-12-10 18:02:41 -080065 // nested class/struct for internal use
66 class SensorEventConnection;
67
68public:
69 void cleanupConnection(SensorEventConnection* connection);
70
71 status_t enable(const sp<SensorEventConnection>& connection, int handle,
72 nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags,
73 const String16& opPackageName);
74
75 status_t disable(const sp<SensorEventConnection>& connection, int handle);
76
77 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns,
78 const String16& opPackageName);
79
80 status_t flushSensor(const sp<SensorEventConnection>& connection,
81 const String16& opPackageName);
82
83private:
Mathias Agopianb6df7d02013-05-09 14:53:35 -070084 friend class BinderService<SensorService>;
Mathias Agopianfc328812010-07-14 23:41:37 -070085
Peng Xueb4d6282015-12-10 18:02:41 -080086 // nested class/struct for internal use
87 class SensorRecord;
88 class SensorEventAckReceiver;
89 struct TrimmedSensorEvent;
90 class MostRecentEventLogger;
91 struct SensorRegistrationInfo;
92
Aravind Akella4949c502015-02-11 15:54:35 -080093 enum Mode {
94 // The regular operating mode where any application can register/unregister/call flush on
95 // sensors.
96 NORMAL = 0,
Peng Xueb4d6282015-12-10 18:02:41 -080097 // This mode is only used for testing purposes. Not all HALs support this mode. In this mode,
98 // the HAL ignores the sensor data provided by physical sensors and accepts the data that is
99 // injected from the SensorService as if it were the real sensor data. This mode is primarily
100 // used for testing various algorithms like vendor provided SensorFusion, Step Counter and
101 // Step Detector etc. Typically in this mode, there will be a client (a
102 // SensorEventConnection) which will be injecting sensor data into the HAL. Normal apps can
103 // unregister and register for any sensor that supports injection. Registering to sensors
104 // that do not support injection will give an error. TODO(aakella) : Allow exactly one
105 // client to inject sensor data at a time.
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700106 DATA_INJECTION = 1,
Aravind Akella4949c502015-02-11 15:54:35 -0800107 // This mode is used only for testing sensors. Each sensor can be tested in isolation with
108 // the required sampling_rate and maxReportLatency parameters without having to think about
109 // the data rates requested by other applications. End user devices are always expected to be
110 // in NORMAL mode. When this mode is first activated, all active sensors from all connections
111 // are disabled. Calling flush() will return an error. In this mode, only the requests from
112 // selected apps whose package names are whitelisted are allowed (typically CTS apps). Only
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700113 // these apps can register/unregister/call flush() on sensors. If SensorService switches to
Aravind Akella4949c502015-02-11 15:54:35 -0800114 // NORMAL mode again, all sensors that were previously registered to are activated with the
115 // corresponding paramaters if the application hasn't unregistered for sensors in the mean
Peng Xueb4d6282015-12-10 18:02:41 -0800116 // time. NOTE: Non whitelisted app whose sensors were previously deactivated may still
117 // receive events if a whitelisted app requests data from the same sensor.
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700118 RESTRICTED = 2
119
120 // State Transitions supported.
121 // RESTRICTED <--- NORMAL ---> DATA_INJECTION
122 // ---> <---
Aravind Akella5c538052015-06-29 12:37:48 -0700123
124 // Shell commands to switch modes in SensorService.
125 // 1) Put SensorService in RESTRICTED mode with packageName .cts. If it is already in
126 // restricted mode it is treated as a NO_OP (and packageName is NOT changed).
Peng Xueb4d6282015-12-10 18:02:41 -0800127 //
128 // $ adb shell dumpsys sensorservice restrict .cts.
Aravind Akella5c538052015-06-29 12:37:48 -0700129 //
130 // 2) Put SensorService in DATA_INJECTION mode with packageName .xts. If it is already in
131 // data_injection mode it is treated as a NO_OP (and packageName is NOT changed).
Peng Xueb4d6282015-12-10 18:02:41 -0800132 //
133 // $ adb shell dumpsys sensorservice data_injection .xts.
Aravind Akella5c538052015-06-29 12:37:48 -0700134 //
135 // 3) Reset sensorservice back to NORMAL mode.
Peng Xueb4d6282015-12-10 18:02:41 -0800136 // $ adb shell dumpsys sensorservice enable
Aravind Akella4949c502015-02-11 15:54:35 -0800137 };
138
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700139 static const char* WAKE_LOCK_NAME;
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700140
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700141 static char const* getServiceName() ANDROID_API { return "sensorservice"; }
142 SensorService() ANDROID_API;
Mathias Agopianfc328812010-07-14 23:41:37 -0700143 virtual ~SensorService();
144
145 virtual void onFirstRef();
146
147 // Thread interface
148 virtual bool threadLoop();
149
150 // ISensorServer interface
Svetoslavb412f6e2015-04-29 16:50:41 -0700151 virtual Vector<Sensor> getSensorList(const String16& opPackageName);
Peng Xu2576cb62016-01-20 00:22:09 -0800152 virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName);
Peng Xueb4d6282015-12-10 18:02:41 -0800153 virtual sp<ISensorEventConnection> createSensorEventConnection(
154 const String8& packageName,
155 int requestedMode, const String16& opPackageName);
Aravind Akella5c538052015-06-29 12:37:48 -0700156 virtual int isDataInjectionEnabled();
Mathias Agopianfc328812010-07-14 23:41:37 -0700157 virtual status_t dump(int fd, const Vector<String16>& args);
158
Aravind Akella18d6d512015-06-18 14:18:28 -0700159
Aravind Akella444f2672015-05-07 12:40:52 -0700160 static int getNumEventsForSensorType(int sensor_event_type);
Mathias Agopianf001c922010-11-11 17:58:51 -0800161 String8 getSensorName(int handle) const;
Aravind Akellab4099e72013-10-15 15:43:10 -0700162 bool isVirtualSensor(int handle) const;
Peng Xu47e96012016-03-28 17:55:56 -0700163 SensorInterface* getSensorInterfaceFromHandle(int handle) const;
Aravind Akella70018042014-04-07 22:52:37 +0000164 Sensor getSensorFromHandle(int handle) const;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800165 bool isWakeUpSensor(int type) const;
Aravind Akella0ec20662014-09-14 17:29:48 -0700166 void recordLastValueLocked(sensors_event_t const* buffer, size_t count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800167 static void sortEventBuffer(sensors_event_t* buffer, size_t count);
Mathias Agopian03193062013-05-10 19:32:39 -0700168 Sensor registerSensor(SensorInterface* sensor);
169 Sensor registerVirtualSensor(SensorInterface* sensor);
Peng Xu2576cb62016-01-20 00:22:09 -0800170 Sensor registerDynamicSensor(SensorInterface* sensor);
171 bool unregisterDynamicSensor(int handle);
Peng Xueb4d6282015-12-10 18:02:41 -0800172 status_t cleanupWithoutDisable(const sp<SensorEventConnection>& connection, int handle);
173 status_t cleanupWithoutDisableLocked(const sp<SensorEventConnection>& connection, int handle);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800174 void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700175 sensors_event_t const* buffer, const int count);
Svetoslavb412f6e2015-04-29 16:50:41 -0700176 static bool canAccessSensor(const Sensor& sensor, const char* operation,
177 const String16& opPackageName);
Aravind Akella9a844cf2014-02-11 18:58:52 -0800178 // SensorService acquires a partial wakelock for delivering events from wake up sensors. This
179 // method checks whether all the events from these wake up sensors have been delivered to the
180 // corresponding applications, if yes the wakelock is released.
181 void checkWakeLockState();
182 void checkWakeLockStateLocked();
Aravind Akellab4373ac2014-10-29 17:55:20 -0700183 bool isWakeLockAcquired();
Aravind Akella9a844cf2014-02-11 18:58:52 -0800184 bool isWakeUpSensorEvent(const sensors_event_t& event) const;
Peng Xu47e96012016-03-28 17:55:56 -0700185 bool isNewHandle(int handle);
Aravind Akella56ae4262014-07-10 16:01:10 -0700186
187 sp<Looper> getLooper() const;
188
Aravind Akellab4373ac2014-10-29 17:55:20 -0700189 // Reset mWakeLockRefCounts for all SensorEventConnections to zero. This may happen if
190 // SensorService did not receive any acknowledgements from apps which have registered for
191 // wake_up sensors.
192 void resetAllWakeLockRefCounts();
193
Peng Xueb4d6282015-12-10 18:02:41 -0800194 // Acquire or release wake_lock. If wake_lock is acquired, set the timeout in the looper to 5
195 // seconds and wake the looper.
Aravind Akellab4373ac2014-10-29 17:55:20 -0700196 void setWakeLockAcquiredLocked(bool acquire);
197
198 // Send events from the event cache for this particular connection.
199 void sendEventsFromCache(const sp<SensorEventConnection>& connection);
200
201 // Promote all weak referecences in mActiveConnections vector to strong references and add them
202 // to the output vector.
Peng Xueb4d6282015-12-10 18:02:41 -0800203 void populateActiveConnections( SortedVector< sp<SensorEventConnection> >* activeConnections);
Aravind Akellab4373ac2014-10-29 17:55:20 -0700204
Aravind Akella4949c502015-02-11 15:54:35 -0800205 // If SensorService is operating in RESTRICTED mode, only select whitelisted packages are
206 // allowed to register for or call flush on sensors. Typically only cts test packages are
207 // allowed.
208 bool isWhiteListedPackage(const String8& packageName);
209
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700210 // Reset the state of SensorService to NORMAL mode.
211 status_t resetToNormalMode();
212 status_t resetToNormalModeLocked();
213
Peng Xu47e96012016-03-28 17:55:56 -0700214 // lists and maps
Mathias Agopianfc328812010-07-14 23:41:37 -0700215 Vector<Sensor> mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700216 Vector<Sensor> mUserSensorListDebug;
Mathias Agopian010e4222011-06-08 20:06:50 -0700217 Vector<Sensor> mUserSensorList;
Peng Xu2576cb62016-01-20 00:22:09 -0800218 Vector<Sensor> mDynamicSensorList;
Mathias Agopianf001c922010-11-11 17:58:51 -0800219 DefaultKeyedVector<int, SensorInterface*> mSensorMap;
220 Vector<SensorInterface *> mVirtualSensorList;
Peng Xu47e96012016-03-28 17:55:56 -0700221 Vector<int> mUsedHandleList;
Mathias Agopian50df2952010-07-19 19:09:10 -0700222 status_t mInitCheck;
Peng Xueb4d6282015-12-10 18:02:41 -0800223
Aravind Akella5466c3d2014-08-22 16:11:10 -0700224 // Socket buffersize used to initialize BitTube. This size depends on whether batching is
225 // supported or not.
Peng Xueb4d6282015-12-10 18:02:41 -0800226 uint32_t mSocketBufferSize; sp<Looper> mLooper; sp<SensorEventAckReceiver> mAckReceiver;
Mathias Agopianfc328812010-07-14 23:41:37 -0700227
228 // protected by mLock
229 mutable Mutex mLock;
Mathias Agopianfc328812010-07-14 23:41:37 -0700230 DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
Mathias Agopianf001c922010-11-11 17:58:51 -0800231 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
Mathias Agopianfc328812010-07-14 23:41:37 -0700232 SortedVector< wp<SensorEventConnection> > mActiveConnections;
Aravind Akella9a844cf2014-02-11 18:58:52 -0800233 bool mWakeLockAcquired;
Aravind Akella8493b792014-09-08 15:45:47 -0700234 sensors_event_t *mSensorEventBuffer, *mSensorEventScratch;
235 SensorEventConnection const **mMapFlushEventsToConnections;
Aravind Akellaa9e6cc32015-04-16 18:57:31 -0700236 Mode mCurrentOperatingMode;
Peng Xueb4d6282015-12-10 18:02:41 -0800237
Aravind Akella5c538052015-06-29 12:37:48 -0700238 // This packagaName is set when SensorService is in RESTRICTED or DATA_INJECTION mode. Only
239 // applications with this packageName are allowed to activate/deactivate or call flush on
240 // sensors. To run CTS this is can be set to ".cts." and only CTS tests will get access to
241 // sensors.
242 String8 mWhiteListedPackage;
Aravind Akella8493b792014-09-08 15:45:47 -0700243
Mathias Agopian3560fb22010-07-22 21:24:39 -0700244 // The size of this vector is constant, only the items are mutable
Peng Xueb4d6282015-12-10 18:02:41 -0800245 KeyedVector<int32_t, MostRecentEventLogger *> mLastEventSeen;
Mathias Agopian3560fb22010-07-22 21:24:39 -0700246
Aravind Akella18d6d512015-06-18 14:18:28 -0700247 int mNextSensorRegIndex;
248 Vector<SensorRegistrationInfo> mLastNSensorRegistrations;
Mathias Agopianfc328812010-07-14 23:41:37 -0700249};
250
Peng Xueb4d6282015-12-10 18:02:41 -0800251} // namespace android
Mathias Agopianfc328812010-07-14 23:41:37 -0700252#endif // ANDROID_SENSOR_SERVICE_H