00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * t38_core.h - An implementation of T.38, less the packet exchange part 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2005 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 General Public License version 2, as 00014 * 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 General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU General Public License 00022 * along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00024 * 00025 * $Id: t38_core.h,v 1.20 2007/06/08 13:49:38 steveu Exp $ 00026 */ 00027 00028 /*! \file */ 00029 00030 #if !defined(_SPANDSP_T38_CORE_H_) 00031 #define _SPANDSP_T38_CORE_H_ 00032 00033 /*! \page t38_core_page T.38 real time FAX over IP message handling 00034 There are two ITU recommendations which address sending FAXes over IP networks. T.37 specifies a 00035 method of encapsulating FAX images in e-mails, and transporting them to the recipient (an e-mail 00036 box, or another FAX machine) in a store-and-forward manner. T.38 defines a protocol for 00037 transmitting a FAX across an IP network in real time. The core T.38 modules implements the basic 00038 message handling for the T.38, real time, FAX over IP (FoIP) protocol. 00039 00040 The T.38 protocol can operate between: 00041 - Internet-aware FAX terminals, which connect directly to an IP network. The T.38 terminal module 00042 extends this module to provide a complete T.38 terminal. 00043 - FAX gateways, which allow traditional PSTN FAX terminals to communicate through the Internet. 00044 The T.38 gateway module extends this module to provide a T.38 gateway. 00045 - A combination of terminals and gateways. 00046 00047 T.38 is the only standardised protocol which exists for real-time FoIP. Reliably transporting a 00048 FAX between PSTN FAX terminals, through an IP network, requires use of the T.38 protocol at FAX 00049 gateways. VoIP connections are not robust for modem use, including FAX modem use. Most use low 00050 bit rate codecs, which cannot convey the modem signals accurately. Even when high bit rate 00051 codecs are used, VoIP connections suffer dropouts and timing adjustments, which modems cannot 00052 tolerate. In a LAN environment the dropout rate may be very low, but the timing adjustments which 00053 occur in VoIP connections still make modem operation unreliable. T.38 FAX gateways deal with the 00054 delays, timing jitter, and packet loss experienced in packet networks, and isolate the PSTN FAX 00055 terminals from these as far as possible. In addition, by sending FAXes as image data, rather than 00056 digitised audio, they reduce the required bandwidth of the IP network. 00057 00058 \section t38_core_page_sec_1 What does it do? 00059 00060 \section t38_core_page_sec_2 How does it work? 00061 00062 Timing differences and jitter between two T.38 entities can be a serious problem, if one of those 00063 entities is a PSTN gateway. 00064 00065 Flow control for non-ECM image data takes advantage of several features of the T.30 specification. 00066 First, an unspecified number of 0xFF octets may be sent at the start of transmission. This means we 00067 can add endless extra 0xFF bytes at this point, without breaking the T.30 spec. In practice, we 00068 cannot add too many, or we will affect the timing tolerance of the T.30 protocol by delaying the 00069 response at the end of each image. Secondly, just before an end of line (EOL) marker we can pad 00070 with zero bits. Again, the number is limited only by need to avoid upsetting the timing of the 00071 step following the non-ECM data. 00072 */ 00073 00074 enum t30_indicator_types_e 00075 { 00076 T38_IND_NO_SIGNAL = 0, 00077 T38_IND_CNG, 00078 T38_IND_CED, 00079 T38_IND_V21_PREAMBLE, 00080 T38_IND_V27TER_2400_TRAINING, 00081 T38_IND_V27TER_4800_TRAINING, 00082 T38_IND_V29_7200_TRAINING, 00083 T38_IND_V29_9600_TRAINING, 00084 T38_IND_V17_7200_SHORT_TRAINING, 00085 T38_IND_V17_7200_LONG_TRAINING, 00086 T38_IND_V17_9600_SHORT_TRAINING, 00087 T38_IND_V17_9600_LONG_TRAINING, 00088 T38_IND_V17_12000_SHORT_TRAINING, 00089 T38_IND_V17_12000_LONG_TRAINING, 00090 T38_IND_V17_14400_SHORT_TRAINING, 00091 T38_IND_V17_14400_LONG_TRAINING, 00092 T38_IND_V8_ANSAM, 00093 T38_IND_V8_SIGNAL, 00094 T38_IND_V34_CNTL_CHANNEL_1200, 00095 T38_IND_V34_PRI_CHANNEL, 00096 T38_IND_V34_CC_RETRAIN, 00097 T38_IND_V33_12000_TRAINING, 00098 T38_IND_V33_14400_TRAINING 00099 }; 00100 00101 enum t38_data_types_e 00102 { 00103 T38_DATA_NONE = -1, 00104 T38_DATA_V21 = 0, 00105 T38_DATA_V27TER_2400, 00106 T38_DATA_V27TER_4800, 00107 T38_DATA_V29_7200, 00108 T38_DATA_V29_9600, 00109 T38_DATA_V17_7200, 00110 T38_DATA_V17_9600, 00111 T38_DATA_V17_12000, 00112 T38_DATA_V17_14400, 00113 T38_DATA_V8, 00114 T38_DATA_V34_PRI_RATE, 00115 T38_DATA_V34_CC_1200, 00116 T38_DATA_V34_PRI_CH, 00117 T38_DATA_V33_12000, 00118 T38_DATA_V33_14400 00119 }; 00120 00121 enum t38_field_types_e 00122 { 00123 T38_FIELD_HDLC_DATA = 0, 00124 T38_FIELD_HDLC_SIG_END, 00125 T38_FIELD_HDLC_FCS_OK, 00126 T38_FIELD_HDLC_FCS_BAD, 00127 T38_FIELD_HDLC_FCS_OK_SIG_END, 00128 T38_FIELD_HDLC_FCS_BAD_SIG_END, 00129 T38_FIELD_T4_NON_ECM_DATA, 00130 T38_FIELD_T4_NON_ECM_SIG_END, 00131 T38_FIELD_CM_MESSAGE, 00132 T38_FIELD_JM_MESSAGE, 00133 T38_FIELD_CI_MESSAGE, 00134 T38_FIELD_V34RATE 00135 }; 00136 00137 enum t38_field_classes_e 00138 { 00139 T38_FIELD_CLASS_NONE = 0, 00140 T38_FIELD_CLASS_HDLC, 00141 T38_FIELD_CLASS_NON_ECM, 00142 }; 00143 00144 enum t38_message_types_e 00145 { 00146 T38_TYPE_OF_MSG_T30_INDICATOR = 0, 00147 T38_TYPE_OF_MSG_T30_DATA 00148 }; 00149 00150 enum t38_transport_types_e 00151 { 00152 T38_TRANSPORT_UDPTL = 0, 00153 T38_TRANSPORT_RTP, 00154 T38_TRANSPORT_TCP 00155 }; 00156 00157 #define T38_RX_BUF_LEN 2048 00158 #define T38_TX_BUF_LEN 16384 00159 00160 typedef struct 00161 { 00162 int field_type; 00163 const uint8_t *field; 00164 int field_len; 00165 } t38_data_field_t; 00166 00167 typedef struct t38_core_state_s t38_core_state_t; 00168 00169 typedef int (t38_tx_packet_handler_t)(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count); 00170 00171 typedef int (t38_rx_indicator_handler_t)(t38_core_state_t *s, void *user_data, int indicator); 00172 typedef int (t38_rx_data_handler_t)(t38_core_state_t *s, void *user_data, int data_type, int field_type, const uint8_t *buf, int len); 00173 typedef int (t38_rx_missing_handler_t)(t38_core_state_t *s, void *user_data, int rx_seq_no, int expected_seq_no); 00174 00175 #include <sys/time.h> 00176 00177 /* 00178 Core T.38 state, common to all modes of T.38. 00179 */ 00180 struct t38_core_state_s 00181 { 00182 /*! Handler routine to transmit IFP packets generated by the T.38 protocol engine */ 00183 t38_tx_packet_handler_t *tx_packet_handler; 00184 /*! An opaque pointer passed to tx_packet_handler */ 00185 void *tx_packet_user_data; 00186 00187 /*! Handler routine to process received indicator packets */ 00188 t38_rx_indicator_handler_t *rx_indicator_handler; 00189 /*! Handler routine to process received data packets */ 00190 t38_rx_data_handler_t *rx_data_handler; 00191 /*! Handler routine to process the missing packet condition */ 00192 t38_rx_missing_handler_t *rx_missing_handler; 00193 /*! An opaque pointer passed to any of the above receive handling routines */ 00194 void *rx_user_data; 00195 00196 /*! NOTE - Bandwidth reduction shall only be done on suitable Phase C data, i.e., MH, MR 00197 and - in the case of transcoding to JBIG - MMR. MMR and JBIG require reliable data 00198 transport such as that provided by TCP. When transcoding is selected, it shall be 00199 applied to every suitable page in a call. */ 00200 00201 /*! Method 1: Local generation of TCF (required for use with TCP). 00202 Method 2: Transfer of TCF is required for use with UDP (UDPTL or RTP). 00203 Method 2 is not recommended for use with TCP. */ 00204 int data_rate_management_method; 00205 00206 /*! The emitting gateway may indicate a preference for either UDP/UDPTL, or 00207 UDP/RTP, or TCP for transport of T.38 IFP Packets. The receiving device 00208 selects the transport protocol. */ 00209 int data_transport_protocol; 00210 00211 /*! Indicates the capability to remove and insert fill bits in Phase C, non-ECM 00212 data to reduce bandwidth in the packet network. Optional. See Note. */ 00213 int fill_bit_removal; 00214 00215 /*! Indicates the ability to convert to/from MMR from/to the line format to 00216 improve the compression of the data, and reduce the bandwidth, in the 00217 packet network. Optional. See Note. */ 00218 int mmr_transcoding; 00219 00220 /*! Indicates the ability to convert to/from JBIG to reduce bandwidth. Optional. 00221 See Note. */ 00222 int jbig_transcoding; 00223 00224 /*! For UDP (UDPTL or RTP) modes, this option indicates the maximum 00225 number of octets that can be stored on the remote device before an overflow 00226 condition occurs. It is the responsibility of the transmitting application to 00227 limit the transfer rate to prevent an overflow. The negotiated data rate 00228 should be used to determine the rate at which data is being removed from 00229 the buffer. */ 00230 int max_buffer_size; 00231 00232 /*! This option indicates the maximum size of a UDPTL packet or the 00233 maximum size of the payload within an RTP packet that can be accepted by 00234 the remote device. */ 00235 int max_datagram_size; 00236 00237 /*! This is the version number of ITU-T Rec. T.38. New versions shall be 00238 compatible with previous versions. */ 00239 int t38_version; 00240 00241 /*! The fastest data rate supported by the T.38 channel. */ 00242 int fastest_image_data_rate; 00243 00244 /*! TRUE if IFP packet sequence numbers are relevant. For some transports, like TPKT 00245 over TCP they are not relevent. */ 00246 int check_sequence_numbers; 00247 00248 /*! The sequence number for the next packet to be transmitted */ 00249 int tx_seq_no; 00250 /*! The sequence number expected in the next received packet */ 00251 int rx_expected_seq_no; 00252 00253 /*! The current receive indicator - i.e. the last indicator received */ 00254 int current_rx_indicator; 00255 /*! The current receive data type - i.e. the last data type received */ 00256 int current_rx_data_type; 00257 /*! The current receive field type - i.e. the last field_type received */ 00258 int current_rx_field_type; 00259 /*! The current transmit indicator - i.e. the last indicator transmitted */ 00260 int current_tx_indicator; 00261 00262 /*! A count of missing receive packets. This count might not be accurate if the 00263 received packet numbers jump wildly. */ 00264 int missing_packets; 00265 00266 logging_state_t logging; 00267 }; 00268 00269 #if defined(__cplusplus) 00270 extern "C" 00271 { 00272 #endif 00273 00274 /*! \brief Convert the code for an indicator to a short text name. 00275 \param indicator The type of indicator. 00276 \return A pointer to a short text name for the indicator. */ 00277 const char *t38_indicator(int indicator); 00278 00279 /*! \brief Convert the code for a type of data to a short text name. 00280 \param data_type The data type. 00281 \return A pointer to a short text name for the data type. */ 00282 const char *t38_data_type(int data_type); 00283 00284 /*! \brief Convert the code for a type of data field to a short text name. 00285 \param field_type The field type. 00286 \return A pointer to a short text name for the field type. */ 00287 const char *t38_field_type(int field_type); 00288 00289 /*! \brief Send an indicator packet 00290 \param s The T.38 context. 00291 \param indicator The indicator to send. 00292 \param count The number of copies of the packet to send. 00293 \return ??? */ 00294 int t38_core_send_indicator(t38_core_state_t *s, int indicator, int count); 00295 00296 /*! \brief Send a data packet 00297 \param s The T.38 context. 00298 \param data_type The packet's data type. 00299 \param field_type The packet's field type. 00300 \param field The message data content for the packet. 00301 \param field_len The length of the message data, in bytes. 00302 \param count The number of copies of the packet to send. 00303 \return ??? */ 00304 int t38_core_send_data(t38_core_state_t *s, int data_type, int field_type, const uint8_t field[], int field_len, int count); 00305 00306 /*! \brief Send a data packet 00307 \param s The T.38 context. 00308 \param data_type The packet's data type. 00309 \param field The list of fields. 00310 \param fields The number of fields in the list. 00311 \param count The number of copies of the packet to send. 00312 \return ??? */ 00313 int t38_core_send_data_multi_field(t38_core_state_t *s, int data_type, const t38_data_field_t field[], int fields, int count); 00314 00315 /*! \brief Process a received T.38 IFP packet. 00316 \param s The T.38 context. 00317 \param buf The packet contents. 00318 \param len The length of the packet contents. 00319 \param seq_no The packet sequence number. 00320 \return 0 for OK, else -1. */ 00321 int t38_core_rx_ifp_packet(t38_core_state_t *s, const uint8_t *buf, int len, uint16_t seq_no); 00322 00323 /*! Set the method to be used for data rate management, as per the T.38 spec. 00324 \param s The T.38 context. 00325 \param method 1 for pass TCF across the T.38 link, 2 for handle TCF locally. 00326 */ 00327 void t38_set_data_rate_management_method(t38_core_state_t *s, int method); 00328 00329 /*! Set the data transport protocol. 00330 \param s The T.38 context. 00331 \param data_transport_protocol UDPTL, RTP or TPKT. 00332 */ 00333 void t38_set_data_transport_protocol(t38_core_state_t *s, int data_transport_protocol); 00334 00335 /*! Set the non-ECM fill bit removal mode. 00336 \param s The T.38 context. 00337 \param fill_bit_removal TRUE to remove fill bits across the T.38 link, else FALSE. 00338 */ 00339 void t38_set_fill_bit_removal(t38_core_state_t *s, int fill_bit_removal); 00340 00341 /*! Set the MMR transcoding mode. 00342 \param s The T.38 context. 00343 \param mmr_transcoding TRUE to transcode to MMR across the T.38 link, else FALSE. 00344 */ 00345 void t38_set_mmr_transcoding(t38_core_state_t *s, int mmr_transcoding); 00346 00347 /*! Set the JBIG transcoding mode. 00348 \param s The T.38 context. 00349 \param jbig_transcoding TRUE to transcode to JBIG across the T.38 link, else FALSE. 00350 */ 00351 void t38_set_jbig_transcoding(t38_core_state_t *s, int jbig_transcoding); 00352 00353 void t38_set_max_buffer_size(t38_core_state_t *s, int max_buffer_size); 00354 00355 void t38_set_max_datagram_size(t38_core_state_t *s, int max_datagram_size); 00356 00357 int t38_get_fastest_image_data_rate(t38_core_state_t *s); 00358 00359 /*! Set the T.38 version to be emulated. 00360 \param s The T.38 context. 00361 \param t38_version Version number, as in the T.38 spec. 00362 */ 00363 void t38_set_t38_version(t38_core_state_t *s, int t38_version); 00364 00365 /* Set the sequence number handling option. 00366 \param s The T.38 context. 00367 \param check TRUE to check sequence numbers, and handle gaps reasonably. FALSE 00368 for no sequence number processing (e.g. for TPKT over TCP transport). 00369 */ 00370 void t38_set_sequence_number_handling(t38_core_state_t *s, int check); 00371 00372 t38_core_state_t *t38_core_init(t38_core_state_t *s, 00373 t38_rx_indicator_handler_t *rx_indicator_handler, 00374 t38_rx_data_handler_t *rx_data_handler, 00375 t38_rx_missing_handler_t *rx_missing_handler, 00376 void *rx_user_data, 00377 t38_tx_packet_handler_t tx_packet_handler, 00378 void *tx_packet_user_data); 00379 00380 #if defined(__cplusplus) 00381 } 00382 #endif 00383 00384 #endif 00385 /*- End of file ------------------------------------------------------------*/