1589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian/* 2589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Copyright (C) 2010 The Android Open Source Project 3589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * 4589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * you may not use this file except in compliance with the License. 6589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * You may obtain a copy of the License at 7589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * 8589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * 10589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * Unless required by applicable law or agreed to in writing, software 11589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * See the License for the specific language governing permissions and 14589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian * limitations under the License. 15589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian */ 16589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 17a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian#define LOG_TAG "Sensors" 18a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian 19589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <stdint.h> 20589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <sys/types.h> 21589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 22589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/Errors.h> 23589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/RefBase.h> 24589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <utils/Singleton.h> 25589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 261a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian#include <binder/IBinder.h> 27a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian#include <binder/IServiceManager.h> 28a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian 29589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/ISensorServer.h> 30589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/ISensorEventConnection.h> 31589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/Sensor.h> 32589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/SensorManager.h> 33589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian#include <gui/SensorEventQueue.h> 34589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 35589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ---------------------------------------------------------------------------- 36589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiannamespace android { 37589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ---------------------------------------------------------------------------- 38589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 39589ce85ee4174829cfedce91b6b2509d2a4002ebMathias AgopianANDROID_SINGLETON_STATIC_INSTANCE(SensorManager) 40589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 41589ce85ee4174829cfedce91b6b2509d2a4002ebMathias AgopianSensorManager::SensorManager() 42589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian : mSensorList(0) 43589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{ 441a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian // okay we're not locked here, but it's not needed during construction 451a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian assertStateLocked(); 46589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian} 47589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 48589ce85ee4174829cfedce91b6b2509d2a4002ebMathias AgopianSensorManager::~SensorManager() 49589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{ 50a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian free(mSensorList); 51589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian} 52589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 531a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopianvoid SensorManager::sensorManagerDied() 541a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian{ 551a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian Mutex::Autolock _l(mLock); 561a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensorServer.clear(); 571a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian free(mSensorList); 581a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensorList = NULL; 591a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensors.clear(); 601a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian} 611a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian 621a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopianstatus_t SensorManager::assertStateLocked() const { 631a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian if (mSensorServer == NULL) { 641a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian // try for one second 651a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian const String16 name("sensorservice"); 661a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian for (int i=0 ; i<4 ; i++) { 671a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian status_t err = getService(name, &mSensorServer); 681a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian if (err == NAME_NOT_FOUND) { 691a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian usleep(250000); 701a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian continue; 711a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 721a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian if (err != NO_ERROR) { 731a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian return err; 741a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 751a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian break; 761a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 771a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian 781a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian class DeathObserver : public IBinder::DeathRecipient { 791a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian SensorManager& mSensorManger; 801a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian virtual void binderDied(const wp<IBinder>& who) { 8132397c1cd3327905173b36baa6fd1c579bc328ffSteve Block ALOGW("sensorservice died [%p]", who.unsafe_get()); 821a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensorManger.sensorManagerDied(); 831a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 841a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian public: 851a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian DeathObserver(SensorManager& mgr) : mSensorManger(mgr) { } 861a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian }; 871a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian 881a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mDeathObserver = new DeathObserver(*const_cast<SensorManager *>(this)); 891a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensorServer->asBinder()->linkToDeath(mDeathObserver); 901a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian 911a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensors = mSensorServer->getSensorList(); 921a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian size_t count = mSensors.size(); 931a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*)); 941a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian for (size_t i=0 ; i<count ; i++) { 951a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensorList[i] = mSensors.array() + i; 961a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 971a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 981a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian 991a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian return NO_ERROR; 1001a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian} 1011a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian 1021a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian 1031a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian 104a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopianssize_t SensorManager::getSensorList(Sensor const* const** list) const 105589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{ 1061a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian Mutex::Autolock _l(mLock); 1071a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian status_t err = assertStateLocked(); 1081a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian if (err < 0) { 1091a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian return ssize_t(err); 1101a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 111589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian *list = mSensorList; 112589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian return mSensors.size(); 113589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian} 114589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 115a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias AgopianSensor const* SensorManager::getDefaultSensor(int type) 116589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{ 1171a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian Mutex::Autolock _l(mLock); 1181a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian if (assertStateLocked() == NO_ERROR) { 1191a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian // For now we just return the first sensor of that type we find. 1201a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian // in the future it will make sense to let the SensorService make 1211a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian // that decision. 1221a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian for (size_t i=0 ; i<mSensors.size() ; i++) { 1231a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian if (mSensorList[i]->getType() == type) 1241a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian return mSensorList[i]; 1251a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 126a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian } 127a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian return NULL; 128589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian} 129589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 130589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiansp<SensorEventQueue> SensorManager::createEventQueue() 131589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{ 132be58de0d627fa0ecb087eeff95da13c783bf2392Mathias Agopian sp<SensorEventQueue> queue; 133be58de0d627fa0ecb087eeff95da13c783bf2392Mathias Agopian 1341a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian Mutex::Autolock _l(mLock); 1351a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian while (assertStateLocked() == NO_ERROR) { 1361a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian sp<ISensorEventConnection> connection = 1371a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian mSensorServer->createSensorEventConnection(); 1381a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian if (connection == NULL) { 1391a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian // SensorService just died. 140e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block ALOGE("createEventQueue: connection is NULL. SensorService died."); 1411a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian continue; 1421a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian } 1431a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian queue = new SensorEventQueue(connection); 1441a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian break; 145be58de0d627fa0ecb087eeff95da13c783bf2392Mathias Agopian } 146be58de0d627fa0ecb087eeff95da13c783bf2392Mathias Agopian return queue; 147589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian} 148589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian 149589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ---------------------------------------------------------------------------- 150589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}; // namespace android 151