Skip to content

Commit 4a11ae7

Browse files
InterLinked1kharwell
authored andcommitted
pbx: Add helper function to execute applications.
Finding an application and executing it if found is a common task throughout Asterisk. This adds a helper function around pbx_exec to do this, to eliminate redundant code and make it easier for modules to substitute variables and execute applications by name. ASTERISK-30061 #close Change-Id: Ifee4d2825df7545fb515d763d393065675140c84
1 parent d052418 commit 4a11ae7

File tree

8 files changed

+51
-49
lines changed

8 files changed

+51
-49
lines changed

apps/app_disa.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,6 @@ static int disa_exec(struct ast_channel *chan, const char *data)
361361

362362
if (k == 3) {
363363
int recheck = 0;
364-
struct ast_app *app_reset_cdr;
365364

366365
if (!ast_exists_extension(chan, args.context, exten, 1,
367366
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, NULL))) {
@@ -386,10 +385,7 @@ static int disa_exec(struct ast_channel *chan, const char *data)
386385
ast_channel_unlock(chan);
387386
}
388387

389-
app_reset_cdr = pbx_findapp("ResetCDR");
390-
if (app_reset_cdr) {
391-
pbx_exec(chan, app_reset_cdr, special_noanswer ? "" : "e");
392-
} else {
388+
if (ast_pbx_exec_application(chan, "ResetCDR", special_noanswer ? "" : "e")) {
393389
ast_log(AST_LOG_NOTICE, "ResetCDR application not found; CDR will not be reset\n");
394390
}
395391
ast_explicit_goto(chan, args.context, exten, 1);

include/asterisk/pbx.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,23 @@ struct ast_app *pbx_findapp(const char *app);
268268
*/
269269
int pbx_exec(struct ast_channel *c, struct ast_app *app, const char *data);
270270

271+
/*!
272+
* \brief Execute an application
273+
*
274+
* \param c channel to execute on
275+
* \param app name of app to execute
276+
* \param data the data passed into the app
277+
*
278+
* This application executes an application by name on a given channel.
279+
* It is a wrapper around pbx_exec that will perform variable substitution
280+
* and then execute the application if it exists.
281+
* If the application is not found, a warning is logged.
282+
*
283+
* \retval 0 success
284+
* \retval -1 failure (including application not found)
285+
*/
286+
int ast_pbx_exec_application(struct ast_channel *chan, const char *app_name, const char *app_args);
287+
271288
/*!
272289
* \brief Register a new context or find an existing one
273290
*

main/bridge_channel.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,23 +1178,7 @@ static int run_app_helper(struct ast_channel *chan, const char *app_name, const
11781178
} else if (!strcasecmp("Macro", app_name)) {
11791179
ast_app_exec_macro(NULL, chan, app_args);
11801180
} else {
1181-
struct ast_app *app;
1182-
1183-
app = pbx_findapp(app_name);
1184-
if (!app) {
1185-
ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
1186-
} else {
1187-
struct ast_str *substituted_args = ast_str_create(16);
1188-
1189-
if (substituted_args) {
1190-
ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
1191-
res = pbx_exec(chan, app, ast_str_buffer(substituted_args));
1192-
ast_free(substituted_args);
1193-
} else {
1194-
ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
1195-
res = pbx_exec(chan, app, app_args);
1196-
}
1197-
}
1181+
res = ast_pbx_exec_application(chan, app_name, app_args);
11981182
}
11991183
return res;
12001184
}

main/dial.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,12 @@ static int predial_disable(void *data)
166166
static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
167167
{
168168
struct ast_channel *chan = dial_channel->owner;
169-
struct ast_app *ast_app = pbx_findapp(app);
170169

171-
/* If the application was not found, return immediately */
172-
if (!ast_app)
170+
/* Execute the application, if available */
171+
if (ast_pbx_exec_application(chan, app, args)) {
172+
/* If the application was not found, return immediately */
173173
return;
174-
175-
/* All is well... execute the application */
176-
pbx_exec(chan, ast_app, args);
174+
}
177175

178176
/* If another thread is not taking over hang up the channel */
179177
ast_mutex_lock(&dial->lock);

main/features.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,12 +504,7 @@ static void bridge_check_monitor(struct ast_channel *chan, struct ast_channel *p
504504
ast_channel_unlock(peer);
505505
}
506506
if (monitor_chan) {
507-
struct ast_app *monitor_app;
508-
509-
monitor_app = pbx_findapp("Monitor");
510-
if (monitor_app) {
511-
pbx_exec(monitor_chan, monitor_app, monitor_args);
512-
}
507+
ast_pbx_exec_application(monitor_chan, "Monitor", monitor_args);
513508
}
514509
}
515510

