--- asterisk-1.2.12.1/res/res_features.c 2006-08-01 18:07:06.000000000 -0500 +++ asterisk-1.2.12.1-app-bridge/res/res_features.c 2006-10-03 18:20:01.290304513 -0500 @@ -175,6 +175,7 @@ struct ast_bridge_config bconfig; struct ast_channel *chan; struct ast_channel *peer; + int return_to_pbx; }; static void check_goto_on_transfer(struct ast_channel *chan) @@ -217,10 +218,11 @@ static void *ast_bridge_call_thread(void *data) { struct ast_bridge_thread_obj *tobj = data; + int res; - tobj->chan->appl = "Transferred Call"; + 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); @@ -232,8 +234,30 @@ } 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 into PBX again\n"); + 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 into PBX again\n"); + 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); + } tobj->chan = tobj->peer = NULL; free(tobj); tobj=NULL; @@ -1843,6 +1867,173 @@ return RESULT_SUCCESS; } +/* DCT: BEGIN APP_BRIDGE PATCH */ +static char mandescr_bridge[] = +"Description: Bridge two active channels together.\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 Channel2.\n" +"\n"; + +static int action_bridge(struct mansession *s, struct message *m) +{ + char *channela = astman_get_header(m, "Channel1"); + char *channelb = astman_get_header(m, "Channel2"); + char *pt = astman_get_header(m, "Tone"); + char *actionid = astman_get_header(m, "ActionID"); + char buf[BUFSIZ]; + struct ast_channel *chana = NULL, *chanb = NULL; + struct ast_channel *tmpchana = NULL; + struct ast_channel *tmpchanb = NULL; + struct ast_bridge_thread_obj *tobj = NULL; + int res = 0; + int playtone = 0; + int useactionid = 0; + + /* Find out if we're supposed to play a tone or not */ + playtone = ast_true(pt); + + /* Find out if there's an ActionID */ + if (!ast_strlen_zero(actionid)) { + useactionid = 1; + } + + char sActionid[80]; + + /* We put the final \r\n in sActionid so that the + * last %s isn't ever NULL and thus literally "%s" + */ + + if (useactionid) { + snprintf(sActionid, sizeof(sActionid), "ActionID: %s\r\n", actionid); + } else { + strcpy(sActionid,""); + } + + /* Make sure we got input for both channels then find + * their actual channel pointers + */ + + 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); + + /* Make sure the pointers exist - err if not */ + if (!chana) { + snprintf(buf, sizeof(buf), "Channel does not exist: %s", channela); + astman_send_error(s, m, buf); + return 0; + } + if (!chanb) { + snprintf(buf, sizeof(buf), "Channel does not exist: %s", channelb); + astman_send_error(s, m, buf); + return 0; + } + } else { + astman_send_error(s, m, "Missing parameter"); + return 0; + } + + if (chana->_state != AST_STATE_UP) { + ast_answer(chana); + } + if (chanb->_state != AST_STATE_UP) { + ast_answer(chanb); + } + + tmpchana = ast_channel_alloc(0); + tmpchanb = ast_channel_alloc(0); + + if (tmpchana) { + /* Rename the Bridge channels so that we can safely remove them after masqerading */ + snprintf((char *) tmpchana->name, sizeof(tmpchana->name), "Bridge/%s", chana->name); + ast_moh_stop(chana); + ast_mutex_lock(&chana->lock); + ast_setstate(tmpchana, chana->_state); + tmpchana->readformat = chana->readformat; + tmpchana->writeformat = chana->writeformat; + ast_channel_masquerade(tmpchana, chana); + ast_mutex_lock(&tmpchana->lock); + ast_do_masquerade(tmpchana); + ast_explicit_goto(tmpchana, chana->context, chana->exten, chana->priority + 1); + ast_mutex_unlock(&tmpchana->lock); + ast_mutex_unlock(&chana->lock); + } else { + astman_send_error(s, m, "Unable to create temporary channels!"); + ast_hangup(tmpchana); + ast_hangup(chana); + return 1; + } + if (tmpchanb) { + /* Rename the Bridge channels so that we can safely remove them after masqerading */ + snprintf((char *) tmpchanb->name, sizeof(tmpchanb->name), "Bridge/%s", chanb->name); + ast_moh_stop(chanb); + ast_mutex_lock(&chanb->lock); + ast_setstate(tmpchanb, chanb->_state); + tmpchanb->readformat = chanb->readformat; + tmpchanb->writeformat = chanb->writeformat; + ast_channel_masquerade(tmpchanb, chanb); + ast_mutex_lock(&tmpchanb->lock); + ast_do_masquerade(tmpchanb); + ast_explicit_goto(tmpchanb, chanb->context, chanb->exten, chanb->priority + 1); + ast_mutex_unlock(&tmpchanb->lock); + ast_mutex_unlock(&chanb->lock); + } else { + snprintf(buf, sizeof(buf), "Unable to create temporary channels!"); + astman_send_error(s, m, buf); + ast_hangup(tmpchanb); + ast_hangup(chanb); + return 1; + } + + /* Make the channels compatible - err if we fail in doing so.*/ + res = ast_channel_make_compatible(tmpchana, tmpchanb); + if (res) { + ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chana->name, chanb->name); + astman_send_error(s, m, "Could not make channels compatible for bridge!"); + ast_hangup(tmpchana); + ast_hangup(tmpchanb); + return 1; + } + /* Set up 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; + + 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 courtesy tone!\n"); + } + } + ast_bridge_call_thread_launch(tobj); + astman_send_ack(s, m, "Launched bridge thread with success"); + } else { + ast_log(LOG_WARNING, "Unable to spawn new bridge on %s and %s: %s\n", tmpchana->name, tmpchanb->name, strerror(errno)); + astman_send_error(s, m, "Unable to spawn new bridge thread"); + ast_hangup(tmpchana); + ast_hangup(tmpchanb); + return 1; + } + return 0; +} +/* DCT: END APP_BRIDGE PATCH */ + static char showfeatures_help[] = "Usage: show features\n" " Lists currently configured features.\n"; @@ -2141,6 +2332,191 @@ return ast_add_extension2(con, 1, ast_parking_ext(), 1, NULL, NULL, parkcall, strdup(""), FREE, registrar); } +/* DCT: END SECOND APP_BRIDGE CHUNK */ + +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 give an audio notice of the bridge.\n" +"BRIDGERESULT will contain SUCCESS, FAILURE, LOOP, NONEXISTENT,\n." +"INCOMPATIBLE, or MISSINGPARAM."; + +static int bridge_exec(struct ast_channel *chan, void *data) +{ + struct localuser *u; + struct ast_channel *current_dest_chan = NULL, *final_dest_chan = NULL; + int 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 requires 1 argument to specify the other bridge end channel\n"); + return -1; + } + + LOCAL_USER_ADD(u); + + tmp_data = ast_strdupa(data); + if ( !tmp_data ) { + ast_log(LOG_ERROR, "Out of memory!\n"); + LOCAL_USER_REMOVE(u); + return -1; + } + + AST_STANDARD_APP_ARGS(args, tmp_data); + + if ( args.playtone ) { + playtone = 1; + } + + /* avoid same end points in bridge */ + 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 to itself: %s %s\n", chan->name, args.dest_chan); + 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" + "Tone: %s\r\n", + chan->name, args.dest_chan, playtone ? "Yes" : "No"); + pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "LOOP"); + LOCAL_USER_REMOVE(u); + return 0; + } + + /* Make sure we got valid destiny end point */ + current_dest_chan = ast_get_channel_by_name_locked(args.dest_chan); + if ( !current_dest_chan ) { + ast_log(LOG_WARNING, "Channel does not exist: %s\n", args.dest_chan); + manager_event(EVENT_FLAG_CALL, "BridgeExec", + "Response: Failed\r\n" + "Reason: end point does not exist\r\n" + "Channel1: %s\r\n" + "Channel2: %s\r\n" + "Tone: %s\r\n", + chan->name, args.dest_chan, playtone ? "Yes" : "No"); + pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "NONEXISTENT"); + LOCAL_USER_REMOVE(u); + return 0; + } + ast_mutex_unlock(¤t_dest_chan->lock); + + /* answer the channel if is not */ + if (current_dest_chan->_state != AST_STATE_UP) + ast_answer(current_dest_chan); + + /* try to allocate a place holder channel where current dest chan will be placed */ + final_dest_chan = ast_channel_alloc(0); + if ( !final_dest_chan ) { + ast_log(LOG_WARNING, "Unable to create placeholder channel!\n"); + manager_event(EVENT_FLAG_CALL, "BridgeExec", + "Response: Failed\r\n" + "Reason: Unable to create placeholder channel\r\n" + "Channel1: %s\r\n" + "Channel2: %s\r\n" + "Tone: %s\r\n", + chan->name, current_dest_chan->name, playtone ? "Yes" : "No"); + pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE"); + LOCAL_USER_REMOVE(u); + return 0; + } + + /* do masquerade to bring current dest chan here for the new bridge */ + + /* put placeholder a name to remember is used to masquerade a channel for Bridge */ + snprintf(final_dest_chan->name, sizeof(final_dest_chan->name), "Bridge/%s", current_dest_chan->name); + ast_moh_stop(current_dest_chan); /* is really needed? */ + ast_mutex_lock(¤t_dest_chan->lock); + ast_setstate(final_dest_chan, current_dest_chan->_state); + final_dest_chan->readformat = current_dest_chan->readformat; + final_dest_chan->writeformat = current_dest_chan->writeformat; + ast_channel_masquerade(final_dest_chan, current_dest_chan); + ast_mutex_lock(&final_dest_chan->lock); + ast_do_masquerade(final_dest_chan); + ast_explicit_goto(final_dest_chan, current_dest_chan->context, current_dest_chan->exten, current_dest_chan->priority + 1); + ast_mutex_unlock(&final_dest_chan->lock); + ast_mutex_unlock(¤t_dest_chan->lock); + + /* now current_dest_chan is a ZOMBIE and with soft hangup set to 1 and final_dest_chan is the end point */ + /* Make the channels compatible - err if we fail in doing so.*/ + 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\r\n" + "Channel1: %s\r\n" + "Channel2: %s\r\n" + "Tone: %s\r\n", + chan->name, final_dest_chan->name, playtone ? "Yes" : "No"); + ast_hangup(final_dest_chan); /* can we do something more friendly here? */ + pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "INCOMPATIBLE"); + LOCAL_USER_REMOVE(u); + return 0; + } + /* Report that the bridge will be successful */ + manager_event(EVENT_FLAG_CALL, "BridgeExec", + "Response: Success\r\n" + "Channel1: %s\r\n" + "Channel2: %s\r\n" + "Tone: %s\r\n", + chan->name, final_dest_chan->name, playtone ? "Yes" : "No"); + + /* We have valid end points for the bridge, is just matter of setting up the bridge object and start 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; + + if ( playtone && !ast_strlen_zero(xfersound) ) { + if ( !ast_streamfile(chan, xfersound, chan->language) ) { + if ( ast_waitstream(chan, "") < 0 ) { + ast_log(LOG_WARNING, "Failed to play courtesy tone on %s!\n", chan->name); + } else { + ast_log(LOG_DEBUG, "Success Played courtesy tone on %s!\n", chan->name); + } + } + } + + res = ast_bridge_call(chan, final_dest_chan, &bconfig); + pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS"); + + /* if the other end has not been hangup at bridge end, return it to its original place +1 priority */ + if ( !ast_check_hangup(final_dest_chan) ) { + ast_log(LOG_DEBUG, "starting PBX in %s,%s,%d\n", final_dest_chan->context, final_dest_chan->exten, final_dest_chan->priority); + res = ast_pbx_start(final_dest_chan); + if ( res != AST_PBX_SUCCESS ) { + 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, "dest end point hangup, not starting PBX\n"); + ast_hangup(final_dest_chan); + } + LOCAL_USER_REMOVE(u); + return 0; +} + + +/* DCT: END SECOND APP_BRIDGE CHUNK */ + int reload(void) { return load_config(); } @@ -2149,6 +2525,8 @@ { 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)); @@ -2162,6 +2540,7 @@ res = ast_register_application(parkcall, park_call_exec, synopsis2, descrip2); if (!res) { ast_manager_register("ParkedCalls", 0, manager_parking_status, "List parked calls" ); + ast_manager_register2("Bridge", EVENT_FLAG_COMMAND, action_bridge, "Bridge two live channels", mandescr_bridge ); } return res; } @@ -2172,9 +2551,11 @@ STANDARD_HANGUP_LOCALUSERS; ast_manager_unregister("ParkedCalls"); + ast_manager_unregister("Bridge"); ast_cli_unregister(&showfeatures); ast_cli_unregister(&showparked); ast_unregister_application(parkcall); + ast_unregister_application(app_bridge); return ast_unregister_application(parkedcall); }