Thu Jul 9 13:41:34 2009

Asterisk developer's documentation


tdd.c File Reference

TTY/TDD Generation support. More...

#include "asterisk.h"
#include <time.h>
#include <math.h>
#include <ctype.h>
#include "asterisk/logger.h"
#include "asterisk/ulaw.h"
#include "asterisk/tdd.h"
#include "asterisk/fskmodem.h"
#include "ecdisa.h"

Go to the source code of this file.

Data Structures

struct  tdd_state

Defines

#define PUT_AUDIO_SAMPLE(y)
#define PUT_BYTE(a)
#define PUT_TDD(byte)
#define PUT_TDD_BAUD(bit)
#define PUT_TDD_MARKMS
#define PUT_TDD_STOP
#define TDD_MARK   1400.0
#define TDD_SPACE   1800.0

Functions

int ast_tdd_gen_ecdisa (unsigned char *outbuf, int len)
static int tdd_decode_baudot (struct tdd_state *tdd, unsigned char data)
int tdd_feed (struct tdd_state *tdd, unsigned char *ubuf, int len)
void tdd_free (struct tdd_state *tdd)
int tdd_gen_holdtone (unsigned char *buf)
int tdd_generate (struct tdd_state *tdd, unsigned char *buf, const char *str)
static float tdd_getcarrier (float *cr, float *ci, int bit)
void tdd_init (void)
tdd_statetdd_new (void)

Variables

static float di [4]
static float dr [4]
static float tddsb = 176.0


Detailed Description

TTY/TDD Generation support.

Author:
Mark Spencer <markster@digium.com>
Note:
Includes code and algorithms from the Zapata library.

Definition in file tdd.c.


Define Documentation

#define PUT_AUDIO_SAMPLE (  ) 

Value:

do { \
   int index = (short)(rint(8192.0 * (y))); \
   *(buf++) = AST_LIN2MU(index); \
   bytes++; \
} while(0)

Definition at line 237 of file tdd.c.

Referenced by tdd_gen_holdtone().

#define PUT_BYTE (  ) 

Value:

do { \
   *(buf++) = (a); \
   bytes++; \
} while(0)

Definition at line 232 of file tdd.c.

Referenced by callerid_generate(), and vmwi_generate().

#define PUT_TDD ( byte   ) 

Definition at line 266 of file tdd.c.

Referenced by tdd_generate().

#define PUT_TDD_BAUD ( bit   ) 

Definition at line 249 of file tdd.c.

#define PUT_TDD_MARKMS

Value:

do { \
   int x; \
   for (x = 0; x < 8; x++) \
      PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
} while(0)

Definition at line 243 of file tdd.c.

#define PUT_TDD_STOP

Definition at line 257 of file tdd.c.

#define TDD_MARK   1400.0

Definition at line 59 of file tdd.c.

Referenced by tdd_init().

#define TDD_SPACE   1800.0

Definition at line 58 of file tdd.c.

Referenced by tdd_init().


Function Documentation

int ast_tdd_gen_ecdisa ( unsigned char *  outbuf,
int  len 
)

Generate Echo Canceller disable tone (2100HZ)

Parameters:
outbuf This is the buffer to receive the tone data
len This is the length (in samples) of the tone data to generate Returns 0 if no error, and -1 if error.

Definition at line 143 of file tdd.c.

References ecdisa, and tdd_state::pos.

Referenced by dahdi_setoption().

00144 {
00145    int pos = 0;
00146    int cnt;
00147    while (len) {
00148       cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len;
00149       memcpy(outbuf + pos, ecdisa, cnt);
00150       pos += cnt;
00151       len -= cnt;
00152    }
00153    return 0;
00154 }

static int tdd_decode_baudot ( struct tdd_state tdd,
unsigned char  data 
) [static]

Definition at line 61 of file tdd.c.

References tdd_state::modo.

Referenced by tdd_feed().

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','?','+','^','.','/',';','^' };
00071    int d = 0;  /* return 0 if not decodeable */
00072    switch (data) {
00073    case 0x1f:
00074       tdd->modo = 0;
00075       break;
00076    case 0x1b:
00077       tdd->modo = 1;
00078       break;
00079    default:
00080       if (tdd->modo == 0)
00081          d = ltrs[data];
00082       else
00083          d = figs[data];
00084       break;
00085    }
00086    return d;
00087 }

int tdd_feed ( struct tdd_state tdd,
unsigned char *  ubuf,
int  samples 
)

Read samples into the state machine, and return character (if any).

Parameters:
tdd Which state machine to act upon
ubuf containing your samples
samples number of samples contained within the buffer.
Send received audio to the TDD demodulator. Returns -1 on error, 0 for "needs more samples", and > 0 (the character) if reception of a character is complete.

Definition at line 156 of file tdd.c.

References ast_log(), AST_MULAW, buf, calloc, free, fsk_serial(), tdd_state::fskd, LOG_ERROR, LOG_NOTICE, LOG_WARNING, tdd_state::mode, tdd_state::oldlen, tdd_state::oldstuff, and tdd_decode_baudot().

