2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008-2009, Digium, Inc.
6 * Dwayne M. Hubbard <dhubbard@digium.com>
7 * Kevin P. Fleming <kpfleming@digium.com>
8 * Matthew Nicholson <mnicholson@digium.com>
10 * Initial T.38-gateway code
11 * 2008, Daniel Ferenci <daniel.ferenci@nethemba.com>
12 * Created by Nethemba s.r.o. http://www.nethemba.com
13 * Sponsored by IPEX a.s. http://www.ipex.cz
15 * T.38-gateway integration into asterisk app_fax and rework
16 * 2008-2011, Gregory Hinton Nietsky <gregory@distrotech.co.za>
17 * dns Telecom http://www.dnstelecom.co.za
19 * Modified to make T.38-gateway compatible with Asterisk 1.6.2
20 * 2010, Anton Verevkin <mymail@verevkin.it>
21 * ViaNetTV http://www.vianettv.com
23 * Modified to make T.38-gateway work
24 * 2010, Klaus Darilion, IPCom GmbH, www.ipcom.at
26 * See http://www.asterisk.org for more information about
27 * the Asterisk project. Please do not directly contact
28 * any of the maintainers of this project for assistance;
29 * the project provides a web site, mailing lists and IRC
30 * channels for your use.
32 * This program is free software, distributed under the terms of
33 * the GNU General Public License Version 2. See the LICENSE file
34 * at the top of the source tree.
38 <conflict>app_fax</conflict>
43 * \brief Generic FAX Resource for FAX technology resource modules
45 * \author Dwayne M. Hubbard <dhubbard@digium.com>
46 * \author Kevin P. Fleming <kpfleming@digium.com>
47 * \author Matthew Nicholson <mnicholson@digium.com>
48 * \author Gregory H. Nietsky <gregory@distrotech.co.za>
50 * A generic FAX resource module that provides SendFAX and ReceiveFAX applications.
51 * This module requires FAX technology modules, like res_fax_spandsp, to register with it
52 * so it can use the technology modules to perform the actual FAX transmissions.
53 * \ingroup applications
57 <support_level>core</support_level>
62 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
64 #include "asterisk/io.h"
65 #include "asterisk/file.h"
66 #include "asterisk/logger.h"
67 #include "asterisk/module.h"
68 #include "asterisk/app.h"
69 #include "asterisk/lock.h"
70 #include "asterisk/options.h"
71 #include "asterisk/strings.h"
72 #include "asterisk/cli.h"
73 #include "asterisk/utils.h"
74 #include "asterisk/config.h"
75 #include "asterisk/astobj2.h"
76 #include "asterisk/res_fax.h"
77 #include "asterisk/file.h"
78 #include "asterisk/channel.h"
79 #include "asterisk/pbx.h"
80 #include "asterisk/manager.h"
81 #include "asterisk/dsp.h"
82 #include "asterisk/indications.h"
83 #include "asterisk/ast_version.h"
84 #include "asterisk/translate.h"
87 <application name="ReceiveFax" language="en_US">
89 Receive a FAX and save as a TIFF/F file.
92 <parameter name="filename" required="true" />
93 <parameter name="options">
96 <para>Enable FAX debugging.</para>
99 <para>Allow audio fallback FAX transfer on T.38 capable channels.</para>
102 <para>Force usage of audio mode on T.38 capable channels.</para>
105 <para>Send progress Manager events (overrides statusevents setting in res_fax.conf).</para>
111 <para>This application is provided by res_fax, which is a FAX technology agnostic module
112 that utilizes FAX technology resource modules to complete a FAX transmission.</para>
113 <para>Session arguments can be set by the FAXOPT function and to check results of the ReceiveFax() application.</para>
116 <ref type="function">FAXOPT</ref>
119 <application name="SendFax" language="en_US">
121 Sends a specified TIFF/F file as a FAX.
124 <parameter name="filename" required="true" argsep="&">
125 <argument name="filename2" multiple="true">
126 <para>TIFF file to send as a FAX.</para>
129 <parameter name="options">
132 <para>Enable FAX debugging.</para>
135 <para>Allow audio fallback FAX transfer on T.38 capable channels.</para>
138 <para>Force usage of audio mode on T.38 capable channels.</para>
141 <para>Send progress Manager events (overrides statusevents setting in res_fax.conf).</para>
144 <para>Initiate a T.38 reinvite on the channel if the remote end does not.</para>
150 <para>This application is provided by res_fax, which is a FAX technology agnostic module
151 that utilizes FAX technology resource modules to complete a FAX transmission.</para>
152 <para>Session arguments can be set by the FAXOPT function and to check results of the SendFax() application.</para>
155 <ref type="function">FAXOPT</ref>
158 <function name="FAXOPT" language="en_US">
160 Gets/sets various pieces of information about a fax session.
163 <parameter name="item" required="true">
166 <para>R/W Error Correction Mode (ECM) enable with 'yes', disable with 'no'.</para>
169 <para>R/O FAX transmission error code upon failure.</para>
171 <enum name="filename">
172 <para>R/O Filename of the first file of the FAX transmission.</para>
174 <enum name="filenames">
175 <para>R/O Filenames of all of the files in the FAX transmission (comma separated).</para>
177 <enum name="headerinfo">
178 <para>R/W FAX header information.</para>
180 <enum name="localstationid">
181 <para>R/W Local Station Identification.</para>
183 <enum name="minrate">
184 <para>R/W Minimum transfer rate set before transmission.</para>
186 <enum name="maxrate">
187 <para>R/W Maximum transfer rate set before transmission.</para>
190 <para>R/W Modem type (v17/v27/v29).</para>
192 <enum name="gateway">
193 <para>R/W T38 fax gateway, with optional fax activity timeout in seconds (yes[,timeout]/no)</para>
196 <para>R/O Number of pages transferred.</para>
199 <para>R/O Negotiated transmission rate.</para>
201 <enum name="remotestationid">
202 <para>R/O Remote Station Identification after transmission.</para>
204 <enum name="resolution">
205 <para>R/O Negotiated image resolution after transmission.</para>
207 <enum name="sessionid">
208 <para>R/O Session ID of the FAX transmission.</para>
211 <para>R/O Result Status of the FAX transmission.</para>
213 <enum name="statusstr">
214 <para>R/O Verbose Result Status of the FAX transmission.</para>
220 <para>FAXOPT can be used to override the settings for a FAX session listed in <filename>res_fax.conf</filename>,
221 it can also be used to retreive information about a FAX session that has finished eg. pages/status.</para>
224 <ref type="application">ReceiveFax</ref>
225 <ref type="application">SendFax</ref>
230 static const char app_receivefax[] = "ReceiveFAX";
231 static const char app_sendfax[] = "SendFAX";
233 struct debug_info_history {
234 unsigned int consec_frames;
235 unsigned int consec_ms;
236 unsigned char silence;
239 struct ast_fax_debug_info {
240 struct timeval base_tv;
241 struct debug_info_history c2s, s2c;
245 /*! \brief used for gateway framehook */
247 /*! \brief FAX Session */
248 struct ast_fax_session *s;
249 /*! \brief reserved fax session token */
250 struct ast_fax_tech_token *token;
251 /*! \brief the start of our timeout counter */
252 struct timeval timeout_start;
253 /*! \brief DSP Processor */
254 struct ast_dsp *chan_dsp;
255 struct ast_dsp *peer_dsp;
256 /*! \brief framehook used in gateway mode */
258 /*! \brief bridged */
260 /*! \brief 1 if a v21 preamble has been detected */
262 /*! \brief a flag to track the state of our negotiation */
263 enum ast_t38_state t38_state;
264 /*! \brief original audio formats */
265 struct ast_format chan_read_format;
266 struct ast_format chan_write_format;
267 struct ast_format peer_read_format;
268 struct ast_format peer_write_format;
271 static int fax_logger_level = -1;
273 /*! \brief maximum buckets for res_fax ao2 containers */
274 #define FAX_MAXBUCKETS 10
276 #define RES_FAX_TIMEOUT 10000
277 #define FAX_GATEWAY_TIMEOUT RES_FAX_TIMEOUT
279 /*! \brief The faxregistry is used to manage information and statistics for all FAX sessions. */
281 /*! The number of active FAX sessions */
283 /*! The number of reserved FAX sessions */
284 int reserved_sessions;
285 /*! active sessions are astobj2 objects */
286 struct ao2_container *container;
287 /*! Total number of Tx FAX attempts */
289 /*! Total number of Rx FAX attempts */
291 /*! Number of successful FAX transmissions */
293 /*! Number of failed FAX transmissions */
295 /*! the next unique session name */
299 /*! \brief registered FAX technology modules are put into this list */
301 const struct ast_fax_tech *tech;
302 AST_RWLIST_ENTRY(fax_module) list;
304 static AST_RWLIST_HEAD_STATIC(faxmodules, fax_module);
306 #define RES_FAX_MINRATE 2400
307 #define RES_FAX_MAXRATE 14400
308 #define RES_FAX_STATUSEVENTS 0
309 #define RES_FAX_MODEM (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V27 | AST_FAX_MODEM_V29)
312 enum ast_fax_modems modems;
313 uint32_t statusevents:1;
315 unsigned int minrate;
316 unsigned int maxrate;
319 static const char *config = "res_fax.conf";
321 static int global_fax_debug = 0;
324 OPT_CALLEDMODE = (1 << 0),
325 OPT_CALLERMODE = (1 << 1),
326 OPT_DEBUG = (1 << 2),
327 OPT_STATUS = (1 << 3),
328 OPT_ALLOWAUDIO = (1 << 5),
329 OPT_REQUEST_T38 = (1 << 6),
330 OPT_FORCE_AUDIO = (1 << 7),
333 AST_APP_OPTIONS(fax_exec_options, BEGIN_OPTIONS
334 AST_APP_OPTION('a', OPT_CALLEDMODE),
335 AST_APP_OPTION('c', OPT_CALLERMODE),
336 AST_APP_OPTION('d', OPT_DEBUG),
337 AST_APP_OPTION('f', OPT_ALLOWAUDIO),
338 AST_APP_OPTION('F', OPT_FORCE_AUDIO),
339 AST_APP_OPTION('s', OPT_STATUS),
340 AST_APP_OPTION('z', OPT_REQUEST_T38),
343 struct manager_event_info {
344 char context[AST_MAX_CONTEXT];
345 char exten[AST_MAX_EXTENSION];
349 static void debug_check_frame_for_silence(struct ast_fax_session *s, unsigned int c2s, struct ast_frame *frame)
351 struct debug_info_history *history = c2s ? &s->debug_info->c2s : &s->debug_info->s2c;
353 unsigned int last_consec_frames, last_consec_ms;
354 unsigned char wassil;
357 diff = ast_tvsub(ast_tvnow(), s->debug_info->base_tv);
359 ast_dsp_reset(s->debug_info->dsp);
360 ast_dsp_silence(s->debug_info->dsp, frame, &dspsilence);
362 wassil = history->silence;
363 history->silence = (dspsilence != 0) ? 1 : 0;
364 if (history->silence != wassil) {
365 last_consec_frames = history->consec_frames;
366 last_consec_ms = history->consec_ms;
367 history->consec_frames = 0;
368 history->consec_ms = 0;
370 if ((last_consec_frames != 0)) {
371 ast_verb(6, "Channel '%s' fax session '%d', [ %.3ld.%.6ld ], %s sent %d frames (%d ms) of %s.\n",
372 s->channame, s->id, (long) diff.tv_sec, (long int) diff.tv_usec,
373 (c2s) ? "channel" : "stack", last_consec_frames, last_consec_ms,
374 (wassil) ? "silence" : "energy");
378 history->consec_frames++;
379 history->consec_ms += (frame->samples / 8);
382 static void destroy_callback(void *data)
389 static const struct ast_datastore_info fax_datastore = {
391 .destroy = destroy_callback,
394 /*! \brief returns a reference counted pointer to a fax datastore, if it exists */
395 static struct ast_fax_session_details *find_details(struct ast_channel *chan)
397 struct ast_fax_session_details *details;
398 struct ast_datastore *datastore;
400 ast_channel_lock(chan);
401 if (!(datastore = ast_channel_datastore_find(chan, &fax_datastore, NULL))) {
402 ast_channel_unlock(chan);
405 if (!(details = datastore->data)) {
406 ast_log(LOG_WARNING, "Huh? channel '%s' has a FAX datastore without data!\n", chan->name);
407 ast_channel_unlock(chan);
411 ast_channel_unlock(chan);
416 /*! \brief destroy a FAX session details structure */
417 static void destroy_session_details(void *details)
419 struct ast_fax_session_details *d = details;
420 struct ast_fax_document *doc;
422 while ((doc = AST_LIST_REMOVE_HEAD(&d->documents, next))) {
425 ast_string_field_free_memory(d);
428 /*! \brief create a FAX session details structure */
429 static struct ast_fax_session_details *session_details_new(void)
431 struct ast_fax_session_details *d;
433 if (!(d = ao2_alloc(sizeof(*d), destroy_session_details))) {
437 if (ast_string_field_init(d, 512)) {
442 AST_LIST_HEAD_INIT_NOLOCK(&d->documents);
444 /* These options need to be set to the configured default and may be overridden by
445 * SendFAX, ReceiveFAX, or FAXOPT */
446 d->option.request_t38 = AST_FAX_OPTFLAG_FALSE;
447 d->option.send_cng = AST_FAX_OPTFLAG_FALSE;
448 d->option.send_ced = AST_FAX_OPTFLAG_FALSE;
449 d->option.ecm = general_options.ecm;
450 d->option.statusevents = general_options.statusevents;
451 d->modems = general_options.modems;
452 d->minrate = general_options.minrate;
453 d->maxrate = general_options.maxrate;
455 d->gateway_timeout = 0;
460 static struct ast_control_t38_parameters our_t38_parameters = {
463 .rate = AST_T38_RATE_14400,
464 .rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF,
467 static void t38_parameters_ast_to_fax(struct ast_fax_t38_parameters *dst, const struct ast_control_t38_parameters *src)
469 dst->version = src->version;
470 dst->max_ifp = src->max_ifp;
471 dst->rate = src->rate;
472 dst->rate_management = src->rate_management;
473 dst->fill_bit_removal = src->fill_bit_removal;
474 dst->transcoding_mmr = src->transcoding_mmr;
475 dst->transcoding_jbig = src->transcoding_jbig;
478 static void t38_parameters_fax_to_ast(struct ast_control_t38_parameters *dst, const struct ast_fax_t38_parameters *src)
480 dst->version = src->version;
481 dst->max_ifp = src->max_ifp;
482 dst->rate = src->rate;
483 dst->rate_management = src->rate_management;
484 dst->fill_bit_removal = src->fill_bit_removal;
485 dst->transcoding_mmr = src->transcoding_mmr;
486 dst->transcoding_jbig = src->transcoding_jbig;
489 /*! \brief returns a reference counted details structure from the channel's fax datastore. If the datastore
490 * does not exist it will be created */
491 static struct ast_fax_session_details *find_or_create_details(struct ast_channel *chan)
493 struct ast_fax_session_details *details;
494 struct ast_datastore *datastore;
496 if ((details = find_details(chan))) {
499 /* channel does not have one so we must create one */
500 if (!(details = session_details_new())) {
501 ast_log(LOG_WARNING, "channel '%s' can't get a FAX details structure for the datastore!\n", chan->name);
504 if (!(datastore = ast_datastore_alloc(&fax_datastore, NULL))) {
505 ao2_ref(details, -1);
506 ast_log(LOG_WARNING, "channel '%s' can't get a datastore!\n", chan->name);
509 /* add the datastore to the channel and increment the refcount */
510 datastore->data = details;
512 /* initialize default T.38 parameters */
513 t38_parameters_ast_to_fax(&details->our_t38_parameters, &our_t38_parameters);
514 t38_parameters_ast_to_fax(&details->their_t38_parameters, &our_t38_parameters);
517 ast_channel_lock(chan);
518 ast_channel_datastore_add(chan, datastore);
519 ast_channel_unlock(chan);
523 unsigned int ast_fax_maxrate(void)
525 return general_options.maxrate;
528 unsigned int ast_fax_minrate(void)
530 return general_options.minrate;
533 static int update_modem_bits(enum ast_fax_modems *bits, const char *value)
535 char *m[5], *tok, *v = (char *)value;
538 if (!(tok = strchr(v, ','))) {
542 tok = strtok(v, ", ");
543 while (tok && (i < 5)) {
545 tok = strtok(NULL, ", ");
551 for (j = 0; j < i; j++) {
552 if (!strcasecmp(m[j], "v17")) {
553 *bits |= AST_FAX_MODEM_V17;
554 } else if (!strcasecmp(m[j], "v27")) {
555 *bits |= AST_FAX_MODEM_V27;
556 } else if (!strcasecmp(m[j], "v29")) {
557 *bits |= AST_FAX_MODEM_V29;
558 } else if (!strcasecmp(m[j], "v34")) {
559 *bits |= AST_FAX_MODEM_V34;
561 ast_log(LOG_WARNING, "ignoring invalid modem setting: '%s', valid options {v17 | v27 | v29 | v34}\n", m[j]);
567 static int ast_fax_modem_to_str(enum ast_fax_modems bits, char *tbuf, size_t bufsize)
571 if (bits & AST_FAX_MODEM_V17) {
575 if (bits & AST_FAX_MODEM_V27) {
582 if (bits & AST_FAX_MODEM_V29) {
589 if (bits & AST_FAX_MODEM_V34) {
600 static int check_modem_rate(enum ast_fax_modems modems, unsigned int rate)
604 if (!(modems & (AST_FAX_MODEM_V27 | AST_FAX_MODEM_V34))) {
609 if (!(modems & (AST_FAX_MODEM_V27 | AST_FAX_MODEM_V34))) {
615 if (!(modems & (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V29 | AST_FAX_MODEM_V34))) {
621 if (!(modems & (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V34))) {
627 if (!(modems & AST_FAX_MODEM_V34)) {
632 /* this should never happen */
639 /*! \brief register a FAX technology module */
640 int ast_fax_tech_register(struct ast_fax_tech *tech)
642 struct fax_module *fax;
644 if (!(fax = ast_calloc(1, sizeof(*fax)))) {
648 AST_RWLIST_WRLOCK(&faxmodules);
649 AST_RWLIST_INSERT_TAIL(&faxmodules, fax, list);
650 AST_RWLIST_UNLOCK(&faxmodules);
651 ast_module_ref(ast_module_info->self);
653 ast_verb(3, "Registered handler for '%s' (%s)\n", fax->tech->type, fax->tech->description);
658 /*! \brief unregister a FAX technology module */
659 void ast_fax_tech_unregister(struct ast_fax_tech *tech)
661 struct fax_module *fax;
663 ast_verb(3, "Unregistering FAX module type '%s'\n", tech->type);
665 AST_RWLIST_WRLOCK(&faxmodules);
666 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&faxmodules, fax, list) {
667 if (fax->tech != tech) {
670 AST_RWLIST_REMOVE_CURRENT(list);
671 ast_module_unref(ast_module_info->self);
673 ast_verb(4, "Unregistered FAX module type '%s'\n", tech->type);
676 AST_RWLIST_TRAVERSE_SAFE_END;
677 AST_RWLIST_UNLOCK(&faxmodules);
680 /*! \brief convert a ast_fax_state to a string */
681 const char *ast_fax_state_to_str(enum ast_fax_state state)
684 case AST_FAX_STATE_UNINITIALIZED:
685 return "Uninitialized";
686 case AST_FAX_STATE_INITIALIZED:
687 return "Initialized";
688 case AST_FAX_STATE_OPEN:
690 case AST_FAX_STATE_ACTIVE:
692 case AST_FAX_STATE_COMPLETE:
694 case AST_FAX_STATE_RESERVED:
696 case AST_FAX_STATE_INACTIVE:
699 ast_log(LOG_WARNING, "unhandled FAX state: %d\n", state);
704 void ast_fax_log(int level, const char *file, const int line, const char *function, const char *msg)
706 if (fax_logger_level != -1) {
707 ast_log_dynamic_level(fax_logger_level, "%s", msg);
709 ast_log(level, file, line, function, "%s", msg);
713 /*! \brief convert a rate string to a rate */
714 static unsigned int fax_rate_str_to_int(const char *ratestr)
718 if (sscanf(ratestr, "%d", &rate) != 1) {
719 ast_log(LOG_ERROR, "failed to sscanf '%s' to rate\n", ratestr);
733 ast_log(LOG_WARNING, "ignoring invalid rate '%s'. Valid options are {2400 | 4800 | 7200 | 9600 | 12000 | 14400 | 28800 | 33600}\n", ratestr);
738 /*! \brief Release a session token.
739 * \param s a session returned from fax_session_reserve()
740 * \param token a token generated from fax_session_reserve()
742 * This function releases the given token and marks the given session as no
743 * longer reserved. It is safe to call on a session that is not actually
744 * reserved and with a NULL token. This is so that sessions returned by
745 * technologies that do not support reserved sessions don't require extra logic
748 * \note This function DOES NOT release the given fax session, only the given
751 static void fax_session_release(struct ast_fax_session *s, struct ast_fax_tech_token *token)
754 s->tech->release_token(token);
757 if (s->state == AST_FAX_STATE_RESERVED) {
758 ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, -1);
759 s->state = AST_FAX_STATE_INACTIVE;
763 /*! \brief destroy a FAX session structure */
764 static void destroy_session(void *session)
766 struct ast_fax_session *s = session;
769 fax_session_release(s, NULL);
771 s->tech->destroy_session(s);
773 ast_module_unref(s->tech->module);
777 ao2_ref(s->details, -1);
781 ast_dsp_free(s->debug_info->dsp);
782 ast_free(s->debug_info);
786 ast_smoother_free(s->smoother);
789 if (s->state != AST_FAX_STATE_INACTIVE) {
790 ast_atomic_fetchadd_int(&faxregistry.active_sessions, -1);
793 ast_free(s->channame);
794 ast_free(s->chan_uniqueid);
797 /*! \brief Reserve a fax session.
798 * \param details the fax session details
799 * \param token a pointer to a place to store a token to be passed to fax_session_new() later
801 * This function reserves a fax session for use later. If the selected fax
802 * technology does not support reserving sessions a session will still be
803 * returned but token will not be set.
805 * \note The reference returned by this function does not get consumed by
806 * fax_session_new() and must always be dereferenced separately.
808 * \return NULL or an uninitialized and possibly reserved session
810 static struct ast_fax_session *fax_session_reserve(struct ast_fax_session_details *details, struct ast_fax_tech_token **token)
812 struct ast_fax_session *s;
813 struct fax_module *faxmod;
815 if (!(s = ao2_alloc(sizeof(*s), destroy_session))) {
819 s->state = AST_FAX_STATE_INACTIVE;
820 s->details = details;
821 ao2_ref(s->details, 1);
823 /* locate a FAX technology module that can handle said requirements
824 * Note: the requirements have not yet been finalized as T.38
825 * negotiation has not yet occured. */
826 AST_RWLIST_RDLOCK(&faxmodules);
827 AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
828 if ((faxmod->tech->caps & details->caps) != details->caps) {
831 ast_debug(4, "Reserving a FAX session from '%s'.\n", faxmod->tech->description);
832 ast_module_ref(faxmod->tech->module);
833 s->tech = faxmod->tech;
836 AST_RWLIST_UNLOCK(&faxmodules);
839 ast_log(LOG_ERROR, "Could not locate a FAX technology module with capabilities (0x%X)\n", details->caps);
844 if (!s->tech->reserve_session) {
845 ast_debug(1, "Selected FAX technology module (%s) does not support reserving sessions.\n", s->tech->description);
849 if (!(*token = s->tech->reserve_session(s))) {
854 s->state = AST_FAX_STATE_RESERVED;
855 ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, 1);
860 /*! \brief create a FAX session
862 * \param details details for the session
863 * \param chan the channel the session will run on
864 * \param reserved a reserved session to base this session on (can be NULL)
865 * \param token the token for a reserved session (can be NULL)
867 * Create a new fax session based on the given details structure.
869 * \note The given token is always consumed (by tech->new_session() or by
870 * fax_session_release() in the event of a failure). The given reference to a
871 * reserved session is never consumed and must be dereferenced separately from
872 * the reference returned by this function.
874 * \return NULL or a reference to a new fax session
876 static struct ast_fax_session *fax_session_new(struct ast_fax_session_details *details, struct ast_channel *chan, struct ast_fax_session *reserved, struct ast_fax_tech_token *token)
878 struct ast_fax_session *s = NULL;
879 struct fax_module *faxmod;
883 ao2_ref(reserved, +1);
885 /* NOTE: we don't consume the reference to the reserved
886 * session. The session returned from fax_session_new() is a
887 * new reference and must be derefed in addition to the
891 if (s->state == AST_FAX_STATE_RESERVED) {
892 ast_atomic_fetchadd_int(&faxregistry.reserved_sessions, -1);
893 s->state = AST_FAX_STATE_UNINITIALIZED;
897 if (!s && !(s = ao2_alloc(sizeof(*s), destroy_session))) {
901 ast_atomic_fetchadd_int(&faxregistry.active_sessions, 1);
902 s->state = AST_FAX_STATE_UNINITIALIZED;
904 if (details->option.debug && (details->caps & AST_FAX_TECH_AUDIO)) {
905 if (!(s->debug_info = ast_calloc(1, sizeof(*(s->debug_info))))) {
906 fax_session_release(s, token);
910 if (!(s->debug_info->dsp = ast_dsp_new())) {
911 ast_free(s->debug_info);
912 s->debug_info = NULL;
913 fax_session_release(s, token);
917 ast_dsp_set_threshold(s->debug_info->dsp, 128);
920 if (!(s->channame = ast_strdup(chan->name))) {
921 fax_session_release(s, token);
926 if (!(s->chan_uniqueid = ast_strdup(chan->uniqueid))) {
927 fax_session_release(s, token);
934 s->details = details;
935 ao2_ref(s->details, 1);
938 details->id = s->id = ast_atomic_fetchadd_int(&faxregistry.nextsessionname, 1);
941 /* locate a FAX technology module that can handle said requirements */
942 AST_RWLIST_RDLOCK(&faxmodules);
943 AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
944 if ((faxmod->tech->caps & details->caps) != details->caps) {
947 ast_debug(4, "Requesting a new FAX session from '%s'.\n", faxmod->tech->description);
948 ast_module_ref(faxmod->tech->module);
949 s->tech = faxmod->tech;
952 AST_RWLIST_UNLOCK(&faxmodules);
955 ast_log(LOG_ERROR, "Could not locate a FAX technology module with capabilities (0x%X)\n", details->caps);
961 if (!(s->tech_pvt = s->tech->new_session(s, token))) {
962 ast_log(LOG_ERROR, "FAX session failed to initialize.\n");
966 /* link the session to the session container */
967 if (!(ao2_link(faxregistry.container, s))) {
968 ast_log(LOG_ERROR, "failed to add FAX session '%d' to container.\n", s->id);
972 ast_debug(4, "channel '%s' using FAX session '%d'\n", s->channame, s->id);
977 static void get_manager_event_info(struct ast_channel *chan, struct manager_event_info *info)
979 pbx_substitute_variables_helper(chan, "${CONTEXT}", info->context, sizeof(info->context));
980 pbx_substitute_variables_helper(chan, "${EXTEN}", info->exten, sizeof(info->exten));
981 pbx_substitute_variables_helper(chan, "${CALLERID(num)}", info->cid, sizeof(info->cid));
985 /* \brief Generate a string of filenames using the given prefix and separator.
986 * \param details the fax session details
987 * \param prefix the prefix to each filename
988 * \param separator the separator between filenames
990 * This function generates a string of filenames from the given details
991 * structure and using the given prefix and separator.
993 * \retval NULL there was an error generating the string
994 * \return the string generated string
996 static char *generate_filenames_string(struct ast_fax_session_details *details, char *prefix, char *separator)
1001 struct ast_fax_document *doc;
1003 /* don't process empty lists */
1004 if (AST_LIST_EMPTY(&details->documents)) {
1008 /* Calculate the total length of all of the file names */
1009 AST_LIST_TRAVERSE(&details->documents, doc, next) {
1010 size += strlen(separator) + strlen(prefix) + strlen(doc->filename);
1012 size += 1; /* add space for the terminating null */
1014 if (!(filenames = ast_malloc(size))) {
1019 ast_build_string(&c, &size, "%s%s", prefix, AST_LIST_FIRST(&details->documents)->filename);
1020 AST_LIST_TRAVERSE(&details->documents, doc, next) {
1026 ast_build_string(&c, &size, "%s%s%s", separator, prefix, doc->filename);
1032 /*! \brief send a FAX status manager event */
1033 static int report_fax_status(struct ast_channel *chan, struct ast_fax_session_details *details, const char *status)
1035 char *filenames = generate_filenames_string(details, "FileName: ", "\r\n");
1037 ast_channel_lock(chan);
1038 if (details->option.statusevents) {
1039 struct manager_event_info info;
1041 get_manager_event_info(chan, &info);
1042 manager_event(EVENT_FLAG_CALL,
1050 "LocalStationID: %s\r\n"
1052 (details->caps & AST_FAX_TECH_GATEWAY) ? "gateway" : (details->caps & AST_FAX_TECH_RECEIVE) ? "receive" : "send",
1058 details->localstationid,
1059 S_OR(filenames, ""),
1060 filenames ? "\r\n" : "");
1062 ast_channel_unlock(chan);
1065 ast_free(filenames);
1071 /*! \brief Set fax related channel variables. */
1072 static void set_channel_variables(struct ast_channel *chan, struct ast_fax_session_details *details)
1075 pbx_builtin_setvar_helper(chan, "FAXSTATUS", S_OR(details->result, NULL));
1076 pbx_builtin_setvar_helper(chan, "FAXERROR", S_OR(details->error, NULL));
1077 pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", S_OR(details->resultstr, NULL));
1078 pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", S_OR(details->remotestationid, NULL));
1079 pbx_builtin_setvar_helper(chan, "LOCALSTATIONID", S_OR(details->localstationid, NULL));
1080 pbx_builtin_setvar_helper(chan, "FAXBITRATE", S_OR(details->transfer_rate, NULL));
1081 pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", S_OR(details->resolution, NULL));
1083 snprintf(buf, sizeof(buf), "%d", details->pages_transferred);
1084 pbx_builtin_setvar_helper(chan, "FAXPAGES", buf);
1087 #define GENERIC_FAX_EXEC_SET_VARS(fax, chan, errorstr, reason) \
1089 if (ast_strlen_zero(fax->details->result)) \
1090 ast_string_field_set(fax->details, result, "FAILED"); \
1091 if (ast_strlen_zero(fax->details->resultstr)) \
1092 ast_string_field_set(fax->details, resultstr, reason); \
1093 if (ast_strlen_zero(fax->details->error)) \
1094 ast_string_field_set(fax->details, error, errorstr); \
1095 set_channel_variables(chan, fax->details); \
1098 #define GENERIC_FAX_EXEC_ERROR_QUIET(fax, chan, errorstr, reason) \
1100 GENERIC_FAX_EXEC_SET_VARS(fax, chan, errorstr, reason); \
1104 #define GENERIC_FAX_EXEC_ERROR(fax, chan, errorstr, reason) \
1106 ast_log(LOG_ERROR, "channel '%s' FAX session '%d' failure, reason: '%s' (%s)\n", chan->name, fax->id, reason, errorstr); \
1107 GENERIC_FAX_EXEC_ERROR_QUIET(fax, chan, errorstr, reason); \
1110 static int set_fax_t38_caps(struct ast_channel *chan, struct ast_fax_session_details *details)
1112 switch (ast_channel_get_t38_state(chan)) {
1113 case T38_STATE_UNKNOWN:
1114 details->caps |= AST_FAX_TECH_T38;
1116 case T38_STATE_REJECTED:
1117 case T38_STATE_UNAVAILABLE:
1118 details->caps |= AST_FAX_TECH_AUDIO;
1120 case T38_STATE_NEGOTIATED:
1121 /* already in T.38 mode? This should not happen. */
1122 case T38_STATE_NEGOTIATING: {
1123 /* the other end already sent us a T.38 reinvite, so we need to prod the channel
1124 * driver into resending their parameters to us if it supports doing so... if
1125 * not, we can't proceed, because we can't create a proper reply without them.
1126 * if it does work, the channel driver will send an AST_CONTROL_T38_PARAMETERS
1127 * with a request of AST_T38_REQUEST_NEGOTIATE, which will be read by the function
1128 * that gets called after this one completes
1130 struct ast_control_t38_parameters parameters = { .request_response = AST_T38_REQUEST_PARMS, };
1131 if (ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, ¶meters, sizeof(parameters)) != AST_T38_REQUEST_PARMS) {
1132 ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", chan->name);
1135 details->caps |= AST_FAX_TECH_T38;
1139 ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", chan->name);
1146 static int disable_t38(struct ast_channel *chan)
1149 struct ast_frame *frame = NULL;
1150 struct ast_control_t38_parameters t38_parameters = { .request_response = AST_T38_REQUEST_TERMINATE, };
1152 ast_debug(1, "Shutting down T.38 on %s\n", chan->name);
1153 if (ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0) {
1154 ast_debug(1, "error while disabling T.38 on channel '%s'\n", chan->name);
1158 /* wait up to five seconds for negotiation to complete */
1162 ms = ast_waitfor(chan, ms);
1164 ast_debug(1, "error while disabling T.38 on channel '%s'\n", chan->name);
1168 if (ms == 0) { /* all done, nothing happened */
1169 ast_debug(1, "channel '%s' timed-out during T.38 shutdown\n", chan->name);
1173 if (!(frame = ast_read(chan))) {
1176 if ((frame->frametype == AST_FRAME_CONTROL) &&
1177 (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
1178 (frame->datalen == sizeof(t38_parameters))) {
1179 struct ast_control_t38_parameters *parameters = frame->data.ptr;
1181 switch (parameters->request_response) {
1182 case AST_T38_TERMINATED:
1183 ast_debug(1, "Shut down T.38 on %s\n", chan->name);
1185 case AST_T38_REFUSED:
1186 ast_log(LOG_WARNING, "channel '%s' refused to disable T.38\n", chan->name);
1190 ast_log(LOG_ERROR, "channel '%s' failed to disable T.38\n", chan->name);
1203 /*! \brief this is the generic FAX session handling function */
1204 static int generic_fax_exec(struct ast_channel *chan, struct ast_fax_session_details *details, struct ast_fax_session *reserved, struct ast_fax_tech_token *token)
1207 int timeout = RES_FAX_TIMEOUT;
1208 int res = 0, chancount;
1209 unsigned int expected_frametype = -1;
1210 union ast_frame_subclass expected_framesubclass = { .integer = -1 };
1211 unsigned int t38negotiated = (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED);
1212 struct ast_control_t38_parameters t38_parameters;
1213 const char *tempvar;
1214 struct ast_fax_session *fax = NULL;
1215 struct ast_frame *frame = NULL;
1216 struct ast_channel *c = chan;
1217 struct ast_format orig_write_format;
1218 struct ast_format orig_read_format;
1220 ast_format_clear(&orig_write_format);
1221 ast_format_clear(&orig_read_format);
1224 /* create the FAX session */
1225 if (!(fax = fax_session_new(details, chan, reserved, token))) {
1226 ast_log(LOG_ERROR, "Can't create a FAX session, FAX attempt failed.\n");
1227 report_fax_status(chan, details, "No Available Resource");
1231 ast_channel_lock(chan);
1232 /* update session details */
1233 if (ast_strlen_zero(details->headerinfo) && (tempvar = pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO"))) {
1234 ast_string_field_set(details, headerinfo, tempvar);
1236 if (ast_strlen_zero(details->localstationid)) {
1237 tempvar = pbx_builtin_getvar_helper(chan, "LOCALSTATIONID");
1238 ast_string_field_set(details, localstationid, tempvar ? tempvar : "unknown");
1240 ast_channel_unlock(chan);
1242 report_fax_status(chan, details, "Allocating Resources");
1244 if (details->caps & AST_FAX_TECH_AUDIO) {
1245 expected_frametype = AST_FRAME_VOICE;;
1246 ast_format_set(&expected_framesubclass.format, AST_FORMAT_SLINEAR, 0);
1247 ast_format_copy(&orig_write_format, &chan->writeformat);
1248 if (ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
1249 ast_log(LOG_ERROR, "channel '%s' failed to set write format to signed linear'.\n", chan->name);
1250 ao2_lock(faxregistry.container);
1251 ao2_unlink(faxregistry.container, fax);
1252 ao2_unlock(faxregistry.container);
1254 ast_channel_unlock(chan);
1257 ast_format_copy(&orig_read_format, &chan->readformat);
1258 if (ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR) < 0) {
1259 ast_log(LOG_ERROR, "channel '%s' failed to set read format to signed linear.\n", chan->name);
1260 ao2_lock(faxregistry.container);
1261 ao2_unlink(faxregistry.container, fax);
1262 ao2_unlock(faxregistry.container);
1264 ast_channel_unlock(chan);
1267 if (fax->smoother) {
1268 ast_smoother_free(fax->smoother);
1269 fax->smoother = NULL;
1271 if (!(fax->smoother = ast_smoother_new(320))) {
1272 ast_log(LOG_WARNING, "Channel '%s' FAX session '%d' failed to obtain a smoother.\n", chan->name, fax->id);
1275 expected_frametype = AST_FRAME_MODEM;
1276 expected_framesubclass.integer = AST_MODEM_T38;
1279 if (fax->debug_info) {
1280 fax->debug_info->base_tv = ast_tvnow();
1283 /* reset our result fields just in case the fax tech driver wants to
1284 * set custom error messages */
1285 ast_string_field_set(details, result, "");
1286 ast_string_field_set(details, resultstr, "");
1287 ast_string_field_set(details, error, "");
1288 set_channel_variables(chan, details);
1290 if (fax->tech->start_session(fax) < 0) {
1291 GENERIC_FAX_EXEC_ERROR(fax, chan, "INIT_ERROR", "failed to start FAX session");
1294 report_fax_status(chan, details, "FAX Transmission In Progress");
1296 ast_debug(5, "channel %s will wait on FAX fd %d\n", chan->name, fax->fd);
1298 /* handle frames for the session */
1300 while ((res > -1) && (ms > -1) && (timeout > 0)) {
1301 struct ast_channel *ready_chan;
1306 ready_chan = ast_waitfor_nandfds(&c, chancount, &fax->fd, 1, &exception, &ofd, &ms);
1308 if (!(frame = ast_read(chan))) {
1309 /* the channel is probably gone, so lets stop polling on it and let the
1310 * FAX session complete before we exit the application. if needed,
1311 * send the FAX stack silence so the modems can finish their session without
1313 ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", chan->name);
1314 GENERIC_FAX_EXEC_SET_VARS(fax, chan, "HANGUP", "remote channel hungup");
1317 timeout -= (1000 - ms);
1318 fax->tech->cancel_session(fax);
1319 if (fax->tech->generate_silence) {
1320 fax->tech->generate_silence(fax);
1325 if ((frame->frametype == AST_FRAME_CONTROL) &&
1326 (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
1327 (frame->datalen == sizeof(t38_parameters))) {
1328 unsigned int was_t38 = t38negotiated;
1329 struct ast_control_t38_parameters *parameters = frame->data.ptr;
1331 switch (parameters->request_response) {
1332 case AST_T38_REQUEST_NEGOTIATE:
1333 /* the other end has requested a switch to T.38, so reply that we are willing, if we can
1336 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
1337 t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
1338 ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
1340 case AST_T38_NEGOTIATED:
1341 t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
1347 if (t38negotiated && !was_t38) {
1348 fax->tech->switch_to_t38(fax);
1349 details->caps &= ~AST_FAX_TECH_AUDIO;
1350 expected_frametype = AST_FRAME_MODEM;
1351 expected_framesubclass.integer = AST_MODEM_T38;
1352 if (fax->smoother) {
1353 ast_smoother_free(fax->smoother);
1354 fax->smoother = NULL;
1357 report_fax_status(chan, details, "T.38 Negotiated");
1359 ast_verb(3, "Channel '%s' switched to T.38 FAX session '%d'.\n", chan->name, fax->id);
1361 } else if ((frame->frametype == expected_frametype) &&
1362 (!memcmp(&frame->subclass, &expected_framesubclass, sizeof(frame->subclass)))) {
1363 struct ast_frame *f;
1365 if (fax->smoother) {
1366 /* push the frame into a smoother */
1367 if (ast_smoother_feed(fax->smoother, frame) < 0) {
1368 GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "Failed to feed the smoother");
1370 while ((f = ast_smoother_read(fax->smoother)) && (f->data.ptr)) {
1371 if (fax->debug_info) {
1372 debug_check_frame_for_silence(fax, 1, f);
1374 /* write the frame to the FAX stack */
1375 fax->tech->write(fax, f);
1376 fax->frames_received++;
1382 /* write the frame to the FAX stack */
1383 fax->tech->write(fax, frame);
1384 fax->frames_received++;
1386 timeout = RES_FAX_TIMEOUT;
1389 } else if (ofd == fax->fd) {
1390 /* read a frame from the FAX stack and send it out the channel.
1391 * the FAX stack will return a NULL if the FAX session has already completed */
1392 if (!(frame = fax->tech->read(fax))) {
1396 if (fax->debug_info && (frame->frametype == AST_FRAME_VOICE)) {
1397 debug_check_frame_for_silence(fax, 0, frame);
1400 ast_write(chan, frame);
1403 timeout = RES_FAX_TIMEOUT;
1405 if (ms && (ofd < 0)) {
1406 if ((errno == 0) || (errno == EINTR)) {
1407 timeout -= (1000 - ms);
1409 GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
1412 ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", chan->name);
1413 GENERIC_FAX_EXEC_ERROR(fax, chan, "UNKNOWN", "error polling data");
1418 /* nothing happened */
1422 GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
1425 ast_log(LOG_WARNING, "channel '%s' timed-out during the FAX transmission.\n", chan->name);
1426 GENERIC_FAX_EXEC_ERROR(fax, chan, "TIMEOUT", "fax session timed-out");
1432 ast_debug(3, "channel '%s' - event loop stopped { timeout: %d, ms: %d, res: %d }\n", chan->name, timeout, ms, res);
1434 set_channel_variables(chan, details);
1436 ast_atomic_fetchadd_int(&faxregistry.fax_complete, 1);
1437 if (!strcasecmp(details->result, "FAILED")) {
1438 ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
1442 ao2_lock(faxregistry.container);
1443 ao2_unlink(faxregistry.container, fax);
1444 ao2_unlock(faxregistry.container);
1448 /* if the channel is still alive, and we changed its read/write formats,
1452 if (orig_read_format.id) {
1453 ast_set_read_format(chan, &orig_read_format);
1455 if (orig_write_format.id) {
1456 ast_set_write_format(chan, &orig_write_format);
1460 /* return the chancount so the calling function can determine if the channel hungup during this FAX session or not */
1464 static int receivefax_t38_init(struct ast_channel *chan, struct ast_fax_session_details *details)
1467 struct ast_frame *frame = NULL;
1468 struct ast_control_t38_parameters t38_parameters;
1470 /* don't send any audio if we've already received a T.38 reinvite */
1471 if (ast_channel_get_t38_state(chan) != T38_STATE_NEGOTIATING) {
1472 /* generate 3 seconds of CED */
1473 if (ast_playtones_start(chan, 1024, "!2100/3000", 1)) {
1474 ast_log(LOG_ERROR, "error generating CED tone on %s\n", chan->name);
1480 ms = ast_waitfor(chan, ms);
1482 ast_log(LOG_ERROR, "error while generating CED tone on %s\n", chan->name);
1483 ast_playtones_stop(chan);
1487 if (ms == 0) { /* all done, nothing happened */
1491 if (!(frame = ast_read(chan))) {
1492 ast_log(LOG_ERROR, "error reading frame while generating CED tone on %s\n", chan->name);
1493 ast_playtones_stop(chan);
1497 if ((frame->frametype == AST_FRAME_CONTROL) &&
1498 (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
1499 (frame->datalen == sizeof(t38_parameters))) {
1500 struct ast_control_t38_parameters *parameters = frame->data.ptr;
1502 switch (parameters->request_response) {
1503 case AST_T38_REQUEST_NEGOTIATE:
1504 /* the other end has requested a switch to T.38, so reply that we are willing, if we can
1507 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
1508 t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
1509 ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
1510 ast_playtones_stop(chan);
1512 case AST_T38_NEGOTIATED:
1513 ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
1514 t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
1515 details->caps &= ~AST_FAX_TECH_AUDIO;
1516 report_fax_status(chan, details, "T.38 Negotiated");
1525 ast_playtones_stop(chan);
1528 /* if T.38 was negotiated, we are done initializing */
1529 if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
1534 ast_debug(1, "Negotiating T.38 for receive on %s\n", chan->name);
1536 /* wait up to five seconds for negotiation to complete */
1539 /* set parameters based on the session's parameters */
1540 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
1541 t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
1542 if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
1547 ms = ast_waitfor(chan, ms);
1549 ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
1553 if (ms == 0) { /* all done, nothing happened */
1554 ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", chan->name);
1555 details->caps &= ~AST_FAX_TECH_T38;
1559 if (!(frame = ast_read(chan))) {
1560 ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
1564 if ((frame->frametype == AST_FRAME_CONTROL) &&
1565 (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
1566 (frame->datalen == sizeof(t38_parameters))) {
1567 struct ast_control_t38_parameters *parameters = frame->data.ptr;
1569 switch (parameters->request_response) {
1570 case AST_T38_REQUEST_NEGOTIATE:
1571 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
1572 t38_parameters.request_response = AST_T38_NEGOTIATED;
1573 ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
1575 case AST_T38_NEGOTIATED:
1576 ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
1577 t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
1578 details->caps &= ~AST_FAX_TECH_AUDIO;
1579 report_fax_status(chan, details, "T.38 Negotiated");
1582 case AST_T38_REFUSED:
1583 ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", chan->name);
1584 details->caps &= ~AST_FAX_TECH_T38;
1588 ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", chan->name);
1589 details->caps &= ~AST_FAX_TECH_T38;
1597 /* if T.38 was negotiated, we are done initializing */
1598 if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
1602 /* if we made it here, then T.38 failed, check the 'f' flag */
1603 if (details->option.allow_audio != AST_FAX_OPTFLAG_TRUE) {
1604 ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", chan->name);
1608 /* ok, audio fallback is allowed */
1609 details->caps |= AST_FAX_TECH_AUDIO;
1614 /*! \brief initiate a receive FAX session */
1615 static int receivefax_exec(struct ast_channel *chan, const char *data)
1617 char *parse, modems[128] = "";
1619 struct ast_fax_session_details *details;
1620 struct ast_fax_session *s;
1621 struct ast_fax_tech_token *token = NULL;
1622 struct ast_fax_document *doc;
1623 AST_DECLARE_APP_ARGS(args,
1624 AST_APP_ARG(filename);
1625 AST_APP_ARG(options);
1627 struct ast_flags opts = { 0, };
1628 struct manager_event_info info;
1629 enum ast_t38_state t38state;
1631 /* initialize output channel variables */
1632 pbx_builtin_setvar_helper(chan, "FAXSTATUS", "FAILED");
1633 pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", NULL);
1634 pbx_builtin_setvar_helper(chan, "LOCALSTATIONID", NULL);
1635 pbx_builtin_setvar_helper(chan, "FAXPAGES", "0");
1636 pbx_builtin_setvar_helper(chan, "FAXBITRATE", NULL);
1637 pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", NULL);
1639 /* Get a FAX session details structure from the channel's FAX datastore and create one if
1640 * it does not already exist. */
1641 if (!(details = find_or_create_details(chan))) {
1642 pbx_builtin_setvar_helper(chan, "FAXERROR", "MEMORY_ERROR");
1643 pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "error allocating memory");
1644 ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
1648 ast_string_field_set(details, result, "FAILED");
1649 ast_string_field_set(details, resultstr, "error starting fax session");
1650 ast_string_field_set(details, error, "INIT_ERROR");
1651 set_channel_variables(chan, details);
1653 if (details->caps & AST_FAX_TECH_GATEWAY) {
1654 ast_string_field_set(details, resultstr, "can't receive a fax on a channel with a T.38 gateway");
1655 set_channel_variables(chan, details);
1656 ast_log(LOG_ERROR, "executing ReceiveFAX on a channel with a T.38 Gateway is not supported\n");
1657 ao2_ref(details, -1);
1661 if (details->maxrate < details->minrate) {
1662 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
1663 ast_string_field_set(details, resultstr, "maxrate is less than minrate");
1664 set_channel_variables(chan, details);
1665 ast_log(LOG_ERROR, "maxrate %d is less than minrate %d\n", details->maxrate, details->minrate);
1666 ao2_ref(details, -1);
1670 if (check_modem_rate(details->modems, details->minrate)) {
1671 ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
1672 ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %d\n", modems, details->minrate);
1673 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
1674 ast_string_field_set(details, resultstr, "incompatible 'modems' and 'minrate' settings");
1675 set_channel_variables(chan, details);
1676 ao2_ref(details, -1);
1680 if (check_modem_rate(details->modems, details->maxrate)) {
1681 ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
1682 ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %d\n", modems, details->maxrate);
1683 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
1684 ast_string_field_set(details, resultstr, "incompatible 'modems' and 'maxrate' settings");
1685 set_channel_variables(chan, details);
1686 ao2_ref(details, -1);
1690 if (ast_strlen_zero(data)) {
1691 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
1692 ast_string_field_set(details, resultstr, "invalid arguments");
1693 set_channel_variables(chan, details);
1694 ast_log(LOG_WARNING, "%s requires an argument (filename[,options])\n", app_receivefax);
1695 ao2_ref(details, -1);
1698 parse = ast_strdupa(data);
1699 AST_STANDARD_APP_ARGS(args, parse);
1701 if (!ast_strlen_zero(args.options) &&
1702 ast_app_parse_options(fax_exec_options, &opts, NULL, args.options)) {
1703 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
1704 ast_string_field_set(details, resultstr, "invalid arguments");
1705 set_channel_variables(chan, details);
1706 ao2_ref(details, -1);
1709 if (ast_strlen_zero(args.filename)) {
1710 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
1711 ast_string_field_set(details, resultstr, "invalid arguments");
1712 set_channel_variables(chan, details);
1713 ast_log(LOG_WARNING, "%s requires an argument (filename[,options])\n", app_receivefax);
1714 ao2_ref(details, -1);
1718 /* check for unsupported FAX application options */
1719 if (ast_test_flag(&opts, OPT_CALLERMODE) || ast_test_flag(&opts, OPT_CALLEDMODE)) {
1720 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
1721 ast_string_field_set(details, resultstr, "invalid arguments");
1722 set_channel_variables(chan, details);
1723 ast_log(LOG_WARNING, "%s does not support polling\n", app_receivefax);
1724 ao2_ref(details, -1);
1728 ast_atomic_fetchadd_int(&faxregistry.fax_rx_attempts, 1);
1730 pbx_builtin_setvar_helper(chan, "FAXERROR", "Channel Problems");
1731 pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "Error before FAX transmission started.");
1733 if (!(doc = ast_calloc(1, sizeof(*doc) + strlen(args.filename) + 1))) {
1734 ast_string_field_set(details, error, "MEMORY_ERROR");
1735 ast_string_field_set(details, resultstr, "error allocating memory");
1736 set_channel_variables(chan, details);
1737 ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
1738 ao2_ref(details, -1);
1742 strcpy(doc->filename, args.filename);
1743 AST_LIST_INSERT_TAIL(&details->documents, doc, next);
1745 ast_verb(3, "Channel '%s' receiving FAX '%s'\n", chan->name, args.filename);
1747 details->caps = AST_FAX_TECH_RECEIVE;
1749 /* check for debug */
1750 if (ast_test_flag(&opts, OPT_DEBUG) || global_fax_debug) {
1751 details->option.debug = AST_FAX_OPTFLAG_TRUE;
1754 /* check for request for status events */
1755 if (ast_test_flag(&opts, OPT_STATUS)) {
1756 details->option.statusevents = AST_FAX_OPTFLAG_TRUE;
1759 t38state = ast_channel_get_t38_state(chan);
1760 if ((t38state == T38_STATE_UNAVAILABLE) || (t38state == T38_STATE_REJECTED) ||
1761 ast_test_flag(&opts, OPT_ALLOWAUDIO) ||
1762 ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
1763 details->option.allow_audio = AST_FAX_OPTFLAG_TRUE;
1766 if (!(s = fax_session_reserve(details, &token))) {
1767 ast_string_field_set(details, resultstr, "error reserving fax session");
1768 set_channel_variables(chan, details);
1769 ast_log(LOG_ERROR, "Unable to reserve FAX session.\n");
1770 ao2_ref(details, -1);
1774 /* make sure the channel is up */
1775 if (chan->_state != AST_STATE_UP) {
1776 if (ast_answer(chan)) {
1777 ast_string_field_set(details, resultstr, "error answering channel");
1778 set_channel_variables(chan, details);
1779 ast_log(LOG_WARNING, "Channel '%s' failed answer attempt.\n", chan->name);
1780 fax_session_release(s, token);
1782 ao2_ref(details, -1);
1787 if (!ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
1788 if (set_fax_t38_caps(chan, details)) {
1789 ast_string_field_set(details, error, "T38_NEG_ERROR");
1790 ast_string_field_set(details, resultstr, "error negotiating T.38");
1791 set_channel_variables(chan, details);
1792 fax_session_release(s, token);
1794 ao2_ref(details, -1);
1798 details->caps |= AST_FAX_TECH_AUDIO;
1801 if (!ast_test_flag(&opts, OPT_FORCE_AUDIO) && (details->caps & AST_FAX_TECH_T38)) {
1802 if (receivefax_t38_init(chan, details)) {
1803 ast_string_field_set(details, error, "T38_NEG_ERROR");
1804 ast_string_field_set(details, resultstr, "error negotiating T.38");
1805 set_channel_variables(chan, details);
1806 fax_session_release(s, token);
1808 ao2_ref(details, -1);
1809 ast_log(LOG_ERROR, "error initializing channel '%s' in T.38 mode\n", chan->name);
1813 details->option.send_ced = 1;
1816 if ((channel_alive = generic_fax_exec(chan, details, s, token)) < 0) {
1817 ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
1820 if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
1821 if (disable_t38(chan)) {
1822 ast_debug(1, "error disabling T.38 mode on %s\n", chan->name);
1826 /* send out the AMI completion event */
1827 ast_channel_lock(chan);
1829 get_manager_event_info(chan, &info);
1830 manager_event(EVENT_FLAG_CALL,
1836 "RemoteStationID: %s\r\n"
1837 "LocalStationID: %s\r\n"
1838 "PagesTransferred: %s\r\n"
1839 "Resolution: %s\r\n"
1840 "TransferRate: %s\r\n"
1846 S_OR(pbx_builtin_getvar_helper(chan, "REMOTESTATIONID"), ""),
1847 S_OR(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"), ""),
1848 S_OR(pbx_builtin_getvar_helper(chan, "FAXPAGES"), ""),
1849 S_OR(pbx_builtin_getvar_helper(chan, "FAXRESOLUTION"), ""),
1850 S_OR(pbx_builtin_getvar_helper(chan, "FAXBITRATE"), ""),
1852 ast_channel_unlock(chan);
1855 ao2_ref(details, -1);
1857 /* If the channel hungup return -1; otherwise, return 0 to continue in the dialplan */
1858 return (!channel_alive) ? -1 : 0;
1861 static int sendfax_t38_init(struct ast_channel *chan, struct ast_fax_session_details *details)
1864 struct ast_frame *frame = NULL;
1865 struct ast_control_t38_parameters t38_parameters;
1867 /* send CNG tone while listening for the receiver to initiate a switch
1868 * to T.38 mode; if they do, stop sending the CNG tone and proceed with
1871 * 10500 is enough time for 3 CNG tones
1875 /* don't send any audio if we've already received a T.38 reinvite */
1876 if (ast_channel_get_t38_state(chan) != T38_STATE_NEGOTIATING) {
1877 if (ast_playtones_start(chan, 1024, "!1100/500,!0/3000,!1100/500,!0/3000,!1100/500,!0/3000", 1)) {
1878 ast_log(LOG_ERROR, "error generating CNG tone on %s\n", chan->name);
1884 ms = ast_waitfor(chan, ms);
1886 ast_log(LOG_ERROR, "error while generating CNG tone on %s\n", chan->name);
1887 ast_playtones_stop(chan);
1891 if (ms == 0) { /* all done, nothing happened */
1895 if (!(frame = ast_read(chan))) {
1896 ast_log(LOG_ERROR, "error reading frame while generating CNG tone on %s\n", chan->name);
1897 ast_playtones_stop(chan);
1901 if ((frame->frametype == AST_FRAME_CONTROL) &&
1902 (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
1903 (frame->datalen == sizeof(t38_parameters))) {
1904 struct ast_control_t38_parameters *parameters = frame->data.ptr;
1906 switch (parameters->request_response) {
1907 case AST_T38_REQUEST_NEGOTIATE:
1908 /* the other end has requested a switch to T.38, so reply that we are willing, if we can
1911 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
1912 t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
1913 ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
1914 ast_playtones_stop(chan);
1916 case AST_T38_NEGOTIATED:
1917 ast_debug(1, "Negotiated T.38 for send on %s\n", chan->name);
1918 t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
1919 details->caps &= ~AST_FAX_TECH_AUDIO;
1920 report_fax_status(chan, details, "T.38 Negotiated");
1930 ast_playtones_stop(chan);
1932 if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
1936 /* T.38 negotiation did not happen, initiate a switch if requested */
1937 if (details->option.request_t38 == AST_FAX_OPTFLAG_TRUE) {
1938 ast_debug(1, "Negotiating T.38 for send on %s\n", chan->name);
1940 /* wait up to five seconds for negotiation to complete */
1943 /* set parameters based on the session's parameters */
1944 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
1945 t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
1946 if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
1951 ms = ast_waitfor(chan, ms);
1953 ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
1957 if (ms == 0) { /* all done, nothing happened */
1958 ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", chan->name);
1959 details->caps &= ~AST_FAX_TECH_T38;
1963 if (!(frame = ast_read(chan))) {
1964 ast_log(LOG_WARNING, "error on '%s' while waiting for T.38 negotiation.\n", chan->name);
1968 if ((frame->frametype == AST_FRAME_CONTROL) &&
1969 (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
1970 (frame->datalen == sizeof(t38_parameters))) {
1971 struct ast_control_t38_parameters *parameters = frame->data.ptr;
1973 switch (parameters->request_response) {
1974 case AST_T38_REQUEST_NEGOTIATE:
1975 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
1976 t38_parameters.request_response = AST_T38_NEGOTIATED;
1977 ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
1979 case AST_T38_NEGOTIATED:
1980 ast_debug(1, "Negotiated T.38 for receive on %s\n", chan->name);
1981 t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
1982 details->caps &= ~AST_FAX_TECH_AUDIO;
1983 report_fax_status(chan, details, "T.38 Negotiated");
1986 case AST_T38_REFUSED:
1987 ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", chan->name);
1988 details->caps &= ~AST_FAX_TECH_T38;
1992 ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", chan->name);
1993 details->caps &= ~AST_FAX_TECH_T38;
2001 /* if T.38 was negotiated, we are done initializing */
2002 if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
2006 /* send one more CNG tone to get audio going again for some
2007 * carriers if we are going to fall back to audio mode */
2008 if (details->option.allow_audio == AST_FAX_OPTFLAG_TRUE) {
2009 if (ast_playtones_start(chan, 1024, "!1100/500,!0/3000", 1)) {
2010 ast_log(LOG_ERROR, "error generating second CNG tone on %s\n", chan->name);
2016 ms = ast_waitfor(chan, ms);
2018 ast_log(LOG_ERROR, "error while generating second CNG tone on %s\n", chan->name);
2019 ast_playtones_stop(chan);
2023 if (ms == 0) { /* all done, nothing happened */
2027 if (!(frame = ast_read(chan))) {
2028 ast_log(LOG_ERROR, "error reading frame while generating second CNG tone on %s\n", chan->name);
2029 ast_playtones_stop(chan);
2033 if ((frame->frametype == AST_FRAME_CONTROL) &&
2034 (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
2035 (frame->datalen == sizeof(t38_parameters))) {
2036 struct ast_control_t38_parameters *parameters = frame->data.ptr;
2038 switch (parameters->request_response) {
2039 case AST_T38_REQUEST_NEGOTIATE:
2040 /* the other end has requested a switch to T.38, so reply that we are willing, if we can
2043 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
2044 t38_parameters.request_response = (details->caps & AST_FAX_TECH_T38) ? AST_T38_NEGOTIATED : AST_T38_REFUSED;
2045 ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
2046 ast_playtones_stop(chan);
2048 case AST_T38_NEGOTIATED:
2049 ast_debug(1, "Negotiated T.38 for send on %s\n", chan->name);
2050 t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
2051 details->caps &= ~AST_FAX_TECH_AUDIO;
2052 report_fax_status(chan, details, "T.38 Negotiated");
2062 ast_playtones_stop(chan);
2064 /* if T.38 was negotiated, we are done initializing */
2065 if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
2071 /* if we made it here, then T.38 failed, check the 'f' flag */
2072 if (details->option.allow_audio == AST_FAX_OPTFLAG_FALSE) {
2073 ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", chan->name);
2077 /* ok, audio fallback is allowed */
2078 details->caps |= AST_FAX_TECH_AUDIO;
2084 /*! \brief initiate a send FAX session */
2085 static int sendfax_exec(struct ast_channel *chan, const char *data)
2087 char *parse, *filenames, *c, modems[128] = "";
2088 int channel_alive, file_count;
2089 struct ast_fax_session_details *details;
2090 struct ast_fax_session *s;
2091 struct ast_fax_tech_token *token = NULL;
2092 struct ast_fax_document *doc;
2093 AST_DECLARE_APP_ARGS(args,
2094 AST_APP_ARG(filenames);
2095 AST_APP_ARG(options);
2097 struct ast_flags opts = { 0, };
2098 struct manager_event_info info;
2099 enum ast_t38_state t38state;
2101 /* initialize output channel variables */
2102 pbx_builtin_setvar_helper(chan, "FAXSTATUS", "FAILED");
2103 pbx_builtin_setvar_helper(chan, "REMOTESTATIONID", NULL);
2104 pbx_builtin_setvar_helper(chan, "LOCALSTATIONID", NULL);
2105 pbx_builtin_setvar_helper(chan, "FAXPAGES", "0");
2106 pbx_builtin_setvar_helper(chan, "FAXBITRATE", NULL);
2107 pbx_builtin_setvar_helper(chan, "FAXRESOLUTION", NULL);
2109 /* Get a requirement structure and set it. This structure is used
2110 * to tell the FAX technology module about the higher level FAX session */
2111 if (!(details = find_or_create_details(chan))) {
2112 pbx_builtin_setvar_helper(chan, "FAXERROR", "MEMORY_ERROR");
2113 pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", "error allocating memory");
2114 ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
2118 ast_string_field_set(details, result, "FAILED");
2119 ast_string_field_set(details, resultstr, "error starting fax session");
2120 ast_string_field_set(details, error, "INIT_ERROR");
2121 set_channel_variables(chan, details);
2123 if (details->caps & AST_FAX_TECH_GATEWAY) {
2124 ast_string_field_set(details, resultstr, "can't send a fax on a channel with a T.38 gateway");
2125 set_channel_variables(chan, details);
2126 ast_log(LOG_ERROR, "executing SendFAX on a channel with a T.38 Gateway is not supported\n");
2127 ao2_ref(details, -1);
2131 if (details->maxrate < details->minrate) {
2132 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
2133 ast_string_field_set(details, resultstr, "maxrate is less than minrate");
2134 set_channel_variables(chan, details);
2135 ast_log(LOG_ERROR, "maxrate %d is less than minrate %d\n", details->maxrate, details->minrate);
2136 ao2_ref(details, -1);
2140 if (check_modem_rate(details->modems, details->minrate)) {
2141 ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
2142 ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'minrate' setting %d\n", modems, details->minrate);
2143 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
2144 ast_string_field_set(details, resultstr, "incompatible 'modems' and 'minrate' settings");
2145 set_channel_variables(chan, details);
2146 ao2_ref(details, -1);
2150 if (check_modem_rate(details->modems, details->maxrate)) {
2151 ast_fax_modem_to_str(details->modems, modems, sizeof(modems));
2152 ast_log(LOG_ERROR, "'modems' setting '%s' is incompatible with 'maxrate' setting %d\n", modems, details->maxrate);
2153 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
2154 ast_string_field_set(details, resultstr, "incompatible 'modems' and 'maxrate' settings");
2155 set_channel_variables(chan, details);
2156 ao2_ref(details, -1);
2160 if (ast_strlen_zero(data)) {
2161 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
2162 ast_string_field_set(details, resultstr, "invalid arguments");
2163 set_channel_variables(chan, details);
2164 ast_log(LOG_WARNING, "%s requires an argument (filename[&filename[&filename]][,options])\n", app_sendfax);
2165 ao2_ref(details, -1);
2168 parse = ast_strdupa(data);
2169 AST_STANDARD_APP_ARGS(args, parse);
2172 if (!ast_strlen_zero(args.options) &&
2173 ast_app_parse_options(fax_exec_options, &opts, NULL, args.options)) {
2174 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
2175 ast_string_field_set(details, resultstr, "invalid arguments");
2176 set_channel_variables(chan, details);
2177 ao2_ref(details, -1);
2180 if (ast_strlen_zero(args.filenames)) {
2181 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
2182 ast_string_field_set(details, resultstr, "invalid arguments");
2183 set_channel_variables(chan, details);
2184 ast_log(LOG_WARNING, "%s requires an argument (filename[&filename[&filename]],options])\n", app_sendfax);
2185 ao2_ref(details, -1);
2189 /* check for unsupported FAX application options */
2190 if (ast_test_flag(&opts, OPT_CALLERMODE) || ast_test_flag(&opts, OPT_CALLEDMODE)) {
2191 ast_string_field_set(details, error, "INVALID_ARGUMENTS");
2192 ast_string_field_set(details, resultstr, "invalid arguments");
2193 set_channel_variables(chan, details);
2194 ast_log(LOG_WARNING, "%s does not support polling\n", app_sendfax);
2195 ao2_ref(details, -1);
2199 ast_atomic_fetchadd_int(&faxregistry.fax_tx_attempts, 1);
2202 filenames = args.filenames;
2203 while ((c = strsep(&filenames, "&"))) {
2204 if (access(c, (F_OK | R_OK)) < 0) {
2205 ast_string_field_set(details, error, "FILE_ERROR");
2206 ast_string_field_set(details, resultstr, "error reading file");
2207 set_channel_variables(chan, details);
2208 ast_log(LOG_ERROR, "access failure. Verify '%s' exists and check permissions.\n", args.filenames);
2209 ao2_ref(details, -1);
2213 if (!(doc = ast_calloc(1, sizeof(*doc) + strlen(c) + 1))) {
2214 ast_string_field_set(details, error, "MEMORY_ERROR");
2215 ast_string_field_set(details, resultstr, "error allocating memory");
2216 set_channel_variables(chan, details);
2217 ast_log(LOG_ERROR, "System cannot provide memory for session requirements.\n");
2218 ao2_ref(details, -1);
2222 strcpy(doc->filename, c);
2223 AST_LIST_INSERT_TAIL(&details->documents, doc, next);
2227 if (file_count > 1) {
2228 details->caps |= AST_FAX_TECH_MULTI_DOC;
2231 ast_verb(3, "Channel '%s' sending FAX:\n", chan->name);
2232 AST_LIST_TRAVERSE(&details->documents, doc, next) {
2233 ast_verb(3, " %s\n", doc->filename);
2236 details->caps = AST_FAX_TECH_SEND;
2238 /* check for debug */
2239 if (ast_test_flag(&opts, OPT_DEBUG) || global_fax_debug) {
2240 details->option.debug = AST_FAX_OPTFLAG_TRUE;
2243 /* check for request for status events */
2244 if (ast_test_flag(&opts, OPT_STATUS)) {
2245 details->option.statusevents = AST_FAX_OPTFLAG_TRUE;
2248 t38state = ast_channel_get_t38_state(chan);
2249 if ((t38state == T38_STATE_UNAVAILABLE) || (t38state == T38_STATE_REJECTED) ||
2250 ast_test_flag(&opts, OPT_ALLOWAUDIO) ||
2251 ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
2252 details->option.allow_audio = AST_FAX_OPTFLAG_TRUE;
2255 if (ast_test_flag(&opts, OPT_REQUEST_T38)) {
2256 details->option.request_t38 = AST_FAX_OPTFLAG_TRUE;
2259 if (!(s = fax_session_reserve(details, &token))) {
2260 ast_string_field_set(details, resultstr, "error reserving fax session");
2261 set_channel_variables(chan, details);
2262 ast_log(LOG_ERROR, "Unable to reserve FAX session.\n");
2263 ao2_ref(details, -1);
2267 /* make sure the channel is up */
2268 if (chan->_state != AST_STATE_UP) {
2269 if (ast_answer(chan)) {
2270 ast_string_field_set(details, resultstr, "error answering channel");
2271 set_channel_variables(chan, details);
2272 ast_log(LOG_WARNING, "Channel '%s' failed answer attempt.\n", chan->name);
2273 fax_session_release(s, token);
2275 ao2_ref(details, -1);
2280 if (!ast_test_flag(&opts, OPT_FORCE_AUDIO)) {
2281 if (set_fax_t38_caps(chan, details)) {
2282 ast_string_field_set(details, error, "T38_NEG_ERROR");
2283 ast_string_field_set(details, resultstr, "error negotiating T.38");
2284 set_channel_variables(chan, details);
2285 fax_session_release(s, token);
2287 ao2_ref(details, -1);
2291 details->caps |= AST_FAX_TECH_AUDIO;
2294 if (!ast_test_flag(&opts, OPT_FORCE_AUDIO) && (details->caps & AST_FAX_TECH_T38)) {
2295 if (sendfax_t38_init(chan, details)) {
2296 ast_string_field_set(details, error, "T38_NEG_ERROR");
2297 ast_string_field_set(details, resultstr, "error negotiating T.38");
2298 set_channel_variables(chan, details);
2299 fax_session_release(s, token);
2301 ao2_ref(details, -1);
2302 ast_log(LOG_ERROR, "error initializing channel '%s' in T.38 mode\n", chan->name);
2306 details->option.send_cng = 1;
2309 if ((channel_alive = generic_fax_exec(chan, details, s, token)) < 0) {
2310 ast_atomic_fetchadd_int(&faxregistry.fax_failures, 1);
2313 if (ast_channel_get_t38_state(chan) == T38_STATE_NEGOTIATED) {
2314 if (disable_t38(chan)) {
2315 ast_debug(1, "error disabling T.38 mode on %s\n", chan->name);
2319 if (!(filenames = generate_filenames_string(details, "FileName: ", "\r\n"))) {
2320 ast_log(LOG_ERROR, "Error generating SendFAX manager event\n");
2322 ao2_ref(details, -1);
2323 return (!channel_alive) ? -1 : 0;
2326 /* send out the AMI completion event */
2327 ast_channel_lock(chan);
2328 get_manager_event_info(chan, &info);
2329 manager_event(EVENT_FLAG_CALL,
2335 "RemoteStationID: %s\r\n"
2336 "LocalStationID: %s\r\n"
2337 "PagesTransferred: %s\r\n"
2338 "Resolution: %s\r\n"
2339 "TransferRate: %s\r\n"
2345 S_OR(pbx_builtin_getvar_helper(chan, "REMOTESTATIONID"), ""),
2346 S_OR(pbx_builtin_getvar_helper(chan, "LOCALSTATIONID"), ""),
2347 S_OR(pbx_builtin_getvar_helper(chan, "FAXPAGES"), ""),
2348 S_OR(pbx_builtin_getvar_helper(chan, "FAXRESOLUTION"), ""),
2349 S_OR(pbx_builtin_getvar_helper(chan, "FAXBITRATE"), ""),
2351 ast_channel_unlock(chan);
2353 ast_free(filenames);
2356 ao2_ref(details, -1);
2358 /* If the channel hungup return -1; otherwise, return 0 to continue in the dialplan */
2359 return (!channel_alive) ? -1 : 0;
2362 /*! \brief destroy a FAX gateway session structure */
2363 static void destroy_gateway(void *data)
2365 struct fax_gateway *gateway = data;
2367 if (gateway->chan_dsp) {
2368 ast_dsp_free(gateway->chan_dsp);
2369 gateway->chan_dsp = NULL;
2372 if (gateway->peer_dsp) {
2373 ast_dsp_free(gateway->peer_dsp);
2374 gateway->peer_dsp = NULL;
2378 fax_session_release(gateway->s, gateway->token);
2379 gateway->token = NULL;
2380 gateway->s->details->caps |= ~AST_FAX_TECH_GATEWAY;
2382 ao2_lock(faxregistry.container);
2383 ao2_unlink(faxregistry.container, gateway->s);
2384 ao2_unlock(faxregistry.container);
2386 ao2_ref(gateway->s, -1);
2391 /*! \brief Create a new fax gateway object.
2392 * \param details the fax session details
2393 * \return NULL or a fax gateway object
2395 static struct fax_gateway *fax_gateway_new(struct ast_fax_session_details *details)
2397 struct fax_gateway *gateway = ao2_alloc(sizeof(*gateway), destroy_gateway);
2402 gateway->chan_dsp = ast_dsp_new();
2403 if (!gateway->chan_dsp) {
2404 ao2_ref(gateway, -1);
2408 gateway->peer_dsp = ast_dsp_new();
2409 if (!gateway->peer_dsp) {
2410 ao2_ref(gateway, -1);
2414 gateway->framehook = -1;
2416 ast_dsp_set_features(gateway->chan_dsp, DSP_FEATURE_FAX_DETECT);
2417 ast_dsp_set_faxmode(gateway->chan_dsp, DSP_FAXMODE_DETECT_V21);
2419 ast_dsp_set_features(gateway->peer_dsp, DSP_FEATURE_FAX_DETECT);
2420 ast_dsp_set_faxmode(gateway->peer_dsp, DSP_FAXMODE_DETECT_V21);
2422 details->caps = AST_FAX_TECH_GATEWAY;
2423 if (details->gateway_timeout && !(gateway->s = fax_session_reserve(details, &gateway->token))) {
2424 details->caps |= ~AST_FAX_TECH_GATEWAY;
2425 ast_log(LOG_ERROR, "Can't reserve a FAX session, gateway attempt failed.\n");
2426 ao2_ref(gateway, -1);
2433 /*! \brief Create a fax session and start T.30<->T.38 gateway mode
2434 * \param gateway a fax gateway object
2435 * \param details fax session details
2436 * \param chan active channel
2437 * \return 0 on error 1 on success*/
2438 static int fax_gateway_start(struct fax_gateway *gateway, struct ast_fax_session_details *details, struct ast_channel *chan)
2440 struct ast_fax_session *s;
2442 /* create the FAX session */
2443 if (!(s = fax_session_new(details, chan, gateway->s, gateway->token))) {
2444 gateway->token = NULL;
2445 ast_string_field_set(details, result, "FAILED");
2446 ast_string_field_set(details, resultstr, "error starting gateway session");
2447 ast_string_field_set(details, error, "INIT_ERROR");
2448 set_channel_variables(chan, details);
2449 report_fax_status(chan, details, "No Available Resource");
2450 ast_log(LOG_ERROR, "Can't create a FAX session, gateway attempt failed.\n");
2453 /* release the reference for the reserved session and replace it with
2454 * the real session */
2455 ao2_ref(gateway->s, -1);
2457 gateway->token = NULL;
2459 if (gateway->s->tech->start_session(gateway->s) < 0) {
2460 ast_string_field_set(details, result, "FAILED");
2461 ast_string_field_set(details, resultstr, "error starting gateway session");
2462 ast_string_field_set(details, error, "INIT_ERROR");
2463 set_channel_variables(chan, details);
2467 gateway->timeout_start.tv_sec = 0;
2468 gateway->timeout_start.tv_usec = 0;
2470 report_fax_status(chan, details, "FAX Transmission In Progress");
2475 static struct ast_frame *fax_gateway_request_t38(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_frame *f)
2477 struct ast_frame *fp;
2478 struct ast_control_t38_parameters t38_parameters = {
2479 .request_response = AST_T38_REQUEST_NEGOTIATE,
2481 struct ast_frame control_frame = {
2483 .frametype = AST_FRAME_CONTROL,
2484 .datalen = sizeof(t38_parameters),
2485 .subclass.integer = AST_CONTROL_T38_PARAMETERS,
2486 .data.ptr = &t38_parameters,
2489 struct ast_fax_session_details *details = find_details(chan);
2492 ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", chan->name);
2493 ast_framehook_detach(chan, gateway->framehook);
2497 t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
2498 ao2_ref(details, -1);
2500 if (!(fp = ast_frisolate(&control_frame))) {
2501 ast_log(LOG_ERROR, "error generating T.38 request control frame on chan %s for T.38 gateway session\n", chan->name);
2505 gateway->t38_state = T38_STATE_NEGOTIATING;
2506 gateway->timeout_start = ast_tvnow();
2507 details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
2509 ast_debug(1, "requesting T.38 for gateway session for %s\n", chan->name);
2513 static struct ast_frame *fax_gateway_detect_v21(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *active, struct ast_frame *f)
2515 struct ast_frame *dfr = ast_frdup(f);
2516 struct ast_dsp *active_dsp = (active == chan) ? gateway->chan_dsp : gateway->peer_dsp;
2517 struct ast_channel *other = (active == chan) ? peer : chan;
2519 if (gateway->detected_v21) {
2527 if (!(dfr = ast_dsp_process(active, active_dsp, dfr))) {
2531 if (dfr->frametype == AST_FRAME_DTMF && dfr->subclass.integer == 'g') {
2532 gateway->detected_v21 = 1;
2533 if (ast_channel_get_t38_state(other) == T38_STATE_UNKNOWN) {
2534 ast_debug(1, "detected v21 preamble from %s\n", active->name);
2535 return fax_gateway_request_t38(gateway, chan, f);
2537 ast_debug(1, "detected v21 preamble on %s, but %s does not support T.38 for T.38 gateway session\n", active->name, other->name);
2545 static int fax_gateway_indicate_t38(struct ast_channel *chan, struct ast_channel *active, struct ast_control_t38_parameters *control_params)
2547 if (active == chan) {
2548 return ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, control_params, sizeof(*control_params));
2550 return ast_queue_control_data(chan, AST_CONTROL_T38_PARAMETERS, control_params, sizeof(*control_params));
2554 /*! \brief T38 Gateway Negotiate t38 parameters
2555 * \param gateway gateway object
2556 * \param chan channel running the gateway
2557 * \param peer channel im bridged too
2558 * \param active channel the frame originated on
2559 * \param f the control frame to process
2560 * \return processed control frame or null frame
2562 static struct ast_frame *fax_gateway_detect_t38(struct fax_gateway *gateway, struct ast_channel *chan, struct ast_channel *peer, struct ast_channel *active, struct ast_frame *f)
2564 struct ast_control_t38_parameters *control_params = f->data.ptr;
2565 struct ast_channel *other = (active == chan) ? peer : chan;
2566 struct ast_fax_session_details *details;
2568 if (f->datalen != sizeof(struct ast_control_t38_parameters)) {
2569 /* invalaid AST_CONTROL_T38_PARAMETERS frame, we can't
2570 * do anything with it, pass it on */
2574 /* ignore frames from ourselves */
2575 if ((gateway->t38_state == T38_STATE_NEGOTIATED && control_params->request_response == AST_T38_NEGOTIATED)
2576 || (gateway->t38_state == T38_STATE_REJECTED && control_params->request_response == AST_T38_REFUSED)
2577 || (gateway->t38_state == T38_STATE_NEGOTIATING && control_params->request_response == AST_T38_REQUEST_TERMINATE)) {
2582 if (!(details = find_details(chan))) {
2583 ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", chan->name);
2584 ast_framehook_detach(chan, gateway->framehook);
2588 if (control_params->request_response == AST_T38_REQUEST_NEGOTIATE) {
2589 enum ast_t38_state state = ast_channel_get_t38_state(other);
2591 if (state == T38_STATE_UNKNOWN) {
2592 /* we detected a request to negotiate T.38 and the
2593 * other channel appears to support T.38, we'll pass
2594 * the request through and only step in if the other
2595 * channel rejects the request */
2596 ast_debug(1, "%s is attempting to negotiate T.38 with %s, we'll see what happens\n", active->name, other->name);
2597 t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
2598 gateway->t38_state = T38_STATE_UNKNOWN;
2599 gateway->timeout_start = ast_tvnow();
2600 details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
2601 ao2_ref(details, -1);
2603 } else if (state == T38_STATE_UNAVAILABLE || state == T38_STATE_REJECTED) {
2604 /* the other channel does not support T.38, we need to
2606 ast_debug(1, "%s is attempting to negotiate T.38 but %s does not support it\n", active->name, other->name);
2607 ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
2609 t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
2610 t38_parameters_fax_to_ast(control_params, &details->our_t38_parameters);
2612 if (fax_gateway_start(gateway, details, chan)) {
2613 ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
2614 gateway->t38_state = T38_STATE_REJECTED;
2615 control_params->request_response = AST_T38_REFUSED;
2617 ast_framehook_detach(chan, details->gateway_id);
2618 details->gateway_id = -1;
2620 gateway->t38_state = T38_STATE_NEGOTIATED;
2621 control_params->request_response = AST_T38_NEGOTIATED;
2622 report_fax_status(chan, details, "T.38 Negotiated");
2625 fax_gateway_indicate_t38(chan, active, control_params);
2627 ao2_ref(details, -1);
2628 return &ast_null_frame;
2629 } else if (gateway->t38_state == T38_STATE_NEGOTIATING) {
2630 /* we got a request to negotiate T.38 after we already
2631 * sent one to the other party based on v21 preamble
2632 * detection. We'll just pretend we passed this request
2633 * through in the first place. */
2635 t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
2636 gateway->t38_state = T38_STATE_UNKNOWN;
2637 gateway->timeout_start = ast_tvnow();
2638 details->gateway_timeout = FAX_GATEWAY_TIMEOUT;
2640 ast_debug(1, "%s is attempting to negotiate T.38 after we already sent a negotiation request based on v21 preamble detection\n", active->name);
2641 ao2_ref(details, -1);
2642 return &ast_null_frame;
2643 } else if (gateway->t38_state == T38_STATE_NEGOTIATED) {
2644 /* we got a request to negotiate T.38 after we already
2645 * sent one to the other party based on v21 preamble
2646 * detection and received a response. We need to
2647 * respond to this and shut down the gateway. */
2649 t38_parameters_fax_to_ast(control_params, &details->their_t38_parameters);
2650 ast_framehook_detach(chan, details->gateway_id);
2651 details->gateway_id = -1;
2653 control_params->request_response = AST_T38_NEGOTIATED;
2655 fax_gateway_indicate_t38(chan, active, control_params);
2657 ast_string_field_set(details, result, "SUCCESS");
2658 ast_string_field_set(details, resultstr, "no gateway necessary");
2659 ast_string_field_set(details, error, "NATIVE_T38");
2660 set_channel_variables(chan, details);
2662 ast_debug(1, "%s is attempting to negotiate T.38 after we already negotiated T.38 with %s, disabling the gateway\n", active->name, other->name);
2663 ao2_ref(details, -1);
2664 return &ast_null_frame;
2666 ast_log(LOG_WARNING, "%s is attempting to negotiate T.38 while %s is in an unsupported state\n", active->name, other->name);
2667 ao2_ref(details, -1);
2670 } else if (gateway->t38_state == T38_STATE_NEGOTIATING
2671 && control_params->request_response == AST_T38_REFUSED) {
2673 ast_debug(1, "unable to negotiate T.38 on %s for fax gateway\n", active->name);
2675 /* our request to negotiate T.38 was refused, if the other
2676 * channel supports T.38, they might still reinvite and save
2677 * the day. Otherwise disable the gateway. */
2678 if (ast_channel_get_t38_state(other) == T38_STATE_UNKNOWN) {
2679 gateway->t38_state = T38_STATE_UNAVAILABLE;
2681 ast_framehook_detach(chan, details->gateway_id);
2682 details->gateway_id = -1;
2684 ast_string_field_set(details, result, "FAILED");
2685 ast_string_field_set(details, resultstr, "unable to negotiate T.38");
2686 ast_string_field_set(details, error, "T38_NEG_ERROR");
2687 set_channel_variables(chan, details);
2690 ao2_ref(details, -1);
2691 return &ast_null_frame;
2692 } else if (gateway->t38_state == T38_STATE_NEGOTIATING
2693 && control_params->request_response == AST_T38_NEGOTIATED) {
2695 ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
2697 t38_parameters_ast_to_fax(&details->their_t38_parameters, control_params);
2699 if (fax_gateway_start(gateway, details, chan)) {
2700 ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
2701 gateway->t38_state = T38_STATE_NEGOTIATING;
2702 control_params->request_response = AST_T38_REQUEST_TERMINATE;
2704 fax_gateway_indicate_t38(chan, active, control_params);
2706 gateway->t38_state = T38_STATE_NEGOTIATED;
2707 report_fax_status(chan, details, "T.38 Negotiated");
2710 ao2_ref(details, -1);
2711 return &ast_null_frame;
2712 } else if (control_params->request_response == AST_T38_REFUSED) {
2713 /* the other channel refused the request to negotiate T.38,
2714 * we'll step in here and pretend the request was accepted */
2716 ast_debug(1, "%s attempted to negotiate T.38 but %s refused the request\n", other->name, active->name);
2717 ast_debug(1, "starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", other->name, active->name);
2719 t38_parameters_fax_to_ast(control_params, &details->our_t38_parameters);
2721 if (fax_gateway_start(gateway, details, chan)) {
2722 ast_log(LOG_ERROR, "error starting T.38 gateway for T.38 channel %s and G.711 channel %s\n", active->name, other->name);
2723 gateway->t38_state = T38_STATE_REJECTED;
2724 control_params->request_response = AST_T38_REFUSED;
2726 ast_framehook_detach(chan, details->gateway_id);
2727 details->gateway_id = -1;
2729 gateway->t38_state = T38_STATE_NEGOTIATED;
2730 control_params->request_response = AST_T38_NEGOTIATED;
2733 ao2_ref(details, -1);
2735 } else if (control_params->request_response == AST_T38_REQUEST_TERMINATE) {
2736 /* the channel wishes to end our short relationship, we shall
2739 ast_debug(1, "T.38 channel %s is requesting a shutdown of T.38, disabling the gateway\n", active->name);
2741 ast_framehook_detach(chan, details->gateway_id);
2742 details->gateway_id = -1;
2744 gateway->t38_state = T38_STATE_REJECTED;
2745 control_params->request_response = AST_T38_TERMINATED;
2747 fax_gateway_indicate_t38(chan, active, control_params);
2749 ao2_ref(details, -1);
2750 return &ast_null_frame;
2751 } else if (control_params->request_response == AST_T38_NEGOTIATED) {
2752 ast_debug(1, "T.38 successfully negotiated between %s and %s, no gateway necessary\n", active->name, other->name);
2754 ast_framehook_detach(chan, details->gateway_id);
2755 details->gateway_id = -1;
2757 ast_string_field_set(details, result, "SUCCESS");
2758 ast_string_field_set(details, resultstr, "no gateway necessary");
2759 ast_string_field_set(details, error, "NATIVE_T38");
2760 set_channel_variables(chan, details);
2762 ao2_ref(details, -1);
2764 } else if (control_params->request_response == AST_T38_TERMINATED) {
2765 ast_debug(1, "T.38 disabled on channel %s\n", active->name);
2767 ast_framehook_detach(chan, details->gateway_id);
2768 details->gateway_id = -1;
2770 ao2_ref(details, -1);
2771 return &ast_null_frame;
2774 ao2_ref(details, -1);
2778 /*! \brief Destroy the gateway data structure when the framehook is detached
2779 * \param data framehook data (gateway data)*/
2780 static void fax_gateway_framehook_destroy(void *data) {
2781 struct fax_gateway *gateway = data;
2784 switch (gateway->s->state) {
2785 case AST_FAX_STATE_INITIALIZED:
2786 case AST_FAX_STATE_OPEN:
2787 case AST_FAX_STATE_ACTIVE:
2788 case AST_FAX_STATE_COMPLETE:
2789 if (gateway->s->tech->cancel_session) {
2790 gateway->s->tech->cancel_session(gateway->s);
2798 ao2_ref(gateway, -1);
2801 /*! \brief T.30<->T.38 gateway framehook.
2803 * Intercept packets on bridged channels and determine if a T.38 gateway is
2804 * required. If a gateway is required, start a gateway and handle T.38
2805 * negotiation if necessary.
2807 * \param chan channel running the gateway
2808 * \param f frame to handle may be NULL
2809 * \param event framehook event
2810 * \param data framehook data (struct fax_gateway *)
2812 * \return processed frame or NULL when f is NULL or a null frame
2814 static struct ast_frame *fax_gateway_framehook(struct ast_channel *chan, struct ast_frame *f, enum ast_framehook_event event, void *data) {
2815 struct fax_gateway *gateway = data;
2816 struct ast_channel *peer, *active;
2817 struct ast_fax_session_details *details;
2820 details = gateway->s->details;
2821 ao2_ref(details, 1);
2823 if (!(details = find_details(chan))) {
2824 ast_log(LOG_ERROR, "no FAX session details found on chan %s for T.38 gateway session, odd\n", chan->name);
2825 ast_framehook_detach(chan, gateway->framehook);
2826 details->gateway_id = -1;
2831 /* restore audio formats when we are detached */
2832 if (event == AST_FRAMEHOOK_EVENT_DETACHED) {
2833 set_channel_variables(chan, details);
2835 if (gateway->bridged) {
2836 ast_set_read_format(chan, &gateway->chan_read_format);
2837 ast_set_read_format(chan, &gateway->chan_write_format);
2839 if ((peer = ast_bridged_channel(chan))) {
2840 ast_set_read_format(peer, &gateway->peer_read_format);
2841 ast_set_read_format(peer, &gateway->peer_write_format);
2842 ast_channel_make_compatible(chan, peer);
2846 details->caps &= ~AST_FAX_TECH_GATEWAY;
2848 ao2_ref(details, -1);
2852 if (!f || (event == AST_FRAMEHOOK_EVENT_ATTACHED)) {
2853 ao2_ref(details, -1);
2857 /* this frame was generated by the fax gateway, pass it on */
2858 if (ast_test_flag(f, AST_FAX_FRFLAG_GATEWAY)) {
2859 ao2_ref(details, -1);
2863 if (!(peer = ast_bridged_channel(chan))) {
2864 /* not bridged, don't do anything */
2865 ao2_ref(details, -1);
2869 if (!gateway->bridged && peer) {
2870 /* don't start a gateway if neither channel can handle T.38 */
2871 if (ast_channel_get_t38_state(chan) == T38_STATE_UNAVAILABLE && ast_channel_get_t38_state(peer) == T38_STATE_UNAVAILABLE) {
2872 ast_debug(1, "not starting gateway for %s and %s; neither channel supports T.38\n", chan->name, peer->name);
2873 ast_framehook_detach(chan, gateway->framehook);
2874 details->gateway_id = -1;
2876 ast_string_field_set(details, result, "FAILED");
2877 ast_string_field_set(details, resultstr, "neither channel supports T.38");
2878 ast_string_field_set(details, error, "T38_NEG_ERROR");
2879 set_channel_variables(chan, details);
2880 ao2_ref(details, -1);
2884 if (details->gateway_timeout) {
2885 gateway->timeout_start = ast_tvnow();
2888 /* we are bridged, change r/w formats to SLIN for v21 preamble
2889 * detection and T.30 */
2890 ast_format_copy(&gateway->chan_read_format, &chan->readformat);
2891 ast_format_copy(&gateway->chan_write_format, &chan->readformat);
2893 ast_format_copy(&gateway->peer_read_format, &peer->readformat);
2894 ast_format_copy(&gateway->peer_write_format, &peer->readformat);
2896 ast_set_read_format_by_id(chan, AST_FORMAT_SLINEAR);
2897 ast_set_write_format_by_id(chan, AST_FORMAT_SLINEAR);
2899 ast_set_read_format_by_id(peer, AST_FORMAT_SLINEAR);
2900 ast_set_write_format_by_id(peer, AST_FORMAT_SLINEAR);
2902 ast_channel_make_compatible(chan, peer);
2903 gateway->bridged = 1;
2906 if (gateway->bridged && !ast_tvzero(gateway->timeout_start)) {
2907 if (ast_tvdiff_ms(ast_tvnow(), gateway->timeout_start) > details->gateway_timeout) {
2908 ast_debug(1, "no fax activity between %s and %s after %d ms, disabling gateway\n", chan->name, peer->name, details->gateway_timeout);
2909 ast_framehook_detach(chan, gateway->framehook);
2910 details->gateway_id = -1;
2912 ast_string_field_set(details, result, "FAILED");
2913 ast_string_field_build(details, resultstr, "no fax activity after %d ms", details->gateway_timeout);
2914 ast_string_field_set(details, error, "TIMEOUT");
2915 set_channel_variables(chan, details);
2916 ao2_ref(details, -1);
2921 /* only handle VOICE, MODEM, and CONTROL frames*/
2922 switch (f->frametype) {
2923 case AST_FRAME_VOICE:
2924 switch (f->subclass.format.id) {
2925 case AST_FORMAT_SLINEAR:
2926 case AST_FORMAT_ALAW:
2927 case AST_FORMAT_ULAW:
2930 ao2_ref(details, -1);
2934 case AST_FRAME_MODEM:
2935 if (f->subclass.integer == AST_MODEM_T38) {
2938 ao2_ref(details, -1);
2940 case AST_FRAME_CONTROL:
2941 if (f->subclass.integer == AST_CONTROL_T38_PARAMETERS) {
2944 ao2_ref(details, -1);
2947 ao2_ref(details, -1);
2951 /* detect the active channel */
2953 case AST_FRAMEHOOK_EVENT_WRITE:
2956 case AST_FRAMEHOOK_EVENT_READ:
2960 ast_log(LOG_WARNING, "unhandled framehook event %i\n", event);
2961 ao2_ref(details, -1);
2965 /* handle control frames */
2966 if (f->frametype == AST_FRAME_CONTROL && f->subclass.integer == AST_CONTROL_T38_PARAMETERS) {
2967 ao2_ref(details, -1);
2968 return fax_gateway_detect_t38(gateway, chan, peer, active, f);
2971 if (!gateway->detected_v21 && gateway->t38_state == T38_STATE_UNAVAILABLE && f->frametype == AST_FRAME_VOICE) {
2972 /* not in gateway mode and have not detected v21 yet, listen
2974 ao2_ref(details, -1);
2975 return fax_gateway_detect_v21(gateway, chan, peer, active, f);
2978 /* in gateway mode, gateway some packets */
2979 if (gateway->t38_state == T38_STATE_NEGOTIATED) {
2980 /* framehooks are called in __ast_read() before frame format
2981 * translation is done, so we need to translate here */
2982 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass.format.id != AST_FORMAT_SLINEAR)) {
2983 if (active->readtrans && (f = ast_translate(active->readtrans, f, 1)) == NULL) {
2984 f = &ast_null_frame;
2985 ao2_ref(details, -1);
2990 /* XXX we ignore the return value here, perhaps we should
2991 * disable the gateway if a write fails. I am not sure how a
2992 * write would fail, or even if a failure would be fatal so for
2993 * now we'll just ignore the return value. */
2994 gateway->s->tech->write(gateway->s, f);
2995 f = &ast_null_frame;
2996 ao2_ref(details, -1);
3000 /* force silence on the line if T.38 negotiation might be taking place */
3001 if (gateway->t38_state != T38_STATE_UNAVAILABLE && gateway->t38_state != T38_STATE_REJECTED) {
3002 if (f->frametype == AST_FRAME_VOICE && f->subclass.format.id == AST_FORMAT_SLINEAR) {
3003 short silence_buf[f->samples];
3004 struct ast_frame silence_frame = {
3005 .frametype = AST_FRAME_VOICE,
3006 .data.ptr = silence_buf,
3007 .samples = f->samples,
3008 .datalen = sizeof(silence_buf),
3010 ast_format_set(&silence_frame.subclass.format, AST_FORMAT_SLINEAR, 0);
3011 memset(silence_buf, 0, sizeof(silence_buf));
3013 ao2_ref(details, -1);
3014 return ast_frisolate(&silence_frame);
3016 ao2_ref(details, -1);
3017 return &ast_null_frame;
3021 ao2_ref(details, -1);