Compare commits

..

No commits in common. "a3a3ee382749c96dd6aa7af555cecfe992eaac2e" and "27853d9ea045e98d3a353cedcb5a33ed632c3411" have entirely different histories.

8 changed files with 180 additions and 195 deletions

View file

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

View file

@ -10,9 +10,9 @@ license = "AGPL-3.0"
[dependencies] [dependencies]
hex = "0.4.3" hex = "0.4.3"
cross_usb = "0.1" nusb = "0.1.4"
tokio = { version = "1.35.1", features = ["macros", "rt", "rt-multi-thread"] } 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"
cross_usb = "0.1" nusb = "0.1.4"
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,12 +31,5 @@ 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,16 +1,11 @@
#![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;
// USB stuff // USB stuff
//use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer}; use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer};
use cross_usb::{UsbDevice, UsbInterface}; use nusb::{Device, DeviceInfo, Interface};
use cross_usb::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
use super::utils::cross_sleep;
//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;
@ -90,7 +85,8 @@ pub struct DeviceId {
/// A connection to a NetMD device /// A connection to a NetMD device
pub struct NetMD { pub struct NetMD {
usb_interface: UsbInterface, usb_device: Device,
usb_interface: Interface,
model: DeviceId, model: DeviceId,
status: Option<Status>, status: Option<Status>,
} }
@ -99,10 +95,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(usb_device: &UsbDevice) -> Result<Self, Box<dyn Error>> { pub async fn new(device_info: &DeviceInfo) -> Result<Self, Box<dyn Error>> {
let mut model = DeviceId { let mut model = DeviceId {
vendor_id: usb_device.vendor_id().await, vendor_id: device_info.vendor_id(),
product_id: usb_device.product_id().await, product_id: device_info.product_id(),
name: None, name: None,
}; };
@ -120,9 +116,11 @@ impl NetMD {
Some(_) => (), Some(_) => (),
} }
let usb_interface = usb_device.open_interface(0).await?; let usb_device = device_info.open()?;
let usb_interface = usb_device.claim_interface(0)?;
Ok(Self { Ok(Self {
usb_device,
usb_interface, usb_interface,
model, model,
status: None, status: None,
@ -159,9 +157,10 @@ impl NetMD {
length: 4, length: 4,
}) })
.await .await
.into_result()
{ {
Ok(size) => size, Ok(size) => size,
Err(error) => return Err(error), Err(error) => return Err(error.into()),
}; };
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]]);
@ -213,9 +212,10 @@ impl NetMD {
data: &command, data: &command,
}) })
.await .await
.into_result()
{ {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(error) => Err(error), Err(error) => Err(error.into()),
} }
} }
@ -239,24 +239,17 @@ impl NetMD {
use_factory_command: bool, use_factory_command: bool,
override_length: Option<i32>, override_length: Option<i32>,
) -> Result<Vec<u8>, Box<dyn Error>> { ) -> Result<Vec<u8>, Box<dyn Error>> {
let mut length = 0; let mut length = self.poll().await?.0;
for attempt in 0..40 {
if attempt == 39 {
return Err("Failed to get response length".into());
}
length = self.poll().await?.0;
if length > 0 {
break
}
let mut current_attempt = 0;
while length == 0 {
// Back off while trying again // Back off while trying again
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64
* (u32::pow(2, attempt / 10) - 1); * (u64::pow(2, current_attempt as u32 / 10) - 1);
cross_sleep(sleep_time).await; std::thread::sleep(std::time::Duration::from_millis(sleep_time));
length = self.poll().await?.0;
current_attempt += 1;
} }
if let Some(value) = override_length { if let Some(value) = override_length {
@ -279,12 +272,14 @@ 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,
@ -306,11 +301,13 @@ 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, to_read) .bulk_in(BULK_READ_ENDPOINT, buffer)
.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()),
@ -322,7 +319,12 @@ impl NetMD {
Ok(final_result) Ok(final_result)
} }
pub async fn write_bulk(&mut self, data: &[u8]) -> Result<usize, Box<dyn Error>> { pub async fn write_bulk(&mut self, data: Vec<u8>) -> Result<usize, Box<dyn Error>> {
self.usb_interface.bulk_out(BULK_WRITE_ENDPOINT, data).await Ok(self
.usb_interface
.bulk_out(BULK_WRITE_ENDPOINT, data)
.await
.into_result()?
.actual_length())
} }
} }

