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/config.h>
19 #include <asterisk/logger.h>
20 #include <asterisk/module.h>
21 #include <asterisk/pbx.h>
22 #include <asterisk/options.h>
23 #include <asterisk/file.h>
24 #include <asterisk/ulaw.h>
25 #include <asterisk/alaw.h>
26 #include <asterisk/callerid.h>
27 #include <asterisk/adsi.h>
28 #include <asterisk/cli.h>
29 #include <asterisk/cdr.h>
30 #include <asterisk/features.h>
31 #include <asterisk/musiconhold.h>
32 #include <asterisk/say.h>
33 #include <asterisk/tdd.h>
34 #include <asterisk/app.h>
35 #include <asterisk/dsp.h>
36 #include <asterisk/astdb.h>
37 #include <asterisk/manager.h>
38 #include <asterisk/causes.h>
39 #include <asterisk/term.h>
40 #include <asterisk/utils.h>
41 #include <sys/signal.h>
48 #include <sys/ioctl.h>
50 #include <linux/zaptel.h>
53 #endif /* __linux__ */
59 #ifndef PRI_REDIRECTING_REASON
60 #error "You need newer libpri"
67 #include "../asterisk.h"
70 #error "Your zaptel is too old. please cvs update"
74 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
75 * the user hangs up to reset the state machine so ring works properly.
76 * This is used to be able to support kewlstart by putting the zhone in
77 * groundstart mode since their forward disconnect supervision is entirely
78 * broken even though their documentation says it isn't and their support
79 * is entirely unwilling to provide any assistance with their channel banks
80 * even though their web site says they support their products for life.
83 /* #define ZHONE_HACK */
86 * Define if you want to check the hook state for an FXO (FXS signalled) interface
87 * before dialing on it. Certain FXO interfaces always think they're out of
88 * service with this method however.
90 /* #define ZAP_CHECK_HOOKSTATE */
92 /* Typically, how many rings before we should send Caller*ID */
93 #define DEFAULT_CIDRINGS 1
95 #define CHANNEL_PSEUDO -12
97 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
99 static const char desc[] = "Zapata Telephony"
108 static const char tdesc[] = "Zapata Telephony Driver"
117 static const char type[] = "Zap";
118 static const char config[] = "zapata.conf";
120 #define SIG_EM ZT_SIG_EM
121 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
122 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
123 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
124 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
125 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
126 #define SIG_FXSLS ZT_SIG_FXSLS
127 #define SIG_FXSGS ZT_SIG_FXSGS
128 #define SIG_FXSKS ZT_SIG_FXSKS
129 #define SIG_FXOLS ZT_SIG_FXOLS
130 #define SIG_FXOGS ZT_SIG_FXOGS
131 #define SIG_FXOKS ZT_SIG_FXOKS
132 #define SIG_PRI ZT_SIG_CLEAR
133 #define SIG_R2 ZT_SIG_CAS
134 #define SIG_SF ZT_SIG_SF
135 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
136 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
137 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
138 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
139 #define SIG_EM_E1 ZT_SIG_EM_E1
140 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
141 #define SIG_GR303FXSKS (0x200000 | ZT_SIG_FXSKS)
144 #define NUM_DCHANS 4 /* No more than 4 d-channels */
145 #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
147 #define CHAN_PSEUDO -2
149 #define DCHAN_PROVISIONED (1 << 0)
150 #define DCHAN_NOTINALARM (1 << 1)
151 #define DCHAN_UP (1 << 2)
153 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
155 static int cur_emdigitwait = 250; /* Wait time in ms for digits on EM channel */
157 static char context[AST_MAX_EXTENSION] = "default";
158 static char cid_num[256] = "";
159 static char cid_name[256] = "";
161 static char language[MAX_LANGUAGE] = "";
162 static char musicclass[MAX_LANGUAGE] = "";
163 static char progzone[10]= "";
165 static int usedistinctiveringdetection = 0;
167 static int use_callerid = 1;
168 static int cid_signalling = CID_SIG_BELL;
169 static int cid_start = CID_START_RING;
170 static int zaptrcallerid = 0;
171 static int cur_signalling = -1;
173 static ast_group_t cur_group = 0;
174 static ast_group_t cur_callergroup = 0;
175 static ast_group_t cur_pickupgroup = 0;
176 static int relaxdtmf = 0;
178 static int immediate = 0;
180 static int stripmsd = 0;
182 static int callwaiting = 0;
184 static int callwaitingcallerid = 0;
186 static int hidecallerid = 0;
188 static int restrictcid = 0;
190 static int use_callingpres = 0;
192 static int callreturn = 0;
194 static int threewaycalling = 0;
196 static int transfer = 0;
198 static int canpark = 0;
200 static int cancallforward = 0;
202 static float rxgain = 0.0;
204 static float txgain = 0.0;
206 static int tonezone = -1;
208 static int echocancel;
210 static int echotraining;
214 static int echocanbridged = 0;
216 static int busydetect = 0;
218 static int busycount = 3;
220 static int callprogress = 0;
222 static char accountcode[20] = "";
224 static char mailbox[AST_MAX_EXTENSION];
226 static int amaflags = 0;
230 static int numbufs = 4;
232 static int cur_prewink = -1;
233 static int cur_preflash = -1;
234 static int cur_wink = -1;
235 static int cur_flash = -1;
236 static int cur_start = -1;
237 static int cur_rxwink = -1;
238 static int cur_rxflash = -1;
239 static int cur_debounce = -1;
241 static int priindication_oob = 0;
244 static int minunused = 2;
245 static int minidle = 0;
246 static char idleext[AST_MAX_EXTENSION];
247 static char idledial[AST_MAX_EXTENSION];
248 static int overlapdial = 0;
249 static char internationalprefix[10] = "";
250 static char nationalprefix[10] = "";
251 static char localprefix[20] = "";
252 static char privateprefix[20] = "";
253 static char unknownprefix[20] = "";
254 static long resetinterval = 3600; /* How often (in seconds) to reset unused channels. Default 1 hour. */
255 static struct ast_channel inuse = { "GR-303InUse" };
256 #ifdef PRI_GETSET_TIMERS
257 static int pritimers[PRI_MAX_TIMERS];
261 /* Wait up to 16 seconds for first digit (FXO logic) */
262 static int firstdigittimeout = 16000;
264 /* How long to wait for following digits (FXO logic) */
265 static int gendigittimeout = 8000;
267 /* How long to wait for an extra digit, if there is an ambiguous match */
268 static int matchdigittimeout = 3000;
270 static int usecnt =0;
271 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
273 /* Protect the interface list (of zt_pvt's) */
274 AST_MUTEX_DEFINE_STATIC(iflock);
276 static int ifcount = 0;
278 /* Whether we answer on a Polarity Switch event */
279 static int answeronpolarityswitch = 0;
281 /* Whether we hang up on a Polarity Switch event */
282 static int hanguponpolarityswitch = 0;
284 /* How long (ms) to ignore Polarity Switch events after we answer a call */
285 static int polarityonanswerdelay = 600;
287 /* When to send the CallerID signals (rings) */
288 static int sendcalleridafter = DEFAULT_CIDRINGS;
290 /* Protect the monitoring thread, so only one process can kill or start it, and not
291 when it's doing something critical. */
292 AST_MUTEX_DEFINE_STATIC(monlock);
294 /* This is the thread for the monitor which checks for input on the channels
295 which are not currently in use. */
296 static pthread_t monitor_thread = AST_PTHREADT_NULL;
298 static int restart_monitor(void);
300 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
302 static int zt_sendtext(struct ast_channel *c, char *text);
304 static inline int zt_get_event(int fd)
306 /* Avoid the silly zt_getevent which ignores a bunch of events */
308 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
312 static inline int zt_wait_event(int fd)
314 /* Avoid the silly zt_waitevent which ignores a bunch of events */
316 i = ZT_IOMUX_SIGEVENT;
317 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
318 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
322 /* Chunk size to read -- we use 20ms chunks to make things happy. */
323 #define READ_SIZE 160
325 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
326 #define MASK_INUSE (1 << 1) /* Channel currently in use */
328 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
329 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
330 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
331 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
332 #define RINGT ( (8000 * 8) / READ_SIZE)
338 static int r2prot = -1;
344 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8))
345 #define PRI_CHANNEL(p) ((p) & 0xff)
346 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
349 pthread_t master; /* Thread of master */
350 ast_mutex_t lock; /* Mutex */
351 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
352 char idlecontext[AST_MAX_EXTENSION]; /* What context to use for idle */
353 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
354 int minunused; /* Min # of channels to keep empty */
355 int minidle; /* Min # of "idling" calls to keep active */
356 int nodetype; /* Node type */
357 int switchtype; /* Type of switch to emulate */
358 int nsf; /* Network-Specific Facilities */
359 int dialplan; /* Dialing plan */
360 int localdialplan; /* Local dialing plan */
361 char internationalprefix[10]; /* country access code ('00' for european dialplans) */
362 char nationalprefix[10]; /* area access code ('0' for european dialplans) */
363 char localprefix[20]; /* area access code + area code ('0'+area code for european dialplans) */
364 char privateprefix[20]; /* for private dialplans */
365 char unknownprefix[20]; /* for unknown dialplans */
366 int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
367 int trunkgroup; /* What our trunkgroup is */
368 int mastertrunkgroup; /* What trunk group is our master */
369 int prilogicalspan; /* Logical span number within trunk group */
370 int numchans; /* Num of channels we represent */
371 int overlapdial; /* In overlap dialing mode */
372 struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
373 int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
374 struct pri *pri; /* Currently active D-channel */
376 int fds[NUM_DCHANS]; /* FD's for d-channels */
381 time_t lastreset; /* time when unused channels were last reset */
382 long resetinterval; /* Interval (in seconds) for resetting unused channels */
383 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
384 struct zt_pvt *crvs; /* Member CRV structs */
385 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
389 static struct zt_pri pris[NUM_SPANS];
391 static int pritype = PRI_CPE;
394 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
396 #define DEFAULT_PRI_DEBUG 0
399 static inline void pri_rel(struct zt_pri *pri)
401 ast_mutex_unlock(&pri->lock);
404 static int switchtype = PRI_SWITCH_NI2;
405 static int nsf = PRI_NSF_NONE;
406 static int dialplan = PRI_NATIONAL_ISDN + 1;
407 static int localdialplan = PRI_NATIONAL_ISDN + 1;
410 /* Shut up the compiler */
414 #define SUB_REAL 0 /* Active call */
415 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
416 #define SUB_THREEWAY 2 /* Three-way call */
418 /* Polarity states */
419 #define POLARITY_IDLE 0
420 #define POLARITY_REV 1
423 static struct zt_distRings drings;
425 struct distRingData {
428 struct ringContextData {
429 char contextData[AST_MAX_EXTENSION];
431 struct zt_distRings {
432 struct distRingData ringnum[3];
433 struct ringContextData ringContext[3];
436 static char *subnames[] = {
442 struct zt_subchannel {
444 struct ast_channel *owner;
446 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
447 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
459 #define CONF_USER_REAL (1 << 0)
460 #define CONF_USER_THIRDCALL (1 << 1)
464 static struct zt_pvt {
466 struct ast_channel *owner; /* Our current active owner (if applicable) */
467 /* Up to three channels can be associated with this call */
469 struct zt_subchannel sub_unused; /* Just a safety precaution */
470 struct zt_subchannel subs[3]; /* Sub-channels */
471 struct zt_confinfo saveconf; /* Saved conference info */
473 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
474 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
475 int inconference; /* If our real should be in the conference */
477 int sig; /* Signalling style */
478 int radio; /* radio type */
479 int firstradio; /* first radio flag */
482 int tonezone; /* tone zone for this chan, or -1 for default */
483 struct zt_pvt *next; /* Next channel in list */
484 struct zt_pvt *prev; /* Prev channel in list */
486 struct zt_distRings drings;
487 int usedistinctiveringdetection;
489 char context[AST_MAX_EXTENSION];
490 char defcontext[AST_MAX_EXTENSION];
491 char exten[AST_MAX_EXTENSION];
492 char language[MAX_LANGUAGE];
493 char musicclass[MAX_LANGUAGE];
494 char cid_num[AST_MAX_EXTENSION];
495 int cid_ton; /* Type Of Number (TON) */
496 char cid_name[AST_MAX_EXTENSION];
497 char lastcid_num[AST_MAX_EXTENSION];
498 char lastcid_name[AST_MAX_EXTENSION];
499 char *origcid_num; /* malloced original callerid */
500 char *origcid_name; /* malloced original callerid */
501 char callwait_num[AST_MAX_EXTENSION];
502 char callwait_name[AST_MAX_EXTENSION];
503 char rdnis[AST_MAX_EXTENSION];
504 char dnid[AST_MAX_EXTENSION];
507 int confno; /* Our conference */
508 int confusers; /* Who is using our conference */
509 int propconfno; /* Propagated conference number */
510 ast_group_t callgroup;
511 ast_group_t pickupgroup;
512 int immediate; /* Answer before getting digits? */
513 int channel; /* Channel Number or CRV */
514 int span; /* Span number */
516 time_t guardtime; /* Must wait this much time before using for new call */
518 int use_callerid; /* Whether or not to use caller id on this channel */
519 int cid_signalling; /* CID signalling type bell202 or v23 */
520 int cid_start; /* CID start indicator, polarity or ring */
523 int permhidecallerid; /* Whether to hide our outgoing caller ID or not */
524 int restrictcid; /* Whether restrict the callerid -> only send ANI */
525 int use_callingpres; /* Whether to use the callingpres the calling switch sends */
526 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
527 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
528 int cidcwexpire; /* When to expire our muting for CID/CW */
529 unsigned char *cidspill;
545 int callwaitingcallerid;
555 int priindication_oob;
556 struct timeval flashtime; /* Last flash-hook time */
558 int cref; /* Call reference number */
559 ZT_DIAL_OPERATION dop;
563 char accountcode[20]; /* Account code */
564 int amaflags; /* AMA Flags */
565 char didtdd; /* flag to say its done it once */
566 struct tdd_state *tdd; /* TDD flag */
569 char call_forward[AST_MAX_EXTENSION];
570 char mailbox[AST_MAX_EXTENSION];
575 int confirmanswer; /* Wait for '#' to confirm answer */
576 int distinctivering; /* Which distinctivering to use */
577 int cidrings; /* Which ring to deliver CID on */
579 int faxhandled; /* Has a fax tone already been handled? */
581 char mate; /* flag to say its in MATE mode */
582 int pulsedial; /* whether a pulse dial phone is detected */
583 int dtmfrelax; /* whether to run in relaxed DTMF mode */
585 int zaptrcallerid; /* should we use the callerid from incoming call on zap transfer or not */
587 int answeronpolarityswitch;
588 int hanguponpolarityswitch;
589 int polarityonanswerdelay;
590 struct timeval polaritydelaytv;
591 int sendcalleridafter;
594 struct zt_pvt *bearer;
595 struct zt_pvt *realcall;
603 int setup_ack; /* whether we received SETUP_ACKNOWLEDGE or not */
614 } *iflist = NULL, *ifend = NULL;
616 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
617 static int zt_digit(struct ast_channel *ast, char digit);
618 static int zt_sendtext(struct ast_channel *c, char *text);
619 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
620 static int zt_hangup(struct ast_channel *ast);
621 static int zt_answer(struct ast_channel *ast);
622 struct ast_frame *zt_read(struct ast_channel *ast);
623 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
624 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
625 struct ast_frame *zt_exception(struct ast_channel *ast);
626 static int zt_indicate(struct ast_channel *chan, int condition);
627 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
628 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
630 static const struct ast_channel_tech zap_tech = {
632 .description = tdesc,
633 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
634 .requester = zt_request,
635 .send_digit = zt_digit,
636 .send_text = zt_sendtext,
643 .exception = zt_exception,
644 .indicate = zt_indicate,
646 .setoption = zt_setoption,
650 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
652 #define GET_CHANNEL(p) ((p)->channel)
655 struct zt_pvt *round_robin[32];
658 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
661 /* Grab the lock first */
663 res = ast_mutex_trylock(&pri->lock);
665 ast_mutex_unlock(&pvt->lock);
666 /* Release the lock and try again */
668 ast_mutex_lock(&pvt->lock);
671 /* Then break the poll */
672 pthread_kill(pri->master, SIGURG);
677 #define NUM_CADENCE_MAX 25
678 static int num_cadence = 4;
679 static int user_has_defined_cadences = 0;
681 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
682 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
683 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
684 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
685 { { 1000, 500, 2500, 5000 } }, /* Long ring */
688 int receivedRingT; /* Used to find out what ringtone we are on */
690 /* cidrings says in which pause to transmit the cid information, where the first pause
691 * is 1, the second pause is 2 and so on.
694 static int cidrings[NUM_CADENCE_MAX] = {
695 2, /* Right after first long ring */
696 4, /* Right after long part */
697 3, /* After third chirp */
698 2, /* Second spell */
701 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
702 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
704 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
705 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
707 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
710 if (p->subs[0].owner == ast)
712 else if (p->subs[1].owner == ast)
714 else if (p->subs[2].owner == ast)
719 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
725 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
727 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
730 struct ast_frame null = { AST_FRAME_NULL, };
733 ast_mutex_unlock(&pri->lock);
736 if (p->subs[a].owner) {
737 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
738 ast_mutex_unlock(&p->lock);
740 ast_mutex_lock(&p->lock);
742 ast_queue_frame(p->subs[a].owner, &null);
743 ast_mutex_unlock(&p->subs[a].owner->lock);
751 ast_mutex_lock(&pri->lock);
756 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
758 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
761 /* We must unlock the PRI to avoid the possibility of a deadlock */
764 ast_mutex_unlock(&pri->lock);
768 if (ast_mutex_trylock(&p->owner->lock)) {
769 ast_mutex_unlock(&p->lock);
771 ast_mutex_lock(&p->lock);
773 ast_queue_frame(p->owner, f);
774 ast_mutex_unlock(&p->owner->lock);
782 ast_mutex_lock(&pri->lock);
786 static int restore_gains(struct zt_pvt *p);
788 static void swap_subs(struct zt_pvt *p, int a, int b)
792 struct ast_channel *towner;
794 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
796 tchan = p->subs[a].chan;
797 towner = p->subs[a].owner;
798 tinthreeway = p->subs[a].inthreeway;
800 p->subs[a].chan = p->subs[b].chan;
801 p->subs[a].owner = p->subs[b].owner;
802 p->subs[a].inthreeway = p->subs[b].inthreeway;
804 p->subs[b].chan = tchan;
805 p->subs[b].owner = towner;
806 p->subs[b].inthreeway = tinthreeway;
808 if (p->subs[a].owner)
809 p->subs[a].owner->fds[0] = p->subs[a].zfd;
810 if (p->subs[b].owner)
811 p->subs[b].owner->fds[0] = p->subs[b].zfd;
812 wakeup_sub(p, a, NULL);
813 wakeup_sub(p, b, NULL);
816 static int zt_open(char *fn)
824 for (x=0;x<strlen(fn);x++) {
825 if (!isdigit(fn[x])) {
833 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
836 fn = "/dev/zap/channel";
838 fd = open(fn, O_RDWR | O_NONBLOCK);
840 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
844 if (ioctl(fd, ZT_SPECIFY, &chan)) {
848 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
853 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
857 static void zt_close(int fd)
863 int zt_setlinear(int zfd, int linear)
866 res = ioctl(zfd, ZT_SETLINEAR, &linear);
873 int zt_setlaw(int zfd, int law)
876 res = ioctl(zfd, ZT_SETLAW, &law);
882 static int alloc_sub(struct zt_pvt *p, int x)
886 if (p->subs[x].zfd < 0) {
887 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
888 if (p->subs[x].zfd > -1) {
889 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
891 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
892 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
893 bi.numbufs = numbufs;
894 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
896 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
899 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
900 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
901 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
902 zt_close(p->subs[x].zfd);
907 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
910 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
913 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
917 static int unalloc_sub(struct zt_pvt *p, int x)
920 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
923 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
924 if (p->subs[x].zfd > -1) {
925 zt_close(p->subs[x].zfd);
928 p->subs[x].linear = 0;
930 p->subs[x].owner = NULL;
931 p->subs[x].inthreeway = 0;
932 p->polarity = POLARITY_IDLE;
933 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
937 static int zt_digit(struct ast_channel *ast, char digit)
939 ZT_DIAL_OPERATION zo;
944 ast_mutex_lock(&p->lock);
945 index = zt_get_index(ast, p, 0);
946 if ((index == SUB_REAL) && p->owner) {
948 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
950 if (!pri_grab(p, p->pri)) {
951 pri_information(p->pri->pri,p->call,digit);
954 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
955 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
956 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
957 res = strlen(p->dialdest);
958 p->dialdest[res++] = digit;
959 p->dialdest[res] = '\0';
965 zo.op = ZT_DIAL_OP_APPEND;
967 zo.dialstr[1] = digit;
969 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
970 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
975 ast_mutex_unlock(&p->lock);
979 static char *events[] = {
992 "Hook Transition Complete",
1004 { ZT_ALARM_RED, "Red Alarm" },
1005 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1006 { ZT_ALARM_BLUE, "Blue Alarm" },
1007 { ZT_ALARM_RECOVER, "Recovering" },
1008 { ZT_ALARM_LOOPBACK, "Loopback" },
1009 { ZT_ALARM_NOTOPEN, "Not Open" },
1010 { ZT_ALARM_NONE, "None" },
1013 static char *alarm2str(int alarm)
1016 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
1017 if (alarms[x].alarm & alarm)
1018 return alarms[x].name;
1020 return alarm ? "Unknown Alarm" : "No Alarm";
1023 static char *event2str(int event)
1025 static char buf[256];
1026 if ((event < 18) && (event > -1))
1027 return events[event];
1028 sprintf(buf, "Event %d", event); /* safe */
1032 static char *dialplan2str(int dialplan)
1034 if (dialplan == -1) {
1035 return("Dynamically set dialplan in ISDN");
1037 return(pri_plan2str(dialplan));
1041 static int str2r2prot(char *swtype)
1043 if (!strcasecmp(swtype, "ar"))
1044 return MFCR2_PROT_ARGENTINA;
1046 if (!strcasecmp(swtype, "cn"))
1047 return MFCR2_PROT_CHINA;
1049 if (!strcasecmp(swtype, "kr"))
1050 return MFCR2_PROT_KOREA;
1056 static char *sig2str(int sig)
1058 static char buf[256];
1061 return "E & M Immediate";
1063 return "E & M Wink";
1067 return "Feature Group D (DTMF)";
1069 return "Feature Group D (MF)";
1071 return "Feature Group B (MF)";
1075 return "FXS Loopstart";
1077 return "FXS Groundstart";
1079 return "FXS Kewlstart";
1081 return "FXO Loopstart";
1083 return "FXO Groundstart";
1085 return "FXO Kewlstart";
1087 return "PRI Signalling";
1089 return "R2 Signalling";
1091 return "SF (Tone) Signalling Immediate";
1093 return "SF (Tone) Signalling Wink";
1095 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1096 case SIG_SF_FEATDMF:
1097 return "SF (Tone) Signalling with Feature Group D (MF)";
1099 return "SF (Tone) Signalling with Feature Group B (MF)";
1100 case SIG_GR303FXOKS:
1101 return "GR-303 Signalling with FXOKS";
1102 case SIG_GR303FXSKS:
1103 return "GR-303 Signalling with FXSKS";
1105 return "Pseudo Signalling";
1107 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1112 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1114 /* If the conference already exists, and we're already in it
1115 don't bother doing anything */
1118 memset(&zi, 0, sizeof(zi));
1121 if (slavechannel > 0) {
1122 /* If we have only one slave, do a digital mon */
1123 zi.confmode = ZT_CONF_DIGITALMON;
1124 zi.confno = slavechannel;
1127 /* Real-side and pseudo-side both participate in conference */
1128 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1129 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1131 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1132 zi.confno = p->confno;
1134 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1138 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1139 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1142 if (slavechannel < 1) {
1143 p->confno = zi.confno;
1145 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1146 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1150 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1152 /* If they're listening to our channel, they're ours */
1153 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1155 /* If they're a talker on our (allocated) conference, they're ours */
1156 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1161 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1164 if (/* Can't delete if there's no zfd */
1166 /* Don't delete from the conference if it's not our conference */
1168 /* Don't delete if we don't think it's conferenced at all (implied) */
1170 memset(&zi, 0, sizeof(zi));
1174 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1175 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1178 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1179 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1183 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1187 struct zt_pvt *slave = NULL;
1188 /* Start out optimistic */
1190 /* Update conference state in a stateless fashion */
1192 /* Any three-way calling makes slave native mode *definitely* out
1194 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1197 /* If we don't have any 3-way calls, check to see if we have
1198 precisely one slave */
1199 if (useslavenative) {
1200 for (x=0;x<MAX_SLAVES;x++) {
1203 /* Whoops already have a slave! No
1204 slave native and stop right away */
1209 /* We have one slave so far */
1210 slave = p->slaves[x];
1215 /* If no slave, slave native definitely out */
1218 else if (slave->law != p->law) {
1224 return useslavenative;
1227 static int reset_conf(struct zt_pvt *p)
1230 memset(&zi, 0, sizeof(zi));
1232 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1233 if (p->subs[SUB_REAL].zfd > -1) {
1234 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1235 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1240 static int update_conf(struct zt_pvt *p)
1245 struct zt_pvt *slave = NULL;
1247 useslavenative = isslavenative(p, &slave);
1248 /* Start with the obvious, general stuff */
1250 /* Look for three way calls */
1251 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1252 conf_add(p, &p->subs[x], x, 0);
1255 conf_del(p, &p->subs[x], x);
1258 /* If we have a slave, add him to our conference now. or DAX
1259 if this is slave native */
1260 for (x=0;x<MAX_SLAVES;x++) {
1263 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1265 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1270 /* If we're supposed to be in there, do so now */
1271 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1273 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1275 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1279 /* If we have a master, add ourselves to his conference */
1281 if (isslavenative(p->master, NULL)) {
1282 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1284 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1288 /* Nobody is left (or should be left) in our conference.
1292 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1296 static void zt_enable_ec(struct zt_pvt *p)
1303 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1307 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1310 if (p->echocancel) {
1311 if (p->sig == SIG_PRI) {
1313 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1315 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1318 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1320 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1323 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1326 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1329 static void zt_train_ec(struct zt_pvt *p)
1333 if (p && p->echocancel && p->echotraining) {
1334 x = p->echotraining;
1335 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1337 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1339 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1342 ast_log(LOG_DEBUG, "No echo training requested\n");
1345 static void zt_disable_ec(struct zt_pvt *p)
1349 if (p->echocancel) {
1351 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1353 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1355 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1360 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1367 if ((rxgain != 0.0) || (txgain != 0.0)) {
1368 /* caluculate linear value of tx gain */
1369 ltxgain = pow(10.0,txgain / 20.0);
1370 /* caluculate linear value of rx gain */
1371 lrxgain = pow(10.0,rxgain / 20.0);
1372 if (law == ZT_LAW_ALAW) {
1373 for (j=0;j<256;j++) {
1374 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1375 if (k > 32767) k = 32767;
1376 if (k < -32767) k = -32767;
1377 g.rxgain[j] = AST_LIN2A(k);
1378 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1379 if (k > 32767) k = 32767;
1380 if (k < -32767) k = -32767;
1381 g.txgain[j] = AST_LIN2A(k);
1384 for (j=0;j<256;j++) {
1385 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1386 if (k > 32767) k = 32767;
1387 if (k < -32767) k = -32767;
1388 g.rxgain[j] = AST_LIN2MU(k);
1389 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1390 if (k > 32767) k = 32767;
1391 if (k < -32767) k = -32767;
1392 g.txgain[j] = AST_LIN2MU(k);
1396 for (j=0;j<256;j++) {
1403 return(ioctl(fd,ZT_SETGAINS,&g));
1406 static inline int zt_set_hook(int fd, int hs)
1410 res = ioctl(fd, ZT_HOOK, &x);
1413 if (errno == EINPROGRESS) return 0;
1414 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1419 static inline int zt_confmute(struct zt_pvt *p, int muted)
1423 if (p->sig == SIG_PRI) {
1425 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1427 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1429 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1431 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1435 static int save_conference(struct zt_pvt *p)
1437 struct zt_confinfo c;
1439 if (p->saveconf.confmode) {
1440 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1443 p->saveconf.chan = 0;
1444 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1446 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1447 p->saveconf.confmode = 0;
1452 c.confmode = ZT_CONF_NORMAL;
1453 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1455 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1459 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1463 static int restore_conference(struct zt_pvt *p)
1466 if (p->saveconf.confmode) {
1467 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1468 p->saveconf.confmode = 0;
1470 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1475 ast_log(LOG_DEBUG, "Restored conferencing\n");
1479 static int send_callerid(struct zt_pvt *p);
1481 int send_cwcidspill(struct zt_pvt *p)
1485 p->cidspill = malloc(MAX_CALLERID_SIZE);
1487 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1488 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1489 /* Make sure we account for the end */
1490 p->cidlen += READ_SIZE * 4;
1493 if (option_verbose > 2)
1494 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1499 static int has_voicemail(struct zt_pvt *p)
1502 return ast_app_has_voicemail(p->mailbox, NULL);
1505 static int send_callerid(struct zt_pvt *p)
1507 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1509 /* Take out of linear mode if necessary */
1510 if (p->subs[SUB_REAL].linear) {
1511 p->subs[SUB_REAL].linear = 0;
1512 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1514 while(p->cidpos < p->cidlen) {
1515 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1517 if (errno == EAGAIN)
1520 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1530 if (p->callwaitcas) {
1531 /* Wait for CID/CW to expire */
1532 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1534 restore_conference(p);
1538 static int zt_callwait(struct ast_channel *ast)
1540 struct zt_pvt *p = ast->tech_pvt;
1541 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1543 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1546 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1550 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1551 if (!p->callwaitrings && p->callwaitingcallerid) {
1552 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1554 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1556 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1558 p->cidlen = 2400 + READ_SIZE * 4;
1563 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1569 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1571 struct zt_pvt *p = ast->tech_pvt;
1577 char dest[256]; /* must be same length as p->dialdest */
1578 ast_mutex_lock(&p->lock);
1579 strncpy(dest, rdest, sizeof(dest) - 1);
1580 strncpy(p->dialdest, rdest, sizeof(dest) - 1);
1581 if ((ast->_state == AST_STATE_BUSY)) {
1582 p->subs[SUB_REAL].needbusy = 1;
1583 ast_mutex_unlock(&p->lock);
1586 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1587 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1588 ast_mutex_unlock(&p->lock);
1592 if (p->radio) /* if a radio channel, up immediately */
1594 /* Special pseudo -- automatically up */
1595 ast_setstate(ast, AST_STATE_UP);
1596 ast_mutex_unlock(&p->lock);
1599 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1600 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1602 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1605 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1611 if (p->owner == ast) {
1612 /* Normal ring, on hook */
1614 /* Don't send audio while on hook, until the call is answered */
1616 if (p->use_callerid) {
1617 /* Generate the Caller-ID spill if desired */
1619 ast_log(LOG_WARNING, "cidspill already exists??\n");
1622 p->cidspill = malloc(MAX_CALLERID_SIZE);
1625 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1629 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1631 /* Choose proper cadence */
1632 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1633 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1634 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1635 p->cidrings = cidrings[p->distinctivering - 1];
1637 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1638 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1639 p->cidrings = p->sendcalleridafter;
1643 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1644 c = strchr(dest, '/');
1647 if (c && (strlen(c) < p->stripmsd)) {
1648 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1652 p->dop.op = ZT_DIAL_OP_REPLACE;
1653 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1654 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1656 p->dop.dialstr[0] = '\0';
1659 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1660 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1661 ast_mutex_unlock(&p->lock);
1666 /* Call waiting call */
1667 p->callwaitrings = 0;
1668 if (ast->cid.cid_num)
1669 strncpy(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num)-1);
1671 p->callwait_num[0] = '\0';
1672 if (ast->cid.cid_name)
1673 strncpy(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name)-1);
1675 p->callwait_name[0] = '\0';
1676 /* Call waiting tone instead */
1677 if (zt_callwait(ast)) {
1678 ast_mutex_unlock(&p->lock);
1681 /* Make ring-back */
1682 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1683 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1686 n = ast->cid.cid_name;
1687 l = ast->cid.cid_num;
1689 strncpy(p->lastcid_num, l, sizeof(p->lastcid_num) - 1);
1691 p->lastcid_num[0] = '\0';
1693 strncpy(p->lastcid_name, n, sizeof(p->lastcid_name) - 1);
1695 p->lastcid_name[0] = '\0';
1696 ast_setstate(ast, AST_STATE_RINGING);
1697 index = zt_get_index(ast, p, 0);
1699 p->subs[index].needringing = 1;
1715 case SIG_SF_FEATDMF:
1717 c = strchr(dest, '/');
1722 if (strlen(c) < p->stripmsd) {
1723 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1724 ast_mutex_unlock(&p->lock);
1728 /* Start the trunk, if not GR-303 */
1732 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1734 if (errno != EINPROGRESS) {
1735 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1736 ast_mutex_unlock(&p->lock);
1743 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1744 p->dop.op = ZT_DIAL_OP_REPLACE;
1745 if (p->sig == SIG_FEATD) {
1746 l = ast->cid.cid_num;
1748 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1750 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1752 if (p->sig == SIG_FEATDMF) {
1753 l = ast->cid.cid_num;
1755 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1757 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1759 if (p->sig == SIG_E911) {
1760 strncpy(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr) - 1);
1762 if (p->sig == SIG_FEATB) {
1763 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1766 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c + p->stripmsd);
1768 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1769 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1770 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1771 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1772 p->echorest[sizeof(p->echorest) - 1] = '\0';
1774 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1778 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1780 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1781 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1782 ast_mutex_unlock(&p->lock);
1786 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1788 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1789 ast_setstate(ast, AST_STATE_DIALING);
1792 /* Special pseudo -- automatically up*/
1793 ast_setstate(ast, AST_STATE_UP);
1796 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1797 p->dialdest[0] = '\0';
1800 ast_log(LOG_DEBUG, "not yet implemented\n");
1801 ast_mutex_unlock(&p->lock);
1809 int prilocaldialplan;
1811 c = strchr(dest, '/');
1816 if (!p->hidecallerid) {
1817 l = ast->cid.cid_num;
1818 n = ast->cid.cid_name;
1823 if (strlen(c) < p->stripmsd) {
1824 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1825 ast_mutex_unlock(&p->lock);
1828 if (p->sig != SIG_FXSKS) {
1829 p->dop.op = ZT_DIAL_OP_REPLACE;
1830 s = strchr(c + p->stripmsd, 'w');
1833 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1835 p->dop.dialstr[0] = '\0';
1838 p->dop.dialstr[0] = '\0';
1841 if (pri_grab(p, p->pri)) {
1842 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1843 ast_mutex_unlock(&p->lock);
1846 if (!(p->call = pri_new_call(p->pri->pri))) {
1847 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1849 ast_mutex_unlock(&p->lock);
1852 if (!(sr = pri_sr_new())) {
1853 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1855 ast_mutex_unlock(&p->lock);
1857 if (p->bearer || (p->sig == SIG_FXSKS)) {
1859 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);
1860 p->bearer->call = p->call;
1862 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
1863 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1865 p->digital = ast_test_flag(ast,AST_FLAG_DIGITAL);
1866 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1867 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1868 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : PRI_TRANS_CAP_SPEECH,
1870 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1872 pridialplan = p->pri->dialplan - 1;
1873 if (pridialplan == -2) { /* compute dynamically */
1874 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
1875 dp_strip = strlen(p->pri->internationalprefix);
1876 pridialplan = PRI_INTERNATIONAL_ISDN;
1877 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
1878 dp_strip = strlen(p->pri->nationalprefix);
1879 pridialplan = PRI_NATIONAL_ISDN;
1881 pridialplan = PRI_LOCAL_ISDN;
1884 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
1887 prilocaldialplan = p->pri->localdialplan - 1;
1888 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
1889 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
1890 ldp_strip = strlen(p->pri->internationalprefix);
1891 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
1892 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
1893 ldp_strip = strlen(p->pri->nationalprefix);
1894 prilocaldialplan = PRI_NATIONAL_ISDN;
1896 prilocaldialplan = PRI_LOCAL_ISDN;
1899 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
1900 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
1901 PRES_NUMBER_NOT_AVAILABLE);
1902 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
1903 if (pri_setup(p->pri->pri, p->call, sr)) {
1904 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
1905 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
1907 ast_mutex_unlock(&p->lock);
1912 ast_setstate(ast, AST_STATE_DIALING);
1916 ast_mutex_unlock(&p->lock);
1920 static void destroy_zt_pvt(struct zt_pvt **pvt)
1922 struct zt_pvt *p = *pvt;
1923 /* Remove channel from the list */
1925 p->prev->next = p->next;
1927 p->next->prev = p->prev;
1928 ast_mutex_destroy(&p->lock);
1933 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
1943 for (i = 0; i < 3; i++) {
1944 if (cur->subs[i].owner) {
1950 prev->next = cur->next;
1952 prev->next->prev = prev;
1958 iflist->prev = NULL;
1962 if (cur->subs[SUB_REAL].zfd > -1) {
1963 zt_close(cur->subs[SUB_REAL].zfd);
1965 destroy_zt_pvt(&cur);
1969 prev->next = cur->next;
1971 prev->next->prev = prev;
1977 iflist->prev = NULL;
1981 if (cur->subs[SUB_REAL].zfd > -1) {
1982 zt_close(cur->subs[SUB_REAL].zfd);
1984 destroy_zt_pvt(&cur);
1990 int pri_is_up(struct zt_pri *pri)
1993 for (x=0;x<NUM_DCHANS;x++) {
1994 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2000 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2002 bearer->owner = &inuse;
2003 bearer->realcall = crv;
2004 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2005 if (crv->subs[SUB_REAL].owner)
2006 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2007 crv->bearer = bearer;
2008 crv->call = bearer->call;
2013 static char *pri_order(int level)
2023 return "Quaternary";
2029 int pri_find_dchan(struct zt_pri *pri)
2036 for(x=0;x<NUM_DCHANS;x++) {
2037 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2039 if (pri->dchans[x] == old) {
2045 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
2046 pri->dchannels[newslot]);
2048 if (old && (oldslot != newslot))
2049 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2050 pri->dchannels[oldslot], pri->dchannels[newslot]);
2051 pri->pri = pri->dchans[newslot];
2056 static int zt_hangup(struct ast_channel *ast)
2060 /*static int restore_gains(struct zt_pvt *p);*/
2061 struct zt_pvt *p = ast->tech_pvt;
2062 struct zt_pvt *tmp = NULL;
2063 struct zt_pvt *prev = NULL;
2067 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2068 if (!ast->tech_pvt) {
2069 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2073 ast_mutex_lock(&p->lock);
2075 index = zt_get_index(ast, p, 1);
2077 if (p->sig == SIG_PRI) {
2079 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2085 if (p->origcid_num) {
2086 strncpy(p->cid_num, p->origcid_num, sizeof(p->cid_num) - 1);
2087 free(p->origcid_num);
2088 p->origcid_num = NULL;
2090 if (p->origcid_name) {
2091 strncpy(p->cid_name, p->origcid_name, sizeof(p->cid_name) - 1);
2092 free(p->origcid_name);
2093 p->origcid_name = NULL;
2096 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2100 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2101 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2105 /* Real channel, do some fixup */
2106 p->subs[index].owner = NULL;
2107 p->subs[index].needanswer = 0;
2108 p->subs[index].needflash = 0;
2109 p->subs[index].needringing = 0;
2110 p->subs[index].needbusy = 0;
2111 p->subs[index].needcongestion = 0;
2112 p->subs[index].linear = 0;
2113 p->subs[index].needcallerid = 0;
2114 p->polarity = POLARITY_IDLE;
2115 zt_setlinear(p->subs[index].zfd, 0);
2116 if (index == SUB_REAL) {
2117 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2118 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2119 if (p->subs[SUB_CALLWAIT].inthreeway) {
2120 /* We had flipped over to answer a callwait and now it's gone */
2121 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2122 /* Move to the call-wait, but un-own us until they flip back. */
2123 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2124 unalloc_sub(p, SUB_CALLWAIT);
2127 /* The three way hung up, but we still have a call wait */
2128 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2129 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2130 unalloc_sub(p, SUB_THREEWAY);
2131 if (p->subs[SUB_REAL].inthreeway) {
2132 /* This was part of a three way call. Immediately make way for
2134 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2135 p->owner = p->subs[SUB_REAL].owner;
2137 /* This call hasn't been completed yet... Set owner to NULL */
2138 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2141 p->subs[SUB_REAL].inthreeway = 0;
2143 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2144 /* Move to the call-wait and switch back to them. */
2145 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2146 unalloc_sub(p, SUB_CALLWAIT);
2147 p->owner = p->subs[SUB_REAL].owner;
2148 if (p->owner->_state != AST_STATE_UP)
2149 p->subs[SUB_REAL].needanswer = 1;
2150 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2151 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2152 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2153 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2154 unalloc_sub(p, SUB_THREEWAY);
2155 if (p->subs[SUB_REAL].inthreeway) {
2156 /* This was part of a three way call. Immediately make way for
2158 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2159 p->owner = p->subs[SUB_REAL].owner;
2161 /* This call hasn't been completed yet... Set owner to NULL */
2162 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2165 p->subs[SUB_REAL].inthreeway = 0;
2167 } else if (index == SUB_CALLWAIT) {
2168 /* Ditch the holding callwait call, and immediately make it availabe */
2169 if (p->subs[SUB_CALLWAIT].inthreeway) {
2170 /* This is actually part of a three way, placed on hold. Place the third part
2171 on music on hold now */
2172 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2173 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2174 p->subs[SUB_THREEWAY].inthreeway = 0;
2175 /* Make it the call wait now */
2176 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2177 unalloc_sub(p, SUB_THREEWAY);
2179 unalloc_sub(p, SUB_CALLWAIT);
2180 } else if (index == SUB_THREEWAY) {
2181 if (p->subs[SUB_CALLWAIT].inthreeway) {
2182 /* The other party of the three way call is currently in a call-wait state.
2183 Start music on hold for them, and take the main guy out of the third call */
2184 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2185 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2186 p->subs[SUB_CALLWAIT].inthreeway = 0;
2188 p->subs[SUB_REAL].inthreeway = 0;
2189 /* If this was part of a three way call index, let us make
2190 another three way call */
2191 unalloc_sub(p, SUB_THREEWAY);
2193 /* This wasn't any sort of call, but how are we an index? */
2194 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2199 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2202 p->distinctivering = 0;
2203 p->confirmanswer = 0;
2209 p->onhooktime = time(NULL);
2215 ast_dsp_free(p->dsp);
2219 law = ZT_LAW_DEFAULT;
2220 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2222 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2223 /* Perform low level hangup if no owner left */
2226 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2227 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2228 if (!pri_grab(p, p->pri)) {
2229 if (p->alreadyhungup) {
2230 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2231 pri_hangup(p->pri->pri, p->call, -1);
2234 p->bearer->call = NULL;
2236 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2237 int icause = ast->hangupcause ? ast->hangupcause : -1;
2238 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2239 p->alreadyhungup = 1;
2241 p->bearer->alreadyhungup = 1;
2244 icause = atoi(cause);
2246 pri_hangup(p->pri->pri, p->call, icause);
2249 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2252 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2257 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2264 if (p->sig == SIG_R2) {
2266 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2274 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2275 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2277 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2283 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2286 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2288 /* If they're off hook, try playing congestion */
2289 if ((par.rxisoffhook) && (!p->radio))
2290 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2292 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2298 /* Make sure we're not made available for at least two seconds assuming
2299 we were actually used for an inbound or outbound call. */
2300 if (ast->_state != AST_STATE_RESERVED) {
2301 time(&p->guardtime);
2306 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2313 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2314 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2318 p->callwaiting = p->permcallwaiting;
2319 p->hidecallerid = p->permhidecallerid;
2324 /* Restore data mode */
2325 if (p->sig == SIG_PRI) {
2327 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2331 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2332 /* Free up the bearer channel as well, and
2333 don't use its file descriptor anymore */
2334 update_conf(p->bearer);
2335 reset_conf(p->bearer);
2336 p->bearer->owner = NULL;
2337 p->bearer->realcall = NULL;
2339 p->subs[SUB_REAL].zfd = -1;
2347 p->callwaitingrepeat = 0;
2349 ast->tech_pvt = NULL;
2350 ast_mutex_unlock(&p->lock);
2351 ast_mutex_lock(&usecnt_lock);
2354 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2355 ast_mutex_unlock(&usecnt_lock);
2356 ast_update_use_count();
2357 if (option_verbose > 2)
2358 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2360 ast_mutex_lock(&iflock);
2366 destroy_channel(prev, tmp, 0);
2374 ast_mutex_unlock(&iflock);
2378 static int zt_answer(struct ast_channel *ast)
2380 struct zt_pvt *p = ast->tech_pvt;
2383 int oldstate = ast->_state;
2384 ast_setstate(ast, AST_STATE_UP);
2385 ast_mutex_lock(&p->lock);
2386 index = zt_get_index(ast, p, 0);
2389 /* nothing to do if a radio channel */
2391 ast_mutex_unlock(&p->lock);
2410 case SIG_SF_FEATDMF:
2415 /* Pick up the line */
2416 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2417 if(p->hanguponpolarityswitch) {
2418 gettimeofday(&p->polaritydelaytv, NULL);
2420 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2421 tone_zone_play_tone(p->subs[index].zfd, -1);
2423 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2424 if (oldstate == AST_STATE_RINGING) {
2425 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2426 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2427 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2428 p->owner = p->subs[SUB_REAL].owner;
2431 if (p->sig & __ZT_SIG_FXS) {
2438 /* Send a pri acknowledge */
2439 if (!pri_grab(p, p->pri)) {
2441 res = pri_answer(p->pri->pri, p->call, 0, 1);
2444 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2451 res = mfcr2_AnswerCall(p->r2, NULL);
2453 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2457 ast_mutex_unlock(&p->lock);
2460 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2463 ast_mutex_unlock(&p->lock);
2467 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2472 struct zt_pvt *p = chan->tech_pvt;
2475 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2476 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2482 if ((!cp) || (datalen < 1))
2488 case AST_OPTION_TONE_VERIFY:
2493 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2494 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2497 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2498 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2501 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2502 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2506 case AST_OPTION_TDD: /* turn on or off TDD */
2507 if (!*cp) { /* turn it off */
2508 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2509 if (p->tdd) tdd_free(p->tdd);
2515 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2516 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2519 /* otherwise, turn it on */
2520 if (!p->didtdd) { /* if havent done it yet */
2521 unsigned char mybuf[41000],*buf;
2522 int size,res,fd,len;
2524 struct pollfd fds[1];
2526 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2527 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2529 index = zt_get_index(chan, p, 0);
2531 ast_log(LOG_WARNING, "No index in TDD?\n");
2534 fd = p->subs[index].zfd;
2536 if (ast_check_hangup(chan)) return -1;
2538 if (size > READ_SIZE)
2541 fds[0].events = POLLPRI | POLLOUT;
2542 res = poll(fds, 1, -1);
2544 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2547 /* if got exception */
2548 if (fds[0].revents & POLLPRI) return -1;
2549 if (!(fds[0].revents & POLLOUT)) {
2550 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2553 res = write(fd, buf, size);
2555 if (res == -1) return -1;
2556 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2562 p->didtdd = 1; /* set to have done it now */
2564 if (*cp == 2) { /* Mate mode */
2565 if (p->tdd) tdd_free(p->tdd);
2570 if (!p->tdd) { /* if we dont have one yet */
2571 p->tdd = tdd_new(); /* allocate one */
2574 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2577 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2582 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2585 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2587 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2590 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2596 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2599 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2600 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2607 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2609 /* Unlink a specific slave or all slaves/masters from a given master */
2615 ast_mutex_lock(&master->lock);
2617 while(ast_mutex_trylock(&slave->lock)) {
2618 ast_mutex_unlock(&master->lock);
2620 ast_mutex_lock(&master->lock);
2625 for (x=0;x<MAX_SLAVES;x++) {
2626 if (master->slaves[x]) {
2627 if (!slave || (master->slaves[x] == slave)) {
2628 /* Take slave out of the conference */
2629 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2630 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2631 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2632 master->slaves[x]->master = NULL;
2633 master->slaves[x] = NULL;
2638 master->inconference = 0;
2641 if (master->master) {
2642 /* Take master out of the conference */
2643 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2644 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2646 for (x=0;x<MAX_SLAVES;x++) {
2647 if (master->master->slaves[x] == master)
2648 master->master->slaves[x] = NULL;
2649 else if (master->master->slaves[x])
2653 master->master->inconference = 0;
2655 master->master = NULL;
2657 update_conf(master);
2660 ast_mutex_unlock(&slave->lock);
2661 ast_mutex_unlock(&master->lock);
2665 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2667 if (!slave || !master) {
2668 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2671 for (x=0;x<MAX_SLAVES;x++) {
2672 if (!master->slaves[x]) {
2673 master->slaves[x] = slave;
2677 if (x >= MAX_SLAVES) {
2678 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2679 master->slaves[MAX_SLAVES - 1] = slave;
2682 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2683 slave->master = master;
2685 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2688 static int zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2690 struct ast_channel *who = NULL, *cs[3];
2691 struct zt_pvt *p0, *p1, *op0, *op1;
2692 struct zt_pvt *master=NULL, *slave=NULL;
2693 struct ast_frame *f;
2698 int oi1, oi2, i1 = -1, i2 = -1, t1, t2;
2699 int os1 = -1, os2 = -1;
2700 struct ast_channel *oc1, *oc2;
2702 int triedtopribridge = 0;
2703 q931_call *q931c0 = NULL, *q931c1 = NULL;
2707 /* if need DTMF, cant native bridge */
2708 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2712 ast_mutex_lock(&c0->lock);
2713 ast_mutex_lock(&c1->lock);
2717 /* cant do pseudo-channels here */
2718 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2719 ast_mutex_unlock(&c0->lock);
2720 ast_mutex_unlock(&c1->lock);
2724 op0 = p0 = c0->tech_pvt;
2725 op1 = p1 = c1->tech_pvt;
2728 oi1 = zt_get_index(c0, p0, 0);
2729 oi2 = zt_get_index(c1, p1, 0);
2732 if ((oi1 < 0) || (oi2 < 0)) {
2733 ast_mutex_unlock(&c0->lock);
2734 ast_mutex_unlock(&c1->lock);
2740 ast_mutex_lock(&p0->lock);
2741 if (ast_mutex_trylock(&p1->lock)) {
2742 /* Don't block, due to potential for deadlock */
2743 ast_mutex_unlock(&p0->lock);
2744 ast_mutex_unlock(&c0->lock);
2745 ast_mutex_unlock(&c1->lock);
2746 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2749 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2750 if (!p0->owner || !p1->owner) {
2751 /* Currently unowned -- Do nothing. */
2754 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2755 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2759 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2764 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2765 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,
2766 p0->channel, oi1, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway);
2769 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_THREEWAY)) {
2770 if (p1->subs[SUB_THREEWAY].inthreeway) {
2776 } else if ((oi1 == SUB_THREEWAY) && (oi2 == SUB_REAL)) {
2777 if (p0->subs[SUB_THREEWAY].inthreeway) {
2783 } else if ((oi1 == SUB_REAL) && (oi2 == SUB_CALLWAIT)) {
2784 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2785 don't put us in anything */
2786 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2792 } else if ((oi1 == SUB_CALLWAIT) && (oi2 == SUB_REAL)) {
2793 /* Same as previous */
2794 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2801 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2802 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2803 if (master && slave) {
2804 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2805 in an active threeway call with a channel that is ringing, we should
2806 indicate ringing. */
2807 if ((oi2 == SUB_THREEWAY) &&
2808 p1->subs[SUB_THREEWAY].inthreeway &&
2809 p1->subs[SUB_REAL].owner &&
2810 p1->subs[SUB_REAL].inthreeway &&
2811 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2812 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2813 tone_zone_play_tone(p0->subs[oi1].zfd, ZT_TONE_RINGTONE);
2814 os2 = p1->subs[SUB_REAL].owner->_state;
2816 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p0->channel, oi1, p1->channel, oi2);
2817 tone_zone_play_tone(p0->subs[oi1].zfd, -1);
2819 if ((oi1 == SUB_THREEWAY) &&
2820 p0->subs[SUB_THREEWAY].inthreeway &&
2821 p0->subs[SUB_REAL].owner &&
2822 p0->subs[SUB_REAL].inthreeway &&
2823 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2824 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2825 tone_zone_play_tone(p1->subs[oi2].zfd, ZT_TONE_RINGTONE);
2826 os1 = p0->subs[SUB_REAL].owner->_state;
2828 ast_log(LOG_DEBUG, "Stoping tones on %d/%d talking to %d/%d\n", p1->channel, oi2, p0->channel, oi1);
2829 tone_zone_play_tone(p1->subs[oi1].zfd, -1);
2831 if ((oi1 == SUB_REAL) && (oi2 == SUB_REAL)) {
2832 if (!p0->echocanbridged || !p1->echocanbridged) {
2833 /* Disable echo cancellation if appropriate */
2838 zt_link(slave, master);
2839 master->inconference = inconf;
2840 } else if (!nothingok)
2841 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi1], p1->channel, subnames[oi2]);
2845 t1 = p0->subs[SUB_REAL].inthreeway;
2846 t2 = p1->subs[SUB_REAL].inthreeway;
2848 ast_mutex_unlock(&p0->lock);
2849 ast_mutex_unlock(&p1->lock);
2851 ast_mutex_unlock(&c0->lock);
2852 ast_mutex_unlock(&c1->lock);
2854 /* Native bridge failed */
2855 if ((!master || !slave) && !nothingok) {
2867 /* Here's our main loop... Start by locking things, looking for private parts,
2868 and then balking if anything is wrong */
2869 ast_mutex_lock(&c0->lock);
2870 ast_mutex_lock(&c1->lock);
2877 if (q931c0 && q931c1 && !triedtopribridge) {
2878 pri_channel_bridge(q931c0, q931c1);
2879 triedtopribridge = 1;
2883 i1 = zt_get_index(c0, p0, 1);
2885 i2 = zt_get_index(c1, p1, 1);
2886 ast_mutex_unlock(&c0->lock);
2887 ast_mutex_unlock(&c1->lock);
2888 if ((op0 != p0) || (op1 != p1) ||
2889 (ofd1 != c0->fds[0]) ||
2890 (ofd2 != c1->fds[0]) ||
2891 (p0->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p0->subs[SUB_REAL].owner->_state)) ||
2892 (p1->subs[SUB_REAL].owner && (os2 > -1) && (os2 != p1->subs[SUB_REAL].owner->_state)) ||
2893 (oc1 != p0->owner) ||
2894 (oc2 != p1->owner) ||
2895 (t1 != p0->subs[SUB_REAL].inthreeway) ||
2896 (t2 != p1->subs[SUB_REAL].inthreeway) ||
2899 if (slave && master)
2900 zt_unlink(slave, master, 1);
2901 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
2902 op0->channel, oi1, op1->channel, oi2);
2910 who = ast_waitfor_n(cs, 2, &to);
2912 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2915 if (who->tech_pvt == op0)
2916 op0->ignoredtmf = 1;
2917 else if (who->tech_pvt == op1)
2918 op1->ignoredtmf = 1;
2920 if (who->tech_pvt == op0)
2921 op0->ignoredtmf = 0;
2922 else if (who->tech_pvt == op1)
2923 op1->ignoredtmf = 0;
2927 if (slave && master)
2928 zt_unlink(slave, master, 1);
2935 if (f->frametype == AST_FRAME_CONTROL) {
2938 if (slave && master)
2939 zt_unlink(slave, master, 1);
2942 if (f->frametype == AST_FRAME_DTMF) {
2943 if (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2944 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))) {
2947 if (slave && master)
2948 zt_unlink(slave, master, 1);
2950 } else if ((who == c0) && p0->pulsedial) {
2952 } else if ((who == c1) && p1->pulsedial) {
2958 /* Swap who gets priority */
2965 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
2967 struct zt_pvt *p = newchan->tech_pvt;
2969 ast_mutex_lock(&p->lock);
2970 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
2971 if (p->owner == oldchan) {
2975 if (p->subs[x].owner == oldchan) {
2977 zt_unlink(NULL, p, 0);
2978 p->subs[x].owner = newchan;
2980 if (newchan->_state == AST_STATE_RINGING)
2981 zt_indicate(newchan, AST_CONTROL_RINGING);
2983 ast_mutex_unlock(&p->lock);
2987 static int zt_ring_phone(struct zt_pvt *p)
2991 /* Make sure our transmit state is on hook */
2994 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2997 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2999 printf("Res: %d, error: %s\n", res, strerror(errno));
3005 /* Wait just in case */
3012 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
3020 static void *ss_thread(void *data);
3022 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
3024 static int attempt_transfer(struct zt_pvt *p)
3026 /* In order to transfer, we need at least one of the channels to
3027 actually be in a call bridge. We can't conference two applications
3028 together (but then, why would we want to?) */
3029 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
3030 /* The three-way person we're about to transfer to could still be in MOH, so
3031 stop if now if appropriate */
3032 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
3033 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
3034 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
3035 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
3037 if (p->subs[SUB_REAL].owner->cdr) {
3038 /* Move CDR from second channel to current one */
3039 p->subs[SUB_THREEWAY].owner->cdr =
3040 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
3041 p->subs[SUB_REAL].owner->cdr = NULL;
3043 if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
3044 /* Move CDR from second channel's bridge to current one */
3045 p->subs[SUB_THREEWAY].owner->cdr =
3046 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
3047 ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
3049 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
3050 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
3051 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
3054 /* Orphan the channel after releasing the lock */
3055 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
3056 unalloc_sub(p, SUB_THREEWAY);
3057 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
3058 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
3059 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
3061 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
3062 if (p->subs[SUB_THREEWAY].owner->cdr) {
3063 /* Move CDR from second channel to current one */
3064 p->subs[SUB_REAL].owner->cdr =
3065 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
3066 p->subs[SUB_THREEWAY].owner->cdr = NULL;
3068 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr) {
3069 /* Move CDR from second channel's bridge to current one */
3070 p->subs[SUB_REAL].owner->cdr =
3071 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr);
3072 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr = NULL;
3074 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
3075 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
3076 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
3079 /* Three-way is now the REAL */
3080 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3081 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
3082 unalloc_sub(p, SUB_THREEWAY);
3083 /* Tell the caller not to hangup */
3086 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
3087 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
3088 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
3094 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
3096 struct ast_frame *f;
3097 f = &p->subs[index].f;
3099 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
3103 case MFCR2_EVENT_BLOCKED:
3104 if (option_verbose > 2)
3105 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
3107 case MFCR2_EVENT_UNBLOCKED:
3108 if (option_verbose > 2)
3109 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
3111 case MFCR2_EVENT_CONFIG_ERR:
3112 if (option_verbose > 2)
3113 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
3115 case MFCR2_EVENT_RING:
3116 if (option_verbose > 2)
3117 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
3119 case MFCR2_EVENT_HANGUP:
3120 if (option_verbose > 2)
3121 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
3123 case MFCR2_EVENT_RINGING:
3124 if (option_verbose > 2)
3125 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
3127 case MFCR2_EVENT_ANSWER:
3128 if (option_verbose > 2)
3129 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
3131 case MFCR2_EVENT_HANGUP_ACK:
3132 if (option_verbose > 2)
3133 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
3135 case MFCR2_EVENT_IDLE:
3136 if (option_verbose > 2)
3137 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
3140 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
3146 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
3151 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
3153 ast_log(LOG_WARNING, "Unable to check received bits\n");
3157 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
3160 e = mfcr2_cas_signaling_event(p->r2, x);
3165 static int check_for_conference(struct zt_pvt *p)
3168 /* Fine if we already have a master, etc */
3169 if (p->master || (p->confno > -1))
3171 memset(&ci, 0, sizeof(ci));
3172 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
3173 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
3176 /* If we have no master and don't have a confno, then
3177 if we're in a conference, it's probably a MeetMe room or
3178 some such, so don't let us 3-way out! */
3179 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
3180 if (option_verbose > 2)
3181 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
3187 static int get_alarms(struct zt_pvt *p)
3191 memset(&zi, 0, sizeof(zi));
3192 zi.spanno = p->span;
3193 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
3195 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
3201 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
3206 struct zt_pvt *p = ast->tech_pvt;
3208 pthread_attr_t attr;
3209 struct ast_channel *chan;
3210 pthread_attr_init(&attr);
3211 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3212 index = zt_get_index(ast, p, 0);
3213 p->subs[index].f.frametype = AST_FRAME_NULL;
3214 p->subs[index].f.datalen = 0;
3215 p->subs[index].f.samples = 0;
3216 p->subs[index].f.mallocd = 0;
3217 p->subs[index].f.offset = 0;
3218 p->subs[index].f.src = "zt_handle_event";
3219 p->subs[index].f.data = NULL;
3221 return &p->subs[index].f;
3222 if (p->fake_event) {
3223 res = p->fake_event;
3226 res = zt_get_event(p->subs[index].zfd);
3227 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
3228 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFDIGIT)) {
3229 if (res & ZT_EVENT_PULSEDIGIT)
3233 ast_log(LOG_DEBUG, "Pulse dial '%c'\n", res & 0xff);
3234 p->subs[index].f.frametype = AST_FRAME_DTMF;
3235 p->subs[index].f.subclass = res & 0xff;
3236 /* Return the captured digit */
3237 return &p->subs[index].f;
3240 case ZT_EVENT_BITSCHANGED:
3241 if (p->sig == SIG_R2) {
3243 struct ast_frame *f = &p->subs[index].f;
3245 e = r2_get_event_bits(p);
3247 f = handle_r2_event(p, e, index);
3253 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
3254 case ZT_EVENT_PULSE_START:
3255 /* Stop tone if there's a pulse start and the PBX isn't started */
3257 tone_zone_play_tone(p->subs[index].zfd, -1);
3259 case ZT_EVENT_DIALCOMPLETE:
3260 if (p->inalarm) break;
3261 if (p->radio) break;
3262 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
3263 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
3266 if (!x) { /* if not still dialing in driver */
3270 strncpy(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr) - 1);
3271 p->dop.op = ZT_DIAL_OP_REPLACE;
3272 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
3276 if (p->sig == SIG_E911) {
3277 /* if thru with dialing after offhook */
3278 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
3279 ast_setstate(ast, AST_STATE_UP);
3280 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3281 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3283 } else { /* if to state wait for offhook to dial rest */
3284 /* we now wait for off hook */
3285 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
3288 if (ast->_state == AST_STATE_DIALING) {
3289 if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
3290 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
3291 } 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)))) {
3292 ast_setstate(ast, AST_STATE_RINGING);
3293 } else if (!p->answeronpolarityswitch) {
3294 ast_setstate(ast, AST_STATE_UP);
3295 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3296 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
3302 case ZT_EVENT_ALARM:
3305 if (p->pri && p->pri->pri) {
3306 if (!pri_grab(p, p->pri)) {
3307 pri_hangup(p->pri->pri, p->call, -1);
3308 pri_destroycall(p->pri->pri, p->call);
3311 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
3313 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
3316 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
3319 p->bearer->inalarm = 1;
3323 res = get_alarms(p);
3324 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
3325 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
3328 alarm2str(res), p->channel);
3329 /* fall through intentionally */
3330 case ZT_EVENT_ONHOOK:
3333 p->subs[index].f.frametype = AST_FRAME_CONTROL;
3334 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
3341 p->onhooktime = time(NULL);
3343 /* Check for some special conditions regarding call waiting */
3344 if (index == SUB_REAL) {
3345 /* The normal line was hung up */
3346 if (p->subs[SUB_CALLWAIT].owner) {
3347 /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
3348 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3349 if (option_verbose > 2)
3350 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
3351 unalloc_sub(p, SUB_CALLWAIT);
3353 p->subs[index].needanswer = 0;
3354 p->subs[index].needringing = 0;
3356 p->callwaitingrepeat = 0;
3359 /* Don't start streaming audio yet if the incoming call isn't up yet */
3360 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
3363 } else if (p->subs[SUB_THREEWAY].owner) {