Wed Jan 8 2020 09:50:12

Asterisk developer's documentation


func_devstate.c File Reference

Manually controlled blinky lights. More...

#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/linkedlists.h"
#include "asterisk/devicestate.h"
#include "asterisk/cli.h"
#include "asterisk/astdb.h"
#include "asterisk/app.h"

Go to the source code of this file.

Enumerations

enum  { HINT_OPT_NAME = (1 << 0) }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static enum ast_device_state custom_devstate_callback (const char *data)
 
static int devstate_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int devstate_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
 
static char * handle_cli_devstate_change (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static char * handle_cli_devstate_list (struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 
static int hint_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Gets or sets a device state in the dialplan" , .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_DEVSTATE_PROVIDER, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static const char astdb_family [] = "CustomDevstate"
 
static struct ast_cli_entry cli_funcdevstate []
 
static struct ast_custom_function devstate_function
 
static struct ast_custom_function hint_function
 
static struct ast_app_option hint_options [128] = { [ 'n' ] = { .flag = HINT_OPT_NAME }, }
 

Detailed Description

Manually controlled blinky lights.

Author
Russell Bryant russe.nosp@m.ll@d.nosp@m.igium.nosp@m..com
Todo:
Delete the entry from AstDB when set to nothing like Set(DEVICE_STATE(Custom:lamp1)=)
Note
Props go out to Ahrimanes in #asterisk for requesting this at 4:30 AM when I couldn't sleep. :)

Definition in file func_devstate.c.

Enumeration Type Documentation

anonymous enum
Enumerator
HINT_OPT_NAME 

Definition at line 140 of file func_devstate.c.

140  {
141  HINT_OPT_NAME = (1 << 0),
142 };

Function Documentation

static void __reg_module ( void  )
static

Definition at line 361 of file func_devstate.c.

static void __unreg_module ( void  )
static

Definition at line 361 of file func_devstate.c.

static enum ast_device_state custom_devstate_callback ( const char *  data)
static

Definition at line 186 of file func_devstate.c.

References ast_db_get(), and ast_devstate_val().

Referenced by load_module().

187 {
188  char buf[256] = "";
189 
190  /* Ignore check_return warning from Coverity fow ast_db_get below */
191  ast_db_get(astdb_family, data, buf, sizeof(buf));
192 
193  return ast_devstate_val(buf);
194 }
int ast_db_get(const char *family, const char *key, char *out, int outlen)
Get key value specified by family/key.
Definition: db.c:348
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:244
static const char astdb_family[]
static int devstate_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 104 of file func_devstate.c.

References ast_copy_string(), and ast_devstate_str().

105 {
107 
108  return 0;
109 }
ast_device_state
Device States.
Definition: devicestate.h:51
const char * ast_devstate_str(enum ast_device_state devstate) attribute_pure
Convert device state to text string that is easier to parse.
Definition: devicestate.c:239
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
static int devstate_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 111 of file func_devstate.c.

References ast_db_put(), AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_devstate_val(), ast_log(), ast_strlen_zero(), len(), LOG_ERROR, and LOG_WARNING.

112 {
113  size_t len = strlen("Custom:");
114  enum ast_device_state state_val;
115 
116  if (strncasecmp(data, "Custom:", len)) {
117  ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
118  return -1;
119  }
120  data += len;
121  if (ast_strlen_zero(data)) {
122  ast_log(LOG_WARNING, "DEVICE_STATE function called with no custom device name!\n");
123  return -1;
124  }
125 
126  state_val = ast_devstate_val(value);
127 
128  if (state_val == AST_DEVICE_UNKNOWN) {
129  ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value);
130  return -1;
131  }
132 
133  ast_db_put(astdb_family, data, value);
134 
135  ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", data);
136 
137  return 0;
138 }
ast_device_state
Device States.
Definition: devicestate.h:51
#define LOG_WARNING
Definition: logger.h:144
int value
Definition: syslog.c:39
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:516
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:244
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
#define LOG_ERROR
Definition: logger.h:155
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 const char astdb_family[]
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: db.c:260
static char* handle_cli_devstate_change ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 240 of file func_devstate.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli_args::argv, ast_cli(), ast_cli_complete(), ast_db_put(), AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_devstate_val(), ast_strlen_zero(), CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, len(), ast_cli_args::n, ast_cli_args::pos, state, ast_cli_entry::usage, and ast_cli_args::word.

241 {
242  size_t len;
243  const char *dev, *state;
244  enum ast_device_state state_val;
245 
246  switch (cmd) {
247  case CLI_INIT:
248  e->command = "devstate change";
249  e->usage =
250  "Usage: devstate change <device> <state>\n"
251  " Change a custom device to a new state.\n"
252  " The possible values for the state are:\n"
253  "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
254  "RINGINUSE | ONHOLD\n"
255  "\n"
256  "Examples:\n"
257  " devstate change Custom:mystate1 INUSE\n"
258  " devstate change Custom:mystate1 NOT_INUSE\n"
259  " \n";
260  return NULL;
261  case CLI_GENERATE:
262  {
263  static const char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
264  "UNAVAILABLE", "RINGING", "RINGINUSE", "ONHOLD", NULL };
265 
266  if (a->pos == e->args + 1)
267  return ast_cli_complete(a->word, cmds, a->n);
268 
269  return NULL;
270  }
271  }
272 
273  if (a->argc != e->args + 2)
274  return CLI_SHOWUSAGE;
275 
276  len = strlen("Custom:");
277  dev = a->argv[e->args];
278  state = a->argv[e->args + 1];
279 
280  if (strncasecmp(dev, "Custom:", len)) {
281  ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
282  return CLI_FAILURE;
283  }
284 
285  dev += len;
286  if (ast_strlen_zero(dev))
287  return CLI_SHOWUSAGE;
288 
289  state_val = ast_devstate_val(state);
290 
291  if (state_val == AST_DEVICE_UNKNOWN)
292  return CLI_SHOWUSAGE;
293 
294  ast_cli(a->fd, "Changing %s to %s\n", dev, state);
295 
296  ast_db_put(astdb_family, dev, state);
297 
298  ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", dev);
299 
300  return CLI_SUCCESS;
301 }
enum sip_cc_notify_state state
Definition: chan_sip.c:842
ast_device_state
Device States.
Definition: devicestate.h:51
const int argc
Definition: cli.h:154
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
int args
This gets set in ast_cli_register()
Definition: cli.h:179
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
Definition: cli.c:1535
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:516
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:244
const int fd
Definition: cli.h:153
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
const int n
Definition: cli.h:159
const char *const * argv
Definition: cli.h:155
#define CLI_SHOWUSAGE
Definition: cli.h:44
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define CLI_FAILURE
Definition: cli.h:45
char * command
Definition: cli.h:180
const char * word
Definition: cli.h:157
static const char astdb_family[]
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
const int pos
Definition: cli.h:158
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: db.c:260
static char* handle_cli_devstate_list ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 196 of file func_devstate.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli(), ast_db_freetree(), ast_db_gettree(), CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_db_entry::data, ast_cli_args::fd, ast_db_entry::key, ast_db_entry::next, and ast_cli_entry::usage.

