Merged revisions 280161 via svnmerge from
[asterisk/asterisk.git] / apps / app_senddtmf.c
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
19 /*! \file
20  *
21  * \brief App to send DTMF digits
22  *
23  * \author Mark Spencer <markster@digium.com>
24  * 
25  * \ingroup applications
26  */
27  
28 #include "asterisk.h"
29
30 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31
32 #include "asterisk/pbx.h"
33 #include "asterisk/module.h"
34 #include "asterisk/app.h"
35 #include "asterisk/manager.h"
36 #include "asterisk/channel.h"
37
38 /*** DOCUMENTATION
39         <application name="SendDTMF" language="en_US">
40                 <synopsis>
41                         Sends arbitrary DTMF digits
42                 </synopsis>
43                 <syntax>
44                         <parameter name="digits" required="true">
45                                 <para>List of digits 0-9,*#,abcd</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>DTMF digits sent to a channel with half second pause</para>
59                         <para>It will pass all digits or terminate if it encounters an error.</para>
60                 </description>
61                 <see-also>
62                         <ref type="application">Read</ref>
63                 </see-also>
64         </application>
65         <manager name="PlayDTMF" language="en_US">
66                 <synopsis>
67                         Play DTMF signal on a specific channel.
68                 </synopsis>
69                 <syntax>
70                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
71                         <parameter name="Channel" required="true">
72                                 <para>Channel name to send digit to.</para>
73                         </parameter>
74                         <parameter name="Digit" required="true">
75                                 <para>The DTMF digit to play.</para>
76                         </parameter>
77                 </syntax>
78                 <description>
79                         <para>Plays a dtmf digit on the specified channel.</para>
80                 </description>
81         </manager>
82  ***/
83 static char *app = "SendDTMF";
84
85 static int senddtmf_exec(struct ast_channel *chan, const char *vdata)
86 {
87         int res = 0;
88         char *data;
89         int dinterval = 0, duration = 0;
90         struct ast_channel *dchan;
91         AST_DECLARE_APP_ARGS(args,
92                 AST_APP_ARG(digits);
93                 AST_APP_ARG(dinterval);
94                 AST_APP_ARG(duration);
95                 AST_APP_ARG(channel);
96         );
97
98         if (ast_strlen_zero(vdata)) {
99                 ast_log(LOG_WARNING, "SendDTMF requires an argument (digits or *#aAbBcCdD)\n");
100                 return 0;
101         }
102
103         dchan = chan;
104
105         data = ast_strdupa(vdata);
106         AST_STANDARD_APP_ARGS(args, data);
107
108         if (!ast_strlen_zero(args.dinterval)) {
109                 ast_app_parse_timelen(args.dinterval, &dinterval, TIMELEN_MILLISECONDS);
110         }
111         if (!ast_strlen_zero(args.duration)) {
112                 ast_app_parse_timelen(args.duration, &duration, TIMELEN_MILLISECONDS);
113         }
114         if (!ast_strlen_zero(args.channel)) {
115                 dchan = ast_channel_get_by_name(args.channel);
116         }
117         if (dchan != chan) {
118                 ast_autoservice_start(chan);
119         }
120         res = ast_dtmf_stream(dchan, NULL, args.digits, dinterval <= 0 ? 250 : dinterval, duration);
121         if (dchan != chan) {
122                 ast_autoservice_stop(chan);
123                 ast_channel_unref(dchan);
124         }
125
126         return res;
127 }
128
129 static int manager_play_dtmf(struct mansession *s, const struct message *m)
130 {
131         const char *channel = astman_get_header(m, "Channel");
132         const char *digit = astman_get_header(m, "Digit");
133         struct ast_channel *chan;
134
135         if (!(chan = ast_channel_get_by_name(channel))) {
136                 astman_send_error(s, m, "Channel not found");
137                 return 0;
138         }
139
140         if (ast_strlen_zero(digit)) {
141                 astman_send_error(s, m, "No digit specified");
142                 chan = ast_channel_unref(chan);
143                 return 0;
144         }
145
146         ast_senddigit(chan, *digit, 0);
147
148         chan = ast_channel_unref(chan);
149
150         astman_send_ack(s, m, "DTMF successfully queued");
151
152         return 0;
153 }
154
155 static int unload_module(void)
156 {
157         int res;
158
159         res = ast_unregister_application(app);
160         res |= ast_manager_unregister("PlayDTMF");
161
162         return res;     
163 }
164
165 static int load_module(void)
166 {
167         int res;
168
169         res = ast_manager_register_xml("PlayDTMF", EVENT_FLAG_CALL, manager_play_dtmf);
170         res |= ast_register_application_xml(app, senddtmf_exec);
171
172         return res;
173 }
174
175 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send DTMF digits Application");