Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 17 | /** |
| 18 | * @addtogroup Sensor |
| 19 | * @{ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file sensor.h |
| 24 | */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 25 | |
| 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 Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 45 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 46 | * 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 |
| 56 | extern "C" { |
| 57 | #endif |
| 58 | |
Peng Xu | 47cddca | 2017-02-15 23:31:22 -0800 | [diff] [blame^] | 59 | typedef struct AHardwareBuffer AHardwareBuffer; |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 60 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Sensor types. |
Johan Euphrosine | 7d319fc | 2015-08-20 18:13:43 -0700 | [diff] [blame] | 63 | * (keep in sync with hardware/sensors.h) |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 64 | */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 65 | enum { |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 66 | /** |
| 67 | * {@link ASENSOR_TYPE_ACCELEROMETER} |
| 68 | * reporting-mode: continuous |
| 69 | * |
| 70 | * All values are in SI units (m/s^2) and measure the acceleration of the |
| 71 | * device minus the force of gravity. |
| 72 | */ |
Johan Euphrosine | 7d319fc | 2015-08-20 18:13:43 -0700 | [diff] [blame] | 73 | ASENSOR_TYPE_ACCELEROMETER = 1, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 74 | /** |
| 75 | * {@link ASENSOR_TYPE_MAGNETIC_FIELD} |
| 76 | * reporting-mode: continuous |
| 77 | * |
| 78 | * All values are in micro-Tesla (uT) and measure the geomagnetic |
| 79 | * field in the X, Y and Z axis. |
| 80 | */ |
Johan Euphrosine | 7d319fc | 2015-08-20 18:13:43 -0700 | [diff] [blame] | 81 | ASENSOR_TYPE_MAGNETIC_FIELD = 2, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 82 | /** |
| 83 | * {@link ASENSOR_TYPE_GYROSCOPE} |
| 84 | * reporting-mode: continuous |
| 85 | * |
| 86 | * All values are in radians/second and measure the rate of rotation |
| 87 | * around the X, Y and Z axis. |
| 88 | */ |
Johan Euphrosine | 7d319fc | 2015-08-20 18:13:43 -0700 | [diff] [blame] | 89 | ASENSOR_TYPE_GYROSCOPE = 4, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 90 | /** |
| 91 | * {@link ASENSOR_TYPE_LIGHT} |
| 92 | * reporting-mode: on-change |
| 93 | * |
| 94 | * The light sensor value is returned in SI lux units. |
| 95 | */ |
Johan Euphrosine | 7d319fc | 2015-08-20 18:13:43 -0700 | [diff] [blame] | 96 | ASENSOR_TYPE_LIGHT = 5, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 97 | /** |
| 98 | * {@link ASENSOR_TYPE_PROXIMITY} |
| 99 | * reporting-mode: on-change |
| 100 | * |
| 101 | * The proximity sensor which turns the screen off and back on during calls is the |
| 102 | * wake-up proximity sensor. Implement wake-up proximity sensor before implementing |
| 103 | * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag |
| 104 | * SENSOR_FLAG_WAKE_UP. |
| 105 | * The value corresponds to the distance to the nearest object in centimeters. |
| 106 | */ |
Johan Euphrosine | 7d319fc | 2015-08-20 18:13:43 -0700 | [diff] [blame] | 107 | ASENSOR_TYPE_PROXIMITY = 8, |
| 108 | /** |
| 109 | * {@link ASENSOR_TYPE_LINEAR_ACCELERATION} |
| 110 | * reporting-mode: continuous |
| 111 | * |
| 112 | * All values are in SI units (m/s^2) and measure the acceleration of the |
| 113 | * device not including the force of gravity. |
| 114 | */ |
| 115 | ASENSOR_TYPE_LINEAR_ACCELERATION = 10 |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Sensor accuracy measure. |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 120 | */ |
| 121 | enum { |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 122 | /** no contact */ |
Etienne Le Grand | 630e31d | 2014-05-22 17:15:08 -0700 | [diff] [blame] | 123 | ASENSOR_STATUS_NO_CONTACT = -1, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 124 | /** unreliable */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 125 | ASENSOR_STATUS_UNRELIABLE = 0, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 126 | /** low accuracy */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 127 | ASENSOR_STATUS_ACCURACY_LOW = 1, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 128 | /** medium accuracy */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 129 | ASENSOR_STATUS_ACCURACY_MEDIUM = 2, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 130 | /** high accuracy */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 131 | ASENSOR_STATUS_ACCURACY_HIGH = 3 |
| 132 | }; |
| 133 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 134 | /** |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 135 | * Sensor Reporting Modes. |
| 136 | */ |
| 137 | enum { |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 138 | /** continuous reporting */ |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 139 | AREPORTING_MODE_CONTINUOUS = 0, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 140 | /** reporting on change */ |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 141 | AREPORTING_MODE_ON_CHANGE = 1, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 142 | /** on shot reporting */ |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 143 | AREPORTING_MODE_ONE_SHOT = 2, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 144 | /** special trigger reporting */ |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 145 | AREPORTING_MODE_SPECIAL_TRIGGER = 3 |
| 146 | }; |
| 147 | |
Peng Xu | 47cddca | 2017-02-15 23:31:22 -0800 | [diff] [blame^] | 148 | /** |
| 149 | * Sensor Direct Report Rates. |
| 150 | */ |
| 151 | enum { |
| 152 | /** stopped */ |
| 153 | ASENSOR_DIRECT_RATE_STOP = 0, |
| 154 | /** nominal 50Hz */ |
| 155 | ASENSOR_DIRECT_RATE_NORMAL = 1, |
| 156 | /** nominal 200Hz */ |
| 157 | ASENSOR_DIRECT_RATE_FAST = 2, |
| 158 | /** nominal 800Hz */ |
| 159 | ASENSOR_DIRECT_RATE_VERY_FAST = 3 |
| 160 | }; |
| 161 | |
| 162 | /** |
| 163 | * Sensor Direct Channel Type. |
| 164 | */ |
| 165 | enum { |
| 166 | /** shared memory created by ASharedMemory_create */ |
| 167 | ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY = 1, |
| 168 | /** AHardwareBuffer */ |
| 169 | ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER = 2 |
| 170 | }; |
| 171 | |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 172 | /* |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 173 | * A few useful constants |
| 174 | */ |
| 175 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 176 | /** Earth's gravity in m/s^2 */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 177 | #define ASENSOR_STANDARD_GRAVITY (9.80665f) |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 178 | /** Maximum magnetic field on Earth's surface in uT */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 179 | #define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f) |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 180 | /** Minimum magnetic field on Earth's surface in uT*/ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 181 | #define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f) |
| 182 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 183 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 184 | * A sensor event. |
| 185 | */ |
| 186 | |
| 187 | /* NOTE: Must match hardware/sensors.h */ |
| 188 | typedef struct ASensorVector { |
| 189 | union { |
| 190 | float v[3]; |
| 191 | struct { |
| 192 | float x; |
| 193 | float y; |
| 194 | float z; |
| 195 | }; |
| 196 | struct { |
| 197 | float azimuth; |
| 198 | float pitch; |
| 199 | float roll; |
| 200 | }; |
| 201 | }; |
| 202 | int8_t status; |
| 203 | uint8_t reserved[3]; |
| 204 | } ASensorVector; |
| 205 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 206 | typedef struct AMetaDataEvent { |
| 207 | int32_t what; |
| 208 | int32_t sensor; |
| 209 | } AMetaDataEvent; |
| 210 | |
| 211 | typedef struct AUncalibratedEvent { |
Peng Xu | 9e72046 | 2016-01-26 18:48:54 -0800 | [diff] [blame] | 212 | union { |
| 213 | float uncalib[3]; |
| 214 | struct { |
| 215 | float x_uncalib; |
| 216 | float y_uncalib; |
| 217 | float z_uncalib; |
| 218 | }; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 219 | }; |
Peng Xu | 9e72046 | 2016-01-26 18:48:54 -0800 | [diff] [blame] | 220 | union { |
| 221 | float bias[3]; |
| 222 | struct { |
| 223 | float x_bias; |
| 224 | float y_bias; |
| 225 | float z_bias; |
| 226 | }; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 227 | }; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 228 | } AUncalibratedEvent; |
| 229 | |
Etienne Le Grand | 630e31d | 2014-05-22 17:15:08 -0700 | [diff] [blame] | 230 | typedef struct AHeartRateEvent { |
Peng Xu | 9e72046 | 2016-01-26 18:48:54 -0800 | [diff] [blame] | 231 | float bpm; |
| 232 | int8_t status; |
Etienne Le Grand | 630e31d | 2014-05-22 17:15:08 -0700 | [diff] [blame] | 233 | } AHeartRateEvent; |
| 234 | |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 235 | typedef struct ADynamicSensorEvent { |
Peng Xu | 9e72046 | 2016-01-26 18:48:54 -0800 | [diff] [blame] | 236 | int32_t connected; |
| 237 | int32_t handle; |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 238 | } ADynamicSensorEvent; |
| 239 | |
Peng Xu | 9e72046 | 2016-01-26 18:48:54 -0800 | [diff] [blame] | 240 | typedef struct { |
| 241 | int32_t type; |
| 242 | int32_t serial; |
| 243 | union { |
| 244 | int32_t data_int32[14]; |
| 245 | float data_float[14]; |
| 246 | }; |
| 247 | } AAdditionalInfoEvent; |
| 248 | |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 249 | /* NOTE: Must match hardware/sensors.h */ |
| 250 | typedef struct ASensorEvent { |
| 251 | int32_t version; /* sizeof(struct ASensorEvent) */ |
| 252 | int32_t sensor; |
| 253 | int32_t type; |
| 254 | int32_t reserved0; |
| 255 | int64_t timestamp; |
| 256 | union { |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 257 | union { |
| 258 | float data[16]; |
| 259 | ASensorVector vector; |
| 260 | ASensorVector acceleration; |
| 261 | ASensorVector magnetic; |
| 262 | float temperature; |
| 263 | float distance; |
| 264 | float light; |
| 265 | float pressure; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 266 | float relative_humidity; |
| 267 | AUncalibratedEvent uncalibrated_gyro; |
| 268 | AUncalibratedEvent uncalibrated_magnetic; |
| 269 | AMetaDataEvent meta_data; |
Etienne Le Grand | 630e31d | 2014-05-22 17:15:08 -0700 | [diff] [blame] | 270 | AHeartRateEvent heart_rate; |
Peng Xu | 2576cb6 | 2016-01-20 00:22:09 -0800 | [diff] [blame] | 271 | ADynamicSensorEvent dynamic_sensor_meta; |
Peng Xu | 9e72046 | 2016-01-26 18:48:54 -0800 | [diff] [blame] | 272 | AAdditionalInfoEvent additional_info; |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 273 | }; |
| 274 | union { |
| 275 | uint64_t data[8]; |
| 276 | uint64_t step_counter; |
| 277 | } u64; |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 278 | }; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 279 | |
| 280 | uint32_t flags; |
| 281 | int32_t reserved1[3]; |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 282 | } ASensorEvent; |
| 283 | |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 284 | struct ASensorManager; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 285 | /** |
| 286 | * {@link ASensorManager} is an opaque type to manage sensors and |
| 287 | * events queues. |
| 288 | * |
| 289 | * {@link ASensorManager} is a singleton that can be obtained using |
| 290 | * ASensorManager_getInstance(). |
| 291 | * |
| 292 | * This file provides a set of functions that uses {@link |
| 293 | * ASensorManager} to access and list hardware sensors, and |
| 294 | * create and destroy event queues: |
| 295 | * - ASensorManager_getSensorList() |
| 296 | * - ASensorManager_getDefaultSensor() |
| 297 | * - ASensorManager_getDefaultSensorEx() |
| 298 | * - ASensorManager_createEventQueue() |
| 299 | * - ASensorManager_destroyEventQueue() |
| 300 | */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 301 | typedef struct ASensorManager ASensorManager; |
| 302 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 303 | |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 304 | struct ASensorEventQueue; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 305 | /** |
| 306 | * {@link ASensorEventQueue} is an opaque type that provides access to |
| 307 | * {@link ASensorEvent} from hardware sensors. |
| 308 | * |
| 309 | * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue(). |
| 310 | * |
| 311 | * This file provides a set of functions to enable and disable |
| 312 | * sensors, check and get events, and set event rates on a {@link |
| 313 | * ASensorEventQueue}. |
| 314 | * - ASensorEventQueue_enableSensor() |
| 315 | * - ASensorEventQueue_disableSensor() |
| 316 | * - ASensorEventQueue_hasEvents() |
| 317 | * - ASensorEventQueue_getEvents() |
| 318 | * - ASensorEventQueue_setEventRate() |
| 319 | */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 320 | typedef struct ASensorEventQueue ASensorEventQueue; |
| 321 | |
| 322 | struct ASensor; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 323 | /** |
| 324 | * {@link ASensor} is an opaque type that provides information about |
| 325 | * an hardware sensors. |
| 326 | * |
| 327 | * A {@link ASensor} pointer can be obtained using |
| 328 | * ASensorManager_getDefaultSensor(), |
| 329 | * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}. |
| 330 | * |
| 331 | * This file provides a set of functions to access properties of a |
| 332 | * {@link ASensor}: |
| 333 | * - ASensor_getName() |
| 334 | * - ASensor_getVendor() |
| 335 | * - ASensor_getType() |
| 336 | * - ASensor_getResolution() |
| 337 | * - ASensor_getMinDelay() |
| 338 | * - ASensor_getFifoMaxEventCount() |
| 339 | * - ASensor_getFifoReservedEventCount() |
| 340 | * - ASensor_getStringType() |
| 341 | * - ASensor_getReportingMode() |
| 342 | * - ASensor_isWakeUpSensor() |
| 343 | */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 344 | typedef struct ASensor ASensor; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 345 | /** |
| 346 | * {@link ASensorRef} is a type for constant pointers to {@link ASensor}. |
| 347 | * |
| 348 | * This is used to define entry in {@link ASensorList} arrays. |
| 349 | */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 350 | typedef ASensor const* ASensorRef; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 351 | /** |
| 352 | * {@link ASensorList} is an array of reference to {@link ASensor}. |
| 353 | * |
| 354 | * A {@link ASensorList} can be initialized using ASensorManager_getSensorList(). |
| 355 | */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 356 | typedef ASensorRef const* ASensorList; |
| 357 | |
| 358 | /*****************************************************************************/ |
| 359 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 360 | /** |
Svet Ganov | 5fa32d4 | 2015-05-07 10:50:59 -0700 | [diff] [blame] | 361 | * Get a reference to the sensor manager. ASensorManager is a singleton |
| 362 | * per package as different packages may have access to different sensors. |
| 363 | * |
| 364 | * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead. |
| 365 | * |
| 366 | * Example: |
| 367 | * |
| 368 | * ASensorManager* sensorManager = ASensorManager_getInstance(); |
| 369 | * |
| 370 | */ |
| 371 | __attribute__ ((deprecated)) ASensorManager* ASensorManager_getInstance(); |
| 372 | |
| 373 | /* |
| 374 | * Get a reference to the sensor manager. ASensorManager is a singleton |
| 375 | * per package as different packages may have access to different sensors. |
| 376 | * |
| 377 | * Example: |
| 378 | * |
| 379 | * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz"); |
| 380 | * |
| 381 | */ |
| 382 | ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName); |
| 383 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 384 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 385 | * Returns the list of available sensors. |
| 386 | */ |
| 387 | int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list); |
| 388 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 389 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 390 | * Returns the default sensor for the given type, or NULL if no sensor |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 391 | * of that type exists. |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 392 | */ |
| 393 | ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type); |
| 394 | |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame] | 395 | #if __ANDROID_API__ >= 21 |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 396 | /** |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 397 | * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor |
| 398 | * of this type and wakeUp properties exists. |
| 399 | */ |
| 400 | ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type, |
| 401 | bool wakeUp); |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame] | 402 | #endif |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 403 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 404 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 405 | * Creates a new sensor event queue and associate it with a looper. |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 406 | * |
| 407 | * "ident" is a identifier for the events that will be returned when |
| 408 | * calling ALooper_pollOnce(). The identifier must be >= 0, or |
| 409 | * ALOOPER_POLL_CALLBACK if providing a non-NULL callback. |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 410 | */ |
| 411 | ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager, |
| 412 | ALooper* looper, int ident, ALooper_callbackFunc callback, void* data); |
| 413 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 414 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 415 | * Destroys the event queue and free all resources associated to it. |
| 416 | */ |
| 417 | int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue); |
| 418 | |
Peng Xu | 47cddca | 2017-02-15 23:31:22 -0800 | [diff] [blame^] | 419 | #if __ANDROID_API__ >= __ANDROID_API_O__ |
| 420 | /** |
| 421 | * Create direct channel based on shared memory |
| 422 | * |
| 423 | * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} to be used |
| 424 | * for configuring sensor direct report. |
| 425 | * |
| 426 | * \param manager the {@link ASensorManager} instance obtained from |
| 427 | * {@link ASensorManager_getInstanceForPackage}. |
| 428 | * \param fd file descriptor representing a shared memory created by |
| 429 | * {@link ASharedMemory_create} |
| 430 | * \param size size to be used, must be less or equal to size of shared memory. |
| 431 | * |
| 432 | * \return a positive integer as a channel id to be used in |
| 433 | * {@link ASensorManager_destroyDirectChannel} and |
| 434 | * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures. |
| 435 | */ |
| 436 | int ASensorManager_createSharedMemoryDirectChannel(ASensorManager* manager, int fd, size_t size); |
| 437 | |
| 438 | /** |
| 439 | * Create direct channel based on AHardwareBuffer |
| 440 | * |
| 441 | * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER} type to be used |
| 442 | * for configuring sensor direct report. |
| 443 | * |
| 444 | * \param manager the {@link ASensorManager} instance obtained from |
| 445 | * {@link ASensorManager_getInstanceForPackage}. |
| 446 | * \param buffer {@link AHardwareBuffer} instance created by {@link AHardwareBuffer_allocate}. |
| 447 | * \param size the intended size to be used, must be less or equal to size of buffer. |
| 448 | * |
| 449 | * \return a positive integer as a channel id to be used in |
| 450 | * {@link ASensorManager_destroyDirectChannel} and |
| 451 | * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures. |
| 452 | */ |
| 453 | int ASensorManager_createHardwareBufferDirectChannel( |
| 454 | ASensorManager* manager, AHardwareBuffer const * buffer, size_t size); |
| 455 | |
| 456 | /** |
| 457 | * Destroy a direct channel |
| 458 | * |
| 459 | * Destroy a direct channel previously created using {@link ASensorManager_createDirectChannel}. |
| 460 | * The buffer used for creating direct channel does not get destroyed with |
| 461 | * {@link ASensorManager_destroy} and has to be close or released separately. |
| 462 | * |
| 463 | * \param manager the {@link ASensorManager} instance obtained from |
| 464 | * {@link ASensorManager_getInstanceForPackage}. |
| 465 | * \param channelId channel id (a positive integer) returned from |
| 466 | * {@link ASensorManager_createSharedMemoryDirectChannel} or |
| 467 | * {@link ASensorManager_createHardwareBufferDirectChannel}. |
| 468 | */ |
| 469 | void ASensorManager_destroyDirectChannel(ASensorManager* manager, int channelId); |
| 470 | |
| 471 | /** |
| 472 | * Configure direct report on channel |
| 473 | * |
| 474 | * Configure sensor direct report on a direct channel: set rate to value other than |
| 475 | * {@link ASENSOR_DIRECT_RATE_STOP} so that sensor event can be directly |
| 476 | * written into the shared memory region used for creating the buffer; set rate to |
| 477 | * {@link ASENSOR_DIRECT_RATE_STOP} will stop the sensor direct report. |
| 478 | * |
| 479 | * To stop all active sensor direct report configured to a channel, set sensor to NULL and rate to |
| 480 | * {@link ASENSOR_DIRECT_RATE_STOP}. |
| 481 | * |
| 482 | * In order to successfully configure a direct report, the sensor has to support the specified rate |
| 483 | * and the channel type, which can be checked by {@link ASensor_getHighestDirectReportRateLevel} and |
| 484 | * {@link ASensor_isDirectChannelTypeSupported}, respectively. |
| 485 | * |
| 486 | * Example: |
| 487 | * \code{.cpp} |
| 488 | * ASensorManager *manager = ...; |
| 489 | * ASensor *sensor = ...; |
| 490 | * int channelId = ...; |
| 491 | * |
| 492 | * ASensorManager_configureDirectReport( |
| 493 | * manager, sensor, channel_id, ASENSOR_DIRECT_RATE_FAST); |
| 494 | * \endcode |
| 495 | * |
| 496 | * \param manager the {@link ASensorManager} instance obtained from |
| 497 | * {@link ASensorManager_getInstanceForPackage}. |
| 498 | * \param sensor a {@link ASensor} to denote which sensor to be operate. It can be NULL if rate |
| 499 | * is {@link ASENSOR_DIRECT_RATE_STOP}, denoting stopping of all active sensor |
| 500 | * direct report. |
| 501 | * \param channelId channel id (a positive integer) returned from |
| 502 | * {@link ASensorManager_createSharedMemoryDirectChannel} or |
| 503 | * {@link ASensorManager_createHardwareBufferDirectChannel}. |
| 504 | * |
| 505 | * \return 0 for success or negative integer for failure. |
| 506 | */ |
| 507 | int ASensorManager_configureDirectReport( |
| 508 | ASensorManager* manager, ASensor const* sensor, int channelId, int rate); |
| 509 | #endif |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 510 | |
| 511 | /*****************************************************************************/ |
| 512 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 513 | /** |
Aniroop Mathur | da94fd8 | 2015-11-03 01:47:46 +0530 | [diff] [blame] | 514 | * Enable the selected sensor with a specified sampling period and max batch report latency. |
| 515 | * Returns a negative error code on failure. |
Aniroop Mathur | e96e577 | 2016-12-13 00:04:06 +0530 | [diff] [blame] | 516 | * Note: To disable the selected sensor, use ASensorEventQueue_disableSensor() same as before. |
Aniroop Mathur | da94fd8 | 2015-11-03 01:47:46 +0530 | [diff] [blame] | 517 | */ |
| 518 | int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor, |
| 519 | int32_t samplingPeriodUs, int maxBatchReportLatencyUs); |
| 520 | |
| 521 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 522 | * Enable the selected sensor. Returns a negative error code on failure. |
| 523 | */ |
| 524 | int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor); |
| 525 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 526 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 527 | * Disable the selected sensor. Returns a negative error code on failure. |
| 528 | */ |
| 529 | int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor); |
| 530 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 531 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 532 | * Sets the delivery rate of events in microseconds for the given sensor. |
| 533 | * Note that this is a hint only, generally event will arrive at a higher |
| 534 | * rate. It is an error to set a rate inferior to the value returned by |
| 535 | * ASensor_getMinDelay(). |
| 536 | * Returns a negative error code on failure. |
| 537 | */ |
| 538 | int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec); |
| 539 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 540 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 541 | * Returns true if there are one or more events available in the |
| 542 | * sensor queue. Returns 1 if the queue has events; 0 if |
| 543 | * it does not have events; and a negative value if there is an error. |
| 544 | */ |
| 545 | int ASensorEventQueue_hasEvents(ASensorEventQueue* queue); |
| 546 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 547 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 548 | * Returns the next available events from the queue. Returns a negative |
| 549 | * value if no events are available or an error has occurred, otherwise |
| 550 | * the number of events returned. |
| 551 | * |
| 552 | * Examples: |
| 553 | * ASensorEvent event; |
| 554 | * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1); |
| 555 | * |
| 556 | * ASensorEvent eventBuffer[8]; |
| 557 | * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8); |
| 558 | * |
| 559 | */ |
| 560 | ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, |
| 561 | ASensorEvent* events, size_t count); |
| 562 | |
| 563 | |
| 564 | /*****************************************************************************/ |
| 565 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 566 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 567 | * Returns this sensor's name (non localized) |
| 568 | */ |
| 569 | const char* ASensor_getName(ASensor const* sensor); |
| 570 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 571 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 572 | * Returns this sensor's vendor's name (non localized) |
| 573 | */ |
| 574 | const char* ASensor_getVendor(ASensor const* sensor); |
| 575 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 576 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 577 | * Return this sensor's type |
| 578 | */ |
| 579 | int ASensor_getType(ASensor const* sensor); |
| 580 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 581 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 582 | * Returns this sensors's resolution |
| 583 | */ |
| 584 | float ASensor_getResolution(ASensor const* sensor); |
| 585 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 586 | /** |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 587 | * Returns the minimum delay allowed between events in microseconds. |
| 588 | * A value of zero means that this sensor doesn't report events at a |
| 589 | * constant rate, but rather only when a new data is available. |
| 590 | */ |
| 591 | int ASensor_getMinDelay(ASensor const* sensor); |
| 592 | |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame] | 593 | #if __ANDROID_API__ >= 21 |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 594 | /** |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 595 | * Returns the maximum size of batches for this sensor. Batches will often be |
| 596 | * smaller, as the hardware fifo might be used for other sensors. |
| 597 | */ |
| 598 | int ASensor_getFifoMaxEventCount(ASensor const* sensor); |
| 599 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 600 | /** |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 601 | * Returns the hardware batch fifo size reserved to this sensor. |
| 602 | */ |
| 603 | int ASensor_getFifoReservedEventCount(ASensor const* sensor); |
| 604 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 605 | /** |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 606 | * Returns this sensor's string type. |
| 607 | */ |
| 608 | const char* ASensor_getStringType(ASensor const* sensor); |
| 609 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 610 | /** |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 611 | * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants. |
| 612 | */ |
| 613 | int ASensor_getReportingMode(ASensor const* sensor); |
| 614 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 615 | /** |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 616 | * Returns true if this is a wake up sensor, false otherwise. |
| 617 | */ |
| 618 | bool ASensor_isWakeUpSensor(ASensor const* sensor); |
Dan Albert | 494ed55 | 2016-09-23 15:57:45 -0700 | [diff] [blame] | 619 | #endif /* __ANDROID_API__ >= 21 */ |
Aravind Akella | b37ba39 | 2014-08-05 14:53:07 -0700 | [diff] [blame] | 620 | |
Peng Xu | 47cddca | 2017-02-15 23:31:22 -0800 | [diff] [blame^] | 621 | #if __ANDROID_API__ >= __ANDROID_API_O__ |
| 622 | /** |
| 623 | * Test if sensor supports a certain type of direct channel. |
| 624 | * |
| 625 | * \param sensor a {@link ASensor} to denote the sensor to be checked. |
| 626 | * \param channelType Channel type constant, either |
| 627 | * {@ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} |
| 628 | * or {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER}. |
| 629 | * \returns true if sensor supports the specified direct channel type. |
| 630 | */ |
| 631 | bool ASensor_isDirectChannelTypeSupported(ASensor const* sensor, int channelType); |
| 632 | /** |
| 633 | * Get the highest direct rate level that a sensor support. |
| 634 | * |
| 635 | * \param sensor a {@link ASensor} to denote the sensor to be checked. |
| 636 | * |
| 637 | * \return a ASENSOR_DIRECT_RATE_... enum denoting the highest rate level supported by the sensor. |
| 638 | * If return value is {@link ASENSOR_DIRECT_RATE_STOP}, it means the sensor |
| 639 | * does not support direct report. |
| 640 | */ |
| 641 | int ASensor_getHighestDirectReportRateLevel(ASensor const* sensor); |
| 642 | #endif |
| 643 | |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 644 | #ifdef __cplusplus |
| 645 | }; |
| 646 | #endif |
| 647 | |
| 648 | #endif // ANDROID_SENSOR_H |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 649 | |
| 650 | /** @} */ |