Started work on WASM support

This commit is contained in:
G2-Games 2024-01-30 18:04:46 -06:00
parent 27853d9ea0
commit 766b4c68f7
8 changed files with 162 additions and 156 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[target.'cfg(target_family = "wasm")']
rustflags = ["--cfg=web_sys_unstable_apis"]

View file

@ -10,9 +10,12 @@ license = "AGPL-3.0"
[dependencies] [dependencies]
hex = "0.4.3" hex = "0.4.3"
nusb = "0.1.4" cross_usb = { path = "/home/g2/Documents/projects/code/rust/cross-usb/" }
[target.'cfg(target_family = "wasm")'.dependencies]
tokio = { version = "1.35.1", features = ["macros"] }
[target.'cfg(not(target_family = "wasm"))'.dependencies]
tokio = { version = "1.35.1", features = ["full"] } tokio = { version = "1.35.1", features = ["full"] }
[dependencies.minidisc-rs] [dependencies.minidisc_rs]
path = "minidisc-rs" path = "minidisc-rs"
version = "0.0.1" version = "0.0.1"

View file

@ -1,5 +1,5 @@
[package] [package]
name = "minidisc-rs" name = "minidisc_rs"
version = "0.0.1" version = "0.0.1"
homepage = "https://github.com/G2-Games/minidisc-cli/" homepage = "https://github.com/G2-Games/minidisc-cli/"
repository = "https://github.com/G2-Games/minidisc-cli/minidisc-rs/" repository = "https://github.com/G2-Games/minidisc-cli/minidisc-rs/"
@ -21,7 +21,7 @@ unicode-normalization = "0.1.22"
hex = "0.4.3" hex = "0.4.3"
regex = "1.10.2" regex = "1.10.2"
lazy_static = "1.4.0" lazy_static = "1.4.0"
nusb = "0.1.4" cross_usb = { path = "../../cross-usb/" }
num-derive = "0.3.3" num-derive = "0.3.3"
num-traits = "0.2.14" num-traits = "0.2.14"
rand = "0.8.5" rand = "0.8.5"
@ -31,5 +31,12 @@ getrandom = { version = "0.2.12", features = ["js"] }
# Relying on this fork for now as it has up-to-date deps # Relying on this fork for now as it has up-to-date deps
git = "https://github.com/uzabase/unicode-jp-rs.git" git = "https://github.com/uzabase/unicode-jp-rs.git"
[target.'cfg(target_family = "wasm")'.dependencies]
web-sys = { version = "0.3", features = ["console"] }
gloo = { version = "0.11.0", features = ["futures"] }
[lib] [lib]
crate-type = ["cdylib", "rlib"] crate-type = ["cdylib", "rlib"]
[package.metadata.wasm-pack.profile.dev.wasm-bindgen]
dwarf-debug-info = true

View file

