Skip to content

Commit

Permalink
feat(adb): add getListenAddresses to AdbTcpIpCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
yume-chan committed Aug 25, 2023
1 parent 6a4e4e7 commit aeff1f8
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cSpell.words": [
"ADB's",
"adbd",
"addrs",
"allowlist",
"arraybuffer",
"autorun",
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ In this mode, this library talks to a Google ADB server, which is either running

## Compatibility

| Connection | Chromium-based Browsers | Firefox | Node.js |
| ----------------------------------------- | ------------------------------ | --------- | ----------------------------- |
| USB cable | Supported using [WebUSB] API | No | Supported using `usb` package |
| Wireless through [WebSocket] <sup>1</sup> | Supported | Supported | Possible using `ws` package |
| Connection | Chromium-based Browsers | Firefox | Node.js |
| ----------------------------------------- | -------------------------------- | --------- | ----------------------------- |
| USB cable | Supported using [WebUSB] API | No | Supported using `usb` package |
| Wireless through [WebSocket] <sup>1</sup> | Supported | Supported | Possible using `ws` package |
| Wireless through TCP | Waiting for [Direct Sockets] API | No | Possible using `net` module |

[webusb]: https://wicg.github.io/webusb/
Expand Down
29 changes: 9 additions & 20 deletions apps/demo/src/pages/tcpip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,19 @@ class TcpIpState {
return;
}

const serviceListenAddresses = await GLOBAL_STATE.adb.getProp(
"service.adb.listen_addrs"
);
const servicePort = await GLOBAL_STATE.adb.getProp(
"service.adb.tcp.port"
);
const persistPort = await GLOBAL_STATE.adb.getProp(
"persist.adb.tcp.port"
);
const { serviceListenAddresses, servicePort, persistPort } =
await GLOBAL_STATE.adb.tcpip.getListenAddresses();

if (signal.aborted) {
return;
}

runInAction(() => {
this.serviceListenAddresses =
serviceListenAddresses !== ""
? serviceListenAddresses.split(",")
: undefined;
this.serviceListenAddresses = serviceListenAddresses;

if (servicePort) {
this.servicePortEnabled =
!serviceListenAddresses && servicePort !== "0";
this.servicePort = servicePort;
this.servicePortEnabled = !serviceListenAddresses;
this.servicePort = servicePort.toString();
} else {
this.servicePortEnabled = false;
this.servicePort = "5555";
Expand All @@ -109,7 +98,7 @@ class TcpIpState {
if (persistPort) {
this.persistPortEnabled =
!serviceListenAddresses && !servicePort;
this.persistPort = persistPort;
this.persistPort = persistPort.toString();
} else {
this.persistPortEnabled = false;
this.persistPort = undefined;
Expand All @@ -124,7 +113,7 @@ class TcpIpState {

if (state.servicePortEnabled) {
await GLOBAL_STATE.adb.tcpip.setPort(
Number.parseInt(state.servicePort, 10)
Number.parseInt(state.servicePort, 10),
);
} else {
await GLOBAL_STATE.adb.tcpip.disable();
Expand Down Expand Up @@ -153,7 +142,7 @@ const TcpIp: NextPage = () => {
state.servicePortEnabled = !!value;
});
},
[]
[],
);

const handleServicePortChange = useCallback(
Expand All @@ -163,7 +152,7 @@ const TcpIp: NextPage = () => {
}
runInAction(() => (state.servicePort = value));
},
[]
[],
);

return (
Expand Down
10 changes: 10 additions & 0 deletions common/changes/@yume-chan/adb/main_2023-08-25-16-30.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@yume-chan/adb",
"comment": "Add `getListenAddresses` method to `AdbTcpIpCommand` class for retrieving current ADB over WiFi state",
"type": "none"
}
],
"packageName": "@yume-chan/adb"
}
41 changes: 41 additions & 0 deletions libraries/adb/src/commands/tcpip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
import { AdbCommandBase } from "./base.js";

/**
* ADB daemon checks for the following properties in the order of
*
* * `serviceListenAddresses` (`service.adb.listen_addrs`)
* * `servicePort` (`service.adb.tcp.port`)
* * `persistPort` (`persist.adb.tcp.port`)
*
* Once it finds a non-empty value, it will use it and ignore the rest.
*
* `serviceListenAddresses` and `persistPort` are fixed at build time,
* only `servicePort` can be changed using `setPort` and `disable`.
* This means if either `serviceListenAddresses` or `persistPort` is non-empty,
* ADB over WiFi is always enabled.
*/
export interface AdbTcpIpListenAddresses {
serviceListenAddresses: string[];
servicePort: number | undefined;
persistPort: number | undefined;
}

export class AdbTcpIpCommand extends AdbCommandBase {
#parsePort(value: string): number | undefined {
if (!value || value === "0") {
return undefined;
}
return Number.parseInt(value, 10);
}

async getListenAddresses(): Promise<AdbTcpIpListenAddresses> {
const serviceListenAddresses = await this.adb.getProp(
"service.adb.listen_addrs",
);
const servicePort = await this.adb.getProp("service.adb.tcp.port");
const persistPort = await this.adb.getProp("persist.adb.tcp.port");

return {
serviceListenAddresses: serviceListenAddresses.split(","),
servicePort: this.#parsePort(servicePort),
persistPort: this.#parsePort(persistPort),
};
}

async setPort(port: number): Promise<string> {
if (port <= 0) {
throw new Error(`Invalid port ${port}`);
Expand Down

1 comment on commit aeff1f8

@vercel
Copy link

@vercel vercel bot commented on aeff1f8 Aug 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.