Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:00:57 2007

Asterisk developer's documentation :: Codename Pineapple


tdd.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * Includes code and algorithms from the Zapata library.
00009  *
00010  * See http://www.asterisk.org for more information about
00011  * the Asterisk project. Please do not directly contact
00012  * any of the maintainers of this project for assistance;
00013  * the project provides a web site, mailing lists and IRC
00014  * channels for your use.
00015  *
00016  * This program is free software, distributed under the terms of
00017  * the GNU General Public License Version 2. See the LICENSE file
00018  * at the top of the source tree.
00019  */
00020 
00021 /*! \file
00022  *
00023  * \brief TTY/TDD Generation support 
00024  *
00025  * \author Mark Spencer <markster@digium.com>
00026  *
00027  * \note Includes code and algorithms from the Zapata library.
00028  */
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 48619 $")
00033 
00034 #include <time.h>
00035 #include <string.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <unistd.h>
00039 #include <math.h>
00040 #include <ctype.h>
00041 
00042 #include "asterisk/ulaw.h"
00043 #include "asterisk/tdd.h"
00044 #include "asterisk/logger.h"
00045 #include "asterisk/fskmodem.h"
00046 #include "ecdisa.h"
00047 
00048 struct tdd_state {
00049    fsk_data fskd;
00050    char rawdata[256];
00051    short oldstuff[4096];
00052    int oldlen;
00053    int pos;
00054    int modo;
00055    int mode;
00056 };
00057 
00058 static float dr[4], di[4];
00059 static float tddsb = 176.0;  /* 45.5 baud */
00060 
00061 #define TDD_SPACE 1800.0      /* 1800 hz for "0" */
00062 #define TDD_MARK  1400.0      /* 1400 hz for "1" */
00063 
00064 static int tdd_decode_baudot(struct tdd_state *tdd,unsigned char data)  /* covert baudot into ASCII */
00065 {
00066    static char ltrs[32] = { '<','E','\n','A',' ','S','I','U',
00067                             '\n','D','R','J','N','F','C','K',
00068                             'T','Z','L','W','H','Y','P','Q',
00069                             'O','B','G','^','M','X','V','^' };
00070    static char figs[32] = { '<','3','\n','-',' ',',','8','7',
00071                             '\n','$','4','\'',',','·',':','(',
00072                             '5','+',')','2','·','6','0','1',
00073                             '9','7','·','^','.','/','=','^' };
00074    int d = 0;  /* return 0 if not decodeable */
00075    switch (data) {
00076    case 0x1f:
00077       tdd->modo = 0;
00078       break;
00079    case 0x1b:
00080       tdd->modo = 1;
00081       break;
00082    default:
00083       if (tdd->modo == 0)
00084          d = ltrs[data];
00085       else
00086          d = figs[data];
00087       break;
00088    }
00089    return d;
00090 }
00091 
00092 void tdd_init(void)
00093 {
00094    /* Initialize stuff for inverse FFT */
00095    dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0);
00096    di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0);
00097    dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0);
00098    di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0);
00099 }
00100 
00101 struct tdd_state *tdd_new(void)
00102 {
00103    struct tdd_state *tdd;
00104    tdd = malloc(sizeof(struct tdd_state));
00105    if (tdd) {
00106       memset(tdd, 0, sizeof(struct tdd_state));
00107       tdd->fskd.spb = 176;        /* 45.5 baud */
00108       tdd->fskd.hdlc = 0;         /* Async */
00109       tdd->fskd.nbit = 5;         /* 5 bits */
00110       tdd->fskd.nstop = 1.5;      /* 1.5 stop bits */
00111       tdd->fskd.parity = 0;       /* No parity */
00112       tdd->fskd.bw=0;             /* Filter 75 Hz */
00113       tdd->fskd.f_mark_idx = 0;   /* 1400 Hz */
00114       tdd->fskd.f_space_idx = 1;  /* 1800 Hz */
00115       tdd->fskd.pcola = 0;        /* No clue */
00116       tdd->fskd.cont = 0;         /* Digital PLL reset */
00117       tdd->fskd.x0 = 0.0;
00118       tdd->fskd.state = 0;
00119       tdd->pos = 0;
00120       tdd->mode = 2;
00121    } else
00122       ast_log(LOG_WARNING, "Out of memory\n");
00123    return tdd;
00124 }
00125 
00126 int ast_tdd_gen_ecdisa(unsigned char *outbuf, int len)
00127 {
00128    int pos = 0;
00129    int cnt;
00130    while (len) {
00131       cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len;
00132       memcpy(outbuf + pos, ecdisa, cnt);
00133       pos += cnt;
00134       len -= cnt;
00135    }
00136    return 0;
00137 }
00138 
00139 int tdd_feed(struct tdd_state *tdd, unsigned char *ubuf, int len)
00140 {
00141    int mylen = len;
00142    int olen;
00143    int b = 'X';
00144    int res;
00145    int c,x;
00146    short *buf = malloc(2 * len + tdd->oldlen);
00147    short *obuf = buf;
00148    if (!buf) {
00149       ast_log(LOG_WARNING, "Out of memory\n");
00150       return -1;
00151    }
00152    memset(buf, 0, 2 * len + tdd->oldlen);
00153    memcpy(buf, tdd->oldstuff, tdd->oldlen);
00154    mylen += tdd->oldlen/2;
00155    for (x = 0; x < len; x++) 
00156       buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]);
00157    c = res = 0;
00158    while (mylen >= 1320) { /* has to have enough to work on */
00159       olen = mylen;
00160       res = fsk_serial(&tdd->fskd, buf, &mylen, &b);
00161       if (mylen < 0) {
00162          ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen);
00163          free(obuf);
00164          return -1;
00165       }
00166       buf += (olen - mylen);
00167       if (res < 0) {
00168          ast_log(LOG_NOTICE, "fsk_serial failed\n");
00169          free(obuf);
00170          return -1;
00171       }
00172       if (res == 1) {
00173          /* Ignore invalid bytes */
00174          if (b > 0x7f)
00175             continue;
00176          c = tdd_decode_baudot(tdd,b);
00177          if ((c < 1) || (c > 126))
00178             continue; /* if not valid */
00179          break;
00180       }
00181    }
00182    if (mylen) {
00183       memcpy(tdd->oldstuff, buf, mylen * 2);
00184       tdd->oldlen = mylen * 2;
00185    } else
00186       tdd->oldlen = 0;
00187    free(obuf);
00188    if (res) {
00189       tdd->mode = 2; /* put it in mode where it
00190          reliably puts teleprinter in correct shift mode */
00191       return(c);
00192    }
00193    return 0;
00194 }
00195 
00196 void tdd_free(struct tdd_state *tdd)
00197 {
00198    free(tdd);
00199 }
00200 
00201 static inline float tdd_getcarrier(float *cr, float *ci, int bit)
00202 {
00203    /* Move along.  There's nothing to see here... */
00204    float t;
00205    t = *cr * dr[bit] - *ci * di[bit];
00206    *ci = *cr * di[bit] + *ci * dr[bit];
00207    *cr = t;
00208    
00209    t = 2.0 - (*cr * *cr + *ci * *ci);
00210    *cr *= t;
00211    *ci *= t;
00212    return *cr;
00213 }  
00214 
00215 #define PUT_BYTE(a) do { \
00216    *(buf++) = (a); \
00217    bytes++; \
00218 } while(0)
00219 
00220 #define PUT_AUDIO_SAMPLE(y) do { \
00221    int index = (short)(rint(8192.0 * (y))); \
00222    *(buf++) = AST_LIN2MU(index); \
00223    bytes++; \
00224 } while(0)
00225    
00226 #define PUT_TDD_MARKMS do { \
00227    int x; \
00228    for (x=0;x<8;x++) \
00229       PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
00230 } while(0)
00231 
00232 #define PUT_TDD_BAUD(bit) do { \
00233    while(scont < tddsb) { \
00234       PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, bit)); \
00235       scont += 1.0; \
00236    } \
00237    scont -= tddsb; \
00238 } while(0)
00239 
00240 #define PUT_TDD_STOP do { \
00241    while(scont < (tddsb * 1.5)) { \
00242       PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
00243       scont += 1.0; \
00244    } \
00245    scont -= (tddsb * 1.5); \
00246 } while(0)
00247 
00248 
00249 #define PUT_TDD(byte) do { \
00250    int z; \
00251    unsigned char b = (byte); \
00252    PUT_TDD_BAUD(0);  /* Start bit */ \
00253    for (z = 0; z < 5; z++) { \
00254       PUT_TDD_BAUD(b & 1); \
00255       b >>= 1; \
00256    } \
00257    PUT_TDD_STOP;  /* Stop bit */ \
00258 } while(0); 
00259 
00260 int tdd_generate(struct tdd_state *tdd, unsigned char *buf, const char *str)
00261 {
00262    int bytes=0;
00263    int i,x;
00264    char  c;
00265    /*! Baudot letters */
00266    static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
00267    /*! Baudot figures */
00268    static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?&\000./;";
00269    /* Initial carriers (real/imaginary) */
00270    float cr = 1.0;
00271    float ci = 0.0;
00272    float scont = 0.0;
00273 
00274    for(x = 0; str[x]; x++) {
00275       c = toupper(str[x]);
00276 #if   0
00277       printf("%c",c); fflush(stdout);
00278 #endif
00279       if (c == 0) { /* send null */
00280          PUT_TDD(0);
00281          continue;
00282       }
00283       if (c == '\r') { /* send c/r */
00284          PUT_TDD(8);
00285          continue;
00286       }
00287       if (c == '\n') { /* send c/r and l/f */
00288          PUT_TDD(8);
00289          PUT_TDD(2);
00290          continue;
00291       }
00292       if (c == ' ') { /* send space */
00293          PUT_TDD(4);
00294          continue;
00295       }
00296       for (i = 0; i < 31; i++) {
00297          if (lstr[i] == c)
00298             break;
00299       }
00300       if (i < 31) {        /* if we found it */
00301          if (tdd->mode) { /* if in figs mode, change it */
00302             PUT_TDD(31); /* Send LTRS */
00303             tdd->mode = 0;
00304          }
00305          PUT_TDD(i);
00306          continue;
00307       }
00308       for (i = 0; i < 31; i++) {
00309          if (fstr[i] == c)
00310             break;
00311       }
00312       if (i < 31) {             /* if we found it */
00313          if (tdd->mode != 1) { /* if in ltrs mode, change it */
00314             PUT_TDD(27);      /* send FIGS */
00315             tdd->mode = 1;
00316          }
00317          PUT_TDD(i);           /* send byte */
00318          continue;
00319       }
00320    }
00321    return bytes;
00322 }
00323 

Asterisk is a trademark for Digium, inc.. | Edvina.net | Asterisk.org | This documentation was generated with Doxygen