Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:56 2009

## Asterisk developer's documentation

- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Directories

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