From b816ac131e663d76985b4b5c6972a61269c9b588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Mon, 28 Aug 2023 15:32:40 +0200 Subject: [PATCH] Update framing --- espflash/src/cli/monitor/mod.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/espflash/src/cli/monitor/mod.rs b/espflash/src/cli/monitor/mod.rs index 412403c4..01185fbc 100644 --- a/espflash/src/cli/monitor/mod.rs +++ b/espflash/src/cli/monitor/mod.rs @@ -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<'_> { @@ -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()); } } }