| 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 |
|
| 35 |
. |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#include "asterisk.h" |
| 43 |
|
| 44 |
#include <dirent.h> |
| 45 |
#include <ctype.h> |
| 46 |
#include <sys/stat.h> |
| 47 |
|
| 48 |
#include "asterisk/paths.h" |
| 49 |
#include "asterisk/lock.h" |
| 50 |
#include "asterisk/file.h" |
| 51 |
#include "asterisk/channel.h" |
| 52 |
#include "asterisk/pbx.h" |
| 53 |
#include "asterisk/module.h" |
| 54 |
#include "asterisk/alaw.h" |
| 55 |
#include "asterisk/callerid.h" |
| 56 |
#include "asterisk/utils.h" |
| 57 |
#include "asterisk/app.h" |
| 58 |
#include "asterisk/format_cache.h" |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
> |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
e |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
static volatile unsigned char message_ref; |
| 126 |
static volatile unsigned int seq; |
| 127 |
|
| 128 |
static char log_file[255]; |
| 129 |
|
| 130 |
static char *app = "SMS"; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static const signed short wave[] = { |
| 139 |
0, 392, 782, 1167, 1545, 1913, 2270, 2612, 2939, 3247, 3536, 3802, 4045, 4263, 4455, 4619, 4755, 4862, 4938, 4985, |
| 140 |
5000, 4985, 4938, 4862, 4755, 4619, 4455, 4263, 4045, 3802, 3536, 3247, 2939, 2612, 2270, 1913, 1545, 1167, 782, 392, |
| 141 |
0, -392, -782, -1167, |
| 142 |
-1545, -1913, -2270, -2612, -2939, -3247, -3536, -3802, -4045, -4263, -4455, -4619, -4755, -4862, -4938, -4985, -5000, |
| 143 |
-4985, -4938, -4862, |
| 144 |
-4755, -4619, -4455, -4263, -4045, -3802, -3536, -3247, -2939, -2612, -2270, -1913, -1545, -1167, -782, -392 |
| 145 |
}; |
| 146 |
|
| 147 |
#ifdef OUTALAW |
| 148 |
static unsigned char wavea[80]; |
| 149 |
typedef unsigned char output_t; |
| 150 |
static const output_t *wave_out = wavea; |
| 151 |
#define __OUT_FMT ast_format_alaw |
| 152 |
#else |
| 153 |
typedef signed short output_t; |
| 154 |
static const output_t *wave_out = wave; |
| 155 |
#define __OUT_FMT ast_format_slin |
| 156 |
#endif |
| 157 |
|
| 158 |
#define OSYNC_BITS 80 |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
enum message_types { |
| 168 |
DLL_SMS_MASK = 0x7f, |
| 169 |
|
| 170 |
|
| 171 |
DLL1_SMS_DATA = 0x11, |
| 172 |
DLL1_SMS_ERROR = 0x12, |
| 173 |
DLL1_SMS_EST = 0x13, |
| 174 |
DLL1_SMS_REL = 0x14, |
| 175 |
DLL1_SMS_ACK = 0x15, |
| 176 |
DLL1_SMS_NACK = 0x16, |
| 177 |
|
| 178 |
DLL1_SMS_COMPLETE = 0x80, |
| 179 |
DLL1_SMS_MORE = 0x00, |
| 180 |
|
| 181 |
|
| 182 |
DLL2_SMS_EST = 0x7f, |
| 183 |
DLL2_SMS_INFO_MO = 0x10, |
| 184 |
DLL2_SMS_INFO_MT = 0x11, |
| 185 |
DLL2_SMS_INFO_STA = 0x12, |
| 186 |
DLL2_SMS_NACK = 0x13, |
| 187 |
DLL2_SMS_ACK0 = 0x14, |
| 188 |
DLL2_SMS_ACK1 = 0x15, |
| 189 |
DLL2_SMS_ENQ = 0x16, |
| 190 |
DLL2_SMS_REL = 0x17, |
| 191 |
|
| 192 |
DLL2_SMS_COMPLETE = 0x00, |
| 193 |
DLL2_SMS_MORE = 0x80, |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
static const unsigned short defaultalphabet[] = { |
| 198 |
0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC, |
| 199 |
0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5, |
| 200 |
0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8, |
| 201 |
0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9, |
| 202 |
' ', '!', '"', '#', 164, '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/', |
| 203 |
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', |
| 204 |
161, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
| 205 |
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 196, 214, 209, 220, 167, |
| 206 |
191, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
| 207 |
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 228, 246, 241, 252, 224, |
| 208 |
}; |
| 209 |
|
| 210 |
static const unsigned short escapes[] = { |
| 211 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x000C, 0, 0, 0, 0, 0, |
| 212 |
0, 0, 0, 0, 0x005E, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 213 |
0, 0, 0, 0, 0, 0, 0, 0, 0x007B, 0x007D, 0, 0, 0, 0, 0, 0x005C, |
| 214 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x005B, 0x007E, 0x005D, 0, |
| 215 |
0x007C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 216 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 217 |
0, 0, 0, 0, 0, 0x20AC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 218 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 219 |
}; |
| 220 |
|
| 221 |
#define SMSLEN 160 |
| 222 |
#define SMSLEN_8 140 |
| 223 |
|
| 224 |
typedef struct sms_s { |
| 225 |
unsigned char hangup; |
| 226 |
unsigned char err; |
| 227 |
unsigned char sent_rel:1; |
| 228 |
unsigned char smsc:1; |
| 229 |
unsigned char rx:1; |
| 230 |
unsigned char nolog:1; |
| 231 |
char queue[30]; |
| 232 |
char oa[20]; |
| 233 |
char da[20]; |
| 234 |
struct timeval scts; |
| 235 |
unsigned char pid; |
| 236 |
unsigned char dcs; |
| 237 |
short mr; |
| 238 |
int udl; |
| 239 |
int udhl; |
| 240 |
unsigned char srr:1; |
| 241 |
unsigned char udhi:1; |
| 242 |
unsigned char rp:1; |
| 243 |
unsigned int vp; |
| 244 |
unsigned short ud[SMSLEN]; |
| 245 |
unsigned char udh[SMSLEN]; |
| 246 |
char cli[20]; |
| 247 |
unsigned char ophase; |
| 248 |
unsigned char ophasep; |
| 249 |
unsigned char obyte; |
| 250 |
unsigned int opause; |
| 251 |
unsigned char obitp; |
| 252 |
unsigned char osync; |
| 253 |
unsigned char obytep; |
| 254 |
unsigned char obyten; |
| 255 |
unsigned char omsg[256]; |
| 256 |
unsigned char imsg[250]; |
| 257 |
signed long long ims0, |
| 258 |
imc0, |
| 259 |
ims1, |
| 260 |
imc1; |
| 261 |
unsigned int idle; |
| 262 |
unsigned short imag; |
| 263 |
unsigned char ips0; |
| 264 |
unsigned char ips1; |
| 265 |
unsigned char ipc0; |
| 266 |
unsigned char ipc1; |
| 267 |
unsigned char ibitl; |
| 268 |
unsigned char ibitc; |
| 269 |
unsigned char iphasep; |
| 270 |
unsigned char ibitn; |
| 271 |
unsigned char ibytev; |
| 272 |
unsigned char ibytep; |
| 273 |
unsigned char ibytec; |
| 274 |
unsigned char ierr; |
| 275 |
unsigned char ibith; |
| 276 |
unsigned char ibitt; |
| 277 |
|
| 278 |
|
| 279 |
int opause_0; |
| 280 |
int protocol; |
| 281 |
int oseizure; |
| 282 |
int framenumber; |
| 283 |
char udtxt[SMSLEN]; |
| 284 |
} sms_t; |
| 285 |
|
| 286 |
|
| 287 |
#define is7bit(dcs) ( ((dcs) & 0xC0) ? (!((dcs) & 4) ) : (((dcs) & 0xc) == 0) ) |
| 288 |
#define is8bit(dcs) ( ((dcs) & 0xC0) ? ( ((dcs) & 4) ) : (((dcs) & 0xc) == 4) ) |
| 289 |
#define is16bit(dcs) ( ((dcs) & 0xC0) ? 0 : (((dcs) & 0xc) == 8) ) |
| 290 |
|
| 291 |
static void sms_messagetx(sms_t *h); |
| 292 |
|
| 293 |
|
| 294 |
static void numcpy(char *d, char *s) |
| 295 |
{ |
| 296 |
if (*s == '+') { |
| 297 |
*d++ = *s++; |
| 298 |
} |
| 299 |
while (*s) { |
| 300 |
if (isdigit(*s)) { |
| 301 |
*d++ = *s; |
| 302 |
} |
| 303 |
s++; |
| 304 |
} |
| 305 |
*d = 0; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
static char *isodate(time_t t, char *buf, int len) |
| 310 |
{ |
| 311 |
struct ast_tm tm; |
| 312 |
struct timeval local = { t, 0 }; |
| 313 |
ast_localtime(&local, &tm, NULL); |
| 314 |
ast_strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm); |
| 315 |
return buf; |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
static long utf8decode(unsigned char **pp) |
| 322 |
{ |
| 323 |
unsigned char *p = *pp; |
| 324 |
if (!*p) { |
| 325 |
return 0; |
| 326 |
} |
| 327 |
(*pp)++; |
| 328 |
if (*p < 0xC0) { |
| 329 |
return *p; |
| 330 |
} |
| 331 |
if (*p < 0xE0) { |
| 332 |
if (*p < 0xC2 || (p[1] & 0xC0) != 0x80) { |
| 333 |
return *p; |
| 334 |
} |
| 335 |
(*pp)++; |
| 336 |
return ((*p & 0x1F) << 6) + (p[1] & 0x3F); |
| 337 |
} |
| 338 |
if (*p < 0xF0) { |
| 339 |
if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80) { |
| 340 |
return *p; |
| 341 |
} |
| 342 |
(*pp) += 2; |
| 343 |
return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F); |
| 344 |
} |
| 345 |
if (*p < 0xF8) { |
| 346 |
if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80) { |
| 347 |
return *p; |
| 348 |
} |
| 349 |
(*pp) += 3; |
| 350 |
return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F); |
| 351 |
} |
| 352 |
if (*p < 0xFC) { |
| 353 |
if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80 |
| 354 |
|| (p[4] & 0xC0) != 0x80) { |
| 355 |
return *p; |
| 356 |
} |
| 357 |
(*pp) += 4; |
| 358 |
return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F); |
| 359 |
} |
| 360 |
if (*p < 0xFE) { |
| 361 |
if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80 |
| 362 |
|| (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80) { |
| 363 |
return *p; |
| 364 |
} |
| 365 |
(*pp) += 5; |
| 366 |
return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) + (p[5] & 0x3F); |
| 367 |
} |
| 368 |
return *p; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
static int packsms7(unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud) |
| 376 |
{ |
| 377 |
unsigned char p = 0; |
| 378 |
unsigned char b = 0; |
| 379 |
unsigned char n = 0; |
| 380 |
unsigned char dummy[SMSLEN]; |
| 381 |
|
| 382 |
if (o == NULL) { |
| 383 |
o = dummy; |
| 384 |
} |
| 385 |
|
| 386 |
if (udhl) { |
| 387 |
o[p++] = udhl; |
| 388 |
b = 1; |
| 389 |
n = 1; |
| 390 |
while (udhl--) { |
| 391 |
o[p++] = *udh++; |
| 392 |
b += 8; |
| 393 |
while (b >= 7) { |
| 394 |
b -= 7; |
| 395 |
n++; |
| 396 |
} |
| 397 |
if (n >= SMSLEN) |
| 398 |
return n; |
| 399 |
} |
| 400 |
if (b) { |
| 401 |
b = 7 - b; |
| 402 |
if (++n >= SMSLEN) |
| 403 |
return n; |
| 404 |
} |
| 405 |
} |
| 406 |
o[p] = 0; |
| 407 |
|
| 408 |
while (udl--) { |
| 409 |
long u; |
| 410 |
unsigned char v; |
| 411 |
u = *ud++; |
| 412 |
|
| 413 |
|
| 414 |
for (v = 0; v < 128 && defaultalphabet[v] != u; v++); |
| 415 |
if (v == 128 && u && n + 1 < SMSLEN) { |
| 416 |
|
| 417 |
for (v = 0; v < 128 && escapes[v] != u; v++); |
| 418 |
if (v < 128) { |
| 419 |
|
| 420 |
o[p] |= (27 << b); |
| 421 |
b += 7; |
| 422 |
if (b >= 8) { |
| 423 |
b -= 8; |
| 424 |
p++; |
| 425 |
o[p] = (27 >> (7 - b)); |
| 426 |
} |
| 427 |
n++; |
| 428 |
} |
| 429 |
} |
| 430 |
if (v == 128) |
| 431 |
return -1; |
| 432 |
|
| 433 |
o[p] |= (v << b); |
| 434 |
b += 7; |
| 435 |
if (b >= 8) { |
| 436 |
b -= 8; |
| 437 |
p++; |
| 438 |
o[p] = (v >> (7 - b)); |
| 439 |
} |
| 440 |
if (++n >= SMSLEN) |
| 441 |
return n; |
| 442 |
} |
| 443 |
return n; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
static int packsms8(unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud) |
| 453 |
{ |
| 454 |
unsigned char p = 0; |
| 455 |
unsigned char dummy[SMSLEN_8]; |
| 456 |
|
| 457 |
if (o == NULL) |
| 458 |
o = dummy; |
| 459 |
|
| 460 |
if (udhl) { |
| 461 |
o[p++] = udhl; |
| 462 |
while (udhl--) { |
| 463 |
o[p++] = *udh++; |
| 464 |
if (p >= SMSLEN_8) { |
| 465 |
return p; |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
while (udl--) { |
| 470 |
long u; |
| 471 |
u = *ud++; |
| 472 |
if (u < 0 || u > 0xFF) { |
| 473 |
return -1; |
| 474 |
} |
| 475 |
o[p++] = u; |
| 476 |
if (p >= SMSLEN_8) { |
| 477 |
return p; |
| 478 |
} |
| 479 |
} |
| 480 |
return p; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
static int packsms16(unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud) |
| 492 |
{ |
| 493 |
unsigned char p = 0; |
| 494 |
unsigned char dummy[SMSLEN_8]; |
| 495 |
|
| 496 |
if (o == NULL) { |
| 497 |
o = dummy; |
| 498 |
} |
| 499 |
|
| 500 |
if (udhl) { |
| 501 |
o[p++] = udhl; |
| 502 |
while (udhl--) { |
| 503 |
o[p++] = *udh++; |
| 504 |
if (p >= SMSLEN_8) { |
| 505 |
return p; |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
while (udl--) { |
| 510 |
long u; |
| 511 |
u = *ud++; |
| 512 |
o[p++] = (u >> 8); |
| 513 |
if (p >= SMSLEN_8) { |
| 514 |
return p - 1; |
| 515 |
} |
| 516 |
o[p++] = u; |
| 517 |
if (p >= SMSLEN_8) { |
| 518 |
return p; |
| 519 |
} |
| 520 |
} |
| 521 |
return p; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
static int packsms(unsigned char dcs, unsigned char *base, unsigned int udhl, unsigned char *udh, int udl, unsigned short *ud) |
| 527 |
{ |
| 528 |
unsigned char *p = base; |
| 529 |
if (udl == 0) { |
| 530 |
*p++ = 0; |
| 531 |
} else { |
| 532 |
|
| 533 |
int l = 0; |
| 534 |
if (is7bit(dcs)) { |
| 535 |
if ((l = packsms7(p + 1, udhl, udh, udl, ud)) < 0) { |
| 536 |
l = 0; |
| 537 |
} |
| 538 |
*p++ = l; |
| 539 |
p += (l * 7 + 7) / 8; |
| 540 |
} else if (is8bit(dcs)) { |
| 541 |
if ((l = packsms8(p + 1, udhl, udh, udl, ud)) < 0) { |
| 542 |
l = 0; |
| 543 |
} |
| 544 |
*p++ = l; |
| 545 |
p += l; |
| 546 |
} else { |
| 547 |
if ((l = packsms16(p + 1, udhl, udh, udl, ud)) < 0) { |
| 548 |
l = 0; |
| 549 |
} |
| 550 |
*p++ = l; |
| 551 |
p += l; |
| 552 |
} |
| 553 |
} |
| 554 |
return p - base; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
static void packdate(unsigned char *o, time_t w) |
| 560 |
{ |
| 561 |
struct ast_tm t; |
| 562 |
struct timeval topack = { w, 0 }; |
| 563 |
int z; |
| 564 |
|
| 565 |
ast_localtime(&topack, &t, NULL); |
| 566 |
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__) |
| 567 |
z = -t.tm_gmtoff / 60 / 15; |
| 568 |
#else |
| 569 |
z = timezone / 60 / 15; |
| 570 |
#endif |
| 571 |
*o++ = ((t.tm_year % 10) << 4) + (t.tm_year % 100) / 10; |
| 572 |
*o++ = (((t.tm_mon + 1) % 10) << 4) + (t.tm_mon + 1) / 10; |
| 573 |
*o++ = ((t.tm_mday % 10) << 4) + t.tm_mday / 10; |
| 574 |
*o++ = ((t.tm_hour % 10) << 4) + t.tm_hour / 10; |
| 575 |
*o++ = ((t.tm_min % 10) << 4) + t.tm_min / 10; |
| 576 |
*o++ = ((t.tm_sec % 10) << 4) + t.tm_sec / 10; |
| 577 |
if (z < 0) { |
| 578 |
*o++ = (((-z) % 10) << 4) + (-z) / 10 + 0x08; |
| 579 |
} else { |
| 580 |
*o++ = ((z % 10) << 4) + z / 10; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
static struct timeval unpackdate(unsigned char *i) |
| 586 |
{ |
| 587 |
struct ast_tm t; |
| 588 |
|
| 589 |
t.tm_year = 100 + (i[0] & 0xF) * 10 + (i[0] >> 4); |
| 590 |
t.tm_mon = (i[1] & 0xF) * 10 + (i[1] >> 4) - 1; |
| 591 |
t.tm_mday = (i[2] & 0xF) * 10 + (i[2] >> 4); |
| 592 |
t.tm_hour = (i[3] & 0xF) * 10 + (i[3] >> 4); |
| 593 |
t.tm_min = (i[4] & 0xF) * 10 + (i[4] >> 4); |
| 594 |
t.tm_sec = (i[5] & 0xF) * 10 + (i[5] >> 4); |
| 595 |
t.tm_isdst = 0; |
| 596 |
if (i[6] & 0x08) { |
| 597 |
t.tm_min += 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4)); |
| 598 |
} else { |
| 599 |
t.tm_min -= 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4)); |
| 600 |
} |
| 601 |
|
| 602 |
return ast_mktime(&t, NULL); |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
static void unpacksms7(unsigned char *i, unsigned char l, unsigned char *udh, size_t udh_size, |
| 609 |
int *udhl, unsigned short *ud, size_t ud_size, int *udl, char udhi) |
| 610 |
{ |
| 611 |
unsigned char b = 0, p = 0; |
| 612 |
unsigned short *o = ud; |
| 613 |
unsigned short *o_end = ud + ud_size; |
| 614 |
unsigned char *h = udh; |
| 615 |
unsigned char *h_end = udh + udh_size; |
| 616 |
size_t stored_udhl = 0; |
| 617 |
*udhl = 0; |
| 618 |
if (udhi && l) { |
| 619 |
int hlen = i[p]; |
| 620 |
if (hlen) { |
| 621 |
b = 1; |
| 622 |
p++; |
| 623 |
l--; |
| 624 |
while (hlen-- && l) { |
| 625 |
if (h < h_end) { |
| 626 |
*h++ = i[p]; |
| 627 |
stored_udhl++; |
| 628 |
} |
| 629 |
p++; |
| 630 |
b += 8; |
| 631 |
while (b >= 7) { |
| 632 |
b -= 7; |
| 633 |
l--; |
| 634 |
if (!l) { |
| 635 |
break; |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
if (b) { |
| 641 |
b = 7 - b; |
| 642 |
l--; |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
while (l--) { |
| 647 |
unsigned char v; |
| 648 |
if (b < 2) { |
| 649 |
v = ((i[p] >> b) & 0x7F); |
| 650 |
} else { |
| 651 |
v = ((((i[p] >> b) + (i[p + 1] << (8 - b)))) & 0x7F); |
| 652 |
} |
| 653 |
b += 7; |
| 654 |
if (b >= 8) { |
| 655 |
b -= 8; |
| 656 |
p++; |
| 657 |
} |
| 658 |
|
| 659 |
if (o > ud && o[-1] == 0x00A0 && escapes[v]) { |
| 660 |
o[-1] = escapes[v]; |
| 661 |
} else if (o < o_end) { |
| 662 |
*o++ = defaultalphabet[v]; |
| 663 |
} |
| 664 |
} |
| 665 |
*udl = o - ud; |
| 666 |
*udhl = stored_udhl; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
static void unpacksms8(unsigned char *i, unsigned char l, unsigned char *udh, size_t udh_size, |
| 674 |
int *udhl, unsigned short *ud, size_t ud_size, int *udl, char udhi) |
| 675 |
{ |
| 676 |
unsigned short *o = ud; |
| 677 |
unsigned short *o_end = ud + ud_size; |
| 678 |
unsigned char *h = udh; |
| 679 |
unsigned char *h_end = udh + udh_size; |
| 680 |
size_t stored_udhl = 0; |
| 681 |
*udhl = 0; |
| 682 |
if (udhi) { |
| 683 |
int n = *i; |
| 684 |
if (n) { |
| 685 |
i++; |
| 686 |
l--; |
| 687 |
while (l && n) { |
| 688 |
l--; |
| 689 |
n--; |
| 690 |
if (h < h_end) { |
| 691 |
*h++ = *i; |
| 692 |
stored_udhl++; |
| 693 |
} |
| 694 |
i++; |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
while (l--) { |
| 699 |
if (o < o_end) { |
| 700 |
*o++ = *i; |
| 701 |
} |
| 702 |
i++; |
| 703 |
} |
| 704 |
*udl = o - ud; |
| 705 |
*udhl = stored_udhl; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
static void unpacksms16(unsigned char *i, unsigned char l, unsigned char *udh, size_t udh_size, |
| 712 |
int *udhl, unsigned short *ud, size_t ud_size, int *udl, char udhi) |
| 713 |
{ |
| 714 |
unsigned short *o = ud; |
| 715 |
unsigned short *o_end = ud + ud_size; |
| 716 |
unsigned char *h = udh; |
| 717 |
unsigned char *h_end = udh + udh_size; |
| 718 |
size_t stored_udhl = 0; |
| 719 |
*udhl = 0; |
| 720 |
if (udhi) { |
| 721 |
int n = *i; |
| 722 |
if (n) { |
| 723 |
i++; |
| 724 |
l--; |
| 725 |
while (l && n) { |
| 726 |
l--; |
| 727 |
n--; |
| 728 |
if (h < h_end) { |
| 729 |
*h++ = *i; |
| 730 |
stored_udhl++; |
| 731 |
} |
| 732 |
i++; |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
while (l--) { |
| 737 |
int v = *i++; |
| 738 |
if (l && l--) { |
| 739 |
v = (v << 8) + *i++; |
| 740 |
} |
| 741 |
if (o < o_end) { |
| 742 |
*o++ = v; |
| 743 |
} |
| 744 |
} |
| 745 |
*udl = o - ud; |
| 746 |
*udhl = stored_udhl; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
static int unpacksms(unsigned char dcs, unsigned char *i, unsigned char *udh, size_t udh_size, |
| 751 |
int *udhl, unsigned short *ud, size_t ud_size, int *udl, char udhi) |
| 752 |
{ |
| 753 |
int l = *i++; |
| 754 |
if (is7bit(dcs)) { |
| 755 |
unpacksms7(i, l, udh, udh_size, udhl, ud, ud_size, udl, udhi); |
| 756 |
l = (l * 7 + 7) / 8; |
| 757 |
} else if (is8bit(dcs)) { |
| 758 |
unpacksms8(i, l, udh, udh_size, udhl, ud, ud_size, udl, udhi); |
| 759 |
} else { |
| 760 |
l += l % 2; |
| 761 |
unpacksms16(i, l, udh, udh_size, udhl, ud, ud_size, udl, udhi); |
| 762 |
} |
| 763 |
return l + 1; |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
static unsigned char unpackaddress(char *o, size_t o_size, unsigned char *i) |
| 768 |
{ |
| 769 |
unsigned char l = i[0], p; |
| 770 |
char *o_end; |
| 771 |
|
| 772 |
if (!o_size) { |
| 773 |
return (l + 5) / 2; |
| 774 |
} |
| 775 |
|
| 776 |
o_end = o + o_size - 1; |
| 777 |
if (i[1] == 0x91) { |
| 778 |
if (o < o_end) { |
| 779 |
*o++ = '+'; |
| 780 |
} |
| 781 |
} |
| 782 |
for (p = 0; p < l; p++) { |
| 783 |
if (o < o_end) { |
| 784 |
if (p & 1) { |
| 785 |
*o++ = (i[2 + p / 2] >> 4) + '0'; |
| 786 |
} else { |
| 787 |
*o++ = (i[2 + p / 2] & 0xF) + '0'; |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
*o = 0; |
| 792 |
return (l + 5) / 2; |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
static unsigned char packaddress(unsigned char *o, char *i) |
| 797 |
{ |
| 798 |
unsigned char p = 2; |
| 799 |
o[0] = 0; |
| 800 |
if (*i == '+') { |
| 801 |
i++; |
| 802 |
o[1] = 0x91; |
| 803 |
} else { |
| 804 |
o[1] = 0x81; |
| 805 |
} |
| 806 |
for ( ; *i ; i++) { |
| 807 |
if (!isdigit(*i)) { |
| 808 |
continue; |
| 809 |
} |
| 810 |
if (o[0] & 1) { |
| 811 |
o[p++] |= ((*i & 0xF) << 4); |
| 812 |
} else { |
| 813 |
o[p] = (*i & 0xF); |
| 814 |
} |
| 815 |
o[0]++; |
| 816 |
} |
| 817 |
if (o[0] & 1) { |
| 818 |
o[p++] |= 0xF0; |
| 819 |
} |
| 820 |
return p; |
| 821 |
} |
| 822 |
|
| 823 |
|
| 824 |
static void sms_log(sms_t * h, char status) |
| 825 |
{ |
| 826 |
int o; |
| 827 |
|
| 828 |
if (*h->oa == '\0' && *h->da == '\0') { |
| 829 |
return; |
| 830 |
} |
| 831 |
o = open(log_file, O_CREAT | O_APPEND | O_WRONLY, AST_FILE_MODE); |
| 832 |
if (o >= 0) { |
| 833 |
char line[1000], mrs[3] = "", *p; |
| 834 |
char buf[30]; |
| 835 |
unsigned char n; |
| 836 |
|
| 837 |
if (h->mr >= 0) { |
| 838 |
snprintf(mrs, sizeof(mrs), "%02hhX", (unsigned char)h->mr); |
| 839 |
} |
| 840 |
snprintf(line, sizeof(line), "%s %c%c%c%s %s %s %s ", |
| 841 |
isodate(time(NULL), buf, sizeof(buf)), |
| 842 |
status, h->rx ? 'I' : 'O', h->smsc ? 'S' : 'M', mrs, h->queue, |
| 843 |
S_OR(h->oa, "-"), S_OR(h->da, "-") ); |
| 844 |
p = line + strlen(line); |
| 845 |
|
| 846 |
if (h->nolog) { |
| 847 |
p += snprintf(p, 1000 - strlen(line), "udl=%d", h->udl); |
| 848 |
} else { |
| 849 |
for (n = 0; n < h->udl; n++) { |
| 850 |
if (h->ud[n] == '\\') { |
| 851 |
*p++ = '\\'; |
| 852 |
*p++ = '\\'; |
| 853 |
} else if (h->ud[n] == '\n') { |
| 854 |
*p++ = '\\'; |
| 855 |
*p++ = 'n'; |
| 856 |
} else if (h->ud[n] == '\r') { |
| 857 |
*p++ = '\\'; |
| 858 |
*p++ = 'r'; |
| 859 |
} else if (h->ud[n] < 32 || h->ud[n] == 127) { |
| 860 |
*p++ = 0xbf; |
| 861 |
} else { |
| 862 |
*p++ = h->ud[n]; |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
*p++ = '\n'; |
| 867 |
*p = 0; |
| 868 |
if (write(o, line, strlen(line)) < 0) { |
| 869 |
ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno)); |
| 870 |
} |
| 871 |
close(o); |
| 872 |
} |
| 873 |
*h->oa = *h->da = h->udl = 0; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
static void sms_readfile(sms_t * h, char *fn) |
| 878 |
{ |
| 879 |
char line[1000]; |
| 880 |
FILE *s; |
| 881 |
char dcsset = 0; |
| 882 |
ast_log(LOG_NOTICE, "Sending %s\n", fn); |
| 883 |
h->rx = h->udl = *h->oa = *h->da = h->pid = h->srr = h->udhi = h->rp = h->vp = h->udhl = 0; |
| 884 |
h->mr = -1; |
| 885 |
h->dcs = 0xF1; |
| 886 |
h->scts = ast_tvnow(); |
| 887 |
s = fopen(fn, "r"); |
| 888 |
if (s) { |
| 889 |
if (unlink(fn)) { |
| 890 |
fclose(s); |
| 891 |
return; |
| 892 |
} |
| 893 |
while (fgets (line, sizeof(line), s)) { |
| 894 |
char *p; |
| 895 |
void *pp = &p; |
| 896 |
for (p = line; *p && *p != '\n' && *p != '\r'; p++); |
| 897 |
*p = 0; |
| 898 |
p = line; |
| 899 |
if (!*p || *p == ';') { |
| 900 |
continue; |
| 901 |
} |
| 902 |
while (isalnum(*p)) { |
| 903 |
*p = tolower (*p); |
| 904 |
p++; |
| 905 |
} |
| 906 |
while (isspace (*p)) { |
| 907 |
*p++ = 0; |
| 908 |
} |
| 909 |
if (*p == '=') { |
| 910 |
*p++ = 0; |
| 911 |
if (!strcmp(line, "ud")) { |
| 912 |
unsigned char o = 0; |
| 913 |
memcpy(h->udtxt, p, SMSLEN); |
| 914 |
while (*p && o < SMSLEN) { |
| 915 |
h->ud[o++] = utf8decode(pp); |
| 916 |
p++; |
| 917 |
} |
| 918 |
h->udl = o; |
| 919 |
if (*p) { |
| 920 |
ast_log(LOG_WARNING, "UD too long in %s\n", fn); |
| 921 |
} |
| 922 |
} else { |
| 923 |
while (isspace (*p)) { |
| 924 |
p++; |
| 925 |
} |
| 926 |
if (!strcmp(line, "oa") && strlen(p) < sizeof(h->oa)) { |
| 927 |
numcpy (h->oa, p); |
| 928 |
} else if (!strcmp(line, "da") && strlen(p) < sizeof(h->oa)) { |
| 929 |
numcpy (h->da, p); |
| 930 |
} else if (!strcmp(line, "pid")) { |
| 931 |
h->pid = atoi(p); |
| 932 |
} else if (!strcmp(line, "dcs")) { |
| 933 |
h->dcs = atoi(p); |
| 934 |
dcsset = 1; |
| 935 |
} else if (!strcmp(line, "mr")) { |
| 936 |
h->mr = atoi(p); |
| 937 |
} else if (!strcmp(line, "srr")) { |
| 938 |
h->srr = (atoi(p) ? 1 : 0); |
| 939 |
} else if (!strcmp(line, "vp")) { |
| 940 |
h->vp = atoi(p); |
| 941 |
} else if (!strcmp(line, "rp")) { |
| 942 |
h->rp = (atoi(p) ? 1 : 0); |
| 943 |
} else if (!strcmp(line, "scts")) { |
| 944 |
int Y, m, d, H, M, S; |
| 945 |
|
| 946 |
if (sscanf(p, "%4d-%2d-%2dT%2d:%2d:%2d", &Y, &m, &d, &H, &M, &S) == 6) { |
| 947 |
struct ast_tm t = { 0, }; |
| 948 |
t.tm_year = Y - 1900; |
| 949 |
t.tm_mon = m - 1; |
| 950 |
t.tm_mday = d; |
| 951 |
t.tm_hour = H; |
| 952 |
t.tm_min = M; |
| 953 |
t.tm_sec = S; |
| 954 |
t.tm_isdst = -1; |
| 955 |
h->scts = ast_mktime(&t, NULL); |
| 956 |
if (h->scts.tv_sec == 0) { |
| 957 |
ast_log(LOG_WARNING, "Bad date/timein %s: %s", fn, p); |
| 958 |
} |
| 959 |
} |
| 960 |
} else { |
| 961 |
ast_log(LOG_WARNING, "Cannot parse in %s: %s=%si\n", fn, line, p); |
| 962 |
} |
| 963 |
} |
| 964 |
} else if (*p == '#') { |
| 965 |
*p++ = 0; |
| 966 |
if (*p == '#') { |
| 967 |
p++; |
| 968 |
if (!strcmp(line, "ud")) { |
| 969 |
int o = 0; |
| 970 |
while (*p && o < SMSLEN) { |
| 971 |
if (isxdigit(*p) && isxdigit(p[1]) && isxdigit(p[2]) && isxdigit(p[3])) { |
| 972 |
h->ud[o++] = |
| 973 |
(((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 12) + |
| 974 |
(((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) + |
| 975 |
(((isalpha(p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha(p[3]) ? 9 : 0) + (p[3] & 0xF)); |
| 976 |
p += 4; |
| 977 |
} else |
| 978 |
break; |
| 979 |
} |
| 980 |
h->udl = o; |
| 981 |
if (*p) |
| 982 |
ast_log(LOG_WARNING, "UD too long / invalid UCS-2 hex in %s\n", fn); |
| 983 |
} else |
| 984 |
ast_log(LOG_WARNING, "Only ud can use ## format, %s\n", fn); |
| 985 |
} else if (!strcmp(line, "ud")) { |
| 986 |
int o = 0; |
| 987 |
while (*p && o < SMSLEN) { |
| 988 |
if (isxdigit(*p) && isxdigit(p[1])) { |
| 989 |
h->ud[o++] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)); |
| 990 |
p += 2; |
| 991 |
} else { |
| 992 |
break; |
| 993 |
} |
| 994 |
} |
| 995 |
h->udl = o; |
| 996 |
if (*p) { |
| 997 |
ast_log(LOG_WARNING, "UD too long / invalid UCS-1 hex in %s\n", fn); |
| 998 |
} |
| 999 |
} else if (!strcmp(line, "udh")) { |
| 1000 |
unsigned char o = 0; |
| 1001 |
h->udhi = 1; |
| 1002 |
while (*p && o < SMSLEN) { |
| 1003 |
if (isxdigit(*p) && isxdigit(p[1])) { |
| 1004 |
h->udh[o] = (((isalpha(*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha(p[1]) ? 9 : 0) + (p[1] & 0xF)); |
| 1005 |
o++; |
| 1006 |
p += 2; |
| 1007 |
} else { |
| 1008 |
break; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
h->udhl = o; |
| 1012 |
if (*p) { |
| 1013 |
ast_log(LOG_WARNING, "UDH too long / invalid hex in %s\n", fn); |
| 1014 |
} |
| 1015 |
} else { |
| 1016 |
ast_log(LOG_WARNING, "Only ud and udh can use # format, %s\n", fn); |
| 1017 |
} |
| 1018 |
} else { |
| 1019 |
ast_log(LOG_WARNING, "Cannot parse in %s: %s\n", fn, line); |
| 1020 |
} |
| 1021 |
} |
| 1022 |
fclose(s); |
| 1023 |
if (!dcsset && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) { |
| 1024 |
if (packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) { |
| 1025 |
if (packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) { |
| 1026 |
ast_log(LOG_WARNING, "Invalid UTF-8 message even for UCS-2 (%s)\n", fn); |
| 1027 |
} else { |
| 1028 |
h->dcs = 0x08; |
| 1029 |
ast_log(LOG_WARNING, "Sending in 16 bit format(%s)\n", fn); |
| 1030 |
} |
| 1031 |
} else { |
| 1032 |
h->dcs = 0xF5; |
| 1033 |
ast_log(LOG_WARNING, "Sending in 8 bit format(%s)\n", fn); |
| 1034 |
} |
| 1035 |
} |
| 1036 |
if (is7bit(h->dcs) && packsms7(0, h->udhl, h->udh, h->udl, h->ud) < 0) { |
| 1037 |
ast_log(LOG_WARNING, "Invalid 7 bit GSM data %s\n", fn); |
| 1038 |
} |
| 1039 |
if (is8bit(h->dcs) && packsms8(0, h->udhl, h->udh, h->udl, h->ud) < 0) { |
| 1040 |
ast_log(LOG_WARNING, "Invalid 8 bit data %s\n", fn); |
| 1041 |
} |
| 1042 |
if (is16bit(h->dcs) && packsms16(0, h->udhl, h->udh, h->udl, h->ud) < 0) { |
| 1043 |
ast_log(LOG_WARNING, "Invalid 16 bit data %s\n", fn); |
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
|
| 1049 |
static void sms_writefile(sms_t * h) |
| 1050 |
{ |
| 1051 |
char fn[200] = "", fn2[200] = ""; |
| 1052 |
char buf[30]; |
| 1053 |
FILE *o; |
| 1054 |
|
| 1055 |
if (ast_tvzero(h->scts)) { |
| 1056 |
h->scts = ast_tvnow(); |
| 1057 |
} |
| 1058 |
snprintf(fn, sizeof(fn), "%s/sms/%s", ast_config_AST_SPOOL_DIR, h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx"); |
| 1059 |
ast_mkdir(fn, 0777); |
| 1060 |
ast_copy_string(fn2, fn, sizeof(fn2)); |
| 1061 |
snprintf(fn2 + strlen(fn2), sizeof(fn2) - strlen(fn2), "/%s.%s-%u", h->queue, isodate(h->scts.tv_sec, buf, sizeof(buf)), seq++); |
| 1062 |
snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/.%s", fn2 + strlen(fn) + 1); |
| 1063 |
if ((o = fopen(fn, "w")) == NULL) { |
| 1064 |
return; |
| 1065 |
} |
| 1066 |
|
| 1067 |
if (*h->oa) { |
| 1068 |
fprintf(o, "oa=%s\n", h->oa); |
| 1069 |
} |
| 1070 |
if (*h->da) { |
| 1071 |
fprintf(o, "da=%s\n", h->da); |
| 1072 |
} |
| 1073 |
if (h->udhi) { |
| 1074 |
unsigned int p; |
| 1075 |
fprintf(o, "udh#"); |
| 1076 |
for (p = 0; p < h->udhl; p++) { |
| 1077 |
fprintf(o, "%02hhX", (unsigned char)h->udh[p]); |
| 1078 |
} |
| 1079 |
fprintf(o, "\n"); |
| 1080 |
} |
| 1081 |
if (h->udl) { |
| 1082 |
unsigned int p; |
| 1083 |
for (p = 0; p < h->udl && h->ud[p] >= ' '; p++); |
| 1084 |
if (p < h->udl) { |
| 1085 |
fputc(';', o); |
| 1086 |
} |
| 1087 |
fprintf(o, "ud="); |
| 1088 |
for (p = 0; p < h->udl; p++) { |
| 1089 |
unsigned short v = h->ud[p]; |
| 1090 |
if (v < 32) { |
| 1091 |
fputc(191, o); |
| 1092 |
} else if (v < 0x80) { |
| 1093 |
fputc(v, o); |
| 1094 |
} else if (v < 0x800) { |
| 1095 |
fputc(0xC0 + (v >> 6), o); |
| 1096 |
fputc(0x80 + (v & 0x3F), o); |
| 1097 |
} else { |
| 1098 |
fputc(0xE0 + (v >> 12), o); |
| 1099 |
fputc(0x80 + ((v >> 6) & 0x3F), o); |
| 1100 |
fputc(0x80 + (v & 0x3F), o); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
fprintf(o, "\n"); |
| 1104 |
for (p = 0; p < h->udl && h->ud[p] >= ' '; p++); |
| 1105 |
if (p < h->udl) { |
| 1106 |
for (p = 0; p < h->udl && h->ud[p] < 0x100; p++); |
| 1107 |
if (p == h->udl) { |
| 1108 |
fprintf(o, "ud#"); |
| 1109 |
for (p = 0; p < h->udl; p++) { |
| 1110 |
fprintf(o, "%02hhX", (unsigned char)h->ud[p]); |
| 1111 |
} |
| 1112 |
fprintf(o, "\n"); |
| 1113 |
} else { |
| 1114 |
fprintf(o, "ud##"); |
| 1115 |
for (p = 0; p < h->udl; p++) { |
| 1116 |
fprintf(o, "%04X", (unsigned)h->ud[p]); |
| 1117 |
} |
| 1118 |
fprintf(o, "\n"); |
| 1119 |
} |
| 1120 |
} |
| 1121 |
} |
| 1122 |
if (h->scts.tv_sec) { |
| 1123 |
char datebuf[30]; |
| 1124 |
fprintf(o, "scts=%s\n", isodate(h->scts.tv_sec, datebuf, sizeof(datebuf))); |
| 1125 |
} |
| 1126 |
if (h->pid) { |
| 1127 |
fprintf(o, "pid=%d\n", h->pid); |
| 1128 |
} |
| 1129 |
if (h->dcs != 0xF1) { |
| 1130 |
fprintf(o, "dcs=%d\n", h->dcs); |
| 1131 |
} |
| 1132 |
if (h->vp) { |
| 1133 |
fprintf(o, "vp=%u\n", h->vp); |
| 1134 |
} |
| 1135 |
if (h->srr) { |
| 1136 |
fprintf(o, "srr=1\n"); |
| 1137 |
} |
| 1138 |
if (h->mr >= 0) { |
| 1139 |
fprintf(o, "mr=%d\n", h->mr); |
| 1140 |
} |
| 1141 |
if (h->rp) { |
| 1142 |
fprintf(o, "rp=1\n"); |
| 1143 |
} |
| 1144 |
fclose(o); |
| 1145 |
if (rename(fn, fn2)) { |
| 1146 |
unlink(fn); |
| 1147 |
} else { |
| 1148 |
ast_log(LOG_NOTICE, "Received to %s\n", fn2); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
static struct dirent *readdirqueue(DIR *d, char *queue) |
| 1154 |
{ |
| 1155 |
struct dirent *f; |
| 1156 |
do { |
| 1157 |
f = readdir(d); |
| 1158 |
} while (f && (*f->d_name == '.' || strncmp(f->d_name, queue, strlen(queue)) || f->d_name[strlen(queue)] != '.')); |
| 1159 |
return f; |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
static unsigned char sms_handleincoming (sms_t * h) |
| 1164 |
{ |
| 1165 |
unsigned char p = 3; |
| 1166 |
if (h->smsc) { |
| 1167 |
if ((h->imsg[2] & 3) == 1) { |
| 1168 |
h->udhl = h->udl = 0; |
| 1169 |
h->vp = 0; |
| 1170 |
h->srr = ((h->imsg[2] & 0x20) ? 1 : 0); |
| 1171 |
h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0); |
| 1172 |
h->rp = ((h->imsg[2] & 0x80) ? 1 : 0); |
| 1173 |
ast_copy_string(h->oa, h->cli, sizeof(h->oa)); |
| 1174 |
h->scts = ast_tvnow(); |
| 1175 |
h->mr = h->imsg[p++]; |
| 1176 |
p += unpackaddress(h->da, ARRAY_LEN(h->da), h->imsg + p); |
| 1177 |
h->pid = h->imsg[p++]; |
| 1178 |
h->dcs = h->imsg[p++]; |
| 1179 |
if ((h->imsg[2] & 0x18) == 0x10) { |
| 1180 |
if (h->imsg[p] < 144) { |
| 1181 |
h->vp = (h->imsg[p] + 1) * 5; |
| 1182 |
} else if (h->imsg[p] < 168) { |
| 1183 |
h->vp = 720 + (h->imsg[p] - 143) * 30; |
| 1184 |
} else if (h->imsg[p] < 197) { |
| 1185 |
h->vp = (h->imsg[p] - 166) * 1440; |
| 1186 |
} else { |
| 1187 |
h->vp = (h->imsg[p] - 192) * 10080; |
| 1188 |
} |
| 1189 |
p++; |
| 1190 |
} else if (h->imsg[2] & 0x18) { |
| 1191 |
p += 7; |
| 1192 |
} |
| 1193 |
p += unpacksms(h->dcs, h->imsg + p, h->udh, ARRAY_LEN(h->udh), &h->udhl, h->ud, ARRAY_LEN(h->ud), &h->udl, h->udhi); |
| 1194 |
h->rx = 1; |
| 1195 |
sms_writefile(h); |
| 1196 |
if (p != h->imsg[1] + 2) { |
| 1197 |
ast_log(LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2); |
| 1198 |
return 0xFF; |
| 1199 |
} |
| 1200 |
} else { |
| 1201 |
ast_log(LOG_WARNING, "Unknown message type %02hhX\n", h->imsg[2]); |
| 1202 |
return 0xFF; |
| 1203 |
} |
| 1204 |
} else { |
| 1205 |
if (!(h->imsg[2] & 3)) { |
| 1206 |
*h->da = h->srr = h->rp = h->vp = h->udhi = h->udhl = h->udl = 0; |
| 1207 |
h->srr = ((h->imsg[2] & 0x20) ? 1 : 0); |
| 1208 |
h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0); |
| 1209 |
h->rp = ((h->imsg[2] & 0x80) ? 1 : 0); |
| 1210 |
h->mr = -1; |
| 1211 |
p += unpackaddress(h->oa, ARRAY_LEN(h->oa), h->imsg + p); |
| 1212 |
h->pid = h->imsg[p++]; |
| 1213 |
h->dcs = h->imsg[p++]; |
| 1214 |
h->scts = unpackdate(h->imsg + p); |
| 1215 |
p += 7; |
| 1216 |
p += unpacksms(h->dcs, h->imsg + p, h->udh, ARRAY_LEN(h->udh), &h->udhl, h->ud, ARRAY_LEN(h->ud), &h->udl, h->udhi); |
| 1217 |
h->rx = 1; |
| 1218 |
sms_writefile(h); |
| 1219 |
if (p != h->imsg[1] + 2) { |
| 1220 |
ast_log(LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2); |
| 1221 |
return 0xFF; |
| 1222 |
} |
| 1223 |
} else { |
| 1224 |
ast_log(LOG_WARNING, "Unknown message type %02hhX\n", h->imsg[2]); |
| 1225 |
return 0xFF; |
| 1226 |
} |
| 1227 |
} |
| 1228 |
return 0; |
| 1229 |
} |
| 1230 |
|
| 1231 |
#ifdef SOLARIS |
| 1232 |
#define NAME_MAX 1024 |
| 1233 |
#endif |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
static void adddata_proto2(sms_t *h, unsigned char msg, char *data, int size) |
| 1240 |
{ |
| 1241 |
int x = h->omsg[1] + 2; |
| 1242 |
if (x == 2) { |
| 1243 |
x += 2; |
| 1244 |
} |
| 1245 |
h->omsg[x++] = msg; |
| 1246 |
h->omsg[x++] = (unsigned char)size; |
| 1247 |
h->omsg[x++] = 0; |
| 1248 |
for (; size > 0 ; size--) { |
| 1249 |
h->omsg[x++] = *data++; |
| 1250 |
} |
| 1251 |
h->omsg[1] = x - 2; |
| 1252 |
h->omsg[2] = x - 4; |
| 1253 |
h->omsg[3] = 0; |
| 1254 |
} |
| 1255 |
|
| 1256 |
static void putdummydata_proto2(sms_t *h) |
| 1257 |
{ |
| 1258 |
adddata_proto2(h, 0x10, "\0", 1); |
| 1259 |
adddata_proto2(h, 0x11, "\0\0\0\0\0\0", 6); |
| 1260 |
adddata_proto2(h, 0x12, "\2\0\4", 3); |
| 1261 |
adddata_proto2(h, 0x13, h->udtxt, h->udl); |
| 1262 |
} |
| 1263 |
|
| 1264 |
static void sms_compose2(sms_t *h, int more) |
| 1265 |
{ |
| 1266 |
struct ast_tm tm; |
| 1267 |
struct timeval now = h->scts; |
| 1268 |
char stm[45]; |
| 1269 |
|
| 1270 |
h->omsg[0] = 0x00; |
| 1271 |
h->omsg[1] = 0; |
| 1272 |
putdummydata_proto2(h); |
| 1273 |
if (h->smsc) { |
| 1274 |
h->omsg[0] = 0x11; |
| 1275 |
|
| 1276 |
ast_localtime(&now, &tm, NULL); |
| 1277 |
sprintf(stm, "%02d%02d%02d%02d", tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); |
| 1278 |
adddata_proto2(h, 0x14, stm, 8); |
| 1279 |
if (*h->oa == 0) { |
| 1280 |
strcpy(h->oa, "00000000"); |
| 1281 |
} |
| 1282 |
adddata_proto2(h, 0x15, h->oa, strlen(h->oa)); |
| 1283 |
adddata_proto2(h, 0x17, "\1", 1); |
| 1284 |
} else { |
| 1285 |
h->omsg[0] = 0x10; |
| 1286 |
|
| 1287 |
adddata_proto2(h, 0x17, "\1", 1); |
| 1288 |
if (*h->da == 0) { |
| 1289 |
strcpy(h->da, "00000000"); |
| 1290 |
} |
| 1291 |
adddata_proto2(h, 0x18, h->da, strlen(h->da)); |
| 1292 |
adddata_proto2(h, 0x1B, "\1", 1); |
| 1293 |
adddata_proto2(h, 0x1C, "\0\0\0", 3); |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
static void putdummydata_proto2(sms_t *h); |
| 1298 |
|
| 1299 |
#define MAX_DEBUG_LEN 300 |
| 1300 |
static char *sms_hexdump(unsigned char buf[], int size, char *s ) |
| 1301 |
{ |
| 1302 |
char *p; |
| 1303 |
int f; |
| 1304 |
|
| 1305 |
for (p = s, f = 0; f < size && f < MAX_DEBUG_LEN; f++, p += 3) { |
| 1306 |
sprintf(p, "%02hhX ", (unsigned char)buf[f]); |
| 1307 |
} |
| 1308 |
return(s); |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
static int sms_handleincoming_proto2(sms_t *h) |
| 1314 |
{ |
| 1315 |
int f, i, sz = 0; |
| 1316 |
int msg, msgsz; |
| 1317 |
struct ast_tm tm; |
| 1318 |
struct timeval now = { 0, 0 }; |
| 1319 |
char debug_buf[MAX_DEBUG_LEN * 3 + 1]; |
| 1320 |
|
| 1321 |
sz = h->imsg[1] + 2; |
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
now = h->scts = ast_tvnow(); |
| 1326 |
for (f = 4; f < sz; ) { |
| 1327 |
msg = h->imsg[f++]; |
| 1328 |
msgsz = h->imsg[f++]; |
| 1329 |
msgsz += (h->imsg[f++] * 256); |
| 1330 |
switch (msg) { |
| 1331 |
case 0x13: |
| 1332 |
ast_verb(3, "SMS-P2 Body#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]); |
| 1333 |
if (msgsz >= sizeof(h->ud)) { |
| 1334 |
msgsz = sizeof(h->ud) - 1; |
| 1335 |
} |
| 1336 |
for (i = 0; i < msgsz; i++) { |
| 1337 |
h->ud[i] = h->imsg[f + i]; |
| 1338 |
} |
| 1339 |
h->udl = msgsz; |
| 1340 |
break; |
| 1341 |
case 0x14: |
| 1342 |
now = h->scts = ast_tvnow(); |
| 1343 |
ast_localtime(&now, &tm, NULL); |
| 1344 |
tm.tm_mon = ( (h->imsg[f] * 10) + h->imsg[f + 1] ) - 1; |
| 1345 |
tm.tm_mday = ( (h->imsg[f + 2] * 10) + h->imsg[f + 3] ); |
| 1346 |
tm.tm_hour = ( (h->imsg[f + 4] * 10) + h->imsg[f + 5] ); |
| 1347 |
tm.tm_min = ( (h->imsg[f + 6] * 10) + h->imsg[f + 7] ); |
| 1348 |
tm.tm_sec = 0; |
| 1349 |
h->scts = ast_mktime(&tm, NULL); |
| 1350 |
ast_verb(3, "SMS-P2 Date#%02X=%02d/%02d %02d:%02d\n", (unsigned)msg, tm.tm_mday, tm.tm_mon + 1, tm.tm_hour, tm.tm_min); |
| 1351 |
break; |
| 1352 |
case 0x15: |
| 1353 |
if (msgsz >= 20) { |
| 1354 |
msgsz = 20 - 1; |
| 1355 |
} |
| 1356 |
ast_verb(3, "SMS-P2 Origin#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]); |
| 1357 |
ast_copy_string(h->oa, (char *)(&h->imsg[f]), msgsz + 1); |
| 1358 |
break; |
| 1359 |
case 0x18: |
| 1360 |
if (msgsz >= 20) { |
| 1361 |
msgsz = 20 - 1; |
| 1362 |
} |
| 1363 |
ast_verb(3, "SMS-P2 Destination#%02X=[%.*s]\n", (unsigned)msg, msgsz, &h->imsg[f]); |
| 1364 |
ast_copy_string(h->da, (char *)(&h->imsg[f]), msgsz + 1); |
| 1365 |
break; |
| 1366 |
case 0x1C: |
| 1367 |
ast_verb(3, "SMS-P2 Notify#%02X=%s\n", (unsigned)msg, sms_hexdump(&h->imsg[f], 3, debug_buf)); |
| 1368 |
break; |
| 1369 |
default: |
| 1370 |
ast_verb(3, "SMS-P2 Par#%02X [%d]: %s\n", (unsigned)msg, msgsz, sms_hexdump(&h->imsg[f], msgsz, debug_buf)); |
| 1371 |
break; |
| 1372 |
} |
| 1373 |
f+=msgsz; |
| 1374 |
} |
| 1375 |
h->rx = 1; |
| 1376 |
sms_writefile(h); |
| 1377 |
return 0; |
| 1378 |
} |
| 1379 |
|
| 1380 |
#if 0 |
| 1381 |
static void smssend(sms_t *h, char *c) |
| 1382 |
{ |
| 1383 |
int f, x; |
| 1384 |
for (f = 0; f < strlen(c); f++) { |
| 1385 |
sscanf(&c[f*3], "%x", &x); |
| 1386 |
h->omsg[f] = x; |
| 1387 |
} |
| 1388 |
sms_messagetx(h); |
| 1389 |
} |
| 1390 |
#endif |
| 1391 |
|
| 1392 |
static void sms_nextoutgoing (sms_t *h); |
| 1393 |
|
| 1394 |
static void sms_messagerx2(sms_t * h) |
| 1395 |
{ |
| 1396 |
int p = h->imsg[0] & DLL_SMS_MASK ; |
| 1397 |
int cause; |
| 1398 |
|
| 1399 |
#define DLL2_ACK(h) ((h->framenumber & 1) ? DLL2_SMS_ACK1: DLL2_SMS_ACK1) |
| 1400 |
switch (p) { |
| 1401 |
case DLL2_SMS_EST: |
| 1402 |
sms_nextoutgoing (h); |
| 1403 |
|
| 1404 |
break; |
| 1405 |
|
| 1406 |
case DLL2_SMS_INFO_MO: |
| 1407 |
case DLL2_SMS_INFO_MT: |
| 1408 |
cause = sms_handleincoming_proto2(h); |
| 1409 |
if (!cause) { |
| 1410 |
sms_log(h, 'Y'); |
| 1411 |
} |
| 1412 |
h->omsg[0] = DLL2_ACK(h); |
| 1413 |
h->omsg[1] = 0x06; |
| 1414 |
h->omsg[2] = 0x04; |
| 1415 |
h->omsg[3] = 0x00; |
| 1416 |
h->omsg[4] = 0x1f; |
| 1417 |
h->omsg[5] = 0x01; |
| 1418 |
h->omsg[6] = 0x00; |
| 1419 |
h->omsg[7] = cause; |
| 1420 |
sms_messagetx(h); |
| 1421 |
break; |
| 1422 |
|
| 1423 |
case DLL2_SMS_NACK: |
| 1424 |
h->omsg[0] = DLL2_SMS_REL; |
| 1425 |
h->omsg[1] = 0x00; |
| 1426 |
sms_messagetx(h); |
| 1427 |
break; |
| 1428 |
|
| 1429 |
case DLL2_SMS_ACK0: |
| 1430 |
case DLL2_SMS_ACK1: |
| 1431 |
|
| 1432 |
if ( (h->omsg[0] & DLL_SMS_MASK) == DLL2_SMS_REL) { |
| 1433 |
|
| 1434 |
h->hangup = 1; |
| 1435 |
} else { |
| 1436 |
|
| 1437 |
ast_log(LOG_NOTICE, "SMS_SUBMIT or SMS_DELIVERY\n"); |
| 1438 |
sms_nextoutgoing (h); |
| 1439 |
} |
| 1440 |
break; |
| 1441 |
|
| 1442 |
case DLL2_SMS_REL: |
| 1443 |
h->omsg[0] = DLL2_ACK(h); |
| 1444 |
h->omsg[1] = 0; |
| 1445 |
sms_messagetx(h); |
| 1446 |
break; |
| 1447 |
} |
| 1448 |
} |
| 1449 |
|
| 1450 |
|
| 1451 |
static void sms_compose1(sms_t *h, int more) |
| 1452 |
{ |
| 1453 |
unsigned int p = 2; |
| 1454 |
|
| 1455 |
h->omsg[0] = 0x91; |
| 1456 |
if (h->smsc) { |
| 1457 |
h->omsg[p++] = (more ? 4 : 0) + ((h->udhl > 0) ? 0x40 : 0); |
| 1458 |
p += packaddress(h->omsg + p, h->oa); |
| 1459 |
h->omsg[p++] = h->pid; |
| 1460 |
h->omsg[p++] = h->dcs; |
| 1461 |
packdate(h->omsg + p, h->scts.tv_sec); |
| 1462 |
p += 7; |
| 1463 |
p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud); |
| 1464 |
} else { |
| 1465 |
h->omsg[p++] = |
| 1466 |
0x01 + (more ? 4 : 0) + (h->srr ? 0x20 : 0) + (h->rp ? 0x80 : 0) + (h->vp ? 0x10 : 0) + (h->udhi ? 0x40 : 0); |
| 1467 |
if (h->mr < 0) { |
| 1468 |
h->mr = message_ref++; |
| 1469 |
} |
| 1470 |
h->omsg[p++] = h->mr; |
| 1471 |
p += packaddress(h->omsg + p, h->da); |
| 1472 |
h->omsg[p++] = h->pid; |
| 1473 |
h->omsg[p++] = h->dcs; |
| 1474 |
if (h->vp) { |
| 1475 |
if (h->vp < 720) { |
| 1476 |
h->omsg[p++] = (h->vp + 4) / 5 - 1; |
| 1477 |
} else if (h->vp < 1440) { |
| 1478 |
h->omsg[p++] = (h->vp - 720 + 29) / 30 + 143; |
| 1479 |
} else if (h->vp < 43200) { |
| 1480 |
h->omsg[p++] = (h->vp + 1439) / 1440 + 166; |
| 1481 |
} else if (h->vp < 635040) { |
| 1482 |
h->omsg[p++] = (h->vp + 10079) / 10080 + 192; |
| 1483 |
} else { |
| 1484 |
h->omsg[p++] = 255; |
| 1485 |
} |
| 1486 |
} |
| 1487 |
p += packsms(h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud); |
| 1488 |
} |
| 1489 |
h->omsg[1] = p - 2; |
| 1490 |
} |
| 1491 |
|
| 1492 |
|
| 1493 |
static void sms_nextoutgoing (sms_t * h) |
| 1494 |
{ |
| 1495 |
char fn[100 + NAME_MAX] = ""; |
| 1496 |
DIR *d; |
| 1497 |
char more = 0; |
| 1498 |
|
| 1499 |
*h->da = *h->oa = '\0'; |
| 1500 |
h->rx = 0; |
| 1501 |
snprintf(fn, sizeof(fn), "%s/sms/%s", ast_config_AST_SPOOL_DIR, h->smsc ? "mttx" : "motx"); |
| 1502 |
ast_mkdir(fn, 0777); |
| 1503 |
d = opendir(fn); |
| 1504 |
if (d) { |
| 1505 |
struct dirent *f = readdirqueue(d, h->queue); |
| 1506 |
if (f) { |
| 1507 |
snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/%s", f->d_name); |
| 1508 |
sms_readfile(h, fn); |
| 1509 |
if (readdirqueue(d, h->queue)) { |
| 1510 |
more = 1; |
| 1511 |
} |
| 1512 |
} |
| 1513 |
closedir(d); |
| 1514 |
} |
| 1515 |
if (*h->da || *h->oa) { |
| 1516 |
if (h->protocol == 2) { |
| 1517 |
sms_compose2(h, more); |
| 1518 |
} else { |
| 1519 |
sms_compose1(h, more); |
| 1520 |
} |
| 1521 |
} else { |
| 1522 |
if (h->protocol == 2) { |
| 1523 |
h->omsg[0] = 0x17; |
| 1524 |
h->omsg[1] = 0; |
| 1525 |
h->sent_rel = 1; |
| 1526 |
} else { |
| 1527 |
h->omsg[0] = 0x94; |
| 1528 |
h->omsg[1] = 0; |
| 1529 |
h->sent_rel = 1; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
sms_messagetx(h); |
| 1533 |
} |
| 1534 |
|
| 1535 |
#define DIR_RX 1 |
| 1536 |
#define DIR_TX 2 |
| 1537 |
static void sms_debug (int dir, sms_t *h) |
| 1538 |
{ |
| 1539 |
char txt[259 * 3 + 1]; |
| 1540 |
char *p = txt; |
| 1541 |
unsigned char *msg = (dir == DIR_RX) ? h->imsg : h->omsg; |
| 1542 |
int n = (dir == DIR_RX) ? h->ibytep : msg[1] + 2; |
| 1543 |
int q = 0; |
| 1544 |
while (q < n && q < 30) { |
| 1545 |
sprintf(p, " %02hhX", msg[q++]); |
| 1546 |
p += 3; |
| 1547 |
} |
| 1548 |
if (q < n) { |
| 1549 |
sprintf(p, "..."); |
| 1550 |
} |
| 1551 |
ast_verb(3, "SMS %s%s\n", dir == DIR_RX ? "RX" : "TX", txt); |
| 1552 |
} |
| 1553 |
|
| 1554 |
|
| 1555 |
static void sms_messagerx(sms_t * h) |
| 1556 |
{ |
| 1557 |
int cause; |
| 1558 |
|
| 1559 |
sms_debug (DIR_RX, h); |
| 1560 |
if (h->protocol == 2) { |
| 1561 |
sms_messagerx2(h); |
| 1562 |
return; |
| 1563 |
} |
| 1564 |
|
| 1565 |
switch (h->imsg[0]) { |
| 1566 |
case 0x91: |
| 1567 |
cause = sms_handleincoming (h); |
| 1568 |
if (!cause) { |
| 1569 |
sms_log(h, 'Y'); |
| 1570 |
h->omsg[0] = 0x95; |
| 1571 |
h->omsg[1] = 0x02; |
| 1572 |
h->omsg[2] = 0x00; |
| 1573 |
h->omsg[3] = 0x00; |
| 1574 |
} else { |
| 1575 |
sms_log(h, 'N'); |
| 1576 |
h->omsg[0] = 0x96; |
| 1577 |
h->omsg[1] = 3; |
| 1578 |
h->omsg[2] = 0; |
| 1579 |
h->omsg[3] = cause; |
| 1580 |
h->omsg[4] = 0; |
| 1581 |
} |
| 1582 |
sms_messagetx(h); |
| 1583 |
break; |
| 1584 |
|
| 1585 |
case 0x92: |
| 1586 |
h->err = 1; |
| 1587 |
sms_messagetx(h); |
| 1588 |
break; |
| 1589 |
case 0x93: |
| 1590 |
sms_nextoutgoing (h); |
| 1591 |
break; |
| 1592 |
case 0x94: |
| 1593 |
h->hangup = 1; |
| 1594 |
break; |
| 1595 |
case 0x95: |
| 1596 |
sms_log(h, 'Y'); |
| 1597 |
sms_nextoutgoing (h); |
| 1598 |
break; |
| 1599 |
case 0x96: |
| 1600 |
h->err = 1; |
| 1601 |
sms_log(h, 'N'); |
| 1602 |
sms_nextoutgoing (h); |
| 1603 |
break; |
| 1604 |
default: |
| 1605 |
h->omsg[0] = 0x92; |
| 1606 |
h->omsg[1] = 1; |
| 1607 |
h->omsg[2] = 3; |
| 1608 |
sms_messagetx(h); |
| 1609 |
break; |
| 1610 |
} |
| 1611 |
} |
| 1612 |
|
| 1613 |
static void sms_messagetx(sms_t * h) |
| 1614 |
{ |
| 1615 |
unsigned char c = 0, p; |
| 1616 |
int len = h->omsg[1] + 2; |
| 1617 |
|
| 1618 |
for (p = 0; p < len; p++) { |
| 1619 |
c += h->omsg[p]; |
| 1620 |
} |
| 1621 |
h->omsg[len] = 0 - c; |
| 1622 |
sms_debug(DIR_TX, h); |
| 1623 |
h->framenumber++; |
| 1624 |
h->obytep = 0; |
| 1625 |
h->obitp = 0; |
| 1626 |
if (h->protocol == 2) { |
| 1627 |
h->oseizure = 300; |
| 1628 |
h->obyte = 0; |
| 1629 |
if (h->omsg[0] == 0x7F) { |
| 1630 |
h->opause = 8 * h->opause_0; |
| 1631 |
} else { |
| 1632 |
h->opause = 400; |
| 1633 |
} |
| 1634 |
} else { |
| 1635 |
h->oseizure = 0; |
| 1636 |
h->obyte = 1; |
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
if (h->omsg[0] == 0x93) { |
| 1642 |
h->opause = 8 * h->opause_0; |
| 1643 |
} else { |
| 1644 |
h->opause = 200; |
| 1645 |
} |
| 1646 |
} |
| 1647 |
|
| 1648 |
h->osync = OSYNC_BITS; |
| 1649 |
h->obyten = len + 1; |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
static int sms_generate(struct ast_channel *chan, void *data, int len, int samples) |
| 1657 |
{ |
| 1658 |
struct ast_frame f = { 0 }; |
| 1659 |
#define MAXSAMPLES (800) |
| 1660 |
output_t *buf; |
| 1661 |
sms_t *h = data; |
| 1662 |
int i, res; |
| 1663 |
|
| 1664 |
if (samples > MAXSAMPLES) { |
| 1665 |
ast_log(LOG_WARNING, "Only doing %d samples (%d requested)\n", |
| 1666 |
MAXSAMPLES, samples); |
| 1667 |
samples = MAXSAMPLES; |
| 1668 |
} |
| 1669 |
len = samples * sizeof(*buf) + AST_FRIENDLY_OFFSET; |
| 1670 |
buf = ast_alloca(len); |
| 1671 |
|
| 1672 |
f.frametype = AST_FRAME_VOICE; |
| 1673 |
f.subclass.format = __OUT_FMT; |
| 1674 |
f.datalen = samples * sizeof(*buf); |
| 1675 |
f.offset = AST_FRIENDLY_OFFSET; |
| 1676 |
f.mallocd = 0; |
| 1677 |
f.data.ptr = buf; |
| 1678 |
f.samples = samples; |
| 1679 |
f.src = "app_sms"; |
| 1680 |
|
| 1681 |
for (i = 0; i < samples; i++) { |
| 1682 |
buf[i] = wave_out[0]; |
| 1683 |
|
| 1684 |
if (h->opause) { |
| 1685 |
h->opause--; |
| 1686 |
} else if (h->obyten || h->osync) { |
| 1687 |
buf[i] = wave_out[h->ophase]; |
| 1688 |
h->ophase += (h->obyte & 1) ? 13 : 21; |
| 1689 |
if (h->ophase >= 80) |
| 1690 |
h->ophase -= 80; |
| 1691 |
if ((h->ophasep += 12) >= 80) { |
| 1692 |
h->ophasep -= 80; |
| 1693 |
if (h->oseizure > 0) { |
| 1694 |
h->oseizure--; |
| 1695 |
h->obyte ^= 1; |
| 1696 |
} else if (h->osync) { |
| 1697 |
h->obyte = 1; |
| 1698 |
h->osync--; |
| 1699 |
if (h->osync == 0 && h->protocol == 2 && h->omsg[0] == DLL2_SMS_EST) { |
| 1700 |
h->obytep = h->obyten = 0; |
| 1701 |
} |
| 1702 |
} else { |
| 1703 |
h->obitp++; |
| 1704 |
if (h->obitp == 1) { |
| 1705 |
h->obyte = 0; |
| 1706 |
} else if (h->obitp == 2) { |
| 1707 |
h->obyte = h->omsg[h->obytep]; |
| 1708 |
} else if (h->obitp == 10) { |
| 1709 |
h->obyte = 1; |
| 1710 |
h->obitp = 0; |
| 1711 |
h->obytep++; |
| 1712 |
if (h->obytep == h->obyten) { |
| 1713 |
h->obytep = h->obyten = 0; |
| 1714 |
h->osync = 10; |
| 1715 |
} |
| 1716 |
} else { |
| 1717 |
h->obyte >>= 1; |
| 1718 |
} |
| 1719 |
} |
| 1720 |
} |
| 1721 |
} |
| 1722 |
} |
| 1723 |
res = ast_write(chan, &f); |
| 1724 |
ast_frfree(&f); |
| 1725 |
if (res < 0) { |
| 1726 |
ast_log(LOG_WARNING, "Failed to write frame to '%s': %s\n", ast_channel_name(chan), strerror(errno)); |
| 1727 |
return -1; |
| 1728 |
} |
| 1729 |
return 0; |
| 1730 |
#undef MAXSAMPLES |
| 1731 |
} |
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
static void *sms_alloc(struct ast_channel *chan, void *sms_t_ptr) |
| 1737 |
{ |
| 1738 |
return sms_t_ptr; |
| 1739 |
} |
| 1740 |
|
| 1741 |
static void sms_release(struct ast_channel *chan, void *data) |
| 1742 |
{ |
| 1743 |
return; |
| 1744 |
} |
| 1745 |
|
| 1746 |
static struct ast_generator smsgen = { |
| 1747 |
.alloc = sms_alloc, |
| 1748 |
.release = sms_release, |
| 1749 |
.generate = sms_generate, |
| 1750 |
}; |
| 1751 |
|
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
static void sms_process(sms_t * h, int samples, signed short *data) |
| 1765 |
{ |
| 1766 |
int bit; |
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
if (h->obyten || h->osync) { |
| 1776 |
return; |
| 1777 |
} |
| 1778 |
for ( ; samples-- ; data++) { |
| 1779 |
unsigned long long m0, m1; |
| 1780 |
if (abs(*data) > h->imag) { |
| 1781 |
h->imag = abs(*data); |
| 1782 |
} else { |
| 1783 |
h->imag = h->imag * 7 / 8; |
| 1784 |
} |
| 1785 |
if (h->imag <= 500) { |
| 1786 |
if (h->idle++ == 80000) { |
| 1787 |
ast_log(LOG_NOTICE, "No data, hanging up\n"); |
| 1788 |
h->hangup = 1; |
| 1789 |
h->err = 1; |
| 1790 |
} |
| 1791 |
if (h->ierr) { |
| 1792 |
ast_log(LOG_NOTICE, "Error %d, hanging up\n", h->ierr); |
| 1793 |
|
| 1794 |
h->err = 1; |
| 1795 |
h->omsg[0] = 0x92; |
| 1796 |
h->omsg[1] = 1; |
| 1797 |
h->omsg[2] = h->ierr; |
| 1798 |
sms_messagetx(h); |
| 1799 |
} |
| 1800 |
h->ierr = h->ibitn = h->ibytep = h->ibytec = 0; |
| 1801 |
continue; |
| 1802 |
} |
| 1803 |
h->idle = 0; |
| 1804 |
|
| 1805 |
|
| 1806 |
h->ims0 = (h->ims0 * 6 + *data * wave[h->ips0]) / 7; |
| 1807 |
h->imc0 = (h->imc0 * 6 + *data * wave[h->ipc0]) / 7; |
| 1808 |
h->ims1 = (h->ims1 * 6 + *data * wave[h->ips1]) / 7; |
| 1809 |
h->imc1 = (h->imc1 * 6 + *data * wave[h->ipc1]) / 7; |
| 1810 |
|
| 1811 |
m0 = h->ims0 * h->ims0 + h->imc0 * h->imc0; |
| 1812 |
m1 = h->ims1 * h->ims1 + h->imc1 * h->imc1; |
| 1813 |
|
| 1814 |
|
| 1815 |
if ((h->ips0 += 21) >= 80) { |
| 1816 |
h->ips0 -= 80; |
| 1817 |
} |
| 1818 |
if ((h->ipc0 += 21) >= 80) { |
| 1819 |
h->ipc0 -= 80; |
| 1820 |
} |
| 1821 |
if ((h->ips1 += 13) >= 80) { |
| 1822 |
h->ips1 -= 80; |
| 1823 |
} |
| 1824 |
if ((h->ipc1 += 13) >= 80) { |
| 1825 |
h->ipc1 -= 80; |
| 1826 |
} |
| 1827 |
|
| 1828 |
|
| 1829 |
h->ibith <<= 1; |
| 1830 |
if (m1 > m0) { |
| 1831 |
h->ibith |= 1; |
| 1832 |
} |
| 1833 |
if (h->ibith & 8) { |
| 1834 |
h->ibitt--; |
| 1835 |
} |
| 1836 |
if (h->ibith & 1) { |
| 1837 |
h->ibitt++; |
| 1838 |
} |
| 1839 |
bit = ((h->ibitt > 1) ? 1 : 0); |
| 1840 |
if (bit != h->ibitl) { |
| 1841 |
h->ibitc = 1; |
| 1842 |
} else { |
| 1843 |
h->ibitc++; |
| 1844 |
} |
| 1845 |
h->ibitl = bit; |
| 1846 |
if (!h->ibitn && h->ibitc == 4 && !bit) { |
| 1847 |
h->ibitn = 1; |
| 1848 |
h->iphasep = 0; |
| 1849 |
} |
| 1850 |
if (bit && h->ibitc == 200) { |
| 1851 |
|
| 1852 |
if (h->framenumber <= 0 && h->ibytec >= 160 && !memcmp(h->imsg, "UUUUUUUUUUUUUUUUUUUU", 20)) { |
| 1853 |
h->framenumber = 1; |
| 1854 |
ast_verb(3, "SMS protocol 2 detected\n"); |
| 1855 |
h->protocol = 2; |
| 1856 |
h->imsg[0] = 0xff; |
| 1857 |
h->imsg[1] = h->imsg[2] = 0x00; |
| 1858 |
h->ierr = h->ibitn = h->ibytep = h->ibytec = 0; |
| 1859 |
sms_messagerx(h); |
| 1860 |
} |
| 1861 |
h->ierr = h->ibitn = h->ibytep = h->ibytec = 0; |
| 1862 |
} |
| 1863 |
if (h->ibitn) { |
| 1864 |
h->iphasep += 12; |
| 1865 |
if (h->iphasep >= 80) { |
| 1866 |
h->iphasep -= 80; |
| 1867 |
if (h->ibitn++ == 9) { |
| 1868 |
if (!bit) { |
| 1869 |
if (h->sent_rel) { |
| 1870 |
h->hangup = 1; |
| 1871 |
} else { |
| 1872 |
ast_log(LOG_NOTICE, "Bad stop bit\n"); |
| 1873 |
h->ierr = 0xFF; |
| 1874 |
} |
| 1875 |
} else { |
| 1876 |
if (h->ibytep < sizeof(h->imsg)) { |
| 1877 |
h->imsg[h->ibytep] = h->ibytev; |
| 1878 |
h->ibytec += h->ibytev; |
| 1879 |
h->ibytep++; |
| 1880 |
} else if (h->ibytep == sizeof(h->imsg)) { |
| 1881 |
ast_log(LOG_NOTICE, "msg too large\n"); |
| 1882 |
h->ierr = 2; |
| 1883 |
} |
| 1884 |
if (h->ibytep > 1 && h->ibytep == 3 + h->imsg[1] && !h->ierr) { |
| 1885 |
if (!h->ibytec) { |
| 1886 |
sms_messagerx(h); |
| 1887 |
} else { |
| 1888 |
ast_log(LOG_NOTICE, "bad checksum\n"); |
| 1889 |
h->ierr = 1; |
| 1890 |
} |
| 1891 |
} |
| 1892 |
} |
| 1893 |
h->ibitn = 0; |
| 1894 |
} |
| 1895 |
h->ibytev = (h->ibytev >> 1) + (bit ? 0x80 : 0); |
| 1896 |
} |
| 1897 |
} |
| 1898 |
} |
| 1899 |
} |
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
enum sms_flags { |
| 1909 |
OPTION_BE_SMSC = (1 << 0), |
| 1910 |
OPTION_ANSWER = (1 << 1), |
| 1911 |
OPTION_TWO = (1 << 2), |
| 1912 |
OPTION_PAUSE = (1 << 3), |
| 1913 |
OPTION_SRR = (1 << 4), |
| 1914 |
OPTION_DCS = (1 << 5), |
| 1915 |
OPTIONS_NO_LOG = (1 << 6), |
| 1916 |
}; |
| 1917 |
|
| 1918 |
enum sms_opt_args { |
| 1919 |
OPTION_ARG_PAUSE = 0, |
| 1920 |
OPTION_ARG_ARRAY_SIZE |
| 1921 |
}; |
| 1922 |
|
| 1923 |
AST_APP_OPTIONS(sms_options, { |
| 1924 |
AST_APP_OPTION('s', OPTION_BE_SMSC), |
| 1925 |
AST_APP_OPTION('a', OPTION_ANSWER), |
| 1926 |
AST_APP_OPTION('t', OPTION_TWO), |
| 1927 |
AST_APP_OPTION('r', OPTION_SRR), |
| 1928 |
AST_APP_OPTION('o', OPTION_DCS), |
| 1929 |
AST_APP_OPTION('n', OPTIONS_NO_LOG), |
| 1930 |
AST_APP_OPTION_ARG('p', OPTION_PAUSE, OPTION_ARG_PAUSE), |
| 1931 |
} ); |
| 1932 |
|
| 1933 |
static int sms_exec(struct ast_channel *chan, const char *data) |
| 1934 |
{ |
| 1935 |
int res = -1; |
| 1936 |
sms_t h = { 0 }; |
| 1937 |
|
| 1938 |
struct ast_flags flags = { 0 }; |
| 1939 |
char *parse, *sms_opts[OPTION_ARG_ARRAY_SIZE] = { 0, }; |
| 1940 |
char *p; |
| 1941 |
AST_DECLARE_APP_ARGS(sms_args, |
| 1942 |
AST_APP_ARG(queue); |
| 1943 |
AST_APP_ARG(options); |
| 1944 |
AST_APP_ARG(addr); |
| 1945 |
AST_APP_ARG(body); |
| 1946 |
); |
| 1947 |
|
| 1948 |
if (!data) { |
| 1949 |
ast_log(LOG_ERROR, "Requires queue name at least\n"); |
| 1950 |
return -1; |
| 1951 |
} |
| 1952 |
|
| 1953 |
parse = ast_strdupa(data); |
| 1954 |
AST_STANDARD_APP_ARGS(sms_args, parse); |
| 1955 |
if (sms_args.argc > 1) { |
| 1956 |
ast_app_parse_options(sms_options, &flags, sms_opts, sms_args.options); |
| 1957 |
} |
| 1958 |
|
| 1959 |
ast_verb(1, "sms argc %u queue <%s> opts <%s> addr <%s> body <%s>\n", |
| 1960 |
sms_args.argc, S_OR(sms_args.queue, ""), |
| 1961 |
S_OR(sms_args.options, ""), |
| 1962 |
S_OR(sms_args.addr, ""), |
| 1963 |
S_OR(sms_args.body, "") ); |
| 1964 |
|
| 1965 |
h.ipc0 = h.ipc1 = 20; |
| 1966 |
h.dcs = 0xF1; |
| 1967 |
|
| 1968 |
ast_copy_string(h.cli, |
| 1969 |
S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""), |
| 1970 |
sizeof(h.cli)); |
| 1971 |
|
| 1972 |
if (ast_strlen_zero(sms_args.queue)) { |
| 1973 |
ast_log(LOG_ERROR, "Requires queue name\n"); |
| 1974 |
goto done; |
| 1975 |
} |
| 1976 |
if (strlen(sms_args.queue) >= sizeof(h.queue)) { |
| 1977 |
ast_log(LOG_ERROR, "Queue name too long\n"); |
| 1978 |
goto done; |
| 1979 |
} |
| 1980 |
ast_copy_string(h.queue, sms_args.queue, sizeof(h.queue)); |
| 1981 |
|
| 1982 |
for (p = h.queue; *p; p++) { |
| 1983 |
if (!isalnum(*p)) { |
| 1984 |
*p = '-'; |
| 1985 |
} |
| 1986 |
} |
| 1987 |
|
| 1988 |
h.smsc = ast_test_flag(&flags, OPTION_BE_SMSC); |
| 1989 |
h.protocol = ast_test_flag(&flags, OPTION_TWO) ? 2 : 1; |
| 1990 |
h.nolog = ast_test_flag(&flags, OPTIONS_NO_LOG) ? 1 : 0; |
| 1991 |
if (!ast_strlen_zero(sms_opts[OPTION_ARG_PAUSE])) { |
| 1992 |
h.opause_0 = atoi(sms_opts[OPTION_ARG_PAUSE]); |
| 1993 |
} |
| 1994 |
if (h.opause_0 < 25 || h.opause_0 > 2000) { |
| 1995 |
h.opause_0 = 300; |
| 1996 |
} |
| 1997 |
ast_verb(1, "initial delay %dms\n", h.opause_0); |
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
if (ast_test_flag(&flags, OPTION_SRR)) { |
| 2002 |
h.srr = 1; |
| 2003 |
} |
| 2004 |
if (ast_test_flag(&flags, OPTION_DCS)) { |
| 2005 |
h.dcs = 1; |
| 2006 |
} |
| 2007 |
#if 0 |
| 2008 |
case '1': |
| 2009 |
case '2': |
| 2010 |
case '3': |
| 2011 |
case '4': |
| 2012 |
case '5': |
| 2013 |
case '6': |
| 2014 |
case '7': |
| 2015 |
h.pid = 0x40 + (*d & 0xF); |
| 2016 |
break; |
| 2017 |
} |
| 2018 |
#endif |
| 2019 |
if (sms_args.argc > 2) { |
| 2020 |
unsigned char *up; |
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
h.scts = ast_tvnow(); |
| 2025 |
if (ast_strlen_zero(sms_args.addr) || strlen(sms_args.addr) >= sizeof(h.oa)) { |
| 2026 |
ast_log(LOG_ERROR, "Address too long %s\n", sms_args.addr); |
| 2027 |
goto done; |
| 2028 |
} |
| 2029 |
if (h.smsc) { |
| 2030 |
ast_copy_string(h.oa, sms_args.addr, sizeof(h.oa)); |
| 2031 |
} else { |
| 2032 |
ast_copy_string(h.da, sms_args.addr, sizeof(h.da)); |
| 2033 |
ast_copy_string(h.oa, h.cli, sizeof(h.oa)); |
| 2034 |
} |
| 2035 |
h.udl = 0; |
| 2036 |
if (ast_strlen_zero(sms_args.body)) { |
| 2037 |
ast_log(LOG_ERROR, "Missing body for %s\n", sms_args.addr); |
| 2038 |
goto done; |
| 2039 |
} |
| 2040 |
up = (unsigned char *)sms_args.body; |
| 2041 |
while (*up && h.udl < SMSLEN) { |
| 2042 |
h.ud[h.udl++] = utf8decode(&up); |
| 2043 |
} |
| 2044 |
if (is7bit(h.dcs) && packsms7(0, h.udhl, h.udh, h.udl, h.ud) < 0) { |
| 2045 |
ast_log(LOG_WARNING, "Invalid 7 bit GSM data\n"); |
| 2046 |
goto done; |
| 2047 |
} |
| 2048 |
if (is8bit(h.dcs) && packsms8(0, h.udhl, h.udh, h.udl, h.ud) < 0) { |
| 2049 |
ast_log(LOG_WARNING, "Invalid 8 bit data\n"); |
| 2050 |
goto done; |
| 2051 |
} |
| 2052 |
if (is16bit(h.dcs) && packsms16(0, h.udhl, h.udh, h.udl, h.ud) < 0) { |
| 2053 |
ast_log(LOG_WARNING, "Invalid 16 bit data\n"); |
| 2054 |
goto done; |
| 2055 |
} |
| 2056 |
h.rx = 0; |
| 2057 |
h.mr = -1; |
| 2058 |
sms_writefile(&h); |
| 2059 |
res = h.err; |
| 2060 |
goto done; |
| 2061 |
} |
| 2062 |
|
| 2063 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 2064 |
ast_answer(chan); |
| 2065 |
} |
| 2066 |
|
| 2067 |
if (ast_test_flag(&flags, OPTION_ANSWER)) { |
| 2068 |
h.framenumber = 1; |
| 2069 |
|
| 2070 |
if (h.protocol == 2) { |
| 2071 |
h.omsg[0] = DLL2_SMS_EST; |
| 2072 |
h.omsg[1] = 0; |
| 2073 |
} else { |
| 2074 |
h.omsg[0] = DLL1_SMS_EST | DLL1_SMS_COMPLETE; |
| 2075 |
h.omsg[1] = 0; |
| 2076 |
} |
| 2077 |
sms_messagetx(&h); |
| 2078 |
} |
| 2079 |
|
| 2080 |
res = ast_set_write_format(chan, __OUT_FMT); |
| 2081 |
if (res >= 0) { |
| 2082 |
res = ast_set_read_format(chan, ast_format_slin); |
| 2083 |
} |
| 2084 |
if (res < 0) { |
| 2085 |
ast_log(LOG_ERROR, "Unable to set to linear mode, giving up\n"); |
| 2086 |
goto done; |
| 2087 |
} |
| 2088 |
|
| 2089 |
if ( (res = ast_activate_generator(chan, &smsgen, &h)) < 0) { |
| 2090 |
ast_log(LOG_ERROR, "Failed to activate generator on '%s'\n", ast_channel_name(chan)); |
| 2091 |
goto done; |
| 2092 |
} |
| 2093 |
|
| 2094 |
|
| 2095 |
for (;;) { |
| 2096 |
struct ast_frame *f; |
| 2097 |
int i = ast_waitfor(chan, -1); |
| 2098 |
if (i < 0) { |
| 2099 |
ast_log(LOG_NOTICE, "waitfor failed\n"); |
| 2100 |
break; |
| 2101 |
} |
| 2102 |
if (h.hangup) { |
| 2103 |
ast_log(LOG_NOTICE, "channel hangup\n"); |
| 2104 |
break; |
| 2105 |
} |
| 2106 |
f = ast_read(chan); |
| 2107 |
if (!f) { |
| 2108 |
ast_log(LOG_NOTICE, "ast_read failed\n"); |
| 2109 |
break; |
| 2110 |
} |
| 2111 |
if (f->frametype == AST_FRAME_VOICE) { |
| 2112 |
sms_process(&h, f->samples, f->data.ptr); |
| 2113 |
} |
| 2114 |
|
| 2115 |
ast_frfree(f); |
| 2116 |
} |
| 2117 |
res = h.err; |
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
ast_deactivate_generator(chan); |
| 2124 |
|
| 2125 |
sms_log(&h, '?'); |
| 2126 |
done: |
| 2127 |
return (res); |
| 2128 |
} |
| 2129 |
|
| 2130 |
static int unload_module(void) |
| 2131 |
{ |
| 2132 |
return ast_unregister_application(app); |
| 2133 |
} |
| 2134 |
|
| 2135 |
static int load_module(void) |
| 2136 |
{ |
| 2137 |
#ifdef OUTALAW |
| 2138 |
int p; |
| 2139 |
for (p = 0; p < 80; p++) { |
| 2140 |
wavea[p] = AST_LIN2A(wave[p]); |
| 2141 |
} |
| 2142 |
#endif |
| 2143 |
snprintf(log_file, sizeof(log_file), "%s/sms", ast_config_AST_LOG_DIR); |
| 2144 |
return ast_register_application_xml(app, sms_exec); |
| 2145 |
} |
| 2146 |
|
| 2147 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "SMS/PSTN handler"); |