3 * Asterisk -- A telephony toolkit for Linux.
5 * Zaptel Pseudo TDM interface
7 * Copyright (C) 2003 Digium
9 * Mark Spencer <markster@digium.com>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/channel_pvt.h>
20 #include <asterisk/config.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/module.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/options.h>
25 #include <asterisk/file.h>
26 #include <asterisk/ulaw.h>
27 #include <asterisk/alaw.h>
28 #include <asterisk/callerid.h>
29 #include <asterisk/adsi.h>
30 #include <asterisk/cli.h>
31 #include <asterisk/cdr.h>
32 #include <asterisk/parking.h>
33 #include <asterisk/musiconhold.h>
34 #include <asterisk/say.h>
35 #include <asterisk/tdd.h>
36 #include <asterisk/app.h>
37 #include <asterisk/dsp.h>
38 #include <asterisk/astdb.h>
39 #include <asterisk/manager.h>
40 #include <asterisk/causes.h>
41 #include <asterisk/term.h>
42 #include <sys/signal.h>
47 #include <sys/ioctl.h>
48 #include <linux/zaptel.h>
59 #include "../asterisk.h"
62 #error "Your zaptel is too old. please cvs update"
66 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
67 * the user hangs up to reset the state machine so ring works properly.
68 * This is used to be able to support kewlstart by putting the zhone in
69 * groundstart mode since their forward disconnect supervision is entirely
70 * broken even though their documentation says it isn't and their support
71 * is entirely unwilling to provide any assistance with their channel banks
72 * even though their web site says they support their products for life.
75 /* #define ZHONE_HACK */
77 /* Typically, how many rings before we should send Caller*ID */
78 #define DEFAULT_CIDRINGS 1
80 #define CHANNEL_PSEUDO -12
82 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
84 static char *desc = "Zapata Telephony"
93 static char *tdesc = "Zapata Telephony Driver"
102 static char *type = "Zap";
103 static char *typecompat = "Tor"; /* Retain compatibility with chan_tor */
104 static char *config = "zapata.conf";
106 #define SIG_EM ZT_SIG_EM
107 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
108 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
109 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
110 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
111 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
112 #define SIG_FXSLS ZT_SIG_FXSLS
113 #define SIG_FXSGS ZT_SIG_FXSGS
114 #define SIG_FXSKS ZT_SIG_FXSKS
115 #define SIG_FXOLS ZT_SIG_FXOLS
116 #define SIG_FXOGS ZT_SIG_FXOGS
117 #define SIG_FXOKS ZT_SIG_FXOKS
118 #define SIG_PRI ZT_SIG_CLEAR
119 #define SIG_R2 ZT_SIG_CAS
120 #define SIG_SF ZT_SIG_SF
121 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
122 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
123 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
124 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
125 #define SIG_EM_E1 ZT_SIG_EM_E1
128 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
130 #define CHAN_PSEUDO -2
132 static char context[AST_MAX_EXTENSION] = "default";
133 static char callerid[256] = "";
135 static char language[MAX_LANGUAGE] = "";
136 static char musicclass[MAX_LANGUAGE] = "";
137 static char progzone[10]= "";
139 static int usedistinctiveringdetection = 0;
141 static int use_callerid = 1;
142 static int zaptrcallerid = 0;
143 static int cur_signalling = -1;
145 static unsigned int cur_group = 0;
146 static unsigned int cur_callergroup = 0;
147 static unsigned int cur_pickupgroup = 0;
148 static int relaxdtmf = 0;
150 static int immediate = 0;
152 static int stripmsd = 0;
154 static int callwaiting = 0;
156 static int callwaitingcallerid = 0;
158 static int hidecallerid = 0;
160 static int restrictcid = 0;
162 static int use_callingpres = 0;
164 static int callreturn = 0;
166 static int threewaycalling = 0;
168 static int transfer = 0;
170 static int cancallforward = 0;
172 static float rxgain = 0.0;
174 static float txgain = 0.0;
176 static int tonezone = -1;
178 static int echocancel;
180 static int echotraining;
182 static int echocanbridged = 0;
184 static int busydetect = 0;
186 static int busycount = 3;
188 static int callprogress = 0;
190 static char accountcode[20] = "";
192 static char mailbox[AST_MAX_EXTENSION];
194 static int amaflags = 0;
198 static int numbufs = 4;
200 static int cur_prewink = -1;
201 static int cur_preflash = -1;
202 static int cur_wink = -1;
203 static int cur_flash = -1;
204 static int cur_start = -1;
205 static int cur_rxwink = -1;
206 static int cur_rxflash = -1;
207 static int cur_debounce = -1;
210 static int minunused = 2;
211 static int minidle = 0;
212 static char idleext[AST_MAX_EXTENSION];
213 static char idledial[AST_MAX_EXTENSION];
214 static int overlapdial = 0;
217 /* Wait up to 16 seconds for first digit (FXO logic) */
218 static int firstdigittimeout = 16000;
220 /* How long to wait for following digits (FXO logic) */
221 static int gendigittimeout = 8000;
223 /* How long to wait for an extra digit, if there is an ambiguous match */
224 static int matchdigittimeout = 3000;
226 static int usecnt =0;
227 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
229 /* Protect the interface list (of zt_pvt's) */
230 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
232 static int ifcount = 0;
234 /* Protect the monitoring thread, so only one process can kill or start it, and not
235 when it's doing something critical. */
236 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
238 /* This is the thread for the monitor which checks for input on the channels
239 which are not currently in use. */
240 static pthread_t monitor_thread = AST_PTHREADT_NULL;
242 static int restart_monitor(void);
244 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
246 static int zt_sendtext(struct ast_channel *c, char *text);
248 static inline int zt_get_event(int fd)
250 /* Avoid the silly zt_getevent which ignores a bunch of events */
252 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
256 static inline int zt_wait_event(int fd)
258 /* Avoid the silly zt_waitevent which ignores a bunch of events */
260 i = ZT_IOMUX_SIGEVENT;
261 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
262 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
266 /* Chunk size to read -- we use 20ms chunks to make things happy. */
267 #define READ_SIZE 160
269 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
270 #define MASK_INUSE (1 << 1) /* Channel currently in use */
272 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
273 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
274 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
275 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
276 #define RINGT ( (8000 * 8) / READ_SIZE)
282 static int r2prot = -1;
288 pthread_t master; /* Thread of master */
289 ast_mutex_t lock; /* Mutex */
290 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
291 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
292 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
293 int minunused; /* Min # of channels to keep empty */
294 int minidle; /* Min # of "idling" calls to keep active */
295 int nodetype; /* Node type */
296 int switchtype; /* Type of switch to emulate */
297 int dialplan; /* Dialing plan */
298 int dchannel; /* What channel the dchannel is on */
299 int channels; /* Num of chans in span (31 or 24) */
300 int overlapdial; /* In overlap dialing mode */
307 int chanmask[32]; /* Channel status */
311 struct zt_pvt *pvt[32]; /* Member channel pvt structs */
312 struct zt_channel *chan[32]; /* Channels on each line */
316 static struct zt_pri pris[NUM_SPANS];
318 static int pritype = PRI_CPE;
321 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
323 #define DEFAULT_PRI_DEBUG 0
326 static inline void pri_rel(struct zt_pri *pri)
328 ast_mutex_unlock(&pri->lock);
331 static int switchtype = PRI_SWITCH_NI2;
332 static int dialplan = PRI_NATIONAL_ISDN + 1;
336 #define SUB_REAL 0 /* Active call */
337 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
338 #define SUB_THREEWAY 2 /* Three-way call */
341 static struct zt_distRings drings;
343 struct distRingData {
346 struct ringContextData {
347 char contextData[AST_MAX_EXTENSION];
349 struct zt_distRings {
350 struct distRingData ringnum[3];
351 struct ringContextData ringContext[3];
354 static char *subnames[] = {
360 struct zt_subchannel {
362 struct ast_channel *owner;
364 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
365 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
376 #define CONF_USER_REAL (1 << 0)
377 #define CONF_USER_THIRDCALL (1 << 1)
381 static struct zt_pvt {
383 struct ast_channel *owner; /* Our current active owner (if applicable) */
384 /* Up to three channels can be associated with this call */
386 struct zt_subchannel sub_unused; /* Just a safety precaution */
387 struct zt_subchannel subs[3]; /* Sub-channels */
388 struct zt_confinfo saveconf; /* Saved conference info */
390 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
391 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
392 int inconference; /* If our real should be in the conference */
394 int sig; /* Signalling style */
395 int radio; /* radio type */
396 int firstradio; /* first radio flag */
399 int tonezone; /* tone zone for this chan, or -1 for default */
400 struct zt_pvt *next; /* Next channel in list */
401 struct zt_pvt *prev; /* Prev channel in list */
403 struct zt_distRings drings;
404 int usedistinctiveringdetection;
406 char context[AST_MAX_EXTENSION];
407 char defcontext[AST_MAX_EXTENSION];
408 char exten[AST_MAX_EXTENSION];
409 char language[MAX_LANGUAGE];
410 char musicclass[MAX_LANGUAGE];
411 char callerid[AST_MAX_EXTENSION];
412 char lastcallerid[AST_MAX_EXTENSION];
413 char *origcallerid; /* malloced original callerid */
414 char callwaitcid[AST_MAX_EXTENSION];
415 char rdnis[AST_MAX_EXTENSION];
416 char dnid[AST_MAX_EXTENSION];
419 int confno; /* Our conference */
420 int confusers; /* Who is using our conference */
421 int propconfno; /* Propagated conference number */
422 unsigned int callgroup;
423 unsigned int pickupgroup;
424 int immediate; /* Answer before getting digits? */
425 int channel; /* Channel Number */
426 int span; /* Span number */
429 int use_callerid; /* Whether or not to use caller id on this channel */
432 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
433 int restrictcid; /* Whether restrict the callerid -> only send ANI */
434 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
435 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
436 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
437 int cidcwexpire; /* When to expire our muting for CID/CW */
438 unsigned char *cidspill;
453 int callwaitingcallerid;
462 struct timeval flashtime; /* Last flash-hook time */
464 int cref; /* Call reference number */
465 ZT_DIAL_OPERATION dop;
469 char accountcode[20]; /* Account code */
470 int amaflags; /* AMA Flags */
471 char didtdd; /* flag to say its done it once */
472 struct tdd_state *tdd; /* TDD flag */
475 char call_forward[AST_MAX_EXTENSION];
476 char mailbox[AST_MAX_EXTENSION];
481 int confirmanswer; /* Wait for '#' to confirm answer */
482 int distinctivering; /* Which distinctivering to use */
483 int cidrings; /* Which ring to deliver CID on */
485 int faxhandled; /* Has a fax tone already been handled? */
487 char mate; /* flag to say its in MATE mode */
488 int pulsedial; /* whether a pulse dial phone is detected */
489 int dtmfrelax; /* whether to run in relaxed DTMF mode */
491 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
499 #ifdef PRI_EVENT_PROCEEDING
502 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
511 } *iflist = NULL, *ifend = NULL;
513 struct zt_pvt *round_robin[32];
516 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
519 /* Grab the lock first */
521 res = ast_mutex_trylock(&pri->lock);
523 ast_mutex_unlock(&pvt->lock);
524 /* Release the lock and try again */
526 ast_mutex_lock(&pvt->lock);
529 /* Then break the poll */
530 pthread_kill(pri->master, SIGURG);
535 #define NUM_CADENCE_MAX 25
536 static int num_cadence = 4;
537 static int user_has_defined_cadences = 0;
539 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
540 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
541 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
542 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
543 { { 1000, 500, 2500, 5000 } }, /* Long ring */
546 int receivedRingT; /* Used to find out what ringtone we are on */
548 /* cidrings says in which pause to transmit the cid information, where the first pause
549 * is 1, the second pause is 2 and so on.
552 static int cidrings[NUM_CADENCE_MAX] = {
553 2, /* Right after first long ring */
554 4, /* Right after long part */
555 3, /* After third chirp */
556 2, /* Second spell */
559 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
560 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
562 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
563 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
566 /* translate between PRI causes and asterisk's */
567 static int hangup_pri2cause(int cause)
570 case PRI_CAUSE_USER_BUSY:
571 return AST_CAUSE_BUSY;
572 case PRI_CAUSE_NORMAL_CLEARING:
573 return AST_CAUSE_NORMAL;
574 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
575 case PRI_CAUSE_REQUESTED_CHAN_UNAVAIL:
576 return AST_CAUSE_CONGESTION;
577 case PRI_CAUSE_UNALLOCATED:
578 case PRI_CAUSE_NUMBER_CHANGED:
579 return AST_CAUSE_UNALLOCATED;
581 return AST_CAUSE_FAILURE;
587 /* translate between ast cause and PRI */
588 static int hangup_cause2pri(int cause)
592 return PRI_CAUSE_USER_BUSY;
593 case AST_CAUSE_NORMAL:
595 return PRI_CAUSE_NORMAL_CLEARING;
602 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
605 if (p->subs[0].owner == ast)
607 else if (p->subs[1].owner == ast)
609 else if (p->subs[2].owner == ast)
614 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
619 static void wakeup_sub(struct zt_pvt *p, int a)
621 struct ast_frame null = { AST_FRAME_NULL, };
623 if (p->subs[a].owner) {
624 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
625 ast_mutex_unlock(&p->lock);
627 ast_mutex_lock(&p->lock);
629 ast_queue_frame(p->subs[a].owner, &null);
630 ast_mutex_unlock(&p->subs[a].owner->lock);
638 static void swap_subs(struct zt_pvt *p, int a, int b)
642 struct ast_channel *towner;
644 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
646 tchan = p->subs[a].chan;
647 towner = p->subs[a].owner;
648 tinthreeway = p->subs[a].inthreeway;
650 p->subs[a].chan = p->subs[b].chan;
651 p->subs[a].owner = p->subs[b].owner;
652 p->subs[a].inthreeway = p->subs[b].inthreeway;
654 p->subs[b].chan = tchan;
655 p->subs[b].owner = towner;
656 p->subs[b].inthreeway = tinthreeway;
658 if (p->subs[a].owner)
659 p->subs[a].owner->fds[0] = p->subs[a].zfd;
660 if (p->subs[b].owner)
661 p->subs[b].owner->fds[0] = p->subs[b].zfd;
666 static int zt_open(char *fn)
674 for (x=0;x<strlen(fn);x++) {
675 if (!isdigit(fn[x])) {
683 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
686 fn = "/dev/zap/channel";
688 fd = open(fn, O_RDWR | O_NONBLOCK);
690 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
694 if (ioctl(fd, ZT_SPECIFY, &chan)) {
698 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
703 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
707 static void zt_close(int fd)
712 int zt_setlinear(int zfd, int linear)
715 res = ioctl(zfd, ZT_SETLINEAR, &linear);
722 int zt_setlaw(int zfd, int law)
725 res = ioctl(zfd, ZT_SETLAW, &law);
731 static int alloc_sub(struct zt_pvt *p, int x)
735 if (p->subs[x].zfd < 0) {
736 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
737 if (p->subs[x].zfd > -1) {
738 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
740 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
741 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
742 bi.numbufs = numbufs;
743 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
745 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
748 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
749 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
750 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
751 zt_close(p->subs[x].zfd);
756 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
759 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
762 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
766 static int unalloc_sub(struct zt_pvt *p, int x)
769 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
772 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
773 if (p->subs[x].zfd > -1) {
774 zt_close(p->subs[x].zfd);
777 p->subs[x].linear = 0;
779 p->subs[x].owner = NULL;
780 p->subs[x].inthreeway = 0;
781 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
785 static int zt_digit(struct ast_channel *ast, char digit)
787 ZT_DIAL_OPERATION zo;
792 index = zt_get_index(ast, p, 0);
793 if (index == SUB_REAL) {
795 #ifdef PRI_EVENT_SETUP_ACK
796 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && p->setup_ack && !p->proceeding) {
798 #ifdef PRI_EVENT_PROCEEDING
799 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && !p->proceeding) {
801 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING) {
804 if (!pri_grab(p, p->pri))
805 pri_information(p->pri->pri,p->call,digit);
807 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
813 zo.op = ZT_DIAL_OP_APPEND;
815 zo.dialstr[1] = digit;
817 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
818 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
827 static char *events[] = {
840 "Hook Transition Complete",
849 { ZT_ALARM_RED, "Red Alarm" },
850 { ZT_ALARM_YELLOW, "Yellow Alarm" },
851 { ZT_ALARM_BLUE, "Blue Alarm" },
852 { ZT_ALARM_RECOVER, "Recovering" },
853 { ZT_ALARM_LOOPBACK, "Loopback" },
854 { ZT_ALARM_NOTOPEN, "Not Open" },
855 { ZT_ALARM_NONE, "None" },
858 static char *alarm2str(int alarm)
861 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
862 if (alarms[x].alarm & alarm)
863 return alarms[x].name;
865 return alarm ? "Unknown Alarm" : "No Alarm";
868 static char *event2str(int event)
870 static char buf[256];
871 if ((event < 15) && (event > -1))
872 return events[event];
873 sprintf(buf, "Event %d", event);
878 static int str2r2prot(char *swtype)
880 if (!strcasecmp(swtype, "ar"))
881 return MFCR2_PROT_ARGENTINA;
883 if (!strcasecmp(swtype, "cn"))
884 return MFCR2_PROT_CHINA;
886 if (!strcasecmp(swtype, "kr"))
887 return MFCR2_PROT_KOREA;
893 static char *sig2str(int sig)
895 static char buf[256];
898 return "E & M Immediate";
904 return "Feature Group D (DTMF)";
906 return "Feature Group D (MF)";
908 return "Feature Group B (MF)";
912 return "FXS Loopstart";
914 return "FXS Groundstart";
916 return "FXS Kewlstart";
918 return "FXO Loopstart";
920 return "FXO Groundstart";
922 return "FXO Kewlstart";
924 return "PRI Signalling";
926 return "R2 Signalling";
928 return "SF (Tone) Signalling Immediate";
930 return "SF (Tone) Signalling Wink";
932 return "SF (Tone) Signalling with Feature Group D (DTMF)";
934 return "SF (Tone) Signalling with Feature Group D (MF)";
936 return "SF (Tone) Signalling with Feature Group B (MF)";
938 return "Pseudo Signalling";
940 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
945 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
947 /* If the conference already exists, and we're already in it
948 don't bother doing anything */
951 memset(&zi, 0, sizeof(zi));
954 if (slavechannel > 0) {
955 /* If we have only one slave, do a digital mon */
956 zi.confmode = ZT_CONF_DIGITALMON;
957 zi.confno = slavechannel;
960 /* Real-side and pseudo-side both participate in conference */
961 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
962 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
964 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
965 zi.confno = p->confno;
967 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
971 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
972 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
975 if (slavechannel < 1) {
976 p->confno = zi.confno;
978 memcpy(&c->curconf, &zi, sizeof(c->curconf));
979 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
983 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
985 /* If they're listening to our channel, they're ours */
986 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
988 /* If they're a talker on our (allocated) conference, they're ours */
989 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
994 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
997 if (/* Can't delete if there's no zfd */
999 /* Don't delete from the conference if it's not our conference */
1001 /* Don't delete if we don't think it's conferenced at all (implied) */
1003 memset(&zi, 0, sizeof(zi));
1007 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1008 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1011 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1012 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1016 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1020 struct zt_pvt *slave = NULL;
1021 /* Start out optimistic */
1023 /* Update conference state in a stateless fashion */
1025 /* Any three-way calling makes slave native mode *definitely* out
1027 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1030 /* If we don't have any 3-way calls, check to see if we have
1031 precisely one slave */
1032 if (useslavenative) {
1033 for (x=0;x<MAX_SLAVES;x++) {
1036 /* Whoops already have a slave! No
1037 slave native and stop right away */
1042 /* We have one slave so far */
1043 slave = p->slaves[x];
1048 /* If no slave, slave native definitely out */
1051 else if (slave->law != p->law) {
1057 return useslavenative;
1060 static int update_conf(struct zt_pvt *p)
1065 struct zt_pvt *slave = NULL;
1067 useslavenative = isslavenative(p, &slave);
1068 /* Start with the obvious, general stuff */
1070 /* Look for three way calls */
1071 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1072 conf_add(p, &p->subs[x], x, 0);
1075 conf_del(p, &p->subs[x], x);
1078 /* If we have a slave, add him to our conference now. or DAX
1079 if this is slave native */
1080 for (x=0;x<MAX_SLAVES;x++) {
1083 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, p->channel);
1085 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1090 /* If we're supposed to be in there, do so now */
1091 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1093 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, slave->channel);
1095 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1099 /* If we have a master, add ourselves to his conference */
1101 if (isslavenative(p->master, NULL)) {
1102 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, p->master->channel);
1104 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1108 /* Nobody is left (or should be left) in our conference.
1112 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1116 static void zt_enable_ec(struct zt_pvt *p)
1121 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1124 if (p && p->echocancel) {
1125 if (p->sig == SIG_PRI) {
1127 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1129 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1132 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1134 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1137 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1140 ast_log(LOG_DEBUG, "No echocancellation requested\n");
1143 static void zt_train_ec(struct zt_pvt *p)
1147 if (p && p->echocancel && p->echotraining) {
1149 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1151 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1153 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1156 ast_log(LOG_DEBUG, "No echo training requested\n");
1159 static void zt_disable_ec(struct zt_pvt *p)
1163 if (p->echocancel) {
1165 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1167 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1169 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1174 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1181 if ((rxgain != 0.0) || (txgain != 0.0)) {
1182 /* caluculate linear value of tx gain */
1183 ltxgain = pow(10.0,txgain / 20.0);
1184 /* caluculate linear value of rx gain */
1185 lrxgain = pow(10.0,rxgain / 20.0);
1186 if (law == ZT_LAW_ALAW) {
1187 for (j=0;j<256;j++) {
1188 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1189 if (k > 32767) k = 32767;
1190 if (k < -32767) k = -32767;
1191 g.rxgain[j] = AST_LIN2A(k);
1192 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1193 if (k > 32767) k = 32767;
1194 if (k < -32767) k = -32767;
1195 g.txgain[j] = AST_LIN2A(k);
1198 for (j=0;j<256;j++) {
1199 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1200 if (k > 32767) k = 32767;
1201 if (k < -32767) k = -32767;
1202 g.rxgain[j] = AST_LIN2MU(k);
1203 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1204 if (k > 32767) k = 32767;
1205 if (k < -32767) k = -32767;
1206 g.txgain[j] = AST_LIN2MU(k);
1210 for (j=0;j<256;j++) {
1217 return(ioctl(fd,ZT_SETGAINS,&g));
1220 static inline int zt_set_hook(int fd, int hs)
1224 res = ioctl(fd, ZT_HOOK, &x);
1227 if (errno == EINPROGRESS) return 0;
1228 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1233 static inline int zt_confmute(struct zt_pvt *p, int muted)
1237 if (p->sig == SIG_PRI) {
1239 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1241 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1243 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1245 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1249 static int save_conference(struct zt_pvt *p)
1251 struct zt_confinfo c;
1253 if (p->saveconf.confmode) {
1254 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1257 p->saveconf.chan = 0;
1258 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1260 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1261 p->saveconf.confmode = 0;
1266 c.confmode = ZT_CONF_NORMAL;
1267 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1269 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1273 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1277 static int restore_conference(struct zt_pvt *p)
1280 if (p->saveconf.confmode) {
1281 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1282 p->saveconf.confmode = 0;
1284 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1289 ast_log(LOG_DEBUG, "Restored conferencing\n");
1293 static int send_callerid(struct zt_pvt *p);
1295 int send_cwcidspill(struct zt_pvt *p)
1299 p->cidspill = malloc(MAX_CALLERID_SIZE);
1301 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1302 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
1303 /* Make sure we account for the end */
1304 p->cidlen += READ_SIZE * 4;
1307 if (option_verbose > 2)
1308 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
1313 static int has_voicemail(struct zt_pvt *p)
1316 return ast_app_has_voicemail(p->mailbox);
1319 static int send_callerid(struct zt_pvt *p)
1321 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1323 /* Take out of linear mode if necessary */
1324 if (p->subs[SUB_REAL].linear) {
1325 p->subs[SUB_REAL].linear = 0;
1326 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1328 while(p->cidpos < p->cidlen) {
1329 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1331 if (errno == EAGAIN)
1334 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1344 if (p->callwaitcas) {
1345 /* Wait for CID/CW to expire */
1346 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1348 restore_conference(p);
1352 static int zt_callwait(struct ast_channel *ast)
1354 struct zt_pvt *p = ast->pvt->pvt;
1355 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1357 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1360 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1364 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1365 if (!p->callwaitrings && p->callwaitingcallerid) {
1366 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1368 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1370 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1372 p->cidlen = 2400 + READ_SIZE * 4;
1377 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1383 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1385 struct zt_pvt *p = ast->pvt->pvt;
1392 char dest[256]; /* must be same length as p->dialdest */
1393 strncpy(dest, rdest, sizeof(dest) - 1);
1394 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1395 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1396 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1400 if (p->radio) /* if a radio channel, up immediately */
1402 /* Special pseudo -- automatically up */
1403 ast_setstate(ast, AST_STATE_UP);
1406 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1407 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1409 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1412 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1418 if (p->owner == ast) {
1419 /* Normal ring, on hook */
1421 /* Don't send audio while on hook, until the call is answered */
1423 if (p->use_callerid) {
1424 /* Generate the Caller-ID spill if desired */
1426 ast_log(LOG_WARNING, "cidspill already exists??\n");
1429 p->cidspill = malloc(MAX_CALLERID_SIZE);
1432 p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
1436 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1438 /* Choose proper cadence */
1439 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1440 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1441 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1442 p->cidrings = cidrings[p->distinctivering - 1];
1444 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1445 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1446 p->cidrings = DEFAULT_CIDRINGS;
1450 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1451 c = strchr(dest, '/');
1454 if (c && (strlen(c) < p->stripmsd)) {
1455 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1459 p->dop.op = ZT_DIAL_OP_REPLACE;
1460 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1461 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1463 strcpy(p->dop.dialstr, "");
1466 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1467 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1472 /* Call waiting call */
1473 p->callwaitrings = 0;
1475 strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
1477 strcpy(p->callwaitcid, "");
1478 /* Call waiting tone instead */
1479 if (zt_callwait(ast))
1481 /* Make ring-back */
1482 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1483 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1487 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1489 strcpy(callerid, "");
1490 ast_callerid_parse(callerid, &n, &l);
1492 ast_shrink_phone_number(l);
1493 if (!ast_isphonenumber(l))
1497 strcpy(p->lastcallerid, l);
1499 strcpy(p->lastcallerid, "");
1500 ast_setstate(ast, AST_STATE_RINGING);
1501 index = zt_get_index(ast, p, 0);
1503 p->subs[index].needringing = 1;
1519 case SIG_SF_FEATDMF:
1521 c = strchr(dest, '/');
1526 if (strlen(c) < p->stripmsd) {
1527 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1531 /* Start the trunk */
1532 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1534 if (errno != EINPROGRESS) {
1535 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1539 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1540 p->dop.op = ZT_DIAL_OP_REPLACE;
1541 if (p->sig == SIG_FEATD) {
1542 if (ast->callerid) {
1543 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1544 ast_callerid_parse(callerid, &n, &l);
1546 ast_shrink_phone_number(l);
1547 if (!ast_isphonenumber(l))
1553 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1555 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1557 if (p->sig == SIG_FEATDMF) {
1558 if (ast->callerid) {
1559 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1560 ast_callerid_parse(callerid, &n, &l);
1562 ast_shrink_phone_number(l);
1563 if (!ast_isphonenumber(l))
1569 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1571 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1573 if (p->sig == SIG_E911) {
1574 strcpy(p->dop.dialstr,"M*911#");
1576 if (p->sig == SIG_FEATB) {
1577 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1579 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1580 if (strlen(p->dop.dialstr) > 4) {
1581 strcpy(p->echorest, "w");
1582 strcpy(p->echorest + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1584 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1588 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1590 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1591 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1595 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1597 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1598 ast_setstate(ast, AST_STATE_DIALING);
1602 c = strchr(dest, '/');
1607 if (ast->callerid && !p->hidecallerid) {
1608 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1609 ast_callerid_parse(callerid, &n, &l);
1611 ast_shrink_phone_number(l);
1612 if (!ast_isphonenumber(l))
1617 if (strlen(c) < p->stripmsd) {
1618 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1621 p->dop.op = ZT_DIAL_OP_REPLACE;
1622 s = strchr(c + p->stripmsd, 'w');
1624 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1627 strcpy(p->dop.dialstr, "");
1629 if (!(p->call = pri_new_call(p->pri->pri))) {
1630 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1633 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1634 if (pri_call(p->pri->pri, p->call, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1635 p->prioffset, p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1, l, p->pri->dialplan - 1, n,
1636 l ? (ast->restrictcid ? PRES_PROHIB_USER_NUMBER_PASSED_SCREEN : (p->use_callingpres ? ast->callingpres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN)) : PRES_NUMBER_NOT_AVAILABLE,
1637 c + p->stripmsd, p->pri->dialplan - 1,
1639 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)))) {
1640 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1643 ast_setstate(ast, AST_STATE_DIALING);
1647 /* Special pseudo -- automatically up*/
1648 ast_setstate(ast, AST_STATE_UP);
1651 ast_log(LOG_DEBUG, "not yet implemented\n");
1657 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1667 for (i = 0; i < 3; i++) {
1668 if (cur->subs[i].owner) {
1674 prev->next = cur->next;
1676 prev->next->prev = prev;
1682 iflist->prev = NULL;
1686 if (cur->subs[SUB_REAL].zfd > -1) {
1687 zt_close(cur->subs[SUB_REAL].zfd);
1693 prev->next = cur->next;
1695 prev->next->prev = prev;
1701 iflist->prev = NULL;
1705 if (cur->subs[SUB_REAL].zfd > -1) {
1706 zt_close(cur->subs[SUB_REAL].zfd);
1714 static int zt_hangup(struct ast_channel *ast)
1718 static int restore_gains(struct zt_pvt *p);
1719 struct zt_pvt *p = ast->pvt->pvt;
1720 struct zt_pvt *tmp = NULL;
1721 struct zt_pvt *prev = NULL;
1725 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1726 if (!ast->pvt->pvt) {
1727 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1731 ast_mutex_lock(&p->lock);
1733 index = zt_get_index(ast, p, 1);
1735 if (p->sig == SIG_PRI) {
1737 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1743 if (p->origcallerid) {
1744 strncpy(p->callerid, p->origcallerid, sizeof(p->callerid) - 1);
1745 free(p->origcallerid);
1746 p->origcallerid = NULL;
1749 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1751 strcpy(p->exten, "");
1753 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1754 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1758 /* Real channel, do some fixup */
1759 p->subs[index].owner = NULL;
1760 p->subs[index].needanswer = 0;
1761 p->subs[index].needringing = 0;
1762 p->subs[index].needbusy = 0;
1763 p->subs[index].needcongestion = 0;
1764 p->subs[index].linear = 0;
1765 p->subs[index].needcallerid = 0;
1766 zt_setlinear(p->subs[index].zfd, 0);
1767 if (index == SUB_REAL) {
1768 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1769 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1770 if (p->subs[SUB_CALLWAIT].inthreeway) {
1771 /* We had flipped over to answer a callwait and now it's gone */
1772 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1773 /* Move to the call-wait, but un-own us until they flip back. */
1774 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1775 unalloc_sub(p, SUB_CALLWAIT);
1778 /* The three way hung up, but we still have a call wait */
1779 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1780 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1781 unalloc_sub(p, SUB_THREEWAY);
1782 if (p->subs[SUB_REAL].inthreeway) {
1783 /* This was part of a three way call. Immediately make way for
1785 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1786 p->owner = p->subs[SUB_REAL].owner;
1788 /* This call hasn't been completed yet... Set owner to NULL */
1789 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1792 p->subs[SUB_REAL].inthreeway = 0;
1794 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
1795 /* Move to the call-wait and switch back to them. */
1796 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1797 unalloc_sub(p, SUB_CALLWAIT);
1798 p->owner = p->subs[SUB_REAL].owner;
1799 if (p->owner->_state != AST_STATE_UP)
1800 p->subs[SUB_REAL].needanswer = 1;
1801 if (p->subs[SUB_REAL].owner->bridge)
1802 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
1803 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
1804 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1805 unalloc_sub(p, SUB_THREEWAY);
1806 if (p->subs[SUB_REAL].inthreeway) {
1807 /* This was part of a three way call. Immediately make way for
1809 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1810 p->owner = p->subs[SUB_REAL].owner;
1812 /* This call hasn't been completed yet... Set owner to NULL */
1813 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1816 p->subs[SUB_REAL].inthreeway = 0;
1818 } else if (index == SUB_CALLWAIT) {
1819 /* Ditch the holding callwait call, and immediately make it availabe */
1820 if (p->subs[SUB_CALLWAIT].inthreeway) {
1821 /* This is actually part of a three way, placed on hold. Place the third part
1822 on music on hold now */
1823 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
1824 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
1825 p->subs[SUB_THREEWAY].inthreeway = 0;
1826 /* Make it the call wait now */
1827 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
1828 unalloc_sub(p, SUB_THREEWAY);
1830 unalloc_sub(p, SUB_CALLWAIT);
1831 } else if (index == SUB_THREEWAY) {
1832 if (p->subs[SUB_CALLWAIT].inthreeway) {
1833 /* The other party of the three way call is currently in a call-wait state.
1834 Start music on hold for them, and take the main guy out of the third call */
1835 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
1836 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
1837 p->subs[SUB_CALLWAIT].inthreeway = 0;
1839 p->subs[SUB_REAL].inthreeway = 0;
1840 /* If this was part of a three way call index, let us make
1841 another three way call */
1842 unalloc_sub(p, SUB_THREEWAY);
1844 /* This wasn't any sort of call, but how are we an index? */
1845 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
1850 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
1853 p->distinctivering = 0;
1854 p->confirmanswer = 0;
1860 p->onhooktime = time(NULL);
1861 #ifdef PRI_EVENT_PROCEEDING
1864 #ifdef PRI_EVENT_SETUP_ACK
1868 ast_dsp_free(p->dsp);
1872 law = ZT_LAW_DEFAULT;
1873 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
1875 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
1876 /* Perform low level hangup if no owner left */
1878 if (p->sig == SIG_PRI) {
1880 if (!pri_grab(p, p->pri)) {
1881 #ifndef NEW_PRI_HANGUP
1882 if (!p->alreadyhungup) {
1883 res = pri_disconnect(p->pri->pri, p->call, PRI_CAUSE_NORMAL_CLEARING);
1885 pri_release(p->pri->pri, p->call, -1);
1887 p->alreadyhungup = 0;
1891 #error Please update libpri. The new hangup routines were implemented. You can debug then using "pri debug span <span_no>". If you dont want to update libpri code simply comment out OPTIONS += -DNEW_PRI_HANGUP in asterisk/Makefile
1893 if (p->alreadyhungup) {
1894 pri_hangup(p->pri->pri, p->call, -1);
1897 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
1898 int icause = ast->hangupcause ? hangup_cause2pri(ast->hangupcause) : -1;
1899 p->alreadyhungup = 1;
1902 icause = atoi(cause);
1904 pri_hangup(p->pri->pri, p->call, icause);
1908 ast_log(LOG_WARNING, "pri_disconnect failed\n");
1911 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1920 if (p->sig == SIG_R2) {
1922 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
1931 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
1933 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
1939 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
1942 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
1944 /* If they're off hook, try playing congestion */
1945 if (par.rxisoffhook)
1946 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
1948 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1952 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1959 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1960 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1964 p->callwaiting = p->permcallwaiting;
1965 p->hidecallerid = p->permhidecallerid;
1967 strcpy(p->rdnis, "");
1969 /* Restore data mode */
1970 if (p->sig == SIG_PRI) {
1972 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1979 p->callwaitingrepeat = 0;
1981 ast->pvt->pvt = NULL;
1982 ast_mutex_unlock(&p->lock);
1983 ast_mutex_lock(&usecnt_lock);
1986 ast_log(LOG_WARNING, "Usecnt < 0???\n");
1987 ast_mutex_unlock(&usecnt_lock);
1988 ast_update_use_count();
1989 if (option_verbose > 2)
1990 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
1992 ast_mutex_lock(&iflock);
1998 destroy_channel(prev, tmp, 0);
2006 ast_mutex_unlock(&iflock);
2010 static int zt_answer(struct ast_channel *ast)
2012 struct zt_pvt *p = ast->pvt->pvt;
2015 int oldstate = ast->_state;
2016 ast_setstate(ast, AST_STATE_UP);
2017 ast_mutex_lock(&p->lock);
2018 index = zt_get_index(ast, p, 0);
2021 /* nothing to do if a radio channel */
2023 ast_mutex_unlock(&p->lock);
2042 case SIG_SF_FEATDMF:
2047 /* Pick up the line */
2048 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2049 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2050 tone_zone_play_tone(p->subs[index].zfd, -1);
2052 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2053 if (oldstate == AST_STATE_RINGING) {
2054 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2055 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2056 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2057 p->owner = p->subs[SUB_REAL].owner;
2060 if (p->sig & __ZT_SIG_FXS) {
2067 /* Send a pri acknowledge */
2068 if (!pri_grab(p, p->pri)) {
2069 res = pri_answer(p->pri->pri, p->call, 0, 1);
2072 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2079 res = mfcr2_AnswerCall(p->r2, NULL);
2081 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2085 ast_mutex_unlock(&p->lock);
2088 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2091 ast_mutex_unlock(&p->lock);
2095 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2100 struct zt_pvt *p = chan->pvt->pvt;
2103 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2104 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2110 if ((!cp) || (datalen < 1))
2116 case AST_OPTION_TONE_VERIFY:
2121 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2122 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2125 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2126 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2129 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2130 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2134 case AST_OPTION_TDD: /* turn on or off TDD */
2135 if (!*cp) { /* turn it off */
2136 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2137 if (p->tdd) tdd_free(p->tdd);
2143 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2144 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2147 /* otherwise, turn it on */
2148 if (!p->didtdd) { /* if havent done it yet */
2149 unsigned char mybuf[41000],*buf;
2150 int size,res,fd,len;
2152 struct pollfd fds[1];
2154 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2155 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2157 index = zt_get_index(chan, p, 0);
2159 ast_log(LOG_WARNING, "No index in TDD?\n");
2162 fd = p->subs[index].zfd;
2164 if (ast_check_hangup(chan)) return -1;
2166 if (size > READ_SIZE)
2169 fds[0].events = POLLPRI | POLLOUT;
2170 res = poll(fds, 1, -1);
2172 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2175 /* if got exception */
2176 if (fds[0].revents & POLLPRI) return -1;
2177 if (!(fds[0].revents & POLLOUT)) {
2178 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2181 res = write(fd, buf, size);
2183 if (res == -1) return -1;
2184 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2190 p->didtdd = 1; /* set to have done it now */
2192 if (*cp == 2) { /* Mate mode */
2193 if (p->tdd) tdd_free(p->tdd);
2198 if (!p->tdd) { /* if we dont have one yet */
2199 p->tdd = tdd_new(); /* allocate one */
2202 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2205 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2210 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2213 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2215 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2218 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2224 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2227 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2228 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2235 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2237 /* Unlink a specific slave or all slaves/masters from a given master */
2243 ast_mutex_lock(&master->lock);
2245 while(ast_mutex_trylock(&slave->lock)) {
2246 ast_mutex_unlock(&master->lock);
2248 ast_mutex_lock(&master->lock);
2253 for (x=0;x<MAX_SLAVES;x++) {
2254 if (master->slaves[x]) {
2255 if (!slave || (master->slaves[x] == slave)) {
2256 /* Take slave out of the conference */
2257 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2258 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2259 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2260 master->slaves[x]->master = NULL;
2261 master->slaves[x] = NULL;
2266 master->inconference = 0;
2269 if (master->master) {
2270 /* Take master out of the conference */
2271 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2272 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2274 for (x=0;x<MAX_SLAVES;x++) {
2275 if (master->master->slaves[x] == master)
2276 master->master->slaves[x] = NULL;
2277 else if (master->master->slaves[x])
2281 master->master->inconference = 0;
2283 master->master = NULL;
2285 update_conf(master);
2288 ast_mutex_unlock(&slave->lock);
2289 ast_mutex_unlock(&master->lock);
2293 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2295 if (!slave || !master) {
2296 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2299 for (x=0;x<MAX_SLAVES;x++) {
2300 if (!master->slaves[x]) {
2301 master->slaves[x] = slave;
2305 if (x >= MAX_SLAVES) {
2306 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2307 master->slaves[MAX_SLAVES - 1] = slave;
2310 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2311 slave->master = master;
2313 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2316 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2318 struct ast_channel *who = NULL, *cs[3];
2319 struct zt_pvt *p0, *p1, *op0, *op1;
2320 struct zt_pvt *master=NULL, *slave=NULL;
2321 struct ast_frame *f;
2326 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2327 int os1 = -1, os2 = -1;
2328 struct ast_channel *oc1, *oc2;
2330 /* if need DTMF, cant native bridge */
2331 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2334 ast_mutex_lock(&c0->lock);
2335 ast_mutex_lock(&c1->lock);
2339 /* cant do pseudo-channels here */
2340 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2341 ast_mutex_unlock(&c0->lock);
2342 ast_mutex_unlock(&c1->lock);
2346 op0 = p0 = c0->pvt->pvt;
2347 op1 = p1 = c1->pvt->pvt;
2350 oi1 = zt_get_index(c0, p0, 0);
2351 oi2 = zt_get_index(c1, p1, 0);
2354 if ((oi1 < 0) || (oi2 < 0)) {
2355 ast_mutex_unlock(&c0->lock);
2356 ast_mutex_unlock(&c1->lock);
2362 ast_mutex_lock(&p0->lock);
2363 if (ast_mutex_trylock(&p1->lock)) {
2364 /* Don't block, due to potential for deadlock */
2365 ast_mutex_unlock(&p0->lock);
2366 ast_mutex_unlock(&c0->lock);
2367 ast_mutex_unlock(&c1->lock);
2368 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2371 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2372 if (!p0->owner || !p1->owner) {
2373 /* Currently unowned -- Do nothing. */
2376 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2377 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2381 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2386 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2387 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,
2388 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2391 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2392 if (p1->subs[SUB_THREEWAY].inthreeway) {
2398 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2399 if (p0->subs[SUB_THREEWAY].inthreeway) {
2405 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2406 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2407 don't put us in anything */
2408 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2414 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2415 /* Same as previous */
2416 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2423 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2424 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2425 if (master && slave) {
2426 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2427 in an active threeway call with a channel that is ringing, we should
2428 indicate ringing. */
2429 if ((oi2 == SUB_THREEWAY) &&
2430 p1->subs[SUB_THREEWAY].inthreeway &&
2431 p1->subs[SUB_REAL].owner &&
2432 p1->subs[SUB_REAL].inthreeway &&
2433 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2434 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2435 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2436 os2 = p1->subs[SUB_REAL].owner->_state;
2438 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2439 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2441 if ((oi1 == SUB_THREEWAY) &&
2442 p0->subs[SUB_THREEWAY].inthreeway &&
2443 p0->subs[SUB_REAL].owner &&
2444 p0->subs[SUB_REAL].inthreeway &&
2445 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2446 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2447 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2448 os1 = p0->subs[SUB_REAL].owner->_state;
2450 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2451 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2453 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2454 if (!p0->echocanbridged || !p1->echocanbridged) {
2455 /* Disable echo cancellation if appropriate */
2460 zt_link(slave, master);
2461 master->inconference = inconf;
2462 } else if (!nothingok)
2463 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2467 t1 = p0->subs[SUB_REAL].inthreeway;
2468 t2 = p1->subs[SUB_REAL].inthreeway;
2470 ast_mutex_unlock(&p0->lock);
2471 ast_mutex_unlock(&p1->lock);
2473 ast_mutex_unlock(&c0->lock);
2474 ast_mutex_unlock(&c1->lock);
2476 /* Native bridge failed */
2477 if ((!master || !slave) && !nothingok) {
2489 /* Here's our main loop... Start by locking things, looking for private parts,
2490 and then balking if anything is wrong */
2491 ast_mutex_lock(&c0->lock);
2492 ast_mutex_lock(&c1->lock);
2496 i1 = zt_get_index(c0, p0, 1);
2498 i2 = zt_get_index(c1, p1, 1);
2499 ast_mutex_unlock(&c0->lock);
2500 ast_mutex_unlock(&c1->lock);
2501 if ((op0 != p0) || (op1 != p1) ||
2502 (ofd1 != c0->fds[0]) ||
2503 (ofd2 != c1->fds[0]) ||
2504 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2505 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2506 (oc1 != p0->owner) ||
2507 (oc2 != p1->owner) ||
2508 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2509 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2512 if (slave && master)
2513 zt_unlink(slave, master, 1);
2514 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2515 op0->channel, oi1, op1->channel, oi2);
2523 who = ast_waitfor_n(cs, 2, &to);
2525 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2528 if (who->pvt->pvt == op0)
2529 op0->ignoredtmf = 1;
2530 else if (who->pvt->pvt == op1)
2531 op1->ignoredtmf = 1;
2533 if (who->pvt->pvt == op0)
2534 op0->ignoredtmf = 0;
2535 else if (who->pvt->pvt == op1)
2536 op1->ignoredtmf = 0;
2540 if (slave && master)
2541 zt_unlink(slave, master, 1);
2548 if (f->frametype == AST_FRAME_DTMF) {
2549 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2550 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2553 if (slave && master)
2554 zt_unlink(slave, master, 1);
2556 } else if ((who == c0) && p0->pulsedial) {
2558 } else if ((who == c1) && p1->pulsedial) {
2564 /* Swap who gets priority */
2571 static int zt_indicate(struct ast_channel *chan, int condition);
2573 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2575 struct zt_pvt *p = newchan->pvt->pvt;
2577 ast_mutex_lock(&p->lock);
2578 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2579 if (p->owner == oldchan)
2582 if (p->subs[x].owner == oldchan) {
2584 zt_unlink(NULL, p, 0);
2585 p->subs[x].owner = newchan;
2587 if (newchan->_state == AST_STATE_RINGING)
2588 zt_indicate(newchan, AST_CONTROL_RINGING);
2590 ast_mutex_unlock(&p->lock);
2594 static int zt_ring_phone(struct zt_pvt *p)
2598 /* Make sure our transmit state is on hook */
2601 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2604 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2606 printf("Res: %d, error: %s\n", res, strerror(errno));
2612 /* Wait just in case */
2619 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2627 static void *ss_thread(void *data);
2629 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2631 static int attempt_transfer(struct zt_pvt *p)
2633 /* In order to transfer, we need at least one of the channels to
2634 actually be in a call bridge. We can't conference two applications
2635 together (but then, why would we want to?) */
2636 if (p->subs[SUB_REAL].owner->bridge) {
2637 /* The three-way person we're about to transfer to could still be in MOH, so
2638 stop if now if appropriate */
2639 if (p->subs[SUB_THREEWAY].owner->bridge)
2640 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2641 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2642 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2644 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2645 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2646 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2649 /* Orphan the channel */
2650 unalloc_sub(p, SUB_THREEWAY);
2651 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2652 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2653 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2655 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2656 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2657 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2658 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2661 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2662 unalloc_sub(p, SUB_THREEWAY);
2663 /* Tell the caller not to hangup */
2666 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2667 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2668 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2674 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2676 struct ast_frame *f;
2677 f = &p->subs[index].f;
2679 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2683 case MFCR2_EVENT_BLOCKED:
2684 if (option_verbose > 2)
2685 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2687 case MFCR2_EVENT_UNBLOCKED:
2688 if (option_verbose > 2)
2689 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2691 case MFCR2_EVENT_CONFIG_ERR:
2692 if (option_verbose > 2)
2693 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2695 case MFCR2_EVENT_RING:
2696 if (option_verbose > 2)
2697 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2699 case MFCR2_EVENT_HANGUP:
2700 if (option_verbose > 2)
2701 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2703 case MFCR2_EVENT_RINGING:
2704 if (option_verbose > 2)
2705 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2707 case MFCR2_EVENT_ANSWER:
2708 if (option_verbose > 2)
2709 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2711 case MFCR2_EVENT_HANGUP_ACK:
2712 if (option_verbose > 2)
2713 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2715 case MFCR2_EVENT_IDLE:
2716 if (option_verbose > 2)
2717 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2720 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2726 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2731 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2733 ast_log(LOG_WARNING, "Unable to check received bits\n");
2737 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2740 e = mfcr2_cas_signaling_event(p->r2, x);
2745 static int check_for_conference(struct zt_pvt *p)
2748 /* Fine if we already have a master, etc */
2749 if (p->master || (p->confno > -1))
2751 memset(&ci, 0, sizeof(ci));
2752 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2753 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2756 /* If we have no master and don't have a confno, then
2757 if we're in a conference, it's probably a MeetMe room or
2758 some such, so don't let us 3-way out! */
2759 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
2760 if (option_verbose > 2)
2761 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
2767 static int get_alarms(struct zt_pvt *p)
2771 memset(&zi, 0, sizeof(zi));
2772 zi.spanno = p->span;
2773 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
2775 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
2781 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
2786 struct zt_pvt *p = ast->pvt->pvt;
2788 pthread_attr_t attr;
2789 struct ast_channel *chan;
2790 pthread_attr_init(&attr);
2791 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2792 index = zt_get_index(ast, p, 0);
2793 p->subs[index].f.frametype = AST_FRAME_NULL;
2794 p->subs[index].f.datalen = 0;
2795 p->subs[index].f.samples = 0;
2796 p->subs[index].f.mallocd = 0;
2797 p->subs[index].f.offset = 0;
2798 p->subs[index].f.src = "zt_handle_event";
2799 p->subs[index].f.data = NULL;
2801 return &p->subs[index].f;
2802 if (p->fake_event) {
2803 res = p->fake_event;
2806 res = zt_get_event(p->subs[index].zfd);
2807 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
2808 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
2809 if (res & ZT_EVENT_PULSEDIGIT)
2813 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
2814 p->subs[index].f.frametype = AST_FRAME_DTMF;
2815 p->subs[index].f.subclass = res & 0xff;
2816 /* Return the captured digit */
2817 return &p->subs[index].f;
2820 case ZT_EVENT_BITSCHANGED:
2821 if (p->sig == SIG_R2) {
2823 struct ast_frame *f = &p->subs[index].f;
2825 e = r2_get_event_bits(p);
2827 f = handle_r2_event(p, e, index);
2833 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
2834 case ZT_EVENT_PULSE_START:
2835 /* Stop tone if there's a pulse start and the PBX isn't started */
2837 tone_zone_play_tone(p->subs[index].zfd, -1);
2839 case ZT_EVENT_DIALCOMPLETE:
2840 if (p->inalarm) break;
2841 if (p->radio) break;
2842 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
2843 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
2846 if (!x) { /* if not still dialing in driver */
2850 strcpy(p->dop.dialstr, p->echorest);
2851 p->dop.op = ZT_DIAL_OP_REPLACE;
2852 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
2856 if (p->sig == SIG_E911) {
2857 /* if thru with dialing after offhook */
2858 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
2859 ast_setstate(ast, AST_STATE_UP);
2860 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2861 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2863 } else { /* if to state wait for offhook to dial rest */
2864 /* we now wait for off hook */
2865 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
2868 if (ast->_state == AST_STATE_DIALING) {
2869 if (p->callprogress && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
2870 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
2871 } else if (p->confirmanswer || (!p->dialednone && ((p->sig == SIG_EM) || (p->sig == SIG_EM_E1) || (p->sig == SIG_EMWINK) || (p->sig == SIG_FEATD) || (p->sig == SIG_FEATDMF) || (p->sig == SIG_E911) || (p->sig == SIG_FEATB) || (p->sig == SIG_SF) || (p->sig == SIG_SFWINK) || (p->sig == SIG_SF_FEATD) || (p->sig == SIG_SF_FEATDMF) || (p->sig == SIG_SF_FEATB)))) {
2872 ast_setstate(ast, AST_STATE_RINGING);
2874 ast_setstate(ast, AST_STATE_UP);
2875 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2876 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2882 case ZT_EVENT_ALARM:
2884 #ifdef PRI_DESTROYCALL
2886 if (p->pri && p->pri->pri) {
2887 pri_hangup(p->pri->pri, p->call, -1);
2888 pri_destroycall(p->pri->pri, p->call);
2890 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
2893 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2896 #error Please "cvs update" and recompile libpri
2900 res = get_alarms(p);
2901 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
2902 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
2905 alarm2str(res), p->channel);
2906 /* fall through intentionally */
2907 case ZT_EVENT_ONHOOK:
2910 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2911 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
2918 p->onhooktime = time(NULL);
2920 /* Check for some special conditions regarding call waiting */
2921 if (index == SUB_REAL) {
2922 /* The normal line was hung up */
2923 if (p->subs[SUB_CALLWAIT].owner) {
2924 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
2925 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2926 if (option_verbose > 2)
2927 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2928 unalloc_sub(p, SUB_CALLWAIT);
2930 p->subs[index].needanswer = 0;
2931 p->subs[index].needringing = 0;
2933 p->callwaitingrepeat = 0;
2937 } else if (p->subs[SUB_THREEWAY].owner) {
2939 unsigned int mssinceflash;
2940 gettimeofday(&tv, NULL);
2941 mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
2942 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
2943 if (mssinceflash < MIN_MS_SINCE_FLASH) {
2944 /* It hasn't been long enough since the last flashook. This is probably a bounce on
2945 hanging up. Hangup both channels now */
2946 if (p->subs[SUB_THREEWAY].owner)
2947 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
2948 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2949 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
2950 } else if ((ast->pbx) ||