diff --git a/src/adb/AdbDeviceOutput.test.ts b/src/adb/AdbDeviceOutput.test.ts index be0caf3f9c..d9492df37d 100644 --- a/src/adb/AdbDeviceOutput.test.ts +++ b/src/adb/AdbDeviceOutput.test.ts @@ -524,3 +524,78 @@ it("uses only the final transport field and distinguishes a missing field", () = transport_identity: { status: "not_reported", source_token: null }, }); }); + +// AOSP `transport.cpp append_transport()` substitutes this placeholder when the transport has no +// serial to report, which `client/usb_linux.cpp` keeps for a device that cannot report one during +// early bringup (http://b/20883914). It contains spaces, and the long listing pads it with `%-22s`, +// so the serial column is not one `\S+` token — and it names no device, so it is not an identity. +it("parses the placeholder adb prints for a transport with no serial", () => { + const output = [ + "List of devices attached", + "(no serial number) device usb:1-3 product:beryllium model:POCO_F1 device:beryllium transport_id:2", + "(no serial number) offline usb:1-4 transport_id:3", + "", + ].join("\n"); + const { devices, unparsedLines } = parseAdbDevicesOutput(output); + expect(unparsedLines).toEqual({ sample: [], total: 0 }); + expect( + devices.map(({ serial, state, transport, kind }) => ({ + serial, + state, + transport, + kind, + })), + ).toEqual([ + { + serial: null, + state: "device", + transport: "usb", + kind: "physical", + }, + { + serial: null, + state: "offline", + transport: "usb", + kind: "physical", + }, + ]); + // The transport ID is what still identifies each of them. + expect(devices[0]?.transport_identity).toMatchObject({ + status: "reported", + decimal: "2", + }); + expect(devices[1]?.transport_identity).toMatchObject({ + status: "reported", + decimal: "3", + }); +}); + +it("keeps the other attached devices when one transport has no serial", () => { + // `AdbDeviceOperations` rejects any unparsed line outright, so a placeholder the pattern missed + // removed every healthy device from the result as well. + const output = [ + "List of devices attached", + "emulator-5554\tdevice", + "07ef9c2a\tdevice", + "(no serial number) offline usb:1-4 transport_id:3", + "", + ].join("\n"); + const { devices, unparsedLines } = parseAdbDevicesOutput(output); + expect(unparsedLines.total).toBe(0); + expect(devices.map(({ serial }) => serial)).toEqual([ + "emulator-5554", + "07ef9c2a", + null, + ]); +}); + +it("parses the placeholder in the short listing too", () => { + const { devices, unparsedLines } = parseAdbDevicesOutput( + ["List of devices attached", "(no serial number)\tdevice", ""].join("\n"), + ); + expect(unparsedLines.total).toBe(0); + expect(devices[0]).toMatchObject({ + serial: null, + state: "device", + }); +}); diff --git a/src/adb/AdbDeviceOutput.ts b/src/adb/AdbDeviceOutput.ts index ad0f075421..76762621d9 100644 --- a/src/adb/AdbDeviceOutput.ts +++ b/src/adb/AdbDeviceOutput.ts @@ -21,7 +21,16 @@ import { import { parseAdbTransportIdentity } from "../domain/adb/adbTransportIdentity.js"; -const DEVICE_LINE = /^(\S+)\s+(.*)$/u; +/** + * The serial column of a `devices` line, which is not always a single token. + * + * AOSP `transport.cpp append_transport()` substitutes the literal `(no serial number)` when the + * transport has none to report — `client/usb_linux.cpp` keeps an empty serial for a device that + * cannot report one during early bringup (http://b/20883914) and still registers the transport. + * The long listing pads that column with `%-22s`, the short one separates it with a tab, and in + * both forms the placeholder carries spaces, so `\S+` alone never matched it. + */ +const DEVICE_LINE = /^(\(no serial number\)|\S+)\s+(.*)$/u; /** * adb prints a wireless-debugging transport's serial as the mDNS instance @@ -117,7 +126,10 @@ export const parseAdbDevicesOutput = ( if (rawLine.trim() !== "") unparsed.add(rawLine); continue; } - const serial = match[1]!; + // The placeholder names no device; two transports without a serial would otherwise be + // indistinguishable, and the text cannot be passed to any device operation. + const placeholder = match[1]! === "(no serial number)"; + const serial = placeholder ? null : match[1]!; const remainder = match[2]!.slice(state.length); const usb = metadataValue(remainder, "usb"); // AOSP appends the authoritative field last; earlier metadata may contain @@ -128,7 +140,7 @@ export const parseAdbDevicesOutput = ( transportToken, rawLine, ); - const isEmulator = /^emulator-\d+$/u.test(serial); + const isEmulator = serial !== null && /^emulator-\d+$/u.test(serial); const kind = isEmulator ? ("emulator" as const) : usb !== null @@ -140,7 +152,9 @@ export const parseAdbDevicesOutput = ( transport: usb !== null ? "usb" - : isEmulator || serial.includes(":") || isAdbMdnsTcpSerial(serial) + : isEmulator || + (serial !== null && + (serial.includes(":") || isAdbMdnsTcpSerial(serial))) ? "tcp" : "unknown", product: metadataValue(remainder, "product"), diff --git a/src/domain/adb/adbDeviceAnalysis.ts b/src/domain/adb/adbDeviceAnalysis.ts index 30303eb826..36c159eca8 100644 --- a/src/domain/adb/adbDeviceAnalysis.ts +++ b/src/domain/adb/adbDeviceAnalysis.ts @@ -582,7 +582,15 @@ const client = z.strictObject({ }); export const adbDeviceObservationSchema = z.strictObject({ - serial: z.string().min(1), + /** + * The serial `adb devices` reports, or null when the transport has none. AOSP + * `transport.cpp append_transport()` prints the literal `(no serial number)` in + * that column for a transport registered without one (`client/usb_linux.cpp` + * keeps an empty serial during early bringup, http://b/20883914). That text is a + * display placeholder, not an identity: it names no device, so it is not repeated + * here as one; `transport_identity` still distinguishes such transports. + */ + serial: z.string().min(1).nullable(), state: adbDeviceStateSchema, transport: z.enum(["usb", "tcp", "unknown"]), product: z.string().nullable(),