Skip to content

Commit

Permalink
Update framing
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Aug 28, 2023
1 parent ac4ac05 commit b816ac1
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions espflash/src/cli/monitor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ struct FrameDelimiter<'a> {
}

const FRAME_START: &[u8] = &[0xFF, 0x00];
const FRAME_END: &[u8] = &[0xFC, 0x00];
const FRAME_END: &[u8] = &[0x00];

fn search(haystack: &[u8], look_for_end: bool) -> Option<(&[u8], usize)> {
fn search(haystack: &[u8], look_for_end: bool) -> Option<(&[u8], &[u8])> {
let needle = if look_for_end { FRAME_END } else { FRAME_START };

haystack
.windows(needle.len())
.position(|window| window == FRAME_START || window == FRAME_END)
.map(|pos| (&haystack[pos..][..2], pos))
.position(|byte| byte == needle)
.map(|pos| (needle, &haystack[..pos]))
}

impl FrameDelimiter<'_> {
Expand All @@ -104,19 +105,22 @@ impl FrameDelimiter<'_> {

self.buffer.extend_from_slice(buffer);

while let Some((delimiter, pos)) = search(&self.buffer, self.in_frame) {
let frame = &self.buffer[..pos];
while let Some((delimiter, frame)) = search(&self.buffer, self.in_frame) {
if delimiter == FRAME_START {
process(FrameKind::Raw(frame));
self.in_frame = true;
} else {
table.received(frame);
// small reliance on rzcobs internals: we need to feed the terminating zero
table.received(delimiter);
if let Ok(frame) = table.decode() {
process(FrameKind::Defmt(frame));
} else {
log::warn!("Failed to decode defmt frame");
}
self.in_frame = false;
}
self.buffer.drain(..pos + delimiter.len());
self.buffer.drain(..frame.len() + delimiter.len());
}
}
}
Expand Down

0 comments on commit b816ac1

Please sign in to comment.