[asterisk-commits] file: branch 1.4 r52049 - in /branches/1.4: apps/ include/asterisk/ main/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Jan 24 11:20:06 MST 2007


Author: file
Date: Wed Jan 24 12:20:05 2007
New Revision: 52049

URL: http://svn.digium.com/view/asterisk?view=rev&rev=52049
Log:
Merge in dialing API and the app_page that uses it. (issue #BE-118)

Added:
    branches/1.4/include/asterisk/dial.h   (with props)
    branches/1.4/main/dial.c   (with props)
Modified:
    branches/1.4/apps/app_page.c
    branches/1.4/main/Makefile

Modified: branches/1.4/apps/app_page.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/apps/app_page.c?view=diff&rev=52049&r1=52048&r2=52049
==============================================================================
--- branches/1.4/apps/app_page.c (original)
+++ branches/1.4/apps/app_page.c Wed Jan 24 12:20:05 2007
@@ -48,6 +48,8 @@
 #include "asterisk/app.h"
 #include "asterisk/chanvars.h"
 #include "asterisk/utils.h"
+#include "asterisk/dial.h"
+#include "asterisk/devicestate.h"
 
 static const char *app_page= "Page";
 
@@ -63,7 +65,6 @@
 "        q - quiet, do not play beep to caller\n"
 "        r - record the page into a file (see 'r' for app_meetme)\n";
 
-
 enum {
 	PAGE_DUPLEX = (1 << 0),
 	PAGE_QUIET = (1 << 1),
@@ -76,82 +77,18 @@
 	AST_APP_OPTION('r', PAGE_RECORD),
 });
 
