/* * CallWeaver -- An open source telephony toolkit. * * Copyright (C) 1999 - 2005, Digium, Inc. * * Mark Spencer * * See http://www.callweaver.org for more information about * the CallWeaver 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. */ /* * * Zaptel Pseudo TDM interface * */ #ifdef HAVE_CONFIG_H #include "confdefs.h" #endif #include #include #include #ifdef __NetBSD__ #include #include #else #include #endif #include #include #include #include #include #include #include #include #include ZAPTEL_H #include TONEZONE_H #include #ifdef ZAPATA_PRI #include #ifndef PRI_USER_USER_TX #error "You need newer libpri" #endif #endif #include "callweaver.h" CALLWEAVER_FILE_VERSION("$HeadURL$", "$Revision$") #include "callweaver/lock.h" #include "callweaver/channel.h" #include "callweaver/config.h" #include "callweaver/logger.h" #include "callweaver/module.h" #include "callweaver/pbx.h" #include "callweaver/options.h" #include "callweaver/file.h" #include "callweaver/phone_no_utils.h" #include "callweaver/callerid.h" #include "callweaver/cli.h" #include "callweaver/cdr.h" #include "callweaver/features.h" #include "callweaver/musiconhold.h" #include "callweaver/say.h" #include "callweaver/app.h" #include "callweaver/dsp.h" #include "callweaver/callweaver_db.h" #include "callweaver/manager.h" #include "callweaver/causes.h" #include "callweaver/utils.h" #include "callweaver/transcap.h" #include "callweaver/callweaver_pcm.h" #include "callweaver/generic_jb.h" #include "callweaver_addon/adsi.h" static struct cw_jb_conf global_jbconf; #if !defined(FALSE) #define FALSE 0 #endif #if !defined(TRUE) #define TRUE (!FALSE) #endif #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_PRI) && !defined(ZT_SIG_HARDHDLC)) #error "Your zaptel is too old. please update" #endif #ifndef ZT_TONEDETECT /* Work around older code with no tone detect */ #define ZT_EVENT_DTMFDOWN 0 #define ZT_EVENT_DTMFUP 0 #endif /* * 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 */ /* * 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 ZAP_CHECK_HOOKSTATE */ #define CHANNEL_PSEUDO -12 #define CW_LAW(p) (((p)->law == ZT_LAW_MULAW) ? CW_FORMAT_ULAW : CW_FORMAT_ALAW) /* 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_FEATB)) static const char desc[] = "DAHDI Telephony" #ifdef ZAPATA_PRI " w/PRI" #endif ; static const char tdesc[] = "DAHDI Telephony Driver" #ifdef ZAPATA_PRI " w/PRI" #endif ; static const char dahdi_type[] = "DAHDI"; static const char zap_type[] = "Zap"; static const char config_zapata[] = "zapata.conf"; static const char config_dahdi[] = "chan_dahdi.conf"; #define SIG_EM ZT_SIG_EM #define SIG_EMWINK (0x0100000 | ZT_SIG_EM) #define SIG_FEATD (0x0200000 | ZT_SIG_EM) #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM) #define SIG_FEATB (0x0800000 | ZT_SIG_EM) #define SIG_E911 (0x1000000 | ZT_SIG_EM) #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM) #define SIG_FXSLS ZT_SIG_FXSLS #define SIG_FXSGS ZT_SIG_FXSGS #define SIG_FXSKS ZT_SIG_FXSKS #define SIG_FXOLS ZT_SIG_FXOLS #define SIG_FXOGS ZT_SIG_FXOGS #define SIG_FXOKS ZT_SIG_FXOKS #define SIG_PRI ZT_SIG_CLEAR #define SIG_SF ZT_SIG_SF #define SIG_SFWINK (0x0100000 | ZT_SIG_SF) #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF) #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF) #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF) #define SIG_EM_E1 ZT_SIG_EM_E1 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS) #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS) #define CID_START_RING 1 #define CID_START_POLARITY 2 #define CID_START_IDLE 3 #define NUM_SPANS 32 #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) static char context[CW_MAX_CONTEXT] = "default"; static char exten[CW_MAX_EXTENSION] = ""; static char cid_num[256] = ""; static char cid_name[256] = ""; static char defaultcic[64] = ""; static char defaultozz[64] = ""; static char language[MAX_LANGUAGE] = ""; static char musicclass[MAX_MUSICCLASS] = ""; static char progzone[10]= ""; static int usedistinctiveringdetection = 1; static int distinctiveringaftercid = 1; static int transfertobusy = 1; static int use_callerid = 1; static int cid_signalling = ADSI_STANDARD_CLIP; static int cid_start = CID_START_POLARITY; static int polarity_events = 1; static int zaptrcallerid = 0; static int cur_signalling = -1; static cw_group_t cur_group = 0; static cw_group_t cur_callergroup = 0; static cw_group_t cur_pickupgroup = 0; static int relaxdtmf = 0; static int immediate = 0; static int stripmsd = 0; static int callwaiting = 0; static int callwaitingcallerid = 0; static int hidecallerid = 0; static int restrictcid = 0; static int use_callingpres = 0; static int callreturn = 0; static int threewaycalling = 0; static int transfer = 0; static int canpark = 0; static int cancallforward = 0; static float cid_rxgain = 5.0; static float rxgain = 0.0; static float txgain = 0.0; static int tonezone = -1; static int echocancel; static int echotraining; static int pulse; static int echocanbridged = 0; static int busydetect = 0; static int busycount = 3; static int busy_tonelength = 0; static int busy_quietlength = 0; static int callprogress = 0; static char accountcode[CW_MAX_ACCOUNT_CODE] = ""; static char mailbox[CW_MAX_EXTENSION]; static int amaflags = 0; static int adsi = 0; static int numbufs = 4; static int cur_prewink = -1; static int cur_preflash = -1; static int cur_wink = -1; static int cur_flash = -1; static int cur_start = -1; static int cur_rxwink = -1; static int cur_rxflash = -1; static int cur_debounce = -1; static int cur_priexclusive = 0; static int priindication_oob = 0; #ifdef ZAPATA_PRI static int minunused = 2; static int minidle = 0; static char idleext[CW_MAX_EXTENSION]; static char idledial[CW_MAX_EXTENSION]; static int overlapdial = 0; static int facilityenable = 0; static char internationalprefix[10] = ""; static char nationalprefix[10] = ""; static char localprefix[20] = ""; static char privateprefix[20] = ""; static char unknownprefix[20] = ""; static long resetinterval = 3600; /* How often (in seconds) to reset unused channels. Default 1 hour. */ static struct cw_channel inuse = { "GR-303InUse" }; #ifdef PRI_GETSET_TIMERS static int pritimers[PRI_MAX_TIMERS]; #endif static int pridebugfd = -1; static char pridebugfilename[1024]=""; #endif /* Wait up to 16 seconds for first digit (FXO logic) */ static int firstdigittimeout = 16000; /* How long to wait for following digits (FXO logic) */ static int gendigittimeout = 8000; /* How long to wait for an extra digit, if there is an ambiguous match */ static int matchdigittimeout = 3000; static int usecnt =0; CW_MUTEX_DEFINE_STATIC(usecnt_lock); /* Protect the interface list (of zt_pvt's) */ CW_MUTEX_DEFINE_STATIC(iflock); static int ifcount = 0; #ifdef ZAPATA_PRI CW_MUTEX_DEFINE_STATIC(pridebugfdlock); #endif /* Whether we answer on a Polarity Switch event */ static int answeronpolarityswitch = 0; /* Whether we hang up on a Polarity Switch event */ static int hanguponpolarityswitch = 0; /* How long (ms) to ignore Polarity Switch events after we answer a call */ static int polarityonanswerdelay = 600; /* When to send the CallerID signals (rings) */ static int sendcalleridafter = -1; /* Protect the monitoring thread, so only one process can kill or start it, and not when it's doing something critical. */ CW_MUTEX_DEFINE_STATIC(monlock); /* 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 = CW_PTHREADT_NULL; static int restart_monitor(void); static enum cw_bridge_result zt_bridge(struct cw_channel *c0, struct cw_channel *c1, int flags, struct cw_frame **fo, struct cw_channel **rc, int timeoutms); static int zt_sendtext(struct cw_channel *c, const char *text); static inline int zt_get_event(int fd) { /* Avoid the silly zt_getevent which ignores a bunch of events */ int j; if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1; return j; } static inline int zt_wait_event(int fd) { /* Avoid the silly zt_waitevent which ignores a bunch of events */ int i,j=0; i = ZT_IOMUX_SIGEVENT; if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1; if (ioctl(fd, ZT_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 */ /* The zaptel driver currently makes this a fixed value but * has comments suggesting it may one day be variable. */ #ifndef ZT_CHUNKSIZE # define ZT_CHUNKSIZE 8 #endif /* Convert milliseconds to samples, assuming the driver is * sampling at a standard 64kbit/s. */ #define MS_TO_SAMPLES(X) ((X) * (64 / ZT_CHUNKSIZE)) #define SAMPLES_TO_MS(X) ((X) / (64 / ZT_CHUNKSIZE)) #define CALLWAITING_REPEAT_SAMPLES MS_TO_SAMPLES(10000) /* 10 s */ #define CIDCW_EXPIRE_SAMPLES MS_TO_SAMPLES(500) /* 500 ms */ #define DEFAULT_RINGT MS_TO_SAMPLES(8000) /* 8 s */ #define MIN_MS_SINCE_FLASH 2000 /* 2 s */ struct zt_pvt; static int ringt_base = DEFAULT_RINGT; #ifdef ZAPATA_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 zt_pri { pthread_t master; /* Thread of master */ cw_mutex_t lock; /* Mutex */ char idleext[CW_MAX_EXTENSION]; /* Where to idle extra calls */ char idlecontext[CW_MAX_CONTEXT]; /* What context to use for idle */ char idledial[CW_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 */ int debug; int fds[NUM_DCHANS]; /* FD's for d-channels */ int offset; int span; int resetting; int resetpos; time_t lastreset; /* time when unused channels were last reset */ long resetinterval; /* Interval (in seconds) for resetting unused channels */ struct zt_pvt *pvts[MAX_CHANNELS]; /* Member channel pvt structs */ struct zt_pvt *crvs; /* Member CRV structs */ struct zt_pvt *crvend; /* Pointer to end of CRV structs */ }; static struct zt_pri pris[NUM_SPANS]; static int pritype = PRI_CPE; #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 zt_pri *pri) { cw_mutex_unlock(&pri->lock); } static int switchtype = PRI_SWITCH_NI2; static int nsf = PRI_NSF_NONE; static int dialplan = PRI_NATIONAL_ISDN + 1; static int localdialplan = PRI_NATIONAL_ISDN + 1; #else /* Shut up the compiler */ struct zt_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 static struct zt_distRings drings; struct distRingData { int ring[3]; }; struct ringContextData { char contextData[CW_MAX_CONTEXT]; char extenData[CW_MAX_EXTENSION]; }; struct zt_distRings { struct distRingData ringnum[3]; struct ringContextData ringContext[3]; }; static char *subnames[] = { "Real", "Callwait", "Threeway" }; struct zt_subchannel { int zfd; struct cw_channel *owner; int chan; short buffer[CW_FRIENDLY_OFFSET/2 + READ_SIZE]; struct cw_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 linear:1; unsigned int inthreeway:1; ZT_CONFINFO curconf; }; #define CONF_USER_REAL (1 << 0) #define CONF_USER_THIRDCALL (1 << 1) #define MAX_SLAVES 4 static struct zt_pvt { cw_mutex_t lock; struct cw_channel *owner; /* Our current active owner (if applicable) */ /* Up to three channels can be associated with this call */ struct zt_subchannel sub_unused; /* Just a safety precaution */ struct zt_subchannel subs[3]; /* Sub-channels */ struct zt_confinfo saveconf; /* Saved conference info */ struct zt_pvt *slaves[MAX_SLAVES]; /* Slave to us (follows our conferencing) */ struct zt_pvt *master; /* Master to us (we follow their conferencing) */ int inconference; /* If our real should be in the conference */ int sig; /* Signalling style */ int radio; /* radio type */ float cid_rxgain; /* Gain to apply during CID detection */ float rxgain; float txgain; int tonezone; /* tone zone for this chan, or -1 for default */ struct zt_pvt *next; /* Next channel in list */ struct zt_pvt *prev; /* Prev channel in list */ /* flags */ unsigned int adsi:1; unsigned int answeronpolarityswitch:1; unsigned int busydetect:1; unsigned int callreturn:1; unsigned int callwaiting:1; unsigned int callwaitingcallerid:1; unsigned int cancallforward:1; unsigned int canpark:1; unsigned int confirmanswer:1; /* Wait for '#' to confirm answer */ unsigned int destroy:1; unsigned int didtdd:1; /* flag to say its done it once */ unsigned int dialednone:1; unsigned int dialing:1; unsigned int digital:1; unsigned int dnd:1; unsigned int echobreak:1; unsigned int echocanbridged:1; unsigned int echocanon:1; unsigned int faxhandled:1; /* Has a fax tone already been handled? */ unsigned int firstradio:1; unsigned int hanguponpolarityswitch:1; unsigned int hardwaredtmf:1; unsigned int hidecallerid; unsigned int ignoredtmf:1; unsigned int immediate:1; /* Answer before getting digits? */ unsigned int inalarm:1; unsigned int mate:1; /* flag to say its in MATE mode */ unsigned int outgoing:1; unsigned int overlapdial:1; unsigned int permcallwaiting:1; unsigned int permhidecallerid:1; /* Whether to hide our outgoing caller ID or not */ unsigned int priindication_oob:1; unsigned int priexclusive:1; unsigned int pulse:1; unsigned int pulsedial:1; /* whether a pulse dial phone is detected */ unsigned int restrictcid:1; /* Whether restrict the callerid -> only send ANI */ unsigned int threewaycalling:1; unsigned int transfer:1; unsigned int use_callerid:1; /* Whether or not to use caller id on this channel */ unsigned int use_callingpres:1; /* Whether to use the callingpres the calling switch sends */ unsigned int usedistinctiveringdetection:1; unsigned int distinctiveringaftercid:1; unsigned int zaptrcallerid:1; /* should we use the callerid from incoming call on zap transfer or not */ unsigned int transfertobusy:1; /* allow flash-transfers to busy channels */ #if defined(ZAPATA_PRI) unsigned int alerting:1; unsigned int alreadyhungup:1; unsigned int isidlecall:1; unsigned int proceeding:1; unsigned int progress:1; unsigned int resetting:1; unsigned int setup_ack:1; #endif unsigned int polarity_events:1; struct zt_distRings drings; char context[CW_MAX_CONTEXT]; char defcontext[CW_MAX_CONTEXT]; char exten[CW_MAX_EXTENSION]; char language[MAX_LANGUAGE]; char musicclass[MAX_MUSICCLASS]; #ifdef PRI_ANI char cid_ani[CW_MAX_EXTENSION]; #endif char cid_num[CW_MAX_EXTENSION]; int cid_ton; /* Type Of Number (TON) */ char cid_name[CW_MAX_EXTENSION]; char lastcid_num[CW_MAX_EXTENSION]; char lastcid_name[CW_MAX_EXTENSION]; char *origcid_num; /* malloced original callerid */ char *origcid_name; /* malloced original callerid */ char callwait_num[CW_MAX_EXTENSION]; char callwait_name[CW_MAX_EXTENSION]; char rdnis[CW_MAX_EXTENSION]; char dnid[CW_MAX_EXTENSION]; unsigned int group; int law; int confno; /* Our conference */ int confusers; /* Who is using our conference */ int propconfno; /* Propagated conference number */ cw_group_t callgroup; cw_group_t pickupgroup; int channel; /* Channel Number or CRV */ int span; /* Span number */ time_t guardtime; /* Must wait this much time before using for new call */ adsi_rx_state_t *adsi_rx1, *adsi_rx2; /* Internal CID decoding states */ int cid_signalling; /* CID signalling type bell202 or v23 */ int cid_start; /* CID start indicator, polarity or ring */ int cid_send_on; /* Internal send CID on polarity or ring indicator */ 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 */ uint8_t *cidspill; int cidpos; int cidlen; uint8_t *cidspill2; int cidlen2; int ringt; int ringt_base; int stripmsd; int callwaitcas; int callwaitrings; int echocancel; int echotraining; char echorest[20]; int busycount; int busy_tonelength; int busy_quietlength; int callprogress; struct timeval flashtime; /* Last flash-hook time */ struct cw_dsp *dsp; int cref; /* Call reference number */ ZT_DIAL_OPERATION dop; int whichwink; /* SIG_FEATDMF_TA Which wink are we on? */ char finaldial[64]; char accountcode[CW_MAX_ACCOUNT_CODE]; /* Account code */ int amaflags; /* AMA Flags */ struct tdd_state *tdd; /* TDD flag */ char call_forward[CW_MAX_EXTENSION]; char mailbox[CW_MAX_EXTENSION]; char dialdest[256]; int onhooktime; 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 */ int fake_event; int polarityonanswerdelay; struct timeval polaritydelaytv; int sendcalleridafter; #ifdef ZAPATA_PRI struct zt_pri *pri; struct zt_pvt *bearer; struct zt_pvt *realcall; q931_call *call; int prioffset; int logicalspan; int dsp_features; #endif int polarity; struct cw_jb_conf jbconf; } *iflist = NULL, *ifend = NULL; static struct cw_channel *zt_request(const char *type, int format, void *data, int *cause); static int zt_digit(struct cw_channel *cw, char digit); static int zt_sendtext(struct cw_channel *c, const char *text); static int zt_call(struct cw_channel *cw, char *rdest); static int zt_hangup(struct cw_channel *cw); static int zt_answer(struct cw_channel *cw); struct cw_frame *zt_read(struct cw_channel *cw); static int zt_write(struct cw_channel *cw, struct cw_frame *frame); struct cw_frame *zt_exception(struct cw_channel *cw); static int zt_indicate(struct cw_channel *chan, int condition); static int zt_fixup(struct cw_channel *oldchan, struct cw_channel *newchan); static int zt_setoption(struct cw_channel *chan, int option, void *data, int datalen); static const struct cw_channel_tech dahdi_tech = { .type = dahdi_type, .description = tdesc, .capabilities = CW_FORMAT_SLINEAR, .requester = zt_request, .send_digit = zt_digit, .send_text = zt_sendtext, .call = zt_call, .hangup = zt_hangup, .answer = zt_answer, .read = zt_read, .write = zt_write, .bridge = zt_bridge, .exception = zt_exception, .indicate = zt_indicate, .fixup = zt_fixup, .setoption = zt_setoption, }; static const struct cw_channel_tech zap_tech = { .type = zap_type, .description = tdesc, .capabilities = CW_FORMAT_SLINEAR, .requester = zt_request, .send_digit = zt_digit, .send_text = zt_sendtext, .call = zt_call, .hangup = zt_hangup, .answer = zt_answer, .read = zt_read, .write = zt_write, .bridge = zt_bridge, .exception = zt_exception, .indicate = zt_indicate, .fixup = zt_fixup, .setoption = zt_setoption, }; #ifdef ZAPATA_PRI #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel) #else #define GET_CHANNEL(p) ((p)->channel) #endif struct zt_pvt *round_robin[32]; #ifdef ZAPATA_PRI static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri) { int res; /* Grab the lock first */ do { res = cw_mutex_trylock(&pri->lock); if (res) { cw_mutex_unlock(&pvt->lock); /* Release the lock and try again */ usleep(1); cw_mutex_lock(&pvt->lock); } } while(res); /* Then break the poll */ 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 zt_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 */ }; /* 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 */ }; #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 & __ZT_SIG_FXO) */) #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */) static int zt_get_index(struct cw_channel *cw, struct zt_pvt *p, int nullok) { int res; if (p->subs[0].owner == cw) res = 0; else if (p->subs[1].owner == cw) res = 1; else if (p->subs[2].owner == cw) res = 2; else { res = -1; if (!nullok) cw_log(CW_LOG_WARNING, "Unable to get index, and nullok is not asserted\n"); } return res; } #ifdef ZAPATA_PRI static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri) #else static void wakeup_sub(struct zt_pvt *p, int a, void *pri) #endif { struct cw_frame null = { CW_FRAME_NULL, }; #ifdef ZAPATA_PRI if (pri) cw_mutex_unlock(&pri->lock); #endif for (;;) { if (p->subs[a].owner) { if (cw_mutex_trylock(&p->subs[a].owner->lock)) { cw_mutex_unlock(&p->lock); usleep(1); cw_mutex_lock(&p->lock); } else { cw_queue_frame(p->subs[a].owner, &null); cw_mutex_unlock(&p->subs[a].owner->lock); break; } } else break; } #ifdef ZAPATA_PRI if (pri) cw_mutex_lock(&pri->lock); #endif } #ifdef ZAPATA_PRI static void zap_queue_frame(struct zt_pvt *p, struct cw_frame *f, struct zt_pri *pri) #else static void zap_queue_frame(struct zt_pvt *p, struct cw_frame *f, void *pri) #endif { /* We must unlock the PRI to avoid the possibility of a deadlock */ #ifdef ZAPATA_PRI if (pri) cw_mutex_unlock(&pri->lock); #endif for (;;) { if (p->owner) { if (cw_mutex_trylock(&p->owner->lock)) { cw_mutex_unlock(&p->lock); usleep(1); cw_mutex_lock(&p->lock); } else { cw_queue_frame(p->owner, f); cw_mutex_unlock(&p->owner->lock); break; } } else break; } #ifdef ZAPATA_PRI if (pri) cw_mutex_lock(&pri->lock); #endif } #ifdef ZAPATA_PRI #define MAX_MAND_IES 10 struct msgtype { int msgnum; char *name; int mandies[MAX_MAND_IES]; }; static char *code2str(int code, struct msgtype *codes, int max) { int x; for (x=0;xsubs[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) p->subs[a].owner->fds[0] = p->subs[a].zfd; if (p->subs[b].owner) p->subs[b].owner->fds[0] = p->subs[b].zfd; wakeup_sub(p, a, NULL); wakeup_sub(p, b, NULL); } static int zt_open(char *fn) { int fd; int isnum; int chan = 0; int bs; int x; isnum = 1; for (x=0;x 0) close(fd); } int zt_setlinear(int zfd, int linear) { int res; res = ioctl(zfd, ZT_SETLINEAR, &linear); if (res) return res; return 0; } int zt_setlaw(int zfd, int law) { int res; res = ioctl(zfd, ZT_SETLAW, &law); if (res) return res; return 0; } static int alloc_sub(struct zt_pvt *p, int x) { ZT_BUFFERINFO bi; int res; if (p->subs[x].zfd < 0) { p->subs[x].zfd = zt_open("/dev/zap/pseudo"); if (p->subs[x].zfd > -1) { res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi); if (!res) { bi.txbufpolicy = ZT_POLICY_IMMEDIATE; bi.rxbufpolicy = ZT_POLICY_IMMEDIATE; bi.numbufs = numbufs; res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi); if (res < 0) { cw_log(CW_LOG_WARNING, "Unable to set buffer policy on channel %d\n", x); } } else cw_log(CW_LOG_WARNING, "Unable to check buffer policy on channel %d\n", x); if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) { cw_log(CW_LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd); zt_close(p->subs[x].zfd); p->subs[x].zfd = -1; return -1; } if (option_debug) cw_log(CW_LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan); return 0; } else cw_log(CW_LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno)); return -1; } cw_log(CW_LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel); return -1; } static int unalloc_sub(struct zt_pvt *p, int x) { if (!x) { cw_log(CW_LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel); return -1; } cw_log(CW_LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel); if (p->subs[x].zfd > -1) { zt_close(p->subs[x].zfd); } p->subs[x].zfd = -1; 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 zt_digit(struct cw_channel *cw, char digit) { ZT_DIAL_OPERATION zo; struct zt_pvt *p; int res = 0; int index; p = cw->tech_pvt; cw_mutex_lock(&p->lock); index = zt_get_index(cw, p, 0); if ((index == SUB_REAL) && p->owner) { #ifdef ZAPATA_PRI if ((p->sig == SIG_PRI) && (cw->_state == CW_STATE_DIALING) && !p->proceeding) { if (p->setup_ack) { if (!pri_grab(p, p->pri)) { pri_information(p->pri->pri,p->call,digit); pri_rel(p->pri); } else cw_log(CW_LOG_WARNING, "Unable to grab PRI on span %d\n", p->span); } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) { cw_log(CW_LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit); res = strlen(p->dialdest); p->dialdest[res++] = digit; p->dialdest[res] = '\0'; } } else { #else { #endif zo.op = ZT_DIAL_OP_APPEND; zo.dialstr[0] = 'T'; zo.dialstr[1] = digit; zo.dialstr[2] = 0; if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo))) cw_log(CW_LOG_WARNING, "Couldn't dial digit %c\n", digit); else p->dialing = 1; } } cw_mutex_unlock(&p->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[] = { { ZT_ALARM_RED, "Red Alarm" }, { ZT_ALARM_YELLOW, "Yellow Alarm" }, { ZT_ALARM_BLUE, "Blue Alarm" }, { ZT_ALARM_RECOVER, "Recovering" }, { ZT_ALARM_LOOPBACK, "Loopback" }, { ZT_ALARM_NOTOPEN, "Not Open" }, { ZT_ALARM_NONE, "None" }, }; static char *alarm2str(int alarm) { int x; for (x=0;x -1)) return events[event]; sprintf(buf, "Event %d", event); /* safe */ return buf; } #ifdef ZAPATA_PRI static char *dialplan2str(int dialplan) { if (dialplan == -1) { return("Dynamically set dialplan in ISDN"); } return(pri_plan2str(dialplan)); } #endif static char *zap_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_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 "PRI Signalling"; case SIG_SF: return "SF (Tone) Signalling Immediate"; case SIG_SFWINK: return "SF (Tone) Signalling Wink"; case SIG_SF_FEATD: return "SF (Tone) Signalling with Feature Group D (DTMF)"; case SIG_SF_FEATDMF: return "SF (Tone) Signalling with Feature Group D (MF)"; case SIG_SF_FEATB: return "SF (Tone) Signalling with Feature Group B (MF)"; case SIG_GR303FXOKS: return "GR-303 Signalling with FXOKS"; case SIG_GR303FXSKS: return "GR-303 Signalling with FXSKS"; case 0: return "Pseudo Signalling"; default: snprintf(buf, sizeof(buf), "Unknown signalling %d", sig); return buf; } } #define sig2str zap_sig2str static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel) { /* If the conference already exists, and we're already in it don't bother doing anything */ ZT_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 = ZT_CONF_DIGITALMON; zi.confno = slavechannel; } else { if (!index) { /* Real-side and pseudo-side both participate in conference */ zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER | ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER; } else zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER; zi.confno = p->confno; } if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode)) return 0; if (c->zfd < 0) return 0; if (ioctl(c->zfd, ZT_SETCONF, &zi)) { cw_log(CW_LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno); return -1; } if (slavechannel < 1) { p->confno = zi.confno; } memcpy(&c->curconf, &zi, sizeof(c->curconf)); cw_log(CW_LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno); return 0; } static int isourconf(struct zt_pvt *p, struct zt_subchannel *c) { /* If they're listening to our channel, they're ours */ if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_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 & ZT_CONF_TALKER)) return 1; return 0; } static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index) { ZT_CONFINFO zi; if (/* Can't delete if there's no zfd */ (c->zfd < 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)); zi.chan = 0; zi.confno = 0; zi.confmode = 0; if (ioctl(c->zfd, ZT_SETCONF, &zi)) { cw_log(CW_LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno); return -1; } cw_log(CW_LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno); memcpy(&c->curconf, &zi, sizeof(c->curconf)); return 0; } static int isslavenative(struct zt_pvt *p, struct zt_pvt **out) { int x; int useslavenative; struct zt_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].zfd > -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;xslaves[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 zt_pvt *p) { ZT_CONFINFO zi; memset(&zi, 0, sizeof(zi)); p->confno = -1; memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf)); if (p->subs[SUB_REAL].zfd > -1) { if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi)) cw_log(CW_LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel); } return 0; } static int update_conf(struct zt_pvt *p) { int needconf = 0; int x; int useslavenative; struct zt_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].zfd > -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;xslaves[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; } cw_log(CW_LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf); return 0; } static void zt_enable_ec(struct zt_pvt *p) { int x; int res; if (!p) return; if (p->echocanon) { cw_log(CW_LOG_DEBUG, "Echo cancellation already on\n"); return; } if (p->digital) { cw_log(CW_LOG_DEBUG, "Echo cancellation isn't required on digital connection\n"); return; } if (p->echocancel) { if (p->sig == SIG_PRI) { x = 1; res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x); if (res) cw_log(CW_LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel); } x = p->echocancel; res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x); if (res) cw_log(CW_LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel); else { p->echocanon = 1; cw_log(CW_LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel); } } else cw_log(CW_LOG_DEBUG, "No echo cancellation requested\n"); } static void zt_train_ec(struct zt_pvt *p) { int x; int res; if (p && p->echocancel && p->echotraining) { x = p->echotraining; res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x); if (res) cw_log(CW_LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel); else { cw_log(CW_LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel); } } else cw_log(CW_LOG_DEBUG, "No echo training requested\n"); } static void zt_disable_ec(struct zt_pvt *p) { int x; int res; if (p->echocancel) { x = 0; res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x); if (res) cw_log(CW_LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel); else cw_log(CW_LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel); } p->echocanon = 0; } static void fill_txgain(struct zt_gains *g, float gain, int law) { int j; int k; float linear_gain = pow(10.0, gain / 20.0); switch (law) { case ZT_LAW_ALAW: for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) { if (gain) { k = (int) (((float) CW_ALAW(j)) * linear_gain); if (k > 32767) k = 32767; if (k < -32767) k = -32767; g->txgain[j] = CW_LIN2A(k); } else { g->txgain[j] = j; } } break; case ZT_LAW_MULAW: for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) { if (gain) { k = (int) (((float) CW_MULAW(j)) * linear_gain); if (k > 32767) k = 32767; if (k < -32767) k = -32767; g->txgain[j] = CW_LIN2MU(k); } else { g->txgain[j] = j; } } break; } } static void fill_rxgain(struct zt_gains *g, float gain, int law) { int j; int k; float linear_gain = pow(10.0, gain / 20.0); switch (law) { case ZT_LAW_ALAW: for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) { if (gain) { k = (int) (((float) CW_ALAW(j)) * linear_gain); if (k > 32767) k = 32767; if (k < -32767) k = -32767; g->rxgain[j] = CW_LIN2A(k); } else { g->rxgain[j] = j; } } break; case ZT_LAW_MULAW: for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) { if (gain) { k = (int) (((float) CW_MULAW(j)) * linear_gain); if (k > 32767) k = 32767; if (k < -32767) k = -32767; g->rxgain[j] = CW_LIN2MU(k); } else { g->rxgain[j] = j; } } break; } } int set_actual_txgain(int fd, int chan, float gain, int law) { struct zt_gains g; int res; memset(&g, 0, sizeof(g)); g.chan = chan; res = ioctl(fd, ZT_GETGAINS, &g); if (res) { cw_log(CW_LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno)); return res; } fill_txgain(&g, gain, law); return ioctl(fd, ZT_SETGAINS, &g); } int set_actual_rxgain(int fd, int chan, float gain, int law) { struct zt_gains g; int res; memset(&g, 0, sizeof(g)); g.chan = chan; res = ioctl(fd, ZT_GETGAINS, &g); if (res) { cw_log(CW_LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno)); return res; } fill_rxgain(&g, gain, law); return ioctl(fd, ZT_SETGAINS, &g); } 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 zt_pvt *p) { int res; res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law); if (res) { cw_log(CW_LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno)); return -1; } return 0; } static int restore_gains(struct zt_pvt *p) { int res; res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law); if (res) { cw_log(CW_LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno)); return -1; } return 0; } static inline int zt_set_hook(int fd, int hs) { int x, res; x = hs; res = ioctl(fd, ZT_HOOK, &x); if (res < 0) { if (errno == EINPROGRESS) return 0; cw_log(CW_LOG_WARNING, "zt hook failed: %s\n", strerror(errno)); } return res; } static inline int zt_confmute(struct zt_pvt *p, int muted) { int x, y, res; x = muted; if (p->sig == SIG_PRI) { y = 1; res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y); if (res) cw_log(CW_LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel); } res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x); if (res < 0) cw_log(CW_LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno)); return res; } static int save_conference(struct zt_pvt *p) { struct zt_confinfo c; int res; if (p->saveconf.confmode) { cw_log(CW_LOG_WARNING, "Can't save conference -- already in use\n"); return -1; } p->saveconf.chan = 0; res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf); if (res) { cw_log(CW_LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno)); p->saveconf.confmode = 0; return -1; } c.chan = 0; c.confno = 0; c.confmode = ZT_CONF_NORMAL; res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c); if (res) { cw_log(CW_LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno)); return -1; } if (option_debug) cw_log(CW_LOG_DEBUG, "Disabled conferencing\n"); return 0; } static int restore_conference(struct zt_pvt *p) { int res; if (p->saveconf.confmode) { res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf); p->saveconf.confmode = 0; if (res) { cw_log(CW_LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno)); return -1; } } if (option_debug) cw_log(CW_LOG_DEBUG, "Restored conferencing\n"); return 0; } static int send_callerid(struct zt_pvt *p); int send_cwcidspill(struct zt_pvt *p) { p->callwaitcas = 0; p->cidcwexpire = 0; p->cidspill = malloc(MAX_CALLERID_SIZE); if (p->cidspill) { p->cidlen = cw_callerid_generate(p->cid_signalling, p->cidspill, MAX_CALLERID_SIZE, CW_PRES_ALLOWED, p->callwait_num, p->callwait_name, 1, CW_LAW(p)); p->cidpos = 0; send_callerid(p); if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num); } else return -1; return 0; } static int send_callerid(struct zt_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; zt_setlinear(p->subs[SUB_REAL].zfd, 0); } while(p->cidpos < p->cidlen) { res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos); if (res < 0) { if (errno == EAGAIN) return 0; else { cw_log(CW_LOG_WARNING, "write failed: %s\n", strerror(errno)); return -1; } } if (!res) return 0; p->cidpos += res; } if (p->callwaitcas) { /* Wait for CID/CW to expire */ p->cidcwexpire = CIDCW_EXPIRE_SAMPLES; } else restore_conference(p); return 1; } static int zt_callwait(struct cw_channel *cw) { struct zt_pvt *p = cw->tech_pvt; p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES; if (p->cidspill) { cw_log(CW_LOG_WARNING, "%s: Discarded existing spill!\n", cw->name); free(p->cidspill); } p->cidspill = malloc(MAX_CALLERID_SIZE); if (p->cidspill) { save_conference(p); if (!p->callwaitrings && p->callwaitingcallerid) { p->cidlen = cw_gen_cas(p->cidspill, MAX_CALLERID_SIZE, 1, CW_LAW(p)); p->callwaitcas = 1; } else { p->cidlen = cw_gen_cas(p->cidspill, MAX_CALLERID_SIZE, 1, CW_LAW(p)); p->callwaitcas = 0; } p->cidpos = 0; send_callerid(p); } else { cw_log(CW_LOG_WARNING, "%s: No memory for SAS/CAS spill\n", cw->name); return -1; } return 0; } static int zt_call(struct cw_channel *cw, char *rdest) { struct zt_ring_cadence rcad, *cadence; struct tone_zone *tzone; struct zt_pvt *p = cw->tech_pvt; int x, res, index; char *c, *n, *l; #ifdef ZAPATA_PRI char *s=NULL; #endif char dest[256]; /* must be same length as p->dialdest */ cw_mutex_lock(&p->lock); cw_copy_string(dest, rdest, sizeof(dest)); cw_copy_string(p->dialdest, rdest, sizeof(p->dialdest)); if ((cw->_state == CW_STATE_BUSY)) { p->subs[SUB_REAL].needbusy = 1; cw_mutex_unlock(&p->lock); return 0; } if ((cw->_state != CW_STATE_DOWN) && (cw->_state != CW_STATE_RESERVED)) { cw_log(CW_LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", cw->name); cw_mutex_unlock(&p->lock); return -1; } p->dialednone = 0; if (p->radio) /* if a radio channel, up immediately */ { /* Special pseudo -- automatically up */ cw_setstate(cw, CW_STATE_UP); cw_mutex_unlock(&p->lock); return 0; } x = ZT_FLUSH_READ | ZT_FLUSH_WRITE; res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x); if (res) cw_log(CW_LOG_WARNING, "Unable to flush input on channel %d\n", p->channel); p->outgoing = 1; set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law); switch(p->sig) { case SIG_FXOLS: case SIG_FXOGS: case SIG_FXOKS: if (p->owner == cw) { /* Normal ring, on hook */ /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */ c = strchr(dest, '/'); if (c) c++; if (c && (strlen(c) < p->stripmsd)) { cw_log(CW_LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd); c = NULL; } if (c) { p->dop.op = ZT_DIAL_OP_REPLACE; snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c); cw_log(CW_LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c); } else { p->dop.dialstr[0] = '\0'; } if (p->cidspill) { cw_log(CW_LOG_WARNING, "cidspill already exists??\n"); free(p->cidspill); p->cidspill = NULL; } if (p->use_callerid) { /* Caller ID before ring uses defined signalling format. Caller ID between * rings is (nearly) always US style. This is sufficiently compatible with all the * possibilities we might want that it should "just work". * Note: India (Bharti) uses DTMF after the first ring apparently. If you want * that you have to use explicit cidsignalling=dtmf, cidstart=ring and forego * any pre-ring CID. */ p->cidspill = malloc(MAX_CALLERID_SIZE); p->callwaitcas = 0; if (p->cidspill) { p->cidlen = cw_callerid_generate(p->cid_signalling, p->cidspill, MAX_CALLERID_SIZE, cw->cid.cid_pres, cw->cid.cid_num, cw->cid.cid_name, 0, CW_LAW(p)); p->cidpos = 0; if (p->cid_start != CID_START_RING || !p->cidrings) { p->cidspill2 = malloc(MAX_CALLERID_SIZE); if (p->cidspill2) p->cidlen2 = cw_callerid_generate(ADSI_STANDARD_CLASS, p->cidspill2, MAX_CALLERID_SIZE, cw->cid.cid_pres, cw->cid.cid_num, cw->cid.cid_name, 0, CW_LAW(p)); } } else { /* No memory for caller ID but maybe we can still make the call */ cw_log(CW_LOG_WARNING, "Unable to generate CallerID spill\n"); } } /* Choose main cadence */ if (p->distinctivering > 0 && p->distinctivering <= num_cadence) { cadence = &cadences[p->distinctivering-1]; p->cidrings = cidrings[p->distinctivering-1]; } else if (!ioctl(p->subs[SUB_REAL].zfd, ZT_GETTONEZONE, &res) && (tzone = tone_zone_find_by_num(res))) { memcpy(rcad.ringcadence, tzone->ringcadence, sizeof(rcad.ringcadence)); cadence = &rcad; p->cidrings = -1; } else { rcad.ringcadence[0] = -ZT_DEFAULT_RINGTIME; rcad.ringcadence[1] = ZT_RINGOFFTIME; cadence = &rcad; p->cidrings = 1; } if (p->cidspill && p->distinctiveringaftercid) { /* Insert the initial cadence making the silence long enough for * the CID. Note that we don't adjust silence like this anywhere * else otherwise it breaks the remote's distinctive ring * detection. Even this might break some but... tough! */ memmove(&rcad.ringcadence[2], &cadence->ringcadence[0], sizeof(rcad.ringcadence) - 2 * sizeof(rcad.ringcadence[0])); rcad.ringcadence[0] = 250; rcad.ringcadence[1] = SAMPLES_TO_MS(p->cidlen); if (rcad.ringcadence[1] < 1000) rcad.ringcadence[1] = 1000; cadence = &rcad; p->cidrings = 1; /* If the main cadence doesn't explicitly mark the loop start * we need to mark it ourselves to be the start of the main * cadence, after the initial cadence */ for (x = 2; x < arraysize(rcad.ringcadence) && rcad.ringcadence[x] >= 0; x += 2); if (x >= arraysize(rcad.ringcadence)) rcad.ringcadence[2] = -rcad.ringcadence[2]; } else if (p->cidrings < 0 && (p->cidrings = p->sendcalleridafter) < 0) { /* Look for the longest silence in the cadence and use * that for the CID position */ index = 0; for (x = 1; x < arraysize(rcad.ringcadence); x += 2) { if (rcad.ringcadence[x] > index) { index = rcad.ringcadence[x]; p->cidrings = (x >> 1) + 1; } } } if (option_debug) { for (x = 0; x < arraysize(rcad.ringcadence) && cadence->ringcadence[x]; x += 2) cw_log(CW_LOG_DEBUG, "%s: cadence[%d] = %d/%d\n", cw->name, (x >> 1) + 1, cadence->ringcadence[x], cadence->ringcadence[x+1]); cw_log(CW_LOG_DEBUG, "%s: Post-ring CID follows ring %d\n", cw->name, p->cidrings); } if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, cadence)) cw_log(CW_LOG_WARNING, "%s: Unable to set ring cadence\n", cw->name); if (!p->cidspill || (p->cid_start == CID_START_RING && p->cidrings)) { /* Caller ID goes in the silence between two rings. Just start ringing * the line. Caller ID will be despatched in due course. */ cw_log(CW_LOG_DEBUG, "%s: Start ringing\n", cw->name); p->cid_send_on = CID_START_RING; x = ZT_RING; if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) { cw_log(CW_LOG_WARNING, "%s: Unable to ring phone: %s\n", cw->name, strerror(errno)); cw_mutex_unlock(&p->lock); return -1; } } else { /* Caller ID goes before the first ring. We assume that means it's flagged * by a UK style polarity reversal, followed by data, followed by ringing. * If we are sending using DTMF then in some implementations there should be * no polarity reversal. It probably doesn't hurt, though? * If one or both of these fail caller ID might fail but the call may still * be possible. We'll find out when we try and start ringing after the * caller ID has been sent (possibly into the ether). */ cw_log(CW_LOG_DEBUG, "%s: Send pre-ring caller ID\n", cw->name); p->cid_send_on = CID_START_POLARITY; x = p->cidlen; if (ioctl(p->subs[SUB_REAL].zfd, ZT_ONHOOKTRANSFER, &x)) cw_log(CW_LOG_DEBUG, "%s: Unable to start on hook transfer: %s\n", cw->name, strerror(errno)); x = POLARITY_REV; ioctl(p->subs[SUB_REAL].zfd, ZT_SETPOLARITY, &x); send_callerid(p); } /* Don't send audio while on hook, until the call is answered */ p->dialing = 1; } else { /* Call waiting call */ p->callwaitrings = 0; if (cw->cid.cid_num) cw_copy_string(p->callwait_num, cw->cid.cid_num, sizeof(p->callwait_num)); else p->callwait_num[0] = '\0'; if (cw->cid.cid_name) cw_copy_string(p->callwait_name, cw->cid.cid_name, sizeof(p->callwait_name)); else p->callwait_name[0] = '\0'; /* Call waiting tone instead */ if (zt_callwait(cw)) { cw_mutex_unlock(&p->lock); return -1; } /* Make ring-back */ if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE)) cw_log(CW_LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", cw->name); } n = cw->cid.cid_name; l = cw->cid.cid_num; if (l) cw_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num)); else p->lastcid_num[0] = '\0'; if (n) cw_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name)); else p->lastcid_name[0] = '\0'; cw_setstate(cw, CW_STATE_RINGING); index = zt_get_index(cw, p, 0); if (index > -1) { p->subs[index].needringing = 1; } break; case SIG_FXSLS: case SIG_FXSGS: case SIG_FXSKS: case SIG_EMWINK: case SIG_EM: case SIG_EM_E1: case SIG_FEATD: case SIG_FEATDMF: case SIG_E911: 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) { cw_log(CW_LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd); cw_mutex_unlock(&p->lock); return -1; } #ifdef ZAPATA_PRI /* Start the trunk, if not GR-303 */ if (!p->pri) { #endif x = ZT_START; res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x); if (res < 0) { if (errno != EINPROGRESS) { cw_log(CW_LOG_WARNING, "Unable to start channel: %s\n", strerror(errno)); cw_mutex_unlock(&p->lock); return -1; } } #ifdef ZAPATA_PRI } #endif cw_log(CW_LOG_DEBUG, "Dialing '%s'\n", c); p->dop.op = ZT_DIAL_OP_REPLACE; c += p->stripmsd; switch (p->sig) { case SIG_FEATD: l = cw->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 = cw->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: { char *cic = NULL, *ozz = NULL; /* 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) { cw_log(CW_LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n"); cw_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: cw_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr)); break; 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].zfd, ZT_DIAL, &p->dop)) { x = ZT_ONHOOK; ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x); cw_log(CW_LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno)); cw_mutex_unlock(&p->lock); return -1; } } else cw_log(CW_LOG_DEBUG, "Deferring dialing...\n"); p->dialing = 1; if (cw_strlen_zero(c)) p->dialednone = 1; cw_setstate(cw, CW_STATE_DIALING); break; case 0: /* Special pseudo -- automatically up*/ cw_setstate(cw, CW_STATE_UP); break; case SIG_PRI: /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */ p->dialdest[0] = '\0'; break; default: cw_log(CW_LOG_DEBUG, "not yet implemented\n"); cw_mutex_unlock(&p->lock); return -1; } #ifdef ZAPATA_PRI if (p->pri) { struct pri_sr *sr; char *useruser; char *t; int pridialplan; int dp_strip; int prilocaldialplan; int ldp_strip; int exclusive; c = strchr(dest, '/'); if (c) c++; else c = dest; if (!p->hidecallerid) { l = cw->cid.cid_num; n = cw->cid.cid_name; } else { l = NULL; n = NULL; } if (strlen(c) < p->stripmsd) { cw_log(CW_LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd); cw_mutex_unlock(&p->lock); return -1; } if (p->sig != SIG_FXSKS) { p->dop.op = ZT_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)) { cw_log(CW_LOG_WARNING, "Failed to grab PRI!\n"); cw_mutex_unlock(&p->lock); return -1; } if (!(p->call = pri_new_call(p->pri->pri))) { cw_log(CW_LOG_WARNING, "Unable to create call on channel %d\n", p->channel); pri_rel(p->pri); cw_mutex_unlock(&p->lock); return -1; } if (!(sr = pri_sr_new())) { cw_log(CW_LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel); pri_rel(p->pri); cw_mutex_unlock(&p->lock); } if (p->bearer || (p->sig == SIG_FXSKS)) { if (p->bearer) { cw_log(CW_LOG_DEBUG, "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 cw_log(CW_LOG_DEBUG, "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(cw->transfercapability); /* Add support for exclusive override */ if (p->priexclusive) exclusive = 1; else { /* otherwise, traditional behavior */ if (p->pri->nodetype == PRI_NETWORK) exclusive = 0; else exclusive = 1; } 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 : cw->transfercapability, (p->digital ? -1 : ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW))); if (p->pri->facilityenable) pri_facility_enable(p->pri->pri); if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", cw->transfercapability, cw_transfercapability2str(cw->transfercapability)); dp_strip = 0; pridialplan = p->pri->dialplan - 1; if (pridialplan == -2) { /* compute dynamically */ if (strlen(p->pri->internationalprefix) > 0 && strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) { dp_strip = strlen(p->pri->internationalprefix); pridialplan = PRI_INTERNATIONAL_ISDN; } else if (strlen(p->pri->nationalprefix) > 0 && strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) { dp_strip = strlen(p->pri->nationalprefix); pridialplan = PRI_NATIONAL_ISDN; } else if (strlen(p->pri->localprefix) > 0 && strncmp(c + p->stripmsd, p->pri->localprefix, strlen(p->pri->localprefix)) == 0) { dp_strip = strlen(p->pri->localprefix); pridialplan = PRI_LOCAL_ISDN; } else if (strlen(p->pri->privateprefix) > 0 && strncmp(c + p->stripmsd, p->pri->privateprefix, strlen(p->pri->privateprefix)) == 0) { dp_strip = strlen(p->pri->privateprefix); pridialplan = PRI_PRIVATE; } else if (strlen(p->pri->unknownprefix) > 0 && strncmp(c + p->stripmsd, p->pri->unknownprefix, strlen(p->pri->unknownprefix)) == 0) { dp_strip = strlen(p->pri->unknownprefix); pridialplan = PRI_UNKNOWN; } else { pridialplan = PRI_LOCAL_ISDN; } } t = pbx_builtin_getvar_helper(cw, "PRITON"); if (t) { if (!strcasecmp(t, "INTERNATIONAL")) { pridialplan = PRI_INTERNATIONAL_ISDN; } else if (!strcasecmp(t, "NATIONAL")) { pridialplan = PRI_NATIONAL_ISDN; } else if (!strcasecmp(t, "LOCAL")) { pridialplan = PRI_LOCAL_ISDN; } else if (!strcasecmp(t, "PRIVATE")) { pridialplan = PRI_PRIVATE; } else if (!strcasecmp(t, "UNKNOWN")) { pridialplan = PRI_UNKNOWN; } else { cw_log(CW_LOG_WARNING, "Unable to determine PRI type of number '%s', using instead '%s'\n", t, ton2str(pridialplan)); } } 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)) { /* compute dynamically */ if (strlen(p->pri->internationalprefix) > 0 && strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) { ldp_strip = strlen(p->pri->internationalprefix); prilocaldialplan = PRI_INTERNATIONAL_ISDN; } else if (strlen(p->pri->nationalprefix) > 0 && strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) { ldp_strip = strlen(p->pri->nationalprefix); prilocaldialplan = PRI_NATIONAL_ISDN; } else if (strlen(p->pri->localprefix) > 0 && strncmp(l, p->pri->localprefix, strlen(p->pri->localprefix)) == 0) { ldp_strip = strlen(p->pri->localprefix); prilocaldialplan = PRI_LOCAL_ISDN; } else if (strlen(p->pri->privateprefix) > 0 && strncmp(l, p->pri->privateprefix, strlen(p->pri->privateprefix)) == 0) { ldp_strip = strlen(p->pri->privateprefix); prilocaldialplan = PRI_PRIVATE; } else if (strlen(p->pri->unknownprefix) > 0 && strncmp(l, p->pri->unknownprefix, strlen(p->pri->unknownprefix)) == 0) { ldp_strip = strlen(p->pri->unknownprefix); prilocaldialplan = PRI_UNKNOWN; } else { prilocaldialplan = PRI_LOCAL_ISDN; } } t = pbx_builtin_getvar_helper(cw, "PRILOCALTON"); if (t) { if (!strcasecmp(t, "INTERNATIONAL")) { prilocaldialplan = PRI_INTERNATIONAL_ISDN; } else if (!strcasecmp(t, "NATIONAL")) { prilocaldialplan = PRI_NATIONAL_ISDN; } else if (!strcasecmp(t, "LOCAL")) { prilocaldialplan = PRI_LOCAL_ISDN; } else if (!strcasecmp(t, "PRIVATE")) { prilocaldialplan = PRI_PRIVATE; } else if (!strcasecmp(t, "UNKNOWN")) { prilocaldialplan = PRI_UNKNOWN; } else { cw_log(CW_LOG_WARNING, "Unable to determine local PRI type of number '%s', using instead '%s'\n", x, ton2str(prilocaldialplan)); } } pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan, l ? (p->use_callingpres ? cw->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) : PRES_NUMBER_NOT_AVAILABLE); pri_sr_set_redirecting(sr, cw->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL); /* User-user info */ useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO"); if (useruser) pri_sr_set_useruser(sr, useruser); if (pri_setup(p->pri->pri, p->call, sr)) { cw_log(CW_LOG_WARNING, "Unable to setup call to %s (using %s)\n", c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan)); pri_rel(p->pri); cw_mutex_unlock(&p->lock); pri_sr_free(sr); return -1; } pri_sr_free(sr); cw_setstate(cw, CW_STATE_DIALING); pri_rel(p->pri); } #endif cw_mutex_unlock(&p->lock); return 0; } static void destroy_zt_pvt(struct zt_pvt **pvt) { struct zt_pvt *p = *pvt; /* Remove channel from the list */ if(p->prev) p->prev->next = p->next; if(p->next) p->next->prev = p->prev; cw_mutex_destroy(&p->lock); free(p); *pvt = NULL; } static int destroy_channel(struct zt_pvt *prev, struct zt_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; } if (cur->subs[SUB_REAL].zfd > -1) { zt_close(cur->subs[SUB_REAL].zfd); } destroy_zt_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; } if (cur->subs[SUB_REAL].zfd > -1) { zt_close(cur->subs[SUB_REAL].zfd); } destroy_zt_pvt(&cur); } return 0; } #ifdef ZAPATA_PRI int pri_is_up(struct zt_pri *pri) { int x; for (x=0;xdchanavail[x] == DCHAN_AVAILABLE) return 1; } return 0; } int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer) { bearer->owner = &inuse; bearer->realcall = crv; crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd; if (crv->subs[SUB_REAL].owner) crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd; crv->bearer = bearer; crv->call = bearer->call; crv->pri = pri; return 0; } static char *pri_order(int level) { switch(level) { case 0: return "Primary"; case 1: return "Secondary"; case 2: return "Tertiary"; case 3: return "Quaternary"; default: return ""; } } /* Returns fd of the active dchan */ int pri_active_dchan_fd(struct zt_pri *pri) { int x = -1; for (x = 0; x < NUM_DCHANS; x++) { if ((pri->dchans[x] == pri->pri)) break; } return pri->fds[x]; } int pri_find_dchan(struct zt_pri *pri) { int oldslot = -1; struct pri *old; int newslot = -1; int x; old = pri->pri; for(x=0;xdchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0)) newslot = x; if (pri->dchans[x] == old) { oldslot = x; } } if (newslot < 0) { newslot = 0; cw_log(CW_LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n", pri->dchannels[newslot]); } if (old && (oldslot != newslot)) cw_log(CW_LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n", pri->dchannels[oldslot], pri->dchannels[newslot]); pri->pri = pri->dchans[newslot]; return 0; } #endif static int zt_hangup(struct cw_channel *cw) { int res; int index,x, law; /*static int restore_gains(struct zt_pvt *p);*/ struct zt_pvt *p = cw->tech_pvt; struct zt_pvt *tmp = NULL; struct zt_pvt *prev = NULL; ZT_PARAMS par; if (option_debug) cw_log(CW_LOG_DEBUG, "zt_hangup(%s)\n", cw->name); if (!cw->tech_pvt) { cw_log(CW_LOG_WARNING, "Asked to hangup channel not connected\n"); return 0; } cw_mutex_lock(&p->lock); index = zt_get_index(cw, p, 1); if (p->sig == SIG_PRI) { x = 1; cw_channel_setoption(cw, CW_OPTION_AUDIO_MODE, &x, sizeof(char)); } x = 0; zt_confmute(p, 0); restore_gains(p); if (p->origcid_num) { cw_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num)); free(p->origcid_num); p->origcid_num = NULL; } if (p->origcid_name) { cw_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name)); free(p->origcid_name); p->origcid_name = NULL; } if (p->dsp) cw_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); #if 0 if (p->exten) p->exten[0] = '\0'; #endif cw_log(CW_LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n", p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd); p->ignoredtmf = 0; if (index > -1) { /* Real channel, do some fixup */ p->subs[index].owner = NULL; p->subs[index].needanswer = 0; p->subs[index].needflash = 0; p->subs[index].needringing = 0; p->subs[index].needbusy = 0; p->subs[index].needcongestion = 0; p->subs[index].linear = 0; p->subs[index].needcallerid = 0; p->polarity = POLARITY_IDLE; zt_setlinear(p->subs[index].zfd, 0); if (index == SUB_REAL) { if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) { cw_log(CW_LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n"); if (p->subs[SUB_CALLWAIT].inthreeway) { /* We had flipped over to answer a callwait and now it's gone */ cw_log(CW_LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n"); /* Move to the call-wait, but un-own us until they flip back. */ swap_subs(p, SUB_CALLWAIT, SUB_REAL); unalloc_sub(p, SUB_CALLWAIT); p->owner = NULL; } else { /* The three way hung up, but we still have a call wait */ cw_log(CW_LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n"); swap_subs(p, SUB_THREEWAY, SUB_REAL); unalloc_sub(p, SUB_THREEWAY); if (p->subs[SUB_REAL].inthreeway) { /* This was part of a three way call. Immediately make way for another call */ cw_log(CW_LOG_DEBUG, "Call was complete, setting owner to former third call\n"); p->owner = p->subs[SUB_REAL].owner; } else { /* This call hasn't been completed yet... Set owner to NULL */ cw_log(CW_LOG_DEBUG, "Call was incomplete, setting owner to NULL\n"); p->owner = NULL; } p->subs[SUB_REAL].inthreeway = 0; } } else if (p->subs[SUB_CALLWAIT].zfd > -1) { /* Move to the call-wait and switch back to them. */ swap_subs(p, SUB_CALLWAIT, SUB_REAL); unalloc_sub(p, SUB_CALLWAIT); p->owner = p->subs[SUB_REAL].owner; if (p->owner->_state != CW_STATE_UP) p->subs[SUB_REAL].needanswer = 1; if (cw_bridged_channel(p->subs[SUB_REAL].owner)) cw_moh_stop(cw_bridged_channel(p->subs[SUB_REAL].owner)); } else if (p->subs[SUB_THREEWAY].zfd > -1) { swap_subs(p, SUB_THREEWAY, SUB_REAL); unalloc_sub(p, SUB_THREEWAY); if (p->subs[SUB_REAL].inthreeway) { /* This was part of a three way call. Immediately make way for another call */ cw_log(CW_LOG_DEBUG, "Call was complete, setting owner to former third call\n"); p->owner = p->subs[SUB_REAL].owner; } else { /* This call hasn't been completed yet... Set owner to NULL */ cw_log(CW_LOG_DEBUG, "Call was incomplete, setting owner to NULL\n"); p->owner = NULL; } p->subs[SUB_REAL].inthreeway = 0; } } else if (index == SUB_CALLWAIT) { /* Ditch the holding callwait call, and immediately make it availabe */ if (p->subs[SUB_CALLWAIT].inthreeway) { /* This is actually part of a three way, placed on hold. Place the third part on music on hold now */ if (p->subs[SUB_THREEWAY].owner && cw_bridged_channel(p->subs[SUB_THREEWAY].owner)) cw_moh_start(cw_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL); p->subs[SUB_THREEWAY].inthreeway = 0; /* Make it the call wait now */ swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY); unalloc_sub(p, SUB_THREEWAY); } else unalloc_sub(p, SUB_CALLWAIT); } else if (index == SUB_THREEWAY) { if (p->subs[SUB_CALLWAIT].inthreeway) { /* The other party of the three way call is currently in a call-wait state. Start music on hold for them, and take the main guy out of the third call */ if (p->subs[SUB_CALLWAIT].owner && cw_bridged_channel(p->subs[SUB_CALLWAIT].owner)) cw_moh_start(cw_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL); p->subs[SUB_CALLWAIT].inthreeway = 0; } p->subs[SUB_REAL].inthreeway = 0; /* If this was part of a three way call index, let us make another three way call */ unalloc_sub(p, SUB_THREEWAY); } else { /* This wasn't any sort of call, but how are we an index? */ cw_log(CW_LOG_WARNING, "Index found but not any type of call?\n"); } } if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) { p->owner = NULL; p->ringt = 0; p->distinctivering = 0; p->confirmanswer = 0; p->cidrings = 1; p->outgoing = 0; p->digital = 0; p->faxhandled = 0; p->pulsedial = 0; p->onhooktime = time(NULL); #ifdef ZAPATA_PRI p->proceeding = 0; p->progress = 0; p->alerting = 0; p->setup_ack = 0; #endif if (p->dsp) { cw_dsp_free(p->dsp); p->dsp = NULL; } law = ZT_LAW_DEFAULT; res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law); if (res < 0) cw_log(CW_LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel); /* Perform low level hangup if no owner left */ #ifdef ZAPATA_PRI if (p->pri) { char *useruser = pbx_builtin_getvar_helper(cw, "USERUSERINFO"); /* Make sure we have a call (or REALLY have a call in the case of a PRI) */ if (p->call && (!p->bearer || (p->bearer->call == p->call))) { if (!pri_grab(p, p->pri)) { if (p->alreadyhungup) { cw_log(CW_LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n"); pri_call_set_useruser(p->call, useruser); pri_hangup(p->pri->pri, p->call, -1); p->call = NULL; if (p->bearer) p->bearer->call = NULL; } else { char *cause = pbx_builtin_getvar_helper(cw, "PRI_CAUSE"); int icause = cw->hangupcause ? cw->hangupcause : -1; cw_log(CW_LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n"); pri_call_set_useruser(p->call, useruser); p->alreadyhungup = 1; if (p->bearer) p->bearer->alreadyhungup = 1; if (cause) { if (atoi(cause)) icause = atoi(cause); } pri_hangup(p->pri->pri, p->call, icause); } if (res < 0) cw_log(CW_LOG_WARNING, "pri_disconnect failed\n"); pri_rel(p->pri); } else { cw_log(CW_LOG_WARNING, "Unable to grab PRI on span %d\n", p->span); res = -1; } } else { if (p->bearer) cw_log(CW_LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call); p->call = NULL; res = 0; } } #endif if (p->sig && (p->sig != SIG_PRI)) res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK); if (res < 0) { cw_log(CW_LOG_WARNING, "Unable to hangup line %s\n", cw->name); } switch(p->sig) { case SIG_FXOGS: case SIG_FXOLS: case SIG_FXOKS: res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par); if (!res) { #if 0 cw_log(CW_LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook); #endif /* If they're off hook, try playing congestion */ if ((par.rxisoffhook) && (!p->radio)) tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION); else tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1); } break; case SIG_FXSGS: case SIG_FXSLS: case SIG_FXSKS: /* Make sure we're not made available for at least two seconds assuming we were actually used for an inbound or outbound call. */ if (cw->_state != CW_STATE_RESERVED) { time(&p->guardtime); p->guardtime += 2; } break; default: tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1); } if (p->cidspill) free(p->cidspill); if (p->cidspill2) free(p->cidspill2); if (p->sig) zt_disable_ec(p); x = 0; cw_channel_setoption(cw, CW_OPTION_TONE_VERIFY, &x, sizeof(char)); cw_channel_setoption(cw, CW_OPTION_TDD, &x, sizeof(char)); p->didtdd = FALSE; p->cidspill = p->cidspill2 = NULL; p->callwaitcas = 0; p->callwaiting = p->permcallwaiting; p->hidecallerid = p->permhidecallerid; p->dialing = 0; p->rdnis[0] = '\0'; update_conf(p); reset_conf(p); /* Restore data mode */ if (p->sig == SIG_PRI) { x = 0; cw_channel_setoption(cw, CW_OPTION_AUDIO_MODE, &x, sizeof(char)); } #ifdef ZAPATA_PRI if (p->bearer) { cw_log(CW_LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel); /* Free up the bearer channel as well, and don't use its file descriptor anymore */ update_conf(p->bearer); reset_conf(p->bearer); p->bearer->owner = NULL; p->bearer->realcall = NULL; p->bearer = NULL; p->subs[SUB_REAL].zfd = -1; p->pri = NULL; } #endif restart_monitor(); } p->callwaitingrepeat = 0; p->cidcwexpire = 0; cw->tech_pvt = NULL; cw_mutex_unlock(&p->lock); cw_mutex_lock(&usecnt_lock); usecnt--; if (usecnt < 0) cw_log(CW_LOG_WARNING, "Usecnt < 0???\n"); cw_mutex_unlock(&usecnt_lock); if (option_verbose > 2) cw_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", cw->name); cw_mutex_lock(&iflock); tmp = iflist; prev = NULL; if (p->destroy) { while (tmp) { if (tmp == p) { destroy_channel(prev, tmp, 0); break; } else { prev = tmp; tmp = tmp->next; } } } cw_mutex_unlock(&iflock); return 0; } static int zt_answer(struct cw_channel *cw) { struct zt_pvt *p = cw->tech_pvt; int res=0; int index; int oldstate = cw->_state; cw_setstate(cw, CW_STATE_UP); cw_mutex_lock(&p->lock); index = zt_get_index(cw, p, 0); if (index < 0) index = SUB_REAL; /* nothing to do if a radio channel */ if (p->radio) { cw_mutex_unlock(&p->lock); return 0; } switch(p->sig) { case SIG_FXSLS: case SIG_FXSGS: case SIG_FXSKS: p->ringt = 0; /* Fall through */ case SIG_EM: case SIG_EM_E1: case SIG_EMWINK: case SIG_FEATD: case SIG_FEATDMF: case SIG_E911: case SIG_FEATB: case SIG_SF: case SIG_SFWINK: case SIG_SF_FEATD: case SIG_SF_FEATDMF: case SIG_SF_FEATB: case SIG_FXOLS: case SIG_FXOGS: case SIG_FXOKS: /* Pick up the line */ cw_log(CW_LOG_DEBUG, "Took %s off hook\n", cw->name); if(p->hanguponpolarityswitch) { gettimeofday(&p->polaritydelaytv, NULL); } res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK); tone_zone_play_tone(p->subs[index].zfd, -1); p->dialing = 0; if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) { if (oldstate == CW_STATE_RINGING) { cw_log(CW_LOG_DEBUG, "Finally swapping real and threeway\n"); tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1); swap_subs(p, SUB_THREEWAY, SUB_REAL); p->owner = p->subs[SUB_REAL].owner; } } if (p->sig & __ZT_SIG_FXS) { zt_enable_ec(p); zt_train_ec(p); } break; #ifdef ZAPATA_PRI case SIG_PRI: /* Send a pri acknowledge */ if (!pri_grab(p, p->pri)) { p->proceeding = 1; res = pri_answer(p->pri->pri, p->call, 0, !p->digital); pri_rel(p->pri); } else { cw_log(CW_LOG_WARNING, "Unable to grab PRI on span %d\n", p->span); res= -1; } break; #endif case 0: cw_mutex_unlock(&p->lock); return 0; default: cw_log(CW_LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel); res = -1; } cw_mutex_unlock(&p->lock); return res; } static int zt_setoption(struct cw_channel *chan, int option, void *data, int datalen) { char *cp; signed char *scp; int x; int index; struct zt_pvt *p = chan->tech_pvt; /* all supported options require data */ if (!data || (datalen < 1)) { errno = EINVAL; return -1; } switch(option) { case CW_OPTION_TXGAIN: scp = (signed char *) data; index = zt_get_index(chan, p, 0); if (index < 0) { cw_log(CW_LOG_WARNING, "No index in TXGAIN?\n"); return -1; } cw_log(CW_LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp); return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law); case CW_OPTION_RXGAIN: scp = (signed char *) data; index = zt_get_index(chan, p, 0); if (index < 0) { cw_log(CW_LOG_WARNING, "No index in RXGAIN?\n"); return -1; } cw_log(CW_LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp); return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law); case CW_OPTION_ECHOCANCEL: if (*(char *)data) zt_enable_ec(p); else zt_disable_ec(p); return 0; case CW_OPTION_TONE_VERIFY: if (!p->dsp) break; cp = (char *) data; switch (*cp) { case 1: cw_log(CW_LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name); cw_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax); /* set mute mode if desired */ break; case 2: cw_log(CW_LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name); cw_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax); /* set mute mode if desired */ break; default: cw_log(CW_LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name); cw_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); /* set mute mode if desired */ break; } break; case CW_OPTION_MUTECONF: zt_confmute(p, *(char *)data); break; case CW_OPTION_TDD: /* turn on or off TDD */ cp = (char *) data; p->mate = 0; if (!*cp) { /* turn it off */ cw_log(CW_LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name); if (p->tdd) tdd_free(p->tdd); p->tdd = FALSE; break; } cw_log(CW_LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n", (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name); zt_disable_ec(p); /* otherwise, turn it on */ if (!p->didtdd) { /* if haven't done it yet */ uint8_t mybuf[41000]; uint8_t *buf; int size,res,fd,len; struct pollfd fds[1]; buf = mybuf; memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */ cw_gen_ecdisa(buf + 16000, 16000, CW_LAW(p)); /* put in tone */ len = 40000; index = zt_get_index(chan, p, 0); if (index < 0) { cw_log(CW_LOG_WARNING, "No index in TDD?\n"); return -1; } fd = p->subs[index].zfd; while(len) { if (cw_check_hangup(chan)) return -1; size = len; if (size > READ_SIZE) size = READ_SIZE; fds[0].fd = fd; fds[0].events = POLLPRI | POLLOUT; fds[0].revents = 0; res = poll(fds, 1, -1); if (!res) { cw_log(CW_LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel); continue; } /* if got exception */ if (fds[0].revents & POLLPRI) return -1; if (!(fds[0].revents & POLLOUT)) { cw_log(CW_LOG_DEBUG, "write fd not ready on channel %d\n", p->channel); continue; } res = write(fd, buf, size); if (res != size) { if (res == -1) return -1; cw_log(CW_LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel); break; } len -= size; buf += size; } p->didtdd = TRUE; /* set to have done it now */ } if (*cp == 2) { /* Mate mode */ if (p->tdd) tdd_free(p->tdd); p->tdd = FALSE; p->mate = 1; break; } if (!p->tdd) { /* if we dont have one yet */ p->tdd = tdd_new(); /* allocate one */ } break; case CW_OPTION_RELAXDTMF: /* Relax DTMF decoding (or not) */ if (!p->dsp) break; cp = (char *) data; cw_log(CW_LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n", *cp ? "ON" : "OFF", (int) *cp, chan->name); cw_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax); break; case CW_OPTION_AUDIO_MODE: /* Set AUDIO mode (or not) */ cp = (char *) data; if (!*cp) { cw_log(CW_LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name); x = 0; zt_disable_ec(p); } else { cw_log(CW_LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name); x = 1; } if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1) cw_log(CW_LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x); break; } errno = 0; return 0; } static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock) { /* Unlink a specific slave or all slaves/masters from a given master */ int x; int hasslaves; if (!master) return; if (needlock) { cw_mutex_lock(&master->lock); if (slave) { while(cw_mutex_trylock(&slave->lock)) { cw_mutex_unlock(&master->lock); usleep(1); cw_mutex_lock(&master->lock); } } } hasslaves = 0; for (x=0;xslaves[x]) { if (!slave || (master->slaves[x] == slave)) { /* Take slave out of the conference */ cw_log(CW_LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel); conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL); conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL); master->slaves[x]->master = NULL; master->slaves[x] = NULL; } else hasslaves = 1; } if (!hasslaves) master->inconference = 0; } if (!slave) { if (master->master) { /* Take master out of the conference */ conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL); conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL); hasslaves = 0; for (x=0;xmaster->slaves[x] == master) master->master->slaves[x] = NULL; else if (master->master->slaves[x]) hasslaves = 1; } if (!hasslaves) master->master->inconference = 0; } master->master = NULL; } update_conf(master); if (needlock) { if (slave) cw_mutex_unlock(&slave->lock); cw_mutex_unlock(&master->lock); } } static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) { int x; if (!slave || !master) { cw_log(CW_LOG_WARNING, "Tried to link to/from NULL??\n"); return; } for (x=0;xslaves[x]) { master->slaves[x] = slave; break; } } if (x >= MAX_SLAVES) { cw_log(CW_LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel); master->slaves[MAX_SLAVES - 1] = slave; } if (slave->master) cw_log(CW_LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel); slave->master = master; cw_log(CW_LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x); } static void disable_dtmf_detect(struct zt_pvt *p) { #ifdef ZT_TONEDETECT int val; #endif p->ignoredtmf = 1; #ifdef ZT_TONEDETECT val = 0; ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val); #endif } static void enable_dtmf_detect(struct zt_pvt *p) { #ifdef ZT_TONEDETECT int val; #endif p->ignoredtmf = 0; #ifdef ZT_TONEDETECT val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE; ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val); #endif } static enum cw_bridge_result zt_bridge(struct cw_channel *c0, struct cw_channel *c1, int flags, struct cw_frame **fo, struct cw_channel **rc, int timeoutms) { struct cw_channel *who; struct zt_pvt *p0, *p1, *op0, *op1; struct zt_pvt *master = NULL, *slave = NULL; struct cw_frame *f; int inconf = 0; int nothingok = 1; int ofd0, ofd1; int oi0, oi1, i0 = -1, i1 = -1, t0, t1; int os0 = -1, os1 = -1; int priority = 0; struct cw_channel *oc0, *oc1; enum cw_bridge_result res; #ifdef PRI_2BCT int triedtopribridge = 0; q931_call *q931c0 = NULL, *q931c1 = NULL; #endif /* For now, don't attempt to native bridge if either channel needs DTMF detection. There is code below to handle it properly until DTMF is actually seen, but due to currently unresolved issues it's ignored... */ if (flags & (CW_BRIDGE_DTMF_CHANNEL_0 | CW_BRIDGE_DTMF_CHANNEL_1)) return CW_BRIDGE_FAILED_NOWARN; cw_mutex_lock(&c0->lock); cw_mutex_lock(&c1->lock); p0 = c0->tech_pvt; p1 = c1->tech_pvt; /* cant do pseudo-channels here */ if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) { cw_mutex_unlock(&c0->lock); cw_mutex_unlock(&c1->lock); return CW_BRIDGE_FAILED_NOWARN; } oi0 = zt_get_index(c0, p0, 0); oi1 = zt_get_index(c1, p1, 0); if ((oi0 < 0) || (oi1 < 0)) { cw_mutex_unlock(&c0->lock); cw_mutex_unlock(&c1->lock); return CW_BRIDGE_FAILED; } op0 = p0 = c0->tech_pvt; op1 = p1 = c1->tech_pvt; ofd0 = c0->fds[0]; ofd1 = c1->fds[0]; oc0 = p0->owner; oc1 = p1->owner; cw_mutex_lock(&p0->lock); if (cw_mutex_trylock(&p1->lock)) { /* Don't block, due to potential for deadlock */ cw_mutex_unlock(&p0->lock); cw_mutex_unlock(&c0->lock); cw_mutex_unlock(&c1->lock); cw_log(CW_LOG_NOTICE, "Avoiding deadlock...\n"); return CW_BRIDGE_RETRY; } if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) { if (p0->owner && p1->owner) { /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */ if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) { master = p0; slave = p1; inconf = 1; } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) { master = p1; slave = p0; inconf = 1; } else { cw_log(CW_LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n"); cw_log(CW_LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n", p0->channel, oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p0->subs[SUB_REAL].inthreeway, p0->channel, oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0, p1->subs[SUB_REAL].inthreeway); } nothingok = 0; } } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) { if (p1->subs[SUB_THREEWAY].inthreeway) { master = p1; slave = p0; nothingok = 0; } } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) { if (p0->subs[SUB_THREEWAY].inthreeway) { master = p0; slave = p1; nothingok = 0; } } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) { /* We have a real and a call wait. If we're in a three way call, put us in it, otherwise, don't put us in anything */ if (p1->subs[SUB_CALLWAIT].inthreeway) { master = p1; slave = p0; nothingok = 0; } } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) { /* Same as previous */ if (p0->subs[SUB_CALLWAIT].inthreeway) { master = p0; slave = p1; nothingok = 0; } } cw_log(CW_LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n", master ? master->channel : 0, slave ? slave->channel : 0, nothingok); if (master && slave) { /* Stop any tones, or play ringtone as appropriate. If they're bridged in an active threeway call with a channel that is ringing, we should indicate ringing. */ if ((oi1 == SUB_THREEWAY) && p1->subs[SUB_THREEWAY].inthreeway && p1->subs[SUB_REAL].owner && p1->subs[SUB_REAL].inthreeway && (p1->subs[SUB_REAL].owner->_state == CW_STATE_RINGING)) { cw_log(CW_LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name); tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE); os1 = p1->subs[SUB_REAL].owner->_state; } else { cw_log(CW_LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1); tone_zone_play_tone(p0->subs[oi0].zfd, -1); } if ((oi0 == SUB_THREEWAY) && p0->subs[SUB_THREEWAY].inthreeway && p0->subs[SUB_REAL].owner && p0->subs[SUB_REAL].inthreeway && (p0->subs[SUB_REAL].owner->_state == CW_STATE_RINGING)) { cw_log(CW_LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name); tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE); os0 = p0->subs[SUB_REAL].owner->_state; } else { cw_log(CW_LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0); tone_zone_play_tone(p1->subs[oi0].zfd, -1); } if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) { if (!p0->echocanbridged || !p1->echocanbridged) { /* Disable echo cancellation if appropriate */ zt_disable_ec(p0); zt_disable_ec(p1); } } zt_link(slave, master); master->inconference = inconf; } else if (!nothingok) cw_log(CW_LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]); update_conf(p0); update_conf(p1); t0 = p0->subs[SUB_REAL].inthreeway; t1 = p1->subs[SUB_REAL].inthreeway; cw_mutex_unlock(&p0->lock); cw_mutex_unlock(&p1->lock); cw_mutex_unlock(&c0->lock); cw_mutex_unlock(&c1->lock); /* Native bridge failed */ if ((!master || !slave) && !nothingok) { zt_enable_ec(p0); zt_enable_ec(p1); return CW_BRIDGE_FAILED; } if (!(flags & CW_BRIDGE_DTMF_CHANNEL_0)) disable_dtmf_detect(op0); if (!(flags & CW_BRIDGE_DTMF_CHANNEL_1)) disable_dtmf_detect(op1); for (;;) { struct cw_channel *c0_priority[2] = {c0, c1}; struct cw_channel *c1_priority[2] = {c1, c0}; /* Here's our main loop... Start by locking things, looking for private parts, and then balking if anything is wrong */ cw_mutex_lock(&c0->lock); cw_mutex_lock(&c1->lock); p0 = c0->tech_pvt; p1 = c1->tech_pvt; if (op0 == p0) i0 = zt_get_index(c0, p0, 1); if (op1 == p1) i1 = zt_get_index(c1, p1, 1); cw_mutex_unlock(&c0->lock); cw_mutex_unlock(&c1->lock); if (!timeoutms || (op0 != p0) || (op1 != p1) || (ofd0 != c0->fds[0]) || (ofd1 != c1->fds[0]) || (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) || (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) || (oc0 != p0->owner) || (oc1 != p1->owner) || (t0 != p0->subs[SUB_REAL].inthreeway) || (t1 != p1->subs[SUB_REAL].inthreeway) || (oi0 != i0) || (oi1 != i1)) { cw_log(CW_LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n", op0->channel, oi0, op1->channel, oi1); res = CW_BRIDGE_RETRY; goto return_from_bridge; } #ifdef PRI_2BCT q931c0 = p0->call; q931c1 = p1->call; if (p0->transfer && p1->transfer && q931c0 && q931c1 && !triedtopribridge) { pri_channel_bridge(q931c0, q931c1); triedtopribridge = 1; } #endif who = cw_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms); if (!who) { cw_log(CW_LOG_DEBUG, "Ooh, empty read...\n"); continue; } f = cw_read(who); if (!f || (f->frametype == CW_FRAME_CONTROL)) { *fo = f; *rc = who; res = CW_BRIDGE_COMPLETE; goto return_from_bridge; } if (f->frametype == CW_FRAME_DTMF) { if ((who == c0) && p0->pulsedial) { cw_write(c1, &f); } else if (p1->pulsedial) { cw_write(c0, &f); } else { *fo = f; *rc = who; res = CW_BRIDGE_COMPLETE; goto return_from_bridge; } } cw_fr_free(f); /* Swap who gets priority */ priority = !priority; } return_from_bridge: if (op0 == p0) zt_enable_ec(p0); if (op1 == p1) zt_enable_ec(p1); if (!(flags & CW_BRIDGE_DTMF_CHANNEL_0)) enable_dtmf_detect(op0); if (!(flags & CW_BRIDGE_DTMF_CHANNEL_1)) enable_dtmf_detect(op1); zt_unlink(slave, master, 1); return res; } static int zt_fixup(struct cw_channel *oldchan, struct cw_channel *newchan) { struct zt_pvt *p = newchan->tech_pvt; int x; cw_mutex_lock(&p->lock); cw_log(CW_LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name); if (p->owner == oldchan) { p->owner = newchan; } for (x=0;x<3;x++) if (p->subs[x].owner == oldchan) { if (!x) zt_unlink(NULL, p, 0); p->subs[x].owner = newchan; } if (newchan->_state == CW_STATE_RINGING) zt_indicate(newchan, CW_CONTROL_RINGING); update_conf(p); cw_mutex_unlock(&p->lock); return 0; } static int zt_ring_phone(struct zt_pvt *p) { int x; int res; /* Make sure our transmit state is on hook */ x = 0; x = ZT_ONHOOK; res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x); do { x = ZT_RING; res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x); if (res) { switch(errno) { case EBUSY: case EINTR: /* Wait just in case */ usleep(10000); continue; case EINPROGRESS: res = 0; break; default: cw_log(CW_LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno)); res = 0; } } } while (res); return res; } static void *ss_thread(void *data); static struct cw_channel *zt_new(struct zt_pvt *, int, int, int, int, int); static int attempt_transfer(struct zt_pvt *p) { /* In order to transfer, we need at least one of the channels to actually be in a call bridge. We can't conference two applications together (but then, why would we want to?) */ if (cw_bridged_channel(p->subs[SUB_REAL].owner)) { /* The three-way person we're about to transfer to could still be in MOH, so stop if now if appropriate */ if (cw_bridged_channel(p->subs[SUB_THREEWAY].owner)) cw_moh_stop(cw_bridged_channel(p->subs[SUB_THREEWAY].owner)); if (p->subs[SUB_THREEWAY].owner->_state == CW_STATE_RINGING) { cw_indicate(cw_bridged_channel(p->subs[SUB_REAL].owner), CW_CONTROL_RINGING); } if (p->subs[SUB_REAL].owner->cdr) { /* Move CDR from second channel to current one */ p->subs[SUB_THREEWAY].owner->cdr = cw_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr); p->subs[SUB_REAL].owner->cdr = NULL; } if (cw_bridged_channel(p->subs[SUB_REAL].owner)->cdr) { /* Move CDR from second channel's bridge to current one */ p->subs[SUB_THREEWAY].owner->cdr = cw_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, cw_bridged_channel(p->subs[SUB_REAL].owner)->cdr); cw_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL; } if (cw_channel_masquerade(p->subs[SUB_THREEWAY].owner, cw_bridged_channel(p->subs[SUB_REAL].owner))) { cw_log(CW_LOG_WARNING, "Unable to masquerade %s as %s\n", cw_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name); return -1; } /* Orphan the channel after releasing the lock */ cw_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock); unalloc_sub(p, SUB_THREEWAY); } else if (cw_bridged_channel(p->subs[SUB_THREEWAY].owner)) { if (p->subs[SUB_REAL].owner->_state == CW_STATE_RINGING) { cw_indicate(cw_bridged_channel(p->subs[SUB_THREEWAY].owner), CW_CONTROL_RINGING); } cw_moh_stop(cw_bridged_channel(p->subs[SUB_THREEWAY].owner)); if (p->subs[SUB_THREEWAY].owner->cdr) { /* Move CDR from second channel to current one */ p->subs[SUB_REAL].owner->cdr = cw_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr); p->subs[SUB_THREEWAY].owner->cdr = NULL; } if (cw_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr) { /* Move CDR from second channel's bridge to current one */ p->subs[SUB_REAL].owner->cdr = cw_cdr_append(p->subs[SUB_REAL].owner->cdr, cw_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr); cw_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr = NULL; } if (cw_channel_masquerade(p->subs[SUB_REAL].owner, cw_bridged_channel(p->subs[SUB_THREEWAY].owner))) { cw_log(CW_LOG_WARNING, "Unable to masquerade %s as %s\n", cw_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name); return -1; } /* Three-way is now the REAL */ swap_subs(p, SUB_THREEWAY, SUB_REAL); cw_mutex_unlock(&p->subs[SUB_REAL].owner->lock); unalloc_sub(p, SUB_THREEWAY); /* Tell the caller not to hangup */ return 1; } else { cw_log(CW_LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n", p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name); p->subs[SUB_THREEWAY].owner->_softhangup |= CW_SOFTHANGUP_DEV; return -1; } return 0; } static int check_for_conference(struct zt_pvt *p) { ZT_CONFINFO ci; /* Fine if we already have a master, etc */ if (p->master || (p->confno > -1)) return 0; memset(&ci, 0, sizeof(ci)); if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) { cw_log(CW_LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel); return 0; } /* If we have no master and don't have a confno, then if we're in a conference, it's probably a MeetMe room or some such, so don't let us 3-way out! */ if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) { if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n"); return 1; } return 0; } static int get_alarms(struct zt_pvt *p) { int res; ZT_SPANINFO zi; memset(&zi, 0, sizeof(zi)); zi.spanno = p->span; res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi); if (res < 0) { cw_log(CW_LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel); return 0; } return zi.alarms; } static struct cw_frame *zt_handle_event(struct cw_channel *cw) { int res,x; int index; char *c; struct zt_pvt *p = cw->tech_pvt; pthread_t threadid; struct cw_channel *chan; index = zt_get_index(cw, p, 0); if (index < 0) { p->subs[index].f.frametype = CW_FRAME_NULL; return &p->subs[index].f; } if (p->fake_event) { res = p->fake_event; p->fake_event = 0; } else res = zt_get_event(p->subs[index].zfd); cw_log(CW_LOG_DEBUG, "%s: Got event %s(%d) on channel %d (index %d)\n", cw->name, event2str(res), res, p->channel, index); if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFUP)) { if (res & ZT_EVENT_PULSEDIGIT) p->pulsedial = 1; else p->pulsedial = 0; cw_log(CW_LOG_DEBUG, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse ": "", res & 0xff); #ifdef ZAPATA_PRI if (!p->proceeding && p->sig == SIG_PRI && p->pri && p->pri->overlapdial) { p->subs[index].f.frametype = CW_FRAME_NULL; p->subs[index].f.subclass = 0; } else { #endif p->subs[index].f.frametype = CW_FRAME_DTMF; p->subs[index].f.subclass = res & 0xff; #ifdef ZAPATA_PRI } #endif /* Unmute conference, return the captured digit */ zt_confmute(p, 0); return &p->subs[index].f; } if (res & ZT_EVENT_DTMFDOWN) { cw_log(CW_LOG_DEBUG, "DTMF Down '%c'\n", res & 0xff); p->subs[index].f.frametype = CW_FRAME_NULL; p->subs[index].f.subclass = 0; zt_confmute(p, 1); /* Mute conference, return null frame */ return &p->subs[index].f; } switch(res) { case ZT_EVENT_BITSCHANGED: cw_log(CW_LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig)); break; case ZT_EVENT_PULSE_START: /* Stop tone if there's a pulse start and the PBX isn't started */ if (!cw->pbx) tone_zone_play_tone(p->subs[index].zfd, -1); break; case ZT_EVENT_DIALCOMPLETE: if (p->inalarm) break; if (p->radio) break; if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) { cw_log(CW_LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",cw->name); return NULL; } if (!x) { /* if not still dialing in driver */ zt_enable_ec(p); if (p->echobreak) { zt_train_ec(p); cw_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr)); p->dop.op = ZT_DIAL_OP_REPLACE; res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop); p->echobreak = 0; } else { p->dialing = 0; if (p->sig == SIG_E911) { /* if thru with dialing after offhook */ if (cw->_state == CW_STATE_DIALING_OFFHOOK) { cw_setstate(cw, CW_STATE_UP); p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_ANSWER; break; } else { /* if to state wait for offhook to dial rest */ /* we now wait for off hook */ cw_setstate(cw,CW_STATE_DIALING_OFFHOOK); } } if (cw->_state == CW_STATE_DIALING) { if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) { cw_log(CW_LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n"); } else if (p->confirmanswer || (!p->dialednone && ((p->sig == SIG_EM) || (p->sig == SIG_EM_E1) || (p->sig == SIG_EMWINK) || (p->sig == SIG_FEATD) || (p->sig == SIG_FEATDMF) || (p->sig == SIG_E911) || (p->sig == SIG_FEATB) || (p->sig == SIG_SF) || (p->sig == SIG_SFWINK) || (p->sig == SIG_SF_FEATD) || (p->sig == SIG_SF_FEATDMF) || (p->sig == SIG_SF_FEATB)))) { cw_setstate(cw, CW_STATE_RINGING); } else if (!p->answeronpolarityswitch) { cw_setstate(cw, CW_STATE_UP); p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_ANSWER; } } } } break; case ZT_EVENT_ALARM: #ifdef ZAPATA_PRI if (p->call) { if (p->pri && p->pri->pri) { if (!pri_grab(p, p->pri)) { pri_hangup(p->pri->pri, p->call, -1); pri_destroycall(p->pri->pri, p->call); p->call = NULL; pri_rel(p->pri); } else cw_log(CW_LOG_WARNING, "Failed to grab PRI!\n"); } else cw_log(CW_LOG_WARNING, "The PRI Call have not been destroyed\n"); } if (p->owner) p->owner->_softhangup |= CW_SOFTHANGUP_DEV; if (p->bearer) p->bearer->inalarm = 1; else #endif p->inalarm = 1; res = get_alarms(p); cw_log(CW_LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res)); manager_event(EVENT_FLAG_SYSTEM, "Alarm", "Alarm: %s\r\n" "Channel: %d\r\n", alarm2str(res), p->channel); /* fall through intentionally */ case ZT_EVENT_ONHOOK: if (p->radio) { p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_RADIO_UNKEY; break; } switch(p->sig) { case SIG_FXOLS: case SIG_FXOGS: case SIG_FXOKS: p->onhooktime = time(NULL); p->msgstate = -1; /* Check for some special conditions regarding call waiting */ if (index == SUB_REAL) { /* The normal line was hung up */ if (p->subs[SUB_CALLWAIT].owner) { /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */ swap_subs(p, SUB_CALLWAIT, SUB_REAL); if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel); unalloc_sub(p, SUB_CALLWAIT); #if 0 p->subs[index].needanswer = 0; p->subs[index].needringing = 0; #endif p->callwaitingrepeat = 0; p->cidcwexpire = 0; p->owner = NULL; /* Don't start streaming audio yet if the incoming call isn't up yet */ if (p->subs[SUB_REAL].owner->_state != CW_STATE_UP) p->dialing = 1; zt_ring_phone(p); } else if (p->subs[SUB_THREEWAY].owner) { unsigned int mssinceflash; /* Here we have to retain the lock on both the main channel, the 3-way channel, and the private structure -- not especially easy or clean */ while(p->subs[SUB_THREEWAY].owner && cw_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) { /* Yuck, didn't get the lock on the 3-way, gotta release everything and re-grab! */ cw_mutex_unlock(&p->lock); cw_mutex_unlock(&cw->lock); usleep(1); /* We can grab cw and p in that order, without worry. We should make sure nothing seriously bad has happened though like some sort of bizarre double masquerade! */ cw_mutex_lock(&cw->lock); cw_mutex_lock(&p->lock); if (p->owner != cw) { cw_log(CW_LOG_WARNING, "This isn't good...\n"); return NULL; } } if (!p->subs[SUB_THREEWAY].owner) { cw_log(CW_LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n"); return NULL; } mssinceflash = cw_tvdiff_ms(cw_tvnow(), p->flashtime); cw_log(CW_LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash); if (mssinceflash < MIN_MS_SINCE_FLASH) { /* It hasn't been long enough since the last flashook. This is probably a bounce on hanging up. Hangup both channels now */ if (p->subs[SUB_THREEWAY].owner) cw_queue_hangup(p->subs[SUB_THREEWAY].owner); p->subs[SUB_THREEWAY].owner->_softhangup |= CW_SOFTHANGUP_DEV; cw_log(CW_LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel); cw_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock); } else if ((cw->pbx) || (cw->_state == CW_STATE_UP)) { if (p->transfer) { /* In any case this isn't a threeway call anymore */ p->subs[SUB_REAL].inthreeway = 0; p->subs[SUB_THREEWAY].inthreeway = 0; /* Only attempt transfer if the phone is ringing; why transfer to busy tone eh? */ if (!p->transfertobusy && cw->_state == CW_STATE_BUSY) { cw_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock); /* Swap subs and dis-own channel */ swap_subs(p, SUB_THREEWAY, SUB_REAL); p->owner = NULL; /* Ring the phone */ zt_ring_phone(p); } else { if ((res = attempt_transfer(p)) < 0) { p->subs[SUB_THREEWAY].owner->_softhangup |= CW_SOFTHANGUP_DEV; if (p->subs[SUB_THREEWAY].owner) cw_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock); } else if (res) { /* Don't actually hang up at this point */ if (p->subs[SUB_THREEWAY].owner) cw_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock); break; } } } else { p->subs[SUB_THREEWAY].owner->_softhangup |= CW_SOFTHANGUP_DEV; if (p->subs[SUB_THREEWAY].owner) cw_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock); } } else { cw_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock); /* Swap subs and dis-own channel */ swap_subs(p, SUB_THREEWAY, SUB_REAL); p->owner = NULL; /* Ring the phone */ zt_ring_phone(p); } } } else { cw_log(CW_LOG_WARNING, "Got a hangup and my index is %d?\n", index); } /* Fall through */ default: zt_disable_ec(p); return NULL; } break; case ZT_EVENT_RINGOFFHOOK: if (p->inalarm) break; if (p->radio) { p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_RADIO_KEY; break; } /* for E911, its supposed to wait for offhook then dial the second half of the dial string */ if ((p->sig == SIG_E911) && (cw->_state == CW_STATE_DIALING_OFFHOOK)) { c = strchr(p->dialdest, '/'); if (c) c++; else c = p->dialdest; if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c); else cw_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr)); if (strlen(p->dop.dialstr) > 4) { memset(p->echorest, 'w', sizeof(p->echorest) - 1); strcpy(p->echorest + (p->echotraining / 401) + 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 (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) { x = ZT_ONHOOK; ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x); cw_log(CW_LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno)); return NULL; } p->dialing = 1; return &p->subs[index].f; } switch(p->sig) { case SIG_FXOLS: case SIG_FXOGS: case SIG_FXOKS: switch(cw->_state) { case CW_STATE_RINGING: zt_enable_ec(p); zt_train_ec(p); p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_ANSWER; /* Make sure it stops ringing */ zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK); cw_log(CW_LOG_DEBUG, "channel %d answered\n", p->channel); /* Cancel any running CallerID spill */ if (p->cidspill) { free(p->cidspill); p->cidspill = NULL; } if (p->cidspill2) { free(p->cidspill2); p->cidspill2 = NULL; } p->dialing = 0; p->callwaitcas = 0; if (p->confirmanswer) { /* Ignore answer if "confirm answer" is enabled */ p->subs[index].f.frametype = CW_FRAME_NULL; p->subs[index].f.subclass = 0; } else if (!cw_strlen_zero(p->dop.dialstr)) { /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */ res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop); if (res < 0) { cw_log(CW_LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel); p->dop.dialstr[0] = '\0'; return NULL; } else { cw_log(CW_LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr); p->subs[index].f.frametype = CW_FRAME_NULL; p->subs[index].f.subclass = 0; p->dialing = 1; } p->dop.dialstr[0] = '\0'; cw_setstate(cw, CW_STATE_DIALING); } else cw_setstate(cw, CW_STATE_UP); return &p->subs[index].f; case CW_STATE_DOWN: cw_setstate(cw, CW_STATE_RING); cw->rings = 1; p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_OFFHOOK; cw_log(CW_LOG_DEBUG, "channel %d picked up\n", p->channel); return &p->subs[index].f; case CW_STATE_UP: /* Make sure it stops ringing */ zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK); /* Okay -- probably call waiting*/ if (cw_bridged_channel(p->owner)) cw_moh_stop(cw_bridged_channel(p->owner)); break; case CW_STATE_RESERVED: /* Start up dialtone */ if (cw_app_has_voicemail(p->mailbox, NULL)) res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_STUTTER); else res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE); break; default: cw_log(CW_LOG_WARNING, "FXO phone off hook in weird state %d??\n", cw->_state); } break; case SIG_FXSLS: case SIG_FXSGS: case SIG_FXSKS: if (cw->_state == CW_STATE_RING) p->ringt = p->ringt_base; /* If we get a ring then we cannot be in * reversed polarity. So we reset to idle */ cw_log(CW_LOG_DEBUG, "Setting IDLE polarity due " "to ring. Old polarity was %d\n", p->polarity); p->polarity = POLARITY_IDLE; /* Fall through */ case SIG_EM: case SIG_EM_E1: case SIG_EMWINK: case SIG_FEATD: case SIG_FEATDMF: case SIG_FEATDMF_TA: case SIG_E911: case SIG_FEATB: case SIG_SF: case SIG_SFWINK: case SIG_SF_FEATD: case SIG_SF_FEATDMF: case SIG_SF_FEATB: if (cw->_state == CW_STATE_PRERING) cw_setstate(cw, CW_STATE_RING); if ((cw->_state == CW_STATE_DOWN) || (cw->_state == CW_STATE_RING)) { if (option_debug) cw_log(CW_LOG_DEBUG, "Ring detected\n"); p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_RING; } else if (p->outgoing && ((cw->_state == CW_STATE_RINGING) || (cw->_state == CW_STATE_DIALING))) { if (option_debug) cw_log(CW_LOG_DEBUG, "Line answered\n"); if (p->confirmanswer) { p->subs[index].f.frametype = CW_FRAME_NULL; p->subs[index].f.subclass = 0; } else { p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_ANSWER; cw_setstate(cw, CW_STATE_UP); } } else if (cw->_state != CW_STATE_RING) cw_log(CW_LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", cw->_state, p->channel); break; default: cw_log(CW_LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig); } break; #ifdef ZT_EVENT_RINGBEGIN case ZT_EVENT_RINGBEGIN: switch(p->sig) { case SIG_FXSLS: case SIG_FXSGS: case SIG_FXSKS: if (cw->_state == CW_STATE_RING) p->ringt = p->ringt_base; break; } break; #endif case ZT_EVENT_RINGEROFF: if (p->inalarm) break; if (p->radio) break; cw->rings++; if (p->cidspill && cw->rings == p->cidrings) cw_log(CW_LOG_DEBUG, "%s: Send post-ring caller ID\n", cw->name); p->subs[index].f.frametype = CW_FRAME_CONTROL; p->subs[index].f.subclass = CW_CONTROL_RINGING; break; case ZT_EVENT_RINGERON: if (p->cidspill && cw->rings > p->cidrings) { cw_log(CW_LOG_WARNING, "%s: Didn't finish sending caller ID before start of ring!\n", cw->name); free(p->cidspill); p->cidspill = NULL; p->callwaitcas = 0; } break; case ZT_EVENT_NOALARM: p->inalarm = 0; #ifdef ZAPATA_PRI /* Extremely unlikely but just in case */ if (p->bearer) p->bearer->inalarm = 0; #endif cw_log(CW_LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel); manager_event(EVENT_FLAG_SYSTEM, "AlarmClear", "Channel: %d\r\n", p->channel); break; case ZT_EVENT_WINKFLASH: if (p->inalarm) break; if (p->radio) break; /* Remember last time we got a flash-hook */ gettimeofday(&p->flashtime, NULL); switch(p->sig) { case SIG_FXOLS: case SIG_FXOGS: case SIG_FXOKS: cw_log(CW_LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n", index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd); p->callwaitcas = 0; if (index != SUB_REAL) { cw_log(CW_LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel); goto winkflashdone; } if (p->subs[SUB_CALLWAIT].owner) { /* Swap to call-wait */ swap_subs(p, SUB_REAL, SUB_CALLWAIT); tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1); p->owner = p->subs[SUB_REAL].owner; cw_log(CW_LOG_DEBUG, "Making %s the new owner\n", p->owner->name); if (p->owner->_state == CW_STATE_RINGING) { cw_setstate(p->owner, CW_STATE_UP); p->subs[SUB_REAL].needanswer = 1; } p->callwaitingrepeat = 0; p->cidcwexpire = 0; /* Start music on hold if appropriate */ if (!p->subs[SUB_CALLWAIT].inthreeway && cw_bridged_channel(p->subs[SUB_CALLWAIT].owner)) cw_moh_start(cw_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL); if (cw_bridged_channel(p->subs[SUB_REAL].owner)) cw_moh_stop(cw_bridged_channel(p->subs[SUB_REAL].owner)); } else if (!p->subs[SUB_THREEWAY].owner) { char cid_num[256]; char cid_name[256]; if (!p->threewaycalling) { /* Just send a flash if no 3-way calling */ p->subs[SUB_REAL].needflash = 1; goto winkflashdone; } else if (!check_for_conference(p)) { if (p->zaptrcallerid && p->owner) { if (p->owner->cid.cid_num) cw_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num)); if (p->owner->cid.cid_name) cw_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name)); } /* XXX This section needs much more error checking!!! XXX */ /* Start a 3-way call if feasible */ if (!((cw->pbx) || (cw->_state == CW_STATE_UP) || (cw->_state == CW_STATE_RING))) { cw_log(CW_LOG_DEBUG, "Flash when call not up or ringing\n"); goto winkflashdone; } if (alloc_sub(p, SUB_THREEWAY)) { cw_log(CW_LOG_WARNING, "Unable to allocate three-way subchannel\n"); goto winkflashdone; } /* Make new channel */ chan = zt_new(p, CW_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0); if (p->zaptrcallerid) { if (!p->origcid_num) p->origcid_num = strdup(p->cid_num); if (!p->origcid_name) p->origcid_name = strdup(p->cid_name); cw_copy_string(p->cid_num, cid_num, sizeof(p->cid_num)); cw_copy_string(p->cid_name, cid_name, sizeof(p->cid_name)); } /* Swap things around between the three-way and real call */ swap_subs(p, SUB_THREEWAY, SUB_REAL); /* Disable echo canceller for better dialing */ zt_disable_ec(p); res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL); if (res) cw_log(CW_LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel); p->owner = chan; if (!chan) { cw_log(CW_LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel); } else if (cw_pthread_create(&threadid, &global_attr_detached, ss_thread, chan)) { cw_log(CW_LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel); res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION); zt_enable_ec(p); cw_hangup(chan); } else { if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel); /* Start music on hold if appropriate */ if (cw_bridged_channel(p->subs[SUB_THREEWAY].owner)) cw_moh_start(cw_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL); } } } else { /* Already have a 3 way call */ if (p->subs[SUB_THREEWAY].inthreeway) { /* Call is already up, drop the last person */ if (option_debug) cw_log(CW_LOG_DEBUG, "Got flash with three way call up, dropping last call on %d\n", p->channel); /* If the primary call isn't answered yet, use it */ if ((p->subs[SUB_REAL].owner->_state != CW_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == CW_STATE_UP)) { /* Swap back -- we're dropping the real 3-way that isn't finished yet*/ swap_subs(p, SUB_THREEWAY, SUB_REAL); p->owner = p->subs[SUB_REAL].owner; } /* Drop the last call and stop the conference */ if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name); p->subs[SUB_THREEWAY].owner->_softhangup |= CW_SOFTHANGUP_DEV; p->subs[SUB_REAL].inthreeway = 0; p->subs[SUB_THREEWAY].inthreeway = 0; } else { /* Lets see what we're up to */ if (((cw->pbx) || (cw->_state == CW_STATE_UP)) && (p->transfertobusy || (cw->_state != CW_STATE_BUSY))) { int otherindex = SUB_THREEWAY; if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "Building conference on call on %s and %s\n", p->subs[SUB_THREEWAY].owner->name, p->subs[SUB_REAL].owner->name); /* Put them in the threeway, and flip */ p->subs[SUB_THREEWAY].inthreeway = 1; p->subs[SUB_REAL].inthreeway = 1; if (cw->_state == CW_STATE_UP) { swap_subs(p, SUB_THREEWAY, SUB_REAL); otherindex = SUB_REAL; } if (p->subs[otherindex].owner && cw_bridged_channel(p->subs[otherindex].owner)) cw_moh_stop(cw_bridged_channel(p->subs[otherindex].owner)); p->owner = p->subs[SUB_REAL].owner; if (cw->_state == CW_STATE_RINGING) { cw_log(CW_LOG_DEBUG, "Enabling ringtone on real and threeway\n"); res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE); res = tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, ZT_TONE_RINGTONE); } } else { if (option_verbose > 2) cw_verbose(VERBOSE_PREFIX_3 "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name); swap_subs(p, SUB_THREEWAY, SUB_REAL); p->subs[SUB_THREEWAY].owner->_softhangup |= CW_SOFTHANGUP_DEV; p->owner = p->subs[SUB_REAL].owner; if (p->subs[SUB_REAL].owner && cw_bridged_channel(p->subs[SUB_REAL].owner)) cw_moh_stop(cw_bridged_channel(p->subs[SUB_REAL].owner)); zt_enable_ec(p); } } } winkflashdone: update_conf(p); break; case SIG_EM: case SIG_EM_E1: case SIG_EMWINK: case SIG_FEATD: case SIG_SF: case SIG_SFWINK: case SIG_SF_FEATD: case SIG_FXSLS: case SIG_FXSGS: if (p->dialing) cw_log(CW_LOG_DEBUG, "Ignoring wink on channel %d\n", p->channel); else cw_log(CW_LOG_DEBUG, "Got wink in weird state %d on channel %d\n", cw->_state, p->channel); break; case SIG_FEATDMF_TA: switch (p->whichwink) { case 0: cw_log(CW_LOG_DEBUG, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani); snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani); break; case 1: cw_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr)); break; case 2: cw_log(CW_LOG_WARNING, "Received unexpected wink on channel of type SIG_FEATDMF_TA\n"); return NULL; } p->whichwink++; /* Fall through */ case SIG_FEATDMF: case SIG_E911: case SIG_FEATB: case SIG_SF_FEATDMF: case SIG_SF_FEATB: /* FGD MF *Must* wait for wink */ if (!cw_strlen_zero(p->dop.dialstr)) res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop); else if (res < 0) { cw_log(CW_LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel); p->dop.dialstr[0] = '\0'; return NULL; } else cw_log(CW_LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr); p->dop.dialstr[0] = '\0'; break; default: cw_log(CW_LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig); } break; case ZT_EVENT_HOOKCOMPLETE: if (p->inalarm) break; if (p->radio) break; switch(p->sig) { case SIG_FXSLS: /* only interesting for FXS */ case SIG_FXSGS: case SIG_FXSKS: case SIG_EM: case SIG_EM_E1: case SIG_EMWINK: case SIG_FEATD: case SIG_SF: case SIG_SFWINK: case SIG_SF_FEATD: if (!cw_strlen_zero(p->dop.dialstr)) res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop); else if (res < 0) { cw_log(CW_LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel); p->dop.dialstr[0] = '\0'; return NULL; } else cw_log(CW_LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr); p->dop.dialstr[0] = '\0'; p->dop.op = ZT_DIAL_OP_REPLACE; break; case SIG_FEATDMF: case SIG_E911: case SIG_FEATB: case SIG_SF_FEATDMF: case SIG_SF_FEATB: cw_log(CW_LOG_DEBUG, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel); break; default: break; } break; case ZT_EVENT_POLARITY: /* * If we get a Polarity Switch event, check to see * if we should change the polarity state and * mark the channel as UP or if this is an indication * of remote end disconnect. */ if (p->polarity == POLARITY_IDLE) { p->polarity = POLARITY_REV; if (p->answeronpolarityswitch && ((cw->_state == CW_STATE_DIALING) || (cw->_state == CW_STATE_RINGING))) { cw_log(CW_LOG_DEBUG, "Answering on polarity switch!\n"); cw_setstate(p->owner, CW_STATE_UP); } else cw_log(CW_LOG_DEBUG, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, cw->_state); } /* Removed else statement from here as it was preventing hangups from ever happening*/ /* Added CW_STATE_RING in if statement below to deal with calling party hangups that take place when ringing */ if(p->hanguponpolarityswitch && (p->polarityonanswerdelay > 0) && (p->polarity == POLARITY_REV) && ((cw->_state == CW_STATE_UP) || (cw->_state == CW_STATE_RING)) ) { /* Added log_debug information below to provide a better indication of what is going on */ cw_log(CW_LOG_DEBUG, "Polarity Reversal event occured - DEBUG 1: channel %d, state %d, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %d\n", p->channel, cw->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, cw_tvdiff_ms(cw_tvnow(), p->polaritydelaytv) ); if(cw_tvdiff_ms(cw_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) { cw_log(CW_LOG_DEBUG, "Polarity Reversal detected and now Hanging up on channel %d\n", p->channel); cw_softhangup(p->owner, CW_SOFTHANGUP_EXPLICIT); p->polarity = POLARITY_IDLE; } else { cw_log(CW_LOG_DEBUG, "Polarity Reversal detected but NOT hanging up (too close to answer event) on channel %d, state %d\n", p->channel, cw->_state); } } else { p->polarity = POLARITY_IDLE; cw_log(CW_LOG_DEBUG, "Ignoring Polarity switch to IDLE on channel %d, state %d\n", p->channel, cw->_state); } /* Added more log_debug information below to provide a