blob: 5b79d3c7bacd04af375889f9938c6d698877c3b8 [file] [log] [blame]
Mathias Agopiane1c61d32012-03-23 14:19:36 -07001/*
2 * Copyright (C) 2010 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
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070017/**
18 * @addtogroup Sensor
19 * @{
20 */
21
22/**
23 * @file sensor.h
24 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070025
26#ifndef ANDROID_SENSOR_H
27#define ANDROID_SENSOR_H
28
29/******************************************************************
30 *
31 * IMPORTANT NOTICE:
32 *
33 * This file is part of Android's set of stable system headers
34 * exposed by the Android NDK (Native Development Kit).
35 *
36 * Third-party source AND binary code relies on the definitions
37 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
38 *
39 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
40 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
41 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
42 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
43 */
44
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070045/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -070046 * Structures and functions to receive and process sensor events in
47 * native code.
48 *
49 */
50
51#include <sys/types.h>
52
53#include <android/looper.h>
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070060/**
61 * Sensor types.
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070062 * (keep in sync with hardware/sensors.h)
Mathias Agopiane1c61d32012-03-23 14:19:36 -070063 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070064enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070065 /**
66 * {@link ASENSOR_TYPE_ACCELEROMETER}
67 * reporting-mode: continuous
68 *
69 * All values are in SI units (m/s^2) and measure the acceleration of the
70 * device minus the force of gravity.
71 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070072 ASENSOR_TYPE_ACCELEROMETER = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070073 /**
74 * {@link ASENSOR_TYPE_MAGNETIC_FIELD}
75 * reporting-mode: continuous
76 *
77 * All values are in micro-Tesla (uT) and measure the geomagnetic
78 * field in the X, Y and Z axis.
79 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070080 ASENSOR_TYPE_MAGNETIC_FIELD = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070081 /**
82 * {@link ASENSOR_TYPE_GYROSCOPE}
83 * reporting-mode: continuous
84 *
85 * All values are in radians/second and measure the rate of rotation
86 * around the X, Y and Z axis.
87 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070088 ASENSOR_TYPE_GYROSCOPE = 4,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070089 /**
90 * {@link ASENSOR_TYPE_LIGHT}
91 * reporting-mode: on-change
92 *
93 * The light sensor value is returned in SI lux units.
94 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070095 ASENSOR_TYPE_LIGHT = 5,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070096 /**
97 * {@link ASENSOR_TYPE_PROXIMITY}
98 * reporting-mode: on-change
99 *
100 * The proximity sensor which turns the screen off and back on during calls is the
101 * wake-up proximity sensor. Implement wake-up proximity sensor before implementing
102 * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag
103 * SENSOR_FLAG_WAKE_UP.
104 * The value corresponds to the distance to the nearest object in centimeters.
105 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700106 ASENSOR_TYPE_PROXIMITY = 8,
107 /**
108 * {@link ASENSOR_TYPE_LINEAR_ACCELERATION}
109 * reporting-mode: continuous
110 *
111 * All values are in SI units (m/s^2) and measure the acceleration of the
112 * device not including the force of gravity.
113 */
114 ASENSOR_TYPE_LINEAR_ACCELERATION = 10
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700115};
116
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700117/**
118 * Sensor accuracy measure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700119 */
120enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700121 /** no contact */
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700122 ASENSOR_STATUS_NO_CONTACT = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700123 /** unreliable */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700124 ASENSOR_STATUS_UNRELIABLE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700125 /** low accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700126 ASENSOR_STATUS_ACCURACY_LOW = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700127 /** medium accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700128 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700129 /** high accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700130 ASENSOR_STATUS_ACCURACY_HIGH = 3
131};
132
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700133/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700134 * Sensor Reporting Modes.
135 */
136enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700137 /** continuous reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700138 AREPORTING_MODE_CONTINUOUS = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700139 /** reporting on change */
Aravind Akella0e025c52014-06-03 19:19:57 -0700140 AREPORTING_MODE_ON_CHANGE = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700141 /** on shot reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700142 AREPORTING_MODE_ONE_SHOT = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700143 /** special trigger reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700144 AREPORTING_MODE_SPECIAL_TRIGGER = 3
145};
146
147/*
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700148 * A few useful constants
149 */
150
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700151/** Earth's gravity in m/s^2 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700152#define ASENSOR_STANDARD_GRAVITY (9.80665f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700153/** Maximum magnetic field on Earth's surface in uT */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700154#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700155/** Minimum magnetic field on Earth's surface in uT*/
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700156#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
157
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700158/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700159 * A sensor event.
160 */
161
162/* NOTE: Must match hardware/sensors.h */
163typedef struct ASensorVector {
164 union {
165 float v[3];
166 struct {
167 float x;
168 float y;
169 float z;
170 };
171 struct {
172 float azimuth;
173 float pitch;
174 float roll;
175 };
176 };
177 int8_t status;
178 uint8_t reserved[3];
179} ASensorVector;
180
Aravind Akella724d91d2013-06-27 12:04:23 -0700181typedef struct AMetaDataEvent {
182 int32_t what;
183 int32_t sensor;
184} AMetaDataEvent;
185
186typedef struct AUncalibratedEvent {
187 union {
188 float uncalib[3];
189 struct {
190 float x_uncalib;
191 float y_uncalib;
192 float z_uncalib;
193 };
194 };
195 union {
196 float bias[3];
197 struct {
198 float x_bias;
199 float y_bias;
200 float z_bias;
201 };
202 };
203} AUncalibratedEvent;
204
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700205typedef struct AHeartRateEvent {
206 float bpm;
207 int8_t status;
208} AHeartRateEvent;
209
Peng Xu2576cb62016-01-20 00:22:09 -0800210typedef struct ADynamicSensorEvent {
211 bool connected;
212 int handle;
213} ADynamicSensorEvent;
214
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700215/* NOTE: Must match hardware/sensors.h */
216typedef struct ASensorEvent {
217 int32_t version; /* sizeof(struct ASensorEvent) */
218 int32_t sensor;
219 int32_t type;
220 int32_t reserved0;
221 int64_t timestamp;
222 union {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700223 union {
224 float data[16];
225 ASensorVector vector;
226 ASensorVector acceleration;
227 ASensorVector magnetic;
228 float temperature;
229 float distance;
230 float light;
231 float pressure;
Aravind Akella724d91d2013-06-27 12:04:23 -0700232 float relative_humidity;
233 AUncalibratedEvent uncalibrated_gyro;
234 AUncalibratedEvent uncalibrated_magnetic;
235 AMetaDataEvent meta_data;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700236 AHeartRateEvent heart_rate;
Peng Xu2576cb62016-01-20 00:22:09 -0800237 ADynamicSensorEvent dynamic_sensor_meta;
Mathias Agopianba02cd22013-07-03 16:20:57 -0700238 };
239 union {
240 uint64_t data[8];
241 uint64_t step_counter;
242 } u64;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700243 };
Aravind Akella9a844cf2014-02-11 18:58:52 -0800244
245 uint32_t flags;
246 int32_t reserved1[3];
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700247} ASensorEvent;
248
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700249struct ASensorManager;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700250/**
251 * {@link ASensorManager} is an opaque type to manage sensors and
252 * events queues.
253 *
254 * {@link ASensorManager} is a singleton that can be obtained using
255 * ASensorManager_getInstance().
256 *
257 * This file provides a set of functions that uses {@link
258 * ASensorManager} to access and list hardware sensors, and
259 * create and destroy event queues:
260 * - ASensorManager_getSensorList()
261 * - ASensorManager_getDefaultSensor()
262 * - ASensorManager_getDefaultSensorEx()
263 * - ASensorManager_createEventQueue()
264 * - ASensorManager_destroyEventQueue()
265 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700266typedef struct ASensorManager ASensorManager;
267
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700268
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700269struct ASensorEventQueue;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700270/**
271 * {@link ASensorEventQueue} is an opaque type that provides access to
272 * {@link ASensorEvent} from hardware sensors.
273 *
274 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue().
275 *
276 * This file provides a set of functions to enable and disable
277 * sensors, check and get events, and set event rates on a {@link
278 * ASensorEventQueue}.
279 * - ASensorEventQueue_enableSensor()
280 * - ASensorEventQueue_disableSensor()
281 * - ASensorEventQueue_hasEvents()
282 * - ASensorEventQueue_getEvents()
283 * - ASensorEventQueue_setEventRate()
284 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700285typedef struct ASensorEventQueue ASensorEventQueue;
286
287struct ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700288/**
289 * {@link ASensor} is an opaque type that provides information about
290 * an hardware sensors.
291 *
292 * A {@link ASensor} pointer can be obtained using
293 * ASensorManager_getDefaultSensor(),
294 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}.
295 *
296 * This file provides a set of functions to access properties of a
297 * {@link ASensor}:
298 * - ASensor_getName()
299 * - ASensor_getVendor()
300 * - ASensor_getType()
301 * - ASensor_getResolution()
302 * - ASensor_getMinDelay()
303 * - ASensor_getFifoMaxEventCount()
304 * - ASensor_getFifoReservedEventCount()
305 * - ASensor_getStringType()
306 * - ASensor_getReportingMode()
307 * - ASensor_isWakeUpSensor()
308 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700309typedef struct ASensor ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700310/**
311 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}.
312 *
313 * This is used to define entry in {@link ASensorList} arrays.
314 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700315typedef ASensor const* ASensorRef;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700316/**
317 * {@link ASensorList} is an array of reference to {@link ASensor}.
318 *
319 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList().
320 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700321typedef ASensorRef const* ASensorList;
322
323/*****************************************************************************/
324
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700325/**
Svet Ganov5fa32d42015-05-07 10:50:59 -0700326 * Get a reference to the sensor manager. ASensorManager is a singleton
327 * per package as different packages may have access to different sensors.
328 *
329 * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead.
330 *
331 * Example:
332 *
333 * ASensorManager* sensorManager = ASensorManager_getInstance();
334 *
335 */
336__attribute__ ((deprecated)) ASensorManager* ASensorManager_getInstance();
337
338/*
339 * Get a reference to the sensor manager. ASensorManager is a singleton
340 * per package as different packages may have access to different sensors.
341 *
342 * Example:
343 *
344 * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz");
345 *
346 */
347ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName);
348
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700349/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700350 * Returns the list of available sensors.
351 */
352int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list);
353
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700354/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700355 * Returns the default sensor for the given type, or NULL if no sensor
Aravind Akellab37ba392014-08-05 14:53:07 -0700356 * of that type exists.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700357 */
358ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
359
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700360/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700361 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor
362 * of this type and wakeUp properties exists.
363 */
364ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type,
365 bool wakeUp);
366
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700367/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700368 * Creates a new sensor event queue and associate it with a looper.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700369 *
370 * "ident" is a identifier for the events that will be returned when
371 * calling ALooper_pollOnce(). The identifier must be >= 0, or
372 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700373 */
374ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
375 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
376
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700377/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700378 * Destroys the event queue and free all resources associated to it.
379 */
380int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
381
382
383/*****************************************************************************/
384
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700385/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700386 * Enable the selected sensor. Returns a negative error code on failure.
387 */
388int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor);
389
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700390/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700391 * Disable the selected sensor. Returns a negative error code on failure.
392 */
393int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor);
394
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700395/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700396 * Sets the delivery rate of events in microseconds for the given sensor.
397 * Note that this is a hint only, generally event will arrive at a higher
398 * rate. It is an error to set a rate inferior to the value returned by
399 * ASensor_getMinDelay().
400 * Returns a negative error code on failure.
401 */
402int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
403
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700404/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700405 * Returns true if there are one or more events available in the
406 * sensor queue. Returns 1 if the queue has events; 0 if
407 * it does not have events; and a negative value if there is an error.
408 */
409int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
410
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700411/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700412 * Returns the next available events from the queue. Returns a negative
413 * value if no events are available or an error has occurred, otherwise
414 * the number of events returned.
415 *
416 * Examples:
417 * ASensorEvent event;
418 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
419 *
420 * ASensorEvent eventBuffer[8];
421 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
422 *
423 */
424ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
425 ASensorEvent* events, size_t count);
426
427
428/*****************************************************************************/
429
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700430/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700431 * Returns this sensor's name (non localized)
432 */
433const char* ASensor_getName(ASensor const* sensor);
434
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700435/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700436 * Returns this sensor's vendor's name (non localized)
437 */
438const char* ASensor_getVendor(ASensor const* sensor);
439
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700440/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700441 * Return this sensor's type
442 */
443int ASensor_getType(ASensor const* sensor);
444
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700445/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700446 * Returns this sensors's resolution
447 */
448float ASensor_getResolution(ASensor const* sensor);
449
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700450/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700451 * Returns the minimum delay allowed between events in microseconds.
452 * A value of zero means that this sensor doesn't report events at a
453 * constant rate, but rather only when a new data is available.
454 */
455int ASensor_getMinDelay(ASensor const* sensor);
456
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700457/**
Aravind Akella70018042014-04-07 22:52:37 +0000458 * Returns the maximum size of batches for this sensor. Batches will often be
459 * smaller, as the hardware fifo might be used for other sensors.
460 */
461int ASensor_getFifoMaxEventCount(ASensor const* sensor);
462
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700463/**
Aravind Akella70018042014-04-07 22:52:37 +0000464 * Returns the hardware batch fifo size reserved to this sensor.
465 */
466int ASensor_getFifoReservedEventCount(ASensor const* sensor);
467
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700468/**
Aravind Akella70018042014-04-07 22:52:37 +0000469 * Returns this sensor's string type.
470 */
471const char* ASensor_getStringType(ASensor const* sensor);
472
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700473/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700474 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants.
475 */
476int ASensor_getReportingMode(ASensor const* sensor);
477
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700478/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700479 * Returns true if this is a wake up sensor, false otherwise.
480 */
481bool ASensor_isWakeUpSensor(ASensor const* sensor);
482
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700483#ifdef __cplusplus
484};
485#endif
486
487#endif // ANDROID_SENSOR_H
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700488
489/** @} */