t30.h

Go to the documentation of this file.
00001 /*
00002  * SpanDSP - a series of DSP components for telephony
00003  *
00004  * t30.h - definitions for T.30 fax processing
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: t30.h,v 1.115 2008/09/04 14:40:05 steveu Exp $
00026  */
00027 
00028 /*! \file */
00029 
00030 #if !defined(_SPANDSP_T30_H_)
00031 #define _SPANDSP_T30_H_
00032 
00033 /*! \page t30_page T.30 FAX protocol handling
00034 
00035 \section t30_page_sec_1 What does it do?
00036 The T.30 protocol is the core protocol used for FAX transmission. This module
00037 implements most of its key featrues. It does not interface to the outside work.
00038 Seperate modules do that for T.38, analogue line, and other forms of FAX
00039 communication.
00040 
00041 Current features of this module include:
00042 
00043     - FAXing to and from multi-page TIFF/F files, whose images are one of the standard
00044       FAX sizes.
00045     - V.27ter, V.29 and V.17 modes (2400bps, to 14,400bps).
00046     - T.4 1D (MH), T.4 2D,(MR) and T.6 (MMR) compression.
00047     - Error correction mode (ECM).
00048     - All standard horizonal resolutions (R8, R16, 300dpi, 600dpi, 800dpi, 1200dpi).
00049     - All standard vertical resolutions (standard, fine, superfine, 300dpi, 600dpi, 800dpi, 1200dpi).
00050     - All standard page widths (A4, B4, A3).
00051     - All standard page lengths (A4, B4, North American letter, North American legal, continuous).
00052     - Monitoring and sending identifier strings (CSI, TSI, and CIG).
00053     - Monitoring and sending sub-address strings (SUB).
00054     - Monitoring and sending polling sub-addresses (SEP).
00055     - Monitoring and sending polled sub-addresses (PSA).
00056     - Monitoring and sending sender identifications (SID).
00057     - Monitoring and sending passwords (PWD).
00058     - Monitoring of non-standard facility frames (NSF, NSC, and NSS).
00059     - Sending custom non-standard facility frames (NSF, NSC, and NSS).
00060     - Analogue modem and T.38 operation.
00061 
00062 \section t30_page_sec_2 How does it work?
00063 
00064 Some of the following is paraphrased from some notes found a while ago on the Internet.
00065 I cannot remember exactly where they came from, but they are useful.
00066 
00067 \subsection t30_page_sec_2a The answer (CED) tone
00068 
00069 The T.30 standard says an answering fax device must send CED (a 2100Hz tone) for
00070 approximately 3 seconds before sending the first handshake message. Some machines
00071 send an 1100Hz or 1850Hz tone, and some send no tone at all. In fact, this answer
00072 tone is so unpredictable, it cannot really be used. It should, however, always be
00073 generated according to the specification.
00074 
00075 \subsection t30_page_sec_2b Common Timing Deviations
00076 
00077 The T.30 spec. specifies a number of time-outs. For example, after dialing a number,
00078 a calling fax system should listen for a response for 35 seconds before giving up.
00079 These time-out periods are as follows: 
00080 
00081     - T1 - 35+-5s: the maximum time for which two fax system will attempt to identify each other
00082     - T2 - 6+-1s:  a time-out used to start the sequence for changing transmit parameters
00083     - T3 - 10+-5s: a time-out used in handling operator interrupts
00084     - T5 - 60+-5s: a time-out used in error correction mode
00085 
00086 These time-outs are sometimes misinterpreted. In addition, they are routinely
00087 ignored, sometimes with good reason. For example, after placing a call, the
00088 calling fax system is supposed to wait for 35 seconds before giving up. If the
00089 answering unit does not answer on the first ring or if a voice answering machine
00090 is connected to the line, or if there are many delays through the network,
00091 the delay before answer can be much longer than 35 seconds. 
00092 
00093 Fax units that support error correction mode (ECM) can respond to a post-image
00094 handshake message with a receiver not ready (RNR) message. The calling unit then
00095 queries the receiving fax unit with a receiver ready (RR) message. If the
00096 answering unit is still busy (printing for example), it will repeat the RNR
00097 message. According to the T.30 standard, this sequence (RR/RNR RR/RNR) can be
00098 repeated for up to the end of T5 (60+-5s). However, many fax systems
00099 ignore the time-out and will continue the sequence indefinitely, unless the user
00100 manually overrides. 
00101 
00102 All the time-outs are subject to alteration, and sometimes misuse. Good T.30
00103 implementations must do the right thing, and tolerate others doing the wrong thing.
00104 
00105 \subsection t30_page_sec_2c Variations in the inter-carrier gap
00106 
00107 T.30 specifies 75+-20ms of silence between signals using different modulation
00108 schemes. Examples are between the end of a DCS signal and the start of a TCF signal,
00109 and between the end of an image and the start of a post-image signal. Many fax systems
00110 violate this requirement, especially for the silent period between DCS and TCF.
00111 This may be stretched to well over 100ms. If this period is too long, it can interfere with
00112 handshake signal error recovery, should a packet be corrupted on the line. Systems
00113 should ensure they stay within the prescribed T.30 limits, and be tolerant of others
00114 being out of spec.. 
00115 
00116 \subsection t30_page_sec_2d Other timing variations
00117 
00118 Testing is required to determine the ability of a fax system to handle
00119 variations in the duration of pauses between unacknowledged handshake message
00120 repetitions, and also in the pauses between the receipt of a handshake command and
00121 the start of a response to that command. In order to reduce the total
00122 transmission time, many fax systems start sending a response message before the
00123 end of the command has been received. 
00124 
00125 \subsection t30_page_sec_2e Other deviations from the T.30 standard
00126 
00127 There are many other commonly encountered variations between machines, including:
00128 
00129     - frame sequence deviations
00130     - preamble and flag sequence variations
00131     - improper EOM usage
00132     - unusual data rate fallback sequences
00133     - common training pattern detection algorithms
00134     - image transmission deviations
00135     - use of the talker echo protect tone
00136     - image padding and short lines
00137     - RTP/RTN handshake message usage
00138     - long duration lines
00139     - nonstandard disconnect sequences
00140     - DCN usage
00141 */
00142 
00143 #define T30_MAX_DIS_DTC_DCS_LEN     22
00144 #define T30_MAX_IDENT_LEN           20
00145 #define T30_MAX_PAGE_HEADER_INFO    50
00146 
00147 typedef struct t30_state_s t30_state_t;
00148 
00149 /*!
00150     T.30 phase B callback handler. This handler can be used to process addition
00151     information available in some FAX calls, such as passwords. The callback handler
00152     can access whatever additional information might have been received, using
00153     t30_get_received_info().
00154     \brief T.30 phase B callback handler.
00155     \param s The T.30 context.
00156     \param user_data An opaque pointer.
00157     \param result The phase B event code.
00158     \return The new status. Normally, T30_ERR_OK is returned.
00159 */
00160 typedef int (t30_phase_b_handler_t)(t30_state_t *s, void *user_data, int result);
00161 
00162 /*!
00163     T.30 phase D callback handler.
00164     \brief T.30 phase D callback handler.
00165     \param s The T.30 context.
00166     \param user_data An opaque pointer.
00167     \param result The phase D event code.
00168     \return The new status. Normally, T30_ERR_OK is returned.
00169 */
00170 typedef int (t30_phase_d_handler_t)(t30_state_t *s, void *user_data, int result);
00171 
00172 /*!
00173     T.30 phase E callback handler.
00174     \brief T.30 phase E callback handler.
00175     \param s The T.30 context.
00176     \param user_data An opaque pointer.
00177     \param completion_code The phase E completion code.
00178 */
00179 typedef void (t30_phase_e_handler_t)(t30_state_t *s, void *user_data, int completion_code);
00180 
00181 /*!
00182     T.30 real time frame handler.
00183     \brief T.30 real time frame handler.
00184     \param s The T.30 context.
00185     \param user_data An opaque pointer.
00186     \param direction TRUE for incoming, FALSE for outgoing.
00187     \param msg The HDLC message.
00188     \param len The length of the message.
00189 */
00190 typedef void (t30_real_time_frame_handler_t)(t30_state_t *s,
00191                                              void *user_data,
00192                                              int direction,
00193                                              const uint8_t *msg,
00194                                              int len);
00195 
00196 /*!
00197     T.30 document handler.
00198     \brief T.30 document handler.
00199     \param s The T.30 context.
00200     \param user_data An opaque pointer.
00201     \param result The document event code.
00202 */
00203 typedef int (t30_document_handler_t)(t30_state_t *s, void *user_data, int status);
00204 
00205 /*!
00206     T.30 set a receive or transmit type handler.
00207     \brief T.30 set a receive or transmit type handler.
00208     \param user_data An opaque pointer.
00209     \param type The modem, tone or silence to be sent or received.
00210     \param bit_rate The bit rate of the modem to be sent or received.
00211     \param short_train TRUE if the short training sequence should be used (where one exists).
00212     \param use_hdlc FALSE for bit stream, TRUE for HDLC framing.
00213 */
00214 typedef void (t30_set_handler_t)(void *user_data, int type, int bit_rate, int short_train, int use_hdlc);
00215 
00216 /*!
00217     T.30 send HDLC handler.
00218     \brief T.30 send HDLC handler.
00219     \param user_data An opaque pointer.
00220     \param msg The HDLC message.
00221     \param len The length of the message.
00222 */
00223 typedef void (t30_send_hdlc_handler_t)(void *user_data, const uint8_t *msg, int len);
00224 
00225 /*!
00226     T.30 protocol completion codes, at phase E.
00227 */
00228 enum
00229 {
00230     T30_ERR_OK = 0,             /*! OK */
00231 
00232     /* Link problems */
00233     T30_ERR_CEDTONE,            /*! The CED tone exceeded 5s */
00234     T30_ERR_T0_EXPIRED,         /*! Timed out waiting for initial communication */
00235     T30_ERR_T1_EXPIRED,         /*! Timed out waiting for the first message */
00236     T30_ERR_T3_EXPIRED,         /*! Timed out waiting for procedural interrupt */
00237     T30_ERR_HDLC_CARRIER,       /*! The HDLC carrier did not stop in a timely manner */
00238     T30_ERR_CANNOT_TRAIN,       /*! Failed to train with any of the compatible modems */
00239     T30_ERR_OPER_INT_FAIL,      /*! Operator intervention failed */
00240     T30_ERR_INCOMPATIBLE,       /*! Far end is not compatible */
00241     T30_ERR_RX_INCAPABLE,       /*! Far end is not able to receive */
00242     T30_ERR_TX_INCAPABLE,       /*! Far end is not able to transmit */
00243     T30_ERR_NORESSUPPORT,       /*! Far end cannot receive at the resolution of the image */
00244     T30_ERR_NOSIZESUPPORT,      /*! Far end cannot receive at the size of image */
00245     T30_ERR_UNEXPECTED,         /*! Unexpected message received */
00246 
00247     /* Phase E status values returned to a transmitter */
00248     T30_ERR_TX_BADDCS,          /*! Received bad response to DCS or training */
00249     T30_ERR_TX_BADPG,           /*! Received a DCN from remote after sending a page */
00250     T30_ERR_TX_ECMPHD,          /*! Invalid ECM response received from receiver */
00251     T30_ERR_TX_GOTDCN,          /*! Received a DCN while waiting for a DIS */
00252     T30_ERR_TX_INVALRSP,        /*! Invalid response after sending a page */
00253     T30_ERR_TX_NODIS,           /*! Received other than DIS while waiting for DIS */
00254     T30_ERR_TX_PHBDEAD,         /*! Received no response to DCS, training or TCF */
00255     T30_ERR_TX_PHDDEAD,         /*! No response after sending a page */
00256     T30_ERR_TX_T5EXP,           /*! Timed out waiting for receiver ready (ECM mode) */
00257 
00258     /* Phase E status values returned to a receiver */
00259     T30_ERR_RX_ECMPHD,          /*! Invalid ECM response received from transmitter */
00260     T30_ERR_RX_GOTDCS,          /*! DCS received while waiting for DTC */
00261     T30_ERR_RX_INVALCMD,        /*! Unexpected command after page received */
00262     T30_ERR_RX_NOCARRIER,       /*! Carrier lost during fax receive */
00263     T30_ERR_RX_NOEOL,           /*! Timed out while waiting for EOL (end Of line) */
00264     T30_ERR_RX_NOFAX,           /*! Timed out while waiting for first line */
00265     T30_ERR_RX_T2EXPDCN,        /*! Timer T2 expired while waiting for DCN */
00266     T30_ERR_RX_T2EXPD,          /*! Timer T2 expired while waiting for phase D */
00267     T30_ERR_RX_T2EXPFAX,        /*! Timer T2 expired while waiting for fax page */
00268     T30_ERR_RX_T2EXPMPS,        /*! Timer T2 expired while waiting for next fax page */
00269     T30_ERR_RX_T2EXPRR,         /*! Timer T2 expired while waiting for RR command */
00270     T30_ERR_RX_T2EXP,           /*! Timer T2 expired while waiting for NSS, DCS or MCF */
00271     T30_ERR_RX_DCNWHY,          /*! Unexpected DCN while waiting for DCS or DIS */
00272     T30_ERR_RX_DCNDATA,         /*! Unexpected DCN while waiting for image data */
00273     T30_ERR_RX_DCNFAX,          /*! Unexpected DCN while waiting for EOM, EOP or MPS */
00274     T30_ERR_RX_DCNPHD,          /*! Unexpected DCN after EOM or MPS sequence */
00275     T30_ERR_RX_DCNRRD,          /*! Unexpected DCN after RR/RNR sequence */
00276     T30_ERR_RX_DCNNORTN,        /*! Unexpected DCN after requested retransmission */
00277 
00278     /* TIFF file problems */
00279     T30_ERR_FILEERROR,          /*! TIFF/F file cannot be opened */
00280     T30_ERR_NOPAGE,             /*! TIFF/F page not found */
00281     T30_ERR_BADTIFF,            /*! TIFF/F format is not compatible */
00282     T30_ERR_BADPAGE,            /*! TIFF/F page number tag missing */
00283     T30_ERR_BADTAG,             /*! Incorrect values for TIFF/F tags */
00284     T30_ERR_BADTIFFHDR,         /*! Bad TIFF/F header - incorrect values in fields */
00285     T30_ERR_NOMEM,              /*! Cannot allocate memory for more pages */
00286     
00287     /* General problems */
00288     T30_ERR_RETRYDCN,           /*! Disconnected after permitted retries */
00289     T30_ERR_CALLDROPPED,        /*! The call dropped prematurely */
00290     
00291     /* Feature negotiation issues */
00292     T30_ERR_NOPOLL,             /*! Poll not accepted */
00293     T30_ERR_IDENT_UNACCEPTABLE, /*! Far end's ident is not acceptable */
00294     T30_ERR_SUB_UNACCEPTABLE,   /*! Far end's sub-address is not acceptable */
00295     T30_ERR_SEP_UNACCEPTABLE,   /*! Far end's selective polling address is not acceptable */
00296     T30_ERR_PSA_UNACCEPTABLE,   /*! Far end's polled sub-address is not acceptable */
00297     T30_ERR_SID_UNACCEPTABLE,   /*! Far end's sender identification is not acceptable */
00298     T30_ERR_PWD_UNACCEPTABLE,   /*! Far end's password is not acceptable */
00299     T30_ERR_TSA_UNACCEPTABLE,   /*! Far end's transmitting subscriber internet address is not acceptable */
00300     T30_ERR_IRA_UNACCEPTABLE,   /*! Far end's internet routing address is not acceptable */
00301     T30_ERR_CIA_UNACCEPTABLE,   /*! Far end's calling subscriber internet address is not acceptable */
00302     T30_ERR_ISP_UNACCEPTABLE,   /*! Far end's internet selective polling address is not acceptable */
00303     T30_ERR_CSA_UNACCEPTABLE    /*! Far end's called subscriber internet address is not acceptable */
00304 };
00305 
00306 /*!
00307     I/O modes for the T.30 protocol.
00308     These are allocated such that the lower 4 bits represents the variant of the modem - e.g. the
00309     particular bit rate selected.
00310 */
00311 enum
00312 {
00313     T30_MODEM_NONE = 0,
00314     T30_MODEM_PAUSE,
00315     T30_MODEM_CED,
00316     T30_MODEM_CNG,
00317     T30_MODEM_V21,
00318     T30_MODEM_V27TER,
00319     T30_MODEM_V29,
00320     T30_MODEM_V17,
00321     T30_MODEM_DONE
00322 };
00323 
00324 enum
00325 {
00326     T30_FRONT_END_SEND_STEP_COMPLETE = 0,
00327     /*! The current receive has completed. This is only needed to report an
00328         unexpected end of the receive operation, as might happen with T.38
00329         dying. */
00330     T30_FRONT_END_RECEIVE_COMPLETE,
00331     T30_FRONT_END_SIGNAL_PRESENT,
00332     T30_FRONT_END_SIGNAL_ABSENT
00333 };
00334 
00335 enum
00336 {
00337     T30_SUPPORT_V27TER = 0x01,
00338     T30_SUPPORT_V29 = 0x02,
00339     T30_SUPPORT_V17 = 0x04,
00340     T30_SUPPORT_V34 = 0x08,
00341     T30_SUPPORT_IAF = 0x10
00342 };
00343 
00344 enum
00345 {
00346     T30_SUPPORT_NO_COMPRESSION = 0x01,
00347     T30_SUPPORT_T4_1D_COMPRESSION = 0x02,
00348     T30_SUPPORT_T4_2D_COMPRESSION = 0x04,
00349     T30_SUPPORT_T6_COMPRESSION = 0x08,
00350     T30_SUPPORT_T85_COMPRESSION = 0x10,     /* Monochrome JBIG */
00351     T30_SUPPORT_T43_COMPRESSION = 0x20,     /* Colour JBIG */
00352     T30_SUPPORT_T45_COMPRESSION = 0x40      /* Run length colour compression */
00353 };
00354 
00355 enum
00356 {
00357     T30_SUPPORT_STANDARD_RESOLUTION = 0x01,
00358     T30_SUPPORT_FINE_RESOLUTION = 0x02,
00359     T30_SUPPORT_SUPERFINE_RESOLUTION = 0x04,
00360 
00361     T30_SUPPORT_R4_RESOLUTION = 0x10000,
00362     T30_SUPPORT_R8_RESOLUTION = 0x20000,
00363     T30_SUPPORT_R16_RESOLUTION = 0x40000,
00364 
00365     T30_SUPPORT_300_300_RESOLUTION = 0x100000,
00366     T30_SUPPORT_400_400_RESOLUTION = 0x200000,
00367     T30_SUPPORT_600_600_RESOLUTION = 0x400000,
00368     T30_SUPPORT_1200_1200_RESOLUTION = 0x800000,
00369     T30_SUPPORT_300_600_RESOLUTION = 0x1000000,
00370     T30_SUPPORT_400_800_RESOLUTION = 0x2000000,
00371     T30_SUPPORT_600_1200_RESOLUTION = 0x4000000
00372 };
00373 
00374 enum
00375 {
00376     T30_SUPPORT_215MM_WIDTH = 0x01,
00377     T30_SUPPORT_255MM_WIDTH = 0x02,
00378     T30_SUPPORT_303MM_WIDTH = 0x04,
00379 
00380     T30_SUPPORT_UNLIMITED_LENGTH = 0x10000,
00381     T30_SUPPORT_A4_LENGTH = 0x20000,
00382     T30_SUPPORT_B4_LENGTH = 0x40000,
00383     T30_SUPPORT_US_LETTER_LENGTH = 0x80000,
00384     T30_SUPPORT_US_LEGAL_LENGTH = 0x100000
00385 };
00386 
00387 enum
00388 {
00389     /*! Enable support of identification, through the SID and/or PWD frames */
00390     T30_SUPPORT_IDENTIFICATION = 0x01,
00391     /*! Enable support of selective polling, through the SEP frame */
00392     T30_SUPPORT_SELECTIVE_POLLING = 0x02,
00393     /*! Enable support of polling sub-addressing, through the PSA frame */
00394     T30_SUPPORT_POLLED_SUB_ADDRESSING = 0x04,
00395     /*! Enable support of multiple selective polling, through repeated used of the SEP and PSA frames */
00396     T30_SUPPORT_MULTIPLE_SELECTIVE_POLLING = 0x08,
00397     /*! Enable support of sub-addressing, through the SUB frame */
00398     T30_SUPPORT_SUB_ADDRESSING = 0x10,
00399     /*! Enable support of transmitting subscriber internet address, through the TSA frame */
00400     T30_SUPPORT_TRANSMITTING_SUBSCRIBER_INTERNET_ADDRESS = 0x20,
00401     /*! Enable support of internet routing address, through the IRA frame */
00402     T30_SUPPORT_INTERNET_ROUTING_ADDRESS = 0x40,
00403     /*! Enable support of calling subscriber internet address, through the CIA frame */
00404     T30_SUPPORT_CALLING_SUBSCRIBER_INTERNET_ADDRESS = 0x80,
00405     /*! Enable support of internet selective polling address, through the ISP frame */
00406     T30_SUPPORT_INTERNET_SELECTIVE_POLLING_ADDRESS = 0x100,
00407     /*! Enable support of called subscriber internet address, through the CSA frame */
00408     T30_SUPPORT_CALLED_SUBSCRIBER_INTERNET_ADDRESS = 0x200,
00409     /*! Enable support of the field not valid (FNV) frame */
00410     T30_SUPPORT_FIELD_NOT_VALID = 0x400,
00411     /*! Enable support of the command repeat (CRP) frame */
00412     T30_SUPPORT_COMMAND_REPEAT = 0x800
00413 };
00414 
00415 enum
00416 {
00417     T30_IAF_MODE_T37 = 0x01,
00418     T30_IAF_MODE_T38 = 0x02,
00419     T30_IAF_MODE_FLOW_CONTROL = 0x04,
00420     /*! Continuous flow mode means data is sent as fast as possible, usually across
00421         the Internet, where speed is not constrained by a PSTN modem. */
00422     T30_IAF_MODE_CONTINUOUS_FLOW = 0x08,
00423     /*! No TCF means TCF is not exchanged. The end points must sort out usable speed
00424         issues locally. */
00425     T30_IAF_MODE_NO_TCF = 0x10,
00426     /*! No fill bits means do not insert fill bits, even if the T.30 messages request
00427         them. */
00428     T30_IAF_MODE_NO_FILL_BITS = 0x20,
00429     /*! No indicators means do not send indicator messages when using T.38. */
00430     T30_IAF_MODE_NO_INDICATORS = 0x40
00431 };
00432 
00433 typedef struct
00434 {
00435     /*! \brief The identifier string (CSI, TSI, CIG). */
00436     char ident[T30_MAX_IDENT_LEN + 1];
00437     /*! \brief The sub-address string (SUB). */
00438     char sub_address[T30_MAX_IDENT_LEN + 1];
00439     /*! \brief The selective polling sub-address (SEP). */
00440     char selective_polling_address[T30_MAX_IDENT_LEN + 1];
00441     /*! \brief The polled sub-address (PSA). */
00442     char polled_sub_address[T30_MAX_IDENT_LEN + 1];
00443     /*! \brief The sender identification (SID). */
00444     char sender_ident[T30_MAX_IDENT_LEN + 1];
00445     /*! \brief The password (PWD). */
00446     char password[T30_MAX_IDENT_LEN + 1];
00447     /*! \brief Non-standard facilities (NSF). */
00448     uint8_t *nsf;
00449     size_t nsf_len;
00450     /*! \brief Non-standard facilities command (NSC). */
00451     uint8_t *nsc;
00452     size_t nsc_len;
00453     /*! \brief Non-standard facilities set-up (NSS). */
00454     uint8_t *nss;
00455     size_t nss_len;
00456     /*! \brief Transmitting subscriber internet address (TSA). */
00457     int tsa_type;
00458     char *tsa;
00459     size_t tsa_len;
00460     /*! \brief Internet routing address (IRA). */
00461     int ira_type;
00462     char *ira;
00463     size_t ira_len;
00464     /*! \brief Calling subscriber internet address (CIA). */
00465     int cia_type;
00466     char *cia;
00467     size_t cia_len;
00468     /*! \brief Internet selective polling address (ISP). */
00469     int isp_type;
00470     char *isp;
00471     size_t isp_len;
00472     /*! \brief Called subscriber internet address (CSA). */
00473     int csa_type;
00474     char *csa;
00475     size_t csa_len;
00476 } t30_exchanged_info_t;
00477 
00478 /*!
00479     T.30 FAX channel descriptor. This defines the state of a single working
00480     instance of a T.30 FAX channel.
00481 */
00482 struct t30_state_s
00483 {
00484     /* This must be kept the first thing in the structure, so it can be pointed
00485        to reliably as the structures change over time. */
00486     /*! \brief T.4 context for reading or writing image data. */
00487     t4_state_t t4;
00488     
00489     int operation_in_progress;
00490 
00491     /*! \brief TRUE if behaving as the calling party */
00492     int calling_party;
00493     
00494     /*! \brief The received DCS, formatted as an ASCII string, for inclusion
00495                in the TIFF file. */
00496     char rx_dcs_string[T30_MAX_DIS_DTC_DCS_LEN*3 + 1];
00497     /*! \brief The text which will be used in FAX page header. No text results
00498                in no header line. */
00499     char header_info[T30_MAX_PAGE_HEADER_INFO + 1];
00500     /*! \brief The information fields received. */
00501     t30_exchanged_info_t rx_info;
00502     /*! \brief The information fields to be transmitted. */
00503     t30_exchanged_info_t tx_info;
00504     /*! \brief The country of origin of the remote machine, if known, else NULL. */
00505     const char *country;
00506     /*! \brief The vendor of the remote machine, if known, else NULL. */
00507     const char *vendor;
00508     /*! \brief The model of the remote machine, if known, else NULL. */
00509     const char *model;
00510 
00511     /*! \brief A pointer to a callback routine to be called when phase B events
00512         occur. */
00513     t30_phase_b_handler_t *phase_b_handler;
00514     /*! \brief An opaque pointer supplied in event B callbacks. */
00515     void *phase_b_user_data;
00516     /*! \brief A pointer to a callback routine to be called when phase D events
00517         occur. */
00518     t30_phase_d_handler_t *phase_d_handler;
00519     /*! \brief An opaque pointer supplied in event D callbacks. */
00520     void *phase_d_user_data;
00521     /*! \brief A pointer to a callback routine to be called when phase E events
00522         occur. */
00523     t30_phase_e_handler_t *phase_e_handler;
00524     /*! \brief An opaque pointer supplied in event E callbacks. */
00525     void *phase_e_user_data;
00526     /*! \brief A pointer to a callback routine to be called when frames are
00527         exchanged. */
00528     t30_real_time_frame_handler_t *real_time_frame_handler;
00529     /*! \brief An opaque pointer supplied in real time frame callbacks. */
00530     void *real_time_frame_user_data;
00531 
00532     /*! \brief A pointer to a callback routine to be called when document events
00533         (e.g. end of transmitted document) occur. */
00534     t30_document_handler_t *document_handler;
00535     /*! \brief An opaque pointer supplied in document callbacks. */
00536     void *document_user_data;
00537 
00538     /*! \brief The handler for changes to the receive mode */
00539     t30_set_handler_t *set_rx_type_handler;
00540     /*! \brief An opaque pointer passed to the handler for changes to the receive mode */
00541     void *set_rx_type_user_data;
00542     /*! \brief The handler for changes to the transmit mode */
00543     t30_set_handler_t *set_tx_type_handler;
00544     /*! \brief An opaque pointer passed to the handler for changes to the transmit mode */
00545     void *set_tx_type_user_data;
00546 
00547     /*! \brief The transmitted HDLC frame handler. */
00548     t30_send_hdlc_handler_t *send_hdlc_handler;
00549     /*! \brief An opaque pointer passed to the transmitted HDLC frame handler. */
00550     void *send_hdlc_user_data;
00551 
00552     /*! \brief The DIS code for the minimum scan row time we require. This is usually 0ms,
00553         but if we are trying to simulate another type of FAX machine, we may need a non-zero
00554         value here. */
00555     uint8_t local_min_scan_time_code;
00556 
00557     /*! \brief The current T.30 phase. */
00558     int phase;
00559     /*! \brief The T.30 phase to change to when the current phase ends. */
00560     int next_phase;
00561     /*! \brief The current state of the T.30 state machine. */
00562     int state;
00563     /*! \brief The step in sending a sequence of HDLC frames. */
00564     int step;
00565 
00566     /*! \brief The preparation buffer for the DCS message to be transmitted. */
00567     uint8_t dcs_frame[T30_MAX_DIS_DTC_DCS_LEN];
00568     /*! \brief The length of the DCS message to be transmitted. */
00569     int dcs_len;
00570     /*! \brief The preparation buffer for DIS or DTC message to be transmitted. */
00571     uint8_t local_dis_dtc_frame[T30_MAX_DIS_DTC_DCS_LEN];
00572     /*! \brief The length of the DIS or DTC message to be transmitted. */
00573     int local_dis_dtc_len;
00574     /*! \brief The last DIS or DTC message received form the far end. */
00575     uint8_t far_dis_dtc_frame[T30_MAX_DIS_DTC_DCS_LEN];
00576     /*! \brief The length of the last DIS or DTC message received form the far end. */
00577     int far_dis_dtc_len;
00578     /*! \brief TRUE if a valid DIS has been received from the far end. */
00579     int dis_received;
00580 
00581     /*! \brief A flag to indicate a message is in progress. */
00582     int in_message;
00583 
00584     /*! \brief TRUE if the short training sequence should be used. */
00585     int short_train;
00586 
00587     /*! \brief A count of the number of bits in the trainability test. This counts down to zero when
00588         sending TCF, and counts up when receiving it. */
00589     int tcf_test_bits;
00590     /*! \brief The current count of consecutive received zero bits, during the trainability test. */
00591     int tcf_current_zeros;
00592     /*! \brief The maximum consecutive received zero bits seen to date, during the trainability test. */
00593     int tcf_most_zeros;
00594 
00595     /*! \brief The current fallback step for the fast message transfer modem. */
00596     int current_fallback;
00597     /*! \brief The subset of supported modems allowed at the current time, allowing for negotiation. */
00598     int current_permitted_modems;
00599     /*! \brief TRUE if a carrier is present. Otherwise FALSE. */
00600     int rx_signal_present;
00601     /*! \brief TRUE if a modem has trained correctly. */
00602     int rx_trained;
00603     /*! \brief TRUE if a valid HDLC frame has been received in the current reception period. */
00604     int rx_frame_received;
00605 
00606     /*! \brief Current reception mode. */
00607     int current_rx_type;
00608     /*! \brief Current transmission mode. */
00609     int current_tx_type;
00610 
00611     /*! \brief T0 is the answer timeout when calling another FAX machine.
00612         Placing calls is handled outside the FAX processing, but this timeout keeps
00613         running until V.21 modulation is sent or received.
00614         T1 is the remote terminal identification timeout (in audio samples). */
00615     int timer_t0_t1;
00616     /*! \brief T2, T2A and T2B are the HDLC command timeouts.
00617                T4, T4A and T4B are the HDLC response timeouts (in audio samples). */
00618     int timer_t2_t4;
00619     /*! \brief A value specifying which of the possible timers is currently running in timer_t2_t4 */
00620     int timer_t2_t4_is;
00621     /*! \brief Procedural interrupt timeout (in audio samples). */
00622     int timer_t3;
00623     /*! \brief This is only used in error correcting mode. */
00624     int timer_t5;
00625     /*! \brief This is only used in full duplex (e.g. ISDN) modes. */
00626     int timer_t6;
00627     /*! \brief This is only used in full duplex (e.g. ISDN) modes. */
00628     int timer_t7;
00629     /*! \brief This is only used in full duplex (e.g. ISDN) modes. */
00630     int timer_t8;
00631 
00632     /*! \brief TRUE once the far end FAX entity has been detected. */
00633     int far_end_detected;
00634 
00635     /*! \brief TRUE if a local T.30 interrupt is pending. */
00636     int local_interrupt_pending;
00637     /*! \brief The image coding being used on the line. */
00638     int line_encoding;
00639     /*! \brief The image coding being used for output files. */
00640     int output_encoding;
00641     /*! \brief The current DCS message minimum scan time code. */
00642     uint8_t min_scan_time_code;
00643     /*! \brief The X direction resolution of the current image, in pixels per metre. */
00644     int x_resolution;
00645     /*! \brief The Y direction resolution of the current image, in pixels per metre. */
00646     int y_resolution;
00647     /*! \brief The width of the current image, in pixels. */
00648     t4_image_width_t image_width;
00649     /*! \brief Current number of retries of the action in progress. */
00650     int retries;
00651     /*! \brief TRUE if error correcting mode is used. */
00652     int error_correcting_mode;
00653     /*! \brief The current count of consecutive T30_PPR messages. */
00654     int ppr_count;
00655     /*! \brief The current count of consecutive T30_RNR messages. */
00656     int receiver_not_ready_count;
00657     /*! \brief The number of octets to be used per ECM frame. */
00658     int octets_per_ecm_frame;
00659     /*! \brief The ECM partial page buffer. */
00660     uint8_t ecm_data[256][260];
00661     /*! \brief The lengths of the frames in the ECM partial page buffer. */
00662     int16_t ecm_len[256];
00663     /*! \brief A bit map of the OK ECM frames, constructed as a PPR frame. */
00664     uint8_t ecm_frame_map[3 + 32];
00665     
00666     /*! \brief The current page number for receiving, in ECM mode. This is reset at the start of a call. */
00667     int ecm_rx_page;
00668     /*! \brief The current page number for sending, in ECM mode. This is reset at the start of a call. */
00669     int ecm_tx_page;
00670     /*! \brief The current block number, in ECM mode */
00671     int ecm_block;
00672     /*! \brief The number of frames in the current block number, in ECM mode */
00673     int ecm_frames;
00674     /*! \brief The number of frames sent in the current burst of image transmission, in ECM mode */
00675     int ecm_frames_this_tx_burst;
00676     /*! \brief The current ECM frame, during ECM transmission. */
00677     int ecm_current_tx_frame;
00678     /*! \brief TRUE if we are at the end of an ECM page to se sent - i.e. there are no more
00679         partial pages still to come. */
00680     int ecm_at_page_end;
00681     int next_tx_step;
00682     int next_rx_step;
00683     /*! \brief Image file name for image reception. */
00684     char rx_file[256];
00685     /*! \brief The last page we are prepared accept for a received image file. -1 means no restriction. */
00686     int rx_stop_page;
00687     /*! \brief Image file name to be sent. */
00688     char tx_file[256];
00689     /*! \brief The first page to be sent from the image file. -1 means no restriction. */
00690     int tx_start_page;
00691     /*! \brief The last page to be sent from the image file. -1 means no restriction. */
00692     int tx_stop_page;
00693     int current_status;
00694     /*! \brief Internet Aware FAX mode bit mask. */
00695     int iaf;
00696     /*! \brief A bit mask of the currently supported modem types. */
00697     int supported_modems;
00698     /*! \brief A bit mask of the currently supported image compression modes. */
00699     int supported_compressions;
00700     /*! \brief A bit mask of the currently supported image resolutions. */
00701     int supported_resolutions;
00702     /*! \brief A bit mask of the currently supported image sizes. */
00703     int supported_image_sizes;
00704     /*! \brief A bit mask of the currently supported T.30 special features. */
00705     int supported_t30_features;
00706     /*! \brief TRUE is ECM mode handling is enabled. */
00707     int ecm_allowed;
00708     
00709     /*! \brief the FCF2 field of the last PPS message we received. */
00710     int last_pps_fcf2;
00711     /*! \brief The number of the first ECM frame which we do not currently received correctly. For
00712         a partial page received correctly, this will be one greater than the number of frames it
00713         contains. */
00714     int ecm_first_bad_frame;
00715     /*! \brief A count of successfully received ECM frames, to assess progress as a basis for
00716         deciding whether to continue error correction when PPRs keep repeating. */
00717     int ecm_progress;
00718 
00719     /*! \brief Error and flow logging control */
00720     logging_state_t logging;
00721 };
00722 
00723 typedef struct
00724 {
00725     /*! \brief The current bit rate for image transfer. */
00726     int bit_rate;
00727     /*! \brief TRUE if error correcting mode is used. */
00728     int error_correcting_mode;
00729     /*! \brief The number of pages transferred so far. */
00730     int pages_transferred;
00731     /*! \brief The number of pages in the file (<0 if not known). */
00732     int pages_in_file;
00733     /*! \brief The number of horizontal pixels in the most recent page. */
00734     int width;
00735     /*! \brief The number of vertical pixels in the most recent page. */
00736     int length;
00737     /*! \brief The number of bad pixel rows in the most recent page. */
00738     int bad_rows;
00739     /*! \brief The largest number of bad pixel rows in a block in the most recent page. */
00740     int longest_bad_row_run;
00741     /*! \brief The horizontal column-to-column resolution of the page in pixels per metre */
00742     int x_resolution;
00743     /*! \brief The vertical row-to-row resolution of the page in pixels per metre */
00744     int y_resolution;
00745     /*! \brief The type of compression used between the FAX machines */
00746     int encoding;
00747     /*! \brief The size of the image, in bytes */
00748     int image_size;
00749     /*! \brief Current status */
00750     int current_status;
00751 } t30_stats_t;
00752 
00753 #if defined(__cplusplus)
00754 extern "C"
00755 {
00756 #endif
00757 
00758 /*! Initialise a T.30 context.
00759     \brief Initialise a T.30 context.
00760     \param s The T.30 context.
00761     \param calling_party TRUE if the context is for a calling party. FALSE if the
00762            context is for an answering party.
00763     \param set_rx_type_handler
00764     \param set_rx_type_user_data
00765     \param set_tx_type_handler
00766     \param set_tx_type_user_data
00767     \param send_hdlc_handler
00768     \param send_hdlc_user_data
00769     \return A pointer to the context, or NULL if there was a problem. */
00770 t30_state_t *t30_init(t30_state_t *s,
00771                       int calling_party,
00772                       t30_set_handler_t *set_rx_type_handler,
00773                       void *set_rx_type_user_data,
00774                       t30_set_handler_t *set_tx_type_handler,
00775                       void *set_tx_type_user_data,
00776                       t30_send_hdlc_handler_t *send_hdlc_handler,
00777                       void *send_hdlc_user_data);
00778 
00779 /*! Release a T.30 context.
00780     \brief Release a T.30 context.
00781     \param s The T.30 context.
00782     \return 0 for OK, else -1. */
00783 int t30_release(t30_state_t *s);
00784 
00785 /*! Free a T.30 context.
00786     \brief Free a T.30 context.
00787     \param s The T.30 context.
00788     \return 0 for OK, else -1. */
00789 int t30_free(t30_state_t *s);
00790 
00791 /*! Restart a T.30 context.
00792     \brief Restart a T.30 context.
00793     \param s The T.30 context.
00794     \return 0 for OK, else -1. */
00795 int t30_restart(t30_state_t *s);
00796 
00797 /*! Cleanup a T.30 context if the call terminates.
00798     \brief Cleanup a T.30 context if the call terminates.
00799     \param s The T.30 context. */
00800 void t30_terminate(t30_state_t *s);
00801 
00802 /*! Inform the T.30 engine of a status change in the front end (end of tx, rx signal change, etc.).
00803     \brief Inform the T.30 engine of a status change in the front end (end of tx, rx signal change, etc.).
00804     \param user_data The T.30 context.
00805     \param status The type of status change which occured. */
00806 void t30_front_end_status(void *user_data, int status);
00807 
00808 /*! Get a bit of received non-ECM image data.
00809     \brief Get a bit of received non-ECM image data.
00810     \param user_data An opaque pointer, which must point to the T.30 context.
00811     \return The next bit to transmit. */
00812 int t30_non_ecm_get_bit(void *user_data);
00813 
00814 /*! Get a byte of received non-ECM image data.
00815     \brief Get a byte of received non-ECM image data.
00816     \param user_data An opaque pointer, which must point to the T.30 context.
00817     \return The next byte to transmit. */
00818 int t30_non_ecm_get_byte(void *user_data);
00819 
00820 /*! Get a chunk of received non-ECM image data.
00821     \brief Get a bit of received non-ECM image data.
00822     \param user_data An opaque pointer, which must point to the T.30 context.
00823     \param buf The buffer to contain the data.
00824     \param max_len The maximum length of the chunk.
00825     \return The actual length of the chunk. */
00826 int t30_non_ecm_get_chunk(void *user_data, uint8_t buf[], int max_len);
00827 
00828 /*! Process a bit of received non-ECM image data.
00829     \brief Process a bit of received non-ECM image data
00830     \param user_data An opaque pointer, which must point to the T.30 context.
00831     \param bit The received bit. */
00832 void t30_non_ecm_put_bit(void *user_data, int bit);
00833 
00834 /*! Process a byte of received non-ECM image data.
00835     \brief Process a byte of received non-ECM image data
00836     \param user_data An opaque pointer, which must point to the T.30 context.
00837     \param byte The received byte. */
00838 void t30_non_ecm_put_byte(void *user_data, int byte);
00839 
00840 /*! Process a chunk of received non-ECM image data.
00841     \brief Process a chunk of received non-ECM image data
00842     \param user_data An opaque pointer, which must point to the T.30 context.
00843     \param buf The buffer containing the received data.
00844     \param len The length of the data in buf. */
00845 void t30_non_ecm_put_chunk(void *user_data, const uint8_t buf[], int len);
00846 
00847 /*! Process a received HDLC frame.
00848     \brief Process a received HDLC frame.
00849     \param user_data The T.30 context.
00850     \param msg The HDLC message.
00851     \param len The length of the message, in octets.
00852     \param ok TRUE if the frame was received without error. */
00853 void t30_hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok);
00854 
00855 /*! Report the passage of time to the T.30 engine.
00856     \brief Report the passage of time to the T.30 engine.
00857     \param s The T.30 context.
00858     \param samples The time change in 1/8000th second steps. */
00859 void t30_timer_update(t30_state_t *s, int samples);
00860 
00861 /*! Get the current transfer statistics for the file being sent or received.
00862     \brief Get the current transfer statistics.
00863     \param s The T.30 context.
00864     \param t A pointer to a buffer for the statistics. */
00865 void t30_get_transfer_statistics(t30_state_t *s, t30_stats_t *t);
00866 
00867 /*! Request a local interrupt of FAX exchange.
00868     \brief Request a local interrupt of FAX exchange.
00869     \param s The T.30 context.
00870     \param state TRUE to enable interrupt request, else FALSE. */
00871 void t30_local_interrupt_request(t30_state_t *s, int state);
00872 
00873 #if defined(__cplusplus)
00874 }
00875 #endif
00876 
00877 #endif
00878 /*- End of file ------------------------------------------------------------*/

Generated on Tue Oct 7 20:25:48 2008 for spandsp by  doxygen 1.5.6