main/pbx_app.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,31 @@ int pbx_exec(struct ast_channel *c, /*!< Channel */
498498
return res;
499499
}
500500

501+
int ast_pbx_exec_application(struct ast_channel *chan, const char *app_name, const char *app_args)
502+
{
503+
int res = -1;
504+
struct ast_app *app;
505+
506+
app = pbx_findapp(app_name);
507+
if (!app) {
508+
ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
509+
} else {
510+
struct ast_str *substituted_args = NULL;
511+
512+
if (!ast_strlen_zero(app_args) && (substituted_args = ast_str_create(16))) {
513+
ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
514+
res = pbx_exec(chan, app, ast_str_buffer(substituted_args));
515+
ast_free(substituted_args);
516+
} else {
517+
if (!ast_strlen_zero(app_args)) {
518+
ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
519+
}
520+
res = pbx_exec(chan, app, app_args);
521+
}
522+
}
523+
return res;
524+
}
525+
501526
static struct ast_cli_entry app_cli[] = {
502527
AST_CLI_DEFINE(handle_show_applications, "Shows registered dialplan applications"),
503528
AST_CLI_DEFINE(handle_show_application, "Describe a specific dialplan application"),

main/pbx_builtins.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,6 @@ static int pbx_builtin_execiftime(struct ast_channel *chan, const char *data)
10001000
{
10011001
char *s, *appname;
10021002
struct ast_timing timing;
1003-
struct ast_app *app;
10041003
static const char * const usage = "ExecIfTime requires an argument:\n <time range>,<days of week>,<days of month>,<months>[,<timezone>]?<appname>[(<appargs>)]";
10051004

10061005
if (ast_strlen_zero(data)) {
@@ -1038,13 +1037,7 @@ static int pbx_builtin_execiftime(struct ast_channel *chan, const char *data)
10381037
ast_log(LOG_WARNING, "Failed to find closing parenthesis\n");
10391038
}
10401039

1041-
1042-
if ((app = pbx_findapp(appname))) {
1043-
return pbx_exec(chan, app, S_OR(s, ""));
1044-
} else {
1045-
ast_log(LOG_WARNING, "Cannot locate application %s\n", appname);
1046-
return -1;
1047-
}
1040+
return ast_pbx_exec_application(chan, appname, S_OR(s, ""));
10481041
}
10491042

10501043
/*!

res/res_stasis_snoop.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,8 @@ static struct ast_channel_tech snoop_tech = {
263263
static void *snoop_stasis_thread(void *obj)
264264
{
265265
RAII_VAR(struct stasis_app_snoop *, snoop, obj, ao2_cleanup);
266-
struct ast_app *stasis = pbx_findapp("Stasis");
267266

268-
if (!stasis) {
269-
ast_hangup(snoop->chan);
270-
return NULL;
271-
}
272-
273-
pbx_exec(snoop->chan, stasis, ast_str_buffer(snoop->app));
267+
ast_pbx_exec_application(snoop->chan, "Stasis", ast_str_buffer(snoop->app));
274268

275269
ast_hangup(snoop->chan);
276270

0 commit comments

Comments
 (0)