Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:48:07 2009

## Asterisk developer's documentation

# app_math.c File Reference

A simple math application. More...

`#include <stdlib.h>`
`#include <errno.h>`
`#include <unistd.h>`
`#include <string.h>`
`#include <stdio.h>`
`#include <sys/time.h>`
`#include <sys/stat.h>`
`#include <sys/types.h>`
`#include <time.h>`
`#include <dirent.h>`
`#include <ctype.h>`
`#include <sys/file.h>`
`#include "asterisk.h"`
`#include "asterisk/lock.h"`
`#include "asterisk/file.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/channel.h"`
`#include "asterisk/pbx.h"`
`#include "asterisk/options.h"`
`#include "asterisk/config.h"`
`#include "asterisk/say.h"`
`#include "asterisk/module.h"`
`#include "asterisk/app.h"`
`#include "asterisk/manager.h"`
`#include "asterisk/localtime.h"`
`#include "asterisk/cli.h"`
`#include "asterisk/utils.h"`
`#include "asterisk/translate.h"`

Include dependency graph for app_math.c:

Go to the source code of this file.

## Defines

#define | ADDFUNCTION 0
#define | DIVIDEFUNCTION 1
#define | EQFUNCTION 9
#define | GTEFUNCTION 7
#define | GTFUNCTION 5
#define | LTEFUNCTION 8
#define | LTFUNCTION 6
#define | MODULUSFUNCTION 4
#define | MULTIPLYFUNCTION 2
#define | SUBTRACTFUNCTION 3

## Functions

char * | description (void)
| Provides a description of the module.
char * | key ()
| Returns the ASTERISK_GPL_KEY.
int | load_module (void)
| Initialize the module.
static int | math_exec (struct ast_channel *chan, void *data)
int | unload_module (void)
| Cleanup all module structures, sockets, etc.
int | usecount (void)
| Provides a usecount.

## Variables

static char * | app_math = "Math"
| LOCAL_USER_DECL
static char * | math_descrip
static char * | math_synopsis = "Performs Mathematical Functions"
| STANDARD_LOCAL_USER
static char * | tdesc = "Basic Math Functions"

## Detailed Description

A simple math application.

Definition in file app_math.c.

## Define Documentation

#define ADDFUNCTION 0

Definition at line 75 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define DIVIDEFUNCTION 1

Definition at line 76 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define EQFUNCTION 9

Definition at line 85 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define GTEFUNCTION 7

Definition at line 83 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define GTFUNCTION 5

Definition at line 81 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define LTEFUNCTION 8

Definition at line 84 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define LTFUNCTION 6

Definition at line 82 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define MODULUSFUNCTION 4

Definition at line 79 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define MULTIPLYFUNCTION 2

Definition at line 77 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define SUBTRACTFUNCTION 3

Definition at line 78 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

## Function Documentation

char* description | ( | void |  | ) |

Provides a description of the module.

Returns:
  a short description of your module

Definition at line 281 of file app_math.c.

    00282 {
    00283    return tdesc;
    00284 }

char* key | ( | void |  | ) |

Returns the ASTERISK_GPL_KEY.

This returns the ASTERISK_GPL_KEY, signifiying that you agree to the terms of the GPL stated in the ASTERISK_GPL_KEY. Your module will not load if it does not return the EXACT message:

     char *key(void) {
             return ASTERISK_GPL_KEY;
     }

Returns:
  ASTERISK_GPL_KEY

Definition at line 293 of file app_math.c.

References ASTERISK_GPL_KEY.

    00294 {
    00295    return ASTERISK_GPL_KEY;
    00296 }

int load_module | ( | void |  | ) |

Initialize the module.

This function is called at module load time. Put all code in here that needs to set up your module's hardware, software, registrations, etc.

Returns:
  This function should return 0 on success and non-zero on failure. If the module is not loaded successfully, Asterisk will call its unload_module() function.

Initialize the Agents module. This function is being called by Asterisk when loading the module. Among other thing it registers applications, cli commands and reads the cofiguration file.

Returns:
  int Always 0.

TE STUFF END

Definition at line 276 of file app_math.c.

References ast_register_application(), and math_exec().

    00277 {
    00278    return ast_register_application(app_math, math_exec, math_synopsis, math_descrip);
    00279 }

static int math_exec | ( | struct ast_channel * | chan,
|  | void * | data |
| ) |  |  | `[static]`

