/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2006, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* 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.
*
* 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.
*/
/*! \file
*
* \brief DAHDI for Pseudo TDM
*
* \author Mark Spencer <markster@digium.com>
*
* Connects to the DAHDI telephony library as well as
* 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
* and install the DAHDI channel.
*
* \par See also
* \arg \ref Config_dahdi
*
* \ingroup channel_drivers
*
* \todo Deprecate the "musiconhold" configuration option post 1.4
*/
/*** MODULEINFO
<depend>res_smdi</depend>
<depend>dahdi</depend>
<depend>tonezone</depend>
<use>pri</use>
<use>ss7</use>
***/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#if defined(__NetBSD__) || defined(__FreeBSD__)
#include <pthread.h>
#include <signal.h>
#else
#include <sys/signal.h>
#endif
#include <sys/ioctl.h>
#include <math.h>
#include <ctype.h>
#include <dahdi/user.h>
#include <dahdi/tonezone.h>
#ifdef HAVE_PRI
#include <libpri.h>
#endif
#ifdef HAVE_SS7
#include <libss7.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/abstract_jb.h"
#include "asterisk/smdi.h"
#include "asterisk/astobj.h"
#include "asterisk/event.h"
#include "asterisk/devicestate.h"
#define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
static const char *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 = "",
.target_extra = -1,
};
static struct ast_jb_conf global_jbconf;
/* define this to send PRI user-user information elements */
#undef SUPPORT_USERUSER
/*!
* \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
* 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.
*/
/* #define ZHONE_HACK */
/*! \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 */
/*! \brief Typically, how many rings before we should send Caller*ID */
#define DEFAULT_CIDRINGS 1
#define CHANNEL_PSEUDO -12
#define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
/*! \brief Signaling types that need to use MF detection should be placed in this macro */
#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"
#ifdef HAVE_PRI
" w/PRI"
#endif
#ifdef HAVE_SS7
" w/SS7"
#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_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)
#ifdef LOTS_OF_SPANS
#define NUM_SPANS DAHDI_MAX_SPANS
#else
#define NUM_SPANS 32
#endif
#define NUM_DCHANS 4 /*!< No more than 4 d-channels */
#define MAX_CHANNELS 672 /*!< No more than a DS3 per trunk group */
#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)
static char defaultcic[64] = "";
static char defaultozz[64] = "";
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] = "";
static int mwisend_rpas = 0;
static char progzone[10] = "";
static int usedistinctiveringdetection = 0;
static int distinctiveringaftercid = 0;
static int numbufs = 4;
static int mwilevel = 512;
#ifdef HAVE_PRI
static struct ast_channel inuse;
#ifdef PRI_GETSET_TIMERS
static int pritimers[PRI_MAX_TIMERS];
#endif
static int pridebugfd = -1;
static char pridebugfilename[1024] = "";
#endif
/*! \brief Wait up to 16 seconds for first digit (FXO logic) */
static int firstdigittimeout = 16000;
/*! \brief How long to wait for following digits (FXO logic) */
static int gendigittimeout = 8000;
/*! \brief How long to wait for an extra digit, if there is an ambiguous match */
static int matchdigittimeout = 3000;
/*! \brief Protect the interface list (of dahdi_pvt's) */
AST_MUTEX_DEFINE_STATIC(iflock);
static int ifcount = 0;
#ifdef HAVE_PRI
AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
#endif
/*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
when it's doing something critical. */
AST_MUTEX_DEFINE_STATIC(monlock);
/*! \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 mwi_thread_complete;
static ast_cond_t ss_thread_complete;
AST_MUTEX_DEFINE_STATIC(mwi_thread_lock);
AST_MUTEX_DEFINE_STATIC(ss_thread_lock);
AST_MUTEX_DEFINE_STATIC(restart_lock);
static int mwi_thread_count = 0;
static int ss_thread_count = 0;
static int num_restart_pending = 0;
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 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)
{
int j;
if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
return -1;
return j;
}
/*! \brief Avoid the silly dahdi_waitevent which ignores a bunch of events */
static inline int dahdi_wait_event(int fd)
{
int i, j = 0;
i = DAHDI_IOMUX_SIGEVENT;
if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
return -1;
if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
return -1;
return j;
}
/*! Chunk size to read -- we use 20ms chunks to make things happy. */
#define READ_SIZE 160
#define MASK_AVAIL (1 << 0) /*!< Channel available for PRI use */
#define MASK_INUSE (1 << 1) /*!< Channel currently in use */
#define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE) /*!< 300 ms */
#define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE) /*!< 10,000 ms */
#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 */
struct dahdi_pvt;
/*!
* \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)
#define SS7_NAI_DYNAMIC -1
#define LINKSET_FLAG_EXPLICITACM (1 << 0)
struct dahdi_ss7 {
pthread_t master; /*!< Thread of master */
ast_mutex_t lock;
int fds[NUM_DCHANS];
int numsigchans;
int linkstate[NUM_DCHANS];
int numchans;
int type;
enum {
LINKSET_STATE_DOWN = 0,
LINKSET_STATE_UP
} state;
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 ss7 *ss7;
struct dahdi_pvt *pvts[MAX_CHANNELS]; /*!< Member channel pvt structs */
int flags; /*!< Linkset flags */
};
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_PRI
#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)
struct dahdi_pri {
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 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 */
/*! \brief TRUE if to dump PRI event info (Tested but never set) */
int debug;
int fds[NUM_DCHANS]; /*!< FD's for d-channels */
/*! \brief Value set but not used */
int offset;
/*! \brief Span number put into user output messages */
int span;
/*! \brief TRUE if span is being reset/restarted */
int resetting;
/*! \brief Current position during a reset (-1 if not started) */
int resetpos;
#ifdef HAVE_PRI_INBANDDISCONNECT
unsigned int inbanddisconnect:1; /*!< Should we support inband audio after receiving DISCONNECT? */
#endif
time_t lastreset; /*!< time when unused channels were last reset */
long resetinterval; /*!< Interval (in seconds) for resetting unused channels */
/*! \brief ISDN signalling type (SIG_PRI, SIG_BRI, SIG_BRI_PTMP, etc...) */
int sig;
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];
#if 0
#define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
#else
#define DEFAULT_PRI_DEBUG 0
#endif
static inline void pri_rel(struct dahdi_pri *pri)
{
ast_mutex_unlock(&pri->lock);
}
#else
/*! Shut up the compiler */
struct dahdi_pri;
#endif
#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];
int range;
};
struct ringContextData {
char contextData[AST_MAX_CONTEXT];
};
struct dahdi_distRings {
struct distRingData ringnum[3];
struct ringContextData ringContext[3];
};
static char *subnames[] = {
"Real",
"Callwait",
"Threeway"
};
struct dahdi_subchannel {
int dfd;
struct ast_channel *owner;
int chan;
short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
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;
struct dahdi_confinfo curconf;
};
#define CONF_USER_REAL (1 << 0)
#define CONF_USER_THIRDCALL (1 << 1)
#define MAX_SLAVES 4
static struct dahdi_pvt {
ast_mutex_t lock; /*!< Channel private lock. */
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) */
int inconference; /*!< If our real should be in the conference */
int buf_no; /*!< Number of buffers */
int buf_policy; /*!< Buffer policy */
int sig; /*!< Signalling style */
/*!
* \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 */
/*! \brief Amount of gain to increase during caller id */
float cid_rxgain;
/*! \brief Rx gain set by chan_dahdi.conf */
float rxgain;
/*! \brief Tx gain set by chan_dahdi.conf */
float txgain;
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 */
/* flags */
/*!
* \brief TRUE if ADSI (Analog Display Services Interface) available
* \note Set from the "adsi" value read in from chan_dahdi.conf
*/
unsigned int adsi:1;
/*!
* \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;
/*!
* \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
*/
unsigned int busydetect:1;
/*!
* \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
*/
unsigned int callreturn:1;
/*!
* \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
*/
unsigned int callwaiting:1;
/*!
* \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;
/*!
* \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
*/
unsigned int cancallforward:1;
/*!
* \brief TRUE if support for call parking is enabled.
* \note Set from the "canpark" value read in from chan_dahdi.conf
*/
unsigned int canpark:1;
/*! \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.)
*/
unsigned int destroy:1;
unsigned int didtdd:1; /*!< flag to say its done it once */
/*! \brief TRUE if analog type line dialed no digits in Dial() */
unsigned int dialednone:1;
/*! \brief TRUE if in the process of dialing digits or sending something. */
unsigned int dialing:1;
/*! \brief TRUE if the transfer capability of the call is digital. */
unsigned int digital:1;
/*! \brief TRUE if Do-Not-Disturb is enabled. */
unsigned int dnd:1;
/*! \brief XXX BOOLEAN Purpose??? */
unsigned int echobreak:1;
/*!
* \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.
*/
unsigned int echocanbridged:1;
/*! \brief TRUE if echo cancellation is turned on. */
unsigned int echocanon:1;
/*! \brief TRUE if a fax tone has already been handled. */
unsigned int faxhandled:1;
/*! \brief TRUE if over a radio and dahdi_read() has been called. */
unsigned int firstradio:1;
/*!
* \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;
/*! \brief TRUE if DTMF detection needs to be done by hardware. */
unsigned int hardwaredtmf:1;
/*!
* \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
*/
unsigned int hidecallerid:1;
/*!
* \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. */
unsigned int ignoredtmf:1;
/*!
* \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. */
unsigned int inalarm:1;
/*! \brief TRUE if TDD in MATE mode */
unsigned int mate:1;
/*! \brief TRUE if we originated the call leg. */
unsigned int outgoing:1;
/* unsigned int overlapdial:1; unused and potentially confusing */
/*!
* \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
*/
unsigned int permcallwaiting:1;
/*!
* \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
*/
unsigned int priindication_oob:1;
/*!
* \brief TRUE if PRI B channels are always exclusively selected.
* \note Set from the "priexclusive" value read in from chan_dahdi.conf
*/
unsigned int priexclusive:1;
/*!
* \brief TRUE if we will pulse dial.
* \note Set from the "pulsedial" value read in from chan_dahdi.conf
*/
unsigned int pulse:1;
/*! \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 */
/*!
* \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
*/
unsigned int threewaycalling:1;
/*!
* \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
*/
unsigned int transfer:1;
/*!
* \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;
/*!
* \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.
* \note Set if the "mwimonitor" value read in contains "fsk" from chan_dahdi.conf
*/
unsigned int mwimonitor_fsk:1;
/*!
* \brief TRUE if the FXO port monitors for rpas precursor to fsk MWI indications from the other end.
* \note RPAS - Ring Pulse Alert Signal
* \note Set if the "mwimonitor" value read in contains "rpas" from chan_dahdi.conf
*/
unsigned int mwimonitor_rpas:1;
/*! \brief TRUE if an MWI monitor thread is currently active */
unsigned int mwimonitoractive:1;
/*! \brief TRUE if a MWI message sending thread is active */
unsigned int mwisendactive:1;
/*!
* \brief TRUE if channel is out of reset and ready
* \note Set but not used.
*/
unsigned int inservice:1;
/*!
* \brief TRUE if the channel is locally blocked.
* \note Applies to SS7 channels.
*/
unsigned int locallyblocked:1;
/*!
* \brief TRUE if the channel is remotely blocked.
* \note Applies to SS7 channels.
*/
unsigned int remotelyblocked:1;
#if defined(HAVE_PRI) || defined(HAVE_SS7)
/*!
* \brief XXX BOOLEAN Purpose???
* \note Applies to SS7 channels.
*/
unsigned int rlt:1;
/*! \brief TRUE if channel is alerting/ringing */
unsigned int alerting:1;
/*! \brief TRUE if the call has already gone/hungup */
unsigned int alreadyhungup:1;
/*!
* \brief TRUE if this is an idle call
* \note Applies to PRI channels.
*/
unsigned int isidlecall:1;
/*!
* \brief TRUE if call is in a proceeding state.
* The call has started working its way through the network.
*/
unsigned int proceeding:1;
/*! \brief TRUE if the call has seen progress through the network. */
unsigned int progress:1;
/*!
* \brief TRUE if this channel is being reset/restarted
* \note Applies to PRI channels.
*/
unsigned int resetting:1;
/*!
* \brief TRUE if this channel has received a SETUP_ACKNOWLEDGE
* \note Applies to PRI channels.
*/
unsigned int setup_ack:1;
#endif
/*!
* \brief TRUE if SMDI (Simplified Message Desk Interface) is enabled
* \note Set from the "usesmdi" value read in from chan_dahdi.conf
*/
unsigned int use_smdi:1;
/*! \brief The serial port to listen for SMDI data on */
struct ast_smdi_interface *smdi_iface;
/*! \brief Distinctive Ring data */
struct dahdi_distRings drings;
/*!
* \brief The configured context for incoming calls.
* \note The "context" string read in from chan_dahdi.conf
*/
char context[AST_MAX_CONTEXT];
/*!
* \brief Saved context string.
*/
char defcontext[AST_MAX_CONTEXT];
/*! \brief Extension to use in the dialplan. */
char exten[AST_MAX_EXTENSION];
/*!
* \brief Language configured for calls.
* \note The "language" string read in from chan_dahdi.conf
*/
char language[MAX_LANGUAGE];
/*!
* \brief The configured music-on-hold class to use for calls.
* \note The "musicclass" or "mohinterpret" or "musiconhold" string read in from chan_dahdi.conf
*/
char mohinterpret[MAX_MUSICCLASS];
/*!
* \brief Suggested music-on-hold class for peer channel to use for calls.
* \note The "mohsuggest" string read in from chan_dahdi.conf
*/
char mohsuggest[MAX_MUSICCLASS];
char parkinglot[AST_MAX_EXTENSION]; /*!< Parking lot for this channel */
#if defined(PRI_ANI) || defined(HAVE_SS7)
/*! \brief Automatic Number Identification number (Alternate PRI caller ID number) */
char cid_ani[AST_MAX_EXTENSION];
#endif
/*! \brief Automatic Number Identification code from PRI */
int cid_ani2;
/*! \brief Caller ID number from an incoming call. */
char cid_num[AST_MAX_EXTENSION];
/*! \brief Caller ID Q.931 TON/NPI field values. Set by PRI. Zero otherwise. */
int cid_ton;
/*! \brief Caller ID name from an incoming call. */
char cid_name[AST_MAX_EXTENSION];
/*! \brief Last Caller ID number from an incoming call. */
char lastcid_num[AST_MAX_EXTENSION];
/*! \brief Last Caller ID name from an incoming call. */
char lastcid_name[AST_MAX_EXTENSION];
char *origcid_num; /*!< malloced original callerid */
char *origcid_name; /*!< malloced original callerid */
/*! \brief Call waiting number. */
char callwait_num[AST_MAX_EXTENSION];
/*! \brief Call waiting name. */
char callwait_name[AST_MAX_EXTENSION];
/*! \brief Redirecting Directory Number Information Service (RDNIS) number */
char rdnis[AST_MAX_EXTENSION];
/*! \brief Dialed Number Identifier */
char dnid[AST_MAX_EXTENSION];
/*!
* \brief Bitmapped groups this belongs to.
* \note The "group" bitmapped group string read in from chan_dahdi.conf
*/
ast_group_t group;
/*! \brief Active PCM encoding format: DAHDI_LAW_ALAW or DAHDI_LAW_MULAW */
int law;
int confno; /*!< Our conference */
int confusers; /*!< Who is using our conference */
int propconfno; /*!< Propagated conference number */
/*!
* \brief Bitmapped call groups this belongs to.
* \note The "callgroup" bitmapped group string read in from chan_dahdi.conf
*/
ast_group_t callgroup;
/*!
* \brief Bitmapped pickup groups this belongs to.
* \note The "pickupgroup" bitmapped group string read in from chan_dahdi.conf
*/
ast_group_t pickupgroup;
/*!
* \brief Channel variable list with associated values to set when a channel is created.
* \note The "setvar" strings read in from chan_dahdi.conf
*/
struct ast_variable *vars;
int channel; /*!< Channel Number or CRV */
int span; /*!< Span number */
time_t guardtime; /*!< Must wait this much time before using for new call */
int cid_signalling; /*!< CID signalling type bell202 or v23 */
int cid_start; /*!< CID start indicator, polarity or ring */
int callingpres; /*!< The value of callling presentation that we're going to use when placing a PRI call */
int callwaitingrepeat; /*!< How many samples to wait before repeating call waiting */
int cidcwexpire; /*!< When to expire our muting for CID/CW */
/*! \brief Analog caller ID waveform sample buffer */
unsigned char *cidspill;
/*! \brief Position in the cidspill buffer to send out next. */
int cidpos;
/*! \brief Length of the cidspill buffer containing samples. */
int cidlen;
/*! \brief Ring timeout timer?? */
int ringt;
/*!
* \brief Ring timeout base.
* \note Value computed indirectly from "ringtimeout" read in from chan_dahdi.conf
*/
int ringt_base;
/*!
* \brief Number of most significant digits/characters to strip from the dialed number.
* \note Feature is deprecated. Use dialplan logic.
* \note The characters are stripped before the PRI TON/NPI prefix
* characters are processed.
*/
int stripmsd;
/*! \brief BOOLEAN. XXX Meaning what?? */
int callwaitcas;
/*! \brief Number of call waiting rings. */
int callwaitrings;
/*! \brief Echo cancel parameters. */
struct {
struct dahdi_echocanparams head;
struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
} echocancel;
/*!
* \brief Echo training time. 0 = disabled
* \note Set from the "echotraining" value read in from chan_dahdi.conf
*/
int echotraining;
/*! \brief Filled with 'w'. XXX Purpose?? */
char echorest[20];
/*!
* \brief Number of times to see "busy" tone before hanging up.
* \note Set from the "busycount" value read in from chan_dahdi.conf
*/
int busycount;
/*!
* \brief Length of "busy" tone on time.
* \note Set from the "busypattern" value read in from chan_dahdi.conf
*/
int busy_tonelength;
/*!
* \brief Length of "busy" tone off time.
* \note Set from the "busypattern" value read in from chan_dahdi.conf
*/
int busy_quietlength;
/*!
* \brief Bitmapped call progress detection flags. CALLPROGRESS_xxx values.
* \note Bits set from the "callprogress" and "faxdetect" values read in from chan_dahdi.conf
*/
int callprogress;
struct timeval flashtime; /*!< Last flash-hook time */
/*! \brief Opaque DSP configuration structure. */
struct ast_dsp *dsp;
//int cref; /*!< Call reference number (Not used) */
/*! \brief DAHDI dial operation command struct for ioctl() call. */
struct dahdi_dialoperation dop;
int whichwink; /*!< SIG_FEATDMF_TA Which wink are we on? */
/*! \brief Second part of SIG_FEATDMF_TA wink operation. */
char finaldial[64];
char accountcode[AST_MAX_ACCOUNT_CODE]; /*!< Account code */
int amaflags; /*!< AMA Flags */
struct tdd_state *tdd; /*!< TDD flag */
/*! \brief Accumulated call forwarding number. */
char call_forward[AST_MAX_EXTENSION];
/*!
* \brief Voice mailbox location.
* \note Set from the "mailbox" string read in from chan_dahdi.conf
*/
char mailbox[AST_MAX_EXTENSION];
/*! \brief Opaque event subscription parameters for message waiting indication support. */
struct ast_event_sub *mwi_event_sub;
/*! \brief Delayed dialing for E911. Overlap digits for ISDN. */
char dialdest[256];
/*! \brief Time the interface went on-hook. */
int onhooktime;
/*! \brief -1 = unknown, 0 = no messages, 1 = new messages available */
int msgstate;
int distinctivering; /*!< Which distinctivering to use */
int cidrings; /*!< Which ring to deliver CID on */
int dtmfrelax; /*!< whether to run in relaxed DTMF mode */
/*! \brief Holding place for event injected from outside normal operation. */
int fake_event;
/*!
* \brief Minimal time period (ms) between the answer polarity
* switch and hangup polarity switch.
*/
int polarityonanswerdelay;
/*! \brief Start delay time if polarityonanswerdelay is nonzero. */
struct timeval polaritydelaytv;
/*!
* \brief Send caller ID after this many rings.
* \note Set from the "sendcalleridafter" value read in from chan_dahdi.conf
*/
int sendcalleridafter;
#ifdef HAVE_PRI
/*! \brief DAHDI PRI control parameters */
struct dahdi_pri *pri;
/*! \brief XXX Purpose??? */
struct dahdi_pvt *bearer;
/*! \brief XXX Purpose??? */
struct dahdi_pvt *realcall;
/*! \brief Opaque libpri call control structure */
q931_call *call;
/*! \brief Channel number in span. */
int prioffset;
/*! \brief Logical span number within trunk group */
int logicalspan;
#endif
/*! \brief Current line interface polarity. POLARITY_IDLE, POLARITY_REV */
int polarity;
/*! \brief DSP feature flags: DSP_FEATURE_xxx */
int dsp_features;
#ifdef HAVE_SS7
/*! \brief SS7 control parameters */
struct dahdi_ss7 *ss7;
/*! \brief Opaque libss7 call control structure */
struct isup_call *ss7call;
char charge_number[50];
char gen_add_number[50];
char gen_dig_number[50];
char orig_called_num[50];
char redirecting_num[50];
char generic_name[50];
unsigned char gen_add_num_plan;
unsigned char gen_add_nai;
unsigned char gen_add_pres_ind;
unsigned char gen_add_type;
unsigned char gen_dig_type;
unsigned char gen_dig_scheme;
char jip_number[50];
unsigned char lspi_type;
unsigned char lspi_scheme;
unsigned char lspi_context;
char lspi_ident[50];
unsigned int call_ref_ident;
unsigned int call_ref_pc;
unsigned char calling_party_cat;
int transcap;
int cic; /*!< CIC associated with channel */
unsigned int dpc; /*!< CIC's DPC */
unsigned int loopedback:1;
#endif
/*! \brief DTMF digit in progress. 0 when no digit in progress. */
char begindigit;
/*! \brief TRUE if confrence is muted. */
int muting;
} *iflist = NULL, *ifend = NULL;
/*! \brief Channel configuration from chan_dahdi.conf .
* This struct is used for parsing the [channels] section of chan_dahdi.conf.
* Generally there is a field here for every possible configuration item.
*
* The state of fields is saved along the parsing and whenever a 'channel'
* statement is reached, the current dahdi_chan_conf is used to configure the
* channel (struct dahdi_pvt)
*
* \see dahdi_chan_init for the default values.
*/
struct dahdi_chan_conf {
struct dahdi_pvt chan;
#ifdef HAVE_PRI
struct dahdi_pri pri;
#endif
#ifdef HAVE_SS7
struct dahdi_ss7 ss7;
#endif
struct dahdi_params timing;
int is_sig_auto; /*!< Use channel signalling from DAHDI? */
/*!
* \brief The serial port to listen for SMDI data on
* \note Set from the "smdiport" string read in from chan_dahdi.conf
*/
char smdi_port[SMDI_MAX_FILENAME_LEN];
};
/*! returns a new dahdi_chan_conf with default values (by-value) */
static struct dahdi_chan_conf dahdi_chan_conf_default(void) {
/* recall that if a field is not included here it is initialized
* to 0 or equivalent
*/
struct dahdi_chan_conf conf = {
#ifdef HAVE_PRI
.pri = {
.nsf = PRI_NSF_NONE,
.switchtype = PRI_SWITCH_NI2,
.dialplan = PRI_UNKNOWN + 1,
.localdialplan = PRI_NATIONAL_ISDN + 1,
.nodetype = PRI_CPE,
.minunused = 2,
.idleext = "",
.idledial = "",
.internationalprefix = "",
.nationalprefix = "",
.localprefix = "",
.privateprefix = "",
.unknownprefix = "",
.resetinterval = -1,
},
#endif
#ifdef HAVE_SS7
.ss7 = {
.called_nai = SS7_NAI_NATIONAL,
.calling_nai = SS7_NAI_NATIONAL,
.internationalprefix = "",
.nationalprefix = "",
.subscriberprefix = "",
.unknownprefix = ""
},
#endif
.chan = {
.context = "default",
.cid_num = "",
.cid_name = "",
.mohinterpret = "default",
.mohsuggest = "",
.parkinglot = "",
.transfertobusy = 1,
.cid_signalling = CID_SIG_BELL,
.cid_start = CID_START_RING,
.dahditrcallerid = 0,
.use_callerid = 1,
.sig = -1,
.outsigmod = -1,
.cid_rxgain = +5.0,
.tonezone = -1,
.echocancel.head.tap_length = 1,
.busycount = 3,
.accountcode = "",
.mailbox = "",
.polarityonanswerdelay = 600,
.sendcalleridafter = DEFAULT_CIDRINGS,
.buf_policy = DAHDI_POLICY_IMMEDIATE,
.buf_no = numbufs
},
.timing = {
.prewinktime = -1,
.preflashtime = -1,
.winktime = -1,
.flashtime = -1,
.starttime = -1,
.rxwinktime = -1,
.rxflashtime = -1,
.debouncetime = -1
},
.is_sig_auto = 1,
.smdi_port = "/dev/ttyS0",
};
return conf;
}
static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause);
static int dahdi_digit_begin(struct ast_channel *ast, char digit);
static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
static int dahdi_sendtext(struct ast_channel *c, const char *text);
static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
static int dahdi_hangup(struct ast_channel *ast);
static int dahdi_answer(struct ast_channel *ast);
static struct ast_frame *dahdi_read(struct ast_channel *ast);
static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
static struct ast_frame *dahdi_exception(struct ast_channel *ast);
static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
static struct dahdi_pvt *handle_init_event(struct dahdi_pvt *i, int event);
static const struct ast_channel_tech dahdi_tech = {
.type = "DAHDI",
.description = tdesc,
.capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
.requester = dahdi_request,
.send_digit_begin = dahdi_digit_begin,
.send_digit_end = dahdi_digit_end,
.send_text = dahdi_sendtext,
.call = dahdi_call,
.hangup = dahdi_hangup,
.answer = dahdi_answer,
.read = dahdi_read,
.write = dahdi_write,
.bridge = dahdi_bridge,
.exception = dahdi_exception,
.indicate = dahdi_indicate,
.fixup = dahdi_fixup,
.setoption = dahdi_setoption,
.func_channel_read = dahdi_func_read,
};
#ifdef HAVE_PRI
#define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
#else
#define GET_CHANNEL(p) ((p)->channel)
#endif
struct dahdi_pvt *round_robin[32];
#ifdef HAVE_PRI
static inline int pri_grab(struct dahdi_pvt *pvt, struct dahdi_pri *pri)
{
int res;
/* Grab the lock first */
do {
res = ast_mutex_trylock(&pri->lock);
if (res) {
DEADLOCK_AVOIDANCE(&pvt->lock);
}
} while (res);
/* Then break the poll */
if (pri->master != AST_PTHREADT_NULL)
pthread_kill(pri->master, SIGURG);
return 0;
}
#endif
#ifdef HAVE_SS7
static inline void ss7_rel(struct dahdi_ss7 *ss7)
{
ast_mutex_unlock(&ss7->lock);
}
static inline int ss7_grab(struct dahdi_pvt *pvt, struct dahdi_ss7 *pri)
{
int res;
/* Grab the lock first */
do {
res = ast_mutex_trylock(&pri->lock);
if (res) {
DEADLOCK_AVOIDANCE(&pvt->lock);
}
} while (res);
/* Then break the poll */
if (pri->master != AST_PTHREADT_NULL)
pthread_kill(pri->master, SIGURG);
return 0;
}
#endif
#define NUM_CADENCE_MAX 25
static int num_cadence = 4;
static int user_has_defined_cadences = 0;
static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
{ { 125, 125, 2000, 4000 } }, /*!< Quick chirp followed by normal ring */
{ { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
{ { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
{ { 1000, 500, 2500, 5000 } }, /*!< Long ring */
};
/*! \brief cidrings says in which pause to transmit the cid information, where the first pause
* is 1, the second pause is 2 and so on.
*/
static int cidrings[NUM_CADENCE_MAX] = {
2, /*!< Right after first long ring */
4, /*!< Right after long part */
3, /*!< After third chirp */
2, /*!< Second spell */
};
/* ETSI EN300 659-1 specifies the ring pulse between 200 and 300 mS */
static struct dahdi_ring_cadence AS_RP_cadence = {{250, 10000}};
#define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
(p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
#define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
#define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __DAHDI_SIG_FXO) */)
static int dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok)
{
int res;
if (p->subs[SUB_REAL].owner == ast)
res = 0;
else if (p->subs[SUB_CALLWAIT].owner == ast)
res = 1;
else if (p->subs[SUB_THREEWAY].owner == ast)
res = 2;
else {
res = -1;
if (!nullok)
ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
}
return res;
}
static void wakeup_sub(struct dahdi_pvt *p, int a, struct dahdi_pri *pri)
{
#ifdef HAVE_PRI
if (pri)
ast_mutex_unlock(&pri->lock);
#endif
for (;;) {
if (p->subs[a].owner) {
if (ast_channel_trylock(p->subs[a].owner)) {
DEADLOCK_AVOIDANCE(&p->lock);
} else {
ast_queue_frame(p->subs[a].owner, &ast_null_frame);
ast_channel_unlock(p->subs[a].owner);
break;
}
} else
break;
}
#ifdef HAVE_PRI
if (pri)
ast_mutex_lock(&pri->lock);
#endif
}
static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *data)
{
#ifdef HAVE_PRI
struct dahdi_pri *pri = (struct dahdi_pri*) data;
#endif
#ifdef HAVE_SS7
struct dahdi_ss7 *ss7 = (struct dahdi_ss7*) data;
#endif
/* We must unlock the PRI to avoid the possibility of a deadlock */
#if defined(HAVE_PRI) || defined(HAVE_SS7)
if (data) {
switch (p->sig) {
#ifdef HAVE_PRI
case SIG_BRI:
case SIG_BRI_PTMP:
case SIG_PRI:
ast_mutex_unlock(&pri->lock);
break;
#endif
#ifdef HAVE_SS7
case SIG_SS7:
ast_mutex_unlock(&ss7->lock);
break;
#endif
default:
break;
}
}
#endif
for (;;) {
if (p->owner) {
if (ast_channel_trylock(p->owner)) {
DEADLOCK_AVOIDANCE(&p->lock);
} else {
ast_queue_frame(p->owner, f);
ast_channel_unlock(p->owner);
break;
}
} else
break;
}
#if defined(HAVE_PRI) || defined(HAVE_SS7)
if (data) {
switch (p->sig) {
#ifdef HAVE_PRI
case SIG_BRI:
case SIG_BRI_PTMP:
case SIG_PRI:
ast_mutex_lock(&pri->lock);
break;
#endif
#ifdef HAVE_SS7
case SIG_SS7:
ast_mutex_lock(&ss7->lock);
break;
#endif
default:
break;
}
}
#endif
}
static int restore_gains(struct dahdi_pvt *p);
static void swap_subs(struct dahdi_pvt *p, int a, int b)
{
int tchan;
int tinthreeway;
struct ast_channel *towner;
ast_debug(1, "Swapping %d and %d\n", a, b);
tchan = p->subs[a].chan;
towner = p->subs[a].owner;
tinthreeway = p->subs[a].inthreeway;
p->subs[a].chan = p->subs[b].chan;
p->subs[a].owner = p->subs[b].owner;
p->subs[a].inthreeway = p->subs[b].inthreeway;
p->subs[b].chan = tchan;
p->subs[b].owner = towner;
p->subs[b].inthreeway = tinthreeway;
if (p->subs[a].owner)
ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].dfd);
if (p->subs[b].owner)
ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].dfd);
wakeup_sub(p, a, NULL);
wakeup_sub(p, b, NULL);
}
static int dahdi_open(char *fn)
{
int fd;
int isnum;
int chan = 0;
int bs;
int x;
isnum = 1;
for (x = 0; x < strlen(fn); x++) {
if (!isdigit(fn[x])) {
isnum = 0;
break;
}
}
if (isnum) {
chan = atoi(fn);
if (chan < 1) {
ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
return -1;
}
fn = "/dev/dahdi/channel";
}
fd = open(fn, O_RDWR | O_NONBLOCK);
if (fd < 0) {
ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
return -1;
}
if (chan) {
if (ioctl(fd, DAHDI_SPECIFY, &chan)) {
x = errno;
close(fd);
errno = x;
ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
return -1;
}
}
bs = READ_SIZE;
if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs) == -1) {
ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
x = errno;
close(fd);
errno = x;
return -1;
}
return fd;
}
static void dahdi_close(int fd)
{
if (fd > 0)
close(fd);
}
static void dahdi_close_sub(struct dahdi_pvt *chan_pvt, int sub_num)
{
dahdi_close(chan_pvt->subs[sub_num].dfd);
chan_pvt->subs[sub_num].dfd = -1;
}
#ifdef HAVE_PRI
static void dahdi_close_pri_fd(struct dahdi_pri *pri, int fd_num)
{
dahdi_close(pri->fds[fd_num]);
pri->fds[fd_num] = -1;
}
#endif
#ifdef HAVE_SS7
static void dahdi_close_ss7_fd(struct dahdi_ss7 *ss7, int fd_num)
{
dahdi_close(ss7->fds[fd_num]);
ss7->fds[fd_num] = -1;
}
#endif
static int dahdi_setlinear(int dfd, int linear)
{
int res;
res = ioctl(dfd, DAHDI_SETLINEAR, &linear);
if (res)
return res;
return 0;
}
static int alloc_sub(struct dahdi_pvt *p, int x)
{
struct dahdi_bufferinfo bi;
int res;
if (p->subs[x].dfd >= 0) {
ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
return -1;
}
p->subs[x].dfd = dahdi_open("/dev/dahdi/pseudo");
if (p->subs[x].dfd <= -1) {
ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
return -1;
}
res = ioctl(p->subs[x].dfd, DAHDI_GET_BUFINFO, &bi);
if (!res) {
bi.txbufpolicy = p->buf_policy;
bi.rxbufpolicy = p->buf_policy;
bi.numbufs = p->buf_no;
res = ioctl(p->subs[x].dfd, DAHDI_SET_BUFINFO, &bi);
if (res < 0) {
ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", x, strerror(errno));
}
} else
ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
if (ioctl(p->subs[x].dfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].dfd, strerror(errno));
dahdi_close_sub(p, x);
p->subs[x].dfd = -1;
return -1;
}
ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].dfd, p->subs[x].chan);
return 0;
}
static int unalloc_sub(struct dahdi_pvt *p, int x)
{
if (!x) {
ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
return -1;
}
ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
dahdi_close_sub(p, x);
p->subs[x].linear = 0;
p->subs[x].chan = 0;
p->subs[x].owner = NULL;
p->subs[x].inthreeway = 0;
p->polarity = POLARITY_IDLE;
memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
return 0;
}
static int digit_to_dtmfindex(char digit)
{
if (isdigit(digit))
return DAHDI_TONE_DTMF_BASE + (digit - '0');
else if (digit >= 'A' && digit <= 'D')
return DAHDI_TONE_DTMF_A + (digit - 'A');
else if (digit >= 'a' && digit <= 'd')
return DAHDI_TONE_DTMF_A + (digit - 'a');
else if (digit == '*')
return DAHDI_TONE_DTMF_s;
else if (digit == '#')
return DAHDI_TONE_DTMF_p;
else
return -1;
}
static int dahdi_digit_begin(struct ast_channel *chan, char digit)
{
struct dahdi_pvt *pvt;
int idx;
int dtmf = -1;
pvt = chan->tech_pvt;
ast_mutex_lock(&pvt->lock);
idx = dahdi_get_index(chan, pvt, 0);
if ((idx != SUB_REAL) || !pvt->owner)
goto out;
#ifdef HAVE_PRI
if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
&& (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
if (pvt->setup_ack) {
if (!pri_grab(pvt, pvt->pri)) {
pri_information(pvt->pri->pri, pvt->call, digit);
pri_rel(pvt->pri);
} else
ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
} else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
int res;
ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
res = strlen(pvt->dialdest);
pvt->dialdest[res++] = digit;
pvt->dialdest[res] = '\0';
}
goto out;
}
#endif
if ((dtmf = digit_to_dtmfindex(digit)) == -1)
goto out;
if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &dtmf)) {
int res;
struct dahdi_dialoperation zo = {
.op = DAHDI_DIAL_OP_APPEND,
};
zo.dialstr[0] = 'T';
zo.dialstr[1] = digit;
zo.dialstr[2] = '\0';
if ((res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_DIAL, &zo)))
ast_log(LOG_WARNING, "Couldn't dial digit %c: %s\n", digit, strerror(errno));
else
pvt->dialing = 1;
} else {
ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
pvt->dialing = 1;
pvt->begindigit = digit;
}
out:
ast_mutex_unlock(&pvt->lock);
return 0;
}
static int dahdi_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
{
struct dahdi_pvt *pvt;
int res = 0;
int idx;
int x;
pvt = chan->tech_pvt;
ast_mutex_lock(&pvt->lock);
idx = dahdi_get_index(chan, pvt, 0);
if ((idx != SUB_REAL) || !pvt->owner || pvt->pulse)
goto out;
#ifdef HAVE_PRI
/* This means that the digit was already sent via PRI signalling */
if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
&& !pvt->begindigit)
goto out;
#endif
if (pvt->begindigit) {
x = -1;
ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &x);
pvt->dialing = 0;
pvt->begindigit = 0;
}
out:
ast_mutex_unlock(&pvt->lock);
return res;
}
static char *events[] = {
"No event",
"On hook",
"Ring/Answered",
"Wink/Flash",
"Alarm",
"No more alarm",
"HDLC Abort",
"HDLC Overrun",
"HDLC Bad FCS",
"Dial Complete",
"Ringer On",
"Ringer Off",
"Hook Transition Complete",
"Bits Changed",
"Pulse Start",
"Timer Expired",
"Timer Ping",
"Polarity Reversal",
"Ring Begin",
};
static struct {
int alarm;
char *name;
} alarms[] = {
{ DAHDI_ALARM_RED, "Red Alarm" },
{ DAHDI_ALARM_YELLOW, "Yellow Alarm" },
{ DAHDI_ALARM_BLUE, "Blue Alarm" },
{ DAHDI_ALARM_RECOVER, "Recovering" },
{ DAHDI_ALARM_LOOPBACK, "Loopback" },
{ DAHDI_ALARM_NOTOPEN, "Not Open" },
{ DAHDI_ALARM_NONE, "None" },
};
static char *alarm2str(int alm)
{
int x;
for (x = 0; x < ARRAY_LEN(alarms); x++) {
if (alarms[x].alarm & alm)
return alarms[x].name;
}
return alm ? "Unknown Alarm" : "No Alarm";
}
static char *event2str(int event)
{
static char buf[256];
if ((event < (ARRAY_LEN(events))) && (event > -1))
return events[event];
sprintf(buf, "Event %d", event); /* safe */
return buf;
}
#ifdef HAVE_PRI
static char *dialplan2str(int dialplan)
{
if (dialplan == -1 || dialplan == -2) {
return("Dynamically set dialplan in ISDN");
}
return (pri_plan2str(dialplan));
}
#endif
static char *dahdi_sig2str(int sig)
{
static char buf[256];
switch (sig) {
case SIG_EM:
return "E & M Immediate";
case SIG_EMWINK:
return "E & M Wink";
case SIG_EM_E1:
return "E & M E1";
case SIG_FEATD:
return "Feature Group D (DTMF)";
case SIG_FEATDMF:
return "Feature Group D (MF)";
case SIG_FEATDMF_TA:
return "Feature Groud D (MF) Tandem Access";
case SIG_FEATB:
return "Feature Group B (MF)";
case SIG_E911:
return "E911 (MF)";
case SIG_FGC_CAMA:
return "FGC/CAMA (Dialpulse)";
case SIG_FGC_CAMAMF:
return "FGC/CAMA (MF)";
case SIG_FXSLS:
return "FXS Loopstart";
case SIG_FXSGS:
return "FXS Groundstart";
case SIG_FXSKS:
return "FXS Kewlstart";
case SIG_FXOLS:
return "FXO Loopstart";
case SIG_FXOGS:
return "FXO Groundstart";
case SIG_FXOKS:
return "FXO Kewlstart";
case SIG_PRI:
return "ISDN PRI";
case SIG_BRI:
return "ISDN BRI Point to Point";
case SIG_BRI_PTMP:
return "ISDN BRI Point to MultiPoint";
case SIG_SS7:
return "SS7";
case SIG_SF:
return "SF (Tone) Immediate";
case SIG_SFWINK:
return "SF (Tone) Wink";
case SIG_SF_FEATD:
return "SF (Tone) with Feature Group D (DTMF)";
case SIG_SF_FEATDMF:
return "SF (Tone) with Feature Group D (MF)";
case SIG_SF_FEATB:
return "SF (Tone) with Feature Group B (MF)";
case SIG_GR303FXOKS:
return "GR-303 with FXOKS";
case SIG_GR303FXSKS:
return "GR-303 with FXSKS";
case 0:
return "Pseudo";
default:
snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
return buf;
}
}
#define sig2str dahdi_sig2str
static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx, int slavechannel)
{
/* If the conference already exists, and we're already in it
don't bother doing anything */
struct dahdi_confinfo zi;
memset(&zi, 0, sizeof(zi));
zi.chan = 0;
if (slavechannel > 0) {
/* If we have only one slave, do a digital mon */
zi.confmode = DAHDI_CONF_DIGITALMON;
zi.confno = slavechannel;
} else {
if (!idx) {
/* Real-side and pseudo-side both participate in conference */
zi.confmode = DAHDI_CONF_REALANDPSEUDO | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER |
DAHDI_CONF_PSEUDO_TALKER | DAHDI_CONF_PSEUDO_LISTENER;
} else
zi.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
zi.confno = p->confno;
}
if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
return 0;
if (c->dfd < 0)
return 0;
if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d: %s\n", c->dfd, zi.confmode, zi.confno, strerror(errno));
return -1;
}
if (slavechannel < 1) {
p->confno = zi.confno;
}
memcpy(&c->curconf, &zi, sizeof(c->curconf));
ast_debug(1, "Added %d to conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
return 0;
}
static int isourconf(struct dahdi_pvt *p, struct dahdi_subchannel *c)
{
/* If they're listening to our channel, they're ours */
if ((p->channel == c->curconf.confno) && (c->curconf.confmode == DAHDI_CONF_DIGITALMON))
return 1;
/* If they're a talker on our (allocated) conference, they're ours */
if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & DAHDI_CONF_TALKER))
return 1;
return 0;
}
static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx)
{
struct dahdi_confinfo zi;
if (/* Can't delete if there's no dfd */
(c->dfd < 0) ||
/* Don't delete from the conference if it's not our conference */
!isourconf(p, c)
/* Don't delete if we don't think it's conferenced at all (implied) */
) return 0;
memset(&zi, 0, sizeof(zi));
if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d: %s\n", c->dfd, c->curconf.confmode, c->curconf.confno, strerror(errno));
return -1;
}
ast_debug(1, "Removed %d from conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
memcpy(&c->curconf, &zi, sizeof(c->curconf));
return 0;
}
static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out)
{
int x;
int useslavenative;
struct dahdi_pvt *slave = NULL;
/* Start out optimistic */
useslavenative = 1;
/* Update conference state in a stateless fashion */
for (x = 0; x < 3; x++) {
/* Any three-way calling makes slave native mode *definitely* out
of the question */
if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway)
useslavenative = 0;
}
/* If we don't have any 3-way calls, check to see if we have
precisely one slave */
if (useslavenative) {
for (x = 0; x < MAX_SLAVES; x++) {
if (p->slaves[x]) {
if (slave) {
/* Whoops already have a slave! No
slave native and stop right away */
slave = NULL;
useslavenative = 0;
break;
} else {
/* We have one slave so far */
slave = p->slaves[x];
}
}
}
}
/* If no slave, slave native definitely out */
if (!slave)
useslavenative = 0;
else if (slave->law != p->law) {
useslavenative = 0;
slave = NULL;
}
if (out)
*out = slave;
return useslavenative;
}
static int reset_conf(struct dahdi_pvt *p)
{
p->confno = -1;
memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
if (p->subs[SUB_REAL].dfd > -1) {
struct dahdi_confinfo zi;
memset(&zi, 0, sizeof(zi));
if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &zi))
ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d: %s\n", p->channel, strerror(errno));
}
return 0;
}
static int update_conf(struct dahdi_pvt *p)
{
int needconf = 0;
int x;
int useslavenative;
struct dahdi_pvt *slave = NULL;
useslavenative = isslavenative(p, &slave);
/* Start with the obvious, general stuff */
for (x = 0; x < 3; x++) {
/* Look for three way calls */
if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway) {
conf_add(p, &p->subs[x], x, 0);
needconf++;
} else {
conf_del(p, &p->subs[x], x);
}
}
/* If we have a slave, add him to our conference now. or DAX
if this is slave native */
for (x = 0; x < MAX_SLAVES; x++) {
if (p->slaves[x]) {
if (useslavenative)
conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
else {
conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
needconf++;
}
}
}
/* If we're supposed to be in there, do so now */
if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
if (useslavenative)
conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
else {
conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
needconf++;
}
}
/* If we have a master, add ourselves to his conference */
if (p->master) {
if (isslavenative(p->master, NULL)) {
conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
} else {
conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
}
}
if (!needconf) {
/* Nobody is left (or should be left) in our conference.
Kill it. */
p->confno = -1;
}
ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
return 0;
}
static void dahdi_enable_ec(struct dahdi_pvt *p)
{
int x;
int res;
if (!p)
return;
if (p->echocanon) {
ast_debug(1, "Echo cancellation already on\n");
return;
}
if (p->digital) {
ast_debug(1, "Echo cancellation isn't required on digital connection\n");
return;
}
if (p->echocancel.head.tap_length) {
if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
x = 1;
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x);
if (res)
ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n", p->channel, strerror(errno));
}
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &p->echocancel);
if (res) {
ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d (%s)\n", p->channel, strerror(errno));
} else {
p->echocanon = 1;
ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
}
} else
ast_debug(1, "No echo cancellation requested\n");
}
static void dahdi_train_ec(struct dahdi_pvt *p)
{
int x;
int res;
if (p && p->echocanon && p->echotraining) {
x = p->echotraining;
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOTRAIN, &x);
if (res)
ast_log(LOG_WARNING, "Unable to request echo training on channel %d: %s\n", p->channel, strerror(errno));
else
ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
} else {
ast_debug(1, "No echo training requested\n");
}
}
static void dahdi_disable_ec(struct dahdi_pvt *p)
{
int res;
if (p->echocanon) {
struct dahdi_echocanparams ecp = { .tap_length = 0 };
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &ecp);
if (res)
ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d: %s\n", p->channel, strerror(errno));
else
ast_debug(1, "Disabled echo cancellation on channel %d\n", p->channel);
}
p->echocanon = 0;
}
static void fill_txgain(struct dahdi_gains *g, float gain, int law)
{
int j;
int k;
float linear_gain = pow(10.0, gain / 20.0);
switch (law) {
case DAHDI_LAW_ALAW:
for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
if (gain) {
k = (int) (((float) AST_ALAW(j)) * linear_gain);
if (k > 32767) k = 32767;
if (k < -32767) k = -32767;
g->txgain[j] = AST_LIN2A(k);
} else {
g->txgain[j] = j;
}
}
break;
case DAHDI_LAW_MULAW:
for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
if (gain) {
k = (int) (((float) AST_MULAW(j)) * linear_gain);
if (k > 32767) k = 32767;
if (k < -32767) k = -32767;
g->txgain[j] = AST_LIN2MU(k);
} else {
g->txgain[j] = j;
}
}
break;
}
}
static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
{
int j;
int k;
float linear_gain = pow(10.0, gain / 20.0);
switch (law) {
case DAHDI_LAW_ALAW:
for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
if (gain) {
k = (int) (((float) AST_ALAW(j)) * linear_gain);
if (k > 32767) k = 32767;
if (k < -32767) k = -32767;
g->rxgain[j] = AST_LIN2A(k);
} else {
g->rxgain[j] = j;
}
}
break;
case DAHDI_LAW_MULAW:
for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
if (gain) {
k = (int) (((float) AST_MULAW(j)) * linear_gain);
if (k > 32767) k = 32767;
if (k < -32767) k = -32767;
g->rxgain[j] = AST_LIN2MU(k);
} else {
g->rxgain[j] = j;
}
}
break;
}
}
static int set_actual_txgain(int fd, int chan, float gain, int law)
{
struct dahdi_gains g;
int res;
memset(&g, 0, sizeof(g));
g.chan = chan;
res = ioctl(fd, DAHDI_GETGAINS, &g);
if (res) {
ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
return res;
}
fill_txgain(&g, gain, law);
return ioctl(fd, DAHDI_SETGAINS, &g);
}
static int set_actual_rxgain(int fd, int chan, float gain, int law)
{
struct dahdi_gains g;
int res;
memset(&g, 0, sizeof(g));
g.chan = chan;
res = ioctl(fd, DAHDI_GETGAINS, &g);
if (res) {
ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
return res;
}
fill_rxgain(&g, gain, law);
return ioctl(fd, DAHDI_SETGAINS, &g);
}
static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
{
return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
}
static int bump_gains(struct dahdi_pvt *p)
{
int res;
/* Bump receive gain by value stored in cid_rxgain */
res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
if (res) {
ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
return -1;
}
return 0;
}
static int restore_gains(struct dahdi_pvt *p)
{
int res;
res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
if (res) {
ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
return -1;
}
return 0;
}
static inline int dahdi_set_hook(int fd, int hs)
{
int x, res;
x = hs;
res = ioctl(fd, DAHDI_HOOK, &x);
if (res < 0) {
if (errno == EINPROGRESS)
return 0;
ast_log(LOG_WARNING, "DAHDI hook failed returned %d (trying %d): %s\n", res, hs, strerror(errno));
/* will expectedly fail if phone is off hook during operation, such as during a restart */
}
return res;
}
static inline int dahdi_confmute(struct dahdi_pvt *p, int muted)
{
int x, y, res;
x = muted;
if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
y = 1;
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &y);
if (res)
ast_log(LOG_WARNING, "Unable to set audio mode on %d: %s\n", p->channel, strerror(errno));
}
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_CONFMUTE, &x);
if (res < 0)
ast_log(LOG_WARNING, "DAHDI confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
return res;
}
static int save_conference(struct dahdi_pvt *p)
{
struct dahdi_confinfo c;
int res;
if (p->saveconf.confmode) {
ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
return -1;
}
p->saveconf.chan = 0;
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &p->saveconf);
if (res) {
ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
p->saveconf.confmode = 0;
return -1;
}
memset(&c, 0, sizeof(c));
c.confmode = DAHDI_CONF_NORMAL;
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &c);
if (res) {
ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
return -1;
}
ast_debug(1, "Disabled conferencing\n");
return 0;
}
/*!
* \brief Send MWI state change
*
* \arg mailbox_full This is the mailbox associated with the FXO line that the
* MWI state has changed on.
* \arg thereornot This argument should simply be set to 1 or 0, to indicate
* whether there are messages waiting or not.
*
* \return nothing
*
* This function does two things:
*
* 1) It generates an internal Asterisk event notifying any other module that
* cares about MWI that the state of a mailbox has changed.
*
* 2) It runs the script specified by the mwimonitornotify option to allow
* some custom handling of the state change.
*/
static void notify_message(char *mailbox_full, int thereornot)
{
char s[sizeof(mwimonitornotify) + 80];
struct ast_event *event;
char *mailbox, *context;
/* Strip off @default */
context = mailbox = ast_strdupa(mailbox_full);
strsep(&context, "@");
if (ast_strlen_zero(context))
context = "default";
if (!(event = ast_event_new(AST_EVENT_MWI,
AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
AST_EVENT_IE_END))) {
return;
}
ast_event_queue_and_cache(event);
if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
ast_safe_system(s);
}
}
static int restore_conference(struct dahdi_pvt *p)
{
int res;
if (p->saveconf.confmode) {
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &p->saveconf);
p->saveconf.confmode = 0;
if (res) {
ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
return -1;
}
}
ast_debug(1, "Restored conferencing\n");
return 0;
}
static int send_callerid(struct dahdi_pvt *p);
static int send_cwcidspill(struct dahdi_pvt *p)
{
p->callwaitcas = 0;
p->cidcwexpire = 0;
if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
return -1;
p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
/* Make sure we account for the end */
p->cidlen += READ_SIZE * 4;
p->cidpos = 0;
send_callerid(p);
ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
return 0;
}
static int has_voicemail(struct dahdi_pvt *p)
{
int new_msgs;
struct ast_event *event;
char *mailbox, *context;
mailbox = context = ast_strdupa(p->mailbox);
strsep(&context, "@");
if (ast_strlen_zero(context))
context = "default";
event = ast_event_get_cached(AST_EVENT_MWI,
AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
AST_EVENT_IE_END);
if (event) {
new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
ast_event_destroy(event);
} else
new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
return new_msgs;
}
static int send_callerid(struct dahdi_pvt *p)
{
/* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
int res;
/* Take out of linear mode if necessary */
if (p->subs[SUB_REAL].linear) {
p->subs[SUB_REAL].linear = 0;
dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
}
while (p->cidpos < p->cidlen) {
res = write(p->subs[SUB_REAL].dfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
if (res < 0) {
if (errno == EAGAIN)
return 0;
else {
ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
return -1;
}
}
if (!res)
return 0;
p->cidpos += res;
}
ast_free(p->cidspill);
p->cidspill = NULL;
if (p->callwaitcas) {
/* Wait for CID/CW to expire */
p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
} else
restore_conference(p);
return 0;
}
static int dahdi_callwait(struct ast_channel *ast)
{
struct dahdi_pvt *p = ast->tech_pvt;
p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
if (p->cidspill) {
ast_log(LOG_WARNING, "Spill already exists?!?\n");
ast_free(p->cidspill);
}
if (!(p->cidspill = ast_malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4)))
return -1;
save_conference(p);
/* Silence */
memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
if (!p->callwaitrings && p->callwaitingcallerid) {
ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
p->callwaitcas = 1;
p->cidlen = 2400 + 680 + READ_SIZE * 4;
} else {
ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
p->callwaitcas = 0;
p->cidlen = 2400 + READ_SIZE * 4;
}
p->cidpos = 0;
send_callerid(p);
return 0;
}
#ifdef HAVE_SS7
static unsigned char cid_pres2ss7pres(int cid_pres)
{
return (cid_pres >> 5) & 0x03;
}
static unsigned char cid_pres2ss7screen(int cid_pres)
{
return cid_pres & 0x03;
}
#endif
static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout)
{
struct dahdi_pvt *p = ast->tech_pvt;
int x, res, idx,mysig;
char *c, *n, *l;
#ifdef HAVE_PRI
char *s = NULL;
#endif
char dest[256]; /* must be same length as p->dialdest */
ast_mutex_lock(&p->lock);
ast_copy_string(dest, rdest, sizeof(dest));
ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
if ((ast->_state == AST_STATE_BUSY)) {
p->subs[SUB_REAL].needbusy = 1;
ast_mutex_unlock(&p->lock);
return 0;
}
if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
ast_log(LOG_WARNING, "dahdi_call called on %s, neither down nor reserved\n", ast->name);
ast_mutex_unlock(&p->lock);
return -1;
}
p->dialednone = 0;
if ((p->radio || (p->oprmode < 0))) /* if a radio channel, up immediately */
{
/* Special pseudo -- automatically up */
ast_setstate(ast, AST_STATE_UP);
ast_mutex_unlock(&p->lock);
return 0;
}
x = DAHDI_FLUSH_READ | DAHDI_FLUSH_WRITE;
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
if (res)
ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", p->channel, strerror(errno));
p->outgoing = 1;
if (IS_DIGITAL(ast->transfercapability)){
set_actual_gain(p->subs[SUB_REAL].dfd, 0, 0, 0, p->law);
} else {
set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
}
mysig = p->sig;
if (p->outsigmod > -1)
mysig = p->outsigmod;
switch (mysig) {
case SIG_FXOLS:
case SIG_FXOGS:
case SIG_FXOKS:
if (p->owner == ast) {
/* Normal ring, on hook */
/* Don't send audio while on hook, until the call is answered */
p->dialing = 1;
if (p->use_callerid) {
/* Generate the Caller-ID spill if desired */
if (p->cidspill) {
ast_log(LOG_WARNING, "cidspill already exists??\n");
ast_free(p->cidspill);
}
p->callwaitcas = 0;
if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
p->cidpos = 0;
send_callerid(p);
}
}
/* Choose proper cadence */
if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
p->cidrings = cidrings[p->distinctivering - 1];
} else {
if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
p->cidrings = p->sendcalleridafter;
}
/* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
c = strchr(dest, '/');
if (c)
c++;
if (c && (strlen(c) < p->stripmsd)) {
ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
c = NULL;
}
if (c) {
p->dop.op = DAHDI_DIAL_OP_REPLACE;
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
} else {
p->dop.dialstr[0] = '\0';
}
x = DAHDI_RING;
if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x) && (errno != EINPROGRESS)) {
ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
ast_mutex_unlock(&p->lock);
return -1;
}
p->dialing = 1;
} else {
/* Call waiting call */
p->callwaitrings = 0;
if (ast->cid.cid_num)
ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
else
p->callwait_num[0] = '\0';
if (ast->cid.cid_name)
ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
else
p->callwait_name[0] = '\0';
/* Call waiting tone instead */
if (dahdi_callwait(ast)) {
ast_mutex_unlock(&p->lock);
return -1;
}
/* Make ring-back */
if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].dfd, DAHDI_TONE_RINGTONE))
ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
}
n = ast->cid.cid_name;
l = ast->cid.cid_num;
if (l)
ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
else
p->lastcid_num[0] = '\0';
if (n)
ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
else
p->lastcid_name[0] = '\0';
ast_setstate(ast, AST_STATE_RINGING);
idx = dahdi_get_index(ast, p, 0);
if (idx > -1) {
p->subs[idx].needringing = 1;
}
break;
case SIG_FXSLS:
case SIG_FXSGS:
case SIG_FXSKS:
if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
p->polaritydelaytv = ast_tvnow();
}
/* fall through */
case SIG_EMWINK:
case SIG_EM:
case SIG_EM_E1:
case SIG_FEATD:
case SIG_FEATDMF:
case SIG_E911:
case SIG_FGC_CAMA:
case SIG_FGC_CAMAMF:
case SIG_FEATB:
case SIG_SFWINK:
case SIG_SF:
case SIG_SF_FEATD:
case SIG_SF_FEATDMF:
case SIG_FEATDMF_TA:
case SIG_SF_FEATB:
c = strchr(dest, '/');
if (c)
c++;
else
c = "";
if (strlen(c) < p->stripmsd) {
ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
ast_mutex_unlock(&p->lock);
return -1;
}
#ifdef HAVE_PRI
/* Start the trunk, if not GR-303 */
if (!p->pri) {
#endif
x = DAHDI_START;
res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
if (res < 0) {
if (errno != EINPROGRESS) {
ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
ast_mutex_unlock(&p->lock);
return -1;
}
}
#ifdef HAVE_PRI
}
#endif
ast_debug(1, "Dialing '%s'\n", c);
p->dop.op = DAHDI_DIAL_OP_REPLACE;
c += p->stripmsd;
switch (mysig) {
case SIG_FEATD:
l = ast->cid.cid_num;
if (l)
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
else
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
break;
case SIG_FEATDMF:
l = ast->cid.cid_num;
if (l)
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
else
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
break;
case SIG_FEATDMF_TA:
{
const char *cic, *ozz;
/* If you have to go through a Tandem Access point you need to use this */
ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
if (!ozz)
ozz = defaultozz;
cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
if (!cic)
cic = defaultcic;
if (!ozz || !cic) {
ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
ast_mutex_unlock(&p->lock);
return -1;
}
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
p->whichwink = 0;
}
break;
case SIG_E911:
ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
break;
case SIG_FGC_CAMA:
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
break;
case SIG_FGC_CAMAMF:
case SIG_FEATB:
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
break;
default:
if (p->pulse)
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
else
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
break;
}
if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
memset(p->echorest, 'w', sizeof(p->echorest) - 1);
strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
p->echorest[sizeof(p->echorest) - 1] = '\0';
p->echobreak = 1;
p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
} else
p->echobreak = 0;
if (!res) {
if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
int saveerr = errno;
x = DAHDI_ONHOOK;
ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
ast_mutex_unlock(&p->lock);
return -1;
}
} else
ast_debug(1, "Deferring dialing...\n");
p->dialing = 1;
if (ast_strlen_zero(c))
p->dialednone = 1;
ast_setstate(ast, AST_STATE_DIALING);
break;
case 0:
/* Special pseudo -- automatically up*/
ast_setstate(ast, AST_STATE_UP);
break;
case SIG_PRI:
case SIG_BRI:
case SIG_BRI_PTMP:
case SIG_SS7:
/* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
p->dialdest[0] = '\0';
p->dialing = 1;
break;
default:
ast_debug(1, "not yet implemented\n");
ast_mutex_unlock(&p->lock);
return -1;
}
#ifdef HAVE_SS7
if (p->ss7) {
char ss7_called_nai;
int called_nai_strip;
char ss7_calling_nai;
int calling_nai_strip;
const char *charge_str = NULL;
const char *gen_address = NULL;
const char *gen_digits = NULL;
const char *gen_dig_type = NULL;
const char *gen_dig_scheme = NULL;
const char *gen_name = NULL;
const char *jip_digits = NULL;
const char *lspi_ident = NULL;
const char *rlt_flag = NULL;
const char *call_ref_id = NULL;
const char *call_ref_pc = NULL;
const char *send_far = NULL;
c = strchr(dest, '/');
if (c) {
c++;
} else {
c = "";
}
if (strlen(c) < p->stripmsd) {
ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
ast_mutex_unlock(&p->lock);
return -1;
}
if (!p->hidecallerid) {
l = ast->cid.cid_num;
} else {
l = NULL;
}
if (ss7_grab(p, p->ss7)) {
ast_log(LOG_WARNING, "Failed to grab SS7!\n");
ast_mutex_unlock(&p->lock);
return -1;
}
p->digital = IS_DIGITAL(ast->transfercapability);
p->ss7call = isup_new_call(p->ss7->ss7);
if (!p->ss7call) {
ss7_rel(p->ss7);
ast_mutex_unlock(&p->lock);
ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
return -1;
}
called_nai_strip = 0;
ss7_called_nai = p->ss7->called_nai;
if (ss7_called_nai == SS7_NAI_DYNAMIC) { /* compute dynamically */
if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
called_nai_strip = strlen(p->ss7->internationalprefix);
ss7_called_nai = SS7_NAI_INTERNATIONAL;
} else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
called_nai_strip = strlen(p->ss7->nationalprefix);
ss7_called_nai = SS7_NAI_NATIONAL;
} else {
ss7_called_nai = SS7_NAI_SUBSCRIBER;
}
}
isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
calling_nai_strip = 0;
ss7_calling_nai = p->ss7->calling_nai;
if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) { /* compute dynamically */
if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
calling_nai_strip = strlen(p->ss7->internationalprefix);
ss7_calling_nai = SS7_NAI_INTERNATIONAL;
} else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
calling_nai_strip = strlen(p->ss7->nationalprefix);
ss7_calling_nai = SS7_NAI_NATIONAL;
} else {
ss7_calling_nai = SS7_NAI_SUBSCRIBER;
}
}
isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
isup_set_oli(p->ss7call, ast->cid.cid_ani2);
isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
ast_channel_lock(ast);
/* Set the charge number if it is set */
charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
if (charge_str)
isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
if (gen_address)
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 */
gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
if (gen_digits)
isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
if (gen_name)
isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
if (jip_digits)
isup_set_jip_digits(p->ss7call, jip_digits);
lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
if (lspi_ident)
isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00); /* Setting for Nortel DMS-250/500 */
}
call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
if (call_ref_id && call_ref_pc) {
isup_set_callref(p->ss7call, atoi(call_ref_id),
call_ref_pc ? atoi(call_ref_pc) : 0);
}
send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
(isup_far(p->ss7->ss7, p->ss7call));
ast_channel_unlock(ast);
isup_iam(p->ss7->ss7, p->ss7call);
ast_setstate(ast, AST_STATE_DIALING);
ss7_rel(p->ss7);
}
#endif /* HAVE_SS7 */
#ifdef HAVE_PRI
if (p->pri) {
struct pri_sr *sr;
#ifdef SUPPORT_USERUSER
const char *useruser;
#endif
int pridialplan;
int dp_strip;
int prilocaldialplan;
int ldp_strip;
int exclusive;
const char *rr_str;
int redirect_reason;
c = strchr(dest, '/');
if (c) {
c++;
} else {
c = "";
}
l = NULL;
n = NULL;
if (!p->hidecallerid) {
l = ast->cid.cid_num;
if (!p->hidecalleridname) {
n = ast->cid.cid_name;
}
}
if (strlen(c) < p->stripmsd) {
ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
ast_mutex_unlock(&p->lock);
return -1;
}
if (mysig != SIG_FXSKS) {
p->dop.op = DAHDI_DIAL_OP_REPLACE;
s = strchr(c + p->stripmsd, 'w');
if (s) {
if (strlen(s) > 1)
snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
else
p->dop.dialstr[0] = '\0';
*s = '\0';
} else {
p->dop.dialstr[0] = '\0';
}
}
if (pri_grab(p, p->pri)) {
ast_log(LOG_WARNING, "Failed to grab PRI!\n");
ast_mutex_unlock(&p->lock);
return -1;
}
if (!(p->call = pri_new_call(p->pri->pri))) {
ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
pri_rel(p->pri);
ast_mutex_unlock(&p->lock);
return -1;
}
if (!(sr = pri_sr_new())) {
ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
pri_rel(p->pri);
ast_mutex_unlock(&p->lock);
}
if (p->bearer || (mysig == SIG_FXSKS)) {
if (p->bearer) {
ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
p->bearer->call = p->call;
} else
ast_debug(1, "I'm being setup with no bearer right now...\n");
pri_set_crv(p->pri->pri, p->call, p->channel, 0);
}
p->digital = IS_DIGITAL(ast->transfercapability);
/* Should the picked channel be used exclusively? */
if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
exclusive = 1;
} else {
exclusive = 0;
}
pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
(p->digital ? -1 :
((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
if (p->pri->facilityenable)
pri_facility_enable(p->pri->pri);
ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
dp_strip = 0;
pridialplan = p->pri->dialplan - 1;
if (pridialplan == -2 || pridialplan == -3) { /* compute dynamically */
if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
if (pridialplan == -2) {
dp_strip = strlen(p->pri->internationalprefix);
}
pridialplan = PRI_INTERNATIONAL_ISDN;
} else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
if (pridialplan == -2) {
dp_strip = strlen(p->pri->nationalprefix);
}
pridialplan = PRI_NATIONAL_ISDN;
} else {
pridialplan = PRI_LOCAL_ISDN;
}
}
while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
switch (c[p->stripmsd]) {
case 'U':
pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
break;
case 'I':
pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
break;
case 'N':
pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
break;
case 'L':
pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
break;
case 'S':
pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
break;
case 'V':
pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
break;
case 'R':
pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
break;
case 'u':
pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
break;
case 'e':
pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
break;
case 'x':
pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
break;
case 'f':
pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
break;
case 'n':
pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
break;
case 'p':
pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
break;
case 'r':
pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
break;
default:
if (isalpha(c[p->stripmsd])) {
ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
}
break;
}
c++;
}
pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
ldp_strip = 0;
prilocaldialplan = p->pri->localdialplan - 1;
if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) { /* compute dynamically */
if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
if (prilocaldialplan == -2) {
ldp_strip = strlen(p->pri->internationalprefix);
}
prilocaldialplan = PRI_INTERNATIONAL_ISDN;
} else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
if (prilocaldialplan == -2) {
ldp_strip = strlen(p->pri->nationalprefix);
}
prilocaldialplan = PRI_NATIONAL_ISDN;
} else {
prilocaldialplan = PRI_LOCAL_ISDN;
}
}
if (l != NULL) {
while (*l > '9' && *l != '*' && *l != '#') {
switch (*l) {
case 'U':
prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
break;
case 'I':
prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
break;
case 'N':
prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
break;
case 'L':
prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
break;
case 'S':
prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
break;
case 'V':
prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
break;
case 'R':
prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
break;
case 'u':
prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
break;
case 'e':
prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
break;
case 'x':
prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
break;
case 'f':
prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
break;
case 'n':
prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
break;
case 'p':
prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
break;
case 'r':
prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
break;
default:
if (isalpha(*l)) {
ast_log(LOG_WARNING,
"Unrecognized prilocaldialplan %s modifier: %c\n",
*l > 'Z' ? "NPI" : "TON", *l);
}
break;
}
l++;
}
}
pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
if (!strcasecmp(rr_str, "UNKNOWN"))
redirect_reason = 0;
else if (!strcasecmp(rr_str, "BUSY"))
redirect_reason = 1;
else if (!strcasecmp(rr_str, "NO_REPLY") || !strcasecmp(rr_str, "NOANSWER"))
/* the NOANSWER is to match diversion-reason from chan_sip, (which never reads PRIREDIRECTREASON) */
redirect_reason = 2;
else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
redirect_reason = 15;
else
redirect_reason = PRI_REDIR_UNCONDITIONAL;
} else
redirect_reason = PRI_REDIR_UNCONDITIONAL;
pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
#ifdef SUPPORT_USERUSER
/* User-user info */
useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
if (useruser)
pri_sr_set_useruser(sr, useruser);
#endif
if (pri_setup(p->pri->pri, p->call, sr)) {
ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
pri_rel(p->pri);
ast_mutex_unlock(&p->lock);
pri_sr_free(sr);
return -1;
}
pri_sr_free(sr);
ast_setstate(ast, AST_STATE_DIALING);
pri_rel(p->pri);
}
#endif
ast_mutex_unlock(&p->lock);
return 0;
}
static void destroy_dahdi_pvt(struct dahdi_pvt **pvt)
{
struct dahdi_pvt *p = *pvt;
/* Remove channel from the list */
if (p->prev)
p->prev->next = p->next;
if (p->next)
p->next->prev = p->prev;
if (p->use_smdi)
ast_smdi_interface_unref(p->smdi_iface);
if (p->mwi_event_sub)
ast_event_unsubscribe(p->mwi_event_sub);
if (p->vars) {
ast_variables_destroy(p->vars);
}
ast_mutex_destroy(&p->lock);
dahdi_close_sub(p, SUB_REAL);
if (p->owner)
p->owner->tech_pvt = NULL;
free(p);
*pvt = NULL;
}
static int destroy_channel(struct dahdi_pvt *prev, struct dahdi_pvt *cur, int now)
{
int owned = 0;
int i = 0;
if (!now) {
if (cur->owner) {
owned = 1;
}
for (i = 0; i < 3; i++) {
if (cur->subs[i].owner) {
owned = 1;
}
}
if (!owned) {
if (prev) {
prev->next = cur->next;
if (prev->next)
prev->next->prev = prev;
else
ifend = prev;
} else {
iflist = cur->next;
if (iflist)
iflist->prev = NULL;
else
ifend = NULL;
}
destroy_dahdi_pvt(&cur);
}
} else {
if (prev) {
prev->next = cur->next;
if (prev->next)
prev->next->prev = prev;
else
ifend = prev;
} else {
iflist = cur->next;
if (iflist)
iflist->prev = NULL;
else
ifend = NULL;
}
destroy_dahdi_pvt(&cur);
}
return 0;
}
static void destroy_all_channels(void)
{
int x;
struct dahdi_pvt *p, *pl;
while (num_restart_pending) {
usleep(1);
}
ast_mutex_lock(&iflock);
/* Destroy all the interfaces and free their memory */
p = iflist;
while (p) {
/* Free any callerid */
if (p->cidspill)
ast_free(p->cidspill);
pl = p;
p = p->next;
x = pl->channel;
/* Free associated memory */
if (pl)
destroy_dahdi_pvt(&pl);
if (option_verbose > 2)
ast_verbose(VERBOSE_PREFIX_2 "Unregistered channel %d\n", x);
}
iflist = NULL;
ifcount = 0;
ast_mutex_unlock(&iflock);
}
#ifdef HAVE_PRI
static char *dahdi_send_keypad_facility_app = "DAHDISendKeypadFacility";
static char *dahdi_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
static char *dahdi_send_keypad_facility_descrip =
" DAHDISendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
" IE over the current channel.\n";
static int dahdi_send_keypad_facility_exec(struct ast_channel *chan, void *data)
{
/* Data will be our digit string */
struct dahdi_pvt *p;
char *digits = (char *) data;
if (ast_strlen_zero(digits)) {
ast_debug(1, "No digit string sent to application!\n");
return -1;
}
p = (struct dahdi_pvt *)chan->tech_pvt;
if (!p) {
ast_debug(1, "Unable to find technology private\n");
return -1;
}
ast_mutex_lock(&p->lock);
if (!p->pri || !p->call) {
ast_debug(1, "Unable to find pri or call on channel!\n");
ast_mutex_unlock(&p->lock);
return -1;
}
if (!pri_grab(p, p->pri)) {
pri_keypad_facility(p->pri->pri, p->call, digits);
pri_rel(p->pri);
} else {
ast_debug(1, "Unable to grab pri to send keypad facility!\n");
ast_mutex_unlock(&p->lock);
return -1;
}
ast_mutex_unlock(&p->lock);
return 0;