~robertgzr/bluedesk

e1eaf6a14ef17604b1e776f2caba0e89bd81ae0c — Robert Günzler 1 year, 8 months ago 4efcfa8
Disconnect and reset adapter power state

Signed-off-by: Robert Günzler <r@gnzler.io>
1 files changed, 47 insertions(+), 1 deletions(-)

M src/main.rs
M src/main.rs => src/main.rs +47 -1
@@ 26,6 26,21 @@ without POSITION the current position will be printed to stdout\n",
    Ok(())
}

struct ScopeCall<F: FnMut()> {
    c: F
}
impl<F: FnMut()> Drop for ScopeCall<F> {
    fn drop(&mut self) {
        (self.c)();
    }
}

macro_rules! defer {
    ($e:expr) => (
        let _scope_call = ScopeCall { c: || -> () { $e; } };
    )
}

fn main() -> Result<(), Box<dyn Error>> {
    let debug: bool = std::env::var("DEBUG").is_ok();



@@ 51,12 66,28 @@ fn main() -> Result<(), Box<dyn Error>> {
    if debug {
        eprintln!("adapter initialized");
    }
    if !adapter.is_powered()? {
    let powered = adapter.is_powered()?;
    if !powered {
        if debug {
            eprintln!("adapter powering on");
        }
        adapter.set_powered(true)?;
        if debug {
            eprintln!("adapter powered on");
        }
    }
    defer!({
        if debug {
            eprintln!("adapter powering off");
        }
        if let Err(e) = adapter.set_powered(powered) {
            eprintln!("adapter failed to power off: {}", e);
            return;
        }
        if debug {
            eprintln!("adapter powered off");
        }
    });

    let devices = adapter.get_device_list()?;
    if devices.is_empty() {


@@ 94,6 125,21 @@ fn main() -> Result<(), Box<dyn Error>> {
    if debug {
        eprintln!("connected");
    }
    defer!({
        if debug {
            eprintln!(
                "disconnecting from {}",
                device.get_name().unwrap_or_else(|_| device.get_id())
            );
        }
        if let Err(e) = device.disconnect() {
            eprintln!("failed to disconnect: {}", e);
            return;
        }
        if debug {
            eprintln!("disconnected");
        }
    });

    let pos_svc = &get_service(session, device, POSITION_SERVICE_UUID)?;
    if debug {