Wed Jan 8 2020 09:49:47

Asterisk developer's documentation


func_rand.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  * Copyright (C) 2006, Claude Patry
6  *
7  * See http://www.asterisk.org for more information about
8  * the Asterisk project. Please do not directly contact
9  * any of the maintainers of this project for assistance;
10  * the project provides a web site, mailing lists and IRC
11  * channels for your use.
12  *
13  * This program is free software, distributed under the terms of
14  * the GNU General Public License Version 2. See the LICENSE file
15  * at the top of the source tree.
16  */
17 
18 /*! \file
19  *
20  * \brief Generate Random Number
21  *
22  * \author Claude Patry <cpatry@gmail.com>
23  * \author Tilghman Lesher ( http://asterisk.drunkcoder.com/ )
24  * \ingroup functions
25  */
26 
27 /*** MODULEINFO
28  <support_level>core</support_level>
29  ***/
30 
31 #include "asterisk.h"
32 
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
34 
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 
41 /*** DOCUMENTATION
42  <function name="RAND" language="en_US">
43  <synopsis>
44  Choose a random number in a range.
45  </synopsis>
46  <syntax>
47  <parameter name="min" />
48  <parameter name="max" />
49  </syntax>
50  <description>
51  <para>Choose a random number between <replaceable>min</replaceable> and <replaceable>max</replaceable>.
52  <replaceable>min</replaceable> defaults to <literal>0</literal>, if not specified, while <replaceable>max</replaceable> defaults
53  to <literal>RAND_MAX</literal> (2147483647 on many systems).</para>
54  <para>Example: Set(junky=${RAND(1,8)});
55  Sets junky to a random number between 1 and 8, inclusive.</para>
56  </description>
57  </function>
58  ***/
59 static int acf_rand_exec(struct ast_channel *chan, const char *cmd,
60  char *parse, char *buffer, size_t buflen)
61 {
62  int min_int, response_int, max_int;
64  AST_APP_ARG(min);
65  AST_APP_ARG(max);
66  );
67 
69 
70  if (ast_strlen_zero(args.min) || sscanf(args.min, "%30d", &min_int) != 1)
71  min_int = 0;
72 
73  if (ast_strlen_zero(args.max) || sscanf(args.max, "%30d", &max_int) != 1)
74  max_int = RAND_MAX;
75 
76  if (max_int < min_int) {
77  int tmp = max_int;
78 
79  max_int = min_int;
80  min_int = tmp;
81  ast_debug(1, "max<min\n");
82  }
83 
84  response_int = min_int + (ast_random() % (max_int - min_int + 1));
85  ast_debug(1, "%d was the lucky number in range [%d,%d]\n", response_int, min_int, max_int);
86  snprintf(buffer, buflen, "%d", response_int);
87 
88  return 0;
89 }
90 
91 static struct ast_custom_function acf_rand = {
92  .name = "RAND",
93  .read = acf_rand_exec,
94  .read_max = 12,
95 };
96 
97 static int unload_module(void)
98 {
100 
101  return 0;
102 }
103 
104 static int load_module(void)
105 {
106  return ast_custom_function_register(&acf_rand);
107 }
108 
109 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Random number dialplan function");
Main Channel structure associated with a channel.
Definition: channel.h:742
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:396
Asterisk main include file. File version handling, generic pbx functions.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
Definition: app.h:572
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
Utility functions.
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static int unload_module(void)
Definition: func_rand.c:97
General Asterisk PBX channel definitions.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
Data structure associated with a custom dialplan function.
Definition: pbx.h:95
static int acf_rand_exec(struct ast_channel *chan, const char *cmd, char *parse, char *buffer, size_t buflen)
Definition: func_rand.c:59
long int ast_random(void)
Definition: utils.c:1640
Core PBX routines and definitions.
static struct @350 args
static void parse(struct mgcp_request *req)
Definition: chan_mgcp.c:1858
static struct ast_custom_function acf_rand
Definition: func_rand.c:91
static int load_module(void)
Definition: func_rand.c:104
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
const char * name
Definition: pbx.h:96
#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
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180