blob: 878f8ff3849e77cbc2c2c337582fc3a119113f2b [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
17
18#ifndef ANDROID_SENSOR_H
19#define ANDROID_SENSOR_H
20
21/******************************************************************
22 *
23 * IMPORTANT NOTICE:
24 *
25 * This file is part of Android's set of stable system headers
26 * exposed by the Android NDK (Native Development Kit).
27 *
28 * Third-party source AND binary code relies on the definitions
29 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES.
30 *
31 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES)
32 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS
33 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY
34 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES
35 */
36
37/*
38 * Structures and functions to receive and process sensor events in
39 * native code.
40 *
41 */
42
43#include <sys/types.h>
44
45#include <android/looper.h>
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51
52/*
53 * Sensor types
54 * (keep in sync with hardware/sensor.h)
55 */
56
57enum {
58 ASENSOR_TYPE_ACCELEROMETER = 1,
59 ASENSOR_TYPE_MAGNETIC_FIELD = 2,
60 ASENSOR_TYPE_GYROSCOPE = 4,
61 ASENSOR_TYPE_LIGHT = 5,
62 ASENSOR_TYPE_PROXIMITY = 8
63};
64
65/*
66 * Sensor accuracy measure
67 */
68enum {
Etienne Le Grand630e31d2014-05-22 17:15:08 -070069 ASENSOR_STATUS_NO_CONTACT = -1,
Mathias Agopiane1c61d32012-03-23 14:19:36 -070070 ASENSOR_STATUS_UNRELIABLE = 0,
71 ASENSOR_STATUS_ACCURACY_LOW = 1,
72 ASENSOR_STATUS_ACCURACY_MEDIUM = 2,
73 ASENSOR_STATUS_ACCURACY_HIGH = 3
74};
75
76/*
77 * A few useful constants
78 */
79
80/* Earth's gravity in m/s^2 */
81#define ASENSOR_STANDARD_GRAVITY (9.80665f)
82/* Maximum magnetic field on Earth's surface in uT */
83#define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f)
84/* Minimum magnetic field on Earth's surface in uT*/
85#define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f)
86
87/*
88 * A sensor event.
89 */
90
91/* NOTE: Must match hardware/sensors.h */
92typedef struct ASensorVector {
93 union {
94 float v[3];
95 struct {
96 float x;
97 float y;
98 float z;
99 };
100 struct {
101 float azimuth;
102 float pitch;
103 float roll;
104 };
105 };
106 int8_t status;
107 uint8_t reserved[3];
108} ASensorVector;
109
Aravind Akella724d91d2013-06-27 12:04:23 -0700110typedef struct AMetaDataEvent {
111 int32_t what;
112 int32_t sensor;
113} AMetaDataEvent;
114
115typedef struct AUncalibratedEvent {
116 union {
117 float uncalib[3];
118 struct {
119 float x_uncalib;
120 float y_uncalib;
121 float z_uncalib;
122 };
123 };
124 union {
125 float bias[3];
126 struct {
127 float x_bias;
128 float y_bias;
129 float z_bias;
130 };
131 };
132} AUncalibratedEvent;
133
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700134typedef struct AHeartRateEvent {
135 float bpm;
136 int8_t status;
137} AHeartRateEvent;
138
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700139/* NOTE: Must match hardware/sensors.h */
140typedef struct ASensorEvent {
141 int32_t version; /* sizeof(struct ASensorEvent) */
142 int32_t sensor;
143 int32_t type;
144 int32_t reserved0;
145 int64_t timestamp;
146 union {
Mathias Agopianba02cd22013-07-03 16:20:57 -0700147 union {
148 float data[16];
149 ASensorVector vector;
150 ASensorVector acceleration;
151 ASensorVector magnetic;
152 float temperature;
153 float distance;
154 float light;
155 float pressure;
Aravind Akella724d91d2013-06-27 12:04:23 -0700156 float relative_humidity;
157 AUncalibratedEvent uncalibrated_gyro;
158 AUncalibratedEvent uncalibrated_magnetic;
159 AMetaDataEvent meta_data;
Etienne Le Grand630e31d2014-05-22 17:15:08 -0700160 AHeartRateEvent heart_rate;
Mathias Agopianba02cd22013-07-03 16:20:57 -0700161 };
162 union {
163 uint64_t data[8];
164 uint64_t step_counter;
165 } u64;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700166 };
Aravind Akella9a844cf2014-02-11 18:58:52 -0800167
168 uint32_t flags;
169 int32_t reserved1[3];
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700170} ASensorEvent;
171
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700172struct ASensorManager;
173typedef struct ASensorManager ASensorManager;
174
175struct ASensorEventQueue;
176typedef struct ASensorEventQueue ASensorEventQueue;
177
178struct ASensor;
179typedef struct ASensor ASensor;
180typedef ASensor const* ASensorRef;
181typedef ASensorRef const* ASensorList;
182
183/*****************************************************************************/
184
185/*
186 * Get a reference to the sensor manager. ASensorManager is a singleton.
187 *
188 * Example:
189 *
190 * ASensorManager* sensorManager = ASensorManager_getInstance();
191 *
192 */
193ASensorManager* ASensorManager_getInstance();
194
195
196/*
197 * Returns the list of available sensors.
198 */
199int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list);
200
201/*
202 * Returns the default sensor for the given type, or NULL if no sensor
203 * of that type exist.
204 */
205ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type);
206
207/*
208 * Creates a new sensor event queue and associate it with a looper.
209 */
210ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager,
211 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data);
212
213/*
214 * Destroys the event queue and free all resources associated to it.
215 */
216int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue);
217
218
219/*****************************************************************************/
220
221/*
222 * Enable the selected sensor. Returns a negative error code on failure.
223 */
224int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor);
225
226/*
227 * Disable the selected sensor. Returns a negative error code on failure.
228 */
229int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor);
230
231/*
232 * Sets the delivery rate of events in microseconds for the given sensor.
233 * Note that this is a hint only, generally event will arrive at a higher
234 * rate. It is an error to set a rate inferior to the value returned by
235 * ASensor_getMinDelay().
236 * Returns a negative error code on failure.
237 */
238int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
239
240/*
241 * Returns true if there are one or more events available in the
242 * sensor queue. Returns 1 if the queue has events; 0 if
243 * it does not have events; and a negative value if there is an error.
244 */
245int ASensorEventQueue_hasEvents(ASensorEventQueue* queue);
246
247/*
248 * Returns the next available events from the queue. Returns a negative
249 * value if no events are available or an error has occurred, otherwise
250 * the number of events returned.
251 *
252 * Examples:
253 * ASensorEvent event;
254 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1);
255 *
256 * ASensorEvent eventBuffer[8];
257 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8);
258 *
259 */
260ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue,
261 ASensorEvent* events, size_t count);
262
263
264/*****************************************************************************/
265
266/*
267 * Returns this sensor's name (non localized)
268 */
269const char* ASensor_getName(ASensor const* sensor);
270
271/*
272 * Returns this sensor's vendor's name (non localized)
273 */
274const char* ASensor_getVendor(ASensor const* sensor);
275
276/*
277 * Return this sensor's type
278 */
279int ASensor_getType(ASensor const* sensor);
280
281/*
282 * Returns this sensors's resolution
283 */
284float ASensor_getResolution(ASensor const* sensor);
285
286/*
287 * Returns the minimum delay allowed between events in microseconds.
288 * A value of zero means that this sensor doesn't report events at a
289 * constant rate, but rather only when a new data is available.
290 */
291int ASensor_getMinDelay(ASensor const* sensor);
292
Aravind Akella70018042014-04-07 22:52:37 +0000293/*
294 * Returns the maximum size of batches for this sensor. Batches will often be
295 * smaller, as the hardware fifo might be used for other sensors.
296 */
297int ASensor_getFifoMaxEventCount(ASensor const* sensor);
298
299/*
300 * Returns the hardware batch fifo size reserved to this sensor.
301 */
302int ASensor_getFifoReservedEventCount(ASensor const* sensor);
303
304/*
305 * Returns this sensor's string type.
306 */
307const char* ASensor_getStringType(ASensor const* sensor);
308
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700309#ifdef __cplusplus
310};
311#endif
312
313#endif // ANDROID_SENSOR_H