Wed Jan 8 2020 09:50:13

Asterisk developer's documentation


func_math.c File Reference

Math related dialplan function. More...

#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"
#include "asterisk/test.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_LOAD_ORDER , .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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
 
static struct ast_module_infoast_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
 

Detailed Description

Math related dialplan function.

Author
Andy Powell
Mark Spencer marks.nosp@m.ter@.nosp@m.digiu.nosp@m.m.co.nosp@m.m
Nir Simionovich nirs@.nosp@m.gree.nosp@m.nfiel.nosp@m.dtec.nosp@m.h.net

Definition in file func_math.c.

Enumeration Type Documentation

Enumerator
ADDFUNCTION 
DIVIDEFUNCTION 
MULTIPLYFUNCTION 
SUBTRACTFUNCTION 
MODULUSFUNCTION 
POWFUNCTION 
SHLEFTFUNCTION 
SHRIGHTFUNCTION 
BITWISEANDFUNCTION 
BITWISEXORFUNCTION 
BITWISEORFUNCTION 
GTFUNCTION 
LTFUNCTION 
GTEFUNCTION 
LTEFUNCTION 
EQFUNCTION 

Definition at line 112 of file func_math.c.

Enumerator
FLOAT_RESULT 
INT_RESULT 
HEX_RESULT 
CHAR_RESULT 

Definition at line 131 of file func_math.c.

131  {
132  FLOAT_RESULT,
133  INT_RESULT,
134  HEX_RESULT,
136 };

Function Documentation

static void __reg_module ( void  )
static

Definition at line 542 of file func_math.c.

static void __unreg_module ( void  )
static

Definition at line 542 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 381 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.

383 {
384  int ret = -1;
385  int int_value = 0;
386  int modify_orig = 0;
387  const char *var;
388  char endchar = 0, returnvar[12]; /* If you need a variable longer than 11 digits - something is way wrong */
389 
390  if (ast_strlen_zero(data)) {
391  ast_log(LOG_WARNING, "Syntax: %s(<data>) - missing argument!\n", cmd);
392  return -1;
393  }
394 
395  if (!chan) {
396  ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
397  return -1;
398  }
399 
400  ast_channel_lock(chan);
401 
402  if (!(var = pbx_builtin_getvar_helper(chan, data))) {
403  ast_log(LOG_NOTICE, "Failed to obtain variable %s, bailing out\n", data);
404  ast_channel_unlock(chan);
405  return -1;
406  }
407 
408  if (ast_strlen_zero(var)) {
409  ast_log(LOG_NOTICE, "Variable %s doesn't exist - are you sure you wrote it correctly?\n", data);
410  ast_channel_unlock(chan);
411  return -1;
412  }
413 
414  if (sscanf(var, "%30d%1c", &int_value, &endchar) == 0 || endchar != 0) {
415  ast_log(LOG_NOTICE, "The content of ${%s} is not a numeric value - bailing out!\n", data);
416  ast_channel_unlock(chan);
417  return -1;
418  }
419 
420  /* now we'll actually do something useful */
421  if (!strcasecmp(cmd, "INC")) { /* Increment variable */
422  int_value++;
423  modify_orig = 1;
424  } else if (!strcasecmp(cmd, "DEC")) { /* Decrement variable */
425  int_value--;
426  modify_orig = 1;
427  }
428 
429  if (snprintf(returnvar, sizeof(returnvar), "%d", int_value) > 0) {
430  pbx_builtin_setvar_helper(chan, data, returnvar);
431  if (modify_orig) {
432  ast_copy_string(buf, returnvar, len);
433  }
434  ret = 0;
435  } else {
436  pbx_builtin_setvar_helper(chan, data, "0");
437  if (modify_orig) {
438  ast_copy_string(buf, "0", len);
439  }
440  ast_log(LOG_NOTICE, "Variable %s refused to be %sREMENTED, setting value to 0", data, cmd);
441  ret = 0;
442  }
443 
444  ast_channel_unlock(chan);
445 
446  return ret;
447 }
#define ast_channel_lock(chan)
Definition: channel.h:2466
#define LOG_WARNING
Definition: logger.h:144
#define var
Definition: ast_expr2f.c:606
const char * data
Definition: channel.h:755
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
Definition: pbx.c:10475
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
#define ast_channel_unlock(chan)
Definition: channel.h:2467
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
Definition: pbx.c:10546
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
static int load_module ( void  )
static

