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