Asterisk - The Open Source Telephony Project GIT-master-b023714
Loading...
Searching...
No Matches
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 = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .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 146 of file func_devstate.c.

146 {
147 HINT_OPT_NAME = (1 << 0),
148};
@ HINT_OPT_NAME

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 368 of file func_devstate.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 368 of file func_devstate.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 368 of file func_devstate.c.

◆ custom_devstate_callback()

static enum ast_device_state custom_devstate_callback ( const char *  data)
static

Definition at line 192 of file func_devstate.c.

193{
194 char buf[256] = "";
195
196 /* Ignore check_return warning from Coverity fow ast_db_get below */
197 ast_db_get(astdb_family, data, buf, sizeof(buf));
198
199 return ast_devstate_val(buf);
200}
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition db.c:421
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
char buf[BUFSIZE]
Definition eagi_proxy.c:66
static const char astdb_family[]

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

Referenced by load_module().

◆ devstate_read()

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

Definition at line 110 of file func_devstate.c.

111{
113
114 return 0;
115}
const char * ast_devstate_str(enum ast_device_state devstate) attribute_pure
Convert device state to text string that is easier to parse.
ast_device_state
Device States.
Definition devicestate.h:52
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:425

References ast_copy_string(), ast_devstate_str(), buf, and len().

◆ devstate_write()

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

Definition at line 117 of file func_devstate.c.

118{
119 size_t len = strlen("Custom:");
120 enum ast_device_state state_val;
121
122 if (strncasecmp(data, "Custom:", len)) {
123 ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
124 return -1;
125 }
126 data += len;
127 if (ast_strlen_zero(data)) {
128 ast_log(LOG_WARNING, "DEVICE_STATE function called with no custom device name!\n");
129 return -1;
130 }
131
132 state_val = ast_devstate_val(value);
133
134 if (state_val == AST_DEVICE_UNKNOWN) {
135 ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value);
136 return -1;
137 }
138
140
141 ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", data);
142
143 return 0;
144}
int ast_db_put(const char *family, const char *key, const char *value)
Store value addressed by family/key.
Definition db.c:335
#define ast_log
Definition astobj2.c:42
@ AST_DEVSTATE_CACHABLE
Definition devicestate.h:70
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.
@ AST_DEVICE_UNKNOWN
Definition devicestate.h:53
#define LOG_ERROR
#define LOG_WARNING
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
int value
Definition syslog.c:37

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, LOG_WARNING, and value.

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

247{
248 size_t len;
249 const char *dev, *state;
250 enum ast_device_state state_val;
251
252 switch (cmd) {
253 case CLI_INIT:
254 e->command = "devstate change";
255 e->usage =
256 "Usage: devstate change <device> <state>\n"
257 " Change a custom device to a new state.\n"
258 " The possible values for the state are:\n"
259 "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
260 "RINGINUSE | ONHOLD\n"
261 "\n"
262 "Examples:\n"
263 " devstate change Custom:mystate1 INUSE\n"
264 " devstate change Custom:mystate1 NOT_INUSE\n"
265 " \n";
266 return NULL;
267 case CLI_GENERATE:
268 {
269 static const char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
270 "UNAVAILABLE", "RINGING", "RINGINUSE", "ONHOLD", NULL };
271
272 if (a->pos == e->args + 1)
273 return ast_cli_complete(a->word, cmds, a->n);
274
275 return NULL;
276 }
277 }
278
279 if (a->argc != e->args + 2)
280 return CLI_SHOWUSAGE;
281
282 len = strlen("Custom:");
283 dev = a->argv[e->args];
284 state = a->argv[e->args + 1];
285
286 if (strncasecmp(dev, "Custom:", len)) {
287 ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
288 return CLI_FAILURE;
289 }
290
291 dev += len;
292 if (ast_strlen_zero(dev))
293 return CLI_SHOWUSAGE;
294
295 state_val = ast_devstate_val(state);
296
297 if (state_val == AST_DEVICE_UNKNOWN)
298 return CLI_SHOWUSAGE;
299
300 ast_cli(a->fd, "Changing %s to %s\n", dev, state);
301
303
304 ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", dev);
305
306 return CLI_SUCCESS;
307}
#define CLI_SHOWUSAGE
Definition cli.h:45
#define CLI_SUCCESS
Definition cli.h:44
void ast_cli(int fd, const char *fmt,...)
Definition clicompat.c:6
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
Definition main/cli.c:1823
@ CLI_INIT
Definition cli.h:152
@ CLI_GENERATE
Definition cli.h:153
#define CLI_FAILURE
Definition cli.h:46
#define NULL
Definition resample.c:96
int args
This gets set in ast_cli_register()
Definition cli.h:185
char * command
Definition cli.h:186
const char * usage
Definition cli.h:177
static struct test_val a

References a, ast_cli_entry::args, 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, len(), NULL, and ast_cli_entry::usage.

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

