| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include <time.h> |
| 37 |
#include <math.h> |
| 38 |
#include <ctype.h> |
| 39 |
|
| 40 |
#include "asterisk/logger.h" |
| 41 |
#include "asterisk/ulaw.h" |
| 42 |
#include "asterisk/tdd.h" |
| 43 |
#include "asterisk/fskmodem.h" |
| 44 |
#include "asterisk/utils.h" |
| 45 |
#include "ecdisa.h" |
| 46 |
|
| 47 |
struct tdd_state { |
| 48 |
fsk_data fskd; |
| 49 |
char rawdata[256]; |
| 50 |
short oldstuff[4096]; |
| 51 |
int oldlen; |
| 52 |
int pos; |
| 53 |
int modo; |
| 54 |
int mode; |
| 55 |
int charnum; |
| 56 |
}; |
| 57 |
|
| 58 |
static float dr[4], di[4]; |
| 59 |
static float tddsb = 176.0; |
| 60 |
|
| 61 |
#define TDD_SPACE 1800.0 |
| 62 |
#define TDD_MARK 1400.0 |
| 63 |
|
| 64 |
static int tdd_decode_baudot(struct tdd_state *tdd,unsigned char data) |
| 65 |
{ |
| 66 |
static char ltrs[32] = { '<','E','\n','A',' ','S','I','U', |
| 67 |
'\n','D','R','J','N','F','C','K', |
| 68 |
'T','Z','L','W','H','Y','P','Q', |
| 69 |
'O','B','G','^','M','X','V','^' }; |
| 70 |
static char figs[32] = { '<','3','\n','-',' ','\'','8','7', |
| 71 |
'\n','$','4','\'',',','!',':','(', |
| 72 |
'5','\"',')','2','=','6','0','1', |
| 73 |
'9','?','+','^','.','/',';','^' }; |
| 74 |
int d = 0; |
| 75 |
if (data < 32) { |
| 76 |
switch (data) { |
| 77 |
case 0x1f: |
| 78 |
tdd->modo = 0; |
| 79 |
break; |
| 80 |
case 0x1b: |
| 81 |
tdd->modo = 1; |
| 82 |
break; |
| 83 |
default: |
| 84 |
if (tdd->modo == 0) |
| 85 |
d = ltrs[data]; |
| 86 |
else |
| 87 |
d = figs[data]; |
| 88 |
break; |
| 89 |
} |
| 90 |
} |
| 91 |
return d; |
| 92 |
} |
| 93 |
|
| 94 |
void tdd_init(void) |
| 95 |
{ |
| 96 |
|
| 97 |
dr[0] = cos(TDD_SPACE * 2.0 * M_PI / 8000.0); |
| 98 |
di[0] = sin(TDD_SPACE * 2.0 * M_PI / 8000.0); |
| 99 |
dr[1] = cos(TDD_MARK * 2.0 * M_PI / 8000.0); |
| 100 |
di[1] = sin(TDD_MARK * 2.0 * M_PI / 8000.0); |
| 101 |
} |
| 102 |
|
| 103 |
struct tdd_state *tdd_new(void) |
| 104 |
{ |
| 105 |
struct tdd_state *tdd; |
| 106 |
tdd = ast_calloc(1, sizeof(*tdd)); |
| 107 |
if (tdd) { |
| 108 |
#ifdef INTEGER_CALLERID |
| 109 |
tdd->fskd.ispb = 176; |
| 110 |
|
| 111 |
tdd->fskd.pllispb = (int)((8000 * 32 * 2) / 90); |
| 112 |
tdd->fskd.pllids = tdd->fskd.pllispb / 32; |
| 113 |
tdd->fskd.pllispb2 = tdd->fskd.pllispb / 2; |
| 114 |
tdd->fskd.hdlc = 0; |
| 115 |
tdd->fskd.nbit = 5; |
| 116 |
tdd->fskd.instop = 1; |
| 117 |
tdd->fskd.parity = 0; |
| 118 |
tdd->fskd.bw=0; |
| 119 |
tdd->fskd.f_mark_idx = 0; |
| 120 |
tdd->fskd.f_space_idx = 1; |
| 121 |
tdd->fskd.xi0 = 0; |
| 122 |
tdd->fskd.state = 0; |
| 123 |
tdd->pos = 0; |
| 124 |
tdd->mode = 0; |
| 125 |
fskmodem_init(&tdd->fskd); |
| 126 |
#else |
| 127 |
tdd->fskd.spb = 176; |
| 128 |
tdd->fskd.hdlc = 0; |
| 129 |
tdd->fskd.nbit = 5; |
| 130 |
tdd->fskd.nstop = 1.5; |
| 131 |
tdd->fskd.parity = 0; |
| 132 |
tdd->fskd.bw=0; |
| 133 |
tdd->fskd.f_mark_idx = 0; |
| 134 |
tdd->fskd.f_space_idx = 1; |
| 135 |
tdd->fskd.pcola = 0; |
| 136 |
tdd->fskd.cont = 0; |
| 137 |
tdd->fskd.x0 = 0.0; |
| 138 |
tdd->fskd.state = 0; |
| 139 |
tdd->pos = 0; |
| 140 |
tdd->mode = 2; |
| 141 |
#endif |
| 142 |
tdd->charnum = 0; |
| 143 |
} else |
| 144 |
ast_log(LOG_WARNING, "Out of memory\n"); |
| 145 |
return tdd; |
| 146 |
} |
| 147 |
|
| 148 |
int ast_tdd_gen_ecdisa(unsigned char *outbuf, int len) |
| 149 |
{ |
| 150 |
int pos = 0; |
| 151 |
int cnt; |
| 152 |
while (len) { |
| 153 |
cnt = len > sizeof(ecdisa) ? sizeof(ecdisa) : len; |
| 154 |
memcpy(outbuf + pos, ecdisa, cnt); |
| 155 |
pos += cnt; |
| 156 |
len -= cnt; |
| 157 |
} |
| 158 |
return 0; |
| 159 |
} |
| 160 |
|
| 161 |
int tdd_feed(struct tdd_state *tdd, unsigned char *ubuf, int len) |
| 162 |
{ |
| 163 |
int mylen = len; |
| 164 |
int olen; |
| 165 |
int b = 'X'; |
| 166 |
int res; |
| 167 |
int c,x; |
| 168 |
short *buf = ast_calloc(1, 2 * len + tdd->oldlen); |
| 169 |
short *obuf = buf; |
| 170 |
if (!buf) { |
| 171 |
ast_log(LOG_WARNING, "Out of memory\n"); |
| 172 |
return -1; |
| 173 |
} |
| 174 |
memcpy(buf, tdd->oldstuff, tdd->oldlen); |
| 175 |
mylen += tdd->oldlen / 2; |
| 176 |
for (x = 0; x < len; x++) |
| 177 |
buf[x + tdd->oldlen / 2] = AST_MULAW(ubuf[x]); |
| 178 |
c = res = 0; |
| 179 |
while (mylen >= 1320) { |
| 180 |
olen = mylen; |
| 181 |
res = fsk_serial(&tdd->fskd, buf, &mylen, &b); |
| 182 |
if (mylen < 0) { |
| 183 |
ast_log(LOG_ERROR, "fsk_serial made mylen < 0 (%d) (olen was %d)\n", mylen, olen); |
| 184 |
ast_free(obuf); |
| 185 |
return -1; |
| 186 |
} |
| 187 |
buf += (olen - mylen); |
| 188 |
if (res < 0) { |
| 189 |
ast_log(LOG_NOTICE, "fsk_serial failed\n"); |
| 190 |
ast_free(obuf); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
if (res == 1) { |
| 194 |
|
| 195 |
if (b > 0x7f) |
| 196 |
continue; |
| 197 |
c = tdd_decode_baudot(tdd, b); |
| 198 |
if ((c < 1) || (c > 126)) |
| 199 |
continue; |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
if (mylen) { |
| 204 |
memcpy(tdd->oldstuff, buf, mylen * 2); |
| 205 |
tdd->oldlen = mylen * 2; |
| 206 |
} else |
| 207 |
tdd->oldlen = 0; |
| 208 |
ast_free(obuf); |
| 209 |
if (res) { |
| 210 |
tdd->mode = 2; |
| 211 |
|
| 212 |
|
| 213 |
return(c); |
| 214 |
} |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
void tdd_free(struct tdd_state *tdd) |
| 219 |
{ |
| 220 |
ast_free(tdd); |
| 221 |
} |
| 222 |
|
| 223 |
static inline float tdd_getcarrier(float *cr, float *ci, int bit) |
| 224 |
{ |
| 225 |
|
| 226 |
float t; |
| 227 |
t = *cr * dr[bit] - *ci * di[bit]; |
| 228 |
*ci = *cr * di[bit] + *ci * dr[bit]; |
| 229 |
*cr = t; |
| 230 |
|
| 231 |
t = 2.0 - (*cr * *cr + *ci * *ci); |
| 232 |
*cr *= t; |
| 233 |
*ci *= t; |
| 234 |
return *cr; |
| 235 |
} |
| 236 |
|
| 237 |
#define PUT_BYTE(a) do { \ |
| 238 |
*(buf++) = (a); \ |
| 239 |
bytes++; \ |
| 240 |
} while(0) |
| 241 |
|
| 242 |
#define PUT_AUDIO_SAMPLE(y) do { \ |
| 243 |
int __pas_idx = (short)(rint(8192.0 * (y))); \ |
| 244 |
*(buf++) = AST_LIN2MU(__pas_idx); \ |
| 245 |
bytes++; \ |
| 246 |
} while(0) |
| 247 |
|
| 248 |
#define PUT_TDD_MARKMS do { \ |
| 249 |
int x; \ |
| 250 |
for (x = 0; x < 8; x++) \ |
| 251 |
PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \ |
| 252 |
} while(0) |
| 253 |
|
| 254 |
#define PUT_TDD_BAUD(bit) do { \ |
| 255 |
while (scont < tddsb) { \ |
| 256 |
PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, bit)); \ |
| 257 |
scont += 1.0; \ |
| 258 |
} \ |
| 259 |
scont -= tddsb; \ |
| 260 |
} while(0) |
| 261 |
|
| 262 |
#define PUT_TDD_STOP do { \ |
| 263 |
while (scont < (tddsb * 1.5)) { \ |
| 264 |
PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); \ |
| 265 |
scont += 1.0; \ |
| 266 |
} \ |
| 267 |
scont -= (tddsb * 1.5); \ |
| 268 |
} while(0) |
| 269 |
|
| 270 |
|
| 271 |
#define PUT_TDD(byte) do { \ |
| 272 |
int z; \ |
| 273 |
unsigned char b = (byte); \ |
| 274 |
PUT_TDD_BAUD(0); \ |
| 275 |
for (z = 0; z < 5; z++) { \ |
| 276 |
PUT_TDD_BAUD(b & 1); \ |
| 277 |
b >>= 1; \ |
| 278 |
} \ |
| 279 |
PUT_TDD_STOP; \ |
| 280 |
} while(0); |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
int tdd_gen_holdtone(unsigned char *buf) |
| 286 |
{ |
| 287 |
int bytes = 0; |
| 288 |
float scont = 0.0, cr = 1.0, ci=0.0; |
| 289 |
while (scont < tddsb * 10.0) { |
| 290 |
PUT_AUDIO_SAMPLE(tdd_getcarrier(&cr, &ci, 1)); |
| 291 |
scont += 1.0; |
| 292 |
} |
| 293 |
return bytes; |
| 294 |
} |
| 295 |
|
| 296 |
int tdd_generate(struct tdd_state *tdd, unsigned char *buf, const char *str) |
| 297 |
{ |
| 298 |
int bytes = 0; |
| 299 |
int i,x; |
| 300 |
char c; |
| 301 |
|
| 302 |
static unsigned char lstr[31] = "\000E\nA SIU\rDRJNFCKTZLWHYPQOBG\000MXV"; |
| 303 |
|
| 304 |
static unsigned char fstr[31] = "\0003\n- \00787\r$4',!:(5\")2\0006019?+\000./;"; |
| 305 |
|
| 306 |
float cr = 1.0; |
| 307 |
float ci = 0.0; |
| 308 |
float scont = 0.0; |
| 309 |
|
| 310 |
for(x = 0; str[x]; x++) { |
| 311 |
|
| 312 |
if ( (tdd->charnum++) % 72 == 0) |
| 313 |
PUT_TDD(tdd->mode ? 27 : 31 ); |
| 314 |
|
| 315 |
c = toupper(str[x]); |
| 316 |
#if 0 |
| 317 |
printf("%c",c); fflush(stdout); |
| 318 |
#endif |
| 319 |
if (c == 0) { |
| 320 |
PUT_TDD(0); |
| 321 |
continue; |
| 322 |
} |
| 323 |
if (c == '\r') { |
| 324 |
PUT_TDD(8); |
| 325 |
continue; |
| 326 |
} |
| 327 |
if (c == '\n') { |
| 328 |
PUT_TDD(8); |
| 329 |
PUT_TDD(2); |
| 330 |
continue; |
| 331 |
} |
| 332 |
if (c == ' ') { |
| 333 |
PUT_TDD(4); |
| 334 |
continue; |
| 335 |
} |
| 336 |
for (i = 0; i < 31; i++) { |
| 337 |
if (lstr[i] == c) |
| 338 |
break; |
| 339 |
} |
| 340 |
if (i < 31) { |
| 341 |
if (tdd->mode) { |
| 342 |
PUT_TDD(31); |
| 343 |
tdd->mode = 0; |
| 344 |
} |
| 345 |
PUT_TDD(i); |
| 346 |
continue; |
| 347 |
} |
| 348 |
for (i = 0; i < 31; i++) { |
| 349 |
if (fstr[i] == c) |
| 350 |
break; |
| 351 |
} |
| 352 |
if (i < 31) { |
| 353 |
if (tdd->mode != 1) { |
| 354 |
PUT_TDD(27); |
| 355 |
tdd->mode = 1; |
| 356 |
} |
| 357 |
PUT_TDD(i); |
| 358 |
continue; |
| 359 |
} |
| 360 |
} |
| 361 |
return bytes; |
| 362 |
} |