Separate GDB request and response into structs
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user