View file

@ -1,10 +1,8 @@
#![cfg_attr(debug_assertions, allow(dead_code))] use std::{error::Error, thread::sleep, time::Duration};
use std::error::Error;
use num_derive::FromPrimitive; use num_derive::FromPrimitive;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
use super::interface::{NetMDInterface, MDTrack}; use super::interface::{NetMDInterface, MDTrack};
use super::utils::cross_sleep;
#[derive(FromPrimitive)] #[derive(FromPrimitive)]
#[derive(PartialEq)] #[derive(PartialEq)]
@ -59,20 +57,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.unwrap_or(OperatingStatus::NoDisc)) { while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(&device_status(interface).await?.state.or(Some(OperatingStatus::NoDisc)).unwrap()) {
cross_sleep(200).await; sleep(Duration::from_millis(200));
} }
let _ = interface.session_key_forget().await; let _ = interface.session_key_forget();
let _ = interface.leave_secure_session().await; let _ = interface.leave_secure_session();
interface.acquire().await?; interface.acquire().await?;
let _ = interface.disable_new_track_protection(1).await; let _ = interface.disable_new_track_protection(1);
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,4 +1,3 @@
#![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::{
@ -13,8 +12,8 @@ use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::thread::sleep;
use super::utils::cross_sleep; use std::time::Duration;
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
enum Action { enum Action {
@ -138,7 +137,7 @@ enum DescriptorAction {
} }
#[repr(u8)] #[repr(u8)]
enum NetmdStatus { enum Status {
// NetMD Protocol return status (first byte of request) // NetMD Protocol return status (first byte of request)
Control = 0x00, Control = 0x00,
Status = 0x01, Status = 0x01,
@ -164,23 +163,23 @@ lazy_static! {
]); ]);
} }
impl std::convert::TryFrom<u8> for NetmdStatus { impl std::convert::TryFrom<u8> for Status {
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(NetmdStatus::Control), 0x00 => Ok(Status::Control),
0x01 => Ok(NetmdStatus::Status), 0x01 => Ok(Status::Status),
0x02 => Ok(NetmdStatus::SpecificInquiry), 0x02 => Ok(Status::SpecificInquiry),
0x03 => Ok(NetmdStatus::Notify), 0x03 => Ok(Status::Notify),
0x04 => Ok(NetmdStatus::GeneralInquiry), 0x04 => Ok(Status::GeneralInquiry),
0x08 => Ok(NetmdStatus::NotImplemented), 0x08 => Ok(Status::NotImplemented),
0x09 => Ok(NetmdStatus::Accepted), 0x09 => Ok(Status::Accepted),
0x0a => Ok(NetmdStatus::Rejected), 0x0a => Ok(Status::Rejected),
0x0b => Ok(NetmdStatus::InTransition), 0x0b => Ok(Status::InTransition),
0x0c => Ok(NetmdStatus::Implemented), 0x0c => Ok(Status::Implemented),
0x0d => Ok(NetmdStatus::Changed), 0x0d => Ok(Status::Changed),
0x0f => Ok(NetmdStatus::Interim), 0x0f => Ok(Status::Interim),
_ => Err("Not a valid value".into()), _ => Err("Not a valid value".into()),
} }
} }
@ -204,7 +203,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: &cross_usb::UsbDevice) -> Result<Self, Box<dyn Error>> { pub async fn new(device: &nusb::DeviceInfo) -> 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 })
} }
@ -224,7 +223,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![])?;
@ -292,7 +291,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).await; self.change_descriptor_state(&Descriptor::DiscSubunitIdentifier, &DescriptorAction::Close);
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 {
@ -322,7 +321,7 @@ impl NetMDInterface {
Ok(result) Ok(result)
} }
async fn change_descriptor_state(&mut self, descriptor: &Descriptor, action: &DescriptorAction) { 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());
@ -331,7 +330,7 @@ impl NetMDInterface {
query.push(0x00); query.push(0x00);
let _ = self.send_query(&mut query, false, false).await; let _ = self.send_query(&mut query, false, false);
} }
/// Send a query to the NetMD player /// Send a query to the NetMD player
@ -354,8 +353,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 => NetmdStatus::GeneralInquiry, true => Status::GeneralInquiry,
false => NetmdStatus::Control, false => Status::Control,
}; };
let mut new_query = Vec::new(); let mut new_query = Vec::new();
@ -378,24 +377,23 @@ impl NetMDInterface {
Err(error) => return Err(error), Err(error) => return Err(error),
}; };
let status = match NetmdStatus::try_from(data[0]) { let status = match Status::try_from(data[0]) {
Ok(status) => status, Ok(status) => status,
Err(error) => return Err(error), Err(error) => return Err(error),
}; };
match status { match status {
NetmdStatus::NotImplemented => return Err("Not implemented".into()), Status::NotImplemented => return Err("Not implemented".into()),
NetmdStatus::Rejected => return Err("Rejected".into()), Status::Rejected => return Err("Rejected".into()),
NetmdStatus::Interim if !accept_interim => { Status::Interim if !accept_interim => {
let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL as u64
* (u32::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);
cross_sleep(sleep_time).await; std::thread::sleep(sleep_dur); // Sleep to wait before retrying
current_attempt += 1; current_attempt += 1;
continue; // Retry! continue; // Retry!
} }
NetmdStatus::Accepted | NetmdStatus::Implemented | NetmdStatus::Interim => { Status::Accepted | Status::Implemented | Status::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());
} }
@ -472,7 +470,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(),
@ -486,7 +484,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).await; self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close);
let final_array = res[0].to_vec().unwrap(); let final_array = res[0].to_vec().unwrap();
@ -506,7 +504,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![],
@ -522,7 +520,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).await; self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close);
if operating_status.len() < 2 { if operating_status.len() < 2 {
return Err("Unparsable operating system".into()); return Err("Unparsable operating system".into());
@ -544,7 +542,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)],
@ -558,7 +556,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).await; self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close);
Ok(res.unwrap()[0].to_vec().unwrap()) Ok(res.unwrap()[0].to_vec().unwrap())
} }
@ -575,7 +573,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(),
@ -599,7 +597,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).await; self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close);
Ok(final_result) Ok(final_result)
} }
@ -706,21 +704,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).await; self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead);
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).await; self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::Close);
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).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead);
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();
@ -733,14 +731,14 @@ impl NetMDInterface {
) )
.unwrap(); .unwrap();
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close);
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).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead);
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead).await; self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead);
let mut done: i32 = 0; let mut done: i32 = 0;
let mut remaining: i32 = 0; let mut remaining: i32 = 0;
@ -794,8 +792,8 @@ impl NetMDInterface {
let res = result.join(""); let res = result.join("");
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close).await; self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close);
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close);
Ok(res) Ok(res)
} }
@ -928,7 +926,7 @@ impl NetMDInterface {
false => Descriptor::AudioUTOC1TD, false => Descriptor::AudioUTOC1TD,
}; };
self.change_descriptor_state(&descriptor_type, &DescriptorAction::OpenRead).await; self.change_descriptor_state(&descriptor_type, &DescriptorAction::OpenRead);
let mut track_titles: Vec<String> = vec![]; let mut track_titles: Vec<String> = vec![];
for i in tracks { for i in tracks {
@ -957,7 +955,7 @@ impl NetMDInterface {
) )
} }
self.change_descriptor_state(&descriptor_type, &DescriptorAction::Close).await; self.change_descriptor_state(&descriptor_type, &DescriptorAction::Close);
Ok(track_titles) Ok(track_titles)
} }
@ -975,7 +973,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: &str, title: String,
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?;
@ -988,7 +986,7 @@ impl NetMDInterface {
let wchar_value = match wchar { let wchar_value = match wchar {
true => { true => {
new_title = sanitize_full_width_title(title, false); new_title = sanitize_full_width_title(&title, false);
1 1
} }
false => { false => {
@ -1000,10 +998,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).await self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::OpenWrite)
} else { } else {
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close).await; self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close);
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenWrite).await self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenWrite)
} }
let mut query = format_query( let mut query = format_query(
@ -1016,14 +1014,14 @@ impl NetMDInterface {
], ],
)?; )?;
let _ = self.send_query(&mut query, false, false).await; let _ = self.send_query(&mut query, false, false);
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).await self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::Close)
} else { } else {
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close).await; self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close);
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead).await; self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::OpenRead);
self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close).await; self.change_descriptor_state(&Descriptor::DiscTitleTD, &DescriptorAction::Close);
} }
Ok(()) Ok(())
@ -1033,17 +1031,17 @@ impl NetMDInterface {
pub async fn set_track_title( pub async fn set_track_title(
&mut self, &mut self,
track: u16, track: u16,
title: &str, title: String,
wchar: bool, wchar: bool,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let new_title: Vec<u8>; let new_title: Vec<u8>;
let (wchar_value, descriptor) = match wchar { let (wchar_value, descriptor) = match wchar {
true => { true => {
new_title = sanitize_full_width_title(title, false); new_title = sanitize_full_width_title(&title, false);
(3, Descriptor::AudioUTOC4TD) (3, Descriptor::AudioUTOC4TD)
} }
false => { false => {
new_title = sanitize_half_width_title(title); new_title = sanitize_half_width_title(title.clone());
(2, Descriptor::AudioUTOC1TD) (2, Descriptor::AudioUTOC1TD)
} }
}; };
@ -1061,7 +1059,7 @@ impl NetMDInterface {
Err(error) => return Err(error), Err(error) => return Err(error),
}; };
self.change_descriptor_state(&descriptor, &DescriptorAction::OpenWrite).await; self.change_descriptor_state(&descriptor, &DescriptorAction::OpenWrite);
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![
@ -1078,7 +1076,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).await; self.change_descriptor_state(&descriptor, &DescriptorAction::Close);
Ok(()) Ok(())
} }
@ -1116,7 +1114,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).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead);
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(),
@ -1133,7 +1131,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).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close);
Ok(res[0].to_vec().unwrap()) Ok(res[0].to_vec().unwrap())
} }
@ -1145,7 +1143,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).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead);
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(),
@ -1177,7 +1175,7 @@ impl NetMDInterface {
times.push(length); times.push(length);
} }
self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close);
Ok(times) Ok(times)
} }
@ -1208,7 +1206,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).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::OpenRead);
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)],
@ -1217,14 +1215,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).await; self.change_descriptor_state(&Descriptor::AudioContentsTD, &DescriptorAction::Close);
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).await; self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::OpenRead);
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];
@ -1247,7 +1245,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).await; self.change_descriptor_state(&Descriptor::RootTD, &DescriptorAction::Close);
Ok(result) Ok(result)
} }
@ -1256,7 +1254,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![],
@ -1266,7 +1264,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).await; self.change_descriptor_state(&Descriptor::OperatingStatusBlock, &DescriptorAction::Close);
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())
} }
@ -1504,7 +1502,7 @@ impl NetMDInterface {
} }
// Sharps are slow // Sharps are slow
cross_sleep(200).await; sleep(Duration::from_millis(200));
let total_bytes = pkt_size + 24; //framesizedict[wireformat] * frames + pktcount * 24; let total_bytes = pkt_size + 24; //framesizedict[wireformat] * frames + pktcount * 24;
@ -1524,7 +1522,7 @@ impl NetMDInterface {
)?; )?;
// Sharps are slow // Sharps are slow
cross_sleep(200).await; sleep(Duration::from_millis(200));
let mut _written_bytes = 0; let mut _written_bytes = 0;
for (packet_count, (key, iv, data)) in packets.into_iter().enumerate() { for (packet_count, (key, iv, data)) in packets.into_iter().enumerate() {
@ -1534,7 +1532,7 @@ impl NetMDInterface {
} else { } else {
data.clone() data.clone()
}; };
self.net_md_device.write_bulk(&binpack).await?; self.net_md_device.write_bulk(binpack).await?;
_written_bytes += data.len(); _written_bytes += data.len();
} }

