Converting sound trigger v2.2 getModelState to be asynchronous

Test: built android with checkbuild flag

Change-Id: I8e927125dcedbc4b98ceb2886806225b2851c082
Bug-Id: 70206501
diff --git a/include/hardware/sound_trigger.h b/include/hardware/sound_trigger.h
index 696b634..99346ef 100644
--- a/include/hardware/sound_trigger.h
+++ b/include/hardware/sound_trigger.h
@@ -117,13 +117,12 @@
     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.
+     * The state will be returned as a recognition event, via the callback that was registered
+     * in the start_recognition method.
      * 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);
+    int (*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 25d4813..38212c4 100644
--- a/modules/soundtrigger/sound_trigger_hw.c
+++ b/modules/soundtrigger/sound_trigger_hw.c
@@ -811,10 +811,9 @@
     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) {
+static int stdev_get_model_state(const struct sound_trigger_hw_device *dev,
+                                 sound_model_handle_t handle) {
+    int ret = 0;
     struct stub_sound_trigger_device *stdev = (struct stub_sound_trigger_device *)dev;
     ALOGI("%s", __func__);
     pthread_mutex_lock(&stdev->lock);
@@ -822,26 +821,29 @@
     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;
+        ret = -ENOSYS;
+        goto exit;
     }
 
-    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);
+    if (!model_context->model_started) {
+        ALOGW("Sound model %d not started", handle);
+        ret = -ENOSYS;
+        goto exit;
     }
 
+    if (model_context->recognition_callback == NULL) {
+        ALOGW("Sound model %d not initialized", handle);
+        ret = -ENOSYS;
+        goto exit;
+    }
+
+    // TODO(mdooley): trigger recognition event
+
+exit:
     pthread_mutex_unlock(&stdev->lock);
     ALOGI("%s done for handle %d", __func__, handle);
 
-    return event;
+    return ret;
 }
 
 __attribute__ ((visibility ("default")))