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 /*** MODULEINFO
00028    <support_level>extended</support_level>
00029  ***/
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 419592 $")
00034 
00035 #include "asterisk/file.h"
00036 #include "asterisk/channel.h"
00037 #include "asterisk/pbx.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/indications.h"
00040 
00041 /*** DOCUMENTATION
00042    <application name="Morsecode" language="en_US">
00043       <synopsis>
00044          Plays morse code.
00045       </synopsis>
00046       <syntax>
00047          <parameter name="string" required="true">
00048             <para>String to playback as morse code to channel</para>
00049          </parameter>
00050       </syntax>
00051       <description>
00052          <para>Plays the Morse code equivalent of the passed string.</para>
00053          <para>This application does not automatically answer and should be preceeded by
00054          an application such as Answer() or Progress().</para>
00055          <para>This application uses the following variables:</para>
00056          <variablelist>
00057             <variable name="MORSEDITLEN">
00058                <para>Use this value in (ms) for length of dit</para>
00059             </variable>
00060             <variable name="MORSETONE">
00061                <para>The pitch of the tone in (Hz), default is 800</para>
00062             </variable>
00063          </variablelist>
00064       </description>
00065       <see-also>
00066          <ref type="application">SayAlpha</ref>
00067          <ref type="application">SayPhonetic</ref>
00068       </see-also>
00069    </application>
00070  ***/ 
00071 static const char app_morsecode[] = "Morsecode";
00072 
00073 static const char * const morsecode[] = {
00074    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /*  0-15 */
00075    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
00076    " ",      /* 32 - <space> */
00077    ".-.-.-", /* 33 - ! */
00078    ".-..-.", /* 34 - " */
00079    "",       /* 35 - # */
00080    "",       /* 36 - $ */
00081    "",       /* 37 - % */
00082    "",       /* 38 - & */
00083    ".----.", /* 39 - ' */
00084    "-.--.-", /* 40 - ( */
00085    "-.--.-", /* 41 - ) */
00086    "",       /* 42 - * */
00087    "",       /* 43 - + */
00088    "--..--", /* 44 - , */
00089    "-....-", /* 45 - - */
00090    ".-.-.-", /* 46 - . */
00091    "-..-.",  /* 47 - / */
00092    "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", /* 48-57 - 0-9 */
00093    "---...", /* 58 - : */
00094    "-.-.-.", /* 59 - ; */
00095    "",       /* 60 - < */
00096    "-...-",  /* 61 - = */
00097    "",       /* 62 - > */
00098    "..--..", /* 63 - ? */
00099    ".--.-.", /* 64 - @ */
00100    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
00101    "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
00102    "-.--.-", /* 91 - [ (really '(') */
00103    "-..-.",  /* 92 - \ (really '/') */
00104    "-.--.-", /* 93 - ] (really ')') */
00105    "",       /* 94 - ^ */
00106    "..--.-", /* 95 - _ */
00107    ".----.", /* 96 - ` */
00108    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
00109    "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
00110    "-.--.-", /* 123 - { (really '(') */
00111    "",       /* 124 - | */
00112    "-.--.-", /* 125 - } (really ')') */
00113    "-..-.",  /* 126 - ~ (really bar) */
00114    ". . .",  /* 127 - <del> (error) */
00115 };
00116 
00117 static void playtone(struct ast_channel *chan, int tone, int len)
00118 {
00119    char dtmf[20];
00120    snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
00121    ast_playtones_start(chan, 0, dtmf, 0);
00122    ast_safe_sleep(chan, len);
00123    ast_playtones_stop(chan);
00124 }
00125 
00126 static int morsecode_exec(struct ast_channel *chan, const char *data)
00127 {
00128    int res=0, ditlen, tone;
00129    const char *digit;
00130    const char *ditlenc, *tonec;
00131 
00132    if (ast_strlen_zero(data)) {
00133       ast_log(LOG_WARNING, "Syntax: Morsecode(<string>) - no argument found\n");
00134       return 0;
00135    }
00136 
00137    /* Use variable MORESEDITLEN, if set (else 80) */
00138    ast_channel_lock(chan);
00139    ditlenc = pbx_builtin_getvar_helper(chan, "MORSEDITLEN");
00140    if (ast_strlen_zero(ditlenc) || (sscanf(ditlenc, "%30d", &ditlen) != 1)) {
00141       ditlen = 80;
00142    }
00143    ast_channel_unlock(chan);
00144 
00145    /* Use variable MORSETONE, if set (else 800) */
00146    ast_channel_lock(chan);
00147    tonec = pbx_builtin_getvar_helper(chan, "MORSETONE");
00148    if (ast_strlen_zero(tonec) || (sscanf(tonec, "%30d", &tone) != 1)) {
00149       tone = 800;
00150    }
00151    ast_channel_unlock(chan);
00152 
00153    for (digit = data; *digit; digit++) {
00154       int digit2 = *digit;
00155       const char *dahdit;
00156       if (digit2 < 0) {
00157          continue;
00158       }
00159       for (dahdit = morsecode[digit2]; *dahdit; dahdit++) {
00160          if (*dahdit == '-') {
00161             playtone(chan, tone, 3 * ditlen);
00162          } else if (*dahdit == '.') {
00163             playtone(chan, tone, 1 * ditlen);
00164          } else {
00165             /* Account for ditlen of silence immediately following */
00166             playtone(chan, 0, 2 * ditlen);
00167          }
00168 
00169          /* Pause slightly between each dit and dah */
00170          playtone(chan, 0, 1 * ditlen);
00171       }
00172       /* Pause between characters */
00173       playtone(chan, 0, 2 * ditlen);
00174    }
00175 
00176    return res;
00177 }
00178 
00179 static int unload_module(void)
00180 {
00181    return ast_unregister_application(app_morsecode);
00182 }
00183 
00184 static int load_module(void)
00185 {
00186    return ast_register_application_xml(app_morsecode, morsecode_exec);
00187 }
00188 
00189 AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Morse code");
00190 

Generated on Thu Apr 16 06:27:10 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6