summary refs log tree commit diff stats
path: root/rust/qmk-hid-com/src/search.rs
blob: 3c954ba9f26fb702d3a0218cae55b8b3e9eb939d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use hidapi::HidApi;

pub fn search(api: &HidApi, vendor_name: String) -> anyhow::Result<()> {
    for device in api.device_list() {
        if let Some(string) = device.manufacturer_string() {
            if string.to_lowercase().contains(&vendor_name.to_lowercase()) {
                println!(
                    "Device matches: {} (VID) - {} (PID) {}:{} - at {} by {} via {:#?}",
                    device.vendor_id(),
                    device.product_id(),
                    device.usage_page(),
                    device.usage(),
                    device.path().to_str()?,
                    string,
                    device.bus_type(),
                );

                println!(
                    "  {} (Interface {}):",
                    match device.product_string() {
                        Some(s) => s,
                        _ => "<COULD NOT FETCH>",
                    },
                    device.interface_number()
                );
                let mut descriptor = vec![0u8; 2048];
                match device
                    .open_device(api)
                    .and_then(|dev| dev.get_report_descriptor(&mut descriptor))
                {
                    Ok(length) => {
                        println!("    {} (descriptor)", {
                            &descriptor[..length]
                                .iter()
                                .map(|val| val.to_string())
                                .collect::<String>()
                        })
                    }
                    Err(err) => println!("    Failed to retrieve descriptor ({:?})", err),
                }
                println!();
            }
        } else {
            eprintln!(
                "Device without manufacturer string ({})",
                device.product_id()
            );
        }
    }
    Ok(())
}