From 3641331da2bf2cf8df7c4fea4bd51f5fb9f9558f Mon Sep 17 00:00:00 2001 From: ju6ge Date: Wed, 25 Mar 2026 13:54:47 +0100 Subject: [PATCH] update crc6 check as well, seperate commit to check that test results are stable --- lib-bms-protocol/src/types.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/lib-bms-protocol/src/types.rs b/lib-bms-protocol/src/types.rs index d3d0bc1..49993b8 100644 --- a/lib-bms-protocol/src/types.rs +++ b/lib-bms-protocol/src/types.rs @@ -365,18 +365,15 @@ pub fn calc_crc6(data: u32) -> u8 { /// This function is the mirror piece to the crc6 calculation, it checks a crc value against /// the data to determine if the crc is valid. pub fn check_crc6(data: u32, crc: u8) -> bool { - let mut working_data: u32 = data << 6 | crc as u32; - let mut polynom: u32 = 0x43 << 25; - let mut mask: u32 = 0x40 << 25; - while working_data >= 0x40 { - if working_data & mask != 0 { - working_data ^= polynom; + let mut check_crc: u8 = 0x00; + for pos in 0..32 { + if ((data >> (31-pos)) & 0x1) ^ ((check_crc >> 5) & 0x1) as u32 != 0 { + check_crc = ((check_crc << 1) ^ 0x03) & 0x3F; } else { - polynom >>= 1; - mask >>= 1; + check_crc = (check_crc << 1) & 0x3F; } } - working_data == 0x00000000 + check_crc == crc } #[cfg(test)]