From 08a4dd9e890d0560c049a044c72e8202aa6f413a Mon Sep 17 00:00:00 2001 From: Peng Xu Date: Thu, 3 Nov 2016 11:56:27 -0700 Subject: [PATCH] [sensors] Adding HAL API support for direct report mode Added HAL function and constant definition for sensor direct report mode. This new mode allows sensor hardware to communicate with application directly via a lock-free queue based on shared memory to achieve low latency and high report frequency. Test: compiles and runs Bug: 30985702 Change-Id: I79c73bcdcc45d43b5a236d65874e5071a589cecd --- include/hardware/sensors-base.h | 21 ++++++++ include/hardware/sensors.h | 85 +++++++++++++++++++++++++++++++- tests/hardware/struct-offset.cpp | 4 +- 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/include/hardware/sensors-base.h b/include/hardware/sensors-base.h index 465b85d5..65d21a85 100644 --- a/include/hardware/sensors-base.h +++ b/include/hardware/sensors-base.h @@ -61,6 +61,11 @@ enum { SENSOR_FLAG_SUPPORTS_DATA_INJECTION = 16ull, // 0x10 SENSOR_FLAG_DYNAMIC_SENSOR = 32ull, // 0x20 SENSOR_FLAG_ADDITIONAL_INFO = 64ull, // 0x40 + SENSOR_FLAG_DIRECT_CHANNEL_ASHMEM = 1024ull, // 0x400 + SENSOR_FLAG_DIRECT_CHANNEL_GRALLOC = 2048ull, // 0x800 + SENSOR_FLAG_MASK_REPORTING_MODE = 14ull, // 0xE + SENSOR_FLAG_MASK_DIRECT_REPORT = 896ull, // 0x380 + SENSOR_FLAG_MASK_DIRECT_CHANNEL = 3072ull, // 0xC00 }; enum { @@ -93,6 +98,22 @@ typedef enum { AINFO_DEBUGGING_START = 1073741824u, // 0x40000000 } additional_info_type_t; +typedef enum { + SENSOR_DIRECT_RATE_STOP = 0, + SENSOR_DIRECT_RATE_NORMAL = 1, + SENSOR_DIRECT_RATE_FAST = 2, + SENSOR_DIRECT_RATE_VERY_FAST = 3, +} direct_rate_level_t; + +typedef enum { + SENSOR_DIRECT_MEM_TYPE_ASHMEM = 1, + SENSOR_DIRECT_MEM_TYPE_GRALLOC = 2, +} direct_mem_type_t; + +typedef enum { + SENSOR_DIRECT_FMT_SENSORS_EVENT = 1, +} direct_format_t; + #ifdef __cplusplus } #endif diff --git a/include/hardware/sensors.h b/include/hardware/sensors.h index 08a65f8b..ba6248ae 100644 --- a/include/hardware/sensors.h +++ b/include/hardware/sensors.h @@ -129,6 +129,18 @@ enum { #define ADDITIONAL_INFO_SHIFT (6) #define ADDITIONAL_INFO_MASK SENSOR_FLAG_MASK_1(ADDITIONAL_INFO_SHIFT) //0x40 +/* + * Shift for sensor direct report support bits (3 bits denoting maximum rate level) + * @see enums SENSOR_DIRECT_RATE_* for definition of direct report rate level. + * @see SENSOR_FLAG_MASK_DIRECT_REPORT for mask + */ +#define SENSOR_FLAG_SHIFT_DIRECT_REPORT (7) +/* + * Shift for sensor direct channel support bit (2 bits representing direct channel supported) + * @see SENSOR_FLAG_DIRECT_CHANNEL_* for details. + */ +#define SENSOR_FLAG_SHIFT_DIRECT_CHANNEL (10) + #define SENSOR_STRING_TYPE_ACCELEROMETER "android.sensor.accelerometer" #define SENSOR_TYPE_MAGNETIC_FIELD SENSOR_TYPE_GEOMAGNETIC_FIELD #define SENSOR_STRING_TYPE_MAGNETIC_FIELD "android.sensor.magnetic_field" @@ -500,6 +512,23 @@ struct sensor_t { void* reserved[2]; }; +/** + * Shared memory information for a direct channel + */ +struct sensors_direct_mem_t { + int type; // enum SENSOR_DIRECT_MEM_... + int format; // enum SENSOR_DIRECT_FMT_... + size_t size; // size of the memory region, in bytes + const struct native_handle *handle; // shared memory handle, which is interpreted differently + // depending on type +}; + +/** + * Direct channel report configuration + */ +struct sensors_direct_cfg_t { + int rate_level; // enum SENSOR_DIRECT_RATE_... +}; /* * sensors_poll_device_t is used with SENSORS_DEVICE_API_VERSION_0_1 @@ -607,7 +636,61 @@ typedef struct sensors_poll_device_1 { */ int (*inject_sensor_data)(struct sensors_poll_device_1 *dev, const sensors_event_t *data); - void (*reserved_procs[7])(void); + /* + * Register/unregister direct report channel. + * + * A HAL declares support for direct report by setting non-NULL values for both + * register_direct_channel and config_direct_report. + * + * This function has two operation modes: + * + * Register: mem != NULL, register a channel using supplied shared memory information. By the + * time this function returns, sensors must finish initializing shared memory content + * (format dependent, see SENSOR_DIRECT_FMT_*). + * Parameters: + * mem points to a valid struct sensors_direct_mem_t. + * channel_handle is ignored. + * Return value: + * A handle of channel (>0) when success, which later can be referred in + * unregister or config_direct_report call, or error code (<0) if failed + * Unregister: mem == NULL, unregister a previously registered channel. + * Parameters: + * mem set to NULL + * channel_handle contains handle of channel to be unregistered + * Return value: + * 0, even if the channel_handle is invalid, in which case it will be a no-op. + */ + int (*register_direct_channel)(struct sensors_poll_device_1 *dev, + const struct sensors_direct_mem_t* mem, int channel_handle); + + /* + * Configure direct sensor event report in direct channel. + * + * Start, modify rate or stop direct report of a sensor in a certain direct channel. A special + * case is setting sensor handle -1 to stop means to stop all active sensor report on the + * channel specified. + * + * A HAL declares support for direct report by setting non-NULL values for both + * register_direct_channel and config_direct_report. + * + * Parameters: + * sensor_handle sensor to be configured. The sensor has to support direct report + * mode by setting flags of sensor_t. Also, direct report mode is only + * defined for continuous reporting mode sensors. + * channel_handle channel handle to be configured. + * config direct report parameters, see sensor_direct_cfg_t. + * Return value: + * - when sensor is started or sensor rate level is changed: return positive identifier of + * sensor in specified channel if successful, otherwise return negative error code. + * - when sensor is stopped: return 0 for success or negative error code for failure. + */ + int (*config_direct_report)(struct sensors_poll_device_1 *dev, + int sensor_handle, int channel_handle, const struct sensors_direct_cfg_t *config); + + /* + * Reserved for future use. + */ + void (*reserved_procs[5])(void); } sensors_poll_device_1_t; diff --git a/tests/hardware/struct-offset.cpp b/tests/hardware/struct-offset.cpp index 10c08957..7f7f2e0d 100644 --- a/tests/hardware/struct-offset.cpp +++ b/tests/hardware/struct-offset.cpp @@ -116,7 +116,9 @@ void CheckOffsets(void) { CHECK_MEMBER_AT(sensors_poll_device_1_t, batch, 76, 144); CHECK_MEMBER_AT(sensors_poll_device_1_t, flush, 80, 152); CHECK_MEMBER_AT(sensors_poll_device_1_t, inject_sensor_data, 84, 160); - CHECK_MEMBER_AT(sensors_poll_device_1_t, reserved_procs, 88, 168); + CHECK_MEMBER_AT(sensors_poll_device_1_t, register_direct_channel, 88, 168); + CHECK_MEMBER_AT(sensors_poll_device_1_t, config_direct_report, 92, 176); + CHECK_MEMBER_AT(sensors_poll_device_1_t, reserved_procs, 96, 184); //Types defined in fb.h CHECK_MEMBER_AT(framebuffer_device_t, common, 0, 0);