func_math.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2004 - 2006, Andy Powell
  5. *
  6. * Updated by Mark Spencer <markster@digium.com>
  7. * Updated by Nir Simionovich <nirs@greenfieldtech.net>
  8. *
  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.
  14. *
  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.
  18. */
  19. /*! \file
  20. *
  21. * \brief Math related dialplan function
  22. *
  23. * \author Andy Powell
  24. * \author Mark Spencer <markster@digium.com>
  25. * \author Nir Simionovich <nirs@greenfieldtech.net>
  26. *
  27. * \ingroup functions
  28. */
  29. /*** MODULEINFO
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include <math.h>
  35. #include "asterisk/module.h"
  36. #include "asterisk/channel.h"
  37. #include "asterisk/pbx.h"
  38. #include "asterisk/utils.h"
  39. #include "asterisk/app.h"
  40. #include "asterisk/config.h"
  41. #include "asterisk/test.h"
  42. /*** DOCUMENTATION
  43. <function name="MATH" language="en_US">
  44. <synopsis>
  45. Performs Mathematical Functions.
  46. </synopsis>
  47. <syntax>
  48. <parameter name="expression" required="true">
  49. <para>Is of the form:
  50. <replaceable>number1</replaceable><replaceable>op</replaceable><replaceable>number2</replaceable>
  51. where the possible values for <replaceable>op</replaceable>
  52. are:</para>
  53. <para>+,-,/,*,%,&lt;&lt;,&gt;&gt;,^,AND,OR,XOR,&lt;,&gt;,&lt;=,&gt;=,== (and behave as their C equivalents)</para>
  54. </parameter>
  55. <parameter name="type">
  56. <para>Wanted type of result:</para>
  57. <para>f, float - float(default)</para>
  58. <para>i, int - integer</para>
  59. <para>h, hex - hex</para>
  60. <para>c, char - char</para>
  61. </parameter>
  62. </syntax>
  63. <description>
  64. <para>Performs mathematical functions based on two parameters and an operator. The returned
  65. value type is <replaceable>type</replaceable></para>
  66. <para>Example: Set(i=${MATH(123%16,int)}) - sets var i=11</para>
  67. </description>
  68. </function>
  69. <function name="INC" language="en_US">
  70. <synopsis>
  71. Increments the value of a variable, while returning the updated value to the dialplan
  72. </synopsis>
  73. <syntax>
  74. <parameter name="variable" required="true">
  75. <para>
  76. The variable name to be manipulated, without the braces.
  77. </para>
  78. </parameter>
  79. </syntax>
  80. <description>
  81. <para>Increments the value of a variable, while returning the updated value to the dialplan</para>
  82. <para>Example: INC(MyVAR) - Increments MyVar</para>
  83. <para>Note: INC(${MyVAR}) - Is wrong, as INC expects the variable name, not its value</para>
  84. </description>
  85. </function>
  86. <function name="DEC" language="en_US">
  87. <synopsis>
  88. Decrements the value of a variable, while returning the updated value to the dialplan
  89. </synopsis>
  90. <syntax>
  91. <parameter name="variable" required="true">
  92. <para>
  93. The variable name to be manipulated, without the braces.
  94. </para>
  95. </parameter>
  96. </syntax>
  97. <description>
  98. <para>Decrements the value of a variable, while returning the updated value to the dialplan</para>
  99. <para>Example: DEC(MyVAR) - Decrements MyVar</para>
  100. <para>Note: DEC(${MyVAR}) - Is wrong, as DEC expects the variable name, not its value</para>
  101. </description>
  102. </function>
  103. ***/
  104. enum TypeOfFunctions {
  105. ADDFUNCTION,
  106. DIVIDEFUNCTION,
  107. MULTIPLYFUNCTION,
  108. SUBTRACTFUNCTION,
  109. MODULUSFUNCTION,
  110. POWFUNCTION,
  111. SHLEFTFUNCTION,
  112. SHRIGHTFUNCTION,
  113. BITWISEANDFUNCTION,
  114. BITWISEXORFUNCTION,
  115. BITWISEORFUNCTION,
  116. GTFUNCTION,
  117. LTFUNCTION,
  118. GTEFUNCTION,
  119. LTEFUNCTION,
  120. EQFUNCTION
  121. };
  122. enum TypeOfResult {
  123. FLOAT_RESULT,
  124. INT_RESULT,
  125. HEX_RESULT,
  126. CHAR_RESULT
  127. };
  128. static int math(struct ast_channel *chan, const char *cmd, char *parse,
  129. char *buf, size_t len)
  130. {
  131. double fnum1;
  132. double fnum2;
  133. double ftmp = 0;
  134. char *op;
  135. int iaction = -1;
  136. int type_of_result = FLOAT_RESULT;
  137. char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
  138. int negvalue1 = 0;
  139. AST_DECLARE_APP_ARGS(args,
  140. AST_APP_ARG(argv0);
  141. AST_APP_ARG(argv1);
  142. );
  143. if (ast_strlen_zero(parse)) {
  144. ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
  145. return -1;
  146. }
  147. AST_STANDARD_APP_ARGS(args, parse);
  148. if (args.argc < 1) {
  149. ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
  150. return -1;
  151. }
  152. mvalue1 = args.argv0;
  153. if (mvalue1[0] == '-') {
  154. negvalue1 = 1;
  155. mvalue1++;
  156. }
  157. if ((op = strchr(mvalue1, '*'))) {
  158. iaction = MULTIPLYFUNCTION;
  159. *op = '\0';
  160. } else if ((op = strchr(mvalue1, '/'))) {
  161. iaction = DIVIDEFUNCTION;
  162. *op = '\0';
  163. } else if ((op = strchr(mvalue1, '%'))) {
  164. iaction = MODULUSFUNCTION;
  165. *op = '\0';
  166. } else if ((op = strchr(mvalue1, '^'))) {
  167. iaction = POWFUNCTION;
  168. *op = '\0';
  169. } else if ((op = strstr(mvalue1, "AND"))) {
  170. iaction = BITWISEANDFUNCTION;
  171. *op = '\0';
  172. op += 2;
  173. } else if ((op = strstr(mvalue1, "XOR"))) {
  174. iaction = BITWISEXORFUNCTION;
  175. *op = '\0';
  176. op += 2;
  177. } else if ((op = strstr(mvalue1, "OR"))) {
  178. iaction = BITWISEORFUNCTION;
  179. *op = '\0';
  180. ++op;
  181. } else if ((op = strchr(mvalue1, '>'))) {
  182. iaction = GTFUNCTION;
  183. *op = '\0';
  184. if (*(op + 1) == '=') {
  185. iaction = GTEFUNCTION;
  186. ++op;
  187. } else if (*(op + 1) == '>') {
  188. iaction = SHRIGHTFUNCTION;
  189. ++op;
  190. }
  191. } else if ((op = strchr(mvalue1, '<'))) {
  192. iaction = LTFUNCTION;
  193. *op = '\0';
  194. if (*(op + 1) == '=') {
  195. iaction = LTEFUNCTION;
  196. ++op;
  197. } else if (*(op + 1) == '<') {
  198. iaction = SHLEFTFUNCTION;
  199. ++op;
  200. }
  201. } else if ((op = strchr(mvalue1, '='))) {
  202. *op = '\0';
  203. if (*(op + 1) == '=') {
  204. iaction = EQFUNCTION;
  205. ++op;
  206. } else
  207. op = NULL;
  208. } else if ((op = strchr(mvalue1, '+'))) {
  209. iaction = ADDFUNCTION;
  210. *op = '\0';
  211. } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative second number */
  212. iaction = SUBTRACTFUNCTION;
  213. *op = '\0';
  214. }
  215. if (op)
  216. mvalue2 = op + 1;
  217. /* detect wanted type of result */
  218. mtype_of_result = args.argv1;
  219. if (mtype_of_result) {
  220. if (!strcasecmp(mtype_of_result, "float")
  221. || !strcasecmp(mtype_of_result, "f"))
  222. type_of_result = FLOAT_RESULT;
  223. else if (!strcasecmp(mtype_of_result, "int")
  224. || !strcasecmp(mtype_of_result, "i"))
  225. type_of_result = INT_RESULT;
  226. else if (!strcasecmp(mtype_of_result, "hex")
  227. || !strcasecmp(mtype_of_result, "h"))
  228. type_of_result = HEX_RESULT;
  229. else if (!strcasecmp(mtype_of_result, "char")
  230. || !strcasecmp(mtype_of_result, "c"))
  231. type_of_result = CHAR_RESULT;
  232. else {
  233. ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
  234. mtype_of_result);
  235. return -1;
  236. }
  237. }
  238. if (!mvalue2) {
  239. ast_log(LOG_WARNING,
  240. "Supply all the parameters - just this once, please\n");
  241. return -1;
  242. }
  243. if (sscanf(mvalue1, "%30lf", &fnum1) != 1) {
  244. ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
  245. return -1;
  246. }
  247. if (sscanf(mvalue2, "%30lf", &fnum2) != 1) {
  248. ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
  249. return -1;
  250. }
  251. if (negvalue1)
  252. fnum1 = 0 - fnum1;
  253. switch (iaction) {
  254. case ADDFUNCTION:
  255. ftmp = fnum1 + fnum2;
  256. break;
  257. case DIVIDEFUNCTION:
  258. if (fnum2 <= 0)
  259. ftmp = 0; /* can't do a divide by 0 */
  260. else
  261. ftmp = (fnum1 / fnum2);
  262. break;
  263. case MULTIPLYFUNCTION:
  264. ftmp = (fnum1 * fnum2);
  265. break;
  266. case SUBTRACTFUNCTION:
  267. ftmp = (fnum1 - fnum2);
  268. break;
  269. case MODULUSFUNCTION:
  270. {
  271. int inum1 = fnum1;
  272. int inum2 = fnum2;
  273. if (inum2 == 0) {
  274. ftmp = 0;
  275. } else {
  276. ftmp = (inum1 % inum2);
  277. }
  278. break;
  279. }
  280. case POWFUNCTION:
  281. ftmp = pow(fnum1, fnum2);
  282. break;
  283. case SHLEFTFUNCTION:
  284. {
  285. int inum1 = fnum1;
  286. int inum2 = fnum2;
  287. ftmp = (inum1 << inum2);
  288. break;
  289. }
  290. case SHRIGHTFUNCTION:
  291. {
  292. int inum1 = fnum1;
  293. int inum2 = fnum2;
  294. ftmp = (inum1 >> inum2);
  295. break;
  296. }
  297. case BITWISEANDFUNCTION:
  298. {
  299. int inum1 = fnum1;
  300. int inum2 = fnum2;
  301. ftmp = (inum1 & inum2);
  302. break;
  303. }
  304. case BITWISEXORFUNCTION:
  305. {
  306. int inum1 = fnum1;
  307. int inum2 = fnum2;
  308. ftmp = (inum1 ^ inum2);
  309. break;
  310. }
  311. case BITWISEORFUNCTION:
  312. {
  313. int inum1 = fnum1;
  314. int inum2 = fnum2;
  315. ftmp = (inum1 | inum2);
  316. break;
  317. }
  318. case GTFUNCTION:
  319. ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
  320. break;
  321. case LTFUNCTION:
  322. ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
  323. break;
  324. case GTEFUNCTION:
  325. ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
  326. break;
  327. case LTEFUNCTION:
  328. ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
  329. break;
  330. case EQFUNCTION:
  331. ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
  332. break;
  333. default:
  334. ast_log(LOG_WARNING,
  335. "Something happened that neither of us should be proud of %d\n",
  336. iaction);
  337. return -1;
  338. }
  339. if (iaction < GTFUNCTION || iaction > EQFUNCTION) {
  340. if (type_of_result == FLOAT_RESULT)
  341. snprintf(buf, len, "%f", ftmp);
  342. else if (type_of_result == INT_RESULT)
  343. snprintf(buf, len, "%i", (int) ftmp);
  344. else if (type_of_result == HEX_RESULT)
  345. snprintf(buf, len, "%x", (unsigned int) ftmp);
  346. else if (type_of_result == CHAR_RESULT)
  347. snprintf(buf, len, "%c", (unsigned char) ftmp);
  348. }
  349. return 0;
  350. }
  351. static int crement_function_read(struct ast_channel *chan, const char *cmd,
  352. char *data, char *buf, size_t len)
  353. {
  354. int ret = -1;
  355. int int_value = 0;
  356. int modify_orig = 0;
  357. const char *var;
  358. char endchar = 0, returnvar[12]; /* If you need a variable longer than 11 digits - something is way wrong */
  359. if (ast_strlen_zero(data)) {
  360. ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
  361. return -1;
  362. }
  363. if (!chan) {
  364. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  365. return -1;
  366. }
  367. ast_channel_lock(chan);
  368. if (!(var = pbx_builtin_getvar_helper(chan, data))) {
  369. ast_log(LOG_NOTICE, "Failed to obtain variable %s, bailing out\n", data);
  370. ast_channel_unlock(chan);
  371. return -1;
  372. }
  373. if (ast_strlen_zero(var)) {
  374. ast_log(LOG_NOTICE, "Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
  375. ast_channel_unlock(chan);
  376. return -1;
  377. }
  378. if (sscanf(var, "%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
  379. ast_log(LOG_NOTICE, "The content of ${%s} is not a numeric value - bailing out!\n", data);
  380. ast_channel_unlock(chan);
  381. return -1;
  382. }
  383. /* now we'll actually do something useful */
  384. if (!strcasecmp(cmd, "INC")) { /* Increment variable */
  385. int_value++;
  386. modify_orig = 1;
  387. } else if (!strcasecmp(cmd, "DEC")) { /* Decrement variable */
  388. int_value--;
  389. modify_orig = 1;
  390. }
  391. if (snprintf(returnvar, sizeof(returnvar), "%d", int_value) > 0) {
  392. pbx_builtin_setvar_helper(chan, data, returnvar);
  393. if (modify_orig) {
  394. ast_copy_string(buf, returnvar, len);
  395. }
  396. ret = 0;
  397. } else {
  398. pbx_builtin_setvar_helper(chan, data, "0");
  399. if (modify_orig) {
  400. ast_copy_string(buf, "0", len);
  401. }
  402. ast_log(LOG_NOTICE, "Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
  403. ret = 0;
  404. }
  405. ast_channel_unlock(chan);
  406. return ret;
  407. }
  408. static struct ast_custom_function math_function = {
  409. .name = "MATH",
  410. .read = math
  411. };
  412. static struct ast_custom_function increment_function = {
  413. .name = "INC",
  414. .read = crement_function_read,
  415. };
  416. static struct ast_custom_function decrement_function = {
  417. .name = "DEC",
  418. .read = crement_function_read,
  419. };
  420. #ifdef TEST_FRAMEWORK
  421. AST_TEST_DEFINE(test_MATH_function)
  422. {
  423. enum ast_test_result_state res = AST_TEST_PASS;
  424. struct ast_str *expr, *result;
  425. switch (cmd) {
  426. case TEST_INIT:
  427. info->name = "test_MATH_function";
  428. info->category = "/main/pbx/";
  429. info->summary = "Test MATH function substitution";
  430. info->description =
  431. "Executes a series of variable substitutions using the MATH function and ensures that the expected results are received.";
  432. return AST_TEST_NOT_RUN;
  433. case TEST_EXECUTE:
  434. break;
  435. }
  436. ast_test_status_update(test, "Testing MATH() substitution ...\n");
  437. if (!(expr = ast_str_create(16)) || !(result = ast_str_create(16))) {
  438. if (expr) {
  439. ast_free(expr);
  440. }
  441. if (result) {
  442. ast_free(result);
  443. }
  444. return AST_TEST_FAIL;
  445. }
  446. ast_str_set(&expr, 0, "${MATH(170 AND 63,i)}");
  447. ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
  448. if (strcmp(ast_str_buffer(result), "42") != 0) {
  449. ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
  450. ast_str_buffer(result));
  451. res = AST_TEST_FAIL;
  452. }
  453. ast_str_set(&expr, 0, "${MATH(170AND63,i)}");
  454. ast_str_substitute_variables(&result, 0, NULL, ast_str_buffer(expr));
  455. if (strcmp(ast_str_buffer(result), "42") != 0) {
  456. ast_test_status_update(test, "Expected result '42' not returned! ('%s')\n",
  457. ast_str_buffer(result));
  458. res = AST_TEST_FAIL;
  459. }
  460. ast_free(expr);
  461. ast_free(result);
  462. return res;
  463. }
  464. #endif
  465. static int unload_module(void)
  466. {
  467. int res = 0;
  468. res |= ast_custom_function_unregister(&math_function);
  469. res |= ast_custom_function_unregister(&increment_function);
  470. res |= ast_custom_function_unregister(&decrement_function);
  471. AST_TEST_UNREGISTER(test_MATH_function);
  472. return res;
  473. }
  474. static int load_module(void)
  475. {
  476. int res = 0;
  477. res |= ast_custom_function_register(&math_function);
  478. res |= ast_custom_function_register(&increment_function);
  479. res |= ast_custom_function_register(&decrement_function);
  480. AST_TEST_REGISTER(test_MATH_function);
  481. return res;
  482. }
  483. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Mathematical dialplan function");