Skip to content

Commit

Permalink
Support ---test-cynthion --save-captures option.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinling committed Jul 1, 2024
1 parent 31dedef commit bb8207a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@ use ui::{
stop_cynthion
};

fn have_argument(name: &str) -> bool {
std::env::args().any(|arg| arg == name)
}

fn main() {
if std::env::args().any(|arg| arg == "--test-cynthion") {
test_cynthion::run_test();
if have_argument("--test-cynthion") {
let save_captures = have_argument("--save-captures");
test_cynthion::run_test(save_captures);
} else {
let application = gtk::Application::new(
Some("com.greatscottgadgets.packetry"),
Expand Down
31 changes: 24 additions & 7 deletions src/test_cynthion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ use crate::capture::{
PacketId,
};
use crate::decoder::Decoder;
use crate::pcap::Writer;

use anyhow::{Context, Error};
use futures_lite::future::block_on;
use nusb::transfer::RequestBuffer;

use std::path::PathBuf;
use std::thread::sleep;
use std::time::Duration;

pub fn run_test() {
for (speed, ep_addr, length, sof) in [
(Speed::High, 0x81, 4096, Some((124500, 125500, 500))),
(Speed::Full, 0x82, 512, Some((995000, 1005000, 50))),
(Speed::Low, 0x83, 64, None)]
pub fn run_test(save_captures: bool) {
for (name, speed, ep_addr, length, sof) in [
("HS", Speed::High, 0x81, 4096, Some((124500, 125500, 500))),
("FS", Speed::Full, 0x82, 512, Some((995000, 1005000, 50))),
("LS", Speed::Low, 0x83, 64, None)]
{
test(speed, ep_addr, length, sof).unwrap();
test(save_captures, name, speed, ep_addr, length, sof).unwrap();
}
}

fn test(speed: Speed,
fn test(save_capture: bool,
name: &str,
speed: Speed,
ep_addr: u8,
length: usize,
sof: Option<(u64, u64, u64)>)
Expand Down Expand Up @@ -96,6 +100,19 @@ fn test(speed: Speed,
.context("Error decoding packet")?;
}

if save_capture {
// Write the capture to a file.
let path = PathBuf::from(format!("./HITL-{name}.pcap"));
let mut writer = Writer::open(path)?;
for i in 0..reader.packet_index.len() {
let packet_id = PacketId::from(i);
let packet = reader.packet(packet_id)?;
let timestamp_ns = reader.packet_time(packet_id)?;
writer.add_packet(&packet, timestamp_ns)?;
}
writer.close()?;
}

// Look for the test device in the capture.
let device_id = DeviceId::from(1);
let device_data = reader.device_data(&device_id)?;
Expand Down

0 comments on commit bb8207a

Please sign in to comment.