commit cf1f5dcde34b7e85505a2bdd0e9bd0b77d0e68a2
parent 2b96e079bc872e221361b5a9513b4f7ccfd5d973
Author: Daniel Barlow <dan@telent.net>
Date: Fri, 21 Nov 2025 23:52:29 +0000
calculate checksum on outgoing packets
Diffstat:
1 file changed, 25 insertions(+), 4 deletions(-)
diff --git a/src/ecu.rs b/src/ecu.rs
@@ -28,9 +28,23 @@ impl Ecu<'_> {
}
}
+ fn checksum(bytes: &mut [u8]) -> bool {
+ let last = bytes.len() - 1;
+ let mut sum : u8 = 0;
+ for b in &bytes[0..last] {
+ sum = sum.wrapping_add(*b);
+ }
+ sum = 0u8 - sum;
+ let is_correct = sum == bytes[last];
+ bytes[last] = sum;
+ is_correct
+ }
+
+
async fn chat<'z>(uart: &mut Uart<'_, Async>,
- send: &[u8],
- receive: &'z mut [u8]) -> &'z [u8] {
+ send: &mut [u8],
+ receive: &'z mut [u8]) -> &'z [u8] {
+ let _ = Self::checksum(send);
uart.write(send).expect("write failed");
let _ = uart.flush();
let mut received = 0;
@@ -78,9 +92,16 @@ impl Ecu<'_> {
.into_async();
let mut buf = [0u8; 80];
- let _ = Self::chat(&mut uart, b"\xfe\x04\xff\xff", &mut buf[0..0]).await;
+
+ let _ = Self::chat(&mut uart,
+ &mut [0xfe, 0x04, 0xff, 0xff],
+ &mut buf[0..0]).await;
Timer::after_millis(200).await;
- let handshake = Self::chat(&mut uart, b"\x72\x05\x00\xF0\x99", &mut buf[0..4]).await;
+
+ // checksum is also correct on this packet
+ let handshake = Self::chat(&mut uart,
+ &mut [0x72, 0x05, 0x00, 0xF0, 0x99],
+ &mut buf[0..4]).await;
info!("matches {}", handshake == b"\x02\x04\x00\xfa");
}