1b957b9d63c88efd3a961759424987b99219adeedMathias Agopian/*
2b957b9d63c88efd3a961759424987b99219adeedMathias Agopian * Copyright (C) 2009 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
17b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#define LOG_TAG "sensor"
18b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/Log.h>
19b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
20b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <android/looper.h>
21b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <android/sensor.h>
22b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
23b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/RefBase.h>
244fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown#include <utils/Looper.h>
25b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <utils/Timers.h>
26b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
27b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/Sensor.h>
28b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/SensorManager.h>
29b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <gui/SensorEventQueue.h>
30b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
31b957b9d63c88efd3a961759424987b99219adeedMathias Agopian#include <poll.h>
32b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
33b957b9d63c88efd3a961759424987b99219adeedMathias Agopianusing android::sp;
34b957b9d63c88efd3a961759424987b99219adeedMathias Agopianusing android::Sensor;
35b957b9d63c88efd3a961759424987b99219adeedMathias Agopianusing android::SensorManager;
36b957b9d63c88efd3a961759424987b99219adeedMathias Agopianusing android::SensorEventQueue;
37b957b9d63c88efd3a961759424987b99219adeedMathias Agopianusing android::String8;
38b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
39b957b9d63c88efd3a961759424987b99219adeedMathias Agopian/*****************************************************************************/
40b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
41b957b9d63c88efd3a961759424987b99219adeedMathias AgopianASensorManager* ASensorManager_getInstance()
42b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
43b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return &SensorManager::getInstance();
44b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
45b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
461bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianint ASensorManager_getSensorList(ASensorManager* manager,
471bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian        ASensorList* list)
48b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
491bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian    Sensor const* const* l;
50b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    int c = static_cast<SensorManager*>(manager)->getSensorList(&l);
51b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    if (list) {
521bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian        *list = reinterpret_cast<ASensorList>(l);
53b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    }
54b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return c;
55b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
56b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
571bf797857e025e8a71db86fb9e79765a767ec1ebMathias AgopianASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type)
58b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
59b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return static_cast<SensorManager*>(manager)->getDefaultSensor(type);
60b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
61b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
6299fe16508200510e3d8ea2e3c34872be8cce826dAravind AkellaASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager,
6399fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella        int type, bool wakeUp) {
6499fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella    Sensor const* const* sensorList;
6599fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella    size_t size = static_cast<SensorManager*>(manager)->getSensorList(&sensorList);
6699fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella    for (size_t i = 0; i < size; ++i) {
6799fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella        if (ASensor_getType(sensorList[i]) == type &&
6899fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella            ASensor_isWakeUpSensor(sensorList[i]) == wakeUp) {
6999fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella            return reinterpret_cast<ASensor const *>(sensorList[i]);
7099fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella       }
7199fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella    }
7299fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella    return NULL;
7399fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella}
7499fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella
75b957b9d63c88efd3a961759424987b99219adeedMathias AgopianASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
764fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        ALooper* looper, int ident, ALooper_callbackFunc callback, void* data)
77b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
78b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    sp<SensorEventQueue> queue =
79b957b9d63c88efd3a961759424987b99219adeedMathias Agopian            static_cast<SensorManager*>(manager)->createEventQueue();
80b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    if (queue != 0) {
814fe6c3e51be77e35f40872cdbca6c80f8f8b7ecbJeff Brown        ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data);
82b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        queue->looper = looper;
83b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        queue->incStrong(manager);
84b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    }
85b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return static_cast<ASensorEventQueue*>(queue.get());
86b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
87b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
88b957b9d63c88efd3a961759424987b99219adeedMathias Agopianint ASensorManager_destroyEventQueue(ASensorManager* manager,
89b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        ASensorEventQueue* inQueue)
90b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
91b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    sp<SensorEventQueue> queue = static_cast<SensorEventQueue*>(inQueue);
92b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    ALooper_removeFd(queue->looper, queue->getFd());
93b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    queue->decStrong(manager);
94b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return 0;
95b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
96b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
97b957b9d63c88efd3a961759424987b99219adeedMathias Agopian/*****************************************************************************/
98b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
991bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianint ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor)
100b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
101b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return static_cast<SensorEventQueue*>(queue)->enableSensor(
1021bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian            static_cast<Sensor const*>(sensor));
103b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
104b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
1051bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianint ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor)
106b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
107b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return static_cast<SensorEventQueue*>(queue)->disableSensor(
1081bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian            static_cast<Sensor const*>(sensor));
109b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
110b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
1111bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianint ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor,
112b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        int32_t usec)
113b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
114b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return static_cast<SensorEventQueue*>(queue)->setEventRate(
1151bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian            static_cast<Sensor const*>(sensor), us2ns(usec));
116b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
117b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
118b957b9d63c88efd3a961759424987b99219adeedMathias Agopianint ASensorEventQueue_hasEvents(ASensorEventQueue* queue)
119b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
120b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    struct pollfd pfd;
121b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    pfd.fd = static_cast<SensorEventQueue*>(queue)->getFd();
122b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    pfd.events = POLLIN;
123b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    pfd.revents = 0;
124b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
125b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    int nfd = poll(&pfd, 1, 0);
126b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
127b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    if (nfd < 0)
128b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        return -errno;
129b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
130b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    if (pfd.revents != POLLIN)
131b957b9d63c88efd3a961759424987b99219adeedMathias Agopian        return -1;
132b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
133b957b9d63c88efd3a961759424987b99219adeedMathias Agopian    return (nfd == 0) ? 0 : 1;
134b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
135b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
136b957b9d63c88efd3a961759424987b99219adeedMathias Agopianssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
137b957b9d63c88efd3a961759424987b99219adeedMathias Agopian                ASensorEvent* events, size_t count)
138b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
13935187bd5c1e40ea52a672c525411d05cbcdd6482Aravind Akella    ssize_t actual = static_cast<SensorEventQueue*>(queue)->read(events, count);
14035187bd5c1e40ea52a672c525411d05cbcdd6482Aravind Akella    if (actual > 0) {
14135187bd5c1e40ea52a672c525411d05cbcdd6482Aravind Akella        static_cast<SensorEventQueue*>(queue)->sendAck(events, actual);
14235187bd5c1e40ea52a672c525411d05cbcdd6482Aravind Akella    }
14335187bd5c1e40ea52a672c525411d05cbcdd6482Aravind Akella    return actual;
144b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
145b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
146b957b9d63c88efd3a961759424987b99219adeedMathias Agopian/*****************************************************************************/
147b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
1481bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianconst char* ASensor_getName(ASensor const* sensor)
149b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
1501bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian    return static_cast<Sensor const*>(sensor)->getName().string();
151b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
152b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
1531bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianconst char* ASensor_getVendor(ASensor const* sensor)
154b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
1551bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian    return static_cast<Sensor const*>(sensor)->getVendor().string();
156b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
157b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
1581bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianint ASensor_getType(ASensor const* sensor)
159b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
1601bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian    return static_cast<Sensor const*>(sensor)->getType();
161b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
162b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
1631bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopianfloat ASensor_getResolution(ASensor const* sensor)
164b957b9d63c88efd3a961759424987b99219adeedMathias Agopian{
1651bf797857e025e8a71db86fb9e79765a767ec1ebMathias Agopian    return static_cast<Sensor const*>(sensor)->getResolution();
166b957b9d63c88efd3a961759424987b99219adeedMathias Agopian}
167b957b9d63c88efd3a961759424987b99219adeedMathias Agopian
168050b56244ff46d43e4886018d7cd20f0b1dc02b9Mathias Agopianint ASensor_getMinDelay(ASensor const* sensor)
169050b56244ff46d43e4886018d7cd20f0b1dc02b9Mathias Agopian{
170050b56244ff46d43e4886018d7cd20f0b1dc02b9Mathias Agopian    return static_cast<Sensor const*>(sensor)->getMinDelay();
171050b56244ff46d43e4886018d7cd20f0b1dc02b9Mathias Agopian}
172cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella
173cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akellaint ASensor_getFifoMaxEventCount(ASensor const* sensor)
174cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella{
175cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella    return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount();
176cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella}
177cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella
178cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akellaint ASensor_getFifoReservedEventCount(ASensor const* sensor)
179cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella{
180cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella    return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount();
181cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella}
182cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella
183cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akellaconst char* ASensor_getStringType(ASensor const* sensor)
184cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella{
185cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella    return static_cast<Sensor const*>(sensor)->getStringType().string();
186cd9a7bb3ba742606f93dcbd549d6d6eeb1cb5247Aravind Akella}
18727900358f2f5b89e8b01516b28a916d16c99ba9bAravind Akella
18827900358f2f5b89e8b01516b28a916d16c99ba9bAravind Akellaint ASensor_getReportingMode(ASensor const* sensor)
18927900358f2f5b89e8b01516b28a916d16c99ba9bAravind Akella{
19027900358f2f5b89e8b01516b28a916d16c99ba9bAravind Akella    return static_cast<Sensor const*>(sensor)->getReportingMode();
19127900358f2f5b89e8b01516b28a916d16c99ba9bAravind Akella}
19299fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella
19399fe16508200510e3d8ea2e3c34872be8cce826dAravind Akellabool ASensor_isWakeUpSensor(ASensor const* sensor)
19499fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella{
19599fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella    return static_cast<Sensor const*>(sensor)->isWakeUpSensor();
19699fe16508200510e3d8ea2e3c34872be8cce826dAravind Akella}