blob: 73b52aa2c6043319ba52b10689fd4eaae3da6c6c [file] [log] [blame]
Mathias Agopianb957b9d2010-07-13 22:21:56 -07001/*
2 * Copyright (C) 2009 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 "sensor"
18#include <utils/Log.h>
19
20#include <android/looper.h>
21#include <android/sensor.h>
22
23#include <utils/RefBase.h>
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070024#include <utils/Looper.h>
Mathias Agopianb957b9d2010-07-13 22:21:56 -070025#include <utils/Timers.h>
26
27#include <gui/Sensor.h>
28#include <gui/SensorManager.h>
29#include <gui/SensorEventQueue.h>
30
31#include <poll.h>
32
33using android::sp;
34using android::Sensor;
35using android::SensorManager;
36using android::SensorEventQueue;
37using android::String8;
38
39/*****************************************************************************/
40
41ASensorManager* ASensorManager_getInstance()
42{
43 return &SensorManager::getInstance();
44}
45
Mathias Agopian1bf79782010-07-14 23:41:37 -070046int ASensorManager_getSensorList(ASensorManager* manager,
47 ASensorList* list)
Mathias Agopianb957b9d2010-07-13 22:21:56 -070048{
Mathias Agopian1bf79782010-07-14 23:41:37 -070049 Sensor const* const* l;
Mathias Agopianb957b9d2010-07-13 22:21:56 -070050 int c = static_cast<SensorManager*>(manager)->getSensorList(&l);
51 if (list) {
Mathias Agopian1bf79782010-07-14 23:41:37 -070052 *list = reinterpret_cast<ASensorList>(l);
Mathias Agopianb957b9d2010-07-13 22:21:56 -070053 }
54 return c;
55}
56
Mathias Agopian1bf79782010-07-14 23:41:37 -070057ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type)
Mathias Agopianb957b9d2010-07-13 22:21:56 -070058{
59 return static_cast<SensorManager*>(manager)->getDefaultSensor(type);
60}
61
Aravind Akella99fe1652014-08-05 15:02:11 -070062ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager,
63 int type, bool wakeUp) {
64 Sensor const* const* sensorList;
65 size_t size = static_cast<SensorManager*>(manager)->getSensorList(&sensorList);
66 for (size_t i = 0; i < size; ++i) {
67 if (ASensor_getType(sensorList[i]) == type &&
68 ASensor_isWakeUpSensor(sensorList[i]) == wakeUp) {
69 return reinterpret_cast<ASensor const *>(sensorList[i]);
70 }
71 }
72 return NULL;
73}
74
Mathias Agopianb957b9d2010-07-13 22:21:56 -070075ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070076 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data)
Mathias Agopianb957b9d2010-07-13 22:21:56 -070077{
78 sp<SensorEventQueue> queue =
79 static_cast<SensorManager*>(manager)->createEventQueue();
80 if (queue != 0) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070081 ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data);
Mathias Agopianb957b9d2010-07-13 22:21:56 -070082 queue->looper = looper;
83 queue->incStrong(manager);
84 }
85 return static_cast<ASensorEventQueue*>(queue.get());
86}
87
88int ASensorManager_destroyEventQueue(ASensorManager* manager,
89 ASensorEventQueue* inQueue)
90{
91 sp<SensorEventQueue> queue = static_cast<SensorEventQueue*>(inQueue);
92 ALooper_removeFd(queue->looper, queue->getFd());
93 queue->decStrong(manager);
94 return 0;
95}
96
97/*****************************************************************************/
98
Mathias Agopian1bf79782010-07-14 23:41:37 -070099int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700100{
101 return static_cast<SensorEventQueue*>(queue)->enableSensor(
Mathias Agopian1bf79782010-07-14 23:41:37 -0700102 static_cast<Sensor const*>(sensor));
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700103}
104
Mathias Agopian1bf79782010-07-14 23:41:37 -0700105int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700106{
107 return static_cast<SensorEventQueue*>(queue)->disableSensor(
Mathias Agopian1bf79782010-07-14 23:41:37 -0700108 static_cast<Sensor const*>(sensor));
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700109}
110
Mathias Agopian1bf79782010-07-14 23:41:37 -0700111int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor,
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700112 int32_t usec)
113{
114 return static_cast<SensorEventQueue*>(queue)->setEventRate(
Mathias Agopian1bf79782010-07-14 23:41:37 -0700115 static_cast<Sensor const*>(sensor), us2ns(usec));
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700116}
117
118int ASensorEventQueue_hasEvents(ASensorEventQueue* queue)
119{
120 struct pollfd pfd;
121 pfd.fd = static_cast<SensorEventQueue*>(queue)->getFd();
122 pfd.events = POLLIN;
123 pfd.revents = 0;
124
125 int nfd = poll(&pfd, 1, 0);
126
127 if (nfd < 0)
128 return -errno;
129
130 if (pfd.revents != POLLIN)
131 return -1;
132
133 return (nfd == 0) ? 0 : 1;
134}
135
136ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
137 ASensorEvent* events, size_t count)
138{
Aravind Akella35187bd2014-02-11 18:44:42 -0800139 ssize_t actual = static_cast<SensorEventQueue*>(queue)->read(events, count);
140 if (actual > 0) {
141 static_cast<SensorEventQueue*>(queue)->sendAck(events, actual);
142 }
143 return actual;
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700144}
145
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700146/*****************************************************************************/
147
Mathias Agopian1bf79782010-07-14 23:41:37 -0700148const char* ASensor_getName(ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700149{
Mathias Agopian1bf79782010-07-14 23:41:37 -0700150 return static_cast<Sensor const*>(sensor)->getName().string();
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700151}
152
Mathias Agopian1bf79782010-07-14 23:41:37 -0700153const char* ASensor_getVendor(ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700154{
Mathias Agopian1bf79782010-07-14 23:41:37 -0700155 return static_cast<Sensor const*>(sensor)->getVendor().string();
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700156}
157
Mathias Agopian1bf79782010-07-14 23:41:37 -0700158int ASensor_getType(ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700159{
Mathias Agopian1bf79782010-07-14 23:41:37 -0700160 return static_cast<Sensor const*>(sensor)->getType();
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700161}
162
Mathias Agopian1bf79782010-07-14 23:41:37 -0700163float ASensor_getResolution(ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700164{
Mathias Agopian1bf79782010-07-14 23:41:37 -0700165 return static_cast<Sensor const*>(sensor)->getResolution();
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700166}
167
Mathias Agopian050b5622010-07-29 16:51:38 -0700168int ASensor_getMinDelay(ASensor const* sensor)
169{
170 return static_cast<Sensor const*>(sensor)->getMinDelay();
171}
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000172
173int ASensor_getFifoMaxEventCount(ASensor const* sensor)
174{
175 return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount();
176}
177
178int ASensor_getFifoReservedEventCount(ASensor const* sensor)
179{
180 return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount();
181}
182
183const char* ASensor_getStringType(ASensor const* sensor)
184{
185 return static_cast<Sensor const*>(sensor)->getStringType().string();
186}
Aravind Akella27900352014-06-03 19:20:42 -0700187
188int ASensor_getReportingMode(ASensor const* sensor)
189{
190 return static_cast<Sensor const*>(sensor)->getReportingMode();
191}
Aravind Akella99fe1652014-08-05 15:02:11 -0700192
193bool ASensor_isWakeUpSensor(ASensor const* sensor)
194{
195 return static_cast<Sensor const*>(sensor)->isWakeUpSensor();
196}