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