2 * Asterisk -- A telephony toolkit for Linux.
4 * Simple fax applications
6 * 2007-2008, Dmitry Andrianov <asterisk@dima.spb.ru>
8 * Code based on original implementation by Steve Underwood <steveu@coppice.org>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 <defaultenabled>no</defaultenabled>
17 <depend>spandsp</depend>
18 <conflict>res_fax</conflict>
23 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
35 #include <spandsp/version.h>
37 #include "asterisk/lock.h"
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/app.h"
43 #include "asterisk/dsp.h"
44 #include "asterisk/module.h"
45 #include "asterisk/manager.h"
48 <application name="SendFAX" language="en_US">
53 <parameter name="filename" required="true">
54 <para>Filename of TIFF file to fax</para>
56 <parameter name="a" required="false">
57 <para>Makes the application behave as the answering machine</para>
58 <para>(Default behavior is as calling machine)</para>
62 <para>Send a given TIFF file to the channel as a FAX.</para>
63 <para>This application sets the following channel variables:</para>
65 <variable name="LOCALSTATIONID">
66 <para>To identify itself to the remote end</para>
68 <variable name="LOCALHEADERINFO">
69 <para>To generate a header line on each page</para>
71 <variable name="FAXSTATUS">
72 <value name="SUCCESS"/>
73 <value name="FAILED"/>
75 <variable name="FAXERROR">
76 <para>Cause of failure</para>
78 <variable name="REMOTESTATIONID">
79 <para>The CSID of the remote side</para>
81 <variable name="FAXPAGES">
82 <para>Number of pages sent</para>
84 <variable name="FAXBITRATE">
85 <para>Transmission rate</para>
87 <variable name="FAXRESOLUTION">
88 <para>Resolution of sent fax</para>
93 <application name="ReceiveFAX" language="en_US">
98 <parameter name="filename" required="true">
99 <para>Filename of TIFF file save incoming fax</para>
101 <parameter name="c" required="false">
102 <para>Makes the application behave as the calling machine</para>
103 <para>(Default behavior is as answering machine)</para>
107 <para>Receives a FAX from the channel into the given filename
108 overwriting the file if it already exists.</para>
109 <para>File created will be in TIFF format.</para>
111 <para>This application sets the following channel variables:</para>
113 <variable name="LOCALSTATIONID">
114 <para>To identify itself to the remote end</para>
116 <variable name="LOCALHEADERINFO">
117 <para>To generate a header line on each page</para>
119 <variable name="FAXSTATUS">
120 <value name="SUCCESS"/>
121 <value name="FAILED"/>
123 <variable name="FAXERROR">
124 <para>Cause of failure</para>
126 <variable name="REMOTESTATIONID">
127 <para>The CSID of the remote side</para>
129 <variable name="FAXPAGES">
130 <para>Number of pages sent</para>
132 <variable name="FAXBITRATE">
133 <para>Transmission rate</para>
135 <variable name="FAXRESOLUTION">
136 <para>Resolution of sent fax</para>
144 static const char app_sndfax_name[] = "SendFAX";
145 static const char app_rcvfax_name[] = "ReceiveFAX";
147 #define MAX_SAMPLES 240
149 /* Watchdog. I have seen situations when remote fax disconnects (because of poor line
150 quality) while SpanDSP continues staying in T30_STATE_IV_CTC state forever.
151 To avoid this, we terminate when we see that T30 state does not change for 5 minutes.
152 We also terminate application when more than 30 minutes passed regardless of
153 state changes. This is just a precaution measure - no fax should take that long */
155 #define WATCHDOG_TOTAL_TIMEOUT 30 * 60
156 #define WATCHDOG_STATE_TIMEOUT 5 * 60
159 struct ast_channel *chan;
160 enum ast_t38_state t38state; /* T38 state of the channel */
161 int direction; /* Fax direction: 0 - receiving, 1 - sending */
164 struct ast_control_t38_parameters t38parameters;
165 volatile int finished;
168 static void span_message(int level, const char *msg)
170 if (level == SPAN_LOG_ERROR) {
171 ast_log(LOG_ERROR, "%s", msg);
172 } else if (level == SPAN_LOG_WARNING) {
173 ast_log(LOG_WARNING, "%s", msg);
175 ast_log(LOG_DEBUG, "%s", msg);
179 static int t38_tx_packet_handler(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
181 struct ast_channel *chan = (struct ast_channel *) user_data;
183 struct ast_frame outf = {
184 .frametype = AST_FRAME_MODEM,
185 .subclass.integer = AST_MODEM_T38,
189 /* TODO: Asterisk does not provide means of resending the same packet multiple
190 times so count is ignored at the moment */
192 AST_FRAME_SET_BUFFER(&outf, buf, 0, len);
194 if (ast_write(chan, &outf) < 0) {
195 ast_log(LOG_WARNING, "Unable to write frame to channel; %s\n", strerror(errno));
202 static void phase_e_handler(t30_state_t *f, void *user_data, int result)
204 const char *local_ident;
205 const char *far_ident;
207 fax_session *s = (fax_session *) user_data;
209 int pages_transferred;
211 ast_debug(1, "Fax phase E handler. result=%d\n", result);
213 t30_get_transfer_statistics(f, &stat);
215 s = (fax_session *) user_data;
217 if (result != T30_ERR_OK) {
220 /* FAXSTATUS is already set to FAILED */
221 pbx_builtin_setvar_helper(s->chan, "FAXERROR", t30_completion_code_to_str(result));
223 ast_log(LOG_WARNING, "Error transmitting fax. result=%d: %s.\n", result, t30_completion_code_to_str(result));
230 local_ident = t30_get_tx_ident(f);
231 far_ident = t30_get_rx_ident(f);
232 pbx_builtin_setvar_helper(s->chan, "FAXSTATUS", "SUCCESS");
233 pbx_builtin_setvar_helper(s->chan, "FAXERROR", NULL);
234 pbx_builtin_setvar_helper(s->chan, "REMOTESTATIONID", far_ident);
235 #if SPANDSP_RELEASE_DATE >= 20090220
236 pages_transferred = (s->direction) ? stat.pages_tx : stat.pages_rx;
238 pages_transferred = stat.pages_transferred;
240 snprintf(buf, sizeof(buf), "%d", pages_transferred);
241 pbx_builtin_setvar_helper(s->chan, "FAXPAGES", buf);
242 snprintf(buf, sizeof(buf), "%d", stat.y_resolution);
243 pbx_builtin_setvar_helper(s->chan, "FAXRESOLUTION", buf);
244 snprintf(buf, sizeof(buf), "%d", stat.bit_rate);
245 pbx_builtin_setvar_helper(s->chan, "FAXBITRATE", buf);
247 ast_debug(1, "Fax transmitted successfully.\n");
248 ast_debug(1, " Remote station ID: %s\n", far_ident);
249 ast_debug(1, " Pages transferred: %d\n", pages_transferred);
250 ast_debug(1, " Image resolution: %d x %d\n", stat.x_resolution, stat.y_resolution);
251 ast_debug(1, " Transfer Rate: %d\n", stat.bit_rate);
253 ast_manager_event(s->chan, EVENT_FLAG_CALL,
254 s->direction ? "FaxSent" : "FaxReceived",
258 "RemoteStationID: %s\r\n"
259 "LocalStationID: %s\r\n"
260 "PagesTransferred: %d\r\n"
262 "TransferRate: %d\r\n"
266 S_OR(s->chan->cid.cid_num, ""),
275 /* === Helper functions to configure fax === */
277 /* Setup SPAN logging according to Asterisk debug level */
278 static int set_logging(logging_state_t *state)
280 int level = SPAN_LOG_WARNING + option_debug;
282 span_log_set_message_handler(state, span_message);
283 span_log_set_level(state, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | level);
288 static void set_local_info(t30_state_t *state, fax_session *s)
292 x = pbx_builtin_getvar_helper(s->chan, "LOCALSTATIONID");
293 if (!ast_strlen_zero(x))
294 t30_set_tx_ident(state, x);
296 x = pbx_builtin_getvar_helper(s->chan, "LOCALHEADERINFO");
297 if (!ast_strlen_zero(x))
298 t30_set_tx_page_header_info(state, x);
301 static void set_file(t30_state_t *state, fax_session *s)
304 t30_set_tx_file(state, s->file_name, -1, -1);
306 t30_set_rx_file(state, s->file_name, -1);
309 static void set_ecm(t30_state_t *state, int ecm)
311 t30_set_ecm_capability(state, ecm);
312 t30_set_supported_compressions(state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
315 /* === Generator === */
317 /* This function is only needed to return passed params so
318 generator_activate will save it to channel's generatordata */
319 static void *fax_generator_alloc(struct ast_channel *chan, void *params)
324 static int fax_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
326 fax_state_t *fax = (fax_state_t*) data;
327 uint8_t buffer[AST_FRIENDLY_OFFSET + MAX_SAMPLES * sizeof(uint16_t)];
328 int16_t *buf = (int16_t *) (buffer + AST_FRIENDLY_OFFSET);
330 struct ast_frame outf = {
331 .frametype = AST_FRAME_VOICE,
332 .subclass.codec = AST_FORMAT_SLINEAR,
336 if (samples > MAX_SAMPLES) {
337 ast_log(LOG_WARNING, "Only generating %d samples, where %d requested\n", MAX_SAMPLES, samples);
338 samples = MAX_SAMPLES;
341 if ((len = fax_tx(fax, buf, samples)) > 0) {
343 AST_FRAME_SET_BUFFER(&outf, buffer, AST_FRIENDLY_OFFSET, len * sizeof(int16_t));
345 if (ast_write(chan, &outf) < 0) {
346 ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
354 static struct ast_generator generator = {
355 alloc: fax_generator_alloc,
356 generate: fax_generator_generate,
360 /* === Transmission === */
362 static int transmit_audio(fax_session *s)
365 int original_read_fmt = AST_FORMAT_SLINEAR;
366 int original_write_fmt = AST_FORMAT_SLINEAR;
368 t30_state_t *t30state;
369 struct ast_frame *inf = NULL;
371 struct timeval now, start, state_change;
372 enum ast_t38_state t38_state;
373 struct ast_control_t38_parameters t38_parameters = { .version = 0,
375 .rate = AST_T38_RATE_14400,
376 .rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF,
377 .fill_bit_removal = 1,
379 * spandsp has API calls to support MMR and JBIG transcoding, but they aren't
380 * implemented quite yet... so don't offer them to the remote endpoint
381 * .transcoding_mmr = 1,
382 * .transcoding_jbig = 1,
386 /* if in called party mode, try to use T.38 */
387 if (s->caller_mode == FALSE) {
388 /* check if we are already in T.38 mode (unlikely), or if we can request
389 * a switch... if so, request it now and wait for the result, rather
390 * than starting an audio FAX session that will have to be cancelled
392 if ((t38_state = ast_channel_get_t38_state(s->chan)) == T38_STATE_NEGOTIATED) {
394 } else if ((t38_state != T38_STATE_UNAVAILABLE) &&
395 (t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE,
396 (ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) == 0))) {
397 /* wait up to five seconds for negotiation to complete */
398 unsigned int timeout = 5000;
401 ast_debug(1, "Negotiating T.38 for receive on %s\n", s->chan->name);
402 while (timeout > 0) {
403 ms = ast_waitfor(s->chan, 1000);
405 ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", s->chan->name);
409 /* nothing happened */
414 ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", s->chan->name);
418 if (!(inf = ast_read(s->chan))) {
421 if ((inf->frametype == AST_FRAME_CONTROL) &&
422 (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
423 (inf->datalen == sizeof(t38_parameters))) {
424 struct ast_control_t38_parameters *parameters = inf->data.ptr;
426 switch (parameters->request_response) {
427 case AST_T38_NEGOTIATED:
428 ast_debug(1, "Negotiated T.38 for receive on %s\n", s->chan->name);
431 case AST_T38_REFUSED:
432 ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", s->chan->name);
435 ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", s->chan->name);
450 #if SPANDSP_RELEASE_DATE >= 20080725
451 /* for spandsp shaphots 0.0.6 and higher */
454 /* for spandsp release 0.0.5 */
455 t30state = &fax.t30_state;
458 original_read_fmt = s->chan->readformat;
459 if (original_read_fmt != AST_FORMAT_SLINEAR) {
460 res = ast_set_read_format(s->chan, AST_FORMAT_SLINEAR);
462 ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n");
467 original_write_fmt = s->chan->writeformat;
468 if (original_write_fmt != AST_FORMAT_SLINEAR) {
469 res = ast_set_write_format(s->chan, AST_FORMAT_SLINEAR);
471 ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n");
476 /* Initialize T30 terminal */
477 fax_init(&fax, s->caller_mode);
480 set_logging(&fax.logging);
481 set_logging(&t30state->logging);
483 /* Configure terminal */
484 set_local_info(t30state, s);
485 set_file(t30state, s);
486 set_ecm(t30state, TRUE);
488 fax_set_transmit_on_idle(&fax, TRUE);
490 t30_set_phase_e_handler(t30state, phase_e_handler, s);
492 start = state_change = ast_tvnow();
494 ast_activate_generator(s->chan, &generator, &fax);
496 while (!s->finished) {
499 if ((res = ast_waitfor(s->chan, 25)) < 0) {
500 ast_debug(1, "Error waiting for a frame\n");
506 if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
507 ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
513 /* There was timeout waiting for a frame. Loop around and wait again */
517 /* There is a frame available. Get it */
520 if (!(inf = ast_read(s->chan))) {
521 ast_debug(1, "Channel hangup\n");
526 ast_debug(10, "frame %d/%llu, len=%d\n", inf->frametype, (unsigned long long) inf->subclass.codec, inf->datalen);
528 /* Check the frame type. Format also must be checked because there is a chance
529 that a frame in old format was already queued before we set channel format
530 to slinear so it will still be received by ast_read */
531 if (inf->frametype == AST_FRAME_VOICE && inf->subclass.codec == AST_FORMAT_SLINEAR) {
532 if (fax_rx(&fax, inf->data.ptr, inf->samples) < 0) {
533 /* I know fax_rx never returns errors. The check here is for good style only */
534 ast_log(LOG_WARNING, "fax_rx returned error\n");
538 if (last_state != t30state->state) {
539 state_change = ast_tvnow();
540 last_state = t30state->state;
542 } else if ((inf->frametype == AST_FRAME_CONTROL) &&
543 (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS)) {
544 struct ast_control_t38_parameters *parameters = inf->data.ptr;
546 if (parameters->request_response == AST_T38_NEGOTIATED) {
547 /* T38 switchover completed */
548 s->t38parameters = *parameters;
549 ast_debug(1, "T38 negotiated, finishing audio loop\n");
552 } else if (parameters->request_response == AST_T38_REQUEST_NEGOTIATE) {
553 t38_parameters.request_response = AST_T38_NEGOTIATED;
554 ast_debug(1, "T38 request received, accepting\n");
555 /* Complete T38 switchover */
556 ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
557 /* Do not break audio loop, wait until channel driver finally acks switchover
558 * with AST_T38_NEGOTIATED
567 ast_debug(1, "Loop finished, res=%d\n", res);
572 ast_deactivate_generator(s->chan);
574 /* If we are switching to T38, remove phase E handler. Otherwise it will be executed
575 by t30_terminate, display diagnostics and set status variables although no transmittion
576 has taken place yet. */
578 t30_set_phase_e_handler(t30state, NULL, NULL);
581 t30_terminate(t30state);
585 if (original_write_fmt != AST_FORMAT_SLINEAR) {
586 if (ast_set_write_format(s->chan, original_write_fmt) < 0)
587 ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", s->chan->name);
590 if (original_read_fmt != AST_FORMAT_SLINEAR) {
591 if (ast_set_read_format(s->chan, original_read_fmt) < 0)
592 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", s->chan->name);
599 static int transmit_t38(fax_session *s)
602 t38_terminal_state_t t38;
603 struct ast_frame *inf = NULL;
605 struct timeval now, start, state_change, last_frame;
606 t30_state_t *t30state;
607 t38_core_state_t *t38state;
609 #if SPANDSP_RELEASE_DATE >= 20080725
610 /* for spandsp shaphots 0.0.6 and higher */
612 t38state = &t38.t38_fe.t38;
614 /* for spandsp releases 0.0.5 */
615 t30state = &t38.t30_state;
619 /* Initialize terminal */
620 memset(&t38, 0, sizeof(t38));
621 if (t38_terminal_init(&t38, s->caller_mode, t38_tx_packet_handler, s->chan) == NULL) {
622 ast_log(LOG_WARNING, "Unable to start T.38 termination.\n");
627 t38_set_max_datagram_size(t38state, s->t38parameters.max_ifp);
629 if (s->t38parameters.fill_bit_removal) {
630 t38_set_fill_bit_removal(t38state, TRUE);
632 if (s->t38parameters.transcoding_mmr) {
633 t38_set_mmr_transcoding(t38state, TRUE);
635 if (s->t38parameters.transcoding_jbig) {
636 t38_set_jbig_transcoding(t38state, TRUE);
640 set_logging(&t38.logging);
641 set_logging(&t30state->logging);
642 set_logging(&t38state->logging);
644 /* Configure terminal */
645 set_local_info(t30state, s);
646 set_file(t30state, s);
647 set_ecm(t30state, TRUE);
649 t30_set_phase_e_handler(t30state, phase_e_handler, s);
651 now = start = state_change = ast_tvnow();
653 while (!s->finished) {
656 if ((res = ast_waitfor(s->chan, 25)) < 0) {
657 ast_debug(1, "Error waiting for a frame\n");
665 if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
666 ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
671 t38_terminal_send_timeout(&t38, ast_tvdiff_us(now, last_frame) / (1000000 / 8000));
674 /* There was timeout waiting for a frame. Loop around and wait again */
678 /* There is a frame available. Get it */
681 if (!(inf = ast_read(s->chan))) {
682 ast_debug(1, "Channel hangup\n");
687 ast_debug(10, "frame %d/%d, len=%d\n", inf->frametype, inf->subclass.integer, inf->datalen);
689 if (inf->frametype == AST_FRAME_MODEM && inf->subclass.integer == AST_MODEM_T38) {
690 t38_core_rx_ifp_packet(t38state, inf->data.ptr, inf->datalen, inf->seqno);
691 if (last_state != t30state->state) {
692 state_change = ast_tvnow();
693 last_state = t30state->state;
695 } else if (inf->frametype == AST_FRAME_CONTROL && inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) {
696 struct ast_control_t38_parameters *parameters = inf->data.ptr;
697 if (parameters->request_response == AST_T38_TERMINATED) {
698 ast_debug(1, "T38 down, finishing\n");
707 ast_debug(1, "Loop finished, res=%d\n", res);
712 t30_terminate(t30state);
713 t38_terminal_release(&t38);
716 /* if we are not the caller, it's our job to shut down the T.38
717 * session when the FAX transmisson is complete.
719 if ((s->caller_mode == FALSE) &&
720 (ast_channel_get_t38_state(s->chan) == T38_STATE_NEGOTIATED)) {
721 struct ast_control_t38_parameters t38_parameters = { .request_response = AST_T38_REQUEST_TERMINATE, };
723 if (ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) == 0) {
724 /* wait up to five seconds for negotiation to complete */
725 unsigned int timeout = 5000;
728 ast_debug(1, "Shutting down T.38 on %s\n", s->chan->name);
729 while (timeout > 0) {
730 ms = ast_waitfor(s->chan, 1000);
732 ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", s->chan->name);
736 /* nothing happened */
741 ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 shutdown.\n", s->chan->name);
745 if (!(inf = ast_read(s->chan))) {
748 if ((inf->frametype == AST_FRAME_CONTROL) &&
749 (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
750 (inf->datalen == sizeof(t38_parameters))) {
751 struct ast_control_t38_parameters *parameters = inf->data.ptr;
753 switch (parameters->request_response) {
754 case AST_T38_TERMINATED:
755 ast_debug(1, "Shut down T.38 on %s\n", s->chan->name);
757 case AST_T38_REFUSED:
758 ast_log(LOG_WARNING, "channel '%s' refused to disable T.38\n", s->chan->name);
761 ast_log(LOG_ERROR, "channel '%s' failed to disable T.38\n", s->chan->name);
775 static int transmit(fax_session *s)
779 /* Clear all channel variables which to be set by the application.
780 Pre-set status to error so in case of any problems we can just leave */
781 pbx_builtin_setvar_helper(s->chan, "FAXSTATUS", "FAILED");
782 pbx_builtin_setvar_helper(s->chan, "FAXERROR", "Channel problems");
784 pbx_builtin_setvar_helper(s->chan, "FAXMODE", NULL);
785 pbx_builtin_setvar_helper(s->chan, "REMOTESTATIONID", NULL);
786 pbx_builtin_setvar_helper(s->chan, "FAXPAGES", "0");
787 pbx_builtin_setvar_helper(s->chan, "FAXRESOLUTION", NULL);
788 pbx_builtin_setvar_helper(s->chan, "FAXBITRATE", NULL);
790 if (s->chan->_state != AST_STATE_UP) {
791 /* Shouldn't need this, but checking to see if channel is already answered
792 * Theoretically asterisk should already have answered before running the app */
793 res = ast_answer(s->chan);
795 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", s->chan->name);
800 s->t38state = ast_channel_get_t38_state(s->chan);
801 if (s->t38state != T38_STATE_NEGOTIATED) {
802 /* T38 is not negotiated on the channel yet. First start regular transmission. If it switches to T38, follow */
803 pbx_builtin_setvar_helper(s->chan, "FAXMODE", "audio");
804 res = transmit_audio(s);
806 /* transmit_audio reports switchover to T38. Update t38state */
807 s->t38state = ast_channel_get_t38_state(s->chan);
808 if (s->t38state != T38_STATE_NEGOTIATED) {
809 ast_log(LOG_ERROR, "Audio loop reports T38 switchover but t38state != T38_STATE_NEGOTIATED\n");
814 if (s->t38state == T38_STATE_NEGOTIATED) {
815 pbx_builtin_setvar_helper(s->chan, "FAXMODE", "T38");
816 res = transmit_t38(s);
820 ast_log(LOG_WARNING, "Transmission error\n");
822 } else if (s->finished < 0) {
823 ast_log(LOG_WARNING, "Transmission failed\n");
824 } else if (s->finished > 0) {
825 ast_debug(1, "Transmission finished Ok\n");
831 /* === Application functions === */
833 static int sndfax_exec(struct ast_channel *chan, const char *data)
837 fax_session session = { 0, };
838 char restore_digit_detect = 0;
840 AST_DECLARE_APP_ARGS(args,
841 AST_APP_ARG(file_name);
842 AST_APP_ARG(options);
846 ast_log(LOG_ERROR, "Fax channel is NULL. Giving up.\n");
850 /* The next few lines of code parse out the filename and header from the input string */
851 if (ast_strlen_zero(data)) {
852 /* No data implies no filename or anything is present */
853 ast_log(LOG_ERROR, "SendFAX requires an argument (filename)\n");
857 parse = ast_strdupa(data);
858 AST_STANDARD_APP_ARGS(args, parse);
860 session.caller_mode = TRUE;
863 if (strchr(args.options, 'a'))
864 session.caller_mode = FALSE;
868 session.direction = 1;
869 session.file_name = args.file_name;
871 session.finished = 0;
873 /* get current digit detection mode, then disable digit detection if enabled */
875 int dummy = sizeof(restore_digit_detect);
877 ast_channel_queryoption(chan, AST_OPTION_DIGIT_DETECT, &restore_digit_detect, &dummy, 0);
880 if (restore_digit_detect) {
881 char new_digit_detect = 0;
883 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &new_digit_detect, sizeof(new_digit_detect), 0);
886 /* disable FAX tone detection if enabled */
888 char new_fax_detect = 0;
890 ast_channel_setoption(chan, AST_OPTION_FAX_DETECT, &new_fax_detect, sizeof(new_fax_detect), 0);
893 res = transmit(&session);
895 if (restore_digit_detect) {
896 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &restore_digit_detect, sizeof(restore_digit_detect), 0);
902 static int rcvfax_exec(struct ast_channel *chan, const char *data)
907 char restore_digit_detect = 0;
909 AST_DECLARE_APP_ARGS(args,
910 AST_APP_ARG(file_name);
911 AST_APP_ARG(options);
915 ast_log(LOG_ERROR, "Fax channel is NULL. Giving up.\n");
919 /* The next few lines of code parse out the filename and header from the input string */
920 if (ast_strlen_zero(data)) {
921 /* No data implies no filename or anything is present */
922 ast_log(LOG_ERROR, "ReceiveFAX requires an argument (filename)\n");
926 parse = ast_strdupa(data);
927 AST_STANDARD_APP_ARGS(args, parse);
929 session.caller_mode = FALSE;
932 if (strchr(args.options, 'c'))
933 session.caller_mode = TRUE;
937 session.direction = 0;
938 session.file_name = args.file_name;
940 session.finished = 0;
942 /* get current digit detection mode, then disable digit detection if enabled */
944 int dummy = sizeof(restore_digit_detect);
946 ast_channel_queryoption(chan, AST_OPTION_DIGIT_DETECT, &restore_digit_detect, &dummy, 0);
949 if (restore_digit_detect) {
950 char new_digit_detect = 0;
952 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &new_digit_detect, sizeof(new_digit_detect), 0);
955 /* disable FAX tone detection if enabled */
957 char new_fax_detect = 0;
959 ast_channel_setoption(chan, AST_OPTION_FAX_DETECT, &new_fax_detect, sizeof(new_fax_detect), 0);
962 res = transmit(&session);
964 if (restore_digit_detect) {
965 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &restore_digit_detect, sizeof(restore_digit_detect), 0);
971 static int unload_module(void)
975 res = ast_unregister_application(app_sndfax_name);
976 res |= ast_unregister_application(app_rcvfax_name);
981 static int load_module(void)
985 res = ast_register_application_xml(app_sndfax_name, sndfax_exec);
986 res |= ast_register_application_xml(app_rcvfax_name, rcvfax_exec);
988 /* The default SPAN message handler prints to stderr. It is something we do not want */
989 span_set_message_handler(NULL);
995 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Simple FAX Application",
997 .unload = unload_module,