Adds stop_all_recognitions as an optional method to the sound trigger hal.

Change-Id: I6c905b79673fe21782f5dc5f3abc4c2ac75de805
diff --git a/include/hardware/sound_trigger.h b/include/hardware/sound_trigger.h
index 2a8db87..bfded85 100644
--- a/include/hardware/sound_trigger.h
+++ b/include/hardware/sound_trigger.h
@@ -106,7 +106,12 @@
      * The implementation does not have to call the callback when stopped via this method.
      */
     int (*stop_recognition)(const struct sound_trigger_hw_device *dev,
-                           sound_model_handle_t sound_model_handle);
+                            sound_model_handle_t sound_model_handle);
+
+    /* Stop recognition on all models.
+     * 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);
 };
 
 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 dadad82..956e77a 100644
--- a/modules/soundtrigger/sound_trigger_hw.c
+++ b/modules/soundtrigger/sound_trigger_hw.c
@@ -787,6 +787,27 @@
     return 0;
 }
 
+static int stdev_stop_all_recognitions(const struct sound_trigger_hw_device *dev) {
+    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 = stdev->root_model_context;
+    while (model_context) {
+        free(model_context->config);
+        model_context->config = NULL;
+        model_context->recognition_callback = NULL;
+        model_context->recognition_cookie = NULL;
+        ALOGI("%s stopped handle %d", __func__, model_context->model_handle);
+
+        model_context = model_context->next;
+    }
+
+    pthread_mutex_unlock(&stdev->lock);
+
+    return 0;
+}
+
 __attribute__ ((visibility ("default")))
 int sound_trigger_open_for_streaming() {
     int ret = 0;
@@ -838,6 +859,7 @@
     stdev->device.unload_sound_model = stdev_unload_sound_model;
     stdev->device.start_recognition = stdev_start_recognition;
     stdev->device.stop_recognition = stdev_stop_recognition;
+    stdev->device.stop_all_recognitions = stdev_stop_all_recognitions;
 
     pthread_mutex_init(&stdev->lock, (const pthread_mutexattr_t *) NULL);