Files
heltec-repeater/firmware/main/wmbus.h
T

39 lines
1.5 KiB
C

// Wireless M-Bus C-mode frame helpers (EN 13757-4).
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#define WMBUS_MODE_BYTE_A_HI 0x54
#define WMBUS_MODE_BYTE_A_LO 0xCD // C-mode frame format A: sync 543D 54CD
#define WMBUS_MODE_BYTE_B_LO 0x3D // C-mode frame format B: sync 543D 543D
typedef enum {
WMBUS_FRAME_A,
WMBUS_FRAME_B,
WMBUS_FRAME_UNKNOWN, // not C-mode (e.g. T-mode 3-of-6 stream)
} wmbus_frame_fmt_t;
// Identify frame format from the two payload "mode bytes".
wmbus_frame_fmt_t wmbus_frame_format(uint8_t b0, uint8_t b1);
// Total over-the-air frame length (including L-field and all CRC bytes)
// for a given L-field. Returns -1 if invalid/oversized.
int wmbus_frame_total_len(wmbus_frame_fmt_t fmt, uint8_t lfield);
// Validate all block CRCs of a complete raw frame. frame[0] is the L-field.
bool wmbus_check_crc(wmbus_frame_fmt_t fmt, const uint8_t *frame, int total_len);
// CRC-16 EN 13757 (poly 0x3D65, init 0, complemented result).
uint16_t wmbus_crc16(const uint8_t *data, size_t len);
// Build a valid C-mode format A test frame with our test manufacturer/ID.
// Returns total frame length written to out (>= 12), or -1 if payload_len
// does not fit. out[0] = L-field.
int wmbus_build_test_frame(uint8_t *out, size_t maxlen, uint8_t access_nr,
uint8_t lfield);
// 32-bit FNV-1a hash used to correlate RX/TX events in the logs.
uint32_t wmbus_hash(const uint8_t *data, size_t len);