[Asterisk-code-review] pbx: Add pbx exec helper function. (asterisk[master])

N A asteriskteam at digium.com
Sun May 15 07:43:34 CDT 2022


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


Change subject: pbx: Add pbx exec helper function.
......................................................................

pbx: Add pbx exec helper function.

Finding an application and executing it if found is
a common task throughout Asterisk. This adds a wrapper
function around pbx_exec to do this, to eliminate
redundant code and make it easier for modules to
execute applications by name.

ASTERISK-30061 #close

Change-Id: Ifee4d2825df7545fb515d763d393065675140c84
---
M apps/app_disa.c
M include/asterisk/pbx.h
M main/bridge_channel.c
M main/dial.c
M main/features.c
M main/pbx_app.c
M main/pbx_builtins.c
M res/res_stasis_snoop.c
8 files changed, 49 insertions(+), 49 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/62/18562/1

diff --git a/apps/app_disa.c b/apps/app_disa.c
index cceb554..7e69fec 100644
--- a/apps/app_disa.c
+++ b/apps/app_disa.c
@@ -361,7 +361,6 @@
 
 	if (k == 3) {
 		int recheck = 0;
-		struct ast_app *app_reset_cdr;
 
 		if (!ast_exists_extension(chan, args.context, exten, 1,
 			S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
@@ -386,10 +385,7 @@
 				ast_channel_unlock(chan);
 			}
 
-			app_reset_cdr = pbx_findapp("ResetCDR");
-			if (app_reset_cdr) {
-				pbx_exec(chan, app_reset_cdr, special_noanswer ? "" : "e");
-			} else {
+			if (pbx_exec_helper(chan, "ResetCDR", special_noanswer ? "" : "e")) {
 				ast_log(AST_LOG_NOTICE, "ResetCDR application not found; CDR will not be reset\n");
 			}
 			ast_explicit_goto(chan, args.context, exten, 1);
diff --git a/include/asterisk/pbx.h b/include/asterisk/pbx.h
index d531b44..d0082e2 100644
--- a/include/asterisk/pbx.h
+++ b/include/asterisk/pbx.h
@@ -269,6 +269,21 @@
 int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data);
 
 /*!
+ * \brief Execute an application
+ *
+ * \param c channel to execute on
+ * \param app name of app to execute
+ * \param data the data passed into the app
+ *
+ * This application executes an application by name on a given channel.
+ * It is a wrapper around pbx_exec that will execute the application if it exists.
+ *
+ * \retval 0 success
+ * \retval -1 failure
+ */
+int pbx_exec_helper(struct ast_channel *chan, const char *app_name, const char *app_args);
+
+/*!
  * \brief Register a new context or find an existing one
  *
  * \param extcontexts pointer to the ast_context structure pointer
diff --git a/main/bridge_channel.c b/main/bridge_channel.c
index 1b0f1dd..29edb6c 100644
--- a/main/bridge_channel.c
+++ b/main/bridge_channel.c
@@ -1178,23 +1178,7 @@
 	} else if (!strcasecmp("Macro", app_name)) {
 		ast_app_exec_macro(NULL, chan, app_args);
 	} else {
-		struct ast_app *app;
-
-		app = pbx_findapp(app_name);
-		if (!app) {
-			ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
-		} else {
-			struct ast_str *substituted_args = ast_str_create(16);
-
-			if (substituted_args) {
-				ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
-				res = pbx_exec(chan, 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);
-				res = pbx_exec(chan, app, app_args);
-			}
-		}
+		res = pbx_exec_helper(chan, app_name, app_args);
 	}
 	return res;
 }
diff --git a/main/dial.c b/main/dial.c
index c40b7fb..256816e 100644
--- a/main/dial.c
+++ b/main/dial.c
@@ -166,14 +166,12 @@
 static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
 {
 	struct ast_channel *chan = dial_channel->owner;
-	struct ast_app *ast_app = pbx_findapp(app);
 
-	/* If the application was not found, return immediately */
-	if (!ast_app)
+	/* Execute the application, if available */
+	if (pbx_exec_helper(chan, app, args)) {
+		/* If the application was not found, return immediately */
 		return;
-
-	/* All is well... execute the application */
-	pbx_exec(chan, ast_app, args);
+	}
 
 	/* If another thread is not taking over hang up the channel */
 	ast_mutex_lock(&dial->lock);
diff --git a/main/features.c b/main/features.c
index b67bf38..3d82496 100644
--- a/main/features.c
+++ b/main/features.c
@@ -504,12 +504,7 @@
 		ast_channel_unlock(peer);
 	}
 	if (monitor_chan) {
-		struct ast_app *monitor_app;
-
-		monitor_app = pbx_findapp("Monitor");
-		if (monitor_app) {
-			pbx_exec(monitor_chan, monitor_app, monitor_args);
-		}
+		pbx_exec_helper(monitor_chan, "Monitor", monitor_args);
 	}
 }
 
