Separate GDB request and response into structs

This commit is contained in:
2025-01-01 17:02:14 -05:00
parent 24487b21b7
commit 9519897711
3 changed files with 201 additions and 179 deletions
+45
View File
@@ -0,0 +1,45 @@
pub struct Response {
buffer: Vec<u8>,
checksum: u8,
}
impl Response {
pub fn new(mut buffer: Vec<u8>, ack: bool) -> Self {
buffer.clear();
if ack {
buffer.push(b'+');
}
buffer.push(b'$');
Self {
buffer,
checksum: 0,
}
}
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);
}
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);
}
self
}
pub fn finish(mut self) -> Vec<u8> {
let checksum = self.checksum;
self.buffer.push(b'#');
self.write_hex_u8(checksum).buffer
}
}