blob: 26b41e85fddd253f181478c695f1f999eef63540 [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;
Svet Ganovcbb56a82015-05-07 10:49:07 -070038using android::String16;
Mathias Agopianb957b9d2010-07-13 22:21:56 -070039
40/*****************************************************************************/
41
Narayan Kamath8d1e3442015-07-29 09:35:46 +000042android::Mutex android::SensorManager::sLock;
43std::map<String16, SensorManager*> android::SensorManager::sPackageInstances;
44
Svet Ganovcbb56a82015-05-07 10:49:07 -070045ASensorManager* ASensorManager_getInstance()
46{
47 return ASensorManager_getInstanceForPackage(NULL);
48}
49
50ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName)
51{
52 if (packageName) {
53 return &SensorManager::getInstanceForPackage(String16(packageName));
54 } else {
55 return &SensorManager::getInstanceForPackage(String16());
56 }
57}
58
Mathias Agopian1bf79782010-07-14 23:41:37 -070059int ASensorManager_getSensorList(ASensorManager* manager,
60 ASensorList* list)
Mathias Agopianb957b9d2010-07-13 22:21:56 -070061{
Mathias Agopian1bf79782010-07-14 23:41:37 -070062 Sensor const* const* l;
Mathias Agopianb957b9d2010-07-13 22:21:56 -070063 int c = static_cast<SensorManager*>(manager)->getSensorList(&l);
64 if (list) {
Mathias Agopian1bf79782010-07-14 23:41:37 -070065 *list = reinterpret_cast<ASensorList>(l);
Mathias Agopianb957b9d2010-07-13 22:21:56 -070066 }
67 return c;
68}
69
Mathias Agopian1bf79782010-07-14 23:41:37 -070070ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type)
Mathias Agopianb957b9d2010-07-13 22:21:56 -070071{
72 return static_cast<SensorManager*>(manager)->getDefaultSensor(type);
73}
74
Aravind Akella99fe1652014-08-05 15:02:11 -070075ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager,
76 int type, bool wakeUp) {
77 Sensor const* const* sensorList;
78 size_t size = static_cast<SensorManager*>(manager)->getSensorList(&sensorList);
79 for (size_t i = 0; i < size; ++i) {
80 if (ASensor_getType(sensorList[i]) == type &&
81 ASensor_isWakeUpSensor(sensorList[i]) == wakeUp) {
82 return reinterpret_cast<ASensor const *>(sensorList[i]);
83 }
84 }
85 return NULL;
86}
87
Mathias Agopianb957b9d2010-07-13 22:21:56 -070088ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070089 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data)
Mathias Agopianb957b9d2010-07-13 22:21:56 -070090{
91 sp<SensorEventQueue> queue =
92 static_cast<SensorManager*>(manager)->createEventQueue();
93 if (queue != 0) {
Jeff Brown4fe6c3e2010-09-13 23:17:30 -070094 ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data);
Mathias Agopianb957b9d2010-07-13 22:21:56 -070095 queue->looper = looper;
96 queue->incStrong(manager);
97 }
98 return static_cast<ASensorEventQueue*>(queue.get());
99}
100
101int ASensorManager_destroyEventQueue(ASensorManager* manager,
102 ASensorEventQueue* inQueue)
103{
104 sp<SensorEventQueue> queue = static_cast<SensorEventQueue*>(inQueue);
105 ALooper_removeFd(queue->looper, queue->getFd());
106 queue->decStrong(manager);
107 return 0;
108}
109
110/*****************************************************************************/
111
Mathias Agopian1bf79782010-07-14 23:41:37 -0700112int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700113{
114 return static_cast<SensorEventQueue*>(queue)->enableSensor(
Mathias Agopian1bf79782010-07-14 23:41:37 -0700115 static_cast<Sensor const*>(sensor));
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700116}
117
Mathias Agopian1bf79782010-07-14 23:41:37 -0700118int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700119{
120 return static_cast<SensorEventQueue*>(queue)->disableSensor(
Mathias Agopian1bf79782010-07-14 23:41:37 -0700121 static_cast<Sensor const*>(sensor));
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700122}
123
Mathias Agopian1bf79782010-07-14 23:41:37 -0700124int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor,
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700125 int32_t usec)
126{
127 return static_cast<SensorEventQueue*>(queue)->setEventRate(
Mathias Agopian1bf79782010-07-14 23:41:37 -0700128 static_cast<Sensor const*>(sensor), us2ns(usec));
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700129}
130
131int ASensorEventQueue_hasEvents(ASensorEventQueue* queue)
132{
133 struct pollfd pfd;
134 pfd.fd = static_cast<SensorEventQueue*>(queue)->getFd();
135 pfd.events = POLLIN;
136 pfd.revents = 0;
137
138 int nfd = poll(&pfd, 1, 0);
139
140 if (nfd < 0)
141 return -errno;
142
143 if (pfd.revents != POLLIN)
144 return -1;
145
146 return (nfd == 0) ? 0 : 1;
147}
148
149ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
150 ASensorEvent* events, size_t count)
151{
Aravind Akella35187bd2014-02-11 18:44:42 -0800152 ssize_t actual = static_cast<SensorEventQueue*>(queue)->read(events, count);
153 if (actual > 0) {
154 static_cast<SensorEventQueue*>(queue)->sendAck(events, actual);
155 }
156 return actual;
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700157}
158
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700159/*****************************************************************************/
160
Mathias Agopian1bf79782010-07-14 23:41:37 -0700161const char* ASensor_getName(ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700162{
Mathias Agopian1bf79782010-07-14 23:41:37 -0700163 return static_cast<Sensor const*>(sensor)->getName().string();
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700164}
165
Mathias Agopian1bf79782010-07-14 23:41:37 -0700166const char* ASensor_getVendor(ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700167{
Mathias Agopian1bf79782010-07-14 23:41:37 -0700168 return static_cast<Sensor const*>(sensor)->getVendor().string();
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700169}
170
Mathias Agopian1bf79782010-07-14 23:41:37 -0700171int ASensor_getType(ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700172{
Mathias Agopian1bf79782010-07-14 23:41:37 -0700173 return static_cast<Sensor const*>(sensor)->getType();
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700174}
175
Mathias Agopian1bf79782010-07-14 23:41:37 -0700176float ASensor_getResolution(ASensor const* sensor)
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700177{
Mathias Agopian1bf79782010-07-14 23:41:37 -0700178 return static_cast<Sensor const*>(sensor)->getResolution();
Mathias Agopianb957b9d2010-07-13 22:21:56 -0700179}
180
Mathias Agopian050b5622010-07-29 16:51:38 -0700181int ASensor_getMinDelay(ASensor const* sensor)
182{
183 return static_cast<Sensor const*>(sensor)->getMinDelay();
184}
Aravind Akellacd9a7bb2014-04-07 22:55:21 +0000185
186int ASensor_getFifoMaxEventCount(ASensor const* sensor)
187{
188 return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount();
189}
190
191int ASensor_getFifoReservedEventCount(ASensor const* sensor)
192{
193 return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount();
194}
195
196const char* ASensor_getStringType(ASensor const* sensor)
197{
198 return static_cast<Sensor const*>(sensor)->getStringType().string();
199}
Aravind Akella27900352014-06-03 19:20:42 -0700200
201int ASensor_getReportingMode(ASensor const* sensor)
202{
203 return static_cast<Sensor const*>(sensor)->getReportingMode();
204}
Aravind Akella99fe1652014-08-05 15:02:11 -0700205
206bool ASensor_isWakeUpSensor(ASensor const* sensor)
207{
208 return static_cast<Sensor const*>(sensor)->isWakeUpSensor();
Narayan Kamath8d1e3442015-07-29 09:35:46 +0000209}