BCNP 3.2.1
Batched Command Network Protocol
Loading...
Searching...
No Matches
bcnp Namespace Reference

Namespaces

namespace  detail
 Type traits to detect container capabilities for packet storage.
 

Classes

class  ByteStream
 Interface for receiving raw bytes from a transport. More...
 
class  ByteWriter
 Interface for sending raw bytes over a transport. More...
 
class  CommandQueue
 Legacy CommandQueue - use MessageQueue<DriveCmd> for new code. More...
 
struct  DecodeViewResult
 Result of decoding a packet from raw bytes. More...
 
struct  DispatcherConfig
 Configuration for the packet dispatcher. More...
 
class  DispatcherDriver
 Drives a PacketDispatcher from a transport adapter. More...
 
class  DuplexAdapter
 Combined send/receive interface for bidirectional transports. More...
 
struct  HasDurationMs
 Type trait to detect messages with a durationMs field. More...
 
struct  IsValidPacketStorage
 Concept-like check for valid packet storage containers. More...
 
struct  MessageInfo
 
class  MessageIterator
 Forward iterator for zero-copy message access from packet payload. More...
 
class  MessageQueue
 Generic timed message queue for any message type with durationMs field. More...
 
struct  MessageQueueConfig
 Configuration parameters for a message queue. More...
 
struct  MessageQueueMetrics
 Runtime metrics for queue diagnostics. More...
 
class  PacketDispatcher
 Parses BCNP stream and dispatches packets to registered handlers. More...
 
struct  PacketHeader
 Parsed packet header structure. More...
 
struct  PacketView
 Zero-copy view into a decoded packet buffer. More...
 
class  SpiStreamAdapter
 SPI transport adapter for legacy SPI-based communication. More...
 
class  StaticVector
 Fixed-capacity vector with no heap allocation. More...
 
class  StreamParser
 Parses a byte stream into BCNP packets. More...
 
class  TcpPosixAdapter
 TCP transport adapter for BCNP over POSIX sockets. More...
 
class  TelemetryAccumulator
 Accumulates high-frequency telemetry data and batches into packets. More...
 
struct  TelemetryAccumulatorConfig
 Configuration for telemetry accumulator. More...
 
struct  TypedPacket
 Generic packet containing messages of a specific type. More...
 
class  UdpPosixAdapter
 UDP transport adapter for BCNP over POSIX sockets. More...
 

Typedefs

using QueueConfig = MessageQueueConfig
 
using QueueMetrics = MessageQueueMetrics
 
using Command = DriveCmd
 
using PacketHandler = std::function<void(const PacketView&)>
 Callback for handling message packets.
 
using ErrorHandler = std::function<void(const StreamParser::ErrorInfo&)>
 Error callback for parse errors.
 
using MessageHandler = PacketHandler
 
template<typename MsgType >
using TimedQueue = MessageQueue<MsgType>
 Convenience alias for MessageQueue (backward compatibility).
 
template<typename MsgType , std::size_t Capacity = 64>
using StaticTypedPacket = TypedPacket<MsgType, StaticVector<MsgType, Capacity>>
 Convenience alias for stack-allocated real-time packets (default 64 messages)
 
template<typename MsgType >
using DynamicTypedPacket = TypedPacket<MsgType, std::vector<MsgType>>
 Convenience alias for heap-allocated packets (backward compatible).
 
template<typename T >
using DynamicPacketStorage = std::vector<T>
 Default packet storage using heap allocation.
 
template<typename T , std::size_t Capacity = 64>
using StaticPacketStorage = StaticVector<T, Capacity>
 Real-time packet storage using stack allocation.
 
template<typename T >
using DefaultRealtimeStorage = StaticPacketStorage<T, 64>
 Alias for the recommended real-time default (64 messages).
 
template<typename MsgType >
using DynamicTelemetryAccumulator = TelemetryAccumulator<MsgType, std::vector<MsgType>>
 Convenience alias for heap-allocated accumulator (large batches).
 
template<typename MsgType , std::size_t Capacity = 64>
using StaticTelemetryAccumulator = TelemetryAccumulator<MsgType, StaticVector<MsgType, Capacity>>
 Convenience alias for stack-allocated accumulator (real-time, default).
 
using ControllerDriver = DispatcherDriver
 Backward-compatible alias (prefer DispatcherDriver for new code).
 

Enumerations

enum class  PacketError {
  None , TooSmall , UnsupportedVersion , TooManyMessages ,
  TooManyCommands = TooManyMessages , Truncated , InvalidFloat , ChecksumMismatch ,
  UnknownMessageType , HandshakeRequired , SchemaMismatch
}
 Error codes returned by packet decoding operations. More...
 
