2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007, Digium, Inc.
6 * Russell Bryant <russell@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Manually controlled blinky lights
23 * \author Russell Bryant <russell@digium.com>
27 * \note Props go out to Ahrimanes in \#asterisk for requesting this at 4:30 AM
28 * when I couldn't sleep. :)
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/module.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/utils.h"
41 #include "asterisk/linkedlists.h"
42 #include "asterisk/devicestate.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/astdb.h"
45 #include "asterisk/app.h"
47 static const char astdb_family[] = "CustomDevstate";
49 static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
51 ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);
56 static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
58 size_t len = strlen("Custom:");
60 if (strncasecmp(data, "Custom:", len)) {
61 ast_log(LOG_WARNING, "The DEVSTATE function can only be used to set 'Custom:' device state!\n");
65 if (ast_strlen_zero(data)) {
66 ast_log(LOG_WARNING, "DEVSTATE function called with no custom device name!\n");
70 ast_db_put(astdb_family, data, value);
72 ast_devstate_changed(ast_devstate_val(value), "Custom:%s", data);
78 HINT_OPT_NAME = (1 << 0),
81 AST_APP_OPTIONS(hint_options, BEGIN_OPTIONS
82 AST_APP_OPTION('n', HINT_OPT_NAME),
85 static int hint_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
87 char *exten, *context;
88 AST_DECLARE_APP_ARGS(args,
92 struct ast_flags opts = { 0, };
95 if (ast_strlen_zero(data)) {
96 ast_log(LOG_WARNING, "The HINT function requires an extension\n");
100 AST_STANDARD_APP_ARGS(args, data);
102 if (ast_strlen_zero(args.exten)) {
103 ast_log(LOG_WARNING, "The HINT function requires an extension\n");
107 context = exten = args.exten;
108 strsep(&context, "@");
109 if (ast_strlen_zero(context))
112 if (!ast_strlen_zero(args.options))
113 ast_app_parse_options(hint_options, &opts, NULL, args.options);
115 if (ast_test_flag(&opts, HINT_OPT_NAME))
116 res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
118 res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);
120 return !res; /* ast_get_hint returns non-zero on success */
123 static enum ast_device_state custom_devstate_callback(const char *data)
127 ast_db_get(astdb_family, data, buf, sizeof(buf));
129 return ast_devstate_val(buf);
132 static char *cli_funcdevstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
134 struct ast_db_entry *db_entry, *db_tree;
138 e->command = "funcdevstate list";
140 "Usage: funcdevstate list\n"
141 " List all custom device states that have been set by using\n"
142 " the DEVSTATE dialplan function.\n";
148 if (a->argc != e->args)
149 return CLI_SHOWUSAGE;
152 "---------------------------------------------------------------------\n"
153 "--- Custom Device States --------------------------------------------\n"
154 "---------------------------------------------------------------------\n"
157 db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
158 for (; db_entry; db_entry = db_entry->next) {
159 const char *dev_name = strrchr(db_entry->key, '/') + 1;
160 if (dev_name <= (const char *) 1)
162 ast_cli(a->fd, "--- Name: 'Custom:%s' State: '%s'\n"
163 "---\n", dev_name, db_entry->data);
165 ast_db_freetree(db_tree);
169 "---------------------------------------------------------------------\n"
170 "---------------------------------------------------------------------\n"
176 static struct ast_cli_entry cli_funcdevstate[] = {
177 NEW_CLI(cli_funcdevstate_list, "List currently known custom device states"),
180 static struct ast_custom_function devstate_function = {
182 .synopsis = "Get or Set a device state",
183 .syntax = "DEVSTATE(device)",
185 " The DEVSTATE function can be used to retrieve the device state from any\n"
186 "device state provider. For example:\n"
187 " NoOp(SIP/mypeer has state ${DEVSTATE(SIP/mypeer)})\n"
188 " NoOp(Conference number 1234 has state ${DEVSTATE(MeetMe:1234)})\n"
190 " The DEVSTATE function can also be used to set custom device state from\n"
191 "the dialplan. The \"Custom:\" prefix must be used. For example:\n"
192 " Set(DEVSTATE(Custom:lamp1)=BUSY)\n"
193 " Set(DEVSTATE(Custom:lamp2)=NOT_INUSE)\n"
194 "You can subscribe to the status of a custom device state using a hint in\n"
196 " exten => 1234,hint,Custom:lamp1\n"
198 " The possible values for both uses of this function are:\n"
199 "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
200 "RINGINUSE | ONHOLD\n",
201 .read = devstate_read,
202 .write = devstate_write,
205 static struct ast_custom_function hint_function = {
207 .synopsis = "Get the devices set for a dialplan hint",
208 .syntax = "HINT(extension[@context][|options])",
210 " The HINT function can be used to retrieve the list of devices that are\n"
211 "mapped to a dialplan hint. For example:\n"
212 " NoOp(Hint for Extension 1234 is ${HINT(1234)})\n"
214 " 'n' - Retrieve name on the hint instead of list of devices\n"
219 static int unload_module(void)
223 res |= ast_custom_function_unregister(&devstate_function);
224 res |= ast_custom_function_unregister(&hint_function);
225 res |= ast_devstate_prov_del("Custom");
226 res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
231 static int load_module(void)
234 struct ast_db_entry *db_entry, *db_tree;
236 /* Populate the device state cache on the system with all of the currently
237 * known custom device states. */
238 db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
239 for (; db_entry; db_entry = db_entry->next) {
240 const char *dev_name = strrchr(db_entry->key, '/') + 1;
241 if (dev_name <= (const char *) 1)
243 ast_devstate_changed(ast_devstate_val(db_entry->data),
244 "Custom:%s\n", dev_name);
246 ast_db_freetree(db_tree);
249 res |= ast_custom_function_register(&devstate_function);
250 res |= ast_custom_function_register(&hint_function);
251 res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
252 res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
257 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets or sets a device state in the dialplan");