From e47794ead1fb35527728a0c655585af35679e258 Mon Sep 17 00:00:00 2001
From: Jonathan Rose <jrose@digium.com>
Date: Mon, 5 Aug 2013 16:59:13 +0000
Subject: [PATCH] ARI: bridges/{bridgeID}/addChannel: add roles parameter

Roles are now cleared with each entry into a bridge with addChannel.
If the roles parameter is present, the role specified will be applied
to all channels being added with the addChannel command.

(closes issue ASTERISK-21973)
Reported by: Matt Jordan
https://reviewboard.asterisk.org/r/2691/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@396182 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 include/asterisk/bridge_roles.h |  7 +++++++
 include/asterisk/stasis_app.h   | 18 ++++++++++++++++++
 main/bridge_roles.c             | 19 +++++++++++++++++++
 res/ari/resource_bridges.c      | 10 ++++++++++
 res/ari/resource_bridges.h      |  2 ++
 res/res_ari_bridges.c           |  3 +++
 res/stasis/control.c            | 10 ++++++++++
 rest-api/api-docs/bridges.json  |  8 ++++++++
 8 files changed, 77 insertions(+)

diff --git a/include/asterisk/bridge_roles.h b/include/asterisk/bridge_roles.h
index d90e564b71..f91a6b0aa2 100644
--- a/include/asterisk/bridge_roles.h
+++ b/include/asterisk/bridge_roles.h
@@ -51,6 +51,13 @@ int ast_channel_add_bridge_role(struct ast_channel *chan, const char *role_name)
  */
 void ast_channel_remove_bridge_role(struct ast_channel *chan, const char *role_name);
 
+/*!
+ * \brief Removes all bridge roles currently on a channel
+ *
+ * \param chan Channel the roles are being removed from
+ */
+void ast_channel_clear_bridge_roles(struct ast_channel *chan);
+
 /*!
  * \brief Set a role option on a channel
  * \param channel Channel receiving the role option
diff --git a/include/asterisk/stasis_app.h b/include/asterisk/stasis_app.h
index 898cb18407..87b4bdb419 100644
--- a/include/asterisk/stasis_app.h
+++ b/include/asterisk/stasis_app.h
@@ -173,6 +173,24 @@ const char *stasis_app_control_get_channel_id(
  */
 int stasis_app_control_dial(struct stasis_app_control *control, const char *endpoint, int timeout);
 
