1fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian/*
2fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Copyright (C) 2010 The Android Open Source Project
3fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
4fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * you may not use this file except in compliance with the License.
6fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * You may obtain a copy of the License at
7fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
8fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
10fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * See the License for the specific language governing permissions and
14fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * limitations under the License.
15fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian */
16fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
17fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#ifndef ANDROID_SENSOR_SERVICE_H
18fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#define ANDROID_SENSOR_SERVICE_H
19fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
20755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include "SensorList.h"
216a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu#include "RecentEventLogger.h"
22fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
23fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <binder/BinderService.h>
24755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <cutils/compiler.h>
25801ea093b0e923a61b832f2adba698a273479880Mathias Agopian#include <sensor/ISensorServer.h>
26801ea093b0e923a61b832f2adba698a273479880Mathias Agopian#include <sensor/ISensorEventConnection.h>
27801ea093b0e923a61b832f2adba698a273479880Mathias Agopian#include <sensor/Sensor.h>
28fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
29755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <utils/AndroidThreads.h>
30755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <utils/KeyedVector.h>
31755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <utils/Looper.h>
32755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <utils/SortedVector.h>
33755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <utils/String8.h>
34755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <utils/Vector.h>
35755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <utils/threads.h>
36755c451c7861a029e26e5f16e319b629169e656dPeng Xu
37755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <stdint.h>
38755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <sys/types.h>
396a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu#include <unordered_map>
40755c451c7861a029e26e5f16e319b629169e656dPeng Xu#include <unordered_set>
41f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
4272952efe5ce8ee10e3c591cc6d2a3475b7b91690Bernhard Rosenkränzer#if __clang__
43eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu// Clang warns about SensorEventConnection::dump hiding BBinder::dump. The cause isn't fixable
44eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu// without changing the API, so let's tell clang this is indeed intentional.
4572952efe5ce8ee10e3c591cc6d2a3475b7b91690Bernhard Rosenkränzer#pragma clang diagnostic ignored "-Woverloaded-virtual"
4672952efe5ce8ee10e3c591cc6d2a3475b7b91690Bernhard Rosenkränzer#endif
4772952efe5ce8ee10e3c591cc6d2a3475b7b91690Bernhard Rosenkränzer
48fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
49f66684a6fb2a2991e84a085673629db2a0494fc6Peng Xu#define IGNORE_HARDWARE_FUSION  false
50a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian#define DEBUG_CONNECTIONS   false
5156ae42613c91f6a6fb0dc3f626daa24666fd18c2Aravind Akella// Max size is 100 KB which is enough to accept a batch of about 1000 events.
52a389c7a87c1ae31e177153f1eb0b0159b9effaeaChih-Hung Hsieh#define MAX_SOCKET_BUFFER_SIZE_BATCHED (100 * 1024)
5356ae42613c91f6a6fb0dc3f626daa24666fd18c2Aravind Akella// For older HALs which don't support batching, use a smaller socket buffer size.
54a389c7a87c1ae31e177153f1eb0b0159b9effaeaChih-Hung Hsieh#define SOCKET_BUFFER_SIZE_NON_BATCHED (4 * 1024)
55a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian
56363b3fde1499521aafdc78d9d28380cc643ec6b5Peng Xu#define SENSOR_REGISTRATIONS_BUF_SIZE 200
57444f2675728dde36378beb8e67a94f86ebf1ca46Aravind Akella
58fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiannamespace android {
59fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
60755c451c7861a029e26e5f16e319b629169e656dPeng Xuclass SensorInterface;
616a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xuusing namespace SensorServiceUtil;
62fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
63fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianclass SensorService :
64fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        public BinderService<SensorService>,
65fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        public BnSensorServer,
66fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        protected Thread
67fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
68eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // nested class/struct for internal use
69eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    class SensorEventConnection;
70e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu    class SensorDirectConnection;
71eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
72eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xupublic:
73eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void cleanupConnection(SensorEventConnection* connection);
74e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu    void cleanupConnection(SensorDirectConnection* c);
75eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
76eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    status_t enable(const sp<SensorEventConnection>& connection, int handle,
77eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu                    nsecs_t samplingPeriodNs,  nsecs_t maxBatchReportLatencyNs, int reservedFlags,
78eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu                    const String16& opPackageName);
79eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
80eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    status_t disable(const sp<SensorEventConnection>& connection, int handle);
81eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
82eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns,
83eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu                          const String16& opPackageName);
84eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
85eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    status_t flushSensor(const sp<SensorEventConnection>& connection,
86eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu                         const String16& opPackageName);
87eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
88eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xuprivate:
89b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    friend class BinderService<SensorService>;
90fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
91eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // nested class/struct for internal use
92eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    class SensorRecord;
93eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    class SensorEventAckReceiver;
9451224681c9a80ff714d40c26d8f465fb1bae17e2Peng Xu    class SensorRegistrationInfo;
95eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
964949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella    enum Mode {
974949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // The regular operating mode where any application can register/unregister/call flush on
984949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // sensors.
994949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       NORMAL = 0,
100eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // This mode is only used for testing purposes. Not all HALs support this mode. In this mode,
101eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // the HAL ignores the sensor data provided by physical sensors and accepts the data that is
102eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // injected from the SensorService as if it were the real sensor data. This mode is primarily
103eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // used for testing various algorithms like vendor provided SensorFusion, Step Counter and
104eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // Step Detector etc. Typically in this mode, there will be a client (a
105eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // SensorEventConnection) which will be injecting sensor data into the HAL. Normal apps can
106eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // unregister and register for any sensor that supports injection. Registering to sensors
107eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // that do not support injection will give an error.  TODO(aakella) : Allow exactly one
108eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // client to inject sensor data at a time.
109a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella       DATA_INJECTION = 1,
1104949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // This mode is used only for testing sensors. Each sensor can be tested in isolation with
1114949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // the required sampling_rate and maxReportLatency parameters without having to think about
1124949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // the data rates requested by other applications. End user devices are always expected to be
1134949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // in NORMAL mode. When this mode is first activated, all active sensors from all connections
1144949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // are disabled. Calling flush() will return an error. In this mode, only the requests from
1154949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // selected apps whose package names are whitelisted are allowed (typically CTS apps).  Only
116a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella       // these apps can register/unregister/call flush() on sensors. If SensorService switches to
1174949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // NORMAL mode again, all sensors that were previously registered to are activated with the
1184949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella       // corresponding paramaters if the application hasn't unregistered for sensors in the mean
119eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // time.  NOTE: Non whitelisted app whose sensors were previously deactivated may still
120eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu       // receive events if a whitelisted app requests data from the same sensor.
121a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella       RESTRICTED = 2
122a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella
123a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella      // State Transitions supported.
124a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella      //     RESTRICTED   <---  NORMAL   ---> DATA_INJECTION
125a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella      //                  --->           <---
1265c538053346107a8ec704d8769059ab6a23ebeefAravind Akella
1275c538053346107a8ec704d8769059ab6a23ebeefAravind Akella      // Shell commands to switch modes in SensorService.
1285c538053346107a8ec704d8769059ab6a23ebeefAravind Akella      // 1) Put SensorService in RESTRICTED mode with packageName .cts. If it is already in
1295c538053346107a8ec704d8769059ab6a23ebeefAravind Akella      // restricted mode it is treated as a NO_OP (and packageName is NOT changed).
130eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu      //
131eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu      //     $ adb shell dumpsys sensorservice restrict .cts.
1325c538053346107a8ec704d8769059ab6a23ebeefAravind Akella      //
1335c538053346107a8ec704d8769059ab6a23ebeefAravind Akella      // 2) Put SensorService in DATA_INJECTION mode with packageName .xts. If it is already in
1345c538053346107a8ec704d8769059ab6a23ebeefAravind Akella      // data_injection mode it is treated as a NO_OP (and packageName is NOT changed).
135eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu      //
136eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu      //     $ adb shell dumpsys sensorservice data_injection .xts.
1375c538053346107a8ec704d8769059ab6a23ebeefAravind Akella      //
1385c538053346107a8ec704d8769059ab6a23ebeefAravind Akella      // 3) Reset sensorservice back to NORMAL mode.
139eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu      //     $ adb shell dumpsys sensorservice enable
1404949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella    };
1414949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella
142b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    static const char* WAKE_LOCK_NAME;
143b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    static char const* getServiceName() ANDROID_API { return "sensorservice"; }
144b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    SensorService() ANDROID_API;
145fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual ~SensorService();
146fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
147fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual void onFirstRef();
148fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
149fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // Thread interface
150fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual bool threadLoop();
151fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
152fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // ISensorServer interface
153b412f6e203b38f8047f760261a5e3dc6d0722f08Svetoslav    virtual Vector<Sensor> getSensorList(const String16& opPackageName);
1542576cb63b3fe1592f54816625036566b9eb0793aPeng Xu    virtual Vector<Sensor> getDynamicSensorList(const String16& opPackageName);
155eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    virtual sp<ISensorEventConnection> createSensorEventConnection(
156eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu            const String8& packageName,
157eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu            int requestedMode, const String16& opPackageName);
1585c538053346107a8ec704d8769059ab6a23ebeefAravind Akella    virtual int isDataInjectionEnabled();
159e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu    virtual sp<ISensorEventConnection> createSensorDirectConnection(const String16& opPackageName,
160e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu            uint32_t size, int32_t type, int32_t format, const native_handle *resource);
161dd5c5cb3b8f3df9b458dff74e247b9a542befee7Peng Xu    virtual int setOperationParameter(
162dd5c5cb3b8f3df9b458dff74e247b9a542befee7Peng Xu            int32_t type, const Vector<float> &floats, const Vector<int32_t> &ints);
163fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual status_t dump(int fd, const Vector<String16>& args);
164fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
165f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    String8 getSensorName(int handle) const;
166b4099e77ec2bf8e9d4259ff30f0cb1d621deed91Aravind Akella    bool isVirtualSensor(int handle) const;
167755c451c7861a029e26e5f16e319b629169e656dPeng Xu    sp<SensorInterface> getSensorInterfaceFromHandle(int handle) const;
1689a844cf78f09953145200b4074d47589257a408cAravind Akella    bool isWakeUpSensor(int type) const;
1690ec2066e4774b851c66176b99b0a5aa5abe6ad00Aravind Akella    void recordLastValueLocked(sensors_event_t const* buffer, size_t count);
170f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    static void sortEventBuffer(sensors_event_t* buffer, size_t count);
1710cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    const Sensor& registerSensor(SensorInterface* sensor,
1720cc8f809924706c7d683da30605f432635dd5bb6Peng Xu                                 bool isDebug = false, bool isVirtual = false);
1730cc8f809924706c7d683da30605f432635dd5bb6Peng Xu    const Sensor& registerVirtualSensor(SensorInterface* sensor, bool isDebug = false);
1746a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu    const Sensor& registerDynamicSensorLocked(SensorInterface* sensor, bool isDebug = false);
1756a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu    bool unregisterDynamicSensorLocked(int handle);
176eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    status_t cleanupWithoutDisable(const sp<SensorEventConnection>& connection, int handle);
177eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    status_t cleanupWithoutDisableLocked(const sp<SensorEventConnection>& connection, int handle);
1789a844cf78f09953145200b4074d47589257a408cAravind Akella    void cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection,
179b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian            sensors_event_t const* buffer, const int count);
180b412f6e203b38f8047f760261a5e3dc6d0722f08Svetoslav    static bool canAccessSensor(const Sensor& sensor, const char* operation,
181b412f6e203b38f8047f760261a5e3dc6d0722f08Svetoslav            const String16& opPackageName);
1829a844cf78f09953145200b4074d47589257a408cAravind Akella    // SensorService acquires a partial wakelock for delivering events from wake up sensors. This
1839a844cf78f09953145200b4074d47589257a408cAravind Akella    // method checks whether all the events from these wake up sensors have been delivered to the
1849a844cf78f09953145200b4074d47589257a408cAravind Akella    // corresponding applications, if yes the wakelock is released.
1859a844cf78f09953145200b4074d47589257a408cAravind Akella    void checkWakeLockState();
1869a844cf78f09953145200b4074d47589257a408cAravind Akella    void checkWakeLockStateLocked();
187b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    bool isWakeLockAcquired();
1889a844cf78f09953145200b4074d47589257a408cAravind Akella    bool isWakeUpSensorEvent(const sensors_event_t& event) const;
18956ae42613c91f6a6fb0dc3f626daa24666fd18c2Aravind Akella
19056ae42613c91f6a6fb0dc3f626daa24666fd18c2Aravind Akella    sp<Looper> getLooper() const;
19156ae42613c91f6a6fb0dc3f626daa24666fd18c2Aravind Akella
192b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    // Reset mWakeLockRefCounts for all SensorEventConnections to zero. This may happen if
193b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    // SensorService did not receive any acknowledgements from apps which have registered for
194b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    // wake_up sensors.
195b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    void resetAllWakeLockRefCounts();
196b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella
197eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // Acquire or release wake_lock. If wake_lock is acquired, set the timeout in the looper to 5
198eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    // seconds and wake the looper.
199b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    void setWakeLockAcquiredLocked(bool acquire);
200b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella
201b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    // Send events from the event cache for this particular connection.
202b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    void sendEventsFromCache(const sp<SensorEventConnection>& connection);
203b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella
204b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    // Promote all weak referecences in mActiveConnections vector to strong references and add them
205b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella    // to the output vector.
206eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu    void populateActiveConnections( SortedVector< sp<SensorEventConnection> >* activeConnections);
207b4373ac9f53e3a6edee915715101d535e1a851a1Aravind Akella
2084949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella    // If SensorService is operating in RESTRICTED mode, only select whitelisted packages are
2094949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella    // allowed to register for or call flush on sensors. Typically only cts test packages are
2104949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella    // allowed.
2114949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella    bool isWhiteListedPackage(const String8& packageName);
212e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu    bool isOperationRestricted(const String16& opPackageName);
2134949c50372de4c7fdb57de1dc0c1f5bb3ac463ebAravind Akella
214a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella    // Reset the state of SensorService to NORMAL mode.
215a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella    status_t resetToNormalMode();
216a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella    status_t resetToNormalModeLocked();
217a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella
21853ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser    // Transforms the UUIDs for all the sensors into proper IDs.
21953ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser    void makeUuidsIntoIdsForSensorList(Vector<Sensor> &sensorList) const;
22053ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser    // Gets the appropriate ID from the given UUID.
22153ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser    int32_t getIdFromUuid(const Sensor::uuid_t &uuid) const;
22253ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser    // Either read from storage or create a new one.
22353ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser    static bool initializeHmacKey();
22453ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser
22598d30f627ef96ca6e47c9cbcbdcfb63adbeda424Peng Xu    // Enable SCHED_FIFO priority for thread
22698d30f627ef96ca6e47c9cbcbdcfb63adbeda424Peng Xu    void enableSchedFifoMode();
22753ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser
22853ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser    static uint8_t sHmacGlobalKey[128];
22953ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser    static bool sHmacGlobalKeyIsValid;
23053ca2e00c6f2a71b93c8e3c8e85aeeb03c5bd06dGreg Kaiser
2316a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu    SensorList mSensors;
23250df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian    status_t mInitCheck;
233eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
2345466c3d20d03d4ae4b0fd0e0a93175091e3b0bb2Aravind Akella    // Socket buffersize used to initialize BitTube. This size depends on whether batching is
2355466c3d20d03d4ae4b0fd0e0a93175091e3b0bb2Aravind Akella    // supported or not.
236755c451c7861a029e26e5f16e319b629169e656dPeng Xu    uint32_t mSocketBufferSize;
237755c451c7861a029e26e5f16e319b629169e656dPeng Xu    sp<Looper> mLooper;
238755c451c7861a029e26e5f16e319b629169e656dPeng Xu    sp<SensorEventAckReceiver> mAckReceiver;
239fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
240fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // protected by mLock
241fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    mutable Mutex mLock;
242fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
243755c451c7861a029e26e5f16e319b629169e656dPeng Xu    std::unordered_set<int> mActiveVirtualSensors;
244fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    SortedVector< wp<SensorEventConnection> > mActiveConnections;
2459a844cf78f09953145200b4074d47589257a408cAravind Akella    bool mWakeLockAcquired;
2468493b79e1cff92450076ca7450c4bf4e434a6816Aravind Akella    sensors_event_t *mSensorEventBuffer, *mSensorEventScratch;
247ded526e8c3539b5986b46dffc1ff8ab15d7da570Peng Xu    wp<const SensorEventConnection> * mMapFlushEventsToConnections;
2486a2d3a06caa337857cf60cfc70a9a78909ad3608Peng Xu    std::unordered_map<int, RecentEventLogger*> mRecentEvent;
249e36e34731cbe77a49aa5e7d687dde041d83d0370Peng Xu    SortedVector< wp<SensorDirectConnection> > mDirectConnections;
250a9e6cc387072e25706a6d847a97b02a6f25a918bAravind Akella    Mode mCurrentOperatingMode;
251eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu
2525c538053346107a8ec704d8769059ab6a23ebeefAravind Akella    // This packagaName is set when SensorService is in RESTRICTED or DATA_INJECTION mode. Only
2535c538053346107a8ec704d8769059ab6a23ebeefAravind Akella    // applications with this packageName are allowed to activate/deactivate or call flush on
2545c538053346107a8ec704d8769059ab6a23ebeefAravind Akella    // sensors. To run CTS this is can be set to ".cts." and only CTS tests will get access to
2555c538053346107a8ec704d8769059ab6a23ebeefAravind Akella    // sensors.
2565c538053346107a8ec704d8769059ab6a23ebeefAravind Akella    String8 mWhiteListedPackage;
2578493b79e1cff92450076ca7450c4bf4e434a6816Aravind Akella
25818d6d51a00897988e3347b130f533e9ffdd8c365Aravind Akella    int mNextSensorRegIndex;
25918d6d51a00897988e3347b130f533e9ffdd8c365Aravind Akella    Vector<SensorRegistrationInfo> mLastNSensorRegistrations;
260fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian};
261fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
262eb4d628b69831d533f14c09fd63400f75e69ba76Peng Xu} // namespace android
263fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#endif // ANDROID_SENSOR_SERVICE_H
264