2 * Asterisk -- A telephony toolkit for Linux.
4 * Zaptel Pseudo TDM interface
6 * Copyright (C) 2003-2004, Digium, Inc.
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/features.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"
61 #ifndef PRI_SWITCH_QSIG
62 #error "You need newer libpri"
69 #include "../asterisk.h"
72 #error "Your zaptel is too old. please cvs update"
76 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
77 * the user hangs up to reset the state machine so ring works properly.
78 * This is used to be able to support kewlstart by putting the zhone in
79 * groundstart mode since their forward disconnect supervision is entirely
80 * broken even though their documentation says it isn't and their support
81 * is entirely unwilling to provide any assistance with their channel banks
82 * even though their web site says they support their products for life.
85 /* #define ZHONE_HACK */
88 * Define if you want to check the hook state for an FXO (FXS signalled) interface
89 * before dialing on it. Certain FXO interfaces always think they're out of
90 * service with this method however.
92 /* #define ZAP_CHECK_HOOKSTATE */
94 /* Typically, how many rings before we should send Caller*ID */
95 #define DEFAULT_CIDRINGS 1
97 #define CHANNEL_PSEUDO -12
99 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
101 static char *desc = "Zapata Telephony"
110 static char *tdesc = "Zapata Telephony Driver"
119 static char *type = "Zap";
120 static char *config = "zapata.conf";
122 #define SIG_EM ZT_SIG_EM
123 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
124 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
125 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
126 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
127 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
128 #define SIG_FXSLS ZT_SIG_FXSLS
129 #define SIG_FXSGS ZT_SIG_FXSGS
130 #define SIG_FXSKS ZT_SIG_FXSKS
131 #define SIG_FXOLS ZT_SIG_FXOLS
132 #define SIG_FXOGS ZT_SIG_FXOGS
133 #define SIG_FXOKS ZT_SIG_FXOKS
134 #define SIG_PRI ZT_SIG_CLEAR
135 #define SIG_R2 ZT_SIG_CAS
136 #define SIG_SF ZT_SIG_SF
137 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
138 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
139 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
140 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
141 #define SIG_EM_E1 ZT_SIG_EM_E1
142 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
143 #define SIG_GR303FXSKS (0x200000 | ZT_SIG_FXSKS)
146 #define NUM_DCHANS 4 /* No more than 4 d-channels */
147 #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
148 #define RESET_INTERVAL 3600 /* How often (in seconds) to reset unused channels */
150 #define CHAN_PSEUDO -2
152 #define DCHAN_PROVISIONED (1 << 0)
153 #define DCHAN_NOTINALARM (1 << 1)
154 #define DCHAN_UP (1 << 2)
156 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
158 static char context[AST_MAX_EXTENSION] = "default";
159 static char cid_num[256] = "";
160 static char cid_name[256] = "";
162 static char language[MAX_LANGUAGE] = "";
163 static char musicclass[MAX_LANGUAGE] = "";
164 static char progzone[10]= "";
166 static int usedistinctiveringdetection = 0;
168 static int use_callerid = 1;
169 static int cid_signalling = CID_SIG_BELL;
170 static int cid_start = CID_START_RING;
171 static int zaptrcallerid = 0;
172 static int cur_signalling = -1;
174 static unsigned int cur_group = 0;
175 static unsigned int cur_callergroup = 0;
176 static unsigned int cur_pickupgroup = 0;
177 static int relaxdtmf = 0;
179 static int immediate = 0;
181 static int stripmsd = 0;
183 static int callwaiting = 0;
185 static int callwaitingcallerid = 0;
187 static int hidecallerid = 0;
189 static int restrictcid = 0;
191 static int use_callingpres = 0;
193 static int callreturn = 0;
195 static int threewaycalling = 0;
197 static int transfer = 0;
199 static int canpark = 0;
201 static int cancallforward = 0;
203 static float rxgain = 0.0;
205 static float txgain = 0.0;
207 static int tonezone = -1;
209 static int echocancel;
211 static int echotraining;
215 static int echocanbridged = 0;
217 static int busydetect = 0;
219 static int busycount = 3;
221 static int callprogress = 0;
223 static char accountcode[20] = "";
225 static char mailbox[AST_MAX_EXTENSION];
227 static int amaflags = 0;
231 static int numbufs = 4;
233 static int cur_prewink = -1;
234 static int cur_preflash = -1;
235 static int cur_wink = -1;
236 static int cur_flash = -1;
237 static int cur_start = -1;
238 static int cur_rxwink = -1;
239 static int cur_rxflash = -1;
240 static int cur_debounce = -1;
242 static int priindication_oob = 0;
245 static int minunused = 2;
246 static int minidle = 0;
247 static char idleext[AST_MAX_EXTENSION];
248 static char idledial[AST_MAX_EXTENSION];
249 static int overlapdial = 0;
250 static struct ast_channel inuse = { "GR-303InUse" };
251 #ifdef PRI_GETSET_TIMERS
252 static int pritimers[PRI_MAX_TIMERS];
256 /* Wait up to 16 seconds for first digit (FXO logic) */
257 static int firstdigittimeout = 16000;
259 /* How long to wait for following digits (FXO logic) */
260 static int gendigittimeout = 8000;
262 /* How long to wait for an extra digit, if there is an ambiguous match */
263 static int matchdigittimeout = 3000;
265 static int usecnt =0;
266 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
268 /* Protect the interface list (of zt_pvt's) */
269 AST_MUTEX_DEFINE_STATIC(iflock);
271 static int ifcount = 0;
273 /* Whether we hang up on a Polarity Switch event */
274 static int hanguponpolarityswitch = 0;
276 /* How long (ms) to ignore Polarity Switch events after we answer a call */
277 static int polarityonanswerdelay = 600;
279 /* Protect the monitoring thread, so only one process can kill or start it, and not
280 when it's doing something critical. */
281 AST_MUTEX_DEFINE_STATIC(monlock);
283 /* This is the thread for the monitor which checks for input on the channels
284 which are not currently in use. */
285 static pthread_t monitor_thread = AST_PTHREADT_NULL;
287 static int restart_monitor(void);
289 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
291 static int zt_sendtext(struct ast_channel *c, char *text);
293 static inline int zt_get_event(int fd)
295 /* Avoid the silly zt_getevent which ignores a bunch of events */
297 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
301 static inline int zt_wait_event(int fd)
303 /* Avoid the silly zt_waitevent which ignores a bunch of events */
305 i = ZT_IOMUX_SIGEVENT;
306 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
307 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
311 /* Chunk size to read -- we use 20ms chunks to make things happy. */
312 #define READ_SIZE 160
314 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
315 #define MASK_INUSE (1 << 1) /* Channel currently in use */
317 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
318 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
319 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
320 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
321 #define RINGT ( (8000 * 8) / READ_SIZE)
327 static int r2prot = -1;
333 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8))
334 #define PRI_CHANNEL(p) ((p) & 0xff)
335 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
338 pthread_t master; /* Thread of master */
339 ast_mutex_t lock; /* Mutex */
340 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
341 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
342 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
343 int minunused; /* Min # of channels to keep empty */
344 int minidle; /* Min # of "idling" calls to keep active */
345 int nodetype; /* Node type */
346 int switchtype; /* Type of switch to emulate */
347 int nsf; /* Network-Specific Facilities */
348 int dialplan; /* Dialing plan */
349 int localdialplan; /* Local dialing plan */
350 int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
351 int trunkgroup; /* What our trunkgroup is */
352 int mastertrunkgroup; /* What trunk group is our master */
353 int prilogicalspan; /* Logical span number within trunk group */
354 int numchans; /* Num of channels we represent */
355 int overlapdial; /* In overlap dialing mode */
356 struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
357 int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
358 struct pri *pri; /* Currently active D-channel */
360 int fds[NUM_DCHANS]; /* FD's for d-channels */
366 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
367 struct zt_pvt *crvs; /* Member CRV structs */
368 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
372 static struct zt_pri pris[NUM_SPANS];
374 static int pritype = PRI_CPE;
377 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
379 #define DEFAULT_PRI_DEBUG 0
382 static inline void pri_rel(struct zt_pri *pri)
384 ast_mutex_unlock(&pri->lock);
387 static int switchtype = PRI_SWITCH_NI2;
388 static int nsf = PRI_NSF_NONE;
389 static int dialplan = PRI_NATIONAL_ISDN + 1;
390 static int localdialplan = PRI_NATIONAL_ISDN + 1;
393 /* Shut up the compiler */
397 #define SUB_REAL 0 /* Active call */
398 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
399 #define SUB_THREEWAY 2 /* Three-way call */
402 static struct zt_distRings drings;
404 struct distRingData {
407 struct ringContextData {
408 char contextData[AST_MAX_EXTENSION];
410 struct zt_distRings {
411 struct distRingData ringnum[3];
412 struct ringContextData ringContext[3];
415 static char *subnames[] = {
421 struct zt_subchannel {
423 struct ast_channel *owner;
425 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
426 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
438 #define CONF_USER_REAL (1 << 0)
439 #define CONF_USER_THIRDCALL (1 << 1)
443 static struct zt_pvt {
445 struct ast_channel *owner; /* Our current active owner (if applicable) */
446 /* Up to three channels can be associated with this call */
448 struct zt_subchannel sub_unused; /* Just a safety precaution */
449 struct zt_subchannel subs[3]; /* Sub-channels */
450 struct zt_confinfo saveconf; /* Saved conference info */
452 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
453 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
454 int inconference; /* If our real should be in the conference */
456 int sig; /* Signalling style */
457 int radio; /* radio type */
458 int firstradio; /* first radio flag */
461 int tonezone; /* tone zone for this chan, or -1 for default */
462 struct zt_pvt *next; /* Next channel in list */
463 struct zt_pvt *prev; /* Prev channel in list */
465 struct zt_distRings drings;
466 int usedistinctiveringdetection;
468 char context[AST_MAX_EXTENSION];
469 char defcontext[AST_MAX_EXTENSION];
470 char exten[AST_MAX_EXTENSION];
471 char language[MAX_LANGUAGE];
472 char musicclass[MAX_LANGUAGE];
473 char cid_num[AST_MAX_EXTENSION];
474 char cid_name[AST_MAX_EXTENSION];
475 char lastcid_num[AST_MAX_EXTENSION];
476 char lastcid_name[AST_MAX_EXTENSION];
477 char *origcid_num; /* malloced original callerid */
478 char *origcid_name; /* malloced original callerid */
479 char callwait_num[AST_MAX_EXTENSION];
480 char callwait_name[AST_MAX_EXTENSION];
481 char rdnis[AST_MAX_EXTENSION];
482 char dnid[AST_MAX_EXTENSION];
485 int confno; /* Our conference */
486 int confusers; /* Who is using our conference */
487 int propconfno; /* Propagated conference number */
488 unsigned int callgroup;
489 unsigned int pickupgroup;
490 int immediate; /* Answer before getting digits? */
491 int channel; /* Channel Number or CRV */
492 int span; /* Span number */
494 time_t guardtime; /* Must wait this much time before using for new call */
496 int use_callerid; /* Whether or not to use caller id on this channel */
497 int cid_signalling; /* CID signalling type bell202 or v23 */
498 int cid_start; /* CID start indicator, polarity or ring */
501 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
502 int restrictcid; /* Whether restrict the callerid -> only send ANI */
503 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
504 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
505 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
506 int cidcwexpire; /* When to expire our muting for CID/CW */
507 unsigned char *cidspill;
523 int callwaitingcallerid;
533 int priindication_oob;
534 struct timeval flashtime; /* Last flash-hook time */
536 int cref; /* Call reference number */
537 ZT_DIAL_OPERATION dop;
541 char accountcode[20]; /* Account code */
542 int amaflags; /* AMA Flags */
543 char didtdd; /* flag to say its done it once */
544 struct tdd_state *tdd; /* TDD flag */
547 char call_forward[AST_MAX_EXTENSION];
548 char mailbox[AST_MAX_EXTENSION];
553 int confirmanswer; /* Wait for '#' to confirm answer */
554 int distinctivering; /* Which distinctivering to use */
555 int cidrings; /* Which ring to deliver CID on */
557 int faxhandled; /* Has a fax tone already been handled? */
559 char mate; /* flag to say its in MATE mode */
560 int pulsedial; /* whether a pulse dial phone is detected */
561 int dtmfrelax; /* whether to run in relaxed DTMF mode */
563 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
564 int hanguponpolarityswitch;
565 int polarityonanswerdelay;
566 struct timeval polaritydelaytv;
569 struct zt_pvt *bearer;
570 struct zt_pvt *realcall;
578 int setup_ack; /* wheter we received SETUP_ACKNOWLEDGE or not */
587 } *iflist = NULL, *ifend = NULL;
590 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
592 #define GET_CHANNEL(p) ((p)->channel)
595 struct zt_pvt *round_robin[32];
598 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
601 /* Grab the lock first */
603 res = ast_mutex_trylock(&pri->lock);
605 ast_mutex_unlock(&pvt->lock);
606 /* Release the lock and try again */
608 ast_mutex_lock(&pvt->lock);
611 /* Then break the poll */
612 pthread_kill(pri->master, SIGURG);
617 #define NUM_CADENCE_MAX 25
618 static int num_cadence = 4;
619 static int user_has_defined_cadences = 0;
621 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
622 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
623 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
624 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
625 { { 1000, 500, 2500, 5000 } }, /* Long ring */
628 int receivedRingT; /* Used to find out what ringtone we are on */
630 /* cidrings says in which pause to transmit the cid information, where the first pause
631 * is 1, the second pause is 2 and so on.
634 static int cidrings[NUM_CADENCE_MAX] = {
635 2, /* Right after first long ring */
636 4, /* Right after long part */
637 3, /* After third chirp */
638 2, /* Second spell */
641 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
642 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
644 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
645 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
647 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
650 if (p->subs[0].owner == ast)
652 else if (p->subs[1].owner == ast)
654 else if (p->subs[2].owner == ast)
659 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
665 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
667 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
670 struct ast_frame null = { AST_FRAME_NULL, };
673 ast_mutex_unlock(&pri->lock);
676 if (p->subs[a].owner) {
677 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
678 ast_mutex_unlock(&p->lock);
680 ast_mutex_lock(&p->lock);
682 ast_queue_frame(p->subs[a].owner, &null);
683 ast_mutex_unlock(&p->subs[a].owner->lock);
691 ast_mutex_lock(&pri->lock);
696 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
698 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
701 /* We must unlock the PRI to avoid the possibility of a deadlock */
704 ast_mutex_unlock(&pri->lock);
708 if (ast_mutex_trylock(&p->owner->lock)) {
709 ast_mutex_unlock(&p->lock);
711 ast_mutex_lock(&p->lock);
713 ast_queue_frame(p->owner, f);
714 ast_mutex_unlock(&p->owner->lock);
722 ast_mutex_lock(&pri->lock);
726 static void swap_subs(struct zt_pvt *p, int a, int b)
730 struct ast_channel *towner;
732 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
734 tchan = p->subs[a].chan;
735 towner = p->subs[a].owner;
736 tinthreeway = p->subs[a].inthreeway;
738 p->subs[a].chan = p->subs[b].chan;
739 p->subs[a].owner = p->subs[b].owner;
740 p->subs[a].inthreeway = p->subs[b].inthreeway;
742 p->subs[b].chan = tchan;
743 p->subs[b].owner = towner;
744 p->subs[b].inthreeway = tinthreeway;
746 if (p->subs[a].owner)
747 p->subs[a].owner->fds[0] = p->subs[a].zfd;
748 if (p->subs[b].owner)
749 p->subs[b].owner->fds[0] = p->subs[b].zfd;
750 wakeup_sub(p, a, NULL);
751 wakeup_sub(p, b, NULL);
754 static int zt_open(char *fn)
762 for (x=0;x<strlen(fn);x++) {
763 if (!isdigit(fn[x])) {
771 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
774 fn = "/dev/zap/channel";
776 fd = open(fn, O_RDWR | O_NONBLOCK);
778 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
782 if (ioctl(fd, ZT_SPECIFY, &chan)) {
786 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
791 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
795 static void zt_close(int fd)
801 int zt_setlinear(int zfd, int linear)
804 res = ioctl(zfd, ZT_SETLINEAR, &linear);
811 int zt_setlaw(int zfd, int law)
814 res = ioctl(zfd, ZT_SETLAW, &law);
820 static int alloc_sub(struct zt_pvt *p, int x)
824 if (p->subs[x].zfd < 0) {
825 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
826 if (p->subs[x].zfd > -1) {
827 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
829 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
830 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
831 bi.numbufs = numbufs;
832 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
834 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
837 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
838 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
839 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
840 zt_close(p->subs[x].zfd);
845 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
848 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
851 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
855 static int unalloc_sub(struct zt_pvt *p, int x)
858 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
861 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
862 if (p->subs[x].zfd > -1) {
863 zt_close(p->subs[x].zfd);
866 p->subs[x].linear = 0;
868 p->subs[x].owner = NULL;
869 p->subs[x].inthreeway = 0;
870 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
874 static int zt_digit(struct ast_channel *ast, char digit)
876 ZT_DIAL_OPERATION zo;
881 ast_mutex_lock(&p->lock);
882 index = zt_get_index(ast, p, 0);
883 if (index == SUB_REAL) {
885 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
887 if (!pri_grab(p, p->pri)) {
888 pri_information(p->pri->pri,p->call,digit);
891 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
892 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
893 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
894 res = strlen(p->dialdest);
895 p->dialdest[res++] = digit;
896 p->dialdest[res] = '\0';
902 zo.op = ZT_DIAL_OP_APPEND;
904 zo.dialstr[1] = digit;
906 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
907 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
912 ast_mutex_unlock(&p->lock);
916 static char *events[] = {
929 "Hook Transition Complete",
941 { ZT_ALARM_RED, "Red Alarm" },
942 { ZT_ALARM_YELLOW, "Yellow Alarm" },
943 { ZT_ALARM_BLUE, "Blue Alarm" },
944 { ZT_ALARM_RECOVER, "Recovering" },
945 { ZT_ALARM_LOOPBACK, "Loopback" },
946 { ZT_ALARM_NOTOPEN, "Not Open" },
947 { ZT_ALARM_NONE, "None" },
950 static char *alarm2str(int alarm)
953 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
954 if (alarms[x].alarm & alarm)
955 return alarms[x].name;
957 return alarm ? "Unknown Alarm" : "No Alarm";
960 static char *event2str(int event)
962 static char buf[256];
963 if ((event < 18) && (event > -1))
964 return events[event];
965 sprintf(buf, "Event %d", event); /* safe */
970 static int str2r2prot(char *swtype)
972 if (!strcasecmp(swtype, "ar"))
973 return MFCR2_PROT_ARGENTINA;
975 if (!strcasecmp(swtype, "cn"))
976 return MFCR2_PROT_CHINA;
978 if (!strcasecmp(swtype, "kr"))
979 return MFCR2_PROT_KOREA;
985 static char *sig2str(int sig)
987 static char buf[256];
990 return "E & M Immediate";
996 return "Feature Group D (DTMF)";
998 return "Feature Group D (MF)";
1000 return "Feature Group B (MF)";
1004 return "FXS Loopstart";
1006 return "FXS Groundstart";
1008 return "FXS Kewlstart";
1010 return "FXO Loopstart";
1012 return "FXO Groundstart";
1014 return "FXO Kewlstart";
1016 return "PRI Signalling";
1018 return "R2 Signalling";
1020 return "SF (Tone) Signalling Immediate";
1022 return "SF (Tone) Signalling Wink";
1024 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1025 case SIG_SF_FEATDMF:
1026 return "SF (Tone) Signalling with Feature Group D (MF)";
1028 return "SF (Tone) Signalling with Feature Group B (MF)";
1029 case SIG_GR303FXOKS:
1030 return "GR-303 Signalling with FXOKS";
1031 case SIG_GR303FXSKS:
1032 return "GR-303 Signalling with FXSKS";
1034 return "Pseudo Signalling";
1036 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1041 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1043 /* If the conference already exists, and we're already in it
1044 don't bother doing anything */
1047 memset(&zi, 0, sizeof(zi));
1050 if (slavechannel > 0) {
1051 /* If we have only one slave, do a digital mon */
1052 zi.confmode = ZT_CONF_DIGITALMON;
1053 zi.confno = slavechannel;
1056 /* Real-side and pseudo-side both participate in conference */
1057 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1058 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1060 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1061 zi.confno = p->confno;
1063 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1067 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1068 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1071 if (slavechannel < 1) {
1072 p->confno = zi.confno;
1074 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1075 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1079 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1081 /* If they're listening to our channel, they're ours */
1082 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1084 /* If they're a talker on our (allocated) conference, they're ours */
1085 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1090 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1093 if (/* Can't delete if there's no zfd */
1095 /* Don't delete from the conference if it's not our conference */
1097 /* Don't delete if we don't think it's conferenced at all (implied) */
1099 memset(&zi, 0, sizeof(zi));
1103 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1104 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1107 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1108 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1112 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1116 struct zt_pvt *slave = NULL;
1117 /* Start out optimistic */
1119 /* Update conference state in a stateless fashion */
1121 /* Any three-way calling makes slave native mode *definitely* out
1123 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1126 /* If we don't have any 3-way calls, check to see if we have
1127 precisely one slave */
1128 if (useslavenative) {
1129 for (x=0;x<MAX_SLAVES;x++) {
1132 /* Whoops already have a slave! No
1133 slave native and stop right away */
1138 /* We have one slave so far */
1139 slave = p->slaves[x];
1144 /* If no slave, slave native definitely out */
1147 else if (slave->law != p->law) {
1153 return useslavenative;
1156 static int reset_conf(struct zt_pvt *p)
1159 memset(&zi, 0, sizeof(zi));
1161 if (p->subs[SUB_REAL].zfd > -1) {
1162 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1163 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1168 static int update_conf(struct zt_pvt *p)
1173 struct zt_pvt *slave = NULL;
1175 useslavenative = isslavenative(p, &slave);
1176 /* Start with the obvious, general stuff */
1178 /* Look for three way calls */
1179 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1180 conf_add(p, &p->subs[x], x, 0);
1183 conf_del(p, &p->subs[x], x);
1186 /* If we have a slave, add him to our conference now. or DAX
1187 if this is slave native */
1188 for (x=0;x<MAX_SLAVES;x++) {
1191 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1193 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1198 /* If we're supposed to be in there, do so now */
1199 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1201 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1203 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1207 /* If we have a master, add ourselves to his conference */
1209 if (isslavenative(p->master, NULL)) {
1210 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1212 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1216 /* Nobody is left (or should be left) in our conference.
1220 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1224 static void zt_enable_ec(struct zt_pvt *p)
1231 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1235 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1238 if (p->echocancel) {
1239 if (p->sig == SIG_PRI) {
1241 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1243 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1246 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1248 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1251 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1254 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1257 static void zt_train_ec(struct zt_pvt *p)
1261 if (p && p->echocancel && p->echotraining) {
1262 x = p->echotraining;
1263 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1265 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1267 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1270 ast_log(LOG_DEBUG, "No echo training requested\n");
1273 static void zt_disable_ec(struct zt_pvt *p)
1277 if (p->echocancel) {
1279 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1281 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1283 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1288 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1295 if ((rxgain != 0.0) || (txgain != 0.0)) {
1296 /* caluculate linear value of tx gain */
1297 ltxgain = pow(10.0,txgain / 20.0);
1298 /* caluculate linear value of rx gain */
1299 lrxgain = pow(10.0,rxgain / 20.0);
1300 if (law == ZT_LAW_ALAW) {
1301 for (j=0;j<256;j++) {
1302 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1303 if (k > 32767) k = 32767;
1304 if (k < -32767) k = -32767;
1305 g.rxgain[j] = AST_LIN2A(k);
1306 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1307 if (k > 32767) k = 32767;
1308 if (k < -32767) k = -32767;
1309 g.txgain[j] = AST_LIN2A(k);
1312 for (j=0;j<256;j++) {
1313 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1314 if (k > 32767) k = 32767;
1315 if (k < -32767) k = -32767;
1316 g.rxgain[j] = AST_LIN2MU(k);
1317 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1318 if (k > 32767) k = 32767;
1319 if (k < -32767) k = -32767;
1320 g.txgain[j] = AST_LIN2MU(k);
1324 for (j=0;j<256;j++) {
1331 return(ioctl(fd,ZT_SETGAINS,&g));
1334 static inline int zt_set_hook(int fd, int hs)
1338 res = ioctl(fd, ZT_HOOK, &x);
1341 if (errno == EINPROGRESS) return 0;
1342 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1347 static inline int zt_confmute(struct zt_pvt *p, int muted)
1351 if (p->sig == SIG_PRI) {
1353 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1355 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1357 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1359 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1363 static int save_conference(struct zt_pvt *p)
1365 struct zt_confinfo c;
1367 if (p->saveconf.confmode) {
1368 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1371 p->saveconf.chan = 0;
1372 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1374 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1375 p->saveconf.confmode = 0;
1380 c.confmode = ZT_CONF_NORMAL;
1381 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1383 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1387 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1391 static int restore_conference(struct zt_pvt *p)
1394 if (p->saveconf.confmode) {
1395 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1396 p->saveconf.confmode = 0;
1398 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1403 ast_log(LOG_DEBUG, "Restored conferencing\n");
1407 static int send_callerid(struct zt_pvt *p);
1409 int send_cwcidspill(struct zt_pvt *p)
1413 p->cidspill = malloc(MAX_CALLERID_SIZE);
1415 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1416 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1417 /* Make sure we account for the end */
1418 p->cidlen += READ_SIZE * 4;
1421 if (option_verbose > 2)
1422 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1427 static int has_voicemail(struct zt_pvt *p)
1430 return ast_app_has_voicemail(p->mailbox, NULL);
1433 static int send_callerid(struct zt_pvt *p)
1435 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1437 /* Take out of linear mode if necessary */
1438 if (p->subs[SUB_REAL].linear) {
1439 p->subs[SUB_REAL].linear = 0;
1440 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1442 while(p->cidpos < p->cidlen) {
1443 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1445 if (errno == EAGAIN)
1448 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1458 if (p->callwaitcas) {
1459 /* Wait for CID/CW to expire */
1460 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1462 restore_conference(p);
1466 static int zt_callwait(struct ast_channel *ast)
1468 struct zt_pvt *p = ast->pvt->pvt;
1469 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1471 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1474 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1478 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1479 if (!p->callwaitrings && p->callwaitingcallerid) {
1480 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1482 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1484 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1486 p->cidlen = 2400 + READ_SIZE * 4;
1491 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1497 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1499 struct zt_pvt *p = ast->pvt->pvt;
1505 char dest[256]; /* must be same length as p->dialdest */
1506 ast_mutex_lock(&p->lock);
1507 strncpy(dest, rdest, sizeof(dest) - 1);
1508 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1509 if ((ast->_state == AST_STATE_BUSY)) {
1510 p->subs[SUB_REAL].needbusy = 1;
1511 ast_mutex_unlock(&p->lock);
1514 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1515 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1516 ast_mutex_unlock(&p->lock);
1520 if (p->radio) /* if a radio channel, up immediately */
1522 /* Special pseudo -- automatically up */
1523 ast_setstate(ast, AST_STATE_UP);
1524 ast_mutex_unlock(&p->lock);
1527 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1528 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1530 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1533 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1539 if (p->owner == ast) {
1540 /* Normal ring, on hook */
1542 /* Don't send audio while on hook, until the call is answered */
1544 if (p->use_callerid) {
1545 /* Generate the Caller-ID spill if desired */
1547 ast_log(LOG_WARNING, "cidspill already exists??\n");
1550 p->cidspill = malloc(MAX_CALLERID_SIZE);
1553 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1557 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1559 /* Choose proper cadence */
1560 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1561 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1562 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1563 p->cidrings = cidrings[p->distinctivering - 1];
1565 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1566 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1567 p->cidrings = DEFAULT_CIDRINGS;
1571 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1572 c = strchr(dest, '/');
1575 if (c && (strlen(c) < p->stripmsd)) {
1576 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1580 p->dop.op = ZT_DIAL_OP_REPLACE;
1581 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1582 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1584 p->dop.dialstr[0] = '\0';
1587 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1588 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1589 ast_mutex_unlock(&p->lock);
1594 /* Call waiting call */
1595 p->callwaitrings = 0;
1596 if (ast->cid.cid_num)
1597 strncpy(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num)-1);
1599 p->callwait_num[0] = '\0';
1600 if (ast->cid.cid_name)
1601 strncpy(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name)-1);
1603 p->callwait_name[0] = '\0';
1604 /* Call waiting tone instead */
1605 if (zt_callwait(ast)) {
1606 ast_mutex_unlock(&p->lock);
1609 /* Make ring-back */
1610 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1611 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1614 n = ast->cid.cid_name;
1615 l = ast->cid.cid_num;
1617 strncpy(p->lastcid_num, l, sizeof(p->lastcid_num) - 1);
1619 p->lastcid_num[0] = '\0';
1621 strncpy(p->lastcid_name, n, sizeof(p->lastcid_name) - 1);
1623 p->lastcid_name[0] = '\0';
1624 ast_setstate(ast, AST_STATE_RINGING);
1625 index = zt_get_index(ast, p, 0);
1627 p->subs[index].needringing = 1;
1643 case SIG_SF_FEATDMF:
1645 c = strchr(dest, '/');
1650 if (strlen(c) < p->stripmsd) {
1651 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1652 ast_mutex_unlock(&p->lock);
1656 /* Start the trunk, if not GR-303 */
1660 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1662 if (errno != EINPROGRESS) {
1663 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1664 ast_mutex_unlock(&p->lock);
1671 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1672 p->dop.op = ZT_DIAL_OP_REPLACE;
1673 if (p->sig == SIG_FEATD) {
1674 l = ast->cid.cid_num;
1676 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1678 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1680 if (p->sig == SIG_FEATDMF) {
1681 l = ast->cid.cid_num;
1683 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1685 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1687 if (p->sig == SIG_E911) {
1688 strncpy(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr) - 1);
1690 if (p->sig == SIG_FEATB) {
1691 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1694 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c + p->stripmsd);
1696 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1697 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1698 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1699 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1700 p->echorest[sizeof(p->echorest) - 1] = '\0';
1702 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1706 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1708 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1709 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1710 ast_mutex_unlock(&p->lock);
1714 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1716 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1717 ast_setstate(ast, AST_STATE_DIALING);
1720 /* Special pseudo -- automatically up*/
1721 ast_setstate(ast, AST_STATE_UP);
1724 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1725 p->dialdest[0] = '\0';
1728 ast_log(LOG_DEBUG, "not yet implemented\n");
1729 ast_mutex_unlock(&p->lock);
1735 c = strchr(dest, '/');
1740 if (!p->hidecallerid) {
1741 l = ast->cid.cid_num;
1742 n = ast->cid.cid_name;
1747 if (strlen(c) < p->stripmsd) {
1748 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1749 ast_mutex_unlock(&p->lock);
1752 if (p->sig != SIG_FXSKS) {
1753 p->dop.op = ZT_DIAL_OP_REPLACE;
1754 s = strchr(c + p->stripmsd, 'w');
1757 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1759 p->dop.dialstr[0] = '\0';
1762 p->dop.dialstr[0] = '\0';
1765 if (pri_grab(p, p->pri)) {
1766 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1767 ast_mutex_unlock(&p->lock);
1770 if (!(p->call = pri_new_call(p->pri->pri))) {
1771 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1773 ast_mutex_unlock(&p->lock);
1776 if (!(sr = pri_sr_new())) {
1777 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1779 ast_mutex_unlock(&p->lock);
1781 if (p->bearer || (p->sig == SIG_FXSKS)) {
1783 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);
1784 p->bearer->call = p->call;
1786 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
1787 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1789 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1790 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1791 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1792 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1794 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1795 pri_sr_set_called(sr, c + p->stripmsd, p->pri->dialplan - 1, s ? 1 : 0);
1796 pri_sr_set_caller(sr, l, n, p->pri->localdialplan - 1,
1797 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
1798 PRES_NUMBER_NOT_AVAILABLE);
1799 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
1800 if (pri_setup(p->pri->pri, p->call, sr)) {
1801 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1803 ast_mutex_unlock(&p->lock);
1808 ast_setstate(ast, AST_STATE_DIALING);
1812 ast_mutex_unlock(&p->lock);
1816 static void destroy_zt_pvt(struct zt_pvt **pvt)
1818 struct zt_pvt *p = *pvt;
1819 /* Remove channel from the list */
1821 p->prev->next = p->next;
1823 p->next->prev = p->prev;
1824 ast_mutex_destroy(&p->lock);
1829 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1839 for (i = 0; i < 3; i++) {
1840 if (cur->subs[i].owner) {
1846 prev->next = cur->next;
1848 prev->next->prev = prev;
1854 iflist->prev = NULL;
1858 if (cur->subs[SUB_REAL].zfd > -1) {
1859 zt_close(cur->subs[SUB_REAL].zfd);
1861 destroy_zt_pvt(&cur);
1865 prev->next = cur->next;
1867 prev->next->prev = prev;
1873 iflist->prev = NULL;
1877 if (cur->subs[SUB_REAL].zfd > -1) {
1878 zt_close(cur->subs[SUB_REAL].zfd);
1880 destroy_zt_pvt(&cur);
1886 int pri_is_up(struct zt_pri *pri)
1889 for (x=0;x<NUM_DCHANS;x++) {
1890 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1896 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
1898 bearer->owner = &inuse;
1899 bearer->master = crv;
1900 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
1901 if (crv->subs[SUB_REAL].owner)
1902 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
1903 crv->bearer = bearer;
1904 crv->call = bearer->call;
1909 static char *pri_order(int level)
1919 return "Quaternary";
1925 int pri_find_dchan(struct zt_pri *pri)
1932 for(x=0;x<NUM_DCHANS;x++) {
1933 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
1935 if (pri->dchans[x] == old) {
1941 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
1942 pri->dchannels[newslot]);
1944 if (old && (oldslot != newslot))
1945 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
1946 pri->dchannels[oldslot], pri->dchannels[newslot]);
1947 pri->pri = pri->dchans[newslot];
1952 static int zt_hangup(struct ast_channel *ast)
1956 static int restore_gains(struct zt_pvt *p);
1957 struct zt_pvt *p = ast->pvt->pvt;
1958 struct zt_pvt *tmp = NULL;
1959 struct zt_pvt *prev = NULL;
1963 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1964 if (!ast->pvt->pvt) {
1965 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1969 ast_mutex_lock(&p->lock);
1971 index = zt_get_index(ast, p, 1);
1973 if (p->sig == SIG_PRI) {
1975 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
1981 if (p->origcid_num) {
1982 strncpy(p->cid_num, p->origcid_num, sizeof(p->cid_num) - 1);
1983 free(p->origcid_num);
1984 p->origcid_num = NULL;
1986 if (p->origcid_name) {
1987 strncpy(p->cid_name, p->origcid_name, sizeof(p->cid_name) - 1);
1988 free(p->origcid_name);
1989 p->origcid_name = NULL;
1992 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
1996 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
1997 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2001 /* Real channel, do some fixup */
2002 p->subs[index].owner = NULL;
2003 p->subs[index].needanswer = 0;
2004 p->subs[index].needflash = 0;
2005 p->subs[index].needringing = 0;
2006 p->subs[index].needbusy = 0;
2007 p->subs[index].needcongestion = 0;
2008 p->subs[index].linear = 0;
2009 p->subs[index].needcallerid = 0;
2010 zt_setlinear(p->subs[index].zfd, 0);
2011 if (index == SUB_REAL) {
2012 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2013 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2014 if (p->subs[SUB_CALLWAIT].inthreeway) {
2015 /* We had flipped over to answer a callwait and now it's gone */
2016 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2017 /* Move to the call-wait, but un-own us until they flip back. */
2018 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2019 unalloc_sub(p, SUB_CALLWAIT);
2022 /* The three way hung up, but we still have a call wait */
2023 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2024 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2025 unalloc_sub(p, SUB_THREEWAY);
2026 if (p->subs[SUB_REAL].inthreeway) {
2027 /* This was part of a three way call. Immediately make way for
2029 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2030 p->owner = p->subs[SUB_REAL].owner;
2032 /* This call hasn't been completed yet... Set owner to NULL */
2033 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2036 p->subs[SUB_REAL].inthreeway = 0;
2038 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2039 /* Move to the call-wait and switch back to them. */
2040 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2041 unalloc_sub(p, SUB_CALLWAIT);
2042 p->owner = p->subs[SUB_REAL].owner;
2043 if (p->owner->_state != AST_STATE_UP)
2044 p->subs[SUB_REAL].needanswer = 1;
2045 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2046 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2047 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2048 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2049 unalloc_sub(p, SUB_THREEWAY);
2050 if (p->subs[SUB_REAL].inthreeway) {
2051 /* This was part of a three way call. Immediately make way for
2053 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2054 p->owner = p->subs[SUB_REAL].owner;
2056 /* This call hasn't been completed yet... Set owner to NULL */
2057 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2060 p->subs[SUB_REAL].inthreeway = 0;
2062 } else if (index == SUB_CALLWAIT) {
2063 /* Ditch the holding callwait call, and immediately make it availabe */
2064 if (p->subs[SUB_CALLWAIT].inthreeway) {
2065 /* This is actually part of a three way, placed on hold. Place the third part
2066 on music on hold now */
2067 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2068 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2069 p->subs[SUB_THREEWAY].inthreeway = 0;
2070 /* Make it the call wait now */
2071 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2072 unalloc_sub(p, SUB_THREEWAY);
2074 unalloc_sub(p, SUB_CALLWAIT);
2075 } else if (index == SUB_THREEWAY) {
2076 if (p->subs[SUB_CALLWAIT].inthreeway) {
2077 /* The other party of the three way call is currently in a call-wait state.
2078 Start music on hold for them, and take the main guy out of the third call */
2079 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2080 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2081 p->subs[SUB_CALLWAIT].inthreeway = 0;
2083 p->subs[SUB_REAL].inthreeway = 0;
2084 /* If this was part of a three way call index, let us make
2085 another three way call */
2086 unalloc_sub(p, SUB_THREEWAY);
2088 /* This wasn't any sort of call, but how are we an index? */
2089 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2094 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2097 p->distinctivering = 0;
2098 p->confirmanswer = 0;
2104 p->onhooktime = time(NULL);
2110 ast_dsp_free(p->dsp);
2114 law = ZT_LAW_DEFAULT;
2115 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2117 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2118 /* Perform low level hangup if no owner left */
2121 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2122 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2123 if (!pri_grab(p, p->pri)) {
2124 if (p->alreadyhungup) {
2125 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2126 pri_hangup(p->pri->pri, p->call, -1);
2129 p->bearer->call = NULL;
2131 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2132 int icause = ast->hangupcause ? ast->hangupcause : -1;
2133 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2134 p->alreadyhungup = 1;
2136 p->bearer->alreadyhungup = 1;
2139 icause = atoi(cause);
2141 pri_hangup(p->pri->pri, p->call, icause);
2144 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2147 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2152 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2159 if (p->sig == SIG_R2) {
2161 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2169 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2170 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2172 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2178 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2181 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2183 /* If they're off hook, try playing congestion */
2184 if ((par.rxisoffhook) && (!p->radio))
2185 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2187 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2193 /* Make sure we're not made available for at least two seconds assuming
2194 we were actually used for an inbound or outbound call. */
2195 if (ast->_state != AST_STATE_RESERVED) {
2196 time(&p->guardtime);
2201 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2208 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2209 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2213 p->callwaiting = p->permcallwaiting;
2214 p->hidecallerid = p->permhidecallerid;
2219 /* Restore data mode */
2220 if (p->sig == SIG_PRI) {
2222 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2226 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2227 /* Free up the bearer channel as well, and
2228 don't use its file descriptor anymore */
2229 update_conf(p->bearer);
2230 reset_conf(p->bearer);
2231 p->bearer->owner = NULL;
2233 p->subs[SUB_REAL].zfd = -1;
2241 p->callwaitingrepeat = 0;
2243 ast->pvt->pvt = NULL;
2244 ast_mutex_unlock(&p->lock);
2245 ast_mutex_lock(&usecnt_lock);
2248 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2249 ast_mutex_unlock(&usecnt_lock);
2250 ast_update_use_count();
2251 if (option_verbose > 2)
2252 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2254 ast_mutex_lock(&iflock);
2260 destroy_channel(prev, tmp, 0);
2268 ast_mutex_unlock(&iflock);
2272 static int zt_answer(struct ast_channel *ast)
2274 struct zt_pvt *p = ast->pvt->pvt;
2277 int oldstate = ast->_state;
2278 ast_setstate(ast, AST_STATE_UP);
2279 ast_mutex_lock(&p->lock);
2280 index = zt_get_index(ast, p, 0);
2283 /* nothing to do if a radio channel */
2285 ast_mutex_unlock(&p->lock);
2304 case SIG_SF_FEATDMF:
2309 /* Pick up the line */
2310 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2311 if(p->hanguponpolarityswitch) {
2312 gettimeofday(&p->polaritydelaytv, NULL);
2314 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2315 tone_zone_play_tone(p->subs[index].zfd, -1);
2317 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2318 if (oldstate == AST_STATE_RINGING) {
2319 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2320 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2321 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2322 p->owner = p->subs[SUB_REAL].owner;
2325 if (p->sig & __ZT_SIG_FXS) {
2332 /* Send a pri acknowledge */
2333 if (!pri_grab(p, p->pri)) {
2335 res = pri_answer(p->pri->pri, p->call, 0, 1);
2338 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2345 res = mfcr2_AnswerCall(p->r2, NULL);
2347 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2351 ast_mutex_unlock(&p->lock);
2354 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2357 ast_mutex_unlock(&p->lock);
2361 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2366 struct zt_pvt *p = chan->pvt->pvt;
2369 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2370 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2376 if ((!cp) || (datalen < 1))
2382 case AST_OPTION_TONE_VERIFY:
2387 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2388 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2391 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2392 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2395 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2396 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2400 case AST_OPTION_TDD: /* turn on or off TDD */
2401 if (!*cp) { /* turn it off */
2402 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2403 if (p->tdd) tdd_free(p->tdd);
2409 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2410 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2413 /* otherwise, turn it on */
2414 if (!p->didtdd) { /* if havent done it yet */
2415 unsigned char mybuf[41000],*buf;
2416 int size,res,fd,len;
2418 struct pollfd fds[1];
2420 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2421 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2423 index = zt_get_index(chan, p, 0);
2425 ast_log(LOG_WARNING, "No index in TDD?\n");
2428 fd = p->subs[index].zfd;
2430 if (ast_check_hangup(chan)) return -1;
2432 if (size > READ_SIZE)
2435 fds[0].events = POLLPRI | POLLOUT;
2436 res = poll(fds, 1, -1);
2438 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2441 /* if got exception */
2442 if (fds[0].revents & POLLPRI) return -1;
2443 if (!(fds[0].revents & POLLOUT)) {
2444 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2447 res = write(fd, buf, size);
2449 if (res == -1) return -1;
2450 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2456 p->didtdd = 1; /* set to have done it now */
2458 if (*cp == 2) { /* Mate mode */
2459 if (p->tdd) tdd_free(p->tdd);
2464 if (!p->tdd) { /* if we dont have one yet */
2465 p->tdd = tdd_new(); /* allocate one */
2468 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2471 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2476 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2479 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2481 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2484 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2490 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2493 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2494 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2501 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2503 /* Unlink a specific slave or all slaves/masters from a given master */
2509 ast_mutex_lock(&master->lock);
2511 while(ast_mutex_trylock(&slave->lock)) {
2512 ast_mutex_unlock(&master->lock);
2514 ast_mutex_lock(&master->lock);
2519 for (x=0;x<MAX_SLAVES;x++) {
2520 if (master->slaves[x]) {
2521 if (!slave || (master->slaves[x] == slave)) {
2522 /* Take slave out of the conference */
2523 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2524 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2525 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2526 master->slaves[x]->master = NULL;
2527 master->slaves[x] = NULL;
2532 master->inconference = 0;
2535 if (master->master) {
2536 /* Take master out of the conference */
2537 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2538 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2540 for (x=0;x<MAX_SLAVES;x++) {
2541 if (master->master->slaves[x] == master)
2542 master->master->slaves[x] = NULL;
2543 else if (master->master->slaves[x])
2547 master->master->inconference = 0;
2549 master->master = NULL;
2551 update_conf(master);
2554 ast_mutex_unlock(&slave->lock);
2555 ast_mutex_unlock(&master->lock);
2559 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2561 if (!slave || !master) {
2562 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2565 for (x=0;x<MAX_SLAVES;x++) {
2566 if (!master->slaves[x]) {
2567 master->slaves[x] = slave;
2571 if (x >= MAX_SLAVES) {
2572 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2573 master->slaves[MAX_SLAVES - 1] = slave;
2576 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2577 slave->master = master;
2579 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2582 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2584 struct ast_channel *who = NULL, *cs[3];
2585 struct zt_pvt *p0, *p1, *op0, *op1;
2586 struct zt_pvt *master=NULL, *slave=NULL;
2587 struct ast_frame *f;
2592 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2593 int os1 = -1, os2 = -1;
2594 struct ast_channel *oc1, *oc2;
2596 /* if need DTMF, cant native bridge */
2597 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2600 ast_mutex_lock(&c0->lock);
2601 ast_mutex_lock(&c1->lock);
2605 /* cant do pseudo-channels here */
2606 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2607 ast_mutex_unlock(&c0->lock);
2608 ast_mutex_unlock(&c1->lock);
2612 op0 = p0 = c0->pvt->pvt;
2613 op1 = p1 = c1->pvt->pvt;
2616 oi1 = zt_get_index(c0, p0, 0);
2617 oi2 = zt_get_index(c1, p1, 0);
2620 if ((oi1 < 0) || (oi2 < 0)) {
2621 ast_mutex_unlock(&c0->lock);
2622 ast_mutex_unlock(&c1->lock);
2628 ast_mutex_lock(&p0->lock);
2629 if (ast_mutex_trylock(&p1->lock)) {
2630 /* Don't block, due to potential for deadlock */
2631 ast_mutex_unlock(&p0->lock);
2632 ast_mutex_unlock(&c0->lock);
2633 ast_mutex_unlock(&c1->lock);
2634 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2637 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2638 if (!p0->owner || !p1->owner) {
2639 /* Currently unowned -- Do nothing. */
2642 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2643 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2647 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2652 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2653 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,
2654 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2657 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2658 if (p1->subs[SUB_THREEWAY].inthreeway) {
2664 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2665 if (p0->subs[SUB_THREEWAY].inthreeway) {
2671 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2672 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2673 don't put us in anything */
2674 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2680 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2681 /* Same as previous */
2682 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2689 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2690 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2691 if (master && slave) {
2692 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2693 in an active threeway call with a channel that is ringing, we should
2694 indicate ringing. */
2695 if ((oi2 == SUB_THREEWAY) &&
2696 p1->subs[SUB_THREEWAY].inthreeway &&
2697 p1->subs[SUB_REAL].owner &&
2698 p1->subs[SUB_REAL].inthreeway &&
2699 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2700 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2701 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2702 os2 = p1->subs[SUB_REAL].owner->_state;
2704 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2705 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2707 if ((oi1 == SUB_THREEWAY) &&
2708 p0->subs[SUB_THREEWAY].inthreeway &&
2709 p0->subs[SUB_REAL].owner &&
2710 p0->subs[SUB_REAL].inthreeway &&
2711 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2712 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2713 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2714 os1 = p0->subs[SUB_REAL].owner->_state;
2716 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2717 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2719 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2720 if (!p0->echocanbridged || !p1->echocanbridged) {
2721 /* Disable echo cancellation if appropriate */
2726 zt_link(slave, master);
2727 master->inconference = inconf;
2728 } else if (!nothingok)
2729 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2733 t1 = p0->subs[SUB_REAL].inthreeway;
2734 t2 = p1->subs[SUB_REAL].inthreeway;
2736 ast_mutex_unlock(&p0->lock);
2737 ast_mutex_unlock(&p1->lock);
2739 ast_mutex_unlock(&c0->lock);
2740 ast_mutex_unlock(&c1->lock);
2742 /* Native bridge failed */
2743 if ((!master || !slave) && !nothingok) {
2755 /* Here's our main loop... Start by locking things, looking for private parts,
2756 and then balking if anything is wrong */
2757 ast_mutex_lock(&c0->lock);
2758 ast_mutex_lock(&c1->lock);
2762 i1 = zt_get_index(c0, p0, 1);
2764 i2 = zt_get_index(c1, p1, 1);
2765 ast_mutex_unlock(&c0->lock);
2766 ast_mutex_unlock(&c1->lock);
2767 if ((op0 != p0) || (op1 != p1) ||
2768 (ofd1 != c0->fds[0]) ||
2769 (ofd2 != c1->fds[0]) ||
2770 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2771 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2772 (oc1 != p0->owner) ||
2773 (oc2 != p1->owner) ||
2774 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2775 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2778 if (slave && master)
2779 zt_unlink(slave, master, 1);
2780 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2781 op0->channel, oi1, op1->channel, oi2);
2789 who = ast_waitfor_n(cs, 2, &to);
2791 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2794 if (who->pvt->pvt == op0)
2795 op0->ignoredtmf = 1;
2796 else if (who->pvt->pvt == op1)
2797 op1->ignoredtmf = 1;
2799 if (who->pvt->pvt == op0)
2800 op0->ignoredtmf = 0;
2801 else if (who->pvt->pvt == op1)
2802 op1->ignoredtmf = 0;
2806 if (slave && master)
2807 zt_unlink(slave, master, 1);
2814 if (f->frametype == AST_FRAME_CONTROL) {
2817 if (slave && master)
2818 zt_unlink(slave, master, 1);
2821 if (f->frametype == AST_FRAME_DTMF) {
2822 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2823 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2826 if (slave && master)
2827 zt_unlink(slave, master, 1);
2829 } else if ((who == c0) && p0->pulsedial) {
2831 } else if ((who == c1) && p1->pulsedial) {
2837 /* Swap who gets priority */
2844 static int zt_indicate(struct ast_channel *chan, int condition);
2846 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2848 struct zt_pvt *p = newchan->pvt->pvt;
2850 ast_mutex_lock(&p->lock);
2851 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2852 if (p->owner == oldchan) {
2856 if (p->subs[x].owner == oldchan) {
2858 zt_unlink(NULL, p, 0);
2859 p->subs[x].owner = newchan;
2861 if (newchan->_state == AST_STATE_RINGING)
2862 zt_indicate(newchan, AST_CONTROL_RINGING);
2864 ast_mutex_unlock(&p->lock);
2868 static int zt_ring_phone(struct zt_pvt *p)
2872 /* Make sure our transmit state is on hook */
2875 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2878 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2880 printf("Res: %d, error: %s\n", res, strerror(errno));
2886 /* Wait just in case */
2893 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2901 static void *ss_thread(void *data);
2903 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2905 static int attempt_transfer(struct zt_pvt *p)
2907 /* In order to transfer, we need at least one of the channels to
2908 actually be in a call bridge. We can't conference two applications
2909 together (but then, why would we want to?) */
2910 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
2911 /* The three-way person we're about to transfer to could still be in MOH, so
2912 stop if now if appropriate */
2913 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2914 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
2915 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2916 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
2918 if (p->subs[SUB_REAL].owner->cdr) {
2919 /* Move CDR from second channel to current one */
2920 p->subs[SUB_THREEWAY].owner->cdr =
2921 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
2922 p->subs[SUB_REAL].owner->cdr = NULL;
2924 if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
2925 /* Move CDR from second channel's bridge to current one */
2926 p->subs[SUB_THREEWAY].owner->cdr =
2927 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
2928 ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
2930 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
2931 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2932 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
2935 /* Orphan the channel after releasing the lock */
2936 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
2937 unalloc_sub(p, SUB_THREEWAY);
2938 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2939 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2940 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
2942 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
2943 if (p->subs[SUB_THREEWAY].owner->cdr) {
2944 /* Move CDR from second channel to current one */
2945 p->subs[SUB_REAL].owner->cdr =
2946 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
2947 p->subs[SUB_THREEWAY].owner->cdr = NULL;
2949 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr) {
2950 /* Move CDR from second channel's bridge to current one */
2951 p->subs[SUB_REAL].owner->cdr =
2952 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr);
2953 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr = NULL;
2955 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
2956 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2957 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
2960 /* Three-way is now the REAL */
2961 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2962 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
2963 unalloc_sub(p, SUB_THREEWAY);
2964 /* Tell the caller not to hangup */
2967 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
2968 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
2969 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
2975 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
2977 struct ast_frame *f;
2978 f = &p->subs[index].f;
2980 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
2984 case MFCR2_EVENT_BLOCKED:
2985 if (option_verbose > 2)
2986 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
2988 case MFCR2_EVENT_UNBLOCKED:
2989 if (option_verbose > 2)
2990 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
2992 case MFCR2_EVENT_CONFIG_ERR:
2993 if (option_verbose > 2)
2994 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
2996 case MFCR2_EVENT_RING:
2997 if (option_verbose > 2)
2998 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
3000 case MFCR2_EVENT_HANGUP:
3001 if (option_verbose > 2)
3002 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
3004 case MFCR2_EVENT_RINGING:
3005 if (option_verbose > 2)
3006 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
3008 case MFCR2_EVENT_ANSWER:
3009 if (option_verbose > 2)
3010 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
3012 case MFCR2_EVENT_HANGUP_ACK:
3013 if (option_verbose > 2)
3014 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
3016 case MFCR2_EVENT_IDLE:
3017 if (option_verbose > 2)
3018 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
3021 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
3027 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
3032 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
3034 ast_log(LOG_WARNING, "Unable to check received bits\n");
3038 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
3041 e = mfcr2_cas_signaling_event(p->r2, x);
3046 static int check_for_conference(struct zt_pvt *p)
3049 /* Fine if we already have a master, etc */
3050 if (p->master || (p->confno > -1))
3052 memset(&ci, 0, sizeof(ci));
3053 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
3054 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
3057 /* If we have no master and don't have a confno, then
3058 if we're in a conference, it's probably a MeetMe room or
3059 some such, so don't let us 3-way out! */
3060 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
3061 if (option_verbose > 2)
3062 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
3068 static int get_alarms(struct zt_pvt *p)
3072 memset(&zi, 0, sizeof(zi));
3073 zi.spanno = p->span;
3074 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
3076 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
3082 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
3087 struct zt_pvt *p = ast->pvt->pvt;
3089 pthread_attr_t attr;
3090 struct ast_channel *chan;
3091 pthread_attr_init(&attr);
3092 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3093 index = zt_get_index(ast, p, 0);
3094 p->subs[index].f.frametype = AST_FRAME_NULL;
3095 p->subs[index].f.datalen = 0;
3096 p->subs[index].f.samples = 0;
3097 p->subs[index].f.mallocd = 0;
3098 p->subs[index].f.offset = 0;
3099 p->subs[index].f.src = "zt_handle_event";
3100 p->subs[index].f.data = NULL;
3102 return &p->subs[index].f;
3103 if (p->fake_event) {
3104 res = p->fake_event;
3107 res = zt_get_event(p->subs[index].zfd);
3108 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
3109 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
3110 if (res & ZT_EVENT_PULSEDIGIT)
3114 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
3115 p->subs[index].f.frametype = AST_FRAME_DTMF;
3116 p->subs[index].f.subclass = res & 0xff;
3117 /* Return the captured digit */
3118 return &p->subs[index].f;
3121 case ZT_EVENT_BITSCHANGED:
3122 if (p->sig == SIG_R2) {
3124 struct ast_frame *f = &p->subs[index].f;
3126 e = r2_get_event_bits(p);
3128 f = handle_r2_event(p, e, index);
3134 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
3135 case ZT_EVENT_PULSE_START:
3136 /* Stop tone if there's a pulse start and the PBX isn't started */
3138 tone_zone_play_tone(p->subs[index].zfd, -1);
3140 case ZT_EVENT_DIALCOMPLETE:
3141 if (p->inalarm) break;
3142 if (p->radio) break;
3143 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
3144 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
3147 if (!x) { /* if not still dialing in driver */
3151 strncpy(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr) - 1);
3152 p->dop.op = ZT_DIAL_OP_REPLACE;
3153 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
3157 if (p->sig == SIG_E911) {
3158 /* if thru with dialing after offhook */
3159 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
3160 ast_setstate(ast, AST_STATE_UP);
3161 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3162 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3164 } else { /* if to state wait for offhook to dial rest */
3165 /* we now wait for off hook */
3166 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
3169 if (ast->_state == AST_STATE_DIALING) {
3170 if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
3171 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
3172 } 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)))) {
3173 ast_setstate(ast, AST_STATE_RINGING);
3175 ast_setstate(ast, AST_STATE_UP);
3176 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3177 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3183 case ZT_EVENT_ALARM:
3186 if (p->pri && p->pri->pri) {
3187 if (!pri_grab(p, p->pri)) {
3188 pri_hangup(p->pri->pri, p->call, -1);
3189 pri_destroycall(p->pri->pri, p->call);
3192 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
3194 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
3197 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
3200 p->bearer->inalarm = 1;
3204 res = get_alarms(p);
3205 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
3206 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
3209 alarm2str(res), p->channel);
3210 /* fall through intentionally */
3211 case ZT_EVENT_ONHOOK:
3214 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3215 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
3222 p->onhooktime = time(NULL);
3224 /* Check for some special conditions regarding call waiting */
3225 if (index == SUB_REAL) {
3226 /* The normal line was hung up */
3227 if (p->subs[SUB_CALLWAIT].owner) {
3228 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
3229 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3230 if (option_verbose > 2)
3231 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
3232 unalloc_sub(p, SUB_CALLWAIT);
3234 p->subs[index].needanswer = 0;
3235 p->subs[index].needringing = 0;
3237 p->callwaitingrepeat = 0;
3240 /* Don't start streaming audio yet if the incoming call isn't up yet */
3241 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
3244 } else if (p->subs[SUB_THREEWAY].owner) {
3246 unsigned int mssinceflash;
3247 /* Here we have to retain the lock on both the main channel, the 3-way channel, and
3248 the private structure -- not especially easy or clean */
3249 while(p->subs[SUB_THREEWAY].owner && ast_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) {
3250 /* Yuck, didn't get the lock on the 3-way, gotta release everything and re-grab! */
3251 ast_mutex_unlock(&p->lock);
3252 ast_mutex_unlock(&ast->lock);
3254 /* We can grab ast and p in that order, without worry. We should make sure
3255 nothing seriously bad has happened though like some sort of bizarre double
3257 ast_mutex_lock(&ast->lock);
3258 ast_mutex_lock(&p->lock);
3259 if (p->owner != ast) {
3260 ast_log(LOG_WARNING, "This isn't good...\n");
3264 if (!p->subs[SUB_THREEWAY].owner) {
3265 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
3268 gettimeofday(&tv, NULL);
3269 mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
3270 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
3271 if (mssinceflash < MIN_MS_SINCE_FLASH) {
3272 /* It hasn't been long enough since the last flashook. This is probably a bounce on
3273 hanging up. Hangup both channels now */
3274 if (p->subs[SUB_THREEWAY].owner)
3275 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
3276 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3277 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
3278 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
3279 } else if ((ast->pbx) ||
3280 (ast->_state == AST_STATE_UP)) {
3282 /* In any case this isn't a threeway call anymore */
3283 p->subs[SUB_REAL].inthreeway = 0;
3284 p->subs[SUB_THREEWAY].inthreeway = 0;
3285 if ((res = attempt_transfer(p)) < 0)
3286 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3288 /* Don't actually hang up at this point */
3289 if (p->subs[SUB_THREEWAY].owner)
3290 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
3294 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3295 if (p->subs[SUB_THREEWAY].owner)
3296 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
3298 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
3299 /* Swap subs and dis-own channel */
3300 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3302 /* Ring the phone */
3307 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
3315 case ZT_EVENT_RINGOFFHOOK:
3316 if (p->inalarm) break;
3319 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3320 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
3323 /* for E911, its supposed to wait for offhook