summary history branches tags files
commit:b47e15e42ff20b8de4fca0a32f3038c8c78678a0
author:Trevor Bentley
committer:Trevor Bentley
date:Tue May 7 02:41:10 2024 +0200
parents:aa640dccf28e9da450084a18362a30f70816e1df
add mgmt select example
diff --git a/examples/mon_mgmt_select.rs b/examples/mon_mgmt_select.rs
line changes: +43/-0
index 0000000..36825fa
--- /dev/null
+++ b/examples/mon_mgmt_select.rs
@@ -0,0 +1,43 @@
+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")
+}