SensorService.h revision 724d91d778e71c8186399f4955de14b54812b3ed
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
31#include <gui/Sensor.h>
32#include <gui/BitTube.h>
33#include <gui/ISensorServer.h>
34#include <gui/ISensorEventConnection.h>
35
36#include "SensorInterface.h"
37
38// ---------------------------------------------------------------------------
39
40#define DEBUG_CONNECTIONS   false
41
42struct 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{
53    friend class BinderService<SensorService>;
54
55    static const char* WAKE_LOCK_NAME;
56
57    static char const* getServiceName() ANDROID_API { return "sensorservice"; }
58    SensorService() ANDROID_API;
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<BitTube> getSensorChannel() const;
76        virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
77                                       nsecs_t maxBatchReportLatencyNs, int reservedFlags);
78        virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
79        virtual status_t flushSensor(int handle);
80
81        sp<SensorService> const mService;
82        sp<BitTube> const mChannel;
83        uid_t mUid;
84        mutable Mutex mConnectionLock;
85
86        // protected by SensorService::mLock
87        SortedVector<int> mSensorInfo;
88
89    public:
90        SensorEventConnection(const sp<SensorService>& service, uid_t uid);
91
92        status_t sendEvents(sensors_event_t const* buffer, size_t count,
93                sensors_event_t* scratch = NULL);
94        bool hasSensor(int32_t handle) const;
95        bool hasAnySensor() const;
96        bool addSensor(int32_t handle);
97        bool removeSensor(int32_t handle);
98
99        uid_t getUid() const { return mUid; }
100    };
101
102    class SensorRecord {
103        SortedVector< wp<SensorEventConnection> > mConnections;
104    public:
105        SensorRecord(const sp<SensorEventConnection>& connection);
106        bool addConnection(const sp<SensorEventConnection>& connection);
107        bool removeConnection(const wp<SensorEventConnection>& connection);
108        size_t getNumConnections() const { return mConnections.size(); }
109    };
110
111    SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
112    DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
113
114    String8 getSensorName(int handle) const;
115    void recordLastValue(sensors_event_t const * buffer, size_t count);
116    static void sortEventBuffer(sensors_event_t* buffer, size_t count);
117    Sensor registerSensor(SensorInterface* sensor);
118    Sensor registerVirtualSensor(SensorInterface* sensor);
119    status_t cleanupWithoutDisable(
120            const sp<SensorEventConnection>& connection, int handle);
121    status_t cleanupWithoutDisableLocked(
122            const sp<SensorEventConnection>& connection, int handle);
123    void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
124            sensors_event_t const* buffer, const int count);
125
126    // constants
127    Vector<Sensor> mSensorList;
128    Vector<Sensor> mUserSensorListDebug;
129    Vector<Sensor> mUserSensorList;
130    DefaultKeyedVector<int, SensorInterface*> mSensorMap;
131    Vector<SensorInterface *> mVirtualSensorList;
132    status_t mInitCheck;
133
134    // protected by mLock
135    mutable Mutex mLock;
136    DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
137    DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
138    SortedVector< wp<SensorEventConnection> > mActiveConnections;
139
140    // The size of this vector is constant, only the items are mutable
141    KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
142
143public:
144    void cleanupConnection(SensorEventConnection* connection);
145    status_t enable(const sp<SensorEventConnection>& connection, int handle,
146                    nsecs_t samplingPeriodNs,  nsecs_t maxBatchReportLatencyNs, int reservedFlags);
147    status_t disable(const sp<SensorEventConnection>& connection, int handle);
148    status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
149    status_t flushSensor(const sp<SensorEventConnection>& connection, int handle);
150};
151
152// ---------------------------------------------------------------------------
153}; // namespace android
154
155#endif // ANDROID_SENSOR_SERVICE_H
156