blob: 97ef3b4186dd4f2d510eb2a4ea95b7acf9e7df44 [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
Mathias Agopiane1c61d32012-03-23 14:19:36 -070051#include <android/looper.h>
52
Dan Albert8f860fd2017-04-25 12:24:28 -070053#include <stdbool.h>
Peng Xuda8385c2017-02-28 20:19:47 -080054#include <sys/types.h>
55#include <math.h>
56#include <stdint.h>
57
Mathias Agopiane1c61d32012-03-23 14:19:36 -070058#ifdef __cplusplus
59extern "C" {
60#endif
61
Peng Xu47cddca2017-02-15 23:31:22 -080062typedef struct AHardwareBuffer AHardwareBuffer;
Mathias Agopiane1c61d32012-03-23 14:19:36 -070063
Peng Xuda8385c2017-02-28 20:19:47 -080064#define ASENSOR_RESOLUTION_INVALID (nanf(""))
65#define ASENSOR_FIFO_COUNT_INVALID (-1)
66#define ASENSOR_DELAY_INVALID INT32_MIN
67
Elliott Hughesf78be362018-01-23 15:33:56 -080068/* (Keep in sync with hardware/sensors-base.h and Sensor.java.) */
69
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070070/**
71 * Sensor types.
Elliott Hughesf78be362018-01-23 15:33:56 -080072 *
73 * See
74 * [android.hardware.SensorEvent#values](https://developer.android.com/reference/android/hardware/SensorEvent.html#values)
75 * for detailed explanations of the data returned for each of these types.
Mathias Agopiane1c61d32012-03-23 14:19:36 -070076 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070077enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070078 /**
Peng Xu37317b62017-03-07 17:49:31 -080079 * Invalid sensor type. Returned by {@link ASensor_getType} as error value.
80 */
81 ASENSOR_TYPE_INVALID = -1,
82 /**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070083 * {@link ASENSOR_TYPE_ACCELEROMETER}
84 * reporting-mode: continuous
85 *
86 * All values are in SI units (m/s^2) and measure the acceleration of the
87 * device minus the force of gravity.
88 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070089 ASENSOR_TYPE_ACCELEROMETER = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070090 /**
91 * {@link ASENSOR_TYPE_MAGNETIC_FIELD}
92 * reporting-mode: continuous
93 *
94 * All values are in micro-Tesla (uT) and measure the geomagnetic
95 * field in the X, Y and Z axis.
96 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070097 ASENSOR_TYPE_MAGNETIC_FIELD = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070098 /**
99 * {@link ASENSOR_TYPE_GYROSCOPE}
100 * reporting-mode: continuous
101 *
102 * All values are in radians/second and measure the rate of rotation
103 * around the X, Y and Z axis.
104 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700105 ASENSOR_TYPE_GYROSCOPE = 4,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700106 /**
107 * {@link ASENSOR_TYPE_LIGHT}
108 * reporting-mode: on-change
109 *
110 * The light sensor value is returned in SI lux units.
111 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700112 ASENSOR_TYPE_LIGHT = 5,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700113 /**
Elliott Hughesf78be362018-01-23 15:33:56 -0800114 * {@link ASENSOR_TYPE_PRESSURE}
115 *
116 * The pressure sensor value is returned in hPa (millibar).
117 */
118 ASENSOR_TYPE_PRESSURE = 6,
119 /**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700120 * {@link ASENSOR_TYPE_PROXIMITY}
121 * reporting-mode: on-change
122 *
123 * The proximity sensor which turns the screen off and back on during calls is the
124 * wake-up proximity sensor. Implement wake-up proximity sensor before implementing
125 * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag
126 * SENSOR_FLAG_WAKE_UP.
127 * The value corresponds to the distance to the nearest object in centimeters.
128 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700129 ASENSOR_TYPE_PROXIMITY = 8,
130 /**
Elliott Hughesf78be362018-01-23 15:33:56 -0800131 * {@link ASENSOR_TYPE_GRAVITY}
132 *
133 * All values are in SI units (m/s^2) and measure the direction and
134 * magnitude of gravity. When the device is at rest, the output of
135 * the gravity sensor should be identical to that of the accelerometer.
136 */
137 ASENSOR_TYPE_GRAVITY = 9,
138 /**
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700139 * {@link ASENSOR_TYPE_LINEAR_ACCELERATION}
140 * reporting-mode: continuous
141 *
142 * All values are in SI units (m/s^2) and measure the acceleration of the
143 * device not including the force of gravity.
144 */
Elliott Hughesf78be362018-01-23 15:33:56 -0800145 ASENSOR_TYPE_LINEAR_ACCELERATION = 10,
146 /**
147 * {@link ASENSOR_TYPE_ROTATION_VECTOR}
148 */
149 ASENSOR_TYPE_ROTATION_VECTOR = 11,
150 /**
151 * {@link ASENSOR_TYPE_RELATIVE_HUMIDITY}
152 *
153 * The relative humidity sensor value is returned in percent.
154 */
155 ASENSOR_TYPE_RELATIVE_HUMIDITY = 12,
156 /**
157 * {@link ASENSOR_TYPE_AMBIENT_TEMPERATURE}
158 *
159 * The ambient temperature sensor value is returned in Celcius.
160 */
161 ASENSOR_TYPE_AMBIENT_TEMPERATURE = 13,
162 /**
163 * {@link ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED}
164 */
165 ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14,
166 /**
167 * {@link ASENSOR_TYPE_GAME_ROTATION_VECTOR}
168 */
169 ASENSOR_TYPE_GAME_ROTATION_VECTOR = 15,
170 /**
171 * {@link ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED}
172 */
173 ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED = 16,
174 /**
175 * {@link ASENSOR_TYPE_SIGNIFICANT_MOTION}
176 */
177 ASENSOR_TYPE_SIGNIFICANT_MOTION = 17,
178 /**
179 * {@link ASENSOR_TYPE_STEP_DETECTOR}
180 */
181 ASENSOR_TYPE_STEP_DETECTOR = 18,
182 /**
183 * {@link ASENSOR_TYPE_STEP_COUNTER}
184 */
185 ASENSOR_TYPE_STEP_COUNTER = 19,
186 /**
187 * {@link ASENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR}
188 */
189 ASENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR = 20,
190 /**
191 * {@link ASENSOR_TYPE_HEART_RATE}
192 */
193 ASENSOR_TYPE_HEART_RATE = 21,
194 /**
195 * {@link ASENSOR_TYPE_POSE_6DOF}
196 */
197 ASENSOR_TYPE_POSE_6DOF = 28,
198 /**
199 * {@link ASENSOR_TYPE_STATIONARY_DETECT}
200 */
201 ASENSOR_TYPE_STATIONARY_DETECT = 29,
202 /**
203 * {@link ASENSOR_TYPE_MOTION_DETECT}
204 */
205 ASENSOR_TYPE_MOTION_DETECT = 30,
206 /**
207 * {@link ASENSOR_TYPE_HEART_BEAT}
208 */
209 ASENSOR_TYPE_HEART_BEAT = 31,
210 /**
Brian Stackccd88432019-01-08 17:04:18 -0800211 * This sensor type is for delivering additional sensor information aside
212 * from sensor event data.
213 *
214 * Additional information may include:
215 * - {@link ASENSOR_ADDITIONAL_INFO_INTERNAL_TEMPERATURE}
216 * - {@link ASENSOR_ADDITIONAL_INFO_SAMPLING}
217 * - {@link ASENSOR_ADDITIONAL_INFO_SENSOR_PLACEMENT}
218 * - {@link ASENSOR_ADDITIONAL_INFO_UNTRACKED_DELAY}
219 * - {@link ASENSOR_ADDITIONAL_INFO_VEC3_CALIBRATION}
220 *
221 * This type will never bind to a sensor. In other words, no sensor in the
222 * sensor list can have the type {@link ASENSOR_TYPE_ADDITIONAL_INFO}.
223 *
224 * If a device supports the sensor additional information feature, it will
225 * report additional information events via {@link ASensorEvent} and will
226 * have {@link ASensorEvent#type} set to
227 * {@link ASENSOR_TYPE_ADDITIONAL_INFO} and {@link ASensorEvent#sensor} set
228 * to the handle of the reporting sensor.
229 *
230 * Additional information reports consist of multiple frames ordered by
231 * {@link ASensorEvent#timestamp}. The first frame in the report will have
232 * a {@link AAdditionalInfoEvent#type} of
233 * {@link ASENSOR_ADDITIONAL_INFO_BEGIN}, and the last frame in the report
234 * will have a {@link AAdditionalInfoEvent#type} of
235 * {@link ASENSOR_ADDITIONAL_INFO_END}.
236 *
237 */
238 ASENSOR_TYPE_ADDITIONAL_INFO = 33,
239 /**
Elliott Hughesf78be362018-01-23 15:33:56 -0800240 * {@link ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT}
241 */
242 ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT = 34,
243 /**
244 * {@link ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED}
245 */
246 ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED = 35,
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700247};
248
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700249/**
250 * Sensor accuracy measure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700251 */
252enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700253 /** no contact */
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700254 ASENSOR_STATUS_NO_CONTACT = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700255 /** unreliable */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700256 ASENSOR_STATUS_UNRELIABLE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700257 /** low accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700258 ASENSOR_STATUS_ACCURACY_LOW = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700259 /** medium accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700260 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700261 /** high accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700262 ASENSOR_STATUS_ACCURACY_HIGH = 3
263};
264
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700265/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700266 * Sensor Reporting Modes.
267 */
268enum {
Peng Xu37317b62017-03-07 17:49:31 -0800269 /** invalid reporting mode */
270 AREPORTING_MODE_INVALID = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700271 /** continuous reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700272 AREPORTING_MODE_CONTINUOUS = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700273 /** reporting on change */
Aravind Akella0e025c52014-06-03 19:19:57 -0700274 AREPORTING_MODE_ON_CHANGE = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700275 /** on shot reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700276 AREPORTING_MODE_ONE_SHOT = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700277 /** special trigger reporting */
Peng Xu37317b62017-03-07 17:49:31 -0800278 AREPORTING_MODE_SPECIAL_TRIGGER = 3
Aravind Akella0e025c52014-06-03 19:19:57 -0700279};
280
Peng Xu47cddca2017-02-15 23:31:22 -0800281/**
282 * Sensor Direct Report Rates.
283 */
284enum {
285 /** stopped */
286 ASENSOR_DIRECT_RATE_STOP = 0,
287 /** nominal 50Hz */
288 ASENSOR_DIRECT_RATE_NORMAL = 1,
289 /** nominal 200Hz */
290 ASENSOR_DIRECT_RATE_FAST = 2,
291 /** nominal 800Hz */
292 ASENSOR_DIRECT_RATE_VERY_FAST = 3
293};
294
295/**
296 * Sensor Direct Channel Type.
297 */
298enum {
299 /** shared memory created by ASharedMemory_create */
300 ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY = 1,
301 /** AHardwareBuffer */
302 ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER = 2
303};
304
Brian Stackccd88432019-01-08 17:04:18 -0800305/**
306 * Sensor Additional Info Types.
307 *
308 * Used to populate {@link AAdditionalInfoEvent#type}.
309 */
310enum {
311 /** Marks the beginning of additional information frames */
312 ASENSOR_ADDITIONAL_INFO_BEGIN = 0,
313
314 /** Marks the end of additional information frames */
315 ASENSOR_ADDITIONAL_INFO_END = 1,
316
317 /**
318 * Estimation of the delay that is not tracked by sensor timestamps. This
319 * includes delay introduced by sensor front-end filtering, data transport,
320 * etc.
321 * float[2]: delay in seconds, standard deviation of estimated value
322 */
323 ASENSOR_ADDITIONAL_INFO_UNTRACKED_DELAY = 0x10000,
324
325 /** float: Celsius temperature */
326 ASENSOR_ADDITIONAL_INFO_INTERNAL_TEMPERATURE,
327
328 /**
329 * First three rows of a homogeneous matrix, which represents calibration to
330 * a three-element vector raw sensor reading.
331 * float[12]: 3x4 matrix in row major order
332 */
333 ASENSOR_ADDITIONAL_INFO_VEC3_CALIBRATION,
334
335 /**
336 * Location and orientation of sensor element in the device frame: origin is
337 * the geometric center of the mobile device screen surface; the axis
338 * definition corresponds to Android sensor definitions.
339 * float[12]: 3x4 matrix in row major order
340 */
341 ASENSOR_ADDITIONAL_INFO_SENSOR_PLACEMENT,
342
343 /**
344 * float[2]: raw sample period in seconds,
345 * standard deviation of sampling period
346 */
347 ASENSOR_ADDITIONAL_INFO_SAMPLING,
348};
349
Aravind Akella0e025c52014-06-03 19:19:57 -0700350/*
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700351 * A few useful constants
352 */
353
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700354/** Earth's gravity in m/s^2 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700355#define ASENSOR_STANDARD_GRAVITY (9.80665f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700356/** Maximum magnetic field on Earth's surface in uT */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700357#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700358/** Minimum magnetic field on Earth's surface in uT*/
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700359#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
360
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700361/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700362 * A sensor event.
363 */
364
Peng Xu70b98382017-08-07 14:09:11 -0700365/* NOTE: changes to these structs have to be backward compatible */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700366typedef struct ASensorVector {
367 union {
368 float v[3];
369 struct {
370 float x;
371 float y;
372 float z;
373 };
374 struct {
375 float azimuth;
376 float pitch;
377 float roll;
378 };
379 };
380 int8_t status;
381 uint8_t reserved[3];
382} ASensorVector;
383
Aravind Akella724d91d2013-06-27 12:04:23 -0700384typedef struct AMetaDataEvent {
385 int32_t what;
386 int32_t sensor;
387} AMetaDataEvent;
388
389typedef struct AUncalibratedEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800390 union {
391 float uncalib[3];
392 struct {
393 float x_uncalib;
394 float y_uncalib;
395 float z_uncalib;
396 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700397 };
Peng Xu9e720462016-01-26 18:48:54 -0800398 union {
399 float bias[3];
400 struct {
401 float x_bias;
402 float y_bias;
403 float z_bias;
404 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700405 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700406} AUncalibratedEvent;
407
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700408typedef struct AHeartRateEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800409 float bpm;
410 int8_t status;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700411} AHeartRateEvent;
412
Peng Xu2576cb62016-01-20 00:22:09 -0800413typedef struct ADynamicSensorEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800414 int32_t connected;
415 int32_t handle;
Peng Xu2576cb62016-01-20 00:22:09 -0800416} ADynamicSensorEvent;
417
Brian Stackccd88432019-01-08 17:04:18 -0800418typedef struct AAdditionalInfoEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800419 int32_t type;
420 int32_t serial;
421 union {
422 int32_t data_int32[14];
423 float data_float[14];
424 };
425} AAdditionalInfoEvent;
426
Peng Xu70b98382017-08-07 14:09:11 -0700427/* NOTE: changes to this struct has to be backward compatible */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700428typedef struct ASensorEvent {
429 int32_t version; /* sizeof(struct ASensorEvent) */
430 int32_t sensor;
431 int32_t type;
432 int32_t reserved0;
433 int64_t timestamp;
434 union {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700435 union {
436 float data[16];
437 ASensorVector vector;
438 ASensorVector acceleration;
439 ASensorVector magnetic;
440 float temperature;
441 float distance;
442 float light;
443 float pressure;
Aravind Akella724d91d2013-06-27 12:04:23 -0700444 float relative_humidity;
445 AUncalibratedEvent uncalibrated_gyro;
446 AUncalibratedEvent uncalibrated_magnetic;
447 AMetaDataEvent meta_data;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700448 AHeartRateEvent heart_rate;
Peng Xu2576cb62016-01-20 00:22:09 -0800449 ADynamicSensorEvent dynamic_sensor_meta;
Peng Xu9e720462016-01-26 18:48:54 -0800450 AAdditionalInfoEvent additional_info;
Mathias Agopianba02cd22013-07-03 16:20:57 -0700451 };
452 union {
453 uint64_t data[8];
454 uint64_t step_counter;
455 } u64;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700456 };
Aravind Akella9a844cf2014-02-11 18:58:52 -0800457
458 uint32_t flags;
459 int32_t reserved1[3];
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700460} ASensorEvent;
461
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700462struct ASensorManager;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700463/**
464 * {@link ASensorManager} is an opaque type to manage sensors and
465 * events queues.
466 *
467 * {@link ASensorManager} is a singleton that can be obtained using
468 * ASensorManager_getInstance().
469 *
470 * This file provides a set of functions that uses {@link
471 * ASensorManager} to access and list hardware sensors, and
472 * create and destroy event queues:
473 * - ASensorManager_getSensorList()
474 * - ASensorManager_getDefaultSensor()
475 * - ASensorManager_getDefaultSensorEx()
476 * - ASensorManager_createEventQueue()
477 * - ASensorManager_destroyEventQueue()
478 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700479typedef struct ASensorManager ASensorManager;
480
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700481
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700482struct ASensorEventQueue;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700483/**
484 * {@link ASensorEventQueue} is an opaque type that provides access to
485 * {@link ASensorEvent} from hardware sensors.
486 *
487 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue().
488 *
489 * This file provides a set of functions to enable and disable
490 * sensors, check and get events, and set event rates on a {@link
491 * ASensorEventQueue}.
492 * - ASensorEventQueue_enableSensor()
493 * - ASensorEventQueue_disableSensor()
494 * - ASensorEventQueue_hasEvents()
495 * - ASensorEventQueue_getEvents()
496 * - ASensorEventQueue_setEventRate()
497 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700498typedef struct ASensorEventQueue ASensorEventQueue;
499
500struct ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700501/**
502 * {@link ASensor} is an opaque type that provides information about
503 * an hardware sensors.
504 *
505 * A {@link ASensor} pointer can be obtained using
506 * ASensorManager_getDefaultSensor(),
507 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}.
508 *
509 * This file provides a set of functions to access properties of a
510 * {@link ASensor}:
511 * - ASensor_getName()
512 * - ASensor_getVendor()
513 * - ASensor_getType()
514 * - ASensor_getResolution()
515 * - ASensor_getMinDelay()
516 * - ASensor_getFifoMaxEventCount()
517 * - ASensor_getFifoReservedEventCount()
518 * - ASensor_getStringType()
519 * - ASensor_getReportingMode()
520 * - ASensor_isWakeUpSensor()
521 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700522typedef struct ASensor ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700523/**
524 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}.
525 *
526 * This is used to define entry in {@link ASensorList} arrays.
527 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700528typedef ASensor const* ASensorRef;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700529/**
530 * {@link ASensorList} is an array of reference to {@link ASensor}.
531 *
532 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList().
533 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700534typedef ASensorRef const* ASensorList;
535
536/*****************************************************************************/
537
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700538/**
Svet Ganov5fa32d42015-05-07 10:50:59 -0700539 * Get a reference to the sensor manager. ASensorManager is a singleton
540 * per package as different packages may have access to different sensors.
541 *
542 * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead.
543 *
544 * Example:
545 *
546 * ASensorManager* sensorManager = ASensorManager_getInstance();
547 *
548 */
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700549#if __ANDROID_API__ >= 26
Svet Ganov5fa32d42015-05-07 10:50:59 -0700550__attribute__ ((deprecated)) ASensorManager* ASensorManager_getInstance();
Peng Xu477db442017-07-17 16:40:50 -0700551#else
552ASensorManager* ASensorManager_getInstance();
553#endif
Svet Ganov5fa32d42015-05-07 10:50:59 -0700554
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700555#if __ANDROID_API__ >= 26
Peng Xu80df0162017-08-05 19:00:23 -0700556/**
Svet Ganov5fa32d42015-05-07 10:50:59 -0700557 * Get a reference to the sensor manager. ASensorManager is a singleton
558 * per package as different packages may have access to different sensors.
559 *
560 * Example:
561 *
Peng Xu80df0162017-08-05 19:00:23 -0700562 * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz");
Svet Ganov5fa32d42015-05-07 10:50:59 -0700563 *
564 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700565ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName) __INTRODUCED_IN(26);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700566#endif
Svet Ganov5fa32d42015-05-07 10:50:59 -0700567
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700568/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700569 * Returns the list of available sensors.
570 */
571int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list);
572
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700573/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700574 * Returns the default sensor for the given type, or NULL if no sensor
Aravind Akellab37ba392014-08-05 14:53:07 -0700575 * of that type exists.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700576 */
577ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
578
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700579#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700580/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700581 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor
582 * of this type and wakeUp properties exists.
583 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700584ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type, bool wakeUp) __INTRODUCED_IN(21);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700585#endif
Aravind Akellab37ba392014-08-05 14:53:07 -0700586
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700587/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700588 * Creates a new sensor event queue and associate it with a looper.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700589 *
590 * "ident" is a identifier for the events that will be returned when
591 * calling ALooper_pollOnce(). The identifier must be >= 0, or
592 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700593 */
594ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
595 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
596
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700597/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700598 * Destroys the event queue and free all resources associated to it.
599 */
600int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
601
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700602#if __ANDROID_API__ >= 26
Peng Xu47cddca2017-02-15 23:31:22 -0800603/**
604 * Create direct channel based on shared memory
605 *
606 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} to be used
607 * for configuring sensor direct report.
608 *
609 * \param manager the {@link ASensorManager} instance obtained from
610 * {@link ASensorManager_getInstanceForPackage}.
611 * \param fd file descriptor representing a shared memory created by
612 * {@link ASharedMemory_create}
613 * \param size size to be used, must be less or equal to size of shared memory.
614 *
615 * \return a positive integer as a channel id to be used in
616 * {@link ASensorManager_destroyDirectChannel} and
617 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
618 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700619int ASensorManager_createSharedMemoryDirectChannel(ASensorManager* manager, int fd, size_t size) __INTRODUCED_IN(26);
Peng Xu47cddca2017-02-15 23:31:22 -0800620
621/**
622 * Create direct channel based on AHardwareBuffer
623 *
624 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER} type to be used
625 * for configuring sensor direct report.
626 *
627 * \param manager the {@link ASensorManager} instance obtained from
628 * {@link ASensorManager_getInstanceForPackage}.
629 * \param buffer {@link AHardwareBuffer} instance created by {@link AHardwareBuffer_allocate}.
630 * \param size the intended size to be used, must be less or equal to size of buffer.
631 *
632 * \return a positive integer as a channel id to be used in
633 * {@link ASensorManager_destroyDirectChannel} and
634 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
635 */
636int ASensorManager_createHardwareBufferDirectChannel(
Elliott Hughes9db409b2018-06-18 12:28:46 -0700637 ASensorManager* manager, AHardwareBuffer const * buffer, size_t size) __INTRODUCED_IN(26);
Peng Xu47cddca2017-02-15 23:31:22 -0800638
639/**
640 * Destroy a direct channel
641 *
642 * Destroy a direct channel previously created using {@link ASensorManager_createDirectChannel}.
643 * The buffer used for creating direct channel does not get destroyed with
644 * {@link ASensorManager_destroy} and has to be close or released separately.
645 *
646 * \param manager the {@link ASensorManager} instance obtained from
647 * {@link ASensorManager_getInstanceForPackage}.
648 * \param channelId channel id (a positive integer) returned from
649 * {@link ASensorManager_createSharedMemoryDirectChannel} or
650 * {@link ASensorManager_createHardwareBufferDirectChannel}.
651 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700652void ASensorManager_destroyDirectChannel(ASensorManager* manager, int channelId) __INTRODUCED_IN(26);
Peng Xu47cddca2017-02-15 23:31:22 -0800653
654/**
655 * Configure direct report on channel
656 *
657 * Configure sensor direct report on a direct channel: set rate to value other than
658 * {@link ASENSOR_DIRECT_RATE_STOP} so that sensor event can be directly
Peng Xuec53d022017-04-06 18:02:29 -0700659 * written into the shared memory region used for creating the buffer. It returns a positive token
660 * which can be used for identify sensor events from different sensors on success. Calling with rate
661 * {@link ASENSOR_DIRECT_RATE_STOP} will stop direct report of the sensor specified in the channel.
Peng Xu47cddca2017-02-15 23:31:22 -0800662 *
663 * To stop all active sensor direct report configured to a channel, set sensor to NULL and rate to
664 * {@link ASENSOR_DIRECT_RATE_STOP}.
665 *
666 * In order to successfully configure a direct report, the sensor has to support the specified rate
667 * and the channel type, which can be checked by {@link ASensor_getHighestDirectReportRateLevel} and
668 * {@link ASensor_isDirectChannelTypeSupported}, respectively.
669 *
670 * Example:
Peng Xu47cddca2017-02-15 23:31:22 -0800671 *
Peng Xu80df0162017-08-05 19:00:23 -0700672 * ASensorManager *manager = ...;
673 * ASensor *sensor = ...;
674 * int channelId = ...;
675 *
676 * ASensorManager_configureDirectReport(manager, sensor, channel_id, ASENSOR_DIRECT_RATE_FAST);
Peng Xu47cddca2017-02-15 23:31:22 -0800677 *
678 * \param manager the {@link ASensorManager} instance obtained from
679 * {@link ASensorManager_getInstanceForPackage}.
680 * \param sensor a {@link ASensor} to denote which sensor to be operate. It can be NULL if rate
681 * is {@link ASENSOR_DIRECT_RATE_STOP}, denoting stopping of all active sensor
682 * direct report.
683 * \param channelId channel id (a positive integer) returned from
684 * {@link ASensorManager_createSharedMemoryDirectChannel} or
685 * {@link ASensorManager_createHardwareBufferDirectChannel}.
686 *
Peng Xuec53d022017-04-06 18:02:29 -0700687 * \return positive token for success or negative error code.
Peng Xu47cddca2017-02-15 23:31:22 -0800688 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700689int ASensorManager_configureDirectReport(ASensorManager* manager,
690 ASensor const* sensor, int channelId, int rate) __INTRODUCED_IN(26);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700691#endif /* __ANDROID_API__ >= 26 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700692
693/*****************************************************************************/
694
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700695/**
Peng Xu80df0162017-08-05 19:00:23 -0700696 * Enable the selected sensor with sampling and report parameters
697 *
698 * Enable the selected sensor at a specified sampling period and max batch report latency.
699 * To disable sensor, use {@link ASensorEventQueue_disableSensor}.
700 *
701 * \param queue {@link ASensorEventQueue} for sensor event to be report to.
702 * \param sensor {@link ASensor} to be enabled.
703 * \param samplingPeriodUs sampling period of sensor in microseconds.
704 * \param maxBatchReportLatencyus maximum time interval between two batch of sensor events are
705 * delievered in microseconds. For sensor streaming, set to 0.
706 * \return 0 on success or a negative error code on failure.
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530707 */
708int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor,
Peng Xuda8385c2017-02-28 20:19:47 -0800709 int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs);
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530710
711/**
Peng Xu80df0162017-08-05 19:00:23 -0700712 * Enable the selected sensor at default sampling rate.
713 *
714 * Start event reports of a sensor to specified sensor event queue at a default rate.
715 *
716 * \param queue {@link ASensorEventQueue} for sensor event to be report to.
717 * \param sensor {@link ASensor} to be enabled.
718 *
719 * \return 0 on success or a negative error code on failure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700720 */
721int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor);
722
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700723/**
Peng Xu80df0162017-08-05 19:00:23 -0700724 * Disable the selected sensor.
725 *
726 * Stop event reports from the sensor to specified sensor event queue.
727 *
728 * \param queue {@link ASensorEventQueue} to be changed
729 * \param sensor {@link ASensor} to be disabled
730 * \return 0 on success or a negative error code on failure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700731 */
732int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor);
733
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700734/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700735 * Sets the delivery rate of events in microseconds for the given sensor.
Peng Xu80df0162017-08-05 19:00:23 -0700736 *
737 * This function has to be called after {@link ASensorEventQueue_enableSensor}.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700738 * Note that this is a hint only, generally event will arrive at a higher
739 * rate. It is an error to set a rate inferior to the value returned by
740 * ASensor_getMinDelay().
Peng Xu80df0162017-08-05 19:00:23 -0700741 *
742 * \param queue {@link ASensorEventQueue} to which sensor event is delivered.
743 * \param sensor {@link ASensor} of which sampling rate to be updated.
744 * \param usec sensor sampling period (1/sampling rate) in microseconds
745 * \return 0 on sucess or a negative error code on failure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700746 */
747int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
748
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700749/**
Peng Xu80df0162017-08-05 19:00:23 -0700750 * Determine if a sensor event queue has pending event to be processed.
751 *
752 * \param queue {@link ASensorEventQueue} to be queried
753 * \return 1 if the queue has events; 0 if it does not have events;
754 * or a negative value if there is an error.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700755 */
756int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
757
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700758/**
Peng Xu80df0162017-08-05 19:00:23 -0700759 * Retrieve pending events in sensor event queue
760 *
761 * Retrieve next available events from the queue to a specified event array.
762 *
763 * \param queue {@link ASensorEventQueue} to get events from
764 * \param events pointer to an array of {@link ASensorEvents}.
765 * \param count max number of event that can be filled into array event.
766 * \return number of events returned on success; negative error code when
767 * no events are pending or an error has occurred.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700768 *
769 * Examples:
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700770 *
Peng Xu80df0162017-08-05 19:00:23 -0700771 * ASensorEvent event;
772 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
773 *
774 * ASensorEvent eventBuffer[8];
775 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700776 *
777 */
Peng Xuda8385c2017-02-28 20:19:47 -0800778ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, ASensorEvent* events, size_t count);
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700779
780
781/*****************************************************************************/
782
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700783/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700784 * Returns this sensor's name (non localized)
785 */
786const char* ASensor_getName(ASensor const* sensor);
787
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700788/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700789 * Returns this sensor's vendor's name (non localized)
790 */
791const char* ASensor_getVendor(ASensor const* sensor);
792
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700793/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700794 * Return this sensor's type
795 */
796int ASensor_getType(ASensor const* sensor);
797
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700798/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700799 * Returns this sensors's resolution
800 */
801float ASensor_getResolution(ASensor const* sensor);
802
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700803/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700804 * Returns the minimum delay allowed between events in microseconds.
805 * A value of zero means that this sensor doesn't report events at a
806 * constant rate, but rather only when a new data is available.
807 */
808int ASensor_getMinDelay(ASensor const* sensor);
809
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700810#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700811/**
Aravind Akella70018042014-04-07 22:52:37 +0000812 * Returns the maximum size of batches for this sensor. Batches will often be
813 * smaller, as the hardware fifo might be used for other sensors.
814 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700815int ASensor_getFifoMaxEventCount(ASensor const* sensor) __INTRODUCED_IN(21);
Aravind Akella70018042014-04-07 22:52:37 +0000816
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700817/**
Aravind Akella70018042014-04-07 22:52:37 +0000818 * Returns the hardware batch fifo size reserved to this sensor.
819 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700820int ASensor_getFifoReservedEventCount(ASensor const* sensor) __INTRODUCED_IN(21);
Aravind Akella70018042014-04-07 22:52:37 +0000821
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700822/**
Aravind Akella70018042014-04-07 22:52:37 +0000823 * Returns this sensor's string type.
824 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700825const char* ASensor_getStringType(ASensor const* sensor) __INTRODUCED_IN(21);
Aravind Akella70018042014-04-07 22:52:37 +0000826
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700827/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700828 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants.
829 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700830int ASensor_getReportingMode(ASensor const* sensor) __INTRODUCED_IN(21);
Aravind Akella0e025c52014-06-03 19:19:57 -0700831
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700832/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700833 * Returns true if this is a wake up sensor, false otherwise.
834 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700835bool ASensor_isWakeUpSensor(ASensor const* sensor) __INTRODUCED_IN(21);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700836#endif /* __ANDROID_API__ >= 21 */
Aravind Akellab37ba392014-08-05 14:53:07 -0700837
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700838#if __ANDROID_API__ >= 26
Peng Xu47cddca2017-02-15 23:31:22 -0800839/**
840 * Test if sensor supports a certain type of direct channel.
841 *
842 * \param sensor a {@link ASensor} to denote the sensor to be checked.
843 * \param channelType Channel type constant, either
844 * {@ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY}
845 * or {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER}.
846 * \returns true if sensor supports the specified direct channel type.
847 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700848bool ASensor_isDirectChannelTypeSupported(ASensor const* sensor, int channelType) __INTRODUCED_IN(26);
849
Peng Xu47cddca2017-02-15 23:31:22 -0800850/**
851 * Get the highest direct rate level that a sensor support.
852 *
853 * \param sensor a {@link ASensor} to denote the sensor to be checked.
854 *
855 * \return a ASENSOR_DIRECT_RATE_... enum denoting the highest rate level supported by the sensor.
856 * If return value is {@link ASENSOR_DIRECT_RATE_STOP}, it means the sensor
857 * does not support direct report.
858 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700859int ASensor_getHighestDirectReportRateLevel(ASensor const* sensor) __INTRODUCED_IN(26);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700860#endif /* __ANDROID_API__ >= 26 */
Peng Xu47cddca2017-02-15 23:31:22 -0800861
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700862#ifdef __cplusplus
863};
864#endif
865
866#endif // ANDROID_SENSOR_H
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700867
868/** @} */