2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, 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
44 #include <sys/signal.h>
48 #if !defined(SOLARIS) && !defined(__FreeBSD__)
52 #include <sys/ioctl.h>
54 #include <linux/zaptel.h>
57 #endif /* __linux__ */
63 #ifndef PRI_KEYPAD_FACILITY_TX
64 #error "You need newer libpri"
73 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
75 #include "asterisk/lock.h"
76 #include "asterisk/channel.h"
77 #include "asterisk/config.h"
78 #include "asterisk/logger.h"
79 #include "asterisk/module.h"
80 #include "asterisk/pbx.h"
81 #include "asterisk/options.h"
82 #include "asterisk/file.h"
83 #include "asterisk/ulaw.h"
84 #include "asterisk/alaw.h"
85 #include "asterisk/callerid.h"
86 #include "asterisk/adsi.h"
87 #include "asterisk/cli.h"
88 #include "asterisk/cdr.h"
89 #include "asterisk/features.h"
90 #include "asterisk/musiconhold.h"
91 #include "asterisk/say.h"
92 #include "asterisk/tdd.h"
93 #include "asterisk/app.h"
94 #include "asterisk/dsp.h"
95 #include "asterisk/astdb.h"
96 #include "asterisk/manager.h"
97 #include "asterisk/causes.h"
98 #include "asterisk/term.h"
99 #include "asterisk/utils.h"
100 #include "asterisk/transcap.h"
103 #error "Your zaptel is too old. please cvs update"
106 #ifndef ZT_TONEDETECT
107 /* Work around older code with no tone detect */
108 #define ZT_EVENT_DTMFDOWN 0
109 #define ZT_EVENT_DTMFUP 0
113 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
114 * the user hangs up to reset the state machine so ring works properly.
115 * This is used to be able to support kewlstart by putting the zhone in
116 * groundstart mode since their forward disconnect supervision is entirely
117 * broken even though their documentation says it isn't and their support
118 * is entirely unwilling to provide any assistance with their channel banks
119 * even though their web site says they support their products for life.
121 /* #define ZHONE_HACK */
124 * Define if you want to check the hook state for an FXO (FXS signalled) interface
125 * before dialing on it. Certain FXO interfaces always think they're out of
126 * service with this method however.
128 /* #define ZAP_CHECK_HOOKSTATE */
130 /*! \brief Typically, how many rings before we should send Caller*ID */
131 #define DEFAULT_CIDRINGS 1
133 #define CHANNEL_PSEUDO -12
135 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
137 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
138 #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))
140 static const char desc[] = "Zapata Telephony"
149 static const char tdesc[] = "Zapata Telephony Driver"
158 static const char type[] = "Zap";
159 static const char config[] = "zapata.conf";
161 #define SIG_EM ZT_SIG_EM
162 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
163 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
164 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
165 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
166 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
167 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
168 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
169 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
170 #define SIG_FXSLS ZT_SIG_FXSLS
171 #define SIG_FXSGS ZT_SIG_FXSGS
172 #define SIG_FXSKS ZT_SIG_FXSKS
173 #define SIG_FXOLS ZT_SIG_FXOLS
174 #define SIG_FXOGS ZT_SIG_FXOGS
175 #define SIG_FXOKS ZT_SIG_FXOKS
176 #define SIG_PRI ZT_SIG_CLEAR
177 #define SIG_R2 ZT_SIG_CAS
178 #define SIG_SF ZT_SIG_SF
179 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
180 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
181 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
182 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
183 #define SIG_EM_E1 ZT_SIG_EM_E1
184 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
185 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
188 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
189 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
191 #define CHAN_PSEUDO -2
193 #define DCHAN_PROVISIONED (1 << 0)
194 #define DCHAN_NOTINALARM (1 << 1)
195 #define DCHAN_UP (1 << 2)
197 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
199 static char context[AST_MAX_CONTEXT] = "default";
200 static char cid_num[256] = "";
201 static char cid_name[256] = "";
202 static char defaultcic[64] = "";
203 static char defaultozz[64] = "";
205 static char language[MAX_LANGUAGE] = "";
206 static char musicclass[MAX_MUSICCLASS] = "";
207 static char progzone[10]= "";
209 static int usedistinctiveringdetection = 0;
211 static int transfertobusy = 1;
213 static int use_callerid = 1;
214 static int cid_signalling = CID_SIG_BELL;
215 static int cid_start = CID_START_RING;
216 static int zaptrcallerid = 0;
217 static int cur_signalling = -1;
219 static ast_group_t cur_group = 0;
220 static ast_group_t cur_callergroup = 0;
221 static ast_group_t cur_pickupgroup = 0;
222 static int relaxdtmf = 0;
224 static int immediate = 0;
226 static int stripmsd = 0;
228 static int callwaiting = 0;
230 static int callwaitingcallerid = 0;
232 static int hidecallerid = 0;
234 static int restrictcid = 0;
236 static int use_callingpres = 0;
238 static int callreturn = 0;
240 static int threewaycalling = 0;
242 static int transfer = 0;
244 static int canpark = 0;
246 static int cancallforward = 0;
248 static float rxgain = 0.0;
250 static float txgain = 0.0;
252 static int tonezone = -1;
254 static int echocancel;
256 static int echotraining;
260 static int echocanbridged = 0;
262 static int busydetect = 0;
264 static int busycount = 3;
265 static int busy_tonelength = 0;
266 static int busy_quietlength = 0;
268 static int callprogress = 0;
270 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
272 static char mailbox[AST_MAX_EXTENSION];
274 static int amaflags = 0;
278 static int numbufs = 4;
280 static int cur_prewink = -1;
281 static int cur_preflash = -1;
282 static int cur_wink = -1;
283 static int cur_flash = -1;
284 static int cur_start = -1;
285 static int cur_rxwink = -1;
286 static int cur_rxflash = -1;
287 static int cur_debounce = -1;
288 static int cur_priexclusive = 0;
290 static int priindication_oob = 0;
293 static int minunused = 2;
294 static int minidle = 0;
295 static char idleext[AST_MAX_EXTENSION];
296 static char idledial[AST_MAX_EXTENSION];
297 static int overlapdial = 0;
298 static int facilityenable = 0;
299 static char internationalprefix[10] = "";
300 static char nationalprefix[10] = "";
301 static char localprefix[20] = "";
302 static char privateprefix[20] = "";
303 static char unknownprefix[20] = "";
304 static long resetinterval = 3600; /*!< How often (in seconds) to reset unused channels. Default 1 hour. */
305 static struct ast_channel inuse = { "GR-303InUse" };
306 #ifdef PRI_GETSET_TIMERS
307 static int pritimers[PRI_MAX_TIMERS];
309 static int pridebugfd = -1;
310 static char pridebugfilename[1024]="";
313 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
314 static int firstdigittimeout = 16000;
316 /*! \brief How long to wait for following digits (FXO logic) */
317 static int gendigittimeout = 8000;
319 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
320 static int matchdigittimeout = 3000;
322 static int usecnt =0;
323 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
325 /*! \brief Protect the interface list (of zt_pvt's) */
326 AST_MUTEX_DEFINE_STATIC(iflock);
329 static int ifcount = 0;
332 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
335 /*! \brief Whether we answer on a Polarity Switch event */
336 static int answeronpolarityswitch = 0;
338 /*! \brief Whether we hang up on a Polarity Switch event */
339 static int hanguponpolarityswitch = 0;
341 /*! \brief How long (ms) to ignore Polarity Switch events after we answer a call */
342 static int polarityonanswerdelay = 600;
344 /*! \brief When to send the CallerID signals (rings) */
345 static int sendcalleridafter = DEFAULT_CIDRINGS;
347 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
348 when it's doing something critical. */
349 AST_MUTEX_DEFINE_STATIC(monlock);
351 /*! \brief This is the thread for the monitor which checks for input on the channels
352 which are not currently in use. */
353 static pthread_t monitor_thread = AST_PTHREADT_NULL;
355 static int restart_monitor(void);
357 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);
359 static int zt_sendtext(struct ast_channel *c, const char *text);
361 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
362 static inline int zt_get_event(int fd)
365 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
369 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
370 static inline int zt_wait_event(int fd)
373 i = ZT_IOMUX_SIGEVENT;
374 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
375 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
379 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
380 #define READ_SIZE 160
382 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
383 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
385 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
386 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
387 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
388 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
389 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
395 static int r2prot = -1;
398 static int ringt_base = DEFAULT_RINGT;
402 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
403 #define PRI_CHANNEL(p) ((p) & 0xff)
404 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
405 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
408 pthread_t master; /*!< Thread of master */
409 ast_mutex_t lock; /*!< Mutex */
410 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
411 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
412 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
413 int minunused; /*!< Min # of channels to keep empty */
414 int minidle; /*!< Min # of "idling" calls to keep active */
415 int nodetype; /*!< Node type */
416 int switchtype; /*!< Type of switch to emulate */
417 int nsf; /*!< Network-Specific Facilities */
418 int dialplan; /*!< Dialing plan */
419 int localdialplan; /*!< Local dialing plan */
420 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
421 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
422 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
423 char privateprefix[20]; /*!< for private dialplans */
424 char unknownprefix[20]; /*!< for unknown dialplans */
425 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
426 int trunkgroup; /*!< What our trunkgroup is */
427 int mastertrunkgroup; /*!< What trunk group is our master */
428 int prilogicalspan; /*!< Logical span number within trunk group */
429 int numchans; /*!< Num of channels we represent */
430 int overlapdial; /*!< In overlap dialing mode */
431 int facilityenable; /*!< Enable facility IEs */
432 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
433 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
434 struct pri *pri; /*!< Currently active D-channel */
436 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
441 time_t lastreset; /*!< time when unused channels were last reset */
442 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
443 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
444 struct zt_pvt *crvs; /*!< Member CRV structs */
445 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
449 static struct zt_pri pris[NUM_SPANS];
451 static int pritype = PRI_CPE;
454 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
456 #define DEFAULT_PRI_DEBUG 0
459 static inline void pri_rel(struct zt_pri *pri)
461 ast_mutex_unlock(&pri->lock);
464 static int switchtype = PRI_SWITCH_NI2;
465 static int nsf = PRI_NSF_NONE;
466 static int dialplan = PRI_NATIONAL_ISDN + 1;
467 static int localdialplan = PRI_NATIONAL_ISDN + 1;
470 /*! Shut up the compiler */
474 #define SUB_REAL 0 /*!< Active call */
475 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
476 #define SUB_THREEWAY 2 /*!< Three-way call */
478 /* Polarity states */
479 #define POLARITY_IDLE 0
480 #define POLARITY_REV 1
483 static struct zt_distRings drings;
485 struct distRingData {
488 struct ringContextData {
489 char contextData[AST_MAX_CONTEXT];
491 struct zt_distRings {
492 struct distRingData ringnum[3];
493 struct ringContextData ringContext[3];
496 static char *subnames[] = {
502 struct zt_subchannel {
504 struct ast_channel *owner;
506 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
507 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
508 unsigned int needringing:1;
509 unsigned int needbusy:1;
510 unsigned int needcongestion:1;
511 unsigned int needcallerid:1;
512 unsigned int needanswer:1;
513 unsigned int needflash:1;
514 unsigned int linear:1;
515 unsigned int inthreeway:1;
519 #define CONF_USER_REAL (1 << 0)
520 #define CONF_USER_THIRDCALL (1 << 1)
524 static struct zt_pvt {
526 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
527 /*!< Up to three channels can be associated with this call */
529 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
530 struct zt_subchannel subs[3]; /*!< Sub-channels */
531 struct zt_confinfo saveconf; /*!< Saved conference info */
533 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
534 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
535 int inconference; /*!< If our real should be in the conference */
537 int sig; /*!< Signalling style */
538 int radio; /*!< radio type */
541 int tonezone; /*!< tone zone for this chan, or -1 for default */
542 struct zt_pvt *next; /*!< Next channel in list */
543 struct zt_pvt *prev; /*!< Prev channel in list */
547 unsigned int answeronpolarityswitch:1;
548 unsigned int busydetect:1;
549 unsigned int callreturn:1;
550 unsigned int callwaiting:1;
551 unsigned int callwaitingcallerid:1;
552 unsigned int cancallforward:1;
553 unsigned int canpark:1;
554 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
555 unsigned int destroy:1;
556 unsigned int didtdd:1; /*!< flag to say its done it once */
557 unsigned int dialednone:1;
558 unsigned int dialing:1;
559 unsigned int digital:1;
561 unsigned int echobreak:1;
562 unsigned int echocanbridged:1;
563 unsigned int echocanon:1;
564 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
565 unsigned int firstradio:1;
566 unsigned int hanguponpolarityswitch:1;
567 unsigned int hardwaredtmf:1;
568 unsigned int hidecallerid;
569 unsigned int ignoredtmf:1;
570 unsigned int immediate:1; /*!< Answer before getting digits? */
571 unsigned int inalarm:1;
572 unsigned int mate:1; /*!< flag to say its in MATE mode */
573 unsigned int outgoing:1;
574 unsigned int overlapdial:1;
575 unsigned int permcallwaiting:1;
576 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
577 unsigned int priindication_oob:1;
578 unsigned int priexclusive:1;
579 unsigned int pulse:1;
580 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
581 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
582 unsigned int threewaycalling:1;
583 unsigned int transfer:1;
584 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
585 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
586 unsigned int usedistinctiveringdetection:1;
587 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
588 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
589 #if defined(ZAPATA_PRI)
590 unsigned int alerting:1;
591 unsigned int alreadyhungup:1;
592 unsigned int isidlecall:1;
593 unsigned int proceeding:1;
594 unsigned int progress:1;
595 unsigned int resetting:1;
596 unsigned int setup_ack:1;
598 #if defined(ZAPATA_R2)
599 unsigned int hasr2call:1;
600 unsigned int r2blocked:1;
601 unsigned int sigchecked:1;
604 struct zt_distRings drings;
606 char context[AST_MAX_CONTEXT];
607 char defcontext[AST_MAX_CONTEXT];
608 char exten[AST_MAX_EXTENSION];
609 char language[MAX_LANGUAGE];
610 char musicclass[MAX_MUSICCLASS];
612 char cid_ani[AST_MAX_EXTENSION];
614 char cid_num[AST_MAX_EXTENSION];
615 int cid_ton; /*!< Type Of Number (TON) */
616 char cid_name[AST_MAX_EXTENSION];
617 char lastcid_num[AST_MAX_EXTENSION];
618 char lastcid_name[AST_MAX_EXTENSION];
619 char *origcid_num; /*!< malloced original callerid */
620 char *origcid_name; /*!< malloced original callerid */
621 char callwait_num[AST_MAX_EXTENSION];
622 char callwait_name[AST_MAX_EXTENSION];
623 char rdnis[AST_MAX_EXTENSION];
624 char dnid[AST_MAX_EXTENSION];
627 int confno; /*!< Our conference */
628 int confusers; /*!< Who is using our conference */
629 int propconfno; /*!< Propagated conference number */
630 ast_group_t callgroup;
631 ast_group_t pickupgroup;
632 int channel; /*!< Channel Number or CRV */
633 int span; /*!< Span number */
634 time_t guardtime; /*!< Must wait this much time before using for new call */
635 int cid_signalling; /*!< CID signalling type bell202 or v23 */
636 int cid_start; /*!< CID start indicator, polarity or ring */
637 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
638 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
639 int cidcwexpire; /*!< When to expire our muting for CID/CW */
640 unsigned char *cidspill;
653 int busy_quietlength;
655 struct timeval flashtime; /*!< Last flash-hook time */
657 int cref; /*!< Call reference number */
658 ZT_DIAL_OPERATION dop;
659 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
661 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
662 int amaflags; /*!< AMA Flags */
663 struct tdd_state *tdd; /*!< TDD flag */
664 char call_forward[AST_MAX_EXTENSION];
665 char mailbox[AST_MAX_EXTENSION];
669 int distinctivering; /*!< Which distinctivering to use */
670 int cidrings; /*!< Which ring to deliver CID on */
671 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
673 int polarityonanswerdelay;
674 struct timeval polaritydelaytv;
675 int sendcalleridafter;
678 struct zt_pvt *bearer;
679 struct zt_pvt *realcall;
691 } *iflist = NULL, *ifend = NULL;
693 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
694 static int zt_digit(struct ast_channel *ast, char digit);
695 static int zt_sendtext(struct ast_channel *c, const char *text);
696 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
697 static int zt_hangup(struct ast_channel *ast);
698 static int zt_answer(struct ast_channel *ast);
699 struct ast_frame *zt_read(struct ast_channel *ast);
700 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
701 struct ast_frame *zt_exception(struct ast_channel *ast);
702 static int zt_indicate(struct ast_channel *chan, int condition);
703 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
704 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
706 static const struct ast_channel_tech zap_tech = {
708 .description = tdesc,
709 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
710 .requester = zt_request,
711 .send_digit = zt_digit,
712 .send_text = zt_sendtext,
719 .exception = zt_exception,
720 .indicate = zt_indicate,
722 .setoption = zt_setoption,
726 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
728 #define GET_CHANNEL(p) ((p)->channel)
731 struct zt_pvt *round_robin[32];
734 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
737 /* Grab the lock first */
739 res = ast_mutex_trylock(&pri->lock);
741 ast_mutex_unlock(&pvt->lock);
742 /* Release the lock and try again */
744 ast_mutex_lock(&pvt->lock);
747 /* Then break the poll */
748 pthread_kill(pri->master, SIGURG);
753 #define NUM_CADENCE_MAX 25
754 static int num_cadence = 4;
755 static int user_has_defined_cadences = 0;
757 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
758 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
759 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
760 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
761 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
764 int receivedRingT; /*!< Used to find out what ringtone we are on */
766 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
767 * is 1, the second pause is 2 and so on.
770 static int cidrings[NUM_CADENCE_MAX] = {
771 2, /*!< Right after first long ring */
772 4, /*!< Right after long part */
773 3, /*!< After third chirp */
774 2, /*!< Second spell */
777 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
778 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
780 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
781 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
783 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
786 if (p->subs[0].owner == ast)
788 else if (p->subs[1].owner == ast)
790 else if (p->subs[2].owner == ast)
795 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
801 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
803 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
806 struct ast_frame null = { AST_FRAME_NULL, };
809 ast_mutex_unlock(&pri->lock);
812 if (p->subs[a].owner) {
813 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
814 ast_mutex_unlock(&p->lock);
816 ast_mutex_lock(&p->lock);
818 ast_queue_frame(p->subs[a].owner, &null);
819 ast_mutex_unlock(&p->subs[a].owner->lock);
827 ast_mutex_lock(&pri->lock);
832 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
834 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
837 /* We must unlock the PRI to avoid the possibility of a deadlock */
840 ast_mutex_unlock(&pri->lock);
844 if (ast_mutex_trylock(&p->owner->lock)) {
845 ast_mutex_unlock(&p->lock);
847 ast_mutex_lock(&p->lock);
849 ast_queue_frame(p->owner, f);
850 ast_mutex_unlock(&p->owner->lock);
858 ast_mutex_lock(&pri->lock);
862 static int restore_gains(struct zt_pvt *p);
864 static void swap_subs(struct zt_pvt *p, int a, int b)
868 struct ast_channel *towner;
870 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
872 tchan = p->subs[a].chan;
873 towner = p->subs[a].owner;
874 tinthreeway = p->subs[a].inthreeway;
876 p->subs[a].chan = p->subs[b].chan;
877 p->subs[a].owner = p->subs[b].owner;
878 p->subs[a].inthreeway = p->subs[b].inthreeway;
880 p->subs[b].chan = tchan;
881 p->subs[b].owner = towner;
882 p->subs[b].inthreeway = tinthreeway;
884 if (p->subs[a].owner)
885 p->subs[a].owner->fds[0] = p->subs[a].zfd;
886 if (p->subs[b].owner)
887 p->subs[b].owner->fds[0] = p->subs[b].zfd;
888 wakeup_sub(p, a, NULL);
889 wakeup_sub(p, b, NULL);
892 static int zt_open(char *fn)
900 for (x=0;x<strlen(fn);x++) {
901 if (!isdigit(fn[x])) {
909 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
912 fn = "/dev/zap/channel";
914 fd = open(fn, O_RDWR | O_NONBLOCK);
916 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
920 if (ioctl(fd, ZT_SPECIFY, &chan)) {
924 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
929 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
933 static void zt_close(int fd)
939 int zt_setlinear(int zfd, int linear)
942 res = ioctl(zfd, ZT_SETLINEAR, &linear);
949 int zt_setlaw(int zfd, int law)
952 res = ioctl(zfd, ZT_SETLAW, &law);
958 static int alloc_sub(struct zt_pvt *p, int x)
962 if (p->subs[x].zfd < 0) {
963 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
964 if (p->subs[x].zfd > -1) {
965 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
967 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
968 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
969 bi.numbufs = numbufs;
970 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
972 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
975 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
976 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
977 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
978 zt_close(p->subs[x].zfd);
983 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
986 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
989 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
993 static int unalloc_sub(struct zt_pvt *p, int x)
996 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
999 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
1000 if (p->subs[x].zfd > -1) {
1001 zt_close(p->subs[x].zfd);
1003 p->subs[x].zfd = -1;
1004 p->subs[x].linear = 0;
1005 p->subs[x].chan = 0;
1006 p->subs[x].owner = NULL;
1007 p->subs[x].inthreeway = 0;
1008 p->polarity = POLARITY_IDLE;
1009 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1013 static int zt_digit(struct ast_channel *ast, char digit)
1015 ZT_DIAL_OPERATION zo;
1020 ast_mutex_lock(&p->lock);
1021 index = zt_get_index(ast, p, 0);
1022 if ((index == SUB_REAL) && p->owner) {
1024 if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
1026 if (!pri_grab(p, p->pri)) {
1027 pri_information(p->pri->pri,p->call,digit);
1030 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1031 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
1032 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1033 res = strlen(p->dialdest);
1034 p->dialdest[res++] = digit;
1035 p->dialdest[res] = '\0';
1041 zo.op = ZT_DIAL_OP_APPEND;
1042 zo.dialstr[0] = 'T';
1043 zo.dialstr[1] = digit;
1045 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1046 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1051 ast_mutex_unlock(&p->lock);
1055 static char *events[] = {
1068 "Hook Transition Complete",
1073 "Polarity Reversal",
1081 { ZT_ALARM_RED, "Red Alarm" },
1082 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1083 { ZT_ALARM_BLUE, "Blue Alarm" },
1084 { ZT_ALARM_RECOVER, "Recovering" },
1085 { ZT_ALARM_LOOPBACK, "Loopback" },
1086 { ZT_ALARM_NOTOPEN, "Not Open" },
1087 { ZT_ALARM_NONE, "None" },
1090 static char *alarm2str(int alarm)
1093 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
1094 if (alarms[x].alarm & alarm)
1095 return alarms[x].name;
1097 return alarm ? "Unknown Alarm" : "No Alarm";
1100 static char *event2str(int event)
1102 static char buf[256];
1103 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1104 return events[event];
1105 sprintf(buf, "Event %d", event); /* safe */
1110 static char *dialplan2str(int dialplan)
1112 if (dialplan == -1) {
1113 return("Dynamically set dialplan in ISDN");
1115 return(pri_plan2str(dialplan));
1120 static int str2r2prot(char *swtype)
1122 if (!strcasecmp(swtype, "ar"))
1123 return MFCR2_PROT_ARGENTINA;
1125 if (!strcasecmp(swtype, "cn"))
1126 return MFCR2_PROT_CHINA;
1128 if (!strcasecmp(swtype, "kr"))
1129 return MFCR2_PROT_KOREA;
1135 static char *zap_sig2str(int sig)
1137 static char buf[256];
1140 return "E & M Immediate";
1142 return "E & M Wink";
1146 return "Feature Group D (DTMF)";
1148 return "Feature Group D (MF)";
1149 case SIG_FEATDMF_TA:
1150 return "Feature Groud D (MF) Tandem Access";
1152 return "Feature Group B (MF)";
1156 return "FGC/CAMA (Dialpulse)";
1157 case SIG_FGC_CAMAMF:
1158 return "FGC/CAMA (MF)";
1160 return "FXS Loopstart";
1162 return "FXS Groundstart";
1164 return "FXS Kewlstart";
1166 return "FXO Loopstart";
1168 return "FXO Groundstart";
1170 return "FXO Kewlstart";
1172 return "PRI Signalling";
1174 return "R2 Signalling";
1176 return "SF (Tone) Signalling Immediate";
1178 return "SF (Tone) Signalling Wink";
1180 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1181 case SIG_SF_FEATDMF:
1182 return "SF (Tone) Signalling with Feature Group D (MF)";
1184 return "SF (Tone) Signalling with Feature Group B (MF)";
1185 case SIG_GR303FXOKS:
1186 return "GR-303 Signalling with FXOKS";
1187 case SIG_GR303FXSKS:
1188 return "GR-303 Signalling with FXSKS";
1190 return "Pseudo Signalling";
1192 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1197 #define sig2str zap_sig2str
1199 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1201 /* If the conference already exists, and we're already in it
1202 don't bother doing anything */
1205 memset(&zi, 0, sizeof(zi));
1208 if (slavechannel > 0) {
1209 /* If we have only one slave, do a digital mon */
1210 zi.confmode = ZT_CONF_DIGITALMON;
1211 zi.confno = slavechannel;
1214 /* Real-side and pseudo-side both participate in conference */
1215 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1216 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1218 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1219 zi.confno = p->confno;
1221 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1225 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1226 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1229 if (slavechannel < 1) {
1230 p->confno = zi.confno;
1232 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1233 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1237 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1239 /* If they're listening to our channel, they're ours */
1240 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1242 /* If they're a talker on our (allocated) conference, they're ours */
1243 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1248 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1251 if (/* Can't delete if there's no zfd */
1253 /* Don't delete from the conference if it's not our conference */
1255 /* Don't delete if we don't think it's conferenced at all (implied) */
1257 memset(&zi, 0, sizeof(zi));
1261 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1262 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1265 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1266 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1270 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1274 struct zt_pvt *slave = NULL;
1275 /* Start out optimistic */
1277 /* Update conference state in a stateless fashion */
1279 /* Any three-way calling makes slave native mode *definitely* out
1281 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1284 /* If we don't have any 3-way calls, check to see if we have
1285 precisely one slave */
1286 if (useslavenative) {
1287 for (x=0;x<MAX_SLAVES;x++) {
1290 /* Whoops already have a slave! No
1291 slave native and stop right away */
1296 /* We have one slave so far */
1297 slave = p->slaves[x];
1302 /* If no slave, slave native definitely out */
1305 else if (slave->law != p->law) {
1311 return useslavenative;
1314 static int reset_conf(struct zt_pvt *p)
1317 memset(&zi, 0, sizeof(zi));
1319 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1320 if (p->subs[SUB_REAL].zfd > -1) {
1321 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1322 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1327 static int update_conf(struct zt_pvt *p)
1332 struct zt_pvt *slave = NULL;
1334 useslavenative = isslavenative(p, &slave);
1335 /* Start with the obvious, general stuff */
1337 /* Look for three way calls */
1338 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1339 conf_add(p, &p->subs[x], x, 0);
1342 conf_del(p, &p->subs[x], x);
1345 /* If we have a slave, add him to our conference now. or DAX
1346 if this is slave native */
1347 for (x=0;x<MAX_SLAVES;x++) {
1350 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1352 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1357 /* If we're supposed to be in there, do so now */
1358 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1360 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1362 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1366 /* If we have a master, add ourselves to his conference */
1368 if (isslavenative(p->master, NULL)) {
1369 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1371 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1375 /* Nobody is left (or should be left) in our conference.
1379 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1383 static void zt_enable_ec(struct zt_pvt *p)
1390 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1394 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1397 if (p->echocancel) {
1398 if (p->sig == SIG_PRI) {
1400 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1402 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1405 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1407 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1410 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1413 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1416 static void zt_train_ec(struct zt_pvt *p)
1420 if (p && p->echocancel && p->echotraining) {
1421 x = p->echotraining;
1422 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1424 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1426 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1429 ast_log(LOG_DEBUG, "No echo training requested\n");
1432 static void zt_disable_ec(struct zt_pvt *p)
1436 if (p->echocancel) {
1438 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1440 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1442 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1447 static void fill_txgain(struct zt_gains *g, float gain, int law)
1451 float linear_gain = pow(10.0, gain / 20.0);
1455 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1457 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1458 if (k > 32767) k = 32767;
1459 if (k < -32767) k = -32767;
1460 g->txgain[j] = AST_LIN2A(k);
1467 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1469 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1470 if (k > 32767) k = 32767;
1471 if (k < -32767) k = -32767;
1472 g->txgain[j] = AST_LIN2MU(k);
1481 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1485 float linear_gain = pow(10.0, gain / 20.0);
1489 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1491 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1492 if (k > 32767) k = 32767;
1493 if (k < -32767) k = -32767;
1494 g->rxgain[j] = AST_LIN2A(k);
1501 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1503 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1504 if (k > 32767) k = 32767;
1505 if (k < -32767) k = -32767;
1506 g->rxgain[j] = AST_LIN2MU(k);
1515 int set_actual_txgain(int fd, int chan, float gain, int law)
1520 memset(&g, 0, sizeof(g));
1522 res = ioctl(fd, ZT_GETGAINS, &g);
1524 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1528 fill_txgain(&g, gain, law);
1530 return ioctl(fd, ZT_SETGAINS, &g);
1533 int set_actual_rxgain(int fd, int chan, float gain, int law)
1538 memset(&g, 0, sizeof(g));
1540 res = ioctl(fd, ZT_GETGAINS, &g);
1542 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1546 fill_rxgain(&g, gain, law);
1548 return ioctl(fd, ZT_SETGAINS, &g);
1551 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1553 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1556 static int bump_gains(struct zt_pvt *p)
1560 /* Bump receive gain by 5.0db */
1561 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
1563 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1570 static int restore_gains(struct zt_pvt *p)
1574 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1576 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1583 static inline int zt_set_hook(int fd, int hs)
1587 res = ioctl(fd, ZT_HOOK, &x);
1590 if (errno == EINPROGRESS) return 0;
1591 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1596 static inline int zt_confmute(struct zt_pvt *p, int muted)
1600 if (p->sig == SIG_PRI) {
1602 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1604 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1606 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1608 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1612 static int save_conference(struct zt_pvt *p)
1614 struct zt_confinfo c;
1616 if (p->saveconf.confmode) {
1617 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1620 p->saveconf.chan = 0;
1621 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1623 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1624 p->saveconf.confmode = 0;
1629 c.confmode = ZT_CONF_NORMAL;
1630 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1632 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1636 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1640 static int restore_conference(struct zt_pvt *p)
1643 if (p->saveconf.confmode) {
1644 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1645 p->saveconf.confmode = 0;
1647 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1652 ast_log(LOG_DEBUG, "Restored conferencing\n");
1656 static int send_callerid(struct zt_pvt *p);
1658 int send_cwcidspill(struct zt_pvt *p)
1662 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1664 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1665 /* Make sure we account for the end */
1666 p->cidlen += READ_SIZE * 4;
1669 if (option_verbose > 2)
1670 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1674 static int has_voicemail(struct zt_pvt *p)
1677 return ast_app_has_voicemail(p->mailbox, NULL);
1680 static int send_callerid(struct zt_pvt *p)
1682 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1684 /* Take out of linear mode if necessary */
1685 if (p->subs[SUB_REAL].linear) {
1686 p->subs[SUB_REAL].linear = 0;
1687 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1689 while(p->cidpos < p->cidlen) {
1690 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1692 if (errno == EAGAIN)
1695 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1705 if (p->callwaitcas) {
1706 /* Wait for CID/CW to expire */
1707 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1709 restore_conference(p);
1713 static int zt_callwait(struct ast_channel *ast)
1715 struct zt_pvt *p = ast->tech_pvt;
1716 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1718 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1721 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1725 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1726 if (!p->callwaitrings && p->callwaitingcallerid) {
1727 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1729 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1731 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1733 p->cidlen = 2400 + READ_SIZE * 4;
1741 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1743 struct zt_pvt *p = ast->tech_pvt;
1749 char dest[256]; /* must be same length as p->dialdest */
1750 ast_mutex_lock(&p->lock);
1751 ast_copy_string(dest, rdest, sizeof(dest));
1752 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1753 if ((ast->_state == AST_STATE_BUSY)) {
1754 p->subs[SUB_REAL].needbusy = 1;
1755 ast_mutex_unlock(&p->lock);
1758 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1759 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1760 ast_mutex_unlock(&p->lock);
1764 if (p->radio) /* if a radio channel, up immediately */
1766 /* Special pseudo -- automatically up */
1767 ast_setstate(ast, AST_STATE_UP);
1768 ast_mutex_unlock(&p->lock);
1771 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1772 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1774 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1777 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1783 if (p->owner == ast) {
1784 /* Normal ring, on hook */
1786 /* Don't send audio while on hook, until the call is answered */
1788 if (p->use_callerid) {
1789 /* Generate the Caller-ID spill if desired */
1791 ast_log(LOG_WARNING, "cidspill already exists??\n");
1795 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1796 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1801 /* Choose proper cadence */
1802 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1803 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1804 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1805 p->cidrings = cidrings[p->distinctivering - 1];
1807 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1808 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1809 p->cidrings = p->sendcalleridafter;
1813 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1814 c = strchr(dest, '/');
1817 if (c && (strlen(c) < p->stripmsd)) {
1818 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1822 p->dop.op = ZT_DIAL_OP_REPLACE;
1823 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1824 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1826 p->dop.dialstr[0] = '\0';
1829 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1830 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1831 ast_mutex_unlock(&p->lock);
1836 /* Call waiting call */
1837 p->callwaitrings = 0;
1838 if (ast->cid.cid_num)
1839 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
1841 p->callwait_num[0] = '\0';
1842 if (ast->cid.cid_name)
1843 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
1845 p->callwait_name[0] = '\0';
1846 /* Call waiting tone instead */
1847 if (zt_callwait(ast)) {
1848 ast_mutex_unlock(&p->lock);
1851 /* Make ring-back */
1852 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1853 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1856 n = ast->cid.cid_name;
1857 l = ast->cid.cid_num;
1859 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1861 p->lastcid_num[0] = '\0';
1863 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1865 p->lastcid_name[0] = '\0';
1866 ast_setstate(ast, AST_STATE_RINGING);
1867 index = zt_get_index(ast, p, 0);
1869 p->subs[index].needringing = 1;
1882 case SIG_FGC_CAMAMF:
1887 case SIG_SF_FEATDMF:
1888 case SIG_FEATDMF_TA:
1890 c = strchr(dest, '/');
1895 if (strlen(c) < p->stripmsd) {
1896 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1897 ast_mutex_unlock(&p->lock);
1901 /* Start the trunk, if not GR-303 */
1905 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1907 if (errno != EINPROGRESS) {
1908 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1909 ast_mutex_unlock(&p->lock);
1916 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1917 p->dop.op = ZT_DIAL_OP_REPLACE;
1923 l = ast->cid.cid_num;
1925 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1927 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1930 l = ast->cid.cid_num;
1932 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1934 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1936 case SIG_FEATDMF_TA:
1938 const char *cic, *ozz;
1940 /* If you have to go through a Tandem Access point you need to use this */
1941 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1944 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1948 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1949 ast_mutex_unlock(&p->lock);
1952 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1953 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1958 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1961 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1963 case SIG_FGC_CAMAMF:
1965 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1969 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1971 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1975 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1976 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1977 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1978 p->echorest[sizeof(p->echorest) - 1] = '\0';
1980 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1984 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1986 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1987 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1988 ast_mutex_unlock(&p->lock);
1992 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1994 if (ast_strlen_zero(c))
1996 ast_setstate(ast, AST_STATE_DIALING);
1999 /* Special pseudo -- automatically up*/
2000 ast_setstate(ast, AST_STATE_UP);
2003 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2004 p->dialdest[0] = '\0';
2007 ast_log(LOG_DEBUG, "not yet implemented\n");
2008 ast_mutex_unlock(&p->lock);
2014 const char *useruser;
2017 int prilocaldialplan;
2021 c = strchr(dest, '/');
2026 if (!p->hidecallerid) {
2027 l = ast->cid.cid_num;
2028 n = ast->cid.cid_name;
2033 if (strlen(c) < p->stripmsd) {
2034 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2035 ast_mutex_unlock(&p->lock);
2038 if (p->sig != SIG_FXSKS) {
2039 p->dop.op = ZT_DIAL_OP_REPLACE;
2040 s = strchr(c + p->stripmsd, 'w');
2043 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2045 p->dop.dialstr[0] = '\0';
2048 p->dop.dialstr[0] = '\0';
2051 if (pri_grab(p, p->pri)) {
2052 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2053 ast_mutex_unlock(&p->lock);
2056 if (!(p->call = pri_new_call(p->pri->pri))) {
2057 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2059 ast_mutex_unlock(&p->lock);
2062 if (!(sr = pri_sr_new())) {
2063 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2065 ast_mutex_unlock(&p->lock);
2067 if (p->bearer || (p->sig == SIG_FXSKS)) {
2069 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);
2070 p->bearer->call = p->call;
2072 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
2073 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2075 p->digital = IS_DIGITAL(ast->transfercapability);
2076 /* Add support for exclusive override */
2077 if (p->priexclusive)
2080 /* otherwise, traditional behavior */
2081 if (p->pri->nodetype == PRI_NETWORK)
2087 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2088 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2090 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2091 if (p->pri->facilityenable)
2092 pri_facility_enable(p->pri->pri);
2094 if (option_verbose > 2)
2095 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2097 pridialplan = p->pri->dialplan - 1;
2098 if (pridialplan == -2) { /* compute dynamically */
2099 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2100 dp_strip = strlen(p->pri->internationalprefix);
2101 pridialplan = PRI_INTERNATIONAL_ISDN;
2102 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2103 dp_strip = strlen(p->pri->nationalprefix);
2104 pridialplan = PRI_NATIONAL_ISDN;
2106 pridialplan = PRI_LOCAL_ISDN;
2109 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2112 prilocaldialplan = p->pri->localdialplan - 1;
2113 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
2114 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2115 ldp_strip = strlen(p->pri->internationalprefix);
2116 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2117 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2118 ldp_strip = strlen(p->pri->nationalprefix);
2119 prilocaldialplan = PRI_NATIONAL_ISDN;
2121 prilocaldialplan = PRI_LOCAL_ISDN;
2124 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2125 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
2126 PRES_NUMBER_NOT_AVAILABLE);
2127 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
2128 /* User-user info */
2129 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2132 pri_sr_set_useruser(sr, useruser);
2134 if (pri_setup(p->pri->pri, p->call, sr)) {
2135 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2136 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2138 ast_mutex_unlock(&p->lock);
2143 ast_setstate(ast, AST_STATE_DIALING);
2147 ast_mutex_unlock(&p->lock);
2151 static void destroy_zt_pvt(struct zt_pvt **pvt)
2153 struct zt_pvt *p = *pvt;
2154 /* Remove channel from the list */
2156 p->prev->next = p->next;
2158 p->next->prev = p->prev;
2159 ast_mutex_destroy(&p->lock);
2164 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2174 for (i = 0; i < 3; i++) {
2175 if (cur->subs[i].owner) {
2181 prev->next = cur->next;
2183 prev->next->prev = prev;
2189 iflist->prev = NULL;
2193 if (cur->subs[SUB_REAL].zfd > -1) {
2194 zt_close(cur->subs[SUB_REAL].zfd);
2196 destroy_zt_pvt(&cur);
2200 prev->next = cur->next;
2202 prev->next->prev = prev;
2208 iflist->prev = NULL;
2212 if (cur->subs[SUB_REAL].zfd > -1) {
2213 zt_close(cur->subs[SUB_REAL].zfd);
2215 destroy_zt_pvt(&cur);
2221 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2223 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2225 static char *zap_send_keypad_facility_descrip =
2226 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2227 " IE over the current channel.\n";
2229 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2231 /* Data will be our digit string */
2233 char *digits = (char *) data;
2235 if (ast_strlen_zero(digits)) {
2236 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
2240 p = (struct zt_pvt *)chan->tech_pvt;
2243 ast_log(LOG_DEBUG, "Unable to find technology private\n");
2247 ast_mutex_lock(&p->lock);
2249 if (!p->pri || !p->call) {
2250 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2251 ast_mutex_unlock(&p->lock);
2255 if (!pri_grab(p, p->pri)) {
2256 pri_keypad_facility(p->pri->pri, p->call, digits);
2259 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
2260 ast_mutex_unlock(&p->lock);
2264 ast_mutex_unlock(&p->lock);
2269 int pri_is_up(struct zt_pri *pri)
2272 for (x=0;x<NUM_DCHANS;x++) {
2273 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2279 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2281 bearer->owner = &inuse;
2282 bearer->realcall = crv;
2283 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2284 if (crv->subs[SUB_REAL].owner)
2285 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2286 crv->bearer = bearer;
2287 crv->call = bearer->call;
2292 static char *pri_order(int level)
2302 return "Quaternary";
2308 /* Returns fd of the active dchan */
2309 int pri_active_dchan_fd(struct zt_pri *pri)
2313 for (x = 0; x < NUM_DCHANS; x++) {
2314 if ((pri->dchans[x] == pri->pri))
2321 int pri_find_dchan(struct zt_pri *pri)
2328 for(x=0;x<NUM_DCHANS;x++) {
2329 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2331 if (pri->dchans[x] == old) {
2337 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2338 pri->dchannels[newslot]);
2340 if (old && (oldslot != newslot))
2341 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2342 pri->dchannels[oldslot], pri->dchannels[newslot]);
2343 pri->pri = pri->dchans[newslot];
2348 static int zt_hangup(struct ast_channel *ast)
2352 /*static int restore_gains(struct zt_pvt *p);*/
2353 struct zt_pvt *p = ast->tech_pvt;
2354 struct zt_pvt *tmp = NULL;
2355 struct zt_pvt *prev = NULL;
2359 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2360 if (!ast->tech_pvt) {
2361 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2365 ast_mutex_lock(&p->lock);
2367 index = zt_get_index(ast, p, 1);
2369 if (p->sig == SIG_PRI) {
2371 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2377 if (p->origcid_num) {
2378 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2379 free(p->origcid_num);
2380 p->origcid_num = NULL;
2382 if (p->origcid_name) {
2383 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2384 free(p->origcid_name);
2385 p->origcid_name = NULL;
2388 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2392 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2393 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2397 /* Real channel, do some fixup */
2398 p->subs[index].owner = NULL;
2399 p->subs[index].needanswer = 0;
2400 p->subs[index].needflash = 0;
2401 p->subs[index].needringing = 0;
2402 p->subs[index].needbusy = 0;
2403 p->subs[index].needcongestion = 0;
2404 p->subs[index].linear = 0;
2405 p->subs[index].needcallerid = 0;
2406 p->polarity = POLARITY_IDLE;
2407 zt_setlinear(p->subs[index].zfd, 0);
2408 if (index == SUB_REAL) {
2409 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2410 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2411 if (p->subs[SUB_CALLWAIT].inthreeway) {
2412 /* We had flipped over to answer a callwait and now it's gone */
2413 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2414 /* Move to the call-wait, but un-own us until they flip back. */
2415 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2416 unalloc_sub(p, SUB_CALLWAIT);
2419 /* The three way hung up, but we still have a call wait */
2420 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2421 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2422 unalloc_sub(p, SUB_THREEWAY);
2423 if (p->subs[SUB_REAL].inthreeway) {
2424 /* This was part of a three way call. Immediately make way for
2426 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2427 p->owner = p->subs[SUB_REAL].owner;
2429 /* This call hasn't been completed yet... Set owner to NULL */
2430 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2433 p->subs[SUB_REAL].inthreeway = 0;
2435 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2436 /* Move to the call-wait and switch back to them. */
2437 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2438 unalloc_sub(p, SUB_CALLWAIT);
2439 p->owner = p->subs[SUB_REAL].owner;
2440 if (p->owner->_state != AST_STATE_UP)
2441 p->subs[SUB_REAL].needanswer = 1;
2442 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2443 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2444 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2445 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2446 unalloc_sub(p, SUB_THREEWAY);
2447 if (p->subs[SUB_REAL].inthreeway) {
2448 /* This was part of a three way call. Immediately make way for
2450 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2451 p->owner = p->subs[SUB_REAL].owner;
2453 /* This call hasn't been completed yet... Set owner to NULL */
2454 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2457 p->subs[SUB_REAL].inthreeway = 0;
2459 } else if (index == SUB_CALLWAIT) {
2460 /* Ditch the holding callwait call, and immediately make it availabe */
2461 if (p->subs[SUB_CALLWAIT].inthreeway) {
2462 /* This is actually part of a three way, placed on hold. Place the third part
2463 on music on hold now */
2464 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2465 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2466 p->subs[SUB_THREEWAY].inthreeway = 0;
2467 /* Make it the call wait now */
2468 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2469 unalloc_sub(p, SUB_THREEWAY);
2471 unalloc_sub(p, SUB_CALLWAIT);
2472 } else if (index == SUB_THREEWAY) {
2473 if (p->subs[SUB_CALLWAIT].inthreeway) {
2474 /* The other party of the three way call is currently in a call-wait state.
2475 Start music on hold for them, and take the main guy out of the third call */
2476 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2477 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2478 p->subs[SUB_CALLWAIT].inthreeway = 0;
2480 p->subs[SUB_REAL].inthreeway = 0;
2481 /* If this was part of a three way call index, let us make
2482 another three way call */
2483 unalloc_sub(p, SUB_THREEWAY);
2485 /* This wasn't any sort of call, but how are we an index? */
2486 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2491 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2494 p->distinctivering = 0;
2495 p->confirmanswer = 0;
2501 p->onhooktime = time(NULL);
2509 ast_dsp_free(p->dsp);
2513 law = ZT_LAW_DEFAULT;
2514 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2516 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2517 /* Perform low level hangup if no owner left */
2520 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2521 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2522 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2523 if (!pri_grab(p, p->pri)) {
2524 if (p->alreadyhungup) {
2525 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2526 pri_call_set_useruser(p->call, useruser);
2527 pri_hangup(p->pri->pri, p->call, -1);
2530 p->bearer->call = NULL;
2532 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2533 int icause = ast->hangupcause ? ast->hangupcause : -1;
2534 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2535 pri_call_set_useruser(p->call, useruser);
2536 p->alreadyhungup = 1;
2538 p->bearer->alreadyhungup = 1;
2541 icause = atoi(cause);
2543 pri_hangup(p->pri->pri, p->call, icause);
2546 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2549 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2554 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2561 if (p->sig == SIG_R2) {
2563 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2571 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2572 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2574 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2580 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2583 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2585 /* If they're off hook, try playing congestion */
2586 if ((par.rxisoffhook) && (!p->radio))
2587 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2589 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2595 /* Make sure we're not made available for at least two seconds assuming
2596 we were actually used for an inbound or outbound call. */
2597 if (ast->_state != AST_STATE_RESERVED) {
2598 time(&p->guardtime);
2603 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2610 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2611 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2615 p->callwaiting = p->permcallwaiting;
2616 p->hidecallerid = p->permhidecallerid;
2621 /* Restore data mode */
2622 if (p->sig == SIG_PRI) {
2624 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2628 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2629 /* Free up the bearer channel as well, and
2630 don't use its file descriptor anymore */
2631 update_conf(p->bearer);
2632 reset_conf(p->bearer);
2633 p->bearer->owner = NULL;
2634 p->bearer->realcall = NULL;
2636 p->subs[SUB_REAL].zfd = -1;
2644 p->callwaitingrepeat = 0;
2646 ast->tech_pvt = NULL;
2647 ast_mutex_unlock(&p->lock);
2648 ast_mutex_lock(&usecnt_lock);
2651 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2652 ast_mutex_unlock(&usecnt_lock);
2653 ast_update_use_count();
2654 if (option_verbose > 2)
2655 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2657 ast_mutex_lock(&iflock);
2663 destroy_channel(prev, tmp, 0);
2671 ast_mutex_unlock(&iflock);
2675 static int zt_answer(struct ast_channel *ast)
2677 struct zt_pvt *p = ast->tech_pvt;
2680 int oldstate = ast->_state;
2681 ast_setstate(ast, AST_STATE_UP);
2682 ast_mutex_lock(&p->lock);
2683 index = zt_get_index(ast, p, 0);
2686 /* nothing to do if a radio channel */
2688 ast_mutex_unlock(&p->lock);
2702 case SIG_FEATDMF_TA:
2705 case SIG_FGC_CAMAMF:
2710 case SIG_SF_FEATDMF:
2715 /* Pick up the line */
2716 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2717 if(p->hanguponpolarityswitch) {
2718 gettimeofday(&p->polaritydelaytv, NULL);
2720 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2721 tone_zone_play_tone(p->subs[index].zfd, -1);
2723 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2724 if (oldstate == AST_STATE_RINGING) {
2725 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2726 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2727 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2728 p->owner = p->subs[SUB_REAL].owner;
2731 if (p->sig & __ZT_SIG_FXS) {
2738 /* Send a pri acknowledge */
2739 if (!pri_grab(p, p->pri)) {
2741 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2744 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2751 res = mfcr2_AnswerCall(p->r2, NULL);
2753 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2757 ast_mutex_unlock(&p->lock);
2760 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2763 ast_mutex_unlock(&p->lock);
2767 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2773 struct zt_pvt *p = chan->tech_pvt;
2775 /* all supported options require data */
2776 if (!data || (datalen < 1)) {
2782 case AST_OPTION_TXGAIN:
2783 scp = (signed char *) data;
2784 index = zt_get_index(chan, p, 0);
2786 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
2789 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
2790 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
2791 case AST_OPTION_RXGAIN:
2792 scp = (signed char *) data;
2793 index = zt_get_index(chan, p, 0);
2795 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
2798 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
2799 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
2800 case AST_OPTION_TONE_VERIFY:
2806 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2807 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2810 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2811 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2814 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2815 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2819 case AST_OPTION_TDD:
2820 /* turn on or off TDD */
2823 if (!*cp) { /* turn it off */
2824 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2825 if (p->tdd) tdd_free(p->tdd);
2829 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
2830 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
2832 /* otherwise, turn it on */
2833 if (!p->didtdd) { /* if havent done it yet */
2834 unsigned char mybuf[41000],*buf;
2835 int size,res,fd,len;
2836 struct pollfd fds[1];
2839 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
2840 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
2842 index = zt_get_index(chan, p, 0);
2844 ast_log(LOG_WARNING, "No index in TDD?\n");
2847 fd = p->subs[index].zfd;
2849 if (ast_check_hangup(chan)) return -1;
2851 if (size > READ_SIZE)
2854 fds[0].events = POLLPRI | POLLOUT;
2856 res = poll(fds, 1, -1);
2858 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2861 /* if got exception */
2862 if (fds[0].revents & POLLPRI) return -1;
2863 if (!(fds[0].revents & POLLOUT)) {
2864 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2867 res = write(fd, buf, size);
2869 if (res == -1) return -1;
2870 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2876 p->didtdd = 1; /* set to have done it now */
2878 if (*cp == 2) { /* Mate mode */
2879 if (p->tdd) tdd_free(p->tdd);
2884 if (!p->tdd) { /* if we dont have one yet */
2885 p->tdd = tdd_new(); /* allocate one */
2888 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2892 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
2893 *cp ? "ON" : "OFF", (int) *cp, chan->name);
2894 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
2896 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2899 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
2903 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
2906 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2907 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2915 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2917 /* Unlink a specific slave or all slaves/masters from a given master */
2923 ast_mutex_lock(&master->lock);
2925 while(ast_mutex_trylock(&slave->lock)) {
2926 ast_mutex_unlock(&master->lock);
2928 ast_mutex_lock(&master->lock);
2933 for (x=0;x<MAX_SLAVES;x++) {
2934 if (master->slaves[x]) {
2935 if (!slave || (master->slaves[x] == slave)) {
2936 /* Take slave out of the conference */
2937 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2938 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2939 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2940 master->slaves[x]->master = NULL;
2941 master->slaves[x] = NULL;
2946 master->inconference = 0;
2949 if (master->master) {
2950 /* Take master out of the conference */
2951 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2952 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2954 for (x=0;x<MAX_SLAVES;x++) {
2955 if (master->master->slaves[x] == master)
2956 master->master->slaves[x] = NULL;
2957 else if (master->master->slaves[x])
2961 master->master->inconference = 0;
2963 master->master = NULL;
2965 update_conf(master);
2968 ast_mutex_unlock(&slave->lock);
2969 ast_mutex_unlock(&master->lock);
2973 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2975 if (!slave || !master) {
2976 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2979 for (x=0;x<MAX_SLAVES;x++) {
2980 if (!master->slaves[x]) {
2981 master->slaves[x] = slave;
2985 if (x >= MAX_SLAVES) {
2986 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2987 master->slaves[MAX_SLAVES - 1] = slave;
2990 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2991 slave->master = master;
2993 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2996 static void disable_dtmf_detect(struct zt_pvt *p)
2998 #ifdef ZT_TONEDETECT
3004 #ifdef ZT_TONEDETECT
3006 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3008 if (!p->hardwaredtmf && p->dsp) {
3009 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
3010 ast_dsp_set_features(p->dsp, p->dsp_features);
3014 static void enable_dtmf_detect(struct zt_pvt *p)
3016 #ifdef ZT_TONEDETECT
3020 if (p->channel == CHAN_PSEUDO)
3025 #ifdef ZT_TONEDETECT
3026 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
3027 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3029 if (!p->hardwaredtmf && p->dsp) {
3030 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
3031 ast_dsp_set_features(p->dsp, p->dsp_features);
3035 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)
3037 struct ast_channel *who;
3038 struct zt_pvt *p0, *p1, *op0, *op1;