Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 1 | /* |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 2 | * isdnhdlc.h -- General purpose ISDN HDLC decoder. |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 3 | * |
| 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 Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 23 | */ |
| 24 | |
Vadim Yanitskiy | 395db20 | 2022-09-28 23:51:42 +0700 | [diff] [blame] | 25 | #pragma once |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 26 | |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 27 | #include <stdint.h> |
| 28 | |
| 29 | struct osmo_isdnhdlc_vars { |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 30 | int bit_shift; |
| 31 | int hdlc_bits1; |
| 32 | int data_bits; |
| 33 | int ffbit_shift; /* encoding only */ |
| 34 | int state; |
| 35 | int dstpos; |
| 36 | |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 37 | uint16_t crc; |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 38 | |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 39 | uint8_t cbin; |
| 40 | uint8_t shift_reg; |
| 41 | uint8_t ffvalue; |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 42 | |
| 43 | /* set if transferring data */ |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 44 | uint32_t data_received:1; |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 45 | /* set if D channel (send idle instead of flags) */ |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 46 | uint32_t dchannel:1; |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 47 | /* set if 56K adaptation */ |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 48 | uint32_t do_adapt56:1; |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 49 | /* set if in closing phase (need to send CRC + flag) */ |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 50 | uint32_t do_closing:1; |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 51 | /* set if data is bitreverse */ |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 52 | uint32_t do_bitreverse:1; |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* Feature Flags */ |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 56 | #define OSMO_HDLC_F_56KBIT 0x01 |
| 57 | #define OSMO_HDLC_F_DCHANNEL 0x02 |
| 58 | #define OSMO_HDLC_F_BITREVERSE 0x04 |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 59 | |
| 60 | /* |
| 61 | The return value from isdnhdlc_decode is |
| 62 | the frame length, 0 if no complete frame was decoded, |
| 63 | or a negative error number |
| 64 | */ |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 65 | #define OSMO_HDLC_FRAMING_ERROR 1 |
| 66 | #define OSMO_HDLC_CRC_ERROR 2 |
| 67 | #define OSMO_HDLC_LENGTH_ERROR 3 |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 68 | |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 69 | extern void osmo_isdnhdlc_rcv_init(struct osmo_isdnhdlc_vars *hdlc, uint32_t features); |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 70 | |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 71 | extern int osmo_isdnhdlc_decode(struct osmo_isdnhdlc_vars *hdlc, const uint8_t *src, |
| 72 | int slen, int *count, uint8_t *dst, int dsize); |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 73 | |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 74 | extern void osmo_isdnhdlc_out_init(struct osmo_isdnhdlc_vars *hdlc, uint32_t features); |
Harald Welte | 85b9fba | 2018-05-11 17:36:13 +0200 | [diff] [blame] | 75 | |
Harald Welte | 9da77ab | 2018-05-11 17:39:21 +0200 | [diff] [blame] | 76 | extern int osmo_isdnhdlc_encode(struct osmo_isdnhdlc_vars *hdlc, const uint8_t *src, |
| 77 | uint16_t slen, int *count, uint8_t *dst, int dsize); |