6b0e7a3f94fd3d39c91672ed82239ddb12192a4d
[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                         break;
105                 }
106         }
107         AST_RWLIST_UNLOCK(&custom_devices);
108
109         return state;
110 }
111
112 static char *cli_funcdevstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
113 {
114         struct custom_device *dev;
115
116         switch (cmd) {
117         case CLI_INIT:
118                 e->command = "funcdevstate list";
119                 e->usage =
120                         "Usage: funcdevstate list\n"
121                         "       List all custom device states that have been set by using\n"
122                         "       the DEVSTATE dialplan function.\n";
123                 return NULL;
124         case CLI_GENERATE:
125                 return NULL;
126         }
127
128         if (a->argc != e->args)
129                 return CLI_SHOWUSAGE;
130
131         ast_cli(a->fd, "\n"
132                 "---------------------------------------------------------------------\n"
133                 "--- Custom Device States --------------------------------------------\n"
134                 "---------------------------------------------------------------------\n"
135                 "---\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));
140         }
141         AST_RWLIST_UNLOCK(&custom_devices);
142         ast_cli(a->fd,
143                 "---------------------------------------------------------------------\n"
144                 "---------------------------------------------------------------------\n"
145                 "\n");
146
147         return CLI_SUCCESS;
148 }
149
150 static struct ast_cli_entry cli_funcdevstate[] = {
151         NEW_CLI(cli_funcdevstate_list, "List currently known custom device states"),
152 };
153
154 static struct ast_custom_function devstate_function = {
155         .name = "DEVSTATE",
156         .synopsis = "Get or Set a device state",
157         .syntax = "DEVSTATE(device)",
158         .desc =
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"
163         "\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"
169         "the dialplan:\n"
170         "  exten => 1234,hint,Custom:lamp1\n"
171         "\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,
177 };
178
179 static int unload_module(void)
180 {
181         struct custom_device *dev;
182         int res = 0;
183
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));
187
188         AST_RWLIST_WRLOCK(&custom_devices);
189         while ((dev = AST_RWLIST_REMOVE_HEAD(&custom_devices, entry)))
190                 free(dev);
191         AST_RWLIST_UNLOCK(&custom_devices);
192
193         return res;
194 }
195
196 static int load_module(void)
197 {
198         int res = 0;
199
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));
203
204         return res;
205 }
206
207 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Device state dialplan functions");