2 * Asterisk -- A telephony toolkit for Linux.
4 * Zaptel Pseudo TDM interface
6 * Copyright (C) 2003-2004, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #include <asterisk/lock.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <asterisk/file.h>
25 #include <asterisk/ulaw.h>
26 #include <asterisk/alaw.h>
27 #include <asterisk/callerid.h>
28 #include <asterisk/adsi.h>
29 #include <asterisk/cli.h>
30 #include <asterisk/cdr.h>
31 #include <asterisk/features.h>
32 #include <asterisk/musiconhold.h>
33 #include <asterisk/say.h>
34 #include <asterisk/tdd.h>
35 #include <asterisk/app.h>
36 #include <asterisk/dsp.h>
37 #include <asterisk/astdb.h>
38 #include <asterisk/manager.h>
39 #include <asterisk/causes.h>
40 #include <asterisk/term.h>
41 #include <asterisk/utils.h>
42 #include <sys/signal.h>
47 #include <sys/ioctl.h>
49 #include <linux/zaptel.h>
52 #endif /* __linux__ */
59 #error "You need newer libpri"
66 #include "../asterisk.h"
69 #error "Your zaptel is too old. please cvs update"
73 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
74 * the user hangs up to reset the state machine so ring works properly.
75 * This is used to be able to support kewlstart by putting the zhone in
76 * groundstart mode since their forward disconnect supervision is entirely
77 * broken even though their documentation says it isn't and their support
78 * is entirely unwilling to provide any assistance with their channel banks
79 * even though their web site says they support their products for life.
82 /* #define ZHONE_HACK */
85 * Define if you want to check the hook state for an FXO (FXS signalled) interface
86 * before dialing on it. Certain FXO interfaces always think they're out of
87 * service with this method however.
89 /* #define ZAP_CHECK_HOOKSTATE */
91 /* Typically, how many rings before we should send Caller*ID */
92 #define DEFAULT_CIDRINGS 1
94 #define CHANNEL_PSEUDO -12
96 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
98 static char *desc = "Zapata Telephony"
107 static char *tdesc = "Zapata Telephony Driver"
116 static char *type = "Zap";
117 static char *config = "zapata.conf";
119 #define SIG_EM ZT_SIG_EM
120 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
121 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
122 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
123 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
124 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
125 #define SIG_FXSLS ZT_SIG_FXSLS
126 #define SIG_FXSGS ZT_SIG_FXSGS
127 #define SIG_FXSKS ZT_SIG_FXSKS
128 #define SIG_FXOLS ZT_SIG_FXOLS
129 #define SIG_FXOGS ZT_SIG_FXOGS
130 #define SIG_FXOKS ZT_SIG_FXOKS
131 #define SIG_PRI ZT_SIG_CLEAR
132 #define SIG_R2 ZT_SIG_CAS
133 #define SIG_SF ZT_SIG_SF
134 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
135 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
136 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
137 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
138 #define SIG_EM_E1 ZT_SIG_EM_E1
139 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
140 #define SIG_GR303FXSKS (0x200000 | ZT_SIG_FXSKS)
143 #define NUM_DCHANS 4 /* No more than 4 d-channels */
144 #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
145 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
147 #define CHAN_PSEUDO -2
149 #define DCHAN_PROVISIONED (1 << 0)
150 #define DCHAN_NOTINALARM (1 << 1)
151 #define DCHAN_UP (1 << 2)
153 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
155 static char context[AST_MAX_EXTENSION] = "default";
156 static char cid_num[256] = "";
157 static char cid_name[256] = "";
159 static char language[MAX_LANGUAGE] = "";
160 static char musicclass[MAX_LANGUAGE] = "";
161 static char progzone[10]= "";
163 static int usedistinctiveringdetection = 0;
165 static int use_callerid = 1;
166 static int cid_signalling = CID_SIG_BELL;
167 static int cid_start = CID_START_RING;
168 static int zaptrcallerid = 0;
169 static int cur_signalling = -1;
171 static unsigned int cur_group = 0;
172 static unsigned int cur_callergroup = 0;
173 static unsigned int cur_pickupgroup = 0;
174 static int relaxdtmf = 0;
176 static int immediate = 0;
178 static int stripmsd = 0;
180 static int callwaiting = 0;
182 static int callwaitingcallerid = 0;
184 static int hidecallerid = 0;
186 static int restrictcid = 0;
188 static int use_callingpres = 0;
190 static int callreturn = 0;
192 static int threewaycalling = 0;
194 static int transfer = 0;
196 static int cancallforward = 0;
198 static float rxgain = 0.0;
200 static float txgain = 0.0;
202 static int tonezone = -1;
204 static int echocancel;
206 static int echotraining;
210 static int echocanbridged = 0;
212 static int busydetect = 0;
214 static int busycount = 3;
216 static int callprogress = 0;
218 static char accountcode[20] = "";
220 static char mailbox[AST_MAX_EXTENSION];
222 static int amaflags = 0;
226 static int numbufs = 4;
228 static int cur_prewink = -1;
229 static int cur_preflash = -1;
230 static int cur_wink = -1;
231 static int cur_flash = -1;
232 static int cur_start = -1;
233 static int cur_rxwink = -1;
234 static int cur_rxflash = -1;
235 static int cur_debounce = -1;
237 static int priindication_oob = 0;
240 static int minunused = 2;
241 static int minidle = 0;
242 static char idleext[AST_MAX_EXTENSION];
243 static char idledial[AST_MAX_EXTENSION];
244 static int overlapdial = 0;
245 static struct ast_channel inuse = { "GR-303InUse" };
246 #ifdef PRI_GETSET_TIMERS
247 static int pritimers[PRI_MAX_TIMERS];
251 /* Wait up to 16 seconds for first digit (FXO logic) */
252 static int firstdigittimeout = 16000;
254 /* How long to wait for following digits (FXO logic) */
255 static int gendigittimeout = 8000;
257 /* How long to wait for an extra digit, if there is an ambiguous match */
258 static int matchdigittimeout = 3000;
260 static int usecnt =0;
261 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
263 /* Protect the interface list (of zt_pvt's) */
264 AST_MUTEX_DEFINE_STATIC(iflock);
266 static int ifcount = 0;
268 /* Protect the monitoring thread, so only one process can kill or start it, and not
269 when it's doing something critical. */
270 AST_MUTEX_DEFINE_STATIC(monlock);
272 /* This is the thread for the monitor which checks for input on the channels
273 which are not currently in use. */
274 static pthread_t monitor_thread = AST_PTHREADT_NULL;
276 static int restart_monitor(void);
278 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
280 static int zt_sendtext(struct ast_channel *c, char *text);
282 static inline int zt_get_event(int fd)
284 /* Avoid the silly zt_getevent which ignores a bunch of events */
286 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
290 static inline int zt_wait_event(int fd)
292 /* Avoid the silly zt_waitevent which ignores a bunch of events */
294 i = ZT_IOMUX_SIGEVENT;
295 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
296 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
300 /* Chunk size to read -- we use 20ms chunks to make things happy. */
301 #define READ_SIZE 160
303 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
304 #define MASK_INUSE (1 << 1) /* Channel currently in use */
306 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
307 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
308 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
309 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
310 #define RINGT ( (8000 * 8) / READ_SIZE)
316 static int r2prot = -1;
322 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8))
323 #define PRI_CHANNEL(p) ((p) & 0xff)
324 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
327 pthread_t master; /* Thread of master */
328 ast_mutex_t lock; /* Mutex */
329 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
330 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
331 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
332 int minunused; /* Min # of channels to keep empty */
333 int minidle; /* Min # of "idling" calls to keep active */
334 int nodetype; /* Node type */
335 int switchtype; /* Type of switch to emulate */
336 int nsf; /* Network-Specific Facilities */
337 int dialplan; /* Dialing plan */
338 int localdialplan; /* Local dialing plan */
339 int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
340 int trunkgroup; /* What our trunkgroup is */
341 int mastertrunkgroup; /* What trunk group is our master */
342 int prilogicalspan; /* Logical span number within trunk group */
343 int numchans; /* Num of channels we represent */
344 int overlapdial; /* In overlap dialing mode */
345 struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
346 int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
347 struct pri *pri; /* Currently active D-channel */
349 int fds[NUM_DCHANS]; /* FD's for d-channels */
355 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
356 struct zt_pvt *crvs; /* Member CRV structs */
357 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
361 static struct zt_pri pris[NUM_SPANS];
363 static int pritype = PRI_CPE;
366 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
368 #define DEFAULT_PRI_DEBUG 0
371 static inline void pri_rel(struct zt_pri *pri)
373 ast_mutex_unlock(&pri->lock);
376 static int switchtype = PRI_SWITCH_NI2;
377 static int nsf = PRI_NSF_NONE;
378 static int dialplan = PRI_NATIONAL_ISDN + 1;
379 static int localdialplan = PRI_NATIONAL_ISDN + 1;
382 /* Shut up the compiler */
386 #define SUB_REAL 0 /* Active call */
387 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
388 #define SUB_THREEWAY 2 /* Three-way call */
391 static struct zt_distRings drings;
393 struct distRingData {
396 struct ringContextData {
397 char contextData[AST_MAX_EXTENSION];
399 struct zt_distRings {
400 struct distRingData ringnum[3];
401 struct ringContextData ringContext[3];
404 static char *subnames[] = {
410 struct zt_subchannel {
412 struct ast_channel *owner;
414 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
415 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
426 #define CONF_USER_REAL (1 << 0)
427 #define CONF_USER_THIRDCALL (1 << 1)
431 static struct zt_pvt {
433 struct ast_channel *owner; /* Our current active owner (if applicable) */
434 /* Up to three channels can be associated with this call */
436 struct zt_subchannel sub_unused; /* Just a safety precaution */
437 struct zt_subchannel subs[3]; /* Sub-channels */
438 struct zt_confinfo saveconf; /* Saved conference info */
440 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
441 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
442 int inconference; /* If our real should be in the conference */
444 int sig; /* Signalling style */
445 int radio; /* radio type */
446 int firstradio; /* first radio flag */
449 int tonezone; /* tone zone for this chan, or -1 for default */
450 struct zt_pvt *next; /* Next channel in list */
451 struct zt_pvt *prev; /* Prev channel in list */
453 struct zt_distRings drings;
454 int usedistinctiveringdetection;
456 char context[AST_MAX_EXTENSION];
457 char defcontext[AST_MAX_EXTENSION];
458 char exten[AST_MAX_EXTENSION];
459 char language[MAX_LANGUAGE];
460 char musicclass[MAX_LANGUAGE];
461 char cid_num[AST_MAX_EXTENSION];
462 char cid_name[AST_MAX_EXTENSION];
463 char lastcid_num[AST_MAX_EXTENSION];
464 char lastcid_name[AST_MAX_EXTENSION];
465 char *origcid_num; /* malloced original callerid */
466 char *origcid_name; /* malloced original callerid */
467 char callwait_num[AST_MAX_EXTENSION];
468 char callwait_name[AST_MAX_EXTENSION];
469 char rdnis[AST_MAX_EXTENSION];
470 char dnid[AST_MAX_EXTENSION];
473 int confno; /* Our conference */
474 int confusers; /* Who is using our conference */
475 int propconfno; /* Propagated conference number */
476 unsigned int callgroup;
477 unsigned int pickupgroup;
478 int immediate; /* Answer before getting digits? */
479 int channel; /* Channel Number or CRV */
480 int span; /* Span number */
482 time_t guardtime; /* Must wait this much time before using for new call */
484 int use_callerid; /* Whether or not to use caller id on this channel */
485 int cid_signalling; /* CID signalling type bell202 or v23 */
486 int cid_start; /* CID start indicator, polarity or ring */
489 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
490 int restrictcid; /* Whether restrict the callerid -> only send ANI */
491 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
492 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
493 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
494 int cidcwexpire; /* When to expire our muting for CID/CW */
495 unsigned char *cidspill;
511 int callwaitingcallerid;
520 int priindication_oob;
521 struct timeval flashtime; /* Last flash-hook time */
523 int cref; /* Call reference number */
524 ZT_DIAL_OPERATION dop;
528 char accountcode[20]; /* Account code */
529 int amaflags; /* AMA Flags */
530 char didtdd; /* flag to say its done it once */
531 struct tdd_state *tdd; /* TDD flag */
534 char call_forward[AST_MAX_EXTENSION];
535 char mailbox[AST_MAX_EXTENSION];
540 int confirmanswer; /* Wait for '#' to confirm answer */
541 int distinctivering; /* Which distinctivering to use */
542 int cidrings; /* Which ring to deliver CID on */
544 int faxhandled; /* Has a fax tone already been handled? */
546 char mate; /* flag to say its in MATE mode */
547 int pulsedial; /* whether a pulse dial phone is detected */
548 int dtmfrelax; /* whether to run in relaxed DTMF mode */
550 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
553 struct zt_pvt *bearer;
554 struct zt_pvt *realcall;
562 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
571 } *iflist = NULL, *ifend = NULL;
574 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
576 #define GET_CHANNEL(p) ((p)->channel)
579 struct zt_pvt *round_robin[32];
582 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
585 /* Grab the lock first */
587 res = ast_mutex_trylock(&pri->lock);
589 ast_mutex_unlock(&pvt->lock);
590 /* Release the lock and try again */
592 ast_mutex_lock(&pvt->lock);
595 /* Then break the poll */
596 pthread_kill(pri->master, SIGURG);
601 #define NUM_CADENCE_MAX 25
602 static int num_cadence = 4;
603 static int user_has_defined_cadences = 0;
605 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
606 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
607 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
608 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
609 { { 1000, 500, 2500, 5000 } }, /* Long ring */
612 int receivedRingT; /* Used to find out what ringtone we are on */
614 /* cidrings says in which pause to transmit the cid information, where the first pause
615 * is 1, the second pause is 2 and so on.
618 static int cidrings[NUM_CADENCE_MAX] = {
619 2, /* Right after first long ring */
620 4, /* Right after long part */
621 3, /* After third chirp */
622 2, /* Second spell */
625 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
626 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
628 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
629 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
631 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
634 if (p->subs[0].owner == ast)
636 else if (p->subs[1].owner == ast)
638 else if (p->subs[2].owner == ast)
643 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
649 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
651 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
654 struct ast_frame null = { AST_FRAME_NULL, };
657 ast_mutex_unlock(&pri->lock);
660 if (p->subs[a].owner) {
661 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
662 ast_mutex_unlock(&p->lock);
664 ast_mutex_lock(&p->lock);
666 ast_queue_frame(p->subs[a].owner, &null);
667 ast_mutex_unlock(&p->subs[a].owner->lock);
675 ast_mutex_lock(&pri->lock);
680 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
682 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
685 /* We must unlock the PRI to avoid the possibility of a deadlock */
688 ast_mutex_unlock(&pri->lock);
692 if (ast_mutex_trylock(&p->owner->lock)) {
693 ast_mutex_unlock(&p->lock);
695 ast_mutex_lock(&p->lock);
697 ast_queue_frame(p->owner, f);
698 ast_mutex_unlock(&p->owner->lock);
706 ast_mutex_lock(&pri->lock);
710 static void swap_subs(struct zt_pvt *p, int a, int b)
714 struct ast_channel *towner;
716 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
718 tchan = p->subs[a].chan;
719 towner = p->subs[a].owner;
720 tinthreeway = p->subs[a].inthreeway;
722 p->subs[a].chan = p->subs[b].chan;
723 p->subs[a].owner = p->subs[b].owner;
724 p->subs[a].inthreeway = p->subs[b].inthreeway;
726 p->subs[b].chan = tchan;
727 p->subs[b].owner = towner;
728 p->subs[b].inthreeway = tinthreeway;
730 if (p->subs[a].owner)
731 p->subs[a].owner->fds[0] = p->subs[a].zfd;
732 if (p->subs[b].owner)
733 p->subs[b].owner->fds[0] = p->subs[b].zfd;
734 wakeup_sub(p, a, NULL);
735 wakeup_sub(p, b, NULL);
738 static int zt_open(char *fn)
746 for (x=0;x<strlen(fn);x++) {
747 if (!isdigit(fn[x])) {
755 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
758 fn = "/dev/zap/channel";
760 fd = open(fn, O_RDWR | O_NONBLOCK);
762 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
766 if (ioctl(fd, ZT_SPECIFY, &chan)) {
770 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
775 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
779 static void zt_close(int fd)
784 int zt_setlinear(int zfd, int linear)
787 res = ioctl(zfd, ZT_SETLINEAR, &linear);
794 int zt_setlaw(int zfd, int law)
797 res = ioctl(zfd, ZT_SETLAW, &law);
803 static int alloc_sub(struct zt_pvt *p, int x)
807 if (p->subs[x].zfd < 0) {
808 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
809 if (p->subs[x].zfd > -1) {
810 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
812 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
813 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
814 bi.numbufs = numbufs;
815 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
817 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
820 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
821 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
822 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
823 zt_close(p->subs[x].zfd);
828 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
831 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
834 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
838 static int unalloc_sub(struct zt_pvt *p, int x)
841 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
844 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
845 if (p->subs[x].zfd > -1) {
846 zt_close(p->subs[x].zfd);
849 p->subs[x].linear = 0;
851 p->subs[x].owner = NULL;
852 p->subs[x].inthreeway = 0;
853 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
857 static int zt_digit(struct ast_channel *ast, char digit)
859 ZT_DIAL_OPERATION zo;
864 ast_mutex_lock(&p->lock);
865 index = zt_get_index(ast, p, 0);
866 if (index == SUB_REAL) {
868 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
870 if (!pri_grab(p, p->pri)) {
871 pri_information(p->pri->pri,p->call,digit);
874 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
875 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
876 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
877 res = strlen(p->dialdest);
878 p->dialdest[res++] = digit;
879 p->dialdest[res] = '\0';
885 zo.op = ZT_DIAL_OP_APPEND;
887 zo.dialstr[1] = digit;
889 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
890 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
895 ast_mutex_unlock(&p->lock);
899 static char *events[] = {
912 "Hook Transition Complete",
924 { ZT_ALARM_RED, "Red Alarm" },
925 { ZT_ALARM_YELLOW, "Yellow Alarm" },
926 { ZT_ALARM_BLUE, "Blue Alarm" },
927 { ZT_ALARM_RECOVER, "Recovering" },
928 { ZT_ALARM_LOOPBACK, "Loopback" },
929 { ZT_ALARM_NOTOPEN, "Not Open" },
930 { ZT_ALARM_NONE, "None" },
933 static char *alarm2str(int alarm)
936 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
937 if (alarms[x].alarm & alarm)
938 return alarms[x].name;
940 return alarm ? "Unknown Alarm" : "No Alarm";
943 static char *event2str(int event)
945 static char buf[256];
946 if ((event < 18) && (event > -1))
947 return events[event];
948 sprintf(buf, "Event %d", event); /* safe */
953 static int str2r2prot(char *swtype)
955 if (!strcasecmp(swtype, "ar"))
956 return MFCR2_PROT_ARGENTINA;
958 if (!strcasecmp(swtype, "cn"))
959 return MFCR2_PROT_CHINA;
961 if (!strcasecmp(swtype, "kr"))
962 return MFCR2_PROT_KOREA;
968 static char *sig2str(int sig)
970 static char buf[256];
973 return "E & M Immediate";
979 return "Feature Group D (DTMF)";
981 return "Feature Group D (MF)";
983 return "Feature Group B (MF)";
987 return "FXS Loopstart";
989 return "FXS Groundstart";
991 return "FXS Kewlstart";
993 return "FXO Loopstart";
995 return "FXO Groundstart";
997 return "FXO Kewlstart";
999 return "PRI Signalling";
1001 return "R2 Signalling";
1003 return "SF (Tone) Signalling Immediate";
1005 return "SF (Tone) Signalling Wink";
1007 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1008 case SIG_SF_FEATDMF:
1009 return "SF (Tone) Signalling with Feature Group D (MF)";
1011 return "SF (Tone) Signalling with Feature Group B (MF)";
1012 case SIG_GR303FXOKS:
1013 return "GR-303 Signalling with FXOKS";
1014 case SIG_GR303FXSKS:
1015 return "GR-303 Signalling with FXSKS";
1017 return "Pseudo Signalling";
1019 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1024 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1026 /* If the conference already exists, and we're already in it
1027 don't bother doing anything */
1030 memset(&zi, 0, sizeof(zi));
1033 if (slavechannel > 0) {
1034 /* If we have only one slave, do a digital mon */
1035 zi.confmode = ZT_CONF_DIGITALMON;
1036 zi.confno = slavechannel;
1039 /* Real-side and pseudo-side both participate in conference */
1040 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1041 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1043 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1044 zi.confno = p->confno;
1046 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1050 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1051 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1054 if (slavechannel < 1) {
1055 p->confno = zi.confno;
1057 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1058 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1062 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1064 /* If they're listening to our channel, they're ours */
1065 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1067 /* If they're a talker on our (allocated) conference, they're ours */
1068 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1073 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1076 if (/* Can't delete if there's no zfd */
1078 /* Don't delete from the conference if it's not our conference */
1080 /* Don't delete if we don't think it's conferenced at all (implied) */
1082 memset(&zi, 0, sizeof(zi));
1086 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1087 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1090 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1091 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1095 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1099 struct zt_pvt *slave = NULL;
1100 /* Start out optimistic */
1102 /* Update conference state in a stateless fashion */
1104 /* Any three-way calling makes slave native mode *definitely* out
1106 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1109 /* If we don't have any 3-way calls, check to see if we have
1110 precisely one slave */
1111 if (useslavenative) {
1112 for (x=0;x<MAX_SLAVES;x++) {
1115 /* Whoops already have a slave! No
1116 slave native and stop right away */
1121 /* We have one slave so far */
1122 slave = p->slaves[x];
1127 /* If no slave, slave native definitely out */
1130 else if (slave->law != p->law) {
1136 return useslavenative;
1139 static int reset_conf(struct zt_pvt *p)
1142 memset(&zi, 0, sizeof(zi));
1144 if (p->subs[SUB_REAL].zfd > -1) {
1145 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1146 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1151 static int update_conf(struct zt_pvt *p)
1156 struct zt_pvt *slave = NULL;
1158 useslavenative = isslavenative(p, &slave);
1159 /* Start with the obvious, general stuff */
1161 /* Look for three way calls */
1162 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1163 conf_add(p, &p->subs[x], x, 0);
1166 conf_del(p, &p->subs[x], x);
1169 /* If we have a slave, add him to our conference now. or DAX
1170 if this is slave native */
1171 for (x=0;x<MAX_SLAVES;x++) {
1174 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1176 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1181 /* If we're supposed to be in there, do so now */
1182 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1184 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1186 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1190 /* If we have a master, add ourselves to his conference */
1192 if (isslavenative(p->master, NULL)) {
1193 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1195 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1199 /* Nobody is left (or should be left) in our conference.
1203 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1207 static void zt_enable_ec(struct zt_pvt *p)
1212 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1215 if (p && p->echocancel) {
1216 if (p->sig == SIG_PRI) {
1218 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1220 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1223 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1225 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1228 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1231 ast_log(LOG_DEBUG, "No echocancellation requested\n");
1234 static void zt_train_ec(struct zt_pvt *p)
1238 if (p && p->echocancel && p->echotraining) {
1239 x = p->echotraining;
1240 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1242 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1244 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1247 ast_log(LOG_DEBUG, "No echo training requested\n");
1250 static void zt_disable_ec(struct zt_pvt *p)
1254 if (p->echocancel) {
1256 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1258 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1260 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1265 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1272 if ((rxgain != 0.0) || (txgain != 0.0)) {
1273 /* caluculate linear value of tx gain */
1274 ltxgain = pow(10.0,txgain / 20.0);
1275 /* caluculate linear value of rx gain */
1276 lrxgain = pow(10.0,rxgain / 20.0);
1277 if (law == ZT_LAW_ALAW) {
1278 for (j=0;j<256;j++) {
1279 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1280 if (k > 32767) k = 32767;
1281 if (k < -32767) k = -32767;
1282 g.rxgain[j] = AST_LIN2A(k);
1283 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1284 if (k > 32767) k = 32767;
1285 if (k < -32767) k = -32767;
1286 g.txgain[j] = AST_LIN2A(k);
1289 for (j=0;j<256;j++) {
1290 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1291 if (k > 32767) k = 32767;
1292 if (k < -32767) k = -32767;
1293 g.rxgain[j] = AST_LIN2MU(k);
1294 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1295 if (k > 32767) k = 32767;
1296 if (k < -32767) k = -32767;
1297 g.txgain[j] = AST_LIN2MU(k);
1301 for (j=0;j<256;j++) {
1308 return(ioctl(fd,ZT_SETGAINS,&g));
1311 static inline int zt_set_hook(int fd, int hs)
1315 res = ioctl(fd, ZT_HOOK, &x);
1318 if (errno == EINPROGRESS) return 0;
1319 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1324 static inline int zt_confmute(struct zt_pvt *p, int muted)
1328 if (p->sig == SIG_PRI) {
1330 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1332 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1334 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1336 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1340 static int save_conference(struct zt_pvt *p)
1342 struct zt_confinfo c;
1344 if (p->saveconf.confmode) {
1345 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1348 p->saveconf.chan = 0;
1349 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1351 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1352 p->saveconf.confmode = 0;
1357 c.confmode = ZT_CONF_NORMAL;
1358 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1360 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1364 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1368 static int restore_conference(struct zt_pvt *p)
1371 if (p->saveconf.confmode) {
1372 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1373 p->saveconf.confmode = 0;
1375 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1380 ast_log(LOG_DEBUG, "Restored conferencing\n");
1384 static int send_callerid(struct zt_pvt *p);
1386 int send_cwcidspill(struct zt_pvt *p)
1390 p->cidspill = malloc(MAX_CALLERID_SIZE);
1392 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1393 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1394 /* Make sure we account for the end */
1395 p->cidlen += READ_SIZE * 4;
1398 if (option_verbose > 2)
1399 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1404 static int has_voicemail(struct zt_pvt *p)
1407 return ast_app_has_voicemail(p->mailbox, NULL);
1410 static int send_callerid(struct zt_pvt *p)
1412 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1414 /* Take out of linear mode if necessary */
1415 if (p->subs[SUB_REAL].linear) {
1416 p->subs[SUB_REAL].linear = 0;
1417 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1419 while(p->cidpos < p->cidlen) {
1420 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1422 if (errno == EAGAIN)
1425 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1435 if (p->callwaitcas) {
1436 /* Wait for CID/CW to expire */
1437 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1439 restore_conference(p);
1443 static int zt_callwait(struct ast_channel *ast)
1445 struct zt_pvt *p = ast->pvt->pvt;
1446 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1448 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1451 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1455 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1456 if (!p->callwaitrings && p->callwaitingcallerid) {
1457 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1459 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1461 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1463 p->cidlen = 2400 + READ_SIZE * 4;
1468 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1474 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1476 struct zt_pvt *p = ast->pvt->pvt;
1482 char dest[256]; /* must be same length as p->dialdest */
1483 ast_mutex_lock(&p->lock);
1484 strncpy(dest, rdest, sizeof(dest) - 1);
1485 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1486 if ((ast->_state == AST_STATE_BUSY)) {
1487 p->subs[SUB_REAL].needbusy = 1;
1488 ast_mutex_unlock(&p->lock);
1491 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1492 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1493 ast_mutex_unlock(&p->lock);
1497 if (p->radio) /* if a radio channel, up immediately */
1499 /* Special pseudo -- automatically up */
1500 ast_setstate(ast, AST_STATE_UP);
1501 ast_mutex_unlock(&p->lock);
1504 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1505 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1507 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1510 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1516 if (p->owner == ast) {
1517 /* Normal ring, on hook */
1519 /* Don't send audio while on hook, until the call is answered */
1521 if (p->use_callerid) {
1522 /* Generate the Caller-ID spill if desired */
1524 ast_log(LOG_WARNING, "cidspill already exists??\n");
1527 p->cidspill = malloc(MAX_CALLERID_SIZE);
1530 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1534 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1536 /* Choose proper cadence */
1537 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1538 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1539 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1540 p->cidrings = cidrings[p->distinctivering - 1];
1542 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1543 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1544 p->cidrings = DEFAULT_CIDRINGS;
1548 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1549 c = strchr(dest, '/');
1552 if (c && (strlen(c) < p->stripmsd)) {
1553 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1557 p->dop.op = ZT_DIAL_OP_REPLACE;
1558 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1559 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1561 p->dop.dialstr[0] = '\0';
1564 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1565 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1566 ast_mutex_unlock(&p->lock);
1571 /* Call waiting call */
1572 p->callwaitrings = 0;
1573 if (ast->cid.cid_num)
1574 strncpy(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num)-1);
1576 p->callwait_num[0] = '\0';
1577 if (ast->cid.cid_name)
1578 strncpy(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name)-1);
1580 p->callwait_name[0] = '\0';
1581 /* Call waiting tone instead */
1582 if (zt_callwait(ast)) {
1583 ast_mutex_unlock(&p->lock);
1586 /* Make ring-back */
1587 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1588 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1591 n = ast->cid.cid_name;
1592 l = ast->cid.cid_num;
1594 strncpy(p->lastcid_num, l, sizeof(p->lastcid_num) - 1);
1596 p->lastcid_num[0] = '\0';
1598 strncpy(p->lastcid_name, n, sizeof(p->lastcid_name) - 1);
1600 p->lastcid_name[0] = '\0';
1601 ast_setstate(ast, AST_STATE_RINGING);
1602 index = zt_get_index(ast, p, 0);
1604 p->subs[index].needringing = 1;
1620 case SIG_SF_FEATDMF:
1622 c = strchr(dest, '/');
1627 if (strlen(c) < p->stripmsd) {
1628 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1629 ast_mutex_unlock(&p->lock);
1633 /* Start the trunk, if not GR-303 */
1637 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1639 if (errno != EINPROGRESS) {
1640 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1641 ast_mutex_unlock(&p->lock);
1648 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1649 p->dop.op = ZT_DIAL_OP_REPLACE;
1650 if (p->sig == SIG_FEATD) {
1651 l = ast->cid.cid_num;
1653 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1655 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1657 if (p->sig == SIG_FEATDMF) {
1658 l = ast->cid.cid_num;
1660 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1662 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1664 if (p->sig == SIG_E911) {
1665 strncpy(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr) - 1);
1667 if (p->sig == SIG_FEATB) {
1668 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1671 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c + p->stripmsd);
1673 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1674 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1675 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1676 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1677 p->echorest[sizeof(p->echorest) - 1] = '\0';
1679 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1683 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1685 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1686 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1687 ast_mutex_unlock(&p->lock);
1691 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1693 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1694 ast_setstate(ast, AST_STATE_DIALING);
1697 /* Special pseudo -- automatically up*/
1698 ast_setstate(ast, AST_STATE_UP);
1701 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1702 p->dialdest[0] = '\0';
1705 ast_log(LOG_DEBUG, "not yet implemented\n");
1706 ast_mutex_unlock(&p->lock);
1712 c = strchr(dest, '/');
1717 if (!p->hidecallerid) {
1718 l = ast->cid.cid_num;
1719 n = ast->cid.cid_name;
1724 if (strlen(c) < p->stripmsd) {
1725 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1726 ast_mutex_unlock(&p->lock);
1729 if (p->sig != SIG_FXSKS) {
1730 p->dop.op = ZT_DIAL_OP_REPLACE;
1731 s = strchr(c + p->stripmsd, 'w');
1734 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1736 p->dop.dialstr[0] = '\0';
1739 p->dop.dialstr[0] = '\0';
1742 if (pri_grab(p, p->pri)) {
1743 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1744 ast_mutex_unlock(&p->lock);
1747 if (!(p->call = pri_new_call(p->pri->pri))) {
1748 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1750 ast_mutex_unlock(&p->lock);
1753 if (!(sr = pri_sr_new())) {
1754 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1756 ast_mutex_unlock(&p->lock);
1758 if (p->bearer || (p->sig == SIG_FXSKS)) {
1760 ast_log(LOG_DEBUG, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
1761 p->bearer->call = p->call;
1763 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
1764 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1766 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1767 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1768 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1769 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1771 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1772 pri_sr_set_called(sr, c + p->stripmsd, p->pri->dialplan - 1, s ? 1 : 0);
1773 pri_sr_set_caller(sr, l, n, p->pri->localdialplan - 1,
1774 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
1775 PRES_NUMBER_NOT_AVAILABLE);
1776 if (pri_setup(p->pri->pri, p->call, sr)) {
1777 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1779 ast_mutex_unlock(&p->lock);
1784 ast_setstate(ast, AST_STATE_DIALING);
1788 ast_mutex_unlock(&p->lock);
1792 static void destroy_zt_pvt(struct zt_pvt **pvt)
1794 struct zt_pvt *p = *pvt;
1795 ast_mutex_destroy(&p->lock);
1800 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1810 for (i = 0; i < 3; i++) {
1811 if (cur->subs[i].owner) {
1817 prev->next = cur->next;
1819 prev->next->prev = prev;
1825 iflist->prev = NULL;
1829 if (cur->subs[SUB_REAL].zfd > -1) {
1830 zt_close(cur->subs[SUB_REAL].zfd);
1832 destroy_zt_pvt(&cur);
1836 prev->next = cur->next;
1838 prev->next->prev = prev;
1844 iflist->prev = NULL;
1848 if (cur->subs[SUB_REAL].zfd > -1) {
1849 zt_close(cur->subs[SUB_REAL].zfd);
1851 destroy_zt_pvt(&cur);
1857 int pri_is_up(struct zt_pri *pri)
1860 for (x=0;x<NUM_DCHANS;x++) {
1861 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1867 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
1869 bearer->owner = &inuse;
1870 bearer->master = crv;
1871 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
1872 if (crv->subs[SUB_REAL].owner)
1873 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
1874 crv->bearer = bearer;
1875 crv->call = bearer->call;
1880 static char *pri_order(int level)
1890 return "Quaternary";
1896 int pri_find_dchan(struct zt_pri *pri)
1903 for(x=0;x<NUM_DCHANS;x++) {
1904 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
1906 if (pri->dchans[x] == old) {
1912 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
1913 pri->dchannels[newslot]);
1915 if (old && (oldslot != newslot))
1916 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
1917 pri->dchannels[oldslot], pri->dchannels[newslot]);
1918 pri->pri = pri->dchans[newslot];
1923 static int zt_hangup(struct ast_channel *ast)
1927 static int restore_gains(struct zt_pvt *p);
1928 struct zt_pvt *p = ast->pvt->pvt;
1929 struct zt_pvt *tmp = NULL;
1930 struct zt_pvt *prev = NULL;
1934 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1935 if (!ast->pvt->pvt) {
1936 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1940 ast_mutex_lock(&p->lock);
1942 index = zt_get_index(ast, p, 1);
1944 if (p->sig == SIG_PRI) {
1946 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1952 if (p->origcid_num) {
1953 strncpy(p->cid_num, p->origcid_num, sizeof(p->cid_num) - 1);
1954 free(p->origcid_num);
1955 p->origcid_num = NULL;
1957 if (p->origcid_name) {
1958 strncpy(p->cid_name, p->origcid_name, sizeof(p->cid_name) - 1);
1959 free(p->origcid_name);
1960 p->origcid_name = NULL;
1963 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1967 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1968 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1972 /* Real channel, do some fixup */
1973 p->subs[index].owner = NULL;
1974 p->subs[index].needanswer = 0;
1975 p->subs[index].needringing = 0;
1976 p->subs[index].needbusy = 0;
1977 p->subs[index].needcongestion = 0;
1978 p->subs[index].linear = 0;
1979 p->subs[index].needcallerid = 0;
1980 zt_setlinear(p->subs[index].zfd, 0);
1981 if (index == SUB_REAL) {
1982 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1983 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1984 if (p->subs[SUB_CALLWAIT].inthreeway) {
1985 /* We had flipped over to answer a callwait and now it's gone */
1986 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1987 /* Move to the call-wait, but un-own us until they flip back. */
1988 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1989 unalloc_sub(p, SUB_CALLWAIT);
1992 /* The three way hung up, but we still have a call wait */
1993 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1994 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1995 unalloc_sub(p, SUB_THREEWAY);
1996 if (p->subs[SUB_REAL].inthreeway) {
1997 /* This was part of a three way call. Immediately make way for
1999 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2000 p->owner = p->subs[SUB_REAL].owner;
2002 /* This call hasn't been completed yet... Set owner to NULL */
2003 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2006 p->subs[SUB_REAL].inthreeway = 0;
2008 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2009 /* Move to the call-wait and switch back to them. */
2010 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2011 unalloc_sub(p, SUB_CALLWAIT);
2012 p->owner = p->subs[SUB_REAL].owner;
2013 if (p->owner->_state != AST_STATE_UP)
2014 p->subs[SUB_REAL].needanswer = 1;
2015 if (p->subs[SUB_REAL].owner->bridge)
2016 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
2017 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2018 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2019 unalloc_sub(p, SUB_THREEWAY);
2020 if (p->subs[SUB_REAL].inthreeway) {
2021 /* This was part of a three way call. Immediately make way for
2023 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2024 p->owner = p->subs[SUB_REAL].owner;
2026 /* This call hasn't been completed yet... Set owner to NULL */
2027 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2030 p->subs[SUB_REAL].inthreeway = 0;
2032 } else if (index == SUB_CALLWAIT) {
2033 /* Ditch the holding callwait call, and immediately make it availabe */
2034 if (p->subs[SUB_CALLWAIT].inthreeway) {
2035 /* This is actually part of a three way, placed on hold. Place the third part
2036 on music on hold now */
2037 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
2038 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
2039 p->subs[SUB_THREEWAY].inthreeway = 0;
2040 /* Make it the call wait now */
2041 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2042 unalloc_sub(p, SUB_THREEWAY);
2044 unalloc_sub(p, SUB_CALLWAIT);
2045 } else if (index == SUB_THREEWAY) {
2046 if (p->subs[SUB_CALLWAIT].inthreeway) {
2047 /* The other party of the three way call is currently in a call-wait state.
2048 Start music on hold for them, and take the main guy out of the third call */
2049 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
2050 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
2051 p->subs[SUB_CALLWAIT].inthreeway = 0;
2053 p->subs[SUB_REAL].inthreeway = 0;
2054 /* If this was part of a three way call index, let us make
2055 another three way call */
2056 unalloc_sub(p, SUB_THREEWAY);
2058 /* This wasn't any sort of call, but how are we an index? */
2059 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2064 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2067 p->distinctivering = 0;
2068 p->confirmanswer = 0;
2074 p->onhooktime = time(NULL);
2080 ast_dsp_free(p->dsp);
2084 law = ZT_LAW_DEFAULT;
2085 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2087 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2088 /* Perform low level hangup if no owner left */
2091 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2092 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2093 if (!pri_grab(p, p->pri)) {
2094 if (p->alreadyhungup) {
2095 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2096 pri_hangup(p->pri->pri, p->call, -1);
2099 p->bearer->call = NULL;
2101 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2102 int icause = ast->hangupcause ? ast->hangupcause : -1;
2103 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2104 p->alreadyhungup = 1;
2106 p->bearer->alreadyhungup = 1;
2109 icause = atoi(cause);
2111 pri_hangup(p->pri->pri, p->call, icause);
2114 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2117 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2122 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2129 if (p->sig == SIG_R2) {
2131 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2139 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2140 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2142 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2148 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2151 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2153 /* If they're off hook, try playing congestion */
2154 if ((par.rxisoffhook) && (!p->radio))
2155 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2157 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2163 /* Make sure we're not made available for at least two seconds assuming
2164 we were actually used for an inbound or outbound call. */
2165 if (ast->_state != AST_STATE_RESERVED) {
2166 time(&p->guardtime);
2171 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2178 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2179 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2183 p->callwaiting = p->permcallwaiting;
2184 p->hidecallerid = p->permhidecallerid;
2189 /* Restore data mode */
2190 if (p->sig == SIG_PRI) {
2192 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2196 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2197 /* Free up the bearer channel as well, and
2198 don't use its file descriptor anymore */
2199 update_conf(p->bearer);
2200 reset_conf(p->bearer);
2201 p->bearer->owner = NULL;
2203 p->subs[SUB_REAL].zfd = -1;
2211 p->callwaitingrepeat = 0;
2213 ast->pvt->pvt = NULL;
2214 ast_mutex_unlock(&p->lock);
2215 ast_mutex_lock(&usecnt_lock);
2218 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2219 ast_mutex_unlock(&usecnt_lock);
2220 ast_update_use_count();
2221 if (option_verbose > 2)
2222 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2224 ast_mutex_lock(&iflock);
2230 destroy_channel(prev, tmp, 0);
2238 ast_mutex_unlock(&iflock);
2242 static int zt_answer(struct ast_channel *ast)
2244 struct zt_pvt *p = ast->pvt->pvt;
2247 int oldstate = ast->_state;
2248 ast_setstate(ast, AST_STATE_UP);
2249 ast_mutex_lock(&p->lock);
2250 index = zt_get_index(ast, p, 0);
2253 /* nothing to do if a radio channel */
2255 ast_mutex_unlock(&p->lock);
2274 case SIG_SF_FEATDMF:
2279 /* Pick up the line */
2280 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2281 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2282 tone_zone_play_tone(p->subs[index].zfd, -1);
2284 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2285 if (oldstate == AST_STATE_RINGING) {
2286 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2287 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2288 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2289 p->owner = p->subs[SUB_REAL].owner;
2292 if (p->sig & __ZT_SIG_FXS) {
2299 /* Send a pri acknowledge */
2300 if (!pri_grab(p, p->pri)) {
2302 res = pri_answer(p->pri->pri, p->call, 0, 1);
2305 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2312 res = mfcr2_AnswerCall(p->r2, NULL);
2314 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2318 ast_mutex_unlock(&p->lock);
2321 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2324 ast_mutex_unlock(&p->lock);
2328 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2333 struct zt_pvt *p = chan->pvt->pvt;
2336 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2337 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2343 if ((!cp) || (datalen < 1))
2349 case AST_OPTION_TONE_VERIFY:
2354 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2355 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2358 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2359 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2362 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2363 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2367 case AST_OPTION_TDD: /* turn on or off TDD */
2368 if (!*cp) { /* turn it off */
2369 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2370 if (p->tdd) tdd_free(p->tdd);
2376 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2377 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2380 /* otherwise, turn it on */
2381 if (!p->didtdd) { /* if havent done it yet */
2382 unsigned char mybuf[41000],*buf;
2383 int size,res,fd,len;
2385 struct pollfd fds[1];
2387 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2388 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2390 index = zt_get_index(chan, p, 0);
2392 ast_log(LOG_WARNING, "No index in TDD?\n");
2395 fd = p->subs[index].zfd;
2397 if (ast_check_hangup(chan)) return -1;
2399 if (size > READ_SIZE)
2402 fds[0].events = POLLPRI | POLLOUT;
2403 res = poll(fds, 1, -1);
2405 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2408 /* if got exception */
2409 if (fds[0].revents & POLLPRI) return -1;
2410 if (!(fds[0].revents & POLLOUT)) {
2411 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2414 res = write(fd, buf, size);
2416 if (res == -1) return -1;
2417 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2423 p->didtdd = 1; /* set to have done it now */
2425 if (*cp == 2) { /* Mate mode */
2426 if (p->tdd) tdd_free(p->tdd);
2431 if (!p->tdd) { /* if we dont have one yet */
2432 p->tdd = tdd_new(); /* allocate one */
2435 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2438 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2443 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2446 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2448 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2451 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2457 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2460 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2461 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2468 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2470 /* Unlink a specific slave or all slaves/masters from a given master */
2476 ast_mutex_lock(&master->lock);
2478 while(ast_mutex_trylock(&slave->lock)) {
2479 ast_mutex_unlock(&master->lock);
2481 ast_mutex_lock(&master->lock);
2486 for (x=0;x<MAX_SLAVES;x++) {
2487 if (master->slaves[x]) {
2488 if (!slave || (master->slaves[x] == slave)) {
2489 /* Take slave out of the conference */
2490 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2491 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2492 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2493 master->slaves[x]->master = NULL;
2494 master->slaves[x] = NULL;
2499 master->inconference = 0;
2502 if (master->master) {
2503 /* Take master out of the conference */
2504 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2505 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2507 for (x=0;x<MAX_SLAVES;x++) {
2508 if (master->master->slaves[x] == master)
2509 master->master->slaves[x] = NULL;
2510 else if (master->master->slaves[x])
2514 master->master->inconference = 0;
2516 master->master = NULL;
2518 update_conf(master);
2521 ast_mutex_unlock(&slave->lock);
2522 ast_mutex_unlock(&master->lock);
2526 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2528 if (!slave || !master) {
2529 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2532 for (x=0;x<MAX_SLAVES;x++) {
2533 if (!master->slaves[x]) {
2534 master->slaves[x] = slave;
2538 if (x >= MAX_SLAVES) {
2539 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2540 master->slaves[MAX_SLAVES - 1] = slave;
2543 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2544 slave->master = master;
2546 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2549 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2551 struct ast_channel *who = NULL, *cs[3];
2552 struct zt_pvt *p0, *p1, *op0, *op1;
2553 struct zt_pvt *master=NULL, *slave=NULL;
2554 struct ast_frame *f;
2559 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2560 int os1 = -1, os2 = -1;
2561 struct ast_channel *oc1, *oc2;
2563 /* if need DTMF, cant native bridge */
2564 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2567 ast_mutex_lock(&c0->lock);
2568 ast_mutex_lock(&c1->lock);
2572 /* cant do pseudo-channels here */
2573 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2574 ast_mutex_unlock(&c0->lock);
2575 ast_mutex_unlock(&c1->lock);
2579 op0 = p0 = c0->pvt->pvt;
2580 op1 = p1 = c1->pvt->pvt;
2583 oi1 = zt_get_index(c0, p0, 0);
2584 oi2 = zt_get_index(c1, p1, 0);
2587 if ((oi1 < 0) || (oi2 < 0)) {
2588 ast_mutex_unlock(&c0->lock);
2589 ast_mutex_unlock(&c1->lock);
2595 ast_mutex_lock(&p0->lock);
2596 if (ast_mutex_trylock(&p1->lock)) {
2597 /* Don't block, due to potential for deadlock */
2598 ast_mutex_unlock(&p0->lock);
2599 ast_mutex_unlock(&c0->lock);
2600 ast_mutex_unlock(&c1->lock);
2601 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2604 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2605 if (!p0->owner || !p1->owner) {
2606 /* Currently unowned -- Do nothing. */
2609 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2610 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2614 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2619 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2620 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n", p0->channel, oi1, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p0->subs[SUB_REAL].inthreeway,
2621 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2624 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2625 if (p1->subs[SUB_THREEWAY].inthreeway) {
2631 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2632 if (p0->subs[SUB_THREEWAY].inthreeway) {
2638 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2639 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2640 don't put us in anything */
2641 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2647 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2648 /* Same as previous */
2649 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2656 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2657 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2658 if (master && slave) {
2659 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2660 in an active threeway call with a channel that is ringing, we should
2661 indicate ringing. */
2662 if ((oi2 == SUB_THREEWAY) &&
2663 p1->subs[SUB_THREEWAY].inthreeway &&
2664 p1->subs[SUB_REAL].owner &&
2665 p1->subs[SUB_REAL].inthreeway &&
2666 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2667 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2668 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2669 os2 = p1->subs[SUB_REAL].owner->_state;
2671 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2672 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2674 if ((oi1 == SUB_THREEWAY) &&
2675 p0->subs[SUB_THREEWAY].inthreeway &&
2676 p0->subs[SUB_REAL].owner &&
2677 p0->subs[SUB_REAL].inthreeway &&
2678 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2679 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2680 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2681 os1 = p0->subs[SUB_REAL].owner->_state;
2683 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2684 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2686 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2687 if (!p0->echocanbridged || !p1->echocanbridged) {
2688 /* Disable echo cancellation if appropriate */
2693 zt_link(slave, master);
2694 master->inconference = inconf;
2695 } else if (!nothingok)
2696 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2700 t1 = p0->subs[SUB_REAL].inthreeway;
2701 t2 = p1->subs[SUB_REAL].inthreeway;
2703 ast_mutex_unlock(&p0->lock);
2704 ast_mutex_unlock(&p1->lock);
2706 ast_mutex_unlock(&c0->lock);
2707 ast_mutex_unlock(&c1->lock);
2709 /* Native bridge failed */
2710 if ((!master || !slave) && !nothingok) {
2722 /* Here's our main loop... Start by locking things, looking for private parts,
2723 and then balking if anything is wrong */
2724 ast_mutex_lock(&c0->lock);
2725 ast_mutex_lock(&c1->lock);
2729 i1 = zt_get_index(c0, p0, 1);
2731 i2 = zt_get_index(c1, p1, 1);
2732 ast_mutex_unlock(&c0->lock);
2733 ast_mutex_unlock(&c1->lock);
2734 if ((op0 != p0) || (op1 != p1) ||
2735 (ofd1 != c0->fds[0]) ||
2736 (ofd2 != c1->fds[0]) ||
2737 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2738 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2739 (oc1 != p0->owner) ||
2740 (oc2 != p1->owner) ||
2741 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2742 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2745 if (slave && master)
2746 zt_unlink(slave, master, 1);
2747 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2748 op0->channel, oi1, op1->channel, oi2);
2756 who = ast_waitfor_n(cs, 2, &to);
2758 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2761 if (who->pvt->pvt == op0)
2762 op0->ignoredtmf = 1;
2763 else if (who->pvt->pvt == op1)
2764 op1->ignoredtmf = 1;
2766 if (who->pvt->pvt == op0)
2767 op0->ignoredtmf = 0;
2768 else if (who->pvt->pvt == op1)
2769 op1->ignoredtmf = 0;
2773 if (slave && master)
2774 zt_unlink(slave, master, 1);
2781 if (f->frametype == AST_FRAME_DTMF) {
2782 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2783 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2786 if (slave && master)
2787 zt_unlink(slave, master, 1);
2789 } else if ((who == c0) && p0->pulsedial) {
2791 } else if ((who == c1) && p1->pulsedial) {
2797 /* Swap who gets priority */
2804 static int zt_indicate(struct ast_channel *chan, int condition);
2806 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2808 struct zt_pvt *p = newchan->pvt->pvt;
2810 ast_mutex_lock(&p->lock);
2811 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2812 if (p->owner == oldchan) {
2816 if (p->subs[x].owner == oldchan) {
2818 zt_unlink(NULL, p, 0);
2819 p->subs[x].owner = newchan;
2821 if (newchan->_state == AST_STATE_RINGING)
2822 zt_indicate(newchan, AST_CONTROL_RINGING);
2824 ast_mutex_unlock(&p->lock);
2828 static int zt_ring_phone(struct zt_pvt *p)
2832 /* Make sure our transmit state is on hook */
2835 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2838 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2840 printf("Res: %d, error: %s\n", res, strerror(errno));
2846 /* Wait just in case */
2853 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2861 static void *ss_thread(void *data);
2863 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2865 static int attempt_transfer(struct zt_pvt *p)
2867 /* In order to transfer, we need at least one of the channels to
2868 actually be in a call bridge. We can't conference two applications
2869 together (but then, why would we want to?) */
2870 if (p->subs[SUB_REAL].owner->bridge) {
2871 /* The three-way person we're about to transfer to could still be in MOH, so
2872 stop if now if appropriate */
2873 if (p->subs[SUB_THREEWAY].owner->bridge)
2874 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2875 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2876 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2878 if (p->subs[SUB_REAL].owner->cdr) {
2879 /* Move CDR from second channel to current one */
2880 p->subs[SUB_THREEWAY].owner->cdr =
2881 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
2882 p->subs[SUB_REAL].owner->cdr = NULL;
2884 if (p->subs[SUB_REAL].owner->bridge->cdr) {
2885 /* Move CDR from second channel's bridge to current one */
2886 p->subs[SUB_THREEWAY].owner->cdr =
2887 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->bridge->cdr);
2888 p->subs[SUB_REAL].owner->bridge->cdr = NULL;
2890 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2891 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2892 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2895 /* Orphan the channel after releasing the lock */
2896 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
2897 unalloc_sub(p, SUB_THREEWAY);
2898 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2899 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2900 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2902 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2903 if (p->subs[SUB_THREEWAY].owner->cdr) {
2904 /* Move CDR from second channel to current one */
2905 p->subs[SUB_REAL].owner->cdr =
2906 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
2907 p->subs[SUB_THREEWAY].owner->cdr = NULL;
2909 if (p->subs[SUB_THREEWAY].owner->bridge->cdr) {
2910 /* Move CDR from second channel's bridge to current one */
2911 p->subs[SUB_REAL].owner->cdr =
2912 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->bridge->cdr);
2913 p->subs[SUB_THREEWAY].owner->bridge->cdr = NULL;
2915 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2916 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2917 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2920 /* Three-way is now the REAL */
2921 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2922 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
2923 unalloc_sub(p, SUB_THREEWAY);
2924 /* Tell the caller not to hangup */
2927 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2928 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2929 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2935 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2937 struct ast_frame *f;
2938 f = &p->subs[index].f;
2940 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2944 case MFCR2_EVENT_BLOCKED:
2945 if (option_verbose > 2)
2946 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2948 case MFCR2_EVENT_UNBLOCKED:
2949 if (option_verbose > 2)
2950 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2952 case MFCR2_EVENT_CONFIG_ERR:
2953 if (option_verbose > 2)
2954 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2956 case MFCR2_EVENT_RING:
2957 if (option_verbose > 2)
2958 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2960 case MFCR2_EVENT_HANGUP:
2961 if (option_verbose > 2)
2962 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2964 case MFCR2_EVENT_RINGING:
2965 if (option_verbose > 2)
2966 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2968 case MFCR2_EVENT_ANSWER:
2969 if (option_verbose > 2)
2970 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2972 case MFCR2_EVENT_HANGUP_ACK:
2973 if (option_verbose > 2)
2974 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2976 case MFCR2_EVENT_IDLE:
2977 if (option_verbose > 2)
2978 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2981 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2987 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2992 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);