nom_teltonika/
lib.rs

1#![doc = include_str!("../README.md")]
2pub mod parser;
3mod protocol;
4mod stream;
5
6pub use protocol::*;
7pub use stream::*;
8
9/// IBM CRC16 Algorithm
10///
11/// Uses 0xA001 polynomial
12pub fn crc16(data: &[u8]) -> u16 {
13    let mut crc: u16 = 0;
14    for &byte in data {
15        crc ^= byte as u16;
16        for _bit in 0..8 {
17            let carry = crc & 1;
18            crc >>= 1;
19            if carry != 0 {
20                crc ^= 0xA001;
21            }
22        }
23    }
24    crc
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn test_crc16() {
33        let input = hex::decode(
34            "08010000016B40D9AD80010000000000000000000000000000000103021503010101425E10000001",
35        )
36        .unwrap();
37        assert_eq!(crc16(&input), 0x0000F22A);
38    }
39}