Skip to content

File syn_mbus.c

FileList > proto > syn_mbus.c

Go to the source code of this file

M-Bus (Meter-Bus) master/slave protocol framing implementation.

  • #include "../util/syn_assert.h"
  • #include "syn_mbus.h"
  • #include <string.h>

Public Types

Type Name
enum syn__mbus_8c_1adf764cbdea00d65edcd07bb9953ad2b7

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).

Public Types Documentation

enum syn__mbus_8c_1adf764cbdea00d65edcd07bb9953ad2b7

enum syn__mbus_8c_1adf764cbdea00d65edcd07bb9953ad2b7 {
    STATE_IDLE = 0,
    STATE_SHORT_C,
    STATE_SHORT_A,
    STATE_SHORT_CHK,
    STATE_SHORT_STOP,
    STATE_LONG_L1,
    STATE_LONG_L2,
    STATE_LONG_SOF2,
    STATE_LONG_BODY
};

Public Functions Documentation

function syn_mbus_calc_checksum

Calculate M-Bus 8-bit checksum (modulo-256 sum over specified buffer).

uint8_t syn_mbus_calc_checksum (
    const uint8_t * data,
    size_t len
) 

Parameters:

  • data Pointer to buffer.
  • len Length of data in bytes.

Returns:

Calculated checksum byte.


function syn_mbus_decode_frame

Decode a raw M-Bus byte buffer into a frame structure.

SYN_Status syn_mbus_decode_frame (
    const uint8_t * buf,
    size_t len,
    SYN_MBUS_Frame * frame
) 

Parameters:

  • buf Input byte buffer containing a complete M-Bus frame.
  • len Buffer length in bytes.
  • frame Destination 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.

void syn_mbus_decoder_feed (
    SYN_MBUS_Decoder * dec,
    uint8_t byte
) 

Parameters:

  • dec Decoder instance.
  • byte Byte received from UART/RS232/transceiver.

function syn_mbus_decoder_init

Initialize an M-Bus streaming state machine decoder.

void syn_mbus_decoder_init (
    SYN_MBUS_Decoder * dec,
    SYN_MBUS_FrameCallback callback,
    void * ctx
) 

Parameters:

  • dec Decoder instance.
  • callback Callback called upon successful frame completion.
  • ctx User context passed to callback.

function syn_mbus_decoder_reset

Reset the streaming decoder state.

void syn_mbus_decoder_reset (
    SYN_MBUS_Decoder * dec
) 

Parameters:

  • dec Decoder instance.

function syn_mbus_encode_ack

Encode a Single Character ACK frame (0xE5).

SYN_Status syn_mbus_encode_ack (
    uint8_t * buf,
    size_t cap,
    size_t * out_len
) 

Parameters:

  • buf Output buffer.
  • cap Buffer capacity (must be >= 1).
  • out_len Receives 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_field Control field byte.
  • a_field Address field byte.
  • ci_field Control Information field byte.
  • buf Output buffer.
  • cap Buffer capacity (must be >= 9).
  • out_len Receives 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_field Control field byte.
  • a_field Address field byte.
  • ci_field Control Information field byte.
  • payload Pointer to payload data buffer.
  • payload_len Payload length (max 252 bytes).
  • buf Output buffer.
  • cap Buffer capacity (must be >= payload_len + 9).
  • out_len Receives 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_field Control field byte.
  • a_field Address field byte.
  • buf Output buffer.
  • cap Buffer capacity (must be >= 5).
  • out_len Receives total bytes written (5).

Returns:

SYN_OK on success, or error status.



The documentation for this class was generated from the following file src/syntropic/proto/syn_mbus.c