blob: 4600dadabf97241c8d124f7b8a86350721b6e6c1 [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
59
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070060/**
61 * Sensor types.
Mathias Agopiane1c61d32012-03-23 14:19:36 -070062 * (keep in sync with hardware/sensor.h)
63 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070064enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070065 /**
66 * {@link ASENSOR_TYPE_ACCELEROMETER}
67 * reporting-mode: continuous
68 *
69 * All values are in SI units (m/s^2) and measure the acceleration of the
70 * device minus the force of gravity.
71 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070072 ASENSOR_TYPE_ACCELEROMETER = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070073 /**
74 * {@link ASENSOR_TYPE_MAGNETIC_FIELD}
75 * reporting-mode: continuous
76 *
77 * All values are in micro-Tesla (uT) and measure the geomagnetic
78 * field in the X, Y and Z axis.
79 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070080 ASENSOR_TYPE_MAGNETIC_FIELD = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070081 /**
82 * {@link ASENSOR_TYPE_GYROSCOPE}
83 * reporting-mode: continuous
84 *
85 * All values are in radians/second and measure the rate of rotation
86 * around the X, Y and Z axis.
87 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070088 ASENSOR_TYPE_GYROSCOPE = 4,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070089 /**
90 * {@link ASENSOR_TYPE_LIGHT}
91 * reporting-mode: on-change
92 *
93 * The light sensor value is returned in SI lux units.
94 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070095 ASENSOR_TYPE_LIGHT = 5,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070096 /**
97 * {@link ASENSOR_TYPE_PROXIMITY}
98 * reporting-mode: on-change
99 *
100 * The proximity sensor which turns the screen off and back on during calls is the
101 * wake-up proximity sensor. Implement wake-up proximity sensor before implementing
102 * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag
103 * SENSOR_FLAG_WAKE_UP.
104 * The value corresponds to the distance to the nearest object in centimeters.
105 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700106 ASENSOR_TYPE_PROXIMITY = 8
107};
108
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700109/**
110 * Sensor accuracy measure.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700111 */
112enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700113 /** no contact */
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700114 ASENSOR_STATUS_NO_CONTACT = -1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700115 /** unreliable */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700116 ASENSOR_STATUS_UNRELIABLE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700117 /** low accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700118 ASENSOR_STATUS_ACCURACY_LOW = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700119 /** medium accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700120 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700121 /** high accuracy */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700122 ASENSOR_STATUS_ACCURACY_HIGH = 3
123};
124
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700125/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700126 * Sensor Reporting Modes.
127 */
128enum {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700129 /** continuous reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700130 AREPORTING_MODE_CONTINUOUS = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700131 /** reporting on change */
Aravind Akella0e025c52014-06-03 19:19:57 -0700132 AREPORTING_MODE_ON_CHANGE = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700133 /** on shot reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700134 AREPORTING_MODE_ONE_SHOT = 2,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700135 /** special trigger reporting */
Aravind Akella0e025c52014-06-03 19:19:57 -0700136 AREPORTING_MODE_SPECIAL_TRIGGER = 3
137};
138
139/*
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700140 * A few useful constants
141 */
142
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700143/** Earth's gravity in m/s^2 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700144#define ASENSOR_STANDARD_GRAVITY (9.80665f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700145/** Maximum magnetic field on Earth's surface in uT */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700146#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700147/** Minimum magnetic field on Earth's surface in uT*/
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700148#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
149
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700150/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700151 * A sensor event.
152 */
153
154/* NOTE: Must match hardware/sensors.h */
155typedef struct ASensorVector {
156 union {
157 float v[3];
158 struct {
159 float x;
160 float y;
161 float z;
162 };
163 struct {
164 float azimuth;
165 float pitch;
166 float roll;
167 };
168 };
169 int8_t status;
170 uint8_t reserved[3];
171} ASensorVector;
172
Aravind Akella724d91d2013-06-27 12:04:23 -0700173typedef struct AMetaDataEvent {
174 int32_t what;
175 int32_t sensor;
176} AMetaDataEvent;
177
178typedef struct AUncalibratedEvent {
179 union {
180 float uncalib[3];
181 struct {
182 float x_uncalib;
183 float y_uncalib;
184 float z_uncalib;
185 };
186 };
187 union {
188 float bias[3];
189 struct {
190 float x_bias;
191 float y_bias;
192 float z_bias;
193 };
194 };
195} AUncalibratedEvent;
196
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700197typedef struct AHeartRateEvent {
198 float bpm;
199 int8_t status;
200} AHeartRateEvent;
201
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700202/* NOTE: Must match hardware/sensors.h */
203typedef struct ASensorEvent {
204 int32_t version; /* sizeof(struct ASensorEvent) */
205 int32_t sensor;
206 int32_t type;
207 int32_t reserved0;
208 int64_t timestamp;
209 union {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700210 union {
211 float data[16];
212 ASensorVector vector;
213 ASensorVector acceleration;
214 ASensorVector magnetic;
215 float temperature;
216 float distance;
217 float light;
218 float pressure;
Aravind Akella724d91d2013-06-27 12:04:23 -0700219 float relative_humidity;
220 AUncalibratedEvent uncalibrated_gyro;
221 AUncalibratedEvent uncalibrated_magnetic;
222 AMetaDataEvent meta_data;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700223 AHeartRateEvent heart_rate;
Mathias Agopianba02cd22013-07-03 16:20:57 -0700224 };
225 union {
226 uint64_t data[8];
227 uint64_t step_counter;
228 } u64;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700229 };
Aravind Akella9a844cf2014-02-11 18:58:52 -0800230
231 uint32_t flags;
232 int32_t reserved1[3];
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700233} ASensorEvent;
234
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700235struct ASensorManager;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700236/**
237 * {@link ASensorManager} is an opaque type to manage sensors and
238 * events queues.
239 *
240 * {@link ASensorManager} is a singleton that can be obtained using
241 * ASensorManager_getInstance().
242 *
243 * This file provides a set of functions that uses {@link
244 * ASensorManager} to access and list hardware sensors, and
245 * create and destroy event queues:
246 * - ASensorManager_getSensorList()
247 * - ASensorManager_getDefaultSensor()
248 * - ASensorManager_getDefaultSensorEx()
249 * - ASensorManager_createEventQueue()
250 * - ASensorManager_destroyEventQueue()
251 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700252typedef struct ASensorManager ASensorManager;
253
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700254
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700255struct ASensorEventQueue;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700256/**
257 * {@link ASensorEventQueue} is an opaque type that provides access to
258 * {@link ASensorEvent} from hardware sensors.
259 *
260 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue().
261 *
262 * This file provides a set of functions to enable and disable
263 * sensors, check and get events, and set event rates on a {@link
264 * ASensorEventQueue}.
265 * - ASensorEventQueue_enableSensor()
266 * - ASensorEventQueue_disableSensor()
267 * - ASensorEventQueue_hasEvents()
268 * - ASensorEventQueue_getEvents()
269 * - ASensorEventQueue_setEventRate()
270 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700271typedef struct ASensorEventQueue ASensorEventQueue;
272
273struct ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700274/**
275 * {@link ASensor} is an opaque type that provides information about
276 * an hardware sensors.
277 *
278 * A {@link ASensor} pointer can be obtained using
279 * ASensorManager_getDefaultSensor(),
280 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}.
281 *
282 * This file provides a set of functions to access properties of a
283 * {@link ASensor}:
284 * - ASensor_getName()
285 * - ASensor_getVendor()
286 * - ASensor_getType()
287 * - ASensor_getResolution()
288 * - ASensor_getMinDelay()
289 * - ASensor_getFifoMaxEventCount()
290 * - ASensor_getFifoReservedEventCount()
291 * - ASensor_getStringType()
292 * - ASensor_getReportingMode()
293 * - ASensor_isWakeUpSensor()
294 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700295typedef struct ASensor ASensor;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700296/**
297 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}.
298 *
299 * This is used to define entry in {@link ASensorList} arrays.
300 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700301typedef ASensor const* ASensorRef;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700302/**
303 * {@link ASensorList} is an array of reference to {@link ASensor}.
304 *
305 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList().
306 */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700307typedef ASensorRef const* ASensorList;
308
309/*****************************************************************************/
310
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700311/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700312 * Get a reference to the sensor manager. ASensorManager is a singleton.
313 *
314 * Example:
315 *
316 * ASensorManager* sensorManager = ASensorManager_getInstance();
317 *
318 */
319ASensorManager* ASensorManager_getInstance();
320
321
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700322/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700323 * Returns the list of available sensors.
324 */
325int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list);
326
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700327/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700328 * Returns the default sensor for the given type, or NULL if no sensor
Aravind Akellab37ba392014-08-05 14:53:07 -0700329 * of that type exists.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700330 */
331ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
332
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700333/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700334 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor
335 * of this type and wakeUp properties exists.
336 */
337ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type,
338 bool wakeUp);
339
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700340/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700341 * Creates a new sensor event queue and associate it with a looper.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700342 *
343 * "ident" is a identifier for the events that will be returned when
344 * calling ALooper_pollOnce(). The identifier must be >= 0, or
345 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700346 */
347ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
348 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
349
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700350/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700351 * Destroys the event queue and free all resources associated to it.
352 */
353int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
354
355
356/*****************************************************************************/
357
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700358/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700359 * Enable the selected sensor. Returns a negative error code on failure.
360 */
361int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor);
362
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700363/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700364 * Disable the selected sensor. Returns a negative error code on failure.
365 */
366int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor);
367
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700368/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700369 * Sets the delivery rate of events in microseconds for the given sensor.
370 * Note that this is a hint only, generally event will arrive at a higher
371 * rate. It is an error to set a rate inferior to the value returned by
372 * ASensor_getMinDelay().
373 * Returns a negative error code on failure.
374 */
375int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
376
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700377/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700378 * Returns true if there are one or more events available in the
379 * sensor queue. Returns 1 if the queue has events; 0 if
380 * it does not have events; and a negative value if there is an error.
381 */
382int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
383
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700384/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700385 * Returns the next available events from the queue. Returns a negative
386 * value if no events are available or an error has occurred, otherwise
387 * the number of events returned.
388 *
389 * Examples:
390 * ASensorEvent event;
391 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
392 *
393 * ASensorEvent eventBuffer[8];
394 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
395 *
396 */
397ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
398 ASensorEvent* events, size_t count);
399
400
401/*****************************************************************************/
402
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700403/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700404 * Returns this sensor's name (non localized)
405 */
406const char* ASensor_getName(ASensor const* sensor);
407
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700408/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700409 * Returns this sensor's vendor's name (non localized)
410 */
411const char* ASensor_getVendor(ASensor const* sensor);
412
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700413/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700414 * Return this sensor's type
415 */
416int ASensor_getType(ASensor const* sensor);
417
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700418/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700419 * Returns this sensors's resolution
420 */
421float ASensor_getResolution(ASensor const* sensor);
422
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700423/**
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700424 * Returns the minimum delay allowed between events in microseconds.
425 * A value of zero means that this sensor doesn't report events at a
426 * constant rate, but rather only when a new data is available.
427 */
428int ASensor_getMinDelay(ASensor const* sensor);
429
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700430/**
Aravind Akella70018042014-04-07 22:52:37 +0000431 * Returns the maximum size of batches for this sensor. Batches will often be
432 * smaller, as the hardware fifo might be used for other sensors.
433 */
434int ASensor_getFifoMaxEventCount(ASensor const* sensor);
435
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700436/**
Aravind Akella70018042014-04-07 22:52:37 +0000437 * Returns the hardware batch fifo size reserved to this sensor.
438 */
439int ASensor_getFifoReservedEventCount(ASensor const* sensor);
440
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700441/**
Aravind Akella70018042014-04-07 22:52:37 +0000442 * Returns this sensor's string type.
443 */
444const char* ASensor_getStringType(ASensor const* sensor);
445
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700446/**
Aravind Akella0e025c52014-06-03 19:19:57 -0700447 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants.
448 */
449int ASensor_getReportingMode(ASensor const* sensor);
450
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700451/**
Aravind Akellab37ba392014-08-05 14:53:07 -0700452 * Returns true if this is a wake up sensor, false otherwise.
453 */
454bool ASensor_isWakeUpSensor(ASensor const* sensor);
455
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700456#ifdef __cplusplus
457};
458#endif
459
460#endif // ANDROID_SENSOR_H
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700461
462/** @} */