[Asterisk-code-review] res_clioriginate: Add dialplan exec CLI command. (asterisk[master])

N A asteriskteam at digium.com
Sat May 14 16:27:46 CDT 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/18561 )


Change subject: res_clioriginate: Add dialplan exec CLI command.
......................................................................

res_clioriginate: Add dialplan exec CLI command.

Adds a CLI command similar to "dialplan eval function" except for
applications: "dialplan exec application", useful for quickly
testing certain application behavior directly from the CLI
without writing any dialplan.

ASTERISK-30062 #close

Change-Id: I42e9fa9b60746c21450d40f99a026d48d2486dde
---
M res/res_clioriginate.c
1 file changed, 102 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/61/18561/1

diff --git a/res/res_clioriginate.c b/res/res_clioriginate.c
index 3ea89d8..aeeeb88 100644
--- a/res/res_clioriginate.c
+++ b/res/res_clioriginate.c
@@ -229,9 +229,111 @@
 	return res ? CLI_FAILURE : CLI_SUCCESS;
 }
 
+static const struct ast_channel_tech mock_channel_tech = {
+};
+
+static int cli_chan = 0;
+
+/*! \brief CLI support for executing function */
+static char *handle_exec(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
+{
+	struct ast_channel *c = NULL;
+	RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
+	char *app_name, *app_args;
+	int ret = 0;
+	struct ast_app *app;
+
+	switch (cmd) {
+	case CLI_INIT:
+		e->command = "dialplan exec application";
+		e->usage =
+			"Usage: dialplan exec application <appname> [<args>]\n"
+			"       Execute a single dialplan application call for\n"
+			"       testing. A dummy channel is used to execute\n"
+			"       the application, so it may not make\n"
+			"       sense to use all applications, and only\n"
+			"       global variables should be used.\n";
+		return NULL;
+	case CLI_GENERATE:
+		return NULL;
+	}
+
+	if (a->argc != e->args + 1 && a->argc != e->args + 2) {
+		return CLI_SHOWUSAGE;
+	}
+
+	app_name = (char *) a->argv[3];
+	app_args = a->argc == e->args + 2 ? (char *) a->argv[4] : NULL;
+
+	if (!app_name) {
+		return CLI_FAILURE;
+	}
+
+	caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
+	if (!caps) {
+		ast_log(LOG_WARNING, "Could not allocate an empty format capabilities structure\n");
+		return CLI_FAILURE;
+	}
+
+	if (ast_format_cap_append(caps, ast_format_ulaw, 0)) {
+		ast_log(LOG_WARNING, "Failed to append a ulaw format to capabilities for channel nativeformats\n");
+		return CLI_FAILURE;
+	}
+
+	if (ast_format_cap_append(caps, ast_format_alaw, 0)) {
+		ast_log(LOG_WARNING, "Failed to append an alaw format to capabilities for channel nativeformats\n");
+		return CLI_FAILURE;
+	}
+
+	if (ast_format_cap_append(caps, ast_format_h264, 0)) {
+		ast_log(LOG_WARNING, "Failed to append an h264 format to capabilities for channel nativeformats\n");
+		return CLI_FAILURE;
+	}
+
+	c = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, "CLIExec/%d", ++cli_chan);
+	if (!c) {
+		ast_cli(a->fd, "Unable to allocate bogus channel for application execution.\n");
+		return CLI_FAILURE;
+	}
+	ast_channel_tech_set(c, &mock_channel_tech);
+	ast_channel_nativeformats_set(c, caps);
+	ast_channel_set_writeformat(c, ast_format_slin);
+	ast_channel_set_rawwriteformat(c, ast_format_slin);
+	ast_channel_set_readformat(c, ast_format_slin);
+	ast_channel_set_rawreadformat(c, ast_format_slin);
+	ast_channel_unlock(c);
+
+	app = pbx_findapp(app_name);
+	if (!app) {
+		ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
+		c = ast_channel_unref(c);
+		return CLI_FAILURE;
+	} else {
+		struct ast_str *substituted_args = ast_str_create(16);
+
+		if (substituted_args) {
+			ast_str_substitute_variables(&substituted_args, 0, c, app_args);
+			ast_cli(a->fd, "Executing: %s(%s)\n", app_name, ast_str_buffer(substituted_args));
+			ret = pbx_exec(c, app, ast_str_buffer(substituted_args));
+			ast_free(substituted_args);
+		} else {
+			ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
+			ast_cli(a->fd, "Executing: %s(%s)\n", app_name, app_args);
+			ret = pbx_exec(c, app, app_args);
+		}
+	}
+
+	ast_hangup(c); /* no need to unref separately */
+
+	ast_cli(a->fd, "Return Value: %s (%d)\n", ret ? "Failure" : "Success", ret);
+
+	return CLI_SUCCESS;
+}
+
 static struct ast_cli_entry cli_cliorig[] = {
 	AST_CLI_DEFINE(handle_orig, "Originate a call"),
 	AST_CLI_DEFINE(handle_redirect, "Redirect a call"),
+	AST_CLI_DEFINE(handle_exec, "Execute dialplan application"),
 };
 
 static int unload_module(void)

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I42e9fa9b60746c21450d40f99a026d48d2486dde
Gerrit-Change-Number: 18561
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220514/6e60f287/attachment.html>


More information about the asterisk-code-review mailing list