B:BD[
2.12583] → [
2.12583:13472]
let count = (len) as usize;
self.read_at_least(count, timeout)?;
let payload = self.buffer.consume(count);
let (data, actual_checksum) = payload.split_at(count - 1);
let expected_checksum = data.iter().fold(0xff ^ len, |a, b| a ^ b);
if actual_checksum[0] != expected_checksum {
debug!(
"received a frame with an invalid checksum: {} != {}",
actual_checksum[0], expected_checksum
);
drop(payload);
debug!("sending nak");
self.write_one(FrameType::Nak)?;
return Err(ReadError::TimeoutOrBadFrame);
}
let raw_command = RawCommandData::new(data[1], &data[2..]);
let command = match raw_command.try_into() {
Err(e) => {
debug!("received a frame with invalid data: {:?}", e);
drop(payload);
self.write_one(FrameType::Nak)?;
return Err(ReadError::TimeoutOrBadFrame);
let (data, actual_checksum) = payload.split_at(count - 1);
let expected_checksum = data.iter().fold(0xff ^ len, |a, b| a ^ b);
if actual_checksum[0] != expected_checksum {
debug!(
"received a frame with an invalid checksum: {} != {}",
actual_checksum[0], expected_checksum
);
(FrameType::Nak, Err(ReadError::TimeoutOrBadFrame))
} else {
let raw_command = RawCommandData::new(data[1], &data[2..]);
match raw_command.try_into() {
Err(e) => {
debug!("received a frame with invalid data: {:?}", e);
(FrameType::Nak, Err(ReadError::TimeoutOrBadFrame))
}
Ok(v) => match data[0] {
0x00 => (FrameType::Ack, Ok(Frame::Request(v))),
0x01 => (FrameType::Ack, Ok(Frame::Response(v))),
v => {
debug!("received a frame with invalid type: {:02x}", v);
(FrameType::Nak, Err(ReadError::TimeoutOrBadFrame))
}
},
}