Merge changes from team/group/appdocsxml
[asterisk/asterisk.git] / apps / app_morsecode.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (c) 2006, Tilghman Lesher.  All rights reserved.
5  *
6  * Tilghman Lesher <app_morsecode__v001@the-tilghman.com>
7  *
8  * This code is released by the author with no restrictions on usage.
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  */
17
18 /*! \file
19  *
20  * \brief Morsecode application
21  *
22  * \author Tilghman Lesher <app_morsecode__v001@the-tilghman.com>
23  *
24  * \ingroup applications
25  */
26
27 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 #include "asterisk/file.h"
32 #include "asterisk/channel.h"
33 #include "asterisk/pbx.h"
34 #include "asterisk/module.h"
35 #include "asterisk/indications.h"
36
37 /*** DOCUMENTATION
38         <application name="Morsecode" language="en_US">
39                 <synopsis>
40                         Plays morse code
41                 </synopsis>
42                 <syntax>
43                         <parameter name="string" required="true">
44                                 <para>String to playback as morse code to channel</para>
45                         </parameter>
46                 </syntax>
47                 <description>
48                         <para>Plays the Morse code equivalent of the passed string.</para>
49
50                         <para>This application uses the following variables:</para>
51                         <variablelist>
52                                 <variable name="MORSEDITLEN">
53                                         <para>Use this value in (ms) for length of dit</para>
54                                 </variable>
55                                 <variable name="MORSETONE">
56                                         <para>The pitch of the tone in (Hz), default is 800</para>
57                                 </variable>
58                         </variablelist>
59                 </description>
60         </application>
61  ***/   
62 static char *app_morsecode = "Morsecode";
63
64 static char *morsecode[] = {
65         "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /*  0-15 */
66         "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
67         " ",      /* 32 - <space> */
68         ".-.-.-", /* 33 - ! */
69         ".-..-.", /* 34 - " */
70         "",       /* 35 - # */
71         "",       /* 36 - $ */
72         "",       /* 37 - % */
73         "",       /* 38 - & */
74         ".----.", /* 39 - ' */
75         "-.--.-", /* 40 - ( */
76         "-.--.-", /* 41 - ) */
77         "",       /* 42 - * */
78         "",       /* 43 - + */
79         "--..--", /* 44 - , */
80         "-....-", /* 45 - - */
81         ".-.-.-", /* 46 - . */
82         "-..-.",  /* 47 - / */
83         "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", /* 48-57 - 0-9 */
84         "---...", /* 58 - : */
85         "-.-.-.", /* 59 - ; */
86         "",       /* 60 - < */
87         "-...-",  /* 61 - = */
88         "",       /* 62 - > */
89         "..--..", /* 63 - ? */
90         ".--.-.", /* 64 - @ */
91         ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
92         "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
93         "-.--.-", /* 91 - [ (really '(') */
94         "-..-.",  /* 92 - \ (really '/') */
95         "-.--.-", /* 93 - ] (really ')') */
96         "",       /* 94 - ^ */
97         "..--.-", /* 95 - _ */
98         ".----.", /* 96 - ` */
99         ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
100         "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
101         "-.--.-", /* 123 - { (really '(') */
102         "",       /* 124 - | */
103         "-.--.-", /* 125 - } (really ')') */
104         "-..-.",  /* 126 - ~ (really bar) */
105         ". . .",  /* 127 - <del> (error) */
106 };
107
108 static void playtone(struct ast_channel *chan, int tone, int len)
109 {
110         char dtmf[20];
111         snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
112         ast_playtones_start(chan, 0, dtmf, 0);
113         ast_safe_sleep(chan, len);
114         ast_playtones_stop(chan);
115 }
116
117 static int morsecode_exec(struct ast_channel *chan, void *data)
118 {
119         int res=0, ditlen, tone;
120         char *digit;
121         const char *ditlenc, *tonec;
122
123         if (ast_strlen_zero(data)) {
124                 ast_log(LOG_WARNING, "Syntax: Morsecode(<string>) - no argument found\n");
125                 return 0;
126         }
127
128         /* Use variable MORESEDITLEN, if set (else 80) */
129         ast_channel_lock(chan);
130         ditlenc = pbx_builtin_getvar_helper(chan, "MORSEDITLEN");
131         if (ast_strlen_zero(ditlenc) || (sscanf(ditlenc, "%d", &ditlen) != 1)) {
132                 ditlen = 80;
133         }
134         ast_channel_unlock(chan);
135
136         /* Use variable MORSETONE, if set (else 800) */
137         ast_channel_lock(chan);
138         tonec = pbx_builtin_getvar_helper(chan, "MORSETONE");
139         if (ast_strlen_zero(tonec) || (sscanf(tonec, "%d", &tone) != 1)) {
140                 tone = 800;
141         }
142         ast_channel_unlock(chan);
143
144         for (digit = data; *digit; digit++) {
145                 int digit2 = *digit;
146                 char *dahdit;
147                 if (digit2 < 0) {
148                         continue;
149                 }
150                 for (dahdit = morsecode[digit2]; *dahdit; dahdit++) {
151                         if (*dahdit == '-') {
152                                 playtone(chan, tone, 3 * ditlen);
153                         } else if (*dahdit == '.') {
154                                 playtone(chan, tone, 1 * ditlen);
155                         } else {
156                                 /* Account for ditlen of silence immediately following */
157                                 playtone(chan, 0, 2 * ditlen);
158                         }
159
160                         /* Pause slightly between each dit and dah */
161                         playtone(chan, 0, 1 * ditlen);
162                 }
163                 /* Pause between characters */
164                 playtone(chan, 0, 2 * ditlen);
165         }
166
167         return res;
168 }
169
170 static int unload_module(void)
171 {
172         return ast_unregister_application(app_morsecode);
173 }
174
175 static int load_module(void)
176 {
177         return ast_register_application_xml(app_morsecode, morsecode_exec);
178 }
179
180 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Morse code");