197 {
198  struct ast_db_entry *db_entry, *db_tree;
199 
200  switch (cmd) {
201  case CLI_INIT:
202  e->command = "devstate list";
203  e->usage =
204  "Usage: devstate list\n"
205  " List all custom device states that have been set by using\n"
206  " the DEVICE_STATE dialplan function.\n";
207  return NULL;
208  case CLI_GENERATE:
209  return NULL;
210  }
211 
212  if (a->argc != e->args)
213  return CLI_SHOWUSAGE;
214 
215  ast_cli(a->fd, "\n"
216  "---------------------------------------------------------------------\n"
217  "--- Custom Device States --------------------------------------------\n"
218  "---------------------------------------------------------------------\n"
219  "---\n");
220 
221  db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
222  for (; db_entry; db_entry = db_entry->next) {
223  const char *dev_name = strrchr(db_entry->key, '/') + 1;
224  if (dev_name <= (const char *) 1)
225  continue;
226  ast_cli(a->fd, "--- Name: 'Custom:%s' State: '%s'\n"
227  "---\n", dev_name, db_entry->data);
228  }
229  ast_db_freetree(db_tree);
230  db_tree = NULL;
231 
232  ast_cli(a->fd,
233  "---------------------------------------------------------------------\n"
234  "---------------------------------------------------------------------\n"
235  "\n");
236 
237  return CLI_SUCCESS;
238 }
const int argc
Definition: cli.h:154
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: db.c:656
Definition: cli.h:146
void ast_cli(int fd, const char *fmt,...)
Definition: cli.c:105
int args
This gets set in ast_cli_register()
Definition: cli.h:179
struct ast_db_entry * next
Definition: astdb.h:31
const int fd
Definition: cli.h:153
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree If family is specified, only those keys will be returned...
Definition: db.c:631
#define CLI_SHOWUSAGE
Definition: cli.h:44
Definition: astdb.h:30
char data[0]
Definition: astdb.h:33
char * command
Definition: cli.h:180
static const char astdb_family[]
const char * usage
Definition: cli.h:171
#define CLI_SUCCESS
Definition: cli.h:43
char * key
Definition: astdb.h:32
static int hint_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 148 of file func_devstate.c.

References args, AST_APP_ARG, ast_app_parse_options(), AST_DECLARE_APP_ARGS, ast_get_hint(), ast_log(), AST_STANDARD_APP_ARGS, ast_strlen_zero(), ast_test_flag, context, exten, HINT_OPT_NAME, hint_options, LOG_WARNING, and strsep().

