2 * Asterisk -- A telephony toolkit for Linux.
4 * Zaptel Pseudo TDM interface
6 * Copyright (C) 2003 Digium
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #include <asterisk/lock.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <asterisk/file.h>
25 #include <asterisk/ulaw.h>
26 #include <asterisk/alaw.h>
27 #include <asterisk/callerid.h>
28 #include <asterisk/adsi.h>
29 #include <asterisk/cli.h>
30 #include <asterisk/cdr.h>
31 #include <asterisk/parking.h>
32 #include <asterisk/musiconhold.h>
33 #include <asterisk/say.h>
34 #include <asterisk/tdd.h>
35 #include <asterisk/app.h>
36 #include <asterisk/dsp.h>
37 #include <asterisk/astdb.h>
38 #include <asterisk/manager.h>
39 #include <sys/signal.h>
40 #include <sys/select.h>
45 #include <sys/ioctl.h>
46 #include <linux/zaptel.h>
57 #include "../asterisk.h"
61 XXX We definitely need to lock the private structure in zt_read and such
67 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
68 * the user hangs up to reset the state machine so ring works properly.
69 * This is used to be able to support kewlstart by putting the zhone in
70 * groundstart mode since their forward disconnect supervision is entirely
71 * broken even though their documentation says it isn't and their support
72 * is entirely unwilling to provide any assistance with their channel banks
73 * even though their web site says they support their products for life.
76 /* #define ZHONE_HACK */
78 /* Typically, how many rings before we should send Caller*ID */
79 #define DEFAULT_CIDRINGS 1
81 #define CHANNEL_PSEUDO -12
83 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
85 static char *desc = "Zapata Telephony"
94 static char *tdesc = "Zapata Telephony Driver"
103 static char *type = "Zap";
104 static char *typecompat = "Tor"; /* Retain compatibility with chan_tor */
105 static char *config = "zapata.conf";
107 #define SIG_EM ZT_SIG_EM
108 #define SIG_EMWINK (0x10000 | ZT_SIG_EM)
109 #define SIG_FEATD (0x20000 | ZT_SIG_EM)
110 #define SIG_FEATDMF (0x40000 | ZT_SIG_EM)
111 #define SIG_FEATB (0x80000 | ZT_SIG_EM)
112 #define SIG_FXSLS ZT_SIG_FXSLS
113 #define SIG_FXSGS ZT_SIG_FXSGS
114 #define SIG_FXSKS ZT_SIG_FXSKS
115 #define SIG_FXOLS ZT_SIG_FXOLS
116 #define SIG_FXOGS ZT_SIG_FXOGS
117 #define SIG_FXOKS ZT_SIG_FXOKS
118 #define SIG_PRI ZT_SIG_CLEAR
119 #define SIG_R2 ZT_SIG_CAS
120 #define SIG_SF ZT_SIG_SF
121 #define SIG_SFWINK (0x10000 | ZT_SIG_SF)
122 #define SIG_SF_FEATD (0x20000 | ZT_SIG_SF)
123 #define SIG_SF_FEATDMF (0x40000 | ZT_SIG_SF)
124 #define SIG_SF_FEATB (0x80000 | ZT_SIG_SF)
127 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
129 #define CHAN_PSEUDO -2
131 static char context[AST_MAX_EXTENSION] = "default";
132 static char callerid[256] = "";
134 static char language[MAX_LANGUAGE] = "";
135 static char musicclass[MAX_LANGUAGE] = "";
137 static int use_callerid = 1;
139 static int cur_signalling = -1;
141 static unsigned int cur_group = 0;
142 static unsigned int cur_callergroup = 0;
143 static unsigned int cur_pickupgroup = 0;
144 static int relaxdtmf = 0;
146 static int immediate = 0;
148 static int stripmsd = 0;
150 static int callwaiting = 0;
152 static int callwaitingcallerid = 0;
154 static int hidecallerid = 0;
156 static int callreturn = 0;
158 static int threewaycalling = 0;
160 static int transfer = 0;
162 static int cancallforward = 0;
164 static float rxgain = 0.0;
166 static float txgain = 0.0;
168 static int echocancel;
170 static int echocanbridged = 0;
172 static int busydetect = 0;
174 static int busycount = 3;
176 static int callprogress = 0;
178 static char accountcode[20] = "";
180 static char mailbox[AST_MAX_EXTENSION];
182 static int amaflags = 0;
187 static int minunused = 2;
188 static int minidle = 0;
189 static char idleext[AST_MAX_EXTENSION];
190 static char idledial[AST_MAX_EXTENSION];
191 static int overlapdial = 0;
194 /* Wait up to 16 seconds for first digit (FXO logic) */
195 static int firstdigittimeout = 16000;
197 /* How long to wait for following digits (FXO logic) */
198 static int gendigittimeout = 8000;
200 /* How long to wait for an extra digit, if there is an ambiguous match */
201 static int matchdigittimeout = 3000;
203 static int usecnt =0;
204 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
206 /* Protect the interface list (of zt_pvt's) */
207 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
209 /* Protect the monitoring thread, so only one process can kill or start it, and not
210 when it's doing something critical. */
211 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
213 /* This is the thread for the monitor which checks for input on the channels
214 which are not currently in use. */
215 static pthread_t monitor_thread = 0;
217 static int restart_monitor(void);
219 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
221 static int zt_sendtext(struct ast_channel *c, char *text);
223 static inline int zt_get_event(int fd)
225 /* Avoid the silly zt_getevent which ignores a bunch of events */
227 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
231 static inline int zt_wait_event(int fd)
233 /* Avoid the silly zt_waitevent which ignores a bunch of events */
235 i = ZT_IOMUX_SIGEVENT;
236 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
237 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
241 /* Chunk size to read -- we use 20ms chunks to make things happy. */
242 #define READ_SIZE 160
244 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
245 #define MASK_INUSE (1 << 1) /* Channel currently in use */
247 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
248 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
249 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
250 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
251 #define RINGT ( (8000 * 8) / READ_SIZE)
257 static int r2prot = -1;
263 pthread_t master; /* Thread of master */
264 ast_mutex_t lock; /* Mutex */
265 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
266 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
267 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
268 int minunused; /* Min # of channels to keep empty */
269 int minidle; /* Min # of "idling" calls to keep active */
270 int nodetype; /* Node type */
271 int switchtype; /* Type of switch to emulate */
272 int dialplan; /* Dialing plan */
273 int dchannel; /* What channel the dchannel is on */
274 int channels; /* Num of chans in span (31 or 24) */
275 int overlapdial; /* In overlap dialing mode */
282 int chanmask[32]; /* Channel status */
286 struct zt_pvt *pvt[32]; /* Member channel pvt structs */
287 struct zt_channel *chan[32]; /* Channels on each line */
291 static struct zt_pri pris[NUM_SPANS];
293 static int pritype = PRI_CPE;
296 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
298 #define DEFAULT_PRI_DEBUG 0
301 static inline void pri_rel(struct zt_pri *pri)
303 ast_mutex_unlock(&pri->lock);
306 static int switchtype = PRI_SWITCH_NI2;
307 static int dialplan = PRI_NATIONAL_ISDN + 1;
311 #define SUB_REAL 0 /* Active call */
312 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
313 #define SUB_THREEWAY 2 /* Three-way call */
315 static char *subnames[] = {
321 struct zt_subchannel {
323 struct ast_channel *owner;
325 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
326 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
332 int curconfno; /* What conference we're currently in */
335 #define CONF_USER_REAL (1 << 0)
336 #define CONF_USER_THIRDCALL (1 << 1)
340 static struct zt_pvt {
342 struct ast_channel *owner; /* Our current active owner (if applicable) */
343 /* Up to three channels can be associated with this call */
345 struct zt_subchannel sub_unused; /* Just a safety precaution */
346 struct zt_subchannel subs[3]; /* Sub-channels */
347 struct zt_confinfo saveconf; /* Saved conference info */
349 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
350 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
351 int inconference; /* If our real should be in the conference */
353 int sig; /* Signalling style */
354 int radio; /* radio type */
355 int firstradio; /* first radio flag */
358 struct zt_pvt *next; /* Next channel in list */
359 struct zt_pvt *prev; /* Prev channel in list */
360 char context[AST_MAX_EXTENSION];
361 char exten[AST_MAX_EXTENSION];
362 char language[MAX_LANGUAGE];
363 char musicclass[MAX_LANGUAGE];
364 char callerid[AST_MAX_EXTENSION];
365 char lastcallerid[AST_MAX_EXTENSION];
366 char callwaitcid[AST_MAX_EXTENSION];
367 char rdnis[AST_MAX_EXTENSION];
370 int confno; /* Our conference */
371 int confusers; /* Who is using our conference */
372 int propconfno; /* Propagated conference number */
373 unsigned int callgroup;
374 unsigned int pickupgroup;
375 int immediate; /* Answer before getting digits? */
376 int channel; /* Channel Number */
377 int span; /* Span number */
380 int use_callerid; /* Whether or not to use caller id on this channel */
383 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
384 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
385 int cidcwexpire; /* When to expire our muting for CID/CW */
386 unsigned char *cidspill;
398 int callwaitingcallerid;
407 struct timeval flashtime; /* Last flash-hook time */
409 int cref; /* Call reference number */
410 ZT_DIAL_OPERATION dop;
414 char accountcode[20]; /* Account code */
415 int amaflags; /* AMA Flags */
416 char didtdd; /* flag to say its done it once */
417 struct tdd_state *tdd; /* TDD flag */
420 char call_forward[AST_MAX_EXTENSION];
421 char mailbox[AST_MAX_EXTENSION];
425 int confirmanswer; /* Wait for '#' to confirm answer */
426 int distinctivering; /* Which distinctivering to use */
427 int cidrings; /* Which ring to deliver CID on */
429 int faxhandled; /* Has a fax tone already been handled? */
431 char mate; /* flag to say its in MATE mode */
432 int pulsedial; /* whether a pulse dial phone is detected */
433 int dtmfrelax; /* whether to run in relaxed DTMF mode */
441 #ifdef PRI_EVENT_PROCEEDING
444 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
453 } *iflist = NULL, *ifend = NULL;
456 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
459 /* Grab the lock first */
461 res = ast_mutex_trylock(&pri->lock);
463 ast_mutex_unlock(&pvt->lock);
464 /* Release the lock and try again */
466 ast_mutex_lock(&pvt->lock);
469 /* Then break the select */
470 pthread_kill(pri->master, SIGURG);
475 static struct zt_ring_cadence cadences[] = {
476 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
477 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
478 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
479 { { 1000, 500, 2500, 5000 } }, /* Long ring */
482 static int cidrings[] = {
483 2, /* Right after first long ring */
484 4, /* Right after long part */
485 3, /* After third chirp */
486 2, /* Second spell */
489 #define NUM_CADENCE (sizeof(cadences) / sizeof(cadences[0]))
491 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
492 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
494 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
495 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
497 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
500 if (p->subs[0].owner == ast)
502 else if (p->subs[1].owner == ast)
504 else if (p->subs[2].owner == ast)
509 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
514 static void swap_subs(struct zt_pvt *p, int a, int b)
518 struct ast_channel *towner;
519 struct ast_frame null = { AST_FRAME_NULL, };
521 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
523 tchan = p->subs[a].chan;
524 towner = p->subs[a].owner;
525 tinthreeway = p->subs[a].inthreeway;
527 p->subs[a].chan = p->subs[b].chan;
528 p->subs[a].owner = p->subs[b].owner;
529 p->subs[a].inthreeway = p->subs[b].inthreeway;
531 p->subs[b].chan = tchan;
532 p->subs[b].owner = towner;
533 p->subs[b].inthreeway = tinthreeway;
535 if (p->subs[a].owner) {
536 p->subs[a].owner->fds[0] = p->subs[a].zfd;
537 ast_queue_frame(p->subs[a].owner, &null, 0);
539 if (p->subs[b].owner) {
540 p->subs[b].owner->fds[0] = p->subs[b].zfd;
541 ast_queue_frame(p->subs[b].owner, &null, 0);
546 static int zt_open(char *fn)
554 for (x=0;x<strlen(fn);x++) {
555 if (!isdigit(fn[x])) {
563 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
566 fn = "/dev/zap/channel";
568 fd = open(fn, O_RDWR | O_NONBLOCK);
570 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
574 if (ioctl(fd, ZT_SPECIFY, &chan)) {
578 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
583 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
587 static void zt_close(int fd)
592 int zt_setlinear(int zfd, int linear)
595 res = ioctl(zfd, ZT_SETLINEAR, &linear);
602 int zt_setlaw(int zfd, int law)
605 res = ioctl(zfd, ZT_SETLAW, &law);
611 static int alloc_sub(struct zt_pvt *p, int x)
615 if (p->subs[x].zfd < 0) {
616 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
617 if (p->subs[x].zfd > -1) {
618 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
620 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
621 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
623 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
625 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
628 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
629 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
630 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
631 zt_close(p->subs[x].zfd);
636 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
639 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
642 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
646 static int unalloc_sub(struct zt_pvt *p, int x)
649 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
652 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
653 if (p->subs[x].zfd > -1) {
654 zt_close(p->subs[x].zfd);
657 p->subs[x].linear = 0;
659 p->subs[x].owner = NULL;
660 p->subs[x].inthreeway = 0;
661 p->subs[x].curconfno = -1;
665 static int zt_digit(struct ast_channel *ast, char digit)
667 ZT_DIAL_OPERATION zo;
672 index = zt_get_index(ast, p, 0);
673 if (index == SUB_REAL) {
675 #ifdef PRI_EVENT_SETUP_ACK
676 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && p->setup_ack && !p->proceeding) {
678 #ifdef PRI_EVENT_PROCEEDING
679 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && !p->proceeding) {
681 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING) {
684 if (!pri_grab(p, p->pri))
685 pri_information(p->pri->pri,p->call,digit);
687 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
693 zo.op = ZT_DIAL_OP_APPEND;
695 zo.dialstr[1] = digit;
697 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
698 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
707 static char *events[] = {
720 "Hook Transition Complete",
729 { ZT_ALARM_RED, "Red Alarm" },
730 { ZT_ALARM_YELLOW, "Yellow Alarm" },
731 { ZT_ALARM_BLUE, "Blue Alarm" },
732 { ZT_ALARM_RECOVER, "Recovering" },
733 { ZT_ALARM_LOOPBACK, "Loopback" },
734 { ZT_ALARM_NOTOPEN, "Not Open" },
735 { ZT_ALARM_NONE, "None" },
738 static char *alarm2str(int alarm)
741 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
742 if (alarms[x].alarm & alarm)
743 return alarms[x].name;
745 return alarm ? "Unknown Alarm" : "No Alarm";
748 static char *event2str(int event)
750 static char buf[256];
751 if ((event < 15) && (event > -1))
752 return events[event];
753 sprintf(buf, "Event %d", event);
758 static int str2r2prot(char *swtype)
760 if (!strcasecmp(swtype, "ar"))
761 return MFCR2_PROT_ARGENTINA;
763 if (!strcasecmp(swtype, "cn"))
764 return MFCR2_PROT_CHINA;
766 if (!strcasecmp(swtype, "kr"))
767 return MFCR2_PROT_KOREA;
773 static char *sig2str(int sig)
775 static char buf[256];
778 return "E & M Immediate";
782 return "Feature Group D (DTMF)";
784 return "Feature Group D (MF)";
786 return "Feature Group B (MF)";
788 return "FXS Loopstart";
790 return "FXS Groundstart";
792 return "FXS Kewlstart";
794 return "FXO Loopstart";
796 return "FXO Groundstart";
798 return "FXO Kewlstart";
800 return "PRI Signalling";
802 return "R2 Signalling";
804 return "SF (Tone) Signalling Immediate";
806 return "SF (Tone) Signalling Wink";
808 return "SF (Tone) Signalling with Feature Group D (DTMF)";
810 return "SF (Tone) Signallong with Feature Group D (MF)";
812 return "SF (Tone) Signalling with Feature Group B (MF)";
814 return "Pseudo Signalling";
816 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
821 static int conf_add(int *confno, struct zt_subchannel *c, int index)
823 /* If the conference already exists, and we're already in it
824 don't bother doing anything */
826 if ((*confno > 0) && (c->curconfno == *confno))
828 if (c->curconfno > 0) {
829 ast_log(LOG_WARNING, "Subchannel %d is already in conference %d, moving to %d\n", c->zfd, c->curconfno, *confno);
833 memset(&zi, 0, sizeof(zi));
837 /* Real-side and pseudo-side both participate in conference */
838 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
839 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
841 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
842 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
843 ast_log(LOG_WARNING, "Failed to add %d to conference %d\n", c->zfd, *confno);
846 c->curconfno = zi.confno;
848 ast_log(LOG_DEBUG, "Added %d to conference %d\n", c->zfd, *confno);
852 static int conf_del(int *confno, struct zt_subchannel *c, int index)
855 /* Can't delete from this conference if it's not 0 */
857 /* Can't delete if there's no zfd */
859 /* Don't delete from the conference if it's not our conference */
860 (*confno != c->curconfno)
861 /* Don't delete if we don't think it's conferenced at all (implied) */
863 memset(&zi, 0, sizeof(zi));
867 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
868 ast_log(LOG_WARNING, "Failed to drop %d from conference %d\n", c->zfd, *confno);
872 ast_log(LOG_DEBUG, "Removed %d from conference %d\n", c->zfd, *confno);
876 static int update_conf(struct zt_pvt *p)
880 /* Update conference state in a stateless fashion */
881 /* Start with the obvious, general stuff */
883 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
884 conf_add(&p->confno, &p->subs[x], x);
887 conf_del(&p->confno, &p->subs[x], x);
890 /* If we have a slave, add him to our conference now */
891 for (x=0;x<MAX_SLAVES;x++) {
893 conf_add(&p->confno, &p->slaves[x]->subs[SUB_REAL], SUB_REAL);
897 /* If we're supposed to be in there, do so now */
898 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
899 conf_add(&p->confno, &p->subs[SUB_REAL], SUB_REAL);
902 /* If we have a master, add ourselves to his conference */
904 conf_add(&p->master->confno, &p->subs[SUB_REAL], SUB_REAL);
906 /* Nobody is left (or should be left) in our conference.
910 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
914 static void zt_enable_ec(struct zt_pvt *p)
919 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
922 if (p && p->echocancel) {
923 if (p->sig == SIG_PRI) {
925 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
927 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
930 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
932 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
935 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
938 ast_log(LOG_DEBUG, "No echocancellation requested\n");
941 static void zt_disable_ec(struct zt_pvt *p)
947 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
949 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
951 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
956 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
963 if ((rxgain != 0.0) || (txgain != 0.0)) {
964 /* caluculate linear value of tx gain */
965 ltxgain = pow(10.0,txgain / 20.0);
966 /* caluculate linear value of rx gain */
967 lrxgain = pow(10.0,rxgain / 20.0);
968 if (law == ZT_LAW_ALAW) {
969 for (j=0;j<256;j++) {
970 k = (int)(((float)AST_ALAW(j)) * lrxgain);
971 if (k > 32767) k = 32767;
972 if (k < -32767) k = -32767;
973 g.rxgain[j] = AST_LIN2A(k);
974 k = (int)(((float)AST_ALAW(j)) * ltxgain);
975 if (k > 32767) k = 32767;
976 if (k < -32767) k = -32767;
977 g.txgain[j] = AST_LIN2A(k);
980 for (j=0;j<256;j++) {
981 k = (int)(((float)AST_MULAW(j)) * lrxgain);
982 if (k > 32767) k = 32767;
983 if (k < -32767) k = -32767;
984 g.rxgain[j] = AST_LIN2MU(k);
985 k = (int)(((float)AST_MULAW(j)) * ltxgain);
986 if (k > 32767) k = 32767;
987 if (k < -32767) k = -32767;
988 g.txgain[j] = AST_LIN2MU(k);
992 for (j=0;j<256;j++) {
999 return(ioctl(fd,ZT_SETGAINS,&g));
1002 static inline int zt_set_hook(int fd, int hs)
1006 res = ioctl(fd, ZT_HOOK, &x);
1009 if (errno == EINPROGRESS) return 0;
1010 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1015 static inline int zt_confmute(struct zt_pvt *p, int muted)
1019 if (p->sig == SIG_PRI) {
1021 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1023 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1025 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1027 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1031 static int save_conference(struct zt_pvt *p)
1033 struct zt_confinfo c;
1035 if (p->saveconf.confmode) {
1036 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1039 p->saveconf.chan = 0;
1040 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1042 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1043 p->saveconf.confmode = 0;
1048 c.confmode = ZT_CONF_NORMAL;
1049 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1051 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1055 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1059 static int restore_conference(struct zt_pvt *p)
1062 if (p->saveconf.confmode) {
1063 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1064 p->saveconf.confmode = 0;
1066 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1071 ast_log(LOG_DEBUG, "Restored conferencing\n");
1075 static int send_callerid(struct zt_pvt *p);
1077 int send_cwcidspill(struct zt_pvt *p)
1081 p->cidspill = malloc(MAX_CALLERID_SIZE);
1083 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1084 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
1085 /* Make sure we account for the end */
1086 p->cidlen += READ_SIZE * 4;
1089 if (option_verbose > 2)
1090 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
1095 static int has_voicemail(struct zt_pvt *p)
1098 return ast_app_has_voicemail(p->mailbox);
1101 static int send_callerid(struct zt_pvt *p)
1103 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1105 /* Take out of linear mode if necessary */
1106 if (p->subs[SUB_REAL].linear) {
1107 p->subs[SUB_REAL].linear = 0;
1108 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1110 while(p->cidpos < p->cidlen) {
1111 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1113 if (errno == EAGAIN)
1116 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1126 if (p->callwaitcas) {
1127 /* Wait for CID/CW to expire */
1128 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1130 restore_conference(p);
1134 static int zt_callwait(struct ast_channel *ast)
1136 struct zt_pvt *p = ast->pvt->pvt;
1137 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1139 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1142 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1146 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1147 if (!p->callwaitrings && p->callwaitingcallerid) {
1148 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1150 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1152 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1154 p->cidlen = 2400 + READ_SIZE * 4;
1159 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1165 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1167 struct zt_pvt *p = ast->pvt->pvt;
1173 strncpy(dest, rdest, sizeof(dest) - 1);
1174 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1175 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1179 if (p->radio) /* if a radio channel, up immediately */
1181 /* Special pseudo -- automatically up */
1182 ast_setstate(ast, AST_STATE_UP);
1185 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1186 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1188 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1195 if (p->owner == ast) {
1196 /* Normal ring, on hook */
1198 /* Don't send audio while on hook, until the call is answered */
1200 if (p->use_callerid) {
1201 /* Generate the Caller-ID spill if desired */
1203 ast_log(LOG_WARNING, "cidspill already exists??\n");
1206 p->cidspill = malloc(MAX_CALLERID_SIZE);
1209 p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
1213 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1215 /* Select proper cadence */
1216 if ((p->distinctivering > 0) && (p->distinctivering <= NUM_CADENCE)) {
1217 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1218 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1219 p->cidrings = cidrings[p->distinctivering - 1];
1221 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1222 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1223 p->cidrings = DEFAULT_CIDRINGS;
1227 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1228 c = strchr(dest, '/');
1231 if (c && (strlen(c) < p->stripmsd)) {
1232 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1236 p->dop.op = ZT_DIAL_OP_REPLACE;
1237 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1238 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1240 strcpy(p->dop.dialstr, "");
1243 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1244 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1249 /* Call waiting call */
1250 p->callwaitrings = 0;
1252 strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
1254 strcpy(p->callwaitcid, "");
1255 /* Call waiting tone instead */
1256 if (zt_callwait(ast))
1258 /* Make ring-back */
1259 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1260 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1264 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1266 strcpy(callerid, "");
1267 ast_callerid_parse(callerid, &n, &l);
1269 ast_shrink_phone_number(l);
1270 if (!ast_isphonenumber(l))
1274 strcpy(p->lastcallerid, l);
1276 strcpy(p->lastcallerid, "");
1277 ast_setstate(ast, AST_STATE_RINGING);
1278 index = zt_get_index(ast, p, 0);
1280 p->subs[index].needringing = 1;
1294 case SIG_SF_FEATDMF:
1296 c = strchr(dest, '/');
1301 if (strlen(c) < p->stripmsd) {
1302 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1306 /* Start the trunk */
1307 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1309 if (errno != EINPROGRESS) {
1310 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1314 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1315 p->dop.op = ZT_DIAL_OP_REPLACE;
1316 if (p->sig == SIG_FEATD) {
1317 if (ast->callerid) {
1318 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1319 ast_callerid_parse(callerid, &n, &l);
1321 ast_shrink_phone_number(l);
1322 if (!ast_isphonenumber(l))
1328 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1330 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1332 if (p->sig == SIG_FEATDMF) {
1333 if (ast->callerid) {
1334 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1335 ast_callerid_parse(callerid, &n, &l);
1337 ast_shrink_phone_number(l);
1338 if (!ast_isphonenumber(l))
1344 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1346 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1348 if (p->sig == SIG_FEATB) {
1349 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1351 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", c + p->stripmsd);
1353 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1355 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1356 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1360 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1362 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1363 ast_setstate(ast, AST_STATE_DIALING);
1367 c = strchr(dest, '/');
1372 if (ast->callerid && !p->hidecallerid) {
1373 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1374 ast_callerid_parse(callerid, &n, &l);
1376 ast_shrink_phone_number(l);
1377 if (!ast_isphonenumber(l))
1382 if (strlen(c) < p->stripmsd) {
1383 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1386 p->dop.op = ZT_DIAL_OP_REPLACE;
1387 s = strchr(c + p->stripmsd, 'w');
1389 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1392 strcpy(p->dop.dialstr, "");
1394 if (!(p->call = pri_new_call(p->pri->pri))) {
1395 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1398 if (pri_call(p->pri->pri, p->call, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1399 p->prioffset, p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1, l, p->pri->dialplan - 1, n,
1400 l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE,
1401 c + p->stripmsd, p->pri->dialplan - 1,
1402 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW))) {
1403 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1406 ast_setstate(ast, AST_STATE_DIALING);
1410 /* Special pseudo -- automatically up*/
1411 ast_setstate(ast, AST_STATE_UP);
1414 ast_log(LOG_DEBUG, "not yet implemented\n");
1420 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1430 for (i = 0; i < 3; i++) {
1431 if (cur->subs[i].owner) {
1437 prev->next = cur->next;
1439 prev->next->prev = prev;
1445 iflist->prev = NULL;
1449 if (cur->subs[SUB_REAL].zfd > -1) {
1450 zt_close(cur->subs[SUB_REAL].zfd);
1456 prev->next = cur->next;
1458 prev->next->prev = prev;
1464 iflist->prev = NULL;
1468 if (cur->subs[SUB_REAL].zfd > -1) {
1469 zt_close(cur->subs[SUB_REAL].zfd);
1477 static int zt_hangup(struct ast_channel *ast)
1481 static int restore_gains(struct zt_pvt *p);
1482 struct zt_pvt *p = ast->pvt->pvt;
1483 struct zt_pvt *tmp = NULL;
1484 struct zt_pvt *prev = NULL;
1488 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1489 if (!ast->pvt->pvt) {
1490 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1494 ast_mutex_lock(&p->lock);
1496 index = zt_get_index(ast, p, 1);
1498 if (p->sig == SIG_PRI) {
1500 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1508 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1511 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1512 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1516 /* Real channel, do some fixup */
1517 p->subs[index].owner = NULL;
1518 p->subs[index].needanswer = 0;
1519 p->subs[index].needringing = 0;
1520 p->subs[index].linear = 0;
1521 p->subs[index].needcallerid = 0;
1522 zt_setlinear(p->subs[index].zfd, 0);
1523 if (index == SUB_REAL) {
1524 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1525 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1526 if (p->subs[SUB_CALLWAIT].inthreeway) {
1527 /* We had flipped over to answer a callwait and now it's gone */
1528 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1529 /* Move to the call-wait, but un-own us until they flip back. */
1530 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1531 unalloc_sub(p, SUB_CALLWAIT);
1534 /* The three way hung up, but we still have a call wait */
1535 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1536 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1537 unalloc_sub(p, SUB_THREEWAY);
1538 if (p->subs[SUB_REAL].inthreeway) {
1539 /* This was part of a three way call. Immediately make way for
1541 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1542 p->owner = p->subs[SUB_REAL].owner;
1544 /* This call hasn't been completed yet... Set owner to NULL */
1545 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1548 p->subs[SUB_REAL].inthreeway = 0;
1550 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
1551 /* Move to the call-wait and switch back to them. */
1552 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1553 unalloc_sub(p, SUB_CALLWAIT);
1554 p->owner = p->subs[SUB_REAL].owner;
1555 if (p->subs[SUB_REAL].owner->bridge)
1556 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
1557 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
1558 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1559 unalloc_sub(p, SUB_THREEWAY);
1560 if (p->subs[SUB_REAL].inthreeway) {
1561 /* This was part of a three way call. Immediately make way for
1563 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1564 p->owner = p->subs[SUB_REAL].owner;
1566 /* This call hasn't been completed yet... Set owner to NULL */
1567 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1570 p->subs[SUB_REAL].inthreeway = 0;
1572 } else if (index == SUB_CALLWAIT) {
1573 /* Ditch the holding callwait call, and immediately make it availabe */
1574 if (p->subs[SUB_CALLWAIT].inthreeway) {
1575 /* This is actually part of a three way, placed on hold. Place the third part
1576 on music on hold now */
1577 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
1578 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
1579 p->subs[SUB_THREEWAY].inthreeway = 0;
1580 /* Make it the call wait now */
1581 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
1582 unalloc_sub(p, SUB_THREEWAY);
1584 unalloc_sub(p, SUB_CALLWAIT);
1585 } else if (index == SUB_THREEWAY) {
1586 if (p->subs[SUB_CALLWAIT].inthreeway) {
1587 /* The other party of the three way call is currently in a call-wait state.
1588 Start music on hold for them, and take the main guy out of the third call */
1589 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
1590 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
1591 p->subs[SUB_CALLWAIT].inthreeway = 0;
1593 p->subs[SUB_REAL].inthreeway = 0;
1594 /* If this was part of a three way call index, let us make
1595 another three way call */
1596 unalloc_sub(p, SUB_THREEWAY);
1598 /* This wasn't any sort of call, but how are we an index? */
1599 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
1604 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
1607 p->distinctivering = 0;
1608 p->confirmanswer = 0;
1614 p->onhooktime = time(NULL);
1615 #ifdef PRI_EVENT_PROCEEDING
1618 #ifdef PRI_EVENT_SETUP_ACK
1622 ast_dsp_free(p->dsp);
1626 law = ZT_LAW_DEFAULT;
1627 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
1629 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
1630 /* Perform low level hangup if no owner left */
1632 if (p->sig == SIG_PRI) {
1634 if (!pri_grab(p, p->pri)) {
1635 #ifndef NEW_PRI_HANGUP
1636 if (!p->alreadyhungup) {
1637 res = pri_disconnect(p->pri->pri, p->call, PRI_CAUSE_NORMAL_CLEARING);
1639 pri_release(p->pri->pri, p->call, -1);
1641 p->alreadyhungup = 0;
1645 #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
1647 if (p->alreadyhungup) {
1648 pri_hangup(p->pri->pri, p->call, -1);
1651 p->alreadyhungup = 1;
1652 pri_hangup(p->pri->pri, p->call, -1);
1656 ast_log(LOG_WARNING, "pri_disconnect failed\n");
1659 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1668 if (p->sig == SIG_R2) {
1670 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
1679 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
1681 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
1687 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
1690 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
1692 /* If they're off hook, try playing congestion */
1693 if (par.rxisoffhook)
1694 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
1696 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1700 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1707 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1708 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1712 p->callwaiting = p->permcallwaiting;
1713 p->hidecallerid = p->permhidecallerid;
1715 strcpy(p->rdnis, "");
1717 /* Restore data mode */
1718 if (p->sig == SIG_PRI) {
1720 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1727 p->callwaitingrepeat = 0;
1729 ast->pvt->pvt = NULL;
1730 ast_mutex_unlock(&p->lock);
1731 ast_mutex_lock(&usecnt_lock);
1734 ast_log(LOG_WARNING, "Usecnt < 0???\n");
1735 ast_mutex_unlock(&usecnt_lock);
1736 ast_update_use_count();
1737 if (option_verbose > 2)
1738 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
1740 ast_mutex_lock(&iflock);
1746 destroy_channel(prev, tmp, 0);
1754 ast_mutex_unlock(&iflock);
1758 static int zt_answer(struct ast_channel *ast)
1760 struct zt_pvt *p = ast->pvt->pvt;
1763 int oldstate = ast->_state;
1764 ast_setstate(ast, AST_STATE_UP);
1765 ast_mutex_lock(&p->lock);
1766 index = zt_get_index(ast, p, 0);
1769 /* nothing to do if a radio channel */
1771 ast_mutex_unlock(&p->lock);
1788 case SIG_SF_FEATDMF:
1793 /* Pick up the line */
1794 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
1795 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
1796 tone_zone_play_tone(p->subs[index].zfd, -1);
1798 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
1799 if (oldstate == AST_STATE_RINGING) {
1800 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
1801 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
1802 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1803 p->owner = p->subs[SUB_REAL].owner;
1809 /* Send a pri acknowledge */
1810 if (!pri_grab(p, p->pri)) {
1811 res = pri_answer(p->pri->pri, p->call, 0, 1);
1814 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1821 res = mfcr2_AnswerCall(p->r2, NULL);
1823 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
1827 ast_mutex_unlock(&p->lock);
1830 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
1833 ast_mutex_unlock(&p->lock);
1837 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
1842 struct zt_pvt *p = chan->pvt->pvt;
1845 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
1846 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
1852 if ((!cp) || (datalen < 1))
1858 case AST_OPTION_TONE_VERIFY:
1863 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
1864 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
1867 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
1868 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
1871 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
1872 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
1876 case AST_OPTION_TDD: /* turn on or off TDD */
1877 if (!*cp) { /* turn it off */
1878 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
1879 if (p->tdd) tdd_free(p->tdd);
1885 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
1886 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
1889 /* otherwise, turn it on */
1890 if (!p->didtdd) { /* if havent done it yet */
1891 unsigned char mybuf[41000],*buf;
1892 int size,res,fd,len;
1896 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
1897 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
1899 index = zt_get_index(chan, p, 0);
1901 ast_log(LOG_WARNING, "No index in TDD?\n");
1904 fd = p->subs[index].zfd;
1906 if (ast_check_hangup(chan)) return -1;
1908 if (size > READ_SIZE)
1914 res = ast_select(fd + 1,NULL,&wfds,&efds,NULL);
1916 ast_log(LOG_DEBUG, "select (for write) ret. 0 on channel %d\n", p->channel);
1919 /* if got exception */
1920 if (FD_ISSET(fd,&efds)) return -1;
1921 if (!FD_ISSET(fd,&wfds)) {
1922 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
1925 res = write(fd, buf, size);
1927 if (res == -1) return -1;
1928 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
1934 p->didtdd = 1; /* set to have done it now */
1936 if (*cp == 2) { /* Mate mode */
1937 if (p->tdd) tdd_free(p->tdd);
1942 if (!p->tdd) { /* if we dont have one yet */
1943 p->tdd = tdd_new(); /* allocate one */
1946 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
1949 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
1954 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
1957 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
1959 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
1962 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
1968 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
1971 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
1972 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
1979 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master)
1981 /* Unlink a specific slave or all slaves/masters from a given master */
1987 for (x=0;x<MAX_SLAVES;x++) {
1988 if (master->slaves[x]) {
1989 if (!slave || (master->slaves[x] == slave)) {
1990 /* Take slave out of the conference */
1991 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
1992 conf_del(&master->confno, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
1993 master->slaves[x]->master = NULL;
1994 master->slaves[x] = NULL;
1999 master->inconference = 0;
2002 if (master->master) {
2003 /* Take master out of the conference */
2004 conf_del(&master->master->confno, &master->subs[SUB_REAL], SUB_REAL);
2006 for (x=0;x<MAX_SLAVES;x++) {
2007 if (master->master->slaves[x] == master)
2008 master->master->slaves[x] = NULL;
2009 else if (master->master->slaves[x])
2013 master->master->inconference = 0;
2015 master->master = NULL;
2017 update_conf(master);
2020 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2022 if (!slave || !master) {
2023 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2026 for (x=0;x<MAX_SLAVES;x++) {
2027 if (!master->slaves[x]) {
2028 master->slaves[x] = slave;
2032 if (x >= MAX_SLAVES) {
2033 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2034 master->slaves[MAX_SLAVES - 1] = slave;
2037 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2038 slave->master = master;
2040 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2043 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2045 struct ast_channel *who = NULL, *cs[3];
2046 struct zt_pvt *p0, *p1, *op0, *op1;
2047 struct zt_pvt *master=NULL, *slave=NULL;
2048 struct ast_frame *f;
2053 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2054 int os1 = -1, os2 = -1;
2055 struct ast_channel *oc1, *oc2;
2057 /* if need DTMF, cant native bridge */
2058 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2062 /* cant do pseudo-channels here */
2063 if ((!p0->sig) || (!p1->sig)) return -2;
2065 ast_mutex_lock(&c0->lock);
2066 ast_mutex_lock(&c1->lock);
2067 op0 = p0 = c0->pvt->pvt;
2068 op1 = p1 = c1->pvt->pvt;
2071 oi1 = zt_get_index(c0, p0, 0);
2072 oi2 = zt_get_index(c1, p1, 0);
2075 if ((oi1 < 0) || (oi2 < 0))
2080 ast_mutex_lock(&p0->lock);
2081 if (ast_mutex_trylock(&p1->lock)) {
2082 /* Don't block, due to potential for deadlock */
2083 ast_mutex_unlock(&p0->lock);
2084 ast_mutex_unlock(&c0->lock);
2085 ast_mutex_unlock(&c1->lock);
2086 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2089 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2090 if (!p0->owner || !p1->owner) {
2091 /* Currently unowned -- Do nothing. */
2094 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2095 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2099 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2104 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2105 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,
2106 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2109 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2110 if (p1->subs[SUB_THREEWAY].inthreeway) {
2116 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2117 if (p0->subs[SUB_THREEWAY].inthreeway) {
2123 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2124 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2125 don't put us in anything */
2126 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2132 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2133 /* Same as previous */
2134 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2141 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2142 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2143 if (master && slave) {
2144 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2145 in an active threeway call with a channel that is ringing, we should
2146 indicate ringing. */
2147 if ((oi2 == SUB_THREEWAY) &&
2148 p1->subs[SUB_THREEWAY].inthreeway &&
2149 p1->subs[SUB_REAL].owner &&
2150 p1->subs[SUB_REAL].inthreeway &&
2151 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2152 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2153 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2154 os2 = p1->subs[SUB_REAL].owner->_state;
2156 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2157 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2159 if ((oi1 == SUB_THREEWAY) &&
2160 p0->subs[SUB_THREEWAY].inthreeway &&
2161 p0->subs[SUB_REAL].owner &&
2162 p0->subs[SUB_REAL].inthreeway &&
2163 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2164 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2165 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2166 os1 = p0->subs[SUB_REAL].owner->_state;
2168 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2169 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2171 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2172 if (!p0->echocanbridged || !p1->echocanbridged) {
2173 /* Disable echo cancellation if appropriate */
2178 zt_link(slave, master);
2179 master->inconference = inconf;
2180 } else if (!nothingok)
2181 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2185 t1 = p0->subs[SUB_REAL].inthreeway;
2186 t2 = p1->subs[SUB_REAL].inthreeway;
2188 ast_mutex_unlock(&p0->lock);
2189 ast_mutex_unlock(&p1->lock);
2191 ast_mutex_unlock(&c0->lock);
2192 ast_mutex_unlock(&c1->lock);
2194 /* Native bridge failed */
2195 if ((!master || !slave) && !nothingok) {
2207 /* Here's our main loop... Start by locking things, looking for private parts,
2208 and then balking if anything is wrong */
2209 ast_mutex_lock(&c0->lock);
2210 ast_mutex_lock(&c1->lock);
2214 i1 = zt_get_index(c0, p0, 1);
2216 i2 = zt_get_index(c1, p1, 1);
2217 ast_mutex_unlock(&c0->lock);
2218 ast_mutex_unlock(&c1->lock);
2219 if ((op0 != p0) || (op1 != p1) ||
2220 (ofd1 != c0->fds[0]) ||
2221 (ofd2 != c1->fds[0]) ||
2222 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2223 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2224 (oc1 != p0->owner) ||
2225 (oc2 != p1->owner) ||
2226 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2227 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2230 if (slave && master)
2231 zt_unlink(slave, master);
2232 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2233 op0->channel, oi1, op1->channel, oi2);
2241 who = ast_waitfor_n(cs, 2, &to);
2243 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2246 if (who->pvt->pvt == op0)
2247 op0->ignoredtmf = 1;
2248 else if (who->pvt->pvt == op1)
2249 op1->ignoredtmf = 1;
2251 if (who->pvt->pvt == op0)
2252 op0->ignoredtmf = 0;
2253 else if (who->pvt->pvt == op1)
2254 op1->ignoredtmf = 0;
2258 if (slave && master)
2259 zt_unlink(slave, master);
2266 if (f->frametype == AST_FRAME_DTMF) {
2267 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2268 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2271 if (slave && master)
2272 zt_unlink(slave, master);
2274 } else if ((who == c0) && p0->pulsedial) {
2276 } else if ((who == c1) && p1->pulsedial) {
2282 /* Swap who gets priority */
2289 static int zt_indicate(struct ast_channel *chan, int condition);
2291 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2293 struct zt_pvt *p = newchan->pvt->pvt;
2295 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2296 if (p->owner == oldchan)
2299 if (p->subs[x].owner == oldchan) {
2302 p->subs[x].owner = newchan;
2304 if (newchan->_state == AST_STATE_RINGING)
2305 zt_indicate(newchan, AST_CONTROL_RINGING);
2310 static int zt_ring_phone(struct zt_pvt *p)
2314 /* Make sure our transmit state is on hook */
2317 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2320 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2322 printf("Res: %d, error: %s\n", res, strerror(errno));
2328 /* Wait just in case */
2335 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2343 static void *ss_thread(void *data);
2345 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int);
2347 static int attempt_transfer(struct zt_pvt *p)
2349 /* In order to transfer, we need at least one of the channels to
2350 actually be in a call bridge. We can't conference two applications
2351 together (but then, why would we want to?) */
2352 if (p->subs[SUB_REAL].owner->bridge) {
2353 /* The three-way person we're about to transfer to could still be in MOH, so
2354 stop if now if appropriate */
2355 if (p->subs[SUB_THREEWAY].owner->bridge)
2356 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2357 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2358 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2360 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2361 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2362 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2365 /* Orphan the channel */
2366 unalloc_sub(p, SUB_THREEWAY);
2367 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2368 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2369 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2371 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2372 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2373 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2374 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2377 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2378 unalloc_sub(p, SUB_THREEWAY);
2379 /* Tell the caller not to hangup */
2382 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2383 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2384 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2390 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2392 struct ast_frame *f;
2393 f = &p->subs[index].f;
2395 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2399 case MFCR2_EVENT_BLOCKED:
2400 if (option_verbose > 2)
2401 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2403 case MFCR2_EVENT_UNBLOCKED:
2404 if (option_verbose > 2)
2405 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2407 case MFCR2_EVENT_CONFIG_ERR:
2408 if (option_verbose > 2)
2409 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2411 case MFCR2_EVENT_RING:
2412 if (option_verbose > 2)
2413 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2415 case MFCR2_EVENT_HANGUP:
2416 if (option_verbose > 2)
2417 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2419 case MFCR2_EVENT_RINGING:
2420 if (option_verbose > 2)
2421 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2423 case MFCR2_EVENT_ANSWER:
2424 if (option_verbose > 2)
2425 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2427 case MFCR2_EVENT_HANGUP_ACK:
2428 if (option_verbose > 2)
2429 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2431 case MFCR2_EVENT_IDLE:
2432 if (option_verbose > 2)
2433 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2436 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2442 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2447 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2449 ast_log(LOG_WARNING, "Unable to check received bits\n");
2453 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2456 e = mfcr2_cas_signaling_event(p->r2, x);
2461 static int check_for_conference(struct zt_pvt *p)
2464 /* Fine if we already have a master, etc */
2465 if (p->master || (p->confno > -1))
2467 memset(&ci, 0, sizeof(ci));
2468 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2469 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2472 /* If we have no master and don't have a confno, then
2473 if we're in a conference, it's probably a MeetMe room or
2474 some such, so don't let us 3-way out! */
2476 if (option_verbose > 2)
2477 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
2483 static int get_alarms(struct zt_pvt *p)
2487 memset(&zi, 0, sizeof(zi));
2488 zi.spanno = p->span;
2489 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
2491 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
2497 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
2501 struct zt_pvt *p = ast->pvt->pvt;
2503 pthread_attr_t attr;
2504 struct ast_channel *chan;
2505 pthread_attr_init(&attr);
2506 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2507 index = zt_get_index(ast, p, 0);
2508 p->subs[index].f.frametype = AST_FRAME_NULL;
2509 p->subs[index].f.datalen = 0;
2510 p->subs[index].f.samples = 0;
2511 p->subs[index].f.mallocd = 0;
2512 p->subs[index].f.offset = 0;
2513 p->subs[index].f.src = "zt_handle_event";
2514 p->subs[index].f.data = NULL;
2516 return &p->subs[index].f;
2517 res = zt_get_event(p->subs[index].zfd);
2518 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
2519 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
2520 if (res & ZT_EVENT_PULSEDIGIT)
2524 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
2525 p->subs[index].f.frametype = AST_FRAME_DTMF;
2526 p->subs[index].f.subclass = res & 0xff;
2527 /* Return the captured digit */
2528 return &p->subs[index].f;
2531 case ZT_EVENT_BITSCHANGED:
2532 if (p->sig == SIG_R2) {
2534 struct ast_frame *f = &p->subs[index].f;
2536 e = r2_get_event_bits(p);
2538 f = handle_r2_event(p, e, index);
2544 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
2545 case ZT_EVENT_PULSE_START:
2546 /* Stop tone if there's a pulse start and the PBX isn't started */
2548 tone_zone_play_tone(p->subs[index].zfd, -1);
2550 case ZT_EVENT_DIALCOMPLETE:
2551 if (p->inalarm) break;
2552 if (p->radio) break;
2553 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
2554 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
2557 if (!x) { /* if not still dialing in driver */
2560 if (ast->_state == AST_STATE_DIALING) {
2561 if (p->callprogress && CANPROGRESSDETECT(p) && p->dsp) {
2562 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
2563 } 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)))) {
2564 ast_setstate(ast, AST_STATE_RINGING);
2566 ast_setstate(ast, AST_STATE_UP);
2567 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2568 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2573 case ZT_EVENT_ALARM:
2575 #ifdef PRI_DESTROYCALL
2576 if (p->call && p->pri && p->pri->pri)
2577 pri_destroycall(p->pri->pri, p->call);
2579 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
2582 #error Please "cvs update" and recompile libpri
2586 res = get_alarms(p);
2587 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
2588 manager_event(EVENT_FLAG_AGENT, "Alarm",
2591 alarm2str(res), p->channel);
2592 /* fall through intentionally */
2593 case ZT_EVENT_ONHOOK:
2596 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2597 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
2604 p->onhooktime = time(NULL);
2606 /* Check for some special conditions regarding call waiting */
2607 if (index == SUB_REAL) {
2608 /* The normal line was hung up */
2609 if (p->subs[SUB_CALLWAIT].owner) {
2610 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
2611 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2612 if (option_verbose > 2)
2613 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2614 unalloc_sub(p, SUB_CALLWAIT);
2616 p->subs[index].needanswer = 0;
2617 p->subs[index].needringing = 0;
2619 p->callwaitingrepeat = 0;
2623 } else if (p->subs[SUB_THREEWAY].owner) {
2625 unsigned int mssinceflash;
2626 gettimeofday(&tv, NULL);
2627 mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
2628 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
2629 if (mssinceflash < MIN_MS_SINCE_FLASH) {
2630 /* It hasn't been long enough since the last flashook. This is probably a bounce on
2631 hanging up. Hangup both channels now */
2632 if (p->subs[SUB_THREEWAY].owner)
2633 ast_queue_hangup(p->subs[SUB_THREEWAY].owner, 0);
2634 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2635 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
2636 } else if ((ast->pbx) ||
2637 (ast->_state == AST_STATE_UP)) {
2639 /* In any case this isn't a threeway call anymore */
2640 p->subs[SUB_REAL].inthreeway = 0;
2641 p->subs[SUB_THREEWAY].inthreeway = 0;
2642 if ((res = attempt_transfer(p)) < 0)
2643 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2645 /* Don't actually hang up at this point */
2649 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2651 /* Swap subs and dis-own channel */
2652 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2654 /* Ring the phone */
2659 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
2667 case ZT_EVENT_RINGOFFHOOK:
2668 if (p->inalarm) break;
2671 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2672 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
2679 switch(ast->_state) {
2680 case AST_STATE_RINGING:
2682 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2683 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2684 /* Make sure it stops ringing */
2685 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
2686 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
2688 /* Cancel any running CallerID spill */
2694 if (p->confirmanswer) {
2695 /* Ignore answer if "confirm answer" is selected */
2696 p->subs[index].f.frametype = AST_FRAME_NULL;
2697 p->subs[index].f.subclass = 0;
2698 } else if (strlen(p->dop.dialstr)) {
2699 /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
2700 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
2702 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
2703 p->dop.dialstr[0] = '\0';
2706 ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
2707 p->subs[index].f.frametype = AST_FRAME_NULL;
2708 p->subs[index].f.subclass = 0;
2711 p->dop.dialstr[0] = '\0';
2712 ast_setstate(ast, AST_STATE_DIALING);
2714 ast_setstate(ast, AST_STATE_UP);
2715 return &p->subs[index].f;
2716 case AST_STATE_DOWN:
2717 ast_setstate(ast, AST_STATE_RING);
2719 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2720 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
2721 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
2722 return &p->subs[index].f;
2724 /* Make sure it stops ringing */
2725 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
2726 /* Okay -- probably call waiting*/
2727 if (p->owner->bridge)
2728 ast_moh_stop(p->owner->bridge);
2731 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
2737 if (ast->_state == AST_STATE_RING) {
2749 case SIG_SF_FEATDMF:
2751 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
2753 ast_log(LOG_DEBUG, "Ring detected\n");
2754 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2755 p->subs[index].f.subclass = AST_CONTROL_RING;
2756 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
2758 ast_log(LOG_DEBUG, "Line answered\n");
2759 if (p->confirmanswer) {
2760 p->subs[index].f.frametype = AST_FRAME_NULL;
2761 p->subs[index].f.subclass = 0;
2763 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2764 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2765 ast_setstate(ast, AST_STATE_UP);
2767 } else if (ast->_state != AST_STATE_RING)
2768 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
2771 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
2774 case ZT_EVENT_RINGEROFF:
2775 if (p->inalarm) break;
2776 if (p->radio) break;
2778 if ((ast->rings > p->cidrings) && (p->cidspill)) {
2779 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
2784 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2785 p->subs[index].f.subclass = AST_CONTROL_RINGING;
2787 case ZT_EVENT_RINGERON:
2789 case ZT_EVENT_NOALARM:
2791 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
2792 manager_event(EVENT_FLAG_AGENT, "AlarmClear",
2793 "Channel: %d\r\n", p->channel);
2795 case ZT_EVENT_WINKFLASH:
2796 if (p->inalarm) break;
2797 if (p->radio) break;
2798 /* Remember last time we got a flash-hook */
2799 gettimeofday(&p->flashtime, NULL);
2804 ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
2805 index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2807 if (index == SUB_REAL) {
2808 if (p->subs[SUB_CALLWAIT].owner) {
2809 /* Swap to call-wait */
2810 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
2811 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2812 p->owner = p->subs[SUB_REAL].owner;
2813 ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
2814 if (p->owner->_state == AST_STATE_RINGING) {
2815 ast_setstate(p->owner, AST_STATE_UP);
2816 p->subs[SUB_REAL].needanswer = 1;
2818 p->callwaitingrepeat = 0;
2820 /* Start music on hold if appropriate */
2821 if (!p->subs[SUB_CALLWAIT].inthreeway && p->subs[SUB_CALLWAIT].owner->bridge)
2822 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
2823 if (p->subs[SUB_REAL].owner->bridge)
2824 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
2825 } else if (!p->subs[SUB_THREEWAY].owner) {
2826 if (p->threewaycalling && !check_for_conference(p)) {
2827 /* XXX This section needs much more error checking!!! XXX */
2828 /* Start a 3-way call if feasible */
2830 (ast->_state == AST_STATE_UP) ||
2831 (ast->_state == AST_STATE_RING)) {
2832 if (!alloc_sub(p, SUB_THREEWAY)) {
2833 /* Make new channel */
2834 chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0);
2835 /* Swap things around between the three-way and real call */
2836 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2837 /* Disable echo canceller for better dialing */
2839 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
2841 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
2843 if (pthread_create(&threadid, &attr, ss_thread, chan)) {
2844 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
2845 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2849 if (option_verbose > 2)
2850 ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);