Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:49:21 2009

## Asterisk developer's documentation

# tdd.c File Reference

TTY/TDD Generation support. More...

`#include <time.h>`
`#include <string.h>`
`#include <stdio.h>`
`#include <stdlib.h>`
`#include <unistd.h>`
`#include <math.h>`
`#include <ctype.h>`
`#include "asterisk.h"`
`#include "asterisk/ulaw.h"`
`#include "asterisk/tdd.h"`
`#include "asterisk/logger.h"`
`#include "asterisk/fskmodem.h"`
`#include "ecdisa.h"`

Include dependency graph for tdd.c:

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_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)
struct tdd_state * | tdd_new (void)

## Variables

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

## Detailed Description

TTY/TDD Generation support.

Definition in file tdd.c.

## Define Documentation

#define PUT_AUDIO_SAMPLE | ( | y |  | ) |

Value:

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

Definition at line 210 of file tdd.c.

#define PUT_BYTE | ( | a |  | ) |

Value:

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

Definition at line 205 of file tdd.c.

Referenced by callerid_generate(), and vmwi_generate().

#define PUT_TDD | ( | byte |  | ) |

Definition at line 239 of file tdd.c.

Referenced by tdd_generate().

#define PUT_TDD_BAUD | ( | bit |  | ) |

Definition at line 222 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 216 of file tdd.c.

#define PUT_TDD_STOP

Definition at line 230 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 diable 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 115 of file tdd.c.

References ecdisa, and tdd_state::pos.

Referenced by zt_setoption().

    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 }

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','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 }

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 130 of file tdd.c.

References ast_log(), AST_MULAW, free, fsk_serie(), tdd_state::fskd, LOG_ERROR, LOG_NOTICE, LOG_WARNING, malloc, tdd_state::mode, tdd_state::oldlen, tdd_state::oldstuff, and tdd_decode_baudot().

Referenced by zt_read().

    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 }

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 186 of file tdd.c.

References free.

Referenced by zt_setoption().

    00187 {
    00188    free(tdd);
    00189 }

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)

Definition at line 250 of file tdd.c.

References tdd_state::mode, and PUT_TDD.

Referenced by zt_sendtext().

    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 }

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

Definition at line 191 of file tdd.c.

References t.

    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 }

void tdd_init | ( | void |  | ) |

CallerID Initialization

Initializes the TDD system. Mostly stuff for inverse FFT

Definition at line 81 of file tdd.c.

References TDD_MARK, and TDD_SPACE.

Referenced by main().

    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 }

struct tdd_state* tdd_new | ( | void |  | ) | `[read]`

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 90 of file tdd.c.

References ast_log(), fsk_data::bw, fsk_data::cont, fsk_data::f_mark_idx, fsk_data::f_space_idx, tdd_state::fskd, fsk_data::hdlc, LOG_WARNING, malloc, tdd_state::mode, fsk_data::nbit, fsk_data::nstop, fsk_data::paridad, fsk_data::pcola, tdd_state::pos, fsk_data::spb, fsk_data::state, and fsk_data::x0.

Referenced by zt_setoption().

    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 }

## Variable Documentation

float di[4]`[static]`

Definition at line 55 of file tdd.c.

float dr[4]`[static]`

Definition at line 55 of file tdd.c.

float tddsb = 176.0`[static]`

Definition at line 56 of file tdd.c.