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

Unverified Commit 1486eed3 authored by Simon Chan's avatar Simon Chan
Browse files

refactor(adb): simplify reading sync responses

parent c9000c5b
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -63,6 +63,7 @@ export class Adb implements Closeable {
                    }
                }
            }), {
                // Don't cancel the source ReadableStream on AbortSignal abort.
                preventCancel: true,
                signal: abortController.signal,
            })
@@ -146,7 +147,7 @@ export class Adb implements Closeable {
    private _device: string | undefined;
    public get device() { return this._device; }

    private _features: AdbFeatures[] | undefined;
    private _features: AdbFeatures[] = [];
    public get features() { return this._features; }

    public readonly subprocess: AdbSubprocess;
@@ -190,8 +191,6 @@ export class Adb implements Closeable {
    }

    private parseBanner(banner: string): void {
        this._features = [];

        const pieces = banner.split('::');
        if (pieces.length > 1) {
            const props = pieces[1]!;
@@ -224,8 +223,13 @@ export class Adb implements Closeable {
        }
    }

    public addIncomingSocketHandler(handler: AdbIncomingSocketHandler) {
        return this.dispatcher.addIncomingSocketHandler(handler);
    /**
     * Add a handler for incoming socket.
     * @param handler A function to call with new incoming sockets. It must return `true` if it accepts the socket.
     * @returns A function to remove the handler.
     */
    public onIncomingSocket(handler: AdbIncomingSocketHandler) {
        return this.dispatcher.onIncomingSocket(handler);
    }

    public async createSocket(service: string): Promise<AdbSocket> {
+17 −13
Original line number Diff line number Diff line
@@ -7,19 +7,6 @@ const Version =
    new Struct({ littleEndian: true })
        .uint32('version');

/*
 * ADB uses 8 int32 fields to describe bit depths
 * The only combination I have seen is RGBA8888, which is
 *   red_offset:   0
 *   red_length:   8
 *   blue_offset:  16
 *   blue_length:  8
 *   green_offset: 8
 *   green_length: 8
 *   alpha_offset: 24
 *   alpha_length: 8
 */

export const AdbFrameBufferV1 =
    new Struct({ littleEndian: true })
        .uint32('bpp')
@@ -57,6 +44,22 @@ export const AdbFrameBufferV2 =

export type AdbFrameBufferV2 = typeof AdbFrameBufferV2['TDeserializeResult'];

/**
 * ADB uses 8 int32 fields to describe bit depths
 *
 * The only combination I have seen is RGBA8888, which is
 *
 *   red_offset:   0
 *   red_length:   8
 *   blue_offset:  16
 *   blue_length:  8
 *   green_offset: 8
 *   green_length: 8
 *   alpha_offset: 24
 *   alpha_length: 8
 *
 * But it doesn't mean that other combinations are not possible.
 */
export type AdbFrameBuffer = AdbFrameBufferV1 | AdbFrameBufferV2;

export async function framebuffer(adb: Adb): Promise<AdbFrameBuffer> {
@@ -65,6 +68,7 @@ export async function framebuffer(adb: Adb): Promise<AdbFrameBuffer> {
    const { version } = await Version.deserialize(stream);
    switch (version) {
        case 1:
            // TODO: AdbFrameBuffer: does all v1 responses uses the same color space? Add it so the command returns same format for all versions.
            return AdbFrameBufferV1.deserialize(stream);
        case 2:
            return AdbFrameBufferV2.deserialize(stream);
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ export function install(
    return new WrapWritableStream<Uint8Array>({
        async start() {
            // TODO: install: support other install apk methods (streaming, etc.)
            // TODO: install: support split apk formats (`adb install-multiple`)

            // Upload apk file to tmp folder
            sync = await adb.sync();
+5 −5
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@ export class AdbReverseCommand extends AutoDisposable {
        super();

        this.adb = adb;
        this.addDisposable(this.adb.addIncomingSocketHandler(this.handleIncomingSocket));
        this.addDisposable(this.adb.onIncomingSocket(this.handleIncomingSocket));
    }

    protected handleIncomingSocket = async (socket: AdbSocket) => {
@@ -80,8 +80,8 @@ export class AdbReverseCommand extends AutoDisposable {
    /**
     * @param deviceAddress The address adbd on device is listening on. Can be `tcp:0` to let adbd choose an available TCP port by itself.
     * @param localAddress Native ADB client will open a connection to this address when reverse connection received. In WebADB, it's only used to uniquely identify a reverse tunnel registry, `handler` will be called to handle the connection.
     * @param handler A callback to handle incoming connections
     * @returns If `deviceAddress` is `tcp:0`, return `tcp:{ACTUAL_LISTENING_PORT}`; otherwise, return `deviceAddress`.
     * @param handler A callback to handle incoming connections. It must return `true` if it accepts the connection.
     * @returns `tcp:{ACTUAL_LISTENING_PORT}`, If `deviceAddress` is `tcp:0`; otherwise, `deviceAddress`.
     */
    public async add(
        deviceAddress: string,
@@ -91,7 +91,7 @@ export class AdbReverseCommand extends AutoDisposable {
        const stream = await this.sendRequest(`reverse:forward:${deviceAddress};${localAddress}`);

        // `tcp:0` tells the device to pick an available port.
        // Begin with Android 8, device will respond with the selected port for all `tcp:` requests.
        // On Android >=8, device will respond with the selected port for all `tcp:` requests.
        if (deviceAddress.startsWith('tcp:')) {
            let length: number | undefined;
            try {
@@ -101,7 +101,7 @@ export class AdbReverseCommand extends AutoDisposable {
                    throw e;
                }

                // Device before Android 8 doesn't have this response.
                // Android <8 doesn't have this response.
                // (the stream is closed now)
                // Can be safely ignored.
            }
+4 −2
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ export class AdbSubprocessNoneProtocol implements AdbSubprocessProtocol {

    public static async raw(adb: Adb, command: string) {
        // `shell,raw:${command}` also triggers raw mode,
        // But is not supported before Android 7.
        // But is not supported on Android version <7.
        return new AdbSubprocessNoneProtocol(await adb.createSocket(`exec:${command}`));
    }

@@ -50,6 +50,8 @@ export class AdbSubprocessNoneProtocol implements AdbSubprocessProtocol {
    public constructor(socket: AdbSocket) {
        this.socket = socket;

        // Link `stdout`, `stderr` and `stdin` together,
        // so closing any of them will close the others.
        this.duplex = new DuplexStreamFactory<Uint8Array, Uint8Array>({
            close: async () => {
                await this.socket.close();
@@ -62,7 +64,7 @@ export class AdbSubprocessNoneProtocol implements AdbSubprocessProtocol {
    }

    public resize() {
        // Not supported
        // Not supported, but don't throw.
    }

    public kill() {
Loading