enum class  MessageTypeId : uint16_t { Unknown = 0 }
 

Functions

uint32_t ComputeCrc32 (const uint8_t *data, std::size_t length)
 Compute CRC32 checksum for data integrity verification.
 
DecodeViewResult DecodePacketViewWithSize (const uint8_t *data, std::size_t length, std::size_t wireSize)
 Decode a packet with explicitly provided message wire size.
 
DecodeViewResult DecodePacketView (const uint8_t *data, std::size_t length)
 Decode a packet using the global message type registry.
 
template<typename MsgType , typename Storage >
bool EncodeTypedPacket (const TypedPacket< MsgType, Storage > &packet, uint8_t *output, std::size_t capacity, std::size_t &bytesWritten)
 Encode a typed packet to a pre-allocated buffer.
 
template<typename MsgType , typename Storage >
bool EncodeTypedPacket (const TypedPacket< MsgType, Storage > &packet, std::vector< uint8_t > &output)
 Encode a typed packet to a dynamically-sized vector.
 
template<typename MsgType >
std::optional< TypedPacket< MsgType > > DecodeTypedPacket (const PacketView &view)
 Decode messages from a PacketView into a typed packet.
 
template<typename MsgType , typename Storage >
std::optional< TypedPacket< MsgType, Storage > > DecodeTypedPacketAs (const PacketView &view)
 Decode messages from a PacketView with custom storage type.
 
template<typename MsgType >
DecodeViewResult DecodePacketViewAs (const uint8_t *data, std::size_t length)
 Type-safe packet view decode using compile-time message type.
 
template<typename Container >
void ReserveIfPossible (Container &container, std::size_t capacity)
 Helper to reserve capacity (no-op for static storage).
 
std::optional< MessageInfoGetMessageInfo (MessageTypeId typeId)
 
std::optional< MessageInfoGetMessageInfo (uint16_t typeId)
 
bool EncodeHandshake (uint8_t *out, std::size_t capacity)
 Encode handshake with default schema hash.
 
bool EncodeHandshakeWithHash (uint8_t *out, std::size_t capacity, uint32_t schemaHash)
 Encode handshake with custom schema hash (for testing)
 
bool ValidateHandshake (const uint8_t *data, std::size_t length)
 
bool ValidateHandshakeWithHash (const uint8_t *data, std::size_t length, uint32_t expectedHash)
 Validate handshake against custom expected hash (for testing)
 
uint32_t ExtractSchemaHash (const uint8_t *data, std::size_t length)
 

Variables

constexpr std::size_t kChecksumSize = 4
 Size of CRC32 checksum in bytes.
 
constexpr std::size_t kMaxMessagesPerPacket = 65535
 Maximum number of messages allowed in a single packet.
 
constexpr uint8_t kFlagClearQueue = 0x01
 Packet flag: Clear command queue before processing this packet.
 
constexpr uint8_t kProtocolMajor = kProtocolMajorV3
 Current protocol major version.
 
constexpr uint8_t kProtocolMinor = kProtocolMinorV3
 Current protocol minor version.
 
constexpr std::size_t kHeaderSize = kHeaderSizeV3
 Size of packet header in bytes.
 
constexpr std::size_t kHeaderMajorIndex = 0
 Byte offset of major version in header.
 
constexpr std::size_t kHeaderMinorIndex = 1
 Byte offset of minor version in header.
 
constexpr std::size_t kHeaderFlagsIndex = 2
 Byte offset of flags byte in header.
 
template<typename Container >
constexpr bool IsValidPacketStorage_v = IsValidPacketStorage<Container>::value
 
constexpr uint8_t kProtocolMajorV3 = 3
 
constexpr uint8_t kProtocolMinorV3 = 2
 
constexpr uint32_t kSchemaHash = 0x5667F881U
 
constexpr std::size_t kHandshakeSize = 8
 
constexpr std::array< uint8_t, 4 > kHandshakeMagic = {{'B', 'C', 'N', 'P'}}
 
constexpr std::size_t kHeaderSizeV3 = 7
 
constexpr std::size_t kHeaderMsgTypeIndex = 3
 
constexpr std::size_t kHeaderMsgCountIndex = 5
 
constexpr std::array< MessageInfo, 0 > kMessageRegistry = {{}}
 

Typedef Documentation

◆ Command

using bcnp::Command = DriveCmd

Definition at line 13 of file command_queue.h.

