Converting sound trigger v2.2 getModelState to be asynchronous

Test: built android with checkbuild flag

Change-Id: Ic12dbfe46aae08666ab02a1a8ee0dbb5c2d9381f
Bug-Id: 70206501
diff --git a/services/soundtrigger/SoundTriggerHalHidl.cpp b/services/soundtrigger/SoundTriggerHalHidl.cpp
index 0f9aa15..1d37a8e 100644
--- a/services/soundtrigger/SoundTriggerHalHidl.cpp
+++ b/services/soundtrigger/SoundTriggerHalHidl.cpp
@@ -356,8 +356,7 @@
     return hidlReturn;
 }
 
-int SoundTriggerHalHidl::getModelState(sound_model_handle_t handle,
-                                       struct sound_trigger_recognition_event** event)
+int SoundTriggerHalHidl::getModelState(sound_model_handle_t handle)
 {
     sp<ISoundTriggerHw> soundtrigger = getService();
     if (soundtrigger == 0) {
@@ -377,24 +376,13 @@
     }
 
     int ret = NO_ERROR;
-    Return<void> hidlReturn;
+    Return<int32_t> hidlReturn(0);
     {
         AutoMutex lock(mHalLock);
-        hidlReturn = soundtrigger_2_2->getModelState(
-            model->mHalHandle,
-            [&](int r, const V2_0_ISoundTriggerHwCallback::RecognitionEvent& halEvent) {
-              ret = r;
-              if (ret != 0) {
-                  ALOGE("getModelState returned error code %d", ret);
-              } else {
-                  *event = convertRecognitionEventFromHal(&halEvent);
-              }
-            });
+        hidlReturn = soundtrigger_2_2->getModelState(model->mHalHandle);
     }
     if (!hidlReturn.isOk()) {
         ALOGE("getModelState error %s", hidlReturn.description().c_str());
-        free(*event);
-        *event = nullptr;
         ret = FAILED_TRANSACTION;
     }
     return ret;
diff --git a/services/soundtrigger/SoundTriggerHalHidl.h b/services/soundtrigger/SoundTriggerHalHidl.h
index 3f4bec3..fb9e39e 100644
--- a/services/soundtrigger/SoundTriggerHalHidl.h
+++ b/services/soundtrigger/SoundTriggerHalHidl.h
@@ -96,12 +96,12 @@
         virtual int stopAllRecognitions();
 
         /* Get the current state of a given model.
-         * Returns 0 or an error code. If successful it also sets indicated the event pointer
-         * and expectes that the caller will free the memory.
+         * Returns 0 or an error code. If successful the state will be returned asynchronously
+         * via a recognition event in the callback method that was registered in the
+         * startRecognition() method.
          * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_2 or above.
          */
-        virtual int getModelState(sound_model_handle_t handle,
-                                  struct sound_trigger_recognition_event** event);
+        virtual int getModelState(sound_model_handle_t handle);
 
         // ISoundTriggerHwCallback
         virtual ::android::hardware::Return<void> recognitionCallback(
diff --git a/services/soundtrigger/SoundTriggerHalInterface.h b/services/soundtrigger/SoundTriggerHalInterface.h
index 076ca23..0183ece 100644
--- a/services/soundtrigger/SoundTriggerHalInterface.h
+++ b/services/soundtrigger/SoundTriggerHalInterface.h
@@ -72,12 +72,12 @@
         virtual int stopAllRecognitions() = 0;
 
         /* Get the current state of a given model.
-         * Returns 0 or an error code. If successful it also sets indicated the event pointer
-         * and expectes that the caller will free the memory.
+         * Returns 0 or an error code. If successful the state will be returned asynchronously
+         * via a recognition event in the callback method that was registered in the
+         * startRecognition() method.
          * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_2 or above.
          */
-        virtual int getModelState(sound_model_handle_t handle,
-                                  struct sound_trigger_recognition_event** event) = 0;
+        virtual int getModelState(sound_model_handle_t handle) = 0;
 
 protected:
         SoundTriggerHalInterface() {}
diff --git a/services/soundtrigger/SoundTriggerHwService.cpp b/services/soundtrigger/SoundTriggerHwService.cpp
index 79e9e88..7915068 100644
--- a/services/soundtrigger/SoundTriggerHwService.cpp
+++ b/services/soundtrigger/SoundTriggerHwService.cpp
@@ -717,8 +717,7 @@
     return NO_ERROR;
 }
 
-status_t SoundTriggerHwService::Module::getModelState(sound_model_handle_t handle,
-                                                      sp<IMemory>& eventMemory)
+status_t SoundTriggerHwService::Module::getModelState(sound_model_handle_t handle)
 {
     ALOGV("getModelState() model handle %d", handle);
     if (mHalInterface == 0) {
@@ -734,21 +733,7 @@
         return INVALID_OPERATION;
     }
 
-    if (model->mType != SOUND_MODEL_TYPE_GENERIC) {
-        return BAD_VALUE;
-    }
-
-    struct sound_trigger_recognition_event* event = nullptr;
-    status_t status = mHalInterface->getModelState(handle, &event);
-    if (status == NO_ERROR) {
-        sp<SoundTriggerHwService> service;
-        service = mService.promote();
-        if (service != 0) {
-            eventMemory = service->prepareRecognitionEvent(event);
-        }
-        free(event);
-    }
-    return status;
+    return mHalInterface->getModelState(handle);
 }
 
 void SoundTriggerHwService::Module::onCallbackEvent(const sp<CallbackEvent>& event)
@@ -784,7 +769,10 @@
             }
 
             recognitionEvent->capture_session = model->mCaptureSession;
