| 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 |
#include "asterisk.h" |
| 37 |
|
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/channel.h" |
| 40 |
#include "asterisk/pbx.h" |
| 41 |
#include "asterisk/app.h" |
| 42 |
#include "asterisk/audiohook.h" |
| 43 |
#include "asterisk/test.h" |
| 44 |
#define AST_API_MODULE |
| 45 |
#include "asterisk/beep.h" |
| 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 |
static const char context_name[] = "__func_periodic_hook_context__"; |
| 97 |
static const char exten_name[] = "hook"; |
| 98 |
static const char full_exten_name[] = "hook@__func_periodic_hook_context__"; |
| 99 |
|
| 100 |
static const char beep_exten[] = "beep"; |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static unsigned int global_hook_id; |
| 109 |
|
| 110 |
|
| 111 |
struct hook_state { |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
struct ast_audiohook audiohook; |
| 118 |
|
| 119 |
unsigned int interval; |
| 120 |
|
| 121 |
struct timeval last_hook; |
| 122 |
|
| 123 |
char *context; |
| 124 |
|
| 125 |
char *exten; |
| 126 |
|
| 127 |
unsigned int hook_id; |
| 128 |
|
| 129 |
unsigned char disabled; |
| 130 |
}; |
| 131 |
|
| 132 |
static void hook_datastore_destroy_callback(void *data) |
| 133 |
{ |
| 134 |
struct hook_state *state = data; |
| 135 |
|
| 136 |
ast_audiohook_lock(&state->audiohook); |
| 137 |
ast_audiohook_detach(&state->audiohook); |
| 138 |
ast_audiohook_unlock(&state->audiohook); |
| 139 |
ast_audiohook_destroy(&state->audiohook); |
| 140 |
|
| 141 |
ast_free(state->context); |
| 142 |
ast_free(state->exten); |
| 143 |
ast_free(state); |
| 144 |
} |
| 145 |
|
| 146 |
static const struct ast_datastore_info hook_datastore = { |
| 147 |
.type = AST_MODULE, |
| 148 |
.destroy = hook_datastore_destroy_callback, |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
struct hook_thread_arg { |
| 153 |
|
| 154 |
char *hook_id; |
| 155 |
|
| 156 |
char *chan_name; |
| 157 |
|
| 158 |
char *context; |
| 159 |
|
| 160 |
char *exten; |
| 161 |
}; |
| 162 |
|
| 163 |
static void hook_thread_arg_destroy(struct hook_thread_arg *arg) |
| 164 |
{ |
| 165 |
ast_free(arg->hook_id); |
| 166 |
ast_free(arg->chan_name); |
| 167 |
ast_free(arg->context); |
| 168 |
ast_free(arg->exten); |
| 169 |
ast_free(arg); |
| 170 |
} |
| 171 |
|
| 172 |
static void *hook_launch_thread(void *data) |
| 173 |
{ |
| 174 |
struct hook_thread_arg *arg = data; |
| 175 |
struct ast_variable hook_id = { |
| 176 |
.name = "HOOK_ID", |
| 177 |
.value = arg->hook_id, |
| 178 |
}; |
| 179 |
struct ast_variable chan_name_var = { |
| 180 |
.name = "HOOK_CHANNEL", |
| 181 |
.value = arg->chan_name, |
| 182 |
.next = &hook_id, |
| 183 |
}; |
| 184 |
|
| 185 |
ast_pbx_outgoing_exten("Local", NULL, full_exten_name, 60, |
| 186 |
arg->context, arg->exten, 1, NULL, AST_OUTGOING_NO_WAIT, |
| 187 |
NULL, NULL, &chan_name_var, NULL, NULL, 1, NULL); |
| 188 |
|
| 189 |
hook_thread_arg_destroy(arg); |
| 190 |
|
| 191 |
return NULL; |
| 192 |
} |
| 193 |
|
| 194 |
static struct hook_thread_arg *hook_thread_arg_alloc(struct ast_channel *chan, |
| 195 |
struct hook_state *state) |
| 196 |
{ |
| 197 |
struct hook_thread_arg *arg; |
| 198 |
|
| 199 |
if (!(arg = ast_calloc(1, sizeof(*arg)))) { |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
ast_channel_lock(chan); |
| 204 |
arg->chan_name = ast_strdup(ast_channel_name(chan)); |
| 205 |
ast_channel_unlock(chan); |
| 206 |
if (!arg->chan_name) { |
| 207 |
hook_thread_arg_destroy(arg); |
| 208 |
return NULL; |
| 209 |
} |
| 210 |
|
| 211 |
if (ast_asprintf(&arg->hook_id, "%u", state->hook_id) == -1) { |
| 212 |
hook_thread_arg_destroy(arg); |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
|
| 216 |
if (!(arg->context = ast_strdup(state->context))) { |
| 217 |
hook_thread_arg_destroy(arg); |
| 218 |
return NULL; |
| 219 |
} |
| 220 |
|
| 221 |
if (!(arg->exten = ast_strdup(state->exten))) { |
| 222 |
hook_thread_arg_destroy(arg); |
| 223 |
return NULL; |
| 224 |
} |
| 225 |
|
| 226 |
return arg; |
| 227 |
} |
| 228 |
|
| 229 |
static int do_hook(struct ast_channel *chan, struct hook_state *state) |
| 230 |
{ |
| 231 |
pthread_t t; |
| 232 |
struct hook_thread_arg *arg; |
| 233 |
int res; |
| 234 |
|
| 235 |
if (!(arg = hook_thread_arg_alloc(chan, state))) { |
| 236 |
return -1; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
res = ast_pthread_create_detached_background(&t, NULL, hook_launch_thread, arg); |
| 244 |
if (res != 0) { |
| 245 |
hook_thread_arg_destroy(arg); |
| 246 |
} |
| 247 |
|
| 248 |
return res; |
| 249 |
} |
| 250 |
|
| 251 |
static int hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, |
| 252 |
struct ast_frame *frame, enum ast_audiohook_direction direction) |
| 253 |
{ |
| 254 |
struct hook_state *state = (struct hook_state *) audiohook; |
| 255 |
struct timeval now; |
| 256 |
int res = 0; |
| 257 |
|
| 258 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || state->disabled) { |
| 259 |
return 0; |
| 260 |
} |
| 261 |
|
| 262 |
now = ast_tvnow(); |
| 263 |
if (ast_tvdiff_ms(now, state->last_hook) > state->interval * 1000) { |
| 264 |
if ((res = do_hook(chan, state))) { |
| 265 |
const char *name; |
| 266 |
ast_channel_lock(chan); |
| 267 |
name = ast_strdupa(ast_channel_name(chan)); |
| 268 |
ast_channel_unlock(chan); |
| 269 |
ast_log(LOG_WARNING, "Failed to run hook on '%s'\n", name); |
| 270 |
} |
| 271 |
state->last_hook = now; |
| 272 |
} |
| 273 |
|
| 274 |
return res; |
| 275 |
} |
| 276 |
|
| 277 |
static struct hook_state *hook_state_alloc(const char *context, const char *exten, |
| 278 |
unsigned int interval, unsigned int hook_id) |
| 279 |
{ |
| 280 |
struct hook_state *state; |
| 281 |
|
| 282 |
if (!(state = ast_calloc(1, sizeof(*state)))) { |
| 283 |
return NULL; |
| 284 |
} |
| 285 |
|
| 286 |
state->context = ast_strdup(context); |
| 287 |
state->exten = ast_strdup(exten); |
| 288 |
state->interval = interval; |
| 289 |
state->hook_id = hook_id; |
| 290 |
|
| 291 |
ast_audiohook_init(&state->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, |
| 292 |
AST_MODULE, AST_AUDIOHOOK_MANIPULATE_ALL_RATES); |
| 293 |
state->audiohook.manipulate_callback = hook_callback; |
| 294 |
|
| 295 |
return state; |
| 296 |
} |
| 297 |
|
| 298 |
static int init_hook(struct ast_channel *chan, const char *context, const char *exten, |
| 299 |
unsigned int interval, unsigned int hook_id) |
| 300 |
{ |
| 301 |
struct hook_state *state; |
| 302 |
struct ast_datastore *datastore; |
| 303 |
char uid[32]; |
| 304 |
|
| 305 |
snprintf(uid, sizeof(uid), "%u", hook_id); |
| 306 |
|
| 307 |
if (!(datastore = ast_datastore_alloc(&hook_datastore, uid))) { |
| 308 |
return -1; |
| 309 |
} |
| 310 |
|
| 311 |
if (!(state = hook_state_alloc(context, exten, interval, hook_id))) { |
| 312 |
ast_datastore_free(datastore); |
| 313 |
return -1; |
| 314 |
} |
| 315 |
datastore->data = state; |
| 316 |
|
| 317 |
ast_channel_lock(chan); |
| 318 |
ast_channel_datastore_add(chan, datastore); |
| 319 |
ast_audiohook_attach(chan, &state->audiohook); |
| 320 |
ast_channel_unlock(chan); |
| 321 |
|
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
static int hook_on(struct ast_channel *chan, const char *data, unsigned int hook_id) |
| 326 |
{ |
| 327 |
char *parse = ast_strdupa(S_OR(data, "")); |
| 328 |
AST_DECLARE_APP_ARGS(args, |
| 329 |
AST_APP_ARG(context); |
| 330 |
AST_APP_ARG(exten); |
| 331 |
AST_APP_ARG(interval); |
| 332 |
); |
| 333 |
unsigned int interval; |
| 334 |
|
| 335 |
AST_STANDARD_APP_ARGS(args, parse); |
| 336 |
|
| 337 |
if (ast_strlen_zero(args.interval) || |
| 338 |
sscanf(args.interval, "%30u", &interval) != 1 || interval == 0) { |
| 339 |
ast_log(LOG_WARNING, "Invalid hook interval: '%s'\n", S_OR(args.interval, "")); |
| 340 |
return -1; |
| 341 |
} |
| 342 |
|
| 343 |
if (ast_strlen_zero(args.context) || ast_strlen_zero(args.exten)) { |
| 344 |
ast_log(LOG_WARNING, "A context and extension are required for PERIODIC_HOOK().\n"); |
| 345 |
return -1; |
| 346 |
} |
| 347 |
|
| 348 |
ast_debug(1, "hook to %s@%s enabled on %s with interval of %u seconds\n", |
| 349 |
args.exten, args.context, ast_channel_name(chan), interval); |
| 350 |
ast_test_suite_event_notify("PERIODIC_HOOK_ENABLED", "Exten: %s\r\nChannel: %s\r\nInterval: %u\r\n", |
| 351 |
args.exten, ast_channel_name(chan), interval); |
| 352 |
|
| 353 |
return init_hook(chan, args.context, args.exten, interval, hook_id); |
| 354 |
} |
| 355 |
|
| 356 |
static int hook_off(struct ast_channel *chan, const char *hook_id) |
| 357 |
{ |
| 358 |
struct ast_datastore *datastore; |
| 359 |
struct hook_state *state; |
| 360 |
|
| 361 |
if (ast_strlen_zero(hook_id)) { |
| 362 |
return -1; |
| 363 |
} |
| 364 |
|
| 365 |
ast_channel_lock(chan); |
| 366 |
|
| 367 |
if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, hook_id))) { |
| 368 |
ast_log(LOG_WARNING, "Hook with ID '%s' not found on channel '%s'\n", hook_id, |
| 369 |
ast_channel_name(chan)); |
| 370 |
ast_channel_unlock(chan); |
| 371 |
return -1; |
| 372 |
} |
| 373 |
|
| 374 |
state = datastore->data; |
| 375 |
state->disabled = 1; |
| 376 |
|
| 377 |
ast_channel_unlock(chan); |
| 378 |
|
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
static int hook_read(struct ast_channel *chan, const char *cmd, char *data, |
| 383 |
char *buf, size_t len) |
| 384 |
{ |
| 385 |
unsigned int hook_id; |
| 386 |
|
| 387 |
if (!chan) { |
| 388 |
return -1; |
| 389 |
} |
| 390 |
|
| 391 |
hook_id = (unsigned int) ast_atomic_fetchadd_int((int *) &global_hook_id, 1); |
| 392 |
|
| 393 |
snprintf(buf, len, "%u", hook_id); |
| 394 |
|
| 395 |
return hook_on(chan, data, hook_id); |
| 396 |
} |
| 397 |
|
| 398 |
static int hook_re_enable(struct ast_channel *chan, const char *uid) |
| 399 |
{ |
| 400 |
struct ast_datastore *datastore; |
| 401 |
struct hook_state *state; |
| 402 |
|
| 403 |
if (ast_strlen_zero(uid)) { |
| 404 |
return -1; |
| 405 |
} |
| 406 |
|
| 407 |
ast_channel_lock(chan); |
| 408 |
|
| 409 |
if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, uid))) { |
| 410 |
ast_log(LOG_WARNING, "Hook with ID '%s' not found on '%s'\n", |
| 411 |
uid, ast_channel_name(chan)); |
| 412 |
ast_channel_unlock(chan); |
| 413 |
return -1; |
| 414 |
} |
| 415 |
|
| 416 |
state = datastore->data; |
| 417 |
state->disabled = 0; |
| 418 |
|
| 419 |
ast_channel_unlock(chan); |
| 420 |
|
| 421 |
return 0; |
| 422 |
} |
| 423 |
|
| 424 |
static int hook_write(struct ast_channel *chan, const char *cmd, char *data, |
| 425 |
const char *value) |
| 426 |
{ |
| 427 |
int res; |
| 428 |
|
| 429 |
if (!chan) { |
| 430 |
return -1; |
| 431 |
} |
| 432 |
|
| 433 |
if (ast_false(value)) { |
| 434 |
res = hook_off(chan, data); |
| 435 |
} else if (ast_true(value)) { |
| 436 |
res = hook_re_enable(chan, data); |
| 437 |
} else { |
| 438 |
ast_log(LOG_WARNING, "Invalid value for PERIODIC_HOOK function: '%s'\n", value); |
| 439 |
res = -1; |
| 440 |
} |
| 441 |
|
| 442 |
return res; |
| 443 |
} |
| 444 |
|
| 445 |
static struct ast_custom_function hook_function = { |
| 446 |
.name = "PERIODIC_HOOK", |
| 447 |
.read = hook_read, |
| 448 |
.write = hook_write, |
| 449 |
}; |
| 450 |
|
| 451 |
static int unload_module(void) |
| 452 |
{ |
| 453 |
ast_context_destroy(NULL, AST_MODULE); |
| 454 |
|
| 455 |
ast_custom_function_unregister(&hook_function); |
| 456 |
return 0; |
| 457 |
} |
| 458 |
|
| 459 |
static int load_module(void) |
| 460 |
{ |
| 461 |
int res; |
| 462 |
|
| 463 |
if (!ast_context_find_or_create(NULL, NULL, context_name, AST_MODULE)) { |
| 464 |
ast_log(LOG_ERROR, "Failed to create %s dialplan context.\n", context_name); |
| 465 |
return AST_MODULE_LOAD_DECLINE; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
res = ast_add_extension(context_name, 1, exten_name, 1, "", "", |
| 472 |
"Set", "EncodedChannel=${HOOK_CHANNEL}", |
| 473 |
NULL, AST_MODULE); |
| 474 |
res |= ast_add_extension(context_name, 1, exten_name, 2, "", "", |
| 475 |
"Set", "GROUP_NAME=${EncodedChannel}${HOOK_ID}", |
| 476 |
NULL, AST_MODULE); |
| 477 |
res |= ast_add_extension(context_name, 1, exten_name, 3, "", "", |
| 478 |
"Set", "GROUP(periodic-hook)=${GROUP_NAME}", |
| 479 |
NULL, AST_MODULE); |
| 480 |
res |= ast_add_extension(context_name, 1, exten_name, 4, "", "", "ExecIf", |
| 481 |
"$[${GROUP_COUNT(${GROUP_NAME}@periodic-hook)} > 1]?Hangup()", |
| 482 |
NULL, AST_MODULE); |
| 483 |
res |= ast_add_extension(context_name, 1, exten_name, 5, "", "", |
| 484 |
"Set", "ChannelToSpy=${URIDECODE(${EncodedChannel})}", |
| 485 |
NULL, AST_MODULE); |
| 486 |
res |= ast_add_extension(context_name, 1, exten_name, 6, "", "", |
| 487 |
"ChanSpy", "${ChannelToSpy},qEB", NULL, AST_MODULE); |
| 488 |
|
| 489 |
res |= ast_add_extension(context_name, 1, beep_exten, 1, "", "", |
| 490 |
"Answer", "", NULL, AST_MODULE); |
| 491 |
res |= ast_add_extension(context_name, 1, beep_exten, 2, "", "", |
| 492 |
"Playback", "beep", NULL, AST_MODULE); |
| 493 |
res |= ast_add_extension(context_name, 1, beep_exten, 3, "", "", |
| 494 |
"Hangup", "", NULL, AST_MODULE); |
| 495 |
|
| 496 |
res |= ast_custom_function_register_escalating(&hook_function, AST_CFE_BOTH); |
| 497 |
|
| 498 |
if (res) { |
| 499 |
unload_module(); |
| 500 |
return AST_MODULE_LOAD_DECLINE; |
| 501 |
} |
| 502 |
return AST_MODULE_LOAD_SUCCESS; |
| 503 |
} |
| 504 |
|
| 505 |
int AST_OPTIONAL_API_NAME(ast_beep_start)(struct ast_channel *chan, |
| 506 |
unsigned int interval, char *beep_id, size_t len) |
| 507 |
{ |
| 508 |
char args[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 32]; |
| 509 |
|
| 510 |
snprintf(args, sizeof(args), "%s,%s,%u", |
| 511 |
context_name, beep_exten, interval); |
| 512 |
|
| 513 |
if (hook_read(chan, NULL, args, beep_id, len)) { |
| 514 |
ast_log(LOG_WARNING, "Failed to enable periodic beep.\n"); |
| 515 |
return -1; |
| 516 |
} |
| 517 |
|
| 518 |
return 0; |
| 519 |
} |
| 520 |
|
| 521 |
int AST_OPTIONAL_API_NAME(ast_beep_stop)(struct ast_channel *chan, const char *beep_id) |
| 522 |
{ |
| 523 |
return hook_write(chan, NULL, (char *) beep_id, "off"); |
| 524 |
} |
| 525 |
|
| 526 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Periodic dialplan hooks.", |
| 527 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 528 |
.load = load_module, |
| 529 |
.unload = unload_module, |
| 530 |
.requires = "app_chanspy,func_cut,func_groupcount,func_uri", |
| 531 |
); |