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
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"
105 #error "Your zaptel is too old. please cvs update"
108 #ifndef ZT_TONEDETECT
109 /* Work around older code with no tone detect */
110 #define ZT_EVENT_DTMFDOWN 0
111 #define ZT_EVENT_DTMFUP 0
115 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
116 * the user hangs up to reset the state machine so ring works properly.
117 * This is used to be able to support kewlstart by putting the zhone in
118 * groundstart mode since their forward disconnect supervision is entirely
119 * broken even though their documentation says it isn't and their support
120 * is entirely unwilling to provide any assistance with their channel banks
121 * even though their web site says they support their products for life.
123 /* #define ZHONE_HACK */
126 * Define if you want to check the hook state for an FXO (FXS signalled) interface
127 * before dialing on it. Certain FXO interfaces always think they're out of
128 * service with this method however.
130 /* #define ZAP_CHECK_HOOKSTATE */
132 /*! \brief Typically, how many rings before we should send Caller*ID */
133 #define DEFAULT_CIDRINGS 1
135 #define CHANNEL_PSEUDO -12
137 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
139 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
140 #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))
142 static const char desc[] = "Zapata Telephony"
151 static const char tdesc[] = "Zapata Telephony Driver"
160 static const char type[] = "Zap";
161 static const char config[] = "zapata.conf";
163 #define SIG_EM ZT_SIG_EM
164 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
165 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
166 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
167 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
168 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
169 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
170 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
171 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
172 #define SIG_FXSLS ZT_SIG_FXSLS
173 #define SIG_FXSGS ZT_SIG_FXSGS
174 #define SIG_FXSKS ZT_SIG_FXSKS
175 #define SIG_FXOLS ZT_SIG_FXOLS
176 #define SIG_FXOGS ZT_SIG_FXOGS
177 #define SIG_FXOKS ZT_SIG_FXOKS
178 #define SIG_PRI ZT_SIG_CLEAR
179 #define SIG_R2 ZT_SIG_CAS
180 #define SIG_SF ZT_SIG_SF
181 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
182 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
183 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
184 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
185 #define SIG_EM_E1 ZT_SIG_EM_E1
186 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
187 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
190 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
191 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
193 #define CHAN_PSEUDO -2
195 #define DCHAN_PROVISIONED (1 << 0)
196 #define DCHAN_NOTINALARM (1 << 1)
197 #define DCHAN_UP (1 << 2)
199 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
201 static char context[AST_MAX_CONTEXT] = "default";
202 static char cid_num[256] = "";
203 static char cid_name[256] = "";
204 static char defaultcic[64] = "";
205 static char defaultozz[64] = "";
207 static char language[MAX_LANGUAGE] = "";
208 static char musicclass[MAX_MUSICCLASS] = "";
209 static char progzone[10]= "";
211 static int usedistinctiveringdetection = 0;
213 static int transfertobusy = 1;
215 static int use_callerid = 1;
216 static int cid_signalling = CID_SIG_BELL;
217 static int cid_start = CID_START_RING;
218 static int zaptrcallerid = 0;
219 static int cur_signalling = -1;
221 static ast_group_t cur_group = 0;
222 static ast_group_t cur_callergroup = 0;
223 static ast_group_t cur_pickupgroup = 0;
224 static int relaxdtmf = 0;
226 static int immediate = 0;
228 static int stripmsd = 0;
230 static int callwaiting = 0;
232 static int callwaitingcallerid = 0;
234 static int hidecallerid = 0;
236 static int restrictcid = 0;
238 static int use_callingpres = 0;
240 static int callreturn = 0;
242 static int threewaycalling = 0;
244 static int transfer = 0;
246 static int canpark = 0;
248 static int cancallforward = 0;
250 static float rxgain = 0.0;
252 static float txgain = 0.0;
254 static int tonezone = -1;
256 static int echocancel;
258 static int echotraining;
262 static int echocanbridged = 0;
264 static int busydetect = 0;
266 static int busycount = 3;
267 static int busy_tonelength = 0;
268 static int busy_quietlength = 0;
270 static int callprogress = 0;
272 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
274 static char mailbox[AST_MAX_EXTENSION];
276 static int amaflags = 0;
280 static int numbufs = 4;
282 static int cur_prewink = -1;
283 static int cur_preflash = -1;
284 static int cur_wink = -1;
285 static int cur_flash = -1;
286 static int cur_start = -1;
287 static int cur_rxwink = -1;
288 static int cur_rxflash = -1;
289 static int cur_debounce = -1;
290 static int cur_priexclusive = 0;
292 static int priindication_oob = 0;
295 static int minunused = 2;
296 static int minidle = 0;
297 static char idleext[AST_MAX_EXTENSION];
298 static char idledial[AST_MAX_EXTENSION];
299 static int overlapdial = 0;
300 static int facilityenable = 0;
301 static char internationalprefix[10] = "";
302 static char nationalprefix[10] = "";
303 static char localprefix[20] = "";
304 static char privateprefix[20] = "";
305 static char unknownprefix[20] = "";
306 static long resetinterval = 3600; /*!< How often (in seconds) to reset unused channels. Default 1 hour. */
307 static struct ast_channel inuse = { "GR-303InUse" };
308 #ifdef PRI_GETSET_TIMERS
309 static int pritimers[PRI_MAX_TIMERS];
311 static int pridebugfd = -1;
312 static char pridebugfilename[1024]="";
315 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
316 static int firstdigittimeout = 16000;
318 /*! \brief How long to wait for following digits (FXO logic) */
319 static int gendigittimeout = 8000;
321 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
322 static int matchdigittimeout = 3000;
324 static int usecnt =0;
325 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
327 /*! \brief Protect the interface list (of zt_pvt's) */
328 AST_MUTEX_DEFINE_STATIC(iflock);
331 static int ifcount = 0;
334 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
337 /*! \brief Whether we answer on a Polarity Switch event */
338 static int answeronpolarityswitch = 0;
340 /*! \brief Whether we hang up on a Polarity Switch event */
341 static int hanguponpolarityswitch = 0;
343 /*! \brief How long (ms) to ignore Polarity Switch events after we answer a call */
344 static int polarityonanswerdelay = 600;
346 /*! \brief When to send the CallerID signals (rings) */
347 static int sendcalleridafter = DEFAULT_CIDRINGS;
349 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
350 when it's doing something critical. */
351 AST_MUTEX_DEFINE_STATIC(monlock);
353 /*! \brief This is the thread for the monitor which checks for input on the channels
354 which are not currently in use. */
355 static pthread_t monitor_thread = AST_PTHREADT_NULL;
357 static int restart_monitor(void);
359 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);
361 static int zt_sendtext(struct ast_channel *c, const char *text);
363 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
364 static inline int zt_get_event(int fd)
367 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
371 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
372 static inline int zt_wait_event(int fd)
375 i = ZT_IOMUX_SIGEVENT;
376 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
377 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
381 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
382 #define READ_SIZE 160
384 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
385 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
387 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
388 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
389 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
390 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
391 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
397 static int r2prot = -1;
400 static int ringt_base = DEFAULT_RINGT;
404 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
405 #define PRI_CHANNEL(p) ((p) & 0xff)
406 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
407 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
410 pthread_t master; /*!< Thread of master */
411 ast_mutex_t lock; /*!< Mutex */
412 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
413 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
414 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
415 int minunused; /*!< Min # of channels to keep empty */
416 int minidle; /*!< Min # of "idling" calls to keep active */
417 int nodetype; /*!< Node type */
418 int switchtype; /*!< Type of switch to emulate */
419 int nsf; /*!< Network-Specific Facilities */
420 int dialplan; /*!< Dialing plan */
421 int localdialplan; /*!< Local dialing plan */
422 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
423 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
424 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
425 char privateprefix[20]; /*!< for private dialplans */
426 char unknownprefix[20]; /*!< for unknown dialplans */
427 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
428 int trunkgroup; /*!< What our trunkgroup is */
429 int mastertrunkgroup; /*!< What trunk group is our master */
430 int prilogicalspan; /*!< Logical span number within trunk group */
431 int numchans; /*!< Num of channels we represent */
432 int overlapdial; /*!< In overlap dialing mode */
433 int facilityenable; /*!< Enable facility IEs */
434 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
435 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
436 struct pri *pri; /*!< Currently active D-channel */
438 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
443 time_t lastreset; /*!< time when unused channels were last reset */
444 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
445 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
446 struct zt_pvt *crvs; /*!< Member CRV structs */
447 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
451 static struct zt_pri pris[NUM_SPANS];
453 static int pritype = PRI_CPE;
456 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
458 #define DEFAULT_PRI_DEBUG 0
461 static inline void pri_rel(struct zt_pri *pri)
463 ast_mutex_unlock(&pri->lock);
466 static int switchtype = PRI_SWITCH_NI2;
467 static int nsf = PRI_NSF_NONE;
468 static int dialplan = PRI_NATIONAL_ISDN + 1;
469 static int localdialplan = PRI_NATIONAL_ISDN + 1;
472 /*! Shut up the compiler */
476 #define SUB_REAL 0 /*!< Active call */
477 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
478 #define SUB_THREEWAY 2 /*!< Three-way call */
480 /* Polarity states */
481 #define POLARITY_IDLE 0
482 #define POLARITY_REV 1
485 static struct zt_distRings drings;
487 struct distRingData {
490 struct ringContextData {
491 char contextData[AST_MAX_CONTEXT];
493 struct zt_distRings {
494 struct distRingData ringnum[3];
495 struct ringContextData ringContext[3];
498 static char *subnames[] = {
504 struct zt_subchannel {
506 struct ast_channel *owner;
508 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
509 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
510 unsigned int needringing:1;
511 unsigned int needbusy:1;
512 unsigned int needcongestion:1;
513 unsigned int needcallerid:1;
514 unsigned int needanswer:1;
515 unsigned int needflash:1;
516 unsigned int linear:1;
517 unsigned int inthreeway:1;
521 #define CONF_USER_REAL (1 << 0)
522 #define CONF_USER_THIRDCALL (1 << 1)
526 static struct zt_pvt {
528 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
529 /*!< Up to three channels can be associated with this call */
531 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
532 struct zt_subchannel subs[3]; /*!< Sub-channels */
533 struct zt_confinfo saveconf; /*!< Saved conference info */
535 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
536 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
537 int inconference; /*!< If our real should be in the conference */
539 int sig; /*!< Signalling style */
540 int radio; /*!< radio type */
543 int tonezone; /*!< tone zone for this chan, or -1 for default */
544 struct zt_pvt *next; /*!< Next channel in list */
545 struct zt_pvt *prev; /*!< Prev channel in list */
549 unsigned int answeronpolarityswitch:1;
550 unsigned int busydetect:1;
551 unsigned int callreturn:1;
552 unsigned int callwaiting:1;
553 unsigned int callwaitingcallerid:1;
554 unsigned int cancallforward:1;
555 unsigned int canpark:1;
556 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
557 unsigned int destroy:1;
558 unsigned int didtdd:1; /*!< flag to say its done it once */
559 unsigned int dialednone:1;
560 unsigned int dialing:1;
561 unsigned int digital:1;
563 unsigned int echobreak:1;
564 unsigned int echocanbridged:1;
565 unsigned int echocanon:1;
566 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
567 unsigned int firstradio:1;
568 unsigned int hanguponpolarityswitch:1;
569 unsigned int hardwaredtmf:1;
570 unsigned int hidecallerid;
571 unsigned int ignoredtmf:1;
572 unsigned int immediate:1; /*!< Answer before getting digits? */
573 unsigned int inalarm:1;
574 unsigned int mate:1; /*!< flag to say its in MATE mode */
575 unsigned int outgoing:1;
576 unsigned int overlapdial:1;
577 unsigned int permcallwaiting:1;
578 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
579 unsigned int priindication_oob:1;
580 unsigned int priexclusive:1;
581 unsigned int pulse:1;
582 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
583 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
584 unsigned int threewaycalling:1;
585 unsigned int transfer:1;
586 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
587 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
588 unsigned int usedistinctiveringdetection:1;
589 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
590 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
591 #if defined(ZAPATA_PRI)
592 unsigned int alerting:1;
593 unsigned int alreadyhungup:1;
594 unsigned int isidlecall:1;
595 unsigned int proceeding:1;
596 unsigned int progress:1;
597 unsigned int resetting:1;
598 unsigned int setup_ack:1;
600 #if defined(ZAPATA_R2)
601 unsigned int hasr2call:1;
602 unsigned int r2blocked:1;
603 unsigned int sigchecked:1;
606 struct zt_distRings drings;
608 char context[AST_MAX_CONTEXT];
609 char defcontext[AST_MAX_CONTEXT];
610 char exten[AST_MAX_EXTENSION];
611 char language[MAX_LANGUAGE];
612 char musicclass[MAX_MUSICCLASS];
614 char cid_ani[AST_MAX_EXTENSION];
616 char cid_num[AST_MAX_EXTENSION];
617 int cid_ton; /*!< Type Of Number (TON) */
618 char cid_name[AST_MAX_EXTENSION];
619 char lastcid_num[AST_MAX_EXTENSION];
620 char lastcid_name[AST_MAX_EXTENSION];
621 char *origcid_num; /*!< malloced original callerid */
622 char *origcid_name; /*!< malloced original callerid */
623 char callwait_num[AST_MAX_EXTENSION];
624 char callwait_name[AST_MAX_EXTENSION];
625 char rdnis[AST_MAX_EXTENSION];
626 char dnid[AST_MAX_EXTENSION];
629 int confno; /*!< Our conference */
630 int confusers; /*!< Who is using our conference */
631 int propconfno; /*!< Propagated conference number */
632 ast_group_t callgroup;
633 ast_group_t pickupgroup;
634 int channel; /*!< Channel Number or CRV */
635 int span; /*!< Span number */
636 time_t guardtime; /*!< Must wait this much time before using for new call */
637 int cid_signalling; /*!< CID signalling type bell202 or v23 */
638 int cid_start; /*!< CID start indicator, polarity or ring */
639 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
640 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
641 int cidcwexpire; /*!< When to expire our muting for CID/CW */
642 unsigned char *cidspill;
655 int busy_quietlength;
657 struct timeval flashtime; /*!< Last flash-hook time */
659 int cref; /*!< Call reference number */
660 ZT_DIAL_OPERATION dop;
661 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
663 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
664 int amaflags; /*!< AMA Flags */
665 struct tdd_state *tdd; /*!< TDD flag */
666 char call_forward[AST_MAX_EXTENSION];
667 char mailbox[AST_MAX_EXTENSION];
671 int distinctivering; /*!< Which distinctivering to use */
672 int cidrings; /*!< Which ring to deliver CID on */
673 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
675 int polarityonanswerdelay;
676 struct timeval polaritydelaytv;
677 int sendcalleridafter;
680 struct zt_pvt *bearer;
681 struct zt_pvt *realcall;
693 } *iflist = NULL, *ifend = NULL;
695 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
696 static int zt_digit(struct ast_channel *ast, char digit);
697 static int zt_sendtext(struct ast_channel *c, const char *text);
698 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
699 static int zt_hangup(struct ast_channel *ast);
700 static int zt_answer(struct ast_channel *ast);
701 struct ast_frame *zt_read(struct ast_channel *ast);
702 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
703 struct ast_frame *zt_exception(struct ast_channel *ast);
704 static int zt_indicate(struct ast_channel *chan, int condition);
705 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
706 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
708 static const struct ast_channel_tech zap_tech = {
710 .description = tdesc,
711 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
712 .requester = zt_request,
713 .send_digit = zt_digit,
714 .send_text = zt_sendtext,
721 .exception = zt_exception,
722 .indicate = zt_indicate,
724 .setoption = zt_setoption,
728 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
730 #define GET_CHANNEL(p) ((p)->channel)
733 struct zt_pvt *round_robin[32];
736 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
739 /* Grab the lock first */
741 res = ast_mutex_trylock(&pri->lock);
743 ast_mutex_unlock(&pvt->lock);
744 /* Release the lock and try again */
746 ast_mutex_lock(&pvt->lock);
749 /* Then break the poll */
750 pthread_kill(pri->master, SIGURG);
755 #define NUM_CADENCE_MAX 25
756 static int num_cadence = 4;
757 static int user_has_defined_cadences = 0;
759 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
760 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
761 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
762 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
763 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
766 int receivedRingT; /*!< Used to find out what ringtone we are on */
768 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
769 * is 1, the second pause is 2 and so on.
772 static int cidrings[NUM_CADENCE_MAX] = {
773 2, /*!< Right after first long ring */
774 4, /*!< Right after long part */
775 3, /*!< After third chirp */
776 2, /*!< Second spell */
779 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
780 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
782 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
783 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
785 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
788 if (p->subs[0].owner == ast)
790 else if (p->subs[1].owner == ast)
792 else if (p->subs[2].owner == ast)
797 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
803 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
805 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
808 struct ast_frame null = { AST_FRAME_NULL, };
811 ast_mutex_unlock(&pri->lock);
814 if (p->subs[a].owner) {
815 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
816 ast_mutex_unlock(&p->lock);
818 ast_mutex_lock(&p->lock);
820 ast_queue_frame(p->subs[a].owner, &null);
821 ast_mutex_unlock(&p->subs[a].owner->lock);
829 ast_mutex_lock(&pri->lock);
834 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
836 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
839 /* We must unlock the PRI to avoid the possibility of a deadlock */
842 ast_mutex_unlock(&pri->lock);
846 if (ast_mutex_trylock(&p->owner->lock)) {
847 ast_mutex_unlock(&p->lock);
849 ast_mutex_lock(&p->lock);
851 ast_queue_frame(p->owner, f);
852 ast_mutex_unlock(&p->owner->lock);
860 ast_mutex_lock(&pri->lock);
864 static int restore_gains(struct zt_pvt *p);
866 static void swap_subs(struct zt_pvt *p, int a, int b)
870 struct ast_channel *towner;
872 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
874 tchan = p->subs[a].chan;
875 towner = p->subs[a].owner;
876 tinthreeway = p->subs[a].inthreeway;
878 p->subs[a].chan = p->subs[b].chan;
879 p->subs[a].owner = p->subs[b].owner;
880 p->subs[a].inthreeway = p->subs[b].inthreeway;
882 p->subs[b].chan = tchan;
883 p->subs[b].owner = towner;
884 p->subs[b].inthreeway = tinthreeway;
886 if (p->subs[a].owner)
887 p->subs[a].owner->fds[0] = p->subs[a].zfd;
888 if (p->subs[b].owner)
889 p->subs[b].owner->fds[0] = p->subs[b].zfd;
890 wakeup_sub(p, a, NULL);
891 wakeup_sub(p, b, NULL);
894 static int zt_open(char *fn)
902 for (x=0;x<strlen(fn);x++) {
903 if (!isdigit(fn[x])) {
911 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
914 fn = "/dev/zap/channel";
916 fd = open(fn, O_RDWR | O_NONBLOCK);
918 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
922 if (ioctl(fd, ZT_SPECIFY, &chan)) {
926 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
931 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
935 static void zt_close(int fd)
941 int zt_setlinear(int zfd, int linear)
944 res = ioctl(zfd, ZT_SETLINEAR, &linear);
951 int zt_setlaw(int zfd, int law)
954 res = ioctl(zfd, ZT_SETLAW, &law);
960 static int alloc_sub(struct zt_pvt *p, int x)
964 if (p->subs[x].zfd < 0) {
965 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
966 if (p->subs[x].zfd > -1) {
967 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
969 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
970 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
971 bi.numbufs = numbufs;
972 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
974 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
977 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
978 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
979 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
980 zt_close(p->subs[x].zfd);
985 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
988 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
991 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
995 static int unalloc_sub(struct zt_pvt *p, int x)
998 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1001 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
1002 if (p->subs[x].zfd > -1) {
1003 zt_close(p->subs[x].zfd);
1005 p->subs[x].zfd = -1;
1006 p->subs[x].linear = 0;
1007 p->subs[x].chan = 0;
1008 p->subs[x].owner = NULL;
1009 p->subs[x].inthreeway = 0;
1010 p->polarity = POLARITY_IDLE;
1011 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1015 static int zt_digit(struct ast_channel *ast, char digit)
1017 ZT_DIAL_OPERATION zo;
1022 ast_mutex_lock(&p->lock);
1023 index = zt_get_index(ast, p, 0);
1024 if ((index == SUB_REAL) && p->owner) {
1026 if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
1028 if (!pri_grab(p, p->pri)) {
1029 pri_information(p->pri->pri,p->call,digit);
1032 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
1033 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
1034 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1035 res = strlen(p->dialdest);
1036 p->dialdest[res++] = digit;
1037 p->dialdest[res] = '\0';
1043 zo.op = ZT_DIAL_OP_APPEND;
1044 zo.dialstr[0] = 'T';
1045 zo.dialstr[1] = digit;
1047 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1048 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1053 ast_mutex_unlock(&p->lock);
1057 static char *events[] = {
1070 "Hook Transition Complete",
1075 "Polarity Reversal",
1083 { ZT_ALARM_RED, "Red Alarm" },
1084 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1085 { ZT_ALARM_BLUE, "Blue Alarm" },
1086 { ZT_ALARM_RECOVER, "Recovering" },
1087 { ZT_ALARM_LOOPBACK, "Loopback" },
1088 { ZT_ALARM_NOTOPEN, "Not Open" },
1089 { ZT_ALARM_NONE, "None" },
1092 static char *alarm2str(int alarm)
1095 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
1096 if (alarms[x].alarm & alarm)
1097 return alarms[x].name;
1099 return alarm ? "Unknown Alarm" : "No Alarm";
1102 static char *event2str(int event)
1104 static char buf[256];
1105 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1106 return events[event];
1107 sprintf(buf, "Event %d", event); /* safe */
1112 static char *dialplan2str(int dialplan)
1114 if (dialplan == -1) {
1115 return("Dynamically set dialplan in ISDN");
1117 return(pri_plan2str(dialplan));
1122 static int str2r2prot(char *swtype)
1124 if (!strcasecmp(swtype, "ar"))
1125 return MFCR2_PROT_ARGENTINA;
1127 if (!strcasecmp(swtype, "cn"))
1128 return MFCR2_PROT_CHINA;
1130 if (!strcasecmp(swtype, "kr"))
1131 return MFCR2_PROT_KOREA;
1137 static char *zap_sig2str(int sig)
1139 static char buf[256];
1142 return "E & M Immediate";
1144 return "E & M Wink";
1148 return "Feature Group D (DTMF)";
1150 return "Feature Group D (MF)";
1151 case SIG_FEATDMF_TA:
1152 return "Feature Groud D (MF) Tandem Access";
1154 return "Feature Group B (MF)";
1158 return "FGC/CAMA (Dialpulse)";
1159 case SIG_FGC_CAMAMF:
1160 return "FGC/CAMA (MF)";
1162 return "FXS Loopstart";
1164 return "FXS Groundstart";
1166 return "FXS Kewlstart";
1168 return "FXO Loopstart";
1170 return "FXO Groundstart";
1172 return "FXO Kewlstart";
1174 return "PRI Signalling";
1176 return "R2 Signalling";
1178 return "SF (Tone) Signalling Immediate";
1180 return "SF (Tone) Signalling Wink";
1182 return "SF (Tone) Signalling with Feature Group D (DTMF)";
1183 case SIG_SF_FEATDMF:
1184 return "SF (Tone) Signalling with Feature Group D (MF)";
1186 return "SF (Tone) Signalling with Feature Group B (MF)";
1187 case SIG_GR303FXOKS:
1188 return "GR-303 Signalling with FXOKS";
1189 case SIG_GR303FXSKS:
1190 return "GR-303 Signalling with FXSKS";
1192 return "Pseudo Signalling";
1194 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1199 #define sig2str zap_sig2str
1201 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1203 /* If the conference already exists, and we're already in it
1204 don't bother doing anything */
1207 memset(&zi, 0, sizeof(zi));
1210 if (slavechannel > 0) {
1211 /* If we have only one slave, do a digital mon */
1212 zi.confmode = ZT_CONF_DIGITALMON;
1213 zi.confno = slavechannel;
1216 /* Real-side and pseudo-side both participate in conference */
1217 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1218 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1220 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1221 zi.confno = p->confno;
1223 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1227 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1228 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1231 if (slavechannel < 1) {
1232 p->confno = zi.confno;
1234 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1235 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1239 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1241 /* If they're listening to our channel, they're ours */
1242 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1244 /* If they're a talker on our (allocated) conference, they're ours */
1245 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1250 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1253 if (/* Can't delete if there's no zfd */
1255 /* Don't delete from the conference if it's not our conference */
1257 /* Don't delete if we don't think it's conferenced at all (implied) */
1259 memset(&zi, 0, sizeof(zi));
1263 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1264 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1267 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1268 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1272 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1276 struct zt_pvt *slave = NULL;
1277 /* Start out optimistic */
1279 /* Update conference state in a stateless fashion */
1281 /* Any three-way calling makes slave native mode *definitely* out
1283 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1286 /* If we don't have any 3-way calls, check to see if we have
1287 precisely one slave */
1288 if (useslavenative) {
1289 for (x=0;x<MAX_SLAVES;x++) {
1292 /* Whoops already have a slave! No
1293 slave native and stop right away */
1298 /* We have one slave so far */
1299 slave = p->slaves[x];
1304 /* If no slave, slave native definitely out */
1307 else if (slave->law != p->law) {
1313 return useslavenative;
1316 static int reset_conf(struct zt_pvt *p)
1319 memset(&zi, 0, sizeof(zi));
1321 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1322 if (p->subs[SUB_REAL].zfd > -1) {
1323 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1324 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1329 static int update_conf(struct zt_pvt *p)
1334 struct zt_pvt *slave = NULL;
1336 useslavenative = isslavenative(p, &slave);
1337 /* Start with the obvious, general stuff */
1339 /* Look for three way calls */
1340 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1341 conf_add(p, &p->subs[x], x, 0);
1344 conf_del(p, &p->subs[x], x);
1347 /* If we have a slave, add him to our conference now. or DAX
1348 if this is slave native */
1349 for (x=0;x<MAX_SLAVES;x++) {
1352 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1354 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1359 /* If we're supposed to be in there, do so now */
1360 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1362 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1364 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1368 /* If we have a master, add ourselves to his conference */
1370 if (isslavenative(p->master, NULL)) {
1371 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1373 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1377 /* Nobody is left (or should be left) in our conference.
1381 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1385 static void zt_enable_ec(struct zt_pvt *p)
1392 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
1396 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
1399 if (p->echocancel) {
1400 if (p->sig == SIG_PRI) {
1402 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1404 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1407 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1409 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1412 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
1415 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
1418 static void zt_train_ec(struct zt_pvt *p)
1422 if (p && p->echocancel && p->echotraining) {
1423 x = p->echotraining;
1424 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1426 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1428 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
1431 ast_log(LOG_DEBUG, "No echo training requested\n");
1434 static void zt_disable_ec(struct zt_pvt *p)
1438 if (p->echocancel) {
1440 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1442 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1444 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
1449 static void fill_txgain(struct zt_gains *g, float gain, int law)
1453 float linear_gain = pow(10.0, gain / 20.0);
1457 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1459 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1460 if (k > 32767) k = 32767;
1461 if (k < -32767) k = -32767;
1462 g->txgain[j] = AST_LIN2A(k);
1469 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1471 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1472 if (k > 32767) k = 32767;
1473 if (k < -32767) k = -32767;
1474 g->txgain[j] = AST_LIN2MU(k);
1483 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1487 float linear_gain = pow(10.0, gain / 20.0);
1491 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1493 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1494 if (k > 32767) k = 32767;
1495 if (k < -32767) k = -32767;
1496 g->rxgain[j] = AST_LIN2A(k);
1503 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1505 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1506 if (k > 32767) k = 32767;
1507 if (k < -32767) k = -32767;
1508 g->rxgain[j] = AST_LIN2MU(k);
1517 int set_actual_txgain(int fd, int chan, float gain, int law)
1522 memset(&g, 0, sizeof(g));
1524 res = ioctl(fd, ZT_GETGAINS, &g);
1526 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1530 fill_txgain(&g, gain, law);
1532 return ioctl(fd, ZT_SETGAINS, &g);
1535 int set_actual_rxgain(int fd, int chan, float gain, int law)
1540 memset(&g, 0, sizeof(g));
1542 res = ioctl(fd, ZT_GETGAINS, &g);
1544 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
1548 fill_rxgain(&g, gain, law);
1550 return ioctl(fd, ZT_SETGAINS, &g);
1553 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1555 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1558 static int bump_gains(struct zt_pvt *p)
1562 /* Bump receive gain by 5.0db */
1563 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
1565 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1572 static int restore_gains(struct zt_pvt *p)
1576 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1578 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1585 static inline int zt_set_hook(int fd, int hs)
1589 res = ioctl(fd, ZT_HOOK, &x);
1592 if (errno == EINPROGRESS) return 0;
1593 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1598 static inline int zt_confmute(struct zt_pvt *p, int muted)
1602 if (p->sig == SIG_PRI) {
1604 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1606 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1608 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1610 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1614 static int save_conference(struct zt_pvt *p)
1616 struct zt_confinfo c;
1618 if (p->saveconf.confmode) {
1619 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1622 p->saveconf.chan = 0;
1623 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1625 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1626 p->saveconf.confmode = 0;
1631 c.confmode = ZT_CONF_NORMAL;
1632 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1634 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1638 ast_log(LOG_DEBUG, "Disabled conferencing\n");
1642 static int restore_conference(struct zt_pvt *p)
1645 if (p->saveconf.confmode) {
1646 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1647 p->saveconf.confmode = 0;
1649 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1654 ast_log(LOG_DEBUG, "Restored conferencing\n");
1658 static int send_callerid(struct zt_pvt *p);
1660 int send_cwcidspill(struct zt_pvt *p)
1664 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1666 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1667 /* Make sure we account for the end */
1668 p->cidlen += READ_SIZE * 4;
1671 if (option_verbose > 2)
1672 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1676 static int has_voicemail(struct zt_pvt *p)
1679 return ast_app_has_voicemail(p->mailbox, NULL);
1682 static int send_callerid(struct zt_pvt *p)
1684 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1686 /* Take out of linear mode if necessary */
1687 if (p->subs[SUB_REAL].linear) {
1688 p->subs[SUB_REAL].linear = 0;
1689 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1691 while(p->cidpos < p->cidlen) {
1692 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1694 if (errno == EAGAIN)
1697 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1707 if (p->callwaitcas) {
1708 /* Wait for CID/CW to expire */
1709 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1711 restore_conference(p);
1715 static int zt_callwait(struct ast_channel *ast)
1717 struct zt_pvt *p = ast->tech_pvt;
1718 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1720 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1723 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1727 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1728 if (!p->callwaitrings && p->callwaitingcallerid) {
1729 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1731 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1733 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1735 p->cidlen = 2400 + READ_SIZE * 4;
1743 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1745 struct zt_pvt *p = ast->tech_pvt;
1751 char dest[256]; /* must be same length as p->dialdest */
1752 ast_mutex_lock(&p->lock);
1753 ast_copy_string(dest, rdest, sizeof(dest));
1754 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1755 if ((ast->_state == AST_STATE_BUSY)) {
1756 p->subs[SUB_REAL].needbusy = 1;
1757 ast_mutex_unlock(&p->lock);
1760 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1761 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1762 ast_mutex_unlock(&p->lock);
1766 if (p->radio) /* if a radio channel, up immediately */
1768 /* Special pseudo -- automatically up */
1769 ast_setstate(ast, AST_STATE_UP);
1770 ast_mutex_unlock(&p->lock);
1773 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1774 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1776 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1779 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1785 if (p->owner == ast) {
1786 /* Normal ring, on hook */
1788 /* Don't send audio while on hook, until the call is answered */
1790 if (p->use_callerid) {
1791 /* Generate the Caller-ID spill if desired */
1793 ast_log(LOG_WARNING, "cidspill already exists??\n");
1797 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
1798 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
1803 /* Choose proper cadence */
1804 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
1805 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
1806 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
1807 p->cidrings = cidrings[p->distinctivering - 1];
1809 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
1810 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
1811 p->cidrings = p->sendcalleridafter;
1815 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
1816 c = strchr(dest, '/');
1819 if (c && (strlen(c) < p->stripmsd)) {
1820 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1824 p->dop.op = ZT_DIAL_OP_REPLACE;
1825 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
1826 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
1828 p->dop.dialstr[0] = '\0';
1831 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
1832 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
1833 ast_mutex_unlock(&p->lock);
1838 /* Call waiting call */
1839 p->callwaitrings = 0;
1840 if (ast->cid.cid_num)
1841 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
1843 p->callwait_num[0] = '\0';
1844 if (ast->cid.cid_name)
1845 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
1847 p->callwait_name[0] = '\0';
1848 /* Call waiting tone instead */
1849 if (zt_callwait(ast)) {
1850 ast_mutex_unlock(&p->lock);
1853 /* Make ring-back */
1854 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
1855 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
1858 n = ast->cid.cid_name;
1859 l = ast->cid.cid_num;
1861 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
1863 p->lastcid_num[0] = '\0';
1865 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
1867 p->lastcid_name[0] = '\0';
1868 ast_setstate(ast, AST_STATE_RINGING);
1869 index = zt_get_index(ast, p, 0);
1871 p->subs[index].needringing = 1;
1884 case SIG_FGC_CAMAMF:
1889 case SIG_SF_FEATDMF:
1890 case SIG_FEATDMF_TA:
1892 c = strchr(dest, '/');
1897 if (strlen(c) < p->stripmsd) {
1898 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
1899 ast_mutex_unlock(&p->lock);
1903 /* Start the trunk, if not GR-303 */
1907 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1909 if (errno != EINPROGRESS) {
1910 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
1911 ast_mutex_unlock(&p->lock);
1918 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
1919 p->dop.op = ZT_DIAL_OP_REPLACE;
1925 l = ast->cid.cid_num;
1927 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
1929 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
1932 l = ast->cid.cid_num;
1934 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
1936 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
1938 case SIG_FEATDMF_TA:
1940 const char *cic, *ozz;
1942 /* If you have to go through a Tandem Access point you need to use this */
1943 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
1946 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
1950 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
1951 ast_mutex_unlock(&p->lock);
1954 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
1955 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
1960 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
1963 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
1965 case SIG_FGC_CAMAMF:
1967 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
1971 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
1973 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
1977 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
1978 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
1979 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
1980 p->echorest[sizeof(p->echorest) - 1] = '\0';
1982 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
1986 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
1988 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
1989 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
1990 ast_mutex_unlock(&p->lock);
1994 ast_log(LOG_DEBUG, "Deferring dialing...\n");
1996 if (ast_strlen_zero(c))
1998 ast_setstate(ast, AST_STATE_DIALING);
2001 /* Special pseudo -- automatically up*/
2002 ast_setstate(ast, AST_STATE_UP);
2005 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2006 p->dialdest[0] = '\0';
2009 ast_log(LOG_DEBUG, "not yet implemented\n");
2010 ast_mutex_unlock(&p->lock);
2016 const char *useruser;
2019 int prilocaldialplan;
2023 c = strchr(dest, '/');
2028 if (!p->hidecallerid) {
2029 l = ast->cid.cid_num;
2030 n = ast->cid.cid_name;
2035 if (strlen(c) < p->stripmsd) {
2036 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2037 ast_mutex_unlock(&p->lock);
2040 if (p->sig != SIG_FXSKS) {
2041 p->dop.op = ZT_DIAL_OP_REPLACE;
2042 s = strchr(c + p->stripmsd, 'w');
2045 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2047 p->dop.dialstr[0] = '\0';
2050 p->dop.dialstr[0] = '\0';
2053 if (pri_grab(p, p->pri)) {
2054 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2055 ast_mutex_unlock(&p->lock);
2058 if (!(p->call = pri_new_call(p->pri->pri))) {
2059 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2061 ast_mutex_unlock(&p->lock);
2064 if (!(sr = pri_sr_new())) {
2065 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2067 ast_mutex_unlock(&p->lock);
2069 if (p->bearer || (p->sig == SIG_FXSKS)) {
2071 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);
2072 p->bearer->call = p->call;
2074 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
2075 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2077 p->digital = IS_DIGITAL(ast->transfercapability);
2078 /* Add support for exclusive override */
2079 if (p->priexclusive)
2082 /* otherwise, traditional behavior */
2083 if (p->pri->nodetype == PRI_NETWORK)
2089 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2090 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2092 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2093 if (p->pri->facilityenable)
2094 pri_facility_enable(p->pri->pri);
2096 if (option_verbose > 2)
2097 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2099 pridialplan = p->pri->dialplan - 1;
2100 if (pridialplan == -2) { /* compute dynamically */
2101 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2102 dp_strip = strlen(p->pri->internationalprefix);
2103 pridialplan = PRI_INTERNATIONAL_ISDN;
2104 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2105 dp_strip = strlen(p->pri->nationalprefix);
2106 pridialplan = PRI_NATIONAL_ISDN;
2108 pridialplan = PRI_LOCAL_ISDN;
2111 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2114 prilocaldialplan = p->pri->localdialplan - 1;
2115 if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
2116 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2117 ldp_strip = strlen(p->pri->internationalprefix);
2118 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2119 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2120 ldp_strip = strlen(p->pri->nationalprefix);
2121 prilocaldialplan = PRI_NATIONAL_ISDN;
2123 prilocaldialplan = PRI_LOCAL_ISDN;
2126 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2127 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
2128 PRES_NUMBER_NOT_AVAILABLE);
2129 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
2130 /* User-user info */
2131 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2134 pri_sr_set_useruser(sr, useruser);
2136 if (pri_setup(p->pri->pri, p->call, sr)) {
2137 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2138 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2140 ast_mutex_unlock(&p->lock);
2145 ast_setstate(ast, AST_STATE_DIALING);
2149 ast_mutex_unlock(&p->lock);
2153 static void destroy_zt_pvt(struct zt_pvt **pvt)
2155 struct zt_pvt *p = *pvt;
2156 /* Remove channel from the list */
2158 p->prev->next = p->next;
2160 p->next->prev = p->prev;
2161 ast_mutex_destroy(&p->lock);
2166 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2176 for (i = 0; i < 3; i++) {
2177 if (cur->subs[i].owner) {
2183 prev->next = cur->next;
2185 prev->next->prev = prev;
2191 iflist->prev = NULL;
2195 if (cur->subs[SUB_REAL].zfd > -1) {
2196 zt_close(cur->subs[SUB_REAL].zfd);
2198 destroy_zt_pvt(&cur);
2202 prev->next = cur->next;
2204 prev->next->prev = prev;
2210 iflist->prev = NULL;
2214 if (cur->subs[SUB_REAL].zfd > -1) {
2215 zt_close(cur->subs[SUB_REAL].zfd);
2217 destroy_zt_pvt(&cur);
2223 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2225 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2227 static char *zap_send_keypad_facility_descrip =
2228 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2229 " IE over the current channel.\n";
2231 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2233 /* Data will be our digit string */
2235 char *digits = (char *) data;
2237 if (ast_strlen_zero(digits)) {
2238 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
2242 p = (struct zt_pvt *)chan->tech_pvt;
2245 ast_log(LOG_DEBUG, "Unable to find technology private\n");
2249 ast_mutex_lock(&p->lock);
2251 if (!p->pri || !p->call) {
2252 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
2253 ast_mutex_unlock(&p->lock);
2257 if (!pri_grab(p, p->pri)) {
2258 pri_keypad_facility(p->pri->pri, p->call, digits);
2261 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
2262 ast_mutex_unlock(&p->lock);
2266 ast_mutex_unlock(&p->lock);
2271 int pri_is_up(struct zt_pri *pri)
2274 for (x=0;x<NUM_DCHANS;x++) {
2275 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2281 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2283 bearer->owner = &inuse;
2284 bearer->realcall = crv;
2285 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2286 if (crv->subs[SUB_REAL].owner)
2287 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
2288 crv->bearer = bearer;
2289 crv->call = bearer->call;
2294 static char *pri_order(int level)
2304 return "Quaternary";
2310 /* Returns fd of the active dchan */
2311 int pri_active_dchan_fd(struct zt_pri *pri)
2315 for (x = 0; x < NUM_DCHANS; x++) {
2316 if ((pri->dchans[x] == pri->pri))
2323 int pri_find_dchan(struct zt_pri *pri)
2330 for(x=0;x<NUM_DCHANS;x++) {
2331 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2333 if (pri->dchans[x] == old) {
2339 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2340 pri->dchannels[newslot]);
2342 if (old && (oldslot != newslot))
2343 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2344 pri->dchannels[oldslot], pri->dchannels[newslot]);
2345 pri->pri = pri->dchans[newslot];
2350 static int zt_hangup(struct ast_channel *ast)
2354 /*static int restore_gains(struct zt_pvt *p);*/
2355 struct zt_pvt *p = ast->tech_pvt;
2356 struct zt_pvt *tmp = NULL;
2357 struct zt_pvt *prev = NULL;
2361 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
2362 if (!ast->tech_pvt) {
2363 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2367 ast_mutex_lock(&p->lock);
2369 index = zt_get_index(ast, p, 1);
2371 if (p->sig == SIG_PRI) {
2373 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2379 if (p->origcid_num) {
2380 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2381 free(p->origcid_num);
2382 p->origcid_num = NULL;
2384 if (p->origcid_name) {
2385 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2386 free(p->origcid_name);
2387 p->origcid_name = NULL;
2390 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2394 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2395 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2399 /* Real channel, do some fixup */
2400 p->subs[index].owner = NULL;
2401 p->subs[index].needanswer = 0;
2402 p->subs[index].needflash = 0;
2403 p->subs[index].needringing = 0;
2404 p->subs[index].needbusy = 0;
2405 p->subs[index].needcongestion = 0;
2406 p->subs[index].linear = 0;
2407 p->subs[index].needcallerid = 0;
2408 p->polarity = POLARITY_IDLE;
2409 zt_setlinear(p->subs[index].zfd, 0);
2410 if (index == SUB_REAL) {
2411 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2412 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
2413 if (p->subs[SUB_CALLWAIT].inthreeway) {
2414 /* We had flipped over to answer a callwait and now it's gone */
2415 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
2416 /* Move to the call-wait, but un-own us until they flip back. */
2417 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2418 unalloc_sub(p, SUB_CALLWAIT);
2421 /* The three way hung up, but we still have a call wait */
2422 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2423 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2424 unalloc_sub(p, SUB_THREEWAY);
2425 if (p->subs[SUB_REAL].inthreeway) {
2426 /* This was part of a three way call. Immediately make way for
2428 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2429 p->owner = p->subs[SUB_REAL].owner;
2431 /* This call hasn't been completed yet... Set owner to NULL */
2432 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2435 p->subs[SUB_REAL].inthreeway = 0;
2437 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2438 /* Move to the call-wait and switch back to them. */
2439 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2440 unalloc_sub(p, SUB_CALLWAIT);
2441 p->owner = p->subs[SUB_REAL].owner;
2442 if (p->owner->_state != AST_STATE_UP)
2443 p->subs[SUB_REAL].needanswer = 1;
2444 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2445 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
2446 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2447 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2448 unalloc_sub(p, SUB_THREEWAY);
2449 if (p->subs[SUB_REAL].inthreeway) {
2450 /* This was part of a three way call. Immediately make way for
2452 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
2453 p->owner = p->subs[SUB_REAL].owner;
2455 /* This call hasn't been completed yet... Set owner to NULL */
2456 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
2459 p->subs[SUB_REAL].inthreeway = 0;
2461 } else if (index == SUB_CALLWAIT) {
2462 /* Ditch the holding callwait call, and immediately make it availabe */
2463 if (p->subs[SUB_CALLWAIT].inthreeway) {
2464 /* This is actually part of a three way, placed on hold. Place the third part
2465 on music on hold now */
2466 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
2467 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
2468 p->subs[SUB_THREEWAY].inthreeway = 0;
2469 /* Make it the call wait now */
2470 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2471 unalloc_sub(p, SUB_THREEWAY);
2473 unalloc_sub(p, SUB_CALLWAIT);
2474 } else if (index == SUB_THREEWAY) {
2475 if (p->subs[SUB_CALLWAIT].inthreeway) {
2476 /* The other party of the three way call is currently in a call-wait state.
2477 Start music on hold for them, and take the main guy out of the third call */
2478 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
2479 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
2480 p->subs[SUB_CALLWAIT].inthreeway = 0;
2482 p->subs[SUB_REAL].inthreeway = 0;
2483 /* If this was part of a three way call index, let us make
2484 another three way call */
2485 unalloc_sub(p, SUB_THREEWAY);
2487 /* This wasn't any sort of call, but how are we an index? */
2488 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2493 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2496 p->distinctivering = 0;
2497 p->confirmanswer = 0;
2503 p->onhooktime = time(NULL);
2511 ast_dsp_free(p->dsp);
2515 law = ZT_LAW_DEFAULT;
2516 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2518 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2519 /* Perform low level hangup if no owner left */
2522 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2523 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
2524 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
2525 if (!pri_grab(p, p->pri)) {
2526 if (p->alreadyhungup) {
2527 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
2528 pri_call_set_useruser(p->call, useruser);
2529 pri_hangup(p->pri->pri, p->call, -1);
2532 p->bearer->call = NULL;
2534 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
2535 int icause = ast->hangupcause ? ast->hangupcause : -1;
2536 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
2537 pri_call_set_useruser(p->call, useruser);
2538 p->alreadyhungup = 1;
2540 p->bearer->alreadyhungup = 1;
2543 icause = atoi(cause);
2545 pri_hangup(p->pri->pri, p->call, icause);
2548 ast_log(LOG_WARNING, "pri_disconnect failed\n");
2551 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2556 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
2563 if (p->sig == SIG_R2) {
2565 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
2573 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
2574 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
2576 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
2582 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
2585 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
2587 /* If they're off hook, try playing congestion */
2588 if ((par.rxisoffhook) && (!p->radio))
2589 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
2591 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2597 /* Make sure we're not made available for at least two seconds assuming
2598 we were actually used for an inbound or outbound call. */
2599 if (ast->_state != AST_STATE_RESERVED) {
2600 time(&p->guardtime);
2605 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
2612 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
2613 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
2617 p->callwaiting = p->permcallwaiting;
2618 p->hidecallerid = p->permhidecallerid;
2623 /* Restore data mode */
2624 if (p->sig == SIG_PRI) {
2626 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2630 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
2631 /* Free up the bearer channel as well, and
2632 don't use its file descriptor anymore */
2633 update_conf(p->bearer);
2634 reset_conf(p->bearer);
2635 p->bearer->owner = NULL;
2636 p->bearer->realcall = NULL;
2638 p->subs[SUB_REAL].zfd = -1;
2646 p->callwaitingrepeat = 0;
2648 ast->tech_pvt = NULL;
2649 ast_mutex_unlock(&p->lock);
2650 ast_mutex_lock(&usecnt_lock);
2653 ast_log(LOG_WARNING, "Usecnt < 0???\n");
2654 ast_mutex_unlock(&usecnt_lock);
2655 ast_update_use_count();
2656 if (option_verbose > 2)
2657 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
2659 ast_mutex_lock(&iflock);
2665 destroy_channel(prev, tmp, 0);
2673 ast_mutex_unlock(&iflock);
2677 static int zt_answer(struct ast_channel *ast)
2679 struct zt_pvt *p = ast->tech_pvt;
2682 int oldstate = ast->_state;
2683 ast_setstate(ast, AST_STATE_UP);
2684 ast_mutex_lock(&p->lock);
2685 index = zt_get_index(ast, p, 0);
2688 /* nothing to do if a radio channel */
2690 ast_mutex_unlock(&p->lock);
2704 case SIG_FEATDMF_TA:
2707 case SIG_FGC_CAMAMF:
2712 case SIG_SF_FEATDMF:
2717 /* Pick up the line */
2718 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
2719 if(p->hanguponpolarityswitch) {
2720 gettimeofday(&p->polaritydelaytv, NULL);
2722 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
2723 tone_zone_play_tone(p->subs[index].zfd, -1);
2725 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
2726 if (oldstate == AST_STATE_RINGING) {
2727 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
2728 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
2729 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2730 p->owner = p->subs[SUB_REAL].owner;
2733 if (p->sig & __ZT_SIG_FXS) {
2740 /* Send a pri acknowledge */
2741 if (!pri_grab(p, p->pri)) {
2743 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
2746 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
2753 res = mfcr2_AnswerCall(p->r2, NULL);
2755 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
2759 ast_mutex_unlock(&p->lock);
2762 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
2765 ast_mutex_unlock(&p->lock);
2769 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
2775 struct zt_pvt *p = chan->tech_pvt;
2777 /* all supported options require data */
2778 if (!data || (datalen < 1)) {
2784 case AST_OPTION_TXGAIN:
2785 scp = (signed char *) data;
2786 index = zt_get_index(chan, p, 0);
2788 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
2791 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
2792 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
2793 case AST_OPTION_RXGAIN:
2794 scp = (signed char *) data;
2795 index = zt_get_index(chan, p, 0);
2797 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
2800 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
2801 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
2802 case AST_OPTION_TONE_VERIFY:
2808 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
2809 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
2812 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
2813 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
2816 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
2817 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
2821 case AST_OPTION_TDD:
2822 /* turn on or off TDD */
2825 if (!*cp) { /* turn it off */
2826 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
2827 if (p->tdd) tdd_free(p->tdd);
2831 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
2832 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
2834 /* otherwise, turn it on */
2835 if (!p->didtdd) { /* if havent done it yet */
2836 unsigned char mybuf[41000],*buf;
2837 int size,res,fd,len;
2838 struct pollfd fds[1];
2841 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
2842 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
2844 index = zt_get_index(chan, p, 0);
2846 ast_log(LOG_WARNING, "No index in TDD?\n");
2849 fd = p->subs[index].zfd;
2851 if (ast_check_hangup(chan)) return -1;
2853 if (size > READ_SIZE)
2856 fds[0].events = POLLPRI | POLLOUT;
2858 res = poll(fds, 1, -1);
2860 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
2863 /* if got exception */
2864 if (fds[0].revents & POLLPRI) return -1;
2865 if (!(fds[0].revents & POLLOUT)) {
2866 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
2869 res = write(fd, buf, size);
2871 if (res == -1) return -1;
2872 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
2878 p->didtdd = 1; /* set to have done it now */
2880 if (*cp == 2) { /* Mate mode */
2881 if (p->tdd) tdd_free(p->tdd);
2886 if (!p->tdd) { /* if we dont have one yet */
2887 p->tdd = tdd_new(); /* allocate one */
2890 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
2894 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
2895 *cp ? "ON" : "OFF", (int) *cp, chan->name);
2896 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
2898 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
2901 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
2905 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
2908 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
2909 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
2917 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
2919 /* Unlink a specific slave or all slaves/masters from a given master */
2925 ast_mutex_lock(&master->lock);
2927 while(ast_mutex_trylock(&slave->lock)) {
2928 ast_mutex_unlock(&master->lock);
2930 ast_mutex_lock(&master->lock);
2935 for (x=0;x<MAX_SLAVES;x++) {
2936 if (master->slaves[x]) {
2937 if (!slave || (master->slaves[x] == slave)) {
2938 /* Take slave out of the conference */
2939 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
2940 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
2941 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
2942 master->slaves[x]->master = NULL;
2943 master->slaves[x] = NULL;
2948 master->inconference = 0;
2951 if (master->master) {
2952 /* Take master out of the conference */
2953 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
2954 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
2956 for (x=0;x<MAX_SLAVES;x++) {
2957 if (master->master->slaves[x] == master)
2958 master->master->slaves[x] = NULL;
2959 else if (master->master->slaves[x])
2963 master->master->inconference = 0;
2965 master->master = NULL;
2967 update_conf(master);
2970 ast_mutex_unlock(&slave->lock);
2971 ast_mutex_unlock(&master->lock);
2975 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
2977 if (!slave || !master) {
2978 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
2981 for (x=0;x<MAX_SLAVES;x++) {
2982 if (!master->slaves[x]) {
2983 master->slaves[x] = slave;
2987 if (x >= MAX_SLAVES) {
2988 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
2989 master->slaves[MAX_SLAVES - 1] = slave;
2992 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
2993 slave->master = master;
2995 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
2998 static void disable_dtmf_detect(struct zt_pvt *p)
3000 #ifdef ZT_TONEDETECT
3006 #ifdef ZT_TONEDETECT
3008 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3010 if (!p->hardwaredtmf && p->dsp) {
3011 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
3012 ast_dsp_set_features(p->dsp, p->dsp_features);
3016 static void enable_dtmf_detect(struct zt_pvt *p)
3018 #ifdef ZT_TONEDETECT
3022 if (p->channel == CHAN_PSEUDO)
3027 #ifdef ZT_TONEDETECT
3028 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
3029 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
3031 if (!p->hardwaredtmf && p->dsp) {
3032 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
3033 ast_dsp_set_features(p->dsp, p->dsp_features);
3037 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)