Skip to content

Commit

Permalink
feat: Add basic bluetooth mock
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Mar 17, 2024
1 parent f4bd6b2 commit 8cfa2ec
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 74 deletions.
101 changes: 78 additions & 23 deletions src/bluetooth/bluetooth_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn get_objects(
}

pub fn convert_device(path: &Path<'static>, map: &MaskedPropMap) -> Option<BluetoothDevice> {
let map = map.get("org.bluez.Device1");
let map = map.get(BLUEZ_DEVICE_INTERFACE!());
map?;
let map = map.unwrap();
bluetooth_device_from_map(path, map)
Expand Down Expand Up @@ -162,7 +162,7 @@ pub fn get_bluetooth_adapter(path: &Path<'static>) -> BluetoothAdapter {
path.clone(),
"GetAll",
"org.freedesktop.DBus.Properties",
("org.bluez.Adapter1",),
(BLUEZ_ADAPTER_INTERFACE!(),),
1000,
(PropMap,),
);
Expand All @@ -176,7 +176,7 @@ pub fn get_bluetooth_adapter(path: &Path<'static>) -> BluetoothAdapter {

pub fn get_connections() -> Vec<re_set_lib::bluetooth::bluetooth_structures::BluetoothDevice> {
let mut devices = Vec::new();
let res = get_objects("org.bluez", "/");
let res = get_objects(BLUEZ_INTERFACE!(), "/");
if res.is_err() {
return devices;
}
Expand Down Expand Up @@ -219,7 +219,7 @@ impl BluetoothInterface {
}
let (res,) = res.unwrap();
for (path, map) in res.iter() {
let map = map.get("org.bluez.Adapter1");
let map = map.get(BLUEZ_ADAPTER_INTERFACE!());
if map.is_none() {
continue;
}
Expand Down Expand Up @@ -337,7 +337,7 @@ impl BluetoothInterface {
let res = conn.add_match(
bluetooth_device_changed,
move |ir: PropertiesPropertiesChanged, _, msg| {
if ir.interface_name != "org.bluez.Device1" {
if ir.interface_name != BLUEZ_DEVICE_INTERFACE!() {
// Here we only want to match on bluetooth device signals, the rest can be
// ignored.
return true;
Expand Down Expand Up @@ -384,9 +384,13 @@ impl BluetoothInterface {
));
}
let other = Connection::new_system().unwrap();
let proxy = other.with_proxy("org.bluez", path.clone(), Duration::from_millis(1000));
let proxy = other.with_proxy(
BLUEZ_INTERFACE!(),
path.clone(),
Duration::from_millis(1000),
);
let res: Result<(), dbus::Error> =
proxy.method_call("org.bluez.Adapter1", "StartDiscovery", ());
proxy.method_call(BLUEZ_ADAPTER_INTERFACE!(), "StartDiscovery", ());
active_listener.store(true, Ordering::SeqCst);
active_scan.store(true, Ordering::SeqCst);
loop {
Expand All @@ -396,7 +400,7 @@ impl BluetoothInterface {
active_listener.store(false, Ordering::SeqCst);
stop_requested.store(false, Ordering::SeqCst);
let res: Result<(), dbus::Error> =
proxy.method_call("org.bluez.Adapter1", "StopDiscovery", ());
proxy.method_call(BLUEZ_ADAPTER_INTERFACE!(), "StopDiscovery", ());
if res.is_err() {
ERROR!(
"/tmp/reset_daemon_log",
Expand All @@ -408,7 +412,7 @@ impl BluetoothInterface {
}
if active_scan.load(Ordering::SeqCst) {
let res: Result<(), dbus::Error> =
proxy.method_call("org.bluez.Adapter1", "StartDiscovery", ());
proxy.method_call(BLUEZ_ADAPTER_INTERFACE!(), "StartDiscovery", ());
if res.is_err() {
ERROR!(
"/tmp/reset_daemon_log",
Expand All @@ -418,7 +422,7 @@ impl BluetoothInterface {
}
} else if !active_scan.load(Ordering::SeqCst) {
let res: Result<(), dbus::Error> =
proxy.method_call("org.bluez.Adapter1", "StopDiscovery", ());
proxy.method_call(BLUEZ_ADAPTER_INTERFACE!(), "StopDiscovery", ());
if res.is_err() {
ERROR!(
"/tmp/reset_daemon_log",
Expand All @@ -439,7 +443,7 @@ impl BluetoothInterface {
BLUEZ_INTERFACE!(),
device.clone(),
"Connect",
"org.bluez.Device1",
BLUEZ_DEVICE_INTERFACE!(),
(),
10000,
(),
Expand All @@ -463,7 +467,7 @@ impl BluetoothInterface {
BLUEZ_INTERFACE!(),
device.clone(),
"Pair",
"org.bluez.Device1",
BLUEZ_DEVICE_INTERFACE!(),
(),
10000,
(),
Expand All @@ -483,7 +487,7 @@ impl BluetoothInterface {
BLUEZ_INTERFACE!(),
device,
"Disconnect",
"org.bluez.Device1",
BLUEZ_DEVICE_INTERFACE!(),
(),
1000,
(),
Expand All @@ -498,7 +502,7 @@ impl BluetoothInterface {
BLUEZ_INTERFACE!(),
Path::from(BLUEZ_PATH!()),
"RegisterAgent",
"org.bluez.AgentManager1",
BLUEZ_AGENT_INTERFACE!(),
(Path::from(DBUS_PATH!()), "DisplayYesNo"),
1000,
(),
Expand All @@ -523,7 +527,7 @@ impl BluetoothInterface {
BLUEZ_INTERFACE!(),
Path::from(BLUEZ_PATH!()),
"UnregisterAgent",
"org.bluez.AgentManager1",
BLUEZ_AGENT_INTERFACE!(),
(Path::from(DBUS_PATH!()),),
1000,
(Path<'static>,),
Expand All @@ -546,13 +550,14 @@ impl BluetoothInterface {
"/tmp/reset_daemon_log",
"Failed to start bluetooth, already active\n"
);
return;
}
scan_active.store(false, Ordering::SeqCst);
let res = dbus_method!(
BLUETOOTH_INTERFACE!(),
BLUEZ_INTERFACE!(),
self.current_adapter.clone(),
"StartDiscovery",
"org.bluez.Adapter1",
BLUEZ_ADAPTER_INTERFACE!(),
(),
1000,
(),
Expand All @@ -571,7 +576,7 @@ impl BluetoothInterface {
BLUEZ_INTERFACE!(),
self.current_adapter.clone(),
"StopDiscovery",
"org.bluez.Adapter1",
BLUEZ_ADAPTER_INTERFACE!(),
(),
1000,
(),
Expand All @@ -590,7 +595,7 @@ impl BluetoothInterface {
BLUEZ_INTERFACE!(),
self.current_adapter.clone(),
"RemoveDevice",
"org.bluez.Adapter1",
BLUEZ_ADAPTER_INTERFACE!(),
(path,),
1000,
(),
Expand All @@ -604,7 +609,7 @@ fn get_bluetooth_device_properties(path: &Path<'static>) -> PropMap {
path,
"GetAll",
"org.freedesktop.DBus.Properties",
("org.bluez.Device1",),
(BLUEZ_DEVICE_INTERFACE!(),),
1000,
(PropMap,),
);
Expand All @@ -623,7 +628,7 @@ pub fn set_adapter_enabled(path: Path<'static>, enabled: bool) -> bool {
let res = set_dbus_property!(
BLUEZ_INTERFACE!(),
path.clone(),
"org.bluez.Adapter1",
BLUEZ_ADAPTER_INTERFACE!(),
"Powered",
enabled,
);
Expand All @@ -645,7 +650,7 @@ pub fn set_adapter_discoverable(path: Path<'static>, enabled: bool) -> bool {
let res = set_dbus_property!(
BLUEZ_INTERFACE!(),
path.clone(),
"org.bluez.Adapter1",
BLUEZ_ADAPTER_INTERFACE!(),
"Discoverable",
enabled,
);
Expand All @@ -667,7 +672,7 @@ pub fn set_adapter_pairable(path: Path<'static>, enabled: bool) -> bool {
let res = set_dbus_property!(
BLUEZ_INTERFACE!(),
path.clone(),
"org.bluez.Adapter1",
BLUEZ_ADAPTER_INTERFACE!(),
"Pairable",
enabled,
);
Expand All @@ -685,6 +690,56 @@ pub fn set_adapter_pairable(path: Path<'static>, enabled: bool) -> bool {
true
}

pub fn get_all_bluetooth_adapters() -> Vec<BluetoothAdapter> {
let mut adapters = Vec::new();
let objects = get_all_objects();
for (path, map) in objects {
if path.contains("Bluez") && map.contains_key(BLUEZ_ADAPTER_INTERFACE!()) {
adapters.push(adapter_from_map(
&path,
map.get(BLUEZ_ADAPTER_INTERFACE!()).unwrap(),
));
}
}
adapters
}

pub fn get_all_bluetooth_devices() -> Vec<BluetoothDevice> {
let mut devices = Vec::new();
let objects = get_all_objects();
for (path, map) in objects {
if path.contains("Bluez") && map.contains_key(BLUEZ_DEVICE_INTERFACE!()) {
devices.push(
bluetooth_device_from_map(&path, map.get(BLUEZ_DEVICE_INTERFACE!()).unwrap())
.unwrap(),
);
}
}
devices
}

fn get_all_objects() -> HashMap<Path<'static>, HashMap<String, PropMap>> {
let res = dbus_method!(
BASE_TEST_INTERFACE!(),
"/",
// DBUS_PATH_TEST!(),
"GetManagedObjects",
"org.freedesktop.DBus.ObjectManager",
(),
1000,
(HashMap<Path<'static>, HashMap<String, PropMap>>,),
);
if res.is_err() {
ERROR!(
"/tmp/reset_daemon_log",
"Could not to get bluetooth objects",
ErrorLevel::PartialBreakage
);
return HashMap::new();
}
res.unwrap().0
}

// command needed to understand anything about bluetooth
// dbus-send --system --dest=org.freedesktop.DBus --type=method_call --print-reply \
// /org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep -v '":'
35 changes: 21 additions & 14 deletions src/bluetooth/bluetooth_manager_dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use re_set_lib::{
use crate::DaemonData;

use super::bluetooth_manager::{
get_bluetooth_adapter, get_connections, set_adapter_discoverable, set_adapter_enabled,
set_adapter_pairable,
get_all_bluetooth_adapters, get_all_bluetooth_devices, get_bluetooth_adapter, get_connections,
set_adapter_discoverable, set_adapter_enabled, set_adapter_pairable,
};

pub fn setup_bluetooth_manager(cross: &mut Crossroads) -> dbus_crossroads::IfaceToken<DaemonData> {
Expand Down Expand Up @@ -63,18 +63,22 @@ pub fn setup_bluetooth_manager(cross: &mut Crossroads) -> dbus_crossroads::Iface
Ok(())
},
);
c.method(
"GetBluetoothAdapters",
(),
("adapters",),
move |_, d: &mut DaemonData, ()| {
let mut adapters = Vec::new();
for path in d.b_interface.adapters.iter() {
adapters.push(get_bluetooth_adapter(path));
}
Ok((adapters,))
},
);
// TODO: test if new version can be used instead
// c.method(
// "GetBluetoothAdapters",
// (),
// ("adapters",),
// move |_, d: &mut DaemonData, ()| {
// let mut adapters = Vec::new();
// for path in d.b_interface.adapters.iter() {
// adapters.push(get_bluetooth_adapter(path));
// }
// Ok((adapters,))
// },
// );
c.method("GetBluetoothAdapters", (), ("devices",), move |_, _, ()| {
Ok((get_all_bluetooth_adapters(),))
});
c.method(
"GetCurrentBluetoothAdapter",
(),
Expand Down Expand Up @@ -121,6 +125,9 @@ pub fn setup_bluetooth_manager(cross: &mut Crossroads) -> dbus_crossroads::Iface
Ok((set_adapter_pairable(path, enabled),))
},
);
c.method("GetBluetoothDevices", (), ("devices",), move |_, _, ()| {
Ok((get_all_bluetooth_devices(),))
});
c.method(
"ConnectToBluetoothDevice",
("device",),
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod macros;
pub mod api;
mod audio;
mod bluetooth;
mod mock;
pub mod mock;
mod network;
mod tests;
pub mod utils;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub async fn run_daemon() {
let wifi_enabled = res.is_ok();
let res = dbus_method!(
BLUEZ_INTERFACE!(),
Path::from(BLUEZ_PATH!()),
"/",
"Introspect",
"org.freedesktop.DBus.Introspectable",
(),
Expand Down
Loading

0 comments on commit 8cfa2ec

Please sign in to comment.