tdd.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. */
  25. #include <time.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <math.h>
  31. #include <ctype.h>
  32. #include "asterisk.h"
  33. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  34. #include "asterisk/ulaw.h"
  35. #include "asterisk/tdd.h"
  36. #include "asterisk/logger.h"
  37. #include "asterisk/fskmodem.h"
  38. #include "ecdisa.h"
  39. struct tdd_state {
  40. fsk_data fskd;
  41. char rawdata[256];
  42. short oldstuff[4096];
  43. int oldlen;
  44. int pos;
  45. int modo;
  46. int mode;
  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','7','·','^','.','/','=','^'};
  62. int d;
  63. d=0; /* return 0 if not decodeable */
  64. switch (data) {
  65. case 0x1f : tdd->modo=0; break;
  66. case 0x1b : tdd->modo=1; break;
  67. default: if (tdd->modo==0) d=ltrs[data]; else d=figs[data]; break;
  68. }
  69. return d;
  70. }
  71. void tdd_init(void)
  72. {
  73. /* Initialize stuff for inverse FFT */
  74. dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0);
  75. di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0);
  76. dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0);
  77. di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0);
  78. }
  79. struct tdd_state *tdd_new(void)
  80. {
  81. struct tdd_state *tdd;
  82. tdd = malloc(sizeof(struct tdd_state));
  83. if (tdd) {
  84. memset(tdd, 0, sizeof(struct tdd_state));
  85. tdd->fskd.spb = 176; /* 45.5 baud */
  86. tdd->fskd.hdlc = 0; /* Async */
  87. tdd->fskd.nbit = 5; /* 5 bits */
  88. tdd->fskd.nstop = 1.5; /* 1.5 stop bits */
  89. tdd->fskd.paridad = 0; /* No parity */
  90. tdd->fskd.bw=0; /* Filter 75 Hz */
  91. tdd->fskd.f_mark_idx = 0; /* 1400 Hz */
  92. tdd->fskd.f_space_idx = 1; /* 1800 Hz */
  93. tdd->fskd.pcola = 0; /* No clue */
  94. tdd->fskd.cont = 0; /* Digital PLL reset */
  95. tdd->fskd.x0 = 0.0;
  96. tdd->fskd.state = 0;
  97. tdd->pos = 0;
  98. tdd->mode = 2;
  99. } else
  100. ast_log(LOG_WARNING, "Out of memory\n");
  101. return tdd;
  102. }
  103. int ast_tdd_gen_ecdisa(unsigned char *outbuf, int len)
  104. {
  105. int pos = 0;
  106. int cnt;
  107. while(len) {
  108. cnt = len;
  109. if (cnt > sizeof(ecdisa))
  110. cnt = sizeof(ecdisa);
  111. memcpy(outbuf + pos, ecdisa, cnt);
  112. pos += cnt;
  113. len -= cnt;
  114. }
  115. return 0;
  116. }
  117. int tdd_feed(struct tdd_state *tdd, unsigned char *ubuf, int len)
  118. {
  119. int mylen = len;
  120. int olen;
  121. int b = 'X';
  122. int res;
  123. int c,x;
  124. short *buf = malloc(2 * len + tdd->oldlen);
  125. short *obuf = buf;
  126. if (!buf) {
  127. ast_log(LOG_WARNING, "Out of memory\n");
  128. return -1;
  129. }
  130. memset(buf, 0, 2 * len + tdd->oldlen);
  131. memcpy(buf, tdd->oldstuff, tdd->oldlen);
  132. mylen += tdd->oldlen/2;
  133. for (x=0;x<len;x++)
  134. buf[x+tdd->oldlen/2] = AST_MULAW(ubuf[x]);
  135. c = res = 0;
  136. while(mylen >= 1320) { /* has to have enough to work on */
  137. olen = mylen;
  138. res = fsk_serie(&tdd->fskd, buf, &mylen, &b);
  139. if (mylen < 0) {
  140. ast_log(LOG_ERROR, "fsk_serie made mylen < 0 (%d) (olen was %d)\n", mylen,olen);
  141. free(obuf);
  142. return -1;
  143. }
  144. buf += (olen - mylen);
  145. if (res < 0) {
  146. ast_log(LOG_NOTICE, "fsk_serie failed\n");
  147. free(obuf);
  148. return -1;
  149. }
  150. if (res == 1) {
  151. /* Ignore invalid bytes */
  152. if (b > 0x7f)
  153. continue;
  154. c = tdd_decode_baudot(tdd,b);
  155. if ((c < 1) || (c > 126)) continue; /* if not valid */
  156. break;
  157. }
  158. }
  159. if (mylen) {
  160. memcpy(tdd->oldstuff, buf, mylen * 2);
  161. tdd->oldlen = mylen * 2;
  162. } else
  163. tdd->oldlen = 0;
  164. free(obuf);
  165. if (res) {
  166. tdd->mode = 2; /* put it in mode where it
  167. reliably puts teleprinter in correct shift mode */
  168. return(c);
  169. }
  170. return 0;
  171. }
  172. void tdd_free(struct tdd_state *tdd)
  173. {
  174. free(tdd);
  175. }
  176. static inline float tdd_getcarrier(float *cr, float *ci, int bit)
  177. {
  178. /* Move along. There's nothing to see here... */
  179. float t;
  180. t = *cr * dr[bit] - *ci * di[bit];
  181. *ci = *cr * di[bit] + *ci * dr[bit];
  182. *cr = t;
  183. t = 2.0 - (*cr * *cr + *ci * *ci);
  184. *cr *= t;
  185. *ci *= t;
  186. return *cr;
  187. }
  188. #define PUT_BYTE(a) do { \
  189. *(buf++) = (a); \
  190. bytes++; \
  191. } while(0)
  192. #define PUT_AUDIO_SAMPLE(y) do { \
  193. int index = (short)(rint(8192.0 * (y))); \
  194. *(buf++) = AST_LIN2MU(index); \
  195. bytes++; \
  196. } while(0)
  197. #define PUT_TDD_MARKMS do { \
  198. int x; \
  199. for (x=0;x<8;x++) \
  200. PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
  201. } while(0)
  202. #define PUT_TDD_BAUD(bit) do { \
  203. while(scont < tddsb) { \
  204. PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, bit)); \
  205. scont += 1.0; \
  206. } \
  207. scont -= tddsb; \
  208. } while(0)
  209. #define PUT_TDD_STOP do { \
  210. while(scont < (tddsb * 1.5)) { \
  211. PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \
  212. scont += 1.0; \
  213. } \
  214. scont -= (tddsb * 1.5); \
  215. } while(0)
  216. #define PUT_TDD(byte) do { \
  217. int z; \
  218. unsigned char b = (byte); \
  219. PUT_TDD_BAUD(0); /* Start bit */ \
  220. for (z=0;z<5;z++) { \
  221. PUT_TDD_BAUD(b & 1); \
  222. b >>= 1; \
  223. } \
  224. PUT_TDD_STOP; /* Stop bit */ \
  225. } while(0);
  226. int tdd_generate(struct tdd_state *tdd, unsigned char *buf, const char *str)
  227. {
  228. int bytes=0;
  229. int i,x;
  230. char c;
  231. static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV";
  232. static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?&\000./;";
  233. /* Initial carriers (real/imaginary) */
  234. float cr = 1.0;
  235. float ci = 0.0;
  236. float scont = 0.0;
  237. for(x = 0; str[x]; x++) {
  238. c = toupper(str[x]);
  239. #if 0
  240. printf("%c",c); fflush(stdout);
  241. #endif
  242. if (c == 0) /* send null */
  243. {
  244. PUT_TDD(0);
  245. continue;
  246. }
  247. if (c == '\r') /* send c/r */
  248. {
  249. PUT_TDD(8);
  250. continue;
  251. }
  252. if (c == '\n') /* send c/r and l/f */
  253. {
  254. PUT_TDD(8);
  255. PUT_TDD(2);
  256. continue;
  257. }
  258. if (c == ' ') /* send space */
  259. {
  260. PUT_TDD(4);
  261. continue;
  262. }
  263. for(i = 0; i < 31; i++)
  264. {
  265. if (lstr[i] == c) break;
  266. }
  267. if (i < 31) /* if we found it */
  268. {
  269. if (tdd->mode) /* if in figs mode, change it */
  270. {
  271. PUT_TDD(31); /* Send LTRS */
  272. tdd->mode = 0;
  273. }
  274. PUT_TDD(i);
  275. continue;
  276. }
  277. for(i = 0; i < 31; i++)
  278. {
  279. if (fstr[i] == c) break;
  280. }
  281. if (i < 31) /* if we found it */
  282. {
  283. if (tdd->mode != 1) /* if in ltrs mode, change it */
  284. {
  285. PUT_TDD(27); /* send FIGS */
  286. tdd->mode = 1;
  287. }
  288. PUT_TDD(i); /* send byte */
  289. continue;
  290. }
  291. }
  292. return bytes;
  293. }