Definition at line 91 of file app_math.c.

References ADDFUNCTION, ast_log(), ast_strdupa, ast_strlen_zero(), DIVIDEFUNCTION, EQFUNCTION, GTEFUNCTION, GTFUNCTION, LOCAL_USER_ADD, LOCAL_USER_REMOVE, LOG_ERROR, LOG_WARNING, LTEFUNCTION, LTFUNCTION, MODULUSFUNCTION, MULTIPLYFUNCTION, pbx_builtin_setvar_helper(), s, strsep(), and SUBTRACTFUNCTION.

Referenced by load_module().

    00092 {
    00093    float fnum1;
    00094    float fnum2;
    00095    float ftmp = 0;
    00096    char *op;
    00097    int iaction=-1;
    00098    static int deprecation_warning = 0;
    00099
    00100    /* dunno, big calulations :D */
    00101    char user_result[30];
    00102
    00103    char *s;
    00104    char *mvar, *mvalue1, *mvalue2=NULL;
    00105
    00106    struct localuser *u;
    00107
    00108    if (!deprecation_warning) {
    00109       ast_log(LOG_WARNING, "Math() is deprecated, please use Set(var=${MATH(...)} instead.\n");
    00110       deprecation_warning = 1;
    00111    }
    00112
    00113    if (ast_strlen_zero(data)) {
    00114       ast_log(LOG_WARNING, "No parameters passed. !\n");
    00115       return -1;
    00116    }
    00117
    00118    LOCAL_USER_ADD(u);
    00119
    00120    s = ast_strdupa(data);
    00121    if (!s) {
    00122       ast_log(LOG_ERROR, "Out of memory\n");
    00123       LOCAL_USER_REMOVE(u);
    00124       return -1;
    00125    }
    00126
    00127    mvar = strsep(&s, "|");
    00128    mvalue1 = strsep(&s, "|");
    00129
    00130    if ((op = strchr(mvalue1, '+'))) {
    00131       iaction = ADDFUNCTION;
    00132       *op = '\0';
    00133    } else if ((op = strchr(mvalue1, '-'))) {
    00134       iaction = SUBTRACTFUNCTION;
    00135       *op = '\0';
    00136    } else if ((op = strchr(mvalue1, '*'))) {
    00137       iaction = MULTIPLYFUNCTION;
    00138       *op = '\0';
    00139    } else if ((op = strchr(mvalue1, '/'))) {
    00140       iaction = DIVIDEFUNCTION;
    00141       *op = '\0';
    00142    } else if ((op = strchr(mvalue1, '>'))) {
    00143       iaction = GTFUNCTION;
    00144       *op = '\0';
    00145       if (*(op+1) == '=') {
    00146          op++;
    00147          *op = '\0';
    00148          iaction = GTEFUNCTION;
    00149       }
    00150    } else if ((op = strchr(mvalue1, '<'))) {
    00151       iaction = LTFUNCTION;
    00152       *op = '\0';
    00153       if (*(op+1) == '=') {
    00154          op++;
    00155          *op = '\0';
    00156          iaction = LTEFUNCTION;
    00157       }
    00158    } else if ((op = strchr(mvalue1, '='))) {
    00159       iaction = GTFUNCTION;
    00160       *op = '\0';
    00161       if (*(op+1) == '=') {
    00162          op++;
    00163          *op = '\0';
    00164          iaction = EQFUNCTION;
    00165       } else
    00166          op = NULL;
    00167    }
    00168
    00169    if (op)
    00170       mvalue2 = op + 1;
    00171
    00172    if (!mvar || !mvalue1 || !mvalue2) {
    00173       ast_log(LOG_WARNING, "Supply all the parameters - just this once, please\n");
    00174       LOCAL_USER_REMOVE(u);
    00175       return -1;
    00176    }
    00177
    00178    if (!strcmp(mvar,"")) {
    00179       ast_log(LOG_WARNING, "No return variable set.\n");
    00180       LOCAL_USER_REMOVE(u);
    00181       return -1;
    00182    }
    00183
    00184    if (sscanf(mvalue1, "%30f", &fnum1) != 1) {
    00185       ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue1);
    00186       LOCAL_USER_REMOVE(u);
    00187       return -1;
    00188    }
    00189
    00190    if (sscanf(mvalue2, "%30f", &fnum2) != 1) {
    00191       ast_log(LOG_WARNING, "'%s' is not a valid number\n", mvalue2);
    00192       LOCAL_USER_REMOVE(u);
    00193       return -1;
    00194    }
    00195
    00196    switch (iaction) {
    00197    case ADDFUNCTION :
    00198       ftmp = fnum1 + fnum2;
    00199       break;
    00200    case DIVIDEFUNCTION :
    00201       if (fnum2 <=0)
    00202          ftmp = 0; /* can't do a divide by 0 */
    00203       else
    00204          ftmp = (fnum1 / fnum2);
    00205       break;
    00206    case MULTIPLYFUNCTION :
    00207       ftmp = (fnum1 * fnum2);
    00208       break;
    00209    case SUBTRACTFUNCTION :
    00210       ftmp = (fnum1 - fnum2);
    00211       break;
    00212    case MODULUSFUNCTION : {
    00213       int inum1 = fnum1;
    00214       int inum2 = fnum2;
    00215
    00216       ftmp = (inum1 % inum2);
    00217
    00218       break;
    00219       }
    00220    case GTFUNCTION :
    00221       if (fnum1 > fnum2)
    00222          strcpy(user_result, "TRUE");
    00223       else
    00224          strcpy(user_result, "FALSE");
    00225       break;
    00226    case LTFUNCTION :
    00227       if (fnum1 < fnum2)
    00228          strcpy(user_result, "TRUE");
    00229       else
    00230          strcpy(user_result, "FALSE");
    00231       break;
    00232    case GTEFUNCTION :
    00233       if (fnum1 >= fnum2)
    00234          strcpy(user_result, "TRUE");
    00235       else
    00236          strcpy(user_result, "FALSE");
    00237       break;
    00238    case LTEFUNCTION :
    00239       if (fnum1 <= fnum2)
    00240          strcpy(user_result, "TRUE");
    00241       else
    00242          strcpy(user_result, "FALSE");
    00243       break;
    00244    case EQFUNCTION :
    00245       if (fnum1 == fnum2)
    00246          strcpy(user_result, "TRUE");
    00247       else
    00248          strcpy(user_result, "FALSE");
    00249       break;
    00250    default :
    00251       ast_log(LOG_WARNING, "Something happened that neither of us should be proud of %d\n", iaction);
    00252       LOCAL_USER_REMOVE(u);
    00253       return -1;
    00254    }
    00255
    00256    if (iaction < GTFUNCTION || iaction > EQFUNCTION)
    00257       snprintf(user_result,sizeof(user_result),"%f",ftmp);
    00258
    00259    pbx_builtin_setvar_helper(chan, mvar, user_result);
    00260
    00261    LOCAL_USER_REMOVE(u);
    00262    return 0;
    00263 }

