| 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 |
#define DEFAULT_POST_DIGIT_TIMER 0.8 |
| 165 |
|
| 166 |
|
| 167 |
#define SF_MIN_OFF 25 |
| 168 |
#define SF_ON 67 |
| 169 |
#define SF_BETWEEN 600 |
| 170 |
#define SF_MIN_DETECT 50 |
| 171 |
|
| 172 |
enum read_option_flags { |
| 173 |
OPT_DELAY = (1 << 0), |
| 174 |
OPT_MUTE = (1 << 1), |
| 175 |
OPT_QUELCH = (1 << 2), |
| 176 |
OPT_RELAXED = (1 << 3), |
| 177 |
OPT_EXTRAPULSES = (1 << 4), |
| 178 |
OPT_DIGIT_TIMEOUT = (1 << 5), |
| 179 |
}; |
| 180 |
|
| 181 |
enum { |
| 182 |
OPT_ARG_DIGIT_TIMEOUT, |
| 183 |
|
| 184 |
OPT_ARG_ARRAY_SIZE, |
| 185 |
}; |
| 186 |
|
| 187 |
AST_APP_OPTIONS(read_app_options, { |
| 188 |
AST_APP_OPTION('d', OPT_DELAY), |
| 189 |
AST_APP_OPTION('e', OPT_EXTRAPULSES), |
| 190 |
AST_APP_OPTION('m', OPT_MUTE), |
| 191 |
AST_APP_OPTION_ARG('t', OPT_DIGIT_TIMEOUT, OPT_ARG_DIGIT_TIMEOUT), |
| 192 |
AST_APP_OPTION('q', OPT_QUELCH), |
| 193 |
AST_APP_OPTION('r', OPT_RELAXED), |
| 194 |
}); |
| 195 |
|
| 196 |
static const char *readsf_name = "ReceiveSF"; |
| 197 |
static const char sendsf_name[] = "SendSF"; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
static int read_sf_digits(struct ast_channel *chan, char *buf, int buflen, int timeout, int digit_timeout, int maxdigits, int freq, int features, int extrapulses) |
| 216 |
{ |
| 217 |
struct ast_dsp *dsp = NULL; |
| 218 |
struct ast_frame *frame = NULL; |
| 219 |
struct timeval start, pulsetimer, digittimer; |
| 220 |
int remaining_time = timeout; |
| 221 |
char *str = buf; |
| 222 |
int hits = 0, digits_read = 0; |
| 223 |
unsigned short int sf_on = 0; |
| 224 |
int res = 0; |
| 225 |
|
| 226 |
if (!(dsp = ast_dsp_new())) { |
| 227 |
ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); |
| 228 |
pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "ERROR"); |
| 229 |
return -1; |
| 230 |
} |
| 231 |
ast_dsp_set_features(dsp, DSP_FEATURE_FREQ_DETECT); |
| 232 |
|
| 233 |
ast_dsp_set_freqmode(dsp, freq, SF_MIN_DETECT, 16, 0); |
| 234 |
|
| 235 |
start = ast_tvnow(); |
| 236 |
*str = 0; |
| 237 |
|
| 238 |
while (timeout == 0 || remaining_time > 0) { |
| 239 |
if (timeout > 0) { |
| 240 |
remaining_time = ast_remaining_ms(start, timeout); |
| 241 |
if (remaining_time <= 0) { |
| 242 |
ast_debug(1, "SF all-digit timer expired\n"); |
| 243 |
pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "TIMEOUT"); |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
if (digits_read > 0 && digit_timeout > 0) { |
| 256 |
int remaining_time_for_next_digit = ast_remaining_ms(digittimer, digit_timeout); |
| 257 |
if (remaining_time_for_next_digit <= 0) { |
| 258 |
ast_debug(1, "SF post-digit timer expired (>= %d ms since last SF pulse)\n", digit_timeout); |
| 259 |
pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "TIMEOUT"); |
| 260 |
break; |
| 261 |
} |
| 262 |
} |
| 263 |
if (digits_read >= (buflen - 1)) { |
| 264 |
|
| 265 |
pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "MAXDIGITS"); |
| 266 |
break; |
| 267 |
} |
| 268 |
if (ast_waitfor(chan, 1000) > 0) { |
| 269 |
frame = ast_read(chan); |
| 270 |
if (!frame) { |
| 271 |
ast_debug(1, "Channel '%s' did not return a frame; probably hung up.\n", ast_channel_name(chan)); |
| 272 |
pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "HANGUP"); |
| 273 |
break; |
| 274 |
} else if (frame->frametype == AST_FRAME_VOICE) { |
| 275 |
frame = ast_dsp_process(chan, dsp, frame); |
| 276 |
if (frame->frametype == AST_FRAME_DTMF) { |
| 277 |
char result = frame->subclass.integer; |
| 278 |
if (result == 'q') { |
| 279 |
sf_on = 1; |
| 280 |
pulsetimer = ast_tvnow(); |
| 281 |
|
| 282 |
} |
| 283 |
} else { |
| 284 |
if (sf_on) { |
| 285 |
int timeleft = ast_remaining_ms(pulsetimer, SF_MIN_OFF); |
| 286 |
if (timeleft <= 0) { |
| 287 |
sf_on = 0; |
| 288 |
|
| 289 |
if (timeleft > -30) { |
| 290 |
hits++; |
| 291 |
digittimer = ast_tvnow(); |
| 292 |
ast_debug(5, "Detected SF pulse (pulse #%d)\n", hits); |
| 293 |
ast_dsp_free(dsp); |
| 294 |
if (!(dsp = ast_dsp_new())) { |
| 295 |
ast_log(LOG_WARNING, "Unable to allocate DSP!\n"); |
| 296 |
pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "ERROR"); |
| 297 |
ast_frfree(frame); |
| 298 |
return -1; |
| 299 |
} |
| 300 |
ast_dsp_set_features(dsp, DSP_FEATURE_FREQ_DETECT); |
| 301 |
ast_dsp_set_freqmode(dsp, freq, SF_MIN_DETECT, 16, 0); |
| 302 |
} else { |
| 303 |
ast_debug(5, "SF noise, ignoring, time elapsed was %d ms\n", timeleft); |
| 304 |
} |
| 305 |
} |
| 306 |
} else if (hits > 0 && ast_remaining_ms(digittimer, SF_BETWEEN) <= 0) { |
| 307 |
|
| 308 |
ast_debug(2, "Received SF digit: %d\n", hits == 10 ? 0 : hits); |
| 309 |
digits_read++; |
| 310 |
if (hits > 10) { |
| 311 |
if (extrapulses) { |
| 312 |
|
| 313 |
if (hits == 11) { |
| 314 |
hits = '*'; |
| 315 |
} else if (hits == 12) { |
| 316 |
hits = '#'; |
| 317 |
} else if (hits == 13) { |
| 318 |
hits = 'D'; |
| 319 |
} else if (hits == 14) { |
| 320 |
hits = 'C'; |
| 321 |
} else if (hits == 15) { |
| 322 |
hits = 'B'; |
| 323 |
} else if (hits == 16) { |
| 324 |
hits = 'A'; |
| 325 |
} else { |
| 326 |
ast_debug(3, "Got %d SF pulses, is someone playing with the phone?\n", hits); |
| 327 |
hits = 'A'; |
| 328 |
} |
| 329 |
*str++ = hits; |
| 330 |
} else { |
| 331 |
ast_debug(2, "Got more than 10 pulses, truncating to 10\n"); |
| 332 |
hits = 0; |
| 333 |
*str++ = hits + '0'; |
| 334 |
} |
| 335 |
} else { |
| 336 |
if (hits == 10) { |
| 337 |
hits = 0; |
| 338 |
} |
| 339 |
*str++ = hits + '0'; |
| 340 |
} |
| 341 |
*str = 0; |
| 342 |
hits = 0; |
| 343 |
if (maxdigits > 0 && digits_read >= maxdigits) { |
| 344 |
pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "START"); |
| 345 |
ast_frfree(frame); |
| 346 |
break; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
ast_frfree(frame); |
| 352 |
} else { |
| 353 |
pbx_builtin_setvar_helper(chan, "RECEIVESFSTATUS", "HANGUP"); |
| 354 |
res = -1; |
| 355 |
} |
| 356 |
} |
| 357 |
if (dsp) { |
| 358 |
ast_dsp_free(dsp); |
| 359 |
} |
| 360 |
ast_debug(3, "channel '%s' - event loop stopped { timeout: %d, remaining_time: %d }\n", ast_channel_name(chan), timeout, remaining_time); |
| 361 |
return res; |
| 362 |
} |
| 363 |
|
| 364 |
static int read_sf_exec(struct ast_channel *chan, const char *data) |
| 365 |
{ |
| 366 |
#define BUFFER_SIZE 256 |
| 367 |
char tmp[BUFFER_SIZE] = ""; |
| 368 |
double tosec, digitsec; |
| 369 |
struct ast_flags flags = {0}; |
| 370 |
char *opt_args[OPT_ARG_ARRAY_SIZE]; |
| 371 |
char *argcopy = NULL; |
| 372 |
int res, features = 0, digits = 0, to = 0, digit_timeout = 0, freq = 2600; |
| 373 |
|
| 374 |
AST_DECLARE_APP_ARGS(arglist, |
| 375 |
AST_APP_ARG(variable); |
| 376 |
AST_APP_ARG(digits); |
| 377 |
AST_APP_ARG(timeout); |
| 378 |
AST_APP_ARG(freq); |
| 379 |
AST_APP_ARG(options); |
| 380 |
); |
| 381 |
|
| 382 |
if (ast_strlen_zero(data)) { |
| 383 |
ast_log(LOG_WARNING, "ReceiveSF requires an argument (variable)\n"); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
|
| 387 |
argcopy = ast_strdupa(data); |
| 388 |
|
| 389 |
AST_STANDARD_APP_ARGS(arglist, argcopy); |
| 390 |
|
| 391 |
if (!ast_strlen_zero(arglist.options)) { |
| 392 |
ast_app_parse_options(read_app_options, &flags, opt_args, arglist.options); |
| 393 |
} |
| 394 |
|
| 395 |
if (!ast_strlen_zero(arglist.timeout)) { |
| 396 |
tosec = atof(arglist.timeout); |
| 397 |
if (tosec <= 0) { |
| 398 |
to = 0; |
| 399 |
} else { |
| 400 |
to = tosec * 1000.0; |
| 401 |
} |
| 402 |
} |
| 403 |
if (ast_test_flag(&flags, OPT_DIGIT_TIMEOUT)) { |
| 404 |
digitsec = (!ast_strlen_zero(opt_args[OPT_ARG_DIGIT_TIMEOUT]) ? atof(opt_args[OPT_ARG_DIGIT_TIMEOUT]) : DEFAULT_POST_DIGIT_TIMER); |
| 405 |
if (digitsec <= 0) { |
| 406 |
digit_timeout = 0; |
| 407 |
} else { |
| 408 |
digit_timeout = digitsec * 1000.0; |
| 409 |
if (digit_timeout < SF_BETWEEN) { |
| 410 |
ast_log(LOG_WARNING, "SF post-digit timer (%d) cannot be less than the SF inter-digit timeout (%d ms)\n", digit_timeout, SF_BETWEEN); |
| 411 |
digit_timeout = SF_BETWEEN; |
| 412 |
} |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
if (!ast_strlen_zero(arglist.digits) && (ast_str_to_int(arglist.digits, &digits) || digits <= 0)) { |
| 417 |
ast_log(LOG_WARNING, "Invalid number of digits: %s\n", arglist.digits); |
| 418 |
return -1; |
| 419 |
} |
| 420 |
|
| 421 |
if (!ast_strlen_zero(arglist.freq) && (ast_str_to_int(arglist.freq, &freq) || freq <= 0)) { |
| 422 |
ast_log(LOG_WARNING, "Invalid freq: %s\n", arglist.freq); |
| 423 |
return -1; |
| 424 |
} |
| 425 |
|
| 426 |
if (ast_strlen_zero(arglist.variable)) { |
| 427 |
ast_log(LOG_WARNING, "Invalid! Usage: ReceiveSF(variable[,timeout][,option])\n"); |
| 428 |
return -1; |
| 429 |
} |
| 430 |
|
| 431 |
if (ast_test_flag(&flags, OPT_DELAY)) { |
| 432 |
features |= DSP_DIGITMODE_MUTEMAX; |
| 433 |
} |
| 434 |
|
| 435 |
if (ast_test_flag(&flags, OPT_MUTE)) { |
| 436 |
features |= DSP_DIGITMODE_MUTECONF; |
| 437 |
} |
| 438 |
|
| 439 |
if (!ast_test_flag(&flags, OPT_QUELCH)) { |
| 440 |
features |= DSP_DIGITMODE_NOQUELCH; |
| 441 |
} |
| 442 |
|
| 443 |
if (ast_test_flag(&flags, OPT_RELAXED)) { |
| 444 |
features |= DSP_DIGITMODE_RELAXDTMF; |
| 445 |
} |
| 446 |
|
| 447 |
res = read_sf_digits(chan, tmp, BUFFER_SIZE, to, digit_timeout, digits, freq, features, ast_test_flag(&flags, OPT_EXTRAPULSES)); |
| 448 |
pbx_builtin_setvar_helper(chan, arglist.variable, tmp); |
| 449 |
if (!ast_strlen_zero(tmp)) { |
| 450 |
ast_verb(3, "SF digits received: '%s'\n", tmp); |
| 451 |
} else if (!res) { |
| 452 |
ast_verb(3, "No SF digits received.\n"); |
| 453 |
} |
| 454 |
return res; |
| 455 |
} |
| 456 |
|
| 457 |
static int sendsf_exec(struct ast_channel *chan, const char *vdata) |
| 458 |
{ |
| 459 |
int res; |
| 460 |
char *data; |
| 461 |
int frequency = 2600; |
| 462 |
struct ast_channel *chan_found = NULL; |
| 463 |
struct ast_channel *chan_dest = chan; |
| 464 |
struct ast_channel *chan_autoservice = NULL; |
| 465 |
AST_DECLARE_APP_ARGS(args, |
| 466 |
AST_APP_ARG(digits); |
| 467 |
AST_APP_ARG(frequency); |
| 468 |
AST_APP_ARG(channel); |
| 469 |
); |
| 470 |
|
| 471 |
if (ast_strlen_zero(vdata)) { |
| 472 |
ast_log(LOG_WARNING, "SendSF requires an argument\n"); |
| 473 |
return 0; |
| 474 |
} |
| 475 |
|
| 476 |
data = ast_strdupa(vdata); |
| 477 |
AST_STANDARD_APP_ARGS(args, data); |
| 478 |
|
| 479 |
if (ast_strlen_zero(args.digits)) { |
| 480 |
ast_log(LOG_WARNING, "The digits argument is required (0-9,wf)\n"); |
| 481 |
return 0; |
| 482 |
} |
| 483 |
if (!ast_strlen_zero(args.frequency) && (ast_str_to_int(args.frequency, &frequency) || frequency < 1)) { |
| 484 |
ast_log(LOG_WARNING, "Invalid duration: %s\n", args.frequency); |
| 485 |
return -1; |
| 486 |
} |
| 487 |
if (!ast_strlen_zero(args.channel)) { |
| 488 |
chan_found = ast_channel_get_by_name(args.channel); |
| 489 |
if (!chan_found) { |
| 490 |
ast_log(LOG_WARNING, "No such channel: %s\n", args.channel); |
| 491 |
return 0; |
| 492 |
} |
| 493 |
chan_dest = chan_found; |
| 494 |
if (chan_found != chan) { |
| 495 |
chan_autoservice = chan; |
| 496 |
} |
| 497 |
} |
| 498 |
res = ast_sf_stream(chan_dest, chan_autoservice, NULL, args.digits, frequency, 0); |
| 499 |
ast_channel_cleanup(chan_found); |
| 500 |
|
| 501 |
return chan_autoservice ? 0 : res; |
| 502 |
} |
| 503 |
|
| 504 |
static int unload_module(void) |
| 505 |
{ |
| 506 |
int res; |
| 507 |
|
| 508 |
res = ast_unregister_application(readsf_name); |
| 509 |
res |= ast_unregister_application(sendsf_name); |
| 510 |
|
| 511 |
return res; |
| 512 |
} |
| 513 |
|
| 514 |
static int load_module(void) |
| 515 |
{ |
| 516 |
int res; |
| 517 |
|
| 518 |
res = ast_register_application_xml(readsf_name, read_sf_exec); |
| 519 |
res |= ast_register_application_xml(sendsf_name, sendsf_exec); |
| 520 |
|
| 521 |
return res; |
| 522 |
} |
| 523 |
|
| 524 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "SF Sender and Receiver Applications"); |