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