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 <sys/signal.h>
38 #include <sys/select.h>
43 #include <sys/ioctl.h>
44 #include <linux/zaptel.h>
55 #include "../asterisk.h"
59 XXX We definitely need to lock the private structure in zt_read and such
65 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
66 * the user hangs up to reset the state machine so ring works properly.
67 * This is used to be able to support kewlstart by putting the zhone in
68 * groundstart mode since their forward disconnect supervision is entirely
69 * broken even though their documentation says it isn't and their support
70 * is entirely unwilling to provide any assistance with their channel banks
71 * even though their web site says they support their products for life.
74 /* #define ZHONE_HACK */
76 #define CHANNEL_PSEUDO -12
78 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
80 static char *desc = "Zapata Telephony"
89 static char *tdesc = "Zapata Telephony Driver"
98 static char *type = "Zap";
99 static char *typecompat = "Tor"; /* Retain compatibility with chan_tor */
100 static char *config = "zapata.conf";
102 #define SIG_EM ZT_SIG_EM
103 #define SIG_EMWINK (0x10000 | ZT_SIG_EM)
104 #define SIG_FEATD (0x20000 | ZT_SIG_EM)
105 #define SIG_FEATDMF (0x40000 | ZT_SIG_EM)
106 #define SIG_FEATB (0x80000 | ZT_SIG_EM)
107 #define SIG_FXSLS ZT_SIG_FXSLS
108 #define SIG_FXSGS ZT_SIG_FXSGS
109 #define SIG_FXSKS ZT_SIG_FXSKS
110 #define SIG_FXOLS ZT_SIG_FXOLS
111 #define SIG_FXOGS ZT_SIG_FXOGS
112 #define SIG_FXOKS ZT_SIG_FXOKS
113 #define SIG_PRI ZT_SIG_CLEAR
114 #define SIG_R2 ZT_SIG_CAS
117 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
119 #define CHAN_PSEUDO -2
121 static char context[AST_MAX_EXTENSION] = "default";
122 static char callerid[256] = "";
124 static char language[MAX_LANGUAGE] = "";
125 static char musicclass[MAX_LANGUAGE] = "";
127 static int use_callerid = 1;
129 static int cur_signalling = -1;
131 static int cur_group = 0;
132 static int cur_callergroup = 0;
133 static int cur_pickupgroup = 0;
134 static int relaxdtmf = 0;
136 static int immediate = 0;
138 static int stripmsd = 0;
140 static int callwaiting = 0;
142 static int callwaitingcallerid = 0;
144 static int hidecallerid = 0;
146 static int callreturn = 0;
148 static int threewaycalling = 0;
150 static int transfer = 0;
152 static int cancallforward = 0;
154 static float rxgain = 0.0;
156 static float txgain = 0.0;
158 static int echocancel;
160 static int echocanbridged = 0;
162 static int busydetect = 0;
164 static int callprogress = 0;
166 static char accountcode[20] = "";
168 static char mailbox[AST_MAX_EXTENSION];
170 static int amaflags = 0;
175 static int minunused = 2;
176 static int minidle = 0;
177 static char idleext[AST_MAX_EXTENSION];
178 static char idledial[AST_MAX_EXTENSION];
181 /* Wait up to 16 seconds for first digit (FXO logic) */
182 static int firstdigittimeout = 16000;
184 /* How long to wait for following digits (FXO logic) */
185 static int gendigittimeout = 8000;
187 /* How long to wait for an extra digit, if there is an ambiguous match */
188 static int matchdigittimeout = 3000;
190 static int usecnt =0;
191 static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
193 /* Protect the interface list (of zt_pvt's) */
194 static pthread_mutex_t iflock = AST_MUTEX_INITIALIZER;
196 /* Protect the monitoring thread, so only one process can kill or start it, and not
197 when it's doing something critical. */
198 static pthread_mutex_t monlock = AST_MUTEX_INITIALIZER;
200 /* This is the thread for the monitor which checks for input on the channels
201 which are not currently in use. */
202 static pthread_t monitor_thread = 0;
204 static int restart_monitor(void);
206 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
208 static int zt_sendtext(struct ast_channel *c, char *text);
210 static inline int zt_get_event(int fd)
212 /* Avoid the silly zt_getevent which ignores a bunch of events */
214 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
218 static inline int zt_wait_event(int fd)
220 /* Avoid the silly zt_waitevent which ignores a bunch of events */
222 i = ZT_IOMUX_SIGEVENT;
223 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
224 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
228 /* Chunk size to read -- we use 20ms chunks to make things happy. */
229 #define READ_SIZE 160
231 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
232 #define MASK_INUSE (1 << 1) /* Channel currently in use */
234 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
235 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
236 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
237 #define RINGT ( (8000 * 8) / READ_SIZE)
243 static int r2prot = -1;
249 pthread_t master; /* Thread of master */
250 pthread_mutex_t lock; /* Mutex */
251 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
252 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
253 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
254 int minunused; /* Min # of channels to keep empty */
255 int minidle; /* Min # of "idling" calls to keep active */
256 int nodetype; /* Node type */
257 int switchtype; /* Type of switch to emulate */
258 int dialplan; /* Dialing plan */
259 int dchannel; /* What channel the dchannel is on */
260 int channels; /* Num of chans in span (31 or 24) */
267 int chanmask[32]; /* Channel status */
271 struct zt_pvt *pvt[32]; /* Member channel pvt structs */
272 struct zt_channel *chan[32]; /* Channels on each line */
276 static struct zt_pri pris[NUM_SPANS];
278 static int pritype = PRI_CPE;
281 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
283 #define DEFAULT_PRI_DEBUG 0
286 static inline int pri_grab(struct zt_pri *pri)
289 /* Grab the lock first */
290 res = ast_pthread_mutex_lock(&pri->lock);
293 /* Then break the select */
294 pthread_kill(pri->master, SIGURG);
298 static inline void pri_rel(struct zt_pri *pri)
300 ast_pthread_mutex_unlock(&pri->lock);
303 static int switchtype = PRI_SWITCH_NI2;
304 static int dialplan = PRI_NATIONAL_ISDN + 1;
308 #define SUB_REAL 0 /* Active call */
309 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
310 #define SUB_THREEWAY 2 /* Three-way call */
312 static char *subnames[] = {
318 struct zt_subchannel {
320 struct ast_channel *owner;
322 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
323 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
329 int curconfno; /* What conference we're currently in */
332 #define CONF_USER_REAL (1 << 0)
333 #define CONF_USER_THIRDCALL (1 << 1)
337 static struct zt_pvt {
338 pthread_mutex_t lock;
339 struct ast_channel *owner; /* Our current active owner (if applicable) */
340 /* Up to three channels can be associated with this call */
342 struct zt_subchannel sub_unused; /* Just a safety precaution */
343 struct zt_subchannel subs[3]; /* Sub-channels */
344 struct zt_confinfo saveconf; /* Saved conference info */
346 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
347 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
348 int inconference; /* If our real should be in the conference */
350 int sig; /* Signalling style */
351 int radio; /* radio type */
352 int firstradio; /* first radio flag */
355 struct zt_pvt *next; /* Next channel in list */
356 char context[AST_MAX_EXTENSION];
357 char exten[AST_MAX_EXTENSION];
358 char language[MAX_LANGUAGE];
359 char musicclass[MAX_LANGUAGE];
360 char callerid[AST_MAX_EXTENSION];
361 char lastcallerid[AST_MAX_EXTENSION];
362 char callwaitcid[AST_MAX_EXTENSION];
363 char rdnis[AST_MAX_EXTENSION];
366 int confno; /* Our conference */
367 int confusers; /* Who is using our conference */
368 int propconfno; /* Propagated conference number */
371 int immediate; /* Answer before getting digits? */
372 int channel; /* Channel Number */
373 int span; /* Span number */
376 int use_callerid; /* Whether or not to use caller id on this channel */
379 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
380 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
381 int cidcwexpire; /* When to expire our muting for CID/CW */
382 unsigned char *cidspill;
394 int callwaitingcallerid;
403 int cref; /* Call reference number */
404 ZT_DIAL_OPERATION dop;
408 char accountcode[20]; /* Account code */
409 int amaflags; /* AMA Flags */
410 char didtdd; /* flag to say its done it once */
411 struct tdd_state *tdd; /* TDD flag */
414 char call_forward[AST_MAX_EXTENSION];
415 char mailbox[AST_MAX_EXTENSION];
419 int confirmanswer; /* Wait for '#' to confirm answer */
420 int distinctivering; /* Which distinctivering to use */
421 int cidrings; /* Which ring to deliver CID on */
423 int faxhandled; /* Has a fax tone already been handled? */
425 char mate; /* flag to say its in MATE mode */
426 int pulsedial; /* whether a pulse dial phone is detected */
427 int dtmfrelax; /* whether to run in relaxed DTMF mode */
445 static struct zt_ring_cadence cadences[] = {
446 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
447 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
448 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
449 { { 1000, 500, 2500, 5000 } }, /* Long ring */
452 static int cidrings[] = {
453 2, /* Right after first long ring */
454 4, /* Right after long part */
455 3, /* After third chirp */
456 2, /* Second spell */
459 #define NUM_CADENCE (sizeof(cadences) / sizeof(cadences[0]))
461 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
462 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
464 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & SIG_EM) /* || (p->sig & __ZT_SIG_FXO) */)
465 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & SIG_EM) /* || (p->sig & __ZT_SIG_FXO) */)
468 /* return non-zero if clear dtmf is appropriate */
469 static int CLEARDTMF(struct ast_channel *chan) {
470 struct zt_pvt *p = chan->pvt->pvt,*themp;
471 struct ast_channel *them;
474 /* if not in a 3 way, we should be okay */
475 if (p->thirdcallindex == -1) return 1;
476 /* get the other side of the call's channel pointer */
477 if (p->owners[p->normalindex] == chan)
478 them = p->owners[p->thirdcallindex];
480 them = p->owners[p->normalindex];
483 if (!them->bridge) return 1;
484 /* get their private structure, too */
485 themp = them->pvt->pvt;
486 /* if does not use zt bridge code, return 0 */
487 if (them->pvt->bridge != zt_bridge) return 0;
488 if (them->bridge->pvt->bridge != zt_bridge) return 0;
489 return 1; /* okay, I guess we are okay to be clear */
493 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
496 if (p->subs[0].owner == ast)
498 else if (p->subs[1].owner == ast)
500 else if (p->subs[2].owner == ast)
505 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
510 static void swap_subs(struct zt_pvt *p, int a, int b)
514 struct ast_channel *towner;
516 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
518 tchan = p->subs[a].chan;
519 towner = p->subs[a].owner;
520 tinthreeway = p->subs[a].inthreeway;
522 p->subs[a].chan = p->subs[b].chan;
523 p->subs[a].owner = p->subs[b].owner;
524 p->subs[a].inthreeway = p->subs[b].inthreeway;
526 p->subs[b].chan = tchan;
527 p->subs[b].owner = towner;
528 p->subs[b].inthreeway = tinthreeway;
530 if (p->subs[a].owner)
531 p->subs[a].owner->fds[0] = p->subs[a].zfd;
532 if (p->subs[b].owner)
533 p->subs[b].owner->fds[0] = p->subs[b].zfd;
537 static int zt_open(char *fn)
545 for (x=0;x<strlen(fn);x++) {
546 if (!isdigit(fn[x])) {
554 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
557 fn = "/dev/zap/channel";
559 fd = open(fn, O_RDWR | O_NONBLOCK);
561 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
565 if (ioctl(fd, ZT_SPECIFY, &chan)) {
569 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
574 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
578 static void zt_close(int fd)
583 int zt_setlinear(int zfd, int linear)
586 res = ioctl(zfd, ZT_SETLINEAR, &linear);
593 int zt_setlaw(int zfd, int law)
596 res = ioctl(zfd, ZT_SETLAW, &law);
602 static int alloc_sub(struct zt_pvt *p, int x)
606 if (p->subs[x].zfd < 0) {
607 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
608 if (p->subs[x].zfd > -1) {
609 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
611 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
612 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
614 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
616 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
619 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
620 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
621 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
622 zt_close(p->subs[x].zfd);
627 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
630 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
633 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
637 static int unalloc_sub(struct zt_pvt *p, int x)
640 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
643 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
644 if (p->subs[x].zfd > -1) {
645 zt_close(p->subs[x].zfd);
648 p->subs[x].linear = 0;
650 p->subs[x].owner = NULL;
651 p->subs[x].inthreeway = 0;
652 p->subs[x].curconfno = -1;
656 static int zt_digit(struct ast_channel *ast, char digit)
658 ZT_DIAL_OPERATION zo;
664 index = zt_get_index(ast, p, 0);
665 if (index == SUB_REAL) {
666 zo.op = ZT_DIAL_OP_APPEND;
668 zo.dialstr[1] = digit;
670 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
671 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
679 static char *events[] = {
692 "Hook Transition Complete",
697 static char *event2str(int event)
699 static char buf[256];
700 if ((event < 15) && (event > -1))
701 return events[event];
702 sprintf(buf, "Event %d", event);
707 static int str2r2prot(char *swtype)
709 if (!strcasecmp(swtype, "ar"))
710 return MFCR2_PROT_ARGENTINA;
712 if (!strcasecmp(swtype, "cn"))
713 return MFCR2_PROT_CHINA;
715 if (!strcasecmp(swtype, "kr"))
716 return MFCR2_PROT_KOREA;
722 static char *sig2str(int sig)
724 static char buf[256];
727 return "E & M Immediate";
731 return "Feature Group D (DTMF)";
733 return "Feature Group D (MF)";
735 return "Feature Group B (MF)";
737 return "FXS Loopstart";
739 return "FXS Groundstart";
741 return "FXS Kewlstart";
743 return "FXO Loopstart";
745 return "FXO Groundstart";
747 return "FXO Kewlstart";
749 return "PRI Signalling";
751 return "R2 Signalling";
753 return "Pseudo Signalling";
755 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
760 static int conf_add(int *confno, struct zt_subchannel *c, int index)
762 /* If the conference already exists, and we're already in it
763 don't bother doing anything */
765 if ((*confno > 0) && (c->curconfno == *confno))
767 if (c->curconfno > 0) {
768 ast_log(LOG_WARNING, "Subchannel %d is already in conference %d, moving to %d\n", c->zfd, c->curconfno, *confno);
772 memset(&zi, 0, sizeof(zi));
776 /* Real-side and pseudo-side both participate in conference */
777 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
778 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
780 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
781 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
782 ast_log(LOG_WARNING, "Failed to add %d to conference %d\n", c->zfd, *confno);
785 c->curconfno = zi.confno;
787 ast_log(LOG_DEBUG, "Added %d to conference %d\n", c->zfd, *confno);
791 static int conf_del(int *confno, struct zt_subchannel *c, int index)
794 /* Can't delete from this conference if it's not 0 */
796 /* Can't delete if there's no zfd */
798 /* Don't delete from the conference if it's not our conference */
799 (*confno != c->curconfno)
800 /* Don't delete if we don't think it's conferenced at all (implied) */
802 memset(&zi, 0, sizeof(zi));
806 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
807 ast_log(LOG_WARNING, "Failed to drop %d from conference %d\n", c->zfd, *confno);
811 ast_log(LOG_DEBUG, "Removed %d from conference %d\n", c->zfd, *confno);
815 static int update_conf(struct zt_pvt *p)
819 /* Update conference state in a stateless fashion */
820 /* Start with the obvious, general stuff */
822 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
823 conf_add(&p->confno, &p->subs[x], x);
826 conf_del(&p->confno, &p->subs[x], x);
829 /* If we have a slave, add him to our conference now */
830 for (x=0;x<MAX_SLAVES;x++) {
832 conf_add(&p->confno, &p->slaves[x]->subs[SUB_REAL], SUB_REAL);
836 /* If we're supposed to be in there, do so now */
837 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
838 conf_add(&p->confno, &p->subs[SUB_REAL], SUB_REAL);
841 /* If we have a master, add ourselves to his conference */
843 conf_add(&p->master->confno, &p->subs[SUB_REAL], SUB_REAL);
845 /* Nobody is left (or should be left) in our conference.
849 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
853 static void zt_enable_ec(struct zt_pvt *p)
858 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
861 if (p && p->echocancel) {
863 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
865 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
868 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
871 ast_log(LOG_DEBUG, "No echocancellation requested\n");
874 static void zt_disable_ec(struct zt_pvt *p)
880 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
882 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
884 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
889 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
896 /* caluculate linear value of tx gain */
897 ltxgain = pow(10.0,txgain / 20.0);
898 /* caluculate linear value of rx gain */
899 lrxgain = pow(10.0,rxgain / 20.0);
900 if (law == ZT_LAW_ALAW) {
901 for (j=0;j<256;j++) {
902 k = (int)(((float)AST_ALAW(j)) * lrxgain);
903 if (k > 32767) k = 32767;
904 if (k < -32767) k = -32767;
905 g.rxgain[j] = AST_LIN2A(k);
906 k = (int)(((float)AST_ALAW(j)) * ltxgain);
907 if (k > 32767) k = 32767;
908 if (k < -32767) k = -32767;
909 g.txgain[j] = AST_LIN2A(k);
912 for (j=0;j<256;j++) {
913 k = (int)(((float)AST_MULAW(j)) * lrxgain);
914 if (k > 32767) k = 32767;
915 if (k < -32767) k = -32767;
916 g.rxgain[j] = AST_LIN2MU(k);
917 k = (int)(((float)AST_MULAW(j)) * ltxgain);
918 if (k > 32767) k = 32767;
919 if (k < -32767) k = -32767;
920 g.txgain[j] = AST_LIN2MU(k);
924 return(ioctl(fd,ZT_SETGAINS,&g));
927 static inline int zt_set_hook(int fd, int hs)
931 res = ioctl(fd, ZT_HOOK, &x);
934 if (errno == EINPROGRESS) return 0;
935 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
940 static inline int zt_confmute(struct zt_pvt *p, int muted)
944 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
946 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
950 static int save_conference(struct zt_pvt *p)
952 struct zt_confinfo c;
954 if (p->saveconf.confmode) {
955 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
958 p->saveconf.chan = 0;
959 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
961 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
962 p->saveconf.confmode = 0;
967 c.confmode = ZT_CONF_NORMAL;
968 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
970 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
974 ast_log(LOG_DEBUG, "Disabled conferencing\n");
978 static int restore_conference(struct zt_pvt *p)
981 if (p->saveconf.confmode) {
982 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
983 p->saveconf.confmode = 0;
985 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
990 ast_log(LOG_DEBUG, "Restored conferencing\n");
994 static int send_callerid(struct zt_pvt *p);
996 int send_cwcidspill(struct zt_pvt *p)
1000 p->cidspill = malloc(MAX_CALLERID_SIZE);
1002 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1003 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
1004 /* Make sure we account for the end */
1005 p->cidlen += READ_SIZE * 4;
1008 if (option_verbose > 2)
1009 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
1014 static int has_voicemail(struct zt_pvt *p)
1017 return ast_app_has_voicemail(p->mailbox);
1020 static int send_callerid(struct zt_pvt *p)
1022 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1024 /* Take out of linear mode if necessary */
1025 if (p->subs[SUB_REAL].linear) {
1026 p->subs[SUB_REAL].linear = 0;
1027 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1029 while(p->cidpos < p->cidlen) {
1030 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1032 if (errno == EAGAIN)
1035 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1045 if (p->callwaitcas) {
1046 /* Wait for CID/CW to expire */
1047 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1049 restore_conference(p);
1053 static int zt_callwait(struct ast_channel *ast)
1055 struct zt_pvt *p = ast->pvt->pvt;
1056 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1058 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1061 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1065 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1066 if (!p->callwaitrings && p->callwaitingcallerid) {
1067 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1069 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1071 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1073 p->cidlen = 2400 + READ_SIZE * 4;
1078 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1084 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1086 struct zt_pvt *p = ast->pvt->pvt;
1092 strncpy(dest, rdest, sizeof(dest) - 1);
1093 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1094 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1098 if (p->radio) /* if a radio channel, up immediately */
1100 /* Special pseudo -- automatically up */
1101 ast_setstate(ast, AST_STATE_UP);
1104 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1105 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1107 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1114 if (p->owner == ast) {
1115 /* Normal ring, on hook */
1117 /* Don't send audio while on hook, until the call is answered */
1119 if (p->use_callerid) {
1120 /* Generate the Caller-ID spill if desired */
1122 ast_log(LOG_WARNING, "cidspill already exists??\n");
1125 p->cidspill = malloc(MAX_CALLERID_SIZE);
1128 p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
1132 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1134 /* Select proper cadence */
1135 if ((p->distinctivering > 0) && (p->distinctivering <= NUM_CADENCE)) {
1136 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1137 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1138 p->cidrings = cidrings[p->distinctivering - 1];
1140 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1141 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1145 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1146 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1151 /* Call waiting call */
1152 p->callwaitrings = 0;
1154 strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
1156 strcpy(p->callwaitcid, "");
1157 /* Call waiting tone instead */
1158 if (zt_callwait(ast))
1160 /* Make ring-back */
1161 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1162 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1166 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1168 strcpy(callerid, "");
1169 ast_callerid_parse(callerid, &n, &l);
1171 ast_shrink_phone_number(l);
1172 if (!ast_isphonenumber(l))
1176 strcpy(p->lastcallerid, l);
1178 strcpy(p->lastcallerid, "");
1179 ast_setstate(ast, AST_STATE_RINGING);
1180 index = zt_get_index(ast, p, 0);
1182 p->subs[index].needringing = 1;
1193 c = strchr(dest, '/');
1198 if (strlen(c) < p->stripmsd) {
1199 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1203 /* Start the trunk */
1204 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1206 if (errno != EINPROGRESS) {
1207 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1211 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1212 p->dop.op = ZT_DIAL_OP_REPLACE;
1213 if (p->sig == SIG_FEATD) {
1214 if (ast->callerid) {
1215 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1216 ast_callerid_parse(callerid, &n, &l);
1218 ast_shrink_phone_number(l);
1219 if (!ast_isphonenumber(l))
1225 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1227 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1229 if (p->sig == SIG_FEATDMF) {
1230 if (ast->callerid) {
1231 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1232 ast_callerid_parse(callerid, &n, &l);
1234 ast_shrink_phone_number(l);
1235 if (!ast_isphonenumber(l))
1241 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1243 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1245 if (p->sig == SIG_FEATB) {
1246 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1248 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", c + p->stripmsd);
1250 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1252 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1253 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1257 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1259 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1260 ast_setstate(ast, AST_STATE_DIALING);
1264 c = strchr(dest, '/');
1269 if (ast->callerid) {
1270 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1271 ast_callerid_parse(callerid, &n, &l);
1273 ast_shrink_phone_number(l);
1274 if (!ast_isphonenumber(l))
1279 if (strlen(c) < p->stripmsd) {
1280 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1283 p->dop.op = ZT_DIAL_OP_REPLACE;
1284 s = strchr(c + p->stripmsd, 'w');
1286 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1289 strcpy(p->dop.dialstr, "");
1291 if (pri_call(p->pri->pri, p->call, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1292 p->prioffset, p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1, l, p->pri->dialplan - 1, n,
1293 l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE,
1294 c + p->stripmsd, p->pri->dialplan - 1,
1295 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW))) {
1296 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1299 ast_setstate(ast, AST_STATE_DIALING);
1303 /* Special pseudo -- automatically up*/
1304 ast_setstate(ast, AST_STATE_UP);
1307 ast_log(LOG_DEBUG, "not yet implemented\n");
1313 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1323 for (i = 0; i < 3; i++) {
1324 if (cur->subs[i].owner) {
1330 prev->next = cur->next;
1334 if (cur->subs[SUB_REAL].zfd > -1) {
1335 zt_close(cur->subs[SUB_REAL].zfd);
1341 prev->next = cur->next;
1345 if (cur->subs[SUB_REAL].zfd > -1) {
1346 zt_close(cur->subs[SUB_REAL].zfd);
1354 static int zt_hangup(struct ast_channel *ast)
1358 static int restore_gains(struct zt_pvt *p);
1359 struct zt_pvt *p = ast->pvt->pvt;
1360 struct zt_pvt *tmp = NULL;
1361 struct zt_pvt *prev = NULL;
1365 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1366 if (!ast->pvt->pvt) {
1367 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1370 index = zt_get_index(ast, p, 1);
1375 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1380 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1381 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1385 /* Real channel, do some fixup */
1386 p->subs[index].owner = NULL;
1387 p->subs[index].needanswer = 0;
1388 p->subs[index].needringing = 0;
1389 p->subs[index].linear = 0;
1390 p->subs[index].needcallerid = 0;
1391 zt_setlinear(p->subs[index].zfd, 0);
1392 if (index == SUB_REAL) {
1393 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1394 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1395 if (p->subs[SUB_CALLWAIT].inthreeway) {
1396 /* We had flipped over to answer a callwait and now it's gone */
1397 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1398 /* Move to the call-wait, but un-own us until they flip back. */
1399 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1400 unalloc_sub(p, SUB_CALLWAIT);
1403 /* The three way hung up, but we still have a call wait */
1404 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1405 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1406 unalloc_sub(p, SUB_THREEWAY);
1407 if (p->subs[SUB_REAL].inthreeway) {
1408 /* This was part of a three way call. Immediately make way for
1410 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1411 p->owner = p->subs[SUB_REAL].owner;
1413 /* This call hasn't been completed yet... Set owner to NULL */
1414 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1417 p->subs[SUB_REAL].inthreeway = 0;
1419 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
1420 /* Move to the call-wait and switch back to them. */
1421 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1422 unalloc_sub(p, SUB_CALLWAIT);
1423 p->owner = p->subs[SUB_REAL].owner;
1424 if (p->subs[SUB_REAL].owner->bridge)
1425 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
1426 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
1427 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1428 unalloc_sub(p, SUB_THREEWAY);
1429 if (p->subs[SUB_REAL].inthreeway) {
1430 /* This was part of a three way call. Immediately make way for
1432 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1433 p->owner = p->subs[SUB_REAL].owner;
1435 /* This call hasn't been completed yet... Set owner to NULL */
1436 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1439 p->subs[SUB_REAL].inthreeway = 0;
1441 } else if (index == SUB_CALLWAIT) {
1442 /* Ditch the holding callwait call, and immediately make it availabe */
1443 if (p->subs[SUB_CALLWAIT].inthreeway) {
1444 /* This is actually part of a three way, placed on hold. Place the third part
1445 on music on hold now */
1446 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
1447 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
1448 p->subs[SUB_THREEWAY].inthreeway = 0;
1449 /* Make it the call wait now */
1450 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
1451 unalloc_sub(p, SUB_THREEWAY);
1453 unalloc_sub(p, SUB_CALLWAIT);
1454 } else if (index == SUB_THREEWAY) {
1455 if (p->subs[SUB_CALLWAIT].inthreeway) {
1456 /* The other party of the three way call is currently in a call-wait state.
1457 Start music on hold for them, and take the main guy out of the third call */
1458 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
1459 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
1460 p->subs[SUB_CALLWAIT].inthreeway = 0;
1462 p->subs[SUB_REAL].inthreeway = 0;
1463 /* If this was part of a three way call index, let us make
1464 another three way call */
1465 unalloc_sub(p, SUB_THREEWAY);
1467 /* This wasn't any sort of call, but how are we an index? */
1468 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
1473 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
1476 p->distinctivering = 0;
1477 p->confirmanswer = 0;
1483 p->onhooktime = time(NULL);
1485 ast_dsp_free(p->dsp);
1488 law = ZT_LAW_DEFAULT;
1489 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
1491 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
1492 /* Perform low level hangup if no owner left */
1494 if (p->sig == SIG_PRI) {
1496 if (!pri_grab(p->pri)) {
1497 res = pri_disconnect(p->pri->pri, p->call, PRI_CAUSE_NORMAL_CLEARING);
1498 if (p->alreadyhungup) {
1500 p->alreadyhungup = 0;
1503 ast_log(LOG_WARNING, "pri_disconnect failed\n");
1506 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1515 if (p->sig == SIG_R2) {
1517 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
1526 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
1528 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
1535 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
1538 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
1540 /* If they're off hook, try playing congestion */
1541 if (par.rxisoffhook)
1542 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
1544 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1548 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1555 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1556 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
1560 p->callwaiting = p->permcallwaiting;
1561 p->hidecallerid = p->permhidecallerid;
1563 strcpy(p->rdnis, "");
1569 p->callwaitingrepeat = 0;
1571 ast->pvt->pvt = NULL;
1572 ast_setstate(ast, AST_STATE_DOWN);
1573 ast_pthread_mutex_lock(&usecnt_lock);
1576 ast_log(LOG_WARNING, "Usecnt < 0???\n");
1577 ast_pthread_mutex_unlock(&usecnt_lock);
1578 ast_update_use_count();
1579 if (option_verbose > 2)
1580 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
1582 ast_pthread_mutex_lock(&iflock);
1588 destroy_channel(prev, tmp, 0);
1596 ast_pthread_mutex_unlock(&iflock);
1600 static int zt_answer(struct ast_channel *ast)
1602 struct zt_pvt *p = ast->pvt->pvt;
1605 int oldstate = ast->_state;
1606 ast_setstate(ast, AST_STATE_UP);
1607 index = zt_get_index(ast, p, 0);
1610 /* nothing to do if a radio channel */
1627 /* Pick up the line */
1628 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
1629 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
1630 tone_zone_play_tone(p->subs[index].zfd, -1);
1632 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
1633 if (oldstate == AST_STATE_RINGING) {
1634 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
1635 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
1636 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1637 p->owner = p->subs[SUB_REAL].owner;
1643 /* Send a pri acknowledge */
1644 if (!pri_grab(p->pri)) {
1645 res = pri_answer(p->pri->pri, p->call, 0, 1);
1648 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1655 res = mfcr2_AnswerCall(p->r2, NULL);
1657 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
1663 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
1669 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
1674 struct zt_pvt *p = chan->pvt->pvt;
1677 if ((option != AST_OPTION_TONE_VERIFY) &&
1678 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
1684 if ((!cp) || (datalen < 1))
1690 case AST_OPTION_TONE_VERIFY:
1695 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
1696 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
1699 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
1700 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
1703 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
1704 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
1708 case AST_OPTION_TDD: /* turn on or off TDD */
1709 if (!*cp) { /* turn it off */
1710 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
1711 if (p->tdd) tdd_free(p->tdd);
1717 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
1718 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
1721 /* otherwise, turn it on */
1722 if (!p->didtdd) { /* if havent done it yet */
1723 unsigned char mybuf[41000],*buf;
1724 int size,res,fd,len;
1728 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
1729 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
1731 index = zt_get_index(chan, p, 0);
1733 ast_log(LOG_WARNING, "No index in TDD?\n");
1736 fd = p->subs[index].zfd;
1738 if (ast_check_hangup(chan)) return -1;
1740 if (size > READ_SIZE)
1746 res = select(fd + 1,NULL,&wfds,&efds,NULL);
1748 ast_log(LOG_DEBUG, "select (for write) ret. 0 on channel %d\n", p->channel);
1751 /* if got exception */
1752 if (FD_ISSET(fd,&efds)) return -1;
1753 if (!FD_ISSET(fd,&wfds)) {
1754 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
1757 res = write(fd, buf, size);
1759 if (res == -1) return -1;
1760 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
1766 p->didtdd = 1; /* set to have done it now */
1768 if (*cp == 2) { /* Mate mode */
1769 if (p->tdd) tdd_free(p->tdd);
1774 if (!p->tdd) { /* if we dont have one yet */
1775 p->tdd = tdd_new(); /* allocate one */
1778 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
1781 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
1786 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
1789 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
1796 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master)
1798 /* Unlink a specific slave or all slaves/masters from a given master */
1804 for (x=0;x<MAX_SLAVES;x++) {
1805 if (master->slaves[x]) {
1806 if (!slave || (master->slaves[x] == slave)) {
1807 /* Take slave out of the conference */
1808 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
1809 conf_del(&master->confno, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
1810 master->slaves[x]->master = NULL;
1811 master->slaves[x] = NULL;
1816 master->inconference = 0;
1819 if (master->master) {
1820 /* Take master out of the conference */
1821 conf_del(&master->master->confno, &master->subs[SUB_REAL], SUB_REAL);
1823 for (x=0;x<MAX_SLAVES;x++) {
1824 if (master->master->slaves[x] == master)
1825 master->master->slaves[x] = NULL;
1826 else if (master->master->slaves[x])
1830 master->master->inconference = 0;
1832 master->master = NULL;
1834 update_conf(master);
1837 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
1839 if (!slave || !master) {
1840 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
1843 for (x=0;x<MAX_SLAVES;x++) {
1844 if (!master->slaves[x]) {
1845 master->slaves[x] = slave;
1849 if (x >= MAX_SLAVES) {
1850 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
1851 master->slaves[MAX_SLAVES - 1] = slave;
1854 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
1855 slave->master = master;
1857 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
1860 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1862 struct ast_channel *who = NULL, *cs[3];
1863 struct zt_pvt *p0, *p1, *op0, *op1;
1864 struct zt_pvt *master=NULL, *slave=NULL;
1865 struct ast_frame *f;
1870 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
1871 int os1 = -1, os2 = -1;
1872 struct ast_channel *oc1, *oc2;
1874 /* if need DTMF, cant native bridge */
1875 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
1879 /* cant do pseudo-channels here */
1880 if ((!p0->sig) || (!p1->sig)) return -2;
1882 ast_pthread_mutex_lock(&c0->lock);
1883 ast_pthread_mutex_lock(&c1->lock);
1884 op0 = p0 = c0->pvt->pvt;
1885 op1 = p1 = c1->pvt->pvt;
1888 oi1 = zt_get_index(c0, p0, 0);
1889 oi2 = zt_get_index(c1, p1, 0);
1892 if ((oi1 < 0) || (oi2 < 0))
1897 ast_pthread_mutex_lock(&p0->lock);
1898 if (pthread_mutex_trylock(&p1->lock)) {
1899 /* Don't block, due to potential for deadlock */
1900 ast_pthread_mutex_unlock(&p0->lock);
1901 ast_pthread_mutex_unlock(&c0->lock);
1902 ast_pthread_mutex_unlock(&c1->lock);
1903 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
1906 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
1907 if (!p0->owner || !p1->owner) {
1908 /* Currently unowned -- Do nothing. */
1911 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
1912 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
1916 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
1921 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
1922 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,
1923 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
1926 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
1927 if (p1->subs[SUB_THREEWAY].inthreeway) {
1933 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
1934 if (p0->subs[SUB_THREEWAY].inthreeway) {
1940 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
1941 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
1942 don't put us in anything */
1943 if (p1->subs[SUB_CALLWAIT].inthreeway) {
1949 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
1950 /* Same as previous */
1951 if (p0->subs[SUB_CALLWAIT].inthreeway) {
1958 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
1959 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
1960 if (master && slave) {
1961 /* Stop any tones, or play ringtone as appropriate. If they're bridged
1962 in an active threeway call with a channel that is ringing, we should
1963 indicate ringing. */
1964 if ((oi2 == SUB_THREEWAY) &&
1965 p1->subs[SUB_THREEWAY].inthreeway &&
1966 p1->subs[SUB_REAL].owner &&
1967 p1->subs[SUB_REAL].inthreeway &&
1968 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
1969 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
1970 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
1971 os2 = p1->subs[SUB_REAL].owner->_state;
1973 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
1974 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
1976 if ((oi1 == SUB_THREEWAY) &&
1977 p0->subs[SUB_THREEWAY].inthreeway &&
1978 p0->subs[SUB_REAL].owner &&
1979 p0->subs[SUB_REAL].inthreeway &&
1980 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
1981 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
1982 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
1983 os1 = p0->subs[SUB_REAL].owner->_state;
1985 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
1986 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
1988 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
1989 if (!p0->echocanbridged || !p1->echocanbridged) {
1990 /* Disable echo cancellation if appropriate */
1995 zt_link(slave, master);
1996 master->inconference = inconf;
1997 } else if (!nothingok)
1998 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2002 t1 = p0->subs[SUB_REAL].inthreeway;
2003 t2 = p1->subs[SUB_REAL].inthreeway;
2005 ast_pthread_mutex_unlock(&p0->lock);
2006 ast_pthread_mutex_unlock(&p1->lock);
2008 ast_pthread_mutex_unlock(&c0->lock);
2009 ast_pthread_mutex_unlock(&c1->lock);
2011 /* Native bridge failed */
2012 if ((!master || !slave) && !nothingok) {
2024 /* Here's our main loop... Start by locking things, looking for private parts,
2025 and then balking if anything is wrong */
2026 ast_pthread_mutex_lock(&c0->lock);
2027 ast_pthread_mutex_lock(&c1->lock);
2031 i1 = zt_get_index(c0, p0, 1);
2033 i2 = zt_get_index(c1, p1, 1);
2034 ast_pthread_mutex_unlock(&c0->lock);
2035 ast_pthread_mutex_unlock(&c1->lock);
2036 if ((op0 != p0) || (op1 != p1) ||
2037 (ofd1 != c0->fds[0]) ||
2038 (ofd2 != c1->fds[0]) ||
2039 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2040 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2041 (oc1 != p0->owner) ||
2042 (oc2 != p1->owner) ||
2043 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2044 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2047 if (slave && master)
2048 zt_unlink(slave, master);
2049 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2050 op0->channel, oi1, op1->channel, oi2);
2058 who = ast_waitfor_n(cs, 2, &to);
2060 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2063 if (who->pvt->pvt == op0)
2064 op0->ignoredtmf = 1;
2065 else if (who->pvt->pvt == op1)
2066 op1->ignoredtmf = 1;
2068 if (who->pvt->pvt == op0)
2069 op0->ignoredtmf = 0;
2070 else if (who->pvt->pvt == op1)
2071 op1->ignoredtmf = 0;
2075 if (slave && master)
2076 zt_unlink(slave, master);
2083 if (f->frametype == AST_FRAME_DTMF) {
2084 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2085 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2088 if (slave && master)
2089 zt_unlink(slave, master);
2091 } else if ((who == c0) && p0->pulsedial) {
2093 } else if ((who == c1) && p1->pulsedial) {
2099 /* Swap who gets priority */
2106 static int zt_indicate(struct ast_channel *chan, int condition);
2108 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2110 struct zt_pvt *p = newchan->pvt->pvt;
2112 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2113 if (p->owner == oldchan)
2116 if (p->subs[x].owner == oldchan) {
2119 p->subs[x].owner = newchan;
2121 if (newchan->_state == AST_STATE_RINGING)
2122 zt_indicate(newchan, AST_CONTROL_RINGING);
2127 static int zt_ring_phone(struct zt_pvt *p)
2131 /* Make sure our transmit state is on hook */
2134 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2137 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2139 printf("Res: %d, error: %s\n", res, strerror(errno));
2145 /* Wait just in case */
2152 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2160 static void *ss_thread(void *data);
2162 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int);
2164 static int attempt_transfer(struct zt_pvt *p)
2166 /* In order to transfer, we need at least one of the channels to
2167 actually be in a call bridge. We can't conference two applications
2168 together (but then, why would we want to?) */
2169 if (p->subs[SUB_REAL].owner->bridge) {
2170 /* The three-way person we're about to transfer to could still be in MOH, so
2171 stop if now if appropriate */
2172 if (p->subs[SUB_THREEWAY].owner->bridge)
2173 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2174 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2175 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2177 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2178 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2179 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2182 /* Orphan the channel */
2183 unalloc_sub(p, SUB_THREEWAY);
2184 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2185 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2186 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2188 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2189 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2190 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2191 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2194 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2195 unalloc_sub(p, SUB_THREEWAY);
2196 /* Tell the caller not to hangup */
2199 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2200 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2201 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2207 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2209 struct ast_frame *f;
2210 f = &p->subs[index].f;
2212 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2216 case MFCR2_EVENT_BLOCKED:
2217 if (option_verbose > 2)
2218 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2220 case MFCR2_EVENT_UNBLOCKED:
2221 if (option_verbose > 2)
2222 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2224 case MFCR2_EVENT_CONFIG_ERR:
2225 if (option_verbose > 2)
2226 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2228 case MFCR2_EVENT_RING:
2229 if (option_verbose > 2)
2230 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2232 case MFCR2_EVENT_HANGUP:
2233 if (option_verbose > 2)
2234 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2236 case MFCR2_EVENT_RINGING:
2237 if (option_verbose > 2)
2238 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2240 case MFCR2_EVENT_ANSWER:
2241 if (option_verbose > 2)
2242 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2244 case MFCR2_EVENT_HANGUP_ACK:
2245 if (option_verbose > 2)
2246 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2248 case MFCR2_EVENT_IDLE:
2249 if (option_verbose > 2)
2250 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2253 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2259 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2264 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2266 ast_log(LOG_WARNING, "Unable to check received bits\n");
2270 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2273 e = mfcr2_cas_signaling_event(p->r2, x);
2278 static int check_for_conference(struct zt_pvt *p)
2281 /* Fine if we already have a master, etc */
2282 if (p->master || (p->confno > -1))
2284 memset(&ci, 0, sizeof(ci));
2285 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2286 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2289 /* If we have no master and don't have a confno, then
2290 if we're in a conference, it's probably a MeetMe room or
2291 some such, so don't let us 3-way out! */
2293 if (option_verbose > 2)
2294 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
2300 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
2304 struct zt_pvt *p = ast->pvt->pvt;
2306 pthread_attr_t attr;
2307 struct ast_channel *chan;
2308 pthread_attr_init(&attr);
2309 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2310 index = zt_get_index(ast, p, 0);
2311 p->subs[index].f.frametype = AST_FRAME_NULL;
2312 p->subs[index].f.datalen = 0;
2313 p->subs[index].f.samples = 0;
2314 p->subs[index].f.mallocd = 0;
2315 p->subs[index].f.offset = 0;
2316 p->subs[index].f.src = "zt_handle_event";
2317 p->subs[index].f.data = NULL;
2319 return &p->subs[index].f;
2320 res = zt_get_event(p->subs[index].zfd);
2321 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
2322 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
2323 if (res & ZT_EVENT_PULSEDIGIT)
2327 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
2328 p->subs[index].f.frametype = AST_FRAME_DTMF;
2329 p->subs[index].f.subclass = res & 0xff;
2330 /* Return the captured digit */
2331 return &p->subs[index].f;
2334 case ZT_EVENT_BITSCHANGED:
2335 if (p->sig == SIG_R2) {
2337 struct ast_frame *f = &p->subs[index].f;
2339 e = r2_get_event_bits(p);
2341 f = handle_r2_event(p, e, index);
2347 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
2348 case ZT_EVENT_PULSE_START:
2349 /* Stop tone if there's a pulse start and the PBX isn't started */
2351 tone_zone_play_tone(p->subs[index].zfd, -1);
2353 case ZT_EVENT_DIALCOMPLETE:
2354 if (p->inalarm) break;
2355 if (p->radio) break;
2356 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
2357 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
2360 if (!x) { /* if not still dialing in driver */
2363 if (ast->_state == AST_STATE_DIALING) {
2364 if (p->callprogress && CANPROGRESSDETECT(p) && p->dsp) {
2365 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
2366 } 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)))) {
2367 ast_setstate(ast, AST_STATE_RINGING);
2369 ast_setstate(ast, AST_STATE_UP);
2370 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2371 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2376 case ZT_EVENT_ALARM:
2378 ast_log(LOG_WARNING, "Detected alarm on channel %d\n", p->channel);
2379 /* fall through intentionally */
2380 case ZT_EVENT_ONHOOK:
2383 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2384 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
2391 p->onhooktime = time(NULL);
2393 /* Check for some special conditions regarding call waiting */
2394 if (index == SUB_REAL) {
2395 /* The normal line was hung up */
2396 if (p->subs[SUB_CALLWAIT].owner) {
2397 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
2398 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2399 if (option_verbose > 2)
2400 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2401 unalloc_sub(p, SUB_CALLWAIT);
2403 p->subs[index].needanswer = 0;
2404 p->subs[index].needringing = 0;
2406 p->callwaitingrepeat = 0;
2410 } else if (p->subs[SUB_THREEWAY].owner) {
2412 (ast->_state == AST_STATE_UP)) {
2414 /* In any case this isn't a threeway call anymore */
2415 p->subs[SUB_REAL].inthreeway = 0;
2416 p->subs[SUB_THREEWAY].inthreeway = 0;
2417 if ((res = attempt_transfer(p)) < 0)
2418 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2420 /* Don't actually hang up at this point */
2424 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2426 /* Swap subs and dis-own channel */
2427 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2429 /* Ring the phone */
2434 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
2442 case ZT_EVENT_RINGOFFHOOK:
2443 if (p->inalarm) break;
2446 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2447 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
2454 switch(ast->_state) {
2455 case AST_STATE_RINGING:
2457 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2458 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2459 /* Make sure it stops ringing */
2460 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
2461 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
2463 /* Cancel any running CallerID spill */
2469 if (p->confirmanswer) {
2470 /* Ignore answer if "confirm answer" is selected */
2471 p->subs[index].f.frametype = AST_FRAME_NULL;
2472 p->subs[index].f.subclass = 0;
2474 ast_setstate(ast, AST_STATE_UP);
2475 return &p->subs[index].f;
2476 case AST_STATE_DOWN:
2477 ast_setstate(ast, AST_STATE_RING);
2479 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2480 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
2481 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
2482 return &p->subs[index].f;
2484 /* Make sure it stops ringing */
2485 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
2486 /* Okay -- probably call waiting*/
2487 if (p->owner->bridge)
2488 ast_moh_stop(p->owner->bridge);
2491 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
2497 if (ast->_state == AST_STATE_RING) {
2506 if (ast->_state == AST_STATE_DOWN) {
2508 ast_log(LOG_DEBUG, "Ring detected\n");
2509 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2510 p->subs[index].f.subclass = AST_CONTROL_RING;
2511 } else if (ast->_state == AST_STATE_RINGING) {
2513 ast_log(LOG_DEBUG, "Line answered\n");
2514 if (p->confirmanswer) {
2515 p->subs[index].f.frametype = AST_FRAME_NULL;
2516 p->subs[index].f.subclass = 0;
2518 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2519 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2520 ast_setstate(ast, AST_STATE_UP);
2522 } else if (ast->_state != AST_STATE_RING)
2523 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
2526 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
2529 case ZT_EVENT_RINGEROFF:
2530 if (p->inalarm) break;
2531 if (p->radio) break;
2533 if ((ast->rings > p->cidrings) && (p->cidspill)) {
2534 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
2539 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2540 p->subs[index].f.subclass = AST_CONTROL_RINGING;
2542 case ZT_EVENT_RINGERON:
2544 case ZT_EVENT_NOALARM:
2547 case ZT_EVENT_WINKFLASH:
2548 if (p->inalarm) break;
2549 if (p->radio) break;
2554 ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
2555 index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2557 if (index == SUB_REAL) {
2558 if (p->subs[SUB_CALLWAIT].owner) {
2559 /* Swap to call-wait */
2560 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
2561 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2562 p->owner = p->subs[SUB_REAL].owner;
2563 ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
2564 if (p->owner->_state == AST_STATE_RINGING) {
2565 ast_setstate(p->owner, AST_STATE_UP);
2566 p->subs[SUB_REAL].needanswer = 1;
2568 p->callwaitingrepeat = 0;
2570 /* Start music on hold if appropriate */
2571 if (!p->subs[SUB_CALLWAIT].inthreeway && p->subs[SUB_CALLWAIT].owner->bridge)
2572 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
2573 if (p->subs[SUB_REAL].owner->bridge)
2574 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
2575 } else if (!p->subs[SUB_THREEWAY].owner) {
2576 if (p->threewaycalling && !check_for_conference(p)) {
2577 /* XXX This section needs much more error checking!!! XXX */
2578 /* Start a 3-way call if feasible */
2580 (ast->_state == AST_STATE_UP) ||
2581 (ast->_state == AST_STATE_RING)) {
2582 if (!alloc_sub(p, SUB_THREEWAY)) {
2583 /* Make new channel */
2584 chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0);
2585 /* Swap things around between the three-way and real call */
2586 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2587 /* Disable echo canceller for better dialing */
2589 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
2591 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
2593 if (pthread_create(&threadid, &attr, ss_thread, chan)) {
2594 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
2595 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2599 if (option_verbose > 2)
2600 ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);
2601 /* Start music on hold if appropriate */
2602 if (p->subs[SUB_THREEWAY].owner->bridge)
2603 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
2606 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
2608 ast_log(LOG_DEBUG, "Flash when call not up or ringing\n");
2611 /* Already have a 3 way call */
2612 if (p->subs[SUB_THREEWAY].inthreeway) {
2613 /* Call is already up, drop the last person */
2615 ast_log(LOG_DEBUG, "Got flash with three way call up, dropping last call on %d\n", p->channel);
2616 /* If the primary call isn't answered yet, use it */
2617 if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
2618 /* Swap back -- we're droppign the real 3-way that isn't finished yet*/
2619 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2620 p->owner = p->subs[SUB_REAL].owner;
2622 /* Drop the last call and stop the conference */
2623 if (option_verbose > 2)
2624 ast_verbose(VERBOSE_PREFIX_3 "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
2625 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2626 p->subs[SUB_REAL].inthreeway = 0;
2627 p->subs[SUB_THREEWAY].inthreeway = 0;
2629 /* Lets see what we're up to */
2631 (ast->_state == AST_STATE_UP)) {
2632 int otherindex = SUB_THREEWAY;
2633 if (option_verbose > 2)
2634 ast_verbose(VERBOSE_PREFIX_3 "Building conference on call on %s and %s\n", p->subs[SUB_THREEWAY].owner->name, p->subs[SUB_REAL].owner->name);
2635 /* Put them in the threeway, and flip */
2636 p->subs[SUB_THREEWAY].inthreeway = 1;
2637 p->subs[SUB_REAL].inthreeway = 1;
2638 if (ast->_state == AST_STATE_UP) {
2639 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2640 otherindex = SUB_REAL;
2642 if (p->subs[otherindex].owner && p->subs[otherindex].owner->bridge)
2643 ast_moh_stop(p->subs[otherindex].owner->bridge);
2644 p->owner = p->subs[SUB_REAL].owner;
2645 if (ast->_state == AST_STATE_RINGING) {
2646 ast_log(LOG_DEBUG, "Enabling ringtone on real and threeway\n");
2647 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
2648 res = tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, ZT_TONE_RINGTONE);
2651 if (option_verbose > 2)
2652 ast_verbose(VERBOSE_PREFIX_3 "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name);
2653 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2654 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2655 p->owner = p->subs[SUB_REAL].owner;
2656 if (p->subs[SUB_REAL].owner && p->subs[SUB_REAL].owner->bridge)
2657 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
2663 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
2673 ast_log(LOG_DEBUG, "Ignoring wink on channel %d\n", p->channel);
2675 ast_log(LOG_DEBUG, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
2679 /* FGD MF *Must* wait for wink */
2680 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
2682 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
2683 p->dop.dialstr[0] = '\0';
2686 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
2687 p->dop.dialstr[0] = '\0';
2690 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
2693 case ZT_EVENT_HOOKCOMPLETE:
2694 if (p->inalarm) break;
2695 if (p->radio) break;
2697 case SIG_FXSLS: /* only interesting for FXS */
2703 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
2705 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
2706 p->dop.dialstr[0] = '\0';
2709 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
2710 p->dop.dialstr[0] = '\0';
2714 ast_log(LOG_DEBUG, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
2721 ast_log(LOG_DEBUG, "Dunno what to do with event %d on channel %d\n", res, p->channel);
2723 return &p->subs[index].f;
2726 struct ast_frame *zt_exception(struct ast_channel *ast)
2728 struct zt_pvt *p = ast->pvt->pvt;
2733 index = zt_get_index(ast, p, 1);
2735 p->subs[index].f.frametype = AST_FRAME_NULL;
2736 p->subs[index].f.datalen = 0;
2737 p->subs[index].f.samples = 0;
2738 p->subs[index].f.mallocd = 0;
2739 p->subs[index].f.offset = 0;
2740 p->subs[index].f.subclass = 0;
2741 p->subs[index].f.src = "zt_exception";
2742 p->subs[index].f.data = NULL;
2745 if ((!p->owner) && (!p->radio)) {
2746 /* If nobody owns us, absorb the event appropriately, otherwise
2747 we loop indefinitely. This occurs when, during call waiting, the
2748 other end hangs up our channel so that it no longer exists, but we
2749 have neither FLASH'd nor ONHOOK'd to signify our desire to
2750 change to the other channel. */
2751 res = zt_get_event(p->subs[SUB_REAL].zfd);
2752 /* Switch to real if there is one and this isn't something really silly... */
2753 if ((res != ZT_EVENT_RINGEROFF) && (res != ZT_EVENT_RINGERON) &&
2754 (res != ZT_EVENT_HOOKCOMPLETE)) {
2755 ast_log(LOG_DEBUG, "Restoring owner of channel %d on event %d\n", p->channel, res);
2756 p->owner = p->subs[SUB_REAL].owner;
2757 if (p->owner && p->owner->bridge)
2758 ast_moh_stop(p->owner->bridge);
2761 case ZT_EVENT_ONHOOK:
2764 if (option_verbose > 2)
2765 ast_verbose(VERBOSE_PREFIX_3 "Channel %s still has call, ringing phone\n", p->owner->name);
2767 p->callwaitingrepeat = 0;
2770 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
2773 case ZT_EVENT_RINGOFFHOOK:
2774 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2775 if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
2776 p->subs[SUB_REAL].needanswer = 1;
2779 case ZT_EVENT_HOOKCOMPLETE:
2780 case ZT_EVENT_RINGERON:
2781 case ZT_EVENT_RINGEROFF:
2784 case ZT_EVENT_WINKFLASH:
2786 if (option_verbose > 2)
2787 ast_verbose(VERBOSE_PREFIX_3 "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
2788 if (p->owner->_state != AST_STATE_UP) {
2789 /* Answer if necessary */
2790 usedindex = zt_get_index(p->owner, p, 0);
2791 if (usedindex > -1) {
2792 p->subs[usedindex].needanswer = 1;
2794 ast_setstate(p->owner, AST_STATE_UP);
2796 p->callwaitingrepeat = 0;
2798 if (p->owner->bridge)
2799 ast_moh_stop(p->owner->bridge);
2801 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
2805 ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
2807 return &p->subs[index].f;
2809 if (!p->radio) ast_log(LOG_DEBUG, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
2810 /* If it's not us, return NULL immediately */
2811 if (ast != p->owner) {
2812 ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
2813 return &p->subs[index].f;
2815 return zt_handle_event(ast);
2818 struct ast_frame *zt_read(struct ast_channel *ast)
2820 struct zt_pvt *p = ast->pvt->pvt;
2824 struct ast_frame *f;
2827 ast_pthread_mutex_lock(&p->lock);
2829 index = zt_get_index(ast, p, 0);
2831 p->subs[index].f.frametype = AST_FRAME_NULL;
2832 p->subs[index].f.datalen = 0;
2833 p->subs[index].f.samples = 0;
2834 p->subs[index].f.mallocd = 0;
2835 p->subs[index].f.offset = 0;
2836 p->subs[index].f.subclass = 0;
2837 p->subs[index].f.src = "zt_read";
2838 p->subs[index].f.data = NULL;
2840 /* Hang up if we don't really exist */
2842 ast_log(LOG_WARNING, "We dont exist?\n");
2843 pthread_mutex_unlock(&p->lock);
2847 /* make sure it sends initial key state as first frame */
2848 if (p->radio && (!p->firstradio))
2852 ps.channo = p->channel;
2853 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0)
2856 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2859 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
2863 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
2865 pthread_mutex_unlock(&p->lock);
2866 return &p->subs[index].f;
2868 if (p->ringt == 1) {
2869 pthread_mutex_unlock(&p->lock);
2872 else if (p->ringt > 0)
2875 if (p->subs[index].needringing) {
2876 /* Send ringing frame if requested */
2877 p->subs[index].needringing = 0;
2878 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2879 p->subs[index].f.subclass = AST_CONTROL_RINGING;
2880 ast_setstate(ast, AST_STATE_RINGING);
2881 pthread_mutex_unlock(&p->lock);
2882 return &p->subs[index].f;
2885 if (p->subs[index].needcallerid) {
2886 ast_set_callerid(ast, strlen(p->lastcallerid) ? p->lastcallerid : NULL, 1);
2887 p->subs[index].needcallerid = 0;
2890 if (p->subs[index].needanswer) {
2891 /* Send ringing frame if requested */
2892 p->subs[index].needanswer = 0;
2893 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2894 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2895 ast_setstate(ast, AST_STATE_UP);
2896 pthread_mutex_unlock(&p->lock);
2897 return &p->subs[index].f;
2900 if (ast->pvt->rawreadformat == AST_FORMAT_SLINEAR) {
2901 if (!p->subs[index].linear) {
2902 p->subs[index].linear = 1;
2903 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
2905 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, index);
2907 } else if ((ast->pvt->rawreadformat == AST_FORMAT_ULAW) ||
2908 (ast->pvt->rawreadformat == AST_FORMAT_ALAW)) {
2909 if (p->subs[index].linear) {
2910 p->subs[index].linear = 0;
2911 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
2913 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to campanded mode.\n", p->channel, index);
2916 ast_log(LOG_WARNING, "Don't know how to read frames in format %d\n", ast->pvt->rawreadformat);
2917 pthread_mutex_unlock(&p->lock);
2920 readbuf = ((unsigned char *)p->subs[index].buffer) + AST_FRIENDLY_OFFSET;
2921 CHECK_BLOCKING(ast);
2922 res = read(p->subs[index].zfd, readbuf, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
2924 /* Check for hangup */
2927 if (errno == EAGAIN) {
2928 /* Return "NULL" frame if there is nobody there */
2929 pthread_mutex_unlock(&p->lock);
2930 return &p->subs[index].f;
2932 ast_log(LOG_WARNING, "zt_rec: %s\n", strerror(errno));
2934 pthread_mutex_unlock(&p->lock);
2937 if (res != (p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE)) {
2938 ast_log(LOG_DEBUG, "Short read (%d/%d), must be an event...\n", res, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
2939 pthread_mutex_unlock(&p->lock);
2940 return zt_handle_event(ast);
2942 if (p->tdd) { /* if in TDD mode, see if we receive that */
2945 c = tdd_feed(p->tdd,readbuf,READ_SIZE);
2947 ast_log(LOG_DEBUG,"tdd_feed failed\n");
2950 if (c) { /* if a char to return */
2951 p->subs[index].f.subclass = 0;
2952 p->subs[index].f.frametype = AST_FRAME_TEXT;
2953 p->subs[index].f.mallocd = 0;
2954 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
2955 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET;
2956 p->subs[index].f.datalen = 1;
2957 *((char *) p->subs[index].f.data) = c;
2958 pthread_mutex_unlock(&p->lock);
2959 return &p->subs[index].f;
2962 if (p->callwaitingrepeat)
2963 p->callwaitingrepeat--;
2966 /* Repeat callwaiting */
2967 if (p->callwaitingrepeat == 1) {
2972 if (p->cidcwexpire == 1) {
2973 if (option_verbose > 2)
2974 ast_verbose(VERBOSE_PREFIX_3 "CPE does not support Call Waiting Caller*ID.\n");
2975 restore_conference(p);
2977 if (p->subs[index].linear) {
2978 p->subs[index].f.datalen = READ_SIZE * 2;
2980 p->subs[index].f.datalen = READ_SIZE;
2982 /* Handle CallerID Transmission */
2983 if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
2987 p->subs[index].f.frametype = AST_FRAME_VOICE;
2988 p->subs[index].f.subclass = ast->pvt->rawreadformat;
2989 p->subs[index].f.samples = READ_SIZE;
2990 p->subs[index].f.mallocd = 0;
2991 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
2992 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET/2;
2994 ast_log(LOG_DEBUG, "Read %d of voice on %s\n", p->subs[index].f.datalen, ast->name);
2996 if (p->dialing || /* Transmitting something */
2997 (index && (ast->_state != AST_STATE_UP)) || /* Three-way or callwait that isn't up */
2998 ((index == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway) /* Inactive and non-confed call-wait */
3000 /* Whoops, we're still dialing, or in a state where we shouldn't transmit....
3001 don't send anything */
3002 p->subs[index].f.frametype = AST_FRAME_NULL;
3003 p->subs[index].f.subclass = 0;
3004 p->subs[index].f.samples = 0;
3005 p->subs[index].f.mallocd = 0;
3006 p->subs[index].f.offset = 0;
3007 p->subs[index].f.data = NULL;
3008 p->subs[index].f.datalen= 0;
3010 if (p->dsp && !p->ignoredtmf && !index) {
3011 /* Perform busy detection. etc on the zap line */
3012 f = ast_dsp_process(ast, p->dsp, &p->subs[index].f, 0);
3014 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
3015 if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
3016 /* Treat this as a "hangup" instead of a "busy" on the assumption that
3020 } else if (f->frametype == AST_FRAME_DTMF) {
3021 /* DSP clears us of being pulse */
3026 f = &p->subs[index].f;
3027 if (f && (f->frametype == AST_FRAME_DTMF)) {
3028 ast_log(LOG_DEBUG, "DTMF digit: %c on %s\n", f->subclass, ast->name);
3029 if (p->confirmanswer) {
3030 ast_log(LOG_DEBUG, "Confirm answer on %s!\n", ast->name);
3031 /* Upon receiving a DTMF digit, consider this an answer confirmation instead
3033 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3034 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3035 ast_setstate(ast, AST_STATE_UP);
3036 f = &p->subs[index].f;
3037 } else if (p->callwaitcas) {
3038 if ((f->subclass == 'A') || (f->subclass == 'D')) {
3039 ast_log(LOG_DEBUG, "Got some DTMF, but it's for the CAS\n");
3045 p->subs[index].f.frametype = AST_FRAME_NULL;
3046 p->subs[index].f.subclass = 0;
3047 f = &p->subs[index].f;
3048 } else if (f->subclass == 'f') {
3049 /* Fax tone -- Handle and return NULL */
3050 if (!p->faxhandled) {
3052 if (strcmp(ast->exten, "fax")) {
3053 if (ast_exists_extension(ast, ast->context, "fax", 1, ast->callerid)) {
3054 if (option_verbose > 2)
3055 ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
3056 if (ast_async_goto(ast, ast->context, "fax", 1, 0))
3057 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, ast->context);
3059 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
3061 ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
3063 ast_log(LOG_DEBUG, "Fax already handled\n");
3065 p->subs[index].f.frametype = AST_FRAME_NULL;
3066 p->subs[index].f.subclass = 0;
3067 f = &p->subs[index].f;
3068 } else if (f->subclass == 'm') {
3069 /* Confmute request */
3071 p->subs[index].f.frametype = AST_FRAME_NULL;
3072 p->subs[index].f.subclass = 0;
3073 f = &p->subs[index].f;
3074 } else if (f->subclass == 'u') {
3077 p->subs[index].f.frametype = AST_FRAME_NULL;
3078 p->subs[index].f.subclass = 0;
3079 f = &p->subs[index].f;
3084 if (f->frametype == AST_FRAME_VOICE && (ast->_state == AST_STATE_UP)) {
3085 p->subs[index].f.frametype = AST_FRAME_NULL;
3086 p->subs[index].f.subclass = 0;
3087 f = &p->subs[index].f;
3090 pthread_mutex_unlock(&p->lock);
3094 static int my_zt_write(struct zt_pvt *p, unsigned char *buf, int len, int index, int linear)
3100 fd = p->subs[index].zfd;
3103 if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
3104 size = (linear ? READ_SIZE * 2 : READ_SIZE);
3105 res = write(fd, buf, size);
3108 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
3117 static int zt_write(struct ast_channel *ast, struct ast_frame *frame)
3119 struct zt_pvt *p = ast->pvt->pvt;
3121 unsigned char outbuf[4096];
3124 index = zt_get_index(ast, p, 0);
3126 ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
3130 /* Write a frame of (presumably voice) data */
3131 if (frame->frametype != AST_FRAME_VOICE) {
3132 if (frame->frametype != AST_FRAME_IMAGE)
3133 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
3136 if ((frame->subclass != AST_FORMAT_SLINEAR) &&
3137 (frame->subclass != AST_FORMAT_ULAW) &&
3138 (frame->subclass != AST_FORMAT_ALAW)) {
3139 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
3144 ast_log(LOG_DEBUG, "Dropping frame since I'm still dialing on %s...\n",ast->name);
3149 ast_log(LOG_DEBUG, "Dropping frame since I've still got a callerid spill\n");
3152 /* Return if it's not valid data */
3153 if (!frame->data || !frame->datalen)
3155 if (frame->datalen > sizeof(outbuf) * 2) {
3156 ast_log(LOG_WARNING, "Frame too large\n");
3160 if (frame->subclass == AST_FORMAT_SLINEAR) {
3161 if (!p->subs[index].linear) {
3162 p->subs[index].linear = 1;
3163 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
3165 ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
3167 res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 1);
3170 if (p->subs[index].linear) {
3171 p->subs[index].linear = 0;
3172 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
3174 ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
3176 res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 0);
3179 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
3185 static int zt_indicate(struct ast_channel *chan, int condition)
3187 struct zt_pvt *p = chan->pvt->pvt;
3189 int index = zt_get_index(chan, p, 0);
3190 if (index == SUB_REAL) {
3192 case AST_CONTROL_BUSY:
3193 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
3195 case AST_CONTROL_RINGING:
3196 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_RINGTON