| 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 "asterisk/file.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/channel.h" |
| 37 |
#include "asterisk/dsp.h" |
| 38 |
#include "asterisk/app.h" |
| 39 |
#include "asterisk/module.h" |
| 40 |
#include "asterisk/indications.h" |
| 41 |
#include "asterisk/conversions.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
enum read_option_flags { |
| 189 |
OPT_DELAY = (1 << 0), |
| 190 |
OPT_MUTE = (1 << 1), |
| 191 |
OPT_QUELCH = (1 << 2), |
| 192 |
OPT_RELAXED = (1 << 3), |
| 193 |
OPT_LAX_KP = (1 << 4), |
| 194 |
OPT_PROCESS = (1 << 5), |
| 195 |
OPT_NO_KP = (1 << 6), |
| 196 |
OPT_NO_ST = (1 << 7), |
| 197 |
OPT_KP_OVERRIDE = (1 << 8), |
| 198 |
OPT_MAXDIGITS = (1 << 9), |
| 199 |
}; |
| 200 |
|
| 201 |
enum { |
| 202 |
OPT_ARG_MAXDIGITS, |
| 203 |
|
| 204 |
OPT_ARG_ARRAY_SIZE, |
| 205 |
}; |
| 206 |
|
| 207 |
AST_APP_OPTIONS(read_app_options, { |
| 208 |
AST_APP_OPTION('d', OPT_DELAY), |
| 209 |
AST_APP_OPTION('l', OPT_LAX_KP), |
| 210 |
AST_APP_OPTION('k', OPT_NO_KP), |
| 211 |
AST_APP_OPTION('m', OPT_MUTE), |
| 212 |
AST_APP_OPTION_ARG('n', OPT_MAXDIGITS, OPT_ARG_MAXDIGITS), |
| 213 |
AST_APP_OPTION('o', OPT_KP_OVERRIDE), |
| 214 |
AST_APP_OPTION('p', OPT_PROCESS), |
| 215 |
AST_APP_OPTION('q', OPT_QUELCH), |
| 216 |
AST_APP_OPTION('r', OPT_RELAXED), |
| 217 |
AST_APP_OPTION('s', OPT_NO_ST), |
| 218 |
}); |
| 219 |
|
| 220 |
static const char *readmf_name = "ReceiveMF"; |
| 221 |
static const char sendmf_name[] = "SendMF"; |
| 222 |
|
| 223 |
#define MF_BETWEEN_MS 50 |
| 224 |
#define MF_DURATION 55 |
| 225 |
#define MF_KP_DURATION 120 |
| 226 |
#define MF_ST_DURATION 65 |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
static int read_mf_digits(struct ast_channel *chan, char *buf, int buflen, int timeout, int features, int laxkp, int override, int no_kp, int no_st, int maxdigits) { |
| 246 |
struct ast_dsp *dsp; |
| 247 |
struct ast_frame *frame = NULL; |
| 248 |
struct timeval start; |
| 249 |
int remaining_time = timeout; |
| 250 |
int digits_read = 0; |
| 251 |
int is_start_digit = 0; |
| 252 |
char *str = buf; |
| 253 |
int res = 0; |
| 254 |
|
| 255 |
if (!(dsp = ast_dsp_new())) { |
| 256 |
ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); |
| 257 |
pbx_builtin_setvar_helper(chan, "RECEIVEMFSTATUS", "ERROR"); |
| 258 |
return -1; |
| 259 |
} |
| 260 |
ast_dsp_set_features(dsp, DSP_FEATURE_DIGIT_DETECT); |
| 261 |
ast_dsp_set_digitmode(dsp, DSP_DIGITMODE_MF | features); |
| 262 |
|
| 263 |
start = ast_tvnow(); |
| 264 |
*str = 0; |
| 265 |
|
| 266 |
|
| 267 |
while (timeout == 0 || remaining_time > 0) { |
| 268 |
if (timeout > 0) { |
| 269 |
remaining_time = ast_remaining_ms(start, timeout); |
| 270 |
if (remaining_time <= 0) { |
| 271 |
pbx_builtin_setvar_helper(chan, "RECEIVEMFSTATUS", "TIMEOUT"); |
| 272 |
break; |
| 273 |
} |
| 274 |
} |
| 275 |
if ((maxdigits && digits_read >= maxdigits) || digits_read >= (buflen - 1)) { |
| 276 |
|
| 277 |
pbx_builtin_setvar_helper(chan, "RECEIVEMFSTATUS", "MAXDIGITS"); |
| 278 |
break; |
| 279 |
} |
| 280 |
|
| 281 |
if (ast_waitfor(chan, 1000) > 0) { |
| 282 |
frame = ast_read(chan); |
| 283 |
if (!frame) { |
| 284 |
ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan)); |
| 285 |
pbx_builtin_setvar_helper(chan, "RECEIVEMFSTATUS", "HANGUP"); |
| 286 |
break; |
| 287 |
} else if (frame->frametype == AST_FRAME_VOICE) { |
| 288 |
frame = ast_dsp_process(chan, dsp, frame); |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
if (frame->frametype == AST_FRAME_DTMF) { |
| 293 |
char result = frame->subclass.integer; |
| 294 |
if (digits_read == 0 && !laxkp && result != '*') { |
| 295 |
ast_debug(1, "Received MF digit, but no KP yet, ignoring: %c\n", result); |
| 296 |
ast_frfree(frame); |
| 297 |
continue; |
| 298 |
} |
| 299 |
ast_debug(1, "Received MF digit: %c\n", result); |
| 300 |
if (result == '*') { |
| 301 |
|
| 302 |
if (override && digits_read > 0) { |
| 303 |
ast_debug(1, "Received another KP, starting over\n"); |
| 304 |
str = buf; |
| 305 |
*str = 0; |
| 306 |
digits_read = 1; |
| 307 |
} else { |
| 308 |
digits_read++; |
| 309 |
} |
| 310 |
|
| 311 |
if (no_kp) { |
| 312 |
ast_frfree(frame); |
| 313 |
continue; |
| 314 |
} |
| 315 |
} else { |
| 316 |
digits_read++; |
| 317 |
} |
| 318 |
is_start_digit = (strchr("#", result) || strchr("A", result) || strchr("B", result) || strchr("C", result)); |
| 319 |
|
| 320 |
if (!no_st || !is_start_digit) { |
| 321 |
*str++ = result; |
| 322 |
*str = 0; |
| 323 |
} |
| 324 |
|
| 325 |
if (is_start_digit) { |
| 326 |
pbx_builtin_setvar_helper(chan, "RECEIVEMFSTATUS", "START"); |
| 327 |
ast_frfree(frame); |
| 328 |
break; |
| 329 |
} |
| 330 |
|
| 331 |
ast_frfree(frame); |
| 332 |
} |
| 333 |
} |
| 334 |
} else { |
| 335 |
pbx_builtin_setvar_helper(chan, "RECEIVEMFSTATUS", "HANGUP"); |
| 336 |
res = -1; |
| 337 |
} |
| 338 |
} |
| 339 |
ast_dsp_free(dsp); |
| 340 |
ast_debug(3, "channel '%s' - event loop stopped { timeout: %d, remaining_time: %d }\n", ast_channel_name(chan), timeout, remaining_time); |
| 341 |
return res; |
| 342 |
} |
| 343 |
|
| 344 |
static int read_mf_exec(struct ast_channel *chan, const char *data) |
| 345 |
{ |
| 346 |
#define BUFFER_SIZE 256 |
| 347 |
char tmp[BUFFER_SIZE] = ""; |
| 348 |
int to = 0; |
| 349 |
double tosec; |
| 350 |
struct ast_flags flags = {0}; |
| 351 |
char *optargs[OPT_ARG_ARRAY_SIZE]; |
| 352 |
char *argcopy = NULL; |
| 353 |
int res, features = 0, maxdigits = 0; |
| 354 |
|
| 355 |
AST_DECLARE_APP_ARGS(arglist, |
| 356 |
AST_APP_ARG(variable); |
| 357 |
AST_APP_ARG(timeout); |
| 358 |
AST_APP_ARG(options); |
| 359 |
); |
| 360 |
|
| 361 |
if (ast_strlen_zero(data)) { |
| 362 |
ast_log(LOG_WARNING, "ReceiveMF requires an argument (variable)\n"); |
| 363 |
return -1; |
| 364 |
} |
| 365 |
|
| 366 |
argcopy = ast_strdupa(data); |
| 367 |
|
| 368 |
AST_STANDARD_APP_ARGS(arglist, argcopy); |
| 369 |
|
| 370 |
if (!ast_strlen_zero(arglist.options)) { |
| 371 |
ast_app_parse_options(read_app_options, &flags, optargs, arglist.options); |
| 372 |
} |
| 373 |
|
| 374 |
if (!ast_strlen_zero(arglist.timeout)) { |
| 375 |
tosec = atof(arglist.timeout); |
| 376 |
if (tosec <= 0) { |
| 377 |
to = 0; |
| 378 |
} else { |
| 379 |
to = tosec * 1000.0; |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
if (ast_strlen_zero(arglist.variable)) { |
| 384 |
ast_log(LOG_WARNING, "Invalid! Usage: ReceiveMF(variable[,timeout][,option])\n"); |
| 385 |
return -1; |
| 386 |
} |
| 387 |
if (ast_test_flag(&flags, OPT_MAXDIGITS) && !ast_strlen_zero(optargs[OPT_ARG_MAXDIGITS])) { |
| 388 |
maxdigits = atoi(optargs[OPT_ARG_MAXDIGITS]); |
| 389 |
if (maxdigits <= 0) { |
| 390 |
ast_log(LOG_WARNING, "Invalid maximum number of digits, ignoring: '%s'\n", optargs[OPT_ARG_MAXDIGITS]); |
| 391 |
maxdigits = 0; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
if (ast_test_flag(&flags, OPT_DELAY)) { |
| 396 |
features |= DSP_DIGITMODE_MUTEMAX; |
| 397 |
} |
| 398 |
|
| 399 |
if (ast_test_flag(&flags, OPT_MUTE)) { |
| 400 |
features |= DSP_DIGITMODE_MUTECONF; |
| 401 |
} |
| 402 |
|
| 403 |
if (!ast_test_flag(&flags, OPT_QUELCH)) { |
| 404 |
features |= DSP_DIGITMODE_NOQUELCH; |
| 405 |
} |
| 406 |
|
| 407 |
if (ast_test_flag(&flags, OPT_RELAXED)) { |
| 408 |
features |= DSP_DIGITMODE_RELAXDTMF; |
| 409 |
} |
| 410 |
|
| 411 |
res = read_mf_digits(chan, tmp, BUFFER_SIZE, to, features, (ast_test_flag(&flags, OPT_LAX_KP)), |
| 412 |
(ast_test_flag(&flags, OPT_KP_OVERRIDE)), (ast_test_flag(&flags, OPT_NO_KP)), (ast_test_flag(&flags, OPT_NO_ST)), maxdigits); |
| 413 |
pbx_builtin_setvar_helper(chan, arglist.variable, tmp); |
| 414 |
if (!ast_strlen_zero(tmp)) { |
| 415 |
ast_verb(3, "MF digits received: '%s'\n", tmp); |
| 416 |
} else if (!res) { |
| 417 |
ast_verb(3, "No MF digits received.\n"); |
| 418 |
} |
| 419 |
return res; |
| 420 |
} |
| 421 |
|
| 422 |
static int sendmf_exec(struct ast_channel *chan, const char *vdata) |
| 423 |
{ |
| 424 |
int res; |
| 425 |
char *data; |
| 426 |
int dinterval = 0, duration = 0, durationkp = 0, durationst = 0; |
| 427 |
struct ast_channel *chan_found = NULL; |
| 428 |
struct ast_channel *chan_dest = chan; |
| 429 |
struct ast_channel *chan_autoservice = NULL; |
| 430 |
AST_DECLARE_APP_ARGS(args, |
| 431 |
AST_APP_ARG(digits); |
| 432 |
AST_APP_ARG(dinterval); |
| 433 |
AST_APP_ARG(duration); |
| 434 |
AST_APP_ARG(durationkp); |
| 435 |
AST_APP_ARG(durationst); |
| 436 |
AST_APP_ARG(channel); |
| 437 |
); |
| 438 |
|
| 439 |
if (ast_strlen_zero(vdata)) { |
| 440 |
ast_log(LOG_WARNING, "SendMF requires an argument\n"); |
| 441 |
return 0; |
| 442 |
} |
| 443 |
|
| 444 |
data = ast_strdupa(vdata); |
| 445 |
AST_STANDARD_APP_ARGS(args, data); |
| 446 |
|
| 447 |
if (ast_strlen_zero(args.digits)) { |
| 448 |
ast_log(LOG_WARNING, "The digits argument is required (0-9,*#ABC,wf)\n"); |
| 449 |
return 0; |
| 450 |
} |
| 451 |
if (!ast_strlen_zero(args.dinterval)) { |
| 452 |
ast_app_parse_timelen(args.dinterval, &dinterval, TIMELEN_MILLISECONDS); |
| 453 |
} |
| 454 |
if (!ast_strlen_zero(args.duration)) { |
| 455 |
ast_app_parse_timelen(args.duration, &duration, TIMELEN_MILLISECONDS); |
| 456 |
} |
| 457 |
if (!ast_strlen_zero(args.durationkp)) { |
| 458 |
ast_app_parse_timelen(args.durationkp, &durationkp, TIMELEN_MILLISECONDS); |
| 459 |
} |
| 460 |
if (!ast_strlen_zero(args.durationst)) { |
| 461 |
ast_app_parse_timelen(args.durationst, &durationst, TIMELEN_MILLISECONDS); |
| 462 |
} |
| 463 |
if (!ast_strlen_zero(args.channel)) { |
| 464 |
chan_found = ast_channel_get_by_name(args.channel); |
| 465 |
if (!chan_found) { |
| 466 |
ast_log(LOG_WARNING, "No such channel: %s\n", args.channel); |
| 467 |
return 0; |
| 468 |
} |
| 469 |
chan_dest = chan_found; |
| 470 |
if (chan_found != chan) { |
| 471 |
chan_autoservice = chan; |
| 472 |
} |
| 473 |
} |
| 474 |
res = ast_mf_stream(chan_dest, chan_autoservice, NULL, args.digits, dinterval <= 0 ? MF_BETWEEN_MS : dinterval, |
| 475 |
duration <= 0 ? MF_DURATION : duration, durationkp <= 0 ? MF_KP_DURATION : durationkp, |
| 476 |
durationst <= 0 ? MF_ST_DURATION : durationst, 0); |
| 477 |
ast_channel_cleanup(chan_found); |
| 478 |
|
| 479 |
return chan_autoservice ? 0 : res; |
| 480 |
} |
| 481 |
|
| 482 |
static int manager_play_mf(struct mansession *s, const struct message *m) |
| 483 |
{ |
| 484 |
const char *channel = astman_get_header(m, "Channel"); |
| 485 |
const char *digit = astman_get_header(m, "Digit"); |
| 486 |
const char *duration = astman_get_header(m, "Duration"); |
| 487 |
struct ast_channel *chan; |
| 488 |
unsigned int duration_ms = MF_DURATION; |
| 489 |
|
| 490 |
if (!(chan = ast_channel_get_by_name(channel))) { |
| 491 |
astman_send_error(s, m, "Channel not found"); |
| 492 |
return 0; |
| 493 |
} |
| 494 |
|
| 495 |
if (ast_strlen_zero(digit)) { |
| 496 |
astman_send_error(s, m, "No digit specified"); |
| 497 |
chan = ast_channel_unref(chan); |
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
if (!strcmp(digit, "*")) |
| 503 |
duration_ms = MF_KP_DURATION; |
| 504 |
|
| 505 |
if (!strcmp(digit, "#") || !strcmp(digit, "A") || !strcmp(digit, "B") || !strcmp(digit, "C")) |
| 506 |
duration_ms = MF_ST_DURATION; |
| 507 |
|
| 508 |
if (!ast_strlen_zero(duration) && (sscanf(duration, "%30u", &duration_ms) != 1)) { |
| 509 |
astman_send_error(s, m, "Could not convert Duration parameter"); |
| 510 |
chan = ast_channel_unref(chan); |
| 511 |
return 0; |
| 512 |
} |
| 513 |
|
| 514 |
ast_mf_stream(chan, NULL, NULL, digit, 0, duration_ms, duration_ms, duration_ms, 1); |
| 515 |
|
| 516 |
chan = ast_channel_unref(chan); |
| 517 |
|
| 518 |
astman_send_ack(s, m, "MF successfully queued"); |
| 519 |
|
| 520 |
return 0; |
| 521 |
} |
| 522 |
|
| 523 |
static int unload_module(void) |
| 524 |
{ |
| 525 |
int res; |
| 526 |
|
| 527 |
res = ast_unregister_application(readmf_name); |
| 528 |
res |= ast_unregister_application(sendmf_name); |
| 529 |
res |= ast_manager_unregister("PlayMF"); |
| 530 |
|
| 531 |
return res; |
| 532 |
} |
| 533 |
|
| 534 |
static int load_module(void) |
| 535 |
{ |
| 536 |
int res; |
| 537 |
|
| 538 |
res = ast_register_application_xml(readmf_name, read_mf_exec); |
| 539 |
res |= ast_register_application_xml(sendmf_name, sendmf_exec); |
| 540 |
res |= ast_manager_register_xml("PlayMF", EVENT_FLAG_CALL, manager_play_mf); |
| 541 |
|
| 542 |
return res; |
| 543 |
} |
| 544 |
|
| 545 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "MF Sender and Receiver Applications"); |