[Asterisk-code-review] app_morsecode: Add American Morse code (asterisk[master])

N A asteriskteam at digium.com
Wed Aug 4 09:47:14 CDT 2021


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/16230 )


Change subject: app_morsecode: Add American Morse code
......................................................................

app_morsecode: Add American Morse code

Previously, the Morsecode application only supported international
Morse code. This adds support for American Morse code and adds an
option to configure the frequency used in off intervals.

Additionally, the application checks for hangup between tones
to prevent application execution from continuing after hangup.

ASTERISK-29541

Change-Id: I172431a2e18e6527d577e74adfb05b154cba7bd4
---
M apps/app_morsecode.c
A doc/CHANGES-staging/app_morsecode.txt
2 files changed, 147 insertions(+), 28 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/30/16230/1

diff --git a/apps/app_morsecode.c b/apps/app_morsecode.c
index 0a9c208..76272f9 100644
--- a/apps/app_morsecode.c
+++ b/apps/app_morsecode.c
@@ -3,6 +3,8 @@
  *
  * Copyright (c) 2006, Tilghman Lesher.  All rights reserved.
  *
+ * Updated by Naveen Albert <asterisk at phreaknet.org>
+ *
  * Tilghman Lesher <app_morsecode__v001 at the-tilghman.com>
  *
  * This code is released by the author with no restrictions on usage.
@@ -20,6 +22,7 @@
  * \brief Morsecode application
  *
  * \author Tilghman Lesher <app_morsecode__v001 at the-tilghman.com>
+ * \author Naveen Albert <asterisk at phreaknet.org>
  *
  * \ingroup applications
  */
@@ -58,6 +61,14 @@
 				<variable name="MORSETONE">
 					<para>The pitch of the tone in (Hz), default is 800</para>
 				</variable>
+				<variable name="MORSEOFF">
+					<para>The pitch of the spaces in (Hz), default is 0</para>
+				</variable>
+				<variable name="MORSETYPE">
+					<para>The code type to use (AMERICAN for standard American Morse
+					or INTERNATIONAL for international code.
+					Default is INTERNATIONAL).</para>
+				</variable>
 			</variablelist>
 		</description>
 		<see-also>
@@ -68,7 +79,7 @@
  ***/
 static const char app_morsecode[] = "Morsecode";
 
