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

72 lines
3.2 KiB
C

// Wireless M-Bus C/T-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);
// Mark an eligible OMS uplink as repeated in place, repairing its block CRC.
// Caller must first validate all CRCs. Supports ELL 8C/8E or a direct short/
// long uplink TPL header in security mode 0/5. ELL takes precedence.
// Returns false, leaving frame unchanged, for unsupported/malformed headers,
// non-SND_NR/SND_IR C-fields, H=1 or R=1. No payload decryption is performed.
bool wmbus_mark_repeated(wmbus_frame_fmt_t fmt, uint8_t *frame, int total_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);
// ---- T-mode "3 of 6" codec (EN 13757-4) -------------------------------
// Chips travel MSB-first: the first received chip is bit 7 of coded[0].
// T1 shares the 0x543D access code with C-mode; after it, T1 sends coded
// data directly (no mode bytes). Tables verified against rtl-wmbus
// t1_c1_packet_decoder.h (HIGH/LOW_NIBBLE_3OUTOF6).
// Coded on-air byte count for a decoded byte count: ceil(total * 12 / 8).
int wmbus_3of6_coded_len(int total_len);
// Decode total_len bytes (2 * total_len six-bit codes) from coded[].
// Invalid codes decode as 0x0 nibbles so the CRC can still judge the
// frame. Returns the number of invalid codes (0 = clean), or -1 if
// coded_len holds fewer than total_len * 12 bits.
int wmbus_3of6_decode(const uint8_t *coded, size_t coded_len,
uint8_t *out, int total_len);
// Decode only the first byte (the T1 L-field). Returns 0..255, or -1 if
// either 6-bit code is invalid or fewer than 2 bytes are available.
int wmbus_3of6_first_byte(const uint8_t *coded, size_t coded_len);
// Encode total_len bytes into 3-of-6 chips, padding the final partial
// byte with 01 postamble chips. Returns coded byte count, or -1 if it
// does not fit maxlen.
int wmbus_3of6_encode(const uint8_t *data, int total_len,
uint8_t *coded, size_t maxlen);