stasis_bridges.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Kinsey Moore <kmoore@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 Messages and Data Types for Bridge Objects
  21. *
  22. * \author Kinsey Moore <kmoore@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/astobj2.h"
  30. #include "asterisk/stasis.h"
  31. #include "asterisk/stasis_cache_pattern.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/stasis_bridges.h"
  34. #include "asterisk/stasis_channels.h"
  35. #include "asterisk/bridge.h"
  36. #include "asterisk/bridge_technology.h"
  37. /* The container of channel snapshots in a bridge snapshot should always be
  38. equivalent to a linked list; otherwise things (like CDRs) that depend on some
  39. consistency in the ordering of channels in a bridge will break. */
  40. #define SNAPSHOT_CHANNELS_BUCKETS 1
  41. /*** DOCUMENTATION
  42. <managerEvent language="en_US" name="BlindTransfer">
  43. <managerEventInstance class="EVENT_FLAG_CALL">
  44. <synopsis>Raised when a blind transfer is complete.</synopsis>
  45. <syntax>
  46. <parameter name="Result">
  47. <para>Indicates if the transfer was successful or if it failed.</para>
  48. <enumlist>
  49. <enum name="Fail"><para>An internal error occurred.</para></enum>
  50. <enum name="Invalid"><para>Invalid configuration for transfer (e.g. Not bridged)</para></enum>
  51. <enum name="Not Permitted"><para>Bridge does not permit transfers</para></enum>
  52. <enum name="Success"><para>Transfer completed successfully</para></enum>
  53. </enumlist>
  54. <note><para>A result of <literal>Success</literal> does not necessarily mean that a target was succesfully
  55. contacted. It means that a party was succesfully placed into the dialplan at the expected location.</para></note>
  56. </parameter>
  57. <channel_snapshot prefix="Transferer"/>
  58. <channel_snapshot prefix="Transferee"/>
  59. <bridge_snapshot/>
  60. <parameter name="IsExternal">
  61. <para>Indicates if the transfer was performed outside of Asterisk. For instance,
  62. a channel protocol native transfer is external. A DTMF transfer is internal.</para>
  63. <enumlist>
  64. <enum name="Yes" />
  65. <enum name="No" />
  66. </enumlist>
  67. </parameter>
  68. <parameter name="Context">
  69. <para>Destination context for the blind transfer.</para>
  70. </parameter>
  71. <parameter name="Extension">
  72. <para>Destination extension for the blind transfer.</para>
  73. </parameter>
  74. </syntax>
  75. <see-also>
  76. <ref type="manager">BlindTransfer</ref>
  77. </see-also>
  78. </managerEventInstance>
  79. </managerEvent>
  80. <managerEvent language="en_US" name="AttendedTransfer">
  81. <managerEventInstance class="EVENT_FLAG_CALL">
  82. <synopsis>Raised when an attended transfer is complete.</synopsis>
  83. <syntax>
  84. <xi:include xpointer="xpointer(docs/managerEvent[@name='BlindTransfer']/managerEventInstance/syntax/parameter[@name='Result'])" />
  85. <channel_snapshot prefix="OrigTransferer"/>
  86. <bridge_snapshot prefix="Orig"/>
  87. <channel_snapshot prefix="SecondTransferer"/>
  88. <bridge_snapshot prefix="Second"/>
  89. <parameter name="DestType">
  90. <para>Indicates the method by which the attended transfer completed.</para>
  91. <enumlist>
  92. <enum name="Bridge"><para>The transfer was accomplished by merging two bridges into one.</para></enum>
  93. <enum name="App"><para>The transfer was accomplished by having a channel or bridge run a dialplan application.</para></enum>
  94. <enum name="Link"><para>The transfer was accomplished by linking two bridges together using a local channel pair.</para></enum>
  95. <enum name="Threeway"><para>The transfer was accomplished by placing all parties into a threeway call.</para></enum>
  96. <enum name="Fail"><para>The transfer failed.</para></enum>
  97. </enumlist>
  98. </parameter>
  99. <parameter name="DestBridgeUniqueid">
  100. <para>Indicates the surviving bridge when bridges were merged to complete the transfer</para>
  101. <note><para>This header is only present when <replaceable>DestType</replaceable> is <literal>Bridge</literal> or <literal>Threeway</literal></para></note>
  102. </parameter>
  103. <parameter name="DestApp">
  104. <para>Indicates the application that is running when the transfer completes</para>
  105. <note><para>This header is only present when <replaceable>DestType</replaceable> is <literal>App</literal></para></note>
  106. </parameter>
  107. <channel_snapshot prefix="LocalOne"/>
  108. <channel_snapshot prefix="LocalTwo"/>
  109. <parameter name="DestTransfererChannel">
  110. <para>The name of the surviving transferer channel when a transfer results in a threeway call</para>
  111. <note><para>This header is only present when <replaceable>DestType</replaceable> is <literal>Threeway</literal></para></note>
  112. </parameter>
  113. <channel_snapshot prefix="Transferee" />
  114. </syntax>
  115. <description>
  116. <para>The headers in this event attempt to describe all the major details of the attended transfer. The two transferer channels
  117. and the two bridges are determined based on their chronological establishment. So consider that Alice calls Bob, and then Alice
  118. transfers the call to Voicemail. The transferer and bridge headers would be arranged as follows:</para>
  119. <para> <replaceable>OrigTransfererChannel</replaceable>: Alice's channel in the bridge with Bob.</para>
  120. <para> <replaceable>OrigBridgeUniqueid</replaceable>: The bridge between Alice and Bob.</para>
  121. <para> <replaceable>SecondTransfererChannel</replaceable>: Alice's channel that called Voicemail.</para>
  122. <para> <replaceable>SecondBridgeUniqueid</replaceable>: Not present, since a call to Voicemail has no bridge.</para>
  123. <para>Now consider if the order were reversed; instead of having Alice call Bob and transfer him to Voicemail, Alice instead
  124. calls her Voicemail and transfers that to Bob. The transferer and bridge headers would be arranged as follows:</para>
  125. <para> <replaceable>OrigTransfererChannel</replaceable>: Alice's channel that called Voicemail.</para>
  126. <para> <replaceable>OrigBridgeUniqueid</replaceable>: Not present, since a call to Voicemail has no bridge.</para>
  127. <para> <replaceable>SecondTransfererChannel</replaceable>: Alice's channel in the bridge with Bob.</para>
  128. <para> <replaceable>SecondBridgeUniqueid</replaceable>: The bridge between Alice and Bob.</para>
  129. </description>
  130. <see-also>
  131. <ref type="manager">AtxFer</ref>
  132. </see-also>
  133. </managerEventInstance>
  134. </managerEvent>
  135. ***/
  136. static struct ast_json *attended_transfer_to_json(struct stasis_message *msg,
  137. const struct stasis_message_sanitizer *sanitize);
  138. static struct ast_manager_event_blob *attended_transfer_to_ami(struct stasis_message *message);
  139. static struct ast_json *blind_transfer_to_json(struct stasis_message *msg,
  140. const struct stasis_message_sanitizer *sanitize);
  141. static struct ast_manager_event_blob *blind_transfer_to_ami(struct stasis_message *message);
  142. static struct ast_json *ast_channel_entered_bridge_to_json(
  143. struct stasis_message *msg,
  144. const struct stasis_message_sanitizer *sanitize);
  145. static struct ast_json *ast_channel_left_bridge_to_json(
  146. struct stasis_message *msg,
  147. const struct stasis_message_sanitizer *sanitize);
  148. static struct ast_json *ast_bridge_merge_message_to_json(
  149. struct stasis_message *msg,
  150. const struct stasis_message_sanitizer *sanitize);
  151. static struct stasis_cp_all *bridge_cache_all;
  152. /*!
  153. * @{ \brief Define bridge message types.
  154. */
  155. STASIS_MESSAGE_TYPE_DEFN(ast_bridge_snapshot_type);
  156. STASIS_MESSAGE_TYPE_DEFN(ast_bridge_merge_message_type,
  157. .to_json = ast_bridge_merge_message_to_json);
  158. STASIS_MESSAGE_TYPE_DEFN(ast_channel_entered_bridge_type,
  159. .to_json = ast_channel_entered_bridge_to_json);
  160. STASIS_MESSAGE_TYPE_DEFN(ast_channel_left_bridge_type,
  161. .to_json = ast_channel_left_bridge_to_json);
  162. STASIS_MESSAGE_TYPE_DEFN(ast_blind_transfer_type,
  163. .to_json = blind_transfer_to_json,
  164. .to_ami = blind_transfer_to_ami);
  165. STASIS_MESSAGE_TYPE_DEFN(ast_attended_transfer_type,
  166. .to_json = attended_transfer_to_json,
  167. .to_ami = attended_transfer_to_ami);
  168. /*! @} */
  169. struct stasis_cache *ast_bridge_cache(void)
  170. {
  171. return stasis_cp_all_cache(bridge_cache_all);
  172. }
  173. struct stasis_topic *ast_bridge_topic_all(void)
  174. {
  175. return stasis_cp_all_topic(bridge_cache_all);
  176. }
  177. struct stasis_topic *ast_bridge_topic_all_cached(void)
  178. {
  179. return stasis_cp_all_topic_cached(bridge_cache_all);
  180. }
  181. int bridge_topics_init(struct ast_bridge *bridge)
  182. {
  183. if (ast_strlen_zero(bridge->uniqueid)) {
  184. ast_log(LOG_ERROR, "Bridge id initialization required\n");
  185. return -1;
  186. }
  187. bridge->topics = stasis_cp_single_create(bridge_cache_all,
  188. bridge->uniqueid);
  189. if (!bridge->topics) {
  190. return -1;
  191. }
  192. return 0;
  193. }
  194. struct stasis_topic *ast_bridge_topic(struct ast_bridge *bridge)
  195. {
  196. if (!bridge) {
  197. return ast_bridge_topic_all();
  198. }
  199. return stasis_cp_single_topic(bridge->topics);
  200. }
  201. struct stasis_topic *ast_bridge_topic_cached(struct ast_bridge *bridge)
  202. {
  203. if (!bridge) {
  204. return ast_bridge_topic_all_cached();
  205. }
  206. return stasis_cp_single_topic_cached(bridge->topics);
  207. }
  208. /*! \brief Destructor for bridge snapshots */
  209. static void bridge_snapshot_dtor(void *obj)
  210. {
  211. struct ast_bridge_snapshot *snapshot = obj;
  212. ast_string_field_free_memory(snapshot);
  213. ao2_cleanup(snapshot->channels);
  214. snapshot->channels = NULL;
  215. }
  216. struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge)
  217. {
  218. struct ast_bridge_snapshot *snapshot;
  219. struct ast_bridge_channel *bridge_channel;
  220. snapshot = ao2_alloc_options(sizeof(*snapshot), bridge_snapshot_dtor,
  221. AO2_ALLOC_OPT_LOCK_NOLOCK);
  222. if (!snapshot) {
  223. return NULL;
  224. }
  225. if (ast_string_field_init(snapshot, 128)
  226. || ast_string_field_init_extended(snapshot, video_source_id)) {
  227. ao2_ref(snapshot, -1);
  228. return NULL;
  229. }
  230. snapshot->channels = ast_str_container_alloc(SNAPSHOT_CHANNELS_BUCKETS);
  231. if (!snapshot->channels) {
  232. ao2_ref(snapshot, -1);
  233. return NULL;
  234. }
  235. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  236. if (ast_str_container_add(snapshot->channels,
  237. ast_channel_uniqueid(bridge_channel->chan))) {
  238. ao2_ref(snapshot, -1);
  239. return NULL;
  240. }
  241. }
  242. ast_string_field_set(snapshot, uniqueid, bridge->uniqueid);
  243. ast_string_field_set(snapshot, technology, bridge->technology->name);
  244. ast_string_field_set(snapshot, subclass, bridge->v_table->name);
  245. ast_string_field_set(snapshot, creator, bridge->creator);
  246. ast_string_field_set(snapshot, name, bridge->name);
  247. snapshot->feature_flags = bridge->feature_flags;
  248. snapshot->capabilities = bridge->technology->capabilities;
  249. snapshot->num_channels = bridge->num_channels;
  250. snapshot->num_active = bridge->num_active;
  251. snapshot->video_mode = bridge->softmix.video_mode.mode;
  252. if (snapshot->video_mode == AST_BRIDGE_VIDEO_MODE_SINGLE_SRC
  253. && bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
  254. ast_string_field_set(snapshot, video_source_id,
  255. ast_channel_uniqueid(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc));
  256. } else if (snapshot->video_mode == AST_BRIDGE_VIDEO_MODE_TALKER_SRC
  257. && bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
  258. ast_string_field_set(snapshot, video_source_id,
  259. ast_channel_uniqueid(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc));
  260. }
  261. return snapshot;
  262. }
  263. void ast_bridge_publish_state(struct ast_bridge *bridge)
  264. {
  265. struct ast_bridge_snapshot *snapshot;
  266. struct stasis_message *msg;
  267. if (!ast_bridge_snapshot_type()) {
  268. return;
  269. }
  270. ast_assert(bridge != NULL);
  271. snapshot = ast_bridge_snapshot_create(bridge);
  272. if (!snapshot) {
  273. return;
  274. }
  275. msg = stasis_message_create(ast_bridge_snapshot_type(), snapshot);
  276. ao2_ref(snapshot, -1);
  277. if (!msg) {
  278. return;
  279. }
  280. stasis_publish(ast_bridge_topic(bridge), msg);
  281. ao2_ref(msg, -1);
  282. }
  283. static void bridge_publish_state_from_blob(struct ast_bridge *bridge,
  284. struct ast_bridge_blob *obj)
  285. {
  286. struct stasis_message *msg;
  287. ast_assert(obj != NULL);
  288. msg = stasis_message_create(ast_bridge_snapshot_type(), obj->bridge);
  289. if (!msg) {
  290. return;
  291. }
  292. stasis_publish(ast_bridge_topic(bridge), msg);
  293. ao2_ref(msg, -1);
  294. }
  295. /*! \brief Destructor for bridge merge messages */
  296. static void bridge_merge_message_dtor(void *obj)
  297. {
  298. struct ast_bridge_merge_message *msg = obj;
  299. ao2_cleanup(msg->to);
  300. msg->to = NULL;
  301. ao2_cleanup(msg->from);
  302. msg->from = NULL;
  303. }
  304. /*! \brief Bridge merge message creation helper */
  305. static struct ast_bridge_merge_message *bridge_merge_message_create(struct ast_bridge *to, struct ast_bridge *from)
  306. {
  307. struct ast_bridge_merge_message *msg;
  308. msg = ao2_alloc(sizeof(*msg), bridge_merge_message_dtor);
  309. if (!msg) {
  310. return NULL;
  311. }
  312. msg->to = ast_bridge_snapshot_create(to);
  313. msg->from = ast_bridge_snapshot_create(from);
  314. if (!msg->to || !msg->from) {
  315. ao2_ref(msg, -1);
  316. return NULL;
  317. }
  318. return msg;
  319. }
  320. static struct ast_json *ast_bridge_merge_message_to_json(
  321. struct stasis_message *msg,
  322. const struct stasis_message_sanitizer *sanitize)
  323. {
  324. struct ast_bridge_merge_message *merge = stasis_message_data(msg);
  325. struct ast_json *json_bridge_to = ast_bridge_snapshot_to_json(merge->to, sanitize);
  326. struct ast_json *json_bridge_from = ast_bridge_snapshot_to_json(merge->from, sanitize);
  327. if (!json_bridge_to || !json_bridge_from) {
  328. ast_json_unref(json_bridge_to);
  329. ast_json_unref(json_bridge_from);
  330. return NULL;
  331. }
  332. return ast_json_pack("{s: s, s: o, s: o, s: o}",
  333. "type", "BridgeMerged",
  334. "timestamp", ast_json_timeval(*stasis_message_timestamp(msg), NULL),
  335. "bridge", json_bridge_to,
  336. "bridge_from", json_bridge_from);
  337. }
  338. void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from)
  339. {
  340. struct ast_bridge_merge_message *merge_msg;
  341. struct stasis_message *msg;
  342. if (!ast_bridge_merge_message_type()) {
  343. return;
  344. }
  345. ast_assert(to != NULL);
  346. ast_assert(from != NULL);
  347. merge_msg = bridge_merge_message_create(to, from);
  348. if (!merge_msg) {
  349. return;
  350. }
  351. msg = stasis_message_create(ast_bridge_merge_message_type(), merge_msg);
  352. ao2_ref(merge_msg, -1);
  353. if (!msg) {
  354. return;
  355. }
  356. stasis_publish(ast_bridge_topic_all(), msg);
  357. ao2_ref(msg, -1);
  358. }
  359. static void bridge_blob_dtor(void *obj)
  360. {
  361. struct ast_bridge_blob *event = obj;
  362. ao2_cleanup(event->bridge);
  363. event->bridge = NULL;
  364. ao2_cleanup(event->channel);
  365. event->channel = NULL;
  366. ast_json_unref(event->blob);
  367. event->blob = NULL;
  368. }
  369. struct stasis_message *ast_bridge_blob_create(
  370. struct stasis_message_type *message_type,
  371. struct ast_bridge *bridge,
  372. struct ast_channel *chan,
  373. struct ast_json *blob)
  374. {
  375. struct ast_bridge_blob *obj;
  376. struct stasis_message *msg;
  377. if (!message_type) {
  378. return NULL;
  379. }
  380. obj = ao2_alloc(sizeof(*obj), bridge_blob_dtor);
  381. if (!obj) {
  382. return NULL;
  383. }
  384. if (bridge) {
  385. obj->bridge = ast_bridge_snapshot_create(bridge);
  386. if (obj->bridge == NULL) {
  387. ao2_ref(obj, -1);
  388. return NULL;
  389. }
  390. }
  391. if (chan) {
  392. obj->channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(chan));
  393. if (obj->channel == NULL) {
  394. ao2_ref(obj, -1);
  395. return NULL;
  396. }
  397. }
  398. if (blob) {
  399. obj->blob = ast_json_ref(blob);
  400. }
  401. msg = stasis_message_create(message_type, obj);
  402. ao2_ref(obj, -1);
  403. return msg;
  404. }
  405. void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *chan,
  406. struct ast_channel *swap)
  407. {
  408. struct stasis_message *msg;
  409. struct ast_json *blob = NULL;
  410. if (swap) {
  411. blob = ast_json_pack("{s: s}", "swap", ast_channel_uniqueid(swap));
  412. if (!blob) {
  413. return;
  414. }
  415. }
  416. msg = ast_bridge_blob_create(ast_channel_entered_bridge_type(), bridge, chan, blob);
  417. ast_json_unref(blob);
  418. if (!msg) {
  419. return;
  420. }
  421. /* enter blob first, then state */
  422. stasis_publish(ast_bridge_topic(bridge), msg);
  423. bridge_publish_state_from_blob(bridge, stasis_message_data(msg));
  424. ao2_ref(msg, -1);
  425. }
  426. void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *chan)
  427. {
  428. struct stasis_message *msg;
  429. msg = ast_bridge_blob_create(ast_channel_left_bridge_type(), bridge, chan, NULL);
  430. if (!msg) {
  431. return;
  432. }
  433. /* state first, then leave blob (opposite of enter, preserves nesting of events) */
  434. bridge_publish_state_from_blob(bridge, stasis_message_data(msg));
  435. stasis_publish(ast_bridge_topic(bridge), msg);
  436. ao2_ref(msg, -1);
  437. }
  438. static struct ast_json *simple_bridge_channel_event(
  439. const char *type,
  440. struct ast_bridge_snapshot *bridge_snapshot,
  441. struct ast_channel_snapshot *channel_snapshot,
  442. const struct timeval *tv,
  443. const struct stasis_message_sanitizer *sanitize)
  444. {
  445. struct ast_json *json_bridge = ast_bridge_snapshot_to_json(bridge_snapshot, sanitize);
  446. struct ast_json *json_channel = ast_channel_snapshot_to_json(channel_snapshot, sanitize);
  447. if (!json_bridge || !json_channel) {
  448. ast_json_unref(json_bridge);
  449. ast_json_unref(json_channel);
  450. return NULL;
  451. }
  452. return ast_json_pack("{s: s, s: o, s: o, s: o}",
  453. "type", type,
  454. "timestamp", ast_json_timeval(*tv, NULL),
  455. "bridge", json_bridge,
  456. "channel", json_channel);
  457. }
  458. struct ast_json *ast_channel_entered_bridge_to_json(
  459. struct stasis_message *msg,
  460. const struct stasis_message_sanitizer *sanitize)
  461. {
  462. struct ast_bridge_blob *obj = stasis_message_data(msg);
  463. return simple_bridge_channel_event("ChannelEnteredBridge", obj->bridge,
  464. obj->channel, stasis_message_timestamp(msg), sanitize);
  465. }
  466. struct ast_json *ast_channel_left_bridge_to_json(
  467. struct stasis_message *msg,
  468. const struct stasis_message_sanitizer *sanitize)
  469. {
  470. struct ast_bridge_blob *obj = stasis_message_data(msg);
  471. return simple_bridge_channel_event("ChannelLeftBridge", obj->bridge,
  472. obj->channel, stasis_message_timestamp(msg), sanitize);
  473. }
  474. static struct ast_json *container_to_json_array(struct ao2_container *items,
  475. const struct stasis_message_sanitizer *sanitize)
  476. {
  477. struct ast_json *json_items = ast_json_array_create();
  478. char *item;
  479. struct ao2_iterator it;
  480. if (!json_items) {
  481. return NULL;
  482. }
  483. for (it = ao2_iterator_init(items, 0);
  484. (item = ao2_iterator_next(&it)); ao2_cleanup(item)) {
  485. if (sanitize && sanitize->channel_id && sanitize->channel_id(item)) {
  486. continue;
  487. }
  488. if (ast_json_array_append(json_items, ast_json_string_create(item))) {
  489. ao2_cleanup(item);
  490. ao2_iterator_destroy(&it);
  491. ast_json_unref(json_items);
  492. return NULL;
  493. }
  494. }
  495. ao2_iterator_destroy(&it);
  496. return json_items;
  497. }
  498. static const char *capability2str(uint32_t capabilities)
  499. {
  500. if (capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
  501. return "holding";
  502. } else {
  503. return "mixing";
  504. }
  505. }
  506. struct ast_json *ast_bridge_snapshot_to_json(
  507. const struct ast_bridge_snapshot *snapshot,
  508. const struct stasis_message_sanitizer *sanitize)
  509. {
  510. struct ast_json *json_bridge;
  511. struct ast_json *json_channels;
  512. if (snapshot == NULL) {
  513. return NULL;
  514. }
  515. json_channels = container_to_json_array(snapshot->channels, sanitize);
  516. if (!json_channels) {
  517. return NULL;
  518. }
  519. json_bridge = ast_json_pack("{s: s, s: s, s: s, s: s, s: s, s: s, s: o, s: s}",
  520. "id", snapshot->uniqueid,
  521. "technology", snapshot->technology,
  522. "bridge_type", capability2str(snapshot->capabilities),
  523. "bridge_class", snapshot->subclass,
  524. "creator", snapshot->creator,
  525. "name", snapshot->name,
  526. "channels", json_channels,
  527. "video_mode", ast_bridge_video_mode_to_string(snapshot->video_mode));
  528. if (!json_bridge) {
  529. return NULL;
  530. }
  531. if (snapshot->video_mode != AST_BRIDGE_VIDEO_MODE_NONE
  532. && !ast_strlen_zero(snapshot->video_source_id)) {
  533. ast_json_object_set(json_bridge, "video_source_id",
  534. ast_json_string_create(snapshot->video_source_id));
  535. }
  536. return json_bridge;
  537. }
  538. /*!
  539. * \internal
  540. * \brief Allocate the fields of an \ref ast_bridge_channel_snapshot_pair.
  541. *
  542. * \param pair A bridge and channel to get snapshots of
  543. * \param[out] snapshot_pair An allocated snapshot pair.
  544. * \retval 0 Success
  545. * \retval non-zero Failure
  546. */
  547. static int bridge_channel_snapshot_pair_init(struct ast_channel *channel, struct ast_bridge *bridge,
  548. struct ast_bridge_channel_snapshot_pair *snapshot_pair)
  549. {
  550. if (bridge) {
  551. ast_bridge_lock(bridge);
  552. snapshot_pair->bridge_snapshot = ast_bridge_snapshot_create(bridge);
  553. ast_bridge_unlock(bridge);
  554. if (!snapshot_pair->bridge_snapshot) {
  555. return -1;
  556. }
  557. }
  558. snapshot_pair->channel_snapshot = ast_channel_snapshot_get_latest(ast_channel_uniqueid(channel));
  559. if (!snapshot_pair->channel_snapshot) {
  560. return -1;
  561. }
  562. return 0;
  563. }
  564. /*!
  565. * \internal
  566. * \brief Free the fields of an \ref ast_bridge_channel_snapshot_pair.
  567. *
  568. * \param pair The snapshot pair whose fields are to be cleaned up
  569. */
  570. static void bridge_channel_snapshot_pair_cleanup(struct ast_bridge_channel_snapshot_pair *pair)
  571. {
  572. ao2_cleanup(pair->bridge_snapshot);
  573. ao2_cleanup(pair->channel_snapshot);
  574. }
  575. static const char *result_strs[] = {
  576. [AST_BRIDGE_TRANSFER_FAIL] = "Fail",
  577. [AST_BRIDGE_TRANSFER_INVALID] = "Invalid",
  578. [AST_BRIDGE_TRANSFER_NOT_PERMITTED] = "Not Permitted",
  579. [AST_BRIDGE_TRANSFER_SUCCESS] = "Success",
  580. };
  581. static struct ast_json *blind_transfer_to_json(struct stasis_message *msg,
  582. const struct stasis_message_sanitizer *sanitize)
  583. {
  584. struct ast_blind_transfer_message *transfer_msg = stasis_message_data(msg);
  585. struct ast_json *json_transferer;
  586. struct ast_json *json_transferee = NULL;
  587. struct ast_json *out;
  588. struct ast_json *json_replace = NULL;
  589. const struct timeval *tv = stasis_message_timestamp(msg);
  590. json_transferer = ast_channel_snapshot_to_json(transfer_msg->transferer, sanitize);
  591. if (!json_transferer) {
  592. return NULL;
  593. }
  594. if (transfer_msg->transferee) {
  595. json_transferee = ast_channel_snapshot_to_json(transfer_msg->transferee, sanitize);
  596. if (!json_transferee) {
  597. ast_json_unref(json_transferer);
  598. return NULL;
  599. }
  600. }
  601. if (transfer_msg->replace_channel) {
  602. json_replace = ast_channel_snapshot_to_json(transfer_msg->replace_channel, sanitize);
  603. if (!json_replace) {
  604. ast_json_unref(json_transferee);
  605. ast_json_unref(json_transferer);
  606. return NULL;
  607. }
  608. }
  609. out = ast_json_pack("{s: s, s: o, s: o, s: s, s: s, s: s, s: o}",
  610. "type", "BridgeBlindTransfer",
  611. "timestamp", ast_json_timeval(*tv, NULL),
  612. "channel", json_transferer,
  613. "exten", transfer_msg->exten,
  614. "context", transfer_msg->context,
  615. "result", result_strs[transfer_msg->result],
  616. "is_external", ast_json_boolean(transfer_msg->is_external));
  617. if (!out) {
  618. ast_json_unref(json_transferee);
  619. ast_json_unref(json_replace);
  620. return NULL;
  621. }
  622. if (json_transferee && ast_json_object_set(out, "transferee", json_transferee)) {
  623. ast_json_unref(out);
  624. ast_json_unref(json_replace);
  625. return NULL;
  626. }
  627. if (json_replace && ast_json_object_set(out, "replace_channel", json_replace)) {
  628. ast_json_unref(out);
  629. return NULL;
  630. }
  631. if (transfer_msg->bridge) {
  632. struct ast_json *json_bridge = ast_bridge_snapshot_to_json(
  633. transfer_msg->bridge, sanitize);
  634. if (!json_bridge || ast_json_object_set(out, "bridge", json_bridge)) {
  635. ast_json_unref(out);
  636. return NULL;
  637. }
  638. }
  639. return out;
  640. }
  641. static struct ast_manager_event_blob *blind_transfer_to_ami(struct stasis_message *msg)
  642. {
  643. RAII_VAR(struct ast_str *, transferer_state, NULL, ast_free_ptr);
  644. RAII_VAR(struct ast_str *, bridge_state, NULL, ast_free_ptr);
  645. RAII_VAR(struct ast_str *, transferee_state, NULL, ast_free_ptr);
  646. struct ast_blind_transfer_message *transfer_msg = stasis_message_data(msg);
  647. if (!transfer_msg) {
  648. return NULL;
  649. }
  650. transferer_state = ast_manager_build_channel_state_string_prefix(
  651. transfer_msg->transferer, "Transferer");
  652. if (!transferer_state) {
  653. return NULL;
  654. }
  655. if (transfer_msg->bridge) {
  656. bridge_state = ast_manager_build_bridge_state_string(transfer_msg->bridge);
  657. if (!bridge_state) {
  658. return NULL;
  659. }
  660. }
  661. if (transfer_msg->transferee) {
  662. transferee_state = ast_manager_build_channel_state_string_prefix(
  663. transfer_msg->transferee, "Transferee");
  664. if (!transferee_state) {
  665. return NULL;
  666. }
  667. }
  668. return ast_manager_event_blob_create(EVENT_FLAG_CALL, "BlindTransfer",
  669. "Result: %s\r\n"
  670. "%s"
  671. "%s"
  672. "%s"
  673. "IsExternal: %s\r\n"
  674. "Context: %s\r\n"
  675. "Extension: %s\r\n",
  676. result_strs[transfer_msg->result],
  677. ast_str_buffer(transferer_state),
  678. transferee_state ? ast_str_buffer(transferee_state) : "",
  679. bridge_state ? ast_str_buffer(bridge_state) : "",
  680. transfer_msg->is_external ? "Yes" : "No",
  681. transfer_msg->context,
  682. transfer_msg->exten);
  683. }
  684. static void blind_transfer_dtor(void *obj)
  685. {
  686. struct ast_blind_transfer_message *msg = obj;
  687. ao2_cleanup(msg->transferer);
  688. ao2_cleanup(msg->bridge);
  689. ao2_cleanup(msg->transferee);
  690. ao2_cleanup(msg->replace_channel);
  691. }
  692. struct ast_blind_transfer_message *ast_blind_transfer_message_create(int is_external,
  693. struct ast_channel *transferer, const char *exten, const char *context)
  694. {
  695. struct ast_blind_transfer_message *msg;
  696. msg = ao2_alloc(sizeof(*msg), blind_transfer_dtor);
  697. if (!msg) {
  698. return NULL;
  699. }
  700. msg->transferer = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferer));
  701. if (!msg->transferer) {
  702. ao2_cleanup(msg);
  703. return NULL;
  704. }
  705. msg->is_external = is_external;
  706. ast_copy_string(msg->context, context, sizeof(msg->context));
  707. ast_copy_string(msg->exten, exten, sizeof(msg->exten));
  708. return msg;
  709. }
  710. void ast_bridge_publish_blind_transfer(struct ast_blind_transfer_message *transfer_message)
  711. {
  712. struct stasis_message *stasis;
  713. stasis = stasis_message_create(ast_blind_transfer_type(), transfer_message);
  714. if (!stasis) {
  715. return;
  716. }
  717. stasis_publish(ast_bridge_topic_all(), stasis);
  718. ao2_cleanup(stasis);
  719. }
  720. static struct ast_json *attended_transfer_to_json(struct stasis_message *msg,
  721. const struct stasis_message_sanitizer *sanitize)
  722. {
  723. struct ast_attended_transfer_message *transfer_msg = stasis_message_data(msg);
  724. RAII_VAR(struct ast_json *, out, NULL, ast_json_unref);
  725. struct ast_json *json_transferer1, *json_transferer2, *json_bridge, *json_channel;
  726. struct ast_json *json_transferee = NULL, *json_target = NULL;
  727. const struct timeval *tv = stasis_message_timestamp(msg);
  728. int res = 0;
  729. json_transferer1 = ast_channel_snapshot_to_json(transfer_msg->to_transferee.channel_snapshot, sanitize);
  730. if (!json_transferer1) {
  731. return NULL;
  732. }
  733. json_transferer2 = ast_channel_snapshot_to_json(transfer_msg->to_transfer_target.channel_snapshot, sanitize);
  734. if (!json_transferer2) {
  735. ast_json_unref(json_transferer1);
  736. return NULL;
  737. }
  738. if (transfer_msg->transferee) {
  739. json_transferee = ast_channel_snapshot_to_json(transfer_msg->transferee, sanitize);
  740. if (!json_transferee) {
  741. ast_json_unref(json_transferer2);
  742. ast_json_unref(json_transferer1);
  743. return NULL;
  744. }
  745. }
  746. if (transfer_msg->target) {
  747. json_target = ast_channel_snapshot_to_json(transfer_msg->target, sanitize);
  748. if (!json_target) {
  749. ast_json_unref(json_transferee);
  750. ast_json_unref(json_transferer2);
  751. ast_json_unref(json_transferer1);
  752. return NULL;
  753. }
  754. }
  755. out = ast_json_pack("{s: s, s: o, s: o, s: o, s: s, s: o}",
  756. "type", "BridgeAttendedTransfer",
  757. "timestamp", ast_json_timeval(*tv, NULL),
  758. "transferer_first_leg", json_transferer1,
  759. "transferer_second_leg", json_transferer2,
  760. "result", result_strs[transfer_msg->result],
  761. "is_external", ast_json_boolean(transfer_msg->is_external));
  762. if (!out) {
  763. ast_json_unref(json_target);
  764. ast_json_unref(json_transferee);
  765. return NULL;
  766. }
  767. if (json_transferee && ast_json_object_set(out, "transferee", json_transferee)) {
  768. ast_json_unref(json_target);
  769. return NULL;
  770. }
  771. if (json_target && ast_json_object_set(out, "transfer_target", json_target)) {
  772. return NULL;
  773. }
  774. if (transfer_msg->to_transferee.bridge_snapshot) {
  775. json_bridge = ast_bridge_snapshot_to_json(transfer_msg->to_transferee.bridge_snapshot, sanitize);
  776. if (!json_bridge) {
  777. return NULL;
  778. }
  779. res |= ast_json_object_set(out, "transferer_first_leg_bridge", json_bridge);
  780. }
  781. if (transfer_msg->to_transfer_target.bridge_snapshot) {
  782. json_bridge = ast_bridge_snapshot_to_json(transfer_msg->to_transfer_target.bridge_snapshot, sanitize);
  783. if (!json_bridge) {
  784. return NULL;
  785. }
  786. res |= ast_json_object_set(out, "transferer_second_leg_bridge", json_bridge);
  787. }
  788. switch (transfer_msg->dest_type) {
  789. case AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE:
  790. res |= ast_json_object_set(out, "destination_type", ast_json_string_create("bridge"));
  791. res |= ast_json_object_set(out, "destination_bridge", ast_json_string_create(transfer_msg->dest.bridge));
  792. break;
  793. case AST_ATTENDED_TRANSFER_DEST_LOCAL_APP:
  794. res |= ast_json_object_set(out, "replace_channel", ast_channel_snapshot_to_json(transfer_msg->replace_channel, sanitize));
  795. /* fallthrough */
  796. case AST_ATTENDED_TRANSFER_DEST_APP:
  797. res |= ast_json_object_set(out, "destination_type", ast_json_string_create("application"));
  798. res |= ast_json_object_set(out, "destination_application", ast_json_string_create(transfer_msg->dest.app));
  799. break;
  800. case AST_ATTENDED_TRANSFER_DEST_LINK:
  801. res |= ast_json_object_set(out, "destination_type", ast_json_string_create("link"));
  802. json_channel = ast_channel_snapshot_to_json(transfer_msg->dest.links[0], sanitize);
  803. if (!json_channel) {
  804. return NULL;
  805. }
  806. res |= ast_json_object_set(out, "destination_link_first_leg", json_channel);
  807. json_channel = ast_channel_snapshot_to_json(transfer_msg->dest.links[1], sanitize);
  808. if (!json_channel) {
  809. return NULL;
  810. }
  811. res |= ast_json_object_set(out, "destination_link_second_leg", json_channel);
  812. break;
  813. case AST_ATTENDED_TRANSFER_DEST_THREEWAY:
  814. res |= ast_json_object_set(out, "destination_type", ast_json_string_create("threeway"));
  815. json_channel = ast_channel_snapshot_to_json(transfer_msg->dest.threeway.channel_snapshot, sanitize);
  816. if (!json_channel) {
  817. return NULL;
  818. }
  819. res |= ast_json_object_set(out, "destination_threeway_channel", json_channel);
  820. json_bridge = ast_bridge_snapshot_to_json(transfer_msg->dest.threeway.bridge_snapshot, sanitize);
  821. if (!json_bridge) {
  822. return NULL;
  823. }
  824. res |= ast_json_object_set(out, "destination_threeway_bridge", json_bridge);
  825. break;
  826. case AST_ATTENDED_TRANSFER_DEST_FAIL:
  827. res |= ast_json_object_set(out, "destination_type", ast_json_string_create("fail"));
  828. break;
  829. }
  830. if (res) {
  831. return NULL;
  832. }
  833. return ast_json_ref(out);
  834. }
  835. static struct ast_manager_event_blob *attended_transfer_to_ami(struct stasis_message *msg)
  836. {
  837. RAII_VAR(struct ast_str *, variable_data, ast_str_create(64), ast_free_ptr);
  838. RAII_VAR(struct ast_str *, transferer1_state, NULL, ast_free_ptr);
  839. RAII_VAR(struct ast_str *, bridge1_state, NULL, ast_free_ptr);
  840. RAII_VAR(struct ast_str *, transferer2_state, NULL, ast_free_ptr);
  841. RAII_VAR(struct ast_str *, bridge2_state, NULL, ast_free_ptr);
  842. RAII_VAR(struct ast_str *, local1_state, NULL, ast_free_ptr);
  843. RAII_VAR(struct ast_str *, local2_state, NULL, ast_free_ptr);
  844. RAII_VAR(struct ast_str *, transferee_state, NULL, ast_free_ptr);
  845. RAII_VAR(struct ast_str *, target_state, NULL, ast_free_ptr);
  846. struct ast_attended_transfer_message *transfer_msg = stasis_message_data(msg);
  847. if (!variable_data) {
  848. return NULL;
  849. }
  850. transferer1_state = ast_manager_build_channel_state_string_prefix(transfer_msg->to_transferee.channel_snapshot, "OrigTransferer");
  851. transferer2_state = ast_manager_build_channel_state_string_prefix(transfer_msg->to_transfer_target.channel_snapshot, "SecondTransferer");
  852. if (!transferer1_state || !transferer2_state) {
  853. return NULL;
  854. }
  855. if (transfer_msg->transferee) {
  856. transferee_state = ast_manager_build_channel_state_string_prefix(transfer_msg->transferee, "Transferee");
  857. if (!transferee_state) {
  858. return NULL;
  859. }
  860. }
  861. if (transfer_msg->target) {
  862. target_state = ast_manager_build_channel_state_string_prefix(transfer_msg->target, "TransferTarget");
  863. if (!target_state) {
  864. return NULL;
  865. }
  866. }
  867. if (transfer_msg->to_transferee.bridge_snapshot) {
  868. bridge1_state = ast_manager_build_bridge_state_string_prefix(
  869. transfer_msg->to_transferee.bridge_snapshot, "Orig");
  870. if (!bridge1_state) {
  871. return NULL;
  872. }
  873. }
  874. if (transfer_msg->to_transfer_target.bridge_snapshot) {
  875. bridge2_state = ast_manager_build_bridge_state_string_prefix(
  876. transfer_msg->to_transfer_target.bridge_snapshot, "Second");
  877. if (!bridge2_state) {
  878. return NULL;
  879. }
  880. }
  881. switch (transfer_msg->dest_type) {
  882. case AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE:
  883. ast_str_append(&variable_data, 0, "DestType: Bridge\r\n");
  884. ast_str_append(&variable_data, 0, "DestBridgeUniqueid: %s\r\n", transfer_msg->dest.bridge);
  885. break;
  886. case AST_ATTENDED_TRANSFER_DEST_APP:
  887. case AST_ATTENDED_TRANSFER_DEST_LOCAL_APP:
  888. ast_str_append(&variable_data, 0, "DestType: App\r\n");
  889. ast_str_append(&variable_data, 0, "DestApp: %s\r\n", transfer_msg->dest.app);
  890. break;
  891. case AST_ATTENDED_TRANSFER_DEST_LINK:
  892. local1_state = ast_manager_build_channel_state_string_prefix(transfer_msg->dest.links[0], "LocalOne");
  893. local2_state = ast_manager_build_channel_state_string_prefix(transfer_msg->dest.links[1], "LocalTwo");
  894. if (!local1_state || !local2_state) {
  895. return NULL;
  896. }
  897. ast_str_append(&variable_data, 0, "DestType: Link\r\n");
  898. ast_str_append(&variable_data, 0, "%s", ast_str_buffer(local1_state));
  899. ast_str_append(&variable_data, 0, "%s", ast_str_buffer(local2_state));
  900. break;
  901. case AST_ATTENDED_TRANSFER_DEST_THREEWAY:
  902. ast_str_append(&variable_data, 0, "DestType: Threeway\r\n");
  903. ast_str_append(&variable_data, 0, "DestBridgeUniqueid: %s\r\n", transfer_msg->dest.threeway.bridge_snapshot->uniqueid);
  904. ast_str_append(&variable_data, 0, "DestTransfererChannel: %s\r\n", transfer_msg->dest.threeway.channel_snapshot->name);
  905. break;
  906. case AST_ATTENDED_TRANSFER_DEST_FAIL:
  907. ast_str_append(&variable_data, 0, "DestType: Fail\r\n");
  908. break;
  909. }
  910. return ast_manager_event_blob_create(EVENT_FLAG_CALL, "AttendedTransfer",
  911. "Result: %s\r\n"
  912. "%s"
  913. "%s"
  914. "%s"
  915. "%s"
  916. "%s"
  917. "%s"
  918. "IsExternal: %s\r\n"
  919. "%s",
  920. result_strs[transfer_msg->result],
  921. ast_str_buffer(transferer1_state),
  922. bridge1_state ? ast_str_buffer(bridge1_state) : "",
  923. ast_str_buffer(transferer2_state),
  924. bridge2_state ? ast_str_buffer(bridge2_state) : "",
  925. transferee_state ? ast_str_buffer(transferee_state) : "",
  926. target_state ? ast_str_buffer(target_state) : "",
  927. transfer_msg->is_external ? "Yes" : "No",
  928. ast_str_buffer(variable_data));
  929. }
  930. static void attended_transfer_dtor(void *obj)
  931. {
  932. struct ast_attended_transfer_message *msg = obj;
  933. int i;
  934. bridge_channel_snapshot_pair_cleanup(&msg->to_transferee);
  935. bridge_channel_snapshot_pair_cleanup(&msg->to_transfer_target);
  936. ao2_cleanup(msg->replace_channel);
  937. ao2_cleanup(msg->transferee);
  938. ao2_cleanup(msg->target);
  939. if (msg->dest_type != AST_ATTENDED_TRANSFER_DEST_LINK) {
  940. return;
  941. }
  942. for (i = 0; i < ARRAY_LEN(msg->dest.links); ++i) {
  943. ao2_cleanup(msg->dest.links[i]);
  944. }
  945. }
  946. struct ast_attended_transfer_message *ast_attended_transfer_message_create(int is_external,
  947. struct ast_channel *to_transferee, struct ast_bridge *transferee_bridge,
  948. struct ast_channel *to_transfer_target, struct ast_bridge *target_bridge,
  949. struct ast_channel *transferee, struct ast_channel *transfer_target)
  950. {
  951. struct ast_attended_transfer_message *transfer_msg;
  952. transfer_msg = ao2_alloc(sizeof(*transfer_msg), attended_transfer_dtor);
  953. if (!transfer_msg) {
  954. return NULL;
  955. }
  956. if (bridge_channel_snapshot_pair_init(to_transferee, transferee_bridge, &transfer_msg->to_transferee) ||
  957. bridge_channel_snapshot_pair_init(to_transfer_target, target_bridge, &transfer_msg->to_transfer_target)) {
  958. ao2_cleanup(transfer_msg);
  959. return NULL;
  960. }
  961. if (transferee) {
  962. transfer_msg->transferee = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferee));
  963. if (!transfer_msg->transferee) {
  964. ao2_cleanup(transfer_msg);
  965. return NULL;
  966. }
  967. } else if (transferee_bridge) {
  968. transferee = ast_bridge_peer(transferee_bridge, to_transferee);
  969. if (transferee) {
  970. transfer_msg->transferee = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferee));
  971. ao2_cleanup(transferee);
  972. if (!transfer_msg->transferee) {
  973. ao2_cleanup(transfer_msg);
  974. return NULL;
  975. }
  976. }
  977. }
  978. if (transfer_target) {
  979. transfer_msg->target = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transfer_target));
  980. if (!transfer_msg->target) {
  981. ao2_cleanup(transfer_msg);
  982. return NULL;
  983. }
  984. } else if (target_bridge) {
  985. transfer_target = ast_bridge_peer(target_bridge, to_transfer_target);
  986. if (transfer_target) {
  987. transfer_msg->target = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transfer_target));
  988. ao2_cleanup(transfer_target);
  989. if (!transfer_msg->target) {
  990. ao2_cleanup(transfer_msg);
  991. return NULL;
  992. }
  993. }
  994. }
  995. return transfer_msg;
  996. }
  997. int ast_attended_transfer_message_add_merge(struct ast_attended_transfer_message *transfer_msg,
  998. struct ast_bridge *final_bridge)
  999. {
  1000. transfer_msg->dest_type = AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE;
  1001. ast_copy_string(transfer_msg->dest.bridge, final_bridge->uniqueid,
  1002. sizeof(transfer_msg->dest.bridge));
  1003. return 0;
  1004. }
  1005. int ast_attended_transfer_message_add_threeway(struct ast_attended_transfer_message *transfer_msg,
  1006. struct ast_channel *survivor_channel, struct ast_bridge *survivor_bridge)
  1007. {
  1008. transfer_msg->dest_type = AST_ATTENDED_TRANSFER_DEST_THREEWAY;
  1009. if (!strcmp(ast_channel_uniqueid(survivor_channel), transfer_msg->to_transferee.channel_snapshot->uniqueid)) {
  1010. transfer_msg->dest.threeway.channel_snapshot = transfer_msg->to_transferee.channel_snapshot;
  1011. } else {
  1012. transfer_msg->dest.threeway.channel_snapshot = transfer_msg->to_transfer_target.channel_snapshot;
  1013. }
  1014. if (!strcmp(survivor_bridge->uniqueid, transfer_msg->to_transferee.bridge_snapshot->uniqueid)) {
  1015. transfer_msg->dest.threeway.bridge_snapshot = transfer_msg->to_transferee.bridge_snapshot;
  1016. } else {
  1017. transfer_msg->dest.threeway.bridge_snapshot = transfer_msg->to_transfer_target.bridge_snapshot;
  1018. }
  1019. return 0;
  1020. }
  1021. int ast_attended_transfer_message_add_app(struct ast_attended_transfer_message *transfer_msg,
  1022. const char *app, struct ast_channel *replace_channel)
  1023. {
  1024. transfer_msg->dest_type = replace_channel ? AST_ATTENDED_TRANSFER_DEST_LOCAL_APP : AST_ATTENDED_TRANSFER_DEST_APP;
  1025. if (replace_channel) {
  1026. transfer_msg->replace_channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(replace_channel));
  1027. if (!transfer_msg->replace_channel) {
  1028. return -1;
  1029. }
  1030. }
  1031. ast_copy_string(transfer_msg->dest.app, app, sizeof(transfer_msg->dest.app));
  1032. return 0;
  1033. }
  1034. int ast_attended_transfer_message_add_link(struct ast_attended_transfer_message *transfer_msg,
  1035. struct ast_channel *locals[2])
  1036. {
  1037. int i;
  1038. transfer_msg->dest_type = AST_ATTENDED_TRANSFER_DEST_LINK;
  1039. for (i = 0; i < 2; ++i) {
  1040. transfer_msg->dest.links[i] = ast_channel_snapshot_get_latest(ast_channel_uniqueid(locals[i]));
  1041. if (!transfer_msg->dest.links[i]) {
  1042. return -1;
  1043. }
  1044. }
  1045. return 0;
  1046. }
  1047. void ast_bridge_publish_attended_transfer(struct ast_attended_transfer_message *transfer_msg)
  1048. {
  1049. struct stasis_message *msg;
  1050. msg = stasis_message_create(ast_attended_transfer_type(), transfer_msg);
  1051. if (!msg) {
  1052. return;
  1053. }
  1054. stasis_publish(ast_bridge_topic_all(), msg);
  1055. ao2_ref(msg, -1);
  1056. }
  1057. struct ast_bridge_snapshot *ast_bridge_snapshot_get_latest(const char *uniqueid)
  1058. {
  1059. struct stasis_message *message;
  1060. struct ast_bridge_snapshot *snapshot;
  1061. ast_assert(!ast_strlen_zero(uniqueid));
  1062. message = stasis_cache_get(ast_bridge_cache(),
  1063. ast_bridge_snapshot_type(),
  1064. uniqueid);
  1065. if (!message) {
  1066. return NULL;
  1067. }
  1068. snapshot = ao2_bump(stasis_message_data(message));
  1069. ao2_ref(message, -1);
  1070. return snapshot;
  1071. }
  1072. /*! \brief snapshot ID getter for caching topic */
  1073. static const char *bridge_snapshot_get_id(struct stasis_message *msg)
  1074. {
  1075. struct ast_bridge_snapshot *snapshot;
  1076. if (stasis_message_type(msg) != ast_bridge_snapshot_type()) {
  1077. return NULL;
  1078. }
  1079. snapshot = stasis_message_data(msg);
  1080. return snapshot->uniqueid;
  1081. }
  1082. static void stasis_bridging_cleanup(void)
  1083. {
  1084. STASIS_MESSAGE_TYPE_CLEANUP(ast_bridge_snapshot_type);
  1085. STASIS_MESSAGE_TYPE_CLEANUP(ast_bridge_merge_message_type);
  1086. STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_entered_bridge_type);
  1087. STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_left_bridge_type);
  1088. STASIS_MESSAGE_TYPE_CLEANUP(ast_blind_transfer_type);
  1089. STASIS_MESSAGE_TYPE_CLEANUP(ast_attended_transfer_type);
  1090. ao2_cleanup(bridge_cache_all);
  1091. bridge_cache_all = NULL;
  1092. }
  1093. int ast_stasis_bridging_init(void)
  1094. {
  1095. int res = 0;
  1096. ast_register_cleanup(stasis_bridging_cleanup);
  1097. bridge_cache_all = stasis_cp_all_create("ast_bridge_topic_all",
  1098. bridge_snapshot_get_id);
  1099. if (!bridge_cache_all) {
  1100. return -1;
  1101. }
  1102. res |= STASIS_MESSAGE_TYPE_INIT(ast_bridge_snapshot_type);
  1103. res |= STASIS_MESSAGE_TYPE_INIT(ast_bridge_merge_message_type);
  1104. res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_entered_bridge_type);
  1105. res |= STASIS_MESSAGE_TYPE_INIT(ast_channel_left_bridge_type);
  1106. res |= STASIS_MESSAGE_TYPE_INIT(ast_blind_transfer_type);
  1107. res |= STASIS_MESSAGE_TYPE_INIT(ast_attended_transfer_type);
  1108. return res;
  1109. }