Compare commits

...

13 commits

Author SHA1 Message Date
asivery
9bdd7ddb39 Fix playback_control 2024-03-10 16:08:51 +01:00
asivery
2e7b9c2dab Fix async errors 2024-03-10 15:52:00 +01:00
asivery
1d205dc012 Fix cross-thread error sending 2024-03-10 14:51:34 +01:00
asivery
79844cdb57 Make DeviceStatus' fields public 2024-03-10 14:03:40 +01:00
asivery
afd799d07d Change vendor_id and product_id functions to return clones of u16 2024-03-10 13:28:28 +01:00
asivery
3fd6bb4c4b Remove redundant async 2024-03-10 13:25:54 +01:00
04563f31d6 Added documentation to interface.rs, fixed track change methods 2024-03-10 06:17:58 -05:00
asivery
f1de5bff2a Remove redundant 'status' field. 2024-03-10 12:01:31 +01:00
asivery
ae6ae5ff9e Make base.rs public 2024-03-10 11:56:17 +01:00
1227591957 Changed name to minidisc-rs 2024-03-10 05:40:00 -05:00
G2
f9356fe368
Create rust.yml 2024-03-10 05:16:35 -05:00
G2
6df9c0e414
Update README.md 2024-03-10 05:06:08 -05:00
G2
3acafb285f
Update README.md 2024-03-10 05:05:20 -05:00
7 changed files with 94 additions and 46 deletions

24
.github/workflows/rust.yml vendored Normal file
View file

@ -0,0 +1,24 @@
name: Rust
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Run clippy
run: cargo clippy --verbose

View file

@ -1,5 +1,5 @@
[package] [package]
name = "minidisc_pc" name = "minidisc_rs"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
homepage = "https://github.com/G2-Games/minidisc-cli/" homepage = "https://github.com/G2-Games/minidisc-cli/"

View file

