[svn-commits] kpfleming: branch group/res_fax r232810 - in /team/group/res_fax: configs/ in...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Thu Dec 3 07:49:16 CST 2009


Author: kpfleming
Date: Thu Dec  3 07:49:13 2009
New Revision: 232810

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=232810
Log:
add trunk-compatible version of res_fax

Added:
    team/group/res_fax/configs/res_fax.conf.sample   (with props)
    team/group/res_fax/include/asterisk/res_fax.h   (with props)
    team/group/res_fax/res/res_fax.c   (with props)
    team/group/res_fax/res/res_fax.exports   (with props)
Modified:
    team/group/res_fax/include/asterisk/frame.h

Added: team/group/res_fax/configs/res_fax.conf.sample
URL: http://svnview.digium.com/svn/asterisk/team/group/res_fax/configs/res_fax.conf.sample?view=auto&rev=232810
==============================================================================
--- team/group/res_fax/configs/res_fax.conf.sample (added)
+++ team/group/res_fax/configs/res_fax.conf.sample Thu Dec  3 07:49:13 2009
@@ -1,0 +1,24 @@
+; Generic Fax Application configuration 
+
+[general]
+; Maximum Transmission Rate
+; Possible values are { 2400 | 4800 | 7200 | 9600 | 12000 | 14400 }
+; Set this value to the maximum desired transfer rate.  Default: 14400
+maxrate=14400
+
+; Minimum Transmission Rate
+; Possible values are { 2400 | 4800 | 7200 | 9600 | 12000 | 14400 }
+; Set this value to the minimum desired transfer rate.  Default: 2400
+minrate=2400
+
+; Send Progress/Status events to manager session
+; Manager events with 'call' class permissions will receive events indicating the
+; steps to initiate a fax session.  Fax completion events are always sent to manager
+; sessions with 'call' class permissions, regardless of the value of this option.
+; Default: no
+statusevents=yes
+
+; modem capabilities
+; Possible values are { v17 | v27 | v29 }
+; Set this value to modify the default modem options.  Default: v17,v27,v29
+modems=v17,v27,v29

Propchange: team/group/res_fax/configs/res_fax.conf.sample
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/group/res_fax/configs/res_fax.conf.sample
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/group/res_fax/configs/res_fax.conf.sample
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: team/group/res_fax/include/asterisk/frame.h
URL: http://svnview.digium.com/svn/asterisk/team/group/res_fax/include/asterisk/frame.h?view=diff&rev=232810&r1=232809&r2=232810
==============================================================================
--- team/group/res_fax/include/asterisk/frame.h (original)
+++ team/group/res_fax/include/asterisk/frame.h Thu Dec  3 07:49:13 2009
@@ -131,16 +131,18 @@
 	AST_FRFLAG_HAS_TIMING_INFO = (1 << 0),
 };
 
