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_DCHANS 4 /*!< No more than 4 d-channels */
210 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
212 #define CHAN_PSEUDO -2
214 #define DCHAN_PROVISIONED (1 << 0)
215 #define DCHAN_NOTINALARM (1 << 1)
216 #define DCHAN_UP (1 << 2)
218 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
220 /* Overlap dialing option types */
221 #define ZAP_OVERLAPDIAL_NONE 0
222 #define ZAP_OVERLAPDIAL_OUTGOING 1
223 #define ZAP_OVERLAPDIAL_INCOMING 2
224 #define ZAP_OVERLAPDIAL_BOTH (ZAP_OVERLAPDIAL_INCOMING|ZAP_OVERLAPDIAL_OUTGOING)
226 static char defaultcic[64] = "";
227 static char defaultozz[64] = "";
229 /*! Run this script when the MWI state changes on an FXO line, if mwimonitor is enabled */
230 static char mwimonitornotify[PATH_MAX] = "";
232 static char progzone[10] = "";
234 static int usedistinctiveringdetection = 0;
235 static int distinctiveringaftercid = 0;
237 static int numbufs = 4;
240 static struct ast_channel inuse;
241 #ifdef PRI_GETSET_TIMERS
242 static int pritimers[PRI_MAX_TIMERS];
244 static int pridebugfd = -1;
245 static char pridebugfilename[1024] = "";
248 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
249 static int firstdigittimeout = 16000;
251 /*! \brief How long to wait for following digits (FXO logic) */
252 static int gendigittimeout = 8000;
254 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
255 static int matchdigittimeout = 3000;
257 /*! \brief Protect the interface list (of zt_pvt's) */
258 AST_MUTEX_DEFINE_STATIC(iflock);
261 static int ifcount = 0;
264 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
267 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
268 when it's doing something critical. */
269 AST_MUTEX_DEFINE_STATIC(monlock);
271 /*! \brief This is the thread for the monitor which checks for input on the channels
272 which are not currently in use. */
273 static pthread_t monitor_thread = AST_PTHREADT_NULL;
275 static int restart_monitor(void);
277 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);
279 static int zt_sendtext(struct ast_channel *c, const char *text);
281 static void mwi_event_cb(const struct ast_event *event, void *userdata)
283 /* This module does not handle MWI in an event-based manner. However, it
284 * subscribes to MWI for each mailbox that is configured so that the core
285 * knows that we care about it. Then, chan_zap will get the MWI from the
286 * event cache instead of checking the mailbox directly. */
289 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
290 static inline int zt_get_event(int fd)
293 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
298 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
299 static inline int zt_wait_event(int fd)
302 i = ZT_IOMUX_SIGEVENT;
303 if (ioctl(fd, ZT_IOMUX, &i) == -1)
305 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
310 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
311 #define READ_SIZE 160
313 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
314 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
316 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
317 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
318 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
319 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
320 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
324 static int ringt_base = DEFAULT_RINGT;
328 #define LINKSTATE_INALARM (1 << 0)
329 #define LINKSTATE_STARTING (1 << 1)
330 #define LINKSTATE_UP (1 << 2)
331 #define LINKSTATE_DOWN (1 << 3)
333 #define SS7_NAI_DYNAMIC -1
336 pthread_t master; /*!< Thread of master */
340 int linkstate[NUM_DCHANS];
344 LINKSET_STATE_DOWN = 0,
347 char called_nai; /*!< Called Nature of Address Indicator */
348 char calling_nai; /*!< Calling Nature of Address Indicator */
349 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
350 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
351 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
352 char unknownprefix[20]; /*!< for unknown dialplans */
354 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
357 static struct zt_ss7 linksets[NUM_SPANS];
359 static int cur_ss7type = -1;
360 static int cur_linkset = -1;
361 static int cur_pointcode = -1;
362 static int cur_cicbeginswith = -1;
363 static int cur_adjpointcode = -1;
364 static int cur_networkindicator = -1;
365 static int cur_defaultdpc = -1;
366 #endif /* HAVE_SS7 */
370 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
371 #define PRI_CHANNEL(p) ((p) & 0xff)
372 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
373 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
376 pthread_t master; /*!< Thread of master */
377 ast_mutex_t lock; /*!< Mutex */
378 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
379 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
380 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
381 int minunused; /*!< Min # of channels to keep empty */
382 int minidle; /*!< Min # of "idling" calls to keep active */
383 int nodetype; /*!< Node type */
384 int switchtype; /*!< Type of switch to emulate */
385 int nsf; /*!< Network-Specific Facilities */
386 int dialplan; /*!< Dialing plan */
387 int localdialplan; /*!< Local dialing plan */
388 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
389 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
390 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
391 char privateprefix[20]; /*!< for private dialplans */
392 char unknownprefix[20]; /*!< for unknown dialplans */
393 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
394 int trunkgroup; /*!< What our trunkgroup is */
395 int mastertrunkgroup; /*!< What trunk group is our master */
396 int prilogicalspan; /*!< Logical span number within trunk group */
397 int numchans; /*!< Num of channels we represent */
398 int overlapdial; /*!< In overlap dialing mode */
399 int facilityenable; /*!< Enable facility IEs */
400 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
401 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
402 struct pri *pri; /*!< Currently active D-channel */
404 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
409 time_t lastreset; /*!< time when unused channels were last reset */
410 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
412 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
413 struct zt_pvt *crvs; /*!< Member CRV structs */
414 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
418 static struct zt_pri pris[NUM_SPANS];
421 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
423 #define DEFAULT_PRI_DEBUG 0
426 static inline void pri_rel(struct zt_pri *pri)
428 ast_mutex_unlock(&pri->lock);
432 /*! Shut up the compiler */
436 #define SUB_REAL 0 /*!< Active call */
437 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
438 #define SUB_THREEWAY 2 /*!< Three-way call */
440 /* Polarity states */
441 #define POLARITY_IDLE 0
442 #define POLARITY_REV 1
445 static struct zt_distRings drings;
447 struct distRingData {
451 struct ringContextData {
452 char contextData[AST_MAX_CONTEXT];
454 struct zt_distRings {
455 struct distRingData ringnum[3];
456 struct ringContextData ringContext[3];
459 static char *subnames[] = {
465 struct zt_subchannel {
467 struct ast_channel *owner;
469 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
470 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
471 unsigned int needringing:1;
472 unsigned int needbusy:1;
473 unsigned int needcongestion:1;
474 unsigned int needcallerid:1;
475 unsigned int needanswer:1;
476 unsigned int needflash:1;
477 unsigned int needhold:1;
478 unsigned int needunhold:1;
479 unsigned int linear:1;
480 unsigned int inthreeway:1;
484 #define CONF_USER_REAL (1 << 0)
485 #define CONF_USER_THIRDCALL (1 << 1)
489 static struct zt_pvt {
491 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
492 /*!< Up to three channels can be associated with this call */
494 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
495 struct zt_subchannel subs[3]; /*!< Sub-channels */
496 struct zt_confinfo saveconf; /*!< Saved conference info */
498 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
499 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
500 int inconference; /*!< If our real should be in the conference */
502 int sig; /*!< Signalling style */
503 int radio; /*!< radio type */
504 int outsigmod; /*!< Outbound Signalling style (modifier) */
505 int oprmode; /*!< "Operator Services" mode */
506 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
507 float cid_rxgain; /*!< "Gain to apply during caller id */
510 int tonezone; /*!< tone zone for this chan, or -1 for default */
511 struct zt_pvt *next; /*!< Next channel in list */
512 struct zt_pvt *prev; /*!< Prev channel in list */
516 unsigned int answeronpolarityswitch:1;
517 unsigned int busydetect:1;
518 unsigned int callreturn:1;
519 unsigned int callwaiting:1;
520 unsigned int callwaitingcallerid:1;
521 unsigned int cancallforward:1;
522 unsigned int canpark:1;
523 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
524 unsigned int destroy:1;
525 unsigned int didtdd:1; /*!< flag to say its done it once */
526 unsigned int dialednone:1;
527 unsigned int dialing:1;
528 unsigned int digital:1;
530 unsigned int echobreak:1;
531 unsigned int echocanbridged:1;
532 unsigned int echocanon:1;
533 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
534 unsigned int firstradio:1;
535 unsigned int hanguponpolarityswitch:1;
536 unsigned int hardwaredtmf:1;
537 unsigned int hidecallerid:1;
538 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
539 unsigned int ignoredtmf:1;
540 unsigned int immediate:1; /*!< Answer before getting digits? */
541 unsigned int inalarm:1;
542 unsigned int mate:1; /*!< flag to say its in MATE mode */
543 unsigned int outgoing:1;
544 /* unsigned int overlapdial:1; unused and potentially confusing */
545 unsigned int permcallwaiting:1;
546 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
547 unsigned int priindication_oob:1;
548 unsigned int priexclusive:1;
549 unsigned int pulse:1;
550 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
551 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
552 unsigned int threewaycalling:1;
553 unsigned int transfer:1;
554 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
555 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
556 unsigned int usedistinctiveringdetection:1;
557 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
558 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
559 unsigned int mwimonitor:1;
560 /* Channel state or unavilability flags */
561 unsigned int inservice:1;
562 unsigned int locallyblocked:1;
563 unsigned int remotelyblocked:1;
564 #if defined(HAVE_PRI) || defined(HAVE_SS7)
565 unsigned int alerting:1;
566 unsigned int alreadyhungup:1;
567 unsigned int isidlecall:1;
568 unsigned int proceeding:1;
569 unsigned int progress:1;
570 unsigned int resetting:1;
571 unsigned int setup_ack:1;
573 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
574 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
576 struct zt_distRings drings;
578 char context[AST_MAX_CONTEXT];
579 char defcontext[AST_MAX_CONTEXT];
580 char exten[AST_MAX_EXTENSION];
581 char language[MAX_LANGUAGE];
582 char mohinterpret[MAX_MUSICCLASS];
583 char mohsuggest[MAX_MUSICCLASS];
584 #if defined(PRI_ANI) || defined(HAVE_SS7)
585 char cid_ani[AST_MAX_EXTENSION];
588 char cid_num[AST_MAX_EXTENSION];
589 int cid_ton; /*!< Type Of Number (TON) */
590 char cid_name[AST_MAX_EXTENSION];
591 char lastcid_num[AST_MAX_EXTENSION];
592 char lastcid_name[AST_MAX_EXTENSION];
593 char *origcid_num; /*!< malloced original callerid */
594 char *origcid_name; /*!< malloced original callerid */
595 char callwait_num[AST_MAX_EXTENSION];
596 char callwait_name[AST_MAX_EXTENSION];
597 char rdnis[AST_MAX_EXTENSION];
598 char dnid[AST_MAX_EXTENSION];
601 int confno; /*!< Our conference */
602 int confusers; /*!< Who is using our conference */
603 int propconfno; /*!< Propagated conference number */
604 ast_group_t callgroup;
605 ast_group_t pickupgroup;
606 struct ast_variable *vars;
607 int channel; /*!< Channel Number or CRV */
608 int span; /*!< Span number */
609 time_t guardtime; /*!< Must wait this much time before using for new call */
610 int cid_signalling; /*!< CID signalling type bell202 or v23 */
611 int cid_start; /*!< CID start indicator, polarity or ring */
612 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
613 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
614 int cidcwexpire; /*!< When to expire our muting for CID/CW */
615 unsigned char *cidspill;
616 struct callerid_state *mwi_state;
629 int busy_quietlength;
631 struct timeval flashtime; /*!< Last flash-hook time */
633 int cref; /*!< Call reference number */
634 ZT_DIAL_OPERATION dop;
635 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
637 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
638 int amaflags; /*!< AMA Flags */
639 struct tdd_state *tdd; /*!< TDD flag */
640 char call_forward[AST_MAX_EXTENSION];
641 char mailbox[AST_MAX_EXTENSION];
642 struct ast_event_sub *mwi_event_sub;
646 int distinctivering; /*!< Which distinctivering to use */
647 int cidrings; /*!< Which ring to deliver CID on */
648 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
650 int polarityonanswerdelay;
651 struct timeval polaritydelaytv;
652 int sendcalleridafter;
655 struct zt_pvt *bearer;
656 struct zt_pvt *realcall;
665 struct isup_call *ss7call;
666 char charge_number[50];
667 char gen_add_number[50];
668 unsigned char gen_add_num_plan;
669 unsigned char gen_add_nai;
670 unsigned char gen_add_pres_ind;
671 unsigned char gen_add_type;
673 int cic; /*!< CIC associated with channel */
674 unsigned int dpc; /*!< CIC's DPC */
675 unsigned int loopedback:1;
678 } *iflist = NULL, *ifend = NULL;
680 /*! \brief Channel configuration from zapata.conf .
681 * This struct is used for parsing the [channels] section of zapata.conf.
682 * Generally there is a field here for every possible configuration item.
684 * The state of fields is saved along the parsing and whenever a 'channel'
685 * statement is reached, the current zt_chan_conf is used to configure the
686 * channel (struct zt_pvt)
688 * \see zt_chan_init for the default values.
690 struct zt_chan_conf {
701 char smdi_port[SMDI_MAX_FILENAME_LEN];
704 /** returns a new zt_chan_conf with default values (by-value) */
705 static struct zt_chan_conf zt_chan_conf_default(void) {
706 /* recall that if a field is not included here it is initialized
709 struct zt_chan_conf conf = {
713 .switchtype = PRI_SWITCH_NI2,
714 .dialplan = PRI_NATIONAL_ISDN + 1,
715 .localdialplan = PRI_NATIONAL_ISDN + 1,
721 .internationalprefix = "",
722 .nationalprefix = "",
731 .called_nai = SS7_NAI_NATIONAL,
732 .calling_nai = SS7_NAI_NATIONAL,
733 .internationalprefix = "",
734 .nationalprefix = "",
735 .subscriberprefix = "",
740 .context = "default",
743 .mohinterpret = "default",
747 .cid_signalling = CID_SIG_BELL,
748 .cid_start = CID_START_RING,
767 .polarityonanswerdelay = 600,
769 .sendcalleridafter = DEFAULT_CIDRINGS
781 .smdi_port = "/dev/ttyS0",
788 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
789 static int zt_digit_begin(struct ast_channel *ast, char digit);
790 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
791 static int zt_sendtext(struct ast_channel *c, const char *text);
792 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
793 static int zt_hangup(struct ast_channel *ast);
794 static int zt_answer(struct ast_channel *ast);
795 static struct ast_frame *zt_read(struct ast_channel *ast);
796 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
797 static struct ast_frame *zt_exception(struct ast_channel *ast);
798 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
799 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
800 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
801 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
803 static const struct ast_channel_tech zap_tech = {
805 .description = tdesc,
806 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
807 .requester = zt_request,
808 .send_digit_begin = zt_digit_begin,
809 .send_digit_end = zt_digit_end,
810 .send_text = zt_sendtext,
817 .exception = zt_exception,
818 .indicate = zt_indicate,
820 .setoption = zt_setoption,
821 .func_channel_read = zt_func_read,
825 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
827 #define GET_CHANNEL(p) ((p)->channel)
830 struct zt_pvt *round_robin[32];
833 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
836 /* Grab the lock first */
838 res = ast_mutex_trylock(&pri->lock);
840 ast_mutex_unlock(&pvt->lock);
841 /* Release the lock and try again */
843 ast_mutex_lock(&pvt->lock);
846 /* Then break the poll */
847 pthread_kill(pri->master, SIGURG);
853 static inline void ss7_rel(struct zt_ss7 *ss7)
855 ast_mutex_unlock(&ss7->lock);
858 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
861 /* Grab the lock first */
863 res = ast_mutex_trylock(&pri->lock);
865 ast_mutex_unlock(&pvt->lock);
866 /* Release the lock and try again */
868 ast_mutex_lock(&pvt->lock);
871 /* Then break the poll */
872 pthread_kill(pri->master, SIGURG);
876 #define NUM_CADENCE_MAX 25
877 static int num_cadence = 4;
878 static int user_has_defined_cadences = 0;
880 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
881 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
882 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
883 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
884 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
887 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
888 * is 1, the second pause is 2 and so on.
891 static int cidrings[NUM_CADENCE_MAX] = {
892 2, /*!< Right after first long ring */
893 4, /*!< Right after long part */
894 3, /*!< After third chirp */
895 2, /*!< Second spell */
898 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
899 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
901 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
902 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
904 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
907 if (p->subs[0].owner == ast)
909 else if (p->subs[1].owner == ast)
911 else if (p->subs[2].owner == ast)
916 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
922 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
924 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
929 ast_mutex_unlock(&pri->lock);
932 if (p->subs[a].owner) {
933 if (ast_channel_trylock(p->subs[a].owner)) {
934 ast_mutex_unlock(&p->lock);
936 ast_mutex_lock(&p->lock);
938 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
939 ast_channel_unlock(p->subs[a].owner);
947 ast_mutex_lock(&pri->lock);
951 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
954 struct zt_pri *pri = (struct zt_pri*) data;
957 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
959 /* We must unlock the PRI to avoid the possibility of a deadlock */
960 #if defined(HAVE_PRI) || defined(HAVE_SS7)
967 ast_mutex_unlock(&pri->lock);
972 ast_mutex_unlock(&ss7->lock);
982 if (ast_channel_trylock(p->owner)) {
983 ast_mutex_unlock(&p->lock);
985 ast_mutex_lock(&p->lock);
987 ast_queue_frame(p->owner, f);
988 ast_channel_unlock(p->owner);
994 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1001 ast_mutex_lock(&pri->lock);
1006 ast_mutex_lock(&ss7->lock);
1017 static int restore_gains(struct zt_pvt *p);
1019 static void swap_subs(struct zt_pvt *p, int a, int b)
1023 struct ast_channel *towner;
1025 ast_debug(1, "Swapping %d and %d\n", a, b);
1027 tchan = p->subs[a].chan;
1028 towner = p->subs[a].owner;
1029 tinthreeway = p->subs[a].inthreeway;
1031 p->subs[a].chan = p->subs[b].chan;
1032 p->subs[a].owner = p->subs[b].owner;
1033 p->subs[a].inthreeway = p->subs[b].inthreeway;
1035 p->subs[b].chan = tchan;
1036 p->subs[b].owner = towner;
1037 p->subs[b].inthreeway = tinthreeway;
1039 if (p->subs[a].owner)
1040 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].zfd);
1041 if (p->subs[b].owner)
1042 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].zfd);
1043 wakeup_sub(p, a, NULL);
1044 wakeup_sub(p, b, NULL);
1047 static int zt_open(char *fn)
1055 for (x = 0; x < strlen(fn); x++) {
1056 if (!isdigit(fn[x])) {
1064 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1067 fn = "/dev/zap/channel";
1069 fd = open(fn, O_RDWR | O_NONBLOCK);
1071 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1075 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1079 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1084 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) {
1085 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
1094 static void zt_close(int fd)
1100 static int zt_setlinear(int zfd, int linear)
1103 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1110 static int alloc_sub(struct zt_pvt *p, int x)
1114 if (p->subs[x].zfd < 0) {
1115 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1116 if (p->subs[x].zfd > -1) {
1117 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1119 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1120 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1121 bi.numbufs = numbufs;
1122 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1124 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1127 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1128 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1129 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1130 zt_close(p->subs[x].zfd);
1131 p->subs[x].zfd = -1;
1134 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1137 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1140 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1144 static int unalloc_sub(struct zt_pvt *p, int x)
1147 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1150 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1151 if (p->subs[x].zfd > -1) {
1152 zt_close(p->subs[x].zfd);
1154 p->subs[x].zfd = -1;
1155 p->subs[x].linear = 0;
1156 p->subs[x].chan = 0;
1157 p->subs[x].owner = NULL;
1158 p->subs[x].inthreeway = 0;
1159 p->polarity = POLARITY_IDLE;
1160 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1164 static int digit_to_dtmfindex(char digit)
1167 return ZT_TONE_DTMF_BASE + (digit - '0');
1168 else if (digit >= 'A' && digit <= 'D')
1169 return ZT_TONE_DTMF_A + (digit - 'A');
1170 else if (digit >= 'a' && digit <= 'd')
1171 return ZT_TONE_DTMF_A + (digit - 'a');
1172 else if (digit == '*')
1173 return ZT_TONE_DTMF_s;
1174 else if (digit == '#')
1175 return ZT_TONE_DTMF_p;
1180 static int zt_digit_begin(struct ast_channel *chan, char digit)
1186 pvt = chan->tech_pvt;
1188 ast_mutex_lock(&pvt->lock);
1190 index = zt_get_index(chan, pvt, 0);
1192 if ((index != SUB_REAL) || !pvt->owner)
1196 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1197 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1198 if (pvt->setup_ack) {
1199 if (!pri_grab(pvt, pvt->pri)) {
1200 pri_information(pvt->pri->pri, pvt->call, digit);
1203 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1204 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1206 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1207 res = strlen(pvt->dialdest);
1208 pvt->dialdest[res++] = digit;
1209 pvt->dialdest[res] = '\0';
1214 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1217 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1219 ZT_DIAL_OPERATION zo = {
1220 .op = ZT_DIAL_OP_APPEND,
1223 zo.dialstr[0] = 'T';
1224 zo.dialstr[1] = digit;
1225 zo.dialstr[2] = '\0';
1226 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1227 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1231 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1233 pvt->begindigit = digit;
1237 ast_mutex_unlock(&pvt->lock);
1242 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1249 pvt = chan->tech_pvt;
1251 ast_mutex_lock(&pvt->lock);
1253 index = zt_get_index(chan, pvt, 0);
1255 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1259 /* This means that the digit was already sent via PRI signalling */
1260 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1261 && !pvt->begindigit)
1265 if (pvt->begindigit) {
1267 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1268 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1270 pvt->begindigit = 0;
1274 ast_mutex_unlock(&pvt->lock);
1279 static char *events[] = {
1292 "Hook Transition Complete",
1297 "Polarity Reversal",
1305 { ZT_ALARM_RED, "Red Alarm" },
1306 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1307 { ZT_ALARM_BLUE, "Blue Alarm" },
1308 { ZT_ALARM_RECOVER, "Recovering" },
1309 { ZT_ALARM_LOOPBACK, "Loopback" },
1310 { ZT_ALARM_NOTOPEN, "Not Open" },
1311 { ZT_ALARM_NONE, "None" },
1314 static char *alarm2str(int alarm)
1317 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1318 if (alarms[x].alarm & alarm)
1319 return alarms[x].name;
1321 return alarm ? "Unknown Alarm" : "No Alarm";
1324 static char *event2str(int event)
1326 static char buf[256];
1327 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1328 return events[event];
1329 sprintf(buf, "Event %d", event); /* safe */
1334 static char *dialplan2str(int dialplan)
1336 if (dialplan == -1 || dialplan == -2) {
1337 return("Dynamically set dialplan in ISDN");
1339 return (pri_plan2str(dialplan));
1343 static char *zap_sig2str(int sig)
1345 static char buf[256];
1348 return "E & M Immediate";
1350 return "E & M Wink";
1354 return "Feature Group D (DTMF)";
1356 return "Feature Group D (MF)";
1357 case SIG_FEATDMF_TA:
1358 return "Feature Groud D (MF) Tandem Access";
1360 return "Feature Group B (MF)";
1364 return "FGC/CAMA (Dialpulse)";
1365 case SIG_FGC_CAMAMF:
1366 return "FGC/CAMA (MF)";
1368 return "FXS Loopstart";
1370 return "FXS Groundstart";
1372 return "FXS Kewlstart";
1374 return "FXO Loopstart";
1376 return "FXO Groundstart";
1378 return "FXO Kewlstart";
1382 return "ISDN BRI Point to Point";
1384 return "ISDN BRI Point to MultiPoint";
1388 return "SF (Tone) Immediate";
1390 return "SF (Tone) Wink";
1392 return "SF (Tone) with Feature Group D (DTMF)";
1393 case SIG_SF_FEATDMF:
1394 return "SF (Tone) with Feature Group D (MF)";
1396 return "SF (Tone) with Feature Group B (MF)";
1397 case SIG_GR303FXOKS:
1398 return "GR-303 with FXOKS";
1399 case SIG_GR303FXSKS:
1400 return "GR-303 with FXSKS";
1404 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1409 #define sig2str zap_sig2str
1411 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1413 /* If the conference already exists, and we're already in it
1414 don't bother doing anything */
1417 memset(&zi, 0, sizeof(zi));
1420 if (slavechannel > 0) {
1421 /* If we have only one slave, do a digital mon */
1422 zi.confmode = ZT_CONF_DIGITALMON;
1423 zi.confno = slavechannel;
1426 /* Real-side and pseudo-side both participate in conference */
1427 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1428 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1430 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1431 zi.confno = p->confno;
1433 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1437 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1438 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1441 if (slavechannel < 1) {
1442 p->confno = zi.confno;
1444 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1445 ast_debug(1, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1449 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1451 /* If they're listening to our channel, they're ours */
1452 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1454 /* If they're a talker on our (allocated) conference, they're ours */
1455 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1460 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1463 if (/* Can't delete if there's no zfd */
1465 /* Don't delete from the conference if it's not our conference */
1467 /* Don't delete if we don't think it's conferenced at all (implied) */
1469 memset(&zi, 0, sizeof(zi));
1473 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1474 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1477 ast_debug(1, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1478 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1482 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1486 struct zt_pvt *slave = NULL;
1487 /* Start out optimistic */
1489 /* Update conference state in a stateless fashion */
1490 for (x = 0; x < 3; x++) {
1491 /* Any three-way calling makes slave native mode *definitely* out
1493 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1496 /* If we don't have any 3-way calls, check to see if we have
1497 precisely one slave */
1498 if (useslavenative) {
1499 for (x = 0; x < MAX_SLAVES; x++) {
1502 /* Whoops already have a slave! No
1503 slave native and stop right away */
1508 /* We have one slave so far */
1509 slave = p->slaves[x];
1514 /* If no slave, slave native definitely out */
1517 else if (slave->law != p->law) {
1523 return useslavenative;
1526 static int reset_conf(struct zt_pvt *p)
1529 memset(&zi, 0, sizeof(zi));
1531 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1532 if (p->subs[SUB_REAL].zfd > -1) {
1533 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1534 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1539 static int update_conf(struct zt_pvt *p)
1544 struct zt_pvt *slave = NULL;
1546 useslavenative = isslavenative(p, &slave);
1547 /* Start with the obvious, general stuff */
1548 for (x = 0; x < 3; x++) {
1549 /* Look for three way calls */
1550 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1551 conf_add(p, &p->subs[x], x, 0);
1554 conf_del(p, &p->subs[x], x);
1557 /* If we have a slave, add him to our conference now. or DAX
1558 if this is slave native */
1559 for (x = 0; x < MAX_SLAVES; x++) {
1562 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1564 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1569 /* If we're supposed to be in there, do so now */
1570 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1572 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1574 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1578 /* If we have a master, add ourselves to his conference */
1580 if (isslavenative(p->master, NULL)) {
1581 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1583 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1587 /* Nobody is left (or should be left) in our conference.
1591 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1595 static void zt_enable_ec(struct zt_pvt *p)
1602 ast_debug(1, "Echo cancellation already on\n");
1606 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1609 if (p->echocancel) {
1610 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1612 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1614 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1617 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1619 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1622 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1625 ast_debug(1, "No echo cancellation requested\n");
1628 static void zt_train_ec(struct zt_pvt *p)
1632 if (p && p->echocancel && p->echotraining) {
1633 x = p->echotraining;
1634 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1636 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1638 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1640 ast_debug(1, "No echo training requested\n");
1643 static void zt_disable_ec(struct zt_pvt *p)
1647 if (p->echocancel) {
1649 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1651 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1653 ast_debug(1, "disabled echo cancellation on channel %d\n", p->channel);
1658 static void fill_txgain(struct zt_gains *g, float gain, int law)
1662 float linear_gain = pow(10.0, gain / 20.0);
1666 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1668 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1669 if (k > 32767) k = 32767;
1670 if (k < -32767) k = -32767;
1671 g->txgain[j] = AST_LIN2A(k);
1678 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1680 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1681 if (k > 32767) k = 32767;
1682 if (k < -32767) k = -32767;
1683 g->txgain[j] = AST_LIN2MU(k);
1692 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1696 float linear_gain = pow(10.0, gain / 20.0);
1700 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1702 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1703 if (k > 32767) k = 32767;
1704 if (k < -32767) k = -32767;
1705 g->rxgain[j] = AST_LIN2A(k);
1712 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1714 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1715 if (k > 32767) k = 32767;
1716 if (k < -32767) k = -32767;
1717 g->rxgain[j] = AST_LIN2MU(k);
1726 static int set_actual_txgain(int fd, int chan, float gain, int law)
1731 memset(&g, 0, sizeof(g));
1733 res = ioctl(fd, ZT_GETGAINS, &g);
1735 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1739 fill_txgain(&g, gain, law);
1741 return ioctl(fd, ZT_SETGAINS, &g);
1744 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1749 memset(&g, 0, sizeof(g));
1751 res = ioctl(fd, ZT_GETGAINS, &g);
1753 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1757 fill_rxgain(&g, gain, law);
1759 return ioctl(fd, ZT_SETGAINS, &g);
1762 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1764 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1767 static int bump_gains(struct zt_pvt *p)
1771 /* Bump receive gain by value stored in cid_rxgain */
1772 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1774 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1781 static int restore_gains(struct zt_pvt *p)
1785 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1787 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1794 static inline int zt_set_hook(int fd, int hs)
1799 res = ioctl(fd, ZT_HOOK, &x);
1802 if (errno == EINPROGRESS)
1804 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1810 static inline int zt_confmute(struct zt_pvt *p, int muted)
1814 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
1816 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1818 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1820 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1822 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1826 static int save_conference(struct zt_pvt *p)
1828 struct zt_confinfo c;
1830 if (p->saveconf.confmode) {
1831 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1834 p->saveconf.chan = 0;
1835 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1837 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1838 p->saveconf.confmode = 0;
1843 c.confmode = ZT_CONF_NORMAL;
1844 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1846 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1849 ast_debug(1, "Disabled conferencing\n");
1854 * \brief Send MWI state change
1856 * \arg mailbox_full This is the mailbox associated with the FXO line that the
1857 * MWI state has changed on.
1858 * \arg thereornot This argument should simply be set to 1 or 0, to indicate
1859 * whether there are messages waiting or not.
1863 * This function does two things:
1865 * 1) It generates an internal Asterisk event notifying any other module that
1866 * cares about MWI that the state of a mailbox has changed.
1868 * 2) It runs the script specified by the mwimonitornotify option to allow
1869 * some custom handling of the state change.
1871 static void notify_message(char *mailbox_full, int thereornot)
1873 char s[sizeof(mwimonitornotify) + 80];
1874 struct ast_event *event;
1875 char *mailbox, *context;
1877 /* Strip off @default */
1878 context = mailbox = ast_strdupa(mailbox_full);
1879 strsep(&context, "@");
1880 if (ast_strlen_zero(context))
1881 context = "default";
1883 if (!(event = ast_event_new(AST_EVENT_MWI,
1884 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1885 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1886 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1887 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1888 AST_EVENT_IE_END))) {
1892 ast_event_queue_and_cache(event,
1893 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
1894 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR,
1897 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
1898 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
1903 static int restore_conference(struct zt_pvt *p)
1906 if (p->saveconf.confmode) {
1907 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1908 p->saveconf.confmode = 0;
1910 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1914 ast_debug(1, "Restored conferencing\n");
1918 static int send_callerid(struct zt_pvt *p);
1920 static int send_cwcidspill(struct zt_pvt *p)
1924 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1926 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1927 /* Make sure we account for the end */
1928 p->cidlen += READ_SIZE * 4;
1931 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1935 static int has_voicemail(struct zt_pvt *p)
1938 struct ast_event *event;
1939 char *mailbox, *context;
1941 mailbox = context = ast_strdupa(p->mailbox);
1942 strsep(&context, "@");
1943 if (ast_strlen_zero(context))
1944 context = "default";
1946 event = ast_event_get_cached(AST_EVENT_MWI,
1947 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1948 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1949 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
1953 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
1954 ast_event_destroy(event);
1956 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
1961 static int send_callerid(struct zt_pvt *p)
1963 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1965 /* Take out of linear mode if necessary */
1966 if (p->subs[SUB_REAL].linear) {
1967 p->subs[SUB_REAL].linear = 0;
1968 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1970 while (p->cidpos < p->cidlen) {
1971 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1973 if (errno == EAGAIN)
1976 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1984 ast_free(p->cidspill);
1986 if (p->callwaitcas) {
1987 /* Wait for CID/CW to expire */
1988 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1990 restore_conference(p);
1994 static int zt_callwait(struct ast_channel *ast)
1996 struct zt_pvt *p = ast->tech_pvt;
1997 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1999 ast_log(LOG_WARNING, "Spill already exists?!?\n");
2000 ast_free(p->cidspill);
2002 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
2006 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
2007 if (!p->callwaitrings && p->callwaitingcallerid) {
2008 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
2010 p->cidlen = 2400 + 680 + READ_SIZE * 4;
2012 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
2014 p->cidlen = 2400 + READ_SIZE * 4;
2023 static unsigned char cid_pres2ss7pres(int cid_pres)
2025 return (cid_pres >> 5) & 0x03;
2028 static unsigned char cid_pres2ss7screen(int cid_pres)
2030 return cid_pres & 0x03;
2034 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
2036 struct zt_pvt *p = ast->tech_pvt;
2037 int x, res, index,mysig;
2042 char dest[256]; /* must be same length as p->dialdest */
2043 ast_mutex_lock(&p->lock);
2044 ast_copy_string(dest, rdest, sizeof(dest));
2045 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
2046 if ((ast->_state == AST_STATE_BUSY)) {
2047 p->subs[SUB_REAL].needbusy = 1;
2048 ast_mutex_unlock(&p->lock);
2051 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
2052 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
2053 ast_mutex_unlock(&p->lock);
2057 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
2059 /* Special pseudo -- automatically up */
2060 ast_setstate(ast, AST_STATE_UP);
2061 ast_mutex_unlock(&p->lock);
2064 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
2065 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
2067 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
2070 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
2073 if (p->outsigmod > -1)
2074 mysig = p->outsigmod;
2080 if (p->owner == ast) {
2081 /* Normal ring, on hook */
2083 /* Don't send audio while on hook, until the call is answered */
2085 if (p->use_callerid) {
2086 /* Generate the Caller-ID spill if desired */
2088 ast_log(LOG_WARNING, "cidspill already exists??\n");
2089 ast_free(p->cidspill);
2092 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2093 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2098 /* Choose proper cadence */
2099 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2100 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
2101 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
2102 p->cidrings = cidrings[p->distinctivering - 1];
2104 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
2105 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
2106 p->cidrings = p->sendcalleridafter;
2109 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2110 c = strchr(dest, '/');
2113 if (c && (strlen(c) < p->stripmsd)) {
2114 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2118 p->dop.op = ZT_DIAL_OP_REPLACE;
2119 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2120 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2122 p->dop.dialstr[0] = '\0';
2125 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2126 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2127 ast_mutex_unlock(&p->lock);
2132 /* Call waiting call */
2133 p->callwaitrings = 0;
2134 if (ast->cid.cid_num)
2135 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2137 p->callwait_num[0] = '\0';
2138 if (ast->cid.cid_name)
2139 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2141 p->callwait_name[0] = '\0';
2142 /* Call waiting tone instead */
2143 if (zt_callwait(ast)) {
2144 ast_mutex_unlock(&p->lock);
2147 /* Make ring-back */
2148 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2149 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2152 n = ast->cid.cid_name;
2153 l = ast->cid.cid_num;
2155 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2157 p->lastcid_num[0] = '\0';
2159 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2161 p->lastcid_name[0] = '\0';
2162 ast_setstate(ast, AST_STATE_RINGING);
2163 index = zt_get_index(ast, p, 0);
2165 p->subs[index].needringing = 1;
2178 case SIG_FGC_CAMAMF:
2183 case SIG_SF_FEATDMF:
2184 case SIG_FEATDMF_TA:
2186 c = strchr(dest, '/');
2191 if (strlen(c) < p->stripmsd) {
2192 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2193 ast_mutex_unlock(&p->lock);
2197 /* Start the trunk, if not GR-303 */
2201 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2203 if (errno != EINPROGRESS) {
2204 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2205 ast_mutex_unlock(&p->lock);
2212 ast_debug(1, "Dialing '%s'\n", c);
2213 p->dop.op = ZT_DIAL_OP_REPLACE;
2219 l = ast->cid.cid_num;
2221 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2223 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2226 l = ast->cid.cid_num;
2228 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2230 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2232 case SIG_FEATDMF_TA:
2234 const char *cic, *ozz;
2236 /* If you have to go through a Tandem Access point you need to use this */
2237 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2240 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2244 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2245 ast_mutex_unlock(&p->lock);
2248 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2249 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2254 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2257 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2259 case SIG_FGC_CAMAMF:
2261 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2265 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2267 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2271 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2272 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2273 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2274 p->echorest[sizeof(p->echorest) - 1] = '\0';
2276 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2280 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2282 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2283 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2284 ast_mutex_unlock(&p->lock);
2288 ast_debug(1, "Deferring dialing...\n");
2291 if (ast_strlen_zero(c))
2293 ast_setstate(ast, AST_STATE_DIALING);
2296 /* Special pseudo -- automatically up*/
2297 ast_setstate(ast, AST_STATE_UP);
2303 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2304 p->dialdest[0] = '\0';
2307 ast_debug(1, "not yet implemented\n");
2308 ast_mutex_unlock(&p->lock);
2313 char ss7_called_nai;
2314 int called_nai_strip;
2315 char ss7_calling_nai;
2316 int calling_nai_strip;
2317 const char *charge_str = NULL;
2319 const char *gen_address = NULL;
2322 c = strchr(dest, '/');
2328 if (!p->hidecallerid) {
2329 l = ast->cid.cid_num;
2334 if (ss7_grab(p, p->ss7)) {
2335 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2336 ast_mutex_unlock(&p->lock);
2339 p->digital = IS_DIGITAL(ast->transfercapability);
2340 p->ss7call = isup_new_call(p->ss7->ss7);
2344 ast_mutex_unlock(&p->lock);
2345 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2349 called_nai_strip = 0;
2350 ss7_called_nai = p->ss7->called_nai;
2351 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2352 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2353 called_nai_strip = strlen(p->ss7->internationalprefix);
2354 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2355 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2356 called_nai_strip = strlen(p->ss7->nationalprefix);
2357 ss7_called_nai = SS7_NAI_NATIONAL;
2359 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2362 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2364 calling_nai_strip = 0;
2365 ss7_calling_nai = p->ss7->calling_nai;
2366 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2367 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2368 calling_nai_strip = strlen(p->ss7->internationalprefix);
2369 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2370 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2371 calling_nai_strip = strlen(p->ss7->nationalprefix);
2372 ss7_calling_nai = SS7_NAI_NATIONAL;
2374 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2377 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2378 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2379 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2381 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
2382 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2384 /* Set the charge number if it is set */
2385 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
2387 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
2390 /* Set the generic address if it is set */
2391 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
2393 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 */
2396 isup_iam(p->ss7->ss7, p->ss7call);
2397 ast_setstate(ast, AST_STATE_DIALING);
2400 #endif /* HAVE_SS7 */
2404 #ifdef SUPPORT_USERUSER
2405 const char *useruser;
2409 int prilocaldialplan;
2413 int redirect_reason;
2415 c = strchr(dest, '/');
2424 if (!p->hidecallerid) {
2425 l = ast->cid.cid_num;
2426 if (!p->hidecalleridname) {
2427 n = ast->cid.cid_name;
2431 if (strlen(c) < p->stripmsd) {
2432 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2433 ast_mutex_unlock(&p->lock);
2436 if (mysig != SIG_FXSKS) {
2437 p->dop.op = ZT_DIAL_OP_REPLACE;
2438 s = strchr(c + p->stripmsd, 'w');
2441 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2443 p->dop.dialstr[0] = '\0';
2446 p->dop.dialstr[0] = '\0';
2449 if (pri_grab(p, p->pri)) {
2450 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2451 ast_mutex_unlock(&p->lock);
2454 if (!(p->call = pri_new_call(p->pri->pri))) {
2455 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2457 ast_mutex_unlock(&p->lock);
2460 if (!(sr = pri_sr_new())) {
2461 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2463 ast_mutex_unlock(&p->lock);
2465 if (p->bearer || (mysig == SIG_FXSKS)) {
2467 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2468 p->bearer->call = p->call;
2470 ast_debug(1, "I'm being setup with no bearer right now...\n");
2472 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2474 p->digital = IS_DIGITAL(ast->transfercapability);
2475 /* Add support for exclusive override */
2476 if (p->priexclusive)
2479 /* otherwise, traditional behavior */
2480 if (p->pri->nodetype == PRI_NETWORK)
2486 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2487 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2489 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2490 if (p->pri->facilityenable)
2491 pri_facility_enable(p->pri->pri);
2493 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2495 pridialplan = p->pri->dialplan - 1;
2496 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2497 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2498 if (pridialplan == -2) {
2499 dp_strip = strlen(p->pri->internationalprefix);
2501 pridialplan = PRI_INTERNATIONAL_ISDN;
2502 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2503 if (pridialplan == -2) {
2504 dp_strip = strlen(p->pri->nationalprefix);
2506 pridialplan = PRI_NATIONAL_ISDN;
2508 pridialplan = PRI_LOCAL_ISDN;
2511 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2512 switch (c[p->stripmsd]) {
2514 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2517 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2520 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2523 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2526 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2529 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2532 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2535 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2538 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2541 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2544 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2547 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2550 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2553 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2557 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2561 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2564 prilocaldialplan = p->pri->localdialplan - 1;
2565 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2566 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2567 if (prilocaldialplan == -2) {
2568 ldp_strip = strlen(p->pri->internationalprefix);
2570 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2571 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2572 if (prilocaldialplan == -2) {
2573 ldp_strip = strlen(p->pri->nationalprefix);
2575 prilocaldialplan = PRI_NATIONAL_ISDN;
2577 prilocaldialplan = PRI_LOCAL_ISDN;
2581 while (*l > '9' && *l != '*' && *l != '#') {
2584 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2587 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2590 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2593 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2596 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2599 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2602 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2605 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2608 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2611 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2614 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2617 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2620 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2623 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2627 ast_log(LOG_WARNING, "Unrecognized prilocaldialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2632 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2633 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2634 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2635 if (!strcasecmp(rr_str, "UNKNOWN"))
2636 redirect_reason = 0;
2637 else if (!strcasecmp(rr_str, "BUSY"))
2638 redirect_reason = 1;
2639 else if (!strcasecmp(rr_str, "NO_REPLY"))
2640 redirect_reason = 2;
2641 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2642 redirect_reason = 15;
2644 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2646 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2647 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2649 #ifdef SUPPORT_USERUSER
2650 /* User-user info */
2651 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2654 pri_sr_set_useruser(sr, useruser);
2657 if (pri_setup(p->pri->pri, p->call, sr)) {
2658 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2659 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2661 ast_mutex_unlock(&p->lock);
2666 ast_setstate(ast, AST_STATE_DIALING);
2670 ast_mutex_unlock(&p->lock);
2674 static void destroy_zt_pvt(struct zt_pvt **pvt)
2676 struct zt_pvt *p = *pvt;
2677 /* Remove channel from the list */
2679 p->prev->next = p->next;
2681 p->next->prev = p->prev;
2683 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2684 if (p->mwi_event_sub)
2685 ast_event_unsubscribe(p->mwi_event_sub);
2687 ast_variables_destroy(p->vars);
2688 ast_mutex_destroy(&p->lock);
2693 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2703 for (i = 0; i < 3; i++) {
2704 if (cur->subs[i].owner) {
2710 prev->next = cur->next;
2712 prev->next->prev = prev;
2718 iflist->prev = NULL;
2722 if (cur->subs[SUB_REAL].zfd > -1) {
2723 zt_close(cur->subs[SUB_REAL].zfd);
2725 destroy_zt_pvt(&cur);
2729 prev->next = cur->next;
2731 prev->next->prev = prev;
2737 iflist->prev = NULL;
2741 if (cur->subs[SUB_REAL].zfd > -1) {
2742 zt_close(cur->subs[SUB_REAL].zfd);
2744 destroy_zt_pvt(&cur);
2750 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2752 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2754 static char *zap_send_keypad_facility_descrip =
2755 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2756 " IE over the current channel.\n";
2758 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2760 /* Data will be our digit string */
2762 char *digits = (char *) data;
2764 if (ast_strlen_zero(digits)) {
2765 ast_debug(1, "No digit string sent to application!\n");
2769 p = (struct zt_pvt *)chan->tech_pvt;
2772 ast_debug(1, "Unable to find technology private\n");
2776 ast_mutex_lock(&p->lock);
2778 if (!p->pri || !p->call) {
2779 ast_debug(1, "Unable to find pri or call on channel!\n");
2780 ast_mutex_unlock(&p->lock);
2784 if (!pri_grab(p, p->pri)) {
2785 pri_keypad_facility(p->pri->pri, p->call, digits);
2788 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2789 ast_mutex_unlock(&p->lock);
2793 ast_mutex_unlock(&p->lock);
2798 static int pri_is_up(struct zt_pri *pri)
2801 for (x = 0; x < NUM_DCHANS; x++) {
2802 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2808 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2810 bearer->owner = &inuse;
2811 bearer->realcall = crv;
2812 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2813 if (crv->subs[SUB_REAL].owner)
2814 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].zfd);
2815 crv->bearer = bearer;
2816 crv->call = bearer->call;
2821 static char *pri_order(int level)
2831 return "Quaternary";
2837 /* Returns fd of the active dchan */
2838 static int pri_active_dchan_fd(struct zt_pri *pri)
2842 for (x = 0; x < NUM_DCHANS; x++) {
2843 if ((pri->dchans[x] == pri->pri))
2850 static int pri_find_dchan(struct zt_pri *pri)
2857 for (x = 0; x < NUM_DCHANS; x++) {
2858 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2860 if (pri->dchans[x] == old) {
2866 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2867 pri->dchannels[newslot]);
2869 if (old && (oldslot != newslot))
2870 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2871 pri->dchannels[oldslot], pri->dchannels[newslot]);
2872 pri->pri = pri->dchans[newslot];
2877 static int zt_hangup(struct ast_channel *ast)
2881 /*static int restore_gains(struct zt_pvt *p);*/
2882 struct zt_pvt *p = ast->tech_pvt;
2883 struct zt_pvt *tmp = NULL;
2884 struct zt_pvt *prev = NULL;
2887 ast_debug(1, "zt_hangup(%s)\n", ast->name);
2888 if (!ast->tech_pvt) {
2889 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2893 ast_mutex_lock(&p->lock);
2895 index = zt_get_index(ast, p, 1);
2897 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
2899 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2905 if (p->origcid_num) {
2906 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2907 ast_free(p->origcid_num);
2908 p->origcid_num = NULL;
2910 if (p->origcid_name) {
2911 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2912 ast_free(p->origcid_name);
2913 p->origcid_name = NULL;
2916 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2920 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2921 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2925 /* Real channel, do some fixup */
2926 p->subs[index].owner = NULL;
2927 p->subs[index].needanswer = 0;
2928 p->subs[index].needflash = 0;
2929 p->subs[index].needringing = 0;
2930 p->subs[index].needbusy = 0;
2931 p->subs[index].needcongestion = 0;
2932 p->subs[index].linear = 0;
2933 p->subs[index].needcallerid = 0;
2934 p->polarity = POLARITY_IDLE;
2935 zt_setlinear(p->subs[index].zfd, 0);
2936 if (index == SUB_REAL) {
2937 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2938 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
2939 if (p->subs[SUB_CALLWAIT].inthreeway) {
2940 /* We had flipped over to answer a callwait and now it's gone */
2941 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
2942 /* Move to the call-wait, but un-own us until they flip back. */
2943 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2944 unalloc_sub(p, SUB_CALLWAIT);
2947 /* The three way hung up, but we still have a call wait */
2948 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2949 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2950 unalloc_sub(p, SUB_THREEWAY);
2951 if (p->subs[SUB_REAL].inthreeway) {
2952 /* This was part of a three way call. Immediately make way for
2954 ast_debug(1, "Call was complete, setting owner to former third call\n");
2955 p->owner = p->subs[SUB_REAL].owner;
2957 /* This call hasn't been completed yet... Set owner to NULL */
2958 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
2961 p->subs[SUB_REAL].inthreeway = 0;
2963 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2964 /* Move to the call-wait and switch back to them. */
2965 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2966 unalloc_sub(p, SUB_CALLWAIT);
2967 p->owner = p->subs[SUB_REAL].owner;
2968 if (p->owner->_state != AST_STATE_UP)
2969 p->subs[SUB_REAL].needanswer = 1;
2970 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2971 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
2972 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2973 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2974 unalloc_sub(p, SUB_THREEWAY);
2975 if (p->subs[SUB_REAL].inthreeway) {
2976 /* This was part of a three way call. Immediately make way for
2978 ast_debug(1, "Call was complete, setting owner to former third call\n");
2979 p->owner = p->subs[SUB_REAL].owner;
2981 /* This call hasn't been completed yet... Set owner to NULL */
2982 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
2985 p->subs[SUB_REAL].inthreeway = 0;
2987 } else if (index == SUB_CALLWAIT) {
2988 /* Ditch the holding callwait call, and immediately make it availabe */
2989 if (p->subs[SUB_CALLWAIT].inthreeway) {
2990 /* This is actually part of a three way, placed on hold. Place the third part
2991 on music on hold now */
2992 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2993 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2994 S_OR(p->mohsuggest, NULL),
2995 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2997 p->subs[SUB_THREEWAY].inthreeway = 0;
2998 /* Make it the call wait now */
2999 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
3000 unalloc_sub(p, SUB_THREEWAY);
3002 unalloc_sub(p, SUB_CALLWAIT);
3003 } else if (index == SUB_THREEWAY) {
3004 if (p->subs[SUB_CALLWAIT].inthreeway) {
3005 /* The other party of the three way call is currently in a call-wait state.
3006 Start music on hold for them, and take the main guy out of the third call */
3007 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
3008 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
3009 S_OR(p->mohsuggest, NULL),
3010 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3012 p->subs[SUB_CALLWAIT].inthreeway = 0;
3014 p->subs[SUB_REAL].inthreeway = 0;
3015 /* If this was part of a three way call index, let us make
3016 another three way call */
3017 unalloc_sub(p, SUB_THREEWAY);
3019 /* This wasn't any sort of call, but how are we an index? */
3020 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
3024 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
3027 p->distinctivering = 0;
3028 p->confirmanswer = 0;
3034 p->onhooktime = time(NULL);
3035 #if defined(HAVE_PRI) || defined(HAVE_SS7)
3042 ast_dsp_free(p->dsp);
3046 law = ZT_LAW_DEFAULT;
3047 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
3049 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
3050 /* Perform low level hangup if no owner left */
3054 if (!ss7_grab(p, p->ss7)) {
3055 if (!p->alreadyhungup) {
3056 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
3057 int icause = ast->hangupcause ? ast->hangupcause : -1;
3061 icause = atoi(cause);
3063 isup_rel(p->ss7->ss7, p->ss7call, icause);
3065 p->alreadyhungup = 1;
3067 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
3069 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
3077 #ifdef SUPPORT_USERUSER
3078 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
3081 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
3082 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
3083 if (!pri_grab(p, p->pri)) {
3084 if (p->alreadyhungup) {
3085 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
3087 #ifdef SUPPORT_USERUSER
3088 pri_call_set_useruser(p->call, useruser);
3091 pri_hangup(p->pri->pri, p->call, -1);
3094 p->bearer->call = NULL;
3096 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
3097 int icause = ast->hangupcause ? ast->hangupcause : -1;
3098 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
3100 #ifdef SUPPORT_USERUSER
3101 pri_call_set_useruser(p->call, useruser);
3104 p->alreadyhungup = 1;
3106 p->bearer->alreadyhungup = 1;
3109 icause = atoi(cause);
3111 pri_hangup(p->pri->pri, p->call, icause);
3114 ast_log(LOG_WARNING, "pri_disconnect failed\n");
3117 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3122 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
3128 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7) && (p->sig != SIG_BRI) && (p->sig != SIG_BRI_PTMP)))
3129 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
3131 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
3137 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
3140 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
3142 /* If they're off hook, try playing congestion */
3143 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
3144 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
3146 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3152 /* Make sure we're not made available for at least two seconds assuming
3153 we were actually used for an inbound or outbound call. */
3154 if (ast->_state != AST_STATE_RESERVED) {
3155 time(&p->guardtime);
3160 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3163 ast_free(p->cidspill);
3167 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
3168 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
3172 p->callwaiting = p->permcallwaiting;
3173 p->hidecallerid = p->permhidecallerid;
3178 /* Restore data mode */
3179 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
3181 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
3185 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
3186 /* Free up the bearer channel as well, and
3187 don't use its file descriptor anymore */
3188 update_conf(p->bearer);
3189 reset_conf(p->bearer);
3190 p->bearer->owner = NULL;
3191 p->bearer->realcall = NULL;
3193 p->subs[SUB_REAL].zfd = -1;
3200 p->callwaitingrepeat = 0;
3203 ast->tech_pvt = NULL;
3204 ast_mutex_unlock(&p->lock);
3205 ast_module_unref(ast_module_info->self);
3206 ast_verb(3, "Hungup '%s'\n", ast->name);
3208 ast_mutex_lock(&iflock);
3214 destroy_channel(prev, tmp, 0);
3222 ast_mutex_unlock(&iflock);
3226 static int zt_answer(struct ast_channel *ast)
3228 struct zt_pvt *p = ast->tech_pvt;
3231 int oldstate = ast->_state;
3232 ast_setstate(ast, AST_STATE_UP);
3233 ast_mutex_lock(&p->lock);
3234 index = zt_get_index(ast, p, 0);
3237 /* nothing to do if a radio channel */
3238 if ((p->radio || (p->oprmode < 0))) {
3239 ast_mutex_unlock(&p->lock);
3253 case SIG_FEATDMF_TA:
3256 case SIG_FGC_CAMAMF:
3261 case SIG_SF_FEATDMF:
3266 /* Pick up the line */
3267 ast_debug(1, "Took %s off hook\n", ast->name);
3268 if (p->hanguponpolarityswitch) {
3269 p->polaritydelaytv = ast_tvnow();
3271 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
3272 tone_zone_play_tone(p->subs[index].zfd, -1);
3274 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
3275 if (oldstate == AST_STATE_RINGING) {
3276 ast_debug(1, "Finally swapping real and threeway\n");
3277 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3278 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3279 p->owner = p->subs[SUB_REAL].owner;
3282 if (p->sig & __ZT_SIG_FXS) {
3291 /* Send a pri acknowledge */
3292 if (!pri_grab(p, p->pri)) {
3294 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3297 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3304 if (!ss7_grab(p, p->ss7)) {
3306 res = isup_anm(p->ss7->ss7, p->ss7call);
3309 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3315 ast_mutex_unlock(&p->lock);
3318 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3321 ast_mutex_unlock(&p->lock);
3325 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3331 struct zt_pvt *p = chan->tech_pvt, *pp;
3332 struct oprmode *oprmode;
3335 /* all supported options require data */
3336 if (!data || (datalen < 1)) {
3342 case AST_OPTION_TXGAIN:
3343 scp = (signed char *) data;
3344 index = zt_get_index(chan, p, 0);
3346 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
3349 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
3350 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
3351 case AST_OPTION_RXGAIN:
3352 scp = (signed char *) data;
3353 index = zt_get_index(chan, p, 0);
3355 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
3358 ast_debug(1, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
3359 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
3360 case AST_OPTION_TONE_VERIFY:
3366 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
3367 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
3370 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
3371 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
3374 ast_debug(1, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
3375 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
3379 case AST_OPTION_TDD:
3380 /* turn on or off TDD */
3383 if (!*cp) { /* turn it off */
3384 ast_debug(1, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
3390 ast_debug(1, "Set option TDD MODE, value: %s(%d) on %s\n",
3391 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
3393 /* otherwise, turn it on */
3394 if (!p->didtdd) { /* if havent done it yet */
3395 unsigned char mybuf[41000], *buf;
3396 int size, res, fd, len;
3397 struct pollfd fds[1];
3400 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
3401 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
3403 index = zt_get_index(chan, p, 0);
3405 ast_log(LOG_WARNING, "No index in TDD?\n");
3408 fd = p->subs[index].zfd;
3410 if (ast_check_hangup(chan))
3413 if (size > READ_SIZE)
3416 fds[0].events = POLLPRI | POLLOUT;
3418 res = poll(fds, 1, -1);