summary history branches tags files
examples/mon_mgmt_select.rs
use menomonmon::{
    monitor,
    CliArgs,
    UsbmonFilter,
};
use nix::libc;


fn main() {
    // get optional device filter from command-line args
    let dev_filter = CliArgs::dev_filter();

    // Start outputting packets when a mgmt applet select APDU is
    // issued
    let start_filter = UsbmonFilter {
        epnum: Some(|x: u8| x == 0x02),              // EP 2 (CCID)
        // select APDU with mgmt AID
        data: Some(|x: &[u8]| {
            for i in 0..(x.len()-12) {
                if x[i..i+12] == [0xa4, 0x04, 0x00, 0x08,
                                 0xa0, 0x00, 0x00, 0x05, 0x27, 0x47, 0x11, 0x17] {
                    return true;
                }
            }
            false
        }),
        ..Default::default()
    };

    // Stop outputting packets when the select data is returned
    let end_filter = UsbmonFilter {
        epnum: Some(|x: u8| x == 0x02),              // EP 2 (CCID)
        data_len: Some(|x: libc::c_uint| x > 0),     // data packet
        dir_out: Some(|x: bool| !x),                 // in packet (response)
        ..Default::default()
    };

    // output all packets for the selected device between the two
    // filter events.
    let usec = monitor(Some(&dev_filter), Some(&start_filter), None, Some(&end_filter));

    println!("MGMT selected in: {usec} µs")
}