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 in v1.5dev
46 #include <sys/signal.h>
50 #if !defined(SOLARIS) && !defined(__FreeBSD__)
54 #include <sys/ioctl.h>
56 #include <linux/zaptel.h>
59 #endif /* __linux__ */
65 #ifndef PRI_KEYPAD_FACILITY_TX
66 #error "You need newer libpri"
75 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
77 #include "asterisk/lock.h"
78 #include "asterisk/channel.h"
79 #include "asterisk/config.h"
80 #include "asterisk/logger.h"
81 #include "asterisk/module.h"
82 #include "asterisk/pbx.h"
83 #include "asterisk/options.h"
84 #include "asterisk/file.h"
85 #include "asterisk/ulaw.h"
86 #include "asterisk/alaw.h"
87 #include "asterisk/callerid.h"
88 #include "asterisk/adsi.h"
89 #include "asterisk/cli.h"
90 #include "asterisk/cdr.h"
91 #include "asterisk/features.h"
92 #include "asterisk/musiconhold.h"
93 #include "asterisk/say.h"
94 #include "asterisk/tdd.h"
95 #include "asterisk/app.h"
96 #include "asterisk/dsp.h"
97 #include "asterisk/astdb.h"
98 #include "asterisk/manager.h"
99 #include "asterisk/causes.h"
100 #include "asterisk/term.h"
101 #include "asterisk/utils.h"
102 #include "asterisk/transcap.h"
103 #include "asterisk/stringfields.h"
105 #include "asterisk/smdi.h"
106 #include "asterisk/astobj.h"
107 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
110 #ifndef ZT_SIG_HARDHDLC
111 #error "Your zaptel is too old. please update"
114 #ifndef ZT_TONEDETECT
115 /* Work around older code with no tone detect */
116 #define ZT_EVENT_DTMFDOWN 0
117 #define ZT_EVENT_DTMFUP 0
120 /* define this to send PRI user-user information elements */
121 #undef SUPPORT_USERUSER
124 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
125 * the user hangs up to reset the state machine so ring works properly.
126 * This is used to be able to support kewlstart by putting the zhone in
127 * groundstart mode since their forward disconnect supervision is entirely
128 * broken even though their documentation says it isn't and their support
129 * is entirely unwilling to provide any assistance with their channel banks
130 * even though their web site says they support their products for life.
132 /* #define ZHONE_HACK */
135 * Define if you want to check the hook state for an FXO (FXS signalled) interface
136 * before dialing on it. Certain FXO interfaces always think they're out of
137 * service with this method however.
139 /* #define ZAP_CHECK_HOOKSTATE */
141 /*! \brief Typically, how many rings before we should send Caller*ID */
142 #define DEFAULT_CIDRINGS 1
144 #define CHANNEL_PSEUDO -12
146 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
148 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
149 #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))
151 static const char desc[] = "Zapata Telephony"
160 static const char tdesc[] = "Zapata Telephony Driver"
169 static const char config[] = "zapata.conf";
171 #define SIG_EM ZT_SIG_EM
172 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
173 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
174 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
175 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
176 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
177 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
178 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
179 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
180 #define SIG_FXSLS ZT_SIG_FXSLS
181 #define SIG_FXSGS ZT_SIG_FXSGS
182 #define SIG_FXSKS ZT_SIG_FXSKS
183 #define SIG_FXOLS ZT_SIG_FXOLS
184 #define SIG_FXOGS ZT_SIG_FXOGS
185 #define SIG_FXOKS ZT_SIG_FXOKS
186 #define SIG_PRI ZT_SIG_CLEAR
187 #define SIG_R2 ZT_SIG_CAS
188 #define SIG_SF ZT_SIG_SF
189 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
190 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
191 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
192 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
193 #define SIG_EM_E1 ZT_SIG_EM_E1
194 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
195 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
198 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
199 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
201 #define CHAN_PSEUDO -2
203 #define DCHAN_PROVISIONED (1 << 0)
204 #define DCHAN_NOTINALARM (1 << 1)
205 #define DCHAN_UP (1 << 2)
207 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
209 static char context[AST_MAX_CONTEXT] = "default";
210 static char cid_num[256] = "";
211 static char cid_name[256] = "";
212 static char defaultcic[64] = "";
213 static char defaultozz[64] = "";
215 static char language[MAX_LANGUAGE] = "";
216 static char musicclass[MAX_MUSICCLASS] = "";
217 static char progzone[10]= "";
219 static int usedistinctiveringdetection = 0;
221 static int transfertobusy = 1;
223 static int use_callerid = 1;
224 static int cid_signalling = CID_SIG_BELL;
225 static int cid_start = CID_START_RING;
226 static int zaptrcallerid = 0;
227 static int cur_signalling = -1;
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;
288 static int use_smdi = 0;
289 static char smdi_port[SMDI_MAX_FILENAME_LEN] = "/dev/ttyS0";
291 static int numbufs = 4;
293 static int cur_prewink = -1;
294 static int cur_preflash = -1;
295 static int cur_wink = -1;
296 static int cur_flash = -1;
297 static int cur_start = -1;
298 static int cur_rxwink = -1;
299 static int cur_rxflash = -1;
300 static int cur_debounce = -1;
301 static int cur_priexclusive = 0;
303 static int priindication_oob = 0;
306 static int minunused = 2;
307 static int minidle = 0;
308 static char idleext[AST_MAX_EXTENSION];
309 static char idledial[AST_MAX_EXTENSION];
310 static int overlapdial = 0;
311 static int facilityenable = 0;
312 static char internationalprefix[10] = "";
313 static char nationalprefix[10] = "";
314 static char localprefix[20] = "";
315 static char privateprefix[20] = "";
316 static char unknownprefix[20] = "";
317 static long resetinterval = 3600; /*!< How often (in seconds) to reset unused channels. Default 1 hour. */
318 static struct ast_channel inuse;
319 #ifdef PRI_GETSET_TIMERS
320 static int pritimers[PRI_MAX_TIMERS];
322 static int pridebugfd = -1;
323 static char pridebugfilename[1024]="";
326 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
327 static int firstdigittimeout = 16000;
329 /*! \brief How long to wait for following digits (FXO logic) */
330 static int gendigittimeout = 8000;
332 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
333 static int matchdigittimeout = 3000;
335 static int usecnt =0;
336 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
338 /*! \brief Protect the interface list (of zt_pvt's) */
339 AST_MUTEX_DEFINE_STATIC(iflock);
342 static int ifcount = 0;
345 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
348 /*! \brief Whether we answer on a Polarity Switch event */
349 static int answeronpolarityswitch = 0;
351 /*! \brief Whether we hang up on a Polarity Switch event */
352 static int hanguponpolarityswitch = 0;
354 /*! \brief How long (ms) to ignore Polarity Switch events after we answer a call */
355 static int polarityonanswerdelay = 600;
357 /*! \brief When to send the CallerID signals (rings) */
358 static int sendcalleridafter = DEFAULT_CIDRINGS;
360 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
361 when it's doing something critical. */
362 AST_MUTEX_DEFINE_STATIC(monlock);
364 /*! \brief This is the thread for the monitor which checks for input on the channels
365 which are not currently in use. */
366 static pthread_t monitor_thread = AST_PTHREADT_NULL;
368 static int restart_monitor(void);
370 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);
372 static int zt_sendtext(struct ast_channel *c, const char *text);
374 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
375 static inline int zt_get_event(int fd)
378 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
382 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
383 static inline int zt_wait_event(int fd)
386 i = ZT_IOMUX_SIGEVENT;
387 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
388 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
392 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
393 #define READ_SIZE 160
395 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
396 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
398 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
399 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
400 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
401 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
402 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
408 static int r2prot = -1;
411 static int ringt_base = DEFAULT_RINGT;
415 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
416 #define PRI_CHANNEL(p) ((p) & 0xff)
417 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
418 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
421 pthread_t master; /*!< Thread of master */
422 ast_mutex_t lock; /*!< Mutex */
423 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
424 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
425 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
426 int minunused; /*!< Min # of channels to keep empty */
427 int minidle; /*!< Min # of "idling" calls to keep active */
428 int nodetype; /*!< Node type */
429 int switchtype; /*!< Type of switch to emulate */
430 int nsf; /*!< Network-Specific Facilities */
431 int dialplan; /*!< Dialing plan */
432 int localdialplan; /*!< Local dialing plan */
433 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
434 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
435 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
436 char privateprefix[20]; /*!< for private dialplans */
437 char unknownprefix[20]; /*!< for unknown dialplans */
438 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
439 int trunkgroup; /*!< What our trunkgroup is */
440 int mastertrunkgroup; /*!< What trunk group is our master */
441 int prilogicalspan; /*!< Logical span number within trunk group */
442 int numchans; /*!< Num of channels we represent */
443 int overlapdial; /*!< In overlap dialing mode */
444 int facilityenable; /*!< Enable facility IEs */
445 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
446 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
447 struct pri *pri; /*!< Currently active D-channel */
449 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
454 time_t lastreset; /*!< time when unused channels were last reset */
455 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
456 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
457 struct zt_pvt *crvs; /*!< Member CRV structs */
458 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
462 static struct zt_pri pris[NUM_SPANS];
464 static int pritype = PRI_CPE;
467 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
469 #define DEFAULT_PRI_DEBUG 0
472 static inline void pri_rel(struct zt_pri *pri)
474 ast_mutex_unlock(&pri->lock);
477 static int switchtype = PRI_SWITCH_NI2;
478 static int nsf = PRI_NSF_NONE;
479 static int dialplan = PRI_NATIONAL_ISDN + 1;
480 static int localdialplan = PRI_NATIONAL_ISDN + 1;
483 /*! Shut up the compiler */
487 #define SUB_REAL 0 /*!< Active call */
488 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
489 #define SUB_THREEWAY 2 /*!< Three-way call */
491 /* Polarity states */
492 #define POLARITY_IDLE 0
493 #define POLARITY_REV 1
496 static struct zt_distRings drings;
498 struct distRingData {
501 struct ringContextData {
502 char contextData[AST_MAX_CONTEXT];
504 struct zt_distRings {
505 struct distRingData ringnum[3];
506 struct ringContextData ringContext[3];
509 static char *subnames[] = {
515 struct zt_subchannel {
517 struct ast_channel *owner;
519 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
520 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
521 unsigned int needringing:1;
522 unsigned int needbusy:1;
523 unsigned int needcongestion:1;
524 unsigned int needcallerid:1;
525 unsigned int needanswer:1;
526 unsigned int needflash:1;
527 unsigned int linear:1;
528 unsigned int inthreeway:1;
532 #define CONF_USER_REAL (1 << 0)
533 #define CONF_USER_THIRDCALL (1 << 1)
537 static struct zt_pvt {
539 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
540 /*!< Up to three channels can be associated with this call */
542 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
543 struct zt_subchannel subs[3]; /*!< Sub-channels */
544 struct zt_confinfo saveconf; /*!< Saved conference info */
546 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
547 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
548 int inconference; /*!< If our real should be in the conference */
550 int sig; /*!< Signalling style */
551 int radio; /*!< radio type */
554 int tonezone; /*!< tone zone for this chan, or -1 for default */
555 struct zt_pvt *next; /*!< Next channel in list */
556 struct zt_pvt *prev; /*!< Prev channel in list */
560 unsigned int answeronpolarityswitch:1;
561 unsigned int busydetect:1;
562 unsigned int callreturn:1;
563 unsigned int callwaiting:1;
564 unsigned int callwaitingcallerid:1;
565 unsigned int cancallforward:1;
566 unsigned int canpark:1;
567 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
568 unsigned int destroy:1;
569 unsigned int didtdd:1; /*!< flag to say its done it once */
570 unsigned int dialednone:1;
571 unsigned int dialing:1;
572 unsigned int digital:1;
574 unsigned int echobreak:1;
575 unsigned int echocanbridged:1;
576 unsigned int echocanon:1;
577 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
578 unsigned int firstradio:1;
579 unsigned int hanguponpolarityswitch:1;
580 unsigned int hardwaredtmf:1;
581 unsigned int hidecallerid;
582 unsigned int ignoredtmf:1;
583 unsigned int immediate:1; /*!< Answer before getting digits? */
584 unsigned int inalarm:1;
585 unsigned int mate:1; /*!< flag to say its in MATE mode */
586 unsigned int outgoing:1;
587 unsigned int overlapdial:1;
588 unsigned int permcallwaiting:1;
589 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
590 unsigned int priindication_oob:1;
591 unsigned int priexclusive:1;
592 unsigned int pulse:1;
593 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
594 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
595 unsigned int threewaycalling:1;
596 unsigned int transfer:1;
597 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
598 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
599 unsigned int usedistinctiveringdetection:1;
600 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
601 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
602 #if defined(ZAPATA_PRI)
603 unsigned int alerting:1;
604 unsigned int alreadyhungup:1;
605 unsigned int isidlecall:1;
606 unsigned int proceeding:1;
607 unsigned int progress:1;
608 unsigned int resetting:1;
609 unsigned int setup_ack:1;
611 #if defined(ZAPATA_R2)
612 unsigned int hasr2call:1;
613 unsigned int r2blocked:1;
614 unsigned int sigchecked:1;
617 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
618 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
621 struct zt_distRings drings;
623 char context[AST_MAX_CONTEXT];
624 char defcontext[AST_MAX_CONTEXT];
625 char exten[AST_MAX_EXTENSION];
626 char language[MAX_LANGUAGE];
627 char musicclass[MAX_MUSICCLASS];
629 char cid_ani[AST_MAX_EXTENSION];
631 char cid_num[AST_MAX_EXTENSION];
632 int cid_ton; /*!< Type Of Number (TON) */
633 char cid_name[AST_MAX_EXTENSION];
634 char lastcid_num[AST_MAX_EXTENSION];
635 char lastcid_name[AST_MAX_EXTENSION];
636 char *origcid_num; /*!< malloced original callerid */
637 char *origcid_name; /*!< malloced original callerid */
638 char callwait_num[AST_MAX_EXTENSION];
639 char callwait_name[AST_MAX_EXTENSION];
640 char rdnis[AST_MAX_EXTENSION];
641 char dnid[AST_MAX_EXTENSION];
644 int confno; /*!< Our conference */
645 int confusers; /*!< Who is using our conference */
646 int propconfno; /*!< Propagated conference number */
647 ast_group_t callgroup;
648 ast_group_t pickupgroup;
649 int channel; /*!< Channel Number or CRV */
650 int span; /*!< Span number */
651 time_t guardtime; /*!< Must wait this much time before using for new call */
652 int cid_signalling; /*!< CID signalling type bell202 or v23 */
653 int cid_start; /*!< CID start indicator, polarity or ring */
654 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
655 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
656 int cidcwexpire; /*!< When to expire our muting for CID/CW */
657 unsigned char *cidspill;
670 int busy_quietlength;
672 struct timeval flashtime; /*!< Last flash-hook time */
674 int cref; /*!< Call reference number */
675 ZT_DIAL_OPERATION dop;
676 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
678 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
679 int amaflags; /*!< AMA Flags */
680 struct tdd_state *tdd; /*!< TDD flag */
681 char call_forward[AST_MAX_EXTENSION];
682 char mailbox[AST_MAX_EXTENSION];
686 int distinctivering; /*!< Which distinctivering to use */
687 int cidrings; /*!< Which ring to deliver CID on */
688 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
690 int polarityonanswerdelay;
691 struct timeval polaritydelaytv;
692 int sendcalleridafter;
695 struct zt_pvt *bearer;
696 struct zt_pvt *realcall;
708 } *iflist = NULL, *ifend = NULL;
710 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
711 static int zt_digit(struct ast_channel *ast, char digit);
712 static int zt_sendtext(struct ast_channel *c, const char *text);
713 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
714 static int zt_hangup(struct ast_channel *ast);
715 static int zt_answer(struct ast_channel *ast);
716 struct ast_frame *zt_read(struct ast_channel *ast);
717 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
718 struct ast_frame *zt_exception(struct ast_channel *ast);
719 static int zt_indicate(struct ast_channel *chan, int condition);
720 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
721 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
723 static const struct ast_channel_tech zap_tech = {
725 .description = tdesc,
726 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
727 .requester = zt_request,
728 .send_digit = zt_digit,
729 .send_text = zt_sendtext,
736 .exception = zt_exception,
737 .indicate = zt_indicate,
739 .setoption = zt_setoption,
743 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
745 #define GET_CHANNEL(p) ((p)->channel)
748 struct zt_pvt *round_robin[32];
751 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
754 /* Grab the lock first */
756 res = ast_mutex_trylock(&pri->lock);
758 ast_mutex_unlock(&pvt->lock);
759 /* Release the lock and try again */
761 ast_mutex_lock(&pvt->lock);
764 /* Then break the poll */
765 pthread_kill(pri->master, SIGURG);
770 #define NUM_CADENCE_MAX 25
771 static int num_cadence = 4;
772 static int user_has_defined_cadences = 0;
774 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
775 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
776 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
777 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
778 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
781 int receivedRingT; /*!< Used to find out what ringtone we are on */
783 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
784 * is 1, the second pause is 2 and so on.
787 static int cidrings[NUM_CADENCE_MAX] = {
788 2, /*!< Right after first long ring */
789 4, /*!< Right after long part */
790 3, /*!< After third chirp */
791 2, /*!< Second spell */
794 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
795 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
797 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
798 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
800 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
803 if (p->subs[0].owner == ast)
805 else if (p->subs[1].owner == ast)
807 else if (p->subs[2].owner == ast)
812 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
818 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
820 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
825 ast_mutex_unlock(&pri->lock);
828 if (p->subs[a].owner) {
829 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
830 ast_mutex_unlock(&p->lock);
832 ast_mutex_lock(&p->lock);
834 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
835 ast_mutex_unlock(&p->subs[a].owner->lock);
843 ast_mutex_lock(&pri->lock);
848 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
850 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
853 /* We must unlock the PRI to avoid the possibility of a deadlock */
856 ast_mutex_unlock(&pri->lock);
860 if (ast_mutex_trylock(&p->owner->lock)) {
861 ast_mutex_unlock(&p->lock);
863 ast_mutex_lock(&p->lock);
865 ast_queue_frame(p->owner, f);
866 ast_mutex_unlock(&p->owner->lock);
874 ast_mutex_lock(&pri->lock);
878 static int restore_gains(struct zt_pvt *p);
880 static void swap_subs(struct zt_pvt *p, int a, int b)
884 struct ast_channel *towner;
886 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
888 tchan = p->subs[a].chan;
889 towner = p->subs[a].owner;
890 tinthreeway = p->subs[a].inthreeway;
892 p->subs[a].chan = p->subs[b].chan;
893 p->subs[a].owner = p->subs[b].owner;
894 p->subs[a].inthreeway = p->subs[b].inthreeway;
896 p->subs[b].chan = tchan;
897 p->subs[b].owner = towner;
898 p->subs[b].inthreeway = tinthreeway;
900 if (p->subs[a].owner)
901 p->subs[a].owner->fds[0] = p->subs[a].zfd;
902 if (p->subs[b].owner)
903 p->subs[b].owner->fds[0] = p->subs[b].zfd;
904 wakeup_sub(p, a, NULL);
905 wakeup_sub(p, b, NULL);
908 static int zt_open(char *fn)
916 for (x=0;x<strlen(fn);x++) {
917 if (!isdigit(fn[x])) {
925 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
928 fn = "/dev/zap/channel";
930 fd = open(fn, O_RDWR | O_NONBLOCK);
932 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
936 if (ioctl(fd, ZT_SPECIFY, &chan)) {
940 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
945 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
949 static void zt_close(int fd)
955 int zt_setlinear(int zfd, int linear)
958 res = ioctl(zfd, ZT_SETLINEAR, &linear);
965 int zt_setlaw(int zfd, int law)
968 res = ioctl(zfd, ZT_SETLAW, &law);
974 static int alloc_sub(struct zt_pvt *p, int x)
978 if (p->subs[x].zfd < 0) {
979 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
980 if (p->subs[x].zfd > -1) {
981 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
983 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
984 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
985 bi.numbufs = numbufs;
986 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
988 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
991 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
992 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
993 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
994 zt_close(p->subs[x].zfd);
999 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1002 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1005 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1009 static int unalloc_sub(struct zt_pvt *p, int x)
1012 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1015 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
1016 if (p->subs[x].zfd > -1) {
1017 zt_close(p->subs[x].zfd);
1019 p->subs[x].zfd = -1;
1020 p->subs[x].linear = 0;
1021 p->subs[x].chan = 0;
1022 p->subs[x].owner = NULL;
1023 p->subs[x].inthreeway = 0;
1024 p->polarity = POLARITY_IDLE;
1025 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1029 static int zt_digit(struct ast_channel *ast, char digit)
1031 ZT_DIAL_OPERATION zo;
1036 ast_mutex_lock(&p->lock);
1037 index = zt_get_index(ast, p, 0);
1038 if ((index == SUB_REAL) && p->owner) {
1040 if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
1042 if (!pri_grab(p, p->pri)) {
1043 pri_information(p->pri->pri,p->call,digit);
1046 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1047 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
1048 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1049 res = strlen(p->dialdest);
1050 p->dialdest[res++] = digit;
1051 p->dialdest[res] = '\0';
1057 zo.op = ZT_DIAL_OP_APPEND;
1058 zo.dialstr[0] = 'T';
1059 zo.dialstr[1] = digit;
1061 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1062 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1067 ast_mutex_unlock(&p->lock);
1071 static char *events[] = {
1084 "Hook Transition Complete",
1089 "Polarity Reversal",
1097 { ZT_ALARM_RED, "Red Alarm" },
1098 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1099 { ZT_ALARM_BLUE, "Blue Alarm" },
1100 { ZT_ALARM_RECOVER, "Recovering" },
1101 { ZT_ALARM_LOOPBACK, "Loopback" },
1102 { ZT_ALARM_NOTOPEN, "Not Open" },
1103 { ZT_ALARM_NONE, "None" },
1106 static char *alarm2str(int alarm)
1109 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
1110 if (alarms[x].alarm & alarm)
1111 return alarms[x].name;
1113 return alarm ? "Unknown Alarm" : "No Alarm";
1116 static char *event2str(int event)
1118 static char buf[256];
1119 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1120 return events[event];
1121 sprintf(buf, "Event %d", event); /* safe */
1126 static char *dialplan2str(int dialplan)
1128 if (dialplan == -1) {
1129 return("Dynamically set dialplan in ISDN");
1131 return(pri_plan2str(dialplan));
1136 static int str2r2prot(char *swtype)
1138 if (!strcasecmp(swtype, "ar"))
1139 return MFCR2_PROT_ARGENTINA;
1141 if (!strcasecmp(swtype, "cn"))
1142 return MFCR2_PROT_CHINA;
1144 if (!strcasecmp(swtype, "kr"))
1145 return MFCR2_PROT_KOREA;
1151 static char *zap_sig2str(int sig)
1153 static char buf[256];
1156 return "E & M Immediate";
1158 return "E & M Wink";
1162 return "Feature Group D (DTMF)";
1164 return "Feature Group D (MF)";
1165 case SIG_FEATDMF_TA:
1166 return "Feature Groud D (MF) Tandem Access";
1168 return "Feature Group B (MF)";
1172 return "FGC/CAMA (Dialpulse)";
1173 case SIG_FGC_CAMAMF:
1174 return "FGC/CAMA (MF)";
1176 return "FXS Loopstart";
1178 return "FXS Groundstart";
1180 return "FXS Kewlstart";
1182 return "FXO Loopstart";
1184 return "FXO Groundstart";
1186 return "FXO Kewlstart";
1188 return "PRI Signalling";
1190 return "R2 Signalling";
1192 return "SF (Tone) Signalling Immediate";
1194 return "SF (Tone) Signalling Wink";
1196 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1197 case SIG_SF_FEATDMF:
1198 return "SF (Tone) Signalling with Feature Group D (MF)";
1200 return "SF (Tone) Signalling with Feature Group B (MF)";
1201 case SIG_GR303FXOKS:
1202 return "GR-303 Signalling with FXOKS";
1203 case SIG_GR303FXSKS:
1204 return "GR-303 Signalling with FXSKS";
1206 return "Pseudo Signalling";
1208 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1213 #define sig2str zap_sig2str
1215 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1217 /* If the conference already exists, and we're already in it
1218 don't bother doing anything */
1221 memset(&zi, 0, sizeof(zi));
1224 if (slavechannel > 0) {
1225 /* If we have only one slave, do a digital mon */
1226 zi.confmode = ZT_CONF_DIGITALMON;
1227 zi.confno = slavechannel;
1230 /* Real-side and pseudo-side both participate in conference */
1231 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1232 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1234 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1235 zi.confno = p->confno;
1237 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1241 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1242 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1245 if (slavechannel < 1) {
1246 p->confno = zi.confno;
1248 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1249 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1253 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1255 /* If they're listening to our channel, they're ours */
1256 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1258 /* If they're a talker on our (allocated) conference, they're ours */
1259 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1264 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1267 if (/* Can't delete if there's no zfd */
1269 /* Don't delete from the conference if it's not our conference */
1271 /* Don't delete if we don't think it's conferenced at all (implied) */
1273 memset(&zi, 0, sizeof(zi));
1277 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1278 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1281 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1282 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1286 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1290 struct zt_pvt *slave = NULL;
1291 /* Start out optimistic */
1293 /* Update conference state in a stateless fashion */
1295 /* Any three-way calling makes slave native mode *definitely* out
1297 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1300 /* If we don't have any 3-way calls, check to see if we have
1301 precisely one slave */
1302 if (useslavenative) {
1303 for (x=0;x<MAX_SLAVES;x++) {
1306 /* Whoops already have a slave! No
1307 slave native and stop right away */
1312 /* We have one slave so far */
1313 slave = p->slaves[x];
1318 /* If no slave, slave native definitely out */
1321 else if (slave->law != p->law) {
1327 return useslavenative;
1330 static int reset_conf(struct zt_pvt *p)
1333 memset(&zi, 0, sizeof(zi));
1335 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1336 if (p->subs[SUB_REAL].zfd > -1) {
1337 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1338 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1343 static int update_conf(struct zt_pvt *p)
1348 struct zt_pvt *slave = NULL;
1350 useslavenative = isslavenative(p, &slave);
1351 /* Start with the obvious, general stuff */
1353 /* Look for three way calls */
1354 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1355 conf_add(p, &p->subs[x], x, 0);
1358 conf_del(p, &p->subs[x], x);
1361 /* If we have a slave, add him to our conference now. or DAX
1362 if this is slave native */
1363 for (x=0;x<MAX_SLAVES;x++) {
1366 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1368 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1373 /* If we're supposed to be in there, do so now */
1374 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1376 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1378 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1382 /* If we have a master, add ourselves to his conference */
1384 if (isslavenative(p->master, NULL)) {
1385 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1387 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1391 /* Nobody is left (or should be left) in our conference.
1395 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1399 static void zt_enable_ec(struct zt_pvt *p)
1406 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1410 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1413 if (p->echocancel) {
1414 if (p->sig == SIG_PRI) {
1416 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1418 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1421 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1423 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1426 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1429 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1432 static void zt_train_ec(struct zt_pvt *p)
1436 if (p && p->echocancel && p->echotraining) {
1437 x = p->echotraining;
1438 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1440 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1442 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1445 ast_log(LOG_DEBUG, "No echo training requested\n");
1448 static void zt_disable_ec(struct zt_pvt *p)
1452 if (p->echocancel) {
1454 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1456 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1458 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1463 static void fill_txgain(struct zt_gains *g, float gain, int law)
1467 float linear_gain = pow(10.0, gain / 20.0);
1471 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1473 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1474 if (k > 32767) k = 32767;
1475 if (k < -32767) k = -32767;
1476 g->txgain[j] = AST_LIN2A(k);
1483 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1485 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1486 if (k > 32767) k = 32767;
1487 if (k < -32767) k = -32767;
1488 g->txgain[j] = AST_LIN2MU(k);
1497 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1501 float linear_gain = pow(10.0, gain / 20.0);
1505 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1507 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1508 if (k > 32767) k = 32767;
1509 if (k < -32767) k = -32767;
1510 g->rxgain[j] = AST_LIN2A(k);
1517 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1519 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1520 if (k > 32767) k = 32767;
1521 if (k < -32767) k = -32767;
1522 g->rxgain[j] = AST_LIN2MU(k);
1531 int set_actual_txgain(int fd, int chan, float gain, int law)
1536 memset(&g, 0, sizeof(g));
1538 res = ioctl(fd, ZT_GETGAINS, &g);
1540 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1544 fill_txgain(&g, gain, law);
1546 return ioctl(fd, ZT_SETGAINS, &g);
1549 int set_actual_rxgain(int fd, int chan, float gain, int law)
1554 memset(&g, 0, sizeof(g));
1556 res = ioctl(fd, ZT_GETGAINS, &g);
1558 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1562 fill_rxgain(&g, gain, law);
1564 return ioctl(fd, ZT_SETGAINS, &g);
1567 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1569 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1572 static int bump_gains(struct zt_pvt *p)
1576 /* Bump receive gain by 5.0db */
1577 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
1579 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1586 static int restore_gains(struct zt_pvt *p)
1590 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1592 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1599 static inline int zt_set_hook(int fd, int hs)
1603 res = ioctl(fd, ZT_HOOK, &x);
1606 if (errno == EINPROGRESS) return 0;
1607 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1612 static inline int zt_confmute(struct zt_pvt *p, int muted)
1616 if (p->sig == SIG_PRI) {
1618 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1620 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1622 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1624 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1628 static int save_conference(struct zt_pvt *p)
1630 struct zt_confinfo c;
1632 if (p->saveconf.confmode) {
1633 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1636 p->saveconf.chan = 0;
1637 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1639 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1640 p->saveconf.confmode = 0;
1645 c.confmode = ZT_CONF_NORMAL;
1646 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1648 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1652 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1656 static int restore_conference(struct zt_pvt *p)
1659 if (p->saveconf.confmode) {
1660 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1661 p->saveconf.confmode = 0;
1663 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1668 ast_log(LOG_DEBUG, "Restored conferencing\n");
1672 static int send_callerid(struct zt_pvt *p);
1674 int send_cwcidspill(struct zt_pvt *p)
1678 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1680 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1681 /* Make sure we account for the end */
1682 p->cidlen += READ_SIZE * 4;
1685 if (option_verbose > 2)
1686 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1690 static int has_voicemail(struct zt_pvt *p)
1693 return ast_app_has_voicemail(p->mailbox, NULL);
1696 static int send_callerid(struct zt_pvt *p)
1698 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1700 /* Take out of linear mode if necessary */
1701 if (p->subs[SUB_REAL].linear) {
1702 p->subs[SUB_REAL].linear = 0;
1703 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1705 while(p->cidpos < p->cidlen) {
1706 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1708 if (errno == EAGAIN)
1711 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1721 if (p->callwaitcas) {
1722 /* Wait for CID/CW to expire */
1723 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1725 restore_conference(p);
1729 static int zt_callwait(struct ast_channel *ast)
1731 struct zt_pvt *p = ast->tech_pvt;
1732 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1734 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1737 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1741 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1742 if (!p->callwaitrings && p->callwaitingcallerid) {
1743 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1745 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1747 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1749 p->cidlen = 2400 + READ_SIZE * 4;
1757 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1759 struct zt_pvt *p = ast->tech_pvt;
1765 char dest[256]; /* must be same length as p->dialdest */
1766 ast_mutex_lock(&p->lock);
1767 ast_copy_string(dest, rdest, sizeof(dest));
1768 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1769 if ((ast->_state == AST_STATE_BUSY)) {
1770 p->subs[SUB_REAL].needbusy = 1;
1771 ast_mutex_unlock(&p->lock);
1774 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1775 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1776 ast_mutex_unlock(&p->lock);
1780 if (p->radio) /* if a radio channel, up immediately */
1782 /* Special pseudo -- automatically up */
1783 ast_setstate(ast, AST_STATE_UP);
1784 ast_mutex_unlock(&p->lock);
1787 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1788 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1790 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1793 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1799 if (p->owner == ast) {
1800 /* Normal ring, on hook */
1802 /* Don't send audio while on hook, until the call is answered */
1804 if (p->use_callerid) {
1805 /* Generate the Caller-ID spill if desired */
1807 ast_log(LOG_WARNING, "cidspill already exists??\n");
1811 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1812 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1817 /* Choose proper cadence */
1818 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1819 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1820 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1821 p->cidrings = cidrings[p->distinctivering - 1];
1823 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1824 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1825 p->cidrings = p->sendcalleridafter;
1829 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1830 c = strchr(dest, '/');
1833 if (c && (strlen(c) < p->stripmsd)) {
1834 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1838 p->dop.op = ZT_DIAL_OP_REPLACE;
1839 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1840 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1842 p->dop.dialstr[0] = '\0';
1845 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1846 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1847 ast_mutex_unlock(&p->lock);
1852 /* Call waiting call */
1853 p->callwaitrings = 0;
1854 if (ast->cid.cid_num)
1855 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
1857 p->callwait_num[0] = '\0';
1858 if (ast->cid.cid_name)
1859 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
1861 p->callwait_name[0] = '\0';
1862 /* Call waiting tone instead */
1863 if (zt_callwait(ast)) {
1864 ast_mutex_unlock(&p->lock);
1867 /* Make ring-back */
1868 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1869 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1872 n = ast->cid.cid_name;
1873 l = ast->cid.cid_num;
1875 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1877 p->lastcid_num[0] = '\0';
1879 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1881 p->lastcid_name[0] = '\0';
1882 ast_setstate(ast, AST_STATE_RINGING);
1883 index = zt_get_index(ast, p, 0);
1885 p->subs[index].needringing = 1;
1898 case SIG_FGC_CAMAMF:
1903 case SIG_SF_FEATDMF:
1904 case SIG_FEATDMF_TA:
1906 c = strchr(dest, '/');
1911 if (strlen(c) < p->stripmsd) {
1912 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1913 ast_mutex_unlock(&p->lock);
1917 /* Start the trunk, if not GR-303 */
1921 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1923 if (errno != EINPROGRESS) {
1924 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1925 ast_mutex_unlock(&p->lock);
1932 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1933 p->dop.op = ZT_DIAL_OP_REPLACE;
1939 l = ast->cid.cid_num;
1941 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1943 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1946 l = ast->cid.cid_num;
1948 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1950 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1952 case SIG_FEATDMF_TA:
1954 const char *cic, *ozz;
1956 /* If you have to go through a Tandem Access point you need to use this */
1957 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1960 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1964 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1965 ast_mutex_unlock(&p->lock);
1968 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1969 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1974 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1977 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1979 case SIG_FGC_CAMAMF:
1981 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1985 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1987 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1991 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1992 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1993 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1994 p->echorest[sizeof(p->echorest) - 1] = '\0';
1996 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2000 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2002 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2003 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2004 ast_mutex_unlock(&p->lock);
2008 ast_log(LOG_DEBUG, "Deferring dialing...\n");
2010 if (ast_strlen_zero(c))
2012 ast_setstate(ast, AST_STATE_DIALING);
2015 /* Special pseudo -- automatically up*/
2016 ast_setstate(ast, AST_STATE_UP);
2019 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2020 p->dialdest[0] = '\0';
2023 ast_log(LOG_DEBUG, "not yet implemented\n");
2024 ast_mutex_unlock(&p->lock);
2030 #ifdef SUPPORT_USERUSER
2031 const char *useruser;
2035 int prilocaldialplan;
2039 c = strchr(dest, '/');
2044 if (!p->hidecallerid) {
2045 l = ast->cid.cid_num;
2046 n = ast->cid.cid_name;
2051 if (strlen(c) < p->stripmsd) {
2052 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2053 ast_mutex_unlock(&p->lock);
2056 if (p->sig != SIG_FXSKS) {
2057 p->dop.op = ZT_DIAL_OP_REPLACE;
2058 s = strchr(c + p->stripmsd, 'w');
2061 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2063 p->dop.dialstr[0] = '\0';
2066 p->dop.dialstr[0] = '\0';
2069 if (pri_grab(p, p->pri)) {
2070 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2071 ast_mutex_unlock(&p->lock);
2074 if (!(p->call = pri_new_call(p->pri->pri))) {
2075 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2077 ast_mutex_unlock(&p->lock);
2080 if (!(sr = pri_sr_new())) {
2081 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2083 ast_mutex_unlock(&p->lock);
2085 if (p->bearer || (p->sig == SIG_FXSKS)) {
2087 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);
2088 p->bearer->call = p->call;
2090 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
2091 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2093 p->digital = IS_DIGITAL(ast->transfercapability);
2094 /* Add support for exclusive override */
2095 if (p->priexclusive)
2098 /* otherwise, traditional behavior */
2099 if (p->pri->nodetype == PRI_NETWORK)
2105 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2106 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2108 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2109 if (p->pri->facilityenable)
2110 pri_facility_enable(p->pri->pri);
2112 if (option_verbose > 2)
2113 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2115 pridialplan = p->pri->dialplan - 1;
2116 if (pridialplan == -2) { /* compute dynamically */
2117 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2118 dp_strip = strlen(p->pri->internationalprefix);
2119 pridialplan = PRI_INTERNATIONAL_ISDN;
2120 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2121 dp_strip = strlen(p->pri->nationalprefix);
2122 pridialplan = PRI_NATIONAL_ISDN;
2124 pridialplan = PRI_LOCAL_ISDN;
2127 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2130 prilocaldialplan = p->pri->localdialplan - 1;
2131 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
2132 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2133 ldp_strip = strlen(p->pri->internationalprefix);
2134 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2135 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2136 ldp_strip = strlen(p->pri->nationalprefix);
2137 prilocaldialplan = PRI_NATIONAL_ISDN;
2139 prilocaldialplan = PRI_LOCAL_ISDN;
2142 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2143 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
2144 PRES_NUMBER_NOT_AVAILABLE);
2145 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
2147 #ifdef SUPPORT_USERUSER
2148 /* User-user info */
2149 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2152 pri_sr_set_useruser(sr, useruser);
2155 if (pri_setup(p->pri->pri, p->call, sr)) {
2156 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2157 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2159 ast_mutex_unlock(&p->lock);
2164 ast_setstate(ast, AST_STATE_DIALING);
2168 ast_mutex_unlock(&p->lock);
2172 static void destroy_zt_pvt(struct zt_pvt **pvt)
2174 struct zt_pvt *p = *pvt;
2175 /* Remove channel from the list */
2177 p->prev->next = p->next;
2179 p->next->prev = p->prev;
2182 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2184 ast_mutex_destroy(&p->lock);
2189 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2199 for (i = 0; i < 3; i++) {
2200 if (cur->subs[i].owner) {
2206 prev->next = cur->next;
2208 prev->next->prev = prev;
2214 iflist->prev = NULL;
2218 if (cur->subs[SUB_REAL].zfd > -1) {
2219 zt_close(cur->subs[SUB_REAL].zfd);
2221 destroy_zt_pvt(&cur);
2225 prev->next = cur->next;
2227 prev->next->prev = prev;
2233 iflist->prev = NULL;
2237 if (cur->subs[SUB_REAL].zfd > -1) {
2238 zt_close(cur->subs[SUB_REAL].zfd);
2240 destroy_zt_pvt(&cur);
2246 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2248 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2250 static char *zap_send_keypad_facility_descrip =
2251 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2252 " IE over the current channel.\n";
2254 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2256 /* Data will be our digit string */
2258 char *digits = (char *) data;
2260 if (ast_strlen_zero(digits)) {
2261 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
2265 p = (struct zt_pvt *)chan->tech_pvt;
2268 ast_log(LOG_DEBUG, "Unable to find technology private\n");
2272 ast_mutex_lock(&p->lock);
2274 if (!p->pri || !p->call) {
2275 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2276 ast_mutex_unlock(&p->lock);
2280 if (!pri_grab(p, p->pri)) {
2281 pri_keypad_facility(p->pri->pri, p->call, digits);
2284 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
2285 ast_mutex_unlock(&p->lock);
2289 ast_mutex_unlock(&p->lock);
2294 int pri_is_up(struct zt_pri *pri)
2297 for (x=0;x<NUM_DCHANS;x++) {
2298 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2304 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2306 bearer->owner = &inuse;
2307 bearer->realcall = crv;
2308 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2309 if (crv->subs[SUB_REAL].owner)
2310 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2311 crv->bearer = bearer;
2312 crv->call = bearer->call;
2317 static char *pri_order(int level)
2327 return "Quaternary";
2333 /* Returns fd of the active dchan */
2334 int pri_active_dchan_fd(struct zt_pri *pri)
2338 for (x = 0; x < NUM_DCHANS; x++) {
2339 if ((pri->dchans[x] == pri->pri))
2346 int pri_find_dchan(struct zt_pri *pri)
2353 for(x=0;x<NUM_DCHANS;x++) {
2354 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2356 if (pri->dchans[x] == old) {
2362 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2363 pri->dchannels[newslot]);
2365 if (old && (oldslot != newslot))
2366 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2367 pri->dchannels[oldslot], pri->dchannels[newslot]);
2368 pri->pri = pri->dchans[newslot];
2373 static int zt_hangup(struct ast_channel *ast)
2377 /*static int restore_gains(struct zt_pvt *p);*/
2378 struct zt_pvt *p = ast->tech_pvt;
2379 struct zt_pvt *tmp = NULL;
2380 struct zt_pvt *prev = NULL;
2384 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2385 if (!ast->tech_pvt) {
2386 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2390 ast_mutex_lock(&p->lock);
2392 index = zt_get_index(ast, p, 1);
2394 if (p->sig == SIG_PRI) {
2396 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2402 if (p->origcid_num) {
2403 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2404 free(p->origcid_num);
2405 p->origcid_num = NULL;
2407 if (p->origcid_name) {
2408 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2409 free(p->origcid_name);
2410 p->origcid_name = NULL;
2413 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2417 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2418 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2422 /* Real channel, do some fixup */
2423 p->subs[index].owner = NULL;
2424 p->subs[index].needanswer = 0;
2425 p->subs[index].needflash = 0;
2426 p->subs[index].needringing = 0;
2427 p->subs[index].needbusy = 0;
2428 p->subs[index].needcongestion = 0;
2429 p->subs[index].linear = 0;
2430 p->subs[index].needcallerid = 0;
2431 p->polarity = POLARITY_IDLE;
2432 zt_setlinear(p->subs[index].zfd, 0);
2433 if (index == SUB_REAL) {
2434 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2435 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2436 if (p->subs[SUB_CALLWAIT].inthreeway) {
2437 /* We had flipped over to answer a callwait and now it's gone */
2438 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2439 /* Move to the call-wait, but un-own us until they flip back. */
2440 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2441 unalloc_sub(p, SUB_CALLWAIT);
2444 /* The three way hung up, but we still have a call wait */
2445 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2446 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2447 unalloc_sub(p, SUB_THREEWAY);
2448 if (p->subs[SUB_REAL].inthreeway) {
2449 /* This was part of a three way call. Immediately make way for
2451 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2452 p->owner = p->subs[SUB_REAL].owner;
2454 /* This call hasn't been completed yet... Set owner to NULL */
2455 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2458 p->subs[SUB_REAL].inthreeway = 0;
2460 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2461 /* Move to the call-wait and switch back to them. */
2462 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2463 unalloc_sub(p, SUB_CALLWAIT);
2464 p->owner = p->subs[SUB_REAL].owner;
2465 if (p->owner->_state != AST_STATE_UP)
2466 p->subs[SUB_REAL].needanswer = 1;
2467 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2468 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2469 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2470 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2471 unalloc_sub(p, SUB_THREEWAY);
2472 if (p->subs[SUB_REAL].inthreeway) {
2473 /* This was part of a three way call. Immediately make way for
2475 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2476 p->owner = p->subs[SUB_REAL].owner;
2478 /* This call hasn't been completed yet... Set owner to NULL */
2479 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2482 p->subs[SUB_REAL].inthreeway = 0;
2484 } else if (index == SUB_CALLWAIT) {
2485 /* Ditch the holding callwait call, and immediately make it availabe */
2486 if (p->subs[SUB_CALLWAIT].inthreeway) {
2487 /* This is actually part of a three way, placed on hold. Place the third part
2488 on music on hold now */
2489 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2490 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2491 p->subs[SUB_THREEWAY].inthreeway = 0;
2492 /* Make it the call wait now */
2493 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2494 unalloc_sub(p, SUB_THREEWAY);
2496 unalloc_sub(p, SUB_CALLWAIT);
2497 } else if (index == SUB_THREEWAY) {
2498 if (p->subs[SUB_CALLWAIT].inthreeway) {
2499 /* The other party of the three way call is currently in a call-wait state.
2500 Start music on hold for them, and take the main guy out of the third call */
2501 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2502 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2503 p->subs[SUB_CALLWAIT].inthreeway = 0;
2505 p->subs[SUB_REAL].inthreeway = 0;
2506 /* If this was part of a three way call index, let us make
2507 another three way call */
2508 unalloc_sub(p, SUB_THREEWAY);
2510 /* This wasn't any sort of call, but how are we an index? */
2511 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2516 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2519 p->distinctivering = 0;
2520 p->confirmanswer = 0;
2526 p->onhooktime = time(NULL);
2534 ast_dsp_free(p->dsp);
2538 law = ZT_LAW_DEFAULT;
2539 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2541 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2542 /* Perform low level hangup if no owner left */
2545 #ifdef SUPPORT_USERUSER
2546 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2549 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2550 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2551 if (!pri_grab(p, p->pri)) {
2552 if (p->alreadyhungup) {
2553 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2555 #ifdef SUPPORT_USERUSER
2556 pri_call_set_useruser(p->call, useruser);
2559 pri_hangup(p->pri->pri, p->call, -1);
2562 p->bearer->call = NULL;
2564 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2565 int icause = ast->hangupcause ? ast->hangupcause : -1;
2566 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2568 #ifdef SUPPORT_USERUSER
2569 pri_call_set_useruser(p->call, useruser);
2572 p->alreadyhungup = 1;
2574 p->bearer->alreadyhungup = 1;
2577 icause = atoi(cause);
2579 pri_hangup(p->pri->pri, p->call, icause);
2582 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2585 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2590 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2597 if (p->sig == SIG_R2) {
2599 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2607 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2608 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2610 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2616 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2619 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2621 /* If they're off hook, try playing congestion */
2622 if ((par.rxisoffhook) && (!p->radio))
2623 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2625 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2631 /* Make sure we're not made available for at least two seconds assuming
2632 we were actually used for an inbound or outbound call. */
2633 if (ast->_state != AST_STATE_RESERVED) {
2634 time(&p->guardtime);
2639 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2646 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2647 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2651 p->callwaiting = p->permcallwaiting;
2652 p->hidecallerid = p->permhidecallerid;
2657 /* Restore data mode */
2658 if (p->sig == SIG_PRI) {
2660 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2664 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2665 /* Free up the bearer channel as well, and
2666 don't use its file descriptor anymore */
2667 update_conf(p->bearer);
2668 reset_conf(p->bearer);
2669 p->bearer->owner = NULL;
2670 p->bearer->realcall = NULL;
2672 p->subs[SUB_REAL].zfd = -1;
2680 p->callwaitingrepeat = 0;
2682 ast->tech_pvt = NULL;
2683 ast_mutex_unlock(&p->lock);
2684 ast_mutex_lock(&usecnt_lock);
2687 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2688 ast_mutex_unlock(&usecnt_lock);
2689 ast_update_use_count();
2690 if (option_verbose > 2)
2691 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2693 ast_mutex_lock(&iflock);
2699 destroy_channel(prev, tmp, 0);
2707 ast_mutex_unlock(&iflock);
2711 static int zt_answer(struct ast_channel *ast)
2713 struct zt_pvt *p = ast->tech_pvt;
2716 int oldstate = ast->_state;
2717 ast_setstate(ast, AST_STATE_UP);
2718 ast_mutex_lock(&p->lock);
2719 index = zt_get_index(ast, p, 0);
2722 /* nothing to do if a radio channel */
2724 ast_mutex_unlock(&p->lock);
2738 case SIG_FEATDMF_TA:
2741 case SIG_FGC_CAMAMF:
2746 case SIG_SF_FEATDMF:
2751 /* Pick up the line */
2752 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2753 if(p->hanguponpolarityswitch) {
2754 gettimeofday(&p->polaritydelaytv, NULL);
2756 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2757 tone_zone_play_tone(p->subs[index].zfd, -1);
2759 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2760 if (oldstate == AST_STATE_RINGING) {
2761 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2762 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2763 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2764 p->owner = p->subs[SUB_REAL].owner;
2767 if (p->sig & __ZT_SIG_FXS) {
2774 /* Send a pri acknowledge */
2775 if (!pri_grab(p, p->pri)) {
2777 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2780 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2787 res = mfcr2_AnswerCall(p->r2, NULL);
2789 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2793 ast_mutex_unlock(&p->lock);
2796 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2799 ast_mutex_unlock(&p->lock);
2803 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2809 struct zt_pvt *p = chan->tech_pvt;
2811 /* all supported options require data */
2812 if (!data || (datalen < 1)) {
2818 case AST_OPTION_TXGAIN:
2819 scp = (signed char *) data;
2820 index = zt_get_index(chan, p, 0);
2822 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
2825 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
2826 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
2827 case AST_OPTION_RXGAIN:
2828 scp = (signed char *) data;
2829 index = zt_get_index(chan, p, 0);
2831 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
2834 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
2835 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
2836 case AST_OPTION_TONE_VERIFY:
2842 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2843 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2846 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2847 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2850 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2851 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2855 case AST_OPTION_TDD:
2856 /* turn on or off TDD */
2859 if (!*cp) { /* turn it off */
2860 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2861 if (p->tdd) tdd_free(p->tdd);
2865 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
2866 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
2868 /* otherwise, turn it on */
2869 if (!p->didtdd) { /* if havent done it yet */
2870 unsigned char mybuf[41000],*buf;
2871 int size,res,fd,len;
2872 struct pollfd fds[1];
2875 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
2876 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
2878 index = zt_get_index(chan, p, 0);
2880 ast_log(LOG_WARNING, "No index in TDD?\n");
2883 fd = p->subs[index].zfd;
2885 if (ast_check_hangup(chan)) return -1;
2887 if (size > READ_SIZE)
2890 fds[0].events = POLLPRI | POLLOUT;
2892 res = poll(fds, 1, -1);
2894 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2897 /* if got exception */
2898 if (fds[0].revents & POLLPRI) return -1;
2899 if (!(fds[0].revents & POLLOUT)) {
2900 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2903 res = write(fd, buf, size);
2905 if (res == -1) return -1;
2906 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2912 p->didtdd = 1; /* set to have done it now */
2914 if (*cp == 2) { /* Mate mode */
2915 if (p->tdd) tdd_free(p->tdd);
2920 if (!p->tdd) { /* if we dont have one yet */
2921 p->tdd = tdd_new(); /* allocate one */
2924 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2928 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
2929 *cp ? "ON" : "OFF", (int) *cp, chan->name);
2930 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
2932 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2935 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
2939 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
2942 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2943 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2951 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2953 /* Unlink a specific slave or all slaves/masters from a given master */
2959 ast_mutex_lock(&master->lock);
2961 while(ast_mutex_trylock(&slave->lock)) {
2962 ast_mutex_unlock(&master->lock);
2964 ast_mutex_lock(&master->lock);
2969 for (x=0;x<MAX_SLAVES;x++) {
2970 if (master->slaves[x]) {
2971 if (!slave || (master->slaves[x] == slave)) {
2972 /* Take slave out of the conference */
2973 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2974 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2975 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2976 master->slaves[x]->master = NULL;
2977 master->slaves[x] = NULL;
2982 master->inconference = 0;
2985 if (master->master) {
2986 /* Take master out of the conference */
2987 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2988 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2990 for (x=0;x<MAX_SLAVES;x++) {
2991 if (master->master->slaves[x] == master)
2992 master->master->slaves[x] = NULL;
2993 else if (master->master->slaves[x])
2997 master->master->inconference = 0;
2999 master->master = NULL;
3001 update_conf(master);
3004 ast_mutex_unlock(&slave->lock);
3005 ast_mutex_unlock(&master->lock);
3009 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
3011 if (!slave || !master) {
3012 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
3015 for (x=0;x<MAX_SLAVES;x++) {
3016 if (!master->slaves[x]) {
3017 master->slaves[x] = slave;
3021 if (x >= MAX_SLAVES) {
3022 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
3023 master->slaves[MAX_SLAVES - 1] = slave;
3026 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
3027 slave->master = master;
3029 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
3032 static void disable_dtmf_detect(struct zt_pvt *p)
3034 #ifdef ZT_TONEDETECT
3040 #ifdef ZT_TONEDETECT
3042 ioctl(p->subs[SUB_REAL].zfd,&nb