blob: 21f12bdbcf423f47271fc7447ac0af8961854099 [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>
30#include <binder/Permission.h>
31
32#include <gui/Sensor.h>
33#include <gui/SensorChannel.h>
34#include <gui/ISensorServer.h>
35#include <gui/ISensorEventConnection.h>
36
Mathias Agopianf001c922010-11-11 17:58:51 -080037#include "SensorInterface.h"
38
Mathias Agopianfc328812010-07-14 23:41:37 -070039// ---------------------------------------------------------------------------
40
41struct sensors_poll_device_t;
42struct sensors_module_t;
43
44namespace android {
45// ---------------------------------------------------------------------------
46
47class SensorService :
48 public BinderService<SensorService>,
49 public BnSensorServer,
50 protected Thread
51{
52 friend class BinderService<SensorService>;
53
Mathias Agopian24d72352010-11-05 19:12:58 -070054 static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz
Mathias Agopian7c1c5312010-07-21 15:59:50 -070055
Mathias Agopianfc328812010-07-14 23:41:37 -070056 SensorService();
57 virtual ~SensorService();
58
59 virtual void onFirstRef();
60
61 // Thread interface
62 virtual bool threadLoop();
63
64 // ISensorServer interface
65 virtual Vector<Sensor> getSensorList();
66 virtual sp<ISensorEventConnection> createSensorEventConnection();
67 virtual status_t dump(int fd, const Vector<String16>& args);
68
69
70 class SensorEventConnection : public BnSensorEventConnection {
Mathias Agopian7c1c5312010-07-21 15:59:50 -070071 virtual ~SensorEventConnection();
72 virtual void onFirstRef();
Mathias Agopianfc328812010-07-14 23:41:37 -070073 virtual sp<SensorChannel> getSensorChannel() const;
74 virtual status_t enableDisable(int handle, bool enabled);
75 virtual status_t setEventRate(int handle, nsecs_t ns);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070076
Mathias Agopianfc328812010-07-14 23:41:37 -070077 sp<SensorService> const mService;
78 sp<SensorChannel> const mChannel;
Mathias Agopian71d7a5c2010-11-14 20:55:25 -080079 mutable Mutex mConnectionLock;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070080
Mathias Agopianf001c922010-11-11 17:58:51 -080081 // protected by SensorService::mLock
82 SortedVector<int> mSensorInfo;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070083
Mathias Agopianfc328812010-07-14 23:41:37 -070084 public:
85 SensorEventConnection(const sp<SensorService>& service);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070086
Mathias Agopiancf510012010-07-22 16:18:10 -070087 status_t sendEvents(sensors_event_t const* buffer, size_t count,
Mathias Agopian3560fb22010-07-22 21:24:39 -070088 sensors_event_t* scratch = NULL);
Mathias Agopianfc328812010-07-14 23:41:37 -070089 bool hasSensor(int32_t handle) const;
90 bool hasAnySensor() const;
Mathias Agopian7c1c5312010-07-21 15:59:50 -070091 bool addSensor(int32_t handle);
92 bool removeSensor(int32_t handle);
Mathias Agopianfc328812010-07-14 23:41:37 -070093 };
94
95 class SensorRecord {
96 SortedVector< wp<SensorEventConnection> > mConnections;
97 public:
98 SensorRecord(const sp<SensorEventConnection>& connection);
Mathias Agopian7c1c5312010-07-21 15:59:50 -070099 bool addConnection(const sp<SensorEventConnection>& connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700100 bool removeConnection(const wp<SensorEventConnection>& connection);
101 size_t getNumConnections() const { return mConnections.size(); }
102 };
103
104 SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
Mathias Agopianf001c922010-11-11 17:58:51 -0800105 DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
Mathias Agopianfc328812010-07-14 23:41:37 -0700106
Mathias Agopianf001c922010-11-11 17:58:51 -0800107 String8 getSensorName(int handle) const;
Mathias Agopian94e8f682010-11-10 17:50:28 -0800108 void recordLastValue(sensors_event_t const * buffer, size_t count);
Mathias Agopianf001c922010-11-11 17:58:51 -0800109 static void sortEventBuffer(sensors_event_t* buffer, size_t count);
110 void registerSensor(SensorInterface* sensor);
111 void registerVirtualSensor(SensorInterface* sensor);
Mathias Agopian94e8f682010-11-10 17:50:28 -0800112
Mathias Agopianfc328812010-07-14 23:41:37 -0700113 // constants
114 Vector<Sensor> mSensorList;
Mathias Agopianf001c922010-11-11 17:58:51 -0800115 DefaultKeyedVector<int, SensorInterface*> mSensorMap;
116 Vector<SensorInterface *> mVirtualSensorList;
Mathias Agopianfc328812010-07-14 23:41:37 -0700117 Permission mDump;
Mathias Agopian50df2952010-07-19 19:09:10 -0700118 status_t mInitCheck;
Mathias Agopianfc328812010-07-14 23:41:37 -0700119
120 // protected by mLock
121 mutable Mutex mLock;
Mathias Agopianfc328812010-07-14 23:41:37 -0700122 DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
Mathias Agopianf001c922010-11-11 17:58:51 -0800123 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
Mathias Agopianfc328812010-07-14 23:41:37 -0700124 SortedVector< wp<SensorEventConnection> > mActiveConnections;
125
Mathias Agopian3560fb22010-07-22 21:24:39 -0700126 // The size of this vector is constant, only the items are mutable
127 KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
128
Mathias Agopianfc328812010-07-14 23:41:37 -0700129public:
130 static char const* getServiceName() { return "sensorservice"; }
131
Mathias Agopiandb5b4bc2011-02-03 14:52:47 -0800132 void cleanupConnection(SensorEventConnection* connection);
Mathias Agopianfc328812010-07-14 23:41:37 -0700133 status_t enable(const sp<SensorEventConnection>& connection, int handle);
134 status_t disable(const sp<SensorEventConnection>& connection, int handle);
Mathias Agopian7c1c5312010-07-21 15:59:50 -0700135 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
Mathias Agopianfc328812010-07-14 23:41:37 -0700136};
137
138// ---------------------------------------------------------------------------
139}; // namespace android
140
141#endif // ANDROID_SENSOR_SERVICE_H