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 #include <android/sharedmem.h>
23 #include <cutils/native_handle.h>
24 #include <sensor/Sensor.h>
25 #include <sensor/SensorManager.h>
26 #include <sensor/SensorEventQueue.h>
27 #include <utils/Looper.h>
28 #include <utils/RefBase.h>
29 #include <utils/Timers.h>
30 #include <vndk/hardware_buffer.h>
31 
32 #include <poll.h>
33 
34 using android::sp;
35 using android::Sensor;
36 using android::SensorManager;
37 using android::SensorEventQueue;
38 using android::String8;
39 using android::String16;
40 
41 /*****************************************************************************/
42 #define ERROR_INVALID_PARAMETER(message) ALOGE("%s: " message, __func__)
43 
44 // frequently used checks
45 #define RETURN_IF_MANAGER_IS_NULL(retval) do {\
46         if (manager == nullptr) { \
47             ERROR_INVALID_PARAMETER("manager cannot be NULL"); \
48             return retval; \
49         } \
50     } while (false)
51 #define RETURN_IF_SENSOR_IS_NULL(retval) do {\
52         if (sensor == nullptr) { \
53             ERROR_INVALID_PARAMETER("sensor cannot be NULL"); \
54             return retval; \
55         } \
56     } while (false)
57 #define RETURN_IF_QUEUE_IS_NULL(retval) do {\
58         if (queue == nullptr) { \
59             ERROR_INVALID_PARAMETER("queue cannot be NULL"); \
60             return retval; \
61         } \
62     } while (false)
63 
ASensorManager_getInstance()64 ASensorManager* ASensorManager_getInstance() {
65     return ASensorManager_getInstanceForPackage(nullptr);
66 }
67 
ASensorManager_getInstanceForPackage(const char * packageName)68 ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName) {
69     if (packageName) {
70         return &SensorManager::getInstanceForPackage(String16(packageName));
71     } else {
72         return &SensorManager::getInstanceForPackage(String16());
73     }
74 }
75 
ASensorManager_getSensorList(ASensorManager * manager,ASensorList * list)76 int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list) {
77     RETURN_IF_MANAGER_IS_NULL(android::BAD_VALUE);
78     Sensor const* const* l;
79     int c = static_cast<SensorManager*>(manager)->getSensorList(&l);
80     if (list) {
81         *list = reinterpret_cast<ASensorList>(l);
82     }
83     return c;
84 }
85 
ASensorManager_getDynamicSensorList(ASensorManager * manager,ASensorList * list)86 ssize_t ASensorManager_getDynamicSensorList(ASensorManager* manager, ASensorList* list) {
87     RETURN_IF_MANAGER_IS_NULL(android::BAD_VALUE);
88     Sensor const* const* l;
89     ssize_t c = static_cast<SensorManager*>(manager)->getDynamicSensorList(&l);
90     if (list) {
91         *list = reinterpret_cast<ASensorList>(l);
92     }
93     return c;
94 }
95 
ASensorManager_getDefaultSensor(ASensorManager * manager,int type)96 ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type) {
97     RETURN_IF_MANAGER_IS_NULL(nullptr);
98     return static_cast<SensorManager*>(manager)->getDefaultSensor(type);
99 }
100 
ASensorManager_getDefaultSensorEx(ASensorManager * manager,int type,bool wakeUp)101 ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type, bool wakeUp) {
102     RETURN_IF_MANAGER_IS_NULL(nullptr);
103     Sensor const* const* sensorList;
104     size_t size = static_cast<SensorManager*>(manager)->getSensorList(&sensorList);
105     for (size_t i = 0; i < size; ++i) {
106         if (ASensor_getType(sensorList[i]) == type &&
107             ASensor_isWakeUpSensor(sensorList[i]) == wakeUp) {
108             return reinterpret_cast<ASensor const *>(sensorList[i]);
109        }
110     }
111     return nullptr;
112 }
113 
ASensorManager_createEventQueue(ASensorManager * manager,ALooper * looper,int ident,ALooper_callbackFunc callback,void * data)114 ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
115         ALooper* looper, int ident, ALooper_callbackFunc callback, void* data) {
116     RETURN_IF_MANAGER_IS_NULL(nullptr);
117 
118     if (looper == nullptr) {
119         ERROR_INVALID_PARAMETER("looper cannot be NULL");
120         return nullptr;
121     }
122 
123     sp<SensorEventQueue> queue =
124             static_cast<SensorManager*>(manager)->createEventQueue();
125     if (queue != 0) {
126         ALooper_addFd(looper, queue->getFd(), ident, ALOOPER_EVENT_INPUT, callback, data);
127         queue->looper = looper;
128         queue->requestAdditionalInfo = false;
129         queue->incStrong(manager);
130     }
131     return static_cast<ASensorEventQueue*>(queue.get());
132 }
133 
ASensorManager_destroyEventQueue(ASensorManager * manager,ASensorEventQueue * queue)134 int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue) {
135     RETURN_IF_MANAGER_IS_NULL(android::BAD_VALUE);
136     RETURN_IF_QUEUE_IS_NULL(android::BAD_VALUE);
137 
138     sp<SensorEventQueue> q = static_cast<SensorEventQueue*>(queue);
139     ALooper_removeFd(q->looper, q->getFd());
140     q->decStrong(manager);
141     return 0;
142 }
143 
ASensorManager_createSharedMemoryDirectChannel(ASensorManager * manager,int fd,size_t size)144 int ASensorManager_createSharedMemoryDirectChannel(ASensorManager *manager, int fd, size_t size) {
145     RETURN_IF_MANAGER_IS_NULL(android::BAD_VALUE);
146 
147     if (fd < 0) {
148         ERROR_INVALID_PARAMETER("fd is invalid.");
149         return android::BAD_VALUE;
150     }
151 
152     if (size < sizeof(ASensorEvent)) {
153         ERROR_INVALID_PARAMETER("size has to be greater or equal to sizeof(ASensorEvent).");
154         return android::BAD_VALUE;
155     }
156 
157     native_handle_t *resourceHandle = native_handle_create(1 /* nFd */, 0 /* nInt */);
158     if (!resourceHandle) {
159         return android::NO_MEMORY;
160     }
161 
162     resourceHandle->data[0] = fd;
163     int ret = static_cast<SensorManager *>(manager)->createDirectChannel(
164             size, ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY, resourceHandle);
165     native_handle_delete(resourceHandle);
166     return ret;
167 }
168 
ASensorManager_createHardwareBufferDirectChannel(ASensorManager * manager,AHardwareBuffer const * buffer,size_t size)169 int ASensorManager_createHardwareBufferDirectChannel(
170         ASensorManager *manager, AHardwareBuffer const *buffer, size_t size) {
171     RETURN_IF_MANAGER_IS_NULL(android::BAD_VALUE);
172 
173     if (buffer == nullptr) {
174         ERROR_INVALID_PARAMETER("buffer cannot be NULL");
175         return android::BAD_VALUE;
176     }
177 
178     if (size < sizeof(ASensorEvent)) {
179         ERROR_INVALID_PARAMETER("size has to be greater or equal to sizeof(ASensorEvent).");
180         return android::BAD_VALUE;
181     }
182 
183     const native_handle_t *resourceHandle = AHardwareBuffer_getNativeHandle(buffer);
184     if (!resourceHandle) {
185         return android::NO_MEMORY;
186     }
187 
188     return static_cast<SensorManager *>(manager)->createDirectChannel(
189             size, ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER, resourceHandle);
190 }
191 
ASensorManager_destroyDirectChannel(ASensorManager * manager,int channelId)192 void ASensorManager_destroyDirectChannel(ASensorManager *manager, int channelId) {
193     RETURN_IF_MANAGER_IS_NULL(void());
194 
195     static_cast<SensorManager *>(manager)->destroyDirectChannel(channelId);
196 }
197 
ASensorManager_configureDirectReport(ASensorManager * manager,ASensor const * sensor,int channelId,int rate)198 int ASensorManager_configureDirectReport(
199         ASensorManager *manager, ASensor const *sensor, int channelId, int rate) {
200     RETURN_IF_MANAGER_IS_NULL(android::BAD_VALUE);
201 
202     int sensorHandle;
203     if (sensor == nullptr) {
204         if (rate != ASENSOR_DIRECT_RATE_STOP) {
205             ERROR_INVALID_PARAMETER(
206                 "sensor cannot be null when rate is not ASENSOR_DIRECT_RATE_STOP");
207             return android::BAD_VALUE;
208         }
209         sensorHandle = -1;
210     } else {
211         sensorHandle = static_cast<Sensor const *>(sensor)->getHandle();
212     }
213     return static_cast<SensorManager *>(manager)->configureDirectChannel(
214             channelId, sensorHandle, rate);
215 }
216 
217 /*****************************************************************************/
218 
ASensorEventQueue_registerSensor(ASensorEventQueue * queue,ASensor const * sensor,int32_t samplingPeriodUs,int64_t maxBatchReportLatencyUs)219 int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor,
220         int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs) {
221     RETURN_IF_QUEUE_IS_NULL(android::BAD_VALUE);
222     RETURN_IF_SENSOR_IS_NULL(android::BAD_VALUE);
223     if (samplingPeriodUs < 0 || maxBatchReportLatencyUs < 0) {
224         ERROR_INVALID_PARAMETER("samplingPeriodUs and maxBatchReportLatencyUs cannot be negative");
225         return android::BAD_VALUE;
226     }
227 
228     return static_cast<SensorEventQueue*>(queue)->enableSensor(
229             static_cast<Sensor const*>(sensor)->getHandle(), samplingPeriodUs,
230                     maxBatchReportLatencyUs, 0);
231 }
232 
ASensorEventQueue_enableSensor(ASensorEventQueue * queue,ASensor const * sensor)233 int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor) {
234     RETURN_IF_QUEUE_IS_NULL(android::BAD_VALUE);
235     RETURN_IF_SENSOR_IS_NULL(android::BAD_VALUE);
236 
237     return static_cast<SensorEventQueue*>(queue)->enableSensor(
238             static_cast<Sensor const*>(sensor));
239 }
240 
ASensorEventQueue_disableSensor(ASensorEventQueue * queue,ASensor const * sensor)241 int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor) {
242     RETURN_IF_QUEUE_IS_NULL(android::BAD_VALUE);
243     RETURN_IF_SENSOR_IS_NULL(android::BAD_VALUE);
244 
245     return static_cast<SensorEventQueue*>(queue)->disableSensor(
246             static_cast<Sensor const*>(sensor));
247 }
248 
ASensorEventQueue_setEventRate(ASensorEventQueue * queue,ASensor const * sensor,int32_t usec)249 int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec) {
250     RETURN_IF_QUEUE_IS_NULL(android::BAD_VALUE);
251     RETURN_IF_SENSOR_IS_NULL(android::BAD_VALUE);
252 
253     if (usec < 0) {
254         ERROR_INVALID_PARAMETER("usec cannot be negative");
255         return android::BAD_VALUE;
256     }
257 
258     return static_cast<SensorEventQueue*>(queue)->setEventRate(
259             static_cast<Sensor const*>(sensor), us2ns(usec));
260 }
261 
ASensorEventQueue_hasEvents(ASensorEventQueue * queue)262 int ASensorEventQueue_hasEvents(ASensorEventQueue* queue) {
263     RETURN_IF_QUEUE_IS_NULL(android::BAD_VALUE);
264 
265     struct pollfd pfd;
266     pfd.fd = static_cast<SensorEventQueue*>(queue)->getFd();
267     pfd.events = POLLIN;
268     pfd.revents = 0;
269 
270     int nfd = poll(&pfd, 1, 0);
271 
272     if (nfd < 0)
273         return -errno;
274 
275     if (pfd.revents != POLLIN)
276         return -1;
277 
278     return (nfd == 0) ? 0 : 1;
279 }
280 
ASensorEventQueue_getEvents(ASensorEventQueue * queue,ASensorEvent * events,size_t count)281 ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, ASensorEvent* events, size_t count) {
282     RETURN_IF_QUEUE_IS_NULL(android::BAD_VALUE);
283     if (events == nullptr) {
284         ERROR_INVALID_PARAMETER("events cannot be NULL");
285         return android::BAD_VALUE;
286     }
287 
288     SensorEventQueue* sensorQueue = static_cast<SensorEventQueue*>(queue);
289     ssize_t actual = sensorQueue->read(events, count);
290     if (actual > 0) {
291         sensorQueue->sendAck(events, actual);
292     }
293 
294     return sensorQueue->filterEvents(events, actual);
295 }
296 
ASensorEventQueue_requestAdditionalInfoEvents(ASensorEventQueue * queue,bool enable)297 int ASensorEventQueue_requestAdditionalInfoEvents(ASensorEventQueue* queue, bool enable) {
298     RETURN_IF_QUEUE_IS_NULL(android::BAD_VALUE);
299     queue->requestAdditionalInfo = enable;
300     return android::OK;
301 }
302 
303 /*****************************************************************************/
304 
ASensor_getName(ASensor const * sensor)305 const char* ASensor_getName(ASensor const* sensor) {
306     RETURN_IF_SENSOR_IS_NULL(nullptr);
307     return static_cast<Sensor const*>(sensor)->getName().c_str();
308 }
309 
ASensor_getVendor(ASensor const * sensor)310 const char* ASensor_getVendor(ASensor const* sensor) {
311     RETURN_IF_SENSOR_IS_NULL(nullptr);
312     return static_cast<Sensor const*>(sensor)->getVendor().c_str();
313 }
314 
ASensor_getType(ASensor const * sensor)315 int ASensor_getType(ASensor const* sensor) {
316     RETURN_IF_SENSOR_IS_NULL(ASENSOR_TYPE_INVALID);
317     return static_cast<Sensor const*>(sensor)->getType();
318 }
319 
ASensor_getResolution(ASensor const * sensor)320 float ASensor_getResolution(ASensor const* sensor) {
321     RETURN_IF_SENSOR_IS_NULL(ASENSOR_RESOLUTION_INVALID);
322     return static_cast<Sensor const*>(sensor)->getResolution();
323 }
324 
ASensor_getMinDelay(ASensor const * sensor)325 int ASensor_getMinDelay(ASensor const* sensor) {
326     RETURN_IF_SENSOR_IS_NULL(ASENSOR_DELAY_INVALID);
327     return static_cast<Sensor const*>(sensor)->getMinDelay();
328 }
329 
ASensor_getFifoMaxEventCount(ASensor const * sensor)330 int ASensor_getFifoMaxEventCount(ASensor const* sensor) {
331     RETURN_IF_SENSOR_IS_NULL(ASENSOR_FIFO_COUNT_INVALID);
332     return static_cast<Sensor const*>(sensor)->getFifoMaxEventCount();
333 }
334 
ASensor_getFifoReservedEventCount(ASensor const * sensor)335 int ASensor_getFifoReservedEventCount(ASensor const* sensor) {
336     RETURN_IF_SENSOR_IS_NULL(ASENSOR_FIFO_COUNT_INVALID);
337     return static_cast<Sensor const*>(sensor)->getFifoReservedEventCount();
338 }
339 
ASensor_getStringType(ASensor const * sensor)340 const char* ASensor_getStringType(ASensor const* sensor) {
341     RETURN_IF_SENSOR_IS_NULL(nullptr);
342     return static_cast<Sensor const*>(sensor)->getStringType().c_str();
343 }
344 
ASensor_getReportingMode(ASensor const * sensor)345 int ASensor_getReportingMode(ASensor const* sensor) {
346     RETURN_IF_SENSOR_IS_NULL(AREPORTING_MODE_INVALID);
347     return static_cast<Sensor const*>(sensor)->getReportingMode();
348 }
349 
ASensor_isWakeUpSensor(ASensor const * sensor)350 bool ASensor_isWakeUpSensor(ASensor const* sensor) {
351     RETURN_IF_SENSOR_IS_NULL(false);
352     return static_cast<Sensor const*>(sensor)->isWakeUpSensor();
353 }
354 
ASensor_isDirectChannelTypeSupported(ASensor const * sensor,int channelType)355 bool ASensor_isDirectChannelTypeSupported(ASensor const *sensor, int channelType) {
356     RETURN_IF_SENSOR_IS_NULL(false);
357     return static_cast<Sensor const *>(sensor)->isDirectChannelTypeSupported(channelType);
358 }
359 
ASensor_getHighestDirectReportRateLevel(ASensor const * sensor)360 int ASensor_getHighestDirectReportRateLevel(ASensor const *sensor) {
361     RETURN_IF_SENSOR_IS_NULL(ASENSOR_DIRECT_RATE_STOP);
362     return static_cast<Sensor const *>(sensor)->getHighestDirectReportRateLevel();
363 }
364 
ASensor_getHandle(ASensor const * sensor)365 int ASensor_getHandle(ASensor const* sensor) {
366     RETURN_IF_SENSOR_IS_NULL(ASENSOR_INVALID);
367     return static_cast<Sensor const*>(sensor)->getHandle();
368 }
369