blocking 80167
[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
46 static const char astdb_family[] = "CustomDevstate";
47
48 static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
49 {
50         ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len);
51
52         return 0;
53 }
54
55 static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
56 {
57         size_t len = strlen("Custom:");
58
59         if (strncasecmp(data, "Custom:", len)) {
60                 ast_log(LOG_WARNING, "The DEVSTATE function can only be used to set 'Custom:' device state!\n");
61                 return -1;
62         }
63         data += len;
64         if (ast_strlen_zero(data)) {
65                 ast_log(LOG_WARNING, "DEVSTATE function called with no custom device name!\n");
66                 return -1;
67         }
68
69         ast_db_put(astdb_family, data, value);
70
71         ast_devstate_changed(ast_devstate_val(value), "Custom:%s", data);
72
73         return 0;
74 }
75
76 static enum ast_device_state custom_devstate_callback(const char *data)
77 {
78         char buf[256] = "";
79
80         ast_db_get(astdb_family, data, buf, sizeof(buf));
81
82         return ast_devstate_val(buf);
83 }
84
85 static char *cli_funcdevstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
86 {
87         struct ast_db_entry *db_entry, *db_tree;
88
89         switch (cmd) {
90         case CLI_INIT:
91                 e->command = "funcdevstate list";
92                 e->usage =
93                         "Usage: funcdevstate list\n"
94                         "       List all custom device states that have been set by using\n"
95                         "       the DEVSTATE dialplan function.\n";
96                 return NULL;
97         case CLI_GENERATE:
98                 return NULL;
99         }
100
101         if (a->argc != e->args)
102                 return CLI_SHOWUSAGE;
103
104         ast_cli(a->fd, "\n"
105                 "---------------------------------------------------------------------\n"
106                 "--- Custom Device States --------------------------------------------\n"
107                 "---------------------------------------------------------------------\n"
108                 "---\n");
109
110         db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
111         for (; db_entry; db_entry = db_entry->next) {
112                 const char *dev_name = strrchr(db_entry->key, '/') + 1;
113                 if (dev_name <= (const char *) 1)
114                         continue;
115                 ast_cli(a->fd, "--- Name: 'Custom:%s'  State: '%s'\n"
116                                "---\n", dev_name, db_entry->data);
117         }
118         ast_db_freetree(db_tree);
119         db_tree = NULL;
120
121         ast_cli(a->fd,
122                 "---------------------------------------------------------------------\n"
123                 "---------------------------------------------------------------------\n"
124                 "\n");
125
126         return CLI_SUCCESS;
127 }
128
129 static struct ast_cli_entry cli_funcdevstate[] = {
130         NEW_CLI(cli_funcdevstate_list, "List currently known custom device states"),
131 };
132
133 static struct ast_custom_function devstate_function = {
134         .name = "DEVSTATE",
135         .synopsis = "Get or Set a device state",
136         .syntax = "DEVSTATE(device)",
137         .desc =
138         "  The DEVSTATE function can be used to retrieve the device state from any\n"
139         "device state provider.  For example:\n"
140         "   NoOp(SIP/mypeer has state ${DEVSTATE(SIP/mypeer)})\n"
141         "   NoOp(Conference number 1234 has state ${DEVSTATE(MeetMe:1234)})\n"
142         "\n"
143         "  The DEVSTATE function can also be used to set custom device state from\n"
144         "the dialplan.  The \"Custom:\" prefix must be used.  For example:\n"
145         "  Set(DEVSTATE(Custom:lamp1)=BUSY)\n"
146         "  Set(DEVSTATE(Custom:lamp2)=NOT_INUSE)\n"
147         "You can subscribe to the status of a custom device state using a hint in\n"
148         "the dialplan:\n"
149         "  exten => 1234,hint,Custom:lamp1\n"
150         "\n"
151         "  The possible values for both uses of this function are:\n"
152         "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
153         "RINGINUSE | ONHOLD\n",
154         .read = devstate_read,
155         .write = devstate_write,
156 };
157
158 static int unload_module(void)
159 {
160         int res = 0;
161
162         res |= ast_custom_function_unregister(&devstate_function);
163         res |= ast_devstate_prov_del("Custom");
164         res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
165
166         return res;
167 }
168
169 static int load_module(void)
170 {
171         int res = 0;
172         struct ast_db_entry *db_entry, *db_tree;
173
174         /* Populate the device state cache on the system with all of the currently
175          * known custom device states. */
176         db_entry = db_tree = ast_db_gettree(astdb_family, NULL);
177         for (; db_entry; db_entry = db_entry->next) {
178                 const char *dev_name = strrchr(db_entry->key, '/') + 1;
179                 if (dev_name <= (const char *) 1)
180                         continue;
181                 ast_devstate_changed(ast_devstate_val(db_entry->data),
182                         "Custom:%s\n", dev_name);
183         }
184         ast_db_freetree(db_tree);
185         db_tree = NULL;
186
187         res |= ast_custom_function_register(&devstate_function);
188         res |= ast_devstate_prov_add("Custom", custom_devstate_callback);
189         res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate));
190
191         return res;
192 }
193
194 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets or sets a device state in the dialplan");