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 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: t31.h,v 1.42 2007/04/30 13:13:36 steveu Exp $ 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*32) 00046 #define T31_TX_BUF_HIGH_TIDE (4096*32 - 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 uint8_t hdlc_tx_buf[256]; 00077 int hdlc_tx_len; 00078 int hdlc_tx_ptr; 00079 /*! TRUE if DLE prefix just used */ 00080 int dled; 00081 uint8_t tx_data[T31_TX_BUF_LEN]; 00082 /*! \brief The number of bytes stored in transmit buffer. */ 00083 int tx_in_bytes; 00084 /*! \brief The number of bytes sent from the transmit buffer. */ 00085 int tx_out_bytes; 00086 int tx_holding; 00087 int tx_data_started; 00088 int bit_no; 00089 int current_byte; 00090 00091 /*! \brief The current bit rate for the FAX fast message transfer modem. */ 00092 int bit_rate; 00093 int rx_message_received; 00094 00095 /*! \brief A tone generator context used to generate supervisory tones during 00096 FAX handling. */ 00097 tone_gen_state_t tone_gen; 00098 /*! \brief An HDLC context used when receiving HDLC over V.21 messages. */ 00099 hdlc_rx_state_t hdlcrx; 00100 /*! \brief An HDLC context used when transmitting HDLC over V.21 messages. */ 00101 hdlc_tx_state_t hdlctx; 00102 /*! \brief A V.21 FSK modem context used when transmitting HDLC over V.21 00103 messages. */ 00104 fsk_tx_state_t v21tx; 00105 /*! \brief A V.21 FSK modem context used when receiving HDLC over V.21 00106 messages. */ 00107 fsk_rx_state_t v21rx; 00108 00109 #if defined(ENABLE_V17) 00110 /*! \brief A V.17 modem context used when sending FAXes at 7200bps, 9600bps 00111 12000bps or 14400bps*/ 00112 v17_tx_state_t v17tx; 00113 /*! \brief A V.29 modem context used when receiving FAXes at 7200bps, 9600bps 00114 12000bps or 14400bps*/ 00115 v17_rx_state_t v17rx; 00116 #endif 00117 00118 /*! \brief A V.29 modem context used when sending FAXes at 7200bps or 00119 9600bps */ 00120 v29_tx_state_t v29tx; 00121 /*! \brief A V.29 modem context used when receiving FAXes at 7200bps or 00122 9600bps */ 00123 v29_rx_state_t v29rx; 00124 00125 /*! \brief A V.27ter modem context used when sending FAXes at 2400bps or 00126 4800bps */ 00127 v27ter_tx_state_t v27ter_tx; 00128 /*! \brief A V.27ter modem context used when receiving FAXes at 2400bps or 00129 4800bps */ 00130 v27ter_rx_state_t v27ter_rx; 00131 00132 /*! \brief Used to insert timed silences. */ 00133 silence_gen_state_t silence_gen; 00134 00135 /*! \brief Rx power meter, use to detect silence */ 00136 power_meter_t rx_power; 00137 int16_t last_sample; 00138 int32_t silence_threshold_power; 00139 00140 t38_core_state_t t38; 00141 00142 /*! \brief Samples of silence heard */ 00143 int silence_heard; 00144 /*! \brief Samples of silence awaited */ 00145 int silence_awaited; 00146 /*! \brief Samples elapsed in the current call */ 00147 int64_t call_samples; 00148 int64_t dte_data_timeout; 00149 int modem; 00150 int short_train; 00151 int hdlc_final; 00152 int data_final; 00153 queue_state_t *rx_queue; 00154 00155 uint8_t hdlc_rx_buf[256]; 00156 int hdlc_rx_len; 00157 00158 int t38_mode; 00159 int timed_step; 00160 int current_tx_data; 00161 int64_t next_send_samples; 00162 int next_tx_indicator; 00163 00164 int current_rx_type; 00165 int current_tx_type; 00166 00167 /*! \brief TRUE is there has been some T.38 data missed */ 00168 int missing_data; 00169 00170 int octets_per_non_ecm_packet; 00171 00172 /*! \brief Error and flow logging control */ 00173 logging_state_t logging; 00174 }; 00175 00176 #if defined(__cplusplus) 00177 extern "C" 00178 { 00179 #endif 00180 00181 void t31_call_event(t31_state_t *s, int event); 00182 00183 int t31_at_rx(t31_state_t *s, const char *t, int len); 00184 00185 /*! Process a block of received T.31 modem audio samples. 00186 \brief Process a block of received T.31 modem audio samples. 00187 \param s The T.31 modem context. 00188 \param amp The audio sample buffer. 00189 \param len The number of samples in the buffer. 00190 \return The number of samples unprocessed. */ 00191 int t31_rx(t31_state_t *s, int16_t amp[], int len); 00192 00193 /*! Generate a block of T.31 modem audio samples. 00194 \brief Generate a block of T.31 modem audio samples. 00195 \param s The T.31 modem context. 00196 \param amp The audio sample buffer. 00197 \param max_len The number of samples to be generated. 00198 \return The number of samples actually generated. 00199 */ 00200 int t31_tx(t31_state_t *s, int16_t amp[], int max_len); 00201 00202 int t31_t38_send_timeout(t31_state_t *s, int samples); 00203 00204 /*! Select whether silent audio will be sent when transmit is idle. 00205 \brief Select whether silent audio will be sent when transmit is idle. 00206 \param s The T.31 modem context. 00207 \param transmit_on_idle TRUE if silent audio should be output when the transmitter is 00208 idle. FALSE to transmit zero length audio when the transmitter is idle. The default 00209 behaviour is FALSE. 00210 */ 00211 void t31_set_transmit_on_idle(t31_state_t *s, int transmit_on_idle); 00212 00213 /*! Initialise a T.31 context. This must be called before the first 00214 use of the context, to initialise its contents. 00215 \brief Initialise a T.31 context. 00216 \param s The T.31 context. 00217 \param at_tx_handler A callback routine to handle AT interpreter channel output. 00218 \param at_tx_user_data An opaque pointer passed in called to at_tx_handler. 00219 \param modem_control_handler A callback routine to handle control of the modem (off-hook, etc). 00220 \param modem_control_user_data An opaque pointer passed in called to modem_control_handler. 00221 \param tx_t38_packet_handler ??? 00222 \param tx_t38_packet_user_data ??? 00223 \return A pointer to the T.31 context. */ 00224 t31_state_t *t31_init(t31_state_t *s, 00225 at_tx_handler_t *at_tx_handler, 00226 void *at_tx_user_data, 00227 t31_modem_control_handler_t *modem_control_handler, 00228 void *modem_control_user_data, 00229 t38_tx_packet_handler_t *tx_t38_packet_handler, 00230 void *tx_t38_packet_user_data); 00231 00232 /*! Release a T.31 context. 00233 \brief Release a T.31 context. 00234 \param s The T.31 context. 00235 \return 0 for OK */ 00236 int t31_release(t31_state_t *s); 00237 00238 #if defined(__cplusplus) 00239 } 00240 #endif 00241 00242 #endif 00243 /*- End of file ------------------------------------------------------------*/