]> glassweightruler.freedombox.rocks Git - waydroid.git/blob - tools/helpers/net.py
net: Add "waydroid adb connect" action
[waydroid.git] / tools / helpers / net.py
1 # Copyright 2023 Maximilian Wende
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 from shutil import which
4 import tools.helpers.run
5 import logging
6 import re
7
8 def adb_connect(args):
9 """
10 Creates an android debugging connection from the host system to the
11 Waydroid device, if ADB is found on the host system and the device
12 has booted.
13 """
14 # Check if adb exists on the system.
15 if not which("adb"):
16 raise RuntimeError("Could not find adb")
17
18 # Start and 'warm up' the adb server
19 tools.helpers.run.user(args, ["adb", "start-server"])
20
21 ip = get_device_ip_address()
22 if not ip:
23 raise RuntimeError("Unknown container IP address. Is Waydroid running?")
24
25 tools.helpers.run.user(args, ["adb", "connect", ip])
26 logging.info("Established ADB connection to Waydroid device at {}.".format(ip))
27
28 def adb_disconnect(args):
29 if not which("adb"):
30 raise RuntimeError("Could not find adb")
31
32 ip = get_device_ip_address()
33 if not ip:
34 raise RuntimeError("Unknown container IP address. Was Waydroid ever running?")
35
36 tools.helpers.run.user(args, ["adb", "disconnect", ip])
37
38 def get_device_ip_address():
39 # The IP address is queried from the DHCP lease file.
40 lease_file = "/var/lib/misc/dnsmasq.waydroid0.leases"
41
42 try:
43 with open(lease_file) as f:
44 return re.search(r"(\d{1,3}\.){3}\d{1,3}\s", f.read()).group().strip()
45 except:
46 pass