3 * Asterisk -- A telephony toolkit for Linux.
5 * Zaptel Pseudo TDM interface
7 * Copyright (C) 2003 Digium
9 * Mark Spencer <markster@digium.com>
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License
17 #include <asterisk/lock.h>
18 #include <asterisk/channel.h>
19 #include <asterisk/channel_pvt.h>
20 #include <asterisk/config.h>
21 #include <asterisk/logger.h>
22 #include <asterisk/module.h>
23 #include <asterisk/pbx.h>
24 #include <asterisk/options.h>
25 #include <asterisk/file.h>
26 #include <asterisk/ulaw.h>
27 #include <asterisk/alaw.h>
28 #include <asterisk/callerid.h>
29 #include <asterisk/adsi.h>
30 #include <asterisk/cli.h>
31 #include <asterisk/cdr.h>
32 #include <asterisk/parking.h>
33 #include <asterisk/musiconhold.h>
34 #include <asterisk/say.h>
35 #include <asterisk/tdd.h>
36 #include <asterisk/app.h>
37 #include <asterisk/dsp.h>
38 #include <asterisk/astdb.h>
39 #include <asterisk/manager.h>
40 #include <asterisk/causes.h>
41 #include <asterisk/term.h>
42 #include <asterisk/utils.h>
43 #include <sys/signal.h>
48 #include <sys/ioctl.h>
49 #include <linux/zaptel.h>
60 #include "../asterisk.h"
63 #error "Your zaptel is too old. please cvs update"
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 (0x100000 | ZT_SIG_EM)
109 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
110 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
111 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
112 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
113 #define SIG_FXSLS ZT_SIG_FXSLS
114 #define SIG_FXSGS ZT_SIG_FXSGS
115 #define SIG_FXSKS ZT_SIG_FXSKS
116 #define SIG_FXOLS ZT_SIG_FXOLS
117 #define SIG_FXOGS ZT_SIG_FXOGS
118 #define SIG_FXOKS ZT_SIG_FXOKS
119 #define SIG_PRI ZT_SIG_CLEAR
120 #define SIG_R2 ZT_SIG_CAS
121 #define SIG_SF ZT_SIG_SF
122 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
123 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
124 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
125 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
126 #define SIG_EM_E1 ZT_SIG_EM_E1
129 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
131 #define CHAN_PSEUDO -2
133 static char context[AST_MAX_EXTENSION] = "default";
134 static char callerid[256] = "";
136 static char language[MAX_LANGUAGE] = "";
137 static char musicclass[MAX_LANGUAGE] = "";
138 static char progzone[10]= "";
140 static int usedistinctiveringdetection = 0;
142 static int use_callerid = 1;
143 static int zaptrcallerid = 0;
144 static int cur_signalling = -1;
146 static unsigned int cur_group = 0;
147 static unsigned int cur_callergroup = 0;
148 static unsigned int cur_pickupgroup = 0;
149 static int relaxdtmf = 0;
151 static int immediate = 0;
153 static int stripmsd = 0;
155 static int callwaiting = 0;
157 static int callwaitingcallerid = 0;
159 static int hidecallerid = 0;
161 static int restrictcid = 0;
163 static int use_callingpres = 0;
165 static int callreturn = 0;
167 static int threewaycalling = 0;
169 static int transfer = 0;
171 static int cancallforward = 0;
173 static float rxgain = 0.0;
175 static float txgain = 0.0;
177 static int tonezone = -1;
179 static int echocancel;
181 static int echotraining;
183 static int echocanbridged = 0;
185 static int busydetect = 0;
187 static int busycount = 3;
189 static int callprogress = 0;
191 static char accountcode[20] = "";
193 static char mailbox[AST_MAX_EXTENSION];
195 static int amaflags = 0;
199 static int numbufs = 4;
201 static int cur_prewink = -1;
202 static int cur_preflash = -1;
203 static int cur_wink = -1;
204 static int cur_flash = -1;
205 static int cur_start = -1;
206 static int cur_rxwink = -1;
207 static int cur_rxflash = -1;
208 static int cur_debounce = -1;
211 static int minunused = 2;
212 static int minidle = 0;
213 static char idleext[AST_MAX_EXTENSION];
214 static char idledial[AST_MAX_EXTENSION];
215 static int overlapdial = 0;
218 /* Wait up to 16 seconds for first digit (FXO logic) */
219 static int firstdigittimeout = 16000;
221 /* How long to wait for following digits (FXO logic) */
222 static int gendigittimeout = 8000;
224 /* How long to wait for an extra digit, if there is an ambiguous match */
225 static int matchdigittimeout = 3000;
227 static int usecnt =0;
228 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
230 /* Protect the interface list (of zt_pvt's) */
231 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
233 static int ifcount = 0;
235 /* Protect the monitoring thread, so only one process can kill or start it, and not
236 when it's doing something critical. */
237 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
239 /* This is the thread for the monitor which checks for input on the channels
240 which are not currently in use. */
241 static pthread_t monitor_thread = AST_PTHREADT_NULL;
243 static int restart_monitor(void);
245 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
247 static int zt_sendtext(struct ast_channel *c, char *text);
249 static inline int zt_get_event(int fd)
251 /* Avoid the silly zt_getevent which ignores a bunch of events */
253 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
257 static inline int zt_wait_event(int fd)
259 /* Avoid the silly zt_waitevent which ignores a bunch of events */
261 i = ZT_IOMUX_SIGEVENT;
262 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
263 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
267 /* Chunk size to read -- we use 20ms chunks to make things happy. */
268 #define READ_SIZE 160
270 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
271 #define MASK_INUSE (1 << 1) /* Channel currently in use */
273 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
274 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
275 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
276 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
277 #define RINGT ( (8000 * 8) / READ_SIZE)
283 static int r2prot = -1;
289 pthread_t master; /* Thread of master */
290 ast_mutex_t lock; /* Mutex */
291 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
292 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
293 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
294 int minunused; /* Min # of channels to keep empty */
295 int minidle; /* Min # of "idling" calls to keep active */
296 int nodetype; /* Node type */
297 int switchtype; /* Type of switch to emulate */
298 int dialplan; /* Dialing plan */
299 int dchannel; /* What channel the dchannel is on */
300 int channels; /* Num of chans in span (31 or 24) */
301 int overlapdial; /* In overlap dialing mode */
308 int chanmask[32]; /* Channel status */
312 struct zt_pvt *pvt[32]; /* Member channel pvt structs */
313 struct zt_channel *chan[32]; /* Channels on each line */
317 static struct zt_pri pris[NUM_SPANS];
319 static int pritype = PRI_CPE;
322 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
324 #define DEFAULT_PRI_DEBUG 0
327 static inline void pri_rel(struct zt_pri *pri)
329 ast_mutex_unlock(&pri->lock);
332 static int switchtype = PRI_SWITCH_NI2;
333 static int dialplan = PRI_NATIONAL_ISDN + 1;
337 #define SUB_REAL 0 /* Active call */
338 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
339 #define SUB_THREEWAY 2 /* Three-way call */
342 static struct zt_distRings drings;
344 struct distRingData {
347 struct ringContextData {
348 char contextData[AST_MAX_EXTENSION];
350 struct zt_distRings {
351 struct distRingData ringnum[3];
352 struct ringContextData ringContext[3];
355 static char *subnames[] = {
361 struct zt_subchannel {
363 struct ast_channel *owner;
365 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
366 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
377 #define CONF_USER_REAL (1 << 0)
378 #define CONF_USER_THIRDCALL (1 << 1)
382 static struct zt_pvt {
384 struct ast_channel *owner; /* Our current active owner (if applicable) */
385 /* Up to three channels can be associated with this call */
387 struct zt_subchannel sub_unused; /* Just a safety precaution */
388 struct zt_subchannel subs[3]; /* Sub-channels */
389 struct zt_confinfo saveconf; /* Saved conference info */
391 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
392 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
393 int inconference; /* If our real should be in the conference */
395 int sig; /* Signalling style */
396 int radio; /* radio type */
397 int firstradio; /* first radio flag */
400 int tonezone; /* tone zone for this chan, or -1 for default */
401 struct zt_pvt *next; /* Next channel in list */
402 struct zt_pvt *prev; /* Prev channel in list */
404 struct zt_distRings drings;
405 int usedistinctiveringdetection;
407 char context[AST_MAX_EXTENSION];
408 char defcontext[AST_MAX_EXTENSION];
409 char exten[AST_MAX_EXTENSION];
410 char language[MAX_LANGUAGE];
411 char musicclass[MAX_LANGUAGE];
412 char callerid[AST_MAX_EXTENSION];
413 char lastcallerid[AST_MAX_EXTENSION];
414 char *origcallerid; /* malloced original callerid */
415 char callwaitcid[AST_MAX_EXTENSION];
416 char rdnis[AST_MAX_EXTENSION];
417 char dnid[AST_MAX_EXTENSION];
420 int confno; /* Our conference */
421 int confusers; /* Who is using our conference */
422 int propconfno; /* Propagated conference number */
423 unsigned int callgroup;
424 unsigned int pickupgroup;
425 int immediate; /* Answer before getting digits? */
426 int channel; /* Channel Number */
427 int span; /* Span number */
429 time_t guardtime; /* Must wait this much time before using for new call */
431 int use_callerid; /* Whether or not to use caller id on this channel */
434 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
435 int restrictcid; /* Whether restrict the callerid -> only send ANI */
436 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
437 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
438 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
439 int cidcwexpire; /* When to expire our muting for CID/CW */
440 unsigned char *cidspill;
455 int callwaitingcallerid;
464 struct timeval flashtime; /* Last flash-hook time */
466 int cref; /* Call reference number */
467 ZT_DIAL_OPERATION dop;
471 char accountcode[20]; /* Account code */
472 int amaflags; /* AMA Flags */
473 char didtdd; /* flag to say its done it once */
474 struct tdd_state *tdd; /* TDD flag */
477 char call_forward[AST_MAX_EXTENSION];
478 char mailbox[AST_MAX_EXTENSION];
483 int confirmanswer; /* Wait for '#' to confirm answer */
484 int distinctivering; /* Which distinctivering to use */
485 int cidrings; /* Which ring to deliver CID on */
487 int faxhandled; /* Has a fax tone already been handled? */
489 char mate; /* flag to say its in MATE mode */
490 int pulsedial; /* whether a pulse dial phone is detected */
491 int dtmfrelax; /* whether to run in relaxed DTMF mode */
493 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
501 #ifdef PRI_EVENT_PROCEEDING
504 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
513 } *iflist = NULL, *ifend = NULL;
515 struct zt_pvt *round_robin[32];
518 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
521 /* Grab the lock first */
523 res = ast_mutex_trylock(&pri->lock);
525 ast_mutex_unlock(&pvt->lock);
526 /* Release the lock and try again */
528 ast_mutex_lock(&pvt->lock);
531 /* Then break the poll */
532 pthread_kill(pri->master, SIGURG);
537 #define NUM_CADENCE_MAX 25
538 static int num_cadence = 4;
539 static int user_has_defined_cadences = 0;
541 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
542 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
543 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
544 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
545 { { 1000, 500, 2500, 5000 } }, /* Long ring */
548 int receivedRingT; /* Used to find out what ringtone we are on */
550 /* cidrings says in which pause to transmit the cid information, where the first pause
551 * is 1, the second pause is 2 and so on.
554 static int cidrings[NUM_CADENCE_MAX] = {
555 2, /* Right after first long ring */
556 4, /* Right after long part */
557 3, /* After third chirp */
558 2, /* Second spell */
561 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
562 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
564 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
565 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
568 /* translate between PRI causes and asterisk's */
569 static int hangup_pri2cause(int cause)
572 case PRI_CAUSE_USER_BUSY:
573 return AST_CAUSE_BUSY;
574 case PRI_CAUSE_NORMAL_CLEARING:
575 return AST_CAUSE_NORMAL;
576 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
577 case PRI_CAUSE_REQUESTED_CHAN_UNAVAIL:
578 return AST_CAUSE_CONGESTION;
579 case PRI_CAUSE_UNALLOCATED:
580 case PRI_CAUSE_NUMBER_CHANGED:
581 return AST_CAUSE_UNALLOCATED;
583 return AST_CAUSE_FAILURE;
589 /* translate between ast cause and PRI */
590 static int hangup_cause2pri(int cause)
594 return PRI_CAUSE_USER_BUSY;
595 case AST_CAUSE_UNALLOCATED:
596 return PRI_CAUSE_UNALLOCATED;
597 case AST_CAUSE_CONGESTION:
598 return PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION;
599 case AST_CAUSE_NORMAL:
601 return PRI_CAUSE_NORMAL_CLEARING;
608 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
611 if (p->subs[0].owner == ast)
613 else if (p->subs[1].owner == ast)
615 else if (p->subs[2].owner == ast)
620 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
625 static void wakeup_sub(struct zt_pvt *p, int a)
627 struct ast_frame null = { AST_FRAME_NULL, };
629 if (p->subs[a].owner) {
630 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
631 ast_mutex_unlock(&p->lock);
633 ast_mutex_lock(&p->lock);
635 ast_queue_frame(p->subs[a].owner, &null);
636 ast_mutex_unlock(&p->subs[a].owner->lock);
644 static void swap_subs(struct zt_pvt *p, int a, int b)
648 struct ast_channel *towner;
650 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
652 tchan = p->subs[a].chan;
653 towner = p->subs[a].owner;
654 tinthreeway = p->subs[a].inthreeway;
656 p->subs[a].chan = p->subs[b].chan;
657 p->subs[a].owner = p->subs[b].owner;
658 p->subs[a].inthreeway = p->subs[b].inthreeway;
660 p->subs[b].chan = tchan;
661 p->subs[b].owner = towner;
662 p->subs[b].inthreeway = tinthreeway;
664 if (p->subs[a].owner)
665 p->subs[a].owner->fds[0] = p->subs[a].zfd;
666 if (p->subs[b].owner)
667 p->subs[b].owner->fds[0] = p->subs[b].zfd;
672 static int zt_open(char *fn)
680 for (x=0;x<strlen(fn);x++) {
681 if (!isdigit(fn[x])) {
689 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
692 fn = "/dev/zap/channel";
694 fd = open(fn, O_RDWR | O_NONBLOCK);
696 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
700 if (ioctl(fd, ZT_SPECIFY, &chan)) {
704 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
709 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
713 static void zt_close(int fd)
718 int zt_setlinear(int zfd, int linear)
721 res = ioctl(zfd, ZT_SETLINEAR, &linear);
728 int zt_setlaw(int zfd, int law)
731 res = ioctl(zfd, ZT_SETLAW, &law);
737 static int alloc_sub(struct zt_pvt *p, int x)
741 if (p->subs[x].zfd < 0) {
742 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
743 if (p->subs[x].zfd > -1) {
744 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
746 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
747 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
748 bi.numbufs = numbufs;
749 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
751 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
754 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
755 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
756 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
757 zt_close(p->subs[x].zfd);
762 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
765 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
768 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
772 static int unalloc_sub(struct zt_pvt *p, int x)
775 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
778 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
779 if (p->subs[x].zfd > -1) {
780 zt_close(p->subs[x].zfd);
783 p->subs[x].linear = 0;
785 p->subs[x].owner = NULL;
786 p->subs[x].inthreeway = 0;
787 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
791 static int zt_digit(struct ast_channel *ast, char digit)
793 ZT_DIAL_OPERATION zo;
798 index = zt_get_index(ast, p, 0);
799 if (index == SUB_REAL) {
801 #ifdef PRI_EVENT_SETUP_ACK
802 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && p->setup_ack && !p->proceeding) {
804 #ifdef PRI_EVENT_PROCEEDING
805 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && !p->proceeding) {
807 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING) {
810 if (!pri_grab(p, p->pri)) {
811 pri_information(p->pri->pri,p->call,digit);
814 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
819 zo.op = ZT_DIAL_OP_APPEND;
821 zo.dialstr[1] = digit;
823 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
824 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
833 static char *events[] = {
846 "Hook Transition Complete",
855 { ZT_ALARM_RED, "Red Alarm" },
856 { ZT_ALARM_YELLOW, "Yellow Alarm" },
857 { ZT_ALARM_BLUE, "Blue Alarm" },
858 { ZT_ALARM_RECOVER, "Recovering" },
859 { ZT_ALARM_LOOPBACK, "Loopback" },
860 { ZT_ALARM_NOTOPEN, "Not Open" },
861 { ZT_ALARM_NONE, "None" },
864 static char *alarm2str(int alarm)
867 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
868 if (alarms[x].alarm & alarm)
869 return alarms[x].name;
871 return alarm ? "Unknown Alarm" : "No Alarm";
874 static char *event2str(int event)
876 static char buf[256];
877 if ((event < 15) && (event > -1))
878 return events[event];
879 sprintf(buf, "Event %d", event);
884 static int str2r2prot(char *swtype)
886 if (!strcasecmp(swtype, "ar"))
887 return MFCR2_PROT_ARGENTINA;
889 if (!strcasecmp(swtype, "cn"))
890 return MFCR2_PROT_CHINA;
892 if (!strcasecmp(swtype, "kr"))
893 return MFCR2_PROT_KOREA;
899 static char *sig2str(int sig)
901 static char buf[256];
904 return "E & M Immediate";
910 return "Feature Group D (DTMF)";
912 return "Feature Group D (MF)";
914 return "Feature Group B (MF)";
918 return "FXS Loopstart";
920 return "FXS Groundstart";
922 return "FXS Kewlstart";
924 return "FXO Loopstart";
926 return "FXO Groundstart";
928 return "FXO Kewlstart";
930 return "PRI Signalling";
932 return "R2 Signalling";
934 return "SF (Tone) Signalling Immediate";
936 return "SF (Tone) Signalling Wink";
938 return "SF (Tone) Signalling with Feature Group D (DTMF)";
940 return "SF (Tone) Signalling with Feature Group D (MF)";
942 return "SF (Tone) Signalling with Feature Group B (MF)";
944 return "Pseudo Signalling";
946 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
951 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
953 /* If the conference already exists, and we're already in it
954 don't bother doing anything */
957 memset(&zi, 0, sizeof(zi));
960 if (slavechannel > 0) {
961 /* If we have only one slave, do a digital mon */
962 zi.confmode = ZT_CONF_DIGITALMON;
963 zi.confno = slavechannel;
966 /* Real-side and pseudo-side both participate in conference */
967 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
968 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
970 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
971 zi.confno = p->confno;
973 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
977 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
978 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
981 if (slavechannel < 1) {
982 p->confno = zi.confno;
984 memcpy(&c->curconf, &zi, sizeof(c->curconf));
985 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
989 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
991 /* If they're listening to our channel, they're ours */
992 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
994 /* If they're a talker on our (allocated) conference, they're ours */
995 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1000 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1003 if (/* Can't delete if there's no zfd */
1005 /* Don't delete from the conference if it's not our conference */
1007 /* Don't delete if we don't think it's conferenced at all (implied) */
1009 memset(&zi, 0, sizeof(zi));
1013 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1014 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1017 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1018 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1022 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1026 struct zt_pvt *slave = NULL;
1027 /* Start out optimistic */
1029 /* Update conference state in a stateless fashion */
1031 /* Any three-way calling makes slave native mode *definitely* out
1033 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1036 /* If we don't have any 3-way calls, check to see if we have
1037 precisely one slave */
1038 if (useslavenative) {
1039 for (x=0;x<MAX_SLAVES;x++) {
1042 /* Whoops already have a slave! No
1043 slave native and stop right away */
1048 /* We have one slave so far */
1049 slave = p->slaves[x];
1054 /* If no slave, slave native definitely out */
1057 else if (slave->law != p->law) {
1063 return useslavenative;
1066 static int update_conf(struct zt_pvt *p)
1071 struct zt_pvt *slave = NULL;
1073 useslavenative = isslavenative(p, &slave);
1074 /* Start with the obvious, general stuff */
1076 /* Look for three way calls */
1077 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1078 conf_add(p, &p->subs[x], x, 0);
1081 conf_del(p, &p->subs[x], x);
1084 /* If we have a slave, add him to our conference now. or DAX
1085 if this is slave native */
1086 for (x=0;x<MAX_SLAVES;x++) {
1089 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, p->channel);
1091 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1096 /* If we're supposed to be in there, do so now */
1097 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1099 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, slave->channel);
1101 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1105 /* If we have a master, add ourselves to his conference */
1107 if (isslavenative(p->master, NULL)) {
1108 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, p->master->channel);
1110 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1114 /* Nobody is left (or should be left) in our conference.
1118 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1122 static void zt_enable_ec(struct zt_pvt *p)
1127 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1130 if (p && p->echocancel) {
1131 if (p->sig == SIG_PRI) {
1133 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1135 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1138 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1140 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1143 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1146 ast_log(LOG_DEBUG, "No echocancellation requested\n");
1149 static void zt_train_ec(struct zt_pvt *p)
1153 if (p && p->echocancel && p->echotraining) {
1155 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1157 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1159 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1162 ast_log(LOG_DEBUG, "No echo training requested\n");
1165 static void zt_disable_ec(struct zt_pvt *p)
1169 if (p->echocancel) {
1171 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1173 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1175 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1180 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1187 if ((rxgain != 0.0) || (txgain != 0.0)) {
1188 /* caluculate linear value of tx gain */
1189 ltxgain = pow(10.0,txgain / 20.0);
1190 /* caluculate linear value of rx gain */
1191 lrxgain = pow(10.0,rxgain / 20.0);
1192 if (law == ZT_LAW_ALAW) {
1193 for (j=0;j<256;j++) {
1194 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1195 if (k > 32767) k = 32767;
1196 if (k < -32767) k = -32767;
1197 g.rxgain[j] = AST_LIN2A(k);
1198 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1199 if (k > 32767) k = 32767;
1200 if (k < -32767) k = -32767;
1201 g.txgain[j] = AST_LIN2A(k);
1204 for (j=0;j<256;j++) {
1205 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1206 if (k > 32767) k = 32767;
1207 if (k < -32767) k = -32767;
1208 g.rxgain[j] = AST_LIN2MU(k);
1209 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1210 if (k > 32767) k = 32767;
1211 if (k < -32767) k = -32767;
1212 g.txgain[j] = AST_LIN2MU(k);
1216 for (j=0;j<256;j++) {
1223 return(ioctl(fd,ZT_SETGAINS,&g));
1226 static inline int zt_set_hook(int fd, int hs)
1230 res = ioctl(fd, ZT_HOOK, &x);
1233 if (errno == EINPROGRESS) return 0;
1234 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1239 static inline int zt_confmute(struct zt_pvt *p, int muted)
1243 if (p->sig == SIG_PRI) {
1245 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1247 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1249 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1251 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1255 static int save_conference(struct zt_pvt *p)
1257 struct zt_confinfo c;
1259 if (p->saveconf.confmode) {
1260 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1263 p->saveconf.chan = 0;
1264 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1266 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1267 p->saveconf.confmode = 0;
1272 c.confmode = ZT_CONF_NORMAL;
1273 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1275 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1279 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1283 static int restore_conference(struct zt_pvt *p)
1286 if (p->saveconf.confmode) {
1287 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1288 p->saveconf.confmode = 0;
1290 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1295 ast_log(LOG_DEBUG, "Restored conferencing\n");
1299 static int send_callerid(struct zt_pvt *p);
1301 int send_cwcidspill(struct zt_pvt *p)
1305 p->cidspill = malloc(MAX_CALLERID_SIZE);
1307 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1308 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
1309 /* Make sure we account for the end */
1310 p->cidlen += READ_SIZE * 4;
1313 if (option_verbose > 2)
1314 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
1319 static int has_voicemail(struct zt_pvt *p)
1322 return ast_app_has_voicemail(p->mailbox);
1325 static int send_callerid(struct zt_pvt *p)
1327 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1329 /* Take out of linear mode if necessary */
1330 if (p->subs[SUB_REAL].linear) {
1331 p->subs[SUB_REAL].linear = 0;
1332 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1334 while(p->cidpos < p->cidlen) {
1335 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1337 if (errno == EAGAIN)
1340 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1350 if (p->callwaitcas) {
1351 /* Wait for CID/CW to expire */
1352 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1354 restore_conference(p);
1358 static int zt_callwait(struct ast_channel *ast)
1360 struct zt_pvt *p = ast->pvt->pvt;
1361 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1363 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1366 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1370 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1371 if (!p->callwaitrings && p->callwaitingcallerid) {
1372 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1374 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1376 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1378 p->cidlen = 2400 + READ_SIZE * 4;
1383 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1389 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1391 struct zt_pvt *p = ast->pvt->pvt;
1398 char dest[256]; /* must be same length as p->dialdest */
1399 ast_mutex_lock(&p->lock);
1400 strncpy(dest, rdest, sizeof(dest) - 1);
1401 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1402 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1403 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1404 ast_mutex_unlock(&p->lock);
1408 if (p->radio) /* if a radio channel, up immediately */
1410 /* Special pseudo -- automatically up */
1411 ast_setstate(ast, AST_STATE_UP);
1412 ast_mutex_unlock(&p->lock);
1415 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1416 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1418 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1421 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1427 if (p->owner == ast) {
1428 /* Normal ring, on hook */
1430 /* Don't send audio while on hook, until the call is answered */
1432 if (p->use_callerid) {
1433 /* Generate the Caller-ID spill if desired */
1435 ast_log(LOG_WARNING, "cidspill already exists??\n");
1438 p->cidspill = malloc(MAX_CALLERID_SIZE);
1441 p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
1445 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1447 /* Choose proper cadence */
1448 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1449 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1450 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1451 p->cidrings = cidrings[p->distinctivering - 1];
1453 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1454 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1455 p->cidrings = DEFAULT_CIDRINGS;
1459 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1460 c = strchr(dest, '/');
1463 if (c && (strlen(c) < p->stripmsd)) {
1464 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1468 p->dop.op = ZT_DIAL_OP_REPLACE;
1469 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1470 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1472 strcpy(p->dop.dialstr, "");
1475 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1476 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1477 ast_mutex_unlock(&p->lock);
1482 /* Call waiting call */
1483 p->callwaitrings = 0;
1485 strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
1487 strcpy(p->callwaitcid, "");
1488 /* Call waiting tone instead */
1489 if (zt_callwait(ast)) {
1490 ast_mutex_unlock(&p->lock);
1493 /* Make ring-back */
1494 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1495 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1499 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1501 strcpy(callerid, "");
1502 ast_callerid_parse(callerid, &n, &l);
1504 ast_shrink_phone_number(l);
1505 if (!ast_isphonenumber(l))
1509 strcpy(p->lastcallerid, l);
1511 strcpy(p->lastcallerid, "");
1512 ast_setstate(ast, AST_STATE_RINGING);
1513 index = zt_get_index(ast, p, 0);
1515 p->subs[index].needringing = 1;
1531 case SIG_SF_FEATDMF:
1533 c = strchr(dest, '/');
1538 if (strlen(c) < p->stripmsd) {
1539 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1540 ast_mutex_unlock(&p->lock);
1544 /* Start the trunk */
1545 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1547 if (errno != EINPROGRESS) {
1548 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1549 ast_mutex_unlock(&p->lock);
1553 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1554 p->dop.op = ZT_DIAL_OP_REPLACE;
1555 if (p->sig == SIG_FEATD) {
1556 if (ast->callerid) {
1557 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1558 ast_callerid_parse(callerid, &n, &l);
1560 ast_shrink_phone_number(l);
1561 if (!ast_isphonenumber(l))
1567 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1569 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1571 if (p->sig == SIG_FEATDMF) {
1572 if (ast->callerid) {
1573 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1574 ast_callerid_parse(callerid, &n, &l);
1576 ast_shrink_phone_number(l);
1577 if (!ast_isphonenumber(l))
1583 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1585 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1587 if (p->sig == SIG_E911) {
1588 strcpy(p->dop.dialstr,"M*911#");
1590 if (p->sig == SIG_FEATB) {
1591 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1593 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1594 if (strlen(p->dop.dialstr) > 4) {
1595 strcpy(p->echorest, "w");
1596 strcpy(p->echorest + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1598 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1602 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1604 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1605 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1606 ast_mutex_unlock(&p->lock);
1610 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1612 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1613 ast_setstate(ast, AST_STATE_DIALING);
1617 c = strchr(dest, '/');
1622 if (ast->callerid && !p->hidecallerid) {
1623 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1624 ast_callerid_parse(callerid, &n, &l);
1626 ast_shrink_phone_number(l);
1627 if (!ast_isphonenumber(l))
1632 if (strlen(c) < p->stripmsd) {
1633 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1634 ast_mutex_unlock(&p->lock);
1637 p->dop.op = ZT_DIAL_OP_REPLACE;
1638 s = strchr(c + p->stripmsd, 'w');
1640 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1643 strcpy(p->dop.dialstr, "");
1645 if (pri_grab(p, p->pri)) {
1646 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1647 ast_mutex_unlock(&p->lock);
1650 if (!(p->call = pri_new_call(p->pri->pri))) {
1651 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1653 ast_mutex_unlock(&p->lock);
1656 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1657 if (pri_call(p->pri->pri, p->call, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1658 p->prioffset, p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1, l, p->pri->dialplan - 1, n,
1659 l ? (ast->restrictcid ? PRES_PROHIB_USER_NUMBER_PASSED_SCREEN : (p->use_callingpres ? ast->callingpres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN)) : PRES_NUMBER_NOT_AVAILABLE,
1660 c + p->stripmsd, p->pri->dialplan - 1,
1662 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)))) {
1663 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1665 ast_mutex_unlock(&p->lock);
1669 ast_setstate(ast, AST_STATE_DIALING);
1674 /* Special pseudo -- automatically up*/
1675 ast_setstate(ast, AST_STATE_UP);
1678 ast_log(LOG_DEBUG, "not yet implemented\n");
1679 ast_mutex_unlock(&p->lock);
1682 ast_mutex_unlock(&p->lock);
1686 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1696 for (i = 0; i < 3; i++) {
1697 if (cur->subs[i].owner) {
1703 prev->next = cur->next;
1705 prev->next->prev = prev;
1711 iflist->prev = NULL;
1715 if (cur->subs[SUB_REAL].zfd > -1) {
1716 zt_close(cur->subs[SUB_REAL].zfd);
1722 prev->next = cur->next;
1724 prev->next->prev = prev;
1730 iflist->prev = NULL;
1734 if (cur->subs[SUB_REAL].zfd > -1) {
1735 zt_close(cur->subs[SUB_REAL].zfd);
1743 static int zt_hangup(struct ast_channel *ast)
1747 static int restore_gains(struct zt_pvt *p);
1748 struct zt_pvt *p = ast->pvt->pvt;
1749 struct zt_pvt *tmp = NULL;
1750 struct zt_pvt *prev = NULL;
1754 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1755 if (!ast->pvt->pvt) {
1756 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1760 ast_mutex_lock(&p->lock);
1762 index = zt_get_index(ast, p, 1);
1764 if (p->sig == SIG_PRI) {
1766 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1772 if (p->origcallerid) {
1773 strncpy(p->callerid, p->origcallerid, sizeof(p->callerid) - 1);
1774 free(p->origcallerid);
1775 p->origcallerid = NULL;
1778 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1780 strcpy(p->exten, "");
1782 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1783 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1787 /* Real channel, do some fixup */
1788 p->subs[index].owner = NULL;
1789 p->subs[index].needanswer = 0;
1790 p->subs[index].needringing = 0;
1791 p->subs[index].needbusy = 0;
1792 p->subs[index].needcongestion = 0;
1793 p->subs[index].linear = 0;
1794 p->subs[index].needcallerid = 0;
1795 zt_setlinear(p->subs[index].zfd, 0);
1796 if (index == SUB_REAL) {
1797 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1798 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1799 if (p->subs[SUB_CALLWAIT].inthreeway) {
1800 /* We had flipped over to answer a callwait and now it's gone */
1801 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1802 /* Move to the call-wait, but un-own us until they flip back. */
1803 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1804 unalloc_sub(p, SUB_CALLWAIT);
1807 /* The three way hung up, but we still have a call wait */
1808 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1809 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1810 unalloc_sub(p, SUB_THREEWAY);
1811 if (p->subs[SUB_REAL].inthreeway) {
1812 /* This was part of a three way call. Immediately make way for
1814 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1815 p->owner = p->subs[SUB_REAL].owner;
1817 /* This call hasn't been completed yet... Set owner to NULL */
1818 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1821 p->subs[SUB_REAL].inthreeway = 0;
1823 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
1824 /* Move to the call-wait and switch back to them. */
1825 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1826 unalloc_sub(p, SUB_CALLWAIT);
1827 p->owner = p->subs[SUB_REAL].owner;
1828 if (p->owner->_state != AST_STATE_UP)
1829 p->subs[SUB_REAL].needanswer = 1;
1830 if (p->subs[SUB_REAL].owner->bridge)
1831 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
1832 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
1833 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1834 unalloc_sub(p, SUB_THREEWAY);
1835 if (p->subs[SUB_REAL].inthreeway) {
1836 /* This was part of a three way call. Immediately make way for
1838 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1839 p->owner = p->subs[SUB_REAL].owner;
1841 /* This call hasn't been completed yet... Set owner to NULL */
1842 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1845 p->subs[SUB_REAL].inthreeway = 0;
1847 } else if (index == SUB_CALLWAIT) {
1848 /* Ditch the holding callwait call, and immediately make it availabe */
1849 if (p->subs[SUB_CALLWAIT].inthreeway) {
1850 /* This is actually part of a three way, placed on hold. Place the third part
1851 on music on hold now */
1852 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
1853 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
1854 p->subs[SUB_THREEWAY].inthreeway = 0;
1855 /* Make it the call wait now */
1856 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
1857 unalloc_sub(p, SUB_THREEWAY);
1859 unalloc_sub(p, SUB_CALLWAIT);
1860 } else if (index == SUB_THREEWAY) {
1861 if (p->subs[SUB_CALLWAIT].inthreeway) {
1862 /* The other party of the three way call is currently in a call-wait state.
1863 Start music on hold for them, and take the main guy out of the third call */
1864 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
1865 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
1866 p->subs[SUB_CALLWAIT].inthreeway = 0;
1868 p->subs[SUB_REAL].inthreeway = 0;
1869 /* If this was part of a three way call index, let us make
1870 another three way call */
1871 unalloc_sub(p, SUB_THREEWAY);
1873 /* This wasn't any sort of call, but how are we an index? */
1874 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
1879 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
1882 p->distinctivering = 0;
1883 p->confirmanswer = 0;
1889 p->onhooktime = time(NULL);
1890 #ifdef PRI_EVENT_PROCEEDING
1893 #ifdef PRI_EVENT_SETUP_ACK
1897 ast_dsp_free(p->dsp);
1901 law = ZT_LAW_DEFAULT;
1902 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
1904 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
1905 /* Perform low level hangup if no owner left */
1907 if (p->sig == SIG_PRI) {
1909 if (!pri_grab(p, p->pri)) {
1910 #ifndef NEW_PRI_HANGUP
1911 if (!p->alreadyhungup) {
1912 res = pri_disconnect(p->pri->pri, p->call, PRI_CAUSE_NORMAL_CLEARING);
1914 pri_release(p->pri->pri, p->call, -1);
1916 p->alreadyhungup = 0;
1920 #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
1922 if (p->alreadyhungup) {
1923 pri_hangup(p->pri->pri, p->call, -1);
1926 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
1927 int icause = ast->hangupcause ? hangup_cause2pri(ast->hangupcause) : -1;
1928 p->alreadyhungup = 1;
1931 icause = atoi(cause);
1933 pri_hangup(p->pri->pri, p->call, icause);
1937 ast_log(LOG_WARNING, "pri_disconnect failed\n");
1940 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1949 if (p->sig == SIG_R2) {
1951 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
1960 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
1962 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
1968 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
1971 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
1973 /* If they're off hook, try playing congestion */
1974 if (par.rxisoffhook)
1975 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
1977 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1983 /* Make sure we're not made available for at least two seconds */
1984 time(&p->guardtime);
1988 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
1995 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
1996 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2000 p->callwaiting = p->permcallwaiting;
2001 p->hidecallerid = p->permhidecallerid;
2003 strcpy(p->rdnis, "");
2005 /* Restore data mode */
2006 if (p->sig == SIG_PRI) {
2008 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2015 p->callwaitingrepeat = 0;
2017 ast->pvt->pvt = NULL;
2018 ast_mutex_unlock(&p->lock);
2019 ast_mutex_lock(&usecnt_lock);
2022 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2023 ast_mutex_unlock(&usecnt_lock);
2024 ast_update_use_count();
2025 if (option_verbose > 2)
2026 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2028 ast_mutex_lock(&iflock);
2034 destroy_channel(prev, tmp, 0);
2042 ast_mutex_unlock(&iflock);
2046 static int zt_answer(struct ast_channel *ast)
2048 struct zt_pvt *p = ast->pvt->pvt;
2051 int oldstate = ast->_state;
2052 ast_setstate(ast, AST_STATE_UP);
2053 ast_mutex_lock(&p->lock);
2054 index = zt_get_index(ast, p, 0);
2057 /* nothing to do if a radio channel */
2059 ast_mutex_unlock(&p->lock);
2078 case SIG_SF_FEATDMF:
2083 /* Pick up the line */
2084 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2085 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2086 tone_zone_play_tone(p->subs[index].zfd, -1);
2088 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2089 if (oldstate == AST_STATE_RINGING) {
2090 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2091 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2092 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2093 p->owner = p->subs[SUB_REAL].owner;
2096 if (p->sig & __ZT_SIG_FXS) {
2103 /* Send a pri acknowledge */
2104 if (!pri_grab(p, p->pri)) {
2106 res = pri_answer(p->pri->pri, p->call, 0, 1);
2109 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2116 res = mfcr2_AnswerCall(p->r2, NULL);
2118 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2122 ast_mutex_unlock(&p->lock);
2125 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2128 ast_mutex_unlock(&p->lock);
2132 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2137 struct zt_pvt *p = chan->pvt->pvt;
2140 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2141 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2147 if ((!cp) || (datalen < 1))
2153 case AST_OPTION_TONE_VERIFY:
2158 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2159 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2162 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2163 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2166 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2167 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2171 case AST_OPTION_TDD: /* turn on or off TDD */
2172 if (!*cp) { /* turn it off */
2173 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2174 if (p->tdd) tdd_free(p->tdd);
2180 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2181 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2184 /* otherwise, turn it on */
2185 if (!p->didtdd) { /* if havent done it yet */
2186 unsigned char mybuf[41000],*buf;
2187 int size,res,fd,len;
2189 struct pollfd fds[1];
2191 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2192 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2194 index = zt_get_index(chan, p, 0);
2196 ast_log(LOG_WARNING, "No index in TDD?\n");
2199 fd = p->subs[index].zfd;
2201 if (ast_check_hangup(chan)) return -1;
2203 if (size > READ_SIZE)
2206 fds[0].events = POLLPRI | POLLOUT;
2207 res = poll(fds, 1, -1);
2209 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2212 /* if got exception */
2213 if (fds[0].revents & POLLPRI) return -1;
2214 if (!(fds[0].revents & POLLOUT)) {
2215 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2218 res = write(fd, buf, size);
2220 if (res == -1) return -1;
2221 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2227 p->didtdd = 1; /* set to have done it now */
2229 if (*cp == 2) { /* Mate mode */
2230 if (p->tdd) tdd_free(p->tdd);
2235 if (!p->tdd) { /* if we dont have one yet */
2236 p->tdd = tdd_new(); /* allocate one */
2239 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2242 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2247 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2250 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2252 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2255 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2261 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2264 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2265 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2272 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2274 /* Unlink a specific slave or all slaves/masters from a given master */
2280 ast_mutex_lock(&master->lock);
2282 while(ast_mutex_trylock(&slave->lock)) {
2283 ast_mutex_unlock(&master->lock);
2285 ast_mutex_lock(&master->lock);
2290 for (x=0;x<MAX_SLAVES;x++) {
2291 if (master->slaves[x]) {
2292 if (!slave || (master->slaves[x] == slave)) {
2293 /* Take slave out of the conference */
2294 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2295 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2296 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2297 master->slaves[x]->master = NULL;
2298 master->slaves[x] = NULL;
2303 master->inconference = 0;
2306 if (master->master) {
2307 /* Take master out of the conference */
2308 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2309 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2311 for (x=0;x<MAX_SLAVES;x++) {
2312 if (master->master->slaves[x] == master)
2313 master->master->slaves[x] = NULL;
2314 else if (master->master->slaves[x])
2318 master->master->inconference = 0;
2320 master->master = NULL;
2322 update_conf(master);
2325 ast_mutex_unlock(&slave->lock);
2326 ast_mutex_unlock(&master->lock);
2330 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2332 if (!slave || !master) {
2333 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2336 for (x=0;x<MAX_SLAVES;x++) {
2337 if (!master->slaves[x]) {
2338 master->slaves[x] = slave;
2342 if (x >= MAX_SLAVES) {
2343 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2344 master->slaves[MAX_SLAVES - 1] = slave;
2347 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2348 slave->master = master;
2350 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2353 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2355 struct ast_channel *who = NULL, *cs[3];
2356 struct zt_pvt *p0, *p1, *op0, *op1;
2357 struct zt_pvt *master=NULL, *slave=NULL;
2358 struct ast_frame *f;
2363 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2364 int os1 = -1, os2 = -1;
2365 struct ast_channel *oc1, *oc2;
2367 /* if need DTMF, cant native bridge */
2368 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2371 ast_mutex_lock(&c0->lock);
2372 ast_mutex_lock(&c1->lock);
2376 /* cant do pseudo-channels here */
2377 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2378 ast_mutex_unlock(&c0->lock);
2379 ast_mutex_unlock(&c1->lock);
2383 op0 = p0 = c0->pvt->pvt;
2384 op1 = p1 = c1->pvt->pvt;
2387 oi1 = zt_get_index(c0, p0, 0);
2388 oi2 = zt_get_index(c1, p1, 0);
2391 if ((oi1 < 0) || (oi2 < 0)) {
2392 ast_mutex_unlock(&c0->lock);
2393 ast_mutex_unlock(&c1->lock);
2399 ast_mutex_lock(&p0->lock);
2400 if (ast_mutex_trylock(&p1->lock)) {
2401 /* Don't block, due to potential for deadlock */
2402 ast_mutex_unlock(&p0->lock);
2403 ast_mutex_unlock(&c0->lock);
2404 ast_mutex_unlock(&c1->lock);
2405 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2408 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2409 if (!p0->owner || !p1->owner) {
2410 /* Currently unowned -- Do nothing. */
2413 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2414 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2418 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2423 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2424 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,
2425 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2428 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2429 if (p1->subs[SUB_THREEWAY].inthreeway) {
2435 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2436 if (p0->subs[SUB_THREEWAY].inthreeway) {
2442 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2443 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2444 don't put us in anything */
2445 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2451 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2452 /* Same as previous */
2453 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2460 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2461 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2462 if (master && slave) {
2463 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2464 in an active threeway call with a channel that is ringing, we should
2465 indicate ringing. */
2466 if ((oi2 == SUB_THREEWAY) &&
2467 p1->subs[SUB_THREEWAY].inthreeway &&
2468 p1->subs[SUB_REAL].owner &&
2469 p1->subs[SUB_REAL].inthreeway &&
2470 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2471 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2472 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2473 os2 = p1->subs[SUB_REAL].owner->_state;
2475 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2476 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2478 if ((oi1 == SUB_THREEWAY) &&
2479 p0->subs[SUB_THREEWAY].inthreeway &&
2480 p0->subs[SUB_REAL].owner &&
2481 p0->subs[SUB_REAL].inthreeway &&
2482 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2483 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2484 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2485 os1 = p0->subs[SUB_REAL].owner->_state;
2487 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2488 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2490 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2491 if (!p0->echocanbridged || !p1->echocanbridged) {
2492 /* Disable echo cancellation if appropriate */
2497 zt_link(slave, master);
2498 master->inconference = inconf;
2499 } else if (!nothingok)
2500 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2504 t1 = p0->subs[SUB_REAL].inthreeway;
2505 t2 = p1->subs[SUB_REAL].inthreeway;
2507 ast_mutex_unlock(&p0->lock);
2508 ast_mutex_unlock(&p1->lock);
2510 ast_mutex_unlock(&c0->lock);
2511 ast_mutex_unlock(&c1->lock);
2513 /* Native bridge failed */
2514 if ((!master || !slave) && !nothingok) {
2526 /* Here's our main loop... Start by locking things, looking for private parts,
2527 and then balking if anything is wrong */
2528 ast_mutex_lock(&c0->lock);
2529 ast_mutex_lock(&c1->lock);
2533 i1 = zt_get_index(c0, p0, 1);
2535 i2 = zt_get_index(c1, p1, 1);
2536 ast_mutex_unlock(&c0->lock);
2537 ast_mutex_unlock(&c1->lock);
2538 if ((op0 != p0) || (op1 != p1) ||
2539 (ofd1 != c0->fds[0]) ||
2540 (ofd2 != c1->fds[0]) ||
2541 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2542 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2543 (oc1 != p0->owner) ||
2544 (oc2 != p1->owner) ||
2545 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2546 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2549 if (slave && master)
2550 zt_unlink(slave, master, 1);
2551 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2552 op0->channel, oi1, op1->channel, oi2);
2560 who = ast_waitfor_n(cs, 2, &to);
2562 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2565 if (who->pvt->pvt == op0)
2566 op0->ignoredtmf = 1;
2567 else if (who->pvt->pvt == op1)
2568 op1->ignoredtmf = 1;
2570 if (who->pvt->pvt == op0)
2571 op0->ignoredtmf = 0;
2572 else if (who->pvt->pvt == op1)
2573 op1->ignoredtmf = 0;
2577 if (slave && master)
2578 zt_unlink(slave, master, 1);
2585 if (f->frametype == AST_FRAME_DTMF) {
2586 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2587 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2590 if (slave && master)
2591 zt_unlink(slave, master, 1);
2593 } else if ((who == c0) && p0->pulsedial) {
2595 } else if ((who == c1) && p1->pulsedial) {
2601 /* Swap who gets priority */
2608 static int zt_indicate(struct ast_channel *chan, int condition);
2610 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2612 struct zt_pvt *p = newchan->pvt->pvt;
2614 ast_mutex_lock(&p->lock);
2615 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2616 if (p->owner == oldchan)
2619 if (p->subs[x].owner == oldchan) {
2621 zt_unlink(NULL, p, 0);
2622 p->subs[x].owner = newchan;
2624 if (newchan->_state == AST_STATE_RINGING)
2625 zt_indicate(newchan, AST_CONTROL_RINGING);
2627 ast_mutex_unlock(&p->lock);
2631 static int zt_ring_phone(struct zt_pvt *p)
2635 /* Make sure our transmit state is on hook */
2638 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2641 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2643 printf("Res: %d, error: %s\n", res, strerror(errno));
2649 /* Wait just in case */
2656 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2664 static void *ss_thread(void *data);
2666 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2668 static int attempt_transfer(struct zt_pvt *p)
2670 /* In order to transfer, we need at least one of the channels to
2671 actually be in a call bridge. We can't conference two applications
2672 together (but then, why would we want to?) */
2673 if (p->subs[SUB_REAL].owner->bridge) {
2674 /* The three-way person we're about to transfer to could still be in MOH, so
2675 stop if now if appropriate */
2676 if (p->subs[SUB_THREEWAY].owner->bridge)
2677 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2678 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2679 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2681 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2682 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2683 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2686 /* Orphan the channel */
2687 unalloc_sub(p, SUB_THREEWAY);
2688 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2689 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2690 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2692 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2693 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2694 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2695 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2698 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2699 unalloc_sub(p, SUB_THREEWAY);
2700 /* Tell the caller not to hangup */
2703 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2704 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2705 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2711 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2713 struct ast_frame *f;
2714 f = &p->subs[index].f;
2716 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2720 case MFCR2_EVENT_BLOCKED:
2721 if (option_verbose > 2)
2722 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2724 case MFCR2_EVENT_UNBLOCKED:
2725 if (option_verbose > 2)
2726 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2728 case MFCR2_EVENT_CONFIG_ERR:
2729 if (option_verbose > 2)
2730 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2732 case MFCR2_EVENT_RING:
2733 if (option_verbose > 2)
2734 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2736 case MFCR2_EVENT_HANGUP:
2737 if (option_verbose > 2)
2738 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2740 case MFCR2_EVENT_RINGING:
2741 if (option_verbose > 2)
2742 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2744 case MFCR2_EVENT_ANSWER:
2745 if (option_verbose > 2)
2746 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2748 case MFCR2_EVENT_HANGUP_ACK:
2749 if (option_verbose > 2)
2750 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2752 case MFCR2_EVENT_IDLE:
2753 if (option_verbose > 2)
2754 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2757 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2763 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2768 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2770 ast_log(LOG_WARNING, "Unable to check received bits\n");
2774 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2777 e = mfcr2_cas_signaling_event(p->r2, x);
2782 static int check_for_conference(struct zt_pvt *p)
2785 /* Fine if we already have a master, etc */
2786 if (p->master || (p->confno > -1))
2788 memset(&ci, 0, sizeof(ci));
2789 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2790 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2793 /* If we have no master and don't have a confno, then
2794 if we're in a conference, it's probably a MeetMe room or
2795 some such, so don't let us 3-way out! */
2796 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
2797 if (option_verbose > 2)
2798 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
2804 static int get_alarms(struct zt_pvt *p)
2808 memset(&zi, 0, sizeof(zi));
2809 zi.spanno = p->span;
2810 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
2812 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
2818 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
2823 struct zt_pvt *p = ast->pvt->pvt;
2825 pthread_attr_t attr;
2826 struct ast_channel *chan;
2827 pthread_attr_init(&attr);
2828 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2829 index = zt_get_index(ast, p, 0);
2830 p->subs[index].f.frametype = AST_FRAME_NULL;
2831 p->subs[index].f.datalen = 0;
2832 p->subs[index].f.samples = 0;
2833 p->subs[index].f.mallocd = 0;
2834 p->subs[index].f.offset = 0;
2835 p->subs[index].f.src = "zt_handle_event";
2836 p->subs[index].f.data = NULL;
2838 return &p->subs[index].f;
2839 if (p->fake_event) {
2840 res = p->fake_event;
2843 res = zt_get_event(p->subs[index].zfd);
2844 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
2845 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
2846 if (res & ZT_EVENT_PULSEDIGIT)
2850 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
2851 p->subs[index].f.frametype = AST_FRAME_DTMF;
2852 p->subs[index].f.subclass = res & 0xff;
2853 /* Return the captured digit */
2854 return &p->subs[index].f;
2857 case ZT_EVENT_BITSCHANGED:
2858 if (p->sig == SIG_R2) {
2860 struct ast_frame *f = &p->subs[index].f;
2862 e = r2_get_event_bits(p);
2864 f = handle_r2_event(p, e, index);
2870 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
2871 case ZT_EVENT_PULSE_START:
2872 /* Stop tone if there's a pulse start and the PBX isn't started */
2874 tone_zone_play_tone(p->subs[index].zfd, -1);
2876 case ZT_EVENT_DIALCOMPLETE:
2877 if (p->inalarm) break;
2878 if (p->radio) break;
2879 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
2880 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
2883 if (!x) { /* if not still dialing in driver */
2887 strcpy(p->dop.dialstr, p->echorest);
2888 p->dop.op = ZT_DIAL_OP_REPLACE;
2889 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
2893 if (p->sig == SIG_E911) {
2894 /* if thru with dialing after offhook */
2895 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
2896 ast_setstate(ast, AST_STATE_UP);
2897 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2898 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2900 } else { /* if to state wait for offhook to dial rest */
2901 /* we now wait for off hook */
2902 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
2905 if (ast->_state == AST_STATE_DIALING) {
2906 if (p->callprogress && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
2907 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
2908 } else if (p->confirmanswer || (!p->dialednone && ((p->sig == SIG_EM) || (p->sig == SIG_EM_E1) || (p->sig == SIG_EMWINK) || (p->sig == SIG_FEATD) || (p->sig == SIG_FEATDMF) || (p->sig == SIG_E911) || (p->sig == SIG_FEATB) || (p->sig == SIG_SF) || (p->sig == SIG_SFWINK) || (p->sig == SIG_SF_FEATD) || (p->sig == SIG_SF_FEATDMF) || (p->sig == SIG_SF_FEATB)))) {
2909 ast_setstate(ast, AST_STATE_RINGING);
2911 ast_setstate(ast, AST_STATE_UP);
2912 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2913 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
2919 case ZT_EVENT_ALARM:
2921 #ifdef PRI_DESTROYCALL
2923 if (p->pri && p->pri->pri) {
2924 pri_hangup(p->pri->pri, p->call, -1);
2925 pri_destroycall(p->pri->pri, p->call);
2927 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
2930 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2933 #error Please "cvs update" and recompile libpri
2937 res = get_alarms(p);
2938 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
2939 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
2942 alarm2str(res), p->channel);
2943 /* fall through intentionally */
2944 case ZT_EVENT_ONHOOK:
2947 p->subs[index].f.frametype = AST_FRAME_CONTROL;
2948 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
2955 p->onhooktime = time(NULL);
2957 /* Check for some special conditions regarding call waiting */
2958 if (index == SUB_REAL) {
2959 /* The normal line was hung up */
2960 if (p->subs[SUB_CALLWAIT].owner) {
2961 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
2962 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2963 if (option_verbose > 2)
2964 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
2965 unalloc_sub(p, SUB_CALLWAIT);
2967 p->subs[index].needanswer = 0;
2968 p->subs[index].needringing = 0;
2970 p->callwaitingrepeat = 0;
2974 } else if (p->subs[SUB_THREEWAY].owner) {
2976 unsigned int mssinceflash;
2977 gettimeofday(&tv, NULL);
2978 mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
2979 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
2980 if (mssinceflash < MIN_MS_SINCE_FLASH) {
2981 /* It hasn't been long enough since the last flashook. This is probably a bounce on
2982 hanging up. Hangup both channels now */
2983 if (p->subs[SUB_THREEWAY].owner)
2984 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
2985 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2986 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
2987 } else if ((ast->pbx) ||
2988 (ast->_state == AST_STATE_UP)) {
2990 /* In any case this isn't a threeway call anymore */
2991 p->subs[SUB_REAL].inthreeway = 0;
2992 p->subs[SUB_THREEWAY].inthreeway = 0;
2993 if ((res = attempt_transfer(p)) < 0)
2994 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2996 /* Don't actually hang up at this point */
3000 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3002 /* Swap subs and dis-own channel */
3003 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3005 /* Ring the phone */
3010 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
3018 case ZT_EVENT_RINGOFFHOOK:
3019 if (p->inalarm) break;
3022 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3023 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
3026 /* for E911, its supposed to wait for offhook then dial
3027 the second half of the dial string */
3028 if ((p->sig == SIG_E911) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
3029 c = strchr(p->dialdest, '/');
3034 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
3035 else strcpy(p->dop.dialstr,"M*2#");
3036 if (strlen(p->dop.dialstr) > 4) {
3037 strcpy(p->echorest, "w");
3038 strcpy(p->echorest + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
3040 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
3043 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
3045 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
3046 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
3050 return &p->subs[index].f;
3056 switch(ast->_state) {
3057 case AST_STATE_RINGING:
3060 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3061 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3062 /* Make sure it stops ringing */
3063 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
3064 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
3066 /* Cancel any running CallerID spill */
3072 if (p->confirmanswer) {
3073 /* Ignore answer if "confirm answer" is enabled */
3074 p->subs[index].f.frametype = AST_FRAME_NULL;
3075 p->subs[index].f.subclass = 0;
3076 } else if (!ast_strlen_zero(p->dop.dialstr)) {
3077 /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
3078 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
3080 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
3081 p->dop.dialstr[0] = '\0';
3084 ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
3085 p->subs[index].f.frametype = AST_FRAME_NULL;
3086 p->subs[index].f.subclass = 0;
3089 p->dop.dialstr[0] = '\0';
3090 ast_setstate(ast, AST_STATE_DIALING);
3092 ast_setstate(ast, AST_STATE_UP);
3093 return &p->subs[index].f;
3094 case AST_STATE_DOWN:
3095 ast_setstate(ast, AST_STATE_RING);
3097 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3098 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
3099 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
3100 return &p->subs[index].f;
3102 /* Make sure it stops ringing */
3103 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
3104 /* Okay -- probably call waiting*/
3105 if (p->owner->bridge)
3106 ast_moh_stop(p->owner->bridge);
3109 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
3115 if (ast->_state == AST_STATE_RING) {
3129 case SIG_SF_FEATDMF:
3131 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
3133 ast_log(LOG_DEBUG, "Ring detected\n");
3134 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3135 p->subs[index].f.subclass = AST_CONTROL_RING;
3136 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
3138 ast_log(LOG_DEBUG, "Line answered\n");
3139 if (p->confirmanswer) {
3140 p->subs[index].f.frametype = AST_FRAME_NULL;
3141 p->subs[index].f.subclass = 0;
3143 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3144 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3145 ast_setstate(ast, AST_STATE_UP);
3147 } else if (ast->_state != AST_STATE_RING)
3148 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
3151 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
3154 case ZT_EVENT_RINGEROFF:
3155 if (p->inalarm) break;
3156 if (p->radio) break;
3158 if ((ast->rings > p->cidrings) && (p->cidspill)) {
3159 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
3164 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3165 p->subs[index].f.subclass = AST_CONTROL_RINGING;
3167 case ZT_EVENT_RINGERON:
3169 case ZT_EVENT_NOALARM:
3171 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
3172 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
3173 "Channel: %d\r\n", p->channel);
3175 case ZT_EVENT_WINKFLASH:
3176 if (p->inalarm) break;
3177 if (p->radio) break;
3178 /* Remember last time we got a flash-hook */
3179 gettimeofday(&p->flashtime, NULL);
3184 ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
3185 index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
3187 if (index == SUB_REAL) {
3188 if (p->subs[SUB_CALLWAIT].owner) {
3189 /* Swap to call-wait */
3190 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
3191 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3192 p->owner = p->subs[SUB_REAL].owner;
3193 ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
3194 if (p->owner->_state == AST_STATE_RINGING) {
3195 ast_setstate(p->owner, AST_STATE_UP);
3196 p->subs[SUB_REAL].needanswer = 1;
3198 p->callwaitingrepeat = 0;
3200 /* Start music on hold if appropriate */
3201 if (!p->subs[SUB_CALLWAIT].inthreeway && p->subs[SUB_CALLWAIT].owner->bridge)
3202 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
3203 if (p->subs[SUB_REAL].owner->bridge)
3204 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
3205 } else if (!p->subs[SUB_THREEWAY].owner) {
3207 if (p->threewaycalling && !check_for_conference(p)) {
3208 if (p->zaptrcallerid && p->owner && p->owner->callerid)
3209 strncpy(callerid, p->owner->callerid, sizeof(callerid) - 1);
3210 /* XXX This section needs much more error checking!!! XXX */
3211 /* Start a 3-way call if feasible */
3213 (ast->_state == AST_STATE_UP) ||
3214 (ast->_state == AST_STATE_RING)) {
3215 if (!alloc_sub(p, SUB_THREEWAY)) {
3216 /* Make new channel */
3217 chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
3218 if (p->zaptrcallerid) {
3219 if (!p->origcallerid) {
3220 p->origcallerid = malloc(strlen(p->callerid) + 1);
3221 strncpy(p->origcallerid, p->callerid, strlen(p->callerid) + 1);
3223 strncpy(p->callerid, callerid, sizeof(p->callerid) -1);
3225 /* Swap things around between the three-way and real call */
3226 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3227 /* Disable echo canceller for better dialing */
3229 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
3231 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
3233 if (chan && pthread_create(&threadid, &attr, ss_thread, chan)) {
3234 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
3235 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);