00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * t31.h - A T.31 compatible class 1 FAX modem interface. 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2004 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: t31.h 5963 2008-07-27 13:06:19Z oron $ 00026 */ 00027 00028 /*! \file */ 00029 00030 #if !defined(_SPANDSP_T31_H_) 00031 #define _SPANDSP_T31_H_ 00032 00033 /*! \page t31_page T.31 Class 1 FAX modem protocol handling 00034 \section t31_page_sec_1 What does it do? 00035 The T.31 class 1 FAX modem modules implements a class 1 interface to the FAX 00036 modems in spandsp. 00037 00038 \section t31_page_sec_2 How does it work? 00039 */ 00040 00041 typedef struct t31_state_s t31_state_t; 00042 00043 typedef int (t31_modem_control_handler_t)(t31_state_t *s, void *user_data, int op, const char *num); 00044 00045 #define T31_TX_BUF_LEN (4096) 00046 #define T31_TX_BUF_HIGH_TIDE (4096 - 1024) 00047 #define T31_TX_BUF_LOW_TIDE (1024) 00048 00049 /*! 00050 T.31 descriptor. This defines the working state for a single instance of 00051 a T.31 FAX modem. 00052 */ 00053 struct t31_state_s 00054 { 00055 at_state_t at_state; 00056 t31_modem_control_handler_t *modem_control_handler; 00057 void *modem_control_user_data; 00058 00059 /*! The current receive signal handler */ 00060 span_rx_handler_t *rx_handler; 00061 void *rx_user_data; 00062 00063 /*! The current transmit signal handler */ 00064 span_tx_handler_t *tx_handler; 00065 void *tx_user_data; 00066 /*! The transmit signal handler to be used when the current one has finished sending. */ 00067 span_tx_handler_t *next_tx_handler; 00068 void *next_tx_user_data; 00069 00070 /*! If TRUE, transmit silence when there is nothing else to transmit. If FALSE return only 00071 the actual generated audio. Note that this only affects untimed silences. Timed silences 00072 (e.g. the 75ms silence between V.21 and a high speed modem) will alway be transmitted as 00073 silent audio. */ 00074 int transmit_on_idle; 00075 00076 /*! \brief Use talker echo protection when transmitting. */ 00077 int use_tep; 00078 00079 uint8_t hdlc_tx_buf[256]; 00080 int hdlc_tx_len; 00081 int hdlc_tx_ptr; 00082 /*! TRUE if DLE prefix just used */ 00083 int dled; 00084 uint8_t tx_data[T31_TX_BUF_LEN]; 00085 /*! \brief The number of bytes stored in transmit buffer. */ 00086 int tx_in_bytes; 00087 /*! \brief The number of bytes sent from the transmit buffer. */ 00088 int tx_out_bytes; 00089 int tx_holding; 00090 int tx_data_started; 00091 int bit_no; 00092 int current_byte; 00093 00094 /*! \brief The current bit rate for the FAX fast message transfer modem. */ 00095 int bit_rate; 00096 int rx_message_received; 00097 00098 /*! \brief A tone generator context used to generate supervisory tones during 00099 FAX handling. */ 00100 tone_gen_state_t tone_gen; 00101 /*! \brief An HDLC context used when receiving HDLC over V.21 messages. */ 00102 hdlc_rx_state_t hdlcrx; 00103 /*! \brief An HDLC context used when transmitting HDLC over V.21 messages. */ 00104 hdlc_tx_state_t hdlctx; 00105 /*! \brief A V.21 FSK modem context used when transmitting HDLC over V.21 00106 messages. */ 00107 fsk_tx_state_t v21_tx; 00108 /*! \brief A V.21 FSK modem context used when receiving HDLC over V.21 00109 messages. */ 00110 fsk_rx_state_t v21_rx; 00111 00112 /*! \brief A V.17 modem context used when sending FAXes at 7200bps, 9600bps 00113 12000bps or 14400bps*/ 00114 v17_tx_state_t v17_tx; 00115 /*! \brief A V.29 modem context used when receiving FAXes at 7200bps, 9600bps 00116 12000bps or 14400bps*/ 00117 v17_rx_state_t v17_rx; 00118 00119 /*! \brief A V.27ter modem context used when sending FAXes at 2400bps or 00120 4800bps */ 00121 v27ter_tx_state_t v27ter_tx; 00122 /*! \brief A V.27ter modem context used when receiving FAXes at 2400bps or 00123 4800bps */ 00124 v27ter_rx_state_t v27ter_rx; 00125 00126 /*! \brief A V.29 modem context used when sending FAXes at 7200bps or 00127 9600bps */ 00128 v29_tx_state_t v29_tx; 00129 /*! \brief A V.29 modem context used when receiving FAXes at 7200bps or 00130 9600bps */ 00131 v29_rx_state_t v29_rx; 00132 00133 /*! \brief Used to insert timed silences. */ 00134 silence_gen_state_t silence_gen; 00135 00136 /*! \brief Rx power meter, use to detect silence */ 00137 power_meter_t rx_power; 00138 int16_t last_sample; 00139 int32_t silence_threshold_power; 00140 00141 t38_core_state_t t38; 00142 int rx_signal_present; 00143 /*! \brief The next queued tramsit indicator */ 00144 int next_tx_indicator; 00145 /*! \brief The current T.38 data type being transmitted */ 00146 int current_tx_data_type; 00147 00148 int ms_per_tx_chunk; 00149 int merge_tx_fields; 00150 00151 /*! \brief The number of times an indicator packet will be sent. Numbers greater than one 00152 will increase reliability for UDP transmission. Zero is valid, to suppress all 00153 indicator packets for TCP transmission. */ 00154 int indicator_tx_count; 00155 00156 /*! \brief The number of times a data packet which ends transmission will be sent. Numbers 00157 greater than one will increase reliability for UDP transmission. Zero is not valid. */ 00158 int data_end_tx_count; 00159 00160 /*! \brief A "sample" count, used to time events */ 00161 int32_t samples; 00162 int32_t next_tx_samples; 00163 int32_t timeout_rx_samples; 00164 00165 /*! \brief Samples of silence heard */ 00166 int silence_heard; 00167 /*! \brief Samples of silence awaited */ 00168 int silence_awaited; 00169 /*! \brief Samples elapsed in the current call */ 00170 int64_t call_samples; 00171 int64_t dte_data_timeout; 00172 int modem; 00173 int short_train; 00174 int hdlc_final; 00175 int data_final; 00176 queue_state_t *rx_queue; 00177 00178 uint8_t hdlc_rx_buf[256]; 00179 int hdlc_rx_len; 00180 00181 int t38_mode; 00182 int timed_step; 00183 int current_tx_data; 00184 00185 int current_rx_type; 00186 int current_tx_type; 00187 00188 int trailer_bytes; 00189 00190 /*! \brief TRUE is there has been some T.38 data missed */ 00191 int missing_data; 00192 00193 /*! \brief The number of octets to send in each image packet (non-ECM or ECM) at the current 00194 rate and the current specified packet interval. */ 00195 int octets_per_data_packet; 00196 00197 /*! \brief Error and flow logging control */ 00198 logging_state_t logging; 00199 }; 00200 00201 #if defined(__cplusplus) 00202 extern "C" 00203 { 00204 #endif 00205 00206 void t31_call_event(t31_state_t *s, int event); 00207 00208 int t31_at_rx(t31_state_t *s, const char *t, int len); 00209 00210 /*! Process a block of received T.31 modem audio samples. 00211 \brief Process a block of received T.31 modem audio samples. 00212 \param s The T.31 modem context. 00213 \param amp The audio sample buffer. 00214 \param len The number of samples in the buffer. 00215 \return The number of samples unprocessed. */ 00216 int t31_rx(t31_state_t *s, int16_t amp[], int len); 00217 00218 /*! Generate a block of T.31 modem audio samples. 00219 \brief Generate a block of T.31 modem audio samples. 00220 \param s The T.31 modem context. 00221 \param amp The audio sample buffer. 00222 \param max_len The number of samples to be generated. 00223 \return The number of samples actually generated. 00224 */ 00225 int t31_tx(t31_state_t *s, int16_t amp[], int max_len); 00226 00227 int t31_t38_send_timeout(t31_state_t *s, int samples); 00228 00229 /*! Select whether silent audio will be sent when transmit is idle. 00230 \brief Select whether silent audio will be sent when transmit is idle. 00231 \param s The T.31 modem context. 00232 \param transmit_on_idle TRUE if silent audio should be output when the transmitter is 00233 idle. FALSE to transmit zero length audio when the transmitter is idle. The default 00234 behaviour is FALSE. 00235 */ 00236 void t31_set_transmit_on_idle(t31_state_t *s, int transmit_on_idle); 00237 00238 /*! Select whether TEP mode will be used (or time allowed for it (when transmitting). 00239 \brief Select whether TEP mode will be used. 00240 \param s The T.31 modem context. 00241 \param use_tep TRUE if TEP is to be ised. 00242 */ 00243 void t31_set_tep_mode(t31_state_t *s, int use_tep); 00244 00245 /*! Select whether T.38 data will be paced as it is transmitted. 00246 \brief Select whether T.38 data will be paced. 00247 \param s The T.31 modem context. 00248 \param without_pacing TRUE if data is to be sent as fast as possible. FALSE if it is 00249 to be paced. 00250 */ 00251 void t31_set_t38_config(t31_state_t *s, int without_pacing); 00252 00253 /*! Initialise a T.31 context. This must be called before the first 00254 use of the context, to initialise its contents. 00255 \brief Initialise a T.31 context. 00256 \param s The T.31 context. 00257 \param at_tx_handler A callback routine to handle AT interpreter channel output. 00258 \param at_tx_user_data An opaque pointer passed in called to at_tx_handler. 00259 \param modem_control_handler A callback routine to handle control of the modem (off-hook, etc). 00260 \param modem_control_user_data An opaque pointer passed in called to modem_control_handler. 00261 \param tx_t38_packet_handler ??? 00262 \param tx_t38_packet_user_data ??? 00263 \return A pointer to the T.31 context. */ 00264 t31_state_t *t31_init(t31_state_t *s, 00265 at_tx_handler_t *at_tx_handler, 00266 void *at_tx_user_data, 00267 t31_modem_control_handler_t *modem_control_handler, 00268 void *modem_control_user_data, 00269 t38_tx_packet_handler_t *tx_t38_packet_handler, 00270 void *tx_t38_packet_user_data); 00271 00272 /*! Release a T.31 context. 00273 \brief Release a T.31 context. 00274 \param s The T.31 context. 00275 \return 0 for OK */ 00276 int t31_release(t31_state_t *s); 00277 00278 #if defined(__cplusplus) 00279 } 00280 #endif 00281 00282 #endif 00283 /*- End of file ------------------------------------------------------------*/