From 90bd7b6709607791ca7cea18006e28980fa4b8ce Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Mon, 14 Nov 2016 22:06:46 +0100 Subject: hdlc: add call-back function for completely decoded HDLC frames ... and some more comments/documentation --- src/hdlc.c | 19 +++++++++++++++---- src/hdlc.h | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/hdlc.c b/src/hdlc.c index 19ba900..d97e6b4 100644 --- a/src/hdlc.c +++ b/src/hdlc.c @@ -35,10 +35,13 @@ static int append_bit_out(struct hdlc_proc *hdlc, uint8_t bit) hdlc->num_bits++; if (hdlc->num_bits == 8) { - hdlc->num_bits = 0; pbit_t out; + hdlc->num_bits = 0; /* generate one output byte */ osmo_ubit2pbit_ext(&out, 0, hdlc->next_outbyte, 0, 8, 0); + /* append to output buffer */ + OSMO_ASSERT(hdlc->out.len < sizeof(hdlc->out.buf)); + hdlc->out.buf[hdlc->out.len++] = out; return out; } @@ -58,6 +61,14 @@ static int process_hdlc_bit(struct hdlc_proc *hdlc, uint8_t bit) hdlc->num_bits = 0; DEBUGP("S "); flag = 1; + if (hdlc->out.len) { + /* call output function if any frame was + * received before the flag octet */ + if (hdlc->out_cb) + hdlc->out_cb(hdlc->out.buf, hdlc->out.len, + hdlc->priv); + hdlc->out.len = 0; + } } else if (!memcmp(five_ones_zero, hdlc->history, sizeof(five_ones_zero))) { /* 4.3.1 Synchronous transmission: receiver shall * discard any "0" bit after five contiguous ones */ @@ -78,16 +89,16 @@ static int process_hdlc_bit(struct hdlc_proc *hdlc, uint8_t bit) return out; } -int process_raw_hdlc(struct hdlc_proc *hdlc, uint8_t *data, unsigned int len) +int process_raw_hdlc(struct hdlc_proc *hdlc, ubit_t *bits, unsigned int len) { unsigned int i; int out; static int last_out; - DEBUGP("process_raw_hdlc(%s)\n", osmo_hexdump(data,len)); + DEBUGP("process_raw_hdlc(%s)\n", osmo_hexdump(bits, len)); for (i = 0; i < len; i ++) { - out = process_hdlc_bit(hdlc, data[i]); + out = process_hdlc_bit(hdlc, bits[i]); if (out == -123) { /* suppress repeating Flag characters */ if (last_out != out) diff --git a/src/hdlc.h b/src/hdlc.h index 2d44e75..2760213 100644 --- a/src/hdlc.h +++ b/src/hdlc.h @@ -2,10 +2,26 @@ #include +/* structure representing a HDLC decoder process */ struct hdlc_proc { + /* 8 bits history of most recently received bits */ ubit_t history[8]; + /* 8 bit buffer for assembling the next output byte */ ubit_t next_outbyte[8]; + /* number of bits currently in use in next_outbyte */ uint8_t num_bits; + + /* output buffer for re-aligned frame bytes */ + struct { + uint8_t buf[1024]; + unsigned int len; + } out; + + /* call-back ot be called at end of frame */ + void (*out_cb)(const uint8_t *data, unsigned int len, void *priv); + /* private data passed to out_cb */ + void *priv; }; -int process_raw_hdlc(struct hdlc_proc *hdlc, uint8_t *data, unsigned int len); +/* input given number of raw bits from the bit stream into HDLC */ +int process_raw_hdlc(struct hdlc_proc *hdlc, ubit_t *bits, unsigned int len); -- cgit v1.2.3