spandsp  0.0.6
hdlc.h
Go to the documentation of this file.
1 /*
2  * SpanDSP - a series of DSP components for telephony
3  *
4  * hdlc.h
5  *
6  * Written by Steve Underwood <steveu@coppice.org>
7  *
8  * Copyright (C) 2003 Steve Underwood
9  *
10  * All rights reserved.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 2.1,
14  * as published by the Free Software Foundation.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25 
26 /*! \file */
27 
28 /*! \page hdlc_page HDLC
29 
30 \section hdlc_page_sec_1 What does it do?
31 The HDLC module provides bit stuffing, destuffing, framing and deframing,
32 according to the HDLC protocol. It also provides 16 and 32 bit CRC generation
33 and checking services for HDLC frames.
34 
35 HDLC may not be a DSP function, but is needed to accompany several DSP components.
36 
37 \section hdlc_page_sec_2 How does it work?
38 */
39 
40 #if !defined(_SPANDSP_HDLC_H_)
41 #define _SPANDSP_HDLC_H_
42 
43 /*!
44  HDLC_MAXFRAME_LEN is the maximum length of a stuffed HDLC frame, excluding the CRC.
45 */
46 #define HDLC_MAXFRAME_LEN 400
47 
48 typedef void (*hdlc_frame_handler_t)(void *user_data, const uint8_t *pkt, int len, int ok);
49 typedef void (*hdlc_underflow_handler_t)(void *user_data);
50 
51 /*!
52  HDLC receive descriptor. This contains all the state information for an HDLC receiver.
53  */
55 
56 /*!
57  HDLC received data statistics.
58  */
59 typedef struct
60 {
61  /*! \brief The number of bytes of good frames received (CRC not included). */
62  unsigned long int bytes;
63  /*! \brief The number of good frames received. */
64  unsigned long int good_frames;
65  /*! \brief The number of frames with CRC errors received. */
66  unsigned long int crc_errors;
67  /*! \brief The number of too short and too long frames received. */
68  unsigned long int length_errors;
69  /*! \brief The number of HDLC aborts received. */
70  unsigned long int aborts;
72 
73 /*!
74  HDLC transmit descriptor. This contains all the state information for an
75  HDLC transmitter.
76  */
78 
79 #if defined(__cplusplus)
80 extern "C"
81 {
82 #endif
83 
84 /*! \brief Initialise an HDLC receiver context.
85  \param s A pointer to an HDLC receiver context.
86  \param crc32 TRUE to use ITU CRC32. FALSE to use ITU CRC16.
87  \param report_bad_frames TRUE to request the reporting of bad frames.
88  \param framing_ok_threshold The number of back-to-back flags needed to
89  start the framing OK condition. This may be used where a series of
90  flag octets is used as a preamble, such as in the T.30 protocol.
91  \param handler The function to be called when a good HDLC frame is received.
92  \param user_data An opaque parameter for the callback routine.
93  \return A pointer to the HDLC receiver context.
94 */
96  int crc32,
97  int report_bad_frames,
98  int framing_ok_threshold,
99  hdlc_frame_handler_t handler,
100  void *user_data);
101 
102 /*! Change the put_bit function associated with an HDLC receiver context.
103  \brief Change the put_bit function associated with an HDLC receiver context.
104  \param s A pointer to an HDLC receiver context.
105  \param handler The function to be called when a good HDLC frame is received.
106  \param user_data An opaque parameter for the callback routine.
107 */
108 SPAN_DECLARE(void) hdlc_rx_set_frame_handler(hdlc_rx_state_t *s, hdlc_frame_handler_t handler, void *user_data);
109 
110 /*! Change the status report function associated with an HDLC receiver context.
111  \brief Change the status report function associated with an HDLC receiver context.
112  \param s A pointer to an HDLC receiver context.
113  \param handler The callback routine used to report status changes.
114  \param user_data An opaque parameter for the callback routine.
115 */
116 SPAN_DECLARE(void) hdlc_rx_set_status_handler(hdlc_rx_state_t *s, modem_rx_status_func_t handler, void *user_data);
117 
118 /*! Release an HDLC receiver context.
119  \brief Release an HDLC receiver context.
120  \param s A pointer to an HDLC receiver context.
121  \return 0 for OK */
122 SPAN_DECLARE(int) hdlc_rx_release(hdlc_rx_state_t *s);
123 
124 /*! Free an HDLC receiver context.
125  \brief Free an HDLC receiver context.
126  \param s A pointer to an HDLC receiver context.
127  \return 0 for OK */
128 SPAN_DECLARE(int) hdlc_rx_free(hdlc_rx_state_t *s);
129 
130 /*! \brief Set the maximum frame length for an HDLC receiver context.
131  \param s A pointer to an HDLC receiver context.
132  \param max_len The maximum permitted length of a frame.
133 */
134 SPAN_DECLARE(void) hdlc_rx_set_max_frame_len(hdlc_rx_state_t *s, size_t max_len);
135 
136 /*! \brief Set the octet counting report interval.
137  \param s A pointer to an HDLC receiver context.
138  \param interval The interval, in octets.
139 */
141  int interval);
142 
143 /*! \brief Get the current receive statistics.
144  \param s A pointer to an HDLC receiver context.
145  \param t A pointer to the buffer for the statistics.
146  \return 0 for OK, else -1.
147 */
148 SPAN_DECLARE(int) hdlc_rx_get_stats(hdlc_rx_state_t *s,
149  hdlc_rx_stats_t *t);
150 
151 /*! \brief Put a single bit of data to an HDLC receiver.
152  \param s A pointer to an HDLC receiver context.
153  \param new_bit The bit.
154 */
155 SPAN_DECLARE_NONSTD(void) hdlc_rx_put_bit(hdlc_rx_state_t *s, int new_bit);
156 
157 /*! \brief Put a byte of data to an HDLC receiver.
158  \param s A pointer to an HDLC receiver context.
159  \param new_byte The byte of data.
160 */
161 SPAN_DECLARE_NONSTD(void) hdlc_rx_put_byte(hdlc_rx_state_t *s, int new_byte);
162 
163 /*! \brief Put a series of bytes of data to an HDLC receiver.
164  \param s A pointer to an HDLC receiver context.
165  \param buf The buffer of data.
166  \param len The length of the data in the buffer.
167 */
168 SPAN_DECLARE_NONSTD(void) hdlc_rx_put(hdlc_rx_state_t *s, const uint8_t buf[], int len);
169 
170 /*! \brief Initialise an HDLC transmitter context.
171  \param s A pointer to an HDLC transmitter context.
172  \param crc32 TRUE to use ITU CRC32. FALSE to use ITU CRC16.
173  \param inter_frame_flags The minimum flag octets to insert between frames (usually one).
174  \param progressive TRUE if frame creation works in progressive mode.
175  \param handler The callback function called when the HDLC transmitter underflows.
176  \param user_data An opaque parameter for the callback routine.
177  \return A pointer to the HDLC transmitter context.
178 */
179 SPAN_DECLARE(hdlc_tx_state_t *) hdlc_tx_init(hdlc_tx_state_t *s,
180  int crc32,
181  int inter_frame_flags,
182  int progressive,
183  hdlc_underflow_handler_t handler,
184  void *user_data);
185 
186 SPAN_DECLARE(int) hdlc_tx_release(hdlc_tx_state_t *s);
187 
188 SPAN_DECLARE(int) hdlc_tx_free(hdlc_tx_state_t *s);
189 
190 /*! \brief Set the maximum frame length for an HDLC transmitter context.
191  \param s A pointer to an HDLC transmitter context.
192  \param max_len The maximum length.
193 */
194 SPAN_DECLARE(void) hdlc_tx_set_max_frame_len(hdlc_tx_state_t *s, size_t max_len);
195 
196 /*! \brief Transmit a frame.
197  \param s A pointer to an HDLC transmitter context.
198  \param frame A pointer to the frame to be transmitted.
199  \param len The length of the frame to be transmitted.
200  \return 0 if the frame was accepted for transmission, else -1.
201 */
202 SPAN_DECLARE(int) hdlc_tx_frame(hdlc_tx_state_t *s, const uint8_t *frame, size_t len);
203 
204 /*! \brief Corrupt the frame currently being transmitted, by giving it the wrong CRC.
205  \param s A pointer to an HDLC transmitter context.
206  \return 0 if the frame was corrupted, else -1.
207 */
208 SPAN_DECLARE(int) hdlc_tx_corrupt_frame(hdlc_tx_state_t *s);
209 
210 /*! \brief Transmit a specified quantity of flag octets, typically as a preamble.
211  \param s A pointer to an HDLC transmitter context.
212  \param len The length of the required period of flags, in flag octets. If len is zero this
213  requests that HDLC transmission be terminated when the buffers have fully
214  drained.
215  \return 0 if the flags were accepted for transmission, else -1.
216 */
217 SPAN_DECLARE(int) hdlc_tx_flags(hdlc_tx_state_t *s, int len);
218 
219 /*! \brief Send an abort.
220  \param s A pointer to an HDLC transmitter context.
221  \return 0 if the frame was aborted, else -1.
222 */
223 SPAN_DECLARE(int) hdlc_tx_abort(hdlc_tx_state_t *s);
224 
225 /*! \brief Get the next bit for transmission.
226  \param s A pointer to an HDLC transmitter context.
227  \return The next bit for transmission.
228 */
229 SPAN_DECLARE_NONSTD(int) hdlc_tx_get_bit(hdlc_tx_state_t *s);
230 
231 /*! \brief Get the next byte for transmission.
232  \param s A pointer to an HDLC transmitter context.
233  \return The next byte for transmission.
234 */
235 SPAN_DECLARE_NONSTD(int) hdlc_tx_get_byte(hdlc_tx_state_t *s);
236 
237 /*! \brief Get the next sequence of bytes for transmission.
238  \param s A pointer to an HDLC transmitter context.
239  \param buf The buffer for the data.
240  \param max_len The number of bytes to get.
241  \return The number of bytes actually got.
242 */
243 SPAN_DECLARE_NONSTD(int) hdlc_tx_get(hdlc_tx_state_t *s, uint8_t buf[], size_t max_len);
244 
245 #if defined(__cplusplus)
246 }
247 #endif
248 
249 #endif
250 /*- End of file ------------------------------------------------------------*/