42b3c68c9f7e64b3b5f6755f9c57e774a9e2a2ba
[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 function
20  *
21  * \author Kevin P. Fleming <kpfleming@digium.com>
22  * 
23  * \ingroup functions
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/module.h"
31 #include "asterisk/channel.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/utils.h"
34 #include "asterisk/app.h"
35 #include "asterisk/indications.h"
36 #include "asterisk/stringfields.h"
37
38 #define locked_copy_string(chan, dest, source, len) \
39         do { \
40                 ast_channel_lock(chan); \
41                 ast_copy_string(dest, source, len); \
42                 ast_channel_unlock(chan); \
43         } while (0)
44 #define locked_string_field_set(chan, field, source) \
45         do { \
46                 ast_channel_lock(chan); \
47                 ast_string_field_set(chan, field, source); \
48                 ast_channel_unlock(chan); \
49         } while (0)
50
51 char *transfercapability_table[0x20] = {
52         "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
53         "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
54         "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
55         "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
56
57 static int func_channel_read(struct ast_channel *chan, const char *function,
58                              char *data, char *buf, size_t len)
59 {
60         int ret = 0;
61
62         if (!strcasecmp(data, "audionativeformat"))
63                 /* use the _multiple version when chan->nativeformats holds multiple formats */
64                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
65                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
66         else if (!strcasecmp(data, "videonativeformat"))
67                 /* use the _multiple version when chan->nativeformats holds multiple formats */
68                 /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
69                 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
70         else if (!strcasecmp(data, "audioreadformat"))
71                 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
72         else if (!strcasecmp(data, "audiowriteformat"))
73                 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
74 #ifdef CHANNEL_TRACE
75         else if (!strcasecmp(data, "trace")) {
76                 ast_channel_lock(chan);
77                 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
78                 ast_channel_unlock(chan);
79         }
80 #endif
81         else if (!strcasecmp(data, "tonezone") && chan->zone)
82                 locked_copy_string(chan, buf, chan->zone->country, len);
83         else if (!strcasecmp(data, "language"))
84                 locked_copy_string(chan, buf, chan->language, len);
85         else if (!strcasecmp(data, "musicclass"))
86                 locked_copy_string(chan, buf, chan->musicclass, len);
87         else if (!strcasecmp(data, "state"))
88                 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
89         else if (!strcasecmp(data, "channeltype"))
90                 locked_copy_string(chan, buf, chan->tech->type, len);
91         else if (!strcasecmp(data, "transfercapability"))
92                 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
93         else if (!strcasecmp(data, "callgroup")) {
94                 char groupbuf[256];
95                 locked_copy_string(chan, buf,  ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
96         } else if (!chan->tech->func_channel_read
97                  || chan->tech->func_channel_read(chan, function, data, buf, len)) {
98                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
99                 ret = -1;
100         }
101
102         return ret;
103 }
104
105 static int func_channel_write(struct ast_channel *chan, const char *function,
106                               char *data, const char *value)
107 {
108         int ret = 0;
109         signed char gainset;
110
111         if (!strcasecmp(data, "language"))
112                 locked_string_field_set(chan, language, value);
113         else if (!strcasecmp(data, "musicclass"))
114                 locked_string_field_set(chan, musicclass, value);
115 #ifdef CHANNEL_TRACE
116         else if (!strcasecmp(data, "trace")) {
117                 ast_channel_lock(chan);
118                 if (ast_true(value)) 
119                         ret = ast_channel_trace_enable(chan);
120                 else if (ast_false(value)) 
121                         ret = ast_channel_trace_disable(chan);
122                 else {
123                         ret = -1;
124                         ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
125                 }
126                 ast_channel_unlock(chan);
127         }
128 #endif
129         else if (!strcasecmp(data, "tonezone")) {
130                 struct ind_tone_zone *new_zone;
131                 if (!(new_zone = ast_get_indication_zone(value))) {
132                         ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
133                         ret = -1;       
134                 } else 
135                         chan->zone = new_zone;
136         } else if (!strcasecmp(data, "callgroup"))
137                 chan->callgroup = ast_get_group(value);
138         else if (!strcasecmp(data, "txgain")) {
139                 sscanf(value, "%hhd", &gainset);
140                 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
141         } else if (!strcasecmp(data, "rxgain")) {
142                 sscanf(value, "%hhd", &gainset);
143                 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
144         } else if (!strcasecmp(data, "transfercapability")) {
145                 unsigned short i;
146                 for (i = 0; i < 0x20; i++) {
147                         if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
148                                 chan->transfercapability = i;
149                                 break;
150                         }
151                 }
152         } else if (!chan->tech->func_channel_write
153                  || chan->tech->func_channel_write(chan, function, data, value)) {
154                 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
155                                 data);
156                 ret = -1;
157         }
158
159         return ret;
160 }
161
162 static struct ast_custom_function channel_function = {
163         .name = "CHANNEL",
164         .synopsis = "Gets/sets various pieces of information about the channel.",
165         .syntax = "CHANNEL(item)",
166         .desc = "Gets/set various pieces of information about the channel.\n"
167                 "Standard items (provided by all channel technologies) are:\n"
168                 "R/O    audioreadformat    format currently being read\n"
169                 "R/O    audionativeformat  format used natively for audio\n"
170                 "R/O    audiowriteformat   format currently being written\n"
171                 "R/W    callgroup          call groups for call pickup\n"
172                 "R/O    channeltype        technology used for channel\n"
173                 "R/W    language           language for sounds played\n"
174                 "R/W    musicclass         class (from musiconhold.conf) for hold music\n"
175                 "R/W    rxgain             set rxgain level on channel drivers that support it\n"
176                 "R/O    state              state for channel\n"
177                 "R/W    tonezone           zone for indications played\n"
178                 "R/W    txgain             set txgain level on channel drivers that support it\n"
179                 "R/O    videonativeformat  format used natively for video\n"
180 #ifdef CHANNEL_TRACE
181                 "R/W    trace              whether or not context tracing is enabled\n"
182 #endif
183                 "\n"
184                 "chan_sip provides the following additional options:\n"
185                 "R/O    rtpqos             Get QOS information about the RTP stream\n"
186                 "       This option takes two additional arguments:\n"
187                 "  Argument 1:\n"
188                 "    audio                 Get data about the audio stream\n"
189                 "    video                 Get data about the video stream\n"
190                 "    text                  Get data about the text stream\n"
191                 "  Argument 2:\n"
192                 "    local_ssrc            Local SSRC (stream ID)\n"
193                 "    local_lostpackets     Local lost packets\n"
194                 "    local_jitter          Local calculated jitter\n"
195                 "    local_count           Number of received packets\n"
196                 "    remote_ssrc           Remote SSRC (stream ID)\n"
197                 "    remote_lostpackets    Remote lost packets\n"
198                 "    remote_jitter         Remote reported jitter\n"
199                 "    remote_count          Number of transmitted packets\n"
200                 "    rtt                   Round trip time\n"
201                 "    all                   All statistics (in a form suited to logging, but not for parsing)\n"
202                 "R/O    rtpdest            Get remote RTP destination information\n"
203                 "       This option takes one additional argument:\n"
204                 "  Argument 1:\n"
205                 "    audio                 Get audio destination\n"
206                 "    video                 Get video destination\n"
207                 "\n"
208                 "chan_iax2 provides the following additional options:\n"
209                 "R/W    osptoken           Get or set the OSP token information for a call\n"
210                 "\n"
211                 "Additional items may be available from the channel driver providing\n"
212                 "the channel; see its documentation for details.\n"
213                 "\n"
214                 "Any item requested that is not available on the current channel will\n"
215                 "return an empty string.\n",
216         .read = func_channel_read,
217         .write = func_channel_write,
218 };
219
220 static int unload_module(void)
221 {
222         return ast_custom_function_unregister(&channel_function);
223 }
224
225 static int load_module(void)
226 {
227         return ast_custom_function_register(&channel_function);
228 }
229
230 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan function");