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_vldtmf</depend>
42 <depend>zaptel</depend>
43 <depend>tonezone</depend>
50 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
58 #include <sys/signal.h>
62 #if !defined(SOLARIS) && !defined(__FreeBSD__)
66 #include <sys/ioctl.h>
69 #include "asterisk/zapata.h"
79 #include "asterisk/lock.h"
80 #include "asterisk/channel.h"
81 #include "asterisk/config.h"
82 #include "asterisk/logger.h"
83 #include "asterisk/module.h"
84 #include "asterisk/pbx.h"
85 #include "asterisk/options.h"
86 #include "asterisk/file.h"
87 #include "asterisk/ulaw.h"
88 #include "asterisk/alaw.h"
89 #include "asterisk/callerid.h"
90 #include "asterisk/adsi.h"
91 #include "asterisk/cli.h"
92 #include "asterisk/cdr.h"
93 #include "asterisk/features.h"
94 #include "asterisk/musiconhold.h"
95 #include "asterisk/say.h"
96 #include "asterisk/tdd.h"
97 #include "asterisk/app.h"
98 #include "asterisk/dsp.h"
99 #include "asterisk/astdb.h"
100 #include "asterisk/manager.h"
101 #include "asterisk/causes.h"
102 #include "asterisk/term.h"
103 #include "asterisk/utils.h"
104 #include "asterisk/transcap.h"
105 #include "asterisk/stringfields.h"
106 #include "asterisk/abstract_jb.h"
107 #include "asterisk/smdi.h"
108 #include "asterisk/astobj.h"
109 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
111 /*! Global jitterbuffer configuration - by default, jb is disabled */
112 static struct ast_jb_conf default_jbconf =
116 .resync_threshold = -1,
119 static struct ast_jb_conf global_jbconf;
121 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_PRI) && !defined(ZT_SIG_HARDHDLC))
122 #error "Your zaptel is too old. Please update"
125 #ifndef ZT_TONEDETECT
126 /* Work around older code with no tone detect */
127 #define ZT_EVENT_DTMFDOWN 0
128 #define ZT_EVENT_DTMFUP 0
131 /* define this to send PRI user-user information elements */
132 #undef SUPPORT_USERUSER
135 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
136 * the user hangs up to reset the state machine so ring works properly.
137 * This is used to be able to support kewlstart by putting the zhone in
138 * groundstart mode since their forward disconnect supervision is entirely
139 * broken even though their documentation says it isn't and their support
140 * is entirely unwilling to provide any assistance with their channel banks
141 * even though their web site says they support their products for life.
143 /* #define ZHONE_HACK */
146 * Define if you want to check the hook state for an FXO (FXS signalled) interface
147 * before dialing on it. Certain FXO interfaces always think they're out of
148 * service with this method however.
150 /* #define ZAP_CHECK_HOOKSTATE */
152 /*! \brief Typically, how many rings before we should send Caller*ID */
153 #define DEFAULT_CIDRINGS 1
155 #define CHANNEL_PSEUDO -12
157 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
159 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
160 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FGC_CAMA) || ((p)->sig == SIG_FGC_CAMAMF) || ((p)->sig == SIG_FEATB))
162 static const char tdesc[] = "Zapata Telephony Driver"
171 static const char config[] = "zapata.conf";
173 #define SIG_EM ZT_SIG_EM
174 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
175 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
176 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
177 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
178 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
179 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
180 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
181 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
182 #define SIG_FXSLS ZT_SIG_FXSLS
183 #define SIG_FXSGS ZT_SIG_FXSGS
184 #define SIG_FXSKS ZT_SIG_FXSKS
185 #define SIG_FXOLS ZT_SIG_FXOLS
186 #define SIG_FXOGS ZT_SIG_FXOGS
187 #define SIG_FXOKS ZT_SIG_FXOKS
188 #define SIG_PRI ZT_SIG_CLEAR
189 #define SIG_SS7 (0x1000000 | ZT_SIG_CLEAR)
190 #define SIG_SF ZT_SIG_SF
191 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
192 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
193 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
194 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
195 #define SIG_EM_E1 ZT_SIG_EM_E1
196 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
197 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
200 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
201 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
203 #define CHAN_PSEUDO -2
205 #define DCHAN_PROVISIONED (1 << 0)
206 #define DCHAN_NOTINALARM (1 << 1)
207 #define DCHAN_UP (1 << 2)
209 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
211 static char defaultcic[64] = "";
212 static char defaultozz[64] = "";
214 static char language[MAX_LANGUAGE] = "";
215 static char progzone[10] = "";
217 static int usedistinctiveringdetection = 0;
218 static int distinctiveringaftercid = 0;
220 static int numbufs = 4;
223 static struct ast_channel inuse;
224 #ifdef PRI_GETSET_TIMERS
225 static int pritimers[PRI_MAX_TIMERS];
227 static int pridebugfd = -1;
228 static char pridebugfilename[1024] = "";
231 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
232 static int firstdigittimeout = 16000;
234 /*! \brief How long to wait for following digits (FXO logic) */
235 static int gendigittimeout = 8000;
237 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
238 static int matchdigittimeout = 3000;
240 /*! \brief Protect the interface list (of zt_pvt's) */
241 AST_MUTEX_DEFINE_STATIC(iflock);
244 static int ifcount = 0;
247 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
250 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
251 when it's doing something critical. */
252 AST_MUTEX_DEFINE_STATIC(monlock);
254 /*! \brief This is the thread for the monitor which checks for input on the channels
255 which are not currently in use. */
256 static pthread_t monitor_thread = AST_PTHREADT_NULL;
258 static int restart_monitor(void);
260 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);
262 static int zt_sendtext(struct ast_channel *c, const char *text);
264 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
265 static inline int zt_get_event(int fd)
268 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
273 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
274 static inline int zt_wait_event(int fd)
277 i = ZT_IOMUX_SIGEVENT;
278 if (ioctl(fd, ZT_IOMUX, &i) == -1)
280 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
285 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
286 #define READ_SIZE 160
288 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
289 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
291 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
292 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
293 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
294 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
295 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
299 static int ringt_base = DEFAULT_RINGT;
303 #define LINKSTATE_INALARM (1 << 0)
304 #define LINKSTATE_STARTING (1 << 1)
305 #define LINKSTATE_UP (1 << 2)
306 #define LINKSTATE_DOWN (1 << 3)
309 pthread_t master; /*!< Thread of master */
313 int linkstate[NUM_DCHANS];
317 LINKSET_STATE_DOWN = 0,
321 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
324 static struct zt_ss7 linksets[NUM_SPANS];
326 static int cur_ss7type = -1;
327 static int cur_linkset = -1;
328 static int cur_pointcode = -1;
329 static int cur_cicbeginswith = -1;
330 static int cur_adjpointcode = -1;
331 static int cur_networkindicator = -1;
332 static int cur_defaultdpc = -1;
333 #endif /* HAVE_SS7 */
337 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
338 #define PRI_CHANNEL(p) ((p) & 0xff)
339 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
340 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
343 pthread_t master; /*!< Thread of master */
344 ast_mutex_t lock; /*!< Mutex */
345 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
346 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
347 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
348 int minunused; /*!< Min # of channels to keep empty */
349 int minidle; /*!< Min # of "idling" calls to keep active */
350 int nodetype; /*!< Node type */
351 int switchtype; /*!< Type of switch to emulate */
352 int nsf; /*!< Network-Specific Facilities */
353 int dialplan; /*!< Dialing plan */
354 int localdialplan; /*!< Local dialing plan */
355 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
356 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
357 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
358 char privateprefix[20]; /*!< for private dialplans */
359 char unknownprefix[20]; /*!< for unknown dialplans */
360 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
361 int trunkgroup; /*!< What our trunkgroup is */
362 int mastertrunkgroup; /*!< What trunk group is our master */
363 int prilogicalspan; /*!< Logical span number within trunk group */
364 int numchans; /*!< Num of channels we represent */
365 int overlapdial; /*!< In overlap dialing mode */
366 int facilityenable; /*!< Enable facility IEs */
367 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
368 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
369 struct pri *pri; /*!< Currently active D-channel */
371 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
376 time_t lastreset; /*!< time when unused channels were last reset */
377 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
378 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
379 struct zt_pvt *crvs; /*!< Member CRV structs */
380 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
384 static struct zt_pri pris[NUM_SPANS];
387 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
389 #define DEFAULT_PRI_DEBUG 0
392 static inline void pri_rel(struct zt_pri *pri)
394 ast_mutex_unlock(&pri->lock);
398 /*! Shut up the compiler */
402 #define SUB_REAL 0 /*!< Active call */
403 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
404 #define SUB_THREEWAY 2 /*!< Three-way call */
406 /* Polarity states */
407 #define POLARITY_IDLE 0
408 #define POLARITY_REV 1
411 static struct zt_distRings drings;
413 struct distRingData {
417 struct ringContextData {
418 char contextData[AST_MAX_CONTEXT];
420 struct zt_distRings {
421 struct distRingData ringnum[3];
422 struct ringContextData ringContext[3];
425 static char *subnames[] = {
431 struct zt_subchannel {
433 struct ast_channel *owner;
435 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
436 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
437 unsigned int needringing:1;
438 unsigned int needbusy:1;
439 unsigned int needcongestion:1;
440 unsigned int needcallerid:1;
441 unsigned int needanswer:1;
442 unsigned int needflash:1;
443 unsigned int needhold:1;
444 unsigned int needunhold:1;
445 unsigned int linear:1;
446 unsigned int inthreeway:1;
450 #define CONF_USER_REAL (1 << 0)
451 #define CONF_USER_THIRDCALL (1 << 1)
455 static struct zt_pvt {
457 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
458 /*!< Up to three channels can be associated with this call */
460 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
461 struct zt_subchannel subs[3]; /*!< Sub-channels */
462 struct zt_confinfo saveconf; /*!< Saved conference info */
464 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
465 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
466 int inconference; /*!< If our real should be in the conference */
468 int sig; /*!< Signalling style */
469 int radio; /*!< radio type */
470 int outsigmod; /*!< Outbound Signalling style (modifier) */
471 int oprmode; /*!< "Operator Services" mode */
472 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
475 int tonezone; /*!< tone zone for this chan, or -1 for default */
476 struct zt_pvt *next; /*!< Next channel in list */
477 struct zt_pvt *prev; /*!< Prev channel in list */
481 unsigned int answeronpolarityswitch:1;
482 unsigned int busydetect:1;
483 unsigned int callreturn:1;
484 unsigned int callwaiting:1;
485 unsigned int callwaitingcallerid:1;
486 unsigned int cancallforward:1;
487 unsigned int canpark:1;
488 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
489 unsigned int destroy:1;
490 unsigned int didtdd:1; /*!< flag to say its done it once */
491 unsigned int dialednone:1;
492 unsigned int dialing:1;
493 unsigned int digital:1;
495 unsigned int echobreak:1;
496 unsigned int echocanbridged:1;
497 unsigned int echocanon:1;
498 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
499 unsigned int firstradio:1;
500 unsigned int hanguponpolarityswitch:1;
501 unsigned int hardwaredtmf:1;
502 unsigned int hidecallerid:1;
503 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
504 unsigned int ignoredtmf:1;
505 unsigned int immediate:1; /*!< Answer before getting digits? */
506 unsigned int inalarm:1;
507 unsigned int mate:1; /*!< flag to say its in MATE mode */
508 unsigned int outgoing:1;
509 unsigned int overlapdial:1;
510 unsigned int permcallwaiting:1;
511 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
512 unsigned int priindication_oob:1;
513 unsigned int priexclusive:1;
514 unsigned int pulse:1;
515 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
516 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
517 unsigned int threewaycalling:1;
518 unsigned int transfer:1;
519 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
520 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
521 unsigned int usedistinctiveringdetection:1;
522 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
523 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
524 /* Channel state or unavilability flags */
525 unsigned int inservice:1;
526 unsigned int locallyblocked:1;
527 unsigned int remotelyblocked:1;
528 #if defined(HAVE_PRI)
529 unsigned int alerting:1;
530 unsigned int alreadyhungup:1;
531 unsigned int isidlecall:1;
532 unsigned int proceeding:1;
533 unsigned int progress:1;
534 unsigned int resetting:1;
535 unsigned int setup_ack:1;
537 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
538 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
540 struct zt_distRings drings;
542 char context[AST_MAX_CONTEXT];
543 char defcontext[AST_MAX_CONTEXT];
544 char exten[AST_MAX_EXTENSION];
545 char language[MAX_LANGUAGE];
546 char mohinterpret[MAX_MUSICCLASS];
547 char mohsuggest[MAX_MUSICCLASS];
549 char cid_ani[AST_MAX_EXTENSION];
551 char cid_num[AST_MAX_EXTENSION];
552 int cid_ton; /*!< Type Of Number (TON) */
553 char cid_name[AST_MAX_EXTENSION];
554 char lastcid_num[AST_MAX_EXTENSION];
555 char lastcid_name[AST_MAX_EXTENSION];
556 char *origcid_num; /*!< malloced original callerid */
557 char *origcid_name; /*!< malloced original callerid */
558 char callwait_num[AST_MAX_EXTENSION];
559 char callwait_name[AST_MAX_EXTENSION];
560 char rdnis[AST_MAX_EXTENSION];
561 char dnid[AST_MAX_EXTENSION];
564 int confno; /*!< Our conference */
565 int confusers; /*!< Who is using our conference */
566 int propconfno; /*!< Propagated conference number */
567 ast_group_t callgroup;
568 ast_group_t pickupgroup;
569 int channel; /*!< Channel Number or CRV */
570 int span; /*!< Span number */
571 time_t guardtime; /*!< Must wait this much time before using for new call */
572 int cid_signalling; /*!< CID signalling type bell202 or v23 */
573 int cid_start; /*!< CID start indicator, polarity or ring */
574 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
575 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
576 int cidcwexpire; /*!< When to expire our muting for CID/CW */
577 unsigned char *cidspill;
590 int busy_quietlength;
592 struct timeval flashtime; /*!< Last flash-hook time */
594 int cref; /*!< Call reference number */
595 ZT_DIAL_OPERATION dop;
596 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
598 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
599 int amaflags; /*!< AMA Flags */
600 struct tdd_state *tdd; /*!< TDD flag */
601 char call_forward[AST_MAX_EXTENSION];
602 char mailbox[AST_MAX_EXTENSION];
606 int distinctivering; /*!< Which distinctivering to use */
607 int cidrings; /*!< Which ring to deliver CID on */
608 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
610 int polarityonanswerdelay;
611 struct timeval polaritydelaytv;
612 int sendcalleridafter;
615 struct zt_pvt *bearer;
616 struct zt_pvt *realcall;
625 struct isup_call *ss7call;
627 int cic; /*!< CIC associated with channel */
630 } *iflist = NULL, *ifend = NULL;
632 /*! \brief Channel configuration from zapata.conf .
633 * This struct is used for parsing the [channels] section of zapata.conf.
634 * Generally there is a field here for every possible configuration item.
636 * The state of fields is saved along the parsing and whenever a 'channel'
637 * statement is reached, the current zt_chan_conf is used to configure the
638 * channel (struct zt_pvt)
640 * \see zt_chan_init for the default values.
642 struct zt_chan_conf {
649 char smdi_port[SMDI_MAX_FILENAME_LEN];
652 /** returns a new zt_chan_conf with default values (by-value) */
653 static struct zt_chan_conf zt_chan_conf_default(void) {
654 /* recall that if a field is not included here it is initialized
657 struct zt_chan_conf conf = {
661 .switchtype = PRI_SWITCH_NI2,
662 .dialplan = PRI_NATIONAL_ISDN + 1,
663 .localdialplan = PRI_NATIONAL_ISDN + 1,
669 .internationalprefix = "",
670 .nationalprefix = "",
675 .resetinterval = 3600
679 .context = "default",
682 .mohinterpret = "default",
686 .cid_signalling = CID_SIG_BELL,
687 .cid_start = CID_START_RING,
704 .polarityonanswerdelay = 600,
706 .sendcalleridafter = DEFAULT_CIDRINGS
718 .smdi_port = "/dev/ttyS0",
725 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
726 static int zt_digit_begin(struct ast_channel *ast, char digit);
727 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
728 static int zt_sendtext(struct ast_channel *c, const char *text);
729 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
730 static int zt_hangup(struct ast_channel *ast);
731 static int zt_answer(struct ast_channel *ast);
732 static struct ast_frame *zt_read(struct ast_channel *ast);
733 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
734 static struct ast_frame *zt_exception(struct ast_channel *ast);
735 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
736 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
737 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
738 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
740 static const struct ast_channel_tech zap_tech = {
742 .description = tdesc,
743 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
744 .requester = zt_request,
745 .send_digit_begin = zt_digit_begin,
746 .send_digit_end = zt_digit_end,
747 .send_text = zt_sendtext,
754 .exception = zt_exception,
755 .indicate = zt_indicate,
757 .setoption = zt_setoption,
758 .func_channel_read = zt_func_read,
762 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
764 #define GET_CHANNEL(p) ((p)->channel)
767 struct zt_pvt *round_robin[32];
770 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
773 /* Grab the lock first */
775 res = ast_mutex_trylock(&pri->lock);
777 ast_mutex_unlock(&pvt->lock);
778 /* Release the lock and try again */
780 ast_mutex_lock(&pvt->lock);
783 /* Then break the poll */
784 pthread_kill(pri->master, SIGURG);
790 static inline void ss7_rel(struct zt_ss7 *ss7)
792 ast_mutex_unlock(&ss7->lock);
795 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
798 /* Grab the lock first */
800 res = ast_mutex_trylock(&pri->lock);
802 ast_mutex_unlock(&pvt->lock);
803 /* Release the lock and try again */
805 ast_mutex_lock(&pvt->lock);
808 /* Then break the poll */
809 pthread_kill(pri->master, SIGURG);
813 #define NUM_CADENCE_MAX 25
814 static int num_cadence = 4;
815 static int user_has_defined_cadences = 0;
817 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
818 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
819 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
820 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
821 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
824 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
825 * is 1, the second pause is 2 and so on.
828 static int cidrings[NUM_CADENCE_MAX] = {
829 2, /*!< Right after first long ring */
830 4, /*!< Right after long part */
831 3, /*!< After third chirp */
832 2, /*!< Second spell */
835 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
836 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
838 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
839 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
841 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
844 if (p->subs[0].owner == ast)
846 else if (p->subs[1].owner == ast)
848 else if (p->subs[2].owner == ast)
853 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
859 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
861 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
866 ast_mutex_unlock(&pri->lock);
869 if (p->subs[a].owner) {
870 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
871 ast_mutex_unlock(&p->lock);
873 ast_mutex_lock(&p->lock);
875 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
876 ast_mutex_unlock(&p->subs[a].owner->lock);
884 ast_mutex_lock(&pri->lock);
888 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
891 struct zt_pri *pri = (struct zt_pri*) data;
894 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
896 /* We must unlock the PRI to avoid the possibility of a deadlock */
897 #if defined(HAVE_PRI) || defined(HAVE_SS7)
902 ast_mutex_unlock(&pri->lock);
907 ast_mutex_unlock(&ss7->lock);
917 if (ast_mutex_trylock(&p->owner->lock)) {
918 ast_mutex_unlock(&p->lock);
920 ast_mutex_lock(&p->lock);
922 ast_queue_frame(p->owner, f);
923 ast_mutex_unlock(&p->owner->lock);
929 #if defined(HAVE_PRI) || defined(HAVE_SS7)
934 ast_mutex_lock(&pri->lock);
939 ast_mutex_lock(&ss7->lock);
950 static int restore_gains(struct zt_pvt *p);
952 static void swap_subs(struct zt_pvt *p, int a, int b)
956 struct ast_channel *towner;
959 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
961 tchan = p->subs[a].chan;
962 towner = p->subs[a].owner;
963 tinthreeway = p->subs[a].inthreeway;
965 p->subs[a].chan = p->subs[b].chan;
966 p->subs[a].owner = p->subs[b].owner;
967 p->subs[a].inthreeway = p->subs[b].inthreeway;
969 p->subs[b].chan = tchan;
970 p->subs[b].owner = towner;
971 p->subs[b].inthreeway = tinthreeway;
973 if (p->subs[a].owner)
974 p->subs[a].owner->fds[0] = p->subs[a].zfd;
975 if (p->subs[b].owner)
976 p->subs[b].owner->fds[0] = p->subs[b].zfd;
977 wakeup_sub(p, a, NULL);
978 wakeup_sub(p, b, NULL);
981 static int zt_open(char *fn)
989 for (x = 0; x < strlen(fn); x++) {
990 if (!isdigit(fn[x])) {
998 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1001 fn = "/dev/zap/channel";
1003 fd = open(fn, O_RDWR | O_NONBLOCK);
1005 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1009 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1013 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1018 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
1022 static void zt_close(int fd)
1028 static int zt_setlinear(int zfd, int linear)
1031 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1038 static int alloc_sub(struct zt_pvt *p, int x)
1042 if (p->subs[x].zfd < 0) {
1043 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1044 if (p->subs[x].zfd > -1) {
1045 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1047 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1048 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1049 bi.numbufs = numbufs;
1050 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1052 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1055 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1056 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1057 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1058 zt_close(p->subs[x].zfd);
1059 p->subs[x].zfd = -1;
1063 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1066 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1069 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1073 static int unalloc_sub(struct zt_pvt *p, int x)
1076 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1080 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
1081 if (p->subs[x].zfd > -1) {
1082 zt_close(p->subs[x].zfd);
1084 p->subs[x].zfd = -1;
1085 p->subs[x].linear = 0;
1086 p->subs[x].chan = 0;
1087 p->subs[x].owner = NULL;
1088 p->subs[x].inthreeway = 0;
1089 p->polarity = POLARITY_IDLE;
1090 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1094 static int digit_to_dtmfindex(char digit)
1097 return ZT_TONE_DTMF_BASE + (digit - '0');
1098 else if (digit >= 'A' && digit <= 'D')
1099 return ZT_TONE_DTMF_A + (digit - 'A');
1100 else if (digit >= 'a' && digit <= 'd')
1101 return ZT_TONE_DTMF_A + (digit - 'a');
1102 else if (digit == '*')
1103 return ZT_TONE_DTMF_s;
1104 else if (digit == '#')
1105 return ZT_TONE_DTMF_p;
1110 static int zt_digit_begin(struct ast_channel *chan, char digit)
1116 pvt = chan->tech_pvt;
1118 ast_mutex_lock(&pvt->lock);
1120 index = zt_get_index(chan, pvt, 0);
1122 if ((index != SUB_REAL) || !pvt->owner)
1126 if ((pvt->sig == SIG_PRI) && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1127 if (pvt->setup_ack) {
1128 if (!pri_grab(pvt, pvt->pri)) {
1129 pri_information(pvt->pri->pri, pvt->call, digit);
1132 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1133 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1136 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1137 res = strlen(pvt->dialdest);
1138 pvt->dialdest[res++] = digit;
1139 pvt->dialdest[res] = '\0';
1144 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1147 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1149 ZT_DIAL_OPERATION zo = {
1150 .op = ZT_DIAL_OP_APPEND,
1153 zo.dialstr[0] = 'T';
1154 zo.dialstr[1] = digit;
1155 zo.dialstr[2] = '\0';
1156 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1157 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1162 ast_log(LOG_DEBUG, "Started VLDTMF digit '%c'\n", digit);
1164 pvt->begindigit = digit;
1168 ast_mutex_unlock(&pvt->lock);
1173 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1180 pvt = chan->tech_pvt;
1182 ast_mutex_lock(&pvt->lock);
1184 index = zt_get_index(chan, pvt, 0);
1186 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1190 /* This means that the digit was already sent via PRI signalling */
1191 if (pvt->sig == SIG_PRI && !pvt->begindigit)
1195 if (pvt->begindigit) {
1198 ast_log(LOG_DEBUG, "Ending VLDTMF digit '%c'\n", digit);
1199 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1201 pvt->begindigit = 0;
1205 ast_mutex_unlock(&pvt->lock);
1210 static char *events[] = {
1223 "Hook Transition Complete",
1228 "Polarity Reversal",
1236 { ZT_ALARM_RED, "Red Alarm" },
1237 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1238 { ZT_ALARM_BLUE, "Blue Alarm" },
1239 { ZT_ALARM_RECOVER, "Recovering" },
1240 { ZT_ALARM_LOOPBACK, "Loopback" },
1241 { ZT_ALARM_NOTOPEN, "Not Open" },
1242 { ZT_ALARM_NONE, "None" },
1245 static char *alarm2str(int alarm)
1248 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1249 if (alarms[x].alarm & alarm)
1250 return alarms[x].name;
1252 return alarm ? "Unknown Alarm" : "No Alarm";
1255 static char *event2str(int event)
1257 static char buf[256];
1258 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1259 return events[event];
1260 sprintf(buf, "Event %d", event); /* safe */
1265 static char *dialplan2str(int dialplan)
1267 if (dialplan == -1 || dialplan == -2) {
1268 return("Dynamically set dialplan in ISDN");
1270 return (pri_plan2str(dialplan));
1274 static char *zap_sig2str(int sig)
1276 static char buf[256];
1279 return "E & M Immediate";
1281 return "E & M Wink";
1285 return "Feature Group D (DTMF)";
1287 return "Feature Group D (MF)";
1288 case SIG_FEATDMF_TA:
1289 return "Feature Groud D (MF) Tandem Access";
1291 return "Feature Group B (MF)";
1295 return "FGC/CAMA (Dialpulse)";
1296 case SIG_FGC_CAMAMF:
1297 return "FGC/CAMA (MF)";
1299 return "FXS Loopstart";
1301 return "FXS Groundstart";
1303 return "FXS Kewlstart";
1305 return "FXO Loopstart";
1307 return "FXO Groundstart";
1309 return "FXO Kewlstart";
1311 return "PRI Signalling";
1313 return "SS7 Signalling";
1315 return "SF (Tone) Signalling Immediate";
1317 return "SF (Tone) Signalling Wink";
1319 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1320 case SIG_SF_FEATDMF:
1321 return "SF (Tone) Signalling with Feature Group D (MF)";
1323 return "SF (Tone) Signalling with Feature Group B (MF)";
1324 case SIG_GR303FXOKS:
1325 return "GR-303 Signalling with FXOKS";
1326 case SIG_GR303FXSKS:
1327 return "GR-303 Signalling with FXSKS";
1329 return "Pseudo Signalling";
1331 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1336 #define sig2str zap_sig2str
1338 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1340 /* If the conference already exists, and we're already in it
1341 don't bother doing anything */
1344 memset(&zi, 0, sizeof(zi));
1347 if (slavechannel > 0) {
1348 /* If we have only one slave, do a digital mon */
1349 zi.confmode = ZT_CONF_DIGITALMON;
1350 zi.confno = slavechannel;
1353 /* Real-side and pseudo-side both participate in conference */
1354 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1355 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1357 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1358 zi.confno = p->confno;
1360 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1364 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1365 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1368 if (slavechannel < 1) {
1369 p->confno = zi.confno;
1371 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1373 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1377 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1379 /* If they're listening to our channel, they're ours */
1380 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1382 /* If they're a talker on our (allocated) conference, they're ours */
1383 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1388 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1391 if (/* Can't delete if there's no zfd */
1393 /* Don't delete from the conference if it's not our conference */
1395 /* Don't delete if we don't think it's conferenced at all (implied) */
1397 memset(&zi, 0, sizeof(zi));
1401 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1402 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1406 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1407 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1411 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1415 struct zt_pvt *slave = NULL;
1416 /* Start out optimistic */
1418 /* Update conference state in a stateless fashion */
1419 for (x = 0; x < 3; x++) {
1420 /* Any three-way calling makes slave native mode *definitely* out
1422 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1425 /* If we don't have any 3-way calls, check to see if we have
1426 precisely one slave */
1427 if (useslavenative) {
1428 for (x = 0; x < MAX_SLAVES; x++) {
1431 /* Whoops already have a slave! No
1432 slave native and stop right away */
1437 /* We have one slave so far */
1438 slave = p->slaves[x];
1443 /* If no slave, slave native definitely out */
1446 else if (slave->law != p->law) {
1452 return useslavenative;
1455 static int reset_conf(struct zt_pvt *p)
1458 memset(&zi, 0, sizeof(zi));
1460 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1461 if (p->subs[SUB_REAL].zfd > -1) {
1462 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1463 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1468 static int update_conf(struct zt_pvt *p)
1473 struct zt_pvt *slave = NULL;
1475 useslavenative = isslavenative(p, &slave);
1476 /* Start with the obvious, general stuff */
1477 for (x = 0; x < 3; x++) {
1478 /* Look for three way calls */
1479 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1480 conf_add(p, &p->subs[x], x, 0);
1483 conf_del(p, &p->subs[x], x);
1486 /* If we have a slave, add him to our conference now. or DAX
1487 if this is slave native */
1488 for (x = 0; x < MAX_SLAVES; x++) {
1491 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1493 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1498 /* If we're supposed to be in there, do so now */
1499 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1501 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1503 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1507 /* If we have a master, add ourselves to his conference */
1509 if (isslavenative(p->master, NULL)) {
1510 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1512 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1516 /* Nobody is left (or should be left) in our conference.
1521 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1525 static void zt_enable_ec(struct zt_pvt *p)
1533 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1538 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1541 if (p->echocancel) {
1542 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1544 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1546 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1549 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1551 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1555 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1559 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1563 static void zt_train_ec(struct zt_pvt *p)
1567 if (p && p->echocancel && p->echotraining) {
1568 x = p->echotraining;
1569 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1571 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1574 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1578 ast_log(LOG_DEBUG, "No echo training requested\n");
1582 static void zt_disable_ec(struct zt_pvt *p)
1586 if (p->echocancel) {
1588 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1590 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1593 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1599 static void fill_txgain(struct zt_gains *g, float gain, int law)
1603 float linear_gain = pow(10.0, gain / 20.0);
1607 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1609 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1610 if (k > 32767) k = 32767;
1611 if (k < -32767) k = -32767;
1612 g->txgain[j] = AST_LIN2A(k);
1619 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1621 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1622 if (k > 32767) k = 32767;
1623 if (k < -32767) k = -32767;
1624 g->txgain[j] = AST_LIN2MU(k);
1633 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1637 float linear_gain = pow(10.0, gain / 20.0);
1641 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1643 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1644 if (k > 32767) k = 32767;
1645 if (k < -32767) k = -32767;
1646 g->rxgain[j] = AST_LIN2A(k);
1653 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1655 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1656 if (k > 32767) k = 32767;
1657 if (k < -32767) k = -32767;
1658 g->rxgain[j] = AST_LIN2MU(k);
1667 static int set_actual_txgain(int fd, int chan, float gain, int law)
1672 memset(&g, 0, sizeof(g));
1674 res = ioctl(fd, ZT_GETGAINS, &g);
1677 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1681 fill_txgain(&g, gain, law);
1683 return ioctl(fd, ZT_SETGAINS, &g);
1686 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1691 memset(&g, 0, sizeof(g));
1693 res = ioctl(fd, ZT_GETGAINS, &g);
1696 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1700 fill_rxgain(&g, gain, law);
1702 return ioctl(fd, ZT_SETGAINS, &g);
1705 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1707 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1710 static int bump_gains(struct zt_pvt *p)
1714 /* Bump receive gain by 5.0db */
1715 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
1717 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1724 static int restore_gains(struct zt_pvt *p)
1728 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1730 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1737 static inline int zt_set_hook(int fd, int hs)
1741 res = ioctl(fd, ZT_HOOK, &x);
1744 if (errno == EINPROGRESS) return 0;
1745 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1750 static inline int zt_confmute(struct zt_pvt *p, int muted)
1754 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1756 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1758 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1760 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1762 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1766 static int save_conference(struct zt_pvt *p)
1768 struct zt_confinfo c;
1770 if (p->saveconf.confmode) {
1771 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1774 p->saveconf.chan = 0;
1775 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1777 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1778 p->saveconf.confmode = 0;
1783 c.confmode = ZT_CONF_NORMAL;
1784 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1786 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1790 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1794 static int restore_conference(struct zt_pvt *p)
1797 if (p->saveconf.confmode) {
1798 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1799 p->saveconf.confmode = 0;
1801 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1806 ast_log(LOG_DEBUG, "Restored conferencing\n");
1810 static int send_callerid(struct zt_pvt *p);
1812 static int send_cwcidspill(struct zt_pvt *p)
1816 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1818 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1819 /* Make sure we account for the end */
1820 p->cidlen += READ_SIZE * 4;
1823 if (option_verbose > 2)
1824 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1828 static int has_voicemail(struct zt_pvt *p)
1831 return ast_app_has_voicemail(p->mailbox, NULL);
1834 static int send_callerid(struct zt_pvt *p)
1836 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1838 /* Take out of linear mode if necessary */
1839 if (p->subs[SUB_REAL].linear) {
1840 p->subs[SUB_REAL].linear = 0;
1841 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1843 while (p->cidpos < p->cidlen) {
1844 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1846 if (errno == EAGAIN)
1849 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1859 if (p->callwaitcas) {
1860 /* Wait for CID/CW to expire */
1861 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1863 restore_conference(p);
1867 static int zt_callwait(struct ast_channel *ast)
1869 struct zt_pvt *p = ast->tech_pvt;
1870 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1872 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1875 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1879 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1880 if (!p->callwaitrings && p->callwaitingcallerid) {
1881 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1883 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1885 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1887 p->cidlen = 2400 + READ_SIZE * 4;
1895 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1897 struct zt_pvt *p = ast->tech_pvt;
1898 int x, res, index,mysig;
1903 char dest[256]; /* must be same length as p->dialdest */
1904 ast_mutex_lock(&p->lock);
1905 ast_copy_string(dest, rdest, sizeof(dest));
1906 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1907 if ((ast->_state == AST_STATE_BUSY)) {
1908 p->subs[SUB_REAL].needbusy = 1;
1909 ast_mutex_unlock(&p->lock);
1912 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1913 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1914 ast_mutex_unlock(&p->lock);
1918 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
1920 /* Special pseudo -- automatically up */
1921 ast_setstate(ast, AST_STATE_UP);
1922 ast_mutex_unlock(&p->lock);
1925 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1926 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1928 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1931 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1934 if (p->outsigmod > -1)
1935 mysig = p->outsigmod;
1941 if (p->owner == ast) {
1942 /* Normal ring, on hook */
1944 /* Don't send audio while on hook, until the call is answered */
1946 if (p->use_callerid) {
1947 /* Generate the Caller-ID spill if desired */
1949 ast_log(LOG_WARNING, "cidspill already exists??\n");
1953 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1954 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1959 /* Choose proper cadence */
1960 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1961 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
1962 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1963 p->cidrings = cidrings[p->distinctivering - 1];
1965 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1966 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1967 p->cidrings = p->sendcalleridafter;
1970 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1971 c = strchr(dest, '/');
1974 if (c && (strlen(c) < p->stripmsd)) {
1975 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1979 p->dop.op = ZT_DIAL_OP_REPLACE;
1980 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1982 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1984 p->dop.dialstr[0] = '\0';
1987 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1988 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1989 ast_mutex_unlock(&p->lock);
1994 /* Call waiting call */
1995 p->callwaitrings = 0;
1996 if (ast->cid.cid_num)
1997 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
1999 p->callwait_num[0] = '\0';
2000 if (ast->cid.cid_name)
2001 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2003 p->callwait_name[0] = '\0';
2004 /* Call waiting tone instead */
2005 if (zt_callwait(ast)) {
2006 ast_mutex_unlock(&p->lock);
2009 /* Make ring-back */
2010 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2011 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2014 n = ast->cid.cid_name;
2015 l = ast->cid.cid_num;
2017 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2019 p->lastcid_num[0] = '\0';
2021 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2023 p->lastcid_name[0] = '\0';
2024 ast_setstate(ast, AST_STATE_RINGING);
2025 index = zt_get_index(ast, p, 0);
2027 p->subs[index].needringing = 1;
2040 case SIG_FGC_CAMAMF:
2045 case SIG_SF_FEATDMF:
2046 case SIG_FEATDMF_TA:
2048 c = strchr(dest, '/');
2053 if (strlen(c) < p->stripmsd) {
2054 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2055 ast_mutex_unlock(&p->lock);
2059 /* Start the trunk, if not GR-303 */
2063 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2065 if (errno != EINPROGRESS) {
2066 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2067 ast_mutex_unlock(&p->lock);
2075 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
2076 p->dop.op = ZT_DIAL_OP_REPLACE;
2082 l = ast->cid.cid_num;
2084 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2086 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2089 l = ast->cid.cid_num;
2091 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2093 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2095 case SIG_FEATDMF_TA:
2097 const char *cic, *ozz;
2099 /* If you have to go through a Tandem Access point you need to use this */
2100 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2103 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2107 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2108 ast_mutex_unlock(&p->lock);
2111 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2112 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2117 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2120 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2122 case SIG_FGC_CAMAMF:
2124 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2128 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2130 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2134 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2135 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2136 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2137 p->echorest[sizeof(p->echorest) - 1] = '\0';
2139 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2143 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2145 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2146 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2147 ast_mutex_unlock(&p->lock);
2152 ast_log(LOG_DEBUG, "Deferring dialing...\n");
2155 if (ast_strlen_zero(c))
2157 ast_setstate(ast, AST_STATE_DIALING);
2160 /* Special pseudo -- automatically up*/
2161 ast_setstate(ast, AST_STATE_UP);
2165 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2166 p->dialdest[0] = '\0';
2170 ast_log(LOG_DEBUG, "not yet implemented\n");
2171 ast_mutex_unlock(&p->lock);
2176 c = strchr(dest, '/');
2182 if (!p->hidecallerid) {
2183 l = ast->cid.cid_num;
2188 ss7_grab(p, p->ss7);
2189 p->digital = IS_DIGITAL(ast->transfercapability);
2190 p->ss7call = isup_new_call(p->ss7->ss7);
2194 ast_mutex_unlock(&p->lock);
2195 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2199 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, c + p->stripmsd, l);
2201 isup_iam(p->ss7->ss7, p->ss7call);
2204 #endif /* HAVE_SS7 */
2208 #ifdef SUPPORT_USERUSER
2209 const char *useruser;
2213 int prilocaldialplan;
2217 int redirect_reason;
2219 c = strchr(dest, '/');
2224 if (!p->hidecalleridname)
2225 n = ast->cid.cid_name;
2228 if (!p->hidecallerid) {
2229 l = ast->cid.cid_num;
2230 n = ast->cid.cid_name;
2235 if (strlen(c) < p->stripmsd) {
2236 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2237 ast_mutex_unlock(&p->lock);
2240 if (mysig != SIG_FXSKS) {
2241 p->dop.op = ZT_DIAL_OP_REPLACE;
2242 s = strchr(c + p->stripmsd, 'w');
2245 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2247 p->dop.dialstr[0] = '\0';
2250 p->dop.dialstr[0] = '\0';
2253 if (pri_grab(p, p->pri)) {
2254 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2255 ast_mutex_unlock(&p->lock);
2258 if (!(p->call = pri_new_call(p->pri->pri))) {
2259 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2261 ast_mutex_unlock(&p->lock);
2264 if (!(sr = pri_sr_new())) {
2265 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2267 ast_mutex_unlock(&p->lock);
2269 if (p->bearer || (mysig == SIG_FXSKS)) {
2272 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);
2273 p->bearer->call = p->call;
2276 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
2278 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2280 p->digital = IS_DIGITAL(ast->transfercapability);
2281 /* Add support for exclusive override */
2282 if (p->priexclusive)
2285 /* otherwise, traditional behavior */
2286 if (p->pri->nodetype == PRI_NETWORK)
2292 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2293 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2295 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2296 if (p->pri->facilityenable)
2297 pri_facility_enable(p->pri->pri);
2299 if (option_verbose > 2)
2300 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2302 pridialplan = p->pri->dialplan - 1;
2303 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2304 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2305 if (pridialplan == -2) {
2306 dp_strip = strlen(p->pri->internationalprefix);
2308 pridialplan = PRI_INTERNATIONAL_ISDN;
2309 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2310 if (pridialplan == -2) {
2311 dp_strip = strlen(p->pri->nationalprefix);
2313 pridialplan = PRI_NATIONAL_ISDN;
2315 pridialplan = PRI_LOCAL_ISDN;
2318 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2321 prilocaldialplan = p->pri->localdialplan - 1;
2322 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2323 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2324 if (prilocaldialplan == -2) {
2325 ldp_strip = strlen(p->pri->internationalprefix);
2327 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2328 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2329 if (prilocaldialplan == -2) {
2330 ldp_strip = strlen(p->pri->nationalprefix);
2332 prilocaldialplan = PRI_NATIONAL_ISDN;
2334 prilocaldialplan = PRI_LOCAL_ISDN;
2337 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2338 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2339 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2340 if (!strcasecmp(rr_str, "UNKNOWN"))
2341 redirect_reason = 0;
2342 else if (!strcasecmp(rr_str, "BUSY"))
2343 redirect_reason = 1;
2344 else if (!strcasecmp(rr_str, "NO_REPLY"))
2345 redirect_reason = 2;
2346 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2347 redirect_reason = 15;
2349 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2351 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2352 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2354 #ifdef SUPPORT_USERUSER
2355 /* User-user info */
2356 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2359 pri_sr_set_useruser(sr, useruser);
2362 if (pri_setup(p->pri->pri, p->call, sr)) {
2363 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2364 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2366 ast_mutex_unlock(&p->lock);
2371 ast_setstate(ast, AST_STATE_DIALING);
2375 ast_mutex_unlock(&p->lock);
2379 static void destroy_zt_pvt(struct zt_pvt **pvt)
2381 struct zt_pvt *p = *pvt;
2382 /* Remove channel from the list */
2384 p->prev->next = p->next;
2386 p->next->prev = p->prev;
2388 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2389 ast_mutex_destroy(&p->lock);
2394 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2404 for (i = 0; i < 3; i++) {
2405 if (cur->subs[i].owner) {
2411 prev->next = cur->next;
2413 prev->next->prev = prev;
2419 iflist->prev = NULL;
2423 if (cur->subs[SUB_REAL].zfd > -1) {
2424 zt_close(cur->subs[SUB_REAL].zfd);
2426 destroy_zt_pvt(&cur);
2430 prev->next = cur->next;
2432 prev->next->prev = prev;
2438 iflist->prev = NULL;
2442 if (cur->subs[SUB_REAL].zfd > -1) {
2443 zt_close(cur->subs[SUB_REAL].zfd);
2445 destroy_zt_pvt(&cur);
2451 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2453 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2455 static char *zap_send_keypad_facility_descrip =
2456 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2457 " IE over the current channel.\n";
2459 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2461 /* Data will be our digit string */
2463 char *digits = (char *) data;
2465 if (ast_strlen_zero(digits)) {
2467 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
2471 p = (struct zt_pvt *)chan->tech_pvt;
2475 ast_log(LOG_DEBUG, "Unable to find technology private\n");
2479 ast_mutex_lock(&p->lock);
2481 if (!p->pri || !p->call) {
2483 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2484 ast_mutex_unlock(&p->lock);
2488 if (!pri_grab(p, p->pri)) {
2489 pri_keypad_facility(p->pri->pri, p->call, digits);
2493 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
2494 ast_mutex_unlock(&p->lock);
2498 ast_mutex_unlock(&p->lock);
2503 static int pri_is_up(struct zt_pri *pri)
2506 for (x = 0; x < NUM_DCHANS; x++) {
2507 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2513 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2515 bearer->owner = &inuse;
2516 bearer->realcall = crv;
2517 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2518 if (crv->subs[SUB_REAL].owner)
2519 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2520 crv->bearer = bearer;
2521 crv->call = bearer->call;
2526 static char *pri_order(int level)
2536 return "Quaternary";
2542 /* Returns fd of the active dchan */
2543 static int pri_active_dchan_fd(struct zt_pri *pri)
2547 for (x = 0; x < NUM_DCHANS; x++) {
2548 if ((pri->dchans[x] == pri->pri))
2555 static int pri_find_dchan(struct zt_pri *pri)
2562 for (x = 0; x < NUM_DCHANS; x++) {
2563 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2565 if (pri->dchans[x] == old) {
2571 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2572 pri->dchannels[newslot]);
2574 if (old && (oldslot != newslot))
2575 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2576 pri->dchannels[oldslot], pri->dchannels[newslot]);
2577 pri->pri = pri->dchans[newslot];
2582 static int zt_hangup(struct ast_channel *ast)
2586 /*static int restore_gains(struct zt_pvt *p);*/
2587 struct zt_pvt *p = ast->tech_pvt;
2588 struct zt_pvt *tmp = NULL;
2589 struct zt_pvt *prev = NULL;
2593 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2594 if (!ast->tech_pvt) {
2595 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2599 ast_mutex_lock(&p->lock);
2601 index = zt_get_index(ast, p, 1);
2603 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2605 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2611 if (p->origcid_num) {
2612 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2613 free(p->origcid_num);
2614 p->origcid_num = NULL;
2616 if (p->origcid_name) {
2617 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2618 free(p->origcid_name);
2619 p->origcid_name = NULL;
2622 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2627 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2628 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2632 /* Real channel, do some fixup */
2633 p->subs[index].owner = NULL;
2634 p->subs[index].needanswer = 0;
2635 p->subs[index].needflash = 0;
2636 p->subs[index].needringing = 0;
2637 p->subs[index].needbusy = 0;
2638 p->subs[index].needcongestion = 0;
2639 p->subs[index].linear = 0;
2640 p->subs[index].needcallerid = 0;
2641 p->polarity = POLARITY_IDLE;
2642 zt_setlinear(p->subs[index].zfd, 0);
2643 if (index == SUB_REAL) {
2644 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2646 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2647 if (p->subs[SUB_CALLWAIT].inthreeway) {
2648 /* We had flipped over to answer a callwait and now it's gone */
2650 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2651 /* Move to the call-wait, but un-own us until they flip back. */
2652 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2653 unalloc_sub(p, SUB_CALLWAIT);
2656 /* The three way hung up, but we still have a call wait */
2658 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2659 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2660 unalloc_sub(p, SUB_THREEWAY);
2661 if (p->subs[SUB_REAL].inthreeway) {
2662 /* This was part of a three way call. Immediately make way for
2665 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2666 p->owner = p->subs[SUB_REAL].owner;
2668 /* This call hasn't been completed yet... Set owner to NULL */
2670 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2673 p->subs[SUB_REAL].inthreeway = 0;
2675 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2676 /* Move to the call-wait and switch back to them. */
2677 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2678 unalloc_sub(p, SUB_CALLWAIT);
2679 p->owner = p->subs[SUB_REAL].owner;
2680 if (p->owner->_state != AST_STATE_UP)
2681 p->subs[SUB_REAL].needanswer = 1;
2682 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2683 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
2684 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2685 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2686 unalloc_sub(p, SUB_THREEWAY);
2687 if (p->subs[SUB_REAL].inthreeway) {
2688 /* This was part of a three way call. Immediately make way for
2691 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2692 p->owner = p->subs[SUB_REAL].owner;
2694 /* This call hasn't been completed yet... Set owner to NULL */
2696 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2699 p->subs[SUB_REAL].inthreeway = 0;
2701 } else if (index == SUB_CALLWAIT) {
2702 /* Ditch the holding callwait call, and immediately make it availabe */
2703 if (p->subs[SUB_CALLWAIT].inthreeway) {
2704 /* This is actually part of a three way, placed on hold. Place the third part
2705 on music on hold now */
2706 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2707 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2708 S_OR(p->mohsuggest, NULL),
2709 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2711 p->subs[SUB_THREEWAY].inthreeway = 0;
2712 /* Make it the call wait now */
2713 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2714 unalloc_sub(p, SUB_THREEWAY);
2716 unalloc_sub(p, SUB_CALLWAIT);
2717 } else if (index == SUB_THREEWAY) {
2718 if (p->subs[SUB_CALLWAIT].inthreeway) {
2719 /* The other party of the three way call is currently in a call-wait state.
2720 Start music on hold for them, and take the main guy out of the third call */
2721 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
2722 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
2723 S_OR(p->mohsuggest, NULL),
2724 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2726 p->subs[SUB_CALLWAIT].inthreeway = 0;
2728 p->subs[SUB_REAL].inthreeway = 0;
2729 /* If this was part of a three way call index, let us make
2730 another three way call */
2731 unalloc_sub(p, SUB_THREEWAY);
2733 /* This wasn't any sort of call, but how are we an index? */
2734 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2738 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2741 p->distinctivering = 0;
2742 p->confirmanswer = 0;
2748 p->onhooktime = time(NULL);
2756 ast_dsp_free(p->dsp);
2760 law = ZT_LAW_DEFAULT;
2761 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2763 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2764 /* Perform low level hangup if no owner left */
2768 if (!ss7_grab(p, p->ss7)) {
2769 if (!p->alreadyhungup) {
2770 isup_rel(p->ss7->ss7, p->ss7call, ast->hangupcause ? ast->hangupcause : -1);
2772 p->alreadyhungup = 1;
2774 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
2776 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
2784 #ifdef SUPPORT_USERUSER
2785 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2788 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2789 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2790 if (!pri_grab(p, p->pri)) {
2791 if (p->alreadyhungup) {
2793 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2795 #ifdef SUPPORT_USERUSER
2796 pri_call_set_useruser(p->call, useruser);
2799 pri_hangup(p->pri->pri, p->call, -1);
2802 p->bearer->call = NULL;
2804 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2805 int icause = ast->hangupcause ? ast->hangupcause : -1;
2807 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2809 #ifdef SUPPORT_USERUSER
2810 pri_call_set_useruser(p->call, useruser);
2813 p->alreadyhungup = 1;
2815 p->bearer->alreadyhungup = 1;
2818 icause = atoi(cause);
2820 pri_hangup(p->pri->pri, p->call, icause);
2823 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2826 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2832 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2838 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7)))
2839 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2841 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2847 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2851 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2853 /* If they're off hook, try playing congestion */
2854 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
2855 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2857 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2863 /* Make sure we're not made available for at least two seconds assuming
2864 we were actually used for an inbound or outbound call. */
2865 if (ast->_state != AST_STATE_RESERVED) {
2866 time(&p->guardtime);
2871 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2878 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2879 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2883 p->callwaiting = p->permcallwaiting;
2884 p->hidecallerid = p->permhidecallerid;
2889 /* Restore data mode */
2890 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2892 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2897 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2898 /* Free up the bearer channel as well, and
2899 don't use its file descriptor anymore */
2900 update_conf(p->bearer);
2901 reset_conf(p->bearer);
2902 p->bearer->owner = NULL;
2903 p->bearer->realcall = NULL;
2905 p->subs[SUB_REAL].zfd = -1;
2912 p->callwaitingrepeat = 0;
2915 ast->tech_pvt = NULL;
2916 ast_mutex_unlock(&p->lock);
2917 ast_module_unref(ast_module_info->self);
2918 if (option_verbose > 2)
2919 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2921 ast_mutex_lock(&iflock);
2927 destroy_channel(prev, tmp, 0);
2935 ast_mutex_unlock(&iflock);
2939 static int zt_answer(struct ast_channel *ast)
2941 struct zt_pvt *p = ast->tech_pvt;
2944 int oldstate = ast->_state;
2945 ast_setstate(ast, AST_STATE_UP);
2946 ast_mutex_lock(&p->lock);
2947 index = zt_get_index(ast, p, 0);
2950 /* nothing to do if a radio channel */
2951 if ((p->radio || (p->oprmode < 0))) {
2952 ast_mutex_unlock(&p->lock);
2966 case SIG_FEATDMF_TA:
2969 case SIG_FGC_CAMAMF:
2974 case SIG_SF_FEATDMF:
2979 /* Pick up the line */
2981 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2982 if (p->hanguponpolarityswitch) {
2983 gettimeofday(&p->polaritydelaytv, NULL);
2985 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2986 tone_zone_play_tone(p->subs[index].zfd, -1);
2988 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2989 if (oldstate == AST_STATE_RINGING) {
2991 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2992 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2993 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2994 p->owner = p->subs[SUB_REAL].owner;
2997 if (p->sig & __ZT_SIG_FXS) {
3004 /* Send a pri acknowledge */
3005 if (!pri_grab(p, p->pri)) {
3007 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3010 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3017 if (!ss7_grab(p, p->ss7)) {
3019 res = isup_anm(p->ss7->ss7, p->ss7call);
3022 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3028 ast_mutex_unlock(&p->lock);
3031 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3034 ast_mutex_unlock(&p->lock);
3038 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3044 struct zt_pvt *p = chan->tech_pvt, *pp;
3045 struct oprmode *oprmode;
3048 /* all supported options require data */
3049 if (!data || (datalen < 1)) {
3055 case AST_OPTION_TXGAIN:
3056 scp = (signed char *) data;
3057 index = zt_get_index(chan, p, 0);
3059 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
3063 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
3064 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
3065 case AST_OPTION_RXGAIN:
3066 scp = (signed char *) data;
3067 index = zt_get_index(chan, p, 0);
3069 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
3073 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
3074 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
3075 case AST_OPTION_TONE_VERIFY:
3082 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
3083 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
3087 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
3088 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
3092 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
3093 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
3097 case AST_OPTION_TDD:
3098 /* turn on or off TDD */
3101 if (!*cp) { /* turn it off */
3103 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
3110 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
3111 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
3113 /* otherwise, turn it on */
3114 if (!p->didtdd) { /* if havent done it yet */
3115 unsigned char mybuf[41000], *buf;
3116 int size, res, fd, len;
3117 struct pollfd fds[1];
3120 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
3121 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
3123 index = zt_get_index(chan, p, 0);
3125 ast_log(LOG_WARNING, "No index in TDD?\n");
3128 fd = p->subs[index].zfd;
3130 if (ast_check_hangup(chan))
3133 if (size > READ_SIZE)
3136 fds[0].events = POLLPRI | POLLOUT;
3138 res = poll(fds, 1, -1);
3141 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
3144 /* if got exception */
3145 if (fds[0].revents & POLLPRI)
3147 if (!(fds[0].revents & POLLOUT)) {
3149 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
3152 res = write(fd, buf, size);
3154 if (res == -1) return -1;
3156 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
3162 p->didtdd = 1; /* set to have done it now */
3164 if (*cp == 2) { /* Mate mode */
3171 if (!p->tdd) { /* if we dont have one yet */
3172 p->tdd = tdd_new(); /* allocate one */
3175 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
3180 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
3181 *cp ? "ON" : "OFF", (int) *cp, chan->name);
3182 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
3184 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
3188 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
3193 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
3196 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
3197 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
3199 case AST_OPTION_OPRMODE: /* Operator services mode */
3200 oprmode = (struct oprmode *) data;
3201 pp = oprmode->peer->tech_pvt;
3202 p->oprmode = pp->oprmode = 0;
3206 /* setup modes, if any */
3209 pp->oprmode = oprmode->mode;
3210 p->oprmode = -oprmode->mode;
3213 ast_log(LOG_DEBUG, "Set Operator Services mode, value: %d on %s/%s\n",
3214 oprmode->mode, chan->name,oprmode->peer->name);
3216 case AST_OPTION_ECHOCAN:
3220 ast_log(LOG_DEBUG, "Enabling echo cancelation on %s\n", chan->name);
3224 ast_log(LOG_DEBUG, "Disabling echo cancelation on %s\n", chan->name);
3234 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
3236 struct zt_pvt *p = chan->tech_pvt;
3238 if (!strcasecmp(data, "rxgain")) {
3239 ast_mutex_lock(&p->lock);
3240 snprintf(buf, len, "%f", p->rxgain);
3241 ast_mutex_unlock(&p->lock);
3242 } else if (!strcasecmp(data, "txgain")) {
3243 ast_mutex_lock(&p->lock);
3244 snprintf(buf, len, "%f", p->txgain);
3245 ast_mutex_unlock(&p->lock);
3247 ast_copy_string(buf, "", len);
3253 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
3255 /* Unlink a specific slave or all slaves/masters from a given master */
3261 ast_mutex_lock(&master->lock);
3263 while (ast_mutex_trylock(&slave->lock)) {
3264 ast_mutex_unlock(&master->lock);
3266 ast_mutex_lock(&master->lock);
3271 for (x = 0; x < MAX_SLAVES; x++) {
3272 if (master->slaves[x]) {
3273 if (!slave || (master->slaves[x] == slave)) {
3274 /* Take slave out of the conference */
3276 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
3277 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
3278 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
3279 master->slaves[x]->master = NULL;
3280 master->slaves[x] = NULL;
3285 master->inconference = 0;
3288 if (master->master) {
3289 /* Take master out of the conference */
3290 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
3291 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
3293 for (x = 0; x < MAX_SLAVES; x++) {
3294 if (master->master->slaves[x] == master)
3295 master->master->slaves[x] = NULL;
3296 else if (master->master->slaves[x])
3300 master->master->inconference = 0;
3302 master->master = NULL;
3304 update_conf(master);
3307 ast_mutex_unlock(&slave->lock);
3308 ast_mutex_unlock(&master->lock);
3312 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
3314 if (!slave || !master) {
3315 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
3318 for (x = 0; x < MAX_SLAVES; x++) {
3319 if (!master->slaves[x]) {
3320 master->slaves[x] = slave;
3324 if (x >= MAX_SLAVES) {
3325 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
3326 master->slaves[MAX_SLAVES - 1] = slave;
3329 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
3330 slave->master = master;
3333 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
3336 static void disable_dtmf_detect(struct zt_pvt *p)
3338 #ifdef ZT_TONEDETECT
3344 #ifdef ZT_TONEDETECT
3346 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3348 if (!p->hardwaredtmf && p->dsp) {
3349 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
3350 ast_dsp_set_features(p->dsp, p->dsp_features);
3354 static void enable_dtmf_detect(struct zt_pvt *p)
3356 #ifdef ZT_TONEDETECT
3360 if (p->channel == CHAN_PSEUDO)
3365 #ifdef ZT_TONEDETECT
3366 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
3367 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3369 if (!p->hardwaredtmf && p->dsp) {
3370 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
3371 ast_dsp_set_features(p->dsp, p->dsp_features);
3375 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)
3377 struct ast_channel *who;
3378 struct zt_pvt *p0, *p1, *op0, *op1;
3379 struct zt_pvt *master = NULL, *slave = NULL;
3380 struct ast_frame *f;
3384 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
3385 int os0 = -1, os1 = -1;
3387 struct ast_channel *oc0, *oc1;
3388 enum ast_bridge_result res;
3391 int triedtopribridge = 0;
3392 q931_call *q931c0 = NULL, *q931c1 = NULL;
3395 /* For now, don't attempt to native bridge if either channel needs DTMF detection.
3396 There is code below to handle it properly until DTMF is actually seen,
3397 but due to currently unresolved issues it's ignored...
3400 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
3401 return AST_BRIDGE_FAILED_NOWARN;
3403 ast_mutex_lock(&c0->lock);
3404 ast_mutex_lock(&c1->lock);
3408 /* cant do pseudo-channels here */
3409 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
3410 ast_mutex_unlock(&c0->lock);
3411 ast_mutex_unlock(&c1->lock);
3412 return AST_BRIDGE_FAILED_NOWARN;
3415 oi0 = zt_get_index(c0, p0, 0);
3416 oi1 = zt_get_index(c1, p1, 0);
3417 if ((oi0 < 0) || (oi1 < 0)) {
3418 ast_mutex_unlock(&c0->lock);
3419 ast_mutex_unlock(&c1->lock);
3420 return AST_BRIDGE_FAILED;
3423 op0 = p0 = c0->tech_pvt;
3424 op1 = p1 = c1->tech_pvt;
3430 if (ast_mutex_trylock(&p0->lock)) {
3431 /* Don't block, due to potential for deadlock */
3432 ast_mutex_unlock(&c0->lock);
3433 ast_mutex_unlock(&c1->lock);
3434 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
3435 return AST_BRIDGE_RETRY;