00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "asterisk.h"
00028
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 314205 $")
00030
00031 #include <regex.h>
00032
00033 #include "asterisk/module.h"
00034 #include "asterisk/channel.h"
00035 #include "asterisk/pbx.h"
00036 #include "asterisk/utils.h"
00037 #include "asterisk/app.h"
00038 #include "asterisk/indications.h"
00039 #include "asterisk/stringfields.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 #define locked_copy_string(chan, dest, source, len) \
00205 do { \
00206 ast_channel_lock(chan); \
00207 ast_copy_string(dest, source, len); \
00208 ast_channel_unlock(chan); \
00209 } while (0)
00210 #define locked_string_field_set(chan, field, source) \
00211 do { \
00212 ast_channel_lock(chan); \
00213 ast_string_field_set(chan, field, source); \
00214 ast_channel_unlock(chan); \
00215 } while (0)
00216
00217 char *transfercapability_table[0x20] = {
00218 "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
00219 "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
00220 "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
00221 "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
00222
00223 static int func_channel_read(struct ast_channel *chan, const char *function,
00224 char *data, char *buf, size_t len)
00225 {
00226 int ret = 0;
00227
00228 if (!strcasecmp(data, "audionativeformat"))
00229
00230
00231 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
00232 else if (!strcasecmp(data, "videonativeformat"))
00233
00234
00235 ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
00236 else if (!strcasecmp(data, "audioreadformat"))
00237 ast_copy_string(buf, ast_getformatname(chan->readformat), len);
00238 else if (!strcasecmp(data, "audiowriteformat"))
00239 ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
00240 #ifdef CHANNEL_TRACE
00241 else if (!strcasecmp(data, "trace")) {
00242 ast_channel_lock(chan);
00243 ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
00244 ast_channel_unlock(chan);
00245 }
00246 #endif
00247 else if (!strcasecmp(data, "tonezone") && chan->zone)
00248 locked_copy_string(chan, buf, chan->zone->country, len);
00249 else if (!strcasecmp(data, "language"))
00250 locked_copy_string(chan, buf, chan->language, len);
00251 else if (!strcasecmp(data, "musicclass"))
00252 locked_copy_string(chan, buf, chan->musicclass, len);
00253 else if (!strcasecmp(data, "parkinglot"))
00254 locked_copy_string(chan, buf, chan->parkinglot, len);
00255 else if (!strcasecmp(data, "state"))
00256 locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
00257 else if (!strcasecmp(data, "channeltype"))
00258 locked_copy_string(chan, buf, chan->tech->type, len);
00259 else if (!strcasecmp(data, "transfercapability"))
00260 locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
00261 else if (!strcasecmp(data, "callgroup")) {
00262 char groupbuf[256];
00263 locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
00264 } else if (!chan->tech->func_channel_read
00265 || chan->tech->func_channel_read(chan, function, data, buf, len)) {
00266 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
00267 ret = -1;
00268 }
00269
00270 return ret;
00271 }
00272
00273 static int func_channel_write_real(struct ast_channel *chan, const char *function,
00274 char *data, const char *value)
00275 {
00276 int ret = 0;
00277 signed char gainset;
00278
00279 if (!strcasecmp(data, "language"))
00280 locked_string_field_set(chan, language, value);
00281 else if (!strcasecmp(data, "parkinglot"))
00282 locked_string_field_set(chan, parkinglot, value);
00283 else if (!strcasecmp(data, "musicclass"))
00284 locked_string_field_set(chan, musicclass, value);
00285 #ifdef CHANNEL_TRACE
00286 else if (!strcasecmp(data, "trace")) {
00287 ast_channel_lock(chan);
00288 if (ast_true(value))
00289 ret = ast_channel_trace_enable(chan);
00290 else if (ast_false(value))
00291 ret = ast_channel_trace_disable(chan);
00292 else {
00293 ret = -1;
00294 ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
00295 }
00296 ast_channel_unlock(chan);
00297 }
00298 #endif
00299 else if (!strcasecmp(data, "tonezone")) {
00300 struct ast_tone_zone *new_zone;
00301 if (!(new_zone = ast_get_indication_zone(value))) {
00302 ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
00303 ret = -1;
00304 } else {
00305 ast_channel_lock(chan);
00306 if (chan->zone) {
00307 chan->zone = ast_tone_zone_unref(chan->zone);
00308 }
00309 chan->zone = ast_tone_zone_ref(new_zone);
00310 ast_channel_unlock(chan);
00311 new_zone = ast_tone_zone_unref(new_zone);
00312 }
00313 } else if (!strcasecmp(data, "callgroup"))
00314 chan->callgroup = ast_get_group(value);
00315 else if (!strcasecmp(data, "txgain")) {
00316 sscanf(value, "%4hhd", &gainset);
00317 ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
00318 } else if (!strcasecmp(data, "rxgain")) {
00319 sscanf(value, "%4hhd", &gainset);
00320 ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
00321 } else if (!strcasecmp(data, "transfercapability")) {
00322 unsigned short i;
00323 for (i = 0; i < 0x20; i++) {
00324 if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
00325 chan->transfercapability = i;
00326 break;
00327 }
00328 }
00329 } else if (!chan->tech->func_channel_write
00330 || chan->tech->func_channel_write(chan, function, data, value)) {
00331 ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
00332 data);
00333 ret = -1;
00334 }
00335
00336 return ret;
00337 }
00338
00339 static int func_channel_write(struct ast_channel *chan, const char *function, char *data, const char *value)
00340 {
00341 int res;
00342 ast_chan_write_info_t write_info = {
00343 .version = AST_CHAN_WRITE_INFO_T_VERSION,
00344 .write_fn = func_channel_write_real,
00345 .chan = chan,
00346 .function = function,
00347 .data = data,
00348 .value = value,
00349 };
00350
00351 res = func_channel_write_real(chan, function, data, value);
00352 ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);
00353
00354 return res;
00355 }
00356
00357 static struct ast_custom_function channel_function = {
00358 .name = "CHANNEL",
00359 .read = func_channel_read,
00360 .write = func_channel_write,
00361 };
00362
00363 static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
00364 {
00365 struct ast_channel *c = NULL;
00366 regex_t re;
00367 int res;
00368 size_t buflen = 0;
00369
00370 buf[0] = '\0';
00371
00372 if (!ast_strlen_zero(data)) {
00373 if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
00374 regerror(res, &re, buf, maxlen);
00375 ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
00376 return -1;
00377 }
00378 }
00379
00380 for (c = ast_channel_walk_locked(NULL); c; ast_channel_unlock(c), c = ast_channel_walk_locked(c)) {
00381 if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
00382 size_t namelen = strlen(c->name);
00383 if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
00384 if (!ast_strlen_zero(buf)) {
00385 strcat(buf, " ");
00386 buflen++;
00387 }
00388 strcat(buf, c->name);
00389 buflen += namelen;
00390 } else {
00391 ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space. Output will be truncated!\n");
00392 }
00393 }
00394 }
00395
00396 if (!ast_strlen_zero(data)) {
00397 regfree(&re);
00398 }
00399
00400 return 0;
00401 }
00402
00403 static struct ast_custom_function channels_function = {
00404 .name = "CHANNELS",
00405 .read = func_channels_read,
00406 };
00407
00408 static int unload_module(void)
00409 {
00410 int res = 0;
00411
00412 res |= ast_custom_function_unregister(&channel_function);
00413 res |= ast_custom_function_unregister(&channels_function);
00414
00415 return res;
00416 }
00417
00418 static int load_module(void)
00419 {
00420 int res = 0;
00421
00422 res |= ast_custom_function_register(&channel_function);
00423 res |= ast_custom_function_register(&channels_function);
00424
00425 return res;
00426 }
00427
00428 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");