be70d7b0fdf4200e8862b89f2f645aebfbe406ed
[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 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35
36 #include "asterisk/pbx.h"
37 #include "asterisk/module.h"
38 #include "asterisk/app.h"
39 #include "asterisk/manager.h"
40 #include "asterisk/channel.h"
41
42 /*** DOCUMENTATION
43         <application name="SendDTMF" language="en_US">
44                 <synopsis>
45                         Sends arbitrary DTMF digits
46                 </synopsis>
47                 <syntax>
48                         <parameter name="digits" required="true">
49                                 <para>List of digits 0-9,*#,a-d,A-D to send also w for a half second pause,
50                                 W for a one second pause, and f or F for a flash-hook if the channel supports
51                                 flash-hook.</para>
52                         </parameter>
53                         <parameter name="timeout_ms" required="false">
54                                 <para>Amount of time to wait in ms between tones. (defaults to .25s)</para>
55                         </parameter>
56                         <parameter name="duration_ms" required="false">
57                                 <para>Duration of each digit</para>
58                         </parameter>
59                         <parameter name="channel" required="false">
60                                 <para>Channel where digits will be played</para>
61                         </parameter>
62                 </syntax>
63                 <description>
64                         <para>It will send all digits or terminate if it encounters an error.</para>
65                 </description>
66                 <see-also>
67                         <ref type="application">Read</ref>
68                 </see-also>
69         </application>
70         <manager name="PlayDTMF" language="en_US">
71                 <synopsis>
72                         Play DTMF signal on a specific channel.
73                 </synopsis>
74                 <syntax>
75                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
76                         <parameter name="Channel" required="true">
77                                 <para>Channel name to send digit to.</para>
78                         </parameter>
79                         <parameter name="Digit" required="true">
80                                 <para>The DTMF digit to play.</para>
81                         </parameter>
82                         <parameter name="Duration" required="false">
83                                 <para>The duration, in milliseconds, of the digit to be played.</para>
84                         </parameter>
85                 </syntax>
86                 <description>
87                         <para>Plays a dtmf digit on the specified channel.</para>
88                 </description>
89         </manager>
90  ***/
91
92 static const char senddtmf_name[] = "SendDTMF";
93
94 static int senddtmf_exec(struct ast_channel *chan, const char *vdata)
95 {
96         int res;
97         char *data;
98         int dinterval = 0, duration = 0;
99         struct ast_channel *chan_found = NULL;
100         struct ast_channel *chan_dest = chan;
101         struct ast_channel *chan_autoservice = NULL;
102         AST_DECLARE_APP_ARGS(args,
103                 AST_APP_ARG(digits);
104                 AST_APP_ARG(dinterval);
105                 AST_APP_ARG(duration);
106                 AST_APP_ARG(channel);
107         );
108
109         if (ast_strlen_zero(vdata)) {
110                 ast_log(LOG_WARNING, "SendDTMF requires an argument\n");
111                 return 0;
112         }
113
114         data = ast_strdupa(vdata);
115         AST_STANDARD_APP_ARGS(args, data);
116
117         if (ast_strlen_zero(args.digits)) {
118                 ast_log(LOG_WARNING, "The digits argument is required (0-9,*#,a-d,A-D,wfF)\n");
119                 return 0;
120         }
121         if (!ast_strlen_zero(args.dinterval)) {
122                 ast_app_parse_timelen(args.dinterval, &dinterval, TIMELEN_MILLISECONDS);
123         }
124         if (!ast_strlen_zero(args.duration)) {
125                 ast_app_parse_timelen(args.duration, &duration, TIMELEN_MILLISECONDS);
126         }
127         if (!ast_strlen_zero(args.channel)) {
128                 chan_found = ast_channel_get_by_name(args.channel);
129                 if (!chan_found) {
130                         ast_log(LOG_WARNING, "No such channel: %s\n", args.channel);
131                         return 0;
132                 }
133                 chan_dest = chan_found;
134                 if (chan_found != chan) {
135                         chan_autoservice = chan;
136                 }
137         }
138         res = ast_dtmf_stream(chan_dest, chan_autoservice, args.digits,
139                 dinterval <= 0 ? 250 : dinterval, duration);
140         if (chan_found) {
141                 ast_channel_unref(chan_found);
142         }
143
144         return chan_autoservice ? 0 : res;
145 }
146
147 static int manager_play_dtmf(struct mansession *s, const struct message *m)
148 {
149         const char *channel = astman_get_header(m, "Channel");
150         const char *digit = astman_get_header(m, "Digit");
151         const char *duration = astman_get_header(m, "Duration");
152         struct ast_channel *chan;
153         unsigned int duration_ms = 0;
154
155         if (!(chan = ast_channel_get_by_name(channel))) {
156                 astman_send_error(s, m, "Channel not found");
157                 return 0;
158         }
159
160         if (ast_strlen_zero(digit)) {
161                 astman_send_error(s, m, "No digit specified");
162                 chan = ast_channel_unref(chan);
163                 return 0;
164         }
165         
166         if (!ast_strlen_zero(duration) && (sscanf(duration, "%30u", &duration_ms) != 1)) {
167                 astman_send_error(s, m, "Could not convert Duration parameter");
168                 chan = ast_channel_unref(chan);
169                 return 0;
170         }
171
172         ast_senddigit(chan, *digit, duration_ms);
173
174         chan = ast_channel_unref(chan);
175
176         astman_send_ack(s, m, "DTMF successfully queued");
177
178         return 0;
179 }
180
181 static int unload_module(void)
182 {
183         int res;
184
185         res = ast_unregister_application(senddtmf_name);
186         res |= ast_manager_unregister("PlayDTMF");
187
188         return res;
189 }
190
191 static int load_module(void)
192 {
193         int res;
194
195         res = ast_manager_register_xml("PlayDTMF", EVENT_FLAG_CALL, manager_play_dtmf);
196         res |= ast_register_application_xml(senddtmf_name, senddtmf_exec);
197
198         return res;
199 }
200
201 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send DTMF digits Application");