2 * Asterisk -- A telephony toolkit for Linux.
4 * Zaptel Pseudo TDM interface
6 * Copyright (C) 2003 Digium
8 * Mark Spencer <markster@digium.com>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
16 #include <asterisk/lock.h>
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <asterisk/file.h>
25 #include <asterisk/ulaw.h>
26 #include <asterisk/alaw.h>
27 #include <asterisk/callerid.h>
28 #include <asterisk/adsi.h>
29 #include <asterisk/cli.h>
30 #include <asterisk/cdr.h>
31 #include <asterisk/parking.h>
32 #include <asterisk/musiconhold.h>
33 #include <asterisk/say.h>
34 #include <asterisk/tdd.h>
35 #include <asterisk/app.h>
36 #include <asterisk/dsp.h>
37 #include <asterisk/astdb.h>
38 #include <asterisk/manager.h>
39 #include <asterisk/causes.h>
40 #include <asterisk/term.h>
41 #include <asterisk/utils.h>
42 #include <sys/signal.h>
47 #include <sys/ioctl.h>
49 #include <linux/zaptel.h>
52 #endif /* __linux__ */
59 #error "You need newer libpri"
66 #include "../asterisk.h"
69 #error "Your zaptel is too old. please cvs update"
73 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
74 * the user hangs up to reset the state machine so ring works properly.
75 * This is used to be able to support kewlstart by putting the zhone in
76 * groundstart mode since their forward disconnect supervision is entirely
77 * broken even though their documentation says it isn't and their support
78 * is entirely unwilling to provide any assistance with their channel banks
79 * even though their web site says they support their products for life.
82 /* #define ZHONE_HACK */
84 /* Typically, how many rings before we should send Caller*ID */
85 #define DEFAULT_CIDRINGS 1
87 #define CHANNEL_PSEUDO -12
89 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
91 static char *desc = "Zapata Telephony"
100 static char *tdesc = "Zapata Telephony Driver"
109 static char *type = "Zap";
110 static char *typecompat = "Tor"; /* Retain compatibility with chan_tor */
111 static char *config = "zapata.conf";
113 #define SIG_EM ZT_SIG_EM
114 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
115 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
116 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
117 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
118 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
119 #define SIG_FXSLS ZT_SIG_FXSLS
120 #define SIG_FXSGS ZT_SIG_FXSGS
121 #define SIG_FXSKS ZT_SIG_FXSKS
122 #define SIG_FXOLS ZT_SIG_FXOLS
123 #define SIG_FXOGS ZT_SIG_FXOGS
124 #define SIG_FXOKS ZT_SIG_FXOKS
125 #define SIG_PRI ZT_SIG_CLEAR
126 #define SIG_R2 ZT_SIG_CAS
127 #define SIG_SF ZT_SIG_SF
128 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
129 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
130 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
131 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
132 #define SIG_EM_E1 ZT_SIG_EM_E1
133 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
134 #define SIG_GR303FXSKS (0x200000 | ZT_SIG_FXSKS)
137 #define NUM_DCHANS 4 /* No more than 4 d-channels */
138 #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
139 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
141 #define CHAN_PSEUDO -2
143 #define DCHAN_PROVISIONED (1 << 0)
144 #define DCHAN_NOTINALARM (1 << 1)
145 #define DCHAN_UP (1 << 2)
147 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
149 static char context[AST_MAX_EXTENSION] = "default";
150 static char callerid[256] = "";
152 static char language[MAX_LANGUAGE] = "";
153 static char musicclass[MAX_LANGUAGE] = "";
154 static char progzone[10]= "";
156 static int usedistinctiveringdetection = 0;
158 static int use_callerid = 1;
159 static int zaptrcallerid = 0;
160 static int cur_signalling = -1;
162 static unsigned int cur_group = 0;
163 static unsigned int cur_callergroup = 0;
164 static unsigned int cur_pickupgroup = 0;
165 static int relaxdtmf = 0;
167 static int immediate = 0;
169 static int stripmsd = 0;
171 static int callwaiting = 0;
173 static int callwaitingcallerid = 0;
175 static int hidecallerid = 0;
177 static int restrictcid = 0;
179 static int use_callingpres = 0;
181 static int callreturn = 0;
183 static int threewaycalling = 0;
185 static int transfer = 0;
187 static int cancallforward = 0;
189 static float rxgain = 0.0;
191 static float txgain = 0.0;
193 static int tonezone = -1;
195 static int echocancel;
197 static int echotraining;
199 static int echocanbridged = 0;
201 static int busydetect = 0;
203 static int busycount = 3;
205 static int callprogress = 0;
207 static char accountcode[20] = "";
209 static char mailbox[AST_MAX_EXTENSION];
211 static int amaflags = 0;
215 static int numbufs = 4;
217 static int cur_prewink = -1;
218 static int cur_preflash = -1;
219 static int cur_wink = -1;
220 static int cur_flash = -1;
221 static int cur_start = -1;
222 static int cur_rxwink = -1;
223 static int cur_rxflash = -1;
224 static int cur_debounce = -1;
227 static int minunused = 2;
228 static int minidle = 0;
229 static char idleext[AST_MAX_EXTENSION];
230 static char idledial[AST_MAX_EXTENSION];
231 static int overlapdial = 0;
232 static struct ast_channel inuse = { "GR-303InUse" };
235 /* Wait up to 16 seconds for first digit (FXO logic) */
236 static int firstdigittimeout = 16000;
238 /* How long to wait for following digits (FXO logic) */
239 static int gendigittimeout = 8000;
241 /* How long to wait for an extra digit, if there is an ambiguous match */
242 static int matchdigittimeout = 3000;
244 static int usecnt =0;
245 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
247 /* Protect the interface list (of zt_pvt's) */
248 AST_MUTEX_DEFINE_STATIC(iflock);
250 static int ifcount = 0;
252 /* Protect the monitoring thread, so only one process can kill or start it, and not
253 when it's doing something critical. */
254 AST_MUTEX_DEFINE_STATIC(monlock);
256 /* This is the thread for the monitor which checks for input on the channels
257 which are not currently in use. */
258 static pthread_t monitor_thread = AST_PTHREADT_NULL;
260 static int restart_monitor(void);
262 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
264 static int zt_sendtext(struct ast_channel *c, char *text);
266 static inline int zt_get_event(int fd)
268 /* Avoid the silly zt_getevent which ignores a bunch of events */
270 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
274 static inline int zt_wait_event(int fd)
276 /* Avoid the silly zt_waitevent which ignores a bunch of events */
278 i = ZT_IOMUX_SIGEVENT;
279 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
280 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
284 /* Chunk size to read -- we use 20ms chunks to make things happy. */
285 #define READ_SIZE 160
287 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
288 #define MASK_INUSE (1 << 1) /* Channel currently in use */
290 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
291 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
292 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
293 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
294 #define RINGT ( (8000 * 8) / READ_SIZE)
300 static int r2prot = -1;
306 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8))
307 #define PRI_CHANNEL(p) ((p) & 0xff)
308 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
311 pthread_t master; /* Thread of master */
312 ast_mutex_t lock; /* Mutex */
313 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
314 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
315 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
316 int minunused; /* Min # of channels to keep empty */
317 int minidle; /* Min # of "idling" calls to keep active */
318 int nodetype; /* Node type */
319 int switchtype; /* Type of switch to emulate */
320 int nsf; /* Network-Specific Facilities */
321 int dialplan; /* Dialing plan */
322 int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
323 int trunkgroup; /* What our trunkgroup is */
324 int mastertrunkgroup; /* What trunk group is our master */
325 int prilogicalspan; /* Logical span number within trunk group */
326 int numchans; /* Num of channels we represent */
327 int overlapdial; /* In overlap dialing mode */
328 struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
329 int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
330 struct pri *pri; /* Currently active D-channel */
332 int fds[NUM_DCHANS]; /* FD's for d-channels */
338 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
339 struct zt_pvt *crvs; /* Member CRV structs */
340 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
344 static struct zt_pri pris[NUM_SPANS];
346 static int pritype = PRI_CPE;
349 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
351 #define DEFAULT_PRI_DEBUG 0
354 static inline void pri_rel(struct zt_pri *pri)
356 ast_mutex_unlock(&pri->lock);
359 static int switchtype = PRI_SWITCH_NI2;
360 static int nsf = PRI_NSF_NONE;
361 static int dialplan = PRI_NATIONAL_ISDN + 1;
364 /* Shut up the compiler */
368 #define SUB_REAL 0 /* Active call */
369 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
370 #define SUB_THREEWAY 2 /* Three-way call */
373 static struct zt_distRings drings;
375 struct distRingData {
378 struct ringContextData {
379 char contextData[AST_MAX_EXTENSION];
381 struct zt_distRings {
382 struct distRingData ringnum[3];
383 struct ringContextData ringContext[3];
386 static char *subnames[] = {
392 struct zt_subchannel {
394 struct ast_channel *owner;
396 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
397 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
408 #define CONF_USER_REAL (1 << 0)
409 #define CONF_USER_THIRDCALL (1 << 1)
413 static struct zt_pvt {
415 struct ast_channel *owner; /* Our current active owner (if applicable) */
416 /* Up to three channels can be associated with this call */
418 struct zt_subchannel sub_unused; /* Just a safety precaution */
419 struct zt_subchannel subs[3]; /* Sub-channels */
420 struct zt_confinfo saveconf; /* Saved conference info */
422 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
423 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
424 int inconference; /* If our real should be in the conference */
426 int sig; /* Signalling style */
427 int radio; /* radio type */
428 int firstradio; /* first radio flag */
431 int tonezone; /* tone zone for this chan, or -1 for default */
432 struct zt_pvt *next; /* Next channel in list */
433 struct zt_pvt *prev; /* Prev channel in list */
435 struct zt_distRings drings;
436 int usedistinctiveringdetection;
438 char context[AST_MAX_EXTENSION];
439 char defcontext[AST_MAX_EXTENSION];
440 char exten[AST_MAX_EXTENSION];
441 char language[MAX_LANGUAGE];
442 char musicclass[MAX_LANGUAGE];
443 char callerid[AST_MAX_EXTENSION];
444 char lastcallerid[AST_MAX_EXTENSION];
445 char *origcallerid; /* malloced original callerid */
446 char callwaitcid[AST_MAX_EXTENSION];
447 char rdnis[AST_MAX_EXTENSION];
448 char dnid[AST_MAX_EXTENSION];
451 int confno; /* Our conference */
452 int confusers; /* Who is using our conference */
453 int propconfno; /* Propagated conference number */
454 unsigned int callgroup;
455 unsigned int pickupgroup;
456 int immediate; /* Answer before getting digits? */
457 int channel; /* Channel Number or CRV */
458 int span; /* Span number */
460 time_t guardtime; /* Must wait this much time before using for new call */
462 int use_callerid; /* Whether or not to use caller id on this channel */
465 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
466 int restrictcid; /* Whether restrict the callerid -> only send ANI */
467 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
468 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
469 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
470 int cidcwexpire; /* When to expire our muting for CID/CW */
471 unsigned char *cidspill;
486 int callwaitingcallerid;
495 struct timeval flashtime; /* Last flash-hook time */
497 int cref; /* Call reference number */
498 ZT_DIAL_OPERATION dop;
502 char accountcode[20]; /* Account code */
503 int amaflags; /* AMA Flags */
504 char didtdd; /* flag to say its done it once */
505 struct tdd_state *tdd; /* TDD flag */
508 char call_forward[AST_MAX_EXTENSION];
509 char mailbox[AST_MAX_EXTENSION];
514 int confirmanswer; /* Wait for '#' to confirm answer */
515 int distinctivering; /* Which distinctivering to use */
516 int cidrings; /* Which ring to deliver CID on */
518 int faxhandled; /* Has a fax tone already been handled? */
520 char mate; /* flag to say its in MATE mode */
521 int pulsedial; /* whether a pulse dial phone is detected */
522 int dtmfrelax; /* whether to run in relaxed DTMF mode */
524 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
527 struct zt_pvt *bearer;
528 struct zt_pvt *realcall;
536 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
545 } *iflist = NULL, *ifend = NULL;
547 struct zt_pvt *round_robin[32];
550 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
553 /* Grab the lock first */
555 res = ast_mutex_trylock(&pri->lock);
557 ast_mutex_unlock(&pvt->lock);
558 /* Release the lock and try again */
560 ast_mutex_lock(&pvt->lock);
563 /* Then break the poll */
564 pthread_kill(pri->master, SIGURG);
569 #define NUM_CADENCE_MAX 25
570 static int num_cadence = 4;
571 static int user_has_defined_cadences = 0;
573 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
574 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
575 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
576 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
577 { { 1000, 500, 2500, 5000 } }, /* Long ring */
580 int receivedRingT; /* Used to find out what ringtone we are on */
582 /* cidrings says in which pause to transmit the cid information, where the first pause
583 * is 1, the second pause is 2 and so on.
586 static int cidrings[NUM_CADENCE_MAX] = {
587 2, /* Right after first long ring */
588 4, /* Right after long part */
589 3, /* After third chirp */
590 2, /* Second spell */
593 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
594 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
596 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
597 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
600 /* translate between PRI causes and asterisk's */
601 static int hangup_pri2cause(int cause)
604 case PRI_CAUSE_USER_BUSY:
605 return AST_CAUSE_BUSY;
606 case PRI_CAUSE_NORMAL_CLEARING:
607 return AST_CAUSE_NORMAL;
608 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
609 case PRI_CAUSE_REQUESTED_CHAN_UNAVAIL:
610 return AST_CAUSE_CONGESTION;
611 case PRI_CAUSE_UNALLOCATED:
612 case PRI_CAUSE_NUMBER_CHANGED:
613 return AST_CAUSE_UNALLOCATED;
614 case PRI_CAUSE_NO_USER_RESPONSE:
615 case PRI_CAUSE_NO_ANSWER:
616 return AST_CAUSE_NOANSWER;
618 return AST_CAUSE_FAILURE;
624 /* translate between ast cause and PRI */
625 static int hangup_cause2pri(int cause)
629 return PRI_CAUSE_USER_BUSY;
630 case AST_CAUSE_UNALLOCATED:
631 return PRI_CAUSE_UNALLOCATED;
632 case AST_CAUSE_CONGESTION:
633 return PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION;
634 case AST_CAUSE_NORMAL:
636 return PRI_CAUSE_NORMAL_CLEARING;
643 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
646 if (p->subs[0].owner == ast)
648 else if (p->subs[1].owner == ast)
650 else if (p->subs[2].owner == ast)
655 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
660 static void wakeup_sub(struct zt_pvt *p, int a)
662 struct ast_frame null = { AST_FRAME_NULL, };
664 if (p->subs[a].owner) {
665 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
666 ast_mutex_unlock(&p->lock);
668 ast_mutex_lock(&p->lock);
670 ast_queue_frame(p->subs[a].owner, &null);
671 ast_mutex_unlock(&p->subs[a].owner->lock);
679 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f)
683 if (ast_mutex_trylock(&p->owner->lock)) {
684 ast_mutex_unlock(&p->lock);
686 ast_mutex_lock(&p->lock);
688 ast_queue_frame(p->owner, f);
689 ast_mutex_unlock(&p->owner->lock);
697 static void swap_subs(struct zt_pvt *p, int a, int b)
701 struct ast_channel *towner;
703 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
705 tchan = p->subs[a].chan;
706 towner = p->subs[a].owner;
707 tinthreeway = p->subs[a].inthreeway;
709 p->subs[a].chan = p->subs[b].chan;
710 p->subs[a].owner = p->subs[b].owner;
711 p->subs[a].inthreeway = p->subs[b].inthreeway;
713 p->subs[b].chan = tchan;
714 p->subs[b].owner = towner;
715 p->subs[b].inthreeway = tinthreeway;
717 if (p->subs[a].owner)
718 p->subs[a].owner->fds[0] = p->subs[a].zfd;
719 if (p->subs[b].owner)
720 p->subs[b].owner->fds[0] = p->subs[b].zfd;
725 static int zt_open(char *fn)
733 for (x=0;x<strlen(fn);x++) {
734 if (!isdigit(fn[x])) {
742 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
745 fn = "/dev/zap/channel";
747 fd = open(fn, O_RDWR | O_NONBLOCK);
749 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
753 if (ioctl(fd, ZT_SPECIFY, &chan)) {
757 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
762 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
766 static void zt_close(int fd)
771 int zt_setlinear(int zfd, int linear)
774 res = ioctl(zfd, ZT_SETLINEAR, &linear);
781 int zt_setlaw(int zfd, int law)
784 res = ioctl(zfd, ZT_SETLAW, &law);
790 static int alloc_sub(struct zt_pvt *p, int x)
794 if (p->subs[x].zfd < 0) {
795 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
796 if (p->subs[x].zfd > -1) {
797 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
799 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
800 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
801 bi.numbufs = numbufs;
802 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
804 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
807 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
808 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
809 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
810 zt_close(p->subs[x].zfd);
815 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
818 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
821 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
825 static int unalloc_sub(struct zt_pvt *p, int x)
828 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
831 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
832 if (p->subs[x].zfd > -1) {
833 zt_close(p->subs[x].zfd);
836 p->subs[x].linear = 0;
838 p->subs[x].owner = NULL;
839 p->subs[x].inthreeway = 0;
840 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
844 static int zt_digit(struct ast_channel *ast, char digit)
846 ZT_DIAL_OPERATION zo;
851 ast_mutex_lock(&p->lock);
852 index = zt_get_index(ast, p, 0);
853 if (index == SUB_REAL) {
855 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
857 if (!pri_grab(p, p->pri)) {
858 pri_information(p->pri->pri,p->call,digit);
861 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
862 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
863 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
864 res = strlen(p->dialdest);
865 p->dialdest[res++] = digit;
866 p->dialdest[res] = '\0';
872 zo.op = ZT_DIAL_OP_APPEND;
874 zo.dialstr[1] = digit;
876 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
877 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
882 ast_mutex_unlock(&p->lock);
886 static char *events[] = {
899 "Hook Transition Complete",
908 { ZT_ALARM_RED, "Red Alarm" },
909 { ZT_ALARM_YELLOW, "Yellow Alarm" },
910 { ZT_ALARM_BLUE, "Blue Alarm" },
911 { ZT_ALARM_RECOVER, "Recovering" },
912 { ZT_ALARM_LOOPBACK, "Loopback" },
913 { ZT_ALARM_NOTOPEN, "Not Open" },
914 { ZT_ALARM_NONE, "None" },
917 static char *alarm2str(int alarm)
920 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
921 if (alarms[x].alarm & alarm)
922 return alarms[x].name;
924 return alarm ? "Unknown Alarm" : "No Alarm";
927 static char *event2str(int event)
929 static char buf[256];
930 if ((event < 15) && (event > -1))
931 return events[event];
932 sprintf(buf, "Event %d", event); /* safe */
937 static int str2r2prot(char *swtype)
939 if (!strcasecmp(swtype, "ar"))
940 return MFCR2_PROT_ARGENTINA;
942 if (!strcasecmp(swtype, "cn"))
943 return MFCR2_PROT_CHINA;
945 if (!strcasecmp(swtype, "kr"))
946 return MFCR2_PROT_KOREA;
952 static char *sig2str(int sig)
954 static char buf[256];
957 return "E & M Immediate";
963 return "Feature Group D (DTMF)";
965 return "Feature Group D (MF)";
967 return "Feature Group B (MF)";
971 return "FXS Loopstart";
973 return "FXS Groundstart";
975 return "FXS Kewlstart";
977 return "FXO Loopstart";
979 return "FXO Groundstart";
981 return "FXO Kewlstart";
983 return "PRI Signalling";
985 return "R2 Signalling";
987 return "SF (Tone) Signalling Immediate";
989 return "SF (Tone) Signalling Wink";
991 return "SF (Tone) Signalling with Feature Group D (DTMF)";
993 return "SF (Tone) Signalling with Feature Group D (MF)";
995 return "SF (Tone) Signalling with Feature Group B (MF)";
997 return "GR-303 Signalling with FXOKS";
999 return "GR-303 Signalling with FXSKS";
1001 return "Pseudo Signalling";
1003 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1008 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1010 /* If the conference already exists, and we're already in it
1011 don't bother doing anything */
1014 memset(&zi, 0, sizeof(zi));
1017 if (slavechannel > 0) {
1018 /* If we have only one slave, do a digital mon */
1019 zi.confmode = ZT_CONF_DIGITALMON;
1020 zi.confno = slavechannel;
1023 /* Real-side and pseudo-side both participate in conference */
1024 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1025 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1027 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1028 zi.confno = p->confno;
1030 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1034 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1035 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1038 if (slavechannel < 1) {
1039 p->confno = zi.confno;
1041 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1042 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1046 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1048 /* If they're listening to our channel, they're ours */
1049 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1051 /* If they're a talker on our (allocated) conference, they're ours */
1052 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1057 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1060 if (/* Can't delete if there's no zfd */
1062 /* Don't delete from the conference if it's not our conference */
1064 /* Don't delete if we don't think it's conferenced at all (implied) */
1066 memset(&zi, 0, sizeof(zi));
1070 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1071 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1074 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1075 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1079 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1083 struct zt_pvt *slave = NULL;
1084 /* Start out optimistic */
1086 /* Update conference state in a stateless fashion */
1088 /* Any three-way calling makes slave native mode *definitely* out
1090 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1093 /* If we don't have any 3-way calls, check to see if we have
1094 precisely one slave */
1095 if (useslavenative) {
1096 for (x=0;x<MAX_SLAVES;x++) {
1099 /* Whoops already have a slave! No
1100 slave native and stop right away */
1105 /* We have one slave so far */
1106 slave = p->slaves[x];
1111 /* If no slave, slave native definitely out */
1114 else if (slave->law != p->law) {
1120 return useslavenative;
1123 static int update_conf(struct zt_pvt *p)
1128 struct zt_pvt *slave = NULL;
1130 useslavenative = isslavenative(p, &slave);
1131 /* Start with the obvious, general stuff */
1133 /* Look for three way calls */
1134 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1135 conf_add(p, &p->subs[x], x, 0);
1138 conf_del(p, &p->subs[x], x);
1141 /* If we have a slave, add him to our conference now. or DAX
1142 if this is slave native */
1143 for (x=0;x<MAX_SLAVES;x++) {
1146 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, p->bearer ? p->bearer->channel : p->channel);
1148 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1153 /* If we're supposed to be in there, do so now */
1154 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1156 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, slave->bearer ? slave->bearer->channel : slave->channel);
1158 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1162 /* If we have a master, add ourselves to his conference */
1164 if (isslavenative(p->master, NULL)) {
1165 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, p->master->bearer ? p->master->bearer->channel : p->master->channel);
1167 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1171 /* Nobody is left (or should be left) in our conference.
1175 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1179 static void zt_enable_ec(struct zt_pvt *p)
1184 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1187 if (p && p->echocancel) {
1188 if (p->sig == SIG_PRI) {
1190 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1192 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1195 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1197 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1200 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1203 ast_log(LOG_DEBUG, "No echocancellation requested\n");
1206 static void zt_train_ec(struct zt_pvt *p)
1210 if (p && p->echocancel && p->echotraining) {
1211 x = p->echotraining;
1212 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1214 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1216 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1219 ast_log(LOG_DEBUG, "No echo training requested\n");
1222 static void zt_disable_ec(struct zt_pvt *p)
1226 if (p->echocancel) {
1228 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1230 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1232 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1237 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1244 if ((rxgain != 0.0) || (txgain != 0.0)) {
1245 /* caluculate linear value of tx gain */
1246 ltxgain = pow(10.0,txgain / 20.0);
1247 /* caluculate linear value of rx gain */
1248 lrxgain = pow(10.0,rxgain / 20.0);
1249 if (law == ZT_LAW_ALAW) {
1250 for (j=0;j<256;j++) {
1251 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1252 if (k > 32767) k = 32767;
1253 if (k < -32767) k = -32767;
1254 g.rxgain[j] = AST_LIN2A(k);
1255 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1256 if (k > 32767) k = 32767;
1257 if (k < -32767) k = -32767;
1258 g.txgain[j] = AST_LIN2A(k);
1261 for (j=0;j<256;j++) {
1262 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1263 if (k > 32767) k = 32767;
1264 if (k < -32767) k = -32767;
1265 g.rxgain[j] = AST_LIN2MU(k);
1266 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1267 if (k > 32767) k = 32767;
1268 if (k < -32767) k = -32767;
1269 g.txgain[j] = AST_LIN2MU(k);
1273 for (j=0;j<256;j++) {
1280 return(ioctl(fd,ZT_SETGAINS,&g));
1283 static inline int zt_set_hook(int fd, int hs)
1287 res = ioctl(fd, ZT_HOOK, &x);
1290 if (errno == EINPROGRESS) return 0;
1291 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1296 static inline int zt_confmute(struct zt_pvt *p, int muted)
1300 if (p->sig == SIG_PRI) {
1302 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1304 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1306 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1308 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1312 static int save_conference(struct zt_pvt *p)
1314 struct zt_confinfo c;
1316 if (p->saveconf.confmode) {
1317 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1320 p->saveconf.chan = 0;
1321 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1323 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1324 p->saveconf.confmode = 0;
1329 c.confmode = ZT_CONF_NORMAL;
1330 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1332 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1336 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1340 static int restore_conference(struct zt_pvt *p)
1343 if (p->saveconf.confmode) {
1344 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1345 p->saveconf.confmode = 0;
1347 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1352 ast_log(LOG_DEBUG, "Restored conferencing\n");
1356 static int send_callerid(struct zt_pvt *p);
1358 int send_cwcidspill(struct zt_pvt *p)
1362 p->cidspill = malloc(MAX_CALLERID_SIZE);
1364 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1365 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwaitcid, AST_LAW(p));
1366 /* Make sure we account for the end */
1367 p->cidlen += READ_SIZE * 4;
1370 if (option_verbose > 2)
1371 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s'\n", p->callwaitcid);
1376 static int has_voicemail(struct zt_pvt *p)
1379 return ast_app_has_voicemail(p->mailbox);
1382 static int send_callerid(struct zt_pvt *p)
1384 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1386 /* Take out of linear mode if necessary */
1387 if (p->subs[SUB_REAL].linear) {
1388 p->subs[SUB_REAL].linear = 0;
1389 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1391 while(p->cidpos < p->cidlen) {
1392 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1394 if (errno == EAGAIN)
1397 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1407 if (p->callwaitcas) {
1408 /* Wait for CID/CW to expire */
1409 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1411 restore_conference(p);
1415 static int zt_callwait(struct ast_channel *ast)
1417 struct zt_pvt *p = ast->pvt->pvt;
1418 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1420 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1423 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1427 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1428 if (!p->callwaitrings && p->callwaitingcallerid) {
1429 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1431 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1433 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1435 p->cidlen = 2400 + READ_SIZE * 4;
1440 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1446 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1448 struct zt_pvt *p = ast->pvt->pvt;
1455 char dest[256]; /* must be same length as p->dialdest */
1456 ast_mutex_lock(&p->lock);
1457 strncpy(dest, rdest, sizeof(dest) - 1);
1458 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1459 if ((ast->_state == AST_STATE_BUSY)) {
1460 p->subs[SUB_REAL].needbusy = 1;
1461 ast_mutex_unlock(&p->lock);
1464 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1465 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1466 ast_mutex_unlock(&p->lock);
1470 if (p->radio) /* if a radio channel, up immediately */
1472 /* Special pseudo -- automatically up */
1473 ast_setstate(ast, AST_STATE_UP);
1474 ast_mutex_unlock(&p->lock);
1477 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1478 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1480 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1483 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1489 if (p->owner == ast) {
1490 /* Normal ring, on hook */
1492 /* Don't send audio while on hook, until the call is answered */
1494 if (p->use_callerid) {
1495 /* Generate the Caller-ID spill if desired */
1497 ast_log(LOG_WARNING, "cidspill already exists??\n");
1500 p->cidspill = malloc(MAX_CALLERID_SIZE);
1503 p->cidlen = ast_callerid_generate(p->cidspill, ast->callerid, AST_LAW(p));
1507 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1509 /* Choose proper cadence */
1510 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1511 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1512 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1513 p->cidrings = cidrings[p->distinctivering - 1];
1515 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1516 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1517 p->cidrings = DEFAULT_CIDRINGS;
1521 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1522 c = strchr(dest, '/');
1525 if (c && (strlen(c) < p->stripmsd)) {
1526 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1530 p->dop.op = ZT_DIAL_OP_REPLACE;
1531 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1532 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1534 p->dop.dialstr[0] = '\0';
1537 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1538 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1539 ast_mutex_unlock(&p->lock);
1544 /* Call waiting call */
1545 p->callwaitrings = 0;
1547 strncpy(p->callwaitcid, ast->callerid, sizeof(p->callwaitcid)-1);
1549 p->callwaitcid[0] = '\0';
1550 /* Call waiting tone instead */
1551 if (zt_callwait(ast)) {
1552 ast_mutex_unlock(&p->lock);
1555 /* Make ring-back */
1556 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1557 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1561 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1564 ast_callerid_parse(callerid, &n, &l);
1566 ast_shrink_phone_number(l);
1567 if (!ast_isphonenumber(l))
1571 strncpy(p->lastcallerid, l, sizeof(p->lastcallerid) - 1);
1573 p->lastcallerid[0] = '\0';
1574 ast_setstate(ast, AST_STATE_RINGING);
1575 index = zt_get_index(ast, p, 0);
1577 p->subs[index].needringing = 1;
1593 case SIG_SF_FEATDMF:
1595 c = strchr(dest, '/');
1600 if (strlen(c) < p->stripmsd) {
1601 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1602 ast_mutex_unlock(&p->lock);
1606 /* Start the trunk */
1607 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1609 if (errno != EINPROGRESS) {
1610 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1611 ast_mutex_unlock(&p->lock);
1615 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1616 p->dop.op = ZT_DIAL_OP_REPLACE;
1617 if (p->sig == SIG_FEATD) {
1618 if (ast->callerid) {
1619 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1620 ast_callerid_parse(callerid, &n, &l);
1622 ast_shrink_phone_number(l);
1623 if (!ast_isphonenumber(l))
1629 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1631 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1633 if (p->sig == SIG_FEATDMF) {
1634 if (ast->callerid) {
1635 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1636 ast_callerid_parse(callerid, &n, &l);
1638 ast_shrink_phone_number(l);
1639 if (!ast_isphonenumber(l))
1645 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1647 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1649 if (p->sig == SIG_E911) {
1650 strncpy(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr) - 1);
1652 if (p->sig == SIG_FEATB) {
1653 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1655 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1656 if (strlen(p->dop.dialstr) > 4) {
1657 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1658 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1659 p->echorest[sizeof(p->echorest) - 1] = '\0';
1661 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1665 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1667 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1668 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1669 ast_mutex_unlock(&p->lock);
1673 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1675 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1676 ast_setstate(ast, AST_STATE_DIALING);
1679 /* Special pseudo -- automatically up*/
1680 ast_setstate(ast, AST_STATE_UP);
1683 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1684 p->dialdest[0] = '\0';
1687 ast_log(LOG_DEBUG, "not yet implemented\n");
1688 ast_mutex_unlock(&p->lock);
1694 c = strchr(dest, '/');
1699 if (ast->callerid && !p->hidecallerid) {
1700 strncpy(callerid, ast->callerid, sizeof(callerid)-1);
1701 ast_callerid_parse(callerid, &n, &l);
1703 ast_shrink_phone_number(l);
1704 if (!ast_isphonenumber(l))
1709 if (strlen(c) < p->stripmsd) {
1710 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1711 ast_mutex_unlock(&p->lock);
1714 p->dop.op = ZT_DIAL_OP_REPLACE;
1715 s = strchr(c + p->stripmsd, 'w');
1718 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1720 p->dop.dialstr[0] = '\0';
1723 p->dop.dialstr[0] = '\0';
1725 if (pri_grab(p, p->pri)) {
1726 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1727 ast_mutex_unlock(&p->lock);
1730 if (!(p->call = pri_new_call(p->pri->pri))) {
1731 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1733 ast_mutex_unlock(&p->lock);
1736 if (!(sr = pri_sr_new())) {
1737 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1739 ast_mutex_unlock(&p->lock);
1742 ast_log(LOG_DEBUG, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
1743 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1744 p->bearer->call = p->call;
1746 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1747 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1748 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1749 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1751 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1752 pri_sr_set_called(sr, c + p->stripmsd, p->pri->dialplan - 1, s ? 1 : 0);
1753 pri_sr_set_caller(sr, l, n, p->pri->dialplan - 1,
1754 l ? (ast->restrictcid ? PRES_PROHIB_USER_NUMBER_PASSED_SCREEN :
1755 (p->use_callingpres ? ast->callingpres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN)) :
1756 PRES_NUMBER_NOT_AVAILABLE);
1757 if (pri_setup(p->pri->pri, p->call, sr)) {
1758 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1760 ast_mutex_unlock(&p->lock);
1765 ast_setstate(ast, AST_STATE_DIALING);
1769 ast_mutex_unlock(&p->lock);
1773 static void destroy_zt_pvt(struct zt_pvt **pvt)
1775 struct zt_pvt *p = *pvt;
1776 ast_mutex_destroy(&p->lock);
1781 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1791 for (i = 0; i < 3; i++) {
1792 if (cur->subs[i].owner) {
1798 prev->next = cur->next;
1800 prev->next->prev = prev;
1806 iflist->prev = NULL;
1810 if (cur->subs[SUB_REAL].zfd > -1) {
1811 zt_close(cur->subs[SUB_REAL].zfd);
1813 destroy_zt_pvt(&cur);
1817 prev->next = cur->next;
1819 prev->next->prev = prev;
1825 iflist->prev = NULL;
1829 if (cur->subs[SUB_REAL].zfd > -1) {
1830 zt_close(cur->subs[SUB_REAL].zfd);
1832 destroy_zt_pvt(&cur);
1838 int pri_is_up(struct zt_pri *pri)
1841 for (x=0;x<NUM_DCHANS;x++) {
1842 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1848 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
1850 bearer->owner = &inuse;
1851 bearer->master = crv;
1852 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
1853 crv->bearer = bearer;
1854 crv->call = bearer->call;
1859 static char *pri_order(int level)
1869 return "Quaternary";
1875 int pri_find_dchan(struct zt_pri *pri)
1882 for(x=0;x<NUM_DCHANS;x++) {
1883 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
1885 if (pri->dchans[x] == old) {
1891 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
1892 pri->dchannels[newslot]);
1894 if (old && (oldslot != newslot))
1895 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
1896 pri->dchannels[oldslot], pri->dchannels[newslot]);
1897 pri->pri = pri->dchans[newslot];
1902 static int zt_hangup(struct ast_channel *ast)
1906 static int restore_gains(struct zt_pvt *p);
1907 struct zt_pvt *p = ast->pvt->pvt;
1908 struct zt_pvt *tmp = NULL;
1909 struct zt_pvt *prev = NULL;
1913 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1914 if (!ast->pvt->pvt) {
1915 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1919 ast_mutex_lock(&p->lock);
1921 index = zt_get_index(ast, p, 1);
1923 if (p->sig == SIG_PRI) {
1925 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1931 if (p->origcallerid) {
1932 strncpy(p->callerid, p->origcallerid, sizeof(p->callerid) - 1);
1933 free(p->origcallerid);
1934 p->origcallerid = NULL;
1937 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1941 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1942 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
1946 /* Real channel, do some fixup */
1947 p->subs[index].owner = NULL;
1948 p->subs[index].needanswer = 0;
1949 p->subs[index].needringing = 0;
1950 p->subs[index].needbusy = 0;
1951 p->subs[index].needcongestion = 0;
1952 p->subs[index].linear = 0;
1953 p->subs[index].needcallerid = 0;
1954 zt_setlinear(p->subs[index].zfd, 0);
1955 if (index == SUB_REAL) {
1956 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
1957 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
1958 if (p->subs[SUB_CALLWAIT].inthreeway) {
1959 /* We had flipped over to answer a callwait and now it's gone */
1960 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
1961 /* Move to the call-wait, but un-own us until they flip back. */
1962 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1963 unalloc_sub(p, SUB_CALLWAIT);
1966 /* The three way hung up, but we still have a call wait */
1967 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
1968 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1969 unalloc_sub(p, SUB_THREEWAY);
1970 if (p->subs[SUB_REAL].inthreeway) {
1971 /* This was part of a three way call. Immediately make way for
1973 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1974 p->owner = p->subs[SUB_REAL].owner;
1976 /* This call hasn't been completed yet... Set owner to NULL */
1977 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
1980 p->subs[SUB_REAL].inthreeway = 0;
1982 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
1983 /* Move to the call-wait and switch back to them. */
1984 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
1985 unalloc_sub(p, SUB_CALLWAIT);
1986 p->owner = p->subs[SUB_REAL].owner;
1987 if (p->owner->_state != AST_STATE_UP)
1988 p->subs[SUB_REAL].needanswer = 1;
1989 if (p->subs[SUB_REAL].owner->bridge)
1990 ast_moh_stop(p->subs[SUB_REAL].owner->bridge);
1991 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
1992 swap_subs(p, SUB_THREEWAY, SUB_REAL);
1993 unalloc_sub(p, SUB_THREEWAY);
1994 if (p->subs[SUB_REAL].inthreeway) {
1995 /* This was part of a three way call. Immediately make way for
1997 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
1998 p->owner = p->subs[SUB_REAL].owner;
2000 /* This call hasn't been completed yet... Set owner to NULL */
2001 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2004 p->subs[SUB_REAL].inthreeway = 0;
2006 } else if (index == SUB_CALLWAIT) {
2007 /* Ditch the holding callwait call, and immediately make it availabe */
2008 if (p->subs[SUB_CALLWAIT].inthreeway) {
2009 /* This is actually part of a three way, placed on hold. Place the third part
2010 on music on hold now */
2011 if (p->subs[SUB_THREEWAY].owner && p->subs[SUB_THREEWAY].owner->bridge)
2012 ast_moh_start(p->subs[SUB_THREEWAY].owner->bridge, NULL);
2013 p->subs[SUB_THREEWAY].inthreeway = 0;
2014 /* Make it the call wait now */
2015 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2016 unalloc_sub(p, SUB_THREEWAY);
2018 unalloc_sub(p, SUB_CALLWAIT);
2019 } else if (index == SUB_THREEWAY) {
2020 if (p->subs[SUB_CALLWAIT].inthreeway) {
2021 /* The other party of the three way call is currently in a call-wait state.
2022 Start music on hold for them, and take the main guy out of the third call */
2023 if (p->subs[SUB_CALLWAIT].owner && p->subs[SUB_CALLWAIT].owner->bridge)
2024 ast_moh_start(p->subs[SUB_CALLWAIT].owner->bridge, NULL);
2025 p->subs[SUB_CALLWAIT].inthreeway = 0;
2027 p->subs[SUB_REAL].inthreeway = 0;
2028 /* If this was part of a three way call index, let us make
2029 another three way call */
2030 unalloc_sub(p, SUB_THREEWAY);
2032 /* This wasn't any sort of call, but how are we an index? */
2033 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2038 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2041 p->distinctivering = 0;
2042 p->confirmanswer = 0;
2048 p->onhooktime = time(NULL);
2054 ast_dsp_free(p->dsp);
2058 law = ZT_LAW_DEFAULT;
2059 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2061 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2062 /* Perform low level hangup if no owner left */
2065 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2066 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2067 if (!pri_grab(p, p->pri)) {
2068 if (p->alreadyhungup) {
2069 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2070 pri_hangup(p->pri->pri, p->call, -1);
2073 p->bearer->call = NULL;
2075 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2076 int icause = ast->hangupcause ? hangup_cause2pri(ast->hangupcause) : -1;
2077 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2078 p->alreadyhungup = 1;
2080 p->bearer->alreadyhungup = 1;
2083 icause = atoi(cause);
2085 pri_hangup(p->pri->pri, p->call, icause);
2088 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2091 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2096 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2103 if (p->sig == SIG_R2) {
2105 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2113 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2114 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2116 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2122 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2125 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2127 /* If they're off hook, try playing congestion */
2128 if (par.rxisoffhook)
2129 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2131 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2137 /* Make sure we're not made available for at least two seconds */
2138 time(&p->guardtime);
2142 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2149 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2150 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2154 p->callwaiting = p->permcallwaiting;
2155 p->hidecallerid = p->permhidecallerid;
2159 /* Restore data mode */
2160 if (p->sig == SIG_PRI) {
2162 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2166 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2167 /* Free up the bearer channel as well, and
2168 don't use its file descriptor anymore */
2169 p->bearer->owner = NULL;
2171 p->subs[SUB_REAL].zfd = -1;
2179 p->callwaitingrepeat = 0;
2181 ast->pvt->pvt = NULL;
2182 ast_mutex_unlock(&p->lock);
2183 ast_mutex_lock(&usecnt_lock);
2186 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2187 ast_mutex_unlock(&usecnt_lock);
2188 ast_update_use_count();
2189 if (option_verbose > 2)
2190 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2192 ast_mutex_lock(&iflock);
2198 destroy_channel(prev, tmp, 0);
2206 ast_mutex_unlock(&iflock);
2210 static int zt_answer(struct ast_channel *ast)
2212 struct zt_pvt *p = ast->pvt->pvt;
2215 int oldstate = ast->_state;
2216 ast_setstate(ast, AST_STATE_UP);
2217 ast_mutex_lock(&p->lock);
2218 index = zt_get_index(ast, p, 0);
2221 /* nothing to do if a radio channel */
2223 ast_mutex_unlock(&p->lock);
2242 case SIG_SF_FEATDMF:
2247 /* Pick up the line */
2248 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2249 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2250 tone_zone_play_tone(p->subs[index].zfd, -1);
2252 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2253 if (oldstate == AST_STATE_RINGING) {
2254 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2255 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2256 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2257 p->owner = p->subs[SUB_REAL].owner;
2260 if (p->sig & __ZT_SIG_FXS) {
2267 /* Send a pri acknowledge */
2268 if (!pri_grab(p, p->pri)) {
2270 res = pri_answer(p->pri->pri, p->call, 0, 1);
2273 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2280 res = mfcr2_AnswerCall(p->r2, NULL);
2282 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2286 ast_mutex_unlock(&p->lock);
2289 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2292 ast_mutex_unlock(&p->lock);
2296 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2301 struct zt_pvt *p = chan->pvt->pvt;
2304 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2305 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2311 if ((!cp) || (datalen < 1))
2317 case AST_OPTION_TONE_VERIFY:
2322 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2323 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2326 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2327 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2330 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2331 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2335 case AST_OPTION_TDD: /* turn on or off TDD */
2336 if (!*cp) { /* turn it off */
2337 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2338 if (p->tdd) tdd_free(p->tdd);
2344 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2345 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2348 /* otherwise, turn it on */
2349 if (!p->didtdd) { /* if havent done it yet */
2350 unsigned char mybuf[41000],*buf;
2351 int size,res,fd,len;
2353 struct pollfd fds[1];
2355 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2356 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2358 index = zt_get_index(chan, p, 0);
2360 ast_log(LOG_WARNING, "No index in TDD?\n");
2363 fd = p->subs[index].zfd;
2365 if (ast_check_hangup(chan)) return -1;
2367 if (size > READ_SIZE)
2370 fds[0].events = POLLPRI | POLLOUT;
2371 res = poll(fds, 1, -1);
2373 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2376 /* if got exception */
2377 if (fds[0].revents & POLLPRI) return -1;
2378 if (!(fds[0].revents & POLLOUT)) {
2379 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2382 res = write(fd, buf, size);
2384 if (res == -1) return -1;
2385 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2391 p->didtdd = 1; /* set to have done it now */
2393 if (*cp == 2) { /* Mate mode */
2394 if (p->tdd) tdd_free(p->tdd);
2399 if (!p->tdd) { /* if we dont have one yet */
2400 p->tdd = tdd_new(); /* allocate one */
2403 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2406 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2411 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2414 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2416 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2419 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2425 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2428 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2429 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2436 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2438 /* Unlink a specific slave or all slaves/masters from a given master */
2444 ast_mutex_lock(&master->lock);
2446 while(ast_mutex_trylock(&slave->lock)) {
2447 ast_mutex_unlock(&master->lock);
2449 ast_mutex_lock(&master->lock);
2454 for (x=0;x<MAX_SLAVES;x++) {
2455 if (master->slaves[x]) {
2456 if (!slave || (master->slaves[x] == slave)) {
2457 /* Take slave out of the conference */
2458 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2459 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2460 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2461 master->slaves[x]->master = NULL;
2462 master->slaves[x] = NULL;
2467 master->inconference = 0;
2470 if (master->master) {
2471 /* Take master out of the conference */
2472 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2473 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2475 for (x=0;x<MAX_SLAVES;x++) {
2476 if (master->master->slaves[x] == master)
2477 master->master->slaves[x] = NULL;
2478 else if (master->master->slaves[x])
2482 master->master->inconference = 0;
2484 master->master = NULL;
2486 update_conf(master);
2489 ast_mutex_unlock(&slave->lock);
2490 ast_mutex_unlock(&master->lock);
2494 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2496 if (!slave || !master) {
2497 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2500 for (x=0;x<MAX_SLAVES;x++) {
2501 if (!master->slaves[x]) {
2502 master->slaves[x] = slave;
2506 if (x >= MAX_SLAVES) {
2507 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2508 master->slaves[MAX_SLAVES - 1] = slave;
2511 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2512 slave->master = master;
2514 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2517 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2519 struct ast_channel *who = NULL, *cs[3];
2520 struct zt_pvt *p0, *p1, *op0, *op1;
2521 struct zt_pvt *master=NULL, *slave=NULL;
2522 struct ast_frame *f;
2527 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2528 int os1 = -1, os2 = -1;
2529 struct ast_channel *oc1, *oc2;
2531 /* if need DTMF, cant native bridge */
2532 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2535 ast_mutex_lock(&c0->lock);
2536 ast_mutex_lock(&c1->lock);
2540 /* cant do pseudo-channels here */
2541 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2542 ast_mutex_unlock(&c0->lock);
2543 ast_mutex_unlock(&c1->lock);
2547 op0 = p0 = c0->pvt->pvt;
2548 op1 = p1 = c1->pvt->pvt;
2551 oi1 = zt_get_index(c0, p0, 0);
2552 oi2 = zt_get_index(c1, p1, 0);
2555 if ((oi1 < 0) || (oi2 < 0)) {
2556 ast_mutex_unlock(&c0->lock);
2557 ast_mutex_unlock(&c1->lock);
2563 ast_mutex_lock(&p0->lock);
2564 if (ast_mutex_trylock(&p1->lock)) {
2565 /* Don't block, due to potential for deadlock */
2566 ast_mutex_unlock(&p0->lock);
2567 ast_mutex_unlock(&c0->lock);
2568 ast_mutex_unlock(&c1->lock);
2569 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2572 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2573 if (!p0->owner || !p1->owner) {
2574 /* Currently unowned -- Do nothing. */
2577 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2578 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2582 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2587 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2588 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,
2589 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2592 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2593 if (p1->subs[SUB_THREEWAY].inthreeway) {
2599 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2600 if (p0->subs[SUB_THREEWAY].inthreeway) {
2606 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2607 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2608 don't put us in anything */
2609 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2615 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2616 /* Same as previous */
2617 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2624 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2625 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2626 if (master && slave) {
2627 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2628 in an active threeway call with a channel that is ringing, we should
2629 indicate ringing. */
2630 if ((oi2 == SUB_THREEWAY) &&
2631 p1->subs[SUB_THREEWAY].inthreeway &&
2632 p1->subs[SUB_REAL].owner &&
2633 p1->subs[SUB_REAL].inthreeway &&
2634 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2635 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2636 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2637 os2 = p1->subs[SUB_REAL].owner->_state;
2639 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2640 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2642 if ((oi1 == SUB_THREEWAY) &&
2643 p0->subs[SUB_THREEWAY].inthreeway &&
2644 p0->subs[SUB_REAL].owner &&
2645 p0->subs[SUB_REAL].inthreeway &&
2646 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2647 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2648 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2649 os1 = p0->subs[SUB_REAL].owner->_state;
2651 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2652 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2654 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2655 if (!p0->echocanbridged || !p1->echocanbridged) {
2656 /* Disable echo cancellation if appropriate */
2661 zt_link(slave, master);
2662 master->inconference = inconf;
2663 } else if (!nothingok)
2664 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2668 t1 = p0->subs[SUB_REAL].inthreeway;
2669 t2 = p1->subs[SUB_REAL].inthreeway;
2671 ast_mutex_unlock(&p0->lock);
2672 ast_mutex_unlock(&p1->lock);
2674 ast_mutex_unlock(&c0->lock);
2675 ast_mutex_unlock(&c1->lock);
2677 /* Native bridge failed */
2678 if ((!master || !slave) && !nothingok) {
2690 /* Here's our main loop... Start by locking things, looking for private parts,
2691 and then balking if anything is wrong */
2692 ast_mutex_lock(&c0->lock);
2693 ast_mutex_lock(&c1->lock);
2697 i1 = zt_get_index(c0, p0, 1);
2699 i2 = zt_get_index(c1, p1, 1);
2700 ast_mutex_unlock(&c0->lock);
2701 ast_mutex_unlock(&c1->lock);
2702 if ((op0 != p0) || (op1 != p1) ||
2703 (ofd1 != c0->fds[0]) ||
2704 (ofd2 != c1->fds[0]) ||
2705 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2706 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2707 (oc1 != p0->owner) ||
2708 (oc2 != p1->owner) ||
2709 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2710 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2713 if (slave && master)
2714 zt_unlink(slave, master, 1);
2715 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2716 op0->channel, oi1, op1->channel, oi2);
2724 who = ast_waitfor_n(cs, 2, &to);
2726 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2729 if (who->pvt->pvt == op0)
2730 op0->ignoredtmf = 1;
2731 else if (who->pvt->pvt == op1)
2732 op1->ignoredtmf = 1;
2734 if (who->pvt->pvt == op0)
2735 op0->ignoredtmf = 0;
2736 else if (who->pvt->pvt == op1)
2737 op1->ignoredtmf = 0;
2741 if (slave && master)
2742 zt_unlink(slave, master, 1);
2749 if (f->frametype == AST_FRAME_DTMF) {
2750 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2751 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2754 if (slave && master)
2755 zt_unlink(slave, master, 1);
2757 } else if ((who == c0) && p0->pulsedial) {
2759 } else if ((who == c1) && p1->pulsedial) {
2765 /* Swap who gets priority */
2772 static int zt_indicate(struct ast_channel *chan, int condition);
2774 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2776 struct zt_pvt *p = newchan->pvt->pvt;
2778 ast_mutex_lock(&p->lock);
2779 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2780 if (p->owner == oldchan) {
2784 if (p->subs[x].owner == oldchan) {
2786 zt_unlink(NULL, p, 0);
2787 p->subs[x].owner = newchan;
2789 if (newchan->_state == AST_STATE_RINGING)
2790 zt_indicate(newchan, AST_CONTROL_RINGING);
2792 ast_mutex_unlock(&p->lock);
2796 static int zt_ring_phone(struct zt_pvt *p)
2800 /* Make sure our transmit state is on hook */
2803 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2806 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2808 printf("Res: %d, error: %s\n", res, strerror(errno));
2814 /* Wait just in case */
2821 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2829 static void *ss_thread(void *data);
2831 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2833 static int attempt_transfer(struct zt_pvt *p)
2835 /* In order to transfer, we need at least one of the channels to
2836 actually be in a call bridge. We can't conference two applications
2837 together (but then, why would we want to?) */
2838 if (p->subs[SUB_REAL].owner->bridge) {
2839 /* The three-way person we're about to transfer to could still be in MOH, so
2840 stop if now if appropriate */
2841 if (p->subs[SUB_THREEWAY].owner->bridge)
2842 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2843 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2844 ast_indicate(p->subs[SUB_REAL].owner->bridge, AST_CONTROL_RINGING);
2846 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, p->subs[SUB_REAL].owner->bridge)) {
2847 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2848 p->subs[SUB_REAL].owner->bridge->name, p->subs[SUB_THREEWAY].owner->name);
2851 /* Orphan the channel */
2852 unalloc_sub(p, SUB_THREEWAY);
2853 } else if (p->subs[SUB_THREEWAY].owner->bridge) {
2854 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2855 ast_indicate(p->subs[SUB_THREEWAY].owner->bridge, AST_CONTROL_RINGING);
2857 ast_moh_stop(p->subs[SUB_THREEWAY].owner->bridge);
2858 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, p->subs[SUB_THREEWAY].owner->bridge)) {
2859 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2860 p->subs[SUB_THREEWAY].owner->bridge->name, p->subs[SUB_REAL].owner->name);
2863 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2864 unalloc_sub(p, SUB_THREEWAY);
2865 /* Tell the caller not to hangup */
2868 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2869 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2870 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2876 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2878 struct ast_frame *f;
2879 f = &p->subs[index].f;
2881 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2885 case MFCR2_EVENT_BLOCKED:
2886 if (option_verbose > 2)
2887 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2889 case MFCR2_EVENT_UNBLOCKED:
2890 if (option_verbose > 2)
2891 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2893 case MFCR2_EVENT_CONFIG_ERR:
2894 if (option_verbose > 2)
2895 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2897 case MFCR2_EVENT_RING:
2898 if (option_verbose > 2)
2899 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
2901 case MFCR2_EVENT_HANGUP:
2902 if (option_verbose > 2)
2903 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
2905 case MFCR2_EVENT_RINGING:
2906 if (option_verbose > 2)
2907 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
2909 case MFCR2_EVENT_ANSWER:
2910 if (option_verbose > 2)
2911 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
2913 case MFCR2_EVENT_HANGUP_ACK:
2914 if (option_verbose > 2)
2915 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
2917 case MFCR2_EVENT_IDLE:
2918 if (option_verbose > 2)
2919 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
2922 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
2928 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
2933 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
2935 ast_log(LOG_WARNING, "Unable to check received bits\n");
2939 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
2942 e = mfcr2_cas_signaling_event(p->r2, x);
2947 static int check_for_conference(struct zt_pvt *p)
2950 /* Fine if we already have a master, etc */
2951 if (p->master || (p->confno > -1))
2953 memset(&ci, 0, sizeof(ci));
2954 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
2955 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
2958 /* If we have no master and don't have a confno, then
2959 if we're in a conference, it's probably a MeetMe room or
2960 some such, so don't let us 3-way out! */
2961 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
2962 if (option_verbose > 2)
2963 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
2969 static int get_alarms(struct zt_pvt *p)
2973 memset(&zi, 0, sizeof(zi));
2974 zi.spanno = p->span;
2975 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
2977 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
2983 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
2988 struct zt_pvt *p = ast->pvt->pvt;
2990 pthread_attr_t attr;
2991 struct ast_channel *chan;
2992 pthread_attr_init(&attr);
2993 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2994 index = zt_get_index(ast, p, 0);
2995 p->subs[index].f.frametype = AST_FRAME_NULL;
2996 p->subs[index].f.datalen = 0;
2997 p->subs[index].f.samples = 0;
2998 p->subs[index].f.mallocd = 0;
2999 p->subs[index].f.offset = 0;
3000 p->subs[index].f.src = "zt_handle_event";
3001 p->subs[index].f.data = NULL;
3003 return &p->subs[index].f;
3004 if (p->fake_event) {
3005 res = p->fake_event;
3008 res = zt_get_event(p->subs[index].zfd);
3009 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
3010 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
3011 if (res & ZT_EVENT_PULSEDIGIT)
3015 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
3016 p->subs[index].f.frametype = AST_FRAME_DTMF;
3017 p->subs[index].f.subclass = res & 0xff;
3018 /* Return the captured digit */
3019 return &p->subs[index].f;
3022 case ZT_EVENT_BITSCHANGED:
3023 if (p->sig == SIG_R2) {
3025 struct ast_frame *f = &p->subs[index].f;
3027 e = r2_get_event_bits(p);
3029 f = handle_r2_event(p, e, index);
3035 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
3036 case ZT_EVENT_PULSE_START:
3037 /* Stop tone if there's a pulse start and the PBX isn't started */
3039 tone_zone_play_tone(p->subs[index].zfd, -1);
3041 case ZT_EVENT_DIALCOMPLETE:
3042 if (p->inalarm) break;
3043 if (p->radio) break;
3044 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
3045 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
3048 if (!x) { /* if not still dialing in driver */
3052 strncpy(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr) - 1);
3053 p->dop.op = ZT_DIAL_OP_REPLACE;
3054 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
3058 if (p->sig == SIG_E911) {
3059 /* if thru with dialing after offhook */
3060 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
3061 ast_setstate(ast, AST_STATE_UP);
3062 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3063 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3065 } else { /* if to state wait for offhook to dial rest */
3066 /* we now wait for off hook */
3067 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
3070 if (ast->_state == AST_STATE_DIALING) {
3071 if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
3072 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
3073 } 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)))) {
3074 ast_setstate(ast, AST_STATE_RINGING);
3076 ast_setstate(ast, AST_STATE_UP);
3077 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3078 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3084 case ZT_EVENT_ALARM:
3087 if (p->pri && p->pri->pri) {
3088 if (!pri_grab(p, p->pri)) {
3089 pri_hangup(p->pri->pri, p->call, -1);
3090 pri_destroycall(p->pri->pri, p->call);
3093 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
3095 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
3098 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
3101 p->bearer->inalarm = 1;
3105 res = get_alarms(p);
3106 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
3107 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
3110 alarm2str(res), p->channel);
3111 /* fall through intentionally */
3112 case ZT_EVENT_ONHOOK:
3115 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3116 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
3123 p->onhooktime = time(NULL);
3125 /* Check for some special conditions regarding call waiting */
3126 if (index == SUB_REAL) {
3127 /* The normal line was hung up */
3128 if (p->subs[SUB_CALLWAIT].owner) {
3129 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
3130 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3131 if (option_verbose > 2)
3132 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
3133 unalloc_sub(p, SUB_CALLWAIT);
3135 p->subs[index].needanswer = 0;
3136 p->subs[index].needringing = 0;
3138 p->callwaitingrepeat = 0;
3141 /* Don't start streaming audio yet if the incoming call isn't up yet */
3142 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
3145 } else if (p->subs[SUB_THREEWAY].owner) {
3147 unsigned int mssinceflash;
3148 gettimeofday(&tv, NULL);
3149 mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
3150 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
3151 if (mssinceflash < MIN_MS_SINCE_FLASH) {
3152 /* It hasn't been long enough since the last flashook. This is probably a bounce on
3153 hanging up. Hangup both channels now */
3154 if (p->subs[SUB_THREEWAY].owner)
3155 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
3156 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3157 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
3158 } else if ((ast->pbx) ||
3159 (ast->_state == AST_STATE_UP)) {
3161 /* In any case this isn't a threeway call anymore */
3162 p->subs[SUB_REAL].inthreeway = 0;
3163 p->subs[SUB_THREEWAY].inthreeway = 0;
3164 if ((res = attempt_transfer(p)) < 0)
3165 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3167 /* Don't actually hang up at this point */
3171 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3173 /* Swap subs and dis-own channel */
3174 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3176 /* Ring the phone */
3181 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
3189 case ZT_EVENT_RINGOFFHOOK:
3190 if (p->inalarm) break;
3193 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3194 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
3197 /* for E911, its supposed to wait for offhook then dial
3198 the second half of the dial string */
3199 if ((p->sig == SIG_E911) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
3200 c = strchr(p->dialdest, '/');
3205 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
3206 else strncpy(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr) - 1);
3207 if (strlen(p->dop.dialstr) > 4) {
3208 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
3209 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
3210 p->echorest[sizeof(p->echorest) - 1] = '\0';
3212 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
3215 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
3217 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
3218 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
3222 return &p->subs[index].f;
3228 switch(ast->_state) {
3229 case AST_STATE_RINGING:
3232 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3233 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3234 /* Make sure it stops ringing */
3235 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
3236 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
3238 /* Cancel any running CallerID spill */
3244 if (p->confirmanswer) {
3245 /* Ignore answer if "confirm answer" is enabled */
3246 p->subs[index].f.frametype = AST_FRAME_NULL;
3247 p->subs[index].f.subclass = 0;
3248 } else if (!ast_strlen_zero(p->dop.dialstr)) {
3249 /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
3250 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
3252 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
3253 p->dop.dialstr[0] = '\0';
3256 ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
3257 p->subs[index].f.frametype = AST_FRAME_NULL;
3258 p->subs[index].f.subclass = 0;
3261 p->dop.dialstr[0] = '\0';
3262 ast_setstate(ast, AST_STATE_DIALING);
3264 ast_setstate(ast, AST_STATE_UP);
3265 return &p->subs[index].f;
3266 case AST_STATE_DOWN:
3267 ast_setstate(ast, AST_STATE_RING);
3269 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3270 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
3271 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
3272 return &p->subs[index].f;
3274 /* Make sure it stops ringing */
3275 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
3276 /* Okay -- probably call waiting*/
3277 if (p->owner->bridge)
3278 ast_moh_stop(p->owner->bridge);
3280 case AST_STATE_RESERVED:
3281 /* Start up dialtone */
3282 if (has_voicemail(p))
3283 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
3285 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
3288 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);