blob: cdb3fffedc93ccb2a30c22450e6fecd29eae55f4 [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
Peng Xuda8385c2017-02-28 20:19:47 -080053#include <sys/types.h>
54#include <math.h>
55#include <stdint.h>
56
Mathias Agopiane1c61d32012-03-23 14:19:36 -070057#ifdef __cplusplus
58extern "C" {
59#endif
60
Peng Xu47cddca2017-02-15 23:31:22 -080061typedef struct AHardwareBuffer AHardwareBuffer;
Mathias Agopiane1c61d32012-03-23 14:19:36 -070062
Peng Xuda8385c2017-02-28 20:19:47 -080063#define ASENSOR_RESOLUTION_INVALID (nanf(""))
64#define ASENSOR_FIFO_COUNT_INVALID (-1)
65#define ASENSOR_DELAY_INVALID INT32_MIN
66
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070067/**
68 * Sensor types.
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070069 * (keep in sync with hardware/sensors.h)
Mathias Agopiane1c61d32012-03-23 14:19:36 -070070 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070071enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070072 /**
Peng Xu37317b62017-03-07 17:49:31 -080073 * Invalid sensor type. Returned by {@link ASensor_getType} as error value.
74 */
75 ASENSOR_TYPE_INVALID = -1,
76 /**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070077 * {@link ASENSOR_TYPE_ACCELEROMETER}
78 * reporting-mode: continuous
79 *
80 * All values are in SI units (m/s^2) and measure the acceleration of the
81 * device minus the force of gravity.
82 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070083 ASENSOR_TYPE_ACCELEROMETER = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070084 /**
85 * {@link ASENSOR_TYPE_MAGNETIC_FIELD}
86 * reporting-mode: continuous
87 *
88 * All values are in micro-Tesla (uT) and measure the geomagnetic
89 * field in the X, Y and Z axis.
90 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070091 ASENSOR_TYPE_MAGNETIC_FIELD = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070092 /**
93 * {@link ASENSOR_TYPE_GYROSCOPE}
94 * reporting-mode: continuous
95 *
96 * All values are in radians/second and measure the rate of rotation
97 * around the X, Y and Z axis.
98 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070099 ASENSOR_TYPE_GYROSCOPE = 4,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700100 /**
101 * {@link ASENSOR_TYPE_LIGHT}
102 * reporting-mode: on-change
103 *
104 * The light sensor value is returned in SI lux units.
105 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700106 ASENSOR_TYPE_LIGHT = 5,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700107 /**
108 * {@link ASENSOR_TYPE_PROXIMITY}
109 * reporting-mode: on-change
110 *
111 * The proximity sensor which turns the screen off and back on during calls is the
112 * wake-up proximity sensor. Implement wake-up proximity sensor before implementing
113 * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag
114 * SENSOR_FLAG_WAKE_UP.
115 * The value corresponds to the distance to the nearest object in centimeters.
116 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700117 ASENSOR_TYPE_PROXIMITY = 8,
118 /**
119 * {@link ASENSOR_TYPE_LINEAR_ACCELERATION}
120 * reporting-mode: continuous
121 *
122 * All values are in SI units (m/s^2) and measure the acceleration of the
123 * device not including the force of gravity.
124 */
Peng Xu37317b62017-03-07 17:49:31 -0800125 ASENSOR_TYPE_LINEAR_ACCELERATION = 10
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700126};
127
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700128/**
129 * Sensor accuracy measure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700130 */
131enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700132 /** no contact */
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700133 ASENSOR_STATUS_NO_CONTACT = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700134 /** unreliable */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700135 ASENSOR_STATUS_UNRELIABLE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700136 /** low accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700137 ASENSOR_STATUS_ACCURACY_LOW = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700138 /** medium accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700139 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700140 /** high accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700141 ASENSOR_STATUS_ACCURACY_HIGH = 3
142};
143
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700144/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700145 * Sensor Reporting Modes.
146 */
147enum {
Peng Xu37317b62017-03-07 17:49:31 -0800148 /** invalid reporting mode */
149 AREPORTING_MODE_INVALID = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700150 /** continuous reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700151 AREPORTING_MODE_CONTINUOUS = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700152 /** reporting on change */
Aravind Akella0e025c52014-06-03 19:19:57 -0700153 AREPORTING_MODE_ON_CHANGE = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700154 /** on shot reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700155 AREPORTING_MODE_ONE_SHOT = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700156 /** special trigger reporting */
Peng Xu37317b62017-03-07 17:49:31 -0800157 AREPORTING_MODE_SPECIAL_TRIGGER = 3
Aravind Akella0e025c52014-06-03 19:19:57 -0700158};
159
Peng Xu47cddca2017-02-15 23:31:22 -0800160/**
161 * Sensor Direct Report Rates.
162 */
163enum {
164 /** stopped */
165 ASENSOR_DIRECT_RATE_STOP = 0,
166 /** nominal 50Hz */
167 ASENSOR_DIRECT_RATE_NORMAL = 1,
168 /** nominal 200Hz */
169 ASENSOR_DIRECT_RATE_FAST = 2,
170 /** nominal 800Hz */
171 ASENSOR_DIRECT_RATE_VERY_FAST = 3
172};
173
174/**
175 * Sensor Direct Channel Type.
176 */
177enum {
178 /** shared memory created by ASharedMemory_create */
179 ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY = 1,
180 /** AHardwareBuffer */
181 ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER = 2
182};
183
Aravind Akella0e025c52014-06-03 19:19:57 -0700184/*
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700185 * A few useful constants
186 */
187
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700188/** Earth's gravity in m/s^2 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700189#define ASENSOR_STANDARD_GRAVITY (9.80665f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700190/** Maximum magnetic field on Earth's surface in uT */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700191#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700192/** Minimum magnetic field on Earth's surface in uT*/
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700193#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
194
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700195/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700196 * A sensor event.
197 */
198
199/* NOTE: Must match hardware/sensors.h */
200typedef struct ASensorVector {
201 union {
202 float v[3];
203 struct {
204 float x;
205 float y;
206 float z;
207 };
208 struct {
209 float azimuth;
210 float pitch;
211 float roll;
212 };
213 };
214 int8_t status;
215 uint8_t reserved[3];
216} ASensorVector;
217
Aravind Akella724d91d2013-06-27 12:04:23 -0700218typedef struct AMetaDataEvent {
219 int32_t what;
220 int32_t sensor;
221} AMetaDataEvent;
222
223typedef struct AUncalibratedEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800224 union {
225 float uncalib[3];
226 struct {
227 float x_uncalib;
228 float y_uncalib;
229 float z_uncalib;
230 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700231 };
Peng Xu9e720462016-01-26 18:48:54 -0800232 union {
233 float bias[3];
234 struct {
235 float x_bias;
236 float y_bias;
237 float z_bias;
238 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700239 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700240} AUncalibratedEvent;
241
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700242typedef struct AHeartRateEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800243 float bpm;
244 int8_t status;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700245} AHeartRateEvent;
246
Peng Xu2576cb62016-01-20 00:22:09 -0800247typedef struct ADynamicSensorEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800248 int32_t connected;
249 int32_t handle;
Peng Xu2576cb62016-01-20 00:22:09 -0800250} ADynamicSensorEvent;
251
Peng Xu9e720462016-01-26 18:48:54 -0800252typedef struct {
253 int32_t type;
254 int32_t serial;
255 union {
256 int32_t data_int32[14];
257 float data_float[14];
258 };
259} AAdditionalInfoEvent;
260
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700261/* NOTE: Must match hardware/sensors.h */
262typedef struct ASensorEvent {
263 int32_t version; /* sizeof(struct ASensorEvent) */
264 int32_t sensor;
265 int32_t type;
266 int32_t reserved0;
267 int64_t timestamp;
268 union {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700269 union {
270 float data[16];
271 ASensorVector vector;
272 ASensorVector acceleration;
273 ASensorVector magnetic;
274 float temperature;
275 float distance;
276 float light;
277 float pressure;
Aravind Akella724d91d2013-06-27 12:04:23 -0700278 float relative_humidity;
279 AUncalibratedEvent uncalibrated_gyro;
280 AUncalibratedEvent uncalibrated_magnetic;
281 AMetaDataEvent meta_data;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700282 AHeartRateEvent heart_rate;
Peng Xu2576cb62016-01-20 00:22:09 -0800283 ADynamicSensorEvent dynamic_sensor_meta;
Peng Xu9e720462016-01-26 18:48:54 -0800284 AAdditionalInfoEvent additional_info;
Mathias Agopianba02cd22013-07-03 16:20:57 -0700285 };
286 union {
287 uint64_t data[8];
288 uint64_t step_counter;
289 } u64;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700290 };
Aravind Akella9a844cf2014-02-11 18:58:52 -0800291
292 uint32_t flags;
293 int32_t reserved1[3];
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700294} ASensorEvent;
295
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700296struct ASensorManager;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700297/**
298 * {@link ASensorManager} is an opaque type to manage sensors and
299 * events queues.
300 *
301 * {@link ASensorManager} is a singleton that can be obtained using
302 * ASensorManager_getInstance().
303 *
304 * This file provides a set of functions that uses {@link
305 * ASensorManager} to access and list hardware sensors, and
306 * create and destroy event queues:
307 * - ASensorManager_getSensorList()
308 * - ASensorManager_getDefaultSensor()
309 * - ASensorManager_getDefaultSensorEx()
310 * - ASensorManager_createEventQueue()
311 * - ASensorManager_destroyEventQueue()
312 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700313typedef struct ASensorManager ASensorManager;
314
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700315
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700316struct ASensorEventQueue;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700317/**
318 * {@link ASensorEventQueue} is an opaque type that provides access to
319 * {@link ASensorEvent} from hardware sensors.
320 *
321 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue().
322 *
323 * This file provides a set of functions to enable and disable
324 * sensors, check and get events, and set event rates on a {@link
325 * ASensorEventQueue}.
326 * - ASensorEventQueue_enableSensor()
327 * - ASensorEventQueue_disableSensor()
328 * - ASensorEventQueue_hasEvents()
329 * - ASensorEventQueue_getEvents()
330 * - ASensorEventQueue_setEventRate()
331 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700332typedef struct ASensorEventQueue ASensorEventQueue;
333
334struct ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700335/**
336 * {@link ASensor} is an opaque type that provides information about
337 * an hardware sensors.
338 *
339 * A {@link ASensor} pointer can be obtained using
340 * ASensorManager_getDefaultSensor(),
341 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}.
342 *
343 * This file provides a set of functions to access properties of a
344 * {@link ASensor}:
345 * - ASensor_getName()
346 * - ASensor_getVendor()
347 * - ASensor_getType()
348 * - ASensor_getResolution()
349 * - ASensor_getMinDelay()
350 * - ASensor_getFifoMaxEventCount()
351 * - ASensor_getFifoReservedEventCount()
352 * - ASensor_getStringType()
353 * - ASensor_getReportingMode()
354 * - ASensor_isWakeUpSensor()
355 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700356typedef struct ASensor ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700357/**
358 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}.
359 *
360 * This is used to define entry in {@link ASensorList} arrays.
361 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700362typedef ASensor const* ASensorRef;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700363/**
364 * {@link ASensorList} is an array of reference to {@link ASensor}.
365 *
366 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList().
367 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700368typedef ASensorRef const* ASensorList;
369
370/*****************************************************************************/
371
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700372/**
Svet Ganov5fa32d42015-05-07 10:50:59 -0700373 * Get a reference to the sensor manager. ASensorManager is a singleton
374 * per package as different packages may have access to different sensors.
375 *
376 * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead.
377 *
378 * Example:
379 *
380 * ASensorManager* sensorManager = ASensorManager_getInstance();
381 *
382 */
383__attribute__ ((deprecated)) ASensorManager* ASensorManager_getInstance();
384
385/*
386 * Get a reference to the sensor manager. ASensorManager is a singleton
387 * per package as different packages may have access to different sensors.
388 *
389 * Example:
390 *
391 * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz");
392 *
393 */
394ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName);
395
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700396/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700397 * Returns the list of available sensors.
398 */
399int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list);
400
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700401/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700402 * Returns the default sensor for the given type, or NULL if no sensor
Aravind Akellab37ba392014-08-05 14:53:07 -0700403 * of that type exists.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700404 */
405ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
406
Dan Albert494ed552016-09-23 15:57:45 -0700407#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700408/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700409 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor
410 * of this type and wakeUp properties exists.
411 */
Peng Xuda8385c2017-02-28 20:19:47 -0800412ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type, bool wakeUp);
Dan Albert494ed552016-09-23 15:57:45 -0700413#endif
Aravind Akellab37ba392014-08-05 14:53:07 -0700414
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700415/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700416 * Creates a new sensor event queue and associate it with a looper.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700417 *
418 * "ident" is a identifier for the events that will be returned when
419 * calling ALooper_pollOnce(). The identifier must be >= 0, or
420 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700421 */
422ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
423 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
424
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700425/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700426 * Destroys the event queue and free all resources associated to it.
427 */
428int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
429
Peng Xu47cddca2017-02-15 23:31:22 -0800430#if __ANDROID_API__ >= __ANDROID_API_O__
431/**
432 * Create direct channel based on shared memory
433 *
434 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} to be used
435 * for configuring sensor direct report.
436 *
437 * \param manager the {@link ASensorManager} instance obtained from
438 * {@link ASensorManager_getInstanceForPackage}.
439 * \param fd file descriptor representing a shared memory created by
440 * {@link ASharedMemory_create}
441 * \param size size to be used, must be less or equal to size of shared memory.
442 *
443 * \return a positive integer as a channel id to be used in
444 * {@link ASensorManager_destroyDirectChannel} and
445 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
446 */
447int ASensorManager_createSharedMemoryDirectChannel(ASensorManager* manager, int fd, size_t size);
448
449/**
450 * Create direct channel based on AHardwareBuffer
451 *
452 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER} type to be used
453 * for configuring sensor direct report.
454 *
455 * \param manager the {@link ASensorManager} instance obtained from
456 * {@link ASensorManager_getInstanceForPackage}.
457 * \param buffer {@link AHardwareBuffer} instance created by {@link AHardwareBuffer_allocate}.
458 * \param size the intended size to be used, must be less or equal to size of buffer.
459 *
460 * \return a positive integer as a channel id to be used in
461 * {@link ASensorManager_destroyDirectChannel} and
462 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
463 */
464int ASensorManager_createHardwareBufferDirectChannel(
465 ASensorManager* manager, AHardwareBuffer const * buffer, size_t size);
466
467/**
468 * Destroy a direct channel
469 *
470 * Destroy a direct channel previously created using {@link ASensorManager_createDirectChannel}.
471 * The buffer used for creating direct channel does not get destroyed with
472 * {@link ASensorManager_destroy} and has to be close or released separately.
473 *
474 * \param manager the {@link ASensorManager} instance obtained from
475 * {@link ASensorManager_getInstanceForPackage}.
476 * \param channelId channel id (a positive integer) returned from
477 * {@link ASensorManager_createSharedMemoryDirectChannel} or
478 * {@link ASensorManager_createHardwareBufferDirectChannel}.
479 */
480void ASensorManager_destroyDirectChannel(ASensorManager* manager, int channelId);
481
482/**
483 * Configure direct report on channel
484 *
485 * Configure sensor direct report on a direct channel: set rate to value other than
486 * {@link ASENSOR_DIRECT_RATE_STOP} so that sensor event can be directly
Peng Xuec53d022017-04-06 18:02:29 -0700487 * written into the shared memory region used for creating the buffer. It returns a positive token
488 * which can be used for identify sensor events from different sensors on success. Calling with rate
489 * {@link ASENSOR_DIRECT_RATE_STOP} will stop direct report of the sensor specified in the channel.
Peng Xu47cddca2017-02-15 23:31:22 -0800490 *
491 * To stop all active sensor direct report configured to a channel, set sensor to NULL and rate to
492 * {@link ASENSOR_DIRECT_RATE_STOP}.
493 *
494 * In order to successfully configure a direct report, the sensor has to support the specified rate
495 * and the channel type, which can be checked by {@link ASensor_getHighestDirectReportRateLevel} and
496 * {@link ASensor_isDirectChannelTypeSupported}, respectively.
497 *
498 * Example:
499 * \code{.cpp}
500 * ASensorManager *manager = ...;
501 * ASensor *sensor = ...;
502 * int channelId = ...;
503 *
504 * ASensorManager_configureDirectReport(
505 * manager, sensor, channel_id, ASENSOR_DIRECT_RATE_FAST);
506 * \endcode
507 *
508 * \param manager the {@link ASensorManager} instance obtained from
509 * {@link ASensorManager_getInstanceForPackage}.
510 * \param sensor a {@link ASensor} to denote which sensor to be operate. It can be NULL if rate
511 * is {@link ASENSOR_DIRECT_RATE_STOP}, denoting stopping of all active sensor
512 * direct report.
513 * \param channelId channel id (a positive integer) returned from
514 * {@link ASensorManager_createSharedMemoryDirectChannel} or
515 * {@link ASensorManager_createHardwareBufferDirectChannel}.
516 *
Peng Xuec53d022017-04-06 18:02:29 -0700517 * \return positive token for success or negative error code.
Peng Xu47cddca2017-02-15 23:31:22 -0800518 */
519int ASensorManager_configureDirectReport(
520 ASensorManager* manager, ASensor const* sensor, int channelId, int rate);
521#endif
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700522
523/*****************************************************************************/
524
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700525/**
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530526 * Enable the selected sensor with a specified sampling period and max batch report latency.
527 * Returns a negative error code on failure.
Aniroop Mathure96e5772016-12-13 00:04:06 +0530528 * Note: To disable the selected sensor, use ASensorEventQueue_disableSensor() same as before.
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530529 */
530int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor,
Peng Xuda8385c2017-02-28 20:19:47 -0800531 int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs);
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530532
533/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700534 * Enable the selected sensor. Returns a negative error code on failure.
535 */
536int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor);
537
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700538/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700539 * Disable the selected sensor. Returns a negative error code on failure.
540 */
541int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor);
542
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700543/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700544 * Sets the delivery rate of events in microseconds for the given sensor.
545 * Note that this is a hint only, generally event will arrive at a higher
546 * rate. It is an error to set a rate inferior to the value returned by
547 * ASensor_getMinDelay().
548 * Returns a negative error code on failure.
549 */
550int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
551
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700552/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700553 * Returns true if there are one or more events available in the
554 * sensor queue. Returns 1 if the queue has events; 0 if
555 * it does not have events; and a negative value if there is an error.
556 */
557int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
558
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700559/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700560 * Returns the next available events from the queue. Returns a negative
561 * value if no events are available or an error has occurred, otherwise
562 * the number of events returned.
563 *
564 * Examples:
565 * ASensorEvent event;
566 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
567 *
568 * ASensorEvent eventBuffer[8];
569 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
570 *
571 */
Peng Xuda8385c2017-02-28 20:19:47 -0800572ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, ASensorEvent* events, size_t count);
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700573
574
575/*****************************************************************************/
576
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700577/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700578 * Returns this sensor's name (non localized)
579 */
580const char* ASensor_getName(ASensor const* sensor);
581
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700582/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700583 * Returns this sensor's vendor's name (non localized)
584 */
585const char* ASensor_getVendor(ASensor const* sensor);
586
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700587/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700588 * Return this sensor's type
589 */
590int ASensor_getType(ASensor const* sensor);
591
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700592/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700593 * Returns this sensors's resolution
594 */
595float ASensor_getResolution(ASensor const* sensor);
596
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700597/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700598 * Returns the minimum delay allowed between events in microseconds.
599 * A value of zero means that this sensor doesn't report events at a
600 * constant rate, but rather only when a new data is available.
601 */
602int ASensor_getMinDelay(ASensor const* sensor);
603
Dan Albert494ed552016-09-23 15:57:45 -0700604#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700605/**
Aravind Akella70018042014-04-07 22:52:37 +0000606 * Returns the maximum size of batches for this sensor. Batches will often be
607 * smaller, as the hardware fifo might be used for other sensors.
608 */
609int ASensor_getFifoMaxEventCount(ASensor const* sensor);
610
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700611/**
Aravind Akella70018042014-04-07 22:52:37 +0000612 * Returns the hardware batch fifo size reserved to this sensor.
613 */
614int ASensor_getFifoReservedEventCount(ASensor const* sensor);
615
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700616/**
Aravind Akella70018042014-04-07 22:52:37 +0000617 * Returns this sensor's string type.
618 */
619const char* ASensor_getStringType(ASensor const* sensor);
620
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700621/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700622 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants.
623 */
624int ASensor_getReportingMode(ASensor const* sensor);
625
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700626/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700627 * Returns true if this is a wake up sensor, false otherwise.
628 */
629bool ASensor_isWakeUpSensor(ASensor const* sensor);
Dan Albert494ed552016-09-23 15:57:45 -0700630#endif /* __ANDROID_API__ >= 21 */
Aravind Akellab37ba392014-08-05 14:53:07 -0700631
Peng Xu47cddca2017-02-15 23:31:22 -0800632#if __ANDROID_API__ >= __ANDROID_API_O__
633/**
634 * Test if sensor supports a certain type of direct channel.
635 *
636 * \param sensor a {@link ASensor} to denote the sensor to be checked.
637 * \param channelType Channel type constant, either
638 * {@ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY}
639 * or {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER}.
640 * \returns true if sensor supports the specified direct channel type.
641 */
642bool ASensor_isDirectChannelTypeSupported(ASensor const* sensor, int channelType);
643/**
644 * Get the highest direct rate level that a sensor support.
645 *
646 * \param sensor a {@link ASensor} to denote the sensor to be checked.
647 *
648 * \return a ASENSOR_DIRECT_RATE_... enum denoting the highest rate level supported by the sensor.
649 * If return value is {@link ASENSOR_DIRECT_RATE_STOP}, it means the sensor
650 * does not support direct report.
651 */
652int ASensor_getHighestDirectReportRateLevel(ASensor const* sensor);
653#endif
654
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700655#ifdef __cplusplus
656};
657#endif
658
659#endif // ANDROID_SENSOR_H
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700660
661/** @} */