From 3dd652c1d70da608c77d1ded9e6d44774fc10ebc Mon Sep 17 00:00:00 2001 From: jamie Date: Sat, 29 Aug 2026 22:15:55 +0100 Subject: refactor: dedupe helpers, name protocol constants, testable daemon args --- src/bin/probe.rs | 74 +++++++++++++++++++------------------------------------- 1 file changed, 25 insertions(+), 49 deletions(-) (limited to 'src/bin/probe.rs') diff --git a/src/bin/probe.rs b/src/bin/probe.rs index e77e6cd..008565c 100644 --- a/src/bin/probe.rs +++ b/src/bin/probe.rs @@ -1,8 +1,8 @@ //! mmx150-probe — reverse-engineering tool for the MMX 150 dongle protocol. //! -//! Phase 1 workflow: send candidate output reports to the dongle and observe -//! what input reports come back, correlating fields with headset on/off, -//! battery charge level and button presses. +//! Sends candidate output reports to the dongle and prints what input +//! reports come back. Kept for future protocol work: battery status, for +//! example, is not exposed by any command found so far. use std::process::ExitCode; use std::time::Duration; @@ -25,6 +25,22 @@ fn main() -> ExitCode { } } +/// Find and open the dongle, printing an error and returning `None` if it +/// is missing or cannot be opened. +fn open_dongle() -> Option { + let Some(dev) = mmx150::hid::find_mmx150().unwrap_or(None) else { + eprintln!("MMX 150 dongle not found"); + return None; + }; + match Hidraw::open(&dev.devnode) { + Ok(h) => Some(h), + Err(e) => { + eprintln!("open failed: {e}"); + None + } + } +} + fn cmd_list(_rest: &[String]) -> ExitCode { match find_devices_by_vendor(BEYERDYNAMIC_VID) { Ok(devices) => { @@ -120,18 +136,9 @@ fn cmd_send(rest: &[String]) -> ExitCode { data.extend(bytes); } - let Some(dev) = mmx150::hid::find_mmx150().unwrap_or(None) else { - eprintln!("MMX 150 dongle not found"); + let Some(mut hid) = open_dongle() else { return ExitCode::FAILURE; }; - let hid = match Hidraw::open(&dev.devnode) { - Ok(h) => h, - Err(e) => { - eprintln!("open failed: {e}"); - return ExitCode::FAILURE; - } - }; - let mut hid = hid; println!( ">> 0x{report_id:02x} [{}B] {}", @@ -160,25 +167,12 @@ fn send_and_read(hid: &mut Hidraw, data: &[u8]) -> Option> { .flatten() } -/// Sweep a set of candidate commands against the dongle, printing responses -/// that differ from the all-zero baseline. Reports only non-trivial responses. -/// -/// usage: mmx150-probe sweep ... -/// is the output report id to use, -/// is the report payload length (bytes, excluding the id byte). +/// Send a fixed set of diagnostic commands (one per known report family) +/// and print their responses side by side. fn cmd_status_dump() -> ExitCode { - let Some(dev) = mmx150::hid::find_mmx150().unwrap_or(None) else { - eprintln!("MMX 150 dongle not found"); + let Some(mut hid) = open_dongle() else { return ExitCode::FAILURE; }; - let hid = match Hidraw::open(&dev.devnode) { - Ok(h) => h, - Err(e) => { - eprintln!("open failed: {e}"); - return ExitCode::FAILURE; - } - }; - let mut hid = hid; // Each row: label, report id, command payload. let cmds: Vec<(&str, u8, Vec)> = vec![ @@ -252,18 +246,9 @@ fn cmd_sel_enum(rest: &[String]) -> ExitCode { .and_then(|s| u8::from_str_radix(s.trim_start_matches("0x"), 16).ok()) .unwrap_or(0x3a); - let Some(dev) = mmx150::hid::find_mmx150().unwrap_or(None) else { - eprintln!("MMX 150 dongle not found"); + let Some(mut hid) = open_dongle() else { return ExitCode::FAILURE; }; - let hid = match Hidraw::open(&dev.devnode) { - Ok(h) => h, - Err(e) => { - eprintln!("open failed: {e}"); - return ExitCode::FAILURE; - } - }; - let mut hid = hid; println!("sweeping report 0x{report_id:02x}, selector range 0x00..0xff"); for sel in 0x00u8..=0xffu8 { @@ -304,18 +289,9 @@ fn cmd_sweep(rest: &[String]) -> ExitCode { }; let plen: usize = rest.get(1).and_then(|s| s.parse().ok()).unwrap_or(60); - let Some(dev) = mmx150::hid::find_mmx150().unwrap_or(None) else { - eprintln!("MMX 150 dongle not found"); + let Some(mut hid) = open_dongle() else { return ExitCode::FAILURE; }; - let hid = match Hidraw::open(&dev.devnode) { - Ok(h) => h, - Err(e) => { - eprintln!("open failed: {e}"); - return ExitCode::FAILURE; - } - }; - let mut hid = hid; // Baseline: all-zero command. let base = { -- cgit v1.3.1