39 lines
include/frame_decoder.h
Declares FrameDecoder, kMaxPayloadBytes, and the processFrame contract.
// FrameDecoder: converts raw USB bulk-transfer bytes into sensor payloads.
#pragma once
#include <cstddef>
#include <cstdint>
#include <functional>
 
// Maximum payload bytes the staging buffer can hold.
static constexpr size_t kMaxPayloadBytes = 64;
 
// Decoded sensor frame ready for dispatch to the host application.
struct SensorFrame {
  uint8_t type;                    // frame type identifier from byte 1
  uint8_t payload[kMaxPayloadBytes]; // decoded payload bytes
  uint8_t payloadLen;              // number of valid bytes in payload[]
};
 
// Dispatches a decoded sensor frame to the host application.
using FrameDispatchFn = std::function<void(const SensorFrame&)>;
 
// Decodes raw device bytes and dispatches complete frames.
class FrameDecoder {
public:
  // Parameters:
  //   dispatch - callback invoked for each fully decoded frame
  // Returns: new FrameDecoder ready to accept processFrame calls
  explicit FrameDecoder(FrameDispatchFn dispatch);
 
  // Frame layout: [payloadLen][type][payload...]
  // Parameters:
  //   buf    - raw bytes from a USB bulk-transfer read
  //   bufLen - number of valid bytes in buf
  // Returns: true if a complete frame was decoded and dispatched,
  //          false if the buffer was too short, inconsistent, or the
  //          payload length exceeded kMaxPayloadBytes
  bool processFrame(const uint8_t* buf, size_t bufLen);
 
private:
  FrameDispatchFn dispatch_; // host callback invoked for each decoded frame
};