395ae28c9147c74c709143fee4c864f3270dcb4c
[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                 </syntax>
54                 <description>
55                         <para>DTMF digits sent to a channel with half second pause</para>
56                         <para>It will pass all digits or terminate if it encounters an error.</para>
57                 </description>
58                 <see-also>
59                         <ref type="application">Read</ref>
60                 </see-also>
61         </application>
62         <manager name="PlayDTMF" language="en_US">
63                 <synopsis>
64                         Play DTMF signal on a specific channel.
65                 </synopsis>
66                 <syntax>
67                         <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
68                         <parameter name="Channel" required="true">
69                                 <para>Channel name to send digit to.</para>
70                         </parameter>
71                         <parameter name="Digit" required="true">
72                                 <para>The DTMF digit to play.</para>
73                         </parameter>
74                 </syntax>
75                 <description>
76                         <para>Plays a dtmf digit on the specified channel.</para>
77                 </description>
78         </manager>
79  ***/
80 static char *app = "SendDTMF";
81
82 static int senddtmf_exec(struct ast_channel *chan, const char *vdata)
83 {
84         int res = 0;
85         char *data;
86         int timeout = 0, duration = 0;
87         AST_DECLARE_APP_ARGS(args,
88                 AST_APP_ARG(digits);
89                 AST_APP_ARG(timeout);
90                 AST_APP_ARG(duration);
91         );
92
93         if (ast_strlen_zero(vdata)) {
94                 ast_log(LOG_WARNING, "SendDTMF requires an argument (digits or *#aAbBcCdD)\n");
95                 return 0;
96         }
97
98         data = ast_strdupa(vdata);
99         AST_STANDARD_APP_ARGS(args, data);
100
101         if (!ast_strlen_zero(args.timeout))
102                 timeout = atoi(args.timeout);
103         if (!ast_strlen_zero(args.duration))
104                 duration = atoi(args.duration);
105         res = ast_dtmf_stream(chan, NULL, args.digits, timeout <= 0 ? 250 : timeout, duration);
106
107         return res;
108 }
109
110 static int manager_play_dtmf(struct mansession *s, const struct message *m)
111 {
112         const char *channel = astman_get_header(m, "Channel");
113         const char *digit = astman_get_header(m, "Digit");
114         struct ast_channel *chan;
115
116         if (!(chan = ast_channel_get_by_name(channel))) {
117                 astman_send_error(s, m, "Channel not found");
118                 return 0;
119         }
120
121         if (ast_strlen_zero(digit)) {
122                 astman_send_error(s, m, "No digit specified");
123                 chan = ast_channel_unref(chan);
124                 return 0;
125         }
126
127         ast_senddigit(chan, *digit, 0);
128
129         chan = ast_channel_unref(chan);
130
131         astman_send_ack(s, m, "DTMF successfully queued");
132
133         return 0;
134 }
135
136 static int unload_module(void)
137 {
138         int res;
139
140         res = ast_unregister_application(app);
141         res |= ast_manager_unregister("PlayDTMF");
142
143         return res;     
144 }
145
146 static int load_module(void)
147 {
148         int res;
149
150         res = ast_manager_register_xml("PlayDTMF", EVENT_FLAG_CALL, manager_play_dtmf);
151         res |= ast_register_application_xml(app, senddtmf_exec);
152
153         return res;
154 }
155
156 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Send DTMF digits Application");