我正在尝试使用the dbus-rs库使用GetSettings方法从NetworkManager获取连接信息。我使用D-Feet尝试了具有GetSettings() -> (Dict of {String, Dict of {String, Variant} } settings)签名的函数,D-Feet返回的数据如下:

{'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': []}}

和我的代码:
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);
}

导致此错误:

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<'_>>`

如何获取连接信息?

我以为Variant的全部意义在于它可以容纳不同类型的数据?我究竟做错了什么?

如果我将Variant类型()替换为&str,则会打印出Dict(Iter("Unknown?!", "Unknown?!", "Unknown?!", "Unknown?!", "Unknown?!"), PhantomData)Unknown?!的值是多少?

API for Dict 也指出



我如何/应该使用它呢?我确实取得了一些进步,也许是天真的吗?

我的代码现在看起来像这样:
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);
}

打印出来:

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: "" }

如何从Variant中获取数据并将其放入connection结构中?有没有更好的方法来使用迭代器,因为这感觉很脆弱?

最佳答案

以下是通过修改第一个示例来解决此问题的方法:

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>());
    }
}

如果您不了解变体中的内容,则需要使用Variant<Iter>。然后,您将不得不使用get::<&str>(), get::<u32>()等在变量中检索实际值。

关于linux - 使用Rust DBUS库时不满足特征绑定(bind) `dbus::arg::Get`,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41442745/

10-09 06:38