-static const char * const morsecode[] = {
+static const char * const internationalcode[] = {
 	"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /*  0-15 */
 	"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
 	" ",      /* 32 - <space> */
@@ -95,16 +106,16 @@
 	"",       /* 62 - > */
 	"..--..", /* 63 - ? */
 	".--.-.", /* 64 - @ */
-	".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
-	"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
+	".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", /* A-M */
+	"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", /* N-Z */
 	"-.--.-", /* 91 - [ (really '(') */
 	"-..-.",  /* 92 - \ (really '/') */
 	"-.--.-", /* 93 - ] (really ')') */
 	"",       /* 94 - ^ */
 	"..--.-", /* 95 - _ */
 	".----.", /* 96 - ` */
-	".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
-	"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..",
+	".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", /* a-m */
+	"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", /* n-z */
 	"-.--.-", /* 123 - { (really '(') */
 	"",       /* 124 - | */
 	"-.--.-", /* 125 - } (really ')') */
@@ -112,20 +123,66 @@
 	". . .",  /* 127 - <del> (error) */
 };
 
-static void playtone(struct ast_channel *chan, int tone, int len)
+static const char * const americanmorsecode[] = {
+	"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /*  0-15 */
+	"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", /* 16-31 */
+	"  ",    /* 32 - <space> */
+	"---.",   /* 33 - ! */
+	"..-. -.",/* 34 - " (QN)*/
+	"",       /* 35 - # */
+	"... .-..",/* 36 - $ (SX) */
+	"",       /* 37 - % */
+	". ...",  /* 38 - & (ES) */
+	"..-. .-..",/* 39 - ' (QX) */
+	"..... -.", /* 40 - ( (PN) */
+	"..... .. ..", /* 41 - ) (PY) */
+	"",       /* 42 - * */
+	"",       /* 43 - + */
+	".-.-",   /* 44 - , */
+	".... .-..",/* 45 - (HX) */
+	"..--..", /* 46 - . */
+	"..- -",  /* 47 - / (UT) */
+	".--.", "..-..", "...-.", "....-", "---", "......", "--..", "-....", "-..-", "0", /* 48-57 - 0-9 */
+	"-.- . .",/* 58 - : (KO) */
+	"... ..", /* 59 - ; */
+	"",       /* 60 - < */
+	"-...-",  /* 61 - = (paragraph mark) */
+	"",       /* 62 - > */
+	"-..-.",  /* 63 - ? */
+	".--.-.", /* 64 - @ */
+	".-", "-...", ".. .", "-..", ".", ".-.", "--.", "....", "..", ".-.-", "-.-", "L", "--", /* A-M */
+	"-.", ". .", ".....", "..-.", ". ..", "...", "-", "..-", "...-", ".--", ".-..", ".. ..", "... .", /* N-Z */
+	"..... -.", /* 91 - [ (really '(') */
+	"..- -",  /* 92 - \ (really '/') */
+	"..... .. ..", /* 93 - ] (really ')') */
+	"",       /* 94 - ^ */
+	"..--.-", /* 95 - _ */
+	".----.", /* 96 - ` */
+	".-", "-...", ".. .", "-..", ".", ".-.", "--.", "....", "..", ".-.-", "-.-", "L", "--", /* a-m */
+	"-.", ". .", ".....", "..-.", ". ..", "...", "-", "..-", "...-", ".--", ".-..", ".. ..", "... .", /* n-z */
+	"..... -.", /* 123 - { (really '(') */
+	"",       /* 124 - | */
+	"..... .. ..", /* 125 - } (really ')') */
+	"..- -",  /* 126 - ~ (really bar) */
+	". . .",  /* 127 - <del> (error) */
+};
+
+static int playtone(struct ast_channel *chan, int tone, int len)
 {
+	int res;
 	char dtmf[20];
 	snprintf(dtmf, sizeof(dtmf), "%d/%d", tone, len);
 	ast_playtones_start(chan, 0, dtmf, 0);
-	ast_safe_sleep(chan, len);
+	res = ast_safe_sleep(chan, len);
 	ast_playtones_stop(chan);
+	return res;
 }
 
 static int morsecode_exec(struct ast_channel *chan, const char *data)
 {
-	int res=0, ditlen, tone;
+	int res = 0, ditlen, tone, toneoff, digit2;
 	const char *digit;
-	const char *ditlenc, *tonec;
+	const char *ditlenc, *tonec, *toneb, *codetype;
 
 	if (ast_strlen_zero(data)) {
 		ast_log(LOG_WARNING, "Syntax: Morsecode(<string>) - no argument found\n");
@@ -148,27 +205,83 @@
 	}
 	ast_channel_unlock(chan);
 
-	for (digit = data; *digit; digit++) {
-		int digit2 = *digit;
-		const char *dahdit;
-		if (digit2 < 0) {
-			continue;
-		}
-		for (dahdit = morsecode[digit2]; *dahdit; dahdit++) {
-			if (*dahdit == '-') {
-				playtone(chan, tone, 3 * ditlen);
-			} else if (*dahdit == '.') {
-				playtone(chan, tone, 1 * ditlen);
-			} else {
-				/* Account for ditlen of silence immediately following */
-				playtone(chan, 0, 2 * ditlen);
-			}
+	/* Use variable MORSEOFF, if set (else 0) */
+	ast_channel_lock(chan);
+	toneb = pbx_builtin_getvar_helper(chan, "MORSEOFF");
+	if (ast_strlen_zero(toneb) || (sscanf(toneb, "%30d", &toneoff) != 1)) {
+		toneoff = 0;
+	}
+	ast_channel_unlock(chan);
 
-			/* Pause slightly between each dit and dah */
-			playtone(chan, 0, 1 * ditlen);
+	/* Use variable MORSETYPE, if set (else INTERNATIONAL) */
+	ast_channel_lock(chan);
+	codetype = pbx_builtin_getvar_helper(chan, "MORSETYPE");
+	if (!codetype || strcmp(codetype, "AMERICAN")) {
+		codetype = "INTERNATIONAL";
+	}
+	ast_channel_unlock(chan);
+
+	if (!strcmp(codetype, "AMERICAN")) {
+		for (digit = data; *digit; digit++) {
+			const char *dahdit;
+			digit2 = *digit;
+			if (digit2 < 0 || digit2 > 127) {
+				continue;
+			}
+			for (dahdit = americanmorsecode[digit2]; *dahdit; dahdit++) {
+				if (*dahdit == '-') {
+					res = playtone(chan, tone, 3 * ditlen);
+				} else if (*dahdit == '.') {
+					res = playtone(chan, tone, 1 * ditlen);
+				} else if (*dahdit == 'L') {
+					res = playtone(chan, tone, 6 * ditlen); /* long dash */
+				} else if (*dahdit == '0') {
+					res = playtone(chan, tone, 9 * ditlen); /* extra long dash */
+				} else if (*dahdit == ' ') { /* space char (x20) = 6 dot lengths */
+					/* Intra-char pauses, specific to American Morse */
+					res = playtone(chan, toneoff, 3 * ditlen);
+				} else {
+					/* Account for ditlen of silence immediately following */
+					res = playtone(chan, toneoff, 2 * ditlen);
+				}
+
+				/* Pause slightly between each dit and dah */
+				res = playtone(chan, toneoff, 1 * ditlen);
+				if (res)
+					break;
+			}
+			/* Pause between characters */
+			res = playtone(chan, toneoff, 3 * ditlen);
+			if (res)
+				break;
 		}
-		/* Pause between characters */
-		playtone(chan, 0, 2 * ditlen);
+	} else { /* International */
+		for (digit = data; *digit; digit++) {
+			const char *dahdit;
+			digit2 = *digit;
+			if (digit2 < 0 || digit2 > 127) {
+				continue;
+			}
+			for (dahdit = internationalcode[digit2]; *dahdit; dahdit++) {
+				if (*dahdit == '-') {
+					res = playtone(chan, tone, 3 * ditlen);
+				} else if (*dahdit == '.') {
+					res = playtone(chan, tone, 1 * ditlen);
+				} else {
+					/* Account for ditlen of silence immediately following */
+					res = playtone(chan, toneoff, 2 * ditlen);
+				}
+
+				/* Pause slightly between each dit and dah */
+				res = playtone(chan, toneoff, 1 * ditlen);
+				if (res)
+					break;
+			}
+			/* Pause between characters */
+			res = playtone(chan, toneoff, 2 * ditlen);
+			if (res)
+				break;
+		}
 	}
 
 	return res;
diff --git a/doc/CHANGES-staging/app_morsecode.txt b/doc/CHANGES-staging/app_morsecode.txt
new file mode 100644
index 0000000..b9e49b6
--- /dev/null
+++ b/doc/CHANGES-staging/app_morsecode.txt
@@ -0,0 +1,6 @@
+Subject: app_morsecode
+
+Extends the Morsecode application by adding support for
+American Morse code and adds a configurable option
+for the frequency used in off intervals.
+

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16230
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I172431a2e18e6527d577e74adfb05b154cba7bd4
Gerrit-Change-Number: 16230
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210804/510252d1/attachment-0001.html>


More information about the asterisk-code-review mailing list