hal: Read hotword data from sound trigger hal

If the input stream is reading the hotword data, get it from
sound trigger hal.

Change-Id: Iebbc0bf60be3bb12d752a1c905f71e9ea14fbf46
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index c0b6de1..2ed761e 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -221,6 +221,7 @@
 #define audio_extn_sound_trigger_set_parameters(adev, parms)           (0)
 #define audio_extn_sound_trigger_check_and_get_session(in)             (0)
 #define audio_extn_sound_trigger_stop_lab(in)                          (0)
+#define audio_extn_sound_trigger_read(in, buffer, bytes)               (0)
 #else
 
 enum st_event_type {
@@ -241,6 +242,8 @@
                                              struct str_parms *parms);
 void audio_extn_sound_trigger_check_and_get_session(struct stream_in *in);
 void audio_extn_sound_trigger_stop_lab(struct stream_in *in);
+int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
+                                  size_t bytes);
 #endif
 
 #ifndef AUXPCM_BT_ENABLED
diff --git a/hal/audio_extn/soundtrigger.c b/hal/audio_extn/soundtrigger.c
index 9051334..3c16c88 100644
--- a/hal/audio_extn/soundtrigger.c
+++ b/hal/audio_extn/soundtrigger.c
@@ -30,6 +30,7 @@
 /* #define LOG_NDEBUG 0 */
 #define LOG_NDDEBUG 0
 
+#include <errno.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <dlfcn.h>
@@ -64,7 +65,7 @@
 {
     struct sound_trigger_info  *st_ses_info = NULL;
     struct listnode *node;
-    ALOGD("%s: list %d capture_handle %d", __func__,
+    ALOGV("%s: list empty %d capture_handle %d", __func__,
            list_empty(&st_dev->st_ses_list), capture_handle);
     list_for_each(node, &st_dev->st_ses_list) {
         st_ses_info = node_to_item(node, struct sound_trigger_info , list);
@@ -128,6 +129,45 @@
     return status;
 }
 
+int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
+                       size_t bytes)
+{
+    int ret = -1;
+    struct sound_trigger_info  *st_info = NULL;
+    audio_event_info_t event;
+
+    if (!st_dev)
+       return ret;
+
+    if (!in->is_st_session_active) {
+        ALOGE(" %s: Sound trigger is not active", __func__);
+        goto exit;
+    }
+    if(in->standby)
+        in->standby = false;
+
+    pthread_mutex_lock(&st_dev->lock);
+    st_info = get_sound_trigger_info(in->capture_handle);
+    pthread_mutex_unlock(&st_dev->lock);
+    if (st_info) {
+        event.u.aud_info.ses_info = &st_info->st_ses;
+        event.u.aud_info.buf = buffer;
+        event.u.aud_info.num_bytes = bytes;
+        ret = st_dev->st_callback(AUDIO_EVENT_READ_SAMPLES, &event);
+    }
+
+exit:
+    if (ret) {
+        if (-ENETRESET == ret)
+            in->is_st_session_active = false;
+        memset(buffer, 0, bytes);
+        ALOGV("%s: read failed status %d - sleep", __func__, ret);
+        usleep((bytes * 1000000) / (audio_stream_in_frame_size((struct audio_stream_in *)in) *
+                                   in->config.rate));
+    }
+    return ret;
+}
+
 void audio_extn_sound_trigger_stop_lab(struct stream_in *in)
 {
     int status = 0;
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 8ce8595..297d671 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -2603,31 +2603,29 @@
 
     pthread_mutex_lock(&in->lock);
 
-    if (in->pcm) {
-        if(SND_CARD_STATE_OFFLINE == snd_scard_state) {
-            ALOGD(" %s: sound card is not active/SSR state", __func__);
-            ret= -EIO;;
-            goto exit;
-        } else {
-            if (in->is_st_session &&  !in->is_st_session_active) {
-                ALOGD(" %s: Sound trigger is not active/SSR", __func__);
-                ret= -EIO;;
-                goto exit;
-            }
-        }
+    if (in->is_st_session) {
+        ALOGVV(" %s: reading on st session bytes=%zu", __func__, bytes);
+        /* Read from sound trigger HAL */
+        audio_extn_sound_trigger_read(in, buffer, bytes);
+        pthread_mutex_unlock(&in->lock);
+        return bytes;
+    }
+
+    if (in->pcm && (SND_CARD_STATE_OFFLINE == snd_scard_state)) {
+        ALOGD(" %s: sound card is not active/SSR state", __func__);
+        ret= -EIO;;
+        goto exit;
     }
 
     if (in->standby) {
-        if (!in->is_st_session) {
-            pthread_mutex_lock(&adev->lock);
-            if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
-                ret = voice_extn_compress_voip_start_input_stream(in);
-            else
-                ret = start_input_stream(in);
-            pthread_mutex_unlock(&adev->lock);
-            if (ret != 0) {
-                goto exit;
-            }
+        pthread_mutex_lock(&adev->lock);
+        if (in->usecase == USECASE_COMPRESS_VOIP_CALL)
+            ret = voice_extn_compress_voip_start_input_stream(in);
+        else
+            ret = start_input_stream(in);
+        pthread_mutex_unlock(&adev->lock);
+        if (ret != 0) {
+            goto exit;
         }
         in->standby = 0;
     }
@@ -2657,17 +2655,9 @@
 exit:
     /* ToDo: There may be a corner case when SSR happens back to back during
        start/stop. Need to post different error to handle that. */
-    if (-ENETRESET == ret) {
-        /* CPE SSR results in kernel returning ENETRESET for sound trigger
-          session reading on LAB data. In this case do not set sound card state
-          offline, instead mark this sound trigger session inactive to avoid
-          further reading of LAB data from CPE driver. Marking the session
-          inactive handles both CPE and ADSP SSR for sound trigger session */
-        if (!in->is_st_session)
-            set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
-        else
-            in->is_st_session_active = false;
-    }
+    if (-ENETRESET == ret)
+        set_snd_card_state(adev,SND_CARD_STATE_OFFLINE);
+
     pthread_mutex_unlock(&in->lock);
 
     if (ret != 0) {
@@ -3386,7 +3376,7 @@
 }
 
 static int adev_open_input_stream(struct audio_hw_device *dev,
-                                  audio_io_handle_t handle __unused,
+                                  audio_io_handle_t handle,
                                   audio_devices_t devices,
                                   struct audio_config *config,
                                   struct audio_stream_in **stream_in,