203{
204 struct ast_db_entry *db_entry, *db_tree;
205
206 switch (cmd) {
207 case CLI_INIT:
208 e->command = "devstate list";
209 e->usage =
210 "Usage: devstate list\n"
211 " List all custom device states that have been set by using\n"
212 " the DEVICE_STATE dialplan function.\n";
213 return NULL;
214 case CLI_GENERATE:
215 return NULL;
216 }
217
218 if (a->argc != e->args)
219 return CLI_SHOWUSAGE;
220
221 ast_cli(a->fd, "\n"
222 "---------------------------------------------------------------------\n"
223 "--- Custom Device States --------------------------------------------\n"
224 "---------------------------------------------------------------------\n"
225 "---\n");
226
227 db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
228 for (; db_entry; db_entry = db_entry->next) {
229 const char *dev_name = strrchr(db_entry->key, '/') + 1;
230 if (dev_name <= (const char *) 1)
231 continue;
232 ast_cli(a->fd, "--- Name: 'Custom:%s' State: '%s'\n"
233 "---\n", dev_name, db_entry->data);
234 }
235 ast_db_freetree(db_tree);
236 db_tree = NULL;
237
238 ast_cli(a->fd,
239 "---------------------------------------------------------------------\n"
240 "---------------------------------------------------------------------\n"
241 "\n");
242
243 return CLI_SUCCESS;
244}
struct ast_db_entry * ast_db_gettree(const char *family, const char *keytree)
Get a list of values within the astdb tree.
Definition db.c:635
void ast_db_freetree(struct ast_db_entry *entry)
Free structure created by ast_db_gettree()
Definition db.c:695
Definition astdb.h:31
struct ast_db_entry * next
Definition astdb.h:32
char * key
Definition astdb.h:33
char data[0]
Definition astdb.h:34

References a, 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_db_entry::key, ast_db_entry::next, NULL, and ast_cli_entry::usage.

◆ hint_read()

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

Definition at line 154 of file func_devstate.c.

155{
156 char *exten, *context;
158 AST_APP_ARG(exten);
160 );
161 struct ast_flags opts = { 0, };
162 int res;
163
164 if (ast_strlen_zero(data)) {
165 ast_log(LOG_WARNING, "The HINT function requires an extension\n");
166 return -1;
167 }
168
170
171 if (ast_strlen_zero(args.exten)) {
172 ast_log(LOG_WARNING, "The HINT function requires an extension\n");
173 return -1;
174 }
175
176 context = exten = args.exten;
177 strsep(&context, "@");
178 if (ast_strlen_zero(context))
179 context = "default";
180
181 if (!ast_strlen_zero(args.options))
183
184 if (ast_test_flag(&opts, HINT_OPT_NAME))
185 res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
186 else
187 res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);
188
189 return !res; /* ast_get_hint returns non-zero on success */
190}
char * strsep(char **str, const char *delims)
static const struct ast_app_option hint_options[128]
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
#define AST_STANDARD_APP_ARGS(args, parse)
Performs the 'standard' argument separation process for an application.
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:3066
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:4158
static struct @519 args
Structure used to handle boolean flags.
Definition utils.h:217
static struct test_options options
#define ast_test_flag(p, flag)
Definition utils.h:63

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, buf, HINT_OPT_NAME, hint_options, len(), LOG_WARNING, NULL, options, and strsep().

◆ load_module()

static int load_module ( void  )
static

Definition at line 337 of file func_devstate.c.

338{
339 int res = 0;
340 struct ast_db_entry *db_entry, *db_tree;
341
342 /* Populate the device state cache on the system with all of the currently
343 * known custom device states. */
344 db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
345 for (; db_entry; db_entry = db_entry->next) {
346 const char *dev_name = strrchr(db_entry->key, '/') + 1;
347 if (dev_name <= (const char *) 1)
348 continue;
350 AST_DEVSTATE_CACHABLE, "Custom:%s", dev_name);
351 }
352 ast_db_freetree(db_tree);
353 db_tree = NULL;
354
359
360 return res;
361}
#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.
static struct ast_cli_entry cli_funcdevstate[]
static enum ast_device_state custom_devstate_callback(const char *data)
static struct ast_custom_function devstate_function
static struct ast_custom_function hint_function
#define ast_custom_function_register(acf)
Register a custom function.
Definition pbx.h:1562
#define ARRAY_LEN(a)
Definition utils.h:703

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(), astdb_family, cli_funcdevstate, custom_devstate_callback(), ast_db_entry::data, devstate_function, hint_function, ast_db_entry::key, ast_db_entry::next, and NULL.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 325 of file func_devstate.c.

326{
327 int res = 0;
328
331 res |= ast_devstate_prov_del("Custom");
333
334 return res;
335}
void ast_cli_unregister_multiple(void)
Definition ael_main.c:408
int ast_devstate_prov_del(const char *label)
Remove device state provider.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.

References ARRAY_LEN, ast_cli_unregister_multiple(), ast_custom_function_unregister(), ast_devstate_prov_del(), cli_funcdevstate, devstate_function, and hint_function.

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 = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_PROVIDER, }
static

Definition at line 368 of file func_devstate.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 368 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_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)

Definition at line 309 of file func_devstate.c.

309 {
310 AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states"),
311 AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"),
312};
#define AST_CLI_DEFINE(fn, txt,...)
Definition cli.h:197

Referenced by load_module(), and unload_module().

◆ devstate_function

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

Definition at line 314 of file func_devstate.c.

314 {
315 .name = "DEVICE_STATE",
316 .read = devstate_read,
317 .write = devstate_write,
318};

Referenced by load_module(), and unload_module().

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

320 {
321 .name = "HINT",
322 .read = hint_read,
323};

Referenced by load_module(), and unload_module().

◆ hint_options

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

Definition at line 152 of file func_devstate.c.

Referenced by hint_read().