blob: 97b4a2ae6089f021c820d4740457e5e4ff59aef5 [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
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070068/**
69 * Sensor types.
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070070 * (keep in sync with hardware/sensors.h)
Mathias Agopiane1c61d32012-03-23 14:19:36 -070071 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070072enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070073 /**
Peng Xu37317b62017-03-07 17:49:31 -080074 * Invalid sensor type. Returned by {@link ASensor_getType} as error value.
75 */
76 ASENSOR_TYPE_INVALID = -1,
77 /**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070078 * {@link ASENSOR_TYPE_ACCELEROMETER}
79 * reporting-mode: continuous
80 *
81 * All values are in SI units (m/s^2) and measure the acceleration of the
82 * device minus the force of gravity.
83 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070084 ASENSOR_TYPE_ACCELEROMETER = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070085 /**
86 * {@link ASENSOR_TYPE_MAGNETIC_FIELD}
87 * reporting-mode: continuous
88 *
89 * All values are in micro-Tesla (uT) and measure the geomagnetic
90 * field in the X, Y and Z axis.
91 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -070092 ASENSOR_TYPE_MAGNETIC_FIELD = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070093 /**
94 * {@link ASENSOR_TYPE_GYROSCOPE}
95 * reporting-mode: continuous
96 *
97 * All values are in radians/second and measure the rate of rotation
98 * around the X, Y and Z axis.
99 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700100 ASENSOR_TYPE_GYROSCOPE = 4,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700101 /**
102 * {@link ASENSOR_TYPE_LIGHT}
103 * reporting-mode: on-change
104 *
105 * The light sensor value is returned in SI lux units.
106 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700107 ASENSOR_TYPE_LIGHT = 5,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700108 /**
109 * {@link ASENSOR_TYPE_PROXIMITY}
110 * reporting-mode: on-change
111 *
112 * The proximity sensor which turns the screen off and back on during calls is the
113 * wake-up proximity sensor. Implement wake-up proximity sensor before implementing
114 * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag
115 * SENSOR_FLAG_WAKE_UP.
116 * The value corresponds to the distance to the nearest object in centimeters.
117 */
Johan Euphrosine7d319fc2015-08-20 18:13:43 -0700118 ASENSOR_TYPE_PROXIMITY = 8,
119 /**
120 * {@link ASENSOR_TYPE_LINEAR_ACCELERATION}
121 * reporting-mode: continuous
122 *
123 * All values are in SI units (m/s^2) and measure the acceleration of the
124 * device not including the force of gravity.
125 */
Peng Xu37317b62017-03-07 17:49:31 -0800126 ASENSOR_TYPE_LINEAR_ACCELERATION = 10
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700127};
128
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700129/**
130 * Sensor accuracy measure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700131 */
132enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700133 /** no contact */
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700134 ASENSOR_STATUS_NO_CONTACT = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700135 /** unreliable */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700136 ASENSOR_STATUS_UNRELIABLE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700137 /** low accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700138 ASENSOR_STATUS_ACCURACY_LOW = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700139 /** medium accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700140 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700141 /** high accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700142 ASENSOR_STATUS_ACCURACY_HIGH = 3
143};
144
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700145/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700146 * Sensor Reporting Modes.
147 */
148enum {
Peng Xu37317b62017-03-07 17:49:31 -0800149 /** invalid reporting mode */
150 AREPORTING_MODE_INVALID = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700151 /** continuous reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700152 AREPORTING_MODE_CONTINUOUS = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700153 /** reporting on change */
Aravind Akella0e025c52014-06-03 19:19:57 -0700154 AREPORTING_MODE_ON_CHANGE = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700155 /** on shot reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700156 AREPORTING_MODE_ONE_SHOT = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700157 /** special trigger reporting */
Peng Xu37317b62017-03-07 17:49:31 -0800158 AREPORTING_MODE_SPECIAL_TRIGGER = 3
Aravind Akella0e025c52014-06-03 19:19:57 -0700159};
160
Peng Xu47cddca2017-02-15 23:31:22 -0800161/**
162 * Sensor Direct Report Rates.
163 */
164enum {
165 /** stopped */
166 ASENSOR_DIRECT_RATE_STOP = 0,
167 /** nominal 50Hz */
168 ASENSOR_DIRECT_RATE_NORMAL = 1,
169 /** nominal 200Hz */
170 ASENSOR_DIRECT_RATE_FAST = 2,
171 /** nominal 800Hz */
172 ASENSOR_DIRECT_RATE_VERY_FAST = 3
173};
174
175/**
176 * Sensor Direct Channel Type.
177 */
178enum {
179 /** shared memory created by ASharedMemory_create */
180 ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY = 1,
181 /** AHardwareBuffer */
182 ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER = 2
183};
184
Aravind Akella0e025c52014-06-03 19:19:57 -0700185/*
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700186 * A few useful constants
187 */
188
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700189/** Earth's gravity in m/s^2 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700190#define ASENSOR_STANDARD_GRAVITY (9.80665f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700191/** Maximum magnetic field on Earth's surface in uT */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700192#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700193/** Minimum magnetic field on Earth's surface in uT*/
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700194#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
195
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700196/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700197 * A sensor event.
198 */
199
200/* NOTE: Must match hardware/sensors.h */
201typedef struct ASensorVector {
202 union {
203 float v[3];
204 struct {
205 float x;
206 float y;
207 float z;
208 };
209 struct {
210 float azimuth;
211 float pitch;
212 float roll;
213 };
214 };
215 int8_t status;
216 uint8_t reserved[3];
217} ASensorVector;
218
Aravind Akella724d91d2013-06-27 12:04:23 -0700219typedef struct AMetaDataEvent {
220 int32_t what;
221 int32_t sensor;
222} AMetaDataEvent;
223
224typedef struct AUncalibratedEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800225 union {
226 float uncalib[3];
227 struct {
228 float x_uncalib;
229 float y_uncalib;
230 float z_uncalib;
231 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700232 };
Peng Xu9e720462016-01-26 18:48:54 -0800233 union {
234 float bias[3];
235 struct {
236 float x_bias;
237 float y_bias;
238 float z_bias;
239 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700240 };
Aravind Akella724d91d2013-06-27 12:04:23 -0700241} AUncalibratedEvent;
242
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700243typedef struct AHeartRateEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800244 float bpm;
245 int8_t status;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700246} AHeartRateEvent;
247
Peng Xu2576cb62016-01-20 00:22:09 -0800248typedef struct ADynamicSensorEvent {
Peng Xu9e720462016-01-26 18:48:54 -0800249 int32_t connected;
250 int32_t handle;
Peng Xu2576cb62016-01-20 00:22:09 -0800251} ADynamicSensorEvent;
252
Peng Xu9e720462016-01-26 18:48:54 -0800253typedef struct {
254 int32_t type;
255 int32_t serial;
256 union {
257 int32_t data_int32[14];
258 float data_float[14];
259 };
260} AAdditionalInfoEvent;
261
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700262/* NOTE: Must match hardware/sensors.h */
263typedef struct ASensorEvent {
264 int32_t version; /* sizeof(struct ASensorEvent) */
265 int32_t sensor;
266 int32_t type;
267 int32_t reserved0;
268 int64_t timestamp;
269 union {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700270 union {
271 float data[16];
272 ASensorVector vector;
273 ASensorVector acceleration;
274 ASensorVector magnetic;
275 float temperature;
276 float distance;
277 float light;
278 float pressure;
Aravind Akella724d91d2013-06-27 12:04:23 -0700279 float relative_humidity;
280 AUncalibratedEvent uncalibrated_gyro;
281 AUncalibratedEvent uncalibrated_magnetic;
282 AMetaDataEvent meta_data;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700283 AHeartRateEvent heart_rate;
Peng Xu2576cb62016-01-20 00:22:09 -0800284 ADynamicSensorEvent dynamic_sensor_meta;
Peng Xu9e720462016-01-26 18:48:54 -0800285 AAdditionalInfoEvent additional_info;
Mathias Agopianba02cd22013-07-03 16:20:57 -0700286 };
287 union {
288 uint64_t data[8];
289 uint64_t step_counter;
290 } u64;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700291 };
Aravind Akella9a844cf2014-02-11 18:58:52 -0800292
293 uint32_t flags;
294 int32_t reserved1[3];
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700295} ASensorEvent;
296
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700297struct ASensorManager;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700298/**
299 * {@link ASensorManager} is an opaque type to manage sensors and
300 * events queues.
301 *
302 * {@link ASensorManager} is a singleton that can be obtained using
303 * ASensorManager_getInstance().
304 *
305 * This file provides a set of functions that uses {@link
306 * ASensorManager} to access and list hardware sensors, and
307 * create and destroy event queues:
308 * - ASensorManager_getSensorList()
309 * - ASensorManager_getDefaultSensor()
310 * - ASensorManager_getDefaultSensorEx()
311 * - ASensorManager_createEventQueue()
312 * - ASensorManager_destroyEventQueue()
313 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700314typedef struct ASensorManager ASensorManager;
315
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700316
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700317struct ASensorEventQueue;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700318/**
319 * {@link ASensorEventQueue} is an opaque type that provides access to
320 * {@link ASensorEvent} from hardware sensors.
321 *
322 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue().
323 *
324 * This file provides a set of functions to enable and disable
325 * sensors, check and get events, and set event rates on a {@link
326 * ASensorEventQueue}.
327 * - ASensorEventQueue_enableSensor()
328 * - ASensorEventQueue_disableSensor()
329 * - ASensorEventQueue_hasEvents()
330 * - ASensorEventQueue_getEvents()
331 * - ASensorEventQueue_setEventRate()
332 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700333typedef struct ASensorEventQueue ASensorEventQueue;
334
335struct ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700336/**
337 * {@link ASensor} is an opaque type that provides information about
338 * an hardware sensors.
339 *
340 * A {@link ASensor} pointer can be obtained using
341 * ASensorManager_getDefaultSensor(),
342 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}.
343 *
344 * This file provides a set of functions to access properties of a
345 * {@link ASensor}:
346 * - ASensor_getName()
347 * - ASensor_getVendor()
348 * - ASensor_getType()
349 * - ASensor_getResolution()
350 * - ASensor_getMinDelay()
351 * - ASensor_getFifoMaxEventCount()
352 * - ASensor_getFifoReservedEventCount()
353 * - ASensor_getStringType()
354 * - ASensor_getReportingMode()
355 * - ASensor_isWakeUpSensor()
356 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700357typedef struct ASensor ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700358/**
359 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}.
360 *
361 * This is used to define entry in {@link ASensorList} arrays.
362 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700363typedef ASensor const* ASensorRef;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700364/**
365 * {@link ASensorList} is an array of reference to {@link ASensor}.
366 *
367 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList().
368 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700369typedef ASensorRef const* ASensorList;
370
371/*****************************************************************************/
372
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700373/**
Svet Ganov5fa32d42015-05-07 10:50:59 -0700374 * 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 * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead.
378 *
379 * Example:
380 *
381 * ASensorManager* sensorManager = ASensorManager_getInstance();
382 *
383 */
384__attribute__ ((deprecated)) ASensorManager* ASensorManager_getInstance();
385
386/*
387 * Get a reference to the sensor manager. ASensorManager is a singleton
388 * per package as different packages may have access to different sensors.
389 *
390 * Example:
391 *
392 * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz");
393 *
394 */
395ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName);
396
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700397/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700398 * Returns the list of available sensors.
399 */
400int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list);
401
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700402/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700403 * Returns the default sensor for the given type, or NULL if no sensor
Aravind Akellab37ba392014-08-05 14:53:07 -0700404 * of that type exists.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700405 */
406ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
407
Dan Albert494ed552016-09-23 15:57:45 -0700408#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700409/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700410 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor
411 * of this type and wakeUp properties exists.
412 */
Peng Xuda8385c2017-02-28 20:19:47 -0800413ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type, bool wakeUp);
Dan Albert494ed552016-09-23 15:57:45 -0700414#endif
Aravind Akellab37ba392014-08-05 14:53:07 -0700415
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700416/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700417 * Creates a new sensor event queue and associate it with a looper.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700418 *
419 * "ident" is a identifier for the events that will be returned when
420 * calling ALooper_pollOnce(). The identifier must be >= 0, or
421 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700422 */
423ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
424 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
425
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700426/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700427 * Destroys the event queue and free all resources associated to it.
428 */
429int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
430
Peng Xu47cddca2017-02-15 23:31:22 -0800431#if __ANDROID_API__ >= __ANDROID_API_O__
432/**
433 * Create direct channel based on shared memory
434 *
435 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} to be used
436 * for configuring sensor direct report.
437 *
438 * \param manager the {@link ASensorManager} instance obtained from
439 * {@link ASensorManager_getInstanceForPackage}.
440 * \param fd file descriptor representing a shared memory created by
441 * {@link ASharedMemory_create}
442 * \param size size to be used, must be less or equal to size of shared memory.
443 *
444 * \return a positive integer as a channel id to be used in
445 * {@link ASensorManager_destroyDirectChannel} and
446 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
447 */
448int ASensorManager_createSharedMemoryDirectChannel(ASensorManager* manager, int fd, size_t size);
449
450/**
451 * Create direct channel based on AHardwareBuffer
452 *
453 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER} type to be used
454 * for configuring sensor direct report.
455 *
456 * \param manager the {@link ASensorManager} instance obtained from
457 * {@link ASensorManager_getInstanceForPackage}.
458 * \param buffer {@link AHardwareBuffer} instance created by {@link AHardwareBuffer_allocate}.
459 * \param size the intended size to be used, must be less or equal to size of buffer.
460 *
461 * \return a positive integer as a channel id to be used in
462 * {@link ASensorManager_destroyDirectChannel} and
463 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures.
464 */
465int ASensorManager_createHardwareBufferDirectChannel(
466 ASensorManager* manager, AHardwareBuffer const * buffer, size_t size);
467
468/**
469 * Destroy a direct channel
470 *
471 * Destroy a direct channel previously created using {@link ASensorManager_createDirectChannel}.
472 * The buffer used for creating direct channel does not get destroyed with
473 * {@link ASensorManager_destroy} and has to be close or released separately.
474 *
475 * \param manager the {@link ASensorManager} instance obtained from
476 * {@link ASensorManager_getInstanceForPackage}.
477 * \param channelId channel id (a positive integer) returned from
478 * {@link ASensorManager_createSharedMemoryDirectChannel} or
479 * {@link ASensorManager_createHardwareBufferDirectChannel}.
480 */
481void ASensorManager_destroyDirectChannel(ASensorManager* manager, int channelId);
482
483/**
484 * Configure direct report on channel
485 *
486 * Configure sensor direct report on a direct channel: set rate to value other than
487 * {@link ASENSOR_DIRECT_RATE_STOP} so that sensor event can be directly
Peng Xuec53d022017-04-06 18:02:29 -0700488 * written into the shared memory region used for creating the buffer. It returns a positive token
489 * which can be used for identify sensor events from different sensors on success. Calling with rate
490 * {@link ASENSOR_DIRECT_RATE_STOP} will stop direct report of the sensor specified in the channel.
Peng Xu47cddca2017-02-15 23:31:22 -0800491 *
492 * To stop all active sensor direct report configured to a channel, set sensor to NULL and rate to
493 * {@link ASENSOR_DIRECT_RATE_STOP}.
494 *
495 * In order to successfully configure a direct report, the sensor has to support the specified rate
496 * and the channel type, which can be checked by {@link ASensor_getHighestDirectReportRateLevel} and
497 * {@link ASensor_isDirectChannelTypeSupported}, respectively.
498 *
499 * Example:
500 * \code{.cpp}
501 * ASensorManager *manager = ...;
502 * ASensor *sensor = ...;
503 * int channelId = ...;
504 *
505 * ASensorManager_configureDirectReport(
506 * manager, sensor, channel_id, ASENSOR_DIRECT_RATE_FAST);
507 * \endcode
508 *
509 * \param manager the {@link ASensorManager} instance obtained from
510 * {@link ASensorManager_getInstanceForPackage}.
511 * \param sensor a {@link ASensor} to denote which sensor to be operate. It can be NULL if rate
512 * is {@link ASENSOR_DIRECT_RATE_STOP}, denoting stopping of all active sensor
513 * direct report.
514 * \param channelId channel id (a positive integer) returned from
515 * {@link ASensorManager_createSharedMemoryDirectChannel} or
516 * {@link ASensorManager_createHardwareBufferDirectChannel}.
517 *
Peng Xuec53d022017-04-06 18:02:29 -0700518 * \return positive token for success or negative error code.
Peng Xu47cddca2017-02-15 23:31:22 -0800519 */
520int ASensorManager_configureDirectReport(
521 ASensorManager* manager, ASensor const* sensor, int channelId, int rate);
522#endif
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700523
524/*****************************************************************************/
525
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700526/**
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530527 * Enable the selected sensor with a specified sampling period and max batch report latency.
528 * Returns a negative error code on failure.
Aniroop Mathure96e5772016-12-13 00:04:06 +0530529 * Note: To disable the selected sensor, use ASensorEventQueue_disableSensor() same as before.
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530530 */
531int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor,
Peng Xuda8385c2017-02-28 20:19:47 -0800532 int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs);
Aniroop Mathurda94fd82015-11-03 01:47:46 +0530533
534/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700535 * Enable the selected sensor. Returns a negative error code on failure.
536 */
537int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor);
538
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700539/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700540 * Disable the selected sensor. Returns a negative error code on failure.
541 */
542int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor);
543
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700544/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700545 * Sets the delivery rate of events in microseconds for the given sensor.
546 * Note that this is a hint only, generally event will arrive at a higher
547 * rate. It is an error to set a rate inferior to the value returned by
548 * ASensor_getMinDelay().
549 * Returns a negative error code on failure.
550 */
551int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
552
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700553/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700554 * Returns true if there are one or more events available in the
555 * sensor queue. Returns 1 if the queue has events; 0 if
556 * it does not have events; and a negative value if there is an error.
557 */
558int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
559
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700560/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700561 * Returns the next available events from the queue. Returns a negative
562 * value if no events are available or an error has occurred, otherwise
563 * the number of events returned.
564 *
565 * Examples:
566 * ASensorEvent event;
567 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
568 *
569 * ASensorEvent eventBuffer[8];
570 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
571 *
572 */
Peng Xuda8385c2017-02-28 20:19:47 -0800573ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, ASensorEvent* events, size_t count);
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700574
575
576/*****************************************************************************/
577
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700578/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700579 * Returns this sensor's name (non localized)
580 */
581const char* ASensor_getName(ASensor const* sensor);
582
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700583/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700584 * Returns this sensor's vendor's name (non localized)
585 */
586const char* ASensor_getVendor(ASensor const* sensor);
587
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700588/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700589 * Return this sensor's type
590 */
591int ASensor_getType(ASensor const* sensor);
592
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700593/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700594 * Returns this sensors's resolution
595 */
596float ASensor_getResolution(ASensor const* sensor);
597
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700598/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700599 * Returns the minimum delay allowed between events in microseconds.
600 * A value of zero means that this sensor doesn't report events at a
601 * constant rate, but rather only when a new data is available.
602 */
603int ASensor_getMinDelay(ASensor const* sensor);
604
Dan Albert494ed552016-09-23 15:57:45 -0700605#if __ANDROID_API__ >= 21
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700606/**
Aravind Akella70018042014-04-07 22:52:37 +0000607 * Returns the maximum size of batches for this sensor. Batches will often be
608 * smaller, as the hardware fifo might be used for other sensors.
609 */
610int ASensor_getFifoMaxEventCount(ASensor const* sensor);
611
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700612/**
Aravind Akella70018042014-04-07 22:52:37 +0000613 * Returns the hardware batch fifo size reserved to this sensor.
614 */
615int ASensor_getFifoReservedEventCount(ASensor const* sensor);
616
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700617/**
Aravind Akella70018042014-04-07 22:52:37 +0000618 * Returns this sensor's string type.
619 */
620const char* ASensor_getStringType(ASensor const* sensor);
621
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700622/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700623 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants.
624 */
625int ASensor_getReportingMode(ASensor const* sensor);
626
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700627/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700628 * Returns true if this is a wake up sensor, false otherwise.
629 */
630bool ASensor_isWakeUpSensor(ASensor const* sensor);
Dan Albert494ed552016-09-23 15:57:45 -0700631#endif /* __ANDROID_API__ >= 21 */
Aravind Akellab37ba392014-08-05 14:53:07 -0700632
Peng Xu47cddca2017-02-15 23:31:22 -0800633#if __ANDROID_API__ >= __ANDROID_API_O__
634/**
635 * Test if sensor supports a certain type of direct channel.
636 *
637 * \param sensor a {@link ASensor} to denote the sensor to be checked.
638 * \param channelType Channel type constant, either
639 * {@ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY}
640 * or {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER}.
641 * \returns true if sensor supports the specified direct channel type.
642 */
643bool ASensor_isDirectChannelTypeSupported(ASensor const* sensor, int channelType);
644/**
645 * Get the highest direct rate level that a sensor support.
646 *
647 * \param sensor a {@link ASensor} to denote the sensor to be checked.
648 *
649 * \return a ASENSOR_DIRECT_RATE_... enum denoting the highest rate level supported by the sensor.
650 * If return value is {@link ASENSOR_DIRECT_RATE_STOP}, it means the sensor
651 * does not support direct report.
652 */
653int ASensor_getHighestDirectReportRateLevel(ASensor const* sensor);
654#endif
655
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700656#ifdef __cplusplus
657};
658#endif
659
660#endif // ANDROID_SENSOR_H
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700661
662/** @} */