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 17 /** 18 * Structures and functions to receive and process sensor events in 19 * native code. 20 * 21 * @addtogroup Sensor 22 * @{ 23 */ 24 25 /** 26 * @file sensor.h 27 */ 28 29 #ifndef ANDROID_SENSOR_H 30 #define ANDROID_SENSOR_H 31 32 /****************************************************************** 33 * 34 * IMPORTANT NOTICE: 35 * 36 * This file is part of Android's set of stable system headers 37 * exposed by the Android NDK (Native Development Kit). 38 * 39 * Third-party source AND binary code relies on the definitions 40 * here to be FROZEN ON ALL UPCOMING PLATFORM RELEASES. 41 * 42 * - DO NOT MODIFY ENUMS (EXCEPT IF YOU ADD NEW 32-BIT VALUES) 43 * - DO NOT MODIFY CONSTANTS OR FUNCTIONAL MACROS 44 * - DO NOT CHANGE THE SIGNATURE OF FUNCTIONS IN ANY WAY 45 * - DO NOT CHANGE THE LAYOUT OR SIZE OF STRUCTURES 46 */ 47 48 #include <android/looper.h> 49 50 #include <stdbool.h> 51 #include <sys/types.h> 52 #include <math.h> 53 #include <stdint.h> 54 55 #if !defined(__INTRODUCED_IN) 56 #define __INTRODUCED_IN(__api_level) /* nothing */ 57 #endif 58 #if !defined(__DEPRECATED_IN) 59 #define __DEPRECATED_IN(__api_level) __attribute__((__deprecated__)) 60 #endif 61 62 #ifdef __cplusplus 63 extern "C" { 64 #endif 65 66 typedef struct AHardwareBuffer AHardwareBuffer; 67 68 #define ASENSOR_RESOLUTION_INVALID (nanf("")) 69 #define ASENSOR_FIFO_COUNT_INVALID (-1) 70 #define ASENSOR_DELAY_INVALID INT32_MIN 71 #define ASENSOR_INVALID (-1) 72 73 /* (Keep in sync with hardware/sensors-base.h and Sensor.java.) */ 74 75 /** 76 * Sensor types. 77 * 78 * See 79 * [android.hardware.SensorEvent#values](https://developer.android.com/reference/android/hardware/SensorEvent.html#values) 80 * for detailed explanations of the data returned for each of these types. 81 */ 82 enum { 83 /** 84 * Invalid sensor type. Returned by {@link ASensor_getType} as error value. 85 */ 86 ASENSOR_TYPE_INVALID = -1, 87 /** 88 * {@link ASENSOR_TYPE_ACCELEROMETER} 89 * reporting-mode: continuous 90 * 91 * All values are in SI units (m/s^2) and measure the acceleration of the 92 * device minus the force of gravity. 93 */ 94 ASENSOR_TYPE_ACCELEROMETER = 1, 95 /** 96 * {@link ASENSOR_TYPE_MAGNETIC_FIELD} 97 * reporting-mode: continuous 98 * 99 * All values are in micro-Tesla (uT) and measure the geomagnetic 100 * field in the X, Y and Z axis. 101 */ 102 ASENSOR_TYPE_MAGNETIC_FIELD = 2, 103 /** 104 * {@link ASENSOR_TYPE_GYROSCOPE} 105 * reporting-mode: continuous 106 * 107 * All values are in radians/second and measure the rate of rotation 108 * around the X, Y and Z axis. 109 */ 110 ASENSOR_TYPE_GYROSCOPE = 4, 111 /** 112 * {@link ASENSOR_TYPE_LIGHT} 113 * reporting-mode: on-change 114 * 115 * The light sensor value is returned in SI lux units. 116 */ 117 ASENSOR_TYPE_LIGHT = 5, 118 /** 119 * {@link ASENSOR_TYPE_PRESSURE} 120 * 121 * The pressure sensor value is returned in hPa (millibar). 122 */ 123 ASENSOR_TYPE_PRESSURE = 6, 124 /** 125 * {@link ASENSOR_TYPE_PROXIMITY} 126 * reporting-mode: on-change 127 * 128 * The proximity sensor which turns the screen off and back on during calls is the 129 * wake-up proximity sensor. Implement wake-up proximity sensor before implementing 130 * a non wake-up proximity sensor. For the wake-up proximity sensor set the flag 131 * SENSOR_FLAG_WAKE_UP. 132 * The value corresponds to the distance to the nearest object in centimeters. 133 */ 134 ASENSOR_TYPE_PROXIMITY = 8, 135 /** 136 * {@link ASENSOR_TYPE_GRAVITY} 137 * 138 * All values are in SI units (m/s^2) and measure the direction and 139 * magnitude of gravity. When the device is at rest, the output of 140 * the gravity sensor should be identical to that of the accelerometer. 141 */ 142 ASENSOR_TYPE_GRAVITY = 9, 143 /** 144 * {@link ASENSOR_TYPE_LINEAR_ACCELERATION} 145 * reporting-mode: continuous 146 * 147 * All values are in SI units (m/s^2) and measure the acceleration of the 148 * device not including the force of gravity. 149 */ 150 ASENSOR_TYPE_LINEAR_ACCELERATION = 10, 151 /** 152 * {@link ASENSOR_TYPE_ROTATION_VECTOR} 153 */ 154 ASENSOR_TYPE_ROTATION_VECTOR = 11, 155 /** 156 * {@link ASENSOR_TYPE_RELATIVE_HUMIDITY} 157 * 158 * The relative humidity sensor value is returned in percent. 159 */ 160 ASENSOR_TYPE_RELATIVE_HUMIDITY = 12, 161 /** 162 * {@link ASENSOR_TYPE_AMBIENT_TEMPERATURE} 163 * 164 * The ambient temperature sensor value is returned in Celcius. 165 */ 166 ASENSOR_TYPE_AMBIENT_TEMPERATURE = 13, 167 /** 168 * {@link ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED} 169 */ 170 ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14, 171 /** 172 * {@link ASENSOR_TYPE_GAME_ROTATION_VECTOR} 173 */ 174 ASENSOR_TYPE_GAME_ROTATION_VECTOR = 15, 175 /** 176 * {@link ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED} 177 */ 178 ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED = 16, 179 /** 180 * {@link ASENSOR_TYPE_SIGNIFICANT_MOTION} 181 */ 182 ASENSOR_TYPE_SIGNIFICANT_MOTION = 17, 183 /** 184 * {@link ASENSOR_TYPE_STEP_DETECTOR} 185 */ 186 ASENSOR_TYPE_STEP_DETECTOR = 18, 187 /** 188 * {@link ASENSOR_TYPE_STEP_COUNTER} 189 */ 190 ASENSOR_TYPE_STEP_COUNTER = 19, 191 /** 192 * {@link ASENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR} 193 */ 194 ASENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR = 20, 195 /** 196 * {@link ASENSOR_TYPE_HEART_RATE} 197 */ 198 ASENSOR_TYPE_HEART_RATE = 21, 199 /** 200 * {@link ASENSOR_TYPE_POSE_6DOF} 201 */ 202 ASENSOR_TYPE_POSE_6DOF = 28, 203 /** 204 * {@link ASENSOR_TYPE_STATIONARY_DETECT} 205 */ 206 ASENSOR_TYPE_STATIONARY_DETECT = 29, 207 /** 208 * {@link ASENSOR_TYPE_MOTION_DETECT} 209 */ 210 ASENSOR_TYPE_MOTION_DETECT = 30, 211 /** 212 * {@link ASENSOR_TYPE_HEART_BEAT} 213 */ 214 ASENSOR_TYPE_HEART_BEAT = 31, 215 /** 216 * This sensor type is for delivering additional sensor information aside 217 * from sensor event data. 218 * 219 * Additional information may include: 220 * - {@link ASENSOR_ADDITIONAL_INFO_INTERNAL_TEMPERATURE} 221 * - {@link ASENSOR_ADDITIONAL_INFO_SAMPLING} 222 * - {@link ASENSOR_ADDITIONAL_INFO_SENSOR_PLACEMENT} 223 * - {@link ASENSOR_ADDITIONAL_INFO_UNTRACKED_DELAY} 224 * - {@link ASENSOR_ADDITIONAL_INFO_VEC3_CALIBRATION} 225 * 226 * This type will never bind to a sensor. In other words, no sensor in the 227 * sensor list can have the type {@link ASENSOR_TYPE_ADDITIONAL_INFO}. 228 * 229 * If a device supports the sensor additional information feature, it will 230 * report additional information events via {@link ASensorEvent} and will 231 * have the type of {@link ASensorEvent} set to 232 * {@link ASENSOR_TYPE_ADDITIONAL_INFO} and the sensor of {@link ASensorEvent} set 233 * to the handle of the reporting sensor. 234 * 235 * Additional information reports consist of multiple frames ordered by 236 * {@link ASensorEvent#timestamp}. The first frame in the report will have 237 * a {@link AAdditionalInfoEvent#type} of 238 * {@link ASENSOR_ADDITIONAL_INFO_BEGIN}, and the last frame in the report 239 * will have a {@link AAdditionalInfoEvent#type} of 240 * {@link ASENSOR_ADDITIONAL_INFO_END}. 241 * 242 */ 243 ASENSOR_TYPE_ADDITIONAL_INFO = 33, 244 /** 245 * {@link ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT} 246 */ 247 ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT = 34, 248 /** 249 * {@link ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED} 250 */ 251 ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED = 35, 252 /** 253 * {@link ASENSOR_TYPE_HINGE_ANGLE} 254 * reporting-mode: on-change 255 * 256 * The hinge angle sensor value is returned in degrees. 257 */ 258 ASENSOR_TYPE_HINGE_ANGLE = 36, 259 }; 260 261 /** 262 * Sensor accuracy measure. 263 */ 264 enum { 265 /** no contact */ 266 ASENSOR_STATUS_NO_CONTACT = -1, 267 /** unreliable */ 268 ASENSOR_STATUS_UNRELIABLE = 0, 269 /** low accuracy */ 270 ASENSOR_STATUS_ACCURACY_LOW = 1, 271 /** medium accuracy */ 272 ASENSOR_STATUS_ACCURACY_MEDIUM = 2, 273 /** high accuracy */ 274 ASENSOR_STATUS_ACCURACY_HIGH = 3 275 }; 276 277 /** 278 * Sensor Reporting Modes. 279 */ 280 enum { 281 /** invalid reporting mode */ 282 AREPORTING_MODE_INVALID = -1, 283 /** continuous reporting */ 284 AREPORTING_MODE_CONTINUOUS = 0, 285 /** reporting on change */ 286 AREPORTING_MODE_ON_CHANGE = 1, 287 /** on shot reporting */ 288 AREPORTING_MODE_ONE_SHOT = 2, 289 /** special trigger reporting */ 290 AREPORTING_MODE_SPECIAL_TRIGGER = 3 291 }; 292 293 /** 294 * Sensor Direct Report Rates. 295 */ 296 enum { 297 /** stopped */ 298 ASENSOR_DIRECT_RATE_STOP = 0, 299 /** nominal 50Hz */ 300 ASENSOR_DIRECT_RATE_NORMAL = 1, 301 /** nominal 200Hz */ 302 ASENSOR_DIRECT_RATE_FAST = 2, 303 /** nominal 800Hz */ 304 ASENSOR_DIRECT_RATE_VERY_FAST = 3 305 }; 306 307 /** 308 * Sensor Direct Channel Type. 309 */ 310 enum { 311 /** shared memory created by ASharedMemory_create */ 312 ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY = 1, 313 /** AHardwareBuffer */ 314 ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER = 2 315 }; 316 317 /** 318 * Sensor Additional Info Types. 319 * 320 * Used to populate {@link AAdditionalInfoEvent#type}. 321 */ 322 enum { 323 /** Marks the beginning of additional information frames */ 324 ASENSOR_ADDITIONAL_INFO_BEGIN = 0, 325 326 /** Marks the end of additional information frames */ 327 ASENSOR_ADDITIONAL_INFO_END = 1, 328 329 /** 330 * Estimation of the delay that is not tracked by sensor timestamps. This 331 * includes delay introduced by sensor front-end filtering, data transport, 332 * etc. 333 * float[2]: delay in seconds, standard deviation of estimated value 334 */ 335 ASENSOR_ADDITIONAL_INFO_UNTRACKED_DELAY = 0x10000, 336 337 /** float: Celsius temperature */ 338 ASENSOR_ADDITIONAL_INFO_INTERNAL_TEMPERATURE, 339 340 /** 341 * First three rows of a homogeneous matrix, which represents calibration to 342 * a three-element vector raw sensor reading. 343 * float[12]: 3x4 matrix in row major order 344 */ 345 ASENSOR_ADDITIONAL_INFO_VEC3_CALIBRATION, 346 347 /** 348 * Location and orientation of sensor element in the device frame: origin is 349 * the geometric center of the mobile device screen surface; the axis 350 * definition corresponds to Android sensor definitions. 351 * float[12]: 3x4 matrix in row major order 352 */ 353 ASENSOR_ADDITIONAL_INFO_SENSOR_PLACEMENT, 354 355 /** 356 * float[2]: raw sample period in seconds, 357 * standard deviation of sampling period 358 */ 359 ASENSOR_ADDITIONAL_INFO_SAMPLING, 360 }; 361 362 /* 363 * A few useful constants 364 */ 365 366 /** Earth's gravity in m/s^2 */ 367 #define ASENSOR_STANDARD_GRAVITY (9.80665f) 368 /** Maximum magnetic field on Earth's surface in uT */ 369 #define ASENSOR_MAGNETIC_FIELD_EARTH_MAX (60.0f) 370 /** Minimum magnetic field on Earth's surface in uT*/ 371 #define ASENSOR_MAGNETIC_FIELD_EARTH_MIN (30.0f) 372 373 /** 374 * A sensor event. 375 */ 376 377 /* NOTE: changes to these structs have to be backward compatible */ 378 typedef struct ASensorVector { 379 union { 380 float v[3]; 381 struct { 382 float x; 383 float y; 384 float z; 385 }; 386 struct { 387 float azimuth; 388 float pitch; 389 float roll; 390 }; 391 }; 392 int8_t status; 393 uint8_t reserved[3]; 394 } ASensorVector; 395 396 typedef struct AMetaDataEvent { 397 int32_t what; 398 int32_t sensor; 399 } AMetaDataEvent; 400 401 typedef struct AUncalibratedEvent { 402 union { 403 float uncalib[3]; 404 struct { 405 float x_uncalib; 406 float y_uncalib; 407 float z_uncalib; 408 }; 409 }; 410 union { 411 float bias[3]; 412 struct { 413 float x_bias; 414 float y_bias; 415 float z_bias; 416 }; 417 }; 418 } AUncalibratedEvent; 419 420 typedef struct AHeartRateEvent { 421 float bpm; 422 int8_t status; 423 } AHeartRateEvent; 424 425 typedef struct ADynamicSensorEvent { 426 int32_t connected; 427 int32_t handle; 428 } ADynamicSensorEvent; 429 430 typedef struct AAdditionalInfoEvent { 431 /** 432 * Event type, such as ASENSOR_ADDITIONAL_INFO_BEGIN, ASENSOR_ADDITIONAL_INFO_END and others. 433 * Refer to {@link ASENSOR_TYPE_ADDITIONAL_INFO} for the expected reporting behavior. 434 */ 435 int32_t type; 436 int32_t serial; 437 union { 438 int32_t data_int32[14]; 439 float data_float[14]; 440 }; 441 } AAdditionalInfoEvent; 442 443 /** 444 * Information that describes a sensor event, refer to 445 * <a href="/reference/android/hardware/SensorEvent">SensorEvent</a> for additional 446 * documentation. 447 */ 448 /* NOTE: changes to this struct has to be backward compatible */ 449 typedef struct ASensorEvent { 450 int32_t version; /* sizeof(struct ASensorEvent) */ 451 int32_t sensor; /** The sensor that generates this event */ 452 int32_t type; /** Sensor type for the event, such as {@link ASENSOR_TYPE_ACCELEROMETER} */ 453 int32_t reserved0; /** do not use */ 454 /** 455 * The time in nanoseconds at which the event happened, and its behavior 456 * is identical to <a href="/reference/android/hardware/SensorEvent#timestamp"> 457 * SensorEvent::timestamp</a> in Java API. 458 */ 459 int64_t timestamp; 460 union { 461 union { 462 float data[16]; 463 ASensorVector vector; 464 ASensorVector acceleration; 465 ASensorVector gyro; 466 ASensorVector magnetic; 467 float temperature; 468 float distance; 469 float light; 470 float pressure; 471 float relative_humidity; 472 AUncalibratedEvent uncalibrated_acceleration; 473 AUncalibratedEvent uncalibrated_gyro; 474 AUncalibratedEvent uncalibrated_magnetic; 475 AMetaDataEvent meta_data; 476 AHeartRateEvent heart_rate; 477 ADynamicSensorEvent dynamic_sensor_meta; 478 AAdditionalInfoEvent additional_info; 479 }; 480 union { 481 uint64_t data[8]; 482 uint64_t step_counter; 483 } u64; 484 }; 485 486 uint32_t flags; 487 int32_t reserved1[3]; 488 } ASensorEvent; 489 490 struct ASensorManager; 491 /** 492 * {@link ASensorManager} is an opaque type to manage sensors and 493 * events queues. 494 * 495 * {@link ASensorManager} is a singleton that can be obtained using 496 * ASensorManager_getInstance(). 497 * 498 * This file provides a set of functions that uses {@link 499 * ASensorManager} to access and list hardware sensors, and 500 * create and destroy event queues: 501 * - ASensorManager_getSensorList() 502 * - ASensorManager_getDefaultSensor() 503 * - ASensorManager_getDefaultSensorEx() 504 * - ASensorManager_createEventQueue() 505 * - ASensorManager_destroyEventQueue() 506 */ 507 typedef struct ASensorManager ASensorManager; 508 509 510 struct ASensorEventQueue; 511 /** 512 * {@link ASensorEventQueue} is an opaque type that provides access to 513 * {@link ASensorEvent} from hardware sensors. 514 * 515 * A new {@link ASensorEventQueue} can be obtained using ASensorManager_createEventQueue(). 516 * 517 * This file provides a set of functions to enable and disable 518 * sensors, check and get events, and set event rates on a {@link 519 * ASensorEventQueue}. 520 * - ASensorEventQueue_enableSensor() 521 * - ASensorEventQueue_disableSensor() 522 * - ASensorEventQueue_hasEvents() 523 * - ASensorEventQueue_getEvents() 524 * - ASensorEventQueue_setEventRate() 525 * - ASensorEventQueue_requestAdditionalInfoEvents() 526 */ 527 typedef struct ASensorEventQueue ASensorEventQueue; 528 529 struct ASensor; 530 /** 531 * {@link ASensor} is an opaque type that provides information about 532 * an hardware sensors. 533 * 534 * A {@link ASensor} pointer can be obtained using 535 * ASensorManager_getDefaultSensor(), 536 * ASensorManager_getDefaultSensorEx() or from a {@link ASensorList}. 537 * 538 * This file provides a set of functions to access properties of a 539 * {@link ASensor}: 540 * - ASensor_getName() 541 * - ASensor_getVendor() 542 * - ASensor_getType() 543 * - ASensor_getResolution() 544 * - ASensor_getMinDelay() 545 * - ASensor_getFifoMaxEventCount() 546 * - ASensor_getFifoReservedEventCount() 547 * - ASensor_getStringType() 548 * - ASensor_getReportingMode() 549 * - ASensor_isWakeUpSensor() 550 * - ASensor_getHandle() 551 */ 552 typedef struct ASensor ASensor; 553 /** 554 * {@link ASensorRef} is a type for constant pointers to {@link ASensor}. 555 * 556 * This is used to define entry in {@link ASensorList} arrays. 557 */ 558 typedef ASensor const* ASensorRef; 559 /** 560 * {@link ASensorList} is an array of reference to {@link ASensor}. 561 * 562 * A {@link ASensorList} can be initialized using ASensorManager_getSensorList(). 563 */ 564 typedef ASensorRef const* ASensorList; 565 566 /*****************************************************************************/ 567 568 /** 569 * Get a reference to the sensor manager. ASensorManager is a singleton 570 * per package as different packages may have access to different sensors. 571 * 572 * Deprecated: Use ASensorManager_getInstanceForPackage(const char*) instead. 573 * 574 * Example: 575 * 576 * ASensorManager* sensorManager = ASensorManager_getInstance(); 577 * 578 */ 579 ASensorManager* ASensorManager_getInstance() __DEPRECATED_IN(26); 580 581 /** 582 * Get a reference to the sensor manager. ASensorManager is a singleton 583 * per package as different packages may have access to different sensors. 584 * 585 * Example: 586 * 587 * ASensorManager* sensorManager = ASensorManager_getInstanceForPackage("foo.bar.baz"); 588 * 589 * Available since API level 26. 590 */ 591 ASensorManager* ASensorManager_getInstanceForPackage(const char* packageName) __INTRODUCED_IN(26); 592 593 /** 594 * Returns the list of available sensors. 595 */ 596 int ASensorManager_getSensorList(ASensorManager* manager, ASensorList* list); 597 598 /** 599 * Returns the default sensor for the given type, or NULL if no sensor 600 * of that type exists. 601 */ 602 ASensor const* ASensorManager_getDefaultSensor(ASensorManager* manager, int type); 603 604 /** 605 * Returns the default sensor with the given type and wakeUp properties or NULL if no sensor 606 * of this type and wakeUp properties exists. 607 * 608 * Available since API level 21. 609 */ 610 ASensor const* ASensorManager_getDefaultSensorEx(ASensorManager* manager, int type, bool wakeUp) __INTRODUCED_IN(21); 611 612 /** 613 * Creates a new sensor event queue and associate it with a looper. 614 * 615 * "ident" is a identifier for the events that will be returned when 616 * calling ALooper_pollOnce(). The identifier must be >= 0, or 617 * ALOOPER_POLL_CALLBACK if providing a non-NULL callback. 618 */ 619 ASensorEventQueue* ASensorManager_createEventQueue(ASensorManager* manager, 620 ALooper* looper, int ident, ALooper_callbackFunc callback, void* data); 621 622 /** 623 * Destroys the event queue and free all resources associated to it. 624 */ 625 int ASensorManager_destroyEventQueue(ASensorManager* manager, ASensorEventQueue* queue); 626 627 /** 628 * Create direct channel based on shared memory 629 * 630 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} to be used 631 * for configuring sensor direct report. 632 * 633 * Available since API level 26. 634 * 635 * \param manager the {@link ASensorManager} instance obtained from 636 * {@link ASensorManager_getInstanceForPackage}. 637 * \param fd file descriptor representing a shared memory created by 638 * {@link ASharedMemory_create} 639 * \param size size to be used, must be less or equal to size of shared memory. 640 * 641 * \return a positive integer as a channel id to be used in 642 * {@link ASensorManager_destroyDirectChannel} and 643 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures. 644 */ 645 int ASensorManager_createSharedMemoryDirectChannel(ASensorManager* manager, int fd, size_t size) __INTRODUCED_IN(26); 646 647 /** 648 * Create direct channel based on AHardwareBuffer 649 * 650 * Create a direct channel of {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER} type to be used 651 * for configuring sensor direct report. 652 * 653 * Available since API level 26. 654 * 655 * \param manager the {@link ASensorManager} instance obtained from 656 * {@link ASensorManager_getInstanceForPackage}. 657 * \param buffer {@link AHardwareBuffer} instance created by {@link AHardwareBuffer_allocate}. 658 * \param size the intended size to be used, must be less or equal to size of buffer. 659 * 660 * \return a positive integer as a channel id to be used in 661 * {@link ASensorManager_destroyDirectChannel} and 662 * {@link ASensorManager_configureDirectReport}, or value less or equal to 0 for failures. 663 */ 664 int ASensorManager_createHardwareBufferDirectChannel( 665 ASensorManager* manager, AHardwareBuffer const * buffer, size_t size) __INTRODUCED_IN(26); 666 667 /** 668 * Destroy a direct channel 669 * 670 * Destroy a direct channel previously created by using one of 671 * ASensorManager_create*DirectChannel() derivative functions. 672 * Note that the buffer used for creating the direct channel does not get destroyed with 673 * ASensorManager_destroyDirectChannel and has to be closed or released separately. 674 * 675 * Available since API level 26. 676 * 677 * \param manager the {@link ASensorManager} instance obtained from 678 * {@link ASensorManager_getInstanceForPackage}. 679 * \param channelId channel id (a positive integer) returned from 680 * {@link ASensorManager_createSharedMemoryDirectChannel} or 681 * {@link ASensorManager_createHardwareBufferDirectChannel}. 682 */ 683 void ASensorManager_destroyDirectChannel(ASensorManager* manager, int channelId) __INTRODUCED_IN(26); 684 685 /** 686 * Configure direct report on channel 687 * 688 * Configure sensor direct report on a direct channel: set rate to value other than 689 * {@link ASENSOR_DIRECT_RATE_STOP} so that sensor event can be directly 690 * written into the shared memory region used for creating the buffer. It returns a positive token 691 * which can be used for identify sensor events from different sensors on success. Calling with rate 692 * {@link ASENSOR_DIRECT_RATE_STOP} will stop direct report of the sensor specified in the channel. 693 * 694 * To stop all active sensor direct report configured to a channel, set sensor to NULL and rate to 695 * {@link ASENSOR_DIRECT_RATE_STOP}. 696 * 697 * In order to successfully configure a direct report, the sensor has to support the specified rate 698 * and the channel type, which can be checked by {@link ASensor_getHighestDirectReportRateLevel} and 699 * {@link ASensor_isDirectChannelTypeSupported}, respectively. 700 * 701 * Example: 702 * 703 * ASensorManager *manager = ...; 704 * ASensor *sensor = ...; 705 * int channelId = ...; 706 * 707 * ASensorManager_configureDirectReport(manager, sensor, channel_id, ASENSOR_DIRECT_RATE_FAST); 708 * 709 * Available since API level 26. 710 * 711 * \param manager the {@link ASensorManager} instance obtained from 712 * {@link ASensorManager_getInstanceForPackage}. 713 * \param sensor a {@link ASensor} to denote which sensor to be operate. It can be NULL if rate 714 * is {@link ASENSOR_DIRECT_RATE_STOP}, denoting stopping of all active sensor 715 * direct report. 716 * \param channelId channel id (a positive integer) returned from 717 * {@link ASensorManager_createSharedMemoryDirectChannel} or 718 * {@link ASensorManager_createHardwareBufferDirectChannel}. 719 * \param rate one of predefined ASENSOR_DIRECT_RATE_... that is supported by the sensor. 720 * \return positive token for success or negative error code. 721 */ 722 int ASensorManager_configureDirectReport(ASensorManager* manager, 723 ASensor const* sensor, int channelId, int rate) __INTRODUCED_IN(26); 724 725 /*****************************************************************************/ 726 727 /** 728 * Enable the selected sensor with sampling and report parameters 729 * 730 * Enable the selected sensor at a specified sampling period and max batch report latency. 731 * To disable sensor, use {@link ASensorEventQueue_disableSensor}. 732 * 733 * \param queue {@link ASensorEventQueue} for sensor event to be report to. 734 * \param sensor {@link ASensor} to be enabled. 735 * \param samplingPeriodUs sampling period of sensor in microseconds. 736 * \param maxBatchReportLatencyUs maximum time interval between two batches of sensor events are 737 * delievered in microseconds. For sensor streaming, set to 0. 738 * \return 0 on success or a negative error code on failure. 739 */ 740 int ASensorEventQueue_registerSensor(ASensorEventQueue* queue, ASensor const* sensor, 741 int32_t samplingPeriodUs, int64_t maxBatchReportLatencyUs); 742 743 /** 744 * Enable the selected sensor at default sampling rate. 745 * 746 * Start event reports of a sensor to specified sensor event queue at a default rate. 747 * 748 * \param queue {@link ASensorEventQueue} for sensor event to be report to. 749 * \param sensor {@link ASensor} to be enabled. 750 * 751 * \return 0 on success or a negative error code on failure. 752 */ 753 int ASensorEventQueue_enableSensor(ASensorEventQueue* queue, ASensor const* sensor); 754 755 /** 756 * Disable the selected sensor. 757 * 758 * Stop event reports from the sensor to specified sensor event queue. 759 * 760 * \param queue {@link ASensorEventQueue} to be changed 761 * \param sensor {@link ASensor} to be disabled 762 * \return 0 on success or a negative error code on failure. 763 */ 764 int ASensorEventQueue_disableSensor(ASensorEventQueue* queue, ASensor const* sensor); 765 766 /** 767 * Sets the delivery rate of events in microseconds for the given sensor. 768 * 769 * This function has to be called after {@link ASensorEventQueue_enableSensor}. 770 * Note that this is a hint only, generally event will arrive at a higher 771 * rate. It is an error to set a rate inferior to the value returned by 772 * ASensor_getMinDelay(). 773 * 774 * \param queue {@link ASensorEventQueue} to which sensor event is delivered. 775 * \param sensor {@link ASensor} of which sampling rate to be updated. 776 * \param usec sensor sampling period (1/sampling rate) in microseconds 777 * \return 0 on sucess or a negative error code on failure. 778 */ 779 int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec); 780 781 /** 782 * Determine if a sensor event queue has pending event to be processed. 783 * 784 * \param queue {@link ASensorEventQueue} to be queried 785 * \return 1 if the queue has events; 0 if it does not have events; 786 * or a negative value if there is an error. 787 */ 788 int ASensorEventQueue_hasEvents(ASensorEventQueue* queue); 789 790 /** 791 * Retrieve pending events in sensor event queue 792 * 793 * Retrieve next available events from the queue to a specified event array. 794 * 795 * \param queue {@link ASensorEventQueue} to get events from 796 * \param events pointer to an array of {@link ASensorEvent}. 797 * \param count max number of event that can be filled into array event. 798 * \return number of events returned on success; negative error code when 799 * no events are pending or an error has occurred. 800 * 801 * Examples: 802 * 803 * ASensorEvent event; 804 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, &event, 1); 805 * 806 * ASensorEvent eventBuffer[8]; 807 * ssize_t numEvent = ASensorEventQueue_getEvents(queue, eventBuffer, 8); 808 * 809 */ 810 ssize_t ASensorEventQueue_getEvents(ASensorEventQueue* queue, ASensorEvent* events, size_t count); 811 812 /** 813 * Request that {@link ASENSOR_TYPE_ADDITIONAL_INFO} events to be delivered on 814 * the given {@link ASensorEventQueue}. 815 * 816 * Sensor data events are always delivered to the {@link ASensorEventQueue}. 817 * 818 * The {@link ASENSOR_TYPE_ADDITIONAL_INFO} events will be returned through 819 * {@link ASensorEventQueue_getEvents}. The client is responsible for checking 820 * {@link ASensorEvent#type} to determine the event type prior to handling of 821 * the event. 822 * 823 * The client must be tolerant of any value for 824 * {@link AAdditionalInfoEvent#type}, as new values may be defined in the future 825 * and may delivered to the client. 826 * 827 * Available since API level 29. 828 * 829 * \param queue {@link ASensorEventQueue} to configure 830 * \param enable true to request {@link ASENSOR_TYPE_ADDITIONAL_INFO} events, 831 * false to stop receiving events 832 * \return 0 on success or a negative error code on failure 833 */ 834 int ASensorEventQueue_requestAdditionalInfoEvents(ASensorEventQueue* queue, bool enable) __INTRODUCED_IN(29); 835 836 /*****************************************************************************/ 837 838 /** 839 * Returns this sensor's name (non localized) 840 */ 841 const char* ASensor_getName(ASensor const* sensor); 842 843 /** 844 * Returns this sensor's vendor's name (non localized) 845 */ 846 const char* ASensor_getVendor(ASensor const* sensor); 847 848 /** 849 * Return this sensor's type 850 */ 851 int ASensor_getType(ASensor const* sensor); 852 853 /** 854 * Returns this sensors's resolution 855 */ 856 float ASensor_getResolution(ASensor const* sensor); 857 858 /** 859 * Returns the minimum delay allowed between events in microseconds. 860 * A value of zero means that this sensor doesn't report events at a 861 * constant rate, but rather only when a new data is available. 862 */ 863 int ASensor_getMinDelay(ASensor const* sensor); 864 865 /** 866 * Returns the maximum size of batches for this sensor. Batches will often be 867 * smaller, as the hardware fifo might be used for other sensors. 868 * 869 * Available since API level 21. 870 */ 871 int ASensor_getFifoMaxEventCount(ASensor const* sensor) __INTRODUCED_IN(21); 872 873 /** 874 * Returns the hardware batch fifo size reserved to this sensor. 875 * 876 * Available since API level 21. 877 */ 878 int ASensor_getFifoReservedEventCount(ASensor const* sensor) __INTRODUCED_IN(21); 879 880 /** 881 * Returns this sensor's string type. 882 * 883 * Available since API level 21. 884 */ 885 const char* ASensor_getStringType(ASensor const* sensor) __INTRODUCED_IN(21); 886 887 /** 888 * Returns the reporting mode for this sensor. One of AREPORTING_MODE_* constants. 889 * 890 * Available since API level 21. 891 */ 892 int ASensor_getReportingMode(ASensor const* sensor) __INTRODUCED_IN(21); 893 894 /** 895 * Returns true if this is a wake up sensor, false otherwise. 896 * 897 * Available since API level 21. 898 */ 899 bool ASensor_isWakeUpSensor(ASensor const* sensor) __INTRODUCED_IN(21); 900 901 /** 902 * Test if sensor supports a certain type of direct channel. 903 * 904 * Available since API level 26. 905 * 906 * \param sensor a {@link ASensor} to denote the sensor to be checked. 907 * \param channelType Channel type constant, either 908 * {@link ASENSOR_DIRECT_CHANNEL_TYPE_SHARED_MEMORY} 909 * or {@link ASENSOR_DIRECT_CHANNEL_TYPE_HARDWARE_BUFFER}. 910 * \returns true if sensor supports the specified direct channel type. 911 */ 912 bool ASensor_isDirectChannelTypeSupported(ASensor const* sensor, int channelType) __INTRODUCED_IN(26); 913 914 /** 915 * Get the highest direct rate level that a sensor supports. 916 * 917 * Available since API level 26. 918 * 919 * \param sensor a {@link ASensor} to denote the sensor to be checked. 920 * 921 * \return a ASENSOR_DIRECT_RATE_... enum denoting the highest rate level supported by the sensor. 922 * If return value is {@link ASENSOR_DIRECT_RATE_STOP}, it means the sensor 923 * does not support direct report. 924 */ 925 int ASensor_getHighestDirectReportRateLevel(ASensor const* sensor) __INTRODUCED_IN(26); 926 927 /** 928 * Returns the sensor's handle. 929 * 930 * The handle identifies the sensor within the system and is included in the 931 * sensor field of {@link ASensorEvent}, including those sent with type 932 * {@link ASENSOR_TYPE_ADDITIONAL_INFO}. 933 * 934 * A sensor's handle is able to be used to map {@link ASENSOR_TYPE_ADDITIONAL_INFO} events to the 935 * sensor that generated the event. 936 * 937 * It is important to note that the value returned by {@link ASensor_getHandle} is not the same as 938 * the value returned by the Java API <a href="/reference/android/hardware/Sensor#getId()"> 939 * android.hardware.Sensor's getId()</a> and no mapping exists between the values. 940 * 941 * Available since API level 29. 942 */ 943 int ASensor_getHandle(ASensor const* sensor) __INTRODUCED_IN(29); 944 945 #ifdef __cplusplus 946 }; 947 #endif 948 949 #endif // ANDROID_SENSOR_H 950 951 /** @} */ 952