31 lines
include/frame_parser.h
Declares FrameParser, Frame, and the feed() contract.
// Stateful binary frame parser for the sensor telemetry protocol.
#pragma once
#include <cstdint>
#include <span>
#include <vector>
 
struct Frame {
  uint8_t              type; // frame type identifier
  std::vector<uint8_t> body; // decoded frame payload
};
 
// Stateful binary frame parser for the sensor telemetry protocol.
// Frame layout: [0xCA][0xFE][len_hi][len_lo][type][reserved][body...]
// kMaxBodyBytes: maximum accepted body length; larger frames are dropped.
class FrameParser {
public:
  static constexpr uint16_t kMaxBodyBytes = 4096; // maximum body size per frame
 
  // Parameters:
  //   data - raw bytes received from the socket in this call
  //   out  - vector to append any complete Frame objects found in data
  // Returns: number of bytes consumed from data; re-feed unconsumed bytes next call
  size_t feed(std::span<const uint8_t> data, std::vector<Frame>& out);
 
  // Parameters: none
  // Returns: void; discards any partially buffered frame bytes
  void reset();
 
private:
  std::vector<uint8_t> buf_; // cross-call reassembly buffer
};