2 * Asterisk -- A telephony toolkit for Linux.
4 * Zaptel Pseudo TDM interface
6 * Copyright (C) 2003 - 2005, 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>
49 #include <sys/ioctl.h>
51 #include <linux/zaptel.h>
54 #endif /* __linux__ */
60 #ifndef PRI_REDIRECTING_REASON
61 #error "You need newer libpri"
68 #include "../asterisk.h"
71 #error "Your zaptel is too old. please cvs update"
75 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
76 * the user hangs up to reset the state machine so ring works properly.
77 * This is used to be able to support kewlstart by putting the zhone in
78 * groundstart mode since their forward disconnect supervision is entirely
79 * broken even though their documentation says it isn't and their support
80 * is entirely unwilling to provide any assistance with their channel banks
81 * even though their web site says they support their products for life.
84 /* #define ZHONE_HACK */
87 * Define if you want to check the hook state for an FXO (FXS signalled) interface
88 * before dialing on it. Certain FXO interfaces always think they're out of
89 * service with this method however.
91 /* #define ZAP_CHECK_HOOKSTATE */
93 /* Typically, how many rings before we should send Caller*ID */
94 #define DEFAULT_CIDRINGS 1
96 #define CHANNEL_PSEUDO -12
98 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
100 static char *desc = "Zapata Telephony"
109 static char *tdesc = "Zapata Telephony Driver"
118 static char *type = "Zap";
119 static char *config = "zapata.conf";
121 #define SIG_EM ZT_SIG_EM
122 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
123 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
124 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
125 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
126 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
127 #define SIG_FXSLS ZT_SIG_FXSLS
128 #define SIG_FXSGS ZT_SIG_FXSGS
129 #define SIG_FXSKS ZT_SIG_FXSKS
130 #define SIG_FXOLS ZT_SIG_FXOLS
131 #define SIG_FXOGS ZT_SIG_FXOGS
132 #define SIG_FXOKS ZT_SIG_FXOKS
133 #define SIG_PRI ZT_SIG_CLEAR
134 #define SIG_R2 ZT_SIG_CAS
135 #define SIG_SF ZT_SIG_SF
136 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
137 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
138 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
139 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
140 #define SIG_EM_E1 ZT_SIG_EM_E1
141 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
142 #define SIG_GR303FXSKS (0x200000 | ZT_SIG_FXSKS)
145 #define NUM_DCHANS 4 /* No more than 4 d-channels */
146 #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
148 #define CHAN_PSEUDO -2
150 #define DCHAN_PROVISIONED (1 << 0)
151 #define DCHAN_NOTINALARM (1 << 1)
152 #define DCHAN_UP (1 << 2)
154 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
156 static int cur_emdigitwait = 250; /* Wait time in ms for digits on EM channel */
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 ast_group_t cur_group = 0;
175 static ast_group_t cur_callergroup = 0;
176 static ast_group_t 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 char internationalprefix[10] = "";
251 static char nationalprefix[10] = "";
252 static char localprefix[20] = "";
253 static char privateprefix[20] = "";
254 static char unknownprefix[20] = "";
255 static long resetinterval = 3600; /* How often (in seconds) to reset unused channels. Default 1 hour. */
256 static struct ast_channel inuse = { "GR-303InUse" };
257 #ifdef PRI_GETSET_TIMERS
258 static int pritimers[PRI_MAX_TIMERS];
262 /* Wait up to 16 seconds for first digit (FXO logic) */
263 static int firstdigittimeout = 16000;
265 /* How long to wait for following digits (FXO logic) */
266 static int gendigittimeout = 8000;
268 /* How long to wait for an extra digit, if there is an ambiguous match */
269 static int matchdigittimeout = 3000;
271 static int usecnt =0;
272 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
274 /* Protect the interface list (of zt_pvt's) */
275 AST_MUTEX_DEFINE_STATIC(iflock);
277 static int ifcount = 0;
279 /* Whether we answer on a Polarity Switch event */
280 static int answeronpolarityswitch = 0;
282 /* Whether we hang up on a Polarity Switch event */
283 static int hanguponpolarityswitch = 0;
285 /* How long (ms) to ignore Polarity Switch events after we answer a call */
286 static int polarityonanswerdelay = 600;
288 /* When to send the CallerID signals (rings) */
289 static int sendcalleridafter = DEFAULT_CIDRINGS;
291 /* Protect the monitoring thread, so only one process can kill or start it, and not
292 when it's doing something critical. */
293 AST_MUTEX_DEFINE_STATIC(monlock);
295 /* This is the thread for the monitor which checks for input on the channels
296 which are not currently in use. */
297 static pthread_t monitor_thread = AST_PTHREADT_NULL;
299 static int restart_monitor(void);
301 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
303 static int zt_sendtext(struct ast_channel *c, char *text);
305 static inline int zt_get_event(int fd)
307 /* Avoid the silly zt_getevent which ignores a bunch of events */
309 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
313 static inline int zt_wait_event(int fd)
315 /* Avoid the silly zt_waitevent which ignores a bunch of events */
317 i = ZT_IOMUX_SIGEVENT;
318 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
319 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
323 /* Chunk size to read -- we use 20ms chunks to make things happy. */
324 #define READ_SIZE 160
326 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
327 #define MASK_INUSE (1 << 1) /* Channel currently in use */
329 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
330 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
331 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
332 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
333 #define RINGT ( (8000 * 8) / READ_SIZE)
339 static int r2prot = -1;
345 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8))
346 #define PRI_CHANNEL(p) ((p) & 0xff)
347 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
350 pthread_t master; /* Thread of master */
351 ast_mutex_t lock; /* Mutex */
352 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
353 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
354 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
355 int minunused; /* Min # of channels to keep empty */
356 int minidle; /* Min # of "idling" calls to keep active */
357 int nodetype; /* Node type */
358 int switchtype; /* Type of switch to emulate */
359 int nsf; /* Network-Specific Facilities */
360 int dialplan; /* Dialing plan */
361 int localdialplan; /* Local dialing plan */
362 char internationalprefix[10]; /* country access code ('00' for european dialplans) */
363 char nationalprefix[10]; /* area access code ('0' for european dialplans) */
364 char localprefix[20]; /* area access code + area code ('0'+area code for european dialplans) */
365 char privateprefix[20]; /* for private dialplans */
366 char unknownprefix[20]; /* for unknown dialplans */
367 int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
368 int trunkgroup; /* What our trunkgroup is */
369 int mastertrunkgroup; /* What trunk group is our master */
370 int prilogicalspan; /* Logical span number within trunk group */
371 int numchans; /* Num of channels we represent */
372 int overlapdial; /* In overlap dialing mode */
373 struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
374 int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
375 struct pri *pri; /* Currently active D-channel */
377 int fds[NUM_DCHANS]; /* FD's for d-channels */
382 time_t lastreset; /* time when unused channels were last reset */
383 long resetinterval; /* Interval (in seconds) for resetting unused channels */
384 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
385 struct zt_pvt *crvs; /* Member CRV structs */
386 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
390 static struct zt_pri pris[NUM_SPANS];
392 static int pritype = PRI_CPE;
395 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
397 #define DEFAULT_PRI_DEBUG 0
400 static inline void pri_rel(struct zt_pri *pri)
402 ast_mutex_unlock(&pri->lock);
405 static int switchtype = PRI_SWITCH_NI2;
406 static int nsf = PRI_NSF_NONE;
407 static int dialplan = PRI_NATIONAL_ISDN + 1;
408 static int localdialplan = PRI_NATIONAL_ISDN + 1;
411 /* Shut up the compiler */
415 #define SUB_REAL 0 /* Active call */
416 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
417 #define SUB_THREEWAY 2 /* Three-way call */
419 /* Polarity states */
420 #define POLARITY_IDLE 0
421 #define POLARITY_REV 1
424 static struct zt_distRings drings;
426 struct distRingData {
429 struct ringContextData {
430 char contextData[AST_MAX_EXTENSION];
432 struct zt_distRings {
433 struct distRingData ringnum[3];
434 struct ringContextData ringContext[3];
437 static char *subnames[] = {
443 struct zt_subchannel {
445 struct ast_channel *owner;
447 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
448 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
460 #define CONF_USER_REAL (1 << 0)
461 #define CONF_USER_THIRDCALL (1 << 1)
465 static struct zt_pvt {
467 struct ast_channel *owner; /* Our current active owner (if applicable) */
468 /* Up to three channels can be associated with this call */
470 struct zt_subchannel sub_unused; /* Just a safety precaution */
471 struct zt_subchannel subs[3]; /* Sub-channels */
472 struct zt_confinfo saveconf; /* Saved conference info */
474 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
475 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
476 int inconference; /* If our real should be in the conference */
478 int sig; /* Signalling style */
479 int radio; /* radio type */
480 int firstradio; /* first radio flag */
483 int tonezone; /* tone zone for this chan, or -1 for default */
484 struct zt_pvt *next; /* Next channel in list */
485 struct zt_pvt *prev; /* Prev channel in list */
487 struct zt_distRings drings;
488 int usedistinctiveringdetection;
490 char context[AST_MAX_EXTENSION];
491 char defcontext[AST_MAX_EXTENSION];
492 char exten[AST_MAX_EXTENSION];
493 char language[MAX_LANGUAGE];
494 char musicclass[MAX_LANGUAGE];
495 char cid_num[AST_MAX_EXTENSION];
496 int cid_ton; /* Type Of Number (TON) */
497 char cid_name[AST_MAX_EXTENSION];
498 char lastcid_num[AST_MAX_EXTENSION];
499 char lastcid_name[AST_MAX_EXTENSION];
500 char *origcid_num; /* malloced original callerid */
501 char *origcid_name; /* malloced original callerid */
502 char callwait_num[AST_MAX_EXTENSION];
503 char callwait_name[AST_MAX_EXTENSION];
504 char rdnis[AST_MAX_EXTENSION];
505 char dnid[AST_MAX_EXTENSION];
508 int confno; /* Our conference */
509 int confusers; /* Who is using our conference */
510 int propconfno; /* Propagated conference number */
511 ast_group_t callgroup;
512 ast_group_t pickupgroup;
513 int immediate; /* Answer before getting digits? */
514 int channel; /* Channel Number or CRV */
515 int span; /* Span number */
517 time_t guardtime; /* Must wait this much time before using for new call */
519 int use_callerid; /* Whether or not to use caller id on this channel */
520 int cid_signalling; /* CID signalling type bell202 or v23 */
521 int cid_start; /* CID start indicator, polarity or ring */
524 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
525 int restrictcid; /* Whether restrict the callerid -> only send ANI */
526 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
527 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
528 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
529 int cidcwexpire; /* When to expire our muting for CID/CW */
530 unsigned char *cidspill;
546 int callwaitingcallerid;
556 int priindication_oob;
557 struct timeval flashtime; /* Last flash-hook time */
559 int cref; /* Call reference number */
560 ZT_DIAL_OPERATION dop;
564 char accountcode[20]; /* Account code */
565 int amaflags; /* AMA Flags */
566 char didtdd; /* flag to say its done it once */
567 struct tdd_state *tdd; /* TDD flag */
570 char call_forward[AST_MAX_EXTENSION];
571 char mailbox[AST_MAX_EXTENSION];
576 int confirmanswer; /* Wait for '#' to confirm answer */
577 int distinctivering; /* Which distinctivering to use */
578 int cidrings; /* Which ring to deliver CID on */
580 int faxhandled; /* Has a fax tone already been handled? */
582 char mate; /* flag to say its in MATE mode */
583 int pulsedial; /* whether a pulse dial phone is detected */
584 int dtmfrelax; /* whether to run in relaxed DTMF mode */
586 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
588 int answeronpolarityswitch;
589 int hanguponpolarityswitch;
590 int polarityonanswerdelay;
591 struct timeval polaritydelaytv;
592 int sendcalleridafter;
595 struct zt_pvt *bearer;
596 struct zt_pvt *realcall;
604 int setup_ack; /* whether we received SETUP_ACKNOWLEDGE or not */
615 } *iflist = NULL, *ifend = NULL;
618 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
620 #define GET_CHANNEL(p) ((p)->channel)
623 struct zt_pvt *round_robin[32];
626 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
629 /* Grab the lock first */
631 res = ast_mutex_trylock(&pri->lock);
633 ast_mutex_unlock(&pvt->lock);
634 /* Release the lock and try again */
636 ast_mutex_lock(&pvt->lock);
639 /* Then break the poll */
640 pthread_kill(pri->master, SIGURG);
645 #define NUM_CADENCE_MAX 25
646 static int num_cadence = 4;
647 static int user_has_defined_cadences = 0;
649 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
650 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
651 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
652 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
653 { { 1000, 500, 2500, 5000 } }, /* Long ring */
656 int receivedRingT; /* Used to find out what ringtone we are on */
658 /* cidrings says in which pause to transmit the cid information, where the first pause
659 * is 1, the second pause is 2 and so on.
662 static int cidrings[NUM_CADENCE_MAX] = {
663 2, /* Right after first long ring */
664 4, /* Right after long part */
665 3, /* After third chirp */
666 2, /* Second spell */
669 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
670 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
672 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
673 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
675 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
678 if (p->subs[0].owner == ast)
680 else if (p->subs[1].owner == ast)
682 else if (p->subs[2].owner == ast)
687 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
693 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
695 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
698 struct ast_frame null = { AST_FRAME_NULL, };
701 ast_mutex_unlock(&pri->lock);
704 if (p->subs[a].owner) {
705 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
706 ast_mutex_unlock(&p->lock);
708 ast_mutex_lock(&p->lock);
710 ast_queue_frame(p->subs[a].owner, &null);
711 ast_mutex_unlock(&p->subs[a].owner->lock);
719 ast_mutex_lock(&pri->lock);
724 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
726 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
729 /* We must unlock the PRI to avoid the possibility of a deadlock */
732 ast_mutex_unlock(&pri->lock);
736 if (ast_mutex_trylock(&p->owner->lock)) {
737 ast_mutex_unlock(&p->lock);
739 ast_mutex_lock(&p->lock);
741 ast_queue_frame(p->owner, f);
742 ast_mutex_unlock(&p->owner->lock);
750 ast_mutex_lock(&pri->lock);
754 static void swap_subs(struct zt_pvt *p, int a, int b)
758 struct ast_channel *towner;
760 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
762 tchan = p->subs[a].chan;
763 towner = p->subs[a].owner;
764 tinthreeway = p->subs[a].inthreeway;
766 p->subs[a].chan = p->subs[b].chan;
767 p->subs[a].owner = p->subs[b].owner;
768 p->subs[a].inthreeway = p->subs[b].inthreeway;
770 p->subs[b].chan = tchan;
771 p->subs[b].owner = towner;
772 p->subs[b].inthreeway = tinthreeway;
774 if (p->subs[a].owner)
775 p->subs[a].owner->fds[0] = p->subs[a].zfd;
776 if (p->subs[b].owner)
777 p->subs[b].owner->fds[0] = p->subs[b].zfd;
778 wakeup_sub(p, a, NULL);
779 wakeup_sub(p, b, NULL);
782 static int zt_open(char *fn)
790 for (x=0;x<strlen(fn);x++) {
791 if (!isdigit(fn[x])) {
799 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
802 fn = "/dev/zap/channel";
804 fd = open(fn, O_RDWR | O_NONBLOCK);
806 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
810 if (ioctl(fd, ZT_SPECIFY, &chan)) {
814 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
819 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
823 static void zt_close(int fd)
829 int zt_setlinear(int zfd, int linear)
832 res = ioctl(zfd, ZT_SETLINEAR, &linear);
839 int zt_setlaw(int zfd, int law)
842 res = ioctl(zfd, ZT_SETLAW, &law);
848 static int alloc_sub(struct zt_pvt *p, int x)
852 if (p->subs[x].zfd < 0) {
853 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
854 if (p->subs[x].zfd > -1) {
855 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
857 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
858 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
859 bi.numbufs = numbufs;
860 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
862 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
865 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
866 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
867 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
868 zt_close(p->subs[x].zfd);
873 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
876 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
879 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
883 static int unalloc_sub(struct zt_pvt *p, int x)
886 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
889 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
890 if (p->subs[x].zfd > -1) {
891 zt_close(p->subs[x].zfd);
894 p->subs[x].linear = 0;
896 p->subs[x].owner = NULL;
897 p->subs[x].inthreeway = 0;
898 p->polarity = POLARITY_IDLE;
899 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
903 static int zt_digit(struct ast_channel *ast, char digit)
905 ZT_DIAL_OPERATION zo;
910 ast_mutex_lock(&p->lock);
911 index = zt_get_index(ast, p, 0);
912 if ((index == SUB_REAL) && p->owner) {
914 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
916 if (!pri_grab(p, p->pri)) {
917 pri_information(p->pri->pri,p->call,digit);
920 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
921 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
922 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
923 res = strlen(p->dialdest);
924 p->dialdest[res++] = digit;
925 p->dialdest[res] = '\0';
931 zo.op = ZT_DIAL_OP_APPEND;
933 zo.dialstr[1] = digit;
935 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
936 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
941 ast_mutex_unlock(&p->lock);
945 static char *events[] = {
958 "Hook Transition Complete",
970 { ZT_ALARM_RED, "Red Alarm" },
971 { ZT_ALARM_YELLOW, "Yellow Alarm" },
972 { ZT_ALARM_BLUE, "Blue Alarm" },
973 { ZT_ALARM_RECOVER, "Recovering" },
974 { ZT_ALARM_LOOPBACK, "Loopback" },
975 { ZT_ALARM_NOTOPEN, "Not Open" },
976 { ZT_ALARM_NONE, "None" },
979 static char *alarm2str(int alarm)
982 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
983 if (alarms[x].alarm & alarm)
984 return alarms[x].name;
986 return alarm ? "Unknown Alarm" : "No Alarm";
989 static char *event2str(int event)
991 static char buf[256];
992 if ((event < 18) && (event > -1))
993 return events[event];
994 sprintf(buf, "Event %d", event); /* safe */
999 static int str2r2prot(char *swtype)
1001 if (!strcasecmp(swtype, "ar"))
1002 return MFCR2_PROT_ARGENTINA;
1004 if (!strcasecmp(swtype, "cn"))
1005 return MFCR2_PROT_CHINA;
1007 if (!strcasecmp(swtype, "kr"))
1008 return MFCR2_PROT_KOREA;
1014 static char *sig2str(int sig)
1016 static char buf[256];
1019 return "E & M Immediate";
1021 return "E & M Wink";
1025 return "Feature Group D (DTMF)";
1027 return "Feature Group D (MF)";
1029 return "Feature Group B (MF)";
1033 return "FXS Loopstart";
1035 return "FXS Groundstart";
1037 return "FXS Kewlstart";
1039 return "FXO Loopstart";
1041 return "FXO Groundstart";
1043 return "FXO Kewlstart";
1045 return "PRI Signalling";
1047 return "R2 Signalling";
1049 return "SF (Tone) Signalling Immediate";
1051 return "SF (Tone) Signalling Wink";
1053 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1054 case SIG_SF_FEATDMF:
1055 return "SF (Tone) Signalling with Feature Group D (MF)";
1057 return "SF (Tone) Signalling with Feature Group B (MF)";
1058 case SIG_GR303FXOKS:
1059 return "GR-303 Signalling with FXOKS";
1060 case SIG_GR303FXSKS:
1061 return "GR-303 Signalling with FXSKS";
1063 return "Pseudo Signalling";
1065 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1070 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1072 /* If the conference already exists, and we're already in it
1073 don't bother doing anything */
1076 memset(&zi, 0, sizeof(zi));
1079 if (slavechannel > 0) {
1080 /* If we have only one slave, do a digital mon */
1081 zi.confmode = ZT_CONF_DIGITALMON;
1082 zi.confno = slavechannel;
1085 /* Real-side and pseudo-side both participate in conference */
1086 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1087 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1089 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1090 zi.confno = p->confno;
1092 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1096 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1097 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1100 if (slavechannel < 1) {
1101 p->confno = zi.confno;
1103 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1104 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1108 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1110 /* If they're listening to our channel, they're ours */
1111 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1113 /* If they're a talker on our (allocated) conference, they're ours */
1114 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1119 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1122 if (/* Can't delete if there's no zfd */
1124 /* Don't delete from the conference if it's not our conference */
1126 /* Don't delete if we don't think it's conferenced at all (implied) */
1128 memset(&zi, 0, sizeof(zi));
1132 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1133 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1136 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1137 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1141 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1145 struct zt_pvt *slave = NULL;
1146 /* Start out optimistic */
1148 /* Update conference state in a stateless fashion */
1150 /* Any three-way calling makes slave native mode *definitely* out
1152 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1155 /* If we don't have any 3-way calls, check to see if we have
1156 precisely one slave */
1157 if (useslavenative) {
1158 for (x=0;x<MAX_SLAVES;x++) {
1161 /* Whoops already have a slave! No
1162 slave native and stop right away */
1167 /* We have one slave so far */
1168 slave = p->slaves[x];
1173 /* If no slave, slave native definitely out */
1176 else if (slave->law != p->law) {
1182 return useslavenative;
1185 static int reset_conf(struct zt_pvt *p)
1188 memset(&zi, 0, sizeof(zi));
1190 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1191 if (p->subs[SUB_REAL].zfd > -1) {
1192 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1193 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1198 static int update_conf(struct zt_pvt *p)
1203 struct zt_pvt *slave = NULL;
1205 useslavenative = isslavenative(p, &slave);
1206 /* Start with the obvious, general stuff */
1208 /* Look for three way calls */
1209 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1210 conf_add(p, &p->subs[x], x, 0);
1213 conf_del(p, &p->subs[x], x);
1216 /* If we have a slave, add him to our conference now. or DAX
1217 if this is slave native */
1218 for (x=0;x<MAX_SLAVES;x++) {
1221 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1223 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1228 /* If we're supposed to be in there, do so now */
1229 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1231 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1233 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1237 /* If we have a master, add ourselves to his conference */
1239 if (isslavenative(p->master, NULL)) {
1240 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1242 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1246 /* Nobody is left (or should be left) in our conference.
1250 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1254 static void zt_enable_ec(struct zt_pvt *p)
1261 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1265 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1268 if (p->echocancel) {
1269 if (p->sig == SIG_PRI) {
1271 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1273 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1276 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1278 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1281 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1284 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1287 static void zt_train_ec(struct zt_pvt *p)
1291 if (p && p->echocancel && p->echotraining) {
1292 x = p->echotraining;
1293 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1295 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1297 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1300 ast_log(LOG_DEBUG, "No echo training requested\n");
1303 static void zt_disable_ec(struct zt_pvt *p)
1307 if (p->echocancel) {
1309 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1311 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1313 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1318 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1325 if ((rxgain != 0.0) || (txgain != 0.0)) {
1326 /* caluculate linear value of tx gain */
1327 ltxgain = pow(10.0,txgain / 20.0);
1328 /* caluculate linear value of rx gain */
1329 lrxgain = pow(10.0,rxgain / 20.0);
1330 if (law == ZT_LAW_ALAW) {
1331 for (j=0;j<256;j++) {
1332 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1333 if (k > 32767) k = 32767;
1334 if (k < -32767) k = -32767;
1335 g.rxgain[j] = AST_LIN2A(k);
1336 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1337 if (k > 32767) k = 32767;
1338 if (k < -32767) k = -32767;
1339 g.txgain[j] = AST_LIN2A(k);
1342 for (j=0;j<256;j++) {
1343 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1344 if (k > 32767) k = 32767;
1345 if (k < -32767) k = -32767;
1346 g.rxgain[j] = AST_LIN2MU(k);
1347 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1348 if (k > 32767) k = 32767;
1349 if (k < -32767) k = -32767;
1350 g.txgain[j] = AST_LIN2MU(k);
1354 for (j=0;j<256;j++) {
1361 return(ioctl(fd,ZT_SETGAINS,&g));
1364 static inline int zt_set_hook(int fd, int hs)
1368 res = ioctl(fd, ZT_HOOK, &x);
1371 if (errno == EINPROGRESS) return 0;
1372 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1377 static inline int zt_confmute(struct zt_pvt *p, int muted)
1381 if (p->sig == SIG_PRI) {
1383 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1385 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1387 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1389 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1393 static int save_conference(struct zt_pvt *p)
1395 struct zt_confinfo c;
1397 if (p->saveconf.confmode) {
1398 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1401 p->saveconf.chan = 0;
1402 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1404 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1405 p->saveconf.confmode = 0;
1410 c.confmode = ZT_CONF_NORMAL;
1411 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1413 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1417 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1421 static int restore_conference(struct zt_pvt *p)
1424 if (p->saveconf.confmode) {
1425 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1426 p->saveconf.confmode = 0;
1428 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1433 ast_log(LOG_DEBUG, "Restored conferencing\n");
1437 static int send_callerid(struct zt_pvt *p);
1439 int send_cwcidspill(struct zt_pvt *p)
1443 p->cidspill = malloc(MAX_CALLERID_SIZE);
1445 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1446 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1447 /* Make sure we account for the end */
1448 p->cidlen += READ_SIZE * 4;
1451 if (option_verbose > 2)
1452 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1457 static int has_voicemail(struct zt_pvt *p)
1460 return ast_app_has_voicemail(p->mailbox, NULL);
1463 static int send_callerid(struct zt_pvt *p)
1465 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1467 /* Take out of linear mode if necessary */
1468 if (p->subs[SUB_REAL].linear) {
1469 p->subs[SUB_REAL].linear = 0;
1470 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1472 while(p->cidpos < p->cidlen) {
1473 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1475 if (errno == EAGAIN)
1478 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1488 if (p->callwaitcas) {
1489 /* Wait for CID/CW to expire */
1490 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1492 restore_conference(p);
1496 static int zt_callwait(struct ast_channel *ast)
1498 struct zt_pvt *p = ast->pvt->pvt;
1499 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1501 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1504 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1508 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1509 if (!p->callwaitrings && p->callwaitingcallerid) {
1510 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1512 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1514 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1516 p->cidlen = 2400 + READ_SIZE * 4;
1521 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1527 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1529 struct zt_pvt *p = ast->pvt->pvt;
1535 char dest[256]; /* must be same length as p->dialdest */
1536 ast_mutex_lock(&p->lock);
1537 strncpy(dest, rdest, sizeof(dest) - 1);
1538 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1539 if ((ast->_state == AST_STATE_BUSY)) {
1540 p->subs[SUB_REAL].needbusy = 1;
1541 ast_mutex_unlock(&p->lock);
1544 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1545 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1546 ast_mutex_unlock(&p->lock);
1550 if (p->radio) /* if a radio channel, up immediately */
1552 /* Special pseudo -- automatically up */
1553 ast_setstate(ast, AST_STATE_UP);
1554 ast_mutex_unlock(&p->lock);
1557 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1558 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1560 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1563 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1569 if (p->owner == ast) {
1570 /* Normal ring, on hook */
1572 /* Don't send audio while on hook, until the call is answered */
1574 if (p->use_callerid) {
1575 /* Generate the Caller-ID spill if desired */
1577 ast_log(LOG_WARNING, "cidspill already exists??\n");
1580 p->cidspill = malloc(MAX_CALLERID_SIZE);
1583 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1587 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1589 /* Choose proper cadence */
1590 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1591 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1592 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1593 p->cidrings = cidrings[p->distinctivering - 1];
1595 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1596 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1597 p->cidrings = p->sendcalleridafter;
1601 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1602 c = strchr(dest, '/');
1605 if (c && (strlen(c) < p->stripmsd)) {
1606 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1610 p->dop.op = ZT_DIAL_OP_REPLACE;
1611 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1612 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1614 p->dop.dialstr[0] = '\0';
1617 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1618 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1619 ast_mutex_unlock(&p->lock);
1624 /* Call waiting call */
1625 p->callwaitrings = 0;
1626 if (ast->cid.cid_num)
1627 strncpy(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num)-1);
1629 p->callwait_num[0] = '\0';
1630 if (ast->cid.cid_name)
1631 strncpy(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name)-1);
1633 p->callwait_name[0] = '\0';
1634 /* Call waiting tone instead */
1635 if (zt_callwait(ast)) {
1636 ast_mutex_unlock(&p->lock);
1639 /* Make ring-back */
1640 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1641 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1644 n = ast->cid.cid_name;
1645 l = ast->cid.cid_num;
1647 strncpy(p->lastcid_num, l, sizeof(p->lastcid_num) - 1);
1649 p->lastcid_num[0] = '\0';
1651 strncpy(p->lastcid_name, n, sizeof(p->lastcid_name) - 1);
1653 p->lastcid_name[0] = '\0';
1654 ast_setstate(ast, AST_STATE_RINGING);
1655 index = zt_get_index(ast, p, 0);
1657 p->subs[index].needringing = 1;
1673 case SIG_SF_FEATDMF:
1675 c = strchr(dest, '/');
1680 if (strlen(c) < p->stripmsd) {
1681 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1682 ast_mutex_unlock(&p->lock);
1686 /* Start the trunk, if not GR-303 */
1690 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1692 if (errno != EINPROGRESS) {
1693 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1694 ast_mutex_unlock(&p->lock);
1701 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1702 p->dop.op = ZT_DIAL_OP_REPLACE;
1703 if (p->sig == SIG_FEATD) {
1704 l = ast->cid.cid_num;
1706 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1708 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1710 if (p->sig == SIG_FEATDMF) {
1711 l = ast->cid.cid_num;
1713 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1715 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1717 if (p->sig == SIG_E911) {
1718 strncpy(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr) - 1);
1720 if (p->sig == SIG_FEATB) {
1721 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1724 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c + p->stripmsd);
1726 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1727 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1728 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1729 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1730 p->echorest[sizeof(p->echorest) - 1] = '\0';
1732 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1736 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1738 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1739 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1740 ast_mutex_unlock(&p->lock);
1744 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1746 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1747 ast_setstate(ast, AST_STATE_DIALING);
1750 /* Special pseudo -- automatically up*/
1751 ast_setstate(ast, AST_STATE_UP);
1754 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1755 p->dialdest[0] = '\0';
1758 ast_log(LOG_DEBUG, "not yet implemented\n");
1759 ast_mutex_unlock(&p->lock);
1765 c = strchr(dest, '/');
1770 if (!p->hidecallerid) {
1771 l = ast->cid.cid_num;
1772 n = ast->cid.cid_name;
1777 if (strlen(c) < p->stripmsd) {
1778 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1779 ast_mutex_unlock(&p->lock);
1782 if (p->sig != SIG_FXSKS) {
1783 p->dop.op = ZT_DIAL_OP_REPLACE;
1784 s = strchr(c + p->stripmsd, 'w');
1787 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1789 p->dop.dialstr[0] = '\0';
1792 p->dop.dialstr[0] = '\0';
1795 if (pri_grab(p, p->pri)) {
1796 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1797 ast_mutex_unlock(&p->lock);
1800 if (!(p->call = pri_new_call(p->pri->pri))) {
1801 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1803 ast_mutex_unlock(&p->lock);
1806 if (!(sr = pri_sr_new())) {
1807 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1809 ast_mutex_unlock(&p->lock);
1811 if (p->bearer || (p->sig == SIG_FXSKS)) {
1813 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);
1814 p->bearer->call = p->call;
1816 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
1817 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1819 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1820 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1821 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1822 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1824 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1825 pri_sr_set_called(sr, c + p->stripmsd, p->pri->dialplan - 1, s ? 1 : 0);
1826 pri_sr_set_caller(sr, l, n, p->pri->localdialplan - 1,
1827 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
1828 PRES_NUMBER_NOT_AVAILABLE);
1829 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
1830 if (pri_setup(p->pri->pri, p->call, sr)) {
1831 ast_log(LOG_WARNING, "Unable to setup call to %s\n", c + p->stripmsd);
1833 ast_mutex_unlock(&p->lock);
1838 ast_setstate(ast, AST_STATE_DIALING);
1842 ast_mutex_unlock(&p->lock);
1846 static void destroy_zt_pvt(struct zt_pvt **pvt)
1848 struct zt_pvt *p = *pvt;
1849 /* Remove channel from the list */
1851 p->prev->next = p->next;
1853 p->next->prev = p->prev;
1854 ast_mutex_destroy(&p->lock);
1859 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1869 for (i = 0; i < 3; i++) {
1870 if (cur->subs[i].owner) {
1876 prev->next = cur->next;
1878 prev->next->prev = prev;
1884 iflist->prev = NULL;
1888 if (cur->subs[SUB_REAL].zfd > -1) {
1889 zt_close(cur->subs[SUB_REAL].zfd);
1891 destroy_zt_pvt(&cur);
1895 prev->next = cur->next;
1897 prev->next->prev = prev;
1903 iflist->prev = NULL;
1907 if (cur->subs[SUB_REAL].zfd > -1) {
1908 zt_close(cur->subs[SUB_REAL].zfd);
1910 destroy_zt_pvt(&cur);
1916 int pri_is_up(struct zt_pri *pri)
1919 for (x=0;x<NUM_DCHANS;x++) {
1920 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1926 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
1928 bearer->owner = &inuse;
1929 bearer->realcall = crv;
1930 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
1931 if (crv->subs[SUB_REAL].owner)
1932 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
1933 crv->bearer = bearer;
1934 crv->call = bearer->call;
1939 static char *pri_order(int level)
1949 return "Quaternary";
1955 int pri_find_dchan(struct zt_pri *pri)
1962 for(x=0;x<NUM_DCHANS;x++) {
1963 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
1965 if (pri->dchans[x] == old) {
1971 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
1972 pri->dchannels[newslot]);
1974 if (old && (oldslot != newslot))
1975 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
1976 pri->dchannels[oldslot], pri->dchannels[newslot]);
1977 pri->pri = pri->dchans[newslot];
1982 static int zt_hangup(struct ast_channel *ast)
1986 static int restore_gains(struct zt_pvt *p);
1987 struct zt_pvt *p = ast->pvt->pvt;
1988 struct zt_pvt *tmp = NULL;
1989 struct zt_pvt *prev = NULL;
1993 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
1994 if (!ast->pvt->pvt) {
1995 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
1999 ast_mutex_lock(&p->lock);
2001 index = zt_get_index(ast, p, 1);
2003 if (p->sig == SIG_PRI) {
2005 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2011 if (p->origcid_num) {
2012 strncpy(p->cid_num, p->origcid_num, sizeof(p->cid_num) - 1);
2013 free(p->origcid_num);
2014 p->origcid_num = NULL;
2016 if (p->origcid_name) {
2017 strncpy(p->cid_name, p->origcid_name, sizeof(p->cid_name) - 1);
2018 free(p->origcid_name);
2019 p->origcid_name = NULL;
2022 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2026 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2027 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2031 /* Real channel, do some fixup */
2032 p->subs[index].owner = NULL;
2033 p->subs[index].needanswer = 0;
2034 p->subs[index].needflash = 0;
2035 p->subs[index].needringing = 0;
2036 p->subs[index].needbusy = 0;
2037 p->subs[index].needcongestion = 0;
2038 p->subs[index].linear = 0;
2039 p->subs[index].needcallerid = 0;
2040 p->polarity = POLARITY_IDLE;
2041 zt_setlinear(p->subs[index].zfd, 0);
2042 if (index == SUB_REAL) {
2043 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2044 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2045 if (p->subs[SUB_CALLWAIT].inthreeway) {
2046 /* We had flipped over to answer a callwait and now it's gone */
2047 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2048 /* Move to the call-wait, but un-own us until they flip back. */
2049 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2050 unalloc_sub(p, SUB_CALLWAIT);
2053 /* The three way hung up, but we still have a call wait */
2054 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2055 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2056 unalloc_sub(p, SUB_THREEWAY);
2057 if (p->subs[SUB_REAL].inthreeway) {
2058 /* This was part of a three way call. Immediately make way for
2060 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2061 p->owner = p->subs[SUB_REAL].owner;
2063 /* This call hasn't been completed yet... Set owner to NULL */
2064 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2067 p->subs[SUB_REAL].inthreeway = 0;
2069 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2070 /* Move to the call-wait and switch back to them. */
2071 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2072 unalloc_sub(p, SUB_CALLWAIT);
2073 p->owner = p->subs[SUB_REAL].owner;
2074 if (p->owner->_state != AST_STATE_UP)
2075 p->subs[SUB_REAL].needanswer = 1;
2076 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2077 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2078 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2079 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2080 unalloc_sub(p, SUB_THREEWAY);
2081 if (p->subs[SUB_REAL].inthreeway) {
2082 /* This was part of a three way call. Immediately make way for
2084 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2085 p->owner = p->subs[SUB_REAL].owner;
2087 /* This call hasn't been completed yet... Set owner to NULL */
2088 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2091 p->subs[SUB_REAL].inthreeway = 0;
2093 } else if (index == SUB_CALLWAIT) {
2094 /* Ditch the holding callwait call, and immediately make it availabe */
2095 if (p->subs[SUB_CALLWAIT].inthreeway) {
2096 /* This is actually part of a three way, placed on hold. Place the third part
2097 on music on hold now */
2098 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2099 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2100 p->subs[SUB_THREEWAY].inthreeway = 0;
2101 /* Make it the call wait now */
2102 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2103 unalloc_sub(p, SUB_THREEWAY);
2105 unalloc_sub(p, SUB_CALLWAIT);
2106 } else if (index == SUB_THREEWAY) {
2107 if (p->subs[SUB_CALLWAIT].inthreeway) {
2108 /* The other party of the three way call is currently in a call-wait state.
2109 Start music on hold for them, and take the main guy out of the third call */
2110 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2111 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2112 p->subs[SUB_CALLWAIT].inthreeway = 0;
2114 p->subs[SUB_REAL].inthreeway = 0;
2115 /* If this was part of a three way call index, let us make
2116 another three way call */
2117 unalloc_sub(p, SUB_THREEWAY);
2119 /* This wasn't any sort of call, but how are we an index? */
2120 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2125 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2128 p->distinctivering = 0;
2129 p->confirmanswer = 0;
2135 p->onhooktime = time(NULL);
2141 ast_dsp_free(p->dsp);
2145 law = ZT_LAW_DEFAULT;
2146 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2148 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2149 /* Perform low level hangup if no owner left */
2152 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2153 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2154 if (!pri_grab(p, p->pri)) {
2155 if (p->alreadyhungup) {
2156 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2157 pri_hangup(p->pri->pri, p->call, -1);
2160 p->bearer->call = NULL;
2162 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2163 int icause = ast->hangupcause ? ast->hangupcause : -1;
2164 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2165 p->alreadyhungup = 1;
2167 p->bearer->alreadyhungup = 1;
2170 icause = atoi(cause);
2172 pri_hangup(p->pri->pri, p->call, icause);
2175 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2178 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2183 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2190 if (p->sig == SIG_R2) {
2192 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2200 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2201 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2203 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2209 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2212 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2214 /* If they're off hook, try playing congestion */
2215 if ((par.rxisoffhook) && (!p->radio))
2216 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2218 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2224 /* Make sure we're not made available for at least two seconds assuming
2225 we were actually used for an inbound or outbound call. */
2226 if (ast->_state != AST_STATE_RESERVED) {
2227 time(&p->guardtime);
2232 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2239 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2240 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2244 p->callwaiting = p->permcallwaiting;
2245 p->hidecallerid = p->permhidecallerid;
2250 /* Restore data mode */
2251 if (p->sig == SIG_PRI) {
2253 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2257 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2258 /* Free up the bearer channel as well, and
2259 don't use its file descriptor anymore */
2260 update_conf(p->bearer);
2261 reset_conf(p->bearer);
2262 p->bearer->owner = NULL;
2263 p->bearer->realcall = NULL;
2265 p->subs[SUB_REAL].zfd = -1;
2273 p->callwaitingrepeat = 0;
2275 ast->pvt->pvt = NULL;
2276 ast_mutex_unlock(&p->lock);
2277 ast_mutex_lock(&usecnt_lock);
2280 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2281 ast_mutex_unlock(&usecnt_lock);
2282 ast_update_use_count();
2283 if (option_verbose > 2)
2284 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2286 ast_mutex_lock(&iflock);
2292 destroy_channel(prev, tmp, 0);
2300 ast_mutex_unlock(&iflock);
2304 static int zt_answer(struct ast_channel *ast)
2306 struct zt_pvt *p = ast->pvt->pvt;
2309 int oldstate = ast->_state;
2310 ast_setstate(ast, AST_STATE_UP);
2311 ast_mutex_lock(&p->lock);
2312 index = zt_get_index(ast, p, 0);
2315 /* nothing to do if a radio channel */
2317 ast_mutex_unlock(&p->lock);
2336 case SIG_SF_FEATDMF:
2341 /* Pick up the line */
2342 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2343 if(p->hanguponpolarityswitch) {
2344 gettimeofday(&p->polaritydelaytv, NULL);
2346 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2347 tone_zone_play_tone(p->subs[index].zfd, -1);
2349 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2350 if (oldstate == AST_STATE_RINGING) {
2351 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2352 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2353 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2354 p->owner = p->subs[SUB_REAL].owner;
2357 if (p->sig & __ZT_SIG_FXS) {
2364 /* Send a pri acknowledge */
2365 if (!pri_grab(p, p->pri)) {
2367 res = pri_answer(p->pri->pri, p->call, 0, 1);
2370 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2377 res = mfcr2_AnswerCall(p->r2, NULL);
2379 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2383 ast_mutex_unlock(&p->lock);
2386 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2389 ast_mutex_unlock(&p->lock);
2393 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2398 struct zt_pvt *p = chan->pvt->pvt;
2401 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2402 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2408 if ((!cp) || (datalen < 1))
2414 case AST_OPTION_TONE_VERIFY:
2419 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2420 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2423 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2424 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2427 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2428 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2432 case AST_OPTION_TDD: /* turn on or off TDD */
2433 if (!*cp) { /* turn it off */
2434 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2435 if (p->tdd) tdd_free(p->tdd);
2441 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2442 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2445 /* otherwise, turn it on */
2446 if (!p->didtdd) { /* if havent done it yet */
2447 unsigned char mybuf[41000],*buf;
2448 int size,res,fd,len;
2450 struct pollfd fds[1];
2452 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2453 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2455 index = zt_get_index(chan, p, 0);
2457 ast_log(LOG_WARNING, "No index in TDD?\n");
2460 fd = p->subs[index].zfd;
2462 if (ast_check_hangup(chan)) return -1;
2464 if (size > READ_SIZE)
2467 fds[0].events = POLLPRI | POLLOUT;
2468 res = poll(fds, 1, -1);
2470 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2473 /* if got exception */
2474 if (fds[0].revents & POLLPRI) return -1;
2475 if (!(fds[0].revents & POLLOUT)) {
2476 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2479 res = write(fd, buf, size);
2481 if (res == -1) return -1;
2482 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2488 p->didtdd = 1; /* set to have done it now */
2490 if (*cp == 2) { /* Mate mode */
2491 if (p->tdd) tdd_free(p->tdd);
2496 if (!p->tdd) { /* if we dont have one yet */
2497 p->tdd = tdd_new(); /* allocate one */
2500 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2503 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2508 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2511 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2513 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2516 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2522 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2525 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2526 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2533 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2535 /* Unlink a specific slave or all slaves/masters from a given master */
2541 ast_mutex_lock(&master->lock);
2543 while(ast_mutex_trylock(&slave->lock)) {
2544 ast_mutex_unlock(&master->lock);
2546 ast_mutex_lock(&master->lock);
2551 for (x=0;x<MAX_SLAVES;x++) {
2552 if (master->slaves[x]) {
2553 if (!slave || (master->slaves[x] == slave)) {
2554 /* Take slave out of the conference */
2555 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2556 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2557 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2558 master->slaves[x]->master = NULL;
2559 master->slaves[x] = NULL;
2564 master->inconference = 0;
2567 if (master->master) {
2568 /* Take master out of the conference */
2569 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2570 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2572 for (x=0;x<MAX_SLAVES;x++) {
2573 if (master->master->slaves[x] == master)
2574 master->master->slaves[x] = NULL;
2575 else if (master->master->slaves[x])
2579 master->master->inconference = 0;
2581 master->master = NULL;
2583 update_conf(master);
2586 ast_mutex_unlock(&slave->lock);
2587 ast_mutex_unlock(&master->lock);
2591 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2593 if (!slave || !master) {
2594 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2597 for (x=0;x<MAX_SLAVES;x++) {
2598 if (!master->slaves[x]) {
2599 master->slaves[x] = slave;
2603 if (x >= MAX_SLAVES) {
2604 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2605 master->slaves[MAX_SLAVES - 1] = slave;
2608 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2609 slave->master = master;
2611 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2614 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2616 struct ast_channel *who = NULL, *cs[3];
2617 struct zt_pvt *p0, *p1, *op0, *op1;
2618 struct zt_pvt *master=NULL, *slave=NULL;
2619 struct ast_frame *f;
2624 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2625 int os1 = -1, os2 = -1;
2626 struct ast_channel *oc1, *oc2;
2628 /* if need DTMF, cant native bridge */
2629 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2633 ast_mutex_lock(&c0->lock);
2634 ast_mutex_lock(&c1->lock);
2638 /* cant do pseudo-channels here */
2639 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2640 ast_mutex_unlock(&c0->lock);
2641 ast_mutex_unlock(&c1->lock);
2645 op0 = p0 = c0->pvt->pvt;
2646 op1 = p1 = c1->pvt->pvt;
2649 oi1 = zt_get_index(c0, p0, 0);
2650 oi2 = zt_get_index(c1, p1, 0);
2653 if ((oi1 < 0) || (oi2 < 0)) {
2654 ast_mutex_unlock(&c0->lock);
2655 ast_mutex_unlock(&c1->lock);
2661 ast_mutex_lock(&p0->lock);
2662 if (ast_mutex_trylock(&p1->lock)) {
2663 /* Don't block, due to potential for deadlock */
2664 ast_mutex_unlock(&p0->lock);
2665 ast_mutex_unlock(&c0->lock);
2666 ast_mutex_unlock(&c1->lock);
2667 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2670 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2671 if (!p0->owner || !p1->owner) {
2672 /* Currently unowned -- Do nothing. */
2675 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2676 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2680 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2685 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2686 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,
2687 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2690 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2691 if (p1->subs[SUB_THREEWAY].inthreeway) {
2697 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2698 if (p0->subs[SUB_THREEWAY].inthreeway) {
2704 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2705 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2706 don't put us in anything */
2707 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2713 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2714 /* Same as previous */
2715 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2722 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2723 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2724 if (master && slave) {
2725 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2726 in an active threeway call with a channel that is ringing, we should
2727 indicate ringing. */
2728 if ((oi2 == SUB_THREEWAY) &&
2729 p1->subs[SUB_THREEWAY].inthreeway &&
2730 p1->subs[SUB_REAL].owner &&
2731 p1->subs[SUB_REAL].inthreeway &&
2732 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2733 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2734 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2735 os2 = p1->subs[SUB_REAL].owner->_state;
2737 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2738 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2740 if ((oi1 == SUB_THREEWAY) &&
2741 p0->subs[SUB_THREEWAY].inthreeway &&
2742 p0->subs[SUB_REAL].owner &&
2743 p0->subs[SUB_REAL].inthreeway &&
2744 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2745 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2746 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2747 os1 = p0->subs[SUB_REAL].owner->_state;
2749 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2750 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2752 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2753 if (!p0->echocanbridged || !p1->echocanbridged) {
2754 /* Disable echo cancellation if appropriate */
2759 zt_link(slave, master);
2760 master->inconference = inconf;
2761 } else if (!nothingok)
2762 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2766 t1 = p0->subs[SUB_REAL].inthreeway;
2767 t2 = p1->subs[SUB_REAL].inthreeway;
2769 ast_mutex_unlock(&p0->lock);
2770 ast_mutex_unlock(&p1->lock);
2772 ast_mutex_unlock(&c0->lock);
2773 ast_mutex_unlock(&c1->lock);
2775 /* Native bridge failed */
2776 if ((!master || !slave) && !nothingok) {
2788 /* Here's our main loop... Start by locking things, looking for private parts,
2789 and then balking if anything is wrong */
2790 ast_mutex_lock(&c0->lock);
2791 ast_mutex_lock(&c1->lock);
2795 i1 = zt_get_index(c0, p0, 1);
2797 i2 = zt_get_index(c1, p1, 1);
2798 ast_mutex_unlock(&c0->lock);
2799 ast_mutex_unlock(&c1->lock);
2800 if ((op0 != p0) || (op1 != p1) ||
2801 (ofd1 != c0->fds[0]) ||
2802 (ofd2 != c1->fds[0]) ||
2803 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2804 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2805 (oc1 != p0->owner) ||
2806 (oc2 != p1->owner) ||
2807 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2808 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2811 if (slave && master)
2812 zt_unlink(slave, master, 1);
2813 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2814 op0->channel, oi1, op1->channel, oi2);
2822 who = ast_waitfor_n(cs, 2, &to);
2824 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2827 if (who->pvt->pvt == op0)
2828 op0->ignoredtmf = 1;
2829 else if (who->pvt->pvt == op1)
2830 op1->ignoredtmf = 1;
2832 if (who->pvt->pvt == op0)
2833 op0->ignoredtmf = 0;
2834 else if (who->pvt->pvt == op1)
2835 op1->ignoredtmf = 0;
2839 if (slave && master)
2840 zt_unlink(slave, master, 1);
2847 if (f->frametype == AST_FRAME_CONTROL) {
2850 if (slave && master)
2851 zt_unlink(slave, master, 1);
2854 if (f->frametype == AST_FRAME_DTMF) {
2855 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2856 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2859 if (slave && master)
2860 zt_unlink(slave, master, 1);
2862 } else if ((who == c0) && p0->pulsedial) {
2864 } else if ((who == c1) && p1->pulsedial) {
2870 /* Swap who gets priority */
2877 static int zt_indicate(struct ast_channel *chan, int condition);
2879 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2881 struct zt_pvt *p = newchan->pvt->pvt;
2883 ast_mutex_lock(&p->lock);
2884 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2885 if (p->owner == oldchan) {
2889 if (p->subs[x].owner == oldchan) {
2891 zt_unlink(NULL, p, 0);
2892 p->subs[x].owner = newchan;
2894 if (newchan->_state == AST_STATE_RINGING)
2895 zt_indicate(newchan, AST_CONTROL_RINGING);
2897 ast_mutex_unlock(&p->lock);
2901 static int zt_ring_phone(struct zt_pvt *p)
2905 /* Make sure our transmit state is on hook */
2908 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2911 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2913 printf("Res: %d, error: %s\n", res, strerror(errno));
2919 /* Wait just in case */
2926 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
2934 static void *ss_thread(void *data);
2936 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
2938 static int attempt_transfer(struct zt_pvt *p)
2940 /* In order to transfer, we need at least one of the channels to
2941 actually be in a call bridge. We can't conference two applications
2942 together (but then, why would we want to?) */
2943 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
2944 /* The three-way person we're about to transfer to could still be in MOH, so
2945 stop if now if appropriate */
2946 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2947 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
2948 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
2949 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
2951 if (p->subs[SUB_REAL].owner->cdr) {
2952 /* Move CDR from second channel to current one */
2953 p->subs[SUB_THREEWAY].owner->cdr =
2954 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
2955 p->subs[SUB_REAL].owner->cdr = NULL;
2957 if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
2958 /* Move CDR from second channel's bridge to current one */
2959 p->subs[SUB_THREEWAY].owner->cdr =
2960 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
2961 ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
2963 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
2964 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2965 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
2968 /* Orphan the channel after releasing the lock */
2969 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
2970 unalloc_sub(p, SUB_THREEWAY);
2971 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2972 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
2973 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
2975 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
2976 if (p->subs[SUB_THREEWAY].owner->cdr) {
2977 /* Move CDR from second channel to current one */
2978 p->subs[SUB_REAL].owner->cdr =
2979 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
2980 p->subs[SUB_THREEWAY].owner->cdr = NULL;
2982 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr) {
2983 /* Move CDR from second channel's bridge to current one */
2984 p->subs[SUB_REAL].owner->cdr =
2985 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr);
2986 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr = NULL;
2988 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
2989 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
2990 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
2993 /* Three-way is now the REAL */
2994 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2995 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
2996 unalloc_sub(p, SUB_THREEWAY);
2997 /* Tell the caller not to hangup */
3000 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
3001 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
3002 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3008 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
3010 struct ast_frame *f;
3011 f = &p->subs[index].f;
3013 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
3017 case MFCR2_EVENT_BLOCKED:
3018 if (option_verbose > 2)
3019 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
3021 case MFCR2_EVENT_UNBLOCKED:
3022 if (option_verbose > 2)
3023 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
3025 case MFCR2_EVENT_CONFIG_ERR:
3026 if (option_verbose > 2)
3027 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
3029 case MFCR2_EVENT_RING:
3030 if (option_verbose > 2)
3031 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
3033 case MFCR2_EVENT_HANGUP:
3034 if (option_verbose > 2)
3035 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
3037 case MFCR2_EVENT_RINGING:
3038 if (option_verbose > 2)
3039 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
3041 case MFCR2_EVENT_ANSWER:
3042 if (option_verbose > 2)
3043 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
3045 case MFCR2_EVENT_HANGUP_ACK:
3046 if (option_verbose > 2)
3047 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
3049 case MFCR2_EVENT_IDLE:
3050 if (option_verbose > 2)
3051 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
3054 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
3060 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
3065 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
3067 ast_log(LOG_WARNING, "Unable to check received bits\n");
3071 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
3074 e = mfcr2_cas_signaling_event(p->r2, x);
3079 static int check_for_conference(struct zt_pvt *p)
3082 /* Fine if we already have a master, etc */
3083 if (p->master || (p->confno > -1))
3085 memset(&ci, 0, sizeof(ci));
3086 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
3087 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
3090 /* If we have no master and don't have a confno, then
3091 if we're in a conference, it's probably a MeetMe room or
3092 some such, so don't let us 3-way out! */
3093 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
3094 if (option_verbose > 2)
3095 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
3101 static int get_alarms(struct zt_pvt *p)
3105 memset(&zi, 0, sizeof(zi));
3106 zi.spanno = p->span;
3107 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
3109 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
3115 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
3120 struct zt_pvt *p = ast->pvt->pvt;
3122 pthread_attr_t attr;
3123 struct ast_channel *chan;
3124 pthread_attr_init(&attr);
3125 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3126 index = zt_get_index(ast, p, 0);
3127 p->subs[index].f.frametype = AST_FRAME_NULL;
3128 p->subs[index].f.datalen = 0;
3129 p->subs[index].f.samples = 0;
3130 p->subs[index].f.mallocd = 0;
3131 p->subs[index].f.offset = 0;
3132 p->subs[index].f.src = "zt_handle_event";
3133 p->subs[index].f.data = NULL;
3135 return &p->subs[index].f;
3136 if (p->fake_event) {
3137 res = p->fake_event;
3140 res = zt_get_event(p->subs[index].zfd);
3141 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
3142 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
3143 if (res & ZT_EVENT_PULSEDIGIT)
3147 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
3148 p->subs[index].f.frametype = AST_FRAME_DTMF;
3149 p->subs[index].f.subclass = res & 0xff;
3150 /* Return the captured digit */
3151 return &p->subs[index].f;
3154 case ZT_EVENT_BITSCHANGED:
3155 if (p->sig == SIG_R2) {
3157 struct ast_frame *f = &p->subs[index].f;
3159 e = r2_get_event_bits(p);
3161 f = handle_r2_event(p, e, index);
3167 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
3168 case ZT_EVENT_PULSE_START:
3169 /* Stop tone if there's a pulse start and the PBX isn't started */
3171 tone_zone_play_tone(p->subs[index].zfd, -1);
3173 case ZT_EVENT_DIALCOMPLETE:
3174 if (p->inalarm) break;
3175 if (p->radio) break;
3176 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
3177 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
3180 if (!x) { /* if not still dialing in driver */
3184 strncpy(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr) - 1);
3185 p->dop.op = ZT_DIAL_OP_REPLACE;
3186 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
3190 if (p->sig == SIG_E911) {
3191 /* if thru with dialing after offhook */
3192 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
3193 ast_setstate(ast, AST_STATE_UP);
3194 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3195 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3197 } else { /* if to state wait for offhook to dial rest */
3198 /* we now wait for off hook */
3199 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
3202 if (ast->_state == AST_STATE_DIALING) {
3203 if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
3204 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
3205 } 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)))) {
3206 ast_setstate(ast, AST_STATE_RINGING);
3207 } else if (!p->answeronpolarityswitch) {
3208 ast_setstate(ast, AST_STATE_UP);
3209 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3210 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3216 case ZT_EVENT_ALARM:
3219 if (p->pri && p->pri->pri) {
3220 if (!pri_grab(p, p->pri)) {
3221 pri_hangup(p->pri->pri, p->call, -1);
3222 pri_destroycall(p->pri->pri, p->call);
3225 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
3227 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
3230 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
3233 p->bearer->inalarm = 1;
3237 res = get_alarms(p);
3238 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
3239 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
3242 alarm2str(res), p->channel);
3243 /* fall through intentionally */
3244 case ZT_EVENT_ONHOOK:
3247 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3248 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
3255 p->onhooktime = time(NULL);
3257 /* Check for some special conditions regarding call waiting */
3258 if (index == SUB_REAL) {
3259 /* The normal line was hung up */
3260 if (p->subs[SUB_CALLWAIT].owner) {
3261 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
3262 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3263 if (option_verbose > 2)
3264 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
3265 unalloc_sub(p, SUB_CALLWAIT);
3267 p->subs[index].needanswer = 0;
3268 p->subs[index].needringing = 0;
3270 p->callwaitingrepeat = 0;
3273 /* Don't start streaming audio yet if the incoming call isn't up yet */
3274 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
3277 } else if (p->subs[SUB_THREEWAY].owner) {
3279 unsigned int mssinceflash;
3280 /* Here we have to retain the lock on both the main channel, the 3-way channel, and
3281 the private structure -- not especially easy or clean */
3282 while(p->subs[SUB_THREEWAY].owner && ast_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) {
3283 /* Yuck, didn't get the lock on the 3-way, gotta release everything and re-grab! */
3284 ast_mutex_unlock(&p->lock);
3285 ast_mutex_unlock(&ast->lock);
3287 /* We can grab ast and p in that order, without worry. We should make sure
3288 nothing seriously bad has happened though like some sort of bizarre double
3290 ast_mutex_lock(&ast->lock);
3291 ast_mutex_lock(&p->lock);
3292 if (p->owner != ast) {
3293 ast_log(LOG_WARNING, "This isn't good...\n");
3297 if (!p->subs[SUB_THREEWAY].owner) {
3298 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
3301 gettimeofday(&tv, NULL);
3302 mssinceflash = (tv.tv_sec - p->flashtime.tv_sec) * 1000 + (tv.tv_usec - p->flashtime.tv_usec) / 1000;
3303 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
3304 if (mssinceflash < MIN_MS_SINCE_FLASH) {
3305 /* It hasn't been long enough since the last flashook. This is probably a bounce on
3306 hanging up. Hangup both channels now */
3307 if (p->subs[SUB_THREEWAY].owner)
3308 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
3309 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3310 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
3311 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
3312 } else if ((ast->pbx) ||
3313 (ast->_state == AST_STATE_UP)) {
3315 /* In any case this isn't a threeway call anymore */
3316 p->subs[SUB_REAL].inthreeway = 0;
3317 p->subs[SUB_THREEWAY].inthreeway = 0;
3318 if ((res = attempt_transfer(p)) < 0)
3319 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3321 /* Don't actually hang up at this point */
3322 if (p->subs[SUB_THREEWAY].owner)