@ -1,11 +1,17 @@
#![cfg_attr(debug_assertions, allow(dead_code))]
use nofmt; use nofmt;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::error::Error; use std::error::Error;
use std::time::Duration; use std::time::Duration;
#[cfg(target_family = "wasm")]
use gloo::timers::future::TimeoutFuture;
// USB stuff // USB stuff
use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer}; //use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer};
use nusb::{Device, DeviceInfo, Interface}; use cross_usb::context::{UsbDevice, UsbInterface};
use cross_usb::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
//use nusb::{Device, DeviceInfo, Interface};
const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0); const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0);
const BULK_WRITE_ENDPOINT: u8 = 0x02; const BULK_WRITE_ENDPOINT: u8 = 0x02;
@ -85,8 +91,7 @@ pub struct DeviceId {
/// A connection to a NetMD device /// A connection to a NetMD device
pub struct NetMD { pub struct NetMD {
usb_device: Device, usb_interface: UsbInterface,
usb_interface: Interface,
model: DeviceId, model: DeviceId,
status: Option<Status>, status: Option<Status>,
} }
@ -95,10 +100,10 @@ impl NetMD {
const READ_REPLY_RETRY_INTERVAL: u32 = 10; const READ_REPLY_RETRY_INTERVAL: u32 = 10;
/// Creates a new interface to a NetMD device /// Creates a new interface to a NetMD device
pub async fn new(device_info: &DeviceInfo) -> Result<Self, Box<dyn Error>> { pub async fn new(usb_device: &UsbDevice) -> Result<Self, Box<dyn Error>> {
let mut model = DeviceId { let mut model = DeviceId {
vendor_id: device_info.vendor_id(), vendor_id: usb_device.vendor_id().await,
product_id: device_info.product_id(), product_id: usb_device.product_id().await,
name: None, name: None,
}; };
@ -116,11 +121,9 @@ impl NetMD {
Some(_) => (), Some(_) => (),
} }
let usb_device = device_info.open()?; let usb_interface = usb_device.open_interface(0).await?;
let usb_interface = usb_device.claim_interface(0)?;
Ok(Self { Ok(Self {
usb_device,
usb_interface, usb_interface,
model, model,
status: None, status: None,
@ -157,10 +160,9 @@ impl NetMD {
length: 4, length: 4,
}) })
.await .await
.into_result()
{ {
Ok(size) => size, Ok(size) => size,
Err(error) => return Err(error.into()), Err(error) => return Err(error),
}; };
let length_bytes = u16::from_le_bytes([poll_result[2], poll_result[3]]); let length_bytes = u16::from_le_bytes([poll_result[2], poll_result[3]]);
@ -190,9 +192,9 @@ impl NetMD {
// First poll to ensure the device is ready // First poll to ensure the device is ready
match self.poll().await { match self.poll().await {
Ok(buffer) => match buffer.1[2] { Ok(buffer) => match buffer.1[2] {
0 => 0, 0 => 0,
_ => return Err("Device not ready!".into()), _ => return Err("Device not ready!".into()),
}, },
Err(error) => return Err(error), Err(error) => return Err(error),
}; };
@ -212,10 +214,9 @@ impl NetMD {
data: &command, data: &command,
}) })
.await .await
.into_result()
{ {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(error) => Err(error.into()), Err(error) => Err(error),
} }
} }
@ -242,12 +243,17 @@ impl NetMD {
let mut length = self.poll().await?.0; let mut length = self.poll().await?.0;
let mut current_attempt = 0; let mut current_attempt = 0;
while length == 0 { while length == 0 && current_attempt < 75 {
// Back off while trying again // Back off while trying again
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64 let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64
* (u64::pow(2, current_attempt as u32 / 10) - 1); * (u64::pow(2, current_attempt as u32 / 10) - 1);
#[cfg(not(target_family = "wasm"))]
std::thread::sleep(std::time::Duration::from_millis(sleep_time)); std::thread::sleep(std::time::Duration::from_millis(sleep_time));
#[cfg(target_family = "wasm")]
TimeoutFuture::new(sleep_time as u32).await;
length = self.poll().await?.0; length = self.poll().await?.0;
current_attempt += 1; current_attempt += 1;
} }
@ -272,14 +278,12 @@ impl NetMD {
index: 0, index: 0,
length, length,
}) })
.await .await?;
.into_result()?;
Ok(reply) Ok(reply)
} }
// Default chunksize should be 0x10000 // Default chunksize should be 0x10000
// TODO: Make these Async eventually
pub async fn read_bulk( pub async fn read_bulk(
&mut self, &mut self,
length: usize, length: usize,
@ -301,13 +305,11 @@ impl NetMD {
while done < length { while done < length {
let to_read = std::cmp::min(chunksize, length - done); let to_read = std::cmp::min(chunksize, length - done);
done -= to_read; done -= to_read;
let buffer = RequestBuffer::new(to_read);
let res = match self let res = match self
.usb_interface .usb_interface
.bulk_in(BULK_READ_ENDPOINT, buffer) .bulk_in(BULK_READ_ENDPOINT, to_read)
.await .await
.into_result()
{ {
Ok(result) => result, Ok(result) => result,
Err(error) => return Err(format!("USB error: {:?}", error).into()), Err(error) => return Err(format!("USB error: {:?}", error).into()),
@ -320,11 +322,6 @@ impl NetMD {
} }
pub async fn write_bulk(&mut self, data: Vec<u8>) -> Result<usize, Box<dyn Error>> { pub async fn write_bulk(&mut self, data: Vec<u8>) -> Result<usize, Box<dyn Error>> {
Ok(self self.usb_interface.bulk_out(BULK_WRITE_ENDPOINT, data).await
.usb_interface
.bulk_out(BULK_WRITE_ENDPOINT, data)
.await
.into_result()?
.actual_length())
} }
} }

View file

@ -1,3 +1,4 @@
#![cfg_attr(debug_assertions, allow(dead_code))]
use std::{error::Error, thread::sleep, time::Duration}; use std::{error::Error, thread::sleep, time::Duration};
use num_derive::FromPrimitive; use num_derive::FromPrimitive;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
@ -57,20 +58,20 @@ pub async fn device_status(interface: &mut NetMDInterface) -> Result<DeviceStatu
} }
pub async fn prepare_download(interface: &mut NetMDInterface) -> Result<(), Box<dyn Error>>{ pub async fn prepare_download(interface: &mut NetMDInterface) -> Result<(), Box<dyn Error>>{
while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(&device_status(interface).await?.state.or(Some(OperatingStatus::NoDisc)).unwrap()) { while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(&device_status(interface).await?.state.unwrap_or(OperatingStatus::NoDisc)) {
sleep(Duration::from_millis(200)); sleep(Duration::from_millis(200));
} }
let _ = interface.session_key_forget(); let _ = interface.session_key_forget().await;
let _ = interface.leave_secure_session(); let _ = interface.leave_secure_session().await;
interface.acquire().await?; interface.acquire().await?;
let _ = interface.disable_new_track_protection(1); let _ = interface.disable_new_track_protection(1).await;
Ok(()) Ok(())
} }
pub async fn download(interface: &mut NetMDInterface, track: MDTrack) -> Result<(), Box<dyn Error>>{ pub async fn download(interface: &mut NetMDInterface, _track: MDTrack) -> Result<(), Box<dyn Error>>{
prepare_download(interface).await?; prepare_download(interface).await?;
Ok(()) Ok(())

View file

@ -1,3 +1,4 @@
#![cfg_attr(debug_assertions, allow(dead_code))]
use crate::netmd::base; use crate::netmd::base;
use crate::netmd::query_utils::{format_query, scan_query, QueryValue}; use crate::netmd::query_utils::{format_query, scan_query, QueryValue};
use crate::netmd::utils::{ use crate::netmd::utils::{
@ -137,7 +138,7 @@ enum DescriptorAction {
} }
#[repr(u8)] #[repr(u8)]
enum Status { enum NetmdStatus {
// NetMD Protocol return status (first byte of request) // NetMD Protocol return status (first byte of request)
Control = 0x00, Control = 0x00,
Status = 0x01, Status = 0x01,
@ -163,23 +164,23 @@ lazy_static! {
]); ]);
} }
impl std::convert::TryFrom<u8> for Status { impl std::convert::TryFrom<u8> for NetmdStatus {
type Error = Box<dyn Error>; type Error = Box<dyn Error>;
fn try_from(item: u8) -> Result<Self, Box<dyn Error>> { fn try_from(item: u8) -> Result<Self, Box<dyn Error>> {
match item { match item {
0x00 => Ok(Status::Control), 0x00 => Ok(NetmdStatus::Control),
0x01 => Ok(Status::Status), 0x01 => Ok(NetmdStatus::Status),
0x02 => Ok(Status::SpecificInquiry), 0x02 => Ok(NetmdStatus::SpecificInquiry),
0x03 => Ok(Status::Notify), 0x03 => Ok(NetmdStatus::Notify),
0x04 => Ok(Status::GeneralInquiry), 0x04 => Ok(NetmdStatus::GeneralInquiry),
0x08 => Ok(Status::NotImplemented), 0x08 => Ok(NetmdStatus::NotImplemented),
0x09 => Ok(Status::Accepted), 0x09 => Ok(NetmdStatus::Accepted),
0x0a => Ok(Status::Rejected), 0x0a => Ok(NetmdStatus::Rejected),
0x0b => Ok(Status::InTransition), 0x0b => Ok(NetmdStatus::InTransition),
0x0c => Ok(Status::Implemented), 0x0c => Ok(NetmdStatus::Implemented),
0x0d => Ok(Status::Changed), 0x0d => Ok(NetmdStatus::Changed),
0x0f => Ok(Status::Interim), 0x0f => Ok(NetmdStatus::Interim),
_ => Err("Not a valid value".into()), _ => Err("Not a valid value".into()),
} }
} }
@ -203,7 +204,7 @@ impl NetMDInterface {
const MAX_INTERIM_READ_ATTEMPTS: u8 = 4; const MAX_INTERIM_READ_ATTEMPTS: u8 = 4;
const INTERIM_RESPONSE_RETRY_INTERVAL: u32 = 100; const INTERIM_RESPONSE_RETRY_INTERVAL: u32 = 100;
pub async fn new(device: &nusb::DeviceInfo) -> Result<Self, Box<dyn Error>> { pub async fn new(device: &cross_usb::context::UsbDevice) -> Result<Self, Box<dyn Error>> {
let net_md_device = base::NetMD::new(device).await?; let net_md_device = base::NetMD::new(device).await?;
Ok(NetMDInterface { net_md_device }) Ok(NetMDInterface { net_md_device })
} }
@ -223,7 +224,7 @@ impl NetMDInterface {
self.change_descriptor_state( self.change_descriptor_state(
&Descriptor::DiscSubunitIdentifier, &Descriptor::DiscSubunitIdentifier,
&DescriptorAction::OpenRead, &DescriptorAction::OpenRead,
); ).await;
let mut query = format_query("1809 00 ff00 0000 0000".to_string(), vec![])?; let mut query = format_query("1809 00 ff00 0000 0000".to_string(), vec![])?;
@ -291,7 +292,7 @@ impl NetMDInterface {
let _manufacturer_dep_data = let _manufacturer_dep_data =
&buffer[buffer_offset..buffer_offset + manufacturer_dep_length as usize]; &buffer[buffer_offset..buffer_offset + manufacturer_dep_length as usize];
self.change_descriptor_state(&Descriptor::DiscSubunitIdentifier, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::DiscSubunitIdentifier, &DescriptorAction::Close).await;
for media in supported_media_type_specifications { for media in supported_media_type_specifications {
if media.supported_media_type != 0x301 { if media.supported_media_type != 0x301 {
@ -321,7 +322,7 @@ impl NetMDInterface {
Ok(result) Ok(result)
} }
fn change_descriptor_state(&mut self, descriptor: &Descriptor, action: &DescriptorAction) { async fn change_descriptor_state(&mut self, descriptor: &Descriptor, action: &DescriptorAction) {
let mut query = format_query("1808".to_string(), vec![]).unwrap(); let mut query = format_query("1808".to_string(), vec![]).unwrap();
query.append(&mut descriptor.get_array()); query.append(&mut descriptor.get_array());
@ -330,7 +331,7 @@ impl NetMDInterface {
query.push(0x00); query.push(0x00);
let _ = self.send_query(&mut query, false, false); let _ = self.send_query(&mut query, false, false).await;
} }
/// Send a query to the NetMD player /// Send a query to the NetMD player
@ -353,8 +354,8 @@ impl NetMDInterface {
test: bool, test: bool,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let status_byte = match test { let status_byte = match test {
true => Status::GeneralInquiry, true => NetmdStatus::GeneralInquiry,
false => Status::Control, false => NetmdStatus::Control,
}; };
let mut new_query = Vec::new(); let mut new_query = Vec::new();
@ -377,15 +378,15 @@ impl NetMDInterface {
Err(error) => return Err(error), Err(error) => return Err(error),
}; };
let status = match Status::try_from(data[0]) { let status = match NetmdStatus::try_from(data[0]) {
Ok(status) => status, Ok(status) => status,
Err(error) => return Err(error), Err(error) => return Err(error),
}; };
match status { match status {
Status::NotImplemented => return Err("Not implemented".into()), NetmdStatus::NotImplemented => return Err("Not implemented".into()),
Status::Rejected => return Err("Rejected".into()), NetmdStatus::Rejected => return Err("Rejected".into()),
Status::Interim if !accept_interim => { NetmdStatus::Interim if !accept_interim => {
let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL as u64 let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL as u64
* (u64::pow(2, current_attempt as u32) - 1); * (u64::pow(2, current_attempt as u32) - 1);
let sleep_dur = std::time::Duration::from_millis(sleep_time); let sleep_dur = std::time::Duration::from_millis(sleep_time);
@ -393,7 +394,7 @@ impl NetMDInterface {
current_attempt += 1; current_attempt += 1;
continue; // Retry! continue; // Retry!
} }
Status::Accepted | Status::Implemented | Status::Interim => { NetmdStatus::Accepted | NetmdStatus::Implemented | NetmdStatus::Interim => {
if current_attempt >= Self::MAX_INTERIM_READ_ATTEMPTS { if current_attempt >= Self::MAX_INTERIM_READ_ATTEMPTS {
return Err("Max interim retry attempts reached".into()); return Err("Max interim retry attempts reached".into());
} }
@ -470,7 +471,7 @@ impl NetMDInterface {
self.change_descriptor_state( self.change_descriptor_state(
&Descriptor::OperatingStatusBlock, &Descriptor::OperatingStatusBlock,
&DescriptorAction::OpenRead, &DescriptorAction::OpenRead,
); ).await;
let mut query = format_query( let mut query = format_query(
"1809 8001 0230 8800 0030 8804 00 ff00 00000000".to_string(), "1809 8001 0230 8800 0030 8804 00 ff00 00000000".to_string(),
@ -484,7 +485,7 @@ impl NetMDInterface {
"1809 8001 0230 8800 0030 8804 00 1000 00090000 %x".to_string(), "1809 8001 0230 8800 0030 8804 00 1000 00090000 %x".to_string(),
)?; )?;
self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close).await;
let final_array = res[0].to_vec().unwrap(); let final_array = res[0].to_vec().unwrap();
@ -504,7 +505,7 @@ impl NetMDInterface {
self.change_descriptor_state( self.change_descriptor_state(
&Descriptor::OperatingStatusBlock, &Descriptor::OperatingStatusBlock,
&DescriptorAction::OpenRead, &DescriptorAction::OpenRead,
); ).await;
let mut query = format_query( let mut query = format_query(
"1809 8001 0330 8802 0030 8805 0030 8806 00 ff00 00000000".to_string(), "1809 8001 0330 8802 0030 8805 0030 8806 00 ff00 00000000".to_string(),
vec![], vec![],
@ -520,7 +521,7 @@ impl NetMDInterface {
let operating_status = result[1].to_vec().unwrap(); let operating_status = result[1].to_vec().unwrap();
let status_mode = result[0].to_i64().unwrap() as u8; let status_mode = result[0].to_i64().unwrap() as u8;
self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close).await;
if operating_status.len() < 2 { if operating_status.len() < 2 {
return Err("Unparsable operating system".into()); return Err("Unparsable operating system".into());
@ -542,7 +543,7 @@ impl NetMDInterface {
self.change_descriptor_state( self.change_descriptor_state(
&Descriptor::OperatingStatusBlock, &Descriptor::OperatingStatusBlock,
&DescriptorAction::OpenRead, &DescriptorAction::OpenRead,
); ).await;
let mut query = format_query( let mut query = format_query(
"1809 8001 0330 %w 0030 8805 0030 %w 00 ff00 00000000".to_string(), "1809 8001 0330 %w 0030 8805 0030 %w 00 ff00 00000000".to_string(),
vec![QueryValue::Number(p1 as i64), QueryValue::Number(p2 as i64)], vec![QueryValue::Number(p1 as i64), QueryValue::Number(p2 as i64)],
@ -556,7 +557,7 @@ impl NetMDInterface {
"1809 8001 0330 %?%? %?%? %?%? %?%? %?%? %? 1000 00%?0000 %x %?".to_string(), "1809 8001 0330 %?%? %?%? %?%? %?%? %?%? %? 1000 00%?0000 %x %?".to_string(),
); );
self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close).await;
Ok(res.unwrap()[0].to_vec().unwrap()) Ok(res.unwrap()[0].to_vec().unwrap())
} }
@ -573,7 +574,7 @@ impl NetMDInterface {
self.change_descriptor_state( self.change_descriptor_state(
&Descriptor::OperatingStatusBlock, &Descriptor::OperatingStatusBlock,
&DescriptorAction::OpenRead, &DescriptorAction::OpenRead,
); ).await;
let mut query = format_query( let mut query = format_query(
"1809 8001 0430 8802 0030 8805 0030 0003 0030 0002 00 ff00 00000000".to_string(), "1809 8001 0430 8802 0030 8805 0030 0003 0030 0002 00 ff00 00000000".to_string(),
@ -597,7 +598,7 @@ impl NetMDInterface {
result[4].to_i64().unwrap() as u16, result[4].to_i64().unwrap() as u16,
]; ];
self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close).await;
Ok(final_result) Ok(final_result)
} }
@ -704,21 +705,21 @@ impl NetMDInterface {
// TODO: Ensure this is returning the correct value, it // TODO: Ensure this is returning the correct value, it
// looks like it actually might be a 16 bit integer // looks like it actually might be a 16 bit integer
pub async fn disc_flags(&mut self) -> Result<u8, Box<dyn Error>> { pub async fn disc_flags(&mut self) -> Result<u8, Box<dyn Error>> {
self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead).await;
let mut query = format_query("1806 01101000 ff00 0001000b".to_string(), vec![]).unwrap(); let mut query = format_query("1806 01101000 ff00 0001000b".to_string(), vec![]).unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&mut query, false, false).await?;
let res = scan_query(reply, "1806 01101000 1000 0001000b %b".to_string()).unwrap(); let res = scan_query(reply, "1806 01101000 1000 0001000b %b".to_string()).unwrap();
self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::Close).await;
Ok(res[0].to_i64().unwrap() as u8) Ok(res[0].to_i64().unwrap() as u8)
} }
/// The number of tracks on the disc /// The number of tracks on the disc
pub async fn track_count(&mut self) -> Result<u16, Box<dyn Error>> { pub async fn track_count(&mut self) -> Result<u16, Box<dyn Error>> {
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead).await;
let mut query = let mut query =
format_query("1806 02101001 3000 1000 ff00 00000000".to_string(), vec![]).unwrap(); format_query("1806 02101001 3000 1000 ff00 00000000".to_string(), vec![]).unwrap();
@ -731,14 +732,14 @@ impl NetMDInterface {
) )
.unwrap(); .unwrap();
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close).await;
Ok(res[0].to_i64().unwrap() as u16) Ok(res[0].to_i64().unwrap() as u16)
} }
async fn raw_disc_title(&mut self, wchar: bool) -> Result<String, Box<dyn Error>> { async fn raw_disc_title(&mut self, wchar: bool) -> Result<String, Box<dyn Error>> {
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead).await;
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead).await;
let mut done: i32 = 0; let mut done: i32 = 0;
let mut remaining: i32 = 0; let mut remaining: i32 = 0;
@ -792,8 +793,8 @@ impl NetMDInterface {
let res = result.join(""); let res = result.join("");
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close).await;
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close).await;
Ok(res) Ok(res)
} }
@ -926,7 +927,7 @@ impl NetMDInterface {
false => Descriptor::AudioUTOC1TD, false => Descriptor::AudioUTOC1TD,
}; };
self.change_descriptor_state(&descriptor_type, &DescriptorAction::OpenRead); self.change_descriptor_state(&descriptor_type, &DescriptorAction::OpenRead).await;
let mut track_titles: Vec<String> = vec![]; let mut track_titles: Vec<String> = vec![];
for i in tracks { for i in tracks {
@ -955,7 +956,7 @@ impl NetMDInterface {
) )
} }
self.change_descriptor_state(&descriptor_type, &DescriptorAction::Close); self.change_descriptor_state(&descriptor_type, &DescriptorAction::Close).await;
Ok(track_titles) Ok(track_titles)
} }
@ -973,7 +974,7 @@ impl NetMDInterface {
// Sets the title of the disc // Sets the title of the disc
pub async fn set_disc_title( pub async fn set_disc_title(
&mut self, &mut self,
title: String, title: &str,
wchar: bool, wchar: bool,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let current_title = self.raw_disc_title(wchar).await?; let current_title = self.raw_disc_title(wchar).await?;
@ -998,10 +999,10 @@ impl NetMDInterface {
let new_len = new_title.len(); let new_len = new_title.len();
if self.net_md_device.vendor_id().await == &0x04dd { if self.net_md_device.vendor_id().await == &0x04dd {
self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::OpenWrite) self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::OpenWrite).await
} else { } else {
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close).await;
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenWrite) self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenWrite).await
} }
let mut query = format_query( let mut query = format_query(
@ -1014,14 +1015,14 @@ impl NetMDInterface {
], ],
)?; )?;
let _ = self.send_query(&mut query, false, false); let _ = self.send_query(&mut query, false, false).await;
if self.net_md_device.vendor_id().await == &0x04dd { if self.net_md_device.vendor_id().await == &0x04dd {
self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::Close) self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::Close).await
} else { } else {
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close).await;
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead).await;
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close).await;
} }
Ok(()) Ok(())
@ -1031,7 +1032,7 @@ impl NetMDInterface {
pub async fn set_track_title( pub async fn set_track_title(
&mut self, &mut self,
track: u16, track: u16,
title: String, title: &str,
wchar: bool, wchar: bool,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let new_title: Vec<u8>; let new_title: Vec<u8>;
@ -1059,7 +1060,7 @@ impl NetMDInterface {
Err(error) => return Err(error), Err(error) => return Err(error),
}; };
self.change_descriptor_state(&descriptor, &DescriptorAction::OpenWrite); self.change_descriptor_state(&descriptor, &DescriptorAction::OpenWrite).await;
let mut query = format_query( let mut query = format_query(
"1807 022018%b %w 3000 0a00 5000 %w 0000 %w %*".to_string(), "1807 022018%b %w 3000 0a00 5000 %w 0000 %w %*".to_string(),
vec![ vec![
@ -1076,7 +1077,7 @@ impl NetMDInterface {
reply, reply,
"1807 022018%? %?%? 3000 0a00 5000 %?%? 0000 %?%?".to_string(), "1807 022018%? %?%? 3000 0a00 5000 %?%? 0000 %?%?".to_string(),
); );
self.change_descriptor_state(&descriptor, &DescriptorAction::Close); self.change_descriptor_state(&descriptor, &DescriptorAction::Close).await;
Ok(()) Ok(())
} }
@ -1114,7 +1115,7 @@ impl NetMDInterface {
p1: i32, p1: i32,
p2: i32, p2: i32,
) -> Result<Vec<u8>, Box<dyn Error>> { ) -> Result<Vec<u8>, Box<dyn Error>> {
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead).await;
let mut query = format_query( let mut query = format_query(
"1806 02201001 %w %w %w ff00 00000000".to_string(), "1806 02201001 %w %w %w ff00 00000000".to_string(),
@ -1131,7 +1132,7 @@ impl NetMDInterface {
"1806 02201001 %?%? %?%? %?%? 1000 00%?0000 %x".to_string(), "1806 02201001 %?%? %?%? %?%? 1000 00%?0000 %x".to_string(),
)?; )?;
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close).await;
Ok(res[0].to_vec().unwrap()) Ok(res[0].to_vec().unwrap())
} }
@ -1143,7 +1144,7 @@ impl NetMDInterface {
) -> Result<Vec<std::time::Duration>, Box<dyn Error>> { ) -> Result<Vec<std::time::Duration>, Box<dyn Error>> {
let mut times: Vec<std::time::Duration> = vec![]; let mut times: Vec<std::time::Duration> = vec![];
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead).await;
for track in tracks { for track in tracks {
let mut query = format_query( let mut query = format_query(
"1806 02201001 %w %w %w ff00 00000000".to_string(), "1806 02201001 %w %w %w ff00 00000000".to_string(),
@ -1175,7 +1176,7 @@ impl NetMDInterface {
times.push(length); times.push(length);
} }
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close).await;
Ok(times) Ok(times)
} }
@ -1206,7 +1207,7 @@ impl NetMDInterface {
/// Gets a track's flags /// Gets a track's flags
pub async fn track_flags(&mut self, track: u16) -> Result<u8, Box<dyn Error>> { pub async fn track_flags(&mut self, track: u16) -> Result<u8, Box<dyn Error>> {
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead).await;
let mut query = format_query( let mut query = format_query(
"1806 01201001 %w ff00 00010008".to_string(), "1806 01201001 %w ff00 00010008".to_string(),
vec![QueryValue::Number(track as i64)], vec![QueryValue::Number(track as i64)],
@ -1215,14 +1216,14 @@ impl NetMDInterface {
let res = scan_query(reply, "1806 01201001 %?%? 10 00 00010008 %b".to_string())?; let res = scan_query(reply, "1806 01201001 %?%? 10 00 00010008 %b".to_string())?;
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close).await;
Ok(res[0].to_i64().unwrap() as u8) Ok(res[0].to_i64().unwrap() as u8)
} }
/// Gets the disc capacity as a `std::time::Duration` /// Gets the disc capacity as a `std::time::Duration`
pub async fn disc_capacity(&mut self) -> Result<[std::time::Duration; 3], Box<dyn Error>> { pub async fn disc_capacity(&mut self) -> Result<[std::time::Duration; 3], Box<dyn Error>> {
self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead); self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead).await;
let mut query = format_query("1806 02101000 3080 0300 ff00 00000000".to_string(), vec![])?; let mut query = format_query("1806 02101000 3080 0300 ff00 00000000".to_string(), vec![])?;
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&mut query, false, false).await?;
let mut result: [std::time::Duration; 3] = [std::time::Duration::from_secs(0); 3]; let mut result: [std::time::Duration; 3] = [std::time::Duration::from_secs(0); 3];
@ -1245,7 +1246,7 @@ impl NetMDInterface {
result[i] = std::time::Duration::from_micros(time_micros); result[i] = std::time::Duration::from_micros(time_micros);
} }
self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::Close).await;
Ok(result) Ok(result)
} }
@ -1254,7 +1255,7 @@ impl NetMDInterface {
self.change_descriptor_state( self.change_descriptor_state(
&Descriptor::OperatingStatusBlock, &Descriptor::OperatingStatusBlock,
&DescriptorAction::OpenRead, &DescriptorAction::OpenRead,
); ).await;
let mut query = format_query( let mut query = format_query(
"1809 8001 0330 8801 0030 8805 0030 8807 00 ff00 00000000".to_string(), "1809 8001 0330 8801 0030 8805 0030 8807 00 ff00 00000000".to_string(),
vec![], vec![],
@ -1264,7 +1265,7 @@ impl NetMDInterface {
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())?; 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())?;
self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close); self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close).await;
Ok(res.into_iter().map(|x| x.to_i64().unwrap() as u8).collect()) Ok(res.into_iter().map(|x| x.to_i64().unwrap() as u8).collect())
} }

