tdd.c 8.4 KB

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