blob: 005564d153ac646a8fc72c664f9370dfe9e917c3 [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 /**
211 * {@link ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT}
212 */
213 ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT = 34,
214 /**
215 * {@link ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED}
216 */
217 ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED = 35,
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700218};
219
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700220/**
221 * Sensor accuracy measure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700222 */
223enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700224 /** no contact */
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700225 ASENSOR_STATUS_NO_CONTACT = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700226 /** unreliable */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700227 ASENSOR_STATUS_UNRELIABLE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700228 /** low accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700229 ASENSOR_STATUS_ACCURACY_LOW = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700230 /** medium accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700231 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700232 /** high accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700233 ASENSOR_STATUS_ACCURACY_HIGH = 3
234};
235
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700236/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700237 * Sensor Reporting Modes.
238 */
239enum {
Peng Xu37317b62017-03-07 17:49:31 -0800240 /** invalid reporting mode */
241 AREPORTING_MODE_INVALID = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700242 /** continuous reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700243 AREPORTING_MODE_CONTINUOUS = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700244 /** reporting on change */
Aravind Akella0e025c52014-06-03 19:19:57 -0700245 AREPORTING_MODE_ON_CHANGE = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700246 /** on shot reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700247 AREPORTING_MODE_ONE_SHOT = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700248 /** special trigger reporting */
Peng Xu37317b62017-03-07 17:49:31 -0800249 AREPORTING_MODE_SPECIAL_TRIGGER = 3
Aravind Akella0e025c52014-06-03 19:19:57 -0700250};
251
Peng Xu47cddca2017-02-15 23:31:22 -0800252/**
253 * Sensor Direct Report Rates.
254 */
255enum {
256 /** stopped */
257 ASENSOR_DIRECT_RATE_STOP = 0,
258 /** nominal 50Hz */
259 ASENSOR_DIRECT_RATE_NORMAL = 1,
260 /** nominal 200Hz */
261 ASENSOR_DIRECT_RATE_FAST = 2,
262 /** nominal 800Hz */
263 ASENSOR_DIRECT_RATE_VERY_FAST = 3
264};
265
266/**
267 * Sensor Direct Channel Type.
268 */
269enum {
270 /** shared memory created by ASharedMemory_create */
271 ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY = 1,
272 /** AHardwareBuffer */
273 ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER = 2
274};
275
Aravind Akella0e025c52014-06-03 19:19:57 -0700276/*
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700277 * A few useful constants
278 */
279
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700280/** Earth's gravity in m/s^2 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700281#define ASENSOR_STANDARD_GRAVITY (9.80665f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700282/** Maximum magnetic field on Earth's surface in uT */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700283#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700284/** Minimum magnetic field on Earth's surface in uT*/
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700285#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
286
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700287/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700288 * A sensor event.
289 */
290
Peng Xu70b98382017-08-07 14:09:11 -0700291/* NOTE: changes to these structs have to be backward compatible */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700292typedef struct ASensorVector {
293 union {
294 float v[3];
295 struct {
296 float x;
297 float y;
298 float z;
299 };
300 struct {
301 float azimuth;
302 float pitch;
303 float roll;
304 };
305 };
306 int8_t status;
307 uint8_t reserved[3];
308} ASensorVector;
309
Aravind Akella724d91d2013-06-27 12:04:23 -0700310typedef struct AMetaDataEvent {
311 int32_t what;
312 int32_t sensor;
313} AMetaDataEvent;
314
315typedef struct AUncalibratedEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800316 union {
317 float uncalib[3];
318 struct {
319 float x_uncalib;
320 float y_uncalib;
321 float z_uncalib;
322 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700323 };
Peng Xu9e720462016-01-26 18:48:54 -0800324 union {
325 float bias[3];
326 struct {
327 float x_bias;
328 float y_bias;
329 float z_bias;
330 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700331 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700332} AUncalibratedEvent;
333
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700334typedef struct AHeartRateEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800335 float bpm;
336 int8_t status;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700337} AHeartRateEvent;
338
Peng Xu2576cb62016-01-20 00:22:09 -0800339typedef struct ADynamicSensorEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800340 int32_t connected;
341 int32_t handle;
Peng Xu2576cb62016-01-20 00:22:09 -0800342} ADynamicSensorEvent;
343
Peng Xu9e720462016-01-26 18:48:54 -0800344typedef struct {
345 int32_t type;
346 int32_t serial;
347 union {
348 int32_t data_int32[14];
349 float data_float[14];
350 };
351} AAdditionalInfoEvent;
352
Peng Xu70b98382017-08-07 14:09:11 -0700353/* NOTE: changes to this struct has to be backward compatible */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700354typedef struct ASensorEvent {
355 int32_t version; /* sizeof(struct ASensorEvent) */
356 int32_t sensor;
357 int32_t type;
358 int32_t reserved0;
359 int64_t timestamp;
360 union {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700361 union {
362 float data[16];
363 ASensorVector vector;
364 ASensorVector acceleration;
365 ASensorVector magnetic;
366 float temperature;
367 float distance;
368 float light;
369 float pressure;
Aravind Akella724d91d2013-06-27 12:04:23 -0700370 float relative_humidity;
371 AUncalibratedEvent uncalibrated_gyro;
372 AUncalibratedEvent uncalibrated_magnetic;
373 AMetaDataEvent meta_data;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700374 AHeartRateEvent heart_rate;
Peng Xu2576cb62016-01-20 00:22:09 -0800375 ADynamicSensorEvent dynamic_sensor_meta;
Peng Xu9e720462016-01-26 18:48:54 -0800376 AAdditionalInfoEvent additional_info;
Mathias Agopianba02cd22013-07-03 16:20:57 -0700377 };
378 union {
379 uint64_t data[8];
380 uint64_t step_counter;
381 } u64;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700382 };
Aravind Akella9a844cf2014-02-11 18:58:52 -0800383
384 uint32_t flags;
385 int32_t reserved1[3];
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700386} ASensorEvent;
387
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700388struct ASensorManager;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700389/**
390 * {@link ASensorManager} is an opaque type to manage sensors and
391 * events queues.
392 *
393 * {@link ASensorManager} is a singleton that can be obtained using
394 * ASensorManager_getInstance().
395 *
396 * This file provides a set of functions that uses {@link
397 * ASensorManager} to access and list hardware sensors, and
398 * create and destroy event queues:
399 * - ASensorManager_getSensorList()
400 * - ASensorManager_getDefaultSensor()
401 * - ASensorManager_getDefaultSensorEx()
402 * - ASensorManager_createEventQueue()
403 * - ASensorManager_destroyEventQueue()
404 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700405typedef struct ASensorManager ASensorManager;
406
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700407
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700408struct ASensorEventQueue;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700409/**
410 * {@link ASensorEventQueue} is an opaque type that provides access to
411 * {@link ASensorEvent} from hardware sensors.
412 *
413 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue().
414 *
415 * This file provides a set of functions to enable and disable
416 * sensors, check and get events, and set event rates on a {@link
417 * ASensorEventQueue}.
418 * - ASensorEventQueue_enableSensor()
419 * - ASensorEventQueue_disableSensor()
420 * - ASensorEventQueue_hasEvents()
421 * - ASensorEventQueue_getEvents()
422 * - ASensorEventQueue_setEventRate()
423 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700424typedef struct ASensorEventQueue ASensorEventQueue;
425
426struct ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700427/**
428 * {@link ASensor} is an opaque type that provides information about
429 * an hardware sensors.
430 *
431 * A {@link ASensor} pointer can be obtained using
432 * ASensorManager_getDefaultSensor(),
433 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}.
434 *
435 * This file provides a set of functions to access properties of a
436 * {@link ASensor}:
437 * - ASensor_getName()
438 * - ASensor_getVendor()
439 * - ASensor_getType()
440 * - ASensor_getResolution()
441 * - ASensor_getMinDelay()
442 * - ASensor_getFifoMaxEventCount()
443 * - ASensor_getFifoReservedEventCount()
444 * - ASensor_getStringType()
445 * - ASensor_getReportingMode()
446 * - ASensor_isWakeUpSensor()
447 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700448typedef struct ASensor ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700449/**
450 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}.
451 *
452 * This is used to define entry in {@link ASensorList} arrays.
453 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700454typedef ASensor const* ASensorRef;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700455/**
456 * {@link ASensorList} is an array of reference to {@link ASensor}.
457 *
458 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList().
459 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700460typedef ASensorRef const* ASensorList;
461
462/*****************************************************************************/
463
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700464/**
Svet Ganov5fa32d42015-05-07 10:50:59 -0700465 * Get a reference to the sensor manager. ASensorManager is a singleton
466 * per package as different packages may have access to different sensors.
467 *
468 * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead.
469 *
470 * Example:
471 *
472 * ASensorManager* sensorManager = ASensorManager_getInstance();
473 *
474 */
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700475#if __ANDROID_API__ >= 26
Svet Ganov5fa32d42015-05-07 10:50:59 -0700476__attribute__ ((deprecated)) ASensorManager* ASensorManager_getInstance();
Peng Xu477db442017-07-17 16:40:50 -0700477#else
478ASensorManager* ASensorManager_getInstance();
479#endif
Svet Ganov5fa32d42015-05-07 10:50:59 -0700480
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700481#if __ANDROID_API__ >= 26
Peng Xu80df0162017-08-05 19:00:23 -0700482/**
Svet Ganov5fa32d42015-05-07 10:50:59 -0700483 * Get a reference to the sensor manager. ASensorManager is a singleton
484 * per package as different packages may have access to different sensors.
485 *
486 * Example:
487 *
Peng Xu80df0162017-08-05 19:00:23 -0700488 * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz");
Svet Ganov5fa32d42015-05-07 10:50:59 -0700489 *
490 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700491ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName) __INTRODUCED_IN(26);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700492#endif
Svet Ganov5fa32d42015-05-07 10:50:59 -0700493
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700494/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700495 * Returns the list of available sensors.
496 */
497int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list);
498
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700499/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700500 * Returns the default sensor for the given type, or NULL if no sensor
Aravind Akellab37ba392014-08-05 14:53:07 -0700501 * of that type exists.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700502 */
503ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
504
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700505#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700506/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700507 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor
508 * of this type and wakeUp properties exists.
509 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700510ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type, bool wakeUp) __INTRODUCED_IN(21);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700511#endif
Aravind Akellab37ba392014-08-05 14:53:07 -0700512
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700513/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700514 * Creates a new sensor event queue and associate it with a looper.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700515 *
516 * "ident" is a identifier for the events that will be returned when
517 * calling ALooper_pollOnce(). The identifier must be >= 0, or
518 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700519 */
520ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
521 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
522
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700523/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700524 * Destroys the event queue and free all resources associated to it.
525 */
526int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
527
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700528#if __ANDROID_API__ >= 26
Peng Xu47cddca2017-02-15 23:31:22 -0800529/**
530 * Create direct channel based on shared memory
531 *
532 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} to be used
533 * for configuring sensor direct report.
534 *
535 * \param manager the {@link ASensorManager} instance obtained from
536 * {@link ASensorManager_getInstanceForPackage}.
537 * \param fd file descriptor representing a shared memory created by
538 * {@link ASharedMemory_create}
539 * \param size size to be used, must be less or equal to size of shared memory.
540 *
541 * \return a positive integer as a channel id to be used in
542 * {@link ASensorManager_destroyDirectChannel} and
543 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
544 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700545int ASensorManager_createSharedMemoryDirectChannel(ASensorManager* manager, int fd, size_t size) __INTRODUCED_IN(26);
Peng Xu47cddca2017-02-15 23:31:22 -0800546
547/**
548 * Create direct channel based on AHardwareBuffer
549 *
550 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER} type to be used
551 * for configuring sensor direct report.
552 *
553 * \param manager the {@link ASensorManager} instance obtained from
554 * {@link ASensorManager_getInstanceForPackage}.
555 * \param buffer {@link AHardwareBuffer} instance created by {@link AHardwareBuffer_allocate}.
556 * \param size the intended size to be used, must be less or equal to size of buffer.
557 *
558 * \return a positive integer as a channel id to be used in
559 * {@link ASensorManager_destroyDirectChannel} and
560 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
561 */
562int ASensorManager_createHardwareBufferDirectChannel(
Elliott Hughes9db409b2018-06-18 12:28:46 -0700563 ASensorManager* manager, AHardwareBuffer const * buffer, size_t size) __INTRODUCED_IN(26);
Peng Xu47cddca2017-02-15 23:31:22 -0800564
565/**
566 * Destroy a direct channel
567 *
568 * Destroy a direct channel previously created using {@link ASensorManager_createDirectChannel}.
569 * The buffer used for creating direct channel does not get destroyed with
570 * {@link ASensorManager_destroy} and has to be close or released separately.
571 *
572 * \param manager the {@link ASensorManager} instance obtained from
573 * {@link ASensorManager_getInstanceForPackage}.
574 * \param channelId channel id (a positive integer) returned from
575 * {@link ASensorManager_createSharedMemoryDirectChannel} or
576 * {@link ASensorManager_createHardwareBufferDirectChannel}.
577 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700578void ASensorManager_destroyDirectChannel(ASensorManager* manager, int channelId) __INTRODUCED_IN(26);
Peng Xu47cddca2017-02-15 23:31:22 -0800579
580/**
581 * Configure direct report on channel
582 *
583 * Configure sensor direct report on a direct channel: set rate to value other than
584 * {@link ASENSOR_DIRECT_RATE_STOP} so that sensor event can be directly
Peng Xuec53d022017-04-06 18:02:29 -0700585 * written into the shared memory region used for creating the buffer. It returns a positive token
586 * which can be used for identify sensor events from different sensors on success. Calling with rate
587 * {@link ASENSOR_DIRECT_RATE_STOP} will stop direct report of the sensor specified in the channel.
Peng Xu47cddca2017-02-15 23:31:22 -0800588 *
589 * To stop all active sensor direct report configured to a channel, set sensor to NULL and rate to
590 * {@link ASENSOR_DIRECT_RATE_STOP}.
591 *
592 * In order to successfully configure a direct report, the sensor has to support the specified rate
593 * and the channel type, which can be checked by {@link ASensor_getHighestDirectReportRateLevel} and
594 * {@link ASensor_isDirectChannelTypeSupported}, respectively.
595 *
596 * Example:
Peng Xu47cddca2017-02-15 23:31:22 -0800597 *
Peng Xu80df0162017-08-05 19:00:23 -0700598 * ASensorManager *manager = ...;
599 * ASensor *sensor = ...;
600 * int channelId = ...;
601 *
602 * ASensorManager_configureDirectReport(manager, sensor, channel_id, ASENSOR_DIRECT_RATE_FAST);
Peng Xu47cddca2017-02-15 23:31:22 -0800603 *
604 * \param manager the {@link ASensorManager} instance obtained from
605 * {@link ASensorManager_getInstanceForPackage}.
606 * \param sensor a {@link ASensor} to denote which sensor to be operate. It can be NULL if rate
607 * is {@link ASENSOR_DIRECT_RATE_STOP}, denoting stopping of all active sensor
608 * direct report.
609 * \param channelId channel id (a positive integer) returned from
610 * {@link ASensorManager_createSharedMemoryDirectChannel} or
611 * {@link ASensorManager_createHardwareBufferDirectChannel}.
612 *
Peng Xuec53d022017-04-06 18:02:29 -0700613 * \return positive token for success or negative error code.
Peng Xu47cddca2017-02-15 23:31:22 -0800614 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700615int ASensorManager_configureDirectReport(ASensorManager* manager,
616 ASensor const* sensor, int channelId, int rate) __INTRODUCED_IN(26);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700617#endif /* __ANDROID_API__ >= 26 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700618
619/*****************************************************************************/
620
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700621/**
Peng Xu80df0162017-08-05 19:00:23 -0700622 * Enable the selected sensor with sampling and report parameters
623 *
624 * Enable the selected sensor at a specified sampling period and max batch report latency.
625 * To disable sensor, use {@link ASensorEventQueue_disableSensor}.
626 *
627 * \param queue {@link ASensorEventQueue} for sensor event to be report to.
628 * \param sensor {@link ASensor} to be enabled.
629 * \param samplingPeriodUs sampling period of sensor in microseconds.
630 * \param maxBatchReportLatencyus maximum time interval between two batch of sensor events are
631 * delievered in microseconds. For sensor streaming, set to 0.
632 * \return 0 on success or a negative error code on failure.
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530633 */
634int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor,
Peng Xuda8385c2017-02-28 20:19:47 -0800635 int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs);
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530636
637/**
Peng Xu80df0162017-08-05 19:00:23 -0700638 * Enable the selected sensor at default sampling rate.
639 *
640 * Start event reports of a sensor to specified sensor event queue at a default rate.
641 *
642 * \param queue {@link ASensorEventQueue} for sensor event to be report to.
643 * \param sensor {@link ASensor} to be enabled.
644 *
645 * \return 0 on success or a negative error code on failure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700646 */
647int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor);
648
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700649/**
Peng Xu80df0162017-08-05 19:00:23 -0700650 * Disable the selected sensor.
651 *
652 * Stop event reports from the sensor to specified sensor event queue.
653 *
654 * \param queue {@link ASensorEventQueue} to be changed
655 * \param sensor {@link ASensor} to be disabled
656 * \return 0 on success or a negative error code on failure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700657 */
658int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor);
659
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700660/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700661 * Sets the delivery rate of events in microseconds for the given sensor.
Peng Xu80df0162017-08-05 19:00:23 -0700662 *
663 * This function has to be called after {@link ASensorEventQueue_enableSensor}.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700664 * Note that this is a hint only, generally event will arrive at a higher
665 * rate. It is an error to set a rate inferior to the value returned by
666 * ASensor_getMinDelay().
Peng Xu80df0162017-08-05 19:00:23 -0700667 *
668 * \param queue {@link ASensorEventQueue} to which sensor event is delivered.
669 * \param sensor {@link ASensor} of which sampling rate to be updated.
670 * \param usec sensor sampling period (1/sampling rate) in microseconds
671 * \return 0 on sucess or a negative error code on failure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700672 */
673int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
674
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700675/**
Peng Xu80df0162017-08-05 19:00:23 -0700676 * Determine if a sensor event queue has pending event to be processed.
677 *
678 * \param queue {@link ASensorEventQueue} to be queried
679 * \return 1 if the queue has events; 0 if it does not have events;
680 * or a negative value if there is an error.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700681 */
682int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
683
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700684/**
Peng Xu80df0162017-08-05 19:00:23 -0700685 * Retrieve pending events in sensor event queue
686 *
687 * Retrieve next available events from the queue to a specified event array.
688 *
689 * \param queue {@link ASensorEventQueue} to get events from
690 * \param events pointer to an array of {@link ASensorEvents}.
691 * \param count max number of event that can be filled into array event.
692 * \return number of events returned on success; negative error code when
693 * no events are pending or an error has occurred.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700694 *
695 * Examples:
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700696 *
Peng Xu80df0162017-08-05 19:00:23 -0700697 * ASensorEvent event;
698 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
699 *
700 * ASensorEvent eventBuffer[8];
701 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700702 *
703 */
Peng Xuda8385c2017-02-28 20:19:47 -0800704ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, ASensorEvent* events, size_t count);
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700705
706
707/*****************************************************************************/
708
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700709/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700710 * Returns this sensor's name (non localized)
711 */
712const char* ASensor_getName(ASensor const* sensor);
713
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700714/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700715 * Returns this sensor's vendor's name (non localized)
716 */
717const char* ASensor_getVendor(ASensor const* sensor);
718
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700719/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700720 * Return this sensor's type
721 */
722int ASensor_getType(ASensor const* sensor);
723
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700724/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700725 * Returns this sensors's resolution
726 */
727float ASensor_getResolution(ASensor const* sensor);
728
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700729/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700730 * Returns the minimum delay allowed between events in microseconds.
731 * A value of zero means that this sensor doesn't report events at a
732 * constant rate, but rather only when a new data is available.
733 */
734int ASensor_getMinDelay(ASensor const* sensor);
735
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700736#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700737/**
Aravind Akella70018042014-04-07 22:52:37 +0000738 * Returns the maximum size of batches for this sensor. Batches will often be
739 * smaller, as the hardware fifo might be used for other sensors.
740 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700741int ASensor_getFifoMaxEventCount(ASensor const* sensor) __INTRODUCED_IN(21);
Aravind Akella70018042014-04-07 22:52:37 +0000742
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700743/**
Aravind Akella70018042014-04-07 22:52:37 +0000744 * Returns the hardware batch fifo size reserved to this sensor.
745 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700746int ASensor_getFifoReservedEventCount(ASensor const* sensor) __INTRODUCED_IN(21);
Aravind Akella70018042014-04-07 22:52:37 +0000747
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700748/**
Aravind Akella70018042014-04-07 22:52:37 +0000749 * Returns this sensor's string type.
750 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700751const char* ASensor_getStringType(ASensor const* sensor) __INTRODUCED_IN(21);
Aravind Akella70018042014-04-07 22:52:37 +0000752
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700753/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700754 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants.
755 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700756int ASensor_getReportingMode(ASensor const* sensor) __INTRODUCED_IN(21);
Aravind Akella0e025c52014-06-03 19:19:57 -0700757
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700758/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700759 * Returns true if this is a wake up sensor, false otherwise.
760 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700761bool ASensor_isWakeUpSensor(ASensor const* sensor) __INTRODUCED_IN(21);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700762#endif /* __ANDROID_API__ >= 21 */
Aravind Akellab37ba392014-08-05 14:53:07 -0700763
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700764#if __ANDROID_API__ >= 26
Peng Xu47cddca2017-02-15 23:31:22 -0800765/**
766 * Test if sensor supports a certain type of direct channel.
767 *
768 * \param sensor a {@link ASensor} to denote the sensor to be checked.
769 * \param channelType Channel type constant, either
770 * {@ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY}
771 * or {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER}.
772 * \returns true if sensor supports the specified direct channel type.
773 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700774bool ASensor_isDirectChannelTypeSupported(ASensor const* sensor, int channelType) __INTRODUCED_IN(26);
775
Peng Xu47cddca2017-02-15 23:31:22 -0800776/**
777 * Get the highest direct rate level that a sensor support.
778 *
779 * \param sensor a {@link ASensor} to denote the sensor to be checked.
780 *
781 * \return a ASENSOR_DIRECT_RATE_... enum denoting the highest rate level supported by the sensor.
782 * If return value is {@link ASENSOR_DIRECT_RATE_STOP}, it means the sensor
783 * does not support direct report.
784 */
Elliott Hughes9db409b2018-06-18 12:28:46 -0700785int ASensor_getHighestDirectReportRateLevel(ASensor const* sensor) __INTRODUCED_IN(26);
Ryan Prichard92b1ebe2018-07-19 20:32:19 -0700786#endif /* __ANDROID_API__ >= 26 */
Peng Xu47cddca2017-02-15 23:31:22 -0800787
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700788#ifdef __cplusplus
789};
790#endif
791
792#endif // ANDROID_SENSOR_H
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700793
794/** @} */