ST-HAL: hold st session mutex until callback is done

If PAL sends a callback to client, at the same time stop
recognition is started and session lock is acquired by
stop's thread, then callback thread will wait for stop
thread, and after stop, model state at framework will be
INACTIVE, and then callback will land at framework with
wrong model state, causing HAL restart triggered.

Now, we hold st session mutex during callback to avoid
ST framework getting event with model state as INACTIVE.

Change-Id: I99a82f372fd6c8b09d50a4c6d3cf6a0b41212bf6
diff --git a/SoundTriggerSession.cpp b/SoundTriggerSession.cpp
index 2dfa978..62a15f6 100644
--- a/SoundTriggerSession.cpp
+++ b/SoundTriggerSession.cpp
@@ -81,7 +81,7 @@
 SoundTriggerSession::SoundTriggerSession(sound_model_handle_t handle,
                                          audio_hw_call_back_t callback)
 {
-    state_ = IDLE;
+    UpdateState(IDLE);
     sm_handle_ = handle;
     rec_config_payload_ = nullptr;
     rec_config_ = nullptr;
@@ -91,8 +91,9 @@
 
 SoundTriggerSession::~SoundTriggerSession()
 {
-    if (pal_handle_ && state_ != IDLE)
+    if (pal_handle_ && IsState(IDLE))
         pal_stream_close(pal_handle_);
+
     pal_handle_ = nullptr;
 
     if (rec_config_payload_) {
@@ -140,9 +141,9 @@
      */
     do {
         lock_status = session->ses_mutex_.try_lock();
-    } while(!lock_status && (session->state_ == ACTIVE));
+    } while(!lock_status && session->IsState(ACTIVE));
 
-    if (session->state_ != ACTIVE) {
+    if (!session->IsState(ACTIVE)) {
         ALOGW("%s: skip notification as client has stopped", __func__);
         goto exit;
     }
@@ -228,13 +229,10 @@
 
     // callback to SoundTriggerService
     session->GetRecognitionCallback(&callback);
-    session->ses_mutex_.unlock();
-    lock_status = false;
     ATRACE_BEGIN("sthal: client detection callback");
-    if (session->state_ == ACTIVE)
-        callback(st_event, session->GetCookie());
-    else
-        ALOGW("%s: skip detection callback as client has stopped", __func__);
+
+    callback(st_event, session->GetCookie());
+
     ATRACE_END();
 
 exit:
@@ -318,6 +316,7 @@
     int status = 0;
 
     ALOGV("%s: Enter", __func__);
+    UpdateState(STOPPING);
 
     // deregister from audio hal
     RegisterHalEvent(false);
@@ -335,7 +334,7 @@
     }
     rec_config_ = nullptr;
 
-    state_ = STOPPED;
+    UpdateState(STOPPED);
     ALOGV("%s: Exit, status = %d", __func__, status);
 
     return status;
@@ -485,7 +484,7 @@
         goto exit;
     }
 
-    state_ = LOADED;
+    UpdateState(LOADED);
 
 exit:
     if (param_payload)
@@ -501,7 +500,7 @@
 
     ALOGV("%s: Enter", __func__);
     std::lock_guard<std::mutex> lck(ses_mutex_);
-    if (state_ == ACTIVE) {
+    if (IsState(ACTIVE)) {
         status = StopRecognition_l();
         if (status) {
             ALOGE("%s: error, failed to stop recognition, status = %d",
@@ -521,7 +520,7 @@
     }
     rec_config_ = nullptr;
 
-    state_ = IDLE;
+    UpdateState(IDLE);
 
     ALOGV("%s: Exit, status = %d", __func__, status);
 
@@ -609,7 +608,7 @@
               __func__, status);
         goto exit;
     }
-    state_ = ACTIVE;
+    UpdateState(ACTIVE);
 
     // register to audio hal
     RegisterHalEvent(true);
@@ -633,23 +632,8 @@
     ALOGV("%s: Enter", __func__);
     std::lock_guard<std::mutex> lck(ses_mutex_);
 
-    // deregister from audio hal
-    RegisterHalEvent(false);
+    StopRecognition_l();
 
-    // stop pal stream
-    status = pal_stream_stop(pal_handle_);
-    if (status) {
-        ALOGE("%s: error, failed to stop pal stream, status = %d",
-              __func__, status);
-    }
-
-    if (rec_config_payload_) {
-        free(rec_config_payload_);
-        rec_config_payload_ = nullptr;
-    }
-    rec_config_ = nullptr;
-
-    state_ = STOPPED;
     ALOGV("%s: Exit, status = %d", __func__, status);
 
     return status;
@@ -739,4 +723,3 @@
 {
     *callback = rec_callback_;
 }
-
diff --git a/SoundTriggerSession.h b/SoundTriggerSession.h
index 29236b7..79dfc86 100644
--- a/SoundTriggerSession.h
+++ b/SoundTriggerSession.h
@@ -26,6 +26,37 @@
  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
+/* Changes from Qualcomm Innovation Center are provided under the following license:
+ *
+ * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted (subject to the limitations in the
+ * disclaimer below) provided that the following conditions are met:
+ *   * Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above
+ *     copyright notice, this list of conditions and the following
+ *     disclaimer in the documentation and/or other materials provided
+ *     with the distribution.
+ *   * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
+ *     contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission.
+ *
+ * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
+ * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
+ * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
+ * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
+ */
 
 #ifndef SOUND_TRIGGER_SESSION_H
 #define SOUND_TRIGGER_SESSION_H
@@ -71,6 +102,8 @@
  protected:
     int OpenPALStream(pal_stream_type_t stream_type);
     bool IsACDSoundModel(struct sound_trigger_sound_model *sound_model);
+    bool IsState(session_state_t state) { return state_ == state; } //Call this with session mutex
+    void UpdateState(session_state_t state) { state_ = state; } //Call this with session mutex
     void RegisterHalEvent(bool is_register);
     static int pal_callback(pal_stream_handle_t *stream_handle,
         uint32_t event_id, uint32_t *event_data,
@@ -88,4 +121,4 @@
     std::mutex ses_mutex_;
 };
 
-#endif  // SOUND_TRIGGER_SESSION_H
\ No newline at end of file
+#endif  // SOUND_TRIGGER_SESSION_H