1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
use alloc::vec::Vec;
use super::DeviceID;
use crate::parse_error::*;
use crate::util::*;
use bstr::BString;
/// Used to transmit general file data.
/// Used by [`UniversalNonRealTimeMsg`](crate::UniversalNonRealTimeMsg).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FileDumpMsg {
/// Request that the file with `name` be sent.
Request {
requester_device: DeviceID,
file_type: FileType,
name: BString,
},
/// The header of the file about to be sent.
Header {
sender_device: DeviceID,
file_type: FileType,
/// Actual (un-encoded) file length, 28 bits (0-2684354561)
length: u32,
name: BString,
},
/// A packet of the file being sent.
///
/// Use `FileDumpMsg::packet` to construct
Packet {
/// Running packet count, 0-127. Wraps back to 0
running_count: u8,
/// At most 112 bytes (full 8 bits may be used)
data: Vec<u8>,
},
}
impl FileDumpMsg {
pub(crate) fn extend_midi(&self, v: &mut Vec<u8>) {
match self {
Self::Header {
sender_device,
file_type,
length,
name,
} => {
v.push(01);
v.push(sender_device.to_u8());
file_type.extend_midi(v);
push_u28(*length, v);
v.extend_from_slice(name);
}
Self::Packet {
running_count,
data,
..
} => {
v.push(02);
v.push(to_u7(*running_count));
let mut len = data.len().min(112);
// Add number of extra encoded bytes
// (/ 7 is -1 of actual number of encoded bytes, but it's sent as length - 1)
len += len / 7;
assert!(len < 128);
v.push(len as u8);
v.extend(Self::encode_data(data));
v.push(0); // Checksum <- Will be written over by `SystemExclusiveMsg.extend_midi`
}
Self::Request {
requester_device,
file_type,
name,
} => {
v.push(03);
v.push(requester_device.to_u8());
file_type.extend_midi(v);
v.extend_from_slice(name);
}
}
}
/// Construct a packet of up to 112 (full) bytes.
/// `num` is the number of this packet.
pub fn packet(num: u32, data: Vec<u8>) -> Self {
Self::Packet {
running_count: (num % 128) as u8,
data,
}
}
fn encode_data(data: &[u8]) -> Vec<u8> {
let mut r = Vec::with_capacity(128);
let mut d = 0; // Data position
let mut e = 0; // Encoded position
loop {
if e >= 128 || d >= data.len() {
break;
}
r.push(0); // First bits
let mut j = 0;
loop {
if j >= 7 || d + j >= data.len() {
break;
}
r[e] += (data[d + j] >> 7) << (6 - j);
r.push(data[d + j] & 0b01111111);
j += 1;
}
e += 8;
d += j;
}
r
}
#[allow(dead_code)]
pub(crate) fn from_midi(_m: &[u8]) -> Result<(Self, usize), ParseError> {
Err(ParseError::NotImplemented("FileDumpMsg"))
}
}
/// A four-character file type used by [`FileDumpMsg`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FileType {
MIDI,
MIEX,
ESEQ,
TEXT,
BIN,
MAC,
Custom([u8; 4]),
}
impl FileType {
fn extend_midi(&self, v: &mut Vec<u8>) {
match self {
Self::MIDI => b"MIDI".iter().for_each(|c| v.push(*c)),
Self::MIEX => b"MIEX".iter().for_each(|c| v.push(*c)),
Self::ESEQ => b"ESEQ".iter().for_each(|c| v.push(*c)),
Self::TEXT => b"TEXT".iter().for_each(|c| v.push(*c)),
Self::BIN => b"BIN ".iter().for_each(|c| v.push(*c)),
Self::MAC => b"MAC ".iter().for_each(|c| v.push(*c)),
Self::Custom(chars) => chars[0..4].iter().for_each(|c| v.push(*c)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::*;
use alloc::vec;
#[test]
fn encode_data() {
assert_eq!(
FileDumpMsg::encode_data(&[
0b11111111, 0b10101010, 0b00000000, 0b01010101, 0b11111111, 0b10101010, 0b00000000,
0b11010101
]),
vec![
0b01100110, 0b01111111, 0b00101010, 0b00000000, 0b01010101, 0b01111111, 0b00101010,
0b00000000, 0b01000000, 0b01010101
]
);
}
#[test]
fn serialize_file_dump_packet() {
let packet_msg = MidiMsg::SystemExclusive {
msg: SystemExclusiveMsg::UniversalNonRealTime {
device: DeviceID::AllCall,
msg: UniversalNonRealTimeMsg::FileDump(FileDumpMsg::packet(
129,
vec![
0b11111111, 0b10101010, 0b00000000, 0b01010101, 0b11111111, 0b10101010,
0b00000000, 0b11010101,
],
)),
},
}
.to_midi();
assert_eq!(packet_msg.len(), 19);
assert_eq!(&packet_msg[0..7], &[0xF0, 0x7E, 0x7F, 0x07, 0x02, 0x01, 9]);
assert_eq!(
&packet_msg[7..17],
&[
0b01100110, 0b01111111, 0b00101010, 0b00000000, 0b01010101, 0b01111111, 0b00101010,
0b00000000, 0b01000000, 0b01010101
]
);
assert_eq!(
packet_msg[17], // Checksum
checksum(&[
0x7E, 0x7F, 0x07, 0x02, 0x01, 9, 0b01100110, 0b01111111, 0b00101010, 0b00000000,
0b01010101, 0b01111111, 0b00101010, 0b00000000, 0b01000000, 0b01010101
])
);
}
#[test]
fn serialize_file_dump_header() {
assert_eq!(
MidiMsg::SystemExclusive {
msg: SystemExclusiveMsg::UniversalNonRealTime {
device: DeviceID::AllCall,
msg: UniversalNonRealTimeMsg::FileDump(FileDumpMsg::Header {
sender_device: DeviceID::Device(9),
file_type: FileType::MIDI,
length: 66,
name: BString::from("Hello"),
}),
},
}
.to_midi(),
vec![
0xF0,
0x7E,
0x7F, // Receiver device
07,
01,
9, // Sender device
b"M"[0],
b"I"[0],
b"D"[0],
b"I"[0],
66, // Size LSB
0,
0,
0,
b"H"[0],
b"e"[0],
b"l"[0],
b"l"[0],
b"o"[0],
0xF7
]
);
}
}