Mon Jun 27 16:50:46 2011

Asterisk developer's documentation


app_morsecode.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (c) 2006, Tilghman Lesher.  All rights reserved.
00005  *
00006  * Tilghman Lesher <app_morsecode__v001@the-tilghman.com>
00007  *
00008  * This code is released by the author with no restrictions on usage.
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  */
00017 
00018 /*! \file
00019  *
00020  * \brief Morsecode application
00021  *
00022  * \author Tilghman Lesher <app_morsecode__v001@the-tilghman.com>
00023  *
00024  * \ingroup applications
00025  */
00026 
00027 #include "asterisk.h"
00028 
00029 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211539 $")
00030 
00031 #include "asterisk/file.h"
00032 #include "asterisk/channel.h"
00033 #include "asterisk/pbx.h"
00034 #include "asterisk/module.h"
00035 #include "asterisk/indications.h"
00036 
00037 /*** DOCUMENTATION
00038    <application name="Morsecode" language="en_US">
00039       <synopsis>
00040          Plays morse code.
00041       </synopsis>
00042       <syntax>
00043          <parameter name="string" required="true">
00044             <para>String to playback as morse code to channel</para>
00045          </parameter>
00046       </syntax>
00047       <description>
00048          <para>Plays the Morse code equivalent of the passed string.</para>
00049 
00050          <para>This application uses the following variables:</para>
00051          <variablelist>
00052             <variable name="MORSEDITLEN">
00053                <para>Use this value in (ms) for length of dit</para>
00054             </variable>
00055             <variable name="MORSETONE">
00056                <para>The pitch of the tone in (Hz), default is 800</para>
00057             </variable>
00058          </variablelist>
00059       </description>
00060       <see-also>
00061          <ref type="application">SayAlpha</ref>
00062          <ref type="application">SayPhonetic</ref>
00063       </see-also>
00064    </application>
00065  ***/ 
00066 static const char app_morsecode[] = "Morsecode";
00067 
00068 static const char * const morsecode[] = {
00069    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /*  0-15 */
00070    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
00071    " ",      /* 32 - <space> */
00072    ".-.-.-", /* 33 - ! */
00073    ".-..-.", /* 34 - " */
00074    "",       /* 35 - # */
00075    "",       /* 36 - $ */
00076    "",       /* 37 - % */
00077    "",       /* 38 - & */
00078    ".----.", /* 39 - ' */
00079    "-.--.-", /* 40 - ( */
00080    "-.--.-", /* 41 - ) */
00081    "",       /* 42 - * */
00082    "",       /* 43 - + */
00083    "--..--", /* 44 - , */
00084    "-....-", /* 45 - - */
00085    ".-.-.-", /* 46 - . */
00086    "-..-.",  /* 47 - / */
00087    "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", /* 48-57 - 0-9 */
00088    "---...", /* 58 - : */
00089    "-.-.-.", /* 59 - ; */
00090    "",       /* 60 - < */
00091    "-...-",  /* 61 - = */
00092    "",       /* 62 - > */
00093    "..--..", /* 63 - ? */
00094    ".--.-.", /* 64 - @ */
00095    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
00096    "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
00097    "-.--.-", /* 91 - [ (really '(') */
00098    "-..-.",  /* 92 - \ (really '/') */
00099    "-.--.-", /* 93 - ] (really ')') */
00100    "",       /* 94 - ^ */
00101    "..--.-", /* 95 - _ */
00102    ".----.", /* 96 - ` */
00103    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
00104    "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
00105    "-.--.-", /* 123 - { (really '(') */
00106    "",       /* 124 - | */
00107    "-.--.-", /* 125 - } (really ')') */
00108    "-..-.",  /* 126 - ~ (really bar) */
00109    ". . .",  /* 127 - <del> (error) */
00110 };
00111 
00112 static void playtone(struct ast_channel *chan, int tone, int len)
00113 {
00114    char dtmf[20];
00115    snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
00116    ast_playtones_start(chan, 0, dtmf, 0);
00117    ast_safe_sleep(chan, len);
00118    ast_playtones_stop(chan);
00119 }
00120 
00121 static int morsecode_exec(struct ast_channel *chan, const char *data)
00122 {
00123    int res=0, ditlen, tone;
00124    const char *digit;
00125    const char *ditlenc, *tonec;
00126 
00127    if (ast_strlen_zero(data)) {
00128       ast_log(LOG_WARNING, "Syntax: Morsecode(<string>) - no argument found\n");
00129       return 0;
00130    }
00131 
00132    /* Use variable MORESEDITLEN, if set (else 80) */
00133    ast_channel_lock(chan);
00134    ditlenc = pbx_builtin_getvar_helper(chan, "MORSEDITLEN");
00135    if (ast_strlen_zero(ditlenc) || (sscanf(ditlenc, "%30d", &ditlen) != 1)) {
00136       ditlen = 80;
00137    }
00138    ast_channel_unlock(chan);
00139 
00140    /* Use variable MORSETONE, if set (else 800) */
00141    ast_channel_lock(chan);
00142    tonec = pbx_builtin_getvar_helper(chan, "MORSETONE");
00143    if (ast_strlen_zero(tonec) || (sscanf(tonec, "%30d", &tone) != 1)) {
00144       tone = 800;
00145    }
00146    ast_channel_unlock(chan);
00147 
00148    for (digit = data; *digit; digit++) {
00149       int digit2 = *digit;
00150       const char *dahdit;
00151       if (digit2 < 0) {
00152          continue;
00153       }
00154       for (dahdit = morsecode[digit2]; *dahdit; dahdit++) {
00155          if (*dahdit == '-') {
00156             playtone(chan, tone, 3 * ditlen);
00157          } else if (*dahdit == '.') {
00158             playtone(chan, tone, 1 * ditlen);
00159          } else {
00160             /* Account for ditlen of silence immediately following */
00161             playtone(chan, 0, 2 * ditlen);
00162          }
00163 
00164          /* Pause slightly between each dit and dah */
00165          playtone(chan, 0, 1 * ditlen);
00166       }
00167       /* Pause between characters */
00168       playtone(chan, 0, 2 * ditlen);
00169    }
00170 
00171    return res;
00172 }
00173 
00174 static int unload_module(void)
00175 {
00176    return ast_unregister_application(app_morsecode);
00177 }
00178 
00179 static int load_module(void)
00180 {
00181    return ast_register_application_xml(app_morsecode, morsecode_exec);
00182 }
00183 
00184 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Morse code");

Generated on Mon Jun 27 16:50:46 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7