summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Parker <jparker@digium.com>2013-07-01 18:19:15 +0000
committerJason Parker <jparker@digium.com>2013-07-01 18:19:15 +0000
commitf41faf0b7dc4c7bb683f9a81b4c09dcfeeef2119 (patch)
treec99142cbc995fe24376fc926ea094a929bbcde56
parentf306dbd8412778ef31df791b658dc38e15629ae3 (diff)
ARI: Implement channel dial.
This creates a new outbound channel, and bridges it to a channel already in the Stasis application. (closes issue ASTERISK-21620) Review: https://reviewboard.asterisk.org/r/2634/ git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@393326 65c4cc65-6c06-0410-ace0-fbb531ad65f3
-rw-r--r--include/asterisk/stasis_app.h14
-rw-r--r--res/res_stasis_http_channels.c3
-rw-r--r--res/stasis/control.c76
-rw-r--r--res/stasis_http/resource_channels.c16
-rw-r--r--res/stasis_http/resource_channels.h2
-rw-r--r--rest-api/api-docs/channels.json9
6 files changed, 119 insertions, 1 deletions
diff --git a/include/asterisk/stasis_app.h b/include/asterisk/stasis_app.h
index 881ef72a4..f31bab8d9 100644
--- a/include/asterisk/stasis_app.h
+++ b/include/asterisk/stasis_app.h
@@ -137,6 +137,20 @@ 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.
*
* If the channel is no longer in \c res_stasis, this function does nothing.
diff --git a/res/res_stasis_http_channels.c b/res/res_stasis_http_channels.c
index 6f5515110..9bc4b3bcc 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 310b66cbc..e05084b88 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 1fc16e576..aeeafa705 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 ddc5f326b..57f2a63d2 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 15e76e4f8..623cb17bb 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": [