Don't include logger.h in asterisk.h by default as it is causing problems building
[asterisk/asterisk.git] / main / tdd.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Includes code and algorithms from the Zapata library.
9  *
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.
15  *
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.
19  */
20
21 /*! \file
22  *
23  * \brief TTY/TDD Generation support 
24  *
25  * \author Mark Spencer <markster@digium.com>
26  *
27  * \note Includes code and algorithms from the Zapata library.
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <time.h>
35 #include <math.h>
36 #include <ctype.h>
37
38 #include "asterisk/logger.h"
39 #include "asterisk/ulaw.h"
40 #include "asterisk/tdd.h"
41 #include "asterisk/fskmodem.h"
42 #include "ecdisa.h"
43
44 struct tdd_state {
45         fsk_data fskd;
46         char rawdata[256];
47         short oldstuff[4096];
48         int oldlen;
49         int pos;
50         int modo;
51         int mode;
52         int charnum;
53 };
54
55 static float dr[4], di[4];
56 static float tddsb = 176.0;  /* 45.5 baud */
57
58 #define TDD_SPACE       1800.0          /* 1800 hz for "0" */
59 #define TDD_MARK        1400.0          /* 1400 hz for "1" */
60
61 static int tdd_decode_baudot(struct tdd_state *tdd,unsigned char data)  /* covert baudot into ASCII */
62 {
63         static char ltrs[32] = { '<','E','\n','A',' ','S','I','U',
64                                  '\n','D','R','J','N','F','C','K',
65                                  'T','Z','L','W','H','Y','P','Q',
66                                  'O','B','G','^','M','X','V','^' };
67         static char figs[32] = { '<','3','\n','-',' ','\'','8','7',
68                                  '\n','$','4','\'',',','!',':','(',
69                                  '5','\"',')','2','=','6','0','1',
70                                  '9','?','+','^','.','/',';','^' };
71         int d = 0;  /* return 0 if not decodeable */
72         switch (data) {
73         case 0x1f:
74                 tdd->modo = 0;
75                 break;
76         case 0x1b:
77                 tdd->modo = 1;
78                 break;
79         default:
80                 if (tdd->modo == 0)
81                         d = ltrs[data];
82                 else
83                         d = figs[data];
84                 break;
85         }
86         return d;
87 }
88
89 void tdd_init(void)
90 {
91         /* Initialize stuff for inverse FFT */
92         dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0);
93         di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0);
94         dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0);
95         di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0);
96 }
97
98 struct tdd_state *tdd_new(void)
99 {
100         struct tdd_state *tdd;
101         tdd = calloc(1, sizeof(*tdd));
102         if (tdd) {
103 #ifdef INTEGER_CALLERID
104                 tdd->fskd.ispb = 176;        /* 45.5 baud */
105                 /* Set up for 45.5 / 8000 freq *32 to allow ints */
106                 tdd->fskd.pllispb  = (int)((8000 * 32 * 2) / 90);
107                 tdd->fskd.pllids   = tdd->fskd.pllispb / 32;
108                 tdd->fskd.pllispb2 = tdd->fskd.pllispb / 2;
109                 tdd->fskd.hdlc = 0;         /* Async */
110                 tdd->fskd.nbit = 5;         /* 5 bits */
111                 tdd->fskd.instop = 1;       /* integer rep of 1.5 stop bits */
112                 tdd->fskd.parity = 0;       /* No parity */
113                 tdd->fskd.bw=0;             /* Filter 75 Hz */
114                 tdd->fskd.f_mark_idx = 0;   /* 1400 Hz */
115                 tdd->fskd.f_space_idx = 1;  /* 1800 Hz */
116                 tdd->fskd.xi0  = 0;
117                 tdd->fskd.state = 0;
118                 tdd->pos = 0;
119                 tdd->mode = 0;
120                 fskmodem_init(&tdd->fskd);
121 #else
122                 tdd->fskd.spb = 176;        /* 45.5 baud */
123                 tdd->fskd.hdlc = 0;         /* Async */
124                 tdd->fskd.nbit = 5;         /* 5 bits */
125                 tdd->fskd.nstop = 1.5;      /* 1.5 stop bits */
126                 tdd->fskd.parity = 0;       /* No parity */
127                 tdd->fskd.bw=0;             /* Filter 75 Hz */
128                 tdd->fskd.f_mark_idx = 0;   /* 1400 Hz */
129                 tdd->fskd.f_space_idx = 1;  /* 1800 Hz */
130                 tdd->fskd.pcola = 0;        /* No clue */
131                 tdd->fskd.cont = 0;         /* Digital PLL reset */
132                 tdd->fskd.x0 = 0.0;
133                 tdd->fskd.state = 0;
134                 tdd->pos = 0;
135                 tdd->mode = 2;
136 #endif
137                 tdd->charnum = 0;
138         } else
139                 ast_log(LOG_WARNING, "Out of memory\n");
140         return tdd;
141 }
142
143 int ast_tdd_gen_ecdisa(unsigned char *outbuf, int len)
144 {
145         int pos = 0;
146         int cnt;
147         while (len) {
148                 cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len;
149                 memcpy(outbuf + pos, ecdisa, cnt);
150                 pos += cnt;
151                 len -= cnt;
152         }
153         return 0;
154 }
155
156 int tdd_feed(struct tdd_state *tdd, unsigned char *ubuf, int len)
157 {
158         int mylen = len;
159         int olen;
160         int b = 'X';
161         int res;
162         int c,x;
163         short *buf = calloc(1, 2 * len + tdd->oldlen);
164         short *obuf = buf;
165         if (!buf) {
166                 ast_log(LOG_WARNING, "Out of memory\n");
167                 return -1;
168         }
169         memcpy(buf, tdd->oldstuff, tdd->oldlen);
170         mylen += tdd->oldlen / 2;
171         for (x = 0; x < len; x++) 
172                 buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]);
173         c = res = 0;
174         while (mylen >= 1320) { /* has to have enough to work on */
175                 olen = mylen;
176                 res = fsk_serial(&tdd->fskd, buf, &mylen, &b);
177                 if (mylen < 0) {
178                         ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen);
179                         free(obuf);
180                         return -1;
181                 }
182                 buf += (olen - mylen);
183                 if (res < 0) {
184                         ast_log(LOG_NOTICE, "fsk_serial failed\n");
185                         free(obuf);
186                         return -1;
187                 }
188                 if (res == 1) {
189                         /* Ignore invalid bytes */
190                         if (b > 0x7f)
191                                 continue;
192                         c = tdd_decode_baudot(tdd, b);
193                         if ((c < 1) || (c > 126))
194                                 continue; /* if not valid */
195                         break;
196                 }
197         }
198         if (mylen) {
199                 memcpy(tdd->oldstuff, buf, mylen * 2);
200                 tdd->oldlen = mylen * 2;
201         } else
202                 tdd->oldlen = 0;
203         free(obuf);
204         if (res) {
205                 tdd->mode = 2; 
206 /* put it in mode where it
207                         reliably puts teleprinter in correct shift mode */
208                 return(c);
209         }
210         return 0;
211 }
212
213 void tdd_free(struct tdd_state *tdd)
214 {
215         free(tdd);
216 }
217
218 static inline float tdd_getcarrier(float *cr, float *ci, int bit)
219 {
220         /* Move along.  There's nothing to see here... */
221         float t;
222         t = *cr * dr[bit] - *ci * di[bit];
223         *ci = *cr * di[bit] + *ci * dr[bit];
224         *cr = t;
225         
226         t = 2.0 - (*cr * *cr + *ci * *ci);
227         *cr *= t;
228         *ci *= t;
229         return *cr;
230 }       
231
232 #define PUT_BYTE(a) do { \
233         *(buf++) = (a); \
234         bytes++; \
235 } while(0)
236
237 #define PUT_AUDIO_SAMPLE(y) do { \
238         int __pas_idx = (short)(rint(8192.0 * (y))); \
239         *(buf++) = AST_LIN2MU(__pas_idx); \
240         bytes++; \
241 } while(0)
242         
243 #define PUT_TDD_MARKMS do { \
244         int x; \
245         for (x = 0; x < 8; x++) \
246                 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
247 } while(0)
248
249 #define PUT_TDD_BAUD(bit) do { \
250         while (scont < tddsb) { \
251                 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, bit)); \
252                 scont += 1.0; \
253         } \
254         scont -= tddsb; \
255 } while(0)
256
257 #define PUT_TDD_STOP do { \
258         while (scont < (tddsb * 1.5)) { \
259                 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
260                 scont += 1.0; \
261         } \
262         scont -= (tddsb * 1.5); \
263 } while(0)
264
265
266 #define PUT_TDD(byte) do { \
267         int z; \
268         unsigned char b = (byte); \
269         PUT_TDD_BAUD(0);        /* Start bit */ \
270         for (z = 0; z < 5; z++) { \
271                 PUT_TDD_BAUD(b & 1); \
272                 b >>= 1; \
273         } \
274         PUT_TDD_STOP;   /* Stop bit */ \
275 } while(0);     
276
277 /*! Generate TDD hold tone */
278 int tdd_gen_holdtone(unsigned char *buf)
279 {
280         int bytes = 0;
281         float scont = 0.0, cr = 1.0, ci=0.0;
282         while (scont < tddsb * 10.0) {
283                 PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1));
284                 scont += 1.0;
285         }
286         return bytes;
287 }
288
289 int tdd_generate(struct tdd_state *tdd, unsigned char *buf, const char *str)
290 {
291         int bytes = 0;
292         int i,x;
293         char c;
294         /*! Baudot letters */
295         static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
296         /*! Baudot figures */
297         static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?+\000./;";
298         /* Initial carriers (real/imaginary) */
299         float cr = 1.0;
300         float ci = 0.0;
301         float scont = 0.0;
302
303         for(x = 0; str[x]; x++) {
304                 /* Do synch for each 72th character */
305                 if ( (tdd->charnum++) % 72 == 0) 
306                         PUT_TDD(tdd->mode ? 27 /* FIGS */ : 31 /* LTRS */);
307
308                 c = toupper(str[x]);
309 #if     0
310                 printf("%c",c); fflush(stdout);
311 #endif
312                 if (c == 0) { /* send null */
313                         PUT_TDD(0);
314                         continue;
315                 }
316                 if (c == '\r') { /* send c/r */
317                         PUT_TDD(8);
318                         continue;
319                 }
320                 if (c == '\n') { /* send c/r and l/f */
321                         PUT_TDD(8);
322                         PUT_TDD(2);
323                         continue;
324                 }
325                 if (c == ' ') { /* send space */
326                         PUT_TDD(4);
327                         continue;
328                 }
329                 for (i = 0; i < 31; i++) {
330                         if (lstr[i] == c)
331                                 break;
332                 }
333                 if (i < 31) {        /* if we found it */
334                         if (tdd->mode) { /* if in figs mode, change it */
335                                 PUT_TDD(31); /* Send LTRS */
336                                 tdd->mode = 0;
337                         }
338                         PUT_TDD(i);
339                         continue;
340                 }
341                 for (i = 0; i < 31; i++) {
342                         if (fstr[i] == c)
343                                 break;
344                 }
345                 if (i < 31) {             /* if we found it */
346                         if (tdd->mode != 1) { /* if in ltrs mode, change it */
347                                 PUT_TDD(27);      /* send FIGS */
348                                 tdd->mode = 1;
349                         }
350                         PUT_TDD(i);           /* send byte */
351                         continue;
352                 }
353         }
354         return bytes;
355 }
356