Stub out reading registers and memory
This commit is contained in:
+42
-11
@@ -1,4 +1,5 @@
|
||||
use anyhow::{bail, Result};
|
||||
use atoi::FromRadix16;
|
||||
use tokio::io::{AsyncRead, AsyncReadExt as _};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
@@ -6,11 +7,32 @@ pub enum RequestKind {
|
||||
Signal,
|
||||
Command,
|
||||
}
|
||||
impl RequestKind {
|
||||
fn name(self) -> &'static str {
|
||||
match self {
|
||||
Self::Signal => "Signal",
|
||||
Self::Command => "Command",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Request<'a> {
|
||||
pub kind: RequestKind,
|
||||
body: &'a str,
|
||||
buffer: &'a [u8],
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for Request<'_> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let mut ds = f.debug_tuple(self.kind.name());
|
||||
match self.kind {
|
||||
RequestKind::Signal => ds.field(&self.buffer),
|
||||
RequestKind::Command => match std::str::from_utf8(self.buffer) {
|
||||
Ok(str) => ds.field(&str),
|
||||
Err(_) => ds.field(&self.buffer),
|
||||
},
|
||||
};
|
||||
ds.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Request<'a> {
|
||||
@@ -31,10 +53,9 @@ impl<'a> Request<'a> {
|
||||
if char == 0x03 {
|
||||
// This is how the client "cancels an in-flight request"
|
||||
buffer.push(char);
|
||||
let body = std::str::from_utf8(buffer)?;
|
||||
return Ok(Self {
|
||||
kind: RequestKind::Signal,
|
||||
body,
|
||||
buffer,
|
||||
});
|
||||
}
|
||||
if char != b'$' {
|
||||
@@ -61,24 +82,34 @@ impl<'a> Request<'a> {
|
||||
|
||||
let mut checksum_bytes = [b'0'; 2];
|
||||
reader.read_exact(&mut checksum_bytes).await?;
|
||||
let checksum_str = std::str::from_utf8(&checksum_bytes)?;
|
||||
let real_checksum = u8::from_str_radix(checksum_str, 16)?;
|
||||
if checksum != real_checksum {
|
||||
let (real_checksum, 2) = u8::from_radix_16(&checksum_bytes) else {
|
||||
bail!("invalid checksum");
|
||||
};
|
||||
if checksum != real_checksum {
|
||||
bail!("mismatched checksum");
|
||||
}
|
||||
|
||||
let body = std::str::from_utf8(buffer)?;
|
||||
Ok(Self {
|
||||
kind: RequestKind::Command,
|
||||
body,
|
||||
buffer,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn match_str(&mut self, prefix: &str) -> bool {
|
||||
if let Some(new_body) = self.body.strip_prefix(prefix) {
|
||||
self.body = new_body;
|
||||
if let Some(new_buffer) = self.buffer.strip_prefix(prefix.as_bytes()) {
|
||||
self.buffer = new_buffer;
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn match_hex<I: FromRadix16>(&mut self) -> Option<I> {
|
||||
match I::from_radix_16(self.buffer) {
|
||||
(_, 0) => None,
|
||||
(val, used) => {
|
||||
self.buffer = self.buffer.split_at(used).1;
|
||||
Some(val)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+17
-13
@@ -1,3 +1,5 @@
|
||||
use num_traits::ToBytes;
|
||||
|
||||
pub struct Response {
|
||||
buffer: Vec<u8>,
|
||||
checksum: u8,
|
||||
@@ -17,22 +19,24 @@ impl Response {
|
||||
}
|
||||
|
||||
pub fn write_str(mut self, str: &str) -> Self {
|
||||
for char in str.as_bytes() {
|
||||
self.buffer.push(*char);
|
||||
self.checksum = self.checksum.wrapping_add(*char);
|
||||
for byte in str.as_bytes() {
|
||||
self.buffer.push(*byte);
|
||||
self.checksum = self.checksum.wrapping_add(*byte);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn write_hex_u8(mut self, value: u8) -> Self {
|
||||
for digit in [(value >> 4), (value & 0xf)] {
|
||||
let char = if digit > 9 {
|
||||
b'a' + digit - 10
|
||||
} else {
|
||||
b'0' + digit
|
||||
};
|
||||
self.buffer.push(char);
|
||||
self.checksum = self.checksum.wrapping_add(char);
|
||||
pub fn write_hex<T: ToBytes>(mut self, value: T) -> Self {
|
||||
for byte in value.to_be_bytes().as_ref() {
|
||||
for digit in [(byte >> 4), (byte & 0xf)] {
|
||||
let char = if digit > 9 {
|
||||
b'a' + digit - 10
|
||||
} else {
|
||||
b'0' + digit
|
||||
};
|
||||
self.buffer.push(char);
|
||||
self.checksum = self.checksum.wrapping_add(char);
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
@@ -40,6 +44,6 @@ impl Response {
|
||||
pub fn finish(mut self) -> Vec<u8> {
|
||||
let checksum = self.checksum;
|
||||
self.buffer.push(b'#');
|
||||
self.write_hex_u8(checksum).buffer
|
||||
self.write_hex(checksum).buffer
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user