| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#include "asterisk.h" |
| 47 |
|
| 48 |
#include <math.h> |
| 49 |
#include <sys/wait.h> |
| 50 |
#include <sys/time.h> |
| 51 |
|
| 52 |
#include "asterisk/lock.h" |
| 53 |
#include "asterisk/file.h" |
| 54 |
#include "asterisk/channel.h" |
| 55 |
#include "asterisk/pbx.h" |
| 56 |
#include "asterisk/module.h" |
| 57 |
#include "asterisk/translate.h" |
| 58 |
#include "asterisk/app.h" |
| 59 |
#include "asterisk/dsp.h" |
| 60 |
#include "asterisk/config.h" |
| 61 |
#include "asterisk/localtime.h" |
| 62 |
#include "asterisk/callerid.h" |
| 63 |
#include "asterisk/astdb.h" |
| 64 |
#include "asterisk/utils.h" |
| 65 |
#include "asterisk/indications.h" |
| 66 |
#include "asterisk/format_cache.h" |
| 67 |
|
| 68 |
#define ALMRCV_CONFIG "alarmreceiver.conf" |
| 69 |
#define UNKNOWN_FORMAT "UNKNOWN_FORMAT" |
| 70 |
|
| 71 |
#define ADEMCO_CONTACT_ID "ADEMCO_CONTACT_ID" |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
#define ADEMCO_EXPRESS_4_1 "ADEMCO_EXPRESS_4_1" |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
#define ADEMCO_EXPRESS_4_2 "ADEMCO_EXPRESS_4_2" |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#define ADEMCO_HIGH_SPEED "ADEMCO_HIGH_SPEED" |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
#define ADEMCO_SUPER_FAST "ADEMCO_SUPER_FAST" |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
#define ADEMCO_MSG_TYPE_1 "18" |
| 122 |
#define ADEMCO_MSG_TYPE_2 "98" |
| 123 |
#define ADEMCO_MSG_TYPE_3 "17" |
| 124 |
#define ADEMCO_MSG_TYPE_4 "27" |
| 125 |
#define ADEMCO_MSG_TYPE_5 "55" |
| 126 |
#define ADEMCO_MSG_TYPE_6 "56" |
| 127 |
|
| 128 |
#define ADEMCO_AUDIO_CALL_NEXT "606" |
| 129 |
|
| 130 |
struct { |
| 131 |
char digit; |
| 132 |
char weight; |
| 133 |
} digits_mapping[] = { {'0', 10}, {'1', 1} , {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5}, |
| 134 |
{'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'*', 11}, {'#', 12}, |
| 135 |
{'A', 13}, {'B', 14}, {'C', 15} }; |
| 136 |
|
| 137 |
struct event_node{ |
| 138 |
char data[17]; |
| 139 |
struct event_node *next; |
| 140 |
}; |
| 141 |
|
| 142 |
typedef struct event_node event_node_t; |
| 143 |
|
| 144 |
struct timeval call_start_time; |
| 145 |
|
| 146 |
static const char app[] = "AlarmReceiver"; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static int fdtimeout = 2000; |
| 186 |
static int sdtimeout = 200; |
| 187 |
static int answait = 1250; |
| 188 |
static int toneloudness = 4096; |
| 189 |
static int log_individual_events = 0; |
| 190 |
static int no_group_meta = 0; |
| 191 |
static char event_spool_dir[128] = {'\0'}; |
| 192 |
static char event_app[128] = {'\0'}; |
| 193 |
static char db_family[128] = {'\0'}; |
| 194 |
static char time_stamp_format[128] = {"%a %b %d, %Y @ %H:%M:%S %Z"}; |
| 195 |
|
| 196 |
|
| 197 |
static char event_file[14] = "/event-XXXXXX"; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
static void database_increment(char *key) |
| 211 |
{ |
| 212 |
unsigned v; |
| 213 |
char value[16]; |
| 214 |
|
| 215 |
if (ast_strlen_zero(db_family)) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
if (ast_db_get(db_family, key, value, sizeof(value) - 1)) { |
| 220 |
ast_verb(4, "AlarmReceiver: Creating database entry %s and setting to 1\n", key); |
| 221 |
|
| 222 |
ast_db_put(db_family, key, "1"); |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
sscanf(value, "%30u", &v); |
| 227 |
v++; |
| 228 |
|
| 229 |
ast_verb(4, "AlarmReceiver: New value for %s: %u\n", key, v); |
| 230 |
snprintf(value, sizeof(value), "%u", v); |
| 231 |
|
| 232 |
if (ast_db_put(db_family, key, value)) { |
| 233 |
ast_verb(4, "AlarmReceiver: database_increment write error\n"); |
| 234 |
} |
| 235 |
|
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
static int receive_dtmf_digits(struct ast_channel *chan, char *digit_string, int buf_size, int expected, int *received) |
| 256 |
{ |
| 257 |
int rtn = 0; |
| 258 |
int r; |
| 259 |
struct ast_frame *f; |
| 260 |
struct timeval lastdigittime; |
| 261 |
|
| 262 |
lastdigittime = ast_tvnow(); |
| 263 |
while (*received < expected && *received < buf_size - 1) { |
| 264 |
|
| 265 |
if (ast_tvdiff_ms(ast_tvnow(), lastdigittime) > ((*received > 0) ? sdtimeout : fdtimeout)) { |
| 266 |
ast_verb(4, "AlarmReceiver: DTMF Digit Timeout on %s\n", ast_channel_name(chan)); |
| 267 |
ast_debug(1, "AlarmReceiver: DTMF timeout on chan %s\n", ast_channel_name(chan)); |
| 268 |
rtn = 1; |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
if ((r = ast_waitfor(chan, -1)) < 0) { |
| 273 |
ast_debug(1, "Waitfor returned %d\n", r); |
| 274 |
continue; |
| 275 |
} |
| 276 |
|
| 277 |
if ((f = ast_read(chan)) == NULL) { |
| 278 |
rtn = -1; |
| 279 |
break; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
if ((f->frametype == AST_FRAME_CONTROL) |
| 284 |
&& (f->subclass.integer == AST_CONTROL_HANGUP)) { |
| 285 |
if (f->data.uint32) { |
| 286 |
ast_channel_hangupcause_set(chan, f->data.uint32); |
| 287 |
} |
| 288 |
ast_frfree(f); |
| 289 |
rtn = -1; |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
if (f->frametype != AST_FRAME_DTMF) { |
| 295 |
ast_frfree(f); |
| 296 |
continue; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
digit_string[(*received)++] = f->subclass.integer; |
| 301 |
ast_frfree(f); |
| 302 |
|
| 303 |
lastdigittime = ast_tvnow(); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
digit_string[*received] = '\0'; |
| 308 |
|
| 309 |
return rtn; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
static int write_metadata(FILE *logfile, char *signalling_type, struct ast_channel *chan, int no_checksum) |
| 324 |
{ |
| 325 |
struct timeval t; |
| 326 |
struct ast_tm now; |
| 327 |
char *cl; |
| 328 |
char *cn; |
| 329 |
char workstring[80]; |
| 330 |
char timestamp[80]; |
| 331 |
|
| 332 |
|
| 333 |
ast_copy_string(workstring, |
| 334 |
S_COR(ast_channel_caller(chan)->id.number.valid, |
| 335 |
ast_channel_caller(chan)->id.number.str, ""), sizeof(workstring)); |
| 336 |
ast_shrink_phone_number(workstring); |
| 337 |
if (ast_strlen_zero(workstring)) { |
| 338 |
cl = "<unknown>"; |
| 339 |
} else { |
| 340 |
cl = workstring; |
| 341 |
} |
| 342 |
cn = S_COR(ast_channel_caller(chan)->id.name.valid, |
| 343 |
ast_channel_caller(chan)->id.name.str, "<unknown>"); |
| 344 |
|
| 345 |
|
| 346 |
t = ast_tvnow(); |
| 347 |
ast_localtime(&t, &now, NULL); |
| 348 |
|
| 349 |
|
| 350 |
ast_strftime(timestamp, sizeof(timestamp), time_stamp_format, &now); |
| 351 |
|
| 352 |
if (no_group_meta && fprintf(logfile, "PROTOCOL=%s\n" |
| 353 |
"CHECKSUM=%s\n" |
| 354 |
"CALLINGFROM=%s\n" |
| 355 |
"CALLERNAME=%s\n" |
| 356 |
"TIMESTAMP=%s\n\n", |
| 357 |
signalling_type, (!no_checksum) ? "yes" : "no", cl, cn, timestamp) > -1) { |
| 358 |
return 0; |
| 359 |
} else if (fprintf(logfile, "\n\n[metadata]\n\n" |
| 360 |
"PROTOCOL=%s\n" |
| 361 |
"CHECKSUM=%s\n" |
| 362 |
"CALLINGFROM=%s\n" |
| 363 |
"CALLERNAME=%s\n" |
| 364 |
"TIMESTAMP=%s\n\n" |
| 365 |
"[events]\n\n", |
| 366 |
signalling_type, (!no_checksum) ? "yes" : "no", cl, cn, timestamp) > -1) { |
| 367 |
return 0; |
| 368 |
} |
| 369 |
|
| 370 |
ast_verb(3, "AlarmReceiver: can't write metadata\n"); |
| 371 |
ast_debug(1, "AlarmReceiver: can't write metadata\n"); |
| 372 |
return -1; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static int write_event(FILE *logfile, event_node_t *event) |
| 385 |
{ |
| 386 |
if (fprintf(logfile, "%s%s\n", no_group_meta ? "event=" : "", event->data) < 0) { |
| 387 |
return -1; |
| 388 |
} |
| 389 |
|
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
static int log_events(struct ast_channel *chan, char *signalling_type, event_node_t *event, int no_checksum) |
| 405 |
{ |
| 406 |
char workstring[sizeof(event_spool_dir) + sizeof(event_file)] = ""; |
| 407 |
int fd; |
| 408 |
FILE *logfile; |
| 409 |
event_node_t *elp = event; |
| 410 |
|
| 411 |
if (!ast_strlen_zero(event_spool_dir)) { |
| 412 |
|
| 413 |
|
| 414 |
ast_copy_string(workstring, event_spool_dir, sizeof(workstring)); |
| 415 |
strncat(workstring, event_file, sizeof(workstring) - strlen(workstring) - 1); |
| 416 |
|
| 417 |
|
| 418 |
fd = mkstemp(workstring); |
| 419 |
|
| 420 |
if (fd == -1) { |
| 421 |
ast_verb(3, "AlarmReceiver: can't make temporary file\n"); |
| 422 |
ast_debug(1, "AlarmReceiver: can't make temporary file\n"); |
| 423 |
return -1; |
| 424 |
} |
| 425 |
|
| 426 |
if ((logfile = fdopen(fd, "w")) == NULL) { |
| 427 |
return -1; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
if (write_metadata(logfile, signalling_type, chan, no_checksum)) { |
| 432 |
fflush(logfile); |
| 433 |
fclose(logfile); |
| 434 |
return -1; |
| 435 |
} |
| 436 |
|
| 437 |
while ((elp != NULL) && (write_event(logfile, elp) == 0)) { |
| 438 |
elp = elp->next; |
| 439 |
} |
| 440 |
|
| 441 |
fflush(logfile); |
| 442 |
fclose(logfile); |
| 443 |
} |
| 444 |
|
| 445 |
return 0; |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
static int ademco_verify_checksum(char *event, int expected) |
| 459 |
{ |
| 460 |
int checksum = 0; |
| 461 |
int i, j; |
| 462 |
|
| 463 |
for (j = 0; j < expected; j++) { |
| 464 |
for (i = 0; i < ARRAY_LEN(digits_mapping); i++) { |
| 465 |
if (digits_mapping[i].digit == event[j]) { |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
if (i >= ARRAY_LEN(digits_mapping)) { |
| 471 |
ast_verb(2, "AlarmReceiver: Bad DTMF character %c, trying again\n", event[j]); |
| 472 |
return -1; |
| 473 |
} |
| 474 |
|
| 475 |
checksum += digits_mapping[i].weight; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
if (!(checksum % 15)) { |
| 480 |
return 0; |
| 481 |
} |
| 482 |
|
| 483 |
return -1; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
static int send_tone_burst(struct ast_channel *chan, const char *tone_freq, int tone_duration, int delay) |
| 499 |
{ |
| 500 |
if (delay && ast_safe_sleep(chan, delay)) { |
| 501 |
return -1; |
| 502 |
} |
| 503 |
|
| 504 |
if (ast_playtones_start(chan, toneloudness, tone_freq, 0)) { |
| 505 |
return -1; |
| 506 |
} |
| 507 |
|
| 508 |
if (ast_safe_sleep(chan, tone_duration)) { |
| 509 |
return -1; |
| 510 |
} |
| 511 |
|
| 512 |
ast_playtones_stop(chan); |
| 513 |
return 0; |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
static int ademco_check_valid(char *signalling_type, char *event) |
| 526 |
{ |
| 527 |
if (!strcmp(signalling_type, UNKNOWN_FORMAT)) { |
| 528 |
return 1; |
| 529 |
} |
| 530 |
|
| 531 |
if (!strcmp(signalling_type, ADEMCO_CONTACT_ID) |
| 532 |
&& strncmp(event + 4, ADEMCO_MSG_TYPE_1, 2) |
| 533 |
&& strncmp(event + 4, ADEMCO_MSG_TYPE_2, 2)) { |
| 534 |
return -1; |
| 535 |
} |
| 536 |
|
| 537 |
if (!strcmp(signalling_type, ADEMCO_EXPRESS_4_1) && strncmp(event + 4, ADEMCO_MSG_TYPE_3, 2)) { |
| 538 |
return -1; |
| 539 |
} |
| 540 |
|
| 541 |
if (!strcmp(signalling_type, ADEMCO_EXPRESS_4_2) && strncmp(event + 4, ADEMCO_MSG_TYPE_4, 2)) { |
| 542 |
return -1; |
| 543 |
} |
| 544 |
|
| 545 |
if (!strcmp(signalling_type, ADEMCO_HIGH_SPEED) && strncmp(event + 4, ADEMCO_MSG_TYPE_5, 2)) { |
| 546 |
return -1; |
| 547 |
} |
| 548 |
|
| 549 |
if (!strcmp(signalling_type, ADEMCO_SUPER_FAST) && strncmp(event + 4, ADEMCO_MSG_TYPE_6, 2)) { |
| 550 |
return -1; |
| 551 |
} |
| 552 |
|
| 553 |
return 0; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
static int ademco_detect_format(char *signalling_type, char *event, int *no_checksum) |
| 566 |
{ |
| 567 |
int res = 16; |
| 568 |
|
| 569 |
if (!strncmp(event + 4, ADEMCO_MSG_TYPE_1, 2) |
| 570 |
|| !strncmp(event + 4, ADEMCO_MSG_TYPE_2, 2)) { |
| 571 |
sprintf(signalling_type, "%s", ADEMCO_CONTACT_ID); |
| 572 |
} |
| 573 |
|
| 574 |
if (!strncmp(event + 4, ADEMCO_MSG_TYPE_3, 2)) { |
| 575 |
sprintf(signalling_type, "%s", ADEMCO_EXPRESS_4_1); |
| 576 |
res = 8; |
| 577 |
} |
| 578 |
|
| 579 |
if (!strncmp(event + 4, ADEMCO_MSG_TYPE_4, 2)) { |
| 580 |
sprintf(signalling_type, "%s", ADEMCO_EXPRESS_4_2); |
| 581 |
res = 9; |
| 582 |
} |
| 583 |
|
| 584 |
if (!strncmp(event + 4, ADEMCO_MSG_TYPE_5, 2)) { |
| 585 |
sprintf(signalling_type, "%s", ADEMCO_HIGH_SPEED); |
| 586 |
} |
| 587 |
|
| 588 |
if (!strncmp(event + 4, ADEMCO_MSG_TYPE_6, 2)) { |
| 589 |
sprintf(signalling_type, "%s", ADEMCO_SUPER_FAST); |
| 590 |
*no_checksum = 1; |
| 591 |
res = 15; |
| 592 |
} |
| 593 |
|
| 594 |
if (strcmp(signalling_type, UNKNOWN_FORMAT)) { |
| 595 |
ast_verb(4, "AlarmMonitoring: Detected format %s.\n", signalling_type); |
| 596 |
ast_debug(1, "AlarmMonitoring: Autodetected format %s.\n", signalling_type); |
| 597 |
} |
| 598 |
|
| 599 |
return res; |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
static int receive_ademco_event(struct ast_channel *chan, event_node_t **ehead, char *signalling_type, int *no_checksum) |
| 614 |
{ |
| 615 |
int res = 0; |
| 616 |
const char *limit; |
| 617 |
char event[17]; |
| 618 |
event_node_t *enew, *elp; |
| 619 |
int got_some_digits = 0; |
| 620 |
int events_received = 0; |
| 621 |
int ack_retries = 0; |
| 622 |
int limit_retries = 0; |
| 623 |
int expected_length = sizeof(event) - 1; |
| 624 |
|
| 625 |
database_increment("calls-received"); |
| 626 |
|
| 627 |
|
| 628 |
ast_verb(4, "AlarmReceiver: Waiting for first event from panel...\n"); |
| 629 |
|
| 630 |
while (res >= 0) { |
| 631 |
int digits_received = 0; |
| 632 |
|
| 633 |
res = 0; |
| 634 |
|
| 635 |
if (log_individual_events) { |
| 636 |
sprintf(signalling_type, "%s", UNKNOWN_FORMAT); |
| 637 |
expected_length = 16; |
| 638 |
*no_checksum = 0; |
| 639 |
} |
| 640 |
|
| 641 |
if (got_some_digits == 0) { |
| 642 |
|
| 643 |
ast_verb(4, "AlarmReceiver: Sending 1400Hz 100ms burst (ACK)\n"); |
| 644 |
res = send_tone_burst(chan, "1400", 100, 0); |
| 645 |
if (!res) { |
| 646 |
ast_verb(4, "AlarmReceiver: Sending 2300Hz 100ms burst (ACK)\n"); |
| 647 |
res = send_tone_burst(chan, "2300", 100, 100); |
| 648 |
} |
| 649 |
} |
| 650 |
if (res) { |
| 651 |
return -1; |
| 652 |
} |
| 653 |
|
| 654 |
res = receive_dtmf_digits(chan, event, sizeof(event), expected_length, &digits_received); |
| 655 |
if (res < 0) { |
| 656 |
if (events_received == 0) { |
| 657 |
|
| 658 |
database_increment("no-events-received"); |
| 659 |
ast_verb(4, "AlarmReceiver: No events received!\n"); |
| 660 |
} else { |
| 661 |
if (ack_retries) { |
| 662 |
database_increment("ack-retries"); |
| 663 |
ast_verb(4, "AlarmReceiver: ACK retries during this call: %d\n", ack_retries); |
| 664 |
} |
| 665 |
} |
| 666 |
ast_verb(4, "AlarmReceiver: App exiting...\n"); |
| 667 |
break; |
| 668 |
} |
| 669 |
|
| 670 |
if (!strcmp(signalling_type, UNKNOWN_FORMAT) && digits_received > 5) { |
| 671 |
expected_length = ademco_detect_format(signalling_type, event, no_checksum); |
| 672 |
|
| 673 |
if (res > 0) { |
| 674 |
if (digits_received == expected_length) { |
| 675 |
res = limit_retries = 0; |
| 676 |
} else if (digits_received == expected_length - 1 |
| 677 |
&& (!strcmp(signalling_type, ADEMCO_EXPRESS_4_2) |
| 678 |
|| !strcmp(signalling_type, ADEMCO_EXPRESS_4_1))) { |
| 679 |
|
| 680 |
res = limit_retries = 0; |
| 681 |
expected_length--; |
| 682 |
*no_checksum = 1; |
| 683 |
ast_verb(4, "AlarmMonitoring: Skipping checksum for format %s.\n", signalling_type); |
| 684 |
ast_debug(1, "AlarmMonitoring: Skipping checksum for format %s.\n", signalling_type); |
| 685 |
} |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
ast_channel_lock(chan); |
| 690 |
limit = pbx_builtin_getvar_helper(chan, "ALARMRECEIVER_CALL_LIMIT"); |
| 691 |
if (!ast_strlen_zero(limit)) { |
| 692 |
if (ast_tvdiff_ms(ast_tvnow(), call_start_time) > atoi(limit)) { |
| 693 |
ast_channel_unlock(chan); |
| 694 |
return -1; |
| 695 |
} |
| 696 |
} |
| 697 |
limit = pbx_builtin_getvar_helper(chan, "ALARMRECEIVER_RETRIES_LIMIT"); |
| 698 |
ast_channel_unlock(chan); |
| 699 |
if (!ast_strlen_zero(limit)) { |
| 700 |
if (limit_retries + 1 >= atoi(limit)) { |
| 701 |
return -1; |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
if (res) { |
| 706 |
|
| 707 |
ast_verb(2, "AlarmReceiver: Incomplete string: %s, trying again...\n", event); |
| 708 |
limit_retries++; |
| 709 |
|
| 710 |
if (!events_received && strcmp(signalling_type, UNKNOWN_FORMAT)) |
| 711 |
{ |
| 712 |
sprintf(signalling_type, "%s", UNKNOWN_FORMAT); |
| 713 |
expected_length = sizeof(event) - 1; |
| 714 |
} |
| 715 |
|
| 716 |
if (!got_some_digits) { |
| 717 |
got_some_digits = (!ast_strlen_zero(event)) ? 1 : 0; |
| 718 |
ack_retries++; |
| 719 |
} |
| 720 |
continue; |
| 721 |
} |
| 722 |
|
| 723 |
got_some_digits = 1; |
| 724 |
|
| 725 |
ast_verb(2, "AlarmReceiver: Received Event %s\n", event); |
| 726 |
ast_debug(1, "AlarmReceiver: Received event: %s\n", event); |
| 727 |
|
| 728 |
|
| 729 |
if (!(*no_checksum) && ademco_verify_checksum(event, expected_length)) { |
| 730 |
database_increment("checksum-errors"); |
| 731 |
ast_verb(2, "AlarmReceiver: Nonzero checksum\n"); |
| 732 |
ast_debug(1, "AlarmReceiver: Nonzero checksum\n"); |
| 733 |
continue; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
if (ademco_check_valid(signalling_type, event)) { |
| 738 |
database_increment("format-errors"); |
| 739 |
ast_verb(2, "AlarmReceiver: Wrong message type\n"); |
| 740 |
ast_debug(1, "AlarmReceiver: Wrong message type\n"); |
| 741 |
continue; |
| 742 |
} |
| 743 |
|
| 744 |
events_received++; |
| 745 |
|
| 746 |
|
| 747 |
if (!(enew = ast_calloc(1, sizeof(*enew)))) { |
| 748 |
return -1; |
| 749 |
} |
| 750 |
|
| 751 |
enew->next = NULL; |
| 752 |
ast_copy_string(enew->data, event, sizeof(enew->data)); |
| 753 |
|
| 754 |
|
| 755 |
if (*ehead == NULL) { |
| 756 |
*ehead = enew; |
| 757 |
} else { |
| 758 |
for (elp = *ehead; elp->next != NULL; elp = elp->next) { |
| 759 |
; |
| 760 |
} |
| 761 |
elp->next = enew; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
if (log_individual_events && log_events(chan, signalling_type, enew, *no_checksum)) { |
| 766 |
return -1; |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
if (send_tone_burst(chan, "1400", 900, 200)) { |
| 771 |
return -1; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
if (!strcmp(signalling_type, ADEMCO_CONTACT_ID) |
| 776 |
&& !strncmp(event + 7, ADEMCO_AUDIO_CALL_NEXT, 3)) { |
| 777 |
ast_verb(4, "AlarmReceiver: App exiting... Audio call next!\n"); |
| 778 |
return 0; |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
return res; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
static int alarmreceiver_exec(struct ast_channel *chan, const char *data) |
| 795 |
{ |
| 796 |
int res = 0; |
| 797 |
int no_checksum = 0; |
| 798 |
event_node_t *elp, *efree; |
| 799 |
char signalling_type[64] = ""; |
| 800 |
event_node_t *event_head = NULL; |
| 801 |
|
| 802 |
if ((ast_format_cmp(ast_channel_writeformat(chan), ast_format_ulaw) == AST_FORMAT_CMP_NOT_EQUAL) && |
| 803 |
(ast_format_cmp(ast_channel_writeformat(chan), ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL)) { |
| 804 |
ast_verb(4, "AlarmReceiver: Setting write format to Mu-law\n"); |
| 805 |
if (ast_set_write_format(chan, ast_format_ulaw)) { |
| 806 |
ast_log(LOG_WARNING, "AlarmReceiver: Unable to set write format to Mu-law on %s\n",ast_channel_name(chan)); |
| 807 |
return -1; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
if ((ast_format_cmp(ast_channel_readformat(chan), ast_format_ulaw) == AST_FORMAT_CMP_NOT_EQUAL) && |
| 812 |
(ast_format_cmp(ast_channel_readformat(chan), ast_format_alaw) == AST_FORMAT_CMP_NOT_EQUAL)) { |
| 813 |
ast_verb(4, "AlarmReceiver: Setting read format to Mu-law\n"); |
| 814 |
if (ast_set_read_format(chan, ast_format_ulaw)) { |
| 815 |
ast_log(LOG_WARNING, "AlarmReceiver: Unable to set read format to Mu-law on %s\n",ast_channel_name(chan)); |
| 816 |
return -1; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
ast_copy_string(signalling_type, UNKNOWN_FORMAT, sizeof(signalling_type)); |
| 822 |
call_start_time = ast_tvnow(); |
| 823 |
|
| 824 |
|
| 825 |
if (ast_channel_state(chan) != AST_STATE_UP) { |
| 826 |
ast_verb(4, "AlarmReceiver: Answering channel\n"); |
| 827 |
if (ast_answer(chan)) { |
| 828 |
return -1; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
ast_verb(4, "AlarmReceiver: Waiting for connection to stabilize\n"); |
| 834 |
if (ast_safe_sleep(chan, answait)) { |
| 835 |
return -1; |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
receive_ademco_event(chan, &event_head, signalling_type, &no_checksum); |
| 840 |
|
| 841 |
|
| 842 |
if (!log_individual_events) { |
| 843 |
res = log_events(chan, signalling_type, event_head, no_checksum); |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
if ((!res) && (!ast_strlen_zero(event_app)) && (event_head)) { |
| 848 |
ast_debug(1,"Alarmreceiver: executing: %s\n", event_app); |
| 849 |
ast_safe_system(event_app); |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
for (elp = event_head; (elp != NULL);) { |
| 854 |
efree = elp; |
| 855 |
elp = elp->next; |
| 856 |
ast_free(efree); |
| 857 |
} |
| 858 |
|
| 859 |
return 0; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
static int load_config(int reload) |
| 871 |
{ |
| 872 |
struct ast_config *cfg; |
| 873 |
const char *value; |
| 874 |
struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 }; |
| 875 |
|
| 876 |
|
| 877 |
cfg = ast_config_load(ALMRCV_CONFIG, config_flags); |
| 878 |
|
| 879 |
if (!cfg) { |
| 880 |
ast_verb(4, "AlarmReceiver: No config file\n"); |
| 881 |
return 0; |
| 882 |
} else if (cfg == CONFIG_STATUS_FILEUNCHANGED) { |
| 883 |
return 1; |
| 884 |
} else if (cfg == CONFIG_STATUS_FILEINVALID) { |
| 885 |
ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", |
| 886 |
ALMRCV_CONFIG); |
| 887 |
return 0; |
| 888 |
} |
| 889 |
|
| 890 |
if ((value = ast_variable_retrieve(cfg, "general", "eventcmd")) != NULL) { |
| 891 |
ast_copy_string(event_app, value, sizeof(event_app)); |
| 892 |
} |
| 893 |
|
| 894 |
if ((value = ast_variable_retrieve(cfg, "general", "loudness")) != NULL) { |
| 895 |
toneloudness = atoi(value); |
| 896 |
if (toneloudness < 100) { |
| 897 |
toneloudness = 100; |
| 898 |
} else if (toneloudness > 8192) { |
| 899 |
toneloudness = 8192; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
if ((value = ast_variable_retrieve(cfg, "general", "fdtimeout")) != NULL) { |
| 904 |
fdtimeout = atoi(value); |
| 905 |
if (fdtimeout < 1000) { |
| 906 |
fdtimeout = 1000; |
| 907 |
} else if (fdtimeout > 10000) { |
| 908 |
fdtimeout = 10000; |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
if ((value = ast_variable_retrieve(cfg, "general", "sdtimeout")) != NULL) { |
| 913 |
sdtimeout = atoi(value); |
| 914 |
if (sdtimeout < 110) { |
| 915 |
sdtimeout = 110; |
| 916 |
} else if (sdtimeout > 4000) { |
| 917 |
sdtimeout = 4000; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
if ((value = ast_variable_retrieve(cfg, "general", "answait")) != NULL) { |
| 922 |
answait = atoi(value); |
| 923 |
if (answait < 500) { |
| 924 |
answait = 500; |
| 925 |
} else if (answait > 10000) { |
| 926 |
answait = 10000; |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
if ((value = ast_variable_retrieve(cfg, "general", "no_group_meta")) != NULL) { |
| 931 |
no_group_meta = ast_true(value); |
| 932 |
} |
| 933 |
|
| 934 |
if ((value = ast_variable_retrieve(cfg, "general", "logindividualevents")) != NULL) { |
| 935 |
log_individual_events = ast_true(value); |
| 936 |
} |
| 937 |
|
| 938 |
if ((value = ast_variable_retrieve(cfg, "general", "eventspooldir")) != NULL) { |
| 939 |
ast_copy_string(event_spool_dir, value, sizeof(event_spool_dir)); |
| 940 |
} |
| 941 |
|
| 942 |
if ((value = ast_variable_retrieve(cfg, "general", "timestampformat")) != NULL) { |
| 943 |
ast_copy_string(time_stamp_format, value, sizeof(time_stamp_format)); |
| 944 |
} |
| 945 |
|
| 946 |
if ((value = ast_variable_retrieve(cfg, "general", "db-family")) != NULL) { |
| 947 |
ast_copy_string(db_family, value, sizeof(db_family)); |
| 948 |
} |
| 949 |
|
| 950 |
ast_config_destroy(cfg); |
| 951 |
|
| 952 |
return 1; |
| 953 |
} |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
static int unload_module(void) |
| 962 |
{ |
| 963 |
return ast_unregister_application(app); |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
static int load_module(void) |
| 977 |
{ |
| 978 |
if (load_config(0)) { |
| 979 |
if (ast_register_application_xml(app, alarmreceiver_exec)) { |
| 980 |
return AST_MODULE_LOAD_DECLINE; |
| 981 |
} |
| 982 |
return AST_MODULE_LOAD_SUCCESS; |
| 983 |
} |
| 984 |
|
| 985 |
return AST_MODULE_LOAD_DECLINE; |
| 986 |
} |
| 987 |
|
| 988 |
static int reload(void) |
| 989 |
{ |
| 990 |
if (load_config(1)) { |
| 991 |
return AST_MODULE_LOAD_SUCCESS; |
| 992 |
} |
| 993 |
|
| 994 |
return AST_MODULE_LOAD_DECLINE; |
| 995 |
} |
| 996 |
|
| 997 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Alarm Receiver for Asterisk", |
| 998 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 999 |
.load = load_module, |
| 1000 |
.unload = unload_module, |
| 1001 |
.reload = reload, |
| 1002 |
); |