2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Zaptel Pseudo TDM interface
23 * \author Mark Spencer <markster@digium.com>
25 * Connects to the zaptel telephony library as well as
26 * libpri. Libpri is optional and needed only if you are
27 * going to use ISDN connections.
29 * You need to install libraries before you attempt to compile
30 * and install the zaptel channel.
33 * \arg \ref Config_zap
35 * \ingroup channel_drivers
37 * \todo Deprecate the "musiconhold" configuration option post 1.4
41 <depend>res_smdi</depend>
42 <depend>zaptel_vldtmf</depend>
43 <depend>zaptel</depend>
44 <depend>tonezone</depend>
51 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
57 #include <sys/signal.h>
59 #include <sys/ioctl.h>
62 #include "asterisk/zapata.h"
72 #include "asterisk/lock.h"
73 #include "asterisk/channel.h"
74 #include "asterisk/config.h"
75 #include "asterisk/module.h"
76 #include "asterisk/pbx.h"
77 #include "asterisk/options.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 static char progzone[10] = "";
231 static int usedistinctiveringdetection = 0;
232 static int distinctiveringaftercid = 0;
234 static int numbufs = 4;
237 static struct ast_channel inuse;
238 #ifdef PRI_GETSET_TIMERS
239 static int pritimers[PRI_MAX_TIMERS];
241 static int pridebugfd = -1;
242 static char pridebugfilename[1024] = "";
245 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
246 static int firstdigittimeout = 16000;
248 /*! \brief How long to wait for following digits (FXO logic) */
249 static int gendigittimeout = 8000;
251 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
252 static int matchdigittimeout = 3000;
254 /*! \brief Protect the interface list (of zt_pvt's) */
255 AST_MUTEX_DEFINE_STATIC(iflock);
258 static int ifcount = 0;
261 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
264 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
265 when it's doing something critical. */
266 AST_MUTEX_DEFINE_STATIC(monlock);
268 /*! \brief This is the thread for the monitor which checks for input on the channels
269 which are not currently in use. */
270 static pthread_t monitor_thread = AST_PTHREADT_NULL;
272 static int restart_monitor(void);
274 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);
276 static int zt_sendtext(struct ast_channel *c, const char *text);
278 static void mwi_event_cb(const struct ast_event *event, void *userdata)
280 /* This module does not handle MWI in an event-based manner. However, it
281 * subscribes to MWI for each mailbox that is configured so that the core
282 * knows that we care about it. Then, chan_zap will get the MWI from the
283 * event cache instead of checking the mailbox directly. */
286 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
287 static inline int zt_get_event(int fd)
290 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
295 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
296 static inline int zt_wait_event(int fd)
299 i = ZT_IOMUX_SIGEVENT;
300 if (ioctl(fd, ZT_IOMUX, &i) == -1)
302 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
307 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
308 #define READ_SIZE 160
310 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
311 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
313 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
314 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
315 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
316 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
317 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
321 static int ringt_base = DEFAULT_RINGT;
325 #define LINKSTATE_INALARM (1 << 0)
326 #define LINKSTATE_STARTING (1 << 1)
327 #define LINKSTATE_UP (1 << 2)
328 #define LINKSTATE_DOWN (1 << 3)
330 #define SS7_NAI_DYNAMIC -1
333 pthread_t master; /*!< Thread of master */
337 int linkstate[NUM_DCHANS];
341 LINKSET_STATE_DOWN = 0,
344 char called_nai; /*!< Called Nature of Address Indicator */
345 char calling_nai; /*!< Calling Nature of Address Indicator */
346 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
347 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
348 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
349 char unknownprefix[20]; /*!< for unknown dialplans */
351 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
354 static struct zt_ss7 linksets[NUM_SPANS];
356 static int cur_ss7type = -1;
357 static int cur_linkset = -1;
358 static int cur_pointcode = -1;
359 static int cur_cicbeginswith = -1;
360 static int cur_adjpointcode = -1;
361 static int cur_networkindicator = -1;
362 static int cur_defaultdpc = -1;
363 #endif /* HAVE_SS7 */
367 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
368 #define PRI_CHANNEL(p) ((p) & 0xff)
369 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
370 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
373 pthread_t master; /*!< Thread of master */
374 ast_mutex_t lock; /*!< Mutex */
375 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
376 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
377 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
378 int minunused; /*!< Min # of channels to keep empty */
379 int minidle; /*!< Min # of "idling" calls to keep active */
380 int nodetype; /*!< Node type */
381 int switchtype; /*!< Type of switch to emulate */
382 int nsf; /*!< Network-Specific Facilities */
383 int dialplan; /*!< Dialing plan */
384 int localdialplan; /*!< Local dialing plan */
385 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
386 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
387 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
388 char privateprefix[20]; /*!< for private dialplans */
389 char unknownprefix[20]; /*!< for unknown dialplans */
390 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
391 int trunkgroup; /*!< What our trunkgroup is */
392 int mastertrunkgroup; /*!< What trunk group is our master */
393 int prilogicalspan; /*!< Logical span number within trunk group */
394 int numchans; /*!< Num of channels we represent */
395 int overlapdial; /*!< In overlap dialing mode */
396 int facilityenable; /*!< Enable facility IEs */
397 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
398 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
399 struct pri *pri; /*!< Currently active D-channel */
401 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
406 time_t lastreset; /*!< time when unused channels were last reset */
407 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
409 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
410 struct zt_pvt *crvs; /*!< Member CRV structs */
411 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
415 static struct zt_pri pris[NUM_SPANS];
418 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
420 #define DEFAULT_PRI_DEBUG 0
423 static inline void pri_rel(struct zt_pri *pri)
425 ast_mutex_unlock(&pri->lock);
429 /*! Shut up the compiler */
433 #define SUB_REAL 0 /*!< Active call */
434 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
435 #define SUB_THREEWAY 2 /*!< Three-way call */
437 /* Polarity states */
438 #define POLARITY_IDLE 0
439 #define POLARITY_REV 1
442 static struct zt_distRings drings;
444 struct distRingData {
448 struct ringContextData {
449 char contextData[AST_MAX_CONTEXT];
451 struct zt_distRings {
452 struct distRingData ringnum[3];
453 struct ringContextData ringContext[3];
456 static char *subnames[] = {
462 struct zt_subchannel {
464 struct ast_channel *owner;
466 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
467 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
468 unsigned int needringing:1;
469 unsigned int needbusy:1;
470 unsigned int needcongestion:1;
471 unsigned int needcallerid:1;
472 unsigned int needanswer:1;
473 unsigned int needflash:1;
474 unsigned int needhold:1;
475 unsigned int needunhold:1;
476 unsigned int linear:1;
477 unsigned int inthreeway:1;
481 #define CONF_USER_REAL (1 << 0)
482 #define CONF_USER_THIRDCALL (1 << 1)
486 static struct zt_pvt {
488 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
489 /*!< Up to three channels can be associated with this call */
491 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
492 struct zt_subchannel subs[3]; /*!< Sub-channels */
493 struct zt_confinfo saveconf; /*!< Saved conference info */
495 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
496 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
497 int inconference; /*!< If our real should be in the conference */
499 int sig; /*!< Signalling style */
500 int radio; /*!< radio type */
501 int outsigmod; /*!< Outbound Signalling style (modifier) */
502 int oprmode; /*!< "Operator Services" mode */
503 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
504 float cid_rxgain; /*!< "Gain to apply during caller id */
507 int tonezone; /*!< tone zone for this chan, or -1 for default */
508 struct zt_pvt *next; /*!< Next channel in list */
509 struct zt_pvt *prev; /*!< Prev channel in list */
513 unsigned int answeronpolarityswitch:1;
514 unsigned int busydetect:1;
515 unsigned int callreturn:1;
516 unsigned int callwaiting:1;
517 unsigned int callwaitingcallerid:1;
518 unsigned int cancallforward:1;
519 unsigned int canpark:1;
520 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
521 unsigned int destroy:1;
522 unsigned int didtdd:1; /*!< flag to say its done it once */
523 unsigned int dialednone:1;
524 unsigned int dialing:1;
525 unsigned int digital:1;
527 unsigned int echobreak:1;
528 unsigned int echocanbridged:1;
529 unsigned int echocanon:1;
530 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
531 unsigned int firstradio:1;
532 unsigned int hanguponpolarityswitch:1;
533 unsigned int hardwaredtmf:1;
534 unsigned int hidecallerid:1;
535 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
536 unsigned int ignoredtmf:1;
537 unsigned int immediate:1; /*!< Answer before getting digits? */
538 unsigned int inalarm:1;
539 unsigned int mate:1; /*!< flag to say its in MATE mode */
540 unsigned int outgoing:1;
541 /* unsigned int overlapdial:1; unused and potentially confusing */
542 unsigned int permcallwaiting:1;
543 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
544 unsigned int priindication_oob:1;
545 unsigned int priexclusive:1;
546 unsigned int pulse:1;
547 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
548 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
549 unsigned int threewaycalling:1;
550 unsigned int transfer:1;
551 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
552 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
553 unsigned int usedistinctiveringdetection:1;
554 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
555 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
556 /* Channel state or unavilability flags */
557 unsigned int inservice:1;
558 unsigned int locallyblocked:1;
559 unsigned int remotelyblocked:1;
560 #if defined(HAVE_PRI) || defined(HAVE_SS7)
561 unsigned int alerting:1;
562 unsigned int alreadyhungup:1;
563 unsigned int isidlecall:1;
564 unsigned int proceeding:1;
565 unsigned int progress:1;
566 unsigned int resetting:1;
567 unsigned int setup_ack:1;
569 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
570 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
572 struct zt_distRings drings;
574 char context[AST_MAX_CONTEXT];
575 char defcontext[AST_MAX_CONTEXT];
576 char exten[AST_MAX_EXTENSION];
577 char language[MAX_LANGUAGE];
578 char mohinterpret[MAX_MUSICCLASS];
579 char mohsuggest[MAX_MUSICCLASS];
580 #if defined(PRI_ANI) || defined(HAVE_SS7)
581 char cid_ani[AST_MAX_EXTENSION];
584 char cid_num[AST_MAX_EXTENSION];
585 int cid_ton; /*!< Type Of Number (TON) */
586 char cid_name[AST_MAX_EXTENSION];
587 char lastcid_num[AST_MAX_EXTENSION];
588 char lastcid_name[AST_MAX_EXTENSION];
589 char *origcid_num; /*!< malloced original callerid */
590 char *origcid_name; /*!< malloced original callerid */
591 char callwait_num[AST_MAX_EXTENSION];
592 char callwait_name[AST_MAX_EXTENSION];
593 char rdnis[AST_MAX_EXTENSION];
594 char dnid[AST_MAX_EXTENSION];
597 int confno; /*!< Our conference */
598 int confusers; /*!< Who is using our conference */
599 int propconfno; /*!< Propagated conference number */
600 ast_group_t callgroup;
601 ast_group_t pickupgroup;
602 struct ast_variable *vars;
603 int channel; /*!< Channel Number or CRV */
604 int span; /*!< Span number */
605 time_t guardtime; /*!< Must wait this much time before using for new call */
606 int cid_signalling; /*!< CID signalling type bell202 or v23 */
607 int cid_start; /*!< CID start indicator, polarity or ring */
608 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
609 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
610 int cidcwexpire; /*!< When to expire our muting for CID/CW */
611 unsigned char *cidspill;
624 int busy_quietlength;
626 struct timeval flashtime; /*!< Last flash-hook time */
628 int cref; /*!< Call reference number */
629 ZT_DIAL_OPERATION dop;
630 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
632 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
633 int amaflags; /*!< AMA Flags */
634 struct tdd_state *tdd; /*!< TDD flag */
635 char call_forward[AST_MAX_EXTENSION];
636 char mailbox[AST_MAX_EXTENSION];
637 struct ast_event_sub *mwi_event_sub;
641 int distinctivering; /*!< Which distinctivering to use */
642 int cidrings; /*!< Which ring to deliver CID on */
643 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
645 int polarityonanswerdelay;
646 struct timeval polaritydelaytv;
647 int sendcalleridafter;
650 struct zt_pvt *bearer;
651 struct zt_pvt *realcall;
660 struct isup_call *ss7call;
661 char charge_number[50];
662 char gen_add_number[50];
663 unsigned char gen_add_num_plan;
664 unsigned char gen_add_nai;
665 unsigned char gen_add_pres_ind;
666 unsigned char gen_add_type;
668 int cic; /*!< CIC associated with channel */
669 unsigned int dpc; /*!< CIC's DPC */
670 unsigned int loopedback:1;
673 } *iflist = NULL, *ifend = NULL;
675 /*! \brief Channel configuration from zapata.conf .
676 * This struct is used for parsing the [channels] section of zapata.conf.
677 * Generally there is a field here for every possible configuration item.
679 * The state of fields is saved along the parsing and whenever a 'channel'
680 * statement is reached, the current zt_chan_conf is used to configure the
681 * channel (struct zt_pvt)
683 * \see zt_chan_init for the default values.
685 struct zt_chan_conf {
696 char smdi_port[SMDI_MAX_FILENAME_LEN];
699 /** returns a new zt_chan_conf with default values (by-value) */
700 static struct zt_chan_conf zt_chan_conf_default(void) {
701 /* recall that if a field is not included here it is initialized
704 struct zt_chan_conf conf = {
708 .switchtype = PRI_SWITCH_NI2,
709 .dialplan = PRI_NATIONAL_ISDN + 1,
710 .localdialplan = PRI_NATIONAL_ISDN + 1,
716 .internationalprefix = "",
717 .nationalprefix = "",
726 .called_nai = SS7_NAI_NATIONAL,
727 .calling_nai = SS7_NAI_NATIONAL,
728 .internationalprefix = "",
729 .nationalprefix = "",
730 .subscriberprefix = "",
735 .context = "default",
738 .mohinterpret = "default",
742 .cid_signalling = CID_SIG_BELL,
743 .cid_start = CID_START_RING,
762 .polarityonanswerdelay = 600,
764 .sendcalleridafter = DEFAULT_CIDRINGS
776 .smdi_port = "/dev/ttyS0",
783 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
784 static int zt_digit_begin(struct ast_channel *ast, char digit);
785 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
786 static int zt_sendtext(struct ast_channel *c, const char *text);
787 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
788 static int zt_hangup(struct ast_channel *ast);
789 static int zt_answer(struct ast_channel *ast);
790 static struct ast_frame *zt_read(struct ast_channel *ast);
791 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
792 static struct ast_frame *zt_exception(struct ast_channel *ast);
793 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
794 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
795 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
796 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
798 static const struct ast_channel_tech zap_tech = {
800 .description = tdesc,
801 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
802 .requester = zt_request,
803 .send_digit_begin = zt_digit_begin,
804 .send_digit_end = zt_digit_end,
805 .send_text = zt_sendtext,
812 .exception = zt_exception,
813 .indicate = zt_indicate,
815 .setoption = zt_setoption,
816 .func_channel_read = zt_func_read,
820 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
822 #define GET_CHANNEL(p) ((p)->channel)
825 struct zt_pvt *round_robin[32];
828 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
831 /* Grab the lock first */
833 res = ast_mutex_trylock(&pri->lock);
835 ast_mutex_unlock(&pvt->lock);
836 /* Release the lock and try again */
838 ast_mutex_lock(&pvt->lock);
841 /* Then break the poll */
842 pthread_kill(pri->master, SIGURG);
848 static inline void ss7_rel(struct zt_ss7 *ss7)
850 ast_mutex_unlock(&ss7->lock);
853 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
856 /* Grab the lock first */
858 res = ast_mutex_trylock(&pri->lock);
860 ast_mutex_unlock(&pvt->lock);
861 /* Release the lock and try again */
863 ast_mutex_lock(&pvt->lock);
866 /* Then break the poll */
867 pthread_kill(pri->master, SIGURG);
871 #define NUM_CADENCE_MAX 25
872 static int num_cadence = 4;
873 static int user_has_defined_cadences = 0;
875 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
876 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
877 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
878 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
879 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
882 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
883 * is 1, the second pause is 2 and so on.
886 static int cidrings[NUM_CADENCE_MAX] = {
887 2, /*!< Right after first long ring */
888 4, /*!< Right after long part */
889 3, /*!< After third chirp */
890 2, /*!< Second spell */
893 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
894 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
896 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
897 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
899 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
902 if (p->subs[0].owner == ast)
904 else if (p->subs[1].owner == ast)
906 else if (p->subs[2].owner == ast)
911 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
917 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
919 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
924 ast_mutex_unlock(&pri->lock);
927 if (p->subs[a].owner) {
928 if (ast_channel_trylock(p->subs[a].owner)) {
929 ast_mutex_unlock(&p->lock);
931 ast_mutex_lock(&p->lock);
933 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
934 ast_channel_unlock(p->subs[a].owner);
942 ast_mutex_lock(&pri->lock);
946 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
949 struct zt_pri *pri = (struct zt_pri*) data;
952 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
954 /* We must unlock the PRI to avoid the possibility of a deadlock */
955 #if defined(HAVE_PRI) || defined(HAVE_SS7)
962 ast_mutex_unlock(&pri->lock);
967 ast_mutex_unlock(&ss7->lock);
977 if (ast_channel_trylock(p->owner)) {
978 ast_mutex_unlock(&p->lock);
980 ast_mutex_lock(&p->lock);
982 ast_queue_frame(p->owner, f);
983 ast_channel_unlock(p->owner);
989 #if defined(HAVE_PRI) || defined(HAVE_SS7)
996 ast_mutex_lock(&pri->lock);
1001 ast_mutex_lock(&ss7->lock);
1012 static int restore_gains(struct zt_pvt *p);
1014 static void swap_subs(struct zt_pvt *p, int a, int b)
1018 struct ast_channel *towner;
1020 ast_debug(1, "Swapping %d and %d\n", a, b);
1022 tchan = p->subs[a].chan;
1023 towner = p->subs[a].owner;
1024 tinthreeway = p->subs[a].inthreeway;
1026 p->subs[a].chan = p->subs[b].chan;
1027 p->subs[a].owner = p->subs[b].owner;
1028 p->subs[a].inthreeway = p->subs[b].inthreeway;
1030 p->subs[b].chan = tchan;
1031 p->subs[b].owner = towner;
1032 p->subs[b].inthreeway = tinthreeway;
1034 if (p->subs[a].owner)
1035 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].zfd);
1036 if (p->subs[b].owner)
1037 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].zfd);
1038 wakeup_sub(p, a, NULL);
1039 wakeup_sub(p, b, NULL);
1042 static int zt_open(char *fn)
1050 for (x = 0; x < strlen(fn); x++) {
1051 if (!isdigit(fn[x])) {
1059 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1062 fn = "/dev/zap/channel";
1064 fd = open(fn, O_RDWR | O_NONBLOCK);
1066 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1070 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1074 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1079 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) {
1080 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
1089 static void zt_close(int fd)
1095 static int zt_setlinear(int zfd, int linear)
1098 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1105 static int alloc_sub(struct zt_pvt *p, int x)
1109 if (p->subs[x].zfd < 0) {
1110 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1111 if (p->subs[x].zfd > -1) {
1112 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1114 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1115 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1116 bi.numbufs = numbufs;
1117 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1119 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1122 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1123 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1124 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1125 zt_close(p->subs[x].zfd);
1126 p->subs[x].zfd = -1;
1129 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1132 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1135 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1139 static int unalloc_sub(struct zt_pvt *p, int x)
1142 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1145 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1146 if (p->subs[x].zfd > -1) {
1147 zt_close(p->subs[x].zfd);
1149 p->subs[x].zfd = -1;
1150 p->subs[x].linear = 0;
1151 p->subs[x].chan = 0;
1152 p->subs[x].owner = NULL;
1153 p->subs[x].inthreeway = 0;
1154 p->polarity = POLARITY_IDLE;
1155 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1159 static int digit_to_dtmfindex(char digit)
1162 return ZT_TONE_DTMF_BASE + (digit - '0');
1163 else if (digit >= 'A' && digit <= 'D')
1164 return ZT_TONE_DTMF_A + (digit - 'A');
1165 else if (digit >= 'a' && digit <= 'd')
1166 return ZT_TONE_DTMF_A + (digit - 'a');
1167 else if (digit == '*')
1168 return ZT_TONE_DTMF_s;
1169 else if (digit == '#')
1170 return ZT_TONE_DTMF_p;
1175 static int zt_digit_begin(struct ast_channel *chan, char digit)
1181 pvt = chan->tech_pvt;
1183 ast_mutex_lock(&pvt->lock);
1185 index = zt_get_index(chan, pvt, 0);
1187 if ((index != SUB_REAL) || !pvt->owner)
1191 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1192 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1193 if (pvt->setup_ack) {
1194 if (!pri_grab(pvt, pvt->pri)) {
1195 pri_information(pvt->pri->pri, pvt->call, digit);
1198 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1199 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1201 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1202 res = strlen(pvt->dialdest);
1203 pvt->dialdest[res++] = digit;
1204 pvt->dialdest[res] = '\0';
1209 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1212 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1214 ZT_DIAL_OPERATION zo = {
1215 .op = ZT_DIAL_OP_APPEND,
1218 zo.dialstr[0] = 'T';
1219 zo.dialstr[1] = digit;
1220 zo.dialstr[2] = '\0';
1221 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1222 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1226 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1228 pvt->begindigit = digit;
1232 ast_mutex_unlock(&pvt->lock);
1237 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1244 pvt = chan->tech_pvt;
1246 ast_mutex_lock(&pvt->lock);
1248 index = zt_get_index(chan, pvt, 0);
1250 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1254 /* This means that the digit was already sent via PRI signalling */
1255 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1256 && !pvt->begindigit)
1260 if (pvt->begindigit) {
1262 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1263 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1265 pvt->begindigit = 0;
1269 ast_mutex_unlock(&pvt->lock);
1274 static char *events[] = {
1287 "Hook Transition Complete",
1292 "Polarity Reversal",
1300 { ZT_ALARM_RED, "Red Alarm" },
1301 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1302 { ZT_ALARM_BLUE, "Blue Alarm" },
1303 { ZT_ALARM_RECOVER, "Recovering" },
1304 { ZT_ALARM_LOOPBACK, "Loopback" },
1305 { ZT_ALARM_NOTOPEN, "Not Open" },
1306 { ZT_ALARM_NONE, "None" },
1309 static char *alarm2str(int alarm)
1312 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1313 if (alarms[x].alarm & alarm)
1314 return alarms[x].name;
1316 return alarm ? "Unknown Alarm" : "No Alarm";
1319 static char *event2str(int event)
1321 static char buf[256];
1322 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1323 return events[event];
1324 sprintf(buf, "Event %d", event); /* safe */
1329 static char *dialplan2str(int dialplan)
1331 if (dialplan == -1 || dialplan == -2) {
1332 return("Dynamically set dialplan in ISDN");
1334 return (pri_plan2str(dialplan));
1338 static char *zap_sig2str(int sig)
1340 static char buf[256];
1343 return "E & M Immediate";
1345 return "E & M Wink";
1349 return "Feature Group D (DTMF)";
1351 return "Feature Group D (MF)";
1352 case SIG_FEATDMF_TA:
1353 return "Feature Groud D (MF) Tandem Access";
1355 return "Feature Group B (MF)";
1359 return "FGC/CAMA (Dialpulse)";
1360 case SIG_FGC_CAMAMF:
1361 return "FGC/CAMA (MF)";
1363 return "FXS Loopstart";
1365 return "FXS Groundstart";
1367 return "FXS Kewlstart";
1369 return "FXO Loopstart";
1371 return "FXO Groundstart";
1373 return "FXO Kewlstart";
1377 return "ISDN BRI Point to Point";
1379 return "ISDN BRI Point to MultiPoint";
1383 return "SF (Tone) Immediate";
1385 return "SF (Tone) Wink";
1387 return "SF (Tone) with Feature Group D (DTMF)";
1388 case SIG_SF_FEATDMF:
1389 return "SF (Tone) with Feature Group D (MF)";
1391 return "SF (Tone) with Feature Group B (MF)";
1392 case SIG_GR303FXOKS:
1393 return "GR-303 with FXOKS";
1394 case SIG_GR303FXSKS:
1395 return "GR-303 with FXSKS";
1399 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1404 #define sig2str zap_sig2str
1406 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1408 /* If the conference already exists, and we're already in it
1409 don't bother doing anything */
1412 memset(&zi, 0, sizeof(zi));
1415 if (slavechannel > 0) {
1416 /* If we have only one slave, do a digital mon */
1417 zi.confmode = ZT_CONF_DIGITALMON;
1418 zi.confno = slavechannel;
1421 /* Real-side and pseudo-side both participate in conference */
1422 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1423 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1425 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1426 zi.confno = p->confno;
1428 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1432 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1433 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1436 if (slavechannel < 1) {
1437 p->confno = zi.confno;
1439 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1440 ast_debug(1, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1444 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1446 /* If they're listening to our channel, they're ours */
1447 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1449 /* If they're a talker on our (allocated) conference, they're ours */
1450 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1455 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1458 if (/* Can't delete if there's no zfd */
1460 /* Don't delete from the conference if it's not our conference */
1462 /* Don't delete if we don't think it's conferenced at all (implied) */
1464 memset(&zi, 0, sizeof(zi));
1468 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1469 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1472 ast_debug(1, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1473 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1477 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1481 struct zt_pvt *slave = NULL;
1482 /* Start out optimistic */
1484 /* Update conference state in a stateless fashion */
1485 for (x = 0; x < 3; x++) {
1486 /* Any three-way calling makes slave native mode *definitely* out
1488 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1491 /* If we don't have any 3-way calls, check to see if we have
1492 precisely one slave */
1493 if (useslavenative) {
1494 for (x = 0; x < MAX_SLAVES; x++) {
1497 /* Whoops already have a slave! No
1498 slave native and stop right away */
1503 /* We have one slave so far */
1504 slave = p->slaves[x];
1509 /* If no slave, slave native definitely out */
1512 else if (slave->law != p->law) {
1518 return useslavenative;
1521 static int reset_conf(struct zt_pvt *p)
1524 memset(&zi, 0, sizeof(zi));
1526 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1527 if (p->subs[SUB_REAL].zfd > -1) {
1528 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1529 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1534 static int update_conf(struct zt_pvt *p)
1539 struct zt_pvt *slave = NULL;
1541 useslavenative = isslavenative(p, &slave);
1542 /* Start with the obvious, general stuff */
1543 for (x = 0; x < 3; x++) {
1544 /* Look for three way calls */
1545 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1546 conf_add(p, &p->subs[x], x, 0);
1549 conf_del(p, &p->subs[x], x);
1552 /* If we have a slave, add him to our conference now. or DAX
1553 if this is slave native */
1554 for (x = 0; x < MAX_SLAVES; x++) {
1557 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1559 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1564 /* If we're supposed to be in there, do so now */
1565 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1567 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1569 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1573 /* If we have a master, add ourselves to his conference */
1575 if (isslavenative(p->master, NULL)) {
1576 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1578 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1582 /* Nobody is left (or should be left) in our conference.
1586 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1590 static void zt_enable_ec(struct zt_pvt *p)
1597 ast_debug(1, "Echo cancellation already on\n");
1601 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1604 if (p->echocancel) {
1605 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1607 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1609 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1612 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1614 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1617 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1620 ast_debug(1, "No echo cancellation requested\n");
1623 static void zt_train_ec(struct zt_pvt *p)
1627 if (p && p->echocancel && p->echotraining) {
1628 x = p->echotraining;
1629 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1631 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1633 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1635 ast_debug(1, "No echo training requested\n");
1638 static void zt_disable_ec(struct zt_pvt *p)
1642 if (p->echocancel) {
1644 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1646 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1648 ast_debug(1, "disabled echo cancellation on channel %d\n", p->channel);
1653 static void fill_txgain(struct zt_gains *g, float gain, int law)
1657 float linear_gain = pow(10.0, gain / 20.0);
1661 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1663 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1664 if (k > 32767) k = 32767;
1665 if (k < -32767) k = -32767;
1666 g->txgain[j] = AST_LIN2A(k);
1673 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1675 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1676 if (k > 32767) k = 32767;
1677 if (k < -32767) k = -32767;
1678 g->txgain[j] = AST_LIN2MU(k);
1687 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1691 float linear_gain = pow(10.0, gain / 20.0);
1695 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1697 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1698 if (k > 32767) k = 32767;
1699 if (k < -32767) k = -32767;
1700 g->rxgain[j] = AST_LIN2A(k);
1707 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1709 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1710 if (k > 32767) k = 32767;
1711 if (k < -32767) k = -32767;
1712 g->rxgain[j] = AST_LIN2MU(k);
1721 static int set_actual_txgain(int fd, int chan, float gain, int law)
1726 memset(&g, 0, sizeof(g));
1728 res = ioctl(fd, ZT_GETGAINS, &g);
1730 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1734 fill_txgain(&g, gain, law);
1736 return ioctl(fd, ZT_SETGAINS, &g);
1739 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1744 memset(&g, 0, sizeof(g));
1746 res = ioctl(fd, ZT_GETGAINS, &g);
1748 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1752 fill_rxgain(&g, gain, law);
1754 return ioctl(fd, ZT_SETGAINS, &g);
1757 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1759 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1762 static int bump_gains(struct zt_pvt *p)
1766 /* Bump receive gain by value stored in cid_rxgain */
1767 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1769 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1776 static int restore_gains(struct zt_pvt *p)
1780 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1782 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1789 static inline int zt_set_hook(int fd, int hs)
1794 res = ioctl(fd, ZT_HOOK, &x);
1797 if (errno == EINPROGRESS)
1799 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1805 static inline int zt_confmute(struct zt_pvt *p, int muted)
1809 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
1811 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1813 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1815 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1817 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1821 static int save_conference(struct zt_pvt *p)
1823 struct zt_confinfo c;
1825 if (p->saveconf.confmode) {
1826 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1829 p->saveconf.chan = 0;
1830 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1832 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1833 p->saveconf.confmode = 0;
1838 c.confmode = ZT_CONF_NORMAL;
1839 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1841 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1844 ast_debug(1, "Disabled conferencing\n");
1848 static int restore_conference(struct zt_pvt *p)
1851 if (p->saveconf.confmode) {
1852 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1853 p->saveconf.confmode = 0;
1855 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1859 ast_debug(1, "Restored conferencing\n");
1863 static int send_callerid(struct zt_pvt *p);
1865 static int send_cwcidspill(struct zt_pvt *p)
1869 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1871 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1872 /* Make sure we account for the end */
1873 p->cidlen += READ_SIZE * 4;
1876 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1880 static int has_voicemail(struct zt_pvt *p)
1883 struct ast_event *event;
1884 char *mailbox, *context;
1886 mailbox = context = ast_strdupa(p->mailbox);
1887 strsep(&context, "@");
1888 if (ast_strlen_zero(context))
1889 context = "default";
1891 event = ast_event_get_cached(AST_EVENT_MWI,
1892 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1893 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1894 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
1898 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
1899 ast_event_destroy(event);
1901 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
1906 static int send_callerid(struct zt_pvt *p)
1908 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1910 /* Take out of linear mode if necessary */
1911 if (p->subs[SUB_REAL].linear) {
1912 p->subs[SUB_REAL].linear = 0;
1913 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1915 while (p->cidpos < p->cidlen) {
1916 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1918 if (errno == EAGAIN)
1921 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1929 ast_free(p->cidspill);
1931 if (p->callwaitcas) {
1932 /* Wait for CID/CW to expire */
1933 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1935 restore_conference(p);
1939 static int zt_callwait(struct ast_channel *ast)
1941 struct zt_pvt *p = ast->tech_pvt;
1942 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1944 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1945 ast_free(p->cidspill);
1947 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1951 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1952 if (!p->callwaitrings && p->callwaitingcallerid) {
1953 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1955 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1957 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1959 p->cidlen = 2400 + READ_SIZE * 4;
1968 static unsigned char cid_pres2ss7pres(int cid_pres)
1970 return (cid_pres >> 5) & 0x03;
1973 static unsigned char cid_pres2ss7screen(int cid_pres)
1975 return cid_pres & 0x03;
1979 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1981 struct zt_pvt *p = ast->tech_pvt;
1982 int x, res, index,mysig;
1987 char dest[256]; /* must be same length as p->dialdest */
1988 ast_mutex_lock(&p->lock);
1989 ast_copy_string(dest, rdest, sizeof(dest));
1990 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1991 if ((ast->_state == AST_STATE_BUSY)) {
1992 p->subs[SUB_REAL].needbusy = 1;
1993 ast_mutex_unlock(&p->lock);
1996 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1997 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1998 ast_mutex_unlock(&p->lock);
2002 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
2004 /* Special pseudo -- automatically up */
2005 ast_setstate(ast, AST_STATE_UP);
2006 ast_mutex_unlock(&p->lock);
2009 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
2010 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
2012 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
2015 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
2018 if (p->outsigmod > -1)
2019 mysig = p->outsigmod;
2025 if (p->owner == ast) {
2026 /* Normal ring, on hook */
2028 /* Don't send audio while on hook, until the call is answered */
2030 if (p->use_callerid) {
2031 /* Generate the Caller-ID spill if desired */
2033 ast_log(LOG_WARNING, "cidspill already exists??\n");
2034 ast_free(p->cidspill);
2037 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2038 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2043 /* Choose proper cadence */
2044 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2045 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
2046 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
2047 p->cidrings = cidrings[p->distinctivering - 1];
2049 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
2050 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
2051 p->cidrings = p->sendcalleridafter;
2054 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2055 c = strchr(dest, '/');
2058 if (c && (strlen(c) < p->stripmsd)) {
2059 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2063 p->dop.op = ZT_DIAL_OP_REPLACE;
2064 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2065 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2067 p->dop.dialstr[0] = '\0';
2070 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2071 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2072 ast_mutex_unlock(&p->lock);
2077 /* Call waiting call */
2078 p->callwaitrings = 0;
2079 if (ast->cid.cid_num)
2080 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2082 p->callwait_num[0] = '\0';
2083 if (ast->cid.cid_name)
2084 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2086 p->callwait_name[0] = '\0';
2087 /* Call waiting tone instead */
2088 if (zt_callwait(ast)) {
2089 ast_mutex_unlock(&p->lock);
2092 /* Make ring-back */
2093 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2094 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2097 n = ast->cid.cid_name;
2098 l = ast->cid.cid_num;
2100 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2102 p->lastcid_num[0] = '\0';
2104 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2106 p->lastcid_name[0] = '\0';
2107 ast_setstate(ast, AST_STATE_RINGING);
2108 index = zt_get_index(ast, p, 0);
2110 p->subs[index].needringing = 1;
2123 case SIG_FGC_CAMAMF:
2128 case SIG_SF_FEATDMF:
2129 case SIG_FEATDMF_TA:
2131 c = strchr(dest, '/');
2136 if (strlen(c) < p->stripmsd) {
2137 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2138 ast_mutex_unlock(&p->lock);
2142 /* Start the trunk, if not GR-303 */
2146 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2148 if (errno != EINPROGRESS) {
2149 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2150 ast_mutex_unlock(&p->lock);
2157 ast_debug(1, "Dialing '%s'\n", c);
2158 p->dop.op = ZT_DIAL_OP_REPLACE;
2164 l = ast->cid.cid_num;
2166 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2168 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2171 l = ast->cid.cid_num;
2173 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2175 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2177 case SIG_FEATDMF_TA:
2179 const char *cic, *ozz;
2181 /* If you have to go through a Tandem Access point you need to use this */
2182 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2185 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2189 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2190 ast_mutex_unlock(&p->lock);
2193 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2194 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2199 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2202 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2204 case SIG_FGC_CAMAMF:
2206 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2210 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2212 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2216 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2217 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2218 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2219 p->echorest[sizeof(p->echorest) - 1] = '\0';
2221 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2225 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2227 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2228 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2229 ast_mutex_unlock(&p->lock);
2233 ast_debug(1, "Deferring dialing...\n");
2236 if (ast_strlen_zero(c))
2238 ast_setstate(ast, AST_STATE_DIALING);
2241 /* Special pseudo -- automatically up*/
2242 ast_setstate(ast, AST_STATE_UP);
2248 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2249 p->dialdest[0] = '\0';
2252 ast_debug(1, "not yet implemented\n");
2253 ast_mutex_unlock(&p->lock);
2258 char ss7_called_nai;
2259 int called_nai_strip;
2260 char ss7_calling_nai;
2261 int calling_nai_strip;
2262 const char *charge_str = NULL;
2264 const char *gen_address = NULL;
2267 c = strchr(dest, '/');
2273 if (!p->hidecallerid) {
2274 l = ast->cid.cid_num;
2279 if (ss7_grab(p, p->ss7)) {
2280 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2281 ast_mutex_unlock(&p->lock);
2284 p->digital = IS_DIGITAL(ast->transfercapability);
2285 p->ss7call = isup_new_call(p->ss7->ss7);
2289 ast_mutex_unlock(&p->lock);
2290 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2294 called_nai_strip = 0;
2295 ss7_called_nai = p->ss7->called_nai;
2296 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2297 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2298 called_nai_strip = strlen(p->ss7->internationalprefix);
2299 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2300 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2301 called_nai_strip = strlen(p->ss7->nationalprefix);
2302 ss7_called_nai = SS7_NAI_NATIONAL;
2304 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2307 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2309 calling_nai_strip = 0;
2310 ss7_calling_nai = p->ss7->calling_nai;
2311 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2312 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2313 calling_nai_strip = strlen(p->ss7->internationalprefix);
2314 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2315 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2316 calling_nai_strip = strlen(p->ss7->nationalprefix);
2317 ss7_calling_nai = SS7_NAI_NATIONAL;
2319 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2322 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2323 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2324 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2326 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
2327 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2329 /* Set the charge number if it is set */
2330 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
2332 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
2335 /* Set the generic address if it is set */
2336 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
2338 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 */
2341 isup_iam(p->ss7->ss7, p->ss7call);
2342 ast_setstate(ast, AST_STATE_DIALING);
2345 #endif /* HAVE_SS7 */
2349 #ifdef SUPPORT_USERUSER
2350 const char *useruser;
2354 int prilocaldialplan;
2358 int redirect_reason;
2360 c = strchr(dest, '/');
2369 if (!p->hidecallerid) {
2370 l = ast->cid.cid_num;
2371 if (!p->hidecalleridname) {
2372 n = ast->cid.cid_name;
2376 if (strlen(c) < p->stripmsd) {
2377 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2378 ast_mutex_unlock(&p->lock);
2381 if (mysig != SIG_FXSKS) {
2382 p->dop.op = ZT_DIAL_OP_REPLACE;
2383 s = strchr(c + p->stripmsd, 'w');
2386 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2388 p->dop.dialstr[0] = '\0';
2391 p->dop.dialstr[0] = '\0';
2394 if (pri_grab(p, p->pri)) {
2395 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2396 ast_mutex_unlock(&p->lock);
2399 if (!(p->call = pri_new_call(p->pri->pri))) {
2400 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2402 ast_mutex_unlock(&p->lock);
2405 if (!(sr = pri_sr_new())) {
2406 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2408 ast_mutex_unlock(&p->lock);
2410 if (p->bearer || (mysig == SIG_FXSKS)) {
2412 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2413 p->bearer->call = p->call;
2415 ast_debug(1, "I'm being setup with no bearer right now...\n");
2417 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2419 p->digital = IS_DIGITAL(ast->transfercapability);
2420 /* Add support for exclusive override */
2421 if (p->priexclusive)
2424 /* otherwise, traditional behavior */
2425 if (p->pri->nodetype == PRI_NETWORK)
2431 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2432 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2434 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2435 if (p->pri->facilityenable)
2436 pri_facility_enable(p->pri->pri);
2438 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2440 pridialplan = p->pri->dialplan - 1;
2441 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2442 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2443 if (pridialplan == -2) {
2444 dp_strip = strlen(p->pri->internationalprefix);
2446 pridialplan = PRI_INTERNATIONAL_ISDN;
2447 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2448 if (pridialplan == -2) {
2449 dp_strip = strlen(p->pri->nationalprefix);
2451 pridialplan = PRI_NATIONAL_ISDN;
2453 pridialplan = PRI_LOCAL_ISDN;
2456 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2457 switch (c[p->stripmsd]) {
2459 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2462 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2465 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2468 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2471 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2474 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2477 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2480 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2483 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2486 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2489 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2492 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2495 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2498 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2502 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2506 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2509 prilocaldialplan = p->pri->localdialplan - 1;
2510 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2511 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2512 if (prilocaldialplan == -2) {
2513 ldp_strip = strlen(p->pri->internationalprefix);
2515 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2516 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2517 if (prilocaldialplan == -2) {
2518 ldp_strip = strlen(p->pri->nationalprefix);
2520 prilocaldialplan = PRI_NATIONAL_ISDN;
2522 prilocaldialplan = PRI_LOCAL_ISDN;
2526 while (*l > '9' && *l != '*' && *l != '#') {
2529 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2532 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2535 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2538 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2541 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2544 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2547 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2550 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2553 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2556 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2559 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2562 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2565 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2568 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2572 ast_log(LOG_WARNING, "Unrecognized prilocaldialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2577 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2578 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2579 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2580 if (!strcasecmp(rr_str, "UNKNOWN"))
2581 redirect_reason = 0;
2582 else if (!strcasecmp(rr_str, "BUSY"))
2583 redirect_reason = 1;
2584 else if (!strcasecmp(rr_str, "NO_REPLY"))
2585 redirect_reason = 2;
2586 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2587 redirect_reason = 15;
2589 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2591 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2592 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2594 #ifdef SUPPORT_USERUSER
2595 /* User-user info */
2596 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2599 pri_sr_set_useruser(sr, useruser);
2602 if (pri_setup(p->pri->pri, p->call, sr)) {
2603 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2604 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2606 ast_mutex_unlock(&p->lock);
2611 ast_setstate(ast, AST_STATE_DIALING);
2615 ast_mutex_unlock(&p->lock);
2619 static void destroy_zt_pvt(struct zt_pvt **pvt)
2621 struct zt_pvt *p = *pvt;
2622 /* Remove channel from the list */
2624 p->prev->next = p->next;
2626 p->next->prev = p->prev;
2628 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2629 if (p->mwi_event_sub)
2630 ast_event_unsubscribe(p->mwi_event_sub);
2632 ast_variables_destroy(p->vars);
2633 ast_mutex_destroy(&p->lock);
2638 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2648 for (i = 0; i < 3; i++) {
2649 if (cur->subs[i].owner) {
2655 prev->next = cur->next;
2657 prev->next->prev = prev;
2663 iflist->prev = NULL;
2667 if (cur->subs[SUB_REAL].zfd > -1) {
2668 zt_close(cur->subs[SUB_REAL].zfd);
2670 destroy_zt_pvt(&cur);
2674 prev->next = cur->next;
2676 prev->next->prev = prev;
2682 iflist->prev = NULL;
2686 if (cur->subs[SUB_REAL].zfd > -1) {
2687 zt_close(cur->subs[SUB_REAL].zfd);
2689 destroy_zt_pvt(&cur);
2695 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2697 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2699 static char *zap_send_keypad_facility_descrip =
2700 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2701 " IE over the current channel.\n";
2703 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2705 /* Data will be our digit string */
2707 char *digits = (char *) data;
2709 if (ast_strlen_zero(digits)) {
2710 ast_debug(1, "No digit string sent to application!\n");
2714 p = (struct zt_pvt *)chan->tech_pvt;
2717 ast_debug(1, "Unable to find technology private\n");
2721 ast_mutex_lock(&p->lock);
2723 if (!p->pri || !p->call) {
2724 ast_debug(1, "Unable to find pri or call on channel!\n");
2725 ast_mutex_unlock(&p->lock);
2729 if (!pri_grab(p, p->pri)) {
2730 pri_keypad_facility(p->pri->pri, p->call, digits);
2733 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2734 ast_mutex_unlock(&p->lock);
2738 ast_mutex_unlock(&p->lock);
2743 static int pri_is_up(struct zt_pri *pri)
2746 for (x = 0; x < NUM_DCHANS; x++) {
2747 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2753 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2755 bearer->owner = &inuse;
2756 bearer->realcall = crv;
2757 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2758 if (crv->subs[SUB_REAL].owner)
2759 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].zfd);
2760 crv->bearer = bearer;
2761 crv->call = bearer->call;
2766 static char *pri_order(int level)
2776 return "Quaternary";
2782 /* Returns fd of the active dchan */
2783 static int pri_active_dchan_fd(struct zt_pri *pri)
2787 for (x = 0; x < NUM_DCHANS; x++) {
2788 if ((pri->dchans[x] == pri->pri))
2795 static int pri_find_dchan(struct zt_pri *pri)
2802 for (x = 0; x < NUM_DCHANS; x++) {
2803 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2805 if (pri->dchans[x] == old) {
2811 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2812 pri->dchannels[newslot]);
2814 if (old && (oldslot != newslot))
2815 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2816 pri->dchannels[oldslot], pri->dchannels[newslot]);
2817 pri->pri = pri->dchans[newslot];
2822 static int zt_hangup(struct ast_channel *ast)
2826 /*static int restore_gains(struct zt_pvt *p);*/
2827 struct zt_pvt *p = ast->tech_pvt;
2828 struct zt_pvt *tmp = NULL;
2829 struct zt_pvt *prev = NULL;
2832 ast_debug(1, "zt_hangup(%s)\n", ast->name);
2833 if (!ast->tech_pvt) {
2834 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2838 ast_mutex_lock(&p->lock);
2840 index = zt_get_index(ast, p, 1);
2842 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
2844 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2850 if (p->origcid_num) {
2851 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2852 ast_free(p->origcid_num);
2853 p->origcid_num = NULL;
2855 if (p->origcid_name) {
2856 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2857 ast_free(p->origcid_name);
2858 p->origcid_name = NULL;
2861 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2865 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2866 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2870 /* Real channel, do some fixup */
2871 p->subs[index].owner = NULL;
2872 p->subs[index].needanswer = 0;
2873 p->subs[index].needflash = 0;
2874 p->subs[index].needringing = 0;
2875 p->subs[index].needbusy = 0;
2876 p->subs[index].needcongestion = 0;
2877 p->subs[index].linear = 0;
2878 p->subs[index].needcallerid = 0;
2879 p->polarity = POLARITY_IDLE;
2880 zt_setlinear(p->subs[index].zfd, 0);
2881 if (index == SUB_REAL) {
2882 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2883 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
2884 if (p->subs[SUB_CALLWAIT].inthreeway) {
2885 /* We had flipped over to answer a callwait and now it's gone */
2886 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
2887 /* Move to the call-wait, but un-own us until they flip back. */
2888 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2889 unalloc_sub(p, SUB_CALLWAIT);
2892 /* The three way hung up, but we still have a call wait */
2893 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2894 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2895 unalloc_sub(p, SUB_THREEWAY);
2896 if (p->subs[SUB_REAL].inthreeway) {
2897 /* This was part of a three way call. Immediately make way for
2899 ast_debug(1, "Call was complete, setting owner to former third call\n");
2900 p->owner = p->subs[SUB_REAL].owner;
2902 /* This call hasn't been completed yet... Set owner to NULL */
2903 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
2906 p->subs[SUB_REAL].inthreeway = 0;
2908 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2909 /* Move to the call-wait and switch back to them. */
2910 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2911 unalloc_sub(p, SUB_CALLWAIT);
2912 p->owner = p->subs[SUB_REAL].owner;
2913 if (p->owner->_state != AST_STATE_UP)
2914 p->subs[SUB_REAL].needanswer = 1;
2915 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2916 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
2917 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2918 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2919 unalloc_sub(p, SUB_THREEWAY);
2920 if (p->subs[SUB_REAL].inthreeway) {
2921 /* This was part of a three way call. Immediately make way for
2923 ast_debug(1, "Call was complete, setting owner to former third call\n");
2924 p->owner = p->subs[SUB_REAL].owner;
2926 /* This call hasn't been completed yet... Set owner to NULL */
2927 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
2930 p->subs[SUB_REAL].inthreeway = 0;
2932 } else if (index == SUB_CALLWAIT) {
2933 /* Ditch the holding callwait call, and immediately make it availabe */
2934 if (p->subs[SUB_CALLWAIT].inthreeway) {
2935 /* This is actually part of a three way, placed on hold. Place the third part
2936 on music on hold now */
2937 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2938 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2939 S_OR(p->mohsuggest, NULL),
2940 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2942 p->subs[SUB_THREEWAY].inthreeway = 0;
2943 /* Make it the call wait now */
2944 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2945 unalloc_sub(p, SUB_THREEWAY);
2947 unalloc_sub(p, SUB_CALLWAIT);
2948 } else if (index == SUB_THREEWAY) {
2949 if (p->subs[SUB_CALLWAIT].inthreeway) {
2950 /* The other party of the three way call is currently in a call-wait state.
2951 Start music on hold for them, and take the main guy out of the third call */
2952 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
2953 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
2954 S_OR(p->mohsuggest, NULL),
2955 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2957 p->subs[SUB_CALLWAIT].inthreeway = 0;
2959 p->subs[SUB_REAL].inthreeway = 0;
2960 /* If this was part of a three way call index, let us make
2961 another three way call */
2962 unalloc_sub(p, SUB_THREEWAY);
2964 /* This wasn't any sort of call, but how are we an index? */
2965 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2969 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2972 p->distinctivering = 0;
2973 p->confirmanswer = 0;
2979 p->onhooktime = time(NULL);
2987 ast_dsp_free(p->dsp);
2991 law = ZT_LAW_DEFAULT;
2992 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2994 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2995 /* Perform low level hangup if no owner left */
2999 if (!ss7_grab(p, p->ss7)) {
3000 if (!p->alreadyhungup) {
3001 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
3002 int icause = ast->hangupcause ? ast->hangupcause : -1;
3006 icause = atoi(cause);
3008 isup_rel(p->ss7->ss7, p->ss7call, icause);
3010 p->alreadyhungup = 1;
3012 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
3014 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
3022 #ifdef SUPPORT_USERUSER
3023 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
3026 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
3027 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
3028 if (!pri_grab(p, p->pri)) {
3029 if (p->alreadyhungup) {
3030 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
3032 #ifdef SUPPORT_USERUSER
3033 pri_call_set_useruser(p->call, useruser);
3036 pri_hangup(p->pri->pri, p->call, -1);
3039 p->bearer->call = NULL;
3041 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
3042 int icause = ast->hangupcause ? ast->hangupcause : -1;
3043 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
3045 #ifdef SUPPORT_USERUSER
3046 pri_call_set_useruser(p->call, useruser);
3049 p->alreadyhungup = 1;
3051 p->bearer->alreadyhungup = 1;
3054 icause = atoi(cause);
3056 pri_hangup(p->pri->pri, p->call, icause);
3059 ast_log(LOG_WARNING, "pri_disconnect failed\n");
3062 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3067 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
3073 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7) && (p->sig != SIG_BRI) && (p->sig != SIG_BRI_PTMP)))
3074 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
3076 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
3082 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
3085 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
3087 /* If they're off hook, try playing congestion */
3088 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
3089 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
3091 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3097 /* Make sure we're not made available for at least two seconds assuming
3098 we were actually used for an inbound or outbound call. */
3099 if (ast->_state != AST_STATE_RESERVED) {
3100 time(&p->guardtime);
3105 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3108 ast_free(p->cidspill);
3112 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
3113 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
3117 p->callwaiting = p->permcallwaiting;
3118 p->hidecallerid = p->permhidecallerid;
3123 /* Restore data mode */
3124 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
3126 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
3130 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
3131 /* Free up the bearer channel as well, and
3132 don't use its file descriptor anymore */
3133 update_conf(p->bearer);
3134 reset_conf(p->bearer);
3135 p->bearer->owner = NULL;
3136 p->bearer->realcall = NULL;
3138 p->subs[SUB_REAL].zfd = -1;
3145 p->callwaitingrepeat = 0;
3148 ast->tech_pvt = NULL;
3149 ast_mutex_unlock(&p->lock);
3150 ast_module_unref(ast_module_info->self);
3151 ast_verb(3, "Hungup '%s'\n", ast->name);
3153 ast_mutex_lock(&iflock);
3159 destroy_channel(prev, tmp, 0);
3167 ast_mutex_unlock(&iflock);
3171 static int zt_answer(struct ast_channel *ast)
3173 struct zt_pvt *p = ast->tech_pvt;
3176 int oldstate = ast->_state;
3177 ast_setstate(ast, AST_STATE_UP);
3178 ast_mutex_lock(&p->lock);
3179 index = zt_get_index(ast, p, 0);
3182 /* nothing to do if a radio channel */
3183 if ((p->radio || (p->oprmode < 0))) {
3184 ast_mutex_unlock(&p->lock);
3198 case SIG_FEATDMF_TA:
3201 case SIG_FGC_CAMAMF:
3206 case SIG_SF_FEATDMF:
3211 /* Pick up the line */
3212 ast_debug(1, "Took %s off hook\n", ast->name);
3213 if (p->hanguponpolarityswitch) {
3214 p->polaritydelaytv = ast_tvnow();
3216 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
3217 tone_zone_play_tone(p->subs[index].zfd, -1);
3219 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
3220 if (oldstate == AST_STATE_RINGING) {
3221 ast_debug(1, "Finally swapping real and threeway\n");
3222 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3223 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3224 p->owner = p->subs[SUB_REAL].owner;
3227 if (p->sig & __ZT_SIG_FXS) {
3236 /* Send a pri acknowledge */
3237 if (!pri_grab(p, p->pri)) {
3239 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3242 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3249 if (!ss7_grab(p, p->ss7)) {
3251 res = isup_anm(p->ss7->ss7, p->ss7call);
3254 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3260 ast_mutex_unlock(&p->lock);
3263 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3266 ast_mutex_unlock(&p->lock);
3270 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3276 struct zt_pvt *p = chan->tech_pvt, *pp;
3277 struct oprmode *oprmode;
3280 /* all supported options require data */
3281 if (!data || (datalen < 1)) {
3287 case AST_OPTION_TXGAIN:
3288 scp = (signed char *) data;
3289 index = zt_get_index(chan, p, 0);
3291 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
3294 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
3295 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
3296 case AST_OPTION_RXGAIN:
3297 scp = (signed char *) data;
3298 index = zt_get_index(chan, p, 0);
3300 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
3303 ast_debug(1, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
3304 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
3305 case AST_OPTION_TONE_VERIFY:
3311 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
3312 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
3315 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
3316 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
3319 ast_debug(1, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
3320 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
3324 case AST_OPTION_TDD:
3325 /* turn on or off TDD */
3328 if (!*cp) { /* turn it off */
3329 ast_debug(1, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
3335 ast_debug(1, "Set option TDD MODE, value: %s(%d) on %s\n",
3336 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
3338 /* otherwise, turn it on */
3339 if (!p->didtdd) { /* if havent done it yet */
3340 unsigned char mybuf[41000], *buf;
3341 int size, res, fd, len;
3342 struct pollfd fds[1];
3345 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
3346 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
3348 index = zt_get_index(chan, p, 0);
3350 ast_log(LOG_WARNING, "No index in TDD?\n");
3353 fd = p->subs[index].zfd;
3355 if (ast_check_hangup(chan))
3358 if (size > READ_SIZE)
3361 fds[0].events = POLLPRI | POLLOUT;
3363 res = poll(fds, 1, -1);
3365 ast_debug(1, "poll (for write) ret. 0 on channel %d\n", p->channel);
3368 /* if got exception */
3369 if (fds[0].revents & POLLPRI)
3371 if (!(fds[0].revents & POLLOUT)) {
3372 ast_debug(1, "write fd not ready on channel %d\n", p->channel);
3375 res = write(fd, buf, size);
3377 if (res == -1) return -1;
3378 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
3384 p->didtdd = 1; /* set to have done it now */
3386 if (*cp == 2) { /* Mate mode */
3393 if (!p->tdd) { /* if we dont have one yet */
3394 p->tdd = tdd_new(); /* allocate one */
3397 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
3401 ast_debug(1, "Set option RELAX DTMF, value: %s(%d) on %s\n",