SensorService.h revision fc32881fcc68640d008c7515cdd1bcd866f72cd5
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// ---------------------------------------------------------------------------
38
39struct sensors_poll_device_t;
40struct sensors_module_t;
41
42namespace android {
43// ---------------------------------------------------------------------------
44
45class SensorService :
46        public BinderService<SensorService>,
47        public BnSensorServer,
48        protected Thread
49{
50   friend class BinderService<SensorService>;
51
52            SensorService();
53    virtual ~SensorService();
54
55    virtual void onFirstRef();
56
57    // Thread interface
58    virtual bool threadLoop();
59
60    // ISensorServer interface
61    virtual Vector<Sensor> getSensorList();
62    virtual sp<ISensorEventConnection> createSensorEventConnection();
63    virtual status_t dump(int fd, const Vector<String16>& args);
64
65
66    class SensorEventConnection : public BnSensorEventConnection {
67        virtual sp<SensorChannel> getSensorChannel() const;
68        virtual status_t enableDisable(int handle, bool enabled);
69        virtual status_t setEventRate(int handle, nsecs_t ns);
70        sp<SensorService> const mService;
71        sp<SensorChannel> const mChannel;
72        SortedVector<int32_t> mSensorList;
73    public:
74        SensorEventConnection(const sp<SensorService>& service);
75        virtual ~SensorEventConnection();
76        virtual void onFirstRef();
77        status_t sendEvents(sensors_event_t const* buffer, size_t count);
78        bool hasSensor(int32_t handle) const;
79        bool hasAnySensor() const;
80        void addSensor(int32_t handle);
81        void removeSensor(int32_t handle);
82    };
83
84    class SensorRecord {
85        SortedVector< wp<SensorEventConnection> > mConnections;
86    public:
87        SensorRecord(const sp<SensorEventConnection>& connection);
88        status_t addConnection(const sp<SensorEventConnection>& connection);
89        bool removeConnection(const wp<SensorEventConnection>& connection);
90        size_t getNumConnections() const { return mConnections.size(); }
91    };
92
93    SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
94
95    // constants
96    Vector<Sensor> mSensorList;
97    struct sensors_poll_device_t* mSensorDevice;
98    struct sensors_module_t* mSensorModule;
99    Permission mDump;
100
101    // protected by mLock
102    mutable Mutex mLock;
103    SortedVector< wp<SensorEventConnection> > mConnections;
104    DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
105    SortedVector< wp<SensorEventConnection> > mActiveConnections;
106
107public:
108    static char const* getServiceName() { return "sensorservice"; }
109
110    void cleanupConnection(const wp<SensorEventConnection>& connection);
111    status_t enable(const sp<SensorEventConnection>& connection, int handle);
112    status_t disable(const sp<SensorEventConnection>& connection, int handle);
113    status_t setRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
114};
115
116// ---------------------------------------------------------------------------
117}; // namespace android
118
119#endif // ANDROID_SENSOR_SERVICE_H
120