25 lines
include/crc_frame_validator.h
Declares CrcFrameValidator and the validateFrame contract.
// CrcFrameValidator: checks serial frames for CRC integrity.
#pragma once
#include <cstddef>
#include <cstdint>
 
// Computes the project CRC16 over len bytes of buf.
// Parameters:
//   buf - pointer to the frame bytes to checksum (excluding the CRC suffix)
//   len - number of bytes to include in the computation
// Returns: 16-bit CRC value
uint16_t computeCRC16(const uint8_t* buf, size_t len);
 
// Validates incoming serial frames for transmission errors.
class CrcFrameValidator {
public:
  // Frame layout: [addr][function][data...][crc_hi][crc_lo]
  // This project stores the two-byte CRC16 as high byte first.
  //
  // Parameters:
  //   buf - complete raw frame including the two appended CRC bytes
  //   len - total frame length in bytes (data + 2 CRC bytes)
  // Returns: true if the received CRC matches the computed value for all 16 bits,
  //          false if any bit differs or the frame is too short to contain a CRC
  bool validateFrame(const uint8_t* buf, size_t len) const;
};