Remove the code that decided when device state changes should be cached or not.
[asterisk-bristuff.git] / funcs / func_channel.c
blobc13c668a306598fa0080d51833ca47f12fc1476b
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2006, Digium, Inc.
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.
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.
17 /*! \file
19 * \brief Channel info dialplan functions
21 * \author Kevin P. Fleming <kpfleming@digium.com>
22 * \author Ben Winslow
24 * \ingroup functions
27 #include "asterisk.h"
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <regex.h>
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"
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)
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", };
60 static int func_channel_read(struct ast_channel *chan, const char *function,
61 char *data, char *buf, size_t len)
63 int ret = 0;
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);
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;
107 return ret;
110 static int func_channel_write(struct ast_channel *chan, const char *function,
111 char *data, const char *value)
113 int ret = 0;
114 signed char gainset;
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).");
133 ast_channel_unlock(chan);
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;
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;
166 return ret;
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 peerip Get the IP address of the peer\n"
194 "R/O recvip Get the source IP address of the peer\n"
195 "R/O from Get the URI from the From: header\n"
196 "R/O uri Get the URI from the Contact: header\n"
197 "R/O useragent Get the useragent\n"
198 "R/O peername Get the name of the peer\n"
199 "R/O t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
200 "R/O rtpqos Get QOS information about the RTP stream\n"
201 " This option takes two additional arguments:\n"
202 " Argument 1:\n"
203 " audio Get data about the audio stream\n"
204 " video Get data about the video stream\n"
205 " text Get data about the text stream\n"
206 " Argument 2:\n"
207 " local_ssrc Local SSRC (stream ID)\n"
208 " local_lostpackets Local lost packets\n"
209 " local_jitter Local calculated jitter\n"
210 " local_maxjitter Local calculated jitter (maximum)\n"
211 " local_minjitter Local calculated jitter (minimum)\n"
212 " local_normdevjitter Local calculated jitter (normal deviation)\n"
213 " local_stdevjitter Local calculated jitter (standard deviation)\n"
214 " local_count Number of received packets\n"
215 " remote_ssrc Remote SSRC (stream ID)\n"
216 " remote_lostpackets Remote lost packets\n"
217 " remote_jitter Remote reported jitter\n"
218 " remote_maxjitter Remote calculated jitter (maximum)\n"
219 " remote_minjitter Remote calculated jitter (minimum)\n"
220 " remote_normdevjitter Remote calculated jitter (normal deviation)\n"
221 " remote_stdevjitter Remote calculated jitter (standard deviation)\n"
222 " remote_count Number of transmitted packets\n"
223 " rtt Round trip time\n"
224 " maxrtt Round trip time (maximum)\n"
225 " minrtt Round trip time (minimum)\n"
226 " normdevrtt Round trip time (normal deviation)\n"
227 " stdevrtt Round trip time (standard deviation)\n"
228 " all All statistics (in a form suited to logging, but not for parsing)\n"
229 "R/O rtpdest Get remote RTP destination information\n"
230 " This option takes one additional argument:\n"
231 " Argument 1:\n"
232 " audio Get audio destination\n"
233 " video Get video destination\n"
234 "\n"
235 "chan_iax2 provides the following additional options:\n"
236 "R/W osptoken Get or set the OSP token information for a call\n"
237 "R/O peerip Get the peer's ip address\n"
238 "R/O peername Get the peer's username\n"
239 "\n"
240 "Additional items may be available from the channel driver providing\n"
241 "the channel; see its documentation for details.\n"
242 "\n"
243 "Any item requested that is not available on the current channel will\n"
244 "return an empty string.\n",
245 .read = func_channel_read,
246 .write = func_channel_write,
249 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
251 struct ast_channel *c = NULL;
252 regex_t re;
253 int res;
254 size_t buflen = 0;
256 buf[0] = '\0';
258 if (!ast_strlen_zero(data)) {
259 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
260 regerror(res, &re, buf, maxlen);
261 ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
262 return -1;
266 for (c = ast_channel_walk_locked(NULL); c; ast_channel_unlock(c), c = ast_channel_walk_locked(c)) {
267 if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
268 size_t namelen = strlen(c->name);
269 if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
270 if (!ast_strlen_zero(buf)) {
271 strcat(buf, " ");
272 buflen++;
274 strcat(buf, c->name);
275 buflen += namelen;
276 } else {
277 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space. Output will be truncated!\n");
282 if (!ast_strlen_zero(data)) {
283 regfree(&re);
286 return 0;
289 static struct ast_custom_function channels_function = {
290 .name = "CHANNELS",
291 .synopsis = "Gets the list of channels, optionally filtering by a regular expression.",
292 .syntax = "CHANNEL([regular expression])",
293 .desc =
294 "Gets the list of channels, optionally filtering by a regular expression. If\n"
295 "no argument is provided, all known channels are returned. The regular\n"
296 "expression must correspond to the POSIX.2 specification, as shown in\n"
297 "regex(7). The list returned will be space-delimited.\n",
298 .read = func_channels_read,
301 static int unload_module(void)
303 int res = 0;
305 res |= ast_custom_function_unregister(&channel_function);
306 res |= ast_custom_function_unregister(&channels_function);
308 return res;
311 static int load_module(void)
313 int res = 0;
315 res |= ast_custom_function_register(&channel_function);
316 res |= ast_custom_function_register(&channels_function);
318 return res;
321 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");