Referenced by dahdi_read().

00157 {
00158    int mylen = len;
00159    int olen;
00160    int b = 'X';
00161    int res;
00162    int c,x;
00163    short *buf = calloc(1, 2 * len + tdd->oldlen);
00164    short *obuf = buf;
00165    if (!buf) {
00166       ast_log(LOG_WARNING, "Out of memory\n");
00167       return -1;
00168    }
00169    memcpy(buf, tdd->oldstuff, tdd->oldlen);
00170    mylen += tdd->oldlen / 2;
00171    for (x = 0; x < len; x++) 
00172       buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]);
00173    c = res = 0;
00174    while (mylen >= 1320) { /* has to have enough to work on */
00175       olen = mylen;
00176       res = fsk_serial(&tdd->fskd, buf, &mylen, &b);
00177       if (mylen < 0) {
00178          ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen);
00179          free(obuf);
00180          return -1;
00181       }
00182       buf += (olen - mylen);
00183       if (res < 0) {
00184          ast_log(LOG_NOTICE, "fsk_serial failed\n");
00185          free(obuf);
00186          return -1;
00187       }
00188       if (res == 1) {
00189          /* Ignore invalid bytes */
00190          if (b > 0x7f)
00191             continue;
00192          c = tdd_decode_baudot(tdd, b);
00193          if ((c < 1) || (c > 126))
00194             continue; /* if not valid */
00195          break;
00196       }
00197    }
00198    if (mylen) {
00199       memcpy(tdd->oldstuff, buf, mylen * 2);
00200       tdd->oldlen = mylen * 2;
00201    } else
00202       tdd->oldlen = 0;
00203    free(obuf);
00204    if (res) {
00205       tdd->mode = 2; 
00206 /* put it in mode where it
00207          reliably puts teleprinter in correct shift mode */
00208       return(c);
00209    }
00210    return 0;
00211 }

void tdd_free ( struct tdd_state tdd  ) 

Free a TDD state machine

Parameters:
tdd This is the tdd_state state machine to free This function frees tdd_state tdd.

Definition at line 213 of file tdd.c.

References free.

Referenced by dahdi_setoption().

00214 {
00215    free(tdd);
00216 }

int tdd_gen_holdtone ( unsigned char *  buf  ) 

Generate TDD hold tone

Definition at line 278 of file tdd.c.

References PUT_AUDIO_SAMPLE, and tdd_getcarrier().

00279 {
00280    int bytes = 0;
00281    float scont = 0.0, cr = 1.0, ci=0.0;
00282    while (scont < tddsb * 10.0) {
00283       PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1));
00284       scont += 1.0;
00285    }
00286    return bytes;
00287 }

int tdd_generate ( struct tdd_state tdd,
unsigned char *  buf,
const char *  string 
)

Generates a CallerID FSK stream in ulaw format suitable for transmission.

Parameters:
tdd tdd structure
buf Buffer to use. This needs to be large enough to accomodate all the generated samples.
string This is the string to send. This function creates a stream of TDD data in ulaw format. It returns the size (in bytes) of the data (if it returns a size of 0, there is probably an error)

Baudot letters

Baudot figures

Definition at line 289 of file tdd.c.

References tdd_state::charnum, tdd_state::mode, and PUT_TDD.

Referenced by dahdi_sendtext().

00290 {
00291    int bytes = 0;
00292    int i,x;
00293    char c;
00294    /*! Baudot letters */
00295    static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
00296    /*! Baudot figures */
00297    static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?+\000./;";
00298    /* Initial carriers (real/imaginary) */
00299    float cr = 1.0;
00300    float ci = 0.0;
00301    float scont = 0.0;
00302 
00303    for(x = 0; str[x]; x++) {
00304       /* Do synch for each 72th character */
00305       if ( (tdd->charnum++) % 72 == 0) 
00306          PUT_TDD(tdd->mode ? 27 /* FIGS */ : 31 /* LTRS */);
00307 
00308       c = toupper(str[x]);
00309 #if   0
00310       printf("%c",c); fflush(stdout);
00311 #endif
00312       if (c == 0) { /* send null */
00313          PUT_TDD(0);
00314          continue;
00315       }
00316       if (c == '\r') { /* send c/r */
00317          PUT_TDD(8);
00318          continue;
00319       }
00320       if (c == '\n') { /* send c/r and l/f */
00321          PUT_TDD(8);
00322          PUT_TDD(2);
00323          continue;
00324       }
00325       if (c == ' ') { /* send space */
00326          PUT_TDD(4);
00327          continue;
00328       }
00329       for (i = 0; i < 31; i++) {
00330          if (lstr[i] == c)
00331             break;
00332       }
00333       if (i < 31) {        /* if we found it */
00334          if (tdd->mode) { /* if in figs mode, change it */
00335             PUT_TDD(31); /* Send LTRS */
00336             tdd->mode = 0;
00337          }
00338          PUT_TDD(i);
00339          continue;
00340       }
00341       for (i = 0; i < 31; i++) {
00342          if (fstr[i] == c)
00343             break;
00344       }
00345       if (i < 31) {             /* if we found it */
00346          if (tdd->mode != 1) { /* if in ltrs mode, change it */
00347             PUT_TDD(27);      /* send FIGS */
00348             tdd->mode = 1;
00349          }
00350          PUT_TDD(i);           /* send byte */
00351          continue;
00352       }
00353    }
00354    return bytes;
00355 }

