2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2006, Andy Powell
6 * Updated by Mark Spencer <markster@digium.com>
7 * Updated by Nir Simionovich <nirs@greenfieldtech.net>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Math related dialplan function
25 * \author Mark Spencer <markster@digium.com>
26 * \author Nir Simionovich <nirs@greenfieldtech.net>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/app.h"
42 #include "asterisk/config.h"
43 #include "asterisk/test.h"
46 <function name="MATH" language="en_US">
48 Performs Mathematical Functions.
51 <parameter name="expression" required="true">
53 <replaceable>number1</replaceable><replaceable>op</replaceable><replaceable>number2</replaceable>
54 where the possible values for <replaceable>op</replaceable>
56 <para>+,-,/,*,%,<<,>>,^,AND,OR,XOR,<,%gt;,>=,<=,== (and behave as their C equivalents)</para>
58 <parameter name="type">
59 <para>Wanted type of result:</para>
60 <para>f, float - float(default)</para>
61 <para>i, int - integer</para>
62 <para>h, hex - hex</para>
63 <para>c, char - char</para>
67 <para>Performs mathematical functions based on two parameters and an operator. The returned
68 value type is <replaceable>type</replaceable></para>
69 <para>Example: Set(i=${MATH(123%16,int)}) - sets var i=11</para>
72 <function name="INC" language="en_US">
74 Increments the value of a variable, while returning the updated value to the dialplan
77 <parameter name="variable" required="true">
79 The variable name to be manipulated, without the braces.
84 <para>Increments the value of a variable, while returning the updated value to the dialplan</para>
85 <para>Example: INC(MyVAR) - Increments MyVar</para>
86 <para>Note: INC(${MyVAR}) - Is wrong, as INC expects the variable name, not its value</para>
89 <function name="DEC" language="en_US">
91 Decrements the value of a variable, while returning the updated value to the dialplan
94 <parameter name="variable" required="true">
96 The variable name to be manipulated, without the braces.
101 <para>Decrements the value of a variable, while returning the updated value to the dialplan</para>
102 <para>Example: DEC(MyVAR) - Increments MyVar</para>
103 <para>Note: DEC(${MyVAR}) - Is wrong, as INC expects the variable name, not its value</para>
108 enum TypeOfFunctions {
134 static int math(struct ast_channel *chan, const char *cmd, char *parse,
135 char *buf, size_t len)
142 int type_of_result = FLOAT_RESULT;
143 char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
145 AST_DECLARE_APP_ARGS(args,
150 if (ast_strlen_zero(parse)) {
151 ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
155 AST_STANDARD_APP_ARGS(args, parse);
158 ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
162 mvalue1 = args.argv0;
164 if (mvalue1[0] == '-') {
169 if ((op = strchr(mvalue1, '*'))) {
170 iaction = MULTIPLYFUNCTION;
172 } else if ((op = strchr(mvalue1, '/'))) {
173 iaction = DIVIDEFUNCTION;
175 } else if ((op = strchr(mvalue1, '%'))) {
176 iaction = MODULUSFUNCTION;
178 } else if ((op = strchr(mvalue1, '^'))) {
179 iaction = POWFUNCTION;
181 } else if ((op = strstr(mvalue1, "AND"))) {
182 iaction = BITWISEANDFUNCTION;
185 } else if ((op = strstr(mvalue1, "XOR"))) {
186 iaction = BITWISEXORFUNCTION;
189 } else if ((op = strstr(mvalue1, "OR"))) {
190 iaction = BITWISEORFUNCTION;
193 } else if ((op = strchr(mvalue1, '>'))) {
194 iaction = GTFUNCTION;
196 if (*(op + 1) == '=') {
197 iaction = GTEFUNCTION;
199 } else if (*(op + 1) == '>') {
200 iaction = SHRIGHTFUNCTION;
203 } else if ((op = strchr(mvalue1, '<'))) {
204 iaction = LTFUNCTION;
206 if (*(op + 1) == '=') {
207 iaction = LTEFUNCTION;
209 } else if (*(op + 1) == '<') {
210 iaction = SHLEFTFUNCTION;
213 } else if ((op = strchr(mvalue1, '='))) {
215 if (*(op + 1) == '=') {
216 iaction = EQFUNCTION;
220 } else if ((op = strchr(mvalue1, '+'))) {
221 iaction = ADDFUNCTION;
223 } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative second number */
224 iaction = SUBTRACTFUNCTION;
231 /* detect wanted type of result */
232 mtype_of_result = args.argv1;
233 if (mtype_of_result) {
234 if (!strcasecmp(mtype_of_result, "float")
235 || !strcasecmp(mtype_of_result, "f"))
236 type_of_result = FLOAT_RESULT;
237 else if (!strcasecmp(mtype_of_result, "int")
238 || !strcasecmp(mtype_of_result, "i"))
239 type_of_result = INT_RESULT;
240 else if (!strcasecmp(mtype_of_result, "hex")
241 || !strcasecmp(mtype_of_result, "h"))
242 type_of_result = HEX_RESULT;
243 else if (!strcasecmp(mtype_of_result, "char")
244 || !strcasecmp(mtype_of_result, "c"))
245 type_of_result = CHAR_RESULT;
247 ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
253 if (!mvalue1 || !mvalue2) {
255 "Supply all the parameters - just this once, please\n");
259 if (sscanf(mvalue1, "%30lf", &fnum1) != 1) {
260 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
264 if (sscanf(mvalue2, "%30lf", &fnum2) != 1) {
265 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
274 ftmp = fnum1 + fnum2;
278 ftmp = 0; /* can't do a divide by 0 */
280 ftmp = (fnum1 / fnum2);
282 case MULTIPLYFUNCTION:
283 ftmp = (fnum1 * fnum2);
285 case SUBTRACTFUNCTION:
286 ftmp = (fnum1 - fnum2);
288 case MODULUSFUNCTION:
296 ftmp = (inum1 % inum2);
302 ftmp = pow(fnum1, fnum2);
309 ftmp = (inum1 << inum2);
312 case SHRIGHTFUNCTION:
317 ftmp = (inum1 >> inum2);
320 case BITWISEANDFUNCTION:
324 ftmp = (inum1 & inum2);
327 case BITWISEXORFUNCTION:
331 ftmp = (inum1 ^ inum2);
334 case BITWISEORFUNCTION:
338 ftmp = (inum1 | inum2);
342 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
345 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
348 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
351 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
354 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
358 "Something happened that neither of us should be proud of %d\n",
363 if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
364 if (type_of_result == FLOAT_RESULT)
365 snprintf(buf, len, "%f", ftmp);
366 else if (type_of_result == INT_RESULT)
367 snprintf(buf, len, "%i", (int) ftmp);
368 else if (type_of_result == HEX_RESULT)
369 snprintf(buf, len, "%x", (unsigned int) ftmp);
370 else if (type_of_result == CHAR_RESULT)
371 snprintf(buf, len, "%c", (unsigned char) ftmp);
377 static int crement_function_read(struct ast_channel *chan, const char *cmd,
378 char *data, char *buf, size_t len)
384 char endchar = 0, returnvar[12]; /* If you need a variable longer than 11 digits - something is way wrong */
386 if (ast_strlen_zero(data)) {
387 ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
391 ast_channel_lock(chan);
393 if (!(var = pbx_builtin_getvar_helper(chan, data))) {
394 ast_log(LOG_NOTICE, "Failed to obtain variable %s, bailing out\n", data);
395 ast_channel_unlock(chan);
399 if (ast_strlen_zero(var)) {
400 ast_log(LOG_NOTICE, "Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
401 ast_channel_unlock(chan);
405 if (sscanf(var, "%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
406 ast_log(LOG_NOTICE, "The content of ${%s} is not a numeric value - bailing out!\n", data);
407 ast_channel_unlock(chan);
411 /* now we'll actually do something useful */
412 if (!strcasecmp(cmd, "INC")) { /* Increment variable */
415 } else if (!strcasecmp(cmd, "DEC")) { /* Decrement variable */
420 ast_log(LOG_NOTICE, "The value is now: %d\n", int_value);
422 if (snprintf(returnvar, sizeof(returnvar), "%d", int_value) > 0) {
423 pbx_builtin_setvar_helper(chan, data, returnvar);
425 ast_copy_string(buf, returnvar, len);
429 pbx_builtin_setvar_helper(chan, data, "0");
431 ast_copy_string(buf, "0", len);
433 ast_log(LOG_NOTICE, "Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
437 ast_channel_unlock(chan);
443 static struct ast_custom_function math_function = {
448 static struct ast_custom_function increment_function = {
450 .read = crement_function_read,
453 static struct ast_custom_function decrement_function = {
455 .read = crement_function_read,
458 #ifdef TEST_FRAMEWORK
459 AST_TEST_DEFINE(test_MATH_function)
461 enum ast_test_result_state res = AST_TEST_PASS;
462 struct ast_str *expr, *result;
466 info->name = "test_MATH_function";
467 info->category = "main/pbx/";
468 info->summary = "Test MATH function substitution";
470 "Executes a series of variable substitutions using the MATH function and ensures that the expected results are received.";
471 return AST_TEST_NOT_RUN;
476 ast_test_status_update(&args->status_update, "Testing MATH() substitution ...\n");
477 ast_str_reset(args->ast_test_error_str);
479 if (!(expr = ast_str_create(16)) || !(result = ast_str_create(16))) {
486 return AST_TEST_FAIL;
489 ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}");
490 ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
491 if (strcmp(ast_str_buffer(result), "42") != 0) {
492 ast_str_append(&args->ast_test_error_str, 0, "Expected result '42' not returned! ('%s')\n", ast_str_buffer(result));
496 ast_str_set(&expr, 0, "${MATH(170AND63,i)}");
497 ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
498 if (strcmp(ast_str_buffer(result), "42") != 0) {
499 ast_str_append(&args->ast_test_error_str, 0, "Expected result '42' not returned! ('%s')\n", ast_str_buffer(result));
507 static int unload_module(void)
511 res |= ast_custom_function_unregister(&math_function);
512 res |= ast_custom_function_unregister(&increment_function);
513 res |= ast_custom_function_unregister(&decrement_function);
514 AST_TEST_UNREGISTER(test_MATH_function);
519 static int load_module(void)
523 res |= ast_custom_function_register(&math_function);
524 res |= ast_custom_function_register(&increment_function);
525 res |= ast_custom_function_register(&decrement_function);
526 AST_TEST_REGISTER(test_MATH_function);
531 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");