2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Zaptel Pseudo TDM interface
23 * \author Mark Spencer <markster@digium.com>
25 * Connects to the Zaptel telephony library as well as
26 * libpri. Libpri is optional and needed only if you are
27 * going to use ISDN connections.
29 * You need to install libraries before you attempt to compile
30 * and install the Zaptel channel.
33 * \arg \ref Config_zap
35 * \ingroup channel_drivers
37 * \todo Deprecate the "musiconhold" configuration option post 1.4
41 <depend>res_smdi</depend>
42 <depend>zaptel_vldtmf</depend>
43 <depend>zaptel</depend>
44 <depend>tonezone</depend>
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
57 #include <sys/signal.h>
59 #include <sys/ioctl.h>
62 #include "asterisk/zapata.h"
72 #include "asterisk/lock.h"
73 #include "asterisk/channel.h"
74 #include "asterisk/config.h"
75 #include "asterisk/module.h"
76 #include "asterisk/pbx.h"
77 #include "asterisk/file.h"
78 #include "asterisk/ulaw.h"
79 #include "asterisk/alaw.h"
80 #include "asterisk/callerid.h"
81 #include "asterisk/adsi.h"
82 #include "asterisk/cli.h"
83 #include "asterisk/cdr.h"
84 #include "asterisk/features.h"
85 #include "asterisk/musiconhold.h"
86 #include "asterisk/say.h"
87 #include "asterisk/tdd.h"
88 #include "asterisk/app.h"
89 #include "asterisk/dsp.h"
90 #include "asterisk/astdb.h"
91 #include "asterisk/manager.h"
92 #include "asterisk/causes.h"
93 #include "asterisk/term.h"
94 #include "asterisk/utils.h"
95 #include "asterisk/transcap.h"
96 #include "asterisk/stringfields.h"
97 #include "asterisk/abstract_jb.h"
98 #include "asterisk/smdi.h"
99 #include "asterisk/astobj.h"
100 #include "asterisk/event.h"
102 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
104 #ifdef ZT_SPANINFO_HAS_LINECONFIG
105 static const char *lbostr[] = {
106 "0 db (CSU)/0-133 feet (DSX-1)",
107 "133-266 feet (DSX-1)",
108 "266-399 feet (DSX-1)",
109 "399-533 feet (DSX-1)",
110 "533-655 feet (DSX-1)",
117 /*! Global jitterbuffer configuration - by default, jb is disabled */
118 static struct ast_jb_conf default_jbconf =
122 .resync_threshold = -1,
125 static struct ast_jb_conf global_jbconf;
127 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_PRI) && !defined(ZT_SIG_HARDHDLC))
128 #error "Your Zaptel is too old. Please update"
131 #ifndef ZT_TONEDETECT
132 /* Work around older code with no tone detect */
133 #define ZT_EVENT_DTMFDOWN 0
134 #define ZT_EVENT_DTMFUP 0
137 /* define this to send PRI user-user information elements */
138 #undef SUPPORT_USERUSER
141 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
142 * the user hangs up to reset the state machine so ring works properly.
143 * This is used to be able to support kewlstart by putting the zhone in
144 * groundstart mode since their forward disconnect supervision is entirely
145 * broken even though their documentation says it isn't and their support
146 * is entirely unwilling to provide any assistance with their channel banks
147 * even though their web site says they support their products for life.
149 /* #define ZHONE_HACK */
152 * Define if you want to check the hook state for an FXO (FXS signalled) interface
153 * before dialing on it. Certain FXO interfaces always think they're out of
154 * service with this method however.
156 /* #define ZAP_CHECK_HOOKSTATE */
158 /*! \brief Typically, how many rings before we should send Caller*ID */
159 #define DEFAULT_CIDRINGS 1
161 #define CHANNEL_PSEUDO -12
163 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
166 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
167 #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))
169 static const char tdesc[] = "Zapata Telephony Driver"
178 static const char config[] = "zapata.conf";
180 #define SIG_EM ZT_SIG_EM
181 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
182 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
183 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
184 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
185 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
186 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
187 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
188 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
189 #define SIG_FXSLS ZT_SIG_FXSLS
190 #define SIG_FXSGS ZT_SIG_FXSGS
191 #define SIG_FXSKS ZT_SIG_FXSKS
192 #define SIG_FXOLS ZT_SIG_FXOLS
193 #define SIG_FXOGS ZT_SIG_FXOGS
194 #define SIG_FXOKS ZT_SIG_FXOKS
195 #define SIG_PRI ZT_SIG_CLEAR
196 #define SIG_BRI (0x2000000 | ZT_SIG_CLEAR)
197 #define SIG_BRI_PTMP (0X4000000 | ZT_SIG_CLEAR)
198 #define SIG_SS7 (0x1000000 | ZT_SIG_CLEAR)
199 #define SIG_SF ZT_SIG_SF
200 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
201 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
202 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
203 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
204 #define SIG_EM_E1 ZT_SIG_EM_E1
205 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
206 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
209 #define NUM_SPANS ZT_MAX_SPANS
213 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
214 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
216 #define CHAN_PSEUDO -2
218 #define DCHAN_PROVISIONED (1 << 0)
219 #define DCHAN_NOTINALARM (1 << 1)
220 #define DCHAN_UP (1 << 2)
222 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
224 /* Overlap dialing option types */
225 #define ZAP_OVERLAPDIAL_NONE 0
226 #define ZAP_OVERLAPDIAL_OUTGOING 1
227 #define ZAP_OVERLAPDIAL_INCOMING 2
228 #define ZAP_OVERLAPDIAL_BOTH (ZAP_OVERLAPDIAL_INCOMING|ZAP_OVERLAPDIAL_OUTGOING)
231 #define CALLPROGRESS_PROGRESS 1
232 #define CALLPROGRESS_FAX_OUTGOING 2
233 #define CALLPROGRESS_FAX_INCOMING 4
234 #define CALLPROGRESS_FAX (CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING)
236 static char defaultcic[64] = "";
237 static char defaultozz[64] = "";
239 /*! Run this script when the MWI state changes on an FXO line, if mwimonitor is enabled */
240 static char mwimonitornotify[PATH_MAX] = "";
242 static char progzone[10] = "";
244 static int usedistinctiveringdetection = 0;
245 static int distinctiveringaftercid = 0;
247 static int numbufs = 4;
249 static int mwilevel = 512;
252 static struct ast_channel inuse;
253 #ifdef PRI_GETSET_TIMERS
254 static int pritimers[PRI_MAX_TIMERS];
256 static int pridebugfd = -1;
257 static char pridebugfilename[1024] = "";
260 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
261 static int firstdigittimeout = 16000;
263 /*! \brief How long to wait for following digits (FXO logic) */
264 static int gendigittimeout = 8000;
266 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
267 static int matchdigittimeout = 3000;
269 /*! \brief Protect the interface list (of zt_pvt's) */
270 AST_MUTEX_DEFINE_STATIC(iflock);
273 static int ifcount = 0;
276 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
279 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
280 when it's doing something critical. */
281 AST_MUTEX_DEFINE_STATIC(monlock);
283 /*! \brief This is the thread for the monitor which checks for input on the channels
284 which are not currently in use. */
285 static pthread_t monitor_thread = AST_PTHREADT_NULL;
287 static int restart_monitor(void);
289 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);
291 static int zt_sendtext(struct ast_channel *c, const char *text);
293 static void mwi_event_cb(const struct ast_event *event, void *userdata)
295 /* This module does not handle MWI in an event-based manner. However, it
296 * subscribes to MWI for each mailbox that is configured so that the core
297 * knows that we care about it. Then, chan_zap will get the MWI from the
298 * event cache instead of checking the mailbox directly. */
301 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
302 static inline int zt_get_event(int fd)
305 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
310 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
311 static inline int zt_wait_event(int fd)
314 i = ZT_IOMUX_SIGEVENT;
315 if (ioctl(fd, ZT_IOMUX, &i) == -1)
317 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
322 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
323 #define READ_SIZE 160
325 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
326 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
328 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
329 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
330 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
331 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
332 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
336 static int ringt_base = DEFAULT_RINGT;
340 #define LINKSTATE_INALARM (1 << 0)
341 #define LINKSTATE_STARTING (1 << 1)
342 #define LINKSTATE_UP (1 << 2)
343 #define LINKSTATE_DOWN (1 << 3)
345 #define SS7_NAI_DYNAMIC -1
348 pthread_t master; /*!< Thread of master */
352 int linkstate[NUM_DCHANS];
356 LINKSET_STATE_DOWN = 0,
359 char called_nai; /*!< Called Nature of Address Indicator */
360 char calling_nai; /*!< Calling Nature of Address Indicator */
361 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
362 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
363 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
364 char unknownprefix[20]; /*!< for unknown dialplans */
366 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
369 static struct zt_ss7 linksets[NUM_SPANS];
371 static int cur_ss7type = -1;
372 static int cur_linkset = -1;
373 static int cur_pointcode = -1;
374 static int cur_cicbeginswith = -1;
375 static int cur_adjpointcode = -1;
376 static int cur_networkindicator = -1;
377 static int cur_defaultdpc = -1;
378 #endif /* HAVE_SS7 */
382 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
383 #define PRI_CHANNEL(p) ((p) & 0xff)
384 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
385 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
388 pthread_t master; /*!< Thread of master */
389 ast_mutex_t lock; /*!< Mutex */
390 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
391 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
392 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
393 int minunused; /*!< Min # of channels to keep empty */
394 int minidle; /*!< Min # of "idling" calls to keep active */
395 int nodetype; /*!< Node type */
396 int switchtype; /*!< Type of switch to emulate */
397 int nsf; /*!< Network-Specific Facilities */
398 int dialplan; /*!< Dialing plan */
399 int localdialplan; /*!< Local dialing plan */
400 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
401 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
402 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
403 char privateprefix[20]; /*!< for private dialplans */
404 char unknownprefix[20]; /*!< for unknown dialplans */
405 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
406 int trunkgroup; /*!< What our trunkgroup is */
407 int mastertrunkgroup; /*!< What trunk group is our master */
408 int prilogicalspan; /*!< Logical span number within trunk group */
409 int numchans; /*!< Num of channels we represent */
410 int overlapdial; /*!< In overlap dialing mode */
411 int facilityenable; /*!< Enable facility IEs */
412 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
413 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
414 struct pri *pri; /*!< Currently active D-channel */
416 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
421 time_t lastreset; /*!< time when unused channels were last reset */
422 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
424 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
425 struct zt_pvt *crvs; /*!< Member CRV structs */
426 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
430 static struct zt_pri pris[NUM_SPANS];
433 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
435 #define DEFAULT_PRI_DEBUG 0
438 static inline void pri_rel(struct zt_pri *pri)
440 ast_mutex_unlock(&pri->lock);
444 /*! Shut up the compiler */
448 #define SUB_REAL 0 /*!< Active call */
449 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
450 #define SUB_THREEWAY 2 /*!< Three-way call */
452 /* Polarity states */
453 #define POLARITY_IDLE 0
454 #define POLARITY_REV 1
457 struct distRingData {
461 struct ringContextData {
462 char contextData[AST_MAX_CONTEXT];
464 struct zt_distRings {
465 struct distRingData ringnum[3];
466 struct ringContextData ringContext[3];
469 static char *subnames[] = {
475 struct zt_subchannel {
477 struct ast_channel *owner;
479 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
480 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
481 unsigned int needringing:1;
482 unsigned int needbusy:1;
483 unsigned int needcongestion:1;
484 unsigned int needcallerid:1;
485 unsigned int needanswer:1;
486 unsigned int needflash:1;
487 unsigned int needhold:1;
488 unsigned int needunhold:1;
489 unsigned int linear:1;
490 unsigned int inthreeway:1;
494 #define CONF_USER_REAL (1 << 0)
495 #define CONF_USER_THIRDCALL (1 << 1)
499 static struct zt_pvt {
501 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
502 /*!< Up to three channels can be associated with this call */
504 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
505 struct zt_subchannel subs[3]; /*!< Sub-channels */
506 struct zt_confinfo saveconf; /*!< Saved conference info */
508 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
509 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
510 int inconference; /*!< If our real should be in the conference */
512 int sig; /*!< Signalling style */
513 int radio; /*!< radio type */
514 int outsigmod; /*!< Outbound Signalling style (modifier) */
515 int oprmode; /*!< "Operator Services" mode */
516 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
517 float cid_rxgain; /*!< "Gain to apply during caller id */
520 int tonezone; /*!< tone zone for this chan, or -1 for default */
521 struct zt_pvt *next; /*!< Next channel in list */
522 struct zt_pvt *prev; /*!< Prev channel in list */
526 unsigned int answeronpolarityswitch:1;
527 unsigned int busydetect:1;
528 unsigned int callreturn:1;
529 unsigned int callwaiting:1;
530 unsigned int callwaitingcallerid:1;
531 unsigned int cancallforward:1;
532 unsigned int canpark:1;
533 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
534 unsigned int destroy:1;
535 unsigned int didtdd:1; /*!< flag to say its done it once */
536 unsigned int dialednone:1;
537 unsigned int dialing:1;
538 unsigned int digital:1;
540 unsigned int echobreak:1;
541 unsigned int echocanbridged:1;
542 unsigned int echocanon:1;
543 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
544 unsigned int firstradio:1;
545 unsigned int hanguponpolarityswitch:1;
546 unsigned int hardwaredtmf:1;
547 unsigned int hidecallerid:1;
548 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
549 unsigned int ignoredtmf:1;
550 unsigned int immediate:1; /*!< Answer before getting digits? */
551 unsigned int inalarm:1;
552 unsigned int mate:1; /*!< flag to say its in MATE mode */
553 unsigned int outgoing:1;
554 /* unsigned int overlapdial:1; unused and potentially confusing */
555 unsigned int permcallwaiting:1;
556 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
557 unsigned int priindication_oob:1;
558 unsigned int priexclusive:1;
559 unsigned int pulse:1;
560 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
561 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
562 unsigned int threewaycalling:1;
563 unsigned int transfer:1;
564 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
565 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
566 unsigned int usedistinctiveringdetection:1;
567 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
568 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
569 unsigned int mwimonitor:1; /*!< monitor this FXO port for MWI indication from other end */
570 unsigned int mwimonitoractive:1; /*!< an MWI monitor thread is currently active */
571 /* Channel state or unavilability flags */
572 unsigned int inservice:1;
573 unsigned int locallyblocked:1;
574 unsigned int remotelyblocked:1;
575 #if defined(HAVE_PRI) || defined(HAVE_SS7)
577 unsigned int alerting:1;
578 unsigned int alreadyhungup:1;
579 unsigned int isidlecall:1;
580 unsigned int proceeding:1;
581 unsigned int progress:1;
582 unsigned int resetting:1;
583 unsigned int setup_ack:1;
585 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
586 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
588 struct zt_distRings drings;
590 char context[AST_MAX_CONTEXT];
591 char defcontext[AST_MAX_CONTEXT];
592 char exten[AST_MAX_EXTENSION];
593 char language[MAX_LANGUAGE];
594 char mohinterpret[MAX_MUSICCLASS];
595 char mohsuggest[MAX_MUSICCLASS];
596 #if defined(PRI_ANI) || defined(HAVE_SS7)
597 char cid_ani[AST_MAX_EXTENSION];
600 char cid_num[AST_MAX_EXTENSION];
601 int cid_ton; /*!< Type Of Number (TON) */
602 char cid_name[AST_MAX_EXTENSION];
603 char lastcid_num[AST_MAX_EXTENSION];
604 char lastcid_name[AST_MAX_EXTENSION];
605 char *origcid_num; /*!< malloced original callerid */
606 char *origcid_name; /*!< malloced original callerid */
607 char callwait_num[AST_MAX_EXTENSION];
608 char callwait_name[AST_MAX_EXTENSION];
609 char rdnis[AST_MAX_EXTENSION];
610 char dnid[AST_MAX_EXTENSION];
613 int confno; /*!< Our conference */
614 int confusers; /*!< Who is using our conference */
615 int propconfno; /*!< Propagated conference number */
616 ast_group_t callgroup;
617 ast_group_t pickupgroup;
618 struct ast_variable *vars;
619 int channel; /*!< Channel Number or CRV */
620 int span; /*!< Span number */
621 time_t guardtime; /*!< Must wait this much time before using for new call */
622 int cid_signalling; /*!< CID signalling type bell202 or v23 */
623 int cid_start; /*!< CID start indicator, polarity or ring */
624 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
625 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
626 int cidcwexpire; /*!< When to expire our muting for CID/CW */
627 unsigned char *cidspill;
635 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
637 struct zt_echocanparams head;
638 struct zt_echocanparam params[ZT_MAX_ECHOCANPARAMS];
647 int busy_quietlength;
649 struct timeval flashtime; /*!< Last flash-hook time */
651 int cref; /*!< Call reference number */
652 ZT_DIAL_OPERATION dop;
653 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
655 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
656 int amaflags; /*!< AMA Flags */
657 struct tdd_state *tdd; /*!< TDD flag */
658 char call_forward[AST_MAX_EXTENSION];
659 char mailbox[AST_MAX_EXTENSION];
660 struct ast_event_sub *mwi_event_sub;
664 int distinctivering; /*!< Which distinctivering to use */
665 int cidrings; /*!< Which ring to deliver CID on */
666 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
668 int polarityonanswerdelay;
669 struct timeval polaritydelaytv;
670 int sendcalleridafter;
673 struct zt_pvt *bearer;
674 struct zt_pvt *realcall;
683 struct isup_call *ss7call;
684 char charge_number[50];
685 char gen_add_number[50];
686 char gen_dig_number[50];
687 unsigned char gen_add_num_plan;
688 unsigned char gen_add_nai;
689 unsigned char gen_add_pres_ind;
690 unsigned char gen_add_type;
691 unsigned char gen_dig_type;
692 unsigned char gen_dig_scheme;
694 unsigned char lspi_type;
695 unsigned char lspi_scheme;
696 unsigned char lspi_context;
698 unsigned int call_ref_ident;
699 unsigned int call_ref_pc;
701 int cic; /*!< CIC associated with channel */
702 unsigned int dpc; /*!< CIC's DPC */
703 unsigned int loopedback:1;
706 } *iflist = NULL, *ifend = NULL;
708 /*! \brief Channel configuration from zapata.conf .
709 * This struct is used for parsing the [channels] section of zapata.conf.
710 * Generally there is a field here for every possible configuration item.
712 * The state of fields is saved along the parsing and whenever a 'channel'
713 * statement is reached, the current zt_chan_conf is used to configure the
714 * channel (struct zt_pvt)
716 * \see zt_chan_init for the default values.
718 struct zt_chan_conf {
728 int is_sig_auto; /*!< Use channel signalling from Zaptel? */
730 char smdi_port[SMDI_MAX_FILENAME_LEN];
733 /*! returns a new zt_chan_conf with default values (by-value) */
734 static struct zt_chan_conf zt_chan_conf_default(void) {
735 /* recall that if a field is not included here it is initialized
738 struct zt_chan_conf conf = {
742 .switchtype = PRI_SWITCH_NI2,
743 .dialplan = PRI_NATIONAL_ISDN + 1,
744 .localdialplan = PRI_NATIONAL_ISDN + 1,
750 .internationalprefix = "",
751 .nationalprefix = "",
760 .called_nai = SS7_NAI_NATIONAL,
761 .calling_nai = SS7_NAI_NATIONAL,
762 .internationalprefix = "",
763 .nationalprefix = "",
764 .subscriberprefix = "",
769 .context = "default",
772 .mohinterpret = "default",
776 .cid_signalling = CID_SIG_BELL,
777 .cid_start = CID_START_RING,
787 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
788 .echocancel.head.tap_length = 1,
800 .polarityonanswerdelay = 600,
802 .sendcalleridafter = DEFAULT_CIDRINGS
815 .smdi_port = "/dev/ttyS0",
822 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
823 static int zt_digit_begin(struct ast_channel *ast, char digit);
824 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
825 static int zt_sendtext(struct ast_channel *c, const char *text);
826 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
827 static int zt_hangup(struct ast_channel *ast);
828 static int zt_answer(struct ast_channel *ast);
829 static struct ast_frame *zt_read(struct ast_channel *ast);
830 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
831 static struct ast_frame *zt_exception(struct ast_channel *ast);
832 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
833 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
834 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
835 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
837 static const struct ast_channel_tech zap_tech = {
839 .description = tdesc,
840 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
841 .requester = zt_request,
842 .send_digit_begin = zt_digit_begin,
843 .send_digit_end = zt_digit_end,
844 .send_text = zt_sendtext,
851 .exception = zt_exception,
852 .indicate = zt_indicate,
854 .setoption = zt_setoption,
855 .func_channel_read = zt_func_read,
859 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
861 #define GET_CHANNEL(p) ((p)->channel)
864 struct zt_pvt *round_robin[32];
867 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
870 /* Grab the lock first */
872 res = ast_mutex_trylock(&pri->lock);
874 ast_mutex_unlock(&pvt->lock);
875 /* Release the lock and try again */
877 ast_mutex_lock(&pvt->lock);
880 /* Then break the poll */
881 pthread_kill(pri->master, SIGURG);
887 static inline void ss7_rel(struct zt_ss7 *ss7)
889 ast_mutex_unlock(&ss7->lock);
892 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
895 /* Grab the lock first */
897 res = ast_mutex_trylock(&pri->lock);
899 ast_mutex_unlock(&pvt->lock);
900 /* Release the lock and try again */
902 ast_mutex_lock(&pvt->lock);
905 /* Then break the poll */
906 pthread_kill(pri->master, SIGURG);
910 #define NUM_CADENCE_MAX 25
911 static int num_cadence = 4;
912 static int user_has_defined_cadences = 0;
914 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
915 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
916 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
917 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
918 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
921 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
922 * is 1, the second pause is 2 and so on.
925 static int cidrings[NUM_CADENCE_MAX] = {
926 2, /*!< Right after first long ring */
927 4, /*!< Right after long part */
928 3, /*!< After third chirp */
929 2, /*!< Second spell */
932 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
933 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
935 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
936 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
938 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
941 if (p->subs[0].owner == ast)
943 else if (p->subs[1].owner == ast)
945 else if (p->subs[2].owner == ast)
950 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
956 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
958 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
963 ast_mutex_unlock(&pri->lock);
966 if (p->subs[a].owner) {
967 if (ast_channel_trylock(p->subs[a].owner)) {
968 ast_mutex_unlock(&p->lock);
970 ast_mutex_lock(&p->lock);
972 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
973 ast_channel_unlock(p->subs[a].owner);
981 ast_mutex_lock(&pri->lock);
985 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
988 struct zt_pri *pri = (struct zt_pri*) data;
991 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
993 /* We must unlock the PRI to avoid the possibility of a deadlock */
994 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1001 ast_mutex_unlock(&pri->lock);
1006 ast_mutex_unlock(&ss7->lock);
1016 if (ast_channel_trylock(p->owner)) {
1017 ast_mutex_unlock(&p->lock);
1019 ast_mutex_lock(&p->lock);
1021 ast_queue_frame(p->owner, f);
1022 ast_channel_unlock(p->owner);
1028 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1035 ast_mutex_lock(&pri->lock);
1040 ast_mutex_lock(&ss7->lock);
1051 static int restore_gains(struct zt_pvt *p);
1053 static void swap_subs(struct zt_pvt *p, int a, int b)
1057 struct ast_channel *towner;
1059 ast_debug(1, "Swapping %d and %d\n", a, b);
1061 tchan = p->subs[a].chan;
1062 towner = p->subs[a].owner;
1063 tinthreeway = p->subs[a].inthreeway;
1065 p->subs[a].chan = p->subs[b].chan;
1066 p->subs[a].owner = p->subs[b].owner;
1067 p->subs[a].inthreeway = p->subs[b].inthreeway;
1069 p->subs[b].chan = tchan;
1070 p->subs[b].owner = towner;
1071 p->subs[b].inthreeway = tinthreeway;
1073 if (p->subs[a].owner)
1074 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].zfd);
1075 if (p->subs[b].owner)
1076 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].zfd);
1077 wakeup_sub(p, a, NULL);
1078 wakeup_sub(p, b, NULL);
1081 static int zt_open(char *fn)
1089 for (x = 0; x < strlen(fn); x++) {
1090 if (!isdigit(fn[x])) {
1098 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1101 fn = "/dev/zap/channel";
1103 fd = open(fn, O_RDWR | O_NONBLOCK);
1105 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1109 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1113 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1118 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) {
1119 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
1128 static void zt_close(int fd)
1134 static int zt_setlinear(int zfd, int linear)
1137 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1144 static int alloc_sub(struct zt_pvt *p, int x)
1148 if (p->subs[x].zfd < 0) {
1149 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1150 if (p->subs[x].zfd > -1) {
1151 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1153 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1154 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1155 bi.numbufs = numbufs;
1156 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1158 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1161 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1162 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1163 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1164 zt_close(p->subs[x].zfd);
1165 p->subs[x].zfd = -1;
1168 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1171 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1174 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1178 static int unalloc_sub(struct zt_pvt *p, int x)
1181 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1184 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1185 if (p->subs[x].zfd > -1) {
1186 zt_close(p->subs[x].zfd);
1188 p->subs[x].zfd = -1;
1189 p->subs[x].linear = 0;
1190 p->subs[x].chan = 0;
1191 p->subs[x].owner = NULL;
1192 p->subs[x].inthreeway = 0;
1193 p->polarity = POLARITY_IDLE;
1194 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1198 static int digit_to_dtmfindex(char digit)
1201 return ZT_TONE_DTMF_BASE + (digit - '0');
1202 else if (digit >= 'A' && digit <= 'D')
1203 return ZT_TONE_DTMF_A + (digit - 'A');
1204 else if (digit >= 'a' && digit <= 'd')
1205 return ZT_TONE_DTMF_A + (digit - 'a');
1206 else if (digit == '*')
1207 return ZT_TONE_DTMF_s;
1208 else if (digit == '#')
1209 return ZT_TONE_DTMF_p;
1214 static int zt_digit_begin(struct ast_channel *chan, char digit)
1220 pvt = chan->tech_pvt;
1222 ast_mutex_lock(&pvt->lock);
1224 index = zt_get_index(chan, pvt, 0);
1226 if ((index != SUB_REAL) || !pvt->owner)
1230 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1231 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1232 if (pvt->setup_ack) {
1233 if (!pri_grab(pvt, pvt->pri)) {
1234 pri_information(pvt->pri->pri, pvt->call, digit);
1237 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1238 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1240 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1241 res = strlen(pvt->dialdest);
1242 pvt->dialdest[res++] = digit;
1243 pvt->dialdest[res] = '\0';
1248 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1251 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1253 ZT_DIAL_OPERATION zo = {
1254 .op = ZT_DIAL_OP_APPEND,
1257 zo.dialstr[0] = 'T';
1258 zo.dialstr[1] = digit;
1259 zo.dialstr[2] = '\0';
1260 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1261 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1265 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1267 pvt->begindigit = digit;
1271 ast_mutex_unlock(&pvt->lock);
1276 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1283 pvt = chan->tech_pvt;
1285 ast_mutex_lock(&pvt->lock);
1287 index = zt_get_index(chan, pvt, 0);
1289 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1293 /* This means that the digit was already sent via PRI signalling */
1294 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1295 && !pvt->begindigit)
1299 if (pvt->begindigit) {
1301 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1302 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1304 pvt->begindigit = 0;
1308 ast_mutex_unlock(&pvt->lock);
1313 static char *events[] = {
1326 "Hook Transition Complete",
1331 "Polarity Reversal",
1339 { ZT_ALARM_RED, "Red Alarm" },
1340 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1341 { ZT_ALARM_BLUE, "Blue Alarm" },
1342 { ZT_ALARM_RECOVER, "Recovering" },
1343 { ZT_ALARM_LOOPBACK, "Loopback" },
1344 { ZT_ALARM_NOTOPEN, "Not Open" },
1345 { ZT_ALARM_NONE, "None" },
1348 static char *alarm2str(int alarm)
1351 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1352 if (alarms[x].alarm & alarm)
1353 return alarms[x].name;
1355 return alarm ? "Unknown Alarm" : "No Alarm";
1358 static char *event2str(int event)
1360 static char buf[256];
1361 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1362 return events[event];
1363 sprintf(buf, "Event %d", event); /* safe */
1368 static char *dialplan2str(int dialplan)
1370 if (dialplan == -1 || dialplan == -2) {
1371 return("Dynamically set dialplan in ISDN");
1373 return (pri_plan2str(dialplan));
1377 static char *zap_sig2str(int sig)
1379 static char buf[256];
1382 return "E & M Immediate";
1384 return "E & M Wink";
1388 return "Feature Group D (DTMF)";
1390 return "Feature Group D (MF)";
1391 case SIG_FEATDMF_TA:
1392 return "Feature Groud D (MF) Tandem Access";
1394 return "Feature Group B (MF)";
1398 return "FGC/CAMA (Dialpulse)";
1399 case SIG_FGC_CAMAMF:
1400 return "FGC/CAMA (MF)";
1402 return "FXS Loopstart";
1404 return "FXS Groundstart";
1406 return "FXS Kewlstart";
1408 return "FXO Loopstart";
1410 return "FXO Groundstart";
1412 return "FXO Kewlstart";
1416 return "ISDN BRI Point to Point";
1418 return "ISDN BRI Point to MultiPoint";
1422 return "SF (Tone) Immediate";
1424 return "SF (Tone) Wink";
1426 return "SF (Tone) with Feature Group D (DTMF)";
1427 case SIG_SF_FEATDMF:
1428 return "SF (Tone) with Feature Group D (MF)";
1430 return "SF (Tone) with Feature Group B (MF)";
1431 case SIG_GR303FXOKS:
1432 return "GR-303 with FXOKS";
1433 case SIG_GR303FXSKS:
1434 return "GR-303 with FXSKS";
1438 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1443 #define sig2str zap_sig2str
1445 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1447 /* If the conference already exists, and we're already in it
1448 don't bother doing anything */
1451 memset(&zi, 0, sizeof(zi));
1454 if (slavechannel > 0) {
1455 /* If we have only one slave, do a digital mon */
1456 zi.confmode = ZT_CONF_DIGITALMON;
1457 zi.confno = slavechannel;
1460 /* Real-side and pseudo-side both participate in conference */
1461 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1462 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1464 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1465 zi.confno = p->confno;
1467 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1471 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1472 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1475 if (slavechannel < 1) {
1476 p->confno = zi.confno;
1478 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1479 ast_debug(1, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1483 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1485 /* If they're listening to our channel, they're ours */
1486 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1488 /* If they're a talker on our (allocated) conference, they're ours */
1489 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1494 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1497 if (/* Can't delete if there's no zfd */
1499 /* Don't delete from the conference if it's not our conference */
1501 /* Don't delete if we don't think it's conferenced at all (implied) */
1503 memset(&zi, 0, sizeof(zi));
1507 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1508 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1511 ast_debug(1, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1512 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1516 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1520 struct zt_pvt *slave = NULL;
1521 /* Start out optimistic */
1523 /* Update conference state in a stateless fashion */
1524 for (x = 0; x < 3; x++) {
1525 /* Any three-way calling makes slave native mode *definitely* out
1527 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1530 /* If we don't have any 3-way calls, check to see if we have
1531 precisely one slave */
1532 if (useslavenative) {
1533 for (x = 0; x < MAX_SLAVES; x++) {
1536 /* Whoops already have a slave! No
1537 slave native and stop right away */
1542 /* We have one slave so far */
1543 slave = p->slaves[x];
1548 /* If no slave, slave native definitely out */
1551 else if (slave->law != p->law) {
1557 return useslavenative;
1560 static int reset_conf(struct zt_pvt *p)
1563 memset(&zi, 0, sizeof(zi));
1565 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1566 if (p->subs[SUB_REAL].zfd > -1) {
1567 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1568 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1573 static int update_conf(struct zt_pvt *p)
1578 struct zt_pvt *slave = NULL;
1580 useslavenative = isslavenative(p, &slave);
1581 /* Start with the obvious, general stuff */
1582 for (x = 0; x < 3; x++) {
1583 /* Look for three way calls */
1584 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1585 conf_add(p, &p->subs[x], x, 0);
1588 conf_del(p, &p->subs[x], x);
1591 /* If we have a slave, add him to our conference now. or DAX
1592 if this is slave native */
1593 for (x = 0; x < MAX_SLAVES; x++) {
1596 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1598 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1603 /* If we're supposed to be in there, do so now */
1604 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1606 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1608 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1612 /* If we have a master, add ourselves to his conference */
1614 if (isslavenative(p->master, NULL)) {
1615 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1617 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1621 /* Nobody is left (or should be left) in our conference.
1625 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1629 static void zt_enable_ec(struct zt_pvt *p)
1636 ast_debug(1, "Echo cancellation already on\n");
1640 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1643 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
1644 if (p->echocancel.head.tap_length) {
1646 if (p->echocancel) {
1648 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1650 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1652 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n", p->channel, strerror(errno));
1654 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
1655 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL_PARAMS, &p->echocancel);
1658 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1661 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d (%s)\n", p->channel, strerror(errno));
1664 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1667 ast_debug(1, "No echo cancellation requested\n");
1670 static void zt_train_ec(struct zt_pvt *p)
1675 if (p && p->echocanon && p->echotraining) {
1676 x = p->echotraining;
1677 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1679 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1681 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1683 ast_debug(1, "No echo training requested\n");
1687 static void zt_disable_ec(struct zt_pvt *p)
1692 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
1693 struct zt_echocanparams ecp = { .tap_length = 0 };
1695 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL_PARAMS, &ecp);
1699 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1703 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1705 ast_debug(1, "Disabled echo cancellation on channel %d\n", p->channel);
1711 static void fill_txgain(struct zt_gains *g, float gain, int law)
1715 float linear_gain = pow(10.0, gain / 20.0);
1719 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1721 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1722 if (k > 32767) k = 32767;
1723 if (k < -32767) k = -32767;
1724 g->txgain[j] = AST_LIN2A(k);
1731 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1733 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1734 if (k > 32767) k = 32767;
1735 if (k < -32767) k = -32767;
1736 g->txgain[j] = AST_LIN2MU(k);
1745 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1749 float linear_gain = pow(10.0, gain / 20.0);
1753 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1755 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1756 if (k > 32767) k = 32767;
1757 if (k < -32767) k = -32767;
1758 g->rxgain[j] = AST_LIN2A(k);
1765 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1767 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1768 if (k > 32767) k = 32767;
1769 if (k < -32767) k = -32767;
1770 g->rxgain[j] = AST_LIN2MU(k);
1779 static int set_actual_txgain(int fd, int chan, float gain, int law)
1784 memset(&g, 0, sizeof(g));
1786 res = ioctl(fd, ZT_GETGAINS, &g);
1788 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1792 fill_txgain(&g, gain, law);
1794 return ioctl(fd, ZT_SETGAINS, &g);
1797 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1802 memset(&g, 0, sizeof(g));
1804 res = ioctl(fd, ZT_GETGAINS, &g);
1806 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1810 fill_rxgain(&g, gain, law);
1812 return ioctl(fd, ZT_SETGAINS, &g);
1815 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1817 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1820 static int bump_gains(struct zt_pvt *p)
1824 /* Bump receive gain by value stored in cid_rxgain */
1825 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1827 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1834 static int restore_gains(struct zt_pvt *p)
1838 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1840 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1847 static inline int zt_set_hook(int fd, int hs)
1852 res = ioctl(fd, ZT_HOOK, &x);
1855 if (errno == EINPROGRESS)
1857 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1863 static inline int zt_confmute(struct zt_pvt *p, int muted)
1867 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
1869 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1871 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1873 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1875 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1879 static int save_conference(struct zt_pvt *p)
1881 struct zt_confinfo c;
1883 if (p->saveconf.confmode) {
1884 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1887 p->saveconf.chan = 0;
1888 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1890 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1891 p->saveconf.confmode = 0;
1896 c.confmode = ZT_CONF_NORMAL;
1897 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1899 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1902 ast_debug(1, "Disabled conferencing\n");
1907 * \brief Send MWI state change
1909 * \arg mailbox_full This is the mailbox associated with the FXO line that the
1910 * MWI state has changed on.
1911 * \arg thereornot This argument should simply be set to 1 or 0, to indicate
1912 * whether there are messages waiting or not.
1916 * This function does two things:
1918 * 1) It generates an internal Asterisk event notifying any other module that
1919 * cares about MWI that the state of a mailbox has changed.
1921 * 2) It runs the script specified by the mwimonitornotify option to allow
1922 * some custom handling of the state change.
1924 static void notify_message(char *mailbox_full, int thereornot)
1926 char s[sizeof(mwimonitornotify) + 80];
1927 struct ast_event *event;
1928 char *mailbox, *context;
1930 /* Strip off @default */
1931 context = mailbox = ast_strdupa(mailbox_full);
1932 strsep(&context, "@");
1933 if (ast_strlen_zero(context))
1934 context = "default";
1936 if (!(event = ast_event_new(AST_EVENT_MWI,
1937 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1938 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1939 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1940 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1941 AST_EVENT_IE_END))) {
1945 ast_event_queue_and_cache(event,
1946 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
1947 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR,
1950 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
1951 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
1956 static int restore_conference(struct zt_pvt *p)
1959 if (p->saveconf.confmode) {
1960 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1961 p->saveconf.confmode = 0;
1963 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1967 ast_debug(1, "Restored conferencing\n");
1971 static int send_callerid(struct zt_pvt *p);
1973 static int send_cwcidspill(struct zt_pvt *p)
1977 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1979 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1980 /* Make sure we account for the end */
1981 p->cidlen += READ_SIZE * 4;
1984 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1988 static int has_voicemail(struct zt_pvt *p)
1991 struct ast_event *event;
1992 char *mailbox, *context;
1994 mailbox = context = ast_strdupa(p->mailbox);
1995 strsep(&context, "@");
1996 if (ast_strlen_zero(context))
1997 context = "default";
1999 event = ast_event_get_cached(AST_EVENT_MWI,
2000 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
2001 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
2002 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
2006 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
2007 ast_event_destroy(event);
2009 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
2014 static int send_callerid(struct zt_pvt *p)
2016 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
2018 /* Take out of linear mode if necessary */
2019 if (p->subs[SUB_REAL].linear) {
2020 p->subs[SUB_REAL].linear = 0;
2021 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
2023 while (p->cidpos < p->cidlen) {
2024 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
2026 if (errno == EAGAIN)
2029 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
2037 ast_free(p->cidspill);
2039 if (p->callwaitcas) {
2040 /* Wait for CID/CW to expire */
2041 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
2043 restore_conference(p);
2047 static int zt_callwait(struct ast_channel *ast)
2049 struct zt_pvt *p = ast->tech_pvt;
2050 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
2052 ast_log(LOG_WARNING, "Spill already exists?!?\n");
2053 ast_free(p->cidspill);
2055 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
2059 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
2060 if (!p->callwaitrings && p->callwaitingcallerid) {
2061 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
2063 p->cidlen = 2400 + 680 + READ_SIZE * 4;
2065 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
2067 p->cidlen = 2400 + READ_SIZE * 4;
2076 static unsigned char cid_pres2ss7pres(int cid_pres)
2078 return (cid_pres >> 5) & 0x03;
2081 static unsigned char cid_pres2ss7screen(int cid_pres)
2083 return cid_pres & 0x03;
2087 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
2089 struct zt_pvt *p = ast->tech_pvt;
2090 int x, res, index,mysig;
2095 char dest[256]; /* must be same length as p->dialdest */
2096 ast_mutex_lock(&p->lock);
2097 ast_copy_string(dest, rdest, sizeof(dest));
2098 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
2099 if ((ast->_state == AST_STATE_BUSY)) {
2100 p->subs[SUB_REAL].needbusy = 1;
2101 ast_mutex_unlock(&p->lock);
2104 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
2105 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
2106 ast_mutex_unlock(&p->lock);
2110 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
2112 /* Special pseudo -- automatically up */
2113 ast_setstate(ast, AST_STATE_UP);
2114 ast_mutex_unlock(&p->lock);
2117 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
2118 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
2120 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
2123 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
2126 if (p->outsigmod > -1)
2127 mysig = p->outsigmod;
2133 if (p->owner == ast) {
2134 /* Normal ring, on hook */
2136 /* Don't send audio while on hook, until the call is answered */
2138 if (p->use_callerid) {
2139 /* Generate the Caller-ID spill if desired */
2141 ast_log(LOG_WARNING, "cidspill already exists??\n");
2142 ast_free(p->cidspill);
2145 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2146 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2151 /* Choose proper cadence */
2152 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2153 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
2154 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
2155 p->cidrings = cidrings[p->distinctivering - 1];
2157 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
2158 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
2159 p->cidrings = p->sendcalleridafter;
2162 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2163 c = strchr(dest, '/');
2166 if (c && (strlen(c) < p->stripmsd)) {
2167 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2171 p->dop.op = ZT_DIAL_OP_REPLACE;
2172 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2173 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2175 p->dop.dialstr[0] = '\0';
2178 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2179 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2180 ast_mutex_unlock(&p->lock);
2185 /* Call waiting call */
2186 p->callwaitrings = 0;
2187 if (ast->cid.cid_num)
2188 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2190 p->callwait_num[0] = '\0';
2191 if (ast->cid.cid_name)
2192 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2194 p->callwait_name[0] = '\0';
2195 /* Call waiting tone instead */
2196 if (zt_callwait(ast)) {
2197 ast_mutex_unlock(&p->lock);
2200 /* Make ring-back */
2201 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2202 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2205 n = ast->cid.cid_name;
2206 l = ast->cid.cid_num;
2208 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2210 p->lastcid_num[0] = '\0';
2212 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2214 p->lastcid_name[0] = '\0';
2215 ast_setstate(ast, AST_STATE_RINGING);
2216 index = zt_get_index(ast, p, 0);
2218 p->subs[index].needringing = 1;
2231 case SIG_FGC_CAMAMF:
2236 case SIG_SF_FEATDMF:
2237 case SIG_FEATDMF_TA:
2239 c = strchr(dest, '/');
2244 if (strlen(c) < p->stripmsd) {
2245 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2246 ast_mutex_unlock(&p->lock);
2250 /* Start the trunk, if not GR-303 */
2254 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2256 if (errno != EINPROGRESS) {
2257 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2258 ast_mutex_unlock(&p->lock);
2265 ast_debug(1, "Dialing '%s'\n", c);
2266 p->dop.op = ZT_DIAL_OP_REPLACE;
2272 l = ast->cid.cid_num;
2274 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2276 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2279 l = ast->cid.cid_num;
2281 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2283 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2285 case SIG_FEATDMF_TA:
2287 const char *cic, *ozz;
2289 /* If you have to go through a Tandem Access point you need to use this */
2290 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2293 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2297 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2298 ast_mutex_unlock(&p->lock);
2301 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2302 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2307 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2310 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2312 case SIG_FGC_CAMAMF:
2314 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2318 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2320 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2324 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2325 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2326 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2327 p->echorest[sizeof(p->echorest) - 1] = '\0';
2329 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2333 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2335 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2336 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2337 ast_mutex_unlock(&p->lock);
2341 ast_debug(1, "Deferring dialing...\n");
2344 if (ast_strlen_zero(c))
2346 ast_setstate(ast, AST_STATE_DIALING);
2349 /* Special pseudo -- automatically up*/
2350 ast_setstate(ast, AST_STATE_UP);
2356 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2357 p->dialdest[0] = '\0';
2360 ast_debug(1, "not yet implemented\n");
2361 ast_mutex_unlock(&p->lock);
2366 char ss7_called_nai;
2367 int called_nai_strip;
2368 char ss7_calling_nai;
2369 int calling_nai_strip;
2370 const char *charge_str = NULL;
2371 const char *gen_address = NULL;
2372 const char *gen_digits = NULL;
2373 const char *gen_dig_type = NULL;
2374 const char *gen_dig_scheme = NULL;
2375 const char *jip_digits = NULL;
2376 const char *lspi_ident = NULL;
2377 const char *rlt_flag = NULL;
2378 const char *call_ref_id = NULL;
2379 const char *call_ref_pc = NULL;
2381 c = strchr(dest, '/');
2387 if (!p->hidecallerid) {
2388 l = ast->cid.cid_num;
2393 if (ss7_grab(p, p->ss7)) {
2394 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2395 ast_mutex_unlock(&p->lock);
2398 p->digital = IS_DIGITAL(ast->transfercapability);
2399 p->ss7call = isup_new_call(p->ss7->ss7);
2403 ast_mutex_unlock(&p->lock);
2404 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2408 called_nai_strip = 0;
2409 ss7_called_nai = p->ss7->called_nai;
2410 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2411 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2412 called_nai_strip = strlen(p->ss7->internationalprefix);
2413 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2414 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2415 called_nai_strip = strlen(p->ss7->nationalprefix);
2416 ss7_called_nai = SS7_NAI_NATIONAL;
2418 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2421 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2423 calling_nai_strip = 0;
2424 ss7_calling_nai = p->ss7->calling_nai;
2425 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2426 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2427 calling_nai_strip = strlen(p->ss7->internationalprefix);
2428 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2429 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2430 calling_nai_strip = strlen(p->ss7->nationalprefix);
2431 ss7_calling_nai = SS7_NAI_NATIONAL;
2433 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2436 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2437 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2438 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2440 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
2441 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2443 /* Set the charge number if it is set */
2444 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
2446 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
2448 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
2450 isup_set_gen_address(p->ss7call, gen_address, p->gen_add_nai,p->gen_add_pres_ind, p->gen_add_num_plan,p->gen_add_type); /* need to add some types here for NAI,PRES,TYPE */
2452 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
2453 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
2454 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
2456 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
2458 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
2460 isup_set_jip_digits(p->ss7call, jip_digits);
2462 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
2464 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
2466 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
2467 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 ))
2468 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
2470 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
2471 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
2473 isup_set_callref(p->ss7call, atoi(call_ref_id),
2474 call_ref_pc ? atoi(call_ref_pc) : 0);
2477 isup_iam(p->ss7->ss7, p->ss7call);
2478 ast_setstate(ast, AST_STATE_DIALING);
2481 #endif /* HAVE_SS7 */
2485 #ifdef SUPPORT_USERUSER
2486 const char *useruser;
2490 int prilocaldialplan;
2494 int redirect_reason;
2496 c = strchr(dest, '/');
2505 if (!p->hidecallerid) {
2506 l = ast->cid.cid_num;
2507 if (!p->hidecalleridname) {
2508 n = ast->cid.cid_name;
2512 if (strlen(c) < p->stripmsd) {
2513 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2514 ast_mutex_unlock(&p->lock);
2517 if (mysig != SIG_FXSKS) {
2518 p->dop.op = ZT_DIAL_OP_REPLACE;
2519 s = strchr(c + p->stripmsd, 'w');
2522 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2524 p->dop.dialstr[0] = '\0';
2527 p->dop.dialstr[0] = '\0';
2530 if (pri_grab(p, p->pri)) {
2531 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2532 ast_mutex_unlock(&p->lock);
2535 if (!(p->call = pri_new_call(p->pri->pri))) {
2536 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2538 ast_mutex_unlock(&p->lock);
2541 if (!(sr = pri_sr_new())) {
2542 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2544 ast_mutex_unlock(&p->lock);
2546 if (p->bearer || (mysig == SIG_FXSKS)) {
2548 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2549 p->bearer->call = p->call;
2551 ast_debug(1, "I'm being setup with no bearer right now...\n");
2553 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2555 p->digital = IS_DIGITAL(ast->transfercapability);
2556 /* Add support for exclusive override */
2557 if (p->priexclusive)
2560 /* otherwise, traditional behavior */
2561 if (p->pri->nodetype == PRI_NETWORK)
2567 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2568 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2570 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2571 if (p->pri->facilityenable)
2572 pri_facility_enable(p->pri->pri);
2574 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2576 pridialplan = p->pri->dialplan - 1;
2577 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2578 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2579 if (pridialplan == -2) {
2580 dp_strip = strlen(p->pri->internationalprefix);
2582 pridialplan = PRI_INTERNATIONAL_ISDN;
2583 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2584 if (pridialplan == -2) {
2585 dp_strip = strlen(p->pri->nationalprefix);
2587 pridialplan = PRI_NATIONAL_ISDN;
2589 pridialplan = PRI_LOCAL_ISDN;
2592 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2593 switch (c[p->stripmsd]) {
2595 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2598 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2601 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2604 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2607 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2610 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2613 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2616 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2619 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2622 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2625 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2628 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2631 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2634 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2638 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2642 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2645 prilocaldialplan = p->pri->localdialplan - 1;
2646 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2647 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2648 if (prilocaldialplan == -2) {
2649 ldp_strip = strlen(p->pri->internationalprefix);
2651 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2652 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2653 if (prilocaldialplan == -2) {
2654 ldp_strip = strlen(p->pri->nationalprefix);
2656 prilocaldialplan = PRI_NATIONAL_ISDN;
2658 prilocaldialplan = PRI_LOCAL_ISDN;
2662 while (*l > '9' && *l != '*' && *l != '#') {
2665 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2668 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2671 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2674 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2677 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2680 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2683 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2686 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2689 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2692 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2695 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2698 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2701 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2704 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2708 ast_log(LOG_WARNING, "Unrecognized prilocaldialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2713 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2714 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2715 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2716 if (!strcasecmp(rr_str, "UNKNOWN"))
2717 redirect_reason = 0;
2718 else if (!strcasecmp(rr_str, "BUSY"))
2719 redirect_reason = 1;
2720 else if (!strcasecmp(rr_str, "NO_REPLY"))
2721 redirect_reason = 2;
2722 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2723 redirect_reason = 15;
2725 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2727 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2728 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2730 #ifdef SUPPORT_USERUSER
2731 /* User-user info */
2732 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2735 pri_sr_set_useruser(sr, useruser);
2738 if (pri_setup(p->pri->pri, p->call, sr)) {
2739 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2740 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2742 ast_mutex_unlock(&p->lock);
2747 ast_setstate(ast, AST_STATE_DIALING);
2751 ast_mutex_unlock(&p->lock);
2755 static void destroy_zt_pvt(struct zt_pvt **pvt)
2757 struct zt_pvt *p = *pvt;
2758 /* Remove channel from the list */
2760 p->prev->next = p->next;
2762 p->next->prev = p->prev;
2764 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2765 if (p->mwi_event_sub)
2766 ast_event_unsubscribe(p->mwi_event_sub);
2768 ast_variables_destroy(p->vars);
2769 ast_mutex_destroy(&p->lock);
2774 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2784 for (i = 0; i < 3; i++) {
2785 if (cur->subs[i].owner) {
2791 prev->next = cur->next;
2793 prev->next->prev = prev;
2799 iflist->prev = NULL;
2803 if (cur->subs[SUB_REAL].zfd > -1) {
2804 zt_close(cur->subs[SUB_REAL].zfd);
2806 destroy_zt_pvt(&cur);
2810 prev->next = cur->next;
2812 prev->next->prev = prev;
2818 iflist->prev = NULL;
2822 if (cur->subs[SUB_REAL].zfd > -1) {
2823 zt_close(cur->subs[SUB_REAL].zfd);
2825 destroy_zt_pvt(&cur);
2831 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2833 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2835 static char *zap_send_keypad_facility_descrip =
2836 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2837 " IE over the current channel.\n";
2839 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2841 /* Data will be our digit string */
2843 char *digits = (char *) data;
2845 if (ast_strlen_zero(digits)) {
2846 ast_debug(1, "No digit string sent to application!\n");
2850 p = (struct zt_pvt *)chan->tech_pvt;
2853 ast_debug(1, "Unable to find technology private\n");
2857 ast_mutex_lock(&p->lock);
2859 if (!p->pri || !p->call) {
2860 ast_debug(1, "Unable to find pri or call on channel!\n");
2861 ast_mutex_unlock(&p->lock);
2865 if (!pri_grab(p, p->pri)) {
2866 pri_keypad_facility(p->pri->pri, p->call, digits);
2869 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2870 ast_mutex_unlock(&p->lock);
2874 ast_mutex_unlock(&p->lock);
2879 static int pri_is_up(struct zt_pri *pri)
2882 for (x = 0; x < NUM_DCHANS; x++) {
2883 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2889 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2891 bearer->owner = &inuse;
2892 bearer->realcall = crv;
2893 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2894 if (crv->subs[SUB_REAL].owner)
2895 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].zfd);
2896 crv->bearer = bearer;
2897 crv->call = bearer->call;
2902 static char *pri_order(int level)
2912 return "Quaternary";
2918 /* Returns fd of the active dchan */
2919 static int pri_active_dchan_fd(struct zt_pri *pri)
2923 for (x = 0; x < NUM_DCHANS; x++) {
2924 if ((pri->dchans[x] == pri->pri))
2931 static int pri_find_dchan(struct zt_pri *pri)
2938 for (x = 0; x < NUM_DCHANS; x++) {
2939 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2941 if (pri->dchans[x] == old) {
2947 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2948 pri->dchannels[newslot]);
2950 if (old && (oldslot != newslot))
2951 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2952 pri->dchannels[oldslot], pri->dchannels[newslot]);
2953 pri->pri = pri->dchans[newslot];
2958 static int zt_hangup(struct ast_channel *ast)
2962 /*static int restore_gains(struct zt_pvt *p);*/
2963 struct zt_pvt *p = ast->tech_pvt;
2964 struct zt_pvt *tmp = NULL;
2965 struct zt_pvt *prev = NULL;
2968 ast_debug(1, "zt_hangup(%s)\n", ast->name);
2969 if (!ast->tech_pvt) {
2970 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2974 ast_mutex_lock(&p->lock);
2976 index = zt_get_index(ast, p, 1);
2978 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
2980 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2986 if (p->origcid_num) {
2987 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2988 ast_free(p->origcid_num);
2989 p->origcid_num = NULL;
2991 if (p->origcid_name) {
2992 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2993 ast_free(p->origcid_name);
2994 p->origcid_name = NULL;
2997 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
3001 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
3002 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
3006 /* Real channel, do some fixup */
3007 p->subs[index].owner = NULL;
3008 p->subs[index].needanswer = 0;
3009 p->subs[index].needflash = 0;
3010 p->subs[index].needringing = 0;
3011 p->subs[index].needbusy = 0;
3012 p->subs[index].needcongestion = 0;
3013 p->subs[index].linear = 0;
3014 p->subs[index].needcallerid = 0;
3015 p->polarity = POLARITY_IDLE;
3016 zt_setlinear(p->subs[index].zfd, 0);
3017 if (index == SUB_REAL) {
3018 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
3019 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
3020 if (p->subs[SUB_CALLWAIT].inthreeway) {
3021 /* We had flipped over to answer a callwait and now it's gone */
3022 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
3023 /* Move to the call-wait, but un-own us until they flip back. */
3024 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3025 unalloc_sub(p, SUB_CALLWAIT);
3028 /* The three way hung up, but we still have a call wait */
3029 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
3030 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3031 unalloc_sub(p, SUB_THREEWAY);
3032 if (p->subs[SUB_REAL].inthreeway) {
3033 /* This was part of a three way call. Immediately make way for
3035 ast_debug(1, "Call was complete, setting owner to former third call\n");
3036 p->owner = p->subs[SUB_REAL].owner;
3038 /* This call hasn't been completed yet... Set owner to NULL */
3039 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
3042 p->subs[SUB_REAL].inthreeway = 0;
3044 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
3045 /* Move to the call-wait and switch back to them. */
3046 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3047 unalloc_sub(p, SUB_CALLWAIT);
3048 p->owner = p->subs[SUB_REAL].owner;
3049 if (p->owner->_state != AST_STATE_UP)
3050 p->subs[SUB_REAL].needanswer = 1;
3051 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
3052 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
3053 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
3054 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3055 unalloc_sub(p, SUB_THREEWAY);
3056 if (p->subs[SUB_REAL].inthreeway) {
3057 /* This was part of a three way call. Immediately make way for
3059 ast_debug(1, "Call was complete, setting owner to former third call\n");
3060 p->owner = p->subs[SUB_REAL].owner;
3062 /* This call hasn't been completed yet... Set owner to NULL */
3063 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
3066 p->subs[SUB_REAL].inthreeway = 0;
3068 } else if (index == SUB_CALLWAIT) {
3069 /* Ditch the holding callwait call, and immediately make it availabe */
3070 if (p->subs[SUB_CALLWAIT].inthreeway) {
3071 /* This is actually part of a three way, placed on hold. Place the third part
3072 on music on hold now */
3073 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
3074 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
3075 S_OR(p->mohsuggest, NULL),
3076 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3078 p->subs[SUB_THREEWAY].inthreeway = 0;
3079 /* Make it the call wait now */
3080 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
3081 unalloc_sub(p, SUB_THREEWAY);
3083 unalloc_sub(p, SUB_CALLWAIT);
3084 } else if (index == SUB_THREEWAY) {
3085 if (p->subs[SUB_CALLWAIT].inthreeway) {
3086 /* The other party of the three way call is currently in a call-wait state.
3087 Start music on hold for them, and take the main guy out of the third call */
3088 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
3089 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
3090 S_OR(p->mohsuggest, NULL),
3091 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3093 p->subs[SUB_CALLWAIT].inthreeway = 0;
3095 p->subs[SUB_REAL].inthreeway = 0;
3096 /* If this was part of a three way call index, let us make
3097 another three way call */
3098 unalloc_sub(p, SUB_THREEWAY);
3100 /* This wasn't any sort of call, but how are we an index? */
3101 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
3105 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
3108 p->distinctivering = 0;
3109 p->confirmanswer = 0;
3115 p->onhooktime = time(NULL);
3116 #if defined(HAVE_PRI) || defined(HAVE_SS7)
3124 ast_dsp_free(p->dsp);
3128 law = ZT_LAW_DEFAULT;
3129 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
3131 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
3132 /* Perform low level hangup if no owner left */
3136 if (!ss7_grab(p, p->ss7)) {
3137 if (!p->alreadyhungup) {
3138 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
3139 int icause = ast->hangupcause ? ast->hangupcause : -1;
3143 icause = atoi(cause);
3145 isup_rel(p->ss7->ss7, p->ss7call, icause);
3147 p->alreadyhungup = 1;
3149 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
3151 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
3159 #ifdef SUPPORT_USERUSER
3160 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
3163 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
3164 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
3165 if (!pri_grab(p, p->pri)) {
3166 if (p->alreadyhungup) {
3167 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
3169 #ifdef SUPPORT_USERUSER
3170 pri_call_set_useruser(p->call, useruser);
3173 pri_hangup(p->pri->pri, p->call, -1);
3176 p->bearer->call = NULL;
3178 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
3179 int icause = ast->hangupcause ? ast->hangupcause : -1;
3180 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
3182 #ifdef SUPPORT_USERUSER
3183 pri_call_set_useruser(p->call, useruser);
3186 p->alreadyhungup = 1;
3188 p->bearer->alreadyhungup = 1;
3191 icause = atoi(cause);
3193 pri_hangup(p->pri->pri, p->call, icause);
3196 ast_log(LOG_WARNING, "pri_disconnect failed\n");
3199 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3204 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
3210 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7) && (p->sig != SIG_BRI) && (p->sig != SIG_BRI_PTMP)))
3211 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
3213 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
3219 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
3222 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
3224 /* If they're off hook, try playing congestion */
3225 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
3226 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
3228 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3234 /* Make sure we're not made available for at least two seconds assuming
3235 we were actually used for an inbound or outbound call. */
3236 if (ast->_state != AST_STATE_RESERVED) {
3237 time(&p->guardtime);
3242 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3245 ast_free(p->cidspill);
3249 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
3250 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
3254 p->callwaiting = p->permcallwaiting;
3255 p->hidecallerid = p->permhidecallerid;
3260 /* Restore data mode */
3261 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
3263 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
3267 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
3268 /* Free up the bearer channel as well, and
3269 don't use its file descriptor anymore */
3270 update_conf(p->bearer);
3271 reset_conf(p->bearer);
3272 p->bearer->owner = NULL;
3273 p->bearer->realcall = NULL;
3275 p->subs[SUB_REAL].zfd = -1;
3282 p->callwaitingrepeat = 0;
3285 ast->tech_pvt = NULL;
3286 ast_mutex_unlock(&p->lock);
3287 ast_module_unref(ast_module_info->self);
3288 ast_verb(3, "Hungup '%s'\n", ast->name);
3290 ast_mutex_lock(&iflock);
3296 destroy_channel(prev, tmp, 0);
3304 ast_mutex_unlock(&iflock);
3308 static int zt_answer(struct ast_channel *ast)
3310 struct zt_pvt *p = ast->tech_pvt;
3313 int oldstate = ast->_state;
3314 ast_setstate(ast, AST_STATE_UP);
3315 ast_mutex_lock(&p->lock);
3316 index = zt_get_index(ast, p, 0);
3319 /* nothing to do if a radio channel */
3320 if ((p->radio || (p->oprmode < 0))) {
3321 ast_mutex_unlock(&p->lock);
3335 case SIG_FEATDMF_TA:
3338 case SIG_FGC_CAMAMF:
3343 case SIG_SF_FEATDMF:
3348 /* Pick up the line */
3349 ast_debug(1, "Took %s off hook\n", ast->name);
3350 if (p->hanguponpolarityswitch) {
3351 p->polaritydelaytv = ast_tvnow();
3353 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
3354 tone_zone_play_tone(p->subs[index].zfd, -1);
3356 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
3357 if (oldstate == AST_STATE_RINGING) {
3358 ast_debug(1, "Finally swapping real and threeway\n");
3359 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3360 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3361 p->owner = p->subs[SUB_REAL].owner;
3364 if (p->sig & __ZT_SIG_FXS) {
3373 /* Send a pri acknowledge */
3374 if (!pri_grab(p, p->pri)) {
3376 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3379 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3386 if (!ss7_grab(p, p->ss7)) {
3388 res = isup_anm(p->ss7->ss7, p->ss7call);
3391 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3397 ast_mutex_unlock(&p->lock);
3400 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3403 ast_mutex_unlock(&p->lock);
3407 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3413 struct zt_pvt *p = chan->tech_pvt, *pp;
3414 struct oprmode *oprmode;
3417 /* all supported options require data */
3418 if (!data || (datalen < 1)) {
3424 case AST_OPTION_TXGAIN:
3425 scp = (signed char *) data;
3426 index = zt_get_index(chan, p, 0);
3428 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
3431 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
3432 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
3433 case AST_OPTION_RXGAIN:
3434 scp = (signed char *) data;
3435 index = zt_get_index(chan, p, 0);
3437 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
3440 ast_debug(1, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
3441 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
3442 case AST_OPTION_TONE_VERIFY:
3448 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);