| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/logger.h" |
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/module.h" |
| 38 |
#include "asterisk/app.h" |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static char *app = "WaitForCondition"; |
| 98 |
|
| 99 |
static int waitforcond_exec(struct ast_channel *chan, const char *data) |
| 100 |
{ |
| 101 |
int ms, i; |
| 102 |
double timeout = 0, poll = 0; |
| 103 |
int timeout_ms = 0; |
| 104 |
int poll_ms = 50; |
| 105 |
struct timeval start = ast_tvnow(); |
| 106 |
char dollarsignrep; |
| 107 |
int brackets = 0; |
| 108 |
char *pos, *open_bracket, *expression, *optargs = NULL; |
| 109 |
char condition[512]; |
| 110 |
|
| 111 |
AST_DECLARE_APP_ARGS(args, |
| 112 |
AST_APP_ARG(timeout); |
| 113 |
AST_APP_ARG(interval); |
| 114 |
); |
| 115 |
|
| 116 |
pos = ast_strdupa(data); |
| 117 |
|
| 118 |
if (ast_strlen_zero(pos)) { |
| 119 |
ast_log(LOG_ERROR, "WaitForCondition requires a condition\n"); |
| 120 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); |
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
if (!(open_bracket = strchr(pos, '[')) || !strchr(open_bracket, ']')) { |
| 126 |
ast_log(LOG_ERROR, "No expression detected. Did you forget to replace the $ signs?\n"); |
| 127 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
dollarsignrep = pos[0]; |
| 132 |
if (dollarsignrep == '$' || dollarsignrep == '[' || dollarsignrep == ']' |
| 133 |
|| dollarsignrep == '{' || dollarsignrep == '}') { |
| 134 |
ast_log(LOG_ERROR, "Dollar sign replacement cannot be %c.\n", dollarsignrep); |
| 135 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); |
| 136 |
return 0; |
| 137 |
} |
| 138 |
++pos; |
| 139 |
if (pos[0] != ',') { |
| 140 |
ast_log(LOG_ERROR, "Invalid separator: %c\n", pos[0]); |
| 141 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); |
| 142 |
return 0; |
| 143 |
} |
| 144 |
++pos; |
| 145 |
if (pos[0] != dollarsignrep) { |
| 146 |
ast_log(LOG_ERROR, "Expression start does not match provided replacement: %c\n", pos[0]); |
| 147 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
expression = pos; |
| 152 |
|
| 153 |
|
| 154 |
while (++pos) { |
| 155 |
if (pos[0] == '\0') { |
| 156 |
ast_log(LOG_ERROR, "Could not parse end of expression.\n"); |
| 157 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "FAILURE"); |
| 158 |
return 0; |
| 159 |
} |
| 160 |
if (pos[0] == '[') { |
| 161 |
brackets++; |
| 162 |
} else if (pos[0] == ']') { |
| 163 |
brackets--; |
| 164 |
} |
| 165 |
if (brackets == 0) { |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
++pos; |
| 170 |
if (pos[0] != '\0') { |
| 171 |
++pos; |
| 172 |
if (pos[0] != '\0') { |
| 173 |
optargs = ast_strdupa(pos); |
| 174 |
AST_STANDARD_APP_ARGS(args, optargs); |
| 175 |
if (!ast_strlen_zero(args.timeout)) { |
| 176 |
if (sscanf(args.timeout, "%30lg", &timeout) != 1) { |
| 177 |
ast_log(LOG_WARNING, "Invalid timeout provided: %s. No timeout set.\n", args.timeout); |
| 178 |
return -1; |
| 179 |
} |
| 180 |
timeout_ms = timeout * 1000.0; |
| 181 |
} |
| 182 |
|
| 183 |
if (!ast_strlen_zero(args.interval)) { |
| 184 |
if (sscanf(args.interval, "%30lg", &poll) != 1) { |
| 185 |
ast_log(LOG_WARNING, "Invalid polling interval provided: %s. Default unchanged.\n", args.interval); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
if (poll < 0.001) { |
| 189 |
ast_log(LOG_WARNING, "Polling interval cannot be less than 1ms. Default unchanged.\n"); |
| 190 |
return -1; |
| 191 |
} |
| 192 |
poll_ms = poll * 1000.0; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
for (i = 0; expression[i] != '\0'; i++) { |
| 198 |
if (expression[i] == dollarsignrep) { |
| 199 |
expression[i] = '$'; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
if (timeout_ms > 0) { |
| 204 |
ast_debug(1, "Waiting for condition for %f seconds: %s (checking every %d ms)", timeout, expression, poll_ms); |
| 205 |
} else { |
| 206 |
ast_debug(1, "Waiting for condition, forever: %s (checking every %d ms)", expression, poll_ms); |
| 207 |
} |
| 208 |
|
| 209 |
while (1) { |
| 210 |
|
| 211 |
pbx_substitute_variables_helper(chan, expression, condition, sizeof(condition) - 1); |
| 212 |
if (pbx_checkcondition(condition)) { |
| 213 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "TRUE"); |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
if ((timeout_ms > 0) && !(ms = ast_remaining_ms(start, timeout_ms))) { |
| 218 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "TIMEOUT"); |
| 219 |
return 0; |
| 220 |
} |
| 221 |
if (ast_safe_sleep(chan, poll_ms)) { |
| 222 |
pbx_builtin_setvar_helper(chan, "WAITFORCONDITIONSTATUS", "HANGUP"); |
| 223 |
return -1; |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
static int unload_module(void) |
| 229 |
{ |
| 230 |
return ast_unregister_application(app); |
| 231 |
} |
| 232 |
|
| 233 |
static int load_module(void) |
| 234 |
{ |
| 235 |
return ast_register_application_xml(app, waitforcond_exec); |
| 236 |
} |
| 237 |
|
| 238 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Wait until condition is true"); |