/* * Asterisk -- An open source telephony toolkit. * * Copyright (C) 2021, Naveen Albert * * Naveen Albert * * See http://www.asterisk.org for more information about * the Asterisk project. Please do not directly contact * any of the maintainers of this project for assistance; * the project provides a web site, mailing lists and IRC * channels for your use. * * This program is free software, distributed under the terms of * the GNU General Public License Version 2. See the LICENSE file * at the top of the source tree. */ /*! \file * * \brief Sleep until a condition is true * * \author Naveen Albert * * \ingroup applications */ /*** MODULEINFO extended ***/ #include "asterisk.h" #include "asterisk/logger.h" #include "asterisk/channel.h" #include "asterisk/pbx.h" #include "asterisk/module.h" #include "asterisk/app.h" /*** DOCUMENTATION 16.20.0 18.6.0 19.0.0 Wait (sleep) until the given condition is true. Specifies the character in the expression used to replace the $ character. This character should not be used anywhere in the expression itself. A modified logical expression with the $ characters replaced by replacementchar. This is necessary to pass the expression itself into the application, rather than its initial evaluation. The maximum amount of time, in seconds, this application should wait for a condition to become true before dialplan execution continues automatically to the next priority. By default, there is no timeout. The frequency, in seconds, of polling the condition, which can be adjusted depending on how time-sensitive execution needs to be. By default, this is 0.05. Waits until expression evaluates to true, checking every interval seconds for up to timeout. Default is evaluate expression every 50 milliseconds with no timeout. same => n,WaitForCondition(#,#["#{condition}"="1"],40,0.5) Sets WAITFORCONDITIONSTATUS to one of the following values: Condition evaluated to true before timeout expired. Invalid argument. Timeout elapsed without condition evaluating to true. Channel hung up before condition became true. ***/ static char *app = "WaitForCondition"; static int waitforcond_exec(struct ast_channel *chan, const char *data) { int ms, i; double timeout = 0, poll = 0; int timeout_ms = 0; int poll_ms = 50; /* default is evaluate the condition every 50ms */ struct timeval start = ast_tvnow(); char dollarsignrep; int brackets = 0; char *pos, *open_bracket, *expression, *optargs = NULL; char condition[512]; AST_DECLARE_APP_ARGS(args, AST_APP_ARG(timeout); AST_APP_ARG(interval); ); pos = ast_strdupa(data); if (ast_strlen_zero(pos)) { ast_log(LOG_ERROR, "WaitForCondition requires a condition\n"); pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); return 0; } /* is there at least a [ followed by a ] somewhere ? */ if (!(open_bracket = strchr(pos, '[')) || !strchr(open_bracket, ']')) { ast_log(LOG_ERROR, "No expression detected. Did you forget to replace the $ signs?\n"); pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); return 0; } dollarsignrep = pos[0]; if (dollarsignrep == '$' || dollarsignrep == '[' || dollarsignrep == ']' || dollarsignrep == '{' || dollarsignrep == '}') { ast_log(LOG_ERROR, "Dollar sign replacement cannot be %c.\n", dollarsignrep); pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); return 0; } ++pos; if (pos[0] != ',') { ast_log(LOG_ERROR, "Invalid separator: %c\n", pos[0]); pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); return 0; } ++pos; if (pos[0] != dollarsignrep) { ast_log(LOG_ERROR, "Expression start does not match provided replacement: %c\n", pos[0]); pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); return 0; } expression = pos; /* we're at the start of the expression */ /* commas may appear within the expression, so go until we've encountered as many closing brackets as opening */ while (++pos) { if (pos[0] == '\0') { ast_log(LOG_ERROR, "Could not parse end of expression.\n"); pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); return 0; } if (pos[0] == '[') { brackets++; } else if (pos[0] == ']') { brackets--; } if (brackets == 0) { /* reached end of expression */ break; } } ++pos; if (pos[0] != '\0') { ++pos; /* eat comma separator */ if (pos[0] != '\0') { optargs = ast_strdupa(pos); AST_STANDARD_APP_ARGS(args, optargs); if (!ast_strlen_zero(args.timeout)) { if (sscanf(args.timeout, "%30lg", &timeout) != 1) { ast_log(LOG_WARNING, "Invalid timeout provided: %s. No timeout set.\n", args.timeout); return -1; } timeout_ms = timeout * 1000.0; } if (!ast_strlen_zero(args.interval)) { if (sscanf(args.interval, "%30lg", &poll) != 1) { ast_log(LOG_WARNING, "Invalid polling interval provided: %s. Default unchanged.\n", args.interval); return -1; } if (poll < 0.001) { ast_log(LOG_WARNING, "Polling interval cannot be less than 1ms. Default unchanged.\n"); return -1; } poll_ms = poll * 1000.0; } } } for (i = 0; expression[i] != '\0'; i++) { if (expression[i] == dollarsignrep) { expression[i] = '$'; /* replace $s back into expression for variable parsing */ } } if (timeout_ms > 0) { ast_debug(1, "Waiting for condition for %f seconds: %s (checking every %d ms)", timeout, expression, poll_ms); } else { ast_debug(1, "Waiting for condition, forever: %s (checking every %d ms)", expression, poll_ms); } while (1) { /* Substitute variables now */ pbx_substitute_variables_helper(chan, expression, condition, sizeof(condition) - 1); if (pbx_checkcondition(condition)) { pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "TRUE"); return 0; } /* If a timeout was specified, check that it hasn't expired */ if ((timeout_ms > 0) && !(ms = ast_remaining_ms(start, timeout_ms))) { pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "TIMEOUT"); return 0; } if (ast_safe_sleep(chan, poll_ms)) { /* don't waste CPU, we don't need a super tight loop */ pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "HANGUP"); return -1; /* channel hung up */ } } } static int unload_module(void) { return ast_unregister_application(app); } static int load_module(void) { return ast_register_application_xml(app, waitforcond_exec); } AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Wait until condition is true");