Skip to content

Commit

Permalink
enhanced
Browse files Browse the repository at this point in the history
  • Loading branch information
lattice0 committed Aug 24, 2024
1 parent 5722a11 commit 8cc1787
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/devices/cc253x.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
devices::devices::{find_supported_device, UsbDeviceInfo}, SniffError, UsbDataHeader, UsbHeader, UsbTickHeader
devices::{find_supported_device, UsbDeviceInfo}, SniffError, UsbDataHeader, UsbHeader, UsbTickHeader
};

pub struct CC253X {
Expand Down Expand Up @@ -34,6 +34,7 @@ impl CC253X {
}
}

#[allow(clippy::type_complexity)]
pub fn blocking_sniff(
&mut self,
on_packet: &dyn Fn(&[u8]) -> Result<(), SniffError>,
Expand Down
48 changes: 0 additions & 48 deletions src/devices/devices.rs

This file was deleted.

51 changes: 49 additions & 2 deletions src/devices/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,49 @@
pub mod devices;
pub mod cc253x;
pub mod cc253x;
use rusb::{Device, DeviceList, Error as RusbError, GlobalContext};

pub struct UsbDeviceInfo {
pub device: Option<Device<GlobalContext>>,
pub product_name: &'static str,
pub manufacturer: &'static str,
pub product_id: u16,
pub vendor_id: u16,
}

// model, manufacturer, vendor_id, product_id
pub const SUPPORTED_DEVICES: [(&str, &str, u16, u16); 1] =
[("CC2531 Dongle", "Texas Instruments", 0x0451, 0x16ae)];

fn supported_device_match(
device: &Device<GlobalContext>,
) -> Result<Option<UsbDeviceInfo>, RusbError> {
let descriptor = device.device_descriptor()?;
let product_id = descriptor.product_id();
let vendor_id = descriptor.vendor_id();
Ok(SUPPORTED_DEVICES
.iter()
.find(|(_, _, vid, pid)| product_id == *pid && vendor_id == *vid)
.map(|d| UsbDeviceInfo {
device: None,
product_name: d.0,
manufacturer: d.1,
vendor_id: d.2,
product_id: d.3,
}))
}

pub fn find_supported_device() -> Result<Option<UsbDeviceInfo>, RusbError> {
let device_list = DeviceList::new()?;
for device in device_list.iter() {
if let Some(d) = supported_device_match(&device)? {
return Ok(Some(UsbDeviceInfo {
device: Some(device),
product_name: d.product_name,
manufacturer: d.manufacturer,
vendor_id: d.vendor_id,
product_id: d.product_id,
}));
}
}
Ok(None)
}

0 comments on commit 8cc1787

Please sign in to comment.