SensorService.h revision a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0
1/*
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
37#include "SensorInterface.h"
38
39// ---------------------------------------------------------------------------
40
41#define DEBUG_CONNECTIONS   false
42
43struct sensors_poll_device_t;
44struct sensors_module_t;
45
46namespace android {
47// ---------------------------------------------------------------------------
48
49class SensorService :
50        public BinderService<SensorService>,
51        public BnSensorServer,
52        protected Thread
53{
54   friend class BinderService<SensorService>;
55
56   static const nsecs_t MINIMUM_EVENTS_PERIOD =   1000000; // 1000 Hz
57
58            SensorService();
59    virtual ~SensorService();
60
61    virtual void onFirstRef();
62
63    // Thread interface
64    virtual bool threadLoop();
65
66    // ISensorServer interface
67    virtual Vector<Sensor> getSensorList();
68    virtual sp<ISensorEventConnection> createSensorEventConnection();
69    virtual status_t dump(int fd, const Vector<String16>& args);
70
71
72    class SensorEventConnection : public BnSensorEventConnection {
73        virtual ~SensorEventConnection();
74        virtual void onFirstRef();
75        virtual sp<SensorChannel> getSensorChannel() const;
76        virtual status_t enableDisable(int handle, bool enabled);
77        virtual status_t setEventRate(int handle, nsecs_t ns);
78
79        sp<SensorService> const mService;
80        sp<SensorChannel> const mChannel;
81        mutable Mutex mConnectionLock;
82
83        // protected by SensorService::mLock
84        SortedVector<int> mSensorInfo;
85
86    public:
87        SensorEventConnection(const sp<SensorService>& service);
88
89        status_t sendEvents(sensors_event_t const* buffer, size_t count,
90                sensors_event_t* scratch = NULL);
91        bool hasSensor(int32_t handle) const;
92        bool hasAnySensor() const;
93        bool addSensor(int32_t handle);
94        bool removeSensor(int32_t handle);
95    };
96
97    class SensorRecord {
98        SortedVector< wp<SensorEventConnection> > mConnections;
99    public:
100        SensorRecord(const sp<SensorEventConnection>& connection);
101        bool addConnection(const sp<SensorEventConnection>& connection);
102        bool removeConnection(const wp<SensorEventConnection>& connection);
103        size_t getNumConnections() const { return mConnections.size(); }
104    };
105
106    SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
107    DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
108
109    String8 getSensorName(int handle) const;
110    void recordLastValue(sensors_event_t const * buffer, size_t count);
111    static void sortEventBuffer(sensors_event_t* buffer, size_t count);
112    void registerSensor(SensorInterface* sensor);
113    void registerVirtualSensor(SensorInterface* sensor);
114
115    // constants
116    Vector<Sensor> mSensorList;
117    DefaultKeyedVector<int, SensorInterface*> mSensorMap;
118    Vector<SensorInterface *> mVirtualSensorList;
119    Permission mDump;
120    status_t mInitCheck;
121
122    // protected by mLock
123    mutable Mutex mLock;
124    DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
125    DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
126    SortedVector< wp<SensorEventConnection> > mActiveConnections;
127
128    // The size of this vector is constant, only the items are mutable
129    KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
130
131public:
132    static char const* getServiceName() { return "sensorservice"; }
133
134    void cleanupConnection(SensorEventConnection* connection);
135    status_t enable(const sp<SensorEventConnection>& connection, int handle);
136    status_t disable(const sp<SensorEventConnection>& connection, int handle);
137    status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
138};
139
140// ---------------------------------------------------------------------------
141}; // namespace android
142
143#endif // ANDROID_SENSOR_SERVICE_H
144