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>
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/logger.h"
77 #include "asterisk/module.h"
78 #include "asterisk/pbx.h"
79 #include "asterisk/options.h"
80 #include "asterisk/file.h"
81 #include "asterisk/ulaw.h"
82 #include "asterisk/alaw.h"
83 #include "asterisk/callerid.h"
84 #include "asterisk/adsi.h"
85 #include "asterisk/cli.h"
86 #include "asterisk/cdr.h"
87 #include "asterisk/features.h"
88 #include "asterisk/musiconhold.h"
89 #include "asterisk/say.h"
90 #include "asterisk/tdd.h"
91 #include "asterisk/app.h"
92 #include "asterisk/dsp.h"
93 #include "asterisk/astdb.h"
94 #include "asterisk/manager.h"
95 #include "asterisk/causes.h"
96 #include "asterisk/term.h"
97 #include "asterisk/utils.h"
98 #include "asterisk/transcap.h"
99 #include "asterisk/stringfields.h"
100 #include "asterisk/abstract_jb.h"
101 #include "asterisk/smdi.h"
102 #include "asterisk/astobj.h"
103 #include "asterisk/event.h"
105 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
107 #ifdef ZT_SPANINFO_HAS_LINECONFIG
108 static const char *lbostr[] = {
109 "0 db (CSU)/0-133 feet (DSX-1)",
110 "133-266 feet (DSX-1)",
111 "266-399 feet (DSX-1)",
112 "399-533 feet (DSX-1)",
113 "533-655 feet (DSX-1)",
120 /*! Global jitterbuffer configuration - by default, jb is disabled */
121 static struct ast_jb_conf default_jbconf =
125 .resync_threshold = -1,
128 static struct ast_jb_conf global_jbconf;
130 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_PRI) && !defined(ZT_SIG_HARDHDLC))
131 #error "Your zaptel is too old. Please update"
134 #ifndef ZT_TONEDETECT
135 /* Work around older code with no tone detect */
136 #define ZT_EVENT_DTMFDOWN 0
137 #define ZT_EVENT_DTMFUP 0
140 /* define this to send PRI user-user information elements */
141 #undef SUPPORT_USERUSER
144 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
145 * the user hangs up to reset the state machine so ring works properly.
146 * This is used to be able to support kewlstart by putting the zhone in
147 * groundstart mode since their forward disconnect supervision is entirely
148 * broken even though their documentation says it isn't and their support
149 * is entirely unwilling to provide any assistance with their channel banks
150 * even though their web site says they support their products for life.
152 /* #define ZHONE_HACK */
155 * Define if you want to check the hook state for an FXO (FXS signalled) interface
156 * before dialing on it. Certain FXO interfaces always think they're out of
157 * service with this method however.
159 /* #define ZAP_CHECK_HOOKSTATE */
161 /*! \brief Typically, how many rings before we should send Caller*ID */
162 #define DEFAULT_CIDRINGS 1
164 #define CHANNEL_PSEUDO -12
166 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
168 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
169 #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))
171 static const char tdesc[] = "Zapata Telephony Driver"
180 static const char config[] = "zapata.conf";
182 #define SIG_EM ZT_SIG_EM
183 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
184 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
185 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
186 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
187 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
188 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
189 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
190 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
191 #define SIG_FXSLS ZT_SIG_FXSLS
192 #define SIG_FXSGS ZT_SIG_FXSGS
193 #define SIG_FXSKS ZT_SIG_FXSKS
194 #define SIG_FXOLS ZT_SIG_FXOLS
195 #define SIG_FXOGS ZT_SIG_FXOGS
196 #define SIG_FXOKS ZT_SIG_FXOKS
197 #define SIG_PRI 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 */
408 struct zt_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
409 struct zt_pvt *crvs; /*!< Member CRV structs */
410 struct zt_pvt *crvend; /*!< Pointer to end of CRV structs */
414 static struct zt_pri pris[NUM_SPANS];
417 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
419 #define DEFAULT_PRI_DEBUG 0
422 static inline void pri_rel(struct zt_pri *pri)
424 ast_mutex_unlock(&pri->lock);
428 /*! Shut up the compiler */
432 #define SUB_REAL 0 /*!< Active call */
433 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
434 #define SUB_THREEWAY 2 /*!< Three-way call */
436 /* Polarity states */
437 #define POLARITY_IDLE 0
438 #define POLARITY_REV 1
441 static struct zt_distRings drings;
443 struct distRingData {
447 struct ringContextData {
448 char contextData[AST_MAX_CONTEXT];
450 struct zt_distRings {
451 struct distRingData ringnum[3];
452 struct ringContextData ringContext[3];
455 static char *subnames[] = {
461 struct zt_subchannel {
463 struct ast_channel *owner;
465 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
466 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
467 unsigned int needringing:1;
468 unsigned int needbusy:1;
469 unsigned int needcongestion:1;
470 unsigned int needcallerid:1;
471 unsigned int needanswer:1;
472 unsigned int needflash:1;
473 unsigned int needhold:1;
474 unsigned int needunhold:1;
475 unsigned int linear:1;
476 unsigned int inthreeway:1;
480 #define CONF_USER_REAL (1 << 0)
481 #define CONF_USER_THIRDCALL (1 << 1)
485 static struct zt_pvt {
487 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
488 /*!< Up to three channels can be associated with this call */
490 struct zt_subchannel sub_unused; /*!< Just a safety precaution */
491 struct zt_subchannel subs[3]; /*!< Sub-channels */
492 struct zt_confinfo saveconf; /*!< Saved conference info */
494 struct zt_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
495 struct zt_pvt *master; /*!< Master to us (we follow their conferencing) */
496 int inconference; /*!< If our real should be in the conference */
498 int sig; /*!< Signalling style */
499 int radio; /*!< radio type */
500 int outsigmod; /*!< Outbound Signalling style (modifier) */
501 int oprmode; /*!< "Operator Services" mode */
502 struct zt_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
503 float cid_rxgain; /*!< "Gain to apply during caller id */
506 int tonezone; /*!< tone zone for this chan, or -1 for default */
507 struct zt_pvt *next; /*!< Next channel in list */
508 struct zt_pvt *prev; /*!< Prev channel in list */
512 unsigned int answeronpolarityswitch:1;
513 unsigned int busydetect:1;
514 unsigned int callreturn:1;
515 unsigned int callwaiting:1;
516 unsigned int callwaitingcallerid:1;
517 unsigned int cancallforward:1;
518 unsigned int canpark:1;
519 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
520 unsigned int destroy:1;
521 unsigned int didtdd:1; /*!< flag to say its done it once */
522 unsigned int dialednone:1;
523 unsigned int dialing:1;
524 unsigned int digital:1;
526 unsigned int echobreak:1;
527 unsigned int echocanbridged:1;
528 unsigned int echocanon:1;
529 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
530 unsigned int firstradio:1;
531 unsigned int hanguponpolarityswitch:1;
532 unsigned int hardwaredtmf:1;
533 unsigned int hidecallerid:1;
534 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
535 unsigned int ignoredtmf:1;
536 unsigned int immediate:1; /*!< Answer before getting digits? */
537 unsigned int inalarm:1;
538 unsigned int mate:1; /*!< flag to say its in MATE mode */
539 unsigned int outgoing:1;
540 /* unsigned int overlapdial:1; unused and potentially confusing */
541 unsigned int permcallwaiting:1;
542 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
543 unsigned int priindication_oob:1;
544 unsigned int priexclusive:1;
545 unsigned int pulse:1;
546 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
547 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
548 unsigned int threewaycalling:1;
549 unsigned int transfer:1;
550 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
551 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
552 unsigned int usedistinctiveringdetection:1;
553 unsigned int zaptrcallerid:1; /*!< should we use the callerid from incoming call on zap transfer or not */
554 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
555 /* Channel state or unavilability flags */
556 unsigned int inservice:1;
557 unsigned int locallyblocked:1;
558 unsigned int remotelyblocked:1;
559 #if defined(HAVE_PRI) || defined(HAVE_SS7)
560 unsigned int alerting:1;
561 unsigned int alreadyhungup:1;
562 unsigned int isidlecall:1;
563 unsigned int proceeding:1;
564 unsigned int progress:1;
565 unsigned int resetting:1;
566 unsigned int setup_ack:1;
568 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
569 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
571 struct zt_distRings drings;
573 char context[AST_MAX_CONTEXT];
574 char defcontext[AST_MAX_CONTEXT];
575 char exten[AST_MAX_EXTENSION];
576 char language[MAX_LANGUAGE];
577 char mohinterpret[MAX_MUSICCLASS];
578 char mohsuggest[MAX_MUSICCLASS];
579 #if defined(PRI_ANI) || defined(HAVE_SS7)
580 char cid_ani[AST_MAX_EXTENSION];
583 char cid_num[AST_MAX_EXTENSION];
584 int cid_ton; /*!< Type Of Number (TON) */
585 char cid_name[AST_MAX_EXTENSION];
586 char lastcid_num[AST_MAX_EXTENSION];
587 char lastcid_name[AST_MAX_EXTENSION];
588 char *origcid_num; /*!< malloced original callerid */
589 char *origcid_name; /*!< malloced original callerid */
590 char callwait_num[AST_MAX_EXTENSION];
591 char callwait_name[AST_MAX_EXTENSION];
592 char rdnis[AST_MAX_EXTENSION];
593 char dnid[AST_MAX_EXTENSION];
596 int confno; /*!< Our conference */
597 int confusers; /*!< Who is using our conference */
598 int propconfno; /*!< Propagated conference number */
599 ast_group_t callgroup;
600 ast_group_t pickupgroup;
601 struct ast_variable *vars;
602 int channel; /*!< Channel Number or CRV */
603 int span; /*!< Span number */
604 time_t guardtime; /*!< Must wait this much time before using for new call */
605 int cid_signalling; /*!< CID signalling type bell202 or v23 */
606 int cid_start; /*!< CID start indicator, polarity or ring */
607 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
608 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
609 int cidcwexpire; /*!< When to expire our muting for CID/CW */
610 unsigned char *cidspill;
623 int busy_quietlength;
625 struct timeval flashtime; /*!< Last flash-hook time */
627 int cref; /*!< Call reference number */
628 ZT_DIAL_OPERATION dop;
629 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
631 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
632 int amaflags; /*!< AMA Flags */
633 struct tdd_state *tdd; /*!< TDD flag */
634 char call_forward[AST_MAX_EXTENSION];
635 char mailbox[AST_MAX_EXTENSION];
636 struct ast_event_sub *mwi_event_sub;
640 int distinctivering; /*!< Which distinctivering to use */
641 int cidrings; /*!< Which ring to deliver CID on */
642 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
644 int polarityonanswerdelay;
645 struct timeval polaritydelaytv;
646 int sendcalleridafter;
649 struct zt_pvt *bearer;
650 struct zt_pvt *realcall;
659 struct isup_call *ss7call;
660 char charge_number[50];
662 int cic; /*!< CIC associated with channel */
663 unsigned int dpc; /*!< CIC's DPC */
664 unsigned int loopedback:1;
667 } *iflist = NULL, *ifend = NULL;
669 /*! \brief Channel configuration from zapata.conf .
670 * This struct is used for parsing the [channels] section of zapata.conf.
671 * Generally there is a field here for every possible configuration item.
673 * The state of fields is saved along the parsing and whenever a 'channel'
674 * statement is reached, the current zt_chan_conf is used to configure the
675 * channel (struct zt_pvt)
677 * \see zt_chan_init for the default values.
679 struct zt_chan_conf {
690 char smdi_port[SMDI_MAX_FILENAME_LEN];
693 /** returns a new zt_chan_conf with default values (by-value) */
694 static struct zt_chan_conf zt_chan_conf_default(void) {
695 /* recall that if a field is not included here it is initialized
698 struct zt_chan_conf conf = {
702 .switchtype = PRI_SWITCH_NI2,
703 .dialplan = PRI_NATIONAL_ISDN + 1,
704 .localdialplan = PRI_NATIONAL_ISDN + 1,
710 .internationalprefix = "",
711 .nationalprefix = "",
720 .called_nai = SS7_NAI_NATIONAL,
721 .calling_nai = SS7_NAI_NATIONAL,
722 .internationalprefix = "",
723 .nationalprefix = "",
724 .subscriberprefix = "",
729 .context = "default",
732 .mohinterpret = "default",
736 .cid_signalling = CID_SIG_BELL,
737 .cid_start = CID_START_RING,
756 .polarityonanswerdelay = 600,
758 .sendcalleridafter = DEFAULT_CIDRINGS
770 .smdi_port = "/dev/ttyS0",
777 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
778 static int zt_digit_begin(struct ast_channel *ast, char digit);
779 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
780 static int zt_sendtext(struct ast_channel *c, const char *text);
781 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
782 static int zt_hangup(struct ast_channel *ast);
783 static int zt_answer(struct ast_channel *ast);
784 static struct ast_frame *zt_read(struct ast_channel *ast);
785 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
786 static struct ast_frame *zt_exception(struct ast_channel *ast);
787 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
788 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
789 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
790 static int zt_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
792 static const struct ast_channel_tech zap_tech = {
794 .description = tdesc,
795 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
796 .requester = zt_request,
797 .send_digit_begin = zt_digit_begin,
798 .send_digit_end = zt_digit_end,
799 .send_text = zt_sendtext,
806 .exception = zt_exception,
807 .indicate = zt_indicate,
809 .setoption = zt_setoption,
810 .func_channel_read = zt_func_read,
814 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
816 #define GET_CHANNEL(p) ((p)->channel)
819 struct zt_pvt *round_robin[32];
822 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
825 /* Grab the lock first */
827 res = ast_mutex_trylock(&pri->lock);
829 ast_mutex_unlock(&pvt->lock);
830 /* Release the lock and try again */
832 ast_mutex_lock(&pvt->lock);
835 /* Then break the poll */
836 pthread_kill(pri->master, SIGURG);
842 static inline void ss7_rel(struct zt_ss7 *ss7)
844 ast_mutex_unlock(&ss7->lock);
847 static inline int ss7_grab(struct zt_pvt *pvt, struct zt_ss7 *pri)
850 /* Grab the lock first */
852 res = ast_mutex_trylock(&pri->lock);
854 ast_mutex_unlock(&pvt->lock);
855 /* Release the lock and try again */
857 ast_mutex_lock(&pvt->lock);
860 /* Then break the poll */
861 pthread_kill(pri->master, SIGURG);
865 #define NUM_CADENCE_MAX 25
866 static int num_cadence = 4;
867 static int user_has_defined_cadences = 0;
869 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
870 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
871 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
872 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
873 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
876 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
877 * is 1, the second pause is 2 and so on.
880 static int cidrings[NUM_CADENCE_MAX] = {
881 2, /*!< Right after first long ring */
882 4, /*!< Right after long part */
883 3, /*!< After third chirp */
884 2, /*!< Second spell */
887 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
888 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
890 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
891 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
893 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
896 if (p->subs[0].owner == ast)
898 else if (p->subs[1].owner == ast)
900 else if (p->subs[2].owner == ast)
905 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
911 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
913 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
918 ast_mutex_unlock(&pri->lock);
921 if (p->subs[a].owner) {
922 if (ast_channel_trylock(p->subs[a].owner)) {
923 ast_mutex_unlock(&p->lock);
925 ast_mutex_lock(&p->lock);
927 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
928 ast_channel_unlock(p->subs[a].owner);
936 ast_mutex_lock(&pri->lock);
940 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *data)
943 struct zt_pri *pri = (struct zt_pri*) data;
946 struct zt_ss7 *ss7 = (struct zt_ss7*) data;
948 /* We must unlock the PRI to avoid the possibility of a deadlock */
949 #if defined(HAVE_PRI) || defined(HAVE_SS7)
954 ast_mutex_unlock(&pri->lock);
959 ast_mutex_unlock(&ss7->lock);
969 if (ast_channel_trylock(p->owner)) {
970 ast_mutex_unlock(&p->lock);
972 ast_mutex_lock(&p->lock);
974 ast_queue_frame(p->owner, f);
975 ast_channel_unlock(p->owner);
981 #if defined(HAVE_PRI) || defined(HAVE_SS7)
986 ast_mutex_lock(&pri->lock);
991 ast_mutex_lock(&ss7->lock);
1002 static int restore_gains(struct zt_pvt *p);
1004 static void swap_subs(struct zt_pvt *p, int a, int b)
1008 struct ast_channel *towner;
1010 ast_debug(1, "Swapping %d and %d\n", a, b);
1012 tchan = p->subs[a].chan;
1013 towner = p->subs[a].owner;
1014 tinthreeway = p->subs[a].inthreeway;
1016 p->subs[a].chan = p->subs[b].chan;
1017 p->subs[a].owner = p->subs[b].owner;
1018 p->subs[a].inthreeway = p->subs[b].inthreeway;
1020 p->subs[b].chan = tchan;
1021 p->subs[b].owner = towner;
1022 p->subs[b].inthreeway = tinthreeway;
1024 if (p->subs[a].owner)
1025 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].zfd);
1026 if (p->subs[b].owner)
1027 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].zfd);
1028 wakeup_sub(p, a, NULL);
1029 wakeup_sub(p, b, NULL);
1032 static int zt_open(char *fn)
1040 for (x = 0; x < strlen(fn); x++) {
1041 if (!isdigit(fn[x])) {
1049 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1052 fn = "/dev/zap/channel";
1054 fd = open(fn, O_RDWR | O_NONBLOCK);
1056 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1060 if (ioctl(fd, ZT_SPECIFY, &chan)) {
1064 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1069 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) {
1070 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
1079 static void zt_close(int fd)
1085 static int zt_setlinear(int zfd, int linear)
1088 res = ioctl(zfd, ZT_SETLINEAR, &linear);
1095 static int alloc_sub(struct zt_pvt *p, int x)
1099 if (p->subs[x].zfd < 0) {
1100 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
1101 if (p->subs[x].zfd > -1) {
1102 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
1104 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
1105 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
1106 bi.numbufs = numbufs;
1107 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
1109 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
1112 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
1113 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
1114 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
1115 zt_close(p->subs[x].zfd);
1116 p->subs[x].zfd = -1;
1119 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1122 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1125 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1129 static int unalloc_sub(struct zt_pvt *p, int x)
1132 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1135 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1136 if (p->subs[x].zfd > -1) {
1137 zt_close(p->subs[x].zfd);
1139 p->subs[x].zfd = -1;
1140 p->subs[x].linear = 0;
1141 p->subs[x].chan = 0;
1142 p->subs[x].owner = NULL;
1143 p->subs[x].inthreeway = 0;
1144 p->polarity = POLARITY_IDLE;
1145 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1149 static int digit_to_dtmfindex(char digit)
1152 return ZT_TONE_DTMF_BASE + (digit - '0');
1153 else if (digit >= 'A' && digit <= 'D')
1154 return ZT_TONE_DTMF_A + (digit - 'A');
1155 else if (digit >= 'a' && digit <= 'd')
1156 return ZT_TONE_DTMF_A + (digit - 'a');
1157 else if (digit == '*')
1158 return ZT_TONE_DTMF_s;
1159 else if (digit == '#')
1160 return ZT_TONE_DTMF_p;
1165 static int zt_digit_begin(struct ast_channel *chan, char digit)
1171 pvt = chan->tech_pvt;
1173 ast_mutex_lock(&pvt->lock);
1175 index = zt_get_index(chan, pvt, 0);
1177 if ((index != SUB_REAL) || !pvt->owner)
1181 if ((pvt->sig == SIG_PRI) && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1182 if (pvt->setup_ack) {
1183 if (!pri_grab(pvt, pvt->pri)) {
1184 pri_information(pvt->pri->pri, pvt->call, digit);
1187 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1188 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1190 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1191 res = strlen(pvt->dialdest);
1192 pvt->dialdest[res++] = digit;
1193 pvt->dialdest[res] = '\0';
1198 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1201 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
1203 ZT_DIAL_OPERATION zo = {
1204 .op = ZT_DIAL_OP_APPEND,
1207 zo.dialstr[0] = 'T';
1208 zo.dialstr[1] = digit;
1209 zo.dialstr[2] = '\0';
1210 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
1211 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
1215 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1217 pvt->begindigit = digit;
1221 ast_mutex_unlock(&pvt->lock);
1226 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1233 pvt = chan->tech_pvt;
1235 ast_mutex_lock(&pvt->lock);
1237 index = zt_get_index(chan, pvt, 0);
1239 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1243 /* This means that the digit was already sent via PRI signalling */
1244 if (pvt->sig == SIG_PRI && !pvt->begindigit)
1248 if (pvt->begindigit) {
1250 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1251 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
1253 pvt->begindigit = 0;
1257 ast_mutex_unlock(&pvt->lock);
1262 static char *events[] = {
1275 "Hook Transition Complete",
1280 "Polarity Reversal",
1288 { ZT_ALARM_RED, "Red Alarm" },
1289 { ZT_ALARM_YELLOW, "Yellow Alarm" },
1290 { ZT_ALARM_BLUE, "Blue Alarm" },
1291 { ZT_ALARM_RECOVER, "Recovering" },
1292 { ZT_ALARM_LOOPBACK, "Loopback" },
1293 { ZT_ALARM_NOTOPEN, "Not Open" },
1294 { ZT_ALARM_NONE, "None" },
1297 static char *alarm2str(int alarm)
1300 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1301 if (alarms[x].alarm & alarm)
1302 return alarms[x].name;
1304 return alarm ? "Unknown Alarm" : "No Alarm";
1307 static char *event2str(int event)
1309 static char buf[256];
1310 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1311 return events[event];
1312 sprintf(buf, "Event %d", event); /* safe */
1317 static char *dialplan2str(int dialplan)
1319 if (dialplan == -1 || dialplan == -2) {
1320 return("Dynamically set dialplan in ISDN");
1322 return (pri_plan2str(dialplan));
1326 static char *zap_sig2str(int sig)
1328 static char buf[256];
1331 return "E & M Immediate";
1333 return "E & M Wink";
1337 return "Feature Group D (DTMF)";
1339 return "Feature Group D (MF)";
1340 case SIG_FEATDMF_TA:
1341 return "Feature Groud D (MF) Tandem Access";
1343 return "Feature Group B (MF)";
1347 return "FGC/CAMA (Dialpulse)";
1348 case SIG_FGC_CAMAMF:
1349 return "FGC/CAMA (MF)";
1351 return "FXS Loopstart";
1353 return "FXS Groundstart";
1355 return "FXS Kewlstart";
1357 return "FXO Loopstart";
1359 return "FXO Groundstart";
1361 return "FXO Kewlstart";
1367 return "SF (Tone) Immediate";
1369 return "SF (Tone) Wink";
1371 return "SF (Tone) with Feature Group D (DTMF)";
1372 case SIG_SF_FEATDMF:
1373 return "SF (Tone) with Feature Group D (MF)";
1375 return "SF (Tone) with Feature Group B (MF)";
1376 case SIG_GR303FXOKS:
1377 return "GR-303 with FXOKS";
1378 case SIG_GR303FXSKS:
1379 return "GR-303 with FXSKS";
1383 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1388 #define sig2str zap_sig2str
1390 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
1392 /* If the conference already exists, and we're already in it
1393 don't bother doing anything */
1396 memset(&zi, 0, sizeof(zi));
1399 if (slavechannel > 0) {
1400 /* If we have only one slave, do a digital mon */
1401 zi.confmode = ZT_CONF_DIGITALMON;
1402 zi.confno = slavechannel;
1405 /* Real-side and pseudo-side both participate in conference */
1406 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
1407 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
1409 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
1410 zi.confno = p->confno;
1412 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1416 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1417 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
1420 if (slavechannel < 1) {
1421 p->confno = zi.confno;
1423 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1424 ast_debug(1, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1428 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
1430 /* If they're listening to our channel, they're ours */
1431 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
1433 /* If they're a talker on our (allocated) conference, they're ours */
1434 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
1439 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
1442 if (/* Can't delete if there's no zfd */
1444 /* Don't delete from the conference if it's not our conference */
1446 /* Don't delete if we don't think it's conferenced at all (implied) */
1448 memset(&zi, 0, sizeof(zi));
1452 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
1453 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1456 ast_debug(1, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1457 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1461 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
1465 struct zt_pvt *slave = NULL;
1466 /* Start out optimistic */
1468 /* Update conference state in a stateless fashion */
1469 for (x = 0; x < 3; x++) {
1470 /* Any three-way calling makes slave native mode *definitely* out
1472 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1475 /* If we don't have any 3-way calls, check to see if we have
1476 precisely one slave */
1477 if (useslavenative) {
1478 for (x = 0; x < MAX_SLAVES; x++) {
1481 /* Whoops already have a slave! No
1482 slave native and stop right away */
1487 /* We have one slave so far */
1488 slave = p->slaves[x];
1493 /* If no slave, slave native definitely out */
1496 else if (slave->law != p->law) {
1502 return useslavenative;
1505 static int reset_conf(struct zt_pvt *p)
1508 memset(&zi, 0, sizeof(zi));
1510 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1511 if (p->subs[SUB_REAL].zfd > -1) {
1512 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
1513 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
1518 static int update_conf(struct zt_pvt *p)
1523 struct zt_pvt *slave = NULL;
1525 useslavenative = isslavenative(p, &slave);
1526 /* Start with the obvious, general stuff */
1527 for (x = 0; x < 3; x++) {
1528 /* Look for three way calls */
1529 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1530 conf_add(p, &p->subs[x], x, 0);
1533 conf_del(p, &p->subs[x], x);
1536 /* If we have a slave, add him to our conference now. or DAX
1537 if this is slave native */
1538 for (x = 0; x < MAX_SLAVES; x++) {
1541 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1543 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1548 /* If we're supposed to be in there, do so now */
1549 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1551 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1553 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1557 /* If we have a master, add ourselves to his conference */
1559 if (isslavenative(p->master, NULL)) {
1560 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1562 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1566 /* Nobody is left (or should be left) in our conference.
1570 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1574 static void zt_enable_ec(struct zt_pvt *p)
1581 ast_debug(1, "Echo cancellation already on\n");
1585 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1588 if (p->echocancel) {
1589 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1591 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
1593 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1596 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1598 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
1601 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1604 ast_debug(1, "No echo cancellation requested\n");
1607 static void zt_train_ec(struct zt_pvt *p)
1611 if (p && p->echocancel && p->echotraining) {
1612 x = p->echotraining;
1613 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
1615 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
1617 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1619 ast_debug(1, "No echo training requested\n");
1622 static void zt_disable_ec(struct zt_pvt *p)
1626 if (p->echocancel) {
1628 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
1630 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
1632 ast_debug(1, "disabled echo cancellation on channel %d\n", p->channel);
1637 static void fill_txgain(struct zt_gains *g, float gain, int law)
1641 float linear_gain = pow(10.0, gain / 20.0);
1645 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1647 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1648 if (k > 32767) k = 32767;
1649 if (k < -32767) k = -32767;
1650 g->txgain[j] = AST_LIN2A(k);
1657 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1659 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1660 if (k > 32767) k = 32767;
1661 if (k < -32767) k = -32767;
1662 g->txgain[j] = AST_LIN2MU(k);
1671 static void fill_rxgain(struct zt_gains *g, float gain, int law)
1675 float linear_gain = pow(10.0, gain / 20.0);
1679 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1681 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1682 if (k > 32767) k = 32767;
1683 if (k < -32767) k = -32767;
1684 g->rxgain[j] = AST_LIN2A(k);
1691 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1693 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1694 if (k > 32767) k = 32767;
1695 if (k < -32767) k = -32767;
1696 g->rxgain[j] = AST_LIN2MU(k);
1705 static int set_actual_txgain(int fd, int chan, float gain, int law)
1710 memset(&g, 0, sizeof(g));
1712 res = ioctl(fd, ZT_GETGAINS, &g);
1714 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1718 fill_txgain(&g, gain, law);
1720 return ioctl(fd, ZT_SETGAINS, &g);
1723 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1728 memset(&g, 0, sizeof(g));
1730 res = ioctl(fd, ZT_GETGAINS, &g);
1732 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1736 fill_rxgain(&g, gain, law);
1738 return ioctl(fd, ZT_SETGAINS, &g);
1741 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1743 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1746 static int bump_gains(struct zt_pvt *p)
1750 /* Bump receive gain by value stored in cid_rxgain */
1751 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1753 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1760 static int restore_gains(struct zt_pvt *p)
1764 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1766 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1773 static inline int zt_set_hook(int fd, int hs)
1778 res = ioctl(fd, ZT_HOOK, &x);
1781 if (errno == EINPROGRESS)
1783 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
1789 static inline int zt_confmute(struct zt_pvt *p, int muted)
1793 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1795 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
1797 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
1799 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
1801 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1805 static int save_conference(struct zt_pvt *p)
1807 struct zt_confinfo c;
1809 if (p->saveconf.confmode) {
1810 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1813 p->saveconf.chan = 0;
1814 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
1816 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1817 p->saveconf.confmode = 0;
1822 c.confmode = ZT_CONF_NORMAL;
1823 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
1825 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1828 ast_debug(1, "Disabled conferencing\n");
1832 static int restore_conference(struct zt_pvt *p)
1835 if (p->saveconf.confmode) {
1836 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
1837 p->saveconf.confmode = 0;
1839 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1843 ast_debug(1, "Restored conferencing\n");
1847 static int send_callerid(struct zt_pvt *p);
1849 static int send_cwcidspill(struct zt_pvt *p)
1853 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1855 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1856 /* Make sure we account for the end */
1857 p->cidlen += READ_SIZE * 4;
1860 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1864 static int has_voicemail(struct zt_pvt *p)
1867 struct ast_event *event;
1868 char *mailbox, *context;
1870 mailbox = context = ast_strdupa(p->mailbox);
1871 strsep(&context, "@");
1872 if (ast_strlen_zero(context))
1873 context = "default";
1875 event = ast_event_get_cached(AST_EVENT_MWI,
1876 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1877 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1878 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
1882 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
1883 ast_event_destroy(event);
1885 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
1890 static int send_callerid(struct zt_pvt *p)
1892 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1894 /* Take out of linear mode if necessary */
1895 if (p->subs[SUB_REAL].linear) {
1896 p->subs[SUB_REAL].linear = 0;
1897 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
1899 while (p->cidpos < p->cidlen) {
1900 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
1902 if (errno == EAGAIN)
1905 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
1913 ast_free(p->cidspill);
1915 if (p->callwaitcas) {
1916 /* Wait for CID/CW to expire */
1917 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
1919 restore_conference(p);
1923 static int zt_callwait(struct ast_channel *ast)
1925 struct zt_pvt *p = ast->tech_pvt;
1926 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
1928 ast_log(LOG_WARNING, "Spill already exists?!?\n");
1929 ast_free(p->cidspill);
1931 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
1935 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
1936 if (!p->callwaitrings && p->callwaitingcallerid) {
1937 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
1939 p->cidlen = 2400 + 680 + READ_SIZE * 4;
1941 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
1943 p->cidlen = 2400 + READ_SIZE * 4;
1952 static unsigned char cid_pres2ss7pres(int cid_pres)
1954 return (cid_pres >> 5) & 0x03;
1957 static unsigned char cid_pres2ss7screen(int cid_pres)
1959 return cid_pres & 0x03;
1963 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
1965 struct zt_pvt *p = ast->tech_pvt;
1966 int x, res, index,mysig;
1971 char dest[256]; /* must be same length as p->dialdest */
1972 ast_mutex_lock(&p->lock);
1973 ast_copy_string(dest, rdest, sizeof(dest));
1974 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
1975 if ((ast->_state == AST_STATE_BUSY)) {
1976 p->subs[SUB_REAL].needbusy = 1;
1977 ast_mutex_unlock(&p->lock);
1980 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
1981 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
1982 ast_mutex_unlock(&p->lock);
1986 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
1988 /* Special pseudo -- automatically up */
1989 ast_setstate(ast, AST_STATE_UP);
1990 ast_mutex_unlock(&p->lock);
1993 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
1994 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
1996 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
1999 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
2002 if (p->outsigmod > -1)
2003 mysig = p->outsigmod;
2009 if (p->owner == ast) {
2010 /* Normal ring, on hook */
2012 /* Don't send audio while on hook, until the call is answered */
2014 if (p->use_callerid) {
2015 /* Generate the Caller-ID spill if desired */
2017 ast_log(LOG_WARNING, "cidspill already exists??\n");
2018 ast_free(p->cidspill);
2021 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2022 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2027 /* Choose proper cadence */
2028 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2029 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
2030 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
2031 p->cidrings = cidrings[p->distinctivering - 1];
2033 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
2034 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
2035 p->cidrings = p->sendcalleridafter;
2038 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2039 c = strchr(dest, '/');
2042 if (c && (strlen(c) < p->stripmsd)) {
2043 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2047 p->dop.op = ZT_DIAL_OP_REPLACE;
2048 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2049 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2051 p->dop.dialstr[0] = '\0';
2054 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
2055 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2056 ast_mutex_unlock(&p->lock);
2061 /* Call waiting call */
2062 p->callwaitrings = 0;
2063 if (ast->cid.cid_num)
2064 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2066 p->callwait_num[0] = '\0';
2067 if (ast->cid.cid_name)
2068 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2070 p->callwait_name[0] = '\0';
2071 /* Call waiting tone instead */
2072 if (zt_callwait(ast)) {
2073 ast_mutex_unlock(&p->lock);
2076 /* Make ring-back */
2077 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
2078 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2081 n = ast->cid.cid_name;
2082 l = ast->cid.cid_num;
2084 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2086 p->lastcid_num[0] = '\0';
2088 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2090 p->lastcid_name[0] = '\0';
2091 ast_setstate(ast, AST_STATE_RINGING);
2092 index = zt_get_index(ast, p, 0);
2094 p->subs[index].needringing = 1;
2107 case SIG_FGC_CAMAMF:
2112 case SIG_SF_FEATDMF:
2113 case SIG_FEATDMF_TA:
2115 c = strchr(dest, '/');
2120 if (strlen(c) < p->stripmsd) {
2121 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2122 ast_mutex_unlock(&p->lock);
2126 /* Start the trunk, if not GR-303 */
2130 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2132 if (errno != EINPROGRESS) {
2133 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2134 ast_mutex_unlock(&p->lock);
2141 ast_debug(1, "Dialing '%s'\n", c);
2142 p->dop.op = ZT_DIAL_OP_REPLACE;
2148 l = ast->cid.cid_num;
2150 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2152 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2155 l = ast->cid.cid_num;
2157 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2159 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2161 case SIG_FEATDMF_TA:
2163 const char *cic, *ozz;
2165 /* If you have to go through a Tandem Access point you need to use this */
2166 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2169 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2173 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2174 ast_mutex_unlock(&p->lock);
2177 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2178 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2183 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2186 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2188 case SIG_FGC_CAMAMF:
2190 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2194 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2196 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2200 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2201 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2202 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2203 p->echorest[sizeof(p->echorest) - 1] = '\0';
2205 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2209 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
2211 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
2212 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
2213 ast_mutex_unlock(&p->lock);
2217 ast_debug(1, "Deferring dialing...\n");
2220 if (ast_strlen_zero(c))
2222 ast_setstate(ast, AST_STATE_DIALING);
2225 /* Special pseudo -- automatically up*/
2226 ast_setstate(ast, AST_STATE_UP);
2230 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2231 p->dialdest[0] = '\0';
2234 ast_debug(1, "not yet implemented\n");
2235 ast_mutex_unlock(&p->lock);
2240 char ss7_called_nai;
2241 int called_nai_strip;
2242 char ss7_calling_nai;
2243 int calling_nai_strip;
2244 const char *charge_str = NULL;
2246 c = strchr(dest, '/');
2252 if (!p->hidecallerid) {
2253 l = ast->cid.cid_num;
2258 if (ss7_grab(p, p->ss7)) {
2259 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2260 ast_mutex_unlock(&p->lock);
2263 p->digital = IS_DIGITAL(ast->transfercapability);
2264 p->ss7call = isup_new_call(p->ss7->ss7);
2268 ast_mutex_unlock(&p->lock);
2269 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2273 called_nai_strip = 0;
2274 ss7_called_nai = p->ss7->called_nai;
2275 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2276 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2277 called_nai_strip = strlen(p->ss7->internationalprefix);
2278 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2279 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2280 called_nai_strip = strlen(p->ss7->nationalprefix);
2281 ss7_called_nai = SS7_NAI_NATIONAL;
2283 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2286 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2288 calling_nai_strip = 0;
2289 ss7_calling_nai = p->ss7->calling_nai;
2290 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2291 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2292 calling_nai_strip = strlen(p->ss7->internationalprefix);
2293 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2294 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2295 calling_nai_strip = strlen(p->ss7->nationalprefix);
2296 ss7_calling_nai = SS7_NAI_NATIONAL;
2298 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2301 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2302 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2303 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2305 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
2306 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2308 /* Set the charge number if it is set */
2309 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
2311 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
2314 isup_iam(p->ss7->ss7, p->ss7call);
2315 ast_setstate(ast, AST_STATE_DIALING);
2318 #endif /* HAVE_SS7 */
2322 #ifdef SUPPORT_USERUSER
2323 const char *useruser;
2327 int prilocaldialplan;
2331 int redirect_reason;
2333 c = strchr(dest, '/');
2342 if (!p->hidecallerid) {
2343 l = ast->cid.cid_num;
2344 if (!p->hidecalleridname) {
2345 n = ast->cid.cid_name;
2349 if (strlen(c) < p->stripmsd) {
2350 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2351 ast_mutex_unlock(&p->lock);
2354 if (mysig != SIG_FXSKS) {
2355 p->dop.op = ZT_DIAL_OP_REPLACE;
2356 s = strchr(c + p->stripmsd, 'w');
2359 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2361 p->dop.dialstr[0] = '\0';
2364 p->dop.dialstr[0] = '\0';
2367 if (pri_grab(p, p->pri)) {
2368 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2369 ast_mutex_unlock(&p->lock);
2372 if (!(p->call = pri_new_call(p->pri->pri))) {
2373 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2375 ast_mutex_unlock(&p->lock);
2378 if (!(sr = pri_sr_new())) {
2379 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2381 ast_mutex_unlock(&p->lock);
2383 if (p->bearer || (mysig == SIG_FXSKS)) {
2385 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2386 p->bearer->call = p->call;
2388 ast_debug(1, "I'm being setup with no bearer right now...\n");
2390 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2392 p->digital = IS_DIGITAL(ast->transfercapability);
2393 /* Add support for exclusive override */
2394 if (p->priexclusive)
2397 /* otherwise, traditional behavior */
2398 if (p->pri->nodetype == PRI_NETWORK)
2404 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2405 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2407 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2408 if (p->pri->facilityenable)
2409 pri_facility_enable(p->pri->pri);
2411 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2413 pridialplan = p->pri->dialplan - 1;
2414 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2415 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2416 if (pridialplan == -2) {
2417 dp_strip = strlen(p->pri->internationalprefix);
2419 pridialplan = PRI_INTERNATIONAL_ISDN;
2420 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2421 if (pridialplan == -2) {
2422 dp_strip = strlen(p->pri->nationalprefix);
2424 pridialplan = PRI_NATIONAL_ISDN;
2426 pridialplan = PRI_LOCAL_ISDN;
2429 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2430 switch (c[p->stripmsd]) {
2432 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2435 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2438 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2441 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2444 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2447 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2450 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2453 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2456 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2459 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2462 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2465 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2468 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2471 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2475 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2479 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2482 prilocaldialplan = p->pri->localdialplan - 1;
2483 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2484 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2485 if (prilocaldialplan == -2) {
2486 ldp_strip = strlen(p->pri->internationalprefix);
2488 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2489 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2490 if (prilocaldialplan == -2) {
2491 ldp_strip = strlen(p->pri->nationalprefix);
2493 prilocaldialplan = PRI_NATIONAL_ISDN;
2495 prilocaldialplan = PRI_LOCAL_ISDN;
2499 while (*l > '9' && *l != '*' && *l != '#') {
2502 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2505 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2508 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2511 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2514 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2517 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2520 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2523 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2526 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2529 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2532 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2535 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2538 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2541 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2545 ast_log(LOG_WARNING, "Unrecognized prilocaldialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2550 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2551 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2552 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2553 if (!strcasecmp(rr_str, "UNKNOWN"))
2554 redirect_reason = 0;
2555 else if (!strcasecmp(rr_str, "BUSY"))
2556 redirect_reason = 1;
2557 else if (!strcasecmp(rr_str, "NO_REPLY"))
2558 redirect_reason = 2;
2559 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2560 redirect_reason = 15;
2562 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2564 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2565 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2567 #ifdef SUPPORT_USERUSER
2568 /* User-user info */
2569 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2572 pri_sr_set_useruser(sr, useruser);
2575 if (pri_setup(p->pri->pri, p->call, sr)) {
2576 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2577 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2579 ast_mutex_unlock(&p->lock);
2584 ast_setstate(ast, AST_STATE_DIALING);
2588 ast_mutex_unlock(&p->lock);
2592 static void destroy_zt_pvt(struct zt_pvt **pvt)
2594 struct zt_pvt *p = *pvt;
2595 /* Remove channel from the list */
2597 p->prev->next = p->next;
2599 p->next->prev = p->prev;
2601 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
2602 if (p->mwi_event_sub)
2603 ast_event_unsubscribe(p->mwi_event_sub);
2605 ast_variables_destroy(p->vars);
2606 ast_mutex_destroy(&p->lock);
2611 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
2621 for (i = 0; i < 3; i++) {
2622 if (cur->subs[i].owner) {
2628 prev->next = cur->next;
2630 prev->next->prev = prev;
2636 iflist->prev = NULL;
2640 if (cur->subs[SUB_REAL].zfd > -1) {
2641 zt_close(cur->subs[SUB_REAL].zfd);
2643 destroy_zt_pvt(&cur);
2647 prev->next = cur->next;
2649 prev->next->prev = prev;
2655 iflist->prev = NULL;
2659 if (cur->subs[SUB_REAL].zfd > -1) {
2660 zt_close(cur->subs[SUB_REAL].zfd);
2662 destroy_zt_pvt(&cur);
2668 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
2670 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2672 static char *zap_send_keypad_facility_descrip =
2673 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2674 " IE over the current channel.\n";
2676 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2678 /* Data will be our digit string */
2680 char *digits = (char *) data;
2682 if (ast_strlen_zero(digits)) {
2683 ast_debug(1, "No digit string sent to application!\n");
2687 p = (struct zt_pvt *)chan->tech_pvt;
2690 ast_debug(1, "Unable to find technology private\n");
2694 ast_mutex_lock(&p->lock);
2696 if (!p->pri || !p->call) {
2697 ast_debug(1, "Unable to find pri or call on channel!\n");
2698 ast_mutex_unlock(&p->lock);
2702 if (!pri_grab(p, p->pri)) {
2703 pri_keypad_facility(p->pri->pri, p->call, digits);
2706 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2707 ast_mutex_unlock(&p->lock);
2711 ast_mutex_unlock(&p->lock);
2716 static int pri_is_up(struct zt_pri *pri)
2719 for (x = 0; x < NUM_DCHANS; x++) {
2720 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2726 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
2728 bearer->owner = &inuse;
2729 bearer->realcall = crv;
2730 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2731 if (crv->subs[SUB_REAL].owner)
2732 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].zfd);
2733 crv->bearer = bearer;
2734 crv->call = bearer->call;
2739 static char *pri_order(int level)
2749 return "Quaternary";
2755 /* Returns fd of the active dchan */
2756 static int pri_active_dchan_fd(struct zt_pri *pri)
2760 for (x = 0; x < NUM_DCHANS; x++) {
2761 if ((pri->dchans[x] == pri->pri))
2768 static int pri_find_dchan(struct zt_pri *pri)
2775 for (x = 0; x < NUM_DCHANS; x++) {
2776 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2778 if (pri->dchans[x] == old) {
2784 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2785 pri->dchannels[newslot]);
2787 if (old && (oldslot != newslot))
2788 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2789 pri->dchannels[oldslot], pri->dchannels[newslot]);
2790 pri->pri = pri->dchans[newslot];
2795 static int zt_hangup(struct ast_channel *ast)
2799 /*static int restore_gains(struct zt_pvt *p);*/
2800 struct zt_pvt *p = ast->tech_pvt;
2801 struct zt_pvt *tmp = NULL;
2802 struct zt_pvt *prev = NULL;
2805 ast_debug(1, "zt_hangup(%s)\n", ast->name);
2806 if (!ast->tech_pvt) {
2807 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2811 ast_mutex_lock(&p->lock);
2813 index = zt_get_index(ast, p, 1);
2815 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
2817 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2823 if (p->origcid_num) {
2824 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2825 ast_free(p->origcid_num);
2826 p->origcid_num = NULL;
2828 if (p->origcid_name) {
2829 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2830 ast_free(p->origcid_name);
2831 p->origcid_name = NULL;
2834 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
2838 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
2839 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
2843 /* Real channel, do some fixup */
2844 p->subs[index].owner = NULL;
2845 p->subs[index].needanswer = 0;
2846 p->subs[index].needflash = 0;
2847 p->subs[index].needringing = 0;
2848 p->subs[index].needbusy = 0;
2849 p->subs[index].needcongestion = 0;
2850 p->subs[index].linear = 0;
2851 p->subs[index].needcallerid = 0;
2852 p->polarity = POLARITY_IDLE;
2853 zt_setlinear(p->subs[index].zfd, 0);
2854 if (index == SUB_REAL) {
2855 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
2856 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
2857 if (p->subs[SUB_CALLWAIT].inthreeway) {
2858 /* We had flipped over to answer a callwait and now it's gone */
2859 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
2860 /* Move to the call-wait, but un-own us until they flip back. */
2861 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2862 unalloc_sub(p, SUB_CALLWAIT);
2865 /* The three way hung up, but we still have a call wait */
2866 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
2867 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2868 unalloc_sub(p, SUB_THREEWAY);
2869 if (p->subs[SUB_REAL].inthreeway) {
2870 /* This was part of a three way call. Immediately make way for
2872 ast_debug(1, "Call was complete, setting owner to former third call\n");
2873 p->owner = p->subs[SUB_REAL].owner;
2875 /* This call hasn't been completed yet... Set owner to NULL */
2876 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
2879 p->subs[SUB_REAL].inthreeway = 0;
2881 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
2882 /* Move to the call-wait and switch back to them. */
2883 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
2884 unalloc_sub(p, SUB_CALLWAIT);
2885 p->owner = p->subs[SUB_REAL].owner;
2886 if (p->owner->_state != AST_STATE_UP)
2887 p->subs[SUB_REAL].needanswer = 1;
2888 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
2889 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
2890 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
2891 swap_subs(p, SUB_THREEWAY, SUB_REAL);
2892 unalloc_sub(p, SUB_THREEWAY);
2893 if (p->subs[SUB_REAL].inthreeway) {
2894 /* This was part of a three way call. Immediately make way for
2896 ast_debug(1, "Call was complete, setting owner to former third call\n");
2897 p->owner = p->subs[SUB_REAL].owner;
2899 /* This call hasn't been completed yet... Set owner to NULL */
2900 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
2903 p->subs[SUB_REAL].inthreeway = 0;
2905 } else if (index == SUB_CALLWAIT) {
2906 /* Ditch the holding callwait call, and immediately make it availabe */
2907 if (p->subs[SUB_CALLWAIT].inthreeway) {
2908 /* This is actually part of a three way, placed on hold. Place the third part
2909 on music on hold now */
2910 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
2911 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
2912 S_OR(p->mohsuggest, NULL),
2913 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2915 p->subs[SUB_THREEWAY].inthreeway = 0;
2916 /* Make it the call wait now */
2917 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
2918 unalloc_sub(p, SUB_THREEWAY);
2920 unalloc_sub(p, SUB_CALLWAIT);
2921 } else if (index == SUB_THREEWAY) {
2922 if (p->subs[SUB_CALLWAIT].inthreeway) {
2923 /* The other party of the three way call is currently in a call-wait state.
2924 Start music on hold for them, and take the main guy out of the third call */
2925 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
2926 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
2927 S_OR(p->mohsuggest, NULL),
2928 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
2930 p->subs[SUB_CALLWAIT].inthreeway = 0;
2932 p->subs[SUB_REAL].inthreeway = 0;
2933 /* If this was part of a three way call index, let us make
2934 another three way call */
2935 unalloc_sub(p, SUB_THREEWAY);
2937 /* This wasn't any sort of call, but how are we an index? */
2938 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
2942 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
2945 p->distinctivering = 0;
2946 p->confirmanswer = 0;
2952 p->onhooktime = time(NULL);
2960 ast_dsp_free(p->dsp);
2964 law = ZT_LAW_DEFAULT;
2965 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
2967 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
2968 /* Perform low level hangup if no owner left */
2972 if (!ss7_grab(p, p->ss7)) {
2973 if (!p->alreadyhungup) {
2974 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
2975 int icause = ast->hangupcause ? ast->hangupcause : -1;
2979 icause = atoi(cause);
2981 isup_rel(p->ss7->ss7, p->ss7call, icause);
2983 p->alreadyhungup = 1;
2985 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
2987 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
2995 #ifdef SUPPORT_USERUSER
2996 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
2999 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
3000 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
3001 if (!pri_grab(p, p->pri)) {
3002 if (p->alreadyhungup) {
3003 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
3005 #ifdef SUPPORT_USERUSER
3006 pri_call_set_useruser(p->call, useruser);
3009 pri_hangup(p->pri->pri, p->call, -1);
3012 p->bearer->call = NULL;
3014 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
3015 int icause = ast->hangupcause ? ast->hangupcause : -1;
3016 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
3018 #ifdef SUPPORT_USERUSER
3019 pri_call_set_useruser(p->call, useruser);
3022 p->alreadyhungup = 1;
3024 p->bearer->alreadyhungup = 1;
3027 icause = atoi(cause);
3029 pri_hangup(p->pri->pri, p->call, icause);
3032 ast_log(LOG_WARNING, "pri_disconnect failed\n");
3035 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3040 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
3046 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7)))
3047 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
3049 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
3055 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
3058 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
3060 /* If they're off hook, try playing congestion */
3061 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
3062 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
3064 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3070 /* Make sure we're not made available for at least two seconds assuming
3071 we were actually used for an inbound or outbound call. */
3072 if (ast->_state != AST_STATE_RESERVED) {
3073 time(&p->guardtime);
3078 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
3081 ast_free(p->cidspill);
3085 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
3086 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
3090 p->callwaiting = p->permcallwaiting;
3091 p->hidecallerid = p->permhidecallerid;
3096 /* Restore data mode */
3097 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
3099 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
3103 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
3104 /* Free up the bearer channel as well, and
3105 don't use its file descriptor anymore */
3106 update_conf(p->bearer);
3107 reset_conf(p->bearer);
3108 p->bearer->owner = NULL;
3109 p->bearer->realcall = NULL;
3111 p->subs[SUB_REAL].zfd = -1;
3118 p->callwaitingrepeat = 0;
3121 ast->tech_pvt = NULL;
3122 ast_mutex_unlock(&p->lock);
3123 ast_module_unref(ast_module_info->self);
3124 ast_verb(3, "Hungup '%s'\n", ast->name);
3126 ast_mutex_lock(&iflock);
3132 destroy_channel(prev, tmp, 0);
3140 ast_mutex_unlock(&iflock);
3144 static int zt_answer(struct ast_channel *ast)
3146 struct zt_pvt *p = ast->tech_pvt;
3149 int oldstate = ast->_state;
3150 ast_setstate(ast, AST_STATE_UP);
3151 ast_mutex_lock(&p->lock);
3152 index = zt_get_index(ast, p, 0);
3155 /* nothing to do if a radio channel */
3156 if ((p->radio || (p->oprmode < 0))) {
3157 ast_mutex_unlock(&p->lock);
3171 case SIG_FEATDMF_TA:
3174 case SIG_FGC_CAMAMF:
3179 case SIG_SF_FEATDMF:
3184 /* Pick up the line */
3185 ast_debug(1, "Took %s off hook\n", ast->name);
3186 if (p->hanguponpolarityswitch) {
3187 p->polaritydelaytv = ast_tvnow();
3189 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
3190 tone_zone_play_tone(p->subs[index].zfd, -1);
3192 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
3193 if (oldstate == AST_STATE_RINGING) {
3194 ast_debug(1, "Finally swapping real and threeway\n");
3195 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
3196 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3197 p->owner = p->subs[SUB_REAL].owner;
3200 if (p->sig & __ZT_SIG_FXS) {
3207 /* Send a pri acknowledge */
3208 if (!pri_grab(p, p->pri)) {
3210 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
3213 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3220 if (!ss7_grab(p, p->ss7)) {
3222 res = isup_anm(p->ss7->ss7, p->ss7call);
3225 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
3231 ast_mutex_unlock(&p->lock);
3234 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
3237 ast_mutex_unlock(&p->lock);
3241 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
3247 struct zt_pvt *p = chan->tech_pvt, *pp;
3248 struct oprmode *oprmode;
3251 /* all supported options require data */
3252 if (!data || (datalen < 1)) {
3258 case AST_OPTION_TXGAIN:
3259 scp = (signed char *) data;
3260 index = zt_get_index(chan, p, 0);
3262 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
3265 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
3266 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
3267 case AST_OPTION_RXGAIN:
3268 scp = (signed char *) data;
3269 index = zt_get_index(chan, p, 0);
3271 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
3274 ast_debug(1, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
3275 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
3276 case AST_OPTION_TONE_VERIFY:
3282 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
3283 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */
3286 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
3287 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */
3290 ast_debug(1, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
3291 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */
3295 case AST_OPTION_TDD:
3296 /* turn on or off TDD */
3299 if (!*cp) { /* turn it off */
3300 ast_debug(1, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
3306 ast_debug(1, "Set option TDD MODE, value: %s(%d) on %s\n",
3307 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
3309 /* otherwise, turn it on */
3310 if (!p->didtdd) { /* if havent done it yet */
3311 unsigned char mybuf[41000], *buf;
3312 int size, res, fd, len;
3313 struct pollfd fds[1];
3316 memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
3317 ast_tdd_gen_ecdisa(buf + 16000, 16000); /* put in tone */
3319 index = zt_get_index(chan, p, 0);
3321 ast_log(LOG_WARNING, "No index in TDD?\n");
3324 fd = p->subs[index].zfd;
3326 if (ast_check_hangup(chan))
3329 if (size > READ_SIZE)
3332 fds[0].events = POLLPRI | POLLOUT;
3334 res = poll(fds, 1, -1);
3336 ast_debug(1, "poll (for write) ret. 0 on channel %d\n", p->channel);
3339 /* if got exception */
3340 if (fds[0].revents & POLLPRI)
3342 if (!(fds[0].revents & POLLOUT)) {
3343 ast_debug(1, "write fd not ready on channel %d\n", p->channel);
3346 res = write(fd, buf, size);
3348 if (res == -1) return -1;
3349 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
3355 p->didtdd = 1; /* set to have done it now */
3357 if (*cp == 2) { /* Mate mode */
3364 if (!p->tdd) { /* if we dont have one yet */
3365 p->tdd = tdd_new(); /* allocate one */
3368 case AST_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */
3372 ast_debug(1, "Set option RELAX DTMF, value: %s(%d) on %s\n",
3373 *cp ? "ON" : "OFF", (int) *cp, chan->name);
3374 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
3376 case AST_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */
3379 ast_debug(1, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
3383 ast_debug(1, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
3386 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
3387 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
3389 case AST_OPTION_OPRMODE: /* Operator services mode */
3390 oprmode = (struct oprmode *) data;
3391 pp = oprmode->peer->tech_pvt;
3392 p->oprmode = pp->oprmode = 0;
3396 /* setup modes, if any */
3399 pp->oprmode = oprmode->mode;