[svn-commits] russell: branch russell/issue_5841 r61114 - /team/russell/issue_5841/res/

svn-commits at lists.digium.com svn-commits at lists.digium.com
Mon Apr 9 15:27:06 MST 2007


Author: russell
Date: Mon Apr  9 17:27:06 2007
New Revision: 61114

URL: http://svn.digium.com/view/asterisk?view=rev&rev=61114
Log:
Add current patch from 5841

Modified:
    team/russell/issue_5841/res/res_features.c

Modified: team/russell/issue_5841/res/res_features.c
URL: http://svn.digium.com/view/asterisk/team/russell/issue_5841/res/res_features.c?view=diff&rev=61114&r1=61113&r2=61114
==============================================================================
--- team/russell/issue_5841/res/res_features.c (original)
+++ team/russell/issue_5841/res/res_features.c Mon Apr  9 17:27:06 2007
@@ -170,6 +170,7 @@
 	struct ast_bridge_config bconfig;
 	struct ast_channel *chan;
 	struct ast_channel *peer;
+	int return_to_pbx;
 };
 
 
@@ -224,10 +225,11 @@
 static void *ast_bridge_call_thread(void *data) 
 {
 	struct ast_bridge_thread_obj *tobj = data;
-
-	tobj->chan->appl = "Transferred Call";
+	int res;
+
+	tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
 	tobj->chan->data = tobj->peer->name;
-	tobj->peer->appl = "Transferred Call";
+	tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
 	tobj->peer->data = tobj->chan->name;
 	if (tobj->chan->cdr) {
 		ast_cdr_reset(tobj->chan->cdr, NULL);
@@ -239,8 +241,29 @@
 	}
 
 	ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
-	ast_hangup(tobj->chan);
-	ast_hangup(tobj->peer);
+	if ( tobj->return_to_pbx ) {
+		if ( !ast_check_hangup(tobj->peer) ) {
+			ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
+			res = ast_pbx_start(tobj->peer);
+			if ( AST_PBX_SUCCESS != res )
+				ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
+			else
+				ast_log(LOG_DEBUG, "SUCCESS continuing PBX on peer %s\n", tobj->peer->name);
+		} else
+			ast_hangup(tobj->peer);
+		if ( !ast_check_hangup(tobj->chan) ) {
+			ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
+			res = ast_pbx_start(tobj->chan);
+			if ( AST_PBX_SUCCESS != res )
+				ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
+			else
+				ast_log(LOG_DEBUG, "SUCCESS continuing PBX on chan %s\n", tobj->chan->name);
+		} else
+			ast_hangup(tobj->chan);
+	} else {
+		ast_hangup(tobj->chan);
+		ast_hangup(tobj->peer);
+	}
 	bzero(tobj, sizeof(*tobj)); /*! \todo XXX for safety */
 	free(tobj);
 	return NULL;
@@ -2028,6 +2051,147 @@
 	ast_cli(fd,"\n");
 	
 	return RESULT_SUCCESS;
+}
+
+static char mandescr_bridge[] =
+"Description: Bridge together two channels already in the PBX\n"
+"Variables: ( Names marked with * are required )\n"
+"	*Channel1: Channel to Bridge to Channel2\n"
+"	*Channel2: Channel to Bridge to Channel1\n"
+"	     Tone: (Yes|No) Play courtesy tone to Channel 2\n"
+"\n";
+
+static void do_bridge_masquerade(struct ast_channel *chan, struct ast_channel *tmpchan)
+{
+	ast_moh_stop(chan);
+	ast_mutex_lock(&chan->lock);
+	ast_setstate(tmpchan, chan->_state);
+	tmpchan->readformat = chan->readformat;
+	tmpchan->writeformat = chan->writeformat;
+	ast_channel_masquerade(tmpchan, chan);
+	ast_mutex_lock(&tmpchan->lock);
+	ast_do_masquerade(tmpchan);
+	/* when returning from bridge, the channel will continue at the next priority */
+	ast_explicit_goto(tmpchan, chan->context, chan->exten, chan->priority + 1);
+	ast_mutex_unlock(&tmpchan->lock);
+	ast_mutex_unlock(&chan->lock);
+}
+
+static int action_bridge(struct mansession *s, const struct message *m)
+{
+	const char *channela = astman_get_header(m, "Channel1");
+	const char *channelb = astman_get_header(m, "Channel2");
+	const char *pt = astman_get_header(m, "Tone");
+	const char *actionid = astman_get_header(m, "ActionID");
+	char buf[BUFSIZ];
+	struct ast_channel *chana = NULL, *chanb = NULL;
+	struct ast_channel *tmpchana = NULL, *tmpchanb = NULL;
+	struct ast_bridge_thread_obj *tobj = NULL;
+	int res = 0;
+	int playtone = 0;
+	char sActionid[1025];
+
+	/* Find out whether we're supposed to play a tone or not */
+	playtone = ast_true(pt);
+	
+	/* if ActionID specified, build a new one with \r\n included */
+	if ( !ast_strlen_zero(actionid) )
+		snprintf(sActionid, sizeof(sActionid), "ActionID: %s\r\n", actionid);
+	else
+		strcpy(sActionid, "");
+
+	/* make sure valid channels were specified */
+	if ( !ast_strlen_zero(channela) && !ast_strlen_zero(channelb) ) {
+		chana = ast_get_channel_by_name_locked(channela);
+		chanb = ast_get_channel_by_name_locked(channelb);
+		if ( chana )
+			ast_mutex_unlock(&chana->lock);
+		if ( chanb )
+			ast_mutex_unlock(&chanb->lock);
+
+		/* send errors if any of the channels could not be found/locked */
+		if ( !chana ) {
+			snprintf(buf, sizeof(buf), "Channel1 does not exists: %s", channela);
+			astman_send_error(s, m, buf);
+			return 0;
+		}
+		if ( !chanb ) {
+			snprintf(buf, sizeof(buf), "Channel2 does not exists: %s", channelb);
+			astman_send_error(s, m, buf);
+			return 0;
+		}
+	} else {
+		astman_send_error(s, m, "Missing channel parameter in request");
+		return 0;
+	}
+
+	/* Answer the channels if needed */
+	if ( AST_STATE_UP != chana->_state )
+		ast_answer(chana);
+	if ( AST_STATE_UP != chanb->_state )
+		ast_answer(chanb);
+
+	/* create the placeholder channels and grab the other channels */
+	tmpchana = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "Bridge/%s", chana->name);
+	tmpchanb = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "Bridge/%s", chanb->name);
+
+	if ( tmpchana )
+		do_bridge_masquerade(chana, tmpchana);
+	else {
+		astman_send_error(s, m, "Unable to create temporary channels!");
+		ast_hangup(tmpchana);
+		ast_hangup(chana);
+		return 1;
+	}
+
+	if ( tmpchanb )
+		do_bridge_masquerade(chanb, tmpchanb);
+	else {
+		astman_send_error(s, m, "Unable to create temporary channels!");
+		ast_hangup(tmpchanb);
+		ast_hangup(chanb);
+		return 1;
+	}
+	
+	/* make the channels compatible, send error if we fail doing so */
+	res = ast_channel_make_compatible(tmpchana, tmpchanb);
+	if ( res ) {
+		ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for manager bridge\n", tmpchana->name, tmpchanb->name);
+		astman_send_error(s, m, "Could not make channels compatible for manager bridge");
+		ast_hangup(tmpchana);
+		ast_hangup(tmpchanb);
+		return 1;
+	}
+
+	/* setup the bridge thread object and start the bridge */
+	tobj = malloc(sizeof(struct ast_bridge_thread_obj));
+	if ( tobj ) {
+		memset(tobj, 0, sizeof(struct ast_bridge_thread_obj));
+		tobj->chan = tmpchana;
+		tobj->peer = tmpchanb;
+		tobj->return_to_pbx = 1;
+		tobj->bconfig.play_warning = 0;
+		tobj->bconfig.warning_freq = 0;
+		tobj->bconfig.warning_sound = NULL;
+		tobj->bconfig.end_sound = NULL;
+		tobj->bconfig.start_sound = NULL;
+		tobj->bconfig.firstpass = 0;
+		tobj->bconfig.timelimit = 0;
+		tobj->bconfig.feature_timer = 0;
+		if ( playtone ) 
+			if ( !ast_strlen_zero(xfersound) && !ast_streamfile(tmpchanb, xfersound, tmpchanb->language) )
+				if ( ast_waitstream(tmpchanb, "") < 0 )
+					ast_log(LOG_WARNING, "Failed to play a courtesy tone on chan %s\n", tmpchanb->name);
+		ast_bridge_call_thread_launch(tobj);
+		astman_send_ack(s, m, "Launched bridge thread with success");
+		return 0;
+	} else {
+		ast_log(LOG_WARNING, "Unable to spawn a new bridge thread on %s and %s: %s\n", tmpchana->name, tmpchanb->name, strerror(errno));
+		astman_send_error(s, m, "Unable to spawn a new bridge thread");
+		ast_hangup(tmpchana);
+		ast_hangup(tmpchanb);
+		return 1;
+	}
 }
 
 static char showfeatures_help[] =
