This introduces a new dialplan function, DEVSTATE, which allows you to do some
[asterisk/asterisk.git] / funcs / func_devstate.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Digium, Inc.
5  *
6  * Russell Bryant <russell@digium.com> 
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief Manually controlled blinky lights
22  *
23  * \author Russell Bryant <russell@digium.com> 
24  *
25  * \ingroup functions
26  *
27  * \note Props go out to Ahrimanes in #asterisk for requesting this at 4:30 AM
28  *       when I couldn't sleep.  :)
29  */
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include <stdlib.h>
36
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
45 struct custom_device {
46         int state;
47         AST_RWLIST_ENTRY(custom_device) entry;
48         char name[1];
49 };
50
51 static AST_RWLIST_HEAD_STATIC(custom_devices, custom_device);
52
53 static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
54 {
55         ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);
56
57         return 0;
58 }
59
60 static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
61 {
62         struct custom_device *dev;
63         int len = strlen("Custom:");
64
65         if (strncasecmp(data, "Custom:", len)) {
66                 ast_log(LOG_WARNING, "The DEVSTATE function can only be used to set 'Custom:' device state!\n");
67                 return -1;
68         }
69         data += len;
70         if (ast_strlen_zero(data)) {
71                 ast_log(LOG_WARNING, "DEVSTATE function called with no custom device name!\n");
72                 return -1;
73         }
74
75         AST_RWLIST_WRLOCK(&custom_devices);
76         AST_RWLIST_TRAVERSE(&custom_devices, dev, entry) {
77                 if (!strcasecmp(dev->name, data))
78                         break;
79         }
80         if (!dev) {
81                 if (!(dev = ast_calloc(1, sizeof(*dev) + strlen(data) + 1))) {
82                         AST_RWLIST_UNLOCK(&custom_devices);
83                         return -1;
84                 }
85                 strcpy(dev->name, data);
86                 AST_RWLIST_INSERT_HEAD(&custom_devices, dev, entry);
87         }
88         dev->state = ast_devstate_val(value);
89         ast_device_state_changed("Custom:%s", dev->name);
90         AST_RWLIST_UNLOCK(&custom_devices);
91
92         return 0;
93 }
94
95 static enum ast_device_state custom_devstate_callback(const char *data)
96 {
97         struct custom_device *dev;
98         enum ast_device_state state = AST_DEVICE_UNKNOWN;
99
100         AST_RWLIST_RDLOCK(&custom_devices);
101         AST_RWLIST_TRAVERSE(&custom_devices, dev, entry) {
102                 if (!strcasecmp(dev->name, data))
103                         state = dev->state;     
104         }
105         AST_RWLIST_UNLOCK(&custom_devices);
106
107         return state;
108 }
109
110 static char *cli_funcdevstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
111 {
112         struct custom_device *dev;
113
114         switch (cmd) {
115         case CLI_INIT:
116                 e->command = "funcdevstate list";
117                 e->usage =
118                         "Usage: funcdevstate list\n"
119                         "       List all custom device states that have been set by using\n"
120                         "       the DEVSTATE dialplan function.\n";
121                 return NULL;
122         case CLI_GENERATE:
123                 return NULL;
124         }
125
126         if (a->argc != e->args)
127                 return CLI_SHOWUSAGE;
128
129         ast_cli(a->fd, "\n"
130                 "---------------------------------------------------------------------\n"
131                 "--- Custom Device States --------------------------------------------\n"
132                 "---------------------------------------------------------------------\n"
133                 "---\n");
134         AST_RWLIST_RDLOCK(&custom_devices);
135         AST_RWLIST_TRAVERSE(&custom_devices, dev, entry) {
136                 ast_cli(a->fd, "--- Name: 'Custom:%s'  State: '%s'\n"
137                                "---\n", dev->name, ast_devstate_str(dev->state));
138         }
139         AST_RWLIST_UNLOCK(&custom_devices);
140         ast_cli(a->fd,
141                 "---------------------------------------------------------------------\n"
142                 "---------------------------------------------------------------------\n"
143                 "\n");
144
145         return CLI_SUCCESS;
146 }
147
148 static struct ast_cli_entry cli_funcdevstate[] = {
149         NEW_CLI(cli_funcdevstate_list, "List currently known custom device states"),
150 };
151
152 static struct ast_custom_function devstate_function = {
153         .name = "DEVSTATE",
154         .synopsis = "Get or Set a device state",
155         .syntax = "DEVSTATE(device)",
156         .desc =
157         "  The DEVSTATE function can be used to retrieve the device state from any\n"
158         "device state provider.  For example:\n"
159         "   NoOp(SIP/mypeer has state ${DEVSTATE(SIP/mypeer)})\n"
160         "   NoOp(Conference number 1234 has state ${DEVSTATE(MeetMe:1234)})\n"
161         "\n"
162         "  The DEVSTATE function can also be used to set custom device state from\n"
163         "the dialplan.  The \"Custom:\" prefix must be used.  For example:\n"
164         "  Set(DEVSTATE(Custom:lamp1)=BUSY)\n"
165         "  Set(DEVSTATE(Custom:lamp2)=NOT_INUSE)\n"
166         "You can subscribe to the status of a custom device state using a hint in\n"
167         "the dialplan:\n"
168         "  exten => 1234,hint,Custom:lamp1\n"
169         "\n"
170         "  The possible values for both uses of this function are:\n"
171         "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
172         "RINGINUSE | ONHOLD\n",
173         .read = devstate_read,
174         .write = devstate_write,
175 };
176
177 static int unload_module(void)
178 {
179         struct custom_device *dev;
180         int res = 0;
181
182         res |= ast_custom_function_unregister(&devstate_function);
183         res |= ast_devstate_prov_del("Custom");
184         res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
185
186         AST_RWLIST_WRLOCK(&custom_devices);
187         while ((dev = AST_RWLIST_REMOVE_HEAD(&custom_devices, entry)))
188                 free(dev);
189         AST_RWLIST_UNLOCK(&custom_devices);
190
191         return res;
192 }
193
194 static int load_module(void)
195 {
196         int res = 0;
197
198         res |= ast_custom_function_register(&devstate_function);
199         res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
200         res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
201
202         return res;
203 }
204
205 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Device state dialplan functions");