Wed Aug 18 22:33:41 2010

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: 211569 $")
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 static char *app_morsecode = "Morsecode";
00038 
00039 static char *morsecode_synopsis = "Plays morse code";
00040 
00041 static char *morsecode_descrip =
00042 "  Morsecode(<string>):\n"
00043 "Plays the Morse code equivalent of the passed string.  If the variable\n"
00044 "MORSEDITLEN is set, it will use that value for the length (in ms) of the dit\n"
00045 "(defaults to 80).  Additionally, if MORSETONE is set, it will use that tone\n"
00046 "(in Hz).  The tone default is 800.\n";
00047 
00048 
00049 static char *morsecode[] = {
00050    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /*  0-15 */
00051    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
00052    " ",      /* 32 - <space> */
00053    ".-.-.-", /* 33 - ! */
00054    ".-..-.", /* 34 - " */
00055    "",       /* 35 - # */
00056    "",       /* 36 - $ */
00057    "",       /* 37 - % */
00058    "",       /* 38 - & */
00059    ".----.", /* 39 - ' */
00060    "-.--.-", /* 40 - ( */
00061    "-.--.-", /* 41 - ) */
00062    "",       /* 42 - * */
00063    "",       /* 43 - + */
00064    "--..--", /* 44 - , */
00065    "-....-", /* 45 - - */
00066    ".-.-.-", /* 46 - . */
00067    "-..-.",  /* 47 - / */
00068    "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", /* 48-57 - 0-9 */
00069    "---...", /* 58 - : */
00070    "-.-.-.", /* 59 - ; */
00071    "",       /* 60 - < */
00072    "-...-",  /* 61 - = */
00073    "",       /* 62 - > */
00074    "..--..", /* 63 - ? */
00075    ".--.-.", /* 64 - @ */
00076    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
00077    "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
00078    "-.--.-", /* 91 - [ (really '(') */
00079    "-..-.",  /* 92 - \ (really '/') */
00080    "-.--.-", /* 93 - ] (really ')') */
00081    "",       /* 94 - ^ */
00082    "..--.-", /* 95 - _ */
00083    ".----.", /* 96 - ` */
00084    ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
00085    "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
00086    "-.--.-", /* 123 - { (really '(') */
00087    "",       /* 124 - | */
00088    "-.--.-", /* 125 - } (really ')') */
00089    "-..-.",  /* 126 - ~ (really bar) */
00090    ". . .",  /* 127 - <del> (error) */
00091 };
00092 
00093 static void playtone(struct ast_channel *chan, int tone, int len)
00094 {
00095    char dtmf[20];
00096    snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
00097    ast_playtones_start(chan, 0, dtmf, 0);
00098    ast_safe_sleep(chan, len);
00099    ast_playtones_stop(chan);
00100 }
00101 
00102 static int morsecode_exec(struct ast_channel *chan, void *data)
00103 {
00104    int res=0, ditlen, tone;
00105    char *digit;
00106    const char *ditlenc, *tonec;
00107 
00108    if (ast_strlen_zero(data)) {
00109       ast_log(LOG_WARNING, "Syntax: Morsecode(<string>) - no argument found\n");
00110       return 0;
00111    }
00112 
00113    /* Use variable MORESEDITLEN, if set (else 80) */
00114    ast_channel_lock(chan);
00115    ditlenc = pbx_builtin_getvar_helper(chan, "MORSEDITLEN");
00116    if (ast_strlen_zero(ditlenc) || (sscanf(ditlenc, "%30d", &ditlen) != 1)) {
00117       ditlen = 80;
00118    }
00119    ast_channel_unlock(chan);
00120 
00121    /* Use variable MORSETONE, if set (else 800) */
00122    ast_channel_lock(chan);
00123    tonec = pbx_builtin_getvar_helper(chan, "MORSETONE");
00124    if (ast_strlen_zero(tonec) || (sscanf(tonec, "%30d", &tone) != 1)) {
00125       tone = 800;
00126    }
00127    ast_channel_unlock(chan);
00128 
00129    for (digit = data; *digit; digit++) {
00130       int digit2 = *digit;
00131       char *dahdit;
00132       if (digit2 < 0) {
00133          continue;
00134       }
00135       for (dahdit = morsecode[digit2]; *dahdit; dahdit++) {
00136          if (*dahdit == '-') {
00137             playtone(chan, tone, 3 * ditlen);
00138          } else if (*dahdit == '.') {
00139             playtone(chan, tone, 1 * ditlen);
00140          } else {
00141             /* Account for ditlen of silence immediately following */
00142             playtone(chan, 0, 2 * ditlen);
00143          }
00144 
00145          /* Pause slightly between each dit and dah */
00146          playtone(chan, 0, 1 * ditlen);
00147       }
00148       /* Pause between characters */
00149       playtone(chan, 0, 2 * ditlen);
00150    }
00151 
00152    return res;
00153 }
00154 
00155 static int unload_module(void)
00156 {
00157    return ast_unregister_application(app_morsecode);
00158 }
00159 
00160 static int load_module(void)
00161 {
00162    return ast_register_application(app_morsecode, morsecode_exec, morsecode_synopsis, morsecode_descrip);
00163 }
00164 
00165 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Morse code");

Generated on Wed Aug 18 22:33:41 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7