FDO2 Driver 0.1.0
API reference
Loading...
Searching...
No Matches
Classes | Namespaces | Enumerations | Functions | Variables
fdo2_protocol.hpp File Reference

The FDO2 line protocol as pure functions: CRC-16, command encoders, the line assembler and the strict reply parser. No port, no state outside the objects, no allocation. More...

#include <algorithm>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <span>
#include <type_traits>
#include <utility>
#include "fdo2_types.hpp"

Go to the source code of this file.

Classes

class  fdo2::LineAssembler
 Collects the bytes of a reply into lines, one byte at a time (datasheet 4.1: one CR ends a reply). More...
 
struct  fdo2::ParseOptions
 How ParseLine() treats the CRC and the spacing (from the Config). More...
 
struct  fdo2::ParsedLine
 A parsed reply line. More...
 

Namespaces

namespace  fdo2
 The FDO2 driver: everything it declares lives here; internals in detail.
 

Enumerations

enum class  fdo2::Command : uint8_t {
  fdo2::Command::Vers = 0 , fdo2::Command::Idnr , fdo2::Command::Moxy , fdo2::Command::Mraw ,
  fdo2::Command::Logo , fdo2::Command::Crce , fdo2::Command::Rdum , fdo2::Command::Wrum ,
  fdo2::Command::Baud , fdo2::Command::Calo , fdo2::Command::Cahi , fdo2::Command::Bcst ,
  fdo2::Command::Setm
}
 The thirteen commands of the module (datasheet 4.3). More...
 
enum class  fdo2::PushResult : uint8_t { fdo2::PushResult::Pending = 0 , fdo2::PushResult::Line , fdo2::PushResult::Bad }
 What LineAssembler::PushByte() did with a byte. More...
 
enum class  fdo2::ParseStatus : uint8_t { fdo2::ParseStatus::Ok = 0 , fdo2::ParseStatus::BadCrc , fdo2::ParseStatus::Malformed }
 The outcome class of ParseLine(). More...
 

Functions

constexpr const char * fdo2::HeaderOf (Command command) noexcept
 The four header letters of a command, without the #.
 
template<typename T >
requires (std::is_same_v<T, uint8_t> || std::is_same_v<T, char>)
constexpr uint16_t fdo2::Crc16Modbus (std::span< const T > data) noexcept
 The CRC-16/MODBUS of a byte or character string (polynomial 0xA001 reflected, start 0xFFFF), as the module computes it over the reply before the colon (datasheet 4.2). "123456789" gives 0x4B37.
 
constexpr std::size_t fdo2::EncodeCommand (Command command, std::span< const int32_t > args, std::span< char > out) noexcept
 Encode a command line: #HEAD, each argument as plain decimal with one space before it, and one carriage return (never a LF: datasheet 4.1 allows CR or CR LF, the driver sends CR).
 
constexpr ParsedLine fdo2::ParseLine (std::span< const char > line, const ParseOptions &options) noexcept
 Parse one reply line (without its CR) under the grammar of datasheet 4.
 
constexpr std::size_t fdo2::ExpectedCount (Command command, std::span< const int32_t > args) noexcept
 How many values the reply to a command holds (datasheet 4.3).
 
constexpr bool fdo2::VerifyEcho (Command command, std::span< const int32_t > args, const ParsedLine &reply) noexcept
 The echo check of datasheet 4.2: the header and the echoed arguments equal what was sent.
 
constexpr uint32_t fdo2::MaxReplyChars (Command command, uint32_t words) noexcept
 The longest reply of a command, in characters including the CR and a CRC suffix, for the deadline (derived from datasheet 4.1 and 4.3: #MRAW with eight 11-character values).
 

Variables

constexpr std::size_t fdo2::kMaxLineLength = 800
 
constexpr std::size_t fdo2::kMaxCommandLength = 800
 Longest command line the driver sends (#WRUM 0 64 and 64 values), including the CR.
 
constexpr std::size_t fdo2::kMaxValues = 66
 Most values a reply holds: #RDUM R N and 64 words.
 
constexpr uint32_t fdo2::kMaxStale = 4
 Complete lines of another command dropped per transaction before the reply is InvalidReply.
 

Detailed Description

The FDO2 line protocol as pure functions: CRC-16, command encoders, the line assembler and the strict reply parser. No port, no state outside the objects, no allocation.

Datasheet section 4 (G2 data sheet, printed pages 10 to 17). A command is #HEAD, values separated by single spaces, and a carriage return; the module answers with the command echoed and the requested values, ended by one carriage return, with : C (the CRC-16/MODBUS in decimal) before it when #CRCE 1 is active. An error is #ERRO E with a negative E.

Because the layer is pure (bytes in, a struct out), an interrupt handler, a DMA completion or an RTOS task can decode what it received with the same checks the driver applies, and the fuzz target drives the parser directly.