◆ ControllerDriver

Backward-compatible alias (prefer DispatcherDriver for new code).

Definition at line 52 of file controller_driver.h.

◆ DefaultRealtimeStorage

template<typename T >
using bcnp::DefaultRealtimeStorage = StaticPacketStorage<T, 64>

Alias for the recommended real-time default (64 messages).

Definition at line 114 of file packet_storage.h.

◆ DynamicPacketStorage

template<typename T >
using bcnp::DynamicPacketStorage = std::vector<T>

Default packet storage using heap allocation.

Use for Large batches, trajectory uploads or config dumps.

Definition at line 96 of file packet_storage.h.

◆ DynamicTelemetryAccumulator

template<typename MsgType >
using bcnp::DynamicTelemetryAccumulator = TelemetryAccumulator<MsgType, std::vector<MsgType>>

Convenience alias for heap-allocated accumulator (large batches).

Definition at line 276 of file telemetry_accumulator.h.

◆ DynamicTypedPacket

template<typename MsgType >
using bcnp::DynamicTypedPacket = TypedPacket<MsgType, std::vector<MsgType>>

Convenience alias for heap-allocated packets (backward compatible).

Definition at line 260 of file packet.h.

◆ ErrorHandler

using bcnp::ErrorHandler = std::function<void(const StreamParser::ErrorInfo&)>

Error callback for parse errors.

Definition at line 24 of file dispatcher.h.

◆ MessageHandler

Definition at line 124 of file dispatcher.h.

◆ PacketHandler

using bcnp::PacketHandler = std::function<void(const PacketView&)>

Callback for handling message packets.

Definition at line 21 of file dispatcher.h.

◆ QueueConfig

Definition at line 11 of file command_queue.h.

◆ QueueMetrics

Definition at line 12 of file command_queue.h.

◆ StaticPacketStorage

template<typename T , std::size_t Capacity = 64>
using bcnp::StaticPacketStorage = StaticVector<T, Capacity>

Real-time packet storage using stack allocation.

Use for Control loop telemetry or command batches.

Default capacity of 64 covers most FRC use cases: 64 × 12 bytes (EncoderData) = 768 bytes (safe for stack) 64 × 32 bytes (TrajectoryPoint) = 2 KB (safe for stack)

Definition at line 108 of file packet_storage.h.

◆ StaticTelemetryAccumulator

template<typename MsgType , std::size_t Capacity = 64>
using bcnp::StaticTelemetryAccumulator = TelemetryAccumulator<MsgType, StaticVector<MsgType, Capacity>>

Convenience alias for stack-allocated accumulator (real-time, default).

Definition at line 282 of file telemetry_accumulator.h.

◆ StaticTypedPacket

template<typename MsgType , std::size_t Capacity = 64>
using bcnp::StaticTypedPacket = TypedPacket<MsgType, StaticVector<MsgType, Capacity>>

Convenience alias for stack-allocated real-time packets (default 64 messages)

Definition at line 256 of file packet.h.

◆ TimedQueue

template<typename MsgType >
using bcnp::TimedQueue = MessageQueue<MsgType>

Convenience alias for MessageQueue (backward compatibility).

Template Parameters
MsgTypeMessage type with durationMs field

Definition at line 408 of file message_queue.h.

Enumeration Type Documentation

◆ MessageTypeId

enum class bcnp::MessageTypeId : uint16_t
strong
Enumerator
Unknown 

Definition at line 39 of file message_types.h.

◆ PacketError

enum class bcnp::PacketError
strong

Error codes returned by packet decoding operations.

These errors indicate various failure modes during packet parsing, from simple size issues to protocol mismatches and data corruption.

Enumerator
None 

No error - packet decoded successfully.

TooSmall 

Buffer too small to contain header.

UnsupportedVersion 

Protocol version mismatch.

TooManyMessages 

Message count exceeds kMaxMessagesPerPacket.

TooManyCommands 
Deprecated
Legacy alias for TooManyMessages
Truncated 

Buffer ends before expected packet length.

InvalidFloat 

NaN or Inf detected in float field.

ChecksumMismatch 

CRC32 validation failed.

UnknownMessageType 

Message type ID not in registry.

HandshakeRequired 

Connection requires handshake first.

SchemaMismatch 

Client/server schema hash mismatch.

Definition at line 268 of file packet.h.

Function Documentation

◆ ComputeCrc32()

uint32_t bcnp::ComputeCrc32 ( const uint8_t * data,
std::size_t length )

Compute CRC32 checksum for data integrity verification.

