9df82b29bf090ce56a9625659dd793415985a0c8
[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 "bridge_native_dahdi.h"
44 #include "asterisk/bridge.h"
45 #include "asterisk/bridge_technology.h"
46 #include "asterisk/frame.h"
47 #include "asterisk/format_cache.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         /*
582          * Make the channels compatible in case the native bridge did
583          * not start for some reason and we need to fallback to 1-1
584          * bridging.
585          */
586         c0 = AST_LIST_FIRST(&bridge->channels)->chan;
587         c1 = AST_LIST_LAST(&bridge->channels)->chan;
588         if (c0 == c1) {
589                 return 0;
590         }
591         return ast_channel_make_compatible(c0, c1);
592 }
593
594 /*!
595  * \internal
596  * \brief Remove a channel from a bridging technology instance for a bridge.
597  * \since 12.0.0
598  *
599  * \note On entry, bridge is already locked.
600  */
601 static void native_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
602 {
603         struct native_pvt_chan *tech_pvt;
604
605         native_request_stop(bridge);
606
607         tech_pvt = bridge_channel->tech_pvt;
608         bridge_channel->tech_pvt = NULL;
609         ast_free(tech_pvt);
610 }
611
612 /*!
613  * \internal
614  * \brief Suspend a channel on a bridging technology instance for a bridge.
615  * \since 12.0.0
616  *
617  * \note On entry, bridge is already locked.
618  */
619 static void native_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
620 {
621         native_request_stop(bridge);
622 }
623
624 /*!
625  * \internal
626  * \brief Unsuspend a channel on a bridging technology instance for a bridge.
627  * \since 12.0.0
628  *
629  * \note On entry, bridge is already locked.
630  */
631 static void native_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
632 {
633         native_request_start(bridge);
634 }
635
636 /*!
637  * \internal
638  * \brief Check if channel is compatible.
639  * \since 12.0.0
640  *
641  * \param bridge_channel Is this channel compatible.
642  *
643  * \retval TRUE if channel is compatible with native DAHDI bridge.
644  */
645 static int native_bridge_is_capable(struct ast_bridge_channel *bridge_channel)
646 {
647         struct ast_channel *chan = bridge_channel->chan;
648         struct dahdi_pvt *pvt;
649         int is_capable;
650
651         if (ao2_container_count(bridge_channel->features->dtmf_hooks)) {
652                 ast_debug(2, "Channel '%s' has DTMF hooks.\n", ast_channel_name(chan));
653                 return 0;
654         }
655
656         ast_channel_lock(chan);
657
658         if (dahdi_tech != ast_channel_tech(chan)) {
659                 ast_debug(2, "Channel '%s' is not %s.\n",
660                         ast_channel_name(chan), dahdi_tech->type);
661                 ast_channel_unlock(chan);
662                 return 0;
663         }
664         if (ast_channel_has_audio_frame_or_monitor(chan)) {
665                 ast_debug(2, "Channel '%s' has an active monitor, audiohook, or framehook.\n",
666                         ast_channel_name(chan));
667                 ast_channel_unlock(chan);
668                 return 0;
669         }
670         pvt = ast_channel_tech_pvt(chan);
671         if (!pvt || !pvt->sig) {
672                 /* No private; or signaling is for a pseudo channel. */
673                 ast_channel_unlock(chan);
674                 return 0;
675         }
676
677         is_capable = 1;
678         ast_mutex_lock(&pvt->lock);
679
680         if (pvt->callwaiting && pvt->callwaitingcallerid) {
681                 /*
682                  * Call Waiting Caller ID requires DTMF detection to know if it
683                  * can send the CID spill.
684                  */
685                 ast_debug(2, "Channel '%s' has call waiting caller ID enabled.\n",
686                         ast_channel_name(chan));
687                 is_capable = 0;
688         }
689
690         ast_mutex_unlock(&pvt->lock);
691         ast_channel_unlock(chan);
692
693         return is_capable;
694 }
695
696 /*!
697  * \internal
698  * \brief Check if a bridge is compatible with the bridging technology.
699  * \since 12.0.0
700  *
701  * \retval 0 if not compatible
702  * \retval non-zero if compatible
703  *
704  * \note On entry, bridge may or may not already be locked.
705  * However, it can be accessed as if it were locked.
706  */
707 static int native_bridge_compatible(struct ast_bridge *bridge)
708 {
709         struct ast_bridge_channel *cur;
710
711         /* We require two channels before even considering native bridging. */
712         if (bridge->num_channels != 2) {
713                 ast_debug(1, "Bridge %s: Cannot use native DAHDI.  Must have two channels.\n",
714                         bridge->uniqueid);
715                 return 0;
716         }
717
718         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
719                 if (!native_bridge_is_capable(cur)) {
720                         ast_debug(1, "Bridge %s: Cannot use native DAHDI.  Channel '%s' not compatible.\n",
721                                 bridge->uniqueid, ast_channel_name(cur->chan));
722                         return 0;
723                 }
724         }
725
726         return -1;
727 }
728
729 /*!
730  * \internal
731  * \brief Check if something changed on the channel.
732  * \since 12.0.0
733  *
734  * \param bridge_channel What to operate upon.
735  *
736  * \retval 0 Nothing changed.
737  * \retval -1 Something changed.
738  *
739  * \note On entry, bridge_channel->bridge is already locked.
740  */
741 static int native_chan_changed(struct ast_bridge_channel *bridge_channel)
742 {
743         struct native_pvt_chan *tech_pvt;
744         struct ast_channel *chan;
745         struct dahdi_pvt *pvt;
746         int idx = -1;
747
748         ast_assert(bridge_channel->tech_pvt != NULL);
749
750         tech_pvt = bridge_channel->tech_pvt;
751
752         chan = bridge_channel->chan;
753         ast_channel_lock(chan);
754         pvt = ast_channel_tech_pvt(chan);
755         if (tech_pvt->pvt == pvt) {
756                 idx = dahdi_get_index(chan, pvt, 1);
757         }
758         ast_channel_unlock(chan);
759
760         if (/* Did chan get masqueraded or PRI change associated B channel? */
761                 tech_pvt->pvt != pvt
762                 /* Did the pvt active owner change? */
763                 || tech_pvt->owner != pvt->owner
764                 /* Did the pvt three way call status change? */
765                 || tech_pvt->inthreeway != pvt->subs[SUB_REAL].inthreeway
766                 /* Did the owner index change? */
767                 || tech_pvt->index != idx
768                 /*
769                  * Did chan file descriptor change? (This seems redundant with
770                  * masquerade and active owner change checks.)
771                  */
772                 || tech_pvt->fd0 != ast_channel_fd(chan, 0)
773                 /* Did chan state change? i.e. Did it stop ringing? */
774                 || (pvt->subs[SUB_REAL].owner
775                         && tech_pvt->state > -1
776                         && tech_pvt->state != ast_channel_state(pvt->subs[SUB_REAL].owner))) {
777                 return -1;
778         }
779
780         return 0;
781 }
782
783 /*!
784  * \internal
785  * \brief Check if something changed on the bridge channels.
786  * \since 12.0.0
787  *
788  * \param bridge What to operate upon.
789  *
790  * \retval 0 Nothing changed.
791  * \retval -1 Something changed.
792  *
793  * \note On entry, bridge is already locked.
794  */
795 static int native_bridge_changed(struct ast_bridge *bridge)
796 {
797         struct ast_bridge_channel *cur;
798
799         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
800                 if (native_chan_changed(cur)) {
801                         ast_debug(1, "Bridge %s: Something changed on channel '%s'.\n",
802                                 bridge->uniqueid, ast_channel_name(cur->chan));
803                         return -1;
804                 }
805         }
806         return 0;
807 }
808
809 /*!
810  * \internal
811  * \brief Write a frame into the bridging technology instance for a bridge.
812  * \since 12.0.0
813  *
814  * \note The bridge must be tolerant of bridge_channel being NULL.
815  *
816  * \retval 0 Frame accepted into the bridge.
817  * \retval -1 Frame needs to be deferred.
818  *
819  * \note On entry, bridge is already locked.
820  */
821 static int native_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
822 {
823         struct native_pvt_bridge *tech_pvt;
824
825         /*
826          * When we are not native bridged by DAHDI, we are like a normal
827          * 1-1 bridge.
828          */
829
830         ast_assert(bridge->tech_pvt != NULL);
831
832         /* Recheck native bridging validity. */
833         tech_pvt = bridge->tech_pvt;
834         switch (frame->frametype) {
835         case AST_FRAME_VOICE:
836         case AST_FRAME_VIDEO:
837                 if (!tech_pvt->connected) {
838                         /* Don't try to start native mode on media frames. */
839                         break;
840                 }
841                 if (native_bridge_changed(bridge)) {
842                         native_request_stop(bridge);
843                         native_request_start(bridge);
844                         if (!tech_pvt->connected) {
845                                 break;
846                         }
847                 }
848
849                 /*
850                  * Native bridge handles voice frames in hardware.  However, it
851                  * also passes the frames up to Asterisk anyway.  Discard the
852                  * media frames.
853                  */
854                 return 0;
855         default:
856                 if (!tech_pvt->connected) {
857                         native_request_start(bridge);
858                         break;
859                 }
860                 if (native_bridge_changed(bridge)) {
861                         native_request_stop(bridge);
862                         native_request_start(bridge);
863                 }
864                 break;
865         }
866
867         return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame);
868 }
869
870 static struct ast_bridge_technology native_bridge = {
871         .name = "native_dahdi",
872         .capabilities = AST_BRIDGE_CAPABILITY_NATIVE,
873         .preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE,
874         .create = native_bridge_create,
875         .start = native_bridge_start,
876         .stop = native_bridge_stop,
877         .destroy = native_bridge_destroy,
878         .join = native_bridge_join,
879         .leave = native_bridge_leave,
880         .suspend = native_bridge_suspend,
881         .unsuspend = native_bridge_unsuspend,
882         .compatible = native_bridge_compatible,
883         .write = native_bridge_write,
884 };
885
886 /*!
887  * \internal
888  * \brief Destroy the DAHDI native bridge support.
889  * \since 12.0.0
890  *
891  * \return Nothing
892  */
893 void dahdi_native_unload(void)
894 {
895         ast_bridge_technology_unregister(&native_bridge);
896 }
897
898 /*!
899  * \internal
900  * \brief Initialize the DAHDI native bridge support.
901  * \since 12.0.0
902  *
903  * \retval 0 on success.
904  * \retval -1 on error.
905  */
906 int dahdi_native_load(struct ast_module *mod, const struct ast_channel_tech *tech)
907 {
908         dahdi_tech = tech;
909
910         if (__ast_bridge_technology_register(&native_bridge, mod)) {
911                 dahdi_native_unload();
912                 return -1;
913         }
914
915         return 0;
916 }