diff --git a/main/pbx_app.c b/main/pbx_app.c
index 0cbb04a..bf5cb34 100644
--- a/main/pbx_app.c
+++ b/main/pbx_app.c
@@ -498,6 +498,31 @@
 	return res;
 }
 
+int pbx_exec_helper(struct ast_channel *chan, const char *app_name, const char *app_args)
+{
+	int res = -1;
+	struct ast_app *app;
+
+	app = pbx_findapp(app_name);
+	if (!app) {
+		ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
+	} else {
+		struct ast_str *substituted_args = NULL;
+
+		if (!ast_strlen_zero(app_args) && (substituted_args = ast_str_create(16))) {
+			ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
+			res = pbx_exec(chan, app, ast_str_buffer(substituted_args));
+			ast_free(substituted_args);
+		} else {
+			if (!ast_strlen_zero(app_args)) {
+				ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
+			}
+			res = pbx_exec(chan, app, app_args);
+		}
+	}
+	return res;
+}
+
 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/main/pbx_builtins.c b/main/pbx_builtins.c
index 078a307..91a2045 100644
--- a/main/pbx_builtins.c
+++ b/main/pbx_builtins.c
@@ -1000,7 +1000,6 @@
 {
 	char *s, *appname;
 	struct ast_timing timing;
-	struct ast_app *app;
 	static const char * const usage = "ExecIfTime requires an argument:\n  <time range>,<days of week>,<days of month>,<months>[,<timezone>]?<appname>[(<appargs>)]";
 
 	if (ast_strlen_zero(data)) {
@@ -1038,13 +1037,7 @@
 			ast_log(LOG_WARNING, "Failed to find closing parenthesis\n");
 	}
 
-
-	if ((app = pbx_findapp(appname))) {
-		return pbx_exec(chan, app, S_OR(s, ""));
-	} else {
-		ast_log(LOG_WARNING, "Cannot locate application %s\n", appname);
-		return -1;
-	}
+	return pbx_exec_helper(chan, appname, S_OR(s, ""));
 }
 
 /*!
diff --git a/res/res_stasis_snoop.c b/res/res_stasis_snoop.c
index 190ee06..89ecd42 100644
--- a/res/res_stasis_snoop.c
+++ b/res/res_stasis_snoop.c
@@ -263,14 +263,8 @@
 static void *snoop_stasis_thread(void *obj)
 {
 	RAII_VAR(struct stasis_app_snoop *, snoop, obj, ao2_cleanup);
-	struct ast_app *stasis = pbx_findapp("Stasis");
 
-	if (!stasis) {
-		ast_hangup(snoop->chan);
-		return NULL;
-	}
-
-	pbx_exec(snoop->chan, stasis, ast_str_buffer(snoop->app));
+	pbx_exec_helper(snoop->chan, "Stasis", ast_str_buffer(snoop->app));
 
 	ast_hangup(snoop->chan);
 

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Ifee4d2825df7545fb515d763d393065675140c84
Gerrit-Change-Number: 18562
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/20220515/583eb9be/attachment.html>


More information about the asterisk-code-review mailing list