#include "asterisk.h"
#include <math.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/config.h"

Go to the source code of this file.
Enumerations | |
| enum | TypeOfFunctions { ADDFUNCTION, DIVIDEFUNCTION, MULTIPLYFUNCTION, SUBTRACTFUNCTION, MODULUSFUNCTION, POWFUNCTION, SHLEFTFUNCTION, SHRIGHTFUNCTION, BITWISEANDFUNCTION, BITWISEXORFUNCTION, BITWISEORFUNCTION, GTFUNCTION, LTFUNCTION, GTEFUNCTION, LTEFUNCTION, EQFUNCTION } |
| enum | TypeOfResult { FLOAT_RESULT, INT_RESULT, HEX_RESULT, CHAR_RESULT } |
Functions | |
| static void | __reg_module (void) |
| static void | __unreg_module (void) |
| static int | crement_function_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| static int | load_module (void) |
| static int | math (struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len) |
| static int | unload_module (void) |
Variables | |
| static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Mathematical dialplan function" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } |
| static struct ast_module_info * | ast_module_info = &__mod_info |
| static struct ast_custom_function | decrement_function |
| static struct ast_custom_function | increment_function |
| static struct ast_custom_function | math_function |
Definition in file func_math.c.
| enum TypeOfFunctions |
Definition at line 107 of file func_math.c.
00107 { 00108 ADDFUNCTION, 00109 DIVIDEFUNCTION, 00110 MULTIPLYFUNCTION, 00111 SUBTRACTFUNCTION, 00112 MODULUSFUNCTION, 00113 POWFUNCTION, 00114 SHLEFTFUNCTION, 00115 SHRIGHTFUNCTION, 00116 BITWISEANDFUNCTION, 00117 BITWISEXORFUNCTION, 00118 BITWISEORFUNCTION, 00119 GTFUNCTION, 00120 LTFUNCTION, 00121 GTEFUNCTION, 00122 LTEFUNCTION, 00123 EQFUNCTION 00124 };
| enum TypeOfResult |
Definition at line 126 of file func_math.c.
00126 { 00127 FLOAT_RESULT, 00128 INT_RESULT, 00129 HEX_RESULT, 00130 CHAR_RESULT 00131 };
| static void __reg_module | ( | void | ) | [static] |
Definition at line 475 of file func_math.c.
| static void __unreg_module | ( | void | ) | [static] |
Definition at line 475 of file func_math.c.
| static int crement_function_read | ( | struct ast_channel * | chan, | |
| const char * | cmd, | |||
| char * | data, | |||
| char * | buf, | |||
| size_t | len | |||
| ) | [static] |
Definition at line 372 of file func_math.c.
References ast_channel_lock, ast_channel_unlock, ast_copy_string(), ast_log(), ast_strlen_zero(), LOG_NOTICE, LOG_WARNING, pbx_builtin_getvar_helper(), pbx_builtin_setvar_helper(), and var.
00374 { 00375 int ret = -1; 00376 int int_value = 0; 00377 int modify_orig = 0; 00378 const char *var; 00379 char endchar = 0, returnvar[12]; /* If you need a variable longer than 11 digits - something is way wrong */ 00380 00381 if (ast_strlen_zero(data)) { 00382 ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd); 00383 return -1; 00384 } 00385 00386 ast_channel_lock(chan); 00387 00388 if (!(var = pbx_builtin_getvar_helper(chan, data))) { 00389 ast_log(LOG_NOTICE, "Failed to obtain variable %s, bailing out\n", data); 00390 ast_channel_unlock(chan); 00391 return -1; 00392 } 00393 00394 if (ast_strlen_zero(var)) { 00395 ast_log(LOG_NOTICE, "Variable %s doesn't exist - are you sure you wrote it correctly?\n", data); 00396 ast_channel_unlock(chan); 00397 return -1; 00398 } 00399 00400 if (sscanf(var, "%30d%1c", &int_value, &endchar) == 0 || endchar != 0) { 00401 ast_log(LOG_NOTICE, "The content of ${%s} is not a numeric value - bailing out!\n", data); 00402 ast_channel_unlock(chan); 00403 return -1; 00404 } 00405 00406 /* now we'll actually do something useful */ 00407 if (!strcasecmp(cmd, "INC")) { /* Increment variable */ 00408 int_value++; 00409 modify_orig = 1; 00410 } else if (!strcasecmp(cmd, "DEC")) { /* Decrement variable */ 00411 int_value--; 00412 modify_orig = 1; 00413 } 00414 00415 ast_log(LOG_NOTICE, "The value is now: %d\n", int_value); 00416 00417 if (snprintf(returnvar, sizeof(returnvar), "%d", int_value) > 0) { 00418 pbx_builtin_setvar_helper(chan, data, returnvar); 00419 if (modify_orig) { 00420 ast_copy_string(buf, returnvar, len); 00421 } 00422 ret = 0; 00423 } else { 00424 pbx_builtin_setvar_helper(chan, data, "0"); 00425 if (modify_orig) { 00426 ast_copy_string(buf, "0", len); 00427 } 00428 ast_log(LOG_NOTICE, "Variable %s refused to be %sREMENTED, setting value to 0", data, cmd); 00429 ret = 0; 00430 } 00431 00432 ast_channel_unlock(chan); 00433 00434 return ret; 00435 }
| static int load_module | ( | void | ) | [static] |
Definition at line 464 of file func_math.c.
References ast_custom_function_register.
00465 { 00466 int res = 0; 00467 00468 res |= ast_custom_function_register(&math_function); 00469 res |= ast_custom_function_register(&increment_function); 00470 res |= ast_custom_function_register(&decrement_function); 00471 00472 return res; 00473 }
| static int math | ( | struct ast_channel * | chan, | |
| const char * | cmd, | |||
| char * | parse, | |||
| char * | buf, | |||
| size_t | len | |||
| ) | [static] |
Definition at line 133 of file func_math.c.
References ADDFUNCTION, AST_APP_ARG, ast_copy_string(), AST_DECLARE_APP_ARGS, ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), BITWISEANDFUNCTION, BITWISEORFUNCTION, BITWISEXORFUNCTION, CHAR_RESULT, DIVIDEFUNCTION, EQFUNCTION, FLOAT_RESULT, GTEFUNCTION, GTFUNCTION, HEX_RESULT, INT_RESULT, LOG_WARNING, LTEFUNCTION, LTFUNCTION, MODULUSFUNCTION, MULTIPLYFUNCTION, POWFUNCTION, SHLEFTFUNCTION, SHRIGHTFUNCTION, and SUBTRACTFUNCTION.
00135 { 00136 double fnum1; 00137 double fnum2; 00138 double ftmp = 0; 00139 char *op; 00140 int iaction = -1; 00141 int type_of_result = FLOAT_RESULT; 00142 char *mvalue1, *mvalue2 = NULL, *mtype_of_result; 00143 int negvalue1 = 0; 00144 AST_DECLARE_APP_ARGS(args, 00145 AST_APP_ARG(argv0); 00146 AST_APP_ARG(argv1); 00147 ); 00148 00149 if (ast_strlen_zero(parse)) { 00150 ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n"); 00151 return -1; 00152 } 00153 00154 AST_STANDARD_APP_ARGS(args, parse); 00155 00156 if (args.argc < 1) { 00157 ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n"); 00158 return -1; 00159 } 00160 00161 mvalue1 = args.argv0; 00162 00163 if (mvalue1[0] == '-') { 00164 negvalue1 = 1; 00165 mvalue1++; 00166 } 00167 00168 if ((op = strchr(mvalue1, '*'))) { 00169 iaction = MULTIPLYFUNCTION; 00170 *op = '\0'; 00171 } else if ((op = strchr(mvalue1, '/'))) { 00172 iaction = DIVIDEFUNCTION; 00173 *op = '\0'; 00174 } else if ((op = strchr(mvalue1, '%'))) { 00175 iaction = MODULUSFUNCTION; 00176 *op = '\0'; 00177 } else if ((op = strchr(mvalue1, '^'))) { 00178 iaction = POWFUNCTION; 00179 *op = '\0'; 00180 } else if ((op = strstr(mvalue1, "AND"))) { 00181 iaction = BITWISEANDFUNCTION; 00182 op += 3; 00183 *op = '\0'; 00184 } else if ((op = strstr(mvalue1, "XOR"))) { 00185 iaction = BITWISEXORFUNCTION; 00186 op += 3; 00187 *op = '\0'; 00188 } else if ((op = strstr(mvalue1, "OR"))) { 00189 iaction = BITWISEORFUNCTION; 00190 op += 2; 00191 *op = '\0'; 00192 } else if ((op = strchr(mvalue1, '>'))) { 00193 iaction = GTFUNCTION; 00194 *op = '\0'; 00195 if (*(op + 1) == '=') { 00196 *++op = '\0'; 00197 iaction = GTEFUNCTION; 00198 } else if (*(op + 1) == '>') { 00199 *++op = '\0'; 00200 iaction = SHRIGHTFUNCTION; 00201 } 00202 } else if ((op = strchr(mvalue1, '<'))) { 00203 iaction = LTFUNCTION; 00204 *op = '\0'; 00205 if (*(op + 1) == '=') { 00206 *++op = '\0'; 00207 iaction = LTEFUNCTION; 00208 } else if (*(op + 1) == '<') { 00209 *++op = '\0'; 00210 iaction = SHLEFTFUNCTION; 00211 } 00212 } else if ((op = strchr(mvalue1, '='))) { 00213 *op = '\0'; 00214 if (*(op + 1) == '=') { 00215 *++op = '\0'; 00216 iaction = EQFUNCTION; 00217 } else 00218 op = NULL; 00219 } else if ((op = strchr(mvalue1, '+'))) { 00220 iaction = ADDFUNCTION; 00221 *op = '\0'; 00222 } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative first number */ 00223 iaction = SUBTRACTFUNCTION; 00224 *op = '\0'; 00225 } 00226 00227 if (op) 00228 mvalue2 = op + 1; 00229 00230 /* detect wanted type of result */ 00231 mtype_of_result = args.argv1; 00232 if (mtype_of_result) { 00233 if (!strcasecmp(mtype_of_result, "float") 00234 || !strcasecmp(mtype_of_result, "f")) 00235 type_of_result = FLOAT_RESULT; 00236 else if (!strcasecmp(mtype_of_result, "int") 00237 || !strcasecmp(mtype_of_result, "i")) 00238 type_of_result = INT_RESULT; 00239 else if (!strcasecmp(mtype_of_result, "hex") 00240 || !strcasecmp(mtype_of_result, "h")) 00241 type_of_result = HEX_RESULT; 00242 else if (!strcasecmp(mtype_of_result, "char") 00243 || !strcasecmp(mtype_of_result, "c")) 00244 type_of_result = CHAR_RESULT; 00245 else { 00246 ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n", 00247 mtype_of_result); 00248 return -1; 00249 } 00250 } 00251 00252 if (!mvalue1 || !mvalue2) { 00253 ast_log(LOG_WARNING, 00254 "Supply all the parameters - just this once, please\n"); 00255 return -1; 00256 } 00257 00258 if (sscanf(mvalue1, "%30lf", &fnum1) != 1) { 00259 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1); 00260 return -1; 00261 } 00262 00263 if (sscanf(mvalue2, "%30lf", &fnum2) != 1) { 00264 ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2); 00265 return -1; 00266 } 00267 00268 if (negvalue1) 00269 fnum1 = 0 - fnum1; 00270 00271 switch (iaction) { 00272 case ADDFUNCTION: 00273 ftmp = fnum1 + fnum2; 00274 break; 00275 case DIVIDEFUNCTION: 00276 if (fnum2 <= 0) 00277 ftmp = 0; /* can't do a divide by 0 */ 00278 else 00279 ftmp = (fnum1 / fnum2); 00280 break; 00281 case MULTIPLYFUNCTION: 00282 ftmp = (fnum1 * fnum2); 00283 break; 00284 case SUBTRACTFUNCTION: 00285 ftmp = (fnum1 - fnum2); 00286 break; 00287 case MODULUSFUNCTION: 00288 { 00289 int inum1 = fnum1; 00290 int inum2 = fnum2; 00291 00292 ftmp = (inum1 % inum2); 00293 00294 break; 00295 } 00296 case POWFUNCTION: 00297 ftmp = pow(fnum1, fnum2); 00298 break; 00299 case SHLEFTFUNCTION: 00300 { 00301 int inum1 = fnum1; 00302 int inum2 = fnum2; 00303 00304 ftmp = (inum1 << inum2); 00305 break; 00306 } 00307 case SHRIGHTFUNCTION: 00308 { 00309 int inum1 = fnum1; 00310 int inum2 = fnum2; 00311 00312 ftmp = (inum1 >> inum2); 00313 break; 00314 } 00315 case BITWISEANDFUNCTION: 00316 { 00317 int inum1 = fnum1; 00318 int inum2 = fnum2; 00319 ftmp = (inum1 & inum2); 00320 break; 00321 } 00322 case BITWISEXORFUNCTION: 00323 { 00324 int inum1 = fnum1; 00325 int inum2 = fnum2; 00326 ftmp = (inum1 ^ inum2); 00327 break; 00328 } 00329 case BITWISEORFUNCTION: 00330 { 00331 int inum1 = fnum1; 00332 int inum2 = fnum2; 00333 ftmp = (inum1 | inum2); 00334 break; 00335 } 00336 case GTFUNCTION: 00337 ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len); 00338 break; 00339 case LTFUNCTION: 00340 ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len); 00341 break; 00342 case GTEFUNCTION: 00343 ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len); 00344 break; 00345 case LTEFUNCTION: 00346 ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len); 00347 break; 00348 case EQFUNCTION: 00349 ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len); 00350 break; 00351 default: 00352 ast_log(LOG_WARNING, 00353 "Something happened that neither of us should be proud of %d\n", 00354 iaction); 00355 return -1; 00356 } 00357 00358 if (iaction < GTFUNCTION || iaction > EQFUNCTION) { 00359 if (type_of_result == FLOAT_RESULT) 00360 snprintf(buf, len, "%f", ftmp); 00361 else if (type_of_result == INT_RESULT) 00362 snprintf(buf, len, "%i", (int) ftmp); 00363 else if (type_of_result == HEX_RESULT) 00364 snprintf(buf, len, "%x", (unsigned int) ftmp); 00365 else if (type_of_result == CHAR_RESULT) 00366 snprintf(buf, len, "%c", (unsigned char) ftmp); 00367 } 00368 00369 return 0; 00370 }
| static int unload_module | ( | void | ) | [static] |
Definition at line 453 of file func_math.c.
References ast_custom_function_unregister().
00454 { 00455 int res = 0; 00456 00457 res |= ast_custom_function_unregister(&math_function); 00458 res |= ast_custom_function_unregister(&increment_function); 00459 res |= ast_custom_function_unregister(&decrement_function); 00460 00461 return res; 00462 }
struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "Mathematical dialplan function" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, } [static] |
Definition at line 475 of file func_math.c.
struct ast_module_info* ast_module_info = &__mod_info [static] |
Definition at line 475 of file func_math.c.
struct ast_custom_function decrement_function [static] |
Initial value:
{
.name = "DEC",
.read = crement_function_read,
}
Definition at line 448 of file func_math.c.
struct ast_custom_function increment_function [static] |
Initial value:
{
.name = "INC",
.read = crement_function_read,
}
Definition at line 443 of file func_math.c.
struct ast_custom_function math_function [static] |
1.5.6