00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * hdlc.h 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2003 Steve Underwood 00009 * 00010 * All rights reserved. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU Lesser General Public License version 2.1, 00014 * as published by the Free Software Foundation. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00024 * 00025 * $Id: hdlc.h,v 1.39 2008/04/17 14:27:00 steveu Exp $ 00026 */ 00027 00028 /*! \file */ 00029 00030 /*! \page hdlc_page HDLC 00031 00032 \section hdlc_page_sec_1 What does it do? 00033 The HDLC module provides bit stuffing, destuffing, framing and deframing, 00034 according to the HDLC protocol. It also provides 16 and 32 bit CRC generation 00035 and checking services for HDLC frames. 00036 00037 HDLC may not be a DSP function, but is needed to accompany several DSP components. 00038 00039 \section hdlc_page_sec_2 How does it work? 00040 */ 00041 00042 #if !defined(_SPANDSP_HDLC_H_) 00043 #define _SPANDSP_HDLC_H_ 00044 00045 /*! 00046 HDLC_MAXFRAME_LEN is the maximum length of a stuffed HDLC frame, excluding the CRC. 00047 */ 00048 #define HDLC_MAXFRAME_LEN 400 00049 00050 typedef void (*hdlc_frame_handler_t)(void *user_data, const uint8_t *pkt, int len, int ok); 00051 typedef void (*hdlc_underflow_handler_t)(void *user_data); 00052 00053 /*! 00054 HDLC receive descriptor. This contains all the state information for an HDLC receiver. 00055 */ 00056 typedef struct 00057 { 00058 /*! 2 for CRC-16, 4 for CRC-32 */ 00059 int crc_bytes; 00060 /*! \brief Maximum permitted frame length. */ 00061 size_t max_frame_len; 00062 /*! \brief The callback routine called to process each good received frame. */ 00063 hdlc_frame_handler_t frame_handler; 00064 /*! \brief An opaque parameter passed to the callback routine. */ 00065 void *user_data; 00066 /*! \brief TRUE if bad frames are to be reported. */ 00067 int report_bad_frames; 00068 /*! \brief The number of consecutive flags which must be seen before framing is 00069 declared OK. */ 00070 int framing_ok_threshold; 00071 /*! \brief TRUE if framing OK has been announced. */ 00072 int framing_ok_announced; 00073 /*! \brief Number of consecutive flags seen so far. */ 00074 int flags_seen; 00075 00076 /*! \brief The raw (stuffed) bit stream buffer. */ 00077 unsigned int raw_bit_stream; 00078 /*! \brief The destuffed bit stream buffer. */ 00079 unsigned int byte_in_progress; 00080 /*! \brief The current number of bits in byte_in_progress. */ 00081 int num_bits; 00082 /*! \brief TRUE if in octet counting mode (e.g. for MTP). */ 00083 int octet_counting_mode; 00084 /*! \brief Octet count, to achieve the functionality needed for things 00085 like MTP. */ 00086 int octet_count; 00087 /*! \brief The number of octets to be allowed between octet count reports. */ 00088 int octet_count_report_interval; 00089 00090 /*! \brief Buffer for a frame in progress. */ 00091 uint8_t buffer[HDLC_MAXFRAME_LEN + 4]; 00092 /*! \brief Length of a frame in progress. */ 00093 size_t len; 00094 00095 /*! \brief The number of bytes of good frames received (CRC not included). */ 00096 unsigned long int rx_bytes; 00097 /*! \brief The number of good frames received. */ 00098 unsigned long int rx_frames; 00099 /*! \brief The number of frames with CRC errors received. */ 00100 unsigned long int rx_crc_errors; 00101 /*! \brief The number of too short and too long frames received. */ 00102 unsigned long int rx_length_errors; 00103 /*! \brief The number of HDLC aborts received. */ 00104 unsigned long int rx_aborts; 00105 } hdlc_rx_state_t; 00106 00107 /*! 00108 HDLC received data statistics. 00109 */ 00110 typedef struct 00111 { 00112 /*! \brief The number of bytes of good frames received (CRC not included). */ 00113 unsigned long int bytes; 00114 /*! \brief The number of good frames received. */ 00115 unsigned long int good_frames; 00116 /*! \brief The number of frames with CRC errors received. */ 00117 unsigned long int crc_errors; 00118 /*! \brief The number of too short and too long frames received. */ 00119 unsigned long int length_errors; 00120 /*! \brief The number of HDLC aborts received. */ 00121 unsigned long int aborts; 00122 } hdlc_rx_stats_t; 00123 00124 /*! 00125 HDLC transmit descriptor. This contains all the state information for an 00126 HDLC transmitter. 00127 */ 00128 typedef struct 00129 { 00130 /*! 2 for CRC-16, 4 for CRC-32 */ 00131 int crc_bytes; 00132 /*! \brief The callback routine called to indicate transmit underflow. */ 00133 hdlc_underflow_handler_t underflow_handler; 00134 /*! \brief An opaque parameter passed to the callback routine. */ 00135 void *user_data; 00136 /*! \brief The minimum flag octets to insert between frames. */ 00137 int inter_frame_flags; 00138 /*! \brief TRUE if frame creation works in progressive mode. */ 00139 int progressive; 00140 /*! \brief Maximum permitted frame length. */ 00141 size_t max_frame_len; 00142 00143 /*! \brief The stuffed bit stream being created. */ 00144 uint32_t octets_in_progress; 00145 /*! \brief The number of bits currently in octets_in_progress. */ 00146 int num_bits; 00147 /*! \brief The currently rotated state of the flag octet. */ 00148 int idle_octet; 00149 /*! \brief The number of flag octets to send for a timed burst of flags. */ 00150 int flag_octets; 00151 /*! \brief The number of abort octets to send for a timed burst of aborts. */ 00152 int abort_octets; 00153 /*! \brief TRUE if the next underflow of timed flag octets should be reported */ 00154 int report_flag_underflow; 00155 00156 /*! \brief The current message being transmitted, with its CRC attached. */ 00157 uint8_t buffer[HDLC_MAXFRAME_LEN + 4]; 00158 /*! \brief The length of the message in the buffer. */ 00159 size_t len; 00160 /*! \brief The current send position within the buffer. */ 00161 int pos; 00162 /*! \brief The running CRC, as data fills the frame buffer. */ 00163 uint32_t crc; 00164 00165 /*! \brief The current byte being broken into bits for transmission. */ 00166 int byte; 00167 /*! \brief The number of bits remaining in byte. */ 00168 int bits; 00169 00170 /*! \brief TRUE if transmission should end on buffer underflow .*/ 00171 int tx_end; 00172 } hdlc_tx_state_t; 00173 00174 #if defined(__cplusplus) 00175 extern "C" 00176 { 00177 #endif 00178 00179 /*! \brief Initialise an HDLC receiver context. 00180 \param s A pointer to an HDLC receiver context. 00181 \param crc32 TRUE to use ITU CRC32. FALSE to use ITU CRC16. 00182 \param report_bad_frames TRUE to request the reporting of bad frames. 00183 \param framing_ok_threshold The number of back-to-back flags needed to 00184 start the framing OK condition. This may be used where a series of 00185 flag octets is used as a preamble, such as in the T.30 protocol. 00186 \param handler The function to be called when a good HDLC frame is received. 00187 \param user_data An opaque parameter for the callback routine. 00188 \return A pointer to the HDLC receiver context. 00189 */ 00190 hdlc_rx_state_t *hdlc_rx_init(hdlc_rx_state_t *s, 00191 int crc32, 00192 int report_bad_frames, 00193 int framing_ok_threshold, 00194 hdlc_frame_handler_t handler, 00195 void *user_data); 00196 00197 /*! \brief Set the maximum frame length for an HDLC receiver context. 00198 \param s A pointer to an HDLC receiver context. 00199 \param max_len The maximum permitted length of a frame. 00200 */ 00201 void hdlc_rx_set_max_frame_len(hdlc_rx_state_t *s, size_t max_len); 00202 00203 /*! \brief Set the octet counting report interval. 00204 \param s A pointer to an HDLC receiver context. 00205 \param interval The interval, in octets. 00206 */ 00207 void hdlc_rx_set_octet_counting_report_interval(hdlc_rx_state_t *s, int interval); 00208 00209 /*! \brief Get the current receive statistics. 00210 \param s A pointer to an HDLC receiver context. 00211 \param t A pointer to the buffer for the statistics. 00212 \return 0 for OK, else -1. 00213 */ 00214 int hdlc_rx_get_stats(hdlc_rx_state_t *s, 00215 hdlc_rx_stats_t *t); 00216 00217 /*! \brief Put a single bit of data to an HDLC receiver. 00218 \param s A pointer to an HDLC receiver context. 00219 \param new_bit The bit. 00220 */ 00221 void hdlc_rx_put_bit(hdlc_rx_state_t *s, int new_bit); 00222 00223 /*! \brief Put a byte of data to an HDLC receiver. 00224 \param s A pointer to an HDLC receiver context. 00225 \param new_byte The byte of data. 00226 */ 00227 void hdlc_rx_put_byte(hdlc_rx_state_t *s, int new_byte); 00228 00229 /*! \brief Put a series of bytes of data to an HDLC receiver. 00230 \param s A pointer to an HDLC receiver context. 00231 \param buf The buffer of data. 00232 \param len The length of the data in the buffer. 00233 */ 00234 void hdlc_rx_put(hdlc_rx_state_t *s, const uint8_t buf[], int len); 00235 00236 /*! \brief Initialise an HDLC transmitter context. 00237 \param s A pointer to an HDLC transmitter context. 00238 \param crc32 TRUE to use ITU CRC32. FALSE to use ITU CRC16. 00239 \param inter_frame_flags The minimum flag octets to insert between frames (usually one). 00240 \param progressive TRUE if frame creation works in progressive mode. 00241 \param handler The callback function called when the HDLC transmitter underflows. 00242 \param user_data An opaque parameter for the callback routine. 00243 \return A pointer to the HDLC transmitter context. 00244 */ 00245 hdlc_tx_state_t *hdlc_tx_init(hdlc_tx_state_t *s, 00246 int crc32, 00247 int inter_frame_flags, 00248 int progressive, 00249 hdlc_underflow_handler_t handler, 00250 void *user_data); 00251 00252 /*! \brief Set the maximum frame length for an HDLC transmitter context. 00253 \param s A pointer to an HDLC transmitter context. 00254 \param max_len The maximum length. 00255 */ 00256 void hdlc_tx_set_max_frame_len(hdlc_tx_state_t *s, size_t max_len); 00257 00258 /*! \brief Transmit a frame. 00259 \param s A pointer to an HDLC transmitter context. 00260 \param frame A pointer to the frame to be transmitted. 00261 \param len The length of the frame to be transmitted. 00262 \return 0 if the frame was accepted for transmission, else -1. 00263 */ 00264 int hdlc_tx_frame(hdlc_tx_state_t *s, const uint8_t *frame, size_t len); 00265 00266 /*! \brief Corrupt the frame currently being transmitted, by giving it the wrong CRC. 00267 \param s A pointer to an HDLC transmitter context. 00268 \return 0 if the frame was corrupted, else -1. 00269 */ 00270 int hdlc_tx_corrupt_frame(hdlc_tx_state_t *s); 00271 00272 /*! \brief Transmit a specified quantity of flag octets, typically as a preamble. 00273 \param s A pointer to an HDLC transmitter context. 00274 \param len The length of the required period of flags, in flag octets. If len is zero this 00275 requests that HDLC transmission be terminated when the buffers have fully 00276 drained. 00277 \return 0 if the flags were accepted for transmission, else -1. 00278 */ 00279 int hdlc_tx_flags(hdlc_tx_state_t *s, int len); 00280 00281 /*! \brief Send an abort. 00282 \param s A pointer to an HDLC transmitter context. 00283 \return 0 if the frame was aborted, else -1. 00284 */ 00285 int hdlc_tx_abort(hdlc_tx_state_t *s); 00286 00287 /*! \brief Get the next bit for transmission. 00288 \param s A pointer to an HDLC transmitter context. 00289 \return The next bit for transmission. 00290 */ 00291 int hdlc_tx_get_bit(hdlc_tx_state_t *s); 00292 00293 /*! \brief Get the next byte for transmission. 00294 \param s A pointer to an HDLC transmitter context. 00295 \return The next byte for transmission. 00296 */ 00297 int hdlc_tx_get_byte(hdlc_tx_state_t *s); 00298 00299 /*! \brief Get the next sequence of bytes for transmission. 00300 \param s A pointer to an HDLC transmitter context. 00301 \param buf The buffer for the data. 00302 \param max_len The number of bytes to get. 00303 \return The number of bytes actually got. 00304 */ 00305 int hdlc_tx_get(hdlc_tx_state_t *s, uint8_t buf[], size_t max_len); 00306 00307 #if defined(__cplusplus) 00308 } 00309 #endif 00310 00311 #endif 00312 /*- End of file ------------------------------------------------------------*/