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.43 2007/05/15 13:22:43 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) 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 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 /*! \brief A V.17 modem context used when sending FAXes at 7200bps, 9600bps 00110 12000bps or 14400bps*/ 00111 v17_tx_state_t v17tx; 00112 /*! \brief A V.29 modem context used when receiving FAXes at 7200bps, 9600bps 00113 12000bps or 14400bps*/ 00114 v17_rx_state_t v17rx; 00115 00116 /*! \brief A V.29 modem context used when sending FAXes at 7200bps or 00117 9600bps */ 00118 v29_tx_state_t v29tx; 00119 /*! \brief A V.29 modem context used when receiving FAXes at 7200bps or 00120 9600bps */ 00121 v29_rx_state_t v29rx; 00122 00123 /*! \brief A V.27ter modem context used when sending FAXes at 2400bps or 00124 4800bps */ 00125 v27ter_tx_state_t v27ter_tx; 00126 /*! \brief A V.27ter modem context used when receiving FAXes at 2400bps or 00127 4800bps */ 00128 v27ter_rx_state_t v27ter_rx; 00129 00130 /*! \brief Used to insert timed silences. */ 00131 silence_gen_state_t silence_gen; 00132 00133 /*! \brief Rx power meter, use to detect silence */ 00134 power_meter_t rx_power; 00135 int16_t last_sample; 00136 int32_t silence_threshold_power; 00137 00138 t38_core_state_t t38; 00139 00140 /*! \brief Samples of silence heard */ 00141 int silence_heard; 00142 /*! \brief Samples of silence awaited */ 00143 int silence_awaited; 00144 /*! \brief Samples elapsed in the current call */ 00145 int64_t call_samples; 00146 int64_t dte_data_timeout; 00147 int modem; 00148 int short_train; 00149 int hdlc_final; 00150 int data_final; 00151 queue_state_t *rx_queue; 00152 00153 uint8_t hdlc_rx_buf[256]; 00154 int hdlc_rx_len; 00155 00156 int t38_mode; 00157 int timed_step; 00158 int current_tx_data; 00159 int64_t next_send_samples; 00160 int next_tx_indicator; 00161 00162 int current_rx_type; 00163 int current_tx_type; 00164 00165 /*! \brief TRUE is there has been some T.38 data missed */ 00166 int missing_data; 00167 00168 int octets_per_non_ecm_packet; 00169 00170 /*! \brief Error and flow logging control */ 00171 logging_state_t logging; 00172 }; 00173 00174 #if defined(__cplusplus) 00175 extern "C" 00176 { 00177 #endif 00178 00179 void t31_call_event(t31_state_t *s, int event); 00180 00181 int t31_at_rx(t31_state_t *s, const char *t, int len); 00182 00183 /*! Process a block of received T.31 modem audio samples. 00184 \brief Process a block of received T.31 modem audio samples. 00185 \param s The T.31 modem context. 00186 \param amp The audio sample buffer. 00187 \param len The number of samples in the buffer. 00188 \return The number of samples unprocessed. */ 00189 int t31_rx(t31_state_t *s, int16_t amp[], int len); 00190 00191 /*! Generate a block of T.31 modem audio samples. 00192 \brief Generate a block of T.31 modem audio samples. 00193 \param s The T.31 modem context. 00194 \param amp The audio sample buffer. 00195 \param max_len The number of samples to be generated. 00196 \return The number of samples actually generated. 00197 */ 00198 int t31_tx(t31_state_t *s, int16_t amp[], int max_len); 00199 00200 int t31_t38_send_timeout(t31_state_t *s, int samples); 00201 00202 /*! Select whether silent audio will be sent when transmit is idle. 00203 \brief Select whether silent audio will be sent when transmit is idle. 00204 \param s The T.31 modem context. 00205 \param transmit_on_idle TRUE if silent audio should be output when the transmitter is 00206 idle. FALSE to transmit zero length audio when the transmitter is idle. The default 00207 behaviour is FALSE. 00208 */ 00209 void t31_set_transmit_on_idle(t31_state_t *s, int transmit_on_idle); 00210 00211 /*! Initialise a T.31 context. This must be called before the first 00212 use of the context, to initialise its contents. 00213 \brief Initialise a T.31 context. 00214 \param s The T.31 context. 00215 \param at_tx_handler A callback routine to handle AT interpreter channel output. 00216 \param at_tx_user_data An opaque pointer passed in called to at_tx_handler. 00217 \param modem_control_handler A callback routine to handle control of the modem (off-hook, etc). 00218 \param modem_control_user_data An opaque pointer passed in called to modem_control_handler. 00219 \param tx_t38_packet_handler ??? 00220 \param tx_t38_packet_user_data ??? 00221 \return A pointer to the T.31 context. */ 00222 t31_state_t *t31_init(t31_state_t *s, 00223 at_tx_handler_t *at_tx_handler, 00224 void *at_tx_user_data, 00225 t31_modem_control_handler_t *modem_control_handler, 00226 void *modem_control_user_data, 00227 t38_tx_packet_handler_t *tx_t38_packet_handler, 00228 void *tx_t38_packet_user_data); 00229 00230 /*! Release a T.31 context. 00231 \brief Release a T.31 context. 00232 \param s The T.31 context. 00233 \return 0 for OK */ 00234 int t31_release(t31_state_t *s); 00235 00236 #if defined(__cplusplus) 00237 } 00238 #endif 00239 00240 #endif 00241 /*- End of file ------------------------------------------------------------*/