1b957b9d63c88efd3a961759424987b99219adeedMathias Agopian/* 2b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * Copyright (C) 2010 The Android Open Source Project 3b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * 4b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * you may not use this file except in compliance with the License. 6b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * You may obtain a copy of the License at 7b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * 8b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * 10b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * Unless required by applicable law or agreed to in writing, software 11b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * See the License for the specific language governing permissions and 14b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * limitations under the License. 15b957b9d63c88efd3a961759424987b99219adeedMathias Agopian */ 16b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 171bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#define LOG_TAG "Sensors" 181bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 19b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <stdint.h> 20b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <sys/types.h> 21b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 22b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/Errors.h> 23b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/RefBase.h> 24b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/Singleton.h> 25b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 26de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian#include <binder/IBinder.h> 271bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian#include <binder/IServiceManager.h> 281bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian 29b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/ISensorServer.h> 30b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/ISensorEventConnection.h> 31b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/Sensor.h> 32b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/SensorManager.h> 33b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/SensorEventQueue.h> 34b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 35b957b9d63c88efd3a961759424987b99219adeedMathias Agopian// ---------------------------------------------------------------------------- 36b957b9d63c88efd3a961759424987b99219adeedMathias Agopiannamespace android { 37b957b9d63c88efd3a961759424987b99219adeedMathias Agopian// ---------------------------------------------------------------------------- 38b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 39b957b9d63c88efd3a961759424987b99219adeedMathias AgopianANDROID_SINGLETON_STATIC_INSTANCE(SensorManager) 40b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 41b957b9d63c88efd3a961759424987b99219adeedMathias AgopianSensorManager::SensorManager() 42b957b9d63c88efd3a961759424987b99219adeedMathias Agopian : mSensorList(0) 43b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{ 44de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian // okay we're not locked here, but it's not needed during construction 45de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian assertStateLocked(); 46b957b9d63c88efd3a961759424987b99219adeedMathias Agopian} 47b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 48b957b9d63c88efd3a961759424987b99219adeedMathias AgopianSensorManager::~SensorManager() 49b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{ 501bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian free(mSensorList); 51b957b9d63c88efd3a961759424987b99219adeedMathias Agopian} 52b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 53de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopianvoid SensorManager::sensorManagerDied() 54de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian{ 55de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian Mutex::Autolock _l(mLock); 56de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensorServer.clear(); 57de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian free(mSensorList); 58de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensorList = NULL; 59de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensors.clear(); 60de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian} 61de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian 62de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopianstatus_t SensorManager::assertStateLocked() const { 63de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian if (mSensorServer == NULL) { 64de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian // try for one second 65de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian const String16 name("sensorservice"); 66de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian for (int i=0 ; i<4 ; i++) { 67de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian status_t err = getService(name, &mSensorServer); 68de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian if (err == NAME_NOT_FOUND) { 69de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian usleep(250000); 70de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian continue; 71de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 72de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian if (err != NO_ERROR) { 73de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian return err; 74de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 75de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian break; 76de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 77de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian 78de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian class DeathObserver : public IBinder::DeathRecipient { 79de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian SensorManager& mSensorManger; 80de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian virtual void binderDied(const wp<IBinder>& who) { 81de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian LOGW("sensorservice died [%p]", who.unsafe_get()); 82de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensorManger.sensorManagerDied(); 83de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 84de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian public: 85de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { } 86de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian }; 87de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian 88de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this)); 89de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensorServer->asBinder()->linkToDeath(mDeathObserver); 90de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian 91de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensors = mSensorServer->getSensorList(); 92de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian size_t count = mSensors.size(); 93de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*)); 94de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian for (size_t i=0 ; i<count ; i++) { 95de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensorList[i] = mSensors.array() + i; 96de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 97de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 98de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian 99de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian return NO_ERROR; 100de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian} 101de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian 102de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian 103de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian 1041bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianssize_t SensorManager::getSensorList(Sensor const* const** list) const 105b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{ 106de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian Mutex::Autolock _l(mLock); 107de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian status_t err = assertStateLocked(); 108de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian if (err < 0) { 109de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian return ssize_t(err); 110de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 111b957b9d63c88efd3a961759424987b99219adeedMathias Agopian *list = mSensorList; 112b957b9d63c88efd3a961759424987b99219adeedMathias Agopian return mSensors.size(); 113b957b9d63c88efd3a961759424987b99219adeedMathias Agopian} 114b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 1151bf797857e025e8a71db86fb9e79765a767ec1ebMathias AgopianSensor const* SensorManager::getDefaultSensor(int type) 116b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{ 117de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian Mutex::Autolock _l(mLock); 118de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian if (assertStateLocked() == NO_ERROR) { 119de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian // For now we just return the first sensor of that type we find. 120de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian // in the future it will make sense to let the SensorService make 121de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian // that decision. 122de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian for (size_t i=0 ; i<mSensors.size() ; i++) { 123de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian if (mSensorList[i]->getType() == type) 124de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian return mSensorList[i]; 125de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 1261bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian } 1271bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian return NULL; 128b957b9d63c88efd3a961759424987b99219adeedMathias Agopian} 129b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 130b957b9d63c88efd3a961759424987b99219adeedMathias Agopiansp<SensorEventQueue> SensorManager::createEventQueue() 131b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{ 132a338379873cdbaf44211223b40592d7a8284f58bMathias Agopian sp<SensorEventQueue> queue; 133a338379873cdbaf44211223b40592d7a8284f58bMathias Agopian 134de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian Mutex::Autolock _l(mLock); 135de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian while (assertStateLocked() == NO_ERROR) { 136de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian sp<ISensorEventConnection> connection = 137de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian mSensorServer->createSensorEventConnection(); 138de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian if (connection == NULL) { 139de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian // SensorService just died. 140de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian LOGE("createEventQueue: connection is NULL. SensorService died."); 141de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian continue; 142de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian } 143de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian queue = new SensorEventQueue(connection); 144de6c44e90681cbdb754e55721953fc828ec8e2aaMathias Agopian break; 145a338379873cdbaf44211223b40592d7a8284f58bMathias Agopian } 146a338379873cdbaf44211223b40592d7a8284f58bMathias Agopian return queue; 147b957b9d63c88efd3a961759424987b99219adeedMathias Agopian} 148b957b9d63c88efd3a961759424987b99219adeedMathias Agopian 149b957b9d63c88efd3a961759424987b99219adeedMathias Agopian// ---------------------------------------------------------------------------- 150b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}; // namespace android 151