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 <asterisk/utils.h>
43 #include <sys/signal.h>
48 #include <sys/ioctl.h>
50 #include <linux/zaptel.h>
53 #endif /* __linux__ */
59 #ifndef PRI_GR303_SUPPORT
60 #error "You need newer libpri"
67 #include "../asterisk.h"
70 #error "Your zaptel is too old. please cvs update"
74 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
75 * the user hangs up to reset the state machine so ring works properly.
76 * This is used to be able to support kewlstart by putting the zhone in
77 * groundstart mode since their forward disconnect supervision is entirely
78 * broken even though their documentation says it isn't and their support
79 * is entirely unwilling to provide any assistance with their channel banks
80 * even though their web site says they support their products for life.
83 /* #define ZHONE_HACK */
85 /* Typically, how many rings before we should send Caller*ID */
86 #define DEFAULT_CIDRINGS 1
88 #define CHANNEL_PSEUDO -12
90 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
92 static char *desc = "Zapata Telephony"
101 static char *tdesc = "Zapata Telephony Driver"
110 static char *type = "Zap";
111 static char *typecompat = "Tor"; /* Retain compatibility with chan_tor */
112 static char *config = "zapata.conf";
114 #define SIG_EM ZT_SIG_EM
115 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
116 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
117 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
118 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
119 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
120 #define SIG_FXSLS ZT_SIG_FXSLS
121 #define SIG_FXSGS ZT_SIG_FXSGS
122 #define SIG_FXSKS ZT_SIG_FXSKS
123 #define SIG_FXOLS ZT_SIG_FXOLS
124 #define SIG_FXOGS ZT_SIG_FXOGS
125 #define SIG_FXOKS ZT_SIG_FXOKS
126 #define SIG_PRI ZT_SIG_CLEAR
127 #define SIG_R2 ZT_SIG_CAS
128 #define SIG_SF ZT_SIG_SF
129 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
130 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
131 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
132 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
133 #define SIG_EM_E1 ZT_SIG_EM_E1
134 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
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 static char context[AST_MAX_EXTENSION] = "default";
143 static char callerid[256] = "";
145 static char language[MAX_LANGUAGE] = "";
146 static char musicclass[MAX_LANGUAGE] = "";
147 static char progzone[10]= "";
149 static int usedistinctiveringdetection = 0;
151 static int use_callerid = 1;
152 static int zaptrcallerid = 0;
153 static int cur_signalling = -1;
155 static unsigned int cur_group = 0;
156 static unsigned int cur_callergroup = 0;
157 static unsigned int cur_pickupgroup = 0;
158 static int relaxdtmf = 0;
160 static int immediate = 0;
162 static int stripmsd = 0;
164 static int callwaiting = 0;
166 static int callwaitingcallerid = 0;
168 static int hidecallerid = 0;
170 static int restrictcid = 0;
172 static int use_callingpres = 0;
174 static int callreturn = 0;
176 static int threewaycalling = 0;
178 static int transfer = 0;
180 static int cancallforward = 0;
182 static float rxgain = 0.0;
184 static float txgain = 0.0;
186 static int tonezone = -1;
188 static int echocancel;
190 static int echotraining;
192 static int echocanbridged = 0;
194 static int busydetect = 0;
196 static int busycount = 3;
198 static int callprogress = 0;
200 static char accountcode[20] = "";
202 static char mailbox[AST_MAX_EXTENSION];
204 static int amaflags = 0;
208 static int numbufs = 4;
210 static int cur_prewink = -1;
211 static int cur_preflash = -1;
212 static int cur_wink = -1;
213 static int cur_flash = -1;
214 static int cur_start = -1;
215 static int cur_rxwink = -1;
216 static int cur_rxflash = -1;
217 static int cur_debounce = -1;
220 static int minunused = 2;
221 static int minidle = 0;
222 static char idleext[AST_MAX_EXTENSION];
223 static char idledial[AST_MAX_EXTENSION];
224 static int overlapdial = 0;
225 static struct ast_channel inuse = { "GR-303InUse" };
228 /* Wait up to 16 seconds for first digit (FXO logic) */
229 static int firstdigittimeout = 16000;
231 /* How long to wait for following digits (FXO logic) */
232 static int gendigittimeout = 8000;
234 /* How long to wait for an extra digit, if there is an ambiguous match */
235 static int matchdigittimeout = 3000;
237 static int usecnt =0;
238 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
240 /* Protect the interface list (of zt_pvt's) */
241 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
243 static int ifcount = 0;
245 /* Protect the monitoring thread, so only one process can kill or start it, and not
246 when it's doing something critical. */
247 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
249 /* This is the thread for the monitor which checks for input on the channels
250 which are not currently in use. */
251 static pthread_t monitor_thread = AST_PTHREADT_NULL;
253 static int restart_monitor(void);
255 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
257 static int zt_sendtext(struct ast_channel *c, char *text);
259 static inline int zt_get_event(int fd)
261 /* Avoid the silly zt_getevent which ignores a bunch of events */
263 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
267 static inline int zt_wait_event(int fd)
269 /* Avoid the silly zt_waitevent which ignores a bunch of events */
271 i = ZT_IOMUX_SIGEVENT;
272 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
273 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
277 /* Chunk size to read -- we use 20ms chunks to make things happy. */
278 #define READ_SIZE 160
280 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
281 #define MASK_INUSE (1 << 1) /* Channel currently in use */
283 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
284 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
285 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
286 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
287 #define RINGT ( (8000 * 8) / READ_SIZE)
293 static int r2prot = -1;
299 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->pri->logicalspan << 8))
300 #define PRI_CHANNEL(p) ((p) & 0xff)
301 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
304 pthread_t master; /* Thread of master */
305 ast_mutex_t lock; /* Mutex */
306 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
307 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
308 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
309 int minunused; /* Min # of channels to keep empty */
310 int minidle; /* Min # of "idling" calls to keep active */
311 int nodetype; /* Node type */
312 int switchtype; /* Type of switch to emulate */
313 int dialplan; /* Dialing plan */
314 int dchannel; /* What channel the dchannel is on */
315 int trunkgroup; /* What our trunkgroup is */
316 int mastertrunkgroup; /* What trunk group is our master */
317 int logicalspan; /* Logical span number within trunk group */
318 int numchans; /* Num of channels we represent */
319 int overlapdial; /* In overlap dialing mode */
329 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
330 struct zt_pvt *crvs; /* Member CRV structs */
331 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
335 static struct zt_pri pris[NUM_SPANS];
337 static int pritype = PRI_CPE;
340 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
342 #define DEFAULT_PRI_DEBUG 0
345 static inline void pri_rel(struct zt_pri *pri)
347 ast_mutex_unlock(&pri->lock);
350 static int switchtype = PRI_SWITCH_NI2;
351 static int dialplan = PRI_NATIONAL_ISDN + 1;
354 /* Shut up the compiler */
358 #define SUB_REAL 0 /* Active call */
359 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
360 #define SUB_THREEWAY 2 /* Three-way call */
363 static struct zt_distRings drings;
365 struct distRingData {
368 struct ringContextData {
369 char contextData[AST_MAX_EXTENSION];
371 struct zt_distRings {
372 struct distRingData ringnum[3];
373 struct ringContextData ringContext[3];
376 static char *subnames[] = {
382 struct zt_subchannel {
384 struct ast_channel *owner;
386 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
387 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
398 #define CONF_USER_REAL (1 << 0)
399 #define CONF_USER_THIRDCALL (1 << 1)
403 static struct zt_pvt {
405 struct ast_channel *owner; /* Our current active owner (if applicable) */
406 /* Up to three channels can be associated with this call */
408 struct zt_subchannel sub_unused; /* Just a safety precaution */
409 struct zt_subchannel subs[3]; /* Sub-channels */
410 struct zt_confinfo saveconf; /* Saved conference info */
412 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
413 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
414 int inconference; /* If our real should be in the conference */
416 int sig; /* Signalling style */
417 int radio; /* radio type */
418 int firstradio; /* first radio flag */
421 int tonezone; /* tone zone for this chan, or -1 for default */
422 struct zt_pvt *next; /* Next channel in list */
423 struct zt_pvt *prev; /* Prev channel in list */
425 struct zt_distRings drings;
426 int usedistinctiveringdetection;
428 char context[AST_MAX_EXTENSION];
429 char defcontext[AST_MAX_EXTENSION];
430 char exten[AST_MAX_EXTENSION];
431 char language[MAX_LANGUAGE];
432 char musicclass[MAX_LANGUAGE];
433 char callerid[AST_MAX_EXTENSION];
434 char lastcallerid[AST_MAX_EXTENSION];
435 char *origcallerid; /* malloced original callerid */
436 char callwaitcid[AST_MAX_EXTENSION];
437 char rdnis[AST_MAX_EXTENSION];
438 char dnid[AST_MAX_EXTENSION];
441 int confno; /* Our conference */
442 int confusers; /* Who is using our conference */
443 int propconfno; /* Propagated conference number */
444 unsigned int callgroup;
445 unsigned int pickupgroup;
446 int immediate; /* Answer before getting digits? */
447 int channel; /* Channel Number or CRV */
448 int span; /* Span number */
450 time_t guardtime; /* Must wait this much time before using for new call */
452 int use_callerid; /* Whether or not to use caller id on this channel */
455 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
456 int restrictcid; /* Whether restrict the callerid -> only send ANI */
457 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
458 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
459 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
460 int cidcwexpire; /* When to expire our muting for CID/CW */
461 unsigned char *cidspill;
476 int callwaitingcallerid;
485 struct timeval flashtime; /* Last flash-hook time */
487 int cref; /* Call reference number */
488 ZT_DIAL_OPERATION dop;
492 char accountcode[20]; /* Account code */
493 int amaflags; /* AMA Flags */
494 char didtdd; /* flag to say its done it once */
495 struct tdd_state *tdd; /* TDD flag */
498 char call_forward[AST_MAX_EXTENSION];
499 char mailbox[AST_MAX_EXTENSION];
504 int confirmanswer; /* Wait for '#' to confirm answer */
505 int distinctivering; /* Which distinctivering to use */
506 int cidrings; /* Which ring to deliver CID on */
508 int faxhandled; /* Has a fax tone already been handled? */
510 char mate; /* flag to say its in MATE mode */
511 int pulsedial; /* whether a pulse dial phone is detected */
512 int dtmfrelax; /* whether to run in relaxed DTMF mode */
514 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
517 struct zt_pvt *bearer;
518 struct zt_pvt *realcall;
525 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
534 } *iflist = NULL, *ifend = NULL;
536 struct zt_pvt *round_robin[32];
539 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
542 /* Grab the lock first */
544 res = ast_mutex_trylock(&pri->lock);
546 ast_mutex_unlock(&pvt->lock);
547 /* Release the lock and try again */
549 ast_mutex_lock(&pvt->lock);
552 /* Then break the poll */
553 pthread_kill(pri->master, SIGURG);
558 #define NUM_CADENCE_MAX 25
559 static int num_cadence = 4;
560 static int user_has_defined_cadences = 0;
562 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
563 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
564 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
565 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
566 { { 1000, 500, 2500, 5000 } }, /* Long ring */
569 int receivedRingT; /* Used to find out what ringtone we are on */
571 /* cidrings says in which pause to transmit the cid information, where the first pause
572 * is 1, the second pause is 2 and so on.
575 static int cidrings[NUM_CADENCE_MAX] = {
576 2, /* Right after first long ring */
577 4, /* Right after long part */
578 3, /* After third chirp */
579 2, /* Second spell */
582 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
583 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
585 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
586 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
589 /* translate between PRI causes and asterisk's */
590 static int hangup_pri2cause(int cause)
593 case PRI_CAUSE_USER_BUSY:
594 return AST_CAUSE_BUSY;
595 case PRI_CAUSE_NORMAL_CLEARING:
596 return AST_CAUSE_NORMAL;
597 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
598 case PRI_CAUSE_REQUESTED_CHAN_UNAVAIL:
599 return AST_CAUSE_CONGESTION;
600 case PRI_CAUSE_UNALLOCATED:
601 case PRI_CAUSE_NUMBER_CHANGED:
602 return AST_CAUSE_UNALLOCATED;
604 return AST_CAUSE_FAILURE;
610 /* translate between ast cause and PRI */
611 static int hangup_cause2pri(int cause)
615 return PRI_CAUSE_USER_BUSY;
616 case AST_CAUSE_UNALLOCATED:
617 return PRI_CAUSE_UNALLOCATED;
618 case AST_CAUSE_CONGESTION:
619 return PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION;
620 case AST_CAUSE_NORMAL:
622 return PRI_CAUSE_NORMAL_CLEARING;
629 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
632 if (p->subs[0].owner == ast)
634 else if (p->subs[1].owner == ast)
636 else if (p->subs[2].owner == ast)
641 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
646 static void wakeup_sub(struct zt_pvt *p, int a)
648 struct ast_frame null = { AST_FRAME_NULL, };
650 if (p->subs[a].owner) {
651 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
652 ast_mutex_unlock(&p->lock);
654 ast_mutex_lock(&p->lock);
656 ast_queue_frame(p->subs[a].owner, &null);
657 ast_mutex_unlock(&p->subs[a].owner->lock);
665 static void swap_subs(struct zt_pvt *p, int a, int b)
669 struct ast_channel *towner;
671 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
673 tchan = p->subs[a].chan;
674 towner = p->subs[a].owner;
675 tinthreeway = p->subs[a].inthreeway;
677 p->subs[a].chan = p->subs[b].chan;
678 p->subs[a].owner = p->subs[b].owner;
679 p->subs[a].inthreeway = p->subs[b].inthreeway;
681 p->subs[b].chan = tchan;
682 p->subs[b].owner = towner;
683 p->subs[b].inthreeway = tinthreeway;
685 if (p->subs[a].owner)
686 p->subs[a].owner->fds[0] = p->subs[a].zfd;
687 if (p->subs[b].owner)
688 p->subs[b].owner->fds[0] = p->subs[b].zfd;
693 static int zt_open(char *fn)
701 for (x=0;x<strlen(fn);x++) {
702 if (!isdigit(fn[x])) {
710 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
713 fn = "/dev/zap/channel";
715 fd = open(fn, O_RDWR | O_NONBLOCK);
717 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
721 if (ioctl(fd, ZT_SPECIFY, &chan)) {
725 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
730 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
734 static void zt_close(int fd)
739 int zt_setlinear(int zfd, int linear)
742 res = ioctl(zfd, ZT_SETLINEAR, &linear);
749 int zt_setlaw(int zfd, int law)
752 res = ioctl(zfd, ZT_SETLAW, &law);
758 static int alloc_sub(struct zt_pvt *p, int x)
762 if (p->subs[x].zfd < 0) {
763 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
764 if (p->subs[x].zfd > -1) {
765 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
767 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
768 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
769 bi.numbufs = numbufs;
770 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
772 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
775 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
776 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
777 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
778 zt_close(p->subs[x].zfd);
783 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
786 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
789 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
793 static int unalloc_sub(struct zt_pvt *p, int x)
796 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
799 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
800 if (p->subs[x].zfd > -1) {
801 zt_close(p->subs[x].zfd);
804 p->subs[x].linear = 0;
806 p->subs[x].owner = NULL;
807 p->subs[x].inthreeway = 0;
808 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
812 static int zt_digit(struct ast_channel *ast, char digit)
814 ZT_DIAL_OPERATION zo;
819 index = zt_get_index(ast, p, 0);
820 if (index == SUB_REAL) {
822 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && p->setup_ack && !p->proceeding) {
823 if (!pri_grab(p, p->pri)) {
824 pri_information(p->pri->pri,p->call,digit);
827 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
832 zo.op = ZT_DIAL_OP_APPEND;
834 zo.dialstr[1] = digit;
836 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
837 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
846 static char *events[] = {
859 "Hook Transition Complete",
868 { ZT_ALARM_RED, "Red Alarm" },
869 { ZT_ALARM_YELLOW, "Yellow Alarm" },
870 { ZT_ALARM_BLUE, "Blue Alarm" },
871 { ZT_ALARM_RECOVER, "Recovering" },
872 { ZT_ALARM_LOOPBACK, "Loopback" },
873 { ZT_ALARM_NOTOPEN, "Not Open" },
874 { ZT_ALARM_NONE, "None" },
877 static char *alarm2str(int alarm)
880 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
881 if (alarms[x].alarm & alarm)
882 return alarms[x].name;
884 return alarm ? "Unknown Alarm" : "No Alarm";
887 static char *event2str(int event)
889 static char buf[256];
890 if ((event < 15) && (event > -1))
891 return events[event];
892 sprintf(buf, "Event %d", event);
897 static int str2r2prot(char *swtype)
899 if (!strcasecmp(swtype, "ar"))
900 return MFCR2_PROT_ARGENTINA;
902 if (!strcasecmp(swtype, "cn"))
903 return MFCR2_PROT_CHINA;
905 if (!strcasecmp(swtype, "kr"))
906 return MFCR2_PROT_KOREA;
912 static char *sig2str(int sig)
914 static char buf[256];
917 return "E & M Immediate";
923 return "Feature Group D (DTMF)";
925 return "Feature Group D (MF)";
927 return "Feature Group B (MF)";
931 return "FXS Loopstart";
933 return "FXS Groundstart";
935 return "FXS Kewlstart";
937 return "FXO Loopstart";
939 return "FXO Groundstart";
941 return "FXO Kewlstart";
943 return "PRI Signalling";
945 return "R2 Signalling";
947 return "SF (Tone) Signalling Immediate";
949 return "SF (Tone) Signalling Wink";
951 return "SF (Tone) Signalling with Feature Group D (DTMF)";
953 return "SF (Tone) Signalling with Feature Group D (MF)";
955 return "SF (Tone) Signalling with Feature Group B (MF)";
957 return "GR-303 Signalling with FXOKS";
959 return "Pseudo Signalling";
961 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
966 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
968 /* If the conference already exists, and we're already in it
969 don't bother doing anything */
972 memset(&zi, 0, sizeof(zi));
975 if (slavechannel > 0) {
976 /* If we have only one slave, do a digital mon */
977 zi.confmode = ZT_CONF_DIGITALMON;
978 zi.confno = slavechannel;
981 /* Real-side and pseudo-side both participate in conference */
982 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
983 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
985 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
986 zi.confno = p->confno;
988 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
992 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
993 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
996 if (slavechannel < 1) {
997 p->confno = zi.confno;
999 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1000 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1004 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1006 /* If they're listening to our channel, they're ours */
1007 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1009 /* If they're a talker on our (allocated) conference, they're ours */
1010 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1015 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1018 if (/* Can't delete if there's no zfd */
1020 /* Don't delete from the conference if it's not our conference */
1022 /* Don't delete if we don't think it's conferenced at all (implied) */
1024 memset(&zi, 0, sizeof(zi));
1028 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1029 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1032 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1033 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1037 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1041 struct zt_pvt *slave = NULL;
1042 /* Start out optimistic */
1044 /* Update conference state in a stateless fashion */
1046 /* Any three-way calling makes slave native mode *definitely* out
1048 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1051 /* If we don't have any 3-way calls, check to see if we have
1052 precisely one slave */
1053 if (useslavenative) {
1054 for (x=0;x<MAX_SLAVES;x++) {
1057 /* Whoops already have a slave! No
1058 slave native and stop right away */
1063 /* We have one slave so far */
1064 slave = p->slaves[x];
1069 /* If no slave, slave native definitely out */
1072 else if (slave->law != p->law) {
1078 return useslavenative;
1081 static int update_conf(struct zt_pvt *p)
1086 struct zt_pvt *slave = NULL;
1088 useslavenative = isslavenative(p, &slave);
1089 /* Start with the obvious, general stuff */
1091 /* Look for three way calls */
1092 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1093 conf_add(p, &p->subs[x], x, 0);
1096 conf_del(p, &p->subs[x], x);
1099 /* If we have a slave, add him to our conference now. or DAX
1100 if this is slave native */
1101 for (x=0;x<MAX_SLAVES;x++) {
1104 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, p->channel);
1106 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1111 /* If we're supposed to be in there, do so now */
1112 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1114 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, slave->channel);
1116 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1120 /* If we have a master, add ourselves to his conference */
1122 if (isslavenative(p->master, NULL)) {
1123 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, p->master->channel);
1125 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1129 /* Nobody is left (or should be left) in our conference.
1133 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1137 static void zt_enable_ec(struct zt_pvt *p)
1142 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1145 if (p && p->echocancel) {
1146 if (p->sig == SIG_PRI) {
1148 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1150 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1153 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1155 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1158 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1161 ast_log(LOG_DEBUG, "No echocancellation requested\n");
1164 static void zt_train_ec(struct zt_pvt *p)
1168 if (p && p->echocancel && p->echotraining) {
1170 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1172 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1174 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1177 ast_log(LOG_DEBUG, "No echo training requested\n");
1180 static void zt_disable_ec(struct zt_pvt *p)
1184 if (p->echocancel) {
1186 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1188 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1190 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1195 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1202 if ((rxgain != 0.0) || (txgain != 0.0)) {
1203 /* caluculate linear value of tx gain */
1204 ltxgain = pow(10.0,txgain / 20.0);
1205 /* caluculate linear value of rx gain */
1206 lrxgain = pow(10.0,rxgain / 20.0);
1207 if (law == ZT_LAW_ALAW) {
1208 for (j=0;j<256;j++) {
1209 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1210 if (k > 32767) k = 32767;
1211 if (k < -32767) k = -32767;
1212 g.rxgain[j] = AST_LIN2A(k);
1213 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1214 if (k > 32767) k = 32767;
1215 if (k < -32767) k = -32767;
1216 g.txgain[j] = AST_LIN2A(k);
1219 for (j=0;j<256;j++) {
1220 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1221 if (k > 32767) k = 32767;
1222 if (k < -32767) k = -32767;
1223 g.rxgain[j] = AST_LIN2MU(k);
1224 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1225 if (k > 32767) k = 32767;
1226 if (k < -32767) k = -32767;
1227 g.txgain[j] = AST_LIN2MU(k);
1231 for (j=0;j<256;j++) {
1238 return(ioctl(fd,ZT_SETGAINS,&g));
1241 static inline int zt_set_hook(int fd, int hs)
1245 res = ioctl(fd, ZT_HOOK, &x);
1248 if (errno == EINPROGRESS) return 0;
1249 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1254 static inline int zt_confmute(struct zt_pvt *p, int muted)
1258 if (p->sig == SIG_PRI) {
1260 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1262 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1264 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1266 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1270 static int save_conference(struct zt_pvt *p)
1272 struct zt_confinfo c;
1274 if (p->saveconf.confmode) {
1275 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1278 p->saveconf.chan = 0;
1279 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1281 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1282 p->saveconf.confmode = 0;
1287 c.confmode = ZT_CONF_NORMAL;
1288 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1290 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1294 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1298 static int restore_conference(struct zt_pvt *p)
1301 if (p->saveconf.confmode) {
1302 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1303 p->saveconf.confmode = 0;
1305 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1310 ast_log(LOG_DEBUG, "Restored conferencing\n");
1314 static int send_callerid(struct zt_pvt *p);
1316 int send_cwcidspill(struct zt_pvt *p)
1320 p->cidspill = malloc(MAX_CALLERID_SIZE);
1322 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1323 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
1324 /* Make sure we account for the end */
1325 p->cidlen += READ_SIZE * 4;
1328 if (option_verbose > 2)
1329 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
1334 static int has_voicemail(struct zt_pvt *p)
1337 return ast_app_has_voicemail(p->mailbox);
1340 static int send_callerid(struct zt_pvt *p)
1342 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1344 /* Take out of linear mode if necessary */
1345 if (p->subs[SUB_REAL].linear) {
1346 p->subs[SUB_REAL].linear = 0;
1347 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1349 while(p->cidpos < p->cidlen) {
1350 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1352 if (errno == EAGAIN)
1355 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1365 if (p->callwaitcas) {
1366 /* Wait for CID/CW to expire */
1367 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1369 restore_conference(p);
1373 static int zt_callwait(struct ast_channel *ast)
1375 struct zt_pvt *p = ast->pvt->pvt;
1376 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1378 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1381 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1385 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1386 if (!p->callwaitrings && p->callwaitingcallerid) {
1387 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1389 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1391 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1393 p->cidlen = 2400 + READ_SIZE * 4;
1398 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1404 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1406 struct zt_pvt *p = ast->pvt->pvt;
1413 char dest[256]; /* must be same length as p->dialdest */
1414 ast_mutex_lock(&p->lock);
1415 strncpy(dest, rdest, sizeof(dest) - 1);
1416 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1417 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1418 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1419 ast_mutex_unlock(&p->lock);
1423 if (p->radio) /* if a radio channel, up immediately */
1425 /* Special pseudo -- automatically up */
1426 ast_setstate(ast, AST_STATE_UP);
1427 ast_mutex_unlock(&p->lock);
1430 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1431 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1433 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1436 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1442 if (p->owner == ast) {
1443 /* Normal ring, on hook */
1445 /* Don't send audio while on hook, until the call is answered */
1447 if (p->use_callerid) {
1448 /* Generate the Caller-ID spill if desired */
1450 ast_log(LOG_WARNING, "cidspill already exists??\n");
1453 p->cidspill = malloc(MAX_CALLERID_SIZE);
1456 p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
1460 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1462 /* Choose proper cadence */
1463 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1464 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1465 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1466 p->cidrings = cidrings[p->distinctivering - 1];
1468 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1469 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1470 p->cidrings = DEFAULT_CIDRINGS;
1474 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1475 c = strchr(dest, '/');
1478 if (c && (strlen(c) < p->stripmsd)) {
1479 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1483 p->dop.op = ZT_DIAL_OP_REPLACE;
1484 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1485 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1487 strcpy(p->dop.dialstr, "");
1490 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1491 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1492 ast_mutex_unlock(&p->lock);
1497 /* Call waiting call */
1498 p->callwaitrings = 0;
1500 strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
1502 strcpy(p->callwaitcid, "");
1503 /* Call waiting tone instead */
1504 if (zt_callwait(ast)) {
1505 ast_mutex_unlock(&p->lock);
1508 /* Make ring-back */
1509 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1510 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1514 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1516 strcpy(callerid, "");
1517 ast_callerid_parse(callerid, &n, &l);
1519 ast_shrink_phone_number(l);
1520 if (!ast_isphonenumber(l))
1524 strcpy(p->lastcallerid, l);
1526 strcpy(p->lastcallerid, "");
1527 ast_setstate(ast, AST_STATE_RINGING);
1528 index = zt_get_index(ast, p, 0);
1530 p->subs[index].needringing = 1;
1546 case SIG_SF_FEATDMF:
1548 c = strchr(dest, '/');
1553 if (strlen(c) < p->stripmsd) {
1554 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1555 ast_mutex_unlock(&p->lock);
1559 /* Start the trunk */
1560 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1562 if (errno != EINPROGRESS) {
1563 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1564 ast_mutex_unlock(&p->lock);
1568 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1569 p->dop.op = ZT_DIAL_OP_REPLACE;
1570 if (p->sig == SIG_FEATD) {
1571 if (ast->callerid) {
1572 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1573 ast_callerid_parse(callerid, &n, &l);
1575 ast_shrink_phone_number(l);
1576 if (!ast_isphonenumber(l))
1582 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1584 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1586 if (p->sig == SIG_FEATDMF) {
1587 if (ast->callerid) {
1588 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1589 ast_callerid_parse(callerid, &n, &l);
1591 ast_shrink_phone_number(l);
1592 if (!ast_isphonenumber(l))
1598 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1600 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1602 if (p->sig == SIG_E911) {
1603 strcpy(p->dop.dialstr,"M*911#");
1605 if (p->sig == SIG_FEATB) {
1606 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1608 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1609 if (strlen(p->dop.dialstr) > 4) {
1610 strcpy(p->echorest, "w");
1611 strcpy(p->echorest + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1613 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1617 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1619 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1620 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1621 ast_mutex_unlock(&p->lock);
1625 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1627 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1628 ast_setstate(ast, AST_STATE_DIALING);
1631 /* Special pseudo -- automatically up*/
1632 ast_setstate(ast, AST_STATE_UP);
1635 /* We'll get it in a moment */
1638 ast_log(LOG_DEBUG, "not yet implemented\n");
1639 ast_mutex_unlock(&p->lock);
1644 c = strchr(dest, '/');
1649 if (ast->callerid && !p->hidecallerid) {
1650 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1651 ast_callerid_parse(callerid, &n, &l);
1653 ast_shrink_phone_number(l);
1654 if (!ast_isphonenumber(l))
1659 if (strlen(c) < p->stripmsd) {
1660 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1661 ast_mutex_unlock(&p->lock);
1664 p->dop.op = ZT_DIAL_OP_REPLACE;
1665 s = strchr(c + p->stripmsd, 'w');
1667 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1670 strcpy(p->dop.dialstr, "");
1672 if (pri_grab(p, p->pri)) {
1673 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1674 ast_mutex_unlock(&p->lock);
1677 if (!(p->call = pri_new_call(p->pri->pri))) {
1678 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1680 ast_mutex_unlock(&p->lock);
1684 ast_log(LOG_DEBUG, "Oooh, I have a bearer on %d\n", PVT_TO_CHANNEL(p->bearer));
1685 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1686 p->bearer->call = p->call;
1688 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1689 if (pri_call(p->pri->pri, p->call, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1690 p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1, l, p->pri->dialplan - 1, n,
1691 l ? (ast->restrictcid ? PRES_PROHIB_USER_NUMBER_PASSED_SCREEN : (p->use_callingpres ? ast->callingpres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN)) : PRES_NUMBER_NOT_AVAILABLE,
1692 c + p->stripmsd, p->pri->dialplan - 1,
1694 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)))) {
1695 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1697 ast_mutex_unlock(&p->lock);
1700 ast_setstate(ast, AST_STATE_DIALING);
1704 ast_mutex_unlock(&p->lock);
1708 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1718 for (i = 0; i < 3; i++) {
1719 if (cur->subs[i].owner) {
1725 prev->next = cur->next;
1727 prev->next->prev = prev;
1733 iflist->prev = NULL;
1737 if (cur->subs[SUB_REAL].zfd > -1) {
1738 zt_close(cur->subs[SUB_REAL].zfd);
1744 prev->next = cur->next;
1746 prev->next->prev = prev;
1752 iflist->prev = NULL;
1756 if (cur->subs[SUB_REAL].zfd > -1) {
1757 zt_close(cur->subs[SUB_REAL].zfd);
1765 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
1767 bearer->owner = &inuse;
1768 bearer->master = crv;
1769 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
1770 crv->bearer = bearer;
1771 crv->call = bearer->call;
1777 static int zt_hangup(struct ast_channel *ast)
1781 static int restore_gains(struct zt_pvt *p);
1782 struct zt_pvt *p = ast->pvt->pvt;
1783 struct zt_pvt *tmp = NULL;
1784 struct zt_pvt *prev = NULL;
1788 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1789 if (!ast->pvt->pvt) {
1790 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1794 ast_mutex_lock(&p->lock);
1796 index = zt_get_index(ast, p, 1);
1798 if (p->sig == SIG_PRI) {
1800 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1806 if (p->origcallerid) {
1807 strncpy(p->callerid, p->origcallerid, sizeof(p->callerid) - 1);
1808 free(p->origcallerid);
1809 p->origcallerid = NULL;
1812 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1814 strcpy(p->exten, "");
1816 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1817 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1821 /* Real channel, do some fixup */
1822 p->subs[index].owner = NULL;
1823 p->subs[index].needanswer = 0;
1824 p->subs[index].needringing = 0;
1825 p->subs[index].needbusy = 0;
1826 p->subs[index].needcongestion = 0;
1827 p->subs[index].linear = 0;
1828 p->subs[index].needcallerid = 0;
1829 zt_setlinear(p->subs[index].zfd, 0);
1830 if (index == SUB_REAL) {
1831 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1832 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1833 if (p->subs[SUB_CALLWAIT].inthreeway) {
1834 /* We had flipped over to answer a callwait and now it's gone */
1835 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1836 /* Move to the call-wait, but un-own us until they flip back. */
1837 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1838 unalloc_sub(p, SUB_CALLWAIT);
1841 /* The three way hung up, but we still have a call wait */
1842 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1843 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1844 unalloc_sub(p, SUB_THREEWAY);
1845 if (p->subs[SUB_REAL].inthreeway) {
1846 /* This was part of a three way call. Immediately make way for
1848 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1849 p->owner = p->subs[SUB_REAL].owner;
1851 /* This call hasn't been completed yet... Set owner to NULL */
1852 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1855 p->subs[SUB_REAL].inthreeway = 0;
1857 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
1858 /* Move to the call-wait and switch back to them. */
1859 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1860 unalloc_sub(p, SUB_CALLWAIT);
1861 p->owner = p->subs[SUB_REAL].owner;
1862 if (p->owner->_state != AST_STATE_UP)
1863 p->subs[SUB_REAL].needanswer = 1;
1864 if (p->subs[SUB_REAL].owner->bridge)
1865 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
1866 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
1867 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1868 unalloc_sub(p, SUB_THREEWAY);
1869 if (p->subs[SUB_REAL].inthreeway) {
1870 /* This was part of a three way call. Immediately make way for
1872 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1873 p->owner = p->subs[SUB_REAL].owner;
1875 /* This call hasn't been completed yet... Set owner to NULL */
1876 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1879 p->subs[SUB_REAL].inthreeway = 0;
1881 } else if (index == SUB_CALLWAIT) {
1882 /* Ditch the holding callwait call, and immediately make it availabe */
1883 if (p->subs[SUB_CALLWAIT].inthreeway) {
1884 /* This is actually part of a three way, placed on hold. Place the third part
1885 on music on hold now */
1886 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
1887 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
1888 p->subs[SUB_THREEWAY].inthreeway = 0;
1889 /* Make it the call wait now */
1890 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
1891 unalloc_sub(p, SUB_THREEWAY);
1893 unalloc_sub(p, SUB_CALLWAIT);
1894 } else if (index == SUB_THREEWAY) {
1895 if (p->subs[SUB_CALLWAIT].inthreeway) {
1896 /* The other party of the three way call is currently in a call-wait state.
1897 Start music on hold for them, and take the main guy out of the third call */
1898 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
1899 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
1900 p->subs[SUB_CALLWAIT].inthreeway = 0;
1902 p->subs[SUB_REAL].inthreeway = 0;
1903 /* If this was part of a three way call index, let us make
1904 another three way call */
1905 unalloc_sub(p, SUB_THREEWAY);
1907 /* This wasn't any sort of call, but how are we an index? */
1908 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
1913 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
1916 p->distinctivering = 0;
1917 p->confirmanswer = 0;
1923 p->onhooktime = time(NULL);
1929 ast_dsp_free(p->dsp);
1933 law = ZT_LAW_DEFAULT;
1934 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
1936 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
1937 /* Perform low level hangup if no owner left */
1940 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
1941 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
1942 if (!pri_grab(p, p->pri)) {
1943 if (p->alreadyhungup) {
1944 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
1945 pri_hangup(p->pri->pri, p->call, -1);
1948 p->bearer->call = NULL;
1950 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
1951 int icause = ast->hangupcause ? hangup_cause2pri(ast->hangupcause) : -1;
1952 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
1953 p->alreadyhungup = 1;
1955 p->bearer->alreadyhungup = 1;
1958 icause = atoi(cause);
1960 pri_hangup(p->pri->pri, p->call, icause);
1963 ast_log(LOG_WARNING, "pri_disconnect failed\n");
1966 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1971 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
1978 if (p->sig == SIG_R2) {
1980 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
1988 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
1989 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
1991 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
1997 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2000 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2002 /* If they're off hook, try playing congestion */
2003 if (par.rxisoffhook)
2004 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2006 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2012 /* Make sure we're not made available for at least two seconds */
2013 time(&p->guardtime);
2017 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2024 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2025 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2029 p->callwaiting = p->permcallwaiting;
2030 p->hidecallerid = p->permhidecallerid;
2032 strcpy(p->rdnis, "");
2034 /* Restore data mode */
2035 if (p->sig == SIG_PRI) {
2037 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2041 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2042 /* Free up the bearer channel as well, and
2043 don't use its file descriptor anymore */
2044 p->bearer->owner = NULL;
2046 p->subs[SUB_REAL].zfd = -1;
2054 p->callwaitingrepeat = 0;
2056 ast->pvt->pvt = NULL;
2057 ast_mutex_unlock(&p->lock);
2058 ast_mutex_lock(&usecnt_lock);
2061 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2062 ast_mutex_unlock(&usecnt_lock);
2063 ast_update_use_count();
2064 if (option_verbose > 2)
2065 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2067 ast_mutex_lock(&iflock);
2073 destroy_channel(prev, tmp, 0);
2081 ast_mutex_unlock(&iflock);
2085 static int zt_answer(struct ast_channel *ast)
2087 struct zt_pvt *p = ast->pvt->pvt;
2090 int oldstate = ast->_state;
2091 ast_setstate(ast, AST_STATE_UP);
2092 ast_mutex_lock(&p->lock);
2093 index = zt_get_index(ast, p, 0);
2096 /* nothing to do if a radio channel */
2098 ast_mutex_unlock(&p->lock);
2117 case SIG_SF_FEATDMF:
2122 /* Pick up the line */
2123 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2124 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2125 tone_zone_play_tone(p->subs[index].zfd, -1);
2127 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2128 if (oldstate == AST_STATE_RINGING) {
2129 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2130 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2131 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2132 p->owner = p->subs[SUB_REAL].owner;
2135 if (p->sig & __ZT_SIG_FXS) {
2142 /* Send a pri acknowledge */
2143 if (!pri_grab(p, p->pri)) {
2145 res = pri_answer(p->pri->pri, p->call, 0, 1);
2148 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2155 res = mfcr2_AnswerCall(p->r2, NULL);
2157 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2161 ast_mutex_unlock(&p->lock);
2164 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2167 ast_mutex_unlock(&p->lock);
2171 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2176 struct zt_pvt *p = chan->pvt->pvt;
2179 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2180 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2186 if ((!cp) || (datalen < 1))
2192 case AST_OPTION_TONE_VERIFY:
2197 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2198 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2201 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2202 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2205 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2206 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2210 case AST_OPTION_TDD: /* turn on or off TDD */
2211 if (!*cp) { /* turn it off */
2212 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2213 if (p->tdd) tdd_free(p->tdd);
2219 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2220 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2223 /* otherwise, turn it on */
2224 if (!p->didtdd) { /* if havent done it yet */
2225 unsigned char mybuf[41000],*buf;
2226 int size,res,fd,len;
2228 struct pollfd fds[1];
2230 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2231 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2233 index = zt_get_index(chan, p, 0);
2235 ast_log(LOG_WARNING, "No index in TDD?\n");
2238 fd = p->subs[index].zfd;
2240 if (ast_check_hangup(chan)) return -1;
2242 if (size > READ_SIZE)
2245 fds[0].events = POLLPRI | POLLOUT;
2246 res = poll(fds, 1, -1);
2248 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2251 /* if got exception */
2252 if (fds[0].revents & POLLPRI) return -1;
2253 if (!(fds[0].revents & POLLOUT)) {
2254 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2257 res = write(fd, buf, size);
2259 if (res == -1) return -1;
2260 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2266 p->didtdd = 1; /* set to have done it now */
2268 if (*cp == 2) { /* Mate mode */
2269 if (p->tdd) tdd_free(p->tdd);
2274 if (!p->tdd) { /* if we dont have one yet */
2275 p->tdd = tdd_new(); /* allocate one */
2278 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2281 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2286 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2289 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2291 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2294 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2300 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2303 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2304 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2311 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2313 /* Unlink a specific slave or all slaves/masters from a given master */
2319 ast_mutex_lock(&master->lock);
2321 while(ast_mutex_trylock(&slave->lock)) {
2322 ast_mutex_unlock(&master->lock);
2324 ast_mutex_lock(&master->lock);
2329 for (x=0;x<MAX_SLAVES;x++) {
2330 if (master->slaves[x]) {
2331 if (!slave || (master->slaves[x] == slave)) {
2332 /* Take slave out of the conference */
2333 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2334 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2335 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2336 master->slaves[x]->master = NULL;
2337 master->slaves[x] = NULL;
2342 master->inconference = 0;
2345 if (master->master) {
2346 /* Take master out of the conference */
2347 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2348 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2350 for (x=0;x<MAX_SLAVES;x++) {
2351 if (master->master->slaves[x] == master)
2352 master->master->slaves[x] = NULL;
2353 else if (master->master->slaves[x])
2357 master->master->inconference = 0;
2359 master->master = NULL;
2361 update_conf(master);
2364 ast_mutex_unlock(&slave->lock);
2365 ast_mutex_unlock(&master->lock);
2369 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2371 if (!slave || !master) {
2372 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2375 for (x=0;x<MAX_SLAVES;x++) {
2376 if (!master->slaves[x]) {
2377 master->slaves[x] = slave;
2381 if (x >= MAX_SLAVES) {
2382 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2383 master->slaves[MAX_SLAVES - 1] = slave;
2386 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2387 slave->master = master;
2389 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2392 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2394 struct ast_channel *who = NULL, *cs[3];
2395 struct zt_pvt *p0, *p1, *op0, *op1;
2396 struct zt_pvt *master=NULL, *slave=NULL;
2397 struct ast_frame *f;
2402 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2403 int os1 = -1, os2 = -1;
2404 struct ast_channel *oc1, *oc2;
2406 /* if need DTMF, cant native bridge */
2407 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2410 ast_mutex_lock(&c0->lock);
2411 ast_mutex_lock(&c1->lock);
2415 /* cant do pseudo-channels here */
2416 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2417 ast_mutex_unlock(&c0->lock);
2418 ast_mutex_unlock(&c1->lock);
2422 op0 = p0 = c0->pvt->pvt;
2423 op1 = p1 = c1->pvt->pvt;
2426 oi1 = zt_get_index(c0, p0, 0);
2427 oi2 = zt_get_index(c1, p1, 0);
2430 if ((oi1 < 0) || (oi2 < 0)) {
2431 ast_mutex_unlock(&c0->lock);
2432 ast_mutex_unlock(&c1->lock);
2438 ast_mutex_lock(&p0->lock);
2439 if (ast_mutex_trylock(&p1->lock)) {
2440 /* Don't block, due to potential for deadlock */
2441 ast_mutex_unlock(&p0->lock);
2442 ast_mutex_unlock(&c0->lock);
2443 ast_mutex_unlock(&c1->lock);
2444 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2447 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2448 if (!p0->owner || !p1->owner) {
2449 /* Currently unowned -- Do nothing. */
2452 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2453 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2457 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2462 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2463 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,
2464 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2467 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2468 if (p1->subs[SUB_THREEWAY].inthreeway) {
2474 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2475 if (p0->subs[SUB_THREEWAY].inthreeway) {
2481 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2482 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2483 don't put us in anything */
2484 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2490 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2491 /* Same as previous */
2492 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2499 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2500 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2501 if (master && slave) {
2502 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2503 in an active threeway call with a channel that is ringing, we should
2504 indicate ringing. */
2505 if ((oi2 == SUB_THREEWAY) &&
2506 p1->subs[SUB_THREEWAY].inthreeway &&
2507 p1->subs[SUB_REAL].owner &&
2508 p1->subs[SUB_REAL].inthreeway &&
2509 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2510 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2511 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2512 os2 = p1->subs[SUB_REAL].owner->_state;
2514 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2515 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2517 if ((oi1 == SUB_THREEWAY) &&
2518 p0->subs[SUB_THREEWAY].inthreeway &&
2519 p0->subs[SUB_REAL].owner &&
2520 p0->subs[SUB_REAL].inthreeway &&
2521 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2522 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2523 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2524 os1 = p0->subs[SUB_REAL].owner->_state;
2526 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2527 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2529 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2530 if (!p0->echocanbridged || !p1->echocanbridged) {
2531 /* Disable echo cancellation if appropriate */
2536 zt_link(slave, master);
2537 master->inconference = inconf;
2538 } else if (!nothingok)
2539 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2543 t1 = p0->subs[SUB_REAL].inthreeway;
2544 t2 = p1->subs[SUB_REAL].inthreeway;
2546 ast_mutex_unlock(&p0->lock);
2547 ast_mutex_unlock(&p1->lock);
2549 ast_mutex_unlock(&c0->lock);
2550 ast_mutex_unlock(&c1->lock);
2552 /* Native bridge failed */
2553 if ((!master || !slave) && !nothingok) {
2565 /* Here's our main loop... Start by locking things, looking for private parts,
2566 and then balking if anything is wrong */
2567 ast_mutex_lock(&c0->lock);
2568 ast_mutex_lock(&c1->lock);
2572 i1 = zt_get_index(c0, p0, 1);
2574 i2 = zt_get_index(c1, p1, 1);
2575 ast_mutex_unlock(&c0->lock);
2576 ast_mutex_unlock(&c1->lock);
2577 if ((op0 != p0) || (op1 != p1) ||
2578 (ofd1 != c0->fds[0]) ||
2579 (ofd2 != c1->fds[0]) ||
2580 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2581 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2582 (oc1 != p0->owner) ||
2583 (oc2 != p1->owner) ||
2584 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2585 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2588 if (slave && master)
2589 zt_unlink(slave, master, 1);
2590 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2591 op0->channel, oi1, op1->channel, oi2);
2599 who = ast_waitfor_n(cs, 2, &to);
2601 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2604 if (who->pvt->pvt == op0)
2605 op0->ignoredtmf = 1;
2606 else if (who->pvt->pvt == op1)
2607 op1->ignoredtmf = 1;
2609 if (who->pvt->pvt == op0)
2610 op0->ignoredtmf = 0;
2611 else if (who->pvt->pvt == op1)
2612 op1->ignoredtmf = 0;
2616 if (slave && master)
2617 zt_unlink(slave, master, 1);
2624 if (f->frametype == AST_FRAME_DTMF) {
2625 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2626 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2629 if (slave && master)
2630 zt_unlink(slave, master, 1);
2632 } else if ((who == c0) && p0->pulsedial) {
2634 } else if ((who == c1) && p1->pulsedial) {
2640 /* Swap who gets priority */
2647 static int zt_indicate(struct ast_channel *chan, int condition);
2649 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2651 struct zt_pvt *p = newchan->pvt->pvt;
2653 ast_mutex_lock(&p->lock);
2654 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2655 if (p->owner == oldchan) {
2659 if (p->subs[x].owner == oldchan) {
2661 zt_unlink(NULL, p, 0);
2662 p->subs[x].owner = newchan;
2664 if (newchan->_state == AST_STATE_RINGING)
2665 zt_indicate(newchan, AST_CONTROL_RINGING);
2667 ast_mutex_unlock(&p->lock);
2671 static int zt_ring_phone(struct zt_pvt *p)
2675 /* Make sure our transmit state is on hook */
2678 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2681 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2683 printf("Res: %d, error: %s\n", res, strerror(errno));
2689 /* Wait just in case */
2696 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2704 static void *ss_thread(void *data);
2706 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2708 static int attempt_transfer(struct zt_pvt *p)
2710 /* In order to transfer, we need at least one of the channels to
2711 actually be in a call bridge. We can't conference two applications
2712 together (but then, why would we want to?) */
2713 if (p->subs[SUB_REAL].owner->bridge) {
2714 /* The three-way person we're about to transfer to could still be in MOH, so
2715 stop if now if appropriate */
2716 if (p->subs[SUB_THREEWAY].owner->bridge)
2717 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2718 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2719 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2721 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2722 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2723 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2726 /* Orphan the channel */
2727 unalloc_sub(p, SUB_THREEWAY);
2728 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2729 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2730 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2732 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2733 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2734 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2735 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2738 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2739 unalloc_sub(p, SUB_THREEWAY);
2740 /* Tell the caller not to hangup */
2743 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2744 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2745 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2751 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2753 struct ast_frame *f;
2754 f = &p->subs[index].f;
2756 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2760 case MFCR2_EVENT_BLOCKED:
2761 if (option_verbose > 2)
2762 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2764 case MFCR2_EVENT_UNBLOCKED:
2765 if (option_verbose > 2)
2766 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2768 case MFCR2_EVENT_CONFIG_ERR:
2769 if (option_verbose > 2)
2770 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2772 case MFCR2_EVENT_RING:
2773 if (option_verbose > 2)
2774 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2776 case MFCR2_EVENT_HANGUP:
2777 if (option_verbose > 2)
2778 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2780 case MFCR2_EVENT_RINGING:
2781 if (option_verbose > 2)
2782 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2784 case MFCR2_EVENT_ANSWER:
2785 if (option_verbose > 2)
2786 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2788 case MFCR2_EVENT_HANGUP_ACK:
2789 if (option_verbose > 2)
2790 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2792 case MFCR2_EVENT_IDLE:
2793 if (option_verbose > 2)
2794 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2797 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2803 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2808 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2810 ast_log(LOG_WARNING, "Unable to check received bits\n");
2814 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2817 e = mfcr2_cas_signaling_event(p->r2, x);
2822 static int check_for_conference(struct zt_pvt *p)
2825 /* Fine if we already have a master, etc */
2826 if (p->master || (p->confno > -1))
2828 memset(&ci, 0, sizeof(ci));
2829 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2830 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2833 /* If we have no master and don't have a confno, then
2834 if we're in a conference, it's probably a MeetMe room or
2835 some such, so don't let us 3-way out! */
2836 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
2837 if (option_verbose > 2)
2838 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
2844 static int get_alarms(struct zt_pvt *p)
2848 memset(&zi, 0, sizeof(zi));
2849 zi.spanno = p->span;
2850 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
2852 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
2858 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
2863 struct zt_pvt *p = ast->pvt->pvt;
2865 pthread_attr_t attr;
2866 struct ast_channel *chan;
2867 pthread_attr_init(&attr);
2868 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2869 index = zt_get_index(ast, p, 0);
2870 p->subs[index].f.frametype = AST_FRAME_NULL;
2871 p->subs[index].f.datalen = 0;
2872 p->subs[index].f.samples = 0;
2873 p->subs[index].f.mallocd = 0;
2874 p->subs[index].f.offset = 0;
2875 p->subs[index].f.src = "zt_handle_event";
2876 p->subs[index].f.data = NULL;
2878 return &p->subs[index].f;
2879 if (p->fake_event) {
2880 res = p->fake_event;
2883 res = zt_get_event(p->subs[index].zfd);
2884 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
2885 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
2886 if (res & ZT_EVENT_PULSEDIGIT)
2890 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
2891 p->subs[index].f.frametype = AST_FRAME_DTMF;
2892 p->subs[index].f.subclass = res & 0xff;
2893 /* Return the captured digit */
2894 return &p->subs[index].f;
2897 case ZT_EVENT_BITSCHANGED:
2898 if (p->sig == SIG_R2) {
2900 struct ast_frame *f = &p->subs[index].f;
2902 e = r2_get_event_bits(p);
2904 f = handle_r2_event(p, e, index);
2910 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
2911 case ZT_EVENT_PULSE_START:
2912 /* Stop tone if there's a pulse start and the PBX isn't started */
2914 tone_zone_play_tone(p->subs[index].zfd, -1);
2916 case ZT_EVENT_DIALCOMPLETE:
2917 if (p->inalarm) break;
2918 if (p->radio) break;
2919 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
2920 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
2923 if (!x) { /* if not still dialing in driver */
2927 strcpy(p->dop.dialstr, p->echorest);
2928 p->dop.op = ZT_DIAL_OP_REPLACE;
2929 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
2933 if (p->sig == SIG_E911) {
2934 /* if thru with dialing after offhook */
2935 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
2936 ast_setstate(ast, AST_STATE_UP);
2937 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2938 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2940 } else { /* if to state wait for offhook to dial rest */
2941 /* we now wait for off hook */
2942 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
2945 if (ast->_state == AST_STATE_DIALING) {
2946 if (p->callprogress && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
2947 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
2948 } 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)))) {
2949 ast_setstate(ast, AST_STATE_RINGING);
2951 ast_setstate(ast, AST_STATE_UP);
2952 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2953 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2959 case ZT_EVENT_ALARM:
2962 if (p->pri && p->pri->pri) {
2963 pri_hangup(p->pri->pri, p->call, -1);
2964 pri_destroycall(p->pri->pri, p->call);
2966 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");