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>
32 <support_level>core</support_level>
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include "asterisk/module.h"
42 #include "asterisk/channel.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/utils.h"
45 #include "asterisk/app.h"
46 #include "asterisk/config.h"
47 #include "asterisk/test.h"
50 <function name="MATH" language="en_US">
52 Performs Mathematical Functions.
55 <parameter name="expression" required="true">
57 <replaceable>number1</replaceable><replaceable>op</replaceable><replaceable>number2</replaceable>
58 where the possible values for <replaceable>op</replaceable>
60 <para>+,-,/,*,%,<<,>>,^,AND,OR,XOR,<,>,<=,>=,== (and behave as their C equivalents)</para>
62 <parameter name="type">
63 <para>Wanted type of result:</para>
64 <para>f, float - float(default)</para>
65 <para>i, int - integer</para>
66 <para>h, hex - hex</para>
67 <para>c, char - char</para>
71 <para>Performs mathematical functions based on two parameters and an operator. The returned
72 value type is <replaceable>type</replaceable></para>
73 <para>Example: Set(i=${MATH(123%16,int)}) - sets var i=11</para>
76 <function name="INC" language="en_US">
78 Increments the value of a variable, while returning the updated value to the dialplan
81 <parameter name="variable" required="true">
83 The variable name to be manipulated, without the braces.
88 <para>Increments the value of a variable, while returning the updated value to the dialplan</para>
89 <para>Example: INC(MyVAR) - Increments MyVar</para>
90 <para>Note: INC(${MyVAR}) - Is wrong, as INC expects the variable name, not its value</para>
93 <function name="DEC" language="en_US">
95 Decrements the value of a variable, while returning the updated value to the dialplan
98 <parameter name="variable" required="true">
100 The variable name to be manipulated, without the braces.
105 <para>Decrements the value of a variable, while returning the updated value to the dialplan</para>
106 <para>Example: DEC(MyVAR) - Increments MyVar</para>
107 <para>Note: DEC(${MyVAR}) - Is wrong, as INC expects the variable name, not its value</para>
112 enum TypeOfFunctions {
138 static int math(struct ast_channel *chan, const char *cmd, char *parse,
139 char *buf, size_t len)
146 int type_of_result = FLOAT_RESULT;
147 char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
149 AST_DECLARE_APP_ARGS(args,
154 if (ast_strlen_zero(parse)) {
155 ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
159 AST_STANDARD_APP_ARGS(args, parse);
162 ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
166 mvalue1 = args.argv0;
168 if (mvalue1[0] == '-') {
173 if ((op = strchr(mvalue1, '*'))) {
174 iaction = MULTIPLYFUNCTION;
176 } else if ((op = strchr(mvalue1, '/'))) {
177 iaction = DIVIDEFUNCTION;
179 } else if ((op = strchr(mvalue1, '%'))) {
180 iaction = MODULUSFUNCTION;
182 } else if ((op = strchr(mvalue1, '^'))) {
183 iaction = POWFUNCTION;
185 } else if ((op = strstr(mvalue1, "AND"))) {
186 iaction = BITWISEANDFUNCTION;
189 } else if ((op = strstr(mvalue1, "XOR"))) {
190 iaction = BITWISEXORFUNCTION;
193 } else if ((op = strstr(mvalue1, "OR"))) {
194 iaction = BITWISEORFUNCTION;
197 } else if ((op = strchr(mvalue1, '>'))) {
198 iaction = GTFUNCTION;
200 if (*(op + 1) == '=') {
201 iaction = GTEFUNCTION;
203 } else if (*(op + 1) == '>') {
204 iaction = SHRIGHTFUNCTION;
207 } else if ((op = strchr(mvalue1, '<'))) {
208 iaction = LTFUNCTION;
210 if (*(op + 1) == '=') {
211 iaction = LTEFUNCTION;
213 } else if (*(op + 1) == '<') {
214 iaction = SHLEFTFUNCTION;
217 } else if ((op = strchr(mvalue1, '='))) {
219 if (*(op + 1) == '=') {
220 iaction = EQFUNCTION;
224 } else if ((op = strchr(mvalue1, '+'))) {
225 iaction = ADDFUNCTION;
227 } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative second number */
228 iaction = SUBTRACTFUNCTION;
235 /* detect wanted type of result */
236 mtype_of_result = args.argv1;
237 if (mtype_of_result) {
238 if (!strcasecmp(mtype_of_result, "float")
239 || !strcasecmp(mtype_of_result, "f"))
240 type_of_result = FLOAT_RESULT;
241 else if (!strcasecmp(mtype_of_result, "int")
242 || !strcasecmp(mtype_of_result, "i"))
243 type_of_result = INT_RESULT;
244 else if (!strcasecmp(mtype_of_result, "hex")
245 || !strcasecmp(mtype_of_result, "h"))
246 type_of_result = HEX_RESULT;
247 else if (!strcasecmp(mtype_of_result, "char")
248 || !strcasecmp(mtype_of_result, "c"))
249 type_of_result = CHAR_RESULT;
251 ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
259 "Supply all the parameters - just this once, please\n");
263 if (sscanf(mvalue1, "%30lf", &fnum1) != 1) {
264 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
268 if (sscanf(mvalue2, "%30lf", &fnum2) != 1) {
269 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
278 ftmp = fnum1 + fnum2;
282 ftmp = 0; /* can't do a divide by 0 */
284 ftmp = (fnum1 / fnum2);
286 case MULTIPLYFUNCTION:
287 ftmp = (fnum1 * fnum2);
289 case SUBTRACTFUNCTION:
290 ftmp = (fnum1 - fnum2);
292 case MODULUSFUNCTION:
300 ftmp = (inum1 % inum2);
306 ftmp = pow(fnum1, fnum2);
313 ftmp = (inum1 << inum2);
316 case SHRIGHTFUNCTION:
321 ftmp = (inum1 >> inum2);
324 case BITWISEANDFUNCTION:
328 ftmp = (inum1 & inum2);
331 case BITWISEXORFUNCTION:
335 ftmp = (inum1 ^ inum2);
338 case BITWISEORFUNCTION:
342 ftmp = (inum1 | inum2);
346 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
349 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
352 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
355 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
358 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
362 "Something happened that neither of us should be proud of %d\n",
367 if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
368 if (type_of_result == FLOAT_RESULT)
369 snprintf(buf, len, "%f", ftmp);
370 else if (type_of_result == INT_RESULT)
371 snprintf(buf, len, "%i", (int) ftmp);
372 else if (type_of_result == HEX_RESULT)
373 snprintf(buf, len, "%x", (unsigned int) ftmp);
374 else if (type_of_result == CHAR_RESULT)
375 snprintf(buf, len, "%c", (unsigned char) ftmp);
381 static int crement_function_read(struct ast_channel *chan, const char *cmd,
382 char *data, char *buf, size_t len)
388 char endchar = 0, returnvar[12]; /* If you need a variable longer than 11 digits - something is way wrong */
390 if (ast_strlen_zero(data)) {
391 ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
395 ast_channel_lock(chan);
397 if (!(var = pbx_builtin_getvar_helper(chan, data))) {
398 ast_log(LOG_NOTICE, "Failed to obtain variable %s, bailing out\n", data);
399 ast_channel_unlock(chan);
403 if (ast_strlen_zero(var)) {
404 ast_log(LOG_NOTICE, "Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
405 ast_channel_unlock(chan);
409 if (sscanf(var, "%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
410 ast_log(LOG_NOTICE, "The content of ${%s} is not a numeric value - bailing out!\n", data);
411 ast_channel_unlock(chan);
415 /* now we'll actually do something useful */
416 if (!strcasecmp(cmd, "INC")) { /* Increment variable */
419 } else if (!strcasecmp(cmd, "DEC")) { /* Decrement variable */
424 ast_log(LOG_NOTICE, "The value is now: %d\n", int_value);
426 if (snprintf(returnvar, sizeof(returnvar), "%d", int_value) > 0) {
427 pbx_builtin_setvar_helper(chan, data, returnvar);
429 ast_copy_string(buf, returnvar, len);
433 pbx_builtin_setvar_helper(chan, data, "0");
435 ast_copy_string(buf, "0", len);
437 ast_log(LOG_NOTICE, "Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
441 ast_channel_unlock(chan);
447 static struct ast_custom_function math_function = {
452 static struct ast_custom_function increment_function = {
454 .read = crement_function_read,
457 static struct ast_custom_function decrement_function = {
459 .read = crement_function_read,
462 #ifdef TEST_FRAMEWORK
463 AST_TEST_DEFINE(test_MATH_function)
465 enum ast_test_result_state res = AST_TEST_PASS;
466 struct ast_str *expr, *result;
470 info->name = "test_MATH_function";
471 info->category = "/main/pbx/";
472 info->summary = "Test MATH function substitution";
474 "Executes a series of variable substitutions using the MATH function and ensures that the expected results are received.";
475 return AST_TEST_NOT_RUN;
480 ast_test_status_update(test, "Testing MATH() substitution ...\n");
482 if (!(expr = ast_str_create(16)) || !(result = ast_str_create(16))) {
489 return AST_TEST_FAIL;
492 ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}");
493 ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
494 if (strcmp(ast_str_buffer(result), "42") != 0) {
495 ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
496 ast_str_buffer(result));
500 ast_str_set(&expr, 0, "${MATH(170AND63,i)}");
501 ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
502 if (strcmp(ast_str_buffer(result), "42") != 0) {
503 ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
504 ast_str_buffer(result));
512 static int unload_module(void)
516 res |= ast_custom_function_unregister(&math_function);
517 res |= ast_custom_function_unregister(&increment_function);
518 res |= ast_custom_function_unregister(&decrement_function);
519 AST_TEST_UNREGISTER(test_MATH_function);
524 static int load_module(void)
528 res |= ast_custom_function_register(&math_function);
529 res |= ast_custom_function_register(&increment_function);
530 res |= ast_custom_function_register(&decrement_function);
531 AST_TEST_REGISTER(test_MATH_function);
536 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");