func_channel.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include <regex.h>
  31. #include <ctype.h>
  32. #include "asterisk/module.h"
  33. #include "asterisk/channel.h"
  34. #include "asterisk/bridge.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. #include "asterisk/global_datastores.h"
  41. #include "asterisk/bridge_basic.h"
  42. #include "asterisk/bridge_after.h"
  43. /*** DOCUMENTATION
  44. <function name="CHANNELS" language="en_US">
  45. <synopsis>
  46. Gets the list of channels, optionally filtering by a regular expression.
  47. </synopsis>
  48. <syntax>
  49. <parameter name="regular_expression" />
  50. </syntax>
  51. <description>
  52. <para>Gets the list of channels, optionally filtering by a <replaceable>regular_expression</replaceable>. If
  53. no argument is provided, all known channels are returned. The
  54. <replaceable>regular_expression</replaceable> must correspond to
  55. the POSIX.2 specification, as shown in <emphasis>regex(7)</emphasis>. The list returned
  56. will be space-delimited.</para>
  57. </description>
  58. </function>
  59. <function name="MASTER_CHANNEL" language="en_US">
  60. <synopsis>
  61. Gets or sets variables on the master channel
  62. </synopsis>
  63. <description>
  64. <para>Allows access to the channel which created the current channel, if any. If the channel is already
  65. a master channel, then accesses local channel variables.</para>
  66. </description>
  67. </function>
  68. <function name="CHANNEL" language="en_US">
  69. <synopsis>
  70. Gets/sets various pieces of information about the channel.
  71. </synopsis>
  72. <syntax>
  73. <parameter name="item" required="true">
  74. <para>Standard items (provided by all channel technologies) are:</para>
  75. <enumlist>
  76. <enum name="amaflags">
  77. <para>R/W the Automatic Message Accounting (AMA) flags on the channel.
  78. When read from a channel, the integer value will always be returned.
  79. When written to a channel, both the string format or integer value
  80. is accepted.</para>
  81. <enumlist>
  82. <enum name="1"><para><literal>OMIT</literal></para></enum>
  83. <enum name="2"><para><literal>BILLING</literal></para></enum>
  84. <enum name="3"><para><literal>DOCUMENTATION</literal></para></enum>
  85. </enumlist>
  86. </enum>
  87. <enum name="accountcode">
  88. <para>R/W the channel's account code.</para>
  89. </enum>
  90. <enum name="audioreadformat">
  91. <para>R/O format currently being read.</para>
  92. </enum>
  93. <enum name="audionativeformat">
  94. <para>R/O format used natively for audio.</para>
  95. </enum>
  96. <enum name="audiowriteformat">
  97. <para>R/O format currently being written.</para>
  98. </enum>
  99. <enum name="dtmf_features">
  100. <para>R/W The channel's DTMF bridge features.
  101. May include one or more of 'T' 'K' 'H' 'W' and 'X' in a similar manner to options
  102. in the <literal>Dial</literal> application. When setting it, the features string
  103. must be all upper case.</para>
  104. </enum>
  105. <enum name="callgroup">
  106. <para>R/W numeric call pickup groups that this channel is a member.</para>
  107. </enum>
  108. <enum name="pickupgroup">
  109. <para>R/W numeric call pickup groups this channel can pickup.</para>
  110. </enum>
  111. <enum name="namedcallgroup">
  112. <para>R/W named call pickup groups that this channel is a member.</para>
  113. </enum>
  114. <enum name="namedpickupgroup">
  115. <para>R/W named call pickup groups this channel can pickup.</para>
  116. </enum>
  117. <enum name="channeltype">
  118. <para>R/O technology used for channel.</para>
  119. </enum>
  120. <enum name="checkhangup">
  121. <para>R/O Whether the channel is hanging up (1/0)</para>
  122. </enum>
  123. <enum name="after_bridge_goto">
  124. <para>R/W the parseable goto string indicating where the channel is
  125. expected to return to in the PBX after exiting the next bridge it joins
  126. on the condition that it doesn't hang up. The parseable goto string uses
  127. the same syntax as the <literal>Goto</literal> application.</para>
  128. </enum>
  129. <enum name="hangup_handler_pop">
  130. <para>W/O Replace the most recently added hangup handler
  131. with a new hangup handler on the channel if supplied. The
  132. assigned string is passed to the Gosub application when
  133. the channel is hung up. Any optionally omitted context
  134. and exten are supplied by the channel pushing the handler
  135. before it is pushed.</para>
  136. </enum>
  137. <enum name="hangup_handler_push">
  138. <para>W/O Push a hangup handler onto the channel hangup
  139. handler stack. The assigned string is passed to the
  140. Gosub application when the channel is hung up. Any
  141. optionally omitted context and exten are supplied by the
  142. channel pushing the handler before it is pushed.</para>
  143. </enum>
  144. <enum name="hangup_handler_wipe">
  145. <para>W/O Wipe the entire hangup handler stack and replace
  146. with a new hangup handler on the channel if supplied. The
  147. assigned string is passed to the Gosub application when
  148. the channel is hung up. Any optionally omitted context
  149. and exten are supplied by the channel pushing the handler
  150. before it is pushed.</para>
  151. </enum>
  152. <enum name="language">
  153. <para>R/W language for sounds played.</para>
  154. </enum>
  155. <enum name="musicclass">
  156. <para>R/W class (from musiconhold.conf) for hold music.</para>
  157. </enum>
  158. <enum name="name">
  159. <para>The name of the channel</para>
  160. </enum>
  161. <enum name="parkinglot">
  162. <para>R/W parkinglot for parking.</para>
  163. </enum>
  164. <enum name="rxgain">
  165. <para>R/W set rxgain level on channel drivers that support it.</para>
  166. </enum>
  167. <enum name="secure_bridge_signaling">
  168. <para>Whether or not channels bridged to this channel require secure signaling (1/0)</para>
  169. </enum>
  170. <enum name="secure_bridge_media">
  171. <para>Whether or not channels bridged to this channel require secure media (1/0)</para>
  172. </enum>
  173. <enum name="state">
  174. <para>R/O state of the channel</para>
  175. </enum>
  176. <enum name="tonezone">
  177. <para>R/W zone for indications played</para>
  178. </enum>
  179. <enum name="transfercapability">
  180. <para>R/W ISDN Transfer Capability, one of:</para>
  181. <enumlist>
  182. <enum name="SPEECH" />
  183. <enum name="DIGITAL" />
  184. <enum name="RESTRICTED_DIGITAL" />
  185. <enum name="3K1AUDIO" />
  186. <enum name="DIGITAL_W_TONES" />
  187. <enum name="VIDEO" />
  188. </enumlist>
  189. </enum>
  190. <enum name="txgain">
  191. <para>R/W set txgain level on channel drivers that support it.</para>
  192. </enum>
  193. <enum name="videonativeformat">
  194. <para>R/O format used natively for video</para>
  195. </enum>
  196. <enum name="trace">
  197. <para>R/W whether or not context tracing is enabled, only available
  198. <emphasis>if CHANNEL_TRACE is defined</emphasis>.</para>
  199. </enum>
  200. </enumlist>
  201. <para><emphasis>chan_sip</emphasis> provides the following additional options:</para>
  202. <enumlist>
  203. <enum name="peerip">
  204. <para>R/O Get the IP address of the peer.</para>
  205. </enum>
  206. <enum name="recvip">
  207. <para>R/O Get the source IP address of the peer.</para>
  208. </enum>
  209. <enum name="recvport">
  210. <para>R/O Get the source port of the peer.</para>
  211. </enum>
  212. <enum name="from">
  213. <para>R/O Get the URI from the From: header.</para>
  214. </enum>
  215. <enum name="uri">
  216. <para>R/O Get the URI from the Contact: header.</para>
  217. </enum>
  218. <enum name="useragent">
  219. <para>R/O Get the useragent.</para>
  220. </enum>
  221. <enum name="peername">
  222. <para>R/O Get the name of the peer.</para>
  223. </enum>
  224. <enum name="t38passthrough">
  225. <para>R/O <literal>1</literal> if T38 is offered or enabled in this channel,
  226. otherwise <literal>0</literal></para>
  227. </enum>
  228. <enum name="rtpqos">
  229. <para>R/O Get QOS information about the RTP stream</para>
  230. <para> This option takes two additional arguments:</para>
  231. <para> Argument 1:</para>
  232. <para> <literal>audio</literal> Get data about the audio stream</para>
  233. <para> <literal>video</literal> Get data about the video stream</para>
  234. <para> <literal>text</literal> Get data about the text stream</para>
  235. <para> Argument 2:</para>
  236. <para> <literal>local_ssrc</literal> Local SSRC (stream ID)</para>
  237. <para> <literal>local_lostpackets</literal> Local lost packets</para>
  238. <para> <literal>local_jitter</literal> Local calculated jitter</para>
  239. <para> <literal>local_maxjitter</literal> Local calculated jitter (maximum)</para>
  240. <para> <literal>local_minjitter</literal> Local calculated jitter (minimum)</para>
  241. <para> <literal>local_normdevjitter</literal>Local calculated jitter (normal deviation)</para>
  242. <para> <literal>local_stdevjitter</literal> Local calculated jitter (standard deviation)</para>
  243. <para> <literal>local_count</literal> Number of received packets</para>
  244. <para> <literal>remote_ssrc</literal> Remote SSRC (stream ID)</para>
  245. <para> <literal>remote_lostpackets</literal>Remote lost packets</para>
  246. <para> <literal>remote_jitter</literal> Remote reported jitter</para>
  247. <para> <literal>remote_maxjitter</literal> Remote calculated jitter (maximum)</para>
  248. <para> <literal>remote_minjitter</literal> Remote calculated jitter (minimum)</para>
  249. <para> <literal>remote_normdevjitter</literal>Remote calculated jitter (normal deviation)</para>
  250. <para> <literal>remote_stdevjitter</literal>Remote calculated jitter (standard deviation)</para>
  251. <para> <literal>remote_count</literal> Number of transmitted packets</para>
  252. <para> <literal>rtt</literal> Round trip time</para>
  253. <para> <literal>maxrtt</literal> Round trip time (maximum)</para>
  254. <para> <literal>minrtt</literal> Round trip time (minimum)</para>
  255. <para> <literal>normdevrtt</literal> Round trip time (normal deviation)</para>
  256. <para> <literal>stdevrtt</literal> Round trip time (standard deviation)</para>
  257. <para> <literal>all</literal> All statistics (in a form suited to logging,
  258. but not for parsing)</para>
  259. </enum>
  260. <enum name="rtpdest">
  261. <para>R/O Get remote RTP destination information.</para>
  262. <para> This option takes one additional argument:</para>
  263. <para> Argument 1:</para>
  264. <para> <literal>audio</literal> Get audio destination</para>
  265. <para> <literal>video</literal> Get video destination</para>
  266. <para> <literal>text</literal> Get text destination</para>
  267. <para> Defaults to <literal>audio</literal> if unspecified.</para>
  268. </enum>
  269. <enum name="rtpsource">
  270. <para>R/O Get source RTP destination information.</para>
  271. <para> This option takes one additional argument:</para>
  272. <para> Argument 1:</para>
  273. <para> <literal>audio</literal> Get audio destination</para>
  274. <para> <literal>video</literal> Get video destination</para>
  275. <para> <literal>text</literal> Get text destination</para>
  276. <para> Defaults to <literal>audio</literal> if unspecified.</para>
  277. </enum>
  278. </enumlist>
  279. <xi:include xpointer="xpointer(/docs/info[@name='PJSIPCHANNEL'])" />
  280. <para><emphasis>chan_iax2</emphasis> provides the following additional options:</para>
  281. <enumlist>
  282. <enum name="osptoken">
  283. <para>R/O Get the peer's osptoken.</para>
  284. </enum>
  285. <enum name="peerip">
  286. <para>R/O Get the peer's ip address.</para>
  287. </enum>
  288. <enum name="peername">
  289. <para>R/O Get the peer's username.</para>
  290. </enum>
  291. <enum name="secure_signaling">
  292. <para>R/O Get the if the IAX channel is secured.</para>
  293. </enum>
  294. <enum name="secure_media">
  295. <para>R/O Get the if the IAX channel is secured.</para>
  296. </enum>
  297. </enumlist>
  298. <para><emphasis>chan_dahdi</emphasis> provides the following additional options:</para>
  299. <enumlist>
  300. <enum name="dahdi_channel">
  301. <para>R/O DAHDI channel related to this channel.</para>
  302. </enum>
  303. <enum name="dahdi_span">
  304. <para>R/O DAHDI span related to this channel.</para>
  305. </enum>
  306. <enum name="dahdi_type">
  307. <para>R/O DAHDI channel type, one of:</para>
  308. <enumlist>
  309. <enum name="analog" />
  310. <enum name="mfc/r2" />
  311. <enum name="pri" />
  312. <enum name="pseudo" />
  313. <enum name="ss7" />
  314. </enumlist>
  315. </enum>
  316. <enum name="keypad_digits">
  317. <para>R/O PRI Keypad digits that came in with the SETUP message.</para>
  318. </enum>
  319. <enum name="reversecharge">
  320. <para>R/O PRI Reverse Charging Indication, one of:</para>
  321. <enumlist>
  322. <enum name="-1"> <para>None</para></enum>
  323. <enum name=" 1"> <para>Reverse Charging Requested</para></enum>
  324. </enumlist>
  325. </enum>
  326. <enum name="no_media_path">
  327. <para>R/O PRI Nonzero if the channel has no B channel.
  328. The channel is either on hold or a call waiting call.</para>
  329. </enum>
  330. <enum name="buffers">
  331. <para>W/O Change the channel's buffer policy (for the current call only)</para>
  332. <para>This option takes two arguments:</para>
  333. <para> Number of buffers,</para>
  334. <para> Buffer policy being one of:</para>
  335. <para> <literal>full</literal></para>
  336. <para> <literal>immediate</literal></para>
  337. <para> <literal>half</literal></para>
  338. </enum>
  339. <enum name="echocan_mode">
  340. <para>W/O Change the configuration of the active echo
  341. canceller on the channel (if any), for the current call
  342. only.</para>
  343. <para>Possible values are:</para>
  344. <para> <literal>on</literal> Normal mode (the echo canceller is actually reinitalized)</para>
  345. <para> <literal>off</literal> Disabled</para>
  346. <para> <literal>fax</literal> FAX/data mode (NLP disabled if possible, otherwise
  347. completely disabled)</para>
  348. <para> <literal>voice</literal> Voice mode (returns from FAX mode, reverting the changes that were made)</para>
  349. </enum>
  350. </enumlist>
  351. <para><emphasis>chan_ooh323</emphasis> provides the following additional options:</para>
  352. <enumlist>
  353. <enum name="faxdetect">
  354. <para>R/W Fax Detect</para>
  355. <para>Returns 0 or 1</para>
  356. <para>Write yes or no</para>
  357. </enum>
  358. <enum name="t38support">
  359. <para>R/W t38support</para>
  360. <para>Returns 0 or 1</para>
  361. <para>Write yes or no</para>
  362. </enum>
  363. <enum name="h323id_url">
  364. <para>R/0 Returns caller URL</para>
  365. </enum>
  366. <enum name="caller_h323id">
  367. <para>R/0 Returns caller h323id</para>
  368. </enum>
  369. <enum name="caller_dialeddigits">
  370. <para>R/0 Returns caller dialed digits</para>
  371. </enum>
  372. <enum name="caller_email">
  373. <para>R/0 Returns caller email</para>
  374. </enum>
  375. <enum name="callee_email">
  376. <para>R/0 Returns callee email</para>
  377. </enum>
  378. <enum name="callee_dialeddigits">
  379. <para>R/0 Returns callee dialed digits</para>
  380. </enum>
  381. <enum name="caller_url">
  382. <para>R/0 Returns caller URL</para>
  383. </enum>
  384. </enumlist>
  385. </parameter>
  386. </syntax>
  387. <description>
  388. <para>Gets/sets various pieces of information about the channel, additional <replaceable>item</replaceable> may
  389. be available from the channel driver; see its documentation for details. Any <replaceable>item</replaceable>
  390. requested that is not available on the current channel will return an empty string.</para>
  391. </description>
  392. </function>
  393. ***/
  394. #define locked_copy_string(chan, dest, source, len) \
  395. do { \
  396. ast_channel_lock(chan); \
  397. ast_copy_string(dest, source, len); \
  398. ast_channel_unlock(chan); \
  399. } while (0)
  400. #define locked_string_field_set(chan, field, source) \
  401. do { \
  402. ast_channel_lock(chan); \
  403. ast_channel_##field##_set(chan, source); \
  404. ast_channel_unlock(chan); \
  405. } while (0)
  406. static const char * const transfercapability_table[0x20] = {
  407. "SPEECH", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
  408. "DIGITAL", "RESTRICTED_DIGITAL", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
  409. "3K1AUDIO", "DIGITAL_W_TONES", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK",
  410. "VIDEO", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", "UNK", };
  411. static int func_channel_read(struct ast_channel *chan, const char *function,
  412. char *data, char *buf, size_t len)
  413. {
  414. int ret = 0;
  415. struct ast_format_cap *tmpcap;
  416. if (!chan) {
  417. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  418. return -1;
  419. }
  420. if (!strcasecmp(data, "audionativeformat")) {
  421. tmpcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  422. if (tmpcap) {
  423. struct ast_str *codec_buf = ast_str_alloca(128);
  424. ast_channel_lock(chan);
  425. ast_format_cap_append_from_cap(tmpcap, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_AUDIO);
  426. ast_channel_unlock(chan);
  427. ast_copy_string(buf, ast_format_cap_get_names(tmpcap, &codec_buf), len);
  428. ao2_ref(tmpcap, -1);
  429. }
  430. } else if (!strcasecmp(data, "videonativeformat")) {
  431. tmpcap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  432. if (tmpcap) {
  433. struct ast_str *codec_buf = ast_str_alloca(128);
  434. ast_channel_lock(chan);
  435. ast_format_cap_append_from_cap(tmpcap, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO);
  436. ast_channel_unlock(chan);
  437. ast_copy_string(buf, ast_format_cap_get_names(tmpcap, &codec_buf), len);
  438. ao2_ref(tmpcap, -1);
  439. }
  440. } else if (!strcasecmp(data, "audioreadformat")) {
  441. locked_copy_string(chan, buf, ast_format_get_name(ast_channel_readformat(chan)), len);
  442. } else if (!strcasecmp(data, "audiowriteformat")) {
  443. locked_copy_string(chan, buf, ast_format_get_name(ast_channel_writeformat(chan)), len);
  444. #ifdef CHANNEL_TRACE
  445. } else if (!strcasecmp(data, "trace")) {
  446. locked_copy_string(chan, buf, ast_channel_trace_is_enabled(chan) ? "1" : "0", len);
  447. #endif
  448. } else if (!strcasecmp(data, "tonezone") && ast_channel_zone(chan)) {
  449. locked_copy_string(chan, buf, ast_channel_zone(chan)->country, len);
  450. } else if (!strcasecmp(data, "dtmf_features")) {
  451. if (ast_bridge_features_ds_get_string(chan, buf, len)) {
  452. buf[0] = '\0';
  453. }
  454. } else if (!strcasecmp(data, "language"))
  455. locked_copy_string(chan, buf, ast_channel_language(chan), len);
  456. else if (!strcasecmp(data, "musicclass"))
  457. locked_copy_string(chan, buf, ast_channel_musicclass(chan), len);
  458. else if (!strcasecmp(data, "name")) {
  459. locked_copy_string(chan, buf, ast_channel_name(chan), len);
  460. } else if (!strcasecmp(data, "parkinglot"))
  461. locked_copy_string(chan, buf, ast_channel_parkinglot(chan), len);
  462. else if (!strcasecmp(data, "state"))
  463. locked_copy_string(chan, buf, ast_state2str(ast_channel_state(chan)), len);
  464. else if (!strcasecmp(data, "channeltype"))
  465. locked_copy_string(chan, buf, ast_channel_tech(chan)->type, len);
  466. else if (!strcasecmp(data, "accountcode"))
  467. locked_copy_string(chan, buf, ast_channel_accountcode(chan), len);
  468. else if (!strcasecmp(data, "checkhangup")) {
  469. locked_copy_string(chan, buf, ast_check_hangup(chan) ? "1" : "0", len);
  470. } else if (!strcasecmp(data, "peeraccount"))
  471. locked_copy_string(chan, buf, ast_channel_peeraccount(chan), len);
  472. else if (!strcasecmp(data, "hangupsource"))
  473. locked_copy_string(chan, buf, ast_channel_hangupsource(chan), len);
  474. else if (!strcasecmp(data, "appname") && ast_channel_appl(chan))
  475. locked_copy_string(chan, buf, ast_channel_appl(chan), len);
  476. else if (!strcasecmp(data, "appdata") && ast_channel_data(chan))
  477. locked_copy_string(chan, buf, ast_channel_data(chan), len);
  478. else if (!strcasecmp(data, "exten") && ast_channel_data(chan))
  479. locked_copy_string(chan, buf, ast_channel_exten(chan), len);
  480. else if (!strcasecmp(data, "context") && ast_channel_data(chan))
  481. locked_copy_string(chan, buf, ast_channel_context(chan), len);
  482. else if (!strcasecmp(data, "userfield") && ast_channel_data(chan))
  483. locked_copy_string(chan, buf, ast_channel_userfield(chan), len);
  484. else if (!strcasecmp(data, "channame") && ast_channel_data(chan))
  485. locked_copy_string(chan, buf, ast_channel_name(chan), len);
  486. else if (!strcasecmp(data, "linkedid")) {
  487. ast_channel_lock(chan);
  488. if (ast_strlen_zero(ast_channel_linkedid(chan))) {
  489. /* fall back on the channel's uniqueid if linkedid is unset */
  490. ast_copy_string(buf, ast_channel_uniqueid(chan), len);
  491. }
  492. else {
  493. ast_copy_string(buf, ast_channel_linkedid(chan), len);
  494. }
  495. ast_channel_unlock(chan);
  496. } else if (!strcasecmp(data, "peer")) {
  497. RAII_VAR(struct ast_channel *, p, NULL, ast_channel_cleanup);
  498. ast_channel_lock(chan);
  499. p = ast_channel_bridge_peer(chan);
  500. if (p || ast_channel_tech(chan)) /* dummy channel? if so, we hid the peer name in the language */
  501. ast_copy_string(buf, (p ? ast_channel_name(p) : ""), len);
  502. else {
  503. /* a dummy channel can still pass along bridged peer info via
  504. the BRIDGEPEER variable */
  505. const char *pname = pbx_builtin_getvar_helper(chan, "BRIDGEPEER");
  506. if (!ast_strlen_zero(pname))
  507. ast_copy_string(buf, pname, len); /* a horrible kludge, but... how else? */
  508. else
  509. buf[0] = 0;
  510. }
  511. ast_channel_unlock(chan);
  512. } else if (!strcasecmp(data, "uniqueid")) {
  513. locked_copy_string(chan, buf, ast_channel_uniqueid(chan), len);
  514. } else if (!strcasecmp(data, "transfercapability")) {
  515. locked_copy_string(chan, buf, transfercapability_table[ast_channel_transfercapability(chan) & 0x1f], len);
  516. } else if (!strcasecmp(data, "callgroup")) {
  517. char groupbuf[256];
  518. locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), ast_channel_callgroup(chan)), len);
  519. } else if (!strcasecmp(data, "pickupgroup")) {
  520. char groupbuf[256];
  521. locked_copy_string(chan, buf, ast_print_group(groupbuf, sizeof(groupbuf), ast_channel_pickupgroup(chan)), len);
  522. } else if (!strcasecmp(data, "namedcallgroup")) {
  523. struct ast_str *tmp_str = ast_str_alloca(1024);
  524. locked_copy_string(chan, buf, ast_print_namedgroups(&tmp_str, ast_channel_named_callgroups(chan)), len);
  525. } else if (!strcasecmp(data, "namedpickupgroup")) {
  526. struct ast_str *tmp_str = ast_str_alloca(1024);
  527. locked_copy_string(chan, buf, ast_print_namedgroups(&tmp_str, ast_channel_named_pickupgroups(chan)), len);
  528. } else if (!strcasecmp(data, "after_bridge_goto")) {
  529. ast_bridge_read_after_goto(chan, buf, len);
  530. } else if (!strcasecmp(data, "amaflags")) {
  531. ast_channel_lock(chan);
  532. snprintf(buf, len, "%u", ast_channel_amaflags(chan));
  533. ast_channel_unlock(chan);
  534. } else if (!strncasecmp(data, "secure_bridge_", 14)) {
  535. struct ast_datastore *ds;
  536. buf[0] = '\0';
  537. ast_channel_lock(chan);
  538. if ((ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
  539. struct ast_secure_call_store *encrypt = ds->data;
  540. if (!strcasecmp(data, "secure_bridge_signaling")) {
  541. snprintf(buf, len, "%s", encrypt->signaling ? "1" : "");
  542. } else if (!strcasecmp(data, "secure_bridge_media")) {
  543. snprintf(buf, len, "%s", encrypt->media ? "1" : "");
  544. }
  545. }
  546. ast_channel_unlock(chan);
  547. } else if (!ast_channel_tech(chan) || !ast_channel_tech(chan)->func_channel_read || ast_channel_tech(chan)->func_channel_read(chan, function, data, buf, len)) {
  548. ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n", data);
  549. ret = -1;
  550. }
  551. return ret;
  552. }
  553. static int func_channel_write_real(struct ast_channel *chan, const char *function,
  554. char *data, const char *value)
  555. {
  556. int ret = 0;
  557. signed char gainset;
  558. if (!strcasecmp(data, "language"))
  559. locked_string_field_set(chan, language, value);
  560. else if (!strcasecmp(data, "parkinglot"))
  561. locked_string_field_set(chan, parkinglot, value);
  562. else if (!strcasecmp(data, "musicclass"))
  563. locked_string_field_set(chan, musicclass, value);
  564. else if (!strcasecmp(data, "accountcode"))
  565. locked_string_field_set(chan, accountcode, value);
  566. else if (!strcasecmp(data, "userfield"))
  567. locked_string_field_set(chan, userfield, value);
  568. else if (!strcasecmp(data, "after_bridge_goto")) {
  569. if (ast_strlen_zero(value)) {
  570. ast_bridge_discard_after_goto(chan);
  571. } else {
  572. ast_bridge_set_after_go_on(chan, ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan), value);
  573. }
  574. } else if (!strcasecmp(data, "amaflags")) {
  575. ast_channel_lock(chan);
  576. if (isdigit(*value)) {
  577. int amaflags;
  578. sscanf(value, "%30d", &amaflags);
  579. ast_channel_amaflags_set(chan, amaflags);
  580. } else if (!strcasecmp(value,"OMIT")){
  581. ast_channel_amaflags_set(chan, 1);
  582. } else if (!strcasecmp(value,"BILLING")){
  583. ast_channel_amaflags_set(chan, 2);
  584. } else if (!strcasecmp(value,"DOCUMENTATION")){
  585. ast_channel_amaflags_set(chan, 3);
  586. }
  587. ast_channel_unlock(chan);
  588. } else if (!strcasecmp(data, "peeraccount"))
  589. locked_string_field_set(chan, peeraccount, value);
  590. else if (!strcasecmp(data, "hangupsource"))
  591. /* XXX - should we be forcing this here? */
  592. ast_set_hangupsource(chan, value, 0);
  593. #ifdef CHANNEL_TRACE
  594. else if (!strcasecmp(data, "trace")) {
  595. ast_channel_lock(chan);
  596. if (ast_true(value))
  597. ret = ast_channel_trace_enable(chan);
  598. else if (ast_false(value))
  599. ret = ast_channel_trace_disable(chan);
  600. else {
  601. ret = -1;
  602. ast_log(LOG_WARNING, "Invalid value for CHANNEL(trace).\n");
  603. }
  604. ast_channel_unlock(chan);
  605. }
  606. #endif
  607. else if (!strcasecmp(data, "tonezone")) {
  608. struct ast_tone_zone *new_zone;
  609. if (!(new_zone = ast_get_indication_zone(value))) {
  610. ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", value);
  611. ret = -1;
  612. } else {
  613. ast_channel_lock(chan);
  614. if (ast_channel_zone(chan)) {
  615. ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
  616. }
  617. ast_channel_zone_set(chan, ast_tone_zone_ref(new_zone));
  618. ast_channel_unlock(chan);
  619. new_zone = ast_tone_zone_unref(new_zone);
  620. }
  621. } else if (!strcasecmp(data, "dtmf_features")) {
  622. ret = ast_bridge_features_ds_set_string(chan, value);
  623. } else if (!strcasecmp(data, "callgroup")) {
  624. ast_channel_lock(chan);
  625. ast_channel_callgroup_set(chan, ast_get_group(value));
  626. ast_channel_unlock(chan);
  627. } else if (!strcasecmp(data, "pickupgroup")) {
  628. ast_channel_lock(chan);
  629. ast_channel_pickupgroup_set(chan, ast_get_group(value));
  630. ast_channel_unlock(chan);
  631. } else if (!strcasecmp(data, "namedcallgroup")) {
  632. struct ast_namedgroups *groups = ast_get_namedgroups(value);
  633. ast_channel_lock(chan);
  634. ast_channel_named_callgroups_set(chan, groups);
  635. ast_channel_unlock(chan);
  636. ast_unref_namedgroups(groups);
  637. } else if (!strcasecmp(data, "namedpickupgroup")) {
  638. struct ast_namedgroups *groups = ast_get_namedgroups(value);
  639. ast_channel_lock(chan);
  640. ast_channel_named_pickupgroups_set(chan, groups);
  641. ast_channel_unlock(chan);
  642. ast_unref_namedgroups(groups);
  643. } else if (!strcasecmp(data, "txgain")) {
  644. sscanf(value, "%4hhd", &gainset);
  645. ast_channel_setoption(chan, AST_OPTION_TXGAIN, &gainset, sizeof(gainset), 0);
  646. } else if (!strcasecmp(data, "rxgain")) {
  647. sscanf(value, "%4hhd", &gainset);
  648. ast_channel_setoption(chan, AST_OPTION_RXGAIN, &gainset, sizeof(gainset), 0);
  649. } else if (!strcasecmp(data, "transfercapability")) {
  650. unsigned short i;
  651. ast_channel_lock(chan);
  652. for (i = 0; i < 0x20; i++) {
  653. if (!strcasecmp(transfercapability_table[i], value) && strcmp(value, "UNK")) {
  654. ast_channel_transfercapability_set(chan, i);
  655. break;
  656. }
  657. }
  658. ast_channel_unlock(chan);
  659. } else if (!strcasecmp(data, "hangup_handler_pop")) {
  660. /* Pop one hangup handler before pushing the new handler. */
  661. ast_pbx_hangup_handler_pop(chan);
  662. ast_pbx_hangup_handler_push(chan, value);
  663. } else if (!strcasecmp(data, "hangup_handler_push")) {
  664. ast_pbx_hangup_handler_push(chan, value);
  665. } else if (!strcasecmp(data, "hangup_handler_wipe")) {
  666. /* Pop all hangup handlers before pushing the new handler. */
  667. while (ast_pbx_hangup_handler_pop(chan)) {
  668. }
  669. ast_pbx_hangup_handler_push(chan, value);
  670. } else if (!strncasecmp(data, "secure_bridge_", 14)) {
  671. struct ast_datastore *ds;
  672. struct ast_secure_call_store *store;
  673. if (!chan || !value) {
  674. return -1;
  675. }
  676. ast_channel_lock(chan);
  677. if (!(ds = ast_channel_datastore_find(chan, &secure_call_info, NULL))) {
  678. if (!(ds = ast_datastore_alloc(&secure_call_info, NULL))) {
  679. ast_channel_unlock(chan);
  680. return -1;
  681. }
  682. if (!(store = ast_calloc(1, sizeof(*store)))) {
  683. ast_channel_unlock(chan);
  684. ast_free(ds);
  685. return -1;
  686. }
  687. ds->data = store;
  688. ast_channel_datastore_add(chan, ds);
  689. } else {
  690. store = ds->data;
  691. }
  692. if (!strcasecmp(data, "secure_bridge_signaling")) {
  693. store->signaling = ast_true(value) ? 1 : 0;
  694. } else if (!strcasecmp(data, "secure_bridge_media")) {
  695. store->media = ast_true(value) ? 1 : 0;
  696. }
  697. ast_channel_unlock(chan);
  698. } else if (!ast_channel_tech(chan)->func_channel_write
  699. || ast_channel_tech(chan)->func_channel_write(chan, function, data, value)) {
  700. ast_log(LOG_WARNING, "Unknown or unavailable item requested: '%s'\n",
  701. data);
  702. ret = -1;
  703. }
  704. return ret;
  705. }
  706. static int func_channel_write(struct ast_channel *chan, const char *function, char *data, const char *value)
  707. {
  708. int res;
  709. ast_chan_write_info_t write_info = {
  710. .version = AST_CHAN_WRITE_INFO_T_VERSION,
  711. .write_fn = func_channel_write_real,
  712. .chan = chan,
  713. .function = function,
  714. .data = data,
  715. .value = value,
  716. };
  717. if (!chan) {
  718. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  719. return -1;
  720. }
  721. res = func_channel_write_real(chan, function, data, value);
  722. ast_channel_setoption(chan, AST_OPTION_CHANNEL_WRITE, &write_info, sizeof(write_info), 0);
  723. return res;
  724. }
  725. static struct ast_custom_function channel_function = {
  726. .name = "CHANNEL",
  727. .read = func_channel_read,
  728. .write = func_channel_write,
  729. };
  730. static int func_channels_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t maxlen)
  731. {
  732. struct ast_channel *c = NULL;
  733. regex_t re;
  734. int res;
  735. size_t buflen = 0;
  736. struct ast_channel_iterator *iter;
  737. buf[0] = '\0';
  738. if (!ast_strlen_zero(data)) {
  739. if ((res = regcomp(&re, data, REG_EXTENDED | REG_ICASE | REG_NOSUB))) {
  740. regerror(res, &re, buf, maxlen);
  741. ast_log(LOG_WARNING, "Error compiling regular expression for %s(%s): %s\n", function, data, buf);
  742. return -1;
  743. }
  744. }
  745. if (!(iter = ast_channel_iterator_all_new())) {
  746. if (!ast_strlen_zero(data)) {
  747. regfree(&re);
  748. }
  749. return -1;
  750. }
  751. while ((c = ast_channel_iterator_next(iter))) {
  752. ast_channel_lock(c);
  753. if (ast_strlen_zero(data) || regexec(&re, ast_channel_name(c), 0, NULL, 0) == 0) {
  754. size_t namelen = strlen(ast_channel_name(c));
  755. if (buflen + namelen + (ast_strlen_zero(buf) ? 0 : 1) + 1 < maxlen) {
  756. if (!ast_strlen_zero(buf)) {
  757. strcat(buf, " ");
  758. buflen++;
  759. }
  760. strcat(buf, ast_channel_name(c));
  761. buflen += namelen;
  762. } else {
  763. ast_log(LOG_WARNING, "Number of channels exceeds the available buffer space. Output will be truncated!\n");
  764. }
  765. }
  766. ast_channel_unlock(c);
  767. c = ast_channel_unref(c);
  768. }
  769. ast_channel_iterator_destroy(iter);
  770. if (!ast_strlen_zero(data)) {
  771. regfree(&re);
  772. }
  773. return 0;
  774. }
  775. static struct ast_custom_function channels_function = {
  776. .name = "CHANNELS",
  777. .read = func_channels_read,
  778. };
  779. static int func_mchan_read(struct ast_channel *chan, const char *function,
  780. char *data, struct ast_str **buf, ssize_t len)
  781. {
  782. struct ast_channel *mchan;
  783. char *template = ast_alloca(4 + strlen(data));
  784. if (!chan) {
  785. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  786. return -1;
  787. }
  788. mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
  789. sprintf(template, "${%s}", data); /* SAFE */
  790. ast_str_substitute_variables(buf, len, mchan ? mchan : chan, template);
  791. if (mchan) {
  792. ast_channel_unref(mchan);
  793. }
  794. return 0;
  795. }
  796. static int func_mchan_write(struct ast_channel *chan, const char *function,
  797. char *data, const char *value)
  798. {
  799. struct ast_channel *mchan;
  800. if (!chan) {
  801. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", function);
  802. return -1;
  803. }
  804. mchan = ast_channel_get_by_name(ast_channel_linkedid(chan));
  805. pbx_builtin_setvar_helper(mchan ? mchan : chan, data, value);
  806. if (mchan) {
  807. ast_channel_unref(mchan);
  808. }
  809. return 0;
  810. }
  811. static struct ast_custom_function mchan_function = {
  812. .name = "MASTER_CHANNEL",
  813. .read2 = func_mchan_read,
  814. .write = func_mchan_write,
  815. };
  816. static int unload_module(void)
  817. {
  818. int res = 0;
  819. res |= ast_custom_function_unregister(&channel_function);
  820. res |= ast_custom_function_unregister(&channels_function);
  821. res |= ast_custom_function_unregister(&mchan_function);
  822. return res;
  823. }
  824. static int load_module(void)
  825. {
  826. int res = 0;
  827. res |= ast_custom_function_register(&channel_function);
  828. res |= ast_custom_function_register(&channels_function);
  829. res |= ast_custom_function_register(&mchan_function);
  830. return res;
  831. }
  832. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel information dialplan functions");