control.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * David M. Lee, II <dlee@digium.com>
  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 Stasis application control support.
  21. *
  22. * \author David M. Lee, II <dlee@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "asterisk/stasis_channels.h"
  27. #include "command.h"
  28. #include "control.h"
  29. #include "app.h"
  30. #include "asterisk/dial.h"
  31. #include "asterisk/bridge.h"
  32. #include "asterisk/bridge_after.h"
  33. #include "asterisk/bridge_basic.h"
  34. #include "asterisk/frame.h"
  35. #include "asterisk/pbx.h"
  36. #include "asterisk/musiconhold.h"
  37. #include "asterisk/app.h"
  38. AST_LIST_HEAD(app_control_rules, stasis_app_control_rule);
  39. struct stasis_app_control {
  40. ast_cond_t wait_cond;
  41. /*! Queue of commands to dispatch on the channel */
  42. struct ao2_container *command_queue;
  43. /*!
  44. * The associated channel.
  45. * Be very careful with the threading associated w/ manipulating
  46. * the channel.
  47. */
  48. struct ast_channel *channel;
  49. /*!
  50. * When a channel is in a bridge, the bridge that it is in.
  51. */
  52. struct ast_bridge *bridge;
  53. /*!
  54. * Holding place for channel's PBX while imparted to a bridge.
  55. */
  56. struct ast_pbx *pbx;
  57. /*!
  58. * A list of rules to check before adding a channel to a bridge.
  59. */
  60. struct app_control_rules add_rules;
  61. /*!
  62. * A list of rules to check before removing a channel from a bridge.
  63. */
  64. struct app_control_rules remove_rules;
  65. /*!
  66. * Silence generator, when silence is being generated.
  67. */
  68. struct ast_silence_generator *silgen;
  69. /*!
  70. * The app for which this control was created
  71. */
  72. struct stasis_app *app;
  73. /*!
  74. * When set, /c app_stasis should exit and continue in the dialplan.
  75. */
  76. int is_done:1;
  77. };
  78. static void control_dtor(void *obj)
  79. {
  80. struct stasis_app_control *control = obj;
  81. ao2_cleanup(control->command_queue);
  82. ast_channel_cleanup(control->channel);
  83. ao2_cleanup(control->app);
  84. ast_cond_destroy(&control->wait_cond);
  85. AST_LIST_HEAD_DESTROY(&control->add_rules);
  86. AST_LIST_HEAD_DESTROY(&control->remove_rules);
  87. }
  88. struct stasis_app_control *control_create(struct ast_channel *channel, struct stasis_app *app)
  89. {
  90. struct stasis_app_control *control;
  91. int res;
  92. control = ao2_alloc(sizeof(*control), control_dtor);
  93. if (!control) {
  94. return NULL;
  95. }
  96. AST_LIST_HEAD_INIT(&control->add_rules);
  97. AST_LIST_HEAD_INIT(&control->remove_rules);
  98. res = ast_cond_init(&control->wait_cond, NULL);
  99. if (res != 0) {
  100. ast_log(LOG_ERROR, "Error initializing ast_cond_t: %s\n",
  101. strerror(errno));
  102. ao2_ref(control, -1);
  103. return NULL;
  104. }
  105. control->app = ao2_bump(app);
  106. ast_channel_ref(channel);
  107. control->channel = channel;
  108. control->command_queue = ao2_container_alloc_list(
  109. AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL, NULL);
  110. if (!control->command_queue) {
  111. ao2_ref(control, -1);
  112. return NULL;
  113. }
  114. return control;
  115. }
  116. static void app_control_register_rule(
  117. const struct stasis_app_control *control,
  118. struct app_control_rules *list, struct stasis_app_control_rule *obj)
  119. {
  120. ao2_lock(control->command_queue);
  121. AST_LIST_INSERT_TAIL(list, obj, next);
  122. ao2_unlock(control->command_queue);
  123. }
  124. static void app_control_unregister_rule(
  125. const struct stasis_app_control *control,
  126. struct app_control_rules *list, struct stasis_app_control_rule *obj)
  127. {
  128. struct stasis_app_control_rule *rule;
  129. ao2_lock(control->command_queue);
  130. AST_RWLIST_TRAVERSE_SAFE_BEGIN(list, rule, next) {
  131. if (rule == obj) {
  132. AST_RWLIST_REMOVE_CURRENT(next);
  133. break;
  134. }
  135. }
  136. AST_RWLIST_TRAVERSE_SAFE_END;
  137. ao2_unlock(control->command_queue);
  138. }
  139. /*!
  140. * \internal
  141. * \brief Checks to make sure each rule in the given list passes.
  142. *
  143. * \details Loops over a list of rules checking for rejections or failures.
  144. * If one rule fails its resulting error code is returned.
  145. *
  146. * \note Command queue should be locked before calling this function.
  147. *
  148. * \param control The stasis application control
  149. * \param list The list of rules to check
  150. *
  151. * \retval 0 if all rules pass
  152. * \retval non-zero error code if a rule fails
  153. */
  154. static enum stasis_app_control_channel_result app_control_check_rules(
  155. const struct stasis_app_control *control,
  156. struct app_control_rules *list)
  157. {
  158. int res = 0;
  159. struct stasis_app_control_rule *rule;
  160. AST_LIST_TRAVERSE(list, rule, next) {
  161. if ((res = rule->check_rule(control))) {
  162. return res;
  163. }
  164. }
  165. return res;
  166. }
  167. void stasis_app_control_register_add_rule(
  168. struct stasis_app_control *control,
  169. struct stasis_app_control_rule *rule)
  170. {
  171. return app_control_register_rule(control, &control->add_rules, rule);
  172. }
  173. void stasis_app_control_unregister_add_rule(
  174. struct stasis_app_control *control,
  175. struct stasis_app_control_rule *rule)
  176. {
  177. app_control_unregister_rule(control, &control->add_rules, rule);
  178. }
  179. void stasis_app_control_register_remove_rule(
  180. struct stasis_app_control *control,
  181. struct stasis_app_control_rule *rule)
  182. {
  183. return app_control_register_rule(control, &control->remove_rules, rule);
  184. }
  185. void stasis_app_control_unregister_remove_rule(
  186. struct stasis_app_control *control,
  187. struct stasis_app_control_rule *rule)
  188. {
  189. app_control_unregister_rule(control, &control->remove_rules, rule);
  190. }
  191. static int app_control_can_add_channel_to_bridge(
  192. struct stasis_app_control *control)
  193. {
  194. return app_control_check_rules(control, &control->add_rules);
  195. }
  196. static int app_control_can_remove_channel_from_bridge(
  197. struct stasis_app_control *control)
  198. {
  199. return app_control_check_rules(control, &control->remove_rules);
  200. }
  201. static int noop_cb(struct stasis_app_control *control,
  202. struct ast_channel *chan, void *data)
  203. {
  204. return 0;
  205. }
  206. /*! Callback type to see if the command can execute
  207. note: command_queue is locked during callback */
  208. typedef int (*app_command_can_exec_cb)(struct stasis_app_control *control);
  209. static struct stasis_app_command *exec_command_on_condition(
  210. struct stasis_app_control *control, stasis_app_command_cb command_fn,
  211. void *data, command_data_destructor_fn data_destructor,
  212. app_command_can_exec_cb can_exec_fn)
  213. {
  214. int retval;
  215. struct stasis_app_command *command;
  216. command_fn = command_fn ? : noop_cb;
  217. command = command_create(command_fn, data, data_destructor);
  218. if (!command) {
  219. return NULL;
  220. }
  221. ao2_lock(control->command_queue);
  222. if (control->is_done) {
  223. ao2_unlock(control->command_queue);
  224. ao2_ref(command, -1);
  225. return NULL;
  226. }
  227. if (can_exec_fn && (retval = can_exec_fn(control))) {
  228. ao2_unlock(control->command_queue);
  229. command_complete(command, retval);
  230. return command;
  231. }
  232. ao2_link_flags(control->command_queue, command, OBJ_NOLOCK);
  233. ast_cond_signal(&control->wait_cond);
  234. ao2_unlock(control->command_queue);
  235. return command;
  236. }
  237. static struct stasis_app_command *exec_command(
  238. struct stasis_app_control *control, stasis_app_command_cb command_fn,
  239. void *data, command_data_destructor_fn data_destructor)
  240. {
  241. return exec_command_on_condition(control, command_fn, data, data_destructor, NULL);
  242. }
  243. struct stasis_app_control_dial_data {
  244. char endpoint[AST_CHANNEL_NAME];
  245. int timeout;
  246. };
  247. static int app_control_dial(struct stasis_app_control *control,
  248. struct ast_channel *chan, void *data)
  249. {
  250. RAII_VAR(struct ast_dial *, dial, ast_dial_create(), ast_dial_destroy);
  251. struct stasis_app_control_dial_data *dial_data = data;
  252. enum ast_dial_result res;
  253. char *tech, *resource;
  254. struct ast_channel *new_chan;
  255. RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
  256. tech = dial_data->endpoint;
  257. if (!(resource = strchr(tech, '/'))) {
  258. return -1;
  259. }
  260. *resource++ = '\0';
  261. if (!dial) {
  262. ast_log(LOG_ERROR, "Failed to create dialing structure.\n");
  263. return -1;
  264. }
  265. if (ast_dial_append(dial, tech, resource, NULL) < 0) {
  266. ast_log(LOG_ERROR, "Failed to add %s/%s to dialing structure.\n", tech, resource);
  267. return -1;
  268. }
  269. ast_dial_set_global_timeout(dial, dial_data->timeout);
  270. res = ast_dial_run(dial, NULL, 0);
  271. if (res != AST_DIAL_RESULT_ANSWERED || !(new_chan = ast_dial_answered_steal(dial))) {
  272. return -1;
  273. }
  274. if (!(bridge = ast_bridge_basic_new())) {
  275. ast_log(LOG_ERROR, "Failed to create basic bridge.\n");
  276. return -1;
  277. }
  278. if (ast_bridge_impart(bridge, new_chan, NULL, NULL,
  279. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  280. ast_hangup(new_chan);
  281. } else {
  282. control_swap_channel_in_bridge(control, bridge, chan, NULL);
  283. }
  284. return 0;
  285. }
  286. int stasis_app_control_dial(struct stasis_app_control *control, const char *endpoint, const char *exten, const char *context,
  287. int timeout)
  288. {
  289. struct stasis_app_control_dial_data *dial_data;
  290. if (!(dial_data = ast_calloc(1, sizeof(*dial_data)))) {
  291. return -1;
  292. }
  293. if (!ast_strlen_zero(endpoint)) {
  294. ast_copy_string(dial_data->endpoint, endpoint, sizeof(dial_data->endpoint));
  295. } else if (!ast_strlen_zero(exten) && !ast_strlen_zero(context)) {
  296. snprintf(dial_data->endpoint, sizeof(dial_data->endpoint), "Local/%s@%s", exten, context);
  297. } else {
  298. return -1;
  299. }
  300. if (timeout > 0) {
  301. dial_data->timeout = timeout * 1000;
  302. } else if (timeout == -1) {
  303. dial_data->timeout = -1;
  304. } else {
  305. dial_data->timeout = 30000;
  306. }
  307. stasis_app_send_command_async(control, app_control_dial, dial_data, ast_free_ptr);
  308. return 0;
  309. }
  310. static int app_control_add_role(struct stasis_app_control *control,
  311. struct ast_channel *chan, void *data)
  312. {
  313. char *role = data;
  314. return ast_channel_add_bridge_role(chan, role);
  315. }
  316. int stasis_app_control_add_role(struct stasis_app_control *control, const char *role)
  317. {
  318. char *role_dup;
  319. role_dup = ast_strdup(role);
  320. if (!role_dup) {
  321. return -1;
  322. }
  323. stasis_app_send_command_async(control, app_control_add_role, role_dup, ast_free_ptr);
  324. return 0;
  325. }
  326. static int app_control_clear_roles(struct stasis_app_control *control,
  327. struct ast_channel *chan, void *data)
  328. {
  329. ast_channel_clear_bridge_roles(chan);
  330. return 0;
  331. }
  332. void stasis_app_control_clear_roles(struct stasis_app_control *control)
  333. {
  334. stasis_app_send_command_async(control, app_control_clear_roles, NULL, NULL);
  335. }
  336. int control_command_count(struct stasis_app_control *control)
  337. {
  338. return ao2_container_count(control->command_queue);
  339. }
  340. int control_is_done(struct stasis_app_control *control)
  341. {
  342. /* Called from stasis_app_exec thread; no lock needed */
  343. return control->is_done;
  344. }
  345. void control_mark_done(struct stasis_app_control *control)
  346. {
  347. /* Locking necessary to sync with other threads adding commands to the queue. */
  348. ao2_lock(control->command_queue);
  349. control->is_done = 1;
  350. ao2_unlock(control->command_queue);
  351. }
  352. struct stasis_app_control_continue_data {
  353. char context[AST_MAX_CONTEXT];
  354. char extension[AST_MAX_EXTENSION];
  355. int priority;
  356. };
  357. static int app_control_continue(struct stasis_app_control *control,
  358. struct ast_channel *chan, void *data)
  359. {
  360. struct stasis_app_control_continue_data *continue_data = data;
  361. ast_assert(control->channel != NULL);
  362. /* If we're in a Stasis bridge, depart it before going back to the
  363. * dialplan */
  364. if (stasis_app_get_bridge(control)) {
  365. ast_bridge_depart(control->channel);
  366. }
  367. /* Called from stasis_app_exec thread; no lock needed */
  368. ast_explicit_goto(control->channel, continue_data->context, continue_data->extension, continue_data->priority);
  369. control_mark_done(control);
  370. return 0;
  371. }
  372. int stasis_app_control_continue(struct stasis_app_control *control, const char *context, const char *extension, int priority)
  373. {
  374. struct stasis_app_control_continue_data *continue_data;
  375. if (!(continue_data = ast_calloc(1, sizeof(*continue_data)))) {
  376. return -1;
  377. }
  378. ast_copy_string(continue_data->context, S_OR(context, ""), sizeof(continue_data->context));
  379. ast_copy_string(continue_data->extension, S_OR(extension, ""), sizeof(continue_data->extension));
  380. if (priority > 0) {
  381. continue_data->priority = priority;
  382. } else {
  383. continue_data->priority = -1;
  384. }
  385. stasis_app_send_command_async(control, app_control_continue, continue_data, ast_free_ptr);
  386. return 0;
  387. }
  388. static int app_control_redirect(struct stasis_app_control *control,
  389. struct ast_channel *chan, void *data)
  390. {
  391. char *endpoint = data;
  392. int res;
  393. ast_assert(control->channel != NULL);
  394. ast_assert(endpoint != NULL);
  395. res = ast_transfer(control->channel, endpoint);
  396. if (!res) {
  397. ast_log(LOG_NOTICE, "Unsupported transfer requested on channel '%s'\n",
  398. ast_channel_name(control->channel));
  399. return 0;
  400. }
  401. return 0;
  402. }
  403. int stasis_app_control_redirect(struct stasis_app_control *control, const char *endpoint)
  404. {
  405. char *endpoint_data = ast_strdup(endpoint);
  406. if (!endpoint_data) {
  407. return -1;
  408. }
  409. stasis_app_send_command_async(control, app_control_redirect, endpoint_data, ast_free_ptr);
  410. return 0;
  411. }
  412. struct stasis_app_control_dtmf_data {
  413. int before;
  414. int between;
  415. unsigned int duration;
  416. int after;
  417. char dtmf[];
  418. };
  419. static void dtmf_in_bridge(struct ast_channel *chan, struct stasis_app_control_dtmf_data *dtmf_data)
  420. {
  421. if (dtmf_data->before) {
  422. usleep(dtmf_data->before * 1000);
  423. }
  424. ast_dtmf_stream_external(chan, dtmf_data->dtmf, dtmf_data->between, dtmf_data->duration);
  425. if (dtmf_data->after) {
  426. usleep(dtmf_data->after * 1000);
  427. }
  428. }
  429. static void dtmf_no_bridge(struct ast_channel *chan, struct stasis_app_control_dtmf_data *dtmf_data)
  430. {
  431. if (dtmf_data->before) {
  432. ast_safe_sleep(chan, dtmf_data->before);
  433. }
  434. ast_dtmf_stream(chan, NULL, dtmf_data->dtmf, dtmf_data->between, dtmf_data->duration);
  435. if (dtmf_data->after) {
  436. ast_safe_sleep(chan, dtmf_data->after);
  437. }
  438. }
  439. static int app_control_dtmf(struct stasis_app_control *control,
  440. struct ast_channel *chan, void *data)
  441. {
  442. struct stasis_app_control_dtmf_data *dtmf_data = data;
  443. if (ast_channel_state(chan) != AST_STATE_UP) {
  444. ast_indicate(chan, AST_CONTROL_PROGRESS);
  445. }
  446. if (stasis_app_get_bridge(control)) {
  447. dtmf_in_bridge(chan, dtmf_data);
  448. } else {
  449. dtmf_no_bridge(chan, dtmf_data);
  450. }
  451. return 0;
  452. }
  453. int stasis_app_control_dtmf(struct stasis_app_control *control, const char *dtmf, int before, int between, unsigned int duration, int after)
  454. {
  455. struct stasis_app_control_dtmf_data *dtmf_data;
  456. if (!(dtmf_data = ast_calloc(1, sizeof(*dtmf_data) + strlen(dtmf) + 1))) {
  457. return -1;
  458. }
  459. dtmf_data->before = before;
  460. dtmf_data->between = between;
  461. dtmf_data->duration = duration;
  462. dtmf_data->after = after;
  463. strcpy(dtmf_data->dtmf, dtmf);
  464. stasis_app_send_command_async(control, app_control_dtmf, dtmf_data, ast_free_ptr);
  465. return 0;
  466. }
  467. static int app_control_ring(struct stasis_app_control *control,
  468. struct ast_channel *chan, void *data)
  469. {
  470. ast_indicate(control->channel, AST_CONTROL_RINGING);
  471. return 0;
  472. }
  473. int stasis_app_control_ring(struct stasis_app_control *control)
  474. {
  475. stasis_app_send_command_async(control, app_control_ring, NULL, NULL);
  476. return 0;
  477. }
  478. static int app_control_ring_stop(struct stasis_app_control *control,
  479. struct ast_channel *chan, void *data)
  480. {
  481. ast_indicate(control->channel, -1);
  482. return 0;
  483. }
  484. int stasis_app_control_ring_stop(struct stasis_app_control *control)
  485. {
  486. stasis_app_send_command_async(control, app_control_ring_stop, NULL, NULL);
  487. return 0;
  488. }
  489. struct stasis_app_control_mute_data {
  490. enum ast_frame_type frametype;
  491. unsigned int direction;
  492. };
  493. static int app_control_mute(struct stasis_app_control *control,
  494. struct ast_channel *chan, void *data)
  495. {
  496. struct stasis_app_control_mute_data *mute_data = data;
  497. ast_channel_lock(chan);
  498. ast_channel_suppress(control->channel, mute_data->direction, mute_data->frametype);
  499. ast_channel_unlock(chan);
  500. return 0;
  501. }
  502. int stasis_app_control_mute(struct stasis_app_control *control, unsigned int direction, enum ast_frame_type frametype)
  503. {
  504. struct stasis_app_control_mute_data *mute_data;
  505. if (!(mute_data = ast_calloc(1, sizeof(*mute_data)))) {
  506. return -1;
  507. }
  508. mute_data->direction = direction;
  509. mute_data->frametype = frametype;
  510. stasis_app_send_command_async(control, app_control_mute, mute_data, ast_free_ptr);
  511. return 0;
  512. }
  513. static int app_control_unmute(struct stasis_app_control *control,
  514. struct ast_channel *chan, void *data)
  515. {
  516. struct stasis_app_control_mute_data *mute_data = data;
  517. ast_channel_lock(chan);
  518. ast_channel_unsuppress(control->channel, mute_data->direction, mute_data->frametype);
  519. ast_channel_unlock(chan);
  520. return 0;
  521. }
  522. int stasis_app_control_unmute(struct stasis_app_control *control, unsigned int direction, enum ast_frame_type frametype)
  523. {
  524. struct stasis_app_control_mute_data *mute_data;
  525. if (!(mute_data = ast_calloc(1, sizeof(*mute_data)))) {
  526. return -1;
  527. }
  528. mute_data->direction = direction;
  529. mute_data->frametype = frametype;
  530. stasis_app_send_command_async(control, app_control_unmute, mute_data, ast_free_ptr);
  531. return 0;
  532. }
  533. /*!
  534. * \brief structure for queuing ARI channel variable setting
  535. *
  536. * It may seem weird to define this custom structure given that we already have
  537. * ast_var_t and ast_variable defined elsewhere. The problem with those is that
  538. * they are not tolerant of NULL channel variable value pointers. In fact, in both
  539. * cases, the best they could do is to have a zero-length variable value. However,
  540. * when un-setting a channel variable, it is important to pass a NULL value, not
  541. * a zero-length string.
  542. */
  543. struct chanvar {
  544. /*! Name of variable to set/unset */
  545. char *name;
  546. /*! Value of variable to set. If unsetting, this will be NULL */
  547. char *value;
  548. };
  549. static void free_chanvar(void *data)
  550. {
  551. struct chanvar *var = data;
  552. ast_free(var->name);
  553. ast_free(var->value);
  554. ast_free(var);
  555. }
  556. static int app_control_set_channel_var(struct stasis_app_control *control,
  557. struct ast_channel *chan, void *data)
  558. {
  559. struct chanvar *var = data;
  560. pbx_builtin_setvar_helper(control->channel, var->name, var->value);
  561. return 0;
  562. }
  563. int stasis_app_control_set_channel_var(struct stasis_app_control *control, const char *variable, const char *value)
  564. {
  565. struct chanvar *var;
  566. var = ast_calloc(1, sizeof(*var));
  567. if (!var) {
  568. return -1;
  569. }
  570. var->name = ast_strdup(variable);
  571. if (!var->name) {
  572. free_chanvar(var);
  573. return -1;
  574. }
  575. /* It's kosher for value to be NULL. It means the variable is being unset */
  576. if (value) {
  577. var->value = ast_strdup(value);
  578. if (!var->value) {
  579. free_chanvar(var);
  580. return -1;
  581. }
  582. }
  583. stasis_app_send_command_async(control, app_control_set_channel_var, var, free_chanvar);
  584. return 0;
  585. }
  586. static int app_control_hold(struct stasis_app_control *control,
  587. struct ast_channel *chan, void *data)
  588. {
  589. ast_indicate(control->channel, AST_CONTROL_HOLD);
  590. return 0;
  591. }
  592. void stasis_app_control_hold(struct stasis_app_control *control)
  593. {
  594. stasis_app_send_command_async(control, app_control_hold, NULL, NULL);
  595. }
  596. static int app_control_unhold(struct stasis_app_control *control,
  597. struct ast_channel *chan, void *data)
  598. {
  599. ast_indicate(control->channel, AST_CONTROL_UNHOLD);
  600. return 0;
  601. }
  602. void stasis_app_control_unhold(struct stasis_app_control *control)
  603. {
  604. stasis_app_send_command_async(control, app_control_unhold, NULL, NULL);
  605. }
  606. static int app_control_moh_start(struct stasis_app_control *control,
  607. struct ast_channel *chan, void *data)
  608. {
  609. char *moh_class = data;
  610. if (ast_channel_state(chan) != AST_STATE_UP) {
  611. ast_indicate(chan, AST_CONTROL_PROGRESS);
  612. }
  613. ast_moh_start(chan, moh_class, NULL);
  614. return 0;
  615. }
  616. void stasis_app_control_moh_start(struct stasis_app_control *control, const char *moh_class)
  617. {
  618. char *data = NULL;
  619. if (!ast_strlen_zero(moh_class)) {
  620. data = ast_strdup(moh_class);
  621. }
  622. stasis_app_send_command_async(control, app_control_moh_start, data, ast_free_ptr);
  623. }
  624. static int app_control_moh_stop(struct stasis_app_control *control,
  625. struct ast_channel *chan, void *data)
  626. {
  627. ast_moh_stop(chan);
  628. return 0;
  629. }
  630. void stasis_app_control_moh_stop(struct stasis_app_control *control)
  631. {
  632. stasis_app_send_command_async(control, app_control_moh_stop, NULL, NULL);
  633. }
  634. static int app_control_silence_start(struct stasis_app_control *control,
  635. struct ast_channel *chan, void *data)
  636. {
  637. if (ast_channel_state(chan) != AST_STATE_UP) {
  638. ast_indicate(chan, AST_CONTROL_PROGRESS);
  639. }
  640. if (control->silgen) {
  641. /* We have a silence generator, but it may have been implicitly
  642. * disabled by media actions (music on hold, playing media,
  643. * etc.) Just stop it and restart a new one.
  644. */
  645. ast_channel_stop_silence_generator(
  646. control->channel, control->silgen);
  647. }
  648. ast_debug(3, "%s: Starting silence generator\n",
  649. stasis_app_control_get_channel_id(control));
  650. control->silgen = ast_channel_start_silence_generator(control->channel);
  651. if (!control->silgen) {
  652. ast_log(LOG_WARNING,
  653. "%s: Failed to start silence generator.\n",
  654. stasis_app_control_get_channel_id(control));
  655. }
  656. return 0;
  657. }
  658. void stasis_app_control_silence_start(struct stasis_app_control *control)
  659. {
  660. stasis_app_send_command_async(control, app_control_silence_start, NULL, NULL);
  661. }
  662. void control_silence_stop_now(struct stasis_app_control *control)
  663. {
  664. if (control->silgen) {
  665. ast_debug(3, "%s: Stopping silence generator\n",
  666. stasis_app_control_get_channel_id(control));
  667. ast_channel_stop_silence_generator(
  668. control->channel, control->silgen);
  669. control->silgen = NULL;
  670. }
  671. }
  672. static int app_control_silence_stop(struct stasis_app_control *control,
  673. struct ast_channel *chan, void *data)
  674. {
  675. control_silence_stop_now(control);
  676. return 0;
  677. }
  678. void stasis_app_control_silence_stop(struct stasis_app_control *control)
  679. {
  680. stasis_app_send_command_async(control, app_control_silence_stop, NULL, NULL);
  681. }
  682. struct ast_channel_snapshot *stasis_app_control_get_snapshot(
  683. const struct stasis_app_control *control)
  684. {
  685. struct stasis_message *msg;
  686. struct ast_channel_snapshot *snapshot;
  687. msg = stasis_cache_get(ast_channel_cache(), ast_channel_snapshot_type(),
  688. stasis_app_control_get_channel_id(control));
  689. if (!msg) {
  690. return NULL;
  691. }
  692. snapshot = stasis_message_data(msg);
  693. ast_assert(snapshot != NULL);
  694. ao2_ref(snapshot, +1);
  695. ao2_ref(msg, -1);
  696. return snapshot;
  697. }
  698. static int app_send_command_on_condition(struct stasis_app_control *control,
  699. stasis_app_command_cb command_fn, void *data,
  700. command_data_destructor_fn data_destructor,
  701. app_command_can_exec_cb can_exec_fn)
  702. {
  703. int ret;
  704. struct stasis_app_command *command;
  705. if (control == NULL || control->is_done) {
  706. /* If exec_command_on_condition fails, it calls the data_destructor.
  707. * In order to provide consistent behavior, we'll also call the data_destructor
  708. * on this error path. This way, callers never have to call the
  709. * data_destructor themselves.
  710. */
  711. if (data_destructor) {
  712. data_destructor(data);
  713. }
  714. return -1;
  715. }
  716. command = exec_command_on_condition(
  717. control, command_fn, data, data_destructor, can_exec_fn);
  718. if (!command) {
  719. return -1;
  720. }
  721. ret = command_join(command);
  722. ao2_ref(command, -1);
  723. return ret;
  724. }
  725. int stasis_app_send_command(struct stasis_app_control *control,
  726. stasis_app_command_cb command_fn, void *data, command_data_destructor_fn data_destructor)
  727. {
  728. return app_send_command_on_condition(control, command_fn, data, data_destructor, NULL);
  729. }
  730. int stasis_app_send_command_async(struct stasis_app_control *control,
  731. stasis_app_command_cb command_fn, void *data,
  732. command_data_destructor_fn data_destructor)
  733. {
  734. struct stasis_app_command *command;
  735. if (control == NULL || control->is_done) {
  736. /* If exec_command fails, it calls the data_destructor. In order to
  737. * provide consistent behavior, we'll also call the data_destructor
  738. * on this error path. This way, callers never have to call the
  739. * data_destructor themselves.
  740. */
  741. if (data_destructor) {
  742. data_destructor(data);
  743. }
  744. return -1;
  745. }
  746. command = exec_command(control, command_fn, data, data_destructor);
  747. if (!command) {
  748. return -1;
  749. }
  750. ao2_ref(command, -1);
  751. return 0;
  752. }
  753. struct ast_bridge *stasis_app_get_bridge(struct stasis_app_control *control)
  754. {
  755. struct ast_bridge *ret;
  756. if (!control) {
  757. return NULL;
  758. }
  759. ao2_lock(control);
  760. ret = control->bridge;
  761. ao2_unlock(control);
  762. return ret;
  763. }
  764. static int bridge_channel_depart(struct stasis_app_control *control,
  765. struct ast_channel *chan, void *data)
  766. {
  767. struct ast_bridge_channel *bridge_channel;
  768. ast_channel_lock(chan);
  769. bridge_channel = ast_channel_internal_bridge_channel(chan);
  770. ast_channel_unlock(chan);
  771. if (bridge_channel != data) {
  772. ast_debug(3, "%s: Channel is no longer in departable state\n",
  773. ast_channel_uniqueid(chan));
  774. return -1;
  775. }
  776. ast_debug(3, "%s: Channel departing bridge\n",
  777. ast_channel_uniqueid(chan));
  778. ast_bridge_depart(chan);
  779. return 0;
  780. }
  781. static void internal_bridge_after_cb(struct ast_channel *chan, void *data,
  782. enum ast_bridge_after_cb_reason reason)
  783. {
  784. struct stasis_app_control *control = data;
  785. struct ast_bridge_channel *bridge_channel;
  786. ao2_lock(control);
  787. ast_debug(3, "%s, %s: %s\n",
  788. ast_channel_uniqueid(chan), control->bridge ? control->bridge->uniqueid : "unknown",
  789. ast_bridge_after_cb_reason_string(reason));
  790. if (reason == AST_BRIDGE_AFTER_CB_REASON_IMPART_FAILED) {
  791. /* The impart actually failed so control->bridge isn't valid. */
  792. control->bridge = NULL;
  793. }
  794. ast_assert(chan == control->channel);
  795. /* Restore the channel's PBX */
  796. ast_channel_pbx_set(control->channel, control->pbx);
  797. control->pbx = NULL;
  798. if (control->bridge) {
  799. app_unsubscribe_bridge(control->app, control->bridge);
  800. /* No longer in the bridge */
  801. control->bridge = NULL;
  802. /* Get the bridge channel so we don't depart from the wrong bridge */
  803. ast_channel_lock(chan);
  804. bridge_channel = ast_channel_get_bridge_channel(chan);
  805. ast_channel_unlock(chan);
  806. /* Depart this channel from the bridge using the command queue if possible */
  807. stasis_app_send_command_async(control, bridge_channel_depart, bridge_channel, __ao2_cleanup);
  808. }
  809. if (stasis_app_channel_is_stasis_end_published(chan)) {
  810. /* The channel has had a StasisEnd published on it, but until now had remained in
  811. * the bridging system. This means that the channel moved from a Stasis bridge to a
  812. * non-Stasis bridge and is now exiting the bridging system. Because of this, the
  813. * channel needs to exit the Stasis application and go to wherever the non-Stasis
  814. * bridge has directed it to go. If the non-Stasis bridge has not set up an after
  815. * bridge destination, then the channel should be hung up.
  816. */
  817. int hangup_flag;
  818. hangup_flag = ast_bridge_setup_after_goto(chan) ? AST_SOFTHANGUP_DEV : AST_SOFTHANGUP_ASYNCGOTO;
  819. ast_channel_lock(chan);
  820. ast_softhangup_nolock(chan, hangup_flag);
  821. ast_channel_unlock(chan);
  822. }
  823. ao2_unlock(control);
  824. }
  825. static void bridge_after_cb(struct ast_channel *chan, void *data)
  826. {
  827. struct stasis_app_control *control = data;
  828. internal_bridge_after_cb(control->channel, data, AST_BRIDGE_AFTER_CB_REASON_DEPART);
  829. }
  830. static void bridge_after_cb_failed(enum ast_bridge_after_cb_reason reason,
  831. void *data)
  832. {
  833. struct stasis_app_control *control = data;
  834. internal_bridge_after_cb(control->channel, data, reason);
  835. ast_debug(3, " reason: %s\n",
  836. ast_bridge_after_cb_reason_string(reason));
  837. }
  838. int control_swap_channel_in_bridge(struct stasis_app_control *control, struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap)
  839. {
  840. int res;
  841. if (!control || !bridge) {
  842. return -1;
  843. }
  844. ast_debug(3, "%s: Adding to bridge %s\n",
  845. stasis_app_control_get_channel_id(control),
  846. bridge->uniqueid);
  847. ast_assert(chan != NULL);
  848. /* Depart whatever Stasis bridge we're currently in. */
  849. if (stasis_app_get_bridge(control)) {
  850. /* Note that it looks like there's a race condition here, since
  851. * we don't have control locked. But this happens from the
  852. * control callback thread, so there won't be any other
  853. * concurrent attempts to bridge.
  854. */
  855. ast_bridge_depart(chan);
  856. }
  857. res = ast_bridge_set_after_callback(chan, bridge_after_cb,
  858. bridge_after_cb_failed, control);
  859. if (res != 0) {
  860. ast_log(LOG_ERROR, "Error setting after-bridge callback\n");
  861. return -1;
  862. }
  863. ao2_lock(control);
  864. /* Ensure the controlling application is subscribed early enough
  865. * to receive the ChannelEnteredBridge message. This works in concert
  866. * with the subscription handled in the Stasis application execution
  867. * loop */
  868. app_subscribe_bridge(control->app, bridge);
  869. /* Save off the channel's PBX */
  870. ast_assert(control->pbx == NULL);
  871. if (!control->pbx) {
  872. control->pbx = ast_channel_pbx(chan);
  873. ast_channel_pbx_set(chan, NULL);
  874. }
  875. ast_assert(stasis_app_get_bridge(control) == NULL);
  876. /* We need to set control->bridge here since bridge_after_cb may be run
  877. * before ast_bridge_impart returns. bridge_after_cb gets a reason
  878. * code so it can tell if the bridge is actually valid or not.
  879. */
  880. control->bridge = bridge;
  881. /* We can't be holding the control lock while impart is running
  882. * or we could create a deadlock with bridge_after_cb which also
  883. * tries to lock control.
  884. */
  885. ao2_unlock(control);
  886. res = ast_bridge_impart(bridge,
  887. chan,
  888. swap,
  889. NULL, /* features */
  890. AST_BRIDGE_IMPART_CHAN_DEPARTABLE);
  891. if (res != 0) {
  892. /* ast_bridge_impart failed before it could spawn the depart
  893. * thread. The callbacks aren't called in this case.
  894. * The impart could still fail even if ast_bridge_impart returned
  895. * ok but that's handled by bridge_after_cb.
  896. */
  897. ast_log(LOG_ERROR, "Error adding channel to bridge\n");
  898. ao2_lock(control);
  899. ast_channel_pbx_set(chan, control->pbx);
  900. control->pbx = NULL;
  901. control->bridge = NULL;
  902. ao2_unlock(control);
  903. }
  904. return res;
  905. }
  906. int control_add_channel_to_bridge(struct stasis_app_control *control, struct ast_channel *chan, void *data)
  907. {
  908. return control_swap_channel_in_bridge(control, data, chan, NULL);
  909. }
  910. int stasis_app_control_add_channel_to_bridge(
  911. struct stasis_app_control *control, struct ast_bridge *bridge)
  912. {
  913. ast_debug(3, "%s: Sending channel add_to_bridge command\n",
  914. stasis_app_control_get_channel_id(control));
  915. return app_send_command_on_condition(
  916. control, control_add_channel_to_bridge, bridge, NULL,
  917. app_control_can_add_channel_to_bridge);
  918. }
  919. static int app_control_remove_channel_from_bridge(
  920. struct stasis_app_control *control,
  921. struct ast_channel *chan, void *data)
  922. {
  923. struct ast_bridge *bridge = data;
  924. if (!control) {
  925. return -1;
  926. }
  927. /* We should only depart from our own bridge */
  928. ast_debug(3, "%s: Departing bridge %s\n",
  929. stasis_app_control_get_channel_id(control),
  930. bridge->uniqueid);
  931. if (bridge != stasis_app_get_bridge(control)) {
  932. ast_log(LOG_WARNING, "%s: Not in bridge %s; not removing\n",
  933. stasis_app_control_get_channel_id(control),
  934. bridge->uniqueid);
  935. return -1;
  936. }
  937. ast_bridge_depart(chan);
  938. return 0;
  939. }
  940. int stasis_app_control_remove_channel_from_bridge(
  941. struct stasis_app_control *control, struct ast_bridge *bridge)
  942. {
  943. ast_debug(3, "%s: Sending channel remove_from_bridge command\n",
  944. stasis_app_control_get_channel_id(control));
  945. return app_send_command_on_condition(
  946. control, app_control_remove_channel_from_bridge, bridge, NULL,
  947. app_control_can_remove_channel_from_bridge);
  948. }
  949. const char *stasis_app_control_get_channel_id(
  950. const struct stasis_app_control *control)
  951. {
  952. return ast_channel_uniqueid(control->channel);
  953. }
  954. void stasis_app_control_publish(
  955. struct stasis_app_control *control, struct stasis_message *message)
  956. {
  957. if (!control || !control->channel || !message) {
  958. return;
  959. }
  960. stasis_publish(ast_channel_topic(control->channel), message);
  961. }
  962. int stasis_app_control_queue_control(struct stasis_app_control *control,
  963. enum ast_control_frame_type frame_type)
  964. {
  965. return ast_queue_control(control->channel, frame_type);
  966. }
  967. void control_flush_queue(struct stasis_app_control *control)
  968. {
  969. struct ao2_iterator iter;
  970. struct stasis_app_command *command;
  971. iter = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK);
  972. while ((command = ao2_iterator_next(&iter))) {
  973. command_complete(command, -1);
  974. ao2_ref(command, -1);
  975. }
  976. ao2_iterator_destroy(&iter);
  977. }
  978. int control_dispatch_all(struct stasis_app_control *control,
  979. struct ast_channel *chan)
  980. {
  981. int count = 0;
  982. struct ao2_iterator iter;
  983. struct stasis_app_command *command;
  984. ast_assert(control->channel == chan);
  985. iter = ao2_iterator_init(control->command_queue, AO2_ITERATOR_UNLINK);
  986. while ((command = ao2_iterator_next(&iter))) {
  987. command_invoke(command, control, chan);
  988. ao2_ref(command, -1);
  989. ++count;
  990. }
  991. ao2_iterator_destroy(&iter);
  992. return count;
  993. }
  994. void control_wait(struct stasis_app_control *control)
  995. {
  996. if (!control) {
  997. return;
  998. }
  999. ast_assert(control->command_queue != NULL);
  1000. ao2_lock(control->command_queue);
  1001. while (ao2_container_count(control->command_queue) == 0) {
  1002. int res = ast_cond_wait(&control->wait_cond,
  1003. ao2_object_get_lockaddr(control->command_queue));
  1004. if (res < 0) {
  1005. ast_log(LOG_ERROR, "Error waiting on command queue\n");
  1006. break;
  1007. }
  1008. }
  1009. ao2_unlock(control->command_queue);
  1010. }
  1011. int control_prestart_dispatch_all(struct stasis_app_control *control,
  1012. struct ast_channel *chan)
  1013. {
  1014. struct ao2_container *command_queue;
  1015. int count = 0;
  1016. struct ao2_iterator iter;
  1017. struct stasis_app_command *command;
  1018. ast_channel_lock(chan);
  1019. command_queue = command_prestart_get_container(chan);
  1020. ast_channel_unlock(chan);
  1021. if (!command_queue) {
  1022. return 0;
  1023. }
  1024. iter = ao2_iterator_init(command_queue, AO2_ITERATOR_UNLINK);
  1025. while ((command = ao2_iterator_next(&iter))) {
  1026. command_invoke(command, control, chan);
  1027. ao2_cleanup(command);
  1028. ++count;
  1029. }
  1030. ao2_iterator_destroy(&iter);
  1031. ao2_cleanup(command_queue);
  1032. return count;
  1033. }
  1034. struct stasis_app *control_app(struct stasis_app_control *control)
  1035. {
  1036. return control->app;
  1037. }