The trait bound `dbus::arg::Get` is not satisfied when using the Rust DBUS library

I am trying to use the dbus-rs library to get connection info from NetworkManager using the GetSettings method. I used D-Feet to try out the function which has a signature of GetSettings() -> (Dict of {String, Dict of {String, Variant} } settings), the data returned by D-Feet is below:

{'802-11-wireless': {'mac-address': [228, 179, 24, 77, 64, 139],
                     'mac-address-blacklist': [],
                     'mode': 'infrastructure',
                     'security': '802-11-wireless-security',
                     'seen-bssids': ['02:1D:AA:80:1B:DA', '02:1D:AA:80:1D:12'],
                     'ssid': [80,
                              108,
                              111,
                              117,
                              103,
                              104,
                              32,
                              87,
                              97,
                              121,
                              32,
                              67,
                              97,
                              102,
                              101]},
 '802-11-wireless-security': {'auth-alg': 'open',
                              'group': [],
                              'key-mgmt': 'wpa-psk',
                              'pairwise': [],
                              'proto': []},
 'connection': {'id': 'Plough Way Cafe',
                'permissions': [],
                'secondaries': [],
                'timestamp': 1479304123,
                'type': '802-11-wireless',
                'uuid': 'ff9b7028-0911-491a-bfe8-53cba76069da'},
 'ipv4': {'address-data': [],
          'addresses': [],
          'dns': [],
          'dns-search': [],
          'method': 'auto',
          'route-data': [],
          'routes': []},
 'ipv6': {'address-data': [],
          'addresses': [],
          'dns': [],
          'dns-search': [],
          'method': 'auto',
          'route-data': [],
          'routes': []}}

and my code:

extern crate dbus;

fn main() {
    let message = dbus::Message::new_method_call("org.freedesktop.NetworkManager",
                                                 "/org/freedesktop/NetworkManager/Settings/0",
                                                 "org.freedesktop.NetworkManager.Settings.\
                                                  Connection",
                                                 "GetSettings")
        .unwrap();

    let response = dbus::Connection::get_private(dbus::BusType::System)
        .unwrap()
        .send_with_reply_and_block(message, 2000)
        .unwrap();

    println!("{:?}", response);

    let test: dbus::arg::Dict<&str, dbus::arg::Dict<&str, dbus::arg::Variant<()>, _>, _> =
        response.get1().unwrap();

    println!("{:?}", test);
}

which results in this error:

error[E0277]: the trait bound `(): dbus::arg::Get<'_>` is not satisfied
  --> src/main.rs:20:18
   |
20 |         response.get1().unwrap();
   |                  ^^^^ the trait `dbus::arg::Get<'_>` is not implemented for `()`
   |
   = note: required because of the requirements on the impl of `dbus::arg::Get<'_>` for `dbus::arg::Variant<()>`
   = note: required because of the requirements on the impl of `dbus::arg::Get<'_>` for `dbus::arg::Dict<'_, &str, dbus::arg::Variant<()>, dbus::arg::Iter<'_>>`
   = note: required because of the requirements on the impl of `dbus::arg::Get<'_>` for `dbus::arg::Dict<'_, &str, dbus::arg::Dict<'_, &str, dbus::arg::Variant<()>, dbus::arg::Iter<'_>>,     dbus::arg::Iter<'_>>`

How can I get the connection info?

I thought the whole point of Variant is that it can hold different types of data? What am I doing wrong?

If I substitute the Variant type () with &str it prints out Dict(Iter("Unknown?!", "Unknown?!", "Unknown?!", "Unknown?!", "Unknown?!"), PhantomData). What is the Unknown?! value?

The API for Dict also states

Creates a new Dict from an iterator. The iterator is consumed when appended.

How/should I use this instead?

UPDATE

I have managed to make some progress, perhaps naively?

My code now looks like this:

extern crate dbus;
extern crate network_manager;

#[derive(Default, Debug)]
struct Connection {
    path: String,
    id: String,
    uuid: String,
    ssid: String,
    interface: String,
    security: String,
    psk: String, // Only used when creating a new connection
}

fn main() {
    let connection = Connection {
        path: "/org/freedesktop/NetworkManager/Settings/0".to_string(),
        ..Default::default()
    };

    let message = dbus::Message::new_method_call("org.freedesktop.NetworkManager",
                                                 connection.path.clone(),
                                                 "org.freedesktop.NetworkManager.Settings.\
                                                  Connection",
                                                 "GetSettings")
        .unwrap();

    let response = dbus::Connection::get_private(dbus::BusType::System)
        .unwrap()
        .send_with_reply_and_block(message, 2000)
        .unwrap();

    let mut outer_array_iter = response.iter_init().recurse(97).unwrap();
    loop {
        let mut outer_dict_iter = outer_array_iter.recurse(101).unwrap();
        outer_dict_iter.next();

        let mut inner_array_iter = outer_dict_iter.recurse(97).unwrap();
        loop {
            let mut inner_dict_iter = inner_array_iter.recurse(101).unwrap();

            let key = inner_dict_iter.read::<&str>().unwrap();
            match key {
                "id" => {
                    let val: dbus::arg::Variant<&str> = inner_dict_iter.read().unwrap();
                    println!("id {:?}", val);
                }
                "uuid" => {
                    let val: dbus::arg::Variant<&str> = inner_dict_iter.read().unwrap();
                    println!("uuid {:?}", val);
                }
                "ssid" => {
                    let val: dbus::arg::Variant<dbus::arg::Array<i32, _>> = inner_dict_iter.read()
                        .unwrap();
                    println!("ssid {:?}", val);
                }
                "type" => {
                    let val: dbus::arg::Variant<&str> = inner_dict_iter.read().unwrap();
                    println!("interface {:?}", val);
                }
                "security" => {
                    let val: dbus::arg::Variant<&str> = inner_dict_iter.read().unwrap();
                    println!("security {:?}", val);
                }
                _ => (),
            }

            if !(inner_array_iter.next()) {
                break;
            }
        }

        if !(outer_array_iter.next()) {
            break;
        }
    }

    println!("{:?}", connection);
}

this prints out:

id Variant("Plough Way Cafe")
uuid Variant("e078808e-626f-4be9-bbb4-5e48b1d626de")
interface Variant("802-11-wireless")
ssid Variant(Array(Iter("u8", "u8", "u8", "u8", "u8", "u8", "u8", "u8"), PhantomData))
security Variant("802-11-wireless-security")
Connection { path: "/org/freedesktop/NetworkManager/Settings/0", id: "", uuid: "", ssid: "", interface: "", security: "", psk: "" }

How can I get the data out of Variant and into my connection struct?

Is there a better way to use iterators as this feels v.fragile?

Thanks

Here's how to fix it:

let test: dbus::arg::Dict<&str, dbus::arg::Dict<&str, dbus::arg::Variant<dbus::arg::Iter>, _>, _> =
response.get1().unwrap();

for (k1, v1) in test {
    println!("outer key = {:?}", k1);
    for (k2, v2) in v1 {
        println!("    inner key = {:?}, inner type = {:?}, string value = {:?}", k2, v2, v2.0.clone().get::<&str>());
    }
}

You need to use Variant<Iter> if you don't know what's inside your variant when you get it. You will then have to use get::<&str>(), get::<u32>(), etc to retrieve the actual value inside the variant.

1 Like