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 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: t30.h,v 1.79 2007/08/11 13:36:16 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 - T.4 1D (MH), T.4 2D,(MR) and T.6 (MMR) compression. 00046 - Error correction (ECM) 00047 - All standard resolutions and page sizes 00048 00049 \section t30_page_sec_2 How does it work? 00050 00051 Some of the following is paraphrased from some notes found a while ago on the Internet. 00052 I cannot remember exactly where they came from, but they are useful. 00053 00054 The answer (CED) tone 00055 00056 The T.30 standard says an answering fax device must send CED (a 2100Hz tone) for 00057 approximately 3 seconds before sending the first handshake message. Some machines 00058 send an 1100Hz or 1850Hz tone, and some send no tone at all. In fact, this answer 00059 tone is so unpredictable, it cannot really be used. It should, however, always be 00060 generated according to the specification. 00061 00062 Common Timing Deviations 00063 00064 The T.30 spec. specifies a number of time-outs. For example, after dialing a number, 00065 a calling fax system should listen for a response for 35 seconds before giving up. 00066 These time-out periods are as follows: 00067 00068 * T1 - 35+-5s: the maximum time for which two fax system will attempt to identify each other 00069 * T2 - 6+-1s: a time-out used to start the sequence for changing transmit parameters 00070 * T3 - 10+-5s: a time-out used in handling operator interrupts 00071 * T5 - 60+-5s: a time-out used in error correction mode 00072 00073 These time-outs are sometimes misinterpreted. In addition, they are routinely 00074 ignored, sometimes with good reason. For example, after placing a call, the 00075 calling fax system is supposed to wait for 35 seconds before giving up. If the 00076 answering unit does not answer on the first ring or if a voice answering machine 00077 is connected to the line, or if there are many delays through the network, 00078 the delay before answer can be much longer than 35 seconds. 00079 00080 Fax units that support error correction mode (ECM) can respond to a post-image 00081 handshake message with a receiver not ready (RNR) message. The calling unit then 00082 queries the receiving fax unit with a receiver ready (RR) message. If the 00083 answering unit is still busy (printing for example), it will repeat the RNR 00084 message. According to the T.30 standard, this sequence (RR/RNR RR/RNR) can be 00085 repeated for up to the end of T5 (60+-5s). However, many fax systems 00086 ignore the time-out and will continue the sequence indefinitely, unless the user 00087 manually overrides. 00088 00089 All the time-outs are subject to alteration, and sometimes misuse. Good T.30 00090 implementations must do the right thing, and tolerate others doing the wrong thing. 00091 00092 Variations in the inter-carrier gap 00093 00094 T.30 specifies 75+-20ms of silence between signals using different modulation 00095 schemes. Examples are between the end of a DCS signal and the start of a TCF signal, 00096 and between the end of an image and the start of a post-image signal. Many fax systems 00097 violate this requirement, especially for the silent period between DCS and TCF. 00098 This may be stretched to well over 100ms. If this period is too long, it can interfere with 00099 handshake signal error recovery, should a packet be corrupted on the line. Systems 00100 should ensure they stay within the prescribed T.30 limits, and be tolerant of others 00101 being out of spec.. 00102 00103 Other timing variations 00104 00105 Testing is required to determine the ability of a fax system to handle 00106 variations in the duration of pauses between unacknowledged handshake message 00107 repetitions, and also in the pauses between the receipt of a handshake command and 00108 the start of a response to that command. In order to reduce the total 00109 transmission time, many fax systems start sending a response message before the 00110 end of the command has been received. 00111 00112 Other deviations from the T.30 standard 00113 00114 There are many other commonly encountered variations between machines, including: 00115 00116 * frame sequence deviations 00117 * preamble and flag sequence variations 00118 * improper EOM usage 00119 * unusual data rate fallback sequences 00120 * common training pattern detection algorithms 00121 * image transmission deviations 00122 * use of the talker echo protect tone 00123 * image padding and short lines 00124 * RTP/RTN handshake message usage 00125 * long duration lines 00126 * nonstandard disconnect sequences 00127 * DCN usage 00128 */ 00129 00130 #define T30_MAX_DIS_DTC_DCS_LEN 22 00131 #define T30_MAX_IDENT_LEN 21 00132 #define T30_MAX_LOCAL_NSF_LEN 100 00133 00134 typedef struct t30_state_s t30_state_t; 00135 00136 /*! 00137 T.30 phase B callback handler. 00138 \brief T.30 phase B callback handler. 00139 \param s The T.30 context. 00140 \param user_data An opaque pointer. 00141 \param result The phase B event code. 00142 */ 00143 typedef void (t30_phase_b_handler_t)(t30_state_t *s, void *user_data, int result); 00144 00145 /*! 00146 T.30 phase D callback handler. 00147 \brief T.30 phase D callback handler. 00148 \param s The T.30 context. 00149 \param user_data An opaque pointer. 00150 \param result The phase D event code. 00151 */ 00152 typedef void (t30_phase_d_handler_t)(t30_state_t *s, void *user_data, int result); 00153 00154 /*! 00155 T.30 phase E callback handler. 00156 \brief T.30 phase E callback handler. 00157 \param s The T.30 context. 00158 \param user_data An opaque pointer. 00159 \param completion_code The phase E completion code. 00160 */ 00161 typedef void (t30_phase_e_handler_t)(t30_state_t *s, void *user_data, int completion_code); 00162 00163 /*! 00164 T.30 document handler. 00165 \brief T.30 document handler. 00166 \param s The T.30 context. 00167 \param user_data An opaque pointer. 00168 \param result The document event code. 00169 */ 00170 typedef int (t30_document_handler_t)(t30_state_t *s, void *user_data, int status); 00171 00172 /*! 00173 T.30 set a receive or transmit type handler. 00174 \brief T.30 set a receive or transmit type handler. 00175 \param user_data An opaque pointer. 00176 \param type The modem, tone or silence to be sent or received. 00177 \param short_train TRUE if the short training sequence should be used (where one exists). 00178 \param use_hdlc FALSE for bit stream, TRUE for HDLC framing. 00179 */ 00180 typedef void (t30_set_handler_t)(void *user_data, int type, int short_train, int use_hdlc); 00181 00182 /*! 00183 T.30 send HDLC handler. 00184 \brief T.30 send HDLC handler. 00185 \param user_data An opaque pointer. 00186 \param msg The HDLC message. 00187 \param len The length of the message. 00188 */ 00189 typedef void (t30_send_hdlc_handler_t)(void *user_data, const uint8_t *msg, int len); 00190 00191 /*! 00192 T.30 protocol completion codes, at phase E. 00193 */ 00194 enum 00195 { 00196 T30_ERR_OK = 0, /*! OK */ 00197 00198 /* Link problems */ 00199 T30_ERR_CEDTONE, /*! The CED tone exceeded 5s */ 00200 T30_ERR_T0_EXPIRED, /*! Timed out waiting for initial communication */ 00201 T30_ERR_T1_EXPIRED, /*! Timed out waiting for the first message */ 00202 T30_ERR_T3_EXPIRED, /*! Timed out waiting for procedural interrupt */ 00203 T30_ERR_HDLC_CARRIER, /*! The HDLC carrier did not stop in a timely manner */ 00204 T30_ERR_CANNOT_TRAIN, /*! Failed to train with any of the compatible modems */ 00205 T30_ERR_OPERINTFAIL, /*! Operator intervention failed */ 00206 T30_ERR_INCOMPATIBLE, /*! Far end is not compatible */ 00207 T30_ERR_RX_INCAPABLE, /*! Far end is not able to receive */ 00208 T30_ERR_TX_INCAPABLE, /*! Far end is not able to transmit */ 00209 T30_ERR_NORESSUPPORT, /*! Far end cannot receive at the resolution of the image */ 00210 T30_ERR_NOSIZESUPPORT, /*! Far end cannot receive at the size of image */ 00211 T30_ERR_UNEXPECTED, /*! Unexpected message received */ 00212 00213 /* TIFF file problems */ 00214 T30_ERR_FILEERROR, /*! TIFF/F file cannot be opened */ 00215 T30_ERR_NOPAGE, /*! TIFF/F page not found */ 00216 T30_ERR_BADTIFF, /*! TIFF/F format is not compatible */ 00217 00218 /* Phase E status values returned to a transmitter */ 00219 T30_ERR_BADDCSTX, /*! Received bad response to DCS or training */ 00220 T30_ERR_BADPGTX, /*! Received a DCN from remote after sending a page */ 00221 T30_ERR_ECMPHDTX, /*! Invalid ECM response received from receiver */ 00222 T30_ERR_T5_EXPIRED, /*! Timed out waiting for receiver ready (ECM mode) */ 00223 T30_ERR_GOTDCNTX, /*! Received a DCN while waiting for a DIS */ 00224 T30_ERR_INVALRSPTX, /*! Invalid response after sending a page */ 00225 T30_ERR_NODISTX, /*! Received other than DIS while waiting for DIS */ 00226 T30_ERR_PHBDEADTX, /*! Received no response to DCS, training or TCF */ 00227 T30_ERR_PHDDEADTX, /*! No response after sending a page */ 00228 00229 /* Phase E status values returned to a receiver */ 00230 T30_ERR_ECMPHDRX, /*! Invalid ECM response received from transmitter */ 00231 T30_ERR_GOTDCSRX, /*! DCS received while waiting for DTC */ 00232 T30_ERR_INVALCMDRX, /*! Unexpected command after page received */ 00233 T30_ERR_NOCARRIERRX, /*! Carrier lost during fax receive */ 00234 T30_ERR_NOEOLRX, /*! Timed out while waiting for EOL (end Of line) */ 00235 T30_ERR_NOFAXRX, /*! Timed out while waiting for first line */ 00236 T30_ERR_T2EXPDCNRX, /*! Timer T2 expired while waiting for DCN */ 00237 T30_ERR_T2EXPDRX, /*! Timer T2 expired while waiting for phase D */ 00238 T30_ERR_T2EXPFAXRX, /*! Timer T2 expired while waiting for fax page */ 00239 T30_ERR_T2EXPMPSRX, /*! Timer T2 expired while waiting for next fax page */ 00240 T30_ERR_T2EXPRRRX, /*! Timer T2 expired while waiting for RR command */ 00241 T30_ERR_T2EXPRX, /*! Timer T2 expired while waiting for NSS, DCS or MCF */ 00242 T30_ERR_DCNWHYRX, /*! Unexpected DCN while waiting for DCS or DIS */ 00243 T30_ERR_DCNDATARX, /*! Unexpected DCN while waiting for image data */ 00244 T30_ERR_DCNFAXRX, /*! Unexpected DCN while waiting for EOM, EOP or MPS */ 00245 T30_ERR_DCNPHDRX, /*! Unexpected DCN after EOM or MPS sequence */ 00246 T30_ERR_DCNRRDRX, /*! Unexpected DCN after RR/RNR sequence */ 00247 T30_ERR_DCNNORTNRX, /*! Unexpected DCN after requested retransmission */ 00248 00249 T30_ERR_BADPAGE, /*! TIFF/F page number tag missing */ 00250 T30_ERR_BADTAG, /*! Incorrect values for TIFF/F tags */ 00251 T30_ERR_BADTIFFHDR, /*! Bad TIFF/F header - incorrect values in fields */ 00252 T30_ERR_NODATA, /*! Data requested is not available (NSF, DIS, DCS) */ 00253 T30_ERR_NOMEM, /*! Cannot allocate memory for more pages */ 00254 T30_ERR_NOPOLL, /*! Poll not accepted */ 00255 T30_ERR_RETRYDCN, /*! Disconnected after permitted retries */ 00256 T30_ERR_CALLDROPPED /*! The call dropped prematurely */ 00257 }; 00258 00259 /*! 00260 I/O modes for the T.30 protocol. 00261 */ 00262 enum 00263 { 00264 T30_MODEM_NONE = 0, 00265 T30_MODEM_PAUSE, 00266 T30_MODEM_CED, 00267 T30_MODEM_CNG, 00268 T30_MODEM_V21, 00269 T30_MODEM_V27TER_2400, 00270 T30_MODEM_V27TER_4800, 00271 T30_MODEM_V29_7200, 00272 T30_MODEM_V29_9600, 00273 T30_MODEM_V17_7200, 00274 T30_MODEM_V17_9600, 00275 T30_MODEM_V17_12000, 00276 T30_MODEM_V17_14400, 00277 T30_MODEM_DONE 00278 }; 00279 00280 enum 00281 { 00282 T30_FRONT_END_SEND_STEP_COMPLETE = 0, 00283 T30_FRONT_END_SEND_COMPLETE, 00284 /*! The current receive has completed. This is only needed to report an 00285 unexpected end of the receive operation, as might happen with T.38 00286 dying. */ 00287 T30_FRONT_END_RECEIVE_COMPLETE, 00288 T30_FRONT_END_SIGNAL_PRESENT, 00289 T30_FRONT_END_SIGNAL_ABSENT 00290 }; 00291 00292 enum 00293 { 00294 T30_SUPPORT_V27TER = 0x01, 00295 T30_SUPPORT_V29 = 0x02, 00296 T30_SUPPORT_V17 = 0x04, 00297 T30_SUPPORT_V34 = 0x08, 00298 T30_SUPPORT_IAF = 0x10, 00299 }; 00300 00301 enum 00302 { 00303 T30_SUPPORT_NO_COMPRESSION = 0x01, 00304 T30_SUPPORT_T4_1D_COMPRESSION = 0x02, 00305 T30_SUPPORT_T4_2D_COMPRESSION = 0x04, 00306 T30_SUPPORT_T6_COMPRESSION = 0x08, 00307 T30_SUPPORT_T85_COMPRESSION = 0x10, /* Monochrome JBIG */ 00308 T30_SUPPORT_T43_COMPRESSION = 0x20, /* Colour JBIG */ 00309 T30_SUPPORT_T45_COMPRESSION = 0x40 /* Run length colour compression */ 00310 }; 00311 00312 enum 00313 { 00314 T30_SUPPORT_STANDARD_RESOLUTION = 0x01, 00315 T30_SUPPORT_FINE_RESOLUTION = 0x02, 00316 T30_SUPPORT_SUPERFINE_RESOLUTION = 0x04, 00317 00318 T30_SUPPORT_R4_RESOLUTION = 0x10000, 00319 T30_SUPPORT_R8_RESOLUTION = 0x20000, 00320 T30_SUPPORT_R16_RESOLUTION = 0x40000, 00321 00322 T30_SUPPORT_300_300_RESOLUTION = 0x100000, 00323 T30_SUPPORT_400_400_RESOLUTION = 0x200000, 00324 T30_SUPPORT_600_600_RESOLUTION = 0x400000, 00325 T30_SUPPORT_1200_1200_RESOLUTION = 0x800000, 00326 T30_SUPPORT_300_600_RESOLUTION = 0x1000000, 00327 T30_SUPPORT_400_800_RESOLUTION = 0x2000000, 00328 T30_SUPPORT_600_1200_RESOLUTION = 0x4000000 00329 }; 00330 00331 enum 00332 { 00333 T30_SUPPORT_215MM_WIDTH = 0x01, 00334 T30_SUPPORT_255MM_WIDTH = 0x02, 00335 T30_SUPPORT_303MM_WIDTH = 0x04, 00336 00337 T30_SUPPORT_UNLIMITED_LENGTH = 0x10000, 00338 T30_SUPPORT_A4_LENGTH = 0x20000, 00339 T30_SUPPORT_B4_LENGTH = 0x40000, 00340 T30_SUPPORT_US_LETTER_LENGTH = 0x80000, 00341 T30_SUPPORT_US_LEGAL_LENGTH = 0x100000 00342 }; 00343 00344 enum 00345 { 00346 T30_SUPPORT_SEP = 0x01, 00347 T30_SUPPORT_PSA = 0x02 00348 }; 00349 00350 enum 00351 { 00352 T30_IAF_MODE_T37 = 0x01, 00353 T30_IAF_MODE_T38 = 0x02, 00354 T30_IAF_MODE_FLOW_CONTROL = 0x04, 00355 /*! Continuous flow mode means data is sent as fast as possible, usually across 00356 the Internet, where speed is not constrained by a PSTN modem. */ 00357 T30_IAF_MODE_CONTINUOUS_FLOW = 0x08, 00358 /*! No TCF means TCF is not exchanged. The end points must sort out usable speed 00359 issues locally. */ 00360 T30_IAF_MODE_NO_TCF = 0x10, 00361 /*! No fill bits means do not insert fill bits, even if the T.30 messages request 00362 them. */ 00363 T30_IAF_MODE_NO_FILL_BITS = 0x20, 00364 /*! No indicators means do not send indicator messages when using T.38. */ 00365 T30_IAF_MODE_NO_INDICATORS = 0x40 00366 }; 00367 00368 /*! 00369 Exact widths in PELs for the difference resolutions, and page widths. 00370 Note: 00371 The A4 widths also apply to North American letter and legal. 00372 The R4 resolution widths are not supported in recent versions of T.30 00373 Only images of exactly these widths are acceptable for FAX transmisson. 00374 00375 R4 864 pels/215mm for ISO A4, North American Letter and Legal 00376 R4 1024 pels/255mm for ISO B4 00377 R4 1216 pels/303mm for ISO A3 00378 R8 1728 pels/215mm for ISO A4, North American Letter and Legal 00379 R8 2048 pels/255mm for ISO B4 00380 R8 2432 pels/303mm for ISO A3 00381 R16 3456 pels/215mm for ISO A4, North American Letter and Legal 00382 R16 4096 pels/255mm for ISO B4 00383 R16 4864 pels/303mm for ISO A3 00384 */ 00385 typedef enum 00386 { 00387 T30_WIDTH_R4_A4 = 864, 00388 T30_WIDTH_R4_B4 = 1024, 00389 T30_WIDTH_R4_A3 = 1216, 00390 T30_WIDTH_R8_A4 = 1728, 00391 T30_WIDTH_R8_B4 = 2048, 00392 T30_WIDTH_R8_A3 = 2432, 00393 T30_WIDTH_R16_A4 = 3456, 00394 T30_WIDTH_R16_B4 = 4096, 00395 T30_WIDTH_R16_A3 = 4864 00396 } t30_image_width_t; 00397 00398 /*! 00399 Length of the various supported paper sizes, in pixels at the various Y resolutions. 00400 Paper sizes are 00401 A4 (297mm x 215mm) 00402 B4 (364mm x 255mm) 00403 A3 (303mm x ???mm) 00404 North American Letter (215.9mm x 279.4mm) 00405 North American Legal (215.9mm x 355.6mm) 00406 Unlimited 00407 */ 00408 enum 00409 { 00410 /* A4 is 297mm long */ 00411 T30_LENGTH_STANDARD_A4 = 1143, 00412 T30_LENGTH_FINE_A4 = 2286, 00413 T30_LENGTH_SUPERFINE_A4 = 4573, 00414 /* B4 is 364mm long */ 00415 T30_LENGTH_STANDARD_B4 = 1401, 00416 T30_LENGTH_FINE_B4 = 2802, 00417 T30_LENGTH_SUPERFINE_B4 = 5605, 00418 /* North American letter is 279.4mm long */ 00419 T30_LENGTH_STANDARD_US_LETTER = 1075, 00420 T30_LENGTH_FINE_US_LETTER = 2151, 00421 T30_LENGTH_SUPERFINE_US_LETTER = 4302, 00422 /* North American legal is 355.6mm long */ 00423 T30_LENGTH_STANDARD_US_LEGAL = 1369, 00424 T30_LENGTH_FINE_US_LEGAL = 2738, 00425 T30_LENGTH_SUPERFINE_US_LEGAL = 5476 00426 }; 00427 00428 /*! 00429 T.30 FAX channel descriptor. This defines the state of a single working 00430 instance of a T.30 FAX channel. 00431 */ 00432 struct t30_state_s 00433 { 00434 /* This must be kept the first thing in the structure, so it can be pointed 00435 to reliably as the structures change over time. */ 00436 t4_state_t t4; 00437 00438 /*! \brief TRUE if behaving as the calling party */ 00439 int calling_party; 00440 00441 00442 /*! \brief The received DCS, formatted as an ASCII string, for inclusion 00443 in the TIFF file. */ 00444 char rx_dcs_string[T30_MAX_DIS_DTC_DCS_LEN*3 + 1]; 00445 /*! \brief The local identifier string. */ 00446 char local_ident[T30_MAX_IDENT_LEN]; 00447 /*! \brief The identifier string supplied by the remote FAX machine. */ 00448 char far_ident[T30_MAX_IDENT_LEN]; 00449 /*! \brief The sub-address string to be sent to the remote FAX machine. */ 00450 char local_sub_address[T30_MAX_IDENT_LEN]; 00451 /*! \brief The sub-address string supplied by the remote FAX machine. */ 00452 char far_sub_address[T30_MAX_IDENT_LEN]; 00453 /*! \brief The selective polling sub-address supplied by the remote FAX machine. */ 00454 char sep_address[T30_MAX_IDENT_LEN]; 00455 /*! \brief The polled sub-address supplied by the remote FAX machine. */ 00456 char psa_address[T30_MAX_IDENT_LEN]; 00457 /*! \brief A password to be associated with the T.30 context. */ 00458 char local_password[T30_MAX_IDENT_LEN]; 00459 /*! \brief A password expected from the far end. */ 00460 char far_password[T30_MAX_IDENT_LEN]; 00461 /*! \brief The text which will be used in FAX page header. No text results 00462 in no header line. */ 00463 char header_info[50 + 1]; 00464 /*! \brief The country of origin of the remote machine, if known, else NULL. */ 00465 const char *country; 00466 /*! \brief The vendor of the remote machine, if known, else NULL. */ 00467 const char *vendor; 00468 /*! \brief The model of the remote machine, if known, else NULL. */ 00469 const char *model; 00470 uint8_t local_nsf[T30_MAX_LOCAL_NSF_LEN]; 00471 int local_nsf_len; 00472 00473 /*! \brief A pointer to a callback routine to be called when phase B events 00474 occur. */ 00475 t30_phase_b_handler_t *phase_b_handler; 00476 /*! \brief An opaque pointer supplied in event B callbacks. */ 00477 void *phase_b_user_data; 00478 /*! \brief A pointer to a callback routine to be called when phase D events 00479 occur. */ 00480 t30_phase_d_handler_t *phase_d_handler; 00481 /*! \brief An opaque pointer supplied in event D callbacks. */ 00482 void *phase_d_user_data; 00483 /*! \brief A pointer to a callback routine to be called when phase E events 00484 occur. */ 00485 t30_phase_e_handler_t *phase_e_handler; 00486 /*! \brief An opaque pointer supplied in event E callbacks. */ 00487 void *phase_e_user_data; 00488 00489 /*! \brief A pointer to a callback routine to be called when document events 00490 (e.g. end of transmitted document) occur. */ 00491 t30_document_handler_t *document_handler; 00492 /*! \brief An opaque pointer supplied in document callbacks. */ 00493 void *document_user_data; 00494 00495 t30_set_handler_t *set_rx_type_handler; 00496 void *set_rx_type_user_data; 00497 t30_set_handler_t *set_tx_type_handler; 00498 void *set_tx_type_user_data; 00499 00500 t30_send_hdlc_handler_t *send_hdlc_handler; 00501 void *send_hdlc_user_data; 00502 00503 int phase; 00504 int next_phase; 00505 int state; 00506 int step; 00507 00508 uint8_t dcs_frame[T30_MAX_DIS_DTC_DCS_LEN]; 00509 int dcs_len; 00510 uint8_t dis_dtc_frame[T30_MAX_DIS_DTC_DCS_LEN]; 00511 int dis_dtc_len; 00512 /*! \brief TRUE if a valid DIS has been received from the far end. */ 00513 int dis_received; 00514 /*! \brief TRUE if a valid passwrod has been received from the far end. */ 00515 int far_password_ok; 00516 00517 /*! \brief A flag to indicate a message is in progress. */ 00518 int in_message; 00519 00520 /*! \brief TRUE if the short training sequence should be used. */ 00521 int short_train; 00522 00523 /*! \brief A count of the number of bits in the trainability test. */ 00524 int training_test_bits; 00525 int training_current_zeros; 00526 int training_most_zeros; 00527 00528 /*! \brief The current fallback step for the fast message transfer modem. */ 00529 int current_fallback; 00530 /*! \brief The subset of supported modems allowed at the current time, allowing for negotiation. */ 00531 int current_permitted_modems; 00532 /*! \brief TRUE if a carrier is present. Otherwise FALSE. */ 00533 int rx_signal_present; 00534 /*! \brief TRUE if a modem has trained correctly. */ 00535 int rx_trained; 00536 int current_rx_type; 00537 int current_tx_type; 00538 00539 /*! \brief T0 is the answer timeout when calling another FAX machine. 00540 Placing calls is handled outside the FAX processing, but this timeout keeps 00541 running until V.21 modulation is sent or received. 00542 T1 is the remote terminal identification timeout (in audio samples). */ 00543 int timer_t0_t1; 00544 /*! \brief T2 is the HDLC command timeout. 00545 T4 is the HDLC response timeout (in audio samples). */ 00546 int timer_t2_t4; 00547 /*! \brief TRUE if the T2/T4 timer is actually timing T4 */ 00548 int timer_is_t4; 00549 /*! \brief Procedural interrupt timeout (in audio samples). */ 00550 int timer_t3; 00551 /*! \brief This is only used in error correcting mode. */ 00552 int timer_t5; 00553 /*! \brief This is only used in full duplex (e.g. ISDN) modes. */ 00554 int timer_t6; 00555 /*! \brief This is only used in full duplex (e.g. ISDN) modes. */ 00556 int timer_t7; 00557 /*! \brief This is only used in full duplex (e.g. ISDN) modes. */ 00558 int timer_t8; 00559 00560 int far_end_detected; 00561 00562 int local_interrupt_pending; 00563 int crp_enabled; 00564 int line_encoding; 00565 int output_encoding; 00566 uint8_t min_scan_time_code; 00567 int x_resolution; 00568 int y_resolution; 00569 t30_image_width_t image_width; 00570 int retries; 00571 /*! \brief TRUE if error correcting mode is used. */ 00572 int error_correcting_mode; 00573 int ppr_count; 00574 int receiver_not_ready_count; 00575 int octets_per_ecm_frame; 00576 uint8_t ecm_data[256][260]; 00577 int16_t ecm_len[256]; 00578 uint8_t ecm_frame_map[3 + 32]; 00579 00580 /*! \brief The current page number in ECM mode */ 00581 int ecm_page; 00582 00583 /*! \brief The current block number in ECM mode */ 00584 int ecm_block; 00585 00586 /*! \brief The number of frames in the current block number in ECM mode */ 00587 int ecm_frames; 00588 00589 /*! \brief The number of frames in the current burst of image transmission in ECM mode */ 00590 int ecm_frames_this_burst; 00591 int ecm_current_frame; 00592 int ecm_at_page_end; 00593 int next_tx_step; 00594 int next_rx_step; 00595 char rx_file[256]; 00596 int rx_stop_page; 00597 char tx_file[256]; 00598 int tx_start_page; 00599 int tx_stop_page; 00600 int current_status; 00601 int iaf; 00602 int supported_modems; 00603 int supported_compressions; 00604 int supported_resolutions; 00605 int supported_image_sizes; 00606 int supported_polling_features; 00607 int support_fnv; 00608 int ecm_allowed; 00609 00610 int last_pps_fcf2; 00611 int ecm_first_bad_frame; 00612 00613 /*! \brief Error and flow logging control */ 00614 logging_state_t logging; 00615 }; 00616 00617 typedef struct 00618 { 00619 /*! \brief The current bit rate for image transfer. */ 00620 int bit_rate; 00621 /*! \brief TRUE if error correcting mode is used. */ 00622 int error_correcting_mode; 00623 /*! \brief The number of pages transferred so far. */ 00624 int pages_transferred; 00625 /*! \brief The number of pages in the file (<0 if not known). */ 00626 int pages_in_file; 00627 /*! \brief The number of horizontal pixels in the most recent page. */ 00628 int width; 00629 /*! \brief The number of vertical pixels in the most recent page. */ 00630 int length; 00631 /*! \brief The number of bad pixel rows in the most recent page. */ 00632 int bad_rows; 00633 /*! \brief The largest number of bad pixel rows in a block in the most recent page. */ 00634 int longest_bad_row_run; 00635 /*! \brief The horizontal column-to-column resolution of the page in pixels per metre */ 00636 int x_resolution; 00637 /*! \brief The vertical row-to-row resolution of the page in pixels per metre */ 00638 int y_resolution; 00639 /*! \brief The type of compression used between the FAX machines */ 00640 int encoding; 00641 /*! \brief The size of the image, in bytes */ 00642 int image_size; 00643 /*! \brief Current status */ 00644 int current_status; 00645 } t30_stats_t; 00646 00647 #if defined(__cplusplus) 00648 extern "C" 00649 { 00650 #endif 00651 00652 /*! Initialise a T.30 context. 00653 \brief Initialise a T.30 context. 00654 \param s The T.30 context. 00655 \param calling_party TRUE if the context is for a calling party. FALSE if the 00656 context is for an answering party. 00657 \return 0 for OK, else -1. */ 00658 int t30_init(t30_state_t *s, 00659 int calling_party, 00660 t30_set_handler_t *set_rx_type_handler, 00661 void *set_rx_type_user_data, 00662 t30_set_handler_t *set_tx_type_handler, 00663 void *set_tx_type_user_data, 00664 t30_send_hdlc_handler_t *send_hdlc_handler, 00665 void *send_hdlc_user_data); 00666 00667 /*! Release a T.30 context. 00668 \brief Release a T.30 context. 00669 \param s The T.30 context. */ 00670 void t30_release(t30_state_t *s); 00671 00672 /*! Restart a T.30 context. 00673 \brief Restart a T.30 context. 00674 \param s The T.30 context. 00675 \return 0 for OK, else -1. */ 00676 int t30_restart(t30_state_t *s); 00677 00678 /*! Create and initialise a T.30 context. 00679 \brief Create and initialise a T.30 context. 00680 \param calling_party TRUE if the context is for a calling party. FALSE if the 00681 context is for an answering party. 00682 \return A pointer to the FAX context, or NULL if one could not be created. 00683 */ 00684 t30_state_t *t30_create(int calling_party, 00685 t30_set_handler_t *set_rx_type_handler, 00686 void *set_rx_type_user_data, 00687 t30_set_handler_t *set_tx_type_handler, 00688 void *set_tx_type_user_data, 00689 t30_send_hdlc_handler_t *send_hdlc_handler, 00690 void *send_hdlc_user_data); 00691 00692 /*! Free a T.30 context. 00693 \brief Free a T.30 context. 00694 \param s The T.30 context. */ 00695 void t30_free(t30_state_t *s); 00696 00697 /*! Cleanup a T.30 context if the call terminates. 00698 \brief Cleanup a T.30 context if the call terminates. 00699 \param s The T.30 context. */ 00700 void t30_terminate(t30_state_t *s); 00701 00702 /*! Return a text name for a T.30 frame type. 00703 \brief Return a text name for a T.30 frame type. 00704 \param x The frametype octet. 00705 \return A pointer to the text name for the frame type. If the frame type is 00706 not value, the string "???" is returned. */ 00707 const char *t30_frametype(uint8_t x); 00708 00709 /*! Decode a DIS, DTC or DCS frame, and log the contents. 00710 \brief Decode a DIS, DTC or DCS frame, and log the contents. 00711 \param s The T.30 context. 00712 \param dis A pointer to the frame to be decoded. 00713 \param len The length of the frame. */ 00714 void t30_decode_dis_dtc_dcs(t30_state_t *s, const uint8_t *dis, int len); 00715 00716 /*! Convert a phase E completion code to a short text description. 00717 \brief Convert a phase E completion code to a short text description. 00718 \param result The result code. 00719 \return A pointer to the description. */ 00720 const char *t30_completion_code_to_str(int result); 00721 00722 /*! Set Internet aware FAX (IAF) mode. 00723 \brief Set Internet aware FAX (IAF) mode. 00724 \param s The T.30 context. 00725 \param iaf TRUE for IAF, or FALSE for non-IAF. */ 00726 void t30_set_iaf_mode(t30_state_t *s, int iaf); 00727 00728 /*! Set the sub-address associated with a T.30 context. 00729 \brief Set the sub-address associated with a T.30 context. 00730 \param s The T.30 context. 00731 \param sub_address A pointer to the sub-address. 00732 \return 0 for OK, else -1. */ 00733 int t30_set_local_sub_address(t30_state_t *s, const char *sub_address); 00734 00735 /*! Set the header information associated with a T.30 context. 00736 \brief Set the header information associated with a T.30 context. 00737 \param s The T.30 context. 00738 \param info A pointer to the information string. 00739 \return 0 for OK, else -1. */ 00740 int t30_set_header_info(t30_state_t *s, const char *info); 00741 00742 /*! Set the local identifier associated with a T.30 context. 00743 \brief Set the local identifier associated with a T.30 context. 00744 \param s The T.30 context. 00745 \param id A pointer to the identifier. 00746 \return 0 for OK, else -1. */ 00747 int t30_set_local_ident(t30_state_t *s, const char *id); 00748 00749 int t30_set_local_nsf(t30_state_t *s, const uint8_t *nsf, int len); 00750 00751 /*! Get the sub-address associated with a T.30 context. 00752 \brief Get the sub-address associated with a T.30 context. 00753 \param s The T.30 context. 00754 \param sub_address A pointer to a buffer for the sub-address. The buffer 00755 should be at least 21 bytes long. 00756 \return the length of the string. */ 00757 size_t t30_get_sub_address(t30_state_t *s, char *sub_address); 00758 00759 /*! Get the header information associated with a T.30 context. 00760 \brief Get the header information associated with a T.30 context. 00761 \param s The T.30 context. 00762 \param sub_address A pointer to a buffer for the header information. The buffer 00763 should be at least 51 bytes long. 00764 \return the length of the string. */ 00765 size_t t30_get_header_info(t30_state_t *s, char *info); 00766 00767 /*! Get the local FAX machine identifier associated with a T.30 context. 00768 \brief Get the local identifier associated with a T.30 context. 00769 \param s The T.30 context. 00770 \param id A pointer to a buffer for the identifier. The buffer should 00771 be at least 21 bytes long. 00772 \return the length of the string. */ 00773 size_t t30_get_local_ident(t30_state_t *s, char *id); 00774 00775 /*! Get the remote FAX machine identifier associated with a T.30 context. 00776 \brief Get the remote identifier associated with a T.30 context. 00777 \param s The T.30 context. 00778 \param id A pointer to a buffer for the identifier. The buffer should 00779 be at least 21 bytes long. 00780 \return the length of the string. */ 00781 size_t t30_get_far_ident(t30_state_t *s, char *id); 00782 00783 /*! Get the country of origin of the remote FAX machine associated with a T.30 context. 00784 \brief Get the country of origin of the remote FAX machine associated with a T.30 context. 00785 \param s The T.30 context. 00786 \return a pointer to the country name, or NULL if the country is not known. */ 00787 const char *t30_get_far_country(t30_state_t *s); 00788 00789 /*! Get the name of the vendor of the remote FAX machine associated with a T.30 context. 00790 \brief Get the name of the vendor of the remote FAX machine associated with a T.30 context. 00791 \param s The T.30 context. 00792 \return a pointer to the vendor name, or NULL if the vendor is not known. */ 00793 const char *t30_get_far_vendor(t30_state_t *s); 00794 00795 /*! Get the name of the model of the remote FAX machine associated with a T.30 context. 00796 \brief Get the name of the model of the remote FAX machine associated with a T.30 context. 00797 \param s The T.30 context. 00798 \return a pointer to the model name, or NULL if the model is not known. */ 00799 const char *t30_get_far_model(t30_state_t *s); 00800 00801 /*! Get the current transfer statistics for the file being sent or received. 00802 \brief Get the current transfer statistics. 00803 \param s The T.30 context. 00804 \param t A pointer to a buffer for the statistics. */ 00805 void t30_get_transfer_statistics(t30_state_t *s, t30_stats_t *t); 00806 00807 /*! Set a callback function for T.30 phase B handling. 00808 \brief Set a callback function for T.30 phase B handling. 00809 \param s The T.30 context. 00810 \param handler The callback function 00811 \param user_data An opaque pointer passed to the callback function. */ 00812 void t30_set_phase_b_handler(t30_state_t *s, t30_phase_b_handler_t *handler, void *user_data); 00813 00814 /*! Set a callback function for T.30 phase D handling. 00815 \brief Set a callback function for T.30 phase D handling. 00816 \param s The T.30 context. 00817 \param handler The callback function 00818 \param user_data An opaque pointer passed to the callback function. */ 00819 void t30_set_phase_d_handler(t30_state_t *s, t30_phase_d_handler_t *handler, void *user_data); 00820 00821 /*! Set a callback function for T.30 phase E handling. 00822 \brief Set a callback function for T.30 phase E handling. 00823 \param s The T.30 context. 00824 \param handler The callback function 00825 \param user_data An opaque pointer passed to the callback function. */ 00826 void t30_set_phase_e_handler(t30_state_t *s, t30_phase_e_handler_t *handler, void *user_data); 00827 00828 /*! Set a callback function for T.30 end of document handling. 00829 \brief Set a callback function for T.30 end of document handling. 00830 \param s The T.30 context. 00831 \param handler The callback function 00832 \param user_data An opaque pointer passed to the callback function. */ 00833 void t30_set_document_handler(t30_state_t *s, t30_document_handler_t *handler, void *user_data); 00834 00835 /*! Specify the file name of the next TIFF file to be received by a T.30 00836 context. 00837 \brief Set next receive file name. 00838 \param s The T.30 context. 00839 \param file The file name 00840 \param stop_page The maximum page to receive. -1 for no restriction. */ 00841 void t30_set_rx_file(t30_state_t *s, const char *file, int stop_page); 00842 00843 /*! Specify the file name of the next TIFF file to be transmitted by a T.30 00844 context. 00845 \brief Set next transmit file name. 00846 \param s The T.30 context. 00847 \param file The file name 00848 \param start_page The first page to send. -1 for no restriction. 00849 \param stop_page The last page to send. -1 for no restriction. */ 00850 void t30_set_tx_file(t30_state_t *s, const char *file, int start_page, int stop_page); 00851 00852 /*! Specify which modem types are supported by a T.30 context. 00853 \brief Specify supported modems. 00854 \param s The T.30 context. 00855 \param supported_modems Bit field list of the supported modems. 00856 \return 0 if OK, else -1. */ 00857 int t30_set_supported_modems(t30_state_t *s, int supported_modems); 00858 00859 /*! Specify a period of responding with receiver not ready. 00860 \brief Specify a period of responding with receiver not ready. 00861 \param s The T.30 context. 00862 \param count The number of times to report receiver not ready. 00863 \return 0 if OK, else -1. */ 00864 int t30_set_receiver_not_ready(t30_state_t *s, int count); 00865 00866 /*! Specify which compression types are supported by a T.30 context. 00867 \brief Specify supported compression types. 00868 \param s The T.30 context. 00869 \param supported_compressions Bit field list of the supported compression types. 00870 \return 0 if OK, else -1. */ 00871 int t30_set_supported_compressions(t30_state_t *s, int supported_compressions); 00872 00873 /*! Specify which resolutions are supported by a T.30 context. 00874 \brief Specify supported resolutions. 00875 \param s The T.30 context. 00876 \param supported_compressions Bit field list of the supported resolutions. 00877 \return 0 if OK, else -1. */ 00878 int t30_set_supported_resolutions(t30_state_t *s, int supported_resolutions); 00879 00880 /*! Specify which images sizes are supported by a T.30 context. 00881 \brief Specify supported image sizes. 00882 \param s The T.30 context. 00883 \param supported_image_sizes Bit field list of the supported widths and lengths. 00884 \return 0 if OK, else -1. */ 00885 int t30_set_supported_image_sizes(t30_state_t *s, int supported_image_sizes); 00886 00887 /*! Specify if error correction mode (ECM) is allowed by a T.30 context. 00888 \brief Select ECM capability. 00889 \param s The T.30 context. 00890 \param enabled TRUE for ECM capable, FALSE for not ECM capable. 00891 \return 0 if OK, else -1. */ 00892 int t30_set_ecm_capability(t30_state_t *s, int enabled); 00893 00894 /*! Specify the output encoding for TIFF files created during FAX reception. 00895 \brief Specify the output encoding for TIFF files created during FAX reception. 00896 \param s The T.30 context. 00897 \param encoding. The coding required. The options are T4_COMPRESSION_ITU_T4_1D, 00898 T4_COMPRESSION_ITU_T4_2D, T4_COMPRESSION_ITU_T6. T6 is usually the 00899 densest option, but support for it is broken in a number of software 00900 packages. 00901 \return 0 if OK, else -1. */ 00902 int t30_set_rx_encoding(t30_state_t *s, int encoding); 00903 00904 /*! Request a local interrupt of FAX exchange. 00905 \brief Request a local interrupt of FAX exchange. 00906 \param s The T.30 context. 00907 \param state TRUE to enable interrupt request, else FALSE. */ 00908 void t30_local_interrupt_request(t30_state_t *s, int state); 00909 00910 /*! Inform the T.30 engine of a status change in the front end (end of tx, rx signal change, etc.). 00911 \brief Inform the T.30 engine of a status change in the front end (end of tx, rx signal change, etc.). 00912 \param user_data The T.30 context. 00913 \param status The type of status change which occured. */ 00914 void t30_front_end_status(void *user_data, int status); 00915 00916 /*! Get a bit of received non-ECM image data. 00917 \brief Get a bit of received non-ECM image data. 00918 \param user_data An opaque pointer, which must point to the T.30 context. 00919 \return The next bit to transmit. */ 00920 int t30_non_ecm_get_bit(void *user_data); 00921 00922 /*! Get a byte of received non-ECM image data. 00923 \brief Get a byte of received non-ECM image data. 00924 \param user_data An opaque pointer, which must point to the T.30 context. 00925 \return The next byte to transmit. */ 00926 int t30_non_ecm_get_byte(void *user_data); 00927 00928 /*! Get a chunk of received non-ECM image data. 00929 \brief Get a bit of received non-ECM image data. 00930 \param user_data An opaque pointer, which must point to the T.30 context. 00931 \param buf The buffer to contain the data. 00932 \param max_len The maximum length of the chunk. 00933 \return The actual length of the chunk. */ 00934 int t30_non_ecm_get_chunk(void *user_data, uint8_t buf[], int max_len); 00935 00936 /*! Process a bit of received non-ECM image data. 00937 \brief Process a bit of received non-ECM image data 00938 \param user_data An opaque pointer, which must point to the T.30 context. 00939 \param bit The received bit. */ 00940 void t30_non_ecm_put_bit(void *user_data, int bit); 00941 00942 /*! Process a byte of received non-ECM image data. 00943 \brief Process a byte of received non-ECM image data 00944 \param user_data An opaque pointer, which must point to the T.30 context. 00945 \param byte The received byte. */ 00946 void t30_non_ecm_put_byte(void *user_data, int byte); 00947 00948 /*! Process a chunk of received non-ECM image data. 00949 \brief Process a chunk of received non-ECM image data 00950 \param user_data An opaque pointer, which must point to the T.30 context. 00951 \param buf The buffer containing the received data. 00952 \param len The length of the data in buf. */ 00953 void t30_non_ecm_put_chunk(void *user_data, const uint8_t buf[], int len); 00954 00955 /*! Process a received HDLC frame. 00956 \brief Process a received HDLC frame. 00957 \param s The T.30 context. 00958 \param msg The HDLC message. 00959 \param int The length of the message, in octets. 00960 \param ok TRUE if the frame was received without error. */ 00961 void t30_hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok); 00962 00963 /*! Report the passage of time to the T.30 engine. 00964 \brief Report the passage of time to the T.30 engine. 00965 \param s The T.30 context. 00966 \param samples The time change in 1/8000th second steps. */ 00967 void t30_timer_update(t30_state_t *s, int samples); 00968 00969 #if defined(__cplusplus) 00970 } 00971 #endif 00972 00973 #endif 00974 /*- End of file ------------------------------------------------------------*/