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