when a PRI call must be moved to a different B channel at the request of the other...
[asterisk-bristuff.git] / main / tdd.c
blobf1d7cf4717008937a1f51655943a05e6042844e1
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 <string.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <math.h>
40 #include <ctype.h>
42 #include "asterisk/ulaw.h"
43 #include "asterisk/tdd.h"
44 #include "asterisk/logger.h"
45 #include "asterisk/fskmodem.h"
46 #include "ecdisa.h"
48 struct tdd_state {
49 fsk_data fskd;
50 char rawdata[256];
51 short oldstuff[4096];
52 int oldlen;
53 int pos;
54 int modo;
55 int mode;
56 int charnum;
59 static float dr[4], di[4];
60 static float tddsb = 176.0; /* 45.5 baud */
62 #define TDD_SPACE 1800.0 /* 1800 hz for "0" */
63 #define TDD_MARK 1400.0 /* 1400 hz for "1" */
65 static int tdd_decode_baudot(struct tdd_state *tdd,unsigned char data) /* covert baudot into ASCII */
67 static char ltrs[32] = { '<','E','\n','A',' ','S','I','U',
68 '\n','D','R','J','N','F','C','K',
69 'T','Z','L','W','H','Y','P','Q',
70 'O','B','G','^','M','X','V','^' };
71 static char figs[32] = { '<','3','\n','-',' ','\'','8','7',
72 '\n','$','4','\'',',','!',':','(',
73 '5','\"',')','2','=','6','0','1',
74 '9','?','+','^','.','/',';','^' };
75 int d = 0; /* return 0 if not decodeable */
76 switch (data) {
77 case 0x1f:
78 tdd->modo = 0;
79 break;
80 case 0x1b:
81 tdd->modo = 1;
82 break;
83 default:
84 if (tdd->modo == 0)
85 d = ltrs[data];
86 else
87 d = figs[data];
88 break;
90 return d;
93 void tdd_init(void)
95 /* Initialize stuff for inverse FFT */
96 dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0);
97 di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0);
98 dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0);
99 di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0);
102 struct tdd_state *tdd_new(void)
104 struct tdd_state *tdd;
105 tdd = calloc(1, sizeof(*tdd));
106 if (tdd) {
107 tdd->fskd.spb = 176; /* 45.5 baud */
108 tdd->fskd.hdlc = 0; /* Async */
109 tdd->fskd.nbit = 5; /* 5 bits */
110 tdd->fskd.nstop = 1.5; /* 1.5 stop bits */
111 tdd->fskd.paridad = 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 */
115 tdd->fskd.pcola = 0; /* No clue */
116 tdd->fskd.cont = 0; /* Digital PLL reset */
117 tdd->fskd.x0 = 0.0;
118 tdd->fskd.state = 0;
119 tdd->pos = 0;
120 tdd->mode = 0;
121 tdd->charnum = 0;
122 } else
123 ast_log(LOG_WARNING, "Out of memory\n");
124 return tdd;
127 int ast_tdd_gen_ecdisa(unsigned char *outbuf, int len)
129 int pos = 0;
130 int cnt;
131 while (len) {
132 cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len;
133 memcpy(outbuf + pos, ecdisa, cnt);
134 pos += cnt;
135 len -= cnt;
137 return 0;
140 int tdd_feed(struct tdd_state *tdd, unsigned char *ubuf, int len)
142 int mylen = len;
143 int olen;
144 int b = 'X';
145 int res;
146 int c,x;
147 short *buf = calloc(1, 2 * len + tdd->oldlen);
148 short *obuf = buf;
149 if (!buf) {
150 ast_log(LOG_WARNING, "Out of memory\n");
151 return -1;
153 memcpy(buf, tdd->oldstuff, tdd->oldlen);
154 mylen += tdd->oldlen/2;
155 for (x = 0; x < len; x++)
156 buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]);
157 c = res = 0;
158 while (mylen >= 1320) { /* has to have enough to work on */
159 olen = mylen;
160 res = fsk_serie(&tdd->fskd, buf, &mylen, &b);
161 if (mylen < 0) {
162 ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen);
163 free(obuf);
164 return -1;
166 buf += (olen - mylen);
167 if (res < 0) {
168 ast_log(LOG_NOTICE, "fsk_serial failed\n");
169 free(obuf);
170 return -1;
172 if (res == 1) {
173 /* Ignore invalid bytes */
174 if (b > 0x7f)
175 continue;
176 c = tdd_decode_baudot(tdd,b);
177 if ((c < 1) || (c > 126))
178 continue; /* if not valid */
179 break;
182 if (mylen) {
183 memcpy(tdd->oldstuff, buf, mylen * 2);
184 tdd->oldlen = mylen * 2;
185 } else
186 tdd->oldlen = 0;
187 free(obuf);
188 if (res) {
189 tdd->mode = 2;
190 /* put it in mode where it
191 reliably puts teleprinter in correct shift mode */
192 return(c);
194 return 0;
197 void tdd_free(struct tdd_state *tdd)
199 free(tdd);
202 static inline float tdd_getcarrier(float *cr, float *ci, int bit)
204 /* Move along. There's nothing to see here... */
205 float t;
206 t = *cr * dr[bit] - *ci * di[bit];
207 *ci = *cr * di[bit] + *ci * dr[bit];
208 *cr = t;
210 t = 2.0 - (*cr * *cr + *ci * *ci);
211 *cr *= t;
212 *ci *= t;
213 return *cr;
216 #define PUT_BYTE(a) do { \
217 *(buf++) = (a); \
218 bytes++; \
219 } while(0)
221 #define PUT_AUDIO_SAMPLE(y) do { \
222 int index = (short)(rint(8192.0 * (y))); \
223 *(buf++) = AST_LIN2MU(index); \
224 bytes++; \
225 } while(0)
227 #define PUT_TDD_MARKMS do { \
228 int x; \
229 for (x=0;x<8;x++) \
230 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
231 } while(0)
233 #define PUT_TDD_BAUD(bit) do { \
234 while (scont < tddsb) { \
235 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, bit)); \
236 scont += 1.0; \
238 scont -= tddsb; \
239 } while(0)
241 #define PUT_TDD_STOP do { \
242 while (scont < (tddsb * 1.5)) { \
243 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
244 scont += 1.0; \
246 scont -= (tddsb * 1.5); \
247 } while(0)
250 #define PUT_TDD(byte) do { \
251 int z; \
252 unsigned char b = (byte); \
253 PUT_TDD_BAUD(0); /* Start bit */ \
254 for (z = 0; z < 5; z++) { \
255 PUT_TDD_BAUD(b & 1); \
256 b >>= 1; \
258 PUT_TDD_STOP; /* Stop bit */ \
259 } while(0);
261 int tdd_generate(struct tdd_state *tdd, unsigned char *buf, const char *str)
263 int bytes=0;
264 int i,x;
265 char c;
266 /*! Baudot letters */
267 static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
268 /*! Baudot figures */
269 static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?+\000./;";
270 /* Initial carriers (real/imaginary) */
271 float cr = 1.0;
272 float ci = 0.0;
273 float scont = 0.0;
275 for(x = 0; str[x]; x++) {
276 /* Do synch for each 72th character */
277 if ( (tdd->charnum++) % 72 == 0)
278 PUT_TDD(tdd->mode ? 27 /* FIGS */ : 31 /* LTRS */);
280 c = toupper(str[x]);
281 #if 0
282 printf("%c",c); fflush(stdout);
283 #endif
284 if (c == 0) { /* send null */
285 PUT_TDD(0);
286 continue;
288 if (c == '\r') { /* send c/r */
289 PUT_TDD(8);
290 continue;
292 if (c == '\n') { /* send c/r and l/f */
293 PUT_TDD(8);
294 PUT_TDD(2);
295 continue;
297 if (c == ' ') { /* send space */
298 PUT_TDD(4);
299 continue;
301 for (i = 0; i < 31; i++) {
302 if (lstr[i] == c)
303 break;
305 if (i < 31) { /* if we found it */
306 if (tdd->mode) { /* if in figs mode, change it */
307 PUT_TDD(31); /* Send LTRS */
308 tdd->mode = 0;
310 PUT_TDD(i);
311 continue;
313 for (i = 0; i < 31; i++) {
314 if (fstr[i] == c)
315 break;
317 if (i < 31) { /* if we found it */
318 if (tdd->mode != 1) { /* if in ltrs mode, change it */
319 PUT_TDD(27); /* send FIGS */
320 tdd->mode = 1;
322 PUT_TDD(i); /* send byte */
323 continue;
326 return bytes;