Mon May 14 04:48:21 2007

Asterisk developer's documentation


func_rand.c File Reference

Generate Random Number. More...

#include "asterisk.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"

Include dependency graph for func_rand.c:

Go to the source code of this file.

Functions

static int acf_rand_exec (struct ast_channel *chan, char *cmd, char *parse, char *buffer, size_t buflen)
 AST_MODULE_INFO_STANDARD (ASTERISK_GPL_KEY,"Random number dialplan function")
static int load_module (void)
static int unload_module (void)

Variables

static struct ast_custom_function acf_rand


Detailed Description

Generate Random Number.

Author:
Claude Patry <cpatry@gmail.com>

Tilghman Lesher ( http://asterisk.drunkcoder.com/ )

Definition in file func_rand.c.


Function Documentation

static int acf_rand_exec ( struct ast_channel chan,
char *  cmd,
char *  parse,
char *  buffer,
size_t  buflen 
) [static]

Definition at line 43 of file func_rand.c.

References AST_APP_ARG, AST_DECLARE_APP_ARGS, ast_log(), ast_module_user_add, ast_module_user_remove, ast_random(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_module_user::chan, and LOG_DEBUG.

00045 {
00046    struct ast_module_user *u;
00047    int min_int, response_int, max_int;
00048    AST_DECLARE_APP_ARGS(args,
00049               AST_APP_ARG(min);
00050               AST_APP_ARG(max);
00051    );
00052 
00053    u = ast_module_user_add(chan);
00054 
00055    AST_STANDARD_APP_ARGS(args, parse);
00056 
00057    if (ast_strlen_zero(args.min) || sscanf(args.min, "%d", &min_int) != 1)
00058       min_int = 0;
00059 
00060    if (ast_strlen_zero(args.max) || sscanf(args.max, "%d", &max_int) != 1)
00061       max_int = RAND_MAX;
00062 
00063    if (max_int < min_int) {
00064       int tmp = max_int;
00065 
00066       max_int = min_int;
00067       min_int = tmp;
00068       ast_log(LOG_DEBUG, "max<min\n");
00069    }
00070 
00071    response_int = min_int + (ast_random() % (max_int - min_int + 1));
00072    ast_log(LOG_DEBUG, "%d was the lucky number in range [%d,%d]\n",
00073       response_int, min_int, max_int);
00074    snprintf(buffer, buflen, "%d", response_int);
00075 
00076    ast_module_user_remove(u);
00077 
00078    return 0;
00079 }

AST_MODULE_INFO_STANDARD ( ASTERISK_GPL_KEY  ,
"Random number dialplan function"   
)

static int load_module ( void   )  [static]

Definition at line 100 of file func_rand.c.

References acf_rand, and ast_custom_function_register().

00101 {
00102    return ast_custom_function_register(&acf_rand);
00103 }

static int unload_module ( void   )  [static]

Definition at line 93 of file func_rand.c.

References acf_rand, and ast_custom_function_unregister().

00094 {
00095    ast_custom_function_unregister(&acf_rand);
00096 
00097    return 0;
00098 }


Variable Documentation

struct ast_custom_function acf_rand [static]

Definition at line 81 of file func_rand.c.

Referenced by load_module(), and unload_module().


Generated on Mon May 14 04:48:22 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1