From ce0c253ae7be831ff2160094e7316c3e948e74e4 Mon Sep 17 00:00:00 2001 From: mike dooley Date: Wed, 22 Aug 2018 15:00:49 +0200 Subject: [PATCH] Adding getModelState API to sound trigger Test: built android Bug-Id: 70206501 Change-Id: I2c84fa941eed3f887974f2377655eb0a700542b0 --- include/hardware/sound_trigger.h | 12 ++++++++- modules/soundtrigger/sound_trigger_hw.c | 35 ++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/include/hardware/sound_trigger.h b/include/hardware/sound_trigger.h index d7828acd..696b6344 100644 --- a/include/hardware/sound_trigger.h +++ b/include/hardware/sound_trigger.h @@ -40,7 +40,8 @@ __BEGIN_DECLS #define SOUND_TRIGGER_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0) #define SOUND_TRIGGER_DEVICE_API_VERSION_1_1 HARDWARE_DEVICE_API_VERSION(1, 1) -#define SOUND_TRIGGER_DEVICE_API_VERSION_CURRENT SOUND_TRIGGER_DEVICE_API_VERSION_1_1 +#define SOUND_TRIGGER_DEVICE_API_VERSION_1_2 HARDWARE_DEVICE_API_VERSION(1, 2) +#define SOUND_TRIGGER_DEVICE_API_VERSION_CURRENT SOUND_TRIGGER_DEVICE_API_VERSION_1_2 /** * List of known sound trigger HAL modules. This is the base name of the sound_trigger HAL @@ -114,6 +115,15 @@ struct sound_trigger_hw_device { * If no implementation is provided, stop_recognition will be called for each running model. */ int (*stop_all_recognitions)(const struct sound_trigger_hw_device* dev); + + /* Get the current state of a given model. + * The state is returned as a recognition event, or null if not implemented or on error. + * Caller takes ownership of the returned event memory. + * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_2 or above. + */ + struct sound_trigger_recognition_event* (*get_model_state)( + const struct sound_trigger_hw_device *dev, + sound_model_handle_t sound_model_handle); }; typedef struct sound_trigger_hw_device sound_trigger_hw_device_t; diff --git a/modules/soundtrigger/sound_trigger_hw.c b/modules/soundtrigger/sound_trigger_hw.c index 0089f980..25d48137 100644 --- a/modules/soundtrigger/sound_trigger_hw.c +++ b/modules/soundtrigger/sound_trigger_hw.c @@ -811,6 +811,39 @@ static int stdev_stop_all_recognitions(const struct sound_trigger_hw_device *dev return 0; } +// Caller is responsible for freeing the memory +static struct sound_trigger_recognition_event * stdev_get_model_state( + const struct sound_trigger_hw_device *dev, + sound_model_handle_t handle) { + struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev; + ALOGI("%s", __func__); + pthread_mutex_lock(&stdev->lock); + + struct recognition_context *model_context = get_model_context(stdev, handle); + if (!model_context) { + ALOGW("Can't find sound model handle %d in registered list", handle); + pthread_mutex_unlock(&stdev->lock); + return NULL; + } + + struct sound_trigger_recognition_event *event = NULL; + if (model_context->model_type == SOUND_MODEL_TYPE_GENERIC) { + // TODO(mdooley): define a new status for this use case? + int status = RECOGNITION_STATUS_SUCCESS; + event = (struct sound_trigger_recognition_event *) + sound_trigger_generic_event_alloc(model_context->model_handle, + model_context->config, status); + } else { + ALOGI("Unsupported sound model type: %d, state not available.", + model_context->model_type); + } + + pthread_mutex_unlock(&stdev->lock); + ALOGI("%s done for handle %d", __func__, handle); + + return event; +} + __attribute__ ((visibility ("default"))) int sound_trigger_open_for_streaming() { int ret = 0; @@ -863,6 +896,7 @@ static int stdev_open(const hw_module_t* module, const char* name, stdev->device.start_recognition = stdev_start_recognition; stdev->device.stop_recognition = stdev_stop_recognition; stdev->device.stop_all_recognitions = stdev_stop_all_recognitions; + stdev->device.get_model_state = stdev_get_model_state; pthread_mutex_init(&stdev->lock, (const pthread_mutexattr_t *) NULL); @@ -890,4 +924,3 @@ struct sound_trigger_module HAL_MODULE_INFO_SYM = { .methods = &hal_module_methods, }, }; -