[Asterisk-code-review] res_stasis.c: Added POST channels/{channelId}/application ARI (...asterisk[master])

sungtae kim asteriskteam at digium.com
Sat Mar 30 13:04:18 CDT 2019


sungtae kim has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/11211


Change subject: res_stasis.c: Added POST channels/{channelId}/application ARI
......................................................................

res_stasis.c: Added POST channels/{channelId}/application ARI

This ARI allows to execute the application within a stasis application.

Added ARI events for status notifination.
ChannelApplicationStarted: Notification that a application has been started.
ChannelApplicationFinished: Notification that a application has been finished.

ASTERISK-28365

Change-Id: I037bcb9fda54563fdcd2197a5cbda642fdec70e8
---
M include/asterisk/pbx.h
M include/asterisk/stasis_app.h
M include/asterisk/stasis_app_impl.h
M main/pbx_app.c
M res/ari/ari_model_validators.c
M res/ari/ari_model_validators.h
M res/ari/resource_channels.c
M res/ari/resource_channels.h
M res/res_ari_channels.c
M res/res_stasis.c
M res/stasis/control.c
M rest-api/api-docs/channels.json
M rest-api/api-docs/events.json
13 files changed, 823 insertions(+), 3 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/11/11211/1

diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h
index cbc5ec2..633a933 100644
--- a/include/asterisk/pbx.h
+++ b/include/asterisk/pbx.h
@@ -267,6 +267,8 @@
  */
 int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data);
 
+int pbx_exec_app(struct ast_channel *c, struct ast_app *app, const char *data);
+
 /*!
  * \brief Register a new context or find an existing one
  *
diff --git a/include/asterisk/stasis_app.h b/include/asterisk/stasis_app.h
index 01c7ff4..a9bc68a 100644
--- a/include/asterisk/stasis_app.h
+++ b/include/asterisk/stasis_app.h
@@ -1018,6 +1018,7 @@
  */
 int stasis_app_event_allowed(const char *app_name, struct ast_json *event);
 
+int stasis_app_control_channel_app(struct stasis_app_control *control, const char *app_name, const char *app_args);
 /*! @} */
 
 #endif /* _ASTERISK_STASIS_APP_H */
diff --git a/include/asterisk/stasis_app_impl.h b/include/asterisk/stasis_app_impl.h
index ce60804..6fce853 100644
--- a/include/asterisk/stasis_app_impl.h
+++ b/include/asterisk/stasis_app_impl.h
@@ -105,4 +105,9 @@
 int stasis_app_send_command_async(struct stasis_app_control *control,
 	stasis_app_command_cb command, void *data, command_data_destructor_fn data_destructor);
 
+
+int publish_channel_application_started_event(struct ast_channel *chan, const char* app, const char* app_args);
+int publish_channel_application_finished_event(struct ast_channel *chan, const char* app, const char* app_args, const char* status);
+
+
 #endif /* _ASTERISK_RES_STASIS_H */
diff --git a/main/pbx_app.c b/main/pbx_app.c
index df8126c..bf3c811 100644
--- a/main/pbx_app.c
+++ b/main/pbx_app.c
@@ -498,6 +498,25 @@
 	return res;
 }
 
+int pbx_exec_app(struct ast_channel *c,	/*!< Channel */
+	     struct ast_app *app,	/*!< Application */
+	     const char *data)		/*!< Data for execution */
+{
+	int ret;
+
+	if (app->module) {
+		ast_module_ref(app->module);
+	}
+
+	ret = app->execute(c, S_OR(data, ""));
+
+	if (app->module) {
+		ast_module_unref(app->module);
+	}
+
+	return ret;
+}
+
 static struct ast_cli_entry app_cli[] = {
 	AST_CLI_DEFINE(handle_show_applications, "Shows registered dialplan applications"),
 	AST_CLI_DEFINE(handle_show_application, "Describe a specific dialplan application"),
diff --git a/res/ari/ari_model_validators.c b/res/ari/ari_model_validators.c
index bc7ac6b..0ee21c1 100644
--- a/res/ari/ari_model_validators.c
+++ b/res/ari/ari_model_validators.c
@@ -1261,6 +1261,76 @@
 	return ast_ari_validate_channel;
 }
 
