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 <sys/signal.h>
19 #if !defined(SOLARIS) && !defined(__FreeBSD__)
23 #include <sys/ioctl.h>
25 #include <linux/zaptel.h>
28 #endif /* __linux__ */
34 #ifndef PRI_CALLINGPLANANI
35 #error "You need newer libpri"
44 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
46 #include "asterisk/lock.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/config.h"
49 #include "asterisk/logger.h"
50 #include "asterisk/module.h"
51 #include "asterisk/pbx.h"
52 #include "asterisk/options.h"
53 #include "asterisk/file.h"
54 #include "asterisk/ulaw.h"
55 #include "asterisk/alaw.h"
56 #include "asterisk/callerid.h"
57 #include "asterisk/adsi.h"
58 #include "asterisk/cli.h"
59 #include "asterisk/cdr.h"
60 #include "asterisk/features.h"
61 #include "asterisk/musiconhold.h"
62 #include "asterisk/say.h"
63 #include "asterisk/tdd.h"
64 #include "asterisk/app.h"
65 #include "asterisk/dsp.h"
66 #include "asterisk/astdb.h"
67 #include "asterisk/manager.h"
68 #include "asterisk/causes.h"
69 #include "asterisk/term.h"
70 #include "asterisk/utils.h"
71 #include "asterisk/transcap.h"
74 #error "Your zaptel is too old. please cvs update"
78 /* Work around older code with no tone detect */
79 #define ZT_EVENT_DTMFDOWN 0
80 #define ZT_EVENT_DTMFUP 0
84 * Define ZHONE_HACK to cause us to go off hook and then back on hook when
85 * the user hangs up to reset the state machine so ring works properly.
86 * This is used to be able to support kewlstart by putting the zhone in
87 * groundstart mode since their forward disconnect supervision is entirely
88 * broken even though their documentation says it isn't and their support
89 * is entirely unwilling to provide any assistance with their channel banks
90 * even though their web site says they support their products for life.
93 /* #define ZHONE_HACK */
96 * Define if you want to check the hook state for an FXO (FXS signalled) interface
97 * before dialing on it. Certain FXO interfaces always think they're out of
98 * service with this method however.
100 /* #define ZAP_CHECK_HOOKSTATE */
102 /* Typically, how many rings before we should send Caller*ID */
103 #define DEFAULT_CIDRINGS 1
105 #define CHANNEL_PSEUDO -12
107 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
109 /* Signaling types that need to use MF detection should be placed in this macro */
110 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FEATB))
112 static const char desc[] = "Zapata Telephony"
121 static const char tdesc[] = "Zapata Telephony Driver"
130 static const char type[] = "Zap";
131 static const char config[] = "zapata.conf";
133 #define SIG_EM ZT_SIG_EM
134 #define SIG_EMWINK (0x100000 | ZT_SIG_EM)
135 #define SIG_FEATD (0x200000 | ZT_SIG_EM)
136 #define SIG_FEATDMF (0x400000 | ZT_SIG_EM)
137 #define SIG_FEATB (0x800000 | ZT_SIG_EM)
138 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
139 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
140 #define SIG_FXSLS ZT_SIG_FXSLS
141 #define SIG_FXSGS ZT_SIG_FXSGS
142 #define SIG_FXSKS ZT_SIG_FXSKS
143 #define SIG_FXOLS ZT_SIG_FXOLS
144 #define SIG_FXOGS ZT_SIG_FXOGS
145 #define SIG_FXOKS ZT_SIG_FXOKS
146 #define SIG_PRI ZT_SIG_CLEAR
147 #define SIG_R2 ZT_SIG_CAS
148 #define SIG_SF ZT_SIG_SF
149 #define SIG_SFWINK (0x100000 | ZT_SIG_SF)
150 #define SIG_SF_FEATD (0x200000 | ZT_SIG_SF)
151 #define SIG_SF_FEATDMF (0x400000 | ZT_SIG_SF)
152 #define SIG_SF_FEATB (0x800000 | ZT_SIG_SF)
153 #define SIG_EM_E1 ZT_SIG_EM_E1
154 #define SIG_GR303FXOKS (0x100000 | ZT_SIG_FXOKS)
155 #define SIG_GR303FXSKS (0x200000 | ZT_SIG_FXSKS)
158 #define NUM_DCHANS 4 /* No more than 4 d-channels */
159 #define MAX_CHANNELS 672 /* No more than a DS3 per trunk group */
161 #define CHAN_PSEUDO -2
163 #define DCHAN_PROVISIONED (1 << 0)
164 #define DCHAN_NOTINALARM (1 << 1)
165 #define DCHAN_UP (1 << 2)
167 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
169 static int cur_emdigitwait = 250; /* Wait time in ms for digits on EM channel */
171 static char context[AST_MAX_CONTEXT] = "default";
172 static char cid_num[256] = "";
173 static char cid_name[256] = "";
174 static char defaultcic[64] = "";
175 static char defaultozz[64] = "";
177 static char language[MAX_LANGUAGE] = "";
178 static char musicclass[MAX_MUSICCLASS] = "";
179 static char progzone[10]= "";
181 static int usedistinctiveringdetection = 0;
183 static int transfertobusy = 1;
185 static int use_callerid = 1;
186 static int cid_signalling = CID_SIG_BELL;
187 static int cid_start = CID_START_RING;
188 static int zaptrcallerid = 0;
189 static int cur_signalling = -1;
191 static ast_group_t cur_group = 0;
192 static ast_group_t cur_callergroup = 0;
193 static ast_group_t cur_pickupgroup = 0;
194 static int relaxdtmf = 0;
196 static int immediate = 0;
198 static int stripmsd = 0;
200 static int callwaiting = 0;
202 static int callwaitingcallerid = 0;
204 static int hidecallerid = 0;
206 static int restrictcid = 0;
208 static int use_callingpres = 0;
210 static int callreturn = 0;
212 static int threewaycalling = 0;
214 static int transfer = 0;
216 static int canpark = 0;
218 static int cancallforward = 0;
220 static float rxgain = 0.0;
222 static float txgain = 0.0;
224 static int tonezone = -1;
226 static int echocancel;
228 static int echotraining;
232 static int echocanbridged = 0;
234 static int busydetect = 0;
236 static int busycount = 3;
237 static int busy_tonelength = 0;
238 static int busy_quietlength = 0;
240 static int callprogress = 0;
242 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
244 static char mailbox[AST_MAX_EXTENSION];
246 static int amaflags = 0;
250 static int numbufs = 4;
252 static int cur_prewink = -1;
253 static int cur_preflash = -1;
254 static int cur_wink = -1;
255 static int cur_flash = -1;
256 static int cur_start = -1;
257 static int cur_rxwink = -1;
258 static int cur_rxflash = -1;
259 static int cur_debounce = -1;
261 static int priindication_oob = 0;
264 static int minunused = 2;
265 static int minidle = 0;
266 static char idleext[AST_MAX_EXTENSION];
267 static char idledial[AST_MAX_EXTENSION];
268 static int overlapdial = 0;
269 static int facilityenable = 0;
270 static char internationalprefix[10] = "";
271 static char nationalprefix[10] = "";
272 static char localprefix[20] = "";
273 static char privateprefix[20] = "";
274 static char unknownprefix[20] = "";
275 static long resetinterval = 3600; /* How often (in seconds) to reset unused channels. Default 1 hour. */
276 static struct ast_channel inuse = { "GR-303InUse" };
277 #ifdef PRI_GETSET_TIMERS
278 static int pritimers[PRI_MAX_TIMERS];
280 static int pridebugfd = -1;
281 static char pridebugfilename[1024]="";
284 /* Wait up to 16 seconds for first digit (FXO logic) */
285 static int firstdigittimeout = 16000;
287 /* How long to wait for following digits (FXO logic) */
288 static int gendigittimeout = 8000;
290 /* How long to wait for an extra digit, if there is an ambiguous match */
291 static int matchdigittimeout = 3000;
293 static int usecnt =0;
294 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
296 /* Protect the interface list (of zt_pvt's) */
297 AST_MUTEX_DEFINE_STATIC(iflock);
300 static int ifcount = 0;
302 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
304 /* Whether we answer on a Polarity Switch event */
305 static int answeronpolarityswitch = 0;
307 /* Whether we hang up on a Polarity Switch event */
308 static int hanguponpolarityswitch = 0;
310 /* How long (ms) to ignore Polarity Switch events after we answer a call */
311 static int polarityonanswerdelay = 600;
313 /* When to send the CallerID signals (rings) */
314 static int sendcalleridafter = DEFAULT_CIDRINGS;
316 /* Protect the monitoring thread, so only one process can kill or start it, and not
317 when it's doing something critical. */
318 AST_MUTEX_DEFINE_STATIC(monlock);
320 /* This is the thread for the monitor which checks for input on the channels
321 which are not currently in use. */
322 static pthread_t monitor_thread = AST_PTHREADT_NULL;
324 static int restart_monitor(void);
326 static enum ast_bridge_result zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc);
328 static int zt_sendtext(struct ast_channel *c, const char *text);
330 static inline int zt_get_event(int fd)
332 /* Avoid the silly zt_getevent which ignores a bunch of events */
334 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
338 static inline int zt_wait_event(int fd)
340 /* Avoid the silly zt_waitevent which ignores a bunch of events */
342 i = ZT_IOMUX_SIGEVENT;
343 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
344 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
348 /* Chunk size to read -- we use 20ms chunks to make things happy. */
349 #define READ_SIZE 160
351 #define MASK_AVAIL (1 << 0) /* Channel available for PRI use */
352 #define MASK_INUSE (1 << 1) /* Channel currently in use */
354 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /* 300 ms */
355 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /* 300 ms */
356 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /* 500 ms */
357 #define MIN_MS_SINCE_FLASH ( (2000) ) /* 2000 ms */
358 #define RINGT ( (8000 * 8) / READ_SIZE)
364 static int r2prot = -1;
370 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
371 #define PRI_CHANNEL(p) ((p) & 0xff)
372 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
373 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
376 pthread_t master; /* Thread of master */
377 ast_mutex_t lock; /* Mutex */
378 char idleext[AST_MAX_EXTENSION]; /* Where to idle extra calls */
379 char idlecontext[AST_MAX_CONTEXT]; /* What context to use for idle */
380 char idledial[AST_MAX_EXTENSION]; /* What to dial before dumping */
381 int minunused; /* Min # of channels to keep empty */
382 int minidle; /* Min # of "idling" calls to keep active */
383 int nodetype; /* Node type */
384 int switchtype; /* Type of switch to emulate */
385 int nsf; /* Network-Specific Facilities */
386 int dialplan; /* Dialing plan */
387 int localdialplan; /* Local dialing plan */
388 char internationalprefix[10]; /* country access code ('00' for european dialplans) */
389 char nationalprefix[10]; /* area access code ('0' for european dialplans) */
390 char localprefix[20]; /* area access code + area code ('0'+area code for european dialplans) */
391 char privateprefix[20]; /* for private dialplans */
392 char unknownprefix[20]; /* for unknown dialplans */
393 int dchannels[NUM_DCHANS]; /* What channel are the dchannels on */
394 int trunkgroup; /* What our trunkgroup is */
395 int mastertrunkgroup; /* What trunk group is our master */
396 int prilogicalspan; /* Logical span number within trunk group */
397 int numchans; /* Num of channels we represent */
398 int overlapdial; /* In overlap dialing mode */
399 int facilityenable; /* Enable facility IEs */
400 struct pri *dchans[NUM_DCHANS]; /* Actual d-channels */
401 int dchanavail[NUM_DCHANS]; /* Whether each channel is available */
402 struct pri *pri; /* Currently active D-channel */
404 int fds[NUM_DCHANS]; /* FD's for d-channels */
409 time_t lastreset; /* time when unused channels were last reset */
410 long resetinterval; /* Interval (in seconds) for resetting unused channels */
411 struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */
412 struct zt_pvt *crvs; /* Member CRV structs */
413 struct zt_pvt *crvend; /* Pointer to end of CRV structs */
417 static struct zt_pri pris[NUM_SPANS];
419 static int pritype = PRI_CPE;
422 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
424 #define DEFAULT_PRI_DEBUG 0
427 static inline void pri_rel(struct zt_pri *pri)
429 ast_mutex_unlock(&pri->lock);
432 static int switchtype = PRI_SWITCH_NI2;
433 static int nsf = PRI_NSF_NONE;
434 static int dialplan = PRI_NATIONAL_ISDN + 1;
435 static int localdialplan = PRI_NATIONAL_ISDN + 1;
438 /* Shut up the compiler */
442 #define SUB_REAL 0 /* Active call */
443 #define SUB_CALLWAIT 1 /* Call-Waiting call on hold */
444 #define SUB_THREEWAY 2 /* Three-way call */
446 /* Polarity states */
447 #define POLARITY_IDLE 0
448 #define POLARITY_REV 1
451 static struct zt_distRings drings;
453 struct distRingData {
456 struct ringContextData {
457 char contextData[AST_MAX_CONTEXT];
459 struct zt_distRings {
460 struct distRingData ringnum[3];
461 struct ringContextData ringContext[3];
464 static char *subnames[] = {
470 struct zt_subchannel {
472 struct ast_channel *owner;
474 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
475 struct ast_frame f; /* One frame for each channel. How did this ever work before? */
476 unsigned int needringing:1;
477 unsigned int needbusy:1;
478 unsigned int needcongestion:1;
479 unsigned int needcallerid:1;
480 unsigned int needanswer:1;
481 unsigned int needflash:1;
482 unsigned int linear:1;
483 unsigned int inthreeway:1;
487 #define CONF_USER_REAL (1 << 0)
488 #define CONF_USER_THIRDCALL (1 << 1)
492 static struct zt_pvt {
494 struct ast_channel *owner; /* Our current active owner (if applicable) */
495 /* Up to three channels can be associated with this call */
497 struct zt_subchannel sub_unused; /* Just a safety precaution */
498 struct zt_subchannel subs[3]; /* Sub-channels */
499 struct zt_confinfo saveconf; /* Saved conference info */
501 struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */
502 struct zt_pvt *master; /* Master to us (we follow their conferencing) */
503 int inconference; /* If our real should be in the conference */
505 int sig; /* Signalling style */
506 int radio; /* radio type */
509 int tonezone; /* tone zone for this chan, or -1 for default */
510 struct zt_pvt *next; /* Next channel in list */
511 struct zt_pvt *prev; /* Prev channel in list */
515 unsigned int answeronpolarityswitch:1;
516 unsigned int busydetect:1;
517 unsigned int callreturn:1;
518 unsigned int callwaiting:1;
519 unsigned int callwaitingcallerid:1;
520 unsigned int cancallforward:1;
521 unsigned int canpark:1;
522 unsigned int confirmanswer:1; /* Wait for '#' to confirm answer */
523 unsigned int destroy:1;
524 unsigned int didtdd:1; /* flag to say its done it once */
525 unsigned int dialednone:1;
526 unsigned int dialing:1;
527 unsigned int digital:1;
529 unsigned int echobreak:1;
530 unsigned int echocanbridged:1;
531 unsigned int echocanon:1;
532 unsigned int faxhandled:1; /* Has a fax tone already been handled? */
533 unsigned int firstradio:1;
534 unsigned int hanguponpolarityswitch:1;
535 unsigned int hardwaredtmf:1;
536 unsigned int hidecallerid;
537 unsigned int ignoredtmf:1;
538 unsigned int immediate:1; /* Answer before getting digits? */
539 unsigned int inalarm:1;
540 unsigned int mate:1; /* flag to say its in MATE mode */
541 unsigned int outgoing:1;
542 unsigned int overlapdial:1;
543 unsigned int permcallwaiting:1;
544 unsigned int permhidecallerid:1; /* Whether to hide our outgoing caller ID or not */
545 unsigned int priindication_oob:1;
546 unsigned int pulse:1;
547 unsigned int pulsedial:1; /* whether a pulse dial phone is detected */
548 unsigned int restrictcid:1; /* Whether restrict the callerid -> only send ANI */
549 unsigned int threewaycalling:1;
550 unsigned int transfer:1;
551 unsigned int use_callerid:1; /* Whether or not to use caller id on this channel */
552 unsigned int use_callingpres:1; /* Whether to use the callingpres the calling switch sends */
553 unsigned int usedistinctiveringdetection:1;
554 unsigned int zaptrcallerid:1; /* should we use the callerid from incoming call on zap transfer or not */
555 unsigned int transfertobusy:1; /* allow flash-transfers to busy channels */
556 #if defined(ZAPATA_PRI)
557 unsigned int alerting:1;
558 unsigned int alreadyhungup:1;
559 unsigned int isidlecall:1;
560 unsigned int resetting:1;
561 unsigned int setup_ack:1;
563 #if defined(ZAPATA_R2)
564 unsigned int hasr2call:1;
565 unsigned int r2blocked:1;
566 unsigned int sigchecked:1;
569 struct zt_distRings drings;
571 char context[AST_MAX_CONTEXT];
572 char defcontext[AST_MAX_CONTEXT];
573 char exten[AST_MAX_EXTENSION];
574 char language[MAX_LANGUAGE];
575 char musicclass[MAX_MUSICCLASS];
577 char cid_ani[AST_MAX_EXTENSION];
579 char cid_num[AST_MAX_EXTENSION];
580 int cid_ton; /* Type Of Number (TON) */
581 char cid_name[AST_MAX_EXTENSION];
582 char lastcid_num[AST_MAX_EXTENSION];
583 char lastcid_name[AST_MAX_EXTENSION];
584 char *origcid_num; /* malloced original callerid */
585 char *origcid_name; /* malloced original callerid */
586 char callwait_num[AST_MAX_EXTENSION];
587 char callwait_name[AST_MAX_EXTENSION];
588 char rdnis[AST_MAX_EXTENSION];
589 char dnid[AST_MAX_EXTENSION];
592 int confno; /* Our conference */
593 int confusers; /* Who is using our conference */
594 int propconfno; /* Propagated conference number */
595 ast_group_t callgroup;
596 ast_group_t pickupgroup;
597 int channel; /* Channel Number or CRV */
598 int span; /* Span number */
599 time_t guardtime; /* Must wait this much time before using for new call */
600 int cid_signalling; /* CID signalling type bell202 or v23 */
601 int cid_start; /* CID start indicator, polarity or ring */
602 int callingpres; /* The value of callling presentation that we're going to use when placing a PRI call */
603 int callwaitingrepeat; /* How many samples to wait before repeating call waiting */
604 int cidcwexpire; /* When to expire our muting for CID/CW */
605 unsigned char *cidspill;
617 int busy_quietlength;
619 struct timeval flashtime; /* Last flash-hook time */
621 int cref; /* Call reference number */
622 ZT_DIAL_OPERATION dop;
623 int whichwink; /* SIG_FEATDMF_TA Which wink are we on? */
625 char accountcode[AST_MAX_ACCOUNT_CODE]; /* Account code */
626 int amaflags; /* AMA Flags */
627 struct tdd_state *tdd; /* TDD flag */
628 char call_forward[AST_MAX_EXTENSION];
629 char mailbox[AST_MAX_EXTENSION];
633 int distinctivering; /* Which distinctivering to use */
634 int cidrings; /* Which ring to deliver CID on */
635 int dtmfrelax; /* whether to run in relaxed DTMF mode */
638 int polarityonanswerdelay;
639 struct timeval polaritydelaytv;
640 int sendcalleridafter;
643 struct zt_pvt *bearer;
644 struct zt_pvt *realcall;
657 } *iflist = NULL, *ifend = NULL;
659 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
660 static int zt_digit(struct ast_channel *ast, char digit);
661 static int zt_sendtext(struct ast_channel *c, const char *text);
662 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
663 static int zt_hangup(struct ast_channel *ast);
664 static int zt_answer(struct ast_channel *ast);
665 struct ast_frame *zt_read(struct ast_channel *ast);
666 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
667 struct ast_frame *zt_exception(struct ast_channel *ast);
668 static int zt_indicate(struct ast_channel *chan, int condition);
669 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
670 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
672 static const struct ast_channel_tech zap_tech = {
674 .description = tdesc,
675 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
676 .requester = zt_request,
677 .send_digit = zt_digit,
678 .send_text = zt_sendtext,
685 .exception = zt_exception,
686 .indicate = zt_indicate,
688 .setoption = zt_setoption,
692 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
694 #define GET_CHANNEL(p) ((p)->channel)
697 struct zt_pvt *round_robin[32];
700 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
703 /* Grab the lock first */
705 res = ast_mutex_trylock(&pri->lock);
707 ast_mutex_unlock(&pvt->lock);
708 /* Release the lock and try again */
710 ast_mutex_lock(&pvt->lock);
713 /* Then break the poll */
714 pthread_kill(pri->master, SIGURG);
719 #define NUM_CADENCE_MAX 25
720 static int num_cadence = 4;
721 static int user_has_defined_cadences = 0;
723 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
724 { { 125, 125, 2000, 4000 } }, /* Quick chirp followed by normal ring */
725 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /* British style ring */
726 { { 125, 125, 125, 125, 125, 4000 } }, /* Three short bursts */
727 { { 1000, 500, 2500, 5000 } }, /* Long ring */
730 int receivedRingT; /* Used to find out what ringtone we are on */
732 /* cidrings says in which pause to transmit the cid information, where the first pause
733 * is 1, the second pause is 2 and so on.
736 static int cidrings[NUM_CADENCE_MAX] = {
737 2, /* Right after first long ring */
738 4, /* Right after long part */
739 3, /* After third chirp */
740 2, /* Second spell */
743 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
744 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
746 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
747 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
749 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
752 if (p->subs[0].owner == ast)
754 else if (p->subs[1].owner == ast)
756 else if (p->subs[2].owner == ast)
761 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
767 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
769 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
772 struct ast_frame null = { AST_FRAME_NULL, };
775 ast_mutex_unlock(&pri->lock);
778 if (p->subs[a].owner) {
779 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
780 ast_mutex_unlock(&p->lock);
782 ast_mutex_lock(&p->lock);
784 ast_queue_frame(p->subs[a].owner, &null);
785 ast_mutex_unlock(&p->subs[a].owner->lock);
793 ast_mutex_lock(&pri->lock);
798 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
800 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
803 /* We must unlock the PRI to avoid the possibility of a deadlock */
806 ast_mutex_unlock(&pri->lock);
810 if (ast_mutex_trylock(&p->owner->lock)) {
811 ast_mutex_unlock(&p->lock);
813 ast_mutex_lock(&p->lock);
815 ast_queue_frame(p->owner, f);
816 ast_mutex_unlock(&p->owner->lock);
824 ast_mutex_lock(&pri->lock);
828 static int restore_gains(struct zt_pvt *p);
830 static void swap_subs(struct zt_pvt *p, int a, int b)
834 struct ast_channel *towner;
836 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
838 tchan = p->subs[a].chan;
839 towner = p->subs[a].owner;
840 tinthreeway = p->subs[a].inthreeway;
842 p->subs[a].chan = p->subs[b].chan;
843 p->subs[a].owner = p->subs[b].owner;
844 p->subs[a].inthreeway = p->subs[b].inthreeway;
846 p->subs[b].chan = tchan;
847 p->subs[b].owner = towner;
848 p->subs[b].inthreeway = tinthreeway;
850 if (p->subs[a].owner)
851 p->subs[a].owner->fds[0] = p->subs[a].zfd;
852 if (p->subs[b].owner)
853 p->subs[b].owner->fds[0] = p->subs[b].zfd;
854 wakeup_sub(p, a, NULL);
855 wakeup_sub(p, b, NULL);
858 static int zt_open(char *fn)
866 for (x=0;x<strlen(fn);x++) {
867 if (!isdigit(fn[x])) {
875 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
878 fn = "/dev/zap/channel";
880 fd = open(fn, O_RDWR | O_NONBLOCK);
882 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
886 if (ioctl(fd, ZT_SPECIFY, &chan)) {
890 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
895 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
899 static void zt_close(int fd)
905 int zt_setlinear(int zfd, int linear)
908 res = ioctl(zfd, ZT_SETLINEAR, &linear);
915 int zt_setlaw(int zfd, int law)
918 res = ioctl(zfd, ZT_SETLAW, &law);
924 static int alloc_sub(struct zt_pvt *p, int x)
928 if (p->subs[x].zfd < 0) {
929 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
930 if (p->subs[x].zfd > -1) {
931 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
933 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
934 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
935 bi.numbufs = numbufs;
936 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
938 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
941 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
942 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
943 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
944 zt_close(p->subs[x].zfd);
949 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
952 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
955 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
959 static int unalloc_sub(struct zt_pvt *p, int x)
962 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
965 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
966 if (p->subs[x].zfd > -1) {
967 zt_close(p->subs[x].zfd);
970 p->subs[x].linear = 0;
972 p->subs[x].owner = NULL;
973 p->subs[x].inthreeway = 0;
974 p->polarity = POLARITY_IDLE;
975 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
979 static int zt_digit(struct ast_channel *ast, char digit)
981 ZT_DIAL_OPERATION zo;
986 ast_mutex_lock(&p->lock);
987 index = zt_get_index(ast, p, 0);
988 if ((index == SUB_REAL) && p->owner) {
990 if (p->sig == SIG_PRI && ast->_state == AST_STATE_DIALING && (p->proceeding < 2)) {
992 if (!pri_grab(p, p->pri)) {
993 pri_information(p->pri->pri,p->call,digit);
996 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
997 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
998 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
999 res = strlen(p->dialdest);
1000 p->dialdest[res++] = digit;
1001 p->dialdest[res] = '\0';
1007 zo.op = ZT_DIAL_OP_APPEND;
1008 zo.dialstr[0] = 'T';
1009 zo.dialstr[1] = digit;
1011 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1012 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1017 ast_mutex_unlock(&p->lock);
1021 static char *events[] = {
1034 "Hook Transition Complete",
1046 { ZT_ALARM_RED, "Red Alarm" },
1047 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1048 { ZT_ALARM_BLUE, "Blue Alarm" },
1049 { ZT_ALARM_RECOVER, "Recovering" },
1050 { ZT_ALARM_LOOPBACK, "Loopback" },
1051 { ZT_ALARM_NOTOPEN, "Not Open" },
1052 { ZT_ALARM_NONE, "None" },
1055 static char *alarm2str(int alarm)
1058 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
1059 if (alarms[x].alarm & alarm)
1060 return alarms[x].name;
1062 return alarm ? "Unknown Alarm" : "No Alarm";
1065 static char *event2str(int event)
1067 static char buf[256];
1068 if ((event < 18) && (event > -1))
1069 return events[event];
1070 sprintf(buf, "Event %d", event); /* safe */
1075 static char *dialplan2str(int dialplan)
1077 if (dialplan == -1) {
1078 return("Dynamically set dialplan in ISDN");
1080 return(pri_plan2str(dialplan));
1085 static int str2r2prot(char *swtype)
1087 if (!strcasecmp(swtype, "ar"))
1088 return MFCR2_PROT_ARGENTINA;
1090 if (!strcasecmp(swtype, "cn"))
1091 return MFCR2_PROT_CHINA;
1093 if (!strcasecmp(swtype, "kr"))
1094 return MFCR2_PROT_KOREA;
1100 static char *zap_sig2str(int sig)
1102 static char buf[256];
1105 return "E & M Immediate";
1107 return "E & M Wink";
1111 return "Feature Group D (DTMF)";
1113 return "Feature Group D (MF)";
1114 case SIG_FEATDMF_TA:
1115 return "Feature Groud D (MF) Tandem Access";
1117 return "Feature Group B (MF)";
1121 return "FXS Loopstart";
1123 return "FXS Groundstart";
1125 return "FXS Kewlstart";
1127 return "FXO Loopstart";
1129 return "FXO Groundstart";
1131 return "FXO Kewlstart";
1133 return "PRI Signalling";
1135 return "R2 Signalling";
1137 return "SF (Tone) Signalling Immediate";
1139 return "SF (Tone) Signalling Wink";
1141 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1142 case SIG_SF_FEATDMF:
1143 return "SF (Tone) Signalling with Feature Group D (MF)";
1145 return "SF (Tone) Signalling with Feature Group B (MF)";
1146 case SIG_GR303FXOKS:
1147 return "GR-303 Signalling with FXOKS";
1148 case SIG_GR303FXSKS:
1149 return "GR-303 Signalling with FXSKS";
1151 return "Pseudo Signalling";
1153 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1158 #define sig2str zap_sig2str
1160 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1162 /* If the conference already exists, and we're already in it
1163 don't bother doing anything */
1166 memset(&zi, 0, sizeof(zi));
1169 if (slavechannel > 0) {
1170 /* If we have only one slave, do a digital mon */
1171 zi.confmode = ZT_CONF_DIGITALMON;
1172 zi.confno = slavechannel;
1175 /* Real-side and pseudo-side both participate in conference */
1176 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1177 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1179 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1180 zi.confno = p->confno;
1182 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1186 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1187 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1190 if (slavechannel < 1) {
1191 p->confno = zi.confno;
1193 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1194 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1198 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1200 /* If they're listening to our channel, they're ours */
1201 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1203 /* If they're a talker on our (allocated) conference, they're ours */
1204 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1209 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1212 if (/* Can't delete if there's no zfd */
1214 /* Don't delete from the conference if it's not our conference */
1216 /* Don't delete if we don't think it's conferenced at all (implied) */
1218 memset(&zi, 0, sizeof(zi));
1222 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1223 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1226 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1227 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1231 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1235 struct zt_pvt *slave = NULL;
1236 /* Start out optimistic */
1238 /* Update conference state in a stateless fashion */
1240 /* Any three-way calling makes slave native mode *definitely* out
1242 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1245 /* If we don't have any 3-way calls, check to see if we have
1246 precisely one slave */
1247 if (useslavenative) {
1248 for (x=0;x<MAX_SLAVES;x++) {
1251 /* Whoops already have a slave! No
1252 slave native and stop right away */
1257 /* We have one slave so far */
1258 slave = p->slaves[x];
1263 /* If no slave, slave native definitely out */
1266 else if (slave->law != p->law) {
1272 return useslavenative;
1275 static int reset_conf(struct zt_pvt *p)
1278 memset(&zi, 0, sizeof(zi));
1280 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1281 if (p->subs[SUB_REAL].zfd > -1) {
1282 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1283 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1288 static int update_conf(struct zt_pvt *p)
1293 struct zt_pvt *slave = NULL;
1295 useslavenative = isslavenative(p, &slave);
1296 /* Start with the obvious, general stuff */
1298 /* Look for three way calls */
1299 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1300 conf_add(p, &p->subs[x], x, 0);
1303 conf_del(p, &p->subs[x], x);
1306 /* If we have a slave, add him to our conference now. or DAX
1307 if this is slave native */
1308 for (x=0;x<MAX_SLAVES;x++) {
1311 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1313 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1318 /* If we're supposed to be in there, do so now */
1319 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1321 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1323 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1327 /* If we have a master, add ourselves to his conference */
1329 if (isslavenative(p->master, NULL)) {
1330 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1332 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1336 /* Nobody is left (or should be left) in our conference.
1340 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1344 static void zt_enable_ec(struct zt_pvt *p)
1351 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1355 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1358 if (p->echocancel) {
1359 if (p->sig == SIG_PRI) {
1361 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1363 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1366 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1368 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1371 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1374 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1377 static void zt_train_ec(struct zt_pvt *p)
1381 if (p && p->echocancel && p->echotraining) {
1382 x = p->echotraining;
1383 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1385 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1387 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1390 ast_log(LOG_DEBUG, "No echo training requested\n");
1393 static void zt_disable_ec(struct zt_pvt *p)
1397 if (p->echocancel) {
1399 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1401 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1403 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1408 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1415 if ((rxgain != 0.0) || (txgain != 0.0)) {
1416 /* caluculate linear value of tx gain */
1417 ltxgain = pow(10.0,txgain / 20.0);
1418 /* caluculate linear value of rx gain */
1419 lrxgain = pow(10.0,rxgain / 20.0);
1420 if (law == ZT_LAW_ALAW) {
1421 for (j=0;j<256;j++) {
1422 k = (int)(((float)AST_ALAW(j)) * lrxgain);
1423 if (k > 32767) k = 32767;
1424 if (k < -32767) k = -32767;
1425 g.rxgain[j] = AST_LIN2A(k);
1426 k = (int)(((float)AST_ALAW(j)) * ltxgain);
1427 if (k > 32767) k = 32767;
1428 if (k < -32767) k = -32767;
1429 g.txgain[j] = AST_LIN2A(k);
1432 for (j=0;j<256;j++) {
1433 k = (int)(((float)AST_MULAW(j)) * lrxgain);
1434 if (k > 32767) k = 32767;
1435 if (k < -32767) k = -32767;
1436 g.rxgain[j] = AST_LIN2MU(k);
1437 k = (int)(((float)AST_MULAW(j)) * ltxgain);
1438 if (k > 32767) k = 32767;
1439 if (k < -32767) k = -32767;
1440 g.txgain[j] = AST_LIN2MU(k);
1444 for (j=0;j<256;j++) {
1451 return(ioctl(fd,ZT_SETGAINS,&g));
1454 static inline int zt_set_hook(int fd, int hs)
1458 res = ioctl(fd, ZT_HOOK, &x);
1461 if (errno == EINPROGRESS) return 0;
1462 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1467 static inline int zt_confmute(struct zt_pvt *p, int muted)
1471 if (p->sig == SIG_PRI) {
1473 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1475 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1477 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1479 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1483 static int save_conference(struct zt_pvt *p)
1485 struct zt_confinfo c;
1487 if (p->saveconf.confmode) {
1488 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1491 p->saveconf.chan = 0;
1492 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1494 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1495 p->saveconf.confmode = 0;
1500 c.confmode = ZT_CONF_NORMAL;
1501 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1503 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1507 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1511 static int restore_conference(struct zt_pvt *p)
1514 if (p->saveconf.confmode) {
1515 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1516 p->saveconf.confmode = 0;
1518 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1523 ast_log(LOG_DEBUG, "Restored conferencing\n");
1527 static int send_callerid(struct zt_pvt *p);
1529 int send_cwcidspill(struct zt_pvt *p)
1533 p->cidspill = malloc(MAX_CALLERID_SIZE);
1535 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
1536 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1537 /* Make sure we account for the end */
1538 p->cidlen += READ_SIZE * 4;
1541 if (option_verbose > 2)
1542 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1547 static int has_voicemail(struct zt_pvt *p)
1550 return ast_app_has_voicemail(p->mailbox, NULL);
1553 static int send_callerid(struct zt_pvt *p)
1555 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1557 /* Take out of linear mode if necessary */
1558 if (p->subs[SUB_REAL].linear) {
1559 p->subs[SUB_REAL].linear = 0;
1560 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1562 while(p->cidpos < p->cidlen) {
1563 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1565 if (errno == EAGAIN)
1568 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1578 if (p->callwaitcas) {
1579 /* Wait for CID/CW to expire */
1580 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1582 restore_conference(p);
1586 static int zt_callwait(struct ast_channel *ast)
1588 struct zt_pvt *p = ast->tech_pvt;
1589 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1591 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1594 p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
1598 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1599 if (!p->callwaitrings && p->callwaitingcallerid) {
1600 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1602 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1604 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1606 p->cidlen = 2400 + READ_SIZE * 4;
1611 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
1617 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1619 struct zt_pvt *p = ast->tech_pvt;
1625 char dest[256]; /* must be same length as p->dialdest */
1626 ast_mutex_lock(&p->lock);
1627 ast_copy_string(dest, rdest, sizeof(dest));
1628 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1629 if ((ast->_state == AST_STATE_BUSY)) {
1630 p->subs[SUB_REAL].needbusy = 1;
1631 ast_mutex_unlock(&p->lock);
1634 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1635 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1636 ast_mutex_unlock(&p->lock);
1640 if (p->radio) /* if a radio channel, up immediately */
1642 /* Special pseudo -- automatically up */
1643 ast_setstate(ast, AST_STATE_UP);
1644 ast_mutex_unlock(&p->lock);
1647 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1648 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1650 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1653 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1659 if (p->owner == ast) {
1660 /* Normal ring, on hook */
1662 /* Don't send audio while on hook, until the call is answered */
1664 if (p->use_callerid) {
1665 /* Generate the Caller-ID spill if desired */
1667 ast_log(LOG_WARNING, "cidspill already exists??\n");
1670 p->cidspill = malloc(MAX_CALLERID_SIZE);
1673 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1677 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
1679 /* Choose proper cadence */
1680 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1681 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1682 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1683 p->cidrings = cidrings[p->distinctivering - 1];
1685 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1686 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1687 p->cidrings = p->sendcalleridafter;
1691 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1692 c = strchr(dest, '/');
1695 if (c && (strlen(c) < p->stripmsd)) {
1696 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1700 p->dop.op = ZT_DIAL_OP_REPLACE;
1701 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1702 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1704 p->dop.dialstr[0] = '\0';
1707 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1708 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1709 ast_mutex_unlock(&p->lock);
1714 /* Call waiting call */
1715 p->callwaitrings = 0;
1716 if (ast->cid.cid_num)
1717 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
1719 p->callwait_num[0] = '\0';
1720 if (ast->cid.cid_name)
1721 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
1723 p->callwait_name[0] = '\0';
1724 /* Call waiting tone instead */
1725 if (zt_callwait(ast)) {
1726 ast_mutex_unlock(&p->lock);
1729 /* Make ring-back */
1730 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1731 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1734 n = ast->cid.cid_name;
1735 l = ast->cid.cid_num;
1737 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1739 p->lastcid_num[0] = '\0';
1741 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1743 p->lastcid_name[0] = '\0';
1744 ast_setstate(ast, AST_STATE_RINGING);
1745 index = zt_get_index(ast, p, 0);
1747 p->subs[index].needringing = 1;
1763 case SIG_SF_FEATDMF:
1764 case SIG_FEATDMF_TA:
1766 c = strchr(dest, '/');
1771 if (strlen(c) < p->stripmsd) {
1772 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1773 ast_mutex_unlock(&p->lock);
1777 /* Start the trunk, if not GR-303 */
1781 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1783 if (errno != EINPROGRESS) {
1784 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1785 ast_mutex_unlock(&p->lock);
1792 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1793 p->dop.op = ZT_DIAL_OP_REPLACE;
1794 if (p->sig == SIG_FEATD) {
1795 l = ast->cid.cid_num;
1797 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c + p->stripmsd);
1799 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c + p->stripmsd);
1801 if (p->sig == SIG_FEATDMF) {
1802 l = ast->cid.cid_num;
1804 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c + p->stripmsd);
1806 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c + p->stripmsd);
1808 if (p->sig == SIG_FEATDMF_TA) {
1809 char *cic = NULL, *ozz = NULL;
1810 /* If you have to go through a Tandem Access point you need to use this */
1811 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1814 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1818 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1819 ast_mutex_unlock(&p->lock);
1822 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1823 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c + p->stripmsd);
1826 if (p->sig == SIG_E911) {
1827 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1829 if (p->sig == SIG_FEATB) {
1830 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c + p->stripmsd);
1833 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c + p->stripmsd);
1835 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c + p->stripmsd);
1836 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1837 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1838 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1839 p->echorest[sizeof(p->echorest) - 1] = '\0';
1841 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1845 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1847 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1848 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1849 ast_mutex_unlock(&p->lock);
1853 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1855 if (strlen(c + p->stripmsd) < 1) p->dialednone = 1;
1856 ast_setstate(ast, AST_STATE_DIALING);
1859 /* Special pseudo -- automatically up*/
1860 ast_setstate(ast, AST_STATE_UP);
1863 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1864 p->dialdest[0] = '\0';
1867 ast_log(LOG_DEBUG, "not yet implemented\n");
1868 ast_mutex_unlock(&p->lock);
1876 int prilocaldialplan;
1878 c = strchr(dest, '/');
1883 if (!p->hidecallerid) {
1884 l = ast->cid.cid_num;
1885 n = ast->cid.cid_name;
1890 if (strlen(c) < p->stripmsd) {
1891 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1892 ast_mutex_unlock(&p->lock);
1895 if (p->sig != SIG_FXSKS) {
1896 p->dop.op = ZT_DIAL_OP_REPLACE;
1897 s = strchr(c + p->stripmsd, 'w');
1900 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
1902 p->dop.dialstr[0] = '\0';
1906 p->dop.dialstr[0] = '\0';
1909 if (pri_grab(p, p->pri)) {
1910 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
1911 ast_mutex_unlock(&p->lock);
1914 if (!(p->call = pri_new_call(p->pri->pri))) {
1915 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
1917 ast_mutex_unlock(&p->lock);
1920 if (!(sr = pri_sr_new())) {
1921 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
1923 ast_mutex_unlock(&p->lock);
1925 if (p->bearer || (p->sig == SIG_FXSKS)) {
1927 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);
1928 p->bearer->call = p->call;
1930 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
1931 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
1933 p->digital = IS_DIGITAL(ast->transfercapability);
1934 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p),
1935 p->pri->nodetype == PRI_NETWORK ? 0 : 1, 1);
1936 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
1938 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
1939 if (p->pri->facilityenable)
1940 pri_facility_enable(p->pri->pri);
1942 if (option_verbose > 2)
1943 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
1945 pridialplan = p->pri->dialplan - 1;
1946 if (pridialplan == -2) { /* compute dynamically */
1947 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
1948 dp_strip = strlen(p->pri->internationalprefix);
1949 pridialplan = PRI_INTERNATIONAL_ISDN;
1950 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
1951 dp_strip = strlen(p->pri->nationalprefix);
1952 pridialplan = PRI_NATIONAL_ISDN;
1954 pridialplan = PRI_LOCAL_ISDN;
1957 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, (s && *s) ? 1 : 0);
1960 prilocaldialplan = p->pri->localdialplan - 1;
1961 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
1962 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
1963 ldp_strip = strlen(p->pri->internationalprefix);
1964 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
1965 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
1966 ldp_strip = strlen(p->pri->nationalprefix);
1967 prilocaldialplan = PRI_NATIONAL_ISDN;
1969 prilocaldialplan = PRI_LOCAL_ISDN;
1972 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
1973 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
1974 PRES_NUMBER_NOT_AVAILABLE);
1975 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
1976 if (pri_setup(p->pri->pri, p->call, sr)) {
1977 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
1978 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
1980 ast_mutex_unlock(&p->lock);
1985 ast_setstate(ast, AST_STATE_DIALING);
1989 ast_mutex_unlock(&p->lock);
1993 static void destroy_zt_pvt(struct zt_pvt **pvt)
1995 struct zt_pvt *p = *pvt;
1996 /* Remove channel from the list */
1998 p->prev->next = p->next;
2000 p->next->prev = p->prev;
2001 ast_mutex_destroy(&p->lock);
2006 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2016 for (i = 0; i < 3; i++) {
2017 if (cur->subs[i].owner) {
2023 prev->next = cur->next;
2025 prev->next->prev = prev;
2031 iflist->prev = NULL;
2035 if (cur->subs[SUB_REAL].zfd > -1) {
2036 zt_close(cur->subs[SUB_REAL].zfd);
2038 destroy_zt_pvt(&cur);
2042 prev->next = cur->next;
2044 prev->next->prev = prev;
2050 iflist->prev = NULL;
2054 if (cur->subs[SUB_REAL].zfd > -1) {
2055 zt_close(cur->subs[SUB_REAL].zfd);
2057 destroy_zt_pvt(&cur);
2063 int pri_is_up(struct zt_pri *pri)
2066 for (x=0;x<NUM_DCHANS;x++) {
2067 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2073 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2075 bearer->owner = &inuse;
2076 bearer->realcall = crv;
2077 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2078 if (crv->subs[SUB_REAL].owner)
2079 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2080 crv->bearer = bearer;
2081 crv->call = bearer->call;
2086 static char *pri_order(int level)
2096 return "Quaternary";
2102 /* Returns fd of the active dchan */
2103 int pri_active_dchan_fd(struct zt_pri *pri)
2107 for (x = 0; x < NUM_DCHANS; x++) {
2108 if ((pri->dchans[x] == pri->pri))
2115 int pri_find_dchan(struct zt_pri *pri)
2122 for(x=0;x<NUM_DCHANS;x++) {
2123 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2125 if (pri->dchans[x] == old) {
2131 ast_log(LOG_WARNING, "No D-channels available! Using Primary on channel anyway %d!\n",
2132 pri->dchannels[newslot]);
2134 if (old && (oldslot != newslot))
2135 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2136 pri->dchannels[oldslot], pri->dchannels[newslot]);
2137 pri->pri = pri->dchans[newslot];
2142 static int zt_hangup(struct ast_channel *ast)
2146 /*static int restore_gains(struct zt_pvt *p);*/
2147 struct zt_pvt *p = ast->tech_pvt;
2148 struct zt_pvt *tmp = NULL;
2149 struct zt_pvt *prev = NULL;
2153 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2154 if (!ast->tech_pvt) {
2155 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2159 ast_mutex_lock(&p->lock);
2161 index = zt_get_index(ast, p, 1);
2163 if (p->sig == SIG_PRI) {
2165 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2171 if (p->origcid_num) {
2172 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2173 free(p->origcid_num);
2174 p->origcid_num = NULL;
2176 if (p->origcid_name) {
2177 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2178 free(p->origcid_name);
2179 p->origcid_name = NULL;
2182 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2186 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2187 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2191 /* Real channel, do some fixup */
2192 p->subs[index].owner = NULL;
2193 p->subs[index].needanswer = 0;
2194 p->subs[index].needflash = 0;
2195 p->subs[index].needringing = 0;
2196 p->subs[index].needbusy = 0;
2197 p->subs[index].needcongestion = 0;
2198 p->subs[index].linear = 0;
2199 p->subs[index].needcallerid = 0;
2200 p->polarity = POLARITY_IDLE;
2201 zt_setlinear(p->subs[index].zfd, 0);
2202 if (index == SUB_REAL) {
2203 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2204 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2205 if (p->subs[SUB_CALLWAIT].inthreeway) {
2206 /* We had flipped over to answer a callwait and now it's gone */
2207 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2208 /* Move to the call-wait, but un-own us until they flip back. */
2209 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2210 unalloc_sub(p, SUB_CALLWAIT);
2213 /* The three way hung up, but we still have a call wait */
2214 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2215 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2216 unalloc_sub(p, SUB_THREEWAY);
2217 if (p->subs[SUB_REAL].inthreeway) {
2218 /* This was part of a three way call. Immediately make way for
2220 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2221 p->owner = p->subs[SUB_REAL].owner;
2223 /* This call hasn't been completed yet... Set owner to NULL */
2224 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2227 p->subs[SUB_REAL].inthreeway = 0;
2229 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2230 /* Move to the call-wait and switch back to them. */
2231 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2232 unalloc_sub(p, SUB_CALLWAIT);
2233 p->owner = p->subs[SUB_REAL].owner;
2234 if (p->owner->_state != AST_STATE_UP)
2235 p->subs[SUB_REAL].needanswer = 1;
2236 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2237 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2238 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2239 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2240 unalloc_sub(p, SUB_THREEWAY);
2241 if (p->subs[SUB_REAL].inthreeway) {
2242 /* This was part of a three way call. Immediately make way for
2244 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2245 p->owner = p->subs[SUB_REAL].owner;
2247 /* This call hasn't been completed yet... Set owner to NULL */
2248 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2251 p->subs[SUB_REAL].inthreeway = 0;
2253 } else if (index == SUB_CALLWAIT) {
2254 /* Ditch the holding callwait call, and immediately make it availabe */
2255 if (p->subs[SUB_CALLWAIT].inthreeway) {
2256 /* This is actually part of a three way, placed on hold. Place the third part
2257 on music on hold now */
2258 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2259 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2260 p->subs[SUB_THREEWAY].inthreeway = 0;
2261 /* Make it the call wait now */
2262 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2263 unalloc_sub(p, SUB_THREEWAY);
2265 unalloc_sub(p, SUB_CALLWAIT);
2266 } else if (index == SUB_THREEWAY) {
2267 if (p->subs[SUB_CALLWAIT].inthreeway) {
2268 /* The other party of the three way call is currently in a call-wait state.
2269 Start music on hold for them, and take the main guy out of the third call */
2270 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2271 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2272 p->subs[SUB_CALLWAIT].inthreeway = 0;
2274 p->subs[SUB_REAL].inthreeway = 0;
2275 /* If this was part of a three way call index, let us make
2276 another three way call */
2277 unalloc_sub(p, SUB_THREEWAY);
2279 /* This wasn't any sort of call, but how are we an index? */
2280 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2285 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2288 p->distinctivering = 0;
2289 p->confirmanswer = 0;
2295 p->onhooktime = time(NULL);
2302 ast_dsp_free(p->dsp);
2306 law = ZT_LAW_DEFAULT;
2307 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2309 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2310 /* Perform low level hangup if no owner left */
2313 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2314 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2315 if (!pri_grab(p, p->pri)) {
2316 if (p->alreadyhungup) {
2317 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2318 pri_hangup(p->pri->pri, p->call, -1);
2321 p->bearer->call = NULL;
2323 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2324 int icause = ast->hangupcause ? ast->hangupcause : -1;
2325 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2326 p->alreadyhungup = 1;
2328 p->bearer->alreadyhungup = 1;
2331 icause = atoi(cause);
2333 pri_hangup(p->pri->pri, p->call, icause);
2336 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2339 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2344 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2351 if (p->sig == SIG_R2) {
2353 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2361 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2362 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2364 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2370 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2373 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2375 /* If they're off hook, try playing congestion */
2376 if ((par.rxisoffhook) && (!p->radio))
2377 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2379 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2385 /* Make sure we're not made available for at least two seconds assuming
2386 we were actually used for an inbound or outbound call. */
2387 if (ast->_state != AST_STATE_RESERVED) {
2388 time(&p->guardtime);
2393 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2400 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2401 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2405 p->callwaiting = p->permcallwaiting;
2406 p->hidecallerid = p->permhidecallerid;
2411 /* Restore data mode */
2412 if (p->sig == SIG_PRI) {
2414 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2418 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2419 /* Free up the bearer channel as well, and
2420 don't use its file descriptor anymore */
2421 update_conf(p->bearer);
2422 reset_conf(p->bearer);
2423 p->bearer->owner = NULL;
2424 p->bearer->realcall = NULL;
2426 p->subs[SUB_REAL].zfd = -1;
2434 p->callwaitingrepeat = 0;
2436 ast->tech_pvt = NULL;
2437 ast_mutex_unlock(&p->lock);
2438 ast_mutex_lock(&usecnt_lock);
2441 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2442 ast_mutex_unlock(&usecnt_lock);
2443 ast_update_use_count();
2444 if (option_verbose > 2)
2445 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2447 ast_mutex_lock(&iflock);
2453 destroy_channel(prev, tmp, 0);
2461 ast_mutex_unlock(&iflock);
2465 static int zt_answer(struct ast_channel *ast)
2467 struct zt_pvt *p = ast->tech_pvt;
2470 int oldstate = ast->_state;
2471 ast_setstate(ast, AST_STATE_UP);
2472 ast_mutex_lock(&p->lock);
2473 index = zt_get_index(ast, p, 0);
2476 /* nothing to do if a radio channel */
2478 ast_mutex_unlock(&p->lock);
2497 case SIG_SF_FEATDMF:
2502 /* Pick up the line */
2503 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2504 if(p->hanguponpolarityswitch) {
2505 gettimeofday(&p->polaritydelaytv, NULL);
2507 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2508 tone_zone_play_tone(p->subs[index].zfd, -1);
2510 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2511 if (oldstate == AST_STATE_RINGING) {
2512 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2513 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2514 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2515 p->owner = p->subs[SUB_REAL].owner;
2518 if (p->sig & __ZT_SIG_FXS) {
2525 /* Send a pri acknowledge */
2526 if (!pri_grab(p, p->pri)) {
2528 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2531 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2538 res = mfcr2_AnswerCall(p->r2, NULL);
2540 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2544 ast_mutex_unlock(&p->lock);
2547 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2550 ast_mutex_unlock(&p->lock);
2554 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2559 struct zt_pvt *p = chan->tech_pvt;
2562 if ((option != AST_OPTION_TONE_VERIFY) && (option != AST_OPTION_AUDIO_MODE) &&
2563 (option != AST_OPTION_TDD) && (option != AST_OPTION_RELAXDTMF))
2569 if ((!cp) || (datalen < 1))
2575 case AST_OPTION_TONE_VERIFY:
2580 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2581 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2584 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2585 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2588 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2589 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2593 case AST_OPTION_TDD: /* turn on or off TDD */
2594 if (!*cp) { /* turn it off */
2595 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2596 if (p->tdd) tdd_free(p->tdd);
2602 ast_log(LOG_DEBUG, "Set option TDD MODE, value: MATE(2) on %s\n",chan->name);
2603 else ast_log(LOG_DEBUG, "Set option TDD MODE, value: ON(1) on %s\n",chan->name);
2606 /* otherwise, turn it on */
2607 if (!p->didtdd) { /* if havent done it yet */
2608 unsigned char mybuf[41000],*buf;
2609 int size,res,fd,len;
2611 struct pollfd fds[1];
2613 memset(buf,0x7f,sizeof(mybuf)); /* set to silence */
2614 ast_tdd_gen_ecdisa(buf + 16000,16000); /* put in tone */
2616 index = zt_get_index(chan, p, 0);
2618 ast_log(LOG_WARNING, "No index in TDD?\n");
2621 fd = p->subs[index].zfd;
2623 if (ast_check_hangup(chan)) return -1;
2625 if (size > READ_SIZE)
2628 fds[0].events = POLLPRI | POLLOUT;
2629 res = poll(fds, 1, -1);
2631 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2634 /* if got exception */
2635 if (fds[0].revents & POLLPRI) return -1;
2636 if (!(fds[0].revents & POLLOUT)) {
2637 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2640 res = write(fd, buf, size);
2642 if (res == -1) return -1;
2643 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2649 p->didtdd = 1; /* set to have done it now */
2651 if (*cp == 2) { /* Mate mode */
2652 if (p->tdd) tdd_free(p->tdd);
2657 if (!p->tdd) { /* if we dont have one yet */
2658 p->tdd = tdd_new(); /* allocate one */
2661 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2666 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: OFF(0) on %s\n",chan->name);
2671 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: ON(1) on %s\n",chan->name);
2674 ast_dsp_digitmode(p->dsp,x ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF | p->dtmfrelax);
2676 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2679 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n",chan->name);
2685 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n",chan->name);
2688 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2689 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2696 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2698 /* Unlink a specific slave or all slaves/masters from a given master */
2704 ast_mutex_lock(&master->lock);
2706 while(ast_mutex_trylock(&slave->lock)) {
2707 ast_mutex_unlock(&master->lock);
2709 ast_mutex_lock(&master->lock);
2714 for (x=0;x<MAX_SLAVES;x++) {
2715 if (master->slaves[x]) {
2716 if (!slave || (master->slaves[x] == slave)) {
2717 /* Take slave out of the conference */
2718 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2719 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2720 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2721 master->slaves[x]->master = NULL;
2722 master->slaves[x] = NULL;
2727 master->inconference = 0;
2730 if (master->master) {
2731 /* Take master out of the conference */
2732 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2733 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2735 for (x=0;x<MAX_SLAVES;x++) {
2736 if (master->master->slaves[x] == master)
2737 master->master->slaves[x] = NULL;
2738 else if (master->master->slaves[x])
2742 master->master->inconference = 0;
2744 master->master = NULL;
2746 update_conf(master);
2749 ast_mutex_unlock(&slave->lock);
2750 ast_mutex_unlock(&master->lock);
2754 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2756 if (!slave || !master) {
2757 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2760 for (x=0;x<MAX_SLAVES;x++) {
2761 if (!master->slaves[x]) {
2762 master->slaves[x] = slave;
2766 if (x >= MAX_SLAVES) {
2767 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2768 master->slaves[MAX_SLAVES - 1] = slave;
2771 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2772 slave->master = master;
2774 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2777 static void disable_dtmf_detect(struct zt_pvt *p)
2783 #ifdef ZT_TONEDETECT
2785 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
2790 static void enable_dtmf_detect(struct zt_pvt *p)
2796 #ifdef ZT_TONEDETECT
2797 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
2798 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
2802 static enum ast_bridge_result zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
2804 struct ast_channel *who;
2805 struct zt_pvt *p0, *p1, *op0, *op1;
2806 struct zt_pvt *master = NULL, *slave = NULL;
2807 struct ast_frame *f;
2812 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
2813 int os0 = -1, os1 = -1;
2814 struct ast_channel *oc0, *oc1;
2815 enum ast_bridge_result res;
2818 int triedtopribridge = 0;
2819 q931_call *q931c0 = NULL, *q931c1 = NULL;
2822 /* For now, don't attempt to native bridge if either channel needs DTMF detection.
2823 There is code below to handle it properly until DTMF is actually seen,
2824 but due to currently unresolved issues it's ignored...
2827 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
2828 return AST_BRIDGE_FAILED_NOWARN;
2830 ast_mutex_lock(&c0->lock);
2831 ast_mutex_lock(&c1->lock);
2835 /* cant do pseudo-channels here */
2836 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
2837 ast_mutex_unlock(&c0->lock);
2838 ast_mutex_unlock(&c1->lock);
2839 return AST_BRIDGE_FAILED_NOWARN;
2842 oi0 = zt_get_index(c0, p0, 0);
2843 oi1 = zt_get_index(c1, p1, 0);
2844 if ((oi0 < 0) || (oi1 < 0)) {
2845 ast_mutex_unlock(&c0->lock);
2846 ast_mutex_unlock(&c1->lock);
2847 return AST_BRIDGE_FAILED;
2850 op0 = p0 = c0->tech_pvt;
2851 op1 = p1 = c1->tech_pvt;
2857 ast_mutex_lock(&p0->lock);
2858 if (ast_mutex_trylock(&p1->lock)) {
2859 /* Don't block, due to potential for deadlock */
2860 ast_mutex_unlock(&p0->lock);
2861 ast_mutex_unlock(&c0->lock);
2862 ast_mutex_unlock(&c1->lock);
2863 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
2864 return AST_BRIDGE_RETRY;
2867 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
2868 if (p0->owner && p1->owner) {
2869 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
2870 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
2874 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
2879 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
2880 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
2882 oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
2883 p0->subs[SUB_REAL].inthreeway, p0->channel,
2884 oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
2885 p1->subs[SUB_REAL].inthreeway);
2889 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
2890 if (p1->subs[SUB_THREEWAY].inthreeway) {
2895 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
2896 if (p0->subs[SUB_THREEWAY].inthreeway) {
2901 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
2902 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
2903 don't put us in anything */
2904 if (p1->subs[SUB_CALLWAIT].inthreeway) {
2909 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
2910 /* Same as previous */
2911 if (p0->subs[SUB_CALLWAIT].inthreeway) {
2917 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
2918 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
2919 if (master && slave) {
2920 /* Stop any tones, or play ringtone as appropriate. If they're bridged
2921 in an active threeway call with a channel that is ringing, we should
2922 indicate ringing. */
2923 if ((oi1 == SUB_THREEWAY) &&
2924 p1->subs[SUB_THREEWAY].inthreeway &&
2925 p1->subs[SUB_REAL].owner &&
2926 p1->subs[SUB_REAL].inthreeway &&
2927 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2928 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
2929 tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE);
2930 os1 = p1->subs[SUB_REAL].owner->_state;
2932 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
2933 tone_zone_play_tone(p0->subs[oi0].zfd, -1);
2935 if ((oi0 == SUB_THREEWAY) &&
2936 p0->subs[SUB_THREEWAY].inthreeway &&
2937 p0->subs[SUB_REAL].owner &&
2938 p0->subs[SUB_REAL].inthreeway &&
2939 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
2940 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
2941 tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE);
2942 os0 = p0->subs[SUB_REAL].owner->_state;
2944 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
2945 tone_zone_play_tone(p1->subs[oi0].zfd, -1);
2947 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
2948 if (!p0->echocanbridged || !p1->echocanbridged) {
2949 /* Disable echo cancellation if appropriate */
2954 zt_link(slave, master);
2955 master->inconference = inconf;
2956 } else if (!nothingok)
2957 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
2961 t0 = p0->subs[SUB_REAL].inthreeway;
2962 t1 = p1->subs[SUB_REAL].inthreeway;
2964 ast_mutex_unlock(&p0->lock);
2965 ast_mutex_unlock(&p1->lock);
2967 ast_mutex_unlock(&c0->lock);
2968 ast_mutex_unlock(&c1->lock);
2970 /* Native bridge failed */
2971 if ((!master || !slave) && !nothingok) {
2974 return AST_BRIDGE_FAILED;
2977 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0))
2978 disable_dtmf_detect(op0);
2980 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1))
2981 disable_dtmf_detect(op1);
2984 struct ast_channel *c0_priority[2] = {c0, c1};
2985 struct ast_channel *c1_priority[2] = {c1, c0};
2988 /* Here's our main loop... Start by locking things, looking for private parts,
2989 and then balking if anything is wrong */
2990 ast_mutex_lock(&c0->lock);
2991 ast_mutex_lock(&c1->lock);
2996 i0 = zt_get_index(c0, p0, 1);
2998 i1 = zt_get_index(c1, p1, 1);
2999 ast_mutex_unlock(&c0->lock);