-            model->mState = Model::STATE_IDLE;
+            // Don't reset the model state if this recognition event is a get-state response
+            if (recognitionEvent->status != RECOGNITION_STATUS_GET_STATE_RESPONSE) {
+                model->mState = Model::STATE_IDLE;
+            }
             clients.add(model->mModuleClient);
         }
     } break;
@@ -1052,8 +1040,7 @@
     return module->stopRecognition(handle);
 }
 
-status_t SoundTriggerHwService::ModuleClient::getModelState(sound_model_handle_t handle,
-                                                            sp<IMemory>& eventMemory)
+status_t SoundTriggerHwService::ModuleClient::getModelState(sound_model_handle_t handle)
 {
     ALOGV("getModelState() model handle %d", handle);
     if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(),
@@ -1065,7 +1052,7 @@
     if (module == 0) {
         return NO_INIT;
     }
-    return module->getModelState(handle, eventMemory);
+    return module->getModelState(handle);
 }
 
 void SoundTriggerHwService::ModuleClient::setCaptureState_l(bool active)
diff --git a/services/soundtrigger/SoundTriggerHwService.h b/services/soundtrigger/SoundTriggerHwService.h
index c222cd9..4258ec0 100644
--- a/services/soundtrigger/SoundTriggerHwService.h
+++ b/services/soundtrigger/SoundTriggerHwService.h
@@ -122,8 +122,7 @@
        virtual status_t startRecognition(sound_model_handle_t handle,
                                          const sp<IMemory>& dataMemory);
        virtual status_t stopRecognition(sound_model_handle_t handle);
-       virtual status_t getModelState(sound_model_handle_t handle,
-                                      sp<IMemory>& eventMemory);
+       virtual status_t getModelState(sound_model_handle_t handle);
 
        sp<SoundTriggerHalInterface> halInterface() const { return mHalInterface; }
        struct sound_trigger_module_descriptor descriptor() { return mDescriptor; }
@@ -171,8 +170,7 @@
        virtual status_t startRecognition(sound_model_handle_t handle,
                                          const sp<IMemory>& dataMemory);
        virtual status_t stopRecognition(sound_model_handle_t handle);
-       virtual status_t getModelState(sound_model_handle_t handle,
-                                      sp<IMemory>& eventMemory);
+       virtual status_t getModelState(sound_model_handle_t handle);
 
        virtual status_t dump(int fd, const Vector<String16>& args);