SensorService.h revision ac9a96da65f6eae4513654adaad8a457d1c1575c
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 564342fdf14ffb792a36c1de25ad14b745df628da2Jaikumar Ganesh static const char* WAKE_LOCK_NAME; 577c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 58fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SensorService(); 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; 76fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian virtual status_t enableDisable(int handle, bool enabled); 77fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian virtual status_t setEventRate(int handle, nsecs_t ns); 787c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 79fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian sp<SensorService> const mService; 80b3989276d17f2b083bec67b695d1078fb86c6c53Mathias Agopian sp<BitTube> const mChannel; 815307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian uid_t mUid; 8271d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian mutable Mutex mConnectionLock; 837c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 84f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // protected by SensorService::mLock 85f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SortedVector<int> mSensorInfo; 867c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 87fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian public: 885307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian SensorEventConnection(const sp<SensorService>& service, uid_t uid); 897c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 90cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian status_t sendEvents(sensors_event_t const* buffer, size_t count, 913560fb24b668675627934356f210d84d19bf4e56Mathias Agopian sensors_event_t* scratch = NULL); 92fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian bool hasSensor(int32_t handle) const; 93fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian bool hasAnySensor() const; 947c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian bool addSensor(int32_t handle); 957c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian bool removeSensor(int32_t handle); 965307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian 975307d17fe33fc26eeeacd6339a9fbfe96cf56873Mathias Agopian uid_t getUid() const { return mUid; } 98fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian }; 99fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 100fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian class SensorRecord { 101fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SortedVector< wp<SensorEventConnection> > mConnections; 102fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian public: 103fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SensorRecord(const sp<SensorEventConnection>& connection); 1047c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian bool addConnection(const sp<SensorEventConnection>& connection); 105fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian bool removeConnection(const wp<SensorEventConnection>& connection); 106fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian size_t getNumConnections() const { return mConnections.size(); } 107fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian }; 108fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 109fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SortedVector< wp<SensorEventConnection> > getActiveConnections() const; 110f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const; 111fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 112f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian String8 getSensorName(int handle) const; 1134342fdf14ffb792a36c1de25ad14b745df628da2Jaikumar Ganesh int getSensorType(int handle) const; 11494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian void recordLastValue(sensors_event_t const * buffer, size_t count); 115f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian static void sortEventBuffer(sensors_event_t* buffer, size_t count); 116f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian void registerSensor(SensorInterface* sensor); 117f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian void registerVirtualSensor(SensorInterface* sensor); 118ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian status_t cleanupWithoutDisable( 119ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian const sp<SensorEventConnection>& connection, int handle); 120ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian status_t cleanupWithoutDisableLocked( 121ac9a96da65f6eae4513654adaad8a457d1c1575cMathias Agopian const sp<SensorEventConnection>& connection, int handle); 1224342fdf14ffb792a36c1de25ad14b745df628da2Jaikumar Ganesh void cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection, 1234342fdf14ffb792a36c1de25ad14b745df628da2Jaikumar Ganesh sensors_event_t const* buffer, const int count); 12494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 125fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // constants 126fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Vector<Sensor> mSensorList; 12733264868ba0aaccb9d4b5e5a067a79a43633f6ecMathias Agopian Vector<Sensor> mUserSensorListDebug; 128010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian Vector<Sensor> mUserSensorList; 129f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian DefaultKeyedVector<int, SensorInterface*> mSensorMap; 130f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Vector<SensorInterface *> mVirtualSensorList; 13150df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian status_t mInitCheck; 132fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 133fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // protected by mLock 134fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mutable Mutex mLock; 135fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian DefaultKeyedVector<int, SensorRecord*> mActiveSensors; 136f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; 137fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SortedVector< wp<SensorEventConnection> > mActiveConnections; 138fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 1393560fb24b668675627934356f210d84d19bf4e56Mathias Agopian // The size of this vector is constant, only the items are mutable 1403560fb24b668675627934356f210d84d19bf4e56Mathias Agopian KeyedVector<int32_t, sensors_event_t> mLastEventSeen; 1413560fb24b668675627934356f210d84d19bf4e56Mathias Agopian 142fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianpublic: 143fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian static char const* getServiceName() { return "sensorservice"; } 144fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 145db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian void cleanupConnection(SensorEventConnection* connection); 146fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t enable(const sp<SensorEventConnection>& connection, int handle); 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); 149fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; 150fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 151fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 152fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; // namespace android 153fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 154fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#endif // ANDROID_SENSOR_SERVICE_H 155