View file

@ -8,15 +8,6 @@ use unicode_normalization::UnicodeNormalization;
extern crate kana; extern crate kana;
use kana::*; use kana::*;
/// Sleep for a specified number of milliseconds on any platform
pub async fn cross_sleep(millis: u32) {
#[cfg(not(target_family = "wasm"))]
std::thread::sleep(std::time::Duration::from_millis(millis as u64));
#[cfg(target_family = "wasm")]
gloo::timers::future::TimeoutFuture::new(millis).await;
}
pub fn bcd_to_int(mut bcd: i32) -> i32 { pub fn bcd_to_int(mut bcd: i32) -> i32 {
let mut value = 0; let mut value = 0;
let mut nibble = 0; let mut nibble = 0;
@ -98,13 +89,15 @@ fn check(string: String) -> Option<String> {
None None
} }
pub fn sanitize_half_width_title(title: &str) -> Vec<u8> { pub fn sanitize_half_width_title(mut title: String) -> Vec<u8> {
let mut string_title = wide2ascii(title); title = wide2ascii(&title);
string_title = nowidespace(&string_title); title = nowidespace(&title);
string_title = hira2kata(&string_title); title = hira2kata(&title);
string_title = combine(&string_title); title = combine(&title);
let new_title: String = string_title println!("{}", title);
let new_title: String = title
.chars() .chars()
.map(|c| { .map(|c| {
check(c.to_string()).unwrap_or( check(c.to_string()).unwrap_or(
@ -116,7 +109,7 @@ pub fn sanitize_half_width_title(title: &str) -> Vec<u8> {
let sjis_string = SHIFT_JIS.encode(&new_title).0; let sjis_string = SHIFT_JIS.encode(&new_title).0;
if validate_shift_jis(sjis_string.clone().into()) { if validate_shift_jis(sjis_string.clone().into()) {
return agressive_sanitize_title(title).into(); return agressive_sanitize_title(&title).into();
} }
sjis_string.into() sjis_string.into()

View file

@ -1,21 +1,23 @@
use minidisc_rs::netmd::interface; use minidisc_rs::netmd::interface;
use cross_usb::usb::Device; use nusb;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let device = cross_usb::get_device(0x054c, 0x0186).await.unwrap(); let devices = nusb::list_devices().unwrap();
dbg!(device.vendor_id().await);
for device in devices {
// Ensure the player is a minidisc player and not some other random device // Ensure the player is a minidisc player and not some other random device
let mut player_controller = match interface::NetMDInterface::new(&device).await { let mut player_controller = match interface::NetMDInterface::new(&device).await {
Ok(player) => player, Ok(player) => player,
Err(err) => { Err(err) => continue,
dbg!(err);
panic!();
},
}; };
println!(
"Connected to VID: {:04x}, PID: {:04x}",
device.vendor_id(),
device.product_id(),
);
println!( println!(
"Player Model: {}", "Player Model: {}",
player_controller player_controller
@ -47,4 +49,5 @@ async fn main() {
); );
} }
println!("{:?}", now.elapsed()); println!("{:?}", now.elapsed());
}
} }