Thu Apr 28 2011 17:14:03

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"
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_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)
struct 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 (   y)
Value:
do { \
   int __pas_idx = (short)(rint(8192.0 * (y))); \
   *(buf++) = AST_LIN2MU(__pas_idx); \
   bytes++; \
} while(0)

Definition at line 237 of file tdd.c.

Referenced by tdd_gen_holdtone().

#define PUT_BYTE (   a)
Value:
do { \
   *(buf++) = (a); \
   bytes++; \
} while(0)

Definition at line 232 of file tdd.c.

#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:
outbufThis is the buffer to receive the tone data
lenThis 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, len(), and tdd_state::pos.

Referenced by dahdi_setoption().

{
   int pos = 0;
   int cnt;
   while (len) {
      cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len;
      memcpy(outbuf + pos, ecdisa, cnt);
      pos += cnt;
      len -= cnt;
   }
   return 0;
}
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().

{
   static char ltrs[32] = { '<','E','\n','A',' ','S','I','U',
                            '\n','D','R','J','N','F','C','K',
                            'T','Z','L','W','H','Y','P','Q',
                            'O','B','G','^','M','X','V','^' };
   static char figs[32] = { '<','3','\n','-',' ','\'','8','7',
                            '\n','$','4','\'',',','!',':','(',
                            '5','\"',')','2','=','6','0','1',
                            '9','?','+','^','.','/',';','^' };
   int d = 0;  /* return 0 if not decodeable */
   switch (data) {
   case 0x1f:
      tdd->modo = 0;
      break;
   case 0x1b:
      tdd->modo = 1;
      break;
   default:
      if (tdd->modo == 0)
         d = ltrs[data];
      else
         d = figs[data];
      break;
   }
   return d;
}
int tdd_feed ( struct tdd_state tdd,
unsigned char *  ubuf,
int  samples 
)

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

Parameters:
tddWhich state machine to act upon
ubufcontaining your samples
samplesnumber 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, len(), LOG_ERROR, LOG_NOTICE, LOG_WARNING, tdd_state::mode, tdd_state::oldlen, tdd_state::oldstuff, and tdd_decode_baudot().

Referenced by dahdi_read().

{
   int mylen = len;
   int olen;
   int b = 'X';
   int res;
   int c,x;
   short *buf = calloc(1, 2 * len + tdd->oldlen);
   short *obuf = buf;
   if (!buf) {
      ast_log(LOG_WARNING, "Out of memory\n");
      return -1;
   }
   memcpy(buf, tdd->oldstuff, tdd->oldlen);
   mylen += tdd->oldlen / 2;
   for (x = 0; x < len; x++) 
      buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]);
   c = res = 0;
   while (mylen >= 1320) { /* has to have enough to work on */
      olen = mylen;
      res = fsk_serial(&tdd->fskd, buf, &mylen, &b);
      if (mylen < 0) {
         ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen);
         free(obuf);
         return -1;
      }
      buf += (olen - mylen);
      if (res < 0) {
         ast_log(LOG_NOTICE, "fsk_serial failed\n");
         free(obuf);
         return -1;
      }
      if (res == 1) {
         /* Ignore invalid bytes */
         if (b > 0x7f)
            continue;
         c = tdd_decode_baudot(tdd, b);
         if ((c < 1) || (c > 126))
            continue; /* if not valid */
         break;
      }
   }
   if (mylen) {
      memcpy(tdd->oldstuff, buf, mylen * 2);
      tdd->oldlen = mylen * 2;
   } else
      tdd->oldlen = 0;
   free(obuf);
   if (res) {
      tdd->mode = 2; 
/* put it in mode where it
         reliably puts teleprinter in correct shift mode */
      return(c);
   }
   return 0;
}
void tdd_free ( struct tdd_state tdd)

Free a TDD state machine

Parameters:
tddThis 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().

{
   free(tdd);
}
int tdd_gen_holdtone ( unsigned char *  buf)

Generate TDD hold tone

Parameters:
bufResult buffer
Todo:
How big should this be???

Definition at line 280 of file tdd.c.

References PUT_AUDIO_SAMPLE, and tdd_getcarrier().

{
   int bytes = 0;
   float scont = 0.0, cr = 1.0, ci=0.0;
   while (scont < tddsb * 10.0) {
      PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1));
      scont += 1.0;
   }
   return bytes;
}
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:
tddtdd structure
bufBuffer to use. This needs to be large enough to accomodate all the generated samples.
stringThis 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 291 of file tdd.c.

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

Referenced by dahdi_sendtext().