-struct calloutdata {
-	char cidnum[64];
-	char cidname[64];
-	char tech[64];
-	char resource[256];
-	char meetmeopts[64];
-	struct ast_variable *variables;
-};
-
-static void *page_thread(void *data)
-{
-	struct calloutdata *cd = data;
-	ast_pbx_outgoing_app(cd->tech, AST_FORMAT_SLINEAR, cd->resource, 30000,
-		"MeetMe", cd->meetmeopts, NULL, 0, cd->cidnum, cd->cidname, cd->variables, NULL, NULL);
-	free(cd);
-	return NULL;
-}
-
-static void launch_page(struct ast_channel *chan, const char *meetmeopts, const char *tech, const char *resource)
-{
-	struct calloutdata *cd;
-	const char *varname;
-	struct ast_variable *lastvar = NULL;
-	struct ast_var_t *varptr;
-	pthread_t t;
-	pthread_attr_t attr;
-	if ((cd = ast_calloc(1, sizeof(*cd)))) {
-		ast_copy_string(cd->cidnum, chan->cid.cid_num ? chan->cid.cid_num : "", sizeof(cd->cidnum));
-		ast_copy_string(cd->cidname, chan->cid.cid_name ? chan->cid.cid_name : "", sizeof(cd->cidname));
-		ast_copy_string(cd->tech, tech, sizeof(cd->tech));
-		ast_copy_string(cd->resource, resource, sizeof(cd->resource));
-		ast_copy_string(cd->meetmeopts, meetmeopts, sizeof(cd->meetmeopts));
-
-		AST_LIST_TRAVERSE(&chan->varshead, varptr, entries) {
-			if (!(varname = ast_var_full_name(varptr)))
-				continue;
-			if (varname[0] == '_') {
-				struct ast_variable *newvar = NULL;
-
-				if (varname[1] == '_') {
-					newvar = ast_variable_new(varname, ast_var_value(varptr));
-				} else {
-					newvar = ast_variable_new(&varname[1], ast_var_value(varptr));
-				}
-
-				if (newvar) {
-					if (lastvar)
-						lastvar->next = newvar;
-					else
-						cd->variables = newvar;
-					lastvar = newvar;
-				}
-			}
-		}
-
-		pthread_attr_init(&attr);
-		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-		if (ast_pthread_create(&t, &attr, page_thread, cd)) {
-			ast_log(LOG_WARNING, "Unable to create paging thread: %s\n", strerror(errno));
-			free(cd);
-		}
-	}
-}
+#define MAX_DIALS 128
 
 static int page_exec(struct ast_channel *chan, void *data)
 {
 	struct ast_module_user *u;
-	char *options;
-	char *tech, *resource;
-	char meetmeopts[80];
+	char *options, *tech, *resource, *tmp;
+	char meetmeopts[88], originator[AST_CHANNEL_NAME];
 	struct ast_flags flags = { 0 };
 	unsigned int confid = ast_random();
 	struct ast_app *app;
-	char *tmp;
-	int res=0;
-	char originator[AST_CHANNEL_NAME];
+	int res = 0, pos = 0, i = 0;
+	struct ast_dial *dials[MAX_DIALS];
 
 	if (ast_strlen_zero(data)) {
 		ast_log(LOG_WARNING, "This application requires at least one argument (destination(s) to page)\n");
@@ -176,20 +113,42 @@
 	if (options)
 		ast_app_parse_options(page_opts, &flags, NULL, options);
 
-	snprintf(meetmeopts, sizeof(meetmeopts), "%ud|%s%sqxdw(5)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
+	snprintf(meetmeopts, sizeof(meetmeopts), "MeetMe|%ud|%s%sqxdw(5)", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "m"),
 		(ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
 
+	/* Go through parsing/calling each device */
 	while ((tech = strsep(&tmp, "&"))) {
+		struct ast_dial *dial = NULL;
+
 		/* don't call the originating device */
 		if (!strcasecmp(tech, originator))
 			continue;
 
-		if ((resource = strchr(tech, '/'))) {
-			*resource++ = '\0';
-			launch_page(chan, meetmeopts, tech, resource);
-		} else {
+		/* If no resource is available, continue on */
+		if (!(resource = strchr(tech, '/'))) {
 			ast_log(LOG_WARNING, "Incomplete destination '%s' supplied.\n", tech);
+			continue;
 		}
+
+		*resource++ = '\0';
+
+		/* Create a dialing structure */
+		if (!(dial = ast_dial_create())) {
+			ast_log(LOG_WARNING, "Failed to create dialing structure.\n");
+			continue;
+		}
+
+		/* Append technology and resource */
+		ast_dial_append(dial, tech, resource);
+
+		/* Set ANSWER_EXEC as global option */
+		ast_dial_option_global_enable(dial, AST_DIAL_OPTION_ANSWER_EXEC, meetmeopts);
+
+		/* Run this dial in async mode */
+		ast_dial_run(dial, chan, 1);
+
+		/* Put in our dialing array */
+		dials[pos++] = dial;
 	}
 
 	if (!ast_test_flag(&flags, PAGE_QUIET)) {
@@ -197,12 +156,28 @@
 		if (!res)
 			res = ast_waitstream(chan, "");
 	}
+
 	if (!res) {
 		snprintf(meetmeopts, sizeof(meetmeopts), "%ud|A%s%sqxd", confid, (ast_test_flag(&flags, PAGE_DUPLEX) ? "" : "t"), 
 			(ast_test_flag(&flags, PAGE_RECORD) ? "r" : "") );
 		pbx_exec(chan, app, meetmeopts);
 	}
 
+	/* Go through each dial attempt cancelling, joining, and destroying */
+	for (i = 0; i < pos; i++) {
+		struct ast_dial *dial = dials[i];
+
+		/* If the dial is already answered, then they will/should get kicked out by Meetme */
+		if (ast_dial_status(dial) != AST_DIAL_RESULT_ANSWERED)
+			ast_dial_join(dial);
+
+		/* Hangup all channels */
+		ast_dial_hangup(dial);
+
+		/* Destroy dialing structure */
+		ast_dial_destroy(dial);
+	}
+
 	ast_module_user_remove(u);
 
 	return -1;

Added: branches/1.4/include/asterisk/dial.h
URL: http://svn.digium.com/view/asterisk/branches/1.4/include/asterisk/dial.h?view=auto&rev=52049
==============================================================================
--- branches/1.4/include/asterisk/dial.h (added)
+++ branches/1.4/include/asterisk/dial.h Wed Jan 24 12:20:05 2007
@@ -1,0 +1,142 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp 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 Dialing API
+ */
+
+#ifndef _ASTERISK_DIAL_H
+#define _ASTERISK_DIAL_H
+
+#if defined(__cplusplus) || defined(c_plusplus)
+extern "C" {
+#endif
+
+/*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
+struct ast_dial;
+
+/*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
+struct ast_dial_channel;
+
+/*! \brief List of options that are applicable either globally or per dialed channel */
+enum ast_dial_option {
+	AST_DIAL_OPTION_RINGING,     /*! Always indicate ringing to caller */
+	AST_DIAL_OPTION_ANSWER_EXEC, /*! Execute application upon answer in async mode */
+	AST_DIAL_OPTION_MAX,         /*! End terminator -- must always remain last */
+};
+
+/*! \brief List of return codes for dial run API calls */
+enum ast_dial_result {
+	AST_DIAL_RESULT_INVALID = 0, /*! Invalid options were passed to run function */
+	AST_DIAL_RESULT_FAILED,      /*! Attempts to dial failed before reaching critical state */
+	AST_DIAL_RESULT_TRYING,      /*! Currently trying to dial */
+	AST_DIAL_RESULT_RINGING,     /*! Dial is presently ringing */
+	AST_DIAL_RESULT_PROGRESS,    /*! Dial is presently progressing */
+	AST_DIAL_RESULT_PROCEEDING,  /*! Dial is presently proceeding */
+	AST_DIAL_RESULT_ANSWERED,    /*! A channel was answered */
+	AST_DIAL_RESULT_TIMEOUT,     /*! Timeout was tripped, nobody answered */
+	AST_DIAL_RESULT_HANGUP,      /*! Caller hung up */
+	AST_DIAL_RESULT_UNANSWERED,  /*! Nobody answered */
+};
+
+/*! \brief New dialing structure
+ * \note Create a dialing structure
+ * \return Returns a calloc'd ast_dial structure, NULL on failure
+ */
+struct ast_dial *ast_dial_create(void);
+
+/*! \brief Append a channel
+ * \note Appends a channel to a dialing structure
+ * \return Returns channel reference number on success, -1 on failure
+ */
+int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device);
+
+/*! \brief Execute dialing synchronously or asynchronously
+ * \note Dials channels in a dial structure.
+ * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
+ */
+enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async);
+
+/*! \brief Return channel that answered
+ * \note Returns the Asterisk channel that answered
+ * \param dial Dialing structure
+ */
+struct ast_channel *ast_dial_answered(struct ast_dial *dial);
+
+/*! \brief Return status of dial
+ * \note Returns the status of the dial attempt
+ * \param dial Dialing structure
+ */
+enum ast_dial_result ast_dial_status(struct ast_dial *dial);
+
+/*! \brief Cancel async thread
+ * \note Cancel a running async thread
+ * \param dial Dialing structure
+ */
+enum ast_dial_result ast_dial_join(struct ast_dial *dial);
+
+/*! \brief Hangup channels
+ * \note Hangup all active channels
+ * \param dial Dialing structure
+ */
+void ast_dial_hangup(struct ast_dial *dial);
+
+/*! \brief Destroys a dialing structure
+ * \note Cancels dialing and destroys (free's) the given ast_dial structure
+ * \param dial Dialing structure to free
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_destroy(struct ast_dial *dial);
+
+/*! \brief Enables an option globally
+ * \param dial Dial structure to enable option on
+ * \param option Option to enable
+ * \param data Data to pass to this option (not always needed)
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data);
+
+/*! \brief Enables an option per channel
+ * \param dial Dial structure
+ * \param num Channel number to enable option on
+ * \param option Option to enable
+ * \param data Data to pass to this option (not always needed)
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data);
+
+/*! \brief Disables an option globally
+ * \param dial Dial structure to disable option on
+ * \param option Option to disable
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option);
+
+/*! \brief Disables an option per channel
+ * \param dial Dial structure
+ * \param num Channel number to disable option on
+ * \param option Option to disable
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option);
+
+#if defined(__cplusplus) || defined(c_plusplus)
+}
+#endif
+
+#endif /* _ASTERISK_DIAL_H */

Propchange: branches/1.4/include/asterisk/dial.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: branches/1.4/include/asterisk/dial.h
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: branches/1.4/include/asterisk/dial.h
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: branches/1.4/main/Makefile
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/Makefile?view=diff&rev=52049&r1=52048&r2=52049
==============================================================================
--- branches/1.4/main/Makefile (original)
+++ branches/1.4/main/Makefile Wed Jan 24 12:20:05 2007
@@ -26,7 +26,7 @@
 	utils.o plc.o jitterbuf.o dnsmgr.o devicestate.o \
 	netsock.o slinfactory.o ast_expr2.o ast_expr2f.o \
 	cryptostub.o sha1.o http.o fixedjitterbuf.o abstract_jb.o \
-	strcompat.o threadstorage.o
+	strcompat.o threadstorage.o dial.o
 
 # we need to link in the objects statically, not as a library, because
 # otherwise modules will not have them available if none of the static

Added: branches/1.4/main/dial.c
URL: http://svn.digium.com/view/asterisk/branches/1.4/main/dial.c?view=auto&rev=52049
==============================================================================
--- branches/1.4/main/dial.c (added)
+++ branches/1.4/main/dial.c Wed Jan 24 12:20:05 2007
@@ -1,0 +1,802 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 1999 - 2007, Digium, Inc.
+ *
+ * Joshua Colp <jcolp 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 Dialing API
+ *
+ * \author Joshua Colp <jcolp at digium.com>
+ */
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <signal.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/options.h"
+#include "asterisk/utils.h"
+#include "asterisk/lock.h"
+#include "asterisk/linkedlists.h"
+#include "asterisk/dial.h"
+#include "asterisk/pbx.h"
+
+/*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
+struct ast_dial {
+	int num;                                           /*! Current number to give to next dialed channel */
+	enum ast_dial_result status;                       /*! Status of dial */
+	void *options[AST_DIAL_OPTION_MAX];                /*! Global options */
+	AST_LIST_HEAD_NOLOCK(, ast_dial_channel) channels; /*! Channels being dialed */
+	pthread_t thread;                                  /*! Thread (if running in async) */
+};
+
+/*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
+struct ast_dial_channel {
+	int num;                               /*! Unique number for dialed channel */
+	const char *tech;                      /*! Technology being dialed */
+	const char *device;                    /*! Device being dialed */
+	void *options[AST_DIAL_OPTION_MAX];    /*! Channel specific options */
+	int cause;                             /*! Cause code in case of failure */
+	struct ast_channel *owner;             /*! Asterisk channel */
+	AST_LIST_ENTRY(ast_dial_channel) list; /*! Linked list information */
+};
+
+/*! \brief Typedef for dial option enable */
+typedef void *(*ast_dial_option_cb_enable)(void *data);
+
+/*! \brief Typedef for dial option disable */
+typedef int (*ast_dial_option_cb_disable)(void *data);
+
+/* Structure for 'ANSWER_EXEC' option */
+struct answer_exec_struct {
+	char app[AST_MAX_APP]; /* Application name */
+	char *args;            /* Application arguments */
+};
+
+/* Enable function for 'ANSWER_EXEC' option */
+static void *answer_exec_enable(void *data)
+{
+	struct answer_exec_struct *answer_exec = NULL;
+	char *app = ast_strdupa((char*)data), *args = NULL;
+
+	/* Not giving any data to this option is bad, mmmk? */
+	if (ast_strlen_zero(app))
+		return NULL;
+
+	/* Create new data structure */
+	if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
+		return NULL;
+	
+	/* Parse out application and arguments */
+	if ((args = strchr(app, '|'))) {
+		*args++ = '\0';
+		answer_exec->args = ast_strdup(args);
+	}
+
+	/* Copy application name */
+	ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
+
+	return answer_exec;
+}
+
+/* Disable function for 'ANSWER_EXEC' option */
+static int answer_exec_disable(void *data)
+{
+	struct answer_exec_struct *answer_exec = data;
+
+	/* Make sure we have a value */
+	if (!answer_exec)
+		return -1;
+
+	/* If arguments are present, free them too */
+	if (answer_exec->args)
+		free(answer_exec->args);
+
+	/* This is simple - just free the structure */
+	free(answer_exec);
+
+	return 0;
+}
+
+/* Application execution function for 'ANSWER_EXEC' option */
+static void answer_exec_run(struct ast_channel *chan, char *app, char *args)
+{
+	struct ast_app *ast_app = pbx_findapp(app);
+
+	/* If the application was not found, return immediately */
+	if (!ast_app)
+		return;
+
+	/* All is well... execute the application */
+	pbx_exec(chan, ast_app, args);
+
+	return;
+}
+
+/*! \brief Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen. */
+static const struct ast_option_types {
+	enum ast_dial_option option;
+	ast_dial_option_cb_enable enable;
+	ast_dial_option_cb_disable disable;
+} option_types[] = {
+	{ AST_DIAL_OPTION_RINGING, NULL, NULL },                                  /*! Always indicate ringing to caller */
+	{ AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*! Execute application upon answer in async mode */
+	{ AST_DIAL_OPTION_MAX, NULL, NULL },                                      /*! Terminator of list */
+};
+
+/* free the buffer if allocated, and set the pointer to the second arg */
+#define S_REPLACE(s, new_val)           \
+        do {                            \
+                if (s)                  \
+                        free(s);        \
+                s = (new_val);          \
+        } while (0)
+
+/*! \brief Maximum number of channels we can watch at a time */
+#define AST_MAX_WATCHERS 256
+
+/*! \brief Macro for finding the option structure to use on a dialed channel */
+#define FIND_RELATIVE_OPTION(dial, dial_channel, ast_dial_option) (dial_channel->options[ast_dial_option] ? dial_channel->options[ast_dial_option] : dial->options[ast_dial_option])
+
+/*! \brief Macro that determines whether a channel is the caller or not */
+#define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
+
+/*! \brief New dialing structure
+ * \note Create a dialing structure
+ * \return Returns a calloc'd ast_dial structure, NULL on failure
+ */
+struct ast_dial *ast_dial_create(void)
+{
+	struct ast_dial *dial = NULL;
+
+	/* Allocate new memory for structure */
+	if (!(dial = ast_calloc(1, sizeof(*dial))))
+		return NULL;
+
+	/* Initialize list of channels */
+	AST_LIST_HEAD_INIT_NOLOCK(&dial->channels);
+
+	/* Initialize thread to NULL */
+	dial->thread = AST_PTHREADT_NULL;
+
+	return dial;
+}
+
+/*! \brief Append a channel
+ * \note Appends a channel to a dialing structure
+ * \return Returns channel reference number on success, -1 on failure
+ */
+int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
+{
+	struct ast_dial_channel *channel = NULL;
+
+	/* Make sure we have required arguments */
+	if (!dial || !tech || !device)
+		return -1;
+
+	/* Allocate new memory for dialed channel structure */
+	if (!(channel = ast_calloc(1, sizeof(*channel))))
+		return -1;
+
+	/* Record technology and device for when we actually dial */
+	channel->tech = tech;
+	channel->device = device;
+
+	/* Grab reference number from dial structure */
+	channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
+
+	/* Insert into channels list */
+	AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
+
+	return channel->num;
+}
+
+/*! \brief Helper function that does the beginning dialing */
+static int begin_dial(struct ast_dial *dial, struct ast_channel *chan)
+{
+	struct ast_dial_channel *channel = NULL;
+	int success = 0, res = 0;
+
+	/* Iterate through channel list, requesting and calling each one */
+	AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+		char numsubst[AST_MAX_EXTENSION];
+
+		/* Copy device string over */
+		ast_copy_string(numsubst, channel->device, sizeof(numsubst));
+
+		/* Request that the channel be created */
+		if (!(channel->owner = ast_request(channel->tech, chan->nativeformats, numsubst, &channel->cause)))
+			continue;
+
+		channel->owner->appl = "AppDial2";
+                channel->owner->data = "(Outgoing Line)";
+                channel->owner->whentohangup = 0;
+
+		/* Inherit everything from he who spawned this Dial */
+		ast_channel_inherit_variables(chan, channel->owner);
+
+		/* Copy over callerid information */
+		S_REPLACE(channel->owner->cid.cid_num, ast_strdup(chan->cid.cid_num));
+		S_REPLACE(channel->owner->cid.cid_name, ast_strdup(chan->cid.cid_name));
+		S_REPLACE(channel->owner->cid.cid_ani, ast_strdup(chan->cid.cid_ani));
+		S_REPLACE(channel->owner->cid.cid_rdnis, ast_strdup(chan->cid.cid_rdnis));
+
+		ast_string_field_set(channel->owner, language, chan->language);
+		ast_string_field_set(channel->owner, accountcode, chan->accountcode);
+		channel->owner->cdrflags = chan->cdrflags;
+		if (ast_strlen_zero(channel->owner->musicclass))
+			ast_string_field_set(channel->owner, musicclass, chan->musicclass);
+
+		channel->owner->cid.cid_pres = chan->cid.cid_pres;
+		channel->owner->cid.cid_ton = chan->cid.cid_ton;
+		channel->owner->cid.cid_tns = chan->cid.cid_tns;
+		channel->owner->adsicpe = chan->adsicpe;
+		channel->owner->transfercapability = chan->transfercapability;
+
+		/* Actually call the device */
+		if ((res = ast_call(channel->owner, numsubst, 0))) {
+			ast_hangup(channel->owner);
+			channel->owner = NULL;
+		} else {
+			success++;
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "Called %s\n", numsubst);
+		}
+	}
+
+	/* If number of failures matches the number of channels, then this truly failed */
+	return success;
+}
+
+/*! \brief Helper function that finds the dialed channel based on owner */
+static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
+{
+	struct ast_dial_channel *channel = NULL;
+
+	AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+		if (channel->owner == owner)
+			break;
+	}
+
+	return channel;
+}
+
+/*! \brief Helper function that handles control frames WITH owner */
+static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
+{
+	if (fr->frametype == AST_FRAME_CONTROL) {
+		switch (fr->subclass) {
+		case AST_CONTROL_ANSWER:
+			if (option_verbose > 2)
+				ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", channel->owner->name, chan->name);
+			AST_LIST_REMOVE(&dial->channels, channel, list);
+			AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
+			dial->status = AST_DIAL_RESULT_ANSWERED;
+			break;
+		case AST_CONTROL_BUSY:
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
+			ast_hangup(channel->owner);
+			channel->owner = NULL;
+			break;
+		case AST_CONTROL_CONGESTION:
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
+			ast_hangup(channel->owner);
+			channel->owner = NULL;
+			break;
+		case AST_CONTROL_RINGING:
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
+			ast_indicate(chan, AST_CONTROL_RINGING);
+			break;
+		case AST_CONTROL_PROGRESS:
+			if (option_verbose > 2)
+				ast_verbose (VERBOSE_PREFIX_3 "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
+			ast_indicate(chan, AST_CONTROL_PROGRESS);
+			break;
+		case AST_CONTROL_VIDUPDATE:
+			if (option_verbose > 2)
+				ast_verbose (VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
+			ast_indicate(chan, AST_CONTROL_VIDUPDATE);
+			break;
+		case AST_CONTROL_PROCEEDING:
+			if (option_verbose > 2)
+				ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
+			ast_indicate(chan, AST_CONTROL_PROCEEDING);
+			break;
+		case AST_CONTROL_HOLD:
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", chan->name);
+			ast_indicate(chan, AST_CONTROL_HOLD);
+			break;
+		case AST_CONTROL_UNHOLD:
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", chan->name);
+			ast_indicate(chan, AST_CONTROL_UNHOLD);
+			break;
+		case AST_CONTROL_OFFHOOK:
+		case AST_CONTROL_FLASH:
+			break;
+		case -1:
+			/* Prod the channel */
+			ast_indicate(chan, -1);
+			break;
+		default:
+			break;
+		}
+	}
+
+	return;
+}
+
+/*! \brief Helper function that handles control frames WITHOUT owner */
+static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
+{
+	/* If we have no owner we can only update the status of the dial structure, so only look at control frames */
+	if (fr->frametype != AST_FRAME_CONTROL)
+		return;
+
+	switch (fr->subclass) {
+	case AST_CONTROL_ANSWER:
+		if (option_verbose > 2)
+			ast_verbose( VERBOSE_PREFIX_3 "%s answered\n", channel->owner->name);
+		AST_LIST_REMOVE(&dial->channels, channel, list);
+		AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
+		dial->status = AST_DIAL_RESULT_ANSWERED;
+		break;
+	case AST_CONTROL_BUSY:
+		if (option_verbose > 2)
+			ast_verbose(VERBOSE_PREFIX_3 "%s is busy\n", channel->owner->name);
+		ast_hangup(channel->owner);
+		channel->owner = NULL;
+		break;
+	case AST_CONTROL_CONGESTION:
+		if (option_verbose > 2)
+			ast_verbose(VERBOSE_PREFIX_3 "%s is circuit-busy\n", channel->owner->name);
+		ast_hangup(channel->owner);
+		channel->owner = NULL;
+		break;
+	case AST_CONTROL_RINGING:
+		if (option_verbose > 2)
+			ast_verbose(VERBOSE_PREFIX_3 "%s is ringing\n", channel->owner->name);
+		dial->status = AST_DIAL_RESULT_RINGING;
+		break;
+	case AST_CONTROL_PROGRESS:
+		if (option_verbose > 2)
+			ast_verbose (VERBOSE_PREFIX_3 "%s is making progress\n", channel->owner->name);
+		dial->status = AST_DIAL_RESULT_PROGRESS;
+		break;
+	case AST_CONTROL_PROCEEDING:
+		if (option_verbose > 2)
+			ast_verbose (VERBOSE_PREFIX_3 "%s is proceeding\n", channel->owner->name);
+		dial->status = AST_DIAL_RESULT_PROCEEDING;
+		break;
+	default:
+		break;
+	}
+
+	return;
+}
+
+/*! \brief Helper function that basically keeps tabs on dialing attempts */
+static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
+{
+	int timeout = -1, count = 0;
+	struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
+	struct ast_dial_channel *channel = NULL;
+	struct answer_exec_struct *answer_exec = NULL;
+
+	/* Switch dialing status to trying */
+	dial->status = AST_DIAL_RESULT_TRYING;
+
+	/* If the "always indicate ringing" option is set, change status to ringing and indicate to the owner if present */
+	if (dial->options[AST_DIAL_OPTION_RINGING]) {
+		dial->status = AST_DIAL_RESULT_RINGING;
+		if (chan)
+			ast_indicate(chan, AST_CONTROL_RINGING);
+	}
+
+	/* Go into an infinite loop while we are trying */
+	while ((dial->status != AST_DIAL_RESULT_UNANSWERED) && (dial->status != AST_DIAL_RESULT_ANSWERED) && (dial->status != AST_DIAL_RESULT_HANGUP) && (dial->status != AST_DIAL_RESULT_TIMEOUT)) {
+		int pos = 0;
+		struct ast_frame *fr = NULL;
+
+		/* Set up channel structure array */
+		pos = count = 0;
+		if (chan)
+			cs[pos++] = chan;
+
+		/* Add channels we are attempting to dial */
+		AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+			if (channel->owner) {
+				cs[pos++] = channel->owner;
+				count++;
+			}
+		}
+
+		/* If we have no outbound channels in progress, switch status to unanswered and stop */
+		if (!count) {
+			dial->status = AST_DIAL_RESULT_UNANSWERED;
+			break;
+		}
+
+		/* Just to be safe... */
+		if (dial->thread == AST_PTHREADT_STOP)
+			break;
+
+		/* Wait for frames from channels */
+		who = ast_waitfor_n(cs, pos, &timeout);
+
+		/* Check to see if our thread is being cancelled */
+		if (dial->thread == AST_PTHREADT_STOP)
+			break;
+
+		/* If we are not being cancelled and we have no channel, then timeout was tripped */
+		if (!who)
+			continue;
+
+		/* Find relative dial channel */
+		if (!chan || !IS_CALLER(chan, who))
+			channel = find_relative_dial_channel(dial, who);
+
+		/* Attempt to read in a frame */
+		if (!(fr = ast_read(who))) {
+			/* If this is the caller then we switch status to hangup and stop */
+			if (chan && IS_CALLER(chan, who)) {
+				dial->status = AST_DIAL_RESULT_HANGUP;
+				break;
+			}
+			ast_hangup(who);
+			channel->owner = NULL;
+			continue;
+		}
+
+		/* Process the frame */
+		if (chan)
+			handle_frame(dial, channel, fr, chan);
+		else
+			handle_frame_ownerless(dial, channel, fr);
+
+		/* Free the received frame and start all over */
+		ast_frfree(fr);
+	}
+
+	/* Do post-processing from loop */
+	if (dial->status == AST_DIAL_RESULT_ANSWERED) {
+		/* Hangup everything except that which answered */
+		AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+			if (!channel->owner || channel->owner == who)
+				continue;
+			ast_hangup(channel->owner);
+			channel->owner = NULL;
+		}
+		/* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
+		if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC)))
+			answer_exec_run(who, answer_exec->app, answer_exec->args);
+	} else if (dial->status == AST_DIAL_RESULT_HANGUP) {
+		/* Hangup everything */
+		AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+			if (!channel->owner)
+				continue;
+			ast_hangup(channel->owner);
+			channel->owner = NULL;
+		}
+	}
+
+	return dial->status;
+}
+
+/*! \brief Dial async thread function */
+static void *async_dial(void *data)
+{
+	struct ast_dial *dial = data;
+
+	/* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
+	monitor_dial(dial, NULL);
+
+	return NULL;
+}
+
+/*! \brief Execute dialing synchronously or asynchronously
+ * \note Dials channels in a dial structure.
+ * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
+ */
+enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
+{
+	enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
+
+	/* Ensure required arguments are passed */
+	if (!dial || !chan)
+		return AST_DIAL_RESULT_INVALID;
+
+	/* If there are no channels to dial we can't very well try to dial them */
+	if (AST_LIST_EMPTY(&dial->channels))
+		return AST_DIAL_RESULT_INVALID;
+
+	/* Dial each requested channel */
+	if (!begin_dial(dial, chan))
+		return AST_DIAL_RESULT_FAILED;
+
+	/* If we are running async spawn a thread and send it away... otherwise block here */
+	if (async) {
+		/* Try to create a thread */
+		if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
+			/* Failed to create the thread - hangup all dialed channels and return failed */
+			ast_dial_hangup(dial);
+			res = AST_DIAL_RESULT_FAILED;
+		}
+	} else {
+		res = monitor_dial(dial, chan);
+	}
+
+	return res;
+}
+
+/*! \brief Return channel that answered
+ * \note Returns the Asterisk channel that answered
+ * \param dial Dialing structure
+ */
+struct ast_channel *ast_dial_answered(struct ast_dial *dial)
+{
+	if (!dial)
+		return NULL;
+
+	return ((dial->status == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
+}
+
+/*! \brief Return status of dial
+ * \note Returns the status of the dial attempt
+ * \param dial Dialing structure
+ */
+enum ast_dial_result ast_dial_status(struct ast_dial *dial)
+{
+	return dial->status;
+}
+
+/*! \brief Cancel async thread
+ * \note Cancel a running async thread
+ * \param dial Dialing structure
+ */
+enum ast_dial_result ast_dial_join(struct ast_dial *dial)
+{
+	pthread_t thread;
+
+	/* If the dial structure is not running in async, return failed */
+	if (dial->thread == AST_PTHREADT_NULL)
+		return AST_DIAL_RESULT_FAILED;
+
+	/* Record thread */
+	thread = dial->thread;
+
+	/* Stop the thread */
+	dial->thread = AST_PTHREADT_STOP;
+
+	/* Now we signal it with SIGURG so it will break out of it's waitfor */
+	pthread_kill(thread, SIGURG);
+
+	/* Finally wait for the thread to exit */
+	pthread_join(thread, NULL);
+
+	/* Yay thread is all gone */
+	dial->thread = AST_PTHREADT_NULL;
+
+	return dial->status;
+}
+
+/*! \brief Hangup channels
+ * \note Hangup all active channels
+ * \param dial Dialing structure
+ */
+void ast_dial_hangup(struct ast_dial *dial)
+{
+	struct ast_dial_channel *channel = NULL;
+
+	if (!dial)
+		return;
+	
+	AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+		if (channel->owner) {
+			ast_hangup(channel->owner);
+			channel->owner = NULL;
+		}
+	}
+
+	return;
+}
+
+/*! \brief Destroys a dialing structure
+ * \note Destroys (free's) the given ast_dial structure
+ * \param dial Dialing structure to free
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_destroy(struct ast_dial *dial)
+{
+	int i = 0;
+	struct ast_dial_channel *channel = NULL;
+
+	if (!dial)
+		return -1;
+	
+	/* Hangup and deallocate all the dialed channels */
+	AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+		/* Disable any enabled options */
+		for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
+			if (!channel->options[i])
+				continue;
+			if (option_types[i].disable)
+				option_types[i].disable(channel->options[i]);
+			channel->options[i] = NULL;
+		}
+		/* Hang up channel if need be */
+		if (channel->owner) {
+			ast_hangup(channel->owner);
+			channel->owner = NULL;
+		}
+		/* Free structure */
+		free(channel);
+	}
+       
+	/* Disable any enabled options globally */
+	for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
+		if (!dial->options[i])
+			continue;
+		if (option_types[i].disable)
+			option_types[i].disable(dial->options[i]);
+		dial->options[i] = NULL;
+	}
+
+	/* Free structure */
+	free(dial);
+
+	return 0;
+}
+
+/*! \brief Enables an option globally
+ * \param dial Dial structure to enable option on
+ * \param option Option to enable
+ * \param data Data to pass to this option (not always needed)
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
+{
+	/* If the option is already enabled, return failure */
+	if (dial->options[option])
+		return -1;
+
+	/* Execute enable callback if it exists, if not simply make sure the value is set */
+	if (option_types[option].enable)
+		dial->options[option] = option_types[option].enable(data);
+	else
+		dial->options[option] = (void*)1;
+
+	return 0;
+}
+
+/*! \brief Enables an option per channel
+ * \param dial Dial structure
+ * \param num Channel number to enable option on
+ * \param option Option to enable
+ * \param data Data to pass to this option (not always needed)
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
+{
+	struct ast_dial_channel *channel = NULL;
+
+	/* Ensure we have required arguments */
+	if (!dial || AST_LIST_EMPTY(&dial->channels))
+		return -1;
+	
+	/* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
+	if (AST_LIST_LAST(&dial->channels)->num != num) {
+		AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+			if (channel->num == num)
+				break;
+		}
+	} else {
+		channel = AST_LIST_LAST(&dial->channels);
+	}
+
+	/* If none found, return failure */
+	if (!channel)
+		return -1;
+
+	/* If the option is already enabled, return failure */
+	if (channel->options[option])
+		return -1;
+
+        /* Execute enable callback if it exists, if not simply make sure the value is set */
+	if (option_types[option].enable)
+		channel->options[option] = option_types[option].enable(data);
+	else
+		channel->options[option] = (void*)1;
+
+	return 0;
+}
+
+/*! \brief Disables an option globally
+ * \param dial Dial structure to disable option on
+ * \param option Option to disable
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
+{
+        /* If the option is not enabled, return failure */
+        if (!dial->options[option])
+                return -1;
+
+	/* Execute callback of option to disable if it exists */
+	if (option_types[option].disable)
+		option_types[option].disable(dial->options[option]);
+
+	/* Finally disable option on the structure */
+	dial->options[option] = NULL;
+
+        return 0;
+}
+
+/*! \brief Disables an option per channel
+ * \param dial Dial structure
+ * \param num Channel number to disable option on
+ * \param option Option to disable
+ * \return Returns 0 on success, -1 on failure
+ */
+int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
+{
+	struct ast_dial_channel *channel = NULL;
+
+	/* Ensure we have required arguments */
+	if (!dial || AST_LIST_EMPTY(&dial->channels))
+		return -1;
+
+	/* Look for channel, we can sort of cheat and predict things - the last channel in the list will probably be what they want */
+	if (AST_LIST_LAST(&dial->channels)->num != num) {
+		AST_LIST_TRAVERSE(&dial->channels, channel, list) {
+			if (channel->num == num)
+				break;
+		}
+	} else {
+		channel = AST_LIST_LAST(&dial->channels);
+	}
+
+	/* If none found, return failure */
+	if (!channel)
+		return -1;
+
+	/* If the option is not enabled, return failure */
+	if (!channel->options[option])
+		return -1;
+
+	/* Execute callback of option to disable it if it exists */
+	if (option_types[option].disable)
+		option_types[option].disable(channel->options[option]);
+
+	/* Finally disable the option on the structure */
+	channel->options[option] = NULL;
+
+	return 0;
+}

Propchange: branches/1.4/main/dial.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: branches/1.4/main/dial.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: branches/1.4/main/dial.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain



More information about the asterisk-commits mailing list