bd6eaae42405283066f06e3f024f01930136f4eb
[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 /*** DOCUMENTATION
42         <function name="CHANNELS" language="en_US">
43                 <synopsis>
44                         Gets the list of channels, optionally filtering by a regular expression.
45                 </synopsis>
46                 <syntax>
47                         <parameter name="regular_expression" />
48                 </syntax>
49                 <description>
50                         <para>Gets the list of channels, optionally filtering by a <replaceable>regular_expression</replaceable>. If
51                         no argument is provided, all known channels are returned. The
52                         <replaceable>regular_expression</replaceable> must correspond to
53                         the POSIX.2 specification, as shown in <emphasis>regex(7)</emphasis>. The list returned
54                         will be space-delimited.</para>
55                 </description>
56         </function>
57         <function name="CHANNEL" language="en_US">
58                 <synopsis>
59                         Gets/sets various pieces of information about the channel.
60                 </synopsis>
61                 <syntax>
62                         <parameter name="item" required="true">
63                                 <para>Standard items (provided by all channel technologies) are:</para>
64                                 <enumlist>
65                                         <enum name="audioreadformat">
66                                                 <para>R/O format currently being read.</para>
67                                         </enum>
68                                         <enum name="audionativeformat">
69                                                 <para>R/O format used natively for audio.</para>
70                                         </enum>
71                                         <enum name="audiowriteformat">
72                                                 <para>R/O format currently being written.</para>
73                                         </enum>
74                                         <enum name="callgroup">
75                                                 <para>R/W call groups for call pickup.</para>
76                                         </enum>
77                                         <enum name="channeltype">
78                                                 <para>R/O technology used for channel.</para>
79                                         </enum>
80                                         <enum name="language">
81                                                 <para>R/W language for sounds played.</para>
82                                         </enum>
83                                         <enum name="musicclass">
84                                                 <para>R/W class (from musiconhold.conf) for hold music.</para>
85                                         </enum>
86                                         <enum name="name">
87                                                 <para>The name of the channel</para>
88                                         </enum>
89                                         <enum name="parkinglot">
90                                                 <para>R/W parkinglot for parking.</para>
91                                         </enum>
92                                         <enum name="rxgain">
93                                                 <para>R/W set rxgain level on channel drivers that support it.</para>
94                                         </enum>
95                                         <enum name="state">
96                                                 <para>R/O state for channel</para>
97                                         </enum>
98                                         <enum name="tonezone">
99                                                 <para>R/W zone for indications played</para>
100                                         </enum>
101                                         <enum name="txgain">
102                                                 <para>R/W set txgain level on channel drivers that support it.</para>
103                                         </enum>
104                                         <enum name="videonativeformat">
105                                                 <para>R/O format used natively for video</para>
106                                         </enum>
107                                         <enum name="trace">
108                                                 <para>R/W whether or not context tracing is enabled, only available
109                                                 <emphasis>if CHANNEL_TRACE is defined</emphasis>.</para>
110                                         </enum>
111                                 </enumlist>
112                                 <para><emphasis>chan_sip</emphasis> provides the following additional options:</para>
113                                 <enumlist>
114                                         <enum name="peerip">
115                                                 <para>R/O Get the IP address of the peer.</para>
116                                         </enum>
117                                         <enum name="recvip">
118                                                 <para>R/O Get the source IP address of the peer.</para>
119                                         </enum>
120                                         <enum name="from">
121                                                 <para>R/O Get the URI from the From: header.</para>
122                                         </enum>
123                                         <enum name="uri">
124                                                 <para>R/O Get the URI from the Contact: header.</para>
125                                         </enum>
126                                         <enum name="useragent">
127                                                 <para>R/O Get the useragent.</para>
128                                         </enum>
129                                         <enum name="peername">
130                                                 <para>R/O Get the name of the peer.</para>
131                                         </enum>
132                                         <enum name="t38passthrough">
133                                                 <para>R/O <literal>1</literal> if T38 is offered or enabled in this channel,
134                                                 otherwise <literal>0</literal></para>
135                                         </enum>
136                                         <enum name="rtpqos">
137                                                 <para>R/O Get QOS information about the RTP stream</para>
138                                                 <para>    This option takes two additional arguments:</para>
139                                                 <para>    Argument 1:</para>
140                                                 <para>     <literal>audio</literal>             Get data about the audio stream</para>
141                                                 <para>     <literal>video</literal>             Get data about the video stream</para>
142                                                 <para>     <literal>text</literal>              Get data about the text stream</para>
143                                                 <para>    Argument 2:</para>
144                                                 <para>     <literal>local_ssrc</literal>        Local SSRC (stream ID)</para>
145                                                 <para>     <literal>local_lostpackets</literal> Local lost packets</para>
146                                                 <para>     <literal>local_jitter</literal>      Local calculated jitter</para>
147                                                 <para>     <literal>local_maxjitter</literal>   Local calculated jitter (maximum)</para>
148                                                 <para>     <literal>local_minjitter</literal>   Local calculated jitter (minimum)</para>
149                                                 <para>     <literal>local_normdevjitter</literal>Local calculated jitter (normal deviation)</para>
150                                                 <para>     <literal>local_stdevjitter</literal> Local calculated jitter (standard deviation)</para>
151                                                 <para>     <literal>local_count</literal>       Number of received packets</para>
152                                                 <para>     <literal>remote_ssrc</literal>       Remote SSRC (stream ID)</para>
153                                                 <para>     <literal>remote_lostpackets</literal>Remote lost packets</para>
154                                                 <para>     <literal>remote_jitter</literal>     Remote reported jitter</para>
155                                                 <para>     <literal>remote_maxjitter</literal>  Remote calculated jitter (maximum)</para>
156                                                 <para>     <literal>remote_minjitter</literal>  Remote calculated jitter (minimum)</para>
157                                                 <para>     <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
158                                                 <para>     <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
159                                                 <para>     <literal>remote_count</literal>      Number of transmitted packets</para>
160                                                 <para>     <literal>remote_ssrc</literal>       Remote SSRC (stream ID)</para>
161                                                 <para>     <literal>remote_lostpackets</literal>Remote lost packets</para>
162                                                 <para>     <literal>remote_jitter</literal>     Remote reported jitter</para>
163                                                 <para>     <literal>remote_maxjitter</literal>  Remote calculated jitter (maximum)</para>
164                                                 <para>     <literal>remote_minjitter</literal>  Remote calculated jitter (minimum)</para>
165                                                 <para>     <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
166                                                 <para>     <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
167                                                 <para>     <literal>remote_count</literal>      Number of transmitted packets</para>
168                                                 <para>     <literal>rtt</literal>               Round trip time</para>
169                                                 <para>     <literal>maxrtt</literal>            Round trip time (maximum)</para>
170                                                 <para>     <literal>minrtt</literal>            Round trip time (minimum)</para>
171                                                 <para>     <literal>normdevrtt</literal>        Round trip time (normal deviation)</para>
172                                                 <para>     <literal>stdevrtt</literal>          Round trip time (standard deviation)</para>
173                                                 <para>     <literal>all</literal>               All statistics (in a form suited to logging,
174                                                 but not for parsing)</para>
175                                         </enum>
176                                         <enum name="rtpdest">
177                                                 <para>R/O Get remote RTP destination information.</para>
178                                                 <para>   This option takes one additional argument:</para>
179                                                 <para>    Argument 1:</para>
180                                                 <para>     <literal>audio</literal>             Get audio destination</para>
181                                                 <para>     <literal>video</literal>             Get video destination</para>
182                                         </enum>
183                                 </enumlist>
184                                 <para><emphasis>chan_iax2</emphasis> provides the following additional options:</para>
185                                 <enumlist>
186                                         <enum name="osptoken">
187                                                 <para>R/W Get or set the OSP token information for a call.</para>
188                                         </enum>
189                                         <enum name="peerip">
190                                                 <para>R/O Get the peer's ip address.</para>
191                                         </enum>
192                                         <enum name="peername">
193                                                 <para>R/O Get the peer's username.</para>
194                                         </enum>
195                                 </enumlist>
196                         </parameter>
197                 </syntax>
198                 <description>
199                         <para>Gets/sets various pieces of information about the channel, additional <replaceable>item</replaceable> may
200                         be available from the channel driver; see its documentation for details. Any <replaceable>item</replaceable>
201                         requested that is not available on the current channel will return an empty string.</para>
202                 </description>
203         </function>
204  ***/
205
206 #define locked_copy_string(chan, dest, source, len) \
207         do { \
208                 ast_channel_lock(chan); \
209                 ast_copy_string(dest, source, len); \
210                 ast_channel_unlock(chan); \
211         } while (0)
212 #define locked_string_field_set(chan, field, source) \
213         do { \
214                 ast_channel_lock(chan); \
215                 ast_string_field_set(chan, field, source); \
216                 ast_channel_unlock(chan); \
217         } while (0)
218
219 char *transfercapability_table[0x20] = {
220         "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
221         "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
222         "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
223         "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
224
225 static int func_channel_read(struct ast_channel *chan, const char *function,
226                              char *data, char *buf, size_t len)
227 {
228         int ret = 0;
229
230         if (!strcasecmp(data, "audionativeformat"))
231                 /* use the _multiple version when chan->nativeformats holds multiple formats */
232                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
233                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
234         else if (!strcasecmp(data, "videonativeformat"))
235                 /* use the _multiple version when chan->nativeformats holds multiple formats */
236                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
237                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
238         else if (!strcasecmp(data, "audioreadformat"))
239                 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
240         else if (!strcasecmp(data, "audiowriteformat"))
241                 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
242 #ifdef CHANNEL_TRACE
243         else if (!strcasecmp(data, "trace")) {
244                 ast_channel_lock(chan);
245                 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
246                 ast_channel_unlock(chan);
247         }
248 #endif
249         else if (!strcasecmp(data, "tonezone") && chan->zone)
250                 locked_copy_string(chan, buf, chan->zone->country, len);
251         else if (!strcasecmp(data, "language"))
252                 locked_copy_string(chan, buf, chan->language, len);
253         else if (!strcasecmp(data, "musicclass"))
254                 locked_copy_string(chan, buf, chan->musicclass, len);
255         else if (!strcasecmp(data, "name")) {
256                 locked_copy_string(chan, buf, chan->name, len);
257         } else if (!strcasecmp(data, "parkinglot"))
258                 locked_copy_string(chan, buf, chan->parkinglot, len);
259         else if (!strcasecmp(data, "state"))
260                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
261         else if (!strcasecmp(data, "channeltype"))
262                 locked_copy_string(chan, buf, chan->tech->type, len);
263         else if (!strcasecmp(data, "transfercapability"))
264                 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
265         else if (!strcasecmp(data, "callgroup")) {
266                 char groupbuf[256];
267                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
268         } else if (!chan->tech->func_channel_read
269                  || chan->tech->func_channel_read(chan, function, data, buf, len)) {
270                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
271                 ret = -1;
272         }
273
274         return ret;
275 }
276
277 static int func_channel_write(struct ast_channel *chan, const char *function,
278                               char *data, const char *value)
279 {
280         int ret = 0;
281         signed char gainset;
282
283         if (!strcasecmp(data, "language"))
284                 locked_string_field_set(chan, language, value);
285         else if (!strcasecmp(data, "parkinglot"))
286                 locked_string_field_set(chan, parkinglot, value);
287         else if (!strcasecmp(data, "musicclass"))
288                 locked_string_field_set(chan, musicclass, value);
289 #ifdef CHANNEL_TRACE
290         else if (!strcasecmp(data, "trace")) {
291                 ast_channel_lock(chan);
292                 if (ast_true(value)) 
293                         ret = ast_channel_trace_enable(chan);
294                 else if (ast_false(value)) 
295                         ret = ast_channel_trace_disable(chan);
296                 else {
297                         ret = -1;
298                         ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
299                 }
300                 ast_channel_unlock(chan);
301         }
302 #endif
303         else if (!strcasecmp(data, "tonezone")) {
304                 struct ast_tone_zone *new_zone;
305                 if (!(new_zone = ast_get_indication_zone(value))) {
306                         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
307                         ret = -1;       
308                 } else {
309                         ast_channel_lock(chan);
310                         if (chan->zone) {
311                                 chan->zone = ast_tone_zone_unref(chan->zone);
312                         }
313                         chan->zone = ast_tone_zone_ref(new_zone);
314                         ast_channel_unlock(chan);
315                         new_zone = ast_tone_zone_unref(new_zone);
316                 }
317         } else if (!strcasecmp(data, "callgroup"))
318                 chan->callgroup = ast_get_group(value);
319         else if (!strcasecmp(data, "txgain")) {
320                 sscanf(value, "%hhd", &gainset);
321                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
322         } else if (!strcasecmp(data, "rxgain")) {
323                 sscanf(value, "%hhd", &gainset);
324                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
325         } else if (!strcasecmp(data, "transfercapability")) {
326                 unsigned short i;
327                 for (i = 0; i < 0x20; i++) {
328                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
329                                 chan->transfercapability = i;
330                                 break;
331                         }
332                 }
333         } else if (!chan->tech->func_channel_write
334                  || chan->tech->func_channel_write(chan, function, data, value)) {
335                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
336                                 data);
337                 ret = -1;
338         }
339
340         return ret;
341 }
342
343 static struct ast_custom_function channel_function = {
344         .name = "CHANNEL",
345         .read = func_channel_read,
346         .write = func_channel_write,
347 };
348
349 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
350 {
351         struct ast_channel *c = NULL;
352         regex_t re;
353         int res;
354         size_t buflen = 0;
355         struct ast_channel_iterator *iter;
356
357         buf[0] = '\0';
358
359         if (!ast_strlen_zero(data)) {
360                 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
361                         regerror(res, &re, buf, maxlen);
362                         ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
363                         return -1;
364                 }
365         }
366
367         if (!(iter = ast_channel_iterator_all_new(0))) {
368                 if (!ast_strlen_zero(data)) {
369                         regfree(&re);
370                 }
371                 return -1;
372         }
373
374         while ((c = ast_channel_iterator_next(iter))) {
375                 ast_channel_lock(c);
376                 if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
377                         size_t namelen = strlen(c->name);
378                         if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
379                                 if (!ast_strlen_zero(buf)) {
380                                         strcat(buf, " ");
381                                         buflen++;
382                                 }
383                                 strcat(buf, c->name);
384                                 buflen += namelen;
385                         } else {
386                                 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space.  Output will be truncated!\n");
387                         }
388                 }
389                 ast_channel_unlock(c);
390                 c = ast_channel_unref(c);
391         }
392
393         ast_channel_iterator_destroy(iter);
394
395         if (!ast_strlen_zero(data)) {
396                 regfree(&re);
397         }
398
399         return 0;
400 }
401
402 static struct ast_custom_function channels_function = {
403         .name = "CHANNELS",
404         .read = func_channels_read,
405 };
406
407 static int unload_module(void)
408 {
409         int res = 0;
410         
411         res |= ast_custom_function_unregister(&channel_function);
412         res |= ast_custom_function_unregister(&channels_function);
413         
414         return res;
415 }
416
417 static int load_module(void)
418 {
419         int res = 0;
420         
421         res |= ast_custom_function_register(&channel_function);
422         res |= ast_custom_function_register(&channels_function);
423         
424         return res;
425 }
426
427 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");