bridge_native_dahdi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@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. /*!
  19. * \file
  20. * \brief Native DAHDI bridging support.
  21. *
  22. * \author Richard Mudgett <rmudgett@digium.com>
  23. *
  24. * See Also:
  25. * \arg \ref AstCREDITS
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "../sig_analog.h"
  33. #if defined(HAVE_PRI)
  34. #include "../sig_pri.h"
  35. #endif /* defined(HAVE_PRI) */
  36. #include "../chan_dahdi.h"
  37. #include "bridge_native_dahdi.h"
  38. #include "asterisk/bridge.h"
  39. #include "asterisk/bridge_technology.h"
  40. #include "asterisk/frame.h"
  41. #include "asterisk/format_cache.h"
  42. /* ------------------------------------------------------------------- */
  43. static const struct ast_channel_tech *dahdi_tech;
  44. struct native_pvt_chan {
  45. /*! Original private. */
  46. struct dahdi_pvt *pvt;
  47. /*! Original private owner. */
  48. struct ast_channel *owner;
  49. /*! Original owner index. */
  50. int index;
  51. /*! Original file descriptor 0. */
  52. int fd0;
  53. /*! Original channel state. */
  54. int state;
  55. /*! Original inthreeway. */
  56. unsigned int inthreeway:1;
  57. };
  58. struct native_pvt_bridge {
  59. /*! Master channel in the native bridge. */
  60. struct dahdi_pvt *master;
  61. /*! Slave channel in the native bridge. */
  62. struct dahdi_pvt *slave;
  63. /*! TRUE if the bridge can start when ready. */
  64. unsigned int saw_start:1;
  65. /*! TRUE if the channels are connected in a conference. */
  66. unsigned int connected:1;
  67. #if defined(HAVE_PRI) && defined(PRI_2BCT)
  68. /*!
  69. * \brief TRUE if tried to eliminate possible PRI tromboned call.
  70. *
  71. * \note A tromboned call uses two B channels of the same ISDN
  72. * span. One leg comes into Asterisk, the other leg goes out of
  73. * Asterisk, and Asterisk is natively bridging the two legs.
  74. */
  75. unsigned int tried_trombone_removal:1;
  76. #endif /* defined(HAVE_PRI) && defined(PRI_2BCT) */
  77. };
  78. /*!
  79. * \internal
  80. * \brief Create a bridge technology instance for a bridge.
  81. * \since 12.0.0
  82. *
  83. * \retval 0 on success
  84. * \retval -1 on failure
  85. *
  86. * \note On entry, bridge may or may not already be locked.
  87. * However, it can be accessed as if it were locked.
  88. */
  89. static int native_bridge_create(struct ast_bridge *bridge)
  90. {
  91. struct native_pvt_bridge *tech_pvt;
  92. ast_assert(!bridge->tech_pvt);
  93. tech_pvt = ast_calloc(1, sizeof(*tech_pvt));
  94. if (!tech_pvt) {
  95. return -1;
  96. }
  97. bridge->tech_pvt = tech_pvt;
  98. return 0;
  99. }
  100. /*!
  101. * \internal
  102. * \brief Destroy a bridging technology instance for a bridge.
  103. * \since 12.0.0
  104. *
  105. * \note On entry, bridge must NOT be locked.
  106. */
  107. static void native_bridge_destroy(struct ast_bridge *bridge)
  108. {
  109. struct native_pvt_bridge *tech_pvt;
  110. tech_pvt = bridge->tech_pvt;
  111. bridge->tech_pvt = NULL;
  112. ast_free(tech_pvt);
  113. }
  114. /*!
  115. * \internal
  116. * \brief Stop native bridging activity.
  117. * \since 12.0.0
  118. *
  119. * \param bridge What to operate upon.
  120. *
  121. * \return Nothing
  122. *
  123. * \note On entry, bridge is already locked.
  124. */
  125. static void native_stop(struct ast_bridge *bridge)
  126. {
  127. struct native_pvt_bridge *bridge_tech_pvt;
  128. struct ast_bridge_channel *cur;
  129. ast_assert(bridge->tech_pvt != NULL);
  130. AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
  131. struct native_pvt_chan *chan_tech_pvt;
  132. chan_tech_pvt = cur->tech_pvt;
  133. if (!chan_tech_pvt) {
  134. continue;
  135. }
  136. ast_mutex_lock(&chan_tech_pvt->pvt->lock);
  137. if (chan_tech_pvt->pvt == ast_channel_tech_pvt(cur->chan)) {
  138. dahdi_ec_enable(chan_tech_pvt->pvt);
  139. }
  140. if (chan_tech_pvt->index == SUB_REAL) {
  141. dahdi_dtmf_detect_enable(chan_tech_pvt->pvt);
  142. }
  143. ast_mutex_unlock(&chan_tech_pvt->pvt->lock);
  144. }
  145. bridge_tech_pvt = bridge->tech_pvt;
  146. dahdi_master_slave_unlink(bridge_tech_pvt->slave, bridge_tech_pvt->master, 1);
  147. ast_debug(2, "Stop native bridging %s and %s\n",
  148. ast_channel_name(AST_LIST_FIRST(&bridge->channels)->chan),
  149. ast_channel_name(AST_LIST_LAST(&bridge->channels)->chan));
  150. }
  151. /*!
  152. * \internal
  153. * \brief Request to stop native bridging activity.
  154. * \since 12.0.0
  155. *
  156. * \param bridge What to operate upon.
  157. *
  158. * \return Nothing
  159. *
  160. * \note On entry, bridge is already locked.
  161. */
  162. static void native_request_stop(struct ast_bridge *bridge)
  163. {
  164. struct native_pvt_bridge *tech_pvt;
  165. ast_assert(bridge->tech_pvt != NULL);
  166. tech_pvt = bridge->tech_pvt;
  167. if (!tech_pvt->connected) {
  168. return;
  169. }
  170. tech_pvt->connected = 0;
  171. /* Now to actually stop the bridge. */
  172. native_stop(bridge);
  173. }
  174. /*!
  175. * \internal
  176. * \brief Start native bridging activity.
  177. * \since 12.0.0
  178. *
  179. * \param bridge What to operate upon.
  180. *
  181. * \retval 0 on success.
  182. * \retval -1 on error. Could not start the bridge.
  183. *
  184. * \note On entry, bridge may or may not already be locked.
  185. * However, it can be accessed as if it were locked.
  186. */
  187. static int native_start(struct ast_bridge *bridge)
  188. {
  189. struct native_pvt_bridge *tech_pvt;
  190. struct ast_bridge_channel *bc0;
  191. struct ast_bridge_channel *bc1;
  192. struct native_pvt_chan *npc0;
  193. struct native_pvt_chan *npc1;
  194. struct ast_channel *c0;
  195. struct ast_channel *c1;
  196. struct dahdi_pvt *p0;
  197. struct dahdi_pvt *p1;
  198. struct dahdi_pvt *master;
  199. struct dahdi_pvt *slave;
  200. int inconf;
  201. int nothing_ok;
  202. ast_assert(bridge->tech_pvt != NULL);
  203. bc0 = AST_LIST_FIRST(&bridge->channels);
  204. bc1 = AST_LIST_LAST(&bridge->channels);
  205. c0 = bc0->chan;
  206. c1 = bc1->chan;
  207. /* Lock channels and privates */
  208. for (;;) {
  209. ast_channel_lock(c0);
  210. if (!ast_channel_trylock(c1)) {
  211. p0 = ast_channel_tech_pvt(c0);
  212. if (!ast_mutex_trylock(&p0->lock)) {
  213. p1 = ast_channel_tech_pvt(c1);
  214. if (!ast_mutex_trylock(&p1->lock)) {
  215. /* Got all locks */
  216. break;
  217. }
  218. ast_mutex_unlock(&p0->lock);
  219. }
  220. ast_channel_unlock(c1);
  221. }
  222. ast_channel_unlock(c0);
  223. sched_yield();
  224. }
  225. npc0 = bc0->tech_pvt;
  226. ast_assert(npc0 != NULL);
  227. npc0->pvt = p0;
  228. npc0->owner = p0->owner;
  229. npc0->index = dahdi_get_index(c0, p0, 0);
  230. npc0->fd0 = ast_channel_fd(c0, 0);
  231. npc0->state = -1;
  232. npc0->inthreeway = p0->subs[SUB_REAL].inthreeway;
  233. npc1 = bc1->tech_pvt;
  234. ast_assert(npc1 != NULL);
  235. npc1->pvt = p1;
  236. npc1->owner = p1->owner;
  237. npc1->index = dahdi_get_index(c1, p1, 0);
  238. npc1->fd0 = ast_channel_fd(c1, 0);
  239. npc1->state = -1;
  240. npc1->inthreeway = p1->subs[SUB_REAL].inthreeway;
  241. /*
  242. * Check things that can change on the privates while in native
  243. * bridging and cause native to not activate.
  244. */
  245. if (npc0->index < 0 || npc1->index < 0
  246. #if defined(HAVE_PRI)
  247. /*
  248. * PRI nobch channels (hold and call waiting) are equivalent to
  249. * pseudo channels and cannot be nativly bridged.
  250. */
  251. || (dahdi_sig_pri_lib_handles(p0->sig)
  252. && ((struct sig_pri_chan *) p0->sig_pvt)->no_b_channel)
  253. || (dahdi_sig_pri_lib_handles(p1->sig)
  254. && ((struct sig_pri_chan *) p1->sig_pvt)->no_b_channel)
  255. #endif /* defined(HAVE_PRI) */
  256. ) {
  257. ast_mutex_unlock(&p0->lock);
  258. ast_mutex_unlock(&p1->lock);
  259. ast_channel_unlock(c0);
  260. ast_channel_unlock(c1);
  261. return -1;
  262. }
  263. inconf = 0;
  264. nothing_ok = 1;
  265. master = NULL;
  266. slave = NULL;
  267. if (npc0->index == SUB_REAL && npc1->index == SUB_REAL) {
  268. if (p0->owner && p1->owner) {
  269. /*
  270. * If we don't have a call-wait in a 3-way, and we aren't in a
  271. * 3-way, we can be master.
  272. */
  273. if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
  274. master = p0;
  275. slave = p1;
  276. inconf = 1;
  277. } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
  278. master = p1;
  279. slave = p0;
  280. inconf = 1;
  281. } else {
  282. ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
  283. ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
  284. p0->channel,
  285. npc0->index, (p0->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
  286. p0->subs[SUB_REAL].inthreeway,
  287. p0->channel,
  288. npc0->index, (p1->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
  289. p1->subs[SUB_REAL].inthreeway);
  290. }
  291. nothing_ok = 0;
  292. }
  293. } else if (npc0->index == SUB_REAL && npc1->index == SUB_THREEWAY) {
  294. if (p1->subs[SUB_THREEWAY].inthreeway) {
  295. master = p1;
  296. slave = p0;
  297. nothing_ok = 0;
  298. }
  299. } else if (npc0->index == SUB_THREEWAY && npc1->index == SUB_REAL) {
  300. if (p0->subs[SUB_THREEWAY].inthreeway) {
  301. master = p0;
  302. slave = p1;
  303. nothing_ok = 0;
  304. }
  305. } else if (npc0->index == SUB_REAL && npc1->index == SUB_CALLWAIT) {
  306. /*
  307. * We have a real and a call wait. If we're in a three way
  308. * call, put us in it, otherwise, don't put us in anything.
  309. */
  310. if (p1->subs[SUB_CALLWAIT].inthreeway) {
  311. master = p1;
  312. slave = p0;
  313. nothing_ok = 0;
  314. }
  315. } else if (npc0->index == SUB_CALLWAIT && npc1->index == SUB_REAL) {
  316. /* Same as previous */
  317. if (p0->subs[SUB_CALLWAIT].inthreeway) {
  318. master = p0;
  319. slave = p1;
  320. nothing_ok = 0;
  321. }
  322. }
  323. ast_debug(3, "master: %d, slave: %d, nothing_ok: %d\n",
  324. master ? master->channel : 0,
  325. slave ? slave->channel : 0,
  326. nothing_ok);
  327. if (master && slave) {
  328. /*
  329. * Stop any tones, or play ringtone as appropriate. If they are
  330. * bridged in an active threeway call with a channel that is
  331. * ringing, we should indicate ringing.
  332. */
  333. if (npc1->index == SUB_THREEWAY
  334. && p1->subs[SUB_THREEWAY].inthreeway
  335. && p1->subs[SUB_REAL].owner
  336. && p1->subs[SUB_REAL].inthreeway
  337. && ast_channel_state(p1->subs[SUB_REAL].owner) == AST_STATE_RINGING) {
  338. ast_debug(2,
  339. "Playing ringback on %d/%d(%s) since %d/%d(%s) is in a ringing three-way\n",
  340. p0->channel, npc0->index, ast_channel_name(c0),
  341. p1->channel, npc1->index, ast_channel_name(c1));
  342. tone_zone_play_tone(p0->subs[npc0->index].dfd, DAHDI_TONE_RINGTONE);
  343. npc1->state = ast_channel_state(p1->subs[SUB_REAL].owner);
  344. } else {
  345. ast_debug(2, "Stopping tones on %d/%d(%s) talking to %d/%d(%s)\n",
  346. p0->channel, npc0->index, ast_channel_name(c0),
  347. p1->channel, npc1->index, ast_channel_name(c1));
  348. tone_zone_play_tone(p0->subs[npc0->index].dfd, -1);
  349. }
  350. if (npc0->index == SUB_THREEWAY
  351. && p0->subs[SUB_THREEWAY].inthreeway
  352. && p0->subs[SUB_REAL].owner
  353. && p0->subs[SUB_REAL].inthreeway
  354. && ast_channel_state(p0->subs[SUB_REAL].owner) == AST_STATE_RINGING) {
  355. ast_debug(2,
  356. "Playing ringback on %d/%d(%s) since %d/%d(%s) is in a ringing three-way\n",
  357. p1->channel, npc1->index, ast_channel_name(c1),
  358. p0->channel, npc0->index, ast_channel_name(c0));
  359. tone_zone_play_tone(p1->subs[npc1->index].dfd, DAHDI_TONE_RINGTONE);
  360. npc0->state = ast_channel_state(p0->subs[SUB_REAL].owner);
  361. } else {
  362. ast_debug(2, "Stopping tones on %d/%d(%s) talking to %d/%d(%s)\n",
  363. p1->channel, npc1->index, ast_channel_name(c1),
  364. p0->channel, npc0->index, ast_channel_name(c0));
  365. tone_zone_play_tone(p1->subs[npc1->index].dfd, -1);
  366. }
  367. if (npc0->index == SUB_REAL && npc1->index == SUB_REAL) {
  368. if (!p0->echocanbridged || !p1->echocanbridged) {
  369. /* Disable echo cancellation if appropriate */
  370. dahdi_ec_disable(p0);
  371. dahdi_ec_disable(p1);
  372. }
  373. }
  374. dahdi_master_slave_link(slave, master);
  375. master->inconference = inconf;
  376. } else if (!nothing_ok) {
  377. ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n",
  378. p0->channel, subnames[npc0->index],
  379. p1->channel, subnames[npc1->index]);
  380. }
  381. dahdi_conf_update(p0);
  382. dahdi_conf_update(p1);
  383. ast_channel_unlock(c0);
  384. ast_channel_unlock(c1);
  385. /* Native bridge failed */
  386. if ((!master || !slave) && !nothing_ok) {
  387. ast_mutex_unlock(&p0->lock);
  388. ast_mutex_unlock(&p1->lock);
  389. return -1;
  390. }
  391. if (npc0->index == SUB_REAL) {
  392. dahdi_dtmf_detect_disable(p0);
  393. }
  394. if (npc1->index == SUB_REAL) {
  395. dahdi_dtmf_detect_disable(p1);
  396. }
  397. ast_mutex_unlock(&p0->lock);
  398. ast_mutex_unlock(&p1->lock);
  399. tech_pvt = bridge->tech_pvt;
  400. tech_pvt->master = master;
  401. tech_pvt->slave = slave;
  402. ast_debug(2, "Start native bridging %s and %s\n",
  403. ast_channel_name(c0), ast_channel_name(c1));
  404. #if defined(HAVE_PRI) && defined(PRI_2BCT)
  405. if (!tech_pvt->tried_trombone_removal) {
  406. tech_pvt->tried_trombone_removal = 1;
  407. if (p0->pri && p0->pri == p1->pri && p0->pri->transfer) {
  408. q931_call *q931_c0;
  409. q931_call *q931_c1;
  410. /* Try to eliminate the tromboned call. */
  411. ast_mutex_lock(&p0->pri->lock);
  412. ast_assert(dahdi_sig_pri_lib_handles(p0->sig));
  413. ast_assert(dahdi_sig_pri_lib_handles(p1->sig));
  414. q931_c0 = ((struct sig_pri_chan *) (p0->sig_pvt))->call;
  415. q931_c1 = ((struct sig_pri_chan *) (p1->sig_pvt))->call;
  416. if (q931_c0 && q931_c1) {
  417. pri_channel_bridge(q931_c0, q931_c1);
  418. ast_debug(2, "Attempt to eliminate tromboned call with %s and %s\n",
  419. ast_channel_name(c0), ast_channel_name(c1));
  420. }
  421. ast_mutex_unlock(&p0->pri->lock);
  422. }
  423. }
  424. #endif /* defined(HAVE_PRI) && defined(PRI_2BCT) */
  425. return 0;
  426. }
  427. /*!
  428. * \internal
  429. * \brief Request to start native bridging activity.
  430. * \since 12.0.0
  431. *
  432. * \param bridge What to operate upon.
  433. *
  434. * \return Nothing
  435. *
  436. * \note On entry, bridge may or may not already be locked.
  437. * However, it can be accessed as if it were locked.
  438. */
  439. static void native_request_start(struct ast_bridge *bridge)
  440. {
  441. struct native_pvt_bridge *tech_pvt;
  442. struct ast_bridge_channel *cur;
  443. ast_assert(bridge->tech_pvt != NULL);
  444. tech_pvt = bridge->tech_pvt;
  445. if (bridge->num_channels != 2 || !tech_pvt->saw_start || tech_pvt->connected) {
  446. return;
  447. }
  448. AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
  449. if (cur->suspended || !cur->tech_pvt) {
  450. return;
  451. }
  452. }
  453. /* Actually try starting the native bridge. */
  454. if (native_start(bridge)) {
  455. return;
  456. }
  457. tech_pvt->connected = 1;
  458. }
  459. /*!
  460. * \internal
  461. * \brief Request a bridge technology instance start operations.
  462. * \since 12.0.0
  463. *
  464. * \retval 0 on success
  465. * \retval -1 on failure
  466. *
  467. * \note On entry, bridge may or may not already be locked.
  468. * However, it can be accessed as if it were locked.
  469. */
  470. static int native_bridge_start(struct ast_bridge *bridge)
  471. {
  472. struct native_pvt_bridge *tech_pvt;
  473. ast_assert(bridge->tech_pvt != NULL);
  474. tech_pvt = bridge->tech_pvt;
  475. tech_pvt->saw_start = 1;
  476. native_request_start(bridge);
  477. return 0;
  478. }
  479. /*!
  480. * \internal
  481. * \brief Request a bridge technology instance stop in preparation for being destroyed.
  482. * \since 12.0.0
  483. *
  484. * \note On entry, bridge is already locked.
  485. */
  486. static void native_bridge_stop(struct ast_bridge *bridge)
  487. {
  488. struct native_pvt_bridge *tech_pvt;
  489. tech_pvt = bridge->tech_pvt;
  490. if (!tech_pvt) {
  491. return;
  492. }
  493. tech_pvt->saw_start = 0;
  494. native_request_stop(bridge);
  495. }
  496. /*!
  497. * \internal
  498. * \brief Add a channel to a bridging technology instance for a bridge.
  499. * \since 12.0.0
  500. *
  501. * \retval 0 on success
  502. * \retval -1 on failure
  503. *
  504. * \note On entry, bridge is already locked.
  505. */
  506. static int native_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  507. {
  508. struct native_pvt_chan *tech_pvt;
  509. struct ast_channel *c0;
  510. struct ast_channel *c1;
  511. ast_assert(!bridge_channel->tech_pvt);
  512. tech_pvt = ast_calloc(1, sizeof(*tech_pvt));
  513. if (!tech_pvt) {
  514. return -1;
  515. }
  516. bridge_channel->tech_pvt = tech_pvt;
  517. native_request_start(bridge);
  518. /*
  519. * Make the channels compatible in case the native bridge did
  520. * not start for some reason and we need to fallback to 1-1
  521. * bridging.
  522. */
  523. c0 = AST_LIST_FIRST(&bridge->channels)->chan;
  524. c1 = AST_LIST_LAST(&bridge->channels)->chan;
  525. if (c0 == c1) {
  526. return 0;
  527. }
  528. return ast_channel_make_compatible(c0, c1);
  529. }
  530. /*!
  531. * \internal
  532. * \brief Remove a channel from a bridging technology instance for a bridge.
  533. * \since 12.0.0
  534. *
  535. * \note On entry, bridge is already locked.
  536. */
  537. static void native_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  538. {
  539. struct native_pvt_chan *tech_pvt;
  540. native_request_stop(bridge);
  541. tech_pvt = bridge_channel->tech_pvt;
  542. bridge_channel->tech_pvt = NULL;
  543. ast_free(tech_pvt);
  544. }
  545. /*!
  546. * \internal
  547. * \brief Suspend a channel on a bridging technology instance for a bridge.
  548. * \since 12.0.0
  549. *
  550. * \note On entry, bridge is already locked.
  551. */
  552. static void native_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  553. {
  554. native_request_stop(bridge);
  555. }
  556. /*!
  557. * \internal
  558. * \brief Unsuspend a channel on a bridging technology instance for a bridge.
  559. * \since 12.0.0
  560. *
  561. * \note On entry, bridge is already locked.
  562. */
  563. static void native_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  564. {
  565. native_request_start(bridge);
  566. }
  567. /*!
  568. * \internal
  569. * \brief Check if channel is compatible.
  570. * \since 12.0.0
  571. *
  572. * \param bridge_channel Is this channel compatible.
  573. *
  574. * \retval TRUE if channel is compatible with native DAHDI bridge.
  575. */
  576. static int native_bridge_is_capable(struct ast_bridge_channel *bridge_channel)
  577. {
  578. struct ast_channel *chan = bridge_channel->chan;
  579. struct dahdi_pvt *pvt;
  580. int is_capable;
  581. if (ao2_container_count(bridge_channel->features->dtmf_hooks)) {
  582. ast_debug(2, "Channel '%s' has DTMF hooks.\n", ast_channel_name(chan));
  583. return 0;
  584. }
  585. ast_channel_lock(chan);
  586. if (dahdi_tech != ast_channel_tech(chan)) {
  587. ast_debug(2, "Channel '%s' is not %s.\n",
  588. ast_channel_name(chan), dahdi_tech->type);
  589. ast_channel_unlock(chan);
  590. return 0;
  591. }
  592. if (ast_channel_has_audio_frame_or_monitor(chan)) {
  593. ast_debug(2, "Channel '%s' has an active monitor, audiohook, or framehook.\n",
  594. ast_channel_name(chan));
  595. ast_channel_unlock(chan);
  596. return 0;
  597. }
  598. pvt = ast_channel_tech_pvt(chan);
  599. if (!pvt || !pvt->sig) {
  600. /* No private; or signaling is for a pseudo channel. */
  601. ast_channel_unlock(chan);
  602. return 0;
  603. }
  604. is_capable = 1;
  605. ast_mutex_lock(&pvt->lock);
  606. if (pvt->callwaiting && pvt->callwaitingcallerid) {
  607. /*
  608. * Call Waiting Caller ID requires DTMF detection to know if it
  609. * can send the CID spill.
  610. */
  611. ast_debug(2, "Channel '%s' has call waiting caller ID enabled.\n",
  612. ast_channel_name(chan));
  613. is_capable = 0;
  614. }
  615. ast_mutex_unlock(&pvt->lock);
  616. ast_channel_unlock(chan);
  617. return is_capable;
  618. }
  619. /*!
  620. * \internal
  621. * \brief Check if a bridge is compatible with the bridging technology.
  622. * \since 12.0.0
  623. *
  624. * \retval 0 if not compatible
  625. * \retval non-zero if compatible
  626. *
  627. * \note On entry, bridge may or may not already be locked.
  628. * However, it can be accessed as if it were locked.
  629. */
  630. static int native_bridge_compatible(struct ast_bridge *bridge)
  631. {
  632. struct ast_bridge_channel *cur;
  633. /* We require two channels before even considering native bridging. */
  634. if (bridge->num_channels != 2) {
  635. ast_debug(1, "Bridge %s: Cannot use native DAHDI. Must have two channels.\n",
  636. bridge->uniqueid);
  637. return 0;
  638. }
  639. AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
  640. if (!native_bridge_is_capable(cur)) {
  641. ast_debug(1, "Bridge %s: Cannot use native DAHDI. Channel '%s' not compatible.\n",
  642. bridge->uniqueid, ast_channel_name(cur->chan));
  643. return 0;
  644. }
  645. }
  646. return -1;
  647. }
  648. /*!
  649. * \internal
  650. * \brief Check if something changed on the channel.
  651. * \since 12.0.0
  652. *
  653. * \param bridge_channel What to operate upon.
  654. *
  655. * \retval 0 Nothing changed.
  656. * \retval -1 Something changed.
  657. *
  658. * \note On entry, bridge_channel->bridge is already locked.
  659. */
  660. static int native_chan_changed(struct ast_bridge_channel *bridge_channel)
  661. {
  662. struct native_pvt_chan *tech_pvt;
  663. struct ast_channel *chan;
  664. struct dahdi_pvt *pvt;
  665. int idx = -1;
  666. ast_assert(bridge_channel->tech_pvt != NULL);
  667. tech_pvt = bridge_channel->tech_pvt;
  668. chan = bridge_channel->chan;
  669. ast_channel_lock(chan);
  670. pvt = ast_channel_tech_pvt(chan);
  671. if (tech_pvt->pvt == pvt) {
  672. idx = dahdi_get_index(chan, pvt, 1);
  673. }
  674. ast_channel_unlock(chan);
  675. if (/* Did chan get masqueraded or PRI change associated B channel? */
  676. tech_pvt->pvt != pvt
  677. /* Did the pvt active owner change? */
  678. || tech_pvt->owner != pvt->owner
  679. /* Did the pvt three way call status change? */
  680. || tech_pvt->inthreeway != pvt->subs[SUB_REAL].inthreeway
  681. /* Did the owner index change? */
  682. || tech_pvt->index != idx
  683. /*
  684. * Did chan file descriptor change? (This seems redundant with
  685. * masquerade and active owner change checks.)
  686. */
  687. || tech_pvt->fd0 != ast_channel_fd(chan, 0)
  688. /* Did chan state change? i.e. Did it stop ringing? */
  689. || (pvt->subs[SUB_REAL].owner
  690. && tech_pvt->state > -1
  691. && tech_pvt->state != ast_channel_state(pvt->subs[SUB_REAL].owner))) {
  692. return -1;
  693. }
  694. return 0;
  695. }
  696. /*!
  697. * \internal
  698. * \brief Check if something changed on the bridge channels.
  699. * \since 12.0.0
  700. *
  701. * \param bridge What to operate upon.
  702. *
  703. * \retval 0 Nothing changed.
  704. * \retval -1 Something changed.
  705. *
  706. * \note On entry, bridge is already locked.
  707. */
  708. static int native_bridge_changed(struct ast_bridge *bridge)
  709. {
  710. struct ast_bridge_channel *cur;
  711. AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
  712. if (native_chan_changed(cur)) {
  713. ast_debug(1, "Bridge %s: Something changed on channel '%s'.\n",
  714. bridge->uniqueid, ast_channel_name(cur->chan));
  715. return -1;
  716. }
  717. }
  718. return 0;
  719. }
  720. /*!
  721. * \internal
  722. * \brief Write a frame into the bridging technology instance for a bridge.
  723. * \since 12.0.0
  724. *
  725. * \note The bridge must be tolerant of bridge_channel being NULL.
  726. *
  727. * \retval 0 Frame accepted into the bridge.
  728. * \retval -1 Frame needs to be deferred.
  729. *
  730. * \note On entry, bridge is already locked.
  731. */
  732. static int native_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  733. {
  734. struct native_pvt_bridge *tech_pvt;
  735. /*
  736. * When we are not native bridged by DAHDI, we are like a normal
  737. * 1-1 bridge.
  738. */
  739. ast_assert(bridge->tech_pvt != NULL);
  740. /* Recheck native bridging validity. */
  741. tech_pvt = bridge->tech_pvt;
  742. switch (frame->frametype) {
  743. case AST_FRAME_VOICE:
  744. case AST_FRAME_VIDEO:
  745. if (!tech_pvt->connected) {
  746. /* Don't try to start native mode on media frames. */
  747. break;
  748. }
  749. if (native_bridge_changed(bridge)) {
  750. native_request_stop(bridge);
  751. native_request_start(bridge);
  752. if (!tech_pvt->connected) {
  753. break;
  754. }
  755. }
  756. /*
  757. * Native bridge handles voice frames in hardware. However, it
  758. * also passes the frames up to Asterisk anyway. Discard the
  759. * media frames.
  760. */
  761. return 0;
  762. default:
  763. if (!tech_pvt->connected) {
  764. native_request_start(bridge);
  765. break;
  766. }
  767. if (native_bridge_changed(bridge)) {
  768. native_request_stop(bridge);
  769. native_request_start(bridge);
  770. }
  771. break;
  772. }
  773. return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
  774. }
  775. static struct ast_bridge_technology native_bridge = {
  776. .name = "native_dahdi",
  777. .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
  778. .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
  779. .create = native_bridge_create,
  780. .start = native_bridge_start,
  781. .stop = native_bridge_stop,
  782. .destroy = native_bridge_destroy,
  783. .join = native_bridge_join,
  784. .leave = native_bridge_leave,
  785. .suspend = native_bridge_suspend,
  786. .unsuspend = native_bridge_unsuspend,
  787. .compatible = native_bridge_compatible,
  788. .write = native_bridge_write,
  789. };
  790. /*!
  791. * \internal
  792. * \brief Destroy the DAHDI native bridge support.
  793. * \since 12.0.0
  794. *
  795. * \return Nothing
  796. */
  797. void dahdi_native_unload(void)
  798. {
  799. ast_bridge_technology_unregister(&native_bridge);
  800. }
  801. /*!
  802. * \internal
  803. * \brief Initialize the DAHDI native bridge support.
  804. * \since 12.0.0
  805. *
  806. * \retval 0 on success.
  807. * \retval -1 on error.
  808. */
  809. int dahdi_native_load(struct ast_module *mod, const struct ast_channel_tech *tech)
  810. {
  811. dahdi_tech = tech;
  812. if (__ast_bridge_technology_register(&native_bridge, mod)) {
  813. dahdi_native_unload();
  814. return -1;
  815. }
  816. return 0;
  817. }