tdd.c 8.2 KB

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