Merged revisions 140670 via svnmerge from
[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  * \todo Delete the entry from AstDB when set to nothing like Set(DEVICE_STATE(Custom:lamp1)=)
28  *
29  * \note Props go out to Ahrimanes in \#asterisk for requesting this at 4:30 AM
30  *       when I couldn't sleep.  :)
31  */
32
33 #include "asterisk.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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         enum ast_device_state state_val;
60
61         if (strncasecmp(data, "Custom:", len)) {
62                 ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n");
63                 return -1;
64         }
65         data += len;
66         if (ast_strlen_zero(data)) {
67                 ast_log(LOG_WARNING, "DEVICE_STATE function called with no custom device name!\n");
68                 return -1;
69         }
70
71         state_val = ast_devstate_val(value);
72
73         if (state_val == AST_DEVICE_UNKNOWN) {
74                 ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value);
75                 return -1;
76         }
77
78         ast_db_put(astdb_family, data, value);
79
80         ast_devstate_changed(state_val, "Custom:%s", data);
81
82         return 0;
83 }
84
85 enum {
86         HINT_OPT_NAME = (1 << 0),
87 };
88
89 AST_APP_OPTIONS(hint_options, BEGIN_OPTIONS
90         AST_APP_OPTION('n', HINT_OPT_NAME),
91 END_OPTIONS );
92
93 static int hint_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
94 {
95         char *exten, *context;
96         AST_DECLARE_APP_ARGS(args,
97                 AST_APP_ARG(exten);
98                 AST_APP_ARG(options);
99         );
100         struct ast_flags opts = { 0, };
101         int res;
102
103         if (ast_strlen_zero(data)) {
104                 ast_log(LOG_WARNING, "The HINT function requires an extension\n");
105                 return -1;
106         }
107
108         AST_STANDARD_APP_ARGS(args, data);
109
110         if (ast_strlen_zero(args.exten)) {
111                 ast_log(LOG_WARNING, "The HINT function requires an extension\n");
112                 return -1;
113         }
114
115         context = exten = args.exten;
116         strsep(&context, "@");
117         if (ast_strlen_zero(context))
118                 context = "default";
119
120         if (!ast_strlen_zero(args.options))
121                 ast_app_parse_options(hint_options, &opts, NULL, args.options);
122
123         if (ast_test_flag(&opts, HINT_OPT_NAME))
124                 res = ast_get_hint(NULL, 0, buf, len, chan, context, exten);
125         else
126                 res = ast_get_hint(buf, len, NULL, 0, chan, context, exten);
127
128         return !res; /* ast_get_hint returns non-zero on success */
129 }
130
131 static enum ast_device_state custom_devstate_callback(const char *data)
132 {
133         char buf[256] = "";
134
135         ast_db_get(astdb_family, data, buf, sizeof(buf));
136
137         return ast_devstate_val(buf);
138 }
139
140 static char *handle_cli_funcdevstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
141 {
142         struct ast_db_entry *db_entry, *db_tree;
143
144         switch (cmd) {
145         case CLI_INIT:
146                 e->command = "funcdevstate list";
147                 e->usage =
148                         "Usage: funcdevstate list\n"
149                         "       List all custom device states that have been set by using\n"
150                         "       the DEVICE_STATE dialplan function.\n";
151                 return NULL;
152         case CLI_GENERATE:
153                 return NULL;
154         }
155
156         if (a->argc != e->args)
157                 return CLI_SHOWUSAGE;
158
159         ast_cli(a->fd, "\n"
160                 "---------------------------------------------------------------------\n"
161                 "--- Custom Device States --------------------------------------------\n"
162                 "---------------------------------------------------------------------\n"
163                 "---\n");
164
165         db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
166         for (; db_entry; db_entry = db_entry->next) {
167                 const char *dev_name = strrchr(db_entry->key, '/') + 1;
168                 if (dev_name <= (const char *) 1)
169                         continue;
170                 ast_cli(a->fd, "--- Name: 'Custom:%s'  State: '%s'\n"
171                                "---\n", dev_name, db_entry->data);
172         }
173         ast_db_freetree(db_tree);
174         db_tree = NULL;
175
176         ast_cli(a->fd,
177                 "---------------------------------------------------------------------\n"
178                 "---------------------------------------------------------------------\n"
179                 "\n");
180
181         return CLI_SUCCESS;
182 }
183
184 static char *handle_cli_devstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
185 {
186         struct ast_db_entry *db_entry, *db_tree;
187
188         switch (cmd) {
189         case CLI_INIT:
190                 e->command = "devstate list";
191                 e->usage =
192                         "Usage: devstate list\n"
193                         "       List all custom device states that have been set by using\n"
194                         "       the DEVICE_STATE dialplan function.\n";
195                 return NULL;
196         case CLI_GENERATE:
197                 return NULL;
198         }
199
200         if (a->argc != e->args)
201                 return CLI_SHOWUSAGE;
202
203         ast_cli(a->fd, "\n"
204                 "---------------------------------------------------------------------\n"
205                 "--- Custom Device States --------------------------------------------\n"
206                 "---------------------------------------------------------------------\n"
207                 "---\n");
208
209         db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
210         for (; db_entry; db_entry = db_entry->next) {
211                 const char *dev_name = strrchr(db_entry->key, '/') + 1;
212                 if (dev_name <= (const char *) 1)
213                         continue;
214                 ast_cli(a->fd, "--- Name: 'Custom:%s'  State: '%s'\n"
215                                "---\n", dev_name, db_entry->data);
216         }
217         ast_db_freetree(db_tree);
218         db_tree = NULL;
219
220         ast_cli(a->fd,
221                 "---------------------------------------------------------------------\n"
222                 "---------------------------------------------------------------------\n"
223                 "\n");
224
225         return CLI_SUCCESS;
226 }
227
228 static char *handle_cli_devstate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
229 {
230     size_t len;
231         const char *dev, *state;
232         enum ast_device_state state_val;
233
234         switch (cmd) {
235         case CLI_INIT:
236                 e->command = "devstate change";
237                 e->usage =
238                         "Usage: devstate change <device> <state>\n"
239                         "       Change a custom device to a new state.\n"
240                         "       The possible values for the state are:\n"
241                         "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
242                         "RINGINUSE | ONHOLD\n",
243                         "\n"
244                         "Examples:\n"
245                         "       devstate change Custom:mystate1 INUSE\n"
246                         "       devstate change Custom:mystate1 NOT_INUSE\n"
247                         "       \n";
248                 return NULL;
249         case CLI_GENERATE:
250         {
251                 static char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY",
252                         "UNAVAILALBE", "RINGING", "RINGINUSE", "ONHOLD", NULL };
253
254                 if (a->pos == e->args + 1)
255                         return ast_cli_complete(a->word, cmds, a->n);
256
257                 return NULL;
258         }
259         }
260
261         if (a->argc != e->args + 2)
262                 return CLI_SHOWUSAGE;
263
264         len = strlen("Custom:");
265         dev = a->argv[e->args];
266         state = a->argv[e->args + 1];
267
268         if (strncasecmp(dev, "Custom:", len)) {
269                 ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n");
270                 return CLI_FAILURE;
271         }
272
273         dev += len;
274         if (ast_strlen_zero(dev))
275                 return CLI_SHOWUSAGE;
276
277         state_val = ast_devstate_val(state);
278
279         if (state_val == AST_DEVICE_UNKNOWN)
280                 return CLI_SHOWUSAGE;
281
282         ast_cli(a->fd, "Changing %s to %s\n", dev, state);
283
284         ast_db_put(astdb_family, dev, state);
285
286         ast_devstate_changed(state_val, "Custom:%s", dev);
287
288         return CLI_SUCCESS;
289 }
290
291 static struct ast_cli_entry cli_funcdevstate_list_deprecated = AST_CLI_DEFINE(handle_cli_funcdevstate_list, "List currently known custom device states");
292 static struct ast_cli_entry cli_funcdevstate[] = {
293         AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states", .deprecate_cmd = &cli_funcdevstate_list_deprecated),
294         AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"),
295 };
296
297 static struct ast_custom_function devstate_function = {
298         .name = "DEVICE_STATE",
299         .synopsis = "Get or Set a device state",
300         .syntax = "DEVICE_STATE(device)",
301         .desc =
302         "  The DEVICE_STATE function can be used to retrieve the device state from any\n"
303         "device state provider.  For example:\n"
304         "   NoOp(SIP/mypeer has state ${DEVICE_STATE(SIP/mypeer)})\n"
305         "   NoOp(Conference number 1234 has state ${DEVICE_STATE(MeetMe:1234)})\n"
306         "\n"
307         "  The DEVICE_STATE function can also be used to set custom device state from\n"
308         "the dialplan.  The \"Custom:\" prefix must be used.  For example:\n"
309         "  Set(DEVICE_STATE(Custom:lamp1)=BUSY)\n"
310         "  Set(DEVICE_STATE(Custom:lamp2)=NOT_INUSE)\n"
311         "You can subscribe to the status of a custom device state using a hint in\n"
312         "the dialplan:\n"
313         "  exten => 1234,hint,Custom:lamp1\n"
314         "\n"
315         "  The possible values for both uses of this function are:\n"
316         "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
317         "RINGINUSE | ONHOLD\n",
318         .read = devstate_read,
319         .write = devstate_write,
320 };
321
322 static struct ast_custom_function hint_function = {
323         .name = "HINT",
324         .synopsis = "Get the devices set for a dialplan hint",
325         .syntax = "HINT(extension[@context][|options])",
326         .desc =
327         "  The HINT function can be used to retrieve the list of devices that are\n"
328         "mapped to a dialplan hint.  For example:\n"
329         "   NoOp(Hint for Extension 1234 is ${HINT(1234)})\n"
330         "Options:\n"
331         "   'n' - Retrieve name on the hint instead of list of devices\n"
332         "",
333         .read = hint_read,
334 };
335
336 static int unload_module(void)
337 {
338         int res = 0;
339
340         res |= ast_custom_function_unregister(&devstate_function);
341         res |= ast_custom_function_unregister(&hint_function);
342         res |= ast_devstate_prov_del("Custom");
343         res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
344
345         return res;
346 }
347
348 static int load_module(void)
349 {
350         int res = 0;
351         struct ast_db_entry *db_entry, *db_tree;
352
353         /* Populate the device state cache on the system with all of the currently
354          * known custom device states. */
355         db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
356         for (; db_entry; db_entry = db_entry->next) {
357                 const char *dev_name = strrchr(db_entry->key, '/') + 1;
358                 if (dev_name <= (const char *) 1)
359                         continue;
360                 ast_devstate_changed(ast_devstate_val(db_entry->data),
361                         "Custom:%s\n", dev_name);
362         }
363         ast_db_freetree(db_tree);
364         db_tree = NULL;
365
366         res |= ast_custom_function_register(&devstate_function);
367         res |= ast_custom_function_register(&hint_function);
368         res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
369         res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
370
371         return res;
372 }
373
374 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets or sets a device state in the dialplan");