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;
640 int busy_quietlength;
642 struct timeval flashtime; /*!< Last flash-hook time */
644 int cref; /*!< Call reference number */
645 ZT_DIAL_OPERATION dop;
646 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
648 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
649 int amaflags; /*!< AMA Flags */
650 struct tdd_state *tdd; /*!< TDD flag */
651 char call_forward[AST_MAX_EXTENSION];
652 char mailbox[AST_MAX_EXTENSION];
653 struct ast_event_sub *mwi_event_sub;
657 int distinctivering; /*!< Which distinctivering to use */
658 int cidrings; /*!< Which ring to deliver CID on */
659 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
661 int polarityonanswerdelay;
662 struct timeval polaritydelaytv;
663 int sendcalleridafter;
666 struct zt_pvt *bearer;
667 struct zt_pvt *realcall;
676 struct isup_call *ss7call;
677 char charge_number[50];
678 char gen_add_number[50];
679 char gen_dig_number[50];
680 unsigned char gen_add_num_plan;
681 unsigned char gen_add_nai;
682 unsigned char gen_add_pres_ind;
683 unsigned char gen_add_type;
684 unsigned char gen_dig_type;
685 unsigned char gen_dig_scheme;
687 unsigned char lspi_type;
688 unsigned char lspi_scheme;
689 unsigned char lspi_context;
691 unsigned int call_ref_ident;
692 unsigned int call_ref_pc;
694 int cic; /*!< CIC associated with channel */
695 unsigned int dpc; /*!< CIC's DPC */
696 unsigned int loopedback:1;
699 } *iflist = NULL, *ifend = NULL;
701 /*! \brief Channel configuration from zapata.conf .
702 * This struct is used for parsing the [channels] section of zapata.conf.
703 * Generally there is a field here for every possible configuration item.
705 * The state of fields is saved along the parsing and whenever a 'channel'
706 * statement is reached, the current zt_chan_conf is used to configure the
707 * channel (struct zt_pvt)
709 * \see zt_chan_init for the default values.
711 struct zt_chan_conf {
722 char smdi_port[SMDI_MAX_FILENAME_LEN];
725 /** returns a new zt_chan_conf with default values (by-value) */
726 static struct zt_chan_conf zt_chan_conf_default(void) {
727 /* recall that if a field is not included here it is initialized
730 struct zt_chan_conf conf = {
734 .switchtype = PRI_SWITCH_NI2,
735 .dialplan = PRI_NATIONAL_ISDN + 1,
736 .localdialplan = PRI_NATIONAL_ISDN + 1,
742 .internationalprefix = "",
743 .nationalprefix = "",
752 .called_nai = SS7_NAI_NATIONAL,
753 .calling_nai = SS7_NAI_NATIONAL,
754 .internationalprefix = "",
755 .nationalprefix = "",
756 .subscriberprefix = "",
761 .context = "default",
764 .mohinterpret = "default",
768 .cid_signalling = CID_SIG_BELL,
769 .cid_start = CID_START_RING,
788 .polarityonanswerdelay = 600,
790 .sendcalleridafter = DEFAULT_CIDRINGS
802 .smdi_port = "/dev/ttyS0",
809 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
810 static int zt_digit_begin(struct ast_channel *ast, char digit);
811 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
812 static int zt_sendtext(struct ast_channel *c, const char *text);
813 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
814 static int zt_hangup(struct ast_channel *ast);
815 static int zt_answer(struct ast_channel *ast);
816 static struct ast_frame *zt_read(struct ast_channel *ast);
817 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
818 static struct ast_frame *zt_exception(struct ast_channel *ast);
819 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
820 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
821 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
822 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
824 static const struct ast_channel_tech zap_tech = {
826 .description = tdesc,
827 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
828 .requester = zt_request,
829 .send_digit_begin = zt_digit_begin,
830 .send_digit_end = zt_digit_end,
831 .send_text = zt_sendtext,
838 .exception = zt_exception,
839 .indicate = zt_indicate,
841 .setoption = zt_setoption,
842 .func_channel_read = zt_func_read,
846 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
848 #define GET_CHANNEL(p) ((p)->channel)
851 struct zt_pvt *round_robin[32];
854 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
857 /* Grab the lock first */
859 res = ast_mutex_trylock(&pri->lock);
861 ast_mutex_unlock(&pvt->lock);
862 /* Release the lock and try again */
864 ast_mutex_lock(&pvt->lock);
867 /* Then break the poll */
868 pthread_kill(pri->master, SIGURG);
874 static inline void ss7_rel(struct zt_ss7 *ss7)
876 ast_mutex_unlock(&ss7->lock);
879 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
882 /* Grab the lock first */
884 res = ast_mutex_trylock(&pri->lock);
886 ast_mutex_unlock(&pvt->lock);
887 /* Release the lock and try again */
889 ast_mutex_lock(&pvt->lock);
892 /* Then break the poll */
893 pthread_kill(pri->master, SIGURG);
897 #define NUM_CADENCE_MAX 25
898 static int num_cadence = 4;
899 static int user_has_defined_cadences = 0;
901 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
902 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
903 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
904 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
905 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
908 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
909 * is 1, the second pause is 2 and so on.
912 static int cidrings[NUM_CADENCE_MAX] = {
913 2, /*!< Right after first long ring */
914 4, /*!< Right after long part */
915 3, /*!< After third chirp */
916 2, /*!< Second spell */
919 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
920 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
922 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
923 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
925 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
928 if (p->subs[0].owner == ast)
930 else if (p->subs[1].owner == ast)
932 else if (p->subs[2].owner == ast)
937 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
943 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
945 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
950 ast_mutex_unlock(&pri->lock);
953 if (p->subs[a].owner) {
954 if (ast_channel_trylock(p->subs[a].owner)) {
955 ast_mutex_unlock(&p->lock);
957 ast_mutex_lock(&p->lock);
959 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
960 ast_channel_unlock(p->subs[a].owner);
968 ast_mutex_lock(&pri->lock);
972 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
975 struct zt_pri *pri = (struct zt_pri*) data;
978 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
980 /* We must unlock the PRI to avoid the possibility of a deadlock */
981 #if defined(HAVE_PRI) || defined(HAVE_SS7)
988 ast_mutex_unlock(&pri->lock);
993 ast_mutex_unlock(&ss7->lock);
1003 if (ast_channel_trylock(p->owner)) {
1004 ast_mutex_unlock(&p->lock);
1006 ast_mutex_lock(&p->lock);
1008 ast_queue_frame(p->owner, f);
1009 ast_channel_unlock(p->owner);
1015 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1022 ast_mutex_lock(&pri->lock);
1027 ast_mutex_lock(&ss7->lock);
1038 static int restore_gains(struct zt_pvt *p);
1040 static void swap_subs(struct zt_pvt *p, int a, int b)
1044 struct ast_channel *towner;
1046 ast_debug(1, "Swapping %d and %d\n", a, b);
1048 tchan = p->subs[a].chan;
1049 towner = p->subs[a].owner;
1050 tinthreeway = p->subs[a].inthreeway;
1052 p->subs[a].chan = p->subs[b].chan;
1053 p->subs[a].owner = p->subs[b].owner;
1054 p->subs[a].inthreeway = p->subs[b].inthreeway;
1056 p->subs[b].chan = tchan;
1057 p->subs[b].owner = towner;
1058 p->subs[b].inthreeway = tinthreeway;
1060 if (p->subs[a].owner)
1061 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].zfd);
1062 if (p->subs[b].owner)
1063 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].zfd);
1064 wakeup_sub(p, a, NULL);
1065 wakeup_sub(p, b, NULL);
1068 static int zt_open(char *fn)
1076 for (x = 0; x < strlen(fn); x++) {
1077 if (!isdigit(fn[x])) {
1085 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1088 fn = "/dev/zap/channel";
1090 fd = open(fn, O_RDWR | O_NONBLOCK);
1092 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1096 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1100 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1105 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) {
1106 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
1115 static void zt_close(int fd)
1121 static int zt_setlinear(int zfd, int linear)
1124 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1131 static int alloc_sub(struct zt_pvt *p, int x)
1135 if (p->subs[x].zfd < 0) {
1136 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1137 if (p->subs[x].zfd > -1) {
1138 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1140 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1141 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1142 bi.numbufs = numbufs;
1143 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1145 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1148 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1149 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1150 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1151 zt_close(p->subs[x].zfd);
1152 p->subs[x].zfd = -1;
1155 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1158 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1161 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1165 static int unalloc_sub(struct zt_pvt *p, int x)
1168 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1171 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1172 if (p->subs[x].zfd > -1) {
1173 zt_close(p->subs[x].zfd);
1175 p->subs[x].zfd = -1;
1176 p->subs[x].linear = 0;
1177 p->subs[x].chan = 0;
1178 p->subs[x].owner = NULL;
1179 p->subs[x].inthreeway = 0;
1180 p->polarity = POLARITY_IDLE;
1181 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1185 static int digit_to_dtmfindex(char digit)
1188 return ZT_TONE_DTMF_BASE + (digit - '0');
1189 else if (digit >= 'A' && digit <= 'D')
1190 return ZT_TONE_DTMF_A + (digit - 'A');
1191 else if (digit >= 'a' && digit <= 'd')
1192 return ZT_TONE_DTMF_A + (digit - 'a');
1193 else if (digit == '*')
1194 return ZT_TONE_DTMF_s;
1195 else if (digit == '#')
1196 return ZT_TONE_DTMF_p;
1201 static int zt_digit_begin(struct ast_channel *chan, char digit)
1207 pvt = chan->tech_pvt;
1209 ast_mutex_lock(&pvt->lock);
1211 index = zt_get_index(chan, pvt, 0);
1213 if ((index != SUB_REAL) || !pvt->owner)
1217 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1218 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1219 if (pvt->setup_ack) {
1220 if (!pri_grab(pvt, pvt->pri)) {
1221 pri_information(pvt->pri->pri, pvt->call, digit);
1224 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1225 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1227 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1228 res = strlen(pvt->dialdest);
1229 pvt->dialdest[res++] = digit;
1230 pvt->dialdest[res] = '\0';
1235 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1238 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1240 ZT_DIAL_OPERATION zo = {
1241 .op = ZT_DIAL_OP_APPEND,
1244 zo.dialstr[0] = 'T';
1245 zo.dialstr[1] = digit;
1246 zo.dialstr[2] = '\0';
1247 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1248 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1252 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1254 pvt->begindigit = digit;
1258 ast_mutex_unlock(&pvt->lock);
1263 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1270 pvt = chan->tech_pvt;
1272 ast_mutex_lock(&pvt->lock);
1274 index = zt_get_index(chan, pvt, 0);
1276 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1280 /* This means that the digit was already sent via PRI signalling */
1281 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1282 && !pvt->begindigit)
1286 if (pvt->begindigit) {
1288 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1289 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1291 pvt->begindigit = 0;
1295 ast_mutex_unlock(&pvt->lock);
1300 static char *events[] = {
1313 "Hook Transition Complete",
1318 "Polarity Reversal",
1326 { ZT_ALARM_RED, "Red Alarm" },
1327 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1328 { ZT_ALARM_BLUE, "Blue Alarm" },
1329 { ZT_ALARM_RECOVER, "Recovering" },
1330 { ZT_ALARM_LOOPBACK, "Loopback" },
1331 { ZT_ALARM_NOTOPEN, "Not Open" },
1332 { ZT_ALARM_NONE, "None" },
1335 static char *alarm2str(int alarm)
1338 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1339 if (alarms[x].alarm & alarm)
1340 return alarms[x].name;
1342 return alarm ? "Unknown Alarm" : "No Alarm";
1345 static char *event2str(int event)
1347 static char buf[256];
1348 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1349 return events[event];
1350 sprintf(buf, "Event %d", event); /* safe */
1355 static char *dialplan2str(int dialplan)
1357 if (dialplan == -1 || dialplan == -2) {
1358 return("Dynamically set dialplan in ISDN");
1360 return (pri_plan2str(dialplan));
1364 static char *zap_sig2str(int sig)
1366 static char buf[256];
1369 return "E & M Immediate";
1371 return "E & M Wink";
1375 return "Feature Group D (DTMF)";
1377 return "Feature Group D (MF)";
1378 case SIG_FEATDMF_TA:
1379 return "Feature Groud D (MF) Tandem Access";
1381 return "Feature Group B (MF)";
1385 return "FGC/CAMA (Dialpulse)";
1386 case SIG_FGC_CAMAMF:
1387 return "FGC/CAMA (MF)";
1389 return "FXS Loopstart";
1391 return "FXS Groundstart";
1393 return "FXS Kewlstart";
1395 return "FXO Loopstart";
1397 return "FXO Groundstart";
1399 return "FXO Kewlstart";
1403 return "ISDN BRI Point to Point";
1405 return "ISDN BRI Point to MultiPoint";
1409 return "SF (Tone) Immediate";
1411 return "SF (Tone) Wink";
1413 return "SF (Tone) with Feature Group D (DTMF)";
1414 case SIG_SF_FEATDMF:
1415 return "SF (Tone) with Feature Group D (MF)";
1417 return "SF (Tone) with Feature Group B (MF)";
1418 case SIG_GR303FXOKS:
1419 return "GR-303 with FXOKS";
1420 case SIG_GR303FXSKS:
1421 return "GR-303 with FXSKS";
1425 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1430 #define sig2str zap_sig2str
1432 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1434 /* If the conference already exists, and we're already in it
1435 don't bother doing anything */
1438 memset(&zi, 0, sizeof(zi));
1441 if (slavechannel > 0) {
1442 /* If we have only one slave, do a digital mon */
1443 zi.confmode = ZT_CONF_DIGITALMON;
1444 zi.confno = slavechannel;
1447 /* Real-side and pseudo-side both participate in conference */
1448 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1449 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1451 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1452 zi.confno = p->confno;
1454 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1458 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1459 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1462 if (slavechannel < 1) {
1463 p->confno = zi.confno;
1465 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1466 ast_debug(1, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1470 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1472 /* If they're listening to our channel, they're ours */
1473 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1475 /* If they're a talker on our (allocated) conference, they're ours */
1476 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1481 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1484 if (/* Can't delete if there's no zfd */
1486 /* Don't delete from the conference if it's not our conference */
1488 /* Don't delete if we don't think it's conferenced at all (implied) */
1490 memset(&zi, 0, sizeof(zi));
1494 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1495 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1498 ast_debug(1, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1499 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1503 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1507 struct zt_pvt *slave = NULL;
1508 /* Start out optimistic */
1510 /* Update conference state in a stateless fashion */
1511 for (x = 0; x < 3; x++) {
1512 /* Any three-way calling makes slave native mode *definitely* out
1514 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1517 /* If we don't have any 3-way calls, check to see if we have
1518 precisely one slave */
1519 if (useslavenative) {
1520 for (x = 0; x < MAX_SLAVES; x++) {
1523 /* Whoops already have a slave! No
1524 slave native and stop right away */
1529 /* We have one slave so far */
1530 slave = p->slaves[x];
1535 /* If no slave, slave native definitely out */
1538 else if (slave->law != p->law) {
1544 return useslavenative;
1547 static int reset_conf(struct zt_pvt *p)
1550 memset(&zi, 0, sizeof(zi));
1552 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1553 if (p->subs[SUB_REAL].zfd > -1) {
1554 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1555 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1560 static int update_conf(struct zt_pvt *p)
1565 struct zt_pvt *slave = NULL;
1567 useslavenative = isslavenative(p, &slave);
1568 /* Start with the obvious, general stuff */
1569 for (x = 0; x < 3; x++) {
1570 /* Look for three way calls */
1571 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1572 conf_add(p, &p->subs[x], x, 0);
1575 conf_del(p, &p->subs[x], x);
1578 /* If we have a slave, add him to our conference now. or DAX
1579 if this is slave native */
1580 for (x = 0; x < MAX_SLAVES; x++) {
1583 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1585 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1590 /* If we're supposed to be in there, do so now */
1591 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1593 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1595 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1599 /* If we have a master, add ourselves to his conference */
1601 if (isslavenative(p->master, NULL)) {
1602 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1604 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1608 /* Nobody is left (or should be left) in our conference.
1612 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1616 static void zt_enable_ec(struct zt_pvt *p)
1623 ast_debug(1, "Echo cancellation already on\n");
1627 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1630 if (p->echocancel) {
1631 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1633 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1635 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1638 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1640 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1643 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1646 ast_debug(1, "No echo cancellation requested\n");
1649 static void zt_train_ec(struct zt_pvt *p)
1653 if (p && p->echocancel && p->echotraining) {
1654 x = p->echotraining;
1655 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1657 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1659 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1661 ast_debug(1, "No echo training requested\n");
1664 static void zt_disable_ec(struct zt_pvt *p)
1668 if (p->echocancel) {
1670 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1672 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1674 ast_debug(1, "disabled echo cancellation on channel %d\n", p->channel);
1679 static void fill_txgain(struct zt_gains *g, float gain, int law)
1683 float linear_gain = pow(10.0, gain / 20.0);
1687 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1689 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1690 if (k > 32767) k = 32767;
1691 if (k < -32767) k = -32767;
1692 g->txgain[j] = AST_LIN2A(k);
1699 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1701 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1702 if (k > 32767) k = 32767;
1703 if (k < -32767) k = -32767;
1704 g->txgain[j] = AST_LIN2MU(k);
1713 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1717 float linear_gain = pow(10.0, gain / 20.0);
1721 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1723 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1724 if (k > 32767) k = 32767;
1725 if (k < -32767) k = -32767;
1726 g->rxgain[j] = AST_LIN2A(k);
1733 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1735 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1736 if (k > 32767) k = 32767;
1737 if (k < -32767) k = -32767;
1738 g->rxgain[j] = AST_LIN2MU(k);
1747 static int set_actual_txgain(int fd, int chan, float gain, int law)
1752 memset(&g, 0, sizeof(g));
1754 res = ioctl(fd, ZT_GETGAINS, &g);
1756 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1760 fill_txgain(&g, gain, law);
1762 return ioctl(fd, ZT_SETGAINS, &g);
1765 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1770 memset(&g, 0, sizeof(g));
1772 res = ioctl(fd, ZT_GETGAINS, &g);
1774 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1778 fill_rxgain(&g, gain, law);
1780 return ioctl(fd, ZT_SETGAINS, &g);
1783 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1785 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1788 static int bump_gains(struct zt_pvt *p)
1792 /* Bump receive gain by value stored in cid_rxgain */
1793 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1795 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1802 static int restore_gains(struct zt_pvt *p)
1806 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1808 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1815 static inline int zt_set_hook(int fd, int hs)
1820 res = ioctl(fd, ZT_HOOK, &x);
1823 if (errno == EINPROGRESS)
1825 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1831 static inline int zt_confmute(struct zt_pvt *p, int muted)
1835 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
1837 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1839 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1841 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1843 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1847 static int save_conference(struct zt_pvt *p)
1849 struct zt_confinfo c;
1851 if (p->saveconf.confmode) {
1852 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1855 p->saveconf.chan = 0;
1856 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1858 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1859 p->saveconf.confmode = 0;
1864 c.confmode = ZT_CONF_NORMAL;
1865 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1867 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1870 ast_debug(1, "Disabled conferencing\n");
1875 * \brief Send MWI state change
1877 * \arg mailbox_full This is the mailbox associated with the FXO line that the
1878 * MWI state has changed on.
1879 * \arg thereornot This argument should simply be set to 1 or 0, to indicate
1880 * whether there are messages waiting or not.
1884 * This function does two things:
1886 * 1) It generates an internal Asterisk event notifying any other module that
1887 * cares about MWI that the state of a mailbox has changed.
1889 * 2) It runs the script specified by the mwimonitornotify option to allow
1890 * some custom handling of the state change.
1892 static void notify_message(char *mailbox_full, int thereornot)
1894 char s[sizeof(mwimonitornotify) + 80];
1895 struct ast_event *event;
1896 char *mailbox, *context;
1898 /* Strip off @default */
1899 context = mailbox = ast_strdupa(mailbox_full);
1900 strsep(&context, "@");
1901 if (ast_strlen_zero(context))
1902 context = "default";
1904 if (!(event = ast_event_new(AST_EVENT_MWI,
1905 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1906 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1907 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1908 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1909 AST_EVENT_IE_END))) {
1913 ast_event_queue_and_cache(event,
1914 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
1915 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR,
1918 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
1919 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
1924 static int restore_conference(struct zt_pvt *p)
1927 if (p->saveconf.confmode) {
1928 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1929 p->saveconf.confmode = 0;
1931 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1935 ast_debug(1, "Restored conferencing\n");
1939 static int send_callerid(struct zt_pvt *p);
1941 static int send_cwcidspill(struct zt_pvt *p)
1945 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1947 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1948 /* Make sure we account for the end */
1949 p->cidlen += READ_SIZE * 4;
1952 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1956 static int has_voicemail(struct zt_pvt *p)
1959 struct ast_event *event;
1960 char *mailbox, *context;
1962 mailbox = context = ast_strdupa(p->mailbox);
1963 strsep(&context, "@");
1964 if (ast_strlen_zero(context))
1965 context = "default";
1967 event = ast_event_get_cached(AST_EVENT_MWI,
1968 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1969 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1970 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
1974 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
1975 ast_event_destroy(event);
1977 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
1982 static int send_callerid(struct zt_pvt *p)
1984 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1986 /* Take out of linear mode if necessary */
1987 if (p->subs[SUB_REAL].linear) {
1988 p->subs[SUB_REAL].linear = 0;
1989 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1991 while (p->cidpos < p->cidlen) {
1992 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1994 if (errno == EAGAIN)
1997 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
2005 ast_free(p->cidspill);
2007 if (p->callwaitcas) {
2008 /* Wait for CID/CW to expire */
2009 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
2011 restore_conference(p);
2015 static int zt_callwait(struct ast_channel *ast)
2017 struct zt_pvt *p = ast->tech_pvt;
2018 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
2020 ast_log(LOG_WARNING, "Spill already exists?!?\n");
2021 ast_free(p->cidspill);
2023 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
2027 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
2028 if (!p->callwaitrings && p->callwaitingcallerid) {
2029 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
2031 p->cidlen = 2400 + 680 + READ_SIZE * 4;
2033 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
2035 p->cidlen = 2400 + READ_SIZE * 4;
2044 static unsigned char cid_pres2ss7pres(int cid_pres)
2046 return (cid_pres >> 5) & 0x03;
2049 static unsigned char cid_pres2ss7screen(int cid_pres)
2051 return cid_pres & 0x03;
2055 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
2057 struct zt_pvt *p = ast->tech_pvt;
2058 int x, res, index,mysig;
2063 char dest[256]; /* must be same length as p->dialdest */
2064 ast_mutex_lock(&p->lock);
2065 ast_copy_string(dest, rdest, sizeof(dest));
2066 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
2067 if ((ast->_state == AST_STATE_BUSY)) {
2068 p->subs[SUB_REAL].needbusy = 1;
2069 ast_mutex_unlock(&p->lock);
2072 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
2073 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
2074 ast_mutex_unlock(&p->lock);
2078 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
2080 /* Special pseudo -- automatically up */
2081 ast_setstate(ast, AST_STATE_UP);
2082 ast_mutex_unlock(&p->lock);
2085 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
2086 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
2088 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
2091 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
2094 if (p->outsigmod > -1)
2095 mysig = p->outsigmod;
2101 if (p->owner == ast) {
2102 /* Normal ring, on hook */
2104 /* Don't send audio while on hook, until the call is answered */
2106 if (p->use_callerid) {
2107 /* Generate the Caller-ID spill if desired */
2109 ast_log(LOG_WARNING, "cidspill already exists??\n");
2110 ast_free(p->cidspill);
2113 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2114 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2119 /* Choose proper cadence */
2120 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2121 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
2122 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
2123 p->cidrings = cidrings[p->distinctivering - 1];
2125 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
2126 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
2127 p->cidrings = p->sendcalleridafter;
2130 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2131 c = strchr(dest, '/');
2134 if (c && (strlen(c) < p->stripmsd)) {
2135 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2139 p->dop.op = ZT_DIAL_OP_REPLACE;
2140 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2141 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2143 p->dop.dialstr[0] = '\0';
2146 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2147 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2148 ast_mutex_unlock(&p->lock);
2153 /* Call waiting call */
2154 p->callwaitrings = 0;
2155 if (ast->cid.cid_num)
2156 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2158 p->callwait_num[0] = '\0';
2159 if (ast->cid.cid_name)
2160 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2162 p->callwait_name[0] = '\0';
2163 /* Call waiting tone instead */
2164 if (zt_callwait(ast)) {
2165 ast_mutex_unlock(&p->lock);
2168 /* Make ring-back */
2169 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2170 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2173 n = ast->cid.cid_name;
2174 l = ast->cid.cid_num;
2176 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2178 p->lastcid_num[0] = '\0';
2180 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2182 p->lastcid_name[0] = '\0';
2183 ast_setstate(ast, AST_STATE_RINGING);
2184 index = zt_get_index(ast, p, 0);
2186 p->subs[index].needringing = 1;
2199 case SIG_FGC_CAMAMF:
2204 case SIG_SF_FEATDMF:
2205 case SIG_FEATDMF_TA:
2207 c = strchr(dest, '/');
2212 if (strlen(c) < p->stripmsd) {
2213 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2214 ast_mutex_unlock(&p->lock);
2218 /* Start the trunk, if not GR-303 */
2222 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2224 if (errno != EINPROGRESS) {
2225 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2226 ast_mutex_unlock(&p->lock);
2233 ast_debug(1, "Dialing '%s'\n", c);
2234 p->dop.op = ZT_DIAL_OP_REPLACE;
2240 l = ast->cid.cid_num;
2242 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2244 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2247 l = ast->cid.cid_num;
2249 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2251 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2253 case SIG_FEATDMF_TA:
2255 const char *cic, *ozz;
2257 /* If you have to go through a Tandem Access point you need to use this */
2258 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2261 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2265 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2266 ast_mutex_unlock(&p->lock);
2269 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2270 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2275 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2278 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2280 case SIG_FGC_CAMAMF:
2282 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2286 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2288 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2292 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2293 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2294 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2295 p->echorest[sizeof(p->echorest) - 1] = '\0';
2297 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2301 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2303 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2304 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2305 ast_mutex_unlock(&p->lock);
2309 ast_debug(1, "Deferring dialing...\n");
2312 if (ast_strlen_zero(c))
2314 ast_setstate(ast, AST_STATE_DIALING);
2317 /* Special pseudo -- automatically up*/
2318 ast_setstate(ast, AST_STATE_UP);
2324 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2325 p->dialdest[0] = '\0';
2328 ast_debug(1, "not yet implemented\n");
2329 ast_mutex_unlock(&p->lock);
2334 char ss7_called_nai;
2335 int called_nai_strip;
2336 char ss7_calling_nai;
2337 int calling_nai_strip;
2338 const char *charge_str = NULL;
2339 const char *gen_address = NULL;
2340 const char *gen_digits = NULL;
2341 const char *gen_dig_type = NULL;
2342 const char *gen_dig_scheme = NULL;
2343 const char *jip_digits = NULL;
2344 const char *lspi_ident = NULL;
2345 const char *rlt_flag = NULL;
2346 const char *call_ref_id = NULL;
2347 const char *call_ref_pc = NULL;
2349 c = strchr(dest, '/');
2355 if (!p->hidecallerid) {
2356 l = ast->cid.cid_num;
2361 if (ss7_grab(p, p->ss7)) {
2362 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2363 ast_mutex_unlock(&p->lock);
2366 p->digital = IS_DIGITAL(ast->transfercapability);
2367 p->ss7call = isup_new_call(p->ss7->ss7);
2371 ast_mutex_unlock(&p->lock);
2372 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2376 called_nai_strip = 0;
2377 ss7_called_nai = p->ss7->called_nai;
2378 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2379 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2380 called_nai_strip = strlen(p->ss7->internationalprefix);
2381 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2382 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2383 called_nai_strip = strlen(p->ss7->nationalprefix);
2384 ss7_called_nai = SS7_NAI_NATIONAL;
2386 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2389 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2391 calling_nai_strip = 0;
2392 ss7_calling_nai = p->ss7->calling_nai;
2393 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2394 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2395 calling_nai_strip = strlen(p->ss7->internationalprefix);
2396 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2397 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2398 calling_nai_strip = strlen(p->ss7->nationalprefix);
2399 ss7_calling_nai = SS7_NAI_NATIONAL;
2401 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2404 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2405 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2406 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2408 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
2409 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2411 /* Set the charge number if it is set */
2412 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
2414 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
2416 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
2418 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 */
2420 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
2421 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
2422 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
2424 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
2426 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
2428 isup_set_jip_digits(p->ss7call, jip_digits);
2430 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
2432 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
2434 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
2435 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 ))
2436 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
2438 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
2439 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
2441 isup_set_callref(p->ss7call, atoi(call_ref_id),
2442 call_ref_pc ? atoi(call_ref_pc) : 0);
2445 isup_iam(p->ss7->ss7, p->ss7call);
2446 ast_setstate(ast, AST_STATE_DIALING);
2449 #endif /* HAVE_SS7 */
2453 #ifdef SUPPORT_USERUSER
2454 const char *useruser;
2458 int prilocaldialplan;
2462 int redirect_reason;
2464 c = strchr(dest, '/');
2473 if (!p->hidecallerid) {
2474 l = ast->cid.cid_num;
2475 if (!p->hidecalleridname) {
2476 n = ast->cid.cid_name;
2480 if (strlen(c) < p->stripmsd) {
2481 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2482 ast_mutex_unlock(&p->lock);
2485 if (mysig != SIG_FXSKS) {
2486 p->dop.op = ZT_DIAL_OP_REPLACE;
2487 s = strchr(c + p->stripmsd, 'w');
2490 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2492 p->dop.dialstr[0] = '\0';
2495 p->dop.dialstr[0] = '\0';
2498 if (pri_grab(p, p->pri)) {
2499 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2500 ast_mutex_unlock(&p->lock);
2503 if (!(p->call = pri_new_call(p->pri->pri))) {
2504 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2506 ast_mutex_unlock(&p->lock);
2509 if (!(sr = pri_sr_new())) {
2510 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2512 ast_mutex_unlock(&p->lock);
2514 if (p->bearer || (mysig == SIG_FXSKS)) {
2516 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2517 p->bearer->call = p->call;
2519 ast_debug(1, "I'm being setup with no bearer right now...\n");
2521 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2523 p->digital = IS_DIGITAL(ast->transfercapability);
2524 /* Add support for exclusive override */
2525 if (p->priexclusive)
2528 /* otherwise, traditional behavior */
2529 if (p->pri->nodetype == PRI_NETWORK)
2535 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2536 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2538 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2539 if (p->pri->facilityenable)
2540 pri_facility_enable(p->pri->pri);
2542 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2544 pridialplan = p->pri->dialplan - 1;
2545 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2546 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2547 if (pridialplan == -2) {
2548 dp_strip = strlen(p->pri->internationalprefix);
2550 pridialplan = PRI_INTERNATIONAL_ISDN;
2551 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2552 if (pridialplan == -2) {
2553 dp_strip = strlen(p->pri->nationalprefix);
2555 pridialplan = PRI_NATIONAL_ISDN;
2557 pridialplan = PRI_LOCAL_ISDN;
2560 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2561 switch (c[p->stripmsd]) {
2563 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2566 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2569 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2572 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2575 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2578 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2581 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2584 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2587 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2590 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2593 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2596 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2599 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2602 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2606 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2610 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2613 prilocaldialplan = p->pri->localdialplan - 1;
2614 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2615 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2616 if (prilocaldialplan == -2) {
2617 ldp_strip = strlen(p->pri->internationalprefix);
2619 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2620 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2621 if (prilocaldialplan == -2) {
2622 ldp_strip = strlen(p->pri->nationalprefix);
2624 prilocaldialplan = PRI_NATIONAL_ISDN;
2626 prilocaldialplan = PRI_LOCAL_ISDN;
2630 while (*l > '9' && *l != '*' && *l != '#') {
2633 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2636 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2639 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2642 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2645 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2648 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2651 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2654 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2657 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2660 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2663 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2666 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2669 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2672 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2676 ast_log(LOG_WARNING, "Unrecognized prilocaldialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2681 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2682 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2683 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2684 if (!strcasecmp(rr_str, "UNKNOWN"))
2685 redirect_reason = 0;
2686 else if (!strcasecmp(rr_str, "BUSY"))
2687 redirect_reason = 1;
2688 else if (!strcasecmp(rr_str, "NO_REPLY"))
2689 redirect_reason = 2;
2690 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2691 redirect_reason = 15;
2693 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2695 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2696 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2698 #ifdef SUPPORT_USERUSER
2699 /* User-user info */
2700 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2703 pri_sr_set_useruser(sr, useruser);
2706 if (pri_setup(p->pri->pri, p->call, sr)) {
2707 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2708 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2710 ast_mutex_unlock(&p->lock);
2715 ast_setstate(ast, AST_STATE_DIALING);
2719 ast_mutex_unlock(&p->lock);
2723 static void destroy_zt_pvt(struct zt_pvt **pvt)
2725 struct zt_pvt *p = *pvt;
2726 /* Remove channel from the list */
2728 p->prev->next = p->next;
2730 p->next->prev = p->prev;
2732 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2733 if (p->mwi_event_sub)
2734 ast_event_unsubscribe(p->mwi_event_sub);
2736 ast_variables_destroy(p->vars);
2737 ast_mutex_destroy(&p->lock);
2742 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2752 for (i = 0; i < 3; i++) {
2753 if (cur->subs[i].owner) {
2759 prev->next = cur->next;
2761 prev->next->prev = prev;
2767 iflist->prev = NULL;
2771 if (cur->subs[SUB_REAL].zfd > -1) {
2772 zt_close(cur->subs[SUB_REAL].zfd);
2774 destroy_zt_pvt(&cur);
2778 prev->next = cur->next;
2780 prev->next->prev = prev;
2786 iflist->prev = NULL;
2790 if (cur->subs[SUB_REAL].zfd > -1) {
2791 zt_close(cur->subs[SUB_REAL].zfd);
2793 destroy_zt_pvt(&cur);
2799 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2801 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2803 static char *zap_send_keypad_facility_descrip =
2804 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2805 " IE over the current channel.\n";
2807 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2809 /* Data will be our digit string */
2811 char *digits = (char *) data;
2813 if (ast_strlen_zero(digits)) {
2814 ast_debug(1, "No digit string sent to application!\n");
2818 p = (struct zt_pvt *)chan->tech_pvt;
2821 ast_debug(1, "Unable to find technology private\n");
2825 ast_mutex_lock(&p->lock);
2827 if (!p->pri || !p->call) {
2828 ast_debug(1, "Unable to find pri or call on channel!\n");
2829 ast_mutex_unlock(&p->lock);
2833 if (!pri_grab(p, p->pri)) {
2834 pri_keypad_facility(p->pri->pri, p->call, digits);
2837 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2838 ast_mutex_unlock(&p->lock);
2842 ast_mutex_unlock(&p->lock);
2847 static int pri_is_up(struct zt_pri *pri)
2850 for (x = 0; x < NUM_DCHANS; x++) {
2851 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2857 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2859 bearer->owner = &inuse;
2860 bearer->realcall = crv;
2861 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2862 if (crv->subs[SUB_REAL].owner)
2863 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].zfd);
2864 crv->bearer = bearer;
2865 crv->call = bearer->call;
2870 static char *pri_order(int level)
2880 return "Quaternary";
2886 /* Returns fd of the active dchan */
2887 static int pri_active_dchan_fd(struct zt_pri *pri)
2891 for (x = 0; x < NUM_DCHANS; x++) {
2892 if ((pri->dchans[x] == pri->pri))
2899 static int pri_find_dchan(struct zt_pri *pri)
2906 for (x = 0; x < NUM_DCHANS; x++) {
2907 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2909 if (pri->dchans[x] == old) {
2915 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2916 pri->dchannels[newslot]);
2918 if (old && (oldslot != newslot))
2919 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2920 pri->dchannels[oldslot], pri->dchannels[newslot]);
2921 pri->pri = pri->dchans[newslot];
2926 static int zt_hangup(struct ast_channel *ast)
2930 /*static int restore_gains(struct zt_pvt *p);*/
2931 struct zt_pvt *p = ast->tech_pvt;
2932 struct zt_pvt *tmp = NULL;
2933 struct zt_pvt *prev = NULL;
2936 ast_debug(1, "zt_hangup(%s)\n", ast->name);
2937 if (!ast->tech_pvt) {
2938 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2942 ast_mutex_lock(&p->lock);
2944 index = zt_get_index(ast, p, 1);
2946 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
2948 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2954 if (p->origcid_num) {
2955 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2956 ast_free(p->origcid_num);
2957 p->origcid_num = NULL;
2959 if (p->origcid_name) {
2960 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2961 ast_free(p->origcid_name);
2962 p->origcid_name = NULL;
2965 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2969 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2970 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2974 /* Real channel, do some fixup */
2975 p->subs[index].owner = NULL;
2976 p->subs[index].needanswer = 0;
2977 p->subs[index].needflash = 0;
2978 p->subs[index].needringing = 0;
2979 p->subs[index].needbusy = 0;
2980 p->subs[index].needcongestion = 0;
2981 p->subs[index].linear = 0;
2982 p->subs[index].needcallerid = 0;
2983 p->polarity = POLARITY_IDLE;
2984 zt_setlinear(p->subs[index].zfd, 0);
2985 if (index == SUB_REAL) {
2986 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2987 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
2988 if (p->subs[SUB_CALLWAIT].inthreeway) {
2989 /* We had flipped over to answer a callwait and now it's gone */
2990 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
2991 /* Move to the call-wait, but un-own us until they flip back. */
2992 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2993 unalloc_sub(p, SUB_CALLWAIT);
2996 /* The three way hung up, but we still have a call wait */
2997 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2998 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2999 unalloc_sub(p, SUB_THREEWAY);
3000 if (p->subs[SUB_REAL].inthreeway) {
3001 /* This was part of a three way call. Immediately make way for
3003 ast_debug(1, "Call was complete, setting owner to former third call\n");
3004 p->owner = p->subs[SUB_REAL].owner;
3006 /* This call hasn't been completed yet... Set owner to NULL */
3007 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
3010 p->subs[SUB_REAL].inthreeway = 0;
3012 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
3013 /* Move to the call-wait and switch back to them. */
3014 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3015 unalloc_sub(p, SUB_CALLWAIT);
3016 p->owner = p->subs[SUB_REAL].owner;
3017 if (p->owner->_state != AST_STATE_UP)
3018 p->subs[SUB_REAL].needanswer = 1;
3019 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
3020 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
3021 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
3022 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3023 unalloc_sub(p, SUB_THREEWAY);
3024 if (p->subs[SUB_REAL].inthreeway) {
3025 /* This was part of a three way call. Immediately make way for
3027 ast_debug(1, "Call was complete, setting owner to former third call\n");
3028 p->owner = p->subs[SUB_REAL].owner;
3030 /* This call hasn't been completed yet... Set owner to NULL */
3031 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
3034 p->subs[SUB_REAL].inthreeway = 0;
3036 } else if (index == SUB_CALLWAIT) {
3037 /* Ditch the holding callwait call, and immediately make it availabe */
3038 if (p->subs[SUB_CALLWAIT].inthreeway) {
3039 /* This is actually part of a three way, placed on hold. Place the third part
3040 on music on hold now */
3041 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
3042 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
3043 S_OR(p->mohsuggest, NULL),
3044 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3046 p->subs[SUB_THREEWAY].inthreeway = 0;
3047 /* Make it the call wait now */
3048 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
3049 unalloc_sub(p, SUB_THREEWAY);
3051 unalloc_sub(p, SUB_CALLWAIT);
3052 } else if (index == SUB_THREEWAY) {
3053 if (p->subs[SUB_CALLWAIT].inthreeway) {
3054 /* The other party of the three way call is currently in a call-wait state.
3055 Start music on hold for them, and take the main guy out of the third call */
3056 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
3057 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
3058 S_OR(p->mohsuggest, NULL),
3059 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3061 p->subs[SUB_CALLWAIT].inthreeway = 0;
3063 p->subs[SUB_REAL].inthreeway = 0;
3064 /* If this was part of a three way call index, let us make
3065 another three way call */
3066 unalloc_sub(p, SUB_THREEWAY);
3068 /* This wasn't any sort of call, but how are we an index? */
3069 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
3073 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
3076 p->distinctivering = 0;
3077 p->confirmanswer = 0;
3083 p->onhooktime = time(NULL);
3084 #if defined(HAVE_PRI) || defined(HAVE_SS7)
3092 ast_dsp_free(p->dsp);
3096 law = ZT_LAW_DEFAULT;
3097 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
3099 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
3100 /* Perform low level hangup if no owner left */
3104 if (!ss7_grab(p, p->ss7)) {
3105 if (!p->alreadyhungup) {
3106 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
3107 int icause = ast->hangupcause ? ast->hangupcause : -1;
3111 icause = atoi(cause);
3113 isup_rel(p->ss7->ss7, p->ss7call, icause);
3115 p->alreadyhungup = 1;
3117 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
3119 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
3127 #ifdef SUPPORT_USERUSER
3128 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
3131 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
3132 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
3133 if (!pri_grab(p, p->pri)) {
3134 if (p->alreadyhungup) {
3135 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
3137 #ifdef SUPPORT_USERUSER
3138 pri_call_set_useruser(p->call, useruser);
3141 pri_hangup(p->pri->pri, p->call, -1);
3144 p->bearer->call = NULL;
3146 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
3147 int icause = ast->hangupcause ? ast->hangupcause : -1;
3148 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
3150 #ifdef SUPPORT_USERUSER
3151 pri_call_set_useruser(p->call, useruser);
3154 p->alreadyhungup = 1;
3156 p->bearer->alreadyhungup = 1;
3159 icause = atoi(cause);
3161 pri_hangup(p->pri->pri, p->call, icause);
3164 ast_log(LOG_WARNING, "pri_disconnect failed\n");
3167 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3172 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
3178 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7) && (p->sig != SIG_BRI) && (p->sig != SIG_BRI_PTMP)))
3179 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
3181 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
3187 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
3190 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
3192 /* If they're off hook, try playing congestion */
3193 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
3194 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
3196 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3202 /* Make sure we're not made available for at least two seconds assuming
3203 we were actually used for an inbound or outbound call. */
3204 if (ast->_state != AST_STATE_RESERVED) {
3205 time(&p->guardtime);
3210 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3213 ast_free(p->cidspill);
3217 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
3218 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
3222 p->callwaiting = p->permcallwaiting;
3223 p->hidecallerid = p->permhidecallerid;
3228 /* Restore data mode */
3229 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
3231 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
3235 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
3236 /* Free up the bearer channel as well, and
3237 don't use its file descriptor anymore */
3238 update_conf(p->bearer);
3239 reset_conf(p->bearer);
3240 p->bearer->owner = NULL;
3241 p->bearer->realcall = NULL;
3243 p->subs[SUB_REAL].zfd = -1;
3250 p->callwaitingrepeat = 0;
3253 ast->tech_pvt = NULL;
3254 ast_mutex_unlock(&p->lock);
3255 ast_module_unref(ast_module_info->self);
3256 ast_verb(3, "Hungup '%s'\n", ast->name);
3258 ast_mutex_lock(&iflock);
3264 destroy_channel(prev, tmp, 0);
3272 ast_mutex_unlock(&iflock);
3276 static int zt_answer(struct ast_channel *ast)
3278 struct zt_pvt *p = ast->tech_pvt;
3281 int oldstate = ast->_state;
3282 ast_setstate(ast, AST_STATE_UP);
3283 ast_mutex_lock(&p->lock);
3284 index = zt_get_index(ast, p, 0);
3287 /* nothing to do if a radio channel */
3288 if ((p->radio || (p->oprmode < 0))) {
3289 ast_mutex_unlock(&p->lock);
3303 case SIG_FEATDMF_TA:
3306 case SIG_FGC_CAMAMF:
3311 case SIG_SF_FEATDMF:
3316 /* Pick up the line */
3317 ast_debug(1, "Took %s off hook\n", ast->name);
3318 if (p->hanguponpolarityswitch) {
3319 p->polaritydelaytv = ast_tvnow();
3321 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
3322 tone_zone_play_tone(p->subs[index].zfd, -1);
3324 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
3325 if (oldstate == AST_STATE_RINGING) {
3326 ast_debug(1, "Finally swapping real and threeway\n");
3327 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3328 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3329 p->owner = p->subs[SUB_REAL].owner;
3332 if (p->sig & __ZT_SIG_FXS) {
3341 /* Send a pri acknowledge */
3342 if (!pri_grab(p, p->pri)) {
3344 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3347 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3354 if (!ss7_grab(p, p->ss7)) {
3356 res = isup_anm(p->ss7->ss7, p->ss7call);
3359 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3365 ast_mutex_unlock(&p->lock);
3368 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3371 ast_mutex_unlock(&p->lock);
3375 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3381 struct zt_pvt *p = chan->tech_pvt, *pp;
3382 struct oprmode *oprmode;
3385 /* all supported options require data */
3386 if (!data || (datalen < 1)) {
3392 case AST_OPTION_TXGAIN:
3393 scp = (signed char *) data;
3394 index = zt_get_index(chan, p, 0);
3396 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
3399 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
3400 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
3401 case AST_OPTION_RXGAIN:
3402 scp = (signed char *) data;
3403 index = zt_get_index(chan, p, 0);
3405 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
3408 ast_debug(1, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
3409 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
3410 case AST_OPTION_TONE_VERIFY:
3416 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
3417 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
3420 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
3421 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
3424 ast_debug(1, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
3425 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */