Skip to content
Snippets Groups Projects
chan_dahdi.c 588 KiB
Newer Older
  • Learn to ignore specific revisions
  • Mark Spencer's avatar
    Mark Spencer committed
    /*
    
     * Asterisk -- An open source telephony toolkit.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Copyright (C) 1999 - 2006, Digium, Inc.
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * Mark Spencer <markster@digium.com>
    
    Mark Spencer's avatar
    Mark Spencer committed
     *
    
     * See http://www.asterisk.org for more information about
     * the Asterisk project. Please do not directly contact
     * any of the maintainers of this project for assistance;
     * the project provides a web site, mailing lists and IRC
     * channels for your use.
     *
    
    Mark Spencer's avatar
    Mark Spencer committed
     * This program is free software, distributed under the terms of
    
     * the GNU General Public License Version 2. See the LICENSE file
     * at the top of the source tree.
     */
    
    
     *
     * \author Mark Spencer <markster@digium.com>
    
    Richard Mudgett's avatar
    Richard Mudgett committed
     *
     * Connects to the DAHDI telephony library as well as
    
    Russell Bryant's avatar
    Russell Bryant committed
     * libpri. Libpri is optional and needed only if you are
     * going to use ISDN connections.
     *
     * You need to install libraries before you attempt to compile
    
    Russell Bryant's avatar
    Russell Bryant committed
     *
     * \par See also
    
    Russell Bryant's avatar
    Russell Bryant committed
     *
     * \ingroup channel_drivers
    
     * \todo Deprecate the "musiconhold" configuration option post 1.4
    
    	<depend>res_smdi</depend>
    
    	<use>openr2</use>
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #ifdef __NetBSD__
    #include <pthread.h>
    #include <signal.h>
    #else
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include <sys/signal.h>
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include <sys/ioctl.h>
    #include <math.h>
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include <ctype.h>
    
    
    #include <dahdi/user.h>
    #include <dahdi/tonezone.h>
    
    Mark Spencer's avatar
    Mark Spencer committed
    #include <libpri.h>
    #endif
    
    
    #ifdef HAVE_SS7
    #include <libss7.h>
    #endif
    
    
    #ifdef HAVE_OPENR2
    #include <openr2.h>
    #endif
    
    
    #include "asterisk/lock.h"
    #include "asterisk/channel.h"
    #include "asterisk/config.h"
    #include "asterisk/module.h"
    #include "asterisk/pbx.h"
    #include "asterisk/file.h"
    #include "asterisk/ulaw.h"
    #include "asterisk/alaw.h"
    #include "asterisk/callerid.h"
    #include "asterisk/adsi.h"
    #include "asterisk/cli.h"
    #include "asterisk/cdr.h"
    #include "asterisk/features.h"
    #include "asterisk/musiconhold.h"
    #include "asterisk/say.h"
    #include "asterisk/tdd.h"
    #include "asterisk/app.h"
    #include "asterisk/dsp.h"
    #include "asterisk/astdb.h"
    #include "asterisk/manager.h"
    #include "asterisk/causes.h"
    #include "asterisk/term.h"
    #include "asterisk/utils.h"
    #include "asterisk/transcap.h"
    
    #include "asterisk/stringfields.h"
    
    #include "asterisk/smdi.h"
    #include "asterisk/astobj.h"
    
    #include "asterisk/event.h"
    
    Jeff Peeler's avatar
    Jeff Peeler committed
    #include "asterisk/devicestate.h"
    
    #include "asterisk/paths.h"
    
    /*** DOCUMENTATION
    	<application name="DAHDISendKeypadFacility" language="en_US">
    		<synopsis>
    			Send digits out of band over a PRI.
    		</synopsis>
    		<syntax>
    			<parameter name="digits" required="true" />
    		</syntax>
    		<description>
    			<para>This application will send the given string of digits in a Keypad
    			Facility IE over the current channel.</para>
    		</description>
    	</application>
    	<application name="DAHDISendCallreroutingFacility" language="en_US">
    		<synopsis>
    			Send QSIG call rerouting facility over a PRI.
    		</synopsis>
    
    			<parameter name="destination" required="true">
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    				<para>Destination number.</para>
    
    			</parameter>
    			<parameter name="original">
    				<para>Original called number.</para>
    			</parameter>
    			<parameter name="reason">
    				<para>Diversion reason, if not specified defaults to <literal>unknown</literal></para>
    			</parameter>
    		</syntax>
    		<description>
    			<para>This application will send a Callrerouting Facility IE over the
    			current channel.</para>
    		</description>
    	</application>
    
    	<application name="DAHDIAcceptR2Call" language="en_US">
    		<synopsis>
    			Accept an R2 call if its not already accepted (you still need to answer it)
    		</synopsis>
    		<syntax>
    			<parameter name="charge" required="true">
    				<para>Yes or No.</para>
    				<para>Whether you want to accept the call with charge or without charge.</para>
    			</parameter>
    		</syntax>
    		<description>
    			<para>This application will Accept the R2 call either with charge or no charge.</para>
    		</description>
    	</application>
    
    	<manager name="DAHDITransfer" language="en_US">
    		<synopsis>
    			Transfer DAHDI Channel.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="DAHDIChannel" required="true">
    				<para>DAHDI channel name to transfer.</para>
    			</parameter>
    		</syntax>
    		<description>
    			<para>Transfer a DAHDI channel.</para>
    		</description>
    	</manager>
    	<manager name="DAHDIHangup" language="en_US">
    		<synopsis>
    			Hangup DAHDI Channel.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="DAHDIChannel" required="true">
    				<para>DAHDI channel name to hangup.</para>
    			</parameter>
    		</syntax>
    		<description>
    			<para>Hangup a DAHDI channel.</para>
    		</description>
    	</manager>
    	<manager name="DAHDIDialOffhook" language="en_US">
    		<synopsis>
    			Dial over DAHDI channel while offhook.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="DAHDIChannel" required="true" />
    			<parameter name="Number" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="DAHDIDNDon" language="en_US">
    		<synopsis>
    			Toggle DAHDI channel Do Not Disturb status ON.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="DAHDIChannel" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="DAHDIDNDoff" language="en_US">
    		<synopsis>
    			Toggle DAHDI channel Do Not Disturb status OFF.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="DAHDIChannel" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="DAHDIShowChannels" language="en_US">
    		<synopsis>
    			Show status DAHDI channels.
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    			<parameter name="DAHDIChannel" required="true" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    	<manager name="DAHDIRestart" language="en_US">
    		<synopsis>
    			Fully Restart DAHDI channels (terminates calls).
    		</synopsis>
    		<syntax>
    			<xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
    		</syntax>
    		<description>
    		</description>
    	</manager>
    
    #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
    
    static const char * const lbostr[] = {
    
    "0 db (CSU)/0-133 feet (DSX-1)",
    "133-266 feet (DSX-1)",
    "266-399 feet (DSX-1)",
    "399-533 feet (DSX-1)",
    "533-655 feet (DSX-1)",
    "-7.5db (CSU)",
    "-15db (CSU)",
    "-22.5db (CSU)"
    };
    
    
    /*! Global jitterbuffer configuration - by default, jb is disabled */
    
    static struct ast_jb_conf default_jbconf =
    {
    	.flags = 0,
    	.max_size = -1,
    	.resync_threshold = -1,
    	.impl = ""
    };
    static struct ast_jb_conf global_jbconf;
    
    
    /* define this to send PRI user-user information elements */
    #undef SUPPORT_USERUSER
    
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    /*!
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
    
    Mark Spencer's avatar
    Mark Spencer committed
     * the user hangs up to reset the state machine so ring works properly.
     * This is used to be able to support kewlstart by putting the zhone in
     * groundstart mode since their forward disconnect supervision is entirely
     * broken even though their documentation says it isn't and their support
     * is entirely unwilling to provide any assistance with their channel banks
     * even though their web site says they support their products for life.
     */
    
    Mark Spencer's avatar
    Mark Spencer committed
    /* #define ZHONE_HACK */
    
    Jeff Peeler's avatar
    Jeff Peeler committed
    /*! \note
     * Define if you want to check the hook state for an FXO (FXS signalled) interface
     * before dialing on it.  Certain FXO interfaces always think they're out of
     * service with this method however.
     */
    /* #define DAHDI_CHECK_HOOKSTATE */
    
    
    Russell Bryant's avatar
    Russell Bryant committed
    /*! \brief Typically, how many rings before we should send Caller*ID */
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define DEFAULT_CIDRINGS 1
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define CHANNEL_PSEUDO -12
    
    
    #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
    
    Russell Bryant's avatar
    Russell Bryant committed
    /*! \brief Signaling types that need to use MF detection should be placed in this macro */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    #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))
    
    static const char tdesc[] = "DAHDI Telephony Driver"
    
    #if defined(HAVE_PRI) || defined(HAVE_SS7) || defined(HAVE_OPENR2)
    	" w/"
    #endif
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    	#ifdef HAVE_PRI
    	" & SS7"
    	#else
    	"SS7"
    	#endif
    #endif
    #ifdef HAVE_OPENR2
    	#if defined(HAVE_PRI) || defined(HAVE_SS7)
    	" & MFC/R2"
    	#else
    	"MFC/R2"
    	#endif
    
    static const char config[] = "chan_dahdi.conf";
    
    #define SIG_EM		DAHDI_SIG_EM
    #define SIG_EMWINK 	(0x0100000 | DAHDI_SIG_EM)
    #define SIG_FEATD	(0x0200000 | DAHDI_SIG_EM)
    #define	SIG_FEATDMF	(0x0400000 | DAHDI_SIG_EM)
    #define	SIG_FEATB	(0x0800000 | DAHDI_SIG_EM)
    #define	SIG_E911	(0x1000000 | DAHDI_SIG_EM)
    #define	SIG_FEATDMF_TA	(0x2000000 | DAHDI_SIG_EM)
    #define	SIG_FGC_CAMA	(0x4000000 | DAHDI_SIG_EM)
    #define	SIG_FGC_CAMAMF	(0x8000000 | DAHDI_SIG_EM)
    #define SIG_FXSLS	DAHDI_SIG_FXSLS
    #define SIG_FXSGS	DAHDI_SIG_FXSGS
    #define SIG_FXSKS	DAHDI_SIG_FXSKS
    #define SIG_FXOLS	DAHDI_SIG_FXOLS
    #define SIG_FXOGS	DAHDI_SIG_FXOGS
    #define SIG_FXOKS	DAHDI_SIG_FXOKS
    #define SIG_PRI		DAHDI_SIG_CLEAR
    #define SIG_BRI		(0x2000000 | DAHDI_SIG_CLEAR)
    #define SIG_BRI_PTMP	(0X4000000 | DAHDI_SIG_CLEAR)
    #define SIG_SS7		(0x1000000 | DAHDI_SIG_CLEAR)
    
    #define SIG_MFCR2 	DAHDI_SIG_CAS
    
    #define	SIG_SF		DAHDI_SIG_SF
    #define SIG_SFWINK 	(0x0100000 | DAHDI_SIG_SF)
    #define SIG_SF_FEATD	(0x0200000 | DAHDI_SIG_SF)
    #define	SIG_SF_FEATDMF	(0x0400000 | DAHDI_SIG_SF)
    #define	SIG_SF_FEATB	(0x0800000 | DAHDI_SIG_SF)
    #define SIG_EM_E1	DAHDI_SIG_EM_E1
    #define SIG_GR303FXOKS	(0x0100000 | DAHDI_SIG_FXOKS)
    #define SIG_GR303FXSKS	(0x0100000 | DAHDI_SIG_FXSKS)
    
    Russell Bryant's avatar
    Russell Bryant committed
    #define NUM_DCHANS		4	/*!< No more than 4 d-channels */
    #define MAX_CHANNELS	672		/*!< No more than a DS3 per trunk group */
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    #define CHAN_PSEUDO	-2
    
    #define DCHAN_PROVISIONED (1 << 0)
    #define DCHAN_NOTINALARM  (1 << 1)
    #define DCHAN_UP          (1 << 2)
    
    #define DCHAN_AVAILABLE	(DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
    
    
    /* Overlap dialing option types */
    
    #define DAHDI_OVERLAPDIAL_NONE 0
    #define DAHDI_OVERLAPDIAL_OUTGOING 1
    #define DAHDI_OVERLAPDIAL_INCOMING 2
    #define DAHDI_OVERLAPDIAL_BOTH (DAHDI_OVERLAPDIAL_INCOMING|DAHDI_OVERLAPDIAL_OUTGOING)
    
    #define CALLPROGRESS_PROGRESS		1
    #define CALLPROGRESS_FAX_OUTGOING	2
    #define CALLPROGRESS_FAX_INCOMING	4
    #define CALLPROGRESS_FAX		(CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING)
    
    
    #ifdef HAVE_PRI_SERVICE_MESSAGES
    /*! \brief Persistent Service State */
    #define SRVST_DBKEY "service-state"
    /*! \brief The out-of-service SERVICE state */
    #define SRVST_TYPE_OOS "O"
    /*! \brief SRVST_INITIALIZED is used to indicate a channel being out-of-service 
     *  The SRVST_INITIALIZED is mostly used maintain backwards compatibility but also may
     *  mean that the channel has not yet received a RESTART message.  If a channel is
     *  out-of-service with this reason a RESTART message will result in the channel
     *  being put into service. */
    #define SRVST_INITIALIZED 0
    /*! \brief SRVST_NEAREND is used to indicate that the near end was put out-of-service */
    #define SRVST_NEAREND  (1 << 0)
    /*! \brief SRVST_FAREND is used to indicate that the far end was taken out-of-service */
    #define SRVST_FAREND   (1 << 1)
    /*! \brief SRVST_BOTH is used to indicate that both sides of the channel are out-of-service */
    #define SRVST_BOTH (SRVST_NEAREND | SRVST_FAREND)
    
    /*! \brief The AstDB family */
    static const char dahdi_db[] = "dahdi/registry";
    #endif
    
    
    static char defaultcic[64] = "";
    static char defaultozz[64] = "";
    
    Jeff Peeler's avatar
    Jeff Peeler committed
    static char parkinglot[AST_MAX_EXTENSION] = "";		/*!< Default parking lot for this channel */
    
    
    /*! Run this script when the MWI state changes on an FXO line, if mwimonitor is enabled */
    static char mwimonitornotify[PATH_MAX] = "";
    
    #ifndef HAVE_DAHDI_LINEREVERSE_VMWI
    
    static char progzone[10] = "";
    
    static int usedistinctiveringdetection = 0;
    
    static int distinctiveringaftercid = 0;
    
    static int numbufs = 4;
    
    
    static struct ast_channel inuse;
    
    static int pritimers[PRI_MAX_TIMERS];
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    static int pridebugfd = -1;
    
    static char pridebugfilename[1024] = "";
    
    Russell Bryant's avatar
    Russell Bryant committed
    /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int firstdigittimeout = 16000;
    
    
    Russell Bryant's avatar
    Russell Bryant committed
    /*! \brief How long to wait for following digits (FXO logic) */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int gendigittimeout = 8000;
    
    
    Russell Bryant's avatar
    Russell Bryant committed
    /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
    
    Mark Spencer's avatar
    Mark Spencer committed
    static int matchdigittimeout = 3000;
    
    
    /*! \brief Protect the interface list (of dahdi_pvt's) */
    
    AST_MUTEX_DEFINE_STATIC(iflock);
    
    #define DAHDI_CHAN_MAPPING_PHYSICAL	0
    #define DAHDI_CHAN_MAPPING_LOGICAL	1
    
    static int ifcount = 0;
    
    
    Russell Bryant's avatar
    Russell Bryant committed
    /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
    
    Mark Spencer's avatar
    Mark Spencer committed
       when it's doing something critical. */
    
    AST_MUTEX_DEFINE_STATIC(monlock);
    
    Russell Bryant's avatar
    Russell Bryant committed
    /*! \brief This is the thread for the monitor which checks for input on the channels
    
       which are not currently in use. */
    
    static pthread_t monitor_thread = AST_PTHREADT_NULL;
    
    static ast_cond_t ss_thread_complete;
    AST_MUTEX_DEFINE_STATIC(ss_thread_lock);
    AST_MUTEX_DEFINE_STATIC(restart_lock);
    static int ss_thread_count = 0;
    static int num_restart_pending = 0;
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    static int restart_monitor(void);
    
    
    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);
    
    static int dahdi_sendtext(struct ast_channel *c, const char *text);
    
    static int analog_lib_handles(int signalling, int radio, int oprmode);
    
    
    static void mwi_event_cb(const struct ast_event *event, void *userdata)
    {
    	/* This module does not handle MWI in an event-based manner.  However, it
    	 * subscribes to MWI for each mailbox that is configured so that the core
    
    	 * knows that we care about it.  Then, chan_dahdi will get the MWI from the
    
    	 * event cache instead of checking the mailbox directly. */
    }
    
    
    /*! \brief Avoid the silly dahdi_getevent which ignores a bunch of events */
    static inline int dahdi_get_event(int fd)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    	int j;
    
    	if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return j;
    }
    
    
    /*! \brief Avoid the silly dahdi_waitevent which ignores a bunch of events */
    static inline int dahdi_wait_event(int fd)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    	i = DAHDI_IOMUX_SIGEVENT;
    	if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
    
    	if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
    
    Mark Spencer's avatar
    Mark Spencer committed
    	return j;
    }
    
    
    /*! Chunk size to read -- we use 20ms chunks to make things happy. */
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define READ_SIZE 160
    
    Russell Bryant's avatar
    Russell Bryant committed
    #define MASK_AVAIL		(1 << 0)	/*!< Channel available for PRI use */
    #define MASK_INUSE		(1 << 1)	/*!< Channel currently in use */
    
    Russell Bryant's avatar
    Russell Bryant committed
    #define CALLWAITING_SILENT_SAMPLES	( (300 * 8) / READ_SIZE) /*!< 300 ms */
    
    #define CALLWAITING_REPEAT_SAMPLES	( (10000 * 8) / READ_SIZE) /*!< 10,000 ms */
    
    Russell Bryant's avatar
    Russell Bryant committed
    #define CIDCW_EXPIRE_SAMPLES		( (500 * 8) / READ_SIZE) /*!< 500 ms */
    #define MIN_MS_SINCE_FLASH			( (2000) )	/*!< 2000 ms */
    
    #define DEFAULT_RINGT 				( (8000 * 8) / READ_SIZE) /*!< 8,000 ms */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    /*!
     * \brief Configured ring timeout base.
     * \note Value computed from "ringtimeout" read in from chan_dahdi.conf if it exists.
     */
    
    static int ringt_base = DEFAULT_RINGT;
    
    #ifdef HAVE_SS7
    
    #define LINKSTATE_INALARM	(1 << 0)
    #define LINKSTATE_STARTING	(1 << 1)
    #define LINKSTATE_UP		(1 << 2)
    #define LINKSTATE_DOWN		(1 << 3)
    
    
    	pthread_t master;						/*!< Thread of master */
    	ast_mutex_t lock;
    	int fds[NUM_DCHANS];
    	int numsigchans;
    	int linkstate[NUM_DCHANS];
    	int numchans;
    	int type;
    
    	char called_nai;						/*!< Called Nature of Address Indicator */
    	char calling_nai;						/*!< Calling Nature of Address Indicator */
    	char internationalprefix[10];					/*!< country access code ('00' for european dialplans) */
    	char nationalprefix[10];					/*!< area access code ('0' for european dialplans) */
    	char subscriberprefix[20];					/*!< area access code + area code ('0'+area code for european dialplans) */
    	char unknownprefix[20];						/*!< for unknown dialplans */
    
    	struct dahdi_pvt *pvts[MAX_CHANNELS];				/*!< Member channel pvt structs */
    
    static struct dahdi_ss7 linksets[NUM_SPANS];
    
    
    static int cur_ss7type = -1;
    static int cur_linkset = -1;
    static int cur_pointcode = -1;
    static int cur_cicbeginswith = -1;
    static int cur_adjpointcode = -1;
    static int cur_networkindicator = -1;
    static int cur_defaultdpc = -1;
    #endif /* HAVE_SS7 */
    
    
    #ifdef HAVE_OPENR2
    struct dahdi_mfcr2 {
    	pthread_t r2master;		       /*!< Thread of master */
    	openr2_context_t *protocol_context;    /*!< OpenR2 context handle */
    	struct dahdi_pvt *pvts[MAX_CHANNELS];     /*!< Member channel pvt structs */
    	int numchans;                          /*!< Number of channels in this R2 block */
    	int monitored_count;                   /*!< Number of channels being monitored */
    
    	ast_mutex_t monitored_count_lock;      /*!< lock access to the counter */
    
    	ast_cond_t do_monitor;                 /*!< Condition to wake up the monitor thread when there's work to do */
    
    };
    struct dahdi_mfcr2_conf {
    	openr2_variant_t variant;
    	int mfback_timeout;
    	int metering_pulse_timeout;
    	int max_ani;
    	int max_dnis;
    	int get_ani_first:1;
    
    	int call_files:1;
    	int allow_collect_calls:1;
    	int charge_calls:1;
    	int accept_on_offer:1;
    	int forced_release:1;
    	int double_answer:1;
    	int immediate_accept:1;
    	char logdir[OR2_MAX_PATH];
    	char r2proto_file[OR2_MAX_PATH];
    	openr2_log_level_t loglevel;
    	openr2_calling_party_category_t category;
    };
    
    /* malloc'd array of malloc'd r2links */
    
    /* how many r2links have been malloc'd */
    
    
    #endif /* HAVE_OPENR2 */
    
    
    #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
    
    #define PRI_CHANNEL(p) ((p) & 0xff)
    #define PRI_SPAN(p) (((p) >> 8) & 0xff)
    
    #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
    
    Russell Bryant's avatar
    Russell Bryant committed
    	pthread_t master;						/*!< Thread of master */
    	ast_mutex_t lock;						/*!< Mutex */
    	char idleext[AST_MAX_EXTENSION];				/*!< Where to idle extra calls */
    	char idlecontext[AST_MAX_CONTEXT];				/*!< What context to use for idle */
    	char idledial[AST_MAX_EXTENSION];				/*!< What to dial before dumping */
    	int minunused;							/*!< Min # of channels to keep empty */
    	int minidle;							/*!< Min # of "idling" calls to keep active */
    	int nodetype;							/*!< Node type */
    	int switchtype;							/*!< Type of switch to emulate */
    	int nsf;							/*!< Network-Specific Facilities */
    	int dialplan;							/*!< Dialing plan */
    	int localdialplan;						/*!< Local dialing plan */
    	char internationalprefix[10];					/*!< country access code ('00' for european dialplans) */
    	char nationalprefix[10];					/*!< area access code ('0' for european dialplans) */
    	char localprefix[20];						/*!< area access code + area code ('0'+area code for european dialplans) */
    	char privateprefix[20];						/*!< for private dialplans */
    	char unknownprefix[20];						/*!< for unknown dialplans */
    	int dchannels[NUM_DCHANS];					/*!< What channel are the dchannels on */
    	int trunkgroup;							/*!< What our trunkgroup is */
    	int mastertrunkgroup;						/*!< What trunk group is our master */
    	int prilogicalspan;						/*!< Logical span number within trunk group */
    	int numchans;							/*!< Num of channels we represent */
    	int overlapdial;						/*!< In overlap dialing mode */
    
    	int qsigchannelmapping;						/*!< QSIG channel mapping type */
    	int discardremoteholdretrieval;					/*!< shall remote hold or remote retrieval notifications be discarded? */
    
    Russell Bryant's avatar
    Russell Bryant committed
    	int facilityenable;						/*!< Enable facility IEs */
    	struct pri *dchans[NUM_DCHANS];					/*!< Actual d-channels */
    	int dchanavail[NUM_DCHANS];					/*!< Whether each channel is available */
    	struct pri *pri;						/*!< Currently active D-channel */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if to dump PRI event info (Tested but never set) */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int debug;
    
    Russell Bryant's avatar
    Russell Bryant committed
    	int fds[NUM_DCHANS];						/*!< FD's for d-channels */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief Value set but not used */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int offset;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief Span number put into user output messages */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int span;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if span is being reset/restarted */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	int resetting;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief Current position during a reset (-1 if not started) */
    
    #ifdef HAVE_PRI_SERVICE_MESSAGES
    	unsigned int enable_service_message_support:1;	/*!< enable SERVICE message support */
    #endif
    
    #ifdef HAVE_PRI_INBANDDISCONNECT
    	unsigned int inbanddisconnect:1;				/*!< Should we support inband audio after receiving DISCONNECT? */
    
    Russell Bryant's avatar
    Russell Bryant committed
    	time_t lastreset;						/*!< time when unused channels were last reset */
    	long resetinterval;						/*!< Interval (in seconds) for resetting unused channels */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief ISDN signalling type (SIG_PRI, SIG_BRI, SIG_BRI_PTMP, etc...) */
    
    	struct dahdi_pvt *pvts[MAX_CHANNELS];				/*!< Member channel pvt structs */
    	struct dahdi_pvt *crvs;						/*!< Member CRV structs */
    	struct dahdi_pvt *crvend;						/*!< Pointer to end of CRV structs */
    
    static struct dahdi_pri pris[NUM_SPANS];
    
    Mark Spencer's avatar
    Mark Spencer committed
    
    #if 0
    
    Mark Spencer's avatar
    Mark Spencer committed
    #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
    
    Mark Spencer's avatar
    Mark Spencer committed
    #else
    #define DEFAULT_PRI_DEBUG 0
    #endif
    
    
    static inline void pri_rel(struct dahdi_pri *pri)
    
    Mark Spencer's avatar
    Mark Spencer committed
    {
    
    Russell Bryant's avatar
    Russell Bryant committed
    /*! Shut up the compiler */
    
    Mark Spencer's avatar
    Mark Spencer committed
    #endif
    
    
    Russell Bryant's avatar
    Russell Bryant committed
    #define SUB_REAL	0			/*!< Active call */
    #define SUB_CALLWAIT	1			/*!< Call-Waiting call on hold */
    #define SUB_THREEWAY	2			/*!< Three-way call */
    
    /* Polarity states */
    #define POLARITY_IDLE   0
    #define POLARITY_REV    1
    
    
    
    struct distRingData {
    	int ring[3];
    
    };
    struct ringContextData {
    
    	char contextData[AST_MAX_CONTEXT];
    
    	struct distRingData ringnum[3];
    	struct ringContextData ringContext[3];
    };
    
    
    static const char * const subnames[] = {
    
    Mark Spencer's avatar
    Mark Spencer committed
    	"Real",
    	"Callwait",
    	"Threeway"
    };
    
    
    Mark Spencer's avatar
    Mark Spencer committed
    	struct ast_channel *owner;
    	int chan;
    	short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
    
    Russell Bryant's avatar
    Russell Bryant committed
    	struct ast_frame f;		/*!< One frame for each channel.  How did this ever work before? */
    
    	unsigned int needringing:1;
    	unsigned int needbusy:1;
    	unsigned int needcongestion:1;
    	unsigned int needcallerid:1;
    	unsigned int needanswer:1;
    	unsigned int needflash:1;
    
    	unsigned int needhold:1;
    	unsigned int needunhold:1;
    
    	unsigned int linear:1;
    	unsigned int inthreeway:1;
    
    Mark Spencer's avatar
    Mark Spencer committed
    };
    
    #define CONF_USER_REAL		(1 << 0)
    #define CONF_USER_THIRDCALL	(1 << 1)
    
    #define MAX_SLAVES	4
    
    
    Richard Mudgett's avatar
    Richard Mudgett committed
     * First three states are required for send Ring Pulse Alert Signal
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	MWI_SEND_NULL = 0,
    	MWI_SEND_SA,
    	MWI_SEND_SA_WAIT,
    	MWI_SEND_PAUSE,
    	MWI_SEND_SPILL,
    	MWI_SEND_CLEANUP,
    	MWI_SEND_DONE,
    } mwisend_states;
    
    
    struct mwisend_info {
    	struct	timeval	pause;
    	mwisend_states 	mwisend_current;
    };
    
    
    Russell Bryant's avatar
    Russell Bryant committed
    	struct ast_channel *owner;			/*!< Our current active owner (if applicable) */
    							/*!< Up to three channels can be associated with this call */
    
    	struct dahdi_subchannel sub_unused;		/*!< Just a safety precaution */
    	struct dahdi_subchannel subs[3];			/*!< Sub-channels */
    	struct dahdi_confinfo saveconf;			/*!< Saved conference info */
    
    	struct dahdi_pvt *slaves[MAX_SLAVES];		/*!< Slave to us (follows our conferencing) */
    	struct dahdi_pvt *master;				/*!< Master to us (we follow their conferencing) */
    
    Russell Bryant's avatar
    Russell Bryant committed
    	int inconference;				/*!< If our real should be in the conference */
    
    	int bufsize;                /*!< Size of the buffers */
    
    	int buf_no;					/*!< Number of buffers */
    	int buf_policy;				/*!< Buffer policy */
    
    	int faxbuf_no;              /*!< Number of Fax buffers */
    	int faxbuf_policy;          /*!< Fax buffer policy */
    
    Russell Bryant's avatar
    Russell Bryant committed
    	int sig;					/*!< Signalling style */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief Nonzero if the signaling type is sent over a radio.
    	 * \note Set to a couple of nonzero values but it is only tested like a boolean.
    	 */
    	int radio;
    
    	int outsigmod;					/*!< Outbound Signalling style (modifier) */
    
    	int oprmode;					/*!< "Operator Services" mode */
    
    	struct dahdi_pvt *oprpeer;				/*!< "Operator Services" peer tech_pvt ptr */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief Amount of gain to increase during caller id */
    	float cid_rxgain;
    	/*! \brief Rx gain set by chan_dahdi.conf */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	float rxgain;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief Tx gain set by chan_dahdi.conf */
    
    Mark Spencer's avatar
    Mark Spencer committed
    	float txgain;
    
    Russell Bryant's avatar
    Russell Bryant committed
    	int tonezone;					/*!< tone zone for this chan, or -1 for default */
    
    	struct dahdi_pvt *next;				/*!< Next channel in list */
    	struct dahdi_pvt *prev;				/*!< Prev channel in list */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    
    	/*!
    	 * \brief TRUE if ADSI (Analog Display Services Interface) available
    	 * \note Set from the "adsi" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if we can use a polarity reversal to mark when an outgoing
    	 * call is answered by the remote party.
    	 * \note Set from the "answeronpolarityswitch" value read in from chan_dahdi.conf
    	 */
    
    	unsigned int answeronpolarityswitch:1;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if busy detection is enabled.
    	 * (Listens for the beep-beep busy pattern.)
    	 * \note Set from the "busydetect" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if call return is enabled.
    	 * (*69, if your dialplan doesn't catch this first)
    	 * \note Set from the "callreturn" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if busy extensions will hear the call-waiting tone
    	 * and can use hook-flash to switch between callers.
    	 * \note Can be disabled by dialing *70.
    	 * \note Initialized with the "callwaiting" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if send caller ID for Call Waiting
    	 * \note Set from the "callwaitingcallerid" value read in from chan_dahdi.conf
    	 */
    
    	unsigned int callwaitingcallerid:1;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if support for call forwarding enabled.
    	 * Dial *72 to enable call forwarding.
    	 * Dial *73 to disable call forwarding.
    	 * \note Set from the "cancallforward" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if support for call parking is enabled.
    	 * \note Set from the "canpark" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if to wait for a DTMF digit to confirm answer */
    	unsigned int confirmanswer:1;
    	/*!
    	 * \brief TRUE if the channel is to be destroyed on hangup.
    	 * (Used by pseudo channels.)
    	 */
    
    Russell Bryant's avatar
    Russell Bryant committed
    	unsigned int didtdd:1;				/*!< flag to say its done it once */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if analog type line dialed no digits in Dial() */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if in the process of dialing digits or sending something. */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if the transfer capability of the call is digital. */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if Do-Not-Disturb is enabled. */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief XXX BOOLEAN Purpose??? */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if echo cancellation enabled when bridged.
    	 * \note Initialized with the "echocancelwhenbridged" value read in from chan_dahdi.conf
    	 * \note Disabled if the echo canceller is not setup.
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if echo cancellation is turned on. */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if a fax tone has already been handled. */
    	unsigned int faxhandled:1;
    
    	/*! TRUE if dynamic faxbuffers are configured for use, default is OFF */
    
    	/*! TRUE while buffer configuration override is in use */
    	unsigned int bufferoverrideinuse:1;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if over a radio and dahdi_read() has been called. */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if the call will be considered "hung up" on a polarity reversal.
    	 * \note Set from the "hanguponpolarityswitch" value read in from chan_dahdi.conf
    	 */
    
    	unsigned int hanguponpolarityswitch:1;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if DTMF detection needs to be done by hardware. */
    
    	unsigned int hardwaredtmf:1;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if the outgoing caller ID is blocked/hidden.
    	 * \note Caller ID can be disabled by dialing *67.
    	 * \note Caller ID can be enabled by dialing *82.
    	 * \note Initialized with the "hidecallerid" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if hide just the name not the number for legacy PBX use.
    	 * \note Only applies to PRI channels.
    	 * \note Set from the "hidecalleridname" value read in from chan_dahdi.conf
    	 */
    	unsigned int hidecalleridname:1;
    	/*! \brief TRUE if DTMF detection is disabled. */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if the channel should be answered immediately
    	 * without attempting to gather any digits.
    	 * \note Set from the "immediate" value read in from chan_dahdi.conf
    	 */
    	unsigned int immediate:1;
    	/*! \brief TRUE if in an alarm condition. */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if TDD in MATE mode */
    	unsigned int mate:1;
    	/*! \brief TRUE if we originated the call leg. */
    
    	/* unsigned int overlapdial:1; 			unused and potentially confusing */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if busy extensions will hear the call-waiting tone
    	 * and can use hook-flash to switch between callers.
    	 * \note Set from the "callwaiting" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if the outgoing caller ID is blocked/restricted/hidden.
    	 * \note Set from the "hidecallerid" value read in from chan_dahdi.conf
    	 */
    	unsigned int permhidecallerid:1;
    	/*!
    	 * \brief TRUE if PRI congestion/busy indications are sent out-of-band.
    	 * \note Set from the "priindication" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if PRI B channels are always exclusively selected.
    	 * \note Set from the "priexclusive" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if we will pulse dial.
    	 * \note Set from the "pulsedial" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*! \brief TRUE if a pulsed digit was detected. (Pulse dial phone detected) */
    	unsigned int pulsedial:1;
    
    	unsigned int restartpending:1;		/*!< flag to ensure counted only once for restart */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if caller ID is restricted.
    	 * \note Set but not used.  Should be deleted.  Redundant with permhidecallerid.
    	 * \note Set from the "restrictcid" value read in from chan_dahdi.conf
    	 */
    	unsigned int restrictcid:1;
    	/*!
    	 * \brief TRUE if three way calling is enabled
    	 * \note Set from the "threewaycalling" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if call transfer is enabled
    	 * \note For FXS ports (either direct analog or over T1/E1):
    	 *   Support flash-hook call transfer
    	 * \note For digital ports using ISDN PRI protocols:
    	 *   Support switch-side transfer (called 2BCT, RLT or other names)
    	 * \note Set from the "transfer" value read in from chan_dahdi.conf
    	 */
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if caller ID is used on this channel.
    	 * \note PRI and SS7 spans will save caller ID from the networking peer.
    	 * \note FXS ports will generate the caller ID spill.
    	 * \note FXO ports will listen for the caller ID spill.
    	 * \note Set from the "usecallerid" value read in from chan_dahdi.conf
    	 */
    	unsigned int use_callerid:1;
    	/*!
    	 * \brief TRUE if we will use the calling presentation setting
    	 * from the Asterisk channel for outgoing calls.
    	 * \note Only applies to PRI and SS7 channels.
    	 * \note Set from the "usecallingpres" value read in from chan_dahdi.conf
    	 */
    	unsigned int use_callingpres:1;
    	/*!
    	 * \brief TRUE if distinctive rings are to be detected.
    	 * \note For FXO lines
    	 * \note Set indirectly from the "usedistinctiveringdetection" value read in from chan_dahdi.conf
    	 */
    
    	unsigned int usedistinctiveringdetection:1;
    
    Richard Mudgett's avatar
    Richard Mudgett committed
    	/*!
    	 * \brief TRUE if we should use the callerid from incoming call on dahdi transfer.
    	 * \note Set from the "useincomingcalleridondahditransfer" value read in from chan_dahdi.conf
    	 */
    	unsigned int dahditrcallerid:1;
    	/*!
    	 * \brief TRUE if allowed to flash-transfer to busy channels.
    	 * \note Set from the "transfertobusy" value read in from chan_dahdi.conf
    	 */
    	unsigned int transfertobusy:1;
    	/*!
    	 * \brief TRUE if the FXO port monitors for neon type MWI indications from the other end.
    	 * \note Set if the "mwimonitor" value read in contains "neon" from chan_dahdi.conf
    	 */
    	unsigned int mwimonitor_neon:1;
    	/*!
    	 * \brief TRUE if the FXO port monitors for fsk type MWI indications from the other end.