{
   int bytes = 0;
   int i,x;
   char c;
   /*! Baudot letters */
   static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
   /*! Baudot figures */
   static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?+\000./;";
   /* Initial carriers (real/imaginary) */
   float cr = 1.0;
   float ci = 0.0;
   float scont = 0.0;

   for(x = 0; str[x]; x++) {
      /* Do synch for each 72th character */
      if ( (tdd->charnum++) % 72 == 0) 
         PUT_TDD(tdd->mode ? 27 /* FIGS */ : 31 /* LTRS */);

      c = toupper(str[x]);
#if   0
      printf("%c",c); fflush(stdout);
#endif
      if (c == 0) { /* send null */
         PUT_TDD(0);
         continue;
      }
      if (c == '\r') { /* send c/r */
         PUT_TDD(8);
         continue;
      }
      if (c == '\n') { /* send c/r and l/f */
         PUT_TDD(8);
         PUT_TDD(2);
         continue;
      }
      if (c == ' ') { /* send space */
         PUT_TDD(4);
         continue;
      }
      for (i = 0; i < 31; i++) {
         if (lstr[i] == c)
            break;
      }
      if (i < 31) {        /* if we found it */
         if (tdd->mode) { /* if in figs mode, change it */
            PUT_TDD(31); /* Send LTRS */
            tdd->mode = 0;
         }
         PUT_TDD(i);
         continue;
      }
      for (i = 0; i < 31; i++) {
         if (fstr[i] == c)
            break;
      }
      if (i < 31) {             /* if we found it */
         if (tdd->mode != 1) { /* if in ltrs mode, change it */
            PUT_TDD(27);      /* send FIGS */
            tdd->mode = 1;
         }
         PUT_TDD(i);           /* send byte */
         continue;
      }
   }
   return bytes;
}
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().

{
   /* Move along.  There's nothing to see here... */
   float t;
   t = *cr * dr[bit] - *ci * di[bit];
   *ci = *cr * di[bit] + *ci * dr[bit];
   *cr = t;
   
   t = 2.0 - (*cr * *cr + *ci * *ci);
   *cr *= t;
   *ci *= t;
   return *cr;
}  
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().

{
   /* Initialize stuff for inverse FFT */
   dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0);
   di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0);
   dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0);
   di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0);
}
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 98 of file tdd.c.

References ast_log(), fsk_data::bw, calloc, tdd_state::charnum, fsk_data::cont, fsk_data::f_mark_idx, fsk_data::f_space_idx, tdd_state::fskd, fskmodem_init(), fsk_data::hdlc, fsk_data::instop, fsk_data::ispb, LOG_WARNING, tdd_state::mode, fsk_data::nbit, fsk_data::nstop, fsk_data::parity, fsk_data::pcola, fsk_data::pllids, fsk_data::pllispb, fsk_data::pllispb2, tdd_state::pos, fsk_data::spb, fsk_data::state, fsk_data::x0, and fsk_data::xi0.

Referenced by dahdi_setoption().

{
   struct tdd_state *tdd;
   tdd = calloc(1, sizeof(*tdd));
   if (tdd) {
#ifdef INTEGER_CALLERID
      tdd->fskd.ispb = 176;        /* 45.5 baud */
      /* Set up for 45.5 / 8000 freq *32 to allow ints */
      tdd->fskd.pllispb  = (int)((8000 * 32 * 2) / 90);
      tdd->fskd.pllids   = tdd->fskd.pllispb / 32;
      tdd->fskd.pllispb2 = tdd->fskd.pllispb / 2;
      tdd->fskd.hdlc = 0;         /* Async */
      tdd->fskd.nbit = 5;         /* 5 bits */
      tdd->fskd.instop = 1;       /* integer rep of 1.5 stop bits */
      tdd->fskd.parity = 0;       /* No parity */
      tdd->fskd.bw=0;             /* Filter 75 Hz */
      tdd->fskd.f_mark_idx = 0;   /* 1400 Hz */
      tdd->fskd.f_space_idx = 1;  /* 1800 Hz */
      tdd->fskd.xi0  = 0;
      tdd->fskd.state = 0;
      tdd->pos = 0;
      tdd->mode = 0;
      fskmodem_init(&tdd->fskd);
#else
      tdd->fskd.spb = 176;        /* 45.5 baud */
      tdd->fskd.hdlc = 0;         /* Async */
      tdd->fskd.nbit = 5;         /* 5 bits */
      tdd->fskd.nstop = 1.5;      /* 1.5 stop bits */
      tdd->fskd.parity = 0;       /* No parity */
      tdd->fskd.bw=0;             /* Filter 75 Hz */
      tdd->fskd.f_mark_idx = 0;   /* 1400 Hz */
      tdd->fskd.f_space_idx = 1;  /* 1800 Hz */
      tdd->fskd.pcola = 0;        /* No clue */
      tdd->fskd.cont = 0;         /* Digital PLL reset */
      tdd->fskd.x0 = 0.0;
      tdd->fskd.state = 0;
      tdd->pos = 0;
      tdd->mode = 2;
#endif
      tdd->charnum = 0;
   } else
      ast_log(LOG_WARNING, "Out of memory\n");
   return tdd;
}

Variable Documentation

float di[4]
float dr[4] [static]

Definition at line 55 of file tdd.c.

Referenced by append_transaction(), and dundi_prop_precache().

float tddsb = 176.0 [static]

Definition at line 56 of file tdd.c.