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>
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
55 #include <sys/signal.h>
59 #if !defined(SOLARIS) && !defined(__FreeBSD__)
63 #include <sys/ioctl.h>
65 #include <linux/zaptel.h>
68 #endif /* __linux__ */
75 #ifndef PRI_KEYPAD_FACILITY_TX
76 #error "You need newer libpri"
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 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
112 /*! Global jitterbuffer configuration - by default, jb is disabled */
113 static struct ast_jb_conf default_jbconf =
117 .resync_threshold = -1,
120 static struct ast_jb_conf global_jbconf;
122 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_LIBPRI) && !defined(ZT_SIG_HARDHDLC))
123 #error "Your zaptel is too old. Please update"
126 #ifndef ZT_TONEDETECT
127 /* Work around older code with no tone detect */
128 #define ZT_EVENT_DTMFDOWN 0
129 #define ZT_EVENT_DTMFUP 0
132 /* define this to send PRI user-user information elements */
133 #undef SUPPORT_USERUSER
136 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
137 * the user hangs up to reset the state machine so ring works properly.
138 * This is used to be able to support kewlstart by putting the zhone in
139 * groundstart mode since their forward disconnect supervision is entirely
140 * broken even though their documentation says it isn't and their support
141 * is entirely unwilling to provide any assistance with their channel banks
142 * even though their web site says they support their products for life.
144 /* #define ZHONE_HACK */
147 * Define if you want to check the hook state for an FXO (FXS signalled) interface
148 * before dialing on it. Certain FXO interfaces always think they're out of
149 * service with this method however.
151 /* #define ZAP_CHECK_HOOKSTATE */
153 /*! \brief Typically, how many rings before we should send Caller*ID */
154 #define DEFAULT_CIDRINGS 1
156 #define CHANNEL_PSEUDO -12
158 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
160 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
161 #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))
163 static const char tdesc[] = "Zapata Telephony Driver"
169 static const char config[] = "zapata.conf";
171 #define SIG_EM ZT_SIG_EM
172 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
173 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
174 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
175 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
176 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
177 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
178 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
179 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
180 #define SIG_FXSLS ZT_SIG_FXSLS
181 #define SIG_FXSGS ZT_SIG_FXSGS
182 #define SIG_FXSKS ZT_SIG_FXSKS
183 #define SIG_FXOLS ZT_SIG_FXOLS
184 #define SIG_FXOGS ZT_SIG_FXOGS
185 #define SIG_FXOKS ZT_SIG_FXOKS
186 #define SIG_PRI ZT_SIG_CLEAR
187 #define SIG_SF ZT_SIG_SF
188 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
189 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
190 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
191 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
192 #define SIG_EM_E1 ZT_SIG_EM_E1
193 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
194 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
197 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
198 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
200 #define CHAN_PSEUDO -2
202 #define DCHAN_PROVISIONED (1 << 0)
203 #define DCHAN_NOTINALARM (1 << 1)
204 #define DCHAN_UP (1 << 2)
206 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
208 static char context[AST_MAX_CONTEXT] = "default";
209 static char cid_num[256] = "";
210 static char cid_name[256] = "";
211 static char defaultcic[64] = "";
212 static char defaultozz[64] = "";
214 static char language[MAX_LANGUAGE] = "";
215 static char musicclass[MAX_MUSICCLASS] = "";
216 static char progzone[10] = "";
218 static int usedistinctiveringdetection = 0;
219 static int distinctiveringaftercid = 0;
221 static int transfertobusy = 1;
223 static int use_callerid = 1;
224 static int cid_signalling = CID_SIG_BELL;
225 static int cid_start = CID_START_RING;
226 static int zaptrcallerid = 0;
227 static int cur_signalling = -1;
228 static int cur_outsignalling = -1;
230 static ast_group_t cur_group = 0;
231 static ast_group_t cur_callergroup = 0;
232 static ast_group_t cur_pickupgroup = 0;
233 static int relaxdtmf = 0;
235 static int immediate = 0;
237 static int stripmsd = 0;
239 static int callwaiting = 0;
241 static int callwaitingcallerid = 0;
243 static int hidecallerid = 0;
245 static int restrictcid = 0;
247 static int use_callingpres = 0;
249 static int callreturn = 0;
251 static int threewaycalling = 0;
253 static int transfer = 0;
255 static int canpark = 0;
257 static int cancallforward = 0;
259 static float rxgain = 0.0;
261 static float txgain = 0.0;
263 static int tonezone = -1;
265 static int echocancel;
267 static int echotraining;
271 static int echocanbridged = 0;
273 static int busydetect = 0;
275 static int busycount = 3;
276 static int busy_tonelength = 0;
277 static int busy_quietlength = 0;
279 static int callprogress = 0;
281 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
283 static char mailbox[AST_MAX_EXTENSION];
285 static int amaflags = 0;
288 static int use_smdi = 0;
289 static char smdi_port[SMDI_MAX_FILENAME_LEN] = "/dev/ttyS0";
290 static int numbufs = 4;
292 static int cur_prewink = -1;
293 static int cur_preflash = -1;
294 static int cur_wink = -1;
295 static int cur_flash = -1;
296 static int cur_start = -1;
297 static int cur_rxwink = -1;
298 static int cur_rxflash = -1;
299 static int cur_debounce = -1;
300 static int cur_priexclusive = 0;
302 static int priindication_oob = 0;
305 static int minunused = 2;
306 static int minidle = 0;
307 static char idleext[AST_MAX_EXTENSION];
308 static char idledial[AST_MAX_EXTENSION];
309 static int overlapdial = 0;
310 static int facilityenable = 0;
311 static char internationalprefix[10] = "";
312 static char nationalprefix[10] = "";
313 static char localprefix[20] = "";
314 static char privateprefix[20] = "";
315 static char unknownprefix[20] = "";
316 static long resetinterval = 3600; /*!< How often (in seconds) to reset unused channels. Default 1 hour. */
317 static struct ast_channel inuse;
318 #ifdef PRI_GETSET_TIMERS
319 static int pritimers[PRI_MAX_TIMERS];
321 static int pridebugfd = -1;
322 static char pridebugfilename[1024] = "";
325 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
326 static int firstdigittimeout = 16000;
328 /*! \brief How long to wait for following digits (FXO logic) */
329 static int gendigittimeout = 8000;
331 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
332 static int matchdigittimeout = 3000;
334 static int usecnt = 0;
335 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
337 /*! \brief Protect the interface list (of zt_pvt's) */
338 AST_MUTEX_DEFINE_STATIC(iflock);
341 static int ifcount = 0;
344 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
347 /*! \brief Whether we answer on a Polarity Switch event */
348 static int answeronpolarityswitch = 0;
350 /*! \brief Whether we hang up on a Polarity Switch event */
351 static int hanguponpolarityswitch = 0;
353 /*! \brief How long (ms) to ignore Polarity Switch events after we answer a call */
354 static int polarityonanswerdelay = 600;
356 /*! \brief When to send the CallerID signals (rings) */
357 static int sendcalleridafter = DEFAULT_CIDRINGS;
359 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
360 when it's doing something critical. */
361 AST_MUTEX_DEFINE_STATIC(monlock);
363 /*! \brief This is the thread for the monitor which checks for input on the channels
364 which are not currently in use. */
365 static pthread_t monitor_thread = AST_PTHREADT_NULL;
367 static int restart_monitor(void);
369 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);
371 static int zt_sendtext(struct ast_channel *c, const char *text);
373 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
374 static inline int zt_get_event(int fd)
377 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
382 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
383 static inline int zt_wait_event(int fd)
386 i = ZT_IOMUX_SIGEVENT;
387 if (ioctl(fd, ZT_IOMUX, &i) == -1)
389 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
394 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
395 #define READ_SIZE 160
397 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
398 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
400 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
401 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
402 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
403 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
404 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
408 static int ringt_base = DEFAULT_RINGT;
412 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
413 #define PRI_CHANNEL(p) ((p) & 0xff)
414 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
415 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
418 pthread_t master; /*!< Thread of master */
419 ast_mutex_t lock; /*!< Mutex */
420 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
421 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
422 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
423 int minunused; /*!< Min # of channels to keep empty */
424 int minidle; /*!< Min # of "idling" calls to keep active */
425 int nodetype; /*!< Node type */
426 int switchtype; /*!< Type of switch to emulate */
427 int nsf; /*!< Network-Specific Facilities */
428 int dialplan; /*!< Dialing plan */
429 int localdialplan; /*!< Local dialing plan */
430 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
431 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
432 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
433 char privateprefix[20]; /*!< for private dialplans */
434 char unknownprefix[20]; /*!< for unknown dialplans */
435 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
436 int trunkgroup; /*!< What our trunkgroup is */
437 int mastertrunkgroup; /*!< What trunk group is our master */
438 int prilogicalspan; /*!< Logical span number within trunk group */
439 int numchans; /*!< Num of channels we represent */
440 int overlapdial; /*!< In overlap dialing mode */
441 int facilityenable; /*!< Enable facility IEs */
442 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
443 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
444 struct pri *pri; /*!< Currently active D-channel */
446 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
451 time_t lastreset; /*!< time when unused channels were last reset */
452 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
453 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
454 struct zt_pvt *crvs; /*!< Member CRV structs */
455 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
459 static struct zt_pri pris[NUM_SPANS];
461 static int pritype = PRI_CPE;
464 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
466 #define DEFAULT_PRI_DEBUG 0
469 static inline void pri_rel(struct zt_pri *pri)
471 ast_mutex_unlock(&pri->lock);
474 static int switchtype = PRI_SWITCH_NI2;
475 static int nsf = PRI_NSF_NONE;
476 static int dialplan = PRI_NATIONAL_ISDN + 1;
477 static int localdialplan = PRI_NATIONAL_ISDN + 1;
480 /*! Shut up the compiler */
484 #define SUB_REAL 0 /*!< Active call */
485 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
486 #define SUB_THREEWAY 2 /*!< Three-way call */
488 /* Polarity states */
489 #define POLARITY_IDLE 0
490 #define POLARITY_REV 1
493 static struct zt_distRings drings;
495 struct distRingData {
498 struct ringContextData {
499 char contextData[AST_MAX_CONTEXT];
501 struct zt_distRings {
502 struct distRingData ringnum[3];
503 struct ringContextData ringContext[3];
506 static char *subnames[] = {
512 struct zt_subchannel {
514 struct ast_channel *owner;
516 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
517 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
518 unsigned int needringing:1;
519 unsigned int needbusy:1;
520 unsigned int needcongestion:1;
521 unsigned int needcallerid:1;
522 unsigned int needanswer:1;
523 unsigned int needflash:1;
524 unsigned int linear:1;
525 unsigned int inthreeway:1;
529 #define CONF_USER_REAL (1 << 0)
530 #define CONF_USER_THIRDCALL (1 << 1)
534 static struct zt_pvt {
536 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
537 /*!< Up to three channels can be associated with this call */
539 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
540 struct zt_subchannel subs[3]; /*!< Sub-channels */
541 struct zt_confinfo saveconf; /*!< Saved conference info */
543 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
544 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
545 int inconference; /*!< If our real should be in the conference */
547 int sig; /*!< Signalling style */
548 int radio; /*!< radio type */
549 int outsigmod; /*!< Outbound Signalling style (modifier) */
550 int oprmode; /*!< "Operator Services" mode */
551 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
554 int tonezone; /*!< tone zone for this chan, or -1 for default */
555 struct zt_pvt *next; /*!< Next channel in list */
556 struct zt_pvt *prev; /*!< Prev channel in list */
560 unsigned int answeronpolarityswitch:1;
561 unsigned int busydetect:1;
562 unsigned int callreturn:1;
563 unsigned int callwaiting:1;
564 unsigned int callwaitingcallerid:1;
565 unsigned int cancallforward:1;
566 unsigned int canpark:1;
567 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
568 unsigned int destroy:1;
569 unsigned int didtdd:1; /*!< flag to say its done it once */
570 unsigned int dialednone:1;
571 unsigned int dialing:1;
572 unsigned int digital:1;
574 unsigned int echobreak:1;
575 unsigned int echocanbridged:1;
576 unsigned int echocanon:1;
577 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
578 unsigned int firstradio:1;
579 unsigned int hanguponpolarityswitch:1;
580 unsigned int hardwaredtmf:1;
581 unsigned int hidecallerid;
582 unsigned int ignoredtmf:1;
583 unsigned int immediate:1; /*!< Answer before getting digits? */
584 unsigned int inalarm:1;
585 unsigned int mate:1; /*!< flag to say its in MATE mode */
586 unsigned int outgoing:1;
587 unsigned int overlapdial:1;
588 unsigned int permcallwaiting:1;
589 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
590 unsigned int priindication_oob:1;
591 unsigned int priexclusive:1;
592 unsigned int pulse:1;
593 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
594 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
595 unsigned int threewaycalling:1;
596 unsigned int transfer:1;
597 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
598 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
599 unsigned int usedistinctiveringdetection:1;
600 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
601 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
602 #if defined(HAVE_LIBPRI)
603 unsigned int alerting:1;
604 unsigned int alreadyhungup:1;
605 unsigned int isidlecall:1;
606 unsigned int proceeding:1;
607 unsigned int progress:1;
608 unsigned int resetting:1;
609 unsigned int setup_ack:1;
611 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
612 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
614 struct zt_distRings drings;
616 char context[AST_MAX_CONTEXT];
617 char defcontext[AST_MAX_CONTEXT];
618 char exten[AST_MAX_EXTENSION];
619 char language[MAX_LANGUAGE];
620 char musicclass[MAX_MUSICCLASS];
622 char cid_ani[AST_MAX_EXTENSION];
624 char cid_num[AST_MAX_EXTENSION];
625 int cid_ton; /*!< Type Of Number (TON) */
626 char cid_name[AST_MAX_EXTENSION];
627 char lastcid_num[AST_MAX_EXTENSION];
628 char lastcid_name[AST_MAX_EXTENSION];
629 char *origcid_num; /*!< malloced original callerid */
630 char *origcid_name; /*!< malloced original callerid */
631 char callwait_num[AST_MAX_EXTENSION];
632 char callwait_name[AST_MAX_EXTENSION];
633 char rdnis[AST_MAX_EXTENSION];
634 char dnid[AST_MAX_EXTENSION];
637 int confno; /*!< Our conference */
638 int confusers; /*!< Who is using our conference */
639 int propconfno; /*!< Propagated conference number */
640 ast_group_t callgroup;
641 ast_group_t pickupgroup;
642 int channel; /*!< Channel Number or CRV */
643 int span; /*!< Span number */
644 time_t guardtime; /*!< Must wait this much time before using for new call */
645 int cid_signalling; /*!< CID signalling type bell202 or v23 */
646 int cid_start; /*!< CID start indicator, polarity or ring */
647 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
648 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
649 int cidcwexpire; /*!< When to expire our muting for CID/CW */
650 unsigned char *cidspill;
663 int busy_quietlength;
665 struct timeval flashtime; /*!< Last flash-hook time */
667 int cref; /*!< Call reference number */
668 ZT_DIAL_OPERATION dop;
669 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
671 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
672 int amaflags; /*!< AMA Flags */
673 struct tdd_state *tdd; /*!< TDD flag */
674 char call_forward[AST_MAX_EXTENSION];
675 char mailbox[AST_MAX_EXTENSION];
679 int distinctivering; /*!< Which distinctivering to use */
680 int cidrings; /*!< Which ring to deliver CID on */
681 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
683 int polarityonanswerdelay;
684 struct timeval polaritydelaytv;
685 int sendcalleridafter;
688 struct zt_pvt *bearer;
689 struct zt_pvt *realcall;
696 } *iflist = NULL, *ifend = NULL;
698 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
699 static int zt_digit(struct ast_channel *ast, char digit);
700 static int zt_sendtext(struct ast_channel *c, const char *text);
701 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
702 static int zt_hangup(struct ast_channel *ast);
703 static int zt_answer(struct ast_channel *ast);
704 struct ast_frame *zt_read(struct ast_channel *ast);
705 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
706 struct ast_frame *zt_exception(struct ast_channel *ast);
707 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
708 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
709 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
710 static int zt_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
712 static const struct ast_channel_tech zap_tech = {
714 .description = tdesc,
715 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
716 .requester = zt_request,
717 .send_digit = zt_digit,
718 .send_text = zt_sendtext,
725 .exception = zt_exception,
726 .indicate = zt_indicate,
728 .setoption = zt_setoption,
729 .func_channel_read = zt_func_read,
733 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
735 #define GET_CHANNEL(p) ((p)->channel)
738 struct zt_pvt *round_robin[32];
741 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
744 /* Grab the lock first */
746 res = ast_mutex_trylock(&pri->lock);
748 ast_mutex_unlock(&pvt->lock);
749 /* Release the lock and try again */
751 ast_mutex_lock(&pvt->lock);
754 /* Then break the poll */
755 pthread_kill(pri->master, SIGURG);
760 #define NUM_CADENCE_MAX 25
761 static int num_cadence = 4;
762 static int user_has_defined_cadences = 0;
764 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
765 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
766 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
767 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
768 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
771 int receivedRingT; /*!< Used to find out what ringtone we are on */
773 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
774 * is 1, the second pause is 2 and so on.
777 static int cidrings[NUM_CADENCE_MAX] = {
778 2, /*!< Right after first long ring */
779 4, /*!< Right after long part */
780 3, /*!< After third chirp */
781 2, /*!< Second spell */
784 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
785 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
787 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
788 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
790 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
793 if (p->subs[0].owner == ast)
795 else if (p->subs[1].owner == ast)
797 else if (p->subs[2].owner == ast)
802 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
808 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
810 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
815 ast_mutex_unlock(&pri->lock);
818 if (p->subs[a].owner) {
819 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
820 ast_mutex_unlock(&p->lock);
822 ast_mutex_lock(&p->lock);
824 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
825 ast_mutex_unlock(&p->subs[a].owner->lock);
833 ast_mutex_lock(&pri->lock);
838 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
840 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
843 /* We must unlock the PRI to avoid the possibility of a deadlock */
846 ast_mutex_unlock(&pri->lock);
850 if (ast_mutex_trylock(&p->owner->lock)) {
851 ast_mutex_unlock(&p->lock);
853 ast_mutex_lock(&p->lock);
855 ast_queue_frame(p->owner, f);
856 ast_mutex_unlock(&p->owner->lock);
864 ast_mutex_lock(&pri->lock);
868 static int restore_gains(struct zt_pvt *p);
870 static void swap_subs(struct zt_pvt *p, int a, int b)
874 struct ast_channel *towner;
876 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
878 tchan = p->subs[a].chan;
879 towner = p->subs[a].owner;
880 tinthreeway = p->subs[a].inthreeway;
882 p->subs[a].chan = p->subs[b].chan;
883 p->subs[a].owner = p->subs[b].owner;
884 p->subs[a].inthreeway = p->subs[b].inthreeway;
886 p->subs[b].chan = tchan;
887 p->subs[b].owner = towner;
888 p->subs[b].inthreeway = tinthreeway;
890 if (p->subs[a].owner)
891 p->subs[a].owner->fds[0] = p->subs[a].zfd;
892 if (p->subs[b].owner)
893 p->subs[b].owner->fds[0] = p->subs[b].zfd;
894 wakeup_sub(p, a, NULL);
895 wakeup_sub(p, b, NULL);
898 static int zt_open(char *fn)
906 for (x = 0; x < strlen(fn); x++) {
907 if (!isdigit(fn[x])) {
915 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
918 fn = "/dev/zap/channel";
920 fd = open(fn, O_RDWR | O_NONBLOCK);
922 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
926 if (ioctl(fd, ZT_SPECIFY, &chan)) {
930 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
935 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
939 static void zt_close(int fd)
945 static int zt_setlinear(int zfd, int linear)
948 res = ioctl(zfd, ZT_SETLINEAR, &linear);
955 static int alloc_sub(struct zt_pvt *p, int x)
959 if (p->subs[x].zfd < 0) {
960 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
961 if (p->subs[x].zfd > -1) {
962 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
964 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
965 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
966 bi.numbufs = numbufs;
967 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
969 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
972 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
973 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
974 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
975 zt_close(p->subs[x].zfd);
980 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
983 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
986 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
990 static int unalloc_sub(struct zt_pvt *p, int x)
993 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
996 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
997 if (p->subs[x].zfd > -1) {
998 zt_close(p->subs[x].zfd);
1000 p->subs[x].zfd = -1;
1001 p->subs[x].linear = 0;
1002 p->subs[x].chan = 0;
1003 p->subs[x].owner = NULL;
1004 p->subs[x].inthreeway = 0;
1005 p->polarity = POLARITY_IDLE;
1006 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1010 static int zt_digit(struct ast_channel *ast, char digit)
1012 ZT_DIAL_OPERATION zo;
1017 ast_mutex_lock(&p->lock);
1018 index = zt_get_index(ast, p, 0);
1019 if ((index == SUB_REAL) && p->owner) {
1021 if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
1023 if (!pri_grab(p, p->pri)) {
1024 pri_information(p->pri->pri,p->call,digit);
1027 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1028 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
1029 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1030 res = strlen(p->dialdest);
1031 p->dialdest[res++] = digit;
1032 p->dialdest[res] = '\0';
1038 zo.op = ZT_DIAL_OP_APPEND;
1039 zo.dialstr[0] = 'T';
1040 zo.dialstr[1] = digit;
1042 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1043 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1048 ast_mutex_unlock(&p->lock);
1052 static char *events[] = {
1065 "Hook Transition Complete",
1070 "Polarity Reversal",
1078 { ZT_ALARM_RED, "Red Alarm" },
1079 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1080 { ZT_ALARM_BLUE, "Blue Alarm" },
1081 { ZT_ALARM_RECOVER, "Recovering" },
1082 { ZT_ALARM_LOOPBACK, "Loopback" },
1083 { ZT_ALARM_NOTOPEN, "Not Open" },
1084 { ZT_ALARM_NONE, "None" },
1087 static char *alarm2str(int alarm)
1090 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1091 if (alarms[x].alarm & alarm)
1092 return alarms[x].name;
1094 return alarm ? "Unknown Alarm" : "No Alarm";
1097 static char *event2str(int event)
1099 static char buf[256];
1100 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1101 return events[event];
1102 sprintf(buf, "Event %d", event); /* safe */
1107 static char *dialplan2str(int dialplan)
1109 if (dialplan == -1) {
1110 return("Dynamically set dialplan in ISDN");
1112 return (pri_plan2str(dialplan));
1116 static char *zap_sig2str(int sig)
1118 static char buf[256];
1121 return "E & M Immediate";
1123 return "E & M Wink";
1127 return "Feature Group D (DTMF)";
1129 return "Feature Group D (MF)";
1130 case SIG_FEATDMF_TA:
1131 return "Feature Groud D (MF) Tandem Access";
1133 return "Feature Group B (MF)";
1137 return "FGC/CAMA (Dialpulse)";
1138 case SIG_FGC_CAMAMF:
1139 return "FGC/CAMA (MF)";
1141 return "FXS Loopstart";
1143 return "FXS Groundstart";
1145 return "FXS Kewlstart";
1147 return "FXO Loopstart";
1149 return "FXO Groundstart";
1151 return "FXO Kewlstart";
1153 return "PRI Signalling";
1155 return "SF (Tone) Signalling Immediate";
1157 return "SF (Tone) Signalling Wink";
1159 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1160 case SIG_SF_FEATDMF:
1161 return "SF (Tone) Signalling with Feature Group D (MF)";
1163 return "SF (Tone) Signalling with Feature Group B (MF)";
1164 case SIG_GR303FXOKS:
1165 return "GR-303 Signalling with FXOKS";
1166 case SIG_GR303FXSKS:
1167 return "GR-303 Signalling with FXSKS";
1169 return "Pseudo Signalling";
1171 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1176 #define sig2str zap_sig2str
1178 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1180 /* If the conference already exists, and we're already in it
1181 don't bother doing anything */
1184 memset(&zi, 0, sizeof(zi));
1187 if (slavechannel > 0) {
1188 /* If we have only one slave, do a digital mon */
1189 zi.confmode = ZT_CONF_DIGITALMON;
1190 zi.confno = slavechannel;
1193 /* Real-side and pseudo-side both participate in conference */
1194 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1195 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1197 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1198 zi.confno = p->confno;
1200 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1204 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1205 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1208 if (slavechannel < 1) {
1209 p->confno = zi.confno;
1211 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1212 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1216 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1218 /* If they're listening to our channel, they're ours */
1219 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1221 /* If they're a talker on our (allocated) conference, they're ours */
1222 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1227 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1230 if (/* Can't delete if there's no zfd */
1232 /* Don't delete from the conference if it's not our conference */
1234 /* Don't delete if we don't think it's conferenced at all (implied) */
1236 memset(&zi, 0, sizeof(zi));
1240 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1241 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1244 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1245 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1249 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1253 struct zt_pvt *slave = NULL;
1254 /* Start out optimistic */
1256 /* Update conference state in a stateless fashion */
1257 for (x = 0; x < 3; x++) {
1258 /* Any three-way calling makes slave native mode *definitely* out
1260 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1263 /* If we don't have any 3-way calls, check to see if we have
1264 precisely one slave */
1265 if (useslavenative) {
1266 for (x = 0; x < MAX_SLAVES; x++) {
1269 /* Whoops already have a slave! No
1270 slave native and stop right away */
1275 /* We have one slave so far */
1276 slave = p->slaves[x];
1281 /* If no slave, slave native definitely out */
1284 else if (slave->law != p->law) {
1290 return useslavenative;
1293 static int reset_conf(struct zt_pvt *p)
1296 memset(&zi, 0, sizeof(zi));
1298 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1299 if (p->subs[SUB_REAL].zfd > -1) {
1300 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1301 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1306 static int update_conf(struct zt_pvt *p)
1311 struct zt_pvt *slave = NULL;
1313 useslavenative = isslavenative(p, &slave);
1314 /* Start with the obvious, general stuff */
1315 for (x = 0; x < 3; x++) {
1316 /* Look for three way calls */
1317 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1318 conf_add(p, &p->subs[x], x, 0);
1321 conf_del(p, &p->subs[x], x);
1324 /* If we have a slave, add him to our conference now. or DAX
1325 if this is slave native */
1326 for (x = 0; x < MAX_SLAVES; x++) {
1329 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1331 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1336 /* If we're supposed to be in there, do so now */
1337 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1339 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1341 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1345 /* If we have a master, add ourselves to his conference */
1347 if (isslavenative(p->master, NULL)) {
1348 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1350 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1354 /* Nobody is left (or should be left) in our conference.
1358 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1362 static void zt_enable_ec(struct zt_pvt *p)
1369 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1373 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1376 if (p->echocancel) {
1377 if (p->sig == SIG_PRI) {
1379 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1381 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1384 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1386 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1389 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1392 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1395 static void zt_train_ec(struct zt_pvt *p)
1399 if (p && p->echocancel && p->echotraining) {
1400 x = p->echotraining;
1401 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1403 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1405 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1408 ast_log(LOG_DEBUG, "No echo training requested\n");
1411 static void zt_disable_ec(struct zt_pvt *p)
1415 if (p->echocancel) {
1417 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1419 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1421 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1426 static void fill_txgain(struct zt_gains *g, float gain, int law)
1430 float linear_gain = pow(10.0, gain / 20.0);
1434 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1436 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1437 if (k > 32767) k = 32767;
1438 if (k < -32767) k = -32767;
1439 g->txgain[j] = AST_LIN2A(k);
1446 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1448 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1449 if (k > 32767) k = 32767;
1450 if (k < -32767) k = -32767;
1451 g->txgain[j] = AST_LIN2MU(k);
1460 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1464 float linear_gain = pow(10.0, gain / 20.0);
1468 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1470 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1471 if (k > 32767) k = 32767;
1472 if (k < -32767) k = -32767;
1473 g->rxgain[j] = AST_LIN2A(k);
1480 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1482 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1483 if (k > 32767) k = 32767;
1484 if (k < -32767) k = -32767;
1485 g->rxgain[j] = AST_LIN2MU(k);
1494 static int set_actual_txgain(int fd, int chan, float gain, int law)
1499 memset(&g, 0, sizeof(g));
1501 res = ioctl(fd, ZT_GETGAINS, &g);
1503 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1507 fill_txgain(&g, gain, law);
1509 return ioctl(fd, ZT_SETGAINS, &g);
1512 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1517 memset(&g, 0, sizeof(g));
1519 res = ioctl(fd, ZT_GETGAINS, &g);
1521 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1525 fill_rxgain(&g, gain, law);
1527 return ioctl(fd, ZT_SETGAINS, &g);
1530 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1532 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1535 static int bump_gains(struct zt_pvt *p)
1539 /* Bump receive gain by 5.0db */
1540 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
1542 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1549 static int restore_gains(struct zt_pvt *p)
1553 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1555 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1562 static inline int zt_set_hook(int fd, int hs)
1566 res = ioctl(fd, ZT_HOOK, &x);
1569 if (errno == EINPROGRESS) return 0;
1570 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1575 static inline int zt_confmute(struct zt_pvt *p, int muted)
1579 if (p->sig == SIG_PRI) {
1581 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1583 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1585 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1587 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1591 static int save_conference(struct zt_pvt *p)
1593 struct zt_confinfo c;
1595 if (p->saveconf.confmode) {
1596 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1599 p->saveconf.chan = 0;
1600 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1602 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1603 p->saveconf.confmode = 0;
1608 c.confmode = ZT_CONF_NORMAL;
1609 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1611 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1615 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1619 static int restore_conference(struct zt_pvt *p)
1622 if (p->saveconf.confmode) {
1623 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1624 p->saveconf.confmode = 0;
1626 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1631 ast_log(LOG_DEBUG, "Restored conferencing\n");
1635 static int send_callerid(struct zt_pvt *p);
1637 static int send_cwcidspill(struct zt_pvt *p)
1641 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1643 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1644 /* Make sure we account for the end */
1645 p->cidlen += READ_SIZE * 4;
1648 if (option_verbose > 2)
1649 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1653 static int has_voicemail(struct zt_pvt *p)
1656 return ast_app_has_voicemail(p->mailbox, NULL);
1659 static int send_callerid(struct zt_pvt *p)
1661 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1663 /* Take out of linear mode if necessary */
1664 if (p->subs[SUB_REAL].linear) {
1665 p->subs[SUB_REAL].linear = 0;
1666 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1668 while (p->cidpos < p->cidlen) {
1669 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1671 if (errno == EAGAIN)
1674 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1684 if (p->callwaitcas) {
1685 /* Wait for CID/CW to expire */
1686 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1688 restore_conference(p);
1692 static int zt_callwait(struct ast_channel *ast)
1694 struct zt_pvt *p = ast->tech_pvt;
1695 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1697 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1700 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1704 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1705 if (!p->callwaitrings && p->callwaitingcallerid) {
1706 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1708 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1710 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1712 p->cidlen = 2400 + READ_SIZE * 4;
1720 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1722 struct zt_pvt *p = ast->tech_pvt;
1723 int x, res, index,mysig;
1728 char dest[256]; /* must be same length as p->dialdest */
1729 ast_mutex_lock(&p->lock);
1730 ast_copy_string(dest, rdest, sizeof(dest));
1731 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1732 if ((ast->_state == AST_STATE_BUSY)) {
1733 p->subs[SUB_REAL].needbusy = 1;
1734 ast_mutex_unlock(&p->lock);
1737 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1738 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1739 ast_mutex_unlock(&p->lock);
1743 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
1745 /* Special pseudo -- automatically up */
1746 ast_setstate(ast, AST_STATE_UP);
1747 ast_mutex_unlock(&p->lock);
1750 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1751 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1753 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1756 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1760 mysig = p->outsigmod;
1766 if (p->owner == ast) {
1767 /* Normal ring, on hook */
1769 /* Don't send audio while on hook, until the call is answered */
1771 if (p->use_callerid) {
1772 /* Generate the Caller-ID spill if desired */
1774 ast_log(LOG_WARNING, "cidspill already exists??\n");
1778 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1779 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1784 /* Choose proper cadence */
1785 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1786 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
1787 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1788 p->cidrings = cidrings[p->distinctivering - 1];
1790 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1791 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1792 p->cidrings = p->sendcalleridafter;
1795 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1796 c = strchr(dest, '/');
1799 if (c && (strlen(c) < p->stripmsd)) {
1800 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1804 p->dop.op = ZT_DIAL_OP_REPLACE;
1805 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1806 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1808 p->dop.dialstr[0] = '\0';
1811 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1812 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1813 ast_mutex_unlock(&p->lock);
1818 /* Call waiting call */
1819 p->callwaitrings = 0;
1820 if (ast->cid.cid_num)
1821 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
1823 p->callwait_num[0] = '\0';
1824 if (ast->cid.cid_name)
1825 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
1827 p->callwait_name[0] = '\0';
1828 /* Call waiting tone instead */
1829 if (zt_callwait(ast)) {
1830 ast_mutex_unlock(&p->lock);
1833 /* Make ring-back */
1834 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1835 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1838 n = ast->cid.cid_name;
1839 l = ast->cid.cid_num;
1841 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1843 p->lastcid_num[0] = '\0';
1845 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1847 p->lastcid_name[0] = '\0';
1848 ast_setstate(ast, AST_STATE_RINGING);
1849 index = zt_get_index(ast, p, 0);
1851 p->subs[index].needringing = 1;
1864 case SIG_FGC_CAMAMF:
1869 case SIG_SF_FEATDMF:
1870 case SIG_FEATDMF_TA:
1872 c = strchr(dest, '/');
1877 if (strlen(c) < p->stripmsd) {
1878 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1879 ast_mutex_unlock(&p->lock);
1883 /* Start the trunk, if not GR-303 */
1887 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1889 if (errno != EINPROGRESS) {
1890 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1891 ast_mutex_unlock(&p->lock);
1898 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1899 p->dop.op = ZT_DIAL_OP_REPLACE;
1905 l = ast->cid.cid_num;
1907 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1909 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1912 l = ast->cid.cid_num;
1914 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1916 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1918 case SIG_FEATDMF_TA:
1920 const char *cic, *ozz;
1922 /* If you have to go through a Tandem Access point you need to use this */
1923 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1926 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1930 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1931 ast_mutex_unlock(&p->lock);
1934 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1935 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1940 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1943 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1945 case SIG_FGC_CAMAMF:
1947 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1951 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1953 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1957 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1958 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1959 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1960 p->echorest[sizeof(p->echorest) - 1] = '\0';
1962 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1966 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1968 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1969 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1970 ast_mutex_unlock(&p->lock);
1974 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1976 if (ast_strlen_zero(c))
1978 ast_setstate(ast, AST_STATE_DIALING);
1981 /* Special pseudo -- automatically up*/
1982 ast_setstate(ast, AST_STATE_UP);
1985 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1986 p->dialdest[0] = '\0';
1989 ast_log(LOG_DEBUG, "not yet implemented\n");
1990 ast_mutex_unlock(&p->lock);
1996 #ifdef SUPPORT_USERUSER
1997 const char *useruser;
2001 int prilocaldialplan;
2005 c = strchr(dest, '/');
2010 if (!p->hidecallerid) {
2011 l = ast->cid.cid_num;
2012 n = ast->cid.cid_name;
2017 if (strlen(c) < p->stripmsd) {
2018 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2019 ast_mutex_unlock(&p->lock);
2022 if (mysig != SIG_FXSKS) {
2023 p->dop.op = ZT_DIAL_OP_REPLACE;
2024 s = strchr(c + p->stripmsd, 'w');
2027 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2029 p->dop.dialstr[0] = '\0';
2032 p->dop.dialstr[0] = '\0';
2035 if (pri_grab(p, p->pri)) {
2036 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2037 ast_mutex_unlock(&p->lock);
2040 if (!(p->call = pri_new_call(p->pri->pri))) {
2041 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2043 ast_mutex_unlock(&p->lock);
2046 if (!(sr = pri_sr_new())) {
2047 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2049 ast_mutex_unlock(&p->lock);
2051 if (p->bearer || (mysig == SIG_FXSKS)) {
2053 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);
2054 p->bearer->call = p->call;
2056 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
2057 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2059 p->digital = IS_DIGITAL(ast->transfercapability);
2060 /* Add support for exclusive override */
2061 if (p->priexclusive)
2064 /* otherwise, traditional behavior */
2065 if (p->pri->nodetype == PRI_NETWORK)
2071 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2072 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2074 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2075 if (p->pri->facilityenable)
2076 pri_facility_enable(p->pri->pri);
2078 if (option_verbose > 2)
2079 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2081 pridialplan = p->pri->dialplan - 1;
2082 if (pridialplan == -2) { /* compute dynamically */
2083 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2084 dp_strip = strlen(p->pri->internationalprefix);
2085 pridialplan = PRI_INTERNATIONAL_ISDN;
2086 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2087 dp_strip = strlen(p->pri->nationalprefix);
2088 pridialplan = PRI_NATIONAL_ISDN;
2090 pridialplan = PRI_LOCAL_ISDN;
2093 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2096 prilocaldialplan = p->pri->localdialplan - 1;
2097 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
2098 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2099 ldp_strip = strlen(p->pri->internationalprefix);
2100 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2101 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2102 ldp_strip = strlen(p->pri->nationalprefix);
2103 prilocaldialplan = PRI_NATIONAL_ISDN;
2105 prilocaldialplan = PRI_LOCAL_ISDN;
2108 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2109 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2110 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
2112 #ifdef SUPPORT_USERUSER
2113 /* User-user info */
2114 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2117 pri_sr_set_useruser(sr, useruser);
2120 if (pri_setup(p->pri->pri, p->call, sr)) {
2121 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2122 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2124 ast_mutex_unlock(&p->lock);
2129 ast_setstate(ast, AST_STATE_DIALING);
2133 ast_mutex_unlock(&p->lock);
2137 static void destroy_zt_pvt(struct zt_pvt **pvt)
2139 struct zt_pvt *p = *pvt;
2140 /* Remove channel from the list */
2142 p->prev->next = p->next;
2144 p->next->prev = p->prev;
2146 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2147 ast_mutex_destroy(&p->lock);
2152 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2162 for (i = 0; i < 3; i++) {
2163 if (cur->subs[i].owner) {
2169 prev->next = cur->next;
2171 prev->next->prev = prev;
2177 iflist->prev = NULL;
2181 if (cur->subs[SUB_REAL].zfd > -1) {
2182 zt_close(cur->subs[SUB_REAL].zfd);
2184 destroy_zt_pvt(&cur);
2188 prev->next = cur->next;
2190 prev->next->prev = prev;
2196 iflist->prev = NULL;
2200 if (cur->subs[SUB_REAL].zfd > -1) {
2201 zt_close(cur->subs[SUB_REAL].zfd);
2203 destroy_zt_pvt(&cur);
2209 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2211 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2213 static char *zap_send_keypad_facility_descrip =
2214 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2215 " IE over the current channel.\n";
2217 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2219 /* Data will be our digit string */
2221 char *digits = (char *) data;
2223 if (ast_strlen_zero(digits)) {
2224 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
2228 p = (struct zt_pvt *)chan->tech_pvt;
2231 ast_log(LOG_DEBUG, "Unable to find technology private\n");
2235 ast_mutex_lock(&p->lock);
2237 if (!p->pri || !p->call) {
2238 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2239 ast_mutex_unlock(&p->lock);
2243 if (!pri_grab(p, p->pri)) {
2244 pri_keypad_facility(p->pri->pri, p->call, digits);
2247 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
2248 ast_mutex_unlock(&p->lock);
2252 ast_mutex_unlock(&p->lock);
2257 static int pri_is_up(struct zt_pri *pri)
2260 for (x = 0; x < NUM_DCHANS; x++) {
2261 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2267 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2269 bearer->owner = &inuse;
2270 bearer->realcall = crv;
2271 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2272 if (crv->subs[SUB_REAL].owner)
2273 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2274 crv->bearer = bearer;
2275 crv->call = bearer->call;
2280 static char *pri_order(int level)
2290 return "Quaternary";
2296 /* Returns fd of the active dchan */
2297 static int pri_active_dchan_fd(struct zt_pri *pri)
2301 for (x = 0; x < NUM_DCHANS; x++) {
2302 if ((pri->dchans[x] == pri->pri))
2309 static int pri_find_dchan(struct zt_pri *pri)
2316 for (x = 0; x < NUM_DCHANS; x++) {
2317 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2319 if (pri->dchans[x] == old) {
2325 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2326 pri->dchannels[newslot]);
2328 if (old && (oldslot != newslot))
2329 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2330 pri->dchannels[oldslot], pri->dchannels[newslot]);
2331 pri->pri = pri->dchans[newslot];
2336 static int zt_hangup(struct ast_channel *ast)
2340 /*static int restore_gains(struct zt_pvt *p);*/
2341 struct zt_pvt *p = ast->tech_pvt;
2342 struct zt_pvt *tmp = NULL;
2343 struct zt_pvt *prev = NULL;
2347 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2348 if (!ast->tech_pvt) {
2349 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2353 ast_mutex_lock(&p->lock);
2355 index = zt_get_index(ast, p, 1);
2357 if (p->sig == SIG_PRI) {
2359 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2365 if (p->origcid_num) {
2366 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2367 free(p->origcid_num);
2368 p->origcid_num = NULL;
2370 if (p->origcid_name) {
2371 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2372 free(p->origcid_name);
2373 p->origcid_name = NULL;
2376 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2380 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2381 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2385 /* Real channel, do some fixup */
2386 p->subs[index].owner = NULL;
2387 p->subs[index].needanswer = 0;
2388 p->subs[index].needflash = 0;
2389 p->subs[index].needringing = 0;
2390 p->subs[index].needbusy = 0;
2391 p->subs[index].needcongestion = 0;
2392 p->subs[index].linear = 0;
2393 p->subs[index].needcallerid = 0;
2394 p->polarity = POLARITY_IDLE;
2395 zt_setlinear(p->subs[index].zfd, 0);
2396 if (index == SUB_REAL) {
2397 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2398 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2399 if (p->subs[SUB_CALLWAIT].inthreeway) {
2400 /* We had flipped over to answer a callwait and now it's gone */
2401 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2402 /* Move to the call-wait, but un-own us until they flip back. */
2403 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2404 unalloc_sub(p, SUB_CALLWAIT);
2407 /* The three way hung up, but we still have a call wait */
2408 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2409 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2410 unalloc_sub(p, SUB_THREEWAY);
2411 if (p->subs[SUB_REAL].inthreeway) {
2412 /* This was part of a three way call. Immediately make way for
2414 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2415 p->owner = p->subs[SUB_REAL].owner;
2417 /* This call hasn't been completed yet... Set owner to NULL */
2418 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2421 p->subs[SUB_REAL].inthreeway = 0;
2423 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2424 /* Move to the call-wait and switch back to them. */
2425 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2426 unalloc_sub(p, SUB_CALLWAIT);
2427 p->owner = p->subs[SUB_REAL].owner;
2428 if (p->owner->_state != AST_STATE_UP)
2429 p->subs[SUB_REAL].needanswer = 1;
2430 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2431 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2432 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2433 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2434 unalloc_sub(p, SUB_THREEWAY);
2435 if (p->subs[SUB_REAL].inthreeway) {
2436 /* This was part of a three way call. Immediately make way for
2438 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2439 p->owner = p->subs[SUB_REAL].owner;
2441 /* This call hasn't been completed yet... Set owner to NULL */
2442 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2445 p->subs[SUB_REAL].inthreeway = 0;
2447 } else if (index == SUB_CALLWAIT) {
2448 /* Ditch the holding callwait call, and immediately make it availabe */
2449 if (p->subs[SUB_CALLWAIT].inthreeway) {
2450 /* This is actually part of a three way, placed on hold. Place the third part
2451 on music on hold now */
2452 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2453 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2454 p->subs[SUB_THREEWAY].inthreeway = 0;
2455 /* Make it the call wait now */
2456 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2457 unalloc_sub(p, SUB_THREEWAY);
2459 unalloc_sub(p, SUB_CALLWAIT);
2460 } else if (index == SUB_THREEWAY) {
2461 if (p->subs[SUB_CALLWAIT].inthreeway) {
2462 /* The other party of the three way call is currently in a call-wait state.
2463 Start music on hold for them, and take the main guy out of the third call */
2464 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2465 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2466 p->subs[SUB_CALLWAIT].inthreeway = 0;
2468 p->subs[SUB_REAL].inthreeway = 0;
2469 /* If this was part of a three way call index, let us make
2470 another three way call */
2471 unalloc_sub(p, SUB_THREEWAY);
2473 /* This wasn't any sort of call, but how are we an index? */
2474 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2478 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2481 p->distinctivering = 0;
2482 p->confirmanswer = 0;
2488 p->onhooktime = time(NULL);
2496 ast_dsp_free(p->dsp);
2500 law = ZT_LAW_DEFAULT;
2501 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2503 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2504 /* Perform low level hangup if no owner left */
2507 #ifdef SUPPORT_USERUSER
2508 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2511 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2512 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2513 if (!pri_grab(p, p->pri)) {
2514 if (p->alreadyhungup) {
2515 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2517 #ifdef SUPPORT_USERUSER
2518 pri_call_set_useruser(p->call, useruser);
2521 pri_hangup(p->pri->pri, p->call, -1);
2524 p->bearer->call = NULL;
2526 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2527 int icause = ast->hangupcause ? ast->hangupcause : -1;
2528 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2530 #ifdef SUPPORT_USERUSER
2531 pri_call_set_useruser(p->call, useruser);
2534 p->alreadyhungup = 1;
2536 p->bearer->alreadyhungup = 1;
2539 icause = atoi(cause);
2541 pri_hangup(p->pri->pri, p->call, icause);
2544 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2547 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2552 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2558 if (p->sig && (p->sig != SIG_PRI))
2559 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2561 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2567 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2570 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2572 /* If they're off hook, try playing congestion */
2573 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
2574 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2576 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2582 /* Make sure we're not made available for at least two seconds assuming
2583 we were actually used for an inbound or outbound call. */
2584 if (ast->_state != AST_STATE_RESERVED) {
2585 time(&p->guardtime);
2590 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2597 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2598 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2602 p->callwaiting = p->permcallwaiting;
2603 p->hidecallerid = p->permhidecallerid;
2608 /* Restore data mode */
2609 if (p->sig == SIG_PRI) {
2611 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2615 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2616 /* Free up the bearer channel as well, and
2617 don't use its file descriptor anymore */
2618 update_conf(p->bearer);
2619 reset_conf(p->bearer);
2620 p->bearer->owner = NULL;
2621 p->bearer->realcall = NULL;
2623 p->subs[SUB_REAL].zfd = -1;
2630 p->callwaitingrepeat = 0;
2633 ast->tech_pvt = NULL;
2634 ast_mutex_unlock(&p->lock);
2635 ast_mutex_lock(&usecnt_lock);
2638 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2639 ast_mutex_unlock(&usecnt_lock);
2640 ast_update_use_count();
2641 if (option_verbose > 2)
2642 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2644 ast_mutex_lock(&iflock);
2650 destroy_channel(prev, tmp, 0);
2658 ast_mutex_unlock(&iflock);
2662 static int zt_answer(struct ast_channel *ast)
2664 struct zt_pvt *p = ast->tech_pvt;
2667 int oldstate = ast->_state;
2668 ast_setstate(ast, AST_STATE_UP);
2669 ast_mutex_lock(&p->lock);
2670 index = zt_get_index(ast, p, 0);
2673 /* nothing to do if a radio channel */
2674 if ((p->radio || (p->oprmode < 0))) {
2675 ast_mutex_unlock(&p->lock);
2689 case SIG_FEATDMF_TA:
2692 case SIG_FGC_CAMAMF:
2697 case SIG_SF_FEATDMF:
2702 /* Pick up the line */
2703 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2704 if (p->hanguponpolarityswitch) {
2705 gettimeofday(&p->polaritydelaytv, NULL);
2707 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2708 tone_zone_play_tone(p->subs[index].zfd, -1);
2710 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2711 if (oldstate == AST_STATE_RINGING) {
2712 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2713 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2714 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2715 p->owner = p->subs[SUB_REAL].owner;
2718 if (p->sig & __ZT_SIG_FXS) {
2725 /* Send a pri acknowledge */
2726 if (!pri_grab(p, p->pri)) {
2728 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2731 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2737 ast_mutex_unlock(&p->lock);
2740 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2743 ast_mutex_unlock(&p->lock);
2747 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2753 struct zt_pvt *p = chan->tech_pvt, *pp;
2754 struct oprmode *oprmode;
2757 /* all supported options require data */
2758 if (!data || (datalen < 1)) {
2764 case AST_OPTION_TXGAIN:
2765 scp = (signed char *) data;
2766 index = zt_get_index(chan, p, 0);
2768 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
2771 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
2772 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
2773 case AST_OPTION_RXGAIN:
2774 scp = (signed char *) data;
2775 index = zt_get_index(chan, p, 0);
2777 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
2780 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
2781 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
2782 case AST_OPTION_TONE_VERIFY:
2788 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2789 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2792 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2793 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2796 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2797 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2801 case AST_OPTION_TDD:
2802 /* turn on or off TDD */
2805 if (!*cp) { /* turn it off */
2806 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2812 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
2813 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
2815 /* otherwise, turn it on */
2816 if (!p->didtdd) { /* if havent done it yet */
2817 unsigned char mybuf[41000], *buf;
2818 int size, res, fd, len;
2819 struct pollfd fds[1];
2822 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
2823 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
2825 index = zt_get_index(chan, p, 0);
2827 ast_log(LOG_WARNING, "No index in TDD?\n");
2830 fd = p->subs[index].zfd;
2832 if (ast_check_hangup(chan))
2835 if (size > READ_SIZE)
2838 fds[0].events = POLLPRI | POLLOUT;
2840 res = poll(fds, 1, -1);
2842 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2845 /* if got exception */
2846 if (fds[0].revents & POLLPRI)
2848 if (!(fds[0].revents & POLLOUT)) {
2849 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2852 res = write(fd, buf, size);
2854 if (res == -1) return -1;
2855 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2861 p->didtdd = 1; /* set to have done it now */
2863 if (*cp == 2) { /* Mate mode */
2870 if (!p->tdd) { /* if we dont have one yet */
2871 p->tdd = tdd_new(); /* allocate one */
2874 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2878 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
2879 *cp ? "ON" : "OFF", (int) *cp, chan->name);
2880 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
2882 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2885 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
2889 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
2892 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2893 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2895 case AST_OPTION_OPRMODE: /* Operator services mode */
2896 oprmode = (struct oprmode *) data;
2897 pp = oprmode->peer->tech_pvt;
2898 p->oprmode = pp->oprmode = 0;
2902 /* setup modes, if any */
2905 pp->oprmode = oprmode->mode;
2906 p->oprmode = -oprmode->mode;
2908 ast_log(LOG_DEBUG, "Set Operator Services mode, value: %d on %s/%s\n",
2909 oprmode->mode, chan->name,oprmode->peer->name);;
2911 case AST_OPTION_ECHOCAN:
2914 ast_log(LOG_DEBUG, "Enabling echo cancelation on %s\n", chan->name);
2917 ast_log(LOG_DEBUG, "Disabling echo cancelation on %s\n", chan->name);
2927 static int zt_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
2929 struct zt_pvt *p = chan->tech_pvt;
2931 if (!strcasecmp(data, "rxgain")) {
2932 ast_mutex_lock(&p->lock);
2933 snprintf(buf, len, "%f", p->rxgain);
2934 ast_mutex_unlock(&p->lock);
2935 } else if (!strcasecmp(data, "txgain")) {
2936 ast_mutex_lock(&p->lock);
2937 snprintf(buf, len, "%f", p->txgain);
2938 ast_mutex_unlock(&p->lock);
2940 ast_copy_string(buf, "", len);
2946 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2948 /* Unlink a specific slave or all slaves/masters from a given master */
2954 ast_mutex_lock(&master->lock);
2956 while (ast_mutex_trylock(&slave->lock)) {
2957 ast_mutex_unlock(&master->lock);
2959 ast_mutex_lock(&master->lock);
2964 for (x = 0; x < MAX_SLAVES; x++) {
2965 if (master->slaves[x]) {
2966 if (!slave || (master->slaves[x] == slave)) {
2967 /* Take slave out of the conference */
2968 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2969 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2970 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2971 master->slaves[x]->master = NULL;
2972 master->slaves[x] = NULL;
2977 master->inconference = 0;
2980 if (master->master) {
2981 /* Take master out of the conference */
2982 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2983 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2985 for (x = 0; x < MAX_SLAVES; x++) {
2986 if (master->master->slaves[x] == master)
2987 master->master->slaves[x] = NULL;
2988 else if (master->master->slaves[x])
2992 master->master->inconference = 0;
2994 master->master = NULL;
2996 update_conf(master);
2999 ast_mutex_unlock(&slave->lock);
3000 ast_mutex_unlock(&master->lock);
3004 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
3006 if (!slave || !master) {
3007 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
3010 for (x = 0; x < MAX_SLAVES; x++) {
3011 if (!master->slaves[x]) {
3012 master->slaves[x] = slave;
3016 if (x >= MAX_SLAVES) {
3017 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
3018 master->slaves[MAX_SLAVES - 1] = slave;
3021 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
3022 slave->master = master;
3024 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
3027 static void disable_dtmf_detect(struct zt_pvt *p)
3029 #ifdef ZT_TONEDETECT
3035 #ifdef ZT_TONEDETECT
3037 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3039 if (!p->hardwaredtmf && p->dsp) {
3040 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
3041 ast_dsp_set_features(p->dsp, p->dsp_features);
3045 static void enable_dtmf_detect(struct zt_pvt *p)
3047 #ifdef ZT_TONEDETECT
3051 if (p->channel == CHAN_PSEUDO)
3056 #ifdef ZT_TONEDETECT
3057 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
3058 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3060 if (!p->hardwaredtmf && p->dsp) {
3061 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
3062 ast_dsp_set_features(p->dsp, p->dsp_features);
3066 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)
3068 struct ast_channel *who;
3069 struct zt_pvt *p0, *p1, *op0, *op1;
3070 struct zt_pvt *master = NULL, *slave = NULL;
3071 struct ast_frame *f;
3075 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
3076 int os0 = -1, os1 = -1;
3078 struct ast_channel *oc0, *oc1;
3079 enum ast_bridge_result res;
3082 int triedtopribridge = 0;
3083 q931_call *q931c0 = NULL, *q931c1 = NULL;
3086 /* For now, don't attempt to native bridge if either channel needs DTMF detection.
3087 There is code below to handle it properly until DTMF is actually seen,
3088 but due to currently unresolved issues it's ignored...
3091 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
3092 return AST_BRIDGE_FAILED_NOWARN;
3094 ast_mutex_lock(&c0->lock);
3095 ast_mutex_lock(&c1->lock);
3099 /* cant do pseudo-channels here */
3100 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
3101 ast_mutex_unlock(&c0->lock);
3102 ast_mutex_unlock(&c1->lock);
3103 return AST_BRIDGE_FAILED_NOWARN;
3106 oi0 = zt_get_index(c0, p0, 0);
3107 oi1 = zt_get_index(c1, p1, 0);
3108 if ((oi0 < 0) || (oi1 < 0)) {
3109 ast_mutex_unlock(&c0->lock);
3110 ast_mutex_unlock(&c1->lock);
3111 return AST_BRIDGE_FAILED;
3114 op0 = p0 = c0->tech_pvt;
3115 op1 = p1 = c1->tech_pvt;
3121 ast_mutex_lock(&p0->lock);
3122 if (ast_mutex_trylock(&p1->lock)) {
3123 /* Don't block, due to potential for deadlock */
3124 ast_mutex_unlock(&p0->lock);
3125 ast_mutex_unlock(&c0->lock);
3126 ast_mutex_unlock(&c1->lock);
3127 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
3128 return AST_BRIDGE_RETRY;
3131 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
3132 if (p0->owner && p1->owner) {
3133 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
3134 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
3138 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
3143 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
3144 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
3146 oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
3147 p0->subs[SUB_REAL].inthreeway, p0->channel,
3148 oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
3149 p1->subs[SUB_REAL].inthreeway);
3153 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
3154 if (p1->subs[SUB_THREEWAY].inthreeway) {
3159 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
3160 if (p0->subs[SUB_THREEWAY].inthreeway) {
3165 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
3166 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
3167 don't put us in anything */
3168 if (p1->subs[SUB_CALLWAIT].inthreeway) {
3173 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
3174 /* Same as previous */
3175 if (p0->subs[SUB_CALLWAIT].inthreeway) {
3181 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
3182 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
3183 if (master && slave) {
3184 /* Stop any tones, or play ringtone as appropriate. If they're bridged
3185 in an active threeway call with a channel that is ringing, we should
3186 indicate ringing. */
3187 if ((oi1 == SUB_THREEWAY) &&
3188 p1->subs[SUB_THREEWAY].inthreeway &&
3189 p1->subs[SUB_REAL].owner &&
3190 p1->subs[SUB_REAL].inthreeway &&
3191 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
3192 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
3193 tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE);
3194 os1 = p1->subs[SUB_REAL].owner->_state;
3196 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
3197 tone_zone_play_tone(p0->subs[oi0].zfd, -1);
3199 if ((oi0 == SUB_THREEWAY) &&
3200 p0->subs[SUB_THREEWAY].inthreeway &&
3201 p0->subs[SUB_REAL].owner &&
3202 p0->subs[SUB_REAL].inthreeway &&
3203 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
3204 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
3205 tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE);
3206 os0 = p0->subs[SUB_REAL].owner->_state;
3208 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
3209 tone_zone_play_tone(p1->subs[oi0].zfd, -1);
3211 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
3212 if (!p0->echocanbridged || !p1->echocanbridged) {
3213 /* Disable echo cancellation if appropriate */
3218 zt_link(slave, master);
3219 master->inconference = inconf;
3220 } else if (!nothingok)
3221 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
3225 t0 = p0->subs[SUB_REAL].inthreeway;
3226 t1 = p1->subs[SUB_REAL].inthreeway;
3228 ast_mutex_unlock(&p0->lock);
3229 ast_mutex_unlock(&p1->lock);
3231 ast_mutex_unlock(&c0->lock);
3232 ast_mutex_unlock(&c1->lock);
3234 /* Native bridge failed */
3235 if ((!master || !slave) && !nothingok) {
3238 return AST_BRIDGE_FAILED;
3241 if (option_verbose > 2)
3242 ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
3244 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
3245 disable_dtmf_detect(op0);
3247 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
3248 disable_dtmf_detect(op1);
3251 struct ast_channel *c0_priority[2] = {c0, c1};
3252 struct ast_channel *c1_priority[2] = {c1, c0};
3254 /* Here's our main loop... Start by locking things, looking for private parts,
3255 and then balking if anything is wrong */
3256 ast_mutex_lock(&c0->lock);
3257 ast_mutex_lock(&c1->lock);
3262 i0 = zt_get_index(c0, p0, 1);
3264 i1 = zt_get_index(c1, p1, 1);
3265 ast_mutex_unlock(&c0->lock);
3266 ast_mutex_unlock(&c1->lock);
3271 (ofd0 != c0->fds[0]) ||
3272 (ofd1 != c1->fds[0]) ||
3273 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
3274 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
3275 (oc0 != p0->owner) ||
3276 (oc1 != p1->owner) ||
3277 (t0 != p0->subs[SUB_REAL].inthreeway) ||
3278 (t1 != p1->subs[SUB_REAL].inthreeway) ||
3281 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
3282 op0->channel, oi0, op1->channel, oi1);
3283 res = AST_BRIDGE_RETRY;
3284 goto return_from_bridge;
3290 if (p0->transfer && p1->transfer
3292 && !triedtopribridge) {
3293 pri_channel_bridge(q931c0, q931c1);
3294 triedtopribridge = 1;
3298 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
3300 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
3304 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
3307 res = AST_BRIDGE_COMPLETE;
3308 goto return_from_bridge;
3310 if (f->frametype == AST_FRAME_DTMF) {
3311 if ((who == c0) && p0->pulsedial) {
3313 } else if ((who == c1) && p1->pulsedial) {
3318 res = AST_BRIDGE_COMPLETE;
3319 goto return_from_bridge;
3324 /* Swap who gets priority */
3325 priority = !priority;
3335 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
3336 enable_dtmf_detect(op0);
3338 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
3339 enable_dtmf_detect(op1);
3341 zt_unlink(slave, master, 1);
3346 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
3348 struct zt_pvt *p = newchan->tech_pvt;
3350 ast_mutex_lock(&p->lock);
3351 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
3352 if (p->owner == oldchan) {
3355 for (x = 0; x < 3; x++)
3356 if (p->subs[x].owner == oldchan) {
3358 zt_unlink(NULL, p, 0);
3359 p->subs[x].owner = newchan;
3361 if (newchan->_state == AST_STATE_RINGING)
3362 zt_indicate(newchan, AST_CONTROL_RINGING, NULL, 0);
3364 ast_mutex_unlock(&p->lock);
3368 static int zt_ring_phone(struct zt_pvt *p)
3372 /* Make sure our transmit state is on hook */
3375 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
3378 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
3380 printf("Res: %d, error: %s\n", res, strerror(errno));
3386 /* Wait just in case */
3393 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
3401 static void *ss_thread(void *data);
3403 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
3405 static int attempt_transfer(struct zt_pvt *p)
3407 /* In order to transfer, we need at least one of the channels to
3408 actually be in a call bridge. We can't conference two applications
3409 together (but then, why would we want to?) */
3410 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
3411 /* The three-way person we're about to transfer to could still be in MOH, so
3412 stop if now if appropriate */
3413 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
3414 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
3415 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
3416 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
3418 if (p->subs[SUB_REAL].owner->cdr) {
3419 /* Move CDR from second channel to current one */
3420 p->subs[SUB_THREEWAY].owner->cdr =
3421 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
3422 p->subs[SUB_REAL].owner->cdr = NULL;
3424 if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
3425 /* Move CDR from second channel's bridge to current one */
3426 p->subs[SUB_THREEWAY].owner->cdr =
3427 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
3428 ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
3430 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
3431 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
3432 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
3435 /* Orphan the channel after releasing the lock */
3436 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
3437 unalloc_sub(p, SUB_THREEWAY);
3438 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
3439 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
3440 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
3442 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
3443 if (p->subs[SUB_THREEWAY].owner->cdr) {
3444 /* Move CDR from second channel to current one */
3445 p->subs[SUB_REAL].owner->cdr =
3446 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);