2 * Asterisk -- A telephony toolkit for Linux.
4 * Zaptel Pseudo TDM interface
6 * Copyright (C) 2003 Digium
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #include <asterisk/lock.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <asterisk/file.h>
25 #include <asterisk/ulaw.h>
26 #include <asterisk/alaw.h>
27 #include <asterisk/callerid.h>
28 #include <asterisk/adsi.h>
29 #include <asterisk/cli.h>
30 #include <asterisk/cdr.h>
31 #include <asterisk/parking.h>
32 #include <asterisk/musiconhold.h>
33 #include <asterisk/say.h>
34 #include <asterisk/tdd.h>
35 #include <asterisk/app.h>
36 #include <asterisk/dsp.h>
37 #include <asterisk/astdb.h>
38 #include <sys/signal.h>
39 #include <sys/select.h>
44 #include <sys/ioctl.h>
45 #include <linux/zaptel.h>
56 #include "../asterisk.h"
60 XXX We definitely need to lock the private structure in zt_read and such
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 (0x10000 | ZT_SIG_EM)
108 #define SIG_FEATD (0x20000 | ZT_SIG_EM)
109 #define SIG_FEATDMF (0x40000 | ZT_SIG_EM)
110 #define SIG_FEATB (0x80000 | ZT_SIG_EM)
111 #define SIG_FXSLS ZT_SIG_FXSLS
112 #define SIG_FXSGS ZT_SIG_FXSGS
113 #define SIG_FXSKS ZT_SIG_FXSKS
114 #define SIG_FXOLS ZT_SIG_FXOLS
115 #define SIG_FXOGS ZT_SIG_FXOGS
116 #define SIG_FXOKS ZT_SIG_FXOKS
117 #define SIG_PRI ZT_SIG_CLEAR
118 #define SIG_R2 ZT_SIG_CAS
119 #define SIG_SF ZT_SIG_SF
120 #define SIG_SFWINK (0x10000 | ZT_SIG_SF)
121 #define SIG_SF_FEATD (0x20000 | ZT_SIG_SF)
122 #define SIG_SF_FEATDMF (0x40000 | ZT_SIG_SF)
123 #define SIG_SF_FEATB (0x80000 | ZT_SIG_SF)
126 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
128 #define CHAN_PSEUDO -2
130 static char context[AST_MAX_EXTENSION] = "default";
131 static char callerid[256] = "";
133 static char language[MAX_LANGUAGE] = "";
134 static char musicclass[MAX_LANGUAGE] = "";
136 static int use_callerid = 1;
138 static int cur_signalling = -1;
140 static unsigned int cur_group = 0;
141 static unsigned int cur_callergroup = 0;
142 static unsigned int cur_pickupgroup = 0;
143 static int relaxdtmf = 0;
145 static int immediate = 0;
147 static int stripmsd = 0;
149 static int callwaiting = 0;
151 static int callwaitingcallerid = 0;
153 static int hidecallerid = 0;
155 static int callreturn = 0;
157 static int threewaycalling = 0;
159 static int transfer = 0;
161 static int cancallforward = 0;
163 static float rxgain = 0.0;
165 static float txgain = 0.0;
167 static int echocancel;
169 static int echocanbridged = 0;
171 static int busydetect = 0;
173 static int busycount = 3;
175 static int callprogress = 0;
177 static char accountcode[20] = "";
179 static char mailbox[AST_MAX_EXTENSION];
181 static int amaflags = 0;
186 static int minunused = 2;
187 static int minidle = 0;
188 static char idleext[AST_MAX_EXTENSION];
189 static char idledial[AST_MAX_EXTENSION];
190 static int overlapdial = 0;
193 /* Wait up to 16 seconds for first digit (FXO logic) */
194 static int firstdigittimeout = 16000;
196 /* How long to wait for following digits (FXO logic) */
197 static int gendigittimeout = 8000;
199 /* How long to wait for an extra digit, if there is an ambiguous match */
200 static int matchdigittimeout = 3000;
202 static int usecnt =0;
203 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
205 /* Protect the interface list (of zt_pvt's) */
206 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
208 /* Protect the monitoring thread, so only one process can kill or start it, and not
209 when it's doing something critical. */
210 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
212 /* This is the thread for the monitor which checks for input on the channels
213 which are not currently in use. */
214 static pthread_t monitor_thread = 0;
216 static int restart_monitor(void);
218 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
220 static int zt_sendtext(struct ast_channel *c, char *text);
222 static inline int zt_get_event(int fd)
224 /* Avoid the silly zt_getevent which ignores a bunch of events */
226 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
230 static inline int zt_wait_event(int fd)
232 /* Avoid the silly zt_waitevent which ignores a bunch of events */
234 i = ZT_IOMUX_SIGEVENT;
235 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
236 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
240 /* Chunk size to read -- we use 20ms chunks to make things happy. */
241 #define READ_SIZE 160
243 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
244 #define MASK_INUSE (1 << 1) /* Channel currently in use */
246 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
247 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
248 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
249 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
250 #define RINGT ( (8000 * 8) / READ_SIZE)
256 static int r2prot = -1;
262 pthread_t master; /* Thread of master */
263 ast_mutex_t lock; /* Mutex */
264 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
265 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
266 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
267 int minunused; /* Min # of channels to keep empty */
268 int minidle; /* Min # of "idling" calls to keep active */
269 int nodetype; /* Node type */
270 int switchtype; /* Type of switch to emulate */
271 int dialplan; /* Dialing plan */
272 int dchannel; /* What channel the dchannel is on */
273 int channels; /* Num of chans in span (31 or 24) */
274 int overlapdial; /* In overlap dialing mode */
281 int chanmask[32]; /* Channel status */
285 struct zt_pvt *pvt[32]; /* Member channel pvt structs */
286 struct zt_channel *chan[32]; /* Channels on each line */
290 static struct zt_pri pris[NUM_SPANS];
292 static int pritype = PRI_CPE;
295 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
297 #define DEFAULT_PRI_DEBUG 0
300 static inline void pri_rel(struct zt_pri *pri)
302 ast_mutex_unlock(&pri->lock);
305 static int switchtype = PRI_SWITCH_NI2;
306 static int dialplan = PRI_NATIONAL_ISDN + 1;
310 #define SUB_REAL 0 /* Active call */
311 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
312 #define SUB_THREEWAY 2 /* Three-way call */
314 static char *subnames[] = {
320 struct zt_subchannel {
322 struct ast_channel *owner;
324 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
325 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
331 int curconfno; /* What conference we're currently in */
334 #define CONF_USER_REAL (1 << 0)
335 #define CONF_USER_THIRDCALL (1 << 1)
339 static struct zt_pvt {
341 struct ast_channel *owner; /* Our current active owner (if applicable) */
342 /* Up to three channels can be associated with this call */
344 struct zt_subchannel sub_unused; /* Just a safety precaution */
345 struct zt_subchannel subs[3]; /* Sub-channels */
346 struct zt_confinfo saveconf; /* Saved conference info */
348 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
349 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
350 int inconference; /* If our real should be in the conference */
352 int sig; /* Signalling style */
353 int radio; /* radio type */
354 int firstradio; /* first radio flag */
357 struct zt_pvt *next; /* Next channel in list */
358 struct zt_pvt *prev; /* Prev channel in list */
359 char context[AST_MAX_EXTENSION];
360 char exten[AST_MAX_EXTENSION];
361 char language[MAX_LANGUAGE];
362 char musicclass[MAX_LANGUAGE];
363 char callerid[AST_MAX_EXTENSION];
364 char lastcallerid[AST_MAX_EXTENSION];
365 char callwaitcid[AST_MAX_EXTENSION];
366 char rdnis[AST_MAX_EXTENSION];
369 int confno; /* Our conference */
370 int confusers; /* Who is using our conference */
371 int propconfno; /* Propagated conference number */
372 unsigned int callgroup;
373 unsigned int pickupgroup;
374 int immediate; /* Answer before getting digits? */
375 int channel; /* Channel Number */
376 int span; /* Span number */
379 int use_callerid; /* Whether or not to use caller id on this channel */
382 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
383 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
384 int cidcwexpire; /* When to expire our muting for CID/CW */
385 unsigned char *cidspill;
397 int callwaitingcallerid;
406 struct timeval flashtime; /* Last flash-hook time */
408 int cref; /* Call reference number */
409 ZT_DIAL_OPERATION dop;
413 char accountcode[20]; /* Account code */
414 int amaflags; /* AMA Flags */
415 char didtdd; /* flag to say its done it once */
416 struct tdd_state *tdd; /* TDD flag */
419 char call_forward[AST_MAX_EXTENSION];
420 char mailbox[AST_MAX_EXTENSION];
424 int confirmanswer; /* Wait for '#' to confirm answer */
425 int distinctivering; /* Which distinctivering to use */
426 int cidrings; /* Which ring to deliver CID on */
428 int faxhandled; /* Has a fax tone already been handled? */
430 char mate; /* flag to say its in MATE mode */
431 int pulsedial; /* whether a pulse dial phone is detected */
432 int dtmfrelax; /* whether to run in relaxed DTMF mode */
440 #ifdef PRI_EVENT_PROCEEDING
443 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
452 } *iflist = NULL, *ifend = NULL;
455 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
458 /* Grab the lock first */
460 res = ast_mutex_trylock(&pri->lock);
462 ast_mutex_unlock(&pvt->lock);
463 /* Release the lock and try again */
465 ast_mutex_lock(&pvt->lock);
468 /* Then break the select */
469 pthread_kill(pri->master, SIGURG);
474 static struct zt_ring_cadence cadences[] = {
475 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
476 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
477 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
478 { { 1000, 500, 2500, 5000 } }, /* Long ring */
481 static int cidrings[] = {
482 2, /* Right after first long ring */
483 4, /* Right after long part */
484 3, /* After third chirp */
485 2, /* Second spell */
488 #define NUM_CADENCE (sizeof(cadences) / sizeof(cadences[0]))
490 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
491 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
493 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
494 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
496 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
499 if (p->subs[0].owner == ast)
501 else if (p->subs[1].owner == ast)
503 else if (p->subs[2].owner == ast)
508 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
513 static void swap_subs(struct zt_pvt *p, int a, int b)
517 struct ast_channel *towner;
518 struct ast_frame null = { AST_FRAME_NULL, };
520 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
522 tchan = p->subs[a].chan;
523 towner = p->subs[a].owner;
524 tinthreeway = p->subs[a].inthreeway;
526 p->subs[a].chan = p->subs[b].chan;
527 p->subs[a].owner = p->subs[b].owner;
528 p->subs[a].inthreeway = p->subs[b].inthreeway;
530 p->subs[b].chan = tchan;
531 p->subs[b].owner = towner;
532 p->subs[b].inthreeway = tinthreeway;
534 if (p->subs[a].owner) {
535 p->subs[a].owner->fds[0] = p->subs[a].zfd;
536 ast_queue_frame(p->subs[a].owner, &null, 0);
538 if (p->subs[b].owner) {
539 p->subs[b].owner->fds[0] = p->subs[b].zfd;
540 ast_queue_frame(p->subs[b].owner, &null, 0);
545 static int zt_open(char *fn)
553 for (x=0;x<strlen(fn);x++) {
554 if (!isdigit(fn[x])) {
562 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
565 fn = "/dev/zap/channel";
567 fd = open(fn, O_RDWR | O_NONBLOCK);
569 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
573 if (ioctl(fd, ZT_SPECIFY, &chan)) {
577 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
582 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
586 static void zt_close(int fd)
591 int zt_setlinear(int zfd, int linear)
594 res = ioctl(zfd, ZT_SETLINEAR, &linear);
601 int zt_setlaw(int zfd, int law)
604 res = ioctl(zfd, ZT_SETLAW, &law);
610 static int alloc_sub(struct zt_pvt *p, int x)
614 if (p->subs[x].zfd < 0) {
615 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
616 if (p->subs[x].zfd > -1) {
617 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
619 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
620 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
622 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
624 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
627 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
628 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
629 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
630 zt_close(p->subs[x].zfd);
635 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
638 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
641 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
645 static int unalloc_sub(struct zt_pvt *p, int x)
648 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
651 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
652 if (p->subs[x].zfd > -1) {
653 zt_close(p->subs[x].zfd);
656 p->subs[x].linear = 0;
658 p->subs[x].owner = NULL;
659 p->subs[x].inthreeway = 0;
660 p->subs[x].curconfno = -1;
664 static int zt_digit(struct ast_channel *ast, char digit)
666 ZT_DIAL_OPERATION zo;
671 index = zt_get_index(ast, p, 0);
672 if (index == SUB_REAL) {
674 #ifdef PRI_EVENT_SETUP_ACK
675 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && p->setup_ack && !p->proceeding) {
677 #ifdef PRI_EVENT_PROCEEDING
678 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && !p->proceeding) {
680 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING) {
683 if (!pri_grab(p, p->pri))
684 pri_information(p->pri->pri,p->call,digit);
686 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
692 zo.op = ZT_DIAL_OP_APPEND;
694 zo.dialstr[1] = digit;
696 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
697 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
706 static char *events[] = {
719 "Hook Transition Complete",
728 { ZT_ALARM_RED, "Red Alarm" },
729 { ZT_ALARM_YELLOW, "Yellow Alarm" },
730 { ZT_ALARM_BLUE, "Blue Alarm" },
731 { ZT_ALARM_RECOVER, "Recovering" },
732 { ZT_ALARM_LOOPBACK, "Loopback" },
733 { ZT_ALARM_NOTOPEN, "Not Open" },
734 { ZT_ALARM_NONE, "None" },
737 static char *alarm2str(int alarm)
740 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
741 if (alarms[x].alarm & alarm)
742 return alarms[x].name;
744 return alarm ? "Unknown Alarm" : "No Alarm";
747 static char *event2str(int event)
749 static char buf[256];
750 if ((event < 15) && (event > -1))
751 return events[event];
752 sprintf(buf, "Event %d", event);
757 static int str2r2prot(char *swtype)
759 if (!strcasecmp(swtype, "ar"))
760 return MFCR2_PROT_ARGENTINA;
762 if (!strcasecmp(swtype, "cn"))
763 return MFCR2_PROT_CHINA;
765 if (!strcasecmp(swtype, "kr"))
766 return MFCR2_PROT_KOREA;
772 static char *sig2str(int sig)
774 static char buf[256];
777 return "E & M Immediate";
781 return "Feature Group D (DTMF)";
783 return "Feature Group D (MF)";
785 return "Feature Group B (MF)";
787 return "FXS Loopstart";
789 return "FXS Groundstart";
791 return "FXS Kewlstart";
793 return "FXO Loopstart";
795 return "FXO Groundstart";
797 return "FXO Kewlstart";
799 return "PRI Signalling";
801 return "R2 Signalling";
803 return "SF (Tone) Signalling Immediate";
805 return "SF (Tone) Signalling Wink";
807 return "SF (Tone) Signalling with Feature Group D (DTMF)";
809 return "SF (Tone) Signallong with Feature Group D (MF)";
811 return "SF (Tone) Signalling with Feature Group B (MF)";
813 return "Pseudo Signalling";
815 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
820 static int conf_add(int *confno, struct zt_subchannel *c, int index)
822 /* If the conference already exists, and we're already in it
823 don't bother doing anything */
825 if ((*confno > 0) && (c->curconfno == *confno))
827 if (c->curconfno > 0) {
828 ast_log(LOG_WARNING, "Subchannel %d is already in conference %d, moving to %d\n", c->zfd, c->curconfno, *confno);
832 memset(&zi, 0, sizeof(zi));
836 /* Real-side and pseudo-side both participate in conference */
837 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
838 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
840 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
841 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
842 ast_log(LOG_WARNING, "Failed to add %d to conference %d\n", c->zfd, *confno);
845 c->curconfno = zi.confno;
847 ast_log(LOG_DEBUG, "Added %d to conference %d\n", c->zfd, *confno);
851 static int conf_del(int *confno, struct zt_subchannel *c, int index)
854 /* Can't delete from this conference if it's not 0 */
856 /* Can't delete if there's no zfd */
858 /* Don't delete from the conference if it's not our conference */
859 (*confno != c->curconfno)
860 /* Don't delete if we don't think it's conferenced at all (implied) */
862 memset(&zi, 0, sizeof(zi));
866 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
867 ast_log(LOG_WARNING, "Failed to drop %d from conference %d\n", c->zfd, *confno);
871 ast_log(LOG_DEBUG, "Removed %d from conference %d\n", c->zfd, *confno);
875 static int update_conf(struct zt_pvt *p)
879 /* Update conference state in a stateless fashion */
880 /* Start with the obvious, general stuff */
882 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
883 conf_add(&p->confno, &p->subs[x], x);
886 conf_del(&p->confno, &p->subs[x], x);
889 /* If we have a slave, add him to our conference now */
890 for (x=0;x<MAX_SLAVES;x++) {
892 conf_add(&p->confno, &p->slaves[x]->subs[SUB_REAL], SUB_REAL);
896 /* If we're supposed to be in there, do so now */
897 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
898 conf_add(&p->confno, &p->subs[SUB_REAL], SUB_REAL);
901 /* If we have a master, add ourselves to his conference */
903 conf_add(&p->master->confno, &p->subs[SUB_REAL], SUB_REAL);
905 /* Nobody is left (or should be left) in our conference.
909 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
913 static void zt_enable_ec(struct zt_pvt *p)
918 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
921 if (p && p->echocancel) {
922 if (p->sig == SIG_PRI) {
924 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
926 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
929 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
931 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
934 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
937 ast_log(LOG_DEBUG, "No echocancellation requested\n");
940 static void zt_disable_ec(struct zt_pvt *p)
946 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
948 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
950 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
955 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
962 if ((rxgain != 0.0) || (txgain != 0.0)) {
963 /* caluculate linear value of tx gain */
964 ltxgain = pow(10.0,txgain / 20.0);
965 /* caluculate linear value of rx gain */
966 lrxgain = pow(10.0,rxgain / 20.0);
967 if (law == ZT_LAW_ALAW) {
968 for (j=0;j<256;j++) {
969 k = (int)(((float)AST_ALAW(j)) * lrxgain);
970 if (k > 32767) k = 32767;
971 if (k < -32767) k = -32767;
972 g.rxgain[j] = AST_LIN2A(k);
973 k = (int)(((float)AST_ALAW(j)) * ltxgain);
974 if (k > 32767) k = 32767;
975 if (k < -32767) k = -32767;
976 g.txgain[j] = AST_LIN2A(k);
979 for (j=0;j<256;j++) {
980 k = (int)(((float)AST_MULAW(j)) * lrxgain);
981 if (k > 32767) k = 32767;
982 if (k < -32767) k = -32767;
983 g.rxgain[j] = AST_LIN2MU(k);
984 k = (int)(((float)AST_MULAW(j)) * ltxgain);
985 if (k > 32767) k = 32767;
986 if (k < -32767) k = -32767;
987 g.txgain[j] = AST_LIN2MU(k);
991 for (j=0;j<256;j++) {
998 return(ioctl(fd,ZT_SETGAINS,&g));
1001 static inline int zt_set_hook(int fd, int hs)
1005 res = ioctl(fd, ZT_HOOK, &x);
1008 if (errno == EINPROGRESS) return 0;
1009 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1014 static inline int zt_confmute(struct zt_pvt *p, int muted)
1018 if (p->sig == SIG_PRI) {
1020 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1022 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1024 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1026 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1030 static int save_conference(struct zt_pvt *p)
1032 struct zt_confinfo c;
1034 if (p->saveconf.confmode) {
1035 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1038 p->saveconf.chan = 0;
1039 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1041 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1042 p->saveconf.confmode = 0;
1047 c.confmode = ZT_CONF_NORMAL;
1048 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1050 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1054 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1058 static int restore_conference(struct zt_pvt *p)
1061 if (p->saveconf.confmode) {
1062 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1063 p->saveconf.confmode = 0;
1065 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1070 ast_log(LOG_DEBUG, "Restored conferencing\n");
1074 static int send_callerid(struct zt_pvt *p);
1076 int send_cwcidspill(struct zt_pvt *p)
1080 p->cidspill = malloc(MAX_CALLERID_SIZE);
1082 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1083 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
1084 /* Make sure we account for the end */
1085 p->cidlen += READ_SIZE * 4;
1088 if (option_verbose > 2)
1089 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
1094 static int has_voicemail(struct zt_pvt *p)
1097 return ast_app_has_voicemail(p->mailbox);
1100 static int send_callerid(struct zt_pvt *p)
1102 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1104 /* Take out of linear mode if necessary */
1105 if (p->subs[SUB_REAL].linear) {
1106 p->subs[SUB_REAL].linear = 0;
1107 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1109 while(p->cidpos < p->cidlen) {
1110 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1112 if (errno == EAGAIN)
1115 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1125 if (p->callwaitcas) {
1126 /* Wait for CID/CW to expire */
1127 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1129 restore_conference(p);
1133 static int zt_callwait(struct ast_channel *ast)
1135 struct zt_pvt *p = ast->pvt->pvt;
1136 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1138 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1141 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1145 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1146 if (!p->callwaitrings && p->callwaitingcallerid) {
1147 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1149 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1151 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1153 p->cidlen = 2400 + READ_SIZE * 4;
1158 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1164 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1166 struct zt_pvt *p = ast->pvt->pvt;
1172 strncpy(dest, rdest, sizeof(dest) - 1);
1173 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1174 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1178 if (p->radio) /* if a radio channel, up immediately */
1180 /* Special pseudo -- automatically up */
1181 ast_setstate(ast, AST_STATE_UP);
1184 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1185 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1187 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1194 if (p->owner == ast) {
1195 /* Normal ring, on hook */
1197 /* Don't send audio while on hook, until the call is answered */
1199 if (p->use_callerid) {
1200 /* Generate the Caller-ID spill if desired */
1202 ast_log(LOG_WARNING, "cidspill already exists??\n");
1205 p->cidspill = malloc(MAX_CALLERID_SIZE);
1208 p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
1212 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1214 /* Select proper cadence */
1215 if ((p->distinctivering > 0) && (p->distinctivering <= NUM_CADENCE)) {
1216 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1217 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1218 p->cidrings = cidrings[p->distinctivering - 1];
1220 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1221 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1222 p->cidrings = DEFAULT_CIDRINGS;
1226 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1227 c = strchr(dest, '/');
1230 if (c && (strlen(c) < p->stripmsd)) {
1231 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1235 p->dop.op = ZT_DIAL_OP_REPLACE;
1236 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1237 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1239 strcpy(p->dop.dialstr, "");
1242 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1243 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1248 /* Call waiting call */
1249 p->callwaitrings = 0;
1251 strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
1253 strcpy(p->callwaitcid, "");
1254 /* Call waiting tone instead */
1255 if (zt_callwait(ast))
1257 /* Make ring-back */
1258 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1259 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1263 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1265 strcpy(callerid, "");
1266 ast_callerid_parse(callerid, &n, &l);
1268 ast_shrink_phone_number(l);
1269 if (!ast_isphonenumber(l))
1273 strcpy(p->lastcallerid, l);
1275 strcpy(p->lastcallerid, "");
1276 ast_setstate(ast, AST_STATE_RINGING);
1277 index = zt_get_index(ast, p, 0);
1279 p->subs[index].needringing = 1;
1293 case SIG_SF_FEATDMF:
1295 c = strchr(dest, '/');
1300 if (strlen(c) < p->stripmsd) {
1301 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1305 /* Start the trunk */
1306 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1308 if (errno != EINPROGRESS) {
1309 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1313 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1314 p->dop.op = ZT_DIAL_OP_REPLACE;
1315 if (p->sig == SIG_FEATD) {
1316 if (ast->callerid) {
1317 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1318 ast_callerid_parse(callerid, &n, &l);
1320 ast_shrink_phone_number(l);
1321 if (!ast_isphonenumber(l))
1327 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1329 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1331 if (p->sig == SIG_FEATDMF) {
1332 if (ast->callerid) {
1333 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1334 ast_callerid_parse(callerid, &n, &l);
1336 ast_shrink_phone_number(l);
1337 if (!ast_isphonenumber(l))
1343 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1345 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1347 if (p->sig == SIG_FEATB) {
1348 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1350 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", c + p->stripmsd);
1352 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1354 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1355 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1359 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1361 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1362 ast_setstate(ast, AST_STATE_DIALING);
1366 c = strchr(dest, '/');
1371 if (ast->callerid && !p->hidecallerid) {
1372 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1373 ast_callerid_parse(callerid, &n, &l);
1375 ast_shrink_phone_number(l);
1376 if (!ast_isphonenumber(l))
1381 if (strlen(c) < p->stripmsd) {
1382 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1385 p->dop.op = ZT_DIAL_OP_REPLACE;
1386 s = strchr(c + p->stripmsd, 'w');
1388 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1391 strcpy(p->dop.dialstr, "");
1393 if (!(p->call = pri_new_call(p->pri->pri))) {
1394 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1397 if (pri_call(p->pri->pri, p->call, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1398 p->prioffset, p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1, l, p->pri->dialplan - 1, n,
1399 l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE,
1400 c + p->stripmsd, p->pri->dialplan - 1,
1401 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW))) {
1402 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1405 ast_setstate(ast, AST_STATE_DIALING);
1409 /* Special pseudo -- automatically up*/
1410 ast_setstate(ast, AST_STATE_UP);
1413 ast_log(LOG_DEBUG, "not yet implemented\n");
1419 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1429 for (i = 0; i < 3; i++) {
1430 if (cur->subs[i].owner) {
1436 prev->next = cur->next;
1438 prev->next->prev = prev;
1444 iflist->prev = NULL;
1448 if (cur->subs[SUB_REAL].zfd > -1) {
1449 zt_close(cur->subs[SUB_REAL].zfd);
1455 prev->next = cur->next;
1457 prev->next->prev = prev;
1463 iflist->prev = NULL;
1467 if (cur->subs[SUB_REAL].zfd > -1) {
1468 zt_close(cur->subs[SUB_REAL].zfd);
1476 static int zt_hangup(struct ast_channel *ast)
1480 static int restore_gains(struct zt_pvt *p);
1481 struct zt_pvt *p = ast->pvt->pvt;
1482 struct zt_pvt *tmp = NULL;
1483 struct zt_pvt *prev = NULL;
1487 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1488 if (!ast->pvt->pvt) {
1489 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1493 ast_mutex_lock(&p->lock);
1495 index = zt_get_index(ast, p, 1);
1497 if (p->sig == SIG_PRI) {
1499 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1507 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1510 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1511 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1515 /* Real channel, do some fixup */
1516 p->subs[index].owner = NULL;
1517 p->subs[index].needanswer = 0;
1518 p->subs[index].needringing = 0;
1519 p->subs[index].linear = 0;
1520 p->subs[index].needcallerid = 0;
1521 zt_setlinear(p->subs[index].zfd, 0);
1522 if (index == SUB_REAL) {
1523 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1524 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1525 if (p->subs[SUB_CALLWAIT].inthreeway) {
1526 /* We had flipped over to answer a callwait and now it's gone */
1527 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1528 /* Move to the call-wait, but un-own us until they flip back. */
1529 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1530 unalloc_sub(p, SUB_CALLWAIT);
1533 /* The three way hung up, but we still have a call wait */
1534 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1535 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1536 unalloc_sub(p, SUB_THREEWAY);
1537 if (p->subs[SUB_REAL].inthreeway) {
1538 /* This was part of a three way call. Immediately make way for
1540 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1541 p->owner = p->subs[SUB_REAL].owner;
1543 /* This call hasn't been completed yet... Set owner to NULL */
1544 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1547 p->subs[SUB_REAL].inthreeway = 0;
1549 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
1550 /* Move to the call-wait and switch back to them. */
1551 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1552 unalloc_sub(p, SUB_CALLWAIT);
1553 p->owner = p->subs[SUB_REAL].owner;
1554 if (p->subs[SUB_REAL].owner->bridge)
1555 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
1556 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
1557 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1558 unalloc_sub(p, SUB_THREEWAY);
1559 if (p->subs[SUB_REAL].inthreeway) {
1560 /* This was part of a three way call. Immediately make way for
1562 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1563 p->owner = p->subs[SUB_REAL].owner;
1565 /* This call hasn't been completed yet... Set owner to NULL */
1566 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1569 p->subs[SUB_REAL].inthreeway = 0;
1571 } else if (index == SUB_CALLWAIT) {
1572 /* Ditch the holding callwait call, and immediately make it availabe */
1573 if (p->subs[SUB_CALLWAIT].inthreeway) {
1574 /* This is actually part of a three way, placed on hold. Place the third part
1575 on music on hold now */
1576 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
1577 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
1578 p->subs[SUB_THREEWAY].inthreeway = 0;
1579 /* Make it the call wait now */
1580 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
1581 unalloc_sub(p, SUB_THREEWAY);
1583 unalloc_sub(p, SUB_CALLWAIT);
1584 } else if (index == SUB_THREEWAY) {
1585 if (p->subs[SUB_CALLWAIT].inthreeway) {
1586 /* The other party of the three way call is currently in a call-wait state.
1587 Start music on hold for them, and take the main guy out of the third call */
1588 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
1589 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
1590 p->subs[SUB_CALLWAIT].inthreeway = 0;
1592 p->subs[SUB_REAL].inthreeway = 0;
1593 /* If this was part of a three way call index, let us make
1594 another three way call */
1595 unalloc_sub(p, SUB_THREEWAY);
1597 /* This wasn't any sort of call, but how are we an index? */
1598 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
1603 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
1606 p->distinctivering = 0;
1607 p->confirmanswer = 0;
1613 p->onhooktime = time(NULL);
1614 #ifdef PRI_EVENT_PROCEEDING
1617 #ifdef PRI_EVENT_SETUP_ACK
1621 ast_dsp_free(p->dsp);
1625 law = ZT_LAW_DEFAULT;
1626 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
1628 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
1629 /* Perform low level hangup if no owner left */
1631 if (p->sig == SIG_PRI) {
1633 if (!pri_grab(p, p->pri)) {
1634 #ifndef NEW_PRI_HANGUP
1635 if (!p->alreadyhungup) {
1636 res = pri_disconnect(p->pri->pri, p->call, PRI_CAUSE_NORMAL_CLEARING);
1638 pri_release(p->pri->pri, p->call, -1);
1640 p->alreadyhungup = 0;
1644 #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
1646 if (p->alreadyhungup) {
1647 pri_hangup(p->pri->pri, p->call, -1);
1650 p->alreadyhungup = 1;
1651 pri_hangup(p->pri->pri, p->call, -1);
1655 ast_log(LOG_WARNING, "pri_disconnect failed\n");
1658 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1667 if (p->sig == SIG_R2) {
1669 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
1678 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
1680 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
1686 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
1689 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
1691 /* If they're off hook, try playing congestion */
1692 if (par.rxisoffhook)
1693 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
1695 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1699 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1706 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1707 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1711 p->callwaiting = p->permcallwaiting;
1712 p->hidecallerid = p->permhidecallerid;
1714 strcpy(p->rdnis, "");
1716 /* Restore data mode */
1717 if (p->sig == SIG_PRI) {
1719 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1726 p->callwaitingrepeat = 0;
1728 ast->pvt->pvt = NULL;
1729 ast_mutex_unlock(&p->lock);
1730 ast_mutex_lock(&usecnt_lock);
1733 ast_log(LOG_WARNING, "Usecnt < 0???\n");
1734 ast_mutex_unlock(&usecnt_lock);
1735 ast_update_use_count();
1736 if (option_verbose > 2)
1737 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
1739 ast_mutex_lock(&iflock);
1745 destroy_channel(prev, tmp, 0);
1753 ast_mutex_unlock(&iflock);
1757 static int zt_answer(struct ast_channel *ast)
1759 struct zt_pvt *p = ast->pvt->pvt;
1762 int oldstate = ast->_state;
1763 ast_setstate(ast, AST_STATE_UP);
1764 ast_mutex_lock(&p->lock);
1765 index = zt_get_index(ast, p, 0);
1768 /* nothing to do if a radio channel */
1770 ast_mutex_unlock(&p->lock);
1787 case SIG_SF_FEATDMF:
1792 /* Pick up the line */
1793 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
1794 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
1795 tone_zone_play_tone(p->subs[index].zfd, -1);
1797 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
1798 if (oldstate == AST_STATE_RINGING) {
1799 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
1800 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
1801 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1802 p->owner = p->subs[SUB_REAL].owner;
1808 /* Send a pri acknowledge */
1809 if (!pri_grab(p, p->pri)) {
1810 res = pri_answer(p->pri->pri, p->call, 0, 1);
1813 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1820 res = mfcr2_AnswerCall(p->r2, NULL);
1822 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
1826 ast_mutex_unlock(&p->lock);
1829 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
1832 ast_mutex_unlock(&p->lock);
1836 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
1841 struct zt_pvt *p = chan->pvt->pvt;
1844 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
1845 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
1851 if ((!cp) || (datalen < 1))
1857 case AST_OPTION_TONE_VERIFY:
1862 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
1863 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
1866 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
1867 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
1870 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
1871 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
1875 case AST_OPTION_TDD: /* turn on or off TDD */
1876 if (!*cp) { /* turn it off */
1877 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
1878 if (p->tdd) tdd_free(p->tdd);
1884 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
1885 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
1888 /* otherwise, turn it on */
1889 if (!p->didtdd) { /* if havent done it yet */
1890 unsigned char mybuf[41000],*buf;
1891 int size,res,fd,len;
1895 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
1896 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
1898 index = zt_get_index(chan, p, 0);
1900 ast_log(LOG_WARNING, "No index in TDD?\n");
1903 fd = p->subs[index].zfd;
1905 if (ast_check_hangup(chan)) return -1;
1907 if (size > READ_SIZE)
1913 res = ast_select(fd + 1,NULL,&wfds,&efds,NULL);
1915 ast_log(LOG_DEBUG, "select (for write) ret. 0 on channel %d\n", p->channel);
1918 /* if got exception */
1919 if (FD_ISSET(fd,&efds)) return -1;
1920 if (!FD_ISSET(fd,&wfds)) {
1921 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
1924 res = write(fd, buf, size);
1926 if (res == -1) return -1;
1927 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
1933 p->didtdd = 1; /* set to have done it now */
1935 if (*cp == 2) { /* Mate mode */
1936 if (p->tdd) tdd_free(p->tdd);
1941 if (!p->tdd) { /* if we dont have one yet */
1942 p->tdd = tdd_new(); /* allocate one */
1945 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
1948 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
1953 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
1956 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
1958 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
1961 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
1967 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
1970 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
1971 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
1978 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master)
1980 /* Unlink a specific slave or all slaves/masters from a given master */
1986 for (x=0;x<MAX_SLAVES;x++) {
1987 if (master->slaves[x]) {
1988 if (!slave || (master->slaves[x] == slave)) {
1989 /* Take slave out of the conference */
1990 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
1991 conf_del(&master->confno, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
1992 master->slaves[x]->master = NULL;
1993 master->slaves[x] = NULL;
1998 master->inconference = 0;
2001 if (master->master) {
2002 /* Take master out of the conference */
2003 conf_del(&master->master->confno, &master->subs[SUB_REAL], SUB_REAL);
2005 for (x=0;x<MAX_SLAVES;x++) {
2006 if (master->master->slaves[x] == master)
2007 master->master->slaves[x] = NULL;
2008 else if (master->master->slaves[x])
2012 master->master->inconference = 0;
2014 master->master = NULL;
2016 update_conf(master);
2019 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2021 if (!slave || !master) {
2022 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2025 for (x=0;x<MAX_SLAVES;x++) {
2026 if (!master->slaves[x]) {
2027 master->slaves[x] = slave;
2031 if (x >= MAX_SLAVES) {
2032 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2033 master->slaves[MAX_SLAVES - 1] = slave;
2036 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2037 slave->master = master;
2039 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2042 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2044 struct ast_channel *who = NULL, *cs[3];
2045 struct zt_pvt *p0, *p1, *op0, *op1;
2046 struct zt_pvt *master=NULL, *slave=NULL;
2047 struct ast_frame *f;
2052 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2053 int os1 = -1, os2 = -1;
2054 struct ast_channel *oc1, *oc2;
2056 /* if need DTMF, cant native bridge */
2057 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2061 /* cant do pseudo-channels here */
2062 if ((!p0->sig) || (!p1->sig)) return -2;
2064 ast_mutex_lock(&c0->lock);
2065 ast_mutex_lock(&c1->lock);
2066 op0 = p0 = c0->pvt->pvt;
2067 op1 = p1 = c1->pvt->pvt;
2070 oi1 = zt_get_index(c0, p0, 0);
2071 oi2 = zt_get_index(c1, p1, 0);
2074 if ((oi1 < 0) || (oi2 < 0))
2079 ast_mutex_lock(&p0->lock);
2080 if (ast_mutex_trylock(&p1->lock)) {
2081 /* Don't block, due to potential for deadlock */
2082 ast_mutex_unlock(&p0->lock);
2083 ast_mutex_unlock(&c0->lock);
2084 ast_mutex_unlock(&c1->lock);
2085 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2088 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2089 if (!p0->owner || !p1->owner) {
2090 /* Currently unowned -- Do nothing. */
2093 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2094 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2098 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2103 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2104 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,
2105 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2108 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2109 if (p1->subs[SUB_THREEWAY].inthreeway) {
2115 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2116 if (p0->subs[SUB_THREEWAY].inthreeway) {
2122 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2123 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2124 don't put us in anything */
2125 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2131 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2132 /* Same as previous */
2133 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2140 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2141 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2142 if (master && slave) {
2143 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2144 in an active threeway call with a channel that is ringing, we should
2145 indicate ringing. */
2146 if ((oi2 == SUB_THREEWAY) &&
2147 p1->subs[SUB_THREEWAY].inthreeway &&
2148 p1->subs[SUB_REAL].owner &&
2149 p1->subs[SUB_REAL].inthreeway &&
2150 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2151 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2152 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2153 os2 = p1->subs[SUB_REAL].owner->_state;
2155 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2156 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2158 if ((oi1 == SUB_THREEWAY) &&
2159 p0->subs[SUB_THREEWAY].inthreeway &&
2160 p0->subs[SUB_REAL].owner &&
2161 p0->subs[SUB_REAL].inthreeway &&
2162 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2163 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2164 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2165 os1 = p0->subs[SUB_REAL].owner->_state;
2167 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2168 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2170 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2171 if (!p0->echocanbridged || !p1->echocanbridged) {
2172 /* Disable echo cancellation if appropriate */
2177 zt_link(slave, master);
2178 master->inconference = inconf;
2179 } else if (!nothingok)
2180 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2184 t1 = p0->subs[SUB_REAL].inthreeway;
2185 t2 = p1->subs[SUB_REAL].inthreeway;
2187 ast_mutex_unlock(&p0->lock);
2188 ast_mutex_unlock(&p1->lock);
2190 ast_mutex_unlock(&c0->lock);
2191 ast_mutex_unlock(&c1->lock);
2193 /* Native bridge failed */
2194 if ((!master || !slave) && !nothingok) {
2206 /* Here's our main loop... Start by locking things, looking for private parts,
2207 and then balking if anything is wrong */
2208 ast_mutex_lock(&c0->lock);
2209 ast_mutex_lock(&c1->lock);
2213 i1 = zt_get_index(c0, p0, 1);
2215 i2 = zt_get_index(c1, p1, 1);
2216 ast_mutex_unlock(&c0->lock);
2217 ast_mutex_unlock(&c1->lock);
2218 if ((op0 != p0) || (op1 != p1) ||
2219 (ofd1 != c0->fds[0]) ||
2220 (ofd2 != c1->fds[0]) ||
2221 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2222 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2223 (oc1 != p0->owner) ||
2224 (oc2 != p1->owner) ||
2225 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2226 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2229 if (slave && master)
2230 zt_unlink(slave, master);
2231 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2232 op0->channel, oi1, op1->channel, oi2);
2240 who = ast_waitfor_n(cs, 2, &to);
2242 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2245 if (who->pvt->pvt == op0)
2246 op0->ignoredtmf = 1;
2247 else if (who->pvt->pvt == op1)
2248 op1->ignoredtmf = 1;
2250 if (who->pvt->pvt == op0)
2251 op0->ignoredtmf = 0;
2252 else if (who->pvt->pvt == op1)
2253 op1->ignoredtmf = 0;
2257 if (slave && master)
2258 zt_unlink(slave, master);
2265 if (f->frametype == AST_FRAME_DTMF) {
2266 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2267 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2270 if (slave && master)
2271 zt_unlink(slave, master);
2273 } else if ((who == c0) && p0->pulsedial) {
2275 } else if ((who == c1) && p1->pulsedial) {
2281 /* Swap who gets priority */
2288 static int zt_indicate(struct ast_channel *chan, int condition);
2290 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2292 struct zt_pvt *p = newchan->pvt->pvt;
2294 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2295 if (p->owner == oldchan)
2298 if (p->subs[x].owner == oldchan) {
2301 p->subs[x].owner = newchan;
2303 if (newchan->_state == AST_STATE_RINGING)
2304 zt_indicate(newchan, AST_CONTROL_RINGING);
2309 static int zt_ring_phone(struct zt_pvt *p)
2313 /* Make sure our transmit state is on hook */
2316 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2319 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2321 printf("Res: %d, error: %s\n", res, strerror(errno));
2327 /* Wait just in case */
2334 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2342 static void *ss_thread(void *data);
2344 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int);
2346 static int attempt_transfer(struct zt_pvt *p)
2348 /* In order to transfer, we need at least one of the channels to
2349 actually be in a call bridge. We can't conference two applications
2350 together (but then, why would we want to?) */
2351 if (p->subs[SUB_REAL].owner->bridge) {
2352 /* The three-way person we're about to transfer to could still be in MOH, so
2353 stop if now if appropriate */
2354 if (p->subs[SUB_THREEWAY].owner->bridge)
2355 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2356 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2357 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2359 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2360 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2361 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2364 /* Orphan the channel */
2365 unalloc_sub(p, SUB_THREEWAY);
2366 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2367 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2368 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2370 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2371 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2372 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2373 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2376 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2377 unalloc_sub(p, SUB_THREEWAY);
2378 /* Tell the caller not to hangup */
2381 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2382 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2383 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2389 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2391 struct ast_frame *f;
2392 f = &p->subs[index].f;
2394 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2398 case MFCR2_EVENT_BLOCKED:
2399 if (option_verbose > 2)
2400 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2402 case MFCR2_EVENT_UNBLOCKED:
2403 if (option_verbose > 2)
2404 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2406 case MFCR2_EVENT_CONFIG_ERR:
2407 if (option_verbose > 2)
2408 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2410 case MFCR2_EVENT_RING:
2411 if (option_verbose > 2)
2412 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2414 case MFCR2_EVENT_HANGUP:
2415 if (option_verbose > 2)
2416 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2418 case MFCR2_EVENT_RINGING:
2419 if (option_verbose > 2)
2420 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2422 case MFCR2_EVENT_ANSWER:
2423 if (option_verbose > 2)
2424 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2426 case MFCR2_EVENT_HANGUP_ACK:
2427 if (option_verbose > 2)
2428 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2430 case MFCR2_EVENT_IDLE:
2431 if (option_verbose > 2)
2432 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2435 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2441 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2446 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2448 ast_log(LOG_WARNING, "Unable to check received bits\n");
2452 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2455 e = mfcr2_cas_signaling_event(p->r2, x);
2460 static int check_for_conference(struct zt_pvt *p)
2463 /* Fine if we already have a master, etc */
2464 if (p->master || (p->confno > -1))
2466 memset(&ci, 0, sizeof(ci));
2467 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2468 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2471 /* If we have no master and don't have a confno, then
2472 if we're in a conference, it's probably a MeetMe room or
2473 some such, so don't let us 3-way out! */
2475 if (option_verbose > 2)
2476 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
2482 static int get_alarms(struct zt_pvt *p)
2486 memset(&zi, 0, sizeof(zi));
2487 zi.spanno = p->span;
2488 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
2490 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
2496 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
2500 struct zt_pvt *p = ast->pvt->pvt;
2502 pthread_attr_t attr;
2503 struct ast_channel *chan;
2504 pthread_attr_init(&attr);
2505 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2506 index = zt_get_index(ast, p, 0);
2507 p->subs[index].f.frametype = AST_FRAME_NULL;
2508 p->subs[index].f.datalen = 0;
2509 p->subs[index].f.samples = 0;
2510 p->subs[index].f.mallocd = 0;
2511 p->subs[index].f.offset = 0;
2512 p->subs[index].f.src = "zt_handle_event";
2513 p->subs[index].f.data = NULL;
2515 return &p->subs[index].f;
2516 res = zt_get_event(p->subs[index].zfd);
2517 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
2518 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
2519 if (res & ZT_EVENT_PULSEDIGIT)
2523 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
2524 p->subs[index].f.frametype = AST_FRAME_DTMF;
2525 p->subs[index].f.subclass = res & 0xff;
2526 /* Return the captured digit */
2527 return &p->subs[index].f;
2530 case ZT_EVENT_BITSCHANGED:
2531 if (p->sig == SIG_R2) {
2533 struct ast_frame *f = &p->subs[index].f;
2535 e = r2_get_event_bits(p);
2537 f = handle_r2_event(p, e, index);
2543 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
2544 case ZT_EVENT_PULSE_START:
2545 /* Stop tone if there's a pulse start and the PBX isn't started */
2547 tone_zone_play_tone(p->subs[index].zfd, -1);
2549 case ZT_EVENT_DIALCOMPLETE:
2550 if (p->inalarm) break;
2551 if (p->radio) break;
2552 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
2553 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
2556 if (!x) { /* if not still dialing in driver */
2559 if (ast->_state == AST_STATE_DIALING) {
2560 if (p->callprogress && CANPROGRESSDETECT(p) && p->dsp) {
2561 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
2562 } else if (p->confirmanswer || (!p->dialednone && ((p->sig == SIG_EM) || (p->sig == SIG_EMWINK) || (p->sig == SIG_FEATD) || (p->sig == SIG_FEATDMF) || (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)))) {
2563 ast_setstate(ast, AST_STATE_RINGING);
2565 ast_setstate(ast, AST_STATE_UP);
2566 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2567 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2572 case ZT_EVENT_ALARM:
2574 #ifdef PRI_DESTROYCALL
2575 if (p->call && p->pri && p->pri->pri)
2576 pri_destroycall(p->pri->pri, p->call);
2578 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
2581 #error Please "cvs update" and recompile libpri
2585 res = get_alarms(p);
2586 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
2587 /* fall through intentionally */
2588 case ZT_EVENT_ONHOOK:
2591 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2592 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
2599 p->onhooktime = time(NULL);
2601 /* Check for some special conditions regarding call waiting */
2602 if (index == SUB_REAL) {
2603 /* The normal line was hung up */
2604 if (p->subs[SUB_CALLWAIT].owner) {
2605 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
2606 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2607 if (option_verbose > 2)
2608 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2609 unalloc_sub(p, SUB_CALLWAIT);
2611 p->subs[index].needanswer = 0;
2612 p->subs[index].needringing = 0;
2614 p->callwaitingrepeat = 0;
2618 } else if (p->subs[SUB_THREEWAY].owner) {
2620 unsigned int mssinceflash;
2621 gettimeofday(&tv, NULL);
2622 mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
2623 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
2624 if (mssinceflash < MIN_MS_SINCE_FLASH) {
2625 /* It hasn't been long enough since the last flashook. This is probably a bounce on
2626 hanging up. Hangup both channels now */
2627 if (p->subs[SUB_THREEWAY].owner)
2628 ast_queue_hangup(p->subs[SUB_THREEWAY].owner, 0);
2629 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2630 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
2631 } else if ((ast->pbx) ||
2632 (ast->_state == AST_STATE_UP)) {
2634 /* In any case this isn't a threeway call anymore */
2635 p->subs[SUB_REAL].inthreeway = 0;
2636 p->subs[SUB_THREEWAY].inthreeway = 0;
2637 if ((res = attempt_transfer(p)) < 0)
2638 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2640 /* Don't actually hang up at this point */
2644 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2646 /* Swap subs and dis-own channel */
2647 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2649 /* Ring the phone */
2654 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
2662 case ZT_EVENT_RINGOFFHOOK:
2663 if (p->inalarm) break;
2666 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2667 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
2674 switch(ast->_state) {
2675 case AST_STATE_RINGING:
2677 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2678 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2679 /* Make sure it stops ringing */
2680 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
2681 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
2683 /* Cancel any running CallerID spill */
2689 if (p->confirmanswer) {
2690 /* Ignore answer if "confirm answer" is selected */
2691 p->subs[index].f.frametype = AST_FRAME_NULL;
2692 p->subs[index].f.subclass = 0;
2693 } else if (strlen(p->dop.dialstr)) {
2694 /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
2695 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
2697 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
2698 p->dop.dialstr[0] = '\0';
2701 ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
2702 p->subs[index].f.frametype = AST_FRAME_NULL;
2703 p->subs[index].f.subclass = 0;
2706 p->dop.dialstr[0] = '\0';
2707 ast_setstate(ast, AST_STATE_DIALING);
2709 ast_setstate(ast, AST_STATE_UP);
2710 return &p->subs[index].f;
2711 case AST_STATE_DOWN:
2712 ast_setstate(ast, AST_STATE_RING);
2714 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2715 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
2716 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
2717 return &p->subs[index].f;
2719 /* Make sure it stops ringing */
2720 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
2721 /* Okay -- probably call waiting*/
2722 if (p->owner->bridge)
2723 ast_moh_stop(p->owner->bridge);
2726 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
2732 if (ast->_state == AST_STATE_RING) {
2744 case SIG_SF_FEATDMF:
2746 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
2748 ast_log(LOG_DEBUG, "Ring detected\n");
2749 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2750 p->subs[index].f.subclass = AST_CONTROL_RING;
2751 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
2753 ast_log(LOG_DEBUG, "Line answered\n");
2754 if (p->confirmanswer) {
2755 p->subs[index].f.frametype = AST_FRAME_NULL;
2756 p->subs[index].f.subclass = 0;
2758 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2759 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2760 ast_setstate(ast, AST_STATE_UP);
2762 } else if (ast->_state != AST_STATE_RING)
2763 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
2766 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
2769 case ZT_EVENT_RINGEROFF:
2770 if (p->inalarm) break;
2771 if (p->radio) break;
2773 if ((ast->rings > p->cidrings) && (p->cidspill)) {
2774 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
2779 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2780 p->subs[index].f.subclass = AST_CONTROL_RINGING;
2782 case ZT_EVENT_RINGERON:
2784 case ZT_EVENT_NOALARM:
2786 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
2788 case ZT_EVENT_WINKFLASH:
2789 if (p->inalarm) break;
2790 if (p->radio) break;
2791 /* Remember last time we got a flash-hook */
2792 gettimeofday(&p->flashtime, NULL);
2797 ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
2798 index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2800 if (index == SUB_REAL) {
2801 if (p->subs[SUB_CALLWAIT].owner) {
2802 /* Swap to call-wait */
2803 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
2804 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2805 p->owner = p->subs[SUB_REAL].owner;
2806 ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
2807 if (p->owner->_state == AST_STATE_RINGING) {
2808 ast_setstate(p->owner, AST_STATE_UP);
2809 p->subs[SUB_REAL].needanswer = 1;
2811 p->callwaitingrepeat = 0;
2813 /* Start music on hold if appropriate */
2814 if (!p->subs[SUB_CALLWAIT].inthreeway && p->subs[SUB_CALLWAIT].owner->bridge)
2815 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
2816 if (p->subs[SUB_REAL].owner->bridge)
2817 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
2818 } else if (!p->subs[SUB_THREEWAY].owner) {
2819 if (p->threewaycalling && !check_for_conference(p)) {
2820 /* XXX This section needs much more error checking!!! XXX */
2821 /* Start a 3-way call if feasible */
2823 (ast->_state == AST_STATE_UP) ||
2824 (ast->_state == AST_STATE_RING)) {
2825 if (!alloc_sub(p, SUB_THREEWAY)) {
2826 /* Make new channel */
2827 chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0);
2828 /* Swap things around between the three-way and real call */
2829 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2830 /* Disable echo canceller for better dialing */
2832 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
2834 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
2836 if (pthread_create(&threadid, &attr, ss_thread, chan)) {
2837 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
2838 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2842 if (option_verbose > 2)
2843 ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);
2844 /* Start music on hold if appropriate */
2845 if (p->subs[SUB_THREEWAY].owner->bridge)
2846 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);