| 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 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include <math.h> |
| 35 |
|
| 36 |
#include "asterisk/module.h" |
| 37 |
#include "asterisk/frame.h" |
| 38 |
#include "asterisk/format_cache.h" |
| 39 |
#include "asterisk/channel.h" |
| 40 |
#include "asterisk/dsp.h" |
| 41 |
#include "asterisk/pbx.h" |
| 42 |
#include "asterisk/stasis_channels.h" |
| 43 |
#include "asterisk/audiohook.h" |
| 44 |
#include "asterisk/app.h" |
| 45 |
#include "asterisk/indications.h" |
| 46 |
#include "asterisk/conversions.h" |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 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 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
struct detect_information { |
| 277 |
struct ast_dsp *dsp; |
| 278 |
struct ast_audiohook audiohook; |
| 279 |
int freq1; |
| 280 |
int freq2; |
| 281 |
int duration; |
| 282 |
int db; |
| 283 |
char *gototx; |
| 284 |
char *gotorx; |
| 285 |
unsigned int squelch:1; |
| 286 |
unsigned int tx:1; |
| 287 |
unsigned int rx:1; |
| 288 |
unsigned int autoend:1; |
| 289 |
unsigned int matchmet:1; |
| 290 |
int txcount; |
| 291 |
int rxcount; |
| 292 |
int hitsrequired; |
| 293 |
int signalfeatures; |
| 294 |
}; |
| 295 |
|
| 296 |
enum td_opts { |
| 297 |
OPT_TX = (1 << 1), |
| 298 |
OPT_RX = (1 << 2), |
| 299 |
OPT_END_FILTER = (1 << 3), |
| 300 |
OPT_GOTO_RX = (1 << 4), |
| 301 |
OPT_GOTO_TX = (1 << 5), |
| 302 |
OPT_DECIBEL = (1 << 6), |
| 303 |
OPT_SQUELCH = (1 << 7), |
| 304 |
OPT_HITS_REQ = (1 << 8), |
| 305 |
OPT_SIT = (1 << 9), |
| 306 |
OPT_BUSY = (1 << 10), |
| 307 |
OPT_DIALTONE = (1 << 11), |
| 308 |
OPT_RINGING = (1 << 12), |
| 309 |
OPT_AUTOEND = (1 << 13), |
| 310 |
}; |
| 311 |
|
| 312 |
enum { |
| 313 |
OPT_ARG_DECIBEL, |
| 314 |
OPT_ARG_GOTO_RX, |
| 315 |
OPT_ARG_GOTO_TX, |
| 316 |
OPT_ARG_HITS_REQ, |
| 317 |
|
| 318 |
OPT_ARG_ARRAY_SIZE, |
| 319 |
}; |
| 320 |
|
| 321 |
AST_APP_OPTIONS(td_opts, { |
| 322 |
AST_APP_OPTION('a', OPT_SIT), |
| 323 |
AST_APP_OPTION('b', OPT_BUSY), |
| 324 |
AST_APP_OPTION('c', OPT_DIALTONE), |
| 325 |
AST_APP_OPTION_ARG('d', OPT_DECIBEL, OPT_ARG_DECIBEL), |
| 326 |
AST_APP_OPTION('e', OPT_AUTOEND), |
| 327 |
AST_APP_OPTION_ARG('g', OPT_GOTO_RX, OPT_ARG_GOTO_RX), |
| 328 |
AST_APP_OPTION_ARG('h', OPT_GOTO_TX, OPT_ARG_GOTO_TX), |
| 329 |
AST_APP_OPTION_ARG('n', OPT_HITS_REQ, OPT_ARG_HITS_REQ), |
| 330 |
AST_APP_OPTION('p', OPT_RINGING), |
| 331 |
AST_APP_OPTION('s', OPT_SQUELCH), |
| 332 |
AST_APP_OPTION('t', OPT_TX), |
| 333 |
AST_APP_OPTION('r', OPT_RX), |
| 334 |
AST_APP_OPTION('x', OPT_END_FILTER), |
| 335 |
}); |
| 336 |
|
| 337 |
static void destroy_callback(void *data) |
| 338 |
{ |
| 339 |
struct detect_information *di = data; |
| 340 |
ast_dsp_free(di->dsp); |
| 341 |
if (di->gotorx) { |
| 342 |
ast_free(di->gotorx); |
| 343 |
} |
| 344 |
if (di->gototx) { |
| 345 |
ast_free(di->gototx); |
| 346 |
} |
| 347 |
ast_audiohook_lock(&di->audiohook); |
| 348 |
ast_audiohook_detach(&di->audiohook); |
| 349 |
ast_audiohook_unlock(&di->audiohook); |
| 350 |
ast_audiohook_destroy(&di->audiohook); |
| 351 |
ast_free(di); |
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
static const struct ast_datastore_info detect_datastore = { |
| 356 |
.type = "detect", |
| 357 |
.destroy = destroy_callback |
| 358 |
}; |
| 359 |
|
| 360 |
static int detect_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction) |
| 361 |
{ |
| 362 |
struct ast_datastore *datastore = NULL; |
| 363 |
struct detect_information *di = NULL; |
| 364 |
struct stasis_message *message; |
| 365 |
int match = 0; |
| 366 |
|
| 367 |
|
| 368 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE) { |
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) { |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
di = datastore->data; |
| 378 |
|
| 379 |
if (!frame || frame->frametype != AST_FRAME_VOICE) { |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
if (di->matchmet && di->autoend) { |
| 388 |
return 0; |
| 389 |
} |
| 390 |
|
| 391 |
if (!(direction == AST_AUDIOHOOK_DIRECTION_READ ? di->rx : di->tx)) { |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
frame = ast_frdup(frame); |
| 397 |
frame = ast_dsp_process(chan, di->dsp, frame); |
| 398 |
if (frame->frametype == AST_FRAME_DTMF) { |
| 399 |
char result = frame->subclass.integer; |
| 400 |
if (result == 'q') { |
| 401 |
int now; |
| 402 |
match = 1; |
| 403 |
if (direction == AST_AUDIOHOOK_DIRECTION_READ) { |
| 404 |
di->rxcount = di->rxcount + 1; |
| 405 |
now = di->rxcount; |
| 406 |
} else { |
| 407 |
di->txcount = di->txcount + 1; |
| 408 |
now = di->txcount; |
| 409 |
} |
| 410 |
ast_debug(1, "TONE_DETECT just got a hit (#%d in this direction, waiting for %d total)\n", now, di->hitsrequired); |
| 411 |
if (now >= di->hitsrequired) { |
| 412 |
message = ast_channel_blob_create_from_cache(ast_channel_uniqueid(chan), ast_channel_tone_detect(), NULL); |
| 413 |
|
| 414 |
if (!message) { |
| 415 |
ast_log(LOG_ERROR, "Unable to publish tone detected event for ARI on channel '%s'", ast_channel_name(chan)); |
| 416 |
} else { |
| 417 |
stasis_publish(ast_channel_topic(chan), message); |
| 418 |
ao2_ref(message, -1); |
| 419 |
} |
| 420 |
|
| 421 |
di->matchmet = 1; |
| 422 |
if (direction == AST_AUDIOHOOK_DIRECTION_READ && di->gotorx) { |
| 423 |
ast_async_parseable_goto(chan, di->gotorx); |
| 424 |
} else if (di->gototx) { |
| 425 |
ast_async_parseable_goto(chan, di->gototx); |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
if (di->signalfeatures && !match) { |
| 431 |
int tstate, tcount; |
| 432 |
tcount = ast_dsp_get_tcount(di->dsp); |
| 433 |
tstate = ast_dsp_get_tstate(di->dsp); |
| 434 |
if (tstate > 0) { |
| 435 |
ast_debug(3, "tcount: %d, tstate: %d\n", tcount, tstate); |
| 436 |
switch (tstate) { |
| 437 |
case DSP_TONE_STATE_RINGING: |
| 438 |
if (di->signalfeatures & DSP_PROGRESS_RINGING) { |
| 439 |
ast_debug(1, "Detected ringing on %s in %s direction\n", ast_channel_name(chan), |
| 440 |
direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write"); |
| 441 |
match = 1; |
| 442 |
} |
| 443 |
break; |
| 444 |
case DSP_TONE_STATE_DIALTONE: |
| 445 |
if (di->signalfeatures & DSP_FEATURE_WAITDIALTONE) { |
| 446 |
ast_debug(1, "Detected dial tone on %s in %s direction\n", ast_channel_name(chan), |
| 447 |
direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write"); |
| 448 |
match = 1; |
| 449 |
} |
| 450 |
break; |
| 451 |
case DSP_TONE_STATE_BUSY: |
| 452 |
if (di->signalfeatures & DSP_PROGRESS_BUSY) { |
| 453 |
ast_debug(1, "Detected busy tone on %s in %s direction\n", ast_channel_name(chan), |
| 454 |
direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write"); |
| 455 |
match = 1; |
| 456 |
} |
| 457 |
break; |
| 458 |
case DSP_TONE_STATE_SPECIAL3: |
| 459 |
if (di->signalfeatures & DSP_PROGRESS_CONGESTION) { |
| 460 |
ast_debug(1, "Detected SIT on %s in %s direction\n", ast_channel_name(chan), |
| 461 |
direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write"); |
| 462 |
match = 1; |
| 463 |
} |
| 464 |
break; |
| 465 |
default: |
| 466 |
break; |
| 467 |
} |
| 468 |
if (match) { |
| 469 |
if (direction == AST_AUDIOHOOK_DIRECTION_READ && di->gotorx) { |
| 470 |
ast_async_parseable_goto(chan, di->gotorx); |
| 471 |
} else if (di->gototx) { |
| 472 |
ast_async_parseable_goto(chan, di->gototx); |
| 473 |
} else { |
| 474 |
ast_debug(3, "Detected call progress signal in %s direction, but don't know where to go\n", |
| 475 |
direction == AST_AUDIOHOOK_DIRECTION_READ ? "read" : "write"); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
ast_frfree(frame); |
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
static int remove_detect(struct ast_channel *chan) |
| 486 |
{ |
| 487 |
struct ast_datastore *datastore = NULL; |
| 488 |
struct detect_information *data; |
| 489 |
SCOPED_CHANNELLOCK(chan_lock, chan); |
| 490 |
|
| 491 |
datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL); |
| 492 |
if (!datastore) { |
| 493 |
ast_log(AST_LOG_WARNING, "Cannot remove TONE_DETECT from %s: TONE_DETECT not currently enabled\n", |
| 494 |
ast_channel_name(chan)); |
| 495 |
return -1; |
| 496 |
} |
| 497 |
data = datastore->data; |
| 498 |
|
| 499 |
if (ast_audiohook_remove(chan, &data->audiohook)) { |
| 500 |
ast_log(AST_LOG_WARNING, "Failed to remove TONE_DETECT audiohook from channel %s\n", ast_channel_name(chan)); |
| 501 |
return -1; |
| 502 |
} |
| 503 |
|
| 504 |
if (ast_channel_datastore_remove(chan, datastore)) { |
| 505 |
ast_log(AST_LOG_WARNING, "Failed to remove TONE_DETECT datastore from channel %s\n", |
| 506 |
ast_channel_name(chan)); |
| 507 |
return -1; |
| 508 |
} |
| 509 |
ast_datastore_free(datastore); |
| 510 |
|
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
static int freq_parser(char *freqs, int *freq1, int *freq2) { |
| 515 |
char *f1, *f2, *f3; |
| 516 |
if (ast_strlen_zero(freqs)) { |
| 517 |
ast_log(LOG_ERROR, "No frequency specified\n"); |
| 518 |
return -1; |
| 519 |
} |
| 520 |
f3 = ast_strdupa(freqs); |
| 521 |
f1 = strsep(&f3, "+"); |
| 522 |
f2 = strsep(&f3, "+"); |
| 523 |
if (!ast_strlen_zero(f3)) { |
| 524 |
ast_log(LOG_WARNING, "Only up to 2 frequencies may be specified: %s\n", freqs); |
| 525 |
return -1; |
| 526 |
} |
| 527 |
if (ast_str_to_int(f1, freq1)) { |
| 528 |
ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f1); |
| 529 |
return -1; |
| 530 |
} |
| 531 |
if (*freq1 < 0) { |
| 532 |
ast_log(LOG_WARNING, "Sorry, no negative frequencies: %d\n", *freq1); |
| 533 |
return -1; |
| 534 |
} |
| 535 |
if (!ast_strlen_zero(f2)) { |
| 536 |
ast_log(LOG_WARNING, "Sorry, currently only 1 frequency is supported\n"); |
| 537 |
return -1; |
| 538 |
|
| 539 |
if (ast_str_to_int(f2, freq2)) { |
| 540 |
ast_log(LOG_WARNING, "Frequency must be an integer: %s\n", f2); |
| 541 |
return -1; |
| 542 |
} |
| 543 |
if (*freq2 < 1) { |
| 544 |
ast_log(LOG_WARNING, "Sorry, positive frequencies only: %d\n", *freq2); |
| 545 |
return -1; |
| 546 |
} |
| 547 |
} |
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
static char* goto_parser(struct ast_channel *chan, char *loc) { |
| 552 |
char *exten, *pri, *context, *parse; |
| 553 |
char *dest; |
| 554 |
int size; |
| 555 |
parse = ast_strdupa(loc); |
| 556 |
context = strsep(&parse, ","); |
| 557 |
exten = strsep(&parse, ","); |
| 558 |
pri = strsep(&parse, ","); |
| 559 |
if (!exten) { |
| 560 |
pri = context; |
| 561 |
exten = NULL; |
| 562 |
context = NULL; |
| 563 |
} else if (!pri) { |
| 564 |
pri = exten; |
| 565 |
exten = context; |
| 566 |
context = NULL; |
| 567 |
} |
| 568 |
ast_channel_lock(chan); |
| 569 |
if (ast_strlen_zero(exten)) { |
| 570 |
exten = ast_strdupa(ast_channel_exten(chan)); |
| 571 |
} |
| 572 |
if (ast_strlen_zero(context)) { |
| 573 |
context = ast_strdupa(ast_channel_context(chan)); |
| 574 |
} |
| 575 |
ast_channel_unlock(chan); |
| 576 |
|
| 577 |
|
| 578 |
size = strlen(context) + strlen(exten) + strlen(pri) + 3; |
| 579 |
dest = ast_malloc(size + 1); |
| 580 |
if (!dest) { |
| 581 |
ast_log(LOG_ERROR, "Failed to parse goto: %s,%s,%s\n", context, exten, pri); |
| 582 |
return NULL; |
| 583 |
} |
| 584 |
snprintf(dest, size, "%s,%s,%s", context, exten, pri); |
| 585 |
return dest; |
| 586 |
} |
| 587 |
|
| 588 |
static int detect_read(struct ast_channel *chan, const char *cmd, char *data, char *buffer, size_t buflen) |
| 589 |
{ |
| 590 |
struct ast_datastore *datastore = NULL; |
| 591 |
struct detect_information *di = NULL; |
| 592 |
|
| 593 |
if (!chan) { |
| 594 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 595 |
return -1; |
| 596 |
} |
| 597 |
|
| 598 |
ast_channel_lock(chan); |
| 599 |
if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) { |
| 600 |
ast_channel_unlock(chan); |
| 601 |
return -1; |
| 602 |
} else { |
| 603 |
ast_channel_unlock(chan); |
| 604 |
di = datastore->data; |
| 605 |
} |
| 606 |
|
| 607 |
if (strchr(data, 't')) { |
| 608 |
snprintf(buffer, buflen, "%d", di->txcount); |
| 609 |
} else if (strchr(data, 'r')) { |
| 610 |
snprintf(buffer, buflen, "%d", di->rxcount); |
| 611 |
} else { |
| 612 |
ast_log(LOG_WARNING, "Invalid direction: %s\n", data); |
| 613 |
} |
| 614 |
|
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
static int parse_signal_features(struct ast_flags *flags) |
| 619 |
{ |
| 620 |
int features = 0; |
| 621 |
|
| 622 |
if (ast_test_flag(flags, OPT_SIT)) { |
| 623 |
features |= DSP_PROGRESS_CONGESTION; |
| 624 |
} |
| 625 |
if (ast_test_flag(flags, OPT_BUSY)) { |
| 626 |
features |= DSP_PROGRESS_BUSY; |
| 627 |
} |
| 628 |
if (ast_test_flag(flags, OPT_DIALTONE)) { |
| 629 |
features |= DSP_FEATURE_WAITDIALTONE; |
| 630 |
} |
| 631 |
if (ast_test_flag(flags, OPT_RINGING)) { |
| 632 |
features |= DSP_PROGRESS_RINGING; |
| 633 |
} |
| 634 |
|
| 635 |
return features; |
| 636 |
} |
| 637 |
|
| 638 |
static int detect_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 639 |
{ |
| 640 |
char *parse; |
| 641 |
struct ast_datastore *datastore = NULL; |
| 642 |
struct detect_information *di = NULL; |
| 643 |
struct ast_flags flags = { 0 }; |
| 644 |
char *opt_args[OPT_ARG_ARRAY_SIZE]; |
| 645 |
struct ast_dsp *dsp; |
| 646 |
int freq1 = 0, freq2 = 0, duration = 500, db = 16, squelch = 0, hitsrequired = 1; |
| 647 |
int signalfeatures = 0; |
| 648 |
|
| 649 |
AST_DECLARE_APP_ARGS(args, |
| 650 |
AST_APP_ARG(freqs); |
| 651 |
AST_APP_ARG(duration); |
| 652 |
AST_APP_ARG(options); |
| 653 |
); |
| 654 |
|
| 655 |
if (!chan) { |
| 656 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 657 |
return -1; |
| 658 |
} |
| 659 |
parse = ast_strdupa(data); |
| 660 |
AST_STANDARD_APP_ARGS(args, parse); |
| 661 |
|
| 662 |
if (!ast_strlen_zero(args.options)) { |
| 663 |
ast_app_parse_options(td_opts, &flags, opt_args, args.options); |
| 664 |
} |
| 665 |
if (ast_test_flag(&flags, OPT_END_FILTER)) { |
| 666 |
return remove_detect(chan); |
| 667 |
} |
| 668 |
if (freq_parser(args.freqs, &freq1, &freq2)) { |
| 669 |
return -1; |
| 670 |
} |
| 671 |
if (!ast_strlen_zero(args.duration) && (ast_str_to_int(args.duration, &duration) || duration < 1)) { |
| 672 |
ast_log(LOG_WARNING, "Invalid duration: %s\n", args.duration); |
| 673 |
return -1; |
| 674 |
} |
| 675 |
if (ast_test_flag(&flags, OPT_HITS_REQ) && !ast_strlen_zero(opt_args[OPT_ARG_HITS_REQ])) { |
| 676 |
if ((ast_str_to_int(opt_args[OPT_ARG_HITS_REQ], &hitsrequired) || hitsrequired < 1)) { |
| 677 |
ast_log(LOG_WARNING, "Invalid number hits required: %s\n", opt_args[OPT_ARG_HITS_REQ]); |
| 678 |
return -1; |
| 679 |
} |
| 680 |
} |
| 681 |
if (ast_test_flag(&flags, OPT_DECIBEL) && !ast_strlen_zero(opt_args[OPT_ARG_DECIBEL])) { |
| 682 |
if ((ast_str_to_int(opt_args[OPT_ARG_DECIBEL], &db) || db < 1)) { |
| 683 |
ast_log(LOG_WARNING, "Invalid decibel level: %s\n", opt_args[OPT_ARG_DECIBEL]); |
| 684 |
return -1; |
| 685 |
} |
| 686 |
} |
| 687 |
signalfeatures = parse_signal_features(&flags); |
| 688 |
|
| 689 |
ast_channel_lock(chan); |
| 690 |
if (!(datastore = ast_channel_datastore_find(chan, &detect_datastore, NULL))) { |
| 691 |
if (!(datastore = ast_datastore_alloc(&detect_datastore, NULL))) { |
| 692 |
ast_channel_unlock(chan); |
| 693 |
return 0; |
| 694 |
} |
| 695 |
if (!(di = ast_calloc(1, sizeof(*di)))) { |
| 696 |
ast_datastore_free(datastore); |
| 697 |
ast_channel_unlock(chan); |
| 698 |
return 0; |
| 699 |
} |
| 700 |
ast_audiohook_init(&di->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "Tone Detector", AST_AUDIOHOOK_MANIPULATE_ALL_RATES); |
| 701 |
di->audiohook.manipulate_callback = detect_callback; |
| 702 |
if (!(dsp = ast_dsp_new())) { |
| 703 |
ast_datastore_free(datastore); |
| 704 |
ast_channel_unlock(chan); |
| 705 |
ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); |
| 706 |
return -1; |
| 707 |
} |
| 708 |
di->signalfeatures = signalfeatures; |
| 709 |
if (freq1 > 0) { |
| 710 |
signalfeatures |= DSP_FEATURE_FREQ_DETECT; |
| 711 |
ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch); |
| 712 |
} |
| 713 |
ast_dsp_set_features(dsp, signalfeatures); |
| 714 |
di->dsp = dsp; |
| 715 |
di->txcount = 0; |
| 716 |
di->rxcount = 0; |
| 717 |
ast_debug(1, "Keeping our ears open for %s Hz, %d db\n", args.freqs, db); |
| 718 |
datastore->data = di; |
| 719 |
ast_channel_datastore_add(chan, datastore); |
| 720 |
ast_audiohook_attach(chan, &di->audiohook); |
| 721 |
} else { |
| 722 |
di = datastore->data; |
| 723 |
dsp = di->dsp; |
| 724 |
di->signalfeatures = signalfeatures; |
| 725 |
if (freq1 > 0) { |
| 726 |
signalfeatures |= DSP_FEATURE_FREQ_DETECT; |
| 727 |
ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch); |
| 728 |
} |
| 729 |
ast_dsp_set_features(dsp, signalfeatures); |
| 730 |
} |
| 731 |
di->duration = duration; |
| 732 |
di->gotorx = NULL; |
| 733 |
di->gototx = NULL; |
| 734 |
|
| 735 |
if (ast_test_flag(&flags, OPT_GOTO_RX) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO_RX])) { |
| 736 |
di->gotorx = goto_parser(chan, opt_args[OPT_ARG_GOTO_RX]); |
| 737 |
} |
| 738 |
if (ast_test_flag(&flags, OPT_GOTO_TX) && !ast_strlen_zero(opt_args[OPT_ARG_GOTO_TX])) { |
| 739 |
di->gototx = goto_parser(chan, opt_args[OPT_ARG_GOTO_TX]); |
| 740 |
} |
| 741 |
di->db = db; |
| 742 |
di->hitsrequired = hitsrequired; |
| 743 |
di->squelch = ast_test_flag(&flags, OPT_SQUELCH) ? 1 : 0; |
| 744 |
di->autoend = ast_test_flag(&flags, OPT_AUTOEND) ? 1 : 0; |
| 745 |
di->tx = 1; |
| 746 |
di->rx = 1; |
| 747 |
if (ast_strlen_zero(args.options) || ast_test_flag(&flags, OPT_TX)) { |
| 748 |
di->tx = 1; |
| 749 |
di->rx = 0; |
| 750 |
} |
| 751 |
if (ast_strlen_zero(args.options) || ast_test_flag(&flags, OPT_RX)) { |
| 752 |
di->rx = 1; |
| 753 |
di->tx = 0; |
| 754 |
} |
| 755 |
ast_channel_unlock(chan); |
| 756 |
|
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
enum { |
| 761 |
OPT_APP_DECIBEL = (1 << 0), |
| 762 |
OPT_APP_SQUELCH = (1 << 1), |
| 763 |
}; |
| 764 |
|
| 765 |
enum { |
| 766 |
OPT_APP_ARG_DECIBEL, |
| 767 |
|
| 768 |
OPT_APP_ARG_ARRAY_SIZE, |
| 769 |
}; |
| 770 |
|
| 771 |
AST_APP_OPTIONS(wait_exec_options, BEGIN_OPTIONS |
| 772 |
AST_APP_OPTION_ARG('d', OPT_APP_DECIBEL, OPT_APP_ARG_DECIBEL), |
| 773 |
AST_APP_OPTION('s', OPT_APP_SQUELCH), |
| 774 |
END_OPTIONS); |
| 775 |
|
| 776 |
static int wait_exec(struct ast_channel *chan, const char *data) |
| 777 |
{ |
| 778 |
char *appdata; |
| 779 |
struct ast_flags flags = {0}; |
| 780 |
char *opt_args[OPT_APP_ARG_ARRAY_SIZE]; |
| 781 |
double timeoutf = 0; |
| 782 |
int freq1 = 0, freq2 = 0, timeout = 0, duration = 500, times = 1, db = 16, squelch = 0; |
| 783 |
struct ast_frame *frame = NULL; |
| 784 |
struct ast_dsp *dsp; |
| 785 |
struct timeval start; |
| 786 |
int remaining_time = 0; |
| 787 |
int hits = 0; |
| 788 |
AST_DECLARE_APP_ARGS(args, |
| 789 |
AST_APP_ARG(freqs); |
| 790 |
AST_APP_ARG(duration); |
| 791 |
AST_APP_ARG(timeout); |
| 792 |
AST_APP_ARG(times); |
| 793 |
AST_APP_ARG(options); |
| 794 |
); |
| 795 |
|
| 796 |
appdata = ast_strdupa(data); |
| 797 |
AST_STANDARD_APP_ARGS(args, appdata); |
| 798 |
|
| 799 |
if (!ast_strlen_zero(args.options)) { |
| 800 |
ast_app_parse_options(wait_exec_options, &flags, opt_args, args.options); |
| 801 |
} |
| 802 |
if (freq_parser(args.freqs, &freq1, &freq2)) { |
| 803 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR"); |
| 804 |
return -1; |
| 805 |
} |
| 806 |
if (!ast_strlen_zero(args.timeout) && (sscanf(args.timeout, "%30lf", &timeoutf) != 1 || timeoutf < 0)) { |
| 807 |
ast_log(LOG_WARNING, "Invalid timeout: %s\n", args.timeout); |
| 808 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR"); |
| 809 |
return -1; |
| 810 |
} |
| 811 |
timeout = 1000 * timeoutf; |
| 812 |
if (!ast_strlen_zero(args.duration) && (ast_str_to_int(args.duration, &duration) || duration < 1)) { |
| 813 |
ast_log(LOG_WARNING, "Invalid duration: %s\n", args.duration); |
| 814 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR"); |
| 815 |
return -1; |
| 816 |
} |
| 817 |
if (!ast_strlen_zero(args.times) && (ast_str_to_int(args.times, ×) || times < 1)) { |
| 818 |
ast_log(LOG_WARNING, "Invalid number of times: %s\n", args.times); |
| 819 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR"); |
| 820 |
return -1; |
| 821 |
} |
| 822 |
if (ast_test_flag(&flags, OPT_APP_DECIBEL) && !ast_strlen_zero(opt_args[OPT_APP_ARG_DECIBEL])) { |
| 823 |
if ((ast_str_to_int(opt_args[OPT_APP_ARG_DECIBEL], &db) || db < 1)) { |
| 824 |
ast_log(LOG_WARNING, "Invalid decibel level: %s\n", opt_args[OPT_APP_ARG_DECIBEL]); |
| 825 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR"); |
| 826 |
return -1; |
| 827 |
} |
| 828 |
} |
| 829 |
squelch = ast_test_flag(&flags, OPT_APP_SQUELCH); |
| 830 |
if (!(dsp = ast_dsp_new())) { |
| 831 |
ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); |
| 832 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "ERROR"); |
| 833 |
return -1; |
| 834 |
} |
| 835 |
ast_dsp_set_features(dsp, DSP_FEATURE_FREQ_DETECT); |
| 836 |
ast_dsp_set_freqmode(dsp, freq1, duration, db, squelch); |
| 837 |
ast_debug(1, "Waiting for %s Hz, %d time(s), timeout %d ms, %d db\n", args.freqs, times, timeout, db); |
| 838 |
start = ast_tvnow(); |
| 839 |
do { |
| 840 |
if (timeout > 0) { |
| 841 |
remaining_time = ast_remaining_ms(start, timeout); |
| 842 |
if (remaining_time <= 0) { |
| 843 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "TIMEOUT"); |
| 844 |
break; |
| 845 |
} |
| 846 |
} |
| 847 |
if (ast_waitfor(chan, 1000) > 0) { |
| 848 |
if (!(frame = ast_read(chan))) { |
| 849 |
ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan)); |
| 850 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "HANGUP"); |
| 851 |
break; |
| 852 |
} else if (frame->frametype == AST_FRAME_VOICE) { |
| 853 |
frame = ast_dsp_process(chan, dsp, frame); |
| 854 |
if (frame->frametype == AST_FRAME_DTMF) { |
| 855 |
char result = frame->subclass.integer; |
| 856 |
if (result == 'q') { |
| 857 |
hits++; |
| 858 |
ast_debug(1, "We just detected %s Hz (hit #%d)\n", args.freqs, hits); |
| 859 |
if (hits >= times) { |
| 860 |
ast_frfree(frame); |
| 861 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "SUCCESS"); |
| 862 |
break; |
| 863 |
} |
| 864 |
} |
| 865 |
} |
| 866 |
} |
| 867 |
ast_frfree(frame); |
| 868 |
} else { |
| 869 |
pbx_builtin_setvar_helper(chan, "WAITFORTONESTATUS", "HANGUP"); |
| 870 |
} |
| 871 |
} while (timeout == 0 || remaining_time > 0); |
| 872 |
ast_dsp_free(dsp); |
| 873 |
|
| 874 |
return 0; |
| 875 |
} |
| 876 |
|
| 877 |
static char *waitapp = "WaitForTone"; |
| 878 |
static char *scanapp = "ToneScan"; |
| 879 |
|
| 880 |
static int scan_exec(struct ast_channel *chan, const char *data) |
| 881 |
{ |
| 882 |
char *appdata; |
| 883 |
double timeoutf = 0; |
| 884 |
int timeout = 0; |
| 885 |
struct ast_frame *frame = NULL, *frame2 = NULL; |
| 886 |
struct ast_dsp *dsp = NULL, *dsp2 = NULL; |
| 887 |
struct timeval start; |
| 888 |
int remaining_time = 0; |
| 889 |
int features, match = 0, fax = 0, voice = 0, threshold = 1; |
| 890 |
AST_DECLARE_APP_ARGS(args, |
| 891 |
AST_APP_ARG(zone); |
| 892 |
AST_APP_ARG(timeout); |
| 893 |
AST_APP_ARG(threshold); |
| 894 |
AST_APP_ARG(options); |
| 895 |
); |
| 896 |
|
| 897 |
appdata = ast_strdupa(data); |
| 898 |
AST_STANDARD_APP_ARGS(args, appdata); |
| 899 |
|
| 900 |
if (!ast_strlen_zero(args.timeout) && (sscanf(args.timeout, "%30lf", &timeoutf) != 1 || timeout < 0)) { |
| 901 |
ast_log(LOG_WARNING, "Invalid timeout: %s\n", args.timeout); |
| 902 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR"); |
| 903 |
return -1; |
| 904 |
} |
| 905 |
if (!ast_strlen_zero(args.threshold) && (ast_str_to_int(args.threshold, &threshold) || threshold < 1)) { |
| 906 |
ast_log(LOG_WARNING, "Invalid threshold: %s\n", args.threshold); |
| 907 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR"); |
| 908 |
return -1; |
| 909 |
} |
| 910 |
timeout = 1000 * timeoutf; |
| 911 |
|
| 912 |
if (!ast_strlen_zero(args.options) && strchr(args.options, 'f')) { |
| 913 |
fax = 1; |
| 914 |
} |
| 915 |
if (!ast_strlen_zero(args.options) && strchr(args.options, 'v')) { |
| 916 |
voice = 1; |
| 917 |
} |
| 918 |
|
| 919 |
if (!(dsp = ast_dsp_new())) { |
| 920 |
ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); |
| 921 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR"); |
| 922 |
return -1; |
| 923 |
} |
| 924 |
|
| 925 |
if (!ast_strlen_zero(args.zone)) { |
| 926 |
if (ast_dsp_set_call_progress_zone(dsp, args.zone)) { |
| 927 |
ast_log(LOG_WARNING, "Invalid call progress zone: %s\n", args.zone); |
| 928 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR"); |
| 929 |
ast_dsp_free(dsp); |
| 930 |
return -1; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
if (fax) { |
| 935 |
if (!(dsp2 = ast_dsp_new())) { |
| 936 |
ast_dsp_free(dsp); |
| 937 |
ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); |
| 938 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "ERROR"); |
| 939 |
return -1; |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
features = DSP_PROGRESS_RINGING; |
| 944 |
features |= DSP_PROGRESS_BUSY; |
| 945 |
features |= DSP_PROGRESS_CONGESTION; |
| 946 |
features |= DSP_PROGRESS_TALK; |
| 947 |
features |= DSP_FEATURE_WAITDIALTONE; |
| 948 |
features |= DSP_FEATURE_FREQ_DETECT; |
| 949 |
if (voice) { |
| 950 |
features |= DSP_TONE_STATE_TALKING; |
| 951 |
} |
| 952 |
ast_dsp_set_features(dsp, features); |
| 953 |
|
| 954 |
ast_dsp_set_freqmode(dsp, 2225, 400, 16, 0); |
| 955 |
|
| 956 |
if (fax) { |
| 957 |
ast_dsp_set_features(dsp2, DSP_FEATURE_FAX_DETECT); |
| 958 |
ast_dsp_set_faxmode(dsp2, DSP_FAXMODE_DETECT_CED); |
| 959 |
} |
| 960 |
|
| 961 |
ast_debug(1, "Starting tone scan, timeout: %d ms, threshold: %d\n", timeout, threshold); |
| 962 |
start = ast_tvnow(); |
| 963 |
do { |
| 964 |
if (timeout > 0) { |
| 965 |
remaining_time = ast_remaining_ms(start, timeout); |
| 966 |
if (remaining_time <= 0) { |
| 967 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "TIMEOUT"); |
| 968 |
break; |
| 969 |
} |
| 970 |
} |
| 971 |
if (ast_waitfor(chan, 1000) > 0) { |
| 972 |
if (!(frame = ast_read(chan))) { |
| 973 |
ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan)); |
| 974 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "HANGUP"); |
| 975 |
break; |
| 976 |
} else if (frame->frametype == AST_FRAME_VOICE) { |
| 977 |
if (fax) { |
| 978 |
frame2 = ast_frdup(frame); |
| 979 |
} |
| 980 |
frame = ast_dsp_process(chan, dsp, frame); |
| 981 |
if (frame->frametype == AST_FRAME_DTMF) { |
| 982 |
char result = frame->subclass.integer; |
| 983 |
match = 1; |
| 984 |
if (result == 'q') { |
| 985 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "MODEM"); |
| 986 |
} else { |
| 987 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "DTMF"); |
| 988 |
} |
| 989 |
} else if (fax) { |
| 990 |
char result; |
| 991 |
frame2 = ast_dsp_process(chan, dsp2, frame2); |
| 992 |
result = frame2->subclass.integer; |
| 993 |
if (frame2->frametype == AST_FRAME_DTMF) { |
| 994 |
if (result == 'e') { |
| 995 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "FAX"); |
| 996 |
match = 1; |
| 997 |
} else { |
| 998 |
ast_debug(1, "Ignoring inactionable event\n"); |
| 999 |
} |
| 1000 |
} |
| 1001 |
ast_frfree(frame2); |
| 1002 |
} |
| 1003 |
if (!match) { |
| 1004 |
int tstate, tcount; |
| 1005 |
tcount = ast_dsp_get_tcount(dsp); |
| 1006 |
tstate = ast_dsp_get_tstate(dsp); |
| 1007 |
if (tstate > 0) { |
| 1008 |
ast_debug(3, "tcount: %d, tstate: %d\n", tcount, tstate); |
| 1009 |
if (tcount >= threshold) { |
| 1010 |
match = 1; |
| 1011 |
switch (tstate) { |
| 1012 |
case DSP_TONE_STATE_RINGING: |
| 1013 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "RINGING"); |
| 1014 |
break; |
| 1015 |
case DSP_TONE_STATE_DIALTONE: |
| 1016 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "DIALTONE"); |
| 1017 |
break; |
| 1018 |
case DSP_TONE_STATE_TALKING: |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
if (voice && tcount > 15 && tcount >= threshold) { |
| 1023 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "VOICE"); |
| 1024 |
} else { |
| 1025 |
match = 0; |
| 1026 |
} |
| 1027 |
break; |
| 1028 |
case DSP_TONE_STATE_BUSY: |
| 1029 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "BUSY"); |
| 1030 |
break; |
| 1031 |
case DSP_TONE_STATE_SPECIAL3: |
| 1032 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "SIT"); |
| 1033 |
break; |
| 1034 |
case DSP_TONE_STATE_HUNGUP: |
| 1035 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "NUT"); |
| 1036 |
break; |
| 1037 |
default: |
| 1038 |
match = 0; |
| 1039 |
ast_debug(1, "Something else we weren't expecting? tstate: %d, #%d\n", tstate, tcount); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
ast_frfree(frame); |
| 1046 |
} else { |
| 1047 |
pbx_builtin_setvar_helper(chan, "TONESCANSTATUS", "HANGUP"); |
| 1048 |
} |
| 1049 |
} while (!match && (timeout == 0 || remaining_time > 0)); |
| 1050 |
ast_dsp_free(dsp); |
| 1051 |
if (dsp2) { |
| 1052 |
ast_dsp_free(dsp2); |
| 1053 |
} |
| 1054 |
|
| 1055 |
return 0; |
| 1056 |
} |
| 1057 |
|
| 1058 |
static struct ast_custom_function detect_function = { |
| 1059 |
.name = "TONE_DETECT", |
| 1060 |
.read = detect_read, |
| 1061 |
.write = detect_write, |
| 1062 |
}; |
| 1063 |
|
| 1064 |
static int unload_module(void) |
| 1065 |
{ |
| 1066 |
int res; |
| 1067 |
|
| 1068 |
res = ast_unregister_application(waitapp); |
| 1069 |
res |= ast_unregister_application(scanapp); |
| 1070 |
res |= ast_custom_function_unregister(&detect_function); |
| 1071 |
|
| 1072 |
return res; |
| 1073 |
} |
| 1074 |
|
| 1075 |
static int load_module(void) |
| 1076 |
{ |
| 1077 |
int res; |
| 1078 |
|
| 1079 |
res = ast_register_application_xml(waitapp, wait_exec); |
| 1080 |
res |= ast_register_application_xml(scanapp, scan_exec); |
| 1081 |
res |= ast_custom_function_register(&detect_function); |
| 1082 |
|
| 1083 |
return res; |
| 1084 |
} |
| 1085 |
|
| 1086 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Tone detection module"); |