2 * Asterisk -- A telephony toolkit for Linux.
4 * Zaptel Pseudo TDM interface
6 * Copyright (C) 2003 Digium
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/parking.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__ */
58 #ifndef PRI_SETUP_CALL
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 */
84 /* Typically, how many rings before we should send Caller*ID */
85 #define DEFAULT_CIDRINGS 1
87 #define CHANNEL_PSEUDO -12
89 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
91 static char *desc = "Zapata Telephony"
100 static char *tdesc = "Zapata Telephony Driver"
109 static char *type = "Zap";
110 static char *typecompat = "Tor"; /* Retain compatibility with chan_tor */
111 static char *config = "zapata.conf";
113 #define SIG_EM ZT_SIG_EM
114 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
115 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
116 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
117 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
118 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
119 #define SIG_FXSLS ZT_SIG_FXSLS
120 #define SIG_FXSGS ZT_SIG_FXSGS
121 #define SIG_FXSKS ZT_SIG_FXSKS
122 #define SIG_FXOLS ZT_SIG_FXOLS
123 #define SIG_FXOGS ZT_SIG_FXOGS
124 #define SIG_FXOKS ZT_SIG_FXOKS
125 #define SIG_PRI ZT_SIG_CLEAR
126 #define SIG_R2 ZT_SIG_CAS
127 #define SIG_SF ZT_SIG_SF
128 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
129 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
130 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
131 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
132 #define SIG_EM_E1 ZT_SIG_EM_E1
133 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
136 #define NUM_DCHANS 4 /* No more than 4 d-channels */
137 #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
138 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
140 #define CHAN_PSEUDO -2
142 #define DCHAN_PROVISIONED (1 << 0)
143 #define DCHAN_NOTINALARM (1 << 1)
144 #define DCHAN_UP (1 << 2)
146 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
148 static char context[AST_MAX_EXTENSION] = "default";
149 static char callerid[256] = "";
151 static char language[MAX_LANGUAGE] = "";
152 static char musicclass[MAX_LANGUAGE] = "";
153 static char progzone[10]= "";
155 static int usedistinctiveringdetection = 0;
157 static int use_callerid = 1;
158 static int zaptrcallerid = 0;
159 static int cur_signalling = -1;
161 static unsigned int cur_group = 0;
162 static unsigned int cur_callergroup = 0;
163 static unsigned int cur_pickupgroup = 0;
164 static int relaxdtmf = 0;
166 static int immediate = 0;
168 static int stripmsd = 0;
170 static int callwaiting = 0;
172 static int callwaitingcallerid = 0;
174 static int hidecallerid = 0;
176 static int restrictcid = 0;
178 static int use_callingpres = 0;
180 static int callreturn = 0;
182 static int threewaycalling = 0;
184 static int transfer = 0;
186 static int cancallforward = 0;
188 static float rxgain = 0.0;
190 static float txgain = 0.0;
192 static int tonezone = -1;
194 static int echocancel;
196 static int echotraining;
198 static int echocanbridged = 0;
200 static int busydetect = 0;
202 static int busycount = 3;
204 static int callprogress = 0;
206 static char accountcode[20] = "";
208 static char mailbox[AST_MAX_EXTENSION];
210 static int amaflags = 0;
214 static int numbufs = 4;
216 static int cur_prewink = -1;
217 static int cur_preflash = -1;
218 static int cur_wink = -1;
219 static int cur_flash = -1;
220 static int cur_start = -1;
221 static int cur_rxwink = -1;
222 static int cur_rxflash = -1;
223 static int cur_debounce = -1;
226 static int minunused = 2;
227 static int minidle = 0;
228 static char idleext[AST_MAX_EXTENSION];
229 static char idledial[AST_MAX_EXTENSION];
230 static int overlapdial = 0;
231 static struct ast_channel inuse = { "GR-303InUse" };
234 /* Wait up to 16 seconds for first digit (FXO logic) */
235 static int firstdigittimeout = 16000;
237 /* How long to wait for following digits (FXO logic) */
238 static int gendigittimeout = 8000;
240 /* How long to wait for an extra digit, if there is an ambiguous match */
241 static int matchdigittimeout = 3000;
243 static int usecnt =0;
244 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
246 /* Protect the interface list (of zt_pvt's) */
247 AST_MUTEX_DEFINE_STATIC(iflock);
249 static int ifcount = 0;
251 /* Protect the monitoring thread, so only one process can kill or start it, and not
252 when it's doing something critical. */
253 AST_MUTEX_DEFINE_STATIC(monlock);
255 /* This is the thread for the monitor which checks for input on the channels
256 which are not currently in use. */
257 static pthread_t monitor_thread = AST_PTHREADT_NULL;
259 static int restart_monitor(void);
261 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
263 static int zt_sendtext(struct ast_channel *c, char *text);
265 static inline int zt_get_event(int fd)
267 /* Avoid the silly zt_getevent which ignores a bunch of events */
269 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
273 static inline int zt_wait_event(int fd)
275 /* Avoid the silly zt_waitevent which ignores a bunch of events */
277 i = ZT_IOMUX_SIGEVENT;
278 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
279 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
283 /* Chunk size to read -- we use 20ms chunks to make things happy. */
284 #define READ_SIZE 160
286 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
287 #define MASK_INUSE (1 << 1) /* Channel currently in use */
289 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
290 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
291 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
292 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
293 #define RINGT ( (8000 * 8) / READ_SIZE)
299 static int r2prot = -1;
305 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8))
306 #define PRI_CHANNEL(p) ((p) & 0xff)
307 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
310 pthread_t master; /* Thread of master */
311 ast_mutex_t lock; /* Mutex */
312 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
313 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
314 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
315 int minunused; /* Min # of channels to keep empty */
316 int minidle; /* Min # of "idling" calls to keep active */
317 int nodetype; /* Node type */
318 int switchtype; /* Type of switch to emulate */
319 int dialplan; /* Dialing plan */
320 int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
321 int trunkgroup; /* What our trunkgroup is */
322 int mastertrunkgroup; /* What trunk group is our master */
323 int prilogicalspan; /* Logical span number within trunk group */
324 int numchans; /* Num of channels we represent */
325 int overlapdial; /* In overlap dialing mode */
326 struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
327 int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
328 struct pri *pri; /* Currently active D-channel */
330 int fds[NUM_DCHANS]; /* FD's for d-channels */
336 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
337 struct zt_pvt *crvs; /* Member CRV structs */
338 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
342 static struct zt_pri pris[NUM_SPANS];
344 static int pritype = PRI_CPE;
347 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
349 #define DEFAULT_PRI_DEBUG 0
352 static inline void pri_rel(struct zt_pri *pri)
354 ast_mutex_unlock(&pri->lock);
357 static int switchtype = PRI_SWITCH_NI2;
358 static int dialplan = PRI_NATIONAL_ISDN + 1;
361 /* Shut up the compiler */
365 #define SUB_REAL 0 /* Active call */
366 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
367 #define SUB_THREEWAY 2 /* Three-way call */
370 static struct zt_distRings drings;
372 struct distRingData {
375 struct ringContextData {
376 char contextData[AST_MAX_EXTENSION];
378 struct zt_distRings {
379 struct distRingData ringnum[3];
380 struct ringContextData ringContext[3];
383 static char *subnames[] = {
389 struct zt_subchannel {
391 struct ast_channel *owner;
393 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
394 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
405 #define CONF_USER_REAL (1 << 0)
406 #define CONF_USER_THIRDCALL (1 << 1)
410 static struct zt_pvt {
412 struct ast_channel *owner; /* Our current active owner (if applicable) */
413 /* Up to three channels can be associated with this call */
415 struct zt_subchannel sub_unused; /* Just a safety precaution */
416 struct zt_subchannel subs[3]; /* Sub-channels */
417 struct zt_confinfo saveconf; /* Saved conference info */
419 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
420 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
421 int inconference; /* If our real should be in the conference */
423 int sig; /* Signalling style */
424 int radio; /* radio type */
425 int firstradio; /* first radio flag */
428 int tonezone; /* tone zone for this chan, or -1 for default */
429 struct zt_pvt *next; /* Next channel in list */
430 struct zt_pvt *prev; /* Prev channel in list */
432 struct zt_distRings drings;
433 int usedistinctiveringdetection;
435 char context[AST_MAX_EXTENSION];
436 char defcontext[AST_MAX_EXTENSION];
437 char exten[AST_MAX_EXTENSION];
438 char language[MAX_LANGUAGE];
439 char musicclass[MAX_LANGUAGE];
440 char callerid[AST_MAX_EXTENSION];
441 char lastcallerid[AST_MAX_EXTENSION];
442 char *origcallerid; /* malloced original callerid */
443 char callwaitcid[AST_MAX_EXTENSION];
444 char rdnis[AST_MAX_EXTENSION];
445 char dnid[AST_MAX_EXTENSION];
448 int confno; /* Our conference */
449 int confusers; /* Who is using our conference */
450 int propconfno; /* Propagated conference number */
451 unsigned int callgroup;
452 unsigned int pickupgroup;
453 int immediate; /* Answer before getting digits? */
454 int channel; /* Channel Number or CRV */
455 int span; /* Span number */
457 time_t guardtime; /* Must wait this much time before using for new call */
459 int use_callerid; /* Whether or not to use caller id on this channel */
462 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
463 int restrictcid; /* Whether restrict the callerid -> only send ANI */
464 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
465 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
466 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
467 int cidcwexpire; /* When to expire our muting for CID/CW */
468 unsigned char *cidspill;
483 int callwaitingcallerid;
492 struct timeval flashtime; /* Last flash-hook time */
494 int cref; /* Call reference number */
495 ZT_DIAL_OPERATION dop;
499 char accountcode[20]; /* Account code */
500 int amaflags; /* AMA Flags */
501 char didtdd; /* flag to say its done it once */
502 struct tdd_state *tdd; /* TDD flag */
505 char call_forward[AST_MAX_EXTENSION];
506 char mailbox[AST_MAX_EXTENSION];
511 int confirmanswer; /* Wait for '#' to confirm answer */
512 int distinctivering; /* Which distinctivering to use */
513 int cidrings; /* Which ring to deliver CID on */
515 int faxhandled; /* Has a fax tone already been handled? */
517 char mate; /* flag to say its in MATE mode */
518 int pulsedial; /* whether a pulse dial phone is detected */
519 int dtmfrelax; /* whether to run in relaxed DTMF mode */
521 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
524 struct zt_pvt *bearer;
525 struct zt_pvt *realcall;
533 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
542 } *iflist = NULL, *ifend = NULL;
544 struct zt_pvt *round_robin[32];
547 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
550 /* Grab the lock first */
552 res = ast_mutex_trylock(&pri->lock);
554 ast_mutex_unlock(&pvt->lock);
555 /* Release the lock and try again */
557 ast_mutex_lock(&pvt->lock);
560 /* Then break the poll */
561 pthread_kill(pri->master, SIGURG);
566 #define NUM_CADENCE_MAX 25
567 static int num_cadence = 4;
568 static int user_has_defined_cadences = 0;
570 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
571 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
572 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
573 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
574 { { 1000, 500, 2500, 5000 } }, /* Long ring */
577 int receivedRingT; /* Used to find out what ringtone we are on */
579 /* cidrings says in which pause to transmit the cid information, where the first pause
580 * is 1, the second pause is 2 and so on.
583 static int cidrings[NUM_CADENCE_MAX] = {
584 2, /* Right after first long ring */
585 4, /* Right after long part */
586 3, /* After third chirp */
587 2, /* Second spell */
590 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
591 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
593 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
594 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
597 /* translate between PRI causes and asterisk's */
598 static int hangup_pri2cause(int cause)
601 case PRI_CAUSE_USER_BUSY:
602 return AST_CAUSE_BUSY;
603 case PRI_CAUSE_NORMAL_CLEARING:
604 return AST_CAUSE_NORMAL;
605 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
606 case PRI_CAUSE_REQUESTED_CHAN_UNAVAIL:
607 return AST_CAUSE_CONGESTION;
608 case PRI_CAUSE_UNALLOCATED:
609 case PRI_CAUSE_NUMBER_CHANGED:
610 return AST_CAUSE_UNALLOCATED;
612 return AST_CAUSE_FAILURE;
618 /* translate between ast cause and PRI */
619 static int hangup_cause2pri(int cause)
623 return PRI_CAUSE_USER_BUSY;
624 case AST_CAUSE_UNALLOCATED:
625 return PRI_CAUSE_UNALLOCATED;
626 case AST_CAUSE_CONGESTION:
627 return PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION;
628 case AST_CAUSE_NORMAL:
630 return PRI_CAUSE_NORMAL_CLEARING;
637 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
640 if (p->subs[0].owner == ast)
642 else if (p->subs[1].owner == ast)
644 else if (p->subs[2].owner == ast)
649 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
654 static void wakeup_sub(struct zt_pvt *p, int a)
656 struct ast_frame null = { AST_FRAME_NULL, };
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 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f)
677 if (ast_mutex_trylock(&p->owner->lock)) {
678 ast_mutex_unlock(&p->lock);
680 ast_mutex_lock(&p->lock);
682 ast_queue_frame(p->owner, f);
683 ast_mutex_unlock(&p->owner->lock);
691 static void swap_subs(struct zt_pvt *p, int a, int b)
695 struct ast_channel *towner;
697 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
699 tchan = p->subs[a].chan;
700 towner = p->subs[a].owner;
701 tinthreeway = p->subs[a].inthreeway;
703 p->subs[a].chan = p->subs[b].chan;
704 p->subs[a].owner = p->subs[b].owner;
705 p->subs[a].inthreeway = p->subs[b].inthreeway;
707 p->subs[b].chan = tchan;
708 p->subs[b].owner = towner;
709 p->subs[b].inthreeway = tinthreeway;
711 if (p->subs[a].owner)
712 p->subs[a].owner->fds[0] = p->subs[a].zfd;
713 if (p->subs[b].owner)
714 p->subs[b].owner->fds[0] = p->subs[b].zfd;
719 static int zt_open(char *fn)
727 for (x=0;x<strlen(fn);x++) {
728 if (!isdigit(fn[x])) {
736 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
739 fn = "/dev/zap/channel";
741 fd = open(fn, O_RDWR | O_NONBLOCK);
743 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
747 if (ioctl(fd, ZT_SPECIFY, &chan)) {
751 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
756 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
760 static void zt_close(int fd)
765 int zt_setlinear(int zfd, int linear)
768 res = ioctl(zfd, ZT_SETLINEAR, &linear);
775 int zt_setlaw(int zfd, int law)
778 res = ioctl(zfd, ZT_SETLAW, &law);
784 static int alloc_sub(struct zt_pvt *p, int x)
788 if (p->subs[x].zfd < 0) {
789 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
790 if (p->subs[x].zfd > -1) {
791 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
793 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
794 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
795 bi.numbufs = numbufs;
796 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
798 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
801 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
802 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
803 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
804 zt_close(p->subs[x].zfd);
809 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
812 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
815 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
819 static int unalloc_sub(struct zt_pvt *p, int x)
822 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
825 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
826 if (p->subs[x].zfd > -1) {
827 zt_close(p->subs[x].zfd);
830 p->subs[x].linear = 0;
832 p->subs[x].owner = NULL;
833 p->subs[x].inthreeway = 0;
834 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
838 static int zt_digit(struct ast_channel *ast, char digit)
840 ZT_DIAL_OPERATION zo;
845 ast_mutex_lock(&p->lock);
846 index = zt_get_index(ast, p, 0);
847 if (index == SUB_REAL) {
849 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
851 if (!pri_grab(p, p->pri)) {
852 pri_information(p->pri->pri,p->call,digit);
855 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
856 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
857 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
858 res = strlen(p->dialdest);
859 p->dialdest[res++] = digit;
860 p->dialdest[res] = '\0';
866 zo.op = ZT_DIAL_OP_APPEND;
868 zo.dialstr[1] = digit;
870 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
871 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
876 ast_mutex_unlock(&p->lock);
880 static char *events[] = {
893 "Hook Transition Complete",
902 { ZT_ALARM_RED, "Red Alarm" },
903 { ZT_ALARM_YELLOW, "Yellow Alarm" },
904 { ZT_ALARM_BLUE, "Blue Alarm" },
905 { ZT_ALARM_RECOVER, "Recovering" },
906 { ZT_ALARM_LOOPBACK, "Loopback" },
907 { ZT_ALARM_NOTOPEN, "Not Open" },
908 { ZT_ALARM_NONE, "None" },
911 static char *alarm2str(int alarm)
914 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
915 if (alarms[x].alarm & alarm)
916 return alarms[x].name;
918 return alarm ? "Unknown Alarm" : "No Alarm";
921 static char *event2str(int event)
923 static char buf[256];
924 if ((event < 15) && (event > -1))
925 return events[event];
926 sprintf(buf, "Event %d", event);
931 static int str2r2prot(char *swtype)
933 if (!strcasecmp(swtype, "ar"))
934 return MFCR2_PROT_ARGENTINA;
936 if (!strcasecmp(swtype, "cn"))
937 return MFCR2_PROT_CHINA;
939 if (!strcasecmp(swtype, "kr"))
940 return MFCR2_PROT_KOREA;
946 static char *sig2str(int sig)
948 static char buf[256];
951 return "E & M Immediate";
957 return "Feature Group D (DTMF)";
959 return "Feature Group D (MF)";
961 return "Feature Group B (MF)";
965 return "FXS Loopstart";
967 return "FXS Groundstart";
969 return "FXS Kewlstart";
971 return "FXO Loopstart";
973 return "FXO Groundstart";
975 return "FXO Kewlstart";
977 return "PRI Signalling";
979 return "R2 Signalling";
981 return "SF (Tone) Signalling Immediate";
983 return "SF (Tone) Signalling Wink";
985 return "SF (Tone) Signalling with Feature Group D (DTMF)";
987 return "SF (Tone) Signalling with Feature Group D (MF)";
989 return "SF (Tone) Signalling with Feature Group B (MF)";
991 return "GR-303 Signalling with FXOKS";
993 return "Pseudo Signalling";
995 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1000 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1002 /* If the conference already exists, and we're already in it
1003 don't bother doing anything */
1006 memset(&zi, 0, sizeof(zi));
1009 if (slavechannel > 0) {
1010 /* If we have only one slave, do a digital mon */
1011 zi.confmode = ZT_CONF_DIGITALMON;
1012 zi.confno = slavechannel;
1015 /* Real-side and pseudo-side both participate in conference */
1016 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1017 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1019 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1020 zi.confno = p->confno;
1022 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1026 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1027 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1030 if (slavechannel < 1) {
1031 p->confno = zi.confno;
1033 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1034 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1038 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1040 /* If they're listening to our channel, they're ours */
1041 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1043 /* If they're a talker on our (allocated) conference, they're ours */
1044 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1049 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1052 if (/* Can't delete if there's no zfd */
1054 /* Don't delete from the conference if it's not our conference */
1056 /* Don't delete if we don't think it's conferenced at all (implied) */
1058 memset(&zi, 0, sizeof(zi));
1062 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1063 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1066 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1067 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1071 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1075 struct zt_pvt *slave = NULL;
1076 /* Start out optimistic */
1078 /* Update conference state in a stateless fashion */
1080 /* Any three-way calling makes slave native mode *definitely* out
1082 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1085 /* If we don't have any 3-way calls, check to see if we have
1086 precisely one slave */
1087 if (useslavenative) {
1088 for (x=0;x<MAX_SLAVES;x++) {
1091 /* Whoops already have a slave! No
1092 slave native and stop right away */
1097 /* We have one slave so far */
1098 slave = p->slaves[x];
1103 /* If no slave, slave native definitely out */
1106 else if (slave->law != p->law) {
1112 return useslavenative;
1115 static int update_conf(struct zt_pvt *p)
1120 struct zt_pvt *slave = NULL;
1122 useslavenative = isslavenative(p, &slave);
1123 /* Start with the obvious, general stuff */
1125 /* Look for three way calls */
1126 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1127 conf_add(p, &p->subs[x], x, 0);
1130 conf_del(p, &p->subs[x], x);
1133 /* If we have a slave, add him to our conference now. or DAX
1134 if this is slave native */
1135 for (x=0;x<MAX_SLAVES;x++) {
1138 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, p->channel);
1140 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1145 /* If we're supposed to be in there, do so now */
1146 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1148 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, slave->channel);
1150 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1154 /* If we have a master, add ourselves to his conference */
1156 if (isslavenative(p->master, NULL)) {
1157 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, p->master->channel);
1159 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1163 /* Nobody is left (or should be left) in our conference.
1167 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1171 static void zt_enable_ec(struct zt_pvt *p)
1176 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1179 if (p && p->echocancel) {
1180 if (p->sig == SIG_PRI) {
1182 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1184 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1187 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1189 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1192 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1195 ast_log(LOG_DEBUG, "No echocancellation requested\n");
1198 static void zt_train_ec(struct zt_pvt *p)
1202 if (p && p->echocancel && p->echotraining) {
1204 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1206 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1208 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1211 ast_log(LOG_DEBUG, "No echo training requested\n");
1214 static void zt_disable_ec(struct zt_pvt *p)
1218 if (p->echocancel) {
1220 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1222 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1224 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1229 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1236 if ((rxgain != 0.0) || (txgain != 0.0)) {
1237 /* caluculate linear value of tx gain */
1238 ltxgain = pow(10.0,txgain / 20.0);
1239 /* caluculate linear value of rx gain */
1240 lrxgain = pow(10.0,rxgain / 20.0);
1241 if (law == ZT_LAW_ALAW) {
1242 for (j=0;j<256;j++) {
1243 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1244 if (k > 32767) k = 32767;
1245 if (k < -32767) k = -32767;
1246 g.rxgain[j] = AST_LIN2A(k);
1247 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1248 if (k > 32767) k = 32767;
1249 if (k < -32767) k = -32767;
1250 g.txgain[j] = AST_LIN2A(k);
1253 for (j=0;j<256;j++) {
1254 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1255 if (k > 32767) k = 32767;
1256 if (k < -32767) k = -32767;
1257 g.rxgain[j] = AST_LIN2MU(k);
1258 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1259 if (k > 32767) k = 32767;
1260 if (k < -32767) k = -32767;
1261 g.txgain[j] = AST_LIN2MU(k);
1265 for (j=0;j<256;j++) {
1272 return(ioctl(fd,ZT_SETGAINS,&g));
1275 static inline int zt_set_hook(int fd, int hs)
1279 res = ioctl(fd, ZT_HOOK, &x);
1282 if (errno == EINPROGRESS) return 0;
1283 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1288 static inline int zt_confmute(struct zt_pvt *p, int muted)
1292 if (p->sig == SIG_PRI) {
1294 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1296 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1298 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1300 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1304 static int save_conference(struct zt_pvt *p)
1306 struct zt_confinfo c;
1308 if (p->saveconf.confmode) {
1309 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1312 p->saveconf.chan = 0;
1313 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1315 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1316 p->saveconf.confmode = 0;
1321 c.confmode = ZT_CONF_NORMAL;
1322 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1324 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1328 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1332 static int restore_conference(struct zt_pvt *p)
1335 if (p->saveconf.confmode) {
1336 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1337 p->saveconf.confmode = 0;
1339 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1344 ast_log(LOG_DEBUG, "Restored conferencing\n");
1348 static int send_callerid(struct zt_pvt *p);
1350 int send_cwcidspill(struct zt_pvt *p)
1354 p->cidspill = malloc(MAX_CALLERID_SIZE);
1356 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1357 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
1358 /* Make sure we account for the end */
1359 p->cidlen += READ_SIZE * 4;
1362 if (option_verbose > 2)
1363 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
1368 static int has_voicemail(struct zt_pvt *p)
1371 return ast_app_has_voicemail(p->mailbox);
1374 static int send_callerid(struct zt_pvt *p)
1376 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1378 /* Take out of linear mode if necessary */
1379 if (p->subs[SUB_REAL].linear) {
1380 p->subs[SUB_REAL].linear = 0;
1381 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1383 while(p->cidpos < p->cidlen) {
1384 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1386 if (errno == EAGAIN)
1389 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1399 if (p->callwaitcas) {
1400 /* Wait for CID/CW to expire */
1401 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1403 restore_conference(p);
1407 static int zt_callwait(struct ast_channel *ast)
1409 struct zt_pvt *p = ast->pvt->pvt;
1410 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1412 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1415 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1419 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1420 if (!p->callwaitrings && p->callwaitingcallerid) {
1421 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1423 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1425 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1427 p->cidlen = 2400 + READ_SIZE * 4;
1432 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1438 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1440 struct zt_pvt *p = ast->pvt->pvt;
1447 char dest[256]; /* must be same length as p->dialdest */
1448 ast_mutex_lock(&p->lock);
1449 strncpy(dest, rdest, sizeof(dest) - 1);
1450 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1451 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1452 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1453 ast_mutex_unlock(&p->lock);
1457 if (p->radio) /* if a radio channel, up immediately */
1459 /* Special pseudo -- automatically up */
1460 ast_setstate(ast, AST_STATE_UP);
1461 ast_mutex_unlock(&p->lock);
1464 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1465 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1467 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1470 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1476 if (p->owner == ast) {
1477 /* Normal ring, on hook */
1479 /* Don't send audio while on hook, until the call is answered */
1481 if (p->use_callerid) {
1482 /* Generate the Caller-ID spill if desired */
1484 ast_log(LOG_WARNING, "cidspill already exists??\n");
1487 p->cidspill = malloc(MAX_CALLERID_SIZE);
1490 p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
1494 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1496 /* Choose proper cadence */
1497 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1498 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1499 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1500 p->cidrings = cidrings[p->distinctivering - 1];
1502 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1503 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1504 p->cidrings = DEFAULT_CIDRINGS;
1508 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1509 c = strchr(dest, '/');
1512 if (c && (strlen(c) < p->stripmsd)) {
1513 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1517 p->dop.op = ZT_DIAL_OP_REPLACE;
1518 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1519 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1521 strcpy(p->dop.dialstr, "");
1524 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1525 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1526 ast_mutex_unlock(&p->lock);
1531 /* Call waiting call */
1532 p->callwaitrings = 0;
1534 strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
1536 strcpy(p->callwaitcid, "");
1537 /* Call waiting tone instead */
1538 if (zt_callwait(ast)) {
1539 ast_mutex_unlock(&p->lock);
1542 /* Make ring-back */
1543 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1544 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1548 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1550 strcpy(callerid, "");
1551 ast_callerid_parse(callerid, &n, &l);
1553 ast_shrink_phone_number(l);
1554 if (!ast_isphonenumber(l))
1558 strcpy(p->lastcallerid, l);
1560 strcpy(p->lastcallerid, "");
1561 ast_setstate(ast, AST_STATE_RINGING);
1562 index = zt_get_index(ast, p, 0);
1564 p->subs[index].needringing = 1;
1580 case SIG_SF_FEATDMF:
1582 c = strchr(dest, '/');
1587 if (strlen(c) < p->stripmsd) {
1588 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1589 ast_mutex_unlock(&p->lock);
1593 /* Start the trunk */
1594 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1596 if (errno != EINPROGRESS) {
1597 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1598 ast_mutex_unlock(&p->lock);
1602 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1603 p->dop.op = ZT_DIAL_OP_REPLACE;
1604 if (p->sig == SIG_FEATD) {
1605 if (ast->callerid) {
1606 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1607 ast_callerid_parse(callerid, &n, &l);
1609 ast_shrink_phone_number(l);
1610 if (!ast_isphonenumber(l))
1616 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1618 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1620 if (p->sig == SIG_FEATDMF) {
1621 if (ast->callerid) {
1622 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1623 ast_callerid_parse(callerid, &n, &l);
1625 ast_shrink_phone_number(l);
1626 if (!ast_isphonenumber(l))
1632 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1634 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1636 if (p->sig == SIG_E911) {
1637 strcpy(p->dop.dialstr,"M*911#");
1639 if (p->sig == SIG_FEATB) {
1640 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1642 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1643 if (strlen(p->dop.dialstr) > 4) {
1644 strcpy(p->echorest, "w");
1645 strcpy(p->echorest + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1647 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1651 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1653 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1654 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1655 ast_mutex_unlock(&p->lock);
1659 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1661 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1662 ast_setstate(ast, AST_STATE_DIALING);
1665 /* Special pseudo -- automatically up*/
1666 ast_setstate(ast, AST_STATE_UP);
1669 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1670 strcpy(p->dialdest, "");
1673 ast_log(LOG_DEBUG, "not yet implemented\n");
1674 ast_mutex_unlock(&p->lock);
1680 c = strchr(dest, '/');
1685 if (ast->callerid && !p->hidecallerid) {
1686 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1687 ast_callerid_parse(callerid, &n, &l);
1689 ast_shrink_phone_number(l);
1690 if (!ast_isphonenumber(l))
1695 if (strlen(c) < p->stripmsd) {
1696 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1697 ast_mutex_unlock(&p->lock);
1700 p->dop.op = ZT_DIAL_OP_REPLACE;
1701 s = strchr(c + p->stripmsd, 'w');
1704 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1706 strcpy(p->dop.dialstr, "");
1709 strcpy(p->dop.dialstr, "");
1711 if (pri_grab(p, p->pri)) {
1712 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1713 ast_mutex_unlock(&p->lock);
1716 if (!(p->call = pri_new_call(p->pri->pri))) {
1717 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1719 ast_mutex_unlock(&p->lock);
1722 if (!(sr = pri_sr_new())) {
1723 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1725 ast_mutex_unlock(&p->lock);
1728 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);
1729 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1730 p->bearer->call = p->call;
1732 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1733 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1734 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1735 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1737 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1738 pri_sr_set_called(sr, c + p->stripmsd, p->pri->dialplan - 1, s ? 1 : 0);
1739 pri_sr_set_caller(sr, l, n, p->pri->dialplan - 1,
1740 l ? (ast->restrictcid ? PRES_PROHIB_USER_NUMBER_PASSED_SCREEN :
1741 (p->use_callingpres ? ast->callingpres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN)) :
1742 PRES_NUMBER_NOT_AVAILABLE);
1743 if (pri_setup(p->pri->pri, p->call, sr)) {
1744 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1746 ast_mutex_unlock(&p->lock);
1751 ast_setstate(ast, AST_STATE_DIALING);
1755 ast_mutex_unlock(&p->lock);
1759 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1769 for (i = 0; i < 3; i++) {
1770 if (cur->subs[i].owner) {
1776 prev->next = cur->next;
1778 prev->next->prev = prev;
1784 iflist->prev = NULL;
1788 if (cur->subs[SUB_REAL].zfd > -1) {
1789 zt_close(cur->subs[SUB_REAL].zfd);
1795 prev->next = cur->next;
1797 prev->next->prev = prev;
1803 iflist->prev = NULL;
1807 if (cur->subs[SUB_REAL].zfd > -1) {
1808 zt_close(cur->subs[SUB_REAL].zfd);
1816 int pri_is_up(struct zt_pri *pri)
1819 for (x=0;x<NUM_DCHANS;x++) {
1820 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1826 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
1828 bearer->owner = &inuse;
1829 bearer->master = crv;
1830 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
1831 crv->bearer = bearer;
1832 crv->call = bearer->call;
1837 static char *pri_order(int level)
1847 return "Quaternary";
1853 int pri_find_dchan(struct zt_pri *pri)
1860 for(x=0;x<NUM_DCHANS;x++) {
1861 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
1863 if (pri->dchans[x] == old) {
1869 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
1870 pri->dchannels[newslot]);
1872 if (old && (oldslot != newslot))
1873 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
1874 pri->dchannels[oldslot], pri->dchannels[newslot]);
1875 pri->pri = pri->dchans[newslot];
1880 static int zt_hangup(struct ast_channel *ast)
1884 static int restore_gains(struct zt_pvt *p);
1885 struct zt_pvt *p = ast->pvt->pvt;
1886 struct zt_pvt *tmp = NULL;
1887 struct zt_pvt *prev = NULL;
1891 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1892 if (!ast->pvt->pvt) {
1893 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1897 ast_mutex_lock(&p->lock);
1899 index = zt_get_index(ast, p, 1);
1901 if (p->sig == SIG_PRI) {
1903 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1909 if (p->origcallerid) {
1910 strncpy(p->callerid, p->origcallerid, sizeof(p->callerid) - 1);
1911 free(p->origcallerid);
1912 p->origcallerid = NULL;
1915 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1917 strcpy(p->exten, "");
1919 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1920 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1924 /* Real channel, do some fixup */
1925 p->subs[index].owner = NULL;
1926 p->subs[index].needanswer = 0;
1927 p->subs[index].needringing = 0;
1928 p->subs[index].needbusy = 0;
1929 p->subs[index].needcongestion = 0;
1930 p->subs[index].linear = 0;
1931 p->subs[index].needcallerid = 0;
1932 zt_setlinear(p->subs[index].zfd, 0);
1933 if (index == SUB_REAL) {
1934 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1935 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1936 if (p->subs[SUB_CALLWAIT].inthreeway) {
1937 /* We had flipped over to answer a callwait and now it's gone */
1938 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1939 /* Move to the call-wait, but un-own us until they flip back. */
1940 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1941 unalloc_sub(p, SUB_CALLWAIT);
1944 /* The three way hung up, but we still have a call wait */
1945 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1946 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1947 unalloc_sub(p, SUB_THREEWAY);
1948 if (p->subs[SUB_REAL].inthreeway) {
1949 /* This was part of a three way call. Immediately make way for
1951 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1952 p->owner = p->subs[SUB_REAL].owner;
1954 /* This call hasn't been completed yet... Set owner to NULL */
1955 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1958 p->subs[SUB_REAL].inthreeway = 0;
1960 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
1961 /* Move to the call-wait and switch back to them. */
1962 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1963 unalloc_sub(p, SUB_CALLWAIT);
1964 p->owner = p->subs[SUB_REAL].owner;
1965 if (p->owner->_state != AST_STATE_UP)
1966 p->subs[SUB_REAL].needanswer = 1;
1967 if (p->subs[SUB_REAL].owner->bridge)
1968 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
1969 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
1970 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1971 unalloc_sub(p, SUB_THREEWAY);
1972 if (p->subs[SUB_REAL].inthreeway) {
1973 /* This was part of a three way call. Immediately make way for
1975 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1976 p->owner = p->subs[SUB_REAL].owner;
1978 /* This call hasn't been completed yet... Set owner to NULL */
1979 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1982 p->subs[SUB_REAL].inthreeway = 0;
1984 } else if (index == SUB_CALLWAIT) {
1985 /* Ditch the holding callwait call, and immediately make it availabe */
1986 if (p->subs[SUB_CALLWAIT].inthreeway) {
1987 /* This is actually part of a three way, placed on hold. Place the third part
1988 on music on hold now */
1989 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
1990 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
1991 p->subs[SUB_THREEWAY].inthreeway = 0;
1992 /* Make it the call wait now */
1993 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
1994 unalloc_sub(p, SUB_THREEWAY);
1996 unalloc_sub(p, SUB_CALLWAIT);
1997 } else if (index == SUB_THREEWAY) {
1998 if (p->subs[SUB_CALLWAIT].inthreeway) {
1999 /* The other party of the three way call is currently in a call-wait state.
2000 Start music on hold for them, and take the main guy out of the third call */
2001 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
2002 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
2003 p->subs[SUB_CALLWAIT].inthreeway = 0;
2005 p->subs[SUB_REAL].inthreeway = 0;
2006 /* If this was part of a three way call index, let us make
2007 another three way call */
2008 unalloc_sub(p, SUB_THREEWAY);
2010 /* This wasn't any sort of call, but how are we an index? */
2011 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2016 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2019 p->distinctivering = 0;
2020 p->confirmanswer = 0;
2026 p->onhooktime = time(NULL);
2032 ast_dsp_free(p->dsp);
2036 law = ZT_LAW_DEFAULT;
2037 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2039 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2040 /* Perform low level hangup if no owner left */
2043 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2044 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2045 if (!pri_grab(p, p->pri)) {
2046 if (p->alreadyhungup) {
2047 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2048 pri_hangup(p->pri->pri, p->call, -1);
2051 p->bearer->call = NULL;
2053 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2054 int icause = ast->hangupcause ? hangup_cause2pri(ast->hangupcause) : -1;
2055 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2056 p->alreadyhungup = 1;
2058 p->bearer->alreadyhungup = 1;
2061 icause = atoi(cause);
2063 pri_hangup(p->pri->pri, p->call, icause);
2066 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2069 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2074 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2081 if (p->sig == SIG_R2) {
2083 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2091 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2092 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2094 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2100 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2103 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2105 /* If they're off hook, try playing congestion */
2106 if (par.rxisoffhook)
2107 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2109 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2115 /* Make sure we're not made available for at least two seconds */
2116 time(&p->guardtime);
2120 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2127 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2128 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2132 p->callwaiting = p->permcallwaiting;
2133 p->hidecallerid = p->permhidecallerid;
2135 strcpy(p->rdnis, "");
2137 /* Restore data mode */
2138 if (p->sig == SIG_PRI) {
2140 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2144 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2145 /* Free up the bearer channel as well, and
2146 don't use its file descriptor anymore */
2147 p->bearer->owner = NULL;
2149 p->subs[SUB_REAL].zfd = -1;
2157 p->callwaitingrepeat = 0;
2159 ast->pvt->pvt = NULL;
2160 ast_mutex_unlock(&p->lock);
2161 ast_mutex_lock(&usecnt_lock);
2164 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2165 ast_mutex_unlock(&usecnt_lock);
2166 ast_update_use_count();
2167 if (option_verbose > 2)
2168 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2170 ast_mutex_lock(&iflock);
2176 destroy_channel(prev, tmp, 0);
2184 ast_mutex_unlock(&iflock);
2188 static int zt_answer(struct ast_channel *ast)
2190 struct zt_pvt *p = ast->pvt->pvt;
2193 int oldstate = ast->_state;
2194 ast_setstate(ast, AST_STATE_UP);
2195 ast_mutex_lock(&p->lock);
2196 index = zt_get_index(ast, p, 0);
2199 /* nothing to do if a radio channel */
2201 ast_mutex_unlock(&p->lock);
2220 case SIG_SF_FEATDMF:
2225 /* Pick up the line */
2226 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2227 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2228 tone_zone_play_tone(p->subs[index].zfd, -1);
2230 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2231 if (oldstate == AST_STATE_RINGING) {
2232 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2233 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2234 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2235 p->owner = p->subs[SUB_REAL].owner;
2238 if (p->sig & __ZT_SIG_FXS) {
2245 /* Send a pri acknowledge */
2246 if (!pri_grab(p, p->pri)) {
2248 res = pri_answer(p->pri->pri, p->call, 0, 1);
2251 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2258 res = mfcr2_AnswerCall(p->r2, NULL);
2260 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2264 ast_mutex_unlock(&p->lock);
2267 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2270 ast_mutex_unlock(&p->lock);
2274 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2279 struct zt_pvt *p = chan->pvt->pvt;
2282 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2283 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2289 if ((!cp) || (datalen < 1))
2295 case AST_OPTION_TONE_VERIFY:
2300 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2301 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2304 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2305 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2308 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2309 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2313 case AST_OPTION_TDD: /* turn on or off TDD */
2314 if (!*cp) { /* turn it off */
2315 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2316 if (p->tdd) tdd_free(p->tdd);
2322 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2323 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2326 /* otherwise, turn it on */
2327 if (!p->didtdd) { /* if havent done it yet */
2328 unsigned char mybuf[41000],*buf;
2329 int size,res,fd,len;
2331 struct pollfd fds[1];
2333 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2334 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2336 index = zt_get_index(chan, p, 0);
2338 ast_log(LOG_WARNING, "No index in TDD?\n");
2341 fd = p->subs[index].zfd;
2343 if (ast_check_hangup(chan)) return -1;
2345 if (size > READ_SIZE)
2348 fds[0].events = POLLPRI | POLLOUT;
2349 res = poll(fds, 1, -1);
2351 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2354 /* if got exception */
2355 if (fds[0].revents & POLLPRI) return -1;
2356 if (!(fds[0].revents & POLLOUT)) {
2357 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2360 res = write(fd, buf, size);
2362 if (res == -1) return -1;
2363 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2369 p->didtdd = 1; /* set to have done it now */
2371 if (*cp == 2) { /* Mate mode */
2372 if (p->tdd) tdd_free(p->tdd);
2377 if (!p->tdd) { /* if we dont have one yet */
2378 p->tdd = tdd_new(); /* allocate one */
2381 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2384 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2389 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2392 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2394 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2397 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2403 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2406 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2407 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2414 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2416 /* Unlink a specific slave or all slaves/masters from a given master */
2422 ast_mutex_lock(&master->lock);
2424 while(ast_mutex_trylock(&slave->lock)) {
2425 ast_mutex_unlock(&master->lock);
2427 ast_mutex_lock(&master->lock);
2432 for (x=0;x<MAX_SLAVES;x++) {
2433 if (master->slaves[x]) {
2434 if (!slave || (master->slaves[x] == slave)) {
2435 /* Take slave out of the conference */
2436 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2437 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2438 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2439 master->slaves[x]->master = NULL;
2440 master->slaves[x] = NULL;
2445 master->inconference = 0;
2448 if (master->master) {
2449 /* Take master out of the conference */
2450 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2451 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2453 for (x=0;x<MAX_SLAVES;x++) {
2454 if (master->master->slaves[x] == master)
2455 master->master->slaves[x] = NULL;
2456 else if (master->master->slaves[x])
2460 master->master->inconference = 0;
2462 master->master = NULL;
2464 update_conf(master);
2467 ast_mutex_unlock(&slave->lock);
2468 ast_mutex_unlock(&master->lock);
2472 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2474 if (!slave || !master) {
2475 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2478 for (x=0;x<MAX_SLAVES;x++) {
2479 if (!master->slaves[x]) {
2480 master->slaves[x] = slave;
2484 if (x >= MAX_SLAVES) {
2485 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2486 master->slaves[MAX_SLAVES - 1] = slave;
2489 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2490 slave->master = master;
2492 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2495 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2497 struct ast_channel *who = NULL, *cs[3];
2498 struct zt_pvt *p0, *p1, *op0, *op1;
2499 struct zt_pvt *master=NULL, *slave=NULL;
2500 struct ast_frame *f;
2505 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2506 int os1 = -1, os2 = -1;
2507 struct ast_channel *oc1, *oc2;
2509 /* if need DTMF, cant native bridge */
2510 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2513 ast_mutex_lock(&c0->lock);
2514 ast_mutex_lock(&c1->lock);
2518 /* cant do pseudo-channels here */
2519 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2520 ast_mutex_unlock(&c0->lock);
2521 ast_mutex_unlock(&c1->lock);
2525 op0 = p0 = c0->pvt->pvt;
2526 op1 = p1 = c1->pvt->pvt;
2529 oi1 = zt_get_index(c0, p0, 0);
2530 oi2 = zt_get_index(c1, p1, 0);
2533 if ((oi1 < 0) || (oi2 < 0)) {
2534 ast_mutex_unlock(&c0->lock);
2535 ast_mutex_unlock(&c1->lock);
2541 ast_mutex_lock(&p0->lock);
2542 if (ast_mutex_trylock(&p1->lock)) {
2543 /* Don't block, due to potential for deadlock */
2544 ast_mutex_unlock(&p0->lock);
2545 ast_mutex_unlock(&c0->lock);
2546 ast_mutex_unlock(&c1->lock);
2547 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2550 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2551 if (!p0->owner || !p1->owner) {
2552 /* Currently unowned -- Do nothing. */
2555 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2556 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2560 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2565 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2566 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,
2567 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2570 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2571 if (p1->subs[SUB_THREEWAY].inthreeway) {
2577 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2578 if (p0->subs[SUB_THREEWAY].inthreeway) {
2584 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2585 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2586 don't put us in anything */
2587 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2593 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2594 /* Same as previous */
2595 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2602 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2603 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2604 if (master && slave) {
2605 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2606 in an active threeway call with a channel that is ringing, we should
2607 indicate ringing. */
2608 if ((oi2 == SUB_THREEWAY) &&
2609 p1->subs[SUB_THREEWAY].inthreeway &&
2610 p1->subs[SUB_REAL].owner &&
2611 p1->subs[SUB_REAL].inthreeway &&
2612 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2613 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2614 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2615 os2 = p1->subs[SUB_REAL].owner->_state;
2617 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2618 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2620 if ((oi1 == SUB_THREEWAY) &&
2621 p0->subs[SUB_THREEWAY].inthreeway &&
2622 p0->subs[SUB_REAL].owner &&
2623 p0->subs[SUB_REAL].inthreeway &&
2624 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2625 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2626 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2627 os1 = p0->subs[SUB_REAL].owner->_state;
2629 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2630 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2632 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2633 if (!p0->echocanbridged || !p1->echocanbridged) {
2634 /* Disable echo cancellation if appropriate */
2639 zt_link(slave, master);
2640 master->inconference = inconf;
2641 } else if (!nothingok)
2642 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2646 t1 = p0->subs[SUB_REAL].inthreeway;
2647 t2 = p1->subs[SUB_REAL].inthreeway;
2649 ast_mutex_unlock(&p0->lock);
2650 ast_mutex_unlock(&p1->lock);
2652 ast_mutex_unlock(&c0->lock);
2653 ast_mutex_unlock(&c1->lock);
2655 /* Native bridge failed */
2656 if ((!master || !slave) && !nothingok) {
2668 /* Here's our main loop... Start by locking things, looking for private parts,
2669 and then balking if anything is wrong */
2670 ast_mutex_lock(&c0->lock);
2671 ast_mutex_lock(&c1->lock);
2675 i1 = zt_get_index(c0, p0, 1);
2677 i2 = zt_get_index(c1, p1, 1);
2678 ast_mutex_unlock(&c0->lock);
2679 ast_mutex_unlock(&c1->lock);
2680 if ((op0 != p0) || (op1 != p1) ||
2681 (ofd1 != c0->fds[0]) ||
2682 (ofd2 != c1->fds[0]) ||
2683 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2684 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2685 (oc1 != p0->owner) ||
2686 (oc2 != p1->owner) ||
2687 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2688 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2691 if (slave && master)
2692 zt_unlink(slave, master, 1);
2693 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2694 op0->channel, oi1, op1->channel, oi2);
2702 who = ast_waitfor_n(cs, 2, &to);
2704 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2707 if (who->pvt->pvt == op0)
2708 op0->ignoredtmf = 1;
2709 else if (who->pvt->pvt == op1)
2710 op1->ignoredtmf = 1;
2712 if (who->pvt->pvt == op0)
2713 op0->ignoredtmf = 0;
2714 else if (who->pvt->pvt == op1)
2715 op1->ignoredtmf = 0;
2719 if (slave && master)
2720 zt_unlink(slave, master, 1);
2727 if (f->frametype == AST_FRAME_DTMF) {
2728 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2729 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2732 if (slave && master)
2733 zt_unlink(slave, master, 1);
2735 } else if ((who == c0) && p0->pulsedial) {
2737 } else if ((who == c1) && p1->pulsedial) {
2743 /* Swap who gets priority */
2750 static int zt_indicate(struct ast_channel *chan, int condition);
2752 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2754 struct zt_pvt *p = newchan->pvt->pvt;
2756 ast_mutex_lock(&p->lock);
2757 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2758 if (p->owner == oldchan) {
2762 if (p->subs[x].owner == oldchan) {
2764 zt_unlink(NULL, p, 0);
2765 p->subs[x].owner = newchan;
2767 if (newchan->_state == AST_STATE_RINGING)
2768 zt_indicate(newchan, AST_CONTROL_RINGING);
2770 ast_mutex_unlock(&p->lock);
2774 static int zt_ring_phone(struct zt_pvt *p)
2778 /* Make sure our transmit state is on hook */
2781 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2784 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2786 printf("Res: %d, error: %s\n", res, strerror(errno));
2792 /* Wait just in case */
2799 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2807 static void *ss_thread(void *data);
2809 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2811 static int attempt_transfer(struct zt_pvt *p)
2813 /* In order to transfer, we need at least one of the channels to
2814 actually be in a call bridge. We can't conference two applications
2815 together (but then, why would we want to?) */
2816 if (p->subs[SUB_REAL].owner->bridge) {
2817 /* The three-way person we're about to transfer to could still be in MOH, so
2818 stop if now if appropriate */
2819 if (p->subs[SUB_THREEWAY].owner->bridge)
2820 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2821 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2822 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2824 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2825 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2826 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2829 /* Orphan the channel */
2830 unalloc_sub(p, SUB_THREEWAY);
2831 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2832 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2833 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2835 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2836 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2837 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2838 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2841 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2842 unalloc_sub(p, SUB_THREEWAY);
2843 /* Tell the caller not to hangup */
2846 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2847 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2848 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2854 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2856 struct ast_frame *f;
2857 f = &p->subs[index].f;
2859 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2863 case MFCR2_EVENT_BLOCKED:
2864 if (option_verbose > 2)
2865 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2867 case MFCR2_EVENT_UNBLOCKED:
2868 if (option_verbose > 2)
2869 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2871 case MFCR2_EVENT_CONFIG_ERR:
2872 if (option_verbose > 2)
2873 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2875 case MFCR2_EVENT_RING:
2876 if (option_verbose > 2)
2877 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2879 case MFCR2_EVENT_HANGUP:
2880 if (option_verbose > 2)
2881 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2883 case MFCR2_EVENT_RINGING:
2884 if (option_verbose > 2)
2885 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2887 case MFCR2_EVENT_ANSWER:
2888 if (option_verbose > 2)
2889 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2891 case MFCR2_EVENT_HANGUP_ACK:
2892 if (option_verbose > 2)
2893 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2895 case MFCR2_EVENT_IDLE:
2896 if (option_verbose > 2)
2897 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2900 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2906 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2911 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2913 ast_log(LOG_WARNING, "Unable to check received bits\n");
2917 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2920 e = mfcr2_cas_signaling_event(p->r2, x);
2925 static int check_for_conference(struct zt_pvt *p)
2928 /* Fine if we already have a master, etc */
2929 if (p->master || (p->confno > -1))
2931 memset(&ci, 0, sizeof(ci));
2932 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2933 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2936 /* If we have no master and don't have a confno, then
2937 if we're in a conference, it's probably a MeetMe room or
2938 some such, so don't let us 3-way out! */
2939 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
2940 if (option_verbose > 2)
2941 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
2947 static int get_alarms(struct zt_pvt *p)
2951 memset(&zi, 0, sizeof(zi));
2952 zi.spanno = p->span;
2953 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
2955 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
2961 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
2966 struct zt_pvt *p = ast->pvt->pvt;
2968 pthread_attr_t attr;
2969 struct ast_channel *chan;
2970 pthread_attr_init(&attr);
2971 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2972 index = zt_get_index(ast, p, 0);
2973 p->subs[index].f.frametype = AST_FRAME_NULL;
2974 p->subs[index].f.datalen = 0;
2975 p->subs[index].f.samples = 0;
2976 p->subs[index].f.mallocd = 0;
2977 p->subs[index].f.offset = 0;
2978 p->subs[index].f.src = "zt_handle_event";
2979 p->subs[index].f.data = NULL;
2981 return &p->subs[index].f;
2982 if (p->fake_event) {
2983 res = p->fake_event;
2986 res = zt_get_event(p->subs[index].zfd);
2987 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
2988 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
2989 if (res & ZT_EVENT_PULSEDIGIT)
2993 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);