Asterisk - The Open Source Telephony Project  18.5.0
app_morsecode.c
Go to the documentation of this file.
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  <synopsis>
45  Plays morse code.
46  </synopsis>
47  <syntax>
48  <parameter name="string" required="true">
49  <para>String to playback as morse code to channel</para>
50  </parameter>
51  </syntax>
52  <description>
53  <para>Plays the Morse code equivalent of the passed string.</para>
54  <para>This application does not automatically answer and should be preceeded by
55  an application such as Answer() or Progress().</para>
56  <para>This application uses the following variables:</para>
57  <variablelist>
58  <variable name="MORSEDITLEN">
59  <para>Use this value in (ms) for length of dit</para>
60  </variable>
61  <variable name="MORSETONE">
62  <para>The pitch of the tone in (Hz), default is 800</para>
63  </variable>
64  <variable name="MORSEOFF">
65  <para>The pitch of the spaces in (Hz), default is 0</para>
66  </variable>
67  <variable name="MORSETYPE">
68  <para>The code type to use (AMERICAN for standard American Morse
69  or INTERNATIONAL for international code.
70  Default is INTERNATIONAL).</para>
71  </variable>
72  </variablelist>
73  </description>
74  <see-also>
75  <ref type="application">SayAlpha</ref>
76  <ref type="application">SayPhonetic</ref>
77  </see-also>
78  </application>
79  ***/
80 static const char app_morsecode[] = "Morsecode";
81 
82 static const char * const internationalcode[] = {
83  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 0-15 */
84  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
85  " ", /* 32 - <space> */
86  ".-.-.-", /* 33 - ! */
87  ".-..-.", /* 34 - " */
88  "", /* 35 - # */
89  "", /* 36 - $ */
90  "", /* 37 - % */
91  "", /* 38 - & */
92  ".----.", /* 39 - ' */
93  "-.--.-", /* 40 - ( */
94  "-.--.-", /* 41 - ) */
95  "", /* 42 - * */
96  "", /* 43 - + */
97  "--..--", /* 44 - , */
98  "-....-", /* 45 - - */
99  ".-.-.-", /* 46 - . */
100  "-..-.", /* 47 - / */
101  "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", /* 48-57 - 0-9 */
102  "---...", /* 58 - : */
103  "-.-.-.", /* 59 - ; */
104  "", /* 60 - < */
105  "-...-", /* 61 - = */
106  "", /* 62 - > */
107  "..--..", /* 63 - ? */
108  ".--.-.", /* 64 - @ */
109  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", /* A-M */
110  "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", /* N-Z */
111  "-.--.-", /* 91 - [ (really '(') */
112  "-..-.", /* 92 - \ (really '/') */
113  "-.--.-", /* 93 - ] (really ')') */
114  "", /* 94 - ^ */
115  "..--.-", /* 95 - _ */
116  ".----.", /* 96 - ` */
117  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", /* a-m */
118  "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", /* n-z */
119  "-.--.-", /* 123 - { (really '(') */
120  "", /* 124 - | */
121  "-.--.-", /* 125 - } (really ')') */
122  "-..-.", /* 126 - ~ (really bar) */
123  ". . .", /* 127 - <del> (error) */
124 };
125 
126 static const char * const americanmorsecode[] = {
127  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 0-15 */
128  "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
129  " ", /* 32 - <space> */
130  "---.", /* 33 - ! */
131  "..-. -.",/* 34 - " (QN)*/
132  "", /* 35 - # */
133  "... .-..",/* 36 - $ (SX) */
134  "", /* 37 - % */
135  ". ...", /* 38 - & (ES) */
136  "..-. .-..",/* 39 - ' (QX) */
137  "..... -.", /* 40 - ( (PN) */
138  "..... .. ..", /* 41 - ) (PY) */
139  "", /* 42 - * */
140  "", /* 43 - + */
141  ".-.-", /* 44 - , */
142  ".... .-..",/* 45 - (HX) */
143  "..--..", /* 46 - . */
144  "..- -", /* 47 - / (UT) */
145  ".--.", "..-..", "...-.", "....-", "---", "......", "--..", "-....", "-..-", "0", /* 48-57 - 0-9 */
146  "-.- . .",/* 58 - : (KO) */
147  "... ..", /* 59 - ; */
148  "", /* 60 - < */
149  "-...-", /* 61 - = (paragraph mark) */
150  "", /* 62 - > */
151  "-..-.", /* 63 - ? */
152  ".--.-.", /* 64 - @ */
153  ".-", "-...", ".. .", "-..", ".", ".-.", "--.", "....", "..", ".-.-", "-.-", "L", "--", /* A-M */
154  "-.", ". .", ".....", "..-.", ". ..", "...", "-", "..-", "...-", ".--", ".-..", ".. ..", "... .", /* N-Z */
155  "..... -.", /* 91 - [ (really '(') */
156  "..- -", /* 92 - \ (really '/') */
157  "..... .. ..", /* 93 - ] (really ')') */
158  "", /* 94 - ^ */
159  "..--.-", /* 95 - _ */
160  ".----.", /* 96 - ` */
161  ".-", "-...", ".. .", "-..", ".", ".-.", "--.", "....", "..", ".-.-", "-.-", "L", "--", /* a-m */
162  "-.", ". .", ".....", "..-.", ". ..", "...", "-", "..-", "...-", ".--", ".-..", ".. ..", "... .", /* n-z */
163  "..... -.", /* 123 - { (really '(') */
164  "", /* 124 - | */
165  "..... .. ..", /* 125 - } (really ')') */
166  "..- -", /* 126 - ~ (really bar) */
167  ". . .", /* 127 - <del> (error) */
168 };
169 
170 static int playtone(struct ast_channel *chan, int tone, int len)
171 {
172  int res;
173  char dtmf[20];
174  snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
175  ast_playtones_start(chan, 0, dtmf, 0);
176  res = ast_safe_sleep(chan, len);
177  ast_playtones_stop(chan);
178  return res;
179 }
180 
181 static int morsecode_exec(struct ast_channel *chan, const char *data)
182 {
183  int res = 0, ditlen, tone, toneoff, digit2;
184  const char *digit;
185  const char *ditlenc, *tonec, *toneb, *codetype;
186 
187  if (ast_strlen_zero(data)) {
188  ast_log(LOG_WARNING, "Syntax: Morsecode(<string>) - no argument found\n");
189  return 0;
190  }
191 
192  /* Use variable MORESEDITLEN, if set (else 80) */
193  ast_channel_lock(chan);
194  ditlenc = pbx_builtin_getvar_helper(chan, "MORSEDITLEN");
195  if (ast_strlen_zero(ditlenc) || (sscanf(ditlenc, "%30d", &ditlen) != 1)) {
196  ditlen = 80;
197  }
198  ast_channel_unlock(chan);
199 
200  /* Use variable MORSETONE, if set (else 800) */
201  ast_channel_lock(chan);
202  tonec = pbx_builtin_getvar_helper(chan, "MORSETONE");
203  if (ast_strlen_zero(tonec) || (sscanf(tonec, "%30d", &tone) != 1)) {
204  tone = 800;
205  }
206  ast_channel_unlock(chan);
207 
208  /* Use variable MORSEOFF, if set (else 0) */
209  ast_channel_lock(chan);
210  toneb = pbx_builtin_getvar_helper(chan, "MORSEOFF");
211  if (ast_strlen_zero(toneb) || (sscanf(toneb, "%30d", &toneoff) != 1)) {
212  toneoff = 0;
213  }
214  ast_channel_unlock(chan);
215 
216  /* Use variable MORSETYPE, if set (else INTERNATIONAL) */
217  ast_channel_lock(chan);
218  codetype = pbx_builtin_getvar_helper(chan, "MORSETYPE");
219  if (!codetype || strcmp(codetype, "AMERICAN")) {
220  codetype = "INTERNATIONAL";
221  }
222  ast_channel_unlock(chan);
223 
224  if (!strcmp(codetype, "AMERICAN")) {
225  for (digit = data; *digit; digit++) {
226  const char *dahdit;
227  digit2 = *digit;
228  if (digit2 < 0 || digit2 > 127) {
229  continue;
230  }
231  for (dahdit = americanmorsecode[digit2]; *dahdit; dahdit++) {
232  if (*dahdit == '-') {
233  res = playtone(chan, tone, 3 * ditlen);
234  } else if (*dahdit == '.') {
235  res = playtone(chan, tone, 1 * ditlen);
236  } else if (*dahdit == 'L') {
237  res = playtone(chan, tone, 6 * ditlen); /* long dash */
238  } else if (*dahdit == '0') {
239  res = playtone(chan, tone, 9 * ditlen); /* extra long dash */
240  } else if (*dahdit == ' ') { /* space char (x20) = 6 dot lengths */
241  /* Intra-char pauses, specific to American Morse */
242  res = playtone(chan, toneoff, 3 * ditlen);
243  } else {
244  /* Account for ditlen of silence immediately following */
245  res = playtone(chan, toneoff, 2 * ditlen);
246  }
247 
248  /* Pause slightly between each dit and dah */
249  res = playtone(chan, toneoff, 1 * ditlen);
250  if (res)
251  break;
252  }
253  /* Pause between characters */
254  res = playtone(chan, toneoff, 3 * ditlen);
255  if (res)
256  break;
257  }
258  } else { /* International */
259  for (digit = data; *digit; digit++) {
260  const char *dahdit;
261  digit2 = *digit;
262  if (digit2 < 0 || digit2 > 127) {
263  continue;
264  }
265  for (dahdit = internationalcode[digit2]; *dahdit; dahdit++) {
266  if (*dahdit == '-') {
267  res = playtone(chan, tone, 3 * ditlen);
268  } else if (*dahdit == '.') {
269  res = playtone(chan, tone, 1 * ditlen);
270  } else {
271  /* Account for ditlen of silence immediately following */
272  res = playtone(chan, toneoff, 2 * ditlen);
273  }
274 
275  /* Pause slightly between each dit and dah */
276  res = playtone(chan, toneoff, 1 * ditlen);
277  if (res)
278  break;
279  }
280  /* Pause between characters */
281  res = playtone(chan, toneoff, 2 * ditlen);
282  if (res)
283  break;
284  }
285  }
286 
287  return res;
288 }
289 
290 static int unload_module(void)
291 {
293 }
294 
295 static int load_module(void)
296 {
298 }
299 
int ast_safe_sleep(struct ast_channel *chan, int ms)
Wait for a specified amount of time, looking for hangups.
Definition: channel.c:1574
Tone Indication Support.
char digit
#define ast_channel_lock(chan)
Definition: channel.h:2945
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
static int playtone(struct ast_channel *chan, int tone, int len)
#define LOG_WARNING
Definition: logger.h:274
static int morsecode_exec(struct ast_channel *chan, const char *data)
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
void ast_playtones_stop(struct ast_channel *chan)
Stop playing tones on a channel.
Definition: indications.c:393
int ast_unregister_application(const char *app)
Unregister an application.
Definition: pbx_app.c:392
static int unload_module(void)
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
General Asterisk PBX channel definitions.
static const char *const internationalcode[]
Definition: app_morsecode.c:82
Core PBX routines and definitions.
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Morse code")
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
static const char *const americanmorsecode[]
#define ast_channel_unlock(chan)
Definition: channel.h:2946
static int load_module(void)
static const char app_morsecode[]
Definition: app_morsecode.c:80
int ast_playtones_start(struct ast_channel *chan, int vol, const char *tonelist, int interruptible)
Start playing a list of tones on a channel.
Definition: indications.c:302
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
#define ast_register_application_xml(app, execute)
Register an application using XML documentation.
Definition: module.h:626