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