1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# deck-it - tablet module
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""The tablet's own battery and Wi-Fi, read over adb.
The tablet is the device whose adb reverse carries the dashboard socket, the
same test `deck-it status` uses, so a USB/Wi-Fi switch is followed.
"""
import re
import subprocess
import threading
import time
from ..module import Module
STATUS = {"2": "charging", "3": "discharging", "4": "not charging", "5": "full"}
TTL = 10 # seconds; each read is two adb round trips
def _adb(*args, timeout=5):
return subprocess.run(["adb", *args], capture_output=True, text=True,
timeout=timeout, check=True).stdout
def _find_tablet():
for line in _adb("devices").splitlines()[1:]:
f = line.split()
if len(f) == 2 and f[1] == "device":
try:
if "deck-it/http.sock" in _adb("-s", f[0], "reverse", "--list", timeout=3):
return f[0]
except (subprocess.SubprocessError, OSError):
continue
return None
def parse_battery(text):
kv = dict(re.findall(r"^\s*([\w ]+): (.+)$", text, re.M))
plug = next((k.split()[0] for k in ("AC powered", "USB powered", "Wireless powered")
if kv.get(k) == "true"), None)
scale = int(kv.get("scale", 100)) or 100
temp = kv.get("temperature")
return {
"level": round(100 * int(kv.get("level", 0)) / scale),
"status": STATUS.get(kv.get("status"), "unknown"),
"plug": plug,
"temp": int(temp) / 10 if temp else None,
}
def parse_wifi(text):
"""`cmd wifi status` (Android 11+): a 'connected to "SSID"' line plus WifiInfo."""
m = re.search(r'connected to "(.*?)"', text)
if not m:
return {"connected": False}
rssi = re.search(r"RSSI: (-?\d+)", text)
speed = re.search(r"Link speed: (\d+)", text)
freq = re.search(r"Frequency: (\d+)", text)
return {
"connected": True,
"ssid": m[1],
"rssi": int(rssi[1]) if rssi else None,
"speed": int(speed[1]) if speed else None,
"band": f"{int(freq[1]) / 1000:.1f} GHz" if freq else None,
}
def make(cfg: dict) -> Module:
lock = threading.Lock()
cache = {"t": 0, "v": None, "serial": None}
def read():
serial = cache["serial"] or _find_tablet()
if not serial:
return {"connected": False, "error": "no tablet with the dashboard tunnel"}
try:
batt = _adb("-s", serial, "shell", "dumpsys", "battery")
wifi = _adb("-s", serial, "shell", "cmd", "wifi", "status")
except (subprocess.SubprocessError, OSError) as e:
cache["serial"] = None # look again next time; it may have moved USB <-> Wi-Fi
return {"connected": False, "error": str(e) or type(e).__name__}
cache["serial"] = serial
return {"connected": True, "link": "Wi-Fi" if ":" in serial else "USB",
"battery": parse_battery(batt), "wifi": parse_wifi(wifi)}
def state():
# ponytail: one shared cache, so several open pages cost one adb read per TTL.
with lock:
if time.monotonic() - cache["t"] >= TTL:
cache["v"] = read()
cache["t"] = time.monotonic()
return cache["v"]
return Module(name="tablet", title="Tablet", state=state)
|