parking_manager.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@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 Call Parking Manager Actions and Events
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. */
  24. #include "asterisk.h"
  25. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  26. #include "res_parking.h"
  27. #include "asterisk/config.h"
  28. #include "asterisk/config_options.h"
  29. #include "asterisk/utils.h"
  30. #include "asterisk/module.h"
  31. #include "asterisk/cli.h"
  32. #include "asterisk/astobj2.h"
  33. #include "asterisk/features.h"
  34. #include "asterisk/manager.h"
  35. #include "asterisk/bridge.h"
  36. #include "asterisk/module.h"
  37. /*** DOCUMENTATION
  38. <manager name="Parkinglots" language="en_US">
  39. <synopsis>
  40. Get a list of parking lots
  41. </synopsis>
  42. <syntax>
  43. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  44. </syntax>
  45. <description>
  46. <para>List all parking lots as a series of AMI events</para>
  47. </description>
  48. </manager>
  49. <manager name="ParkedCalls" language="en_US">
  50. <synopsis>
  51. List parked calls.
  52. </synopsis>
  53. <syntax>
  54. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  55. <parameter name="ParkingLot">
  56. <para>If specified, only show parked calls from the parking lot with this name.</para>
  57. </parameter>
  58. </syntax>
  59. <description>
  60. <para>List parked calls.</para>
  61. </description>
  62. </manager>
  63. <manager name="Park" language="en_US">
  64. <synopsis>
  65. Park a channel.
  66. </synopsis>
  67. <syntax>
  68. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  69. <parameter name="Channel" required="true">
  70. <para>Channel name to park.</para>
  71. </parameter>
  72. <parameter name="TimeoutChannel" required="false">
  73. <para>Channel name to use when constructing the dial string that will be dialed if the parked channel
  74. times out. If <literal>TimeoutChannel</literal> is in a two party bridge with
  75. <literal>Channel</literal>, then <literal>TimeoutChannel</literal> will receive an announcement and be
  76. treated as having parked <literal>Channel</literal> in the same manner as the Park Call DTMF feature.
  77. </para>
  78. </parameter>
  79. <parameter name="AnnounceChannel" required="false">
  80. <para>If specified, then this channel will receive an announcement when <literal>Channel</literal>
  81. is parked if <literal>AnnounceChannel</literal> is in a state where it can receive announcements
  82. (AnnounceChannel must be bridged). <literal>AnnounceChannel</literal> has no bearing on the actual
  83. state of the parked call.</para>
  84. </parameter>
  85. <parameter name="Timeout" required="false">
  86. <para>Overrides the timeout of the parking lot for this park action. Specified in milliseconds, but will be converted to
  87. seconds. Use a value of 0 to disable the timeout.
  88. </para>
  89. </parameter>
  90. <parameter name="Parkinglot" required="false">
  91. <para>The parking lot to use when parking the channel</para>
  92. </parameter>
  93. </syntax>
  94. <description>
  95. <para>Park an arbitrary channel with optional arguments for specifying the parking lot used, how long
  96. the channel should remain parked, and what dial string to use as the parker if the call times out.
  97. </para>
  98. </description>
  99. </manager>
  100. <managerEvent language="en_US" name="ParkedCall">
  101. <managerEventInstance class="EVENT_FLAG_CALL">
  102. <synopsis>Raised when a channel is parked.</synopsis>
  103. <syntax>
  104. <channel_snapshot prefix="Parkee"/>
  105. <parameter name="ParkerDialString">
  106. <para>Dial String that can be used to call back the parker on ParkingTimeout.</para>
  107. </parameter>
  108. <parameter name="Parkinglot">
  109. <para>Name of the parking lot that the parkee is parked in</para>
  110. </parameter>
  111. <parameter name="ParkingSpace">
  112. <para>Parking Space that the parkee is parked in</para>
  113. </parameter>
  114. <parameter name="ParkingTimeout">
  115. <para>Time remaining until the parkee is forcefully removed from parking in seconds</para>
  116. </parameter>
  117. <parameter name="ParkingDuration">
  118. <para>Time the parkee has been in the parking bridge (in seconds)</para>
  119. </parameter>
  120. </syntax>
  121. </managerEventInstance>
  122. </managerEvent>
  123. <managerEvent language="en_US" name="ParkedCallTimeOut">
  124. <managerEventInstance class="EVENT_FLAG_CALL">
  125. <synopsis>Raised when a channel leaves a parking lot due to reaching the time limit of being parked.</synopsis>
  126. <syntax>
  127. <channel_snapshot prefix="Parkee"/>
  128. <channel_snapshot prefix="Parker"/>
  129. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCall']/managerEventInstance/syntax/parameter)" />
  130. </syntax>
  131. </managerEventInstance>
  132. </managerEvent>
  133. <managerEvent language="en_US" name="ParkedCallGiveUp">
  134. <managerEventInstance class="EVENT_FLAG_CALL">
  135. <synopsis>Raised when a channel leaves a parking lot because it hung up without being answered.</synopsis>
  136. <syntax>
  137. <channel_snapshot prefix="Parkee"/>
  138. <channel_snapshot prefix="Parker"/>
  139. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCall']/managerEventInstance/syntax/parameter)" />
  140. </syntax>
  141. </managerEventInstance>
  142. </managerEvent>
  143. <managerEvent language="en_US" name="UnParkedCall">
  144. <managerEventInstance class="EVENT_FLAG_CALL">
  145. <synopsis>Raised when a channel leaves a parking lot because it was retrieved from the parking lot and reconnected.</synopsis>
  146. <syntax>
  147. <channel_snapshot prefix="Parkee"/>
  148. <channel_snapshot prefix="Parker"/>
  149. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCall']/managerEventInstance/syntax/parameter)" />
  150. <channel_snapshot prefix="Retriever"/>
  151. </syntax>
  152. </managerEventInstance>
  153. </managerEvent>
  154. <managerEvent language="en_US" name="ParkedCallSwap">
  155. <managerEventInstance class="EVENT_FLAG_CALL">
  156. <synopsis>Raised when a channel takes the place of a previously parked channel</synopsis>
  157. <syntax>
  158. <channel_snapshot prefix="Parkee"/>
  159. <channel_snapshot prefix="Parker"/>
  160. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCall']/managerEventInstance/syntax/parameter)" />
  161. </syntax>
  162. <description>
  163. <para>This event is raised when a channel initially parked in the parking lot
  164. is swapped out with a different channel. The most common case for this is when
  165. an attended transfer to a parking lot occurs. The Parkee information in the event
  166. will indicate the party that was swapped into the parking lot.</para>
  167. </description>
  168. </managerEventInstance>
  169. </managerEvent>
  170. ***/
  171. /*! \brief subscription to the parking lot topic */
  172. static struct stasis_subscription *parking_sub;
  173. static struct ast_parked_call_payload *parked_call_payload_from_failure(struct ast_channel *chan)
  174. {
  175. RAII_VAR(struct ast_channel_snapshot *, parkee_snapshot, NULL, ao2_cleanup);
  176. ast_channel_lock(chan);
  177. parkee_snapshot = ast_channel_snapshot_create(chan);
  178. ast_channel_unlock(chan);
  179. if (!parkee_snapshot) {
  180. return NULL;
  181. }
  182. return ast_parked_call_payload_create(PARKED_CALL_FAILED, parkee_snapshot, NULL, NULL, NULL, 0, 0, 0);
  183. }
  184. static struct ast_parked_call_payload *parked_call_payload_from_parked_user(struct parked_user *pu, enum ast_parked_call_event_type event_type)
  185. {
  186. RAII_VAR(struct ast_channel_snapshot *, parkee_snapshot, NULL, ao2_cleanup);
  187. long int timeout;
  188. long int duration;
  189. struct timeval now = ast_tvnow();
  190. const char *lot_name = pu->lot->name;
  191. ast_channel_lock(pu->chan);
  192. parkee_snapshot = ast_channel_snapshot_create(pu->chan);
  193. ast_channel_unlock(pu->chan);
  194. if (!parkee_snapshot) {
  195. return NULL;
  196. }
  197. timeout = pu->start.tv_sec + (long) pu->time_limit - now.tv_sec;
  198. duration = now.tv_sec - pu->start.tv_sec;
  199. return ast_parked_call_payload_create(event_type, parkee_snapshot, pu->parker_dial_string, pu->retriever, lot_name, pu->parking_space, timeout, duration);
  200. }
  201. /*! \brief Builds a manager string based on the contents of a parked call payload */
  202. static struct ast_str *manager_build_parked_call_string(const struct ast_parked_call_payload *payload)
  203. {
  204. struct ast_str *out = ast_str_create(1024);
  205. RAII_VAR(struct ast_str *, parkee_string, NULL, ast_free);
  206. RAII_VAR(struct ast_str *, retriever_string, NULL, ast_free);
  207. if (!out) {
  208. return NULL;
  209. }
  210. parkee_string = ast_manager_build_channel_state_string_prefix(payload->parkee, "Parkee");
  211. if (!parkee_string) {
  212. ast_free(out);
  213. return NULL;
  214. }
  215. if (payload->retriever) {
  216. retriever_string = ast_manager_build_channel_state_string_prefix(payload->retriever, "Retriever");
  217. if (!retriever_string) {
  218. ast_free(out);
  219. return NULL;
  220. }
  221. }
  222. ast_str_set(&out, 0,
  223. "%s" /* parkee channel state */
  224. "%s" /* retriever channel state (when available) */
  225. "ParkerDialString: %s\r\n"
  226. "Parkinglot: %s\r\n"
  227. "ParkingSpace: %u\r\n"
  228. "ParkingTimeout: %lu\r\n"
  229. "ParkingDuration: %lu\r\n",
  230. ast_str_buffer(parkee_string),
  231. retriever_string ? ast_str_buffer(retriever_string) : "",
  232. payload->parker_dial_string,
  233. payload->parkinglot,
  234. payload->parkingspace,
  235. payload->timeout,
  236. payload->duration);
  237. return out;
  238. }
  239. static void manager_parking_status_single_lot(struct mansession *s, const struct message *m, const char *id_text, const char *lot_name)
  240. {
  241. RAII_VAR(struct parking_lot *, curlot, NULL, ao2_cleanup);
  242. struct parked_user *curuser;
  243. struct ao2_iterator iter_users;
  244. int total = 0;
  245. curlot = parking_lot_find_by_name(lot_name);
  246. if (!curlot) {
  247. astman_send_error(s, m, "Requested parking lot could not be found.");
  248. return;
  249. }
  250. astman_send_listack(s, m, "Parked calls will follow", "start");
  251. iter_users = ao2_iterator_init(curlot->parked_users, 0);
  252. while ((curuser = ao2_iterator_next(&iter_users))) {
  253. RAII_VAR(struct ast_parked_call_payload *, payload, NULL, ao2_cleanup);
  254. RAII_VAR(struct ast_str *, parked_call_string, NULL, ast_free);
  255. payload = parked_call_payload_from_parked_user(curuser, PARKED_CALL);
  256. if (!payload) {
  257. ao2_ref(curuser, -1);
  258. break;
  259. }
  260. parked_call_string = manager_build_parked_call_string(payload);
  261. if (!parked_call_string) {
  262. ao2_ref(curuser, -1);
  263. break;
  264. }
  265. total++;
  266. astman_append(s, "Event: ParkedCall\r\n"
  267. "%s" /* The parked call string */
  268. "%s" /* The action ID */
  269. "\r\n",
  270. ast_str_buffer(parked_call_string),
  271. id_text);
  272. ao2_ref(curuser, -1);
  273. }
  274. ao2_iterator_destroy(&iter_users);
  275. astman_send_list_complete_start(s, m, "ParkedCallsComplete", total);
  276. astman_append(s, "Total: %d\r\n", total);
  277. astman_send_list_complete_end(s);
  278. }
  279. static void manager_parking_status_all_lots(struct mansession *s, const struct message *m, const char *id_text)
  280. {
  281. struct parked_user *curuser;
  282. struct ao2_container *lot_container;
  283. struct ao2_iterator iter_lots;
  284. struct ao2_iterator iter_users;
  285. struct parking_lot *curlot;
  286. int total = 0;
  287. lot_container = get_parking_lot_container();
  288. if (!lot_container) {
  289. ast_log(LOG_ERROR, "Failed to obtain parking lot list. Action canceled.\n");
  290. astman_send_error(s, m, "Could not create parking lot list");
  291. return;
  292. }
  293. astman_send_listack(s, m, "Parked calls will follow", "start");
  294. iter_lots = ao2_iterator_init(lot_container, 0);
  295. while ((curlot = ao2_iterator_next(&iter_lots))) {
  296. iter_users = ao2_iterator_init(curlot->parked_users, 0);
  297. while ((curuser = ao2_iterator_next(&iter_users))) {
  298. RAII_VAR(struct ast_parked_call_payload *, payload, NULL, ao2_cleanup);
  299. RAII_VAR(struct ast_str *, parked_call_string, NULL, ast_free);
  300. payload = parked_call_payload_from_parked_user(curuser, PARKED_CALL);
  301. if (!payload) {
  302. ao2_ref(curuser, -1);
  303. ao2_iterator_destroy(&iter_users);
  304. ao2_ref(curlot, -1);
  305. goto abort_list;
  306. }
  307. parked_call_string = manager_build_parked_call_string(payload);
  308. if (!parked_call_string) {
  309. ao2_ref(curuser, -1);
  310. ao2_iterator_destroy(&iter_users);
  311. ao2_ref(curlot, -1);
  312. goto abort_list;
  313. }
  314. total++;
  315. astman_append(s, "Event: ParkedCall\r\n"
  316. "%s" /* The parked call string */
  317. "%s" /* The action ID */
  318. "\r\n",
  319. ast_str_buffer(parked_call_string),
  320. id_text);
  321. ao2_ref(curuser, -1);
  322. }
  323. ao2_iterator_destroy(&iter_users);
  324. ao2_ref(curlot, -1);
  325. }
  326. abort_list:
  327. ao2_iterator_destroy(&iter_lots);
  328. astman_send_list_complete_start(s, m, "ParkedCallsComplete", total);
  329. astman_append(s, "Total: %d\r\n", total);
  330. astman_send_list_complete_end(s);
  331. }
  332. static int manager_parking_status(struct mansession *s, const struct message *m)
  333. {
  334. const char *id = astman_get_header(m, "ActionID");
  335. const char *lot_name = astman_get_header(m, "ParkingLot");
  336. char id_text[256];
  337. id_text[0] = '\0';
  338. if (!ast_strlen_zero(id)) {
  339. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
  340. }
  341. if (!ast_strlen_zero(lot_name)) {
  342. manager_parking_status_single_lot(s, m, id_text, lot_name);
  343. } else {
  344. manager_parking_status_all_lots(s, m, id_text);
  345. }
  346. return 0;
  347. }
  348. struct park_list_data {
  349. const char *id_text;
  350. int count;
  351. };
  352. static int manager_append_event_parking_lot_data_cb(void *obj, void *arg, void *data, int flags)
  353. {
  354. struct parking_lot *curlot = obj;
  355. struct mansession *s = arg;
  356. struct park_list_data *list_data = data;
  357. astman_append(s, "Event: Parkinglot\r\n"
  358. "%s" /* The Action ID */
  359. "Name: %s\r\n"
  360. "StartSpace: %d\r\n"
  361. "StopSpace: %d\r\n"
  362. "Timeout: %u\r\n"
  363. "\r\n",
  364. list_data->id_text,
  365. curlot->name,
  366. curlot->cfg->parking_start,
  367. curlot->cfg->parking_stop,
  368. curlot->cfg->parkingtime);
  369. ++list_data->count;
  370. return 0;
  371. }
  372. static int manager_parking_lot_list(struct mansession *s, const struct message *m)
  373. {
  374. const char *id = astman_get_header(m, "ActionID");
  375. struct ao2_container *lot_container;
  376. char id_text[256];
  377. struct park_list_data list_data;
  378. id_text[0] = '\0';
  379. if (!ast_strlen_zero(id)) {
  380. snprintf(id_text, sizeof(id_text), "ActionID: %s\r\n", id);
  381. }
  382. lot_container = get_parking_lot_container();
  383. if (!lot_container) {
  384. ast_log(LOG_ERROR, "Failed to obtain parking lot list. Action canceled.\n");
  385. astman_send_error(s, m, "Could not create parking lot list");
  386. return 0;
  387. }
  388. astman_send_listack(s, m, "Parking lots will follow", "start");
  389. list_data.id_text = id_text;
  390. list_data.count = 0;
  391. ao2_callback_data(lot_container, OBJ_MULTIPLE | OBJ_NODATA,
  392. manager_append_event_parking_lot_data_cb, s, &list_data);
  393. astman_send_list_complete_start(s, m, "ParkinglotsComplete", list_data.count);
  394. astman_send_list_complete_end(s);
  395. return 0;
  396. }
  397. static void manager_park_unbridged(struct mansession *s, const struct message *m,
  398. struct ast_channel *chan, const char *parkinglot, int timeout_override)
  399. {
  400. struct ast_bridge *parking_bridge = park_common_setup(chan,
  401. chan, parkinglot, NULL, 0, 0, timeout_override, 1);
  402. if (!parking_bridge) {
  403. astman_send_error(s, m, "Park action failed\n");
  404. return;
  405. }
  406. if (ast_bridge_add_channel(parking_bridge, chan, NULL, 0, NULL)) {
  407. astman_send_error(s, m, "Park action failed\n");
  408. ao2_cleanup(parking_bridge);
  409. return;
  410. }
  411. astman_send_ack(s, m, "Park successful\n");
  412. ao2_cleanup(parking_bridge);
  413. }
  414. static void manager_park_bridged(struct mansession *s, const struct message *m,
  415. struct ast_channel *chan, struct ast_channel *parker_chan,
  416. const char *parkinglot, int timeout_override)
  417. {
  418. struct ast_bridge_channel *bridge_channel;
  419. char *app_data;
  420. if (timeout_override != -1) {
  421. if (ast_asprintf(&app_data, "%s,t(%d)", parkinglot, timeout_override) == -1) {
  422. astman_send_error(s, m, "Park action failed\n");
  423. return;
  424. }
  425. } else {
  426. if (ast_asprintf(&app_data, "%s", parkinglot) == -1) {
  427. astman_send_error(s, m, "Park action failed\n");
  428. return;
  429. }
  430. }
  431. ast_channel_lock(parker_chan);
  432. bridge_channel = ast_channel_get_bridge_channel(parker_chan);
  433. ast_channel_unlock(parker_chan);
  434. if (!bridge_channel) {
  435. ast_free(app_data);
  436. astman_send_error(s, m, "Park action failed\n");
  437. return;
  438. }
  439. /* Subscribe to park messages for the channel being parked */
  440. if (create_parked_subscription(parker_chan, ast_channel_uniqueid(chan), 1)) {
  441. ast_free(app_data);
  442. astman_send_error(s, m, "Park action failed\n");
  443. ao2_cleanup(bridge_channel);
  444. return;
  445. }
  446. ast_bridge_channel_write_park(bridge_channel, ast_channel_uniqueid(chan),
  447. ast_channel_uniqueid(parker_chan), app_data);
  448. ast_free(app_data);
  449. astman_send_ack(s, m, "Park successful\n");
  450. ao2_cleanup(bridge_channel);
  451. }
  452. static int manager_park(struct mansession *s, const struct message *m)
  453. {
  454. const char *channel = astman_get_header(m, "Channel");
  455. const char *timeout_channel = S_OR(astman_get_header(m, "TimeoutChannel"), astman_get_header(m, "Channel2"));
  456. const char *announce_channel = astman_get_header(m, "AnnounceChannel");
  457. const char *timeout = astman_get_header(m, "Timeout");
  458. const char *parkinglot = astman_get_header(m, "Parkinglot");
  459. char buf[BUFSIZ];
  460. int timeout_override = -1;
  461. RAII_VAR(struct ast_channel *, parker_chan, NULL, ao2_cleanup);
  462. RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
  463. if (ast_strlen_zero(channel)) {
  464. astman_send_error(s, m, "Channel not specified");
  465. return 0;
  466. }
  467. if (!ast_strlen_zero(timeout)) {
  468. if (sscanf(timeout, "%30d", &timeout_override) != 1 || timeout_override < 0) {
  469. astman_send_error(s, m, "Invalid Timeout value.");
  470. return 0;
  471. }
  472. if (timeout_override) {
  473. /* If greater than zero, convert to seconds for internal use. Must be >= 1 second. */
  474. timeout_override = MAX(1, timeout_override / 1000);
  475. }
  476. }
  477. if (!(chan = ast_channel_get_by_name(channel))) {
  478. snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel);
  479. astman_send_error(s, m, buf);
  480. return 0;
  481. }
  482. if (!ast_strlen_zero(timeout_channel)) {
  483. ast_channel_lock(chan);
  484. ast_bridge_set_transfer_variables(chan, timeout_channel, 0);
  485. ast_channel_unlock(chan);
  486. }
  487. parker_chan = ast_channel_bridge_peer(chan);
  488. if (!parker_chan || strcmp(ast_channel_name(parker_chan), timeout_channel)) {
  489. if (!ast_strlen_zero(announce_channel)) {
  490. struct ast_channel *announce_chan = ast_channel_get_by_name(announce_channel);
  491. if (!announce_channel) {
  492. astman_send_error(s, m, "AnnounceChannel does not exist");
  493. return 0;
  494. }
  495. create_parked_subscription(announce_chan, ast_channel_uniqueid(chan), 0);
  496. ast_channel_cleanup(announce_chan);
  497. }
  498. manager_park_unbridged(s, m, chan, parkinglot, timeout_override);
  499. return 0;
  500. }
  501. if (!ast_strlen_zero(announce_channel) && strcmp(announce_channel, timeout_channel)) {
  502. /* When using an announce_channel in bridge mode, only add the announce channel if it isn't
  503. * the same as the timeout channel (which will play announcements anyway) */
  504. struct ast_channel *announce_chan = ast_channel_get_by_name(announce_channel);
  505. if (!announce_channel) {
  506. astman_send_error(s, m, "AnnounceChannel does not exist");
  507. return 0;
  508. }
  509. create_parked_subscription(announce_chan, ast_channel_uniqueid(chan), 0);
  510. ast_channel_cleanup(announce_chan);
  511. }
  512. manager_park_bridged(s, m, chan, parker_chan, parkinglot, timeout_override);
  513. return 0;
  514. }
  515. void publish_parked_call_failure(struct ast_channel *parkee)
  516. {
  517. RAII_VAR(struct ast_parked_call_payload *, payload, NULL, ao2_cleanup);
  518. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  519. if (!ast_parked_call_type()) {
  520. return;
  521. }
  522. payload = parked_call_payload_from_failure(parkee);
  523. if (!payload) {
  524. return;
  525. }
  526. msg = stasis_message_create(ast_parked_call_type(), payload);
  527. if (!msg) {
  528. return;
  529. }
  530. stasis_publish(ast_parking_topic(), msg);
  531. }
  532. void publish_parked_call(struct parked_user *pu, enum ast_parked_call_event_type event_type)
  533. {
  534. RAII_VAR(struct ast_parked_call_payload *, payload, NULL, ao2_cleanup);
  535. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  536. if (!ast_parked_call_type()) {
  537. return;
  538. }
  539. payload = parked_call_payload_from_parked_user(pu, event_type);
  540. if (!payload) {
  541. return;
  542. }
  543. msg = stasis_message_create(ast_parked_call_type(), payload);
  544. if (!msg) {
  545. return;
  546. }
  547. stasis_publish(ast_parking_topic(), msg);
  548. }
  549. static void parked_call_message_response(struct ast_parked_call_payload *parked_call)
  550. {
  551. char *event_type = "";
  552. RAII_VAR(struct ast_str *, parked_call_string, NULL, ast_free);
  553. switch (parked_call->event_type) {
  554. case PARKED_CALL:
  555. event_type = "ParkedCall";
  556. break;
  557. case PARKED_CALL_TIMEOUT:
  558. event_type = "ParkedCallTimeOut";
  559. break;
  560. case PARKED_CALL_GIVEUP:
  561. event_type = "ParkedCallGiveUp";
  562. break;
  563. case PARKED_CALL_UNPARKED:
  564. event_type = "UnParkedCall";
  565. break;
  566. case PARKED_CALL_SWAP:
  567. event_type = "ParkedCallSwap";
  568. break;
  569. case PARKED_CALL_FAILED:
  570. /* PARKED_CALL_FAILED doesn't currently get a message and is used exclusively for bridging */
  571. return;
  572. }
  573. parked_call_string = manager_build_parked_call_string(parked_call);
  574. if (!parked_call_string) {
  575. ast_log(LOG_ERROR, "Failed to issue an AMI event of '%s' in response to a stasis message.\n", event_type);
  576. return;
  577. }
  578. manager_event(EVENT_FLAG_CALL, event_type,
  579. "%s",
  580. ast_str_buffer(parked_call_string)
  581. );
  582. }
  583. static void parking_event_cb(void *data, struct stasis_subscription *sub, struct stasis_message *message)
  584. {
  585. if (stasis_message_type(message) == ast_parked_call_type()) {
  586. struct ast_parked_call_payload *parked_call_message = stasis_message_data(message);
  587. parked_call_message_response(parked_call_message);
  588. }
  589. }
  590. static void parking_manager_enable_stasis(void)
  591. {
  592. if (!parking_sub) {
  593. parking_sub = stasis_subscribe(ast_parking_topic(), parking_event_cb, NULL);
  594. stasis_subscription_accept_message_type(parking_sub, ast_parked_call_type());
  595. stasis_subscription_set_filter(parking_sub, STASIS_SUBSCRIPTION_FILTER_SELECTIVE);
  596. }
  597. }
  598. int load_parking_manager(void)
  599. {
  600. int res;
  601. const struct ast_module_info *module = parking_get_module_info();
  602. res = ast_manager_register2("Parkinglots", EVENT_FLAG_CALL, manager_parking_lot_list, module->self, NULL, NULL);
  603. res |= ast_manager_register2("ParkedCalls", EVENT_FLAG_CALL, manager_parking_status, module->self, NULL, NULL);
  604. res |= ast_manager_register2("Park", EVENT_FLAG_CALL, manager_park, module->self, NULL, NULL);
  605. parking_manager_enable_stasis();
  606. return res ? -1 : 0;
  607. }
  608. static void parking_manager_disable_stasis(void)
  609. {
  610. parking_sub = stasis_unsubscribe_and_join(parking_sub);
  611. }
  612. void unload_parking_manager(void)
  613. {
  614. ast_manager_unregister("Parkinglots");
  615. ast_manager_unregister("ParkedCalls");
  616. ast_manager_unregister("Park");
  617. parking_manager_disable_stasis();
  618. }