• 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 using android::String16;
39 
40 /*****************************************************************************/
ASensorManager_getInstance()41 ASensorManager* ASensorManager_getInstance()
42 {
43     return ASensorManager_getInstanceForPackage(NULL);
44 }
45 
ASensorManager_getInstanceForPackage(const char * packageName)46 ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName)
47 {
48     if (packageName) {
49         return &SensorManager::getInstanceForPackage(String16(packageName));
50     } else {
51         return &SensorManager::getInstanceForPackage(String16());
52     }
53 }
54 
ASensorManager_getSensorList(ASensorManager * manager,ASensorList * list)55 int ASensorManager_getSensorList(ASensorManager* manager,
56         ASensorList* list)
57 {
58     Sensor const* const* l;
59     int c = static_cast<SensorManager*>(manager)->getSensorList(&l);
60     if (list) {
61         *list = reinterpret_cast<ASensorList>(l);
62     }
63     return c;
64 }
65 
ASensorManager_getDefaultSensor(ASensorManager * manager,int type)66 ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type)
67 {
68     return static_cast<SensorManager*>(manager)->getDefaultSensor(type);
69 }
70 
ASensorManager_getDefaultSensorEx(ASensorManager * manager,int type,bool wakeUp)71 ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager,
72         int type, bool wakeUp) {
73     Sensor const* const* sensorList;
74     size_t size = static_cast<SensorManager*>(manager)->getSensorList(&sensorList);
75     for (size_t i = 0; i < size; ++i) {
76         if (ASensor_getType(sensorList[i]) == type &&
77             ASensor_isWakeUpSensor(sensorList[i]) == wakeUp) {
78             return reinterpret_cast<ASensor const *>(sensorList[i]);
79        }
80     }
81     return NULL;
82 }
83 
ASensorManager_createEventQueue(ASensorManager * manager,ALooper * looper,int ident,ALooper_callbackFunc callback,void * data)84 ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
85         ALooper* looper, int ident, ALooper_callbackFunc callback, void* data)
86 {
87     sp<SensorEventQueue> queue =
88             static_cast<SensorManager*>(manager)->createEventQueue();
89     if (queue != 0) {
90         ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data);
91         queue->looper = looper;
92         queue->incStrong(manager);
93     }
94     return static_cast<ASensorEventQueue*>(queue.get());
95 }
96 
ASensorManager_destroyEventQueue(ASensorManager * manager,ASensorEventQueue * inQueue)97 int ASensorManager_destroyEventQueue(ASensorManager* manager,
98         ASensorEventQueue* inQueue)
99 {
100     sp<SensorEventQueue> queue = static_cast<SensorEventQueue*>(inQueue);
101     ALooper_removeFd(queue->looper, queue->getFd());
102     queue->decStrong(manager);
103     return 0;
104 }
105 
106 /*****************************************************************************/
107 
ASensorEventQueue_registerSensor(ASensorEventQueue * queue,ASensor const * sensor,int32_t samplingPeriodUs,int maxBatchReportLatencyUs)108 int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor,
109         int32_t samplingPeriodUs, int maxBatchReportLatencyUs)
110 {
111     return static_cast<SensorEventQueue*>(queue)->enableSensor(
112             static_cast<Sensor const*>(sensor)->getHandle(), samplingPeriodUs,
113                     maxBatchReportLatencyUs, 0);
114 }
115 
ASensorEventQueue_enableSensor(ASensorEventQueue * queue,ASensor const * sensor)116 int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor)
117 {
118     return static_cast<SensorEventQueue*>(queue)->enableSensor(
119             static_cast<Sensor const*>(sensor));
120 }
121 
ASensorEventQueue_disableSensor(ASensorEventQueue * queue,ASensor const * sensor)122 int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor)
123 {
124     return static_cast<SensorEventQueue*>(queue)->disableSensor(
125             static_cast<Sensor const*>(sensor));
126 }
127 
ASensorEventQueue_setEventRate(ASensorEventQueue * queue,ASensor const * sensor,int32_t usec)128 int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor,
129         int32_t usec)
130 {
131     return static_cast<SensorEventQueue*>(queue)->setEventRate(
132             static_cast<Sensor const*>(sensor), us2ns(usec));
133 }
134 
ASensorEventQueue_hasEvents(ASensorEventQueue * queue)135 int ASensorEventQueue_hasEvents(ASensorEventQueue* queue)
136 {
137     struct pollfd pfd;
138     pfd.fd = static_cast<SensorEventQueue*>(queue)->getFd();
139     pfd.events = POLLIN;
140     pfd.revents = 0;
141 
142     int nfd = poll(&pfd, 1, 0);
143 
144     if (nfd < 0)
145         return -errno;
146 
147     if (pfd.revents != POLLIN)
148         return -1;
149 
150     return (nfd == 0) ? 0 : 1;
151 }
152 
ASensorEventQueue_getEvents(ASensorEventQueue * queue,ASensorEvent * events,size_t count)153 ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
154                 ASensorEvent* events, size_t count)
155 {
156     ssize_t actual = static_cast<SensorEventQueue*>(queue)->read(events, count);
157     if (actual > 0) {
158         static_cast<SensorEventQueue*>(queue)->sendAck(events, actual);
159     }
160     return actual;
161 }
162 
163 /*****************************************************************************/
164 
ASensor_getName(ASensor const * sensor)165 const char* ASensor_getName(ASensor const* sensor)
166 {
167     return static_cast<Sensor const*>(sensor)->getName().string();
168 }
169 
ASensor_getVendor(ASensor const * sensor)170 const char* ASensor_getVendor(ASensor const* sensor)
171 {
172     return static_cast<Sensor const*>(sensor)->getVendor().string();
173 }
174 
ASensor_getType(ASensor const * sensor)175 int ASensor_getType(ASensor const* sensor)
176 {
177     return static_cast<Sensor const*>(sensor)->getType();
178 }
179 
ASensor_getResolution(ASensor const * sensor)180 float ASensor_getResolution(ASensor const* sensor)
181 {
182     return static_cast<Sensor const*>(sensor)->getResolution();
183 }
184 
ASensor_getMinDelay(ASensor const * sensor)185 int ASensor_getMinDelay(ASensor const* sensor)
186 {
187     return static_cast<Sensor const*>(sensor)->getMinDelay();
188 }
189 
ASensor_getFifoMaxEventCount(ASensor const * sensor)190 int ASensor_getFifoMaxEventCount(ASensor const* sensor)
191 {
192     return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount();
193 }
194 
ASensor_getFifoReservedEventCount(ASensor const * sensor)195 int ASensor_getFifoReservedEventCount(ASensor const* sensor)
196 {
197     return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount();
198 }
199 
ASensor_getStringType(ASensor const * sensor)200 const char* ASensor_getStringType(ASensor const* sensor)
201 {
202     return static_cast<Sensor const*>(sensor)->getStringType().string();
203 }
204 
ASensor_getReportingMode(ASensor const * sensor)205 int ASensor_getReportingMode(ASensor const* sensor)
206 {
207     return static_cast<Sensor const*>(sensor)->getReportingMode();
208 }
209 
ASensor_isWakeUpSensor(ASensor const * sensor)210 bool ASensor_isWakeUpSensor(ASensor const* sensor)
211 {
212     return static_cast<Sensor const*>(sensor)->isWakeUpSensor();
213 }
214