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>
45 <depend>res_features</depend>
52 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
58 #include <sys/signal.h>
60 #include <sys/ioctl.h>
63 #include "asterisk/zapata.h"
73 #include "asterisk/lock.h"
74 #include "asterisk/channel.h"
75 #include "asterisk/config.h"
76 #include "asterisk/module.h"
77 #include "asterisk/pbx.h"
78 #include "asterisk/file.h"
79 #include "asterisk/ulaw.h"
80 #include "asterisk/alaw.h"
81 #include "asterisk/callerid.h"
82 #include "asterisk/adsi.h"
83 #include "asterisk/cli.h"
84 #include "asterisk/cdr.h"
85 #include "asterisk/features.h"
86 #include "asterisk/musiconhold.h"
87 #include "asterisk/say.h"
88 #include "asterisk/tdd.h"
89 #include "asterisk/app.h"
90 #include "asterisk/dsp.h"
91 #include "asterisk/astdb.h"
92 #include "asterisk/manager.h"
93 #include "asterisk/causes.h"
94 #include "asterisk/term.h"
95 #include "asterisk/utils.h"
96 #include "asterisk/transcap.h"
97 #include "asterisk/stringfields.h"
98 #include "asterisk/abstract_jb.h"
99 #include "asterisk/smdi.h"
100 #include "asterisk/astobj.h"
101 #include "asterisk/event.h"
103 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
105 #ifdef ZT_SPANINFO_HAS_LINECONFIG
106 static const char *lbostr[] = {
107 "0 db (CSU)/0-133 feet (DSX-1)",
108 "133-266 feet (DSX-1)",
109 "266-399 feet (DSX-1)",
110 "399-533 feet (DSX-1)",
111 "533-655 feet (DSX-1)",
118 /*! Global jitterbuffer configuration - by default, jb is disabled */
119 static struct ast_jb_conf default_jbconf =
123 .resync_threshold = -1,
126 static struct ast_jb_conf global_jbconf;
128 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_PRI) && !defined(ZT_SIG_HARDHDLC))
129 #error "Your zaptel is too old. Please update"
132 #ifndef ZT_TONEDETECT
133 /* Work around older code with no tone detect */
134 #define ZT_EVENT_DTMFDOWN 0
135 #define ZT_EVENT_DTMFUP 0
138 /* define this to send PRI user-user information elements */
139 #undef SUPPORT_USERUSER
142 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
143 * the user hangs up to reset the state machine so ring works properly.
144 * This is used to be able to support kewlstart by putting the zhone in
145 * groundstart mode since their forward disconnect supervision is entirely
146 * broken even though their documentation says it isn't and their support
147 * is entirely unwilling to provide any assistance with their channel banks
148 * even though their web site says they support their products for life.
150 /* #define ZHONE_HACK */
153 * Define if you want to check the hook state for an FXO (FXS signalled) interface
154 * before dialing on it. Certain FXO interfaces always think they're out of
155 * service with this method however.
157 /* #define ZAP_CHECK_HOOKSTATE */
159 /*! \brief Typically, how many rings before we should send Caller*ID */
160 #define DEFAULT_CIDRINGS 1
162 #define CHANNEL_PSEUDO -12
164 #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;
250 static struct ast_channel inuse;
251 #ifdef PRI_GETSET_TIMERS
252 static int pritimers[PRI_MAX_TIMERS];
254 static int pridebugfd = -1;
255 static char pridebugfilename[1024] = "";
258 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
259 static int firstdigittimeout = 16000;
261 /*! \brief How long to wait for following digits (FXO logic) */
262 static int gendigittimeout = 8000;
264 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
265 static int matchdigittimeout = 3000;
267 /*! \brief Protect the interface list (of zt_pvt's) */
268 AST_MUTEX_DEFINE_STATIC(iflock);
271 static int ifcount = 0;
274 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
277 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
278 when it's doing something critical. */
279 AST_MUTEX_DEFINE_STATIC(monlock);
281 /*! \brief This is the thread for the monitor which checks for input on the channels
282 which are not currently in use. */
283 static pthread_t monitor_thread = AST_PTHREADT_NULL;
285 static int restart_monitor(void);
287 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);
289 static int zt_sendtext(struct ast_channel *c, const char *text);
291 static void mwi_event_cb(const struct ast_event *event, void *userdata)
293 /* This module does not handle MWI in an event-based manner. However, it
294 * subscribes to MWI for each mailbox that is configured so that the core
295 * knows that we care about it. Then, chan_zap will get the MWI from the
296 * event cache instead of checking the mailbox directly. */
299 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
300 static inline int zt_get_event(int fd)
303 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
308 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
309 static inline int zt_wait_event(int fd)
312 i = ZT_IOMUX_SIGEVENT;
313 if (ioctl(fd, ZT_IOMUX, &i) == -1)
315 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
320 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
321 #define READ_SIZE 160
323 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
324 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
326 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
327 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
328 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
329 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
330 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
334 static int ringt_base = DEFAULT_RINGT;
338 #define LINKSTATE_INALARM (1 << 0)
339 #define LINKSTATE_STARTING (1 << 1)
340 #define LINKSTATE_UP (1 << 2)
341 #define LINKSTATE_DOWN (1 << 3)
343 #define SS7_NAI_DYNAMIC -1
346 pthread_t master; /*!< Thread of master */
350 int linkstate[NUM_DCHANS];
354 LINKSET_STATE_DOWN = 0,
357 char called_nai; /*!< Called Nature of Address Indicator */
358 char calling_nai; /*!< Calling Nature of Address Indicator */
359 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
360 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
361 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
362 char unknownprefix[20]; /*!< for unknown dialplans */
364 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
367 static struct zt_ss7 linksets[NUM_SPANS];
369 static int cur_ss7type = -1;
370 static int cur_linkset = -1;
371 static int cur_pointcode = -1;
372 static int cur_cicbeginswith = -1;
373 static int cur_adjpointcode = -1;
374 static int cur_networkindicator = -1;
375 static int cur_defaultdpc = -1;
376 #endif /* HAVE_SS7 */
380 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
381 #define PRI_CHANNEL(p) ((p) & 0xff)
382 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
383 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
386 pthread_t master; /*!< Thread of master */
387 ast_mutex_t lock; /*!< Mutex */
388 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
389 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
390 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
391 int minunused; /*!< Min # of channels to keep empty */
392 int minidle; /*!< Min # of "idling" calls to keep active */
393 int nodetype; /*!< Node type */
394 int switchtype; /*!< Type of switch to emulate */
395 int nsf; /*!< Network-Specific Facilities */
396 int dialplan; /*!< Dialing plan */
397 int localdialplan; /*!< Local dialing plan */
398 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
399 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
400 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
401 char privateprefix[20]; /*!< for private dialplans */
402 char unknownprefix[20]; /*!< for unknown dialplans */
403 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
404 int trunkgroup; /*!< What our trunkgroup is */
405 int mastertrunkgroup; /*!< What trunk group is our master */
406 int prilogicalspan; /*!< Logical span number within trunk group */
407 int numchans; /*!< Num of channels we represent */
408 int overlapdial; /*!< In overlap dialing mode */
409 int facilityenable; /*!< Enable facility IEs */
410 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
411 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
412 struct pri *pri; /*!< Currently active D-channel */
414 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
419 time_t lastreset; /*!< time when unused channels were last reset */
420 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
422 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
423 struct zt_pvt *crvs; /*!< Member CRV structs */
424 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
428 static struct zt_pri pris[NUM_SPANS];
431 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
433 #define DEFAULT_PRI_DEBUG 0
436 static inline void pri_rel(struct zt_pri *pri)
438 ast_mutex_unlock(&pri->lock);
442 /*! Shut up the compiler */
446 #define SUB_REAL 0 /*!< Active call */
447 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
448 #define SUB_THREEWAY 2 /*!< Three-way call */
450 /* Polarity states */
451 #define POLARITY_IDLE 0
452 #define POLARITY_REV 1
455 static struct zt_distRings drings;
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;
570 /* Channel state or unavilability flags */
571 unsigned int inservice:1;
572 unsigned int locallyblocked:1;
573 unsigned int remotelyblocked:1;
574 #if defined(HAVE_PRI) || defined(HAVE_SS7)
576 unsigned int alerting:1;
577 unsigned int alreadyhungup:1;
578 unsigned int isidlecall:1;
579 unsigned int proceeding:1;
580 unsigned int progress:1;
581 unsigned int resetting:1;
582 unsigned int setup_ack:1;
584 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
585 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
587 struct zt_distRings drings;
589 char context[AST_MAX_CONTEXT];
590 char defcontext[AST_MAX_CONTEXT];
591 char exten[AST_MAX_EXTENSION];
592 char language[MAX_LANGUAGE];
593 char mohinterpret[MAX_MUSICCLASS];
594 char mohsuggest[MAX_MUSICCLASS];
595 #if defined(PRI_ANI) || defined(HAVE_SS7)
596 char cid_ani[AST_MAX_EXTENSION];
599 char cid_num[AST_MAX_EXTENSION];
600 int cid_ton; /*!< Type Of Number (TON) */
601 char cid_name[AST_MAX_EXTENSION];
602 char lastcid_num[AST_MAX_EXTENSION];
603 char lastcid_name[AST_MAX_EXTENSION];
604 char *origcid_num; /*!< malloced original callerid */
605 char *origcid_name; /*!< malloced original callerid */
606 char callwait_num[AST_MAX_EXTENSION];
607 char callwait_name[AST_MAX_EXTENSION];
608 char rdnis[AST_MAX_EXTENSION];
609 char dnid[AST_MAX_EXTENSION];
612 int confno; /*!< Our conference */
613 int confusers; /*!< Who is using our conference */
614 int propconfno; /*!< Propagated conference number */
615 ast_group_t callgroup;
616 ast_group_t pickupgroup;
617 struct ast_variable *vars;
618 int channel; /*!< Channel Number or CRV */
619 int span; /*!< Span number */
620 time_t guardtime; /*!< Must wait this much time before using for new call */
621 int cid_signalling; /*!< CID signalling type bell202 or v23 */
622 int cid_start; /*!< CID start indicator, polarity or ring */
623 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
624 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
625 int cidcwexpire; /*!< When to expire our muting for CID/CW */
626 unsigned char *cidspill;
627 struct callerid_state *mwi_state;
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 {
729 char smdi_port[SMDI_MAX_FILENAME_LEN];
732 /** returns a new zt_chan_conf with default values (by-value) */
733 static struct zt_chan_conf zt_chan_conf_default(void) {
734 /* recall that if a field is not included here it is initialized
737 struct zt_chan_conf conf = {
741 .switchtype = PRI_SWITCH_NI2,
742 .dialplan = PRI_NATIONAL_ISDN + 1,
743 .localdialplan = PRI_NATIONAL_ISDN + 1,
749 .internationalprefix = "",
750 .nationalprefix = "",
759 .called_nai = SS7_NAI_NATIONAL,
760 .calling_nai = SS7_NAI_NATIONAL,
761 .internationalprefix = "",
762 .nationalprefix = "",
763 .subscriberprefix = "",
768 .context = "default",
771 .mohinterpret = "default",
775 .cid_signalling = CID_SIG_BELL,
776 .cid_start = CID_START_RING,
786 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
787 .echocancel.head.tap_length = 1,
799 .polarityonanswerdelay = 600,
801 .sendcalleridafter = DEFAULT_CIDRINGS
813 .smdi_port = "/dev/ttyS0",
820 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
821 static int zt_digit_begin(struct ast_channel *ast, char digit);
822 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
823 static int zt_sendtext(struct ast_channel *c, const char *text);
824 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
825 static int zt_hangup(struct ast_channel *ast);
826 static int zt_answer(struct ast_channel *ast);
827 static struct ast_frame *zt_read(struct ast_channel *ast);
828 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
829 static struct ast_frame *zt_exception(struct ast_channel *ast);
830 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
831 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
832 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
833 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
835 static const struct ast_channel_tech zap_tech = {
837 .description = tdesc,
838 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
839 .requester = zt_request,
840 .send_digit_begin = zt_digit_begin,
841 .send_digit_end = zt_digit_end,
842 .send_text = zt_sendtext,
849 .exception = zt_exception,
850 .indicate = zt_indicate,
852 .setoption = zt_setoption,
853 .func_channel_read = zt_func_read,
857 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
859 #define GET_CHANNEL(p) ((p)->channel)
862 struct zt_pvt *round_robin[32];
865 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
868 /* Grab the lock first */
870 res = ast_mutex_trylock(&pri->lock);
872 ast_mutex_unlock(&pvt->lock);
873 /* Release the lock and try again */
875 ast_mutex_lock(&pvt->lock);
878 /* Then break the poll */
879 pthread_kill(pri->master, SIGURG);
885 static inline void ss7_rel(struct zt_ss7 *ss7)
887 ast_mutex_unlock(&ss7->lock);
890 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
893 /* Grab the lock first */
895 res = ast_mutex_trylock(&pri->lock);
897 ast_mutex_unlock(&pvt->lock);
898 /* Release the lock and try again */
900 ast_mutex_lock(&pvt->lock);
903 /* Then break the poll */
904 pthread_kill(pri->master, SIGURG);
908 #define NUM_CADENCE_MAX 25
909 static int num_cadence = 4;
910 static int user_has_defined_cadences = 0;
912 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
913 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
914 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
915 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
916 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
919 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
920 * is 1, the second pause is 2 and so on.
923 static int cidrings[NUM_CADENCE_MAX] = {
924 2, /*!< Right after first long ring */
925 4, /*!< Right after long part */
926 3, /*!< After third chirp */
927 2, /*!< Second spell */
930 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
931 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
933 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
934 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
936 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
939 if (p->subs[0].owner == ast)
941 else if (p->subs[1].owner == ast)
943 else if (p->subs[2].owner == ast)
948 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
954 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
956 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
961 ast_mutex_unlock(&pri->lock);
964 if (p->subs[a].owner) {
965 if (ast_channel_trylock(p->subs[a].owner)) {
966 ast_mutex_unlock(&p->lock);
968 ast_mutex_lock(&p->lock);
970 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
971 ast_channel_unlock(p->subs[a].owner);
979 ast_mutex_lock(&pri->lock);
983 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
986 struct zt_pri *pri = (struct zt_pri*) data;
989 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
991 /* We must unlock the PRI to avoid the possibility of a deadlock */
992 #if defined(HAVE_PRI) || defined(HAVE_SS7)
999 ast_mutex_unlock(&pri->lock);
1004 ast_mutex_unlock(&ss7->lock);
1014 if (ast_channel_trylock(p->owner)) {
1015 ast_mutex_unlock(&p->lock);
1017 ast_mutex_lock(&p->lock);
1019 ast_queue_frame(p->owner, f);
1020 ast_channel_unlock(p->owner);
1026 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1033 ast_mutex_lock(&pri->lock);
1038 ast_mutex_lock(&ss7->lock);
1049 static int restore_gains(struct zt_pvt *p);
1051 static void swap_subs(struct zt_pvt *p, int a, int b)
1055 struct ast_channel *towner;
1057 ast_debug(1, "Swapping %d and %d\n", a, b);
1059 tchan = p->subs[a].chan;
1060 towner = p->subs[a].owner;
1061 tinthreeway = p->subs[a].inthreeway;
1063 p->subs[a].chan = p->subs[b].chan;
1064 p->subs[a].owner = p->subs[b].owner;
1065 p->subs[a].inthreeway = p->subs[b].inthreeway;
1067 p->subs[b].chan = tchan;
1068 p->subs[b].owner = towner;
1069 p->subs[b].inthreeway = tinthreeway;
1071 if (p->subs[a].owner)
1072 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].zfd);
1073 if (p->subs[b].owner)
1074 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].zfd);
1075 wakeup_sub(p, a, NULL);
1076 wakeup_sub(p, b, NULL);
1079 static int zt_open(char *fn)
1087 for (x = 0; x < strlen(fn); x++) {
1088 if (!isdigit(fn[x])) {
1096 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1099 fn = "/dev/zap/channel";
1101 fd = open(fn, O_RDWR | O_NONBLOCK);
1103 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1107 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1111 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1116 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) {
1117 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
1126 static void zt_close(int fd)
1132 static int zt_setlinear(int zfd, int linear)
1135 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1142 static int alloc_sub(struct zt_pvt *p, int x)
1146 if (p->subs[x].zfd < 0) {
1147 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1148 if (p->subs[x].zfd > -1) {
1149 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1151 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1152 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1153 bi.numbufs = numbufs;
1154 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1156 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1159 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1160 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1161 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1162 zt_close(p->subs[x].zfd);
1163 p->subs[x].zfd = -1;
1166 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1169 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1172 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1176 static int unalloc_sub(struct zt_pvt *p, int x)
1179 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1182 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1183 if (p->subs[x].zfd > -1) {
1184 zt_close(p->subs[x].zfd);
1186 p->subs[x].zfd = -1;
1187 p->subs[x].linear = 0;
1188 p->subs[x].chan = 0;
1189 p->subs[x].owner = NULL;
1190 p->subs[x].inthreeway = 0;
1191 p->polarity = POLARITY_IDLE;
1192 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1196 static int digit_to_dtmfindex(char digit)
1199 return ZT_TONE_DTMF_BASE + (digit - '0');
1200 else if (digit >= 'A' && digit <= 'D')
1201 return ZT_TONE_DTMF_A + (digit - 'A');
1202 else if (digit >= 'a' && digit <= 'd')
1203 return ZT_TONE_DTMF_A + (digit - 'a');
1204 else if (digit == '*')
1205 return ZT_TONE_DTMF_s;
1206 else if (digit == '#')
1207 return ZT_TONE_DTMF_p;
1212 static int zt_digit_begin(struct ast_channel *chan, char digit)
1218 pvt = chan->tech_pvt;
1220 ast_mutex_lock(&pvt->lock);
1222 index = zt_get_index(chan, pvt, 0);
1224 if ((index != SUB_REAL) || !pvt->owner)
1228 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1229 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1230 if (pvt->setup_ack) {
1231 if (!pri_grab(pvt, pvt->pri)) {
1232 pri_information(pvt->pri->pri, pvt->call, digit);
1235 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1236 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1238 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1239 res = strlen(pvt->dialdest);
1240 pvt->dialdest[res++] = digit;
1241 pvt->dialdest[res] = '\0';
1246 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1249 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1251 ZT_DIAL_OPERATION zo = {
1252 .op = ZT_DIAL_OP_APPEND,
1255 zo.dialstr[0] = 'T';
1256 zo.dialstr[1] = digit;
1257 zo.dialstr[2] = '\0';
1258 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1259 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1263 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1265 pvt->begindigit = digit;
1269 ast_mutex_unlock(&pvt->lock);
1274 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1281 pvt = chan->tech_pvt;
1283 ast_mutex_lock(&pvt->lock);
1285 index = zt_get_index(chan, pvt, 0);
1287 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1291 /* This means that the digit was already sent via PRI signalling */
1292 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1293 && !pvt->begindigit)
1297 if (pvt->begindigit) {
1299 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1300 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1302 pvt->begindigit = 0;
1306 ast_mutex_unlock(&pvt->lock);
1311 static char *events[] = {
1324 "Hook Transition Complete",
1329 "Polarity Reversal",
1337 { ZT_ALARM_RED, "Red Alarm" },
1338 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1339 { ZT_ALARM_BLUE, "Blue Alarm" },
1340 { ZT_ALARM_RECOVER, "Recovering" },
1341 { ZT_ALARM_LOOPBACK, "Loopback" },
1342 { ZT_ALARM_NOTOPEN, "Not Open" },
1343 { ZT_ALARM_NONE, "None" },
1346 static char *alarm2str(int alarm)
1349 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1350 if (alarms[x].alarm & alarm)
1351 return alarms[x].name;
1353 return alarm ? "Unknown Alarm" : "No Alarm";
1356 static char *event2str(int event)
1358 static char buf[256];
1359 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1360 return events[event];
1361 sprintf(buf, "Event %d", event); /* safe */
1366 static char *dialplan2str(int dialplan)
1368 if (dialplan == -1 || dialplan == -2) {
1369 return("Dynamically set dialplan in ISDN");
1371 return (pri_plan2str(dialplan));
1375 static char *zap_sig2str(int sig)
1377 static char buf[256];
1380 return "E & M Immediate";
1382 return "E & M Wink";
1386 return "Feature Group D (DTMF)";
1388 return "Feature Group D (MF)";
1389 case SIG_FEATDMF_TA:
1390 return "Feature Groud D (MF) Tandem Access";
1392 return "Feature Group B (MF)";
1396 return "FGC/CAMA (Dialpulse)";
1397 case SIG_FGC_CAMAMF:
1398 return "FGC/CAMA (MF)";
1400 return "FXS Loopstart";
1402 return "FXS Groundstart";
1404 return "FXS Kewlstart";
1406 return "FXO Loopstart";
1408 return "FXO Groundstart";
1410 return "FXO Kewlstart";
1414 return "ISDN BRI Point to Point";
1416 return "ISDN BRI Point to MultiPoint";
1420 return "SF (Tone) Immediate";
1422 return "SF (Tone) Wink";
1424 return "SF (Tone) with Feature Group D (DTMF)";
1425 case SIG_SF_FEATDMF:
1426 return "SF (Tone) with Feature Group D (MF)";
1428 return "SF (Tone) with Feature Group B (MF)";
1429 case SIG_GR303FXOKS:
1430 return "GR-303 with FXOKS";
1431 case SIG_GR303FXSKS:
1432 return "GR-303 with FXSKS";
1436 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1441 #define sig2str zap_sig2str
1443 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1445 /* If the conference already exists, and we're already in it
1446 don't bother doing anything */
1449 memset(&zi, 0, sizeof(zi));
1452 if (slavechannel > 0) {
1453 /* If we have only one slave, do a digital mon */
1454 zi.confmode = ZT_CONF_DIGITALMON;
1455 zi.confno = slavechannel;
1458 /* Real-side and pseudo-side both participate in conference */
1459 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1460 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1462 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1463 zi.confno = p->confno;
1465 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1469 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1470 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1473 if (slavechannel < 1) {
1474 p->confno = zi.confno;
1476 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1477 ast_debug(1, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1481 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1483 /* If they're listening to our channel, they're ours */
1484 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1486 /* If they're a talker on our (allocated) conference, they're ours */
1487 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1492 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1495 if (/* Can't delete if there's no zfd */
1497 /* Don't delete from the conference if it's not our conference */
1499 /* Don't delete if we don't think it's conferenced at all (implied) */
1501 memset(&zi, 0, sizeof(zi));
1505 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1506 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1509 ast_debug(1, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1510 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1514 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1518 struct zt_pvt *slave = NULL;
1519 /* Start out optimistic */
1521 /* Update conference state in a stateless fashion */
1522 for (x = 0; x < 3; x++) {
1523 /* Any three-way calling makes slave native mode *definitely* out
1525 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1528 /* If we don't have any 3-way calls, check to see if we have
1529 precisely one slave */
1530 if (useslavenative) {
1531 for (x = 0; x < MAX_SLAVES; x++) {
1534 /* Whoops already have a slave! No
1535 slave native and stop right away */
1540 /* We have one slave so far */
1541 slave = p->slaves[x];
1546 /* If no slave, slave native definitely out */
1549 else if (slave->law != p->law) {
1555 return useslavenative;
1558 static int reset_conf(struct zt_pvt *p)
1561 memset(&zi, 0, sizeof(zi));
1563 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1564 if (p->subs[SUB_REAL].zfd > -1) {
1565 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1566 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1571 static int update_conf(struct zt_pvt *p)
1576 struct zt_pvt *slave = NULL;
1578 useslavenative = isslavenative(p, &slave);
1579 /* Start with the obvious, general stuff */
1580 for (x = 0; x < 3; x++) {
1581 /* Look for three way calls */
1582 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1583 conf_add(p, &p->subs[x], x, 0);
1586 conf_del(p, &p->subs[x], x);
1589 /* If we have a slave, add him to our conference now. or DAX
1590 if this is slave native */
1591 for (x = 0; x < MAX_SLAVES; x++) {
1594 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1596 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1601 /* If we're supposed to be in there, do so now */
1602 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1604 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1606 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1610 /* If we have a master, add ourselves to his conference */
1612 if (isslavenative(p->master, NULL)) {
1613 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1615 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1619 /* Nobody is left (or should be left) in our conference.
1623 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1627 static void zt_enable_ec(struct zt_pvt *p)
1634 ast_debug(1, "Echo cancellation already on\n");
1638 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1641 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
1642 if (p->echocancel.head.tap_length) {
1644 if (p->echocancel) {
1646 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1648 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1650 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d\n", p->channel);
1652 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
1653 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL_PARAMS, &p->echocancel);
1656 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1659 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1662 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1665 ast_debug(1, "No echo cancellation requested\n");
1668 static void zt_train_ec(struct zt_pvt *p)
1673 if (p && p->echocanon && p->echotraining) {
1674 x = p->echotraining;
1675 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1677 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1679 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1681 ast_debug(1, "No echo training requested\n");
1685 static void zt_disable_ec(struct zt_pvt *p)
1690 #if defined(HAVE_ZAPTEL_ECHOCANPARAMS)
1691 struct zt_echocanparams ecp = { .tap_length = 0 };
1693 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL_PARAMS, &ecp);
1697 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1701 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1703 ast_debug(1, "Disabled echo cancellation on channel %d\n", p->channel);
1709 static void fill_txgain(struct zt_gains *g, float gain, int law)
1713 float linear_gain = pow(10.0, gain / 20.0);
1717 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1719 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1720 if (k > 32767) k = 32767;
1721 if (k < -32767) k = -32767;
1722 g->txgain[j] = AST_LIN2A(k);
1729 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1731 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1732 if (k > 32767) k = 32767;
1733 if (k < -32767) k = -32767;
1734 g->txgain[j] = AST_LIN2MU(k);
1743 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1747 float linear_gain = pow(10.0, gain / 20.0);
1751 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1753 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1754 if (k > 32767) k = 32767;
1755 if (k < -32767) k = -32767;
1756 g->rxgain[j] = AST_LIN2A(k);
1763 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1765 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1766 if (k > 32767) k = 32767;
1767 if (k < -32767) k = -32767;
1768 g->rxgain[j] = AST_LIN2MU(k);
1777 static int set_actual_txgain(int fd, int chan, float gain, int law)
1782 memset(&g, 0, sizeof(g));
1784 res = ioctl(fd, ZT_GETGAINS, &g);
1786 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1790 fill_txgain(&g, gain, law);
1792 return ioctl(fd, ZT_SETGAINS, &g);
1795 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1800 memset(&g, 0, sizeof(g));
1802 res = ioctl(fd, ZT_GETGAINS, &g);
1804 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1808 fill_rxgain(&g, gain, law);
1810 return ioctl(fd, ZT_SETGAINS, &g);
1813 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1815 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1818 static int bump_gains(struct zt_pvt *p)
1822 /* Bump receive gain by value stored in cid_rxgain */
1823 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1825 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1832 static int restore_gains(struct zt_pvt *p)
1836 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1838 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1845 static inline int zt_set_hook(int fd, int hs)
1850 res = ioctl(fd, ZT_HOOK, &x);
1853 if (errno == EINPROGRESS)
1855 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1861 static inline int zt_confmute(struct zt_pvt *p, int muted)
1865 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
1867 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1869 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1871 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1873 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1877 static int save_conference(struct zt_pvt *p)
1879 struct zt_confinfo c;
1881 if (p->saveconf.confmode) {
1882 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1885 p->saveconf.chan = 0;
1886 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1888 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1889 p->saveconf.confmode = 0;
1894 c.confmode = ZT_CONF_NORMAL;
1895 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1897 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1900 ast_debug(1, "Disabled conferencing\n");
1905 * \brief Send MWI state change
1907 * \arg mailbox_full This is the mailbox associated with the FXO line that the
1908 * MWI state has changed on.
1909 * \arg thereornot This argument should simply be set to 1 or 0, to indicate
1910 * whether there are messages waiting or not.
1914 * This function does two things:
1916 * 1) It generates an internal Asterisk event notifying any other module that
1917 * cares about MWI that the state of a mailbox has changed.
1919 * 2) It runs the script specified by the mwimonitornotify option to allow
1920 * some custom handling of the state change.
1922 static void notify_message(char *mailbox_full, int thereornot)
1924 char s[sizeof(mwimonitornotify) + 80];
1925 struct ast_event *event;
1926 char *mailbox, *context;
1928 /* Strip off @default */
1929 context = mailbox = ast_strdupa(mailbox_full);
1930 strsep(&context, "@");
1931 if (ast_strlen_zero(context))
1932 context = "default";
1934 if (!(event = ast_event_new(AST_EVENT_MWI,
1935 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1936 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1937 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1938 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1939 AST_EVENT_IE_END))) {
1943 ast_event_queue_and_cache(event,
1944 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
1945 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR,
1948 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
1949 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
1954 static int restore_conference(struct zt_pvt *p)
1957 if (p->saveconf.confmode) {
1958 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1959 p->saveconf.confmode = 0;
1961 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1965 ast_debug(1, "Restored conferencing\n");
1969 static int send_callerid(struct zt_pvt *p);
1971 static int send_cwcidspill(struct zt_pvt *p)
1975 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1977 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1978 /* Make sure we account for the end */
1979 p->cidlen += READ_SIZE * 4;
1982 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1986 static int has_voicemail(struct zt_pvt *p)
1989 struct ast_event *event;
1990 char *mailbox, *context;
1992 mailbox = context = ast_strdupa(p->mailbox);
1993 strsep(&context, "@");
1994 if (ast_strlen_zero(context))
1995 context = "default";
1997 event = ast_event_get_cached(AST_EVENT_MWI,
1998 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1999 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
2000 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
2004 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
2005 ast_event_destroy(event);
2007 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
2012 static int send_callerid(struct zt_pvt *p)
2014 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
2016 /* Take out of linear mode if necessary */
2017 if (p->subs[SUB_REAL].linear) {
2018 p->subs[SUB_REAL].linear = 0;
2019 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
2021 while (p->cidpos < p->cidlen) {
2022 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
2024 if (errno == EAGAIN)
2027 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
2035 ast_free(p->cidspill);
2037 if (p->callwaitcas) {
2038 /* Wait for CID/CW to expire */
2039 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
2041 restore_conference(p);
2045 static int zt_callwait(struct ast_channel *ast)
2047 struct zt_pvt *p = ast->tech_pvt;
2048 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
2050 ast_log(LOG_WARNING, "Spill already exists?!?\n");
2051 ast_free(p->cidspill);
2053 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
2057 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
2058 if (!p->callwaitrings && p->callwaitingcallerid) {
2059 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
2061 p->cidlen = 2400 + 680 + READ_SIZE * 4;
2063 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
2065 p->cidlen = 2400 + READ_SIZE * 4;
2074 static unsigned char cid_pres2ss7pres(int cid_pres)
2076 return (cid_pres >> 5) & 0x03;
2079 static unsigned char cid_pres2ss7screen(int cid_pres)
2081 return cid_pres & 0x03;
2085 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
2087 struct zt_pvt *p = ast->tech_pvt;
2088 int x, res, index,mysig;
2093 char dest[256]; /* must be same length as p->dialdest */
2094 ast_mutex_lock(&p->lock);
2095 ast_copy_string(dest, rdest, sizeof(dest));
2096 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
2097 if ((ast->_state == AST_STATE_BUSY)) {
2098 p->subs[SUB_REAL].needbusy = 1;
2099 ast_mutex_unlock(&p->lock);
2102 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
2103 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
2104 ast_mutex_unlock(&p->lock);
2108 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
2110 /* Special pseudo -- automatically up */
2111 ast_setstate(ast, AST_STATE_UP);
2112 ast_mutex_unlock(&p->lock);
2115 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
2116 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
2118 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
2121 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
2124 if (p->outsigmod > -1)
2125 mysig = p->outsigmod;
2131 if (p->owner == ast) {
2132 /* Normal ring, on hook */
2134 /* Don't send audio while on hook, until the call is answered */
2136 if (p->use_callerid) {
2137 /* Generate the Caller-ID spill if desired */
2139 ast_log(LOG_WARNING, "cidspill already exists??\n");
2140 ast_free(p->cidspill);
2143 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2144 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2149 /* Choose proper cadence */
2150 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2151 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
2152 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
2153 p->cidrings = cidrings[p->distinctivering - 1];
2155 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
2156 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
2157 p->cidrings = p->sendcalleridafter;
2160 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2161 c = strchr(dest, '/');
2164 if (c && (strlen(c) < p->stripmsd)) {
2165 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2169 p->dop.op = ZT_DIAL_OP_REPLACE;
2170 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2171 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2173 p->dop.dialstr[0] = '\0';
2176 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2177 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2178 ast_mutex_unlock(&p->lock);
2183 /* Call waiting call */
2184 p->callwaitrings = 0;
2185 if (ast->cid.cid_num)
2186 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2188 p->callwait_num[0] = '\0';
2189 if (ast->cid.cid_name)
2190 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2192 p->callwait_name[0] = '\0';
2193 /* Call waiting tone instead */
2194 if (zt_callwait(ast)) {
2195 ast_mutex_unlock(&p->lock);
2198 /* Make ring-back */
2199 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2200 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2203 n = ast->cid.cid_name;
2204 l = ast->cid.cid_num;
2206 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2208 p->lastcid_num[0] = '\0';
2210 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2212 p->lastcid_name[0] = '\0';
2213 ast_setstate(ast, AST_STATE_RINGING);
2214 index = zt_get_index(ast, p, 0);
2216 p->subs[index].needringing = 1;
2229 case SIG_FGC_CAMAMF:
2234 case SIG_SF_FEATDMF:
2235 case SIG_FEATDMF_TA:
2237 c = strchr(dest, '/');
2242 if (strlen(c) < p->stripmsd) {
2243 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2244 ast_mutex_unlock(&p->lock);
2248 /* Start the trunk, if not GR-303 */
2252 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2254 if (errno != EINPROGRESS) {
2255 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2256 ast_mutex_unlock(&p->lock);
2263 ast_debug(1, "Dialing '%s'\n", c);
2264 p->dop.op = ZT_DIAL_OP_REPLACE;
2270 l = ast->cid.cid_num;
2272 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2274 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2277 l = ast->cid.cid_num;
2279 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2281 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2283 case SIG_FEATDMF_TA:
2285 const char *cic, *ozz;
2287 /* If you have to go through a Tandem Access point you need to use this */
2288 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2291 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2295 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2296 ast_mutex_unlock(&p->lock);
2299 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2300 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2305 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2308 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2310 case SIG_FGC_CAMAMF:
2312 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2316 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2318 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2322 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2323 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2324 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2325 p->echorest[sizeof(p->echorest) - 1] = '\0';
2327 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2331 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2333 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2334 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2335 ast_mutex_unlock(&p->lock);
2339 ast_debug(1, "Deferring dialing...\n");
2342 if (ast_strlen_zero(c))
2344 ast_setstate(ast, AST_STATE_DIALING);
2347 /* Special pseudo -- automatically up*/
2348 ast_setstate(ast, AST_STATE_UP);
2354 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2355 p->dialdest[0] = '\0';
2358 ast_debug(1, "not yet implemented\n");
2359 ast_mutex_unlock(&p->lock);
2364 char ss7_called_nai;
2365 int called_nai_strip;
2366 char ss7_calling_nai;
2367 int calling_nai_strip;
2368 const char *charge_str = NULL;
2369 const char *gen_address = NULL;
2370 const char *gen_digits = NULL;
2371 const char *gen_dig_type = NULL;
2372 const char *gen_dig_scheme = NULL;
2373 const char *jip_digits = NULL;
2374 const char *lspi_ident = NULL;
2375 const char *rlt_flag = NULL;
2376 const char *call_ref_id = NULL;
2377 const char *call_ref_pc = NULL;
2379 c = strchr(dest, '/');
2385 if (!p->hidecallerid) {
2386 l = ast->cid.cid_num;
2391 if (ss7_grab(p, p->ss7)) {
2392 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2393 ast_mutex_unlock(&p->lock);
2396 p->digital = IS_DIGITAL(ast->transfercapability);
2397 p->ss7call = isup_new_call(p->ss7->ss7);
2401 ast_mutex_unlock(&p->lock);
2402 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2406 called_nai_strip = 0;
2407 ss7_called_nai = p->ss7->called_nai;
2408 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2409 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2410 called_nai_strip = strlen(p->ss7->internationalprefix);
2411 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2412 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2413 called_nai_strip = strlen(p->ss7->nationalprefix);
2414 ss7_called_nai = SS7_NAI_NATIONAL;
2416 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2419 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2421 calling_nai_strip = 0;
2422 ss7_calling_nai = p->ss7->calling_nai;
2423 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2424 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2425 calling_nai_strip = strlen(p->ss7->internationalprefix);
2426 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2427 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2428 calling_nai_strip = strlen(p->ss7->nationalprefix);
2429 ss7_calling_nai = SS7_NAI_NATIONAL;
2431 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2434 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2435 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2436 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2438 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
2439 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2441 /* Set the charge number if it is set */
2442 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
2444 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
2446 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
2448 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 */
2450 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
2451 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
2452 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
2454 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
2456 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
2458 isup_set_jip_digits(p->ss7call, jip_digits);
2460 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
2462 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
2464 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
2465 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 ))
2466 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
2468 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
2469 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
2471 isup_set_callref(p->ss7call, atoi(call_ref_id),
2472 call_ref_pc ? atoi(call_ref_pc) : 0);
2475 isup_iam(p->ss7->ss7, p->ss7call);
2476 ast_setstate(ast, AST_STATE_DIALING);
2479 #endif /* HAVE_SS7 */
2483 #ifdef SUPPORT_USERUSER
2484 const char *useruser;
2488 int prilocaldialplan;
2492 int redirect_reason;
2494 c = strchr(dest, '/');
2503 if (!p->hidecallerid) {
2504 l = ast->cid.cid_num;
2505 if (!p->hidecalleridname) {
2506 n = ast->cid.cid_name;
2510 if (strlen(c) < p->stripmsd) {
2511 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2512 ast_mutex_unlock(&p->lock);
2515 if (mysig != SIG_FXSKS) {
2516 p->dop.op = ZT_DIAL_OP_REPLACE;
2517 s = strchr(c + p->stripmsd, 'w');
2520 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2522 p->dop.dialstr[0] = '\0';
2525 p->dop.dialstr[0] = '\0';
2528 if (pri_grab(p, p->pri)) {
2529 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2530 ast_mutex_unlock(&p->lock);
2533 if (!(p->call = pri_new_call(p->pri->pri))) {
2534 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2536 ast_mutex_unlock(&p->lock);
2539 if (!(sr = pri_sr_new())) {
2540 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2542 ast_mutex_unlock(&p->lock);
2544 if (p->bearer || (mysig == SIG_FXSKS)) {
2546 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2547 p->bearer->call = p->call;
2549 ast_debug(1, "I'm being setup with no bearer right now...\n");
2551 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2553 p->digital = IS_DIGITAL(ast->transfercapability);
2554 /* Add support for exclusive override */
2555 if (p->priexclusive)
2558 /* otherwise, traditional behavior */
2559 if (p->pri->nodetype == PRI_NETWORK)
2565 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2566 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2568 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2569 if (p->pri->facilityenable)
2570 pri_facility_enable(p->pri->pri);
2572 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2574 pridialplan = p->pri->dialplan - 1;
2575 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2576 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2577 if (pridialplan == -2) {
2578 dp_strip = strlen(p->pri->internationalprefix);
2580 pridialplan = PRI_INTERNATIONAL_ISDN;
2581 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2582 if (pridialplan == -2) {
2583 dp_strip = strlen(p->pri->nationalprefix);
2585 pridialplan = PRI_NATIONAL_ISDN;
2587 pridialplan = PRI_LOCAL_ISDN;
2590 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2591 switch (c[p->stripmsd]) {
2593 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2596 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2599 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2602 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2605 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2608 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2611 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2614 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2617 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2620 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2623 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2626 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2629 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2632 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2636 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2640 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2643 prilocaldialplan = p->pri->localdialplan - 1;
2644 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2645 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2646 if (prilocaldialplan == -2) {
2647 ldp_strip = strlen(p->pri->internationalprefix);
2649 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2650 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2651 if (prilocaldialplan == -2) {
2652 ldp_strip = strlen(p->pri->nationalprefix);
2654 prilocaldialplan = PRI_NATIONAL_ISDN;
2656 prilocaldialplan = PRI_LOCAL_ISDN;
2660 while (*l > '9' && *l != '*' && *l != '#') {
2663 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2666 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2669 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2672 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2675 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2678 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2681 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2684 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2687 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2690 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2693 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2696 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2699 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2702 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2706 ast_log(LOG_WARNING, "Unrecognized prilocaldialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2711 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2712 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2713 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2714 if (!strcasecmp(rr_str, "UNKNOWN"))
2715 redirect_reason = 0;
2716 else if (!strcasecmp(rr_str, "BUSY"))
2717 redirect_reason = 1;
2718 else if (!strcasecmp(rr_str, "NO_REPLY"))
2719 redirect_reason = 2;
2720 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2721 redirect_reason = 15;
2723 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2725 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2726 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2728 #ifdef SUPPORT_USERUSER
2729 /* User-user info */
2730 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2733 pri_sr_set_useruser(sr, useruser);
2736 if (pri_setup(p->pri->pri, p->call, sr)) {
2737 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2738 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2740 ast_mutex_unlock(&p->lock);
2745 ast_setstate(ast, AST_STATE_DIALING);
2749 ast_mutex_unlock(&p->lock);
2753 static void destroy_zt_pvt(struct zt_pvt **pvt)
2755 struct zt_pvt *p = *pvt;
2756 /* Remove channel from the list */
2758 p->prev->next = p->next;
2760 p->next->prev = p->prev;
2762 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2763 if (p->mwi_event_sub)
2764 ast_event_unsubscribe(p->mwi_event_sub);
2766 ast_variables_destroy(p->vars);
2767 ast_mutex_destroy(&p->lock);
2772 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2782 for (i = 0; i < 3; i++) {
2783 if (cur->subs[i].owner) {
2789 prev->next = cur->next;
2791 prev->next->prev = prev;
2797 iflist->prev = NULL;
2801 if (cur->subs[SUB_REAL].zfd > -1) {
2802 zt_close(cur->subs[SUB_REAL].zfd);
2804 destroy_zt_pvt(&cur);
2808 prev->next = cur->next;
2810 prev->next->prev = prev;
2816 iflist->prev = NULL;
2820 if (cur->subs[SUB_REAL].zfd > -1) {
2821 zt_close(cur->subs[SUB_REAL].zfd);
2823 destroy_zt_pvt(&cur);
2829 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2831 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2833 static char *zap_send_keypad_facility_descrip =
2834 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2835 " IE over the current channel.\n";
2837 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2839 /* Data will be our digit string */
2841 char *digits = (char *) data;
2843 if (ast_strlen_zero(digits)) {
2844 ast_debug(1, "No digit string sent to application!\n");
2848 p = (struct zt_pvt *)chan->tech_pvt;
2851 ast_debug(1, "Unable to find technology private\n");
2855 ast_mutex_lock(&p->lock);
2857 if (!p->pri || !p->call) {
2858 ast_debug(1, "Unable to find pri or call on channel!\n");
2859 ast_mutex_unlock(&p->lock);
2863 if (!pri_grab(p, p->pri)) {
2864 pri_keypad_facility(p->pri->pri, p->call, digits);
2867 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2868 ast_mutex_unlock(&p->lock);
2872 ast_mutex_unlock(&p->lock);
2877 static int pri_is_up(struct zt_pri *pri)
2880 for (x = 0; x < NUM_DCHANS; x++) {
2881 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2887 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2889 bearer->owner = &inuse;
2890 bearer->realcall = crv;
2891 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2892 if (crv->subs[SUB_REAL].owner)
2893 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].zfd);
2894 crv->bearer = bearer;
2895 crv->call = bearer->call;
2900 static char *pri_order(int level)
2910 return "Quaternary";
2916 /* Returns fd of the active dchan */
2917 static int pri_active_dchan_fd(struct zt_pri *pri)
2921 for (x = 0; x < NUM_DCHANS; x++) {
2922 if ((pri->dchans[x] == pri->pri))
2929 static int pri_find_dchan(struct zt_pri *pri)
2936 for (x = 0; x < NUM_DCHANS; x++) {
2937 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2939 if (pri->dchans[x] == old) {
2945 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2946 pri->dchannels[newslot]);
2948 if (old && (oldslot != newslot))
2949 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2950 pri->dchannels[oldslot], pri->dchannels[newslot]);
2951 pri->pri = pri->dchans[newslot];
2956 static int zt_hangup(struct ast_channel *ast)
2960 /*static int restore_gains(struct zt_pvt *p);*/
2961 struct zt_pvt *p = ast->tech_pvt;
2962 struct zt_pvt *tmp = NULL;
2963 struct zt_pvt *prev = NULL;
2966 ast_debug(1, "zt_hangup(%s)\n", ast->name);
2967 if (!ast->tech_pvt) {
2968 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2972 ast_mutex_lock(&p->lock);
2974 index = zt_get_index(ast, p, 1);
2976 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
2978 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2984 if (p->origcid_num) {
2985 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2986 ast_free(p->origcid_num);
2987 p->origcid_num = NULL;
2989 if (p->origcid_name) {
2990 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2991 ast_free(p->origcid_name);
2992 p->origcid_name = NULL;
2995 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2999 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
3000 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
3004 /* Real channel, do some fixup */
3005 p->subs[index].owner = NULL;
3006 p->subs[index].needanswer = 0;
3007 p->subs[index].needflash = 0;
3008 p->subs[index].needringing = 0;
3009 p->subs[index].needbusy = 0;
3010 p->subs[index].needcongestion = 0;
3011 p->subs[index].linear = 0;
3012 p->subs[index].needcallerid = 0;
3013 p->polarity = POLARITY_IDLE;
3014 zt_setlinear(p->subs[index].zfd, 0);
3015 if (index == SUB_REAL) {
3016 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
3017 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
3018 if (p->subs[SUB_CALLWAIT].inthreeway) {
3019 /* We had flipped over to answer a callwait and now it's gone */
3020 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
3021 /* Move to the call-wait, but un-own us until they flip back. */
3022 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3023 unalloc_sub(p, SUB_CALLWAIT);
3026 /* The three way hung up, but we still have a call wait */
3027 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
3028 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3029 unalloc_sub(p, SUB_THREEWAY);
3030 if (p->subs[SUB_REAL].inthreeway) {
3031 /* This was part of a three way call. Immediately make way for
3033 ast_debug(1, "Call was complete, setting owner to former third call\n");
3034 p->owner = p->subs[SUB_REAL].owner;
3036 /* This call hasn't been completed yet... Set owner to NULL */
3037 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
3040 p->subs[SUB_REAL].inthreeway = 0;
3042 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
3043 /* Move to the call-wait and switch back to them. */
3044 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3045 unalloc_sub(p, SUB_CALLWAIT);
3046 p->owner = p->subs[SUB_REAL].owner;
3047 if (p->owner->_state != AST_STATE_UP)
3048 p->subs[SUB_REAL].needanswer = 1;
3049 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
3050 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
3051 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
3052 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3053 unalloc_sub(p, SUB_THREEWAY);
3054 if (p->subs[SUB_REAL].inthreeway) {
3055 /* This was part of a three way call. Immediately make way for
3057 ast_debug(1, "Call was complete, setting owner to former third call\n");
3058 p->owner = p->subs[SUB_REAL].owner;
3060 /* This call hasn't been completed yet... Set owner to NULL */
3061 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
3064 p->subs[SUB_REAL].inthreeway = 0;
3066 } else if (index == SUB_CALLWAIT) {
3067 /* Ditch the holding callwait call, and immediately make it availabe */
3068 if (p->subs[SUB_CALLWAIT].inthreeway) {
3069 /* This is actually part of a three way, placed on hold. Place the third part
3070 on music on hold now */
3071 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
3072 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
3073 S_OR(p->mohsuggest, NULL),
3074 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3076 p->subs[SUB_THREEWAY].inthreeway = 0;
3077 /* Make it the call wait now */
3078 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
3079 unalloc_sub(p, SUB_THREEWAY);
3081 unalloc_sub(p, SUB_CALLWAIT);
3082 } else if (index == SUB_THREEWAY) {
3083 if (p->subs[SUB_CALLWAIT].inthreeway) {
3084 /* The other party of the three way call is currently in a call-wait state.
3085 Start music on hold for them, and take the main guy out of the third call */
3086 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
3087 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
3088 S_OR(p->mohsuggest, NULL),
3089 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3091 p->subs[SUB_CALLWAIT].inthreeway = 0;
3093 p->subs[SUB_REAL].inthreeway = 0;
3094 /* If this was part of a three way call index, let us make
3095 another three way call */
3096 unalloc_sub(p, SUB_THREEWAY);
3098 /* This wasn't any sort of call, but how are we an index? */
3099 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
3103 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
3106 p->distinctivering = 0;
3107 p->confirmanswer = 0;
3113 p->onhooktime = time(NULL);
3114 #if defined(HAVE_PRI) || defined(HAVE_SS7)
3122 ast_dsp_free(p->dsp);
3126 law = ZT_LAW_DEFAULT;
3127 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
3129 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
3130 /* Perform low level hangup if no owner left */
3134 if (!ss7_grab(p, p->ss7)) {
3135 if (!p->alreadyhungup) {
3136 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
3137 int icause = ast->hangupcause ? ast->hangupcause : -1;
3141 icause = atoi(cause);
3143 isup_rel(p->ss7->ss7, p->ss7call, icause);
3145 p->alreadyhungup = 1;
3147 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
3149 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
3157 #ifdef SUPPORT_USERUSER
3158 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
3161 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
3162 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
3163 if (!pri_grab(p, p->pri)) {
3164 if (p->alreadyhungup) {
3165 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
3167 #ifdef SUPPORT_USERUSER
3168 pri_call_set_useruser(p->call, useruser);
3171 pri_hangup(p->pri->pri, p->call, -1);
3174 p->bearer->call = NULL;
3176 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
3177 int icause = ast->hangupcause ? ast->hangupcause : -1;
3178 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
3180 #ifdef SUPPORT_USERUSER
3181 pri_call_set_useruser(p->call, useruser);
3184 p->alreadyhungup = 1;
3186 p->bearer->alreadyhungup = 1;
3189 icause = atoi(cause);
3191 pri_hangup(p->pri->pri, p->call, icause);
3194 ast_log(LOG_WARNING, "pri_disconnect failed\n");
3197 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3202 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
3208 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7) && (p->sig != SIG_BRI) && (p->sig != SIG_BRI_PTMP)))
3209 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
3211 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
3217 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
3220 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
3222 /* If they're off hook, try playing congestion */
3223 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
3224 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
3226 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3232 /* Make sure we're not made available for at least two seconds assuming
3233 we were actually used for an inbound or outbound call. */
3234 if (ast->_state != AST_STATE_RESERVED) {
3235 time(&p->guardtime);
3240 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3243 ast_free(p->cidspill);
3247 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
3248 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
3252 p->callwaiting = p->permcallwaiting;
3253 p->hidecallerid = p->permhidecallerid;
3258 /* Restore data mode */
3259 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
3261 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
3265 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
3266 /* Free up the bearer channel as well, and
3267 don't use its file descriptor anymore */
3268 update_conf(p->bearer);
3269 reset_conf(p->bearer);
3270 p->bearer->owner = NULL;
3271 p->bearer->realcall = NULL;
3273 p->subs[SUB_REAL].zfd = -1;
3280 p->callwaitingrepeat = 0;
3283 ast->tech_pvt = NULL;
3284 ast_mutex_unlock(&p->lock);
3285 ast_module_unref(ast_module_info->self);
3286 ast_verb(3, "Hungup '%s'\n", ast->name);
3288 ast_mutex_lock(&iflock);
3294 destroy_channel(prev, tmp, 0);
3302 ast_mutex_unlock(&iflock);
3306 static int zt_answer(struct ast_channel *ast)
3308 struct zt_pvt *p = ast->tech_pvt;
3311 int oldstate = ast->_state;
3312 ast_setstate(ast, AST_STATE_UP);
3313 ast_mutex_lock(&p->lock);
3314 index = zt_get_index(ast, p, 0);
3317 /* nothing to do if a radio channel */
3318 if ((p->radio || (p->oprmode < 0))) {
3319 ast_mutex_unlock(&p->lock);
3333 case SIG_FEATDMF_TA:
3336 case SIG_FGC_CAMAMF:
3341 case SIG_SF_FEATDMF:
3346 /* Pick up the line */
3347 ast_debug(1, "Took %s off hook\n", ast->name);
3348 if (p->hanguponpolarityswitch) {
3349 p->polaritydelaytv = ast_tvnow();
3351 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
3352 tone_zone_play_tone(p->subs[index].zfd, -1);
3354 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
3355 if (oldstate == AST_STATE_RINGING) {
3356 ast_debug(1, "Finally swapping real and threeway\n");
3357 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3358 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3359 p->owner = p->subs[SUB_REAL].owner;
3362 if (p->sig & __ZT_SIG_FXS) {
3371 /* Send a pri acknowledge */
3372 if (!pri_grab(p, p->pri)) {
3374 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3377 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3384 if (!ss7_grab(p, p->ss7)) {
3386 res = isup_anm(p->ss7->ss7, p->ss7call);
3389 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3395 ast_mutex_unlock(&p->lock);
3398 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3401 ast_mutex_unlock(&p->lock);
3405 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3411 struct zt_pvt *p = chan->tech_pvt, *pp;
3412 struct oprmode *oprmode;
3415 /* all supported options require data */
3416 if (!data || (datalen < 1)) {
3422 case AST_OPTION_TXGAIN:
3423 scp = (signed char *) data;
3424 index = zt_get_index(chan, p, 0);
3426 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
3429 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
3430 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
3431 case AST_OPTION_RXGAIN:
3432 scp = (signed char *) data;
3433 index = zt_get_index(chan, p, 0);
3435 ast_log(LOG_WARNING, "No index in RXGAIN?\n");