Asterisk - The Open Source Telephony Project  18.5.0
Enumerations | Functions | Variables
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"
Include dependency graph for func_devstate.c:

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)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PROVIDER, }
 
static const 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 const 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

anonymous enum
Enumerator
HINT_OPT_NAME 

Definition at line 138 of file func_devstate.c.

138  {
139  HINT_OPT_NAME = (1 << 0),
140 };

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 360 of file func_devstate.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 360 of file func_devstate.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 360 of file func_devstate.c.

◆ custom_devstate_callback()

static enum ast_device_state custom_devstate_callback ( const char *  data)
static

Definition at line 184 of file func_devstate.c.

References ast_db_get(), ast_devstate_val(), astdb_family, and buf.

Referenced by load_module().

185 {
186  char buf[256] = "";
187 
188  /* Ignore check_return warning from Coverity fow ast_db_get below */
189  ast_db_get(astdb_family, data, buf, sizeof(buf));
190 
191  return ast_devstate_val(buf);
192 }
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:260
static const char astdb_family[]
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: main/db.c:412

◆ devstate_read()

static int devstate_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 102 of file func_devstate.c.

References ast_copy_string(), and ast_devstate_str().

103 {
105 
106  return 0;
107 }
ast_device_state
Device States.
Definition: devicestate.h:52
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
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:255
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:401

◆ devstate_write()

static int devstate_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
)
static

Definition at line 109 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, astdb_family, len(), LOG_ERROR, and LOG_WARNING.

110 {
111  size_t len = strlen("Custom:");
112  enum ast_device_state state_val;
113 
114  if (strncasecmp(data, "Custom:", len)) {
115  ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
116  return -1;
117  }
118  data += len;
119  if (ast_strlen_zero(data)) {
120  ast_log(LOG_WARNING, "DEVICE_STATE function called with no custom device name!\n");
121  return -1;
122  }
123 
124  state_val = ast_devstate_val(value);
125 
126  if (state_val == AST_DEVICE_UNKNOWN) {
127  ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value);
128  return -1;
129  }
130 
131  ast_db_put(astdb_family, data, value);
132 
133  ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", data);
134 
135  return 0;
136 }
ast_device_state
Device States.
Definition: devicestate.h:52
#define LOG_WARNING
Definition: logger.h:274
int value
Definition: syslog.c:37
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
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:510
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:260
#define LOG_ERROR
Definition: logger.h:285
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
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: main/db.c:327

◆ handle_cli_devstate_change()

static char* handle_cli_devstate_change ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 238 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, astdb_family, CLI_FAILURE, CLI_GENERATE, CLI_INIT, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::command, ast_cli_args::fd, len(), ast_cli_args::n, NULL, ast_cli_args::pos, state, ast_cli_entry::usage, and ast_cli_args::word.

239 {
240  size_t len;
241  const char *dev, *state;
242  enum ast_device_state state_val;
243 
244  switch (cmd) {
245  case CLI_INIT:
246  e->command = "devstate change";
247  e->usage =
248  "Usage: devstate change <device> <state>\n"
249  " Change a custom device to a new state.\n"
250  " The possible values for the state are:\n"
251  "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
252  "RINGINUSE | ONHOLD\n"
253  "\n"
254  "Examples:\n"
255  " devstate change Custom:mystate1 INUSE\n"
256  " devstate change Custom:mystate1 NOT_INUSE\n"
257  " \n";
258  return NULL;
259  case CLI_GENERATE:
260  {
261  static const char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
262  "UNAVAILABLE", "RINGING", "RINGINUSE", "ONHOLD", NULL };
263 
264  if (a->pos == e->args + 1)
265  return ast_cli_complete(a->word, cmds, a->n);
266 
267  return NULL;
268  }
269  }
270 
271  if (a->argc != e->args + 2)
272  return CLI_SHOWUSAGE;
273 
274  len = strlen("Custom:");
275  dev = a->argv[e->args];
276  state = a->argv[e->args + 1];
277 
278  if (strncasecmp(dev, "Custom:", len)) {
279  ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
280  return CLI_FAILURE;
281  }
282 
283  dev += len;
284  if (ast_strlen_zero(dev))
285  return CLI_SHOWUSAGE;
286 
287  state_val = ast_devstate_val(state);
288 
289  if (state_val == AST_DEVICE_UNKNOWN)
290  return CLI_SHOWUSAGE;
291 
292  ast_cli(a->fd, "Changing %s to %s\n", dev, state);
293 
294  ast_db_put(astdb_family, dev, state);
295 
296  ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", dev);
297 
298  return CLI_SUCCESS;
299 }
enum sip_cc_notify_state state
Definition: chan_sip.c:959
ast_device_state
Device States.
Definition: devicestate.h:52
const int argc
Definition: cli.h:160
Definition: cli.h:152
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
int args
This gets set in ast_cli_register()
Definition: cli.h:185
#define ast_strlen_zero(foo)
Definition: strings.h:52
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
Definition: main/cli.c:1811
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:510
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:260
const int fd
Definition: cli.h:159
const int n
Definition: cli.h:165
const char *const * argv
Definition: cli.h:161
#define CLI_SHOWUSAGE
Definition: cli.h:45
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define CLI_FAILURE
Definition: cli.h:46
char * command
Definition: cli.h:186
const char * word
Definition: cli.h:163
static const char astdb_family[]
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
const int pos
Definition: cli.h:164
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition: main/db.c:327