static float tdd_getcarrier ( float *  cr,
float *  ci,
int  bit 
) [inline, static]

Definition at line 218 of file tdd.c.

Referenced by tdd_gen_holdtone().

00219 {
00220    /* Move along.  There's nothing to see here... */
00221    float t;
00222    t = *cr * dr[bit] - *ci * di[bit];
00223    *ci = *cr * di[bit] + *ci * dr[bit];
00224    *cr = t;
00225    
00226    t = 2.0 - (*cr * *cr + *ci * *ci);
00227    *cr *= t;
00228    *ci *= t;
00229    return *cr;
00230 }  

void tdd_init ( void   ) 

CallerID Initialization Initializes the TDD system. Mostly stuff for inverse FFT

Definition at line 89 of file tdd.c.

References cos, TDD_MARK, and TDD_SPACE.

Referenced by main().

00090 {
00091    /* Initialize stuff for inverse FFT */
00092    dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0);
00093    di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0);
00094    dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0);
00095    di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0);
00096 }

struct tdd_state* tdd_new ( void   ) 

Create a TDD state machine This function returns a malloc'd instance of the tdd_state data structure. Returns a pointer to a malloc'd tdd_state structure, or NULL on error.

Definition at line 98 of file tdd.c.

References ast_log(), calloc, fskmodem_init(), and LOG_WARNING.

Referenced by dahdi_setoption().

00099 {
00100    struct tdd_state *tdd;
00101    tdd = calloc(1, sizeof(*tdd));
00102    if (tdd) {
00103 #ifdef INTEGER_CALLERID
00104       tdd->fskd.ispb = 176;        /* 45.5 baud */
00105       /* Set up for 45.5 / 8000 freq *32 to allow ints */
00106       tdd->fskd.pllispb  = (int)((8000 * 32 * 2) / 90);
00107       tdd->fskd.pllids   = tdd->fskd.pllispb / 32;
00108       tdd->fskd.pllispb2 = tdd->fskd.pllispb / 2;
00109       tdd->fskd.hdlc = 0;         /* Async */
00110       tdd->fskd.nbit = 5;         /* 5 bits */
00111       tdd->fskd.instop = 1;       /* integer rep of 1.5 stop bits */
00112       tdd->fskd.parity = 0;       /* No parity */
00113       tdd->fskd.bw=0;             /* Filter 75 Hz */
00114       tdd->fskd.f_mark_idx = 0;   /* 1400 Hz */
00115       tdd->fskd.f_space_idx = 1;  /* 1800 Hz */
00116       tdd->fskd.xi0  = 0;
00117       tdd->fskd.state = 0;
00118       tdd->pos = 0;
00119       tdd->mode = 0;
00120       fskmodem_init(&tdd->fskd);
00121 #else
00122       tdd->fskd.spb = 176;        /* 45.5 baud */
00123       tdd->fskd.hdlc = 0;         /* Async */
00124       tdd->fskd.nbit = 5;         /* 5 bits */
00125       tdd->fskd.nstop = 1.5;      /* 1.5 stop bits */
00126       tdd->fskd.parity = 0;       /* No parity */
00127       tdd->fskd.bw=0;             /* Filter 75 Hz */
00128       tdd->fskd.f_mark_idx = 0;   /* 1400 Hz */
00129       tdd->fskd.f_space_idx = 1;  /* 1800 Hz */
00130       tdd->fskd.pcola = 0;        /* No clue */
00131       tdd->fskd.cont = 0;         /* Digital PLL reset */
00132       tdd->fskd.x0 = 0.0;
00133       tdd->fskd.state = 0;
00134       tdd->pos = 0;
00135       tdd->mode = 2;
00136 #endif
00137       tdd->charnum = 0;
00138    } else
00139       ast_log(LOG_WARNING, "Out of memory\n");
00140    return tdd;
00141 }


Variable Documentation

float di[4] [static]

Definition at line 55 of file tdd.c.

Referenced by dial_exec_full(), dialed_interface_destroy(), dialed_interface_duplicate(), and try_calling().

float dr[4] [static]

Definition at line 55 of file tdd.c.

Referenced by abort_request(), append_transaction(), build_transactions(), cancel_request(), check_request(), discover_transactions(), dundi_do_lookup(), dundi_lookup_internal(), dundi_lookup_local(), dundi_lookup_thread(), dundi_precache_internal(), dundi_prop_precache(), dundi_query_eid_internal(), dundifunc_read(), optimize_transactions(), precache_trans(), precache_transactions(), query_transactions(), register_request(), and unregister_request().

float tddsb = 176.0 [static]

Definition at line 56 of file tdd.c.


Generated on Thu Jul 9 13:41:34 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7