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.40 2008/11/30 05:43:37 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 hdlc_rx_state_s hdlc_rx_state_t; 00057 00058 /*! 00059 HDLC received data statistics. 00060 */ 00061 typedef struct 00062 { 00063 /*! \brief The number of bytes of good frames received (CRC not included). */ 00064 unsigned long int bytes; 00065 /*! \brief The number of good frames received. */ 00066 unsigned long int good_frames; 00067 /*! \brief The number of frames with CRC errors received. */ 00068 unsigned long int crc_errors; 00069 /*! \brief The number of too short and too long frames received. */ 00070 unsigned long int length_errors; 00071 /*! \brief The number of HDLC aborts received. */ 00072 unsigned long int aborts; 00073 } hdlc_rx_stats_t; 00074 00075 /*! 00076 HDLC transmit descriptor. This contains all the state information for an 00077 HDLC transmitter. 00078 */ 00079 typedef struct hdlc_tx_state_s hdlc_tx_state_t; 00080 00081 #if defined(__cplusplus) 00082 extern "C" 00083 { 00084 #endif 00085 00086 /*! \brief Initialise an HDLC receiver context. 00087 \param s A pointer to an HDLC receiver context. 00088 \param crc32 TRUE to use ITU CRC32. FALSE to use ITU CRC16. 00089 \param report_bad_frames TRUE to request the reporting of bad frames. 00090 \param framing_ok_threshold The number of back-to-back flags needed to 00091 start the framing OK condition. This may be used where a series of 00092 flag octets is used as a preamble, such as in the T.30 protocol. 00093 \param handler The function to be called when a good HDLC frame is received. 00094 \param user_data An opaque parameter for the callback routine. 00095 \return A pointer to the HDLC receiver context. 00096 */ 00097 hdlc_rx_state_t *hdlc_rx_init(hdlc_rx_state_t *s, 00098 int crc32, 00099 int report_bad_frames, 00100 int framing_ok_threshold, 00101 hdlc_frame_handler_t handler, 00102 void *user_data); 00103 00104 /*! \brief Set the maximum frame length for an HDLC receiver context. 00105 \param s A pointer to an HDLC receiver context. 00106 \param max_len The maximum permitted length of a frame. 00107 */ 00108 void hdlc_rx_set_max_frame_len(hdlc_rx_state_t *s, size_t max_len); 00109 00110 /*! \brief Set the octet counting report interval. 00111 \param s A pointer to an HDLC receiver context. 00112 \param interval The interval, in octets. 00113 */ 00114 void hdlc_rx_set_octet_counting_report_interval(hdlc_rx_state_t *s, int interval); 00115 00116 /*! \brief Get the current receive statistics. 00117 \param s A pointer to an HDLC receiver context. 00118 \param t A pointer to the buffer for the statistics. 00119 \return 0 for OK, else -1. 00120 */ 00121 int hdlc_rx_get_stats(hdlc_rx_state_t *s, 00122 hdlc_rx_stats_t *t); 00123 00124 /*! \brief Put a single bit of data to an HDLC receiver. 00125 \param s A pointer to an HDLC receiver context. 00126 \param new_bit The bit. 00127 */ 00128 void hdlc_rx_put_bit(hdlc_rx_state_t *s, int new_bit); 00129 00130 /*! \brief Put a byte of data to an HDLC receiver. 00131 \param s A pointer to an HDLC receiver context. 00132 \param new_byte The byte of data. 00133 */ 00134 void hdlc_rx_put_byte(hdlc_rx_state_t *s, int new_byte); 00135 00136 /*! \brief Put a series of bytes of data to an HDLC receiver. 00137 \param s A pointer to an HDLC receiver context. 00138 \param buf The buffer of data. 00139 \param len The length of the data in the buffer. 00140 */ 00141 void hdlc_rx_put(hdlc_rx_state_t *s, const uint8_t buf[], int len); 00142 00143 /*! \brief Initialise an HDLC transmitter context. 00144 \param s A pointer to an HDLC transmitter context. 00145 \param crc32 TRUE to use ITU CRC32. FALSE to use ITU CRC16. 00146 \param inter_frame_flags The minimum flag octets to insert between frames (usually one). 00147 \param progressive TRUE if frame creation works in progressive mode. 00148 \param handler The callback function called when the HDLC transmitter underflows. 00149 \param user_data An opaque parameter for the callback routine. 00150 \return A pointer to the HDLC transmitter context. 00151 */ 00152 hdlc_tx_state_t *hdlc_tx_init(hdlc_tx_state_t *s, 00153 int crc32, 00154 int inter_frame_flags, 00155 int progressive, 00156 hdlc_underflow_handler_t handler, 00157 void *user_data); 00158 00159 /*! \brief Set the maximum frame length for an HDLC transmitter context. 00160 \param s A pointer to an HDLC transmitter context. 00161 \param max_len The maximum length. 00162 */ 00163 void hdlc_tx_set_max_frame_len(hdlc_tx_state_t *s, size_t max_len); 00164 00165 /*! \brief Transmit a frame. 00166 \param s A pointer to an HDLC transmitter context. 00167 \param frame A pointer to the frame to be transmitted. 00168 \param len The length of the frame to be transmitted. 00169 \return 0 if the frame was accepted for transmission, else -1. 00170 */ 00171 int hdlc_tx_frame(hdlc_tx_state_t *s, const uint8_t *frame, size_t len); 00172 00173 /*! \brief Corrupt the frame currently being transmitted, by giving it the wrong CRC. 00174 \param s A pointer to an HDLC transmitter context. 00175 \return 0 if the frame was corrupted, else -1. 00176 */ 00177 int hdlc_tx_corrupt_frame(hdlc_tx_state_t *s); 00178 00179 /*! \brief Transmit a specified quantity of flag octets, typically as a preamble. 00180 \param s A pointer to an HDLC transmitter context. 00181 \param len The length of the required period of flags, in flag octets. If len is zero this 00182 requests that HDLC transmission be terminated when the buffers have fully 00183 drained. 00184 \return 0 if the flags were accepted for transmission, else -1. 00185 */ 00186 int hdlc_tx_flags(hdlc_tx_state_t *s, int len); 00187 00188 /*! \brief Send an abort. 00189 \param s A pointer to an HDLC transmitter context. 00190 \return 0 if the frame was aborted, else -1. 00191 */ 00192 int hdlc_tx_abort(hdlc_tx_state_t *s); 00193 00194 /*! \brief Get the next bit for transmission. 00195 \param s A pointer to an HDLC transmitter context. 00196 \return The next bit for transmission. 00197 */ 00198 int hdlc_tx_get_bit(hdlc_tx_state_t *s); 00199 00200 /*! \brief Get the next byte for transmission. 00201 \param s A pointer to an HDLC transmitter context. 00202 \return The next byte for transmission. 00203 */ 00204 int hdlc_tx_get_byte(hdlc_tx_state_t *s); 00205 00206 /*! \brief Get the next sequence of bytes for transmission. 00207 \param s A pointer to an HDLC transmitter context. 00208 \param buf The buffer for the data. 00209 \param max_len The number of bytes to get. 00210 \return The number of bytes actually got. 00211 */ 00212 int hdlc_tx_get(hdlc_tx_state_t *s, uint8_t buf[], size_t max_len); 00213 00214 #if defined(__cplusplus) 00215 } 00216 #endif 00217 00218 #endif 00219 /*- End of file ------------------------------------------------------------*/