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) {
119b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella        bool wakeUpSensor = false;
120b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella        // For the following sensor types, return a wake-up sensor. These types are by default
121b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella        // defined as wake-up sensors. For the rest of the sensor types defined in sensors.h return
122b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella        // a non_wake-up version.
123b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella        if (type == SENSOR_TYPE_PROXIMITY || type == SENSOR_TYPE_SIGNIFICANT_MOTION ||
124b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella            type == SENSOR_TYPE_TILT_DETECTOR || type == SENSOR_TYPE_WAKE_GESTURE ||
125b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella            type == SENSOR_TYPE_GLANCE_GESTURE || type == SENSOR_TYPE_PICK_UP_GESTURE) {
126b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella            wakeUpSensor = true;
127b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella        }
1281a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        // For now we just return the first sensor of that type we find.
1291a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        // in the future it will make sense to let the SensorService make
1301a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        // that decision.
1311a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        for (size_t i=0 ; i<mSensors.size() ; i++) {
132b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella            if (mSensorList[i]->getType() == type &&
133b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella                mSensorList[i]->isWakeUpSensor() == wakeUpSensor) {
1341a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian                return mSensorList[i];
135b37ba399c1521c3eba40e126f65d9663532de446Aravind Akella            }
1361a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        }
137a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    }
138a7352c9f4a6e642c29782b19db5bc0bd98feddc8Mathias Agopian    return NULL;
139589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
140589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
141589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopiansp<SensorEventQueue> SensorManager::createEventQueue()
142589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian{
143be58de0d627fa0ecb087eeff95da13c783bf2392Mathias Agopian    sp<SensorEventQueue> queue;
144be58de0d627fa0ecb087eeff95da13c783bf2392Mathias Agopian
1451a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian    Mutex::Autolock _l(mLock);
1461a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian    while (assertStateLocked() == NO_ERROR) {
1471a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        sp<ISensorEventConnection> connection =
1481a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian                mSensorServer->createSensorEventConnection();
1491a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        if (connection == NULL) {
1501a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian            // SensorService just died.
151e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block            ALOGE("createEventQueue: connection is NULL. SensorService died.");
1521a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian            continue;
1531a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        }
1541a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        queue = new SensorEventQueue(connection);
1551a2b83a17d3bdb2440b8b5b96136f7916a5528b5Mathias Agopian        break;
156be58de0d627fa0ecb087eeff95da13c783bf2392Mathias Agopian    }
157be58de0d627fa0ecb087eeff95da13c783bf2392Mathias Agopian    return queue;
158589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}
159589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian
160589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian// ----------------------------------------------------------------------------
161589ce85ee4174829cfedce91b6b2509d2a4002ebMathias Agopian}; // namespace android
162