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 /*****************************************************************************/
41 
42 android::Mutex android::SensorManager::sLock;
43 std::map<String16, SensorManager*> android::SensorManager::sPackageInstances;
44 
ASensorManager_getInstance()45 ASensorManager* ASensorManager_getInstance()
46 {
47     return ASensorManager_getInstanceForPackage(NULL);
48 }
49 
ASensorManager_getInstanceForPackage(const char * packageName)50 ASensorManager* 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 
ASensorManager_getSensorList(ASensorManager * manager,ASensorList * list)59 int ASensorManager_getSensorList(ASensorManager* manager,
60         ASensorList* list)
61 {
62     Sensor const* const* l;
63     int c = static_cast<SensorManager*>(manager)->getSensorList(&l);
64     if (list) {
65         *list = reinterpret_cast<ASensorList>(l);
66     }
67     return c;
68 }
69 
ASensorManager_getDefaultSensor(ASensorManager * manager,int type)70 ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type)
71 {
72     return static_cast<SensorManager*>(manager)->getDefaultSensor(type);
73 }
74 
ASensorManager_getDefaultSensorEx(ASensorManager * manager,int type,bool wakeUp)75 ASensor 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 
ASensorManager_createEventQueue(ASensorManager * manager,ALooper * looper,int ident,ALooper_callbackFunc callback,void * data)88 ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
89         ALooper* looper, int ident, ALooper_callbackFunc callback, void* data)
90 {
91     sp<SensorEventQueue> queue =
92             static_cast<SensorManager*>(manager)->createEventQueue();
93     if (queue != 0) {
94         ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data);
95         queue->looper = looper;
96         queue->incStrong(manager);
97     }
98     return static_cast<ASensorEventQueue*>(queue.get());
99 }
100 
ASensorManager_destroyEventQueue(ASensorManager * manager,ASensorEventQueue * inQueue)101 int 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 
ASensorEventQueue_enableSensor(ASensorEventQueue * queue,ASensor const * sensor)112 int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor)
113 {
114     return static_cast<SensorEventQueue*>(queue)->enableSensor(
115             static_cast<Sensor const*>(sensor));
116 }
117 
ASensorEventQueue_disableSensor(ASensorEventQueue * queue,ASensor const * sensor)118 int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor)
119 {
120     return static_cast<SensorEventQueue*>(queue)->disableSensor(
121             static_cast<Sensor const*>(sensor));
122 }
123 
ASensorEventQueue_setEventRate(ASensorEventQueue * queue,ASensor const * sensor,int32_t usec)124 int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor,
125         int32_t usec)
126 {
127     return static_cast<SensorEventQueue*>(queue)->setEventRate(
128             static_cast<Sensor const*>(sensor), us2ns(usec));
129 }
130 
ASensorEventQueue_hasEvents(ASensorEventQueue * queue)131 int 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 
ASensorEventQueue_getEvents(ASensorEventQueue * queue,ASensorEvent * events,size_t count)149 ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
150                 ASensorEvent* events, size_t count)
151 {
152     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;
157 }
158 
159 /*****************************************************************************/
160 
ASensor_getName(ASensor const * sensor)161 const char* ASensor_getName(ASensor const* sensor)
162 {
163     return static_cast<Sensor const*>(sensor)->getName().string();
164 }
165 
ASensor_getVendor(ASensor const * sensor)166 const char* ASensor_getVendor(ASensor const* sensor)
167 {
168     return static_cast<Sensor const*>(sensor)->getVendor().string();
169 }
170 
ASensor_getType(ASensor const * sensor)171 int ASensor_getType(ASensor const* sensor)
172 {
173     return static_cast<Sensor const*>(sensor)->getType();
174 }
175 
ASensor_getResolution(ASensor const * sensor)176 float ASensor_getResolution(ASensor const* sensor)
177 {
178     return static_cast<Sensor const*>(sensor)->getResolution();
179 }
180 
ASensor_getMinDelay(ASensor const * sensor)181 int ASensor_getMinDelay(ASensor const* sensor)
182 {
183     return static_cast<Sensor const*>(sensor)->getMinDelay();
184 }
185 
ASensor_getFifoMaxEventCount(ASensor const * sensor)186 int ASensor_getFifoMaxEventCount(ASensor const* sensor)
187 {
188     return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount();
189 }
190 
ASensor_getFifoReservedEventCount(ASensor const * sensor)191 int ASensor_getFifoReservedEventCount(ASensor const* sensor)
192 {
193     return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount();
194 }
195 
ASensor_getStringType(ASensor const * sensor)196 const char* ASensor_getStringType(ASensor const* sensor)
197 {
198     return static_cast<Sensor const*>(sensor)->getStringType().string();
199 }
200 
ASensor_getReportingMode(ASensor const * sensor)201 int ASensor_getReportingMode(ASensor const* sensor)
202 {
203     return static_cast<Sensor const*>(sensor)->getReportingMode();
204 }
205 
ASensor_isWakeUpSensor(ASensor const * sensor)206 bool ASensor_isWakeUpSensor(ASensor const* sensor)
207 {
208     return static_cast<Sensor const*>(sensor)->isWakeUpSensor();
209 }