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 int restore_gains(struct zt_pvt *p);
756 static void swap_subs(struct zt_pvt *p, int a, int b)
760 struct ast_channel *towner;
762 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
764 tchan = p->subs[a].chan;
765 towner = p->subs[a].owner;
766 tinthreeway = p->subs[a].inthreeway;
768 p->subs[a].chan = p->subs[b].chan;
769 p->subs[a].owner = p->subs[b].owner;
770 p->subs[a].inthreeway = p->subs[b].inthreeway;
772 p->subs[b].chan = tchan;
773 p->subs[b].owner = towner;
774 p->subs[b].inthreeway = tinthreeway;
776 if (p->subs[a].owner)
777 p->subs[a].owner->fds[0] = p->subs[a].zfd;
778 if (p->subs[b].owner)
779 p->subs[b].owner->fds[0] = p->subs[b].zfd;
780 wakeup_sub(p, a, NULL);
781 wakeup_sub(p, b, NULL);
784 static int zt_open(char *fn)
792 for (x=0;x<strlen(fn);x++) {
793 if (!isdigit(fn[x])) {
801 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
804 fn = "/dev/zap/channel";
806 fd = open(fn, O_RDWR | O_NONBLOCK);
808 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
812 if (ioctl(fd, ZT_SPECIFY, &chan)) {
816 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
821 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
825 static void zt_close(int fd)
831 int zt_setlinear(int zfd, int linear)
834 res = ioctl(zfd, ZT_SETLINEAR, &linear);
841 int zt_setlaw(int zfd, int law)
844 res = ioctl(zfd, ZT_SETLAW, &law);
850 static int alloc_sub(struct zt_pvt *p, int x)
854 if (p->subs[x].zfd < 0) {
855 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
856 if (p->subs[x].zfd > -1) {
857 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
859 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
860 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
861 bi.numbufs = numbufs;
862 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
864 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
867 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
868 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
869 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
870 zt_close(p->subs[x].zfd);
875 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
878 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
881 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
885 static int unalloc_sub(struct zt_pvt *p, int x)
888 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
891 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
892 if (p->subs[x].zfd > -1) {
893 zt_close(p->subs[x].zfd);
896 p->subs[x].linear = 0;
898 p->subs[x].owner = NULL;
899 p->subs[x].inthreeway = 0;
900 p->polarity = POLARITY_IDLE;
901 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
905 static int zt_digit(struct ast_channel *ast, char digit)
907 ZT_DIAL_OPERATION zo;
912 ast_mutex_lock(&p->lock);
913 index = zt_get_index(ast, p, 0);
914 if ((index == SUB_REAL) && p->owner) {
916 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
918 if (!pri_grab(p, p->pri)) {
919 pri_information(p->pri->pri,p->call,digit);
922 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
923 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
924 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
925 res = strlen(p->dialdest);
926 p->dialdest[res++] = digit;
927 p->dialdest[res] = '\0';
933 zo.op = ZT_DIAL_OP_APPEND;
935 zo.dialstr[1] = digit;
937 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
938 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
943 ast_mutex_unlock(&p->lock);
947 static char *events[] = {
960 "Hook Transition Complete",
972 { ZT_ALARM_RED, "Red Alarm" },
973 { ZT_ALARM_YELLOW, "Yellow Alarm" },
974 { ZT_ALARM_BLUE, "Blue Alarm" },
975 { ZT_ALARM_RECOVER, "Recovering" },
976 { ZT_ALARM_LOOPBACK, "Loopback" },
977 { ZT_ALARM_NOTOPEN, "Not Open" },
978 { ZT_ALARM_NONE, "None" },
981 static char *alarm2str(int alarm)
984 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
985 if (alarms[x].alarm & alarm)
986 return alarms[x].name;
988 return alarm ? "Unknown Alarm" : "No Alarm";
991 static char *event2str(int event)
993 static char buf[256];
994 if ((event < 18) && (event > -1))
995 return events[event];
996 sprintf(buf, "Event %d", event); /* safe */
1000 static char *dialplan2str(int dialplan)
1002 if (dialplan == -1) {
1003 return("Dynamically set dialplan in ISDN");
1005 return(pri_plan2str(dialplan));
1009 static int str2r2prot(char *swtype)
1011 if (!strcasecmp(swtype, "ar"))
1012 return MFCR2_PROT_ARGENTINA;
1014 if (!strcasecmp(swtype, "cn"))
1015 return MFCR2_PROT_CHINA;
1017 if (!strcasecmp(swtype, "kr"))
1018 return MFCR2_PROT_KOREA;
1024 static char *sig2str(int sig)
1026 static char buf[256];
1029 return "E & M Immediate";
1031 return "E & M Wink";
1035 return "Feature Group D (DTMF)";
1037 return "Feature Group D (MF)";
1039 return "Feature Group B (MF)";
1043 return "FXS Loopstart";
1045 return "FXS Groundstart";
1047 return "FXS Kewlstart";
1049 return "FXO Loopstart";
1051 return "FXO Groundstart";
1053 return "FXO Kewlstart";
1055 return "PRI Signalling";
1057 return "R2 Signalling";
1059 return "SF (Tone) Signalling Immediate";
1061 return "SF (Tone) Signalling Wink";
1063 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1064 case SIG_SF_FEATDMF:
1065 return "SF (Tone) Signalling with Feature Group D (MF)";
1067 return "SF (Tone) Signalling with Feature Group B (MF)";
1068 case SIG_GR303FXOKS:
1069 return "GR-303 Signalling with FXOKS";
1070 case SIG_GR303FXSKS:
1071 return "GR-303 Signalling with FXSKS";
1073 return "Pseudo Signalling";
1075 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1080 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1082 /* If the conference already exists, and we're already in it
1083 don't bother doing anything */
1086 memset(&zi, 0, sizeof(zi));
1089 if (slavechannel > 0) {
1090 /* If we have only one slave, do a digital mon */
1091 zi.confmode = ZT_CONF_DIGITALMON;
1092 zi.confno = slavechannel;
1095 /* Real-side and pseudo-side both participate in conference */
1096 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1097 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1099 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1100 zi.confno = p->confno;
1102 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1106 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1107 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1110 if (slavechannel < 1) {
1111 p->confno = zi.confno;
1113 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1114 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1118 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1120 /* If they're listening to our channel, they're ours */
1121 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1123 /* If they're a talker on our (allocated) conference, they're ours */
1124 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1129 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1132 if (/* Can't delete if there's no zfd */
1134 /* Don't delete from the conference if it's not our conference */
1136 /* Don't delete if we don't think it's conferenced at all (implied) */
1138 memset(&zi, 0, sizeof(zi));
1142 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1143 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1146 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1147 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1151 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1155 struct zt_pvt *slave = NULL;
1156 /* Start out optimistic */
1158 /* Update conference state in a stateless fashion */
1160 /* Any three-way calling makes slave native mode *definitely* out
1162 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1165 /* If we don't have any 3-way calls, check to see if we have
1166 precisely one slave */
1167 if (useslavenative) {
1168 for (x=0;x<MAX_SLAVES;x++) {
1171 /* Whoops already have a slave! No
1172 slave native and stop right away */
1177 /* We have one slave so far */
1178 slave = p->slaves[x];
1183 /* If no slave, slave native definitely out */
1186 else if (slave->law != p->law) {
1192 return useslavenative;
1195 static int reset_conf(struct zt_pvt *p)
1198 memset(&zi, 0, sizeof(zi));
1200 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1201 if (p->subs[SUB_REAL].zfd > -1) {
1202 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1203 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1208 static int update_conf(struct zt_pvt *p)
1213 struct zt_pvt *slave = NULL;
1215 useslavenative = isslavenative(p, &slave);
1216 /* Start with the obvious, general stuff */
1218 /* Look for three way calls */
1219 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1220 conf_add(p, &p->subs[x], x, 0);
1223 conf_del(p, &p->subs[x], x);
1226 /* If we have a slave, add him to our conference now. or DAX
1227 if this is slave native */
1228 for (x=0;x<MAX_SLAVES;x++) {
1231 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1233 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1238 /* If we're supposed to be in there, do so now */
1239 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1241 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1243 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1247 /* If we have a master, add ourselves to his conference */
1249 if (isslavenative(p->master, NULL)) {
1250 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1252 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1256 /* Nobody is left (or should be left) in our conference.
1260 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1264 static void zt_enable_ec(struct zt_pvt *p)
1271 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1275 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1278 if (p->echocancel) {
1279 if (p->sig == SIG_PRI) {
1281 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1283 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1286 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1288 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1291 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1294 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1297 static void zt_train_ec(struct zt_pvt *p)
1301 if (p && p->echocancel && p->echotraining) {
1302 x = p->echotraining;
1303 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1305 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1307 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1310 ast_log(LOG_DEBUG, "No echo training requested\n");
1313 static void zt_disable_ec(struct zt_pvt *p)
1317 if (p->echocancel) {
1319 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1321 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1323 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1328 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1335 if ((rxgain != 0.0) || (txgain != 0.0)) {
1336 /* caluculate linear value of tx gain */
1337 ltxgain = pow(10.0,txgain / 20.0);
1338 /* caluculate linear value of rx gain */
1339 lrxgain = pow(10.0,rxgain / 20.0);
1340 if (law == ZT_LAW_ALAW) {
1341 for (j=0;j<256;j++) {
1342 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1343 if (k > 32767) k = 32767;
1344 if (k < -32767) k = -32767;
1345 g.rxgain[j] = AST_LIN2A(k);
1346 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1347 if (k > 32767) k = 32767;
1348 if (k < -32767) k = -32767;
1349 g.txgain[j] = AST_LIN2A(k);
1352 for (j=0;j<256;j++) {
1353 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1354 if (k > 32767) k = 32767;
1355 if (k < -32767) k = -32767;
1356 g.rxgain[j] = AST_LIN2MU(k);
1357 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1358 if (k > 32767) k = 32767;
1359 if (k < -32767) k = -32767;
1360 g.txgain[j] = AST_LIN2MU(k);
1364 for (j=0;j<256;j++) {
1371 return(ioctl(fd,ZT_SETGAINS,&g));
1374 static inline int zt_set_hook(int fd, int hs)
1378 res = ioctl(fd, ZT_HOOK, &x);
1381 if (errno == EINPROGRESS) return 0;
1382 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1387 static inline int zt_confmute(struct zt_pvt *p, int muted)
1391 if (p->sig == SIG_PRI) {
1393 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1395 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1397 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1399 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1403 static int save_conference(struct zt_pvt *p)
1405 struct zt_confinfo c;
1407 if (p->saveconf.confmode) {
1408 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1411 p->saveconf.chan = 0;
1412 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1414 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1415 p->saveconf.confmode = 0;
1420 c.confmode = ZT_CONF_NORMAL;
1421 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1423 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1427 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1431 static int restore_conference(struct zt_pvt *p)
1434 if (p->saveconf.confmode) {
1435 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1436 p->saveconf.confmode = 0;
1438 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1443 ast_log(LOG_DEBUG, "Restored conferencing\n");
1447 static int send_callerid(struct zt_pvt *p);
1449 int send_cwcidspill(struct zt_pvt *p)
1453 p->cidspill = malloc(MAX_CALLERID_SIZE);
1455 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1456 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1457 /* Make sure we account for the end */
1458 p->cidlen += READ_SIZE * 4;
1461 if (option_verbose > 2)
1462 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1467 static int has_voicemail(struct zt_pvt *p)
1470 return ast_app_has_voicemail(p->mailbox, NULL);
1473 static int send_callerid(struct zt_pvt *p)
1475 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1477 /* Take out of linear mode if necessary */
1478 if (p->subs[SUB_REAL].linear) {
1479 p->subs[SUB_REAL].linear = 0;
1480 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1482 while(p->cidpos < p->cidlen) {
1483 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1485 if (errno == EAGAIN)
1488 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1498 if (p->callwaitcas) {
1499 /* Wait for CID/CW to expire */
1500 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1502 restore_conference(p);
1506 static int zt_callwait(struct ast_channel *ast)
1508 struct zt_pvt *p = ast->pvt->pvt;
1509 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1511 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1514 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1518 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1519 if (!p->callwaitrings && p->callwaitingcallerid) {
1520 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1522 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1524 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1526 p->cidlen = 2400 + READ_SIZE * 4;
1531 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1537 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1539 struct zt_pvt *p = ast->pvt->pvt;
1545 char dest[256]; /* must be same length as p->dialdest */
1546 ast_mutex_lock(&p->lock);
1547 strncpy(dest, rdest, sizeof(dest) - 1);
1548 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1549 if ((ast->_state == AST_STATE_BUSY)) {
1550 p->subs[SUB_REAL].needbusy = 1;
1551 ast_mutex_unlock(&p->lock);
1554 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1555 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1556 ast_mutex_unlock(&p->lock);
1560 if (p->radio) /* if a radio channel, up immediately */
1562 /* Special pseudo -- automatically up */
1563 ast_setstate(ast, AST_STATE_UP);
1564 ast_mutex_unlock(&p->lock);
1567 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1568 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1570 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1573 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1579 if (p->owner == ast) {
1580 /* Normal ring, on hook */
1582 /* Don't send audio while on hook, until the call is answered */
1584 if (p->use_callerid) {
1585 /* Generate the Caller-ID spill if desired */
1587 ast_log(LOG_WARNING, "cidspill already exists??\n");
1590 p->cidspill = malloc(MAX_CALLERID_SIZE);
1593 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1597 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1599 /* Choose proper cadence */
1600 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1601 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1602 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1603 p->cidrings = cidrings[p->distinctivering - 1];
1605 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1606 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1607 p->cidrings = p->sendcalleridafter;
1611 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1612 c = strchr(dest, '/');
1615 if (c && (strlen(c) < p->stripmsd)) {
1616 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1620 p->dop.op = ZT_DIAL_OP_REPLACE;
1621 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1622 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1624 p->dop.dialstr[0] = '\0';
1627 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1628 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1629 ast_mutex_unlock(&p->lock);
1634 /* Call waiting call */
1635 p->callwaitrings = 0;
1636 if (ast->cid.cid_num)
1637 strncpy(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num)-1);
1639 p->callwait_num[0] = '\0';
1640 if (ast->cid.cid_name)
1641 strncpy(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name)-1);
1643 p->callwait_name[0] = '\0';
1644 /* Call waiting tone instead */
1645 if (zt_callwait(ast)) {
1646 ast_mutex_unlock(&p->lock);
1649 /* Make ring-back */
1650 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1651 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1654 n = ast->cid.cid_name;
1655 l = ast->cid.cid_num;
1657 strncpy(p->lastcid_num, l, sizeof(p->lastcid_num) - 1);
1659 p->lastcid_num[0] = '\0';
1661 strncpy(p->lastcid_name, n, sizeof(p->lastcid_name) - 1);
1663 p->lastcid_name[0] = '\0';
1664 ast_setstate(ast, AST_STATE_RINGING);
1665 index = zt_get_index(ast, p, 0);
1667 p->subs[index].needringing = 1;
1683 case SIG_SF_FEATDMF:
1685 c = strchr(dest, '/');
1690 if (strlen(c) < p->stripmsd) {
1691 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1692 ast_mutex_unlock(&p->lock);
1696 /* Start the trunk, if not GR-303 */
1700 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1702 if (errno != EINPROGRESS) {
1703 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1704 ast_mutex_unlock(&p->lock);
1711 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1712 p->dop.op = ZT_DIAL_OP_REPLACE;
1713 if (p->sig == SIG_FEATD) {
1714 l = ast->cid.cid_num;
1716 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1718 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1720 if (p->sig == SIG_FEATDMF) {
1721 l = ast->cid.cid_num;
1723 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1725 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1727 if (p->sig == SIG_E911) {
1728 strncpy(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr) - 1);
1730 if (p->sig == SIG_FEATB) {
1731 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1734 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c + p->stripmsd);
1736 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1737 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1738 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1739 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1740 p->echorest[sizeof(p->echorest) - 1] = '\0';
1742 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1746 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1748 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1749 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1750 ast_mutex_unlock(&p->lock);
1754 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1756 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1757 ast_setstate(ast, AST_STATE_DIALING);
1760 /* Special pseudo -- automatically up*/
1761 ast_setstate(ast, AST_STATE_UP);
1764 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1765 p->dialdest[0] = '\0';
1768 ast_log(LOG_DEBUG, "not yet implemented\n");
1769 ast_mutex_unlock(&p->lock);
1777 int prilocaldialplan;
1779 c = strchr(dest, '/');
1784 if (!p->hidecallerid) {
1785 l = ast->cid.cid_num;
1786 n = ast->cid.cid_name;
1791 if (strlen(c) < p->stripmsd) {
1792 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1793 ast_mutex_unlock(&p->lock);
1796 if (p->sig != SIG_FXSKS) {
1797 p->dop.op = ZT_DIAL_OP_REPLACE;
1798 s = strchr(c + p->stripmsd, 'w');
1801 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1803 p->dop.dialstr[0] = '\0';
1806 p->dop.dialstr[0] = '\0';
1809 if (pri_grab(p, p->pri)) {
1810 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1811 ast_mutex_unlock(&p->lock);
1814 if (!(p->call = pri_new_call(p->pri->pri))) {
1815 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1817 ast_mutex_unlock(&p->lock);
1820 if (!(sr = pri_sr_new())) {
1821 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1823 ast_mutex_unlock(&p->lock);
1825 if (p->bearer || (p->sig == SIG_FXSKS)) {
1827 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);
1828 p->bearer->call = p->call;
1830 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
1831 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1833 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1834 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1835 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1836 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1838 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1840 pridialplan = p->pri->dialplan - 1;
1841 if (pridialplan == -2) { /* compute dynamically */
1842 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
1843 dp_strip = strlen(p->pri->internationalprefix);
1844 pridialplan = PRI_INTERNATIONAL_ISDN;
1845 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
1846 dp_strip = strlen(p->pri->nationalprefix);
1847 pridialplan = PRI_NATIONAL_ISDN;
1849 pridialplan = PRI_LOCAL_ISDN;
1852 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
1855 prilocaldialplan = p->pri->localdialplan - 1;
1856 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
1857 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
1858 ldp_strip = strlen(p->pri->internationalprefix);
1859 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
1860 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
1861 ldp_strip = strlen(p->pri->nationalprefix);
1862 prilocaldialplan = PRI_NATIONAL_ISDN;
1864 prilocaldialplan = PRI_LOCAL_ISDN;
1867 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
1868 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
1869 PRES_NUMBER_NOT_AVAILABLE);
1870 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
1871 if (pri_setup(p->pri->pri, p->call, sr)) {
1872 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
1873 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
1875 ast_mutex_unlock(&p->lock);
1880 ast_setstate(ast, AST_STATE_DIALING);
1884 ast_mutex_unlock(&p->lock);
1888 static void destroy_zt_pvt(struct zt_pvt **pvt)
1890 struct zt_pvt *p = *pvt;
1891 /* Remove channel from the list */
1893 p->prev->next = p->next;
1895 p->next->prev = p->prev;
1896 ast_mutex_destroy(&p->lock);
1901 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1911 for (i = 0; i < 3; i++) {
1912 if (cur->subs[i].owner) {
1918 prev->next = cur->next;
1920 prev->next->prev = prev;
1926 iflist->prev = NULL;
1930 if (cur->subs[SUB_REAL].zfd > -1) {
1931 zt_close(cur->subs[SUB_REAL].zfd);
1933 destroy_zt_pvt(&cur);
1937 prev->next = cur->next;
1939 prev->next->prev = prev;
1945 iflist->prev = NULL;
1949 if (cur->subs[SUB_REAL].zfd > -1) {
1950 zt_close(cur->subs[SUB_REAL].zfd);
1952 destroy_zt_pvt(&cur);
1958 int pri_is_up(struct zt_pri *pri)
1961 for (x=0;x<NUM_DCHANS;x++) {
1962 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
1968 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
1970 bearer->owner = &inuse;
1971 bearer->realcall = crv;
1972 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
1973 if (crv->subs[SUB_REAL].owner)
1974 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
1975 crv->bearer = bearer;
1976 crv->call = bearer->call;
1981 static char *pri_order(int level)
1991 return "Quaternary";
1997 int pri_find_dchan(struct zt_pri *pri)
2004 for(x=0;x<NUM_DCHANS;x++) {
2005 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2007 if (pri->dchans[x] == old) {
2013 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
2014 pri->dchannels[newslot]);
2016 if (old && (oldslot != newslot))
2017 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2018 pri->dchannels[oldslot], pri->dchannels[newslot]);
2019 pri->pri = pri->dchans[newslot];
2024 static int zt_hangup(struct ast_channel *ast)
2028 /*static int restore_gains(struct zt_pvt *p);*/
2029 struct zt_pvt *p = ast->pvt->pvt;
2030 struct zt_pvt *tmp = NULL;
2031 struct zt_pvt *prev = NULL;
2035 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2036 if (!ast->pvt->pvt) {
2037 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2041 ast_mutex_lock(&p->lock);
2043 index = zt_get_index(ast, p, 1);
2045 if (p->sig == SIG_PRI) {
2047 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2053 if (p->origcid_num) {
2054 strncpy(p->cid_num, p->origcid_num, sizeof(p->cid_num) - 1);
2055 free(p->origcid_num);
2056 p->origcid_num = NULL;
2058 if (p->origcid_name) {
2059 strncpy(p->cid_name, p->origcid_name, sizeof(p->cid_name) - 1);
2060 free(p->origcid_name);
2061 p->origcid_name = NULL;
2064 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2068 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2069 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2073 /* Real channel, do some fixup */
2074 p->subs[index].owner = NULL;
2075 p->subs[index].needanswer = 0;
2076 p->subs[index].needflash = 0;
2077 p->subs[index].needringing = 0;
2078 p->subs[index].needbusy = 0;
2079 p->subs[index].needcongestion = 0;
2080 p->subs[index].linear = 0;
2081 p->subs[index].needcallerid = 0;
2082 p->polarity = POLARITY_IDLE;
2083 zt_setlinear(p->subs[index].zfd, 0);
2084 if (index == SUB_REAL) {
2085 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2086 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2087 if (p->subs[SUB_CALLWAIT].inthreeway) {
2088 /* We had flipped over to answer a callwait and now it's gone */
2089 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2090 /* Move to the call-wait, but un-own us until they flip back. */
2091 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2092 unalloc_sub(p, SUB_CALLWAIT);
2095 /* The three way hung up, but we still have a call wait */
2096 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2097 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2098 unalloc_sub(p, SUB_THREEWAY);
2099 if (p->subs[SUB_REAL].inthreeway) {
2100 /* This was part of a three way call. Immediately make way for
2102 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2103 p->owner = p->subs[SUB_REAL].owner;
2105 /* This call hasn't been completed yet... Set owner to NULL */
2106 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2109 p->subs[SUB_REAL].inthreeway = 0;
2111 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2112 /* Move to the call-wait and switch back to them. */
2113 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2114 unalloc_sub(p, SUB_CALLWAIT);
2115 p->owner = p->subs[SUB_REAL].owner;
2116 if (p->owner->_state != AST_STATE_UP)
2117 p->subs[SUB_REAL].needanswer = 1;
2118 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2119 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2120 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2121 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2122 unalloc_sub(p, SUB_THREEWAY);
2123 if (p->subs[SUB_REAL].inthreeway) {
2124 /* This was part of a three way call. Immediately make way for
2126 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2127 p->owner = p->subs[SUB_REAL].owner;
2129 /* This call hasn't been completed yet... Set owner to NULL */
2130 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2133 p->subs[SUB_REAL].inthreeway = 0;
2135 } else if (index == SUB_CALLWAIT) {
2136 /* Ditch the holding callwait call, and immediately make it availabe */
2137 if (p->subs[SUB_CALLWAIT].inthreeway) {
2138 /* This is actually part of a three way, placed on hold. Place the third part
2139 on music on hold now */
2140 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2141 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2142 p->subs[SUB_THREEWAY].inthreeway = 0;
2143 /* Make it the call wait now */
2144 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2145 unalloc_sub(p, SUB_THREEWAY);
2147 unalloc_sub(p, SUB_CALLWAIT);
2148 } else if (index == SUB_THREEWAY) {
2149 if (p->subs[SUB_CALLWAIT].inthreeway) {
2150 /* The other party of the three way call is currently in a call-wait state.
2151 Start music on hold for them, and take the main guy out of the third call */
2152 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2153 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2154 p->subs[SUB_CALLWAIT].inthreeway = 0;
2156 p->subs[SUB_REAL].inthreeway = 0;
2157 /* If this was part of a three way call index, let us make
2158 another three way call */
2159 unalloc_sub(p, SUB_THREEWAY);
2161 /* This wasn't any sort of call, but how are we an index? */
2162 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2167 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2170 p->distinctivering = 0;
2171 p->confirmanswer = 0;
2177 p->onhooktime = time(NULL);
2183 ast_dsp_free(p->dsp);
2187 law = ZT_LAW_DEFAULT;
2188 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2190 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2191 /* Perform low level hangup if no owner left */
2194 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2195 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2196 if (!pri_grab(p, p->pri)) {
2197 if (p->alreadyhungup) {
2198 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2199 pri_hangup(p->pri->pri, p->call, -1);
2202 p->bearer->call = NULL;
2204 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2205 int icause = ast->hangupcause ? ast->hangupcause : -1;
2206 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2207 p->alreadyhungup = 1;
2209 p->bearer->alreadyhungup = 1;
2212 icause = atoi(cause);
2214 pri_hangup(p->pri->pri, p->call, icause);
2217 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2220 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2225 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2232 if (p->sig == SIG_R2) {
2234 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2242 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2243 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2245 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2251 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2254 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2256 /* If they're off hook, try playing congestion */
2257 if ((par.rxisoffhook) && (!p->radio))
2258 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2260 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2266 /* Make sure we're not made available for at least two seconds assuming
2267 we were actually used for an inbound or outbound call. */
2268 if (ast->_state != AST_STATE_RESERVED) {
2269 time(&p->guardtime);
2274 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2281 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2282 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2286 p->callwaiting = p->permcallwaiting;
2287 p->hidecallerid = p->permhidecallerid;
2292 /* Restore data mode */
2293 if (p->sig == SIG_PRI) {
2295 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2299 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2300 /* Free up the bearer channel as well, and
2301 don't use its file descriptor anymore */
2302 update_conf(p->bearer);
2303 reset_conf(p->bearer);
2304 p->bearer->owner = NULL;
2305 p->bearer->realcall = NULL;
2307 p->subs[SUB_REAL].zfd = -1;
2315 p->callwaitingrepeat = 0;
2317 ast->pvt->pvt = NULL;
2318 ast_mutex_unlock(&p->lock);
2319 ast_mutex_lock(&usecnt_lock);
2322 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2323 ast_mutex_unlock(&usecnt_lock);
2324 ast_update_use_count();
2325 if (option_verbose > 2)
2326 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2328 ast_mutex_lock(&iflock);
2334 destroy_channel(prev, tmp, 0);
2342 ast_mutex_unlock(&iflock);
2346 static int zt_answer(struct ast_channel *ast)
2348 struct zt_pvt *p = ast->pvt->pvt;
2351 int oldstate = ast->_state;
2352 ast_setstate(ast, AST_STATE_UP);
2353 ast_mutex_lock(&p->lock);
2354 index = zt_get_index(ast, p, 0);
2357 /* nothing to do if a radio channel */
2359 ast_mutex_unlock(&p->lock);
2378 case SIG_SF_FEATDMF:
2383 /* Pick up the line */
2384 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2385 if(p->hanguponpolarityswitch) {
2386 gettimeofday(&p->polaritydelaytv, NULL);
2388 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2389 tone_zone_play_tone(p->subs[index].zfd, -1);
2391 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2392 if (oldstate == AST_STATE_RINGING) {
2393 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2394 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2395 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2396 p->owner = p->subs[SUB_REAL].owner;
2399 if (p->sig & __ZT_SIG_FXS) {
2406 /* Send a pri acknowledge */
2407 if (!pri_grab(p, p->pri)) {
2409 res = pri_answer(p->pri->pri, p->call, 0, 1);
2412 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2419 res = mfcr2_AnswerCall(p->r2, NULL);
2421 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2425 ast_mutex_unlock(&p->lock);
2428 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2431 ast_mutex_unlock(&p->lock);
2435 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2440 struct zt_pvt *p = chan->pvt->pvt;
2443 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2444 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2450 if ((!cp) || (datalen < 1))
2456 case AST_OPTION_TONE_VERIFY:
2461 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2462 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2465 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2466 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2469 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2470 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2474 case AST_OPTION_TDD: /* turn on or off TDD */
2475 if (!*cp) { /* turn it off */
2476 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2477 if (p->tdd) tdd_free(p->tdd);
2483 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2484 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2487 /* otherwise, turn it on */
2488 if (!p->didtdd) { /* if havent done it yet */
2489 unsigned char mybuf[41000],*buf;
2490 int size,res,fd,len;
2492 struct pollfd fds[1];
2494 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2495 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2497 index = zt_get_index(chan, p, 0);
2499 ast_log(LOG_WARNING, "No index in TDD?\n");
2502 fd = p->subs[index].zfd;
2504 if (ast_check_hangup(chan)) return -1;
2506 if (size > READ_SIZE)
2509 fds[0].events = POLLPRI | POLLOUT;
2510 res = poll(fds, 1, -1);
2512 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2515 /* if got exception */
2516 if (fds[0].revents & POLLPRI) return -1;
2517 if (!(fds[0].revents & POLLOUT)) {
2518 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2521 res = write(fd, buf, size);
2523 if (res == -1) return -1;
2524 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2530 p->didtdd = 1; /* set to have done it now */
2532 if (*cp == 2) { /* Mate mode */
2533 if (p->tdd) tdd_free(p->tdd);
2538 if (!p->tdd) { /* if we dont have one yet */
2539 p->tdd = tdd_new(); /* allocate one */
2542 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2545 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2550 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2553 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2555 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2558 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2564 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2567 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2568 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2575 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2577 /* Unlink a specific slave or all slaves/masters from a given master */
2583 ast_mutex_lock(&master->lock);
2585 while(ast_mutex_trylock(&slave->lock)) {
2586 ast_mutex_unlock(&master->lock);
2588 ast_mutex_lock(&master->lock);
2593 for (x=0;x<MAX_SLAVES;x++) {
2594 if (master->slaves[x]) {
2595 if (!slave || (master->slaves[x] == slave)) {
2596 /* Take slave out of the conference */
2597 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2598 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2599 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2600 master->slaves[x]->master = NULL;
2601 master->slaves[x] = NULL;
2606 master->inconference = 0;
2609 if (master->master) {
2610 /* Take master out of the conference */
2611 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2612 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2614 for (x=0;x<MAX_SLAVES;x++) {
2615 if (master->master->slaves[x] == master)
2616 master->master->slaves[x] = NULL;
2617 else if (master->master->slaves[x])
2621 master->master->inconference = 0;
2623 master->master = NULL;
2625 update_conf(master);
2628 ast_mutex_unlock(&slave->lock);
2629 ast_mutex_unlock(&master->lock);
2633 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2635 if (!slave || !master) {
2636 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2639 for (x=0;x<MAX_SLAVES;x++) {
2640 if (!master->slaves[x]) {
2641 master->slaves[x] = slave;
2645 if (x >= MAX_SLAVES) {
2646 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2647 master->slaves[MAX_SLAVES - 1] = slave;
2650 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2651 slave->master = master;
2653 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2656 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2658 struct ast_channel *who = NULL, *cs[3];
2659 struct zt_pvt *p0, *p1, *op0, *op1;
2660 struct zt_pvt *master=NULL, *slave=NULL;
2661 struct ast_frame *f;
2666 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2667 int os1 = -1, os2 = -1;
2668 struct ast_channel *oc1, *oc2;
2670 int triedtopribridge = 0;
2671 q931_call *q931c0 = NULL, *q931c1 = NULL;
2675 /* if need DTMF, cant native bridge */
2676 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2680 ast_mutex_lock(&c0->lock);
2681 ast_mutex_lock(&c1->lock);
2685 /* cant do pseudo-channels here */
2686 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2687 ast_mutex_unlock(&c0->lock);
2688 ast_mutex_unlock(&c1->lock);
2692 op0 = p0 = c0->pvt->pvt;
2693 op1 = p1 = c1->pvt->pvt;
2696 oi1 = zt_get_index(c0, p0, 0);
2697 oi2 = zt_get_index(c1, p1, 0);
2700 if ((oi1 < 0) || (oi2 < 0)) {
2701 ast_mutex_unlock(&c0->lock);
2702 ast_mutex_unlock(&c1->lock);
2708 ast_mutex_lock(&p0->lock);
2709 if (ast_mutex_trylock(&p1->lock)) {
2710 /* Don't block, due to potential for deadlock */
2711 ast_mutex_unlock(&p0->lock);
2712 ast_mutex_unlock(&c0->lock);
2713 ast_mutex_unlock(&c1->lock);
2714 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2717 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2718 if (!p0->owner || !p1->owner) {
2719 /* Currently unowned -- Do nothing. */
2722 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2723 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2727 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2732 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2733 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,
2734 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2737 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2738 if (p1->subs[SUB_THREEWAY].inthreeway) {
2744 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2745 if (p0->subs[SUB_THREEWAY].inthreeway) {
2751 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2752 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2753 don't put us in anything */
2754 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2760 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2761 /* Same as previous */
2762 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2769 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2770 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2771 if (master && slave) {
2772 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2773 in an active threeway call with a channel that is ringing, we should
2774 indicate ringing. */
2775 if ((oi2 == SUB_THREEWAY) &&
2776 p1->subs[SUB_THREEWAY].inthreeway &&
2777 p1->subs[SUB_REAL].owner &&
2778 p1->subs[SUB_REAL].inthreeway &&
2779 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2780 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2781 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2782 os2 = p1->subs[SUB_REAL].owner->_state;
2784 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2785 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2787 if ((oi1 == SUB_THREEWAY) &&
2788 p0->subs[SUB_THREEWAY].inthreeway &&
2789 p0->subs[SUB_REAL].owner &&
2790 p0->subs[SUB_REAL].inthreeway &&
2791 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2792 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2793 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2794 os1 = p0->subs[SUB_REAL].owner->_state;
2796 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2797 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2799 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2800 if (!p0->echocanbridged || !p1->echocanbridged) {
2801 /* Disable echo cancellation if appropriate */
2806 zt_link(slave, master);
2807 master->inconference = inconf;
2808 } else if (!nothingok)
2809 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2813 t1 = p0->subs[SUB_REAL].inthreeway;
2814 t2 = p1->subs[SUB_REAL].inthreeway;
2816 ast_mutex_unlock(&p0->lock);
2817 ast_mutex_unlock(&p1->lock);
2819 ast_mutex_unlock(&c0->lock);
2820 ast_mutex_unlock(&c1->lock);
2822 /* Native bridge failed */
2823 if ((!master || !slave) && !nothingok) {
2835 /* Here's our main loop... Start by locking things, looking for private parts,
2836 and then balking if anything is wrong */
2837 ast_mutex_lock(&c0->lock);
2838 ast_mutex_lock(&c1->lock);
2845 if (q931c0 && q931c1 && !triedtopribridge) {
2846 pri_channel_bridge(q931c0, q931c1);
2847 triedtopribridge = 1;
2851 i1 = zt_get_index(c0, p0, 1);
2853 i2 = zt_get_index(c1, p1, 1);
2854 ast_mutex_unlock(&c0->lock);
2855 ast_mutex_unlock(&c1->lock);
2856 if ((op0 != p0) || (op1 != p1) ||
2857 (ofd1 != c0->fds[0]) ||
2858 (ofd2 != c1->fds[0]) ||
2859 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2860 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2861 (oc1 != p0->owner) ||
2862 (oc2 != p1->owner) ||
2863 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2864 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2867 if (slave && master)
2868 zt_unlink(slave, master, 1);
2869 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2870 op0->channel, oi1, op1->channel, oi2);
2878 who = ast_waitfor_n(cs, 2, &to);
2880 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2883 if (who->pvt->pvt == op0)
2884 op0->ignoredtmf = 1;
2885 else if (who->pvt->pvt == op1)
2886 op1->ignoredtmf = 1;
2888 if (who->pvt->pvt == op0)
2889 op0->ignoredtmf = 0;
2890 else if (who->pvt->pvt == op1)
2891 op1->ignoredtmf = 0;
2895 if (slave && master)
2896 zt_unlink(slave, master, 1);
2903 if (f->frametype == AST_FRAME_CONTROL) {
2906 if (slave && master)
2907 zt_unlink(slave, master, 1);
2910 if (f->frametype == AST_FRAME_DTMF) {
2911 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2912 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2915 if (slave && master)
2916 zt_unlink(slave, master, 1);
2918 } else if ((who == c0) && p0->pulsedial) {
2920 } else if ((who == c1) && p1->pulsedial) {
2926 /* Swap who gets priority */
2933 static int zt_indicate(struct ast_channel *chan, int condition);
2935 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2937 struct zt_pvt *p = newchan->pvt->pvt;
2939 ast_mutex_lock(&p->lock);
2940 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2941 if (p->owner == oldchan) {
2945 if (p->subs[x].owner == oldchan) {
2947 zt_unlink(NULL, p, 0);
2948 p->subs[x].owner = newchan;
2950 if (newchan->_state == AST_STATE_RINGING)
2951 zt_indicate(newchan, AST_CONTROL_RINGING);
2953 ast_mutex_unlock(&p->lock);
2957 static int zt_ring_phone(struct zt_pvt *p)
2961 /* Make sure our transmit state is on hook */
2964 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2967 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2969 printf("Res: %d, error: %s\n", res, strerror(errno));
2975 /* Wait just in case */
2982 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));