app_senddtmf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief App to send DTMF digits
  21. *
  22. * \author Mark Spencer <markster@digium.com>
  23. *
  24. * \ingroup applications
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/pbx.h"
  32. #include "asterisk/module.h"
  33. #include "asterisk/app.h"
  34. #include "asterisk/manager.h"
  35. #include "asterisk/channel.h"
  36. /*** DOCUMENTATION
  37. <application name="SendDTMF" language="en_US">
  38. <synopsis>
  39. Sends arbitrary DTMF digits
  40. </synopsis>
  41. <syntax>
  42. <parameter name="digits" required="true">
  43. <para>List of digits 0-9,*#,a-d,A-D to send also w for a half second pause,
  44. W for a one second pause, and f or F for a flash-hook if the channel supports
  45. flash-hook.</para>
  46. </parameter>
  47. <parameter name="timeout_ms" required="false">
  48. <para>Amount of time to wait in ms between tones. (defaults to .25s)</para>
  49. </parameter>
  50. <parameter name="duration_ms" required="false">
  51. <para>Duration of each digit</para>
  52. </parameter>
  53. <parameter name="channel" required="false">
  54. <para>Channel where digits will be played</para>
  55. </parameter>
  56. </syntax>
  57. <description>
  58. <para>It will send all digits or terminate if it encounters an error.</para>
  59. </description>
  60. <see-also>
  61. <ref type="application">Read</ref>
  62. </see-also>
  63. </application>
  64. <manager name="PlayDTMF" language="en_US">
  65. <synopsis>
  66. Play DTMF signal on a specific channel.
  67. </synopsis>
  68. <syntax>
  69. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  70. <parameter name="Channel" required="true">
  71. <para>Channel name to send digit to.</para>
  72. </parameter>
  73. <parameter name="Digit" required="true">
  74. <para>The DTMF digit to play.</para>
  75. </parameter>
  76. <parameter name="Duration" required="false">
  77. <para>The duration, in milliseconds, of the digit to be played.</para>
  78. </parameter>
  79. </syntax>
  80. <description>
  81. <para>Plays a dtmf digit on the specified channel.</para>
  82. </description>
  83. </manager>
  84. ***/
  85. static const char senddtmf_name[] = "SendDTMF";
  86. static int senddtmf_exec(struct ast_channel *chan, const char *vdata)
  87. {
  88. int res;
  89. char *data;
  90. int dinterval = 0, duration = 0;
  91. struct ast_channel *chan_found = NULL;
  92. struct ast_channel *chan_dest = chan;
  93. struct ast_channel *chan_autoservice = NULL;
  94. AST_DECLARE_APP_ARGS(args,
  95. AST_APP_ARG(digits);
  96. AST_APP_ARG(dinterval);
  97. AST_APP_ARG(duration);
  98. AST_APP_ARG(channel);
  99. );
  100. if (ast_strlen_zero(vdata)) {
  101. ast_log(LOG_WARNING, "SendDTMF requires an argument\n");
  102. return 0;
  103. }
  104. data = ast_strdupa(vdata);
  105. AST_STANDARD_APP_ARGS(args, data);
  106. if (ast_strlen_zero(args.digits)) {
  107. ast_log(LOG_WARNING, "The digits argument is required (0-9,*#,a-d,A-D,wfF)\n");
  108. return 0;
  109. }
  110. if (!ast_strlen_zero(args.dinterval)) {
  111. ast_app_parse_timelen(args.dinterval, &dinterval, TIMELEN_MILLISECONDS);
  112. }
  113. if (!ast_strlen_zero(args.duration)) {
  114. ast_app_parse_timelen(args.duration, &duration, TIMELEN_MILLISECONDS);
  115. }
  116. if (!ast_strlen_zero(args.channel)) {
  117. chan_found = ast_channel_get_by_name(args.channel);
  118. if (!chan_found) {
  119. ast_log(LOG_WARNING, "No such channel: %s\n", args.channel);
  120. return 0;
  121. }
  122. chan_dest = chan_found;
  123. if (chan_found != chan) {
  124. chan_autoservice = chan;
  125. }
  126. }
  127. res = ast_dtmf_stream(chan_dest, chan_autoservice, args.digits,
  128. dinterval <= 0 ? 250 : dinterval, duration);
  129. if (chan_found) {
  130. ast_channel_unref(chan_found);
  131. }
  132. return chan_autoservice ? 0 : res;
  133. }
  134. static int manager_play_dtmf(struct mansession *s, const struct message *m)
  135. {
  136. const char *channel = astman_get_header(m, "Channel");
  137. const char *digit = astman_get_header(m, "Digit");
  138. const char *duration = astman_get_header(m, "Duration");
  139. struct ast_channel *chan;
  140. unsigned int duration_ms = 0;
  141. if (!(chan = ast_channel_get_by_name(channel))) {
  142. astman_send_error(s, m, "Channel not found");
  143. return 0;
  144. }
  145. if (ast_strlen_zero(digit)) {
  146. astman_send_error(s, m, "No digit specified");
  147. chan = ast_channel_unref(chan);
  148. return 0;
  149. }
  150. if (!ast_strlen_zero(duration) && (sscanf(duration, "%30u", &duration_ms) != 1)) {
  151. astman_send_error(s, m, "Could not convert Duration parameter");
  152. chan = ast_channel_unref(chan);
  153. return 0;
  154. }
  155. ast_senddigit_external(chan, *digit, duration_ms);
  156. chan = ast_channel_unref(chan);
  157. astman_send_ack(s, m, "DTMF successfully queued");
  158. return 0;
  159. }
  160. static int unload_module(void)
  161. {
  162. int res;
  163. res = ast_unregister_application(senddtmf_name);
  164. res |= ast_manager_unregister("PlayDTMF");
  165. return res;
  166. }
  167. static int load_module(void)
  168. {
  169. int res;
  170. res = ast_manager_register_xml("PlayDTMF", EVENT_FLAG_CALL, manager_play_dtmf);
  171. res |= ast_register_application_xml(senddtmf_name, senddtmf_exec);
  172. return res;
  173. }
  174. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send DTMF digits Application");