tdd.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * Includes code and algorithms from the Zapata library.
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*! \file
  21. *
  22. * \brief TTY/TDD Generation support
  23. *
  24. * \author Mark Spencer <markster@digium.com>
  25. *
  26. * \note Includes code and algorithms from the Zapata library.
  27. */
  28. /*** MODULEINFO
  29. <support_level>core</support_level>
  30. ***/
  31. #include "asterisk.h"
  32. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  33. #include <time.h>
  34. #include <math.h>
  35. #include <ctype.h>
  36. #include "asterisk/logger.h"
  37. #include "asterisk/ulaw.h"
  38. #include "asterisk/tdd.h"
  39. #include "asterisk/fskmodem.h"
  40. #include "ecdisa.h"
  41. struct tdd_state {
  42. fsk_data fskd;
  43. char rawdata[256];
  44. short oldstuff[4096];
  45. int oldlen;
  46. int pos;
  47. int modo;
  48. int mode;
  49. int charnum;
  50. };
  51. static float dr[4], di[4];
  52. static float tddsb = 176.0; /* 45.5 baud */
  53. #define TDD_SPACE 1800.0 /* 1800 hz for "0" */
  54. #define TDD_MARK 1400.0 /* 1400 hz for "1" */
  55. static int tdd_decode_baudot(struct tdd_state *tdd,unsigned char data) /* covert baudot into ASCII */
  56. {
  57. static char ltrs[32] = { '<','E','\n','A',' ','S','I','U',
  58. '\n','D','R','J','N','F','C','K',
  59. 'T','Z','L','W','H','Y','P','Q',
  60. 'O','B','G','^','M','X','V','^' };
  61. static char figs[32] = { '<','3','\n','-',' ','\'','8','7',
  62. '\n','$','4','\'',',','!',':','(',
  63. '5','\"',')','2','=','6','0','1',
  64. '9','?','+','^','.','/',';','^' };
  65. int d = 0; /* return 0 if not decodeable */
  66. if (data < 32) {
  67. switch (data) {
  68. case 0x1f:
  69. tdd->modo = 0;
  70. break;
  71. case 0x1b:
  72. tdd->modo = 1;
  73. break;
  74. default:
  75. if (tdd->modo == 0)
  76. d = ltrs[data];
  77. else
  78. d = figs[data];
  79. break;
  80. }
  81. }
  82. return d;
  83. }
  84. void tdd_init(void)
  85. {
  86. /* Initialize stuff for inverse FFT */
  87. dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0);
  88. di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0);
  89. dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0);
  90. di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0);
  91. }
  92. struct tdd_state *tdd_new(void)
  93. {
  94. struct tdd_state *tdd;
  95. tdd = calloc(1, sizeof(*tdd));
  96. if (tdd) {
  97. #ifdef INTEGER_CALLERID
  98. tdd->fskd.ispb = 176; /* 45.5 baud */
  99. /* Set up for 45.5 / 8000 freq *32 to allow ints */
  100. tdd->fskd.pllispb = (int)((8000 * 32 * 2) / 90);
  101. tdd->fskd.pllids = tdd->fskd.pllispb / 32;
  102. tdd->fskd.pllispb2 = tdd->fskd.pllispb / 2;
  103. tdd->fskd.hdlc = 0; /* Async */
  104. tdd->fskd.nbit = 5; /* 5 bits */
  105. tdd->fskd.instop = 1; /* integer rep of 1.5 stop bits */
  106. tdd->fskd.parity = 0; /* No parity */
  107. tdd->fskd.bw=0; /* Filter 75 Hz */
  108. tdd->fskd.f_mark_idx = 0; /* 1400 Hz */
  109. tdd->fskd.f_space_idx = 1; /* 1800 Hz */
  110. tdd->fskd.xi0 = 0;
  111. tdd->fskd.state = 0;
  112. tdd->pos = 0;
  113. tdd->mode = 0;
  114. fskmodem_init(&tdd->fskd);
  115. #else
  116. tdd->fskd.spb = 176; /* 45.5 baud */
  117. tdd->fskd.hdlc = 0; /* Async */
  118. tdd->fskd.nbit = 5; /* 5 bits */
  119. tdd->fskd.nstop = 1.5; /* 1.5 stop bits */
  120. tdd->fskd.parity = 0; /* No parity */
  121. tdd->fskd.bw=0; /* Filter 75 Hz */
  122. tdd->fskd.f_mark_idx = 0; /* 1400 Hz */
  123. tdd->fskd.f_space_idx = 1; /* 1800 Hz */
  124. tdd->fskd.pcola = 0; /* No clue */
  125. tdd->fskd.cont = 0; /* Digital PLL reset */
  126. tdd->fskd.x0 = 0.0;
  127. tdd->fskd.state = 0;
  128. tdd->pos = 0;
  129. tdd->mode = 2;
  130. #endif
  131. tdd->charnum = 0;
  132. } else
  133. ast_log(LOG_WARNING, "Out of memory\n");
  134. return tdd;
  135. }
  136. int ast_tdd_gen_ecdisa(unsigned char *outbuf, int len)
  137. {
  138. int pos = 0;
  139. int cnt;
  140. while (len) {
  141. cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len;
  142. memcpy(outbuf + pos, ecdisa, cnt);
  143. pos += cnt;
  144. len -= cnt;
  145. }
  146. return 0;
  147. }
  148. int tdd_feed(struct tdd_state *tdd, unsigned char *ubuf, int len)
  149. {
  150. int mylen = len;
  151. int olen;
  152. int b = 'X';
  153. int res;
  154. int c,x;
  155. short *buf = calloc(1, 2 * len + tdd->oldlen);
  156. short *obuf = buf;
  157. if (!buf) {
  158. ast_log(LOG_WARNING, "Out of memory\n");
  159. return -1;
  160. }
  161. memcpy(buf, tdd->oldstuff, tdd->oldlen);
  162. mylen += tdd->oldlen / 2;
  163. for (x = 0; x < len; x++)
  164. buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]);
  165. c = res = 0;
  166. while (mylen >= 1320) { /* has to have enough to work on */
  167. olen = mylen;
  168. res = fsk_serial(&tdd->fskd, buf, &mylen, &b);
  169. if (mylen < 0) {
  170. ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen);
  171. free(obuf);
  172. return -1;
  173. }
  174. buf += (olen - mylen);
  175. if (res < 0) {
  176. ast_log(LOG_NOTICE, "fsk_serial failed\n");
  177. free(obuf);
  178. return -1;
  179. }
  180. if (res == 1) {
  181. /* Ignore invalid bytes */
  182. if (b > 0x7f)
  183. continue;
  184. c = tdd_decode_baudot(tdd, b);
  185. if ((c < 1) || (c > 126))
  186. continue; /* if not valid */
  187. break;
  188. }
  189. }
  190. if (mylen) {
  191. memcpy(tdd->oldstuff, buf, mylen * 2);
  192. tdd->oldlen = mylen * 2;
  193. } else
  194. tdd->oldlen = 0;
  195. free(obuf);
  196. if (res) {
  197. tdd->mode = 2;
  198. /* put it in mode where it
  199. reliably puts teleprinter in correct shift mode */
  200. return(c);
  201. }
  202. return 0;
  203. }
  204. void tdd_free(struct tdd_state *tdd)
  205. {
  206. free(tdd);
  207. }
  208. static inline float tdd_getcarrier(float *cr, float *ci, int bit)
  209. {
  210. /* Move along. There's nothing to see here... */
  211. float t;
  212. t = *cr * dr[bit] - *ci * di[bit];
  213. *ci = *cr * di[bit] + *ci * dr[bit];
  214. *cr = t;
  215. t = 2.0 - (*cr * *cr + *ci * *ci);
  216. *cr *= t;
  217. *ci *= t;
  218. return *cr;
  219. }
  220. #define PUT_BYTE(a) do { \
  221. *(buf++) = (a); \
  222. bytes++; \
  223. } while(0)
  224. #define PUT_AUDIO_SAMPLE(y) do { \
  225. int __pas_idx = (short)(rint(8192.0 * (y))); \
  226. *(buf++) = AST_LIN2MU(__pas_idx); \
  227. bytes++; \
  228. } while(0)
  229. #define PUT_TDD_MARKMS do { \
  230. int x; \
  231. for (x = 0; x < 8; x++) \
  232. PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
  233. } while(0)
  234. #define PUT_TDD_BAUD(bit) do { \
  235. while (scont < tddsb) { \
  236. PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, bit)); \
  237. scont += 1.0; \
  238. } \
  239. scont -= tddsb; \
  240. } while(0)
  241. #define PUT_TDD_STOP do { \
  242. while (scont < (tddsb * 1.5)) { \
  243. PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
  244. scont += 1.0; \
  245. } \
  246. scont -= (tddsb * 1.5); \
  247. } while(0)
  248. #define PUT_TDD(byte) do { \
  249. int z; \
  250. unsigned char b = (byte); \
  251. PUT_TDD_BAUD(0); /* Start bit */ \
  252. for (z = 0; z < 5; z++) { \
  253. PUT_TDD_BAUD(b & 1); \
  254. b >>= 1; \
  255. } \
  256. PUT_TDD_STOP; /* Stop bit */ \
  257. } while(0);
  258. /*! Generate TDD hold tone
  259. * \param outbuf, buf Result buffer
  260. * \todo How big should this be?
  261. */
  262. int tdd_gen_holdtone(unsigned char *buf)
  263. {
  264. int bytes = 0;
  265. float scont = 0.0, cr = 1.0, ci=0.0;
  266. while (scont < tddsb * 10.0) {
  267. PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1));
  268. scont += 1.0;
  269. }
  270. return bytes;
  271. }
  272. int tdd_generate(struct tdd_state *tdd, unsigned char *buf, const char *str)
  273. {
  274. int bytes = 0;
  275. int i,x;
  276. char c;
  277. /*! Baudot letters */
  278. static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
  279. /*! Baudot figures */
  280. static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?+\000./;";
  281. /* Initial carriers (real/imaginary) */
  282. float cr = 1.0;
  283. float ci = 0.0;
  284. float scont = 0.0;
  285. for(x = 0; str[x]; x++) {
  286. /* Do synch for each 72th character */
  287. if ( (tdd->charnum++) % 72 == 0)
  288. PUT_TDD(tdd->mode ? 27 /* FIGS */ : 31 /* LTRS */);
  289. c = toupper(str[x]);
  290. #if 0
  291. printf("%c",c); fflush(stdout);
  292. #endif
  293. if (c == 0) { /* send null */
  294. PUT_TDD(0);
  295. continue;
  296. }
  297. if (c == '\r') { /* send c/r */
  298. PUT_TDD(8);
  299. continue;
  300. }
  301. if (c == '\n') { /* send c/r and l/f */
  302. PUT_TDD(8);
  303. PUT_TDD(2);
  304. continue;
  305. }
  306. if (c == ' ') { /* send space */
  307. PUT_TDD(4);
  308. continue;
  309. }
  310. for (i = 0; i < 31; i++) {
  311. if (lstr[i] == c)
  312. break;
  313. }
  314. if (i < 31) { /* if we found it */
  315. if (tdd->mode) { /* if in figs mode, change it */
  316. PUT_TDD(31); /* Send LTRS */
  317. tdd->mode = 0;
  318. }
  319. PUT_TDD(i);
  320. continue;
  321. }
  322. for (i = 0; i < 31; i++) {
  323. if (fstr[i] == c)
  324. break;
  325. }
  326. if (i < 31) { /* if we found it */
  327. if (tdd->mode != 1) { /* if in ltrs mode, change it */
  328. PUT_TDD(27); /* send FIGS */
  329. tdd->mode = 1;
  330. }
  331. PUT_TDD(i); /* send byte */
  332. continue;
  333. }
  334. }
  335. return bytes;
  336. }