use bytes::{Buf, BufMut, BytesMut};
use tokio_util::codec::{Decoder, Encoder};
pub const SOF: u8 = 0x01;
pub const ACK: u8 = 0x06;
pub const NAK: u8 = 0x15;
pub const CAN: u8 = 0x18;
pub const MAX_PAYLOAD: usize = 255 - 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FrameType {
Request,
Response,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataFrame {
pub kind: FrameType,
pub function: u8,
pub payload: Vec<u8>,
}
impl DataFrame {
pub fn request(function: impl Into<u8>, payload: Vec<u8>) -> Self {
DataFrame {
kind: FrameType::Request,
function: function.into(),
payload,
}
}
pub fn response(function: impl Into<u8>, payload: Vec<u8>) -> Self {
DataFrame {
kind: FrameType::Response,
function: function.into(),
payload,
}
}
pub fn encode(&self) -> Vec<u8> {
assert!(
self.payload.len() <= MAX_PAYLOAD,
"Serial API payload too long"
);
let len = (self.payload.len() + 3) as u8;
let mut v = Vec::with_capacity(self.payload.len() + 5);
v.push(SOF);
v.push(len);
v.push(match self.kind {
FrameType::Request => 0x00,
FrameType::Response => 0x01,
});
v.push(self.function);
v.extend_from_slice(&self.payload);
v.push(checksum(&v[1..]));
v
}
}
pub fn checksum(bytes: &[u8]) -> u8 {
bytes.iter().fold(0xff, |acc, b| acc ^ b)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Frame {
Ack,
Nak,
Can,
Data(DataFrame),
Invalid,
}
impl Frame {
pub fn encode(&self) -> Vec<u8> {
match self {
Frame::Ack => vec![ACK],
Frame::Nak => vec![NAK],
Frame::Can => vec![CAN],
Frame::Data(d) => d.encode(),
Frame::Invalid => vec![],
}
}
}
#[derive(Debug, Default, Clone)]
pub struct FrameCodec {
skipped: usize,
}
impl FrameCodec {
pub fn skipped(&self) -> usize {
self.skipped
}
pub fn reset(&mut self, buf: &mut BytesMut) {
self.skipped += buf.len();
buf.clear();
}
}
impl Decoder for FrameCodec {
type Item = Frame;
type Error = std::io::Error;
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<Frame>, std::io::Error> {
loop {
let Some(&first) = buf.first() else {
return Ok(None);
};
match first {
ACK => {
buf.advance(1);
return Ok(Some(Frame::Ack));
}
NAK => {
buf.advance(1);
return Ok(Some(Frame::Nak));
}
CAN => {
buf.advance(1);
return Ok(Some(Frame::Can));
}
SOF => {
let Some(&len) = buf.get(1) else {
return Ok(None);
};
let len = len as usize;
if len < 3 {
buf.advance(1);
self.skipped += 1;
continue;
}
if buf.len() < len + 2 {
buf.reserve(len + 2 - buf.len());
return Ok(None);
}
let raw = buf.split_to(len + 2);
let kind = match raw[2] {
0x00 => FrameType::Request,
0x01 => FrameType::Response,
_ => return Ok(Some(Frame::Invalid)),
};
if checksum(&raw[1..len + 1]) != raw[len + 1] {
return Ok(Some(Frame::Invalid));
}
return Ok(Some(Frame::Data(DataFrame {
kind,
function: raw[3],
payload: raw[4..len + 1].to_vec(),
})));
}
_ => {
buf.advance(1);
self.skipped += 1;
}
}
}
}
}
impl Encoder<Frame> for FrameCodec {
type Error = std::io::Error;
fn encode(&mut self, item: Frame, dst: &mut BytesMut) -> Result<(), std::io::Error> {
dst.put_slice(&item.encode());
Ok(())
}
}
pub fn decode_all(bytes: &[u8]) -> Vec<Frame> {
let mut codec = FrameCodec::default();
let mut buf = BytesMut::from(bytes);
let mut out = Vec::new();
while let Ok(Some(f)) = codec.decode(&mut buf) {
out.push(f);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn known_frames() {
let f = DataFrame::request(0x07u8, vec![]);
assert_eq!(f.encode(), vec![0x01, 0x03, 0x00, 0x07, 0xfb]);
let r = DataFrame::response(0x20u8, vec![0xc0, 0xff, 0xee, 0x01, 0x01]);
let enc = r.encode();
assert_eq!(decode_all(&enc), vec![Frame::Data(r)]);
}
#[test]
fn noise_partial_and_bad_checksum() {
let good = DataFrame::request(0x04u8, vec![0x00, 0x02, 0x03, 0x25, 0x03, 0xff]).encode();
let mut bad = good.clone();
let last = bad.len() - 1;
bad[last] ^= 0xff;
let mut stream = vec![0x42, 0x00, SOF, 0x02];
stream.extend_from_slice(&[ACK, NAK, CAN]);
stream.extend_from_slice(&bad);
stream.extend_from_slice(&good);
let frames = decode_all(&stream);
assert_eq!(frames.len(), 5);
assert_eq!(frames[0], Frame::Ack);
assert_eq!(frames[3], Frame::Invalid);
assert!(matches!(frames[4], Frame::Data(_)));
let mut codec = FrameCodec::default();
let mut buf = BytesMut::from(&good[..4]);
assert_eq!(codec.decode(&mut buf).unwrap(), None);
buf.extend_from_slice(&good[4..]);
assert!(matches!(
codec.decode(&mut buf).unwrap(),
Some(Frame::Data(_))
));
}
#[test]
fn bad_type_byte_is_invalid() {
let mut raw = DataFrame::request(0x04u8, vec![1]).encode();
raw[2] = 0x07;
let n = raw.len() - 1;
raw[n] = checksum(&raw[1..n]);
assert_eq!(decode_all(&raw), vec![Frame::Invalid]);
}
}