SensorService.h revision 5307d17fe33fc26eeeacd6339a9fbfe96cf56873
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 nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz 56 57 SensorService(); 58 virtual ~SensorService(); 59 60 virtual void onFirstRef(); 61 62 // Thread interface 63 virtual bool threadLoop(); 64 65 // ISensorServer interface 66 virtual Vector<Sensor> getSensorList(); 67 virtual sp<ISensorEventConnection> createSensorEventConnection(); 68 virtual status_t dump(int fd, const Vector<String16>& args); 69 70 71 class SensorEventConnection : public BnSensorEventConnection { 72 virtual ~SensorEventConnection(); 73 virtual void onFirstRef(); 74 virtual sp<BitTube> getSensorChannel() const; 75 virtual status_t enableDisable(int handle, bool enabled); 76 virtual status_t setEventRate(int handle, nsecs_t ns); 77 78 sp<SensorService> const mService; 79 sp<BitTube> const mChannel; 80 uid_t mUid; 81 mutable Mutex mConnectionLock; 82 83 // protected by SensorService::mLock 84 SortedVector<int> mSensorInfo; 85 86 public: 87 SensorEventConnection(const sp<SensorService>& service, uid_t uid); 88 89 status_t sendEvents(sensors_event_t const* buffer, size_t count, 90 sensors_event_t* scratch = NULL); 91 bool hasSensor(int32_t handle) const; 92 bool hasAnySensor() const; 93 bool addSensor(int32_t handle); 94 bool removeSensor(int32_t handle); 95 96 uid_t getUid() const { return mUid; } 97 }; 98 99 class SensorRecord { 100 SortedVector< wp<SensorEventConnection> > mConnections; 101 public: 102 SensorRecord(const sp<SensorEventConnection>& connection); 103 bool addConnection(const sp<SensorEventConnection>& connection); 104 bool removeConnection(const wp<SensorEventConnection>& connection); 105 size_t getNumConnections() const { return mConnections.size(); } 106 }; 107 108 SortedVector< wp<SensorEventConnection> > getActiveConnections() const; 109 DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const; 110 111 String8 getSensorName(int handle) const; 112 void recordLastValue(sensors_event_t const * buffer, size_t count); 113 static void sortEventBuffer(sensors_event_t* buffer, size_t count); 114 void registerSensor(SensorInterface* sensor); 115 void registerVirtualSensor(SensorInterface* sensor); 116 117 // constants 118 Vector<Sensor> mSensorList; 119 Vector<Sensor> mUserSensorListDebug; 120 Vector<Sensor> mUserSensorList; 121 DefaultKeyedVector<int, SensorInterface*> mSensorMap; 122 Vector<SensorInterface *> mVirtualSensorList; 123 status_t mInitCheck; 124 125 // protected by mLock 126 mutable Mutex mLock; 127 DefaultKeyedVector<int, SensorRecord*> mActiveSensors; 128 DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; 129 SortedVector< wp<SensorEventConnection> > mActiveConnections; 130 131 // The size of this vector is constant, only the items are mutable 132 KeyedVector<int32_t, sensors_event_t> mLastEventSeen; 133 134public: 135 static char const* getServiceName() { return "sensorservice"; } 136 137 void cleanupConnection(SensorEventConnection* connection); 138 status_t enable(const sp<SensorEventConnection>& connection, int handle); 139 status_t disable(const sp<SensorEventConnection>& connection, int handle); 140 status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns); 141}; 142 143// --------------------------------------------------------------------------- 144}; // namespace android 145 146#endif // ANDROID_SENSOR_SERVICE_H 147