From bb1477bf8c610343d203098f85869e88ee9bb149 Mon Sep 17 00:00:00 2001 From: Jackson Coxson Date: Wed, 29 Jan 2025 09:06:25 -0700 Subject: [PATCH] Remove USB impl --- src/devices.rs | 31 ------------------ src/main.rs | 16 --------- src/mdns.rs | 4 +-- src/usb.rs | 88 -------------------------------------------------- 4 files changed, 2 insertions(+), 137 deletions(-) delete mode 100644 src/usb.rs diff --git a/src/devices.rs b/src/devices.rs index cfc95f1..2570294 100644 --- a/src/devices.rs +++ b/src/devices.rs @@ -119,28 +119,6 @@ impl SharedDevices { self.devices.values().find(|x| x.device_id == id) } - #[cfg(feature = "usb")] - pub fn add_usb_device(&mut self, udid: String, _data: Arc>) { - self.last_index += 1; - self.last_interface_index += 1; - - let dev = MuxerDevice { - connection_type: "USB".to_string(), - device_id: self.last_index, - service_name: None, - interface_index: self.last_interface_index, - network_address: None, - serial_number: udid, - heartbeat_handle: None, - connection_speed: None, - location_id: None, - product_id: None, - }; - - info!("Adding device: {:?}", dev.serial_number); - self.devices.insert(dev.serial_number.clone(), dev); - } - pub fn remove_device(&mut self, udid: &String) { if !self.devices.contains_key(udid) { warn!("Device isn't in the muxer, skipping"); @@ -286,15 +264,6 @@ impl SharedDevices { trace!("No UDID found after a re-cache"); Err(()) } - - #[cfg(feature = "usb")] - pub fn check_udid(&mut self, udid: String) -> bool { - if self.paired_udids.contains(&udid) { - return true; - } - self.update_cache(); - self.paired_udids.contains(&udid) - } } impl From<&MuxerDevice> for plist::Dictionary { diff --git a/src/main.rs b/src/main.rs index 231f04e..74b4bc5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,6 @@ mod devices; mod heartbeat; mod mdns; mod raw_packet; -#[cfg(feature = "usb")] -mod usb; #[tokio::main] async fn main() { @@ -38,8 +36,6 @@ async fn main() { let mut use_unix = true; let mut use_mdns = true; - #[cfg(feature = "usb")] - let mut use_usb = false; // Loop through args let mut i = 0; @@ -66,11 +62,6 @@ async fn main() { use_mdns = false; i += 1; } - #[cfg(feature = "usb")] - "--enable-usb" => { - use_usb = true; - i += 1; - } "--disable-heartbeat" => { use_heartbeat = false; i += 1; @@ -111,8 +102,6 @@ async fn main() { )); info!("Created new central data"); let data_clone = data.clone(); - #[cfg(feature = "usb")] - let usb_data = data.clone(); if let Some(host) = host.clone() { let tcp_data = data.clone(); @@ -169,11 +158,6 @@ async fn main() { }); } - #[cfg(feature = "usb")] - if use_usb { - usb::start_listener(usb_data); - } - if use_mdns { let local = tokio::task::LocalSet::new(); local.spawn_local(async move { diff --git a/src/mdns.rs b/src/mdns.rs index 15cbfaf..13722dd 100644 --- a/src/mdns.rs +++ b/src/mdns.rs @@ -47,7 +47,7 @@ pub async fn discover(data: Arc>) { let mac_addr = name.split("@").collect::>()[0]; let mut lock = data.lock().await; - if let Ok(udid) = lock.get_udid_from_mac(mac_addr.to_string()) { + if let Ok(udid) = lock.get_udid_from_mac(mac_addr.to_string()).await { if lock.devices.contains_key(&udid) { info!("Device has already been added to muxer, skipping"); continue; @@ -101,7 +101,7 @@ pub async fn discover(data: Arc>) { } let mac_addr = mac_addr.unwrap(); let mut lock = data.lock().await; - if let Ok(udid) = lock.get_udid_from_mac(mac_addr.to_string()) { + if let Ok(udid) = lock.get_udid_from_mac(mac_addr.to_string()).await { if lock.devices.contains_key(&udid) { info!("Device has already been added to muxer, skipping"); continue; diff --git a/src/usb.rs b/src/usb.rs deleted file mode 100644 index f208ffd..0000000 --- a/src/usb.rs +++ /dev/null @@ -1,88 +0,0 @@ -// jkcoxson - -use log::{error, info, warn}; -use rusb::UsbContext; -use std::sync::Arc; -use tokio::sync::Mutex; - -use crate::devices::SharedDevices; - -const APPLE_VENDOR_ID: u16 = 0x05ac; - -pub fn start_listener(data: Arc>) { - let context = rusb::Context::new().unwrap(); - let reg: Result, rusb::Error> = rusb::HotplugBuilder::new() - .enumerate(true) - .register(&context, Box::new(Handler { data })); - - tokio::task::spawn_blocking(move || { - let _reg = Some(reg.unwrap()); - loop { - match context.handle_events(None) { - Ok(_) => {} - Err(e) => { - error!("Error handling USB events: {:?}", e); - break; - } - } - } - }); -} - -struct Handler { - #[allow(dead_code)] - data: Arc>, -} - -impl rusb::Hotplug for Handler { - fn device_arrived(&mut self, device: rusb::Device) { - let desc = device.device_descriptor().unwrap(); - if desc.vendor_id() == APPLE_VENDOR_ID { - info!("iDevice plugged in!"); - let handle = device.open().unwrap(); - - // Get the device's serial number - let langs = handle - .read_languages(std::time::Duration::from_secs(3)) - .unwrap(); - let serial_number = handle - .read_serial_number_string(langs[0], &desc, std::time::Duration::from_secs(1)) - .unwrap(); - - let serial_number = match serial_number.len() { - 0x28 => { - let mut s = serial_number[..8].to_string(); - s.push('-'); - s.push_str(&serial_number[8..]); - s - } - _ => { - warn!("Serial number is unexpected length: {}", serial_number); - return; - } - }; - - let serial_number = serial_number.trim().to_string(); - let serial_number = serial_number.replace('\0', ""); - - // Determine if the device is paired - let data = self.data.clone(); - tokio::spawn(async move { - let cloned_data = data.clone(); - let mut d = cloned_data.lock().await; - if d.check_udid(serial_number.clone()) { - d.add_usb_device(serial_number, data); - } else { - todo!() - // Try and pair the device - } - }); - } - } - - fn device_left(&mut self, _: rusb::Device) { - println!("Device removed"); - - // Remove device by serial number - } -}