func_periodic_hook.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Russell Bryant
  5. *
  6. * Russell Bryant <russell@russellbryant.net>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief Periodic dialplan hooks.
  21. *
  22. * \author Russell Bryant <russell@russellbryant.net>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <depend>app_chanspy</depend>
  28. <depend>func_cut</depend>
  29. <depend>func_groupcount</depend>
  30. <depend>func_uri</depend>
  31. <support_level>core</support_level>
  32. ***/
  33. #include "asterisk.h"
  34. #include "asterisk/module.h"
  35. #include "asterisk/channel.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/audiohook.h"
  39. #include "asterisk/test.h"
  40. #define AST_API_MODULE
  41. #include "asterisk/beep.h"
  42. /*** DOCUMENTATION
  43. <function name="PERIODIC_HOOK" language="en_US">
  44. <synopsis>
  45. Execute a periodic dialplan hook into the audio of a call.
  46. </synopsis>
  47. <syntax>
  48. <parameter name="context" required="true">
  49. <para>(On Read Only) Context for the hook extension.</para>
  50. </parameter>
  51. <parameter name="extension" required="true">
  52. <para>(On Read Only) The hook extension.</para>
  53. </parameter>
  54. <parameter name="interval" required="true">
  55. <para>(On Read Only) Number of seconds in between hook runs.
  56. Whole seconds only.</para>
  57. </parameter>
  58. <parameter name="hook_id" required="true">
  59. <para>(On Write Only) The hook ID.</para>
  60. </parameter>
  61. </syntax>
  62. <description>
  63. <para>For example, you could use this function to enable playing
  64. a periodic <literal>beep</literal> sound in a call.</para>
  65. <para/>
  66. <example title="To turn on">
  67. same => n,Set(BEEPID=${PERIODIC_HOOK(hooks,beep,180)})
  68. </example>
  69. <example title="To turn off">
  70. same => n,Set(PERIODIC_HOOK(${BEEPID})=off)
  71. </example>
  72. <example title="To turn back on again later">
  73. same => n,Set(PERIODIC_HOOK(${BEEPID})=on)
  74. </example>
  75. <para>It is important to note that the hook does not actually
  76. run on the channel itself. It runs asynchronously on a new channel.
  77. Any audio generated by the hook gets injected into the call for
  78. the channel PERIODIC_HOOK() was set on.</para>
  79. <para/>
  80. <para>The hook dialplan will have two variables available.
  81. <variable>HOOK_CHANNEL</variable> is the channel the hook is
  82. enabled on. <variable>HOOK_ID</variable> is the hook ID for
  83. enabling or disabling the hook.</para>
  84. </description>
  85. </function>
  86. ***/
  87. static const char context_name[] = "__func_periodic_hook_context__";
  88. static const char exten_name[] = "hook";
  89. static const char full_exten_name[] = "hook@__func_periodic_hook_context__";
  90. static const char beep_exten[] = "beep";
  91. /*!
  92. * \brief Last used hook ID
  93. *
  94. * This is incremented each time a hook is created to give each hook a unique
  95. * ID.
  96. */
  97. static unsigned int global_hook_id;
  98. /*! State put in a datastore to track the state of the hook */
  99. struct hook_state {
  100. /*!
  101. * \brief audiohook used as a callback into this module
  102. *
  103. * \note The code assumes this is the first element in the struct
  104. */
  105. struct ast_audiohook audiohook;
  106. /*! Seconds between each hook run */
  107. unsigned int interval;
  108. /*! The last time the hook ran */
  109. struct timeval last_hook;
  110. /*! Dialplan context for the hook */
  111. char *context;
  112. /*! Dialplan extension for the hook */
  113. char *exten;
  114. /*! Hook ID */
  115. unsigned int hook_id;
  116. /*! Non-zero if the hook is currently disabled */
  117. unsigned char disabled;
  118. };
  119. static void hook_datastore_destroy_callback(void *data)
  120. {
  121. struct hook_state *state = data;
  122. ast_audiohook_lock(&state->audiohook);
  123. ast_audiohook_detach(&state->audiohook);
  124. ast_audiohook_unlock(&state->audiohook);
  125. ast_audiohook_destroy(&state->audiohook);
  126. ast_free(state->context);
  127. ast_free(state->exten);
  128. ast_free(state);
  129. }
  130. static const struct ast_datastore_info hook_datastore = {
  131. .type = AST_MODULE,
  132. .destroy = hook_datastore_destroy_callback,
  133. };
  134. /*! Arguments to the thread that launches the hook */
  135. struct hook_thread_arg {
  136. /*! Hook ID */
  137. char *hook_id;
  138. /*! Name of the channel the hook was set on */
  139. char *chan_name;
  140. /*! Dialplan context for the hook */
  141. char *context;
  142. /*! Dialplan extension for the hook */
  143. char *exten;
  144. };
  145. static void hook_thread_arg_destroy(struct hook_thread_arg *arg)
  146. {
  147. ast_free(arg->hook_id);
  148. ast_free(arg->chan_name);
  149. ast_free(arg->context);
  150. ast_free(arg->exten);
  151. ast_free(arg);
  152. }
  153. static void *hook_launch_thread(void *data)
  154. {
  155. struct hook_thread_arg *arg = data;
  156. struct ast_variable hook_id = {
  157. .name = "HOOK_ID",
  158. .value = arg->hook_id,
  159. };
  160. struct ast_variable chan_name_var = {
  161. .name = "HOOK_CHANNEL",
  162. .value = arg->chan_name,
  163. .next = &hook_id,
  164. };
  165. ast_pbx_outgoing_exten("Local", NULL, full_exten_name, 60,
  166. arg->context, arg->exten, 1, NULL, AST_OUTGOING_NO_WAIT,
  167. NULL, NULL, &chan_name_var, NULL, NULL, 1, NULL);
  168. hook_thread_arg_destroy(arg);
  169. return NULL;
  170. }
  171. static struct hook_thread_arg *hook_thread_arg_alloc(struct ast_channel *chan,
  172. struct hook_state *state)
  173. {
  174. struct hook_thread_arg *arg;
  175. if (!(arg = ast_calloc(1, sizeof(*arg)))) {
  176. return NULL;
  177. }
  178. ast_channel_lock(chan);
  179. arg->chan_name = ast_strdup(ast_channel_name(chan));
  180. ast_channel_unlock(chan);
  181. if (!arg->chan_name) {
  182. hook_thread_arg_destroy(arg);
  183. return NULL;
  184. }
  185. if (ast_asprintf(&arg->hook_id, "%u", state->hook_id) == -1) {
  186. hook_thread_arg_destroy(arg);
  187. return NULL;
  188. }
  189. if (!(arg->context = ast_strdup(state->context))) {
  190. hook_thread_arg_destroy(arg);
  191. return NULL;
  192. }
  193. if (!(arg->exten = ast_strdup(state->exten))) {
  194. hook_thread_arg_destroy(arg);
  195. return NULL;
  196. }
  197. return arg;
  198. }
  199. static int do_hook(struct ast_channel *chan, struct hook_state *state)
  200. {
  201. pthread_t t;
  202. struct hook_thread_arg *arg;
  203. int res;
  204. if (!(arg = hook_thread_arg_alloc(chan, state))) {
  205. return -1;
  206. }
  207. /*
  208. * We don't want to block normal frame processing *at all* while we kick
  209. * this off, so do it in a new thread.
  210. */
  211. res = ast_pthread_create_detached_background(&t, NULL, hook_launch_thread, arg);
  212. if (res != 0) {
  213. hook_thread_arg_destroy(arg);
  214. }
  215. return res;
  216. }
  217. static int hook_callback(struct ast_audiohook *audiohook, struct ast_channel *chan,
  218. struct ast_frame *frame, enum ast_audiohook_direction direction)
  219. {
  220. struct hook_state *state = (struct hook_state *) audiohook; /* trust me. */
  221. struct timeval now;
  222. int res = 0;
  223. if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || state->disabled) {
  224. return 0;
  225. }
  226. now = ast_tvnow();
  227. if (ast_tvdiff_ms(now, state->last_hook) > state->interval * 1000) {
  228. if ((res = do_hook(chan, state))) {
  229. const char *name;
  230. ast_channel_lock(chan);
  231. name = ast_strdupa(ast_channel_name(chan));
  232. ast_channel_unlock(chan);
  233. ast_log(LOG_WARNING, "Failed to run hook on '%s'\n", name);
  234. }
  235. state->last_hook = now;
  236. }
  237. return res;
  238. }
  239. static struct hook_state *hook_state_alloc(const char *context, const char *exten,
  240. unsigned int interval, unsigned int hook_id)
  241. {
  242. struct hook_state *state;
  243. if (!(state = ast_calloc(1, sizeof(*state)))) {
  244. return NULL;
  245. }
  246. state->context = ast_strdup(context);
  247. state->exten = ast_strdup(exten);
  248. state->interval = interval;
  249. state->hook_id = hook_id;
  250. ast_audiohook_init(&state->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE,
  251. AST_MODULE, AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
  252. state->audiohook.manipulate_callback = hook_callback;
  253. return state;
  254. }
  255. static int init_hook(struct ast_channel *chan, const char *context, const char *exten,
  256. unsigned int interval, unsigned int hook_id)
  257. {
  258. struct hook_state *state;
  259. struct ast_datastore *datastore;
  260. char uid[32];
  261. snprintf(uid, sizeof(uid), "%u", hook_id);
  262. if (!(datastore = ast_datastore_alloc(&hook_datastore, uid))) {
  263. return -1;
  264. }
  265. if (!(state = hook_state_alloc(context, exten, interval, hook_id))) {
  266. ast_datastore_free(datastore);
  267. return -1;
  268. }
  269. datastore->data = state;
  270. ast_channel_lock(chan);
  271. ast_channel_datastore_add(chan, datastore);
  272. ast_audiohook_attach(chan, &state->audiohook);
  273. ast_channel_unlock(chan);
  274. return 0;
  275. }
  276. static int hook_on(struct ast_channel *chan, const char *data, unsigned int hook_id)
  277. {
  278. char *parse = ast_strdupa(S_OR(data, ""));
  279. AST_DECLARE_APP_ARGS(args,
  280. AST_APP_ARG(context);
  281. AST_APP_ARG(exten);
  282. AST_APP_ARG(interval);
  283. );
  284. unsigned int interval;
  285. AST_STANDARD_APP_ARGS(args, parse);
  286. if (ast_strlen_zero(args.interval) ||
  287. sscanf(args.interval, "%30u", &interval) != 1 || interval == 0) {
  288. ast_log(LOG_WARNING, "Invalid hook interval: '%s'\n", S_OR(args.interval, ""));
  289. return -1;
  290. }
  291. if (ast_strlen_zero(args.context) || ast_strlen_zero(args.exten)) {
  292. ast_log(LOG_WARNING, "A context and extension are required for PERIODIC_HOOK().\n");
  293. return -1;
  294. }
  295. ast_debug(1, "hook to %s@%s enabled on %s with interval of %u seconds\n",
  296. args.exten, args.context, ast_channel_name(chan), interval);
  297. ast_test_suite_event_notify("PERIODIC_HOOK_ENABLED", "Exten: %s\r\nChannel: %s\r\nInterval: %u\r\n",
  298. args.exten, ast_channel_name(chan), interval);
  299. return init_hook(chan, args.context, args.exten, interval, hook_id);
  300. }
  301. static int hook_off(struct ast_channel *chan, const char *hook_id)
  302. {
  303. struct ast_datastore *datastore;
  304. struct hook_state *state;
  305. if (ast_strlen_zero(hook_id)) {
  306. return -1;
  307. }
  308. ast_channel_lock(chan);
  309. if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, hook_id))) {
  310. ast_log(LOG_WARNING, "Hook with ID '%s' not found on channel '%s'\n", hook_id,
  311. ast_channel_name(chan));
  312. ast_channel_unlock(chan);
  313. return -1;
  314. }
  315. state = datastore->data;
  316. state->disabled = 1;
  317. ast_channel_unlock(chan);
  318. return 0;
  319. }
  320. static int hook_read(struct ast_channel *chan, const char *cmd, char *data,
  321. char *buf, size_t len)
  322. {
  323. unsigned int hook_id;
  324. if (!chan) {
  325. return -1;
  326. }
  327. hook_id = (unsigned int) ast_atomic_fetchadd_int((int *) &global_hook_id, 1);
  328. snprintf(buf, len, "%u", hook_id);
  329. return hook_on(chan, data, hook_id);
  330. }
  331. static int hook_re_enable(struct ast_channel *chan, const char *uid)
  332. {
  333. struct ast_datastore *datastore;
  334. struct hook_state *state;
  335. if (ast_strlen_zero(uid)) {
  336. return -1;
  337. }
  338. ast_channel_lock(chan);
  339. if (!(datastore = ast_channel_datastore_find(chan, &hook_datastore, uid))) {
  340. ast_log(LOG_WARNING, "Hook with ID '%s' not found on '%s'\n",
  341. uid, ast_channel_name(chan));
  342. ast_channel_unlock(chan);
  343. return -1;
  344. }
  345. state = datastore->data;
  346. state->disabled = 0;
  347. ast_channel_unlock(chan);
  348. return 0;
  349. }
  350. static int hook_write(struct ast_channel *chan, const char *cmd, char *data,
  351. const char *value)
  352. {
  353. int res;
  354. if (!chan) {
  355. return -1;
  356. }
  357. if (ast_false(value)) {
  358. res = hook_off(chan, data);
  359. } else if (ast_true(value)) {
  360. res = hook_re_enable(chan, data);
  361. } else {
  362. ast_log(LOG_WARNING, "Invalid value for PERIODIC_HOOK function: '%s'\n", value);
  363. res = -1;
  364. }
  365. return res;
  366. }
  367. static struct ast_custom_function hook_function = {
  368. .name = "PERIODIC_HOOK",
  369. .read = hook_read,
  370. .write = hook_write,
  371. };
  372. static int unload_module(void)
  373. {
  374. ast_context_destroy(NULL, AST_MODULE);
  375. ast_custom_function_unregister(&hook_function);
  376. return 0;
  377. }
  378. static int load_module(void)
  379. {
  380. int res;
  381. if (!ast_context_find_or_create(NULL, NULL, context_name, AST_MODULE)) {
  382. ast_log(LOG_ERROR, "Failed to create %s dialplan context.\n", context_name);
  383. return AST_MODULE_LOAD_DECLINE;
  384. }
  385. /*
  386. * Based on a handy recipe from the Asterisk Cookbook.
  387. */
  388. res = ast_add_extension(context_name, 1, exten_name, 1, "", "",
  389. "Set", "EncodedChannel=${HOOK_CHANNEL}",
  390. NULL, AST_MODULE);
  391. res |= ast_add_extension(context_name, 1, exten_name, 2, "", "",
  392. "Set", "GROUP_NAME=${EncodedChannel}${HOOK_ID}",
  393. NULL, AST_MODULE);
  394. res |= ast_add_extension(context_name, 1, exten_name, 3, "", "",
  395. "Set", "GROUP(periodic-hook)=${GROUP_NAME}",
  396. NULL, AST_MODULE);
  397. res |= ast_add_extension(context_name, 1, exten_name, 4, "", "", "ExecIf",
  398. "$[${GROUP_COUNT(${GROUP_NAME}@periodic-hook)} > 1]?Hangup()",
  399. NULL, AST_MODULE);
  400. res |= ast_add_extension(context_name, 1, exten_name, 5, "", "",
  401. "Set", "ChannelToSpy=${URIDECODE(${EncodedChannel})}",
  402. NULL, AST_MODULE);
  403. res |= ast_add_extension(context_name, 1, exten_name, 6, "", "",
  404. "ChanSpy", "${ChannelToSpy},qEB", NULL, AST_MODULE);
  405. res |= ast_add_extension(context_name, 1, beep_exten, 1, "", "",
  406. "Answer", "", NULL, AST_MODULE);
  407. res |= ast_add_extension(context_name, 1, beep_exten, 2, "", "",
  408. "Playback", "beep", NULL, AST_MODULE);
  409. res |= ast_add_extension(context_name, 1, beep_exten, 3, "", "",
  410. "Hangup", "", NULL, AST_MODULE);
  411. res |= ast_custom_function_register_escalating(&hook_function, AST_CFE_BOTH);
  412. if (res) {
  413. unload_module();
  414. return AST_MODULE_LOAD_DECLINE;
  415. }
  416. return AST_MODULE_LOAD_SUCCESS;
  417. }
  418. int AST_OPTIONAL_API_NAME(ast_beep_start)(struct ast_channel *chan,
  419. unsigned int interval, char *beep_id, size_t len)
  420. {
  421. char args[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 32];
  422. snprintf(args, sizeof(args), "%s,%s,%u",
  423. context_name, beep_exten, interval);
  424. if (hook_read(chan, NULL, args, beep_id, len)) {
  425. ast_log(LOG_WARNING, "Failed to enable periodic beep.\n");
  426. return -1;
  427. }
  428. return 0;
  429. }
  430. int AST_OPTIONAL_API_NAME(ast_beep_stop)(struct ast_channel *chan, const char *beep_id)
  431. {
  432. return hook_write(chan, NULL, (char *) beep_id, "off");
  433. }
  434. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS, "Periodic dialplan hooks.",
  435. .support_level = AST_MODULE_SUPPORT_CORE,
  436. .load = load_module,
  437. .unload = unload_module,
  438. .requires = "app_chanspy,func_cut,func_groupcount,func_uri",
  439. );