11bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian/* 21bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * Copyright (C) 2010 The Android Open Source Project 31bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * 41bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 51bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * you may not use this file except in compliance with the License. 61bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * You may obtain a copy of the License at 71bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * 81bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 91bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * 101bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * Unless required by applicable law or agreed to in writing, software 111bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 121bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * See the License for the specific language governing permissions and 141bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian * limitations under the License. 151bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian */ 161bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 171bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#ifndef ANDROID_SENSOR_SERVICE_H 181bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#define ANDROID_SENSOR_SERVICE_H 191bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 201bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <stdint.h> 211bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <sys/types.h> 221bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 231bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <utils/Vector.h> 241bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <utils/SortedVector.h> 251bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <utils/KeyedVector.h> 261bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <utils/threads.h> 271bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <utils/RefBase.h> 281bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 291bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <binder/BinderService.h> 301bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 311bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <gui/Sensor.h> 3212cdf5116c312b8fb4f96bd8ea64134ce06e899dMathias Agopian#include <gui/BitTube.h> 331bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <gui/ISensorServer.h> 341bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <gui/ISensorEventConnection.h> 351bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 36671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian#include "SensorInterface.h" 37671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian 381bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian// --------------------------------------------------------------------------- 391bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 409ff73de08f137ba03e18b8fd9fe0b070029c00f5Mathias Agopian#define DEBUG_CONNECTIONS false 419ff73de08f137ba03e18b8fd9fe0b070029c00f5Mathias Agopian 421bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianstruct sensors_poll_device_t; 431bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianstruct sensors_module_t; 441bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 451bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopiannamespace android { 461bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian// --------------------------------------------------------------------------- 471bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 481bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianclass SensorService : 491bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian public BinderService<SensorService>, 501bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian public BnSensorServer, 511bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian protected Thread 521bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian{ 531bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian friend class BinderService<SensorService>; 541bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 5594c4f5c1b28344001bae5ac4ad22dd01dc2fd679Mathias Agopian static const nsecs_t MINIMUM_EVENTS_PERIOD = 1000000; // 1000 Hz 56d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian 571bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian SensorService(); 581bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian virtual ~SensorService(); 591bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 601bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian virtual void onFirstRef(); 611bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 621bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian // Thread interface 631bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian virtual bool threadLoop(); 641bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 651bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian // ISensorServer interface 661bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian virtual Vector<Sensor> getSensorList(); 671bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian virtual sp<ISensorEventConnection> createSensorEventConnection(); 681bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian virtual status_t dump(int fd, const Vector<String16>& args); 691bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 701bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 711bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian class SensorEventConnection : public BnSensorEventConnection { 72d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian virtual ~SensorEventConnection(); 73d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian virtual void onFirstRef(); 7412cdf5116c312b8fb4f96bd8ea64134ce06e899dMathias Agopian virtual sp<BitTube> getSensorChannel() const; 751bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian virtual status_t enableDisable(int handle, bool enabled); 761bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian virtual status_t setEventRate(int handle, nsecs_t ns); 77d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian 781bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian sp<SensorService> const mService; 7912cdf5116c312b8fb4f96bd8ea64134ce06e899dMathias Agopian sp<BitTube> const mChannel; 80f33a6e9645517b9bcf7236bc6f2b7fe182a0137dMathias Agopian mutable Mutex mConnectionLock; 81d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian 82671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian // protected by SensorService::mLock 83671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian SortedVector<int> mSensorInfo; 84d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian 851bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian public: 861bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian SensorEventConnection(const sp<SensorService>& service); 87d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian 88efba8bf53aa6d0efb8cf778aaa63a482b813d2b1Mathias Agopian status_t sendEvents(sensors_event_t const* buffer, size_t count, 89aea786f42cee6a899e8f78fdbf82cb1cac544430Mathias Agopian sensors_event_t* scratch = NULL); 901bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian bool hasSensor(int32_t handle) const; 911bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian bool hasAnySensor() const; 92d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian bool addSensor(int32_t handle); 93d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian bool removeSensor(int32_t handle); 941bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian }; 951bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 961bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian class SensorRecord { 971bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian SortedVector< wp<SensorEventConnection> > mConnections; 981bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian public: 991bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian SensorRecord(const sp<SensorEventConnection>& connection); 100d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian bool addConnection(const sp<SensorEventConnection>& connection); 1011bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian bool removeConnection(const wp<SensorEventConnection>& connection); 1021bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian size_t getNumConnections() const { return mConnections.size(); } 1031bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian }; 1041bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 1051bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian SortedVector< wp<SensorEventConnection> > getActiveConnections() const; 106671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian DefaultKeyedVector<int, SensorInterface*> getActiveVirtualSensors() const; 1071bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 108671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian String8 getSensorName(int handle) const; 1093a9223ebf881b10996dac34b00e51e08a602d44bMathias Agopian void recordLastValue(sensors_event_t const * buffer, size_t count); 110671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian static void sortEventBuffer(sensors_event_t* buffer, size_t count); 111671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian void registerSensor(SensorInterface* sensor); 112671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian void registerVirtualSensor(SensorInterface* sensor); 1133a9223ebf881b10996dac34b00e51e08a602d44bMathias Agopian 1141bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian // constants 1151bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian Vector<Sensor> mSensorList; 116f8a6737f3d812b6b151990ad1695b55bfb0d8ddaMathias Agopian Vector<Sensor> mUserSensorList; 117671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian DefaultKeyedVector<int, SensorInterface*> mSensorMap; 118671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian Vector<SensorInterface *> mVirtualSensorList; 1195df13ef30e7dd58e4e718e6d949ac3d1f5af94e0Mathias Agopian status_t mInitCheck; 1201bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 1211bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian // protected by mLock 1221bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian mutable Mutex mLock; 1231bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian DefaultKeyedVector<int, SensorRecord*> mActiveSensors; 124671a6ff4be11b3e2d8eb017e0c7a78e6133fb2b8Mathias Agopian DefaultKeyedVector<int, SensorInterface*> mActiveVirtualSensors; 1251bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian SortedVector< wp<SensorEventConnection> > mActiveConnections; 1261bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 127aea786f42cee6a899e8f78fdbf82cb1cac544430Mathias Agopian // The size of this vector is constant, only the items are mutable 128aea786f42cee6a899e8f78fdbf82cb1cac544430Mathias Agopian KeyedVector<int32_t, sensors_event_t> mLastEventSeen; 129aea786f42cee6a899e8f78fdbf82cb1cac544430Mathias Agopian 1301bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianpublic: 1311bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian static char const* getServiceName() { return "sensorservice"; } 1321bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 133f0aec213ec02fb833f3c927206dad9693e0672deMathias Agopian void cleanupConnection(SensorEventConnection* connection); 1341bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian status_t enable(const sp<SensorEventConnection>& connection, int handle); 1351bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian status_t disable(const sp<SensorEventConnection>& connection, int handle); 136d8653518449e3dbf03fe6f8b11dbdff5ef6ce60eMathias Agopian status_t setEventRate(const sp<SensorEventConnection>& connection, int handle, nsecs_t ns); 1371bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian}; 1381bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 1391bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian// --------------------------------------------------------------------------- 1401bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian}; // namespace android 1411bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 1421bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#endif // ANDROID_SENSOR_SERVICE_H 143