Debian Codegraph @

apps/app_morsecode.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (c) 2006, Tilghman Lesher. All rights reserved.
5 *
6 * Updated by Naveen Albert <[email protected]>
7 *
8 * Tilghman Lesher <[email protected]>
9 *
10 * This code is released by the author with no restrictions on usage.
11 *
12 * See http://www.asterisk.org for more information about
13 * the Asterisk project. Please do not directly contact
14 * any of the maintainers of this project for assistance;
15 * the project provides a web site, mailing lists and IRC
16 * channels for your use.
17 *
18 */
19
20 /*! \file
21 *
22 * \brief Morsecode application
23 *
24 * \author Tilghman Lesher <[email protected]>
25 * \author Naveen Albert <[email protected]>
26 *
27 * \ingroup applications
28 */
29
30 /*** MODULEINFO
31 <support_level>extended</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include "asterisk/file.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/pbx.h"
39 #include "asterisk/module.h"
40 #include "asterisk/indications.h"
41
42 /*** DOCUMENTATION
43 <application name="Morsecode" language="en_US">
44 <since>
45 <version>1.4.0</version>
46 </since>
47 <synopsis>
48 Plays morse code.
49 </synopsis>
50 <syntax>
51 <parameter name="string" required="true">
52 <para>String to playback as morse code to channel</para>
53 </parameter>
54 </syntax>
55 <description>
56 <para>Plays the Morse code equivalent of the passed string.</para>
57 <para>This application does not automatically answer and should be preceded by
58 an application such as Answer() or Progress().</para>
59 <para>This application uses the following variables:</para>
60 <variablelist>
61 <variable name="MORSEDITLEN">
62 <para>Use this value in (ms) for length of dit</para>
63 </variable>
64 <variable name="MORSETONE">
65 <para>The pitch of the tone in (Hz), default is 800</para>
66 </variable>
67 <variable name="MORSESPACETONE">
68 <para>The pitch of the spaces in (Hz), default is 0</para>
69 </variable>
70 <variable name="MORSETYPE">
71 <para>The code type to use (AMERICAN for standard American Morse
72 or INTERNATIONAL for international code.
73 Default is INTERNATIONAL).</para>
74 </variable>
75 </variablelist>
76 </description>
77 <see-also>
78 <ref type="application">SayAlpha</ref>
79 <ref type="application">SayPhonetic</ref>
80 </see-also>
81 </application>
82 ***/
83 static const char app_morsecode[] = "Morsecode";
84
85 static const char * const internationalcode[] = {
86 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 0-15 */
87 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
88 " ", /* 32 - <space> */
89 ".-.-.-", /* 33 - ! */
90 ".-..-.", /* 34 - " */
91 "", /* 35 - # */
92 "", /* 36 - $ */
93 "", /* 37 - % */
94 "", /* 38 - & */
95 ".----.", /* 39 - ' */
96 "-.--.-", /* 40 - ( */
97 "-.--.-", /* 41 - ) */
98 "", /* 42 - * */
99 "", /* 43 - + */
100 "--..--", /* 44 - , */
101 "-....-", /* 45 - - */
102 ".-.-.-", /* 46 - . */
103 "-..-.", /* 47 - / */
104 "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", /* 48-57 - 0-9 */
105 "---...", /* 58 - : */
106 "-.-.-.", /* 59 - ; */
107 "", /* 60 - < */
108 "-...-", /* 61 - = */
109 "", /* 62 - > */
110 "..--..", /* 63 - ? */
111 ".--.-.", /* 64 - @ */
112 ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", /* A-M */
113 "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", /* N-Z */
114 "-.--.-", /* 91 - [ (really '(') */
115 "-..-.", /* 92 - \ (really '/') */
116 "-.--.-", /* 93 - ] (really ')') */
117 "", /* 94 - ^ */
118 "..--.-", /* 95 - _ */
119 ".----.", /* 96 - ` */
120 ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", /* a-m */
121 "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", /* n-z */
122 "-.--.-", /* 123 - { (really '(') */
123 "", /* 124 - | */
124 "-.--.-", /* 125 - } (really ')') */
125 "-..-.", /* 126 - ~ (really bar) */
126 ". . .", /* 127 - <del> (error) */
127 };
128
129 static const char * const americanmorsecode[] = {
130 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 0-15 */
131 "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
132 " ", /* 32 - <space> */
133 "---.", /* 33 - ! */
134 "..-. -.",/* 34 - " (QN)*/
135 "", /* 35 - # */
136 "... .-..",/* 36 - $ (SX) */
137 "", /* 37 - % */
138 ". ...", /* 38 - & (ES) */
139 "..-. .-..",/* 39 - ' (QX) */
140 "..... -.", /* 40 - ( (PN) */
141 "..... .. ..", /* 41 - ) (PY) */
142 "", /* 42 - * */
143 "", /* 43 - + */
144 ".-.-", /* 44 - , */
145 ".... .-..",/* 45 - (HX) */
146 "..--..", /* 46 - . */
147 "..- -", /* 47 - / (UT) */
148 ".--.", "..-..", "...-.", "....-", "---", "......", "--..", "-....", "-..-", "0", /* 48-57 - 0-9 */
149 "-.- . .",/* 58 - : (KO) */
150 "... ..", /* 59 - ; */
151 "", /* 60 - < */
152 "-...-", /* 61 - = (paragraph mark) */
153 "", /* 62 - > */
154 "-..-.", /* 63 - ? */
155 ".--.-.", /* 64 - @ */
156 ".-", "-...", ".. .", "-..", ".", ".-.", "--.", "....", "..", ".-.-", "-.-", "L", "--", /* A-M */
157 "-.", ". .", ".....", "..-.", ". ..", "...", "-", "..-", "...-", ".--", ".-..", ".. ..", "... .", /* N-Z */
158 "..... -.", /* 91 - [ (really '(') */
159 "..- -", /* 92 - \ (really '/') */
160 "..... .. ..", /* 93 - ] (really ')') */
161 "", /* 94 - ^ */
162 "..--.-", /* 95 - _ */
163 ".----.", /* 96 - ` */
164 ".-", "-...", ".. .", "-..", ".", ".-.", "--.", "....", "..", ".-.-", "-.-", "L", "--", /* a-m */
165 "-.", ". .", ".....", "..-.", ". ..", "...", "-", "..-", "...-", ".--", ".-..", ".. ..", "... .", /* n-z */
166 "..... -.", /* 123 - { (really '(') */
167 "", /* 124 - | */
168 "..... .. ..", /* 125 - } (really ')') */
169 "..- -", /* 126 - ~ (really bar) */
170 ". . .", /* 127 - <del> (error) */
171 };
172
173 static int playtone(struct ast_channel *chan, int tone, int len)
174 {
175 int res;
176 char dtmf[20];
177 snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
178 ast_playtones_start(chan, 0, dtmf, 0);
179 res = ast_safe_sleep(chan, len);
180 ast_playtones_stop(chan);
181 return res;
182 }
183
184 static int morsecode_exec(struct ast_channel *chan, const char *data)
185 {
186 int res = 0, ditlen, tone, toneoff, digit2;
187 const char *digit;
188 const char *ditlenc, *tonec, *toneb, *codetype;
189
190 if (ast_strlen_zero(data)) {
191 ast_log(LOG_WARNING, "Syntax: Morsecode(<string>) - no argument found\n");
192 return 0;
193 }
194
195 ast_channel_lock(chan);
196 /* Use variable MORESEDITLEN, if set (else 80) */
197 ditlenc = pbx_builtin_getvar_helper(chan, "MORSEDITLEN");
198 if (ast_strlen_zero(ditlenc) || (sscanf(ditlenc, "%30d", &ditlen) != 1)) {
199 ditlen = 80;
200 }
201
202 /* Use variable MORSETONE, if set (else 800) */
203 tonec = pbx_builtin_getvar_helper(chan, "MORSETONE");
204 if (ast_strlen_zero(tonec) || (sscanf(tonec, "%30d", &tone) != 1)) {
205 tone = 800;
206 }
207
208 /* Use variable MORSESPACETONE, if set (else 0) */
209 toneb = pbx_builtin_getvar_helper(chan, "MORSESPACETONE");
210 if (ast_strlen_zero(toneb) || (sscanf(toneb, "%30d", &toneoff) != 1)) {
211 toneoff = 0;
212 }
213
214 /* Use variable MORSETYPE, if set (else INTERNATIONAL) */
215 codetype = pbx_builtin_getvar_helper(chan, "MORSETYPE");
216 if (!codetype || strcmp(codetype, "AMERICAN")) {
217 codetype = "INTERNATIONAL";
218 }
219
220 ast_channel_unlock(chan);
221 if (!strcmp(codetype, "AMERICAN")) {
222 for (digit = data; *digit; digit++) {
223 const char *dahdit;
224 digit2 = *digit;
225 if (digit2 < 0 || digit2 > 127) {
226 continue;
227 }
228 for (dahdit = americanmorsecode[digit2]; *dahdit; dahdit++) {
229 if (*dahdit == '-') {
230 res = playtone(chan, tone, 3 * ditlen);
231 } else if (*dahdit == '.') {
232 res = playtone(chan, tone, 1 * ditlen);
233 } else if (*dahdit == 'L' || *dahdit == 'l') {
234 res = playtone(chan, tone, 6 * ditlen); /* long dash */
235 } else if (*dahdit == '0') {
236 res = playtone(chan, tone, 9 * ditlen); /* extra long dash */
237 } else if (*dahdit == ' ') { /* space char (x20) = 6 dot lengths */
238 /* Intra-char pauses, specific to American Morse */
239 res = playtone(chan, toneoff, 3 * ditlen);
240 } else {
241 /* Account for ditlen of silence immediately following */
242 res = playtone(chan, toneoff, 2 * ditlen);
243 }
244
245 /* Pause slightly between each dit and dah */
246 res = playtone(chan, toneoff, 1 * ditlen);
247 if (res)
248 break;
249 }
250 /* Pause between characters */
251 res = playtone(chan, toneoff, 3 * ditlen);
252 if (res)
253 break;
254 }
255 } else { /* International */
256 for (digit = data; *digit; digit++) {
257 const char *dahdit;
258 digit2 = *digit;
259 if (digit2 < 0 || digit2 > 127) {
260 continue;
261 }
262 for (dahdit = internationalcode[digit2]; *dahdit; dahdit++) {
263 if (*dahdit == '-') {
264 res = playtone(chan, tone, 3 * ditlen);
265 } else if (*dahdit == '.') {
266 res = playtone(chan, tone, 1 * ditlen);
267 } else {
268 /* Account for ditlen of silence immediately following */
269 res = playtone(chan, toneoff, 2 * ditlen);
270 }
271
272 /* Pause slightly between each dit and dah */
273 res = playtone(chan, toneoff, 1 * ditlen);
274 if (res)
275 break;
276 }
277 /* Pause between characters */
278 res = playtone(chan, toneoff, 2 * ditlen);
279 if (res)
280 break;
281 }
282 }
283
284 return res;
285 }
286
287 static int unload_module(void)
288 {
289 return ast_unregister_application(app_morsecode);
290 }
291
292 static int load_module(void)
293 {
294 return ast_register_application_xml(app_morsecode, morsecode_exec);
295 }
296
297 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Morse code");