blob: 69e5dbb1d77b2557949f4b50e281ceea65119ea1 [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
41
Mathias Agopianfc328812010-07-14 23:41:37 -070042struct sensors_poll_device_t;
43struct sensors_module_t;
44
45namespace android {
46// ---------------------------------------------------------------------------
47
48class SensorService :
49 public BinderService<SensorService>,
50 public BnSensorServer,
51 protected Thread
52{
Mathias Agopianb6df7d02013-05-09 14:53:35 -070053 friend class BinderService<SensorService>;
Mathias Agopianfc328812010-07-14 23:41:37 -070054
Mathias Agopianb6df7d02013-05-09 14:53:35 -070055 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
56 static const char* WAKE_LOCK_NAME;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070057
Mathias Agopianb6df7d02013-05-09 14:53:35 -070058 static char const* getServiceName() ANDROID_API { return "sensorservice"; }
59 SensorService() ANDROID_API;
Mathias Agopianfc328812010-07-14 23:41:37 -070060 virtual ~SensorService();
61
62 virtual void onFirstRef();
63
64 // Thread interface
65 virtual bool threadLoop();
66
67 // ISensorServer interface
68 virtual Vector<Sensor> getSensorList();
69 virtual sp<ISensorEventConnection> createSensorEventConnection();
70 virtual status_t dump(int fd, const Vector<String16>& args);
71
72
73 class SensorEventConnection : public BnSensorEventConnection {
Mathias Agopian7c1c5312010-07-21 15:59:50 -070074 virtual ~SensorEventConnection();
75 virtual void onFirstRef();
Mathias Agopianb3989272011-10-20 18:42:02 -070076 virtual sp<BitTube> getSensorChannel() const;
Mathias Agopianfc328812010-07-14 23:41:37 -070077 virtual status_t enableDisable(int handle, bool enabled);
78 virtual status_t setEventRate(int handle, nsecs_t ns);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070079
Mathias Agopianfc328812010-07-14 23:41:37 -070080 sp<SensorService> const mService;
Mathias Agopianb3989272011-10-20 18:42:02 -070081 sp<BitTube> const mChannel;
Mathias Agopian5307d172012-09-18 17:02:43 -070082 uid_t mUid;
Mathias Agopian71d7a5c2010-11-14 20:55:25 -080083 mutable Mutex mConnectionLock;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070084
Mathias Agopianf001c922010-11-11 17:58:51 -080085 // protected by SensorService::mLock
86 SortedVector<int> mSensorInfo;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070087
Mathias Agopianfc328812010-07-14 23:41:37 -070088 public:
Mathias Agopian5307d172012-09-18 17:02:43 -070089 SensorEventConnection(const sp<SensorService>& service, uid_t uid);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070090
Mathias Agopiancf510012010-07-22 16:18:10 -070091 status_t sendEvents(sensors_event_t const* buffer, size_t count,
Mathias Agopian3560fb22010-07-22 21:24:39 -070092 sensors_event_t* scratch = NULL);
Mathias Agopianfc328812010-07-14 23:41:37 -070093 bool hasSensor(int32_t handle) const;
94 bool hasAnySensor() const;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070095 bool addSensor(int32_t handle);
96 bool removeSensor(int32_t handle);
Mathias Agopian5307d172012-09-18 17:02:43 -070097
98 uid_t getUid() const { return mUid; }
Mathias Agopianfc328812010-07-14 23:41:37 -070099 };
100
101 class SensorRecord {
102 SortedVector< wp<SensorEventConnection> > mConnections;
103 public:
104 SensorRecord(const sp<SensorEventConnection>& connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700105 bool addConnection(const sp<SensorEventConnection>& connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700106 bool removeConnection(const wp<SensorEventConnection>& connection);
107 size_t getNumConnections() const { return mConnections.size(); }
108 };
109
110 SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
Mathias Agopianf001c922010-11-11 17:58:51 -0800111 DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
Mathias Agopianfc328812010-07-14 23:41:37 -0700112
Mathias Agopianf001c922010-11-11 17:58:51 -0800113 String8 getSensorName(int handle) const;
Mathias Agopian94e8f682010-11-10 17:50:28 -0800114 void recordLastValue(sensors_event_t const * buffer, size_t count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800115 static void sortEventBuffer(sensors_event_t* buffer, size_t count);
Mathias Agopian03193062013-05-10 19:32:39 -0700116 Sensor registerSensor(SensorInterface* sensor);
117 Sensor registerVirtualSensor(SensorInterface* sensor);
Mathias Agopianac9a96d2013-07-12 02:01:16 -0700118 status_t cleanupWithoutDisable(
119 const sp<SensorEventConnection>& connection, int handle);
120 status_t cleanupWithoutDisableLocked(
121 const sp<SensorEventConnection>& connection, int handle);
Jaikumar Ganesh4342fdf2013-04-08 16:43:12 -0700122 void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
Mathias Agopianb6df7d02013-05-09 14:53:35 -0700123 sensors_event_t const* buffer, const int count);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800124
Mathias Agopianfc328812010-07-14 23:41:37 -0700125 // constants
126 Vector<Sensor> mSensorList;
Mathias Agopian33264862012-06-28 19:46:54 -0700127 Vector<Sensor> mUserSensorListDebug;
Mathias Agopian010e4222011-06-08 20:06:50 -0700128 Vector<Sensor> mUserSensorList;
Mathias Agopianf001c922010-11-11 17:58:51 -0800129 DefaultKeyedVector<int, SensorInterface*> mSensorMap;
130 Vector<SensorInterface *> mVirtualSensorList;
Mathias Agopian50df2952010-07-19 19:09:10 -0700131 status_t mInitCheck;
Mathias Agopianfc328812010-07-14 23:41:37 -0700132
133 // protected by mLock
134 mutable Mutex mLock;
Mathias Agopianfc328812010-07-14 23:41:37 -0700135 DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
Mathias Agopianf001c922010-11-11 17:58:51 -0800136 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
Mathias Agopianfc328812010-07-14 23:41:37 -0700137 SortedVector< wp<SensorEventConnection> > mActiveConnections;
138
Mathias Agopian3560fb22010-07-22 21:24:39 -0700139 // The size of this vector is constant, only the items are mutable
140 KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
141
Mathias Agopianfc328812010-07-14 23:41:37 -0700142public:
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800143 void cleanupConnection(SensorEventConnection* connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700144 status_t enable(const sp<SensorEventConnection>& connection, int handle);
145 status_t disable(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700146 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700147};
148
149// ---------------------------------------------------------------------------
150}; // namespace android
151
152#endif // ANDROID_SENSOR_SERVICE_H