+int ast_ari_validate_channel_application(struct ast_json *json)
+{
+	int res = 1;
+	struct ast_json_iter *iter;
+	int has_args = 0;
+	int has_name = 0;
+	int has_status = 0;
+
+	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
+		if (strcmp("args", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_args = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplication field args failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("name", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_name = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplication field name failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("status", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_status = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplication field status failed validation\n");
+				res = 0;
+			}
+		} else
+		{
+			ast_log(LOG_ERROR,
+				"ARI ChannelApplication has undocumented field %s\n",
+				ast_json_object_iter_key(iter));
+			res = 0;
+		}
+	}
+
+	if (!has_args) {
+		ast_log(LOG_ERROR, "ARI ChannelApplication missing required field args\n");
+		res = 0;
+	}
+
+	if (!has_name) {
+		ast_log(LOG_ERROR, "ARI ChannelApplication missing required field name\n");
+		res = 0;
+	}
+
+	if (!has_status) {
+		ast_log(LOG_ERROR, "ARI ChannelApplication missing required field status\n");
+		res = 0;
+	}
+
+	return res;
+}
+
+ari_validator ast_ari_validate_channel_application_fn(void)
+{
+	return ast_ari_validate_channel_application;
+}
+
 int ast_ari_validate_dialed(struct ast_json *json)
 {
 	int res = 1;
@@ -2050,6 +2120,7 @@
 	struct ast_json_iter *iter;
 	int has_type = 0;
 	int has_application = 0;
+	int has_timestamp = 0;
 	int has_args = 0;
 	int has_channel = 0;
 	int has_destination = 0;
@@ -2086,6 +2157,7 @@
 		} else
 		if (strcmp("timestamp", ast_json_object_iter_key(iter)) == 0) {
 			int prop_is_valid;
+			has_timestamp = 1;
 			prop_is_valid = ast_ari_validate_date(
 				ast_json_object_iter_value(iter));
 			if (!prop_is_valid) {
@@ -2142,6 +2214,11 @@
 		res = 0;
 	}
 
+	if (!has_timestamp) {
+		ast_log(LOG_ERROR, "ARI ApplicationMoveFailed missing required field timestamp\n");
+		res = 0;
+	}
+
 	if (!has_args) {
 		ast_log(LOG_ERROR, "ARI ApplicationMoveFailed missing required field args\n");
 		res = 0;
@@ -3093,6 +3170,214 @@
 	return ast_ari_validate_bridge_video_source_changed;
 }
 
+int ast_ari_validate_channel_application_finished(struct ast_json *json)
+{
+	int res = 1;
+	struct ast_json_iter *iter;
+	int has_type = 0;
+	int has_application = 0;
+	int has_timestamp = 0;
+	int has_channel = 0;
+
+	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
+		if (strcmp("asterisk_id", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationFinished field asterisk_id failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("type", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_type = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationFinished field type failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("application", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_application = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationFinished field application failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("timestamp", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_timestamp = 1;
+			prop_is_valid = ast_ari_validate_date(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationFinished field timestamp failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("channel", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_channel = 1;
+			prop_is_valid = ast_ari_validate_channel(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationFinished field channel failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("channel_application", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			prop_is_valid = ast_ari_validate_channel_application(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationFinished field channel_application failed validation\n");
+				res = 0;
+			}
+		} else
+		{
+			ast_log(LOG_ERROR,
+				"ARI ChannelApplicationFinished has undocumented field %s\n",
+				ast_json_object_iter_key(iter));
+			res = 0;
+		}
+	}
+
+	if (!has_type) {
+		ast_log(LOG_ERROR, "ARI ChannelApplicationFinished missing required field type\n");
+		res = 0;
+	}
+
+	if (!has_application) {
+		ast_log(LOG_ERROR, "ARI ChannelApplicationFinished missing required field application\n");
+		res = 0;
+	}
+
+	if (!has_timestamp) {
+		ast_log(LOG_ERROR, "ARI ChannelApplicationFinished missing required field timestamp\n");
+		res = 0;
+	}
+
+	if (!has_channel) {
+		ast_log(LOG_ERROR, "ARI ChannelApplicationFinished missing required field channel\n");
+		res = 0;
+	}
+
+	return res;
+}
+
+ari_validator ast_ari_validate_channel_application_finished_fn(void)
+{
+	return ast_ari_validate_channel_application_finished;
+}
+
+int ast_ari_validate_channel_application_started(struct ast_json *json)
+{
+	int res = 1;
+	struct ast_json_iter *iter;
+	int has_type = 0;
+	int has_application = 0;
+	int has_timestamp = 0;
+	int has_channel = 0;
+
+	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
+		if (strcmp("asterisk_id", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationStarted field asterisk_id failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("type", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_type = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationStarted field type failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("application", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_application = 1;
+			prop_is_valid = ast_ari_validate_string(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationStarted field application failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("timestamp", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_timestamp = 1;
+			prop_is_valid = ast_ari_validate_date(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationStarted field timestamp failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("channel", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			has_channel = 1;
+			prop_is_valid = ast_ari_validate_channel(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationStarted field channel failed validation\n");
+				res = 0;
+			}
+		} else
+		if (strcmp("channel_application", ast_json_object_iter_key(iter)) == 0) {
+			int prop_is_valid;
+			prop_is_valid = ast_ari_validate_channel_application(
+				ast_json_object_iter_value(iter));
+			if (!prop_is_valid) {
+				ast_log(LOG_ERROR, "ARI ChannelApplicationStarted field channel_application failed validation\n");
+				res = 0;
+			}
+		} else
+		{
+			ast_log(LOG_ERROR,
+				"ARI ChannelApplicationStarted has undocumented field %s\n",
+				ast_json_object_iter_key(iter));
+			res = 0;
+		}
+	}
+
+	if (!has_type) {
+		ast_log(LOG_ERROR, "ARI ChannelApplicationStarted missing required field type\n");
+		res = 0;
+	}
+
+	if (!has_application) {
+		ast_log(LOG_ERROR, "ARI ChannelApplicationStarted missing required field application\n");
+		res = 0;
+	}
+
+	if (!has_timestamp) {
+		ast_log(LOG_ERROR, "ARI ChannelApplicationStarted missing required field timestamp\n");
+		res = 0;
+	}
+
+	if (!has_channel) {
+		ast_log(LOG_ERROR, "ARI ChannelApplicationStarted missing required field channel\n");
+		res = 0;
+	}
+
+	return res;
+}
+
+ari_validator ast_ari_validate_channel_application_started_fn(void)
+{
+	return ast_ari_validate_channel_application_started;
+}
+
 int ast_ari_validate_channel_caller_id(struct ast_json *json)
 {
 	int res = 1;
@@ -5446,6 +5731,12 @@
 	if (strcmp("BridgeVideoSourceChanged", discriminator) == 0) {
 		return ast_ari_validate_bridge_video_source_changed(json);
 	} else
+	if (strcmp("ChannelApplicationFinished", discriminator) == 0) {
+		return ast_ari_validate_channel_application_finished(json);
+	} else
+	if (strcmp("ChannelApplicationStarted", discriminator) == 0) {
+		return ast_ari_validate_channel_application_started(json);
+	} else
 	if (strcmp("ChannelCallerId", discriminator) == 0) {
 		return ast_ari_validate_channel_caller_id(json);
 	} else
@@ -5653,6 +5944,12 @@
 	if (strcmp("BridgeVideoSourceChanged", discriminator) == 0) {
 		return ast_ari_validate_bridge_video_source_changed(json);
 	} else
+	if (strcmp("ChannelApplicationFinished", discriminator) == 0) {
+		return ast_ari_validate_channel_application_finished(json);
+	} else
+	if (strcmp("ChannelApplicationStarted", discriminator) == 0) {
+		return ast_ari_validate_channel_application_started(json);
+	} else
 	if (strcmp("ChannelCallerId", discriminator) == 0) {
 		return ast_ari_validate_channel_caller_id(json);
 	} else
diff --git a/res/ari/ari_model_validators.h b/res/ari/ari_model_validators.h
index 8fa28bc..993dc9f 100644
--- a/res/ari/ari_model_validators.h
+++ b/res/ari/ari_model_validators.h
@@ -442,6 +442,24 @@
 ari_validator ast_ari_validate_channel_fn(void);
 
 /*!
+ * \brief Validator for ChannelApplication.
+ *
+ * Channel application info
+ *
+ * \param json JSON object to validate.
+ * \returns True (non-zero) if valid.
+ * \returns False (zero) if invalid.
+ */
+int ast_ari_validate_channel_application(struct ast_json *json);
+
+/*!
+ * \brief Function pointer to ast_ari_validate_channel_application().
+ *
+ * See \ref ast_ari_model_validators.h for more details.
+ */
+ari_validator ast_ari_validate_channel_application_fn(void);
+
+/*!
  * \brief Validator for Dialed.
  *
  * Dialed channel information.
@@ -770,6 +788,42 @@
 ari_validator ast_ari_validate_bridge_video_source_changed_fn(void);
 
 /*!
+ * \brief Validator for ChannelApplicationFinished.
+ *
+ * Notification that a application has been finished.
+ *
+ * \param json JSON object to validate.
+ * \returns True (non-zero) if valid.
+ * \returns False (zero) if invalid.
+ */
+int ast_ari_validate_channel_application_finished(struct ast_json *json);
+
+/*!
+ * \brief Function pointer to ast_ari_validate_channel_application_finished().
+ *
+ * See \ref ast_ari_model_validators.h for more details.
+ */
+ari_validator ast_ari_validate_channel_application_finished_fn(void);
+
+/*!
+ * \brief Validator for ChannelApplicationStarted.
+ *
+ * Notification that a application has been started.
+ *
+ * \param json JSON object to validate.
+ * \returns True (non-zero) if valid.
+ * \returns False (zero) if invalid.
+ */
+int ast_ari_validate_channel_application_started(struct ast_json *json);
+
+/*!
+ * \brief Function pointer to ast_ari_validate_channel_application_started().
+ *
+ * See \ref ast_ari_model_validators.h for more details.
+ */
+ari_validator ast_ari_validate_channel_application_started_fn(void);
+
+/*!
  * \brief Validator for ChannelCallerId.
  *
  * Channel changed Caller ID.
@@ -1497,6 +1551,10 @@
  * - language: string (required)
  * - name: string (required)
  * - state: string (required)
+ * ChannelApplication
+ * - args: string (required)
+ * - name: string (required)
+ * - status: string (required)
  * Dialed
  * DialplanCEP
  * - context: string (required)
@@ -1550,7 +1608,7 @@
  * - asterisk_id: string
  * - type: string (required)
  * - application: string (required)
- * - timestamp: Date
+ * - timestamp: Date (required)
  * - args: List[string] (required)
  * - channel: Channel (required)
  * - destination: string (required)
@@ -1619,6 +1677,20 @@
  * - timestamp: Date (required)
  * - bridge: Bridge (required)
  * - old_video_source_id: string
+ * ChannelApplicationFinished
+ * - asterisk_id: string
+ * - type: string (required)
+ * - application: string (required)
+ * - timestamp: Date (required)
+ * - channel: Channel (required)
+ * - channel_application: ChannelApplication
+ * ChannelApplicationStarted
+ * - asterisk_id: string
+ * - type: string (required)
+ * - application: string (required)
+ * - timestamp: Date (required)
+ * - channel: Channel (required)
+ * - channel_application: ChannelApplication
  * ChannelCallerId
  * - asterisk_id: string
  * - type: string (required)
diff --git a/res/ari/resource_channels.c b/res/ari/resource_channels.c
index eca70ce..5674731 100644
--- a/res/ari/resource_channels.c
+++ b/res/ari/resource_channels.c
@@ -1966,3 +1966,26 @@
 
 	ast_ari_response_no_content(response);
 }
+
+void ast_ari_channels_application(
+		struct ast_variable *headers,
+		struct ast_ari_channels_application_args *args,
+		struct ast_ari_response *response)
+{
+	RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
+
+	control = find_control(response, args->channel_id);
+	if (control == NULL) {
+		/* Response filled in by find_control */
+		return;
+	}
+
+	if (stasis_app_control_channel_app(control, args->app, S_OR(args->app_args, ""))) {
+		ast_ari_response_error(response, 500, "Internal Server Error",
+			"Failed to execute applications");
+		return;
+	}
+
+	ast_ari_response_no_content(response);
+}
+
diff --git a/res/ari/resource_channels.h b/res/ari/resource_channels.h
index fdd7a6b..a43ab37 100644
--- a/res/ari/resource_channels.h
+++ b/res/ari/resource_channels.h
@@ -809,5 +809,33 @@
  * \param[out] response HTTP response
  */
 void ast_ari_channels_dial(struct ast_variable *headers, struct ast_ari_channels_dial_args *args, struct ast_ari_response *response);
+/*! Argument struct for ast_ari_channels_application() */
+struct ast_ari_channels_application_args {
+	/*! Channel's id */
+	const char *channel_id;
+	/*! Application's name */
+	const char *app;
+	/*! Application's data */
+	const char *app_args;
+};
+/*!
+ * \brief Body parsing function for /channels/{channelId}/application.
+ * \param body The JSON body from which to parse parameters.
+ * \param[out] args The args structure to parse into.
+ * \retval zero on success
+ * \retval non-zero on failure
+ */
+int ast_ari_channels_application_parse_body(
+	struct ast_json *body,
+	struct ast_ari_channels_application_args *args);
+
+/*!
+ * \brief Execute a application on a channel.
+ *
+ * \param headers HTTP headers
+ * \param args Swagger parameters
+ * \param[out] response HTTP response
+ */
+void ast_ari_channels_application(struct ast_variable *headers, struct ast_ari_channels_application_args *args, struct ast_ari_response *response);
 
 #endif /* _ASTERISK_RESOURCE_CHANNELS_H */
diff --git a/res/res_ari_channels.c b/res/res_ari_channels.c
index 3d96d60..8f0d541 100644
--- a/res/res_ari_channels.c
+++ b/res/res_ari_channels.c
@@ -2749,6 +2749,95 @@
 fin: __attribute__((unused))
 	return;
 }
+int ast_ari_channels_application_parse_body(
+	struct ast_json *body,
+	struct ast_ari_channels_application_args *args)
+{
+	struct ast_json *field;
+	/* Parse query parameters out of it */
+	field = ast_json_object_get(body, "app");
+	if (field) {
+		args->app = ast_json_string_get(field);
+	}
+	field = ast_json_object_get(body, "app_args");
+	if (field) {
+		args->app_args = ast_json_string_get(field);
+	}
+	return 0;
+}
+
+/*!
+ * \brief Parameter parsing callback for /channels/{channelId}/application.
+ * \param get_params GET parameters in the HTTP request.
+ * \param path_vars Path variables extracted from the request.
+ * \param headers HTTP headers.
+ * \param[out] response Response to the HTTP request.
+ */
+static void ast_ari_channels_application_cb(
+	struct ast_tcptls_session_instance *ser,
+	struct ast_variable *get_params, struct ast_variable *path_vars,
+	struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
+{
+	struct ast_ari_channels_application_args args = {};
+	struct ast_variable *i;
+#if defined(AST_DEVMODE)
+	int is_valid;
+	int code;
+#endif /* AST_DEVMODE */
+
+	for (i = get_params; i; i = i->next) {
+		if (strcmp(i->name, "app") == 0) {
+			args.app = (i->value);
+		} else
+		if (strcmp(i->name, "app_args") == 0) {
+			args.app_args = (i->value);
+		} else
+		{}
+	}
+	for (i = path_vars; i; i = i->next) {
+		if (strcmp(i->name, "channelId") == 0) {
+			args.channel_id = (i->value);
+		} else
+		{}
+	}
+	if (ast_ari_channels_application_parse_body(body, &args)) {
+		ast_ari_response_alloc_failed(response);
+		goto fin;
+	}
+	ast_ari_channels_application(headers, &args, response);
+#if defined(AST_DEVMODE)
+	code = response->response_code;
+
+	switch (code) {
+	case 0: /* Implementation is still a stub, or the code wasn't set */
+		is_valid = response->message == NULL;
+		break;
+	case 500: /* Internal Server Error */
+	case 501: /* Not Implemented */
+	case 404: /* Channel cannot be found. */
+	case 409: /* Channel cannot be dialed. */
+		is_valid = 1;
+		break;
+	default:
+		if (200 <= code && code <= 299) {
+			is_valid = ast_ari_validate_void(
+				response->message);
+		} else {
+			ast_log(LOG_ERROR, "Invalid error response %d for /channels/{channelId}/application\n", code);
+			is_valid = 0;
+		}
+	}
+
+	if (!is_valid) {
+		ast_log(LOG_ERROR, "Response validation failed for /channels/{channelId}/application\n");
+		ast_ari_response_error(response, 500,
+			"Internal Server Error", "Response validation failed");
+	}
+#endif /* AST_DEVMODE */
+
+fin: __attribute__((unused))
+	return;
+}
 
 /*! \brief REST handler for /api-docs/channels.json */
 static struct stasis_rest_handlers channels_create = {
@@ -2921,6 +3010,15 @@
 	.children = {  }
 };
 /*! \brief REST handler for /api-docs/channels.json */
+static struct stasis_rest_handlers channels_channelId_application = {
+	.path_segment = "application",
+	.callbacks = {
+		[AST_HTTP_POST] = ast_ari_channels_application_cb,
+	},
+	.num_children = 0,
+	.children = {  }
+};
+/*! \brief REST handler for /api-docs/channels.json */
 static struct stasis_rest_handlers channels_channelId = {
 	.path_segment = "channelId",
 	.is_wildcard = 1,
@@ -2929,8 +3027,8 @@
 		[AST_HTTP_POST] = ast_ari_channels_originate_with_id_cb,
 		[AST_HTTP_DELETE] = ast_ari_channels_hangup_cb,
 	},
-	.num_children = 15,
-	.children = { &channels_channelId_continue,&channels_channelId_move,&channels_channelId_redirect,&channels_channelId_answer,&channels_channelId_ring,&channels_channelId_dtmf,&channels_channelId_mute,&channels_channelId_hold,&channels_channelId_moh,&channels_channelId_silence,&channels_channelId_play,&channels_channelId_record,&channels_channelId_variable,&channels_channelId_snoop,&channels_channelId_dial, }
+	.num_children = 16,
+	.children = { &channels_channelId_continue,&channels_channelId_move,&channels_channelId_redirect,&channels_channelId_answer,&channels_channelId_ring,&channels_channelId_dtmf,&channels_channelId_mute,&channels_channelId_hold,&channels_channelId_moh,&channels_channelId_silence,&channels_channelId_play,&channels_channelId_record,&channels_channelId_variable,&channels_channelId_snoop,&channels_channelId_dial,&channels_channelId_application, }
 };
 /*! \brief REST handler for /api-docs/channels.json */
 static struct stasis_rest_handlers channels = {
diff --git a/res/res_stasis.c b/res/res_stasis.c
index e635dc5..782dc49 100644
--- a/res/res_stasis.c
+++ b/res/res_stasis.c
@@ -182,6 +182,96 @@
 STASIS_MESSAGE_TYPE_DEFN_LOCAL(start_message_type,
 	.to_json = stasis_start_to_json);
 
+static struct ast_json *channel_application_finished_to_json(
+		struct stasis_message *message,
+		const struct stasis_message_sanitizer *sanitize)
+{
+	struct ast_channel_blob *channel_blob = stasis_message_data(message);
+	struct ast_json *blob = channel_blob->blob;
+
+	return ast_json_pack("{s: s, s: o?, s: O, s: o}",
+		"type", "ChannelApplicationFinished",
+		"timestamp", ast_json_timeval(*stasis_message_timestamp(message), NULL),
+		"channel_application", blob,
+		"channel", ast_channel_snapshot_to_json(channel_blob->snapshot, NULL)
+		);
+}
+
+STASIS_MESSAGE_TYPE_DEFN_LOCAL(application_finished_event_type,
+	.to_json = channel_application_finished_to_json,
+/*	.to_ami = multi_user_event_to_ami, */
+	);
+
+int publish_channel_application_finished_event(
+		struct ast_channel *chan, const char* app, const char* app_args, const char* status)
+{
+	struct stasis_message *msg;
+	struct ast_json *j_data;
+
+	j_data = ast_json_pack("{s: s?, s: s?, s: s?}",
+			"name", app,
+			"args", app_args,
+			"status", status
+			);
+
+	msg = ast_channel_blob_create_from_cache(
+			ast_channel_uniqueid(chan),
+			application_finished_event_type(),
+			j_data);
+	if (msg == NULL) {
+		return 1;
+	}
+
+	stasis_publish(ast_channel_topic(chan), msg);
+	ao2_ref(msg, -1);
+
+	return 0;
+}
+
+static struct ast_json *channel_application_started_to_json(
+		struct stasis_message *message,
+		const struct stasis_message_sanitizer *sanitize)
+{
+	struct ast_channel_blob *channel_blob = stasis_message_data(message);
+	struct ast_json *blob = channel_blob->blob;
+
+	return ast_json_pack("{s: s, s: o?, s: O, s: o}",
+		"type", "ChannelApplicationStarted",
+		"timestamp", ast_json_timeval(*stasis_message_timestamp(message), NULL),
+		"channel_application", blob,
+		"channel", ast_channel_snapshot_to_json(channel_blob->snapshot, NULL)
+		);
+}
+STASIS_MESSAGE_TYPE_DEFN_LOCAL(application_started_event_type,
+	.to_json = channel_application_started_to_json,
+/*	.to_ami = multi_user_event_to_ami, */
+	);
+
+int publish_channel_application_started_event(struct ast_channel *chan, const char* app, const char* app_args)
+{
+	struct stasis_message *msg;
+	struct ast_json *j_data;
+
+	j_data = ast_json_pack("{s: s?, s: s?, s: s?}",
+			"name", app,
+			"args", app_args,
+			"status", "started"
+			);
+
+	msg = ast_channel_blob_create_from_cache(
+			ast_channel_uniqueid(chan),
+			application_started_event_type(),
+			j_data);
+	if (msg == NULL) {
+		return 1;
+	}
+
+	stasis_publish(ast_channel_topic(chan), msg);
+	ao2_ref(msg, -1);
+
+	return 0;
+}
+
 /*! AO2 hash function for \ref app */
 static int app_hash(const void *obj, const int flags)
 {
@@ -2191,6 +2281,9 @@
 
 	STASIS_MESSAGE_TYPE_CLEANUP(end_message_type);
 	STASIS_MESSAGE_TYPE_CLEANUP(start_message_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(application_started_event_type);
+	STASIS_MESSAGE_TYPE_CLEANUP(application_finished_event_type);
+
 
 	return 0;
 }
@@ -2314,6 +2407,13 @@
 	if (STASIS_MESSAGE_TYPE_INIT(end_message_type) != 0) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
+	if (STASIS_MESSAGE_TYPE_INIT(application_started_event_type) != 0) {
+		return AST_MODULE_LOAD_DECLINE;
+	}
+	if (STASIS_MESSAGE_TYPE_INIT(application_finished_event_type) != 0) {
+		return AST_MODULE_LOAD_DECLINE;
+	}
+
 	apps_registry = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
 		APPS_NUM_BUCKETS, app_hash, NULL, app_compare);
 	app_controls = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
diff --git a/res/stasis/control.c b/res/stasis/control.c
index e209a6a..d25b499 100644
--- a/res/stasis/control.c
+++ b/res/stasis/control.c
@@ -40,6 +40,7 @@
 #include "asterisk/pbx.h"
 #include "asterisk/musiconhold.h"
 #include "asterisk/app.h"
+#include "asterisk/stasis.h"
 
 AST_LIST_HEAD(app_control_rules, stasis_app_control_rule);
 
@@ -406,6 +407,50 @@
 	return 0;
 }
 
+struct stasis_app_control_application_data {
+	char *app_name;
+	char *app_args;
+};
+
+static int app_control_application(struct stasis_app_control *control,
+	struct ast_channel *chan, void *data)
+{
+	struct stasis_app_control_application_data *application = data;
+	struct ast_app *app;
+	int ret;
+
+	ast_debug(1, "Fired app_control_application. app: %s, app_arg: %s\n",
+			application->app_name, application->app_args);
+
+	ret = publish_channel_application_started_event(chan, application->app_name, application->app_args);
+	if (ret) {
+		ast_log(LOG_WARNING, "Could not publish the channel application started event. channelId: %s, app_name: %s, app_args %s\n",
+				ast_channel_uniqueid(chan), application->app_name, application->app_args);
+		return 1;
+	}
+
+	app = pbx_findapp(application->app_name);
+	if (!app) {
+		ast_log(LOG_NOTICE, "Could not find application. app: %s, app_args: %s\n",
+				application->app_name, application->app_args);
+
+		publish_channel_application_finished_event(chan, application->app_name, application->app_args, "failed");
+		return 1;
+	}
+
+	ret = pbx_exec_app(chan, app, application->app_args);
+
+	ret = publish_channel_application_finished_event(chan, application->app_name, application->app_args, "done");
+	if (ret) {
+		ast_log(LOG_WARNING, "Could not publish the channel application finished event. channelId: %s, app_name: %s, app_args %s\n",
+				ast_channel_uniqueid(chan), application->app_name, application->app_args);
+		return 1;
+	}
+
+	return ret;
+}
+
+
 struct stasis_app_control_move_data {
 	char *app_name;
 	char *app_args;
@@ -863,6 +908,37 @@
 	return ast_channel_snapshot_get_latest(stasis_app_control_get_channel_id(control));
 }
 
+int stasis_app_control_channel_app(struct stasis_app_control *control, const char *app_name, const char *app_args)
+{
+	struct stasis_app_control_application_data *data;
+	size_t size;
+
+	if (!control || !app_name || !app_args) {
+		ast_log(LOG_WARNING, "Wrong input parameter.\n");
+		return 1;
+	}
+	ast_debug(1, "Fired stasis_app_control_chanapp. app_name: %s, app_args: %s\n", app_name, app_args);
+
+	size = sizeof(*data) + strlen(app_name) + 1;
+	size += strlen(app_args) + 1;
+
+	data = ast_calloc(1, size);
+	if (!data) {
+		return 1;
+	}
+
+	data->app_name = (char *)data + sizeof(*data);
+	strcpy(data->app_name, app_name); /* Safe */
+
+	data->app_args = data->app_name + strlen(app_name) + 1;
+	strcpy(data->app_args, app_args); /* Safe */
+
+	stasis_app_send_command_async(control, app_control_application, data, ast_free_ptr);
+
+	return 0;
+}
+
+
 static int app_send_command_on_condition(struct stasis_app_control *control,
 					 stasis_app_command_cb command_fn, void *data,
 					 command_data_destructor_fn data_destructor,
diff --git a/rest-api/api-docs/channels.json b/rest-api/api-docs/channels.json
index 6161934..28ff14f 100644
--- a/rest-api/api-docs/channels.json
+++ b/rest-api/api-docs/channels.json
@@ -1720,6 +1720,54 @@
 					]
 				}
 			]
+		},
+				{
+			"path": "/channels/{channelId}/application",
+			"description": "Execute a application on a channel",
+			"operations": [
+				{
+					"httpMethod": "POST",
+					"summary": "Execute a application on a channel.",
+					"nickname": "application",
+					"responseClass": "void",
+					"parameters": [
+						{
+							"name": "channelId",
+							"description": "Channel's id",
+							"paramType": "path",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						},
+						{
+							"name": "app",
+							"description": "Application's name",
+							"paramType": "query",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						},
+						{
+							"name": "app_args",
+							"description": "Application's data",
+							"paramType": "query",
+							"required": true,
+							"allowMultiple": false,
+							"dataType": "string"
+						}
+					],
+					"errorResponses": [
+						{
+							"code": 404,
+							"reason": "Channel cannot be found."
+						},
+						{
+							"code": 409,
+							"reason": "Channel cannot be dialed."
+						}
+					]
+				}
+			]
 		}
 	],
 	"models": {
@@ -1763,6 +1811,27 @@
 				}
 			}
 		},
+		"ChannelApplication": {
+			"id": "ChannelApplication",
+			"description": "Channel application info",
+			"properties": {
+				"name": {
+					"description": "Application name",
+					"required": true,
+					"type": "string"
+				},
+				"args": {
+					"description": "Application args",
+					"required": true,
+					"type": "string"
+				},
+				"status": {
+					"description": "Application status",
+					"required": true,
+					"type": "string"
+				}
+			}
+		},
 		"Channel": {
 			"id": "Channel",
 			"description": "A specific communication connection between Asterisk and an Endpoint.",
diff --git a/rest-api/api-docs/events.json b/rest-api/api-docs/events.json
index c9822f6..6b4c5af 100644
--- a/rest-api/api-docs/events.json
+++ b/rest-api/api-docs/events.json
@@ -167,6 +167,8 @@
 				"BridgeBlindTransfer",
 				"BridgeAttendedTransfer",
 				"BridgeVideoSourceChanged",
+				"ChannelApplicationStarted",
+				"ChannelApplicationFinished",
 				"ChannelCreated",
 				"ChannelDestroyed",
 				"ChannelEnteredBridge",
@@ -531,6 +533,34 @@
 				}
 			}
 		},
+		"ChannelApplicationStarted": {
+			"id": "ChannelApplicationStarted",
+			"description": "Notification that a application has been started.",
+			"properties": {
+				"channel": {
+					"required": true,
+					"type": "Channel"
+				},
+				"channel_application": {
+					"description": "Application detail info.",
+					"type": "ChannelApplication"
+				}
+			}
+		},
+		"ChannelApplicationFinished": {
+			"id": "ChannelApplicationFinished",
+			"description": "Notification that a application has been finished.",
+			"properties": {
+				"channel": {
+					"required": true,
+					"type": "Channel"
+				},
+				"channel_application": {
+					"description": "Application detail info.",
+					"type": "ChannelApplication"
+				}
+			}
+		},
 		"ChannelCreated": {
 			"id": "ChannelCreated",
 			"description": "Notification that a channel has been created.",

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/11211
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I037bcb9fda54563fdcd2197a5cbda642fdec70e8
Gerrit-Change-Number: 11211
Gerrit-PatchSet: 1
Gerrit-Owner: sungtae kim <pchero21 at gmail.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190330/0c78cd3c/attachment-0001.html>


More information about the asterisk-code-review mailing list