@ -1,3 +1,19 @@
# minidisc # Rust Minidisc
A library for controlling and interfacing with [MiniDisc](https://en.wikipedia.org/wiki/MiniDisc) devices from within Rust programs. Compatible with many cross platform targets (including Web Assembly!) by using [cross-usb](https://github.com/G2-Games/cross-usb).
Docs are a work in progress. The feature set is very similar to that of [netmd-js](https://github.com/cybercase/netmd-js) which this library is inspired by. For more information check out the absolutely awesome [Web Minidisc project](https://github.com/asivery/webminidisc), [NetMD-exploits](https://github.com/asivery/netmd-exploits), and the C based [Linux Minidisc project](https://github.com/linux-minidisc/linux-minidisc).
Documentation has not been finished and is a work in progress.
## Current Features
### NetMD
- [x] Track upload
- [x] Track management
- [x] Playback control
- [x] Track download ([MZ-RH1](https://www.minidisc.wiki/equipment/sony/portable/mz-rh1) only)
### Hi-MD
- [ ] Track upload
- [ ] Track management
- [ ] Playback control
- [ ] Track download

View file

@ -99,7 +99,6 @@ pub struct DeviceId {
pub struct NetMD { pub struct NetMD {
usb_interface: UsbInterface, usb_interface: UsbInterface,
model: DeviceId, model: DeviceId,
status: Option<Status>,
} }
impl NetMD { impl NetMD {
@ -132,23 +131,22 @@ impl NetMD {
Ok(Self { Ok(Self {
usb_interface, usb_interface,
model, model,
status: None,
}) })
} }
/// Gets the device name, this is limited to the devices in the list /// Gets the device name, this is limited to the devices in the list
pub async fn device_name(&self) -> &Option<String> { pub fn device_name(&self) -> &Option<String> {
&self.model.name &self.model.name
} }
/// Gets the vendor id /// Gets the vendor id
pub async fn vendor_id(&self) -> &u16 { pub fn vendor_id(&self) -> u16 {
&self.model.vendor_id self.model.vendor_id
} }
/// Gets the product id /// Gets the product id
pub async fn product_id(&self) -> &u16 { pub fn product_id(&self) -> u16 {
&self.model.product_id self.model.product_id
} }
/// Poll the device to get either the result /// Poll the device to get either the result

View file

@ -20,16 +20,16 @@ pub enum OperatingStatus {
} }
pub struct Time { pub struct Time {
minute: u16, pub minute: u16,
second: u16, pub second: u16,
frame: u16, pub frame: u16,
} }
pub struct DeviceStatus { pub struct DeviceStatus {
disc_present: bool, pub disc_present: bool,
state: Option<OperatingStatus>, pub state: Option<OperatingStatus>,
track: u8, pub track: u8,
time: Time, pub time: Time,
} }
pub async fn device_status(interface: &mut NetMDInterface) -> Result<DeviceStatus, Box<dyn Error>> { pub async fn device_status(interface: &mut NetMDInterface) -> Result<DeviceStatus, Box<dyn Error>> {

View file

@ -206,6 +206,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;
/// Get a new interface to a NetMD device
pub async fn new(device: &cross_usb::UsbDevice) -> Result<Self, Box<dyn Error>> { pub async fn new(device: &cross_usb::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 })
@ -419,7 +420,7 @@ impl NetMDInterface {
async fn playback_control(&mut self, action: Action) -> Result<(), Box<dyn Error>> { async fn playback_control(&mut self, action: Action) -> Result<(), Box<dyn Error>> {
let mut query = format_query( let mut query = format_query(
"18c3 00 %b 000000".to_string(), "18c3 ff %b 000000".to_string(),
vec![QueryValue::Number(action as i64)], vec![QueryValue::Number(action as i64)],
)?; )?;
@ -430,23 +431,28 @@ impl NetMDInterface {
Ok(()) Ok(())
} }
/// Begin playback or resume after paused
pub async fn play(&mut self) -> Result<(), Box<dyn Error>> { pub async fn play(&mut self) -> Result<(), Box<dyn Error>> {
self.playback_control(Action::Play).await self.playback_control(Action::Play).await
} }
/// Fast foward through the disc
pub async fn fast_forward(&mut self) -> Result<(), Box<dyn Error>> { pub async fn fast_forward(&mut self) -> Result<(), Box<dyn Error>> {
self.playback_control(Action::FastForward).await self.playback_control(Action::FastForward).await
} }
/// Rewind through the disc
pub async fn rewind(&mut self) -> Result<(), Box<dyn Error>> { pub async fn rewind(&mut self) -> Result<(), Box<dyn Error>> {
self.playback_control(Action::Rewind).await self.playback_control(Action::Rewind).await
} }
/// Pause playback
pub async fn pause(&mut self) -> Result<(), Box<dyn Error>> { pub async fn pause(&mut self) -> Result<(), Box<dyn Error>> {
self.playback_control(Action::Pause).await self.playback_control(Action::Pause).await
} }
//TODO: Implement fix for LAM-1 //TODO: Implement fix for LAM-1
/// Stop playback
pub async fn stop(&mut self) -> Result<(), Box<dyn Error>> { pub async fn stop(&mut self) -> Result<(), Box<dyn Error>> {
let mut query = format_query("18c5 ff 00000000".to_string(), vec![])?; let mut query = format_query("18c5 ff 00000000".to_string(), vec![])?;
@ -503,6 +509,7 @@ impl NetMDInterface {
Ok(final_array) Ok(final_array)
} }
/// Check if a disc is loaded in the player
pub async fn disc_present(&mut self) -> Result<bool, Box<dyn Error>> { pub async fn disc_present(&mut self) -> Result<bool, Box<dyn Error>> {
let status = self.status().await?; let status = self.status().await?;
@ -561,30 +568,30 @@ impl NetMDInterface {
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)],
) )?;
.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( let res = scan_query(
reply, reply,
"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; .await;
Ok(res.unwrap()[0].to_vec().unwrap()) Ok(res[0].to_vec().unwrap())
} }
pub async fn playback_status1(&mut self) -> Result<Vec<u8>, Box<dyn Error>> { pub async fn playback_status1(&mut self) -> Result<Vec<u8>, Box<dyn Error>> {
self.playback_status_query(0x8801, 0x8807).await Ok(self.playback_status_query(0x8801, 0x8807).await?)
} }
pub async fn playback_status2(&mut self) -> Result<Vec<u8>, Box<dyn Error>> { pub async fn playback_status2(&mut self) -> Result<Vec<u8>, Box<dyn Error>> {
self.playback_status_query(0x8802, 0x8806).await Ok(self.playback_status_query(0x8802, 0x8806).await?)
} }
/// Get the current playback position
pub async fn position(&mut self) -> Result<[u16; 5], Box<dyn Error>> { pub async fn position(&mut self) -> Result<[u16; 5], Box<dyn Error>> {
self.change_descriptor_state( self.change_descriptor_state(
&Descriptor::OperatingStatusBlock, &Descriptor::OperatingStatusBlock,
@ -620,6 +627,7 @@ impl NetMDInterface {
Ok(final_result) Ok(final_result)
} }
/// Eject the disc from the player if supported
pub async fn eject_disc(&mut self) -> Result<(), Box<dyn Error>> { pub async fn eject_disc(&mut self) -> Result<(), Box<dyn Error>> {
let mut query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap(); let mut query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap();
@ -627,6 +635,7 @@ impl NetMDInterface {
Ok(()) Ok(())
} }
/// Check if the machine has the capability to eject a disc
pub async fn can_eject_disc(&mut self) -> Result<bool, Box<dyn Error>> { pub async fn can_eject_disc(&mut self) -> Result<bool, Box<dyn Error>> {
let mut query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap(); let mut query = format_query("18c1 ff 6000".to_string(), vec![]).unwrap();
@ -636,7 +645,7 @@ impl NetMDInterface {
} }
} }
/* Track control */ /// Move the playback to a specific track
pub async fn go_to_track(&mut self, track_number: u16) -> Result<u16, Box<dyn Error>> { pub async fn go_to_track(&mut self, track_number: u16) -> Result<u16, Box<dyn Error>> {
let mut query = format_query( let mut query = format_query(
"1850 ff010000 0000 %w".to_string(), "1850 ff010000 0000 %w".to_string(),
@ -653,6 +662,7 @@ impl NetMDInterface {
Ok(value as u16) Ok(value as u16)
} }
/// Move the playback to a specific time
pub async fn go_to_time( pub async fn go_to_time(
&mut self, &mut self,
track_number: u16, track_number: u16,
@ -703,15 +713,15 @@ impl NetMDInterface {
/// Change to the next track (skip back) /// Change to the next track (skip back)
pub async fn previous_track(&mut self) -> Result<(), Box<dyn Error>> { pub async fn previous_track(&mut self) -> Result<(), Box<dyn Error>> {
self.track_change(Track::Next).await self.track_change(Track::Previous).await
} }
/// Change to the next track (skip to beginning of track) /// Change to the next track (skip to beginning of track)
pub async fn restart_track(&mut self) -> Result<(), Box<dyn Error>> { pub async fn restart_track(&mut self) -> Result<(), Box<dyn Error>> {
self.track_change(Track::Next).await self.track_change(Track::Restart).await
} }
/* Content access and control */ /// Erase the disc entirely
pub async fn erase_disc(&mut self) -> Result<(), Box<dyn Error>> { pub async fn erase_disc(&mut self) -> Result<(), Box<dyn Error>> {
let mut query = format_query("1840 ff 0000".to_string(), vec![]).unwrap(); let mut query = format_query("1840 ff 0000".to_string(), vec![]).unwrap();
let reply = self.send_query(&mut query, false, false).await?; let reply = self.send_query(&mut query, false, false).await?;
@ -758,6 +768,7 @@ impl NetMDInterface {
Ok(res[0].to_i64().unwrap() as u16) Ok(res[0].to_i64().unwrap() as u16)
} }
/// Gets the disc title as it is stored
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; .await;
@ -1021,7 +1032,7 @@ 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() == 0x04dd {
self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::OpenWrite) self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::OpenWrite)
.await .await
} else { } else {
@ -1043,7 +1054,7 @@ impl NetMDInterface {
let _ = self.send_query(&mut query, false, false).await; 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() == 0x04dd {
self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::Close) self.change_descriptor_state(&Descriptor::AudioUTOC1TD, &DescriptorAction::Close)
.await .await
} else { } else {
@ -1114,7 +1125,7 @@ impl NetMDInterface {
Ok(()) Ok(())
} }
/// Removes a track from the UTOC /// Erases a track from the disc's UTOC
pub async fn erase_track(&mut self, track: u16) -> Result<(), Box<dyn Error>> { pub async fn erase_track(&mut self, track: u16) -> Result<(), Box<dyn Error>> {
let mut query = format_query( let mut query = format_query(
"1840 ff01 00 201001 %w".to_string(), "1840 ff01 00 201001 %w".to_string(),
@ -1126,7 +1137,7 @@ impl NetMDInterface {
Ok(()) Ok(())
} }
/// Moves a track to another position on the disc /// Moves a track to another index on the disc
pub async fn move_track(&mut self, source: u16, dest: u16) -> Result<(), Box<dyn Error>> { pub async fn move_track(&mut self, source: u16, dest: u16) -> Result<(), Box<dyn Error>> {
let mut query = format_query( let mut query = format_query(
"1843 ff00 00 201001 %w 201001 %w".to_string(), "1843 ff00 00 201001 %w 201001 %w".to_string(),
@ -1141,6 +1152,7 @@ impl NetMDInterface {
Ok(()) Ok(())
} }
/// Raw information about a track
async fn raw_track_info( async fn raw_track_info(
&mut self, &mut self,
track: u16, track: u16,
@ -1171,7 +1183,7 @@ impl NetMDInterface {
Ok(res[0].to_vec().unwrap()) Ok(res[0].to_vec().unwrap())
} }
/// Gets the length of tracks as a `std::time::Duration` from a set /// Gets the length of tracks as a [std::time::Duration] from a set
pub async fn track_lengths( pub async fn track_lengths(
&mut self, &mut self,
tracks: Vec<u16>, tracks: Vec<u16>,
@ -1217,7 +1229,7 @@ impl NetMDInterface {
Ok(times) Ok(times)
} }
/// Gets the length of a track as a `std::time::Duration` /// Gets the length of a track as a [std::time::Duration]
pub async fn track_length( pub async fn track_length(
&mut self, &mut self,
track: u16, track: u16,
@ -1259,7 +1271,7 @@ impl NetMDInterface {
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; .await;
@ -1382,21 +1394,19 @@ impl NetMDInterface {
Ok(()) Ok(())
} }
/** /// Read the leaf ID of the present NetMD device. The leaf ID tells
* 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
* 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
* parts of the EKB needs to be sent to the device for it to decrypt /// the root key.
* the root key. ///
* /// The leaf ID is a 8-byte constant
* The leaf ID is a 8-byte constant pub async fn leaf_id(&mut self) -> Result<[u8; 8], Box<dyn Error>> {
**/
pub async fn leaf_id(&mut self) -> Result<Vec<u8>, Box<dyn Error>> {
let mut query = format_query("1800 080046 f0030103 11 ff".to_string(), vec![])?; let mut query = format_query("1800 080046 f0030103 11 ff".to_string(), vec![])?;
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, "1800 080046 f0030103 11 00 %*".to_string())?; let res = scan_query(reply, "1800 080046 f0030103 11 00 %*".to_string())?;
Ok(res[0].to_vec().unwrap()) Ok(res[0].to_vec().unwrap().try_into().unwrap())
} }
pub async fn send_key_data( pub async fn send_key_data(

View file

@ -4,7 +4,7 @@
* Documentation coming soon * Documentation coming soon
*/ */
mod base; pub mod base;
pub mod commands; pub mod commands;
pub mod encryption; pub mod encryption;
pub mod interface; pub mod interface;