149 {
150  char *exten, *context;
152  AST_APP_ARG(exten);
153  AST_APP_ARG(options);
154  );
155  struct ast_flags opts = { 0, };
156  int res;
157 
158  if (ast_strlen_zero(data)) {
159  ast_log(LOG_WARNING, "The HINT function requires an extension\n");
160  return -1;
161  }
162 
164 
165  if (ast_strlen_zero(args.exten)) {
166  ast_log(LOG_WARNING, "The HINT function requires an extension\n");
167  return -1;
168  }
169 
170  context = exten = args.exten;
171  strsep(&context, "@");
172  if (ast_strlen_zero(context))
173  context = "default";
174 
175  if (!ast_strlen_zero(args.options))
176  ast_app_parse_options(hint_options, &opts, NULL, args.options);
177 
178  if (ast_test_flag(&opts, HINT_OPT_NAME))
179  res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
180  else
181  res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);
182 
183  return !res; /* ast_get_hint returns non-zero on success */
184 }
int ast_get_hint(char *hint, int hintsize, char *name, int namesize, struct ast_channel *c, const char *context, const char *exten)
If an extension hint exists, return non-zero.
Definition: pbx.c:5362
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:109
char * strsep(char **str, const char *delims)
static struct ast_app_option hint_options[128]
#define ast_test_flag(p, flag)
Definition: utils.h:63
#define LOG_WARNING
Definition: logger.h:144
int ast_app_parse_options(const struct ast_app_option *options, struct ast_flags *flags, char **args, char *optstr)
Parses a string containing application options and sets flags/arguments.
Definition: app.c:2101
#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
Structure used to handle boolean flags.
Definition: utils.h:200
#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 char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:107
static int load_module ( void  )
static

Definition at line 331 of file func_devstate.c.

References ARRAY_LEN, ast_cli_register_multiple(), ast_custom_function_register, ast_db_freetree(), ast_db_gettree(), AST_DEVSTATE_CACHABLE, ast_devstate_changed(), ast_devstate_prov_add(), ast_devstate_val(), custom_devstate_callback(), ast_db_entry::data, ast_db_entry::key, and ast_db_entry::next.

332 {
333  int res = 0;
334  struct ast_db_entry *db_entry, *db_tree;
335 
336  /* Populate the device state cache on the system with all of the currently
337  * known custom device states. */
338  db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
339  for (; db_entry; db_entry = db_entry->next) {
340  const char *dev_name = strrchr(db_entry->key, '/') + 1;
341  if (dev_name <= (const char *) 1)
342  continue;
344  AST_DEVSTATE_CACHABLE, "Custom:%s\n", dev_name);
345  }
346  ast_db_freetree(db_tree);
347  db_tree = NULL;
348 
353 
354  return res;
355 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: db.c:656
int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
Add device state provider.
Definition: devicestate.c:371
static struct ast_custom_function hint_function
struct ast_db_entry * next
Definition: astdb.h:31
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:516
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:244
static enum ast_device_state custom_devstate_callback(const char *data)
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree If family is specified, only those keys will be returned...
Definition: db.c:631
static struct ast_custom_function devstate_function
Definition: astdb.h:30
char data[0]
Definition: astdb.h:33
static const char astdb_family[]
int ast_cli_register_multiple(struct ast_cli_entry *e, int len)
Register multiple commands.
Definition: cli.c:2167
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1164
static struct ast_cli_entry cli_funcdevstate[]
char * key
Definition: astdb.h:32
static int unload_module ( void  )
static

Definition at line 319 of file func_devstate.c.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_custom_function_unregister(), and ast_devstate_prov_del().

320 {
321  int res = 0;
322 
325  res |= ast_devstate_prov_del("Custom");
327 
328  return res;
329 }
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
Unregister multiple commands.
Definition: cli.c:2177
int ast_devstate_prov_del(const char *label)
Remove device state provider.
Definition: devicestate.c:389
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
Definition: pbx.c:3814
static struct ast_custom_function hint_function
static struct ast_custom_function devstate_function
static struct ast_cli_entry cli_funcdevstate[]

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Gets or sets a device state in the dialplan" , .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_DEVSTATE_PROVIDER, }
static

Definition at line 361 of file func_devstate.c.

Definition at line 361 of file func_devstate.c.

const char astdb_family[] = "CustomDevstate"
static

Definition at line 102 of file func_devstate.c.

struct ast_cli_entry cli_funcdevstate[]
static
Initial value:
= {
AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states"),
AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"),
}
#define AST_CLI_DEFINE(fn, txt,...)
Definition: cli.h:191
static char * handle_cli_devstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
static char * handle_cli_devstate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)

Definition at line 303 of file func_devstate.c.

struct ast_custom_function devstate_function
static
Initial value:
= {
.name = "DEVICE_STATE",
.read = devstate_read,
.write = devstate_write,
}
static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)

Definition at line 308 of file func_devstate.c.

struct ast_custom_function hint_function
static
Initial value:
= {
.name = "HINT",
.read = hint_read,
}
static int hint_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)

Definition at line 314 of file func_devstate.c.

struct ast_app_option hint_options[128] = { [ 'n' ] = { .flag = HINT_OPT_NAME }, }
static

Definition at line 146 of file func_devstate.c.

Referenced by hint_read().