◆ handle_cli_devstate_list()

static char* handle_cli_devstate_list ( struct ast_cli_entry e,
int  cmd,
struct ast_cli_args a 
)
static

Definition at line 194 of file func_devstate.c.

References ast_cli_args::argc, ast_cli_entry::args, ast_cli(), ast_db_freetree(), ast_db_gettree(), astdb_family, 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, NULL, and ast_cli_entry::usage.

195 {
196  struct ast_db_entry *db_entry, *db_tree;
197 
198  switch (cmd) {
199  case CLI_INIT:
200  e->command = "devstate list";
201  e->usage =
202  "Usage: devstate list\n"
203  " List all custom device states that have been set by using\n"
204  " the DEVICE_STATE dialplan function.\n";
205  return NULL;
206  case CLI_GENERATE:
207  return NULL;
208  }
209 
210  if (a->argc != e->args)
211  return CLI_SHOWUSAGE;
212 
213  ast_cli(a->fd, "\n"
214  "---------------------------------------------------------------------\n"
215  "--- Custom Device States --------------------------------------------\n"
216  "---------------------------------------------------------------------\n"
217  "---\n");
218 
219  db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
220  for (; db_entry; db_entry = db_entry->next) {
221  const char *dev_name = strrchr(db_entry->key, '/') + 1;
222  if (dev_name <= (const char *) 1)
223  continue;
224  ast_cli(a->fd, "--- Name: 'Custom:%s' State: '%s'\n"
225  "---\n", dev_name, db_entry->data);
226  }
227  ast_db_freetree(db_tree);
228  db_tree = NULL;
229 
230  ast_cli(a->fd,
231  "---------------------------------------------------------------------\n"
232  "---------------------------------------------------------------------\n"
233  "\n");
234 
235  return CLI_SUCCESS;
236 }
const int argc
Definition: cli.h:160
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition: main/db.c:598
Definition: cli.h:152
#define NULL
Definition: resample.c:96
void ast_cli(int fd, const char *fmt,...)
Definition: clicompat.c:6
int args
This gets set in ast_cli_register()
Definition: cli.h:185
struct ast_db_entry * next
Definition: astdb.h:32
const int fd
Definition: cli.h:159
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
Definition: main/db.c:531
#define CLI_SHOWUSAGE
Definition: cli.h:45
Definition: astdb.h:31
char data[0]
Definition: astdb.h:34
char * command
Definition: cli.h:186
static const char astdb_family[]
const char * usage
Definition: cli.h:177
#define CLI_SUCCESS
Definition: cli.h:44
char * key
Definition: astdb.h:33

◆ hint_read()

static int hint_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
)
static

Definition at line 146 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, NULL, options, and strsep().

