use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
pub(crate) const BINARY_MODE: &[u8] = b"N#";
pub(crate) const VERSION: &[u8] = b"V#";
pub(crate) const IDENTIFY: &[u8] = b"I#";
pub(crate) const RESET: &[u8] = b"K#";
pub(crate) fn erase(address: u32) -> Vec<u8> {
format!("X{address:08X}#").into_bytes()
}
pub(crate) fn send(buffer: u32, size: u32) -> Vec<u8> {
format!("S{buffer:08X},{size:08X}#").into_bytes()
}
pub(crate) fn select_buffer(buffer: u32) -> Vec<u8> {
format!("Y{buffer:08X},0#").into_bytes()
}
pub(crate) fn copy(address: u32, size: u32) -> Vec<u8> {
format!("Y{address:08X},{size:08X}#").into_bytes()
}
pub(crate) fn checksum(address: u32, size: u32) -> Vec<u8> {
format!("Z{address:08X},{size:08X}#").into_bytes()
}
pub(crate) fn read_word(address: u32) -> Vec<u8> {
format!("w{address:08X},4#").into_bytes()
}
pub(crate) fn write_word(address: u32, value: u32) -> Vec<u8> {
format!("W{address:08X},{value:08X}#").into_bytes()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct Capabilities {
pub identify: bool,
pub reset: bool,
pub erase: bool,
pub write_buffer: bool,
pub checksum: bool,
}
pub(crate) fn capabilities(version: &str) -> Option<Capabilities> {
const OPENING: &str = "[Arduino:";
let start = version.find(OPENING)? + OPENING.len();
let letters = version.get(start..)?;
let letters = &letters[..letters.find(']')?];
let mut found = Capabilities::default();
for letter in letters.chars() {
match letter {
'I' => found.identify = true,
'K' => found.reset = true,
'X' => found.erase = true,
'Y' => found.write_buffer = true,
'Z' => found.checksum = true,
_ => {}
}
}
Some(found)
}
pub(crate) fn line(reply: &[u8]) -> String {
reply
.iter()
.take_while(|byte| (0x20..0x7F).contains(*byte))
.map(|&byte| char::from(byte))
.collect()
}
pub(crate) fn parse_checksum(reply: &[u8]) -> Option<u16> {
let rest = reply.strip_prefix(b"Z")?;
let digits = rest.get(..8)?;
if rest.get(8) != Some(&b'#') || !digits.iter().all(u8::is_ascii_hexdigit) {
return None;
}
let value = u32::from_str_radix(core::str::from_utf8(digits).ok()?, 16).ok()?;
u16::try_from(value).ok()
}
pub fn crc16(data: &[u8]) -> u16 {
const XMODEM: crc::Crc<u16> = crc::Crc::<u16>::new(&crc::CRC_16_XMODEM);
XMODEM.checksum(data)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn formats_commands_as_bossac_sends_them() {
assert_eq!(erase(0), b"X00000000#");
assert_eq!(send(0x34, 0x1000), b"S00000034,00001000#");
assert_eq!(select_buffer(0x34), b"Y00000034,0#");
assert_eq!(copy(0x1A000, 0x1000), b"Y0001A000,00001000#");
assert_eq!(checksum(0x25000, 0x800), b"Z00025000,00000800#");
assert_eq!(read_word(0x4100_2018), b"w41002018,4#");
assert_eq!(write_word(0xE000_ED0C, 0x05FA_0004), b"WE000ED0C,05FA0004#");
}
#[test]
fn reads_the_extensions() {
let all = capabilities("Arduino Bootloader (SAM-BA extended) 2.0 [Arduino:IKXYZ]");
assert_eq!(
all,
Some(Capabilities {
identify: true,
reset: true,
erase: true,
write_buffer: true,
checksum: true
})
);
let samd = capabilities("v2.0 [Arduino:XYZP] Nov 23 2026 12:00:00");
assert_eq!(
samd,
Some(Capabilities {
identify: false,
reset: false,
erase: true,
write_buffer: true,
checksum: true
})
);
assert_eq!(capabilities("v1.1 Dec 15 2010"), None);
assert_eq!(capabilities("[Arduino:XYZ"), None);
}
#[test]
fn crc16_is_xmodem() {
assert_eq!(crc16(b"123456789"), 0x31C3);
assert_eq!(crc16(&[]), 0);
assert_eq!(crc16(&[0xFF; 4096]), 0xE03E);
}
#[test]
fn parses_checksum_replies() {
assert_eq!(parse_checksum(b"Z0000D5B0#\n\r"), Some(0xD5B0));
assert_eq!(parse_checksum(b"Z0000d5b0#"), Some(0xD5B0));
assert_eq!(parse_checksum(b"Z0001D5B0#"), None);
assert_eq!(parse_checksum(b"Z00D5B0#"), None);
assert_eq!(parse_checksum(b"Y\n\r"), None);
assert_eq!(parse_checksum(b"Z0000G5B0#"), None);
assert_eq!(parse_checksum(b"Z+000D5B0#"), None);
}
#[test]
fn keeps_the_printable_line() {
assert_eq!(line(b"nRF52840-QIAA\n\r"), "nRF52840-QIAA");
assert_eq!(line(b""), "");
}
}