2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Zaptel Pseudo TDM interface
23 * \author Mark Spencer <markster@digium.com>
25 * Connects to the zaptel telephony library as well as
26 * libpri. Libpri is optional and needed only if you are
27 * going to use ISDN connections.
29 * You need to install libraries before you attempt to compile
30 * and install the zaptel channel.
33 * \arg \ref Config_zap
35 * \ingroup channel_drivers
37 * \todo Deprecate the "musiconhold" configuration option post 1.4
41 <depend>zaptel</depend>
42 <depend>tonezone</depend>
49 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
57 #include <sys/signal.h>
61 #if !defined(SOLARIS) && !defined(__FreeBSD__)
65 #include <sys/ioctl.h>
68 #include <zaptel/zaptel.h>
69 #include <zaptel/tonezone.h>
79 #include "asterisk/lock.h"
80 #include "asterisk/channel.h"
81 #include "asterisk/config.h"
82 #include "asterisk/logger.h"
83 #include "asterisk/module.h"
84 #include "asterisk/pbx.h"
85 #include "asterisk/options.h"
86 #include "asterisk/file.h"
87 #include "asterisk/ulaw.h"
88 #include "asterisk/alaw.h"
89 #include "asterisk/callerid.h"
90 #include "asterisk/adsi.h"
91 #include "asterisk/cli.h"
92 #include "asterisk/cdr.h"
93 #include "asterisk/features.h"
94 #include "asterisk/musiconhold.h"
95 #include "asterisk/say.h"
96 #include "asterisk/tdd.h"
97 #include "asterisk/app.h"
98 #include "asterisk/dsp.h"
99 #include "asterisk/astdb.h"
100 #include "asterisk/manager.h"
101 #include "asterisk/causes.h"
102 #include "asterisk/term.h"
103 #include "asterisk/utils.h"
104 #include "asterisk/transcap.h"
105 #include "asterisk/stringfields.h"
106 #include "asterisk/abstract_jb.h"
107 #include "asterisk/smdi.h"
108 #include "asterisk/astobj.h"
109 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
111 /*! Global jitterbuffer configuration - by default, jb is disabled */
112 static struct ast_jb_conf default_jbconf =
116 .resync_threshold = -1,
119 static struct ast_jb_conf global_jbconf;
121 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_PRI) && !defined(ZT_SIG_HARDHDLC))
122 #error "Your zaptel is too old. Please update"
125 #ifndef ZT_TONEDETECT
126 /* Work around older code with no tone detect */
127 #define ZT_EVENT_DTMFDOWN 0
128 #define ZT_EVENT_DTMFUP 0
131 /* define this to send PRI user-user information elements */
132 #undef SUPPORT_USERUSER
135 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
136 * the user hangs up to reset the state machine so ring works properly.
137 * This is used to be able to support kewlstart by putting the zhone in
138 * groundstart mode since their forward disconnect supervision is entirely
139 * broken even though their documentation says it isn't and their support
140 * is entirely unwilling to provide any assistance with their channel banks
141 * even though their web site says they support their products for life.
143 /* #define ZHONE_HACK */
146 * Define if you want to check the hook state for an FXO (FXS signalled) interface
147 * before dialing on it. Certain FXO interfaces always think they're out of
148 * service with this method however.
150 /* #define ZAP_CHECK_HOOKSTATE */
152 /*! \brief Typically, how many rings before we should send Caller*ID */
153 #define DEFAULT_CIDRINGS 1
155 #define CHANNEL_PSEUDO -12
157 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
159 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
160 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FGC_CAMA) || ((p)->sig == SIG_FGC_CAMAMF) || ((p)->sig == SIG_FEATB))
162 static const char tdesc[] = "Zapata Telephony Driver"
171 static const char config[] = "zapata.conf";
173 #define SIG_EM ZT_SIG_EM
174 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
175 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
176 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
177 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
178 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
179 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
180 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
181 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
182 #define SIG_FXSLS ZT_SIG_FXSLS
183 #define SIG_FXSGS ZT_SIG_FXSGS
184 #define SIG_FXSKS ZT_SIG_FXSKS
185 #define SIG_FXOLS ZT_SIG_FXOLS
186 #define SIG_FXOGS ZT_SIG_FXOGS
187 #define SIG_FXOKS ZT_SIG_FXOKS
188 #define SIG_PRI ZT_SIG_CLEAR
189 #define SIG_SS7 (0x1000000 | ZT_SIG_CLEAR)
190 #define SIG_SF ZT_SIG_SF
191 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
192 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
193 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
194 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
195 #define SIG_EM_E1 ZT_SIG_EM_E1
196 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
197 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
200 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
201 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
203 #define CHAN_PSEUDO -2
205 #define DCHAN_PROVISIONED (1 << 0)
206 #define DCHAN_NOTINALARM (1 << 1)
207 #define DCHAN_UP (1 << 2)
209 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
211 static char context[AST_MAX_CONTEXT] = "default";
212 static char cid_num[256] = "";
213 static char cid_name[256] = "";
214 static char defaultcic[64] = "";
215 static char defaultozz[64] = "";
217 static char language[MAX_LANGUAGE] = "";
218 static char mohinterpret[MAX_MUSICCLASS] = "default";
219 static char mohsuggest[MAX_MUSICCLASS] = "";
220 static char progzone[10] = "";
222 static int usedistinctiveringdetection = 0;
223 static int distinctiveringaftercid = 0;
225 static int transfertobusy = 1;
227 static int use_callerid = 1;
228 static int cid_signalling = CID_SIG_BELL;
229 static int cid_start = CID_START_RING;
230 static int zaptrcallerid = 0;
231 static int cur_radio = 0;
232 static int cur_signalling = -1;
233 static int cur_outsignalling = -1;
235 static ast_group_t cur_group = 0;
236 static ast_group_t cur_callergroup = 0;
237 static ast_group_t cur_pickupgroup = 0;
238 static int relaxdtmf = 0;
240 static int immediate = 0;
242 static int stripmsd = 0;
244 static int callwaiting = 0;
246 static int callwaitingcallerid = 0;
248 static int hidecallerid = 0;
250 static int restrictcid = 0;
252 static int use_callingpres = 0;
254 static int callreturn = 0;
256 static int threewaycalling = 0;
258 static int transfer = 0;
260 static int canpark = 0;
262 static int cancallforward = 0;
264 static float rxgain = 0.0;
266 static float txgain = 0.0;
268 static int tonezone = -1;
270 static int echocancel;
272 static int echotraining;
276 static int echocanbridged = 0;
278 static int busydetect = 0;
280 static int busycount = 3;
281 static int busy_tonelength = 0;
282 static int busy_quietlength = 0;
284 static int callprogress = 0;
286 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
288 static char mailbox[AST_MAX_EXTENSION];
290 static int amaflags = 0;
293 static int use_smdi = 0;
294 static char smdi_port[SMDI_MAX_FILENAME_LEN] = "/dev/ttyS0";
295 static int numbufs = 4;
297 static int cur_prewink = -1;
298 static int cur_preflash = -1;
299 static int cur_wink = -1;
300 static int cur_flash = -1;
301 static int cur_start = -1;
302 static int cur_rxwink = -1;
303 static int cur_rxflash = -1;
304 static int cur_debounce = -1;
305 static int cur_priexclusive = 0;
307 static int priindication_oob = 0;
310 static int minunused = 2;
311 static int minidle = 0;
312 static char idleext[AST_MAX_EXTENSION];
313 static char idledial[AST_MAX_EXTENSION];
314 static int overlapdial = 0;
315 static int facilityenable = 0;
316 static char internationalprefix[10] = "";
317 static char nationalprefix[10] = "";
318 static char localprefix[20] = "";
319 static char privateprefix[20] = "";
320 static char unknownprefix[20] = "";
321 static long resetinterval = 3600; /*!< How often (in seconds) to reset unused channels. Default 1 hour. */
322 static struct ast_channel inuse;
323 #ifdef PRI_GETSET_TIMERS
324 static int pritimers[PRI_MAX_TIMERS];
326 static int pridebugfd = -1;
327 static char pridebugfilename[1024] = "";
330 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
331 static int firstdigittimeout = 16000;
333 /*! \brief How long to wait for following digits (FXO logic) */
334 static int gendigittimeout = 8000;
336 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
337 static int matchdigittimeout = 3000;
339 static int usecnt = 0;
341 /*! \brief Protect the interface list (of zt_pvt's) */
342 AST_MUTEX_DEFINE_STATIC(iflock);
345 static int ifcount = 0;
348 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
351 /*! \brief Whether we answer on a Polarity Switch event */
352 static int answeronpolarityswitch = 0;
354 /*! \brief Whether we hang up on a Polarity Switch event */
355 static int hanguponpolarityswitch = 0;
357 /*! \brief How long (ms) to ignore Polarity Switch events after we answer a call */
358 static int polarityonanswerdelay = 600;
360 /*! \brief When to send the CallerID signals (rings) */
361 static int sendcalleridafter = DEFAULT_CIDRINGS;
363 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
364 when it's doing something critical. */
365 AST_MUTEX_DEFINE_STATIC(monlock);
367 /*! \brief This is the thread for the monitor which checks for input on the channels
368 which are not currently in use. */
369 static pthread_t monitor_thread = AST_PTHREADT_NULL;
371 static int restart_monitor(void);
373 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, int timeoutms);
375 static int zt_sendtext(struct ast_channel *c, const char *text);
377 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
378 static inline int zt_get_event(int fd)
381 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
386 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
387 static inline int zt_wait_event(int fd)
390 i = ZT_IOMUX_SIGEVENT;
391 if (ioctl(fd, ZT_IOMUX, &i) == -1)
393 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
398 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
399 #define READ_SIZE 160
401 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
402 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
404 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
405 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
406 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
407 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
408 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
412 static int ringt_base = DEFAULT_RINGT;
416 #define LINKSTATE_INALARM (1 << 0)
417 #define LINKSTATE_STARTING (1 << 1)
418 #define LINKSTATE_UP (1 << 2)
419 #define LINKSTATE_DOWN (1 << 3)
422 pthread_t master; /*!< Thread of master */
426 int linkstate[NUM_DCHANS];
430 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
433 static struct zt_ss7 linksets[NUM_SPANS];
435 static int cur_ss7type = -1;
436 static int cur_linkset = -1;
437 static int cur_pointcode = -1;
438 static int cur_cicbeginswith = -1;
439 static int cur_adjpointcode = -1;
440 static int cur_networkindicator = -1;
441 static int cur_defaultdpc = -1;
442 #endif /* HAVE_SS7 */
446 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
447 #define PRI_CHANNEL(p) ((p) & 0xff)
448 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
449 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
452 pthread_t master; /*!< Thread of master */
453 ast_mutex_t lock; /*!< Mutex */
454 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
455 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
456 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
457 int minunused; /*!< Min # of channels to keep empty */
458 int minidle; /*!< Min # of "idling" calls to keep active */
459 int nodetype; /*!< Node type */
460 int switchtype; /*!< Type of switch to emulate */
461 int nsf; /*!< Network-Specific Facilities */
462 int dialplan; /*!< Dialing plan */
463 int localdialplan; /*!< Local dialing plan */
464 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
465 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
466 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
467 char privateprefix[20]; /*!< for private dialplans */
468 char unknownprefix[20]; /*!< for unknown dialplans */
469 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
470 int trunkgroup; /*!< What our trunkgroup is */
471 int mastertrunkgroup; /*!< What trunk group is our master */
472 int prilogicalspan; /*!< Logical span number within trunk group */
473 int numchans; /*!< Num of channels we represent */
474 int overlapdial; /*!< In overlap dialing mode */
475 int facilityenable; /*!< Enable facility IEs */
476 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
477 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
478 struct pri *pri; /*!< Currently active D-channel */
480 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
485 time_t lastreset; /*!< time when unused channels were last reset */
486 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
487 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
488 struct zt_pvt *crvs; /*!< Member CRV structs */
489 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
493 static struct zt_pri pris[NUM_SPANS];
495 static int pritype = PRI_CPE;
498 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
500 #define DEFAULT_PRI_DEBUG 0
503 static inline void pri_rel(struct zt_pri *pri)
505 ast_mutex_unlock(&pri->lock);
508 static int switchtype = PRI_SWITCH_NI2;
509 static int nsf = PRI_NSF_NONE;
510 static int dialplan = PRI_NATIONAL_ISDN + 1;
511 static int localdialplan = PRI_NATIONAL_ISDN + 1;
514 /*! Shut up the compiler */
518 #define SUB_REAL 0 /*!< Active call */
519 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
520 #define SUB_THREEWAY 2 /*!< Three-way call */
522 /* Polarity states */
523 #define POLARITY_IDLE 0
524 #define POLARITY_REV 1
527 static struct zt_distRings drings;
529 struct distRingData {
532 struct ringContextData {
533 char contextData[AST_MAX_CONTEXT];
535 struct zt_distRings {
536 struct distRingData ringnum[3];
537 struct ringContextData ringContext[3];
540 static char *subnames[] = {
546 struct zt_subchannel {
548 struct ast_channel *owner;
550 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
551 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
552 unsigned int needringing:1;
553 unsigned int needbusy:1;
554 unsigned int needcongestion:1;
555 unsigned int needcallerid:1;
556 unsigned int needanswer:1;
557 unsigned int needflash:1;
558 unsigned int needhold:1;
559 unsigned int needunhold:1;
560 unsigned int linear:1;
561 unsigned int inthreeway:1;
565 #define CONF_USER_REAL (1 << 0)
566 #define CONF_USER_THIRDCALL (1 << 1)
570 static struct zt_pvt {
572 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
573 /*!< Up to three channels can be associated with this call */
575 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
576 struct zt_subchannel subs[3]; /*!< Sub-channels */
577 struct zt_confinfo saveconf; /*!< Saved conference info */
579 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
580 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
581 int inconference; /*!< If our real should be in the conference */
583 int sig; /*!< Signalling style */
584 int radio; /*!< radio type */
585 int outsigmod; /*!< Outbound Signalling style (modifier) */
586 int oprmode; /*!< "Operator Services" mode */
587 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
590 int tonezone; /*!< tone zone for this chan, or -1 for default */
591 struct zt_pvt *next; /*!< Next channel in list */
592 struct zt_pvt *prev; /*!< Prev channel in list */
596 unsigned int answeronpolarityswitch:1;
597 unsigned int busydetect:1;
598 unsigned int callreturn:1;
599 unsigned int callwaiting:1;
600 unsigned int callwaitingcallerid:1;
601 unsigned int cancallforward:1;
602 unsigned int canpark:1;
603 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
604 unsigned int destroy:1;
605 unsigned int didtdd:1; /*!< flag to say its done it once */
606 unsigned int dialednone:1;
607 unsigned int dialing:1;
608 unsigned int digital:1;
610 unsigned int echobreak:1;
611 unsigned int echocanbridged:1;
612 unsigned int echocanon:1;
613 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
614 unsigned int firstradio:1;
615 unsigned int hanguponpolarityswitch:1;
616 unsigned int hardwaredtmf:1;
617 unsigned int hidecallerid;
618 unsigned int ignoredtmf:1;
619 unsigned int immediate:1; /*!< Answer before getting digits? */
620 unsigned int inalarm:1;
621 unsigned int mate:1; /*!< flag to say its in MATE mode */
622 unsigned int outgoing:1;
623 unsigned int overlapdial:1;
624 unsigned int permcallwaiting:1;
625 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
626 unsigned int priindication_oob:1;
627 unsigned int priexclusive:1;
628 unsigned int pulse:1;
629 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
630 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
631 unsigned int threewaycalling:1;
632 unsigned int transfer:1;
633 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
634 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
635 unsigned int usedistinctiveringdetection:1;
636 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
637 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
638 /* Channel state or unavilability flags */
639 unsigned int inservice:1;
640 unsigned int locallyblocked:1;
641 unsigned int remotelyblocked:1;
642 #if defined(HAVE_PRI)
643 unsigned int alerting:1;
644 unsigned int alreadyhungup:1;
645 unsigned int isidlecall:1;
646 unsigned int proceeding:1;
647 unsigned int progress:1;
648 unsigned int resetting:1;
649 unsigned int setup_ack:1;
651 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
652 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
654 struct zt_distRings drings;
656 char context[AST_MAX_CONTEXT];
657 char defcontext[AST_MAX_CONTEXT];
658 char exten[AST_MAX_EXTENSION];
659 char language[MAX_LANGUAGE];
660 char mohinterpret[MAX_MUSICCLASS];
661 char mohsuggest[MAX_MUSICCLASS];
663 char cid_ani[AST_MAX_EXTENSION];
665 char cid_num[AST_MAX_EXTENSION];
666 int cid_ton; /*!< Type Of Number (TON) */
667 char cid_name[AST_MAX_EXTENSION];
668 char lastcid_num[AST_MAX_EXTENSION];
669 char lastcid_name[AST_MAX_EXTENSION];
670 char *origcid_num; /*!< malloced original callerid */
671 char *origcid_name; /*!< malloced original callerid */
672 char callwait_num[AST_MAX_EXTENSION];
673 char callwait_name[AST_MAX_EXTENSION];
674 char rdnis[AST_MAX_EXTENSION];
675 char dnid[AST_MAX_EXTENSION];
678 int confno; /*!< Our conference */
679 int confusers; /*!< Who is using our conference */
680 int propconfno; /*!< Propagated conference number */
681 ast_group_t callgroup;
682 ast_group_t pickupgroup;
683 int channel; /*!< Channel Number or CRV */
684 int span; /*!< Span number */
685 time_t guardtime; /*!< Must wait this much time before using for new call */
686 int cid_signalling; /*!< CID signalling type bell202 or v23 */
687 int cid_start; /*!< CID start indicator, polarity or ring */
688 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
689 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
690 int cidcwexpire; /*!< When to expire our muting for CID/CW */
691 unsigned char *cidspill;
704 int busy_quietlength;
706 struct timeval flashtime; /*!< Last flash-hook time */
708 int cref; /*!< Call reference number */
709 ZT_DIAL_OPERATION dop;
710 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
712 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
713 int amaflags; /*!< AMA Flags */
714 struct tdd_state *tdd; /*!< TDD flag */
715 char call_forward[AST_MAX_EXTENSION];
716 char mailbox[AST_MAX_EXTENSION];
720 int distinctivering; /*!< Which distinctivering to use */
721 int cidrings; /*!< Which ring to deliver CID on */
722 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
724 int polarityonanswerdelay;
725 struct timeval polaritydelaytv;
726 int sendcalleridafter;
729 struct zt_pvt *bearer;
730 struct zt_pvt *realcall;
739 struct isup_call *ss7call;
741 int cic; /*!< CIC associated with channel */
744 } *iflist = NULL, *ifend = NULL;
746 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
747 static int zt_digit_begin(struct ast_channel *ast, char digit);
748 static int zt_digit_end(struct ast_channel *ast, char digit);
749 static int zt_sendtext(struct ast_channel *c, const char *text);
750 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
751 static int zt_hangup(struct ast_channel *ast);
752 static int zt_answer(struct ast_channel *ast);
753 static struct ast_frame *zt_read(struct ast_channel *ast);
754 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
755 static struct ast_frame *zt_exception(struct ast_channel *ast);
756 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
757 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
758 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
759 static int zt_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
761 static const struct ast_channel_tech zap_tech = {
763 .description = tdesc,
764 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
765 .requester = zt_request,
766 .send_digit_begin = zt_digit_begin,
767 .send_digit_end = zt_digit_end,
768 .send_text = zt_sendtext,
775 .exception = zt_exception,
776 .indicate = zt_indicate,
778 .setoption = zt_setoption,
779 .func_channel_read = zt_func_read,
783 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
785 #define GET_CHANNEL(p) ((p)->channel)
788 struct zt_pvt *round_robin[32];
791 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
794 /* Grab the lock first */
796 res = ast_mutex_trylock(&pri->lock);
798 ast_mutex_unlock(&pvt->lock);
799 /* Release the lock and try again */
801 ast_mutex_lock(&pvt->lock);
804 /* Then break the poll */
805 pthread_kill(pri->master, SIGURG);
811 static inline void ss7_rel(struct zt_ss7 *ss7)
813 ast_mutex_unlock(&ss7->lock);
816 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
819 /* Grab the lock first */
821 res = ast_mutex_trylock(&pri->lock);
823 ast_mutex_unlock(&pvt->lock);
824 /* Release the lock and try again */
826 ast_mutex_lock(&pvt->lock);
829 /* Then break the poll */
830 pthread_kill(pri->master, SIGURG);
834 #define NUM_CADENCE_MAX 25
835 static int num_cadence = 4;
836 static int user_has_defined_cadences = 0;
838 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
839 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
840 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
841 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
842 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
845 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
846 * is 1, the second pause is 2 and so on.
849 static int cidrings[NUM_CADENCE_MAX] = {
850 2, /*!< Right after first long ring */
851 4, /*!< Right after long part */
852 3, /*!< After third chirp */
853 2, /*!< Second spell */
856 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
857 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
859 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
860 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
862 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
865 if (p->subs[0].owner == ast)
867 else if (p->subs[1].owner == ast)
869 else if (p->subs[2].owner == ast)
874 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
880 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
882 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
887 ast_mutex_unlock(&pri->lock);
890 if (p->subs[a].owner) {
891 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
892 ast_mutex_unlock(&p->lock);
894 ast_mutex_lock(&p->lock);
896 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
897 ast_mutex_unlock(&p->subs[a].owner->lock);
905 ast_mutex_lock(&pri->lock);
909 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
912 struct zt_pri *pri = (struct zt_pri*) data;
915 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
917 /* We must unlock the PRI to avoid the possibility of a deadlock */
918 #if defined(HAVE_PRI) || defined(HAVE_SS7)
923 ast_mutex_unlock(&pri->lock);
928 ast_mutex_unlock(&ss7->lock);
938 if (ast_mutex_trylock(&p->owner->lock)) {
939 ast_mutex_unlock(&p->lock);
941 ast_mutex_lock(&p->lock);
943 ast_queue_frame(p->owner, f);
944 ast_mutex_unlock(&p->owner->lock);
950 #if defined(HAVE_PRI) || defined(HAVE_SS7)
955 ast_mutex_lock(&pri->lock);
960 ast_mutex_lock(&ss7->lock);
971 static int restore_gains(struct zt_pvt *p);
973 static void swap_subs(struct zt_pvt *p, int a, int b)
977 struct ast_channel *towner;
980 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
982 tchan = p->subs[a].chan;
983 towner = p->subs[a].owner;
984 tinthreeway = p->subs[a].inthreeway;
986 p->subs[a].chan = p->subs[b].chan;
987 p->subs[a].owner = p->subs[b].owner;
988 p->subs[a].inthreeway = p->subs[b].inthreeway;
990 p->subs[b].chan = tchan;
991 p->subs[b].owner = towner;
992 p->subs[b].inthreeway = tinthreeway;
994 if (p->subs[a].owner)
995 p->subs[a].owner->fds[0] = p->subs[a].zfd;
996 if (p->subs[b].owner)
997 p->subs[b].owner->fds[0] = p->subs[b].zfd;
998 wakeup_sub(p, a, NULL);
999 wakeup_sub(p, b, NULL);
1002 static int zt_open(char *fn)
1010 for (x = 0; x < strlen(fn); x++) {
1011 if (!isdigit(fn[x])) {
1019 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1022 fn = "/dev/zap/channel";
1024 fd = open(fn, O_RDWR | O_NONBLOCK);
1026 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1030 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1034 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1039 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
1043 static void zt_close(int fd)
1049 static int zt_setlinear(int zfd, int linear)
1052 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1059 static int alloc_sub(struct zt_pvt *p, int x)
1063 if (p->subs[x].zfd < 0) {
1064 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1065 if (p->subs[x].zfd > -1) {
1066 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1068 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1069 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1070 bi.numbufs = numbufs;
1071 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1073 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1076 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1077 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1078 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1079 zt_close(p->subs[x].zfd);
1080 p->subs[x].zfd = -1;
1084 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1087 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1090 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1094 static int unalloc_sub(struct zt_pvt *p, int x)
1097 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1101 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
1102 if (p->subs[x].zfd > -1) {
1103 zt_close(p->subs[x].zfd);
1105 p->subs[x].zfd = -1;
1106 p->subs[x].linear = 0;
1107 p->subs[x].chan = 0;
1108 p->subs[x].owner = NULL;
1109 p->subs[x].inthreeway = 0;
1110 p->polarity = POLARITY_IDLE;
1111 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1115 static int digit_to_dtmfindex(char digit)
1118 return ZT_TONE_DTMF_BASE + (digit - '0');
1119 else if (digit >= 'A' && digit <= 'D')
1120 return ZT_TONE_DTMF_A + (digit - 'A');
1121 else if (digit >= 'a' && digit <= 'd')
1122 return ZT_TONE_DTMF_A + (digit - 'a');
1123 else if (digit == '*')
1124 return ZT_TONE_DTMF_s;
1125 else if (digit == '#')
1126 return ZT_TONE_DTMF_p;
1131 static int zt_digit_begin(struct ast_channel *chan, char digit)
1137 pvt = chan->tech_pvt;
1139 ast_mutex_lock(&pvt->lock);
1141 index = zt_get_index(chan, pvt, 0);
1143 if ((index != SUB_REAL) || !pvt->owner)
1147 if ((pvt->sig == SIG_PRI) && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1148 if (pvt->setup_ack) {
1149 if (!pri_grab(pvt, pvt->pri)) {
1150 pri_information(pvt->pri->pri, pvt->call, digit);
1153 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1154 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1157 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1158 res = strlen(pvt->dialdest);
1159 pvt->dialdest[res++] = digit;
1160 pvt->dialdest[res] = '\0';
1165 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1168 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1170 ZT_DIAL_OPERATION zo = {
1171 .op = ZT_DIAL_OP_APPEND,
1173 .dialstr[1] = digit,
1176 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1177 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1182 ast_log(LOG_DEBUG, "Started VLDTMF digit '%c'\n", digit);
1184 pvt->begindigit = digit;
1188 ast_mutex_unlock(&pvt->lock);
1190 return 0; /* Tell Asterisk not to generate inband indications */
1193 static int zt_digit_end(struct ast_channel *chan, char digit)
1200 pvt = chan->tech_pvt;
1202 ast_mutex_lock(&pvt->lock);
1204 index = zt_get_index(chan, pvt, 0);
1206 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1210 /* This means that the digit was already sent via PRI signalling */
1211 if (pvt->sig == SIG_PRI && !pvt->begindigit)
1215 if (pvt->begindigit) {
1218 ast_log(LOG_DEBUG, "Ending VLDTMF digit '%c'\n", digit);
1219 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1221 pvt->begindigit = 0;
1225 ast_mutex_unlock(&pvt->lock);
1230 static char *events[] = {
1243 "Hook Transition Complete",
1248 "Polarity Reversal",
1256 { ZT_ALARM_RED, "Red Alarm" },
1257 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1258 { ZT_ALARM_BLUE, "Blue Alarm" },
1259 { ZT_ALARM_RECOVER, "Recovering" },
1260 { ZT_ALARM_LOOPBACK, "Loopback" },
1261 { ZT_ALARM_NOTOPEN, "Not Open" },
1262 { ZT_ALARM_NONE, "None" },
1265 static char *alarm2str(int alarm)
1268 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1269 if (alarms[x].alarm & alarm)
1270 return alarms[x].name;
1272 return alarm ? "Unknown Alarm" : "No Alarm";
1275 static char *event2str(int event)
1277 static char buf[256];
1278 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1279 return events[event];
1280 sprintf(buf, "Event %d", event); /* safe */
1285 static char *dialplan2str(int dialplan)
1287 if (dialplan == -1) {
1288 return("Dynamically set dialplan in ISDN");
1290 return (pri_plan2str(dialplan));
1294 static char *zap_sig2str(int sig)
1296 static char buf[256];
1299 return "E & M Immediate";
1301 return "E & M Wink";
1305 return "Feature Group D (DTMF)";
1307 return "Feature Group D (MF)";
1308 case SIG_FEATDMF_TA:
1309 return "Feature Groud D (MF) Tandem Access";
1311 return "Feature Group B (MF)";
1315 return "FGC/CAMA (Dialpulse)";
1316 case SIG_FGC_CAMAMF:
1317 return "FGC/CAMA (MF)";
1319 return "FXS Loopstart";
1321 return "FXS Groundstart";
1323 return "FXS Kewlstart";
1325 return "FXO Loopstart";
1327 return "FXO Groundstart";
1329 return "FXO Kewlstart";
1331 return "PRI Signalling";
1333 return "SS7 Signalling";
1335 return "SF (Tone) Signalling Immediate";
1337 return "SF (Tone) Signalling Wink";
1339 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1340 case SIG_SF_FEATDMF:
1341 return "SF (Tone) Signalling with Feature Group D (MF)";
1343 return "SF (Tone) Signalling with Feature Group B (MF)";
1344 case SIG_GR303FXOKS:
1345 return "GR-303 Signalling with FXOKS";
1346 case SIG_GR303FXSKS:
1347 return "GR-303 Signalling with FXSKS";
1349 return "Pseudo Signalling";
1351 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1356 #define sig2str zap_sig2str
1358 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1360 /* If the conference already exists, and we're already in it
1361 don't bother doing anything */
1364 memset(&zi, 0, sizeof(zi));
1367 if (slavechannel > 0) {
1368 /* If we have only one slave, do a digital mon */
1369 zi.confmode = ZT_CONF_DIGITALMON;
1370 zi.confno = slavechannel;
1373 /* Real-side and pseudo-side both participate in conference */
1374 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1375 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1377 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1378 zi.confno = p->confno;
1380 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1384 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1385 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1388 if (slavechannel < 1) {
1389 p->confno = zi.confno;
1391 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1393 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1397 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1399 /* If they're listening to our channel, they're ours */
1400 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1402 /* If they're a talker on our (allocated) conference, they're ours */
1403 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1408 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1411 if (/* Can't delete if there's no zfd */
1413 /* Don't delete from the conference if it's not our conference */
1415 /* Don't delete if we don't think it's conferenced at all (implied) */
1417 memset(&zi, 0, sizeof(zi));
1421 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1422 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1426 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1427 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1431 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1435 struct zt_pvt *slave = NULL;
1436 /* Start out optimistic */
1438 /* Update conference state in a stateless fashion */
1439 for (x = 0; x < 3; x++) {
1440 /* Any three-way calling makes slave native mode *definitely* out
1442 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1445 /* If we don't have any 3-way calls, check to see if we have
1446 precisely one slave */
1447 if (useslavenative) {
1448 for (x = 0; x < MAX_SLAVES; x++) {
1451 /* Whoops already have a slave! No
1452 slave native and stop right away */
1457 /* We have one slave so far */
1458 slave = p->slaves[x];
1463 /* If no slave, slave native definitely out */
1466 else if (slave->law != p->law) {
1472 return useslavenative;
1475 static int reset_conf(struct zt_pvt *p)
1478 memset(&zi, 0, sizeof(zi));
1480 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1481 if (p->subs[SUB_REAL].zfd > -1) {
1482 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1483 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1488 static int update_conf(struct zt_pvt *p)
1493 struct zt_pvt *slave = NULL;
1495 useslavenative = isslavenative(p, &slave);
1496 /* Start with the obvious, general stuff */
1497 for (x = 0; x < 3; x++) {
1498 /* Look for three way calls */
1499 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1500 conf_add(p, &p->subs[x], x, 0);
1503 conf_del(p, &p->subs[x], x);
1506 /* If we have a slave, add him to our conference now. or DAX
1507 if this is slave native */
1508 for (x = 0; x < MAX_SLAVES; x++) {
1511 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1513 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1518 /* If we're supposed to be in there, do so now */
1519 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1521 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1523 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1527 /* If we have a master, add ourselves to his conference */
1529 if (isslavenative(p->master, NULL)) {
1530 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1532 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1536 /* Nobody is left (or should be left) in our conference.
1541 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1545 static void zt_enable_ec(struct zt_pvt *p)
1553 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1558 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1561 if (p->echocancel) {
1562 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1564 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1566 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1569 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1571 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1575 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1579 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1583 static void zt_train_ec(struct zt_pvt *p)
1587 if (p && p->echocancel && p->echotraining) {
1588 x = p->echotraining;
1589 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1591 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1594 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1598 ast_log(LOG_DEBUG, "No echo training requested\n");
1602 static void zt_disable_ec(struct zt_pvt *p)
1606 if (p->echocancel) {
1608 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1610 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1613 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1619 static void fill_txgain(struct zt_gains *g, float gain, int law)
1623 float linear_gain = pow(10.0, gain / 20.0);
1627 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1629 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1630 if (k > 32767) k = 32767;
1631 if (k < -32767) k = -32767;
1632 g->txgain[j] = AST_LIN2A(k);
1639 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1641 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1642 if (k > 32767) k = 32767;
1643 if (k < -32767) k = -32767;
1644 g->txgain[j] = AST_LIN2MU(k);
1653 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1657 float linear_gain = pow(10.0, gain / 20.0);
1661 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1663 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1664 if (k > 32767) k = 32767;
1665 if (k < -32767) k = -32767;
1666 g->rxgain[j] = AST_LIN2A(k);
1673 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1675 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1676 if (k > 32767) k = 32767;
1677 if (k < -32767) k = -32767;
1678 g->rxgain[j] = AST_LIN2MU(k);
1687 static int set_actual_txgain(int fd, int chan, float gain, int law)
1692 memset(&g, 0, sizeof(g));
1694 res = ioctl(fd, ZT_GETGAINS, &g);
1697 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1701 fill_txgain(&g, gain, law);
1703 return ioctl(fd, ZT_SETGAINS, &g);
1706 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1711 memset(&g, 0, sizeof(g));
1713 res = ioctl(fd, ZT_GETGAINS, &g);
1716 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1720 fill_rxgain(&g, gain, law);
1722 return ioctl(fd, ZT_SETGAINS, &g);
1725 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1727 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1730 static int bump_gains(struct zt_pvt *p)
1734 /* Bump receive gain by 5.0db */
1735 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
1737 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1744 static int restore_gains(struct zt_pvt *p)
1748 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1750 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1757 static inline int zt_set_hook(int fd, int hs)
1761 res = ioctl(fd, ZT_HOOK, &x);
1764 if (errno == EINPROGRESS) return 0;
1765 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1770 static inline int zt_confmute(struct zt_pvt *p, int muted)
1774 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1776 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1778 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1780 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1782 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1786 static int save_conference(struct zt_pvt *p)
1788 struct zt_confinfo c;
1790 if (p->saveconf.confmode) {
1791 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1794 p->saveconf.chan = 0;
1795 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1797 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1798 p->saveconf.confmode = 0;
1803 c.confmode = ZT_CONF_NORMAL;
1804 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1806 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1810 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1814 static int restore_conference(struct zt_pvt *p)
1817 if (p->saveconf.confmode) {
1818 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1819 p->saveconf.confmode = 0;
1821 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1826 ast_log(LOG_DEBUG, "Restored conferencing\n");
1830 static int send_callerid(struct zt_pvt *p);
1832 static int send_cwcidspill(struct zt_pvt *p)
1836 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1838 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1839 /* Make sure we account for the end */
1840 p->cidlen += READ_SIZE * 4;
1843 if (option_verbose > 2)
1844 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1848 static int has_voicemail(struct zt_pvt *p)
1851 return ast_app_has_voicemail(p->mailbox, NULL);
1854 static int send_callerid(struct zt_pvt *p)
1856 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1858 /* Take out of linear mode if necessary */
1859 if (p->subs[SUB_REAL].linear) {
1860 p->subs[SUB_REAL].linear = 0;
1861 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1863 while (p->cidpos < p->cidlen) {
1864 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1866 if (errno == EAGAIN)
1869 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1879 if (p->callwaitcas) {
1880 /* Wait for CID/CW to expire */
1881 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1883 restore_conference(p);
1887 static int zt_callwait(struct ast_channel *ast)
1889 struct zt_pvt *p = ast->tech_pvt;
1890 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1892 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1895 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1899 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1900 if (!p->callwaitrings && p->callwaitingcallerid) {
1901 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1903 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1905 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1907 p->cidlen = 2400 + READ_SIZE * 4;
1915 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1917 struct zt_pvt *p = ast->tech_pvt;
1918 int x, res, index,mysig;
1923 char dest[256]; /* must be same length as p->dialdest */
1924 ast_mutex_lock(&p->lock);
1925 ast_copy_string(dest, rdest, sizeof(dest));
1926 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1927 if ((ast->_state == AST_STATE_BUSY)) {
1928 p->subs[SUB_REAL].needbusy = 1;
1929 ast_mutex_unlock(&p->lock);
1932 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1933 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1934 ast_mutex_unlock(&p->lock);
1938 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
1940 /* Special pseudo -- automatically up */
1941 ast_setstate(ast, AST_STATE_UP);
1942 ast_mutex_unlock(&p->lock);
1945 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1946 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1948 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1951 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1954 if (p->outsigmod > -1)
1955 mysig = p->outsigmod;
1961 if (p->owner == ast) {
1962 /* Normal ring, on hook */
1964 /* Don't send audio while on hook, until the call is answered */
1966 if (p->use_callerid) {
1967 /* Generate the Caller-ID spill if desired */
1969 ast_log(LOG_WARNING, "cidspill already exists??\n");
1973 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1974 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1979 /* Choose proper cadence */
1980 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1981 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
1982 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1983 p->cidrings = cidrings[p->distinctivering - 1];
1985 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1986 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1987 p->cidrings = p->sendcalleridafter;
1990 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1991 c = strchr(dest, '/');
1994 if (c && (strlen(c) < p->stripmsd)) {
1995 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1999 p->dop.op = ZT_DIAL_OP_REPLACE;
2000 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2002 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
2004 p->dop.dialstr[0] = '\0';
2007 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2008 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2009 ast_mutex_unlock(&p->lock);
2014 /* Call waiting call */
2015 p->callwaitrings = 0;
2016 if (ast->cid.cid_num)
2017 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2019 p->callwait_num[0] = '\0';
2020 if (ast->cid.cid_name)
2021 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2023 p->callwait_name[0] = '\0';
2024 /* Call waiting tone instead */
2025 if (zt_callwait(ast)) {
2026 ast_mutex_unlock(&p->lock);
2029 /* Make ring-back */
2030 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2031 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2034 n = ast->cid.cid_name;
2035 l = ast->cid.cid_num;
2037 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2039 p->lastcid_num[0] = '\0';
2041 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2043 p->lastcid_name[0] = '\0';
2044 ast_setstate(ast, AST_STATE_RINGING);
2045 index = zt_get_index(ast, p, 0);
2047 p->subs[index].needringing = 1;
2060 case SIG_FGC_CAMAMF:
2065 case SIG_SF_FEATDMF:
2066 case SIG_FEATDMF_TA:
2068 c = strchr(dest, '/');
2073 if (strlen(c) < p->stripmsd) {
2074 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2075 ast_mutex_unlock(&p->lock);
2079 /* Start the trunk, if not GR-303 */
2083 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2085 if (errno != EINPROGRESS) {
2086 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2087 ast_mutex_unlock(&p->lock);
2095 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
2096 p->dop.op = ZT_DIAL_OP_REPLACE;
2102 l = ast->cid.cid_num;
2104 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2106 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2109 l = ast->cid.cid_num;
2111 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2113 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2115 case SIG_FEATDMF_TA:
2117 const char *cic, *ozz;
2119 /* If you have to go through a Tandem Access point you need to use this */
2120 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2123 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2127 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2128 ast_mutex_unlock(&p->lock);
2131 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2132 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2137 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2140 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2142 case SIG_FGC_CAMAMF:
2144 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2148 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2150 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2154 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2155 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2156 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2157 p->echorest[sizeof(p->echorest) - 1] = '\0';
2159 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2163 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2165 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2166 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2167 ast_mutex_unlock(&p->lock);
2172 ast_log(LOG_DEBUG, "Deferring dialing...\n");
2175 if (ast_strlen_zero(c))
2177 ast_setstate(ast, AST_STATE_DIALING);
2180 /* Special pseudo -- automatically up*/
2181 ast_setstate(ast, AST_STATE_UP);
2185 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2186 p->dialdest[0] = '\0';
2190 ast_log(LOG_DEBUG, "not yet implemented\n");
2191 ast_mutex_unlock(&p->lock);
2196 c = strchr(dest, '/');
2202 if (!p->hidecallerid) {
2203 l = ast->cid.cid_num;
2208 ss7_grab(p, p->ss7);
2209 p->digital = IS_DIGITAL(ast->transfercapability);
2210 p->ss7call = isup_new_call(p->ss7->ss7);
2214 ast_mutex_unlock(&p->lock);
2215 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2219 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, c + p->stripmsd, l);
2221 isup_iam(p->ss7->ss7, p->ss7call);
2224 #endif /* HAVE_SS7 */
2228 #ifdef SUPPORT_USERUSER
2229 const char *useruser;
2233 int prilocaldialplan;
2237 int redirect_reason;
2239 c = strchr(dest, '/');
2244 if (!p->hidecallerid) {
2245 l = ast->cid.cid_num;
2246 n = ast->cid.cid_name;
2251 if (strlen(c) < p->stripmsd) {
2252 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2253 ast_mutex_unlock(&p->lock);
2256 if (mysig != SIG_FXSKS) {
2257 p->dop.op = ZT_DIAL_OP_REPLACE;
2258 s = strchr(c + p->stripmsd, 'w');
2261 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2263 p->dop.dialstr[0] = '\0';
2266 p->dop.dialstr[0] = '\0';
2269 if (pri_grab(p, p->pri)) {
2270 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2271 ast_mutex_unlock(&p->lock);
2274 if (!(p->call = pri_new_call(p->pri->pri))) {
2275 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2277 ast_mutex_unlock(&p->lock);
2280 if (!(sr = pri_sr_new())) {
2281 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2283 ast_mutex_unlock(&p->lock);
2285 if (p->bearer || (mysig == SIG_FXSKS)) {
2288 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);
2289 p->bearer->call = p->call;
2292 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
2294 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2296 p->digital = IS_DIGITAL(ast->transfercapability);
2297 /* Add support for exclusive override */
2298 if (p->priexclusive)
2301 /* otherwise, traditional behavior */
2302 if (p->pri->nodetype == PRI_NETWORK)
2308 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2309 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2311 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2312 if (p->pri->facilityenable)
2313 pri_facility_enable(p->pri->pri);
2315 if (option_verbose > 2)
2316 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2318 pridialplan = p->pri->dialplan - 1;
2319 if (pridialplan == -2) { /* compute dynamically */
2320 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2321 dp_strip = strlen(p->pri->internationalprefix);
2322 pridialplan = PRI_INTERNATIONAL_ISDN;
2323 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2324 dp_strip = strlen(p->pri->nationalprefix);
2325 pridialplan = PRI_NATIONAL_ISDN;
2327 pridialplan = PRI_LOCAL_ISDN;
2330 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2333 prilocaldialplan = p->pri->localdialplan - 1;
2334 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
2335 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2336 ldp_strip = strlen(p->pri->internationalprefix);
2337 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2338 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2339 ldp_strip = strlen(p->pri->nationalprefix);
2340 prilocaldialplan = PRI_NATIONAL_ISDN;
2342 prilocaldialplan = PRI_LOCAL_ISDN;
2345 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2346 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2347 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2348 if (!strcasecmp(rr_str, "UNKNOWN"))
2349 redirect_reason = 0;
2350 else if (!strcasecmp(rr_str, "BUSY"))
2351 redirect_reason = 1;
2352 else if (!strcasecmp(rr_str, "NO_REPLY"))
2353 redirect_reason = 2;
2354 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2355 redirect_reason = 15;
2357 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2359 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2360 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2362 #ifdef SUPPORT_USERUSER
2363 /* User-user info */
2364 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2367 pri_sr_set_useruser(sr, useruser);
2370 if (pri_setup(p->pri->pri, p->call, sr)) {
2371 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2372 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2374 ast_mutex_unlock(&p->lock);
2379 ast_setstate(ast, AST_STATE_DIALING);
2383 ast_mutex_unlock(&p->lock);
2387 static void destroy_zt_pvt(struct zt_pvt **pvt)
2389 struct zt_pvt *p = *pvt;
2390 /* Remove channel from the list */
2392 p->prev->next = p->next;
2394 p->next->prev = p->prev;
2396 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2397 ast_mutex_destroy(&p->lock);
2402 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2412 for (i = 0; i < 3; i++) {
2413 if (cur->subs[i].owner) {
2419 prev->next = cur->next;
2421 prev->next->prev = prev;
2427 iflist->prev = NULL;
2431 if (cur->subs[SUB_REAL].zfd > -1) {
2432 zt_close(cur->subs[SUB_REAL].zfd);
2434 destroy_zt_pvt(&cur);
2438 prev->next = cur->next;
2440 prev->next->prev = prev;
2446 iflist->prev = NULL;
2450 if (cur->subs[SUB_REAL].zfd > -1) {
2451 zt_close(cur->subs[SUB_REAL].zfd);
2453 destroy_zt_pvt(&cur);
2459 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2461 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2463 static char *zap_send_keypad_facility_descrip =
2464 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2465 " IE over the current channel.\n";
2467 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2469 /* Data will be our digit string */
2471 char *digits = (char *) data;
2473 if (ast_strlen_zero(digits)) {
2475 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
2479 p = (struct zt_pvt *)chan->tech_pvt;
2483 ast_log(LOG_DEBUG, "Unable to find technology private\n");
2487 ast_mutex_lock(&p->lock);
2489 if (!p->pri || !p->call) {
2491 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2492 ast_mutex_unlock(&p->lock);
2496 if (!pri_grab(p, p->pri)) {
2497 pri_keypad_facility(p->pri->pri, p->call, digits);
2501 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
2502 ast_mutex_unlock(&p->lock);
2506 ast_mutex_unlock(&p->lock);
2511 static int pri_is_up(struct zt_pri *pri)
2514 for (x = 0; x < NUM_DCHANS; x++) {
2515 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2521 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2523 bearer->owner = &inuse;
2524 bearer->realcall = crv;
2525 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2526 if (crv->subs[SUB_REAL].owner)
2527 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2528 crv->bearer = bearer;
2529 crv->call = bearer->call;
2534 static char *pri_order(int level)
2544 return "Quaternary";
2550 /* Returns fd of the active dchan */
2551 static int pri_active_dchan_fd(struct zt_pri *pri)
2555 for (x = 0; x < NUM_DCHANS; x++) {
2556 if ((pri->dchans[x] == pri->pri))
2563 static int pri_find_dchan(struct zt_pri *pri)
2570 for (x = 0; x < NUM_DCHANS; x++) {
2571 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2573 if (pri->dchans[x] == old) {
2579 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2580 pri->dchannels[newslot]);
2582 if (old && (oldslot != newslot))
2583 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2584 pri->dchannels[oldslot], pri->dchannels[newslot]);
2585 pri->pri = pri->dchans[newslot];
2590 static int zt_hangup(struct ast_channel *ast)
2594 /*static int restore_gains(struct zt_pvt *p);*/
2595 struct zt_pvt *p = ast->tech_pvt;
2596 struct zt_pvt *tmp = NULL;
2597 struct zt_pvt *prev = NULL;
2601 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2602 if (!ast->tech_pvt) {
2603 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2607 ast_mutex_lock(&p->lock);
2609 index = zt_get_index(ast, p, 1);
2611 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2613 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2619 if (p->origcid_num) {
2620 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2621 free(p->origcid_num);
2622 p->origcid_num = NULL;
2624 if (p->origcid_name) {
2625 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2626 free(p->origcid_name);
2627 p->origcid_name = NULL;
2630 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2635 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2636 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2640 /* Real channel, do some fixup */
2641 p->subs[index].owner = NULL;
2642 p->subs[index].needanswer = 0;
2643 p->subs[index].needflash = 0;
2644 p->subs[index].needringing = 0;
2645 p->subs[index].needbusy = 0;
2646 p->subs[index].needcongestion = 0;
2647 p->subs[index].linear = 0;
2648 p->subs[index].needcallerid = 0;
2649 p->polarity = POLARITY_IDLE;
2650 zt_setlinear(p->subs[index].zfd, 0);
2651 if (index == SUB_REAL) {
2652 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2654 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2655 if (p->subs[SUB_CALLWAIT].inthreeway) {
2656 /* We had flipped over to answer a callwait and now it's gone */
2658 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2659 /* Move to the call-wait, but un-own us until they flip back. */
2660 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2661 unalloc_sub(p, SUB_CALLWAIT);
2664 /* The three way hung up, but we still have a call wait */
2666 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2667 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2668 unalloc_sub(p, SUB_THREEWAY);
2669 if (p->subs[SUB_REAL].inthreeway) {
2670 /* This was part of a three way call. Immediately make way for
2673 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2674 p->owner = p->subs[SUB_REAL].owner;
2676 /* This call hasn't been completed yet... Set owner to NULL */
2678 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2681 p->subs[SUB_REAL].inthreeway = 0;
2683 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2684 /* Move to the call-wait and switch back to them. */
2685 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2686 unalloc_sub(p, SUB_CALLWAIT);
2687 p->owner = p->subs[SUB_REAL].owner;
2688 if (p->owner->_state != AST_STATE_UP)
2689 p->subs[SUB_REAL].needanswer = 1;
2690 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2691 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
2692 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2693 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2694 unalloc_sub(p, SUB_THREEWAY);
2695 if (p->subs[SUB_REAL].inthreeway) {
2696 /* This was part of a three way call. Immediately make way for
2699 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2700 p->owner = p->subs[SUB_REAL].owner;
2702 /* This call hasn't been completed yet... Set owner to NULL */
2704 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2707 p->subs[SUB_REAL].inthreeway = 0;
2709 } else if (index == SUB_CALLWAIT) {
2710 /* Ditch the holding callwait call, and immediately make it availabe */
2711 if (p->subs[SUB_CALLWAIT].inthreeway) {
2712 /* This is actually part of a three way, placed on hold. Place the third part
2713 on music on hold now */
2714 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2715 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2716 S_OR(p->mohsuggest, NULL),
2717 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2719 p->subs[SUB_THREEWAY].inthreeway = 0;
2720 /* Make it the call wait now */
2721 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2722 unalloc_sub(p, SUB_THREEWAY);
2724 unalloc_sub(p, SUB_CALLWAIT);
2725 } else if (index == SUB_THREEWAY) {
2726 if (p->subs[SUB_CALLWAIT].inthreeway) {
2727 /* The other party of the three way call is currently in a call-wait state.
2728 Start music on hold for them, and take the main guy out of the third call */
2729 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
2730 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
2731 S_OR(p->mohsuggest, NULL),
2732 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2734 p->subs[SUB_CALLWAIT].inthreeway = 0;
2736 p->subs[SUB_REAL].inthreeway = 0;
2737 /* If this was part of a three way call index, let us make
2738 another three way call */
2739 unalloc_sub(p, SUB_THREEWAY);
2741 /* This wasn't any sort of call, but how are we an index? */
2742 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2746 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2749 p->distinctivering = 0;
2750 p->confirmanswer = 0;
2756 p->onhooktime = time(NULL);
2764 ast_dsp_free(p->dsp);
2768 law = ZT_LAW_DEFAULT;
2769 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2771 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2772 /* Perform low level hangup if no owner left */
2776 if (!ss7_grab(p, p->ss7)) {
2777 if (!p->alreadyhungup) {
2778 isup_rel(p->ss7->ss7, p->ss7call, ast->hangupcause ? ast->hangupcause : -1);
2780 p->alreadyhungup = 1;
2782 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
2784 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
2792 #ifdef SUPPORT_USERUSER
2793 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2796 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2797 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2798 if (!pri_grab(p, p->pri)) {
2799 if (p->alreadyhungup) {
2801 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2803 #ifdef SUPPORT_USERUSER
2804 pri_call_set_useruser(p->call, useruser);
2807 pri_hangup(p->pri->pri, p->call, -1);
2810 p->bearer->call = NULL;
2812 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2813 int icause = ast->hangupcause ? ast->hangupcause : -1;
2815 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2817 #ifdef SUPPORT_USERUSER
2818 pri_call_set_useruser(p->call, useruser);
2821 p->alreadyhungup = 1;
2823 p->bearer->alreadyhungup = 1;
2826 icause = atoi(cause);
2828 pri_hangup(p->pri->pri, p->call, icause);
2831 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2834 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2840 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2846 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7)))
2847 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2849 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2855 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2859 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2861 /* If they're off hook, try playing congestion */
2862 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
2863 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2865 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2871 /* Make sure we're not made available for at least two seconds assuming
2872 we were actually used for an inbound or outbound call. */
2873 if (ast->_state != AST_STATE_RESERVED) {
2874 time(&p->guardtime);
2879 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2886 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2887 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2891 p->callwaiting = p->permcallwaiting;
2892 p->hidecallerid = p->permhidecallerid;
2897 /* Restore data mode */
2898 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2900 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2905 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2906 /* Free up the bearer channel as well, and
2907 don't use its file descriptor anymore */
2908 update_conf(p->bearer);
2909 reset_conf(p->bearer);
2910 p->bearer->owner = NULL;
2911 p->bearer->realcall = NULL;
2913 p->subs[SUB_REAL].zfd = -1;
2920 p->callwaitingrepeat = 0;
2923 ast->tech_pvt = NULL;
2924 ast_mutex_unlock(&p->lock);
2925 ast_atomic_fetchadd_int(&usecnt, -1);
2926 ast_update_use_count();
2927 if (option_verbose > 2)
2928 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2930 ast_mutex_lock(&iflock);
2936 destroy_channel(prev, tmp, 0);
2944 ast_mutex_unlock(&iflock);
2948 static int zt_answer(struct ast_channel *ast)
2950 struct zt_pvt *p = ast->tech_pvt;
2953 int oldstate = ast->_state;
2954 ast_setstate(ast, AST_STATE_UP);
2955 ast_mutex_lock(&p->lock);
2956 index = zt_get_index(ast, p, 0);
2959 /* nothing to do if a radio channel */
2960 if ((p->radio || (p->oprmode < 0))) {
2961 ast_mutex_unlock(&p->lock);
2975 case SIG_FEATDMF_TA:
2978 case SIG_FGC_CAMAMF:
2983 case SIG_SF_FEATDMF:
2988 /* Pick up the line */
2990 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2991 if (p->hanguponpolarityswitch) {
2992 gettimeofday(&p->polaritydelaytv, NULL);
2994 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2995 tone_zone_play_tone(p->subs[index].zfd, -1);
2997 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2998 if (oldstate == AST_STATE_RINGING) {
3000 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
3001 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3002 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3003 p->owner = p->subs[SUB_REAL].owner;
3006 if (p->sig & __ZT_SIG_FXS) {
3013 /* Send a pri acknowledge */
3014 if (!pri_grab(p, p->pri)) {
3016 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3019 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3026 if (!ss7_grab(p, p->ss7)) {
3028 res = isup_anm(p->ss7->ss7, p->ss7call);
3031 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3037 ast_mutex_unlock(&p->lock);
3040 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3043 ast_mutex_unlock(&p->lock);
3047 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3053 struct zt_pvt *p = chan->tech_pvt, *pp;
3054 struct oprmode *oprmode;