Definition at line 530 of file func_math.c.

References ast_custom_function_register, and AST_TEST_REGISTER.

531 {
532  int res = 0;
533 
537  AST_TEST_REGISTER(test_MATH_function);
538 
539  return res;
540 }
static struct ast_custom_function math_function
Definition: func_math.c:450
static struct ast_custom_function decrement_function
Definition: func_math.c:460
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
static struct ast_custom_function increment_function
Definition: func_math.c:455
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
static int math ( struct ast_channel chan,
const char *  cmd,
char *  parse,
char *  buf,
size_t  len 
)
static

Definition at line 138 of file func_math.c.

References ADDFUNCTION, args, 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.

140 {
141  double fnum1;
142  double fnum2;
143  double ftmp = 0;
144  char *op;
145  int iaction = -1;
146  int type_of_result = FLOAT_RESULT;
147  char *mvalue1, *mvalue2 = NULL, *mtype_of_result;
148  int negvalue1 = 0;
150  AST_APP_ARG(argv0);
151  AST_APP_ARG(argv1);
152  );
153 
154  if (ast_strlen_zero(parse)) {
155  ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
156  return -1;
157  }
158 
160 
161  if (args.argc < 1) {
162  ast_log(LOG_WARNING, "Syntax: MATH(<number1><op><number 2>[,<type_of_result>]) - missing argument!\n");
163  return -1;
164  }
165 
166  mvalue1 = args.argv0;
167 
168  if (mvalue1[0] == '-') {
169  negvalue1 = 1;
170  mvalue1++;
171  }
172 
173  if ((op = strchr(mvalue1, '*'))) {
174  iaction = MULTIPLYFUNCTION;
175  *op = '\0';
176  } else if ((op = strchr(mvalue1, '/'))) {
177  iaction = DIVIDEFUNCTION;
178  *op = '\0';
179  } else if ((op = strchr(mvalue1, '%'))) {
180  iaction = MODULUSFUNCTION;
181  *op = '\0';
182  } else if ((op = strchr(mvalue1, '^'))) {
183  iaction = POWFUNCTION;
184  *op = '\0';
185  } else if ((op = strstr(mvalue1, "AND"))) {
186  iaction = BITWISEANDFUNCTION;
187  *op = '\0';
188  op += 2;
189  } else if ((op = strstr(mvalue1, "XOR"))) {
190  iaction = BITWISEXORFUNCTION;
191  *op = '\0';
192  op += 2;
193  } else if ((op = strstr(mvalue1, "OR"))) {
194  iaction = BITWISEORFUNCTION;
195  *op = '\0';
196  ++op;
197  } else if ((op = strchr(mvalue1, '>'))) {
198  iaction = GTFUNCTION;
199  *op = '\0';
200  if (*(op + 1) == '=') {
201  iaction = GTEFUNCTION;
202  ++op;
203  } else if (*(op + 1) == '>') {
204  iaction = SHRIGHTFUNCTION;
205  ++op;
206  }
207  } else if ((op = strchr(mvalue1, '<'))) {
208  iaction = LTFUNCTION;
209  *op = '\0';
210  if (*(op + 1) == '=') {
211  iaction = LTEFUNCTION;
212  ++op;
213  } else if (*(op + 1) == '<') {
214  iaction = SHLEFTFUNCTION;
215  ++op;
216  }
217  } else if ((op = strchr(mvalue1, '='))) {
218  *op = '\0';
219  if (*(op + 1) == '=') {
220  iaction = EQFUNCTION;
221  ++op;
222  } else
223  op = NULL;
224  } else if ((op = strchr(mvalue1, '+'))) {
225  iaction = ADDFUNCTION;
226  *op = '\0';
227  } else if ((op = strchr(mvalue1, '-'))) { /* subtraction MUST always be last, in case we have a negative second number */
228  iaction = SUBTRACTFUNCTION;
229  *op = '\0';
230  }
231 
232  if (op)
233  mvalue2 = op + 1;
234 
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;
250  else {
251  ast_log(LOG_WARNING, "Unknown type of result requested '%s'.\n",
252  mtype_of_result);
253  return -1;
254  }
255  }
256 
257  if (!mvalue2) {
259  "Supply all the parameters - just this once, please\n");
260  return -1;
261  }
262 
263  if (sscanf(mvalue1, "%30lf", &fnum1) != 1) {
264  ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
265  return -1;
266  }
267 
268  if (sscanf(mvalue2, "%30lf", &fnum2) != 1) {
269  ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
270  return -1;
271  }
272 
273  if (negvalue1)
274  fnum1 = 0 - fnum1;
275 
276  switch (iaction) {
277  case ADDFUNCTION:
278  ftmp = fnum1 + fnum2;
279  break;
280  case DIVIDEFUNCTION:
281  if (fnum2 <= 0)
282  ftmp = 0; /* can't do a divide by 0 */
283  else
284  ftmp = (fnum1 / fnum2);
285  break;
286  case MULTIPLYFUNCTION:
287  ftmp = (fnum1 * fnum2);
288  break;
289  case SUBTRACTFUNCTION:
290  ftmp = (fnum1 - fnum2);
291  break;
292  case MODULUSFUNCTION:
293  {
294  int inum1 = fnum1;
295  int inum2 = fnum2;
296 
297  if (inum2 == 0) {
298  ftmp = 0;
299  } else {
300  ftmp = (inum1 % inum2);
301  }
302 
303  break;
304  }
305  case POWFUNCTION:
306  ftmp = pow(fnum1, fnum2);
307  break;
308  case SHLEFTFUNCTION:
309  {
310  int inum1 = fnum1;
311  int inum2 = fnum2;
312 
313  ftmp = (inum1 << inum2);
314  break;
315  }
316  case SHRIGHTFUNCTION:
317  {
318  int inum1 = fnum1;
319  int inum2 = fnum2;
320 
321  ftmp = (inum1 >> inum2);
322  break;
323  }
324  case BITWISEANDFUNCTION:
325  {
326  int inum1 = fnum1;
327  int inum2 = fnum2;
328  ftmp = (inum1 & inum2);
329  break;
330  }
331  case BITWISEXORFUNCTION:
332  {
333  int inum1 = fnum1;
334  int inum2 = fnum2;
335  ftmp = (inum1 ^ inum2);
336  break;
337  }
338  case BITWISEORFUNCTION:
339  {
340  int inum1 = fnum1;
341  int inum2 = fnum2;
342  ftmp = (inum1 | inum2);
343  break;
344  }
345  case GTFUNCTION:
346  ast_copy_string(buf, (fnum1 > fnum2) ? "TRUE" : "FALSE", len);
347  break;
348  case LTFUNCTION:
349  ast_copy_string(buf, (fnum1 < fnum2) ? "TRUE" : "FALSE", len);
350  break;
351  case GTEFUNCTION:
352  ast_copy_string(buf, (fnum1 >= fnum2) ? "TRUE" : "FALSE", len);
353  break;
354  case LTEFUNCTION:
355  ast_copy_string(buf, (fnum1 <= fnum2) ? "TRUE" : "FALSE", len);
356  break;
357  case EQFUNCTION:
358  ast_copy_string(buf, (fnum1 == fnum2) ? "TRUE" : "FALSE", len);
359  break;
360  default:
362  "Something happened that neither of us should be proud of %d\n",
363  iaction);
364  return -1;
365  }
366 
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);
376  }
377 
378  return 0;
379 }
#define LOG_WARNING
Definition: logger.h:144
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static struct @350 args
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define AST_APP_ARG(name)
Define an application argument.
Definition: app.h:555
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
Definition: app.h:604
static int unload_module ( void  )
static

Definition at line 518 of file func_math.c.

References ast_custom_function_unregister(), and AST_TEST_UNREGISTER.

519 {
520  int res = 0;
521 
525  AST_TEST_UNREGISTER(test_MATH_function);
526 
527  return res;
528 }
static struct ast_custom_function math_function
Definition: func_math.c:450
static struct ast_custom_function decrement_function
Definition: func_math.c:460
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static struct ast_custom_function increment_function
Definition: func_math.c:455
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .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 = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, }
static

Definition at line 542 of file func_math.c.

Definition at line 542 of file func_math.c.

struct ast_custom_function decrement_function
static
Initial value:
= {
.name = "DEC",
}
static int crement_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_math.c:381

Definition at line 460 of file func_math.c.

struct ast_custom_function increment_function
static
Initial value:
= {
.name = "INC",
}
static int crement_function_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_math.c:381

Definition at line 455 of file func_math.c.

struct ast_custom_function math_function
static
Initial value:
= {
.name = "MATH",
.read = math
}
static int math(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_math.c:138

Definition at line 450 of file func_math.c.