Skip to main content

rm_frame/
validator.rs

1use crate::{calc_dji8, calc_dji16};
2
3///
4/// CRC validator abstraction for the frame protocol.
5///
6/// Implementations define how frame integrity is verified:
7/// - CRC8 for the frame header
8/// - CRC16 for the frame body
9///
10pub trait Validator {
11    ///
12    /// Calculate CRC8 over the given raw bytes.
13    ///
14    /// Typically used for validating the frame header.
15    ///
16    fn calculate_crc8(raw: &[u8]) -> u8;
17    ///
18    /// Calculate CRC16 over the given raw bytes.
19    ///
20    /// Typically used for validating the full frame
21    /// (header + command + payload).
22    ///
23    fn calculate_crc16(raw: &[u8]) -> u16;
24}
25
26///
27/// DJI protocol CRC validator.
28///
29/// This implementation uses DJI-compatible CRC8 and CRC16
30/// algorithms for frame validation.
31///
32pub struct DjiValidator;
33
34impl Validator for DjiValidator {
35    fn calculate_crc8(raw: &[u8]) -> u8 {
36        calc_dji8(raw)
37    }
38
39    fn calculate_crc16(raw: &[u8]) -> u16 {
40        calc_dji16(raw)
41    }
42}