#![no_std]
use consts::{DfuRequest, DfuStatus};
#[cfg(feature = "defmt")]
use defmt::{debug, error, info};
pub mod consts;
enum InternalState {
Idle,
DownloadBusy(DownloadState),
Download(DownloadState),
Upload { offset: usize },
Error,
}
impl Into<consts::State> for &InternalState {
fn into(self) -> consts::State {
use consts::State;
match self {
InternalState::Idle => State::DfuIdle,
InternalState::DownloadBusy(_) => State::DownloadBusy,
InternalState::Download(_) => State::DownloadIdle,
InternalState::Upload { .. } => State::UploadIdle,
InternalState::Error => State::Error,
}
}
}
struct DownloadState {
offset: usize,
}
pub trait DfuHandler {
fn write_data(&mut self, offset: usize, data: &[u8]);
fn is_write_complete(&self) -> bool {
true
}
fn complete_download(&mut self);
fn upload(&self, buffer: &mut [u8], offset: usize) -> usize;
}
pub struct UsbDfuDevice<'a> {
state: InternalState,
handler: &'a mut dyn DfuHandler,
}
impl<'a> UsbDfuDevice<'a> {
pub fn new(handler: &'a mut dyn DfuHandler) -> Self {
UsbDfuDevice {
state: InternalState::Idle,
handler,
}
}
fn handle_download(
&mut self,
DownloadState { offset }: DownloadState,
buf: &[u8],
) -> (InternalState, Result<(), ()>) {
if buf.len() == 0 {
return (InternalState::Idle, Ok(()));
}
self.handler.write_data(offset, buf);
return (
InternalState::DownloadBusy(DownloadState {
offset: offset + buf.len(),
}),
Ok(()),
);
}
pub fn handle_control_out(&mut self, req: DfuRequest, data: &[u8]) -> Result<(), ()> {
let (state, response) = match core::mem::replace(&mut self.state, InternalState::Error) {
InternalState::Idle => match req {
DfuRequest::Dnload => self.handle_download(DownloadState { offset: 0 }, data),
DfuRequest::Abort => (InternalState::Idle, Ok(())),
r => {
#[cfg(feature = "defmt")]
error!("received {} at state idle", r as u8);
(InternalState::Idle, Err(()))
}
},
InternalState::Download(download_state) => match req {
DfuRequest::Dnload => self.handle_download(download_state, data),
DfuRequest::Abort => (InternalState::Idle, Ok(())),
r => {
#[cfg(feature = "defmt")]
error!("received {} at state download", r as u8);
(InternalState::Error, Err(()))
}
},
state => {
#[cfg(feature = "defmt")]
error!("received {} for control out", req as u8);
(state, Err(()))
}
};
self.state = state;
response
}
pub fn handle_control_in<'b>(
&mut self,
req: DfuRequest,
buf: &'b mut [u8],
) -> Result<&'b [u8], ()> {
match req {
DfuRequest::GetState => {
buf[0] = Into::<consts::State>::into(&self.state) as u8;
Ok(&buf[0..1])
}
DfuRequest::GetStatus => {
let (next_state, status) =
match core::mem::replace(&mut self.state, InternalState::Error) {
InternalState::DownloadBusy(download_state) => {
match self.handler.is_write_complete() {
true => (InternalState::Download(download_state), DfuStatus::Ok),
false => {
(InternalState::DownloadBusy(download_state), DfuStatus::Ok)
}
}
}
InternalState::Error => (InternalState::Error, DfuStatus::ErrUnknown),
state => (state, DfuStatus::Ok),
};
self.state = next_state;
let next_state = Into::<consts::State>::into(&self.state);
buf[0] = status as u8;
buf[1..5].copy_from_slice(&(250_u32.to_le_bytes())); buf[4] = next_state as u8; buf[5] = 0;
Ok(&buf[0..6])
}
req => {
let (state, response): (InternalState, Result<&'a [u8], ()>) =
match core::mem::replace(&mut self.state, InternalState::Error) {
InternalState::Idle => todo!(),
InternalState::DownloadBusy(download_state) => todo!(),
InternalState::Download(download_state) => todo!(),
InternalState::Upload { offset } => todo!(),
InternalState::Error => todo!(),
};
}
}
}
}