blob: 43e6e7ac6d0b9562533a46b14e0d5c0dd99920ad [file] [log] [blame]
Harald Welte85b9fba2018-05-11 17:36:13 +02001/*
Harald Welte9da77ab2018-05-11 17:39:21 +02002 * isdnhdlc.h -- General purpose ISDN HDLC decoder.
Harald Welte85b9fba2018-05-11 17:36:13 +02003 *
4 * Implementation of a HDLC decoder/encoder in software.
5 * Necessary because some ISDN devices don't have HDLC
6 * controllers.
7 *
8 * Copyright (C)
9 * 2009 Karsten Keil <keil@b1-systems.de>
10 * 2002 Wolfgang Mües <wolfgang@iksw-muees.de>
11 * 2001 Frode Isaksen <fisaksen@bewan.com>
12 * 2001 Kai Germaschewski <kai.germaschewski@gmx.de>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
Harald Welte85b9fba2018-05-11 17:36:13 +020023 */
24
25#ifndef __ISDNHDLC_H__
26#define __ISDNHDLC_H__
27
Harald Welte9da77ab2018-05-11 17:39:21 +020028#include <stdint.h>
29
30struct osmo_isdnhdlc_vars {
Harald Welte85b9fba2018-05-11 17:36:13 +020031 int bit_shift;
32 int hdlc_bits1;
33 int data_bits;
34 int ffbit_shift; /* encoding only */
35 int state;
36 int dstpos;
37
Harald Welte9da77ab2018-05-11 17:39:21 +020038 uint16_t crc;
Harald Welte85b9fba2018-05-11 17:36:13 +020039
Harald Welte9da77ab2018-05-11 17:39:21 +020040 uint8_t cbin;
41 uint8_t shift_reg;
42 uint8_t ffvalue;
Harald Welte85b9fba2018-05-11 17:36:13 +020043
44 /* set if transferring data */
Harald Welte9da77ab2018-05-11 17:39:21 +020045 uint32_t data_received:1;
Harald Welte85b9fba2018-05-11 17:36:13 +020046 /* set if D channel (send idle instead of flags) */
Harald Welte9da77ab2018-05-11 17:39:21 +020047 uint32_t dchannel:1;
Harald Welte85b9fba2018-05-11 17:36:13 +020048 /* set if 56K adaptation */
Harald Welte9da77ab2018-05-11 17:39:21 +020049 uint32_t do_adapt56:1;
Harald Welte85b9fba2018-05-11 17:36:13 +020050 /* set if in closing phase (need to send CRC + flag) */
Harald Welte9da77ab2018-05-11 17:39:21 +020051 uint32_t do_closing:1;
Harald Welte85b9fba2018-05-11 17:36:13 +020052 /* set if data is bitreverse */
Harald Welte9da77ab2018-05-11 17:39:21 +020053 uint32_t do_bitreverse:1;
Harald Welte85b9fba2018-05-11 17:36:13 +020054};
55
56/* Feature Flags */
Harald Welte9da77ab2018-05-11 17:39:21 +020057#define OSMO_HDLC_F_56KBIT 0x01
58#define OSMO_HDLC_F_DCHANNEL 0x02
59#define OSMO_HDLC_F_BITREVERSE 0x04
Harald Welte85b9fba2018-05-11 17:36:13 +020060
61/*
62 The return value from isdnhdlc_decode is
63 the frame length, 0 if no complete frame was decoded,
64 or a negative error number
65*/
Harald Welte9da77ab2018-05-11 17:39:21 +020066#define OSMO_HDLC_FRAMING_ERROR 1
67#define OSMO_HDLC_CRC_ERROR 2
68#define OSMO_HDLC_LENGTH_ERROR 3
Harald Welte85b9fba2018-05-11 17:36:13 +020069
Harald Welte9da77ab2018-05-11 17:39:21 +020070extern void osmo_isdnhdlc_rcv_init(struct osmo_isdnhdlc_vars *hdlc, uint32_t features);
Harald Welte85b9fba2018-05-11 17:36:13 +020071
Harald Welte9da77ab2018-05-11 17:39:21 +020072extern int osmo_isdnhdlc_decode(struct osmo_isdnhdlc_vars *hdlc, const uint8_t *src,
73 int slen, int *count, uint8_t *dst, int dsize);
Harald Welte85b9fba2018-05-11 17:36:13 +020074
Harald Welte9da77ab2018-05-11 17:39:21 +020075extern void osmo_isdnhdlc_out_init(struct osmo_isdnhdlc_vars *hdlc, uint32_t features);
Harald Welte85b9fba2018-05-11 17:36:13 +020076
Harald Welte9da77ab2018-05-11 17:39:21 +020077extern int osmo_isdnhdlc_encode(struct osmo_isdnhdlc_vars *hdlc, const uint8_t *src,
78 uint16_t slen, int *count, uint8_t *dst, int dsize);
Harald Welte85b9fba2018-05-11 17:36:13 +020079
80#endif /* __ISDNHDLC_H__ */