2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Includes code and algorithms from the Zapata library.
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
23 * \brief TTY/TDD Generation support
25 * \author Mark Spencer <markster@digium.com>
27 * \note Includes code and algorithms from the Zapata library.
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/ulaw.h"
39 #include "asterisk/tdd.h"
40 #include "asterisk/fskmodem.h"
54 static float dr[4], di[4];
55 static float tddsb = 176.0; /* 45.5 baud */
57 #define TDD_SPACE 1800.0 /* 1800 hz for "0" */
58 #define TDD_MARK 1400.0 /* 1400 hz for "1" */
60 static int tdd_decode_baudot(struct tdd_state *tdd,unsigned char data) /* covert baudot into ASCII */
62 static char ltrs[32] = { '<','E','\n','A',' ','S','I','U',
63 '\n','D','R','J','N','F','C','K',
64 'T','Z','L','W','H','Y','P','Q',
65 'O','B','G','^','M','X','V','^' };
66 static char figs[32] = { '<','3','\n','-',' ','\'','8','7',
67 '\n','$','4','\'',',','!',':','(',
68 '5','\"',')','2','=','6','0','1',
69 '9','?','+','^','.','/',';','^' };
70 int d = 0; /* return 0 if not decodeable */
90 /* Initialize stuff for inverse FFT */
91 dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0);
92 di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0);
93 dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0);
94 di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0);
97 struct tdd_state *tdd_new(void)
99 struct tdd_state *tdd;
100 tdd = calloc(1, sizeof(*tdd));
102 #ifdef INTEGER_CALLERID
103 tdd->fskd.ispb = 176; /* 45.5 baud */
104 /* Set up for 45.5 / 8000 freq *32 to allow ints */
105 tdd->fskd.pllispb = (int)((8000 * 32 * 2) / 90);
106 tdd->fskd.pllids = tdd->fskd.pllispb / 32;
107 tdd->fskd.pllispb2 = tdd->fskd.pllispb / 2;
108 tdd->fskd.hdlc = 0; /* Async */
109 tdd->fskd.nbit = 5; /* 5 bits */
110 tdd->fskd.instop = 1; /* integer rep of 1.5 stop bits */
111 tdd->fskd.parity = 0; /* No parity */
112 tdd->fskd.bw=0; /* Filter 75 Hz */
113 tdd->fskd.f_mark_idx = 0; /* 1400 Hz */
114 tdd->fskd.f_space_idx = 1; /* 1800 Hz */
119 fskmodem_init(&tdd->fskd);
121 tdd->fskd.spb = 176; /* 45.5 baud */
122 tdd->fskd.hdlc = 0; /* Async */
123 tdd->fskd.nbit = 5; /* 5 bits */
124 tdd->fskd.nstop = 1.5; /* 1.5 stop bits */
125 tdd->fskd.parity = 0; /* No parity */
126 tdd->fskd.bw=0; /* Filter 75 Hz */
127 tdd->fskd.f_mark_idx = 0; /* 1400 Hz */
128 tdd->fskd.f_space_idx = 1; /* 1800 Hz */
129 tdd->fskd.pcola = 0; /* No clue */
130 tdd->fskd.cont = 0; /* Digital PLL reset */
138 ast_log(LOG_WARNING, "Out of memory\n");
142 int ast_tdd_gen_ecdisa(unsigned char *outbuf, int len)
147 cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len;
148 memcpy(outbuf + pos, ecdisa, cnt);
155 int tdd_feed(struct tdd_state *tdd, unsigned char *ubuf, int len)
162 short *buf = calloc(1, 2 * len + tdd->oldlen);
165 ast_log(LOG_WARNING, "Out of memory\n");
168 memcpy(buf, tdd->oldstuff, tdd->oldlen);
169 mylen += tdd->oldlen / 2;
170 for (x = 0; x < len; x++)
171 buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]);
173 while (mylen >= 1320) { /* has to have enough to work on */
175 res = fsk_serial(&tdd->fskd, buf, &mylen, &b);
177 ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen);
181 buf += (olen - mylen);
183 ast_log(LOG_NOTICE, "fsk_serial failed\n");
188 /* Ignore invalid bytes */
191 c = tdd_decode_baudot(tdd, b);
192 if ((c < 1) || (c > 126))
193 continue; /* if not valid */
198 memcpy(tdd->oldstuff, buf, mylen * 2);
199 tdd->oldlen = mylen * 2;
205 /* put it in mode where it
206 reliably puts teleprinter in correct shift mode */
212 void tdd_free(struct tdd_state *tdd)
217 static inline float tdd_getcarrier(float *cr, float *ci, int bit)
219 /* Move along. There's nothing to see here... */
221 t = *cr * dr[bit] - *ci * di[bit];
222 *ci = *cr * di[bit] + *ci * dr[bit];
225 t = 2.0 - (*cr * *cr + *ci * *ci);
231 #define PUT_BYTE(a) do { \
236 #define PUT_AUDIO_SAMPLE(y) do { \
237 int __pas_idx = (short)(rint(8192.0 * (y))); \
238 *(buf++) = AST_LIN2MU(__pas_idx); \
242 #define PUT_TDD_MARKMS do { \
244 for (x = 0; x < 8; x++) \
245 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
248 #define PUT_TDD_BAUD(bit) do { \
249 while (scont < tddsb) { \
250 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, bit)); \
256 #define PUT_TDD_STOP do { \
257 while (scont < (tddsb * 1.5)) { \
258 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
261 scont -= (tddsb * 1.5); \
265 #define PUT_TDD(byte) do { \
267 unsigned char b = (byte); \
268 PUT_TDD_BAUD(0); /* Start bit */ \
269 for (z = 0; z < 5; z++) { \
270 PUT_TDD_BAUD(b & 1); \
273 PUT_TDD_STOP; /* Stop bit */ \
276 /*! Generate TDD hold tone */
277 int tdd_gen_holdtone(unsigned char *buf)
280 float scont = 0.0, cr = 1.0, ci=0.0;
281 while (scont < tddsb * 10.0) {
282 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1));
288 int tdd_generate(struct tdd_state *tdd, unsigned char *buf, const char *str)
293 /*! Baudot letters */
294 static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
295 /*! Baudot figures */
296 static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?+\000./;";
297 /* Initial carriers (real/imaginary) */
302 for(x = 0; str[x]; x++) {
303 /* Do synch for each 72th character */
304 if ( (tdd->charnum++) % 72 == 0)
305 PUT_TDD(tdd->mode ? 27 /* FIGS */ : 31 /* LTRS */);
309 printf("%c",c); fflush(stdout);
311 if (c == 0) { /* send null */
315 if (c == '\r') { /* send c/r */
319 if (c == '\n') { /* send c/r and l/f */
324 if (c == ' ') { /* send space */
328 for (i = 0; i < 31; i++) {
332 if (i < 31) { /* if we found it */
333 if (tdd->mode) { /* if in figs mode, change it */
334 PUT_TDD(31); /* Send LTRS */
340 for (i = 0; i < 31; i++) {
344 if (i < 31) { /* if we found it */
345 if (tdd->mode != 1) { /* if in ltrs mode, change it */
346 PUT_TDD(27); /* send FIGS */
349 PUT_TDD(i); /* send byte */