147 {
148  char *exten, *context;
150  AST_APP_ARG(exten);
152  );
153  struct ast_flags opts = { 0, };
154  int res;
155 
156  if (ast_strlen_zero(data)) {
157  ast_log(LOG_WARNING, "The HINT function requires an extension\n");
158  return -1;
159  }
160 
162 
163  if (ast_strlen_zero(args.exten)) {
164  ast_log(LOG_WARNING, "The HINT function requires an extension\n");
165  return -1;
166  }
167 
168  context = exten = args.exten;
169  strsep(&context, "@");
170  if (ast_strlen_zero(context))
171  context = "default";
172 
173  if (!ast_strlen_zero(args.options))
174  ast_app_parse_options(hint_options, &opts, NULL, args.options);
175 
176  if (ast_test_flag(&opts, HINT_OPT_NAME))
177  res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
178  else
179  res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);
180 
181  return !res; /* ast_get_hint returns non-zero on success */
182 }
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:4141
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
#define ast_test_flag(p, flag)
Definition: utils.h:63
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the &#39;standard&#39; argument separation process for an application.
#define LOG_WARNING
Definition: logger.h:274
static const struct ast_app_option hint_options[128]
const char * args
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
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: main/app.c:2906
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Structure used to handle boolean flags.
Definition: utils.h:199
char * strsep(char **str, const char *delims)
static struct test_options options
static char context[AST_MAX_CONTEXT]
Definition: chan_alsa.c:116
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application&#39;s arguments.
#define AST_APP_ARG(name)
Define an application argument.

◆ load_module()

static int load_module ( void  )
static

Definition at line 329 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(), AST_MODFLAG_LOAD_ORDER, AST_MODPRI_DEVSTATE_PROVIDER, AST_MODULE_INFO(), AST_MODULE_SUPPORT_CORE, astdb_family, ASTERISK_GPL_KEY, custom_devstate_callback(), ast_db_entry::data, ast_db_entry::key, ast_db_entry::next, NULL, and unload_module().

330 {
331  int res = 0;
332  struct ast_db_entry *db_entry, *db_tree;
333 
334  /* Populate the device state cache on the system with all of the currently
335  * known custom device states. */
336  db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
337  for (; db_entry; db_entry = db_entry->next) {
338  const char *dev_name = strrchr(db_entry->key, '/') + 1;
339  if (dev_name <= (const char *) 1)
340  continue;
342  AST_DEVSTATE_CACHABLE, "Custom:%s", dev_name);
343  }
344  ast_db_freetree(db_tree);
345  db_tree = NULL;
346 
351 
352  return res;
353 }
#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: main/db.c:598
#define ast_cli_register_multiple(e, len)
Register multiple commands.
Definition: cli.h:265
int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback)
Add device state provider.
Definition: devicestate.c:391
#define NULL
Definition: resample.c:96
static struct ast_custom_function hint_function
struct ast_db_entry * next
Definition: astdb.h:32
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:510
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
Definition: devicestate.c:260
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.
Definition: main/db.c:531
static struct ast_custom_function devstate_function
Definition: astdb.h:31
char data[0]
Definition: astdb.h:34
static const char astdb_family[]
#define ast_custom_function_register(acf)
Register a custom function.
Definition: pbx.h:1508
static struct ast_cli_entry cli_funcdevstate[]
char * key
Definition: astdb.h:33

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 317 of file func_devstate.c.

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

Referenced by load_module().

318 {
319  int res = 0;
320 
323  res |= ast_devstate_prov_del("Custom");
325 
326  return res;
327 }
#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: clicompat.c:30
int ast_devstate_prov_del(const char *label)
Remove device state provider.
Definition: devicestate.c:418
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
static struct ast_custom_function hint_function
static struct ast_custom_function devstate_function
static struct ast_cli_entry cli_funcdevstate[]

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PROVIDER, }
static

Definition at line 360 of file func_devstate.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 360 of file func_devstate.c.

◆ astdb_family

const char astdb_family[] = "CustomDevstate"
static

◆ cli_funcdevstate

struct ast_cli_entry cli_funcdevstate[]
static
Initial value:
= {
{ .handler = handle_cli_devstate_list , .summary = "List currently known custom device states" ,},
{ .handler = handle_cli_devstate_change , .summary = "Change a custom device state" ,},
}
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 301 of file func_devstate.c.

◆ devstate_function

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 306 of file func_devstate.c.

◆ hint_function

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 312 of file func_devstate.c.

◆ hint_options

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

Definition at line 144 of file func_devstate.c.

Referenced by hint_read().