+/*!
+ * \brief Apply a bridge role to a channel controlled by a stasis app control
+ *
+ * \param control Control for \c res_stasis
+ * \param role Role to apply
+ *
+ * \return 0 for success
+ * \return -1 for error.
+ */
+int stasis_app_control_add_role(struct stasis_app_control *control, const char *role);
+
+/*!
+ * \brief Clear bridge roles currently applied to a channel controlled by a stasis app control
+ *
+ * \param control Control for \c res_stasis
+ */
+void stasis_app_control_clear_roles(struct stasis_app_control *control);
+
 /*!
  * \brief Exit \c res_stasis and continue execution in the dialplan.
  *
diff --git a/main/bridge_roles.c b/main/bridge_roles.c
index dd1012b9cc..1d781ca93c 100644
--- a/main/bridge_roles.c
+++ b/main/bridge_roles.c
@@ -355,6 +355,25 @@ void ast_channel_remove_bridge_role(struct ast_channel *chan, const char *role_n
 	ast_debug(2, "Role %s did not exist on channel %s\n", role_name, ast_channel_name(chan));
 }
 
+void ast_channel_clear_bridge_roles(struct ast_channel *chan)
+{
+	struct bridge_roles_datastore *roles_datastore = fetch_bridge_roles_datastore(chan);
+	struct bridge_role *role;
+
+	if (!roles_datastore) {
+		/* The roles datastore didn't already exist, so there is no need to remove any roles */
+		ast_debug(2, "Roles did not exist on channel %s\n", ast_channel_name(chan));
+		return;
+	}
+
+	AST_LIST_TRAVERSE_SAFE_BEGIN(&roles_datastore->role_list, role, list) {
+		ast_debug(2, "Removing bridge role %s from channel %s\n", role->role, ast_channel_name(chan));
+		AST_LIST_REMOVE_CURRENT(list);
+		bridge_role_destroy(role);
+	}
+	AST_LIST_TRAVERSE_SAFE_END;
+}
+
 int ast_channel_set_bridge_role_option(struct ast_channel *channel, const char *role_name, const char *option, const char *value)
 {
 	struct bridge_role *role = get_role_from_channel(channel, role_name);
diff --git a/res/ari/resource_bridges.c b/res/ari/resource_bridges.c
index e4c7194ff2..65fe8d79f7 100644
--- a/res/ari/resource_bridges.c
+++ b/res/ari/resource_bridges.c
@@ -175,6 +175,16 @@ void ast_ari_add_channel_to_bridge(struct ast_variable *headers, struct ast_add_
 		return;
 	}
 
+	for (i = 0; i < list->count; ++i) {
+		stasis_app_control_clear_roles(list->controls[i]);
+		if (!ast_strlen_zero(args->role)) {
+			if (stasis_app_control_add_role(list->controls[i], args->role)) {
+				ast_ari_response_alloc_failed(response);
+				return;
+			}
+		}
+	}
+
 	for (i = 0; i < list->count; ++i) {
 		stasis_app_control_add_channel_to_bridge(list->controls[i], bridge);
 	}
diff --git a/res/ari/resource_bridges.h b/res/ari/resource_bridges.h
index d124324133..d82cb6f8bb 100644
--- a/res/ari/resource_bridges.h
+++ b/res/ari/resource_bridges.h
@@ -103,6 +103,8 @@ struct ast_add_channel_to_bridge_args {
 	size_t channel_count;
 	/*! \brief Parsing context for channel. */
 	char *channel_parse;
+	/*! \brief Channel's role in the bridge */
+	const char *role;
 };
 /*!
  * \brief Add a channel to a bridge.
diff --git a/res/res_ari_bridges.c b/res/res_ari_bridges.c
index 57dc6a48da..05ea12e6ac 100644
--- a/res/res_ari_bridges.c
+++ b/res/res_ari_bridges.c
@@ -324,6 +324,9 @@ static void ast_ari_add_channel_to_bridge_cb(
 				args.channel[j] = (vals[j]);
 			}
 		} else
+		if (strcmp(i->name, "role") == 0) {
+			args.role = (i->value);
+		} else
 		{}
 	}
 	for (i = path_vars; i; i = i->next) {
diff --git a/res/stasis/control.c b/res/stasis/control.c
index 211566e11b..dcc029701c 100644
--- a/res/stasis/control.c
+++ b/res/stasis/control.c
@@ -168,6 +168,16 @@ int stasis_app_control_dial(struct stasis_app_control *control, const char *endp
 	return 0;
 }
 
+int stasis_app_control_add_role(struct stasis_app_control *control, const char *role)
+{
+	return ast_channel_add_bridge_role(control->channel, role);
+}
+
+void stasis_app_control_clear_roles(struct stasis_app_control *control)
+{
+	ast_channel_clear_bridge_roles(control->channel);
+}
+
 int control_is_done(struct stasis_app_control *control)
 {
 	/* Called from stasis_app_exec thread; no lock needed */
diff --git a/rest-api/api-docs/bridges.json b/rest-api/api-docs/bridges.json
index 57954dd277..940ed0aedf 100644
--- a/rest-api/api-docs/bridges.json
+++ b/rest-api/api-docs/bridges.json
@@ -119,6 +119,14 @@
 							"required": true,
 							"allowMultiple": true,
 							"dataType": "string"
+						},
+						{
+							"name": "role",
+							"description": "Channel's role in the bridge",
+							"paramType": "query",
+							"required": false,
+							"allowMultiple": false,
+							"dataType": "string"
 						}
 					],
 					"errorResponses": [
-- 
GitLab