int unload_module | ( | void |  | ) |

Cleanup all module structures, sockets, etc.

This is called at exit. Any registrations and memory allocations need to be unregistered and free'd here. Nothing else will do these for you (until exit).

Returns:
  Zero on success, or non-zero on error.

Definition at line 265 of file app_math.c.

References ast_unregister_application(), and STANDARD_HANGUP_LOCALUSERS.

    00266 {
    00267    int res;
    00268
    00269    res = ast_unregister_application(app_math);
    00270
    00271    STANDARD_HANGUP_LOCALUSERS;
    00272
    00273    return res;
    00274 }

int usecount | ( | void |  | ) |

Provides a usecount.

This function will be called by various parts of asterisk. Basically, all it has to do is to return a usecount when called. You will need to maintain your usecount within the module somewhere. The usecount should be how many channels provided by this module are in use.

Returns:
  The module's usecount.

Definition at line 286 of file app_math.c.

References STANDARD_USECOUNT.

    00287 {
    00288    int res;
    00289    STANDARD_USECOUNT(res);
    00290    return res;
    00291 }

## Variable Documentation

char* app_math = "Math"`[static]`

Definition at line 63 of file app_math.c.

LOCAL_USER_DECL

Definition at line 89 of file app_math.c.

char* math_descrip`[static]`

Definition at line 67 of file app_math.c.

char* math_synopsis = "Performs Mathematical Functions"`[static]`

Definition at line 65 of file app_math.c.

STANDARD_LOCAL_USER

Definition at line 87 of file app_math.c.

char* tdesc = "Basic Math Functions"`[static]`

Definition at line 61 of file app_math.c.