Sun Oct 16 2011 08:41:28

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