Merged revisions 116463 via svnmerge from
[asterisk-bristuff.git] / main / tdd.c
blobe07d5b0d15561ebf12b2d7ddf47356f5e65ce119
1 /*
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.
21 /*! \file
23 * \brief TTY/TDD Generation support
25 * \author Mark Spencer <markster@digium.com>
27 * \note Includes code and algorithms from the Zapata library.
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <time.h>
35 #include <math.h>
36 #include <ctype.h>
38 #include "asterisk/ulaw.h"
39 #include "asterisk/tdd.h"
40 #include "asterisk/fskmodem.h"
41 #include "ecdisa.h"
43 struct tdd_state {
44 fsk_data fskd;
45 char rawdata[256];
46 short oldstuff[4096];
47 int oldlen;
48 int pos;
49 int modo;
50 int mode;
51 int charnum;
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 */
71 switch (data) {
72 case 0x1f:
73 tdd->modo = 0;
74 break;
75 case 0x1b:
76 tdd->modo = 1;
77 break;
78 default:
79 if (tdd->modo == 0)
80 d = ltrs[data];
81 else
82 d = figs[data];
83 break;
85 return d;
88 void tdd_init(void)
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));
101 if (tdd) {
102 tdd->fskd.ispb = 176; /* 45.5 baud */
103 /* Set up for 45.5 / 8000 freq *32 to allow ints */
104 tdd->fskd.pllispb = (int)((8000 * 32 * 2) / 90);
105 tdd->fskd.pllids = tdd->fskd.pllispb / 32;
106 tdd->fskd.pllispb2 = tdd->fskd.pllispb / 2;
107 tdd->fskd.hdlc = 0; /* Async */
108 tdd->fskd.nbit = 5; /* 5 bits */
109 tdd->fskd.instop = 1; /* integer rep of 1.5 stop bits */
110 tdd->fskd.parity = 0; /* No parity */
111 tdd->fskd.bw=0; /* Filter 75 Hz */
112 tdd->fskd.f_mark_idx = 0; /* 1400 Hz */
113 tdd->fskd.f_space_idx = 1; /* 1800 Hz */
114 tdd->fskd.xi0 = 0;
115 tdd->fskd.state = 0;
116 tdd->pos = 0;
117 tdd->mode = 0;
118 tdd->charnum = 0;
119 fskmodem_init(&tdd->fskd);
120 } else
121 ast_log(LOG_WARNING, "Out of memory\n");
122 return tdd;
125 int ast_tdd_gen_ecdisa(unsigned char *outbuf, int len)
127 int pos = 0;
128 int cnt;
129 while (len) {
130 cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len;
131 memcpy(outbuf + pos, ecdisa, cnt);
132 pos += cnt;
133 len -= cnt;
135 return 0;
138 int tdd_feed(struct tdd_state *tdd, unsigned char *ubuf, int len)
140 int mylen = len;
141 int olen;
142 int b = 'X';
143 int res;
144 int c,x;
145 short *buf = calloc(1, 2 * len + tdd->oldlen);
146 short *obuf = buf;
147 if (!buf) {
148 ast_log(LOG_WARNING, "Out of memory\n");
149 return -1;
151 memcpy(buf, tdd->oldstuff, tdd->oldlen);
152 mylen += tdd->oldlen / 2;
153 for (x = 0; x < len; x++)
154 buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]);
155 c = res = 0;
156 while (mylen >= 1320) { /* has to have enough to work on */
157 olen = mylen;
158 res = fsk_serial(&tdd->fskd, buf, &mylen, &b);
159 if (mylen < 0) {
160 ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen);
161 free(obuf);
162 return -1;
164 buf += (olen - mylen);
165 if (res < 0) {
166 ast_log(LOG_NOTICE, "fsk_serial failed\n");
167 free(obuf);
168 return -1;
170 if (res == 1) {
171 /* Ignore invalid bytes */
172 if (b > 0x7f)
173 continue;
174 c = tdd_decode_baudot(tdd, b);
175 if ((c < 1) || (c > 126))
176 continue; /* if not valid */
177 break;
180 if (mylen) {
181 memcpy(tdd->oldstuff, buf, mylen * 2);
182 tdd->oldlen = mylen * 2;
183 } else
184 tdd->oldlen = 0;
185 free(obuf);
186 if (res) {
187 tdd->mode = 2;
188 /* put it in mode where it
189 reliably puts teleprinter in correct shift mode */
190 return(c);
192 return 0;
195 void tdd_free(struct tdd_state *tdd)
197 free(tdd);
200 static inline float tdd_getcarrier(float *cr, float *ci, int bit)
202 /* Move along. There's nothing to see here... */
203 float t;
204 t = *cr * dr[bit] - *ci * di[bit];
205 *ci = *cr * di[bit] + *ci * dr[bit];
206 *cr = t;
208 t = 2.0 - (*cr * *cr + *ci * *ci);
209 *cr *= t;
210 *ci *= t;
211 return *cr;
214 #define PUT_BYTE(a) do { \
215 *(buf++) = (a); \
216 bytes++; \
217 } while(0)
219 #define PUT_AUDIO_SAMPLE(y) do { \
220 int index = (short)(rint(8192.0 * (y))); \
221 *(buf++) = AST_LIN2MU(index); \
222 bytes++; \
223 } while(0)
225 #define PUT_TDD_MARKMS do { \
226 int x; \
227 for (x = 0; x < 8; x++) \
228 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
229 } while(0)
231 #define PUT_TDD_BAUD(bit) do { \
232 while (scont < tddsb) { \
233 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, bit)); \
234 scont += 1.0; \
236 scont -= tddsb; \
237 } while(0)
239 #define PUT_TDD_STOP do { \
240 while (scont < (tddsb * 1.5)) { \
241 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
242 scont += 1.0; \
244 scont -= (tddsb * 1.5); \
245 } while(0)
248 #define PUT_TDD(byte) do { \
249 int z; \
250 unsigned char b = (byte); \
251 PUT_TDD_BAUD(0); /* Start bit */ \
252 for (z = 0; z < 5; z++) { \
253 PUT_TDD_BAUD(b & 1); \
254 b >>= 1; \
256 PUT_TDD_STOP; /* Stop bit */ \
257 } while(0);
259 /*! Generate TDD hold tone */
260 int tdd_gen_holdtone(unsigned char *buf)
262 int bytes = 0;
263 float scont = 0.0, cr = 1.0, ci=0.0;
264 while (scont < tddsb * 10.0) {
265 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1));
266 scont += 1.0;
268 return bytes;
271 int tdd_generate(struct tdd_state *tdd, unsigned char *buf, const char *str)
273 int bytes = 0;
274 int i,x;
275 char c;
276 /*! Baudot letters */
277 static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
278 /*! Baudot figures */
279 static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?+\000./;";
280 /* Initial carriers (real/imaginary) */
281 float cr = 1.0;
282 float ci = 0.0;
283 float scont = 0.0;
285 for(x = 0; str[x]; x++) {
286 /* Do synch for each 72th character */
287 if ( (tdd->charnum++) % 72 == 0)
288 PUT_TDD(tdd->mode ? 27 /* FIGS */ : 31 /* LTRS */);
290 c = toupper(str[x]);
291 #if 0
292 printf("%c",c); fflush(stdout);
293 #endif
294 if (c == 0) { /* send null */
295 PUT_TDD(0);
296 continue;
298 if (c == '\r') { /* send c/r */
299 PUT_TDD(8);
300 continue;
302 if (c == '\n') { /* send c/r and l/f */
303 PUT_TDD(8);
304 PUT_TDD(2);
305 continue;
307 if (c == ' ') { /* send space */
308 PUT_TDD(4);
309 continue;
311 for (i = 0; i < 31; i++) {
312 if (lstr[i] == c)
313 break;
315 if (i < 31) { /* if we found it */
316 if (tdd->mode) { /* if in figs mode, change it */
317 PUT_TDD(31); /* Send LTRS */
318 tdd->mode = 0;
320 PUT_TDD(i);
321 continue;
323 for (i = 0; i < 31; i++) {
324 if (fstr[i] == c)
325 break;
327 if (i < 31) { /* if we found it */
328 if (tdd->mode != 1) { /* if in ltrs mode, change it */
329 PUT_TDD(27); /* send FIGS */
330 tdd->mode = 1;
332 PUT_TDD(i); /* send byte */
333 continue;
336 return bytes;