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 *typecompat = "Tor"; /* Retain compatibility with chan_tor */
118 static char *config = "zapata.conf";
120 #define SIG_EM ZT_SIG_EM
121 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
122 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
123 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
124 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
125 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
126 #define SIG_FXSLS ZT_SIG_FXSLS
127 #define SIG_FXSGS ZT_SIG_FXSGS
128 #define SIG_FXSKS ZT_SIG_FXSKS
129 #define SIG_FXOLS ZT_SIG_FXOLS
130 #define SIG_FXOGS ZT_SIG_FXOGS
131 #define SIG_FXOKS ZT_SIG_FXOKS
132 #define SIG_PRI ZT_SIG_CLEAR
133 #define SIG_R2 ZT_SIG_CAS
134 #define SIG_SF ZT_SIG_SF
135 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
136 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
137 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
138 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
139 #define SIG_EM_E1 ZT_SIG_EM_E1
140 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
141 #define SIG_GR303FXSKS (0x200000 | ZT_SIG_FXSKS)
144 #define NUM_DCHANS 4 /* No more than 4 d-channels */
145 #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
146 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
148 #define CHAN_PSEUDO -2
150 #define DCHAN_PROVISIONED (1 << 0)
151 #define DCHAN_NOTINALARM (1 << 1)
152 #define DCHAN_UP (1 << 2)
154 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
156 static char context[AST_MAX_EXTENSION] = "default";
157 static char cid_num[256] = "";
158 static char cid_name[256] = "";
160 static char language[MAX_LANGUAGE] = "";
161 static char musicclass[MAX_LANGUAGE] = "";
162 static char progzone[10]= "";
164 static int usedistinctiveringdetection = 0;
166 static int use_callerid = 1;
167 static int cid_signalling = CID_SIG_BELL;
168 static int cid_start = CID_START_RING;
169 static int zaptrcallerid = 0;
170 static int cur_signalling = -1;
172 static unsigned int cur_group = 0;
173 static unsigned int cur_callergroup = 0;
174 static unsigned int cur_pickupgroup = 0;
175 static int relaxdtmf = 0;
177 static int immediate = 0;
179 static int stripmsd = 0;
181 static int callwaiting = 0;
183 static int callwaitingcallerid = 0;
185 static int hidecallerid = 0;
187 static int restrictcid = 0;
189 static int use_callingpres = 0;
191 static int callreturn = 0;
193 static int threewaycalling = 0;
195 static int transfer = 0;
197 static int cancallforward = 0;
199 static float rxgain = 0.0;
201 static float txgain = 0.0;
203 static int tonezone = -1;
205 static int echocancel;
207 static int echotraining;
211 static int echocanbridged = 0;
213 static int busydetect = 0;
215 static int busycount = 3;
217 static int callprogress = 0;
219 static char accountcode[20] = "";
221 static char mailbox[AST_MAX_EXTENSION];
223 static int amaflags = 0;
227 static int numbufs = 4;
229 static int cur_prewink = -1;
230 static int cur_preflash = -1;
231 static int cur_wink = -1;
232 static int cur_flash = -1;
233 static int cur_start = -1;
234 static int cur_rxwink = -1;
235 static int cur_rxflash = -1;
236 static int cur_debounce = -1;
238 static int priindication_oob = 0;
241 static int minunused = 2;
242 static int minidle = 0;
243 static char idleext[AST_MAX_EXTENSION];
244 static char idledial[AST_MAX_EXTENSION];
245 static int overlapdial = 0;
246 static struct ast_channel inuse = { "GR-303InUse" };
249 /* Wait up to 16 seconds for first digit (FXO logic) */
250 static int firstdigittimeout = 16000;
252 /* How long to wait for following digits (FXO logic) */
253 static int gendigittimeout = 8000;
255 /* How long to wait for an extra digit, if there is an ambiguous match */
256 static int matchdigittimeout = 3000;
258 static int usecnt =0;
259 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
261 /* Protect the interface list (of zt_pvt's) */
262 AST_MUTEX_DEFINE_STATIC(iflock);
264 static int ifcount = 0;
266 /* Protect the monitoring thread, so only one process can kill or start it, and not
267 when it's doing something critical. */
268 AST_MUTEX_DEFINE_STATIC(monlock);
270 /* This is the thread for the monitor which checks for input on the channels
271 which are not currently in use. */
272 static pthread_t monitor_thread = AST_PTHREADT_NULL;
274 static int restart_monitor(void);
276 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
278 static int zt_sendtext(struct ast_channel *c, char *text);
280 static inline int zt_get_event(int fd)
282 /* Avoid the silly zt_getevent which ignores a bunch of events */
284 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
288 static inline int zt_wait_event(int fd)
290 /* Avoid the silly zt_waitevent which ignores a bunch of events */
292 i = ZT_IOMUX_SIGEVENT;
293 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
294 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
298 /* Chunk size to read -- we use 20ms chunks to make things happy. */
299 #define READ_SIZE 160
301 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
302 #define MASK_INUSE (1 << 1) /* Channel currently in use */
304 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
305 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
306 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
307 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
308 #define RINGT ( (8000 * 8) / READ_SIZE)
314 static int r2prot = -1;
320 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8))
321 #define PRI_CHANNEL(p) ((p) & 0xff)
322 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
325 pthread_t master; /* Thread of master */
326 ast_mutex_t lock; /* Mutex */
327 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
328 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
329 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
330 int minunused; /* Min # of channels to keep empty */
331 int minidle; /* Min # of "idling" calls to keep active */
332 int nodetype; /* Node type */
333 int switchtype; /* Type of switch to emulate */
334 int nsf; /* Network-Specific Facilities */
335 int dialplan; /* Dialing plan */
336 int localdialplan; /* Local dialing plan */
337 int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
338 int trunkgroup; /* What our trunkgroup is */
339 int mastertrunkgroup; /* What trunk group is our master */
340 int prilogicalspan; /* Logical span number within trunk group */
341 int numchans; /* Num of channels we represent */
342 int overlapdial; /* In overlap dialing mode */
343 struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
344 int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
345 struct pri *pri; /* Currently active D-channel */
347 int fds[NUM_DCHANS]; /* FD's for d-channels */
353 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
354 struct zt_pvt *crvs; /* Member CRV structs */
355 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
359 static struct zt_pri pris[NUM_SPANS];
361 static int pritype = PRI_CPE;
364 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
366 #define DEFAULT_PRI_DEBUG 0
369 static inline void pri_rel(struct zt_pri *pri)
371 ast_mutex_unlock(&pri->lock);
374 static int switchtype = PRI_SWITCH_NI2;
375 static int nsf = PRI_NSF_NONE;
376 static int dialplan = PRI_NATIONAL_ISDN + 1;
377 static int localdialplan = PRI_NATIONAL_ISDN + 1;
380 /* Shut up the compiler */
384 #define SUB_REAL 0 /* Active call */
385 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
386 #define SUB_THREEWAY 2 /* Three-way call */
389 static struct zt_distRings drings;
391 struct distRingData {
394 struct ringContextData {
395 char contextData[AST_MAX_EXTENSION];
397 struct zt_distRings {
398 struct distRingData ringnum[3];
399 struct ringContextData ringContext[3];
402 static char *subnames[] = {
408 struct zt_subchannel {
410 struct ast_channel *owner;
412 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
413 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
424 #define CONF_USER_REAL (1 << 0)
425 #define CONF_USER_THIRDCALL (1 << 1)
429 static struct zt_pvt {
431 struct ast_channel *owner; /* Our current active owner (if applicable) */
432 /* Up to three channels can be associated with this call */
434 struct zt_subchannel sub_unused; /* Just a safety precaution */
435 struct zt_subchannel subs[3]; /* Sub-channels */
436 struct zt_confinfo saveconf; /* Saved conference info */
438 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
439 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
440 int inconference; /* If our real should be in the conference */
442 int sig; /* Signalling style */
443 int radio; /* radio type */
444 int firstradio; /* first radio flag */
447 int tonezone; /* tone zone for this chan, or -1 for default */
448 struct zt_pvt *next; /* Next channel in list */
449 struct zt_pvt *prev; /* Prev channel in list */
451 struct zt_distRings drings;
452 int usedistinctiveringdetection;
454 char context[AST_MAX_EXTENSION];
455 char defcontext[AST_MAX_EXTENSION];
456 char exten[AST_MAX_EXTENSION];
457 char language[MAX_LANGUAGE];
458 char musicclass[MAX_LANGUAGE];
459 char cid_num[AST_MAX_EXTENSION];
460 char cid_name[AST_MAX_EXTENSION];
461 char lastcid_num[AST_MAX_EXTENSION];
462 char lastcid_name[AST_MAX_EXTENSION];
463 char *origcid_num; /* malloced original callerid */
464 char *origcid_name; /* malloced original callerid */
465 char callwait_num[AST_MAX_EXTENSION];
466 char callwait_name[AST_MAX_EXTENSION];
467 char rdnis[AST_MAX_EXTENSION];
468 char dnid[AST_MAX_EXTENSION];
471 int confno; /* Our conference */
472 int confusers; /* Who is using our conference */
473 int propconfno; /* Propagated conference number */
474 unsigned int callgroup;
475 unsigned int pickupgroup;
476 int immediate; /* Answer before getting digits? */
477 int channel; /* Channel Number or CRV */
478 int span; /* Span number */
480 time_t guardtime; /* Must wait this much time before using for new call */
482 int use_callerid; /* Whether or not to use caller id on this channel */
483 int cid_signalling; /* CID signalling type bell202 or v23 */
484 int cid_start; /* CID start indicator, polarity or ring */
487 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
488 int restrictcid; /* Whether restrict the callerid -> only send ANI */
489 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
490 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
491 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
492 int cidcwexpire; /* When to expire our muting for CID/CW */
493 unsigned char *cidspill;
509 int callwaitingcallerid;
518 int priindication_oob;
519 struct timeval flashtime; /* Last flash-hook time */
521 int cref; /* Call reference number */
522 ZT_DIAL_OPERATION dop;
526 char accountcode[20]; /* Account code */
527 int amaflags; /* AMA Flags */
528 char didtdd; /* flag to say its done it once */
529 struct tdd_state *tdd; /* TDD flag */
532 char call_forward[AST_MAX_EXTENSION];
533 char mailbox[AST_MAX_EXTENSION];
538 int confirmanswer; /* Wait for '#' to confirm answer */
539 int distinctivering; /* Which distinctivering to use */
540 int cidrings; /* Which ring to deliver CID on */
542 int faxhandled; /* Has a fax tone already been handled? */
544 char mate; /* flag to say its in MATE mode */
545 int pulsedial; /* whether a pulse dial phone is detected */
546 int dtmfrelax; /* whether to run in relaxed DTMF mode */
548 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
551 struct zt_pvt *bearer;
552 struct zt_pvt *realcall;
560 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
569 } *iflist = NULL, *ifend = NULL;
572 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
574 #define GET_CHANNEL(p) ((p)->channel)
577 struct zt_pvt *round_robin[32];
580 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
583 /* Grab the lock first */
585 res = ast_mutex_trylock(&pri->lock);
587 ast_mutex_unlock(&pvt->lock);
588 /* Release the lock and try again */
590 ast_mutex_lock(&pvt->lock);
593 /* Then break the poll */
594 pthread_kill(pri->master, SIGURG);
599 #define NUM_CADENCE_MAX 25
600 static int num_cadence = 4;
601 static int user_has_defined_cadences = 0;
603 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
604 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
605 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
606 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
607 { { 1000, 500, 2500, 5000 } }, /* Long ring */
610 int receivedRingT; /* Used to find out what ringtone we are on */
612 /* cidrings says in which pause to transmit the cid information, where the first pause
613 * is 1, the second pause is 2 and so on.
616 static int cidrings[NUM_CADENCE_MAX] = {
617 2, /* Right after first long ring */
618 4, /* Right after long part */
619 3, /* After third chirp */
620 2, /* Second spell */
623 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
624 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
626 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
627 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
629 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
632 if (p->subs[0].owner == ast)
634 else if (p->subs[1].owner == ast)
636 else if (p->subs[2].owner == ast)
641 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
647 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
649 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
652 struct ast_frame null = { AST_FRAME_NULL, };
655 ast_mutex_unlock(&pri->lock);
658 if (p->subs[a].owner) {
659 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
660 ast_mutex_unlock(&p->lock);
662 ast_mutex_lock(&p->lock);
664 ast_queue_frame(p->subs[a].owner, &null);
665 ast_mutex_unlock(&p->subs[a].owner->lock);
673 ast_mutex_lock(&pri->lock);
678 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
680 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
683 /* We must unlock the PRI to avoid the possibility of a deadlock */
686 ast_mutex_unlock(&pri->lock);
690 if (ast_mutex_trylock(&p->owner->lock)) {
691 ast_mutex_unlock(&p->lock);
693 ast_mutex_lock(&p->lock);
695 ast_queue_frame(p->owner, f);
696 ast_mutex_unlock(&p->owner->lock);
704 ast_mutex_lock(&pri->lock);
708 static void swap_subs(struct zt_pvt *p, int a, int b)
712 struct ast_channel *towner;
714 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
716 tchan = p->subs[a].chan;
717 towner = p->subs[a].owner;
718 tinthreeway = p->subs[a].inthreeway;
720 p->subs[a].chan = p->subs[b].chan;
721 p->subs[a].owner = p->subs[b].owner;
722 p->subs[a].inthreeway = p->subs[b].inthreeway;
724 p->subs[b].chan = tchan;
725 p->subs[b].owner = towner;
726 p->subs[b].inthreeway = tinthreeway;
728 if (p->subs[a].owner)
729 p->subs[a].owner->fds[0] = p->subs[a].zfd;
730 if (p->subs[b].owner)
731 p->subs[b].owner->fds[0] = p->subs[b].zfd;
732 wakeup_sub(p, a, NULL);
733 wakeup_sub(p, b, NULL);
736 static int zt_open(char *fn)
744 for (x=0;x<strlen(fn);x++) {
745 if (!isdigit(fn[x])) {
753 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
756 fn = "/dev/zap/channel";
758 fd = open(fn, O_RDWR | O_NONBLOCK);
760 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
764 if (ioctl(fd, ZT_SPECIFY, &chan)) {
768 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
773 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
777 static void zt_close(int fd)
782 int zt_setlinear(int zfd, int linear)
785 res = ioctl(zfd, ZT_SETLINEAR, &linear);
792 int zt_setlaw(int zfd, int law)
795 res = ioctl(zfd, ZT_SETLAW, &law);
801 static int alloc_sub(struct zt_pvt *p, int x)
805 if (p->subs[x].zfd < 0) {
806 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
807 if (p->subs[x].zfd > -1) {
808 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
810 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
811 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
812 bi.numbufs = numbufs;
813 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
815 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
818 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
819 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
820 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
821 zt_close(p->subs[x].zfd);
826 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
829 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
832 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
836 static int unalloc_sub(struct zt_pvt *p, int x)
839 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
842 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
843 if (p->subs[x].zfd > -1) {
844 zt_close(p->subs[x].zfd);
847 p->subs[x].linear = 0;
849 p->subs[x].owner = NULL;
850 p->subs[x].inthreeway = 0;
851 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
855 static int zt_digit(struct ast_channel *ast, char digit)
857 ZT_DIAL_OPERATION zo;
862 ast_mutex_lock(&p->lock);
863 index = zt_get_index(ast, p, 0);
864 if (index == SUB_REAL) {
866 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
868 if (!pri_grab(p, p->pri)) {
869 pri_information(p->pri->pri,p->call,digit);
872 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
873 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
874 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
875 res = strlen(p->dialdest);
876 p->dialdest[res++] = digit;
877 p->dialdest[res] = '\0';
883 zo.op = ZT_DIAL_OP_APPEND;
885 zo.dialstr[1] = digit;
887 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
888 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
893 ast_mutex_unlock(&p->lock);
897 static char *events[] = {
910 "Hook Transition Complete",
922 { ZT_ALARM_RED, "Red Alarm" },
923 { ZT_ALARM_YELLOW, "Yellow Alarm" },
924 { ZT_ALARM_BLUE, "Blue Alarm" },
925 { ZT_ALARM_RECOVER, "Recovering" },
926 { ZT_ALARM_LOOPBACK, "Loopback" },
927 { ZT_ALARM_NOTOPEN, "Not Open" },
928 { ZT_ALARM_NONE, "None" },
931 static char *alarm2str(int alarm)
934 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
935 if (alarms[x].alarm & alarm)
936 return alarms[x].name;
938 return alarm ? "Unknown Alarm" : "No Alarm";
941 static char *event2str(int event)
943 static char buf[256];
944 if ((event < 18) && (event > -1))
945 return events[event];
946 sprintf(buf, "Event %d", event); /* safe */
951 static int str2r2prot(char *swtype)
953 if (!strcasecmp(swtype, "ar"))
954 return MFCR2_PROT_ARGENTINA;
956 if (!strcasecmp(swtype, "cn"))
957 return MFCR2_PROT_CHINA;
959 if (!strcasecmp(swtype, "kr"))
960 return MFCR2_PROT_KOREA;
966 static char *sig2str(int sig)
968 static char buf[256];
971 return "E & M Immediate";
977 return "Feature Group D (DTMF)";
979 return "Feature Group D (MF)";
981 return "Feature Group B (MF)";
985 return "FXS Loopstart";
987 return "FXS Groundstart";
989 return "FXS Kewlstart";
991 return "FXO Loopstart";
993 return "FXO Groundstart";
995 return "FXO Kewlstart";
997 return "PRI Signalling";
999 return "R2 Signalling";
1001 return "SF (Tone) Signalling Immediate";
1003 return "SF (Tone) Signalling Wink";
1005 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1006 case SIG_SF_FEATDMF:
1007 return "SF (Tone) Signalling with Feature Group D (MF)";
1009 return "SF (Tone) Signalling with Feature Group B (MF)";
1010 case SIG_GR303FXOKS:
1011 return "GR-303 Signalling with FXOKS";
1012 case SIG_GR303FXSKS:
1013 return "GR-303 Signalling with FXSKS";
1015 return "Pseudo Signalling";
1017 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1022 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1024 /* If the conference already exists, and we're already in it
1025 don't bother doing anything */
1028 memset(&zi, 0, sizeof(zi));
1031 if (slavechannel > 0) {
1032 /* If we have only one slave, do a digital mon */
1033 zi.confmode = ZT_CONF_DIGITALMON;
1034 zi.confno = slavechannel;
1037 /* Real-side and pseudo-side both participate in conference */
1038 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1039 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1041 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1042 zi.confno = p->confno;
1044 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1048 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1049 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1052 if (slavechannel < 1) {
1053 p->confno = zi.confno;
1055 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1056 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1060 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1062 /* If they're listening to our channel, they're ours */
1063 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1065 /* If they're a talker on our (allocated) conference, they're ours */
1066 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1071 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1074 if (/* Can't delete if there's no zfd */
1076 /* Don't delete from the conference if it's not our conference */
1078 /* Don't delete if we don't think it's conferenced at all (implied) */
1080 memset(&zi, 0, sizeof(zi));
1084 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1085 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1088 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1089 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1093 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1097 struct zt_pvt *slave = NULL;
1098 /* Start out optimistic */
1100 /* Update conference state in a stateless fashion */
1102 /* Any three-way calling makes slave native mode *definitely* out
1104 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1107 /* If we don't have any 3-way calls, check to see if we have
1108 precisely one slave */
1109 if (useslavenative) {
1110 for (x=0;x<MAX_SLAVES;x++) {
1113 /* Whoops already have a slave! No
1114 slave native and stop right away */
1119 /* We have one slave so far */
1120 slave = p->slaves[x];
1125 /* If no slave, slave native definitely out */
1128 else if (slave->law != p->law) {
1134 return useslavenative;
1137 static int reset_conf(struct zt_pvt *p)
1140 memset(&zi, 0, sizeof(zi));
1142 if (p->subs[SUB_REAL].zfd > -1) {
1143 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1144 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1149 static int update_conf(struct zt_pvt *p)
1154 struct zt_pvt *slave = NULL;
1156 useslavenative = isslavenative(p, &slave);
1157 /* Start with the obvious, general stuff */
1159 /* Look for three way calls */
1160 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1161 conf_add(p, &p->subs[x], x, 0);
1164 conf_del(p, &p->subs[x], x);
1167 /* If we have a slave, add him to our conference now. or DAX
1168 if this is slave native */
1169 for (x=0;x<MAX_SLAVES;x++) {
1172 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1174 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1179 /* If we're supposed to be in there, do so now */
1180 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1182 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1184 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1188 /* If we have a master, add ourselves to his conference */
1190 if (isslavenative(p->master, NULL)) {
1191 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1193 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1197 /* Nobody is left (or should be left) in our conference.
1201 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1205 static void zt_enable_ec(struct zt_pvt *p)
1210 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1213 if (p && p->echocancel) {
1214 if (p->sig == SIG_PRI) {
1216 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1218 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1221 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1223 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1226 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1229 ast_log(LOG_DEBUG, "No echocancellation requested\n");
1232 static void zt_train_ec(struct zt_pvt *p)
1236 if (p && p->echocancel && p->echotraining) {
1237 x = p->echotraining;
1238 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1240 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1242 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1245 ast_log(LOG_DEBUG, "No echo training requested\n");
1248 static void zt_disable_ec(struct zt_pvt *p)
1252 if (p->echocancel) {
1254 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1256 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1258 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1263 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1270 if ((rxgain != 0.0) || (txgain != 0.0)) {
1271 /* caluculate linear value of tx gain */
1272 ltxgain = pow(10.0,txgain / 20.0);
1273 /* caluculate linear value of rx gain */
1274 lrxgain = pow(10.0,rxgain / 20.0);
1275 if (law == ZT_LAW_ALAW) {
1276 for (j=0;j<256;j++) {
1277 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1278 if (k > 32767) k = 32767;
1279 if (k < -32767) k = -32767;
1280 g.rxgain[j] = AST_LIN2A(k);
1281 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1282 if (k > 32767) k = 32767;
1283 if (k < -32767) k = -32767;
1284 g.txgain[j] = AST_LIN2A(k);
1287 for (j=0;j<256;j++) {
1288 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1289 if (k > 32767) k = 32767;
1290 if (k < -32767) k = -32767;
1291 g.rxgain[j] = AST_LIN2MU(k);
1292 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1293 if (k > 32767) k = 32767;
1294 if (k < -32767) k = -32767;
1295 g.txgain[j] = AST_LIN2MU(k);
1299 for (j=0;j<256;j++) {
1306 return(ioctl(fd,ZT_SETGAINS,&g));
1309 static inline int zt_set_hook(int fd, int hs)
1313 res = ioctl(fd, ZT_HOOK, &x);
1316 if (errno == EINPROGRESS) return 0;
1317 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1322 static inline int zt_confmute(struct zt_pvt *p, int muted)
1326 if (p->sig == SIG_PRI) {
1328 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1330 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1332 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1334 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1338 static int save_conference(struct zt_pvt *p)
1340 struct zt_confinfo c;
1342 if (p->saveconf.confmode) {
1343 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1346 p->saveconf.chan = 0;
1347 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1349 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1350 p->saveconf.confmode = 0;
1355 c.confmode = ZT_CONF_NORMAL;
1356 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1358 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1362 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1366 static int restore_conference(struct zt_pvt *p)
1369 if (p->saveconf.confmode) {
1370 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1371 p->saveconf.confmode = 0;
1373 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1378 ast_log(LOG_DEBUG, "Restored conferencing\n");
1382 static int send_callerid(struct zt_pvt *p);
1384 int send_cwcidspill(struct zt_pvt *p)
1388 p->cidspill = malloc(MAX_CALLERID_SIZE);
1390 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1391 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1392 /* Make sure we account for the end */
1393 p->cidlen += READ_SIZE * 4;
1396 if (option_verbose > 2)
1397 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1402 static int has_voicemail(struct zt_pvt *p)
1405 return ast_app_has_voicemail(p->mailbox);
1408 static int send_callerid(struct zt_pvt *p)
1410 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1412 /* Take out of linear mode if necessary */
1413 if (p->subs[SUB_REAL].linear) {
1414 p->subs[SUB_REAL].linear = 0;
1415 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1417 while(p->cidpos < p->cidlen) {
1418 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1420 if (errno == EAGAIN)
1423 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1433 if (p->callwaitcas) {
1434 /* Wait for CID/CW to expire */
1435 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1437 restore_conference(p);
1441 static int zt_callwait(struct ast_channel *ast)
1443 struct zt_pvt *p = ast->pvt->pvt;
1444 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1446 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1449 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1453 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1454 if (!p->callwaitrings && p->callwaitingcallerid) {
1455 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1457 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1459 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1461 p->cidlen = 2400 + READ_SIZE * 4;
1466 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1472 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1474 struct zt_pvt *p = ast->pvt->pvt;
1480 char dest[256]; /* must be same length as p->dialdest */
1481 ast_mutex_lock(&p->lock);
1482 strncpy(dest, rdest, sizeof(dest) - 1);
1483 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1484 if ((ast->_state == AST_STATE_BUSY)) {
1485 p->subs[SUB_REAL].needbusy = 1;
1486 ast_mutex_unlock(&p->lock);
1489 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1490 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1491 ast_mutex_unlock(&p->lock);
1495 if (p->radio) /* if a radio channel, up immediately */
1497 /* Special pseudo -- automatically up */
1498 ast_setstate(ast, AST_STATE_UP);
1499 ast_mutex_unlock(&p->lock);
1502 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1503 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1505 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1508 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1514 if (p->owner == ast) {
1515 /* Normal ring, on hook */
1517 /* Don't send audio while on hook, until the call is answered */
1519 if (p->use_callerid) {
1520 /* Generate the Caller-ID spill if desired */
1522 ast_log(LOG_WARNING, "cidspill already exists??\n");
1525 p->cidspill = malloc(MAX_CALLERID_SIZE);
1528 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1532 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1534 /* Choose proper cadence */
1535 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1536 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1537 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1538 p->cidrings = cidrings[p->distinctivering - 1];
1540 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1541 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1542 p->cidrings = DEFAULT_CIDRINGS;
1546 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1547 c = strchr(dest, '/');
1550 if (c && (strlen(c) < p->stripmsd)) {
1551 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1555 p->dop.op = ZT_DIAL_OP_REPLACE;
1556 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1557 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1559 p->dop.dialstr[0] = '\0';
1562 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1563 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1564 ast_mutex_unlock(&p->lock);
1569 /* Call waiting call */
1570 p->callwaitrings = 0;
1571 if (ast->cid.cid_num)
1572 strncpy(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num)-1);
1574 p->callwait_num[0] = '\0';
1575 if (ast->cid.cid_name)
1576 strncpy(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name)-1);
1578 p->callwait_name[0] = '\0';
1579 /* Call waiting tone instead */
1580 if (zt_callwait(ast)) {
1581 ast_mutex_unlock(&p->lock);
1584 /* Make ring-back */
1585 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1586 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1589 n = ast->cid.cid_name;
1590 l = ast->cid.cid_num;
1592 strncpy(p->lastcid_num, l, sizeof(p->lastcid_num) - 1);
1594 p->lastcid_num[0] = '\0';
1596 strncpy(p->lastcid_name, n, sizeof(p->lastcid_name) - 1);
1598 p->lastcid_name[0] = '\0';
1599 ast_setstate(ast, AST_STATE_RINGING);
1600 index = zt_get_index(ast, p, 0);
1602 p->subs[index].needringing = 1;
1618 case SIG_SF_FEATDMF:
1620 c = strchr(dest, '/');
1625 if (strlen(c) < p->stripmsd) {
1626 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1627 ast_mutex_unlock(&p->lock);
1631 /* Start the trunk, if not GR-303 */
1635 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1637 if (errno != EINPROGRESS) {
1638 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1639 ast_mutex_unlock(&p->lock);
1646 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1647 p->dop.op = ZT_DIAL_OP_REPLACE;
1648 if (p->sig == SIG_FEATD) {
1649 l = ast->cid.cid_num;
1651 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1653 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1655 if (p->sig == SIG_FEATDMF) {
1656 l = ast->cid.cid_num;
1658 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1660 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1662 if (p->sig == SIG_E911) {
1663 strncpy(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr) - 1);
1665 if (p->sig == SIG_FEATB) {
1666 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1669 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c + p->stripmsd);
1671 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1672 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1673 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1674 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1675 p->echorest[sizeof(p->echorest) - 1] = '\0';
1677 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1681 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1683 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1684 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1685 ast_mutex_unlock(&p->lock);
1689 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1691 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1692 ast_setstate(ast, AST_STATE_DIALING);
1695 /* Special pseudo -- automatically up*/
1696 ast_setstate(ast, AST_STATE_UP);
1699 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1700 p->dialdest[0] = '\0';
1703 ast_log(LOG_DEBUG, "not yet implemented\n");
1704 ast_mutex_unlock(&p->lock);
1710 c = strchr(dest, '/');
1715 if (!p->hidecallerid) {
1716 l = ast->cid.cid_num;
1717 n = ast->cid.cid_name;
1722 if (strlen(c) < p->stripmsd) {
1723 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1724 ast_mutex_unlock(&p->lock);
1727 if (p->sig != SIG_FXSKS) {
1728 p->dop.op = ZT_DIAL_OP_REPLACE;
1729 s = strchr(c + p->stripmsd, 'w');
1732 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1734 p->dop.dialstr[0] = '\0';
1737 p->dop.dialstr[0] = '\0';
1740 if (pri_grab(p, p->pri)) {
1741 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1742 ast_mutex_unlock(&p->lock);
1745 if (!(p->call = pri_new_call(p->pri->pri))) {
1746 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1748 ast_mutex_unlock(&p->lock);
1751 if (!(sr = pri_sr_new())) {
1752 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1754 ast_mutex_unlock(&p->lock);
1756 if (p->bearer || (p->sig == SIG_FXSKS)) {
1758 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);
1759 p->bearer->call = p->call;
1761 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
1762 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1764 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1765 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1766 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1767 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1769 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1770 pri_sr_set_called(sr, c + p->stripmsd, p->pri->dialplan - 1, s ? 1 : 0);
1771 pri_sr_set_caller(sr, l, n, p->pri->localdialplan - 1,
1772 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
1773 PRES_NUMBER_NOT_AVAILABLE);
1774 if (pri_setup(p->pri->pri, p->call, sr)) {
1775 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1777 ast_mutex_unlock(&p->lock);
1782 ast_setstate(ast, AST_STATE_DIALING);
1786 ast_mutex_unlock(&p->lock);
1790 static void destroy_zt_pvt(struct zt_pvt **pvt)
1792 struct zt_pvt *p = *pvt;
1793 ast_mutex_destroy(&p->lock);
1798 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1808 for (i = 0; i < 3; i++) {
1809 if (cur->subs[i].owner) {
1815 prev->next = cur->next;
1817 prev->next->prev = prev;
1823 iflist->prev = NULL;
1827 if (cur->subs[SUB_REAL].zfd > -1) {
1828 zt_close(cur->subs[SUB_REAL].zfd);
1830 destroy_zt_pvt(&cur);
1834 prev->next = cur->next;
1836 prev->next->prev = prev;
1842 iflist->prev = NULL;
1846 if (cur->subs[SUB_REAL].zfd > -1) {
1847 zt_close(cur->subs[SUB_REAL].zfd);
1849 destroy_zt_pvt(&cur);
1855 int pri_is_up(struct zt_pri *pri)
1858 for (x=0;x<NUM_DCHANS;x++) {
1859 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1865 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
1867 bearer->owner = &inuse;
1868 bearer->master = crv;
1869 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
1870 if (crv->subs[SUB_REAL].owner)
1871 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
1872 crv->bearer = bearer;
1873 crv->call = bearer->call;
1878 static char *pri_order(int level)
1888 return "Quaternary";
1894 int pri_find_dchan(struct zt_pri *pri)
1901 for(x=0;x<NUM_DCHANS;x++) {
1902 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
1904 if (pri->dchans[x] == old) {
1910 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
1911 pri->dchannels[newslot]);
1913 if (old && (oldslot != newslot))
1914 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
1915 pri->dchannels[oldslot], pri->dchannels[newslot]);
1916 pri->pri = pri->dchans[newslot];
1921 static int zt_hangup(struct ast_channel *ast)
1925 static int restore_gains(struct zt_pvt *p);
1926 struct zt_pvt *p = ast->pvt->pvt;
1927 struct zt_pvt *tmp = NULL;
1928 struct zt_pvt *prev = NULL;
1932 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1933 if (!ast->pvt->pvt) {
1934 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1938 ast_mutex_lock(&p->lock);
1940 index = zt_get_index(ast, p, 1);
1942 if (p->sig == SIG_PRI) {
1944 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1950 if (p->origcid_num) {
1951 strncpy(p->cid_num, p->origcid_num, sizeof(p->cid_num) - 1);
1952 free(p->origcid_num);
1953 p->origcid_num = NULL;
1955 if (p->origcid_name) {
1956 strncpy(p->cid_name, p->origcid_name, sizeof(p->cid_name) - 1);
1957 free(p->origcid_name);
1958 p->origcid_name = NULL;
1961 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1965 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1966 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1970 /* Real channel, do some fixup */
1971 p->subs[index].owner = NULL;
1972 p->subs[index].needanswer = 0;
1973 p->subs[index].needringing = 0;
1974 p->subs[index].needbusy = 0;
1975 p->subs[index].needcongestion = 0;
1976 p->subs[index].linear = 0;
1977 p->subs[index].needcallerid = 0;
1978 zt_setlinear(p->subs[index].zfd, 0);
1979 if (index == SUB_REAL) {
1980 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1981 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1982 if (p->subs[SUB_CALLWAIT].inthreeway) {
1983 /* We had flipped over to answer a callwait and now it's gone */
1984 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1985 /* Move to the call-wait, but un-own us until they flip back. */
1986 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1987 unalloc_sub(p, SUB_CALLWAIT);
1990 /* The three way hung up, but we still have a call wait */
1991 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1992 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1993 unalloc_sub(p, SUB_THREEWAY);
1994 if (p->subs[SUB_REAL].inthreeway) {
1995 /* This was part of a three way call. Immediately make way for
1997 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1998 p->owner = p->subs[SUB_REAL].owner;
2000 /* This call hasn't been completed yet... Set owner to NULL */
2001 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2004 p->subs[SUB_REAL].inthreeway = 0;
2006 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2007 /* Move to the call-wait and switch back to them. */
2008 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2009 unalloc_sub(p, SUB_CALLWAIT);
2010 p->owner = p->subs[SUB_REAL].owner;
2011 if (p->owner->_state != AST_STATE_UP)
2012 p->subs[SUB_REAL].needanswer = 1;
2013 if (p->subs[SUB_REAL].owner->bridge)
2014 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
2015 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2016 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2017 unalloc_sub(p, SUB_THREEWAY);
2018 if (p->subs[SUB_REAL].inthreeway) {
2019 /* This was part of a three way call. Immediately make way for
2021 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2022 p->owner = p->subs[SUB_REAL].owner;
2024 /* This call hasn't been completed yet... Set owner to NULL */
2025 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2028 p->subs[SUB_REAL].inthreeway = 0;
2030 } else if (index == SUB_CALLWAIT) {
2031 /* Ditch the holding callwait call, and immediately make it availabe */
2032 if (p->subs[SUB_CALLWAIT].inthreeway) {
2033 /* This is actually part of a three way, placed on hold. Place the third part
2034 on music on hold now */
2035 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
2036 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
2037 p->subs[SUB_THREEWAY].inthreeway = 0;
2038 /* Make it the call wait now */
2039 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2040 unalloc_sub(p, SUB_THREEWAY);
2042 unalloc_sub(p, SUB_CALLWAIT);
2043 } else if (index == SUB_THREEWAY) {
2044 if (p->subs[SUB_CALLWAIT].inthreeway) {
2045 /* The other party of the three way call is currently in a call-wait state.
2046 Start music on hold for them, and take the main guy out of the third call */
2047 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
2048 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
2049 p->subs[SUB_CALLWAIT].inthreeway = 0;
2051 p->subs[SUB_REAL].inthreeway = 0;
2052 /* If this was part of a three way call index, let us make
2053 another three way call */
2054 unalloc_sub(p, SUB_THREEWAY);
2056 /* This wasn't any sort of call, but how are we an index? */
2057 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2062 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2065 p->distinctivering = 0;
2066 p->confirmanswer = 0;
2072 p->onhooktime = time(NULL);
2078 ast_dsp_free(p->dsp);
2082 law = ZT_LAW_DEFAULT;
2083 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2085 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2086 /* Perform low level hangup if no owner left */
2089 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2090 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2091 if (!pri_grab(p, p->pri)) {
2092 if (p->alreadyhungup) {
2093 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2094 pri_hangup(p->pri->pri, p->call, -1);
2097 p->bearer->call = NULL;
2099 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2100 int icause = ast->hangupcause ? ast->hangupcause : -1;
2101 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2102 p->alreadyhungup = 1;
2104 p->bearer->alreadyhungup = 1;
2107 icause = atoi(cause);
2109 pri_hangup(p->pri->pri, p->call, icause);
2112 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2115 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2120 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2127 if (p->sig == SIG_R2) {
2129 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2137 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2138 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2140 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2146 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2149 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2151 /* If they're off hook, try playing congestion */
2152 if ((par.rxisoffhook) && (!p->radio))
2153 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2155 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2161 /* Make sure we're not made available for at least two seconds assuming
2162 we were actually used for an inbound or outbound call. */
2163 if (ast->_state != AST_STATE_RESERVED) {
2164 time(&p->guardtime);
2169 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2176 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2177 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2181 p->callwaiting = p->permcallwaiting;
2182 p->hidecallerid = p->permhidecallerid;
2187 /* Restore data mode */
2188 if (p->sig == SIG_PRI) {
2190 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2194 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2195 /* Free up the bearer channel as well, and
2196 don't use its file descriptor anymore */
2197 update_conf(p->bearer);
2198 reset_conf(p->bearer);
2199 p->bearer->owner = NULL;
2201 p->subs[SUB_REAL].zfd = -1;
2209 p->callwaitingrepeat = 0;
2211 ast->pvt->pvt = NULL;
2212 ast_mutex_unlock(&p->lock);
2213 ast_mutex_lock(&usecnt_lock);
2216 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2217 ast_mutex_unlock(&usecnt_lock);
2218 ast_update_use_count();
2219 if (option_verbose > 2)
2220 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2222 ast_mutex_lock(&iflock);
2228 destroy_channel(prev, tmp, 0);
2236 ast_mutex_unlock(&iflock);
2240 static int zt_answer(struct ast_channel *ast)
2242 struct zt_pvt *p = ast->pvt->pvt;
2245 int oldstate = ast->_state;
2246 ast_setstate(ast, AST_STATE_UP);
2247 ast_mutex_lock(&p->lock);
2248 index = zt_get_index(ast, p, 0);
2251 /* nothing to do if a radio channel */
2253 ast_mutex_unlock(&p->lock);
2272 case SIG_SF_FEATDMF:
2277 /* Pick up the line */
2278 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2279 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2280 tone_zone_play_tone(p->subs[index].zfd, -1);
2282 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2283 if (oldstate == AST_STATE_RINGING) {
2284 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2285 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2286 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2287 p->owner = p->subs[SUB_REAL].owner;
2290 if (p->sig & __ZT_SIG_FXS) {
2297 /* Send a pri acknowledge */
2298 if (!pri_grab(p, p->pri)) {
2300 res = pri_answer(p->pri->pri, p->call, 0, 1);
2303 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2310 res = mfcr2_AnswerCall(p->r2, NULL);
2312 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2316 ast_mutex_unlock(&p->lock);
2319 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2322 ast_mutex_unlock(&p->lock);
2326 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2331 struct zt_pvt *p = chan->pvt->pvt;
2334 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2335 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2341 if ((!cp) || (datalen < 1))
2347 case AST_OPTION_TONE_VERIFY:
2352 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2353 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2356 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2357 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2360 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2361 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2365 case AST_OPTION_TDD: /* turn on or off TDD */
2366 if (!*cp) { /* turn it off */
2367 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2368 if (p->tdd) tdd_free(p->tdd);
2374 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2375 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2378 /* otherwise, turn it on */
2379 if (!p->didtdd) { /* if havent done it yet */
2380 unsigned char mybuf[41000],*buf;
2381 int size,res,fd,len;
2383 struct pollfd fds[1];
2385 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2386 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2388 index = zt_get_index(chan, p, 0);
2390 ast_log(LOG_WARNING, "No index in TDD?\n");
2393 fd = p->subs[index].zfd;
2395 if (ast_check_hangup(chan)) return -1;
2397 if (size > READ_SIZE)
2400 fds[0].events = POLLPRI | POLLOUT;
2401 res = poll(fds, 1, -1);
2403 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2406 /* if got exception */
2407 if (fds[0].revents & POLLPRI) return -1;
2408 if (!(fds[0].revents & POLLOUT)) {
2409 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2412 res = write(fd, buf, size);
2414 if (res == -1) return -1;
2415 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2421 p->didtdd = 1; /* set to have done it now */
2423 if (*cp == 2) { /* Mate mode */
2424 if (p->tdd) tdd_free(p->tdd);
2429 if (!p->tdd) { /* if we dont have one yet */
2430 p->tdd = tdd_new(); /* allocate one */
2433 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2436 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2441 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2444 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2446 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2449 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2455 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2458 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2459 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2466 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2468 /* Unlink a specific slave or all slaves/masters from a given master */
2474 ast_mutex_lock(&master->lock);
2476 while(ast_mutex_trylock(&slave->lock)) {
2477 ast_mutex_unlock(&master->lock);
2479 ast_mutex_lock(&master->lock);
2484 for (x=0;x<MAX_SLAVES;x++) {
2485 if (master->slaves[x]) {
2486 if (!slave || (master->slaves[x] == slave)) {
2487 /* Take slave out of the conference */
2488 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2489 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2490 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2491 master->slaves[x]->master = NULL;
2492 master->slaves[x] = NULL;
2497 master->inconference = 0;
2500 if (master->master) {
2501 /* Take master out of the conference */
2502 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2503 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2505 for (x=0;x<MAX_SLAVES;x++) {
2506 if (master->master->slaves[x] == master)
2507 master->master->slaves[x] = NULL;
2508 else if (master->master->slaves[x])
2512 master->master->inconference = 0;
2514 master->master = NULL;
2516 update_conf(master);
2519 ast_mutex_unlock(&slave->lock);
2520 ast_mutex_unlock(&master->lock);
2524 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2526 if (!slave || !master) {
2527 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2530 for (x=0;x<MAX_SLAVES;x++) {
2531 if (!master->slaves[x]) {
2532 master->slaves[x] = slave;
2536 if (x >= MAX_SLAVES) {
2537 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2538 master->slaves[MAX_SLAVES - 1] = slave;
2541 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2542 slave->master = master;
2544 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2547 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2549 struct ast_channel *who = NULL, *cs[3];
2550 struct zt_pvt *p0, *p1, *op0, *op1;
2551 struct zt_pvt *master=NULL, *slave=NULL;
2552 struct ast_frame *f;
2557 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2558 int os1 = -1, os2 = -1;
2559 struct ast_channel *oc1, *oc2;
2561 /* if need DTMF, cant native bridge */
2562 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2565 ast_mutex_lock(&c0->lock);
2566 ast_mutex_lock(&c1->lock);
2570 /* cant do pseudo-channels here */
2571 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2572 ast_mutex_unlock(&c0->lock);
2573 ast_mutex_unlock(&c1->lock);
2577 op0 = p0 = c0->pvt->pvt;
2578 op1 = p1 = c1->pvt->pvt;
2581 oi1 = zt_get_index(c0, p0, 0);
2582 oi2 = zt_get_index(c1, p1, 0);
2585 if ((oi1 < 0) || (oi2 < 0)) {
2586 ast_mutex_unlock(&c0->lock);
2587 ast_mutex_unlock(&c1->lock);
2593 ast_mutex_lock(&p0->lock);
2594 if (ast_mutex_trylock(&p1->lock)) {
2595 /* Don't block, due to potential for deadlock */
2596 ast_mutex_unlock(&p0->lock);
2597 ast_mutex_unlock(&c0->lock);
2598 ast_mutex_unlock(&c1->lock);
2599 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2602 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2603 if (!p0->owner || !p1->owner) {
2604 /* Currently unowned -- Do nothing. */
2607 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2608 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2612 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2617 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2618 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,
2619 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2622 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2623 if (p1->subs[SUB_THREEWAY].inthreeway) {
2629 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2630 if (p0->subs[SUB_THREEWAY].inthreeway) {
2636 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2637 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2638 don't put us in anything */
2639 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2645 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2646 /* Same as previous */
2647 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2654 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2655 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2656 if (master && slave) {
2657 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2658 in an active threeway call with a channel that is ringing, we should
2659 indicate ringing. */
2660 if ((oi2 == SUB_THREEWAY) &&
2661 p1->subs[SUB_THREEWAY].inthreeway &&
2662 p1->subs[SUB_REAL].owner &&
2663 p1->subs[SUB_REAL].inthreeway &&
2664 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2665 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2666 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2667 os2 = p1->subs[SUB_REAL].owner->_state;
2669 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2670 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2672 if ((oi1 == SUB_THREEWAY) &&
2673 p0->subs[SUB_THREEWAY].inthreeway &&
2674 p0->subs[SUB_REAL].owner &&
2675 p0->subs[SUB_REAL].inthreeway &&
2676 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2677 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2678 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2679 os1 = p0->subs[SUB_REAL].owner->_state;
2681 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2682 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2684 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2685 if (!p0->echocanbridged || !p1->echocanbridged) {
2686 /* Disable echo cancellation if appropriate */
2691 zt_link(slave, master);
2692 master->inconference = inconf;
2693 } else if (!nothingok)
2694 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2698 t1 = p0->subs[SUB_REAL].inthreeway;
2699 t2 = p1->subs[SUB_REAL].inthreeway;
2701 ast_mutex_unlock(&p0->lock);
2702 ast_mutex_unlock(&p1->lock);
2704 ast_mutex_unlock(&c0->lock);
2705 ast_mutex_unlock(&c1->lock);
2707 /* Native bridge failed */
2708 if ((!master || !slave) && !nothingok) {
2720 /* Here's our main loop... Start by locking things, looking for private parts,
2721 and then balking if anything is wrong */
2722 ast_mutex_lock(&c0->lock);
2723 ast_mutex_lock(&c1->lock);
2727 i1 = zt_get_index(c0, p0, 1);
2729 i2 = zt_get_index(c1, p1, 1);
2730 ast_mutex_unlock(&c0->lock);
2731 ast_mutex_unlock(&c1->lock);
2732 if ((op0 != p0) || (op1 != p1) ||
2733 (ofd1 != c0->fds[0]) ||
2734 (ofd2 != c1->fds[0]) ||
2735 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2736 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2737 (oc1 != p0->owner) ||
2738 (oc2 != p1->owner) ||
2739 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2740 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2743 if (slave && master)
2744 zt_unlink(slave, master, 1);
2745 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2746 op0->channel, oi1, op1->channel, oi2);
2754 who = ast_waitfor_n(cs, 2, &to);
2756 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2759 if (who->pvt->pvt == op0)
2760 op0->ignoredtmf = 1;
2761 else if (who->pvt->pvt == op1)
2762 op1->ignoredtmf = 1;
2764 if (who->pvt->pvt == op0)
2765 op0->ignoredtmf = 0;
2766 else if (who->pvt->pvt == op1)
2767 op1->ignoredtmf = 0;
2771 if (slave && master)
2772 zt_unlink(slave, master, 1);
2779 if (f->frametype == AST_FRAME_DTMF) {
2780 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2781 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2784 if (slave && master)
2785 zt_unlink(slave, master, 1);
2787 } else if ((who == c0) && p0->pulsedial) {
2789 } else if ((who == c1) && p1->pulsedial) {
2795 /* Swap who gets priority */
2802 static int zt_indicate(struct ast_channel *chan, int condition);
2804 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2806 struct zt_pvt *p = newchan->pvt->pvt;
2808 ast_mutex_lock(&p->lock);
2809 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2810 if (p->owner == oldchan) {
2814 if (p->subs[x].owner == oldchan) {
2816 zt_unlink(NULL, p, 0);
2817 p->subs[x].owner = newchan;
2819 if (newchan->_state == AST_STATE_RINGING)
2820 zt_indicate(newchan, AST_CONTROL_RINGING);
2822 ast_mutex_unlock(&p->lock);
2826 static int zt_ring_phone(struct zt_pvt *p)
2830 /* Make sure our transmit state is on hook */
2833 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2836 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2838 printf("Res: %d, error: %s\n", res, strerror(errno));
2844 /* Wait just in case */
2851 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2859 static void *ss_thread(void *data);
2861 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2863 static int attempt_transfer(struct zt_pvt *p)
2865 /* In order to transfer, we need at least one of the channels to
2866 actually be in a call bridge. We can't conference two applications
2867 together (but then, why would we want to?) */
2868 if (p->subs[SUB_REAL].owner->bridge) {
2869 /* The three-way person we're about to transfer to could still be in MOH, so
2870 stop if now if appropriate */
2871 if (p->subs[SUB_THREEWAY].owner->bridge)
2872 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2873 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2874 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2876 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2877 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2878 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2881 /* Orphan the channel after releasing the lock */
2882 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
2883 unalloc_sub(p, SUB_THREEWAY);
2884 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2885 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2886 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2888 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2889 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2890 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2891 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2894 /* Three-way is now the REAL */
2895 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2896 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
2897 unalloc_sub(p, SUB_THREEWAY);
2898 /* Tell the caller not to hangup */
2901 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2902 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2903 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2909 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2911 struct ast_frame *f;
2912 f = &p->subs[index].f;
2914 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2918 case MFCR2_EVENT_BLOCKED:
2919 if (option_verbose > 2)
2920 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2922 case MFCR2_EVENT_UNBLOCKED:
2923 if (option_verbose > 2)
2924 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2926 case MFCR2_EVENT_CONFIG_ERR:
2927 if (option_verbose > 2)
2928 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2930 case MFCR2_EVENT_RING:
2931 if (option_verbose > 2)
2932 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2934 case MFCR2_EVENT_HANGUP:
2935 if (option_verbose > 2)
2936 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2938 case MFCR2_EVENT_RINGING:
2939 if (option_verbose > 2)
2940 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2942 case MFCR2_EVENT_ANSWER:
2943 if (option_verbose > 2)
2944 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2946 case MFCR2_EVENT_HANGUP_ACK:
2947 if (option_verbose > 2)
2948 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2950 case MFCR2_EVENT_IDLE:
2951 if (option_verbose > 2)
2952 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2955 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2961 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2966 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2968 ast_log(LOG_WARNING, "Unable to check received bits\n");
2972 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2975 e = mfcr2_cas_signaling_event(p->r2, x);
2980 static int check_for_conference(struct zt_pvt *p)
2983 /* Fine if we already have a master, etc */
2984 if (p->master || (p->confno > -1))
2986 memset(&ci, 0, sizeof(ci));
2987 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2988 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2991 /* If we have no master and don't have a confno, then
2992 if we're in a conference, it's probably a MeetMe room or
2993 some such, so don't let us 3-way out! */
2994 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
2995 if (option_verbose > 2)
2996 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
3002 static int get_alarms(struct zt_pvt *p)