Skip to content

Commit

Permalink
Add a value to SimpleError
Browse files Browse the repository at this point in the history
The error values aren't meaningful in the protocol, but it might be
for a particular handler implementation to use different values when
debugging.
  • Loading branch information
tromey committed Dec 20, 2017
1 parent 0a35923 commit efaa64b
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,27 @@ fn command<'a>(i: &'a [u8]) -> IResult<&'a [u8], Command<'a>> {
)
}

const NOT_IMPLEMENTED: u8 = 0xff;

pub enum SimpleError {
Error
// The meaning of the value is not defined by the protocol; so it
// can be used by a handler for debugging.
Error(u8)
}

pub trait Handler {
fn query_supported_features() {}

fn ping_thread(&self, _id: ThreadId) -> Result<(), SimpleError> {
Err(SimpleError::Error)
Err(SimpleError::Error(NOT_IMPLEMENTED))
}

fn read_memory(&self, _address: u64, _length: u64) -> Result<Vec<u8>, SimpleError> {
Err(SimpleError::Error)
Err(SimpleError::Error(NOT_IMPLEMENTED))
}

fn read_register(&self, _register: u64) -> Result<Vec<u8>, SimpleError> {
Err(SimpleError::Error)
Err(SimpleError::Error(NOT_IMPLEMENTED))
}
}

Expand All @@ -311,6 +315,7 @@ fn compute_checksum(bytes: &[u8]) -> u8 {

enum Response<'a> {
Empty,
Error(u8),
String(&'a str),
Bytes(Vec<u8>),
}
Expand All @@ -322,6 +327,10 @@ fn write_response<W>(response: Response, writer: &mut W) -> io::Result<()>
Response::Empty => {
writer.write_all(&b"$#00"[..])
}
Response::Error(val) => {
let response = format!("E${:02x}", val);
write!(writer, "${}#{:02x}", response, compute_checksum(response.as_bytes()))
}
Response::String(s) => {
write!(writer, "${}#{:02x}", s, compute_checksum(s.as_bytes()))
}
Expand Down Expand Up @@ -363,18 +372,18 @@ fn handle_packet<H, W>(data: &[u8],
// The k packet requires no response.
Command::Kill(None) => Response::Empty,
// We don't implement this, so return an error.
Command::Kill(Some(_)) => Response::String("E01"),
Command::Kill(Some(_)) => Response::Error(NOT_IMPLEMENTED),
Command::Reset => Response::Empty,
Command::ReadRegister(regno) => {
match handler.read_register(regno) {
Result::Ok(bytes) => Response::Bytes(bytes),
Result::Err(_) => Response::String("E01"),
Result::Err(SimpleError::Error(val)) => Response::Error(val),
}
},
Command::ReadMemory(address, length) => {
match handler.read_memory(address, length) {
Result::Ok(bytes) => Response::Bytes(bytes),
Result::Err(_) => Response::String("E01"),
Result::Err(SimpleError::Error(val)) => Response::Error(val),
}
},
Command::Query(Query::SupportedFeatures(features)) =>
Expand All @@ -386,7 +395,7 @@ fn handle_packet<H, W>(data: &[u8],
Command::PingThread(thread_id) => {
match handler.ping_thread(thread_id) {
Result::Ok(_) => Response::String("OK"),
Result::Err(_) => Response::String("E01"),
Result::Err(SimpleError::Error(val)) => Response::Error(val),
}
}
Command::CtrlC => Response::String("E01"),
Expand Down

0 comments on commit efaa64b

Please sign in to comment.