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>res_smdi</depend>
42 <depend>zaptel_vldtmf</depend>
43 <depend>zaptel</depend>
44 <depend>tonezone</depend>
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
59 #include <sys/signal.h>
63 #if !defined(SOLARIS) && !defined(__FreeBSD__)
67 #include <sys/ioctl.h>
70 #include "asterisk/zapata.h"
80 #include "asterisk/lock.h"
81 #include "asterisk/channel.h"
82 #include "asterisk/config.h"
83 #include "asterisk/logger.h"
84 #include "asterisk/module.h"
85 #include "asterisk/pbx.h"
86 #include "asterisk/options.h"
87 #include "asterisk/file.h"
88 #include "asterisk/ulaw.h"
89 #include "asterisk/alaw.h"
90 #include "asterisk/callerid.h"
91 #include "asterisk/adsi.h"
92 #include "asterisk/cli.h"
93 #include "asterisk/cdr.h"
94 #include "asterisk/features.h"
95 #include "asterisk/musiconhold.h"
96 #include "asterisk/say.h"
97 #include "asterisk/tdd.h"
98 #include "asterisk/app.h"
99 #include "asterisk/dsp.h"
100 #include "asterisk/astdb.h"
101 #include "asterisk/manager.h"
102 #include "asterisk/causes.h"
103 #include "asterisk/term.h"
104 #include "asterisk/utils.h"
105 #include "asterisk/transcap.h"
106 #include "asterisk/stringfields.h"
107 #include "asterisk/abstract_jb.h"
108 #include "asterisk/smdi.h"
109 #include "asterisk/astobj.h"
110 #include "asterisk/event.h"
112 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
114 /*! Global jitterbuffer configuration - by default, jb is disabled */
115 static struct ast_jb_conf default_jbconf =
119 .resync_threshold = -1,
122 static struct ast_jb_conf global_jbconf;
124 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_PRI) && !defined(ZT_SIG_HARDHDLC))
125 #error "Your zaptel is too old. Please update"
128 #ifndef ZT_TONEDETECT
129 /* Work around older code with no tone detect */
130 #define ZT_EVENT_DTMFDOWN 0
131 #define ZT_EVENT_DTMFUP 0
134 /* define this to send PRI user-user information elements */
135 #undef SUPPORT_USERUSER
138 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
139 * the user hangs up to reset the state machine so ring works properly.
140 * This is used to be able to support kewlstart by putting the zhone in
141 * groundstart mode since their forward disconnect supervision is entirely
142 * broken even though their documentation says it isn't and their support
143 * is entirely unwilling to provide any assistance with their channel banks
144 * even though their web site says they support their products for life.
146 /* #define ZHONE_HACK */
149 * Define if you want to check the hook state for an FXO (FXS signalled) interface
150 * before dialing on it. Certain FXO interfaces always think they're out of
151 * service with this method however.
153 /* #define ZAP_CHECK_HOOKSTATE */
155 /*! \brief Typically, how many rings before we should send Caller*ID */
156 #define DEFAULT_CIDRINGS 1
158 #define CHANNEL_PSEUDO -12
160 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
162 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
163 #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))
165 static const char tdesc[] = "Zapata Telephony Driver"
174 static const char config[] = "zapata.conf";
176 #define SIG_EM ZT_SIG_EM
177 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
178 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
179 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
180 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
181 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
182 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
183 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
184 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
185 #define SIG_FXSLS ZT_SIG_FXSLS
186 #define SIG_FXSGS ZT_SIG_FXSGS
187 #define SIG_FXSKS ZT_SIG_FXSKS
188 #define SIG_FXOLS ZT_SIG_FXOLS
189 #define SIG_FXOGS ZT_SIG_FXOGS
190 #define SIG_FXOKS ZT_SIG_FXOKS
191 #define SIG_PRI ZT_SIG_CLEAR
192 #define SIG_SS7 (0x1000000 | ZT_SIG_CLEAR)
193 #define SIG_SF ZT_SIG_SF
194 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
195 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
196 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
197 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
198 #define SIG_EM_E1 ZT_SIG_EM_E1
199 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
200 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
203 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
204 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
206 #define CHAN_PSEUDO -2
208 #define DCHAN_PROVISIONED (1 << 0)
209 #define DCHAN_NOTINALARM (1 << 1)
210 #define DCHAN_UP (1 << 2)
212 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
214 /* Overlap dialing option types */
215 #define ZAP_OVERLAPDIAL_NONE 0
216 #define ZAP_OVERLAPDIAL_OUTGOING 1
217 #define ZAP_OVERLAPDIAL_INCOMING 2
218 #define ZAP_OVERLAPDIAL_BOTH (ZAP_OVERLAPDIAL_INCOMING|ZAP_OVERLAPDIAL_OUTGOING)
220 static char defaultcic[64] = "";
221 static char defaultozz[64] = "";
223 static char progzone[10] = "";
225 static int usedistinctiveringdetection = 0;
226 static int distinctiveringaftercid = 0;
228 static int numbufs = 4;
231 static struct ast_channel inuse;
232 #ifdef PRI_GETSET_TIMERS
233 static int pritimers[PRI_MAX_TIMERS];
235 static int pridebugfd = -1;
236 static char pridebugfilename[1024] = "";
239 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
240 static int firstdigittimeout = 16000;
242 /*! \brief How long to wait for following digits (FXO logic) */
243 static int gendigittimeout = 8000;
245 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
246 static int matchdigittimeout = 3000;
248 /*! \brief Protect the interface list (of zt_pvt's) */
249 AST_MUTEX_DEFINE_STATIC(iflock);
252 static int ifcount = 0;
255 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
258 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
259 when it's doing something critical. */
260 AST_MUTEX_DEFINE_STATIC(monlock);
262 /*! \brief This is the thread for the monitor which checks for input on the channels
263 which are not currently in use. */
264 static pthread_t monitor_thread = AST_PTHREADT_NULL;
266 static int restart_monitor(void);
268 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);
270 static int zt_sendtext(struct ast_channel *c, const char *text);
272 static void mwi_event_cb(const struct ast_event *event, void *userdata)
274 /* This module does not handle MWI in an event-based manner. However, it
275 * subscribes to MWI for each mailbox that is configured so that the core
276 * knows that we care about it. Then, chan_zap will get the MWI from the
277 * event cache instead of checking the mailbox directly. */
280 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
281 static inline int zt_get_event(int fd)
284 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
289 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
290 static inline int zt_wait_event(int fd)
293 i = ZT_IOMUX_SIGEVENT;
294 if (ioctl(fd, ZT_IOMUX, &i) == -1)
296 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
301 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
302 #define READ_SIZE 160
304 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
305 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
307 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
308 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
309 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
310 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
311 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
315 static int ringt_base = DEFAULT_RINGT;
319 #define LINKSTATE_INALARM (1 << 0)
320 #define LINKSTATE_STARTING (1 << 1)
321 #define LINKSTATE_UP (1 << 2)
322 #define LINKSTATE_DOWN (1 << 3)
324 #define SS7_NAI_DYNAMIC -1
327 pthread_t master; /*!< Thread of master */
331 int linkstate[NUM_DCHANS];
335 LINKSET_STATE_DOWN = 0,
338 char called_nai; /*!< Called Nature of Address Indicator */
339 char calling_nai; /*!< Calling Nature of Address Indicator */
340 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
341 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
342 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
343 char unknownprefix[20]; /*!< for unknown dialplans */
345 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
348 static struct zt_ss7 linksets[NUM_SPANS];
350 static int cur_ss7type = -1;
351 static int cur_linkset = -1;
352 static int cur_pointcode = -1;
353 static int cur_cicbeginswith = -1;
354 static int cur_adjpointcode = -1;
355 static int cur_networkindicator = -1;
356 static int cur_defaultdpc = -1;
357 #endif /* HAVE_SS7 */
361 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
362 #define PRI_CHANNEL(p) ((p) & 0xff)
363 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
364 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
367 pthread_t master; /*!< Thread of master */
368 ast_mutex_t lock; /*!< Mutex */
369 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
370 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
371 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
372 int minunused; /*!< Min # of channels to keep empty */
373 int minidle; /*!< Min # of "idling" calls to keep active */
374 int nodetype; /*!< Node type */
375 int switchtype; /*!< Type of switch to emulate */
376 int nsf; /*!< Network-Specific Facilities */
377 int dialplan; /*!< Dialing plan */
378 int localdialplan; /*!< Local dialing plan */
379 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
380 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
381 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
382 char privateprefix[20]; /*!< for private dialplans */
383 char unknownprefix[20]; /*!< for unknown dialplans */
384 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
385 int trunkgroup; /*!< What our trunkgroup is */
386 int mastertrunkgroup; /*!< What trunk group is our master */
387 int prilogicalspan; /*!< Logical span number within trunk group */
388 int numchans; /*!< Num of channels we represent */
389 int overlapdial; /*!< In overlap dialing mode */
390 int facilityenable; /*!< Enable facility IEs */
391 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
392 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
393 struct pri *pri; /*!< Currently active D-channel */
395 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
400 time_t lastreset; /*!< time when unused channels were last reset */
401 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
402 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
403 struct zt_pvt *crvs; /*!< Member CRV structs */
404 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
408 static struct zt_pri pris[NUM_SPANS];
411 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
413 #define DEFAULT_PRI_DEBUG 0
416 static inline void pri_rel(struct zt_pri *pri)
418 ast_mutex_unlock(&pri->lock);
422 /*! Shut up the compiler */
426 #define SUB_REAL 0 /*!< Active call */
427 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
428 #define SUB_THREEWAY 2 /*!< Three-way call */
430 /* Polarity states */
431 #define POLARITY_IDLE 0
432 #define POLARITY_REV 1
435 static struct zt_distRings drings;
437 struct distRingData {
441 struct ringContextData {
442 char contextData[AST_MAX_CONTEXT];
444 struct zt_distRings {
445 struct distRingData ringnum[3];
446 struct ringContextData ringContext[3];
449 static char *subnames[] = {
455 struct zt_subchannel {
457 struct ast_channel *owner;
459 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
460 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
461 unsigned int needringing:1;
462 unsigned int needbusy:1;
463 unsigned int needcongestion:1;
464 unsigned int needcallerid:1;
465 unsigned int needanswer:1;
466 unsigned int needflash:1;
467 unsigned int needhold:1;
468 unsigned int needunhold:1;
469 unsigned int linear:1;
470 unsigned int inthreeway:1;
474 #define CONF_USER_REAL (1 << 0)
475 #define CONF_USER_THIRDCALL (1 << 1)
479 static struct zt_pvt {
481 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
482 /*!< Up to three channels can be associated with this call */
484 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
485 struct zt_subchannel subs[3]; /*!< Sub-channels */
486 struct zt_confinfo saveconf; /*!< Saved conference info */
488 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
489 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
490 int inconference; /*!< If our real should be in the conference */
492 int sig; /*!< Signalling style */
493 int radio; /*!< radio type */
494 int outsigmod; /*!< Outbound Signalling style (modifier) */
495 int oprmode; /*!< "Operator Services" mode */
496 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
497 float cid_rxgain; /*!< "Gain to apply during caller id */
500 int tonezone; /*!< tone zone for this chan, or -1 for default */
501 struct zt_pvt *next; /*!< Next channel in list */
502 struct zt_pvt *prev; /*!< Prev channel in list */
506 unsigned int answeronpolarityswitch:1;
507 unsigned int busydetect:1;
508 unsigned int callreturn:1;
509 unsigned int callwaiting:1;
510 unsigned int callwaitingcallerid:1;
511 unsigned int cancallforward:1;
512 unsigned int canpark:1;
513 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
514 unsigned int destroy:1;
515 unsigned int didtdd:1; /*!< flag to say its done it once */
516 unsigned int dialednone:1;
517 unsigned int dialing:1;
518 unsigned int digital:1;
520 unsigned int echobreak:1;
521 unsigned int echocanbridged:1;
522 unsigned int echocanon:1;
523 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
524 unsigned int firstradio:1;
525 unsigned int hanguponpolarityswitch:1;
526 unsigned int hardwaredtmf:1;
527 unsigned int hidecallerid:1;
528 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
529 unsigned int ignoredtmf:1;
530 unsigned int immediate:1; /*!< Answer before getting digits? */
531 unsigned int inalarm:1;
532 unsigned int mate:1; /*!< flag to say its in MATE mode */
533 unsigned int outgoing:1;
534 /* unsigned int overlapdial:1; unused and potentially confusing */
535 unsigned int permcallwaiting:1;
536 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
537 unsigned int priindication_oob:1;
538 unsigned int priexclusive:1;
539 unsigned int pulse:1;
540 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
541 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
542 unsigned int threewaycalling:1;
543 unsigned int transfer:1;
544 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
545 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
546 unsigned int usedistinctiveringdetection:1;
547 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
548 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
549 /* Channel state or unavilability flags */
550 unsigned int inservice:1;
551 unsigned int locallyblocked:1;
552 unsigned int remotelyblocked:1;
553 #if defined(HAVE_PRI)
554 unsigned int alerting:1;
555 unsigned int alreadyhungup:1;
556 unsigned int isidlecall:1;
557 unsigned int proceeding:1;
558 unsigned int progress:1;
559 unsigned int resetting:1;
560 unsigned int setup_ack:1;
562 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
563 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
565 struct zt_distRings drings;
567 char context[AST_MAX_CONTEXT];
568 char defcontext[AST_MAX_CONTEXT];
569 char exten[AST_MAX_EXTENSION];
570 char language[MAX_LANGUAGE];
571 char mohinterpret[MAX_MUSICCLASS];
572 char mohsuggest[MAX_MUSICCLASS];
574 char cid_ani[AST_MAX_EXTENSION];
576 char cid_num[AST_MAX_EXTENSION];
577 int cid_ton; /*!< Type Of Number (TON) */
578 char cid_name[AST_MAX_EXTENSION];
579 char lastcid_num[AST_MAX_EXTENSION];
580 char lastcid_name[AST_MAX_EXTENSION];
581 char *origcid_num; /*!< malloced original callerid */
582 char *origcid_name; /*!< malloced original callerid */
583 char callwait_num[AST_MAX_EXTENSION];
584 char callwait_name[AST_MAX_EXTENSION];
585 char rdnis[AST_MAX_EXTENSION];
586 char dnid[AST_MAX_EXTENSION];
589 int confno; /*!< Our conference */
590 int confusers; /*!< Who is using our conference */
591 int propconfno; /*!< Propagated conference number */
592 ast_group_t callgroup;
593 ast_group_t pickupgroup;
594 int channel; /*!< Channel Number or CRV */
595 int span; /*!< Span number */
596 time_t guardtime; /*!< Must wait this much time before using for new call */
597 int cid_signalling; /*!< CID signalling type bell202 or v23 */
598 int cid_start; /*!< CID start indicator, polarity or ring */
599 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
600 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
601 int cidcwexpire; /*!< When to expire our muting for CID/CW */
602 unsigned char *cidspill;
615 int busy_quietlength;
617 struct timeval flashtime; /*!< Last flash-hook time */
619 int cref; /*!< Call reference number */
620 ZT_DIAL_OPERATION dop;
621 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
623 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
624 int amaflags; /*!< AMA Flags */
625 struct tdd_state *tdd; /*!< TDD flag */
626 char call_forward[AST_MAX_EXTENSION];
627 char mailbox[AST_MAX_EXTENSION];
628 struct ast_event_sub *mwi_event_sub;
632 int distinctivering; /*!< Which distinctivering to use */
633 int cidrings; /*!< Which ring to deliver CID on */
634 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
636 int polarityonanswerdelay;
637 struct timeval polaritydelaytv;
638 int sendcalleridafter;
641 struct zt_pvt *bearer;
642 struct zt_pvt *realcall;
651 struct isup_call *ss7call;
653 int cic; /*!< CIC associated with channel */
654 unsigned int dpc; /*!< CIC's DPC */
657 } *iflist = NULL, *ifend = NULL;
659 /*! \brief Channel configuration from zapata.conf .
660 * This struct is used for parsing the [channels] section of zapata.conf.
661 * Generally there is a field here for every possible configuration item.
663 * The state of fields is saved along the parsing and whenever a 'channel'
664 * statement is reached, the current zt_chan_conf is used to configure the
665 * channel (struct zt_pvt)
667 * \see zt_chan_init for the default values.
669 struct zt_chan_conf {
680 char smdi_port[SMDI_MAX_FILENAME_LEN];
683 /** returns a new zt_chan_conf with default values (by-value) */
684 static struct zt_chan_conf zt_chan_conf_default(void) {
685 /* recall that if a field is not included here it is initialized
688 struct zt_chan_conf conf = {
692 .switchtype = PRI_SWITCH_NI2,
693 .dialplan = PRI_NATIONAL_ISDN + 1,
694 .localdialplan = PRI_NATIONAL_ISDN + 1,
700 .internationalprefix = "",
701 .nationalprefix = "",
706 .resetinterval = 3600
711 .called_nai = SS7_NAI_NATIONAL,
712 .calling_nai = SS7_NAI_NATIONAL,
713 .internationalprefix = "",
714 .nationalprefix = "",
715 .subscriberprefix = "",
720 .context = "default",
723 .mohinterpret = "default",
727 .cid_signalling = CID_SIG_BELL,
728 .cid_start = CID_START_RING,
747 .polarityonanswerdelay = 600,
749 .sendcalleridafter = DEFAULT_CIDRINGS
761 .smdi_port = "/dev/ttyS0",
768 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
769 static int zt_digit_begin(struct ast_channel *ast, char digit);
770 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
771 static int zt_sendtext(struct ast_channel *c, const char *text);
772 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
773 static int zt_hangup(struct ast_channel *ast);
774 static int zt_answer(struct ast_channel *ast);
775 static struct ast_frame *zt_read(struct ast_channel *ast);
776 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
777 static struct ast_frame *zt_exception(struct ast_channel *ast);
778 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
779 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
780 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
781 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
783 static const struct ast_channel_tech zap_tech = {
785 .description = tdesc,
786 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
787 .requester = zt_request,
788 .send_digit_begin = zt_digit_begin,
789 .send_digit_end = zt_digit_end,
790 .send_text = zt_sendtext,
797 .exception = zt_exception,
798 .indicate = zt_indicate,
800 .setoption = zt_setoption,
801 .func_channel_read = zt_func_read,
805 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
807 #define GET_CHANNEL(p) ((p)->channel)
810 struct zt_pvt *round_robin[32];
813 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
816 /* Grab the lock first */
818 res = ast_mutex_trylock(&pri->lock);
820 ast_mutex_unlock(&pvt->lock);
821 /* Release the lock and try again */
823 ast_mutex_lock(&pvt->lock);
826 /* Then break the poll */
827 pthread_kill(pri->master, SIGURG);
833 static inline void ss7_rel(struct zt_ss7 *ss7)
835 ast_mutex_unlock(&ss7->lock);
838 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
841 /* Grab the lock first */
843 res = ast_mutex_trylock(&pri->lock);
845 ast_mutex_unlock(&pvt->lock);
846 /* Release the lock and try again */
848 ast_mutex_lock(&pvt->lock);
851 /* Then break the poll */
852 pthread_kill(pri->master, SIGURG);
856 #define NUM_CADENCE_MAX 25
857 static int num_cadence = 4;
858 static int user_has_defined_cadences = 0;
860 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
861 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
862 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
863 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
864 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
867 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
868 * is 1, the second pause is 2 and so on.
871 static int cidrings[NUM_CADENCE_MAX] = {
872 2, /*!< Right after first long ring */
873 4, /*!< Right after long part */
874 3, /*!< After third chirp */
875 2, /*!< Second spell */
878 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
879 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
881 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
882 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
884 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
887 if (p->subs[0].owner == ast)
889 else if (p->subs[1].owner == ast)
891 else if (p->subs[2].owner == ast)
896 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
902 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
904 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
909 ast_mutex_unlock(&pri->lock);
912 if (p->subs[a].owner) {
913 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
914 ast_mutex_unlock(&p->lock);
916 ast_mutex_lock(&p->lock);
918 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
919 ast_mutex_unlock(&p->subs[a].owner->lock);
927 ast_mutex_lock(&pri->lock);
931 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
934 struct zt_pri *pri = (struct zt_pri*) data;
937 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
939 /* We must unlock the PRI to avoid the possibility of a deadlock */
940 #if defined(HAVE_PRI) || defined(HAVE_SS7)
945 ast_mutex_unlock(&pri->lock);
950 ast_mutex_unlock(&ss7->lock);
960 if (ast_mutex_trylock(&p->owner->lock)) {
961 ast_mutex_unlock(&p->lock);
963 ast_mutex_lock(&p->lock);
965 ast_queue_frame(p->owner, f);
966 ast_mutex_unlock(&p->owner->lock);
972 #if defined(HAVE_PRI) || defined(HAVE_SS7)
977 ast_mutex_lock(&pri->lock);
982 ast_mutex_lock(&ss7->lock);
993 static int restore_gains(struct zt_pvt *p);
995 static void swap_subs(struct zt_pvt *p, int a, int b)
999 struct ast_channel *towner;
1001 ast_debug(1, "Swapping %d and %d\n", a, b);
1003 tchan = p->subs[a].chan;
1004 towner = p->subs[a].owner;
1005 tinthreeway = p->subs[a].inthreeway;
1007 p->subs[a].chan = p->subs[b].chan;
1008 p->subs[a].owner = p->subs[b].owner;
1009 p->subs[a].inthreeway = p->subs[b].inthreeway;
1011 p->subs[b].chan = tchan;
1012 p->subs[b].owner = towner;
1013 p->subs[b].inthreeway = tinthreeway;
1015 if (p->subs[a].owner)
1016 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].zfd);
1017 if (p->subs[b].owner)
1018 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].zfd);
1019 wakeup_sub(p, a, NULL);
1020 wakeup_sub(p, b, NULL);
1023 static int zt_open(char *fn)
1031 for (x = 0; x < strlen(fn); x++) {
1032 if (!isdigit(fn[x])) {
1040 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1043 fn = "/dev/zap/channel";
1045 fd = open(fn, O_RDWR | O_NONBLOCK);
1047 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1051 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1055 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1060 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
1064 static void zt_close(int fd)
1070 static int zt_setlinear(int zfd, int linear)
1073 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1080 static int alloc_sub(struct zt_pvt *p, int x)
1084 if (p->subs[x].zfd < 0) {
1085 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1086 if (p->subs[x].zfd > -1) {
1087 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1089 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1090 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1091 bi.numbufs = numbufs;
1092 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1094 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1097 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1098 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1099 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1100 zt_close(p->subs[x].zfd);
1101 p->subs[x].zfd = -1;
1104 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1107 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1110 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1114 static int unalloc_sub(struct zt_pvt *p, int x)
1117 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1120 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1121 if (p->subs[x].zfd > -1) {
1122 zt_close(p->subs[x].zfd);
1124 p->subs[x].zfd = -1;
1125 p->subs[x].linear = 0;
1126 p->subs[x].chan = 0;
1127 p->subs[x].owner = NULL;
1128 p->subs[x].inthreeway = 0;
1129 p->polarity = POLARITY_IDLE;
1130 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1134 static int digit_to_dtmfindex(char digit)
1137 return ZT_TONE_DTMF_BASE + (digit - '0');
1138 else if (digit >= 'A' && digit <= 'D')
1139 return ZT_TONE_DTMF_A + (digit - 'A');
1140 else if (digit >= 'a' && digit <= 'd')
1141 return ZT_TONE_DTMF_A + (digit - 'a');
1142 else if (digit == '*')
1143 return ZT_TONE_DTMF_s;
1144 else if (digit == '#')
1145 return ZT_TONE_DTMF_p;
1150 static int zt_digit_begin(struct ast_channel *chan, char digit)
1156 pvt = chan->tech_pvt;
1158 ast_mutex_lock(&pvt->lock);
1160 index = zt_get_index(chan, pvt, 0);
1162 if ((index != SUB_REAL) || !pvt->owner)
1166 if ((pvt->sig == SIG_PRI) && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1167 if (pvt->setup_ack) {
1168 if (!pri_grab(pvt, pvt->pri)) {
1169 pri_information(pvt->pri->pri, pvt->call, digit);
1172 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1173 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1175 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1176 res = strlen(pvt->dialdest);
1177 pvt->dialdest[res++] = digit;
1178 pvt->dialdest[res] = '\0';
1183 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1186 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1188 ZT_DIAL_OPERATION zo = {
1189 .op = ZT_DIAL_OP_APPEND,
1192 zo.dialstr[0] = 'T';
1193 zo.dialstr[1] = digit;
1194 zo.dialstr[2] = '\0';
1195 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1196 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1200 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1202 pvt->begindigit = digit;
1206 ast_mutex_unlock(&pvt->lock);
1211 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1218 pvt = chan->tech_pvt;
1220 ast_mutex_lock(&pvt->lock);
1222 index = zt_get_index(chan, pvt, 0);
1224 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1228 /* This means that the digit was already sent via PRI signalling */
1229 if (pvt->sig == SIG_PRI && !pvt->begindigit)
1233 if (pvt->begindigit) {
1235 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1236 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1238 pvt->begindigit = 0;
1242 ast_mutex_unlock(&pvt->lock);
1247 static char *events[] = {
1260 "Hook Transition Complete",
1265 "Polarity Reversal",
1273 { ZT_ALARM_RED, "Red Alarm" },
1274 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1275 { ZT_ALARM_BLUE, "Blue Alarm" },
1276 { ZT_ALARM_RECOVER, "Recovering" },
1277 { ZT_ALARM_LOOPBACK, "Loopback" },
1278 { ZT_ALARM_NOTOPEN, "Not Open" },
1279 { ZT_ALARM_NONE, "None" },
1282 static char *alarm2str(int alarm)
1285 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1286 if (alarms[x].alarm & alarm)
1287 return alarms[x].name;
1289 return alarm ? "Unknown Alarm" : "No Alarm";
1292 static char *event2str(int event)
1294 static char buf[256];
1295 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1296 return events[event];
1297 sprintf(buf, "Event %d", event); /* safe */
1302 static char *dialplan2str(int dialplan)
1304 if (dialplan == -1 || dialplan == -2) {
1305 return("Dynamically set dialplan in ISDN");
1307 return (pri_plan2str(dialplan));
1311 static char *zap_sig2str(int sig)
1313 static char buf[256];
1316 return "E & M Immediate";
1318 return "E & M Wink";
1322 return "Feature Group D (DTMF)";
1324 return "Feature Group D (MF)";
1325 case SIG_FEATDMF_TA:
1326 return "Feature Groud D (MF) Tandem Access";
1328 return "Feature Group B (MF)";
1332 return "FGC/CAMA (Dialpulse)";
1333 case SIG_FGC_CAMAMF:
1334 return "FGC/CAMA (MF)";
1336 return "FXS Loopstart";
1338 return "FXS Groundstart";
1340 return "FXS Kewlstart";
1342 return "FXO Loopstart";
1344 return "FXO Groundstart";
1346 return "FXO Kewlstart";
1348 return "PRI Signalling";
1350 return "SS7 Signalling";
1352 return "SF (Tone) Signalling Immediate";
1354 return "SF (Tone) Signalling Wink";
1356 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1357 case SIG_SF_FEATDMF:
1358 return "SF (Tone) Signalling with Feature Group D (MF)";
1360 return "SF (Tone) Signalling with Feature Group B (MF)";
1361 case SIG_GR303FXOKS:
1362 return "GR-303 Signalling with FXOKS";
1363 case SIG_GR303FXSKS:
1364 return "GR-303 Signalling with FXSKS";
1366 return "Pseudo Signalling";
1368 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1373 #define sig2str zap_sig2str
1375 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1377 /* If the conference already exists, and we're already in it
1378 don't bother doing anything */
1381 memset(&zi, 0, sizeof(zi));
1384 if (slavechannel > 0) {
1385 /* If we have only one slave, do a digital mon */
1386 zi.confmode = ZT_CONF_DIGITALMON;
1387 zi.confno = slavechannel;
1390 /* Real-side and pseudo-side both participate in conference */
1391 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1392 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1394 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1395 zi.confno = p->confno;
1397 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1401 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1402 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1405 if (slavechannel < 1) {
1406 p->confno = zi.confno;
1408 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1409 ast_debug(1, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1413 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1415 /* If they're listening to our channel, they're ours */
1416 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1418 /* If they're a talker on our (allocated) conference, they're ours */
1419 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1424 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1427 if (/* Can't delete if there's no zfd */
1429 /* Don't delete from the conference if it's not our conference */
1431 /* Don't delete if we don't think it's conferenced at all (implied) */
1433 memset(&zi, 0, sizeof(zi));
1437 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1438 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1441 ast_debug(1, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1442 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1446 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1450 struct zt_pvt *slave = NULL;
1451 /* Start out optimistic */
1453 /* Update conference state in a stateless fashion */
1454 for (x = 0; x < 3; x++) {
1455 /* Any three-way calling makes slave native mode *definitely* out
1457 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1460 /* If we don't have any 3-way calls, check to see if we have
1461 precisely one slave */
1462 if (useslavenative) {
1463 for (x = 0; x < MAX_SLAVES; x++) {
1466 /* Whoops already have a slave! No
1467 slave native and stop right away */
1472 /* We have one slave so far */
1473 slave = p->slaves[x];
1478 /* If no slave, slave native definitely out */
1481 else if (slave->law != p->law) {
1487 return useslavenative;
1490 static int reset_conf(struct zt_pvt *p)
1493 memset(&zi, 0, sizeof(zi));
1495 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1496 if (p->subs[SUB_REAL].zfd > -1) {
1497 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1498 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1503 static int update_conf(struct zt_pvt *p)
1508 struct zt_pvt *slave = NULL;
1510 useslavenative = isslavenative(p, &slave);
1511 /* Start with the obvious, general stuff */
1512 for (x = 0; x < 3; x++) {
1513 /* Look for three way calls */
1514 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1515 conf_add(p, &p->subs[x], x, 0);
1518 conf_del(p, &p->subs[x], x);
1521 /* If we have a slave, add him to our conference now. or DAX
1522 if this is slave native */
1523 for (x = 0; x < MAX_SLAVES; x++) {
1526 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1528 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1533 /* If we're supposed to be in there, do so now */
1534 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1536 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1538 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1542 /* If we have a master, add ourselves to his conference */
1544 if (isslavenative(p->master, NULL)) {
1545 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1547 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1551 /* Nobody is left (or should be left) in our conference.
1555 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1559 static void zt_enable_ec(struct zt_pvt *p)
1566 ast_debug(1, "Echo cancellation already on\n");
1570 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1573 if (p->echocancel) {
1574 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1576 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1578 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1581 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1583 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1586 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1589 ast_debug(1, "No echo cancellation requested\n");
1592 static void zt_train_ec(struct zt_pvt *p)
1596 if (p && p->echocancel && p->echotraining) {
1597 x = p->echotraining;
1598 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1600 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1602 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1604 ast_debug(1, "No echo training requested\n");
1607 static void zt_disable_ec(struct zt_pvt *p)
1611 if (p->echocancel) {
1613 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1615 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1617 ast_debug(1, "disabled echo cancellation on channel %d\n", p->channel);
1622 static void fill_txgain(struct zt_gains *g, float gain, int law)
1626 float linear_gain = pow(10.0, gain / 20.0);
1630 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1632 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1633 if (k > 32767) k = 32767;
1634 if (k < -32767) k = -32767;
1635 g->txgain[j] = AST_LIN2A(k);
1642 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1644 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1645 if (k > 32767) k = 32767;
1646 if (k < -32767) k = -32767;
1647 g->txgain[j] = AST_LIN2MU(k);
1656 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1660 float linear_gain = pow(10.0, gain / 20.0);
1664 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1666 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1667 if (k > 32767) k = 32767;
1668 if (k < -32767) k = -32767;
1669 g->rxgain[j] = AST_LIN2A(k);
1676 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1678 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1679 if (k > 32767) k = 32767;
1680 if (k < -32767) k = -32767;
1681 g->rxgain[j] = AST_LIN2MU(k);
1690 static int set_actual_txgain(int fd, int chan, float gain, int law)
1695 memset(&g, 0, sizeof(g));
1697 res = ioctl(fd, ZT_GETGAINS, &g);
1699 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1703 fill_txgain(&g, gain, law);
1705 return ioctl(fd, ZT_SETGAINS, &g);
1708 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1713 memset(&g, 0, sizeof(g));
1715 res = ioctl(fd, ZT_GETGAINS, &g);
1717 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1721 fill_rxgain(&g, gain, law);
1723 return ioctl(fd, ZT_SETGAINS, &g);
1726 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1728 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1731 static int bump_gains(struct zt_pvt *p)
1735 /* Bump receive gain by value stored in cid_rxgain */
1736 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1738 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1745 static int restore_gains(struct zt_pvt *p)
1749 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1751 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1758 static inline int zt_set_hook(int fd, int hs)
1763 res = ioctl(fd, ZT_HOOK, &x);
1766 if (errno == EINPROGRESS)
1768 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1774 static inline int zt_confmute(struct zt_pvt *p, int muted)
1778 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1780 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1782 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1784 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1786 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1790 static int save_conference(struct zt_pvt *p)
1792 struct zt_confinfo c;
1794 if (p->saveconf.confmode) {
1795 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1798 p->saveconf.chan = 0;
1799 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1801 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1802 p->saveconf.confmode = 0;
1807 c.confmode = ZT_CONF_NORMAL;
1808 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1810 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1813 ast_debug(1, "Disabled conferencing\n");
1817 static int restore_conference(struct zt_pvt *p)
1820 if (p->saveconf.confmode) {
1821 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1822 p->saveconf.confmode = 0;
1824 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1828 ast_debug(1, "Restored conferencing\n");
1832 static int send_callerid(struct zt_pvt *p);
1834 static int send_cwcidspill(struct zt_pvt *p)
1838 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1840 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1841 /* Make sure we account for the end */
1842 p->cidlen += READ_SIZE * 4;
1845 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1849 static int has_voicemail(struct zt_pvt *p)
1852 struct ast_event *event;
1853 char *mailbox, *context;
1855 mailbox = context = ast_strdupa(p->mailbox);
1856 strsep(&context, "@");
1857 if (ast_strlen_zero(context))
1858 context = "default";
1860 event = ast_event_get_cached(AST_EVENT_MWI,
1861 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1862 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1863 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
1867 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
1868 ast_event_destroy(event);
1870 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
1875 static int send_callerid(struct zt_pvt *p)
1877 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1879 /* Take out of linear mode if necessary */
1880 if (p->subs[SUB_REAL].linear) {
1881 p->subs[SUB_REAL].linear = 0;
1882 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1884 while (p->cidpos < p->cidlen) {
1885 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1887 if (errno == EAGAIN)
1890 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1898 ast_free(p->cidspill);
1900 if (p->callwaitcas) {
1901 /* Wait for CID/CW to expire */
1902 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1904 restore_conference(p);
1908 static int zt_callwait(struct ast_channel *ast)
1910 struct zt_pvt *p = ast->tech_pvt;
1911 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1913 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1914 ast_free(p->cidspill);
1916 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1920 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1921 if (!p->callwaitrings && p->callwaitingcallerid) {
1922 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1924 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1926 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1928 p->cidlen = 2400 + READ_SIZE * 4;
1937 static unsigned char cid_pres2ss7pres(int cid_pres)
1939 return (cid_pres >> 5) & 0x03;
1942 static unsigned char cid_pres2ss7screen(int cid_pres)
1944 return cid_pres & 0x03;
1948 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1950 struct zt_pvt *p = ast->tech_pvt;
1951 int x, res, index,mysig;
1956 char dest[256]; /* must be same length as p->dialdest */
1957 ast_mutex_lock(&p->lock);
1958 ast_copy_string(dest, rdest, sizeof(dest));
1959 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1960 if ((ast->_state == AST_STATE_BUSY)) {
1961 p->subs[SUB_REAL].needbusy = 1;
1962 ast_mutex_unlock(&p->lock);
1965 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1966 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1967 ast_mutex_unlock(&p->lock);
1971 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
1973 /* Special pseudo -- automatically up */
1974 ast_setstate(ast, AST_STATE_UP);
1975 ast_mutex_unlock(&p->lock);
1978 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1979 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1981 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1984 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1987 if (p->outsigmod > -1)
1988 mysig = p->outsigmod;
1994 if (p->owner == ast) {
1995 /* Normal ring, on hook */
1997 /* Don't send audio while on hook, until the call is answered */
1999 if (p->use_callerid) {
2000 /* Generate the Caller-ID spill if desired */
2002 ast_log(LOG_WARNING, "cidspill already exists??\n");
2003 ast_free(p->cidspill);
2006 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2007 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2012 /* Choose proper cadence */
2013 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2014 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
2015 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
2016 p->cidrings = cidrings[p->distinctivering - 1];
2018 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
2019 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
2020 p->cidrings = p->sendcalleridafter;
2023 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2024 c = strchr(dest, '/');
2027 if (c && (strlen(c) < p->stripmsd)) {
2028 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2032 p->dop.op = ZT_DIAL_OP_REPLACE;
2033 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2034 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2036 p->dop.dialstr[0] = '\0';
2039 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2040 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2041 ast_mutex_unlock(&p->lock);
2046 /* Call waiting call */
2047 p->callwaitrings = 0;
2048 if (ast->cid.cid_num)
2049 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2051 p->callwait_num[0] = '\0';
2052 if (ast->cid.cid_name)
2053 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2055 p->callwait_name[0] = '\0';
2056 /* Call waiting tone instead */
2057 if (zt_callwait(ast)) {
2058 ast_mutex_unlock(&p->lock);
2061 /* Make ring-back */
2062 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2063 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2066 n = ast->cid.cid_name;
2067 l = ast->cid.cid_num;
2069 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2071 p->lastcid_num[0] = '\0';
2073 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2075 p->lastcid_name[0] = '\0';
2076 ast_setstate(ast, AST_STATE_RINGING);
2077 index = zt_get_index(ast, p, 0);
2079 p->subs[index].needringing = 1;
2092 case SIG_FGC_CAMAMF:
2097 case SIG_SF_FEATDMF:
2098 case SIG_FEATDMF_TA:
2100 c = strchr(dest, '/');
2105 if (strlen(c) < p->stripmsd) {
2106 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2107 ast_mutex_unlock(&p->lock);
2111 /* Start the trunk, if not GR-303 */
2115 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2117 if (errno != EINPROGRESS) {
2118 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2119 ast_mutex_unlock(&p->lock);
2126 ast_debug(1, "Dialing '%s'\n", c);
2127 p->dop.op = ZT_DIAL_OP_REPLACE;
2133 l = ast->cid.cid_num;
2135 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2137 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2140 l = ast->cid.cid_num;
2142 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2144 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2146 case SIG_FEATDMF_TA:
2148 const char *cic, *ozz;
2150 /* If you have to go through a Tandem Access point you need to use this */
2151 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2154 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2158 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2159 ast_mutex_unlock(&p->lock);
2162 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2163 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2168 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2171 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2173 case SIG_FGC_CAMAMF:
2175 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2179 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2181 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2185 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2186 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2187 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2188 p->echorest[sizeof(p->echorest) - 1] = '\0';
2190 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2194 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2196 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2197 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2198 ast_mutex_unlock(&p->lock);
2202 ast_debug(1, "Deferring dialing...\n");
2205 if (ast_strlen_zero(c))
2207 ast_setstate(ast, AST_STATE_DIALING);
2210 /* Special pseudo -- automatically up*/
2211 ast_setstate(ast, AST_STATE_UP);
2215 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2216 p->dialdest[0] = '\0';
2219 ast_debug(1, "not yet implemented\n");
2220 ast_mutex_unlock(&p->lock);
2225 char ss7_called_nai;
2226 int called_nai_strip;
2227 char ss7_calling_nai;
2228 int calling_nai_strip;
2230 c = strchr(dest, '/');
2236 if (!p->hidecallerid) {
2237 l = ast->cid.cid_num;
2242 if (ss7_grab(p, p->ss7)) {
2243 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2244 ast_mutex_unlock(&p->lock);
2247 p->digital = IS_DIGITAL(ast->transfercapability);
2248 p->ss7call = isup_new_call(p->ss7->ss7);
2252 ast_mutex_unlock(&p->lock);
2253 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2257 called_nai_strip = 0;
2258 ss7_called_nai = p->ss7->called_nai;
2259 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2260 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2261 called_nai_strip = strlen(p->ss7->internationalprefix);
2262 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2263 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2264 called_nai_strip = strlen(p->ss7->nationalprefix);
2265 ss7_called_nai = SS7_NAI_NATIONAL;
2267 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2270 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2272 calling_nai_strip = 0;
2273 ss7_calling_nai = p->ss7->calling_nai;
2274 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2275 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2276 calling_nai_strip = strlen(p->ss7->internationalprefix);
2277 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2278 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2279 calling_nai_strip = strlen(p->ss7->nationalprefix);
2280 ss7_calling_nai = SS7_NAI_NATIONAL;
2282 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2285 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2286 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2287 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2289 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2291 isup_iam(p->ss7->ss7, p->ss7call);
2292 ast_setstate(ast, AST_STATE_DIALING);
2295 #endif /* HAVE_SS7 */
2299 #ifdef SUPPORT_USERUSER
2300 const char *useruser;
2304 int prilocaldialplan;
2308 int redirect_reason;
2310 c = strchr(dest, '/');
2315 if (!p->hidecalleridname)
2316 n = ast->cid.cid_name;
2319 if (!p->hidecallerid) {
2320 l = ast->cid.cid_num;
2321 n = ast->cid.cid_name;
2326 if (strlen(c) < p->stripmsd) {
2327 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2328 ast_mutex_unlock(&p->lock);
2331 if (mysig != SIG_FXSKS) {
2332 p->dop.op = ZT_DIAL_OP_REPLACE;
2333 s = strchr(c + p->stripmsd, 'w');
2336 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2338 p->dop.dialstr[0] = '\0';
2341 p->dop.dialstr[0] = '\0';
2344 if (pri_grab(p, p->pri)) {
2345 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2346 ast_mutex_unlock(&p->lock);
2349 if (!(p->call = pri_new_call(p->pri->pri))) {
2350 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2352 ast_mutex_unlock(&p->lock);
2355 if (!(sr = pri_sr_new())) {
2356 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2358 ast_mutex_unlock(&p->lock);
2360 if (p->bearer || (mysig == SIG_FXSKS)) {
2362 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2363 p->bearer->call = p->call;
2365 ast_debug(1, "I'm being setup with no bearer right now...\n");
2367 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2369 p->digital = IS_DIGITAL(ast->transfercapability);
2370 /* Add support for exclusive override */
2371 if (p->priexclusive)
2374 /* otherwise, traditional behavior */
2375 if (p->pri->nodetype == PRI_NETWORK)
2381 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2382 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2384 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2385 if (p->pri->facilityenable)
2386 pri_facility_enable(p->pri->pri);
2388 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2390 pridialplan = p->pri->dialplan - 1;
2391 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2392 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2393 if (pridialplan == -2) {
2394 dp_strip = strlen(p->pri->internationalprefix);
2396 pridialplan = PRI_INTERNATIONAL_ISDN;
2397 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2398 if (pridialplan == -2) {
2399 dp_strip = strlen(p->pri->nationalprefix);
2401 pridialplan = PRI_NATIONAL_ISDN;
2403 pridialplan = PRI_LOCAL_ISDN;
2406 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2409 prilocaldialplan = p->pri->localdialplan - 1;
2410 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2411 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2412 if (prilocaldialplan == -2) {
2413 ldp_strip = strlen(p->pri->internationalprefix);
2415 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2416 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2417 if (prilocaldialplan == -2) {
2418 ldp_strip = strlen(p->pri->nationalprefix);
2420 prilocaldialplan = PRI_NATIONAL_ISDN;
2422 prilocaldialplan = PRI_LOCAL_ISDN;
2425 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2426 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2427 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2428 if (!strcasecmp(rr_str, "UNKNOWN"))
2429 redirect_reason = 0;
2430 else if (!strcasecmp(rr_str, "BUSY"))
2431 redirect_reason = 1;
2432 else if (!strcasecmp(rr_str, "NO_REPLY"))
2433 redirect_reason = 2;
2434 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2435 redirect_reason = 15;
2437 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2439 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2440 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2442 #ifdef SUPPORT_USERUSER
2443 /* User-user info */
2444 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2447 pri_sr_set_useruser(sr, useruser);
2450 if (pri_setup(p->pri->pri, p->call, sr)) {
2451 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2452 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2454 ast_mutex_unlock(&p->lock);
2459 ast_setstate(ast, AST_STATE_DIALING);
2463 ast_mutex_unlock(&p->lock);
2467 static void destroy_zt_pvt(struct zt_pvt **pvt)
2469 struct zt_pvt *p = *pvt;
2470 /* Remove channel from the list */
2472 p->prev->next = p->next;
2474 p->next->prev = p->prev;
2476 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2477 if (p->mwi_event_sub)
2478 ast_event_unsubscribe(p->mwi_event_sub);
2479 ast_mutex_destroy(&p->lock);
2484 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2494 for (i = 0; i < 3; i++) {
2495 if (cur->subs[i].owner) {
2501 prev->next = cur->next;
2503 prev->next->prev = prev;
2509 iflist->prev = NULL;
2513 if (cur->subs[SUB_REAL].zfd > -1) {
2514 zt_close(cur->subs[SUB_REAL].zfd);
2516 destroy_zt_pvt(&cur);
2520 prev->next = cur->next;
2522 prev->next->prev = prev;
2528 iflist->prev = NULL;
2532 if (cur->subs[SUB_REAL].zfd > -1) {
2533 zt_close(cur->subs[SUB_REAL].zfd);
2535 destroy_zt_pvt(&cur);
2541 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2543 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2545 static char *zap_send_keypad_facility_descrip =
2546 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2547 " IE over the current channel.\n";
2549 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2551 /* Data will be our digit string */
2553 char *digits = (char *) data;
2555 if (ast_strlen_zero(digits)) {
2556 ast_debug(1, "No digit string sent to application!\n");
2560 p = (struct zt_pvt *)chan->tech_pvt;
2563 ast_debug(1, "Unable to find technology private\n");
2567 ast_mutex_lock(&p->lock);
2569 if (!p->pri || !p->call) {
2570 ast_debug(1, "Unable to find pri or call on channel!\n");
2571 ast_mutex_unlock(&p->lock);
2575 if (!pri_grab(p, p->pri)) {
2576 pri_keypad_facility(p->pri->pri, p->call, digits);
2579 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2580 ast_mutex_unlock(&p->lock);
2584 ast_mutex_unlock(&p->lock);
2589 static int pri_is_up(struct zt_pri *pri)
2592 for (x = 0; x < NUM_DCHANS; x++) {
2593 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2599 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2601 bearer->owner = &inuse;
2602 bearer->realcall = crv;
2603 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2604 if (crv->subs[SUB_REAL].owner)
2605 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].zfd);
2606 crv->bearer = bearer;
2607 crv->call = bearer->call;
2612 static char *pri_order(int level)
2622 return "Quaternary";
2628 /* Returns fd of the active dchan */
2629 static int pri_active_dchan_fd(struct zt_pri *pri)
2633 for (x = 0; x < NUM_DCHANS; x++) {
2634 if ((pri->dchans[x] == pri->pri))
2641 static int pri_find_dchan(struct zt_pri *pri)
2648 for (x = 0; x < NUM_DCHANS; x++) {
2649 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2651 if (pri->dchans[x] == old) {
2657 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2658 pri->dchannels[newslot]);
2660 if (old && (oldslot != newslot))
2661 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2662 pri->dchannels[oldslot], pri->dchannels[newslot]);
2663 pri->pri = pri->dchans[newslot];
2668 static int zt_hangup(struct ast_channel *ast)
2672 /*static int restore_gains(struct zt_pvt *p);*/
2673 struct zt_pvt *p = ast->tech_pvt;
2674 struct zt_pvt *tmp = NULL;
2675 struct zt_pvt *prev = NULL;
2678 ast_debug(1, "zt_hangup(%s)\n", ast->name);
2679 if (!ast->tech_pvt) {
2680 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2684 ast_mutex_lock(&p->lock);
2686 index = zt_get_index(ast, p, 1);
2688 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2690 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2696 if (p->origcid_num) {
2697 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2698 ast_free(p->origcid_num);
2699 p->origcid_num = NULL;
2701 if (p->origcid_name) {
2702 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2703 ast_free(p->origcid_name);
2704 p->origcid_name = NULL;
2707 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2711 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2712 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2716 /* Real channel, do some fixup */
2717 p->subs[index].owner = NULL;
2718 p->subs[index].needanswer = 0;
2719 p->subs[index].needflash = 0;
2720 p->subs[index].needringing = 0;
2721 p->subs[index].needbusy = 0;
2722 p->subs[index].needcongestion = 0;
2723 p->subs[index].linear = 0;
2724 p->subs[index].needcallerid = 0;
2725 p->polarity = POLARITY_IDLE;
2726 zt_setlinear(p->subs[index].zfd, 0);
2727 if (index == SUB_REAL) {
2728 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2729 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
2730 if (p->subs[SUB_CALLWAIT].inthreeway) {
2731 /* We had flipped over to answer a callwait and now it's gone */
2732 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
2733 /* Move to the call-wait, but un-own us until they flip back. */
2734 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2735 unalloc_sub(p, SUB_CALLWAIT);
2738 /* The three way hung up, but we still have a call wait */
2739 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2740 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2741 unalloc_sub(p, SUB_THREEWAY);
2742 if (p->subs[SUB_REAL].inthreeway) {
2743 /* This was part of a three way call. Immediately make way for
2745 ast_debug(1, "Call was complete, setting owner to former third call\n");
2746 p->owner = p->subs[SUB_REAL].owner;
2748 /* This call hasn't been completed yet... Set owner to NULL */
2749 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
2752 p->subs[SUB_REAL].inthreeway = 0;
2754 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2755 /* Move to the call-wait and switch back to them. */
2756 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2757 unalloc_sub(p, SUB_CALLWAIT);
2758 p->owner = p->subs[SUB_REAL].owner;
2759 if (p->owner->_state != AST_STATE_UP)
2760 p->subs[SUB_REAL].needanswer = 1;
2761 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2762 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
2763 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2764 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2765 unalloc_sub(p, SUB_THREEWAY);
2766 if (p->subs[SUB_REAL].inthreeway) {
2767 /* This was part of a three way call. Immediately make way for
2769 ast_debug(1, "Call was complete, setting owner to former third call\n");
2770 p->owner = p->subs[SUB_REAL].owner;
2772 /* This call hasn't been completed yet... Set owner to NULL */
2773 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
2776 p->subs[SUB_REAL].inthreeway = 0;
2778 } else if (index == SUB_CALLWAIT) {
2779 /* Ditch the holding callwait call, and immediately make it availabe */
2780 if (p->subs[SUB_CALLWAIT].inthreeway) {
2781 /* This is actually part of a three way, placed on hold. Place the third part
2782 on music on hold now */
2783 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2784 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2785 S_OR(p->mohsuggest, NULL),
2786 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2788 p->subs[SUB_THREEWAY].inthreeway = 0;
2789 /* Make it the call wait now */
2790 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2791 unalloc_sub(p, SUB_THREEWAY);
2793 unalloc_sub(p, SUB_CALLWAIT);
2794 } else if (index == SUB_THREEWAY) {
2795 if (p->subs[SUB_CALLWAIT].inthreeway) {
2796 /* The other party of the three way call is currently in a call-wait state.
2797 Start music on hold for them, and take the main guy out of the third call */
2798 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
2799 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
2800 S_OR(p->mohsuggest, NULL),
2801 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2803 p->subs[SUB_CALLWAIT].inthreeway = 0;
2805 p->subs[SUB_REAL].inthreeway = 0;
2806 /* If this was part of a three way call index, let us make
2807 another three way call */
2808 unalloc_sub(p, SUB_THREEWAY);
2810 /* This wasn't any sort of call, but how are we an index? */
2811 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2815 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2818 p->distinctivering = 0;
2819 p->confirmanswer = 0;
2825 p->onhooktime = time(NULL);
2833 ast_dsp_free(p->dsp);
2837 law = ZT_LAW_DEFAULT;
2838 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2840 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2841 /* Perform low level hangup if no owner left */
2845 if (!ss7_grab(p, p->ss7)) {
2846 if (!p->alreadyhungup) {
2847 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
2848 int icause = ast->hangupcause ? ast->hangupcause : -1;
2852 icause = atoi(cause);
2854 isup_rel(p->ss7->ss7, p->ss7call, icause);
2856 p->alreadyhungup = 1;
2858 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
2860 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
2868 #ifdef SUPPORT_USERUSER
2869 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2872 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2873 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2874 if (!pri_grab(p, p->pri)) {
2875 if (p->alreadyhungup) {
2876 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
2878 #ifdef SUPPORT_USERUSER
2879 pri_call_set_useruser(p->call, useruser);
2882 pri_hangup(p->pri->pri, p->call, -1);
2885 p->bearer->call = NULL;
2887 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2888 int icause = ast->hangupcause ? ast->hangupcause : -1;
2889 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2891 #ifdef SUPPORT_USERUSER
2892 pri_call_set_useruser(p->call, useruser);
2895 p->alreadyhungup = 1;
2897 p->bearer->alreadyhungup = 1;
2900 icause = atoi(cause);
2902 pri_hangup(p->pri->pri, p->call, icause);
2905 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2908 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2913 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2919 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7)))
2920 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2922 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2928 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2931 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2933 /* If they're off hook, try playing congestion */
2934 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
2935 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2937 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2943 /* Make sure we're not made available for at least two seconds assuming
2944 we were actually used for an inbound or outbound call. */
2945 if (ast->_state != AST_STATE_RESERVED) {
2946 time(&p->guardtime);
2951 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2954 ast_free(p->cidspill);
2958 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2959 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2963 p->callwaiting = p->permcallwaiting;
2964 p->hidecallerid = p->permhidecallerid;
2969 /* Restore data mode */
2970 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2972 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2976 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
2977 /* Free up the bearer channel as well, and
2978 don't use its file descriptor anymore */
2979 update_conf(p->bearer);
2980 reset_conf(p->bearer);
2981 p->bearer->owner = NULL;
2982 p->bearer->realcall = NULL;
2984 p->subs[SUB_REAL].zfd = -1;
2991 p->callwaitingrepeat = 0;
2994 ast->tech_pvt = NULL;
2995 ast_mutex_unlock(&p->lock);
2996 ast_module_unref(ast_module_info->self);
2997 ast_verb(3, "Hungup '%s'\n", ast->name);
2999 ast_mutex_lock(&iflock);
3005 destroy_channel(prev, tmp, 0);
3013 ast_mutex_unlock(&iflock);
3017 static int zt_answer(struct ast_channel *ast)
3019 struct zt_pvt *p = ast->tech_pvt;
3022 int oldstate = ast->_state;
3023 ast_setstate(ast, AST_STATE_UP);
3024 ast_mutex_lock(&p->lock);
3025 index = zt_get_index(ast, p, 0);
3028 /* nothing to do if a radio channel */
3029 if ((p->radio || (p->oprmode < 0))) {
3030 ast_mutex_unlock(&p->lock);
3044 case SIG_FEATDMF_TA:
3047 case SIG_FGC_CAMAMF:
3052 case SIG_SF_FEATDMF:
3057 /* Pick up the line */
3058 ast_debug(1, "Took %s off hook\n", ast->name);
3059 if (p->hanguponpolarityswitch) {
3060 p->polaritydelaytv = ast_tvnow();
3062 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
3063 tone_zone_play_tone(p->subs[index].zfd, -1);
3065 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
3066 if (oldstate == AST_STATE_RINGING) {
3067 ast_debug(1, "Finally swapping real and threeway\n");
3068 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3069 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3070 p->owner = p->subs[SUB_REAL].owner;
3073 if (p->sig & __ZT_SIG_FXS) {
3080 /* Send a pri acknowledge */
3081 if (!pri_grab(p, p->pri)) {
3083 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3086 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3093 if (!ss7_grab(p, p->ss7)) {
3095 res = isup_anm(p->ss7->ss7, p->ss7call);
3098 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3104 ast_mutex_unlock(&p->lock);
3107 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3110 ast_mutex_unlock(&p->lock);
3114 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3120 struct zt_pvt *p = chan->tech_pvt, *pp;
3121 struct oprmode *oprmode;
3124 /* all supported options require data */
3125 if (!data || (datalen < 1)) {
3131 case AST_OPTION_TXGAIN:
3132 scp = (signed char *) data;
3133 index = zt_get_index(chan, p, 0);
3135 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
3138 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
3139 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
3140 case AST_OPTION_RXGAIN:
3141 scp = (signed char *) data;
3142 index = zt_get_index(chan, p, 0);
3144 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
3147 ast_debug(1, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
3148 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
3149 case AST_OPTION_TONE_VERIFY:
3155 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
3156 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
3159 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
3160 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
3163 ast_debug(1, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
3164 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
3168 case AST_OPTION_TDD:
3169 /* turn on or off TDD */
3172 if (!*cp) { /* turn it off */
3173 ast_debug(1, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
3179 ast_debug(1, "Set option TDD MODE, value: %s(%d) on %s\n",
3180 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
3182 /* otherwise, turn it on */
3183 if (!p->didtdd) { /* if havent done it yet */
3184 unsigned char mybuf[41000], *buf;
3185 int size, res, fd, len;
3186 struct pollfd fds[1];
3189 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
3190 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
3192 index = zt_get_index(chan, p, 0);
3194 ast_log(LOG_WARNING, "No index in TDD?\n");
3197 fd = p->subs[index].zfd;
3199 if (ast_check_hangup(chan))
3202 if (size > READ_SIZE)
3205 fds[0].events = POLLPRI | POLLOUT;
3207 res = poll(fds, 1, -1);
3209 ast_debug(1, "poll (for write) ret. 0 on channel %d\n", p->channel);
3212 /* if got exception */
3213 if (fds[0].revents & POLLPRI)
3215 if (!(fds[0].revents & POLLOUT)) {
3216 ast_debug(1, "write fd not ready on channel %d\n", p->channel);
3219 res = write(fd, buf, size);
3221 if (res == -1) return -1;
3222 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
3228 p->didtdd = 1; /* set to have done it now */
3230 if (*cp == 2) { /* Mate mode */
3237 if (!p->tdd) { /* if we dont have one yet */
3238 p->tdd = tdd_new(); /* allocate one */
3241 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
3245 ast_debug(1, "Set option RELAX DTMF, value: %s(%d) on %s\n",
3246 *cp ? "ON" : "OFF", (int) *cp, chan->name);
3247 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
3249 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
3252 ast_debug(1, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
3256 ast_debug(1, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
3259 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
3260 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
3262 case AST_OPTION_OPRMODE: /* Operator services mode */
3263 oprmode = (struct oprmode *) data;
3264 pp = oprmode->peer->tech_pvt;
3265 p->oprmode = pp->oprmode = 0;
3269 /* setup modes, if any */
3272 pp->oprmode = oprmode->mode;
3273 p->oprmode = -oprmode->mode;
3275 ast_debug(1, "Set Operator Services mode, value: %d on %s/%s\n",
3276 oprmode->mode, chan->name,oprmode->peer->name);
3278 case AST_OPTION_ECHOCAN:
3281 ast_debug(1, "Enabling echo cancelation on %s\n", chan->name);
3284 ast_debug(1, "Disabling echo cancelation on %s\n", chan->name);
3294 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
3296 struct zt_pvt *p = chan->tech_pvt;
3298 if (!strcasecmp(data, "rxgain")) {
3299 ast_mutex_lock(&p->lock);
3300 snprintf(buf, len, "%f", p->rxgain);
3301 ast_mutex_unlock(&p->lock);
3302 } else if (!strcasecmp(data, "txgain")) {
3303 ast_mutex_lock(&p->lock);
3304 snprintf(buf, len, "%f", p->txgain);
3305 ast_mutex_unlock(&p->lock);
3307 ast_copy_string(buf, "", len);
3313 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
3315 /* Unlink a specific slave or all slaves/masters from a given master */
3321 ast_mutex_lock(&master->lock);
3323 while (ast_mutex_trylock(&slave->lock)) {
3324 ast_mutex_unlock(&master->lock);
3326 ast_mutex_lock(&master->lock);
3331 for (x = 0; x < MAX_SLAVES; x++) {
3332 if (master->slaves[x]) {
3333 if (!slave || (master->slaves[x] == slave)) {
3334 /* Take slave out of the conference */
3335 ast_debug(1, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
3336 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
3337 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
3338 master->slaves[x]->master = NULL;
3339 master->slaves[x] = NULL;
3344 master->inconference = 0;
3347 if (master->master) {
3348 /* Take master out of the conference */
3349 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
3350 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
3352 for (x = 0; x < MAX_SLAVES; x++) {
3353 if (master->master->slaves[x] == master)
3354 master->master->slaves[x] = NULL;
3355 else if (master->master->slaves[x])
3359 master->master->inconference = 0;
3361 master->master = NULL;
3363 update_conf(master);
3366 ast_mutex_unlock(&slave->lock);
3367 ast_mutex_unlock(&master->lock);
3371 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
3373 if (!slave || !master) {
3374 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
3377 for (x = 0; x < MAX_SLAVES; x++) {
3378 if (!master->slaves[x]) {
3379 master->slaves[x] = slave;
3383 if (x >= MAX_SLAVES) {
3384 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
3385 master->slaves[MAX_SLAVES - 1] = slave;
3388 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
3389 slave->master = master;
3391 ast_debug(1, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
3394 static void disable_dtmf_detect(struct zt_pvt *p)
3396 #ifdef ZT_TONEDETECT
3402 #ifdef ZT_TONEDETECT
3404 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3406 if (!p->hardwaredtmf && p->dsp) {
3407 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
3408 ast_dsp_set_features(p->dsp, p->dsp_features);
3412 static void enable_dtmf_detect(struct zt_pvt *p)
3414 #ifdef ZT_TONEDETECT
3418 if (p->channel == CHAN_PSEUDO)
3423 #ifdef ZT_TONEDETECT
3424 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
3425 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3427 if (!p->hardwaredtmf && p->dsp) {
3428 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
3429 ast_dsp_set_features(p->dsp, p->dsp_features);
3433 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)
3435 struct ast_channel *who;
3436 struct zt_pvt *p0, *p1, *op0, *op1;
3437 struct zt_pvt *master = NULL, *slave = NULL;
3438 struct ast_frame *f;