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 DAHDI for Pseudo TDM
23 * \author Mark Spencer <markster@digium.com>
25 * Connects to the DAHDI 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 DAHDI channel.
33 * \arg \ref Config_dahdi
35 * \ingroup channel_drivers
37 * \todo Deprecate the "musiconhold" configuration option post 1.4
41 <depend>res_smdi</depend>
42 <depend>dahdi</depend>
43 <depend>tonezone</depend>
50 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
56 #include <sys/signal.h>
58 #include <sys/ioctl.h>
62 #include <dahdi/user.h>
63 #include <dahdi/tonezone.h>
73 #include "asterisk/lock.h"
74 #include "asterisk/channel.h"
75 #include "asterisk/config.h"
76 #include "asterisk/module.h"
77 #include "asterisk/pbx.h"
78 #include "asterisk/file.h"
79 #include "asterisk/ulaw.h"
80 #include "asterisk/alaw.h"
81 #include "asterisk/callerid.h"
82 #include "asterisk/adsi.h"
83 #include "asterisk/cli.h"
84 #include "asterisk/cdr.h"
85 #include "asterisk/features.h"
86 #include "asterisk/musiconhold.h"
87 #include "asterisk/say.h"
88 #include "asterisk/tdd.h"
89 #include "asterisk/app.h"
90 #include "asterisk/dsp.h"
91 #include "asterisk/astdb.h"
92 #include "asterisk/manager.h"
93 #include "asterisk/causes.h"
94 #include "asterisk/term.h"
95 #include "asterisk/utils.h"
96 #include "asterisk/transcap.h"
97 #include "asterisk/stringfields.h"
98 #include "asterisk/abstract_jb.h"
99 #include "asterisk/smdi.h"
100 #include "asterisk/astobj.h"
101 #include "asterisk/event.h"
102 #include "asterisk/devicestate.h"
104 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
106 #ifdef DAHDI_SPANINFO_HAS_LINECONFIG
107 static const char *lbostr[] = {
108 "0 db (CSU)/0-133 feet (DSX-1)",
109 "133-266 feet (DSX-1)",
110 "266-399 feet (DSX-1)",
111 "399-533 feet (DSX-1)",
112 "533-655 feet (DSX-1)",
119 /*! Global jitterbuffer configuration - by default, jb is disabled */
120 static struct ast_jb_conf default_jbconf =
124 .resync_threshold = -1,
127 static struct ast_jb_conf global_jbconf;
129 /* define this to send PRI user-user information elements */
130 #undef SUPPORT_USERUSER
133 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
134 * the user hangs up to reset the state machine so ring works properly.
135 * This is used to be able to support kewlstart by putting the zhone in
136 * groundstart mode since their forward disconnect supervision is entirely
137 * broken even though their documentation says it isn't and their support
138 * is entirely unwilling to provide any assistance with their channel banks
139 * even though their web site says they support their products for life.
141 /* #define ZHONE_HACK */
144 * Define if you want to check the hook state for an FXO (FXS signalled) interface
145 * before dialing on it. Certain FXO interfaces always think they're out of
146 * service with this method however.
148 /* #define DAHDI_CHECK_HOOKSTATE */
150 /*! \brief Typically, how many rings before we should send Caller*ID */
151 #define DEFAULT_CIDRINGS 1
153 #define CHANNEL_PSEUDO -12
155 #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
158 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
159 #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))
161 static const char tdesc[] = "DAHDI Telephony Driver"
170 static const char config[] = "chan_dahdi.conf";
172 #define SIG_EM DAHDI_SIG_EM
173 #define SIG_EMWINK (0x0100000 | DAHDI_SIG_EM)
174 #define SIG_FEATD (0x0200000 | DAHDI_SIG_EM)
175 #define SIG_FEATDMF (0x0400000 | DAHDI_SIG_EM)
176 #define SIG_FEATB (0x0800000 | DAHDI_SIG_EM)
177 #define SIG_E911 (0x1000000 | DAHDI_SIG_EM)
178 #define SIG_FEATDMF_TA (0x2000000 | DAHDI_SIG_EM)
179 #define SIG_FGC_CAMA (0x4000000 | DAHDI_SIG_EM)
180 #define SIG_FGC_CAMAMF (0x8000000 | DAHDI_SIG_EM)
181 #define SIG_FXSLS DAHDI_SIG_FXSLS
182 #define SIG_FXSGS DAHDI_SIG_FXSGS
183 #define SIG_FXSKS DAHDI_SIG_FXSKS
184 #define SIG_FXOLS DAHDI_SIG_FXOLS
185 #define SIG_FXOGS DAHDI_SIG_FXOGS
186 #define SIG_FXOKS DAHDI_SIG_FXOKS
187 #define SIG_PRI DAHDI_SIG_CLEAR
188 #define SIG_BRI (0x2000000 | DAHDI_SIG_CLEAR)
189 #define SIG_BRI_PTMP (0X4000000 | DAHDI_SIG_CLEAR)
190 #define SIG_SS7 (0x1000000 | DAHDI_SIG_CLEAR)
191 #define SIG_SF DAHDI_SIG_SF
192 #define SIG_SFWINK (0x0100000 | DAHDI_SIG_SF)
193 #define SIG_SF_FEATD (0x0200000 | DAHDI_SIG_SF)
194 #define SIG_SF_FEATDMF (0x0400000 | DAHDI_SIG_SF)
195 #define SIG_SF_FEATB (0x0800000 | DAHDI_SIG_SF)
196 #define SIG_EM_E1 DAHDI_SIG_EM_E1
197 #define SIG_GR303FXOKS (0x0100000 | DAHDI_SIG_FXOKS)
198 #define SIG_GR303FXSKS (0x0100000 | DAHDI_SIG_FXSKS)
201 #define NUM_SPANS DAHDI_MAX_SPANS
205 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
206 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
208 #define CHAN_PSEUDO -2
210 #define DCHAN_PROVISIONED (1 << 0)
211 #define DCHAN_NOTINALARM (1 << 1)
212 #define DCHAN_UP (1 << 2)
214 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
216 /* Overlap dialing option types */
217 #define DAHDI_OVERLAPDIAL_NONE 0
218 #define DAHDI_OVERLAPDIAL_OUTGOING 1
219 #define DAHDI_OVERLAPDIAL_INCOMING 2
220 #define DAHDI_OVERLAPDIAL_BOTH (DAHDI_OVERLAPDIAL_INCOMING|DAHDI_OVERLAPDIAL_OUTGOING)
223 #define CALLPROGRESS_PROGRESS 1
224 #define CALLPROGRESS_FAX_OUTGOING 2
225 #define CALLPROGRESS_FAX_INCOMING 4
226 #define CALLPROGRESS_FAX (CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING)
228 static char defaultcic[64] = "";
229 static char defaultozz[64] = "";
231 static char parkinglot[AST_MAX_EXTENSION] = ""; /*!< Default parking lot for this channel */
233 /*! Run this script when the MWI state changes on an FXO line, if mwimonitor is enabled */
234 static char mwimonitornotify[PATH_MAX] = "";
235 static int mwisend_rpas = 0;
237 static char progzone[10] = "";
239 static int usedistinctiveringdetection = 0;
240 static int distinctiveringaftercid = 0;
242 static int numbufs = 4;
244 static int mwilevel = 512;
247 static struct ast_channel inuse;
248 #ifdef PRI_GETSET_TIMERS
249 static int pritimers[PRI_MAX_TIMERS];
251 static int pridebugfd = -1;
252 static char pridebugfilename[1024] = "";
255 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
256 static int firstdigittimeout = 16000;
258 /*! \brief How long to wait for following digits (FXO logic) */
259 static int gendigittimeout = 8000;
261 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
262 static int matchdigittimeout = 3000;
264 /*! \brief Protect the interface list (of dahdi_pvt's) */
265 AST_MUTEX_DEFINE_STATIC(iflock);
268 static int ifcount = 0;
271 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
274 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
275 when it's doing something critical. */
276 AST_MUTEX_DEFINE_STATIC(monlock);
278 /*! \brief This is the thread for the monitor which checks for input on the channels
279 which are not currently in use. */
280 static pthread_t monitor_thread = AST_PTHREADT_NULL;
282 static int restart_monitor(void);
284 static enum ast_bridge_result dahdi_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
286 static int dahdi_sendtext(struct ast_channel *c, const char *text);
288 static void mwi_event_cb(const struct ast_event *event, void *userdata)
290 /* This module does not handle MWI in an event-based manner. However, it
291 * subscribes to MWI for each mailbox that is configured so that the core
292 * knows that we care about it. Then, chan_dahdi will get the MWI from the
293 * event cache instead of checking the mailbox directly. */
296 /*! \brief Avoid the silly dahdi_getevent which ignores a bunch of events */
297 static inline int dahdi_get_event(int fd)
300 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
305 /*! \brief Avoid the silly dahdi_waitevent which ignores a bunch of events */
306 static inline int dahdi_wait_event(int fd)
309 i = DAHDI_IOMUX_SIGEVENT;
310 if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
312 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
317 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
318 #define READ_SIZE 160
320 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
321 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
323 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
324 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 10,000 ms */
325 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
326 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
327 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE) /*!< 8,000 ms */
331 static int ringt_base = DEFAULT_RINGT;
335 #define LINKSTATE_INALARM (1 << 0)
336 #define LINKSTATE_STARTING (1 << 1)
337 #define LINKSTATE_UP (1 << 2)
338 #define LINKSTATE_DOWN (1 << 3)
340 #define SS7_NAI_DYNAMIC -1
342 #define LINKSET_FLAG_EXPLICITACM (1 << 0)
345 pthread_t master; /*!< Thread of master */
349 int linkstate[NUM_DCHANS];
353 LINKSET_STATE_DOWN = 0,
356 char called_nai; /*!< Called Nature of Address Indicator */
357 char calling_nai; /*!< Calling Nature of Address Indicator */
358 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
359 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
360 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
361 char unknownprefix[20]; /*!< for unknown dialplans */
363 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
364 int flags; /*!< Linkset flags */
367 static struct dahdi_ss7 linksets[NUM_SPANS];
369 static int cur_ss7type = -1;
370 static int cur_linkset = -1;
371 static int cur_pointcode = -1;
372 static int cur_cicbeginswith = -1;
373 static int cur_adjpointcode = -1;
374 static int cur_networkindicator = -1;
375 static int cur_defaultdpc = -1;
376 #endif /* HAVE_SS7 */
380 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
381 #define PRI_CHANNEL(p) ((p) & 0xff)
382 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
383 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
386 pthread_t master; /*!< Thread of master */
387 ast_mutex_t lock; /*!< Mutex */
388 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
389 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
390 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
391 int minunused; /*!< Min # of channels to keep empty */
392 int minidle; /*!< Min # of "idling" calls to keep active */
393 int nodetype; /*!< Node type */
394 int switchtype; /*!< Type of switch to emulate */
395 int nsf; /*!< Network-Specific Facilities */
396 int dialplan; /*!< Dialing plan */
397 int localdialplan; /*!< Local dialing plan */
398 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
399 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
400 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
401 char privateprefix[20]; /*!< for private dialplans */
402 char unknownprefix[20]; /*!< for unknown dialplans */
403 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
404 int trunkgroup; /*!< What our trunkgroup is */
405 int mastertrunkgroup; /*!< What trunk group is our master */
406 int prilogicalspan; /*!< Logical span number within trunk group */
407 int numchans; /*!< Num of channels we represent */
408 int overlapdial; /*!< In overlap dialing mode */
409 int facilityenable; /*!< Enable facility IEs */
410 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
411 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
412 struct pri *pri; /*!< Currently active D-channel */
414 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
419 time_t lastreset; /*!< time when unused channels were last reset */
420 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
422 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
423 struct dahdi_pvt *crvs; /*!< Member CRV structs */
424 struct dahdi_pvt *crvend; /*!< Pointer to end of CRV structs */
428 static struct dahdi_pri pris[NUM_SPANS];
431 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
433 #define DEFAULT_PRI_DEBUG 0
436 static inline void pri_rel(struct dahdi_pri *pri)
438 ast_mutex_unlock(&pri->lock);
442 /*! Shut up the compiler */
446 #define SUB_REAL 0 /*!< Active call */
447 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
448 #define SUB_THREEWAY 2 /*!< Three-way call */
450 /* Polarity states */
451 #define POLARITY_IDLE 0
452 #define POLARITY_REV 1
455 struct distRingData {
459 struct ringContextData {
460 char contextData[AST_MAX_CONTEXT];
462 struct dahdi_distRings {
463 struct distRingData ringnum[3];
464 struct ringContextData ringContext[3];
467 static char *subnames[] = {
473 struct dahdi_subchannel {
475 struct ast_channel *owner;
477 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
478 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
479 unsigned int needringing:1;
480 unsigned int needbusy:1;
481 unsigned int needcongestion:1;
482 unsigned int needcallerid:1;
483 unsigned int needanswer:1;
484 unsigned int needflash:1;
485 unsigned int needhold:1;
486 unsigned int needunhold:1;
487 unsigned int linear:1;
488 unsigned int inthreeway:1;
489 DAHDI_CONFINFO curconf;
492 #define CONF_USER_REAL (1 << 0)
493 #define CONF_USER_THIRDCALL (1 << 1)
497 static struct dahdi_pvt {
499 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
500 /*!< Up to three channels can be associated with this call */
502 struct dahdi_subchannel sub_unused; /*!< Just a safety precaution */
503 struct dahdi_subchannel subs[3]; /*!< Sub-channels */
504 struct dahdi_confinfo saveconf; /*!< Saved conference info */
506 struct dahdi_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
507 struct dahdi_pvt *master; /*!< Master to us (we follow their conferencing) */
508 int inconference; /*!< If our real should be in the conference */
510 int sig; /*!< Signalling style */
511 int radio; /*!< radio type */
512 int outsigmod; /*!< Outbound Signalling style (modifier) */
513 int oprmode; /*!< "Operator Services" mode */
514 struct dahdi_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
515 float cid_rxgain; /*!< "Gain to apply during caller id */
518 int tonezone; /*!< tone zone for this chan, or -1 for default */
519 struct dahdi_pvt *next; /*!< Next channel in list */
520 struct dahdi_pvt *prev; /*!< Prev channel in list */
524 unsigned int answeronpolarityswitch:1;
525 unsigned int busydetect:1;
526 unsigned int callreturn:1;
527 unsigned int callwaiting:1;
528 unsigned int callwaitingcallerid:1;
529 unsigned int cancallforward:1;
530 unsigned int canpark:1;
531 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
532 unsigned int destroy:1;
533 unsigned int didtdd:1; /*!< flag to say its done it once */
534 unsigned int dialednone:1;
535 unsigned int dialing:1;
536 unsigned int digital:1;
538 unsigned int echobreak:1;
539 unsigned int echocanbridged:1;
540 unsigned int echocanon:1;
541 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
542 unsigned int firstradio:1;
543 unsigned int hanguponpolarityswitch:1;
544 unsigned int hardwaredtmf:1;
545 unsigned int hidecallerid:1;
546 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
547 unsigned int ignoredtmf:1;
548 unsigned int immediate:1; /*!< Answer before getting digits? */
549 unsigned int inalarm:1;
550 unsigned int mate:1; /*!< flag to say its in MATE mode */
551 unsigned int outgoing:1;
552 /* unsigned int overlapdial:1; unused and potentially confusing */
553 unsigned int permcallwaiting:1;
554 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
555 unsigned int priindication_oob:1;
556 unsigned int priexclusive:1;
557 unsigned int pulse:1;
558 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
559 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
560 unsigned int threewaycalling:1;
561 unsigned int transfer:1;
562 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
563 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
564 unsigned int usedistinctiveringdetection:1;
565 unsigned int dahditrcallerid:1; /*!< should we use the callerid from incoming call on dahdi transfer or not */
566 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
567 unsigned int mwimonitor_neon:1; /*!< monitor this FXO port for neon type MWI indication from other end */
568 unsigned int mwimonitor_fsk:1; /*!< monitor this FXO port for fsk MWI indication from other end */
569 unsigned int mwimonitoractive:1; /*!< an MWI monitor thread is currently active */
570 unsigned int mwisendactive:1; /*!< a MWI message sending thread is active */
571 /* Channel state or unavilability flags */
572 unsigned int inservice:1;
573 unsigned int locallyblocked:1;
574 unsigned int remotelyblocked:1;
575 #if defined(HAVE_PRI) || defined(HAVE_SS7)
577 unsigned int alerting:1;
578 unsigned int alreadyhungup:1;
579 unsigned int isidlecall:1;
580 unsigned int proceeding:1;
581 unsigned int progress:1;
582 unsigned int resetting:1;
583 unsigned int setup_ack:1;
585 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
586 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
588 struct dahdi_distRings drings;
590 char context[AST_MAX_CONTEXT];
591 char defcontext[AST_MAX_CONTEXT];
592 char exten[AST_MAX_EXTENSION];
593 char language[MAX_LANGUAGE];
594 char mohinterpret[MAX_MUSICCLASS];
595 char mohsuggest[MAX_MUSICCLASS];
596 char parkinglot[AST_MAX_EXTENSION]; /*!< Parking lot for this channel */
597 #if defined(PRI_ANI) || defined(HAVE_SS7)
598 char cid_ani[AST_MAX_EXTENSION];
601 char cid_num[AST_MAX_EXTENSION];
602 int cid_ton; /*!< Type Of Number (TON) */
603 char cid_name[AST_MAX_EXTENSION];
604 char lastcid_num[AST_MAX_EXTENSION];
605 char lastcid_name[AST_MAX_EXTENSION];
606 char *origcid_num; /*!< malloced original callerid */
607 char *origcid_name; /*!< malloced original callerid */
608 char callwait_num[AST_MAX_EXTENSION];
609 char callwait_name[AST_MAX_EXTENSION];
610 char rdnis[AST_MAX_EXTENSION];
611 char dnid[AST_MAX_EXTENSION];
614 int confno; /*!< Our conference */
615 int confusers; /*!< Who is using our conference */
616 int propconfno; /*!< Propagated conference number */
617 ast_group_t callgroup;
618 ast_group_t pickupgroup;
619 struct ast_variable *vars;
620 int channel; /*!< Channel Number or CRV */
621 int span; /*!< Span number */
622 time_t guardtime; /*!< Must wait this much time before using for new call */
623 int cid_signalling; /*!< CID signalling type bell202 or v23 */
624 int cid_start; /*!< CID start indicator, polarity or ring */
625 int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
626 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
627 int cidcwexpire; /*!< When to expire our muting for CID/CW */
628 unsigned char *cidspill;
637 struct dahdi_echocanparams head;
638 struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
644 int busy_quietlength;
646 struct timeval flashtime; /*!< Last flash-hook time */
648 int cref; /*!< Call reference number */
649 DAHDI_DIAL_OPERATION dop;
650 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
652 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
653 int amaflags; /*!< AMA Flags */
654 struct tdd_state *tdd; /*!< TDD flag */
655 char call_forward[AST_MAX_EXTENSION];
656 char mailbox[AST_MAX_EXTENSION];
657 struct ast_event_sub *mwi_event_sub;
661 int distinctivering; /*!< Which distinctivering to use */
662 int cidrings; /*!< Which ring to deliver CID on */
663 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
665 int polarityonanswerdelay;
666 struct timeval polaritydelaytv;
667 int sendcalleridafter;
669 struct dahdi_pri *pri;
670 struct dahdi_pvt *bearer;
671 struct dahdi_pvt *realcall;
679 struct dahdi_ss7 *ss7;
680 struct isup_call *ss7call;
681 char charge_number[50];
682 char gen_add_number[50];
683 char gen_dig_number[50];
684 char orig_called_num[50];
685 char redirecting_num[50];
686 char generic_name[50];
687 unsigned char gen_add_num_plan;
688 unsigned char gen_add_nai;
689 unsigned char gen_add_pres_ind;
690 unsigned char gen_add_type;
691 unsigned char gen_dig_type;
692 unsigned char gen_dig_scheme;
694 unsigned char lspi_type;
695 unsigned char lspi_scheme;
696 unsigned char lspi_context;
698 unsigned int call_ref_ident;
699 unsigned int call_ref_pc;
700 unsigned char calling_party_cat;
702 int cic; /*!< CIC associated with channel */
703 unsigned int dpc; /*!< CIC's DPC */
704 unsigned int loopedback:1;
708 } *iflist = NULL, *ifend = NULL;
710 /*! \brief Channel configuration from chan_dahdi.conf .
711 * This struct is used for parsing the [channels] section of chan_dahdi.conf.
712 * Generally there is a field here for every possible configuration item.
714 * The state of fields is saved along the parsing and whenever a 'channel'
715 * statement is reached, the current dahdi_chan_conf is used to configure the
716 * channel (struct dahdi_pvt)
718 * \see dahdi_chan_init for the default values.
720 struct dahdi_chan_conf {
721 struct dahdi_pvt chan;
723 struct dahdi_pri pri;
727 struct dahdi_ss7 ss7;
730 int is_sig_auto; /*!< Use channel signalling from DAHDI? */
732 char smdi_port[SMDI_MAX_FILENAME_LEN];
735 /*! returns a new dahdi_chan_conf with default values (by-value) */
736 static struct dahdi_chan_conf dahdi_chan_conf_default(void) {
737 /* recall that if a field is not included here it is initialized
740 struct dahdi_chan_conf conf = {
744 .switchtype = PRI_SWITCH_NI2,
745 .dialplan = PRI_UNKNOWN + 1,
746 .localdialplan = PRI_NATIONAL_ISDN + 1,
752 .internationalprefix = "",
753 .nationalprefix = "",
762 .called_nai = SS7_NAI_NATIONAL,
763 .calling_nai = SS7_NAI_NATIONAL,
764 .internationalprefix = "",
765 .nationalprefix = "",
766 .subscriberprefix = "",
771 .context = "default",
774 .mohinterpret = "default",
779 .cid_signalling = CID_SIG_BELL,
780 .cid_start = CID_START_RING,
781 .dahditrcallerid = 0,
790 .echocancel.head.tap_length = 1,
799 .polarityonanswerdelay = 600,
801 .sendcalleridafter = DEFAULT_CIDRINGS
814 .smdi_port = "/dev/ttyS0",
821 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause);
822 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
823 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
824 static int dahdi_sendtext(struct ast_channel *c, const char *text);
825 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
826 static int dahdi_hangup(struct ast_channel *ast);
827 static int dahdi_answer(struct ast_channel *ast);
828 static struct ast_frame *dahdi_read(struct ast_channel *ast);
829 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
830 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
831 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
832 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
833 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
834 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
835 static int handle_init_event(struct dahdi_pvt *i, int event);
837 static const struct ast_channel_tech dahdi_tech = {
839 .description = tdesc,
840 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
841 .requester = dahdi_request,
842 .send_digit_begin = dahdi_digit_begin,
843 .send_digit_end = dahdi_digit_end,
844 .send_text = dahdi_sendtext,
846 .hangup = dahdi_hangup,
847 .answer = dahdi_answer,
849 .write = dahdi_write,
850 .bridge = dahdi_bridge,
851 .exception = dahdi_exception,
852 .indicate = dahdi_indicate,
853 .fixup = dahdi_fixup,
854 .setoption = dahdi_setoption,
855 .func_channel_read = dahdi_func_read,
859 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
861 #define GET_CHANNEL(p) ((p)->channel)
864 struct dahdi_pvt *round_robin[32];
867 static inline int pri_grab(struct dahdi_pvt *pvt, struct dahdi_pri *pri)
870 /* Grab the lock first */
872 res = ast_mutex_trylock(&pri->lock);
874 DEADLOCK_AVOIDANCE(&pvt->lock);
877 /* Then break the poll */
878 pthread_kill(pri->master, SIGURG);
884 static inline void ss7_rel(struct dahdi_ss7 *ss7)
886 ast_mutex_unlock(&ss7->lock);
889 static inline int ss7_grab(struct dahdi_pvt *pvt, struct dahdi_ss7 *pri)
892 /* Grab the lock first */
894 res = ast_mutex_trylock(&pri->lock);
896 DEADLOCK_AVOIDANCE(&pvt->lock);
899 /* Then break the poll */
900 pthread_kill(pri->master, SIGURG);
904 #define NUM_CADENCE_MAX 25
905 static int num_cadence = 4;
906 static int user_has_defined_cadences = 0;
908 static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
909 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
910 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
911 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
912 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
915 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
916 * is 1, the second pause is 2 and so on.
919 static int cidrings[NUM_CADENCE_MAX] = {
920 2, /*!< Right after first long ring */
921 4, /*!< Right after long part */
922 3, /*!< After third chirp */
923 2, /*!< Second spell */
926 /* ETSI EN300 659-1 specifies the ring pulse between 200 and 300 mS */
927 static struct dahdi_ring_cadence AS_RP_cadence = {{250, 10000}};
929 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
930 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
932 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
933 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
935 static int dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok)
938 if (p->subs[0].owner == ast)
940 else if (p->subs[1].owner == ast)
942 else if (p->subs[2].owner == ast)
947 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
953 static void wakeup_sub(struct dahdi_pvt *p, int a, struct dahdi_pri *pri)
955 static void wakeup_sub(struct dahdi_pvt *p, int a, void *pri)
960 ast_mutex_unlock(&pri->lock);
963 if (p->subs[a].owner) {
964 if (ast_channel_trylock(p->subs[a].owner)) {
965 DEADLOCK_AVOIDANCE(&p->lock);
967 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
968 ast_channel_unlock(p->subs[a].owner);
976 ast_mutex_lock(&pri->lock);
980 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *data)
983 struct dahdi_pri *pri = (struct dahdi_pri*) data;
986 struct dahdi_ss7 *ss7 = (struct dahdi_ss7*) data;
988 /* We must unlock the PRI to avoid the possibility of a deadlock */
989 #if defined(HAVE_PRI) || defined(HAVE_SS7)
996 ast_mutex_unlock(&pri->lock);
1001 ast_mutex_unlock(&ss7->lock);
1011 if (ast_channel_trylock(p->owner)) {
1012 DEADLOCK_AVOIDANCE(&p->lock);
1014 ast_queue_frame(p->owner, f);
1015 ast_channel_unlock(p->owner);
1021 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1028 ast_mutex_lock(&pri->lock);
1033 ast_mutex_lock(&ss7->lock);
1044 static int restore_gains(struct dahdi_pvt *p);
1046 static void swap_subs(struct dahdi_pvt *p, int a, int b)
1050 struct ast_channel *towner;
1052 ast_debug(1, "Swapping %d and %d\n", a, b);
1054 tchan = p->subs[a].chan;
1055 towner = p->subs[a].owner;
1056 tinthreeway = p->subs[a].inthreeway;
1058 p->subs[a].chan = p->subs[b].chan;
1059 p->subs[a].owner = p->subs[b].owner;
1060 p->subs[a].inthreeway = p->subs[b].inthreeway;
1062 p->subs[b].chan = tchan;
1063 p->subs[b].owner = towner;
1064 p->subs[b].inthreeway = tinthreeway;
1066 if (p->subs[a].owner)
1067 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].zfd);
1068 if (p->subs[b].owner)
1069 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].zfd);
1070 wakeup_sub(p, a, NULL);
1071 wakeup_sub(p, b, NULL);
1074 static int dahdi_open(char *fn)
1082 for (x = 0; x < strlen(fn); x++) {
1083 if (!isdigit(fn[x])) {
1091 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1094 fn = "/dev/dahdi/channel";
1096 fd = open(fn, O_RDWR | O_NONBLOCK);
1098 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1102 if (ioctl(fd, DAHDI_SPECIFY, &chan)) {
1106 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1111 if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs) == -1) {
1112 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
1121 static void dahdi_close(int fd)
1127 static int dahdi_setlinear(int zfd, int linear)
1130 res = ioctl(zfd, DAHDI_SETLINEAR, &linear);
1137 static int alloc_sub(struct dahdi_pvt *p, int x)
1139 DAHDI_BUFFERINFO bi;
1141 if (p->subs[x].zfd >= 0) {
1142 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1146 p->subs[x].zfd = dahdi_open("/dev/dahdi/pseudo");
1147 if (p->subs[x].zfd <= -1) {
1148 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1152 res = ioctl(p->subs[x].zfd, DAHDI_GET_BUFINFO, &bi);
1154 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
1155 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
1156 bi.numbufs = numbufs;
1157 res = ioctl(p->subs[x].zfd, DAHDI_SET_BUFINFO, &bi);
1159 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", x, strerror(errno));
1162 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
1164 if (ioctl(p->subs[x].zfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
1165 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].zfd, strerror(errno));
1166 dahdi_close(p->subs[x].zfd);
1167 p->subs[x].zfd = -1;
1170 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
1174 static int unalloc_sub(struct dahdi_pvt *p, int x)
1177 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1180 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1181 if (p->subs[x].zfd > -1) {
1182 dahdi_close(p->subs[x].zfd);
1184 p->subs[x].zfd = -1;
1185 p->subs[x].linear = 0;
1186 p->subs[x].chan = 0;
1187 p->subs[x].owner = NULL;
1188 p->subs[x].inthreeway = 0;
1189 p->polarity = POLARITY_IDLE;
1190 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1194 static int digit_to_dtmfindex(char digit)
1197 return DAHDI_TONE_DTMF_BASE + (digit - '0');
1198 else if (digit >= 'A' && digit <= 'D')
1199 return DAHDI_TONE_DTMF_A + (digit - 'A');
1200 else if (digit >= 'a' && digit <= 'd')
1201 return DAHDI_TONE_DTMF_A + (digit - 'a');
1202 else if (digit == '*')
1203 return DAHDI_TONE_DTMF_s;
1204 else if (digit == '#')
1205 return DAHDI_TONE_DTMF_p;
1210 static int dahdi_digit_begin(struct ast_channel *chan, char digit)
1212 struct dahdi_pvt *pvt;
1216 pvt = chan->tech_pvt;
1218 ast_mutex_lock(&pvt->lock);
1220 index = dahdi_get_index(chan, pvt, 0);
1222 if ((index != SUB_REAL) || !pvt->owner)
1226 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1227 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1228 if (pvt->setup_ack) {
1229 if (!pri_grab(pvt, pvt->pri)) {
1230 pri_information(pvt->pri->pri, pvt->call, digit);
1233 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1234 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1236 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1237 res = strlen(pvt->dialdest);
1238 pvt->dialdest[res++] = digit;
1239 pvt->dialdest[res] = '\0';
1244 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1247 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, DAHDI_SENDTONE, &dtmf)) {
1249 DAHDI_DIAL_OPERATION zo = {
1250 .op = DAHDI_DIAL_OP_APPEND,
1253 zo.dialstr[0] = 'T';
1254 zo.dialstr[1] = digit;
1255 zo.dialstr[2] = '\0';
1256 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, DAHDI_DIAL, &zo)))
1257 ast_log(LOG_WARNING, "Couldn't dial digit %c: %s\n", digit, strerror(errno));
1261 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1263 pvt->begindigit = digit;
1267 ast_mutex_unlock(&pvt->lock);
1272 static int dahdi_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1274 struct dahdi_pvt *pvt;
1279 pvt = chan->tech_pvt;
1281 ast_mutex_lock(&pvt->lock);
1283 index = dahdi_get_index(chan, pvt, 0);
1285 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
1289 /* This means that the digit was already sent via PRI signalling */
1290 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1291 && !pvt->begindigit)
1295 if (pvt->begindigit) {
1297 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1298 res = ioctl(pvt->subs[SUB_REAL].zfd, DAHDI_SENDTONE, &x);
1300 pvt->begindigit = 0;
1304 ast_mutex_unlock(&pvt->lock);
1309 static char *events[] = {
1322 "Hook Transition Complete",
1327 "Polarity Reversal",
1335 { DAHDI_ALARM_RED, "Red Alarm" },
1336 { DAHDI_ALARM_YELLOW, "Yellow Alarm" },
1337 { DAHDI_ALARM_BLUE, "Blue Alarm" },
1338 { DAHDI_ALARM_RECOVER, "Recovering" },
1339 { DAHDI_ALARM_LOOPBACK, "Loopback" },
1340 { DAHDI_ALARM_NOTOPEN, "Not Open" },
1341 { DAHDI_ALARM_NONE, "None" },
1344 static char *alarm2str(int alarm)
1347 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
1348 if (alarms[x].alarm & alarm)
1349 return alarms[x].name;
1351 return alarm ? "Unknown Alarm" : "No Alarm";
1354 static char *event2str(int event)
1356 static char buf[256];
1357 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
1358 return events[event];
1359 sprintf(buf, "Event %d", event); /* safe */
1364 static char *dialplan2str(int dialplan)
1366 if (dialplan == -1 || dialplan == -2) {
1367 return("Dynamically set dialplan in ISDN");
1369 return (pri_plan2str(dialplan));
1373 static char *dahdi_sig2str(int sig)
1375 static char buf[256];
1378 return "E & M Immediate";
1380 return "E & M Wink";
1384 return "Feature Group D (DTMF)";
1386 return "Feature Group D (MF)";
1387 case SIG_FEATDMF_TA:
1388 return "Feature Groud D (MF) Tandem Access";
1390 return "Feature Group B (MF)";
1394 return "FGC/CAMA (Dialpulse)";
1395 case SIG_FGC_CAMAMF:
1396 return "FGC/CAMA (MF)";
1398 return "FXS Loopstart";
1400 return "FXS Groundstart";
1402 return "FXS Kewlstart";
1404 return "FXO Loopstart";
1406 return "FXO Groundstart";
1408 return "FXO Kewlstart";
1412 return "ISDN BRI Point to Point";
1414 return "ISDN BRI Point to MultiPoint";
1418 return "SF (Tone) Immediate";
1420 return "SF (Tone) Wink";
1422 return "SF (Tone) with Feature Group D (DTMF)";
1423 case SIG_SF_FEATDMF:
1424 return "SF (Tone) with Feature Group D (MF)";
1426 return "SF (Tone) with Feature Group B (MF)";
1427 case SIG_GR303FXOKS:
1428 return "GR-303 with FXOKS";
1429 case SIG_GR303FXSKS:
1430 return "GR-303 with FXSKS";
1434 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1439 #define sig2str dahdi_sig2str
1441 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index, int slavechannel)
1443 /* If the conference already exists, and we're already in it
1444 don't bother doing anything */
1447 memset(&zi, 0, sizeof(zi));
1450 if (slavechannel > 0) {
1451 /* If we have only one slave, do a digital mon */
1452 zi.confmode = DAHDI_CONF_DIGITALMON;
1453 zi.confno = slavechannel;
1456 /* Real-side and pseudo-side both participate in conference */
1457 zi.confmode = DAHDI_CONF_REALANDPSEUDO | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER |
1458 DAHDI_CONF_PSEUDO_TALKER | DAHDI_CONF_PSEUDO_LISTENER;
1460 zi.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
1461 zi.confno = p->confno;
1463 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1467 if (ioctl(c->zfd, DAHDI_SETCONF, &zi)) {
1468 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d: %s\n", c->zfd, zi.confmode, zi.confno, strerror(errno));
1471 if (slavechannel < 1) {
1472 p->confno = zi.confno;
1474 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1475 ast_debug(1, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1479 static int isourconf(struct dahdi_pvt *p, struct dahdi_subchannel *c)
1481 /* If they're listening to our channel, they're ours */
1482 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == DAHDI_CONF_DIGITALMON))
1484 /* If they're a talker on our (allocated) conference, they're ours */
1485 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & DAHDI_CONF_TALKER))
1490 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index)
1493 if (/* Can't delete if there's no zfd */
1495 /* Don't delete from the conference if it's not our conference */
1497 /* Don't delete if we don't think it's conferenced at all (implied) */
1499 memset(&zi, 0, sizeof(zi));
1503 if (ioctl(c->zfd, DAHDI_SETCONF, &zi)) {
1504 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d: %s\n", c->zfd, c->curconf.confmode, c->curconf.confno, strerror(errno));
1507 ast_debug(1, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
1508 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1512 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out)
1516 struct dahdi_pvt *slave = NULL;
1517 /* Start out optimistic */
1519 /* Update conference state in a stateless fashion */
1520 for (x = 0; x < 3; x++) {
1521 /* Any three-way calling makes slave native mode *definitely* out
1523 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
1526 /* If we don't have any 3-way calls, check to see if we have
1527 precisely one slave */
1528 if (useslavenative) {
1529 for (x = 0; x < MAX_SLAVES; x++) {
1532 /* Whoops already have a slave! No
1533 slave native and stop right away */
1538 /* We have one slave so far */
1539 slave = p->slaves[x];
1544 /* If no slave, slave native definitely out */
1547 else if (slave->law != p->law) {
1553 return useslavenative;
1556 static int reset_conf(struct dahdi_pvt *p)
1559 memset(&zi, 0, sizeof(zi));
1561 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1562 if (p->subs[SUB_REAL].zfd > -1) {
1563 if (ioctl(p->subs[SUB_REAL].zfd, DAHDI_SETCONF, &zi))
1564 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d: %s\n", p->channel, strerror(errno));
1569 static int update_conf(struct dahdi_pvt *p)
1574 struct dahdi_pvt *slave = NULL;
1576 useslavenative = isslavenative(p, &slave);
1577 /* Start with the obvious, general stuff */
1578 for (x = 0; x < 3; x++) {
1579 /* Look for three way calls */
1580 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
1581 conf_add(p, &p->subs[x], x, 0);
1584 conf_del(p, &p->subs[x], x);
1587 /* If we have a slave, add him to our conference now. or DAX
1588 if this is slave native */
1589 for (x = 0; x < MAX_SLAVES; x++) {
1592 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1594 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1599 /* If we're supposed to be in there, do so now */
1600 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1602 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1604 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1608 /* If we have a master, add ourselves to his conference */
1610 if (isslavenative(p->master, NULL)) {
1611 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1613 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1617 /* Nobody is left (or should be left) in our conference.
1621 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1625 static void dahdi_enable_ec(struct dahdi_pvt *p)
1632 ast_debug(1, "Echo cancellation already on\n");
1636 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1639 if (p->echocancel.head.tap_length) {
1640 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1642 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_AUDIOMODE, &x);
1644 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n", p->channel, strerror(errno));
1646 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_ECHOCANCEL_PARAMS, &p->echocancel);
1648 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d (%s)\n", p->channel, strerror(errno));
1651 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1654 ast_debug(1, "No echo cancellation requested\n");
1657 static void dahdi_train_ec(struct dahdi_pvt *p)
1662 if (p && p->echocanon && p->echotraining) {
1663 x = p->echotraining;
1664 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_ECHOTRAIN, &x);
1666 ast_log(LOG_WARNING, "Unable to request echo training on channel %d: %s\n", p->channel, strerror(errno));
1668 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1670 ast_debug(1, "No echo training requested\n");
1674 static void dahdi_disable_ec(struct dahdi_pvt *p)
1679 struct dahdi_echocanparams ecp = { .tap_length = 0 };
1681 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_ECHOCANCEL_PARAMS, &ecp);
1684 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d: %s\n", p->channel, strerror(errno));
1686 ast_debug(1, "Disabled echo cancellation on channel %d\n", p->channel);
1692 static void fill_txgain(struct dahdi_gains *g, float gain, int law)
1696 float linear_gain = pow(10.0, gain / 20.0);
1699 case DAHDI_LAW_ALAW:
1700 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1702 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1703 if (k > 32767) k = 32767;
1704 if (k < -32767) k = -32767;
1705 g->txgain[j] = AST_LIN2A(k);
1711 case DAHDI_LAW_MULAW:
1712 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
1714 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1715 if (k > 32767) k = 32767;
1716 if (k < -32767) k = -32767;
1717 g->txgain[j] = AST_LIN2MU(k);
1726 static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
1730 float linear_gain = pow(10.0, gain / 20.0);
1733 case DAHDI_LAW_ALAW:
1734 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1736 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1737 if (k > 32767) k = 32767;
1738 if (k < -32767) k = -32767;
1739 g->rxgain[j] = AST_LIN2A(k);
1745 case DAHDI_LAW_MULAW:
1746 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
1748 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1749 if (k > 32767) k = 32767;
1750 if (k < -32767) k = -32767;
1751 g->rxgain[j] = AST_LIN2MU(k);
1760 static int set_actual_txgain(int fd, int chan, float gain, int law)
1762 struct dahdi_gains g;
1765 memset(&g, 0, sizeof(g));
1767 res = ioctl(fd, DAHDI_GETGAINS, &g);
1769 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1773 fill_txgain(&g, gain, law);
1775 return ioctl(fd, DAHDI_SETGAINS, &g);
1778 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1780 struct dahdi_gains g;
1783 memset(&g, 0, sizeof(g));
1785 res = ioctl(fd, DAHDI_GETGAINS, &g);
1787 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1791 fill_rxgain(&g, gain, law);
1793 return ioctl(fd, DAHDI_SETGAINS, &g);
1796 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1798 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1801 static int bump_gains(struct dahdi_pvt *p)
1805 /* Bump receive gain by value stored in cid_rxgain */
1806 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1808 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1815 static int restore_gains(struct dahdi_pvt *p)
1819 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
1821 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1828 static inline int dahdi_set_hook(int fd, int hs)
1833 res = ioctl(fd, DAHDI_HOOK, &x);
1836 if (errno == EINPROGRESS)
1838 ast_log(LOG_WARNING, "DAHDI hook failed: %s\n", strerror(errno));
1844 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted)
1848 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
1850 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_AUDIOMODE, &y);
1852 ast_log(LOG_WARNING, "Unable to set audio mode on %d: %s\n", p->channel, strerror(errno));
1854 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_CONFMUTE, &x);
1856 ast_log(LOG_WARNING, "DAHDI confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1860 static int save_conference(struct dahdi_pvt *p)
1862 struct dahdi_confinfo c;
1864 if (p->saveconf.confmode) {
1865 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1868 p->saveconf.chan = 0;
1869 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_GETCONF, &p->saveconf);
1871 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1872 p->saveconf.confmode = 0;
1877 c.confmode = DAHDI_CONF_NORMAL;
1878 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_SETCONF, &c);
1880 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1883 ast_debug(1, "Disabled conferencing\n");
1888 * \brief Send MWI state change
1890 * \arg mailbox_full This is the mailbox associated with the FXO line that the
1891 * MWI state has changed on.
1892 * \arg thereornot This argument should simply be set to 1 or 0, to indicate
1893 * whether there are messages waiting or not.
1897 * This function does two things:
1899 * 1) It generates an internal Asterisk event notifying any other module that
1900 * cares about MWI that the state of a mailbox has changed.
1902 * 2) It runs the script specified by the mwimonitornotify option to allow
1903 * some custom handling of the state change.
1905 static void notify_message(char *mailbox_full, int thereornot)
1907 char s[sizeof(mwimonitornotify) + 80];
1908 struct ast_event *event;
1909 char *mailbox, *context;
1911 /* Strip off @default */
1912 context = mailbox = ast_strdupa(mailbox_full);
1913 strsep(&context, "@");
1914 if (ast_strlen_zero(context))
1915 context = "default";
1917 if (!(event = ast_event_new(AST_EVENT_MWI,
1918 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1919 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1920 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1921 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
1922 AST_EVENT_IE_END))) {
1926 ast_event_queue_and_cache(event,
1927 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
1928 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR,
1931 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
1932 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
1937 static int restore_conference(struct dahdi_pvt *p)
1940 if (p->saveconf.confmode) {
1941 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_SETCONF, &p->saveconf);
1942 p->saveconf.confmode = 0;
1944 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
1948 ast_debug(1, "Restored conferencing\n");
1952 static int send_callerid(struct dahdi_pvt *p);
1954 static int send_cwcidspill(struct dahdi_pvt *p)
1958 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
1960 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
1961 /* Make sure we account for the end */
1962 p->cidlen += READ_SIZE * 4;
1965 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
1969 static int has_voicemail(struct dahdi_pvt *p)
1972 struct ast_event *event;
1973 char *mailbox, *context;
1975 mailbox = context = ast_strdupa(p->mailbox);
1976 strsep(&context, "@");
1977 if (ast_strlen_zero(context))
1978 context = "default";
1980 event = ast_event_get_cached(AST_EVENT_MWI,
1981 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
1982 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
1983 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
1987 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
1988 ast_event_destroy(event);
1990 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
1995 static int send_callerid(struct dahdi_pvt *p)
1997 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
1999 /* Take out of linear mode if necessary */
2000 if (p->subs[SUB_REAL].linear) {
2001 p->subs[SUB_REAL].linear = 0;
2002 dahdi_setlinear(p->subs[SUB_REAL].zfd, 0);
2004 while (p->cidpos < p->cidlen) {
2005 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
2007 if (errno == EAGAIN)
2010 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
2018 ast_free(p->cidspill);
2020 if (p->callwaitcas) {
2021 /* Wait for CID/CW to expire */
2022 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
2024 restore_conference(p);
2028 static int dahdi_callwait(struct ast_channel *ast)
2030 struct dahdi_pvt *p = ast->tech_pvt;
2031 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
2033 ast_log(LOG_WARNING, "Spill already exists?!?\n");
2034 ast_free(p->cidspill);
2036 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
2040 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
2041 if (!p->callwaitrings && p->callwaitingcallerid) {
2042 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
2044 p->cidlen = 2400 + 680 + READ_SIZE * 4;
2046 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
2048 p->cidlen = 2400 + READ_SIZE * 4;
2057 static unsigned char cid_pres2ss7pres(int cid_pres)
2059 return (cid_pres >> 5) & 0x03;
2062 static unsigned char cid_pres2ss7screen(int cid_pres)
2064 return cid_pres & 0x03;
2068 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout)
2070 struct dahdi_pvt *p = ast->tech_pvt;
2071 int x, res, index,mysig;
2076 char dest[256]; /* must be same length as p->dialdest */
2077 ast_mutex_lock(&p->lock);
2078 ast_copy_string(dest, rdest, sizeof(dest));
2079 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
2080 if ((ast->_state == AST_STATE_BUSY)) {
2081 p->subs[SUB_REAL].needbusy = 1;
2082 ast_mutex_unlock(&p->lock);
2085 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
2086 ast_log(LOG_WARNING, "dahdi_call called on %s, neither down nor reserved\n", ast->name);
2087 ast_mutex_unlock(&p->lock);
2091 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
2093 /* Special pseudo -- automatically up */
2094 ast_setstate(ast, AST_STATE_UP);
2095 ast_mutex_unlock(&p->lock);
2098 x = DAHDI_FLUSH_READ | DAHDI_FLUSH_WRITE;
2099 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_FLUSH, &x);
2101 ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", p->channel, strerror(errno));
2104 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
2107 if (p->outsigmod > -1)
2108 mysig = p->outsigmod;
2114 if (p->owner == ast) {
2115 /* Normal ring, on hook */
2117 /* Don't send audio while on hook, until the call is answered */
2119 if (p->use_callerid) {
2120 /* Generate the Caller-ID spill if desired */
2122 ast_log(LOG_WARNING, "cidspill already exists??\n");
2123 ast_free(p->cidspill);
2126 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2127 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2132 /* Choose proper cadence */
2133 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2134 if (ioctl(p->subs[SUB_REAL].zfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
2135 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
2136 p->cidrings = cidrings[p->distinctivering - 1];
2138 if (ioctl(p->subs[SUB_REAL].zfd, DAHDI_SETCADENCE, NULL))
2139 ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
2140 p->cidrings = p->sendcalleridafter;
2143 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2144 c = strchr(dest, '/');
2147 if (c && (strlen(c) < p->stripmsd)) {
2148 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2152 p->dop.op = DAHDI_DIAL_OP_REPLACE;
2153 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2154 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2156 p->dop.dialstr[0] = '\0';
2159 if (ioctl(p->subs[SUB_REAL].zfd, DAHDI_HOOK, &x) && (errno != EINPROGRESS)) {
2160 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2161 ast_mutex_unlock(&p->lock);
2166 /* Call waiting call */
2167 p->callwaitrings = 0;
2168 if (ast->cid.cid_num)
2169 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2171 p->callwait_num[0] = '\0';
2172 if (ast->cid.cid_name)
2173 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2175 p->callwait_name[0] = '\0';
2176 /* Call waiting tone instead */
2177 if (dahdi_callwait(ast)) {
2178 ast_mutex_unlock(&p->lock);
2181 /* Make ring-back */
2182 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, DAHDI_TONE_RINGTONE))
2183 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2186 n = ast->cid.cid_name;
2187 l = ast->cid.cid_num;
2189 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2191 p->lastcid_num[0] = '\0';
2193 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2195 p->lastcid_name[0] = '\0';
2196 ast_setstate(ast, AST_STATE_RINGING);
2197 index = dahdi_get_index(ast, p, 0);
2199 p->subs[index].needringing = 1;
2212 case SIG_FGC_CAMAMF:
2217 case SIG_SF_FEATDMF:
2218 case SIG_FEATDMF_TA:
2220 c = strchr(dest, '/');
2225 if (strlen(c) < p->stripmsd) {
2226 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2227 ast_mutex_unlock(&p->lock);
2231 /* Start the trunk, if not GR-303 */
2235 res = ioctl(p->subs[SUB_REAL].zfd, DAHDI_HOOK, &x);
2237 if (errno != EINPROGRESS) {
2238 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2239 ast_mutex_unlock(&p->lock);
2246 ast_debug(1, "Dialing '%s'\n", c);
2247 p->dop.op = DAHDI_DIAL_OP_REPLACE;
2253 l = ast->cid.cid_num;
2255 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2257 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2260 l = ast->cid.cid_num;
2262 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2264 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2266 case SIG_FEATDMF_TA:
2268 const char *cic, *ozz;
2270 /* If you have to go through a Tandem Access point you need to use this */
2271 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2274 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2278 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2279 ast_mutex_unlock(&p->lock);
2282 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2283 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2288 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2291 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2293 case SIG_FGC_CAMAMF:
2295 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2299 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2301 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2305 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2306 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2307 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2308 p->echorest[sizeof(p->echorest) - 1] = '\0';
2310 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2314 if (ioctl(p->subs[SUB_REAL].zfd, DAHDI_DIAL, &p->dop)) {
2315 int saveerr = errno;
2318 ioctl(p->subs[SUB_REAL].zfd, DAHDI_HOOK, &x);
2319 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
2320 ast_mutex_unlock(&p->lock);
2324 ast_debug(1, "Deferring dialing...\n");
2327 if (ast_strlen_zero(c))
2329 ast_setstate(ast, AST_STATE_DIALING);
2332 /* Special pseudo -- automatically up*/
2333 ast_setstate(ast, AST_STATE_UP);
2339 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2340 p->dialdest[0] = '\0';
2343 ast_debug(1, "not yet implemented\n");
2344 ast_mutex_unlock(&p->lock);
2349 char ss7_called_nai;
2350 int called_nai_strip;
2351 char ss7_calling_nai;
2352 int calling_nai_strip;
2353 const char *charge_str = NULL;
2354 const char *gen_address = NULL;
2355 const char *gen_digits = NULL;
2356 const char *gen_dig_type = NULL;
2357 const char *gen_dig_scheme = NULL;
2358 const char *gen_name = NULL;
2359 const char *jip_digits = NULL;
2360 const char *lspi_ident = NULL;
2361 const char *rlt_flag = NULL;
2362 const char *call_ref_id = NULL;
2363 const char *call_ref_pc = NULL;
2364 const char *send_far = NULL;
2366 c = strchr(dest, '/');
2372 if (!p->hidecallerid) {
2373 l = ast->cid.cid_num;
2378 if (ss7_grab(p, p->ss7)) {
2379 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2380 ast_mutex_unlock(&p->lock);
2383 p->digital = IS_DIGITAL(ast->transfercapability);
2384 p->ss7call = isup_new_call(p->ss7->ss7);
2388 ast_mutex_unlock(&p->lock);
2389 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2393 called_nai_strip = 0;
2394 ss7_called_nai = p->ss7->called_nai;
2395 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2396 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2397 called_nai_strip = strlen(p->ss7->internationalprefix);
2398 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2399 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2400 called_nai_strip = strlen(p->ss7->nationalprefix);
2401 ss7_called_nai = SS7_NAI_NATIONAL;
2403 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2406 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2408 calling_nai_strip = 0;
2409 ss7_calling_nai = p->ss7->calling_nai;
2410 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2411 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2412 calling_nai_strip = strlen(p->ss7->internationalprefix);
2413 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2414 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2415 calling_nai_strip = strlen(p->ss7->nationalprefix);
2416 ss7_calling_nai = SS7_NAI_NATIONAL;
2418 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2421 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2422 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2423 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2425 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
2426 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2428 ast_channel_lock(ast);
2429 /* Set the charge number if it is set */
2430 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
2432 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
2434 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
2436 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 */
2438 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
2439 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
2440 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
2442 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
2444 gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
2446 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
2448 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
2450 isup_set_jip_digits(p->ss7call, jip_digits);
2452 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
2454 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
2456 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
2457 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
2458 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
2461 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
2462 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
2463 if (call_ref_id && call_ref_pc) {
2464 isup_set_callref(p->ss7call, atoi(call_ref_id),
2465 call_ref_pc ? atoi(call_ref_pc) : 0);
2468 send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
2469 if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
2470 (isup_far(p->ss7->ss7, p->ss7call));
2472 ast_channel_unlock(ast);
2474 isup_iam(p->ss7->ss7, p->ss7call);
2475 ast_setstate(ast, AST_STATE_DIALING);
2478 #endif /* HAVE_SS7 */
2482 #ifdef SUPPORT_USERUSER
2483 const char *useruser;
2487 int prilocaldialplan;
2491 int redirect_reason;
2493 c = strchr(dest, '/');
2502 if (!p->hidecallerid) {
2503 l = ast->cid.cid_num;
2504 if (!p->hidecalleridname) {
2505 n = ast->cid.cid_name;
2509 if (strlen(c) < p->stripmsd) {
2510 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2511 ast_mutex_unlock(&p->lock);
2514 if (mysig != SIG_FXSKS) {
2515 p->dop.op = DAHDI_DIAL_OP_REPLACE;
2516 s = strchr(c + p->stripmsd, 'w');
2519 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2521 p->dop.dialstr[0] = '\0';
2524 p->dop.dialstr[0] = '\0';
2527 if (pri_grab(p, p->pri)) {
2528 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2529 ast_mutex_unlock(&p->lock);
2532 if (!(p->call = pri_new_call(p->pri->pri))) {
2533 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2535 ast_mutex_unlock(&p->lock);
2538 if (!(sr = pri_sr_new())) {
2539 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2541 ast_mutex_unlock(&p->lock);
2543 if (p->bearer || (mysig == SIG_FXSKS)) {
2545 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2546 p->bearer->call = p->call;
2548 ast_debug(1, "I'm being setup with no bearer right now...\n");
2550 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2552 p->digital = IS_DIGITAL(ast->transfercapability);
2553 /* Add support for exclusive override */
2554 if (p->priexclusive)
2557 /* otherwise, traditional behavior */
2558 if (p->pri->nodetype == PRI_NETWORK)
2564 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2565 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2567 ((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2568 if (p->pri->facilityenable)
2569 pri_facility_enable(p->pri->pri);
2571 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2573 pridialplan = p->pri->dialplan - 1;
2574 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2575 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2576 if (pridialplan == -2) {
2577 dp_strip = strlen(p->pri->internationalprefix);
2579 pridialplan = PRI_INTERNATIONAL_ISDN;
2580 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2581 if (pridialplan == -2) {
2582 dp_strip = strlen(p->pri->nationalprefix);
2584 pridialplan = PRI_NATIONAL_ISDN;
2586 pridialplan = PRI_LOCAL_ISDN;
2589 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2590 switch (c[p->stripmsd]) {
2592 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2595 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2598 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2601 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2604 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2607 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2610 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2613 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2616 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2619 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2622 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2625 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2628 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2631 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2635 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2639 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2642 prilocaldialplan = p->pri->localdialplan - 1;
2643 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2644 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2645 if (prilocaldialplan == -2) {
2646 ldp_strip = strlen(p->pri->internationalprefix);
2648 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2649 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2650 if (prilocaldialplan == -2) {
2651 ldp_strip = strlen(p->pri->nationalprefix);
2653 prilocaldialplan = PRI_NATIONAL_ISDN;
2655 prilocaldialplan = PRI_LOCAL_ISDN;
2659 while (*l > '9' && *l != '*' && *l != '#') {
2662 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2665 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2668 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2671 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2674 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2677 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2680 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2683 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2686 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2689 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2692 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2695 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2698 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2701 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2705 ast_log(LOG_WARNING, "Unrecognized prilocaldialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2710 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2711 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2712 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2713 if (!strcasecmp(rr_str, "UNKNOWN"))
2714 redirect_reason = 0;
2715 else if (!strcasecmp(rr_str, "BUSY"))
2716 redirect_reason = 1;
2717 else if (!strcasecmp(rr_str, "NO_REPLY"))
2718 redirect_reason = 2;
2719 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2720 redirect_reason = 15;
2722 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2724 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2725 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2727 #ifdef SUPPORT_USERUSER
2728 /* User-user info */
2729 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2732 pri_sr_set_useruser(sr, useruser);
2735 if (pri_setup(p->pri->pri, p->call, sr)) {
2736 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2737 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2739 ast_mutex_unlock(&p->lock);
2744 ast_setstate(ast, AST_STATE_DIALING);
2748 ast_mutex_unlock(&p->lock);
2752 static void destroy_dahdi_pvt(struct dahdi_pvt **pvt)
2754 struct dahdi_pvt *p = *pvt;
2755 /* Remove channel from the list */
2757 p->prev->next = p->next;
2759 p->next->prev = p->prev;
2761 ast_smdi_interface_unref(p->smdi_iface);
2762 if (p->mwi_event_sub)
2763 ast_event_unsubscribe(p->mwi_event_sub);
2765 ast_variables_destroy(p->vars);
2766 ast_mutex_destroy(&p->lock);
2771 static int destroy_channel(struct dahdi_pvt *prev, struct dahdi_pvt *cur, int now)
2781 for (i = 0; i < 3; i++) {
2782 if (cur->subs[i].owner) {
2788 prev->next = cur->next;
2790 prev->next->prev = prev;
2796 iflist->prev = NULL;
2800 if (cur->subs[SUB_REAL].zfd > -1) {
2801 dahdi_close(cur->subs[SUB_REAL].zfd);
2803 destroy_dahdi_pvt(&cur);
2807 prev->next = cur->next;
2809 prev->next->prev = prev;
2815 iflist->prev = NULL;
2819 if (cur->subs[SUB_REAL].zfd > -1) {
2820 dahdi_close(cur->subs[SUB_REAL].zfd);
2822 destroy_dahdi_pvt(&cur);
2828 static char *dahdi_send_keypad_facility_app = "DAHDISendKeypadFacility";
2830 static char *dahdi_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
2832 static char *dahdi_send_keypad_facility_descrip =
2833 " DAHDISendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
2834 " IE over the current channel.\n";
2836 static int dahdi_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2838 /* Data will be our digit string */
2839 struct dahdi_pvt *p;
2840 char *digits = (char *) data;
2842 if (ast_strlen_zero(digits)) {
2843 ast_debug(1, "No digit string sent to application!\n");
2847 p = (struct dahdi_pvt *)chan->tech_pvt;
2850 ast_debug(1, "Unable to find technology private\n");
2854 ast_mutex_lock(&p->lock);
2856 if (!p->pri || !p->call) {
2857 ast_debug(1, "Unable to find pri or call on channel!\n");
2858 ast_mutex_unlock(&p->lock);
2862 if (!pri_grab(p, p->pri)) {
2863 pri_keypad_facility(p->pri->pri, p->call, digits);
2866 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
2867 ast_mutex_unlock(&p->lock);
2871 ast_mutex_unlock(&p->lock);
2876 static int pri_is_up(struct dahdi_pri *pri)
2879 for (x = 0; x < NUM_DCHANS; x++) {
2880 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
2886 static int pri_assign_bearer(struct dahdi_pvt *crv, struct dahdi_pri *pri, struct dahdi_pvt *bearer)
2888 bearer->owner = &inuse;
2889 bearer->realcall = crv;
2890 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
2891 if (crv->subs[SUB_REAL].owner)
2892 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].zfd);
2893 crv->bearer = bearer;
2894 crv->call = bearer->call;
2899 static char *pri_order(int level)
2909 return "Quaternary";
2915 /* Returns fd of the active dchan */
2916 static int pri_active_dchan_fd(struct dahdi_pri *pri)
2920 for (x = 0; x < NUM_DCHANS; x++) {
2921 if ((pri->dchans[x] == pri->pri))
2928 static int pri_find_dchan(struct dahdi_pri *pri)
2935 for (x = 0; x < NUM_DCHANS; x++) {
2936 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
2938 if (pri->dchans[x] == old) {
2944 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
2945 pri->dchannels[newslot]);
2947 if (old && (oldslot != newslot))
2948 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
2949 pri->dchannels[oldslot], pri->dchannels[newslot]);
2950 pri->pri = pri->dchans[newslot];
2955 static int dahdi_hangup(struct ast_channel *ast)
2959 /*static int restore_gains(struct dahdi_pvt *p);*/
2960 struct dahdi_pvt *p = ast->tech_pvt;
2961 struct dahdi_pvt *tmp = NULL;
2962 struct dahdi_pvt *prev = NULL;
2965 ast_debug(1, "dahdi_hangup(%s)\n", ast->name);
2966 if (!ast->tech_pvt) {
2967 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
2971 ast_mutex_lock(&p->lock);
2973 index = dahdi_get_index(ast, p, 1);
2975 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
2977 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
2981 dahdi_confmute(p, 0);
2984 if (p->origcid_num) {
2985 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
2986 ast_free(p->origcid_num);
2987 p->origcid_num = NULL;
2989 if (p->origcid_name) {
2990 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
2991 ast_free(p->origcid_name);
2992 p->origcid_name = NULL;
2995 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
2999 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
3000 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
3004 /* Real channel, do some fixup */
3005 p->subs[index].owner = NULL;
3006 p->subs[index].needanswer = 0;
3007 p->subs[index].needflash = 0;
3008 p->subs[index].needringing = 0;
3009 p->subs[index].needbusy = 0;
3010 p->subs[index].needcongestion = 0;
3011 p->subs[index].linear = 0;
3012 p->subs[index].needcallerid = 0;
3013 p->polarity = POLARITY_IDLE;
3014 dahdi_setlinear(p->subs[index].zfd, 0);
3015 if (index == SUB_REAL) {
3016 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
3017 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
3018 if (p->subs[SUB_CALLWAIT].inthreeway) {
3019 /* We had flipped over to answer a callwait and now it's gone */
3020 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
3021 /* Move to the call-wait, but un-own us until they flip back. */
3022 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3023 unalloc_sub(p, SUB_CALLWAIT);
3026 /* The three way hung up, but we still have a call wait */
3027 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
3028 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3029 unalloc_sub(p, SUB_THREEWAY);
3030 if (p->subs[SUB_REAL].inthreeway) {
3031 /* This was part of a three way call. Immediately make way for
3033 ast_debug(1, "Call was complete, setting owner to former third call\n");
3034 p->owner = p->subs[SUB_REAL].owner;
3036 /* This call hasn't been completed yet... Set owner to NULL */
3037 ast_debug(1, "Call was incomplete, setting owner to NULL\n");