diff --git a/include/asterisk/stasis_app.h b/include/asterisk/stasis_app.h index 881ef72a49b72cb38fb10aff4969fd9493865a12..f31bab8d905bcfbf8ffc6b2cddbac21e7d13334f 100644 --- a/include/asterisk/stasis_app.h +++ b/include/asterisk/stasis_app.h @@ -136,6 +136,20 @@ struct stasis_app_control *stasis_app_control_find_by_channel_id( const char *stasis_app_control_get_channel_id( const struct stasis_app_control *control); +/*! + * \brief Dial an endpoint and bridge it to a channel in \c res_stasis + * + * If the channel is no longer in \c res_stasis, this function does nothing. + * + * \param control Control for \c res_stasis + * \param endpoint The endpoint to dial. + * \param timeout The amount of time to wait for answer, before giving up. + * + * \return 0 for success + * \return -1 for error. + */ +int stasis_app_control_dial(struct stasis_app_control *control, const char *endpoint, int timeout); + /*! * \brief Exit \c res_stasis and continue execution in the dialplan. * diff --git a/res/res_stasis_http_channels.c b/res/res_stasis_http_channels.c index 6f5515110cd2045c9fff8d68a5b5ef68d32a9907..9bc4b3bcc14e0fd1c6d226c45d3eea58a7129f60 100644 --- a/res/res_stasis_http_channels.c +++ b/res/res_stasis_http_channels.c @@ -170,6 +170,9 @@ static void stasis_http_dial_cb( if (strcmp(i->name, "context") == 0) { args.context = (i->value); } else + if (strcmp(i->name, "timeout") == 0) { + args.timeout = atoi(i->value); + } else {} } for (i = path_vars; i; i = i->next) { diff --git a/res/stasis/control.c b/res/stasis/control.c index 310b66cbc7e61ce47b333c07366efbb8b3376d69..e05084b88772fbde1a55ec125fcfca84caabce9f 100644 --- a/res/stasis/control.c +++ b/res/stasis/control.c @@ -31,7 +31,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "command.h" #include "control.h" +#include "asterisk/dial.h" #include "asterisk/bridging.h" +#include "asterisk/bridging_basic.h" #include "asterisk/bridging_features.h" #include "asterisk/pbx.h" @@ -86,6 +88,80 @@ static struct stasis_app_command *exec_command( return command; } +struct stasis_app_control_dial_data { + char endpoint[AST_CHANNEL_NAME]; + int timeout; +}; + +static void *app_control_dial(struct stasis_app_control *control, + struct ast_channel *chan, void *data) +{ + RAII_VAR(struct ast_dial *, dial, ast_dial_create(), ast_dial_destroy); + RAII_VAR(struct stasis_app_control_dial_data *, dial_data, data, ast_free); + enum ast_dial_result res; + char *tech, *resource; + + struct ast_channel *new_chan; + struct ast_bridge *bridge; + + tech = dial_data->endpoint; + if (!(resource = strchr(tech, '/'))) { + return NULL; + } + *resource++ = '\0'; + + if (!dial) { + ast_log(LOG_ERROR, "Failed to create dialing structure.\n"); + return NULL; + } + + if (ast_dial_append(dial, tech, resource) < 0) { + ast_log(LOG_ERROR, "Failed to add %s/%s to dialing structure.\n", tech, resource); + return NULL; + } + + ast_dial_set_global_timeout(dial, dial_data->timeout); + + res = ast_dial_run(dial, NULL, 0); + + if (res != AST_DIAL_RESULT_ANSWERED || !(new_chan = ast_dial_answered_steal(dial))) { + return NULL; + } + + if (!(bridge = ast_bridge_basic_new())) { + ast_log(LOG_ERROR, "Failed to create basic bridge.\n"); + return NULL; + } + + ast_bridge_impart(bridge, new_chan, NULL, NULL, 1); + stasis_app_control_add_channel_to_bridge(control, bridge); + + return NULL; +} + +int stasis_app_control_dial(struct stasis_app_control *control, const char *endpoint, int timeout) +{ + struct stasis_app_control_dial_data *dial_data; + + if (!(dial_data = ast_calloc(1, sizeof(*dial_data)))) { + return -1; + } + + ast_copy_string(dial_data->endpoint, endpoint, sizeof(dial_data->endpoint)); + + if (timeout > 0) { + dial_data->timeout = timeout * 1000; + } else if (timeout == -1) { + dial_data->timeout = -1; + } else { + dial_data->timeout = 30000; + } + + stasis_app_send_command_async(control, app_control_dial, dial_data); + + return 0; +} + int control_is_done(struct stasis_app_control *control) { /* Called from stasis_app_exec thread; no lock needed */ diff --git a/res/stasis_http/resource_channels.c b/res/stasis_http/resource_channels.c index 1fc16e576e871a98d63cdd14a7087fa0d8d4057d..aeeafa7052f458531426744e42fcf6ced5f7254b 100644 --- a/res/stasis_http/resource_channels.c +++ b/res/stasis_http/resource_channels.c @@ -34,6 +34,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/file.h" #include "asterisk/pbx.h" +#include "asterisk/dial.h" +#include "asterisk/bridging.h" #include "asterisk/callerid.h" #include "asterisk/stasis_app.h" #include "asterisk/stasis_app_playback.h" @@ -80,7 +82,19 @@ static struct stasis_app_control *find_control( void stasis_http_dial(struct ast_variable *headers, struct ast_dial_args *args, struct stasis_http_response *response) { - ast_log(LOG_ERROR, "TODO: stasis_http_dial\n"); + struct stasis_app_control *control; + + control = find_control(response, args->channel_id); + if (control == NULL) { + return; + } + + if (stasis_app_control_dial(control, args->endpoint, args->timeout)) { + stasis_http_response_alloc_failed(response); + return; + } + + stasis_http_response_no_content(response); } void stasis_http_continue_in_dialplan( diff --git a/res/stasis_http/resource_channels.h b/res/stasis_http/resource_channels.h index ddc5f326b10f3973665a2bc2d363f9bd32ca3cf0..57f2a63d20821cf35be66eb71d1faee5ab1dbb9d 100644 --- a/res/stasis_http/resource_channels.h +++ b/res/stasis_http/resource_channels.h @@ -113,6 +113,8 @@ struct ast_dial_args { const char *extension; /*! \brief When routing via dialplan, the context use. If omitted, uses 'default' */ const char *context; + /*! \brief Timeout (in seconds) before giving up dialing, or -1 for no timeout. */ + int timeout; }; /*! * \brief Create a new channel (originate) and bridge to this channel. diff --git a/rest-api/api-docs/channels.json b/rest-api/api-docs/channels.json index 15e76e4f8b124b599d00100b44bfcbb146bce04e..623cb17bb24827352b4f81ec64f416fcd7401318 100644 --- a/rest-api/api-docs/channels.json +++ b/rest-api/api-docs/channels.json @@ -183,6 +183,15 @@ "required": false, "allowMultiple": false, "dataType": "string" + }, + { + "name": "timeout", + "description": "Timeout (in seconds) before giving up dialing, or -1 for no timeout.", + "paramType": "query", + "required": false, + "allowMultiple": false, + "dataType": "int", + "defaultValue": 30 } ], "errorResponses": [