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 Decprecate the "musiconhold" configuration option post 1.4
41 <depend>zaptel</depend>
50 #include <sys/signal.h>
54 #if !defined(SOLARIS) && !defined(__FreeBSD__)
58 #include <sys/ioctl.h>
60 #include <linux/zaptel.h>
63 #endif /* __linux__ */
72 #ifndef PRI_KEYPAD_FACILITY_TX
73 #error "You need newer libpri"
77 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
79 #include "asterisk/lock.h"
80 #include "asterisk/channel.h"
81 #include "asterisk/config.h"
82 #include "asterisk/logger.h"
83 #include "asterisk/module.h"
84 #include "asterisk/pbx.h"
85 #include "asterisk/options.h"
86 #include "asterisk/file.h"
87 #include "asterisk/ulaw.h"
88 #include "asterisk/alaw.h"
89 #include "asterisk/callerid.h"
90 #include "asterisk/adsi.h"
91 #include "asterisk/cli.h"
92 #include "asterisk/cdr.h"
93 #include "asterisk/features.h"
94 #include "asterisk/musiconhold.h"
95 #include "asterisk/say.h"
96 #include "asterisk/tdd.h"
97 #include "asterisk/app.h"
98 #include "asterisk/dsp.h"
99 #include "asterisk/astdb.h"
100 #include "asterisk/manager.h"
101 #include "asterisk/causes.h"
102 #include "asterisk/term.h"
103 #include "asterisk/utils.h"
104 #include "asterisk/transcap.h"
105 #include "asterisk/stringfields.h"
106 #include "asterisk/abstract_jb.h"
107 #include "asterisk/smdi.h"
108 #include "asterisk/astobj.h"
109 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
111 /*! Global jitterbuffer configuration - by default, jb is disabled */
112 static struct ast_jb_conf default_jbconf =
116 .resync_threshold = -1,
119 static struct ast_jb_conf global_jbconf;
121 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_LIBPRI) && !defined(ZT_SIG_HARDHDLC))
122 #error "Your zaptel is too old. Please update"
125 #ifndef ZT_TONEDETECT
126 /* Work around older code with no tone detect */
127 #define ZT_EVENT_DTMFDOWN 0
128 #define ZT_EVENT_DTMFUP 0
131 /* define this to send PRI user-user information elements */
132 #undef SUPPORT_USERUSER
135 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
136 * the user hangs up to reset the state machine so ring works properly.
137 * This is used to be able to support kewlstart by putting the zhone in
138 * groundstart mode since their forward disconnect supervision is entirely
139 * broken even though their documentation says it isn't and their support
140 * is entirely unwilling to provide any assistance with their channel banks
141 * even though their web site says they support their products for life.
143 /* #define ZHONE_HACK */
146 * Define if you want to check the hook state for an FXO (FXS signalled) interface
147 * before dialing on it. Certain FXO interfaces always think they're out of
148 * service with this method however.
150 /* #define ZAP_CHECK_HOOKSTATE */
152 /*! \brief Typically, how many rings before we should send Caller*ID */
153 #define DEFAULT_CIDRINGS 1
155 #define CHANNEL_PSEUDO -12
157 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
159 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
160 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FGC_CAMA) || ((p)->sig == SIG_FGC_CAMAMF) || ((p)->sig == SIG_FEATB))
162 static const char tdesc[] = "Zapata Telephony Driver"
168 static const char config[] = "zapata.conf";
170 #define SIG_EM ZT_SIG_EM
171 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
172 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
173 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
174 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
175 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
176 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
177 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
178 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
179 #define SIG_FXSLS ZT_SIG_FXSLS
180 #define SIG_FXSGS ZT_SIG_FXSGS
181 #define SIG_FXSKS ZT_SIG_FXSKS
182 #define SIG_FXOLS ZT_SIG_FXOLS
183 #define SIG_FXOGS ZT_SIG_FXOGS
184 #define SIG_FXOKS ZT_SIG_FXOKS
185 #define SIG_PRI ZT_SIG_CLEAR
186 #define SIG_SF ZT_SIG_SF
187 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
188 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
189 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
190 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
191 #define SIG_EM_E1 ZT_SIG_EM_E1
192 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
193 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
196 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
197 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
199 #define CHAN_PSEUDO -2
201 #define DCHAN_PROVISIONED (1 << 0)
202 #define DCHAN_NOTINALARM (1 << 1)
203 #define DCHAN_UP (1 << 2)
205 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
207 static char context[AST_MAX_CONTEXT] = "default";
208 static char cid_num[256] = "";
209 static char cid_name[256] = "";
210 static char defaultcic[64] = "";
211 static char defaultozz[64] = "";
213 static char language[MAX_LANGUAGE] = "";
214 static char musicclass[MAX_MUSICCLASS] = "";
215 static char progzone[10] = "";
217 static int usedistinctiveringdetection = 0;
218 static int distinctiveringaftercid = 0;
220 static int transfertobusy = 1;
222 static int use_callerid = 1;
223 static int cid_signalling = CID_SIG_BELL;
224 static int cid_start = CID_START_RING;
225 static int zaptrcallerid = 0;
226 static int cur_signalling = -1;
227 static int cur_outsignalling = -1;
229 static ast_group_t cur_group = 0;
230 static ast_group_t cur_callergroup = 0;
231 static ast_group_t cur_pickupgroup = 0;
232 static int relaxdtmf = 0;
234 static int immediate = 0;
236 static int stripmsd = 0;
238 static int callwaiting = 0;
240 static int callwaitingcallerid = 0;
242 static int hidecallerid = 0;
244 static int restrictcid = 0;
246 static int use_callingpres = 0;
248 static int callreturn = 0;
250 static int threewaycalling = 0;
252 static int transfer = 0;
254 static int canpark = 0;
256 static int cancallforward = 0;
258 static float rxgain = 0.0;
260 static float txgain = 0.0;
262 static int tonezone = -1;
264 static int echocancel;
266 static int echotraining;
270 static int echocanbridged = 0;
272 static int busydetect = 0;
274 static int busycount = 3;
275 static int busy_tonelength = 0;
276 static int busy_quietlength = 0;
278 static int callprogress = 0;
280 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
282 static char mailbox[AST_MAX_EXTENSION];
284 static int amaflags = 0;
287 static int use_smdi = 0;
288 static char smdi_port[SMDI_MAX_FILENAME_LEN] = "/dev/ttyS0";
289 static int numbufs = 4;
291 static int cur_prewink = -1;
292 static int cur_preflash = -1;
293 static int cur_wink = -1;
294 static int cur_flash = -1;
295 static int cur_start = -1;
296 static int cur_rxwink = -1;
297 static int cur_rxflash = -1;
298 static int cur_debounce = -1;
299 static int cur_priexclusive = 0;
301 static int priindication_oob = 0;
304 static int minunused = 2;
305 static int minidle = 0;
306 static char idleext[AST_MAX_EXTENSION];
307 static char idledial[AST_MAX_EXTENSION];
308 static int overlapdial = 0;
309 static int facilityenable = 0;
310 static char internationalprefix[10] = "";
311 static char nationalprefix[10] = "";
312 static char localprefix[20] = "";
313 static char privateprefix[20] = "";
314 static char unknownprefix[20] = "";
315 static long resetinterval = 3600; /*!< How often (in seconds) to reset unused channels. Default 1 hour. */
316 static struct ast_channel inuse;
317 #ifdef PRI_GETSET_TIMERS
318 static int pritimers[PRI_MAX_TIMERS];
320 static int pridebugfd = -1;
321 static char pridebugfilename[1024] = "";
324 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
325 static int firstdigittimeout = 16000;
327 /*! \brief How long to wait for following digits (FXO logic) */
328 static int gendigittimeout = 8000;
330 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
331 static int matchdigittimeout = 3000;
333 static int usecnt = 0;
334 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
336 /*! \brief Protect the interface list (of zt_pvt's) */
337 AST_MUTEX_DEFINE_STATIC(iflock);
340 static int ifcount = 0;
343 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
346 /*! \brief Whether we answer on a Polarity Switch event */
347 static int answeronpolarityswitch = 0;
349 /*! \brief Whether we hang up on a Polarity Switch event */
350 static int hanguponpolarityswitch = 0;
352 /*! \brief How long (ms) to ignore Polarity Switch events after we answer a call */
353 static int polarityonanswerdelay = 600;
355 /*! \brief When to send the CallerID signals (rings) */
356 static int sendcalleridafter = DEFAULT_CIDRINGS;
358 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
359 when it's doing something critical. */
360 AST_MUTEX_DEFINE_STATIC(monlock);
362 /*! \brief This is the thread for the monitor which checks for input on the channels
363 which are not currently in use. */
364 static pthread_t monitor_thread = AST_PTHREADT_NULL;
366 static int restart_monitor(void);
368 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);
370 static int zt_sendtext(struct ast_channel *c, const char *text);
372 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
373 static inline int zt_get_event(int fd)
376 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
381 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
382 static inline int zt_wait_event(int fd)
385 i = ZT_IOMUX_SIGEVENT;
386 if (ioctl(fd, ZT_IOMUX, &i) == -1)
388 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
393 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
394 #define READ_SIZE 160
396 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
397 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
399 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
400 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
401 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
402 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
403 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
407 static int ringt_base = DEFAULT_RINGT;
411 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
412 #define PRI_CHANNEL(p) ((p) & 0xff)
413 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
414 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
417 pthread_t master; /*!< Thread of master */
418 ast_mutex_t lock; /*!< Mutex */
419 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
420 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
421 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
422 int minunused; /*!< Min # of channels to keep empty */
423 int minidle; /*!< Min # of "idling" calls to keep active */
424 int nodetype; /*!< Node type */
425 int switchtype; /*!< Type of switch to emulate */
426 int nsf; /*!< Network-Specific Facilities */
427 int dialplan; /*!< Dialing plan */
428 int localdialplan; /*!< Local dialing plan */
429 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
430 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
431 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
432 char privateprefix[20]; /*!< for private dialplans */
433 char unknownprefix[20]; /*!< for unknown dialplans */
434 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
435 int trunkgroup; /*!< What our trunkgroup is */
436 int mastertrunkgroup; /*!< What trunk group is our master */
437 int prilogicalspan; /*!< Logical span number within trunk group */
438 int numchans; /*!< Num of channels we represent */
439 int overlapdial; /*!< In overlap dialing mode */
440 int facilityenable; /*!< Enable facility IEs */
441 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
442 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
443 struct pri *pri; /*!< Currently active D-channel */
445 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
450 time_t lastreset; /*!< time when unused channels were last reset */
451 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
452 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
453 struct zt_pvt *crvs; /*!< Member CRV structs */
454 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
458 static struct zt_pri pris[NUM_SPANS];
460 static int pritype = PRI_CPE;
463 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
465 #define DEFAULT_PRI_DEBUG 0
468 static inline void pri_rel(struct zt_pri *pri)
470 ast_mutex_unlock(&pri->lock);
473 static int switchtype = PRI_SWITCH_NI2;
474 static int nsf = PRI_NSF_NONE;
475 static int dialplan = PRI_NATIONAL_ISDN + 1;
476 static int localdialplan = PRI_NATIONAL_ISDN + 1;
479 /*! Shut up the compiler */
483 #define SUB_REAL 0 /*!< Active call */
484 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
485 #define SUB_THREEWAY 2 /*!< Three-way call */
487 /* Polarity states */
488 #define POLARITY_IDLE 0
489 #define POLARITY_REV 1
492 static struct zt_distRings drings;
494 struct distRingData {
497 struct ringContextData {
498 char contextData[AST_MAX_CONTEXT];
500 struct zt_distRings {
501 struct distRingData ringnum[3];
502 struct ringContextData ringContext[3];
505 static char *subnames[] = {
511 struct zt_subchannel {
513 struct ast_channel *owner;
515 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
516 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
517 unsigned int needringing:1;
518 unsigned int needbusy:1;
519 unsigned int needcongestion:1;
520 unsigned int needcallerid:1;
521 unsigned int needanswer:1;
522 unsigned int needflash:1;
523 unsigned int linear:1;
524 unsigned int inthreeway:1;
528 #define CONF_USER_REAL (1 << 0)
529 #define CONF_USER_THIRDCALL (1 << 1)
533 static struct zt_pvt {
535 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
536 /*!< Up to three channels can be associated with this call */
538 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
539 struct zt_subchannel subs[3]; /*!< Sub-channels */
540 struct zt_confinfo saveconf; /*!< Saved conference info */
542 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
543 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
544 int inconference; /*!< If our real should be in the conference */
546 int sig; /*!< Signalling style */
547 int radio; /*!< radio type */
548 int outsigmod; /*!< Outbound Signalling style (modifier) */
549 int oprmode; /*!< "Operator Services" mode */
550 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
553 int tonezone; /*!< tone zone for this chan, or -1 for default */
554 struct zt_pvt *next; /*!< Next channel in list */
555 struct zt_pvt *prev; /*!< Prev channel in list */
559 unsigned int answeronpolarityswitch:1;
560 unsigned int busydetect:1;
561 unsigned int callreturn:1;
562 unsigned int callwaiting:1;
563 unsigned int callwaitingcallerid:1;
564 unsigned int cancallforward:1;
565 unsigned int canpark:1;
566 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
567 unsigned int destroy:1;
568 unsigned int didtdd:1; /*!< flag to say its done it once */
569 unsigned int dialednone:1;
570 unsigned int dialing:1;
571 unsigned int digital:1;
573 unsigned int echobreak:1;
574 unsigned int echocanbridged:1;
575 unsigned int echocanon:1;
576 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
577 unsigned int firstradio:1;
578 unsigned int hanguponpolarityswitch:1;
579 unsigned int hardwaredtmf:1;
580 unsigned int hidecallerid;
581 unsigned int ignoredtmf:1;
582 unsigned int immediate:1; /*!< Answer before getting digits? */
583 unsigned int inalarm:1;
584 unsigned int mate:1; /*!< flag to say its in MATE mode */
585 unsigned int outgoing:1;
586 unsigned int overlapdial:1;
587 unsigned int permcallwaiting:1;
588 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
589 unsigned int priindication_oob:1;
590 unsigned int priexclusive:1;
591 unsigned int pulse:1;
592 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
593 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
594 unsigned int threewaycalling:1;
595 unsigned int transfer:1;
596 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
597 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
598 unsigned int usedistinctiveringdetection:1;
599 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
600 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
601 #if defined(HAVE_LIBPRI)
602 unsigned int alerting:1;
603 unsigned int alreadyhungup:1;
604 unsigned int isidlecall:1;
605 unsigned int proceeding:1;
606 unsigned int progress:1;
607 unsigned int resetting:1;
608 unsigned int setup_ack:1;
610 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
611 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
613 struct zt_distRings drings;
615 char context[AST_MAX_CONTEXT];
616 char defcontext[AST_MAX_CONTEXT];
617 char exten[AST_MAX_EXTENSION];
618 char language[MAX_LANGUAGE];
619 char musicclass[MAX_MUSICCLASS];
621 char cid_ani[AST_MAX_EXTENSION];
623 char cid_num[AST_MAX_EXTENSION];
624 int cid_ton; /*!< Type Of Number (TON) */
625 char cid_name[AST_MAX_EXTENSION];
626 char lastcid_num[AST_MAX_EXTENSION];
627 char lastcid_name[AST_MAX_EXTENSION];
628 char *origcid_num; /*!< malloced original callerid */
629 char *origcid_name; /*!< malloced original callerid */
630 char callwait_num[AST_MAX_EXTENSION];
631 char callwait_name[AST_MAX_EXTENSION];
632 char rdnis[AST_MAX_EXTENSION];
633 char dnid[AST_MAX_EXTENSION];
636 int confno; /*!< Our conference */
637 int confusers; /*!< Who is using our conference */
638 int propconfno; /*!< Propagated conference number */
639 ast_group_t callgroup;
640 ast_group_t pickupgroup;
641 int channel; /*!< Channel Number or CRV */
642 int span; /*!< Span number */
643 time_t guardtime; /*!< Must wait this much time before using for new call */
644 int cid_signalling; /*!< CID signalling type bell202 or v23 */
645 int cid_start; /*!< CID start indicator, polarity or ring */
646 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
647 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
648 int cidcwexpire; /*!< When to expire our muting for CID/CW */
649 unsigned char *cidspill;
662 int busy_quietlength;
664 struct timeval flashtime; /*!< Last flash-hook time */
666 int cref; /*!< Call reference number */
667 ZT_DIAL_OPERATION dop;
668 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
670 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
671 int amaflags; /*!< AMA Flags */
672 struct tdd_state *tdd; /*!< TDD flag */
673 char call_forward[AST_MAX_EXTENSION];
674 char mailbox[AST_MAX_EXTENSION];
678 int distinctivering; /*!< Which distinctivering to use */
679 int cidrings; /*!< Which ring to deliver CID on */
680 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
682 int polarityonanswerdelay;
683 struct timeval polaritydelaytv;
684 int sendcalleridafter;
687 struct zt_pvt *bearer;
688 struct zt_pvt *realcall;
695 } *iflist = NULL, *ifend = NULL;
697 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
698 static int zt_digit(struct ast_channel *ast, char digit);
699 static int zt_sendtext(struct ast_channel *c, const char *text);
700 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
701 static int zt_hangup(struct ast_channel *ast);
702 static int zt_answer(struct ast_channel *ast);
703 struct ast_frame *zt_read(struct ast_channel *ast);
704 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
705 struct ast_frame *zt_exception(struct ast_channel *ast);
706 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
707 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
708 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
709 static int zt_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
711 static const struct ast_channel_tech zap_tech = {
713 .description = tdesc,
714 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
715 .requester = zt_request,
716 .send_digit = zt_digit,
717 .send_text = zt_sendtext,
724 .exception = zt_exception,
725 .indicate = zt_indicate,
727 .setoption = zt_setoption,
728 .func_channel_read = zt_func_read,
732 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
734 #define GET_CHANNEL(p) ((p)->channel)
737 struct zt_pvt *round_robin[32];
740 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
743 /* Grab the lock first */
745 res = ast_mutex_trylock(&pri->lock);
747 ast_mutex_unlock(&pvt->lock);
748 /* Release the lock and try again */
750 ast_mutex_lock(&pvt->lock);
753 /* Then break the poll */
754 pthread_kill(pri->master, SIGURG);
759 #define NUM_CADENCE_MAX 25
760 static int num_cadence = 4;
761 static int user_has_defined_cadences = 0;
763 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
764 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
765 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
766 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
767 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
770 int receivedRingT; /*!< Used to find out what ringtone we are on */
772 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
773 * is 1, the second pause is 2 and so on.
776 static int cidrings[NUM_CADENCE_MAX] = {
777 2, /*!< Right after first long ring */
778 4, /*!< Right after long part */
779 3, /*!< After third chirp */
780 2, /*!< Second spell */
783 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
784 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
786 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
787 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
789 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
792 if (p->subs[0].owner == ast)
794 else if (p->subs[1].owner == ast)
796 else if (p->subs[2].owner == ast)
801 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
807 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
809 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
814 ast_mutex_unlock(&pri->lock);
817 if (p->subs[a].owner) {
818 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
819 ast_mutex_unlock(&p->lock);
821 ast_mutex_lock(&p->lock);
823 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
824 ast_mutex_unlock(&p->subs[a].owner->lock);
832 ast_mutex_lock(&pri->lock);
837 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
839 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
842 /* We must unlock the PRI to avoid the possibility of a deadlock */
845 ast_mutex_unlock(&pri->lock);
849 if (ast_mutex_trylock(&p->owner->lock)) {
850 ast_mutex_unlock(&p->lock);
852 ast_mutex_lock(&p->lock);
854 ast_queue_frame(p->owner, f);
855 ast_mutex_unlock(&p->owner->lock);
863 ast_mutex_lock(&pri->lock);
867 static int restore_gains(struct zt_pvt *p);
869 static void swap_subs(struct zt_pvt *p, int a, int b)
873 struct ast_channel *towner;
875 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
877 tchan = p->subs[a].chan;
878 towner = p->subs[a].owner;
879 tinthreeway = p->subs[a].inthreeway;
881 p->subs[a].chan = p->subs[b].chan;
882 p->subs[a].owner = p->subs[b].owner;
883 p->subs[a].inthreeway = p->subs[b].inthreeway;
885 p->subs[b].chan = tchan;
886 p->subs[b].owner = towner;
887 p->subs[b].inthreeway = tinthreeway;
889 if (p->subs[a].owner)
890 p->subs[a].owner->fds[0] = p->subs[a].zfd;
891 if (p->subs[b].owner)
892 p->subs[b].owner->fds[0] = p->subs[b].zfd;
893 wakeup_sub(p, a, NULL);
894 wakeup_sub(p, b, NULL);
897 static int zt_open(char *fn)
905 for (x = 0; x < strlen(fn); x++) {
906 if (!isdigit(fn[x])) {
914 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
917 fn = "/dev/zap/channel";
919 fd = open(fn, O_RDWR | O_NONBLOCK);
921 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
925 if (ioctl(fd, ZT_SPECIFY, &chan)) {
929 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
934 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
938 static void zt_close(int fd)
944 static int zt_setlinear(int zfd, int linear)
947 res = ioctl(zfd, ZT_SETLINEAR, &linear);
954 static int alloc_sub(struct zt_pvt *p, int x)
958 if (p->subs[x].zfd < 0) {
959 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
960 if (p->subs[x].zfd > -1) {
961 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
963 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
964 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
965 bi.numbufs = numbufs;
966 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
968 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
971 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
972 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
973 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
974 zt_close(p->subs[x].zfd);
979 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
982 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
985 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
989 static int unalloc_sub(struct zt_pvt *p, int x)
992 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
995 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
996 if (p->subs[x].zfd > -1) {
997 zt_close(p->subs[x].zfd);
1000 p->subs[x].linear = 0;
1001 p->subs[x].chan = 0;
1002 p->subs[x].owner = NULL;
1003 p->subs[x].inthreeway = 0;
1004 p->polarity = POLARITY_IDLE;
1005 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1009 static int zt_digit(struct ast_channel *ast, char digit)
1011 ZT_DIAL_OPERATION zo;
1016 ast_mutex_lock(&p->lock);
1017 index = zt_get_index(ast, p, 0);
1018 if ((index == SUB_REAL) && p->owner) {
1020 if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
1022 if (!pri_grab(p, p->pri)) {
1023 pri_information(p->pri->pri,p->call,digit);
1026 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1027 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
1028 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1029 res = strlen(p->dialdest);
1030 p->dialdest[res++] = digit;
1031 p->dialdest[res] = '\0';
1037 zo.op = ZT_DIAL_OP_APPEND;
1038 zo.dialstr[0] = 'T';
1039 zo.dialstr[1] = digit;
1041 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1042 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1047 ast_mutex_unlock(&p->lock);
1051 static char *events[] = {
1064 "Hook Transition Complete",
1069 "Polarity Reversal",
1077 { ZT_ALARM_RED, "Red Alarm" },
1078 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1079 { ZT_ALARM_BLUE, "Blue Alarm" },
1080 { ZT_ALARM_RECOVER, "Recovering" },
1081 { ZT_ALARM_LOOPBACK, "Loopback" },
1082 { ZT_ALARM_NOTOPEN, "Not Open" },
1083 { ZT_ALARM_NONE, "None" },
1086 static char *alarm2str(int alarm)
1089 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1090 if (alarms[x].alarm & alarm)
1091 return alarms[x].name;
1093 return alarm ? "Unknown Alarm" : "No Alarm";
1096 static char *event2str(int event)
1098 static char buf[256];
1099 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1100 return events[event];
1101 sprintf(buf, "Event %d", event); /* safe */
1106 static char *dialplan2str(int dialplan)
1108 if (dialplan == -1) {
1109 return("Dynamically set dialplan in ISDN");
1111 return (pri_plan2str(dialplan));
1115 static char *zap_sig2str(int sig)
1117 static char buf[256];
1120 return "E & M Immediate";
1122 return "E & M Wink";
1126 return "Feature Group D (DTMF)";
1128 return "Feature Group D (MF)";
1129 case SIG_FEATDMF_TA:
1130 return "Feature Groud D (MF) Tandem Access";
1132 return "Feature Group B (MF)";
1136 return "FGC/CAMA (Dialpulse)";
1137 case SIG_FGC_CAMAMF:
1138 return "FGC/CAMA (MF)";
1140 return "FXS Loopstart";
1142 return "FXS Groundstart";
1144 return "FXS Kewlstart";
1146 return "FXO Loopstart";
1148 return "FXO Groundstart";
1150 return "FXO Kewlstart";
1152 return "PRI Signalling";
1154 return "SF (Tone) Signalling Immediate";
1156 return "SF (Tone) Signalling Wink";
1158 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1159 case SIG_SF_FEATDMF:
1160 return "SF (Tone) Signalling with Feature Group D (MF)";
1162 return "SF (Tone) Signalling with Feature Group B (MF)";
1163 case SIG_GR303FXOKS:
1164 return "GR-303 Signalling with FXOKS";
1165 case SIG_GR303FXSKS:
1166 return "GR-303 Signalling with FXSKS";
1168 return "Pseudo Signalling";
1170 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1175 #define sig2str zap_sig2str
1177 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1179 /* If the conference already exists, and we're already in it
1180 don't bother doing anything */
1183 memset(&zi, 0, sizeof(zi));
1186 if (slavechannel > 0) {
1187 /* If we have only one slave, do a digital mon */
1188 zi.confmode = ZT_CONF_DIGITALMON;
1189 zi.confno = slavechannel;
1192 /* Real-side and pseudo-side both participate in conference */
1193 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1194 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1196 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1197 zi.confno = p->confno;
1199 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1203 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1204 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1207 if (slavechannel < 1) {
1208 p->confno = zi.confno;
1210 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1211 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1215 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1217 /* If they're listening to our channel, they're ours */
1218 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1220 /* If they're a talker on our (allocated) conference, they're ours */
1221 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1226 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1229 if (/* Can't delete if there's no zfd */
1231 /* Don't delete from the conference if it's not our conference */
1233 /* Don't delete if we don't think it's conferenced at all (implied) */
1235 memset(&zi, 0, sizeof(zi));
1239 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1240 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1243 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1244 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1248 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1252 struct zt_pvt *slave = NULL;
1253 /* Start out optimistic */
1255 /* Update conference state in a stateless fashion */
1256 for (x = 0; x < 3; x++) {
1257 /* Any three-way calling makes slave native mode *definitely* out
1259 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1262 /* If we don't have any 3-way calls, check to see if we have
1263 precisely one slave */
1264 if (useslavenative) {
1265 for (x = 0; x < MAX_SLAVES; x++) {
1268 /* Whoops already have a slave! No
1269 slave native and stop right away */
1274 /* We have one slave so far */
1275 slave = p->slaves[x];
1280 /* If no slave, slave native definitely out */
1283 else if (slave->law != p->law) {
1289 return useslavenative;
1292 static int reset_conf(struct zt_pvt *p)
1295 memset(&zi, 0, sizeof(zi));
1297 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1298 if (p->subs[SUB_REAL].zfd > -1) {
1299 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1300 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1305 static int update_conf(struct zt_pvt *p)
1310 struct zt_pvt *slave = NULL;
1312 useslavenative = isslavenative(p, &slave);
1313 /* Start with the obvious, general stuff */
1314 for (x = 0; x < 3; x++) {
1315 /* Look for three way calls */
1316 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1317 conf_add(p, &p->subs[x], x, 0);
1320 conf_del(p, &p->subs[x], x);
1323 /* If we have a slave, add him to our conference now. or DAX
1324 if this is slave native */
1325 for (x = 0; x < MAX_SLAVES; x++) {
1328 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1330 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1335 /* If we're supposed to be in there, do so now */
1336 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1338 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1340 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1344 /* If we have a master, add ourselves to his conference */
1346 if (isslavenative(p->master, NULL)) {
1347 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1349 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1353 /* Nobody is left (or should be left) in our conference.
1357 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1361 static void zt_enable_ec(struct zt_pvt *p)
1368 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1372 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1375 if (p->echocancel) {
1376 if (p->sig == SIG_PRI) {
1378 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1380 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1383 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1385 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1388 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1391 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1394 static void zt_train_ec(struct zt_pvt *p)
1398 if (p && p->echocancel && p->echotraining) {
1399 x = p->echotraining;
1400 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1402 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1404 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1407 ast_log(LOG_DEBUG, "No echo training requested\n");
1410 static void zt_disable_ec(struct zt_pvt *p)
1414 if (p->echocancel) {
1416 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1418 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1420 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1425 static void fill_txgain(struct zt_gains *g, float gain, int law)
1429 float linear_gain = pow(10.0, gain / 20.0);
1433 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1435 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1436 if (k > 32767) k = 32767;
1437 if (k < -32767) k = -32767;
1438 g->txgain[j] = AST_LIN2A(k);
1445 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1447 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1448 if (k > 32767) k = 32767;
1449 if (k < -32767) k = -32767;
1450 g->txgain[j] = AST_LIN2MU(k);
1459 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1463 float linear_gain = pow(10.0, gain / 20.0);
1467 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1469 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1470 if (k > 32767) k = 32767;
1471 if (k < -32767) k = -32767;
1472 g->rxgain[j] = AST_LIN2A(k);
1479 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1481 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1482 if (k > 32767) k = 32767;
1483 if (k < -32767) k = -32767;
1484 g->rxgain[j] = AST_LIN2MU(k);
1493 static int set_actual_txgain(int fd, int chan, float gain, int law)
1498 memset(&g, 0, sizeof(g));
1500 res = ioctl(fd, ZT_GETGAINS, &g);
1502 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1506 fill_txgain(&g, gain, law);
1508 return ioctl(fd, ZT_SETGAINS, &g);
1511 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1516 memset(&g, 0, sizeof(g));
1518 res = ioctl(fd, ZT_GETGAINS, &g);
1520 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1524 fill_rxgain(&g, gain, law);
1526 return ioctl(fd, ZT_SETGAINS, &g);
1529 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1531 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1534 static int bump_gains(struct zt_pvt *p)
1538 /* Bump receive gain by 5.0db */
1539 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
1541 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1548 static int restore_gains(struct zt_pvt *p)
1552 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1554 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1561 static inline int zt_set_hook(int fd, int hs)
1565 res = ioctl(fd, ZT_HOOK, &x);
1568 if (errno == EINPROGRESS) return 0;
1569 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1574 static inline int zt_confmute(struct zt_pvt *p, int muted)
1578 if (p->sig == SIG_PRI) {
1580 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1582 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1584 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1586 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1590 static int save_conference(struct zt_pvt *p)
1592 struct zt_confinfo c;
1594 if (p->saveconf.confmode) {
1595 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1598 p->saveconf.chan = 0;
1599 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1601 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1602 p->saveconf.confmode = 0;
1607 c.confmode = ZT_CONF_NORMAL;
1608 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1610 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1614 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1618 static int restore_conference(struct zt_pvt *p)
1621 if (p->saveconf.confmode) {
1622 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1623 p->saveconf.confmode = 0;
1625 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1630 ast_log(LOG_DEBUG, "Restored conferencing\n");
1634 static int send_callerid(struct zt_pvt *p);
1636 static int send_cwcidspill(struct zt_pvt *p)
1640 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1642 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1643 /* Make sure we account for the end */
1644 p->cidlen += READ_SIZE * 4;
1647 if (option_verbose > 2)
1648 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1652 static int has_voicemail(struct zt_pvt *p)
1655 return ast_app_has_voicemail(p->mailbox, NULL);
1658 static int send_callerid(struct zt_pvt *p)
1660 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1662 /* Take out of linear mode if necessary */
1663 if (p->subs[SUB_REAL].linear) {
1664 p->subs[SUB_REAL].linear = 0;
1665 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1667 while (p->cidpos < p->cidlen) {
1668 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1670 if (errno == EAGAIN)
1673 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1683 if (p->callwaitcas) {
1684 /* Wait for CID/CW to expire */
1685 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1687 restore_conference(p);
1691 static int zt_callwait(struct ast_channel *ast)
1693 struct zt_pvt *p = ast->tech_pvt;
1694 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1696 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1699 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1703 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1704 if (!p->callwaitrings && p->callwaitingcallerid) {
1705 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1707 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1709 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1711 p->cidlen = 2400 + READ_SIZE * 4;
1719 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1721 struct zt_pvt *p = ast->tech_pvt;
1722 int x, res, index,mysig;
1727 char dest[256]; /* must be same length as p->dialdest */
1728 ast_mutex_lock(&p->lock);
1729 ast_copy_string(dest, rdest, sizeof(dest));
1730 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1731 if ((ast->_state == AST_STATE_BUSY)) {
1732 p->subs[SUB_REAL].needbusy = 1;
1733 ast_mutex_unlock(&p->lock);
1736 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1737 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1738 ast_mutex_unlock(&p->lock);
1742 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
1744 /* Special pseudo -- automatically up */
1745 ast_setstate(ast, AST_STATE_UP);
1746 ast_mutex_unlock(&p->lock);
1749 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1750 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1752 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1755 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1759 mysig = p->outsigmod;
1765 if (p->owner == ast) {
1766 /* Normal ring, on hook */
1768 /* Don't send audio while on hook, until the call is answered */
1770 if (p->use_callerid) {
1771 /* Generate the Caller-ID spill if desired */
1773 ast_log(LOG_WARNING, "cidspill already exists??\n");
1777 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1778 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1783 /* Choose proper cadence */
1784 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1785 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
1786 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1787 p->cidrings = cidrings[p->distinctivering - 1];
1789 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1790 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1791 p->cidrings = p->sendcalleridafter;
1794 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1795 c = strchr(dest, '/');
1798 if (c && (strlen(c) < p->stripmsd)) {
1799 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1803 p->dop.op = ZT_DIAL_OP_REPLACE;
1804 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1805 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1807 p->dop.dialstr[0] = '\0';
1810 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1811 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1812 ast_mutex_unlock(&p->lock);
1817 /* Call waiting call */
1818 p->callwaitrings = 0;
1819 if (ast->cid.cid_num)
1820 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
1822 p->callwait_num[0] = '\0';
1823 if (ast->cid.cid_name)
1824 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
1826 p->callwait_name[0] = '\0';
1827 /* Call waiting tone instead */
1828 if (zt_callwait(ast)) {
1829 ast_mutex_unlock(&p->lock);
1832 /* Make ring-back */
1833 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1834 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1837 n = ast->cid.cid_name;
1838 l = ast->cid.cid_num;
1840 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1842 p->lastcid_num[0] = '\0';
1844 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1846 p->lastcid_name[0] = '\0';
1847 ast_setstate(ast, AST_STATE_RINGING);
1848 index = zt_get_index(ast, p, 0);
1850 p->subs[index].needringing = 1;
1863 case SIG_FGC_CAMAMF:
1868 case SIG_SF_FEATDMF:
1869 case SIG_FEATDMF_TA:
1871 c = strchr(dest, '/');
1876 if (strlen(c) < p->stripmsd) {
1877 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1878 ast_mutex_unlock(&p->lock);
1882 /* Start the trunk, if not GR-303 */
1886 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1888 if (errno != EINPROGRESS) {
1889 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1890 ast_mutex_unlock(&p->lock);
1897 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1898 p->dop.op = ZT_DIAL_OP_REPLACE;
1904 l = ast->cid.cid_num;
1906 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1908 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1911 l = ast->cid.cid_num;
1913 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1915 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1917 case SIG_FEATDMF_TA:
1919 const char *cic, *ozz;
1921 /* If you have to go through a Tandem Access point you need to use this */
1922 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1925 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1929 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1930 ast_mutex_unlock(&p->lock);
1933 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1934 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1939 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1942 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1944 case SIG_FGC_CAMAMF:
1946 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1950 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1952 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1956 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1957 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1958 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1959 p->echorest[sizeof(p->echorest) - 1] = '\0';
1961 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1965 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1967 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1968 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1969 ast_mutex_unlock(&p->lock);
1973 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1975 if (ast_strlen_zero(c))
1977 ast_setstate(ast, AST_STATE_DIALING);
1980 /* Special pseudo -- automatically up*/
1981 ast_setstate(ast, AST_STATE_UP);
1984 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
1985 p->dialdest[0] = '\0';
1988 ast_log(LOG_DEBUG, "not yet implemented\n");
1989 ast_mutex_unlock(&p->lock);
1995 #ifdef SUPPORT_USERUSER
1996 const char *useruser;
2000 int prilocaldialplan;
2004 c = strchr(dest, '/');
2009 if (!p->hidecallerid) {
2010 l = ast->cid.cid_num;
2011 n = ast->cid.cid_name;
2016 if (strlen(c) < p->stripmsd) {
2017 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2018 ast_mutex_unlock(&p->lock);
2021 if (mysig != SIG_FXSKS) {
2022 p->dop.op = ZT_DIAL_OP_REPLACE;
2023 s = strchr(c + p->stripmsd, 'w');
2026 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2028 p->dop.dialstr[0] = '\0';
2031 p->dop.dialstr[0] = '\0';
2034 if (pri_grab(p, p->pri)) {
2035 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2036 ast_mutex_unlock(&p->lock);
2039 if (!(p->call = pri_new_call(p->pri->pri))) {
2040 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2042 ast_mutex_unlock(&p->lock);
2045 if (!(sr = pri_sr_new())) {
2046 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2048 ast_mutex_unlock(&p->lock);
2050 if (p->bearer || (mysig == SIG_FXSKS)) {
2052 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);
2053 p->bearer->call = p->call;
2055 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
2056 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2058 p->digital = IS_DIGITAL(ast->transfercapability);
2059 /* Add support for exclusive override */
2060 if (p->priexclusive)
2063 /* otherwise, traditional behavior */
2064 if (p->pri->nodetype == PRI_NETWORK)
2070 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2071 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2073 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2074 if (p->pri->facilityenable)
2075 pri_facility_enable(p->pri->pri);
2077 if (option_verbose > 2)
2078 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2080 pridialplan = p->pri->dialplan - 1;
2081 if (pridialplan == -2) { /* compute dynamically */
2082 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2083 dp_strip = strlen(p->pri->internationalprefix);
2084 pridialplan = PRI_INTERNATIONAL_ISDN;
2085 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2086 dp_strip = strlen(p->pri->nationalprefix);
2087 pridialplan = PRI_NATIONAL_ISDN;
2089 pridialplan = PRI_LOCAL_ISDN;
2092 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2095 prilocaldialplan = p->pri->localdialplan - 1;
2096 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
2097 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2098 ldp_strip = strlen(p->pri->internationalprefix);
2099 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2100 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2101 ldp_strip = strlen(p->pri->nationalprefix);
2102 prilocaldialplan = PRI_NATIONAL_ISDN;
2104 prilocaldialplan = PRI_LOCAL_ISDN;
2107 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2108 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2109 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
2111 #ifdef SUPPORT_USERUSER
2112 /* User-user info */
2113 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2116 pri_sr_set_useruser(sr, useruser);
2119 if (pri_setup(p->pri->pri, p->call, sr)) {
2120 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2121 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2123 ast_mutex_unlock(&p->lock);
2128 ast_setstate(ast, AST_STATE_DIALING);
2132 ast_mutex_unlock(&p->lock);
2136 static void destroy_zt_pvt(struct zt_pvt **pvt)
2138 struct zt_pvt *p = *pvt;
2139 /* Remove channel from the list */
2141 p->prev->next = p->next;
2143 p->next->prev = p->prev;
2145 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2146 ast_mutex_destroy(&p->lock);
2151 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2161 for (i = 0; i < 3; i++) {
2162 if (cur->subs[i].owner) {
2168 prev->next = cur->next;
2170 prev->next->prev = prev;
2176 iflist->prev = NULL;
2180 if (cur->subs[SUB_REAL].zfd > -1) {
2181 zt_close(cur->subs[SUB_REAL].zfd);
2183 destroy_zt_pvt(&cur);
2187 prev->next = cur->next;
2189 prev->next->prev = prev;
2195 iflist->prev = NULL;
2199 if (cur->subs[SUB_REAL].zfd > -1) {
2200 zt_close(cur->subs[SUB_REAL].zfd);
2202 destroy_zt_pvt(&cur);
2208 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2210 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2212 static char *zap_send_keypad_facility_descrip =
2213 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2214 " IE over the current channel.\n";
2216 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2218 /* Data will be our digit string */
2220 char *digits = (char *) data;
2222 if (ast_strlen_zero(digits)) {
2223 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
2227 p = (struct zt_pvt *)chan->tech_pvt;
2230 ast_log(LOG_DEBUG, "Unable to find technology private\n");
2234 ast_mutex_lock(&p->lock);
2236 if (!p->pri || !p->call) {
2237 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2238 ast_mutex_unlock(&p->lock);
2242 if (!pri_grab(p, p->pri)) {
2243 pri_keypad_facility(p->pri->pri, p->call, digits);
2246 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
2247 ast_mutex_unlock(&p->lock);
2251 ast_mutex_unlock(&p->lock);
2256 static int pri_is_up(struct zt_pri *pri)
2259 for (x = 0; x < NUM_DCHANS; x++) {
2260 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2266 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2268 bearer->owner = &inuse;
2269 bearer->realcall = crv;
2270 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2271 if (crv->subs[SUB_REAL].owner)
2272 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2273 crv->bearer = bearer;
2274 crv->call = bearer->call;
2279 static char *pri_order(int level)
2289 return "Quaternary";
2295 /* Returns fd of the active dchan */
2296 static int pri_active_dchan_fd(struct zt_pri *pri)
2300 for (x = 0; x < NUM_DCHANS; x++) {
2301 if ((pri->dchans[x] == pri->pri))
2308 static int pri_find_dchan(struct zt_pri *pri)
2315 for (x = 0; x < NUM_DCHANS; x++) {
2316 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2318 if (pri->dchans[x] == old) {
2324 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2325 pri->dchannels[newslot]);
2327 if (old && (oldslot != newslot))
2328 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2329 pri->dchannels[oldslot], pri->dchannels[newslot]);
2330 pri->pri = pri->dchans[newslot];
2335 static int zt_hangup(struct ast_channel *ast)
2339 /*static int restore_gains(struct zt_pvt *p);*/
2340 struct zt_pvt *p = ast->tech_pvt;
2341 struct zt_pvt *tmp = NULL;
2342 struct zt_pvt *prev = NULL;
2346 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2347 if (!ast->tech_pvt) {
2348 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2352 ast_mutex_lock(&p->lock);
2354 index = zt_get_index(ast, p, 1);
2356 if (p->sig == SIG_PRI) {
2358 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2364 if (p->origcid_num) {
2365 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2366 free(p->origcid_num);
2367 p->origcid_num = NULL;
2369 if (p->origcid_name) {
2370 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2371 free(p->origcid_name);
2372 p->origcid_name = NULL;
2375 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2379 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2380 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2384 /* Real channel, do some fixup */
2385 p->subs[index].owner = NULL;
2386 p->subs[index].needanswer = 0;
2387 p->subs[index].needflash = 0;
2388 p->subs[index].needringing = 0;
2389 p->subs[index].needbusy = 0;
2390 p->subs[index].needcongestion = 0;
2391 p->subs[index].linear = 0;
2392 p->subs[index].needcallerid = 0;
2393 p->polarity = POLARITY_IDLE;
2394 zt_setlinear(p->subs[index].zfd, 0);
2395 if (index == SUB_REAL) {
2396 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2397 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2398 if (p->subs[SUB_CALLWAIT].inthreeway) {
2399 /* We had flipped over to answer a callwait and now it's gone */
2400 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2401 /* Move to the call-wait, but un-own us until they flip back. */
2402 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2403 unalloc_sub(p, SUB_CALLWAIT);
2406 /* The three way hung up, but we still have a call wait */
2407 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2408 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2409 unalloc_sub(p, SUB_THREEWAY);
2410 if (p->subs[SUB_REAL].inthreeway) {
2411 /* This was part of a three way call. Immediately make way for
2413 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2414 p->owner = p->subs[SUB_REAL].owner;
2416 /* This call hasn't been completed yet... Set owner to NULL */
2417 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2420 p->subs[SUB_REAL].inthreeway = 0;
2422 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2423 /* Move to the call-wait and switch back to them. */
2424 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2425 unalloc_sub(p, SUB_CALLWAIT);
2426 p->owner = p->subs[SUB_REAL].owner;
2427 if (p->owner->_state != AST_STATE_UP)
2428 p->subs[SUB_REAL].needanswer = 1;
2429 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2430 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2431 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2432 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2433 unalloc_sub(p, SUB_THREEWAY);
2434 if (p->subs[SUB_REAL].inthreeway) {
2435 /* This was part of a three way call. Immediately make way for
2437 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2438 p->owner = p->subs[SUB_REAL].owner;
2440 /* This call hasn't been completed yet... Set owner to NULL */
2441 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2444 p->subs[SUB_REAL].inthreeway = 0;
2446 } else if (index == SUB_CALLWAIT) {
2447 /* Ditch the holding callwait call, and immediately make it availabe */
2448 if (p->subs[SUB_CALLWAIT].inthreeway) {
2449 /* This is actually part of a three way, placed on hold. Place the third part
2450 on music on hold now */
2451 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2452 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2453 p->subs[SUB_THREEWAY].inthreeway = 0;
2454 /* Make it the call wait now */
2455 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2456 unalloc_sub(p, SUB_THREEWAY);
2458 unalloc_sub(p, SUB_CALLWAIT);
2459 } else if (index == SUB_THREEWAY) {
2460 if (p->subs[SUB_CALLWAIT].inthreeway) {
2461 /* The other party of the three way call is currently in a call-wait state.
2462 Start music on hold for them, and take the main guy out of the third call */
2463 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2464 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2465 p->subs[SUB_CALLWAIT].inthreeway = 0;
2467 p->subs[SUB_REAL].inthreeway = 0;
2468 /* If this was part of a three way call index, let us make
2469 another three way call */
2470 unalloc_sub(p, SUB_THREEWAY);
2472 /* This wasn't any sort of call, but how are we an index? */
2473 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2477 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2480 p->distinctivering = 0;
2481 p->confirmanswer = 0;
2487 p->onhooktime = time(NULL);
2495 ast_dsp_free(p->dsp);
2499 law = ZT_LAW_DEFAULT;
2500 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2502 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2503 /* Perform low level hangup if no owner left */
2506 #ifdef SUPPORT_USERUSER
2507 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2510 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2511 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2512 if (!pri_grab(p, p->pri)) {
2513 if (p->alreadyhungup) {
2514 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2516 #ifdef SUPPORT_USERUSER
2517 pri_call_set_useruser(p->call, useruser);
2520 pri_hangup(p->pri->pri, p->call, -1);
2523 p->bearer->call = NULL;
2525 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2526 int icause = ast->hangupcause ? ast->hangupcause : -1;
2527 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2529 #ifdef SUPPORT_USERUSER
2530 pri_call_set_useruser(p->call, useruser);
2533 p->alreadyhungup = 1;
2535 p->bearer->alreadyhungup = 1;
2538 icause = atoi(cause);
2540 pri_hangup(p->pri->pri, p->call, icause);
2543 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2546 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2551 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2557 if (p->sig && (p->sig != SIG_PRI))
2558 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2560 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2566 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2569 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2571 /* If they're off hook, try playing congestion */
2572 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
2573 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2575 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2581 /* Make sure we're not made available for at least two seconds assuming
2582 we were actually used for an inbound or outbound call. */
2583 if (ast->_state != AST_STATE_RESERVED) {
2584 time(&p->guardtime);
2589 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2596 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2597 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2601 p->callwaiting = p->permcallwaiting;
2602 p->hidecallerid = p->permhidecallerid;
2607 /* Restore data mode */
2608 if (p->sig == SIG_PRI) {
2610 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2614 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2615 /* Free up the bearer channel as well, and
2616 don't use its file descriptor anymore */
2617 update_conf(p->bearer);
2618 reset_conf(p->bearer);
2619 p->bearer->owner = NULL;
2620 p->bearer->realcall = NULL;
2622 p->subs[SUB_REAL].zfd = -1;
2629 p->callwaitingrepeat = 0;
2632 ast->tech_pvt = NULL;
2633 ast_mutex_unlock(&p->lock);
2634 ast_mutex_lock(&usecnt_lock);
2637 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2638 ast_mutex_unlock(&usecnt_lock);
2639 ast_update_use_count();
2640 if (option_verbose > 2)
2641 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2643 ast_mutex_lock(&iflock);
2649 destroy_channel(prev, tmp, 0);
2657 ast_mutex_unlock(&iflock);
2661 static int zt_answer(struct ast_channel *ast)
2663 struct zt_pvt *p = ast->tech_pvt;
2666 int oldstate = ast->_state;
2667 ast_setstate(ast, AST_STATE_UP);
2668 ast_mutex_lock(&p->lock);
2669 index = zt_get_index(ast, p, 0);
2672 /* nothing to do if a radio channel */
2673 if ((p->radio || (p->oprmode < 0))) {
2674 ast_mutex_unlock(&p->lock);
2688 case SIG_FEATDMF_TA:
2691 case SIG_FGC_CAMAMF:
2696 case SIG_SF_FEATDMF:
2701 /* Pick up the line */
2702 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2703 if (p->hanguponpolarityswitch) {
2704 gettimeofday(&p->polaritydelaytv, NULL);
2706 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2707 tone_zone_play_tone(p->subs[index].zfd, -1);
2709 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2710 if (oldstate == AST_STATE_RINGING) {
2711 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2712 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2713 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2714 p->owner = p->subs[SUB_REAL].owner;
2717 if (p->sig & __ZT_SIG_FXS) {
2724 /* Send a pri acknowledge */
2725 if (!pri_grab(p, p->pri)) {
2727 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2730 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2736 ast_mutex_unlock(&p->lock);
2739 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2742 ast_mutex_unlock(&p->lock);
2746 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2752 struct zt_pvt *p = chan->tech_pvt, *pp;
2753 struct oprmode *oprmode;
2756 /* all supported options require data */
2757 if (!data || (datalen < 1)) {
2763 case AST_OPTION_TXGAIN:
2764 scp = (signed char *) data;
2765 index = zt_get_index(chan, p, 0);
2767 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
2770 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
2771 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
2772 case AST_OPTION_RXGAIN:
2773 scp = (signed char *) data;
2774 index = zt_get_index(chan, p, 0);
2776 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
2779 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
2780 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
2781 case AST_OPTION_TONE_VERIFY:
2787 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2788 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2791 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2792 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2795 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2796 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2800 case AST_OPTION_TDD:
2801 /* turn on or off TDD */
2804 if (!*cp) { /* turn it off */
2805 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2811 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
2812 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
2814 /* otherwise, turn it on */
2815 if (!p->didtdd) { /* if havent done it yet */
2816 unsigned char mybuf[41000], *buf;
2817 int size, res, fd, len;
2818 struct pollfd fds[1];
2821 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
2822 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
2824 index = zt_get_index(chan, p, 0);
2826 ast_log(LOG_WARNING, "No index in TDD?\n");
2829 fd = p->subs[index].zfd;
2831 if (ast_check_hangup(chan))
2834 if (size > READ_SIZE)
2837 fds[0].events = POLLPRI | POLLOUT;
2839 res = poll(fds, 1, -1);
2841 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2844 /* if got exception */
2845 if (fds[0].revents & POLLPRI)
2847 if (!(fds[0].revents & POLLOUT)) {
2848 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2851 res = write(fd, buf, size);
2853 if (res == -1) return -1;
2854 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2860 p->didtdd = 1; /* set to have done it now */
2862 if (*cp == 2) { /* Mate mode */
2869 if (!p->tdd) { /* if we dont have one yet */
2870 p->tdd = tdd_new(); /* allocate one */
2873 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2877 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
2878 *cp ? "ON" : "OFF", (int) *cp, chan->name);
2879 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
2881 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2884 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
2888 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
2891 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2892 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2894 case AST_OPTION_OPRMODE: /* Operator services mode */
2895 oprmode = (struct oprmode *) data;
2896 pp = oprmode->peer->tech_pvt;
2897 p->oprmode = pp->oprmode = 0;
2901 /* setup modes, if any */
2904 pp->oprmode = oprmode->mode;
2905 p->oprmode = -oprmode->mode;
2907 ast_log(LOG_DEBUG, "Set Operator Services mode, value: %d on %s/%s\n",
2908 oprmode->mode, chan->name,oprmode->peer->name);;
2910 case AST_OPTION_ECHOCAN:
2913 ast_log(LOG_DEBUG, "Enabling echo cancelation on %s\n", chan->name);
2916 ast_log(LOG_DEBUG, "Disabling echo cancelation on %s\n", chan->name);
2926 static int zt_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
2928 struct zt_pvt *p = chan->tech_pvt;
2930 if (!strcasecmp(data, "rxgain")) {
2931 ast_mutex_lock(&p->lock);
2932 snprintf(buf, len, "%f", p->rxgain);
2933 ast_mutex_unlock(&p->lock);
2934 } else if (!strcasecmp(data, "txgain")) {
2935 ast_mutex_lock(&p->lock);
2936 snprintf(buf, len, "%f", p->txgain);
2937 ast_mutex_unlock(&p->lock);
2939 ast_copy_string(buf, "", len);
2945 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2947 /* Unlink a specific slave or all slaves/masters from a given master */
2953 ast_mutex_lock(&master->lock);
2955 while (ast_mutex_trylock(&slave->lock)) {
2956 ast_mutex_unlock(&master->lock);
2958 ast_mutex_lock(&master->lock);
2963 for (x = 0; x < MAX_SLAVES; x++) {
2964 if (master->slaves[x]) {
2965 if (!slave || (master->slaves[x] == slave)) {
2966 /* Take slave out of the conference */
2967 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2968 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2969 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2970 master->slaves[x]->master = NULL;
2971 master->slaves[x] = NULL;
2976 master->inconference = 0;
2979 if (master->master) {
2980 /* Take master out of the conference */
2981 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2982 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2984 for (x = 0; x < MAX_SLAVES; x++) {
2985 if (master->master->slaves[x] == master)
2986 master->master->slaves[x] = NULL;
2987 else if (master->master->slaves[x])
2991 master->master->inconference = 0;
2993 master->master = NULL;
2995 update_conf(master);
2998 ast_mutex_unlock(&slave->lock);
2999 ast_mutex_unlock(&master->lock);
3003 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
3005 if (!slave || !master) {
3006 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
3009 for (x = 0; x < MAX_SLAVES; x++) {
3010 if (!master->slaves[x]) {
3011 master->slaves[x] = slave;
3015 if (x >= MAX_SLAVES) {
3016 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
3017 master->slaves[MAX_SLAVES - 1] = slave;
3020 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
3021 slave->master = master;
3023 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
3026 static void disable_dtmf_detect(struct zt_pvt *p)
3028 #ifdef ZT_TONEDETECT
3034 #ifdef ZT_TONEDETECT
3036 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3038 if (!p->hardwaredtmf && p->dsp) {
3039 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
3040 ast_dsp_set_features(p->dsp, p->dsp_features);
3044 static void enable_dtmf_detect(struct zt_pvt *p)
3046 #ifdef ZT_TONEDETECT
3050 if (p->channel == CHAN_PSEUDO)
3055 #ifdef ZT_TONEDETECT
3056 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
3057 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3059 if (!p->hardwaredtmf && p->dsp) {
3060 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
3061 ast_dsp_set_features(p->dsp, p->dsp_features);
3065 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)
3067 struct ast_channel *who;
3068 struct zt_pvt *p0, *p1, *op0, *op1;
3069 struct zt_pvt *master = NULL, *slave = NULL;
3070 struct ast_frame *f;
3074 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
3075 int os0 = -1, os1 = -1;
3077 struct ast_channel *oc0, *oc1;
3078 enum ast_bridge_result res;
3081 int triedtopribridge = 0;
3082 q931_call *q931c0 = NULL, *q931c1 = NULL;
3085 /* For now, don't attempt to native bridge if either channel needs DTMF detection.
3086 There is code below to handle it properly until DTMF is actually seen,
3087 but due to currently unresolved issues it's ignored...
3090 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
3091 return AST_BRIDGE_FAILED_NOWARN;
3093 ast_mutex_lock(&c0->lock);
3094 ast_mutex_lock(&c1->lock);
3098 /* cant do pseudo-channels here */
3099 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
3100 ast_mutex_unlock(&c0->lock);
3101 ast_mutex_unlock(&c1->lock);
3102 return AST_BRIDGE_FAILED_NOWARN;
3105 oi0 = zt_get_index(c0, p0, 0);
3106 oi1 = zt_get_index(c1, p1, 0);
3107 if ((oi0 < 0) || (oi1 < 0)) {
3108 ast_mutex_unlock(&c0->lock);
3109 ast_mutex_unlock(&c1->lock);
3110 return AST_BRIDGE_FAILED;
3113 op0 = p0 = c0->tech_pvt;
3114 op1 = p1 = c1->tech_pvt;
3120 ast_mutex_lock(&p0->lock);
3121 if (ast_mutex_trylock(&p1->lock)) {
3122 /* Don't block, due to potential for deadlock */
3123 ast_mutex_unlock(&p0->lock);
3124 ast_mutex_unlock(&c0->lock);
3125 ast_mutex_unlock(&c1->lock);
3126 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
3127 return AST_BRIDGE_RETRY;
3130 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
3131 if (p0->owner && p1->owner) {
3132 /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
3133 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
3137 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
3142 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
3143 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
3145 oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
3146 p0->subs[SUB_REAL].inthreeway, p0->channel,
3147 oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
3148 p1->subs[SUB_REAL].inthreeway);
3152 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
3153 if (p1->subs[SUB_THREEWAY].inthreeway) {
3158 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
3159 if (p0->subs[SUB_THREEWAY].inthreeway) {
3164 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
3165 /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise,
3166 don't put us in anything */
3167 if (p1->subs[SUB_CALLWAIT].inthreeway) {
3172 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
3173 /* Same as previous */
3174 if (p0->subs[SUB_CALLWAIT].inthreeway) {
3180 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
3181 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
3182 if (master && slave) {
3183 /* Stop any tones, or play ringtone as appropriate. If they're bridged
3184 in an active threeway call with a channel that is ringing, we should
3185 indicate ringing. */
3186 if ((oi1 == SUB_THREEWAY) &&
3187 p1->subs[SUB_THREEWAY].inthreeway &&
3188 p1->subs[SUB_REAL].owner &&
3189 p1->subs[SUB_REAL].inthreeway &&
3190 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
3191 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
3192 tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE);
3193 os1 = p1->subs[SUB_REAL].owner->_state;
3195 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
3196 tone_zone_play_tone(p0->subs[oi0].zfd, -1);
3198 if ((oi0 == SUB_THREEWAY) &&
3199 p0->subs[SUB_THREEWAY].inthreeway &&
3200 p0->subs[SUB_REAL].owner &&
3201 p0->subs[SUB_REAL].inthreeway &&
3202 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
3203 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
3204 tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE);
3205 os0 = p0->subs[SUB_REAL].owner->_state;
3207 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
3208 tone_zone_play_tone(p1->subs[oi0].zfd, -1);
3210 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
3211 if (!p0->echocanbridged || !p1->echocanbridged) {
3212 /* Disable echo cancellation if appropriate */
3217 zt_link(slave, master);
3218 master->inconference = inconf;
3219 } else if (!nothingok)
3220 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
3224 t0 = p0->subs[SUB_REAL].inthreeway;
3225 t1 = p1->subs[SUB_REAL].inthreeway;
3227 ast_mutex_unlock(&p0->lock);
3228 ast_mutex_unlock(&p1->lock);
3230 ast_mutex_unlock(&c0->lock);
3231 ast_mutex_unlock(&c1->lock);
3233 /* Native bridge failed */
3234 if ((!master || !slave) && !nothingok) {
3237 return AST_BRIDGE_FAILED;
3240 if (option_verbose > 2)
3241 ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
3243 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
3244 disable_dtmf_detect(op0);
3246 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
3247 disable_dtmf_detect(op1);
3250 struct ast_channel *c0_priority[2] = {c0, c1};
3251 struct ast_channel *c1_priority[2] = {c1, c0};
3253 /* Here's our main loop... Start by locking things, looking for private parts,
3254 and then balking if anything is wrong */
3255 ast_mutex_lock(&c0->lock);
3256 ast_mutex_lock(&c1->lock);
3261 i0 = zt_get_index(c0, p0, 1);
3263 i1 = zt_get_index(c1, p1, 1);
3264 ast_mutex_unlock(&c0->lock);
3265 ast_mutex_unlock(&c1->lock);
3270 (ofd0 != c0->fds[0]) ||
3271 (ofd1 != c1->fds[0]) ||
3272 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
3273 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
3274 (oc0 != p0->owner) ||
3275 (oc1 != p1->owner) ||
3276 (t0 != p0->subs[SUB_REAL].inthreeway) ||
3277 (t1 != p1->subs[SUB_REAL].inthreeway) ||
3280 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
3281 op0->channel, oi0, op1->channel, oi1);
3282 res = AST_BRIDGE_RETRY;
3283 goto return_from_bridge;
3289 if (p0->transfer && p1->transfer
3291 && !triedtopribridge) {
3292 pri_channel_bridge(q931c0, q931c1);
3293 triedtopribridge = 1;
3297 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
3299 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
3303 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
3306 res = AST_BRIDGE_COMPLETE;
3307 goto return_from_bridge;
3309 if (f->frametype == AST_FRAME_DTMF) {
3310 if ((who == c0) && p0->pulsedial) {
3312 } else if ((who == c1) && p1->pulsedial) {
3317 res = AST_BRIDGE_COMPLETE;
3318 goto return_from_bridge;
3323 /* Swap who gets priority */
3324 priority = !priority;
3334 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
3335 enable_dtmf_detect(op0);
3337 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
3338 enable_dtmf_detect(op1);
3340 zt_unlink(slave, master, 1);
3345 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
3347 struct zt_pvt *p = newchan->tech_pvt;
3349 ast_mutex_lock(&p->lock);
3350 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
3351 if (p->owner == oldchan) {
3354 for (x = 0; x < 3; x++)
3355 if (p->subs[x].owner == oldchan) {
3357 zt_unlink(NULL, p, 0);
3358 p->subs[x].owner = newchan;
3360 if (newchan->_state == AST_STATE_RINGING)
3361 zt_indicate(newchan, AST_CONTROL_RINGING, NULL, 0);
3363 ast_mutex_unlock(&p->lock);
3367 static int zt_ring_phone(struct zt_pvt *p)
3371 /* Make sure our transmit state is on hook */
3374 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
3377 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
3379 printf("Res: %d, error: %s\n", res, strerror(errno));
3385 /* Wait just in case */
3392 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
3400 static void *ss_thread(void *data);
3402 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
3404 static int attempt_transfer(struct zt_pvt *p)
3406 /* In order to transfer, we need at least one of the channels to
3407 actually be in a call bridge. We can't conference two applications
3408 together (but then, why would we want to?) */
3409 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
3410 /* The three-way person we're about to transfer to could still be in MOH, so
3411 stop if now if appropriate */
3412 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
3413 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
3414 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
3415 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
3417 if (p->subs[SUB_REAL].owner->cdr) {
3418 /* Move CDR from second channel to current one */
3419 p->subs[SUB_THREEWAY].owner->cdr =
3420 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
3421 p->subs[SUB_REAL].owner->cdr = NULL;
3423 if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
3424 /* Move CDR from second channel's bridge to current one */
3425 p->subs[SUB_THREEWAY].owner->cdr =
3426 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
3427 ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
3429 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
3430 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
3431 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
3434 /* Orphan the channel after releasing the lock */
3435 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
3436 unalloc_sub(p, SUB_THREEWAY);
3437 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
3438 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
3439 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
3441 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
3442 if (p->subs[SUB_THREEWAY].owner->cdr) {
3443 /* Move CDR from second channel to current one */
3444 p->subs[SUB_REAL].owner->cdr =
3445 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
3446 p->subs[SUB_THREEWAY].owner->cdr = NULL;