diff --git a/Cargo.toml b/Cargo.toml index 5587ca2..bc5405a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,10 @@ authors = ["G2 "] license = "AGPL-3.0" [dependencies] +futures = "0.3.30" hex = "0.4.3" nusb = "0.1.4" +tokio = { version = "1.35.1", features = ["full"] } translit = "0.5.0" [dependencies.minidisc-rs] diff --git a/minidisc-rs/src/netmd/base.rs b/minidisc-rs/src/netmd/base.rs index 3a4de06..3f05e02 100644 --- a/minidisc-rs/src/netmd/base.rs +++ b/minidisc-rs/src/netmd/base.rs @@ -4,13 +4,10 @@ use std::error::Error; use std::time::Duration; // USB stuff -use nusb::transfer::{Control, ControlType, Recipient, RequestBuffer}; +use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer}; use nusb::{Device, DeviceInfo, Interface}; -use futures_lite::future::block_on; - const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0); - const BULK_WRITE_ENDPOINT: u8 = 0x02; const BULK_READ_ENDPOINT: u8 = 0x81; @@ -98,7 +95,7 @@ impl NetMD { const READ_REPLY_RETRY_INTERVAL: u32 = 10; /// Creates a new interface to a NetMD device - pub fn new(device_info: &DeviceInfo) -> Result> { + pub async fn new(device_info: &DeviceInfo) -> Result> { let mut model = DeviceId { vendor_id: device_info.vendor_id(), product_id: device_info.product_id(), @@ -131,61 +128,67 @@ impl NetMD { } /// Gets the device name, this is limited to the devices in the list - pub fn device_name(&self) -> &Option { + pub async fn device_name(&self) -> &Option { &self.model.name } /// Gets the vendor id - pub fn vendor_id(&self) -> &u16 { + pub async fn vendor_id(&self) -> &u16 { &self.model.vendor_id } /// Gets the product id - pub fn product_id(&self) -> &u16 { + pub async fn product_id(&self) -> &u16 { &self.model.product_id } /// Poll the device to get either the result /// of the previous command, or the status - pub fn poll(&mut self) -> Result<(u16, [u8; 4]), Box> { + pub async fn poll(&mut self) -> Result<(u16, [u8; 4]), Box> { // Create an array to store the result of the poll - let mut poll_result = [0u8; 4]; - - let _status = match self.usb_interface.control_in_blocking( - Control { + let poll_result = match self + .usb_interface + .control_in(ControlIn { control_type: ControlType::Vendor, recipient: Recipient::Interface, request: 0x01, value: 0, index: 0, - }, - &mut poll_result, - DEFAULT_TIMEOUT, - ) { + length: 4, + }) + .await + .into_result() + { Ok(size) => size, Err(error) => return Err(error.into()), }; - let length_bytes = [poll_result[2], poll_result[3]]; - Ok((u16::from_le_bytes(length_bytes), poll_result)) + let length_bytes = u16::from_le_bytes([poll_result[2], poll_result[3]]); + + let poll_result: [u8; 4] = match poll_result.try_into() { + Ok(val) => val, + Err(_) => return Err("could not convert result".into()), + }; + + Ok((length_bytes, poll_result)) } - pub fn send_command(&mut self, command: Vec) -> Result<(), Box> { - self._send_command(command, false) + pub async fn send_command(&mut self, command: Vec) -> Result<(), Box> { + self._send_command(command, false).await } - pub fn send_factory_command(&mut self, command: Vec) -> Result<(), Box> { - self._send_command(command, true) + pub async fn send_factory_command(&mut self, command: Vec) -> Result<(), Box> { + self._send_command(command, true).await } /// Send a control message to the device - fn _send_command( + async fn _send_command( &mut self, command: Vec, use_factory_command: bool, ) -> Result<(), Box> { // First poll to ensure the device is ready - match self.poll() { + match self.poll().await { Ok(buffer) => match buffer.1[2] { 0 => 0, _ => return Err("Device not ready!".into()), @@ -198,49 +201,54 @@ impl NetMD { true => 0xff, }; - match self.usb_interface.control_out_blocking( - Control { + match self + .usb_interface + .control_out(ControlOut { control_type: ControlType::Vendor, recipient: Recipient::Interface, request, value: 0, index: 0, - }, - &command, - DEFAULT_TIMEOUT, - ) { + data: &command, + }) + .await + .into_result() + { Ok(_) => Ok(()), Err(error) => Err(error.into()), } } - pub fn read_reply(&mut self, override_length: Option) -> Result, Box> { - self._read_reply(false, override_length) - } - - pub fn read_factory_reply( + pub async fn read_reply( &mut self, override_length: Option, ) -> Result, Box> { - self._read_reply(true, override_length) + self._read_reply(false, override_length).await } - /// Poll to see if a message is ready, - /// and if so, recieve it - fn _read_reply( + pub async fn read_factory_reply( + &mut self, + override_length: Option, + ) -> Result, Box> { + self._read_reply(true, override_length).await + } + + /// Poll to see if a message is ready, and once it is, retrieve it + async fn _read_reply( &mut self, use_factory_command: bool, override_length: Option, ) -> Result, Box> { - let mut length = self.poll()?.0; + let mut length = self.poll().await?.0; let mut current_attempt = 0; while length == 0 { + // Back off while trying again let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64 * (u64::pow(2, current_attempt as u32 / 10) - 1); std::thread::sleep(std::time::Duration::from_millis(sleep_time)); - length = self.poll()?.0; + length = self.poll().await?.0; current_attempt += 1; } @@ -254,38 +262,35 @@ impl NetMD { }; // Create a buffer to fill with the result - let mut buf: Vec = vec![0; length as usize]; - - // Create a buffer to fill with the result - match self.usb_interface.control_in_blocking( - Control { + let reply = self + .usb_interface + .control_in(ControlIn { control_type: ControlType::Vendor, recipient: Recipient::Interface, request, value: 0, index: 0, - }, - &mut buf, - DEFAULT_TIMEOUT, - ) { - Ok(_) => Ok(buf), - Err(error) => Err(error.into()), - } + length, + }) + .await + .into_result()?; + + Ok(reply) } // Default chunksize should be 0x10000 // TODO: Make these Async eventually - pub fn read_bulk( + pub async fn read_bulk( &mut self, length: usize, chunksize: usize, ) -> Result, Box> { - let result = self.read_bulk_to_array(length, chunksize)?; + let result = self.read_bulk_to_array(length, chunksize).await?; Ok(result) } - pub fn read_bulk_to_array( + pub async fn read_bulk_to_array( &mut self, length: usize, chunksize: usize, @@ -298,7 +303,10 @@ impl NetMD { done -= to_read; let buffer = RequestBuffer::new(to_read); - let res = match block_on(self.usb_interface.bulk_in(BULK_READ_ENDPOINT, buffer)) + let res = match self + .usb_interface + .bulk_in(BULK_READ_ENDPOINT, buffer) + .await .into_result() { Ok(result) => result, @@ -311,11 +319,12 @@ impl NetMD { Ok(final_result) } - pub fn write_bulk(&mut self, data: Vec) -> Result> { - Ok( - block_on(self.usb_interface.bulk_out(BULK_WRITE_ENDPOINT, data)) - .into_result()? - .actual_length(), - ) + pub async fn write_bulk(&mut self, data: Vec) -> Result> { + Ok(self + .usb_interface + .bulk_out(BULK_WRITE_ENDPOINT, data) + .await + .into_result()? + .actual_length()) } } diff --git a/minidisc-rs/src/netmd/interface.rs b/minidisc-rs/src/netmd/interface.rs index 80e8e28..7e69888 100644 --- a/minidisc-rs/src/netmd/interface.rs +++ b/minidisc-rs/src/netmd/interface.rs @@ -203,8 +203,8 @@ impl NetMDInterface { const MAX_INTERIM_READ_ATTEMPTS: u8 = 4; const INTERIM_RESPONSE_RETRY_INTERVAL: u32 = 100; - pub fn new(device: &nusb::DeviceInfo) -> Result> { - let net_md_device = base::NetMD::new(device)?; + pub async fn new(device: &nusb::DeviceInfo) -> Result> { + let net_md_device = base::NetMD::new(device).await?; Ok(NetMDInterface { net_md_device }) } @@ -219,7 +219,7 @@ impl NetMDInterface { } // TODO: Finish proper implementation - fn disc_subunit_identifier(&mut self) -> Result> { + async fn disc_subunit_identifier(&mut self) -> Result> { self.change_descriptor_state( &Descriptor::DiscSubunitIdentifier, &DescriptorAction::OpenRead, @@ -227,7 +227,7 @@ impl NetMDInterface { let mut query = format_query("1809 00 ff00 0000 0000".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query( reply, @@ -315,8 +315,8 @@ impl NetMDInterface { } */ - fn net_md_level(&mut self) -> Result> { - let result = self.disc_subunit_identifier()?; + async fn net_md_level(&mut self) -> Result> { + let result = self.disc_subunit_identifier().await?; Ok(result) } @@ -334,20 +334,24 @@ impl NetMDInterface { } /// Send a query to the NetMD player - fn send_query( + async fn send_query( &mut self, query: &mut Vec, test: bool, accept_interim: bool, ) -> Result, Box> { - self.send_command(query, test)?; + self.send_command(query, test).await?; - let result = self.read_reply(accept_interim)?; + let result = self.read_reply(accept_interim).await?; Ok(result) } - fn send_command(&mut self, query: &mut Vec, test: bool) -> Result<(), Box> { + async fn send_command( + &mut self, + query: &mut Vec, + test: bool, + ) -> Result<(), Box> { let status_byte = match test { true => Status::GeneralInquiry, false => Status::Control, @@ -358,17 +362,17 @@ impl NetMDInterface { new_query.push(status_byte as u8); new_query.append(query); - self.net_md_device.send_command(new_query)?; + self.net_md_device.send_command(new_query).await?; Ok(()) } - fn read_reply(&mut self, accept_interim: bool) -> Result, Box> { + async fn read_reply(&mut self, accept_interim: bool) -> Result, Box> { let mut current_attempt = 0; let mut data; while current_attempt < Self::MAX_INTERIM_READ_ATTEMPTS { - data = match self.net_md_device.read_reply(None) { + data = match self.net_md_device.read_reply(None).await { Ok(reply) => reply, Err(error) => return Err(error), }; @@ -403,66 +407,66 @@ impl NetMDInterface { Err("The max retries is set to 0".into()) } - fn playback_control(&mut self, action: Action) -> Result<(), Box> { + async fn playback_control(&mut self, action: Action) -> Result<(), Box> { let mut query = format_query( "18c3 00 %b 000000".to_string(), vec![QueryValue::Number(action as i64)], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "18c3 00 %b 000000".to_string())?; Ok(()) } - pub fn play(&mut self) -> Result<(), Box> { - self.playback_control(Action::Play) + pub async fn play(&mut self) -> Result<(), Box> { + self.playback_control(Action::Play).await } - pub fn fast_forward(&mut self) -> Result<(), Box> { - self.playback_control(Action::FastForward) + pub async fn fast_forward(&mut self) -> Result<(), Box> { + self.playback_control(Action::FastForward).await } - pub fn rewind(&mut self) -> Result<(), Box> { - self.playback_control(Action::Rewind) + pub async fn rewind(&mut self) -> Result<(), Box> { + self.playback_control(Action::Rewind).await } - pub fn pause(&mut self) -> Result<(), Box> { - self.playback_control(Action::Pause) + pub async fn pause(&mut self) -> Result<(), Box> { + self.playback_control(Action::Pause).await } //TODO: Implement fix for LAM-1 - pub fn stop(&mut self) -> Result<(), Box> { + pub async fn stop(&mut self) -> Result<(), Box> { let mut query = format_query("18c5 ff 00000000".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "18c5 00 00000000".to_string())?; Ok(()) } - pub fn acquire(&mut self) -> Result<(), Box> { + async fn acquire(&mut self) -> Result<(), Box> { let mut query = format_query("ff 010c ffff ffff ffff ffff ffff ffff".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "ff 010c ffff ffff ffff ffff ffff ffff".to_string())?; Ok(()) } - pub fn release(&mut self) -> Result<(), Box> { + async fn release(&mut self) -> Result<(), Box> { let mut query = format_query("ff 0100 ffff ffff ffff ffff ffff ffff".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "ff 0100 ffff ffff ffff ffff ffff ffff".to_string())?; Ok(()) } - pub fn status(&mut self) -> Result, Box> { + pub async fn status(&mut self) -> Result, Box> { self.change_descriptor_state( &Descriptor::OperatingStatusBlock, &DescriptorAction::OpenRead, @@ -473,7 +477,7 @@ impl NetMDInterface { vec![], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query( reply, @@ -487,15 +491,15 @@ impl NetMDInterface { Ok(final_array) } - pub fn disc_present(&mut self) -> Result> { - let status = self.status()?; + pub async fn disc_present(&mut self) -> Result> { + let status = self.status().await?; println!("{:X?}", status); Ok(status[4] == 0x40) } - fn full_operating_status(&mut self) -> Result<(u8, u16), Box> { + async fn full_operating_status(&mut self) -> Result<(u8, u16), Box> { // WARNING: Does not work for all devices. See https://github.com/cybercase/webminidisc/issues/21 self.change_descriptor_state( &Descriptor::OperatingStatusBlock, @@ -506,7 +510,7 @@ impl NetMDInterface { vec![], ) .unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let result = scan_query( reply, @@ -528,13 +532,13 @@ impl NetMDInterface { Ok((status_mode, operating_status_number)) } - pub fn operating_status(&mut self) -> Result> { - let status = self.full_operating_status()?.1; + pub async fn operating_status(&mut self) -> Result> { + let status = self.full_operating_status().await?.1; Ok(status) } - fn playback_status_query(&mut self, p1: u32, p2: u32) -> Result, Box> { + async fn playback_status_query(&mut self, p1: u32, p2: u32) -> Result, Box> { self.change_descriptor_state( &Descriptor::OperatingStatusBlock, &DescriptorAction::OpenRead, @@ -545,7 +549,7 @@ impl NetMDInterface { ) .unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query( reply, @@ -557,15 +561,15 @@ impl NetMDInterface { Ok(res.unwrap()[0].to_vec().unwrap()) } - pub fn playback_status1(&mut self) -> Result, Box> { - self.playback_status_query(0x8801, 0x8807) + pub async fn playback_status1(&mut self) -> Result, Box> { + self.playback_status_query(0x8801, 0x8807).await } - pub fn playback_status2(&mut self) -> Result, Box> { - self.playback_status_query(0x8802, 0x8806) + pub async fn playback_status2(&mut self) -> Result, Box> { + self.playback_status_query(0x8802, 0x8806).await } - pub fn position(&mut self) -> Result<[u16; 5], Box> { + pub async fn position(&mut self) -> Result<[u16; 5], Box> { self.change_descriptor_state( &Descriptor::OperatingStatusBlock, &DescriptorAction::OpenRead, @@ -577,7 +581,7 @@ impl NetMDInterface { ) .unwrap(); - let reply = match self.send_query(&mut query, false, false) { + let reply = match self.send_query(&mut query, false, false).await { Ok(result) => result, Err(e) if e.to_string() == "Rejected" => Vec::new(), Err(e) => return Err(e), @@ -598,31 +602,31 @@ impl NetMDInterface { Ok(final_result) } - pub fn eject_disc(&mut self) -> Result<(), Box> { + pub async fn eject_disc(&mut self) -> Result<(), Box> { let mut query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap(); - let _reply = self.send_query(&mut query, false, false)?; + let _reply = self.send_query(&mut query, false, false).await?; Ok(()) } - pub fn can_eject_disc(&mut self) -> Result> { + pub async fn can_eject_disc(&mut self) -> Result> { let mut query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap(); - match self.send_query(&mut query, true, false) { + match self.send_query(&mut query, true, false).await { Ok(_) => Ok(true), Err(error) => Err(error), } } /* Track control */ - pub fn go_to_track(&mut self, track_number: u16) -> Result> { + pub async fn go_to_track(&mut self, track_number: u16) -> Result> { let mut query = format_query( "1850 ff010000 0000 %w".to_string(), vec![QueryValue::Number(track_number as i64)], ) .unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query(reply, "1850 00010000 0000 %w".to_string())?; @@ -631,7 +635,7 @@ impl NetMDInterface { Ok(value as u16) } - pub fn go_to_time( + pub async fn go_to_time( &mut self, track_number: u16, hour: u8, @@ -651,7 +655,7 @@ impl NetMDInterface { ) .unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query(reply, "1850 00000000 %?%? %w %B%B%B%B".to_string())?; @@ -660,14 +664,14 @@ impl NetMDInterface { Ok(value as u16) } - fn _track_change(&mut self, direction: Track) -> Result<(), Box> { + async fn track_change(&mut self, direction: Track) -> Result<(), Box> { let mut query = format_query( "1850 ff10 00000000 %w".to_string(), vec![QueryValue::Number(direction as i64)], ) .unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "1850 0010 00000000 %?%?".to_string())?; @@ -675,35 +679,35 @@ impl NetMDInterface { } /// Change to the next track (skip forward) - pub fn next_track(&mut self) -> Result<(), Box> { - self._track_change(Track::Next) + pub async fn next_track(&mut self) -> Result<(), Box> { + self.track_change(Track::Next).await } /// Change to the next track (skip back) - pub fn previous_track(&mut self) -> Result<(), Box> { - self._track_change(Track::Next) + pub async fn previous_track(&mut self) -> Result<(), Box> { + self.track_change(Track::Next).await } /// Change to the next track (skip to beginning of track) - pub fn restart_track(&mut self) -> Result<(), Box> { - self._track_change(Track::Next) + pub async fn restart_track(&mut self) -> Result<(), Box> { + self.track_change(Track::Next).await } /* Content access and control */ - pub fn erase_disc(&mut self) -> Result<(), Box> { + pub async fn erase_disc(&mut self) -> Result<(), Box> { let mut query = format_query("1840 ff 0000".to_string(), vec![]).unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "1840 00 0000".to_string())?; Ok(()) } // TODO: Ensure this is returning the correct value, it // looks like it actually might be a 16 bit integer - pub fn disc_flags(&mut self) -> Result> { + pub async fn disc_flags(&mut self) -> Result> { self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead); let mut query = format_query("1806 01101000 ff00 0001000b".to_string(), vec![]).unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query(reply, "1806 01101000 1000 0001000b %b".to_string()).unwrap(); @@ -713,13 +717,13 @@ impl NetMDInterface { } /// The number of tracks on the disc - pub fn track_count(&mut self) -> Result> { + pub async fn track_count(&mut self) -> Result> { self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); let mut query = format_query("1806 02101001 3000 1000 ff00 00000000".to_string(), vec![]).unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query( reply, @@ -732,7 +736,7 @@ impl NetMDInterface { Ok(res[0].to_i64().unwrap() as u16) } - fn _disc_title(&mut self, wchar: bool) -> Result> { + async fn raw_disc_title(&mut self, wchar: bool) -> Result> { self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead); @@ -759,7 +763,7 @@ impl NetMDInterface { ) .unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; if remaining == 0 { let res = scan_query( @@ -795,8 +799,8 @@ impl NetMDInterface { } /// Gets the disc title - pub fn disc_title(&mut self, wchar: bool) -> Result> { - let mut title = self._disc_title(wchar)?; + pub async fn disc_title(&mut self, wchar: bool) -> Result> { + let mut title = self.raw_disc_title(wchar).await?; let delim = match wchar { true => "//", @@ -821,16 +825,16 @@ impl NetMDInterface { } /// Gets all groups on the disc - pub fn track_group_list( + pub async fn track_group_list( &mut self, ) -> Result, Option, Vec)>, Box> { - let raw_title = self._disc_title(false)?; + let raw_title = self.raw_disc_title(false).await?; let group_list = raw_title.split("//"); let mut track_dict: HashMap = HashMap::new(); - let track_count = self.track_count()?; + let track_count = self.track_count().await?; let mut result: Vec<(Option, Option, Vec)> = Vec::new(); - let raw_full_title = self._disc_title(true)?; + let raw_full_title = self.raw_disc_title(true).await?; let mut full_width_group_list = raw_full_title.split("//"); @@ -907,7 +911,7 @@ impl NetMDInterface { } /// Gets a list of track titles from a set - pub fn track_titles( + pub async fn track_titles( &mut self, tracks: Vec, wchar: bool, @@ -935,7 +939,7 @@ impl NetMDInterface { ) .unwrap(); - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query( reply, @@ -956,9 +960,9 @@ impl NetMDInterface { Ok(track_titles) } - /// Gets the title of a track at an index - pub fn track_title(&mut self, track: u16, wchar: bool) -> Result> { - let title = match self.track_titles([track].into(), wchar) { + /// Gets the title of a single track at an index + pub async fn track_title(&mut self, track: u16, wchar: bool) -> Result> { + let title = match self.track_titles([track].into(), wchar).await { Ok(titles) => titles[0].clone(), Err(error) if error.to_string() == "Rejected" => String::new(), Err(error) => return Err(error), @@ -967,10 +971,14 @@ impl NetMDInterface { } // Sets the title of the disc - pub fn set_disc_title(&mut self, title: String, wchar: bool) -> Result<(), Box> { - let current_title = self._disc_title(wchar)?; + pub async fn set_disc_title( + &mut self, + title: String, + wchar: bool, + ) -> Result<(), Box> { + let current_title = self.raw_disc_title(wchar).await?; if current_title == title { - return Ok(()); + return Err("Title is already the same".into()); } let new_title: Vec; @@ -989,7 +997,7 @@ impl NetMDInterface { let new_len = new_title.len(); - if self.net_md_device.vendor_id() == &0x04dd { + if self.net_md_device.vendor_id().await == &0x04dd { self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::OpenWrite) } else { self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close); @@ -1008,7 +1016,7 @@ impl NetMDInterface { let _ = self.send_query(&mut query, false, false); - if self.net_md_device.vendor_id() == &0x04dd { + if self.net_md_device.vendor_id().await == &0x04dd { self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::Close) } else { self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close); @@ -1020,7 +1028,7 @@ impl NetMDInterface { } /// Sets the title of a track - pub fn set_track_title( + pub async fn set_track_title( &mut self, track: u16, title: String, @@ -1040,7 +1048,7 @@ impl NetMDInterface { let new_len = new_title.len(); - let old_len: u16 = match self.track_title(track, wchar) { + let old_len: u16 = match self.track_title(track, wchar).await { Ok(current_title) => { if title == current_title { return Ok(()); @@ -1062,7 +1070,7 @@ impl NetMDInterface { QueryValue::Array(new_title), ], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let _ = scan_query( reply, @@ -1074,19 +1082,19 @@ impl NetMDInterface { } /// Removes a track from the UTOC - pub fn erase_track(&mut self, track: u16) -> Result<(), Box> { + pub async fn erase_track(&mut self, track: u16) -> Result<(), Box> { let mut query = format_query( "1840 ff01 00 201001 %w".to_string(), vec![QueryValue::Number(track as i64)], )?; - let _ = self.send_query(&mut query, false, false); + let _result = self.send_query(&mut query, false, false).await; Ok(()) } /// Moves a track to another position on the disc - pub fn move_track(&mut self, source: u16, dest: u16) -> Result<(), Box> { + pub async fn move_track(&mut self, source: u16, dest: u16) -> Result<(), Box> { let mut query = format_query( "1843 ff00 00 201001 %w 201001 %w".to_string(), vec![ @@ -1095,12 +1103,17 @@ impl NetMDInterface { ], )?; - let _ = self.send_query(&mut query, false, false); + let _result = self.send_query(&mut query, false, false).await; Ok(()) } - fn _track_info(&mut self, track: u16, p1: i32, p2: i32) -> Result, Box> { + async fn raw_track_info( + &mut self, + track: u16, + p1: i32, + p2: i32, + ) -> Result, Box> { self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); let mut query = format_query( @@ -1112,7 +1125,7 @@ impl NetMDInterface { ], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query( reply, "1806 02201001 %?%? %?%? %?%? 1000 00%?0000 %x".to_string(), @@ -1124,7 +1137,7 @@ impl NetMDInterface { } /// Gets the length of tracks as a `std::time::Duration` from a set - pub fn track_lengths( + pub async fn track_lengths( &mut self, tracks: Vec, ) -> Result, Box> { @@ -1141,7 +1154,7 @@ impl NetMDInterface { ], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query( reply, @@ -1168,13 +1181,16 @@ impl NetMDInterface { } /// Gets the length of a track as a `std::time::Duration` - pub fn track_length(&mut self, track: u16) -> Result> { - Ok(self.track_lengths([track].into())?[0]) + pub async fn track_length( + &mut self, + track: u16, + ) -> Result> { + Ok(self.track_lengths([track].into()).await?[0]) } /// Gets the encoding of a track (SP, LP2, LP4) - pub fn track_encoding(&mut self, track_number: u16) -> Result> { - let raw_value = self._track_info(track_number, 0x3080, 0x0700)?; + pub async fn track_encoding(&mut self, track_number: u16) -> Result> { + let raw_value = self.raw_track_info(track_number, 0x3080, 0x0700).await?; let result = scan_query(raw_value, "07 0004 0110 %b %b".to_string())?; let final_encoding = match result[0].to_i64() { @@ -1189,13 +1205,13 @@ impl NetMDInterface { } /// Gets a track's flags - pub fn track_flags(&mut self, track: u16) -> Result> { + pub async fn track_flags(&mut self, track: u16) -> Result> { self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); let mut query = format_query( "1806 01201001 %w ff00 00010008".to_string(), vec![QueryValue::Number(track as i64)], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query(reply, "1806 01201001 %?%? 10 00 00010008 %b".to_string())?; @@ -1205,10 +1221,10 @@ impl NetMDInterface { } /// Gets the disc capacity as a `std::time::Duration` - pub fn disc_capacity(&mut self) -> Result<[std::time::Duration; 3], Box> { + pub async fn disc_capacity(&mut self) -> Result<[std::time::Duration; 3], Box> { self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead); let mut query = format_query("1806 02101000 3080 0300 ff00 00000000".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let mut result: [std::time::Duration; 3] = [std::time::Duration::from_secs(0); 3]; // 8003 changed to %?03 - Panasonic returns 0803 instead. This byte's meaning is unknown @@ -1234,7 +1250,7 @@ impl NetMDInterface { Ok(result) } - pub fn recording_parameters(&mut self) -> Result, Box> { + pub async fn recording_parameters(&mut self) -> Result, Box> { self.change_descriptor_state( &Descriptor::OperatingStatusBlock, &DescriptorAction::OpenRead, @@ -1244,7 +1260,7 @@ impl NetMDInterface { vec![], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query(reply, "1809 8001 0330 8801 0030 8805 0030 8807 00 1000 000e0000 000c 8805 0008 80e0 0110 %b %b 4000".to_string())?; @@ -1256,7 +1272,7 @@ impl NetMDInterface { /// Gets the bytes of a track /// /// This can only be executed on an MZ-RH1 / M200 - pub fn save_track_to_array( + pub async fn save_track_to_array( &mut self, track: u16, ) -> Result<(DiscFormat, u16, Vec), Box> { @@ -1265,7 +1281,7 @@ impl NetMDInterface { vec![QueryValue::Number((track + 1) as i64)], )?; - let reply = self.send_query(&mut query, false, true)?; + let reply = self.send_query(&mut query, false, true).await?; let res = scan_query( reply, @@ -1276,10 +1292,10 @@ impl NetMDInterface { let codec = res[1].to_i64().unwrap() as u8; let length = res[2].to_i64().unwrap() as usize; - let result = self.net_md_device.read_bulk(length, 0x10000)?; + let result = self.net_md_device.read_bulk(length, 0x10000).await?; scan_query( - self.read_reply(false)?, + self.read_reply(false).await?, "1800 080046 f003010330 0000 1001 %?%? %?%?".to_string(), )?; @@ -1296,50 +1312,51 @@ impl NetMDInterface { Ok((format, frames, result)) } - pub fn disable_new_track_protection(&mut self, val: u16) -> Result<(), Box> { + pub async fn disable_new_track_protection(&mut self, val: u16) -> Result<(), Box> { let mut query = format_query( "1800 080046 f0030103 2b ff %w".to_string(), vec![QueryValue::Number(val as i64)], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "1800 080046 f0030103 2b 00 %?%?".to_string())?; Ok(()) } - pub fn enter_secure_session(&mut self) -> Result<(), Box> { + pub async fn enter_secure_session(&mut self) -> Result<(), Box> { let mut query = format_query("1800 080046 f0030103 80 ff".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "1800 080046 f0030103 80 00".to_string())?; Ok(()) } - pub fn leave_secure_session(&mut self) -> Result<(), Box> { + pub async fn leave_secure_session(&mut self) -> Result<(), Box> { let mut query = format_query("1800 080046 f0030103 81 ff".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "1800 080046 f0030103 81 00".to_string())?; Ok(()) } /** - Read the leaf ID of the present NetMD device. The leaf ID tells - which keys the device posesses, which is needed to find out which - parts of the EKB needs to be sent to the device for it to decrypt - the root key. - The leaf ID is a 8-byte constant - **/ - pub fn leaf_id(&mut self) -> Result, Box> { + * Read the leaf ID of the present NetMD device. The leaf ID tells + * which keys the device posesses, which is needed to find out which + * parts of the EKB needs to be sent to the device for it to decrypt + * the root key. + * + * The leaf ID is a 8-byte constant + **/ + pub async fn leaf_id(&mut self) -> Result, Box> { let mut query = format_query("1800 080046 f0030103 11 ff".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query(reply, "1800 080046 f0030103 11 00 %*".to_string())?; Ok(res[0].to_vec().unwrap()) } - pub fn send_key_data( + pub async fn send_key_data( &mut self, ekbid: i32, keychain: [[u8; 16]; 2], @@ -1371,7 +1388,7 @@ impl NetMDInterface { ], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query( reply, @@ -1381,7 +1398,10 @@ impl NetMDInterface { Ok(res[0].to_vec().unwrap()) } - pub fn session_key_exchange(&mut self, hostnonce: Vec) -> Result, Box> { + pub async fn session_key_exchange( + &mut self, + hostnonce: Vec, + ) -> Result, Box> { if hostnonce.len() != 8 { return Err("Supplied host nonce length wrong".into()); } @@ -1390,23 +1410,23 @@ impl NetMDInterface { vec![QueryValue::Array(hostnonce)], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query(reply, "1800 080046 f0030103 20 %? 000000 %#".to_string())?; Ok(res[0].to_vec().unwrap()) } - pub fn session_key_forget(&mut self) -> Result<(), Box> { + pub async fn session_key_forget(&mut self) -> Result<(), Box> { let mut query = format_query("1800 080046 f0030103 21 ff 000000".to_string(), vec![])?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let _ = scan_query(reply, "1800 080046 f0030103 21 00 000000".to_string())?; Ok(()) } - pub fn setup_download( + pub async fn setup_download( &mut self, contentid: Vec, keyenckey: Vec, @@ -1433,14 +1453,14 @@ impl NetMDInterface { vec![QueryValue::Array(encryptedarg)], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "1800 080046 f0030103 22 00 0000".to_string())?; Ok(()) } - pub fn commit_track( + pub async fn commit_track( &mut self, track_number: u16, hex_session_key: String, @@ -1460,14 +1480,14 @@ impl NetMDInterface { ], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; scan_query(reply, "1800 080046 f0030103 22 00 0000".to_string())?; Ok(()) } - pub fn send_track( + pub async fn send_track( &mut self, wireformat: u8, discformat: u8, @@ -1495,7 +1515,7 @@ impl NetMDInterface { QueryValue::Number(total_bytes as i64), ], )?; - let mut reply = self.send_query(&mut query, false, true)?; + let mut reply = self.send_query(&mut query, false, true).await?; scan_query( reply, "1800 080046 f0030103 28 00 000100 1001 %?%? 00 %*".to_string(), @@ -1512,12 +1532,12 @@ impl NetMDInterface { } else { data.clone() }; - self.net_md_device.write_bulk(binpack)?; + self.net_md_device.write_bulk(binpack).await?; _written_bytes += data.len(); } - reply = self.read_reply(false)?; - self.net_md_device.poll()?; + reply = self.read_reply(false).await?; + self.net_md_device.poll().await?; let res = scan_query( reply, "1800 080046 f0030103 28 00 000100 1001 %w 00 %?%? %?%?%?%? %?%?%?%? %*".to_string(), @@ -1536,21 +1556,21 @@ impl NetMDInterface { Ok((res[0].to_i64().unwrap(), part1, part2)) } - pub fn track_uuid(&mut self, track: u16) -> Result> { + pub async fn track_uuid(&mut self, track: u16) -> Result> { let mut query = format_query( "1800 080046 f0030103 23 ff 1001 %w".to_string(), vec![QueryValue::Number(track as i64)], )?; - let reply = self.send_query(&mut query, false, false)?; + let reply = self.send_query(&mut query, false, false).await?; let res = scan_query(reply, "1800 080046 f0030103 23 00 1001 %?%? %*".to_string())?; Ok(String::from_utf8_lossy(&res[0].to_vec().unwrap()).to_string()) } - pub fn terminate(&mut self) -> Result<(), Box> { + pub async fn terminate(&mut self) -> Result<(), Box> { let mut query = format_query("1800 080046 f0030103 2a ff00".to_string(), vec![])?; - self.send_query(&mut query, false, false)?; + self.send_query(&mut query, false, false).await?; Ok(()) } @@ -1700,4 +1720,4 @@ impl MDSession { // TODO Ok(()) } -} \ No newline at end of file +} diff --git a/minidisc-rs/src/netmd/mappings.rs b/minidisc-rs/src/netmd/mappings.rs index bc06079..6dec72a 100644 --- a/minidisc-rs/src/netmd/mappings.rs +++ b/minidisc-rs/src/netmd/mappings.rs @@ -193,7 +193,6 @@ lazy_static! { ("。".to_string(), "。".to_string()), ("、".to_string(), "、".to_string()) ]); - pub static ref MAPPINGS_RU: HashMap = HashMap::from([ ("а".to_string(), "a".to_string()), ("б".to_string(), "b".to_string()), @@ -262,7 +261,6 @@ lazy_static! { ("Ю".to_string(), "Iu".to_string()), ("Я".to_string(), "Ia".to_string()) ]); - pub static ref MAPPINGS_DE: HashMap = HashMap::from([ ("Ä".to_string(), "Ae".to_string()), ("ä".to_string(), "ae".to_string()), @@ -272,287 +270,285 @@ lazy_static! { ("ü".to_string(), "ue".to_string()), ("ß".to_string(), "ss".to_string()) ]); - pub static ref MAPPINGS_HW: HashMap = HashMap::from([ - ("-".to_string(),"-".to_string()), - ("ー".to_string(),"-".to_string()), - ("ァ".to_string(),"ァ".to_string()), - ("ア".to_string(),"ア".to_string()), - ("ィ".to_string(),"ィ".to_string()), - ("イ".to_string(),"イ".to_string()), - ("ゥ".to_string(),"ゥ".to_string()), - ("ウ".to_string(),"ウ".to_string()), - ("ェ".to_string(),"ェ".to_string()), - ("エ".to_string(),"エ".to_string()), - ("ォ".to_string(),"ォ".to_string()), - ("オ".to_string(),"オ".to_string()), - ("カ".to_string(),"カ".to_string()), - ("ガ".to_string(),"ガ".to_string()), - ("キ".to_string(),"キ".to_string()), - ("ギ".to_string(),"ギ".to_string()), - ("ク".to_string(),"ク".to_string()), - ("グ".to_string(),"グ".to_string()), - ("ケ".to_string(),"ケ".to_string()), - ("ゲ".to_string(),"ゲ".to_string()), - ("コ".to_string(),"コ".to_string()), - ("ゴ".to_string(),"ゴ".to_string()), - ("サ".to_string(),"サ".to_string()), - ("ザ".to_string(),"ザ".to_string()), - ("シ".to_string(),"シ".to_string()), - ("ジ".to_string(),"ジ".to_string()), - ("ス".to_string(),"ス".to_string()), - ("ズ".to_string(),"ズ".to_string()), - ("セ".to_string(),"セ".to_string()), - ("ゼ".to_string(),"ゼ".to_string()), - ("ソ".to_string(),"ソ".to_string()), - ("ゾ".to_string(),"ゾ".to_string()), - ("タ".to_string(),"タ".to_string()), - ("ダ".to_string(),"ダ".to_string()), - ("チ".to_string(),"チ".to_string()), - ("ヂ".to_string(),"ヂ".to_string()), - ("ッ".to_string(),"ッ".to_string()), - ("ツ".to_string(),"ツ".to_string()), - ("ヅ".to_string(),"ヅ".to_string()), - ("テ".to_string(),"テ".to_string()), - ("デ".to_string(),"デ".to_string()), - ("ト".to_string(),"ト".to_string()), - ("ド".to_string(),"ド".to_string()), - ("ナ".to_string(),"ナ".to_string()), - ("ニ".to_string(),"ニ".to_string()), - ("ヌ".to_string(),"ヌ".to_string()), - ("ネ".to_string(),"ネ".to_string()), - ("ノ".to_string(),"ノ".to_string()), - ("ハ".to_string(),"ハ".to_string()), - ("バ".to_string(),"バ".to_string()), - ("パ".to_string(),"パ".to_string()), - ("ヒ".to_string(),"ヒ".to_string()), - ("ビ".to_string(),"ビ".to_string()), - ("ピ".to_string(),"ピ".to_string()), - ("フ".to_string(),"フ".to_string()), - ("ブ".to_string(),"ブ".to_string()), - ("プ".to_string(),"プ".to_string()), - ("ヘ".to_string(),"ヘ".to_string()), - ("ベ".to_string(),"ベ".to_string()), - ("ペ".to_string(),"ペ".to_string()), - ("ホ".to_string(),"ホ".to_string()), - ("ボ".to_string(),"ボ".to_string()), - ("ポ".to_string(),"ポ".to_string()), - ("マ".to_string(),"マ".to_string()), - ("ミ".to_string(),"ミ".to_string()), - ("ム".to_string(),"ム".to_string()), - ("メ".to_string(),"メ".to_string()), - ("モ".to_string(),"モ".to_string()), - ("ャ".to_string(),"ャ".to_string()), - ("ヤ".to_string(),"ヤ".to_string()), - ("ュ".to_string(),"ュ".to_string()), - ("ユ".to_string(),"ユ".to_string()), - ("ョ".to_string(),"ョ".to_string()), - ("ヨ".to_string(),"ヨ".to_string()), - ("ラ".to_string(),"ラ".to_string()), - ("リ".to_string(),"リ".to_string()), - ("ル".to_string(),"ル".to_string()), - ("レ".to_string(),"レ".to_string()), - ("ロ".to_string(),"ロ".to_string()), - ("ワ".to_string(),"ワ".to_string()), - ("ヲ".to_string(),"ヲ".to_string()), - ("ン".to_string(),"ン".to_string()), - ("ー".to_string(),"-".to_string()), - ("ヮ".to_string(),"ヮ".to_string()), - ("ヰ".to_string(),"ヰ".to_string()), - ("ヱ".to_string(),"ヱ".to_string()), - ("ヵ".to_string(),"ヵ".to_string()), - ("ヶ".to_string(),"ヶ".to_string()), - ("ヴ".to_string(),"ヴ".to_string()), - ("ヽ".to_string(),"ヽ".to_string()), - ("ヾ".to_string(),"ヾ".to_string()), - ("・".to_string(),"・".to_string()), - ("「".to_string(),"「".to_string()), - ("」".to_string(),"」".to_string()), - ("。".to_string(),"。".to_string()), - ("、".to_string(),"、".to_string()), - ("!".to_string(),"!".to_string()), - (""".to_string(),"\"".to_string()), - ("#".to_string(),"#".to_string()), - ("$".to_string(),"$".to_string()), - ("%".to_string(),"%".to_string()), - ("&".to_string(),"&".to_string()), - ("'".to_string(),"'".to_string()), - ("(".to_string(),"(".to_string()), - (")".to_string(),")".to_string()), - ("*".to_string(),"*".to_string()), - ("+".to_string(),"+".to_string()), - (",".to_string(),",".to_string()), - (".".to_string(),".".to_string()), - ("/".to_string(),"/".to_string()), - (":".to_string(),":".to_string()), - (";".to_string(),";".to_string()), - ("<".to_string(),"<".to_string()), - ("=".to_string(),"=".to_string()), - (">".to_string(),">".to_string()), - ("?".to_string(),"?".to_string()), - ("@".to_string(),"@".to_string()), - ("A".to_string(),"A".to_string()), - ("B".to_string(),"B".to_string()), - ("C".to_string(),"C".to_string()), - ("D".to_string(),"D".to_string()), - ("E".to_string(),"E".to_string()), - ("F".to_string(),"F".to_string()), - ("G".to_string(),"G".to_string()), - ("H".to_string(),"H".to_string()), - ("I".to_string(),"I".to_string()), - ("J".to_string(),"J".to_string()), - ("K".to_string(),"K".to_string()), - ("L".to_string(),"L".to_string()), - ("M".to_string(),"M".to_string()), - ("N".to_string(),"N".to_string()), - ("O".to_string(),"O".to_string()), - ("P".to_string(),"P".to_string()), - ("Q".to_string(),"Q".to_string()), - ("R".to_string(),"R".to_string()), - ("S".to_string(),"S".to_string()), - ("T".to_string(),"T".to_string()), - ("U".to_string(),"U".to_string()), - ("V".to_string(),"V".to_string()), - ("W".to_string(),"W".to_string()), - ("X".to_string(),"X".to_string()), - ("Y".to_string(),"Y".to_string()), - ("Z".to_string(),"Z".to_string()), - ("[".to_string(),"[".to_string()), - ("\".to_string(),"\\".to_string()), - ("]".to_string(),"]".to_string()), - ("^".to_string(),"^".to_string()), - ("_".to_string(),"_".to_string()), - ("`".to_string(),"`".to_string()), - ("a".to_string(),"a".to_string()), - ("b".to_string(),"b".to_string()), - ("c".to_string(),"c".to_string()), - ("d".to_string(),"d".to_string()), - ("e".to_string(),"e".to_string()), - ("f".to_string(),"f".to_string()), - ("g".to_string(),"g".to_string()), - ("h".to_string(),"h".to_string()), - ("i".to_string(),"i".to_string()), - ("j".to_string(),"j".to_string()), - ("k".to_string(),"k".to_string()), - ("l".to_string(),"l".to_string()), - ("m".to_string(),"m".to_string()), - ("n".to_string(),"n".to_string()), - ("o".to_string(),"o".to_string()), - ("p".to_string(),"p".to_string()), - ("q".to_string(),"q".to_string()), - ("r".to_string(),"r".to_string()), - ("s".to_string(),"s".to_string()), - ("t".to_string(),"t".to_string()), - ("u".to_string(),"u".to_string()), - ("v".to_string(),"v".to_string()), - ("w".to_string(),"w".to_string()), - ("x".to_string(),"x".to_string()), - ("y".to_string(),"y".to_string()), - ("z".to_string(),"z".to_string()), - ("{".to_string(),"{".to_string()), - ("|".to_string(),"|".to_string()), - ("}".to_string(),"}".to_string()), - ("~".to_string(),"~".to_string()), - (" ".to_string()," ".to_string()), - ("0".to_string(),"0".to_string()), - ("1".to_string(),"1".to_string()), - ("2".to_string(),"2".to_string()), - ("3".to_string(),"3".to_string()), - ("4".to_string(),"4".to_string()), - ("5".to_string(),"5".to_string()), - ("6".to_string(),"6".to_string()), - ("7".to_string(),"7".to_string()), - ("8".to_string(),"8".to_string()), - ("9".to_string(),"9".to_string()), - ("ぁ".to_string(),"ァ".to_string()), - ("あ".to_string(),"ア".to_string()), - ("ぃ".to_string(),"ィ".to_string()), - ("い".to_string(),"イ".to_string()), - ("ぅ".to_string(),"ゥ".to_string()), - ("う".to_string(),"ウ".to_string()), - ("ぇ".to_string(),"ェ".to_string()), - ("え".to_string(),"エ".to_string()), - ("ぉ".to_string(),"ォ".to_string()), - ("お".to_string(),"オ".to_string()), - ("か".to_string(),"カ".to_string()), - ("が".to_string(),"ガ".to_string()), - ("き".to_string(),"キ".to_string()), - ("ぎ".to_string(),"ギ".to_string()), - ("く".to_string(),"ク".to_string()), - ("ぐ".to_string(),"グ".to_string()), - ("け".to_string(),"ケ".to_string()), - ("げ".to_string(),"ゲ".to_string()), - ("こ".to_string(),"コ".to_string()), - ("ご".to_string(),"ゴ".to_string()), - ("さ".to_string(),"サ".to_string()), - ("ざ".to_string(),"ザ".to_string()), - ("し".to_string(),"シ".to_string()), - ("じ".to_string(),"ジ".to_string()), - ("す".to_string(),"ス".to_string()), - ("ず".to_string(),"ズ".to_string()), - ("せ".to_string(),"セ".to_string()), - ("ぜ".to_string(),"ゼ".to_string()), - ("そ".to_string(),"ソ".to_string()), - ("ぞ".to_string(),"ゾ".to_string()), - ("た".to_string(),"タ".to_string()), - ("だ".to_string(),"ダ".to_string()), - ("ち".to_string(),"チ".to_string()), - ("ぢ".to_string(),"ヂ".to_string()), - ("っ".to_string(),"ッ".to_string()), - ("つ".to_string(),"ツ".to_string()), - ("づ".to_string(),"ヅ".to_string()), - ("て".to_string(),"テ".to_string()), - ("で".to_string(),"デ".to_string()), - ("と".to_string(),"ト".to_string()), - ("ど".to_string(),"ド".to_string()), - ("な".to_string(),"ナ".to_string()), - ("に".to_string(),"ニ".to_string()), - ("ぬ".to_string(),"ヌ".to_string()), - ("ね".to_string(),"ネ".to_string()), - ("の".to_string(),"ノ".to_string()), - ("は".to_string(),"ハ".to_string()), - ("ば".to_string(),"バ".to_string()), - ("ぱ".to_string(),"パ".to_string()), - ("ひ".to_string(),"ヒ".to_string()), - ("び".to_string(),"ビ".to_string()), - ("ぴ".to_string(),"ピ".to_string()), - ("ふ".to_string(),"フ".to_string()), - ("ぶ".to_string(),"ブ".to_string()), - ("ぷ".to_string(),"プ".to_string()), - ("へ".to_string(),"ヘ".to_string()), - ("べ".to_string(),"ベ".to_string()), - ("ぺ".to_string(),"ペ".to_string()), - ("ほ".to_string(),"ホ".to_string()), - ("ぼ".to_string(),"ボ".to_string()), - ("ぽ".to_string(),"ポ".to_string()), - ("ま".to_string(),"マ".to_string()), - ("み".to_string(),"ミ".to_string()), - ("む".to_string(),"ム".to_string()), - ("め".to_string(),"メ".to_string()), - ("も".to_string(),"モ".to_string()), - ("ゃ".to_string(),"ャ".to_string()), - ("や".to_string(),"ヤ".to_string()), - ("ゅ".to_string(),"ュ".to_string()), - ("ゆ".to_string(),"ユ".to_string()), - ("ょ".to_string(),"ョ".to_string()), - ("よ".to_string(),"ヨ".to_string()), - ("ら".to_string(),"ラ".to_string()), - ("り".to_string(),"リ".to_string()), - ("る".to_string(),"ル".to_string()), - ("れ".to_string(),"レ".to_string()), - ("ろ".to_string(),"ロ".to_string()), - ("わ".to_string(),"ワ".to_string()), - ("を".to_string(),"ヲ".to_string()), - ("ん".to_string(),"ン".to_string()), - ("ゎ".to_string(),"ヮ".to_string()), - ("ゐ".to_string(),"ヰ".to_string()), - ("ゑ".to_string(),"ヱ".to_string()), - ("ゕ".to_string(),"ヵ".to_string()), - ("ゖ".to_string(),"ヶ".to_string()), - ("ゔ".to_string(),"ヴ".to_string()), - ("ゝ".to_string(),"ヽ".to_string()), - ("ゞ".to_string(),"ヾ".to_string()), + ("-".to_string(), "-".to_string()), + ("ー".to_string(), "-".to_string()), + ("ァ".to_string(), "ァ".to_string()), + ("ア".to_string(), "ア".to_string()), + ("ィ".to_string(), "ィ".to_string()), + ("イ".to_string(), "イ".to_string()), + ("ゥ".to_string(), "ゥ".to_string()), + ("ウ".to_string(), "ウ".to_string()), + ("ェ".to_string(), "ェ".to_string()), + ("エ".to_string(), "エ".to_string()), + ("ォ".to_string(), "ォ".to_string()), + ("オ".to_string(), "オ".to_string()), + ("カ".to_string(), "カ".to_string()), + ("ガ".to_string(), "ガ".to_string()), + ("キ".to_string(), "キ".to_string()), + ("ギ".to_string(), "ギ".to_string()), + ("ク".to_string(), "ク".to_string()), + ("グ".to_string(), "グ".to_string()), + ("ケ".to_string(), "ケ".to_string()), + ("ゲ".to_string(), "ゲ".to_string()), + ("コ".to_string(), "コ".to_string()), + ("ゴ".to_string(), "ゴ".to_string()), + ("サ".to_string(), "サ".to_string()), + ("ザ".to_string(), "ザ".to_string()), + ("シ".to_string(), "シ".to_string()), + ("ジ".to_string(), "ジ".to_string()), + ("ス".to_string(), "ス".to_string()), + ("ズ".to_string(), "ズ".to_string()), + ("セ".to_string(), "セ".to_string()), + ("ゼ".to_string(), "ゼ".to_string()), + ("ソ".to_string(), "ソ".to_string()), + ("ゾ".to_string(), "ゾ".to_string()), + ("タ".to_string(), "タ".to_string()), + ("ダ".to_string(), "ダ".to_string()), + ("チ".to_string(), "チ".to_string()), + ("ヂ".to_string(), "ヂ".to_string()), + ("ッ".to_string(), "ッ".to_string()), + ("ツ".to_string(), "ツ".to_string()), + ("ヅ".to_string(), "ヅ".to_string()), + ("テ".to_string(), "テ".to_string()), + ("デ".to_string(), "デ".to_string()), + ("ト".to_string(), "ト".to_string()), + ("ド".to_string(), "ド".to_string()), + ("ナ".to_string(), "ナ".to_string()), + ("ニ".to_string(), "ニ".to_string()), + ("ヌ".to_string(), "ヌ".to_string()), + ("ネ".to_string(), "ネ".to_string()), + ("ノ".to_string(), "ノ".to_string()), + ("ハ".to_string(), "ハ".to_string()), + ("バ".to_string(), "バ".to_string()), + ("パ".to_string(), "パ".to_string()), + ("ヒ".to_string(), "ヒ".to_string()), + ("ビ".to_string(), "ビ".to_string()), + ("ピ".to_string(), "ピ".to_string()), + ("フ".to_string(), "フ".to_string()), + ("ブ".to_string(), "ブ".to_string()), + ("プ".to_string(), "プ".to_string()), + ("ヘ".to_string(), "ヘ".to_string()), + ("ベ".to_string(), "ベ".to_string()), + ("ペ".to_string(), "ペ".to_string()), + ("ホ".to_string(), "ホ".to_string()), + ("ボ".to_string(), "ボ".to_string()), + ("ポ".to_string(), "ポ".to_string()), + ("マ".to_string(), "マ".to_string()), + ("ミ".to_string(), "ミ".to_string()), + ("ム".to_string(), "ム".to_string()), + ("メ".to_string(), "メ".to_string()), + ("モ".to_string(), "モ".to_string()), + ("ャ".to_string(), "ャ".to_string()), + ("ヤ".to_string(), "ヤ".to_string()), + ("ュ".to_string(), "ュ".to_string()), + ("ユ".to_string(), "ユ".to_string()), + ("ョ".to_string(), "ョ".to_string()), + ("ヨ".to_string(), "ヨ".to_string()), + ("ラ".to_string(), "ラ".to_string()), + ("リ".to_string(), "リ".to_string()), + ("ル".to_string(), "ル".to_string()), + ("レ".to_string(), "レ".to_string()), + ("ロ".to_string(), "ロ".to_string()), + ("ワ".to_string(), "ワ".to_string()), + ("ヲ".to_string(), "ヲ".to_string()), + ("ン".to_string(), "ン".to_string()), + ("ー".to_string(), "-".to_string()), + ("ヮ".to_string(), "ヮ".to_string()), + ("ヰ".to_string(), "ヰ".to_string()), + ("ヱ".to_string(), "ヱ".to_string()), + ("ヵ".to_string(), "ヵ".to_string()), + ("ヶ".to_string(), "ヶ".to_string()), + ("ヴ".to_string(), "ヴ".to_string()), + ("ヽ".to_string(), "ヽ".to_string()), + ("ヾ".to_string(), "ヾ".to_string()), + ("・".to_string(), "・".to_string()), + ("「".to_string(), "「".to_string()), + ("」".to_string(), "」".to_string()), + ("。".to_string(), "。".to_string()), + ("、".to_string(), "、".to_string()), + ("!".to_string(), "!".to_string()), + (""".to_string(), "\"".to_string()), + ("#".to_string(), "#".to_string()), + ("$".to_string(), "$".to_string()), + ("%".to_string(), "%".to_string()), + ("&".to_string(), "&".to_string()), + ("'".to_string(), "'".to_string()), + ("(".to_string(), "(".to_string()), + (")".to_string(), ")".to_string()), + ("*".to_string(), "*".to_string()), + ("+".to_string(), "+".to_string()), + (",".to_string(), ",".to_string()), + (".".to_string(), ".".to_string()), + ("/".to_string(), "/".to_string()), + (":".to_string(), ":".to_string()), + (";".to_string(), ";".to_string()), + ("<".to_string(), "<".to_string()), + ("=".to_string(), "=".to_string()), + (">".to_string(), ">".to_string()), + ("?".to_string(), "?".to_string()), + ("@".to_string(), "@".to_string()), + ("A".to_string(), "A".to_string()), + ("B".to_string(), "B".to_string()), + ("C".to_string(), "C".to_string()), + ("D".to_string(), "D".to_string()), + ("E".to_string(), "E".to_string()), + ("F".to_string(), "F".to_string()), + ("G".to_string(), "G".to_string()), + ("H".to_string(), "H".to_string()), + ("I".to_string(), "I".to_string()), + ("J".to_string(), "J".to_string()), + ("K".to_string(), "K".to_string()), + ("L".to_string(), "L".to_string()), + ("M".to_string(), "M".to_string()), + ("N".to_string(), "N".to_string()), + ("O".to_string(), "O".to_string()), + ("P".to_string(), "P".to_string()), + ("Q".to_string(), "Q".to_string()), + ("R".to_string(), "R".to_string()), + ("S".to_string(), "S".to_string()), + ("T".to_string(), "T".to_string()), + ("U".to_string(), "U".to_string()), + ("V".to_string(), "V".to_string()), + ("W".to_string(), "W".to_string()), + ("X".to_string(), "X".to_string()), + ("Y".to_string(), "Y".to_string()), + ("Z".to_string(), "Z".to_string()), + ("[".to_string(), "[".to_string()), + ("\".to_string(), "\\".to_string()), + ("]".to_string(), "]".to_string()), + ("^".to_string(), "^".to_string()), + ("_".to_string(), "_".to_string()), + ("`".to_string(), "`".to_string()), + ("a".to_string(), "a".to_string()), + ("b".to_string(), "b".to_string()), + ("c".to_string(), "c".to_string()), + ("d".to_string(), "d".to_string()), + ("e".to_string(), "e".to_string()), + ("f".to_string(), "f".to_string()), + ("g".to_string(), "g".to_string()), + ("h".to_string(), "h".to_string()), + ("i".to_string(), "i".to_string()), + ("j".to_string(), "j".to_string()), + ("k".to_string(), "k".to_string()), + ("l".to_string(), "l".to_string()), + ("m".to_string(), "m".to_string()), + ("n".to_string(), "n".to_string()), + ("o".to_string(), "o".to_string()), + ("p".to_string(), "p".to_string()), + ("q".to_string(), "q".to_string()), + ("r".to_string(), "r".to_string()), + ("s".to_string(), "s".to_string()), + ("t".to_string(), "t".to_string()), + ("u".to_string(), "u".to_string()), + ("v".to_string(), "v".to_string()), + ("w".to_string(), "w".to_string()), + ("x".to_string(), "x".to_string()), + ("y".to_string(), "y".to_string()), + ("z".to_string(), "z".to_string()), + ("{".to_string(), "{".to_string()), + ("|".to_string(), "|".to_string()), + ("}".to_string(), "}".to_string()), + ("~".to_string(), "~".to_string()), + (" ".to_string(), " ".to_string()), + ("0".to_string(), "0".to_string()), + ("1".to_string(), "1".to_string()), + ("2".to_string(), "2".to_string()), + ("3".to_string(), "3".to_string()), + ("4".to_string(), "4".to_string()), + ("5".to_string(), "5".to_string()), + ("6".to_string(), "6".to_string()), + ("7".to_string(), "7".to_string()), + ("8".to_string(), "8".to_string()), + ("9".to_string(), "9".to_string()), + ("ぁ".to_string(), "ァ".to_string()), + ("あ".to_string(), "ア".to_string()), + ("ぃ".to_string(), "ィ".to_string()), + ("い".to_string(), "イ".to_string()), + ("ぅ".to_string(), "ゥ".to_string()), + ("う".to_string(), "ウ".to_string()), + ("ぇ".to_string(), "ェ".to_string()), + ("え".to_string(), "エ".to_string()), + ("ぉ".to_string(), "ォ".to_string()), + ("お".to_string(), "オ".to_string()), + ("か".to_string(), "カ".to_string()), + ("が".to_string(), "ガ".to_string()), + ("き".to_string(), "キ".to_string()), + ("ぎ".to_string(), "ギ".to_string()), + ("く".to_string(), "ク".to_string()), + ("ぐ".to_string(), "グ".to_string()), + ("け".to_string(), "ケ".to_string()), + ("げ".to_string(), "ゲ".to_string()), + ("こ".to_string(), "コ".to_string()), + ("ご".to_string(), "ゴ".to_string()), + ("さ".to_string(), "サ".to_string()), + ("ざ".to_string(), "ザ".to_string()), + ("し".to_string(), "シ".to_string()), + ("じ".to_string(), "ジ".to_string()), + ("す".to_string(), "ス".to_string()), + ("ず".to_string(), "ズ".to_string()), + ("せ".to_string(), "セ".to_string()), + ("ぜ".to_string(), "ゼ".to_string()), + ("そ".to_string(), "ソ".to_string()), + ("ぞ".to_string(), "ゾ".to_string()), + ("た".to_string(), "タ".to_string()), + ("だ".to_string(), "ダ".to_string()), + ("ち".to_string(), "チ".to_string()), + ("ぢ".to_string(), "ヂ".to_string()), + ("っ".to_string(), "ッ".to_string()), + ("つ".to_string(), "ツ".to_string()), + ("づ".to_string(), "ヅ".to_string()), + ("て".to_string(), "テ".to_string()), + ("で".to_string(), "デ".to_string()), + ("と".to_string(), "ト".to_string()), + ("ど".to_string(), "ド".to_string()), + ("な".to_string(), "ナ".to_string()), + ("に".to_string(), "ニ".to_string()), + ("ぬ".to_string(), "ヌ".to_string()), + ("ね".to_string(), "ネ".to_string()), + ("の".to_string(), "ノ".to_string()), + ("は".to_string(), "ハ".to_string()), + ("ば".to_string(), "バ".to_string()), + ("ぱ".to_string(), "パ".to_string()), + ("ひ".to_string(), "ヒ".to_string()), + ("び".to_string(), "ビ".to_string()), + ("ぴ".to_string(), "ピ".to_string()), + ("ふ".to_string(), "フ".to_string()), + ("ぶ".to_string(), "ブ".to_string()), + ("ぷ".to_string(), "プ".to_string()), + ("へ".to_string(), "ヘ".to_string()), + ("べ".to_string(), "ベ".to_string()), + ("ぺ".to_string(), "ペ".to_string()), + ("ほ".to_string(), "ホ".to_string()), + ("ぼ".to_string(), "ボ".to_string()), + ("ぽ".to_string(), "ポ".to_string()), + ("ま".to_string(), "マ".to_string()), + ("み".to_string(), "ミ".to_string()), + ("む".to_string(), "ム".to_string()), + ("め".to_string(), "メ".to_string()), + ("も".to_string(), "モ".to_string()), + ("ゃ".to_string(), "ャ".to_string()), + ("や".to_string(), "ヤ".to_string()), + ("ゅ".to_string(), "ュ".to_string()), + ("ゆ".to_string(), "ユ".to_string()), + ("ょ".to_string(), "ョ".to_string()), + ("よ".to_string(), "ヨ".to_string()), + ("ら".to_string(), "ラ".to_string()), + ("り".to_string(), "リ".to_string()), + ("る".to_string(), "ル".to_string()), + ("れ".to_string(), "レ".to_string()), + ("ろ".to_string(), "ロ".to_string()), + ("わ".to_string(), "ワ".to_string()), + ("を".to_string(), "ヲ".to_string()), + ("ん".to_string(), "ン".to_string()), + ("ゎ".to_string(), "ヮ".to_string()), + ("ゐ".to_string(), "ヰ".to_string()), + ("ゑ".to_string(), "ヱ".to_string()), + ("ゕ".to_string(), "ヵ".to_string()), + ("ゖ".to_string(), "ヶ".to_string()), + ("ゔ".to_string(), "ヴ".to_string()), + ("ゝ".to_string(), "ヽ".to_string()), + ("ゞ".to_string(), "ヾ".to_string()), ]); - pub static ref ALLOWED_HW_KANA: Vec = MAPPINGS_HW.values().cloned().collect(); } diff --git a/minidisc-rs/src/netmd/mod.rs b/minidisc-rs/src/netmd/mod.rs index a8450ea..234810e 100644 --- a/minidisc-rs/src/netmd/mod.rs +++ b/minidisc-rs/src/netmd/mod.rs @@ -6,7 +6,7 @@ mod base; pub mod interface; +mod mappings; mod query_utils; mod utils; -mod mappings; mod commands; diff --git a/minidisc-rs/src/netmd/query_utils.rs b/minidisc-rs/src/netmd/query_utils.rs index d3a8b20..f008a43 100644 --- a/minidisc-rs/src/netmd/query_utils.rs +++ b/minidisc-rs/src/netmd/query_utils.rs @@ -3,7 +3,7 @@ use lazy_static::lazy_static; use std::collections::hash_map::HashMap; use std::error::Error; -lazy_static!{ +lazy_static! { /// %b, w, d, q - explained above (can have endiannes overriden by '>' and '<' operators, f. ex. %>d %, -) -> Result, Box> { +pub fn format_query(format: String, args: Vec) -> Result, Box> { if DEBUG { println!("SENT>>> F: {}", format); } diff --git a/minidisc-rs/src/netmd/utils.rs b/minidisc-rs/src/netmd/utils.rs index 3bb1a95..ced7644 100644 --- a/minidisc-rs/src/netmd/utils.rs +++ b/minidisc-rs/src/netmd/utils.rs @@ -59,9 +59,7 @@ pub fn half_width_to_full_width_range(range: &str) -> String { .collect() } -pub fn get_bytes( - iterator: &mut IntoIter, -) -> Result<[u8; S], Box> { +pub fn get_bytes(iterator: &mut IntoIter) -> Result<[u8; S], Box> { let byte_vec: Vec = iterator.take(S).collect(); let bytes: [u8; S] = byte_vec.try_into().unwrap(); @@ -170,5 +168,7 @@ pub fn agressive_sanitize_title(title: &str) -> String { pub fn time_to_duration(time: &Vec) -> std::time::Duration { assert_eq!(time.len(), 4); - std::time::Duration::from_micros((time[0] * 3600000000) + (time[1] * 60000000) + (time[2] * 1000000) + (time[3] * 11600)) + std::time::Duration::from_micros( + (time[0] * 3600000000) + (time[1] * 60000000) + (time[2] * 1000000) + (time[3] * 11600), + ) } diff --git a/src/main.rs b/src/main.rs index bcece72..e3b196b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,15 @@ use minidisc_rs::netmd::interface; use nusb; -fn main() { +#[tokio::main] +async fn main() { let devices = nusb::list_devices().unwrap(); for device in devices { // Ensure the player is a minidisc player and not some other random device - let mut player_controller = match interface::NetMDInterface::new(&device) { + let mut player_controller = match interface::NetMDInterface::new(&device).await { Ok(player) => player, - Err(_) => continue, + Err(err) => continue, }; println!( @@ -22,22 +23,25 @@ fn main() { player_controller .net_md_device .device_name() + .await .clone() .unwrap() ); let now = std::time::Instant::now(); + let half_title = player_controller.disc_title(false).await.unwrap_or("".to_string()); + let full_title = player_controller.disc_title(true).await.unwrap_or("".to_string()); println!( "Disc Title: {} | {}", - player_controller - .disc_title(false) - .unwrap_or("".to_string()), - player_controller.disc_title(true).unwrap_or("".to_string()) + half_title, + full_title ); - let track_count = player_controller.track_count().unwrap(); - let track_titles = player_controller.track_titles((0..track_count).collect(), false).unwrap(); - let track_titlesw = player_controller.track_titles((0..track_count).collect(), true).unwrap(); - let track_lengths = player_controller.track_lengths((0..track_count).collect()).unwrap(); + + let track_count = player_controller.track_count().await.unwrap(); + println!("{}", track_count); + let track_titles = player_controller.track_titles((0..track_count).collect(), false).await.unwrap(); + let track_titlesw = player_controller.track_titles((0..track_count).collect(), true).await.unwrap(); + let track_lengths = player_controller.track_lengths((0..track_count).collect()).await.unwrap(); for (i, track) in track_titles.iter().enumerate() { println!( "Track {i} Info:\n Title: {track} | {}\n Length: {:?}",