SensorManager.cpp revision be58de0d627fa0ecb087eeff95da13c783bf2392
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#define LOG_TAG "Sensors"
18
19#include <stdint.h>
20#include <sys/types.h>
21
22#include <utils/Errors.h>
23#include <utils/RefBase.h>
24#include <utils/Singleton.h>
25
26#include <binder/IServiceManager.h>
27
28#include <gui/ISensorServer.h>
29#include <gui/ISensorEventConnection.h>
30#include <gui/Sensor.h>
31#include <gui/SensorManager.h>
32#include <gui/SensorEventQueue.h>
33
34// ----------------------------------------------------------------------------
35namespace android {
36// ----------------------------------------------------------------------------
37
38ANDROID_SINGLETON_STATIC_INSTANCE(SensorManager)
39
40SensorManager::SensorManager()
41    : mSensorList(0)
42{
43    const String16 name("sensorservice");
44    while (getService(name, &mSensorServer) != NO_ERROR) {
45        usleep(250000);
46    }
47
48    mSensors = mSensorServer->getSensorList();
49    size_t count = mSensors.size();
50    mSensorList = (Sensor const**)malloc(count * sizeof(Sensor*));
51    for (size_t i=0 ; i<count ; i++) {
52        mSensorList[i] = mSensors.array() + i;
53    }
54}
55
56SensorManager::~SensorManager()
57{
58    free(mSensorList);
59}
60
61ssize_t SensorManager::getSensorList(Sensor const* const** list) const
62{
63    *list = mSensorList;
64    return mSensors.size();
65}
66
67Sensor const* SensorManager::getDefaultSensor(int type)
68{
69    // For now we just return the first sensor of that type we find.
70    // in the future it will make sense to let the SensorService make
71    // that decision.
72    for (size_t i=0 ; i<mSensors.size() ; i++) {
73        if (mSensorList[i]->getType() == type)
74            return mSensorList[i];
75    }
76    return NULL;
77}
78
79sp<SensorEventQueue> SensorManager::createEventQueue()
80{
81    sp<SensorEventQueue> queue;
82
83    if (mSensorServer == NULL) {
84        LOGE("createEventQueue: mSensorSever is NULL");
85        return queue;
86    }
87
88    sp<ISensorEventConnection> connection =
89            mSensorServer->createSensorEventConnection();
90    if (connection == NULL) {
91        LOGE("createEventQueue: connection is NULL");
92        return queue;
93    }
94
95    queue = new SensorEventQueue(connection);
96
97    return queue;
98}
99
100// ----------------------------------------------------------------------------
101}; // namespace android
102