7ed389c9bdfe0bba39f299c1744cc5dab09bae17
[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 #include "asterisk/astdb.h"
45 #include "asterisk/app.h"
46
47 static const char astdb_family[] = "CustomDevstate";
48
49 static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
50 {
51         ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);
52
53         return 0;
54 }
55
56 static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
57 {
58         size_t len = strlen("Custom:");
59
60         if (strncasecmp(data, "Custom:", len)) {
61                 ast_log(LOG_WARNING, "The DEVSTATE function can only be used to set 'Custom:' device state!\n");
62                 return -1;
63         }
64         data += len;
65         if (ast_strlen_zero(data)) {
66                 ast_log(LOG_WARNING, "DEVSTATE function called with no custom device name!\n");
67                 return -1;
68         }
69
70         ast_db_put(astdb_family, data, value);
71
72         ast_devstate_changed(ast_devstate_val(value), "Custom:%s", data);
73
74         return 0;
75 }
76
77 enum {
78         HINT_OPT_NAME = (1 << 0),
79 };
80
81 AST_APP_OPTIONS(hint_options, BEGIN_OPTIONS
82         AST_APP_OPTION('n', HINT_OPT_NAME),
83 END_OPTIONS );
84
85 static int hint_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
86 {
87         char *exten, *context;
88         AST_DECLARE_APP_ARGS(args,
89                 AST_APP_ARG(exten);
90                 AST_APP_ARG(options);
91         );
92         struct ast_flags opts = { 0, };
93         int res;
94
95         if (ast_strlen_zero(data)) {
96                 ast_log(LOG_WARNING, "The HINT function requires an extension\n");
97                 return -1;
98         }
99
100         AST_STANDARD_APP_ARGS(args, data);
101
102         if (ast_strlen_zero(args.exten)) {
103                 ast_log(LOG_WARNING, "The HINT function requires an extension\n");
104                 return -1;
105         }
106
107         context = exten = args.exten;
108         strsep(&context, "@");
109         if (ast_strlen_zero(context))
110                 context = "default";
111
112         if (!ast_strlen_zero(args.options))
113                 ast_app_parse_options(hint_options, &opts, NULL, args.options);
114
115         if (ast_test_flag(&opts, HINT_OPT_NAME))
116                 res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
117         else
118                 res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);
119
120         return !res; /* ast_get_hint returns non-zero on success */
121 }
122
123 static enum ast_device_state custom_devstate_callback(const char *data)
124 {
125         char buf[256] = "";
126
127         ast_db_get(astdb_family, data, buf, sizeof(buf));
128
129         return ast_devstate_val(buf);
130 }
131
132 static char *cli_funcdevstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
133 {
134         struct ast_db_entry *db_entry, *db_tree;
135
136         switch (cmd) {
137         case CLI_INIT:
138                 e->command = "funcdevstate list";
139                 e->usage =
140                         "Usage: funcdevstate list\n"
141                         "       List all custom device states that have been set by using\n"
142                         "       the DEVSTATE dialplan function.\n";
143                 return NULL;
144         case CLI_GENERATE:
145                 return NULL;
146         }
147
148         if (a->argc != e->args)
149                 return CLI_SHOWUSAGE;
150
151         ast_cli(a->fd, "\n"
152                 "---------------------------------------------------------------------\n"
153                 "--- Custom Device States --------------------------------------------\n"
154                 "---------------------------------------------------------------------\n"
155                 "---\n");
156
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)
161                         continue;
162                 ast_cli(a->fd, "--- Name: 'Custom:%s'  State: '%s'\n"
163                                "---\n", dev_name, db_entry->data);
164         }
165         ast_db_freetree(db_tree);
166         db_tree = NULL;
167
168         ast_cli(a->fd,
169                 "---------------------------------------------------------------------\n"
170                 "---------------------------------------------------------------------\n"
171                 "\n");
172
173         return CLI_SUCCESS;
174 }
175
176 static struct ast_cli_entry cli_funcdevstate[] = {
177         NEW_CLI(cli_funcdevstate_list, "List currently known custom device states"),
178 };
179
180 static struct ast_custom_function devstate_function = {
181         .name = "DEVSTATE",
182         .synopsis = "Get or Set a device state",
183         .syntax = "DEVSTATE(device)",
184         .desc =
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"
189         "\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"
195         "the dialplan:\n"
196         "  exten => 1234,hint,Custom:lamp1\n"
197         "\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,
203 };
204
205 static struct ast_custom_function hint_function = {
206         .name = "HINT",
207         .synopsis = "Get the devices set for a dialplan hint",
208         .syntax = "HINT(extension[@context][|options])",
209         .desc =
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"
213         "Options:\n"
214         "   'n' - Retrieve name on the hint instead of list of devices\n"
215         "",
216         .read = hint_read,
217 };
218
219 static int unload_module(void)
220 {
221         int res = 0;
222
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));
227
228         return res;
229 }
230
231 static int load_module(void)
232 {
233         int res = 0;
234         struct ast_db_entry *db_entry, *db_tree;
235
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)
242                         continue;
243                 ast_devstate_changed(ast_devstate_val(db_entry->data),
244                         "Custom:%s\n", dev_name);
245         }
246         ast_db_freetree(db_tree);
247         db_tree = NULL;
248
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));
253
254         return res;
255 }
256
257 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets or sets a device state in the dialplan");