| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include "../sig_analog.h" |
| 36 |
#if defined(HAVE_PRI) |
| 37 |
#include "../sig_pri.h" |
| 38 |
#endif |
| 39 |
#include "../chan_dahdi.h" |
| 40 |
|
| 41 |
#include "bridge_native_dahdi.h" |
| 42 |
#include "asterisk/bridge.h" |
| 43 |
#include "asterisk/bridge_technology.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 |
|
| 51 |
struct native_pvt_chan { |
| 52 |
|
| 53 |
struct dahdi_pvt *pvt; |
| 54 |
|
| 55 |
struct ast_channel *owner; |
| 56 |
|
| 57 |
int index; |
| 58 |
|
| 59 |
int fd0; |
| 60 |
|
| 61 |
int state; |
| 62 |
|
| 63 |
unsigned int inthreeway:1; |
| 64 |
}; |
| 65 |
|
| 66 |
struct native_pvt_bridge { |
| 67 |
|
| 68 |
struct dahdi_pvt *master; |
| 69 |
|
| 70 |
struct dahdi_pvt *slave; |
| 71 |
|
| 72 |
unsigned int saw_start:1; |
| 73 |
|
| 74 |
unsigned int connected:1; |
| 75 |
#if defined(HAVE_PRI) && defined(PRI_2BCT) |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
unsigned int tried_trombone_removal:1; |
| 84 |
#endif |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
static void native_stop(struct ast_bridge *bridge) |
| 139 |
{ |
| 140 |
struct native_pvt_bridge *bridge_tech_pvt; |
| 141 |
struct ast_bridge_channel *cur; |
| 142 |
|
| 143 |
ast_assert(bridge->tech_pvt != NULL); |
| 144 |
|
| 145 |
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) { |
| 146 |
struct native_pvt_chan *chan_tech_pvt; |
| 147 |
|
| 148 |
chan_tech_pvt = cur->tech_pvt; |
| 149 |
if (!chan_tech_pvt) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
|
| 153 |
ast_mutex_lock(&chan_tech_pvt->pvt->lock); |
| 154 |
if (chan_tech_pvt->pvt == ast_channel_tech_pvt(cur->chan)) { |
| 155 |
dahdi_ec_enable(chan_tech_pvt->pvt); |
| 156 |
} |
| 157 |
if (chan_tech_pvt->index == SUB_REAL) { |
| 158 |
dahdi_dtmf_detect_enable(chan_tech_pvt->pvt); |
| 159 |
} |
| 160 |
ast_mutex_unlock(&chan_tech_pvt->pvt->lock); |
| 161 |
} |
| 162 |
|
| 163 |
bridge_tech_pvt = bridge->tech_pvt; |
| 164 |
dahdi_master_slave_unlink(bridge_tech_pvt->slave, bridge_tech_pvt->master, 1); |
| 165 |
|
| 166 |
ast_debug(2, "Stop native bridging %s and %s\n", |
| 167 |
ast_channel_name(AST_LIST_FIRST(&bridge->channels)->chan), |
| 168 |
ast_channel_name(AST_LIST_LAST(&bridge->channels)->chan)); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
static void native_request_stop(struct ast_bridge *bridge) |
| 181 |
{ |
| 182 |
struct native_pvt_bridge *tech_pvt; |
| 183 |
|
| 184 |
ast_assert(bridge->tech_pvt != NULL); |
| 185 |
|
| 186 |
tech_pvt = bridge->tech_pvt; |
| 187 |
if (!tech_pvt->connected) { |
| 188 |
return; |
| 189 |
} |
| 190 |
tech_pvt->connected = 0; |
| 191 |
|
| 192 |
|
| 193 |
native_stop(bridge); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
static int native_start(struct ast_bridge *bridge) |
| 210 |
{ |
| 211 |
struct native_pvt_bridge *tech_pvt; |
| 212 |
struct ast_bridge_channel *bc0; |
| 213 |
struct ast_bridge_channel *bc1; |
| 214 |
struct native_pvt_chan *npc0; |
| 215 |
struct native_pvt_chan *npc1; |
| 216 |
struct ast_channel *c0; |
| 217 |
struct ast_channel *c1; |
| 218 |
struct dahdi_pvt *p0; |
| 219 |
struct dahdi_pvt *p1; |
| 220 |
struct dahdi_pvt *master; |
| 221 |
struct dahdi_pvt *slave; |
| 222 |
int inconf; |
| 223 |
int nothing_ok; |
| 224 |
|
| 225 |
ast_assert(bridge->tech_pvt != NULL); |
| 226 |
|
| 227 |
bc0 = AST_LIST_FIRST(&bridge->channels); |
| 228 |
bc1 = AST_LIST_LAST(&bridge->channels); |
| 229 |
c0 = bc0->chan; |
| 230 |
c1 = bc1->chan; |
| 231 |
|
| 232 |
|
| 233 |
for (;;) { |
| 234 |
ast_channel_lock(c0); |
| 235 |
if (!ast_channel_trylock(c1)) { |
| 236 |
p0 = ast_channel_tech_pvt(c0); |
| 237 |
if (!ast_mutex_trylock(&p0->lock)) { |
| 238 |
p1 = ast_channel_tech_pvt(c1); |
| 239 |
if (!ast_mutex_trylock(&p1->lock)) { |
| 240 |
|
| 241 |
break; |
| 242 |
} |
| 243 |
ast_mutex_unlock(&p0->lock); |
| 244 |
} |
| 245 |
ast_channel_unlock(c1); |
| 246 |
} |
| 247 |
ast_channel_unlock(c0); |
| 248 |
sched_yield(); |
| 249 |
} |
| 250 |
|
| 251 |
npc0 = bc0->tech_pvt; |
| 252 |
ast_assert(npc0 != NULL); |
| 253 |
npc0->pvt = p0; |
| 254 |
npc0->owner = p0->owner; |
| 255 |
npc0->index = dahdi_get_index(c0, p0, 0); |
| 256 |
npc0->fd0 = ast_channel_fd(c0, 0); |
| 257 |
npc0->state = -1; |
| 258 |
npc0->inthreeway = p0->subs[SUB_REAL].inthreeway; |
| 259 |
|
| 260 |
npc1 = bc1->tech_pvt; |
| 261 |
ast_assert(npc1 != NULL); |
| 262 |
npc1->pvt = p1; |
| 263 |
npc1->owner = p1->owner; |
| 264 |
npc1->index = dahdi_get_index(c1, p1, 0); |
| 265 |
npc1->fd0 = ast_channel_fd(c1, 0); |
| 266 |
npc1->state = -1; |
| 267 |
npc1->inthreeway = p1->subs[SUB_REAL].inthreeway; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if (npc0->index < 0 || npc1->index < 0 |
| 274 |
#if defined(HAVE_PRI) |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|| (dahdi_sig_pri_lib_handles(p0->sig) |
| 280 |
&& ((struct sig_pri_chan *) p0->sig_pvt)->no_b_channel) |
| 281 |
|| (dahdi_sig_pri_lib_handles(p1->sig) |
| 282 |
&& ((struct sig_pri_chan *) p1->sig_pvt)->no_b_channel) |
| 283 |
#endif |
| 284 |
) { |
| 285 |
ast_mutex_unlock(&p0->lock); |
| 286 |
ast_mutex_unlock(&p1->lock); |
| 287 |
ast_channel_unlock(c0); |
| 288 |
ast_channel_unlock(c1); |
| 289 |
return -1; |
| 290 |
} |
| 291 |
|
| 292 |
inconf = 0; |
| 293 |
nothing_ok = 1; |
| 294 |
master = NULL; |
| 295 |
slave = NULL; |
| 296 |
if (npc0->index == SUB_REAL && npc1->index == SUB_REAL) { |
| 297 |
if (p0->owner && p1->owner) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) { |
| 303 |
master = p0; |
| 304 |
slave = p1; |
| 305 |
inconf = 1; |
| 306 |
} else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) { |
| 307 |
master = p1; |
| 308 |
slave = p0; |
| 309 |
inconf = 1; |
| 310 |
} else { |
| 311 |
ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n"); |
| 312 |
ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n", |
| 313 |
p0->channel, |
| 314 |
npc0->index, (p0->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0, |
| 315 |
p0->subs[SUB_REAL].inthreeway, |
| 316 |
p0->channel, |
| 317 |
npc0->index, (p1->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0, |
| 318 |
p1->subs[SUB_REAL].inthreeway); |
| 319 |
} |
| 320 |
nothing_ok = 0; |
| 321 |
} |
| 322 |
} else if (npc0->index == SUB_REAL && npc1->index == SUB_THREEWAY) { |
| 323 |
if (p1->subs[SUB_THREEWAY].inthreeway) { |
| 324 |
master = p1; |
| 325 |
slave = p0; |
| 326 |
nothing_ok = 0; |
| 327 |
} |
| 328 |
} else if (npc0->index == SUB_THREEWAY && npc1->index == SUB_REAL) { |
| 329 |
if (p0->subs[SUB_THREEWAY].inthreeway) { |
| 330 |
master = p0; |
| 331 |
slave = p1; |
| 332 |
nothing_ok = 0; |
| 333 |
} |
| 334 |
} else if (npc0->index == SUB_REAL && npc1->index == SUB_CALLWAIT) { |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
if (p1->subs[SUB_CALLWAIT].inthreeway) { |
| 340 |
master = p1; |
| 341 |
slave = p0; |
| 342 |
nothing_ok = 0; |
| 343 |
} |
| 344 |
} else if (npc0->index == SUB_CALLWAIT && npc1->index == SUB_REAL) { |
| 345 |
|
| 346 |
if (p0->subs[SUB_CALLWAIT].inthreeway) { |
| 347 |
master = p0; |
| 348 |
slave = p1; |
| 349 |
nothing_ok = 0; |
| 350 |
} |
| 351 |
} |
| 352 |
ast_debug(3, "master: %d, slave: %d, nothing_ok: %d\n", |
| 353 |
master ? master->channel : 0, |
| 354 |
slave ? slave->channel : 0, |
| 355 |
nothing_ok); |
| 356 |
if (master && slave) { |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
if (npc1->index == SUB_THREEWAY |
| 363 |
&& p1->subs[SUB_THREEWAY].inthreeway |
| 364 |
&& p1->subs[SUB_REAL].owner |
| 365 |
&& p1->subs[SUB_REAL].inthreeway |
| 366 |
&& ast_channel_state(p1->subs[SUB_REAL].owner) == AST_STATE_RINGING) { |
| 367 |
ast_debug(2, |
| 368 |
"Playing ringback on %d/%d(%s) since %d/%d(%s) is in a ringing three-way\n", |
| 369 |
p0->channel, npc0->index, ast_channel_name(c0), |
| 370 |
p1->channel, npc1->index, ast_channel_name(c1)); |
| 371 |
tone_zone_play_tone(p0->subs[npc0->index].dfd, DAHDI_TONE_RINGTONE); |
| 372 |
npc1->state = ast_channel_state(p1->subs[SUB_REAL].owner); |
| 373 |
} else { |
| 374 |
ast_debug(2, "Stopping tones on %d/%d(%s) talking to %d/%d(%s)\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, -1); |
| 378 |
} |
| 379 |
|
| 380 |
if (npc0->index == SUB_THREEWAY |
| 381 |
&& p0->subs[SUB_THREEWAY].inthreeway |
| 382 |
&& p0->subs[SUB_REAL].owner |
| 383 |
&& p0->subs[SUB_REAL].inthreeway |
| 384 |
&& ast_channel_state(p0->subs[SUB_REAL].owner) == AST_STATE_RINGING) { |
| 385 |
ast_debug(2, |
| 386 |
"Playing ringback on %d/%d(%s) since %d/%d(%s) is in a ringing three-way\n", |
| 387 |
p1->channel, npc1->index, ast_channel_name(c1), |
| 388 |
p0->channel, npc0->index, ast_channel_name(c0)); |
| 389 |
tone_zone_play_tone(p1->subs[npc1->index].dfd, DAHDI_TONE_RINGTONE); |
| 390 |
npc0->state = ast_channel_state(p0->subs[SUB_REAL].owner); |
| 391 |
} else { |
| 392 |
ast_debug(2, "Stopping tones on %d/%d(%s) talking to %d/%d(%s)\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, -1); |
| 396 |
} |
| 397 |
|
| 398 |
if (npc0->index == SUB_REAL && npc1->index == SUB_REAL) { |
| 399 |
if (!p0->echocanbridged || !p1->echocanbridged) { |
| 400 |
|
| 401 |
dahdi_ec_disable(p0); |
| 402 |
dahdi_ec_disable(p1); |
| 403 |
} |
| 404 |
} |
| 405 |
dahdi_master_slave_link(slave, master); |
| 406 |
master->inconference = inconf; |
| 407 |
} else if (!nothing_ok) { |
| 408 |
ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", |
| 409 |
p0->channel, subnames[npc0->index], |
| 410 |
p1->channel, subnames[npc1->index]); |
| 411 |
} |
| 412 |
dahdi_conf_update(p0); |
| 413 |
dahdi_conf_update(p1); |
| 414 |
|
| 415 |
ast_channel_unlock(c0); |
| 416 |
ast_channel_unlock(c1); |
| 417 |
|
| 418 |
|
| 419 |
if ((!master || !slave) && !nothing_ok) { |
| 420 |
ast_mutex_unlock(&p0->lock); |
| 421 |
ast_mutex_unlock(&p1->lock); |
| 422 |
return -1; |
| 423 |
} |
| 424 |
|
| 425 |
if (npc0->index == SUB_REAL) { |
| 426 |
dahdi_dtmf_detect_disable(p0); |
| 427 |
} |
| 428 |
if (npc1->index == SUB_REAL) { |
| 429 |
dahdi_dtmf_detect_disable(p1); |
| 430 |
} |
| 431 |
|
| 432 |
ast_mutex_unlock(&p0->lock); |
| 433 |
ast_mutex_unlock(&p1->lock); |
| 434 |
|
| 435 |
tech_pvt = bridge->tech_pvt; |
| 436 |
tech_pvt->master = master; |
| 437 |
tech_pvt->slave = slave; |
| 438 |
|
| 439 |
ast_debug(2, "Start native bridging %s and %s\n", |
| 440 |
ast_channel_name(c0), ast_channel_name(c1)); |
| 441 |
|
| 442 |
#if defined(HAVE_PRI) && defined(PRI_2BCT) |
| 443 |
if (!tech_pvt->tried_trombone_removal) { |
| 444 |
tech_pvt->tried_trombone_removal = 1; |
| 445 |
|
| 446 |
if (p0->pri && p0->pri == p1->pri && p0->pri->transfer) { |
| 447 |
q931_call *q931_c0; |
| 448 |
q931_call *q931_c1; |
| 449 |
|
| 450 |
|
| 451 |
ast_mutex_lock(&p0->pri->lock); |
| 452 |
ast_assert(dahdi_sig_pri_lib_handles(p0->sig)); |
| 453 |
ast_assert(dahdi_sig_pri_lib_handles(p1->sig)); |
| 454 |
q931_c0 = ((struct sig_pri_chan *) (p0->sig_pvt))->call; |
| 455 |
q931_c1 = ((struct sig_pri_chan *) (p1->sig_pvt))->call; |
| 456 |
if (q931_c0 && q931_c1) { |
| 457 |
pri_channel_bridge(q931_c0, q931_c1); |
| 458 |
ast_debug(2, "Attempt to eliminate tromboned call with %s and %s\n", |
| 459 |
ast_channel_name(c0), ast_channel_name(c1)); |
| 460 |
} |
| 461 |
ast_mutex_unlock(&p0->pri->lock); |
| 462 |
} |
| 463 |
} |
| 464 |
#endif |
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
static void native_request_start(struct ast_bridge *bridge) |
| 479 |
{ |
| 480 |
struct native_pvt_bridge *tech_pvt; |
| 481 |
struct ast_bridge_channel *cur; |
| 482 |
|
| 483 |
ast_assert(bridge->tech_pvt != NULL); |
| 484 |
|
| 485 |
tech_pvt = bridge->tech_pvt; |
| 486 |
|
| 487 |
if (bridge->num_channels != 2 || !tech_pvt->saw_start || tech_pvt->connected) { |
| 488 |
return; |
| 489 |
} |
| 490 |
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) { |
| 491 |
if (cur->suspended || !cur->tech_pvt) { |
| 492 |
return; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
if (native_start(bridge)) { |
| 498 |
return; |
| 499 |
} |
| 500 |
tech_pvt->connected = 1; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
static int native_bridge_start(struct ast_bridge *bridge) |
| 515 |
{ |
| 516 |
struct native_pvt_bridge *tech_pvt; |
| 517 |
|
| 518 |
ast_assert(bridge->tech_pvt != NULL); |
| 519 |
|
| 520 |
tech_pvt = bridge->tech_pvt; |
| 521 |
tech_pvt->saw_start = 1; |
| 522 |
|
| 523 |
native_request_start(bridge); |
| 524 |
return 0; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
static void native_bridge_stop(struct ast_bridge *bridge) |
| 535 |
{ |
| 536 |
struct native_pvt_bridge *tech_pvt; |
| 537 |
|
| 538 |
tech_pvt = bridge->tech_pvt; |
| 539 |
if (!tech_pvt) { |
| 540 |
return; |
| 541 |
} |
| 542 |
|
| 543 |
tech_pvt->saw_start = 0; |
| 544 |
native_request_stop(bridge); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
static int native_bridge_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 558 |
{ |
| 559 |
struct native_pvt_chan *tech_pvt; |
| 560 |
struct ast_channel *c0; |
| 561 |
struct ast_channel *c1; |
| 562 |
|
| 563 |
ast_assert(!bridge_channel->tech_pvt); |
| 564 |
|
| 565 |
tech_pvt = ast_calloc(1, sizeof(*tech_pvt)); |
| 566 |
if (!tech_pvt) { |
| 567 |
return -1; |
| 568 |
} |
| 569 |
|
| 570 |
bridge_channel->tech_pvt = tech_pvt; |
| 571 |
native_request_start(bridge); |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
c0 = AST_LIST_FIRST(&bridge->channels)->chan; |
| 579 |
c1 = AST_LIST_LAST(&bridge->channels)->chan; |
| 580 |
if (c0 == c1) { |
| 581 |
return 0; |
| 582 |
} |
| 583 |
return ast_channel_make_compatible(c0, c1); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
static void native_bridge_leave(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 594 |
{ |
| 595 |
struct native_pvt_chan *tech_pvt; |
| 596 |
|
| 597 |
native_request_stop(bridge); |
| 598 |
|
| 599 |
tech_pvt = bridge_channel->tech_pvt; |
| 600 |
bridge_channel->tech_pvt = NULL; |
| 601 |
ast_free(tech_pvt); |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
static void native_bridge_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 612 |
{ |
| 613 |
native_request_stop(bridge); |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
static void native_bridge_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel) |
| 624 |
{ |
| 625 |
native_request_start(bridge); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
static int native_bridge_is_capable(struct ast_bridge_channel *bridge_channel) |
| 638 |
{ |
| 639 |
struct ast_channel *chan = bridge_channel->chan; |
| 640 |
struct dahdi_pvt *pvt; |
| 641 |
int is_capable; |
| 642 |
|
| 643 |
if (ao2_container_count(bridge_channel->features->dtmf_hooks)) { |
| 644 |
ast_debug(2, "Channel '%s' has DTMF hooks.\n", ast_channel_name(chan)); |
| 645 |
return 0; |
| 646 |
} |
| 647 |
|
| 648 |
ast_channel_lock(chan); |
| 649 |
|
| 650 |
if (dahdi_tech != ast_channel_tech(chan)) { |
| 651 |
ast_debug(2, "Channel '%s' is not %s.\n", |
| 652 |
ast_channel_name(chan), dahdi_tech->type); |
| 653 |
ast_channel_unlock(chan); |
| 654 |
return 0; |
| 655 |
} |
| 656 |
if (ast_channel_has_audio_frame_or_monitor(chan)) { |
| 657 |
ast_debug(2, "Channel '%s' has an active monitor, audiohook, or framehook.\n", |
| 658 |
ast_channel_name(chan)); |
| 659 |
ast_channel_unlock(chan); |
| 660 |
return 0; |
| 661 |
} |
| 662 |
pvt = ast_channel_tech_pvt(chan); |
| 663 |
if (!pvt || !pvt->sig) { |
| 664 |
|
| 665 |
ast_channel_unlock(chan); |
| 666 |
return 0; |
| 667 |
} |
| 668 |
|
| 669 |
is_capable = 1; |
| 670 |
ast_mutex_lock(&pvt->lock); |
| 671 |
|
| 672 |
if (pvt->callwaiting && pvt->callwaitingcallerid) { |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
ast_debug(2, "Channel '%s' has call waiting caller ID enabled.\n", |
| 678 |
ast_channel_name(chan)); |
| 679 |
is_capable = 0; |
| 680 |
} |
| 681 |
|
| 682 |
ast_mutex_unlock(&pvt->lock); |
| 683 |
ast_channel_unlock(chan); |
| 684 |
|
| 685 |
return is_capable; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
static int native_bridge_compatible(struct ast_bridge *bridge) |
| 700 |
{ |
| 701 |
struct ast_bridge_channel *cur; |
| 702 |
|
| 703 |
|
| 704 |
if (bridge->num_channels != 2) { |
| 705 |
ast_debug(1, "Bridge %s: Cannot use native DAHDI. Must have two channels.\n", |
| 706 |
bridge->uniqueid); |
| 707 |
return 0; |
| 708 |
} |
| 709 |
|
| 710 |
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) { |
| 711 |
if (!native_bridge_is_capable(cur)) { |
| 712 |
ast_debug(1, "Bridge %s: Cannot use native DAHDI. Channel '%s' not compatible.\n", |
| 713 |
bridge->uniqueid, ast_channel_name(cur->chan)); |
| 714 |
return 0; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
return -1; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
static int native_chan_changed(struct ast_bridge_channel *bridge_channel) |
| 734 |
{ |
| 735 |
struct native_pvt_chan *tech_pvt; |
| 736 |
struct ast_channel *chan; |
| 737 |
struct dahdi_pvt *pvt; |
| 738 |
int idx = -1; |
| 739 |
|
| 740 |
ast_assert(bridge_channel->tech_pvt != NULL); |
| 741 |
|
| 742 |
tech_pvt = bridge_channel->tech_pvt; |
| 743 |
|
| 744 |
chan = bridge_channel->chan; |
| 745 |
ast_channel_lock(chan); |
| 746 |
pvt = ast_channel_tech_pvt(chan); |
| 747 |
if (tech_pvt->pvt == pvt) { |
| 748 |
idx = dahdi_get_index(chan, pvt, 1); |
| 749 |
} |
| 750 |
ast_channel_unlock(chan); |
| 751 |
|
| 752 |
if ( |
| 753 |
tech_pvt->pvt != pvt |
| 754 |
|
| 755 |
|| tech_pvt->owner != pvt->owner |
| 756 |
|
| 757 |
|| tech_pvt->inthreeway != pvt->subs[SUB_REAL].inthreeway |
| 758 |
|
| 759 |
|| tech_pvt->index != idx |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|| tech_pvt->fd0 != ast_channel_fd(chan, 0) |
| 765 |
|
| 766 |
|| (pvt->subs[SUB_REAL].owner |
| 767 |
&& tech_pvt->state > -1 |
| 768 |
&& tech_pvt->state != ast_channel_state(pvt->subs[SUB_REAL].owner))) { |
| 769 |
return -1; |
| 770 |
} |
| 771 |
|
| 772 |
return 0; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
static int native_bridge_changed(struct ast_bridge *bridge) |
| 788 |
{ |
| 789 |
struct ast_bridge_channel *cur; |
| 790 |
|
| 791 |
AST_LIST_TRAVERSE(&bridge->channels, cur, entry) { |
| 792 |
if (native_chan_changed(cur)) { |
| 793 |
ast_debug(1, "Bridge %s: Something changed on channel '%s'.\n", |
| 794 |
bridge->uniqueid, ast_channel_name(cur->chan)); |
| 795 |
return -1; |
| 796 |
} |
| 797 |
} |
| 798 |
return 0; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
static int native_bridge_write(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame) |
| 814 |
{ |
| 815 |
struct native_pvt_bridge *tech_pvt; |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
ast_assert(bridge->tech_pvt != NULL); |
| 823 |
|
| 824 |
|
| 825 |
tech_pvt = bridge->tech_pvt; |
| 826 |
switch (frame->frametype) { |
| 827 |
case AST_FRAME_VOICE: |
| 828 |
case AST_FRAME_VIDEO: |
| 829 |
if (!tech_pvt->connected) { |
| 830 |
|
| 831 |
break; |
| 832 |
} |
| 833 |
if (native_bridge_changed(bridge)) { |
| 834 |
native_request_stop(bridge); |
| 835 |
native_request_start(bridge); |
| 836 |
if (!tech_pvt->connected) { |
| 837 |
break; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
return 0; |
| 847 |
default: |
| 848 |
if (!tech_pvt->connected) { |
| 849 |
native_request_start(bridge); |
| 850 |
break; |
| 851 |
} |
| 852 |
if (native_bridge_changed(bridge)) { |
| 853 |
native_request_stop(bridge); |
| 854 |
native_request_start(bridge); |
| 855 |
} |
| 856 |
break; |
| 857 |
} |
| 858 |
|
| 859 |
return ast_bridge_queue_everyone_else(bridge, bridge_channel, frame); |
| 860 |
} |
| 861 |
|
| 862 |
static struct ast_bridge_technology native_bridge = { |
| 863 |
.name = "native_dahdi", |
| 864 |
.capabilities = AST_BRIDGE_CAPABILITY_NATIVE, |
| 865 |
.preference = AST_BRIDGE_PREFERENCE_BASE_NATIVE, |
| 866 |
.create = native_bridge_create, |
| 867 |
.start = native_bridge_start, |
| 868 |
.stop = native_bridge_stop, |
| 869 |
.destroy = native_bridge_destroy, |
| 870 |
.join = native_bridge_join, |
| 871 |
.leave = native_bridge_leave, |
| 872 |
.suspend = native_bridge_suspend, |
| 873 |
.unsuspend = native_bridge_unsuspend, |
| 874 |
.compatible = native_bridge_compatible, |
| 875 |
.write = native_bridge_write, |
| 876 |
}; |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
void dahdi_native_unload(void) |
| 884 |
{ |
| 885 |
ast_bridge_technology_unregister(&native_bridge); |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
int dahdi_native_load(const struct ast_channel_tech *tech) |
| 897 |
{ |
| 898 |
dahdi_tech = tech; |
| 899 |
|
| 900 |
if (ast_bridge_technology_register(&native_bridge)) { |
| 901 |
dahdi_native_unload(); |
| 902 |
return -1; |
| 903 |
} |
| 904 |
|
| 905 |
return 0; |
| 906 |
} |