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

Generated on Sat Aug 6 00:39:20 2011 for Asterisk - the Open Source PBX by  doxygen 1.4.7