Add a function, CHANNELS(), which retrieves a list of all active channels.
[asterisk/asterisk.git] / funcs / func_channel.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2006, Digium, Inc.
5  *
6  * See http://www.asterisk.org for more information about
7  * the Asterisk project. Please do not directly contact
8  * any of the maintainers of this project for assistance;
9  * the project provides a web site, mailing lists and IRC
10  * channels for your use.
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License Version 2. See the LICENSE file
14  * at the top of the source tree.
15  */
16
17 /*! \file
18  *
19  * \brief Channel info dialplan functions
20  *
21  * \author Kevin P. Fleming <kpfleming@digium.com>
22  * \author Ben Winslow
23  * 
24  * \ingroup functions
25  */
26
27 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include <regex.h>
32
33 #include "asterisk/module.h"
34 #include "asterisk/channel.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/utils.h"
37 #include "asterisk/app.h"
38 #include "asterisk/indications.h"
39 #include "asterisk/stringfields.h"
40
41 #define locked_copy_string(chan, dest, source, len) \
42         do { \
43                 ast_channel_lock(chan); \
44                 ast_copy_string(dest, source, len); \
45                 ast_channel_unlock(chan); \
46         } while (0)
47 #define locked_string_field_set(chan, field, source) \
48         do { \
49                 ast_channel_lock(chan); \
50                 ast_string_field_set(chan, field, source); \
51                 ast_channel_unlock(chan); \
52         } while (0)
53
54 char *transfercapability_table[0x20] = {
55         "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
56         "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
57         "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
58         "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
59
60 static int func_channel_read(struct ast_channel *chan, const char *function,
61                              char *data, char *buf, size_t len)
62 {
63         int ret = 0;
64
65         if (!strcasecmp(data, "audionativeformat"))
66                 /* use the _multiple version when chan->nativeformats holds multiple formats */
67                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
68                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
69         else if (!strcasecmp(data, "videonativeformat"))
70                 /* use the _multiple version when chan->nativeformats holds multiple formats */
71                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
72                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
73         else if (!strcasecmp(data, "audioreadformat"))
74                 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
75         else if (!strcasecmp(data, "audiowriteformat"))
76                 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
77 #ifdef CHANNEL_TRACE
78         else if (!strcasecmp(data, "trace")) {
79                 ast_channel_lock(chan);
80                 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
81                 ast_channel_unlock(chan);
82         }
83 #endif
84         else if (!strcasecmp(data, "tonezone") && chan->zone)
85                 locked_copy_string(chan, buf, chan->zone->country, len);
86         else if (!strcasecmp(data, "language"))
87                 locked_copy_string(chan, buf, chan->language, len);
88         else if (!strcasecmp(data, "musicclass"))
89                 locked_copy_string(chan, buf, chan->musicclass, len);
90         else if (!strcasecmp(data, "parkinglot"))
91                 locked_copy_string(chan, buf, chan->parkinglot, len);
92         else if (!strcasecmp(data, "state"))
93                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
94         else if (!strcasecmp(data, "channeltype"))
95                 locked_copy_string(chan, buf, chan->tech->type, len);
96         else if (!strcasecmp(data, "transfercapability"))
97                 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
98         else if (!strcasecmp(data, "callgroup")) {
99                 char groupbuf[256];
100                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
101         } else if (!chan->tech->func_channel_read
102                  || chan->tech->func_channel_read(chan, function, data, buf, len)) {
103                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
104                 ret = -1;
105         }
106
107         return ret;
108 }
109
110 static int func_channel_write(struct ast_channel *chan, const char *function,
111                               char *data, const char *value)
112 {
113         int ret = 0;
114         signed char gainset;
115
116         if (!strcasecmp(data, "language"))
117                 locked_string_field_set(chan, language, value);
118         else if (!strcasecmp(data, "parkinglot"))
119                 locked_string_field_set(chan, parkinglot, value);
120         else if (!strcasecmp(data, "musicclass"))
121                 locked_string_field_set(chan, musicclass, value);
122 #ifdef CHANNEL_TRACE
123         else if (!strcasecmp(data, "trace")) {
124                 ast_channel_lock(chan);
125                 if (ast_true(value)) 
126                         ret = ast_channel_trace_enable(chan);
127                 else if (ast_false(value)) 
128                         ret = ast_channel_trace_disable(chan);
129                 else {
130                         ret = -1;
131                         ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
132                 }
133                 ast_channel_unlock(chan);
134         }
135 #endif
136         else if (!strcasecmp(data, "tonezone")) {
137                 struct ind_tone_zone *new_zone;
138                 if (!(new_zone = ast_get_indication_zone(value))) {
139                         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
140                         ret = -1;       
141                 } else 
142                         chan->zone = new_zone;
143         } else if (!strcasecmp(data, "callgroup"))
144                 chan->callgroup = ast_get_group(value);
145         else if (!strcasecmp(data, "txgain")) {
146                 sscanf(value, "%hhd", &gainset);
147                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
148         } else if (!strcasecmp(data, "rxgain")) {
149                 sscanf(value, "%hhd", &gainset);
150                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
151         } else if (!strcasecmp(data, "transfercapability")) {
152                 unsigned short i;
153                 for (i = 0; i < 0x20; i++) {
154                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
155                                 chan->transfercapability = i;
156                                 break;
157                         }
158                 }
159         } else if (!chan->tech->func_channel_write
160                  || chan->tech->func_channel_write(chan, function, data, value)) {
161                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
162                                 data);
163                 ret = -1;
164         }
165
166         return ret;
167 }
168
169 static struct ast_custom_function channel_function = {
170         .name = "CHANNEL",
171         .synopsis = "Gets/sets various pieces of information about the channel.",
172         .syntax = "CHANNEL(item)",
173         .desc = "Gets/set various pieces of information about the channel.\n"
174                 "Standard items (provided by all channel technologies) are:\n"
175                 "R/O    audioreadformat    format currently being read\n"
176                 "R/O    audionativeformat  format used natively for audio\n"
177                 "R/O    audiowriteformat   format currently being written\n"
178                 "R/W    callgroup          call groups for call pickup\n"
179                 "R/O    channeltype        technology used for channel\n"
180                 "R/W    language           language for sounds played\n"
181                 "R/W    musicclass         class (from musiconhold.conf) for hold music\n"
182                 "R/W    parkinglot         parkinglot for parking\n"
183                 "R/W    rxgain             set rxgain level on channel drivers that support it\n"
184                 "R/O    state              state for channel\n"
185                 "R/W    tonezone           zone for indications played\n"
186                 "R/W    txgain             set txgain level on channel drivers that support it\n"
187                 "R/O    videonativeformat  format used natively for video\n"
188 #ifdef CHANNEL_TRACE
189                 "R/W    trace              whether or not context tracing is enabled\n"
190 #endif
191                 "\n"
192                 "chan_sip provides the following additional options:\n"
193                 "R/O    rtpqos             Get QOS information about the RTP stream\n"
194                 "       This option takes two additional arguments:\n"
195                 "  Argument 1:\n"
196                 "    audio                 Get data about the audio stream\n"
197                 "    video                 Get data about the video stream\n"
198                 "    text                  Get data about the text stream\n"
199                 "  Argument 2:\n"
200                 "    local_ssrc            Local SSRC (stream ID)\n"
201                 "    local_lostpackets     Local lost packets\n"
202                 "    local_jitter          Local calculated jitter\n"
203                 "    local_count           Number of received packets\n"
204                 "    remote_ssrc           Remote SSRC (stream ID)\n"
205                 "    remote_lostpackets    Remote lost packets\n"
206                 "    remote_jitter         Remote reported jitter\n"
207                 "    remote_count          Number of transmitted packets\n"
208                 "    rtt                   Round trip time\n"
209                 "    all                   All statistics (in a form suited to logging, but not for parsing)\n"
210                 "R/O    rtpdest            Get remote RTP destination information\n"
211                 "       This option takes one additional argument:\n"
212                 "  Argument 1:\n"
213                 "    audio                 Get audio destination\n"
214                 "    video                 Get video destination\n"
215                 "\n"
216                 "chan_iax2 provides the following additional options:\n"
217                 "R/W    osptoken           Get or set the OSP token information for a call\n"
218                 "\n"
219                 "Additional items may be available from the channel driver providing\n"
220                 "the channel; see its documentation for details.\n"
221                 "\n"
222                 "Any item requested that is not available on the current channel will\n"
223                 "return an empty string.\n",
224         .read = func_channel_read,
225         .write = func_channel_write,
226 };
227
228 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
229 {
230         struct ast_channel *c = NULL;
231         regex_t re;
232         int res;
233         size_t buflen = 0;
234         
235         buf[0] = '\0';
236
237         if (!ast_strlen_zero(data)) {
238                 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
239                         regerror(res, &re, buf, maxlen);
240                         ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
241                         return -1;
242                 }
243         }
244
245         for (c = ast_channel_walk_locked(NULL); c; ast_channel_unlock(c), c = ast_channel_walk_locked(c)) {
246                 if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
247                         size_t namelen = strlen(c->name);
248                         if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
249                                 if (!ast_strlen_zero(buf)) {
250                                         strcat(buf, " ");
251                                         buflen++;
252                                 }
253                                 strcat(buf, c->name);
254                                 buflen += namelen;
255                         } else {
256                                 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space.  Output will be truncated!\n");
257                         }
258                 }
259         }
260
261         if (!ast_strlen_zero(data)) {
262                 regfree(&re);
263         }
264
265         return 0;
266 }
267
268 static struct ast_custom_function channels_function = {
269         .name = "CHANNELS",
270         .synopsis = "Gets the list of channels, optionally filtering by a regular expression.",
271         .syntax = "CHANNEL([regular expression])",
272         .desc =
273 "Gets the list of channels, optionally filtering by a regular expression.  If\n"
274 "no argument is provided, all known channels are returned.  The regular\n"
275 "expression must correspond to the POSIX.2 specification, as shown in\n"
276 "regex(7).  The list returned will be space-delimited.\n",
277         .read = func_channels_read,
278 };
279
280 static int unload_module(void)
281 {
282         int res = 0;
283         
284         res |= ast_custom_function_unregister(&channel_function);
285         res |= ast_custom_function_unregister(&channels_function);
286         
287         return res;
288 }
289
290 static int load_module(void)
291 {
292         int res = 0;
293         
294         res |= ast_custom_function_register(&channel_function);
295         res |= ast_custom_function_register(&channels_function);
296         
297         return res;
298 }
299
300 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");