+union ast_frame_subclass {
+	int integer;
+	format_t codec;
+};
+
 /*! \brief Data structure associated with a single frame of data
  */
 struct ast_frame {
 	/*! Kind of frame */
 	enum ast_frame_type frametype;				
 	/*! Subclass, frame dependent */
-	union {
-		int integer;
-		format_t codec;
-	} subclass;
+	union ast_frame_subclass subclass;
 	/*! Length of data */
 	int datalen;				
 	/*! Number of samples in this frame */

Added: team/group/res_fax/include/asterisk/res_fax.h
URL: http://svnview.digium.com/svn/asterisk/team/group/res_fax/include/asterisk/res_fax.h?view=auto&rev=232810
==============================================================================
--- team/group/res_fax/include/asterisk/res_fax.h (added)
+++ team/group/res_fax/include/asterisk/res_fax.h Thu Dec  3 07:49:13 2009
@@ -1,0 +1,249 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2008-2009, Digium, Inc.
+ *
+ * Dwayne M. Hubbard <dhubbard at digium.com>
+ * Kevin P. Fleming <kpfleming at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+#ifndef _ASTERISK_RES_FAX_H
+#define _ASTERISK_RES_FAX_H
+
+#include <asterisk.h>
+#include <asterisk/lock.h>
+#include <asterisk/linkedlists.h>
+#include <asterisk/module.h>
+#include <asterisk/utils.h>
+#include <asterisk/options.h>
+#include <asterisk/frame.h>
+#include <asterisk/cli.h>
+#include <asterisk/stringfields.h>
+
+/*! \brief capabilities for res_fax to locate a fax technology module */
+enum ast_fax_capabilities {
+	/*! SendFax is supported */
+	AST_FAX_TECH_SEND    = (1 << 0),
+	/*! ReceiveFax is supported */
+	AST_FAX_TECH_RECEIVE = (1 << 1),
+	/*! Audio FAX session supported */
+	AST_FAX_TECH_AUDIO   = (1 << 2),
+	/*! T.38 FAX session supported */
+	AST_FAX_TECH_T38     = (1 << 3),
+};
+
+/*! \brief fax modem capabilities */
+enum ast_fax_modems {
+	/*! V.17 */
+	AST_FAX_MODEM_V17 = (1 << 0),
+	/*! V.27 */
+	AST_FAX_MODEM_V27 = (1 << 1),
+	/*! V.29 */
+	AST_FAX_MODEM_V29 = (1 << 2),
+	/*! V.34 */
+	AST_FAX_MODEM_V34 = (1 << 3),
+};
+
+/*! \brief current state of a fax session */
+enum ast_fax_state {
+	/*! uninitialized state */
+	AST_FAX_STATE_UNINITIALIZED = 0,
+	/*! initialized state */
+	AST_FAX_STATE_INITIALIZED,
+	/*! fax resources open state */
+	AST_FAX_STATE_OPEN,
+	/*! fax session in progress */
+	AST_FAX_STATE_ACTIVE,
+	/*! fax session complete */
+	AST_FAX_STATE_COMPLETE,
+};
+
+/*! \brief fax session options */
+enum ast_fax_optflag {
+	/*! false/disable configuration override */
+	AST_FAX_OPTFLAG_FALSE = 0,
+	/*! true/enable configuration override */
+	AST_FAX_OPTFLAG_TRUE,
+	/*! use the configured default */
+	AST_FAX_OPTFLAG_DEFAULT,
+};
+
+struct ast_fax_t38_parameters {
+	unsigned int version;					/*!< Supported T.38 version */
+	unsigned int max_ifp; 					/*!< Maximum IFP size supported */
+	enum ast_control_t38_rate rate;				/*!< Maximum fax rate supported */
+	enum ast_control_t38_rate_management rate_management;	/*!< Rate management setting */
+	unsigned int fill_bit_removal:1;			/*!< Set if fill bit removal can be used */
+	unsigned int transcoding_mmr:1;				/*!< Set if MMR transcoding can be used */
+	unsigned int transcoding_jbig:1;			/*!< Set if JBIG transcoding can be used */
+};
+
+struct ast_fax_document {
+	AST_LIST_ENTRY(ast_fax_document) next;
+	char filename[0];
+};
+
+AST_LIST_HEAD_NOLOCK(ast_fax_documents, ast_fax_document);
+
+/*! \brief The data communicated between the high level applications and the generic fax function */
+struct ast_fax_session_details {
+	/*! fax session capability requirements.  The caps field is used to select
+ 	 * the proper fax technology module before the session starts */
+	enum ast_fax_capabilities caps;
+	/*! modem requirement for the session */
+	enum ast_fax_modems modems;
+	/*! session id */
+	unsigned int id;
+	/*! document(s) to be sent/received */
+	struct ast_fax_documents documents;
+	AST_DECLARE_STRING_FIELDS(
+		/*! resolution negotiated during the fax session.  This is stored in the
+	 	 * FAXRESOLUTION channel variable when the fax session completes */
+		AST_STRING_FIELD(resolution);
+		/*! transfer rate negotiated during the fax session.  This is stored in the
+	 	 * FAXBITRATE channel variable when the fax session completes */
+		AST_STRING_FIELD(transfer_rate);
+		/*! local station identification.  This is set from the LOCALSTATIONID
+	 	 * channel variable before the fax session starts */
+		AST_STRING_FIELD(localstationid);
+		/*! remote station identification.  This is stored in the REMOTESTATIONID
+	 	 * channel variable after the fax session completes */
+		AST_STRING_FIELD(remotestationid);
+		/*! headerinfo variable is set from the LOCALHEADERINFO channel variable
+	 	 * before the fax session starts */
+		AST_STRING_FIELD(headerinfo);
+		/*! the result of the fax session */
+		AST_STRING_FIELD(result);
+		/*! a more descriptive result string of the fax session */
+		AST_STRING_FIELD(resultstr);
+		/*! the error reason of the fax session */
+		AST_STRING_FIELD(error);
+	);
+	/*! the number of pages sent/received during a fax session */
+	unsigned int pages_transferred;
+	/*! session details flags for options */
+	union {
+		/*! dontuse dummy variable - do not ever use */	
+		uint32_t dontuse;
+		struct {
+			/*! flag to send debug manager events */
+			uint32_t debug:2;
+			/*! flag indicating the use of Error Correction Mode (ECM) */
+			uint32_t ecm:2;
+			/*! flag indicating the sending of status manager events */
+			uint32_t statusevents:2;
+			/*! allow audio mode FAX on T.38-capable channels */
+			uint32_t allow_audio:2;
+			/*! indicating the session switched to T38 */
+			uint32_t switch_to_t38:1;
+		};
+	} option;
+	/*! override the minimum transmission rate with a channel variable */
+	unsigned int minrate;
+	/*! override the maximum transmission rate with a channel varialbe */
+	unsigned int maxrate;
+	/*! our T.38 session parameters, if any */
+	struct ast_fax_t38_parameters our_t38_parameters;
+	/*! the other endpoint's T.38 session parameters, if any */
+	struct ast_fax_t38_parameters their_t38_parameters;
+};
+
+struct ast_fax_tech;
+struct ast_fax_debug_info;
+	
+/*! \brief The data required to handle a fax session */
+struct ast_fax_session {
+	/*! session id */
+	unsigned int id;
+	/*! session file descriptor */
+	int fd;
+	/*! fax session details structure */
+	struct ast_fax_session_details *details;
+	/*! fax frames received */
+	unsigned long frames_received;
+	/*! fax frames sent */
+	unsigned long frames_sent;
+	/*! the fax technology callbacks */
+	const struct ast_fax_tech *tech;
+	/*! private implementation pointer */
+	void *tech_pvt;
+	/*! fax state */
+	enum ast_fax_state state;
+	/*! name of the Asterisk channel using the fax session */
+	char *channame;
+	/*! Asterisk channel using the fax session */
+	struct ast_channel *chan;
+	/*! fax debugging structure */
+	struct ast_fax_debug_info *debug_info;
+	/*! used to take variable-sized frames in and output frames of an expected size to the fax stack */
+	struct ast_smoother *smoother;
+};
+
+struct ast_fax_tech_token;
+
+/*! \brief used to register a FAX technology module with res_fax */
+struct ast_fax_tech {
+	/*! the type of fax session supported with this ast_fax_tech structure */
+	const char * const type;
+	/*! a short description of the fax technology */
+	const char * const description;
+	/*! version string of the technology module */
+	const char * const version;
+	/*! the ast_fax_capabilities supported by the fax technology */
+	const enum ast_fax_capabilities caps;
+	/*! module information for the fax technology */
+	struct ast_module *module;
+	/*! reserves a session for future use; returns a token */
+	struct ast_fax_tech_token *(* const reserve_session)(struct ast_fax_session *);
+	/*! releases an unused session token */
+	void (* const release_token)(struct ast_fax_tech_token *);
+	/*! creates a new fax session, optionally using a previously-reserved token */
+	void *(* const new_session)(struct ast_fax_session *, struct ast_fax_tech_token *);
+	/*! destroys an existing fax session */
+	void (* const destroy_session)(struct ast_fax_session *);
+	/*! sends an Asterisk frame to res_fax */
+	struct ast_frame *(* const read)(struct ast_fax_session *);
+	/*! writes an Asterisk frame to the fax session */
+	int (* const write)(struct ast_fax_session *, const struct ast_frame *);
+	/*! starts the fax session */
+	int (* const start_session)(struct ast_fax_session *);
+	/*! cancels a fax session */
+	int (* const cancel_session)(struct ast_fax_session *);
+	/*! initiates the generation of silence to the fax session */
+	int (* const generate_silence)(struct ast_fax_session *);
+	/*! switches an existing dual-mode session from audio to T.38 */
+	int (* const switch_to_t38)(struct ast_fax_session *);
+	/*! displays capabilities of the fax technology */
+	char * (* const cli_show_capabilities)(int);
+	/*! displays details about the fax session */
+	char * (* const cli_show_session)(struct ast_fax_session *, int);
+	/*! displays statistics from the fax technology module */
+	char * (* const cli_show_stats)(int);
+};
+  
+/*! \brief register a fax technology */
+int ast_fax_tech_register(struct ast_fax_tech *tech);
+
+/*! \brief unregister a fax technology */
+void ast_fax_tech_unregister(struct ast_fax_tech *tech);
+
+/*! \brief get the minimum supported fax rate */
+unsigned int ast_fax_minrate(void);
+
+/*! \brief get the maxiumum supported fax rate */
+unsigned int ast_fax_maxrate(void);
+
+/*! \brief convert an ast_fax_state to a string */
+const char *ast_fax_state_to_str(enum ast_fax_state state);
+
+#endif

Propchange: team/group/res_fax/include/asterisk/res_fax.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/group/res_fax/include/asterisk/res_fax.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/group/res_fax/include/asterisk/res_fax.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: team/group/res_fax/res/res_fax.c
URL: http://svnview.digium.com/svn/asterisk/team/group/res_fax/res/res_fax.c?view=auto&rev=232810
==============================================================================
--- team/group/res_fax/res/res_fax.c (added)
+++ team/group/res_fax/res/res_fax.c Thu Dec  3 07:49:13 2009
@@ -1,0 +1,1813 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2008-2009, Digium, Inc.
+ *
+ * Dwayne M. Hubbard <dhubbard at digium.com>
+ * Kevin P. Fleming <kpfleming at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*! \file
+ *
+ * \brief Generic FAX Resource for FAX technology resource modules
+ *
+ * \author Dwayne M. Hubbard <dhubbard at digium.com>
+ * \author Kevin P. Fleming <kpfleming at digium.com>
+ * 
+ * A generic FAX resource module that provides SendFAX and ReceiveFAX applications.
+ * This module requires FAX technology modules, like res_fax_spandsp, to register with it
+ * so it can use the technology modules to perform the actual FAX transmissions.
+ * \ingroup applications
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include "asterisk/io.h"
+#include "asterisk/file.h"
+#include "asterisk/logger.h"
+#include "asterisk/module.h"
+#include "asterisk/app.h"
+#include "asterisk/lock.h"
+#include "asterisk/options.h"
+#include "asterisk/strings.h"
+#include "asterisk/cli.h"
+#include "asterisk/utils.h"
+#include "asterisk/config.h"
+#include "asterisk/astobj2.h"
+#include "asterisk/file.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/manager.h"
+#include "asterisk/dsp.h"
+#include "asterisk/res_fax.h"
+
+static const char app_receivefax[] = "ReceiveFAX";
+static const char synopsis_receivefax[] = "Receive a FAX and save as a TIFF/F file.";
+static const char descrip_receivefax[] = "ReceiveFAX(filename[,options]):\n"
+ " The ReceiveFAX() application receives a FAX as a TIFF/F file with specified filename.\n"
+ " The application arguments are:\n"
+ "    'd' - enables FAX debugging\n"
+ "    'f' - allow audio fallback FAX transfer on T.38 capable channels\n"
+ "    's' - send progress Manager events (overrides statusevents setting in res_fax.conf)\n"
+ "\n"
+ " Use the FAXOPT function to specify session arguments prior to calling ReceiveFAX()\n"
+ " and use FAXOPT after ReceiveFAX completes to query result status for the session.\n"
+ " The ReceiveFAX() is provided by res_fax, which is a FAX technology agnostic module\n"
+ " that utilizes FAX technology resource modules to complete a FAX transmission.\n";
+
+static const char app_sendfax[] = "SendFAX";
+static const char synopsis_sendfax[] = "Sends a specified TIFF/F file as a FAX.";
+static const char descrip_sendfax[] = "SendFAX(filename[,options]):\n"
+ " The SendFAX() application sends the specified TIFF/F file as a FAX.\n"
+ " The application arguments are:\n"
+ "    'd' - enables FAX debugging\n"
+ "    'f' - allow audio fallback FAX transfer on T.38 capable channels\n"
+ "    's' - send progress Manager events (overrides statusevents setting in res_fax.conf)\n"
+ "\n"
+ " Use the FAXOPT function to specify session arguments prior to calling SendFAX()\n"
+ " and use FAXOPT after SendFAX completes to query result status for the session.\n"
+ " The SendFAX() application is provided by res_fax, which is a FAX technology agnostic module\n"
+ " that utilizes FAX technology resource modules to complete a FAX transmission.\n";
+
+struct debug_info_history {
+	unsigned int consec_frames;
+	unsigned int consec_ms;
+	unsigned char silence;
+};
+
+struct ast_fax_debug_info {
+	struct timeval base_tv;
+	struct debug_info_history c2s, s2c;
+	struct ast_dsp *dsp;
+};
+
+/*! \brief maximum buckets for res_fax ao2 containers */
+#define FAX_MAXBUCKETS 10
+
+#define RES_FAX_TIMEOUT 10000
+
+/*! \brief The faxregistry is used to manage information and statistics for all FAX sessions. */
+static struct {
+	/*! The number of active FAX sessions */
+	int active_sessions;
+	/*! active sessions are astobj2 objects */
+	struct ao2_container *container;
+	/*! Total number of Tx FAX attempts */
+	int fax_tx_attempts;
+	/*! Total number of Rx FAX attempts */
+	int fax_rx_attempts;
+	/*! Number of successful FAX transmissions */
+	int fax_complete;
+	/*! Number of failed FAX transmissions */
+	int fax_failures;
+	/*! the next unique session name */
+	int nextsessionname;
+} faxregistry;
+
+/*! \brief registered FAX technology modules are put into this list */
+struct fax_module {
+	const struct ast_fax_tech *tech;
+	AST_RWLIST_ENTRY(fax_module) list;
+};
+static AST_RWLIST_HEAD_STATIC(faxmodules, fax_module);
+
+#define RES_FAX_MINRATE 2400
+#define RES_FAX_MAXRATE 14400
+#define RES_FAX_STATUSEVENTS 0
+#define RES_FAX_MODEM (AST_FAX_MODEM_V17 | AST_FAX_MODEM_V27 | AST_FAX_MODEM_V29)
+
+static struct {
+	enum ast_fax_modems modems;
+	uint32_t statusevents:1;
+	unsigned int minrate;	
+	unsigned int maxrate;
+} general_options;
+
+static const char *config = "res_fax.conf";
+
+static int global_fax_debug = 0;
+
+enum {
+	OPT_CALLEDMODE = (1 << 0),
+	OPT_CALLERMODE = (1 << 1),
+	OPT_DEBUG      = (1 << 2),
+	OPT_STATUS     = (1 << 3),
+	OPT_ALLOWAUDIO = (1 << 5),
+};
+
+AST_APP_OPTIONS(fax_exec_options, BEGIN_OPTIONS
+	AST_APP_OPTION('a', OPT_CALLEDMODE),
+	AST_APP_OPTION('c', OPT_CALLERMODE),
+	AST_APP_OPTION('d', OPT_DEBUG),
+	AST_APP_OPTION('f', OPT_ALLOWAUDIO),
+	AST_APP_OPTION('s', OPT_STATUS),
+END_OPTIONS);
+
+struct manager_event_info {
+	char context[AST_MAX_CONTEXT];
+	char exten[AST_MAX_EXTENSION];
+	char cid[128];
+};
+
+static void debug_check_frame_for_silence(struct ast_fax_session *s, unsigned int c2s, struct ast_frame *frame)
+{	
+	struct debug_info_history *history = c2s ? &s->debug_info->c2s : &s->debug_info->s2c;
+	int dspsilence;
+	unsigned int last_consec_frames, last_consec_ms;
+	unsigned char wassil;
+	struct timeval diff;
+
+	diff = ast_tvsub(ast_tvnow(), s->debug_info->base_tv);
+
+	ast_dsp_reset(s->debug_info->dsp);
+	ast_dsp_silence(s->debug_info->dsp, frame, &dspsilence);
+
+	wassil = history->silence;
+	history->silence = (dspsilence != 0) ? 1 : 0;
+	if (history->silence != wassil) {
+		last_consec_frames = history->consec_frames;
+		last_consec_ms = history->consec_ms;
+		history->consec_frames = 0;
+		history->consec_ms = 0;
+
+		if ((last_consec_frames != 0)) {
+			ast_verb(6, "Channel '%s' fax session '%d', [ %.3ld.%.6ld ], %s sent %d frames (%d ms) of %s.\n",
+				 s->channame, s->id, (long) diff.tv_sec, (long int) diff.tv_usec,
+				 (c2s) ? "channel" : "stack", last_consec_frames, last_consec_ms,
+				 (wassil) ? "silence" : "energy");
+		}
+	}
+
+	history->consec_frames++;
+	history->consec_ms += (frame->samples / 8);
+}
+
+static void destroy_callback(void *data) 
+{
+	if (data) {
+		ao2_ref(data, -1);
+	}
+}
+
+static const struct ast_datastore_info fax_datastore = {
+	.type = "res_fax",
+	.destroy = destroy_callback,
+};
+
+/*! \brief returns a reference counted pointer to a fax datastore, if it exists */
+static struct ast_fax_session_details *find_details(struct ast_channel *chan)
+{
+	struct ast_fax_session_details *details;
+	struct ast_datastore *datastore;
+
+	ast_channel_lock(chan);	
+	if (!(datastore = ast_channel_datastore_find(chan, &fax_datastore, NULL))) {
+		ast_channel_unlock(chan);	
+		return NULL;
+	}
+	if (!(details = datastore->data)) {
+		ast_log(LOG_WARNING, "Huh?  channel '%s' has a FAX datastore without data!\n", chan->name);
+		ast_channel_unlock(chan);
+		return NULL;
+	}
+	ao2_ref(details, 1);	
+	ast_channel_unlock(chan);	
+
+	return details;
+}
+
+/*! \brief destroy a FAX session details structure */
+static void destroy_session_details(void *details)
+{
+	struct ast_fax_session_details *d = details;
+	struct ast_fax_document *doc;
+	
+	while ((doc = AST_LIST_REMOVE_HEAD(&d->documents, next))) {
+		ast_free(doc);
+	}
+	ast_string_field_free_memory(d);	
+}
+
+/*! \brief create a FAX session details structure */
+static struct ast_fax_session_details *session_details_new(void)
+{
+	struct ast_fax_session_details *d;
+
+	if (!(d = ao2_alloc(sizeof(*d), destroy_session_details))) {
+		return NULL;
+	}
+	
+	if (ast_string_field_init(d, 512)) {
+		ao2_ref(d, -1);
+		return NULL;
+	}
+
+	AST_LIST_HEAD_INIT_NOLOCK(&d->documents);
+
+	/* These options need to be set to the configured default and may be overridden by
+ 	 * SendFAX, ReceiveFAX, or FAXOPT */
+	d->option.ecm = AST_FAX_OPTFLAG_DEFAULT;
+	d->option.statusevents = general_options.statusevents;
+	d->modems = general_options.modems;
+	d->minrate = general_options.minrate;
+	d->maxrate = general_options.maxrate;
+
+	return d;
+}
+
+/*! \brief returns a reference counted details structure from the channel's fax datastore.  If the datastore
+ * does not exist it will be created */	
+static struct ast_fax_session_details *find_or_create_details(struct ast_channel *chan)
+{
+	struct ast_fax_session_details *details;
+	struct ast_datastore *datastore;
+
+	if ((details = find_details(chan))) {
+		return details;
+	}
+	/* channel does not have one so we must create one */
+	if (!(details = session_details_new())) {
+		ast_log(LOG_WARNING, "channel '%s' can't get a FAX details structure for the datastore!\n", chan->name);
+		return NULL;
+	}
+	if (!(datastore = ast_datastore_alloc(&fax_datastore, NULL))) {
+		ao2_ref(details, -1);
+		ast_log(LOG_WARNING, "channel '%s' can't get a datastore!\n", chan->name);
+		return NULL;
+	}
+	/* add the datastore to the channel and increment the refcount */
+	datastore->data = details;
+	ao2_ref(details, 1);
+	ast_channel_lock(chan);
+	ast_channel_datastore_add(chan, datastore);
+	ast_channel_unlock(chan);
+	return details;
+}
+
+unsigned int ast_fax_maxrate(void)
+{
+	return general_options.maxrate;
+}
+
+unsigned int ast_fax_minrate(void)
+{
+	return general_options.minrate;
+}
+
+static int update_modem_bits(enum ast_fax_modems *bits, const char *value)
+{		
+	char *m[5], *tok, *v = (char *)value;
+	int i = 0, j;
+
+	if (!(tok = strchr(v, ','))) {
+		m[i++] = v;
+		m[i] = NULL;
+	} else {
+		tok = strtok(v, ", ");
+		while (tok && (i < 5)) {
+			m[i++] = tok;
+			tok = strtok(NULL, ", ");
+		}
+		m[i] = NULL;
+	}
+
+	*bits = 0;
+	for (j = 0; j < i; j++) {
+		if (!strcasecmp(m[j], "v17")) {
+			*bits |= AST_FAX_MODEM_V17;
+		} else if (!strcasecmp(m[j], "v27")) {
+			*bits |= AST_FAX_MODEM_V27;
+		} else if (!strcasecmp(m[j], "v29")) {
+			*bits |= AST_FAX_MODEM_V29;
+		} else if (!strcasecmp(m[j], "v34")) {
+			*bits |= AST_FAX_MODEM_V34;
+		} else {
+			ast_log(LOG_WARNING, "ignoring invalid modem setting: '%s', valid options {v17 | v27 | v29 | v34}\n", m[j]);
+		}
+	}
+	return 0;
+}
+
+static int ast_fax_modem_to_str(enum ast_fax_modems bits, char *tbuf, size_t bufsize)
+{
+	int count = 0;
+
+	if (bits & AST_FAX_MODEM_V17) {
+		strcat(tbuf, "V17");
+		count++;
+	}
+	if (bits & AST_FAX_MODEM_V27) {
+		if (count) {
+			strcat(tbuf, ",");
+		}
+		strcat(tbuf, "V27");
+		count++;
+	}
+	if (bits & AST_FAX_MODEM_V29) {
+		if (count) {
+			strcat(tbuf, ",");
+		}
+		strcat(tbuf, "V29");
+		count++;
+	}
+	if (bits & AST_FAX_MODEM_V34) {
+		if (count) {
+			strcat(tbuf, ",");
+		}
+		strcat(tbuf, "V34");
+		count++;
+	}
+
+	return 0;
+}
+
+/*! \brief register a FAX technology module */
+int ast_fax_tech_register(struct ast_fax_tech *tech)
+{
+	struct fax_module *fax;
+
+	if (!(fax = ast_calloc(1, sizeof(*fax)))) {
+		return -1;
+	}
+	fax->tech = tech;
+	AST_RWLIST_WRLOCK(&faxmodules);
+	AST_RWLIST_INSERT_TAIL(&faxmodules, fax, list);
+	AST_RWLIST_UNLOCK(&faxmodules);
+	ast_module_ref(ast_module_info->self);
+
+	ast_verb(3, "Registered handler for '%s' (%s)\n", fax->tech->type, fax->tech->description);
+
+	return 0;
+}
+
+/*! \brief unregister a FAX technology module */
+void ast_fax_tech_unregister(struct ast_fax_tech *tech)
+{
+	struct fax_module *fax;
+
+	ast_verb(3, "Unregistering FAX module type '%s'\n", tech->type);
+
+	AST_RWLIST_WRLOCK(&faxmodules);
+	AST_RWLIST_TRAVERSE_SAFE_BEGIN(&faxmodules, fax, list) {
+		if (fax->tech != tech) {
+			continue;
+		}
+		AST_RWLIST_REMOVE_CURRENT(list);
+		ast_module_unref(ast_module_info->self);
+		ast_free(fax);
+		ast_verb(4, "Unregistered FAX module type '%s'\n", tech->type);
+		break;	
+	}
+	AST_RWLIST_TRAVERSE_SAFE_END;
+	AST_RWLIST_UNLOCK(&faxmodules);
+}
+
+/*! \brief convert a ast_fax_state to a string */
+const char *ast_fax_state_to_str(enum ast_fax_state state)
+{
+	switch (state) {
+	case AST_FAX_STATE_UNINITIALIZED:
+		return "Uninitialized";
+	case AST_FAX_STATE_INITIALIZED:
+		return "Initialized";
+	case AST_FAX_STATE_OPEN:
+		return "Open";
+	case AST_FAX_STATE_ACTIVE:
+		return "Active";
+	case AST_FAX_STATE_COMPLETE:
+		return "Complete";
+	default:
+		ast_log(LOG_WARNING, "unhandled FAX state: %d\n", state);
+		return "Unknown";
+	}
+}
+
+/*! \brief convert a rate string to a rate */
+static int fax_rate_str_to_int(const char *ratestr)
+{
+	int rate;
+
+	if (sscanf(ratestr, "%d", &rate) != 1) {
+		ast_log(LOG_ERROR, "failed to sscanf '%s' to rate\n", ratestr);
+		return -1;
+	}
+	switch (rate) {
+	case 2400:
+	case 4800:
+	case 7200:
+	case 9600:
+	case 12000:
+	case 14400:
+	case 28800:
+	case 33600:
+		return rate;
+	default:
+		ast_log(LOG_WARNING, "ignoring invalid rate '%s'.  Valid options are {2400 | 4800 | 7200 | 9600 | 12000 | 14400 | 28800 | 33600}\n", ratestr);
+		return -1;
+	}
+}
+
+/*! \brief destroy a FAX session structure */
+static void destroy_session(void *session)
+{
+	struct ast_fax_session *s = session;
+
+	if (s->tech) {
+		if (s->tech_pvt) {
+			s->tech->destroy_session(s);
+		}
+		ast_module_unref(s->tech->module);
+	}
+
+	if (s->details) {
+		ao2_ref(s->details, -1);
+	}
+	
+	if (s->debug_info) {
+		ast_dsp_free(s->debug_info->dsp);
+		ast_free(s->debug_info);
+	}
+
+	if (s->smoother) {
+		ast_smoother_free(s->smoother);
+	}
+
+	ast_atomic_fetchadd_int(&faxregistry.active_sessions, -1);
+
+	ast_free(s->channame);
+}
+
+/*! \brief create a FAX session */
+static struct ast_fax_session *fax_session_new(struct ast_fax_session_details *details, struct ast_channel *chan)
+{
+	struct ast_fax_session *s;
+	struct fax_module *faxmod;
+
+	if (!(s = ao2_alloc(sizeof(*s), destroy_session))) {
+		return NULL;
+	}
+
+	ast_atomic_fetchadd_int(&faxregistry.active_sessions, 1);
+
+	if (details->option.debug && (details->caps & AST_FAX_TECH_AUDIO)) {
+		if (!(s->debug_info = ast_calloc(1, sizeof(*(s->debug_info))))) {
+			ao2_ref(s, -1);
+			return NULL;
+		}
+		if (!(s->debug_info->dsp = ast_dsp_new())) {
+			ast_free(s->debug_info);
+			s->debug_info = NULL;
+			ao2_ref(s, -1);
+			return NULL;
+		}
+		ast_dsp_set_threshold(s->debug_info->dsp, 128);
+	}	
+
+	if (!(s->channame = ast_strdup(chan->name))) {
+		ao2_ref(s, -1);
+		return NULL;
+	}
+	s->chan = chan;
+	s->details = details;
+	ao2_ref(s->details, 1);
+	s->state = AST_FAX_STATE_UNINITIALIZED;
+
+	details->id = s->id = ast_atomic_fetchadd_int(&faxregistry.nextsessionname, 1);
+
+	/* locate a FAX technology module that can handle said requirements */
+	AST_RWLIST_RDLOCK(&faxmodules);
+	AST_RWLIST_TRAVERSE(&faxmodules, faxmod, list) {
+		if ((faxmod->tech->caps & details->caps) != details->caps) {
+			continue;
+		}
+		ast_debug(4, "Requesting a new FAX session from '%s'.\n", faxmod->tech->description);
+		ast_module_ref(faxmod->tech->module);
+		s->tech = faxmod->tech;
+		break;
+	}
+	AST_RWLIST_UNLOCK(&faxmodules);
+
+	if (!faxmod) {
+		ast_log(LOG_ERROR, "Could not locate a FAX technology module with capabilities (0x%X)\n", details->caps);
+		ao2_ref(s, -1);
+		return NULL;
+	}
+	if (!(s->tech_pvt = s->tech->new_session(s, NULL))) {
+		ast_log(LOG_ERROR, "FAX session failed to initialize.\n");
+		ao2_ref(s, -1);
+		ast_module_unref(faxmod->tech->module);
+		return NULL;
+	}
+	/* link the session to the session container */
+	if (!(ao2_link(faxregistry.container, s))) {
+		ast_log(LOG_ERROR, "failed to add FAX session '%d' to container.\n", s->id);
+		ao2_ref(s, -1);
+		ast_module_unref(faxmod->tech->module);
+		return NULL;
+	}
+	ast_debug(4, "channel '%s' using FAX session '%d'\n", s->channame, s->id);
+
+	return s;
+}
+
+static void get_manager_event_info(struct ast_channel *chan, struct manager_event_info *info)
+{
+	pbx_substitute_variables_helper(chan, "${CONTEXT}", info->context, sizeof(info->context));
+	pbx_substitute_variables_helper(chan, "${EXTEN}", info->exten, sizeof(info->exten));
+	pbx_substitute_variables_helper(chan, "${CALLERID(num)}", info->cid, sizeof(info->cid));
+}
+
+/*! \brief send a FAX status manager event */
+static int report_fax_status(struct ast_channel *chan, struct ast_fax_session_details *details, const char *status)
+{
+	ast_channel_lock(chan);
+	pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", status);
+	if (details->option.statusevents) {
+		struct manager_event_info info;
+
+		get_manager_event_info(chan, &info);
+		manager_event(EVENT_FLAG_CALL,
+			      (details->caps & AST_FAX_TECH_RECEIVE) ? "ReceiveFAXStatus" : "SendFAXStatus",
+			      "Status: %s\r\n"
+			      "Channel: %s\r\n"
+			      "Context: %s\r\n"
+			      "Exten: %s\r\n"
+			      "CallerID: %s\r\n"
+			      "LocalStationID: %s\r\n"
+			      "FileName: %s\r\n",
+			      status,
+			      chan->name,
+			      info.context,
+			      info.exten,
+			      info.cid,
+			      details->localstationid,
+			      AST_LIST_FIRST(&details->documents)->filename);
+	}
+	ast_channel_unlock(chan);
+
+	return 0;
+}
+
+#define GENERIC_FAX_EXEC_ERROR(fax, chan, reason)	\
+	do {	\
+		ast_log(LOG_ERROR, "channel '%s' FAX session '%d' failure, reason: '%s'\n", chan->name, fax->id, reason); \
+		pbx_builtin_setvar_helper(chan, "FAXSTATUSSTRING", reason); \
+		if (ast_strlen_zero(fax->details->result)) ast_string_field_set(fax->details, result, "FAILED"); \
+		res = ms = -1; \
+	} while (0)
+
+static void t38_parameters_ast_to_fax(struct ast_fax_t38_parameters *dst, const struct ast_control_t38_parameters *src)
+{
+	dst->version = src->version;
+	dst->max_ifp = src->max_ifp;
+	dst->rate = src->rate;
+	dst->rate_management = src->rate_management;
+	dst->fill_bit_removal = src->fill_bit_removal;
+	dst->transcoding_mmr = src->transcoding_mmr;
+	dst->transcoding_jbig = src->transcoding_jbig;
+}
+
+static void t38_parameters_fax_to_ast(struct ast_control_t38_parameters *dst, const struct ast_fax_t38_parameters *src)
+{
+	dst->version = src->version;
+	dst->max_ifp = src->max_ifp;
+	dst->rate = src->rate;
+	dst->rate_management = src->rate_management;
+	dst->fill_bit_removal = src->fill_bit_removal;
+	dst->transcoding_mmr = src->transcoding_mmr;
+	dst->transcoding_jbig = src->transcoding_jbig;
+}
+
+/*! \brief this is the generic FAX session handling function */
+static int generic_fax_exec(struct ast_channel *chan, struct ast_fax_session_details *details)
+{
+	int ms = 1000;
+	int timeout = RES_FAX_TIMEOUT;
+	int res = 0, chancount;
+	unsigned int expected_frametype = -1;
+	union ast_frame_subclass expected_framesubclass = { .integer = -1 };
+	char tbuf[10];
+	unsigned int t38negotiated = 0;
+	enum ast_t38_state t38_state;
+	struct ast_control_t38_parameters t38_parameters;
+	int send_cng = -1;
+	unsigned int disable_t38 = 0;
+	const char *tempvar;
+	struct ast_fax_session *fax = NULL;
+	struct ast_frame *frame = NULL;
+	struct ast_channel *c = chan;
+	unsigned int orig_write_format = 0, orig_read_format = 0;
+	unsigned int request_t38 = 0;
+
+	details->our_t38_parameters.version = 0;
+	details->our_t38_parameters.max_ifp = 400;
+	details->our_t38_parameters.rate = AST_T38_RATE_14400;
+	details->our_t38_parameters.rate_management = AST_T38_RATE_MANAGEMENT_TRANSFERRED_TCF;
+
+	chancount = 1;
+
+	switch ((t38_state = ast_channel_get_t38_state(chan))) {
+	case T38_STATE_UNKNOWN:
+		if (details->caps & AST_FAX_TECH_SEND) {
+			if (details->option.allow_audio) {
+				details->caps |= AST_FAX_TECH_AUDIO;
+			} else {
+				/* we are going to send CNG to attempt to stimulate the receiver
+				 * into switching to T.38, since audio mode is not allowed
+				 */
+				send_cng = 0;
+			}
+		} else {
+			/* we *always* request a switch to T.38 if allowed; if audio is also
+			 * allowed, then we will allow the switch to happen later if needed
+			 */
+			if (details->option.allow_audio) {
+				details->caps |= AST_FAX_TECH_AUDIO;
+			}
+			request_t38 = 1;
+		}
+		details->caps |= AST_FAX_TECH_T38;
+		break;
+	case T38_STATE_UNAVAILABLE:
+		details->caps |= AST_FAX_TECH_AUDIO;
+		break;
+	default:
+		ast_log(LOG_ERROR, "channel '%s' is in an unsupported T.38 negotiation state, cannot continue.\n", chan->name);
+		return -1;
+	}
+
+	if (request_t38) {
+		/* wait up to five seconds for negotiation to complete */
+		timeout = 5000;
+
+		/* set parameters based on the session's parameters */
+		t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
+		t38_parameters.request_response = AST_T38_REQUEST_NEGOTIATE;
+		if ((ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters)) != 0)) {
+			res = -1;
+			goto t38done;
+		}
+
+		ast_log(LOG_NOTICE, "Negotiating T.38 for %s on %s\n", (details->caps & AST_FAX_TECH_SEND) ? "send" : "receive", chan->name);
+	} else if (!details->option.allow_audio) {
+		/* wait up to sixty seconds for negotiation to complete */
+		timeout = 60000;
+
+		ast_log(LOG_NOTICE, "Waiting for T.38 negotiation for %s on %s\n", (details->caps & AST_FAX_TECH_SEND) ? "send" : "receive", chan->name);
+	}
+
+	if (request_t38 || !details->option.allow_audio) {
+		while (timeout > 0) {
+			if (send_cng > 3000) {
+				ast_tonepair_start(chan, 1100, 0, 500, 0);
+				send_cng = 0;
+			}
+			/* this timeout *MUST* be 500ms, in order to keep the spacing
+			 * of CNG tones correct when this loop is sending them
+			 */
+			ms = ast_waitfor(chan, 500);
+			if (ms < 0) {
+				ast_log(LOG_WARNING, "something bad happened while channel '%s' was polling.\n", chan->name);
+				res = -1;
+				break;
+			}
+			if (send_cng != -1) {
+				send_cng += 500 - ms;
+			}
+			if (!ms) {
+				/* nothing happened */
+				if (timeout > 0) {
+					timeout -= 500;
+					continue;
+				} else {
+					ast_log(LOG_WARNING, "channel '%s' timed-out during the T.38 negotiation.\n", chan->name);
+					res = -1;
+					break;
+				}
+			}
+			if (!(frame = ast_read(chan))) {
+				return -1;
+			}
+			if ((frame->frametype == AST_FRAME_CONTROL) &&
+			    (frame->subclass.integer == AST_CONTROL_T38_PARAMETERS) &&
+			    (frame->datalen == sizeof(t38_parameters))) {
+				struct ast_control_t38_parameters *parameters = frame->data.ptr;
+				int stop = 1;
+				
+				switch (parameters->request_response) {
+				case AST_T38_REQUEST_NEGOTIATE:
+					t38_parameters_fax_to_ast(&t38_parameters, &details->our_t38_parameters);
+					t38_parameters.request_response = AST_T38_NEGOTIATED;
+					ast_indicate_data(chan, AST_CONTROL_T38_PARAMETERS, &t38_parameters, sizeof(t38_parameters));
+					stop = 0;
+					break;
+				case AST_T38_NEGOTIATED:
+					ast_log(LOG_NOTICE, "Negotiated T.38 for %s on %s\n", (details->caps & AST_FAX_TECH_SEND) ? "send" : "receive", chan->name);
+					t38_parameters_ast_to_fax(&details->their_t38_parameters, parameters);
+					details->caps &= ~AST_FAX_TECH_AUDIO;
+					t38negotiated = 1;
+					disable_t38 = 1;
+					break;
+				case AST_T38_REFUSED:
+					ast_log(LOG_WARNING, "channel '%s' refused to negotiate T.38\n", chan->name);
+					res = -1;
+					break;
+				default:
+					ast_log(LOG_ERROR, "channel '%s' failed to negotiate T.38\n", chan->name);
+					res = -1;
+					break;
+				}
+				if (stop) {
+					ast_frfree(frame);
+					break;
+				}
+			}
+			ast_frfree(frame);
+		}
+	}
+
+t38done:
+	/* if any failures occurred during T.38 negotiation, handle them here */
+	if (res) {
+		/* if audio is allowed, then drop the T.38 session requirement
+		 * and proceed, otherwise the request has failed
+		 */
+		if (details->option.allow_audio) {
+			details->caps &= ~AST_FAX_TECH_T38;
+			res = 0;
+		} else {
+			ast_log(LOG_WARNING, "Audio FAX not allowed on channel '%s' and T.38 negotiation failed; aborting.\n", chan->name);
+			return -1;
+		}
+	}
+
+	/* create the FAX session */
+	if (!(fax = fax_session_new(details, chan))) {
+		ast_log(LOG_ERROR, "Can't create a FAX session, FAX attempt failed.\n");
+		report_fax_status(chan, details, "No Available Resource");
+		chancount = -1;
+		goto disable_t38;
+	}
+
+	ast_channel_lock(chan);
+	/* update session details */	
+	if (ast_strlen_zero(details->headerinfo) && (tempvar = pbx_builtin_getvar_helper(chan, "LOCALHEADERINFO"))) {
+		ast_string_field_set(details, headerinfo, tempvar);
+	}
+	if (ast_strlen_zero(details->localstationid)) {
+		tempvar = pbx_builtin_getvar_helper(chan, "LOCALSTATIONID");
+		ast_string_field_set(details, localstationid, tempvar ? tempvar : "unknown");
+	}
+	ast_channel_unlock(chan);
+
+	report_fax_status(chan, details, "Allocating Resources");
+
+	if (details->caps & AST_FAX_TECH_AUDIO) {
+		expected_frametype = AST_FRAME_VOICE;;
+		expected_framesubclass.codec = AST_FORMAT_SLINEAR;
+		orig_write_format = chan->writeformat;
+		if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
+			ast_log(LOG_ERROR, "channel '%s' failed to set write format to signed linear'.\n", chan->name);
+ 			ao2_lock(faxregistry.container);
+ 			ao2_unlink(faxregistry.container, fax);
+ 			ao2_unlock(faxregistry.container);
+ 			ao2_ref(fax, -1);
+			ast_channel_unlock(chan);
+			return -1;
+		}
+		orig_read_format = chan->readformat;
+		if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
+			ast_log(LOG_ERROR, "channel '%s' failed to set read format to signed linear.\n", chan->name);
+ 			ao2_lock(faxregistry.container);
+ 			ao2_unlink(faxregistry.container, fax);
+ 			ao2_unlock(faxregistry.container);
+ 			ao2_ref(fax, -1);
+			ast_channel_unlock(chan);
+			return -1;
+		}
+		if (fax->smoother) {
+			ast_smoother_free(fax->smoother);
+			fax->smoother = NULL;
+		}
+		if (!(fax->smoother = ast_smoother_new(320))) {
+			ast_log(LOG_WARNING, "Channel '%s' FAX session '%d' failed to obtain a smoother.\n", chan->name, fax->id);
+		}
+	} else {
+		expected_frametype = AST_FRAME_MODEM;
+		expected_framesubclass.codec = AST_MODEM_T38;
+	}
+

[... 1005 lines stripped ...]



More information about the svn-commits mailing list