Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial PcapNG file format support #185

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ built = { version = "0.7.4", features = ["cargo-lock", "git2"] }
[dependencies]
bytemuck = "1.14.1"
bytemuck_derive = "1.5.0"
gtk = { version = "0.8.0", package = "gtk4" }
gtk = { version = "0.8.0", package = "gtk4", features = ["v4_4"] }
num_enum = "0.7.2"
once_cell = "1.19.0"
pcap-file = "2.0.0"
Expand All @@ -55,6 +55,9 @@ usb-ids = "1.2024.4"
dark-light = "1.1.1"
hidreport = "0.4.1"
hut = "0.2.1"
byteorder_slice = "3.0.0"
merge = "0.1.0"
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }

[dev-dependencies]
serde = { version = "1.0.196", features = ["derive"] }
Expand Down
31 changes: 29 additions & 2 deletions src/backend/cynthion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::cmp::Ordering;
use std::collections::VecDeque;
use std::num::NonZeroU32;
use std::time::Duration;
use std::sync::mpsc;

Expand All @@ -27,6 +28,8 @@ use super::{
TransferQueue,
};

use crate::capture::CaptureMetadata;

pub const VID_PID: (u16, u16) = (0x1d50, 0x615b);
const CLASS: u8 = 0xff;
const SUBCLASS: u8 = 0x10;
Expand Down Expand Up @@ -80,12 +83,14 @@ pub struct CynthionDevice {
interface_number: u8,
alt_setting_number: u8,
speeds: Vec<Speed>,
metadata: CaptureMetadata,
}

/// A handle to an open Cynthion device.
#[derive(Clone)]
pub struct CynthionHandle {
interface: Interface,
metadata: CaptureMetadata,
}

/// Converts from received data bytes to timestamped packets.
Expand Down Expand Up @@ -161,8 +166,22 @@ impl CynthionDevice {
.context("Failed to select alternate setting")?;
}

let metadata = CaptureMetadata {
iface_desc: Some("Cynthion USB Analyzer".to_string()),
iface_hardware: Some({
let bcd = device_info.device_version();
let major = bcd >> 8;
let minor = bcd as u8;
format!("Cynthion r{major}.{minor}")
}),
iface_os: Some(
format!("USB Analyzer v{protocol}")),
iface_snaplen: Some(NonZeroU32::new(0xFFFF).unwrap()),
.. Default::default()
};

// Fetch the available speeds.
let handle = CynthionHandle { interface };
let handle = CynthionHandle { interface, metadata };
let speeds = handle
.speeds()
.context("Failed to fetch available speeds")?;
Expand All @@ -174,6 +193,7 @@ impl CynthionDevice {
interface_number,
alt_setting_number,
speeds,
metadata: handle.metadata,
}
)
}
Expand All @@ -189,7 +209,10 @@ impl CynthionDevice {
if self.alt_setting_number != 0 {
interface.set_alt_setting(self.alt_setting_number)?;
}
Ok(CynthionHandle { interface })
Ok(CynthionHandle {
interface,
metadata: self.metadata.clone()
})
}
}

Expand All @@ -204,6 +227,10 @@ impl BackendDevice for CynthionDevice {
}

impl BackendHandle for CynthionHandle {
fn metadata(&self) -> &CaptureMetadata {
&self.metadata
}

fn begin_capture(
&mut self,
speed: Speed,
Expand Down
12 changes: 11 additions & 1 deletion src/backend/ice40usbtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use nusb::{
DeviceInfo, Interface,
};

use crate::capture::CaptureMetadata;
use crate::usb::crc5;

use super::{
Expand Down Expand Up @@ -50,6 +51,7 @@ pub struct Ice40UsbtraceDevice {
#[derive(Clone)]
pub struct Ice40UsbtraceHandle {
interface: Interface,
metadata: CaptureMetadata,
}

/// Converts from received data bytes to timestamped packets.
Expand Down Expand Up @@ -84,7 +86,11 @@ impl BackendDevice for Ice40UsbtraceDevice {
fn open_as_generic(&self) -> Result<Box<dyn BackendHandle>, Error> {
let device = self.device_info.open()?;
let interface = device.claim_interface(INTERFACE)?;
Ok(Box::new(Ice40UsbtraceHandle { interface }))
let metadata = CaptureMetadata {
iface_desc: Some("iCE40-usbtrace".to_string()),
.. Default::default()
};
Ok(Box::new(Ice40UsbtraceHandle { interface, metadata }))
}

fn supported_speeds(&self) -> &[Speed] {
Expand All @@ -93,6 +99,10 @@ impl BackendDevice for Ice40UsbtraceDevice {
}

impl BackendHandle for Ice40UsbtraceHandle {
fn metadata(&self) -> &CaptureMetadata {
&self.metadata
}

fn begin_capture(
&mut self,
speed: Speed,
Expand Down
30 changes: 5 additions & 25 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use anyhow::{Context, Error};
use futures_channel::oneshot;
use futures_lite::future::block_on;
use nusb::{self, DeviceInfo};
use num_enum::{FromPrimitive, IntoPrimitive};
use once_cell::sync::Lazy;

use crate::capture::CaptureMetadata;
use crate::util::handle_thread_panic;
pub use crate::usb::Speed;

pub mod cynthion;
pub mod ice40usbtrace;
Expand Down Expand Up @@ -67,30 +68,6 @@ pub trait BackendDevice {
fn supported_speeds(&self) -> &[Speed];
}

/// Possible capture speed settings.
#[derive(Debug, Copy, Clone, PartialEq, FromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum Speed {
#[default]
High = 0,
Full = 1,
Low = 2,
Auto = 3,
}

impl Speed {
/// How this speed setting should be displayed in the UI.
pub fn description(&self) -> &'static str {
use Speed::*;
match self {
Auto => "Auto",
High => "High (480Mbps)",
Full => "Full (12Mbps)",
Low => "Low (1.5Mbps)",
}
}
}

/// A timestamped packet.
pub struct TimestampedPacket {
pub timestamp_ns: u64,
Expand All @@ -109,6 +86,9 @@ pub trait PacketIterator: Iterator<Item=PacketResult> + Send {}
/// A handle to an open capture device.
pub trait BackendHandle: Send + Sync {

/// Get metadata about the capture device.
fn metadata(&self) -> &CaptureMetadata;

/// Begin capture.
///
/// This method should send whatever control requests etc are necessary to
Expand Down
Loading
Loading