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 <depend>spandsp</depend>
21 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
33 #include <spandsp/version.h>
35 #include "asterisk/lock.h"
36 #include "asterisk/file.h"
37 #include "asterisk/logger.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/app.h"
41 #include "asterisk/dsp.h"
42 #include "asterisk/module.h"
43 #include "asterisk/manager.h"
46 <application name="SendFAX" language="en_US">
51 <parameter name="filename" required="true">
52 <para>Filename of TIFF file to fax</para>
54 <parameter name="a" required="false">
55 <para>Makes the application behave as the answering machine</para>
56 <para>(Default behavior is as calling machine)</para>
60 <para>Send a given TIFF file to the channel as a FAX.</para>
61 <para>This application sets the following channel variables:</para>
63 <variable name="LOCALSTATIONID">
64 <para>To identify itself to the remote end</para>
66 <variable name="LOCALHEADERINFO">
67 <para>To generate a header line on each page</para>
69 <variable name="FAXSTATUS">
70 <value name="SUCCESS"/>
71 <value name="FAILED"/>
73 <variable name="FAXERROR">
74 <para>Cause of failure</para>
76 <variable name="REMOTESTATIONID">
77 <para>The CSID of the remote side</para>
79 <variable name="FAXPAGES">
80 <para>Number of pages sent</para>
82 <variable name="FAXBITRATE">
83 <para>Transmission rate</para>
85 <variable name="FAXRESOLUTION">
86 <para>Resolution of sent fax</para>
91 <application name="ReceiveFAX" language="en_US">
96 <parameter name="filename" required="true">
97 <para>Filename of TIFF file save incoming fax</para>
99 <parameter name="c" required="false">
100 <para>Makes the application behave as the calling machine</para>
101 <para>(Default behavior is as answering machine)</para>
105 <para>Receives a FAX from the channel into the given filename
106 overwriting the file if it already exists.</para>
107 <para>File created will be in TIFF format.</para>
109 <para>This application sets the following channel variables:</para>
111 <variable name="LOCALSTATIONID">
112 <para>To identify itself to the remote end</para>
114 <variable name="LOCALHEADERINFO">
115 <para>To generate a header line on each page</para>
117 <variable name="FAXSTATUS">
118 <value name="SUCCESS"/>
119 <value name="FAILED"/>
121 <variable name="FAXERROR">
122 <para>Cause of failure</para>
124 <variable name="REMOTESTATIONID">
125 <para>The CSID of the remote side</para>
127 <variable name="FAXPAGES">
128 <para>Number of pages sent</para>
130 <variable name="FAXBITRATE">
131 <para>Transmission rate</para>
133 <variable name="FAXRESOLUTION">
134 <para>Resolution of sent fax</para>
142 static const char app_sndfax_name[] = "SendFAX";
143 static const char app_rcvfax_name[] = "ReceiveFAX";
145 #define MAX_SAMPLES 240
147 /* Watchdog. I have seen situations when remote fax disconnects (because of poor line
148 quality) while SpanDSP continues staying in T30_STATE_IV_CTC state forever.
149 To avoid this, we terminate when we see that T30 state does not change for 5 minutes.
150 We also terminate application when more than 30 minutes passed regardless of
151 state changes. This is just a precaution measure - no fax should take that long */
153 #define WATCHDOG_TOTAL_TIMEOUT 30 * 60
154 #define WATCHDOG_STATE_TIMEOUT 5 * 60
157 struct ast_channel *chan;
158 enum ast_t38_state t38state; /* T38 state of the channel */
159 int direction; /* Fax direction: 0 - receiving, 1 - sending */
162 struct ast_control_t38_parameters t38parameters;
163 volatile int finished;
166 static void span_message(int level, const char *msg)
168 if (level == SPAN_LOG_ERROR) {
169 ast_log(LOG_ERROR, "%s", msg);
170 } else if (level == SPAN_LOG_WARNING) {
171 ast_log(LOG_WARNING, "%s", msg);
173 ast_log(LOG_DEBUG, "%s", msg);
177 static int t38_tx_packet_handler(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
179 struct ast_channel *chan = (struct ast_channel *) user_data;
181 struct ast_frame outf = {
182 .frametype = AST_FRAME_MODEM,
183 .subclass.integer = AST_MODEM_T38,
187 /* TODO: Asterisk does not provide means of resending the same packet multiple
188 times so count is ignored at the moment */
190 AST_FRAME_SET_BUFFER(&outf, buf, 0, len);
192 if (ast_write(chan, &outf) < 0) {
193 ast_log(LOG_WARNING, "Unable to write frame to channel; %s\n", strerror(errno));
200 static void phase_e_handler(t30_state_t *f, void *user_data, int result)
202 const char *local_ident;
203 const char *far_ident;
205 fax_session *s = (fax_session *) user_data;
207 int pages_transferred;
209 ast_debug(1, "Fax phase E handler. result=%d\n", result);
211 t30_get_transfer_statistics(f, &stat);
213 s = (fax_session *) user_data;
215 if (result != T30_ERR_OK) {
218 /* FAXSTATUS is already set to FAILED */
219 pbx_builtin_setvar_helper(s->chan, "FAXERROR", t30_completion_code_to_str(result));
221 ast_log(LOG_WARNING, "Error transmitting fax. result=%d: %s.\n", result, t30_completion_code_to_str(result));
228 local_ident = t30_get_tx_ident(f);
229 far_ident = t30_get_rx_ident(f);
230 pbx_builtin_setvar_helper(s->chan, "FAXSTATUS", "SUCCESS");
231 pbx_builtin_setvar_helper(s->chan, "FAXERROR", NULL);
232 pbx_builtin_setvar_helper(s->chan, "REMOTESTATIONID", far_ident);
233 #if SPANDSP_RELEASE_DATE >= 20090220
234 pages_transferred = (s->direction) ? stat.pages_tx : stat.pages_rx;
236 pages_transferred = stat.pages_transferred;
238 snprintf(buf, sizeof(buf), "%d", pages_transferred);
239 pbx_builtin_setvar_helper(s->chan, "FAXPAGES", buf);
240 snprintf(buf, sizeof(buf), "%d", stat.y_resolution);
241 pbx_builtin_setvar_helper(s->chan, "FAXRESOLUTION", buf);
242 snprintf(buf, sizeof(buf), "%d", stat.bit_rate);
243 pbx_builtin_setvar_helper(s->chan, "FAXBITRATE", buf);
245 ast_debug(1, "Fax transmitted successfully.\n");
246 ast_debug(1, " Remote station ID: %s\n", far_ident);
247 ast_debug(1, " Pages transferred: %d\n", pages_transferred);
248 ast_debug(1, " Image resolution: %d x %d\n", stat.x_resolution, stat.y_resolution);
249 ast_debug(1, " Transfer Rate: %d\n", stat.bit_rate);
251 ast_manager_event(s->chan, EVENT_FLAG_CALL,
252 s->direction ? "FaxSent" : "FaxReceived",
256 "RemoteStationID: %s\r\n"
257 "LocalStationID: %s\r\n"
258 "PagesTransferred: %d\r\n"
260 "TransferRate: %d\r\n"
264 S_OR(s->chan->cid.cid_num, ""),
273 /* === Helper functions to configure fax === */
275 /* Setup SPAN logging according to Asterisk debug level */
276 static int set_logging(logging_state_t *state)
278 int level = SPAN_LOG_WARNING + option_debug;
280 span_log_set_message_handler(state, span_message);
281 span_log_set_level(state, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | level);
286 static void set_local_info(t30_state_t *state, fax_session *s)
290 x = pbx_builtin_getvar_helper(s->chan, "LOCALSTATIONID");
291 if (!ast_strlen_zero(x))
292 t30_set_tx_ident(state, x);
294 x = pbx_builtin_getvar_helper(s->chan, "LOCALHEADERINFO");
295 if (!ast_strlen_zero(x))
296 t30_set_tx_page_header_info(state, x);
299 static void set_file(t30_state_t *state, fax_session *s)
302 t30_set_tx_file(state, s->file_name, -1, -1);
304 t30_set_rx_file(state, s->file_name, -1);
307 static void set_ecm(t30_state_t *state, int ecm)
309 t30_set_ecm_capability(state, ecm);
310 t30_set_supported_compressions(state, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
313 /* === Generator === */
315 /* This function is only needed to return passed params so
316 generator_activate will save it to channel's generatordata */
317 static void *fax_generator_alloc(struct ast_channel *chan, void *params)
322 static int fax_generator_generate(struct ast_channel *chan, void *data, int len, int samples)
324 fax_state_t *fax = (fax_state_t*) data;
325 uint8_t buffer[AST_FRIENDLY_OFFSET + MAX_SAMPLES * sizeof(uint16_t)];
326 int16_t *buf = (int16_t *) (buffer + AST_FRIENDLY_OFFSET);
328 struct ast_frame outf = {
329 .frametype = AST_FRAME_VOICE,
330 .subclass.codec = AST_FORMAT_SLINEAR,
334 if (samples > MAX_SAMPLES) {
335 ast_log(LOG_WARNING, "Only generating %d samples, where %d requested\n", MAX_SAMPLES, samples);
336 samples = MAX_SAMPLES;
339 if ((len = fax_tx(fax, buf, samples)) > 0) {
341 AST_FRAME_SET_BUFFER(&outf, buffer, AST_FRIENDLY_OFFSET, len * sizeof(int16_t));
343 if (ast_write(chan, &outf) < 0) {
344 ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror(errno));
352 static struct ast_generator generator = {
353 alloc: fax_generator_alloc,
354 generate: fax_generator_generate,
358 /* === Transmission === */
360 static int transmit_audio(fax_session *s)
363 int original_read_fmt = AST_FORMAT_SLINEAR;
364 int original_write_fmt = AST_FORMAT_SLINEAR;
366 t30_state_t *t30state;
367 struct ast_frame *inf = NULL;
369 struct timeval now, start, state_change;
370 enum ast_t38_state t38_state;
371 struct ast_control_t38_parameters t38_parameters = { .version = 0,
373 .rate = AST_T38_RATE_14400,
374 .rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF,
375 .fill_bit_removal = 1,
376 .transcoding_mmr = 1,
377 .transcoding_jbig = 1,
380 /* if in called party mode, try to use T.38 */
381 if (s->caller_mode == FALSE) {
382 /* check if we are already in T.38 mode (unlikely), or if we can request
383 * a switch... if so, request it now and wait for the result, rather
384 * than starting an audio FAX session that will have to be cancelled
386 if ((t38_state = ast_channel_get_t38_state(s->chan)) == T38_STATE_NEGOTIATED) {
388 } else if ((t38_state != T38_STATE_UNAVAILABLE) &&
389 (t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE,
390 (ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) == 0))) {
391 /* wait up to five seconds for negotiation to complete */
392 unsigned int timeout = 5000;
395 ast_debug(1, "Negotiating T.38 for receive on %s\n", s->chan->name);
396 while (timeout > 0) {
397 ms = ast_waitfor(s->chan, 1000);
399 ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", s->chan->name);
403 /* nothing happened */
408 ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", s->chan->name);
412 if (!(inf = ast_read(s->chan))) {
415 if ((inf->frametype == AST_FRAME_CONTROL) &&
416 (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
417 (inf->datalen == sizeof(t38_parameters))) {
418 struct ast_control_t38_parameters *parameters = inf->data.ptr;
420 switch (parameters->request_response) {
421 case AST_T38_NEGOTIATED:
422 ast_debug(1, "Negotiated T.38 for receive on %s\n", s->chan->name);
425 case AST_T38_REFUSED:
426 ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", s->chan->name);
429 ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", s->chan->name);
444 #if SPANDSP_RELEASE_DATE >= 20080725
445 /* for spandsp shaphots 0.0.6 and higher */
448 /* for spandsp release 0.0.5 */
449 t30state = &fax.t30_state;
452 original_read_fmt = s->chan->readformat;
453 if (original_read_fmt != AST_FORMAT_SLINEAR) {
454 res = ast_set_read_format(s->chan, AST_FORMAT_SLINEAR);
456 ast_log(LOG_WARNING, "Unable to set to linear read mode, giving up\n");
461 original_write_fmt = s->chan->writeformat;
462 if (original_write_fmt != AST_FORMAT_SLINEAR) {
463 res = ast_set_write_format(s->chan, AST_FORMAT_SLINEAR);
465 ast_log(LOG_WARNING, "Unable to set to linear write mode, giving up\n");
470 /* Initialize T30 terminal */
471 fax_init(&fax, s->caller_mode);
474 set_logging(&fax.logging);
475 set_logging(&t30state->logging);
477 /* Configure terminal */
478 set_local_info(t30state, s);
479 set_file(t30state, s);
480 set_ecm(t30state, TRUE);
482 fax_set_transmit_on_idle(&fax, TRUE);
484 t30_set_phase_e_handler(t30state, phase_e_handler, s);
486 start = state_change = ast_tvnow();
488 ast_activate_generator(s->chan, &generator, &fax);
490 while (!s->finished) {
493 if ((res = ast_waitfor(s->chan, 25)) < 0) {
494 ast_debug(1, "Error waiting for a frame\n");
500 if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
501 ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
507 /* There was timeout waiting for a frame. Loop around and wait again */
511 /* There is a frame available. Get it */
514 if (!(inf = ast_read(s->chan))) {
515 ast_debug(1, "Channel hangup\n");
520 ast_debug(10, "frame %d/%llu, len=%d\n", inf->frametype, (unsigned long long) inf->subclass.codec, inf->datalen);
522 /* Check the frame type. Format also must be checked because there is a chance
523 that a frame in old format was already queued before we set channel format
524 to slinear so it will still be received by ast_read */
525 if (inf->frametype == AST_FRAME_VOICE && inf->subclass.codec == AST_FORMAT_SLINEAR) {
526 if (fax_rx(&fax, inf->data.ptr, inf->samples) < 0) {
527 /* I know fax_rx never returns errors. The check here is for good style only */
528 ast_log(LOG_WARNING, "fax_rx returned error\n");
532 if (last_state != t30state->state) {
533 state_change = ast_tvnow();
534 last_state = t30state->state;
536 } else if ((inf->frametype == AST_FRAME_CONTROL) &&
537 (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS)) {
538 struct ast_control_t38_parameters *parameters = inf->data.ptr;
540 if (parameters->request_response == AST_T38_NEGOTIATED) {
541 /* T38 switchover completed */
542 s->t38parameters = *parameters;
543 ast_debug(1, "T38 negotiated, finishing audio loop\n");
546 } else if (parameters->request_response == AST_T38_REQUEST_NEGOTIATE) {
547 t38_parameters.request_response = AST_T38_NEGOTIATED;
548 ast_debug(1, "T38 request received, accepting\n");
549 /* Complete T38 switchover */
550 ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
551 /* Do not break audio loop, wait until channel driver finally acks switchover
552 * with AST_T38_NEGOTIATED
560 ast_debug(1, "Loop finished, res=%d\n", res);
565 ast_deactivate_generator(s->chan);
567 /* If we are switching to T38, remove phase E handler. Otherwise it will be executed
568 by t30_terminate, display diagnostics and set status variables although no transmittion
569 has taken place yet. */
571 t30_set_phase_e_handler(t30state, NULL, NULL);
574 t30_terminate(t30state);
578 if (original_write_fmt != AST_FORMAT_SLINEAR) {
579 if (ast_set_write_format(s->chan, original_write_fmt) < 0)
580 ast_log(LOG_WARNING, "Unable to restore write format on '%s'\n", s->chan->name);
583 if (original_read_fmt != AST_FORMAT_SLINEAR) {
584 if (ast_set_read_format(s->chan, original_read_fmt) < 0)
585 ast_log(LOG_WARNING, "Unable to restore read format on '%s'\n", s->chan->name);
592 static int transmit_t38(fax_session *s)
595 t38_terminal_state_t t38;
596 struct ast_frame *inf = NULL;
598 struct timeval now, start, state_change, last_frame;
599 t30_state_t *t30state;
600 t38_core_state_t *t38state;
602 #if SPANDSP_RELEASE_DATE >= 20080725
603 /* for spandsp shaphots 0.0.6 and higher */
605 t38state = &t38.t38_fe.t38;
607 /* for spandsp releases 0.0.5 */
608 t30state = &t38.t30_state;
612 /* Initialize terminal */
613 memset(&t38, 0, sizeof(t38));
614 if (t38_terminal_init(&t38, s->caller_mode, t38_tx_packet_handler, s->chan) == NULL) {
615 ast_log(LOG_WARNING, "Unable to start T.38 termination.\n");
620 t38_set_max_datagram_size(t38state, s->t38parameters.max_ifp);
622 if (s->t38parameters.fill_bit_removal) {
623 t38_set_fill_bit_removal(t38state, TRUE);
625 if (s->t38parameters.transcoding_mmr) {
626 t38_set_mmr_transcoding(t38state, TRUE);
628 if (s->t38parameters.transcoding_jbig) {
629 t38_set_jbig_transcoding(t38state, TRUE);
633 set_logging(&t38.logging);
634 set_logging(&t30state->logging);
635 set_logging(&t38state->logging);
637 /* Configure terminal */
638 set_local_info(t30state, s);
639 set_file(t30state, s);
640 set_ecm(t30state, TRUE);
642 t30_set_phase_e_handler(t30state, phase_e_handler, s);
644 now = start = state_change = ast_tvnow();
646 while (!s->finished) {
649 if ((res = ast_waitfor(s->chan, 25)) < 0) {
650 ast_debug(1, "Error waiting for a frame\n");
658 if (ast_tvdiff_sec(now, start) > WATCHDOG_TOTAL_TIMEOUT || ast_tvdiff_sec(now, state_change) > WATCHDOG_STATE_TIMEOUT) {
659 ast_log(LOG_WARNING, "It looks like we hung. Aborting.\n");
664 t38_terminal_send_timeout(&t38, ast_tvdiff_us(now, last_frame) / (1000000 / 8000));
667 /* There was timeout waiting for a frame. Loop around and wait again */
671 /* There is a frame available. Get it */
674 if (!(inf = ast_read(s->chan))) {
675 ast_debug(1, "Channel hangup\n");
680 ast_debug(10, "frame %d/%d, len=%d\n", inf->frametype, inf->subclass.integer, inf->datalen);
682 if (inf->frametype == AST_FRAME_MODEM && inf->subclass.integer == AST_MODEM_T38) {
683 t38_core_rx_ifp_packet(t38state, inf->data.ptr, inf->datalen, inf->seqno);
684 if (last_state != t30state->state) {
685 state_change = ast_tvnow();
686 last_state = t30state->state;
688 } else if (inf->frametype == AST_FRAME_CONTROL && inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) {
689 struct ast_control_t38_parameters *parameters = inf->data.ptr;
690 if (parameters->request_response == AST_T38_TERMINATED) {
691 ast_debug(1, "T38 down, finishing\n");
699 ast_debug(1, "Loop finished, res=%d\n", res);
704 t30_terminate(t30state);
705 t38_terminal_release(&t38);
708 /* if we are not the caller, it's our job to shut down the T.38
709 * session when the FAX transmisson is complete.
711 if ((s->caller_mode == FALSE) &&
712 (ast_channel_get_t38_state(s->chan) == T38_STATE_NEGOTIATED)) {
713 struct ast_control_t38_parameters t38_parameters = { .request_response = AST_T38_REQUEST_TERMINATE, };
715 if (ast_indicate_data(s->chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) == 0) {
716 /* wait up to five seconds for negotiation to complete */
717 unsigned int timeout = 5000;
720 ast_debug(1, "Shutting down T.38 on %s\n", s->chan->name);
721 while (timeout > 0) {
722 ms = ast_waitfor(s->chan, 1000);
724 ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", s->chan->name);
728 /* nothing happened */
733 ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 shutdown.\n", s->chan->name);
737 if (!(inf = ast_read(s->chan))) {
740 if ((inf->frametype == AST_FRAME_CONTROL) &&
741 (inf->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
742 (inf->datalen == sizeof(t38_parameters))) {
743 struct ast_control_t38_parameters *parameters = inf->data.ptr;
745 switch (parameters->request_response) {
746 case AST_T38_TERMINATED:
747 ast_debug(1, "Shut down T.38 on %s\n", s->chan->name);
749 case AST_T38_REFUSED:
750 ast_log(LOG_WARNING, "channel '%s' refused to disable T.38\n", s->chan->name);
753 ast_log(LOG_ERROR, "channel '%s' failed to disable T.38\n", s->chan->name);
767 static int transmit(fax_session *s)
771 /* Clear all channel variables which to be set by the application.
772 Pre-set status to error so in case of any problems we can just leave */
773 pbx_builtin_setvar_helper(s->chan, "FAXSTATUS", "FAILED");
774 pbx_builtin_setvar_helper(s->chan, "FAXERROR", "Channel problems");
776 pbx_builtin_setvar_helper(s->chan, "FAXMODE", NULL);
777 pbx_builtin_setvar_helper(s->chan, "REMOTESTATIONID", NULL);
778 pbx_builtin_setvar_helper(s->chan, "FAXPAGES", "0");
779 pbx_builtin_setvar_helper(s->chan, "FAXRESOLUTION", NULL);
780 pbx_builtin_setvar_helper(s->chan, "FAXBITRATE", NULL);
782 if (s->chan->_state != AST_STATE_UP) {
783 /* Shouldn't need this, but checking to see if channel is already answered
784 * Theoretically asterisk should already have answered before running the app */
785 res = ast_answer(s->chan);
787 ast_log(LOG_WARNING, "Could not answer channel '%s'\n", s->chan->name);
792 s->t38state = ast_channel_get_t38_state(s->chan);
793 if (s->t38state != T38_STATE_NEGOTIATED) {
794 /* T38 is not negotiated on the channel yet. First start regular transmission. If it switches to T38, follow */
795 pbx_builtin_setvar_helper(s->chan, "FAXMODE", "audio");
796 res = transmit_audio(s);
798 /* transmit_audio reports switchover to T38. Update t38state */
799 s->t38state = ast_channel_get_t38_state(s->chan);
800 if (s->t38state != T38_STATE_NEGOTIATED) {
801 ast_log(LOG_ERROR, "Audio loop reports T38 switchover but t38state != T38_STATE_NEGOTIATED\n");
806 if (s->t38state == T38_STATE_NEGOTIATED) {
807 pbx_builtin_setvar_helper(s->chan, "FAXMODE", "T38");
808 res = transmit_t38(s);
812 ast_log(LOG_WARNING, "Transmission error\n");
814 } else if (s->finished < 0) {
815 ast_log(LOG_WARNING, "Transmission failed\n");
816 } else if (s->finished > 0) {
817 ast_debug(1, "Transmission finished Ok\n");
823 /* === Application functions === */
825 static int sndfax_exec(struct ast_channel *chan, const char *data)
829 fax_session session = { 0, };
830 char restore_digit_detect = 0;
832 AST_DECLARE_APP_ARGS(args,
833 AST_APP_ARG(file_name);
834 AST_APP_ARG(options);
838 ast_log(LOG_ERROR, "Fax channel is NULL. Giving up.\n");
842 /* The next few lines of code parse out the filename and header from the input string */
843 if (ast_strlen_zero(data)) {
844 /* No data implies no filename or anything is present */
845 ast_log(LOG_ERROR, "SendFAX requires an argument (filename)\n");
849 parse = ast_strdupa(data);
850 AST_STANDARD_APP_ARGS(args, parse);
852 session.caller_mode = TRUE;
855 if (strchr(args.options, 'a'))
856 session.caller_mode = FALSE;
860 session.direction = 1;
861 session.file_name = args.file_name;
863 session.finished = 0;
865 /* get current digit detection mode, then disable digit detection if enabled */
867 int dummy = sizeof(restore_digit_detect);
869 ast_channel_queryoption(chan, AST_OPTION_DIGIT_DETECT, &restore_digit_detect, &dummy, 0);
872 if (restore_digit_detect) {
873 char new_digit_detect = 0;
875 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &new_digit_detect, sizeof(new_digit_detect), 0);
878 /* disable FAX tone detection if enabled */
880 char new_fax_detect = 0;
882 ast_channel_setoption(chan, AST_OPTION_FAX_DETECT, &new_fax_detect, sizeof(new_fax_detect), 0);
885 res = transmit(&session);
887 if (restore_digit_detect) {
888 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &restore_digit_detect, sizeof(restore_digit_detect), 0);
894 static int rcvfax_exec(struct ast_channel *chan, const char *data)
899 char restore_digit_detect = 0;
901 AST_DECLARE_APP_ARGS(args,
902 AST_APP_ARG(file_name);
903 AST_APP_ARG(options);
907 ast_log(LOG_ERROR, "Fax channel is NULL. Giving up.\n");
911 /* The next few lines of code parse out the filename and header from the input string */
912 if (ast_strlen_zero(data)) {
913 /* No data implies no filename or anything is present */
914 ast_log(LOG_ERROR, "ReceiveFAX requires an argument (filename)\n");
918 parse = ast_strdupa(data);
919 AST_STANDARD_APP_ARGS(args, parse);
921 session.caller_mode = FALSE;
924 if (strchr(args.options, 'c'))
925 session.caller_mode = TRUE;
929 session.direction = 0;
930 session.file_name = args.file_name;
932 session.finished = 0;
934 /* get current digit detection mode, then disable digit detection if enabled */
936 int dummy = sizeof(restore_digit_detect);
938 ast_channel_queryoption(chan, AST_OPTION_DIGIT_DETECT, &restore_digit_detect, &dummy, 0);
941 if (restore_digit_detect) {
942 char new_digit_detect = 0;
944 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &new_digit_detect, sizeof(new_digit_detect), 0);
947 /* disable FAX tone detection if enabled */
949 char new_fax_detect = 0;
951 ast_channel_setoption(chan, AST_OPTION_FAX_DETECT, &new_fax_detect, sizeof(new_fax_detect), 0);
954 res = transmit(&session);
956 if (restore_digit_detect) {
957 ast_channel_setoption(chan, AST_OPTION_DIGIT_DETECT, &restore_digit_detect, sizeof(restore_digit_detect), 0);
963 static int unload_module(void)
967 res = ast_unregister_application(app_sndfax_name);
968 res |= ast_unregister_application(app_rcvfax_name);
973 static int load_module(void)
977 res = ast_register_application_xml(app_sndfax_name, sndfax_exec);
978 res |= ast_register_application_xml(app_rcvfax_name, rcvfax_exec);
980 /* The default SPAN message handler prints to stderr. It is something we do not want */
981 span_set_message_handler(NULL);
987 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Simple FAX Application",
989 .unload = unload_module,