• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
24 #include <utils/Looper.h>
25 #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 
33 using android::sp;
34 using android::Sensor;
35 using android::SensorManager;
36 using android::SensorEventQueue;
37 using android::String8;
38 
39 /*****************************************************************************/
40 
ASensorManager_getInstance()41 ASensorManager* ASensorManager_getInstance()
42 {
43     return &SensorManager::getInstance();
44 }
45 
ASensorManager_getSensorList(ASensorManager * manager,ASensorList * list)46 int ASensorManager_getSensorList(ASensorManager* manager,
47         ASensorList* list)
48 {
49     Sensor const* const* l;
50     int c = static_cast<SensorManager*>(manager)->getSensorList(&l);
51     if (list) {
52         *list = reinterpret_cast<ASensorList>(l);
53     }
54     return c;
55 }
56 
ASensorManager_getDefaultSensor(ASensorManager * manager,int type)57 ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type)
58 {
59     return static_cast<SensorManager*>(manager)->getDefaultSensor(type);
60 }
61 
ASensorManager_getDefaultSensorEx(ASensorManager * manager,int type,bool wakeUp)62 ASensor 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 
ASensorManager_createEventQueue(ASensorManager * manager,ALooper * looper,int ident,ALooper_callbackFunc callback,void * data)75 ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
76         ALooper* looper, int ident, ALooper_callbackFunc callback, void* data)
77 {
78     sp<SensorEventQueue> queue =
79             static_cast<SensorManager*>(manager)->createEventQueue();
80     if (queue != 0) {
81         ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data);
82         queue->looper = looper;
83         queue->incStrong(manager);
84     }
85     return static_cast<ASensorEventQueue*>(queue.get());
86 }
87 
ASensorManager_destroyEventQueue(ASensorManager * manager,ASensorEventQueue * inQueue)88 int 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 
ASensorEventQueue_enableSensor(ASensorEventQueue * queue,ASensor const * sensor)99 int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor)
100 {
101     return static_cast<SensorEventQueue*>(queue)->enableSensor(
102             static_cast<Sensor const*>(sensor));
103 }
104 
ASensorEventQueue_disableSensor(ASensorEventQueue * queue,ASensor const * sensor)105 int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor)
106 {
107     return static_cast<SensorEventQueue*>(queue)->disableSensor(
108             static_cast<Sensor const*>(sensor));
109 }
110 
ASensorEventQueue_setEventRate(ASensorEventQueue * queue,ASensor const * sensor,int32_t usec)111 int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor,
112         int32_t usec)
113 {
114     return static_cast<SensorEventQueue*>(queue)->setEventRate(
115             static_cast<Sensor const*>(sensor), us2ns(usec));
116 }
117 
ASensorEventQueue_hasEvents(ASensorEventQueue * queue)118 int 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 
ASensorEventQueue_getEvents(ASensorEventQueue * queue,ASensorEvent * events,size_t count)136 ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
137                 ASensorEvent* events, size_t count)
138 {
139     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;
144 }
145 
146 /*****************************************************************************/
147 
ASensor_getName(ASensor const * sensor)148 const char* ASensor_getName(ASensor const* sensor)
149 {
150     return static_cast<Sensor const*>(sensor)->getName().string();
151 }
152 
ASensor_getVendor(ASensor const * sensor)153 const char* ASensor_getVendor(ASensor const* sensor)
154 {
155     return static_cast<Sensor const*>(sensor)->getVendor().string();
156 }
157 
ASensor_getType(ASensor const * sensor)158 int ASensor_getType(ASensor const* sensor)
159 {
160     return static_cast<Sensor const*>(sensor)->getType();
161 }
162 
ASensor_getResolution(ASensor const * sensor)163 float ASensor_getResolution(ASensor const* sensor)
164 {
165     return static_cast<Sensor const*>(sensor)->getResolution();
166 }
167 
ASensor_getMinDelay(ASensor const * sensor)168 int ASensor_getMinDelay(ASensor const* sensor)
169 {
170     return static_cast<Sensor const*>(sensor)->getMinDelay();
171 }
172 
ASensor_getFifoMaxEventCount(ASensor const * sensor)173 int ASensor_getFifoMaxEventCount(ASensor const* sensor)
174 {
175     return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount();
176 }
177 
ASensor_getFifoReservedEventCount(ASensor const * sensor)178 int ASensor_getFifoReservedEventCount(ASensor const* sensor)
179 {
180     return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount();
181 }
182 
ASensor_getStringType(ASensor const * sensor)183 const char* ASensor_getStringType(ASensor const* sensor)
184 {
185     return static_cast<Sensor const*>(sensor)->getStringType().string();
186 }
187 
ASensor_getReportingMode(ASensor const * sensor)188 int ASensor_getReportingMode(ASensor const* sensor)
189 {
190     return static_cast<Sensor const*>(sensor)->getReportingMode();
191 }
192 
ASensor_isWakeUpSensor(ASensor const * sensor)193 bool ASensor_isWakeUpSensor(ASensor const* sensor)
194 {
195     return static_cast<Sensor const*>(sensor)->isWakeUpSensor();
196 }