tdd.c 6.5 KB

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