func_channel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /*! \file
  17. *
  18. * \brief Channel info dialplan functions
  19. *
  20. * \author Kevin P. Fleming <kpfleming@digium.com>
  21. * \author Ben Winslow
  22. *
  23. * \ingroup functions
  24. */
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include <regex.h>
  28. #include "asterisk/module.h"
  29. #include "asterisk/channel.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/app.h"
  33. #include "asterisk/indications.h"
  34. #include "asterisk/stringfields.h"
  35. #define locked_copy_string(chan, dest, source, len) \
  36. do { \
  37. ast_channel_lock(chan); \
  38. ast_copy_string(dest, source, len); \
  39. ast_channel_unlock(chan); \
  40. } while (0)
  41. #define locked_string_field_set(chan, field, source) \
  42. do { \
  43. ast_channel_lock(chan); \
  44. ast_string_field_set(chan, field, source); \
  45. ast_channel_unlock(chan); \
  46. } while (0)
  47. char *transfercapability_table[0x20] = {
  48. "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
  49. "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
  50. "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
  51. "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
  52. static int func_channel_read(struct ast_channel *chan, const char *function,
  53. char *data, char *buf, size_t len)
  54. {
  55. int ret = 0;
  56. if (!strcasecmp(data, "audionativeformat"))
  57. /* use the _multiple version when chan->nativeformats holds multiple formats */
  58. /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_AUDIO_MASK); */
  59. ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_AUDIO_MASK), len);
  60. else if (!strcasecmp(data, "videonativeformat"))
  61. /* use the _multiple version when chan->nativeformats holds multiple formats */
  62. /* ast_getformatname_multiple(buf, len, chan->nativeformats & AST_FORMAT_VIDEO_MASK); */
  63. ast_copy_string(buf, ast_getformatname(chan->nativeformats & AST_FORMAT_VIDEO_MASK), len);
  64. else if (!strcasecmp(data, "audioreadformat"))
  65. ast_copy_string(buf, ast_getformatname(chan->readformat), len);
  66. else if (!strcasecmp(data, "audiowriteformat"))
  67. ast_copy_string(buf, ast_getformatname(chan->writeformat), len);
  68. #ifdef CHANNEL_TRACE
  69. else if (!strcasecmp(data, "trace")) {
  70. ast_channel_lock(chan);
  71. ast_copy_string(buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
  72. ast_channel_unlock(chan);
  73. }
  74. #endif
  75. else if (!strcasecmp(data, "tonezone") && chan->zone)
  76. locked_copy_string(chan, buf, chan->zone->country, len);
  77. else if (!strcasecmp(data, "language"))
  78. locked_copy_string(chan, buf, chan->language, len);
  79. else if (!strcasecmp(data, "musicclass"))
  80. locked_copy_string(chan, buf, chan->musicclass, len);
  81. else if (!strcasecmp(data, "parkinglot"))
  82. locked_copy_string(chan, buf, chan->parkinglot, len);
  83. else if (!strcasecmp(data, "state"))
  84. locked_copy_string(chan, buf, ast_state2str(chan->_state), len);
  85. else if (!strcasecmp(data, "channeltype"))
  86. locked_copy_string(chan, buf, chan->tech->type, len);
  87. else if (!strcasecmp(data, "transfercapability"))
  88. locked_copy_string(chan, buf, transfercapability_table[chan->transfercapability & 0x1f], len);
  89. else if (!strcasecmp(data, "callgroup")) {
  90. char groupbuf[256];
  91. locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), chan->callgroup), len);
  92. } else if (!chan->tech->func_channel_read
  93. || chan->tech->func_channel_read(chan, function, data, buf, len)) {
  94. ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
  95. ret = -1;
  96. }
  97. return ret;
  98. }
  99. static int func_channel_write(struct ast_channel *chan, const char *function,
  100. char *data, const char *value)
  101. {
  102. int ret = 0;
  103. signed char gainset;
  104. if (!strcasecmp(data, "language"))
  105. locked_string_field_set(chan, language, value);
  106. else if (!strcasecmp(data, "parkinglot"))
  107. locked_string_field_set(chan, parkinglot, value);
  108. else if (!strcasecmp(data, "musicclass"))
  109. locked_string_field_set(chan, musicclass, value);
  110. #ifdef CHANNEL_TRACE
  111. else if (!strcasecmp(data, "trace")) {
  112. ast_channel_lock(chan);
  113. if (ast_true(value))
  114. ret = ast_channel_trace_enable(chan);
  115. else if (ast_false(value))
  116. ret = ast_channel_trace_disable(chan);
  117. else {
  118. ret = -1;
  119. ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).");
  120. }
  121. ast_channel_unlock(chan);
  122. }
  123. #endif
  124. else if (!strcasecmp(data, "tonezone")) {
  125. struct tone_zone *new_zone;
  126. if (!(new_zone = ast_get_indication_zone(value))) {
  127. ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
  128. ret = -1;
  129. } else
  130. chan->zone = new_zone;
  131. } else if (!strcasecmp(data, "callgroup"))
  132. chan->callgroup = ast_get_group(value);
  133. else if (!strcasecmp(data, "txgain")) {
  134. sscanf(value, "%4hhd", &gainset);
  135. ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
  136. } else if (!strcasecmp(data, "rxgain")) {
  137. sscanf(value, "%4hhd", &gainset);
  138. ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
  139. } else if (!strcasecmp(data, "transfercapability")) {
  140. unsigned short i;
  141. for (i = 0; i < 0x20; i++) {
  142. if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
  143. chan->transfercapability = i;
  144. break;
  145. }
  146. }
  147. } else if (!chan->tech->func_channel_write
  148. || chan->tech->func_channel_write(chan, function, data, value)) {
  149. ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
  150. data);
  151. ret = -1;
  152. }
  153. return ret;
  154. }
  155. static struct ast_custom_function channel_function = {
  156. .name = "CHANNEL",
  157. .synopsis = "Gets/sets various pieces of information about the channel.",
  158. .syntax = "CHANNEL(item)",
  159. .desc = "Gets/set various pieces of information about the channel.\n"
  160. "Standard items (provided by all channel technologies) are:\n"
  161. "R/O audioreadformat format currently being read\n"
  162. "R/O audionativeformat format used natively for audio\n"
  163. "R/O audiowriteformat format currently being written\n"
  164. "R/W callgroup call groups for call pickup\n"
  165. "R/O channeltype technology used for channel\n"
  166. "R/W language language for sounds played\n"
  167. "R/W musicclass class (from musiconhold.conf) for hold music\n"
  168. "R/W parkinglot parkinglot for parking\n"
  169. "R/W rxgain set rxgain level on channel drivers that support it\n"
  170. "R/O state state for channel\n"
  171. "R/W tonezone zone for indications played\n"
  172. "R/W transfercapability ISDN transfer capability (one of SPEECH, DIGITAL,\n"
  173. " RESTRICTED_DIGITAL, 3K1AUDIO, DIGITAL_W_TONES, or VIDEO).\n"
  174. "R/W txgain set txgain level on channel drivers that support it\n"
  175. "R/O videonativeformat format used natively for video\n"
  176. #ifdef CHANNEL_TRACE
  177. "R/W trace whether or not context tracing is enabled\n"
  178. #endif
  179. "\n"
  180. "chan_sip provides the following additional options:\n"
  181. "R/O peerip Get the IP address of the peer\n"
  182. "R/O recvip Get the source IP address of the peer\n"
  183. "R/O from Get the URI from the From: header\n"
  184. "R/O uri Get the URI from the Contact: header\n"
  185. "R/O useragent Get the useragent\n"
  186. "R/O peername Get the name of the peer\n"
  187. "R/O t38passthrough 1 if T38 is offered or enabled in this channel, otherwise 0\n"
  188. "R/O rtpqos Get QOS information about the RTP stream\n"
  189. " This option takes two additional arguments:\n"
  190. " Argument 1:\n"
  191. " audio Get data about the audio stream\n"
  192. " video Get data about the video stream\n"
  193. " text Get data about the text stream\n"
  194. " Argument 2:\n"
  195. " local_ssrc Local SSRC (stream ID)\n"
  196. " local_lostpackets Local lost packets\n"
  197. " local_jitter Local calculated jitter\n"
  198. " local_maxjitter Local calculated jitter (maximum)\n"
  199. " local_minjitter Local calculated jitter (minimum)\n"
  200. " local_normdevjitter Local calculated jitter (normal deviation)\n"
  201. " local_stdevjitter Local calculated jitter (standard deviation)\n"
  202. " local_count Number of received packets\n"
  203. " remote_ssrc Remote SSRC (stream ID)\n"
  204. " remote_lostpackets Remote lost packets\n"
  205. " remote_jitter Remote reported jitter\n"
  206. " remote_maxjitter Remote calculated jitter (maximum)\n"
  207. " remote_minjitter Remote calculated jitter (minimum)\n"
  208. " remote_normdevjitter Remote calculated jitter (normal deviation)\n"
  209. " remote_stdevjitter Remote calculated jitter (standard deviation)\n"
  210. " remote_count Number of transmitted packets\n"
  211. " rtt Round trip time\n"
  212. " maxrtt Round trip time (maximum)\n"
  213. " minrtt Round trip time (minimum)\n"
  214. " normdevrtt Round trip time (normal deviation)\n"
  215. " stdevrtt Round trip time (standard deviation)\n"
  216. " all All statistics (in a form suited to logging, but not for parsing)\n"
  217. "R/O rtpdest Get remote RTP destination information\n"
  218. " This option takes one additional argument:\n"
  219. " Argument 1:\n"
  220. " audio Get audio destination\n"
  221. " video Get video destination\n"
  222. " text Get text destination\n"
  223. "\n"
  224. "chan_iax2 provides the following additional options:\n"
  225. "R/O peerip Get the peer's ip address\n"
  226. "R/O peername Get the peer's username\n"
  227. "\n"
  228. "Additional items may be available from the channel driver providing\n"
  229. "the channel; see its documentation for details.\n"
  230. "\n"
  231. "Any item requested that is not available on the current channel will\n"
  232. "return an empty string.\n",
  233. .read = func_channel_read,
  234. .write = func_channel_write,
  235. };
  236. static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
  237. {
  238. struct ast_channel *c = NULL;
  239. regex_t re;
  240. int res;
  241. size_t buflen = 0;
  242. buf[0] = '\0';
  243. if (!ast_strlen_zero(data)) {
  244. if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
  245. regerror(res, &re, buf, maxlen);
  246. ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
  247. return -1;
  248. }
  249. }
  250. for (c = ast_channel_walk_locked(NULL); c; ast_channel_unlock(c), c = ast_channel_walk_locked(c)) {
  251. if (ast_strlen_zero(data) || regexec(&re, c->name, 0, NULL, 0) == 0) {
  252. size_t namelen = strlen(c->name);
  253. if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
  254. if (!ast_strlen_zero(buf)) {
  255. strcat(buf, " ");
  256. buflen++;
  257. }
  258. strcat(buf, c->name);
  259. buflen += namelen;
  260. } else {
  261. ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space. Output will be truncated!\n");
  262. }
  263. }
  264. }
  265. if (!ast_strlen_zero(data)) {
  266. regfree(&re);
  267. }
  268. return 0;
  269. }
  270. static struct ast_custom_function channels_function = {
  271. .name = "CHANNELS",
  272. .synopsis = "Gets the list of channels, optionally filtering by a regular expression.",
  273. .syntax = "CHANNEL([regular expression])",
  274. .desc =
  275. "Gets the list of channels, optionally filtering by a regular expression. If\n"
  276. "no argument is provided, all known channels are returned. The regular\n"
  277. "expression must correspond to the POSIX.2 specification, as shown in\n"
  278. "regex(7). The list returned will be space-delimited.\n",
  279. .read = func_channels_read,
  280. };
  281. static int unload_module(void)
  282. {
  283. int res = 0;
  284. res |= ast_custom_function_unregister(&channel_function);
  285. res |= ast_custom_function_unregister(&channels_function);
  286. return res;
  287. }
  288. static int load_module(void)
  289. {
  290. int res = 0;
  291. res |= ast_custom_function_register(&channel_function);
  292. res |= ast_custom_function_register(&channels_function);
  293. return res;
  294. }
  295. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");