blob: 807b074cf7e5a19fc19af72b8553908ddcb1b68e [file] [log] [blame]
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -08001/*
Mathias Agopiana4557722012-11-28 17:21:55 -08002 * Copyright (C) 2012 The Android Open Source Project
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -08003 *
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#ifndef ANDROID_SENSORS_INTERFACE_H
18#define ANDROID_SENSORS_INTERFACE_H
19
20#include <stdint.h>
21#include <sys/cdefs.h>
22#include <sys/types.h>
23
24#include <hardware/hardware.h>
Mike Lockwood21b652f2009-05-22 10:05:48 -040025#include <cutils/native_handle.h>
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080026
Andreas Huber92190172016-10-10 13:18:52 -070027#include "sensors-base.h"
28
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080029__BEGIN_DECLS
30
Mathias Agopian56f66cc2012-11-08 15:57:38 -080031/*****************************************************************************/
32
33#define SENSORS_HEADER_VERSION 1
34#define SENSORS_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
35#define SENSORS_DEVICE_API_VERSION_0_1 HARDWARE_DEVICE_API_VERSION_2(0, 1, SENSORS_HEADER_VERSION)
Mathias Agopiana4557722012-11-28 17:21:55 -080036#define SENSORS_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION_2(1, 0, SENSORS_HEADER_VERSION)
Mathias Agopian16671c52013-07-24 21:07:40 -070037#define SENSORS_DEVICE_API_VERSION_1_1 HARDWARE_DEVICE_API_VERSION_2(1, 1, SENSORS_HEADER_VERSION)
Aravind Akella477fbd52014-04-07 22:46:01 +000038#define SENSORS_DEVICE_API_VERSION_1_2 HARDWARE_DEVICE_API_VERSION_2(1, 2, SENSORS_HEADER_VERSION)
Aravind Akella6242f322014-02-28 18:46:19 -080039#define SENSORS_DEVICE_API_VERSION_1_3 HARDWARE_DEVICE_API_VERSION_2(1, 3, SENSORS_HEADER_VERSION)
Ashutosh Joshi6507f502015-04-03 16:22:32 -070040#define SENSORS_DEVICE_API_VERSION_1_4 HARDWARE_DEVICE_API_VERSION_2(1, 4, SENSORS_HEADER_VERSION)
Mathias Agopian56f66cc2012-11-08 15:57:38 -080041
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080042/**
Clay Murphy8db1fb42013-12-19 09:58:28 -080043 * Please see the Sensors section of source.android.com for an
44 * introduction to and detailed descriptions of Android sensor types:
45 * http://source.android.com/devices/sensors/index.html
46 */
47
48/**
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080049 * The id of this module
50 */
51#define SENSORS_HARDWARE_MODULE_ID "sensors"
52
53/**
54 * Name of the sensors device to open
55 */
Mathias Agopianb1e212e2010-07-08 16:44:54 -070056#define SENSORS_HARDWARE_POLL "poll"
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080057
58/**
59 * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
60 * A Handle identifies a given sensors. The handle is used to activate
61 * and/or deactivate sensors.
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080062 */
63#define SENSORS_HANDLE_BASE 0
Peng Xu95f79b12017-01-11 14:16:15 -080064#define SENSORS_HANDLE_BITS 32
65#define SENSORS_HANDLE_COUNT (1ull<<SENSORS_HANDLE_BITS)
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080066
67
Mathias Agopiana4557722012-11-28 17:21:55 -080068/*
Aravind Akella6242f322014-02-28 18:46:19 -080069 * **** Deprecated *****
Mathias Agopiana4557722012-11-28 17:21:55 -080070 * flags for (*batch)()
71 * Availability: SENSORS_DEVICE_API_VERSION_1_0
Aravind Akella6242f322014-02-28 18:46:19 -080072 * see (*batch)() documentation for details.
73 * Deprecated as of SENSORS_DEVICE_API_VERSION_1_3.
74 * WAKE_UP_* sensors replace WAKE_UPON_FIFO_FULL concept.
Mathias Agopiana4557722012-11-28 17:21:55 -080075 */
76enum {
77 SENSORS_BATCH_DRY_RUN = 0x00000001,
78 SENSORS_BATCH_WAKE_UPON_FIFO_FULL = 0x00000002
79};
80
Mathias Agopian16671c52013-07-24 21:07:40 -070081/*
82 * what field for meta_data_event_t
83 */
84enum {
85 /* a previous flush operation has completed */
Andreas Huber92190172016-10-10 13:18:52 -070086 // META_DATA_FLUSH_COMPLETE = 1,
Mathias Agopianaf32a8d2013-08-06 20:33:38 -070087 META_DATA_VERSION /* always last, leave auto-assigned */
Mathias Agopian16671c52013-07-24 21:07:40 -070088};
89
Mathias Agopiana4557722012-11-28 17:21:55 -080090/*
Aravind Akella477fbd52014-04-07 22:46:01 +000091 * The permission to use for body sensors (like heart rate monitors).
92 * See sensor types for more details on what sensors should require this
93 * permission.
94 */
95#define SENSOR_PERMISSION_BODY_SENSORS "android.permission.BODY_SENSORS"
96
97/*
Peng Xu95f79b12017-01-11 14:16:15 -080098 * sensor flags legacy names
99 *
100 * please use SENSOR_FLAG_* directly for new implementation.
101 * @see sensor_t
Ashutosh Joshi6507f502015-04-03 16:22:32 -0700102 */
Ashutosh Joshi6507f502015-04-03 16:22:32 -0700103
Peng Xud1562992016-03-29 18:14:05 -0700104#define SENSOR_FLAG_MASK(nbit, shift) (((1<<(nbit))-1)<<(shift))
105#define SENSOR_FLAG_MASK_1(shift) SENSOR_FLAG_MASK(1, shift)
106
107/*
108 * Mask and shift for reporting mode sensor flags defined above.
109 */
Peng Xu95f79b12017-01-11 14:16:15 -0800110#define REPORTING_MODE_SHIFT SENSOR_FLAG_SHIFT_REPORTING_MODE
Peng Xud1562992016-03-29 18:14:05 -0700111#define REPORTING_MODE_NBIT (3)
Peng Xu95f79b12017-01-11 14:16:15 -0800112#define REPORTING_MODE_MASK SENSOR_FLAG_MASK_REPORTING_MODE
Peng Xud1562992016-03-29 18:14:05 -0700113
114/*
115 * Mask and shift for data_injection mode sensor flags defined above.
116 */
Peng Xu95f79b12017-01-11 14:16:15 -0800117#define DATA_INJECTION_SHIFT SENSOR_FLAG_SHIFT_DATA_INJECTION
118#define DATA_INJECTION_MASK SENSOR_FLAG_DATA_INJECTION
Peng Xud1562992016-03-29 18:14:05 -0700119
120/*
121 * Mask and shift for dynamic sensor flag.
122 */
Peng Xu95f79b12017-01-11 14:16:15 -0800123#define DYNAMIC_SENSOR_SHIFT SENSOR_FLAG_SHIFT_DYNAMIC_SENSOR
124#define DYNAMIC_SENSOR_MASK SENSOR_FLAG_DYNAMIC_SENSOR
Peng Xud1562992016-03-29 18:14:05 -0700125
126/*
127 * Mask and shift for sensor additional information support.
128 */
Peng Xu95f79b12017-01-11 14:16:15 -0800129#define ADDITIONAL_INFO_SHIFT SENSOR_FLAG_SHIFT_ADDITIONAL_INFO
130#define ADDITIONAL_INFO_MASK SENSOR_FLAG_ADDITIONAL_INFO
Peng Xud1562992016-03-29 18:14:05 -0700131
Peng Xu08a4dd92016-11-03 11:56:27 -0700132/*
Peng Xu95f79b12017-01-11 14:16:15 -0800133 * Legacy alias of SENSOR_TYPE_MAGNETIC_FIELD.
134 *
135 * Previously, the type of a sensor measuring local magnetic field is named
136 * SENSOR_TYPE_GEOMAGNETIC_FIELD and SENSOR_TYPE_MAGNETIC_FIELD is its alias.
137 * SENSOR_TYPE_MAGNETIC_FIELD is redefined as primary name to avoid confusion.
138 * SENSOR_TYPE_GEOMAGNETIC_FIELD is the alias and is deprecating. New implementation must not use
139 * SENSOR_TYPE_GEOMAGNETIC_FIELD.
Peng Xu08a4dd92016-11-03 11:56:27 -0700140 */
Peng Xu95f79b12017-01-11 14:16:15 -0800141#define SENSOR_TYPE_GEOMAGNETIC_FIELD SENSOR_TYPE_MAGNETIC_FIELD
Peng Xu08a4dd92016-11-03 11:56:27 -0700142
Peng Xu95f79b12017-01-11 14:16:15 -0800143/*
144 * Sensor string types for Android defined sensor types.
145 *
146 * For Android defined sensor types, string type will be override in sensor service and thus no
147 * longer needed to be added to sensor_t data structure.
148 *
149 * These definitions are going to be removed soon.
150 */
151#define SENSOR_STRING_TYPE_ACCELEROMETER "android.sensor.accelerometer"
152#define SENSOR_STRING_TYPE_MAGNETIC_FIELD "android.sensor.magnetic_field"
153#define SENSOR_STRING_TYPE_ORIENTATION "android.sensor.orientation"
154#define SENSOR_STRING_TYPE_GYROSCOPE "android.sensor.gyroscope"
155#define SENSOR_STRING_TYPE_LIGHT "android.sensor.light"
156#define SENSOR_STRING_TYPE_PRESSURE "android.sensor.pressure"
157#define SENSOR_STRING_TYPE_TEMPERATURE "android.sensor.temperature"
158#define SENSOR_STRING_TYPE_PROXIMITY "android.sensor.proximity"
159#define SENSOR_STRING_TYPE_GRAVITY "android.sensor.gravity"
160#define SENSOR_STRING_TYPE_LINEAR_ACCELERATION "android.sensor.linear_acceleration"
161#define SENSOR_STRING_TYPE_ROTATION_VECTOR "android.sensor.rotation_vector"
162#define SENSOR_STRING_TYPE_RELATIVE_HUMIDITY "android.sensor.relative_humidity"
163#define SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE "android.sensor.ambient_temperature"
164#define SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED "android.sensor.magnetic_field_uncalibrated"
165#define SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR "android.sensor.game_rotation_vector"
166#define SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED "android.sensor.gyroscope_uncalibrated"
167#define SENSOR_STRING_TYPE_SIGNIFICANT_MOTION "android.sensor.significant_motion"
168#define SENSOR_STRING_TYPE_STEP_DETECTOR "android.sensor.step_detector"
169#define SENSOR_STRING_TYPE_STEP_COUNTER "android.sensor.step_counter"
170#define SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR "android.sensor.geomagnetic_rotation_vector"
171#define SENSOR_STRING_TYPE_HEART_RATE "android.sensor.heart_rate"
172#define SENSOR_STRING_TYPE_TILT_DETECTOR "android.sensor.tilt_detector"
173#define SENSOR_STRING_TYPE_WAKE_GESTURE "android.sensor.wake_gesture"
174#define SENSOR_STRING_TYPE_GLANCE_GESTURE "android.sensor.glance_gesture"
175#define SENSOR_STRING_TYPE_PICK_UP_GESTURE "android.sensor.pick_up_gesture"
176#define SENSOR_STRING_TYPE_WRIST_TILT_GESTURE "android.sensor.wrist_tilt_gesture"
177#define SENSOR_STRING_TYPE_DEVICE_ORIENTATION "android.sensor.device_orientation"
178#define SENSOR_STRING_TYPE_POSE_6DOF "android.sensor.pose_6dof"
Ashutosh Joshicb963312016-01-25 18:48:57 -0800179#define SENSOR_STRING_TYPE_STATIONARY_DETECT "android.sensor.stationary_detect"
Ashutosh Joshicb963312016-01-25 18:48:57 -0800180#define SENSOR_STRING_TYPE_MOTION_DETECT "android.sensor.motion_detect"
Ashutosh Joshicb963312016-01-25 18:48:57 -0800181#define SENSOR_STRING_TYPE_HEART_BEAT "android.sensor.heart_beat"
Peng Xu95f79b12017-01-11 14:16:15 -0800182#define SENSOR_STRING_TYPE_DYNAMIC_SENSOR_META "android.sensor.dynamic_sensor_meta"
183#define SENSOR_STRING_TYPE_ADDITIONAL_INFO "android.sensor.additional_info"
184#define SENSOR_STRING_TYPE_LOW_LATENCY_OFFBODY_DETECT "android.sensor.low_latency_offbody"
185#define SENSOR_STRING_TYPE_ACCELEROMETER_UNCALIBRATED "android.sensor.accelerometer_uncalibrated"
Peng Xu0743a5c2016-01-21 17:30:02 -0800186
187/**
Mathias Agopian56f66cc2012-11-08 15:57:38 -0800188 * Values returned by the accelerometer in various locations in the universe.
189 * all values are in SI units (m/s^2)
190 */
191#define GRAVITY_SUN (275.0f)
192#define GRAVITY_EARTH (9.80665f)
193
194/** Maximum magnetic field on Earth's surface */
195#define MAGNETIC_FIELD_EARTH_MAX (60.0f)
196
197/** Minimum magnetic field on Earth's surface */
198#define MAGNETIC_FIELD_EARTH_MIN (30.0f)
199
Peng Xue641ba92016-01-20 00:27:21 -0800200struct sensor_t;
201
Mathias Agopian56f66cc2012-11-08 15:57:38 -0800202/**
203 * sensor event data
204 */
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800205typedef struct {
206 union {
207 float v[3];
208 struct {
209 float x;
210 float y;
211 float z;
212 };
213 struct {
214 float azimuth;
215 float pitch;
216 float roll;
217 };
218 };
219 int8_t status;
220 uint8_t reserved[3];
221} sensors_vec_t;
222
223/**
Ashutosh Joshid1e25622017-01-10 18:33:53 -0800224 * uncalibrated accelerometer, gyroscope and magnetometer event data
Etienne Le Grandca858142013-02-26 19:17:20 -0800225 */
226typedef struct {
Etienne Le Grand28f04112013-03-27 18:59:10 -0700227 union {
228 float uncalib[3];
229 struct {
230 float x_uncalib;
231 float y_uncalib;
232 float z_uncalib;
233 };
234 };
235 union {
236 float bias[3];
237 struct {
238 float x_bias;
239 float y_bias;
240 float z_bias;
241 };
242 };
Etienne Le Grandca858142013-02-26 19:17:20 -0800243} uncalibrated_event_t;
244
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700245/**
246 * Meta data event data
247 */
Mathias Agopian16671c52013-07-24 21:07:40 -0700248typedef struct meta_data_event {
249 int32_t what;
250 int32_t sensor;
251} meta_data_event_t;
252
Etienne Le Grandca858142013-02-26 19:17:20 -0800253/**
Peng Xue641ba92016-01-20 00:27:21 -0800254 * Dynamic sensor meta event. See the description of SENSOR_TYPE_DYNAMIC_SENSOR_META type for
255 * details.
256 */
257typedef struct dynamic_sensor_meta_event {
Peng Xue20707a2016-01-27 18:26:10 -0800258 int32_t connected;
259 int32_t handle;
Peng Xue641ba92016-01-20 00:27:21 -0800260 const struct sensor_t * sensor; // should be NULL if connected == false
Peng Xu69b5dba2016-04-22 22:34:51 -0700261 uint8_t uuid[16]; // UUID of a dynamic sensor (using RFC 4122 byte order)
262 // For UUID 12345678-90AB-CDEF-1122-334455667788 the uuid field
263 // should be initialized as:
264 // {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x11, ...}
Peng Xue641ba92016-01-20 00:27:21 -0800265} dynamic_sensor_meta_event_t;
266
267/**
Etienne Le Grand7b361582014-05-16 11:08:28 -0700268 * Heart rate event data
269 */
270typedef struct {
271 // Heart rate in beats per minute.
272 // Set to 0 when status is SENSOR_STATUS_UNRELIABLE or ..._NO_CONTACT
273 float bpm;
274 // Status of the sensor for this reading. Set to one SENSOR_STATUS_...
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700275 // Note that this value should only be set for sensors that explicitly define
276 // the meaning of this field. This field is not piped through the framework
277 // for other sensors.
Etienne Le Grand7b361582014-05-16 11:08:28 -0700278 int8_t status;
279} heart_rate_event_t;
280
Peng Xu0743a5c2016-01-21 17:30:02 -0800281typedef struct {
282 int32_t type; // type of payload data, see additional_info_type_t
283 int32_t serial; // sequence number of this frame for this type
284 union {
285 // for each frame, a single data type, either int32_t or float, should be used.
286 int32_t data_int32[14];
287 float data_float[14];
288 };
289} additional_info_event_t;
290
Etienne Le Grand7b361582014-05-16 11:08:28 -0700291/**
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800292 * Union of the various types of sensor data
293 * that can be returned.
294 */
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700295typedef struct sensors_event_t {
296 /* must be sizeof(struct sensors_event_t) */
297 int32_t version;
298
299 /* sensor identifier */
300 int32_t sensor;
301
302 /* sensor type */
303 int32_t type;
304
305 /* reserved */
306 int32_t reserved0;
307
308 /* time is in nanosecond */
309 int64_t timestamp;
310
311 union {
Mathias Agopian27e16682013-07-08 14:00:54 -0700312 union {
313 float data[16];
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700314
Mathias Agopian27e16682013-07-08 14:00:54 -0700315 /* acceleration values are in meter per second per second (m/s^2) */
316 sensors_vec_t acceleration;
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700317
Mathias Agopian27e16682013-07-08 14:00:54 -0700318 /* magnetic vector values are in micro-Tesla (uT) */
319 sensors_vec_t magnetic;
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700320
Mathias Agopian27e16682013-07-08 14:00:54 -0700321 /* orientation values are in degrees */
322 sensors_vec_t orientation;
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700323
Mathias Agopian27e16682013-07-08 14:00:54 -0700324 /* gyroscope values are in rad/s */
325 sensors_vec_t gyro;
Makarand Karvekar3120b582010-08-11 15:10:10 -0700326
Mathias Agopian27e16682013-07-08 14:00:54 -0700327 /* temperature is in degrees centigrade (Celsius) */
328 float temperature;
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700329
Mathias Agopian27e16682013-07-08 14:00:54 -0700330 /* distance in centimeters */
331 float distance;
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700332
Mathias Agopian27e16682013-07-08 14:00:54 -0700333 /* light in SI lux units */
334 float light;
Mathias Agopian1832f552010-07-29 15:22:30 -0700335
Mathias Agopian27e16682013-07-08 14:00:54 -0700336 /* pressure in hectopascal (hPa) */
337 float pressure;
Urs Fleischd2ed15a2010-12-29 17:00:33 +0100338
Mathias Agopian27e16682013-07-08 14:00:54 -0700339 /* relative humidity in percent */
340 float relative_humidity;
Mathias Agopiana4557722012-11-28 17:21:55 -0800341
Mathias Agopian27e16682013-07-08 14:00:54 -0700342 /* uncalibrated gyroscope values are in rad/s */
343 uncalibrated_event_t uncalibrated_gyro;
Etienne Le Grandca858142013-02-26 19:17:20 -0800344
Mathias Agopian27e16682013-07-08 14:00:54 -0700345 /* uncalibrated magnetometer values are in micro-Teslas */
346 uncalibrated_event_t uncalibrated_magnetic;
Mathias Agopian16671c52013-07-24 21:07:40 -0700347
Ashutosh Joshid1e25622017-01-10 18:33:53 -0800348 /* uncalibrated accelerometer values are in meter per second per second (m/s^2) */
349 uncalibrated_event_t uncalibrated_accelerometer;
350
Etienne Le Grand7b361582014-05-16 11:08:28 -0700351 /* heart rate data containing value in bpm and status */
352 heart_rate_event_t heart_rate;
Aravind Akella477fbd52014-04-07 22:46:01 +0000353
Mathias Agopian16671c52013-07-24 21:07:40 -0700354 /* this is a special event. see SENSOR_TYPE_META_DATA above.
355 * sensors_meta_data_event_t events are all reported with a type of
356 * SENSOR_TYPE_META_DATA. The handle is ignored and must be zero.
357 */
358 meta_data_event_t meta_data;
Peng Xue641ba92016-01-20 00:27:21 -0800359
360 /* dynamic sensor meta event. See SENSOR_TYPE_DYNAMIC_SENSOR_META type for details */
361 dynamic_sensor_meta_event_t dynamic_sensor_meta;
Peng Xu0743a5c2016-01-21 17:30:02 -0800362
363 /*
364 * special additional sensor information frame, see
365 * SENSOR_TYPE_ADDITIONAL_INFO for details.
366 */
367 additional_info_event_t additional_info;
Mathias Agopian27e16682013-07-08 14:00:54 -0700368 };
Etienne Le Grandca858142013-02-26 19:17:20 -0800369
Mathias Agopian27e16682013-07-08 14:00:54 -0700370 union {
371 uint64_t data[8];
372
373 /* step-counter */
374 uint64_t step_counter;
375 } u64;
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700376 };
Aravind Akella6242f322014-02-28 18:46:19 -0800377
378 /* Reserved flags for internal use. Set to zero. */
379 uint32_t flags;
380
381 uint32_t reserved1[3];
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700382} sensors_event_t;
383
384
Mathias Agopian16671c52013-07-24 21:07:40 -0700385/* see SENSOR_TYPE_META_DATA */
386typedef sensors_event_t sensors_meta_data_event_t;
387
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700388
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800389/**
390 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
391 * and the fields of this data structure must begin with hw_module_t
392 * followed by module specific information.
393 */
394struct sensors_module_t {
395 struct hw_module_t common;
396
397 /**
398 * Enumerate all available sensors. The list is returned in "list".
Colin Cross867e1e32016-10-06 16:44:46 -0700399 * return number of sensors in the list
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800400 */
401 int (*get_sensors_list)(struct sensors_module_t* module,
402 struct sensor_t const** list);
Ashutosh Joshi6507f502015-04-03 16:22:32 -0700403
404 /**
405 * Place the module in a specific mode. The following modes are defined
406 *
Ashutosh Joshi050f2e42015-04-15 13:56:53 -0700407 * 0 - Normal operation. Default state of the module.
Peng Xu0743a5c2016-01-21 17:30:02 -0800408 * 1 - Loopback mode. Data is injected for the supported
Ashutosh Joshi050f2e42015-04-15 13:56:53 -0700409 * sensors by the sensor service in this mode.
Colin Cross867e1e32016-10-06 16:44:46 -0700410 * return 0 on success
Ashutosh Joshi050f2e42015-04-15 13:56:53 -0700411 * -EINVAL if requested mode is not supported
Aravind Akellac7f54132015-06-22 18:26:54 -0700412 * -EPERM if operation is not allowed
Ashutosh Joshi6507f502015-04-03 16:22:32 -0700413 */
414 int (*set_operation_mode)(unsigned int mode);
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800415};
416
417struct sensor_t {
Mathias Agopian1144bea2013-01-29 15:52:10 -0800418
419 /* Name of this sensor.
420 * All sensors of the same "type" must have a different "name".
421 */
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800422 const char* name;
Mathias Agopiana4557722012-11-28 17:21:55 -0800423
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800424 /* vendor of the hardware part */
425 const char* vendor;
Mathias Agopiana4557722012-11-28 17:21:55 -0800426
Mathias Agopiane9eaf372011-11-07 21:32:34 -0800427 /* version of the hardware part + driver. The value of this field
428 * must increase when the driver is updated in a way that changes the
429 * output of this sensor. This is important for fused sensors when the
430 * fusion algorithm is updated.
Aravind Akella6242f322014-02-28 18:46:19 -0800431 */
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800432 int version;
Mathias Agopiana4557722012-11-28 17:21:55 -0800433
434 /* handle that identifies this sensors. This handle is used to reference
435 * this sensor throughout the HAL API.
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800436 */
437 int handle;
Mathias Agopiana4557722012-11-28 17:21:55 -0800438
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800439 /* this sensor's type. */
440 int type;
Mathias Agopiana4557722012-11-28 17:21:55 -0800441
442 /* maximum range of this sensor's value in SI units */
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800443 float maxRange;
Mathias Agopiana4557722012-11-28 17:21:55 -0800444
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800445 /* smallest difference between two values reported by this sensor */
446 float resolution;
Mathias Agopiana4557722012-11-28 17:21:55 -0800447
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800448 /* rough estimate of this sensor's power consumption in mA */
449 float power;
Mathias Agopiana4557722012-11-28 17:21:55 -0800450
Aravind Akellac841efd2014-06-03 19:21:35 -0700451 /* this value depends on the reporting mode:
Mathias Agopiana4557722012-11-28 17:21:55 -0800452 *
453 * continuous: minimum sample period allowed in microseconds
454 * on-change : 0
455 * one-shot :-1
456 * special : 0, unless otherwise noted
457 */
Mathias Agopian1511e202010-07-29 15:33:22 -0700458 int32_t minDelay;
Mathias Agopiana4557722012-11-28 17:21:55 -0800459
Mathias Agopian16671c52013-07-24 21:07:40 -0700460 /* number of events reserved for this sensor in the batch mode FIFO.
461 * If there is a dedicated FIFO for this sensor, then this is the
462 * size of this FIFO. If the FIFO is shared with other sensors,
463 * this is the size reserved for that sensor and it can be zero.
464 */
465 uint32_t fifoReservedEventCount;
466
467 /* maximum number of events of this sensor that could be batched.
468 * This is especially relevant when the FIFO is shared between
469 * several sensors; this value is then set to the size of that FIFO.
470 */
471 uint32_t fifoMaxEventCount;
472
Peng Xu95f79b12017-01-11 14:16:15 -0800473 /* type of this sensor as a string.
474 *
475 * If type is OEM specific or sensor manufacturer specific type
476 * (>=SENSOR_TYPE_DEVICE_PRIVATE_BASE), this string must be defined with reserved domain of
477 * vendor/OEM as a prefix, e.g. com.google.glass.onheaddetector
478 *
479 * For sensors of Android defined types, Android framework will override this value. It is ok to
480 * leave it pointing to an empty string.
Aravind Akella477fbd52014-04-07 22:46:01 +0000481 */
482 const char* stringType;
483
484 /* permission required to see this sensor, register to it and receive data.
485 * Set to "" if no permission is required. Some sensor types like the
486 * heart rate monitor have a mandatory require_permission.
487 * For sensors that always require a specific permission, like the heart
488 * rate monitor, the android framework might overwrite this string
489 * automatically.
490 */
491 const char* requiredPermission;
492
Aravind Akella110d2f22014-09-04 15:36:31 -0700493 /* This value is defined only for continuous mode and on-change sensors. It is the delay between
494 * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
495 * frequencies are requested through batch()/setDelay() the events will be generated at this
496 * frequency instead. It can be used by the framework or applications to estimate when the batch
497 * FIFO may be full.
Aravind Akellac841efd2014-06-03 19:21:35 -0700498 *
499 * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
Aravind Akella110d2f22014-09-04 15:36:31 -0700500 * continuous, on-change: maximum sampling period allowed in microseconds.
501 * one-shot, special : 0
Aravind Akellac841efd2014-06-03 19:21:35 -0700502 * 2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
503 * on 64 bit architectures only for binary compatibility reasons.
Aravind Akella6242f322014-02-28 18:46:19 -0800504 * Availability: SENSORS_DEVICE_API_VERSION_1_3
505 */
506 #ifdef __LP64__
507 int64_t maxDelay;
508 #else
509 int32_t maxDelay;
510 #endif
511
Aravind Akellac841efd2014-06-03 19:21:35 -0700512 /* Flags for sensor. See SENSOR_FLAG_* above. Only the least significant 32 bits are used here.
513 * It is declared as 64 bit on 64 bit architectures only for binary compatibility reasons.
514 * Availability: SENSORS_DEVICE_API_VERSION_1_3
515 */
Aravind Akella6242f322014-02-28 18:46:19 -0800516 #ifdef __LP64__
517 uint64_t flags;
518 #else
519 uint32_t flags;
520 #endif
521
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800522 /* reserved fields, must be zero */
Aravind Akella6242f322014-02-28 18:46:19 -0800523 void* reserved[2];
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800524};
525
Peng Xu08a4dd92016-11-03 11:56:27 -0700526/**
527 * Shared memory information for a direct channel
528 */
529struct sensors_direct_mem_t {
530 int type; // enum SENSOR_DIRECT_MEM_...
531 int format; // enum SENSOR_DIRECT_FMT_...
532 size_t size; // size of the memory region, in bytes
533 const struct native_handle *handle; // shared memory handle, which is interpreted differently
534 // depending on type
535};
536
537/**
538 * Direct channel report configuration
539 */
540struct sensors_direct_cfg_t {
541 int rate_level; // enum SENSOR_DIRECT_RATE_...
542};
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800543
Mathias Agopiana4557722012-11-28 17:21:55 -0800544/*
545 * sensors_poll_device_t is used with SENSORS_DEVICE_API_VERSION_0_1
546 * and is present for backward binary and source compatibility.
Clay Murphy8db1fb42013-12-19 09:58:28 -0800547 * See the Sensors HAL interface section for complete descriptions of the
548 * following functions:
549 * http://source.android.com/devices/sensors/index.html#hal
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800550 */
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700551struct sensors_poll_device_t {
552 struct hw_device_t common;
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700553 int (*activate)(struct sensors_poll_device_t *dev,
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700554 int sensor_handle, int enabled);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700555 int (*setDelay)(struct sensors_poll_device_t *dev,
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700556 int sensor_handle, int64_t sampling_period_ns);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700557 int (*poll)(struct sensors_poll_device_t *dev,
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700558 sensors_event_t* data, int count);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700559};
560
Mathias Agopiana4557722012-11-28 17:21:55 -0800561/*
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700562 * struct sensors_poll_device_1 is used in HAL versions >= SENSORS_DEVICE_API_VERSION_1_0
Mathias Agopiana4557722012-11-28 17:21:55 -0800563 */
564typedef struct sensors_poll_device_1 {
565 union {
566 /* sensors_poll_device_1 is compatible with sensors_poll_device_t,
567 * and can be down-cast to it
568 */
Andrew Hsieh1082c0b2012-12-11 20:51:41 -0800569 struct sensors_poll_device_t v0;
Mathias Agopiana4557722012-11-28 17:21:55 -0800570
571 struct {
572 struct hw_device_t common;
573
Peng Xuaaeeaa42016-10-05 14:56:54 -0700574 /* Activate/de-activate one sensor.
Mathias Agopiana4557722012-11-28 17:21:55 -0800575 *
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700576 * sensor_handle is the handle of the sensor to change.
Mathias Agopiana4557722012-11-28 17:21:55 -0800577 * enabled set to 1 to enable, or 0 to disable the sensor.
578 *
Peng Xuaaeeaa42016-10-05 14:56:54 -0700579 * After sensor de-activation, existing sensor events that have not
580 * been picked up by poll() should be abandoned immediately so that
581 * subsequent activation will not get stale sensor events (events
582 * that is generated prior to the latter activation).
583 *
Clay Murphy8db1fb42013-12-19 09:58:28 -0800584 * Return 0 on success, negative errno code otherwise.
Mathias Agopiana4557722012-11-28 17:21:55 -0800585 */
586 int (*activate)(struct sensors_poll_device_t *dev,
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700587 int sensor_handle, int enabled);
Mathias Agopiana4557722012-11-28 17:21:55 -0800588
589 /**
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700590 * Set the events's period in nanoseconds for a given sensor.
591 * If sampling_period_ns > max_delay it will be truncated to
592 * max_delay and if sampling_period_ns < min_delay it will be
593 * replaced by min_delay.
Mathias Agopiana4557722012-11-28 17:21:55 -0800594 */
595 int (*setDelay)(struct sensors_poll_device_t *dev,
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700596 int sensor_handle, int64_t sampling_period_ns);
Mathias Agopiana4557722012-11-28 17:21:55 -0800597
598 /**
Peng Xu355e3f62016-07-19 15:11:53 -0700599 * Write an array of sensor_event_t to data. The size of the
600 * available buffer is specified by count. Returns number of
601 * valid sensor_event_t.
602 *
603 * This function should block if there is no sensor event
604 * available when being called. Thus, return value should always be
605 * positive.
Mathias Agopiana4557722012-11-28 17:21:55 -0800606 */
607 int (*poll)(struct sensors_poll_device_t *dev,
608 sensors_event_t* data, int count);
609 };
610 };
611
Mathias Agopiana4557722012-11-28 17:21:55 -0800612
613 /*
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700614 * Sets a sensor’s parameters, including sampling frequency and maximum
615 * report latency. This function can be called while the sensor is
616 * activated, in which case it must not cause any sensor measurements to
617 * be lost: transitioning from one sampling rate to the other cannot cause
618 * lost events, nor can transitioning from a high maximum report latency to
619 * a low maximum report latency.
Clay Murphy8db1fb42013-12-19 09:58:28 -0800620 * See the Batching sensor results page for details:
621 * http://source.android.com/devices/sensors/batching.html
Mathias Agopiana4557722012-11-28 17:21:55 -0800622 */
623 int (*batch)(struct sensors_poll_device_1* dev,
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700624 int sensor_handle, int flags, int64_t sampling_period_ns,
625 int64_t max_report_latency_ns);
Mathias Agopiana4557722012-11-28 17:21:55 -0800626
Mathias Agopian16671c52013-07-24 21:07:40 -0700627 /*
628 * Flush adds a META_DATA_FLUSH_COMPLETE event (sensors_event_meta_data_t)
629 * to the end of the "batch mode" FIFO for the specified sensor and flushes
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700630 * the FIFO.
631 * If the FIFO is empty or if the sensor doesn't support batching (FIFO size zero),
Aravind Akellac841efd2014-06-03 19:21:35 -0700632 * it should return SUCCESS along with a trivial META_DATA_FLUSH_COMPLETE event added to the
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700633 * event stream. This applies to all sensors other than one-shot sensors.
634 * If the sensor is a one-shot sensor, flush must return -EINVAL and not generate
635 * any flush complete metadata.
Aravind Akellaa7f2cda2014-08-21 16:31:14 -0700636 * If the sensor is not active at the time flush() is called, flush() should return
637 * -EINVAL.
Mathias Agopian16671c52013-07-24 21:07:40 -0700638 */
Etienne Le Grand772d85a2014-08-19 14:30:19 -0700639 int (*flush)(struct sensors_poll_device_1* dev, int sensor_handle);
Mathias Agopian16671c52013-07-24 21:07:40 -0700640
Ashutosh Joshi6507f502015-04-03 16:22:32 -0700641 /*
Ashutosh Joshi050f2e42015-04-15 13:56:53 -0700642 * Inject a single sensor sample to be to this device.
643 * data points to the sensor event to be injected
Colin Cross867e1e32016-10-06 16:44:46 -0700644 * return 0 on success
Aravind Akellac7f54132015-06-22 18:26:54 -0700645 * -EPERM if operation is not allowed
Ashutosh Joshi050f2e42015-04-15 13:56:53 -0700646 * -EINVAL if sensor event cannot be injected
Ashutosh Joshi6507f502015-04-03 16:22:32 -0700647 */
648 int (*inject_sensor_data)(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
649
Peng Xu08a4dd92016-11-03 11:56:27 -0700650 /*
651 * Register/unregister direct report channel.
652 *
653 * A HAL declares support for direct report by setting non-NULL values for both
654 * register_direct_channel and config_direct_report.
655 *
656 * This function has two operation modes:
657 *
658 * Register: mem != NULL, register a channel using supplied shared memory information. By the
659 * time this function returns, sensors must finish initializing shared memory content
660 * (format dependent, see SENSOR_DIRECT_FMT_*).
661 * Parameters:
662 * mem points to a valid struct sensors_direct_mem_t.
663 * channel_handle is ignored.
664 * Return value:
665 * A handle of channel (>0) when success, which later can be referred in
666 * unregister or config_direct_report call, or error code (<0) if failed
667 * Unregister: mem == NULL, unregister a previously registered channel.
668 * Parameters:
669 * mem set to NULL
670 * channel_handle contains handle of channel to be unregistered
671 * Return value:
672 * 0, even if the channel_handle is invalid, in which case it will be a no-op.
673 */
674 int (*register_direct_channel)(struct sensors_poll_device_1 *dev,
675 const struct sensors_direct_mem_t* mem, int channel_handle);
676
677 /*
678 * Configure direct sensor event report in direct channel.
679 *
680 * Start, modify rate or stop direct report of a sensor in a certain direct channel. A special
681 * case is setting sensor handle -1 to stop means to stop all active sensor report on the
682 * channel specified.
683 *
684 * A HAL declares support for direct report by setting non-NULL values for both
685 * register_direct_channel and config_direct_report.
686 *
687 * Parameters:
688 * sensor_handle sensor to be configured. The sensor has to support direct report
689 * mode by setting flags of sensor_t. Also, direct report mode is only
690 * defined for continuous reporting mode sensors.
691 * channel_handle channel handle to be configured.
692 * config direct report parameters, see sensor_direct_cfg_t.
693 * Return value:
694 * - when sensor is started or sensor rate level is changed: return positive identifier of
695 * sensor in specified channel if successful, otherwise return negative error code.
696 * - when sensor is stopped: return 0 for success or negative error code for failure.
697 */
698 int (*config_direct_report)(struct sensors_poll_device_1 *dev,
699 int sensor_handle, int channel_handle, const struct sensors_direct_cfg_t *config);
700
701 /*
702 * Reserved for future use.
703 */
704 void (*reserved_procs[5])(void);
Mathias Agopiana4557722012-11-28 17:21:55 -0800705
706} sensors_poll_device_1_t;
707
708
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800709/** convenience API for opening and closing a device */
710
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700711static inline int sensors_open(const struct hw_module_t* module,
712 struct sensors_poll_device_t** device) {
713 return module->methods->open(module,
Colin Crosscc8d9f92016-10-06 16:44:23 -0700714 SENSORS_HARDWARE_POLL, TO_HW_DEVICE_T_OPEN(device));
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700715}
716
717static inline int sensors_close(struct sensors_poll_device_t* device) {
718 return device->common.close(&device->common);
719}
720
Mathias Agopiana4557722012-11-28 17:21:55 -0800721static inline int sensors_open_1(const struct hw_module_t* module,
Andrew Hsieh1082c0b2012-12-11 20:51:41 -0800722 sensors_poll_device_1_t** device) {
Mathias Agopiana4557722012-11-28 17:21:55 -0800723 return module->methods->open(module,
Colin Crosscc8d9f92016-10-06 16:44:23 -0700724 SENSORS_HARDWARE_POLL, TO_HW_DEVICE_T_OPEN(device));
Mathias Agopiana4557722012-11-28 17:21:55 -0800725}
726
Andrew Hsieh1082c0b2012-12-11 20:51:41 -0800727static inline int sensors_close_1(sensors_poll_device_1_t* device) {
Mathias Agopiana4557722012-11-28 17:21:55 -0800728 return device->common.close(&device->common);
729}
730
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800731__END_DECLS
732
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800733#endif // ANDROID_SENSORS_INTERFACE_H