View file

@ -89,15 +89,13 @@ fn check(string: String) -> Option<String> {
None None
} }
pub fn sanitize_half_width_title(mut title: String) -> Vec<u8> { pub fn sanitize_half_width_title(title: &str) -> Vec<u8> {
title = wide2ascii(&title); let mut string_title = wide2ascii(&title);
title = nowidespace(&title); string_title = nowidespace(&string_title);
title = hira2kata(&title); string_title = hira2kata(&string_title);
title = combine(&title); string_title = combine(&string_title);
println!("{}", title); let new_title: String = string_title
let new_title: String = title
.chars() .chars()
.map(|c| { .map(|c| {
check(c.to_string()).unwrap_or( check(c.to_string()).unwrap_or(

View file

@ -1,53 +1,50 @@
use minidisc_rs::netmd::interface; use minidisc_rs::netmd::interface;
use nusb; use cross_usb::usb::Device;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let devices = nusb::list_devices().unwrap(); let device = cross_usb::context::get_device(0x054c, 0x0186).await.unwrap();
for device in devices { dbg!(device.vendor_id().await);
// Ensure the player is a minidisc player and not some other random device
let mut player_controller = match interface::NetMDInterface::new(&device).await {
Ok(player) => player,
Err(err) => continue,
};
// Ensure the player is a minidisc player and not some other random device
let mut player_controller = match interface::NetMDInterface::new(&device).await {
Ok(player) => player,
Err(err) => {
dbg!(err);
panic!();
},
};
println!(
"Player Model: {}",
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: {} | {}",
half_title,
full_title
);
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!( println!(
"Connected to VID: {:04x}, PID: {:04x}", "Track {i} Info:\n Title: {track} | {}\n Length: {:?}",
device.vendor_id(), track_titlesw[i], track_lengths[i]
device.product_id(),
); );
println!(
"Player Model: {}",
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: {} | {}",
half_title,
full_title
);
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: {:?}",
track_titlesw[i], track_lengths[i]
);
}
println!("{:?}", now.elapsed());
} }
println!("{:?}", now.elapsed());
} }