Fix Bluetooth discovery so scanned devices are listed

This commit is contained in:
Luxferre committed 2026-11-10 12:58:51 +02:00
1 parent 5d859d7b11
commit 6a6222c198
9 files changed
+276 -7

No files matched your search

+129
View File
@@ -0,0 +1,129 @@
#!/usr/bin/env python3
"""Host unit test for the networks plugin's Bluetooth scan-stream parser.
The plugin is cross-compiled for armv7; this test extracts the pure parser
functions and compiles them on the host with a stubbed BtDeviceEntry/trimStr,
so a BlueZ discovery stream can be validated without a controller.
"""
import pathlib
import subprocess
import sys
import tempfile
BASE = pathlib.Path(__file__).resolve().parent.parent
SOURCE = BASE / "src/dwm-plugins/networks_plugin.cpp"
FUNCTIONS = (
"std::string stripBtAnsi",
"bool isBluetoothMac",
"std::vector<BtDeviceEntry> parseBtScanDevices",
)
HEAD = r'''
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
struct BtDeviceEntry {
std::string mac;
std::string name;
bool paired = false;
bool connected = false;
};
std::string trimStr(const std::string& s) {
size_t a = 0;
size_t b = s.size();
while (a < b && std::isspace(static_cast<unsigned char>(s[a]))) ++a;
while (b > a && std::isspace(static_cast<unsigned char>(s[b - 1]))) --b;
return s.substr(a, b - a);
}
#define CHECK(cond) do { \
if (!(cond)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); return 1; } \
} while (0)
'''
MAIN = r'''
int main() {
CHECK(isBluetoothMac("AA:BB:CC:DD:EE:01"));
CHECK(isBluetoothMac("de:ad:be:ef:00:01"));
CHECK(!isBluetoothMac("AA-BB-CC-DD-EE-01"));
CHECK(!isBluetoothMac("AA:BB:CC:DD:EE"));
CHECK(!isBluetoothMac("ZZ:BB:CC:DD:EE:01"));
std::string raw =
"\033[0;94m[NEW]\033[0m Device AA:BB:CC:DD:EE:01 Kitchen Speaker\r\n"
"[CHG] Device AA:BB:CC:DD:EE:01 RSSI: -55\n"
"\033[0;93m[CHG]\033[0m Device AA:BB:CC:DD:EE:01 Name: Living Room\n"
"[NEW] Device 11:22:33:44:55:66 11:22:33:44:55:66\n"
"[NEW] Device DE:AD:BE:EF:00:01 Headset\n"
"[DEL] Device 99:88:77:66:55:44 Gone\n"
"[NEW] Device ZZ:BB:CC:DD:EE:01 Broken\n"
"Discovery started\n";
std::vector<BtDeviceEntry> found = parseBtScanDevices(raw);
CHECK(found.size() == 3);
CHECK(found[0].mac == "AA:BB:CC:DD:EE:01");
CHECK(found[0].name == "Living Room");
CHECK(found[1].mac == "11:22:33:44:55:66");
CHECK(found[1].name == "11:22:33:44:55:66");
CHECK(found[2].mac == "DE:AD:BE:EF:00:01");
CHECK(found[2].name == "Headset");
CHECK(stripBtAnsi("\033[0;94mOK\033[0m") == "OK");
std::puts("bluetooth scan parser: ok");
return 0;
}
'''
def extract(name, text):
start = text.find("\n" + name + "(")
if start < 0:
raise SystemExit("missing function %s in networks_plugin.cpp" % name)
start += 1
depth = 0
opened = False
for pos in range(start, len(text)):
if text[pos] == "{":
depth += 1
opened = True
elif text[pos] == "}":
depth -= 1
if opened and depth == 0:
return text[start:pos + 1] + "\n"
raise SystemExit("unterminated function %s" % name)
def main():
text = SOURCE.read_text(encoding="utf-8")
funcs = "\n".join(extract(name, text) for name in FUNCTIONS)
harness = HEAD + funcs + MAIN
with tempfile.TemporaryDirectory(prefix="bananarchy-bt-scan-") as tmp:
root = pathlib.Path(tmp)
src = root / "bt_scan_test.cpp"
exe = root / "bt_scan_test"
src.write_text(harness, encoding="utf-8")
build = subprocess.run(
["g++", "-std=c++20", "-Wall", "-Wextra", "-Werror", str(src), "-o", str(exe)],
capture_output=True, text=True)
if build.returncode:
sys.stderr.write(build.stderr)
return 1
probe = subprocess.run([str(exe)], capture_output=True, text=True)
if probe.returncode:
sys.stderr.write(probe.stderr)
return 1
print(probe.stdout.strip())
if "queryBtDevices(scanRaw)" not in text or \
"scan on 2>/dev/null" not in text:
sys.stderr.write("FAIL: scan output is not fed into the device list\n")
return 1
print("bluetooth scan wiring: all checks passed")
return 0
if __name__ == "__main__":
sys.exit(main())