Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d3b4e408 authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Support mock STHAL

Have SoundTriggerMiddlewareService try to connect to a "mock"
instance of the HAL, instead of "default", whenever the system
property debug.soundtrigger_middleware.use_mock_hal is set.

This allows, e.g., a test to register a mock HAL and have the real
service connect to it. Simulation of a HAL reboot can be achieved via
the debug() API.

This change also updates the properties of the HAL every time it
reattaches, in order to reflect the mock HAL properties. In a
production this is meaningless.

Test: Manual verification of no regression and of support of the mock
      HAL when present.
Change-Id: I3a3105ae1342f460b942c932bd0c56a4b6e7812d
parent 61b944be
Loading
Loading
Loading
Loading
+33 −7
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.media.soundtrigger_middleware.PhraseSoundModel;
import android.media.soundtrigger_middleware.RecognitionConfig;
import android.media.soundtrigger_middleware.SoundModel;
import android.media.soundtrigger_middleware.SoundTriggerModuleDescriptor;
import android.os.HwBinder;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.util.Log;
@@ -41,6 +42,8 @@ import com.android.server.SystemService;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;

/**
@@ -230,13 +233,36 @@ public class SoundTriggerMiddlewareService extends ISoundTriggerMiddlewareServic
        public void onStart() {
            HalFactory[] factories = new HalFactory[]{() -> {
                try {
                    if (SystemProperties.getBoolean("debug.soundtrigger_middleware.use_mock_hal",
                            false)) {
                        Log.d(TAG, "Connecting to mock ISoundTriggerHw");
                        HwBinder.setTrebleTestingOverride(true);
                        try {
                            ISoundTriggerHw driver = ISoundTriggerHw.getService("mock", true);
                            return SoundTriggerHw2Compat.create(driver,
                                    () -> {
                                        try {
                                            driver.debug(null,
                                                    new ArrayList<>(Arrays.asList(
                                                            new String[]{"reboot"}
                                                    )));
                                        } catch (Exception e) {
                                            Log.e(TAG, "Failed to reboot mock HAL", e);
                                        }
                                    }, mCaptureStateNotifier);
                        } finally {
                            HwBinder.setTrebleTestingOverride(false);
                        }
                    } else {
                        Log.d(TAG, "Connecting to default ISoundTriggerHw");
                    return SoundTriggerHw2Compat.create(ISoundTriggerHw.getService(true),
                        ISoundTriggerHw driver = ISoundTriggerHw.getService(true);
                        return SoundTriggerHw2Compat.create(driver,
                                () -> {
                                    // This property needs to be defined in an init.rc script and
                                    // trigger a HAL reboot.
                                    SystemProperties.set("sys.audio.restart.hal", "1");
                                }, mCaptureStateNotifier);
                    }
                } catch (RemoteException e) {
                    throw e.rethrowAsRuntimeException();
                }
+3 −2
Original line number Diff line number Diff line
@@ -112,8 +112,8 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
    }

    private class ModuleState {
        final @NonNull SoundTriggerModuleProperties properties;
        Set<Session> sessions = new HashSet<>();
        public @NonNull SoundTriggerModuleProperties properties;
        public Set<Session> sessions = new HashSet<>();

        private ModuleState(@NonNull SoundTriggerModuleProperties properties) {
            this.properties = properties;
@@ -178,6 +178,7 @@ public class SoundTriggerMiddlewareValidation implements ISoundTriggerMiddleware
                            throw new RuntimeException(
                                    "listModules must always return the same result.");
                        }
                        mModules.get(desc.handle).properties = desc.properties;
                    }
                }
                return result;
+2 −2
Original line number Diff line number Diff line
@@ -91,7 +91,7 @@ class SoundTriggerModule implements IHwBinder.DeathRecipient, ISoundTriggerHw2.G
    @NonNull private ISoundTriggerHw2 mHalService;
    @NonNull private final SoundTriggerMiddlewareImpl.AudioSessionProvider mAudioSessionProvider;
    private final Set<Session> mActiveSessions = new HashSet<>();
    private final SoundTriggerModuleProperties mProperties;
    private SoundTriggerModuleProperties mProperties;

    /**
     * Ctor.
@@ -105,7 +105,6 @@ class SoundTriggerModule implements IHwBinder.DeathRecipient, ISoundTriggerHw2.G
        mAudioSessionProvider = audioSessionProvider;

        attachToHal();
        mProperties = ConversionUtil.hidl2aidlProperties(mHalService.getProperties());
    }

    /**
@@ -175,6 +174,7 @@ class SoundTriggerModule implements IHwBinder.DeathRecipient, ISoundTriggerHw2.G
                new SoundTriggerHw2Watchdog(mHalFactory.create()));
        mHalService.linkToDeath(this, 0);
        mHalService.registerCallback(this);
        mProperties = ConversionUtil.hidl2aidlProperties(mHalService.getProperties());
    }

    /**