2005-06-09 22:59:08 +00:00
|
|
|
/*
|
2005-09-14 20:46:50 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
2005-06-09 22:59:08 +00:00
|
|
|
*
|
2006-02-12 04:28:58 +00:00
|
|
|
* Copyright (C) 2004 - 2006, Andy Powell
|
2005-06-09 22:59:08 +00:00
|
|
|
*
|
|
|
|
* Updated by Mark Spencer <markster@digium.com>
|
|
|
|
*
|
2005-09-14 20:46:50 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
2005-09-14 20:46:50 +00:00
|
|
|
*
|
2006-02-12 04:28:58 +00:00
|
|
|
* \brief Math related dialplan function
|
2005-12-30 21:18:06 +00:00
|
|
|
*
|
|
|
|
* \author Andy Powell
|
|
|
|
* \author Mark Spencer <markster@digium.com>
|
2007-01-24 09:05:29 +00:00
|
|
|
*
|
|
|
|
* \ingroup functions
|
2005-06-09 22:59:08 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-07 18:54:56 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
2006-12-31 05:20:18 +00:00
|
|
|
#include <math.h>
|
2005-06-09 22:59:08 +00:00
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
#include "asterisk/module.h"
|
2005-06-09 22:59:08 +00:00
|
|
|
#include "asterisk/channel.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
#include "asterisk/utils.h"
|
|
|
|
#include "asterisk/app.h"
|
|
|
|
#include "asterisk/config.h"
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
enum TypeOfFunctions {
|
|
|
|
ADDFUNCTION,
|
|
|
|
DIVIDEFUNCTION,
|
|
|
|
MULTIPLYFUNCTION,
|
|
|
|
SUBTRACTFUNCTION,
|
|
|
|
MODULUSFUNCTION,
|
2006-12-31 05:20:18 +00:00
|
|
|
POWFUNCTION,
|
|
|
|
SHLEFTFUNCTION,
|
|
|
|
SHRIGHTFUNCTION,
|
2007-06-28 20:52:22 +00:00
|
|
|
BITWISEANDFUNCTION,
|
|
|
|
BITWISEXORFUNCTION,
|
|
|
|
BITWISEORFUNCTION,
|
2006-02-12 04:28:58 +00:00
|
|
|
GTFUNCTION,
|
|
|
|
LTFUNCTION,
|
|
|
|
GTEFUNCTION,
|
|
|
|
LTEFUNCTION,
|
|
|
|
EQFUNCTION
|
2005-06-09 22:59:08 +00:00
|
|
|
};
|
|
|
|
|
2006-02-12 04:28:58 +00:00
|
|
|
enum TypeOfResult {
|
|
|
|
FLOAT_RESULT,
|
|
|
|
INT_RESULT,
|
|
|
|
HEX_RESULT,
|
|
|
|
CHAR_RESULT
|
2005-06-09 22:59:08 +00:00
|
|
|
};
|
|
|
|
|
2007-01-06 00:13:33 +00:00
|
|
|
static int math(struct ast_channel *chan, const char *cmd, char *parse,
|
2006-02-12 04:28:58 +00:00
|
|
|
char *buf, size_t len)
|
2005-06-09 22:59:08 +00:00
|
|
|
{
|
2007-06-04 23:45:05 +00:00
|
|
|
double fnum1;
|
|
|
|
double fnum2;
|
|
|
|
double ftmp = 0;
|
2005-06-09 22:59:08 +00:00
|
|
|
char *op;
|
2006-02-12 04:28:58 +00:00
|
|
|
int iaction = -1;
|
|
|
|
int type_of_result = FLOAT_RESULT;
|
|
|
|
char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
|
2007-05-24 15:35:50 +00:00
|
|
|
int negvalue1 = 0;
|
2006-01-10 16:08:28 +00:00
|
|
|
AST_DECLARE_APP_ARGS(args,
|
2006-02-12 04:28:58 +00:00
|
|
|
AST_APP_ARG(argv0);
|
|
|
|
AST_APP_ARG(argv1);
|
2006-01-10 16:08:28 +00:00
|
|
|
);
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
if (ast_strlen_zero(parse)) {
|
2005-06-09 22:59:08 +00:00
|
|
|
ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2006-01-10 16:08:28 +00:00
|
|
|
AST_STANDARD_APP_ARGS(args, parse);
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2006-01-10 16:08:28 +00:00
|
|
|
if (args.argc < 1) {
|
2005-06-09 22:59:08 +00:00
|
|
|
ast_log(LOG_WARNING, "Syntax: Math(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2006-01-10 16:08:28 +00:00
|
|
|
mvalue1 = args.argv0;
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2007-05-24 15:35:50 +00:00
|
|
|
if (mvalue1[0] == '-') {
|
|
|
|
negvalue1 = 1;
|
|
|
|
mvalue1++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((op = strchr(mvalue1, '*'))) {
|
2005-06-09 22:59:08 +00:00
|
|
|
iaction = MULTIPLYFUNCTION;
|
|
|
|
*op = '\0';
|
|
|
|
} else if ((op = strchr(mvalue1, '/'))) {
|
|
|
|
iaction = DIVIDEFUNCTION;
|
|
|
|
*op = '\0';
|
|
|
|
} else if ((op = strchr(mvalue1, '%'))) {
|
|
|
|
iaction = MODULUSFUNCTION;
|
|
|
|
*op = '\0';
|
2006-12-31 05:20:18 +00:00
|
|
|
} else if ((op = strchr(mvalue1, '^'))) {
|
|
|
|
iaction = POWFUNCTION;
|
|
|
|
*op = '\0';
|
2007-06-28 20:52:22 +00:00
|
|
|
} else if ((op = strstr(mvalue1, "AND"))) {
|
|
|
|
iaction = BITWISEANDFUNCTION;
|
|
|
|
op += 3;
|
|
|
|
*op = '\0';
|
|
|
|
} else if ((op = strstr(mvalue1, "XOR"))) {
|
|
|
|
iaction = BITWISEXORFUNCTION;
|
|
|
|
op += 3;
|
|
|
|
*op = '\0';
|
|
|
|
} else if ((op = strstr(mvalue1, "OR"))) {
|
|
|
|
iaction = BITWISEORFUNCTION;
|
|
|
|
op += 2;
|
|
|
|
*op = '\0';
|
2005-06-09 22:59:08 +00:00
|
|
|
} else if ((op = strchr(mvalue1, '>'))) {
|
|
|
|
iaction = GTFUNCTION;
|
|
|
|
*op = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
if (*(op + 1) == '=') {
|
2005-06-09 22:59:08 +00:00
|
|
|
*++op = '\0';
|
|
|
|
iaction = GTEFUNCTION;
|
2006-12-31 05:20:18 +00:00
|
|
|
} else if (*(op + 1) == '>') {
|
|
|
|
*++op = '\0';
|
|
|
|
iaction = SHRIGHTFUNCTION;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
} else if ((op = strchr(mvalue1, '<'))) {
|
|
|
|
iaction = LTFUNCTION;
|
|
|
|
*op = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
if (*(op + 1) == '=') {
|
2005-06-09 22:59:08 +00:00
|
|
|
*++op = '\0';
|
|
|
|
iaction = LTEFUNCTION;
|
2006-12-31 05:20:18 +00:00
|
|
|
} else if (*(op + 1) == '<') {
|
|
|
|
*++op = '\0';
|
|
|
|
iaction = SHLEFTFUNCTION;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
} else if ((op = strchr(mvalue1, '='))) {
|
|
|
|
*op = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
if (*(op + 1) == '=') {
|
2005-06-09 22:59:08 +00:00
|
|
|
*++op = '\0';
|
|
|
|
iaction = EQFUNCTION;
|
|
|
|
} else
|
|
|
|
op = NULL;
|
2007-05-24 15:35:50 +00:00
|
|
|
} else if ((op = strchr(mvalue1, '+'))) {
|
|
|
|
iaction = ADDFUNCTION;
|
|
|
|
*op = '\0';
|
|
|
|
} else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative first number */
|
|
|
|
iaction = SUBTRACTFUNCTION;
|
|
|
|
*op = '\0';
|
2006-02-12 04:28:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (op)
|
2005-06-09 22:59:08 +00:00
|
|
|
mvalue2 = op + 1;
|
|
|
|
|
|
|
|
/* detect wanted type of result */
|
2006-01-10 16:08:28 +00:00
|
|
|
mtype_of_result = args.argv1;
|
2006-02-12 04:28:58 +00:00
|
|
|
if (mtype_of_result) {
|
|
|
|
if (!strcasecmp(mtype_of_result, "float")
|
|
|
|
|| !strcasecmp(mtype_of_result, "f"))
|
|
|
|
type_of_result = FLOAT_RESULT;
|
|
|
|
else if (!strcasecmp(mtype_of_result, "int")
|
|
|
|
|| !strcasecmp(mtype_of_result, "i"))
|
|
|
|
type_of_result = INT_RESULT;
|
|
|
|
else if (!strcasecmp(mtype_of_result, "hex")
|
|
|
|
|| !strcasecmp(mtype_of_result, "h"))
|
|
|
|
type_of_result = HEX_RESULT;
|
|
|
|
else if (!strcasecmp(mtype_of_result, "char")
|
|
|
|
|| !strcasecmp(mtype_of_result, "c"))
|
|
|
|
type_of_result = CHAR_RESULT;
|
|
|
|
else {
|
|
|
|
ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
|
|
|
|
mtype_of_result);
|
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
2005-06-09 22:59:08 +00:00
|
|
|
if (!mvalue1 || !mvalue2) {
|
2006-02-12 04:28:58 +00:00
|
|
|
ast_log(LOG_WARNING,
|
|
|
|
"Supply all the parameters - just this once, please\n");
|
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-04 23:45:05 +00:00
|
|
|
if (sscanf(mvalue1, "%lf", &fnum1) != 1) {
|
2005-06-09 22:59:08 +00:00
|
|
|
ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-04 23:45:05 +00:00
|
|
|
if (sscanf(mvalue2, "%lf", &fnum2) != 1) {
|
2005-06-09 22:59:08 +00:00
|
|
|
ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
|
2006-02-12 04:28:58 +00:00
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2007-05-24 15:35:50 +00:00
|
|
|
if (negvalue1)
|
|
|
|
fnum1 = 0 - fnum1;
|
|
|
|
|
2005-06-09 22:59:08 +00:00
|
|
|
switch (iaction) {
|
2006-02-12 04:28:58 +00:00
|
|
|
case ADDFUNCTION:
|
2005-06-09 22:59:08 +00:00
|
|
|
ftmp = fnum1 + fnum2;
|
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case DIVIDEFUNCTION:
|
2005-06-09 22:59:08 +00:00
|
|
|
if (fnum2 <= 0)
|
2006-02-12 04:28:58 +00:00
|
|
|
ftmp = 0; /* can't do a divide by 0 */
|
2005-06-09 22:59:08 +00:00
|
|
|
else
|
|
|
|
ftmp = (fnum1 / fnum2);
|
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case MULTIPLYFUNCTION:
|
2005-06-09 22:59:08 +00:00
|
|
|
ftmp = (fnum1 * fnum2);
|
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case SUBTRACTFUNCTION:
|
2005-06-09 22:59:08 +00:00
|
|
|
ftmp = (fnum1 - fnum2);
|
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case MODULUSFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
|
|
|
|
ftmp = (inum1 % inum2);
|
|
|
|
|
2006-12-31 05:20:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case POWFUNCTION:
|
|
|
|
ftmp = pow(fnum1, fnum2);
|
|
|
|
break;
|
|
|
|
case SHLEFTFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
|
|
|
|
ftmp = (inum1 << inum2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SHRIGHTFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
|
|
|
|
ftmp = (inum1 >> inum2);
|
2006-02-12 04:28:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-06-28 20:52:22 +00:00
|
|
|
case BITWISEANDFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
ftmp = (inum1 & inum2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BITWISEXORFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
ftmp = (inum1 ^ inum2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case BITWISEORFUNCTION:
|
|
|
|
{
|
|
|
|
int inum1 = fnum1;
|
|
|
|
int inum2 = fnum2;
|
|
|
|
ftmp = (inum1 | inum2);
|
|
|
|
break;
|
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
case GTFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case LTFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case GTEFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case LTEFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
case EQFUNCTION:
|
|
|
|
ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
|
2005-06-09 22:59:08 +00:00
|
|
|
break;
|
2006-02-12 04:28:58 +00:00
|
|
|
default:
|
|
|
|
ast_log(LOG_WARNING,
|
|
|
|
"Something happened that neither of us should be proud of %d\n",
|
|
|
|
iaction);
|
|
|
|
return -1;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
|
2006-02-12 04:28:58 +00:00
|
|
|
if (type_of_result == FLOAT_RESULT)
|
|
|
|
snprintf(buf, len, "%f", ftmp);
|
|
|
|
else if (type_of_result == INT_RESULT)
|
|
|
|
snprintf(buf, len, "%i", (int) ftmp);
|
|
|
|
else if (type_of_result == HEX_RESULT)
|
|
|
|
snprintf(buf, len, "%x", (unsigned int) ftmp);
|
|
|
|
else if (type_of_result == CHAR_RESULT)
|
|
|
|
snprintf(buf, len, "%c", (unsigned char) ftmp);
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
2006-02-12 04:28:58 +00:00
|
|
|
|
|
|
|
return 0;
|
2005-06-09 22:59:08 +00:00
|
|
|
}
|
|
|
|
|
2006-02-11 03:14:05 +00:00
|
|
|
static struct ast_custom_function math_function = {
|
2005-06-09 22:59:08 +00:00
|
|
|
.name = "MATH",
|
|
|
|
.synopsis = "Performs Mathematical Functions",
|
2007-01-02 17:04:08 +00:00
|
|
|
.syntax = "MATH(<number1><op><number2>[,<type_of_result>])",
|
|
|
|
.desc = "Perform calculation on number1 to number2. Valid ops are: \n"
|
2007-06-28 20:52:22 +00:00
|
|
|
" +,-,/,*,%,<<,>>,^,AND,OR,XOR,<,>,>=,<=,==\n"
|
2005-06-09 22:59:08 +00:00
|
|
|
"and behave as their C equivalents.\n"
|
|
|
|
"<type_of_result> - wanted type of result:\n"
|
|
|
|
" f, float - float(default)\n"
|
|
|
|
" i, int - integer,\n"
|
|
|
|
" h, hex - hex,\n"
|
|
|
|
" c, char - char\n"
|
|
|
|
"Example: Set(i=${MATH(123%16,int)}) - sets var i=11",
|
2006-02-11 03:14:05 +00:00
|
|
|
.read = math
|
2005-06-09 22:59:08 +00:00
|
|
|
};
|
2006-02-11 03:14:05 +00:00
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int unload_module(void)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
2006-02-12 04:28:58 +00:00
|
|
|
return ast_custom_function_unregister(&math_function);
|
2006-02-11 03:14:05 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
static int load_module(void)
|
2006-02-11 03:14:05 +00:00
|
|
|
{
|
2006-02-12 04:28:58 +00:00
|
|
|
return ast_custom_function_register(&math_function);
|
2006-02-11 03:14:05 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 02:11:39 +00:00
|
|
|
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");
|