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"
105 <application name="DAHDISendKeypadFacility" language="en_US">
107 Send digits out of band over a PRI.
110 <parameter name="digits" required="true" />
113 <para>This application will send the given string of digits in a Keypad
114 Facility IE over the current channel.</para>
117 <application name="DAHDISendCallreroutingFacility" language="en_US">
119 Send QSIG call rerouting facility over a PRI.
122 <parameter name="destination" required="true">
123 <para>Destination number.</para>
125 <parameter name="original">
126 <para>Original called number.</para>
128 <parameter name="reason">
129 <para>Diversion reason, if not specified defaults to <literal>unknown</literal></para>
133 <para>This application will send a Callrerouting Facility IE over the
134 current channel.</para>
139 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
141 static const char *lbostr[] = {
142 "0 db (CSU)/0-133 feet (DSX-1)",
143 "133-266 feet (DSX-1)",
144 "266-399 feet (DSX-1)",
145 "399-533 feet (DSX-1)",
146 "533-655 feet (DSX-1)",
152 /*! Global jitterbuffer configuration - by default, jb is disabled */
153 static struct ast_jb_conf default_jbconf =
157 .resync_threshold = -1,
160 static struct ast_jb_conf global_jbconf;
162 /* define this to send PRI user-user information elements */
163 #undef SUPPORT_USERUSER
166 * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
167 * the user hangs up to reset the state machine so ring works properly.
168 * This is used to be able to support kewlstart by putting the zhone in
169 * groundstart mode since their forward disconnect supervision is entirely
170 * broken even though their documentation says it isn't and their support
171 * is entirely unwilling to provide any assistance with their channel banks
172 * even though their web site says they support their products for life.
174 /* #define ZHONE_HACK */
177 * Define if you want to check the hook state for an FXO (FXS signalled) interface
178 * before dialing on it. Certain FXO interfaces always think they're out of
179 * service with this method however.
181 /* #define DAHDI_CHECK_HOOKSTATE */
183 /*! \brief Typically, how many rings before we should send Caller*ID */
184 #define DEFAULT_CIDRINGS 1
186 #define CHANNEL_PSEUDO -12
188 #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
191 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
192 #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))
194 static const char tdesc[] = "DAHDI Telephony Driver"
203 static const char config[] = "chan_dahdi.conf";
205 #define SIG_EM DAHDI_SIG_EM
206 #define SIG_EMWINK (0x0100000 | DAHDI_SIG_EM)
207 #define SIG_FEATD (0x0200000 | DAHDI_SIG_EM)
208 #define SIG_FEATDMF (0x0400000 | DAHDI_SIG_EM)
209 #define SIG_FEATB (0x0800000 | DAHDI_SIG_EM)
210 #define SIG_E911 (0x1000000 | DAHDI_SIG_EM)
211 #define SIG_FEATDMF_TA (0x2000000 | DAHDI_SIG_EM)
212 #define SIG_FGC_CAMA (0x4000000 | DAHDI_SIG_EM)
213 #define SIG_FGC_CAMAMF (0x8000000 | DAHDI_SIG_EM)
214 #define SIG_FXSLS DAHDI_SIG_FXSLS
215 #define SIG_FXSGS DAHDI_SIG_FXSGS
216 #define SIG_FXSKS DAHDI_SIG_FXSKS
217 #define SIG_FXOLS DAHDI_SIG_FXOLS
218 #define SIG_FXOGS DAHDI_SIG_FXOGS
219 #define SIG_FXOKS DAHDI_SIG_FXOKS
220 #define SIG_PRI DAHDI_SIG_CLEAR
221 #define SIG_BRI (0x2000000 | DAHDI_SIG_CLEAR)
222 #define SIG_BRI_PTMP (0X4000000 | DAHDI_SIG_CLEAR)
223 #define SIG_SS7 (0x1000000 | DAHDI_SIG_CLEAR)
224 #define SIG_SF DAHDI_SIG_SF
225 #define SIG_SFWINK (0x0100000 | DAHDI_SIG_SF)
226 #define SIG_SF_FEATD (0x0200000 | DAHDI_SIG_SF)
227 #define SIG_SF_FEATDMF (0x0400000 | DAHDI_SIG_SF)
228 #define SIG_SF_FEATB (0x0800000 | DAHDI_SIG_SF)
229 #define SIG_EM_E1 DAHDI_SIG_EM_E1
230 #define SIG_GR303FXOKS (0x0100000 | DAHDI_SIG_FXOKS)
231 #define SIG_GR303FXSKS (0x0100000 | DAHDI_SIG_FXSKS)
234 #define NUM_SPANS DAHDI_MAX_SPANS
238 #define NUM_DCHANS 4 /*!< No more than 4 d-channels */
239 #define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
241 #define CHAN_PSEUDO -2
243 #define DCHAN_PROVISIONED (1 << 0)
244 #define DCHAN_NOTINALARM (1 << 1)
245 #define DCHAN_UP (1 << 2)
247 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
249 /* Overlap dialing option types */
250 #define DAHDI_OVERLAPDIAL_NONE 0
251 #define DAHDI_OVERLAPDIAL_OUTGOING 1
252 #define DAHDI_OVERLAPDIAL_INCOMING 2
253 #define DAHDI_OVERLAPDIAL_BOTH (DAHDI_OVERLAPDIAL_INCOMING|DAHDI_OVERLAPDIAL_OUTGOING)
255 #define CALLPROGRESS_PROGRESS 1
256 #define CALLPROGRESS_FAX_OUTGOING 2
257 #define CALLPROGRESS_FAX_INCOMING 4
258 #define CALLPROGRESS_FAX (CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING)
260 static char defaultcic[64] = "";
261 static char defaultozz[64] = "";
263 static char parkinglot[AST_MAX_EXTENSION] = ""; /*!< Default parking lot for this channel */
265 /*! Run this script when the MWI state changes on an FXO line, if mwimonitor is enabled */
266 static char mwimonitornotify[PATH_MAX] = "";
267 #ifndef HAVE_DAHDI_LINEREVERSE_VMWI
268 static int mwisend_rpas = 0;
271 static char progzone[10] = "";
273 static int usedistinctiveringdetection = 0;
274 static int distinctiveringaftercid = 0;
276 static int numbufs = 4;
278 static int mwilevel = 512;
281 static struct ast_channel inuse;
282 #ifdef PRI_GETSET_TIMERS
283 static int pritimers[PRI_MAX_TIMERS];
285 static int pridebugfd = -1;
286 static char pridebugfilename[1024] = "";
289 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
290 static int firstdigittimeout = 16000;
292 /*! \brief How long to wait for following digits (FXO logic) */
293 static int gendigittimeout = 8000;
295 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
296 static int matchdigittimeout = 3000;
298 /*! \brief Protect the interface list (of dahdi_pvt's) */
299 AST_MUTEX_DEFINE_STATIC(iflock);
301 /* QSIG channel mapping option types */
302 #define DAHDI_CHAN_MAPPING_PHYSICAL 0
303 #define DAHDI_CHAN_MAPPING_LOGICAL 1
306 static int ifcount = 0;
309 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
312 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
313 when it's doing something critical. */
314 AST_MUTEX_DEFINE_STATIC(monlock);
316 /*! \brief This is the thread for the monitor which checks for input on the channels
317 which are not currently in use. */
318 static pthread_t monitor_thread = AST_PTHREADT_NULL;
319 static ast_cond_t ss_thread_complete;
320 AST_MUTEX_DEFINE_STATIC(ss_thread_lock);
321 AST_MUTEX_DEFINE_STATIC(restart_lock);
322 static int ss_thread_count = 0;
323 static int num_restart_pending = 0;
325 static int restart_monitor(void);
327 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);
329 static int dahdi_sendtext(struct ast_channel *c, const char *text);
331 static void mwi_event_cb(const struct ast_event *event, void *userdata)
333 /* This module does not handle MWI in an event-based manner. However, it
334 * subscribes to MWI for each mailbox that is configured so that the core
335 * knows that we care about it. Then, chan_dahdi will get the MWI from the
336 * event cache instead of checking the mailbox directly. */
339 /*! \brief Avoid the silly dahdi_getevent which ignores a bunch of events */
340 static inline int dahdi_get_event(int fd)
343 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
348 /*! \brief Avoid the silly dahdi_waitevent which ignores a bunch of events */
349 static inline int dahdi_wait_event(int fd)
352 i = DAHDI_IOMUX_SIGEVENT;
353 if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
355 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
360 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
361 #define READ_SIZE 160
363 #define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
364 #define MASK_INUSE (1 << 1) /*!< Channel currently in use */
366 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
367 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 10,000 ms */
368 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE) /*!< 500 ms */
369 #define MIN_MS_SINCE_FLASH ( (2000) ) /*!< 2000 ms */
370 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE) /*!< 8,000 ms */
374 static int ringt_base = DEFAULT_RINGT;
378 #define LINKSTATE_INALARM (1 << 0)
379 #define LINKSTATE_STARTING (1 << 1)
380 #define LINKSTATE_UP (1 << 2)
381 #define LINKSTATE_DOWN (1 << 3)
383 #define SS7_NAI_DYNAMIC -1
385 #define LINKSET_FLAG_EXPLICITACM (1 << 0)
388 pthread_t master; /*!< Thread of master */
392 int linkstate[NUM_DCHANS];
396 LINKSET_STATE_DOWN = 0,
399 char called_nai; /*!< Called Nature of Address Indicator */
400 char calling_nai; /*!< Calling Nature of Address Indicator */
401 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
402 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
403 char subscriberprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
404 char unknownprefix[20]; /*!< for unknown dialplans */
406 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
407 int flags; /*!< Linkset flags */
410 static struct dahdi_ss7 linksets[NUM_SPANS];
412 static int cur_ss7type = -1;
413 static int cur_linkset = -1;
414 static int cur_pointcode = -1;
415 static int cur_cicbeginswith = -1;
416 static int cur_adjpointcode = -1;
417 static int cur_networkindicator = -1;
418 static int cur_defaultdpc = -1;
419 #endif /* HAVE_SS7 */
423 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
424 #define PRI_CHANNEL(p) ((p) & 0xff)
425 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
426 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
429 pthread_t master; /*!< Thread of master */
430 ast_mutex_t lock; /*!< Mutex */
431 char idleext[AST_MAX_EXTENSION]; /*!< Where to idle extra calls */
432 char idlecontext[AST_MAX_CONTEXT]; /*!< What context to use for idle */
433 char idledial[AST_MAX_EXTENSION]; /*!< What to dial before dumping */
434 int minunused; /*!< Min # of channels to keep empty */
435 int minidle; /*!< Min # of "idling" calls to keep active */
436 int nodetype; /*!< Node type */
437 int switchtype; /*!< Type of switch to emulate */
438 int nsf; /*!< Network-Specific Facilities */
439 int dialplan; /*!< Dialing plan */
440 int localdialplan; /*!< Local dialing plan */
441 char internationalprefix[10]; /*!< country access code ('00' for european dialplans) */
442 char nationalprefix[10]; /*!< area access code ('0' for european dialplans) */
443 char localprefix[20]; /*!< area access code + area code ('0'+area code for european dialplans) */
444 char privateprefix[20]; /*!< for private dialplans */
445 char unknownprefix[20]; /*!< for unknown dialplans */
446 int dchannels[NUM_DCHANS]; /*!< What channel are the dchannels on */
447 int trunkgroup; /*!< What our trunkgroup is */
448 int mastertrunkgroup; /*!< What trunk group is our master */
449 int prilogicalspan; /*!< Logical span number within trunk group */
450 int numchans; /*!< Num of channels we represent */
451 int overlapdial; /*!< In overlap dialing mode */
452 int qsigchannelmapping; /*!< QSIG channel mapping type */
453 int discardremoteholdretrieval; /*!< shall remote hold or remote retrieval notifications be discarded? */
454 int facilityenable; /*!< Enable facility IEs */
455 struct pri *dchans[NUM_DCHANS]; /*!< Actual d-channels */
456 int dchanavail[NUM_DCHANS]; /*!< Whether each channel is available */
457 struct pri *pri; /*!< Currently active D-channel */
459 int fds[NUM_DCHANS]; /*!< FD's for d-channels */
464 #ifdef HAVE_PRI_INBANDDISCONNECT
465 unsigned int inbanddisconnect:1; /*!< Should we support inband audio after receiving DISCONNECT? */
467 time_t lastreset; /*!< time when unused channels were last reset */
468 long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
470 struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
471 struct dahdi_pvt *crvs; /*!< Member CRV structs */
472 struct dahdi_pvt *crvend; /*!< Pointer to end of CRV structs */
476 static struct dahdi_pri pris[NUM_SPANS];
479 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
481 #define DEFAULT_PRI_DEBUG 0
484 static inline void pri_rel(struct dahdi_pri *pri)
486 ast_mutex_unlock(&pri->lock);
490 /*! Shut up the compiler */
494 #define SUB_REAL 0 /*!< Active call */
495 #define SUB_CALLWAIT 1 /*!< Call-Waiting call on hold */
496 #define SUB_THREEWAY 2 /*!< Three-way call */
498 /* Polarity states */
499 #define POLARITY_IDLE 0
500 #define POLARITY_REV 1
503 struct distRingData {
507 struct ringContextData {
508 char contextData[AST_MAX_CONTEXT];
510 struct dahdi_distRings {
511 struct distRingData ringnum[3];
512 struct ringContextData ringContext[3];
515 static char *subnames[] = {
521 struct dahdi_subchannel {
523 struct ast_channel *owner;
525 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
526 struct ast_frame f; /*!< One frame for each channel. How did this ever work before? */
527 unsigned int needringing:1;
528 unsigned int needbusy:1;
529 unsigned int needcongestion:1;
530 unsigned int needcallerid:1;
531 unsigned int needanswer:1;
532 unsigned int needflash:1;
533 unsigned int needhold:1;
534 unsigned int needunhold:1;
535 unsigned int linear:1;
536 unsigned int inthreeway:1;
537 struct dahdi_confinfo curconf;
540 #define CONF_USER_REAL (1 << 0)
541 #define CONF_USER_THIRDCALL (1 << 1)
545 /* States for sending MWI message
546 * First three states are required for send Ring Pulse Alert Signal
558 struct mwisend_info {
559 struct timeval pause;
560 mwisend_states mwisend_current;
563 static struct dahdi_pvt {
565 struct ast_channel *owner; /*!< Our current active owner (if applicable) */
566 /*!< Up to three channels can be associated with this call */
568 struct dahdi_subchannel sub_unused; /*!< Just a safety precaution */
569 struct dahdi_subchannel subs[3]; /*!< Sub-channels */
570 struct dahdi_confinfo saveconf; /*!< Saved conference info */
572 struct dahdi_pvt *slaves[MAX_SLAVES]; /*!< Slave to us (follows our conferencing) */
573 struct dahdi_pvt *master; /*!< Master to us (we follow their conferencing) */
574 int inconference; /*!< If our real should be in the conference */
576 int buf_no; /*!< Number of buffers */
577 int buf_policy; /*!< Buffer policy */
578 int sig; /*!< Signalling style */
579 int radio; /*!< radio type */
580 int outsigmod; /*!< Outbound Signalling style (modifier) */
581 int oprmode; /*!< "Operator Services" mode */
582 struct dahdi_pvt *oprpeer; /*!< "Operator Services" peer tech_pvt ptr */
583 float cid_rxgain; /*!< "Gain to apply during caller id */
586 int tonezone; /*!< tone zone for this chan, or -1 for default */
587 struct dahdi_pvt *next; /*!< Next channel in list */
588 struct dahdi_pvt *prev; /*!< Prev channel in list */
592 unsigned int answeronpolarityswitch:1;
593 unsigned int busydetect:1;
594 unsigned int callreturn:1;
595 unsigned int callwaiting:1;
596 unsigned int callwaitingcallerid:1;
597 unsigned int cancallforward:1;
598 unsigned int canpark:1;
599 unsigned int confirmanswer:1; /*!< Wait for '#' to confirm answer */
600 unsigned int destroy:1;
601 unsigned int didtdd:1; /*!< flag to say its done it once */
602 unsigned int dialednone:1;
603 unsigned int dialing:1;
604 unsigned int digital:1;
606 unsigned int echobreak:1;
607 unsigned int echocanbridged:1;
608 unsigned int echocanon:1;
609 unsigned int faxhandled:1; /*!< Has a fax tone already been handled? */
610 unsigned int firstradio:1;
611 unsigned int hanguponpolarityswitch:1;
612 unsigned int hardwaredtmf:1;
613 unsigned int hidecallerid:1;
614 unsigned int hidecalleridname:1; /*!< Hide just the name not the number for legacy PBX use */
615 unsigned int ignoredtmf:1;
616 unsigned int immediate:1; /*!< Answer before getting digits? */
617 unsigned int inalarm:1;
618 unsigned int mate:1; /*!< flag to say its in MATE mode */
619 unsigned int outgoing:1;
620 /* unsigned int overlapdial:1; unused and potentially confusing */
621 unsigned int permcallwaiting:1;
622 unsigned int permhidecallerid:1; /*!< Whether to hide our outgoing caller ID or not */
623 unsigned int priindication_oob:1;
624 unsigned int priexclusive:1;
625 unsigned int pulse:1;
626 unsigned int pulsedial:1; /*!< whether a pulse dial phone is detected */
627 unsigned int restartpending:1; /*!< flag to ensure counted only once for restart */
628 unsigned int restrictcid:1; /*!< Whether restrict the callerid -> only send ANI */
629 unsigned int threewaycalling:1;
630 unsigned int transfer:1;
631 unsigned int use_callerid:1; /*!< Whether or not to use caller id on this channel */
632 unsigned int use_callingpres:1; /*!< Whether to use the callingpres the calling switch sends */
633 unsigned int usedistinctiveringdetection:1;
634 unsigned int dahditrcallerid:1; /*!< should we use the callerid from incoming call on dahdi transfer or not */
635 unsigned int transfertobusy:1; /*!< allow flash-transfers to busy channels */
636 unsigned int mwimonitor_neon:1; /*!< monitor this FXO port for neon type MWI indication from other end */
637 unsigned int mwimonitor_fsk:1; /*!< monitor this FXO port for fsk MWI indication from other end */
638 unsigned int mwimonitor_rpas:1; /*!< monitor this FXO port for rpas precursor to fsk MWI indication */
639 unsigned int mwimonitoractive:1; /*!< an MWI monitor thread is currently active */
640 unsigned int mwisendactive:1; /*!< a MWI message sending thread is active */
641 /* Channel state or unavilability flags */
642 unsigned int inservice:1;
643 unsigned int locallyblocked:1;
644 unsigned int remotelyblocked:1;
645 #if defined(HAVE_PRI) || defined(HAVE_SS7)
647 unsigned int alerting:1;
648 unsigned int alreadyhungup:1;
649 unsigned int isidlecall:1;
650 unsigned int proceeding:1;
651 unsigned int progress:1;
652 unsigned int resetting:1;
653 unsigned int setup_ack:1;
655 unsigned int use_smdi:1; /* Whether to use SMDI on this channel */
656 struct mwisend_info mwisend_data;
657 struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
659 struct dahdi_distRings drings;
661 char context[AST_MAX_CONTEXT];
662 char defcontext[AST_MAX_CONTEXT];
663 char exten[AST_MAX_EXTENSION];
664 char language[MAX_LANGUAGE];
665 char mohinterpret[MAX_MUSICCLASS];
666 char mohsuggest[MAX_MUSICCLASS];
667 char parkinglot[AST_MAX_EXTENSION]; /*!< Parking lot for this channel */
668 #if defined(PRI_ANI) || defined(HAVE_SS7)
669 char cid_ani[AST_MAX_EXTENSION];
672 char cid_num[AST_MAX_EXTENSION];
673 int cid_ton; /*!< Type Of Number (TON) */
674 char cid_name[AST_MAX_EXTENSION];
675 char lastcid_num[AST_MAX_EXTENSION];
676 char lastcid_name[AST_MAX_EXTENSION];
677 char *origcid_num; /*!< malloced original callerid */
678 char *origcid_name; /*!< malloced original callerid */
679 char callwait_num[AST_MAX_EXTENSION];
680 char callwait_name[AST_MAX_EXTENSION];
681 char rdnis[AST_MAX_EXTENSION];
682 char dnid[AST_MAX_EXTENSION];
685 int confno; /*!< Our conference */
686 int confusers; /*!< Who is using our conference */
687 int propconfno; /*!< Propagated conference number */
688 ast_group_t callgroup;
689 ast_group_t pickupgroup;
690 struct ast_variable *vars;
691 int channel; /*!< Channel Number or CRV */
692 int span; /*!< Span number */
693 time_t guardtime; /*!< Must wait this much time before using for new call */
694 int cid_signalling; /*!< CID signalling type bell202 or v23 */
695 int cid_start; /*!< CID start indicator, polarity or ring */
696 int callingpres; /*!< The value of calling presentation that we're going to use when placing a PRI call */
697 int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
698 int cidcwexpire; /*!< When to expire our muting for CID/CW */
699 unsigned char *cidspill;
708 struct dahdi_echocanparams head;
709 struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
715 int busy_quietlength;
718 struct timeval waitingfordt; /*!< Time we started waiting for dialtone */
719 struct timeval flashtime; /*!< Last flash-hook time */
721 int cref; /*!< Call reference number */
722 struct dahdi_dialoperation dop;
723 int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
725 char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
726 int amaflags; /*!< AMA Flags */
727 struct tdd_state *tdd; /*!< TDD flag */
728 char call_forward[AST_MAX_EXTENSION];
729 char mailbox[AST_MAX_EXTENSION];
730 struct ast_event_sub *mwi_event_sub;
735 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
736 struct dahdi_vmwi_info mwisend_setting; /*!< Which VMWI methods to use */
737 unsigned int mwisend_fsk: 1; /*! Variable for enabling FSK MWI handling in chan_dahdi */
738 unsigned int mwisend_rpas:1; /*! Variable for enabling Ring Pulse Alert before MWI FSK Spill */
740 int distinctivering; /*!< Which distinctivering to use */
741 int cidrings; /*!< Which ring to deliver CID on */
742 int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
744 int polarityonanswerdelay;
745 struct timeval polaritydelaytv;
746 int sendcalleridafter;
748 struct dahdi_pri *pri;
749 struct dahdi_pvt *bearer;
750 struct dahdi_pvt *realcall;
758 struct dahdi_ss7 *ss7;
759 struct isup_call *ss7call;
760 char charge_number[50];
761 char gen_add_number[50];
762 char gen_dig_number[50];
763 char orig_called_num[50];
764 char redirecting_num[50];
765 char generic_name[50];
766 unsigned char gen_add_num_plan;
767 unsigned char gen_add_nai;
768 unsigned char gen_add_pres_ind;
769 unsigned char gen_add_type;
770 unsigned char gen_dig_type;
771 unsigned char gen_dig_scheme;
773 unsigned char lspi_type;
774 unsigned char lspi_scheme;
775 unsigned char lspi_context;
777 unsigned int call_ref_ident;
778 unsigned int call_ref_pc;
779 unsigned char calling_party_cat;
781 int cic; /*!< CIC associated with channel */
782 unsigned int dpc; /*!< CIC's DPC */
783 unsigned int loopedback:1;
787 } *iflist = NULL, *ifend = NULL;
789 /*! \brief Channel configuration from chan_dahdi.conf .
790 * This struct is used for parsing the [channels] section of chan_dahdi.conf.
791 * Generally there is a field here for every possible configuration item.
793 * The state of fields is saved along the parsing and whenever a 'channel'
794 * statement is reached, the current dahdi_chan_conf is used to configure the
795 * channel (struct dahdi_pvt)
797 * \see dahdi_chan_init for the default values.
799 struct dahdi_chan_conf {
800 struct dahdi_pvt chan;
802 struct dahdi_pri pri;
806 struct dahdi_ss7 ss7;
808 struct dahdi_params timing;
809 int is_sig_auto; /*!< Use channel signalling from DAHDI? */
811 char smdi_port[SMDI_MAX_FILENAME_LEN];
814 /*! returns a new dahdi_chan_conf with default values (by-value) */
815 static struct dahdi_chan_conf dahdi_chan_conf_default(void)
817 /* recall that if a field is not included here it is initialized
820 struct dahdi_chan_conf conf = {
824 .switchtype = PRI_SWITCH_NI2,
825 .dialplan = PRI_UNKNOWN + 1,
826 .localdialplan = PRI_NATIONAL_ISDN + 1,
828 .qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL,
833 .internationalprefix = "",
834 .nationalprefix = "",
843 .called_nai = SS7_NAI_NATIONAL,
844 .calling_nai = SS7_NAI_NATIONAL,
845 .internationalprefix = "",
846 .nationalprefix = "",
847 .subscriberprefix = "",
852 .context = "default",
855 .mohinterpret = "default",
860 .cid_signalling = CID_SIG_BELL,
861 .cid_start = CID_START_RING,
862 .dahditrcallerid = 0,
871 .echocancel.head.tap_length = 1,
879 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
882 .polarityonanswerdelay = 600,
884 .sendcalleridafter = DEFAULT_CIDRINGS,
886 .buf_policy = DAHDI_POLICY_IMMEDIATE,
900 .smdi_port = "/dev/ttyS0",
907 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause);
908 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
909 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
910 static int dahdi_sendtext(struct ast_channel *c, const char *text);
911 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
912 static int dahdi_hangup(struct ast_channel *ast);
913 static int dahdi_answer(struct ast_channel *ast);
914 static struct ast_frame *dahdi_read(struct ast_channel *ast);
915 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
916 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
917 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
918 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
919 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
920 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
921 static int handle_init_event(struct dahdi_pvt *i, int event);
923 static const struct ast_channel_tech dahdi_tech = {
925 .description = tdesc,
926 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
927 .requester = dahdi_request,
928 .send_digit_begin = dahdi_digit_begin,
929 .send_digit_end = dahdi_digit_end,
930 .send_text = dahdi_sendtext,
932 .hangup = dahdi_hangup,
933 .answer = dahdi_answer,
935 .write = dahdi_write,
936 .bridge = dahdi_bridge,
937 .exception = dahdi_exception,
938 .indicate = dahdi_indicate,
939 .fixup = dahdi_fixup,
940 .setoption = dahdi_setoption,
941 .func_channel_read = dahdi_func_read,
945 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
947 #define GET_CHANNEL(p) ((p)->channel)
950 struct dahdi_pvt *round_robin[32];
953 static inline int pri_grab(struct dahdi_pvt *pvt, struct dahdi_pri *pri)
956 /* Grab the lock first */
958 res = ast_mutex_trylock(&pri->lock);
960 DEADLOCK_AVOIDANCE(&pvt->lock);
963 /* Then break the poll */
964 if (pri->master != AST_PTHREADT_NULL)
965 pthread_kill(pri->master, SIGURG);
971 static inline void ss7_rel(struct dahdi_ss7 *ss7)
973 ast_mutex_unlock(&ss7->lock);
976 static inline int ss7_grab(struct dahdi_pvt *pvt, struct dahdi_ss7 *pri)
979 /* Grab the lock first */
981 res = ast_mutex_trylock(&pri->lock);
983 DEADLOCK_AVOIDANCE(&pvt->lock);
986 /* Then break the poll */
987 if (pri->master != AST_PTHREADT_NULL)
988 pthread_kill(pri->master, SIGURG);
992 #define NUM_CADENCE_MAX 25
993 static int num_cadence = 4;
994 static int user_has_defined_cadences = 0;
996 static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
997 { { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
998 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
999 { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
1000 { { 1000, 500, 2500, 5000 } }, /*!< Long ring */
1003 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
1004 * is 1, the second pause is 2 and so on.
1007 static int cidrings[NUM_CADENCE_MAX] = {
1008 2, /*!< Right after first long ring */
1009 4, /*!< Right after long part */
1010 3, /*!< After third chirp */
1011 2, /*!< Second spell */
1014 /* ETSI EN300 659-1 specifies the ring pulse between 200 and 300 mS */
1015 static struct dahdi_ring_cadence AS_RP_cadence = {{250, 10000}};
1017 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
1018 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
1020 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
1021 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
1023 static int dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok)
1026 if (p->subs[SUB_REAL].owner == ast)
1028 else if (p->subs[SUB_CALLWAIT].owner == ast)
1030 else if (p->subs[SUB_THREEWAY].owner == ast)
1035 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
1040 static void wakeup_sub(struct dahdi_pvt *p, int a, struct dahdi_pri *pri)
1044 ast_mutex_unlock(&pri->lock);
1047 if (p->subs[a].owner) {
1048 if (ast_channel_trylock(p->subs[a].owner)) {
1049 DEADLOCK_AVOIDANCE(&p->lock);
1051 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
1052 ast_channel_unlock(p->subs[a].owner);
1060 ast_mutex_lock(&pri->lock);
1064 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *data)
1067 struct dahdi_pri *pri = (struct dahdi_pri*) data;
1070 struct dahdi_ss7 *ss7 = (struct dahdi_ss7*) data;
1072 /* We must unlock the PRI to avoid the possibility of a deadlock */
1073 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1080 ast_mutex_unlock(&pri->lock);
1085 ast_mutex_unlock(&ss7->lock);
1095 if (ast_channel_trylock(p->owner)) {
1096 DEADLOCK_AVOIDANCE(&p->lock);
1098 ast_queue_frame(p->owner, f);
1099 ast_channel_unlock(p->owner);
1105 #if defined(HAVE_PRI) || defined(HAVE_SS7)
1112 ast_mutex_lock(&pri->lock);
1117 ast_mutex_lock(&ss7->lock);
1127 static int restore_gains(struct dahdi_pvt *p);
1129 static void swap_subs(struct dahdi_pvt *p, int a, int b)
1133 struct ast_channel *towner;
1135 ast_debug(1, "Swapping %d and %d\n", a, b);
1137 tchan = p->subs[a].chan;
1138 towner = p->subs[a].owner;
1139 tinthreeway = p->subs[a].inthreeway;
1141 p->subs[a].chan = p->subs[b].chan;
1142 p->subs[a].owner = p->subs[b].owner;
1143 p->subs[a].inthreeway = p->subs[b].inthreeway;
1145 p->subs[b].chan = tchan;
1146 p->subs[b].owner = towner;
1147 p->subs[b].inthreeway = tinthreeway;
1149 if (p->subs[a].owner)
1150 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].dfd);
1151 if (p->subs[b].owner)
1152 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].dfd);
1153 wakeup_sub(p, a, NULL);
1154 wakeup_sub(p, b, NULL);
1157 static int dahdi_open(char *fn)
1165 for (x = 0; x < strlen(fn); x++) {
1166 if (!isdigit(fn[x])) {
1174 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
1177 fn = "/dev/dahdi/channel";
1179 fd = open(fn, O_RDWR | O_NONBLOCK);
1181 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
1185 if (ioctl(fd, DAHDI_SPECIFY, &chan)) {
1189 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
1194 if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs) == -1) {
1195 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
1204 static void dahdi_close(int fd)
1210 static void dahdi_close_sub(struct dahdi_pvt *chan_pvt, int sub_num)
1212 dahdi_close(chan_pvt->subs[sub_num].dfd);
1213 chan_pvt->subs[sub_num].dfd = -1;
1217 static void dahdi_close_pri_fd(struct dahdi_pri *pri, int fd_num)
1219 dahdi_close(pri->fds[fd_num]);
1220 pri->fds[fd_num] = -1;
1225 static void dahdi_close_ss7_fd(struct dahdi_ss7 *ss7, int fd_num)
1227 dahdi_close(ss7->fds[fd_num]);
1228 ss7->fds[fd_num] = -1;
1232 static int dahdi_setlinear(int dfd, int linear)
1235 res = ioctl(dfd, DAHDI_SETLINEAR, &linear);
1242 static int alloc_sub(struct dahdi_pvt *p, int x)
1244 struct dahdi_bufferinfo bi;
1246 if (p->subs[x].dfd >= 0) {
1247 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
1251 p->subs[x].dfd = dahdi_open("/dev/dahdi/pseudo");
1252 if (p->subs[x].dfd <= -1) {
1253 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
1257 res = ioctl(p->subs[x].dfd, DAHDI_GET_BUFINFO, &bi);
1259 bi.txbufpolicy = p->buf_policy;
1260 bi.rxbufpolicy = p->buf_policy;
1261 bi.numbufs = p->buf_no;
1262 res = ioctl(p->subs[x].dfd, DAHDI_SET_BUFINFO, &bi);
1264 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", x, strerror(errno));
1267 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
1269 if (ioctl(p->subs[x].dfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
1270 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].dfd, strerror(errno));
1271 dahdi_close_sub(p, x);
1272 p->subs[x].dfd = -1;
1275 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].dfd, p->subs[x].chan);
1279 static int unalloc_sub(struct dahdi_pvt *p, int x)
1282 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
1285 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
1286 dahdi_close_sub(p, x);
1287 p->subs[x].linear = 0;
1288 p->subs[x].chan = 0;
1289 p->subs[x].owner = NULL;
1290 p->subs[x].inthreeway = 0;
1291 p->polarity = POLARITY_IDLE;
1292 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
1296 static int digit_to_dtmfindex(char digit)
1299 return DAHDI_TONE_DTMF_BASE + (digit - '0');
1300 else if (digit >= 'A' && digit <= 'D')
1301 return DAHDI_TONE_DTMF_A + (digit - 'A');
1302 else if (digit >= 'a' && digit <= 'd')
1303 return DAHDI_TONE_DTMF_A + (digit - 'a');
1304 else if (digit == '*')
1305 return DAHDI_TONE_DTMF_s;
1306 else if (digit == '#')
1307 return DAHDI_TONE_DTMF_p;
1312 static int dahdi_digit_begin(struct ast_channel *chan, char digit)
1314 struct dahdi_pvt *pvt;
1318 pvt = chan->tech_pvt;
1320 ast_mutex_lock(&pvt->lock);
1322 idx = dahdi_get_index(chan, pvt, 0);
1324 if ((idx != SUB_REAL) || !pvt->owner)
1328 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1329 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
1330 if (pvt->setup_ack) {
1331 if (!pri_grab(pvt, pvt->pri)) {
1332 pri_information(pvt->pri->pri, pvt->call, digit);
1335 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
1336 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
1338 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
1339 res = strlen(pvt->dialdest);
1340 pvt->dialdest[res++] = digit;
1341 pvt->dialdest[res] = '\0';
1346 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
1349 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &dtmf)) {
1351 struct dahdi_dialoperation zo = {
1352 .op = DAHDI_DIAL_OP_APPEND,
1355 zo.dialstr[0] = 'T';
1356 zo.dialstr[1] = digit;
1357 zo.dialstr[2] = '\0';
1358 if ((res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_DIAL, &zo)))
1359 ast_log(LOG_WARNING, "Couldn't dial digit %c: %s\n", digit, strerror(errno));
1363 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
1365 pvt->begindigit = digit;
1369 ast_mutex_unlock(&pvt->lock);
1374 static int dahdi_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
1376 struct dahdi_pvt *pvt;
1381 pvt = chan->tech_pvt;
1383 ast_mutex_lock(&pvt->lock);
1385 idx = dahdi_get_index(chan, pvt, 0);
1387 if ((idx != SUB_REAL) || !pvt->owner || pvt->pulse)
1391 /* This means that the digit was already sent via PRI signalling */
1392 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
1393 && !pvt->begindigit)
1397 if (pvt->begindigit) {
1399 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
1400 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &x);
1402 pvt->begindigit = 0;
1406 ast_mutex_unlock(&pvt->lock);
1411 static char *events[] = {
1424 "Hook Transition Complete",
1429 "Polarity Reversal",
1437 { DAHDI_ALARM_RED, "Red Alarm" },
1438 { DAHDI_ALARM_YELLOW, "Yellow Alarm" },
1439 { DAHDI_ALARM_BLUE, "Blue Alarm" },
1440 { DAHDI_ALARM_RECOVER, "Recovering" },
1441 { DAHDI_ALARM_LOOPBACK, "Loopback" },
1442 { DAHDI_ALARM_NOTOPEN, "Not Open" },
1443 { DAHDI_ALARM_NONE, "None" },
1446 static char *alarm2str(int alm)
1449 for (x = 0; x < ARRAY_LEN(alarms); x++) {
1450 if (alarms[x].alarm & alm)
1451 return alarms[x].name;
1453 return alm ? "Unknown Alarm" : "No Alarm";
1456 static char *event2str(int event)
1458 static char buf[256];
1459 if ((event < (ARRAY_LEN(events))) && (event > -1))
1460 return events[event];
1461 sprintf(buf, "Event %d", event); /* safe */
1466 static char *dialplan2str(int dialplan)
1468 if (dialplan == -1 || dialplan == -2) {
1469 return("Dynamically set dialplan in ISDN");
1471 return (pri_plan2str(dialplan));
1475 static char *dahdi_sig2str(int sig)
1477 static char buf[256];
1480 return "E & M Immediate";
1482 return "E & M Wink";
1486 return "Feature Group D (DTMF)";
1488 return "Feature Group D (MF)";
1489 case SIG_FEATDMF_TA:
1490 return "Feature Groud D (MF) Tandem Access";
1492 return "Feature Group B (MF)";
1496 return "FGC/CAMA (Dialpulse)";
1497 case SIG_FGC_CAMAMF:
1498 return "FGC/CAMA (MF)";
1500 return "FXS Loopstart";
1502 return "FXS Groundstart";
1504 return "FXS Kewlstart";
1506 return "FXO Loopstart";
1508 return "FXO Groundstart";
1510 return "FXO Kewlstart";
1514 return "ISDN BRI Point to Point";
1516 return "ISDN BRI Point to MultiPoint";
1520 return "SF (Tone) Immediate";
1522 return "SF (Tone) Wink";
1524 return "SF (Tone) with Feature Group D (DTMF)";
1525 case SIG_SF_FEATDMF:
1526 return "SF (Tone) with Feature Group D (MF)";
1528 return "SF (Tone) with Feature Group B (MF)";
1529 case SIG_GR303FXOKS:
1530 return "GR-303 with FXOKS";
1531 case SIG_GR303FXSKS:
1532 return "GR-303 with FXSKS";
1536 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
1541 #define sig2str dahdi_sig2str
1543 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx, int slavechannel)
1545 /* If the conference already exists, and we're already in it
1546 don't bother doing anything */
1547 struct dahdi_confinfo zi;
1549 memset(&zi, 0, sizeof(zi));
1552 if (slavechannel > 0) {
1553 /* If we have only one slave, do a digital mon */
1554 zi.confmode = DAHDI_CONF_DIGITALMON;
1555 zi.confno = slavechannel;
1558 /* Real-side and pseudo-side both participate in conference */
1559 zi.confmode = DAHDI_CONF_REALANDPSEUDO | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER |
1560 DAHDI_CONF_PSEUDO_TALKER | DAHDI_CONF_PSEUDO_LISTENER;
1562 zi.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
1563 zi.confno = p->confno;
1565 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
1569 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
1570 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d: %s\n", c->dfd, zi.confmode, zi.confno, strerror(errno));
1573 if (slavechannel < 1) {
1574 p->confno = zi.confno;
1576 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1577 ast_debug(1, "Added %d to conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
1581 static int isourconf(struct dahdi_pvt *p, struct dahdi_subchannel *c)
1583 /* If they're listening to our channel, they're ours */
1584 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == DAHDI_CONF_DIGITALMON))
1586 /* If they're a talker on our (allocated) conference, they're ours */
1587 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & DAHDI_CONF_TALKER))
1592 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx)
1594 struct dahdi_confinfo zi;
1595 if (/* Can't delete if there's no dfd */
1597 /* Don't delete from the conference if it's not our conference */
1599 /* Don't delete if we don't think it's conferenced at all (implied) */
1601 memset(&zi, 0, sizeof(zi));
1605 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
1606 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d: %s\n", c->dfd, c->curconf.confmode, c->curconf.confno, strerror(errno));
1609 ast_debug(1, "Removed %d from conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
1610 memcpy(&c->curconf, &zi, sizeof(c->curconf));
1614 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out)
1618 struct dahdi_pvt *slave = NULL;
1619 /* Start out optimistic */
1621 /* Update conference state in a stateless fashion */
1622 for (x = 0; x < 3; x++) {
1623 /* Any three-way calling makes slave native mode *definitely* out
1625 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway)
1628 /* If we don't have any 3-way calls, check to see if we have
1629 precisely one slave */
1630 if (useslavenative) {
1631 for (x = 0; x < MAX_SLAVES; x++) {
1634 /* Whoops already have a slave! No
1635 slave native and stop right away */
1640 /* We have one slave so far */
1641 slave = p->slaves[x];
1646 /* If no slave, slave native definitely out */
1649 else if (slave->law != p->law) {
1655 return useslavenative;
1658 static int reset_conf(struct dahdi_pvt *p)
1660 struct dahdi_confinfo zi;
1661 memset(&zi, 0, sizeof(zi));
1663 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
1664 if (p->subs[SUB_REAL].dfd > -1) {
1665 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &zi))
1666 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d: %s\n", p->channel, strerror(errno));
1671 static int update_conf(struct dahdi_pvt *p)
1676 struct dahdi_pvt *slave = NULL;
1678 useslavenative = isslavenative(p, &slave);
1679 /* Start with the obvious, general stuff */
1680 for (x = 0; x < 3; x++) {
1681 /* Look for three way calls */
1682 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway) {
1683 conf_add(p, &p->subs[x], x, 0);
1686 conf_del(p, &p->subs[x], x);
1689 /* If we have a slave, add him to our conference now. or DAX
1690 if this is slave native */
1691 for (x = 0; x < MAX_SLAVES; x++) {
1694 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
1696 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
1701 /* If we're supposed to be in there, do so now */
1702 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
1704 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
1706 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
1710 /* If we have a master, add ourselves to his conference */
1712 if (isslavenative(p->master, NULL)) {
1713 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
1715 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
1719 /* Nobody is left (or should be left) in our conference.
1723 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
1727 static void dahdi_enable_ec(struct dahdi_pvt *p)
1734 ast_debug(1, "Echo cancellation already on\n");
1738 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
1741 if (p->echocancel.head.tap_length) {
1742 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
1744 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x);
1746 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n", p->channel, strerror(errno));
1748 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &p->echocancel);
1750 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d (%s)\n", p->channel, strerror(errno));
1753 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
1756 ast_debug(1, "No echo cancellation requested\n");
1759 static void dahdi_train_ec(struct dahdi_pvt *p)
1764 if (p && p->echocanon && p->echotraining) {
1765 x = p->echotraining;
1766 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOTRAIN, &x);
1768 ast_log(LOG_WARNING, "Unable to request echo training on channel %d: %s\n", p->channel, strerror(errno));
1770 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
1772 ast_debug(1, "No echo training requested\n");
1776 static void dahdi_disable_ec(struct dahdi_pvt *p)
1781 struct dahdi_echocanparams ecp = { .tap_length = 0 };
1783 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &ecp);
1786 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d: %s\n", p->channel, strerror(errno));
1788 ast_debug(1, "Disabled echo cancellation on channel %d\n", p->channel);
1794 static void fill_txgain(struct dahdi_gains *g, float gain, int law)
1798 float linear_gain = pow(10.0, gain / 20.0);
1801 case DAHDI_LAW_ALAW:
1802 for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
1804 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1805 if (k > 32767) k = 32767;
1806 if (k < -32767) k = -32767;
1807 g->txgain[j] = AST_LIN2A(k);
1813 case DAHDI_LAW_MULAW:
1814 for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
1816 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1817 if (k > 32767) k = 32767;
1818 if (k < -32767) k = -32767;
1819 g->txgain[j] = AST_LIN2MU(k);
1828 static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
1832 float linear_gain = pow(10.0, gain / 20.0);
1835 case DAHDI_LAW_ALAW:
1836 for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
1838 k = (int) (((float) AST_ALAW(j)) * linear_gain);
1839 if (k > 32767) k = 32767;
1840 if (k < -32767) k = -32767;
1841 g->rxgain[j] = AST_LIN2A(k);
1847 case DAHDI_LAW_MULAW:
1848 for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
1850 k = (int) (((float) AST_MULAW(j)) * linear_gain);
1851 if (k > 32767) k = 32767;
1852 if (k < -32767) k = -32767;
1853 g->rxgain[j] = AST_LIN2MU(k);
1862 static int set_actual_txgain(int fd, int chan, float gain, int law)
1864 struct dahdi_gains g;
1867 memset(&g, 0, sizeof(g));
1869 res = ioctl(fd, DAHDI_GETGAINS, &g);
1871 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1875 fill_txgain(&g, gain, law);
1877 return ioctl(fd, DAHDI_SETGAINS, &g);
1880 static int set_actual_rxgain(int fd, int chan, float gain, int law)
1882 struct dahdi_gains g;
1885 memset(&g, 0, sizeof(g));
1887 res = ioctl(fd, DAHDI_GETGAINS, &g);
1889 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
1893 fill_rxgain(&g, gain, law);
1895 return ioctl(fd, DAHDI_SETGAINS, &g);
1898 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
1900 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
1903 static int bump_gains(struct dahdi_pvt *p)
1907 /* Bump receive gain by value stored in cid_rxgain */
1908 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
1910 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
1917 static int restore_gains(struct dahdi_pvt *p)
1921 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
1923 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
1930 static inline int dahdi_set_hook(int fd, int hs)
1935 res = ioctl(fd, DAHDI_HOOK, &x);
1938 if (errno == EINPROGRESS)
1940 ast_log(LOG_WARNING, "DAHDI hook failed returned %d (trying %d): %s\n", res, hs, strerror(errno));
1941 /* will expectedly fail if phone is off hook during operation, such as during a restart */
1947 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted)
1951 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
1953 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &y);
1955 ast_log(LOG_WARNING, "Unable to set audio mode on %d: %s\n", p->channel, strerror(errno));
1957 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_CONFMUTE, &x);
1959 ast_log(LOG_WARNING, "DAHDI confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
1963 static int save_conference(struct dahdi_pvt *p)
1965 struct dahdi_confinfo c;
1967 if (p->saveconf.confmode) {
1968 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
1971 p->saveconf.chan = 0;
1972 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &p->saveconf);
1974 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
1975 p->saveconf.confmode = 0;
1980 c.confmode = DAHDI_CONF_NORMAL;
1981 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &c);
1983 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
1986 ast_debug(1, "Disabled conferencing\n");
1991 * \brief Send MWI state change
1993 * \arg mailbox_full This is the mailbox associated with the FXO line that the
1994 * MWI state has changed on.
1995 * \arg thereornot This argument should simply be set to 1 or 0, to indicate
1996 * whether there are messages waiting or not.
2000 * This function does two things:
2002 * 1) It generates an internal Asterisk event notifying any other module that
2003 * cares about MWI that the state of a mailbox has changed.
2005 * 2) It runs the script specified by the mwimonitornotify option to allow
2006 * some custom handling of the state change.
2008 static void notify_message(char *mailbox_full, int thereornot)
2010 char s[sizeof(mwimonitornotify) + 80];
2011 struct ast_event *event;
2012 char *mailbox, *context;
2014 /* Strip off @default */
2015 context = mailbox = ast_strdupa(mailbox_full);
2016 strsep(&context, "@");
2017 if (ast_strlen_zero(context))
2018 context = "default";
2020 if (!(event = ast_event_new(AST_EVENT_MWI,
2021 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
2022 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
2023 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
2024 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
2025 AST_EVENT_IE_END))) {
2029 ast_event_queue_and_cache(event,
2030 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR,
2031 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR,
2034 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
2035 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
2040 static int restore_conference(struct dahdi_pvt *p)
2043 if (p->saveconf.confmode) {
2044 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &p->saveconf);
2045 p->saveconf.confmode = 0;
2047 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
2051 ast_debug(1, "Restored conferencing\n");
2055 static int send_callerid(struct dahdi_pvt *p);
2057 static int send_cwcidspill(struct dahdi_pvt *p)
2061 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
2063 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
2064 /* Make sure we account for the end */
2065 p->cidlen += READ_SIZE * 4;
2068 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
2072 static int has_voicemail(struct dahdi_pvt *p)
2075 struct ast_event *event;
2076 char *mailbox, *context;
2078 mailbox = context = ast_strdupa(p->mailbox);
2079 strsep(&context, "@");
2080 if (ast_strlen_zero(context))
2081 context = "default";
2083 event = ast_event_get_cached(AST_EVENT_MWI,
2084 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
2085 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
2086 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
2090 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
2091 ast_event_destroy(event);
2093 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
2098 static int send_callerid(struct dahdi_pvt *p)
2100 /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
2102 /* Take out of linear mode if necessary */
2103 if (p->subs[SUB_REAL].linear) {
2104 p->subs[SUB_REAL].linear = 0;
2105 dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
2107 while (p->cidpos < p->cidlen) {
2108 res = write(p->subs[SUB_REAL].dfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
2110 if (errno == EAGAIN)
2113 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
2121 ast_free(p->cidspill);
2123 if (p->callwaitcas) {
2124 /* Wait for CID/CW to expire */
2125 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
2127 restore_conference(p);
2131 static int dahdi_callwait(struct ast_channel *ast)
2133 struct dahdi_pvt *p = ast->tech_pvt;
2134 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
2136 ast_log(LOG_WARNING, "Spill already exists?!?\n");
2137 ast_free(p->cidspill);
2139 if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
2143 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
2144 if (!p->callwaitrings && p->callwaitingcallerid) {
2145 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
2147 p->cidlen = 2400 + 680 + READ_SIZE * 4;
2149 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
2151 p->cidlen = 2400 + READ_SIZE * 4;
2160 static unsigned char cid_pres2ss7pres(int cid_pres)
2162 return (cid_pres >> 5) & 0x03;
2165 static unsigned char cid_pres2ss7screen(int cid_pres)
2167 return cid_pres & 0x03;
2171 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout)
2173 struct dahdi_pvt *p = ast->tech_pvt;
2174 int x, res, idx,mysig;
2179 char dest[256]; /* must be same length as p->dialdest */
2180 ast_mutex_lock(&p->lock);
2181 ast_copy_string(dest, rdest, sizeof(dest));
2182 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
2183 if ((ast->_state == AST_STATE_BUSY)) {
2184 p->subs[SUB_REAL].needbusy = 1;
2185 ast_mutex_unlock(&p->lock);
2188 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
2189 ast_log(LOG_WARNING, "dahdi_call called on %s, neither down nor reserved\n", ast->name);
2190 ast_mutex_unlock(&p->lock);
2193 p->waitingfordt.tv_sec = 0;
2195 if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
2197 /* Special pseudo -- automatically up */
2198 ast_setstate(ast, AST_STATE_UP);
2199 ast_mutex_unlock(&p->lock);
2202 x = DAHDI_FLUSH_READ | DAHDI_FLUSH_WRITE;
2203 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
2205 ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", p->channel, strerror(errno));
2208 set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
2211 if (p->outsigmod > -1)
2212 mysig = p->outsigmod;
2218 if (p->owner == ast) {
2219 /* Normal ring, on hook */
2221 /* Don't send audio while on hook, until the call is answered */
2223 if (p->use_callerid) {
2224 /* Generate the Caller-ID spill if desired */
2226 ast_log(LOG_WARNING, "cidspill already exists??\n");
2227 ast_free(p->cidspill);
2230 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
2231 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
2236 /* Choose proper cadence */
2237 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
2238 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
2239 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
2240 p->cidrings = cidrings[p->distinctivering - 1];
2242 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
2243 ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
2244 p->cidrings = p->sendcalleridafter;
2247 /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
2248 c = strchr(dest, '/');
2251 if (c && (strlen(c) < p->stripmsd)) {
2252 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2256 p->dop.op = DAHDI_DIAL_OP_REPLACE;
2257 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
2258 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
2260 p->dop.dialstr[0] = '\0';
2263 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x) && (errno != EINPROGRESS)) {
2264 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
2265 ast_mutex_unlock(&p->lock);
2270 /* Call waiting call */
2271 p->callwaitrings = 0;
2272 if (ast->cid.cid_num)
2273 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
2275 p->callwait_num[0] = '\0';
2276 if (ast->cid.cid_name)
2277 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
2279 p->callwait_name[0] = '\0';
2280 /* Call waiting tone instead */
2281 if (dahdi_callwait(ast)) {
2282 ast_mutex_unlock(&p->lock);
2285 /* Make ring-back */
2286 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].dfd, DAHDI_TONE_RINGTONE))
2287 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
2289 n = ast->cid.cid_name;
2290 l = ast->cid.cid_num;
2292 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
2294 p->lastcid_num[0] = '\0';
2296 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
2298 p->lastcid_name[0] = '\0';
2299 ast_setstate(ast, AST_STATE_RINGING);
2300 idx = dahdi_get_index(ast, p, 0);
2302 p->subs[idx].needringing = 1;
2315 case SIG_FGC_CAMAMF:
2320 case SIG_SF_FEATDMF:
2321 case SIG_FEATDMF_TA:
2323 c = strchr(dest, '/');
2328 if (strlen(c) < p->stripmsd) {
2329 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2330 ast_mutex_unlock(&p->lock);
2334 /* Start the trunk, if not GR-303 */
2338 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
2340 if (errno != EINPROGRESS) {
2341 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
2342 ast_mutex_unlock(&p->lock);
2349 ast_debug(1, "Dialing '%s'\n", c);
2350 p->dop.op = DAHDI_DIAL_OP_REPLACE;
2356 l = ast->cid.cid_num;
2358 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
2360 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
2363 l = ast->cid.cid_num;
2365 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
2367 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
2369 case SIG_FEATDMF_TA:
2371 const char *cic, *ozz;
2373 /* If you have to go through a Tandem Access point you need to use this */
2374 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
2377 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
2381 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
2382 ast_mutex_unlock(&p->lock);
2385 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
2386 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
2391 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
2394 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
2396 case SIG_FGC_CAMAMF:
2398 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
2402 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
2404 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
2408 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
2409 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
2410 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
2411 p->echorest[sizeof(p->echorest) - 1] = '\0';
2413 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
2417 /* waitfordialtone ? */
2421 if( p->waitfordialtone && CANPROGRESSDETECT(p) && p->dsp ) {
2422 ast_log(LOG_DEBUG, "Defer dialling for %dms or dialtone\n", p->waitfordialtone);
2423 gettimeofday(&p->waitingfordt,NULL);
2424 ast_setstate(ast, AST_STATE_OFFHOOK);
2431 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
2432 int saveerr = errno;
2435 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
2436 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
2437 ast_mutex_unlock(&p->lock);
2441 ast_debug(1, "Deferring dialing...\n");
2444 if (ast_strlen_zero(c))
2446 ast_setstate(ast, AST_STATE_DIALING);
2449 /* Special pseudo -- automatically up*/
2450 ast_setstate(ast, AST_STATE_UP);
2456 /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
2457 p->dialdest[0] = '\0';
2460 ast_debug(1, "not yet implemented\n");
2461 ast_mutex_unlock(&p->lock);
2466 char ss7_called_nai;
2467 int called_nai_strip;
2468 char ss7_calling_nai;
2469 int calling_nai_strip;
2470 const char *charge_str = NULL;
2471 const char *gen_address = NULL;
2472 const char *gen_digits = NULL;
2473 const char *gen_dig_type = NULL;
2474 const char *gen_dig_scheme = NULL;
2475 const char *gen_name = NULL;
2476 const char *jip_digits = NULL;
2477 const char *lspi_ident = NULL;
2478 const char *rlt_flag = NULL;
2479 const char *call_ref_id = NULL;
2480 const char *call_ref_pc = NULL;
2481 const char *send_far = NULL;
2483 c = strchr(dest, '/');
2489 if (!p->hidecallerid) {
2490 l = ast->cid.cid_num;
2495 if (ss7_grab(p, p->ss7)) {
2496 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
2497 ast_mutex_unlock(&p->lock);
2500 p->digital = IS_DIGITAL(ast->transfercapability);
2501 p->ss7call = isup_new_call(p->ss7->ss7);
2505 ast_mutex_unlock(&p->lock);
2506 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
2510 called_nai_strip = 0;
2511 ss7_called_nai = p->ss7->called_nai;
2512 if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
2513 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2514 called_nai_strip = strlen(p->ss7->internationalprefix);
2515 ss7_called_nai = SS7_NAI_INTERNATIONAL;
2516 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2517 called_nai_strip = strlen(p->ss7->nationalprefix);
2518 ss7_called_nai = SS7_NAI_NATIONAL;
2520 ss7_called_nai = SS7_NAI_SUBSCRIBER;
2523 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
2525 calling_nai_strip = 0;
2526 ss7_calling_nai = p->ss7->calling_nai;
2527 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
2528 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
2529 calling_nai_strip = strlen(p->ss7->internationalprefix);
2530 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
2531 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
2532 calling_nai_strip = strlen(p->ss7->nationalprefix);
2533 ss7_calling_nai = SS7_NAI_NATIONAL;
2535 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
2538 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
2539 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
2540 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
2542 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
2543 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
2545 ast_channel_lock(ast);
2546 /* Set the charge number if it is set */
2547 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
2549 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
2551 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
2553 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 */
2555 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
2556 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
2557 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
2559 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
2561 gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
2563 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
2565 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
2567 isup_set_jip_digits(p->ss7call, jip_digits);
2569 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
2571 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
2573 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
2574 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
2575 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
2578 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
2579 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
2580 if (call_ref_id && call_ref_pc) {
2581 isup_set_callref(p->ss7call, atoi(call_ref_id),
2582 call_ref_pc ? atoi(call_ref_pc) : 0);
2585 send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
2586 if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
2587 (isup_far(p->ss7->ss7, p->ss7call));
2589 ast_channel_unlock(ast);
2591 isup_iam(p->ss7->ss7, p->ss7call);
2592 ast_setstate(ast, AST_STATE_DIALING);
2595 #endif /* HAVE_SS7 */
2599 #ifdef SUPPORT_USERUSER
2600 const char *useruser;
2604 int prilocaldialplan;
2608 int redirect_reason;
2610 c = strchr(dest, '/');
2619 if (!p->hidecallerid) {
2620 l = ast->cid.cid_num;
2621 if (!p->hidecalleridname) {
2622 n = ast->cid.cid_name;
2626 if (strlen(c) < p->stripmsd) {
2627 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
2628 ast_mutex_unlock(&p->lock);
2631 if (mysig != SIG_FXSKS) {
2632 p->dop.op = DAHDI_DIAL_OP_REPLACE;
2633 s = strchr(c + p->stripmsd, 'w');
2636 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
2638 p->dop.dialstr[0] = '\0';
2641 p->dop.dialstr[0] = '\0';
2644 if (pri_grab(p, p->pri)) {
2645 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
2646 ast_mutex_unlock(&p->lock);
2649 if (!(p->call = pri_new_call(p->pri->pri))) {
2650 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
2652 ast_mutex_unlock(&p->lock);
2655 if (!(sr = pri_sr_new())) {
2656 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
2658 ast_mutex_unlock(&p->lock);
2660 if (p->bearer || (mysig == SIG_FXSKS)) {
2662 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
2663 p->bearer->call = p->call;
2665 ast_debug(1, "I'm being setup with no bearer right now...\n");
2667 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
2669 p->digital = IS_DIGITAL(ast->transfercapability);
2670 /* Add support for exclusive override */
2671 if (p->priexclusive)
2674 /* otherwise, traditional behavior */
2675 if (p->pri->nodetype == PRI_NETWORK)
2681 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
2682 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
2684 ((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
2685 if (p->pri->facilityenable)
2686 pri_facility_enable(p->pri->pri);
2688 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
2691 pridialplan = p->pri->dialplan - 1;
2692 if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
2693 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2694 if (pridialplan == -2) {
2695 dp_strip = strlen(p->pri->internationalprefix);
2697 pridialplan = PRI_INTERNATIONAL_ISDN;
2698 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2699 if (pridialplan == -2) {
2700 dp_strip = strlen(p->pri->nationalprefix);
2702 pridialplan = PRI_NATIONAL_ISDN;
2704 pridialplan = PRI_LOCAL_ISDN;
2707 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
2708 switch (c[p->stripmsd]) {
2710 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
2713 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
2716 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
2719 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
2722 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
2725 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
2728 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
2731 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
2734 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
2737 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
2740 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
2743 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
2746 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
2749 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
2753 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2757 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
2760 prilocaldialplan = p->pri->localdialplan - 1;
2761 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
2762 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
2763 if (prilocaldialplan == -2) {
2764 ldp_strip = strlen(p->pri->internationalprefix);
2766 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
2767 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
2768 if (prilocaldialplan == -2) {
2769 ldp_strip = strlen(p->pri->nationalprefix);
2771 prilocaldialplan = PRI_NATIONAL_ISDN;
2773 prilocaldialplan = PRI_LOCAL_ISDN;
2777 while (*l > '9' && *l != '*' && *l != '#') {
2780 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
2783 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
2786 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
2789 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
2792 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
2795 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
2798 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
2801 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
2804 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
2807 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
2810 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
2813 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
2816 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
2819 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
2823 ast_log(LOG_WARNING, "Unrecognized prilocaldialplan %s modifier: %c\n", *c > 'Z' ? "NPI" : "TON", *c);
2828 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
2829 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
2830 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
2831 if (!strcasecmp(rr_str, "UNKNOWN"))
2832 redirect_reason = 0;
2833 else if (!strcasecmp(rr_str, "BUSY"))
2834 redirect_reason = 1;
2835 else if (!strcasecmp(rr_str, "NO_REPLY"))
2836 redirect_reason = 2;
2837 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
2838 redirect_reason = 15;
2840 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2842 redirect_reason = PRI_REDIR_UNCONDITIONAL;
2843 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
2845 #ifdef SUPPORT_USERUSER
2846 /* User-user info */
2847 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
2849 pri_sr_set_useruser(sr, useruser);
2852 if (pri_setup(p->pri->pri, p->call, sr)) {
2853 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
2854 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
2856 ast_mutex_unlock(&p->lock);
2861 ast_setstate(ast, AST_STATE_DIALING);
2865 ast_mutex_unlock(&p->lock);
2869 static void destroy_dahdi_pvt(struct dahdi_pvt **pvt)
2871 struct dahdi_pvt *p = *pvt;
2872 /* Remove channel from the list */
2874 p->prev->next = p->next;
2876 p->next->prev = p->prev;
2878 ast_smdi_interface_unref(p->smdi_iface);
2879 if (p->mwi_event_sub)
2880 ast_event_unsubscribe(p->mwi_event_sub);
2882 ast_variables_destroy(p->vars);
2883 ast_mutex_destroy(&p->lock);
2884 dahdi_close_sub(p, SUB_REAL);
2886 p->owner->tech_pvt = NULL;
2891 static int destroy_channel(struct dahdi_pvt *prev, struct dahdi_pvt *cur, int now)
2901 for (i = 0; i < 3; i++) {
2902 if (cur->subs[i].owner) {
2908 prev->next = cur->next;
2910 prev->next->prev = prev;
2916 iflist->prev = NULL;
2920 destroy_dahdi_pvt(&cur);
2924 prev->next = cur->next;
2926 prev->next->prev = prev;
2932 iflist->prev = NULL;
2936 destroy_dahdi_pvt(&cur);
2941 static void destroy_all_channels(void)
2944 struct dahdi_pvt *p, *pl;
2946 while (num_restart_pending) {
2950 ast_mutex_lock(&iflock);
2951 /* Destroy all the interfaces and free their memory */
2954 /* Free any callerid */
2956 ast_free(p->cidspill);
2960 /* Free associated memory */
2962 destroy_dahdi_pvt(&pl);
2963 if (option_verbose > 2)
2964 ast_verbose(VERBOSE_PREFIX_2 "Unregistered channel %d\n", x);
2968 ast_mutex_unlock(&iflock);
2972 static char *dahdi_send_keypad_facility_app = "DAHDISendKeypadFacility";
2974 static int dahdi_send_keypad_facility_exec(struct ast_channel *chan, void *data)
2976 /* Data will be our digit string */
2977 struct dahdi_pvt *p;
2978 char *digits = (char *) data;
2980 if (ast_strlen_zero(digits)) {
2981 ast_debug(1, "No digit string sent to application!\n");
2985 p = (struct dahdi_pvt *)chan->tech_pvt;
2988 ast_debug(1, "Unable to find technology private\n");
2992 ast_mutex_lock(&p->lock);
2994 if (!p->pri || !p->call) {
2995 ast_debug(1, "Unable to find pri or call on channel!\n");
2996 ast_mutex_unlock(&p->lock);
3000 if (!pri_grab(p, p->pri)) {
3001 pri_keypad_facility(p->pri->pri, p->call, digits);
3004 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
3005 ast_mutex_unlock(&p->lock);
3009 ast_mutex_unlock(&p->lock);
3014 #ifdef HAVE_PRI_PROG_W_CAUSE
3016 static char *dahdi_send_callrerouting_facility_app = "DAHDISendCallreroutingFacility";
3018 static int dahdi_send_callrerouting_facility_exec(struct ast_channel *chan, void *data)
3020 /* Data will be our digit string */
3021 struct dahdi_pvt *p;
3024 AST_DECLARE_APP_ARGS(args,
3025 AST_APP_ARG(destination);
3026 AST_APP_ARG(original);
3027 AST_APP_ARG(reason);
3030 if (ast_strlen_zero(data)) {
3031 ast_log(LOG_DEBUG, "No data sent to application!\n");
3035 p = (struct dahdi_pvt *)chan->tech_pvt;
3038 ast_log(LOG_DEBUG, "Unable to find technology private\n");
3042 parse = ast_strdupa(data);
3043 AST_STANDARD_APP_ARGS(args, parse);
3045 if (ast_strlen_zero(args.destination)) {
3046 ast_log(LOG_WARNING, "callrerouting facility requires at least destination number argument\n");
3050 if (ast_strlen_zero(args.original)) {
3051 ast_log(LOG_WARNING, "Callrerouting Facility without original called number argument\n");
3052 args.original = NULL;
3055 if (ast_strlen_zero(args.reason)) {
3056 ast_log(LOG_NOTICE, "Callrerouting Facility without diversion reason argument, defaulting to unknown\n");
3060 ast_mutex_lock(&p->lock);
3062 if (!p->pri || !p->call) {
3063 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
3064 ast_mutex_unlock(&p->lock);
3070 if (!pri_grab(p, p->pri)) {
3071 if (chan->_state == AST_STATE_RING) {
3072 res = pri_callrerouting_facility(p->pri->pri, p->call, args.destination, args.original, args.reason);
3076 ast_log(LOG_DEBUG, "Unable to grab pri to send callrerouting facility on span %d!\n", p->span);
3077 ast_mutex_unlock(&p->lock);
3083 ast_mutex_unlock(&p->lock);
3090 static int pri_is_up(struct dahdi_pri *pri)
3093 for (x = 0; x < NUM_DCHANS; x++) {
3094 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
3100 static int pri_assign_bearer(struct dahdi_pvt *crv, struct dahdi_pri *pri, struct dahdi_pvt *bearer)
3102 bearer->owner = &inuse;
3103 bearer->realcall = crv;
3104 crv->subs[SUB_REAL].dfd = bearer->subs[SUB_REAL].dfd;
3105 if (crv->subs[SUB_REAL].owner)
3106 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].dfd);
3107 crv->bearer = bearer;
3108 crv->call = bearer->call;
3113 static char *pri_order(int level)
3123 return "Quaternary";
3129 /* Returns fd of the active dchan */
3130 static int pri_active_dchan_fd(struct dahdi_pri *pri)
3134 for (x = 0; x < NUM_DCHANS; x++) {
3135 if ((pri->dchans[x] == pri->pri))
3142 static int pri_find_dchan(struct dahdi_pri *pri)
3149 for (x = 0; x < NUM_DCHANS; x++) {
3150 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
3152 if (pri->dchans[x] == old) {
3158 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
3159 pri->dchannels[newslot]);
3161 if (old && (oldslot != newslot))
3162 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
3163 pri->dchannels[oldslot], pri->dchannels[newslot]);
3164 pri->pri = pri->dchans[newslot];
3169 static int dahdi_hangup(struct ast_channel *ast)
3173 /*static int restore_gains(struct dahdi_pvt *p);*/
3174 struct dahdi_pvt *p = ast->tech_pvt;
3175 struct dahdi_pvt *tmp = NULL;
3176 struct dahdi_pvt *prev = NULL;
3177 struct dahdi_params par;
3179 ast_debug(1, "dahdi_hangup(%s)\n", ast->name);
3180 if (!ast->tech_pvt) {
3181 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
3185 ast_mutex_lock(&p->lock);
3187 idx = dahdi_get_index(ast, p, 1);
3189 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
3191 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
3195 dahdi_confmute(p, 0);
3198 if (p->origcid_num) {
3199 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
3200 ast_free(p->origcid_num);
3201 p->origcid_num = NULL;
3203 if (p->origcid_name) {
3204 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
3205 ast_free(p->origcid_name);
3206 p->origcid_name = NULL;
3209 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
3213 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
3214 p->channel, idx, p->subs[SUB_REAL].dfd, p->subs[SUB_CALLWAIT].dfd, p->subs[SUB_THREEWAY].dfd);
3218 /* Real channel, do some fixup */
3219 p->subs[idx].owner = NULL;
3220 p->subs[idx].needanswer = 0;
3221 p->subs[idx].needflash = 0;
3222 p->subs[idx].needringing = 0;
3223 p->subs[idx].needbusy = 0;
3224 p->subs[idx].needcongestion = 0;
3225 p->subs[idx].linear = 0;
3226 p->subs[idx].needcallerid = 0;
3227 p->polarity = POLARITY_IDLE;
3228 dahdi_setlinear(p->subs[idx].dfd, 0);
3229 if (idx == SUB_REAL) {
3230 if ((p->subs[SUB_CALLWAIT].dfd > -1) && (p->subs[SUB_THREEWAY].dfd > -1)) {
3231 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
3232 if (p->subs[SUB_CALLWAIT].inthreeway) {
3233 /* We had flipped over to answer a callwait and now it's gone */
3234 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
3235 /* Move to the call-wait, but un-own us until they flip back. */
3236 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3237 unalloc_sub(p, SUB_CALLWAIT);
3240 /* The three way hung up, but we still have a call wait */
3241 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
3242 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3243 unalloc_sub(p, SUB_THREEWAY);
3244 if (p->subs[SUB_REAL].inthreeway) {
3245 /* This was part of a three way call. Immediately make way for
3247 ast_debug(1, "Call was complete, setting owner to former third call\n");
3248 p->owner = p->subs[SUB_REAL].owner;
3250 /* This call hasn't been completed yet... Set owner to NULL */
3251 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
3254 p->subs[SUB_REAL].inthreeway = 0;
3256 } else if (p->subs[SUB_CALLWAIT].dfd > -1) {
3257 /* Move to the call-wait and switch back to them. */
3258 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
3259 unalloc_sub(p, SUB_CALLWAIT);
3260 p->owner = p->subs[SUB_REAL].owner;
3261 if (p->owner->_state != AST_STATE_UP)
3262 p->subs[SUB_REAL].needanswer = 1;
3263 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
3264 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
3265 } else if (p->subs[SUB_THREEWAY].dfd > -1) {
3266 swap_subs(p, SUB_THREEWAY, SUB_REAL);
3267 unalloc_sub(p, SUB_THREEWAY);
3268 if (p->subs[SUB_REAL].inthreeway) {
3269 /* This was part of a three way call. Immediately make way for
3271 ast_debug(1, "Call was complete, setting owner to former third call\n");
3272 p->owner = p->subs[SUB_REAL].owner;
3274 /* This call hasn't been completed yet... Set owner to NULL */
3275 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
3278 p->subs[SUB_REAL].inthreeway = 0;
3280 } else if (idx == SUB_CALLWAIT) {
3281 /* Ditch the holding callwait call, and immediately make it availabe */
3282 if (p->subs[SUB_CALLWAIT].inthreeway) {
3283 /* This is actually part of a three way, placed on hold. Place the third part
3284 on music on hold now */
3285 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
3286 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
3287 S_OR(p->mohsuggest, NULL),
3288 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3290 p->subs[SUB_THREEWAY].inthreeway = 0;
3291 /* Make it the call wait now */
3292 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
3293 unalloc_sub(p, SUB_THREEWAY);
3295 unalloc_sub(p, SUB_CALLWAIT);
3296 } else if (idx == SUB_THREEWAY) {
3297 if (p->subs[SUB_CALLWAIT].inthreeway) {
3298 /* The other party of the three way call is currently in a call-wait state.
3299 Start music on hold for them, and take the main guy out of the third call */
3300 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
3301 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
3302 S_OR(p->mohsuggest, NULL),
3303 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
3305 p->subs[SUB_CALLWAIT].inthreeway = 0;
3307 p->subs[SUB_REAL].inthreeway = 0;
3308 /* If this was part of a three way call index, let us make
3309 another three way call */
3310 unalloc_sub(p, SUB_THREEWAY);
3312 /* This wasn't any sort of call, but how are we an index? */
3313 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
3317 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
3320 p->distinctivering = 0;
3321 p->confirmanswer = 0;
3327 p->onhooktime = time(NULL);
3328 #if defined(HAVE_PRI) || defined(HAVE_SS7)
3336 ast_dsp_free(p->dsp);
3340 law = DAHDI_LAW_DEFAULT;
3341 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETLAW, &law);
3343 ast_log(LOG_WARNING, "Unable to set law on channel %d to default: %s\n", p->channel, strerror(errno));
3344 /* Perform low level hangup if no owner left */
3348 if (!ss7_grab(p, p->ss7)) {
3349 if (!p->alreadyhungup) {
3350 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
3351 int icause = ast->hangupcause ? ast->hangupcause : -1;
3355 icause = atoi(cause);
3357 isup_rel(p->ss7->ss7, p->ss7call, icause);
3359 p->alreadyhungup = 1;
3361 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
3363 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
3371 #ifdef SUPPORT_USERUSER
3372 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
3375 /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
3376 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
3377 if (!pri_grab(p, p->pri)) {
3378 if (p->alreadyhungup) {
3379 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
3381 #ifdef SUPPORT_USERUSER
3382 pri_call_set_useruser(p->call, useruser);
3385 pri_hangup(p->pri->pri, p->call, -1);
3388 p->bearer->call = NULL;
3390 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
3391 int icause = ast->hangupcause ? ast->hangupcause : -1;
3392 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
3394 #ifdef SUPPORT_USERUSER
3395 pri_call_set_useruser(p->call, useruser);
3398 p->alreadyhungup = 1;
3400 p->bearer->alreadyhungup = 1;
3403 icause = atoi(cause);
3405 pri_hangup(p->pri->pri, p->call, icause);
3408 ast_log(LOG_WARNING, "pri_disconnect failed\n");
3411 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
3416 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
3422 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7) && (p->sig != SIG_BRI) && (p->sig != SIG_BRI_PTMP)))
3423 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
3425 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
3431 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
3434 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
3436 /* If they're off hook, try playing congestion */
3437 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
3438 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
3440 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
3441 p->fxsoffhookstate = par.rxisoffhook;