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{
53fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian   friend class BinderService<SensorService>;
54fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
5524d72350f321c17b0bfe1ef3fd52d4070a1c02c3Mathias Agopian   static const nsecs_t MINIMUM_EVENTS_PERIOD =   1000000; // 1000 Hz
567c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
57fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian            SensorService();
58fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual ~SensorService();
59fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
60fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual void onFirstRef();
61fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
62fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // Thread interface
63fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual bool threadLoop();
64fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
65fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // ISensorServer interface
66fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual Vector<Sensor> getSensorList();
67fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual sp<ISensorEventConnection> createSensorEventConnection();
68fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    virtual status_t dump(int fd, const Vector<String16>& args);
69fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
70fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
71fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    class SensorEventConnection : public BnSensorEventConnection {
727c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        virtual ~SensorEventConnection();
737c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        virtual void onFirstRef();
74b3989276d17f2b083bec67b695d1078fb86c6c53Mathias Agopian        virtual sp<BitTube> getSensorChannel() const;
75fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        virtual status_t enableDisable(int handle, bool enabled);
76fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        virtual status_t setEventRate(int handle, nsecs_t ns);
777c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
78fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        sp<SensorService> const mService;
79b3989276d17f2b083bec67b695d1078fb86c6c53Mathias Agopian        sp<BitTube> const mChannel;
805307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian        uid_t mUid;
8171d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian        mutable Mutex mConnectionLock;
827c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
83f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        // protected by SensorService::mLock
84f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian        SortedVector<int> mSensorInfo;
857c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
86fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    public:
875307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian        SensorEventConnection(const sp<SensorService>& service, uid_t uid);
887c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian
89cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian        status_t sendEvents(sensors_event_t const* buffer, size_t count,
903560fb24b668675627934356f210d84d19bf4e56Mathias Agopian                sensors_event_t* scratch = NULL);
91fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        bool hasSensor(int32_t handle) const;
92fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        bool hasAnySensor() const;
937c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        bool addSensor(int32_t handle);
947c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        bool removeSensor(int32_t handle);
955307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian
965307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian        uid_t getUid() const { return mUid; }
97fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    };
98fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
99fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    class SensorRecord {
100fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        SortedVector< wp<SensorEventConnection> > mConnections;
101fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    public:
102fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        SensorRecord(const sp<SensorEventConnection>& connection);
1037c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian        bool addConnection(const sp<SensorEventConnection>& connection);
104fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        bool removeConnection(const wp<SensorEventConnection>& connection);
105fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian        size_t getNumConnections() const { return mConnections.size(); }
106fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    };
107fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
108fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    SortedVector< wp<SensorEventConnection> > getActiveConnections() const;
109f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const;
110fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
111f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    String8 getSensorName(int handle) const;
11294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian    void recordLastValue(sensors_event_t const * buffer, size_t count);
113f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    static void sortEventBuffer(sensors_event_t* buffer, size_t count);
114f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    void registerSensor(SensorInterface* sensor);
115f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    void registerVirtualSensor(SensorInterface* sensor);
11694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian
117fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // constants
118fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    Vector<Sensor> mSensorList;
11933264868ba0aaccb9d4b5e5a067a79a43633f6ecMathias Agopian    Vector<Sensor> mUserSensorListDebug;
120010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian    Vector<Sensor> mUserSensorList;
121f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    DefaultKeyedVector<int, SensorInterface*> mSensorMap;
122f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    Vector<SensorInterface *> mVirtualSensorList;
12350df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian    status_t mInitCheck;
124fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
125fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    // protected by mLock
126fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    mutable Mutex mLock;
127fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    DefaultKeyedVector<int, SensorRecord*> mActiveSensors;
128f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian    DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors;
129fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    SortedVector< wp<SensorEventConnection> > mActiveConnections;
130fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
1313560fb24b668675627934356f210d84d19bf4e56Mathias Agopian    // The size of this vector is constant, only the items are mutable
1323560fb24b668675627934356f210d84d19bf4e56Mathias Agopian    KeyedVector<int32_t, sensors_event_t> mLastEventSeen;
1333560fb24b668675627934356f210d84d19bf4e56Mathias Agopian
134fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianpublic:
135fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    static char const* getServiceName() { return "sensorservice"; }
136fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
137db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian    void cleanupConnection(SensorEventConnection* connection);
138fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    status_t enable(const sp<SensorEventConnection>& connection, int handle);
139fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian    status_t disable(const sp<SensorEventConnection>& connection, int handle);
1407c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian    status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns);
141fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian};
142fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
143fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// ---------------------------------------------------------------------------
144fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; // namespace android
145fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian
146fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#endif // ANDROID_SENSOR_SERVICE_H
147