Fix Bluetooth discovery so scanned devices are listed
This commit is contained in:
1 parent
5d859d7b11
commit
6a6222c198
9 files changed
+276
-7
No files matched your search
@@ -1158,6 +1158,28 @@ if [ -x "$BASE_DIR/scripts/test-idle-blank-lock.py" ] && \
|
||||
fail "host idle-blank screenlock integration test failed"
|
||||
fi
|
||||
|
||||
echo "[check] Bluetooth scan stream parsing..."
|
||||
NET_SRC="$BASE_DIR/src/dwm-plugins/networks_plugin.cpp"
|
||||
NET_SO="$BASE_DIR/prebuilt/sxmo-x11/usr/lib/dwm/plugins/networks.so"
|
||||
for token in 'parseBtScanDevices' 'queryBtDevices(const std::string& scanRaw' \
|
||||
'queryBtDevices(scanRaw)' 'bluetoothctl power on' \
|
||||
'scan on 2>/dev/null'; do
|
||||
if ! grep -qF "$token" "$NET_SRC"; then
|
||||
fail "networks_plugin.cpp is missing the Bluetooth scan fix: $token"
|
||||
fi
|
||||
done
|
||||
if ! strings "$NET_SO" 2>/dev/null | \
|
||||
grep -qF 'timeout 16 bluetoothctl --timeout 15 scan on'; then
|
||||
fail "shipped networks.so is stale: rebuild with scripts/build-sxmo-x11.sh"
|
||||
fi
|
||||
if [ ! -x "$BASE_DIR/scripts/test-bluetooth-scan.py" ]; then
|
||||
fail "missing host unit test scripts/test-bluetooth-scan.py"
|
||||
fi
|
||||
if command -v g++ >/dev/null 2>&1 && \
|
||||
! "$BASE_DIR/scripts/test-bluetooth-scan.py" >/dev/null 2>&1; then
|
||||
fail "host Bluetooth scan parser test failed"
|
||||
fi
|
||||
|
||||
if [ "$FAILURES" -ne 0 ]; then
|
||||
echo "[check] $FAILURES check(s) failed."
|
||||
exit 1
|
||||
|
||||
Executable
+129
@@ -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())
|
||||
Reference in new issue
Block a user