Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ edition = "2021"
all-features = true

[features]
default = []
default = ["heapless"]
xmodem = []
ymodem = []
zmodem = []
alloc = []

[dependencies]
core2 = { version = "0.4.0", default-features = false }
crc16 = "0.4.0"
thiserror-no-std = "2.0.2"
anyhow = { version = "1.0.75", default-features = false }
heapless = { version = "0.8.0", optional = true }
16 changes: 11 additions & 5 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#![allow(dead_code)]

#[cfg(feature = "alloc")]
extern crate alloc;

use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use alloc::string::String;
#[cfg(not(feature = "alloc"))]
use heapless::String;

use anyhow::Result;
use core2::io::{Error, Read, Write};
Expand Down Expand Up @@ -34,7 +37,7 @@ pub enum ModemError {
/// The number of communications errors exceeded `max_errors` in a single
/// transmission.
#[error("Too many errors, aborting - max errors: {errors}")]
ExhaustedRetries { errors: Box<u32> },
ExhaustedRetries { errors: u32 },

/// The transmission was canceled by the other end of the channel.
#[error("Cancelled by the other party.")]
Expand Down Expand Up @@ -142,14 +145,16 @@ pub trait YModemTrait: ModemTrait {
&mut self,
dev: &mut D,
out: &mut W,
file_name: &mut String,
#[cfg(feature = "alloc")] file_name: &mut String,
#[cfg(not(feature = "alloc"))] file_name: &mut String<128>,
file_size: &mut u32,
) -> ModemResult<()>;
fn send<D: Read + Write, R: Read>(
&mut self,
dev: &mut D,
inp: &mut R,
file_name: String,
#[cfg(feature = "alloc")] file_name: String,
#[cfg(not(feature = "alloc"))] file_name: String<128>,
file_size: u64,
) -> ModemResult<()>;
fn send_stream<D: Read + Write, R: Read>(
Expand All @@ -162,7 +167,8 @@ pub trait YModemTrait: ModemTrait {
fn send_start_frame<D: Read + Write>(
&mut self,
dev: &mut D,
file_name: String,
#[cfg(feature = "alloc")] file_name: String,
#[cfg(not(feature = "alloc"))] file_name: String<128>,
file_size: u64,
) -> ModemResult<()>;
fn send_end_frame<D: Read + Write>(
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
variant_size_differences
)]

#[cfg(feature = "alloc")]
extern crate alloc;

mod common;
Expand Down
40 changes: 33 additions & 7 deletions src/variants/api/xmodem.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use alloc::{boxed::Box, vec, vec::Vec};
use core::convert::From;

use crate::common::{
calc_checksum, calc_crc, get_byte, get_byte_timeout, ModemError,
ModemResult, ModemTrait, XModemTrait,
};
#[cfg(feature = "alloc")]
use alloc::{vec, vec::Vec};
use core::convert::From;
use core2::io::{Read, Write};
#[cfg(not(feature = "alloc"))]
use heapless::Vec;

use crate::variants::xmodem::{
common::{BlockLengthKind, ChecksumKind},
Expand Down Expand Up @@ -101,8 +103,14 @@ impl XModemTrait for XModem {
// We'll respond with cancel later if the packet number is wrong
let cancel_packet =
packet_num != pnum || (255 - pnum) != pnum_1c;
#[cfg(feature = "alloc")]
let mut data: Vec<u8> = Vec::new();
#[cfg(not(feature = "alloc"))]
let mut data: Vec<u8, 1024> = Vec::new();
#[cfg(feature = "alloc")]
data.resize(packet_size, 0);
#[cfg(not(feature = "alloc"))]
data.resize(packet_size, 0).unwrap_or_default();
dev.read_exact(&mut data)?;
let success = match self.checksum_mode {
ChecksumKind::Standard => {
Expand Down Expand Up @@ -144,7 +152,7 @@ impl XModemTrait for XModem {
if self.errors >= self.max_errors {
dev.write_all(&[Consts::CAN.into()])?;
return Err(ModemError::ExhaustedRetries {
errors: Box::from(self.errors),
errors: self.errors,
});
}
}
Expand Down Expand Up @@ -183,7 +191,7 @@ impl XModemTrait for XModem {
if self.errors >= self.max_errors {
// FIXME: Removed a unused 'if let' here. To be re-added?
return Err(ModemError::ExhaustedRetries {
errors: Box::from(self.errors),
errors: self.errors,
});
}
}
Expand All @@ -208,7 +216,7 @@ impl XModemTrait for XModem {

if self.errors >= self.max_errors {
return Err(ModemError::ExhaustedRetries {
errors: Box::from(self.errors),
errors: self.errors,
});
}
}
Expand All @@ -221,7 +229,16 @@ impl XModemTrait for XModem {
{
let mut block_num = 0u32;
loop {
#[cfg(feature = "alloc")]
let mut buff = vec![self.pad_byte; self.block_length as usize + 3];
#[cfg(not(feature = "alloc"))]
let mut buff: Vec<u8, 1029> = Vec::new();
#[cfg(not(feature = "alloc"))]
buff.resize(self.block_length as usize + 3, self.pad_byte)
.unwrap_or_default();

#[cfg(feature = "alloc")]
buff.resize(self.block_length as usize + 3, self.pad_byte);
let n = inp.read(&mut buff[3..])?;
if n == 0 {
return Ok(());
Expand All @@ -238,12 +255,21 @@ impl XModemTrait for XModem {
match self.checksum_mode {
ChecksumKind::Standard => {
let checksum = calc_checksum(&buff[3..]);
#[cfg(feature = "alloc")]
buff.push(checksum);
#[cfg(not(feature = "alloc"))]
buff.push(checksum).unwrap_or_default();
}
ChecksumKind::Crc16 => {
let crc = calc_crc(&buff[3..]);
#[cfg(feature = "alloc")]
buff.push(((crc >> 8) & 0xFF) as u8);
#[cfg(feature = "alloc")]
buff.push((&crc & 0xFF) as u8);
#[cfg(not(feature = "alloc"))]
buff.push(((crc >> 8) & 0xFF) as u8).unwrap_or_default();
#[cfg(not(feature = "alloc"))]
buff.push((&crc & 0xFF) as u8).unwrap_or_default();
}
}

Expand All @@ -260,7 +286,7 @@ impl XModemTrait for XModem {

if self.errors >= self.max_errors {
return Err(ModemError::ExhaustedRetries {
errors: Box::from(self.errors),
errors: self.errors,
});
}
}
Expand Down