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