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 hidecalleridname = 0;
252 static int restrictcid = 0;
254 static int use_callingpres = 0;
256 static int callreturn = 0;
258 static int threewaycalling = 0;
260 static int transfer = 0;
262 static int canpark = 0;
264 static int cancallforward = 0;
266 static float rxgain = 0.0;
268 static float txgain = 0.0;
270 static int tonezone = -1;
272 static int echocancel;
274 static int echotraining;
278 static int echocanbridged = 0;
280 static int busydetect = 0;
282 static int busycount = 3;
283 static int busy_tonelength = 0;
284 static int busy_quietlength = 0;
286 static int callprogress = 0;
288 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
290 static char mailbox[AST_MAX_EXTENSION];
292 static int amaflags = 0;
295 static int use_smdi = 0;
296 static char smdi_port[SMDI_MAX_FILENAME_LEN] = "/dev/ttyS0";
297 static int numbufs = 4;
299 static int cur_prewink = -1;
300 static int cur_preflash = -1;
301 static int cur_wink = -1;
302 static int cur_flash = -1;
303 static int cur_start = -1;
304 static int cur_rxwink = -1;
305 static int cur_rxflash = -1;
306 static int cur_debounce = -1;
307 static int cur_priexclusive = 0;
309 static int priindication_oob = 0;
312 static int minunused = 2;
313 static int minidle = 0;
314 static char idleext[AST_MAX_EXTENSION];
315 static char idledial[AST_MAX_EXTENSION];
316 static int overlapdial = 0;
317 static int facilityenable = 0;
318 static char internationalprefix[10] = "";
319 static char nationalprefix[10] = "";
320 static char localprefix[20] = "";
321 static char privateprefix[20] = "";
322 static char unknownprefix[20] = "";
323 static long resetinterval = 3600; /*!< How often (in seconds) to reset unused channels. Default 1 hour. */
324 static struct ast_channel inuse;
325 #ifdef PRI_GETSET_TIMERS
326 static int pritimers[PRI_MAX_TIMERS];
328 static int pridebugfd = -1;
329 static char pridebugfilename[1024] = "";
332 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
333 static int firstdigittimeout = 16000;
335 /*! \brief How long to wait for following digits (FXO logic) */
336 static int gendigittimeout = 8000;
338 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
339 static int matchdigittimeout = 3000;
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 LINKSET_STATE_DOWN = 0,
434 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
437 static struct zt_ss7 linksets[NUM_SPANS];
439 static int cur_ss7type = -1;
440 static int cur_linkset = -1;
441 static int cur_pointcode = -1;
442 static int cur_cicbeginswith = -1;
443 static int cur_adjpointcode = -1;
444 static int cur_networkindicator = -1;
445 static int cur_defaultdpc = -1;
446 #endif /* HAVE_SS7 */
450 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
451 #define PRI_CHANNEL(p) ((p) & 0xff)
452 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
453 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
456 pthread_t master; /*!< Thread of master */
457 ast_mutex_t lock; /*!< Mutex */
458 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
459 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
460 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
461 int minunused; /*!< Min # of channels to keep empty */
462 int minidle; /*!< Min # of "idling" calls to keep active */
463 int nodetype; /*!< Node type */
464 int switchtype; /*!< Type of switch to emulate */
465 int nsf; /*!< Network-Specific Facilities */
466 int dialplan; /*!< Dialing plan */
467 int localdialplan; /*!< Local dialing plan */
468 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
469 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
470 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
471 char privateprefix[20]; /*!< for private dialplans */
472 char unknownprefix[20]; /*!< for unknown dialplans */
473 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
474 int trunkgroup; /*!< What our trunkgroup is */
475 int mastertrunkgroup; /*!< What trunk group is our master */
476 int prilogicalspan; /*!< Logical span number within trunk group */
477 int numchans; /*!< Num of channels we represent */
478 int overlapdial; /*!< In overlap dialing mode */
479 int facilityenable; /*!< Enable facility IEs */
480 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
481 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
482 struct pri *pri; /*!< Currently active D-channel */
484 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
489 time_t lastreset; /*!< time when unused channels were last reset */
490 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
491 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
492 struct zt_pvt *crvs; /*!< Member CRV structs */
493 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
497 static struct zt_pri pris[NUM_SPANS];
499 static int pritype = PRI_CPE;
502 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
504 #define DEFAULT_PRI_DEBUG 0
507 static inline void pri_rel(struct zt_pri *pri)
509 ast_mutex_unlock(&pri->lock);
512 static int switchtype = PRI_SWITCH_NI2;
513 static int nsf = PRI_NSF_NONE;
514 static int dialplan = PRI_NATIONAL_ISDN + 1;
515 static int localdialplan = PRI_NATIONAL_ISDN + 1;
518 /*! Shut up the compiler */
522 #define SUB_REAL 0 /*!< Active call */
523 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
524 #define SUB_THREEWAY 2 /*!< Three-way call */
526 /* Polarity states */
527 #define POLARITY_IDLE 0
528 #define POLARITY_REV 1
531 static struct zt_distRings drings;
533 struct distRingData {
537 struct ringContextData {
538 char contextData[AST_MAX_CONTEXT];
540 struct zt_distRings {
541 struct distRingData ringnum[3];
542 struct ringContextData ringContext[3];
545 static char *subnames[] = {
551 struct zt_subchannel {
553 struct ast_channel *owner;
555 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
556 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
557 unsigned int needringing:1;
558 unsigned int needbusy:1;
559 unsigned int needcongestion:1;
560 unsigned int needcallerid:1;
561 unsigned int needanswer:1;
562 unsigned int needflash:1;
563 unsigned int needhold:1;
564 unsigned int needunhold:1;
565 unsigned int linear:1;
566 unsigned int inthreeway:1;
570 #define CONF_USER_REAL (1 << 0)
571 #define CONF_USER_THIRDCALL (1 << 1)
575 static struct zt_pvt {
577 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
578 /*!< Up to three channels can be associated with this call */
580 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
581 struct zt_subchannel subs[3]; /*!< Sub-channels */
582 struct zt_confinfo saveconf; /*!< Saved conference info */
584 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
585 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
586 int inconference; /*!< If our real should be in the conference */
588 int sig; /*!< Signalling style */
589 int radio; /*!< radio type */
590 int outsigmod; /*!< Outbound Signalling style (modifier) */
591 int oprmode; /*!< "Operator Services" mode */
592 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
595 int tonezone; /*!< tone zone for this chan, or -1 for default */
596 struct zt_pvt *next; /*!< Next channel in list */
597 struct zt_pvt *prev; /*!< Prev channel in list */
601 unsigned int answeronpolarityswitch:1;
602 unsigned int busydetect:1;
603 unsigned int callreturn:1;
604 unsigned int callwaiting:1;
605 unsigned int callwaitingcallerid:1;
606 unsigned int cancallforward:1;
607 unsigned int canpark:1;
608 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
609 unsigned int destroy:1;
610 unsigned int didtdd:1; /*!< flag to say its done it once */
611 unsigned int dialednone:1;
612 unsigned int dialing:1;
613 unsigned int digital:1;
615 unsigned int echobreak:1;
616 unsigned int echocanbridged:1;
617 unsigned int echocanon:1;
618 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
619 unsigned int firstradio:1;
620 unsigned int hanguponpolarityswitch:1;
621 unsigned int hardwaredtmf:1;
622 unsigned int hidecallerid:1;
623 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
624 unsigned int ignoredtmf:1;
625 unsigned int immediate:1; /*!< Answer before getting digits? */
626 unsigned int inalarm:1;
627 unsigned int mate:1; /*!< flag to say its in MATE mode */
628 unsigned int outgoing:1;
629 unsigned int overlapdial:1;
630 unsigned int permcallwaiting:1;
631 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
632 unsigned int priindication_oob:1;
633 unsigned int priexclusive:1;
634 unsigned int pulse:1;
635 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
636 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
637 unsigned int threewaycalling:1;
638 unsigned int transfer:1;
639 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
640 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
641 unsigned int usedistinctiveringdetection:1;
642 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
643 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
644 /* Channel state or unavilability flags */
645 unsigned int inservice:1;
646 unsigned int locallyblocked:1;
647 unsigned int remotelyblocked:1;
648 #if defined(HAVE_PRI)
649 unsigned int alerting:1;
650 unsigned int alreadyhungup:1;
651 unsigned int isidlecall:1;
652 unsigned int proceeding:1;
653 unsigned int progress:1;
654 unsigned int resetting:1;
655 unsigned int setup_ack:1;
657 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
658 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
660 struct zt_distRings drings;
662 char context[AST_MAX_CONTEXT];
663 char defcontext[AST_MAX_CONTEXT];
664 char exten[AST_MAX_EXTENSION];
665 char language[MAX_LANGUAGE];
666 char mohinterpret[MAX_MUSICCLASS];
667 char mohsuggest[MAX_MUSICCLASS];
669 char cid_ani[AST_MAX_EXTENSION];
671 char cid_num[AST_MAX_EXTENSION];
672 int cid_ton; /*!< Type Of Number (TON) */
673 char cid_name[AST_MAX_EXTENSION];
674 char lastcid_num[AST_MAX_EXTENSION];
675 char lastcid_name[AST_MAX_EXTENSION];
676 char *origcid_num; /*!< malloced original callerid */
677 char *origcid_name; /*!< malloced original callerid */
678 char callwait_num[AST_MAX_EXTENSION];
679 char callwait_name[AST_MAX_EXTENSION];
680 char rdnis[AST_MAX_EXTENSION];
681 char dnid[AST_MAX_EXTENSION];
684 int confno; /*!< Our conference */
685 int confusers; /*!< Who is using our conference */
686 int propconfno; /*!< Propagated conference number */
687 ast_group_t callgroup;
688 ast_group_t pickupgroup;
689 int channel; /*!< Channel Number or CRV */
690 int span; /*!< Span number */
691 time_t guardtime; /*!< Must wait this much time before using for new call */
692 int cid_signalling; /*!< CID signalling type bell202 or v23 */
693 int cid_start; /*!< CID start indicator, polarity or ring */
694 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
695 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
696 int cidcwexpire; /*!< When to expire our muting for CID/CW */
697 unsigned char *cidspill;
710 int busy_quietlength;
712 struct timeval flashtime; /*!< Last flash-hook time */
714 int cref; /*!< Call reference number */
715 ZT_DIAL_OPERATION dop;
716 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
718 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
719 int amaflags; /*!< AMA Flags */
720 struct tdd_state *tdd; /*!< TDD flag */
721 char call_forward[AST_MAX_EXTENSION];
722 char mailbox[AST_MAX_EXTENSION];
726 int distinctivering; /*!< Which distinctivering to use */
727 int cidrings; /*!< Which ring to deliver CID on */
728 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
730 int polarityonanswerdelay;
731 struct timeval polaritydelaytv;
732 int sendcalleridafter;
735 struct zt_pvt *bearer;
736 struct zt_pvt *realcall;
745 struct isup_call *ss7call;
747 int cic; /*!< CIC associated with channel */
750 } *iflist = NULL, *ifend = NULL;
752 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
753 static int zt_digit_begin(struct ast_channel *ast, char digit);
754 static int zt_digit_end(struct ast_channel *ast, char digit);
755 static int zt_sendtext(struct ast_channel *c, const char *text);
756 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
757 static int zt_hangup(struct ast_channel *ast);
758 static int zt_answer(struct ast_channel *ast);
759 static struct ast_frame *zt_read(struct ast_channel *ast);
760 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
761 static struct ast_frame *zt_exception(struct ast_channel *ast);
762 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
763 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
764 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
765 static int zt_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
767 static const struct ast_channel_tech zap_tech = {
769 .description = tdesc,
770 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
771 .requester = zt_request,
772 .send_digit_begin = zt_digit_begin,
773 .send_digit_end = zt_digit_end,
774 .send_text = zt_sendtext,
781 .exception = zt_exception,
782 .indicate = zt_indicate,
784 .setoption = zt_setoption,
785 .func_channel_read = zt_func_read,
789 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
791 #define GET_CHANNEL(p) ((p)->channel)
794 struct zt_pvt *round_robin[32];
797 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
800 /* Grab the lock first */
802 res = ast_mutex_trylock(&pri->lock);
804 ast_mutex_unlock(&pvt->lock);
805 /* Release the lock and try again */
807 ast_mutex_lock(&pvt->lock);
810 /* Then break the poll */
811 pthread_kill(pri->master, SIGURG);
817 static inline void ss7_rel(struct zt_ss7 *ss7)
819 ast_mutex_unlock(&ss7->lock);
822 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
825 /* Grab the lock first */
827 res = ast_mutex_trylock(&pri->lock);
829 ast_mutex_unlock(&pvt->lock);
830 /* Release the lock and try again */
832 ast_mutex_lock(&pvt->lock);
835 /* Then break the poll */
836 pthread_kill(pri->master, SIGURG);
840 #define NUM_CADENCE_MAX 25
841 static int num_cadence = 4;
842 static int user_has_defined_cadences = 0;
844 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
845 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
846 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
847 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
848 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
851 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
852 * is 1, the second pause is 2 and so on.
855 static int cidrings[NUM_CADENCE_MAX] = {
856 2, /*!< Right after first long ring */
857 4, /*!< Right after long part */
858 3, /*!< After third chirp */
859 2, /*!< Second spell */
862 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
863 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
865 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
866 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
868 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
871 if (p->subs[0].owner == ast)
873 else if (p->subs[1].owner == ast)
875 else if (p->subs[2].owner == ast)
880 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
886 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
888 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
893 ast_mutex_unlock(&pri->lock);
896 if (p->subs[a].owner) {
897 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
898 ast_mutex_unlock(&p->lock);
900 ast_mutex_lock(&p->lock);
902 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
903 ast_mutex_unlock(&p->subs[a].owner->lock);
911 ast_mutex_lock(&pri->lock);
915 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
918 struct zt_pri *pri = (struct zt_pri*) data;
921 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
923 /* We must unlock the PRI to avoid the possibility of a deadlock */
924 #if defined(HAVE_PRI) || defined(HAVE_SS7)
929 ast_mutex_unlock(&pri->lock);
934 ast_mutex_unlock(&ss7->lock);
944 if (ast_mutex_trylock(&p->owner->lock)) {
945 ast_mutex_unlock(&p->lock);
947 ast_mutex_lock(&p->lock);
949 ast_queue_frame(p->owner, f);
950 ast_mutex_unlock(&p->owner->lock);
956 #if defined(HAVE_PRI) || defined(HAVE_SS7)
961 ast_mutex_lock(&pri->lock);
966 ast_mutex_lock(&ss7->lock);
977 static int restore_gains(struct zt_pvt *p);
979 static void swap_subs(struct zt_pvt *p, int a, int b)
983 struct ast_channel *towner;
986 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
988 tchan = p->subs[a].chan;
989 towner = p->subs[a].owner;
990 tinthreeway = p->subs[a].inthreeway;
992 p->subs[a].chan = p->subs[b].chan;
993 p->subs[a].owner = p->subs[b].owner;
994 p->subs[a].inthreeway = p->subs[b].inthreeway;
996 p->subs[b].chan = tchan;
997 p->subs[b].owner = towner;
998 p->subs[b].inthreeway = tinthreeway;
1000 if (p->subs[a].owner)
1001 p->subs[a].owner->fds[0] = p->subs[a].zfd;
1002 if (p->subs[b].owner)
1003 p->subs[b].owner->fds[0] = p->subs[b].zfd;
1004 wakeup_sub(p, a, NULL);
1005 wakeup_sub(p, b, NULL);
1008 static int zt_open(char *fn)
1016 for (x = 0; x < strlen(fn); x++) {
1017 if (!isdigit(fn[x])) {
1025 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1028 fn = "/dev/zap/channel";
1030 fd = open(fn, O_RDWR | O_NONBLOCK);
1032 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1036 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1040 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1045 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
1049 static void zt_close(int fd)
1055 static int zt_setlinear(int zfd, int linear)
1058 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1065 static int alloc_sub(struct zt_pvt *p, int x)
1069 if (p->subs[x].zfd < 0) {
1070 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1071 if (p->subs[x].zfd > -1) {
1072 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1074 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1075 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1076 bi.numbufs = numbufs;
1077 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1079 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1082 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1083 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1084 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1085 zt_close(p->subs[x].zfd);
1086 p->subs[x].zfd = -1;
1090 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1093 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1096 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1100 static int unalloc_sub(struct zt_pvt *p, int x)
1103 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1107 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
1108 if (p->subs[x].zfd > -1) {
1109 zt_close(p->subs[x].zfd);
1111 p->subs[x].zfd = -1;
1112 p->subs[x].linear = 0;
1113 p->subs[x].chan = 0;
1114 p->subs[x].owner = NULL;
1115 p->subs[x].inthreeway = 0;
1116 p->polarity = POLARITY_IDLE;
1117 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1121 static int digit_to_dtmfindex(char digit)
1124 return ZT_TONE_DTMF_BASE + (digit - '0');
1125 else if (digit >= 'A' && digit <= 'D')
1126 return ZT_TONE_DTMF_A + (digit - 'A');
1127 else if (digit >= 'a' && digit <= 'd')
1128 return ZT_TONE_DTMF_A + (digit - 'a');
1129 else if (digit == '*')
1130 return ZT_TONE_DTMF_s;
1131 else if (digit == '#')
1132 return ZT_TONE_DTMF_p;
1137 static int zt_digit_begin(struct ast_channel *chan, char digit)
1143 pvt = chan->tech_pvt;
1145 ast_mutex_lock(&pvt->lock);
1147 index = zt_get_index(chan, pvt, 0);
1149 if ((index != SUB_REAL) || !pvt->owner)
1153 if ((pvt->sig == SIG_PRI) && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1154 if (pvt->setup_ack) {
1155 if (!pri_grab(pvt, pvt->pri)) {
1156 pri_information(pvt->pri->pri, pvt->call, digit);
1159 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1160 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1163 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1164 res = strlen(pvt->dialdest);
1165 pvt->dialdest[res++] = digit;
1166 pvt->dialdest[res] = '\0';
1171 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1174 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1176 ZT_DIAL_OPERATION zo = {
1177 .op = ZT_DIAL_OP_APPEND,
1179 .dialstr[1] = digit,
1182 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1183 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1188 ast_log(LOG_DEBUG, "Started VLDTMF digit '%c'\n", digit);
1190 pvt->begindigit = digit;
1194 ast_mutex_unlock(&pvt->lock);
1196 return 0; /* Tell Asterisk not to generate inband indications */
1199 static int zt_digit_end(struct ast_channel *chan, char digit)
1206 pvt = chan->tech_pvt;
1208 ast_mutex_lock(&pvt->lock);
1210 index = zt_get_index(chan, pvt, 0);
1212 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1216 /* This means that the digit was already sent via PRI signalling */
1217 if (pvt->sig == SIG_PRI && !pvt->begindigit)
1221 if (pvt->begindigit) {
1224 ast_log(LOG_DEBUG, "Ending VLDTMF digit '%c'\n", digit);
1225 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1227 pvt->begindigit = 0;
1231 ast_mutex_unlock(&pvt->lock);
1236 static char *events[] = {
1249 "Hook Transition Complete",
1254 "Polarity Reversal",
1262 { ZT_ALARM_RED, "Red Alarm" },
1263 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1264 { ZT_ALARM_BLUE, "Blue Alarm" },
1265 { ZT_ALARM_RECOVER, "Recovering" },
1266 { ZT_ALARM_LOOPBACK, "Loopback" },
1267 { ZT_ALARM_NOTOPEN, "Not Open" },
1268 { ZT_ALARM_NONE, "None" },
1271 static char *alarm2str(int alarm)
1274 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1275 if (alarms[x].alarm & alarm)
1276 return alarms[x].name;
1278 return alarm ? "Unknown Alarm" : "No Alarm";
1281 static char *event2str(int event)
1283 static char buf[256];
1284 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1285 return events[event];
1286 sprintf(buf, "Event %d", event); /* safe */
1291 static char *dialplan2str(int dialplan)
1293 if (dialplan == -1) {
1294 return("Dynamically set dialplan in ISDN");
1296 return (pri_plan2str(dialplan));
1300 static char *zap_sig2str(int sig)
1302 static char buf[256];
1305 return "E & M Immediate";
1307 return "E & M Wink";
1311 return "Feature Group D (DTMF)";
1313 return "Feature Group D (MF)";
1314 case SIG_FEATDMF_TA:
1315 return "Feature Groud D (MF) Tandem Access";
1317 return "Feature Group B (MF)";
1321 return "FGC/CAMA (Dialpulse)";
1322 case SIG_FGC_CAMAMF:
1323 return "FGC/CAMA (MF)";
1325 return "FXS Loopstart";
1327 return "FXS Groundstart";
1329 return "FXS Kewlstart";
1331 return "FXO Loopstart";
1333 return "FXO Groundstart";
1335 return "FXO Kewlstart";
1337 return "PRI Signalling";
1339 return "SS7 Signalling";
1341 return "SF (Tone) Signalling Immediate";
1343 return "SF (Tone) Signalling Wink";
1345 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1346 case SIG_SF_FEATDMF:
1347 return "SF (Tone) Signalling with Feature Group D (MF)";
1349 return "SF (Tone) Signalling with Feature Group B (MF)";
1350 case SIG_GR303FXOKS:
1351 return "GR-303 Signalling with FXOKS";
1352 case SIG_GR303FXSKS:
1353 return "GR-303 Signalling with FXSKS";
1355 return "Pseudo Signalling";
1357 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1362 #define sig2str zap_sig2str
1364 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1366 /* If the conference already exists, and we're already in it
1367 don't bother doing anything */
1370 memset(&zi, 0, sizeof(zi));
1373 if (slavechannel > 0) {
1374 /* If we have only one slave, do a digital mon */
1375 zi.confmode = ZT_CONF_DIGITALMON;
1376 zi.confno = slavechannel;
1379 /* Real-side and pseudo-side both participate in conference */
1380 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1381 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1383 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1384 zi.confno = p->confno;
1386 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1390 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1391 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1394 if (slavechannel < 1) {
1395 p->confno = zi.confno;
1397 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1399 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1403 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1405 /* If they're listening to our channel, they're ours */
1406 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1408 /* If they're a talker on our (allocated) conference, they're ours */
1409 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1414 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1417 if (/* Can't delete if there's no zfd */
1419 /* Don't delete from the conference if it's not our conference */
1421 /* Don't delete if we don't think it's conferenced at all (implied) */
1423 memset(&zi, 0, sizeof(zi));
1427 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1428 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1432 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1433 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1437 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1441 struct zt_pvt *slave = NULL;
1442 /* Start out optimistic */
1444 /* Update conference state in a stateless fashion */
1445 for (x = 0; x < 3; x++) {
1446 /* Any three-way calling makes slave native mode *definitely* out
1448 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1451 /* If we don't have any 3-way calls, check to see if we have
1452 precisely one slave */
1453 if (useslavenative) {
1454 for (x = 0; x < MAX_SLAVES; x++) {
1457 /* Whoops already have a slave! No
1458 slave native and stop right away */
1463 /* We have one slave so far */
1464 slave = p->slaves[x];
1469 /* If no slave, slave native definitely out */
1472 else if (slave->law != p->law) {
1478 return useslavenative;
1481 static int reset_conf(struct zt_pvt *p)
1484 memset(&zi, 0, sizeof(zi));
1486 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1487 if (p->subs[SUB_REAL].zfd > -1) {
1488 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1489 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1494 static int update_conf(struct zt_pvt *p)
1499 struct zt_pvt *slave = NULL;
1501 useslavenative = isslavenative(p, &slave);
1502 /* Start with the obvious, general stuff */
1503 for (x = 0; x < 3; x++) {
1504 /* Look for three way calls */
1505 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1506 conf_add(p, &p->subs[x], x, 0);
1509 conf_del(p, &p->subs[x], x);
1512 /* If we have a slave, add him to our conference now. or DAX
1513 if this is slave native */
1514 for (x = 0; x < MAX_SLAVES; x++) {
1517 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1519 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1524 /* If we're supposed to be in there, do so now */
1525 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1527 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1529 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1533 /* If we have a master, add ourselves to his conference */
1535 if (isslavenative(p->master, NULL)) {
1536 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1538 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1542 /* Nobody is left (or should be left) in our conference.
1547 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1551 static void zt_enable_ec(struct zt_pvt *p)
1559 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1564 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1567 if (p->echocancel) {
1568 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1570 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1572 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1575 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1577 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1581 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1585 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1589 static void zt_train_ec(struct zt_pvt *p)
1593 if (p && p->echocancel && p->echotraining) {
1594 x = p->echotraining;
1595 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1597 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1600 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1604 ast_log(LOG_DEBUG, "No echo training requested\n");
1608 static void zt_disable_ec(struct zt_pvt *p)
1612 if (p->echocancel) {
1614 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1616 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1619 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1625 static void fill_txgain(struct zt_gains *g, float gain, int law)
1629 float linear_gain = pow(10.0, gain / 20.0);
1633 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1635 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1636 if (k > 32767) k = 32767;
1637 if (k < -32767) k = -32767;
1638 g->txgain[j] = AST_LIN2A(k);
1645 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1647 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1648 if (k > 32767) k = 32767;
1649 if (k < -32767) k = -32767;
1650 g->txgain[j] = AST_LIN2MU(k);
1659 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1663 float linear_gain = pow(10.0, gain / 20.0);
1667 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1669 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1670 if (k > 32767) k = 32767;
1671 if (k < -32767) k = -32767;
1672 g->rxgain[j] = AST_LIN2A(k);
1679 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1681 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1682 if (k > 32767) k = 32767;
1683 if (k < -32767) k = -32767;
1684 g->rxgain[j] = AST_LIN2MU(k);
1693 static int set_actual_txgain(int fd, int chan, float gain, int law)
1698 memset(&g, 0, sizeof(g));
1700 res = ioctl(fd, ZT_GETGAINS, &g);
1703 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1707 fill_txgain(&g, gain, law);
1709 return ioctl(fd, ZT_SETGAINS, &g);
1712 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1717 memset(&g, 0, sizeof(g));
1719 res = ioctl(fd, ZT_GETGAINS, &g);
1722 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1726 fill_rxgain(&g, gain, law);
1728 return ioctl(fd, ZT_SETGAINS, &g);
1731 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1733 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1736 static int bump_gains(struct zt_pvt *p)
1740 /* Bump receive gain by 5.0db */
1741 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
1743 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1750 static int restore_gains(struct zt_pvt *p)
1754 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1756 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1763 static inline int zt_set_hook(int fd, int hs)
1767 res = ioctl(fd, ZT_HOOK, &x);
1770 if (errno == EINPROGRESS) return 0;
1771 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1776 static inline int zt_confmute(struct zt_pvt *p, int muted)
1780 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1782 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1784 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1786 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1788 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1792 static int save_conference(struct zt_pvt *p)
1794 struct zt_confinfo c;
1796 if (p->saveconf.confmode) {
1797 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1800 p->saveconf.chan = 0;
1801 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1803 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1804 p->saveconf.confmode = 0;
1809 c.confmode = ZT_CONF_NORMAL;
1810 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1812 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1816 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1820 static int restore_conference(struct zt_pvt *p)
1823 if (p->saveconf.confmode) {
1824 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1825 p->saveconf.confmode = 0;
1827 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1832 ast_log(LOG_DEBUG, "Restored conferencing\n");
1836 static int send_callerid(struct zt_pvt *p);
1838 static int send_cwcidspill(struct zt_pvt *p)
1842 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1844 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1845 /* Make sure we account for the end */
1846 p->cidlen += READ_SIZE * 4;
1849 if (option_verbose > 2)
1850 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1854 static int has_voicemail(struct zt_pvt *p)
1857 return ast_app_has_voicemail(p->mailbox, NULL);
1860 static int send_callerid(struct zt_pvt *p)
1862 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1864 /* Take out of linear mode if necessary */
1865 if (p->subs[SUB_REAL].linear) {
1866 p->subs[SUB_REAL].linear = 0;
1867 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1869 while (p->cidpos < p->cidlen) {
1870 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1872 if (errno == EAGAIN)
1875 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1885 if (p->callwaitcas) {
1886 /* Wait for CID/CW to expire */
1887 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1889 restore_conference(p);
1893 static int zt_callwait(struct ast_channel *ast)
1895 struct zt_pvt *p = ast->tech_pvt;
1896 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1898 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1901 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1905 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1906 if (!p->callwaitrings && p->callwaitingcallerid) {
1907 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1909 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1911 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1913 p->cidlen = 2400 + READ_SIZE * 4;
1921 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1923 struct zt_pvt *p = ast->tech_pvt;
1924 int x, res, index,mysig;
1929 char dest[256]; /* must be same length as p->dialdest */
1930 ast_mutex_lock(&p->lock);
1931 ast_copy_string(dest, rdest, sizeof(dest));
1932 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1933 if ((ast->_state == AST_STATE_BUSY)) {
1934 p->subs[SUB_REAL].needbusy = 1;
1935 ast_mutex_unlock(&p->lock);
1938 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1939 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1940 ast_mutex_unlock(&p->lock);
1944 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
1946 /* Special pseudo -- automatically up */
1947 ast_setstate(ast, AST_STATE_UP);
1948 ast_mutex_unlock(&p->lock);
1951 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1952 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1954 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1957 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1960 if (p->outsigmod > -1)
1961 mysig = p->outsigmod;
1967 if (p->owner == ast) {
1968 /* Normal ring, on hook */
1970 /* Don't send audio while on hook, until the call is answered */
1972 if (p->use_callerid) {
1973 /* Generate the Caller-ID spill if desired */
1975 ast_log(LOG_WARNING, "cidspill already exists??\n");
1979 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1980 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1985 /* Choose proper cadence */
1986 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1987 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
1988 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1989 p->cidrings = cidrings[p->distinctivering - 1];
1991 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1992 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1993 p->cidrings = p->sendcalleridafter;
1996 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1997 c = strchr(dest, '/');
2000 if (c && (strlen(c) < p->stripmsd)) {
2001 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2005 p->dop.op = ZT_DIAL_OP_REPLACE;
2006 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2008 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
2010 p->dop.dialstr[0] = '\0';
2013 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2014 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2015 ast_mutex_unlock(&p->lock);
2020 /* Call waiting call */
2021 p->callwaitrings = 0;
2022 if (ast->cid.cid_num)
2023 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2025 p->callwait_num[0] = '\0';
2026 if (ast->cid.cid_name)
2027 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2029 p->callwait_name[0] = '\0';
2030 /* Call waiting tone instead */
2031 if (zt_callwait(ast)) {
2032 ast_mutex_unlock(&p->lock);
2035 /* Make ring-back */
2036 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2037 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2040 n = ast->cid.cid_name;
2041 l = ast->cid.cid_num;
2043 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2045 p->lastcid_num[0] = '\0';
2047 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2049 p->lastcid_name[0] = '\0';
2050 ast_setstate(ast, AST_STATE_RINGING);
2051 index = zt_get_index(ast, p, 0);
2053 p->subs[index].needringing = 1;
2066 case SIG_FGC_CAMAMF:
2071 case SIG_SF_FEATDMF:
2072 case SIG_FEATDMF_TA:
2074 c = strchr(dest, '/');
2079 if (strlen(c) < p->stripmsd) {
2080 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2081 ast_mutex_unlock(&p->lock);
2085 /* Start the trunk, if not GR-303 */
2089 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2091 if (errno != EINPROGRESS) {
2092 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2093 ast_mutex_unlock(&p->lock);
2101 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
2102 p->dop.op = ZT_DIAL_OP_REPLACE;
2108 l = ast->cid.cid_num;
2110 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2112 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2115 l = ast->cid.cid_num;
2117 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2119 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2121 case SIG_FEATDMF_TA:
2123 const char *cic, *ozz;
2125 /* If you have to go through a Tandem Access point you need to use this */
2126 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2129 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2133 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2134 ast_mutex_unlock(&p->lock);
2137 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2138 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2143 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2146 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2148 case SIG_FGC_CAMAMF:
2150 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2154 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2156 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2160 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2161 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2162 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2163 p->echorest[sizeof(p->echorest) - 1] = '\0';
2165 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2169 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2171 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2172 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2173 ast_mutex_unlock(&p->lock);
2178 ast_log(LOG_DEBUG, "Deferring dialing...\n");
2181 if (ast_strlen_zero(c))
2183 ast_setstate(ast, AST_STATE_DIALING);
2186 /* Special pseudo -- automatically up*/
2187 ast_setstate(ast, AST_STATE_UP);
2191 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2192 p->dialdest[0] = '\0';
2196 ast_log(LOG_DEBUG, "not yet implemented\n");
2197 ast_mutex_unlock(&p->lock);
2202 c = strchr(dest, '/');
2208 if (!p->hidecallerid) {
2209 l = ast->cid.cid_num;
2214 ss7_grab(p, p->ss7);
2215 p->digital = IS_DIGITAL(ast->transfercapability);
2216 p->ss7call = isup_new_call(p->ss7->ss7);
2220 ast_mutex_unlock(&p->lock);
2221 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2225 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, c + p->stripmsd, l);
2227 isup_iam(p->ss7->ss7, p->ss7call);
2230 #endif /* HAVE_SS7 */
2234 #ifdef SUPPORT_USERUSER
2235 const char *useruser;
2239 int prilocaldialplan;
2243 int redirect_reason;
2245 c = strchr(dest, '/');
2250 if (!p->hidecalleridname)
2251 n = ast->cid.cid_name;
2254 if (!p->hidecallerid) {
2255 l = ast->cid.cid_num;
2256 n = ast->cid.cid_name;
2261 if (strlen(c) < p->stripmsd) {
2262 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2263 ast_mutex_unlock(&p->lock);
2266 if (mysig != SIG_FXSKS) {
2267 p->dop.op = ZT_DIAL_OP_REPLACE;
2268 s = strchr(c + p->stripmsd, 'w');
2271 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2273 p->dop.dialstr[0] = '\0';
2276 p->dop.dialstr[0] = '\0';
2279 if (pri_grab(p, p->pri)) {
2280 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2281 ast_mutex_unlock(&p->lock);
2284 if (!(p->call = pri_new_call(p->pri->pri))) {
2285 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2287 ast_mutex_unlock(&p->lock);
2290 if (!(sr = pri_sr_new())) {
2291 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2293 ast_mutex_unlock(&p->lock);
2295 if (p->bearer || (mysig == SIG_FXSKS)) {
2298 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);
2299 p->bearer->call = p->call;
2302 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
2304 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2306 p->digital = IS_DIGITAL(ast->transfercapability);
2307 /* Add support for exclusive override */
2308 if (p->priexclusive)
2311 /* otherwise, traditional behavior */
2312 if (p->pri->nodetype == PRI_NETWORK)
2318 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2319 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2321 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2322 if (p->pri->facilityenable)
2323 pri_facility_enable(p->pri->pri);
2325 if (option_verbose > 2)
2326 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2328 pridialplan = p->pri->dialplan - 1;
2329 if (pridialplan == -2) { /* compute dynamically */
2330 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2331 dp_strip = strlen(p->pri->internationalprefix);
2332 pridialplan = PRI_INTERNATIONAL_ISDN;
2333 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2334 dp_strip = strlen(p->pri->nationalprefix);
2335 pridialplan = PRI_NATIONAL_ISDN;
2337 pridialplan = PRI_LOCAL_ISDN;
2340 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2343 prilocaldialplan = p->pri->localdialplan - 1;
2344 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
2345 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2346 ldp_strip = strlen(p->pri->internationalprefix);
2347 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2348 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2349 ldp_strip = strlen(p->pri->nationalprefix);
2350 prilocaldialplan = PRI_NATIONAL_ISDN;
2352 prilocaldialplan = PRI_LOCAL_ISDN;
2355 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2356 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2357 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2358 if (!strcasecmp(rr_str, "UNKNOWN"))
2359 redirect_reason = 0;
2360 else if (!strcasecmp(rr_str, "BUSY"))
2361 redirect_reason = 1;
2362 else if (!strcasecmp(rr_str, "NO_REPLY"))
2363 redirect_reason = 2;
2364 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2365 redirect_reason = 15;
2367 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2369 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2370 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2372 #ifdef SUPPORT_USERUSER
2373 /* User-user info */
2374 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2377 pri_sr_set_useruser(sr, useruser);
2380 if (pri_setup(p->pri->pri, p->call, sr)) {
2381 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2382 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2384 ast_mutex_unlock(&p->lock);
2389 ast_setstate(ast, AST_STATE_DIALING);
2393 ast_mutex_unlock(&p->lock);
2397 static void destroy_zt_pvt(struct zt_pvt **pvt)
2399 struct zt_pvt *p = *pvt;
2400 /* Remove channel from the list */
2402 p->prev->next = p->next;
2404 p->next->prev = p->prev;
2406 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2407 ast_mutex_destroy(&p->lock);
2412 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2422 for (i = 0; i < 3; i++) {
2423 if (cur->subs[i].owner) {
2429 prev->next = cur->next;
2431 prev->next->prev = prev;
2437 iflist->prev = NULL;
2441 if (cur->subs[SUB_REAL].zfd > -1) {
2442 zt_close(cur->subs[SUB_REAL].zfd);
2444 destroy_zt_pvt(&cur);
2448 prev->next = cur->next;
2450 prev->next->prev = prev;
2456 iflist->prev = NULL;
2460 if (cur->subs[SUB_REAL].zfd > -1) {
2461 zt_close(cur->subs[SUB_REAL].zfd);
2463 destroy_zt_pvt(&cur);
2469 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2471 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2473 static char *zap_send_keypad_facility_descrip =
2474 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2475 " IE over the current channel.\n";
2477 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2479 /* Data will be our digit string */
2481 char *digits = (char *) data;
2483 if (ast_strlen_zero(digits)) {
2485 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
2489 p = (struct zt_pvt *)chan->tech_pvt;
2493 ast_log(LOG_DEBUG, "Unable to find technology private\n");
2497 ast_mutex_lock(&p->lock);
2499 if (!p->pri || !p->call) {
2501 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2502 ast_mutex_unlock(&p->lock);
2506 if (!pri_grab(p, p->pri)) {
2507 pri_keypad_facility(p->pri->pri, p->call, digits);
2511 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
2512 ast_mutex_unlock(&p->lock);
2516 ast_mutex_unlock(&p->lock);
2521 static int pri_is_up(struct zt_pri *pri)
2524 for (x = 0; x < NUM_DCHANS; x++) {
2525 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2531 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2533 bearer->owner = &inuse;
2534 bearer->realcall = crv;
2535 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2536 if (crv->subs[SUB_REAL].owner)
2537 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2538 crv->bearer = bearer;
2539 crv->call = bearer->call;
2544 static char *pri_order(int level)
2554 return "Quaternary";
2560 /* Returns fd of the active dchan */
2561 static int pri_active_dchan_fd(struct zt_pri *pri)
2565 for (x = 0; x < NUM_DCHANS; x++) {
2566 if ((pri->dchans[x] == pri->pri))
2573 static int pri_find_dchan(struct zt_pri *pri)
2580 for (x = 0; x < NUM_DCHANS; x++) {
2581 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2583 if (pri->dchans[x] == old) {
2589 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2590 pri->dchannels[newslot]);
2592 if (old && (oldslot != newslot))
2593 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2594 pri->dchannels[oldslot], pri->dchannels[newslot]);
2595 pri->pri = pri->dchans[newslot];
2600 static int zt_hangup(struct ast_channel *ast)
2604 /*static int restore_gains(struct zt_pvt *p);*/
2605 struct zt_pvt *p = ast->tech_pvt;
2606 struct zt_pvt *tmp = NULL;
2607 struct zt_pvt *prev = NULL;
2611 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2612 if (!ast->tech_pvt) {
2613 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2617 ast_mutex_lock(&p->lock);
2619 index = zt_get_index(ast, p, 1);
2621 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2623 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2629 if (p->origcid_num) {
2630 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2631 free(p->origcid_num);
2632 p->origcid_num = NULL;
2634 if (p->origcid_name) {
2635 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2636 free(p->origcid_name);
2637 p->origcid_name = NULL;
2640 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2645 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2646 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2650 /* Real channel, do some fixup */
2651 p->subs[index].owner = NULL;
2652 p->subs[index].needanswer = 0;
2653 p->subs[index].needflash = 0;
2654 p->subs[index].needringing = 0;
2655 p->subs[index].needbusy = 0;
2656 p->subs[index].needcongestion = 0;
2657 p->subs[index].linear = 0;
2658 p->subs[index].needcallerid = 0;
2659 p->polarity = POLARITY_IDLE;
2660 zt_setlinear(p->subs[index].zfd, 0);
2661 if (index == SUB_REAL) {
2662 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2664 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2665 if (p->subs[SUB_CALLWAIT].inthreeway) {
2666 /* We had flipped over to answer a callwait and now it's gone */
2668 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2669 /* Move to the call-wait, but un-own us until they flip back. */
2670 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2671 unalloc_sub(p, SUB_CALLWAIT);
2674 /* The three way hung up, but we still have a call wait */
2676 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2677 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2678 unalloc_sub(p, SUB_THREEWAY);
2679 if (p->subs[SUB_REAL].inthreeway) {
2680 /* This was part of a three way call. Immediately make way for
2683 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2684 p->owner = p->subs[SUB_REAL].owner;
2686 /* This call hasn't been completed yet... Set owner to NULL */
2688 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2691 p->subs[SUB_REAL].inthreeway = 0;
2693 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2694 /* Move to the call-wait and switch back to them. */
2695 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2696 unalloc_sub(p, SUB_CALLWAIT);
2697 p->owner = p->subs[SUB_REAL].owner;
2698 if (p->owner->_state != AST_STATE_UP)
2699 p->subs[SUB_REAL].needanswer = 1;
2700 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2701 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
2702 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2703 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2704 unalloc_sub(p, SUB_THREEWAY);
2705 if (p->subs[SUB_REAL].inthreeway) {
2706 /* This was part of a three way call. Immediately make way for
2709 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2710 p->owner = p->subs[SUB_REAL].owner;
2712 /* This call hasn't been completed yet... Set owner to NULL */
2714 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2717 p->subs[SUB_REAL].inthreeway = 0;
2719 } else if (index == SUB_CALLWAIT) {
2720 /* Ditch the holding callwait call, and immediately make it availabe */
2721 if (p->subs[SUB_CALLWAIT].inthreeway) {
2722 /* This is actually part of a three way, placed on hold. Place the third part
2723 on music on hold now */
2724 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2725 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2726 S_OR(p->mohsuggest, NULL),
2727 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2729 p->subs[SUB_THREEWAY].inthreeway = 0;
2730 /* Make it the call wait now */
2731 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2732 unalloc_sub(p, SUB_THREEWAY);
2734 unalloc_sub(p, SUB_CALLWAIT);
2735 } else if (index == SUB_THREEWAY) {
2736 if (p->subs[SUB_CALLWAIT].inthreeway) {
2737 /* The other party of the three way call is currently in a call-wait state.
2738 Start music on hold for them, and take the main guy out of the third call */
2739 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
2740 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
2741 S_OR(p->mohsuggest, NULL),
2742 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2744 p->subs[SUB_CALLWAIT].inthreeway = 0;
2746 p->subs[SUB_REAL].inthreeway = 0;
2747 /* If this was part of a three way call index, let us make
2748 another three way call */
2749 unalloc_sub(p, SUB_THREEWAY);
2751 /* This wasn't any sort of call, but how are we an index? */
2752 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2756 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2759 p->distinctivering = 0;
2760 p->confirmanswer = 0;
2766 p->onhooktime = time(NULL);
2774 ast_dsp_free(p->dsp);
2778 law = ZT_LAW_DEFAULT;
2779 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2781 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2782 /* Perform low level hangup if no owner left */
2786 if (!ss7_grab(p, p->ss7)) {
2787 if (!p->alreadyhungup) {
2788 isup_rel(p->ss7->ss7, p->ss7call, ast->hangupcause ? ast->hangupcause : -1);
2790 p->alreadyhungup = 1;
2792 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
2794 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
2802 #ifdef SUPPORT_USERUSER
2803 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2806 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2807 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2808 if (!pri_grab(p, p->pri)) {
2809 if (p->alreadyhungup) {
2811 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2813 #ifdef SUPPORT_USERUSER
2814 pri_call_set_useruser(p->call, useruser);
2817 pri_hangup(p->pri->pri, p->call, -1);
2820 p->bearer->call = NULL;
2822 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2823 int icause = ast->hangupcause ? ast->hangupcause : -1;
2825 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2827 #ifdef SUPPORT_USERUSER
2828 pri_call_set_useruser(p->call, useruser);
2831 p->alreadyhungup = 1;
2833 p->bearer->alreadyhungup = 1;
2836 icause = atoi(cause);
2838 pri_hangup(p->pri->pri, p->call, icause);
2841 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2844 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2850 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2856 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7)))
2857 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2859 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2865 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2869 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2871 /* If they're off hook, try playing congestion */
2872 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
2873 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2875 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2881 /* Make sure we're not made available for at least two seconds assuming
2882 we were actually used for an inbound or outbound call. */
2883 if (ast->_state != AST_STATE_RESERVED) {
2884 time(&p->guardtime);
2889 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2896 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2897 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2901 p->callwaiting = p->permcallwaiting;
2902 p->hidecallerid = p->permhidecallerid;
2907 /* Restore data mode */
2908 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2910 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2915 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2916 /* Free up the bearer channel as well, and
2917 don't use its file descriptor anymore */
2918 update_conf(p->bearer);
2919 reset_conf(p->bearer);
2920 p->bearer->owner = NULL;
2921 p->bearer->realcall = NULL;
2923 p->subs[SUB_REAL].zfd = -1;
2930 p->callwaitingrepeat = 0;
2933 ast->tech_pvt = NULL;
2934 ast_mutex_unlock(&p->lock);
2935 if (option_verbose > 2)
2936 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2938 ast_mutex_lock(&iflock);
2944 destroy_channel(prev, tmp, 0);
2952 ast_mutex_unlock(&iflock);
2956 static int zt_answer(struct ast_channel *ast)
2958 struct zt_pvt *p = ast->tech_pvt;
2961 int oldstate = ast->_state;
2962 ast_setstate(ast, AST_STATE_UP);
2963 ast_mutex_lock(&p->lock);
2964 index = zt_get_index(ast, p, 0);
2967 /* nothing to do if a radio channel */
2968 if ((p->radio || (p->oprmode < 0))) {
2969 ast_mutex_unlock(&p->lock);
2983 case SIG_FEATDMF_TA:
2986 case SIG_FGC_CAMAMF:
2991 case SIG_SF_FEATDMF:
2996 /* Pick up the line */
2998 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2999 if (p->hanguponpolarityswitch) {
3000 gettimeofday(&p->polaritydelaytv, NULL);
3002 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
3003 tone_zone_play_tone(p->subs[index].zfd, -1);
3005 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
3006 if (oldstate == AST_STATE_RINGING) {
3008 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
3009 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3010 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3011 p->owner = p->subs[SUB_REAL].owner;
3014 if (p->sig & __ZT_SIG_FXS) {
3021 /* Send a pri acknowledge */
3022 if (!pri_grab(p, p->pri)) {
3024 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3027 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3034 if (!ss7_grab(p, p->ss7)) {
3036 res = isup_anm(p->ss7->ss7, p->ss7call);
3039 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3045 ast_mutex_unlock(&p->lock);
3048 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3051 ast_mutex_unlock(&p->lock);
3055 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)