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