LordOS — a free, fast and secure GNU/Linux distribution based on Debian, with its own desktop environment: display server and Wayland compositor in C, a React UI engine on QuickJS, and private native apps.
C cdebiandesktop-environmentebpffirewalllandlocklinuxlinux-distributionopen-sourceprivacyquickjsraspberry-pi-5reactsecuritywaylandwayland-compositor
/* bt_test.c — BlueZ parsing for Settings → Bluetooth (src/hw/bt.c), without a bus: messages
* are built with our own D-Bus writer (vault/dbus.c), parsed by the strict reader and fed to
* the model. Covers GetManagedObjects, PropertiesChanged-style dictionaries, Battery1, removal,
* object path validation and the state JSON. Run by `make test` (mk/settings.mk). */
#include "hw/bt.h"
#include "vault/dbus.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int fails, checks;
#define CHECK(c) do { checks++; if (!(c)) { fails++; fprintf(stderr, "%s:%d: failed: %s\n", __FILE__, __LINE__, #c); } } while (0)
static void prop_s(DBuf *b, const char *k, const char *v) { dw_struct(b); dw_str(b, k); dw_variant(b, "s"); dw_str(b, v); }
static void prop_o(DBuf *b, const char *k, const char *v) { dw_struct(b); dw_str(b, k); dw_variant(b, "o"); dw_str(b, v); }
static void prop_b(DBuf *b, const char *k, int v) { dw_struct(b); dw_str(b, k); dw_variant(b, "b"); dw_bool(b, v); }
static void prop_u(DBuf *b, const char *k, uint32_t v) { dw_struct(b); dw_str(b, k); dw_variant(b, "u"); dw_u32(b, v); }
static void prop_y(DBuf *b, const char *k, uint8_t v) { dw_struct(b); dw_str(b, k); dw_variant(b, "y"); dw_u8(b, v); }
static void prop_n(DBuf *b, const char *k, int16_t v) {
dw_struct(b); dw_str(b, k); dw_variant(b, "n");
dw_align(b, 2); dw_u8(b, (uint8_t)(v & 0xff)); dw_u8(b, (uint8_t)((uint16_t)v >> 8));
}
static void prop_as(DBuf *b, const char *k, const char *a, const char *c) {
dw_struct(b); dw_str(b, k); dw_variant(b, "as");
size_t m = dw_arr_begin(b, 4); dw_str(b, a); dw_str(b, c); dw_arr_end(b, m);
}
/* Builds a full message with that body and parses it back like one from the bus. */
static int as_msg(const char *sig, DBuf *body, uint8_t type, DMsg *m) {
DHdr h = { .type = type, .serial = 7, .reply_serial = type == DBUS_RETURN ? 3 : 0, .sig = sig,
.path = type == DBUS_SIGNAL ? "/org/bluez/hci0/dev_00_AA_01_01_00_01" : NULL,
.iface = type == DBUS_SIGNAL ? "org.freedesktop.DBus.Properties" : NULL,
.member = type == DBUS_SIGNAL ? "PropertiesChanged" : NULL, .sender = ":1.5" };
DBuf out;
dw_init(&out);
if (dbus_build(&h, body, &out)) return -1;
long n = dbus_msg_size(out.p, out.n);
int r = n == (long)out.n ? dbus_msg_parse(out.p, out.n, m) : -1;
dw_free(&out);
return r;
}
static void managed_body(DBuf *b) {
dw_init(b);
size_t objs = dw_arr_begin(b, 8);
/* the adapter */
dw_struct(b); dw_str(b, "/org/bluez/hci0");
size_t ifs = dw_arr_begin(b, 8);
dw_struct(b); dw_str(b, "org.freedesktop.DBus.Introspectable");
size_t p0 = dw_arr_begin(b, 8); dw_arr_end(b, p0);
dw_struct(b); dw_str(b, "org.bluez.Adapter1");
size_t p1 = dw_arr_begin(b, 8);
prop_s(b, "Address", "00:AA:01:00:00:00");
prop_s(b, "Alias", "lordos-notes");
prop_b(b, "Powered", 1);
prop_b(b, "Discoverable", 0);
prop_b(b, "Discovering", 1);
prop_u(b, "Class", 0x6c010c);
prop_as(b, "UUIDs", "0000110e-0000-1000-8000-00805f9b34fb", "0000110c-0000-1000-8000-00805f9b34fb");
dw_arr_end(b, p1);
dw_arr_end(b, ifs);
/* a paired headset with a battery */
dw_struct(b); dw_str(b, "/org/bluez/hci0/dev_00_AA_01_01_00_01");
ifs = dw_arr_begin(b, 8);
dw_struct(b); dw_str(b, "org.bluez.Device1");
p1 = dw_arr_begin(b, 8);
prop_s(b, "Address", "00:AA:01:01:00:01");
prop_s(b, "Name", "Auriculares de prueba");
prop_s(b, "Alias", "Auriculares de prueba");
prop_s(b, "Icon", "audio-headset");
prop_o(b, "Adapter", "/org/bluez/hci0");
prop_u(b, "Class", 0x240404);
prop_b(b, "Paired", 1);
prop_b(b, "Trusted", 1);
prop_b(b, "Connected", 0);
prop_n(b, "RSSI", -67);
prop_as(b, "UUIDs", "0000110b-0000-1000-8000-00805f9b34fb", "0000111e-0000-1000-8000-00805f9b34fb");
dw_arr_end(b, p1);
dw_struct(b); dw_str(b, "org.bluez.Battery1");
p1 = dw_arr_begin(b, 8);
prop_y(b, "Percentage", 80);
dw_arr_end(b, p1);
dw_arr_end(b, ifs);
/* an unnamed LE device on a second adapter's path that does not exist (kept) */
dw_struct(b); dw_str(b, "/org/bluez/hci1/dev_11_22_33_44_55_66");
ifs = dw_arr_begin(b, 8);
dw_struct(b); dw_str(b, "org.bluez.Device1");
p1 = dw_arr_begin(b, 8);
prop_s(b, "Address", "11:22:33:44:55:66");
dw_arr_end(b, p1);
dw_arr_end(b, ifs);
/* something that is not BlueZ's: ignored */
dw_struct(b); dw_str(b, "/org/bluez");
ifs = dw_arr_begin(b, 8);
dw_struct(b); dw_str(b, "org.bluez.AgentManager1");
p1 = dw_arr_begin(b, 8); dw_arr_end(b, p1);
dw_arr_end(b, ifs);
dw_arr_end(b, objs);
}
int main(void) {
CHECK(bt_path_valid("/org/bluez/hci0"));
CHECK(bt_path_valid("/org/bluez/hci12/dev_00_AA_01_01_00_01"));
CHECK(!bt_path_valid("/org/bluez/hci0/dev_00_AA_01_01_00"));
CHECK(!bt_path_valid("/org/bluez/hci0/dev_00_AA_01_01_00_0G"));
CHECK(!bt_path_valid("/org/bluez/hci0/dev_00_AA_01_01_00_01/sep1"));
CHECK(!bt_path_valid("/org/bluez/hcix"));
CHECK(!bt_path_valid("/org/evil/hci0"));
CHECK(!bt_path_valid(NULL));
BtModel *m = bt_model();
memset(m, 0, sizeof *m);
DBuf b;
managed_body(&b);
DMsg msg;
CHECK(as_msg("a{oa{sa{sv}}}", &b, DBUS_RETURN, &msg) == 0);
dw_free(&b);
CHECK(bt_parse_managed(m, &msg) == 0);
dbus_msg_free(&msg);
CHECK(m->nadapters == 1);
CHECK(!strcmp(m->adapters[0].address, "00:AA:01:00:00:00") && !strcmp(m->adapters[0].alias, "lordos-notes"));
CHECK(m->adapters[0].powered && !m->adapters[0].discoverable && m->adapters[0].discovering);
CHECK(m->ndevices == 2);
BtDevice *d = &m->devices[0];
CHECK(!strcmp(d->path, "/org/bluez/hci0/dev_00_AA_01_01_00_01") && !strcmp(d->adapter, "/org/bluez/hci0"));
CHECK(!strcmp(d->name, "Auriculares de prueba") && !strcmp(d->icon, "audio-headset") && d->cls == 0x240404);
CHECK(d->paired && d->trusted && !d->connected && d->has_rssi && d->rssi == -67 && d->battery == 80);
CHECK(m->devices[1].battery == -1 && !strcmp(m->devices[1].adapter, "/org/bluez/hci1"));
/* PropertiesChanged on the device: connected, battery gone */
dw_init(&b);
dw_str(&b, "org.bluez.Device1");
size_t a = dw_arr_begin(&b, 8);
prop_b(&b, "Connected", 1);
prop_s(&b, "Alias", "Mis auriculares");
dw_arr_end(&b, a);
a = dw_arr_begin(&b, 4);
dw_str(&b, "RSSI");
dw_arr_end(&b, a);
CHECK(as_msg("sa{sv}as", &b, DBUS_SIGNAL, &msg) == 0);
dw_free(&b);
DRd r;
dr_init(&r, &msg);
const char *iface = dr_str(&r, NULL);
CHECK(iface && !strcmp(iface, "org.bluez.Device1"));
bt_apply_props(m, msg.path, iface, &r);
CHECK(!r.err);
dbus_msg_free(&msg);
CHECK(d->connected && !strcmp(d->alias, "Mis auriculares"));
bt_remove_object(m, d->path, "org.bluez.Battery1");
CHECK(d->battery == -1);
char *j = bt_state_json();
CHECK(j != NULL);
/* without bluetoothd on the bus the state says so, but still lists what is in the model */
CHECK(j && strstr(j, "\"available\":false") && strstr(j, "\"name\":\"Mis auriculares\"") && strstr(j, "\"connected\":true"));
CHECK(j && strstr(j, "\"rssi\":-67") && !strstr(j, "11:22:33:44:55:66")); /* other adapter's device: not listed */
free(j);
/* a truncated dictionary is rejected by the reader, the model is not corrupted */
dw_init(&b);
dw_str(&b, "org.bluez.Device1");
a = dw_arr_begin(&b, 8);
prop_b(&b, "Paired", 0);
dw_arr_end(&b, a);
a = dw_arr_begin(&b, 4);
dw_arr_end(&b, a);
CHECK(as_msg("sa{sv}as", &b, DBUS_SIGNAL, &msg) == 0);
msg.body_len -= 6; /* cut inside the dictionary */
dr_init(&r, &msg);
iface = dr_str(&r, NULL);
bt_apply_props(m, msg.path, iface, &r);
CHECK(r.err);
dbus_msg_free(&msg);
dw_free(&b);
/* removing the adapter takes its devices */
bt_remove_object(m, "/org/bluez/hci0", "org.bluez.Adapter1");
CHECK(m->nadapters == 0 && m->ndevices == 1 && !strcmp(m->devices[0].address, "11:22:33:44:55:66"));
bt_remove_object(m, "/org/bluez/hci1/dev_11_22_33_44_55_66", "org.bluez.Device1");
CHECK(m->ndevices == 0);
/* a reply with the wrong signature */
dw_init(&b);
dw_str(&b, "x");
CHECK(as_msg("s", &b, DBUS_RETURN, &msg) == 0);
CHECK(bt_parse_managed(m, &msg) != 0);
dbus_msg_free(&msg);
dw_free(&b);
printf("bt: %d/%d passed\n", checks - fails, checks);
return fails != 0;
}