Mon Mar 20 08:20:15 2006

Asterisk developer's documentation


Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

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"

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.
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

char * app_math = "Math"
 LOCAL_USER_DECL
char * math_descrip
char * math_synopsis = "Performs Mathematical Functions"
 STANDARD_LOCAL_USER
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 74 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define DIVIDEFUNCTION   1
 

Definition at line 75 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define EQFUNCTION   9
 

Definition at line 84 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define GTEFUNCTION   7
 

Definition at line 82 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define GTFUNCTION   5
 

Definition at line 80 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define LTEFUNCTION   8
 

Definition at line 83 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define LTFUNCTION   6
 

Definition at line 81 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define MODULUSFUNCTION   4
 

Definition at line 78 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define MULTIPLYFUNCTION   2
 

Definition at line 76 of file app_math.c.

Referenced by builtin_function_math(), and math_exec().

#define SUBTRACTFUNCTION   3
 

Definition at line 77 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 280 of file app_math.c.

00281 {
00282    return tdesc;
00283 }

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 292 of file app_math.c.

00293 {
00294    return ASTERISK_GPL_KEY;
00295 }

int load_module void   ) 
 

Initialize the module.

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.

Definition at line 275 of file app_math.c.

References app_math, ast_register_application(), math_descrip, math_exec(), and math_synopsis.

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

int math_exec struct ast_channel chan,
void *  data
[static]
 

Definition at line 90 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().

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

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 264 of file app_math.c.

References app_math, and ast_unregister_application().

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

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 285 of file app_math.c.

References STANDARD_USECOUNT.

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


Variable Documentation

char* app_math = "Math" [static]
 

Definition at line 63 of file app_math.c.

Referenced by load_module(), and unload_module().

LOCAL_USER_DECL
 

Definition at line 88 of file app_math.c.

char* math_descrip [static]
 

Definition at line 67 of file app_math.c.

Referenced by load_module().

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

Definition at line 65 of file app_math.c.

Referenced by load_module().

STANDARD_LOCAL_USER
 

Definition at line 86 of file app_math.c.

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

Definition at line 61 of file app_math.c.


Generated on Mon Mar 20 08:20:15 2006 for Asterisk - the Open Source PBX by  doxygen 1.3.9.1