CSD: return interface for messages to audio server

Whenever the AudioService registers for receiving sound dose callbacks from the
native side, the native sends back the ISoundDose interface.

Test: logs, dumpsys and UT
Bug: 257238734
Change-Id: I5605196e8be91f00a7fbcf65573dfabe6e5c03d3
diff --git a/services/audioflinger/sounddose/SoundDoseManager.h b/services/audioflinger/sounddose/SoundDoseManager.h
index 9a95e8a..b0aa5d6 100644
--- a/services/audioflinger/sounddose/SoundDoseManager.h
+++ b/services/audioflinger/sounddose/SoundDoseManager.h
@@ -17,10 +17,11 @@
 
 #pragma once
 
+#include <android/media/BnSoundDose.h>
 #include <android/media/ISoundDoseCallback.h>
 #include <audio_utils/MelAggregator.h>
 #include <audio_utils/MelProcessor.h>
-#include <utils/Errors.h>
+#include <binder/Status.h>
 #include <mutex>
 #include <unordered_map>
 
@@ -69,10 +70,15 @@
      */
     void setOutputRs2(float rs2Value);
 
-    std::string dump() const;
+    /**
+     * \brief Registers the interface for passing callbacks to the AudioService and gets
+     * the ISoundDose interface.
+     *
+     * \returns the sound dose binder to send commands to the SoundDoseManager
+     **/
+    sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback);
 
-    /** \brief Registers the interface for passing callbacks to the AudioService. */
-    void registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback);
+    std::string dump() const;
 
     // used for testing
     size_t getCachedMelRecordsSize() const;
@@ -86,9 +92,32 @@
 
     void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const override;
 
-  private:
-    sp<media::ISoundDoseCallback> getSoundDoseCallback() const;
+private:
+    class SoundDose : public media::BnSoundDose,
+                      public IBinder::DeathRecipient {
+    public:
+        SoundDose(SoundDoseManager* manager, const sp<media::ISoundDoseCallback>& callback)
+            : mSoundDoseManager(manager),
+              mSoundDoseCallback(callback) {};
 
+        /** IBinder::DeathRecipient. Listen to the death of ISoundDoseCallback. */
+        virtual void binderDied(const wp<IBinder>& who);
+
+        /** BnSoundDose override */
+        binder::Status setOutputRs2(float value) override;
+        binder::Status resetCsd(float currentCsd,
+                                const std::vector<media::SoundDoseRecord>& records) override;
+
+        wp<SoundDoseManager> mSoundDoseManager;
+        const sp<media::ISoundDoseCallback> mSoundDoseCallback;
+    };
+
+    void resetSoundDose();
+
+    void resetCsd(float currentCsd, const std::vector<media::SoundDoseRecord>& records);
+
+    sp<media::ISoundDoseCallback> getSoundDoseCallback() const;
+    
     mutable std::mutex mLock;
 
     // no need for lock since MelAggregator is thread-safe
@@ -99,7 +128,7 @@
 
     float mRs2Value GUARDED_BY(mLock);
 
-    sp<media::ISoundDoseCallback> mSoundDoseCallback GUARDED_BY(mLock);
+    sp<SoundDose> mSoundDose GUARDED_BY(mLock);
 };
 
 }  // namespace android