Uses the standard CRC32 polynomial (0xEDB88320) with initial value of 0xFFFFFFFF and final XOR. Compatible with common CRC32 implementations.

Parameters
dataPointer to data buffer
lengthNumber of bytes to checksum
Returns
32-bit CRC checksum

Definition at line 49 of file packet.cpp.

Here is the caller graph for this function:

◆ DecodePacketView()

DecodeViewResult bcnp::DecodePacketView ( const uint8_t * data,
std::size_t length )

Decode a packet using the global message type registry.

Decode packet view using the global message type registry.

Extracts the message type ID from the header and looks up the wire size from GetMessageInfo(). Returns UnknownMessageType error if the type is not registered.

Parameters
dataPointer to raw packet bytes
lengthAvailable bytes in buffer
Returns
DecodeViewResult with validation status and optional view

Looks up the wire size from GetMessageInfo() based on the message type ID in the packet header. Use this for general-purpose packet handling.

Parameters
dataRaw packet bytes
lengthNumber of bytes available
Returns
DecodeViewResult with view and error status

Definition at line 143 of file packet.cpp.

Here is the call graph for this function:

◆ DecodePacketViewAs()

template<typename MsgType >
DecodeViewResult bcnp::DecodePacketViewAs ( const uint8_t * data,
std::size_t length )

Type-safe packet view decode using compile-time message type.

Template version that uses MsgType::kWireSize directly, avoiding runtime registry lookup. Preferred when the expected type is known.

Template Parameters
MsgTypeExpected message type with kWireSize constant
Parameters
dataRaw packet bytes
lengthNumber of bytes available
Returns
DecodeViewResult with view and error status

Definition at line 490 of file packet.h.

Here is the call graph for this function:

◆ DecodePacketViewWithSize()

DecodeViewResult bcnp::DecodePacketViewWithSize ( const uint8_t * data,
std::size_t length,
std::size_t wireSize )

Decode a packet with explicitly provided message wire size.

Decode packet view with explicit wire size.

This is the core decoding function used by all other decode variants. Validates the header, checks CRC, and constructs a PacketView on success.

Parameters
dataPointer to raw packet bytes
lengthAvailable bytes in buffer
wireSizeSize of each message in bytes (from schema or lookup)
Returns
DecodeViewResult with validation status and optional view

Low-level decode function used when the message wire size is known (e.g., from a custom lookup table or test fixture).

Parameters
dataRaw packet bytes
lengthNumber of bytes available
wireSizeSize of each message in bytes
Returns
DecodeViewResult with view and error status

Definition at line 76 of file packet.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ DecodeTypedPacket()

template<typename MsgType >
std::optional< TypedPacket< MsgType > > bcnp::DecodeTypedPacket ( const PacketView & view)

Decode messages from a PacketView into a typed packet.

Converts a validated PacketView into a TypedPacket with heap-allocated storage. Returns nullopt if the view's message type doesn't match MsgType.

Template Parameters
MsgTypeExpected message type
Parameters
viewValidated packet view to decode from
Returns
TypedPacket containing decoded messages, or nullopt on type mismatch

Definition at line 397 of file packet.h.

◆ DecodeTypedPacketAs()

template<typename MsgType , typename Storage >
std::optional< TypedPacket< MsgType, Storage > > bcnp::DecodeTypedPacketAs ( const PacketView & view)

Decode messages from a PacketView with custom storage type.

Allows decoding into stack-allocated or custom storage for real-time applications. Use StaticVector<MsgType, N> for deterministic memory usage.

Template Parameters
MsgTypeExpected message type
StorageContainer type (e.g., StaticVector<MsgType, 64>)
Parameters
viewValidated packet view to decode from
Returns
TypedPacket with specified storage, or nullopt on type mismatch/decode failure

Definition at line 431 of file packet.h.

Here is the call graph for this function:

◆ EncodeHandshake()

bool bcnp::EncodeHandshake ( uint8_t * out,
std::size_t capacity )
inline

Encode handshake with default schema hash.

Definition at line 135 of file message_types.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EncodeHandshakeWithHash()

bool bcnp::EncodeHandshakeWithHash ( uint8_t * out,
std::size_t capacity,
uint32_t schemaHash )
inline

Encode handshake with custom schema hash (for testing)

Definition at line 143 of file message_types.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EncodeTypedPacket() [1/2]

template<typename MsgType , typename Storage >
bool bcnp::EncodeTypedPacket ( const TypedPacket< MsgType, Storage > & packet,
std::vector< uint8_t > & output )

