soundtrigger: Refactor the default implementation to be extensible

Introduce an inner "trampoline" class that implements
ISoundTriggerHw. This allows minor uprev implementation to inherit
from SoundTriggerHalImpl and reuse its functionality.

Split SoundModelClient into an abstract common part and
version-specific part. This allows the client to be redefined for
the types used in callback interface extensions.

Split the impl library into "core" part and the part implementing
HIDL_FETCH_ISoundTriggerHw function to avoid clash with the same
function introduced in minor uprev implementation.

Bug: 68823037
Change-Id: Ibec647f1aa7bc6a2a0bdfd1c9f9a066e4779a1bf
Test: make
diff --git a/soundtrigger/2.0/default/SoundTriggerHalImpl.h b/soundtrigger/2.0/default/SoundTriggerHalImpl.h
index 2dd7166..5a9f0e1 100644
--- a/soundtrigger/2.0/default/SoundTriggerHalImpl.h
+++ b/soundtrigger/2.0/default/SoundTriggerHalImpl.h
@@ -35,50 +35,115 @@
 using ::android::hardware::audio::common::V2_0::Uuid;
 using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
 
-class SoundTriggerHalImpl : public ISoundTriggerHw {
+class SoundTriggerHalImpl : public RefBase {
    public:
     SoundTriggerHalImpl();
+    ISoundTriggerHw* getInterface() { return new TrampolineSoundTriggerHw_2_0(this); }
 
-    // Methods from ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw follow.
-    Return<void> getProperties(getProperties_cb _hidl_cb) override;
-    Return<void> loadSoundModel(const ISoundTriggerHw::SoundModel& soundModel,
-                                const sp<ISoundTriggerHwCallback>& callback,
-                                ISoundTriggerHwCallback::CallbackCookie cookie,
-                                loadSoundModel_cb _hidl_cb) override;
-    Return<void> loadPhraseSoundModel(const ISoundTriggerHw::PhraseSoundModel& soundModel,
-                                      const sp<ISoundTriggerHwCallback>& callback,
-                                      ISoundTriggerHwCallback::CallbackCookie cookie,
-                                      loadPhraseSoundModel_cb _hidl_cb) override;
-
-    Return<int32_t> unloadSoundModel(SoundModelHandle modelHandle) override;
-    Return<int32_t> startRecognition(SoundModelHandle modelHandle,
-                                     const ISoundTriggerHw::RecognitionConfig& config,
-                                     const sp<ISoundTriggerHwCallback>& callback,
-                                     ISoundTriggerHwCallback::CallbackCookie cookie) override;
-    Return<int32_t> stopRecognition(SoundModelHandle modelHandle) override;
-    Return<int32_t> stopAllRecognitions() override;
-
-    // RefBase
-    virtual void onFirstRef();
-
-    static void soundModelCallback(struct sound_trigger_model_event* halEvent, void* cookie);
-    static void recognitionCallback(struct sound_trigger_recognition_event* halEvent, void* cookie);
-
-   private:
+   protected:
     class SoundModelClient : public RefBase {
        public:
-        SoundModelClient(uint32_t id, sp<ISoundTriggerHwCallback> callback,
-                         ISoundTriggerHwCallback::CallbackCookie cookie)
-            : mId(id), mCallback(callback), mCookie(cookie) {}
+        SoundModelClient(uint32_t id, ISoundTriggerHwCallback::CallbackCookie cookie)
+            : mId(id), mCookie(cookie) {}
         virtual ~SoundModelClient() {}
 
-        uint32_t mId;
+        uint32_t getId() const { return mId; }
+        sound_model_handle_t getHalHandle() const { return mHalHandle; }
+        void setHalHandle(sound_model_handle_t handle) { mHalHandle = handle; }
+
+        virtual void recognitionCallback(struct sound_trigger_recognition_event* halEvent) = 0;
+        virtual void soundModelCallback(struct sound_trigger_model_event* halEvent) = 0;
+
+       protected:
+        const uint32_t mId;
         sound_model_handle_t mHalHandle;
-        sp<ISoundTriggerHwCallback> mCallback;
         ISoundTriggerHwCallback::CallbackCookie mCookie;
     };
 
-    uint32_t nextUniqueId();
+    static void convertPhaseRecognitionEventFromHal(
+        ISoundTriggerHwCallback::PhraseRecognitionEvent* event,
+        const struct sound_trigger_phrase_recognition_event* halEvent);
+    static void convertRecognitionEventFromHal(
+        ISoundTriggerHwCallback::RecognitionEvent* event,
+        const struct sound_trigger_recognition_event* halEvent);
+    static void convertSoundModelEventFromHal(ISoundTriggerHwCallback::ModelEvent* event,
+                                              const struct sound_trigger_model_event* halEvent);
+
+    virtual ~SoundTriggerHalImpl();
+
+    Return<void> getProperties(ISoundTriggerHw::getProperties_cb _hidl_cb);
+    Return<void> loadSoundModel(const ISoundTriggerHw::SoundModel& soundModel,
+                                const sp<ISoundTriggerHwCallback>& callback,
+                                ISoundTriggerHwCallback::CallbackCookie cookie,
+                                ISoundTriggerHw::loadSoundModel_cb _hidl_cb);
+    Return<void> loadPhraseSoundModel(const ISoundTriggerHw::PhraseSoundModel& soundModel,
+                                      const sp<ISoundTriggerHwCallback>& callback,
+                                      ISoundTriggerHwCallback::CallbackCookie cookie,
+                                      ISoundTriggerHw::loadPhraseSoundModel_cb _hidl_cb);
+    Return<int32_t> unloadSoundModel(SoundModelHandle modelHandle);
+    Return<int32_t> startRecognition(SoundModelHandle modelHandle,
+                                     const ISoundTriggerHw::RecognitionConfig& config);
+    Return<int32_t> stopRecognition(SoundModelHandle modelHandle);
+    Return<int32_t> stopAllRecognitions();
+
+    uint32_t nextUniqueModelId();
+    int doLoadSoundModel(const ISoundTriggerHw::SoundModel& soundModel,
+                         sp<SoundModelClient> client);
+
+    // RefBase
+    void onFirstRef() override;
+
+   private:
+    struct TrampolineSoundTriggerHw_2_0 : public ISoundTriggerHw {
+        explicit TrampolineSoundTriggerHw_2_0(sp<SoundTriggerHalImpl> impl) : mImpl(impl) {}
+
+        // Methods from ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw follow.
+        Return<void> getProperties(getProperties_cb _hidl_cb) override {
+            return mImpl->getProperties(_hidl_cb);
+        }
+        Return<void> loadSoundModel(const ISoundTriggerHw::SoundModel& soundModel,
+                                    const sp<ISoundTriggerHwCallback>& callback,
+                                    ISoundTriggerHwCallback::CallbackCookie cookie,
+                                    loadSoundModel_cb _hidl_cb) override {
+            return mImpl->loadSoundModel(soundModel, callback, cookie, _hidl_cb);
+        }
+        Return<void> loadPhraseSoundModel(const ISoundTriggerHw::PhraseSoundModel& soundModel,
+                                          const sp<ISoundTriggerHwCallback>& callback,
+                                          ISoundTriggerHwCallback::CallbackCookie cookie,
+                                          loadPhraseSoundModel_cb _hidl_cb) override {
+            return mImpl->loadPhraseSoundModel(soundModel, callback, cookie, _hidl_cb);
+        }
+        Return<int32_t> unloadSoundModel(SoundModelHandle modelHandle) override {
+            return mImpl->unloadSoundModel(modelHandle);
+        }
+        Return<int32_t> startRecognition(
+            SoundModelHandle modelHandle, const ISoundTriggerHw::RecognitionConfig& config,
+            const sp<ISoundTriggerHwCallback>& /*callback*/,
+            ISoundTriggerHwCallback::CallbackCookie /*cookie*/) override {
+            return mImpl->startRecognition(modelHandle, config);
+        }
+        Return<int32_t> stopRecognition(SoundModelHandle modelHandle) override {
+            return mImpl->stopRecognition(modelHandle);
+        }
+        Return<int32_t> stopAllRecognitions() override { return mImpl->stopAllRecognitions(); }
+
+       private:
+        sp<SoundTriggerHalImpl> mImpl;
+    };
+
+    class SoundModelClient_2_0 : public SoundModelClient {
+       public:
+        SoundModelClient_2_0(uint32_t id, ISoundTriggerHwCallback::CallbackCookie cookie,
+                             sp<ISoundTriggerHwCallback> callback)
+            : SoundModelClient(id, cookie), mCallback(callback) {}
+
+        void recognitionCallback(struct sound_trigger_recognition_event* halEvent) override;
+        void soundModelCallback(struct sound_trigger_model_event* halEvent) override;
+
+       private:
+        sp<ISoundTriggerHwCallback> mCallback;
+    };
+
     void convertUuidFromHal(Uuid* uuid, const sound_trigger_uuid_t* halUuid);
     void convertUuidToHal(sound_trigger_uuid_t* halUuid, const Uuid* uuid);
     void convertPropertiesFromHal(ISoundTriggerHw::Properties* properties,
@@ -94,19 +159,12 @@
     struct sound_trigger_recognition_config* convertRecognitionConfigToHal(
         const ISoundTriggerHw::RecognitionConfig* config);
 
-    static void convertSoundModelEventFromHal(ISoundTriggerHwCallback::ModelEvent* event,
-                                              const struct sound_trigger_model_event* halEvent);
-    static ISoundTriggerHwCallback::RecognitionEvent* convertRecognitionEventFromHal(
-        const struct sound_trigger_recognition_event* halEvent);
     static void convertPhraseRecognitionExtraFromHal(
         PhraseRecognitionExtra* extra,
         const struct sound_trigger_phrase_recognition_extra* halExtra);
 
-    int doLoadSoundModel(const ISoundTriggerHw::SoundModel& soundModel,
-                         const sp<ISoundTriggerHwCallback>& callback,
-                         ISoundTriggerHwCallback::CallbackCookie cookie, uint32_t* modelId);
-
-    virtual ~SoundTriggerHalImpl();
+    static void soundModelCallback(struct sound_trigger_model_event* halEvent, void* cookie);
+    static void recognitionCallback(struct sound_trigger_recognition_event* halEvent, void* cookie);
 
     const char* mModuleName;
     struct sound_trigger_hw_device* mHwDevice;
@@ -115,8 +173,6 @@
     Mutex mLock;
 };
 
-extern "C" ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* name);
-
 }  // namespace implementation
 }  // namespace V2_0
 }  // namespace soundtrigger