SensorService.h revision 724d91d778e71c8186399f4955de14b54812b3ed
1fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian/*
2fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Copyright (C) 2010 The Android Open Source Project
3fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
4fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
5fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * you may not use this file except in compliance with the License.
6fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * You may obtain a copy of the License at
7fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
8fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
9fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian *
10fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Unless required by applicable law or agreed to in writing, software
11fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
12fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * See the License for the specific language governing permissions and
14fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * limitations under the License.
15fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian */
16fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
17fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#ifndef ANDROID_SENSOR_SERVICE_H
18fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#define ANDROID_SENSOR_SERVICE_H
19fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
20fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <stdint.h>
21fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <sys/types.h>
22fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
23fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/Vector.h>
24fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/SortedVector.h>
25fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/KeyedVector.h>
26fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/threads.h>
27fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/RefBase.h>
28fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
29fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <binder/BinderService.h>
30fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
31fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/Sensor.h>
32b3989276d17f2b083bec67b695d1078fb86c6c53Mathias Agopian#include <gui/BitTube.h>
33fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/ISensorServer.h>
34fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/ISensorEventConnection.h>
35fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
36f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "SensorInterface.h"
37f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian
38fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
39fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
40a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian#define DEBUG_CONNECTIONS   false
41a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian
42fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstruct sensors_poll_device_t;
43fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstruct sensors_module_t;
44fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
45fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiannamespace android {
46fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
47fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
48fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianclass SensorService :
49fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        public BinderService<SensorService>,
50fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        public BnSensorServer,
51fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        protected Thread
52fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{
53b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    friend class BinderService<SensorService>;
54fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
55b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    static const char* WAKE_LOCK_NAME;
567c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
57b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    static char const* getServiceName() ANDROID_API { return "sensorservice"; }
58b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian    SensorService() ANDROID_API;
59fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual ~SensorService();
60fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
61fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual void onFirstRef();
62fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
63fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // Thread interface
64fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual bool threadLoop();
65fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
66fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // ISensorServer interface
67fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual Vector<Sensor> getSensorList();
68fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual sp<ISensorEventConnection> createSensorEventConnection();
69fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual status_t dump(int fd, const Vector<String16>& args);
70fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
71fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
72fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    class SensorEventConnection : public BnSensorEventConnection {
737c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        virtual ~SensorEventConnection();
747c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        virtual void onFirstRef();
75b3989276d17f2b083bec67b695d1078fb86c6c53Mathias Agopian        virtual sp<BitTube> getSensorChannel() const;
76724d91d778e71c8186399f4955de14b54812b3edAravind Akella        virtual status_t enableDisable(int handle, bool enabled, nsecs_t samplingPeriodNs,
77724d91d778e71c8186399f4955de14b54812b3edAravind Akella                                       nsecs_t maxBatchReportLatencyNs, int reservedFlags);
78724d91d778e71c8186399f4955de14b54812b3edAravind Akella        virtual status_t setEventRate(int handle, nsecs_t samplingPeriodNs);
79724d91d778e71c8186399f4955de14b54812b3edAravind Akella        virtual status_t flushSensor(int handle);
807c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
81fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        sp<SensorService> const mService;
82b3989276d17f2b083bec67b695d1078fb86c6c53Mathias Agopian        sp<BitTube> const mChannel;
835307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian        uid_t mUid;
8471d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian        mutable Mutex mConnectionLock;
857c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
86f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        // protected by SensorService::mLock
87f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        SortedVector<int> mSensorInfo;
887c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
89fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    public:
905307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian        SensorEventConnection(const sp<SensorService>& service, uid_t uid);
917c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
92cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian        status_t sendEvents(sensors_event_t const* buffer, size_t count,
933560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                sensors_event_t* scratch = NULL);
94fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        bool hasSensor(int32_t handle) const;
95fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        bool hasAnySensor() const;
967c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        bool addSensor(int32_t handle);
977c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        bool removeSensor(int32_t handle);
985307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian
995307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian        uid_t getUid() const { return mUid; }
100fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    };
101fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
102fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    class SensorRecord {
103fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        SortedVector< wp<SensorEventConnection> > mConnections;
104fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    public:
105fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        SensorRecord(const sp<SensorEventConnection>& connection);
1067c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        bool addConnection(const sp<SensorEventConnection>& connection);
107fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        bool removeConnection(const wp<SensorEventConnection>& connection);
108fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        size_t getNumConnections() const { return mConnections.size(); }
109fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    };
110fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
111fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
112f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
113fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
114f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    String8 getSensorName(int handle) const;
11594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian    void recordLastValue(sensors_event_t const * buffer, size_t count);
116f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    static void sortEventBuffer(sensors_event_t* buffer, size_t count);
1170319306670b0344da99efa606b6f172dde575a39Mathias Agopian    Sensor registerSensor(SensorInterface* sensor);
1180319306670b0344da99efa606b6f172dde575a39Mathias Agopian    Sensor registerVirtualSensor(SensorInterface* sensor);
119ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian    status_t cleanupWithoutDisable(
120ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian            const sp<SensorEventConnection>& connection, int handle);
121ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian    status_t cleanupWithoutDisableLocked(
122ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian            const sp<SensorEventConnection>& connection, int handle);
1234342fdf14ffb792a36c1de25ad14b745df628da2Jaikumar Ganesh    void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection,
124b6df7d0e4c2117ca476662bd52b6745b3d8a305fMathias Agopian            sensors_event_t const* buffer, const int count);
12594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian
126fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // constants
127fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    Vector<Sensor> mSensorList;
12833264868ba0aaccb9d4b5e5a067a79a43633f6ecMathias Agopian    Vector<Sensor> mUserSensorListDebug;
129010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian    Vector<Sensor> mUserSensorList;
130f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    DefaultKeyedVector<int, SensorInterface*> mSensorMap;
131f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    Vector<SensorInterface *> mVirtualSensorList;
13250df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian    status_t mInitCheck;
133fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
134fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // protected by mLock
135fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    mutable Mutex mLock;
136fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
137f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
138fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    SortedVector< wp<SensorEventConnection> > mActiveConnections;
139fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
1403560fb24b668675627934356f210d84d19bf4e56Mathias Agopian    // The size of this vector is constant, only the items are mutable
1413560fb24b668675627934356f210d84d19bf4e56Mathias Agopian    KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
1423560fb24b668675627934356f210d84d19bf4e56Mathias Agopian
143fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianpublic:
144db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian    void cleanupConnection(SensorEventConnection* connection);
145724d91d778e71c8186399f4955de14b54812b3edAravind Akella    status_t enable(const sp<SensorEventConnection>& connection, int handle,
146724d91d778e71c8186399f4955de14b54812b3edAravind Akella                    nsecs_t samplingPeriodNs,  nsecs_t maxBatchReportLatencyNs, int reservedFlags);
147fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    status_t disable(const sp<SensorEventConnection>& connection, int handle);
1487c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
149724d91d778e71c8186399f4955de14b54812b3edAravind Akella    status_t flushSensor(const sp<SensorEventConnection>& connection, int handle);
150fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian};
151fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
152fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
153fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; // namespace android
154fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
155fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#endif // ANDROID_SENSOR_SERVICE_H
156