Encode a typed packet to a dynamically-sized vector.

Convenience overload that allocates the required buffer automatically. Suitable for non-real-time code paths where allocation is acceptable.

Template Parameters
MsgTypeMessage struct type with Encode() and kWireSize
StorageContainer type holding the messages
Parameters
packetThe packet to encode
[out]outputVector to receive encoded bytes (resized automatically)
Returns
true if encoding succeeded, false on encoding failure

Definition at line 372 of file packet.h.

Here is the call graph for this function:

◆ EncodeTypedPacket() [2/2]

template<typename MsgType , typename Storage >
bool bcnp::EncodeTypedPacket ( const TypedPacket< MsgType, Storage > & packet,
uint8_t * output,
std::size_t capacity,
std::size_t & bytesWritten )

Encode a typed packet to a pre-allocated buffer.

Serializes the packet header and all messages to wire format, appending a CRC32 checksum. Works with any storage type (vector, StaticVector, etc.).

Template Parameters
MsgTypeMessage struct type with Encode() and kWireSize
StorageContainer type holding the messages
Parameters
packetThe packet to encode
outputDestination buffer (must have sufficient capacity)
capacitySize of output buffer in bytes
[out]bytesWrittenNumber of bytes written on success
Returns
true if encoding succeeded, false if capacity insufficient or encoding failed

Definition at line 322 of file packet.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ExtractSchemaHash()

uint32_t bcnp::ExtractSchemaHash ( const uint8_t * data,
std::size_t length )
inline

Definition at line 165 of file message_types.h.

Here is the call graph for this function:

◆ GetMessageInfo() [1/2]

std::optional< MessageInfo > bcnp::GetMessageInfo ( MessageTypeId typeId)
inline

Definition at line 119 of file message_types.h.

Here is the caller graph for this function:

◆ GetMessageInfo() [2/2]

std::optional< MessageInfo > bcnp::GetMessageInfo ( uint16_t typeId)
inline

Definition at line 126 of file message_types.h.

Here is the call graph for this function:

◆ ReserveIfPossible()

template<typename Container >
void bcnp::ReserveIfPossible ( Container & container,
std::size_t capacity )

Helper to reserve capacity (no-op for static storage).

This allows generic code to call reserve() without knowing storage type.

Definition at line 122 of file packet_storage.h.

Here is the caller graph for this function:

◆ ValidateHandshake()

bool bcnp::ValidateHandshake ( const uint8_t * data,
std::size_t length )
inline

Definition at line 150 of file message_types.h.

Here is the call graph for this function:

◆ ValidateHandshakeWithHash()

bool bcnp::ValidateHandshakeWithHash ( const uint8_t * data,
std::size_t length,
uint32_t expectedHash )
inline

Validate handshake against custom expected hash (for testing)

Definition at line 158 of file message_types.h.

Here is the call graph for this function:

Variable Documentation

◆ IsValidPacketStorage_v

template<typename Container >
bool bcnp::IsValidPacketStorage_v = IsValidPacketStorage<Container>::value
inlineconstexpr

Definition at line 87 of file packet_storage.h.

◆ kHandshakeMagic

std::array<uint8_t, 4> bcnp::kHandshakeMagic = {{'B', 'C', 'N', 'P'}}
constexpr

Definition at line 28 of file message_types.h.

◆ kHandshakeSize

std::size_t bcnp::kHandshakeSize = 8
constexpr

Definition at line 27 of file message_types.h.

◆ kHeaderMsgCountIndex

std::size_t bcnp::kHeaderMsgCountIndex = 5
constexpr

Definition at line 33 of file message_types.h.

◆ kHeaderMsgTypeIndex

std::size_t bcnp::kHeaderMsgTypeIndex = 3
constexpr

Definition at line 32 of file message_types.h.

◆ kHeaderSizeV3

std::size_t bcnp::kHeaderSizeV3 = 7
constexpr

Definition at line 31 of file message_types.h.

◆ kMessageRegistry

std::array<MessageInfo, 0> bcnp::kMessageRegistry = {{}}
inlineconstexpr

Definition at line 117 of file message_types.h.

◆ kProtocolMajorV3

uint8_t bcnp::kProtocolMajorV3 = 3
constexpr

Definition at line 22 of file message_types.h.

◆ kProtocolMinorV3

uint8_t bcnp::kProtocolMinorV3 = 2
constexpr

Definition at line 23 of file message_types.h.

◆ kSchemaHash

uint32_t bcnp::kSchemaHash = 0x5667F881U
constexpr

Definition at line 24 of file message_types.h.