blob: a5e5c057f81fe6f148a37783cb650aa9c785d78b [file] [log] [blame]
Mathias Agopiane1c61d32012-03-23 14:19:36 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070017/**
18 * @addtogroup Sensor
19 * @{
20 */
21
22/**
23 * @file sensor.h
24 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070025
26#ifndef ANDROID_SENSOR_H
27#define ANDROID_SENSOR_H
28
29/******************************************************************
30 *
31 * IMPORTANT NOTICE:
32 *
33 * This file is part of Android's set of stable system headers
34 * exposed by the Android NDK (Native Development Kit).
35 *
36 * Third-party source AND binary code relies on the definitions
37 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
38 *
39 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
40 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
41 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
42 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
43 */
44
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070045/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -070046 * Structures and functions to receive and process sensor events in
47 * native code.
48 *
49 */
50
51#include <sys/types.h>
52
53#include <android/looper.h>
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
Peng Xu47cddca2017-02-15 23:31:22 -080059typedef struct AHardwareBuffer AHardwareBuffer;
Mathias Agopiane1c61d32012-03-23 14:19:36 -070060
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070061/**
62 * Sensor types.
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070063 * (keep in sync with hardware/sensors.h)
Mathias Agopiane1c61d32012-03-23 14:19:36 -070064 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070065enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070066 /**
67 * {@link ASENSOR_TYPE_ACCELEROMETER}
68 * reporting-mode: continuous
69 *
70 * All values are in SI units (m/s^2) and measure the acceleration of the
71 * device minus the force of gravity.
72 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070073 ASENSOR_TYPE_ACCELEROMETER = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070074 /**
75 * {@link ASENSOR_TYPE_MAGNETIC_FIELD}
76 * reporting-mode: continuous
77 *
78 * All values are in micro-Tesla (uT) and measure the geomagnetic
79 * field in the X, Y and Z axis.
80 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070081 ASENSOR_TYPE_MAGNETIC_FIELD = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070082 /**
83 * {@link ASENSOR_TYPE_GYROSCOPE}
84 * reporting-mode: continuous
85 *
86 * All values are in radians/second and measure the rate of rotation
87 * around the X, Y and Z axis.
88 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070089 ASENSOR_TYPE_GYROSCOPE = 4,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070090 /**
91 * {@link ASENSOR_TYPE_LIGHT}
92 * reporting-mode: on-change
93 *
94 * The light sensor value is returned in SI lux units.
95 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070096 ASENSOR_TYPE_LIGHT = 5,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070097 /**
98 * {@link ASENSOR_TYPE_PROXIMITY}
99 * reporting-mode: on-change
100 *
101 * The proximity sensor which turns the screen off and back on during calls is the
102 * wake-up proximity sensor. Implement wake-up proximity sensor before implementing
103 * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag
104 * SENSOR_FLAG_WAKE_UP.
105 * The value corresponds to the distance to the nearest object in centimeters.
106 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700107 ASENSOR_TYPE_PROXIMITY = 8,
108 /**
109 * {@link ASENSOR_TYPE_LINEAR_ACCELERATION}
110 * reporting-mode: continuous
111 *
112 * All values are in SI units (m/s^2) and measure the acceleration of the
113 * device not including the force of gravity.
114 */
115 ASENSOR_TYPE_LINEAR_ACCELERATION = 10
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700116};
117
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700118/**
119 * Sensor accuracy measure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700120 */
121enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700122 /** no contact */
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700123 ASENSOR_STATUS_NO_CONTACT = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700124 /** unreliable */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700125 ASENSOR_STATUS_UNRELIABLE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700126 /** low accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700127 ASENSOR_STATUS_ACCURACY_LOW = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700128 /** medium accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700129 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700130 /** high accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700131 ASENSOR_STATUS_ACCURACY_HIGH = 3
132};
133
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700134/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700135 * Sensor Reporting Modes.
136 */
137enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700138 /** continuous reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700139 AREPORTING_MODE_CONTINUOUS = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700140 /** reporting on change */
Aravind Akella0e025c52014-06-03 19:19:57 -0700141 AREPORTING_MODE_ON_CHANGE = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700142 /** on shot reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700143 AREPORTING_MODE_ONE_SHOT = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700144 /** special trigger reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700145 AREPORTING_MODE_SPECIAL_TRIGGER = 3
146};
147
Peng Xu47cddca2017-02-15 23:31:22 -0800148/**
149 * Sensor Direct Report Rates.
150 */
151enum {
152 /** stopped */
153 ASENSOR_DIRECT_RATE_STOP = 0,
154 /** nominal 50Hz */
155 ASENSOR_DIRECT_RATE_NORMAL = 1,
156 /** nominal 200Hz */
157 ASENSOR_DIRECT_RATE_FAST = 2,
158 /** nominal 800Hz */
159 ASENSOR_DIRECT_RATE_VERY_FAST = 3
160};
161
162/**
163 * Sensor Direct Channel Type.
164 */
165enum {
166 /** shared memory created by ASharedMemory_create */
167 ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY = 1,
168 /** AHardwareBuffer */
169 ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER = 2
170};
171
Aravind Akella0e025c52014-06-03 19:19:57 -0700172/*
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700173 * A few useful constants
174 */
175
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700176/** Earth's gravity in m/s^2 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700177#define ASENSOR_STANDARD_GRAVITY (9.80665f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700178/** Maximum magnetic field on Earth's surface in uT */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700179#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700180/** Minimum magnetic field on Earth's surface in uT*/
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700181#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
182
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700183/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700184 * A sensor event.
185 */
186
187/* NOTE: Must match hardware/sensors.h */
188typedef struct ASensorVector {
189 union {
190 float v[3];
191 struct {
192 float x;
193 float y;
194 float z;
195 };
196 struct {
197 float azimuth;
198 float pitch;
199 float roll;
200 };
201 };
202 int8_t status;
203 uint8_t reserved[3];
204} ASensorVector;
205
Aravind Akella724d91d2013-06-27 12:04:23 -0700206typedef struct AMetaDataEvent {
207 int32_t what;
208 int32_t sensor;
209} AMetaDataEvent;
210
211typedef struct AUncalibratedEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800212 union {
213 float uncalib[3];
214 struct {
215 float x_uncalib;
216 float y_uncalib;
217 float z_uncalib;
218 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700219 };
Peng Xu9e720462016-01-26 18:48:54 -0800220 union {
221 float bias[3];
222 struct {
223 float x_bias;
224 float y_bias;
225 float z_bias;
226 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700227 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700228} AUncalibratedEvent;
229
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700230typedef struct AHeartRateEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800231 float bpm;
232 int8_t status;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700233} AHeartRateEvent;
234
Peng Xu2576cb62016-01-20 00:22:09 -0800235typedef struct ADynamicSensorEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800236 int32_t connected;
237 int32_t handle;
Peng Xu2576cb62016-01-20 00:22:09 -0800238} ADynamicSensorEvent;
239
Peng Xu9e720462016-01-26 18:48:54 -0800240typedef struct {
241 int32_t type;
242 int32_t serial;
243 union {
244 int32_t data_int32[14];
245 float data_float[14];
246 };
247} AAdditionalInfoEvent;
248
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700249/* NOTE: Must match hardware/sensors.h */
250typedef struct ASensorEvent {
251 int32_t version; /* sizeof(struct ASensorEvent) */
252 int32_t sensor;
253 int32_t type;
254 int32_t reserved0;
255 int64_t timestamp;
256 union {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700257 union {
258 float data[16];
259 ASensorVector vector;
260 ASensorVector acceleration;
261 ASensorVector magnetic;
262 float temperature;
263 float distance;
264 float light;
265 float pressure;
Aravind Akella724d91d2013-06-27 12:04:23 -0700266 float relative_humidity;
267 AUncalibratedEvent uncalibrated_gyro;
268 AUncalibratedEvent uncalibrated_magnetic;
269 AMetaDataEvent meta_data;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700270 AHeartRateEvent heart_rate;
Peng Xu2576cb62016-01-20 00:22:09 -0800271 ADynamicSensorEvent dynamic_sensor_meta;
Peng Xu9e720462016-01-26 18:48:54 -0800272 AAdditionalInfoEvent additional_info;
Mathias Agopianba02cd22013-07-03 16:20:57 -0700273 };
274 union {
275 uint64_t data[8];
276 uint64_t step_counter;
277 } u64;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700278 };
Aravind Akella9a844cf2014-02-11 18:58:52 -0800279
280 uint32_t flags;
281 int32_t reserved1[3];
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700282} ASensorEvent;
283
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700284struct ASensorManager;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700285/**
286 * {@link ASensorManager} is an opaque type to manage sensors and
287 * events queues.
288 *
289 * {@link ASensorManager} is a singleton that can be obtained using
290 * ASensorManager_getInstance().
291 *
292 * This file provides a set of functions that uses {@link
293 * ASensorManager} to access and list hardware sensors, and
294 * create and destroy event queues:
295 * - ASensorManager_getSensorList()
296 * - ASensorManager_getDefaultSensor()
297 * - ASensorManager_getDefaultSensorEx()
298 * - ASensorManager_createEventQueue()
299 * - ASensorManager_destroyEventQueue()
300 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700301typedef struct ASensorManager ASensorManager;
302
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700303
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700304struct ASensorEventQueue;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700305/**
306 * {@link ASensorEventQueue} is an opaque type that provides access to
307 * {@link ASensorEvent} from hardware sensors.
308 *
309 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue().
310 *
311 * This file provides a set of functions to enable and disable
312 * sensors, check and get events, and set event rates on a {@link
313 * ASensorEventQueue}.
314 * - ASensorEventQueue_enableSensor()
315 * - ASensorEventQueue_disableSensor()
316 * - ASensorEventQueue_hasEvents()
317 * - ASensorEventQueue_getEvents()
318 * - ASensorEventQueue_setEventRate()
319 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700320typedef struct ASensorEventQueue ASensorEventQueue;
321
322struct ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700323/**
324 * {@link ASensor} is an opaque type that provides information about
325 * an hardware sensors.
326 *
327 * A {@link ASensor} pointer can be obtained using
328 * ASensorManager_getDefaultSensor(),
329 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}.
330 *
331 * This file provides a set of functions to access properties of a
332 * {@link ASensor}:
333 * - ASensor_getName()
334 * - ASensor_getVendor()
335 * - ASensor_getType()
336 * - ASensor_getResolution()
337 * - ASensor_getMinDelay()
338 * - ASensor_getFifoMaxEventCount()
339 * - ASensor_getFifoReservedEventCount()
340 * - ASensor_getStringType()
341 * - ASensor_getReportingMode()
342 * - ASensor_isWakeUpSensor()
343 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700344typedef struct ASensor ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700345/**
346 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}.
347 *
348 * This is used to define entry in {@link ASensorList} arrays.
349 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700350typedef ASensor const* ASensorRef;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700351/**
352 * {@link ASensorList} is an array of reference to {@link ASensor}.
353 *
354 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList().
355 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700356typedef ASensorRef const* ASensorList;
357
358/*****************************************************************************/
359
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700360/**
Svet Ganov5fa32d42015-05-07 10:50:59 -0700361 * Get a reference to the sensor manager. ASensorManager is a singleton
362 * per package as different packages may have access to different sensors.
363 *
364 * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead.
365 *
366 * Example:
367 *
368 * ASensorManager* sensorManager = ASensorManager_getInstance();
369 *
370 */
371__attribute__ ((deprecated)) ASensorManager* ASensorManager_getInstance();
372
373/*
374 * Get a reference to the sensor manager. ASensorManager is a singleton
375 * per package as different packages may have access to different sensors.
376 *
377 * Example:
378 *
379 * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz");
380 *
381 */
382ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName);
383
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700384/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700385 * Returns the list of available sensors.
386 */
387int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list);
388
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700389/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700390 * Returns the default sensor for the given type, or NULL if no sensor
Aravind Akellab37ba392014-08-05 14:53:07 -0700391 * of that type exists.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700392 */
393ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
394
Dan Albert494ed552016-09-23 15:57:45 -0700395#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700396/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700397 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor
398 * of this type and wakeUp properties exists.
399 */
400ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type,
401 bool wakeUp);
Dan Albert494ed552016-09-23 15:57:45 -0700402#endif
Aravind Akellab37ba392014-08-05 14:53:07 -0700403
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700404/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700405 * Creates a new sensor event queue and associate it with a looper.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700406 *
407 * "ident" is a identifier for the events that will be returned when
408 * calling ALooper_pollOnce(). The identifier must be >= 0, or
409 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700410 */
411ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
412 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
413
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700414/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700415 * Destroys the event queue and free all resources associated to it.
416 */
417int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
418
Peng Xu47cddca2017-02-15 23:31:22 -0800419#if __ANDROID_API__ >= __ANDROID_API_O__
420/**
421 * Create direct channel based on shared memory
422 *
423 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} to be used
424 * for configuring sensor direct report.
425 *
426 * \param manager the {@link ASensorManager} instance obtained from
427 * {@link ASensorManager_getInstanceForPackage}.
428 * \param fd file descriptor representing a shared memory created by
429 * {@link ASharedMemory_create}
430 * \param size size to be used, must be less or equal to size of shared memory.
431 *
432 * \return a positive integer as a channel id to be used in
433 * {@link ASensorManager_destroyDirectChannel} and
434 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
435 */
436int ASensorManager_createSharedMemoryDirectChannel(ASensorManager* manager, int fd, size_t size);
437
438/**
439 * Create direct channel based on AHardwareBuffer
440 *
441 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER} type to be used
442 * for configuring sensor direct report.
443 *
444 * \param manager the {@link ASensorManager} instance obtained from
445 * {@link ASensorManager_getInstanceForPackage}.
446 * \param buffer {@link AHardwareBuffer} instance created by {@link AHardwareBuffer_allocate}.
447 * \param size the intended size to be used, must be less or equal to size of buffer.
448 *
449 * \return a positive integer as a channel id to be used in
450 * {@link ASensorManager_destroyDirectChannel} and
451 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
452 */
453int ASensorManager_createHardwareBufferDirectChannel(
454 ASensorManager* manager, AHardwareBuffer const * buffer, size_t size);
455
456/**
457 * Destroy a direct channel
458 *
459 * Destroy a direct channel previously created using {@link ASensorManager_createDirectChannel}.
460 * The buffer used for creating direct channel does not get destroyed with
461 * {@link ASensorManager_destroy} and has to be close or released separately.
462 *
463 * \param manager the {@link ASensorManager} instance obtained from
464 * {@link ASensorManager_getInstanceForPackage}.
465 * \param channelId channel id (a positive integer) returned from
466 * {@link ASensorManager_createSharedMemoryDirectChannel} or
467 * {@link ASensorManager_createHardwareBufferDirectChannel}.
468 */
469void ASensorManager_destroyDirectChannel(ASensorManager* manager, int channelId);
470
471/**
472 * Configure direct report on channel
473 *
474 * Configure sensor direct report on a direct channel: set rate to value other than
475 * {@link ASENSOR_DIRECT_RATE_STOP} so that sensor event can be directly
476 * written into the shared memory region used for creating the buffer; set rate to
477 * {@link ASENSOR_DIRECT_RATE_STOP} will stop the sensor direct report.
478 *
479 * To stop all active sensor direct report configured to a channel, set sensor to NULL and rate to
480 * {@link ASENSOR_DIRECT_RATE_STOP}.
481 *
482 * In order to successfully configure a direct report, the sensor has to support the specified rate
483 * and the channel type, which can be checked by {@link ASensor_getHighestDirectReportRateLevel} and
484 * {@link ASensor_isDirectChannelTypeSupported}, respectively.
485 *
486 * Example:
487 * \code{.cpp}
488 * ASensorManager *manager = ...;
489 * ASensor *sensor = ...;
490 * int channelId = ...;
491 *
492 * ASensorManager_configureDirectReport(
493 * manager, sensor, channel_id, ASENSOR_DIRECT_RATE_FAST);
494 * \endcode
495 *
496 * \param manager the {@link ASensorManager} instance obtained from
497 * {@link ASensorManager_getInstanceForPackage}.
498 * \param sensor a {@link ASensor} to denote which sensor to be operate. It can be NULL if rate
499 * is {@link ASENSOR_DIRECT_RATE_STOP}, denoting stopping of all active sensor
500 * direct report.
501 * \param channelId channel id (a positive integer) returned from
502 * {@link ASensorManager_createSharedMemoryDirectChannel} or
503 * {@link ASensorManager_createHardwareBufferDirectChannel}.
504 *
505 * \return 0 for success or negative integer for failure.
506 */
507int ASensorManager_configureDirectReport(
508 ASensorManager* manager, ASensor const* sensor, int channelId, int rate);
509#endif
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700510
511/*****************************************************************************/
512
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700513/**
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530514 * Enable the selected sensor with a specified sampling period and max batch report latency.
515 * Returns a negative error code on failure.
Aniroop Mathure96e5772016-12-13 00:04:06 +0530516 * Note: To disable the selected sensor, use ASensorEventQueue_disableSensor() same as before.
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530517 */
518int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor,
519 int32_t samplingPeriodUs, int maxBatchReportLatencyUs);
520
521/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700522 * Enable the selected sensor. Returns a negative error code on failure.
523 */
524int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor);
525
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700526/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700527 * Disable the selected sensor. Returns a negative error code on failure.
528 */
529int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor);
530
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700531/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700532 * Sets the delivery rate of events in microseconds for the given sensor.
533 * Note that this is a hint only, generally event will arrive at a higher
534 * rate. It is an error to set a rate inferior to the value returned by
535 * ASensor_getMinDelay().
536 * Returns a negative error code on failure.
537 */
538int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
539
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700540/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700541 * Returns true if there are one or more events available in the
542 * sensor queue. Returns 1 if the queue has events; 0 if
543 * it does not have events; and a negative value if there is an error.
544 */
545int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
546
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700547/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700548 * Returns the next available events from the queue. Returns a negative
549 * value if no events are available or an error has occurred, otherwise
550 * the number of events returned.
551 *
552 * Examples:
553 * ASensorEvent event;
554 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
555 *
556 * ASensorEvent eventBuffer[8];
557 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
558 *
559 */
560ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
561 ASensorEvent* events, size_t count);
562
563
564/*****************************************************************************/
565
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700566/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700567 * Returns this sensor's name (non localized)
568 */
569const char* ASensor_getName(ASensor const* sensor);
570
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700571/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700572 * Returns this sensor's vendor's name (non localized)
573 */
574const char* ASensor_getVendor(ASensor const* sensor);
575
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700576/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700577 * Return this sensor's type
578 */
579int ASensor_getType(ASensor const* sensor);
580
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700581/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700582 * Returns this sensors's resolution
583 */
584float ASensor_getResolution(ASensor const* sensor);
585
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700586/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700587 * Returns the minimum delay allowed between events in microseconds.
588 * A value of zero means that this sensor doesn't report events at a
589 * constant rate, but rather only when a new data is available.
590 */
591int ASensor_getMinDelay(ASensor const* sensor);
592
Dan Albert494ed552016-09-23 15:57:45 -0700593#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700594/**
Aravind Akella70018042014-04-07 22:52:37 +0000595 * Returns the maximum size of batches for this sensor. Batches will often be
596 * smaller, as the hardware fifo might be used for other sensors.
597 */
598int ASensor_getFifoMaxEventCount(ASensor const* sensor);
599
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700600/**
Aravind Akella70018042014-04-07 22:52:37 +0000601 * Returns the hardware batch fifo size reserved to this sensor.
602 */
603int ASensor_getFifoReservedEventCount(ASensor const* sensor);
604
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700605/**
Aravind Akella70018042014-04-07 22:52:37 +0000606 * Returns this sensor's string type.
607 */
608const char* ASensor_getStringType(ASensor const* sensor);
609
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700610/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700611 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants.
612 */
613int ASensor_getReportingMode(ASensor const* sensor);
614
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700615/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700616 * Returns true if this is a wake up sensor, false otherwise.
617 */
618bool ASensor_isWakeUpSensor(ASensor const* sensor);
Dan Albert494ed552016-09-23 15:57:45 -0700619#endif /* __ANDROID_API__ >= 21 */
Aravind Akellab37ba392014-08-05 14:53:07 -0700620
Peng Xu47cddca2017-02-15 23:31:22 -0800621#if __ANDROID_API__ >= __ANDROID_API_O__
622/**
623 * Test if sensor supports a certain type of direct channel.
624 *
625 * \param sensor a {@link ASensor} to denote the sensor to be checked.
626 * \param channelType Channel type constant, either
627 * {@ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY}
628 * or {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER}.
629 * \returns true if sensor supports the specified direct channel type.
630 */
631bool ASensor_isDirectChannelTypeSupported(ASensor const* sensor, int channelType);
632/**
633 * Get the highest direct rate level that a sensor support.
634 *
635 * \param sensor a {@link ASensor} to denote the sensor to be checked.
636 *
637 * \return a ASENSOR_DIRECT_RATE_... enum denoting the highest rate level supported by the sensor.
638 * If return value is {@link ASENSOR_DIRECT_RATE_STOP}, it means the sensor
639 * does not support direct report.
640 */
641int ASensor_getHighestDirectReportRateLevel(ASensor const* sensor);
642#endif
643
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700644#ifdef __cplusplus
645};
646#endif
647
648#endif // ANDROID_SENSOR_H
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700649
650/** @} */