File syn_mbus.h¶
FileList > proto > syn_mbus.h
Go to the source code of this file
M-Bus (Meter-Bus, EN 13757-2 / EN 13757-3) Protocol Engine. More...
#include "../common/syn_defs.h"#include <stdbool.h>#include <stddef.h>#include <stdint.h>
Classes¶
| Type | Name |
|---|---|
| struct | SYN_MBUS_Decoder M-Bus streaming state machine decoder. |
| struct | SYN_MBUS_Frame |
Public Types¶
| Type | Name |
|---|---|
| typedef void(* | SYN_MBUS_FrameCallback Callback invoked when a complete, valid M-Bus frame is received. |
| enum | SYN_MBUS_FrameType |
Public Functions¶
| Type | Name |
|---|---|
| uint8_t | syn_mbus_calc_checksum (const uint8_t * data, size_t len) Calculate M-Bus 8-bit checksum (modulo-256 sum over specified buffer). |
| SYN_Status | syn_mbus_decode_frame (const uint8_t * buf, size_t len, SYN_MBUS_Frame * frame) Decode a raw M-Bus byte buffer into a frame structure. |
| void | syn_mbus_decoder_feed (SYN_MBUS_Decoder * dec, uint8_t byte) Feed a single byte to the streaming M-Bus decoder. |
| void | syn_mbus_decoder_init (SYN_MBUS_Decoder * dec, SYN_MBUS_FrameCallback callback, void * ctx) Initialize an M-Bus streaming state machine decoder. |
| void | syn_mbus_decoder_reset (SYN_MBUS_Decoder * dec) Reset the streaming decoder state. |
| SYN_Status | syn_mbus_encode_ack (uint8_t * buf, size_t cap, size_t * out_len) Encode a Single Character ACK frame (0xE5). |
| SYN_Status | syn_mbus_encode_control (uint8_t c_field, uint8_t a_field, uint8_t ci_field, uint8_t * buf, size_t cap, size_t * out_len) Encode a Control Frame (9 bytes: 0x68 | 0x03 | 0x03 | 0x68 | C | A | CI | Checksum | 0x16). |
| SYN_Status | syn_mbus_encode_long (uint8_t c_field, uint8_t a_field, uint8_t ci_field, const uint8_t * payload, uint8_t payload_len, uint8_t * buf, size_t cap, size_t * out_len) Encode a Long Frame (0x68 | L | L | 0x68 | C | A | CI | Payload... | Checksum | 0x16). |
| SYN_Status | syn_mbus_encode_short (uint8_t c_field, uint8_t a_field, uint8_t * buf, size_t cap, size_t * out_len) Encode a Short Frame (5 bytes: 0x10 | C | A | Checksum | 0x16). |
Macros¶
| Type | Name |
|---|---|
| define | SYN_MBUS_ACK_BYTE 0xE5u |
| define | SYN_MBUS_ADDR_BROADCAST_NONE 0xFFu |
| define | SYN_MBUS_ADDR_BROADCAST_REPLY 0xFEu |
| define | SYN_MBUS_CI_RSP_DATA_LSB 0x72u |
| define | SYN_MBUS_CI_RSP_DATA_MSB 0x73u |
| define | SYN_MBUS_CI_SELECT_SLAVE 0x52u |
| define | SYN_MBUS_CI_SND_UD_LSB 0x51u |
| define | SYN_MBUS_C_REQ_UD1 0x5Au |
| define | SYN_MBUS_C_REQ_UD2 0x5Bu |
| define | SYN_MBUS_C_REQ_UD2_FCB 0x7Bu |
| define | SYN_MBUS_C_RSP_UD 0x08u |
| define | SYN_MBUS_C_SND_NKE 0x40u |
| define | SYN_MBUS_C_SND_UD 0x53u |
| define | SYN_MBUS_C_SND_UD_FCB 0x73u |
| define | SYN_MBUS_MAX_FRAME_LEN 261u |
| define | SYN_MBUS_MAX_PAYLOAD 252u |
| define | SYN_MBUS_START_CONTROL 0x68u |
| define | SYN_MBUS_START_LONG 0x68u |
| define | SYN_MBUS_START_SHORT 0x10u |
| define | SYN_MBUS_STOP 0x16u |
Detailed Description¶
Implements M-Bus master and slave frame formatting, checksum validation, one-shot encoding/decoding, and streaming byte-by-byte frame reception. Supports all 4 M-Bus EN 13757-2 frame types: * Single Character ACK (0xE5) * Short Frame (0x10) * Control Frame (0x68) * Long Frame (0x68)
Public Types Documentation¶
typedef SYN_MBUS_FrameCallback¶
Callback invoked when a complete, valid M-Bus frame is received.
Parameters:
framePointer to decoded frame structure.ctxUser context.
enum SYN_MBUS_FrameType¶
enum SYN_MBUS_FrameType {
SYN_MBUS_FRAME_TYPE_UNKNOWN = 0,
SYN_MBUS_FRAME_TYPE_SINGLE_ACK,
SYN_MBUS_FRAME_TYPE_SHORT,
SYN_MBUS_FRAME_TYPE_CONTROL,
SYN_MBUS_FRAME_TYPE_LONG
};
Public Functions Documentation¶
function syn_mbus_calc_checksum¶
Calculate M-Bus 8-bit checksum (modulo-256 sum over specified buffer).
Parameters:
dataPointer to buffer.lenLength of data in bytes.
Returns:
Calculated checksum byte.
function syn_mbus_decode_frame¶
Decode a raw M-Bus byte buffer into a frame structure.
Parameters:
bufInput byte buffer containing a complete M-Bus frame.lenBuffer length in bytes.frameDestination frame structure.
Returns:
SYN_OK on success, SYN_ERR_INVALID_CHECKSUM on checksum failure, or error status.
function syn_mbus_decoder_feed¶
Feed a single byte to the streaming M-Bus decoder.
Parameters:
decDecoder instance.byteByte received from UART/RS232/transceiver.
function syn_mbus_decoder_init¶
Initialize an M-Bus streaming state machine decoder.
Parameters:
decDecoder instance.callbackCallback called upon successful frame completion.ctxUser context passed to callback.
function syn_mbus_decoder_reset¶
Reset the streaming decoder state.
Parameters:
decDecoder instance.
function syn_mbus_encode_ack¶
Encode a Single Character ACK frame (0xE5).
Parameters:
bufOutput buffer.capBuffer capacity (must be >= 1).out_lenReceives total bytes written (1).
Returns:
SYN_OK on success, or error status.
function syn_mbus_encode_control¶
Encode a Control Frame (9 bytes: 0x68 | 0x03 | 0x03 | 0x68 | C | A | CI | Checksum | 0x16).
SYN_Status syn_mbus_encode_control (
uint8_t c_field,
uint8_t a_field,
uint8_t ci_field,
uint8_t * buf,
size_t cap,
size_t * out_len
)
Parameters:
c_fieldControl field byte.a_fieldAddress field byte.ci_fieldControl Information field byte.bufOutput buffer.capBuffer capacity (must be >= 9).out_lenReceives total bytes written (9).
Returns:
SYN_OK on success, or error status.
function syn_mbus_encode_long¶
Encode a Long Frame (0x68 | L | L | 0x68 | C | A | CI | Payload... | Checksum | 0x16).
SYN_Status syn_mbus_encode_long (
uint8_t c_field,
uint8_t a_field,
uint8_t ci_field,
const uint8_t * payload,
uint8_t payload_len,
uint8_t * buf,
size_t cap,
size_t * out_len
)
Parameters:
c_fieldControl field byte.a_fieldAddress field byte.ci_fieldControl Information field byte.payloadPointer to payload data buffer.payload_lenPayload length (max 252 bytes).bufOutput buffer.capBuffer capacity (must be >= payload_len + 9).out_lenReceives total bytes written.
Returns:
SYN_OK on success, or error status.
function syn_mbus_encode_short¶
Encode a Short Frame (5 bytes: 0x10 | C | A | Checksum | 0x16).
SYN_Status syn_mbus_encode_short (
uint8_t c_field,
uint8_t a_field,
uint8_t * buf,
size_t cap,
size_t * out_len
)
Parameters:
c_fieldControl field byte.a_fieldAddress field byte.bufOutput buffer.capBuffer capacity (must be >= 5).out_lenReceives total bytes written (5).
Returns:
SYN_OK on success, or error status.
Macro Definition Documentation¶
define SYN_MBUS_ACK_BYTE¶
Single character ACK byte
define SYN_MBUS_ADDR_BROADCAST_NONE¶
Broadcast without reply
define SYN_MBUS_ADDR_BROADCAST_REPLY¶
Broadcast with reply
define SYN_MBUS_CI_RSP_DATA_LSB¶
Response data, 12-byte header (LSB first)
define SYN_MBUS_CI_RSP_DATA_MSB¶
Response data, 12-byte header (MSB first)
define SYN_MBUS_CI_SELECT_SLAVE¶
Select slave (secondary address)
define SYN_MBUS_CI_SND_UD_LSB¶
Send data, 12-byte header
define SYN_MBUS_C_REQ_UD1¶
Master -> Slave: Request data class 1 (FCB=0)
define SYN_MBUS_C_REQ_UD2¶
Master -> Slave: Request data class 2 (FCB=0)
define SYN_MBUS_C_REQ_UD2_FCB¶
Master -> Slave: Request data class 2 (FCB=1)
define SYN_MBUS_C_RSP_UD¶
Slave -> Master: Response user data
define SYN_MBUS_C_SND_NKE¶
Master -> Slave: Link reset (SND_NKE)
define SYN_MBUS_C_SND_UD¶
Master -> Slave: Send user data (FCB=0)
define SYN_MBUS_C_SND_UD_FCB¶
Master -> Slave: Send user data (FCB=1)
define SYN_MBUS_MAX_FRAME_LEN¶
Max total raw frame size (SOF+L+L+SOF+C+A+CI+Data+CHK+EOF)
define SYN_MBUS_MAX_PAYLOAD¶
Max user payload size in bytes
define SYN_MBUS_START_CONTROL¶
Start delimiter for Control frame
define SYN_MBUS_START_LONG¶
Start delimiter for Long frame
define SYN_MBUS_START_SHORT¶
Start delimiter for Short frame
define SYN_MBUS_STOP¶
Stop delimiter for frames
The documentation for this class was generated from the following file src/syntropic/proto/syn_mbus.h