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

Commit 987fd97c authored by Atneya Nair's avatar Atneya Nair Committed by Android (Google) Code Review
Browse files

Merge "Implement fake STHAL" into udc-dev

parents 506d917c 890b2b36
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -13,11 +13,13 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media.soundtrigger_middleware;

import android.media.permission.Identity;
import android.media.soundtrigger_middleware.ISoundTriggerModule;
import android.media.soundtrigger_middleware.ISoundTriggerCallback;
import android.media.soundtrigger_middleware.ISoundTriggerInjection;
import android.media.soundtrigger_middleware.ISoundTriggerModule;
import android.media.soundtrigger_middleware.SoundTriggerModuleDescriptor;

/**
@@ -86,4 +88,12 @@ interface ISoundTriggerMiddlewareService {
                                          in Identity middlemanIdentity,
                                          in Identity originatorIdentity,
                                          ISoundTriggerCallback callback);

    /**
     * Attach an injection interface interface to the ST mock HAL.
     * See {@link ISoundTriggerInjection} for injection details.
     * If another client attaches, this session will be pre-empted.
     */
    void attachFakeHalInjection(ISoundTriggerInjection injection);

}
+96 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.soundtrigger_middleware;

import android.media.soundtrigger_middleware.IInjectGlobalEvent;
import android.media.soundtrigger_middleware.ISoundTriggerInjection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Slog;

import com.android.internal.annotations.GuardedBy;
import com.android.server.soundtrigger_middleware.FakeSoundTriggerHal.ExecutorHolder;


/**
 * Alternate HAL factory which constructs {@link FakeSoundTriggerHal} with addition hooks to
 * observe framework events.
 */
class FakeHalFactory implements HalFactory {

    private static final String TAG = "FakeHalFactory";
    private final ISoundTriggerInjection mInjection;

    FakeHalFactory(ISoundTriggerInjection injection) {
        mInjection = injection;
    }

    /**
     * We override the methods below at the {@link ISoundTriggerHal} level, because
     * they do not represent real HAL events, rather, they are framework exclusive.
     * So, we intercept them here and report them to the injection interface.
     */
    @Override
    public ISoundTriggerHal create() {
        final FakeSoundTriggerHal hal = new FakeSoundTriggerHal(mInjection);
        final IInjectGlobalEvent session = hal.getGlobalEventInjection();
        // The fake hal is a ST3 HAL implementation.
        final ISoundTriggerHal wrapper = new SoundTriggerHw3Compat(hal,
                /* reboot runnable */ () -> {
                    try {
                        session.triggerRestart();
                    }
                    catch (RemoteException e) {
                        Slog.wtf(TAG, "Unexpected RemoteException from same process");
                    }
                }) {
            @Override
            public void detach() {
                ExecutorHolder.INJECTION_EXECUTOR.execute(() -> {
                    try {
                        mInjection.onFrameworkDetached(session);
                    } catch (RemoteException e) {
                        Slog.wtf(TAG, "Unexpected RemoteException from same process");
                    }
                });
            }

            @Override
            public void clientAttached(IBinder token) {
                ExecutorHolder.INJECTION_EXECUTOR.execute(() -> {
                    try {
                        mInjection.onClientAttached(token, session);
                    } catch (RemoteException e) {
                        Slog.wtf(TAG, "Unexpected RemoteException from same process");
                    }
                });
            }

            @Override
            public void clientDetached(IBinder token) {
                ExecutorHolder.INJECTION_EXECUTOR.execute(() -> {
                    try {
                        mInjection.onClientDetached(token);
                    } catch (RemoteException e) {
                        Slog.wtf(TAG, "Unexpected RemoteException from same process");
                    }
                });
            }
        };
        return wrapper;
    }
}
+712 −0

File added.

Preview size limit exceeded, changes collapsed.

+16 −0
Original line number Diff line number Diff line
@@ -140,6 +140,22 @@ interface ISoundTriggerHal {
     */
    void flushCallbacks();

    /**
     * Used only for testing purposes. Called when a client attaches to the framework.
     * Transmitting this event to the fake STHAL allows observation of this event, which is
     * normally consumed by the framework, and is not communicated to the STHAL.
     * @param token - A unique binder token associated with this session.
     */
    void clientAttached(IBinder token);

    /**
     * Used only for testing purposes. Called when a client detached from the framework.
     * Transmitting this event to the fake STHAL allows observation of this event, which is
     * normally consumed by the framework, and is not communicated to the STHAL.
     * @param token - The same token passed to the corresponding {@link clientAttached(IBinder)}.
     */
    void clientDetached(IBinder token);

    /**
     * Kill and restart the HAL instance. This is typically a last resort for error recovery and may
     * result in other related services being killed.
+10 −0
Original line number Diff line number Diff line
@@ -283,6 +283,16 @@ public class SoundTriggerHalConcurrentCaptureHandler implements ISoundTriggerHal
        mCallbackThread.flush();
    }

    @Override
    public void clientAttached(IBinder binder) {
        mDelegate.clientAttached(binder);
    }

    @Override
    public void clientDetached(IBinder binder) {
        mDelegate.clientDetached(binder);
    }

    /**
     * This is a thread for asynchronous delivery of callback events, having the following features:
     * <ul>
Loading