@@ -2451,6 +2615,151 @@
 
 }
 
+static char *app_bridge = "Bridge";
+static char *bridge_synopsis = "Bridge two channels";
+static char *bridge_descrip =
+"Usage: Bridge(CHANNEL|PLAYTONE)\n"
+"	Allows the ability to bridge two channels via the dialplan.\n"
+"The current channel is bridged to the specified CHANNEL.\n"
+"Adding PLAYTONE will play a courtesy audio tone on CHANNEL when bridged successfully.\n"
+"BRIDGERESULT dial plan variable will contain SUCCESS, FAILURE, LOOP, NONEXISTENT or INCOMPATIBLE.\n";
+
+static int bridge_exec(struct ast_channel *chan, void *data)
+{
+	struct ast_module_user *u;
+	struct ast_channel *current_dest_chan = NULL, *final_dest_chan = NULL;
+	int res, pbx_res = 0;
+	int playtone = 0;
+	char *tmp_data  = NULL;
+
+	AST_DECLARE_APP_ARGS(args,
+		AST_APP_ARG(dest_chan);
+		AST_APP_ARG(playtone);
+	);
+	
+	if ( ast_strlen_zero(data) ) {
+		ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
+		return -1;
+	}
+	
+	u = ast_module_user_add(chan);
+
+	tmp_data = ast_strdupa(data);
+	if ( !tmp_data ) {
+		ast_log(LOG_ERROR, "Out of memory!\n");
+		ast_module_user_remove(u);
+		return -1;
+	}
+
+	AST_STANDARD_APP_ARGS(args, tmp_data);
+	if ( args.playtone ) {
+		playtone = 1;
+	}
+	
+	/* avoid bridge with ourselves */
+	int cmplen = strlen(chan->name) < strlen(args.dest_chan) ? strlen(chan->name) : strlen(args.dest_chan);
+	if ( 0 == strncmp(chan->name, args.dest_chan, cmplen) ) {
+		ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", chan->name);
+		manager_event(EVENT_FLAG_CALL, "BridgeExec",
+					"Response: Failed\r\n"
+					"Reason: Unable to bridge channel to itself\r\n"
+					"Channel1: %s\r\n"
+					"Channel2: %s\r\n",
+					chan->name, args.dest_chan);
+		pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "LOOP");
+		ast_module_user_remove(u);
+		return 0;
+	}
+
+	/* make sure we have a valid end point */
+	current_dest_chan = ast_get_channel_by_name_locked(args.dest_chan);
+	if ( !current_dest_chan ) {
+		ast_log(LOG_WARNING, "Bridge failed because channel %s does not exists or we cannot get its lock\n", args.dest_chan);
+		manager_event(EVENT_FLAG_CALL, "BridgeExec",
+					"Response: Failed\r\n"
+					"Reason: Cannot grab end point\r\n"
+					"Channel1: %s\r\n"
+					"Channel2: %s\r\n", chan->name, args.dest_chan);
+		pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "NONEXISTENT");
+		ast_module_user_remove(u);
+		return 0;
+	}
+	ast_mutex_unlock(&current_dest_chan->lock);
+
+	/* answer the channel if needed */
+	if ( AST_STATE_UP != current_dest_chan->_state )
+		ast_answer(current_dest_chan);
+
+	/* try to allocate a place holder where current_dest_chan will be placed */
+	final_dest_chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "Bridge/%s", current_dest_chan->name);
+	if ( !final_dest_chan ) {
+		ast_log(LOG_WARNING, "Cannot create placeholder channel for chan %s\n", args.dest_chan);
+		manager_event(EVENT_FLAG_CALL, "BridgeExec",
+					"Response: Failed\r\n"
+					"Reason: cannot create placeholder\r\n"
+					"Channel1: %s\r\n"
+					"Channel2: %s\r\n", chan->name, args.dest_chan);
+	}
+	do_bridge_masquerade(current_dest_chan, final_dest_chan);
+	/* now current_dest_chan is a ZOMBIE and with softhangup set to 1 and final_dest_chan is our end point */
+	/* try to make compatible, send error if we fail */
+	res = ast_channel_make_compatible(chan, final_dest_chan);
+	if ( res < 0 ) {
+		ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, final_dest_chan->name);
+		manager_event(EVENT_FLAG_CALL, "BridgeExec",
+					"Response: Failed\r\n"
+					"Reason: Could not make channels compatible for bridge\r\n"
+					"Channel1: %s\r\n"
+					"Channel2: %s\r\n", chan->name, final_dest_chan->name);
+		ast_hangup(final_dest_chan); /* may be we should return this channel to the PBX? */
+		pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "INCOMPATIBLE");
+		ast_module_user_remove(u);
+		return 0;
+	}
+	/* Report that the bridge will be successfull */
+	manager_event(EVENT_FLAG_CALL, "BridgeExec",
+				"Response: Success\r\n"
+				"Channel1: %s\r\n"
+				"Channel2: %s\r\n", chan->name, final_dest_chan->name);
+	/* we have 2 valid channels to bridge, now it is just a matter of setting up the bridge config and starting the bridge */	
+	struct ast_bridge_config bconfig;
+	bconfig.play_warning = 0;
+	bconfig.warning_freq = 0;
+	bconfig.warning_sound = NULL;
+	bconfig.end_sound = NULL;
+	bconfig.start_sound = NULL;
+	bconfig.firstpass = 0;
+	bconfig.timelimit = 0;
+	bconfig.feature_timer = 0;
+	ast_clear_flag(&(bconfig.features_caller), AST_FLAGS_ALL);
+	ast_clear_flag(&(bconfig.features_callee), AST_FLAGS_ALL);
+
+	if ( playtone && !ast_strlen_zero(xfersound) )
+		if ( !ast_streamfile(final_dest_chan, xfersound, final_dest_chan->language) )
+			if ( ast_waitstream(final_dest_chan, "") < 0 )
+				ast_log(LOG_WARNING, "Failed to play courtesy tone on %s\n", final_dest_chan->name);
+	
+	/* do the bridge */
+	res = ast_bridge_call(chan, final_dest_chan, &bconfig);
+
+	/* the bridge has ended, set BRIDGERESULT to SUCCESS. If the other channel has not been hung up, return it to the PBX */
+	pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
+	if ( !ast_check_hangup(final_dest_chan) ) {
+		ast_log(LOG_DEBUG, "starting new PBX in %s,%s,%d for chan %s\n", final_dest_chan->context, final_dest_chan->exten, 
+		final_dest_chan->priority, final_dest_chan->name);
+		pbx_res = ast_pbx_start(final_dest_chan);
+		if ( AST_PBX_SUCCESS != pbx_res ) {
+			ast_log(LOG_WARNING, "FAILED continuing PBX on dest chan %s\n", final_dest_chan->name);
+			ast_hangup(final_dest_chan);
+		} else
+			ast_log(LOG_DEBUG, "SUCCESS continuing PBX on chan %s\n", final_dest_chan->name);
+	} else {
+		ast_log(LOG_DEBUG, "hangup chan %s since the other endpoint has hung up\n", final_dest_chan->name);
+		ast_hangup(final_dest_chan);
+	}
+	ast_module_user_remove(u);
+	return res;
+}
 static int reload(void)
 {
 	return load_config();
@@ -2459,7 +2768,9 @@
 static int load_module(void)
 {
 	int res;
-	
+
+	ast_register_application(app_bridge, bridge_exec, bridge_synopsis, bridge_descrip);	
+
 	memset(parking_ext, 0, sizeof(parking_ext));
 	memset(parking_con, 0, sizeof(parking_con));
 
@@ -2474,6 +2785,7 @@
 		ast_manager_register("ParkedCalls", 0, manager_parking_status, "List parked calls" );
 		ast_manager_register2("Park", EVENT_FLAG_CALL, manager_park,
 			"Park a channel", mandescr_park); 
+		ast_manager_register2("Bridge", EVENT_FLAG_COMMAND, action_bridge, "Bridge two channels already in the PBX", mandescr_bridge);
 	}
 
 	res |= ast_devstate_prov_add("Park", metermaidstate);
@@ -2487,9 +2799,11 @@
 	ast_module_user_hangup_all();
 
 	ast_manager_unregister("ParkedCalls");
+	ast_manager_unregister("Bridge");
 	ast_manager_unregister("Park");
 	ast_cli_unregister_multiple(cli_features, sizeof(cli_features) / sizeof(struct ast_cli_entry));
 	ast_unregister_application(parkcall);
+	ast_unregister_application(app_bridge);
 	ast_devstate_prov_del("Park");
 	return ast_unregister_application(parkedcall);
 }



More information about the svn-commits mailing list