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"
45 struct custom_device {
47 AST_RWLIST_ENTRY(custom_device) entry;
51 static AST_RWLIST_HEAD_STATIC(custom_devices, custom_device);
53 static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
55 ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);
60 static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
62 struct custom_device *dev;
63 int len = strlen("Custom:");
65 if (strncasecmp(data, "Custom:", len)) {
66 ast_log(LOG_WARNING, "The DEVSTATE function can only be used to set 'Custom:' device state!\n");
70 if (ast_strlen_zero(data)) {
71 ast_log(LOG_WARNING, "DEVSTATE function called with no custom device name!\n");
75 AST_RWLIST_WRLOCK(&custom_devices);
76 AST_RWLIST_TRAVERSE(&custom_devices, dev, entry) {
77 if (!strcasecmp(dev->name, data))
81 if (!(dev = ast_calloc(1, sizeof(*dev) + strlen(data) + 1))) {
82 AST_RWLIST_UNLOCK(&custom_devices);
85 strcpy(dev->name, data);
86 AST_RWLIST_INSERT_HEAD(&custom_devices, dev, entry);
88 dev->state = ast_devstate_val(value);
89 ast_device_state_changed("Custom:%s", dev->name);
90 AST_RWLIST_UNLOCK(&custom_devices);
95 static enum ast_device_state custom_devstate_callback(const char *data)
97 struct custom_device *dev;
98 enum ast_device_state state = AST_DEVICE_UNKNOWN;
100 AST_RWLIST_RDLOCK(&custom_devices);
101 AST_RWLIST_TRAVERSE(&custom_devices, dev, entry) {
102 if (!strcasecmp(dev->name, data)) {
107 AST_RWLIST_UNLOCK(&custom_devices);
112 static char *cli_funcdevstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
114 struct custom_device *dev;
118 e->command = "funcdevstate list";
120 "Usage: funcdevstate list\n"
121 " List all custom device states that have been set by using\n"
122 " the DEVSTATE dialplan function.\n";
128 if (a->argc != e->args)
129 return CLI_SHOWUSAGE;
132 "---------------------------------------------------------------------\n"
133 "--- Custom Device States --------------------------------------------\n"
134 "---------------------------------------------------------------------\n"
136 AST_RWLIST_RDLOCK(&custom_devices);
137 AST_RWLIST_TRAVERSE(&custom_devices, dev, entry) {
138 ast_cli(a->fd, "--- Name: 'Custom:%s' State: '%s'\n"
139 "---\n", dev->name, ast_devstate_str(dev->state));
141 AST_RWLIST_UNLOCK(&custom_devices);
143 "---------------------------------------------------------------------\n"
144 "---------------------------------------------------------------------\n"
150 static struct ast_cli_entry cli_funcdevstate[] = {
151 NEW_CLI(cli_funcdevstate_list, "List currently known custom device states"),
154 static struct ast_custom_function devstate_function = {
156 .synopsis = "Get or Set a device state",
157 .syntax = "DEVSTATE(device)",
159 " The DEVSTATE function can be used to retrieve the device state from any\n"
160 "device state provider. For example:\n"
161 " NoOp(SIP/mypeer has state ${DEVSTATE(SIP/mypeer)})\n"
162 " NoOp(Conference number 1234 has state ${DEVSTATE(MeetMe:1234)})\n"
164 " The DEVSTATE function can also be used to set custom device state from\n"
165 "the dialplan. The \"Custom:\" prefix must be used. For example:\n"
166 " Set(DEVSTATE(Custom:lamp1)=BUSY)\n"
167 " Set(DEVSTATE(Custom:lamp2)=NOT_INUSE)\n"
168 "You can subscribe to the status of a custom device state using a hint in\n"
170 " exten => 1234,hint,Custom:lamp1\n"
172 " The possible values for both uses of this function are:\n"
173 "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
174 "RINGINUSE | ONHOLD\n",
175 .read = devstate_read,
176 .write = devstate_write,
179 static int unload_module(void)
181 struct custom_device *dev;
184 res |= ast_custom_function_unregister(&devstate_function);
185 res |= ast_devstate_prov_del("Custom");
186 res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
188 AST_RWLIST_WRLOCK(&custom_devices);
189 while ((dev = AST_RWLIST_REMOVE_HEAD(&custom_devices, entry)))
191 AST_RWLIST_UNLOCK(&custom_devices);
196 static int load_module(void)
200 res |= ast_custom_function_register(&devstate_function);
201 res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
202 res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
207 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Device state dialplan functions");