Thu May 24 14:21:09 2007

Asterisk developer's documentation


chan_zap.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Zaptel Pseudo TDM interface 
00022  * 
00023  * Connects to the zaptel telephony library as well as 
00024  * libpri. Libpri is optional and needed only if you are
00025  * going to use ISDN connections.
00026  *
00027  * You need to install libraries before you attempt to compile
00028  * and install the zaptel channel.
00029  *
00030  * \par See also
00031  * \arg \ref Config_zap
00032  *
00033  * \ingroup channel_drivers
00034  */
00035 
00036 #include <stdio.h>
00037 #include <string.h>
00038 #ifdef __NetBSD__
00039 #include <pthread.h>
00040 #include <signal.h>
00041 #else
00042 #include <sys/signal.h>
00043 #endif
00044 #include <errno.h>
00045 #include <stdlib.h>
00046 #if !defined(SOLARIS) && !defined(__FreeBSD__)
00047 #include <stdint.h>
00048 #endif
00049 #include <unistd.h>
00050 #include <sys/ioctl.h>
00051 #ifdef __linux__
00052 #include <linux/zaptel.h>
00053 #else
00054 #include <zaptel.h>
00055 #endif /* __linux__ */
00056 #include <math.h>
00057 #include <tonezone.h>
00058 #include <ctype.h>
00059 #ifdef ZAPATA_PRI
00060 #include <libpri.h>
00061 #ifndef PRI_USER_USER_TX
00062 #error "You need newer libpri"
00063 #endif
00064 #endif
00065 #ifdef ZAPATA_R2
00066 #include <libmfcr2.h>
00067 #endif
00068 
00069 #include "asterisk.h"
00070 
00071 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 41069 $")
00072 
00073 #include "asterisk/lock.h"
00074 #include "asterisk/channel.h"
00075 #include "asterisk/config.h"
00076 #include "asterisk/logger.h"
00077 #include "asterisk/module.h"
00078 #include "asterisk/pbx.h"
00079 #include "asterisk/options.h"
00080 #include "asterisk/file.h"
00081 #include "asterisk/ulaw.h"
00082 #include "asterisk/alaw.h"
00083 #include "asterisk/callerid.h"
00084 #include "asterisk/adsi.h"
00085 #include "asterisk/cli.h"
00086 #include "asterisk/cdr.h"
00087 #include "asterisk/features.h"
00088 #include "asterisk/musiconhold.h"
00089 #include "asterisk/say.h"
00090 #include "asterisk/tdd.h"
00091 #include "asterisk/app.h"
00092 #include "asterisk/dsp.h"
00093 #include "asterisk/astdb.h"
00094 #include "asterisk/manager.h"
00095 #include "asterisk/causes.h"
00096 #include "asterisk/term.h"
00097 #include "asterisk/utils.h"
00098 #include "asterisk/transcap.h"
00099 
00100 #ifndef ZT_SIG_EM_E1
00101 #error "Your zaptel is too old.  please cvs update"
00102 #endif
00103 
00104 #ifndef ZT_TONEDETECT
00105 /* Work around older code with no tone detect */
00106 #define ZT_EVENT_DTMFDOWN 0
00107 #define ZT_EVENT_DTMFUP 0
00108 #endif
00109 
00110 /* define this to send PRI user-user information elements */
00111 #undef SUPPORT_USERUSER
00112 
00113 /*! 
00114  * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
00115  * the user hangs up to reset the state machine so ring works properly.
00116  * This is used to be able to support kewlstart by putting the zhone in
00117  * groundstart mode since their forward disconnect supervision is entirely
00118  * broken even though their documentation says it isn't and their support
00119  * is entirely unwilling to provide any assistance with their channel banks
00120  * even though their web site says they support their products for life.
00121  */
00122 /* #define ZHONE_HACK */
00123 
00124 /*! \note
00125  * Define if you want to check the hook state for an FXO (FXS signalled) interface
00126  * before dialing on it.  Certain FXO interfaces always think they're out of
00127  * service with this method however.
00128  */
00129 /* #define ZAP_CHECK_HOOKSTATE */
00130 
00131 /*! \brief Typically, how many rings before we should send Caller*ID */
00132 #define DEFAULT_CIDRINGS 1
00133 
00134 #define CHANNEL_PSEUDO -12
00135 
00136 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
00137 
00138 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
00139 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FEATB)) 
00140 
00141 static const char desc[] = "Zapata Telephony"
00142 #ifdef ZAPATA_PRI
00143                " w/PRI"
00144 #endif
00145 #ifdef ZAPATA_R2
00146                " w/R2"
00147 #endif
00148 ;
00149 
00150 static const char tdesc[] = "Zapata Telephony Driver"
00151 #ifdef ZAPATA_PRI
00152                " w/PRI"
00153 #endif
00154 #ifdef ZAPATA_R2
00155                " w/R2"
00156 #endif
00157 ;
00158 
00159 static const char type[] = "Zap";
00160 static const char config[] = "zapata.conf";
00161 
00162 #define SIG_EM    ZT_SIG_EM
00163 #define SIG_EMWINK   (0x0100000 | ZT_SIG_EM)
00164 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
00165 #define  SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
00166 #define  SIG_FEATB   (0x0800000 | ZT_SIG_EM)
00167 #define  SIG_E911 (0x1000000 | ZT_SIG_EM)
00168 #define  SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
00169 #define SIG_FXSLS ZT_SIG_FXSLS
00170 #define SIG_FXSGS ZT_SIG_FXSGS
00171 #define SIG_FXSKS ZT_SIG_FXSKS
00172 #define SIG_FXOLS ZT_SIG_FXOLS
00173 #define SIG_FXOGS ZT_SIG_FXOGS
00174 #define SIG_FXOKS ZT_SIG_FXOKS
00175 #define SIG_PRI      ZT_SIG_CLEAR
00176 #define SIG_R2    ZT_SIG_CAS
00177 #define  SIG_SF      ZT_SIG_SF
00178 #define SIG_SFWINK   (0x0100000 | ZT_SIG_SF)
00179 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
00180 #define  SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
00181 #define  SIG_SF_FEATB   (0x0800000 | ZT_SIG_SF)
00182 #define SIG_EM_E1 ZT_SIG_EM_E1
00183 #define SIG_GR303FXOKS  (0x0100000 | ZT_SIG_FXOKS)
00184 #define SIG_GR303FXSKS  (0x0100000 | ZT_SIG_FXSKS)
00185 
00186 #define NUM_SPANS       32
00187 #define NUM_DCHANS      4  /*!< No more than 4 d-channels */
00188 #define MAX_CHANNELS 672      /*!< No more than a DS3 per trunk group */
00189 
00190 #define CHAN_PSEUDO  -2
00191 
00192 #define DCHAN_PROVISIONED (1 << 0)
00193 #define DCHAN_NOTINALARM  (1 << 1)
00194 #define DCHAN_UP          (1 << 2)
00195 
00196 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
00197 
00198 static char context[AST_MAX_CONTEXT] = "default";
00199 static char cid_num[256] = "";
00200 static char cid_name[256] = "";
00201 static char defaultcic[64] = "";
00202 static char defaultozz[64] = "";
00203 
00204 static char language[MAX_LANGUAGE] = "";
00205 static char musicclass[MAX_MUSICCLASS] = "";
00206 static char progzone[10]= "";
00207 
00208 static int usedistinctiveringdetection = 0;
00209 
00210 static int transfertobusy = 1;
00211 
00212 static int use_callerid = 1;
00213 static int cid_signalling = CID_SIG_BELL;
00214 static int cid_start = CID_START_RING;
00215 static int zaptrcallerid = 0;
00216 static int cur_signalling = -1;
00217 
00218 static ast_group_t cur_group = 0;
00219 static ast_group_t cur_callergroup = 0;
00220 static ast_group_t cur_pickupgroup = 0;
00221 static int relaxdtmf = 0;
00222 
00223 static int immediate = 0;
00224 
00225 static int stripmsd = 0;
00226 
00227 static int callwaiting = 0;
00228 
00229 static int callwaitingcallerid = 0;
00230 
00231 static int hidecallerid = 0;
00232 
00233 static int restrictcid = 0;
00234 
00235 static int use_callingpres = 0;
00236 
00237 static int callreturn = 0;
00238 
00239 static int threewaycalling = 0;
00240 
00241 static int transfer = 0;
00242 
00243 static int canpark = 0;
00244 
00245 static int cancallforward = 0;
00246 
00247 static float rxgain = 0.0;
00248 
00249 static float txgain = 0.0;
00250 
00251 static int tonezone = -1;
00252 
00253 static int echocancel;
00254 
00255 static int echotraining;
00256 
00257 static int pulse;
00258 
00259 static int echocanbridged = 0;
00260 
00261 static int busydetect = 0;
00262 
00263 static int busycount = 3;
00264 static int busy_tonelength = 0;
00265 static int busy_quietlength = 0;
00266 
00267 static int callprogress = 0;
00268 
00269 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
00270 
00271 static char mailbox[AST_MAX_EXTENSION];
00272 
00273 static int amaflags = 0;
00274 
00275 static int adsi = 0;
00276 
00277 static int numbufs = 4;
00278 
00279 static int cur_prewink = -1;
00280 static int cur_preflash = -1;
00281 static int cur_wink = -1;
00282 static int cur_flash = -1;
00283 static int cur_start = -1;
00284 static int cur_rxwink = -1;
00285 static int cur_rxflash = -1;
00286 static int cur_debounce = -1;
00287 static int cur_priexclusive = 0;
00288 
00289 static int priindication_oob = 0;
00290 
00291 #ifdef ZAPATA_PRI
00292 static int minunused = 2;
00293 static int minidle = 0;
00294 static char idleext[AST_MAX_EXTENSION];
00295 static char idledial[AST_MAX_EXTENSION];
00296 static int overlapdial = 0;
00297 static int facilityenable = 0;
00298 static char internationalprefix[10] = "";
00299 static char nationalprefix[10] = "";
00300 static char localprefix[20] = "";
00301 static char privateprefix[20] = "";
00302 static char unknownprefix[20] = "";
00303 static long resetinterval = 3600;   /*!< How often (in seconds) to reset unused channels. Default 1 hour. */
00304 static struct ast_channel inuse = { "GR-303InUse" };
00305 #ifdef PRI_GETSET_TIMERS
00306 static int pritimers[PRI_MAX_TIMERS];
00307 #endif
00308 static int pridebugfd = -1;
00309 static char pridebugfilename[1024]="";
00310 #endif
00311 
00312 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
00313 static int firstdigittimeout = 16000;
00314 
00315 /*! \brief How long to wait for following digits (FXO logic) */
00316 static int gendigittimeout = 8000;
00317 
00318 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
00319 static int matchdigittimeout = 3000;
00320 
00321 static int usecnt =0;
00322 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
00323 
00324 /*! \brief Protect the interface list (of zt_pvt's) */
00325 AST_MUTEX_DEFINE_STATIC(iflock);
00326 
00327 
00328 static int ifcount = 0;
00329 
00330 #ifdef ZAPATA_PRI
00331 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
00332 #endif
00333 
00334 /*! \brief Whether we answer on a Polarity Switch event */
00335 static int answeronpolarityswitch = 0;
00336 
00337 /*! \brief Whether we hang up on a Polarity Switch event */
00338 static int hanguponpolarityswitch = 0;
00339 
00340 /*! \brief How long (ms) to ignore Polarity Switch events after we answer a call */
00341 static int polarityonanswerdelay = 600;
00342 
00343 /*! \brief When to send the CallerID signals (rings) */
00344 static int sendcalleridafter = DEFAULT_CIDRINGS;
00345 
00346 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
00347    when it's doing something critical. */
00348 AST_MUTEX_DEFINE_STATIC(monlock);
00349 
00350 /*! \brief This is the thread for the monitor which checks for input on the channels
00351    which are not currently in use.  */
00352 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00353 
00354 static int restart_monitor(void);
00355 
00356 static enum ast_bridge_result zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
00357 
00358 static int zt_sendtext(struct ast_channel *c, const char *text);
00359 
00360 /*! \brief Avoid the silly zt_getevent which ignores a bunch of events */
00361 static inline int zt_get_event(int fd)
00362 {
00363    int j;
00364    if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
00365    return j;
00366 }
00367 
00368 /*! \brief Avoid the silly zt_waitevent which ignores a bunch of events */
00369 static inline int zt_wait_event(int fd)
00370 {
00371    int i,j=0;
00372    i = ZT_IOMUX_SIGEVENT;
00373    if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
00374    if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
00375    return j;
00376 }
00377 
00378 /*! Chunk size to read -- we use 20ms chunks to make things happy.  */   
00379 #define READ_SIZE 160
00380 
00381 #define MASK_AVAIL      (1 << 0) /*!< Channel available for PRI use */
00382 #define MASK_INUSE      (1 << 1) /*!< Channel currently in use */
00383 
00384 #define CALLWAITING_SILENT_SAMPLES  ( (300 * 8) / READ_SIZE) /*!< 300 ms */
00385 #define CALLWAITING_REPEAT_SAMPLES  ( (10000 * 8) / READ_SIZE) /*!< 300 ms */
00386 #define CIDCW_EXPIRE_SAMPLES     ( (500 * 8) / READ_SIZE) /*!< 500 ms */
00387 #define MIN_MS_SINCE_FLASH       ( (2000) )  /*!< 2000 ms */
00388 #define DEFAULT_RINGT            ( (8000 * 8) / READ_SIZE)
00389 
00390 struct zt_pvt;
00391 
00392 
00393 #ifdef ZAPATA_R2
00394 static int r2prot = -1;
00395 #endif
00396 
00397 static int ringt_base = DEFAULT_RINGT;
00398 
00399 #ifdef ZAPATA_PRI
00400 
00401 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
00402 #define PRI_CHANNEL(p) ((p) & 0xff)
00403 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
00404 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
00405 
00406 struct zt_pri {
00407    pthread_t master;                /*!< Thread of master */
00408    ast_mutex_t lock;                /*!< Mutex */
00409    char idleext[AST_MAX_EXTENSION];          /*!< Where to idle extra calls */
00410    char idlecontext[AST_MAX_CONTEXT];           /*!< What context to use for idle */
00411    char idledial[AST_MAX_EXTENSION];            /*!< What to dial before dumping */
00412    int minunused;                   /*!< Min # of channels to keep empty */
00413    int minidle;                     /*!< Min # of "idling" calls to keep active */
00414    int nodetype;                    /*!< Node type */
00415    int switchtype;                     /*!< Type of switch to emulate */
00416    int nsf;                   /*!< Network-Specific Facilities */
00417    int dialplan;                    /*!< Dialing plan */
00418    int localdialplan;                  /*!< Local dialing plan */
00419    char internationalprefix[10];             /*!< country access code ('00' for european dialplans) */
00420    char nationalprefix[10];               /*!< area access code ('0' for european dialplans) */
00421    char localprefix[20];                  /*!< area access code + area code ('0'+area code for european dialplans) */
00422    char privateprefix[20];                /*!< for private dialplans */
00423    char unknownprefix[20];                /*!< for unknown dialplans */
00424    int dchannels[NUM_DCHANS];             /*!< What channel are the dchannels on */
00425    int trunkgroup;                     /*!< What our trunkgroup is */
00426    int mastertrunkgroup;                  /*!< What trunk group is our master */
00427    int prilogicalspan;                 /*!< Logical span number within trunk group */
00428    int numchans;                    /*!< Num of channels we represent */
00429    int overlapdial;                 /*!< In overlap dialing mode */
00430    int facilityenable;                 /*!< Enable facility IEs */
00431    struct pri *dchans[NUM_DCHANS];              /*!< Actual d-channels */
00432    int dchanavail[NUM_DCHANS];               /*!< Whether each channel is available */
00433    struct pri *pri;                 /*!< Currently active D-channel */
00434    int debug;
00435    int fds[NUM_DCHANS];                /*!< FD's for d-channels */
00436    int offset;
00437    int span;
00438    int resetting;
00439    int resetpos;
00440    time_t lastreset;                /*!< time when unused channels were last reset */
00441    long resetinterval;                 /*!< Interval (in seconds) for resetting unused channels */
00442    struct zt_pvt *pvts[MAX_CHANNELS];           /*!< Member channel pvt structs */
00443    struct zt_pvt *crvs;                /*!< Member CRV structs */
00444    struct zt_pvt *crvend;                 /*!< Pointer to end of CRV structs */
00445 };
00446 
00447 
00448 static struct zt_pri pris[NUM_SPANS];
00449 
00450 static int pritype = PRI_CPE;
00451 
00452 #if 0
00453 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
00454 #else
00455 #define DEFAULT_PRI_DEBUG 0
00456 #endif
00457 
00458 static inline void pri_rel(struct zt_pri *pri)
00459 {
00460    ast_mutex_unlock(&pri->lock);
00461 }
00462 
00463 static int switchtype = PRI_SWITCH_NI2;
00464 static int nsf = PRI_NSF_NONE;
00465 static int dialplan = PRI_NATIONAL_ISDN + 1;
00466 static int localdialplan = PRI_NATIONAL_ISDN + 1;
00467 
00468 #else
00469 /*! Shut up the compiler */
00470 struct zt_pri;
00471 #endif
00472 
00473 #define SUB_REAL  0        /*!< Active call */
00474 #define SUB_CALLWAIT 1        /*!< Call-Waiting call on hold */
00475 #define SUB_THREEWAY 2        /*!< Three-way call */
00476 
00477 /* Polarity states */
00478 #define POLARITY_IDLE   0
00479 #define POLARITY_REV    1
00480 
00481 
00482 static struct zt_distRings drings;
00483 
00484 struct distRingData {
00485    int ring[3];
00486 };
00487 struct ringContextData {
00488    char contextData[AST_MAX_CONTEXT];
00489 };
00490 struct zt_distRings {
00491    struct distRingData ringnum[3];
00492    struct ringContextData ringContext[3];
00493 };
00494 
00495 static char *subnames[] = {
00496    "Real",
00497    "Callwait",
00498    "Threeway"
00499 };
00500 
00501 struct zt_subchannel {
00502    int zfd;
00503    struct ast_channel *owner;
00504    int chan;
00505    short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
00506    struct ast_frame f;     /*!< One frame for each channel.  How did this ever work before? */
00507    unsigned int needringing:1;
00508    unsigned int needbusy:1;
00509    unsigned int needcongestion:1;
00510    unsigned int needcallerid:1;
00511    unsigned int needanswer:1;
00512    unsigned int needflash:1;
00513    unsigned int linear:1;
00514    unsigned int inthreeway:1;
00515    ZT_CONFINFO curconf;
00516 };
00517 
00518 #define CONF_USER_REAL     (1 << 0)
00519 #define CONF_USER_THIRDCALL   (1 << 1)
00520 
00521 #define MAX_SLAVES   4
00522 
00523 static struct zt_pvt {
00524    ast_mutex_t lock;
00525    struct ast_channel *owner;       /*!< Our current active owner (if applicable) */
00526                      /*!< Up to three channels can be associated with this call */
00527       
00528    struct zt_subchannel sub_unused;    /*!< Just a safety precaution */
00529    struct zt_subchannel subs[3];       /*!< Sub-channels */
00530    struct zt_confinfo saveconf;        /*!< Saved conference info */
00531 
00532    struct zt_pvt *slaves[MAX_SLAVES];     /*!< Slave to us (follows our conferencing) */
00533    struct zt_pvt *master;           /*!< Master to us (we follow their conferencing) */
00534    int inconference;          /*!< If our real should be in the conference */
00535    
00536    int sig;             /*!< Signalling style */
00537    int radio;              /*!< radio type */
00538    float rxgain;
00539    float txgain;
00540    int tonezone;              /*!< tone zone for this chan, or -1 for default */
00541    struct zt_pvt *next;          /*!< Next channel in list */
00542    struct zt_pvt *prev;          /*!< Prev channel in list */
00543 
00544    /* flags */
00545    unsigned int adsi:1;
00546    unsigned int answeronpolarityswitch:1;
00547    unsigned int busydetect:1;
00548    unsigned int callreturn:1;
00549    unsigned int callwaiting:1;
00550    unsigned int callwaitingcallerid:1;
00551    unsigned int cancallforward:1;
00552    unsigned int canpark:1;
00553    unsigned int confirmanswer:1;       /*!< Wait for '#' to confirm answer */
00554    unsigned int destroy:1;
00555    unsigned int didtdd:1;           /*!< flag to say its done it once */
00556    unsigned int dialednone:1;
00557    unsigned int dialing:1;
00558    unsigned int digital:1;
00559    unsigned int dnd:1;
00560    unsigned int echobreak:1;
00561    unsigned int echocanbridged:1;
00562    unsigned int echocanon:1;
00563    unsigned int faxhandled:1;       /*!< Has a fax tone already been handled? */
00564    unsigned int firstradio:1;
00565    unsigned int hanguponpolarityswitch:1;
00566    unsigned int hardwaredtmf:1;
00567    unsigned int hidecallerid;
00568    unsigned int ignoredtmf:1;
00569    unsigned int immediate:1;        /*!< Answer before getting digits? */
00570    unsigned int inalarm:1;
00571    unsigned int mate:1;          /*!< flag to say its in MATE mode */
00572    unsigned int outgoing:1;
00573    unsigned int overlapdial:1;
00574    unsigned int permcallwaiting:1;
00575    unsigned int permhidecallerid:1;    /*!< Whether to hide our outgoing caller ID or not */
00576    unsigned int priindication_oob:1;
00577    unsigned int priexclusive:1;
00578    unsigned int pulse:1;
00579    unsigned int pulsedial:1;        /*!< whether a pulse dial phone is detected */
00580    unsigned int restrictcid:1;         /*!< Whether restrict the callerid -> only send ANI */
00581    unsigned int threewaycalling:1;
00582    unsigned int transfer:1;
00583    unsigned int use_callerid:1;        /*!< Whether or not to use caller id on this channel */
00584    unsigned int use_callingpres:1;        /*!< Whether to use the callingpres the calling switch sends */
00585    unsigned int usedistinctiveringdetection:1;
00586    unsigned int zaptrcallerid:1;       /*!< should we use the callerid from incoming call on zap transfer or not */
00587    unsigned int transfertobusy:1;         /*!< allow flash-transfers to busy channels */
00588 #if defined(ZAPATA_PRI)
00589    unsigned int alerting:1;
00590    unsigned int alreadyhungup:1;
00591    unsigned int isidlecall:1;
00592    unsigned int proceeding:1;
00593    unsigned int progress:1;
00594    unsigned int resetting:1;
00595    unsigned int setup_ack:1;
00596 #endif
00597 #if defined(ZAPATA_R2)
00598    unsigned int hasr2call:1;
00599    unsigned int r2blocked:1;
00600    unsigned int sigchecked:1;
00601 #endif
00602 
00603    struct zt_distRings drings;
00604 
00605    char context[AST_MAX_CONTEXT];
00606    char defcontext[AST_MAX_CONTEXT];
00607    char exten[AST_MAX_EXTENSION];
00608    char language[MAX_LANGUAGE];
00609    char musicclass[MAX_MUSICCLASS];
00610 #ifdef PRI_ANI
00611    char cid_ani[AST_MAX_EXTENSION];
00612 #endif
00613    char cid_num[AST_MAX_EXTENSION];
00614    int cid_ton;               /*!< Type Of Number (TON) */
00615    char cid_name[AST_MAX_EXTENSION];
00616    char lastcid_num[AST_MAX_EXTENSION];
00617    char lastcid_name[AST_MAX_EXTENSION];
00618    char *origcid_num;            /*!< malloced original callerid */
00619    char *origcid_name;           /*!< malloced original callerid */
00620    char callwait_num[AST_MAX_EXTENSION];
00621    char callwait_name[AST_MAX_EXTENSION];
00622    char rdnis[AST_MAX_EXTENSION];
00623    char dnid[AST_MAX_EXTENSION];
00624    unsigned int group;
00625    int law;
00626    int confno;             /*!< Our conference */
00627    int confusers;             /*!< Who is using our conference */
00628    int propconfno;               /*!< Propagated conference number */
00629    ast_group_t callgroup;
00630    ast_group_t pickupgroup;
00631    int channel;               /*!< Channel Number or CRV */
00632    int span;               /*!< Span number */
00633    time_t guardtime;          /*!< Must wait this much time before using for new call */
00634    int cid_signalling;           /*!< CID signalling type bell202 or v23 */
00635    int cid_start;             /*!< CID start indicator, polarity or ring */
00636    int callingpres;           /*!< The value of callling presentation that we're going to use when placing a PRI call */
00637    int callwaitingrepeat;           /*!< How many samples to wait before repeating call waiting */
00638    int cidcwexpire;           /*!< When to expire our muting for CID/CW */
00639    unsigned char *cidspill;
00640    int cidpos;
00641    int cidlen;
00642    int ringt;
00643    int ringt_base;
00644    int stripmsd;
00645    int callwaitcas;
00646    int callwaitrings;
00647    int echocancel;
00648    int echotraining;
00649    char echorest[20];
00650    int busycount;
00651    int busy_tonelength;
00652    int busy_quietlength;
00653    int callprogress;
00654    struct timeval flashtime;        /*!< Last flash-hook time */
00655    struct ast_dsp *dsp;
00656    int cref;               /*!< Call reference number */
00657    ZT_DIAL_OPERATION dop;
00658    int whichwink;             /*!< SIG_FEATDMF_TA Which wink are we on? */
00659    char finaldial[64];
00660    char accountcode[AST_MAX_ACCOUNT_CODE];      /*!< Account code */
00661    int amaflags;              /*!< AMA Flags */
00662    struct tdd_state *tdd;           /*!< TDD flag */
00663    char call_forward[AST_MAX_EXTENSION];
00664    char mailbox[AST_MAX_EXTENSION];
00665    char dialdest[256];
00666    int onhooktime;
00667    int msgstate;
00668    int distinctivering;          /*!< Which distinctivering to use */
00669    int cidrings;              /*!< Which ring to deliver CID on */
00670    int dtmfrelax;             /*!< whether to run in relaxed DTMF mode */
00671    int fake_event;
00672    int polarityonanswerdelay;
00673    struct timeval polaritydelaytv;
00674    int sendcalleridafter;
00675 #ifdef ZAPATA_PRI
00676    struct zt_pri *pri;
00677    struct zt_pvt *bearer;
00678    struct zt_pvt *realcall;
00679    q931_call *call;
00680    int prioffset;
00681    int logicalspan;
00682 #endif   
00683 #ifdef ZAPATA_R2
00684    int r2prot;
00685    mfcr2_t *r2;
00686 #endif   
00687    int polarity;
00688    int dsp_features;
00689 
00690 } *iflist = NULL, *ifend = NULL;
00691 
00692 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
00693 static int zt_digit(struct ast_channel *ast, char digit);
00694 static int zt_sendtext(struct ast_channel *c, const char *text);
00695 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
00696 static int zt_hangup(struct ast_channel *ast);
00697 static int zt_answer(struct ast_channel *ast);
00698 struct ast_frame *zt_read(struct ast_channel *ast);
00699 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
00700 struct ast_frame *zt_exception(struct ast_channel *ast);
00701 static int zt_indicate(struct ast_channel *chan, int condition);
00702 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00703 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
00704 
00705 static const struct ast_channel_tech zap_tech = {
00706    .type = type,
00707    .description = tdesc,
00708    .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
00709    .requester = zt_request,
00710    .send_digit = zt_digit,
00711    .send_text = zt_sendtext,
00712    .call = zt_call,
00713    .hangup = zt_hangup,
00714    .answer = zt_answer,
00715    .read = zt_read,
00716    .write = zt_write,
00717    .bridge = zt_bridge,
00718    .exception = zt_exception,
00719    .indicate = zt_indicate,
00720    .fixup = zt_fixup,
00721    .setoption = zt_setoption,
00722 };
00723 
00724 #ifdef ZAPATA_PRI
00725 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
00726 #else
00727 #define GET_CHANNEL(p) ((p)->channel)
00728 #endif
00729 
00730 struct zt_pvt *round_robin[32];
00731 
00732 #ifdef ZAPATA_PRI
00733 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
00734 {
00735    int res;
00736    /* Grab the lock first */
00737    do {
00738        res = ast_mutex_trylock(&pri->lock);
00739       if (res) {
00740          ast_mutex_unlock(&pvt->lock);
00741          /* Release the lock and try again */
00742          usleep(1);
00743          ast_mutex_lock(&pvt->lock);
00744       }
00745    } while(res);
00746    /* Then break the poll */
00747    pthread_kill(pri->master, SIGURG);
00748    return 0;
00749 }
00750 #endif
00751 
00752 #define NUM_CADENCE_MAX 25
00753 static int num_cadence = 4;
00754 static int user_has_defined_cadences = 0;
00755 
00756 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
00757    { { 125, 125, 2000, 4000 } },       /*!< Quick chirp followed by normal ring */
00758    { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
00759    { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
00760    { { 1000, 500, 2500, 5000 } },   /*!< Long ring */
00761 };
00762 
00763 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause
00764  * is 1, the second pause is 2 and so on.
00765  */
00766 
00767 static int cidrings[NUM_CADENCE_MAX] = {
00768    2,                            /*!< Right after first long ring */
00769    4,                            /*!< Right after long part */
00770    3,                            /*!< After third chirp */
00771    2,                            /*!< Second spell */
00772 };
00773 
00774 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
00775          (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
00776 
00777 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
00778 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) /* || (p->sig & __ZT_SIG_FXO) */)
00779 
00780 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
00781 {
00782    int res;
00783    if (p->subs[0].owner == ast)
00784       res = 0;
00785    else if (p->subs[1].owner == ast)
00786       res = 1;
00787    else if (p->subs[2].owner == ast)
00788       res = 2;
00789    else {
00790       res = -1;
00791       if (!nullok)
00792          ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
00793    }
00794    return res;
00795 }
00796 
00797 #ifdef ZAPATA_PRI
00798 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
00799 #else
00800 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
00801 #endif
00802 {
00803    struct ast_frame null = { AST_FRAME_NULL, };
00804 #ifdef ZAPATA_PRI
00805    if (pri)
00806       ast_mutex_unlock(&pri->lock);
00807 #endif         
00808    for (;;) {
00809       if (p->subs[a].owner) {
00810          if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
00811             ast_mutex_unlock(&p->lock);
00812             usleep(1);
00813             ast_mutex_lock(&p->lock);
00814          } else {
00815             ast_queue_frame(p->subs[a].owner, &null);
00816             ast_mutex_unlock(&p->subs[a].owner->lock);
00817             break;
00818          }
00819       } else
00820          break;
00821    }
00822 #ifdef ZAPATA_PRI
00823    if (pri)
00824       ast_mutex_lock(&pri->lock);
00825 #endif         
00826 }
00827 
00828 #ifdef ZAPATA_PRI
00829 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
00830 #else
00831 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
00832 #endif
00833 {
00834    /* We must unlock the PRI to avoid the possibility of a deadlock */
00835 #ifdef ZAPATA_PRI
00836    if (pri)
00837       ast_mutex_unlock(&pri->lock);
00838 #endif      
00839    for (;;) {
00840       if (p->owner) {
00841          if (ast_mutex_trylock(&p->owner->lock)) {
00842             ast_mutex_unlock(&p->lock);
00843             usleep(1);
00844             ast_mutex_lock(&p->lock);
00845          } else {
00846             ast_queue_frame(p->owner, f);
00847             ast_mutex_unlock(&p->owner->lock);
00848             break;
00849          }
00850       } else
00851          break;
00852    }
00853 #ifdef ZAPATA_PRI
00854    if (pri)
00855       ast_mutex_lock(&pri->lock);
00856 #endif      
00857 }
00858 
00859 static int restore_gains(struct zt_pvt *p);
00860 
00861 static void swap_subs(struct zt_pvt *p, int a, int b)
00862 {
00863    int tchan;
00864    int tinthreeway;
00865    struct ast_channel *towner;
00866 
00867    ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
00868 
00869    tchan = p->subs[a].chan;
00870    towner = p->subs[a].owner;
00871    tinthreeway = p->subs[a].inthreeway;
00872 
00873    p->subs[a].chan = p->subs[b].chan;
00874    p->subs[a].owner = p->subs[b].owner;
00875    p->subs[a].inthreeway = p->subs[b].inthreeway;
00876 
00877    p->subs[b].chan = tchan;
00878    p->subs[b].owner = towner;
00879    p->subs[b].inthreeway = tinthreeway;
00880 
00881    if (p->subs[a].owner) 
00882       p->subs[a].owner->fds[0] = p->subs[a].zfd;
00883    if (p->subs[b].owner) 
00884       p->subs[b].owner->fds[0] = p->subs[b].zfd;
00885    wakeup_sub(p, a, NULL);
00886    wakeup_sub(p, b, NULL);
00887 }
00888 
00889 static int zt_open(char *fn)
00890 {
00891    int fd;
00892    int isnum;
00893    int chan = 0;
00894    int bs;
00895    int x;
00896    isnum = 1;
00897    for (x=0;x<strlen(fn);x++) {
00898       if (!isdigit(fn[x])) {
00899          isnum = 0;
00900          break;
00901       }
00902    }
00903    if (isnum) {
00904       chan = atoi(fn);
00905       if (chan < 1) {
00906          ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
00907          return -1;
00908       }
00909       fn = "/dev/zap/channel";
00910    }
00911    fd = open(fn, O_RDWR | O_NONBLOCK);
00912    if (fd < 0) {
00913       ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
00914       return -1;
00915    }
00916    if (chan) {
00917       if (ioctl(fd, ZT_SPECIFY, &chan)) {
00918          x = errno;
00919          close(fd);
00920          errno = x;
00921          ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
00922          return -1;
00923       }
00924    }
00925    bs = READ_SIZE;
00926    if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
00927    return fd;
00928 }
00929 
00930 static void zt_close(int fd)
00931 {
00932    if(fd > 0)
00933       close(fd);
00934 }
00935 
00936 int zt_setlinear(int zfd, int linear)
00937 {
00938    int res;
00939    res = ioctl(zfd, ZT_SETLINEAR, &linear);
00940    if (res)
00941       return res;
00942    return 0;
00943 }
00944 
00945 
00946 int zt_setlaw(int zfd, int law)
00947 {
00948    int res;
00949    res = ioctl(zfd, ZT_SETLAW, &law);
00950    if (res)
00951       return res;
00952    return 0;
00953 }
00954 
00955 static int zt_get_history(int fd, void *buf, int buf_size)
00956 {
00957    struct zt_history hist;
00958    hist.buf=buf;
00959    hist.len=buf_size;
00960    return ioctl(fd, ZT_GET_HISTORY, &hist);
00961 }
00962 
00963 static int alloc_sub(struct zt_pvt *p, int x)
00964 {
00965    ZT_BUFFERINFO bi;
00966    int res;
00967    if (p->subs[x].zfd < 0) {
00968       p->subs[x].zfd = zt_open("/dev/zap/pseudo");
00969       if (p->subs[x].zfd > -1) {
00970          res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
00971          if (!res) {
00972             bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
00973             bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
00974             bi.numbufs = numbufs;
00975             res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
00976             if (res < 0) {
00977                ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
00978             }
00979          } else 
00980             ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
00981          if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
00982             ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
00983             zt_close(p->subs[x].zfd);
00984             p->subs[x].zfd = -1;
00985             return -1;
00986          }
00987          if (option_debug)
00988             ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
00989          return 0;
00990       } else
00991          ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
00992       return -1;
00993    }
00994    ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
00995    return -1;
00996 }
00997 
00998 static int unalloc_sub(struct zt_pvt *p, int x)
00999 {
01000    if (!x) {
01001       ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
01002       return -1;
01003    }
01004    ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
01005    if (p->subs[x].zfd > -1) {
01006       zt_close(p->subs[x].zfd);
01007    }
01008    p->subs[x].zfd = -1;
01009    p->subs[x].linear = 0;
01010    p->subs[x].chan = 0;
01011    p->subs[x].owner = NULL;
01012    p->subs[x].inthreeway = 0;
01013    p->polarity = POLARITY_IDLE;
01014    memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
01015    return 0;
01016 }
01017 
01018 static int zt_digit(struct ast_channel *ast, char digit)
01019 {
01020    ZT_DIAL_OPERATION zo;
01021    struct zt_pvt *p;
01022    int res = 0;
01023    int index;
01024    p = ast->tech_pvt;
01025    ast_mutex_lock(&p->lock);
01026    index = zt_get_index(ast, p, 0);
01027    if ((index == SUB_REAL) && p->owner) {
01028 #ifdef ZAPATA_PRI
01029       if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
01030          if (p->setup_ack) {
01031             if (!pri_grab(p, p->pri)) {
01032                pri_information(p->pri->pri,p->call,digit);
01033                pri_rel(p->pri);
01034             } else
01035                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
01036          } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
01037             ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
01038             res = strlen(p->dialdest);
01039             p->dialdest[res++] = digit;
01040             p->dialdest[res] = '\0';
01041          }
01042       } else {
01043 #else
01044       {
01045 #endif
01046          zo.op = ZT_DIAL_OP_APPEND;
01047          zo.dialstr[0] = 'T';
01048          zo.dialstr[1] = digit;
01049          zo.dialstr[2] = 0;
01050          if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
01051             ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
01052          else
01053             p->dialing = 1;
01054       }
01055    }
01056    ast_mutex_unlock(&p->lock);
01057    return res;
01058 }
01059 
01060 static char *events[] = {
01061       "No event",
01062       "On hook",
01063       "Ring/Answered",
01064       "Wink/Flash",
01065       "Alarm",
01066       "No more alarm",
01067       "HDLC Abort",
01068       "HDLC Overrun",
01069       "HDLC Bad FCS",
01070       "Dial Complete",
01071       "Ringer On",
01072       "Ringer Off",
01073       "Hook Transition Complete",
01074       "Bits Changed",
01075       "Pulse Start",
01076       "Timer Expired",
01077       "Timer Ping",
01078       "Polarity Reversal",
01079       "Ring Begin",
01080 };
01081 
01082 static struct {
01083    int alarm;
01084    char *name;
01085 } alarms[] = {
01086    { ZT_ALARM_RED, "Red Alarm" },
01087    { ZT_ALARM_YELLOW, "Yellow Alarm" },
01088    { ZT_ALARM_BLUE, "Blue Alarm" },
01089    { ZT_ALARM_RECOVER, "Recovering" },
01090    { ZT_ALARM_LOOPBACK, "Loopback" },
01091    { ZT_ALARM_NOTOPEN, "Not Open" },
01092    { ZT_ALARM_NONE, "None" },
01093 };
01094 
01095 static char *alarm2str(int alarm)
01096 {
01097    int x;
01098    for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
01099       if (alarms[x].alarm & alarm)
01100          return alarms[x].name;
01101    }
01102    return alarm ? "Unknown Alarm" : "No Alarm";
01103 }
01104 
01105 static char *event2str(int event)
01106 {
01107         static char buf[256];
01108         if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
01109                 return events[event];
01110         sprintf(buf, "Event %d", event); /* safe */
01111         return buf;
01112 }
01113 
01114 #ifdef ZAPATA_PRI
01115 static char *dialplan2str(int dialplan)
01116 {
01117    if (dialplan == -1) {
01118       return("Dynamically set dialplan in ISDN");
01119    }
01120    return(pri_plan2str(dialplan));
01121 }
01122 #endif
01123 
01124 #ifdef ZAPATA_R2
01125 static int str2r2prot(char *swtype)
01126 {
01127     if (!strcasecmp(swtype, "ar"))
01128         return MFCR2_PROT_ARGENTINA;
01129     /*endif*/
01130     if (!strcasecmp(swtype, "cn"))
01131         return MFCR2_PROT_CHINA;
01132     /*endif*/
01133     if (!strcasecmp(swtype, "kr"))
01134         return MFCR2_PROT_KOREA;
01135     /*endif*/
01136     return -1;
01137 }
01138 #endif
01139 
01140 static char *zap_sig2str(int sig)
01141 {
01142    static char buf[256];
01143    switch(sig) {
01144    case SIG_EM:
01145       return "E & M Immediate";
01146    case SIG_EMWINK:
01147       return "E & M Wink";
01148    case SIG_EM_E1:
01149       return "E & M E1";
01150    case SIG_FEATD:
01151       return "Feature Group D (DTMF)";
01152    case SIG_FEATDMF:
01153       return "Feature Group D (MF)";
01154    case SIG_FEATDMF_TA:
01155       return "Feature Groud D (MF) Tandem Access";
01156    case SIG_FEATB:
01157       return "Feature Group B (MF)";
01158    case SIG_E911:
01159       return "E911 (MF)";
01160    case SIG_FXSLS:
01161       return "FXS Loopstart";
01162    case SIG_FXSGS:
01163       return "FXS Groundstart";
01164    case SIG_FXSKS:
01165       return "FXS Kewlstart";
01166    case SIG_FXOLS:
01167       return "FXO Loopstart";
01168    case SIG_FXOGS:
01169       return "FXO Groundstart";
01170    case SIG_FXOKS:
01171       return "FXO Kewlstart";
01172    case SIG_PRI:
01173       return "PRI Signalling";
01174    case SIG_R2:
01175       return "R2 Signalling";
01176    case SIG_SF:
01177       return "SF (Tone) Signalling Immediate";
01178    case SIG_SFWINK:
01179       return "SF (Tone) Signalling Wink";
01180    case SIG_SF_FEATD:
01181       return "SF (Tone) Signalling with Feature Group D (DTMF)";
01182    case SIG_SF_FEATDMF:
01183       return "SF (Tone) Signalling with Feature Group D (MF)";
01184    case SIG_SF_FEATB:
01185       return "SF (Tone) Signalling with Feature Group B (MF)";
01186    case SIG_GR303FXOKS:
01187       return "GR-303 Signalling with FXOKS";
01188    case SIG_GR303FXSKS:
01189       return "GR-303 Signalling with FXSKS";
01190    case 0:
01191       return "Pseudo Signalling";
01192    default:
01193       snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
01194       return buf;
01195    }
01196 }
01197 
01198 #define sig2str zap_sig2str
01199 
01200 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
01201 {
01202    /* If the conference already exists, and we're already in it
01203       don't bother doing anything */
01204    ZT_CONFINFO zi;
01205    
01206    memset(&zi, 0, sizeof(zi));
01207    zi.chan = 0;
01208 
01209    if (slavechannel > 0) {
01210       /* If we have only one slave, do a digital mon */
01211       zi.confmode = ZT_CONF_DIGITALMON;
01212       zi.confno = slavechannel;
01213    } else {
01214       if (!index) {
01215          /* Real-side and pseudo-side both participate in conference */
01216          zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
01217                         ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
01218       } else
01219          zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
01220       zi.confno = p->confno;
01221    }
01222    if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
01223       return 0;
01224    if (c->zfd < 0)
01225       return 0;
01226    if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01227       ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
01228       return -1;
01229    }
01230    if (slavechannel < 1) {
01231       p->confno = zi.confno;
01232    }
01233    memcpy(&c->curconf, &zi, sizeof(c->curconf));
01234    ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01235    return 0;
01236 }
01237 
01238 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
01239 {
01240    /* If they're listening to our channel, they're ours */  
01241    if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
01242       return 1;
01243    /* If they're a talker on our (allocated) conference, they're ours */
01244    if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
01245       return 1;
01246    return 0;
01247 }
01248 
01249 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
01250 {
01251    ZT_CONFINFO zi;
01252    if (/* Can't delete if there's no zfd */
01253       (c->zfd < 0) ||
01254       /* Don't delete from the conference if it's not our conference */
01255       !isourconf(p, c)
01256       /* Don't delete if we don't think it's conferenced at all (implied) */
01257       ) return 0;
01258    memset(&zi, 0, sizeof(zi));
01259    zi.chan = 0;
01260    zi.confno = 0;
01261    zi.confmode = 0;
01262    if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01263       ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01264       return -1;
01265    }
01266    ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01267    memcpy(&c->curconf, &zi, sizeof(c->curconf));
01268    return 0;
01269 }
01270 
01271 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
01272 {
01273    int x;
01274    int useslavenative;
01275    struct zt_pvt *slave = NULL;
01276    /* Start out optimistic */
01277    useslavenative = 1;
01278    /* Update conference state in a stateless fashion */
01279    for (x=0;x<3;x++) {
01280       /* Any three-way calling makes slave native mode *definitely* out
01281          of the question */
01282       if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
01283          useslavenative = 0;
01284    }
01285    /* If we don't have any 3-way calls, check to see if we have
01286       precisely one slave */
01287    if (useslavenative) {
01288       for (x=0;x<MAX_SLAVES;x++) {
01289          if (p->slaves[x]) {
01290             if (slave) {
01291                /* Whoops already have a slave!  No 
01292                   slave native and stop right away */
01293                slave = NULL;
01294                useslavenative = 0;
01295                break;
01296             } else {
01297                /* We have one slave so far */
01298                slave = p->slaves[x];
01299             }
01300          }
01301       }
01302    }
01303    /* If no slave, slave native definitely out */
01304    if (!slave)
01305       useslavenative = 0;
01306    else if (slave->law != p->law) {
01307       useslavenative = 0;
01308       slave = NULL;
01309    }
01310    if (out)
01311       *out = slave;
01312    return useslavenative;
01313 }
01314 
01315 static int reset_conf(struct zt_pvt *p)
01316 {
01317    ZT_CONFINFO zi;
01318    memset(&zi, 0, sizeof(zi));
01319    p->confno = -1;
01320    memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
01321    if (p->subs[SUB_REAL].zfd > -1) {
01322       if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
01323          ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
01324    }
01325    return 0;
01326 }
01327 
01328 static int update_conf(struct zt_pvt *p)
01329 {
01330    int needconf = 0;
01331    int x;
01332    int useslavenative;
01333    struct zt_pvt *slave = NULL;
01334 
01335    useslavenative = isslavenative(p, &slave);
01336    /* Start with the obvious, general stuff */
01337    for (x=0;x<3;x++) {
01338       /* Look for three way calls */
01339       if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
01340          conf_add(p, &p->subs[x], x, 0);
01341          needconf++;
01342       } else {
01343          conf_del(p, &p->subs[x], x);
01344       }
01345    }
01346    /* If we have a slave, add him to our conference now. or DAX
01347       if this is slave native */
01348    for (x=0;x<MAX_SLAVES;x++) {
01349       if (p->slaves[x]) {
01350          if (useslavenative)
01351             conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
01352          else {
01353             conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
01354             needconf++;
01355          }
01356       }
01357    }
01358    /* If we're supposed to be in there, do so now */
01359    if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
01360       if (useslavenative)
01361          conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
01362       else {
01363          conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
01364          needconf++;
01365       }
01366    }
01367    /* If we have a master, add ourselves to his conference */
01368    if (p->master) {
01369       if (isslavenative(p->master, NULL)) {
01370          conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
01371       } else {
01372          conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
01373       }
01374    }
01375    if (!needconf) {
01376       /* Nobody is left (or should be left) in our conference.  
01377          Kill it.  */
01378       p->confno = -1;
01379    }
01380    ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
01381    return 0;
01382 }
01383 
01384 static void zt_enable_ec(struct zt_pvt *p)
01385 {
01386    int x;
01387    int res;
01388    if (!p)
01389       return;
01390    if (p->echocanon) {
01391       ast_log(LOG_DEBUG, "Echo cancellation already on\n");
01392       return;
01393    }
01394    if (p->digital) {
01395       ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
01396       return;
01397    }
01398    if (p->echocancel) {
01399       if (p->sig == SIG_PRI) {
01400          x = 1;
01401          res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
01402          if (res)
01403             ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01404       }
01405       x = p->echocancel;
01406       res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01407       if (res) 
01408          ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01409       else {
01410          p->echocanon = 1;
01411          ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
01412       }
01413    } else
01414       ast_log(LOG_DEBUG, "No echo cancellation requested\n");
01415 }
01416 
01417 static void zt_train_ec(struct zt_pvt *p)
01418 {
01419    int x;
01420    int res;
01421    if (p && p->echocancel && p->echotraining) {
01422       x = p->echotraining;
01423       res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
01424       if (res) 
01425          ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
01426       else {
01427          ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
01428       }
01429    } else
01430       ast_log(LOG_DEBUG, "No echo training requested\n");
01431 }
01432 
01433 static void zt_disable_ec(struct zt_pvt *p)
01434 {
01435    int x;
01436    int res;
01437    if (p->echocancel) {
01438       x = 0;
01439       res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01440       if (res) 
01441          ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
01442       else
01443          ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
01444    }
01445    p->echocanon = 0;
01446 }
01447 
01448 static void fill_txgain(struct zt_gains *g, float gain, int law)
01449 {
01450    int j;
01451    int k;
01452    float linear_gain = pow(10.0, gain / 20.0);
01453 
01454    switch (law) {
01455    case ZT_LAW_ALAW:
01456       for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01457          if (gain) {
01458             k = (int) (((float) AST_ALAW(j)) * linear_gain);
01459             if (k > 32767) k = 32767;
01460             if (k < -32767) k = -32767;
01461             g->txgain[j] = AST_LIN2A(k);
01462          } else {
01463             g->txgain[j] = j;
01464          }
01465       }
01466       break;
01467    case ZT_LAW_MULAW:
01468       for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01469          if (gain) {
01470             k = (int) (((float) AST_MULAW(j)) * linear_gain);
01471             if (k > 32767) k = 32767;
01472             if (k < -32767) k = -32767;
01473             g->txgain[j] = AST_LIN2MU(k);
01474          } else {
01475             g->txgain[j] = j;
01476          }
01477       }
01478       break;
01479    }
01480 }
01481 
01482 static void fill_rxgain(struct zt_gains *g, float gain, int law)
01483 {
01484    int j;
01485    int k;
01486    float linear_gain = pow(10.0, gain / 20.0);
01487 
01488    switch (law) {
01489    case ZT_LAW_ALAW:
01490       for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01491          if (gain) {
01492             k = (int) (((float) AST_ALAW(j)) * linear_gain);
01493             if (k > 32767) k = 32767;
01494             if (k < -32767) k = -32767;
01495             g->rxgain[j] = AST_LIN2A(k);
01496          } else {
01497             g->rxgain[j] = j;
01498          }
01499       }
01500       break;
01501    case ZT_LAW_MULAW:
01502       for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01503          if (gain) {
01504             k = (int) (((float) AST_MULAW(j)) * linear_gain);
01505             if (k > 32767) k = 32767;
01506             if (k < -32767) k = -32767;
01507             g->rxgain[j] = AST_LIN2MU(k);
01508          } else {
01509             g->rxgain[j] = j;
01510          }
01511       }
01512       break;
01513    }
01514 }
01515 
01516 int set_actual_txgain(int fd, int chan, float gain, int law)
01517 {
01518    struct zt_gains g;
01519    int res;
01520 
01521    memset(&g, 0, sizeof(g));
01522    g.chan = chan;
01523    res = ioctl(fd, ZT_GETGAINS, &g);
01524    if (res) {
01525       ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01526       return res;
01527    }
01528 
01529    fill_txgain(&g, gain, law);
01530 
01531    return ioctl(fd, ZT_SETGAINS, &g);
01532 }
01533 
01534 int set_actual_rxgain(int fd, int chan, float gain, int law)
01535 {
01536    struct zt_gains g;
01537    int res;
01538 
01539    memset(&g, 0, sizeof(g));
01540    g.chan = chan;
01541    res = ioctl(fd, ZT_GETGAINS, &g);
01542    if (res) {
01543       ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01544       return res;
01545    }
01546 
01547    fill_rxgain(&g, gain, law);
01548 
01549    return ioctl(fd, ZT_SETGAINS, &g);
01550 }
01551 
01552 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
01553 {
01554    return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
01555 }
01556 
01557 static int bump_gains(struct zt_pvt *p)
01558 {
01559    int res;
01560 
01561    /* Bump receive gain by 5.0db */
01562    res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
01563    if (res) {
01564       ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
01565       return -1;
01566    }
01567 
01568    return 0;
01569 }
01570 
01571 static int restore_gains(struct zt_pvt *p)
01572 {
01573    int res;
01574 
01575    res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01576    if (res) {
01577       ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
01578       return -1;
01579    }
01580 
01581    return 0;
01582 }
01583 
01584 static inline int zt_set_hook(int fd, int hs)
01585 {
01586    int x, res;
01587    x = hs;
01588    res = ioctl(fd, ZT_HOOK, &x);
01589    if (res < 0) 
01590    {
01591       if (errno == EINPROGRESS) return 0;
01592       ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
01593    }
01594    return res;
01595 }
01596 
01597 static inline int zt_confmute(struct zt_pvt *p, int muted)
01598 {
01599    int x, y, res;
01600    x = muted;
01601    if (p->sig == SIG_PRI) {
01602       y = 1;
01603       res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
01604       if (res)
01605          ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
01606    }
01607    res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
01608    if (res < 0) 
01609       ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
01610    return res;
01611 }
01612 
01613 static int save_conference(struct zt_pvt *p)
01614 {
01615    struct zt_confinfo c;
01616    int res;
01617    if (p->saveconf.confmode) {
01618       ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
01619       return -1;
01620    }
01621    p->saveconf.chan = 0;
01622    res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
01623    if (res) {
01624       ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
01625       p->saveconf.confmode = 0;
01626       return -1;
01627    }
01628    c.chan = 0;
01629    c.confno = 0;
01630    c.confmode = ZT_CONF_NORMAL;
01631    res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
01632    if (res) {
01633       ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
01634       return -1;
01635    }
01636    if (option_debug)
01637       ast_log(LOG_DEBUG, "Disabled conferencing\n");
01638    return 0;
01639 }
01640 
01641 static int restore_conference(struct zt_pvt *p)
01642 {
01643    int res;
01644    if (p->saveconf.confmode) {
01645       res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
01646       p->saveconf.confmode = 0;
01647       if (res) {
01648          ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
01649          return -1;
01650       }
01651    }
01652    if (option_debug)
01653       ast_log(LOG_DEBUG, "Restored conferencing\n");
01654    return 0;
01655 }
01656 
01657 static int send_callerid(struct zt_pvt *p);
01658 
01659 int send_cwcidspill(struct zt_pvt *p)
01660 {
01661    p->callwaitcas = 0;
01662    p->cidcwexpire = 0;
01663    p->cidspill = malloc(MAX_CALLERID_SIZE);
01664    if (p->cidspill) {
01665       memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
01666       p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
01667       /* Make sure we account for the end */
01668       p->cidlen += READ_SIZE * 4;
01669       p->cidpos = 0;
01670       send_callerid(p);
01671       if (option_verbose > 2)
01672          ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID.  Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
01673    } else return -1;
01674    return 0;
01675 }
01676 
01677 static int has_voicemail(struct zt_pvt *p)
01678 {
01679 
01680    return ast_app_has_voicemail(p->mailbox, NULL);
01681 }
01682 
01683 static int send_callerid(struct zt_pvt *p)
01684 {
01685    /* Assumes spill in p->cidspill, p->cidlen in length and we're p->cidpos into it */
01686    int res;
01687    /* Take out of linear mode if necessary */
01688    if (p->subs[SUB_REAL].linear) {
01689       p->subs[SUB_REAL].linear = 0;
01690       zt_setlinear(p->subs[SUB_REAL].zfd, 0);
01691    }
01692    while(p->cidpos < p->cidlen) {
01693       res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
01694       if (res < 0) {
01695          if (errno == EAGAIN)
01696             return 0;
01697          else {
01698             ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
01699             return -1;
01700          }
01701       }
01702       if (!res)
01703          return 0;
01704       p->cidpos += res;
01705    }
01706    free(p->cidspill);
01707    p->cidspill = NULL;
01708    if (p->callwaitcas) {
01709       /* Wait for CID/CW to expire */
01710       p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
01711    } else
01712       restore_conference(p);
01713    return 0;
01714 }
01715 
01716 static int zt_callwait(struct ast_channel *ast)
01717 {
01718    struct zt_pvt *p = ast->tech_pvt;
01719    p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
01720    if (p->cidspill) {
01721       ast_log(LOG_WARNING, "Spill already exists?!?\n");
01722       free(p->cidspill);
01723    }
01724    p->cidspill = malloc(2400 /* SAS */ + 680 /* CAS */ + READ_SIZE * 4);
01725    if (p->cidspill) {
01726       save_conference(p);
01727       /* Silence */
01728       memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
01729       if (!p->callwaitrings && p->callwaitingcallerid) {
01730          ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
01731          p->callwaitcas = 1;
01732          p->cidlen = 2400 + 680 + READ_SIZE * 4;
01733       } else {
01734          ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
01735          p->callwaitcas = 0;
01736          p->cidlen = 2400 + READ_SIZE * 4;
01737       }
01738       p->cidpos = 0;
01739       send_callerid(p);
01740    } else {
01741       ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
01742       return -1;
01743    }
01744    return 0;
01745 }
01746 
01747 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
01748 {
01749    struct zt_pvt *p = ast->tech_pvt;
01750    int x, res, index;
01751    char *c, *n, *l;
01752 #ifdef ZAPATA_PRI
01753    char *s=NULL;
01754 #endif
01755    char dest[256]; /* must be same length as p->dialdest */
01756    ast_mutex_lock(&p->lock);
01757    ast_copy_string(dest, rdest, sizeof(dest));
01758    ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
01759    if ((ast->_state == AST_STATE_BUSY)) {
01760       p->subs[SUB_REAL].needbusy = 1;
01761       ast_mutex_unlock(&p->lock);
01762       return 0;
01763    }
01764    if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
01765       ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
01766       ast_mutex_unlock(&p->lock);
01767       return -1;
01768    }
01769    p->dialednone = 0;
01770    if (p->radio)  /* if a radio channel, up immediately */
01771    {
01772       /* Special pseudo -- automatically up */
01773       ast_setstate(ast, AST_STATE_UP); 
01774       ast_mutex_unlock(&p->lock);
01775       return 0;
01776    }
01777    x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
01778    res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
01779    if (res)
01780       ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
01781    p->outgoing = 1;
01782 
01783    set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01784 
01785    switch(p->sig) {
01786    case SIG_FXOLS:
01787    case SIG_FXOGS:
01788    case SIG_FXOKS:
01789       if (p->owner == ast) {
01790          /* Normal ring, on hook */
01791          
01792          /* Don't send audio while on hook, until the call is answered */
01793          p->dialing = 1;
01794          if (p->use_callerid) {
01795             /* Generate the Caller-ID spill if desired */
01796             if (p->cidspill) {
01797                ast_log(LOG_WARNING, "cidspill already exists??\n");
01798                free(p->cidspill);
01799             }
01800             p->cidspill = malloc(MAX_CALLERID_SIZE);
01801             p->callwaitcas = 0;
01802             if (p->cidspill) {
01803                p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
01804                p->cidpos = 0;
01805                send_callerid(p);
01806             } else
01807                ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
01808          }
01809          /* Choose proper cadence */
01810          if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
01811             if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
01812                ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
01813             p->cidrings = cidrings[p->distinctivering - 1];
01814          } else {
01815             if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
01816                ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
01817             p->cidrings = p->sendcalleridafter;
01818          }
01819 
01820 
01821          /* nick@dccinc.com 4/3/03 mods to allow for deferred dialing */
01822          c = strchr(dest, '/');
01823          if (c)
01824             c++;
01825          if (c && (strlen(c) < p->stripmsd)) {
01826             ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01827             c = NULL;
01828          }
01829          if (c) {
01830             p->dop.op = ZT_DIAL_OP_REPLACE;
01831             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
01832             ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
01833          } else {
01834             p->dop.dialstr[0] = '\0';
01835          }
01836          x = ZT_RING;
01837          if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
01838             ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
01839             ast_mutex_unlock(&p->lock);
01840             return -1;
01841          }
01842          p->dialing = 1;
01843       } else {
01844          /* Call waiting call */
01845          p->callwaitrings = 0;
01846          if (ast->cid.cid_num)
01847             ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
01848          else
01849             p->callwait_num[0] = '\0';
01850          if (ast->cid.cid_name)
01851             ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
01852          else
01853             p->callwait_name[0] = '\0';
01854          /* Call waiting tone instead */
01855          if (zt_callwait(ast)) {
01856             ast_mutex_unlock(&p->lock);
01857             return -1;
01858          }
01859          /* Make ring-back */
01860          if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
01861             ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
01862             
01863       }
01864       n = ast->cid.cid_name;
01865       l = ast->cid.cid_num;
01866       if (l)
01867          ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
01868       else
01869          p->lastcid_num[0] = '\0';
01870       if (n)
01871          ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
01872       else
01873          p->lastcid_name[0] = '\0';
01874       ast_setstate(ast, AST_STATE_RINGING);
01875       index = zt_get_index(ast, p, 0);
01876       if (index > -1) {
01877          p->subs[index].needringing = 1;
01878       }
01879       break;
01880    case SIG_FXSLS:
01881    case SIG_FXSGS:
01882    case SIG_FXSKS:
01883    case SIG_EMWINK:
01884    case SIG_EM:
01885    case SIG_EM_E1:
01886    case SIG_FEATD:
01887    case SIG_FEATDMF:
01888    case SIG_E911:
01889    case SIG_FEATB:
01890    case SIG_SFWINK:
01891    case SIG_SF:
01892    case SIG_SF_FEATD:
01893    case SIG_SF_FEATDMF:
01894    case SIG_FEATDMF_TA:
01895    case SIG_SF_FEATB:
01896       c = strchr(dest, '/');
01897       if (c)
01898          c++;
01899       else
01900          c = "";
01901       if (strlen(c) < p->stripmsd) {
01902          ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01903          ast_mutex_unlock(&p->lock);
01904          return -1;
01905       }
01906 #ifdef ZAPATA_PRI
01907       /* Start the trunk, if not GR-303 */
01908       if (!p->pri) {
01909 #endif
01910          x = ZT_START;
01911          res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01912          if (res < 0) {
01913             if (errno != EINPROGRESS) {
01914                ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
01915                ast_mutex_unlock(&p->lock);
01916                return -1;
01917             }
01918          }
01919 #ifdef ZAPATA_PRI
01920       }
01921 #endif
01922       ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
01923       p->dop.op = ZT_DIAL_OP_REPLACE;
01924 
01925       c += p->stripmsd;
01926 
01927       switch (p->sig) {
01928       case SIG_FEATD:
01929          l = ast->cid.cid_num;
01930          if (l) 
01931             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
01932          else
01933             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
01934          break;
01935       case SIG_FEATDMF:
01936          l = ast->cid.cid_num;
01937          if (l) 
01938             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
01939          else
01940             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
01941          break;
01942       case SIG_FEATDMF_TA:
01943       {
01944          char *cic = NULL, *ozz = NULL;
01945 
01946          /* If you have to go through a Tandem Access point you need to use this */
01947          ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
01948          if (!ozz)
01949             ozz = defaultozz;
01950          cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
01951          if (!cic)
01952             cic = defaultcic;
01953          if (!ozz || !cic) {
01954             ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
01955             ast_mutex_unlock(&p->lock);
01956             return -1;
01957          }
01958          snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
01959          snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
01960          p->whichwink = 0;
01961       }
01962          break;
01963       case SIG_E911:
01964          ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
01965          break;
01966       case SIG_FEATB:
01967          snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
01968          break;
01969       default:
01970          if (p->pulse)
01971             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
01972          else
01973             snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
01974          break;
01975       }
01976 
01977       if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
01978          memset(p->echorest, 'w', sizeof(p->echorest) - 1);
01979          strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
01980          p->echorest[sizeof(p->echorest) - 1] = '\0';
01981          p->echobreak = 1;
01982          p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
01983       } else
01984          p->echobreak = 0;
01985       if (!res) {
01986          if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
01987             x = ZT_ONHOOK;
01988             ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01989             ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
01990             ast_mutex_unlock(&p->lock);
01991             return -1;
01992          }
01993       } else
01994          ast_log(LOG_DEBUG, "Deferring dialing...\n");
01995       p->dialing = 1;
01996       if (ast_strlen_zero(c))
01997          p->dialednone = 1;
01998       ast_setstate(ast, AST_STATE_DIALING);
01999       break;
02000    case 0:
02001       /* Special pseudo -- automatically up*/
02002       ast_setstate(ast, AST_STATE_UP);
02003       break;      
02004    case SIG_PRI:
02005       /* We'll get it in a moment -- but use dialdest to store pre-setup_ack digits */
02006       p->dialdest[0] = '\0';
02007       break;
02008    default:
02009       ast_log(LOG_DEBUG, "not yet implemented\n");
02010       ast_mutex_unlock(&p->lock);
02011       return -1;
02012    }
02013 #ifdef ZAPATA_PRI
02014    if (p->pri) {
02015       struct pri_sr *sr;
02016 #ifdef SUPPORT_USERUSER
02017       char *useruser;
02018 #endif
02019       int pridialplan;
02020       int dp_strip;
02021       int prilocaldialplan;
02022       int ldp_strip;
02023       int exclusive;
02024 
02025       c = strchr(dest, '/');
02026       if (c)
02027          c++;
02028       else
02029          c = dest;
02030       if (!p->hidecallerid) {
02031          l = ast->cid.cid_num;
02032          n = ast->cid.cid_name;
02033       } else {
02034          l = NULL;
02035          n = NULL;
02036       }
02037       if (strlen(c) < p->stripmsd) {
02038          ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
02039          ast_mutex_unlock(&p->lock);
02040          return -1;
02041       }
02042       if (p->sig != SIG_FXSKS) {
02043          p->dop.op = ZT_DIAL_OP_REPLACE;
02044          s = strchr(c + p->stripmsd, 'w');
02045          if (s) {
02046             if (strlen(s) > 1)
02047                snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
02048             else
02049                p->dop.dialstr[0] = '\0';
02050             *s = '\0';
02051          } else {
02052             p->dop.dialstr[0] = '\0';
02053          }
02054       }
02055       if (pri_grab(p, p->pri)) {
02056          ast_log(LOG_WARNING, "Failed to grab PRI!\n");
02057          ast_mutex_unlock(&p->lock);
02058          return -1;
02059       }
02060       if (!(p->call = pri_new_call(p->pri->pri))) {
02061          ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
02062          pri_rel(p->pri);
02063          ast_mutex_unlock(&p->lock);
02064          return -1;
02065       }
02066       if (!(sr = pri_sr_new())) {
02067          ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
02068          pri_rel(p->pri);
02069          ast_mutex_unlock(&p->lock);
02070       }
02071       if (p->bearer || (p->sig == SIG_FXSKS)) {
02072          if (p->bearer) {
02073             ast_log(LOG_DEBUG, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
02074             p->bearer->call = p->call;
02075          } else
02076             ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
02077          pri_set_crv(p->pri->pri, p->call, p->channel, 0);
02078       }
02079       p->digital = IS_DIGITAL(ast->transfercapability);
02080       /* Add support for exclusive override */
02081       if (p->priexclusive)
02082          exclusive = 1;
02083       else {
02084       /* otherwise, traditional behavior */
02085          if (p->pri->nodetype == PRI_NETWORK)
02086             exclusive = 0;
02087          else
02088             exclusive = 1;
02089       }
02090       
02091       pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
02092       pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability, 
02093                (p->digital ? -1 : 
02094                   ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
02095       if (p->pri->facilityenable)
02096          pri_facility_enable(p->pri->pri);
02097 
02098       if (option_verbose > 2)
02099          ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
02100       dp_strip = 0;
02101       pridialplan = p->pri->dialplan - 1;
02102       if (pridialplan == -2) { /* compute dynamically */
02103          if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02104             dp_strip = strlen(p->pri->internationalprefix);
02105             pridialplan = PRI_INTERNATIONAL_ISDN;
02106          } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02107             dp_strip = strlen(p->pri->nationalprefix);
02108             pridialplan = PRI_NATIONAL_ISDN;
02109          } else {
02110             pridialplan = PRI_LOCAL_ISDN;
02111          }
02112       }
02113       pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan,  s ? 1 : 0);
02114 
02115       ldp_strip = 0;
02116       prilocaldialplan = p->pri->localdialplan - 1;
02117       if ((l != NULL) && (prilocaldialplan == -2)) { /* compute dynamically */
02118          if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02119             ldp_strip = strlen(p->pri->internationalprefix);
02120             prilocaldialplan = PRI_INTERNATIONAL_ISDN;
02121          } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02122             ldp_strip = strlen(p->pri->nationalprefix);
02123             prilocaldialplan = PRI_NATIONAL_ISDN;
02124          } else {
02125             prilocaldialplan = PRI_LOCAL_ISDN;
02126          }
02127       }
02128       pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
02129               p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
02130       pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
02131 
02132 #ifdef SUPPORT_USERUSER
02133       /* User-user info */
02134       useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
02135 
02136       if (useruser)
02137          pri_sr_set_useruser(sr, useruser);
02138 #endif
02139 
02140       if (pri_setup(p->pri->pri, p->call,  sr)) {
02141          ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n", 
02142                   c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
02143          pri_rel(p->pri);
02144          ast_mutex_unlock(&p->lock);
02145          pri_sr_free(sr);
02146          return -1;
02147       }
02148       pri_sr_free(sr);
02149       ast_setstate(ast, AST_STATE_DIALING);
02150       pri_rel(p->pri);
02151    }
02152 #endif      
02153    ast_mutex_unlock(&p->lock);
02154    return 0;
02155 }
02156 
02157 static void destroy_zt_pvt(struct zt_pvt **pvt)
02158 {
02159    struct zt_pvt *p = *pvt;
02160    /* Remove channel from the list */
02161    if(p->prev)
02162       p->prev->next = p->next;
02163    if(p->next)
02164       p->next->prev = p->prev;
02165    ast_mutex_destroy(&p->lock);
02166    free(p);
02167    *pvt = NULL;
02168 }
02169 
02170 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
02171 {
02172    int owned = 0;
02173    int i = 0;
02174 
02175    if (!now) {
02176       if (cur->owner) {
02177          owned = 1;
02178       }
02179 
02180       for (i = 0; i < 3; i++) {
02181          if (cur->subs[i].owner) {
02182             owned = 1;
02183          }
02184       }
02185       if (!owned) {
02186          if (prev) {
02187             prev->next = cur->next;
02188             if (prev->next)
02189                prev->next->prev = prev;
02190             else
02191                ifend = prev;
02192          } else {
02193             iflist = cur->next;
02194             if (iflist)
02195                iflist->prev = NULL;
02196             else
02197                ifend = NULL;
02198          }
02199          if (cur->subs[SUB_REAL].zfd > -1) {
02200             zt_close(cur->subs[SUB_REAL].zfd);
02201          }
02202          destroy_zt_pvt(&cur);
02203       }
02204    } else {
02205       if (prev) {
02206          prev->next = cur->next;
02207          if (prev->next)
02208             prev->next->prev = prev;
02209          else
02210             ifend = prev;
02211       } else {
02212          iflist = cur->next;
02213          if (iflist)
02214             iflist->prev = NULL;
02215          else
02216             ifend = NULL;
02217       }
02218       if (cur->subs[SUB_REAL].zfd > -1) {
02219          zt_close(cur->subs[SUB_REAL].zfd);
02220       }
02221       destroy_zt_pvt(&cur);
02222    }
02223    return 0;
02224 }
02225 
02226 #ifdef ZAPATA_PRI
02227 int pri_is_up(struct zt_pri *pri)
02228 {
02229    int x;
02230    for (x=0;x<NUM_DCHANS;x++) {
02231       if (pri->dchanavail[x] == DCHAN_AVAILABLE)
02232          return 1;
02233    }
02234    return 0;
02235 }
02236 
02237 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
02238 {
02239    bearer->owner = &inuse;
02240    bearer->realcall = crv;
02241    crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
02242    if (crv->subs[SUB_REAL].owner)
02243       crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
02244    crv->bearer = bearer;
02245    crv->call = bearer->call;
02246    crv->pri = pri;
02247    return 0;
02248 }
02249 
02250 static char *pri_order(int level)
02251 {
02252    switch(level) {
02253    case 0:
02254       return "Primary";
02255    case 1:
02256       return "Secondary";
02257    case 2:
02258       return "Tertiary";
02259    case 3:
02260       return "Quaternary";
02261    default:
02262       return "<Unknown>";
02263    }     
02264 }
02265 
02266 /* Returns fd of the active dchan */
02267 int pri_active_dchan_fd(struct zt_pri *pri)
02268 {
02269    int x = -1;
02270 
02271    for (x = 0; x < NUM_DCHANS; x++) {
02272       if ((pri->dchans[x] == pri->pri))
02273          break;
02274    }
02275 
02276    return pri->fds[x];
02277 }
02278 
02279 int pri_find_dchan(struct zt_pri *pri)
02280 {
02281    int oldslot = -1;
02282    struct pri *old;
02283    int newslot = -1;
02284    int x;
02285    old = pri->pri;
02286    for(x=0;x<NUM_DCHANS;x++) {
02287       if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
02288          newslot = x;
02289       if (pri->dchans[x] == old) {
02290          oldslot = x;
02291       }
02292    }
02293    if (newslot < 0) {
02294       newslot = 0;
02295       ast_log(LOG_WARNING, "No D-channels available!  Using Primary channel %d as D-channel anyway!\n",
02296          pri->dchannels[newslot]);
02297    }
02298    if (old && (oldslot != newslot))
02299       ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
02300          pri->dchannels[oldslot], pri->dchannels[newslot]);
02301    pri->pri = pri->dchans[newslot];
02302    return 0;
02303 }
02304 #endif
02305 
02306 static int zt_hangup(struct ast_channel *ast)
02307 {
02308    int res;
02309    int index,x, law;
02310    /*static int restore_gains(struct zt_pvt *p);*/
02311    struct zt_pvt *p = ast->tech_pvt;
02312    struct zt_pvt *tmp = NULL;
02313    struct zt_pvt *prev = NULL;
02314    ZT_PARAMS par;
02315 
02316    if (option_debug)
02317       ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
02318    if (!ast->tech_pvt) {
02319       ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
02320       return 0;
02321    }
02322    
02323    ast_mutex_lock(&p->lock);
02324    
02325    index = zt_get_index(ast, p, 1);
02326 
02327    if (p->sig == SIG_PRI) {
02328       x = 1;
02329       ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02330    }
02331 
02332    x = 0;
02333    zt_confmute(p, 0);
02334    restore_gains(p);
02335    if (p->origcid_num) {
02336       ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
02337       free(p->origcid_num);
02338       p->origcid_num = NULL;
02339    }  
02340    if (p->origcid_name) {
02341       ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
02342       free(p->origcid_name);
02343       p->origcid_name = NULL;
02344    }  
02345    if (p->dsp)
02346       ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02347    if (p->exten)
02348       p->exten[0] = '\0';
02349 
02350    ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
02351       p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
02352    p->ignoredtmf = 0;
02353    
02354    if (index > -1) {
02355       /* Real channel, do some fixup */
02356       p->subs[index].owner = NULL;
02357       p->subs[index].needanswer = 0;
02358       p->subs[index].needflash = 0;
02359       p->subs[index].needringing = 0;
02360       p->subs[index].needbusy = 0;
02361       p->subs[index].needcongestion = 0;
02362       p->subs[index].linear = 0;
02363       p->subs[index].needcallerid = 0;
02364       p->polarity = POLARITY_IDLE;
02365       zt_setlinear(p->subs[index].zfd, 0);
02366       if (index == SUB_REAL) {
02367          if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
02368             ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
02369             if (p->subs[SUB_CALLWAIT].inthreeway) {
02370                /* We had flipped over to answer a callwait and now it's gone */
02371                ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
02372                /* Move to the call-wait, but un-own us until they flip back. */
02373                swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02374                unalloc_sub(p, SUB_CALLWAIT);
02375                p->owner = NULL;
02376             } else {
02377                /* The three way hung up, but we still have a call wait */
02378                ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still.  Ditching the threeway.\n");
02379                swap_subs(p, SUB_THREEWAY, SUB_REAL);
02380                unalloc_sub(p, SUB_THREEWAY);
02381                if (p->subs[SUB_REAL].inthreeway) {
02382                   /* This was part of a three way call.  Immediately make way for
02383                      another call */
02384                   ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02385                   p->owner = p->subs[SUB_REAL].owner;
02386                } else {
02387                   /* This call hasn't been completed yet...  Set owner to NULL */
02388                   ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02389                   p->owner = NULL;
02390                }
02391                p->subs[SUB_REAL].inthreeway = 0;
02392             }
02393          } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
02394             /* Move to the call-wait and switch back to them. */
02395             swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02396             unalloc_sub(p, SUB_CALLWAIT);
02397             p->owner = p->subs[SUB_REAL].owner;
02398             if (p->owner->_state != AST_STATE_UP)
02399                p->subs[SUB_REAL].needanswer = 1;
02400             if (ast_bridged_channel(p->subs[SUB_REAL].owner))
02401                ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
02402          } else if (p->subs[SUB_THREEWAY].zfd > -1) {
02403             swap_subs(p, SUB_THREEWAY, SUB_REAL);
02404             unalloc_sub(p, SUB_THREEWAY);
02405             if (p->subs[SUB_REAL].inthreeway) {
02406                /* This was part of a three way call.  Immediately make way for
02407                   another call */
02408                ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02409                p->owner = p->subs[SUB_REAL].owner;
02410             } else {
02411                /* This call hasn't been completed yet...  Set owner to NULL */
02412                ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02413                p->owner = NULL;
02414             }
02415             p->subs[SUB_REAL].inthreeway = 0;
02416          }
02417       } else if (index == SUB_CALLWAIT) {
02418          /* Ditch the holding callwait call, and immediately make it availabe */
02419          if (p->subs[SUB_CALLWAIT].inthreeway) {
02420             /* This is actually part of a three way, placed on hold.  Place the third part
02421                on music on hold now */
02422             if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
02423                ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
02424             p->subs[SUB_THREEWAY].inthreeway = 0;
02425             /* Make it the call wait now */
02426             swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
02427             unalloc_sub(p, SUB_THREEWAY);
02428          } else
02429             unalloc_sub(p, SUB_CALLWAIT);
02430       } else if (index == SUB_THREEWAY) {
02431          if (p->subs[SUB_CALLWAIT].inthreeway) {
02432             /* The other party of the three way call is currently in a call-wait state.
02433                Start music on hold for them, and take the main guy out of the third call */
02434             if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
02435                ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
02436             p->subs[SUB_CALLWAIT].inthreeway = 0;
02437          }
02438          p->subs[SUB_REAL].inthreeway = 0;
02439          /* If this was part of a three way call index, let us make
02440             another three way call */
02441          unalloc_sub(p, SUB_THREEWAY);
02442       } else {
02443          /* This wasn't any sort of call, but how are we an index? */
02444          ast_log(LOG_WARNING, "Index found but not any type of call?\n");
02445       }
02446    }
02447 
02448 
02449    if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
02450       p->owner = NULL;
02451       p->ringt = 0;
02452       p->distinctivering = 0;
02453       p->confirmanswer = 0;
02454       p->cidrings = 1;
02455       p->outgoing = 0;
02456       p->digital = 0;
02457       p->faxhandled = 0;
02458       p->pulsedial = 0;
02459       p->onhooktime = time(NULL);
02460 #ifdef ZAPATA_PRI
02461       p->proceeding = 0;
02462       p->progress = 0;
02463       p->alerting = 0;
02464       p->setup_ack = 0;
02465 #endif      
02466       if (p->dsp) {
02467          ast_dsp_free(p->dsp);
02468          p->dsp = NULL;
02469       }
02470 
02471       law = ZT_LAW_DEFAULT;
02472       res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
02473       if (res < 0) 
02474          ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
02475       /* Perform low level hangup if no owner left */
02476 #ifdef ZAPATA_PRI
02477       if (p->pri) {
02478 #ifdef SUPPORT_USERUSER
02479          char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
02480 #endif
02481 
02482          /* Make sure we have a call (or REALLY have a call in the case of a PRI) */
02483          if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
02484             if (!pri_grab(p, p->pri)) {
02485                if (p->alreadyhungup) {
02486                   ast_log(LOG_DEBUG, "Already hungup...  Calling hangup once, and clearing call\n");
02487 
02488 #ifdef SUPPORT_USERUSER
02489                   pri_call_set_useruser(p->call, useruser);
02490 #endif
02491 
02492                   pri_hangup(p->pri->pri, p->call, -1);
02493                   p->call = NULL;
02494                   if (p->bearer) 
02495                      p->bearer->call = NULL;
02496                } else {
02497                   char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
02498                   int icause = ast->hangupcause ? ast->hangupcause : -1;
02499                   ast_log(LOG_DEBUG, "Not yet hungup...  Calling hangup once with icause, and clearing call\n");
02500 
02501 #ifdef SUPPORT_USERUSER
02502                   pri_call_set_useruser(p->call, useruser);
02503 #endif
02504 
02505                   p->alreadyhungup = 1;
02506                   if (p->bearer)
02507                      p->bearer->alreadyhungup = 1;
02508                   if (cause) {
02509                      if (atoi(cause))
02510                         icause = atoi(cause);
02511                   }
02512                   pri_hangup(p->pri->pri, p->call, icause);
02513                }
02514                if (res < 0) 
02515                   ast_log(LOG_WARNING, "pri_disconnect failed\n");
02516                pri_rel(p->pri);        
02517             } else {
02518                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02519                res = -1;
02520             }
02521          } else {
02522             if (p->bearer)
02523                ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
02524             p->call = NULL;
02525             res = 0;
02526          }
02527       }
02528 #endif
02529 #ifdef ZAPATA_R2
02530       if (p->sig == SIG_R2) {
02531          if (p->hasr2call) {
02532             mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
02533             p->hasr2call = 0;
02534             res = 0;
02535          } else
02536             res = 0;
02537 
02538       }
02539 #endif
02540       if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
02541          res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
02542       if (res < 0) {
02543          ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
02544       }
02545       switch(p->sig) {
02546       case SIG_FXOGS:
02547       case SIG_FXOLS:
02548       case SIG_FXOKS:
02549          res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
02550          if (!res) {
02551 #if 0
02552             ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
02553 #endif
02554             /* If they're off hook, try playing congestion */
02555             if ((par.rxisoffhook) && (!p->radio))
02556                tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
02557             else
02558                tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02559          }
02560          break;
02561       case SIG_FXSGS:
02562       case SIG_FXSLS:
02563       case SIG_FXSKS:
02564          /* Make sure we're not made available for at least two seconds assuming
02565             we were actually used for an inbound or outbound call. */
02566          if (ast->_state != AST_STATE_RESERVED) {
02567             time(&p->guardtime);
02568             p->guardtime += 2;
02569          }
02570          break;
02571       default:
02572          tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02573       }
02574       if (p->cidspill)
02575          free(p->cidspill);
02576       if (p->sig)
02577          zt_disable_ec(p);
02578       x = 0;
02579       ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
02580       ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
02581       p->didtdd = 0;
02582       p->cidspill = NULL;
02583       p->callwaitcas = 0;
02584       p->callwaiting = p->permcallwaiting;
02585       p->hidecallerid = p->permhidecallerid;
02586       p->dialing = 0;
02587       p->rdnis[0] = '\0';
02588       update_conf(p);
02589       reset_conf(p);
02590       /* Restore data mode */
02591       if (p->sig == SIG_PRI) {
02592          x = 0;
02593          ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02594       }
02595 #ifdef ZAPATA_PRI
02596       if (p->bearer) {
02597          ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
02598          /* Free up the bearer channel as well, and
02599             don't use its file descriptor anymore */
02600          update_conf(p->bearer);
02601          reset_conf(p->bearer);
02602          p->bearer->owner = NULL;
02603          p->bearer->realcall = NULL;
02604          p->bearer = NULL;
02605          p->subs[SUB_REAL].zfd = -1;
02606          p->pri = NULL;
02607       }
02608 #endif
02609       restart_monitor();
02610    }
02611 
02612 
02613    p->callwaitingrepeat = 0;
02614    p->cidcwexpire = 0;
02615    ast->tech_pvt = NULL;
02616    ast_mutex_unlock(&p->lock);
02617    ast_mutex_lock(&usecnt_lock);
02618    usecnt--;
02619    if (usecnt < 0) 
02620       ast_log(LOG_WARNING, "Usecnt < 0???\n");
02621    ast_mutex_unlock(&usecnt_lock);
02622    ast_update_use_count();
02623    if (option_verbose > 2) 
02624       ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
02625 
02626    ast_mutex_lock(&iflock);
02627    tmp = iflist;
02628    prev = NULL;
02629    if (p->destroy) {
02630       while (tmp) {
02631          if (tmp == p) {
02632             destroy_channel(prev, tmp, 0);
02633             break;
02634          } else {
02635             prev = tmp;
02636             tmp = tmp->next;
02637          }
02638       }
02639    }
02640    ast_mutex_unlock(&iflock);
02641    return 0;
02642 }
02643 
02644 static int zt_answer(struct ast_channel *ast)
02645 {
02646    struct zt_pvt *p = ast->tech_pvt;
02647    int res=0;
02648    int index;
02649    int oldstate = ast->_state;
02650    ast_setstate(ast, AST_STATE_UP);
02651    ast_mutex_lock(&p->lock);
02652    index = zt_get_index(ast, p, 0);
02653    if (index < 0)
02654       index = SUB_REAL;
02655    /* nothing to do if a radio channel */
02656    if (p->radio) {
02657       ast_mutex_unlock(&p->lock);
02658       return 0;
02659    }
02660    switch(p->sig) {
02661    case SIG_FXSLS:
02662    case SIG_FXSGS:
02663    case SIG_FXSKS:
02664       p->ringt = 0;
02665       /* Fall through */
02666    case SIG_EM:
02667    case SIG_EM_E1:
02668    case SIG_EMWINK:
02669    case SIG_FEATD:
02670    case SIG_FEATDMF:
02671    case SIG_E911:
02672    case SIG_FEATB:
02673    case SIG_SF:
02674    case SIG_SFWINK:
02675    case SIG_SF_FEATD:
02676    case SIG_SF_FEATDMF:
02677    case SIG_SF_FEATB:
02678    case SIG_FXOLS:
02679    case SIG_FXOGS:
02680    case SIG_FXOKS:
02681       /* Pick up the line */
02682       ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
02683       if(p->hanguponpolarityswitch) {
02684          gettimeofday(&p->polaritydelaytv, NULL);
02685       }
02686       res =  zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
02687       tone_zone_play_tone(p->subs[index].zfd, -1);
02688       p->dialing = 0;
02689       if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
02690          if (oldstate == AST_STATE_RINGING) {
02691             ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
02692             tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
02693             swap_subs(p, SUB_THREEWAY, SUB_REAL);
02694             p->owner = p->subs[SUB_REAL].owner;
02695          }
02696       }
02697       if (p->sig & __ZT_SIG_FXS) {
02698          zt_enable_ec(p);
02699          zt_train_ec(p);
02700       }
02701       break;
02702 #ifdef ZAPATA_PRI
02703    case SIG_PRI:
02704       /* Send a pri acknowledge */
02705       if (!pri_grab(p, p->pri)) {
02706          p->proceeding = 1;
02707          res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
02708          pri_rel(p->pri);
02709       } else {
02710          ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02711          res= -1;
02712       }
02713       break;
02714 #endif
02715 #ifdef ZAPATA_R2
02716    case SIG_R2:
02717       res = mfcr2_AnswerCall(p->r2, NULL);
02718       if (res)
02719          ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
02720       break;
02721 #endif         
02722    case 0:
02723       ast_mutex_unlock(&p->lock);
02724       return 0;
02725    default:
02726       ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
02727       res = -1;
02728    }
02729    ast_mutex_unlock(&p->lock);
02730    return res;
02731 }
02732 
02733 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
02734 {
02735    char *cp;
02736    signed char *scp;
02737    int x;
02738    int index;
02739    struct zt_pvt *p = chan->tech_pvt;
02740 
02741    /* all supported options require data */
02742    if (!data || (datalen < 1)) {
02743       errno = EINVAL;
02744       return -1;
02745    }
02746 
02747    switch(option) {
02748    case AST_OPTION_TXGAIN:
02749       scp = (signed char *) data;
02750       index = zt_get_index(chan, p, 0);
02751       if (index < 0) {
02752          ast_log(LOG_WARNING, "No index in TXGAIN?\n");
02753          return -1;
02754       }
02755       ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
02756       return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
02757    case AST_OPTION_RXGAIN:
02758       scp = (signed char *) data;
02759       index = zt_get_index(chan, p, 0);
02760       if (index < 0) {
02761          ast_log(LOG_WARNING, "No index in RXGAIN?\n");
02762          return -1;
02763       }
02764       ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
02765       return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
02766    case AST_OPTION_TONE_VERIFY:
02767       if (!p->dsp)
02768          break;
02769       cp = (char *) data;
02770       switch (*cp) {
02771       case 1:
02772          ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
02773          ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax);  /* set mute mode if desired */
02774          break;
02775       case 2:
02776          ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
02777          ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax);  /* set mute mode if desired */
02778          break;
02779       default:
02780          ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
02781          ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);  /* set mute mode if desired */
02782          break;
02783       }
02784       break;
02785    case AST_OPTION_TDD:
02786       /* turn on or off TDD */
02787       cp = (char *) data;
02788       p->mate = 0;
02789       if (!*cp) { /* turn it off */
02790          ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
02791          if (p->tdd) tdd_free(p->tdd);
02792          p->tdd = 0;
02793          break;
02794       }
02795       ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
02796          (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
02797       zt_disable_ec(p);
02798       /* otherwise, turn it on */
02799       if (!p->didtdd) { /* if havent done it yet */
02800          unsigned char mybuf[41000],*buf;
02801          int size,res,fd,len;
02802          struct pollfd fds[1];
02803 
02804          buf = mybuf;
02805          memset(buf, 0x7f, sizeof(mybuf)); /* set to silence */
02806          ast_tdd_gen_ecdisa(buf + 16000, 16000);  /* put in tone */
02807          len = 40000;
02808          index = zt_get_index(chan, p, 0);
02809          if (index < 0) {
02810             ast_log(LOG_WARNING, "No index in TDD?\n");
02811             return -1;
02812          }
02813          fd = p->subs[index].zfd;
02814          while(len) {
02815             if (ast_check_hangup(chan)) return -1;
02816             size = len;
02817             if (size > READ_SIZE)
02818                size = READ_SIZE;
02819             fds[0].fd = fd;
02820             fds[0].events = POLLPRI | POLLOUT;
02821             fds[0].revents = 0;
02822             res = poll(fds, 1, -1);
02823             if (!res) {
02824                ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
02825                continue;
02826             }
02827             /* if got exception */
02828             if (fds[0].revents & POLLPRI) return -1;
02829             if (!(fds[0].revents & POLLOUT)) {
02830                ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
02831                continue;
02832             }
02833             res = write(fd, buf, size);
02834             if (res != size) {
02835                if (res == -1) return -1;
02836                ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
02837                break;
02838             }
02839             len -= size;
02840             buf += size;
02841          }
02842          p->didtdd = 1; /* set to have done it now */    
02843       }
02844       if (*cp == 2) { /* Mate mode */
02845          if (p->tdd) tdd_free(p->tdd);
02846          p->tdd = 0;
02847          p->mate = 1;
02848          break;
02849       }     
02850       if (!p->tdd) { /* if we dont have one yet */
02851          p->tdd = tdd_new(); /* allocate one */
02852       }     
02853       break;
02854    case AST_OPTION_RELAXDTMF:  /* Relax DTMF decoding (or not) */
02855       if (!p->dsp)
02856          break;
02857       cp = (char *) data;
02858       ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
02859          *cp ? "ON" : "OFF", (int) *cp, chan->name);
02860       ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
02861       break;
02862    case AST_OPTION_AUDIO_MODE:  /* Set AUDIO mode (or not) */
02863       cp = (char *) data;
02864       if (!*cp) {    
02865          ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
02866          x = 0;
02867          zt_disable_ec(p);
02868       } else {    
02869          ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
02870          x = 1;
02871       }
02872       if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
02873          ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
02874       break;
02875    }
02876    errno = 0;
02877 
02878    return 0;
02879 }
02880 
02881 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
02882 {
02883    /* Unlink a specific slave or all slaves/masters from a given master */
02884    int x;
02885    int hasslaves;
02886    if (!master)
02887       return;
02888    if (needlock) {
02889       ast_mutex_lock(&master->lock);
02890       if (slave) {
02891          while(ast_mutex_trylock(&slave->lock)) {
02892             ast_mutex_unlock(&master->lock);
02893             usleep(1);
02894             ast_mutex_lock(&master->lock);
02895          }
02896       }
02897    }
02898    hasslaves = 0;
02899    for (x=0;x<MAX_SLAVES;x++) {
02900       if (master->slaves[x]) {
02901          if (!slave || (master->slaves[x] == slave)) {
02902             /* Take slave out of the conference */
02903             ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
02904             conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
02905             conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
02906             master->slaves[x]->master = NULL;
02907             master->slaves[x] = NULL;
02908          } else
02909             hasslaves = 1;
02910       }
02911       if (!hasslaves)
02912          master->inconference = 0;
02913    }
02914    if (!slave) {
02915       if (master->master) {
02916          /* Take master out of the conference */
02917          conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
02918          conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
02919          hasslaves = 0;
02920          for (x=0;x<MAX_SLAVES;x++) {
02921             if (master->master->slaves[x] == master)
02922                master->master->slaves[x] = NULL;
02923             else if (master->master->slaves[x])
02924                hasslaves = 1;
02925          }
02926          if (!hasslaves)
02927             master->master->inconference = 0;
02928       }
02929       master->master = NULL;
02930    }
02931    update_conf(master);
02932    if (needlock) {
02933       if (slave)
02934          ast_mutex_unlock(&slave->lock);
02935       ast_mutex_unlock(&master->lock);
02936    }
02937 }
02938 
02939 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
02940    int x;
02941    if (!slave || !master) {
02942       ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
02943       return;
02944    }
02945    for (x=0;x<MAX_SLAVES;x++) {
02946       if (!master->slaves[x]) {
02947          master->slaves[x] = slave;
02948          break;
02949       }
02950    }
02951    if (x >= MAX_SLAVES) {
02952       ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
02953       master->slaves[MAX_SLAVES - 1] = slave;
02954    }
02955    if (slave->master) 
02956       ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
02957    slave->master = master;
02958    
02959    ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
02960 }
02961 
02962 static void disable_dtmf_detect(struct zt_pvt *p)
02963 {
02964 #ifdef ZT_TONEDETECT
02965    int val;
02966 #endif
02967 
02968    p->ignoredtmf = 1;
02969 
02970 #ifdef ZT_TONEDETECT
02971    val = 0;
02972    ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
02973 #endif      
02974    if (!p->hardwaredtmf && p->dsp) {
02975       p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
02976       ast_dsp_set_features(p->dsp, p->dsp_features);
02977    }
02978 }
02979 
02980 static void enable_dtmf_detect(struct zt_pvt *p)
02981 {
02982 #ifdef ZT_TONEDETECT
02983    int val;
02984 #endif
02985 
02986    if (p->channel == CHAN_PSEUDO)
02987       return;
02988 
02989    p->ignoredtmf = 0;
02990 
02991 #ifdef ZT_TONEDETECT
02992    val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
02993    ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
02994 #endif      
02995    if (!p->hardwaredtmf && p->dsp) {
02996       p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
02997       ast_dsp_set_features(p->dsp, p->dsp_features);
02998    }
02999 }
03000 
03001 static enum ast_bridge_result zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
03002 {
03003    struct ast_channel *who;
03004    struct zt_pvt *p0, *p1, *op0, *op1;
03005    struct zt_pvt *master = NULL, *slave = NULL;
03006    struct ast_frame *f;
03007    int inconf = 0;
03008    int nothingok = 1;
03009    int ofd0, ofd1;
03010    int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
03011    int os0 = -1, os1 = -1;
03012    int priority = 0;
03013    struct ast_channel *oc0, *oc1;
03014    enum ast_bridge_result res;
03015 
03016 #ifdef PRI_2BCT
03017    int triedtopribridge = 0;
03018    q931_call *q931c0 = NULL, *q931c1 = NULL;
03019 #endif
03020 
03021    /* For now, don't attempt to native bridge if either channel needs DTMF detection.
03022       There is code below to handle it properly until DTMF is actually seen,
03023       but due to currently unresolved issues it's ignored...
03024    */
03025 
03026    if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
03027       return AST_BRIDGE_FAILED_NOWARN;
03028 
03029    ast_mutex_lock(&c0->lock);
03030    ast_mutex_lock(&c1->lock);
03031 
03032    p0 = c0->tech_pvt;
03033    p1 = c1->tech_pvt;
03034    /* cant do pseudo-channels here */
03035    if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
03036       ast_mutex_unlock(&c0->lock);
03037       ast_mutex_unlock(&c1->lock);
03038       return AST_BRIDGE_FAILED_NOWARN;
03039    }
03040 
03041    oi0 = zt_get_index(c0, p0, 0);
03042    oi1 = zt_get_index(c1, p1, 0);
03043    if ((oi0 < 0) || (oi1 < 0)) {
03044       ast_mutex_unlock(&c0->lock);
03045       ast_mutex_unlock(&c1->lock);
03046       return AST_BRIDGE_FAILED;
03047    }
03048 
03049    op0 = p0 = c0->tech_pvt;
03050    op1 = p1 = c1->tech_pvt;
03051    ofd0 = c0->fds[0];
03052    ofd1 = c1->fds[0];
03053    oc0 = p0->owner;
03054    oc1 = p1->owner;
03055 
03056    if (ast_mutex_trylock(&p0->lock)) {
03057       /* Don't block, due to potential for deadlock */
03058       ast_mutex_unlock(&c0->lock);
03059       ast_mutex_unlock(&c1->lock);
03060       ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03061       return AST_BRIDGE_RETRY;
03062    }
03063    if (ast_mutex_trylock(&p1->lock)) {
03064       /* Don't block, due to potential for deadlock */
03065       ast_mutex_unlock(&p0->lock);
03066       ast_mutex_unlock(&c0->lock);
03067       ast_mutex_unlock(&c1->lock);
03068       ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03069       return AST_BRIDGE_RETRY;
03070    }
03071 
03072    if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03073       if (p0->owner && p1->owner) {
03074          /* If we don't have a call-wait in a 3-way, and we aren't in a 3-way, we can be master */
03075          if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
03076             master = p0;
03077             slave = p1;
03078             inconf = 1;
03079          } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
03080             master = p1;
03081             slave = p0;
03082             inconf = 1;
03083          } else {
03084             ast_log(LOG_WARNING, "Huh?  Both calls are callwaits or 3-ways?  That's clever...?\n");
03085             ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
03086                p0->channel,
03087                oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03088                p0->subs[SUB_REAL].inthreeway, p0->channel,
03089                oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03090                p1->subs[SUB_REAL].inthreeway);
03091          }
03092          nothingok = 0;
03093       }
03094    } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
03095       if (p1->subs[SUB_THREEWAY].inthreeway) {
03096          master = p1;
03097          slave = p0;
03098          nothingok = 0;
03099       }
03100    } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
03101       if (p0->subs[SUB_THREEWAY].inthreeway) {
03102          master = p0;
03103          slave = p1;
03104          nothingok = 0;
03105       }
03106    } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
03107       /* We have a real and a call wait.  If we're in a three way call, put us in it, otherwise, 
03108          don't put us in anything */
03109       if (p1->subs[SUB_CALLWAIT].inthreeway) {
03110          master = p1;
03111          slave = p0;
03112          nothingok = 0;
03113       }
03114    } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
03115       /* Same as previous */
03116       if (p0->subs[SUB_CALLWAIT].inthreeway) {
03117          master = p0;
03118          slave = p1;
03119          nothingok = 0;
03120       }
03121    }
03122    ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
03123       master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
03124    if (master && slave) {
03125       /* Stop any tones, or play ringtone as appropriate.  If they're bridged
03126          in an active threeway call with a channel that is ringing, we should
03127          indicate ringing. */
03128       if ((oi1 == SUB_THREEWAY) && 
03129           p1->subs[SUB_THREEWAY].inthreeway && 
03130           p1->subs[SUB_REAL].owner && 
03131           p1->subs[SUB_REAL].inthreeway && 
03132           (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03133          ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
03134          tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE);
03135          os1 = p1->subs[SUB_REAL].owner->_state;
03136       } else {
03137          ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
03138          tone_zone_play_tone(p0->subs[oi0].zfd, -1);
03139       }
03140       if ((oi0 == SUB_THREEWAY) && 
03141           p0->subs[SUB_THREEWAY].inthreeway && 
03142           p0->subs[SUB_REAL].owner && 
03143           p0->subs[SUB_REAL].inthreeway && 
03144           (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03145          ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
03146          tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE);
03147          os0 = p0->subs[SUB_REAL].owner->_state;
03148       } else {
03149          ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
03150          tone_zone_play_tone(p1->subs[oi0].zfd, -1);
03151       }
03152       if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03153          if (!p0->echocanbridged || !p1->echocanbridged) {
03154             /* Disable echo cancellation if appropriate */
03155             zt_disable_ec(p0);
03156             zt_disable_ec(p1);
03157          }
03158       }
03159       zt_link(slave, master);
03160       master->inconference = inconf;
03161    } else if (!nothingok)
03162       ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
03163 
03164    update_conf(p0);
03165    update_conf(p1);
03166    t0 = p0->subs[SUB_REAL].inthreeway;
03167    t1 = p1->subs[SUB_REAL].inthreeway;
03168 
03169    ast_mutex_unlock(&p0->lock);
03170    ast_mutex_unlock(&p1->lock);
03171 
03172    ast_mutex_unlock(&c0->lock);
03173    ast_mutex_unlock(&c1->lock);
03174 
03175    /* Native bridge failed */
03176    if ((!master || !slave) && !nothingok) {
03177       zt_enable_ec(p0);
03178       zt_enable_ec(p1);
03179       return AST_BRIDGE_FAILED;
03180    }
03181    
03182    if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03183       disable_dtmf_detect(op0);
03184 
03185    if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03186       disable_dtmf_detect(op1);
03187 
03188    for (;;) {
03189       struct ast_channel *c0_priority[2] = {c0, c1};
03190       struct ast_channel *c1_priority[2] = {c1, c0};
03191 
03192       /* Here's our main loop...  Start by locking things, looking for private parts, 
03193          and then balking if anything is wrong */
03194       ast_mutex_lock(&c0->lock);
03195       ast_mutex_lock(&c1->lock);
03196       p0 = c0->tech_pvt;
03197       p1 = c1->tech_pvt;
03198 
03199       if (op0 == p0)
03200          i0 = zt_get_index(c0, p0, 1);
03201       if (op1 == p1)
03202          i1 = zt_get_index(c1, p1, 1);
03203       ast_mutex_unlock(&c0->lock);
03204       ast_mutex_unlock(&c1->lock);
03205 
03206       if (!timeoutms || 
03207           (op0 != p0) ||
03208           (op1 != p1) || 
03209           (ofd0 != c0->fds[0]) || 
03210           (ofd1 != c1->fds[0]) ||
03211           (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) || 
03212           (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) || 
03213           (oc0 != p0->owner) || 
03214           (oc1 != p1->owner) ||
03215           (t0 != p0->subs[SUB_REAL].inthreeway) ||
03216           (t1 != p1->subs[SUB_REAL].inthreeway) ||
03217           (oi0 != i0) ||
03218           (oi1 != i1)) {
03219          ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
03220             op0->channel, oi0, op1->channel, oi1);
03221          res = AST_BRIDGE_RETRY;
03222          goto return_from_bridge;
03223       }
03224 
03225 #ifdef PRI_2BCT
03226       q931c0 = p0->call;
03227       q931c1 = p1->call;
03228       if (p0->transfer && p1->transfer 
03229           && q931c0 && q931c1 
03230           && !triedtopribridge) {
03231          pri_channel_bridge(q931c0, q931c1);
03232          triedtopribridge = 1;
03233       }
03234 #endif
03235 
03236       who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
03237       if (!who) {
03238          ast_log(LOG_DEBUG, "Ooh, empty read...\n");
03239          continue;
03240       }
03241       f = ast_read(who);
03242       if (!f || (f->frametype == AST_FRAME_CONTROL)) {
03243          *fo = f;
03244          *rc = who;
03245          res = AST_BRIDGE_COMPLETE;
03246          goto return_from_bridge;
03247       }
03248       if (f->frametype == AST_FRAME_DTMF) {
03249          if ((who == c0) && p0->pulsedial) {
03250             ast_write(c1, f);
03251          } else if ((who == c1) && p1->pulsedial) {
03252             ast_write(c0, f);
03253          } else {
03254             *fo = f;
03255             *rc = who;
03256             res = AST_BRIDGE_COMPLETE;
03257             goto return_from_bridge;
03258          }
03259       }
03260       ast_frfree(f);
03261       
03262       /* Swap who gets priority */
03263       priority = !priority;
03264    }
03265 
03266 return_from_bridge:
03267    if (op0 == p0)
03268       zt_enable_ec(p0);
03269 
03270    if (op1 == p1)
03271       zt_enable_ec(p1);
03272 
03273    if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03274       enable_dtmf_detect(op0);
03275 
03276    if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03277       enable_dtmf_detect(op1);
03278 
03279    zt_unlink(slave, master, 1);
03280 
03281    return res;
03282 }
03283 
03284 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03285 {
03286    struct zt_pvt *p = newchan->tech_pvt;
03287    int x;
03288    ast_mutex_lock(&p->lock);
03289    ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
03290    if (p->owner == oldchan) {
03291       p->owner = newchan;
03292    }
03293    for (x=0;x<3;x++)
03294       if (p->subs[x].owner == oldchan) {
03295          if (!x)
03296             zt_unlink(NULL, p, 0);
03297          p->subs[x].owner = newchan;
03298       }
03299    if (newchan->_state == AST_STATE_RINGING) 
03300       zt_indicate(newchan, AST_CONTROL_RINGING);
03301    update_conf(p);
03302    ast_mutex_unlock(&p->lock);
03303    return 0;
03304 }
03305 
03306 static int zt_ring_phone(struct zt_pvt *p)
03307 {
03308    int x;
03309    int res;
03310    /* Make sure our transmit state is on hook */
03311    x = 0;
03312    x = ZT_ONHOOK;
03313    res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03314    do {
03315       x = ZT_RING;
03316       res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03317       if (res) {
03318          switch(errno) {
03319          case EBUSY:
03320          case EINTR:
03321             /* Wait just in case */
03322             usleep(10000);
03323             continue;
03324          case EINPROGRESS:
03325             res = 0;
03326             break;
03327          default:
03328             ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
03329             res = 0;
03330          }
03331       }
03332    } while (res);
03333    return res;
03334 }
03335 
03336 static void *ss_thread(void *data);
03337 
03338 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
03339 
03340 static int attempt_transfer(struct zt_pvt *p)
03341 {
03342    /* In order to transfer, we need at least one of the channels to
03343       actually be in a call bridge.  We can't conference two applications
03344       together (but then, why would we want to?) */
03345    if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
03346       /* The three-way person we're about to transfer to could still be in MOH, so
03347          stop if now if appropriate */
03348       if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
03349          ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
03350       if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
03351          ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
03352       }
03353       if (p->subs[SUB_REAL].owner->cdr) {
03354          /* Move CDR from second channel to current one */
03355          p->subs[SUB_THREEWAY].owner->cdr =
03356             ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
03357          p->subs[SUB_REAL].owner->cdr = NULL;
03358       }
03359       if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
03360          /* Move CDR from second channel's bridge to current one */
03361          p->subs[SUB_THREEWAY].owner->cdr =
03362             ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
03363          ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
03364       }
03365        if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
03366          ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03367                ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
03368          return -1;
03369       }
03370       /* Orphan the channel after releasing the lock */
03371       ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03372       unalloc_sub(p, SUB_THREEWAY);
03373    } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
03374       if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
03375          ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
03376       }
03377       ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
03378       if (p->subs[SUB_THREEWAY].owner->cdr) {
03379          /* Move CDR from second channel to current one */
03380          p->subs[SUB_REAL].owner->cdr = 
03381             ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
03382          p->subs[SUB_THREEWAY].owner->cdr = NULL;
03383       }
03384       if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr) {
03385          /* Move CDR from second channel's bridge to current one */
03386          p->subs[SUB_REAL].owner->cdr = 
03387             ast_cdr_append(p->subs[SUB_REAL].owner->cdr, ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr);
03388          ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr = NULL;
03389       }
03390       if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
03391          ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03392                ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
03393          return -1;
03394       }
03395       /* Three-way is now the REAL */
03396       swap_subs(p, SUB_THREEWAY, SUB_REAL);
03397       ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
03398       unalloc_sub(p, SUB_THREEWAY);
03399       /* Tell the caller not to hangup */
03400       return 1;
03401    } else {
03402       ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
03403                p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
03404       p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03405       return -1;
03406    }
03407    return 0;
03408 }
03409 
03410 #ifdef ZAPATA_R2
03411 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
03412 {
03413    struct ast_frame *f;
03414    f = &p->subs[index].f;
03415    if (!p->r2) {
03416       ast_log(LOG_WARNING, "Huh?  No R2 structure :(\n");
03417       return NULL;
03418    }
03419    switch(e->e) {
03420    case MFCR2_EVENT_BLOCKED:
03421       if (option_verbose > 2)
03422          ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
03423       break;
03424    case MFCR2_EVENT_UNBLOCKED:
03425       if (option_verbose > 2)
03426          ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
03427       break;
03428    case MFCR2_EVENT_CONFIG_ERR:
03429       if (option_verbose > 2)
03430          ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
03431       break;
03432    case MFCR2_EVENT_RING:
03433       if (option_verbose > 2)
03434          ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
03435       break;
03436    case MFCR2_EVENT_HANGUP:
03437       if (option_verbose > 2)
03438          ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
03439       break;
03440    case MFCR2_EVENT_RINGING:
03441       if (option_verbose > 2)
03442          ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
03443       break;
03444    case MFCR2_EVENT_ANSWER:
03445       if (option_verbose > 2)
03446          ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
03447       break;
03448    case MFCR2_EVENT_HANGUP_ACK:
03449       if (option_verbose > 2)
03450          ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
03451       break;
03452    case MFCR2_EVENT_IDLE:
03453       if (option_verbose > 2)
03454          ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
03455       break;
03456    default:
03457       ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
03458       break;
03459    }
03460    return f;
03461 }
03462 
03463 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
03464 {
03465    int x;
03466    int res;
03467    mfcr2_event_t *e;
03468    res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
03469    if (res) {
03470       ast_log(LOG_WARNING, "Unable to check received bits\n");
03471       return NULL;
03472    }
03473    if (!p->r2) {
03474       ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
03475       return NULL;
03476    }
03477    e = mfcr2_cas_signaling_event(p->r2, x);
03478    return e;
03479 }
03480 #endif
03481 
03482 static int check_for_conference(struct zt_pvt *p)
03483 {
03484    ZT_CONFINFO ci;
03485    /* Fine if we already have a master, etc */
03486    if (p->master || (p->confno > -1))
03487       return 0;
03488    memset(&ci, 0, sizeof(ci));
03489    if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
03490       ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
03491       return 0;
03492    }
03493    /* If we have no master and don't have a confno, then 
03494       if we're in a conference, it's probably a MeetMe room or
03495       some such, so don't let us 3-way out! */
03496    if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
03497       if (option_verbose > 2) 
03498          ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
03499       return 1;
03500    }
03501    return 0;
03502 }
03503 
03504 static int get_alarms(struct zt_pvt *p)
03505 {
03506    int res;
03507    ZT_SPANINFO zi;
03508    memset(&zi, 0, sizeof(zi));
03509    zi.spanno = p->span;
03510    res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
03511    if (res < 0) {
03512       ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
03513       return 0;
03514    }
03515    return zi.alarms;
03516 }
03517          
03518 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
03519 {
03520    int res,x;
03521    int index;
03522    char *c;
03523    struct zt_pvt *p = ast->tech_pvt;
03524    pthread_t threadid;
03525    pthread_attr_t attr;
03526    struct ast_channel *chan;
03527 
03528    pthread_attr_init(&attr);
03529    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
03530 
03531    index = zt_get_index(ast, p, 0);
03532    p->subs[index].f.frametype = AST_FRAME_NULL;
03533    p->subs[index].f.datalen = 0;
03534    p->subs[index].f.samples = 0;
03535    p->subs[index].f.mallocd = 0;
03536    p->subs[index].f.offset = 0;
03537    p->subs[index].f.src = "zt_handle_event";
03538    p->subs[index].f.data = NULL;
03539    if (index < 0)
03540       return &p->subs[index].f;
03541    if (p->fake_event) {
03542       res = p->fake_event;
03543       p->fake_event = 0;
03544    } else
03545       res = zt_get_event(p->subs[index].zfd);
03546 
03547    ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
03548 
03549    if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFUP)) {
03550       if (res & ZT_EVENT_PULSEDIGIT)
03551          p->pulsedial = 1;
03552       else
03553          p->pulsedial = 0;
03554       ast_log(LOG_DEBUG, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse ": "", res & 0xff);
03555 #ifdef ZAPATA_PRI
03556       if (!p->proceeding && p->sig == SIG_PRI && p->pri && p->pri->overlapdial) {
03557          p->subs[index].f.frametype = AST_FRAME_NULL;
03558          p->subs[index].f.subclass = 0;
03559       } else {
03560 #endif
03561          p->subs[index].f.frametype = AST_FRAME_DTMF;
03562          p->subs[index].f.subclass = res & 0xff;
03563 #ifdef ZAPATA_PRI
03564       }
03565 #endif
03566       /* Unmute conference, return the captured digit */
03567       zt_confmute(p, 0);
03568       return &p->subs[index].f;
03569    }
03570 
03571    if (res & ZT_EVENT_DTMFDOWN) {
03572       ast_log(LOG_DEBUG, "DTMF Down '%c'\n", res & 0xff);
03573       p->subs[index].f.frametype = AST_FRAME_NULL;
03574       p->subs[index].f.subclass = 0;
03575       zt_confmute(p, 1);
03576       /* Mute conference, return null frame */
03577       return &p->subs[index].f;
03578    }
03579 
03580    switch(res) {
03581       case ZT_EVENT_BITSCHANGED:
03582          if (p->sig == SIG_R2) {
03583 #ifdef ZAPATA_R2
03584             struct ast_frame  *f = &p->subs[index].f;
03585             mfcr2_event_t *e;
03586             e = r2_get_event_bits(p);
03587             if (e)
03588                f = handle_r2_event(p, e, index);
03589             return f;
03590 #else          
03591             break;
03592 #endif
03593          }
03594          ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
03595       case ZT_EVENT_PULSE_START:
03596          /* Stop tone if there's a pulse start and the PBX isn't started */
03597          if (!ast->pbx)
03598             tone_zone_play_tone(p->subs[index].zfd, -1);
03599          break;   
03600       case ZT_EVENT_DIALCOMPLETE:
03601          if (p->inalarm) break;
03602          if (p->radio) break;
03603          if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
03604             ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
03605             return NULL;
03606          }
03607          if (!x) { /* if not still dialing in driver */
03608             zt_enable_ec(p);
03609             if (p->echobreak) {
03610                zt_train_ec(p);
03611                ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
03612                p->dop.op = ZT_DIAL_OP_REPLACE;
03613                res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
03614                p->echobreak = 0;
03615             } else {
03616                p->dialing = 0;
03617                if (p->sig == SIG_E911) {
03618                   /* if thru with dialing after offhook */
03619                   if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
03620                      ast_setstate(ast, AST_STATE_UP);
03621                      p->subs[index].f.frametype = AST_FRAME_CONTROL;
03622                      p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03623                      break;
03624                   } else { /* if to state wait for offhook to dial rest */
03625                      /* we now wait for off hook */
03626                      ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
03627                   }
03628                }
03629                if (ast->_state == AST_STATE_DIALING) {
03630                   if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
03631                      ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
03632                   } 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)))) {
03633                      ast_setstate(ast, AST_STATE_RINGING);
03634                   } else if (!p->answeronpolarityswitch) {
03635                      ast_setstate(ast, AST_STATE_UP);
03636                      p->subs[index].f.frametype = AST_FRAME_CONTROL;
03637                      p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03638                   }
03639                }
03640             }
03641          }
03642          break;
03643       case ZT_EVENT_ALARM:
03644 #ifdef ZAPATA_PRI
03645          if (p->call) {
03646             if (p->pri && p->pri->pri) {
03647                if (!pri_grab(p, p->pri)) {
03648                   pri_hangup(p->pri->pri, p->call, -1);
03649                   pri_destroycall(p->pri->pri, p->call);
03650                   p->call = NULL;
03651                   pri_rel(p->pri);
03652                } else
03653                   ast_log(LOG_WARNING, "Failed to grab PRI!\n");
03654             } else
03655                ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
03656          }
03657          if (p->owner)
03658             p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
03659          if (p->bearer)
03660             p->bearer->inalarm = 1;
03661          else
03662 #endif
03663          p->inalarm = 1;
03664          res = get_alarms(p);
03665          ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
03666          manager_event(EVENT_FLAG_SYSTEM, "Alarm",
03667                         "Alarm: %s\r\n"
03668                         "Channel: %d\r\n",
03669                         alarm2str(res), p->channel);
03670          /* fall through intentionally */
03671       case ZT_EVENT_ONHOOK:
03672          if (p->radio)
03673          {
03674             p->subs[index].f.frametype = AST_FRAME_CONTROL;
03675             p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
03676             break;
03677          }
03678          switch(p->sig) {
03679          case SIG_FXOLS:
03680          case SIG_FXOGS:
03681          case SIG_FXOKS:
03682             p->onhooktime = time(NULL);
03683             p->msgstate = -1;
03684             /* Check for some special conditions regarding call waiting */
03685             if (index == SUB_REAL) {
03686                /* The normal line was hung up */
03687                if (p->subs[SUB_CALLWAIT].owner) {
03688                   /* There's a call waiting call, so ring the phone, but make it unowned in the mean time */
03689                   swap_subs(p, SUB_CALLWAIT, SUB_REAL);
03690                   if (option_verbose > 2) 
03691                      ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
03692                   unalloc_sub(p, SUB_CALLWAIT); 
03693 #if 0
03694                   p->subs[index].needanswer = 0;
03695                   p->subs[index].needringing = 0;
03696 #endif                  
03697                   p->callwaitingrepeat = 0;
03698                   p->cidcwexpire = 0;
03699                   p->owner = NULL;
03700                   /* Don't start streaming audio yet if the incoming call isn't up yet */
03701                   if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
03702                      p->dialing = 1;
03703                   zt_ring_phone(p);
03704                } else if (p->subs[SUB_THREEWAY].owner) {
03705                   unsigned int mssinceflash;
03706                   /* Here we have to retain the lock on both the main channel, the 3-way channel, and
03707                      the private structure -- not especially easy or clean */
03708                   while(p->subs[SUB_THREEWAY].owner && ast_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) {
03709                      /* Yuck, didn't get the lock on the 3-way, gotta release everything and re-grab! */
03710                      ast_mutex_unlock(&p->lock);
03711                      ast_mutex_unlock(&ast->lock);
03712                      usleep(1);
03713                      /* We can grab ast and p in that order, without worry.  We should make sure
03714                         nothing seriously bad has happened though like some sort of bizarre double
03715                         masquerade! */
03716                      ast_mutex_lock(&ast->lock);
03717                      ast_mutex_lock(&p->lock);
03718                      if (p->owner != ast) {
03719                         ast_log(LOG_WARNING, "This isn't good...\n");
03720                         return NULL;
03721                      }
03722                   }
03723                   if (!p->subs[SUB_THREEWAY].owner) {
03724                      ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
03725                      return NULL;
03726                   }
03727                   mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
03728                   ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
03729                   if (mssinceflash < MIN_MS_SINCE_FLASH) {
03730                      /* It hasn't been long enough since the last flashook.  This is probably a bounce on 
03731                         hanging up.  Hangup both channels now */
03732                      if (p->subs[SUB_THREEWAY].owner)
03733                         ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
03734                      p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03735                      ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
03736                      ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03737                   } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
03738                      if (p->transfer) {
03739                         /* In any case this isn't a threeway call anymore */
03740                         p->subs[SUB_REAL].inthreeway = 0;
03741                         p->subs[SUB_THREEWAY].inthreeway = 0;
03742                         /* Only attempt transfer if the phone is ringing; why transfer to busy tone eh? */
03743                         if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
03744                            ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03745                            /* Swap subs and dis-own channel */
03746                            swap_subs(p, SUB_THREEWAY, SUB_REAL);
03747                            p->owner = NULL;
03748                            /* Ring the phone */
03749                            zt_ring_phone(p);
03750                         } else {
03751                            if ((res = attempt_transfer(p)) < 0) {
03752                               p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03753                               if (p->subs[SUB_THREEWAY].owner)
03754                                  ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03755                            } else if (res) {
03756                               /* Don't actually hang up at this point */
03757                               if (p->subs[SUB_THREEWAY].owner)
03758                                  ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03759                               break;
03760                            }
03761                         }
03762                      } else {
03763                         p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03764                         if (p->subs[SUB_THREEWAY].owner)
03765                            ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03766                      }
03767                   } else {
03768                      ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03769                      /* Swap subs and dis-own channel */
03770                      swap_subs(p, SUB_THREEWAY, SUB_REAL);
03771                      p->owner = NULL;
03772                      /* Ring the phone */
03773                      zt_ring_phone(p);
03774                   }
03775                }
03776             } else {
03777                ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
03778             }
03779             /* Fall through */
03780          default:
03781             zt_disable_ec(p);
03782             return NULL;
03783          }
03784          break;
03785       case ZT_EVENT_RINGOFFHOOK:
03786          if (p->inalarm) break;
03787          if (p->radio)
03788          {
03789             p->subs[index].f.frametype = AST_FRAME_CONTROL;
03790             p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
03791             break;
03792          }
03793          /* for E911, its supposed to wait for offhook then dial
03794             the second half of the dial string */
03795          if ((p->sig == SIG_E911) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
03796             c = strchr(p->dialdest, '/');
03797             if (c)
03798                c++;
03799             else
03800                c = p->dialdest;
03801             if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
03802             else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
03803             if (strlen(p->dop.dialstr) > 4) {
03804                memset(p->echorest, 'w', sizeof(p->echorest) - 1);
03805                strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
03806                p->echorest[sizeof(p->echorest) - 1] = '\0';
03807                p->echobreak = 1;
03808                p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
03809             } else
03810                p->echobreak = 0;
03811             if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
03812                x = ZT_ONHOOK;
03813                ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03814                ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
03815                return NULL;
03816                }
03817             p->dialing = 1;
03818             return &p->subs[index].f;
03819          }
03820          switch(p->sig) {
03821          case SIG_FXOLS:
03822          case SIG_FXOGS:
03823          case SIG_FXOKS:
03824             switch(ast->_state) {
03825             case AST_STATE_RINGING:
03826                zt_enable_ec(p);
03827                zt_train_ec(p);
03828                p->subs[index].f.frametype = AST_FRAME_CONTROL;
03829                p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03830                /* Make sure it stops ringing */
03831                zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
03832                ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
03833                if (p->cidspill) {
03834                   /* Cancel any running CallerID spill */
03835                   free(p->cidspill);
03836                   p->cidspill = NULL;
03837                }
03838                p->dialing = 0;
03839                p->callwaitcas = 0;
03840                if (p->confirmanswer) {
03841                   /* Ignore answer if "confirm answer" is enabled */
03842                   p->subs[index].f.frametype = AST_FRAME_NULL;
03843                   p->subs[index].f.subclass = 0;
03844                } else if (!ast_strlen_zero(p->dop.dialstr)) {
03845                   /* nick@dccinc.com 4/3/03 - fxo should be able to do deferred dialing */
03846                   res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
03847                   if (res < 0) {
03848                      ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
03849                      p->dop.dialstr[0] = '\0';
03850                      return NULL;
03851                   } else {
03852                      ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
03853                      p->subs[index].f.frametype = AST_FRAME_NULL;
03854                      p->subs[index].f.subclass = 0;
03855                      p->dialing = 1;
03856                   }
03857                   p->dop.dialstr[0] = '\0';
03858                   ast_setstate(ast, AST_STATE_DIALING);
03859                } else
03860                   ast_setstate(ast, AST_STATE_UP);
03861                return &p->subs[index].f;
03862             case AST_STATE_DOWN:
03863                ast_setstate(ast, AST_STATE_RING);
03864                ast->rings = 1;
03865                p->subs[index].f.frametype = AST_FRAME_CONTROL;
03866                p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
03867                ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
03868                return &p->subs[index].f;
03869             case AST_STATE_UP:
03870                /* Make sure it stops ringing */
03871                zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
03872                /* Okay -- probably call waiting*/
03873                if (ast_bridged_channel(p->owner))
03874                      ast_moh_stop(ast_bridged_channel(p->owner));
03875                break;
03876             case AST_STATE_RESERVED:
03877                /* Start up dialtone */
03878                if (has_voicemail(p))
03879                   res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
03880                else
03881                   res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
03882                break;
03883             default:
03884                ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
03885             }
03886             break;
03887          case SIG_FXSLS:
03888          case SIG_FXSGS:
03889          case SIG_FXSKS:
03890             if (ast->_state == AST_STATE_RING) {
03891                p->ringt = p->ringt_base;
03892             }
03893 
03894             /* If we get a ring then we cannot be in 
03895              * reversed polarity. So we reset to idle */
03896             ast_log(LOG_DEBUG, "Setting IDLE polarity due "
03897                "to ring. Old polarity was %d\n", 
03898                p->polarity);
03899             p->polarity = POLARITY_IDLE;
03900 
03901             /* Fall through */
03902          case SIG_EM:
03903          case SIG_EM_E1:
03904          case SIG_EMWINK:
03905          case SIG_FEATD:
03906          case SIG_FEATDMF:
03907          case SIG_FEATDMF_TA:
03908          case SIG_E911:
03909          case SIG_FEATB:
03910          case SIG_SF:
03911          case SIG_SFWINK:
03912          case SIG_SF_FEATD:
03913          case SIG_SF_FEATDMF:
03914          case SIG_SF_FEATB:
03915             if (ast->_state == AST_STATE_PRERING)
03916                ast_setstate(ast, AST_STATE_RING);
03917             if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
03918                if (option_debug)
03919                   ast_log(LOG_DEBUG, "Ring detected\n");
03920                p->subs[index].f.frametype = AST_FRAME_CONTROL;
03921                p->subs[index].f.subclass = AST_CONTROL_RING;
03922             } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
03923                if (option_debug)
03924                   ast_log(LOG_DEBUG, "Line answered\n");
03925                if (p->confirmanswer) {
03926                   p->subs[index].f.frametype = AST_FRAME_NULL;
03927                   p->subs[index].f.subclass = 0;
03928                } else {
03929                   p->subs[index].f.frametype = AST_FRAME_CONTROL;
03930                   p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03931                   ast_setstate(ast, AST_STATE_UP);
03932                }
03933             } else if (ast->_state != AST_STATE_RING)
03934                ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
03935             break;
03936          default:
03937             ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
03938          }
03939          break;
03940 #ifdef ZT_EVENT_RINGBEGIN
03941       case ZT_EVENT_RINGBEGIN:
03942          switch(p->sig) {
03943          case SIG_FXSLS:
03944          case SIG_FXSGS:
03945          case SIG_FXSKS:
03946             if (ast->_state == AST_STATE_RING) {
03947                p->ringt = p->ringt_base;
03948             }
03949             break;
03950          }
03951          break;
03952 #endif         
03953       case ZT_EVENT_RINGEROFF:
03954          if (p->inalarm) break;
03955          if (p->radio) break;
03956          ast->rings++;
03957          if ((ast->rings > p->cidrings) && (p->cidspill)) {
03958             ast_log(LOG_WARNING, "Didn't finish Caller-ID spill.  Cancelling.\n");
03959             free(p->cidspill);
03960             p->cidspill = NULL;
03961             p->callwaitcas = 0;
03962          }
03963          p->subs[index].f.frametype = AST_FRAME_CONTROL;
03964          p->subs[index].f.subclass = AST_CONTROL_RINGING;
03965          break;
03966       case ZT_EVENT_RINGERON:
03967          break;
03968       case ZT_EVENT_NOALARM:
03969          p->inalarm = 0;
03970 #ifdef ZAPATA_PRI
03971          /* Extremely unlikely but just in case */
03972          if (p->bearer)
03973             p->bearer->inalarm = 0;
03974 #endif            
03975          ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
03976          manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
03977                         "Channel: %d\r\n", p->channel);
03978          break;
03979       case ZT_EVENT_WINKFLASH:
03980          if (p->inalarm) break;
03981          if (p->radio) break;
03982          /* Remember last time we got a flash-hook */
03983          gettimeofday(&p->flashtime, NULL);
03984          switch(p->sig) {
03985          case SIG_FXOLS:
03986          case SIG_FXOGS:
03987          case SIG_FXOKS:
03988             ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
03989                index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
03990             p->callwaitcas = 0;
03991 
03992             if (index != SUB_REAL) {
03993                ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
03994                goto winkflashdone;
03995             }
03996             
03997             if (p->subs[SUB_CALLWAIT].owner) {
03998                /* Swap to call-wait */
03999                swap_subs(p, SUB_REAL, SUB_CALLWAIT);
04000                tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
04001                p->owner = p->subs[SUB_REAL].owner;
04002                ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
04003                if (p->owner->_state == AST_STATE_RINGING) {
04004                   ast_setstate(p->owner, AST_STATE_UP);
04005                   p->subs[SUB_REAL].needanswer = 1;
04006                }
04007                p->callwaitingrepeat = 0;
04008                p->cidcwexpire = 0;
04009                /* Start music on hold if appropriate */
04010                if (!p->subs[SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
04011                   ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
04012                if (ast_bridged_channel(p->subs[SUB_REAL].owner))
04013                   ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
04014             } else if (!p->subs[SUB_THREEWAY].owner) {
04015                char cid_num[256];
04016                char cid_name[256];
04017 
04018                if (!p->threewaycalling) {
04019                   /* Just send a flash if no 3-way calling */
04020                   p->subs[SUB_REAL].needflash = 1;
04021                   goto winkflashdone;
04022                } else if (!check_for_conference(p)) {
04023                   if (p->zaptrcallerid && p->owner) {
04024                      if (p->owner->cid.cid_num)
04025                         ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
04026                      if (p->owner->cid.cid_name)
04027                         ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
04028                   }
04029                   /* XXX This section needs much more error checking!!! XXX */
04030                   /* Start a 3-way call if feasible */
04031                   if (!((ast->pbx) ||
04032                         (ast->_state == AST_STATE_UP) ||
04033                         (ast->_state == AST_STATE_RING))) {
04034                      ast_log(LOG_DEBUG, "Flash when call not up or ringing\n");
04035                         goto winkflashdone;
04036                   }
04037                   if (alloc_sub(p, SUB_THREEWAY)) {
04038                      ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
04039                      goto winkflashdone;
04040                   }
04041                   /* Make new channel */
04042                   chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
04043                   if (p->zaptrcallerid) {
04044                      if (!p->origcid_num)
04045                         p->origcid_num = strdup(p->cid_num);
04046                      if (!p->origcid_name)
04047                         p->origcid_name = strdup(p->cid_name);
04048                      ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
04049                      ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
04050                   }
04051                   /* Swap things around between the three-way and real call */
04052                   swap_subs(p, SUB_THREEWAY, SUB_REAL);
04053                   /* Disable echo canceller for better dialing */
04054                   zt_disable_ec(p);
04055                   res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
04056                   if (res)
04057                      ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
04058                   p->owner = chan;
04059                   if (!chan) {
04060                      ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
04061                   } else if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
04062                      ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
04063                      res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
04064                      zt_enable_ec(p);
04065                      ast_hangup(chan);
04066                   } else {
04067                      if (option_verbose > 2) 
04068                         ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);
04069                      /* Start music on hold if appropriate */
04070                      if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
04071                         ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
04072                   }     
04073                }
04074             } else {
04075                /* Already have a 3 way call */
04076                if (p->subs[SUB_THREEWAY].inthreeway) {
04077                   /* Call is already up, drop the last person */
04078                   if (option_debug)
04079                      ast_log(LOG_DEBUG, "Got flash with three way call up, dropping last call on %d\n", p->channel);
04080                   /* If the primary call isn't answered yet, use it */
04081                   if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
04082                      /* Swap back -- we're dropping the real 3-way that isn't finished yet*/
04083                      swap_subs(p, SUB_THREEWAY, SUB_REAL);
04084                      p->owner = p->subs[SUB_REAL].owner;
04085                   }
04086                   /* Drop the last call and stop the conference */
04087                   if (option_verbose > 2)
04088                      ast_verbose(VERBOSE_PREFIX_3 "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
04089                   p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04090                   p->subs[SUB_REAL].inthreeway = 0;
04091                   p->subs[SUB_THREEWAY].inthreeway = 0;
04092                } else {
04093                   /* Lets see what we're up to */
04094                   if (((ast->pbx) || (ast->_state == AST_STATE_UP)) && 
04095                       (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
04096                      int otherindex = SUB_THREEWAY;
04097 
04098                      if (option_verbose > 2)
04099                         ast_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);
04100                      /* Put them in the threeway, and flip */
04101                      p->subs[SUB_THREEWAY].inthreeway = 1;
04102                      p->subs[SUB_REAL].inthreeway = 1;
04103                      if (ast->_state == AST_STATE_UP) {
04104                         swap_subs(p, SUB_THREEWAY, SUB_REAL);
04105                         otherindex = SUB_REAL;
04106                      }
04107                      if (p->subs[otherindex].owner && ast_bridged_channel(p->subs[otherindex].owner))
04108                         ast_moh_stop(ast_bridged_channel(p->subs[otherindex].owner));
04109                      p->owner = p->subs[SUB_REAL].owner;
04110                      if (ast->_state == AST_STATE_RINGING) {
04111                         ast_log(LOG_DEBUG, "Enabling ringtone on real and threeway\n");
04112                         res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
04113                         res = tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, ZT_TONE_RINGTONE);
04114                      }
04115                   } else {
04116                      if (option_verbose > 2)
04117                         ast_verbose(VERBOSE_PREFIX_3 "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name);
04118                      swap_subs(p, SUB_THREEWAY, SUB_REAL);
04119                      p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04120                      p->owner = p->subs[SUB_REAL].owner;
04121                      if (p->subs[SUB_REAL].owner && ast_bridged_channel(p->subs[SUB_REAL].owner))
04122                         ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
04123                      zt_enable_ec(p);
04124                   }
04125                      
04126                }
04127             }
04128          winkflashdone:              
04129             update_conf(p);
04130             break;
04131          case SIG_EM:
04132          case SIG_EM_E1:
04133          case SIG_EMWINK:
04134          case SIG_FEATD:
04135          case SIG_SF:
04136          case SIG_SFWINK:
04137          case SIG_SF_FEATD:
04138          case SIG_FXSLS:
04139          case SIG_FXSGS:
04140             if (p->dialing)
04141                ast_log(LOG_DEBUG, "Ignoring wink on channel %d\n", p->channel);
04142             else
04143                ast_log(LOG_DEBUG, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
04144             break;
04145          case SIG_FEATDMF_TA:
04146             switch (p->whichwink) {
04147             case 0:
04148                ast_log(LOG_DEBUG, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04149                snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04150                break;
04151             case 1:
04152                ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
04153                break;
04154             case 2:
04155                ast_log(LOG_WARNING, "Received unexpected wink on channel of type SIG_FEATDMF_TA\n");
04156                return NULL;
04157             }
04158             p->whichwink++;
04159             /* Fall through */
04160          case SIG_FEATDMF:
04161          case SIG_E911:
04162          case SIG_FEATB:
04163          case SIG_SF_FEATDMF:
04164          case SIG_SF_FEATB:
04165             /* FGD MF *Must* wait for wink */
04166             if (!ast_strlen_zero(p->dop.dialstr))
04167                res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04168             else if (res < 0) {
04169                ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04170                p->dop.dialstr[0] = '\0';
04171                return NULL;
04172             } else 
04173                ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04174             p->dop.dialstr[0] = '\0';
04175             break;
04176          default:
04177             ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
04178          }
04179          break;
04180       case ZT_EVENT_HOOKCOMPLETE:
04181          if (p->inalarm) break;
04182          if (p->radio) break;
04183          switch(p->sig) {
04184          case SIG_FXSLS:  /* only interesting for FXS */
04185          case SIG_FXSGS:
04186          case SIG_FXSKS:
04187          case SIG_EM:
04188          case SIG_EM_E1:
04189          case SIG_EMWINK:
04190          case SIG_FEATD:
04191          case SIG_SF:
04192          case SIG_SFWINK:
04193          case SIG_SF_FEATD:
04194             if (!ast_strlen_zero(p->dop.dialstr)) 
04195                res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04196             else if (res < 0) {
04197                ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04198                p->dop.dialstr[0] = '\0';
04199                return NULL;
04200             } else 
04201                ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04202             p->dop.dialstr[0] = '\0';
04203             p->dop.op = ZT_DIAL_OP_REPLACE;
04204             break;
04205          case SIG_FEATDMF:
04206          case SIG_E911:
04207          case SIG_FEATB:
04208          case SIG_SF_FEATDMF:
04209          case SIG_SF_FEATB:
04210             ast_log(LOG_DEBUG, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
04211             break;
04212          default:
04213             break;
04214          }
04215          break;
04216       case ZT_EVENT_POLARITY:
04217                         /*
04218                          * If we get a Polarity Switch event, check to see
04219                          * if we should change the polarity state and
04220                          * mark the channel as UP or if this is an indication
04221                          * of remote end disconnect.
04222                          */
04223                         if (p->polarity == POLARITY_IDLE) {
04224                                 p->polarity = POLARITY_REV;
04225                                 if (p->answeronpolarityswitch &&
04226                                     ((ast->_state == AST_STATE_DIALING) ||
04227                                      (ast->_state == AST_STATE_RINGING))) {
04228                                         ast_log(LOG_DEBUG, "Answering on polarity switch!\n");
04229                                         ast_setstate(p->owner, AST_STATE_UP);
04230                if(p->hanguponpolarityswitch) {
04231                   gettimeofday(&p->polaritydelaytv, NULL);
04232                }
04233                break;
04234                                 } else
04235                                         ast_log(LOG_DEBUG, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, ast->_state);
04236          } 
04237          /* Removed else statement from here as it was preventing hangups from ever happening*/
04238          /* Added AST_STATE_RING in if statement below to deal with calling party hangups that take place when ringing */
04239          if(p->hanguponpolarityswitch &&
04240             (p->polarityonanswerdelay > 0) &&
04241                 (p->polarity == POLARITY_REV) &&
04242             ((ast->_state == AST_STATE_UP) || (ast->_state == AST_STATE_RING)) ) {
04243                                 /* Added log_debug information below to provide a better indication of what is going on */
04244             ast_log(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, ast->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
04245          
04246             if(ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
04247                ast_log(LOG_DEBUG, "Polarity Reversal detected and now Hanging up on channel %d\n", p->channel);
04248                ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
04249                p->polarity = POLARITY_IDLE;
04250             } else {
04251                ast_log(LOG_DEBUG, "Polarity Reversal detected but NOT hanging up (too close to answer event) on channel %d, state %d\n", p->channel, ast->_state);
04252             }
04253          } else {
04254             p->polarity = POLARITY_IDLE;
04255             ast_log(LOG_DEBUG, "Ignoring Polarity switch to IDLE on channel %d, state %d\n", p->channel, ast->_state);
04256          }
04257                         /* Added more log_debug information below to provide a better indication of what is going on */
04258          ast_log(LOG_DEBUG, "Polarity Reversal event occured - DEBUG 2: channel %d, state %d, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %d\n", p->channel, ast->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
04259          break;
04260       default:
04261          ast_log(LOG_DEBUG, "Dunno what to do with event %d on channel %d\n", res, p->channel);
04262    }
04263    return &p->subs[index].f;
04264 }
04265 
04266 static struct ast_frame *__zt_exception(struct ast_channel *ast)
04267 {
04268    struct zt_pvt *p = ast->tech_pvt;
04269    int res;
04270    int usedindex=-1;
04271    int index;
04272    struct ast_frame *f;
04273 
04274 
04275    index = zt_get_index(ast, p, 1);
04276    
04277    p->subs[index].f.frametype = AST_FRAME_NULL;
04278    p->subs[index].f.datalen = 0;
04279    p->subs[index].f.samples = 0;
04280    p->subs[index].f.mallocd = 0;
04281    p->subs[index].f.offset = 0;
04282    p->subs[index].f.subclass = 0;
04283    p->subs[index].f.delivery = ast_tv(0,0);
04284    p->subs[index].f.src = "zt_exception";
04285    p->subs[index].f.data = NULL;
04286    
04287    
04288    if ((!p->owner) && (!p->radio)) {
04289       /* If nobody owns us, absorb the event appropriately, otherwise
04290          we loop indefinitely.  This occurs when, during call waiting, the
04291          other end hangs up our channel so that it no longer exists, but we
04292          have neither FLASH'd nor ONHOOK'd to signify our desire to
04293          change to the other channel. */
04294       if (p->fake_event) {
04295          res = p->fake_event;
04296          p->fake_event = 0;
04297       } else
04298          res = zt_get_event(p->subs[SUB_REAL].zfd);
04299       /* Switch to real if there is one and this isn't something really silly... */
04300       if ((res != ZT_EVENT_RINGEROFF) && (res != ZT_EVENT_RINGERON) &&
04301          (res != ZT_EVENT_HOOKCOMPLETE)) {
04302          ast_log(LOG_DEBUG, "Restoring owner of channel %d on event %d\n", p->channel, res);
04303          p->owner = p->subs[SUB_REAL].owner;
04304          if (p->owner && ast_bridged_channel(p->owner))
04305             ast_moh_stop(ast_bridged_channel(p->owner));
04306       }
04307       switch(res) {
04308       case ZT_EVENT_ONHOOK:
04309          zt_disable_ec(p);
04310          if (p->owner) {
04311             if (option_verbose > 2) 
04312                ast_verbose(VERBOSE_PREFIX_3 "Channel %s still has call, ringing phone\n", p->owner->name);
04313             zt_ring_phone(p);
04314             p->callwaitingrepeat = 0;
04315             p->cidcwexpire = 0;
04316          } else
04317             ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04318          update_conf(p);
04319          break;
04320       case ZT_EVENT_RINGOFFHOOK:
04321          zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
04322          if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
04323             p->subs[SUB_REAL].needanswer = 1;
04324             p->dialing = 0;
04325          }
04326          break;
04327       case ZT_EVENT_HOOKCOMPLETE:
04328       case ZT_EVENT_RINGERON:
04329       case ZT_EVENT_RINGEROFF:
04330          /* Do nothing */
04331          break;
04332       case ZT_EVENT_WINKFLASH:
04333          gettimeofday(&p->flashtime, NULL);
04334          if (p->owner) {
04335             if (option_verbose > 2) 
04336                ast_verbose(VERBOSE_PREFIX_3 "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
04337             if (p->owner->_state != AST_STATE_UP) {
04338                /* Answer if necessary */
04339                usedindex = zt_get_index(p->owner, p, 0);
04340                if (usedindex > -1) {
04341                   p->subs[usedindex].needanswer = 1;
04342                }
04343                ast_setstate(p->owner, AST_STATE_UP);
04344             }
04345             p->callwaitingrepeat = 0;
04346             p->cidcwexpire = 0;
04347             if (ast_bridged_channel(p->owner))
04348                ast_moh_stop(ast_bridged_channel(p->owner));
04349          } else
04350             ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04351          update_conf(p);
04352          break;
04353       default:
04354          ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
04355       }
04356       f = &p->subs[index].f;
04357       return f;
04358    }
04359    if (!p->radio) ast_log(LOG_DEBUG, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
04360    /* If it's not us, return NULL immediately */
04361    if (ast != p->owner) {
04362       ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
04363       f = &p->subs[index].f;
04364       return f;
04365    }
04366    f = zt_handle_event(ast);
04367    return f;
04368 }
04369 
04370 struct ast_frame *zt_exception(struct ast_channel *ast)
04371 {
04372    struct zt_pvt *p = ast->tech_pvt;
04373    struct ast_frame *f;
04374    ast_mutex_lock(&p->lock);
04375    f = __zt_exception(ast);
04376    ast_mutex_unlock(&p->lock);
04377    return f;
04378 }
04379 
04380 struct ast_frame  *zt_read(struct ast_channel *ast)
04381 {
04382    struct zt_pvt *p = ast->tech_pvt;
04383    int res;
04384    int index;
04385    void *readbuf;
04386    struct ast_frame *f;
04387    
04388 
04389    ast_mutex_lock(&p->lock);
04390    
04391    index = zt_get_index(ast, p, 0);
04392    
04393    /* Hang up if we don't really exist */
04394    if (index < 0) {
04395       ast_log(LOG_WARNING, "We dont exist?\n");
04396       ast_mutex_unlock(&p->lock);
04397       return NULL;
04398    }
04399    
04400    if (p->radio && p->inalarm) return NULL;
04401 
04402    p->subs[index].f.frametype = AST_FRAME_NULL;
04403    p->subs[index].f.datalen = 0;
04404    p->subs[index].f.samples = 0;
04405    p->subs[index].f.mallocd = 0;
04406    p->subs[index].f.offset = 0;
04407    p->subs[index].f.subclass = 0;
04408    p->subs[index].f.delivery = ast_tv(0,0);
04409    p->subs[index].f.src = "zt_read";
04410    p->subs[index].f.data = NULL;
04411    
04412    /* make sure it sends initial key state as first frame */
04413    if (p->radio && (!p->firstradio))
04414    {
04415       ZT_PARAMS ps;
04416 
04417       ps.channo = p->channel;
04418       if (ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
04419          ast_mutex_unlock(&p->lock);
04420          return NULL;
04421       }
04422       p->firstradio = 1;
04423       p->subs[index].f.frametype = AST_FRAME_CONTROL;
04424       if (ps.rxisoffhook)
04425       {
04426          p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
04427       }
04428       else
04429       {
04430          p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
04431       }
04432       ast_mutex_unlock(&p->lock);
04433       return &p->subs[index].f;
04434    }
04435    if (p->ringt == 1) {
04436       ast_mutex_unlock(&p->lock);
04437       return NULL;
04438    }
04439    else if (p->ringt > 0) 
04440       p->ringt--;
04441 
04442    if (p->subs[index].needringing) {
04443       /* Send ringing frame if requested */
04444       p->subs[index].needringing = 0;
04445       p->subs[index].f.frametype = AST_FRAME_CONTROL;
04446       p->subs[index].f.subclass = AST_CONTROL_RINGING;
04447       ast_setstate(ast, AST_STATE_RINGING);
04448       ast_mutex_unlock(&p->lock);
04449       return &p->subs[index].f;
04450    }
04451 
04452    if (p->subs[index].needbusy) {
04453       /* Send busy frame if requested */
04454       p->subs[index].needbusy = 0;
04455       p->subs[index].f.frametype = AST_FRAME_CONTROL;
04456       p->subs[index].f.subclass = AST_CONTROL_BUSY;
04457       ast_mutex_unlock(&p->lock);
04458       return &p->subs[index].f;
04459    }
04460 
04461    if (p->subs[index].needcongestion) {
04462       /* Send congestion frame if requested */
04463       p->subs[index].needcongestion = 0;
04464       p->subs[index].f.frametype = AST_FRAME_CONTROL;
04465       p->subs[index].f.subclass = AST_CONTROL_CONGESTION;
04466       ast_mutex_unlock(&p->lock);
04467       return &p->subs[index].f;
04468    }
04469 
04470    if (p->subs[index].needcallerid) {
04471       ast_set_callerid(ast, !ast_strlen_zero(p->lastcid_num) ? p->lastcid_num : NULL, 
04472                      !ast_strlen_zero(p->lastcid_name) ? p->lastcid_name : NULL,
04473                      !ast_strlen_zero(p->lastcid_num) ? p->lastcid_num : NULL
04474                      );
04475       p->subs[index].needcallerid = 0;
04476    }
04477    
04478    if (p->subs[index].needanswer) {
04479       /* Send answer frame if requested */
04480       p->subs[index].needanswer = 0;
04481       p->subs[index].f.frametype = AST_FRAME_CONTROL;
04482       p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04483       ast_mutex_unlock(&p->lock);
04484       return &p->subs[index].f;
04485    }  
04486    
04487    if (p->subs[index].needflash) {
04488       /* Send answer frame if requested */
04489       p->subs[index].needflash = 0;
04490       p->subs[index].f.frametype = AST_FRAME_CONTROL;
04491       p->subs[index].f.subclass = AST_CONTROL_FLASH;
04492       ast_mutex_unlock(&p->lock);
04493       return &p->subs[index].f;
04494    }  
04495    
04496    if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
04497       if (!p->subs[index].linear) {
04498          p->subs[index].linear = 1;
04499          res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04500          if (res) 
04501             ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, index);
04502       }
04503    } else if ((ast->rawreadformat == AST_FORMAT_ULAW) ||
04504          (ast->rawreadformat == AST_FORMAT_ALAW)) {
04505       if (p->subs[index].linear) {
04506          p->subs[index].linear = 0;
04507          res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04508          if (res) 
04509             ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to companded mode.\n", p->channel, index);
04510       }
04511    } else {
04512       ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast->rawreadformat));
04513       ast_mutex_unlock(&p->lock);
04514       return NULL;
04515    }
04516    readbuf = ((unsigned char *)p->subs[index].buffer) + AST_FRIENDLY_OFFSET;
04517    CHECK_BLOCKING(ast);
04518    res = read(p->subs[index].zfd, readbuf, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04519    ast_clear_flag(ast, AST_FLAG_BLOCKING);
04520    /* Check for hangup */
04521    if (res < 0) {
04522       f = NULL;
04523       if (res == -1)  {
04524          if (errno == EAGAIN) {
04525             /* Return "NULL" frame if there is nobody there */
04526             ast_mutex_unlock(&p->lock);
04527             return &p->subs[index].f;
04528          } else if (errno == ELAST) {
04529             f = __zt_exception(ast);
04530          } else
04531             ast_log(LOG_WARNING, "zt_rec: %s\n", strerror(errno));
04532       }
04533       ast_mutex_unlock(&p->lock);
04534       return f;
04535    }
04536    if (res != (p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE)) {
04537       ast_log(LOG_DEBUG, "Short read (%d/%d), must be an event...\n", res, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04538       f = __zt_exception(ast);
04539       ast_mutex_unlock(&p->lock);
04540       return f;
04541    }
04542    if (p->tdd) { /* if in TDD mode, see if we receive that */
04543       int c;
04544 
04545       c = tdd_feed(p->tdd,readbuf,READ_SIZE);
04546       if (c < 0) {
04547          ast_log(LOG_DEBUG,"tdd_feed failed\n");
04548          ast_mutex_unlock(&p->lock);
04549          return NULL;
04550       }
04551       if (c) { /* if a char to return */
04552          p->subs[index].f.subclass = 0;
04553          p->subs[index].f.frametype = AST_FRAME_TEXT;
04554          p->subs[index].f.mallocd = 0;
04555          p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04556          p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET;
04557          p->subs[index].f.datalen = 1;
04558          *((char *) p->subs[index].f.data) = c;
04559          ast_mutex_unlock(&p->lock);
04560          return &p->subs[index].f;
04561       }
04562    }
04563    if (p->callwaitingrepeat)
04564       p->callwaitingrepeat--;
04565    if (p->cidcwexpire)
04566       p->cidcwexpire--;
04567    /* Repeat callwaiting */
04568    if (p->callwaitingrepeat == 1) {
04569       p->callwaitrings++;
04570       zt_callwait(ast);
04571    }
04572    /* Expire CID/CW */
04573    if (p->cidcwexpire == 1) {
04574       if (option_verbose > 2)
04575          ast_verbose(VERBOSE_PREFIX_3 "CPE does not support Call Waiting Caller*ID.\n");
04576       restore_conference(p);
04577    }
04578    if (p->subs[index].linear) {
04579       p->subs[index].f.datalen = READ_SIZE * 2;
04580    } else 
04581       p->subs[index].f.datalen = READ_SIZE;
04582 
04583    /* Handle CallerID Transmission */
04584    if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
04585       send_callerid(p);
04586    }
04587 
04588    p->subs[index].f.frametype = AST_FRAME_VOICE;
04589    p->subs[index].f.subclass = ast->rawreadformat;
04590    p->subs[index].f.samples = READ_SIZE;
04591    p->subs[index].f.mallocd = 0;
04592    p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04593    p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET/2;
04594 #if 0
04595    ast_log(LOG_DEBUG, "Read %d of voice on %s\n", p->subs[index].f.datalen, ast->name);
04596 #endif   
04597    if (p->dialing || /* Transmitting something */
04598       (index && (ast->_state != AST_STATE_UP)) || /* Three-way or callwait that isn't up */
04599       ((index == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway) /* Inactive and non-confed call-wait */
04600       ) {
04601       /* Whoops, we're still dialing, or in a state where we shouldn't transmit....
04602          don't send anything */
04603       p->subs[index].f.frametype = AST_FRAME_NULL;
04604       p->subs[index].f.subclass = 0;
04605       p->subs[index].f.samples = 0;
04606       p->subs[index].f.mallocd = 0;
04607       p->subs[index].f.offset = 0;
04608       p->subs[index].f.data = NULL;
04609       p->subs[index].f.datalen= 0;
04610    }
04611    if (p->dsp && (!p->ignoredtmf || p->callwaitcas || p->busydetect  || p->callprogress) && !index) {
04612       /* Perform busy detection. etc on the zap line */
04613       f = ast_dsp_process(ast, p->dsp, &p->subs[index].f);
04614       if (f) {
04615          if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
04616             if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
04617                /* Treat this as a "hangup" instead of a "busy" on the assumption that
04618                   a busy  */
04619                f = NULL;
04620             }
04621          } else if (f->frametype == AST_FRAME_DTMF) {
04622 #ifdef ZAPATA_PRI
04623             if (!p->proceeding && p->sig==SIG_PRI && p->pri && p->pri->overlapdial) {
04624                /* Don't accept in-band DTMF when in overlap dial mode */
04625                f->frametype = AST_FRAME_NULL;
04626                f->subclass = 0;
04627             }
04628 #endif            
04629             /* DSP clears us of being pulse */
04630             p->pulsedial = 0;
04631          }
04632       }
04633    } else 
04634       f = &p->subs[index].f; 
04635    if (f && (f->frametype == AST_FRAME_DTMF)) {
04636       ast_log(LOG_DEBUG, "DTMF digit: %c on %s\n", f->subclass, ast->name);
04637       if (p->confirmanswer) {
04638          ast_log(LOG_DEBUG, "Confirm answer on %s!\n", ast->name);
04639          /* Upon receiving a DTMF digit, consider this an answer confirmation instead
04640             of a DTMF digit */
04641          p->subs[index].f.frametype = AST_FRAME_CONTROL;
04642          p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04643          f = &p->subs[index].f;
04644          /* Reset confirmanswer so DTMF's will behave properly for the duration of the call */
04645          p->confirmanswer = 0;
04646       } else if (p->callwaitcas) {
04647          if ((f->subclass == 'A') || (f->subclass == 'D')) {
04648             ast_log(LOG_DEBUG, "Got some DTMF, but it's for the CAS\n");
04649             if (p->cidspill)
04650                free(p->cidspill);
04651             send_cwcidspill(p);
04652          }
04653          if ((f->subclass != 'm') && (f->subclass != 'u')) 
04654             p->callwaitcas = 0;
04655          p->subs[index].f.frametype = AST_FRAME_NULL;
04656          p->subs[index].f.subclass = 0;
04657          f = &p->subs[index].f;
04658       } else if (f->subclass == 'f') {
04659          /* Fax tone -- Handle and return NULL */
04660          if (!p->faxhandled) {
04661             p->faxhandled++;
04662             if (strcmp(ast->exten, "fax")) {
04663                const char *target_context = ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext;
04664 
04665                if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
04666                   if (option_verbose > 2)
04667                      ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
04668                   /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
04669                   pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
04670                   if (ast_async_goto(ast, target_context, "fax", 1))
04671                      ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
04672                } else
04673                   ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
04674             } else
04675                ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
04676          } else
04677                ast_log(LOG_DEBUG, "Fax already handled\n");
04678          zt_confmute(p, 0);
04679          p->subs[index].f.frametype = AST_FRAME_NULL;
04680          p->subs[index].f.subclass = 0;
04681          f = &p->subs[index].f;
04682       } else if (f->subclass == 'm') {
04683          /* Confmute request */
04684          zt_confmute(p, 1);
04685          p->subs[index].f.frametype = AST_FRAME_NULL;
04686          p->subs[index].f.subclass = 0;
04687          f = &p->subs[index].f;     
04688       } else if (f->subclass == 'u') {
04689          /* Unmute */
04690          zt_confmute(p, 0);
04691          p->subs[index].f.frametype = AST_FRAME_NULL;
04692          p->subs[index].f.subclass = 0;
04693          f = &p->subs[index].f;     
04694       } else
04695          zt_confmute(p, 0);
04696    }
04697 
04698    /* If we have a fake_event, trigger exception to handle it */
04699    if (p->fake_event)
04700       ast_set_flag(ast, AST_FLAG_EXCEPTION);
04701 
04702    ast_mutex_unlock(&p->lock);
04703    return f;
04704 }
04705 
04706 static int my_zt_write(struct zt_pvt *p, unsigned char *buf, int len, int index, int linear)
04707 {
04708    int sent=0;
04709    int size;
04710    int res;
04711    int fd;
04712    fd = p->subs[index].zfd;
04713    while(len) {
04714       size = len;
04715       if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
04716          size = (linear ? READ_SIZE * 2 : READ_SIZE);
04717       res = write(fd, buf, size);
04718       if (res != size) {
04719          if (option_debug)
04720             ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
04721          return sent;
04722       }
04723       len -= size;
04724       buf += size;
04725    }
04726    return sent;
04727 }
04728 
04729 static int zt_write(struct ast_channel *ast, struct ast_frame *frame)
04730 {
04731    struct zt_pvt *p = ast->tech_pvt;
04732    int res;
04733    unsigned char outbuf[4096];
04734    int index;
04735    index = zt_get_index(ast, p, 0);
04736    if (index < 0) {
04737       ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
04738       return -1;
04739    }
04740 
04741 #if 0
04742 #ifdef ZAPATA_PRI
04743    ast_mutex_lock(&p->lock);
04744    if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04745       if (p->pri->pri) {      
04746          if (!pri_grab(p, p->pri)) {
04747                pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04748                pri_rel(p->pri);
04749          } else
04750                ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04751       }
04752       p->proceeding=1;
04753    }
04754    ast_mutex_unlock(&p->lock);
04755 #endif
04756 #endif
04757    /* Write a frame of (presumably voice) data */
04758    if (frame->frametype != AST_FRAME_VOICE) {
04759       if (frame->frametype != AST_FRAME_IMAGE)
04760          ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
04761       return 0;
04762    }
04763    if ((frame->subclass != AST_FORMAT_SLINEAR) && 
04764        (frame->subclass != AST_FORMAT_ULAW) &&
04765        (frame->subclass != AST_FORMAT_ALAW)) {
04766       ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
04767       return -1;
04768    }
04769    if (p->dialing) {
04770       if (option_debug)
04771          ast_log(LOG_DEBUG, "Dropping frame since I'm still dialing on %s...\n",ast->name);
04772       return 0;
04773    }
04774    if (!p->owner) {
04775       if (option_debug)
04776          ast_log(LOG_DEBUG, "Dropping frame since there is no active owner on %s...\n",ast->name);
04777       return 0;
04778    }
04779    if (p->cidspill) {
04780       if (option_debug)
04781          ast_log(LOG_DEBUG, "Dropping frame since I've still got a callerid spill\n");
04782       return 0;
04783    }
04784    /* Return if it's not valid data */
04785    if (!frame->data || !frame->datalen)
04786       return 0;
04787    if (frame->datalen > sizeof(outbuf) * 2) {
04788       ast_log(LOG_WARNING, "Frame too large\n");
04789       return 0;
04790    }
04791 
04792    if (frame->subclass == AST_FORMAT_SLINEAR) {
04793       if (!p->subs[index].linear) {
04794          p->subs[index].linear = 1;
04795          res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04796          if (res)
04797             ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
04798       }
04799       res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 1);
04800    } else {
04801       /* x-law already */
04802       if (p->subs[index].linear) {
04803          p->subs[index].linear = 0;
04804          res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04805          if (res)
04806             ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
04807       }
04808       res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 0);
04809    }
04810    if (res < 0) {
04811       ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
04812       return -1;
04813    } 
04814    return 0;
04815 }
04816 
04817 static int zt_indicate(struct ast_channel *chan, int condition)
04818 {
04819    struct zt_pvt *p = chan->tech_pvt;
04820    int res=-1;
04821    int index;
04822    int func = ZT_FLASH;
04823    ast_mutex_lock(&p->lock);
04824    index = zt_get_index(chan, p, 0);
04825    ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
04826    if (index == SUB_REAL) {
04827       switch(condition) {
04828       case AST_CONTROL_BUSY:
04829 #ifdef ZAPATA_PRI
04830          if (p->priindication_oob && p->sig == SIG_PRI) {
04831             chan->hangupcause = AST_CAUSE_USER_BUSY;
04832             chan->_softhangup |= AST_SOFTHANGUP_DEV;
04833             res = 0;
04834          } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04835             if (p->pri->pri) {      
04836                if (!pri_grab(p, p->pri)) {
04837                   pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04838                   pri_rel(p->pri);
04839                }
04840                else
04841                   ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04842             }
04843             p->progress = 1;
04844             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
04845          } else
04846 #endif
04847             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
04848          break;
04849       case AST_CONTROL_RINGING:
04850 #ifdef ZAPATA_PRI
04851          if ((!p->alerting) && p->sig==SIG_PRI && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
04852             if (p->pri->pri) {      
04853                if (!pri_grab(p, p->pri)) {
04854                   pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04855                   pri_rel(p->pri);
04856                }
04857                else
04858                   ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04859             }
04860             p->alerting = 1;
04861          }
04862 #endif
04863          res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_RINGTONE);
04864          if (chan->_state != AST_STATE_UP) {
04865             if ((chan->_state != AST_STATE_RING) ||
04866                ((p->sig != SIG_FXSKS) &&
04867                 (p->sig != SIG_FXSLS) &&
04868                 (p->sig != SIG_FXSGS)))
04869                ast_setstate(chan, AST_STATE_RINGING);
04870          }
04871          break;
04872       case AST_CONTROL_PROCEEDING:
04873          ast_log(LOG_DEBUG,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
04874 #ifdef ZAPATA_PRI
04875          if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04876             if (p->pri->pri) {      
04877                if (!pri_grab(p, p->pri)) {
04878                   pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04879                   pri_rel(p->pri);
04880                }
04881                else
04882                   ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04883             }
04884             p->proceeding = 1;
04885          }
04886 #endif
04887          /* don't continue in ast_indicate */
04888          res = 0;
04889          break;
04890       case AST_CONTROL_PROGRESS:
04891          ast_log(LOG_DEBUG,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
04892 #ifdef ZAPATA_PRI
04893          p->digital = 0;   /* Digital-only calls isn't allows any inband progress messages */
04894          if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04895             if (p->pri->pri) {      
04896                if (!pri_grab(p, p->pri)) {
04897                   pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04898                   pri_rel(p->pri);
04899                }
04900                else
04901                   ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04902             }
04903             p->progress = 1;
04904          }
04905 #endif
04906          /* don't continue in ast_indicate */
04907          res = 0;
04908          break;
04909       case AST_CONTROL_CONGESTION:
04910          chan->hangupcause = AST_CAUSE_CONGESTION;
04911 #ifdef ZAPATA_PRI
04912          if (p->priindication_oob && p->sig == SIG_PRI) {
04913             chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
04914             chan->_softhangup |= AST_SOFTHANGUP_DEV;
04915             res = 0;
04916          } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04917             if (p->pri) {     
04918                if (!pri_grab(p, p->pri)) {
04919                   pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04920                   pri_rel(p->pri);
04921                } else
04922                   ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04923             }
04924             p->progress = 1;
04925             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
04926          } else
04927 #endif
04928             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
04929          break;
04930 #ifdef ZAPATA_PRI
04931       case AST_CONTROL_HOLD:
04932          if (p->pri) {
04933             if (!pri_grab(p, p->pri)) {
04934                res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
04935                pri_rel(p->pri);
04936             } else
04937                   ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);       
04938          }
04939          break;
04940       case AST_CONTROL_UNHOLD:
04941          if (p->pri) {
04942             if (!pri_grab(p, p->pri)) {
04943                res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
04944                pri_rel(p->pri);
04945             } else
04946                   ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);       
04947          }
04948          break;
04949 #endif
04950       case AST_CONTROL_RADIO_KEY:
04951          if (p->radio) 
04952              res =  zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
04953          res = 0;
04954          break;
04955       case AST_CONTROL_RADIO_UNKEY:
04956          if (p->radio)
04957              res =  zt_set_hook(p->subs[index].zfd, ZT_RINGOFF);
04958          res = 0;
04959          break;
04960       case AST_CONTROL_FLASH:
04961          /* flash hookswitch */
04962          if (ISTRUNK(p) && (p->sig != SIG_PRI)) {
04963             /* Clear out the dial buffer */
04964             p->dop.dialstr[0] = '\0';
04965             if ((ioctl(p->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
04966                ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n", 
04967                   chan->name, strerror(errno));
04968             } else
04969                res = 0;
04970          } else
04971             res = 0;
04972          break;
04973       case -1:
04974          res = tone_zone_play_tone(p->subs[index].zfd, -1);
04975          break;
04976       }
04977    } else
04978       res = 0;
04979    ast_mutex_unlock(&p->lock);
04980    return res;
04981 }
04982 
04983 static struct ast_channel *zt_new(struct zt_pvt *i, int state, int startpbx, int index, int law, int transfercapability)
04984 {
04985    struct ast_channel *tmp;
04986    int deflaw;
04987    int res;
04988    int x,y;
04989    int features;
04990    ZT_PARAMS ps;
04991    if (i->subs[index].owner) {
04992       ast_log(LOG_WARNING, "Channel %d already has a %s call\n", i->channel,subnames[index]);
04993       return NULL;
04994    }
04995    tmp = ast_channel_alloc(0);
04996    if (tmp) {
04997       tmp->tech = &zap_tech;
04998       ps.channo = i->channel;
04999       res = ioctl(i->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps);
05000       if (res) {
05001          ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW\n");
05002          ps.curlaw = ZT_LAW_MULAW;
05003       }
05004       if (ps.curlaw == ZT_LAW_ALAW)
05005          deflaw = AST_FORMAT_ALAW;
05006       else
05007          deflaw = AST_FORMAT_ULAW;
05008       if (law) {
05009          if (law == ZT_LAW_ALAW)
05010             deflaw = AST_FORMAT_ALAW;
05011          else
05012             deflaw = AST_FORMAT_ULAW;
05013       }
05014       y = 1;
05015       do {
05016 #ifdef ZAPATA_PRI
05017          if (i->bearer || (i->pri && (i->sig == SIG_FXSKS)))
05018             snprintf(tmp->name, sizeof(tmp->name), "Zap/%d:%d-%d", i->pri->trunkgroup, i->channel, y);
05019          else
05020 #endif
05021          if (i->channel == CHAN_PSEUDO)
05022             snprintf(tmp->name, sizeof(tmp->name), "Zap/pseudo-%d", rand());
05023          else  
05024             snprintf(tmp->name, sizeof(tmp->name), "Zap/%d-%d", i->channel, y);
05025          for (x=0;x<3;x++) {
05026             if ((index != x) && i->subs[x].owner && !strcasecmp(tmp->name, i->subs[x].owner->name))
05027                break;
05028          }
05029          y++;
05030       } while (x < 3);
05031       tmp->type = type;
05032       tmp->fds[0] = i->subs[index].zfd;
05033       tmp->nativeformats = AST_FORMAT_SLINEAR | deflaw;
05034       /* Start out assuming ulaw since it's smaller :) */
05035       tmp->rawreadformat = deflaw;
05036       tmp->readformat = deflaw;
05037       tmp->rawwriteformat = deflaw;
05038       tmp->writeformat = deflaw;
05039       i->subs[index].linear = 0;
05040       zt_setlinear(i->subs[index].zfd, i->subs[index].linear);
05041       features = 0;
05042       if (i->busydetect && CANBUSYDETECT(i)) {
05043          features |= DSP_FEATURE_BUSY_DETECT;
05044       }
05045       if ((i->callprogress & 1) && CANPROGRESSDETECT(i)) {
05046          features |= DSP_FEATURE_CALL_PROGRESS;
05047       }
05048       if ((!i->outgoing && (i->callprogress & 4)) || 
05049           (i->outgoing && (i->callprogress & 2))) {
05050          features |= DSP_FEATURE_FAX_DETECT;
05051       }
05052 #ifdef ZT_TONEDETECT
05053       x = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
05054       if (ioctl(i->subs[index].zfd, ZT_TONEDETECT, &x)) {
05055 #endif      
05056          i->hardwaredtmf = 0;
05057          features |= DSP_FEATURE_DTMF_DETECT;
05058 #ifdef ZT_TONEDETECT
05059       } else if (NEED_MFDETECT(i)) {
05060          i->hardwaredtmf = 1;
05061          features |= DSP_FEATURE_DTMF_DETECT;
05062       }
05063 #endif
05064       if (features) {
05065          if (i->dsp) {
05066             ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", tmp->name);
05067          } else {
05068             if (i->channel != CHAN_PSEUDO)
05069                i->dsp = ast_dsp_new();
05070             else
05071                i->dsp = NULL;
05072             if (i->dsp) {
05073                i->dsp_features = features & ~DSP_PROGRESS_TALK;
05074 #ifdef ZAPATA_PRI
05075                /* We cannot do progress detection until receives PROGRESS message */
05076                if (i->outgoing && (i->sig == SIG_PRI)) {
05077                   /* Remember requested DSP features, don't treat
05078                      talking as ANSWER */
05079                   features = 0;
05080                }
05081 #endif
05082                ast_dsp_set_features(i->dsp, features);
05083                ast_dsp_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax);
05084                if (!ast_strlen_zero(progzone))
05085                   ast_dsp_set_call_progress_zone(i->dsp, progzone);
05086                if (i->busydetect && CANBUSYDETECT(i)) {
05087                   ast_dsp_set_busy_count(i->dsp, i->busycount);
05088                   ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength);
05089                }
05090             }
05091          }
05092       }
05093       
05094       if (state == AST_STATE_RING)
05095          tmp->rings = 1;
05096       tmp->tech_pvt = i;
05097       if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) {
05098          /* Only FXO signalled stuff can be picked up */
05099          tmp->callgroup = i->callgroup;
05100          tmp->pickupgroup = i->pickupgroup;
05101       }
05102       if (!ast_strlen_zero(i->language))
05103          ast_copy_string(tmp->language, i->language, sizeof(tmp->language));
05104       if (!ast_strlen_zero(i->musicclass))
05105          ast_copy_string(tmp->musicclass, i->musicclass, sizeof(tmp->musicclass));
05106       if (!i->owner)
05107          i->owner = tmp;
05108       if (!ast_strlen_zero(i->accountcode))
05109          ast_copy_string(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode));
05110       if (i->amaflags)
05111          tmp->amaflags = i->amaflags;
05112       i->subs[index].owner = tmp;
05113       ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
05114       /* Copy call forward info */
05115       ast_copy_string(tmp->call_forward, i->call_forward, sizeof(tmp->call_forward));
05116       /* If we've been told "no ADSI" then enforce it */
05117       if (!i->adsi)
05118          tmp->adsicpe = AST_ADSI_UNAVAILABLE;
05119       if (!ast_strlen_zero(i->exten))
05120          ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
05121       if (!ast_strlen_zero(i->rdnis))
05122          tmp->cid.cid_rdnis = strdup(i->rdnis);
05123       if (!ast_strlen_zero(i->dnid))
05124          tmp->cid.cid_dnid = strdup(i->dnid);
05125 
05126 #ifdef PRI_ANI
05127       if (!ast_strlen_zero(i->cid_num))
05128          tmp->cid.cid_num = strdup(i->cid_num);
05129       if (!ast_strlen_zero(i->cid_name))
05130          tmp->cid.cid_name = strdup(i->cid_name);
05131       if (!ast_strlen_zero(i->cid_ani))
05132          tmp->cid.cid_ani = strdup(i->cid_num);
05133       else if (!ast_strlen_zero(i->cid_num)) 
05134          tmp->cid.cid_ani = strdup(i->cid_num);
05135 #else
05136       if (!ast_strlen_zero(i->cid_num)) {
05137          tmp->cid.cid_num = strdup(i->cid_num);
05138          tmp->cid.cid_ani = strdup(i->cid_num);
05139       }
05140       if (!ast_strlen_zero(i->cid_name))
05141          tmp->cid.cid_name = strdup(i->cid_name);
05142 #endif
05143       tmp->cid.cid_pres = i->callingpres;
05144       tmp->cid.cid_ton = i->cid_ton;
05145 #ifdef ZAPATA_PRI
05146       tmp->transfercapability = transfercapability;
05147       pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
05148       if (transfercapability & PRI_TRANS_CAP_DIGITAL) {
05149          i->digital = 1;
05150       }
05151       /* Assume calls are not idle calls unless we're told differently */
05152       i->isidlecall = 0;
05153       i->alreadyhungup = 0;
05154 #endif
05155       /* clear the fake event in case we posted one before we had ast_channel */
05156       i->fake_event = 0;
05157       /* Assure there is no confmute on this channel */
05158       zt_confmute(i, 0);
05159       ast_setstate(tmp, state);
05160       ast_mutex_lock(&usecnt_lock);
05161       usecnt++;
05162       ast_mutex_unlock(&usecnt_lock);
05163       ast_update_use_count();
05164       if (startpbx) {
05165          if (ast_pbx_start(tmp)) {
05166             ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
05167             ast_hangup(tmp);
05168             tmp = NULL;
05169          }
05170       }
05171    } else
05172       ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
05173    return tmp;
05174 }
05175 
05176 
05177 static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
05178 {
05179    char c;
05180 
05181    *str = 0; /* start with empty output buffer */
05182    for (;;)
05183    {
05184       /* Wait for the first digit (up to specified ms). */
05185       c = ast_waitfordigit(chan, ms);
05186       /* if timeout, hangup or error, return as such */
05187       if (c < 1)
05188          return c;
05189       *str++ = c;
05190       *str = 0;
05191       if (strchr(term, c))
05192          return 1;
05193    }
05194 }
05195 
05196 static int zt_wink(struct zt_pvt *p, int index)
05197 {
05198    int j;
05199    zt_set_hook(p->subs[index].zfd, ZT_WINK);
05200    for(;;)
05201    {
05202          /* set bits of interest */
05203       j = ZT_IOMUX_SIGEVENT;
05204           /* wait for some happening */
05205       if (ioctl(p->subs[index].zfd,ZT_IOMUX,&j) == -1) return(-1);
05206          /* exit loop if we have it */
05207       if (j & ZT_IOMUX_SIGEVENT) break;
05208    }
05209      /* get the event info */
05210    if (ioctl(p->subs[index].zfd,ZT_GETEVENT,&j) == -1) return(-1);
05211    return 0;
05212 }
05213 
05214 static void *ss_thread(void *data)
05215 {
05216    struct ast_channel *chan = data;
05217    struct zt_pvt *p = chan->tech_pvt;
05218    char exten[AST_MAX_EXTENSION]="";
05219    char exten2[AST_MAX_EXTENSION]="";
05220    unsigned char buf[256];
05221    char dtmfcid[300];
05222    char dtmfbuf[300];
05223    struct callerid_state *cs;
05224    char *name=NULL, *number=NULL;
05225    int distMatches;
05226    int curRingData[3];
05227    int receivedRingT;
05228    int counter1;
05229    int counter;
05230    int samples = 0;
05231 
05232    int flags;
05233    int i;
05234    int timeout;
05235    int getforward=0;
05236    char *s1, *s2;
05237    int len = 0;
05238    int res;
05239    int index;
05240    if (option_verbose > 2) 
05241       ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name);
05242    index = zt_get_index(chan, p, 1);
05243    if (index < 0) {
05244       ast_log(LOG_WARNING, "Huh?\n");
05245       ast_hangup(chan);
05246       return NULL;
05247    }
05248    if (p->dsp)
05249       ast_dsp_digitreset(p->dsp);
05250    switch(p->sig) {
05251 #ifdef ZAPATA_PRI
05252    case SIG_PRI:
05253       /* Now loop looking for an extension */
05254       ast_copy_string(exten, p->exten, sizeof(exten));
05255       len = strlen(exten);
05256       res = 0;
05257       while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05258          if (len && !ast_ignore_pattern(chan->context, exten))
05259             tone_zone_play_tone(p->subs[index].zfd, -1);
05260          else
05261             tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05262          if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
05263             timeout = matchdigittimeout;
05264          else
05265             timeout = gendigittimeout;
05266          res = ast_waitfordigit(chan, timeout);
05267          if (res < 0) {
05268             ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05269             ast_hangup(chan);
05270             return NULL;
05271          } else if (res) {
05272             exten[len++] = res;
05273             exten[len] = '\0';
05274          } else
05275             break;
05276       }
05277       /* if no extension was received ('unspecified') on overlap call, use the 's' extension */
05278       if (ast_strlen_zero(exten)) {
05279          if (option_verbose > 2)
05280             ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n");
05281          exten[0] = 's';
05282          exten[1] = '\0';
05283       }
05284       tone_zone_play_tone(p->subs[index].zfd, -1);
05285       if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
05286          /* Start the real PBX */
05287          ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05288          if (p->dsp) ast_dsp_digitreset(p->dsp);
05289          zt_enable_ec(p);
05290          ast_setstate(chan, AST_STATE_RING);
05291          res = ast_pbx_run(chan);
05292          if (res) {
05293             ast_log(LOG_WARNING, "PBX exited non-zero!\n");
05294          }
05295       } else {
05296          ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
05297          chan->hangupcause = AST_CAUSE_UNALLOCATED;
05298          ast_hangup(chan);
05299          p->exten[0] = '\0';
05300          /* Since we send release complete here, we won't get one */
05301          p->call = NULL;
05302       }
05303       return NULL;
05304       break;
05305 #endif
05306    case SIG_FEATD:
05307    case SIG_FEATDMF:
05308    case SIG_E911:
05309    case SIG_FEATB:
05310    case SIG_EMWINK:
05311    case SIG_SF_FEATD:
05312    case SIG_SF_FEATDMF:
05313    case SIG_SF_FEATB:
05314    case SIG_SFWINK:
05315       if (zt_wink(p, index))  
05316          return NULL;
05317       /* Fall through */
05318    case SIG_EM:
05319    case SIG_EM_E1:
05320    case SIG_SF:
05321       res = tone_zone_play_tone(p->subs[index].zfd, -1);
05322       if (p->dsp)
05323          ast_dsp_digitreset(p->dsp);
05324       /* set digit mode appropriately */
05325       if (p->dsp) {
05326          if (NEED_MFDETECT(p))
05327             ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax); 
05328          else 
05329             ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05330       }
05331       memset(dtmfbuf, 0, sizeof(dtmfbuf));
05332       /* Wait for the first digit only if immediate=no */
05333       if (!p->immediate)
05334          /* Wait for the first digit (up to 5 seconds). */
05335          res = ast_waitfordigit(chan, 5000);
05336       else res = 0;
05337       if (res > 0) {
05338          /* save first char */
05339          dtmfbuf[0] = res;
05340          switch(p->sig) {
05341          case SIG_FEATD:
05342          case SIG_SF_FEATD:
05343             res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05344             if (res > 0)
05345                res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05346             if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05347             break;
05348          case SIG_FEATDMF:
05349          case SIG_E911:
05350          case SIG_SF_FEATDMF:
05351             res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05352             if (res > 0) {
05353                /* if E911, take off hook */
05354                if (p->sig == SIG_E911)
05355                   zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
05356                res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
05357             }
05358             if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05359             break;
05360          case SIG_FEATB:
05361          case SIG_SF_FEATB:
05362             res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05363             if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05364             break;
05365          case SIG_EMWINK:
05366             /* if we received a '*', we are actually receiving Feature Group D
05367                dial syntax, so use that mode; otherwise, fall through to normal
05368                mode
05369             */
05370             if (res == '*') {
05371                res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05372                if (res > 0)
05373                   res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05374                if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05375                break;
05376             }
05377          default:
05378             /* If we got the first digit, get the rest */
05379             len = 1;
05380             dtmfbuf[len] = '\0';
05381             while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05382                if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05383                   timeout = matchdigittimeout;
05384                } else {
05385                   timeout = gendigittimeout;
05386                }
05387                res = ast_waitfordigit(chan, timeout);
05388                if (res < 0) {
05389                   ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05390                   ast_hangup(chan);
05391                   return NULL;
05392                } else if (res) {
05393                   dtmfbuf[len++] = res;
05394                   dtmfbuf[len] = '\0';
05395                } else {
05396                   break;
05397                }
05398             }
05399             break;
05400          }
05401       }
05402       if (res == -1) {
05403          ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
05404          ast_hangup(chan);
05405          return NULL;
05406       } else if (res < 0) {
05407          ast_log(LOG_DEBUG, "Got hung up before digits finished\n");
05408          ast_hangup(chan);
05409          return NULL;
05410       }
05411       ast_copy_string(exten, dtmfbuf, sizeof(exten));
05412       if (ast_strlen_zero(exten))
05413          ast_copy_string(exten, "s", sizeof(exten));
05414       if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
05415          /* Look for Feature Group D on all E&M Wink and Feature Group D trunks */
05416          if (exten[0] == '*') {
05417             char *stringp=NULL;
05418             ast_copy_string(exten2, exten, sizeof(exten2));
05419             /* Parse out extension and callerid */
05420             stringp=exten2 +1;
05421             s1 = strsep(&stringp, "*");
05422             s2 = strsep(&stringp, "*");
05423             if (s2) {
05424                if (!ast_strlen_zero(p->cid_num))
05425                   ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05426                else
05427                   ast_set_callerid(chan, s1, NULL, s1);
05428                ast_copy_string(exten, s2, sizeof(exten));
05429             } else
05430                ast_copy_string(exten, s1, sizeof(exten));
05431          } else if (p->sig == SIG_FEATD)
05432             ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
05433       }
05434       if (p->sig == SIG_FEATDMF) {
05435          if (exten[0] == '*') {
05436             char *stringp=NULL;
05437             ast_copy_string(exten2, exten, sizeof(exten2));
05438             /* Parse out extension and callerid */
05439             stringp=exten2 +1;
05440             s1 = strsep(&stringp, "#");
05441             s2 = strsep(&stringp, "#");
05442             if (s2) {
05443                if (!ast_strlen_zero(p->cid_num))
05444                   ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05445                else
05446                   if(*(s1 + 2))
05447                      ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
05448                ast_copy_string(exten, s2 + 1, sizeof(exten));
05449             } else
05450                ast_copy_string(exten, s1 + 2, sizeof(exten));
05451          } else
05452             ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d.  Assuming E&M Wink instead\n", p->channel);
05453       }
05454       if (p->sig == SIG_E911) {
05455          if (exten[0] == '*') {
05456             char *stringp=NULL;
05457             ast_copy_string(exten2, exten, sizeof(exten2));
05458             /* Parse out extension and callerid */
05459             stringp=exten2 +1;
05460             s1 = strsep(&stringp, "#");
05461             s2 = strsep(&stringp, "#");
05462             if (s2 && (*(s2 + 1) == '0')) {
05463                if(*(s2 + 2))
05464                   ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
05465             }
05466             if (s1)  ast_copy_string(exten, s1, sizeof(exten));
05467             else ast_copy_string(exten, "911", sizeof(exten));
05468          } else
05469             ast_log(LOG_WARNING, "Got a non-E911 input on channel %d.  Assuming E&M Wink instead\n", p->channel);
05470       }
05471       if (p->sig == SIG_FEATB) {
05472          if (exten[0] == '*') {
05473             char *stringp=NULL;
05474             ast_copy_string(exten2, exten, sizeof(exten2));
05475             /* Parse out extension and callerid */
05476             stringp=exten2 +1;
05477             s1 = strsep(&stringp, "#");
05478             ast_copy_string(exten, exten2 + 1, sizeof(exten));
05479          } else
05480             ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d.  Assuming E&M Wink instead\n", p->channel);
05481       }
05482       if (p->sig == SIG_FEATDMF) {
05483          zt_wink(p, index);
05484       }
05485       zt_enable_ec(p);
05486       if (NEED_MFDETECT(p)) {
05487          if (p->dsp) {
05488             if (!p->hardwaredtmf)
05489                ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax); 
05490             else {
05491                ast_dsp_free(p->dsp);
05492                p->dsp = NULL;
05493             }
05494          }
05495       }
05496 
05497       if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
05498          ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05499          if (p->dsp) ast_dsp_digitreset(p->dsp);
05500          res = ast_pbx_run(chan);
05501          if (res) {
05502             ast_log(LOG_WARNING, "PBX exited non-zero\n");
05503             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05504          }
05505          return NULL;
05506       } else {
05507          if (option_verbose > 2)
05508             ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
05509          sleep(2);
05510          res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_INFO);
05511          if (res < 0)
05512             ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
05513          else
05514             sleep(1);
05515          res = ast_streamfile(chan, "ss-noservice", chan->language);
05516          if (res >= 0)
05517             ast_waitstream(chan, "");
05518          res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05519          ast_hangup(chan);
05520          return NULL;
05521       }
05522       break;
05523    case SIG_FXOLS:
05524    case SIG_FXOGS:
05525    case SIG_FXOKS:
05526       /* Read the first digit */
05527       timeout = firstdigittimeout;
05528       /* If starting a threeway call, never timeout on the first digit so someone
05529          can use flash-hook as a "hold" feature */
05530       if (p->subs[SUB_THREEWAY].owner) 
05531          timeout = 999999;
05532       while(len < AST_MAX_EXTENSION-1) {
05533          /* Read digit unless it's supposed to be immediate, in which case the
05534             only answer is 's' */
05535          if (p->immediate) 
05536             res = 's';
05537          else
05538             res = ast_waitfordigit(chan, timeout);
05539          timeout = 0;
05540          if (res < 0) {
05541             ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05542             res = tone_zone_play_tone(p->subs[index].zfd, -1);
05543             ast_hangup(chan);
05544             return NULL;
05545          } else if (res)  {
05546             exten[len++]=res;
05547             exten[len] = '\0';
05548          }
05549          if (!ast_ignore_pattern(chan->context, exten))
05550             tone_zone_play_tone(p->subs[index].zfd, -1);
05551          else
05552             tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05553          if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
05554             if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05555                if (getforward) {
05556                   /* Record this as the forwarding extension */
05557                   ast_copy_string(p->call_forward, exten, sizeof(p->call_forward)); 
05558                   if (option_verbose > 2)
05559                      ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
05560                   res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05561                   if (res)
05562                      break;
05563                   usleep(500000);
05564                   res = tone_zone_play_tone(p->subs[index].zfd, -1);
05565                   sleep(1);
05566                   memset(exten, 0, sizeof(exten));
05567                   res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05568                   len = 0;
05569                   getforward = 0;
05570                } else  {
05571                   res = tone_zone_play_tone(p->subs[index].zfd, -1);
05572                   ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05573                   if (!ast_strlen_zero(p->cid_num)) {
05574                      if (!p->hidecallerid)
05575                         ast_set_callerid(chan, p->cid_num, NULL, p->cid_num); 
05576                      else
05577                         ast_set_callerid(chan, NULL, NULL, p->cid_num); 
05578                   }
05579                   if (!ast_strlen_zero(p->cid_name)) {
05580                      if (!p->hidecallerid)
05581                         ast_set_callerid(chan, NULL, p->cid_name, NULL);
05582                   }
05583                   ast_setstate(chan, AST_STATE_RING);
05584                   zt_enable_ec(p);
05585                   res = ast_pbx_run(chan);
05586                   if (res) {
05587                      ast_log(LOG_WARNING, "PBX exited non-zero\n");
05588                      res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05589                   }
05590                   return NULL;
05591                }
05592             } else {
05593                /* It's a match, but they just typed a digit, and there is an ambiguous match,
05594                   so just set the timeout to matchdigittimeout and wait some more */
05595                timeout = matchdigittimeout;
05596             }
05597          } else if (res == 0) {
05598             ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n");
05599             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05600             zt_wait_event(p->subs[index].zfd);
05601             ast_hangup(chan);
05602             return NULL;
05603          } else if (p->callwaiting && !strcmp(exten, "*70")) {
05604             if (option_verbose > 2) 
05605                ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
05606             /* Disable call waiting if enabled */
05607             p->callwaiting = 0;
05608             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05609             if (res) {
05610                ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", 
05611                   chan->name, strerror(errno));
05612             }
05613             len = 0;
05614             ioctl(p->subs[index].zfd,ZT_CONFDIAG,&len);
05615             memset(exten, 0, sizeof(exten));
05616             timeout = firstdigittimeout;
05617                
05618          } else if (!strcmp(exten,ast_pickup_ext())) {
05619             /* Scan all channels and see if any there
05620              * ringing channqels with that have call groups
05621              * that equal this channels pickup group  
05622              */
05623             if (index == SUB_REAL) {
05624                /* Switch us from Third call to Call Wait */
05625                if (p->subs[SUB_THREEWAY].owner) {
05626                   /* If you make a threeway call and the *8# a call, it should actually 
05627                      look like a callwait */
05628                   alloc_sub(p, SUB_CALLWAIT);   
05629                   swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
05630                   unalloc_sub(p, SUB_THREEWAY);
05631                }
05632                zt_enable_ec(p);
05633                if (ast_pickup_call(chan)) {
05634                   ast_log(LOG_DEBUG, "No call pickup possible...\n");
05635                   res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05636                   zt_wait_event(p->subs[index].zfd);
05637                }
05638                ast_hangup(chan);
05639                return NULL;
05640             } else {
05641                ast_log(LOG_WARNING, "Huh?  Got *8# on call not on real\n");
05642                ast_hangup(chan);
05643                return NULL;
05644             }
05645             
05646          } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
05647             if (option_verbose > 2) 
05648                ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
05649             /* Disable Caller*ID if enabled */
05650             p->hidecallerid = 1;
05651             if (chan->cid.cid_num)
05652                free(chan->cid.cid_num);
05653             chan->cid.cid_num = NULL;
05654             if (chan->cid.cid_name)
05655                free(chan->cid.cid_name);
05656             chan->cid.cid_name = NULL;
05657             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05658             if (res) {
05659                ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", 
05660                   chan->name, strerror(errno));
05661             }
05662             len = 0;
05663             memset(exten, 0, sizeof(exten));
05664             timeout = firstdigittimeout;
05665          } else if (p->callreturn && !strcmp(exten, "*69")) {
05666             res = 0;
05667             if (!ast_strlen_zero(p->lastcid_num)) {
05668                res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
05669             }
05670             if (!res)
05671                res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05672             break;
05673          } else if (!strcmp(exten, "*78")) {
05674             /* Do not disturb */
05675             if (option_verbose > 2) {
05676                ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel);
05677             }
05678             manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05679                      "Channel: Zap/%d\r\n"
05680                      "Status: enabled\r\n", p->channel);
05681             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05682             p->dnd = 1;
05683             getforward = 0;
05684             memset(exten, 0, sizeof(exten));
05685             len = 0;
05686          } else if (!strcmp(exten, "*79")) {
05687             /* Do not disturb */
05688             if (option_verbose > 2)
05689                ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel);
05690             manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05691                         "Channel: Zap/%d\r\n"
05692                         "Status: disabled\r\n", p->channel);
05693             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05694             p->dnd = 0;
05695             getforward = 0;
05696             memset(exten, 0, sizeof(exten));
05697             len = 0;
05698          } else if (p->cancallforward && !strcmp(exten, "*72")) {
05699             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05700             getforward = 1;
05701             memset(exten, 0, sizeof(exten));
05702             len = 0;
05703          } else if (p->cancallforward && !strcmp(exten, "*73")) {
05704             if (option_verbose > 2)
05705                ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel);
05706             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05707             memset(p->call_forward, 0, sizeof(p->call_forward));
05708             getforward = 0;
05709             memset(exten, 0, sizeof(exten));
05710             len = 0;
05711          } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) && 
05712                   p->subs[SUB_THREEWAY].owner &&
05713                   ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
05714             /* This is a three way call, the main call being a real channel, 
05715                and we're parking the first call. */
05716             ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL);
05717             if (option_verbose > 2)
05718                ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
05719             break;
05720          } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
05721             if (option_verbose > 2)
05722                ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num);
05723             res = ast_db_put("blacklist", p->lastcid_num, "1");
05724             if (!res) {
05725                res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05726                memset(exten, 0, sizeof(exten));
05727                len = 0;
05728             }
05729          } else if (p->hidecallerid && !strcmp(exten, "*82")) {
05730             if (option_verbose > 2) 
05731                ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
05732             /* Enable Caller*ID if enabled */
05733             p->hidecallerid = 0;
05734             if (chan->cid.cid_num)
05735                free(chan->cid.cid_num);
05736             chan->cid.cid_num = NULL;
05737             if (chan->cid.cid_name)
05738                free(chan->cid.cid_name);
05739             chan->cid.cid_name = NULL;
05740             ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
05741             res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05742             if (res) {
05743                ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n", 
05744                   chan->name, strerror(errno));
05745             }
05746             len = 0;
05747             memset(exten, 0, sizeof(exten));
05748             timeout = firstdigittimeout;
05749          } else if (!strcmp(exten, "*0")) {
05750             struct ast_channel *nbridge = 
05751                p->subs[SUB_THREEWAY].owner;
05752             struct zt_pvt *pbridge = NULL;
05753               /* set up the private struct of the bridged one, if any */
05754             if (nbridge && ast_bridged_channel(nbridge)) 
05755                pbridge = ast_bridged_channel(nbridge)->tech_pvt;
05756             if (nbridge && pbridge && 
05757                 (!strcmp(nbridge->type,"Zap")) && 
05758                (!strcmp(ast_bridged_channel(nbridge)->type, "Zap")) &&
05759                 ISTRUNK(pbridge)) {
05760                int func = ZT_FLASH;
05761                /* Clear out the dial buffer */
05762                p->dop.dialstr[0] = '\0';
05763                /* flash hookswitch */
05764                if ((ioctl(pbridge->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
05765                   ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n", 
05766                      nbridge->name, strerror(errno));
05767                }
05768                swap_subs(p, SUB_REAL, SUB_THREEWAY);
05769                unalloc_sub(p, SUB_THREEWAY);
05770                p->owner = p->subs[SUB_REAL].owner;
05771                if (ast_bridged_channel(p->subs[SUB_REAL].owner))
05772                   ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
05773                ast_hangup(chan);
05774                return NULL;
05775             } else {
05776                tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05777                zt_wait_event(p->subs[index].zfd);
05778                tone_zone_play_tone(p->subs[index].zfd, -1);
05779                swap_subs(p, SUB_REAL, SUB_THREEWAY);
05780                unalloc_sub(p, SUB_THREEWAY);
05781                p->owner = p->subs[SUB_REAL].owner;
05782                ast_hangup(chan);
05783                return NULL;
05784             }              
05785          } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
05786                      ((exten[0] != '*') || (strlen(exten) > 2))) {
05787             if (option_debug)
05788                ast_log(LOG_DEBUG, "Can't match %s from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "<Unknown Caller>", chan->context);
05789             break;
05790          }
05791          if (!timeout)
05792             timeout = gendigittimeout;
05793          if (len && !ast_ignore_pattern(chan->context, exten))
05794             tone_zone_play_tone(p->subs[index].zfd, -1);
05795       }
05796       break;
05797    case SIG_FXSLS:
05798    case SIG_FXSGS:
05799    case SIG_FXSKS:
05800 #ifdef ZAPATA_PRI
05801       if (p->pri) {
05802          /* This is a GR-303 trunk actually.  Wait for the first ring... */
05803          struct ast_frame *f;
05804          int res;
05805          time_t start;
05806 
05807          time(&start);
05808          ast_setstate(chan, AST_STATE_RING);
05809          while(time(NULL) < start + 3) {
05810             res = ast_waitfor(chan, 1000);
05811             if (res) {
05812                f = ast_read(chan);
05813                if (!f) {
05814                   ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
05815                   ast_hangup(chan);
05816                   return NULL;
05817                } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
05818                   res = 1;
05819                } else
05820                   res = 0;
05821                ast_frfree(f);
05822                if (res) {
05823                   ast_log(LOG_DEBUG, "Got ring!\n");
05824                   res = 0;
05825                   break;
05826                }
05827             }
05828          }
05829       }
05830 #endif
05831       /* If we're using an X100P in the UK, caller ID needs to be extracted from
05832        * the history buffer */
05833       if (p->use_callerid && p->cid_start == CID_START_USEHIST) {
05834          ast_log(LOG_DEBUG,"Using history buffer to extract UK caller ID\n");
05835          cs = callerid_new(cid_signalling);
05836          if (cs) {
05837             unsigned char cidbuf[16384];
05838             res=0;
05839 
05840             res = zt_get_history(p->subs[index].zfd,cidbuf,sizeof(cidbuf));
05841             if(res<0) {
05842                ast_log(LOG_ERROR,"zt_get_history failed: %s\n", strerror(errno));
05843             } else {
05844                res=callerid_feed(cs,cidbuf,sizeof(cidbuf),AST_LAW(p));
05845                if (res < 0) {
05846                   ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
05847                }
05848             }
05849 
05850             if(res==1) {
05851                callerid_get(cs, &name, &number, &flags);
05852                if (option_debug)
05853                   ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
05854             }
05855          }
05856          if (p->usedistinctiveringdetection == 1) {
05857 #if 1
05858             bump_gains(p);
05859 #endif            
05860             len = 0;
05861             distMatches = 0;
05862             /* Clear the current ring data array so we dont have old data in it. */
05863             for (receivedRingT=0; receivedRingT < 3; receivedRingT++) {
05864                curRingData[receivedRingT] = 0;
05865             }
05866             receivedRingT = 0;
05867             counter = 0;
05868             counter1 = 0;
05869             /* Check to see if context is what it should be, if not set to be. */
05870             if (strcmp(p->context,p->defcontext) != 0) {
05871                strncpy(p->context, p->defcontext, sizeof(p->context)-1);
05872                strncpy(chan->context,p->defcontext,sizeof(chan->context)-1);
05873             }
05874 
05875             for(;;) {   
05876                i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
05877                if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i)))  {
05878                   ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
05879                   callerid_free(cs);
05880                   ast_hangup(chan);
05881                   return NULL;
05882                }
05883                if (i & ZT_IOMUX_SIGEVENT) {
05884                   res = zt_get_event(p->subs[index].zfd);
05885                   ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
05886                   res = 0;
05887                   /* Let us detect distinctive ring */
05888 
05889                   curRingData[receivedRingT] = p->ringt;
05890 
05891                   if (p->ringt < ringt_base/2)
05892                      break;
05893                   ++receivedRingT; /* Increment the ringT counter so we can match it against
05894                         values in zapata.conf for distinctive ring */
05895                } else if (i & ZT_IOMUX_READ) {
05896                   res = read(p->subs[index].zfd, buf, sizeof(buf));
05897                   if (res < 0) {
05898                      if (errno != ELAST) {
05899                         ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
05900                         callerid_free(cs);
05901                         ast_hangup(chan);
05902                         return NULL;
05903                      }
05904                      break;
05905                   }
05906                   if (p->ringt) 
05907                      p->ringt--;
05908                   if (p->ringt == 1) {
05909                      res = -1;
05910                      break;
05911                   }
05912                }
05913             }
05914             if(option_verbose > 2)
05915                /* this only shows up if you have n of the dring patterns filled in */
05916                ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
05917 
05918             for (counter=0; counter < 3; counter++) {
05919                /* Check to see if the rings we received match any of the ones in zapata.conf for this
05920                channel */
05921                distMatches = 0;
05922                for (counter1=0; counter1 < 3; counter1++) {
05923                   if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
05924                   (p->drings.ringnum[counter].ring[counter1]-10)) {
05925                      distMatches++;
05926                   }
05927                }
05928                if (distMatches == 3) {
05929                   /* The ring matches, set the context to whatever is for distinctive ring.. */
05930                   strncpy(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context)-1);
05931                   strncpy(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context)-1);
05932                   if(option_verbose > 2)
05933                      ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
05934                   break;
05935                }
05936             }
05937          }
05938          /* Restore linear mode (if appropriate) for Caller*ID processing */
05939          zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
05940 #if 1
05941          restore_gains(p);
05942 #endif   
05943       }
05944 
05945       /* If we want caller id, we're in a prering state due to a polarity reversal
05946        * and we're set to use a polarity reversal to trigger the start of caller id,
05947        * grab the caller id and wait for ringing to start... */
05948       if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) {
05949          /* If set to use DTMF CID signalling, listen for DTMF */
05950          if (p->cid_signalling == CID_SIG_DTMF) {
05951             int i = 0;
05952             cs = NULL;
05953             ast_log(LOG_DEBUG, "Receiving DTMF cid on "
05954                "channel %s\n", chan->name);
05955             zt_setlinear(p->subs[index].zfd, 0);
05956             res = 2000;
05957             for (;;) {
05958                struct ast_frame *f;
05959                res = ast_waitfor(chan, res);
05960                if (res <= 0) {
05961                   ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
05962                      "Exiting simple switch\n");
05963                   ast_hangup(chan);
05964                   return NULL;
05965                } 
05966                f = ast_read(chan);
05967                if (f->frametype == AST_FRAME_DTMF) {
05968                   dtmfbuf[i++] = f->subclass;
05969                   ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
05970                   res = 2000;
05971                }
05972                ast_frfree(f);
05973                if (chan->_state == AST_STATE_RING ||
05974                    chan->_state == AST_STATE_RINGING) 
05975                   break; /* Got ring */
05976             }
05977             dtmfbuf[i] = 0;
05978             zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
05979             /* Got cid and ring. */
05980             ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf);
05981             callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
05982             ast_log(LOG_DEBUG, "CID is '%s', flags %d\n", 
05983                dtmfcid, flags);
05984             /* If first byte is NULL, we have no cid */
05985             if (dtmfcid[0]) 
05986                number = dtmfcid;
05987             else
05988                number = 0;
05989          /* If set to use V23 Signalling, launch our FSK gubbins and listen for it */
05990          } else if (p->cid_signalling == CID_SIG_V23) {
05991             cs = callerid_new(p->cid_signalling);
05992             if (cs) {
05993                samples = 0;
05994 #if 1
05995                bump_gains(p);
05996 #endif            
05997                /* Take out of linear mode for Caller*ID processing */
05998                zt_setlinear(p->subs[index].zfd, 0);
05999                
06000                /* First we wait and listen for the Caller*ID */
06001                for(;;) {   
06002                   i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06003                   if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i)))  {
06004                      ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06005                      callerid_free(cs);
06006                      ast_hangup(chan);
06007                      return NULL;
06008                   }
06009                   if (i & ZT_IOMUX_SIGEVENT) {
06010                      res = zt_get_event(p->subs[index].zfd);
06011                      ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06012                      res = 0;
06013                      break;
06014                   } else if (i & ZT_IOMUX_READ) {
06015                      res = read(p->subs[index].zfd, buf, sizeof(buf));
06016                      if (res < 0) {
06017                         if (errno != ELAST) {
06018                            ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06019                            callerid_free(cs);
06020                            ast_hangup(chan);
06021                            return NULL;
06022                         }
06023                         break;
06024                      }
06025                      samples += res;
06026                      res = callerid_feed(cs, buf, res, AST_LAW(p));
06027                      if (res < 0) {
06028                         ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06029                         break;
06030                      } else if (res)
06031                         break;
06032                      else if (samples > (8000 * 10))
06033                         break;
06034                   }
06035                }
06036                if (res == 1) {
06037                   callerid_get(cs, &name, &number, &flags);
06038                   if (option_debug)
06039                      ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06040                }
06041                if (res < 0) {
06042                   ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06043                }
06044 
06045                /* Finished with Caller*ID, now wait for a ring to make sure there really is a call coming */ 
06046                res = 2000;
06047                for (;;) {
06048                   struct ast_frame *f;
06049                   res = ast_waitfor(chan, res);
06050                   if (res <= 0) {
06051                      ast_log(LOG_WARNING, "CID timed out waiting for ring. "
06052                         "Exiting simple switch\n");
06053                      ast_hangup(chan);
06054                      return NULL;
06055                   } 
06056                   f = ast_read(chan);
06057                   ast_frfree(f);
06058                   if (chan->_state == AST_STATE_RING ||
06059                       chan->_state == AST_STATE_RINGING) 
06060                      break; /* Got ring */
06061                }
06062    
06063                /* We must have a ring by now, so, if configured, lets try to listen for
06064                 * distinctive ringing */ 
06065                if (p->usedistinctiveringdetection == 1) {
06066                   len = 0;
06067                   distMatches = 0;
06068                   /* Clear the current ring data array so we dont have old data in it. */
06069                   for (receivedRingT=0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
06070                      curRingData[receivedRingT] = 0;
06071                   receivedRingT = 0;
06072                   counter = 0;
06073                   counter1 = 0;
06074                   /* Check to see if context is what it should be, if not set to be. */
06075                   if (strcmp(p->context,p->defcontext) != 0) {
06076                      ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06077                      ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06078                   }
06079       
06080                   for(;;) {   
06081                      i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06082                      if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i)))  {
06083                         ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06084                         callerid_free(cs);
06085                         ast_hangup(chan);
06086                         return NULL;
06087                      }
06088                      if (i & ZT_IOMUX_SIGEVENT) {
06089                         res = zt_get_event(p->subs[index].zfd);
06090                         ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06091                         res = 0;
06092                         /* Let us detect distinctive ring */
06093       
06094                         curRingData[receivedRingT] = p->ringt;
06095       
06096                         if (p->ringt < p->ringt_base/2)
06097                            break;
06098                         /* Increment the ringT counter so we can match it against
06099                            values in zapata.conf for distinctive ring */
06100                         if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06101                            break;
06102                      } else if (i & ZT_IOMUX_READ) {
06103                         res = read(p->subs[index].zfd, buf, sizeof(buf));
06104                         if (res < 0) {
06105                            if (errno != ELAST) {
06106                               ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06107                               callerid_free(cs);
06108                               ast_hangup(chan);
06109                               return NULL;
06110                            }
06111                            break;
06112                         }
06113                         if (p->ringt) 
06114                            p->ringt--;
06115                         if (p->ringt == 1) {
06116                            res = -1;
06117                            break;
06118                         }
06119                      }
06120                   }
06121                   if(option_verbose > 2)
06122                      /* this only shows up if you have n of the dring patterns filled in */
06123                      ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06124    
06125                   for (counter=0; counter < 3; counter++) {
06126                      /* Check to see if the rings we received match any of the ones in zapata.conf for this
06127                      channel */
06128                      distMatches = 0;
06129                      for (counter1=0; counter1 < 3; counter1++) {
06130                         if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06131                         (p->drings.ringnum[counter].ring[counter1]-10)) {
06132                            distMatches++;
06133                         }
06134                      }
06135                      if (distMatches == 3) {
06136                         /* The ring matches, set the context to whatever is for distinctive ring.. */
06137                         ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06138                         ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06139                         if(option_verbose > 2)
06140                            ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06141                         break;
06142                      }
06143                   }
06144                }
06145                /* Restore linear mode (if appropriate) for Caller*ID processing */
06146                zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06147 #if 1
06148                restore_gains(p);
06149 #endif            
06150             } else
06151                ast_log(LOG_WARNING, "Unable to get caller ID space\n");       
06152          } else {
06153             ast_log(LOG_WARNING, "Channel %s in prering "
06154                "state, but I have nothing to do. "
06155                "Terminating simple switch, should be "
06156                "restarted by the actual ring.\n", 
06157                chan->name);
06158             ast_hangup(chan);
06159             return NULL;
06160          }
06161       } else if (p->use_callerid && p->cid_start == CID_START_RING) {
06162          /* FSK Bell202 callerID */
06163          cs = callerid_new(p->cid_signalling);
06164          if (cs) {
06165 #if 1
06166             bump_gains(p);
06167 #endif            
06168             samples = 0;
06169             len = 0;
06170             distMatches = 0;
06171             /* Clear the current ring data array so we dont have old data in it. */
06172             for (receivedRingT=0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
06173                curRingData[receivedRingT] = 0;
06174             receivedRingT = 0;
06175             counter = 0;
06176             counter1 = 0;
06177             /* Check to see if context is what it should be, if not set to be. */
06178             if (strcmp(p->context,p->defcontext) != 0) {
06179                ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06180                ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06181             }
06182 
06183             /* Take out of linear mode for Caller*ID processing */
06184             zt_setlinear(p->subs[index].zfd, 0);
06185             for(;;) {   
06186                i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06187                if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i)))  {
06188                   ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06189                   callerid_free(cs);
06190                   ast_hangup(chan);
06191                   return NULL;
06192                }
06193                if (i & ZT_IOMUX_SIGEVENT) {
06194                   res = zt_get_event(p->subs[index].zfd);
06195                   ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06196                   res = 0;
06197                   /* Let us detect callerid when the telco uses distinctive ring */
06198 
06199                   curRingData[receivedRingT] = p->ringt;
06200 
06201                   if (p->ringt < p->ringt_base/2)
06202                      break;
06203                   /* Increment the ringT counter so we can match it against
06204                      values in zapata.conf for distinctive ring */
06205                   if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06206                      break;
06207                } else if (i & ZT_IOMUX_READ) {
06208                   res = read(p->subs[index].zfd, buf, sizeof(buf));
06209                   if (res < 0) {
06210                      if (errno != ELAST) {
06211                         ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06212                         callerid_free(cs);
06213                         ast_hangup(chan);
06214                         return NULL;
06215                      }
06216                      break;
06217                   }
06218                   if (p->ringt) 
06219                      p->ringt--;
06220                   if (p->ringt == 1) {
06221                      res = -1;
06222                      break;
06223                   }
06224                   samples += res;
06225                   res = callerid_feed(cs, buf, res, AST_LAW(p));
06226                   if (res < 0) {
06227                      ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06228                      break;
06229                   } else if (res)
06230                      break;
06231                   else if (samples > (8000 * 10))
06232                      break;
06233                }
06234             }
06235             if (p->usedistinctiveringdetection == 1) {
06236                if(option_verbose > 2)
06237                   /* this only shows up if you have n of the dring patterns filled in */
06238                   ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06239 
06240                for (counter=0; counter < 3; counter++) {
06241                   /* Check to see if the rings we received match any of the ones in zapata.conf for this
06242                   channel */
06243                   distMatches = 0;
06244                   for (counter1=0; counter1 < 3; counter1++) {
06245                      if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06246                      (p->drings.ringnum[counter].ring[counter1]-10)) {
06247                         distMatches++;
06248                      }
06249                   }
06250                   if (distMatches == 3) {
06251                      /* The ring matches, set the context to whatever is for distinctive ring.. */
06252                      ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06253                      ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06254                      if(option_verbose > 2)
06255                         ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06256                      break;
06257                   }
06258                }
06259             }
06260             if (res == 1) {
06261                callerid_get(cs, &name, &number, &flags);
06262                if (option_debug)
06263                   ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06264             }
06265             /* Restore linear mode (if appropriate) for Caller*ID processing */
06266             zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06267 #if 1
06268             restore_gains(p);
06269 #endif            
06270             if (res < 0) {
06271                ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06272             }
06273          } else
06274             ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06275       }
06276       else
06277          cs = NULL;
06278       if (number || name) {
06279           if (chan->cid.cid_num) {
06280          free(chan->cid.cid_num);
06281          chan->cid.cid_num = NULL;
06282           }
06283           if (chan->cid.cid_name) {
06284          free(chan->cid.cid_name);
06285          chan->cid.cid_name = NULL;
06286           }
06287       }
06288       if (number)
06289          ast_shrink_phone_number(number);
06290 
06291       ast_set_callerid(chan, number, name, number);
06292 
06293       if (cs)
06294          callerid_free(cs);
06295       ast_setstate(chan, AST_STATE_RING);
06296       chan->rings = 1;
06297       p->ringt = p->ringt_base;
06298       res = ast_pbx_run(chan);
06299       if (res) {
06300          ast_hangup(chan);
06301          ast_log(LOG_WARNING, "PBX exited non-zero\n");
06302       }
06303       return NULL;
06304    default:
06305       ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
06306       res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06307       if (res < 0)
06308             ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06309    }
06310    res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06311    if (res < 0)
06312          ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06313    ast_hangup(chan);
06314    return NULL;
06315 }
06316 
06317 #ifdef ZAPATA_R2
06318 static int handle_init_r2_event(struct zt_pvt *i, mfcr2_event_t *e)
06319 {
06320    struct ast_channel *chan;
06321    
06322    switch(e->e) {
06323    case MFCR2_EVENT_UNBLOCKED:
06324       i->r2blocked = 0;
06325       if (option_verbose > 2)
06326          ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
06327       break;
06328    case MFCR2_EVENT_BLOCKED:
06329       i->r2blocked = 1;
06330       if (option_verbose > 2)
06331          ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
06332       break;
06333    case MFCR2_EVENT_IDLE:
06334       if (option_verbose > 2)
06335          ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d idle\n", i->channel);
06336       break;
06337    case MFCR2_EVENT_RINGING:
06338          /* This is what Asterisk refers to as a "RING" event. For some reason they're reversed in
06339             Steve's code */
06340          /* Check for callerid, digits, etc */
06341          i->hasr2call = 1;
06342          chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06343          if (!chan) {
06344             ast_log(LOG_WARNING, "Unable to create channel for channel %d\n", i->channel);
06345             mfcr2_DropCall(i->r2, NULL, UC_NETWORK_CONGESTION);
06346             i->hasr2call = 0;
06347          }
06348          if (ast_pbx_start(chan)) {
06349             ast_log(LOG_WARNING, "Unable to start PBX on channel %s\n", chan->name);
06350             ast_hangup(chan);
06351          }
06352          break;
06353    default:
06354       ast_log(LOG_WARNING, "Don't know how to handle initial R2 event %s on channel %d\n", mfcr2_event2str(e->e), i->channel);   
06355       return -1;
06356    }
06357    return 0;
06358 }
06359 #endif
06360 
06361 static int handle_init_event(struct zt_pvt *i, int event)
06362 {
06363    int res;
06364    pthread_t threadid;
06365    pthread_attr_t attr;
06366    struct ast_channel *chan;
06367    pthread_attr_init(&attr);
06368    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06369    /* Handle an event on a given channel for the monitor thread. */
06370    switch(event) {
06371    case ZT_EVENT_NONE:
06372    case ZT_EVENT_BITSCHANGED:
06373       if (i->radio) break;
06374 #ifdef ZAPATA_R2
06375       if (i->r2) {
06376          mfcr2_event_t *e;
06377          e = r2_get_event_bits(i);
06378          i->sigchecked = 1;
06379          if (e)
06380             handle_init_r2_event(i, e);
06381       }
06382 #endif      
06383       break;
06384    case ZT_EVENT_WINKFLASH:
06385    case ZT_EVENT_RINGOFFHOOK:
06386       if (i->inalarm) break;
06387       if (i->radio) break;
06388       /* Got a ring/answer.  What kind of channel are we? */
06389       switch(i->sig) {
06390       case SIG_FXOLS:
06391       case SIG_FXOGS:
06392       case SIG_FXOKS:
06393               zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06394          if (i->cidspill) {
06395             /* Cancel VMWI spill */
06396             free(i->cidspill);
06397             i->cidspill = NULL;
06398          }
06399          if (i->immediate) {
06400             zt_enable_ec(i);
06401             /* The channel is immediately up.  Start right away */
06402             res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
06403             chan = zt_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
06404             if (!chan) {
06405                ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
06406                res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06407                if (res < 0)
06408                   ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06409             }
06410          } else {
06411             /* Check for callerid, digits, etc */
06412             chan = zt_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
06413             if (chan) {
06414                if (has_voicemail(i))
06415                   res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
06416                else
06417                   res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
06418                if (res < 0) 
06419                   ast_log(LOG_WARNING, "Unable to play dialtone on channel %d\n", i->channel);
06420                if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06421                   ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06422                   res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06423                   if (res < 0)
06424                      ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06425                   ast_hangup(chan);
06426                }
06427             } else
06428                ast_log(LOG_WARNING, "Unable to create channel\n");
06429          }
06430          break;
06431       case SIG_FXSLS:
06432       case SIG_FXSGS:
06433       case SIG_FXSKS:
06434             i->ringt = i->ringt_base;
06435             /* Fall through */
06436       case SIG_EMWINK:
06437       case SIG_FEATD:
06438       case SIG_FEATDMF:
06439       case SIG_E911:
06440       case SIG_FEATB:
06441       case SIG_EM:
06442       case SIG_EM_E1:
06443       case SIG_SFWINK:
06444       case SIG_SF_FEATD:
06445       case SIG_SF_FEATDMF:
06446       case SIG_SF_FEATB:
06447       case SIG_SF:
06448             /* Check for callerid, digits, etc */
06449             chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06450             if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06451                ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06452                res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06453                if (res < 0)
06454                   ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06455                ast_hangup(chan);
06456             } else if (!chan) {
06457                ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
06458             }
06459             break;
06460       default:
06461          ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06462          res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06463          if (res < 0)
06464                ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06465          return -1;
06466       }
06467       break;
06468    case ZT_EVENT_NOALARM:
06469       i->inalarm = 0;
06470       ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
06471       manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
06472                     "Channel: %d\r\n", i->channel);
06473       break;
06474    case ZT_EVENT_ALARM:
06475       i->inalarm = 1;
06476       res = get_alarms(i);
06477       ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", i->channel, alarm2str(res));
06478       manager_event(EVENT_FLAG_SYSTEM, "Alarm",
06479                     "Alarm: %s\r\n"
06480                     "Channel: %d\r\n",
06481                     alarm2str(res), i->channel);
06482       /* fall thru intentionally */
06483    case ZT_EVENT_ONHOOK:
06484       if (i->radio) break;
06485       /* Back on hook.  Hang up. */
06486       switch(i->sig) {
06487       case SIG_FXOLS:
06488       case SIG_FXOGS:
06489       case SIG_FEATD:
06490       case SIG_FEATDMF:
06491       case SIG_E911:
06492       case SIG_FEATB:
06493       case SIG_EM:
06494       case SIG_EM_E1:
06495       case SIG_EMWINK:
06496       case SIG_SF_FEATD:
06497       case SIG_SF_FEATDMF:
06498       case SIG_SF_FEATB:
06499       case SIG_SF:
06500       case SIG_SFWINK:
06501       case SIG_FXSLS:
06502       case SIG_FXSGS:
06503       case SIG_FXSKS:
06504       case SIG_GR303FXSKS:
06505          zt_disable_ec(i);
06506          res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06507          zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06508          break;
06509       case SIG_GR303FXOKS:
06510       case SIG_FXOKS:
06511          zt_disable_ec(i);
06512          /* Diddle the battery for the zhone */
06513 #ifdef ZHONE_HACK
06514          zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06515          usleep(1);
06516 #endif         
06517          res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06518          zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06519          break;
06520       case SIG_PRI:
06521          zt_disable_ec(i);
06522          res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06523          break;
06524       default:
06525          ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06526          res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06527          return -1;
06528       }
06529       break;
06530    case ZT_EVENT_POLARITY:
06531       switch(i->sig) {
06532       case SIG_FXSLS:
06533       case SIG_FXSKS:
06534       case SIG_FXSGS:
06535          if (i->cid_start == CID_START_POLARITY) {
06536             i->polarity = POLARITY_REV;
06537             ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
06538                    "CID detection on channel %d\n",
06539                    i->channel);
06540             chan = zt_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
06541             if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06542                ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06543             }
06544          }
06545          break;
06546       default:
06547          ast_log(LOG_WARNING, "handle_init_event detected "
06548             "polarity reversal on non-FXO (SIG_FXS) "
06549             "interface %d\n", i->channel);
06550       }
06551    }
06552    return 0;
06553 }
06554 
06555 static void *do_monitor(void *data)
06556 {
06557    int count, res, res2, spoint, pollres=0;
06558    struct zt_pvt *i;
06559    struct zt_pvt *last = NULL;
06560    time_t thispass = 0, lastpass = 0;
06561    int found;
06562    char buf[1024];
06563    struct pollfd *pfds=NULL;
06564    int lastalloc = -1;
06565    /* This thread monitors all the frame relay interfaces which are not yet in use
06566       (and thus do not have a separate thread) indefinitely */
06567    /* From here on out, we die whenever asked */
06568 #if 0
06569    if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
06570       ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
06571       return NULL;
06572    }
06573    ast_log(LOG_DEBUG, "Monitor starting...\n");
06574 #endif
06575    for(;;) {
06576       /* Lock the interface list */
06577       if (ast_mutex_lock(&iflock)) {
06578          ast_log(LOG_ERROR, "Unable to grab interface lock\n");
06579          return NULL;
06580       }
06581       if (!pfds || (lastalloc != ifcount)) {
06582          if (pfds)
06583             free(pfds);
06584          if (ifcount) {
06585             pfds = malloc(ifcount * sizeof(struct pollfd));
06586             if (!pfds) {
06587                ast_log(LOG_WARNING, "Critical memory error.  Zap dies.\n");
06588                ast_mutex_unlock(&iflock);
06589                return NULL;
06590             }
06591          }
06592          lastalloc = ifcount;
06593       }
06594       /* Build the stuff we're going to poll on, that is the socket of every
06595          zt_pvt that does not have an associated owner channel */
06596       count = 0;
06597       i = iflist;
06598       while(i) {
06599          if ((i->subs[SUB_REAL].zfd > -1) && i->sig && (!i->radio)) {
06600             if (!i->owner && !i->subs[SUB_REAL].owner) {
06601                /* This needs to be watched, as it lacks an owner */
06602                pfds[count].fd = i->subs[SUB_REAL].zfd;
06603                pfds[count].events = POLLPRI;
06604                pfds[count].revents = 0;
06605                /* Message waiting or r2 channels also get watched for reading */
06606 #ifdef ZAPATA_R2
06607                if (i->cidspill || i->r2)
06608 #else             
06609                if (i->cidspill)
06610 #endif               
06611                   pfds[count].events |= POLLIN;
06612                count++;
06613             }
06614          }
06615          i = i->next;
06616       }
06617       /* Okay, now that we know what to do, release the interface lock */
06618       ast_mutex_unlock(&iflock);
06619       
06620       pthread_testcancel();
06621       /* Wait at least a second for something to happen */
06622       res = poll(pfds, count, 1000);
06623       pthread_testcancel();
06624       /* Okay, poll has finished.  Let's see what happened.  */
06625       if (res < 0) {
06626          if ((errno != EAGAIN) && (errno != EINTR))
06627             ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
06628          continue;
06629       }
06630       /* Alright, lock the interface list again, and let's look and see what has
06631          happened */
06632       if (ast_mutex_lock(&iflock)) {
06633          ast_log(LOG_WARNING, "Unable to lock the interface list\n");
06634          continue;
06635       }
06636       found = 0;
06637       spoint = 0;
06638       lastpass = thispass;
06639       thispass = time(NULL);
06640       i = iflist;
06641       while(i) {
06642          if (thispass != lastpass) {
06643             if (!found && ((i == last) || ((i == iflist) && !last))) {
06644                last = i;
06645                if (last) {
06646                   if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) &&
06647                      (last->sig & __ZT_SIG_FXO)) {
06648                      res = ast_app_has_voicemail(last->mailbox, NULL);
06649                      if (last->msgstate != res) {
06650                         int x;
06651                         ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel);
06652                         x = ZT_FLUSH_BOTH;
06653                         res2 = ioctl(last->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
06654                         if (res2)
06655                            ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", last->channel);
06656                         last->cidspill = malloc(MAX_CALLERID_SIZE);
06657                         if (last->cidspill) {
06658                            /* Turn on on hook transfer for 4 seconds */
06659                            x = 4000;
06660                            ioctl(last->subs[SUB_REAL].zfd, ZT_ONHOOKTRANSFER, &x);
06661                            last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last));
06662                            last->cidpos = 0;
06663                            last->msgstate = res;
06664                            last->onhooktime = thispass;
06665                         }
06666                         found ++;
06667                      }
06668                   }
06669                   last = last->next;
06670                }
06671             }
06672          }
06673          if ((i->subs[SUB_REAL].zfd > -1) && i->sig) {
06674             if (i->radio && !i->owner)
06675             {
06676                res = zt_get_event(i->subs[SUB_REAL].zfd);
06677                if (res)
06678                {
06679                   if (option_debug)
06680                      ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
06681                   /* Don't hold iflock while handling init events */
06682                   ast_mutex_unlock(&iflock);
06683                   handle_init_event(i, res);
06684                   ast_mutex_lock(&iflock);   
06685                }
06686                i = i->next;
06687                continue;
06688             }              
06689             pollres = ast_fdisset(pfds, i->subs[SUB_REAL].zfd, count, &spoint);
06690             if (pollres & POLLIN) {
06691                if (i->owner || i->subs[SUB_REAL].owner) {
06692 #ifdef ZAPATA_PRI
06693                   if (!i->pri)
06694 #endif                  
06695                      ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].zfd);
06696                   i = i->next;
06697                   continue;
06698                }
06699 #ifdef ZAPATA_R2
06700                if (i->r2) {
06701                   /* If it's R2 signalled, we always have to check for events */
06702                   mfcr2_event_t *e;
06703                   e = mfcr2_check_event(i->r2);
06704                   if (e)
06705                      handle_init_r2_event(i, e);
06706                   else {
06707                      e = mfcr2_schedule_run(i->r2);
06708                      if (e)
06709                         handle_init_r2_event(i, e);
06710                   }
06711                   i = i->next;
06712                   continue;
06713                }
06714 #endif
06715                if (!i->cidspill) {
06716                   ast_log(LOG_WARNING, "Whoa....  I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].zfd);
06717                   i = i->next;
06718                   continue;
06719                }
06720                res = read(i->subs[SUB_REAL].zfd, buf, sizeof(buf));
06721                if (res > 0) {
06722                   /* We read some number of bytes.  Write an equal amount of data */
06723                   if (res > i->cidlen - i->cidpos) 
06724                      res = i->cidlen - i->cidpos;
06725                   res2 = write(i->subs[SUB_REAL].zfd, i->cidspill + i->cidpos, res);
06726                   if (res2 > 0) {
06727                      i->cidpos += res2;
06728                      if (i->cidpos >= i->cidlen) {
06729                         free(i->cidspill);
06730                         i->cidspill = 0;
06731                         i->cidpos = 0;
06732                         i->cidlen = 0;
06733                      }
06734                   } else {
06735                      ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
06736                      i->msgstate = -1;
06737                   }
06738                } else {
06739                   ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
06740                }
06741                if (option_debug)
06742                   ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06743                /* Don't hold iflock while handling init events -- race with chlock */
06744                ast_mutex_unlock(&iflock);
06745                handle_init_event(i, res);
06746                ast_mutex_lock(&iflock);   
06747             }
06748 #ifdef ZAPATA_R2
06749             if ((pollres & POLLPRI) || (i->r2 && !i->sigchecked)) 
06750 #else          
06751             if (pollres & POLLPRI) 
06752 #endif            
06753             {
06754                if (i->owner || i->subs[SUB_REAL].owner) {
06755 #ifdef ZAPATA_PRI
06756                   if (!i->pri)
06757 #endif                  
06758                      ast_log(LOG_WARNING, "Whoa....  I'm owned but found (%d)...\n", i->subs[SUB_REAL].zfd);
06759                   i = i->next;
06760                   continue;
06761                }
06762                res = zt_get_event(i->subs[SUB_REAL].zfd);
06763                if (option_debug)
06764                   ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06765                /* Don't hold iflock while handling init events */
06766                ast_mutex_unlock(&iflock);
06767                handle_init_event(i, res);
06768                ast_mutex_lock(&iflock);   
06769             }
06770          }
06771          i=i->next;
06772       }
06773       ast_mutex_unlock(&iflock);
06774    }
06775    /* Never reached */
06776    return NULL;
06777    
06778 }
06779 
06780 static int restart_monitor(void)
06781 {
06782    pthread_attr_t attr;
06783    pthread_attr_init(&attr);
06784    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06785    /* If we're supposed to be stopped -- stay stopped */
06786    if (monitor_thread == AST_PTHREADT_STOP)
06787       return 0;
06788    if (ast_mutex_lock(&monlock)) {
06789       ast_log(LOG_WARNING, "Unable to lock monitor\n");
06790       return -1;
06791    }
06792    if (monitor_thread == pthread_self()) {
06793       ast_mutex_unlock(&monlock);
06794       ast_log(LOG_WARNING, "Cannot kill myself\n");
06795       return -1;
06796    }
06797    if (monitor_thread != AST_PTHREADT_NULL) {
06798       /* Just signal it to be sure it wakes up */
06799 #if 0
06800       pthread_cancel(monitor_thread);
06801 #endif
06802       pthread_kill(monitor_thread, SIGURG);
06803 #if 0
06804       pthread_join(monitor_thread, NULL);
06805 #endif
06806    } else {
06807       /* Start a new monitor */
06808       if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
06809          ast_mutex_unlock(&monlock);
06810          ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
06811          return -1;
06812       }
06813    }
06814    ast_mutex_unlock(&monlock);
06815    return 0;
06816 }
06817 
06818 #ifdef ZAPATA_PRI
06819 static int pri_resolve_span(int *span, int channel, int offset, struct zt_spaninfo *si)
06820 {
06821    int x;
06822    int trunkgroup;
06823    /* Get appropriate trunk group if there is one */
06824    trunkgroup = pris[*span].mastertrunkgroup;
06825    if (trunkgroup) {
06826       /* Select a specific trunk group */
06827       for (x=0;x<NUM_SPANS;x++) {
06828          if (pris[x].trunkgroup == trunkgroup) {
06829             *span = x;
06830             return 0;
06831          }
06832       }
06833       ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup);
06834       *span = -1;
06835    } else {
06836       if (pris[*span].trunkgroup) {
06837          ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup);
06838          *span = -1;
06839       } else if (pris[*span].mastertrunkgroup) {
06840          ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup);
06841          *span = -1;
06842       } else {
06843          if (si->totalchans == 31) { /* if it's an E1 */
06844             pris[*span].dchannels[0] = 16 + offset;
06845          } else {
06846             pris[*span].dchannels[0] = 24 + offset;
06847          }
06848          pris[*span].dchanavail[0] |= DCHAN_PROVISIONED;
06849          pris[*span].offset = offset;
06850          pris[*span].span = *span + 1;
06851       }
06852    }
06853    return 0;
06854 }
06855 
06856 static int pri_create_trunkgroup(int trunkgroup, int *channels)
06857 {
06858    struct zt_spaninfo si;
06859    ZT_PARAMS p;
06860    int fd;
06861    int span;
06862    int ospan=0;
06863    int x,y;
06864    for (x=0;x<NUM_SPANS;x++) {
06865       if (pris[x].trunkgroup == trunkgroup) {
06866          ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]);
06867          return -1;
06868       }
06869    }
06870    for (y=0;y<NUM_DCHANS;y++) {
06871       if (!channels[y]) 
06872          break;
06873       memset(&si, 0, sizeof(si));
06874       memset(&p, 0, sizeof(p));
06875       fd = open("/dev/zap/channel", O_RDWR);
06876       if (fd < 0) {
06877          ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno));
06878          return -1;
06879       }
06880       x = channels[y];
06881       if (ioctl(fd, ZT_SPECIFY, &x)) {
06882          ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
06883          zt_close(fd);
06884          return -1;
06885       }
06886       if (ioctl(fd, ZT_GET_PARAMS, &p)) {
06887          ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno));
06888          return -1;
06889       }
06890       if (ioctl(fd, ZT_SPANSTAT, &si)) {
06891          ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d)\n", channels[y], p.spanno);
06892          zt_close(fd);
06893          return -1;
06894       }
06895       span = p.spanno - 1;
06896       if (pris[span].trunkgroup) {
06897          ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
06898          zt_close(fd);
06899          return -1;
06900       }
06901       if (pris[span].pvts[0]) {
06902          ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
06903          zt_close(fd);
06904          return -1;
06905       }
06906       if (!y) {
06907          pris[span].trunkgroup = trunkgroup;
06908          pris[span].offset = channels[y] - p.chanpos;
06909          ospan = span;
06910       }
06911       pris[ospan].dchannels[y] = channels[y];
06912       pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
06913       pris[span].span = span + 1;
06914       zt_close(fd);
06915    }
06916    return 0;   
06917 }
06918 
06919 static int pri_create_spanmap(int span, int trunkgroup, int logicalspan)
06920 {
06921    if (pris[span].mastertrunkgroup) {
06922       ast_log(LOG_WARNING, "Span %d is already part of trunk group %d, cannot add to trunk group %d\n", span + 1, pris[span].mastertrunkgroup, trunkgroup);
06923       return -1;
06924    }
06925    pris[span].mastertrunkgroup = trunkgroup;
06926    pris[span].prilogicalspan = logicalspan;
06927    return 0;
06928 }
06929 
06930 #endif
06931 
06932 static struct zt_pvt *mkintf(int channel, int signalling, int radio, struct zt_pri *pri, int reloading)
06933 {
06934    /* Make a zt_pvt structure for this interface (or CRV if "pri" is specified) */
06935    struct zt_pvt *tmp = NULL, *tmp2,  *prev = NULL;
06936    char fn[80];
06937 #if 1
06938    struct zt_bufferinfo bi;
06939 #endif
06940    struct zt_spaninfo si;
06941    int res;
06942    int span=0;
06943    int here = 0;
06944    int x;
06945    struct zt_pvt **wlist;
06946    struct zt_pvt **wend;
06947    ZT_PARAMS p;
06948 
06949    wlist = &iflist;
06950    wend = &ifend;
06951 
06952 #ifdef ZAPATA_PRI
06953    if (pri) {
06954       wlist = &pri->crvs;
06955       wend = &pri->crvend;
06956    }
06957 #endif
06958 
06959    tmp2 = *wlist;
06960    prev = NULL;
06961 
06962    while (tmp2) {
06963       if (!tmp2->destroy) {
06964          if (tmp2->channel == channel) {
06965             tmp = tmp2;
06966             here = 1;
06967             break;
06968          }
06969          if (tmp2->channel > channel) {
06970             break;
06971          }
06972       }
06973       prev = tmp2;
06974       tmp2 = tmp2->next;
06975    }
06976 
06977    if (!here && !reloading) {
06978       tmp = (struct zt_pvt*)malloc(sizeof(struct zt_pvt));
06979       if (!tmp) {
06980          ast_log(LOG_ERROR, "MALLOC FAILED\n");
06981          destroy_zt_pvt(&tmp);
06982          return NULL;
06983       }
06984       memset(tmp, 0, sizeof(struct zt_pvt));
06985       ast_mutex_init(&tmp->lock);
06986       ifcount++;
06987       for (x=0;x<3;x++)
06988          tmp->subs[x].zfd = -1;
06989       tmp->channel = channel;
06990    }
06991 
06992    if (tmp) {
06993       if (!here) {
06994          if ((channel != CHAN_PSEUDO) && !pri) {
06995             snprintf(fn, sizeof(fn), "%d", channel);
06996             /* Open non-blocking */
06997             if (!here)
06998                tmp->subs[SUB_REAL].zfd = zt_open(fn);
06999             /* Allocate a zapata structure */
07000             if (tmp->subs[SUB_REAL].zfd < 0) {
07001                ast_log(LOG_ERROR, "Unable to open channel %d: %s\nhere = %d, tmp->channel = %d, channel = %d\n", channel, strerror(errno), here, tmp->channel, channel);
07002                destroy_zt_pvt(&tmp);
07003                return NULL;
07004             }
07005             memset(&p, 0, sizeof(p));
07006             res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
07007             if (res < 0) {
07008                ast_log(LOG_ERROR, "Unable to get parameters\n");
07009                destroy_zt_pvt(&tmp);
07010                return NULL;
07011             }
07012             if (p.sigtype != (signalling & 0x3ffff)) {
07013                ast_log(LOG_ERROR, "Signalling requested on channel %d is %s but line is in %s signalling\n", channel, sig2str(signalling), sig2str(p.sigtype));
07014                destroy_zt_pvt(&tmp);
07015                return tmp;
07016             }
07017             tmp->law = p.curlaw;
07018             tmp->span = p.spanno;
07019             span = p.spanno - 1;
07020          } else {
07021             if (channel == CHAN_PSEUDO)
07022                signalling = 0;
07023             else if ((signalling != SIG_FXOKS) && (signalling != SIG_FXSKS)) {
07024                ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n");
07025                return NULL;
07026             }
07027          }
07028 #ifdef ZAPATA_PRI
07029          if ((signalling == SIG_PRI) || (signalling == SIG_GR303FXOKS) || (signalling == SIG_GR303FXSKS)) {
07030             int offset;
07031             int myswitchtype;
07032             int matchesdchan;
07033             int x,y;
07034             offset = 0;
07035             if ((signalling == SIG_PRI) && ioctl(tmp->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &offset)) {
07036                ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
07037                destroy_zt_pvt(&tmp);
07038                return NULL;
07039             }
07040             if (span >= NUM_SPANS) {
07041                ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span);
07042                destroy_zt_pvt(&tmp);
07043                return NULL;
07044             } else {
07045                si.spanno = 0;
07046                if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
07047                   ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
07048                   destroy_zt_pvt(&tmp);
07049                   return NULL;
07050                }
07051                /* Store the logical span first based upon the real span */
07052                tmp->logicalspan = pris[span].prilogicalspan;
07053                pri_resolve_span(&span, channel, (channel - p.chanpos), &si);
07054                if (span < 0) {
07055                   ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel);
07056                   destroy_zt_pvt(&tmp);
07057                   return NULL;
07058                }
07059                if (signalling == SIG_PRI)
07060                   myswitchtype = switchtype;
07061                else
07062                   myswitchtype = PRI_SWITCH_GR303_TMC;
07063                /* Make sure this isn't a d-channel */
07064                matchesdchan=0;
07065                for (x=0;x<NUM_SPANS;x++) {
07066                   for (y=0;y<NUM_DCHANS;y++) {
07067                      if (pris[x].dchannels[y] == tmp->channel) {
07068                         matchesdchan = 1;
07069                         break;
07070                      }
07071                   }
07072                }
07073                offset = p.chanpos;
07074                if (!matchesdchan) {
07075                   if (pris[span].nodetype && (pris[span].nodetype != pritype)) {
07076                      ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype));
07077                      destroy_zt_pvt(&tmp);
07078                      return NULL;
07079                   }
07080                   if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) {
07081                      ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype));
07082                      destroy_zt_pvt(&tmp);
07083                      return NULL;
07084                   }
07085                   if ((pris[span].dialplan) && (pris[span].dialplan != dialplan)) {
07086                      ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan));
07087                      destroy_zt_pvt(&tmp);
07088                      return NULL;
07089                   }
07090                   if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, idledial)) {
07091                      ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, idledial);
07092                      destroy_zt_pvt(&tmp);
07093                      return NULL;
07094                   }
07095                   if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, idleext)) {
07096                      ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, idleext);
07097                      destroy_zt_pvt(&tmp);
07098                      return NULL;
07099                   }
07100                   if (pris[span].minunused && (pris[span].minunused != minunused)) {
07101                      ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, minunused);
07102                      destroy_zt_pvt(&tmp);
07103                      return NULL;
07104                   }
07105                   if (pris[span].minidle && (pris[span].minidle != minidle)) {
07106                      ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, minidle);
07107                      destroy_zt_pvt(&tmp);
07108                      return NULL;
07109                   }
07110                   if (pris[span].numchans >= MAX_CHANNELS) {
07111                      ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel,
07112                         pris[span].trunkgroup);
07113                      destroy_zt_pvt(&tmp);
07114                      return NULL;
07115                   }
07116                   pris[span].nodetype = pritype;
07117                   pris[span].switchtype = myswitchtype;
07118                   pris[span].nsf = nsf;
07119                   pris[span].dialplan = dialplan;
07120                   pris[span].localdialplan = localdialplan;
07121                   pris[span].pvts[pris[span].numchans++] = tmp;
07122                   pris[span].minunused = minunused;
07123                   pris[span].minidle = minidle;
07124                   pris[span].overlapdial = overlapdial;
07125                   pris[span].facilityenable = facilityenable;
07126                   ast_copy_string(pris[span].idledial, idledial, sizeof(pris[span].idledial));
07127                   ast_copy_string(pris[span].idleext, idleext, sizeof(pris[span].idleext));
07128                   ast_copy_string(pris[span].internationalprefix, internationalprefix, sizeof(pris[span].internationalprefix));
07129                   ast_copy_string(pris[span].nationalprefix, nationalprefix, sizeof(pris[span].nationalprefix));
07130                   ast_copy_string(pris[span].localprefix, localprefix, sizeof(pris[span].localprefix));
07131                   ast_copy_string(pris[span].privateprefix, privateprefix, sizeof(pris[span].privateprefix));
07132                   ast_copy_string(pris[span].unknownprefix, unknownprefix, sizeof(pris[span].unknownprefix));
07133                   pris[span].resetinterval = resetinterval;
07134                   
07135                   tmp->pri = &pris[span];
07136                   tmp->prioffset = offset;
07137                   tmp->call = NULL;
07138                } else {
07139                   ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset);
07140                   destroy_zt_pvt(&tmp);
07141                   return NULL;
07142                }
07143             }
07144          } else {
07145             tmp->prioffset = 0;
07146          }
07147 #endif
07148 #ifdef ZAPATA_R2
07149          if (signalling == SIG_R2) {
07150             if (r2prot < 0) {
07151                ast_log(LOG_WARNING, "R2 Country not specified for channel %d -- Assuming China\n", tmp->channel);
07152                tmp->r2prot = MFCR2_PROT_CHINA;
07153             } else
07154                tmp->r2prot = r2prot;
07155             tmp->r2 = mfcr2_new(tmp->subs[SUB_REAL].zfd, tmp->r2prot, 1);
07156             if (!tmp->r2) {
07157                ast_log(LOG_WARNING, "Unable to create r2 call :(\n");
07158                zt_close(tmp->subs[SUB_REAL].zfd);
07159                destroy_zt_pvt(&tmp);
07160                return NULL;
07161             }
07162          } else {
07163             if (tmp->r2) 
07164                mfcr2_free(tmp->r2);
07165             tmp->r2 = NULL;
07166          }
07167 #endif
07168       } else {
07169          signalling = tmp->sig;
07170          radio = tmp->radio;
07171          memset(&p, 0, sizeof(p));
07172          if (tmp->subs[SUB_REAL].zfd > -1)
07173             res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
07174       }
07175       /* Adjust starttime on loopstart and kewlstart trunks to reasonable values */
07176       if ((signalling == SIG_FXSKS) || (signalling == SIG_FXSLS) ||
07177           (signalling == SIG_EM) || (signalling == SIG_EM_E1) ||  (signalling == SIG_EMWINK) ||
07178          (signalling == SIG_FEATD) || (signalling == SIG_FEATDMF) || (signalling == SIG_FEATDMF_TA) ||
07179            (signalling == SIG_FEATB) || (signalling == SIG_E911) ||
07180           (signalling == SIG_SF) || (signalling == SIG_SFWINK) ||
07181          (signalling == SIG_SF_FEATD) || (signalling == SIG_SF_FEATDMF) ||
07182            (signalling == SIG_SF_FEATB)) {
07183          p.starttime = 250;
07184       }
07185       if (radio) {
07186          /* XXX Waiting to hear back from Jim if these should be adjustable XXX */
07187          p.channo = channel;
07188          p.rxwinktime = 1;
07189          p.rxflashtime = 1;
07190          p.starttime = 1;
07191          p.debouncetime = 5;
07192       }
07193       if (!radio) {
07194          p.channo = channel;
07195          /* Override timing settings based on config file */
07196          if (cur_prewink >= 0)
07197             p.prewinktime = cur_prewink;
07198          if (cur_preflash >= 0)
07199             p.preflashtime = cur_preflash;
07200          if (cur_wink >= 0)
07201             p.winktime = cur_wink;
07202          if (cur_flash >= 0)
07203             p.flashtime = cur_flash;
07204          if (cur_start >= 0)
07205             p.starttime = cur_start;
07206          if (cur_rxwink >= 0)
07207             p.rxwinktime = cur_rxwink;
07208          if (cur_rxflash >= 0)
07209             p.rxflashtime = cur_rxflash;
07210          if (cur_debounce >= 0)
07211             p.debouncetime = cur_debounce;
07212       }
07213       
07214       /* dont set parms on a pseudo-channel (or CRV) */
07215       if (tmp->subs[SUB_REAL].zfd >= 0)
07216       {
07217          res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_PARAMS, &p);
07218          if (res < 0) {
07219             ast_log(LOG_ERROR, "Unable to set parameters\n");
07220             destroy_zt_pvt(&tmp);
07221             return NULL;
07222          }
07223       }
07224 #if 1
07225       if (!here && (tmp->subs[SUB_REAL].zfd > -1)) {
07226          memset(&bi, 0, sizeof(bi));
07227          res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
07228          if (!res) {
07229             bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
07230             bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
07231             bi.numbufs = numbufs;
07232             res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
07233             if (res < 0) {
07234                ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", channel);
07235             }
07236          } else
07237             ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", channel);
07238       }
07239 #endif
07240       tmp->immediate = immediate;
07241       tmp->transfertobusy = transfertobusy;
07242       tmp->sig = signalling;
07243       tmp->radio = radio;
07244       tmp->ringt_base = ringt_base;
07245       tmp->firstradio = 0;
07246       if ((signalling == SIG_FXOKS) || (signalling == SIG_FXOLS) || (signalling == SIG_FXOGS))
07247          tmp->permcallwaiting = callwaiting;
07248       else
07249          tmp->permcallwaiting = 0;
07250       /* Flag to destroy the channel must be cleared on new mkif.  Part of changes for reload to work */
07251       tmp->destroy = 0;
07252       tmp->drings = drings;
07253       tmp->usedistinctiveringdetection = usedistinctiveringdetection;
07254       tmp->callwaitingcallerid = callwaitingcallerid;
07255       tmp->threewaycalling = threewaycalling;
07256       tmp->adsi = adsi;
07257       tmp->permhidecallerid = hidecallerid;
07258       tmp->callreturn = callreturn;
07259       tmp->echocancel = echocancel;
07260       tmp->echotraining = echotraining;
07261       tmp->pulse = pulse;
07262       tmp->echocanbridged = echocanbridged;
07263       tmp->busydetect = busydetect;
07264       tmp->busycount = busycount;
07265       tmp->busy_tonelength = busy_tonelength;
07266       tmp->busy_quietlength = busy_quietlength;
07267       tmp->callprogress = callprogress;
07268       tmp->cancallforward = cancallforward;
07269       tmp->dtmfrelax = relaxdtmf;
07270       tmp->callwaiting = tmp->permcallwaiting;
07271       tmp->hidecallerid = tmp->permhidecallerid;
07272       tmp->channel = channel;
07273       tmp->stripmsd = stripmsd;
07274       tmp->use_callerid = use_callerid;
07275       tmp->cid_signalling = cid_signalling;
07276       tmp->cid_start = cid_start;
07277       tmp->zaptrcallerid = zaptrcallerid;
07278       tmp->restrictcid = restrictcid;
07279       tmp->use_callingpres = use_callingpres;
07280       tmp->priindication_oob = priindication_oob;
07281       tmp->priexclusive = cur_priexclusive;
07282       if (tmp->usedistinctiveringdetection) {
07283          if (!tmp->use_callerid) {
07284             ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n");
07285             tmp->use_callerid = 1;
07286          }
07287       }
07288 
07289       ast_copy_string(tmp->accountcode, accountcode, sizeof(tmp->accountcode));
07290       tmp->amaflags = amaflags;
07291       if (!here) {
07292          tmp->confno = -1;
07293          tmp->propconfno = -1;
07294       }
07295       tmp->canpark = canpark;
07296       tmp->transfer = transfer;
07297       ast_copy_string(tmp->defcontext,context,sizeof(tmp->defcontext));
07298       ast_copy_string(tmp->language, language, sizeof(tmp->language));
07299       ast_copy_string(tmp->musicclass, musicclass, sizeof(tmp->musicclass));
07300       ast_copy_string(tmp->context, context, sizeof(tmp->context));
07301       ast_copy_string(tmp->cid_num, cid_num, sizeof(tmp->cid_num));
07302       tmp->cid_ton = 0;
07303       ast_copy_string(tmp->cid_name, cid_name, sizeof(tmp->cid_name));
07304       ast_copy_string(tmp->mailbox, mailbox, sizeof(tmp->mailbox));
07305       tmp->msgstate = -1;
07306       tmp->group = cur_group;
07307       tmp->callgroup=cur_callergroup;
07308       tmp->pickupgroup=cur_pickupgroup;
07309       tmp->rxgain = rxgain;
07310       tmp->txgain = txgain;
07311       tmp->tonezone = tonezone;
07312       tmp->onhooktime = time(NULL);
07313       if (tmp->subs[SUB_REAL].zfd > -1) {
07314          set_actual_gain(tmp->subs[SUB_REAL].zfd, 0, tmp->rxgain, tmp->txgain, tmp->law);
07315          if (tmp->dsp)
07316             ast_dsp_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax);
07317          update_conf(tmp);
07318          if (!here) {
07319             if ((signalling != SIG_PRI) && (signalling != SIG_R2))
07320                /* Hang it up to be sure it's good */
07321                zt_set_hook(tmp->subs[SUB_REAL].zfd, ZT_ONHOOK);
07322          }
07323          ioctl(tmp->subs[SUB_REAL].zfd,ZT_SETTONEZONE,&tmp->tonezone);
07324 #ifdef ZAPATA_PRI
07325          /* the dchannel is down so put the channel in alarm */
07326          if (tmp->pri && !pri_is_up(tmp->pri))
07327             tmp->inalarm = 1;
07328          else
07329             tmp->inalarm = 0;
07330 #endif            
07331          memset(&si, 0, sizeof(si));
07332          if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
07333             ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
07334             destroy_zt_pvt(&tmp);
07335             return NULL;
07336          }
07337          if (si.alarms) tmp->inalarm = 1;
07338       }
07339 
07340       tmp->polarityonanswerdelay = polarityonanswerdelay;
07341       tmp->answeronpolarityswitch = answeronpolarityswitch;
07342       tmp->hanguponpolarityswitch = hanguponpolarityswitch;
07343       tmp->sendcalleridafter = sendcalleridafter;
07344 
07345    }
07346    if (tmp && !here) {
07347       /* nothing on the iflist */
07348       if (!*wlist) {
07349          *wlist = tmp;
07350          tmp->prev = NULL;
07351          tmp->next = NULL;
07352          *wend = tmp;
07353       } else {
07354          /* at least one member on the iflist */
07355          struct zt_pvt *working = *wlist;
07356 
07357          /* check if we maybe have to put it on the begining */
07358          if (working->channel > tmp->channel) {
07359             tmp->next = *wlist;
07360             tmp->prev = NULL;
07361             (*wlist)->prev = tmp;
07362             *wlist = tmp;
07363          } else {
07364          /* go through all the members and put the member in the right place */
07365             while (working) {
07366                /* in the middle */
07367                if (working->next) {
07368                   if (working->channel < tmp->channel && working->next->channel > tmp->channel) {
07369                      tmp->next = working->next;
07370                      tmp->prev = working;
07371                      working->next->prev = tmp;
07372                      working->next = tmp;
07373                      break;
07374                   }
07375                } else {
07376                /* the last */
07377                   if (working->channel < tmp->channel) {
07378                      working->next = tmp;
07379                      tmp->next = NULL;
07380                      tmp->prev = working;
07381                      *wend = tmp;
07382                      break;
07383                   }
07384                }
07385                working = working->next;
07386             }
07387          }
07388       }
07389    }
07390    return tmp;
07391 }
07392 
07393 static inline int available(struct zt_pvt *p, int channelmatch, int groupmatch, int *busy, int *channelmatched, int *groupmatched)
07394 {
07395    int res;
07396    ZT_PARAMS par;
07397 
07398    /* First, check group matching */
07399    if (groupmatch) {
07400       if ((p->group & groupmatch) != groupmatch)
07401          return 0;
07402       *groupmatched = 1;
07403    }
07404    /* Check to see if we have a channel match */
07405    if (channelmatch != -1) {
07406       if (p->channel != channelmatch)
07407          return 0;
07408       *channelmatched = 1;
07409    }
07410    /* We're at least busy at this point */
07411    if (busy) {
07412       if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS))
07413          *busy = 1;
07414    }
07415    /* If do not disturb, definitely not */
07416    if (p->dnd)
07417       return 0;
07418    /* If guard time, definitely not */
07419    if (p->guardtime && (time(NULL) < p->guardtime)) 
07420       return 0;
07421       
07422    /* If no owner definitely available */
07423    if (!p->owner) {
07424 #ifdef ZAPATA_PRI
07425       /* Trust PRI */
07426       if (p->pri) {
07427          if (p->resetting || p->call)
07428             return 0;
07429          else
07430             return 1;
07431       }
07432 #endif
07433 #ifdef ZAPATA_R2
07434       /* Trust R2 as well */
07435       if (p->r2) {
07436          if (p->hasr2call || p->r2blocked)
07437             return 0;
07438          else
07439             return 1;
07440       }
07441 #endif
07442       if (!p->radio)
07443       {
07444          if (!p->sig || (p->sig == SIG_FXSLS))
07445             return 1;
07446          /* Check hook state */
07447          if (p->subs[SUB_REAL].zfd > -1)
07448             res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
07449          else {
07450             /* Assume not off hook on CVRS */
07451             res = 0;
07452             par.rxisoffhook = 0;
07453          }
07454          if (res) {
07455             ast_log(LOG_WARNING, "Unable to check hook state on channel %d\n", p->channel);
07456          } else if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
07457             /* When "onhook" that means no battery on the line, and thus
07458               it is out of service..., if it's on a TDM card... If it's a channel
07459               bank, there is no telling... */
07460             if (par.rxbits > -1)
07461                return 1;
07462             if (par.rxisoffhook)
07463                return 1;
07464             else
07465 #ifdef ZAP_CHECK_HOOKSTATE
07466                return 0;
07467 #else
07468                return 1;
07469 #endif
07470          } else if (par.rxisoffhook) {
07471             ast_log(LOG_DEBUG, "Channel %d off hook, can't use\n", p->channel);
07472             /* Not available when the other end is off hook */
07473             return 0;
07474          }
07475       }
07476       return 1;
07477    }
07478 
07479    /* If it's not an FXO, forget about call wait */
07480    if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS)) 
07481       return 0;
07482 
07483    if (!p->callwaiting) {
07484       /* If they don't have call waiting enabled, then for sure they're unavailable at this point */
07485       return 0;
07486    }
07487 
07488    if (p->subs[SUB_CALLWAIT].zfd > -1) {
07489       /* If there is already a call waiting call, then we can't take a second one */
07490       return 0;
07491    }
07492    
07493    if ((p->owner->_state != AST_STATE_UP) &&
07494        ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
07495       /* If the current call is not up, then don't allow the call */
07496       return 0;
07497    }
07498    if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) {
07499       /* Can't take a call wait when the three way calling hasn't been merged yet. */
07500       return 0;
07501    }
07502    /* We're cool */
07503    return 1;
07504 }
07505 
07506 static struct zt_pvt *chandup(struct zt_pvt *src)
07507 {
07508    struct zt_pvt *p;
07509    ZT_BUFFERINFO bi;
07510    int res;
07511    p = malloc(sizeof(struct zt_pvt));
07512    if (p) {
07513       memcpy(p, src, sizeof(struct zt_pvt));
07514       ast_mutex_init(&p->lock);
07515       p->subs[SUB_REAL].zfd = zt_open("/dev/zap/pseudo");
07516       /* Allocate a zapata structure */
07517       if (p->subs[SUB_REAL].zfd < 0) {
07518          ast_log(LOG_ERROR, "Unable to dup channel: %s\n",  strerror(errno));
07519          destroy_zt_pvt(&p);
07520          return NULL;
07521       }
07522       res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
07523       if (!res) {
07524          bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
07525          bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
07526          bi.numbufs = numbufs;
07527          res = ioctl(p->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
07528          if (res < 0) {
07529             ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel\n");
07530          }
07531       } else
07532          ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel\n");
07533    }
07534    p->destroy = 1;
07535    p->next = iflist;
07536    iflist = p;
07537    return p;
07538 }
07539    
07540 
07541 #ifdef ZAPATA_PRI
07542 static int pri_find_empty_chan(struct zt_pri *pri, int backwards)
07543 {
07544    int x;
07545    if (backwards)
07546       x = pri->numchans;
07547    else
07548       x = 0;
07549    for (;;) {
07550       if (backwards && (x < 0))
07551          break;
07552       if (!backwards && (x >= pri->numchans))
07553          break;
07554       if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
07555          ast_log(LOG_DEBUG, "Found empty available channel %d/%d\n", 
07556             pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
07557          return x;
07558       }
07559       if (backwards)
07560          x--;
07561       else
07562          x++;
07563    }
07564    return -1;
07565 }
07566 #endif
07567 
07568 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause)
07569 {
07570    int oldformat;
07571    int groupmatch = 0;
07572    int channelmatch = -1;
07573    int roundrobin = 0;
07574    int callwait = 0;
07575    int busy = 0;
07576    struct zt_pvt *p;
07577    struct ast_channel *tmp = NULL;
07578    char *dest=NULL;
07579    int x;
07580    char *s;
07581    char opt=0;
07582    int res=0, y=0;
07583    int backwards = 0;
07584 #ifdef ZAPATA_PRI
07585    int crv;
07586    int bearer = -1;
07587    int trunkgroup;
07588    struct zt_pri *pri=NULL;
07589 #endif   
07590    struct zt_pvt *exit, *start, *end;
07591    ast_mutex_t *lock;
07592    int channelmatched = 0;
07593    int groupmatched = 0;
07594    
07595    /* Assume we're locking the iflock */
07596    lock = &iflock;
07597    start = iflist;
07598    end = ifend;
07599    /* We do signed linear */
07600    oldformat = format;
07601    format &= (AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
07602    if (!format) {
07603       ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
07604       return NULL;
07605    }
07606    if (data) {
07607       dest = ast_strdupa((char *)data);
07608    } else {
07609       ast_log(LOG_WARNING, "Channel requested with no data\n");
07610       return NULL;
07611    }
07612    if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') {
07613       /* Retrieve the group number */
07614       char *stringp=NULL;
07615       stringp=dest + 1;
07616       s = strsep(&stringp, "/");
07617       if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07618          ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data);
07619          return NULL;
07620       }
07621       groupmatch = 1 << x;
07622       if (toupper(dest[0]) == 'G') {
07623          if (dest[0] == 'G') {
07624             backwards = 1;
07625             p = ifend;
07626          } else
07627             p = iflist;
07628       } else {
07629          if (dest[0] == 'R') {
07630             backwards = 1;
07631             p = round_robin[x]?round_robin[x]->prev:ifend;
07632             if (!p)
07633                p = ifend;
07634          } else {
07635             p = round_robin[x]?round_robin[x]->next:iflist;
07636             if (!p)
07637                p = iflist;
07638          }
07639          roundrobin = 1;
07640       }
07641    } else {
07642       char *stringp=NULL;
07643       stringp=dest;
07644       s = strsep(&stringp, "/");
07645       p = iflist;
07646       if (!strcasecmp(s, "pseudo")) {
07647          /* Special case for pseudo */
07648          x = CHAN_PSEUDO;
07649          channelmatch = x;
07650       } 
07651 #ifdef ZAPATA_PRI
07652       else if ((res = sscanf(s, "%d:%d%c%d", &trunkgroup, &crv, &opt, &y)) > 1) {
07653          if ((trunkgroup < 1) || (crv < 1)) {
07654             ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data);
07655             return NULL;
07656          }
07657          res--;
07658          for (x=0;x<NUM_SPANS;x++) {
07659             if (pris[x].trunkgroup == trunkgroup) {
07660                pri = pris + x;
07661                lock = &pri->lock;
07662                start = pri->crvs;
07663                end = pri->crvend;
07664                break;
07665             }
07666          }
07667          if (!pri) {
07668             ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup);
07669             return NULL;
07670          }
07671          channelmatch = crv;
07672          p = pris[x].crvs;
07673       }
07674 #endif   
07675       else if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07676          ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data);
07677          return NULL;
07678       } else {
07679          channelmatch = x;
07680       }
07681    }
07682    /* Search for an unowned channel */
07683    if (ast_mutex_lock(lock)) {
07684       ast_log(LOG_ERROR, "Unable to lock interface list???\n");
07685       return NULL;
07686    }
07687    exit = p;
07688    while(p && !tmp) {
07689       if (roundrobin)
07690          round_robin[x] = p;
07691 #if 0
07692       ast_verbose("name = %s, %d, %d, %d\n",p->owner ? p->owner->name : "<none>", p->channel, channelmatch, groupmatch);
07693 #endif
07694 
07695       if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) {
07696          if (option_debug)
07697             ast_log(LOG_DEBUG, "Using channel %d\n", p->channel);
07698             if (p->inalarm) 
07699                goto next;
07700 
07701          callwait = (p->owner != NULL);
07702 #ifdef ZAPATA_PRI
07703          if (pri && (p->subs[SUB_REAL].zfd < 0)) {
07704             if (p->sig != SIG_FXSKS) {
07705                /* Gotta find an actual channel to use for this
07706                   CRV if this isn't a callwait */
07707                bearer = pri_find_empty_chan(pri, 0);
07708                if (bearer < 0) {
07709                   ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv);
07710                   p = NULL;
07711                   break;
07712                }
07713                pri_assign_bearer(p, pri, pri->pvts[bearer]);
07714             } else {
07715                if (alloc_sub(p, 0)) {
07716                   ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n");
07717                   p = NULL;
07718                   break;
07719                } else
07720                   ast_log(LOG_DEBUG, "Allocated placeholder pseudo channel\n");
07721                p->pri = pri;
07722             }
07723          }
07724 #endif         
07725          if (p->channel == CHAN_PSEUDO) {
07726             p = chandup(p);
07727             if (!p) {
07728                break;
07729             }
07730          }
07731          if (p->owner) {
07732             if (alloc_sub(p, SUB_CALLWAIT)) {
07733                p = NULL;
07734                break;
07735             }
07736          }
07737          p->outgoing = 1;
07738          tmp = zt_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0);
07739 #ifdef ZAPATA_PRI
07740          if (p->bearer) {
07741             /* Log owner to bearer channel, too */
07742             p->bearer->owner = tmp;
07743          }
07744 #endif         
07745          /* Make special notes */
07746          if (res > 1) {
07747             if (opt == 'c') {
07748                /* Confirm answer */
07749                p->confirmanswer = 1;
07750             } else if (opt == 'r') {
07751                /* Distinctive ring */
07752                if (res < 3)
07753                   ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data);
07754                else
07755                   p->distinctivering = y;
07756             } else if (opt == 'd') {
07757                /* If this is an ISDN call, make it digital */
07758                p->digital = 1;
07759                if (tmp)
07760                   tmp->transfercapability = AST_TRANS_CAP_DIGITAL;
07761             } else {
07762                ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data);
07763             }
07764          }
07765          /* Note if the call is a call waiting call */
07766          if (tmp && callwait)
07767             tmp->cdrflags |= AST_CDR_CALLWAIT;
07768          break;
07769       }
07770 next:
07771       if (backwards) {
07772          p = p->prev;
07773          if (!p)
07774             p = end;
07775       } else {
07776          p = p->next;
07777          if (!p)
07778             p = start;
07779       }
07780       /* stop when you roll to the one that we started from */
07781       if (p == exit)
07782          break;
07783    }
07784    ast_mutex_unlock(lock);
07785    restart_monitor();
07786    if (callwait)
07787       *cause = AST_CAUSE_BUSY;
07788    else if (!tmp) {
07789       if (channelmatched) {
07790          if (busy)
07791             *cause = AST_CAUSE_BUSY;
07792       } else if (groupmatched) {
07793          *cause = AST_CAUSE_CONGESTION;
07794       }
07795    }
07796       
07797    return tmp;
07798 }
07799 
07800 
07801 #ifdef ZAPATA_PRI
07802 static struct zt_pvt *pri_find_crv(struct zt_pri *pri, int crv)
07803 {
07804    struct zt_pvt *p;
07805    p = pri->crvs;
07806    while(p) {
07807       if (p->channel == crv)
07808          return p;
07809       p = p->next;
07810    }
07811    return NULL;
07812 }
07813 
07814 
07815 static int pri_find_principle(struct zt_pri *pri, int channel)
07816 {
07817    int x;
07818    int span = PRI_SPAN(channel);
07819    int spanfd;
07820    ZT_PARAMS param;
07821    int principle = -1;
07822    int explicit = PRI_EXPLICIT(channel);
07823    span = PRI_SPAN(channel);
07824    channel = PRI_CHANNEL(channel);
07825 
07826    if (!explicit) {
07827       spanfd = pri_active_dchan_fd(pri);
07828       if (ioctl(spanfd, ZT_GET_PARAMS, &param))
07829          return -1;
07830       span = pris[param.spanno - 1].prilogicalspan;
07831    }
07832 
07833    for (x=0;x<pri->numchans;x++) {
07834       if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
07835          principle = x;
07836          break;
07837       }
07838    }
07839    
07840    return principle;
07841 }
07842 
07843 static int pri_fixup_principle(struct zt_pri *pri, int principle, q931_call *c)
07844 {
07845    int x;
07846    struct zt_pvt *crv;
07847    if (!c) {
07848       if (principle < 0)
07849          return -1;
07850       return principle;
07851    }
07852    if ((principle > -1) && 
07853       (principle < pri->numchans) && 
07854       (pri->pvts[principle]) && 
07855       (pri->pvts[principle]->call == c))
07856       return principle;
07857    /* First, check for other bearers */
07858    for (x=0;x<pri->numchans;x++) {
07859       if (!pri->pvts[x]) continue;
07860       if (pri->pvts[x]->call == c) {
07861          /* Found our call */
07862          if (principle != x) {
07863             if (option_verbose > 2)
07864                ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n",
07865                   pri->pvts[x]->channel, pri->pvts[principle]->channel);
07866             if (pri->pvts[principle]->owner) {
07867                ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
07868                   pri->pvts[x]->channel, pri->pvts[principle]->channel, pri->pvts[principle]->channel);
07869                return -1;
07870             }
07871             /* Fix it all up now */
07872             pri->pvts[principle]->owner = pri->pvts[x]->owner;
07873             if (pri->pvts[principle]->owner) {
07874                snprintf(pri->pvts[principle]->owner->name, sizeof(pri->pvts[principle]->owner->name), 
07875                   "Zap/%d:%d-%d", pri->trunkgroup, pri->pvts[principle]->channel, 1);
07876                pri->pvts[principle]->owner->tech_pvt = pri->pvts[principle];
07877                pri->pvts[principle]->owner->fds[0] = pri->pvts[principle]->subs[SUB_REAL].zfd;
07878                pri->pvts[principle]->subs[SUB_REAL].owner = pri->pvts[x]->subs[SUB_REAL].owner;
07879             } else
07880                ast_log(LOG_WARNING, "Whoa, there's no  owner, and we're having to fix up channel %d to channel %d\n", pri->pvts[x]->channel, pri->pvts[principle]->channel);
07881             pri->pvts[principle]->call = pri->pvts[x]->call;
07882             /* Free up the old channel, now not in use */
07883             pri->pvts[x]->subs[SUB_REAL].owner = NULL;
07884             pri->pvts[x]->owner = NULL;
07885             pri->pvts[x]->call = NULL;
07886          }
07887          return principle;
07888       }
07889    }
07890    /* Now check for a CRV with no bearer */
07891    crv = pri->crvs;
07892    while(crv) {
07893       if (crv->call == c) {
07894          /* This is our match...  Perform some basic checks */
07895          if (crv->bearer)
07896             ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n");
07897          else if (pri->pvts[principle]->owner) 
07898             ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n");
07899          else {
07900             /* Looks good.  Drop the pseudo channel now, clear up the assignment, and
07901                wakeup the potential sleeper */
07902             zt_close(crv->subs[SUB_REAL].zfd);
07903             pri->pvts[principle]->call = crv->call;
07904             pri_assign_bearer(crv, pri, pri->pvts[principle]);
07905             ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n",
07906                            pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset,
07907                            pri->trunkgroup, crv->channel);
07908             wakeup_sub(crv, SUB_REAL, pri);
07909          }
07910          return principle;
07911       }
07912       crv = crv->next;
07913    }
07914    ast_log(LOG_WARNING, "Call specified, but not found?\n");
07915    return -1;
07916 }
07917 
07918 static void *do_idle_thread(void *vchan)
07919 {
07920    struct ast_channel *chan = vchan;
07921    struct zt_pvt *pvt = chan->tech_pvt;
07922    struct ast_frame *f;
07923    char ex[80];
07924    /* Wait up to 30 seconds for an answer */
07925    int newms, ms = 30000;
07926    if (option_verbose > 2) 
07927       ast_verbose(VERBOSE_PREFIX_3 "Initiating idle call on channel %s\n", chan->name);
07928    snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
07929    if (ast_call(chan, ex, 0)) {
07930       ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
07931       ast_hangup(chan);
07932       return NULL;
07933    }
07934    while((newms = ast_waitfor(chan, ms)) > 0) {
07935       f = ast_read(chan);
07936       if (!f) {
07937          /* Got hangup */
07938          break;
07939       }
07940       if (f->frametype == AST_FRAME_CONTROL) {
07941          switch(f->subclass) {
07942          case AST_CONTROL_ANSWER:
07943             /* Launch the PBX */
07944             ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
07945             ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
07946             chan->priority = 1;
07947             if (option_verbose > 3) 
07948                ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
07949             ast_pbx_run(chan);
07950             /* It's already hungup, return immediately */
07951             return NULL;
07952          case AST_CONTROL_BUSY:
07953             if (option_verbose > 3) 
07954                ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' busy, waiting...\n", chan->name);
07955             break;
07956          case AST_CONTROL_CONGESTION:
07957             if (option_verbose > 3) 
07958                ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' congested, waiting...\n", chan->name);
07959             break;
07960          };
07961       }
07962       ast_frfree(f);
07963       ms = newms;
07964    }
07965    /* Hangup the channel since nothing happend */
07966    ast_hangup(chan);
07967    return NULL;
07968 }
07969 
07970 #ifndef PRI_RESTART
07971 #error "Upgrade your libpri"
07972 #endif
07973 static void zt_pri_message(struct pri *pri, char *s)
07974 {
07975    int x, y;
07976    int dchan = -1, span = -1;
07977    int dchancount = 0;
07978 
07979    if (pri) {
07980       for (x = 0; x < NUM_SPANS; x++) {
07981          for (y = 0; y < NUM_DCHANS; y++) {
07982             if (pris[x].dchans[y])
07983                dchancount++;
07984 
07985             if (pris[x].dchans[y] == pri)
07986                dchan = y;
07987          }
07988          if (dchan >= 0) {
07989             span = x;
07990             break;
07991          }
07992          dchancount = 0;
07993       }
07994       if ((dchan >= 0) && (span >= 0)) {
07995          if (dchancount > 1)
07996             ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s);
07997          else
07998             ast_verbose("%s", s);
07999       } else
08000          ast_verbose("PRI debug error: could not find pri associated it with debug message output\n");
08001    } else
08002       ast_verbose("%s", s);
08003 
08004    ast_mutex_lock(&pridebugfdlock);
08005 
08006    if (pridebugfd >= 0)
08007       write(pridebugfd, s, strlen(s));
08008 
08009    ast_mutex_unlock(&pridebugfdlock);
08010 }
08011 
08012 static void zt_pri_error(struct pri *pri, char *s)
08013 {
08014    int x, y;
08015    int dchan = -1, span = -1;
08016    int dchancount = 0;
08017 
08018    if (pri) {
08019       for (x = 0; x < NUM_SPANS; x++) {
08020          for (y = 0; y < NUM_DCHANS; y++) {
08021             if (pris[x].dchans[y])
08022                dchancount++;
08023 
08024             if (pris[x].dchans[y] == pri)
08025                dchan = y;
08026          }
08027          if (dchan >= 0) {
08028             span = x;
08029             break;
08030          }
08031          dchancount = 0;
08032       }
08033       if ((dchan >= 0) && (span >= 0)) {
08034          if (dchancount > 1)
08035             ast_log(LOG_WARNING, "[Span %d D-Channel %d] PRI: %s", span, dchan, s);
08036          else
08037             ast_verbose("%s", s);
08038       } else
08039          ast_verbose("PRI debug error: could not find pri associated it with debug message output\n");
08040    } else
08041       ast_log(LOG_WARNING, "%s", s);
08042 
08043    ast_mutex_lock(&pridebugfdlock);
08044 
08045    if (pridebugfd >= 0)
08046       write(pridebugfd, s, strlen(s));
08047 
08048    ast_mutex_unlock(&pridebugfdlock);
08049 }
08050 
08051 static int pri_check_restart(struct zt_pri *pri)
08052 {
08053    do {
08054       pri->resetpos++;
08055    } while((pri->resetpos < pri->numchans) &&
08056        (!pri->pvts[pri->resetpos] ||
08057         pri->pvts[pri->resetpos]->call ||
08058         pri->pvts[pri->resetpos]->resetting));
08059    if (pri->resetpos < pri->numchans) {
08060       /* Mark the channel as resetting and restart it */
08061       pri->pvts[pri->resetpos]->resetting = 1;
08062       pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
08063    } else {
08064       pri->resetting = 0;
08065       time(&pri->lastreset);
08066    }
08067    return 0;
08068 }
08069 
08070 static int pri_hangup_all(struct zt_pvt *p, struct zt_pri *pri)
08071 {
08072    int x;
08073    int redo;
08074    ast_mutex_unlock(&pri->lock);
08075    ast_mutex_lock(&p->lock);
08076    do {
08077       redo = 0;
08078       for (x=0;x<3;x++) {
08079          while(p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) {
08080             redo++;
08081             ast_mutex_unlock(&p->lock);
08082             usleep(1);
08083             ast_mutex_lock(&p->lock);
08084          }
08085          if (p->subs[x].owner) {
08086             ast_queue_hangup(p->subs[x].owner);
08087             ast_mutex_unlock(&p->subs[x].owner->lock);
08088          }
08089       }
08090    } while (redo);
08091    ast_mutex_unlock(&p->lock);
08092    ast_mutex_lock(&pri->lock);
08093    return 0;
08094 }
08095 char * redirectingreason2str(int redirectingreason)
08096 {
08097    switch (redirectingreason) {
08098    case 0:
08099       return "UNKNOWN";
08100    case 1:
08101       return "BUSY";
08102    case 2:
08103       return "NO_REPLY";
08104    case 0xF:
08105       return "UNCONDITIONAL";
08106    default:
08107       return "NOREDIRECT";
08108    }
08109 }
08110 
08111 static void apply_plan_to_number(char *buf, size_t size, const struct zt_pri *pri, const char *number, const int plan)
08112 {
08113    switch (plan) {
08114    case PRI_INTERNATIONAL_ISDN:     /* Q.931 dialplan == 0x11 international dialplan => prepend international prefix digits */
08115       snprintf(buf, size, "%s%s", pri->internationalprefix, number);
08116       break;
08117    case PRI_NATIONAL_ISDN:       /* Q.931 dialplan == 0x21 national dialplan => prepend national prefix digits */
08118       snprintf(buf, size, "%s%s", pri->nationalprefix, number);
08119       break;
08120    case PRI_LOCAL_ISDN:       /* Q.931 dialplan == 0x41 local dialplan => prepend local prefix digits */
08121       snprintf(buf, size, "%s%s", pri->localprefix, number);
08122       break;
08123    case PRI_PRIVATE:       /* Q.931 dialplan == 0x49 private dialplan => prepend private prefix digits */
08124       snprintf(buf, size, "%s%s", pri->privateprefix, number);
08125       break;
08126    case PRI_UNKNOWN:       /* Q.931 dialplan == 0x00 unknown dialplan => prepend unknown prefix digits */
08127       snprintf(buf, size, "%s%s", pri->unknownprefix, number);
08128       break;
08129    default:          /* other Q.931 dialplan => don't twiddle with callingnum */
08130       snprintf(buf, size, "%s", number);
08131       break;
08132    }
08133 }
08134 
08135 static void *pri_dchannel(void *vpri)
08136 {
08137    struct zt_pri *pri = vpri;
08138    pri_event *e;
08139    struct pollfd fds[NUM_DCHANS];
08140    int res;
08141    int chanpos = 0;
08142    int x;
08143    int haveidles;
08144    int activeidles;
08145    int nextidle = -1;
08146    struct ast_channel *c;
08147    struct timeval tv, lowest, *next;
08148    struct timeval lastidle = { 0, 0 };
08149    int doidling=0;
08150    char *cc;
08151    char idlen[80];
08152    struct ast_channel *idle;
08153    pthread_t p;
08154    time_t t;
08155    int i, which=-1;
08156    int numdchans;
08157    int cause=0;
08158    struct zt_pvt *crv;
08159    pthread_t threadid;
08160    pthread_attr_t attr;
08161    char ani2str[6];
08162    char plancallingnum[256];
08163    char plancallingani[256];
08164    char calledtonstr[10];
08165    
08166    pthread_attr_init(&attr);
08167    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
08168 
08169    gettimeofday(&lastidle, NULL);
08170    if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
08171       /* Need to do idle dialing, check to be sure though */
08172       cc = strchr(pri->idleext, '@');
08173       if (cc) {
08174          *cc = '\0';
08175          cc++;
08176          ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
08177 #if 0
08178          /* Extensions may not be loaded yet */
08179          if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
08180             ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
08181          else
08182 #endif
08183             doidling = 1;
08184       } else
08185          ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
08186    }
08187    for(;;) {
08188       for (i=0;i<NUM_DCHANS;i++) {
08189          if (!pri->dchannels[i])
08190             break;
08191          fds[i].fd = pri->fds[i];
08192          fds[i].events = POLLIN | POLLPRI;
08193          fds[i].revents = 0;
08194       }
08195       numdchans = i;
08196       time(&t);
08197       ast_mutex_lock(&pri->lock);
08198       if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->resetinterval > 0)) {
08199          if (pri->resetting && pri_is_up(pri)) {
08200             if (pri->resetpos < 0)
08201                pri_check_restart(pri);
08202          } else {
08203             if (!pri->resetting  && (t - pri->lastreset) >= pri->resetinterval) {
08204                pri->resetting = 1;
08205                pri->resetpos = -1;
08206             }
08207          }
08208       }
08209       /* Look for any idle channels if appropriate */
08210       if (doidling && pri_is_up(pri)) {
08211          nextidle = -1;
08212          haveidles = 0;
08213          activeidles = 0;
08214          for (x=pri->numchans;x>=0;x--) {
08215             if (pri->pvts[x] && !pri->pvts[x]->owner && 
08216                 !pri->pvts[x]->call) {
08217                if (haveidles < pri->minunused) {
08218                   haveidles++;
08219                } else if (!pri->pvts[x]->resetting) {
08220                   nextidle = x;
08221                   break;
08222                }
08223             } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
08224                activeidles++;
08225          }
08226          if (nextidle > -1) {
08227             if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
08228                /* Don't create a new idle call more than once per second */
08229                snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
08230                idle = zt_request("Zap", AST_FORMAT_ULAW, idlen, &cause);
08231                if (idle) {
08232                   pri->pvts[nextidle]->isidlecall = 1;
08233                   if (ast_pthread_create(&p, NULL, do_idle_thread, idle)) {
08234                      ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
08235                      zt_hangup(idle);
08236                   }
08237                } else
08238                   ast_log(LOG_WARNING, "Unable to request channel 'Zap/%s' for idle call\n", idlen);
08239                gettimeofday(&lastidle, NULL);
08240             }
08241          } else if ((haveidles < pri->minunused) &&
08242                (activeidles > pri->minidle)) {
08243             /* Mark something for hangup if there is something 
08244                that can be hungup */
08245             for (x=pri->numchans;x>=0;x--) {
08246                /* find a candidate channel */
08247                if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
08248                   pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08249                   haveidles++;
08250                   /* Stop if we have enough idle channels or
08251                     can't spare any more active idle ones */
08252                   if ((haveidles >= pri->minunused) ||
08253                       (activeidles <= pri->minidle))
08254                      break;
08255                } 
08256             }
08257          }
08258       }
08259       /* Start with reasonable max */
08260       lowest = ast_tv(60, 0);
08261       for (i=0; i<NUM_DCHANS; i++) {
08262          /* Find lowest available d-channel */
08263          if (!pri->dchannels[i])
08264             break;
08265          if ((next = pri_schedule_next(pri->dchans[i]))) {
08266             /* We need relative time here */
08267             tv = ast_tvsub(*next, ast_tvnow());
08268             if (tv.tv_sec < 0) {
08269                tv = ast_tv(0,0);
08270             }
08271             if (doidling || pri->resetting) {
08272                if (tv.tv_sec > 1) {
08273                   tv = ast_tv(1, 0);
08274                }
08275             } else {
08276                if (tv.tv_sec > 60) {
08277                   tv = ast_tv(60, 0);
08278                }
08279             }
08280          } else if (doidling || pri->resetting) {
08281             /* Make sure we stop at least once per second if we're
08282                monitoring idle channels */
08283             tv = ast_tv(1,0);
08284          } else {
08285             /* Don't poll for more than 60 seconds */
08286             tv = ast_tv(60, 0);
08287          }
08288          if (!i || ast_tvcmp(tv, lowest) < 0) {
08289             lowest = tv;
08290          }
08291       }
08292       ast_mutex_unlock(&pri->lock);
08293 
08294       e = NULL;
08295       res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
08296 
08297       ast_mutex_lock(&pri->lock);
08298       if (!res) {
08299          for (which=0;which<NUM_DCHANS;which++) {
08300             if (!pri->dchans[which])
08301                break;
08302             /* Just a timeout, run the scheduler */
08303             e = pri_schedule_run(pri->dchans[which]);
08304             if (e)
08305                break;
08306          }
08307       } else if (res > -1) {
08308          for (which=0;which<NUM_DCHANS;which++) {
08309             if (!pri->dchans[which])
08310                break;
08311             if (fds[which].revents & POLLPRI) {
08312                /* Check for an event */
08313                x = 0;
08314                res = ioctl(pri->fds[which], ZT_GETEVENT, &x);
08315                if (x) 
08316                   ast_log(LOG_NOTICE, "PRI got event: %s (%d) on %s D-channel of span %d\n", event2str(x), x, pri_order(which), pri->span);
08317                /* Keep track of alarm state */  
08318                if (x == ZT_EVENT_ALARM) {
08319                   pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
08320                   pri_find_dchan(pri);
08321                } else if (x == ZT_EVENT_NOALARM) {
08322                   pri->dchanavail[which] |= DCHAN_NOTINALARM;
08323                   pri_restart(pri->dchans[which]);
08324                }
08325             
08326                if (option_debug)
08327                   ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
08328             } else if (fds[which].revents & POLLIN) {
08329                e = pri_check_event(pri->dchans[which]);
08330             }
08331             if (e)
08332                break;
08333          }
08334       } else if (errno != EINTR)
08335          ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
08336 
08337       if (e) {
08338          if (pri->debug)
08339             pri_dump_event(pri->dchans[which], e);
08340          if (e->e != PRI_EVENT_DCHAN_DOWN)
08341             pri->dchanavail[which] |= DCHAN_UP;
08342          switch(e->e) {
08343          case PRI_EVENT_DCHAN_UP:
08344             if (option_verbose > 1) 
08345                ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span);
08346             pri->dchanavail[which] |= DCHAN_UP;
08347             if (!pri->pri) pri_find_dchan(pri);
08348 
08349             /* Note presense of D-channel */
08350             time(&pri->lastreset);
08351 
08352             /* Restart in 5 seconds */
08353             if (pri->resetinterval > -1) {
08354                pri->lastreset -= pri->resetinterval;
08355                pri->lastreset += 5;
08356             }
08357             pri->resetting = 0;
08358             /* Take the channels from inalarm condition */
08359             for (i=0; i<pri->numchans; i++)
08360                if (pri->pvts[i]) {
08361                   pri->pvts[i]->inalarm = 0;
08362                }
08363             break;
08364          case PRI_EVENT_DCHAN_DOWN:
08365             if (option_verbose > 1) 
08366                ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span);
08367             pri->dchanavail[which] &= ~DCHAN_UP;
08368             pri_find_dchan(pri);
08369             if (!pri_is_up(pri)) {
08370                pri->resetting = 0;
08371                /* Hangup active channels and put them in alarm mode */
08372                for (i=0; i<pri->numchans; i++) {
08373                   struct zt_pvt *p = pri->pvts[i];
08374                   if (p) {
08375                      if (p->call) {
08376                         if (p->pri && p->pri->pri) {
08377                            pri_hangup(p->pri->pri, p->call, -1);
08378                            pri_destroycall(p->pri->pri, p->call);
08379                            p->call = NULL;
08380                         } else
08381                            ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
08382                      }
08383                      if (p->realcall) {
08384                         pri_hangup_all(p->realcall, pri);
08385                      } else if (p->owner)
08386                         p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08387                      p->inalarm = 1;
08388                   }
08389                }
08390             }
08391             break;
08392          case PRI_EVENT_RESTART:
08393             if (e->restart.channel > -1) {
08394                chanpos = pri_find_principle(pri, e->restart.channel);
08395                if (chanpos < 0)
08396                   ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n", 
08397                      PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08398                else {
08399                   if (option_verbose > 2)
08400                      ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n", 
08401                         PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08402                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08403                   if (pri->pvts[chanpos]->call) {
08404                      pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
08405                      pri->pvts[chanpos]->call = NULL;
08406                   }
08407                   /* Force soft hangup if appropriate */
08408                   if (pri->pvts[chanpos]->realcall) 
08409                      pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08410                   else if (pri->pvts[chanpos]->owner)
08411                      pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08412                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08413                }
08414             } else {
08415                if (option_verbose > 2)
08416                   ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span);
08417                for (x=0;x < pri->numchans;x++)
08418                   if (pri->pvts[x]) {
08419                      ast_mutex_lock(&pri->pvts[x]->lock);
08420                      if (pri->pvts[x]->call) {
08421                         pri_destroycall(pri->pri, pri->pvts[x]->call);
08422                         pri->pvts[x]->call = NULL;
08423                      }
08424                      if (pri->pvts[chanpos]->realcall) 
08425                         pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08426                      else if (pri->pvts[x]->owner)
08427                         pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08428                      ast_mutex_unlock(&pri->pvts[x]->lock);
08429                   }
08430             }
08431             break;
08432          case PRI_EVENT_KEYPAD_DIGIT:
08433             chanpos = pri_find_principle(pri, e->digit.channel);
08434             if (chanpos < 0) {
08435                ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n", 
08436                   PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
08437             } else {
08438                chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
08439                if (chanpos > -1) {
08440                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08441                   /* queue DTMF frame if the PBX for this call was already started (we're forwarding KEYPAD_DIGITs further on */
08442                   if (pri->overlapdial && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
08443                      /* how to do that */
08444                      int digitlen = strlen(e->digit.digits);
08445                      char digit;
08446                      int i;               
08447                      for (i=0; i<digitlen; i++) {  
08448                         digit = e->digit.digits[i];
08449                         {
08450                            struct ast_frame f = { AST_FRAME_DTMF, digit, };
08451                            zap_queue_frame(pri->pvts[chanpos], &f, pri);
08452                         }
08453                      }
08454                   }
08455                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08456                }
08457             }
08458             break;
08459             
08460          case PRI_EVENT_INFO_RECEIVED:
08461             chanpos = pri_find_principle(pri, e->ring.channel);
08462             if (chanpos < 0) {
08463                ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n", 
08464                   PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08465             } else {
08466                chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
08467                if (chanpos > -1) {
08468                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08469                   /* queue DTMF frame if the PBX for this call was already started (we're forwarding INFORMATION further on */
08470                   if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
08471                      /* how to do that */
08472                      int digitlen = strlen(e->ring.callednum);
08473                      char digit;
08474                      int i;               
08475                      for (i=0; i<digitlen; i++) {  
08476                         digit = e->ring.callednum[i];
08477                         {
08478                            struct ast_frame f = { AST_FRAME_DTMF, digit, };
08479                            zap_queue_frame(pri->pvts[chanpos], &f, pri);
08480                         }
08481                      }
08482                   }
08483                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08484                }
08485             }
08486             break;
08487          case PRI_EVENT_RING:
08488             crv = NULL;
08489             if (e->ring.channel == -1)
08490                chanpos = pri_find_empty_chan(pri, 1);
08491             else
08492                chanpos = pri_find_principle(pri, e->ring.channel);
08493             /* if no channel specified find one empty */
08494             if (chanpos < 0) {
08495                ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n", 
08496                   PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08497             } else {
08498                ast_mutex_lock(&pri->pvts[chanpos]->lock);
08499                if (pri->pvts[chanpos]->owner) {
08500                   if (pri->pvts[chanpos]->call == e->ring.call) {
08501                      ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n", 
08502                         PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08503                      break;
08504                   } else {
08505                      ast_log(LOG_WARNING, "Ring requested on channel %d/%d already in use on span %d.  Hanging up owner.\n", 
08506                      PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08507                      if (pri->pvts[chanpos]->realcall) 
08508                         pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08509                      else
08510                         pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08511                      ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08512                      chanpos = -1;
08513                   }
08514                }
08515                if (chanpos > -1)
08516                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08517             }
08518             if ((chanpos < 0) && (e->ring.flexible))
08519                chanpos = pri_find_empty_chan(pri, 1);
08520             if (chanpos > -1) {
08521                ast_mutex_lock(&pri->pvts[chanpos]->lock);
08522                if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
08523                   /* Should be safe to lock CRV AFAIK while bearer is still locked */
08524                   crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
08525                   if (crv)
08526                      ast_mutex_lock(&crv->lock);
08527                   if (!crv || crv->owner) {
08528                      pri->pvts[chanpos]->call = NULL;
08529                      if (crv) {
08530                         if (crv->owner)
08531                            crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08532                         ast_log(LOG_WARNING, "Call received for busy CRV %d on span %d\n", pri_get_crv(pri->pri, e->ring.call, NULL), pri->span);
08533                      } else
08534                         ast_log(LOG_NOTICE, "Call received for unconfigured CRV %d on span %d\n", pri_get_crv(pri->pri, e->ring.call, NULL), pri->span);
08535                      pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
08536                      if (crv)
08537                         ast_mutex_unlock(&crv->lock);
08538                      ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08539                      break;
08540                   }
08541                }
08542                pri->pvts[chanpos]->call = e->ring.call;
08543                apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
08544                if (pri->pvts[chanpos]->use_callerid) {
08545                   ast_shrink_phone_number(plancallingnum);
08546                   ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
08547 #ifdef PRI_ANI
08548                   if (!ast_strlen_zero(e->ring.callingani)) {
08549                      apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
08550                      ast_shrink_phone_number(plancallingani);
08551                      ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
08552                   } else {
08553                      pri->pvts[chanpos]->cid_ani[0] = '\0';
08554                   }
08555 #endif
08556                   ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
08557                   pri->pvts[chanpos]->cid_ton = e->ring.callingplan; /* this is the callingplan (TON/NPI), e->ring.callingplan>>4 would be the TON */
08558                } else {
08559                   pri->pvts[chanpos]->cid_num[0] = '\0';
08560                   pri->pvts[chanpos]->cid_ani[0] = '\0';
08561                   pri->pvts[chanpos]->cid_name[0] = '\0';
08562                   pri->pvts[chanpos]->cid_ton = 0;
08563                }
08564                apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
08565                           e->ring.redirectingnum, e->ring.callingplanrdnis);
08566                /* If immediate=yes go to s|1 */
08567                if (pri->pvts[chanpos]->immediate) {
08568                   if (option_verbose > 2)
08569                      ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n");
08570                   pri->pvts[chanpos]->exten[0] = 's';
08571                   pri->pvts[chanpos]->exten[1] = '\0';
08572                }
08573                /* Get called number */
08574                else if (!ast_strlen_zero(e->ring.callednum)) {
08575                   ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
08576                   ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08577                } else
08578                   pri->pvts[chanpos]->exten[0] = '\0';
08579                /* Set DNID on all incoming calls -- even immediate */
08580                if (!ast_strlen_zero(e->ring.callednum))
08581                   ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08582                /* No number yet, but received "sending complete"? */
08583                if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
08584                   if (option_verbose > 2)
08585                      ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n");
08586                   pri->pvts[chanpos]->exten[0] = 's';
08587                   pri->pvts[chanpos]->exten[1] = '\0';
08588                }
08589                /* Make sure extension exists (or in overlap dial mode, can exist) */
08590                if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
08591                   ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08592                   /* Setup law */
08593                   int law;
08594                   if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
08595                      /* Set to audio mode at this point */
08596                      law = 1;
08597                      if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &law) == -1)
08598                         ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", pri->pvts[chanpos]->channel, law);
08599                   }
08600                   if (e->ring.layer1 == PRI_LAYER_1_ALAW)
08601                      law = ZT_LAW_ALAW;
08602                   else
08603                      law = ZT_LAW_MULAW;
08604                   res = zt_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].zfd, law);
08605                   if (res < 0) 
08606                      ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
08607                   res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].zfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
08608                   if (res < 0)
08609                      ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
08610                   if (e->ring.complete || !pri->overlapdial) {
08611                      /* Just announce proceeding */
08612                      pri->pvts[chanpos]->proceeding = 1;
08613                      pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
08614                   } else  {
08615                      if (pri->switchtype != PRI_SWITCH_GR303_TMC) 
08616                         pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08617                      else
08618                         pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08619                   }
08620                   /* Get the use_callingpres state */
08621                   pri->pvts[chanpos]->callingpres = e->ring.callingpres;
08622                
08623                   /* Start PBX */
08624                   if (pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08625                      /* Release the PRI lock while we create the channel */
08626                      ast_mutex_unlock(&pri->lock);
08627                      if (crv) {
08628                         /* Set bearer and such */
08629                         pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
08630                         c = zt_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08631                         pri->pvts[chanpos]->owner = &inuse;
08632                         ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
08633                      } else {
08634                         c = zt_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08635                      }
08636                      if (!ast_strlen_zero(e->ring.callingsubaddr)) {
08637                         pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
08638                      }
08639                      if(e->ring.ani2 >= 0) {
08640                         snprintf(ani2str, 5, "%.2d", e->ring.ani2);
08641                         pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08642                      }
08643 
08644 #ifdef SUPPORT_USERUSER
08645                      if (!ast_strlen_zero(e->ring.useruserinfo)) {
08646                         pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08647                      }
08648 #endif
08649 
08650                      snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08651                      pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08652                      if (e->ring.redirectingreason >= 0)
08653                         pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08654                      
08655                      ast_mutex_lock(&pri->lock);
08656                      if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) {
08657                         if (option_verbose > 2)
08658                            ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
08659                               plancallingnum, !ast_strlen_zero(pri->pvts[chanpos]->exten) ? pri->pvts[chanpos]->exten : "<unspecified>", 
08660                               pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08661                      } else {
08662                         ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n", 
08663                            pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08664                         if (c)
08665                            ast_hangup(c);
08666                         else {
08667                            pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08668                            pri->pvts[chanpos]->call = NULL;
08669                         }
08670                      }
08671                   } else  {
08672                      ast_mutex_unlock(&pri->lock);
08673                      /* Release PRI lock while we create the channel */
08674                      c = zt_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype);
08675                      ast_mutex_lock(&pri->lock);
08676                      if (c) {
08677                         char calledtonstr[10];
08678                         if(e->ring.ani2 >= 0) {
08679                            snprintf(ani2str, 5, "%d", e->ring.ani2);
08680                            pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08681                         }
08682 
08683 #ifdef SUPPORT_USERUSER
08684                         if (!ast_strlen_zero(e->ring.useruserinfo)) {
08685                            pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08686                         }
08687 #endif
08688 
08689                         if (e->ring.redirectingreason >= 0)
08690                            pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08691                      
08692                         snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08693                         pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08694                         if (option_verbose > 2)
08695                            ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
08696                               plancallingnum, pri->pvts[chanpos]->exten, 
08697                                  pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08698                         zt_enable_ec(pri->pvts[chanpos]);
08699                      } else {
08700                         ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n", 
08701                            pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08702                         pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08703                         pri->pvts[chanpos]->call = NULL;
08704                      }
08705                   }
08706                } else {
08707                   if (option_verbose > 2)
08708                      ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist.  Rejecting call on channel %d/%d, span %d\n",
08709                         pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan, 
08710                            pri->pvts[chanpos]->prioffset, pri->span);
08711                   pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
08712                   pri->pvts[chanpos]->call = NULL;
08713                   pri->pvts[chanpos]->exten[0] = '\0';
08714                }
08715                if (crv)
08716                   ast_mutex_unlock(&crv->lock);
08717                ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08718             } else 
08719                pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
08720             break;
08721          case PRI_EVENT_RINGING:
08722             chanpos = pri_find_principle(pri, e->ringing.channel);
08723             if (chanpos < 0) {
08724                ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n", 
08725                   PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08726                chanpos = -1;
08727             }
08728             if (chanpos > -1) {
08729                chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
08730                if (chanpos < 0) {
08731                   ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n", 
08732                      PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08733                   chanpos = -1;
08734                } else {
08735                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08736                   if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08737                      zt_enable_ec(pri->pvts[chanpos]);
08738                      pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1;
08739                      pri->pvts[chanpos]->alerting = 1;
08740                   } else
08741                      ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n");
08742 #ifdef PRI_PROGRESS_MASK
08743                   if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08744 #else
08745                   if (e->ringing.progress == 8) {
08746 #endif
08747                      /* Now we can do call progress detection */
08748                      if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08749                         /* RINGING detection isn't required because we got ALERTING signal */
08750                         ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING);
08751                         pri->pvts[chanpos]->dsp_features = 0;
08752                      }
08753                   }
08754 
08755 #ifdef SUPPORT_USERUSER
08756                   if (!ast_strlen_zero(e->ringing.useruserinfo)) {
08757                      pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->ringing.useruserinfo);
08758                   }
08759 #endif
08760 
08761                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08762                }
08763             }
08764             break;
08765          case PRI_EVENT_PROGRESS:
08766             /* Get chan value if e->e is not PRI_EVNT_RINGING */
08767             chanpos = pri_find_principle(pri, e->proceeding.channel);
08768             if (chanpos > -1) {
08769 #ifdef PRI_PROGRESS_MASK
08770                if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) {
08771 #else
08772                if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) {
08773 #endif
08774                   struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
08775 
08776                   if (e->proceeding.cause > -1) {
08777                      if (option_verbose > 2)
08778                         ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with cause code %d received\n", e->proceeding.cause);
08779 
08780                      /* Work around broken, out of spec USER_BUSY cause in a progress message */
08781                      if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
08782                         if (pri->pvts[chanpos]->owner) {
08783                            if (option_verbose > 2)
08784                               ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
08785 
08786                            pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
08787                            f.subclass = AST_CONTROL_BUSY;
08788                         }
08789                      }
08790                   }
08791                   
08792                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08793                   ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
08794                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08795                   zap_queue_frame(pri->pvts[chanpos], &f, pri);
08796 #ifdef PRI_PROGRESS_MASK
08797                   if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08798 #else
08799                   if (e->proceeding.progress == 8) {
08800 #endif
08801                      /* Now we can do call progress detection */
08802                      if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08803                         ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08804                         pri->pvts[chanpos]->dsp_features = 0;
08805                      }
08806                   }
08807                   pri->pvts[chanpos]->progress = 1;
08808                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08809                }
08810             }
08811             break;
08812          case PRI_EVENT_PROCEEDING:
08813             chanpos = pri_find_principle(pri, e->proceeding.channel);
08814             if (chanpos > -1) {
08815                if (!pri->pvts[chanpos]->proceeding) {
08816                   struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
08817                   
08818                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08819                   ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
08820                         pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08821                   zap_queue_frame(pri->pvts[chanpos], &f, pri);
08822 #ifdef PRI_PROGRESS_MASK
08823                   if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08824 #else
08825                   if (e->proceeding.progress == 8) {
08826 #endif
08827                      /* Now we can do call progress detection */
08828                      if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08829                         ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08830                         pri->pvts[chanpos]->dsp_features = 0;
08831                      }
08832                      /* Bring voice path up */
08833                      f.subclass = AST_CONTROL_PROGRESS;
08834                      zap_queue_frame(pri->pvts[chanpos], &f, pri);
08835                   }
08836                   pri->pvts[chanpos]->proceeding = 1;
08837                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08838                }
08839             }
08840             break;
08841          case PRI_EVENT_FACNAME:
08842             chanpos = pri_find_principle(pri, e->facname.channel);
08843             if (chanpos < 0) {
08844                ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n", 
08845                   PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08846                chanpos = -1;
08847             }
08848             if (chanpos > -1) {
08849                chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
08850                if (chanpos < 0) {
08851                   ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n", 
08852                      PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08853                   chanpos = -1;
08854                } else {
08855                   /* Re-use *69 field for PRI */
08856                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08857                   ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
08858                   ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
08859                   pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
08860                   zt_enable_ec(pri->pvts[chanpos]);
08861                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08862                }
08863             }
08864             break;            
08865          case PRI_EVENT_ANSWER:
08866             chanpos = pri_find_principle(pri, e->answer.channel);
08867             if (chanpos < 0) {
08868                ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n", 
08869                   PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
08870                chanpos = -1;
08871             }
08872             if (chanpos > -1) {
08873                chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
08874                if (chanpos < 0) {
08875                   ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n", 
08876                      PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
08877                   chanpos = -1;
08878                } else {
08879                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08880                   /* Now we can do call progress detection */
08881 
08882                   /* We changed this so it turns on the DSP no matter what... progress or no progress.
08883                    * By this time, we need DTMF detection and other features that were previously disabled
08884                    * -- Matt F */
08885                   if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08886                      ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08887                      pri->pvts[chanpos]->dsp_features = 0;
08888                   }
08889                   if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) {
08890                      ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n");
08891                      x = ZT_START;
08892                      res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_HOOK, &x);
08893                      if (res < 0) {
08894                         if (errno != EINPROGRESS) {
08895                            ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
08896                         }
08897                      }
08898                   } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08899                      pri->pvts[chanpos]->dialing = 1;
08900                      /* Send any "w" waited stuff */
08901                      res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_DIAL, &pri->pvts[chanpos]->dop);
08902                      if (res < 0) {
08903                         ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", pri->pvts[chanpos]->channel);
08904                         pri->pvts[chanpos]->dop.dialstr[0] = '\0';
08905                      } else 
08906                         ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
08907                      pri->pvts[chanpos]->dop.dialstr[0] = '\0';
08908                   } else if (pri->pvts[chanpos]->confirmanswer) {
08909                      ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
08910                   } else {
08911                      pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
08912                      /* Enable echo cancellation if it's not on already */
08913                      zt_enable_ec(pri->pvts[chanpos]);
08914                   }
08915 
08916 #ifdef SUPPORT_USERUSER
08917                   if (!ast_strlen_zero(e->answer.useruserinfo)) {
08918                      pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->answer.useruserinfo);
08919                   }
08920 #endif
08921 
08922                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08923                }
08924             }
08925             break;            
08926          case PRI_EVENT_HANGUP:
08927             chanpos = pri_find_principle(pri, e->hangup.channel);
08928             if (chanpos < 0) {
08929                ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n", 
08930                   PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08931                chanpos = -1;
08932             }
08933             if (chanpos > -1) {
08934                chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
08935                if (chanpos > -1) {
08936                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
08937                   if (!pri->pvts[chanpos]->alreadyhungup) {
08938                      /* we're calling here zt_hangup so once we get there we need to clear p->call after calling pri_hangup */
08939                      pri->pvts[chanpos]->alreadyhungup = 1;
08940                      if (pri->pvts[chanpos]->realcall) 
08941                         pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08942                      else if (pri->pvts[chanpos]->owner) {
08943                         /* Queue a BUSY instead of a hangup if our cause is appropriate */
08944                         pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
08945                         switch(e->hangup.cause) {
08946                         case PRI_CAUSE_USER_BUSY:
08947                            pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
08948                            break;
08949                         case PRI_CAUSE_CALL_REJECTED:
08950                         case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
08951                         case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
08952                         case PRI_CAUSE_SWITCH_CONGESTION:
08953                         case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
08954                         case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
08955                            pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
08956                            break;
08957                         default:
08958                            pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08959                         }
08960                      }
08961                      if (option_verbose > 2) 
08962                         ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup\n", 
08963                            pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08964                   } else {
08965                      pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
08966                      pri->pvts[chanpos]->call = NULL;
08967                   }
08968                   if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
08969                      if (option_verbose > 2)
08970                         ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n", 
08971                            PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08972                      pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
08973                      pri->pvts[chanpos]->resetting = 1;
08974                   }
08975                   if (e->hangup.aoc_units > -1)
08976                      if (option_verbose > 2)
08977                         ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
08978                            pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
08979 
08980 #ifdef SUPPORT_USERUSER
08981                   if (!ast_strlen_zero(e->hangup.useruserinfo)) {
08982                      pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
08983                   }
08984 #endif
08985 
08986                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08987                } else {
08988                   ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n", 
08989                      PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08990                }
08991             } 
08992             break;
08993 #ifndef PRI_EVENT_HANGUP_REQ
08994 #error please update libpri
08995 #endif
08996          case PRI_EVENT_HANGUP_REQ:
08997             chanpos = pri_find_principle(pri, e->hangup.channel);
08998             if (chanpos < 0) {
08999                ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n", 
09000                   PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09001                chanpos = -1;
09002             }
09003             if (chanpos > -1) {
09004                chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09005                if (chanpos > -1) {
09006                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
09007                   if (pri->pvts[chanpos]->realcall) 
09008                      pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09009                   else if (pri->pvts[chanpos]->owner) {
09010                      pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
09011                      switch(e->hangup.cause) {
09012                      case PRI_CAUSE_USER_BUSY:
09013                         pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
09014                         break;
09015                      case PRI_CAUSE_CALL_REJECTED:
09016                      case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
09017                      case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
09018                      case PRI_CAUSE_SWITCH_CONGESTION:
09019                      case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
09020                      case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
09021                         pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
09022                         break;
09023                      default:
09024                         pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09025                      }
09026                      if (option_verbose > 2) 
09027                         ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup request\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09028                      if (e->hangup.aoc_units > -1)
09029                         if (option_verbose > 2)
09030                            ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
09031                               pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
09032                   } else {
09033                      pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
09034                      pri->pvts[chanpos]->call = NULL;
09035                   }
09036                   if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
09037                      if (option_verbose > 2)
09038                         ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n", 
09039                            PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09040                      pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
09041                      pri->pvts[chanpos]->resetting = 1;
09042                   }
09043 
09044 #ifdef SUPPORT_USERUSER
09045                   if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09046                      pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
09047                   }
09048 #endif
09049 
09050                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09051                } else {
09052                   ast_log(LOG_WARNING, "Hangup REQ on bad channel %d/%d on span %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09053                }
09054             } 
09055             break;
09056          case PRI_EVENT_HANGUP_ACK:
09057             chanpos = pri_find_principle(pri, e->hangup.channel);
09058             if (chanpos < 0) {
09059                ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n", 
09060                   PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09061                chanpos = -1;
09062             }
09063             if (chanpos > -1) {
09064                chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09065                if (chanpos > -1) {
09066                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
09067                   pri->pvts[chanpos]->call = NULL;
09068                   pri->pvts[chanpos]->resetting = 0;
09069                   if (pri->pvts[chanpos]->owner) {
09070                      if (option_verbose > 2) 
09071                         ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09072                   }
09073 
09074 #ifdef SUPPORT_USERUSER
09075                   if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09076                      pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
09077                   }
09078 #endif
09079 
09080                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09081                }
09082             }
09083             break;
09084          case PRI_EVENT_CONFIG_ERR:
09085             ast_log(LOG_WARNING, "PRI Error: %s\n", e->err.err);
09086             break;
09087          case PRI_EVENT_RESTART_ACK:
09088             chanpos = pri_find_principle(pri, e->restartack.channel);
09089             if (chanpos < 0) {
09090                /* Sometime switches (e.g. I421 / British Telecom) don't give us the
09091                   channel number, so we have to figure it out...  This must be why
09092                   everybody resets exactly a channel at a time. */
09093                for (x=0;x<pri->numchans;x++) {
09094                   if (pri->pvts[x] && pri->pvts[x]->resetting) {
09095                      chanpos = x;
09096                      ast_mutex_lock(&pri->pvts[chanpos]->lock);
09097                      ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan, 
09098                            pri->pvts[chanpos]->prioffset, pri->span);
09099                      if (pri->pvts[chanpos]->realcall) 
09100                         pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09101                      else if (pri->pvts[chanpos]->owner) {
09102                         ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan, 
09103                            pri->pvts[chanpos]->prioffset, pri->span);
09104                         pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09105                      }
09106                      pri->pvts[chanpos]->resetting = 0;
09107                      if (option_verbose > 2)
09108                         ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan, 
09109                            pri->pvts[chanpos]->prioffset, pri->span);
09110                      ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09111                      if (pri->resetting)
09112                         pri_check_restart(pri);
09113                      break;
09114                   }
09115                }
09116                if (chanpos < 0) {
09117                   ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n", 
09118                      PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09119                }
09120                chanpos = -1;
09121             }
09122             if (chanpos > -1) {
09123                if (pri->pvts[chanpos]) {
09124                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
09125                   if (pri->pvts[chanpos]->realcall) 
09126                      pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09127                   else if (pri->pvts[chanpos]->owner) {
09128                      ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
09129                         PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09130                      pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09131                   }
09132                   pri->pvts[chanpos]->resetting = 0;
09133                   if (option_verbose > 2)
09134                      ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan, 
09135                            pri->pvts[chanpos]->prioffset, pri->span);
09136                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09137                   if (pri->resetting)
09138                      pri_check_restart(pri);
09139                }
09140             }
09141             break;
09142          case PRI_EVENT_SETUP_ACK:
09143             chanpos = pri_find_principle(pri, e->setup_ack.channel);
09144             if (chanpos < 0) {
09145                ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n", 
09146                   PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
09147             } else {
09148                chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
09149                if (chanpos > -1) {
09150                   ast_mutex_lock(&pri->pvts[chanpos]->lock);
09151                   pri->pvts[chanpos]->setup_ack = 1;
09152                   /* Send any queued digits */
09153                   for (x=0;x<strlen(pri->pvts[chanpos]->dialdest);x++) {
09154                      ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
09155                      pri_information(pri->pri, pri->pvts[chanpos]->call, 
09156                         pri->pvts[chanpos]->dialdest[x]);
09157                   }
09158                   ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09159                } else
09160                   ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
09161             }
09162             break;
09163          case PRI_EVENT_NOTIFY:
09164             chanpos = pri_find_principle(pri, e->notify.channel);
09165             if (chanpos < 0) {
09166                ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
09167                   PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
09168             } else {
09169                struct ast_frame f = { AST_FRAME_CONTROL, };
09170                ast_mutex_lock(&pri->pvts[chanpos]->lock);
09171                switch(e->notify.info) {
09172                case PRI_NOTIFY_REMOTE_HOLD:
09173                   f.subclass = AST_CONTROL_HOLD;
09174                   zap_queue_frame(pri->pvts[chanpos], &f, pri);
09175                   break;
09176                case PRI_NOTIFY_REMOTE_RETRIEVAL:
09177                   f.subclass = AST_CONTROL_UNHOLD;
09178                   zap_queue_frame(pri->pvts[chanpos], &f, pri);
09179                   break;
09180                }
09181                ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09182             }
09183             break;
09184          default:
09185             ast_log(LOG_DEBUG, "Event: %d\n", e->e);
09186          }
09187       }  
09188       ast_mutex_unlock(&pri->lock);
09189    }
09190    /* Never reached */
09191    return NULL;
09192 }
09193 
09194 static int start_pri(struct zt_pri *pri)
09195 {
09196    int res, x;
09197    ZT_PARAMS p;
09198    ZT_BUFFERINFO bi;
09199    struct zt_spaninfo si;
09200    int i;
09201    
09202    for (i=0;i<NUM_DCHANS;i++) {
09203       if (!pri->dchannels[i])
09204          break;
09205       pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600);
09206       x = pri->dchannels[i];
09207       if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],ZT_SPECIFY,&x) == -1)) {
09208          ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
09209          return -1;
09210       }
09211       res = ioctl(pri->fds[i], ZT_GET_PARAMS, &p);
09212       if (res) {
09213          zt_close(pri->fds[i]);
09214          pri->fds[i] = -1;
09215          ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
09216          return -1;
09217       }
09218       if (p.sigtype != ZT_SIG_HDLCFCS) {
09219          zt_close(pri->fds[i]);
09220          pri->fds[i] = -1;
09221          ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode.  See /etc/zaptel.conf\n", x);
09222          return -1;
09223       }
09224       memset(&si, 0, sizeof(si));
09225       res = ioctl(pri->fds[i], ZT_SPANSTAT, &si);
09226       if (res) {
09227          zt_close(pri->fds[i]);
09228          pri->fds[i] = -1;
09229          ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
09230       }
09231       if (!si.alarms)
09232          pri->dchanavail[i] |= DCHAN_NOTINALARM;
09233       else
09234          pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
09235       bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
09236       bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
09237       bi.numbufs = 32;
09238       bi.bufsize = 1024;
09239       if (ioctl(pri->fds[i], ZT_SET_BUFINFO, &bi)) {
09240          ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d\n", x);
09241          zt_close(pri->fds[i]);
09242          pri->fds[i] = -1;
09243          return -1;
09244       }
09245       pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
09246       /* Force overlap dial if we're doing GR-303! */
09247       if (pri->switchtype == PRI_SWITCH_GR303_TMC)
09248          pri->overlapdial = 1;
09249       pri_set_overlapdial(pri->dchans[i],pri->overlapdial);
09250       /* Enslave to master if appropriate */
09251       if (i)
09252          pri_enslave(pri->dchans[0], pri->dchans[i]);
09253       if (!pri->dchans[i]) {
09254          zt_close(pri->fds[i]);
09255          pri->fds[i] = -1;
09256          ast_log(LOG_ERROR, "Unable to create PRI structure\n");
09257          return -1;
09258       }
09259       pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
09260       pri_set_nsf(pri->dchans[i], pri->nsf);
09261 #ifdef PRI_GETSET_TIMERS
09262       for (x = 0; x < PRI_MAX_TIMERS; x++) {
09263          if (pritimers[x] != 0)
09264             pri_set_timer(pri->dchans[i], x, pritimers[x]);
09265       }
09266 #endif
09267    }
09268    /* Assume primary is the one we use */
09269    pri->pri = pri->dchans[0];
09270    pri->resetpos = -1;
09271    if (ast_pthread_create(&pri->master, NULL, pri_dchannel, pri)) {
09272       for (i=0;i<NUM_DCHANS;i++) {
09273          if (!pri->dchannels[i])
09274             break;
09275          zt_close(pri->fds[i]);
09276          pri->fds[i] = -1;
09277       }
09278       ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
09279       return -1;
09280    }
09281    return 0;
09282 }
09283 
09284 static char *complete_span_helper(char *line, char *word, int pos, int state, int rpos)
09285 {
09286    int span=1;
09287    char tmp[50];
09288    if (pos != rpos)
09289       return 0;
09290    while(span <= NUM_SPANS) {
09291       if (span > state && pris[span-1].pri)
09292          break;
09293       span++;
09294    }
09295    if (span <= NUM_SPANS) {
09296       snprintf(tmp, sizeof(tmp), "%d", span);
09297       return strdup(tmp);
09298    } else
09299       return NULL;
09300 }
09301 
09302 static char *complete_span_4(char *line, char *word, int pos, int state)
09303 {
09304    return complete_span_helper(line,word,pos,state,3);
09305 }
09306 
09307 static char *complete_span_5(char *line, char *word, int pos, int state)
09308 {
09309    return complete_span_helper(line,word,pos,state,4);
09310 }
09311 
09312 static int handle_pri_set_debug_file(int fd, int argc, char **argv)
09313 {
09314    int myfd;
09315 
09316    if (!strncasecmp(argv[1], "set", 3)) {
09317       if (argc < 5) 
09318          return RESULT_SHOWUSAGE;
09319 
09320       if (ast_strlen_zero(argv[4]))
09321          return RESULT_SHOWUSAGE;
09322 
09323       myfd = open(argv[4], O_CREAT|O_WRONLY);
09324       if (myfd < 0) {
09325          ast_cli(fd, "Unable to open '%s' for writing\n", argv[4]);
09326          return RESULT_SUCCESS;
09327       }
09328 
09329       ast_mutex_lock(&pridebugfdlock);
09330 
09331       if (pridebugfd >= 0)
09332          close(pridebugfd);
09333 
09334       pridebugfd = myfd;
09335       ast_copy_string(pridebugfilename,argv[4],sizeof(pridebugfilename));
09336       
09337       ast_mutex_unlock(&pridebugfdlock);
09338 
09339       ast_cli(fd, "PRI debug output will be sent to '%s'\n", argv[4]);
09340    } else {
09341       /* Assume it is unset */
09342       ast_mutex_lock(&pridebugfdlock);
09343       close(pridebugfd);
09344       pridebugfd = -1;
09345       ast_cli(fd, "PRI debug output to file disabled\n");
09346       ast_mutex_unlock(&pridebugfdlock);
09347    }
09348 
09349    return RESULT_SUCCESS;
09350 }
09351 
09352 static int handle_pri_debug(int fd, int argc, char *argv[])
09353 {
09354    int span;
09355    int x;
09356    if (argc < 4) {
09357       return RESULT_SHOWUSAGE;
09358    }
09359    span = atoi(argv[3]);
09360    if ((span < 1) || (span > NUM_SPANS)) {
09361       ast_cli(fd, "Invalid span %s.  Should be a number %d to %d\n", argv[3], 1, NUM_SPANS);
09362       return RESULT_SUCCESS;
09363    }
09364    if (!pris[span-1].pri) {
09365       ast_cli(fd, "No PRI running on span %d\n", span);
09366       return RESULT_SUCCESS;
09367    }
09368    for (x=0;x<NUM_DCHANS;x++) {
09369       if (pris[span-1].dchans[x])
09370          pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE);
09371    }
09372    ast_cli(fd, "Enabled debugging on span %d\n", span);
09373    return RESULT_SUCCESS;
09374 }
09375 
09376 
09377 
09378 static int handle_pri_no_debug(int fd, int argc, char *argv[])
09379 {
09380    int span;
09381    int x;
09382    if (argc < 5)
09383       return RESULT_SHOWUSAGE;
09384    span = atoi(argv[4]);
09385    if ((span < 1) || (span > NUM_SPANS)) {
09386       ast_cli(fd, "Invalid span %s.  Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09387       return RESULT_SUCCESS;
09388    }
09389    if (!pris[span-1].pri) {
09390       ast_cli(fd, "No PRI running on span %d\n", span);
09391       return RESULT_SUCCESS;
09392    }
09393    for (x=0;x<NUM_DCHANS;x++) {
09394       if (pris[span-1].dchans[x])
09395          pri_set_debug(pris[span-1].dchans[x], 0);
09396    }
09397    ast_cli(fd, "Disabled debugging on span %d\n", span);
09398    return RESULT_SUCCESS;
09399 }
09400 
09401 static int handle_pri_really_debug(int fd, int argc, char *argv[])
09402 {
09403    int span;
09404    int x;
09405    if (argc < 5)
09406       return RESULT_SHOWUSAGE;
09407    span = atoi(argv[4]);
09408    if ((span < 1) || (span > NUM_SPANS)) {
09409       ast_cli(fd, "Invalid span %s.  Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09410       return RESULT_SUCCESS;
09411    }
09412    if (!pris[span-1].pri) {
09413       ast_cli(fd, "No PRI running on span %d\n", span);
09414       return RESULT_SUCCESS;
09415    }
09416    for (x=0;x<NUM_DCHANS;x++) {
09417       if (pris[span-1].dchans[x])
09418          pri_set_debug(pris[span-1].dchans[x], (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE));
09419    }
09420    ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span);
09421    return RESULT_SUCCESS;
09422 }
09423 
09424 static void build_status(char *s, size_t len, int status, int active)
09425 {
09426    if (!s || len < 1) {
09427       return;
09428    }
09429    s[0] = '\0';
09430    if (status & DCHAN_PROVISIONED)
09431       strncat(s, "Provisioned, ", len - strlen(s) - 1);
09432    if (!(status & DCHAN_NOTINALARM))
09433       strncat(s, "In Alarm, ", len - strlen(s) - 1);
09434    if (status & DCHAN_UP)
09435       strncat(s, "Up", len - strlen(s) - 1);
09436    else
09437       strncat(s, "Down", len - strlen(s) - 1);
09438    if (active)
09439       strncat(s, ", Active", len - strlen(s) - 1);
09440    else
09441       strncat(s, ", Standby", len - strlen(s) - 1);
09442    s[len - 1] = '\0';
09443 }
09444 
09445 static int handle_pri_show_span(int fd, int argc, char *argv[])
09446 {
09447    int span;
09448    int x;
09449    char status[256];
09450    if (argc < 4)
09451       return RESULT_SHOWUSAGE;
09452    span = atoi(argv[3]);
09453    if ((span < 1) || (span > NUM_SPANS)) {
09454       ast_cli(fd, "Invalid span %s.  Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09455       return RESULT_SUCCESS;
09456    }
09457    if (!pris[span-1].pri) {
09458       ast_cli(fd, "No PRI running on span %d\n", span);
09459       return RESULT_SUCCESS;
09460    }
09461    for(x=0;x<NUM_DCHANS;x++) {
09462       if (pris[span-1].dchannels[x]) {
09463 #ifdef PRI_DUMP_INFO_STR
09464          char *info_str = NULL;
09465 #endif
09466          ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
09467          build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
09468          ast_cli(fd, "Status: %s\n", status);
09469 #ifdef PRI_DUMP_INFO_STR
09470          info_str = pri_dump_info_str(pris[span-1].pri);
09471          if (info_str) {
09472             ast_cli(fd, "%s", info_str);
09473             free(info_str);
09474          }
09475 #else
09476          pri_dump_info(pris[span-1].pri);
09477 #endif
09478          ast_cli(fd, "\n");
09479       }
09480    }
09481    return RESULT_SUCCESS;
09482 }
09483 
09484 static int handle_pri_show_debug(int fd, int argc, char *argv[])
09485 {
09486    int x;
09487    int span;
09488    int count=0;
09489    int debug=0;
09490 
09491    for(span=0;span<NUM_SPANS;span++) {
09492            if (pris[span].pri) {
09493          for(x=0;x<NUM_DCHANS;x++) {
09494             debug=0;
09495                if (pris[span].dchans[x]) {
09496                   debug = pri_get_debug(pris[span].dchans[x]);
09497                ast_cli(fd, "Span %d: Debug: %s\tIntense: %s\n", span+1, (debug&PRI_DEBUG_Q931_STATE)? "Yes" : "No" ,(debug&PRI_DEBUG_Q921_RAW)? "Yes" : "No" );
09498                count++;
09499             }
09500          }
09501       }
09502 
09503    }
09504    ast_mutex_lock(&pridebugfdlock);
09505    if (pridebugfd >= 0) 
09506       ast_cli(fd, "Logging PRI debug to file %s\n", pridebugfilename);
09507    ast_mutex_unlock(&pridebugfdlock);
09508        
09509    if (!count) 
09510       ast_cli(fd, "No debug set or no PRI running\n");
09511    return RESULT_SUCCESS;
09512 }
09513 
09514 static char pri_debug_help[] = 
09515    "Usage: pri debug span <span>\n"
09516    "       Enables debugging on a given PRI span\n";
09517    
09518 static char pri_no_debug_help[] = 
09519    "Usage: pri no debug span <span>\n"
09520    "       Disables debugging on a given PRI span\n";
09521 
09522 static char pri_really_debug_help[] = 
09523    "Usage: pri intensive debug span <span>\n"
09524    "       Enables debugging down to the Q.921 level\n";
09525 
09526 static char pri_show_span_help[] = 
09527    "Usage: pri show span <span>\n"
09528    "       Displays PRI Information\n";
09529 
09530 static struct ast_cli_entry zap_pri_cli[] = {
09531    { { "pri", "debug", "span", NULL }, handle_pri_debug,
09532      "Enables PRI debugging on a span", pri_debug_help, complete_span_4 },
09533    { { "pri", "no", "debug", "span", NULL }, handle_pri_no_debug,
09534      "Disables PRI debugging on a span", pri_no_debug_help, complete_span_5 },
09535    { { "pri", "intense", "debug", "span", NULL }, handle_pri_really_debug,
09536      "Enables REALLY INTENSE PRI debugging", pri_really_debug_help, complete_span_5 },
09537    { { "pri", "show", "span", NULL }, handle_pri_show_span,
09538      "Displays PRI Information", pri_show_span_help, complete_span_4 },
09539    { { "pri", "show", "debug", NULL }, handle_pri_show_debug,
09540      "Displays current PRI debug settings" },
09541    { { "pri", "set", "debug", "file", NULL }, handle_pri_set_debug_file,
09542      "Sends PRI debug output to the specified file" },
09543    { { "pri", "unset", "debug", "file", NULL }, handle_pri_set_debug_file,
09544      "Ends PRI debug output to file" },
09545 };
09546 
09547 #endif /* ZAPATA_PRI */
09548 
09549 
09550 #ifdef ZAPATA_R2
09551 static int handle_r2_no_debug(int fd, int argc, char *argv[])
09552 {
09553    int chan;
09554    struct zt_pvt *tmp = NULL;;
09555    if (argc < 5)
09556       return RESULT_SHOWUSAGE;
09557    chan = atoi(argv[4]);
09558    if ((chan < 1) || (chan > NUM_SPANS)) {
09559       ast_cli(fd, "Invalid channel %s.  Should be a number greater than 0\n", argv[4]);
09560       return RESULT_SUCCESS;
09561    }
09562    tmp = iflist;
09563    while(tmp) {
09564       if (tmp->channel == chan) {
09565          if (tmp->r2) {
09566             mfcr2_set_debug(tmp->r2, 0);
09567             ast_cli(fd, "Disabled R2 debugging on channel %d\n", chan);
09568             return RESULT_SUCCESS;
09569          }
09570          break;
09571       }
09572       tmp = tmp->next;
09573    }
09574    if (tmp) 
09575       ast_cli(fd, "No R2 running on channel %d\n", chan);
09576    else
09577       ast_cli(fd, "No such zap channel %d\n", chan);
09578    return RESULT_SUCCESS;
09579 }
09580 
09581 static int handle_r2_debug(int fd, int argc, char *argv[])
09582 {
09583    int chan;
09584    struct zt_pvt *tmp = NULL;;
09585    if (argc < 4) {
09586       return RESULT_SHOWUSAGE;
09587    }
09588    chan = atoi(argv[3]);
09589    if ((chan < 1) || (chan > NUM_SPANS)) {
09590       ast_cli(fd, "Invalid channel %s.  Should be a number greater than 0\n", argv[3]);
09591       return RESULT_SUCCESS;
09592    }
09593    tmp = iflist;
09594    while(tmp) {
09595       if (tmp->channel == chan) {
09596          if (tmp->r2) {
09597             mfcr2_set_debug(tmp->r2, 0xFFFFFFFF);
09598             ast_cli(fd, "Enabled R2 debugging on channel %d\n", chan);
09599             return RESULT_SUCCESS;
09600          }
09601          break;
09602       }
09603       tmp = tmp->next;
09604    }
09605    if (tmp) 
09606       ast_cli(fd, "No R2 running on channel %d\n", chan);
09607    else
09608       ast_cli(fd, "No such zap channel %d\n", chan);
09609    return RESULT_SUCCESS;
09610 }
09611 static char r2_debug_help[] = 
09612    "Usage: r2 debug channel <channel>\n"
09613    "       Enables R2 protocol level debugging on a given channel\n";
09614    
09615 static char r2_no_debug_help[] = 
09616    "Usage: r2 no debug channel <channel>\n"
09617    "       Enables R2 protocol level debugging on a given channel\n";
09618 
09619 static struct ast_cli_entry zap_r2_cli[] = {
09620    { { "r2", "debug", "channel", NULL }, handle_r2_debug,
09621      "Enables R2 debugging on a channel", r2_debug_help },
09622    { { "r2", "no", "debug", "channel", NULL }, handle_r2_no_debug,
09623      "Disables R2 debugging on a channel", r2_no_debug_help },
09624 };
09625 
09626 #endif
09627 
09628 static int zap_destroy_channel(int fd, int argc, char **argv)
09629 {
09630    int channel = 0;
09631    struct zt_pvt *tmp = NULL;
09632    struct zt_pvt *prev = NULL;
09633    
09634    if (argc != 4) {
09635       return RESULT_SHOWUSAGE;
09636    }
09637    channel = atoi(argv[3]);
09638 
09639    tmp = iflist;
09640    while (tmp) {
09641       if (tmp->channel == channel) {
09642          destroy_channel(prev, tmp, 1);
09643          return RESULT_SUCCESS;
09644       }
09645       prev = tmp;
09646       tmp = tmp->next;
09647    }
09648    return RESULT_FAILURE;
09649 }
09650 
09651 static int setup_zap(int reload);
09652 static int zap_restart(void)
09653 {
09654    if (option_verbose > 0)
09655       ast_verbose(VERBOSE_PREFIX_1 "Destroying channels and reloading zaptel configuration.\n");
09656    while (iflist) {
09657       if (option_debug)
09658          ast_log(LOG_DEBUG, "Destroying zaptel channel no. %d\n", iflist->channel);
09659       /* Also updates iflist: */
09660       destroy_channel(NULL, iflist, 1);
09661    }
09662    if (option_debug)
09663       ast_log(LOG_DEBUG, "Channels destroyed. Now re-reading config.\n");
09664    if (setup_zap(0) != 0) {
09665       ast_log(LOG_WARNING, "Reload channels from zap config failed!\n");
09666       return 1;
09667    }
09668    return 0;
09669 }
09670 
09671 static int zap_restart_cmd(int fd, int argc, char **argv)
09672 {
09673    if (argc != 2) {
09674       return RESULT_SHOWUSAGE;
09675    }
09676 
09677    if (zap_restart() != 0)
09678       return RESULT_FAILURE;
09679    return RESULT_SUCCESS;
09680 }
09681 
09682 static int action_zaprestart(struct mansession *s, struct message *m)
09683 {
09684    if (zap_restart() != 0) {
09685       astman_send_error(s, m, "Failed rereading zaptel configuration");
09686       return 1;
09687    }
09688    astman_send_ack(s, m, "ZapRestart: Success");
09689    return 0;
09690 }
09691 
09692 static int zap_show_channels(int fd, int argc, char **argv)
09693 {
09694 #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09695 #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09696    struct zt_pvt *tmp = NULL;
09697    char tmps[20] = "";
09698    ast_mutex_t *lock;
09699    struct zt_pvt *start;
09700 #ifdef ZAPATA_PRI
09701    int trunkgroup;
09702    struct zt_pri *pri=NULL;
09703    int x;
09704 #endif
09705 
09706    lock = &iflock;
09707    start = iflist;
09708 
09709 #ifdef ZAPATA_PRI
09710    if (argc == 4) {
09711       if ((trunkgroup = atoi(argv[3])) < 1)
09712          return RESULT_SHOWUSAGE;
09713       for (x=0;x<NUM_SPANS;x++) {
09714          if (pris[x].trunkgroup == trunkgroup) {
09715             pri = pris + x;
09716             break;
09717          }
09718       }
09719       if (pri) {
09720          start = pri->crvs;
09721          lock = &pri->lock;
09722       } else {
09723          ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09724          return RESULT_FAILURE;
09725       }
09726    } else
09727 #endif
09728    if (argc != 3)
09729       return RESULT_SHOWUSAGE;
09730 
09731    ast_mutex_lock(lock);
09732 #ifdef ZAPATA_PRI
09733    ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MusicOnHold");
09734 #else
09735    ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MusicOnHold");
09736 #endif   
09737    
09738    tmp = start;
09739    while (tmp) {
09740       if (tmp->channel > 0) {
09741          snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
09742       } else
09743          ast_copy_string(tmps, "pseudo", sizeof(tmps));
09744       ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->musicclass);
09745       tmp = tmp->next;
09746    }
09747    ast_mutex_unlock(lock);
09748    return RESULT_SUCCESS;
09749 #undef FORMAT
09750 #undef FORMAT2
09751 }
09752 
09753 static int zap_show_channel(int fd, int argc, char **argv)
09754 {
09755    int channel;
09756    struct zt_pvt *tmp = NULL;
09757    ZT_CONFINFO ci;
09758    ZT_PARAMS ps;
09759    int x;
09760    ast_mutex_t *lock;
09761    struct zt_pvt *start;
09762 #ifdef ZAPATA_PRI
09763    char *c;
09764    int trunkgroup;
09765    struct zt_pri *pri=NULL;
09766 #endif
09767 
09768    lock = &iflock;
09769    start = iflist;
09770 
09771    if (argc != 4)
09772       return RESULT_SHOWUSAGE;
09773 #ifdef ZAPATA_PRI
09774    if ((c = strchr(argv[3], ':'))) {
09775       if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2)
09776          return RESULT_SHOWUSAGE;
09777       if ((trunkgroup < 1) || (channel < 1))
09778          return RESULT_SHOWUSAGE;
09779       for (x=0;x<NUM_SPANS;x++) {
09780          if (pris[x].trunkgroup == trunkgroup) {
09781             pri = pris + x;
09782             break;
09783          }
09784       }
09785       if (pri) {
09786          start = pri->crvs;
09787          lock = &pri->lock;
09788       } else {
09789          ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09790          return RESULT_FAILURE;
09791       }
09792    } else
09793 #endif
09794       channel = atoi(argv[3]);
09795 
09796    ast_mutex_lock(lock);
09797    tmp = start;
09798    while (tmp) {
09799       if (tmp->channel == channel) {
09800 #ifdef ZAPATA_PRI
09801          if (pri) 
09802             ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
09803          else
09804 #endif         
09805          ast_cli(fd, "Channel: %d\n", tmp->channel);
09806          ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].zfd);
09807          ast_cli(fd, "Span: %d\n", tmp->span);
09808          ast_cli(fd, "Extension: %s\n", tmp->exten);
09809          ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
09810          ast_cli(fd, "Context: %s\n", tmp->context);
09811          ast_cli(fd, "Caller ID: %s\n", tmp->cid_num);
09812          ast_cli(fd, "Calling TON: %d\n", tmp->cid_ton);
09813          ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name);
09814          ast_cli(fd, "Destroy: %d\n", tmp->destroy);
09815          ast_cli(fd, "InAlarm: %d\n", tmp->inalarm);
09816          ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig));
09817          ast_cli(fd, "Radio: %d\n", tmp->radio);
09818          ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
09819          ast_cli(fd, "Real: %s%s%s\n", tmp->subs[SUB_REAL].owner ? tmp->subs[SUB_REAL].owner->name : "<None>", tmp->subs[SUB_REAL].inthreeway ? " (Confed)" : "", tmp->subs[SUB_REAL].linear ? " (Linear)" : "");
09820          ast_cli(fd, "Callwait: %s%s%s\n", tmp->subs[SUB_CALLWAIT].owner ? tmp->subs[SUB_CALLWAIT].owner->name : "<None>", tmp->subs[SUB_CALLWAIT].inthreeway ? " (Confed)" : "", tmp->subs[SUB_CALLWAIT].linear ? " (Linear)" : "");
09821          ast_cli(fd, "Threeway: %s%s%s\n", tmp->subs[SUB_THREEWAY].owner ? tmp->subs[SUB_THREEWAY].owner->name : "<None>", tmp->subs[SUB_THREEWAY].inthreeway ? " (Confed)" : "", tmp->subs[SUB_THREEWAY].linear ? " (Linear)" : "");
09822          ast_cli(fd, "Confno: %d\n", tmp->confno);
09823          ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno);
09824          ast_cli(fd, "Real in conference: %d\n", tmp->inconference);
09825          ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
09826          ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
09827          ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
09828          ast_cli(fd, "Default law: %s\n", tmp->law == ZT_LAW_MULAW ? "ulaw" : tmp->law == ZT_LAW_ALAW ? "alaw" : "unknown");
09829          ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
09830          ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
09831          ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF");
09832          if (tmp->master)
09833             ast_cli(fd, "Master Channel: %d\n", tmp->master->channel);
09834          for (x=0;x<MAX_SLAVES;x++) {
09835             if (tmp->slaves[x])
09836                ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
09837          }
09838 #ifdef ZAPATA_PRI
09839          if (tmp->pri) {
09840             ast_cli(fd, "PRI Flags: ");
09841             if (tmp->resetting)
09842                ast_cli(fd, "Resetting ");
09843             if (tmp->call)
09844                ast_cli(fd, "Call ");
09845             if (tmp->bearer)
09846                ast_cli(fd, "Bearer ");
09847             ast_cli(fd, "\n");
09848             if (tmp->logicalspan) 
09849                ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan);
09850             else
09851                ast_cli(fd, "PRI Logical Span: Implicit\n");
09852          }
09853             
09854 #endif
09855 #ifdef ZAPATA_R2
09856          if (tmp->r2) {
09857             ast_cli(fd, "R2 Flags: ");
09858             if (tmp->r2blocked)
09859                ast_cli(fd, "Blocked ");
09860             if (tmp->hasr2call)
09861                ast_cli(fd, "Call ");
09862             ast_cli(fd, "\n");
09863          }
09864 #endif
09865          memset(&ci, 0, sizeof(ci));
09866          ps.channo = tmp->channel;
09867          if (tmp->subs[SUB_REAL].zfd > -1) {
09868             if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
09869                ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
09870             }
09871 #ifdef ZT_GETCONFMUTE
09872             if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONFMUTE, &x)) {
09873                ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
09874             }
09875 #endif
09876             if (ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
09877                ast_log(LOG_WARNING, "Failed to get parameters on channel %d\n", tmp->channel);
09878             } else {
09879                ast_cli(fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
09880             }
09881          }
09882          ast_mutex_unlock(lock);
09883          return RESULT_SUCCESS;
09884       }
09885       tmp = tmp->next;
09886    }
09887    
09888    ast_cli(fd, "Unable to find given channel %d\n", channel);
09889    ast_mutex_unlock(lock);
09890    return RESULT_FAILURE;
09891 }
09892 
09893 static char zap_show_cadences_help[] =
09894 "Usage: zap show cadences\n"
09895 "       Shows all cadences currently defined\n";
09896 
09897 static int handle_zap_show_cadences(int fd, int argc, char *argv[])
09898 {
09899    int i, j;
09900    for (i=0;i<num_cadence;i++) {
09901       char output[1024];
09902       char tmp[16], tmp2[64];
09903       snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
09904       term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
09905 
09906       for (j=0;j<16;j++) {
09907          if (cadences[i].ringcadence[j] == 0)
09908             break;
09909          snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
09910          if (cidrings[i] * 2 - 1 == j)
09911             term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
09912          else
09913             term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
09914          if (j != 0)
09915             strncat(output, ",", sizeof(output) - strlen(output) - 1);
09916          strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
09917       }
09918       ast_cli(fd,"%s\n",output);
09919    }
09920    return 0;
09921 }
09922 
09923 /* Based on irqmiss.c */
09924 static int zap_show_status(int fd, int argc, char *argv[]) {
09925    #define FORMAT "%-40.40s %-10.10s %-10d %-10d %-10d\n"
09926    #define FORMAT2 "%-40.40s %-10.10s %-10.10s %-10.10s %-10.10s\n"
09927 
09928    int span;
09929    int res;
09930    char alarms[50];
09931 
09932    int ctl;
09933    ZT_SPANINFO s;
09934 
09935    ctl = open("/dev/zap/ctl", O_RDWR);
09936    if (ctl < 0) {
09937       ast_log(LOG_WARNING, "Unable to open /dev/zap/ctl: %s\n", strerror(errno));
09938       ast_cli(fd, "No Zaptel interface found.\n");
09939       return RESULT_FAILURE;
09940    }
09941    ast_cli(fd,FORMAT2, "Description", "Alarms","IRQ","bpviol","CRC4");
09942 
09943    for (span=1;span < ZT_MAX_SPANS;++span) {
09944       s.spanno = span;
09945       res = ioctl(ctl, ZT_SPANSTAT, &s);
09946       if (res) {
09947          continue;
09948       }
09949       alarms[0] = '\0';
09950       if (s.alarms > 0) {
09951          if (s.alarms & ZT_ALARM_BLUE)
09952             strcat(alarms,"BLU/");
09953          if (s.alarms & ZT_ALARM_YELLOW)
09954             strcat(alarms, "YEL/");
09955          if (s.alarms & ZT_ALARM_RED)
09956             strcat(alarms, "RED/");
09957          if (s.alarms & ZT_ALARM_LOOPBACK)
09958             strcat(alarms,"LB/");
09959          if (s.alarms & ZT_ALARM_RECOVER)
09960             strcat(alarms,"REC/");
09961          if (s.alarms & ZT_ALARM_NOTOPEN)
09962             strcat(alarms, "NOP/");
09963          if (!strlen(alarms))
09964             strcat(alarms, "UUU/");
09965          if (strlen(alarms)) {
09966             /* Strip trailing / */
09967             alarms[strlen(alarms)-1]='\0';
09968          }
09969       } else {
09970          if (s.numchans)
09971             strcpy(alarms, "OK");
09972          else
09973             strcpy(alarms, "UNCONFIGURED");
09974       }
09975 
09976       ast_cli(fd, FORMAT, s.desc, alarms, s.irqmisses, s.bpvcount, s.crc4count);
09977    }
09978    close(ctl);
09979 
09980    return RESULT_SUCCESS;
09981 #undef FORMAT
09982 #undef FORMAT2
09983 }
09984 
09985 static char show_channels_usage[] =
09986    "Usage: zap show channels\n"
09987    "  Shows a list of available channels\n";
09988 
09989 static char show_channel_usage[] =
09990    "Usage: zap show channel <chan num>\n"
09991    "  Detailed information about a given channel\n";
09992 
09993 static char zap_show_status_usage[] =
09994    "Usage: zap show status\n"
09995    "       Shows a list of Zaptel cards with status\n";
09996 
09997 static char destroy_channel_usage[] =
09998    "Usage: zap destroy channel <chan num>\n"
09999    "  DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.  Immediately removes a given channel, whether it is in use or not\n";
10000 
10001 static char zap_restart_usage[] =
10002    "Usage: zap restart\n"
10003    "  fully restarts the zaptel channels: destroys them all and then re-reads from config.\n";
10004 
10005 static struct ast_cli_entry zap_cli[] = {
10006    { { "zap", "show", "cadences", NULL }, handle_zap_show_cadences,
10007      "List cadences", zap_show_cadences_help },
10008    { {"zap", "show", "channels", NULL}, zap_show_channels,
10009      "Show active zapata channels", show_channels_usage },
10010    { {"zap", "show", "channel", NULL}, zap_show_channel,
10011      "Show information on a channel", show_channel_usage },
10012    { {"zap", "destroy", "channel", NULL}, zap_destroy_channel,
10013      "Destroy a channel", destroy_channel_usage },
10014    { {"zap", "restart", NULL}, zap_restart_cmd,
10015      "Fully restart zaptel channels", zap_restart_usage },
10016    { {"zap", "show", "status", NULL}, zap_show_status,
10017      "Show all Zaptel cards status", zap_show_status_usage },
10018 };
10019 
10020 #define TRANSFER  0
10021 #define HANGUP    1
10022 
10023 static int zap_fake_event(struct zt_pvt *p, int mode)
10024 {
10025    if (p) {
10026       switch(mode) {
10027          case TRANSFER:
10028             p->fake_event = ZT_EVENT_WINKFLASH;
10029             break;
10030          case HANGUP:
10031             p->fake_event = ZT_EVENT_ONHOOK;
10032             break;
10033          default:
10034             ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);   
10035       }
10036    }
10037    return 0;
10038 }
10039 static struct zt_pvt *find_channel(int channel)
10040 {
10041    struct zt_pvt *p = iflist;
10042    while(p) {
10043       if (p->channel == channel) {
10044          break;
10045       }
10046       p = p->next;
10047    }
10048    return p;
10049 }
10050 
10051 static int action_zapdndon(struct mansession *s, struct message *m)
10052 {
10053     struct zt_pvt *p = NULL;
10054     char *channel = astman_get_header(m, "ZapChannel");
10055     if (ast_strlen_zero(channel)) {
10056         astman_send_error(s, m, "No channel specified");
10057         return 0;
10058     }
10059     p = find_channel(atoi(channel));
10060     if (!p) {
10061         astman_send_error(s, m, "No such channel");
10062         return 0;
10063     }
10064     p->dnd = 1;
10065     astman_send_ack(s, m, "DND Enabled");
10066     return 0;
10067 }
10068 
10069 static int action_zapdndoff(struct mansession *s, struct message *m)
10070 {
10071     struct zt_pvt *p = NULL;
10072     char *channel = astman_get_header(m, "ZapChannel");
10073     if (ast_strlen_zero(channel)) {
10074         astman_send_error(s, m, "No channel specified");
10075         return 0;
10076     }
10077     p = find_channel(atoi(channel));
10078     if (!p) {
10079         astman_send_error(s, m, "No such channel");
10080         return 0;
10081     }
10082     p->dnd = 0;
10083     astman_send_ack(s, m, "DND Disabled");
10084     return 0;
10085 }
10086 
10087 static int action_transfer(struct mansession *s, struct message *m)
10088 {
10089    struct zt_pvt *p = NULL;
10090    char *channel = astman_get_header(m, "ZapChannel");
10091    if (ast_strlen_zero(channel)) {
10092       astman_send_error(s, m, "No channel specified");
10093       return 0;
10094    }
10095    p = find_channel(atoi(channel));
10096    if (!p) {
10097       astman_send_error(s, m, "No such channel");
10098       return 0;
10099    }
10100    zap_fake_event(p,TRANSFER);
10101    astman_send_ack(s, m, "ZapTransfer");
10102    return 0;
10103 }
10104 
10105 static int action_transferhangup(struct mansession *s, struct message *m)
10106 {
10107    struct zt_pvt *p = NULL;
10108    char *channel = astman_get_header(m, "ZapChannel");
10109    if (ast_strlen_zero(channel)) {
10110       astman_send_error(s, m, "No channel specified");
10111       return 0;
10112    }
10113    p = find_channel(atoi(channel));
10114    if (!p) {
10115       astman_send_error(s, m, "No such channel");
10116       return 0;
10117    }
10118    zap_fake_event(p,HANGUP);
10119    astman_send_ack(s, m, "ZapHangup");
10120    return 0;
10121 }
10122 
10123 static int action_zapdialoffhook(struct mansession *s, struct message *m)
10124 {
10125    struct zt_pvt *p = NULL;
10126    char *channel = astman_get_header(m, "ZapChannel");
10127    char *number = astman_get_header(m, "Number");
10128    int i;
10129    if (ast_strlen_zero(channel)) {
10130       astman_send_error(s, m, "No channel specified");
10131       return 0;
10132    }
10133    if (ast_strlen_zero(number)) {
10134       astman_send_error(s, m, "No number specified");
10135       return 0;
10136    }
10137    p = find_channel(atoi(channel));
10138    if (!p) {
10139       astman_send_error(s, m, "No such channel");
10140       return 0;
10141    }
10142    if (!p->owner) {
10143       astman_send_error(s, m, "Channel does not have it's owner");
10144       return 0;
10145    }
10146    for (i=0; i<strlen(number); i++) {
10147       struct ast_frame f = { AST_FRAME_DTMF, number[i] };
10148       zap_queue_frame(p, &f, NULL); 
10149    }
10150    astman_send_ack(s, m, "ZapDialOffhook");
10151    return 0;
10152 }
10153 
10154 static int action_zapshowchannels(struct mansession *s, struct message *m)
10155 {
10156    struct zt_pvt *tmp = NULL;
10157    char *id = astman_get_header(m, "ActionID");
10158    char idText[256] = "";
10159 
10160    astman_send_ack(s, m, "Zapata channel status will follow");
10161    if (!ast_strlen_zero(id))
10162       snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id);
10163 
10164    ast_mutex_lock(&iflock);
10165    
10166    tmp = iflist;
10167    while (tmp) {
10168       if (tmp->channel > 0) {
10169          int alarm = get_alarms(tmp);
10170          ast_cli(s->fd,
10171             "Event: ZapShowChannels\r\n"
10172             "Channel: %d\r\n"
10173             "Signalling: %s\r\n"
10174             "Context: %s\r\n"
10175             "DND: %s\r\n"
10176             "Alarm: %s\r\n"
10177             "%s"
10178             "\r\n",
10179             tmp->channel, sig2str(tmp->sig), tmp->context, 
10180             tmp->dnd ? "Enabled" : "Disabled",
10181             alarm2str(alarm), idText);
10182       } 
10183 
10184       tmp = tmp->next;
10185    }
10186 
10187    ast_mutex_unlock(&iflock);
10188    
10189    ast_cli(s->fd, 
10190       "Event: ZapShowChannelsComplete\r\n"
10191       "%s"
10192       "\r\n", 
10193       idText);
10194    return 0;
10195 }
10196 
10197 static int __unload_module(void)
10198 {
10199    int x = 0;
10200    struct zt_pvt *p, *pl;
10201 #ifdef ZAPATA_PRI
10202    int i;
10203    for(i=0;i<NUM_SPANS;i++) {
10204       if (pris[i].master != AST_PTHREADT_NULL) 
10205          pthread_cancel(pris[i].master);
10206    }
10207    ast_cli_unregister_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(zap_pri_cli[0]));
10208 #endif
10209 #ifdef ZAPATA_R2
10210    ast_cli_unregister_multiple(zap_r2_cli, sizeof(zap_r2_cli) / sizeof(zap_r2_cli[0]));
10211 #endif
10212    ast_cli_unregister_multiple(zap_cli, sizeof(zap_cli) / sizeof(zap_cli[0]));
10213    ast_manager_unregister( "ZapDialOffhook" );
10214    ast_manager_unregister( "ZapHangup" );
10215    ast_manager_unregister( "ZapTransfer" );
10216    ast_manager_unregister( "ZapDNDoff" );
10217    ast_manager_unregister( "ZapDNDon" );
10218    ast_manager_unregister("ZapShowChannels");
10219    ast_channel_unregister(&zap_tech);
10220    if (!ast_mutex_lock(&iflock)) {
10221       /* Hangup all interfaces if they have an owner */
10222       p = iflist;
10223       while(p) {
10224          if (p->owner)
10225             ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
10226          p = p->next;
10227       }
10228       ast_mutex_unlock(&iflock);
10229    } else {
10230       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10231       return -1;
10232    }
10233    if (!ast_mutex_lock(&monlock)) {
10234       if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
10235          pthread_cancel(monitor_thread);
10236          pthread_kill(monitor_thread, SIGURG);
10237          pthread_join(monitor_thread, NULL);
10238       }
10239       monitor_thread = AST_PTHREADT_STOP;
10240       ast_mutex_unlock(&monlock);
10241    } else {
10242       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10243       return -1;
10244    }
10245 
10246    if (!ast_mutex_lock(&iflock)) {
10247       /* Destroy all the interfaces and free their memory */
10248       p = iflist;
10249       while(p) {
10250          /* Free any callerid */
10251          if (p->cidspill)
10252             free(p->cidspill);
10253          /* Close the zapata thingy */
10254          if (p->subs[SUB_REAL].zfd > -1)
10255             zt_close(p->subs[SUB_REAL].zfd);
10256          pl = p;
10257          p = p->next;
10258          x++;
10259          /* Free associated memory */
10260          if(pl)
10261             destroy_zt_pvt(&pl);
10262          ast_verbose(VERBOSE_PREFIX_3 "Unregistered channel %d\n", x);
10263       }
10264       iflist = NULL;
10265       ifcount = 0;
10266       ast_mutex_unlock(&iflock);
10267    } else {
10268       ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10269       return -1;
10270    }
10271 #ifdef ZAPATA_PRI    
10272    for(i=0;i<NUM_SPANS;i++) {
10273       if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
10274          pthread_join(pris[i].master, NULL);
10275       zt_close(pris[i].fds[i]);
10276    }
10277 #endif
10278    return 0;
10279 }
10280 
10281 int unload_module()
10282 {
10283 #ifdef ZAPATA_PRI    
10284    int y;
10285    for (y=0;y<NUM_SPANS;y++)
10286       ast_mutex_destroy(&pris[y].lock);
10287 #endif
10288    return __unload_module();
10289 }
10290       
10291 static int setup_zap(int reload)
10292 {
10293    struct ast_config *cfg;
10294    struct ast_variable *v;
10295    struct zt_pvt *tmp;
10296    char *chan;
10297    char *c;
10298    char *ringc;
10299    int start, finish,x;
10300    int y;
10301    int found_pseudo = 0;
10302    int cur_radio = 0;
10303 #ifdef ZAPATA_PRI
10304    int spanno;
10305    int i;
10306    int logicalspan;
10307    int trunkgroup;
10308    int dchannels[NUM_DCHANS];
10309    struct zt_pri *pri;
10310 #endif
10311 
10312    cfg = ast_config_load(config);
10313 
10314    /* We *must* have a config file otherwise stop immediately */
10315    if (!cfg) {
10316       ast_log(LOG_ERROR, "Unable to load config %s\n", config);
10317       return -1;
10318    }
10319    
10320 
10321    if (ast_mutex_lock(&iflock)) {
10322       /* It's a little silly to lock it, but we mind as well just to be sure */
10323       ast_log(LOG_ERROR, "Unable to lock interface list???\n");
10324       return -1;
10325    }
10326 #ifdef ZAPATA_PRI
10327    if (!reload) {
10328       /* Process trunkgroups first */
10329       v = ast_variable_browse(cfg, "trunkgroups");
10330       while(v) {
10331          if (!strcasecmp(v->name, "trunkgroup")) {
10332             trunkgroup = atoi(v->value);
10333             if (trunkgroup > 0) {
10334                if ((c = strchr(v->value, ','))) {
10335                   i = 0;
10336                   memset(dchannels, 0, sizeof(dchannels));
10337                   while(c && (i < NUM_DCHANS)) {
10338                      dchannels[i] = atoi(c + 1);
10339                      if (dchannels[i] < 0) {
10340                         ast_log(LOG_WARNING, "D-channel for trunk group %d must be a postiive number at line %d of zapata.conf\n", trunkgroup, v->lineno);
10341                      } else
10342                         i++;
10343                      c = strchr(c + 1, ',');
10344                   }
10345                   if (i) {
10346                      if (pri_create_trunkgroup(trunkgroup, dchannels)) {
10347                         ast_log(LOG_WARNING, "Unable to create trunk group %d with Primary D-channel %d at line %d of zapata.conf\n", trunkgroup, dchannels[0], v->lineno);
10348                      } else if (option_verbose > 1)
10349                         ast_verbose(VERBOSE_PREFIX_2 "Created trunk group %d with Primary D-channel %d and %d backup%s\n", trunkgroup, dchannels[0], i - 1, (i == 1) ? "" : "s");
10350                   } else
10351                      ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of zapata.conf\n", trunkgroup, v->lineno);
10352                } else
10353                   ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of zapata.conf\n", trunkgroup, v->lineno);
10354             } else
10355                ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of zapata.conf\n", v->lineno);
10356          } else if (!strcasecmp(v->name, "spanmap")) {
10357             spanno = atoi(v->value);
10358             if (spanno > 0) {
10359                if ((c = strchr(v->value, ','))) {
10360                   trunkgroup = atoi(c + 1);
10361                   if (trunkgroup > 0) {
10362                      if ((c = strchr(c + 1, ','))) 
10363                         logicalspan = atoi(c + 1);
10364                      else
10365                         logicalspan = 0;
10366                      if (logicalspan >= 0) {
10367                         if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
10368                            ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
10369                         } else if (option_verbose > 1) 
10370                            ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
10371                      } else
10372                         ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of zapata.conf\n", v->lineno);
10373                   } else
10374                      ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of zapata.conf\n", v->lineno);
10375                } else
10376                   ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of zapata.conf\n", v->lineno);
10377             } else
10378                ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of zapata.conf\n", v->lineno);
10379          } else {
10380             ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
10381          }
10382          v = v->next;
10383       }
10384    }
10385 #endif
10386    v = ast_variable_browse(cfg, "channels");
10387    while(v) {
10388       /* Create the interface list */
10389       if (!strcasecmp(v->name, "channel")
10390 #ifdef ZAPATA_PRI
10391          || !strcasecmp(v->name, "crv")
10392 #endif         
10393                ) {
10394          if (reload == 0) {
10395             if (cur_signalling < 0) {
10396                ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
10397                ast_config_destroy(cfg);
10398                ast_mutex_unlock(&iflock);
10399                return -1;
10400             }
10401          }
10402          c = v->value;
10403 
10404 #ifdef ZAPATA_PRI
10405          pri = NULL;
10406          if (!strcasecmp(v->name, "crv")) {
10407             if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) {
10408                ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", v->lineno);
10409                ast_config_destroy(cfg);
10410                ast_mutex_unlock(&iflock);
10411                return -1;
10412             }
10413             if (trunkgroup < 1) {
10414                ast_log(LOG_WARNING, "CRV trunk group must be a postive number at line %d\n", v->lineno);
10415                ast_config_destroy(cfg);
10416                ast_mutex_unlock(&iflock);
10417                return -1;
10418             }
10419             c+=y;
10420             for (y=0;y<NUM_SPANS;y++) {
10421                if (pris[y].trunkgroup == trunkgroup) {
10422                   pri = pris + y;
10423                   break;
10424                }
10425             }
10426             if (!pri) {
10427                ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, v->lineno);
10428                ast_config_destroy(cfg);
10429                ast_mutex_unlock(&iflock);
10430                return -1;
10431             }
10432          }
10433 #endif         
10434          chan = strsep(&c, ",");
10435          while(chan) {
10436             if (sscanf(chan, "%d-%d", &start, &finish) == 2) {
10437                /* Range */
10438             } else if (sscanf(chan, "%d", &start)) {
10439                /* Just one */
10440                finish = start;
10441             } else if (!strcasecmp(chan, "pseudo")) {
10442                finish = start = CHAN_PSEUDO;
10443                found_pseudo = 1;
10444             } else {
10445                ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", v->value, chan);
10446                ast_config_destroy(cfg);
10447                ast_mutex_unlock(&iflock);
10448                return -1;
10449             }
10450             if (finish < start) {
10451                ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
10452                x = finish;
10453                finish = start;
10454                start = x;
10455             }
10456             for (x=start;x<=finish;x++) {
10457 #ifdef ZAPATA_PRI
10458                tmp = mkintf(x, cur_signalling, cur_radio, pri, reload);
10459 #else             
10460                tmp = mkintf(x, cur_signalling, cur_radio, NULL, reload);
10461 #endif               
10462 
10463                if (tmp) {
10464                   if (option_verbose > 2) {
10465 #ifdef ZAPATA_PRI
10466                      if (pri)
10467                         ast_verbose(VERBOSE_PREFIX_3 "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup,x, sig2str(tmp->sig));
10468                      else
10469 #endif
10470                         ast_verbose(VERBOSE_PREFIX_3 "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig));
10471                   }
10472                } else {
10473                   if (reload == 1)
10474                      ast_log(LOG_ERROR, "Unable to reconfigure channel '%s'\n", v->value);
10475                   else
10476                      ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
10477                   ast_config_destroy(cfg);
10478                   ast_mutex_unlock(&iflock);
10479                   return -1;
10480                }
10481             }
10482             chan = strsep(&c, ",");
10483          }
10484       } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
10485          if (ast_true(v->value))
10486             usedistinctiveringdetection = 1;
10487       } else if (!strcasecmp(v->name, "dring1context")) {
10488          ast_copy_string(drings.ringContext[0].contextData,v->value,sizeof(drings.ringContext[0].contextData));
10489       } else if (!strcasecmp(v->name, "dring2context")) {
10490          ast_copy_string(drings.ringContext[1].contextData,v->value,sizeof(drings.ringContext[1].contextData));
10491       } else if (!strcasecmp(v->name, "dring3context")) {
10492          ast_copy_string(drings.ringContext[2].contextData,v->value,sizeof(drings.ringContext[2].contextData));
10493       } else if (!strcasecmp(v->name, "dring1")) {
10494          ringc = v->value;
10495          sscanf(ringc, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]);
10496       } else if (!strcasecmp(v->name, "dring2")) {
10497          ringc = v->value;
10498          sscanf(ringc,"%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]);
10499       } else if (!strcasecmp(v->name, "dring3")) {
10500          ringc = v->value;
10501          sscanf(ringc, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]);
10502       } else if (!strcasecmp(v->name, "usecallerid")) {
10503          use_callerid = ast_true(v->value);
10504       } else if (!strcasecmp(v->name, "cidsignalling")) {
10505          if (!strcasecmp(v->value, "bell"))
10506             cid_signalling = CID_SIG_BELL;
10507          else if (!strcasecmp(v->value, "v23"))
10508             cid_signalling = CID_SIG_V23;
10509          else if (!strcasecmp(v->value, "dtmf"))
10510             cid_signalling = CID_SIG_DTMF;
10511          else if (ast_true(v->value))
10512             cid_signalling = CID_SIG_BELL;
10513       } else if (!strcasecmp(v->name, "cidstart")) {
10514          if (!strcasecmp(v->value, "ring"))
10515             cid_start = CID_START_RING;
10516          else if (!strcasecmp(v->value, "polarity"))
10517             cid_start = CID_START_POLARITY;
10518          else if (!strcasecmp(v->value, "usehist"))
10519             cid_start = CID_START_USEHIST;
10520          else if (ast_true(v->value))
10521             cid_start = CID_START_RING;
10522       } else if (!strcasecmp(v->name, "threewaycalling")) {
10523          threewaycalling = ast_true(v->value);
10524       } else if (!strcasecmp(v->name, "cancallforward")) {
10525          cancallforward = ast_true(v->value);
10526       } else if (!strcasecmp(v->name, "relaxdtmf")) {
10527          if (ast_true(v->value)) 
10528             relaxdtmf = DSP_DIGITMODE_RELAXDTMF;
10529          else
10530             relaxdtmf = 0;
10531       } else if (!strcasecmp(v->name, "mailbox")) {
10532          ast_copy_string(mailbox, v->value, sizeof(mailbox));
10533       } else if (!strcasecmp(v->name, "adsi")) {
10534          adsi = ast_true(v->value);
10535       } else if (!strcasecmp(v->name, "transfer")) {
10536          transfer = ast_true(v->value);
10537       } else if (!strcasecmp(v->name, "canpark")) {
10538          canpark = ast_true(v->value);
10539       } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
10540          echocanbridged = ast_true(v->value);
10541       } else if (!strcasecmp(v->name, "busydetect")) {
10542          busydetect = ast_true(v->value);
10543       } else if (!strcasecmp(v->name, "busycount")) {
10544          busycount = atoi(v->value);
10545       } else if (!strcasecmp(v->name, "busypattern")) {
10546          if (sscanf(v->value, "%d,%d", &busy_tonelength, &busy_quietlength) != 2) {
10547             ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength\n");
10548          }
10549       } else if (!strcasecmp(v->name, "callprogress")) {
10550          if (ast_true(v->value))
10551             callprogress |= 1;
10552          else
10553             callprogress &= ~1;
10554       } else if (!strcasecmp(v->name, "faxdetect")) {
10555          if (!strcasecmp(v->value, "incoming")) {
10556             callprogress |= 4;
10557             callprogress &= ~2;
10558          } else if (!strcasecmp(v->value, "outgoing")) {
10559             callprogress &= ~4;
10560             callprogress |= 2;
10561          } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
10562             callprogress |= 6;
10563          else
10564             callprogress &= ~6;
10565       } else if (!strcasecmp(v->name, "echocancel")) {
10566          if (!ast_strlen_zero(v->value)) {
10567             y = atoi(v->value);
10568          } else
10569             y = 0;
10570          if ((y == 32) || (y == 64) || (y == 128) || (y == 256))
10571             echocancel = y;
10572          else {
10573             echocancel = ast_true(v->value);
10574             if (echocancel)
10575                echocancel=128;
10576          }
10577       } else if (!strcasecmp(v->name, "echotraining")) {
10578          if (sscanf(v->value, "%d", &y) == 1) {
10579             if ((y < 10) || (y > 4000)) {
10580                ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 2000 ms at line %d\n", v->lineno);              
10581             } else {
10582                echotraining = y;
10583             }
10584          } else if (ast_true(v->value)) {
10585             echotraining = 400;
10586          } else
10587             echotraining = 0;
10588       } else if (!strcasecmp(v->name, "hidecallerid")) {
10589          hidecallerid = ast_true(v->value);
10590       } else if (!strcasecmp(v->name, "pulsedial")) {
10591          pulse = ast_true(v->value);
10592       } else if (!strcasecmp(v->name, "callreturn")) {
10593          callreturn = ast_true(v->value);
10594       } else if (!strcasecmp(v->name, "callwaiting")) {
10595          callwaiting = ast_true(v->value);
10596       } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
10597          callwaitingcallerid = ast_true(v->value);
10598       } else if (!strcasecmp(v->name, "context")) {
10599          ast_copy_string(context, v->value, sizeof(context));
10600       } else if (!strcasecmp(v->name, "language")) {
10601          ast_copy_string(language, v->value, sizeof(language));
10602       } else if (!strcasecmp(v->name, "progzone")) {
10603          ast_copy_string(progzone, v->value, sizeof(progzone));
10604       } else if (!strcasecmp(v->name, "musiconhold")) {
10605          ast_copy_string(musicclass, v->value, sizeof(musicclass));
10606       } else if (!strcasecmp(v->name, "stripmsd")) {
10607          stripmsd = atoi(v->value);
10608       } else if (!strcasecmp(v->name, "jitterbuffers")) {
10609          numbufs = atoi(v->value);
10610       } else if (!strcasecmp(v->name, "group")) {
10611          cur_group = ast_get_group(v->value);
10612       } else if (!strcasecmp(v->name, "callgroup")) {
10613          cur_callergroup = ast_get_group(v->value);
10614       } else if (!strcasecmp(v->name, "pickupgroup")) {
10615          cur_pickupgroup = ast_get_group(v->value);
10616       } else if (!strcasecmp(v->name, "immediate")) {
10617          immediate = ast_true(v->value);
10618       } else if (!strcasecmp(v->name, "transfertobusy")) {
10619          transfertobusy = ast_true(v->value);
10620       } else if (!strcasecmp(v->name, "rxgain")) {
10621          if (sscanf(v->value, "%f", &rxgain) != 1) {
10622             ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value);
10623          }
10624       } else if (!strcasecmp(v->name, "txgain")) {
10625          if (sscanf(v->value, "%f", &txgain) != 1) {
10626             ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value);
10627          }
10628       } else if (!strcasecmp(v->name, "tonezone")) {
10629          if (sscanf(v->value, "%d", &tonezone) != 1) {
10630             ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
10631          }
10632       } else if (!strcasecmp(v->name, "callerid")) {
10633          if (!strcasecmp(v->value, "asreceived")) {
10634             cid_num[0] = '\0';
10635             cid_name[0] = '\0';
10636          } else {
10637             ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
10638          }
10639       } else if (!strcasecmp(v->name, "useincomingcalleridonzaptransfer")) {
10640          zaptrcallerid = ast_true(v->value);
10641       } else if (!strcasecmp(v->name, "restrictcid")) {
10642          restrictcid = ast_true(v->value);
10643       } else if (!strcasecmp(v->name, "usecallingpres")) {
10644          use_callingpres = ast_true(v->value);
10645       } else if (!strcasecmp(v->name, "accountcode")) {
10646          ast_copy_string(accountcode, v->value, sizeof(accountcode));
10647       } else if (!strcasecmp(v->name, "amaflags")) {
10648          y = ast_cdr_amaflags2int(v->value);
10649          if (y < 0) 
10650             ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
10651          else
10652             amaflags = y;
10653       } else if(!reload){ 
10654           if (!strcasecmp(v->name, "signalling")) {
10655             if (!strcasecmp(v->value, "em")) {
10656                cur_signalling = SIG_EM;
10657             } else if (!strcasecmp(v->value, "em_e1")) {
10658                cur_signalling = SIG_EM_E1;
10659             } else if (!strcasecmp(v->value, "em_w")) {
10660                cur_signalling = SIG_EMWINK;
10661                cur_radio = 0;
10662             } else if (!strcasecmp(v->value, "fxs_ls")) {
10663                cur_signalling = SIG_FXSLS;
10664                cur_radio = 0;
10665             } else if (!strcasecmp(v->value, "fxs_gs")) {
10666                cur_signalling = SIG_FXSGS;
10667                cur_radio = 0;
10668             } else if (!strcasecmp(v->value, "fxs_ks")) {
10669                cur_signalling = SIG_FXSKS;
10670                cur_radio = 0;
10671             } else if (!strcasecmp(v->value, "fxo_ls")) {
10672                cur_signalling = SIG_FXOLS;
10673                cur_radio = 0;
10674             } else if (!strcasecmp(v->value, "fxo_gs")) {
10675                cur_signalling = SIG_FXOGS;
10676                cur_radio = 0;
10677             } else if (!strcasecmp(v->value, "fxo_ks")) {
10678                cur_signalling = SIG_FXOKS;
10679                cur_radio = 0;
10680             } else if (!strcasecmp(v->value, "fxs_rx")) {
10681                cur_signalling = SIG_FXSKS;
10682                cur_radio = 1;
10683             } else if (!strcasecmp(v->value, "fxo_rx")) {
10684                cur_signalling = SIG_FXOLS;
10685                cur_radio = 1;
10686             } else if (!strcasecmp(v->value, "fxs_tx")) {
10687                cur_signalling = SIG_FXSLS;
10688                cur_radio = 1;
10689             } else if (!strcasecmp(v->value, "fxo_tx")) {
10690                cur_signalling = SIG_FXOGS;
10691                cur_radio = 1;
10692             } else if (!strcasecmp(v->value, "em_rx")) {
10693                cur_signalling = SIG_EM;
10694                cur_radio = 1;
10695             } else if (!strcasecmp(v->value, "em_tx")) {
10696                cur_signalling = SIG_EM;
10697                cur_radio = 1;
10698             } else if (!strcasecmp(v->value, "em_rxtx")) {
10699                cur_signalling = SIG_EM;
10700                cur_radio = 2;
10701             } else if (!strcasecmp(v->value, "em_txrx")) {
10702                cur_signalling = SIG_EM;
10703                cur_radio = 2;
10704             } else if (!strcasecmp(v->value, "sf")) {
10705                cur_signalling = SIG_SF;
10706                cur_radio = 0;
10707             } else if (!strcasecmp(v->value, "sf_w")) {
10708                cur_signalling = SIG_SFWINK;
10709                cur_radio = 0;
10710             } else if (!strcasecmp(v->value, "sf_featd")) {
10711                cur_signalling = SIG_FEATD;
10712                cur_radio = 0;
10713             } else if (!strcasecmp(v->value, "sf_featdmf")) {
10714                cur_signalling = SIG_FEATDMF;
10715                cur_radio = 0;
10716             } else if (!strcasecmp(v->value, "sf_featb")) {
10717                cur_signalling = SIG_SF_FEATB;
10718                cur_radio = 0;
10719             } else if (!strcasecmp(v->value, "sf")) {
10720                cur_signalling = SIG_SF;
10721                cur_radio = 0;
10722             } else if (!strcasecmp(v->value, "sf_rx")) {
10723                cur_signalling = SIG_SF;
10724                cur_radio = 1;
10725             } else if (!strcasecmp(v->value, "sf_tx")) {
10726                cur_signalling = SIG_SF;
10727                cur_radio = 1;
10728             } else if (!strcasecmp(v->value, "sf_rxtx")) {
10729                cur_signalling = SIG_SF;
10730                cur_radio = 2;
10731             } else if (!strcasecmp(v->value, "sf_txrx")) {
10732                cur_signalling = SIG_SF;
10733                cur_radio = 2;
10734             } else if (!strcasecmp(v->value, "featd")) {
10735                cur_signalling = SIG_FEATD;
10736                cur_radio = 0;
10737             } else if (!strcasecmp(v->value, "featdmf")) {
10738                cur_signalling = SIG_FEATDMF;
10739                cur_radio = 0;
10740             } else if (!strcasecmp(v->value, "featdmf_ta")) {
10741                cur_signalling = SIG_FEATDMF_TA;
10742                cur_radio = 0;
10743             } else if (!strcasecmp(v->value, "e911")) {
10744                cur_signalling = SIG_E911;
10745                cur_radio = 0;
10746             } else if (!strcasecmp(v->value, "featb")) {
10747                cur_signalling = SIG_FEATB;
10748                cur_radio = 0;
10749 #ifdef ZAPATA_PRI
10750             } else if (!strcasecmp(v->value, "pri_net")) {
10751                cur_radio = 0;
10752                cur_signalling = SIG_PRI;
10753                pritype = PRI_NETWORK;
10754             } else if (!strcasecmp(v->value, "pri_cpe")) {
10755                cur_signalling = SIG_PRI;
10756                cur_radio = 0;
10757                pritype = PRI_CPE;
10758             } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
10759                cur_signalling = SIG_GR303FXOKS;
10760                cur_radio = 0;
10761                pritype = PRI_NETWORK;
10762             } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
10763                cur_signalling = SIG_GR303FXSKS;
10764                cur_radio = 0;
10765                pritype = PRI_CPE;
10766 #endif
10767 #ifdef ZAPATA_R2
10768             } else if (!strcasecmp(v->value, "r2")) {
10769                cur_signalling = SIG_R2;
10770                cur_radio = 0;
10771 #endif         
10772             } else {
10773                ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
10774             }
10775 #ifdef ZAPATA_R2
10776          } else if (!strcasecmp(v->name, "r2country")) {
10777             r2prot = str2r2prot(v->value);
10778             if (r2prot < 0) {
10779                ast_log(LOG_WARNING, "Unknown R2 Country '%s' at line %d.\n", v->value, v->lineno);
10780             }
10781 #endif
10782 #ifdef ZAPATA_PRI
10783          } else if (!strcasecmp(v->name, "pridialplan")) {
10784             if (!strcasecmp(v->value, "national")) {
10785                dialplan = PRI_NATIONAL_ISDN + 1;
10786             } else if (!strcasecmp(v->value, "unknown")) {
10787                dialplan = PRI_UNKNOWN + 1;
10788             } else if (!strcasecmp(v->value, "private")) {
10789                dialplan = PRI_PRIVATE + 1;
10790             } else if (!strcasecmp(v->value, "international")) {
10791                dialplan = PRI_INTERNATIONAL_ISDN + 1;
10792             } else if (!strcasecmp(v->value, "local")) {
10793                dialplan = PRI_LOCAL_ISDN + 1;
10794             } else if (!strcasecmp(v->value, "dynamic")) {
10795                dialplan = -1;
10796             } else {
10797                ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10798             }
10799          } else if (!strcasecmp(v->name, "prilocaldialplan")) {
10800             if (!strcasecmp(v->value, "national")) {
10801                localdialplan = PRI_NATIONAL_ISDN + 1;
10802             } else if (!strcasecmp(v->value, "unknown")) {
10803                localdialplan = PRI_UNKNOWN + 1;
10804             } else if (!strcasecmp(v->value, "private")) {
10805                localdialplan = PRI_PRIVATE + 1;
10806             } else if (!strcasecmp(v->value, "international")) {
10807                localdialplan = PRI_INTERNATIONAL_ISDN + 1;
10808             } else if (!strcasecmp(v->value, "local")) {
10809                localdialplan = PRI_LOCAL_ISDN + 1;
10810             } else if (!strcasecmp(v->value, "dynamic")) {
10811                localdialplan = -1;
10812             } else {
10813                ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10814             }
10815          } else if (!strcasecmp(v->name, "switchtype")) {
10816             if (!strcasecmp(v->value, "national")) 
10817                switchtype = PRI_SWITCH_NI2;
10818             else if (!strcasecmp(v->value, "ni1"))
10819                switchtype = PRI_SWITCH_NI1;
10820             else if (!strcasecmp(v->value, "dms100"))
10821                switchtype = PRI_SWITCH_DMS100;
10822             else if (!strcasecmp(v->value, "4ess"))
10823                switchtype = PRI_SWITCH_ATT4ESS;
10824             else if (!strcasecmp(v->value, "5ess"))
10825                switchtype = PRI_SWITCH_LUCENT5E;
10826             else if (!strcasecmp(v->value, "euroisdn"))
10827                switchtype = PRI_SWITCH_EUROISDN_E1;
10828             else if (!strcasecmp(v->value, "qsig"))
10829                switchtype = PRI_SWITCH_QSIG;
10830             else {
10831                ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value);
10832                ast_config_destroy(cfg);
10833                ast_mutex_unlock(&iflock);
10834                return -1;
10835             }
10836          } else if (!strcasecmp(v->name, "nsf")) {
10837             if (!strcasecmp(v->value, "sdn"))
10838                nsf = PRI_NSF_SDN;
10839             else if (!strcasecmp(v->value, "megacom"))
10840                nsf = PRI_NSF_MEGACOM;
10841             else if (!strcasecmp(v->value, "accunet"))
10842                nsf = PRI_NSF_ACCUNET;
10843             else if (!strcasecmp(v->value, "none"))
10844                nsf = PRI_NSF_NONE;
10845             else {
10846                ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value);
10847                nsf = PRI_NSF_NONE;
10848             }
10849          } else if (!strcasecmp(v->name, "priindication")) {
10850             if (!strcasecmp(v->value, "outofband"))
10851                priindication_oob = 1;
10852             else if (!strcasecmp(v->value, "inband"))
10853                priindication_oob = 0;
10854             else
10855                ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n",
10856                   v->value, v->lineno);
10857          } else if (!strcasecmp(v->name, "priexclusive")) {
10858             cur_priexclusive = ast_true(v->value);
10859          } else if (!strcasecmp(v->name, "internationalprefix")) {
10860             ast_copy_string(internationalprefix, v->value, sizeof(internationalprefix));
10861          } else if (!strcasecmp(v->name, "nationalprefix")) {
10862             ast_copy_string(nationalprefix, v->value, sizeof(nationalprefix));
10863          } else if (!strcasecmp(v->name, "localprefix")) {
10864             ast_copy_string(localprefix, v->value, sizeof(localprefix));
10865          } else if (!strcasecmp(v->name, "privateprefix")) {
10866             ast_copy_string(privateprefix, v->value, sizeof(privateprefix));
10867          } else if (!strcasecmp(v->name, "unknownprefix")) {
10868             ast_copy_string(unknownprefix, v->value, sizeof(unknownprefix));
10869          } else if (!strcasecmp(v->name, "resetinterval")) {
10870             if (!strcasecmp(v->value, "never"))
10871                resetinterval = -1;
10872             else if( atoi(v->value) >= 60 )
10873                resetinterval = atoi(v->value);
10874             else
10875                ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d\n",
10876                   v->value, v->lineno);
10877          } else if (!strcasecmp(v->name, "minunused")) {
10878             minunused = atoi(v->value);
10879          } else if (!strcasecmp(v->name, "minidle")) {
10880             minidle = atoi(v->value); 
10881          } else if (!strcasecmp(v->name, "idleext")) {
10882             ast_copy_string(idleext, v->value, sizeof(idleext));
10883          } else if (!strcasecmp(v->name, "idledial")) {
10884             ast_copy_string(idledial, v->value, sizeof(idledial));
10885          } else if (!strcasecmp(v->name, "overlapdial")) {
10886             overlapdial = ast_true(v->value);
10887          } else if (!strcasecmp(v->name, "pritimer")) {
10888 #ifdef PRI_GETSET_TIMERS
10889             char *timerc;
10890             int timer, timeridx;
10891             c = v->value;
10892             timerc = strsep(&c, ",");
10893             if (timerc) {
10894                timer = atoi(c);
10895                if (!timer)
10896                   ast_log(LOG_WARNING, "'%s' is not a valid value for an ISDN timer\n", timerc);
10897                else {
10898                   if ((timeridx = pri_timer2idx(timerc)) >= 0)
10899                      pritimers[timeridx] = timer;
10900                   else
10901                      ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer\n", timerc);
10902                }
10903             } else
10904                ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer configuration string\n", v->value);
10905 
10906          } else if (!strcasecmp(v->name, "facilityenable")) {
10907             facilityenable = ast_true(v->value);
10908 #endif /* PRI_GETSET_TIMERS */
10909 #endif /* ZAPATA_PRI */
10910          } else if (!strcasecmp(v->name, "cadence")) {
10911             /* setup to scan our argument */
10912             int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
10913             int i;
10914             struct zt_ring_cadence new_cadence;
10915             int cid_location = -1;
10916                      int firstcadencepos = 0;
10917             char original_args[80];
10918             int cadence_is_ok = 1;
10919 
10920             ast_copy_string(original_args, v->value, sizeof(original_args));
10921             /* 16 cadences allowed (8 pairs) */
10922             element_count = sscanf(v->value, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7], &c[8], &c[9], &c[10], &c[11], &c[12], &c[13], &c[14], &c[15]);
10923    
10924             /* Cadence must be even (on/off) */
10925             if (element_count % 2 == 1) {
10926                ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args);
10927                cadence_is_ok = 0;
10928             }
10929    
10930             /* Ring cadences cannot be negative */
10931             for (i=0;i<element_count;i++) {
10932                     if (c[i] == 0) {
10933                        ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s\n", original_args);
10934                   cadence_is_ok = 0;
10935                   break;
10936                } else if (c[i] < 0) {
10937                   if (i % 2 == 1) {
10938                           /* Silence duration, negative possibly okay */
10939                      if (cid_location == -1) {
10940                              cid_location = i;
10941                         c[i] *= -1;
10942                      } else {
10943                              ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args);
10944                         cadence_is_ok = 0;
10945                         break;
10946                      }
10947                   } else {
10948                      if (firstcadencepos == 0) {
10949                              firstcadencepos = i; /* only recorded to avoid duplicate specification */
10950                                              /* duration will be passed negative to the zaptel driver */
10951                      } else {
10952                              ast_log(LOG_ERROR, "First cadence position specified twice: %s\n",original_args);
10953                         cadence_is_ok = 0;
10954                         break;
10955                      }
10956                   }
10957                }
10958             }
10959    
10960             /* Substitute our scanned cadence */
10961             for (i=0;i<16;i++) {
10962                new_cadence.ringcadence[i] = c[i];
10963             }
10964    
10965             if (cadence_is_ok) {
10966                /* ---we scanned it without getting annoyed; now some sanity checks--- */
10967                if (element_count < 2) {
10968                   ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args);
10969                } else {
10970                   if (cid_location == -1) {
10971                      /* user didn't say; default to first pause */
10972                      cid_location = 1;
10973                   } else {
10974                      /* convert element_index to cidrings value */
10975                      cid_location = (cid_location + 1) / 2;
10976                   }
10977                   /* ---we like their cadence; try to install it--- */
10978                   if (!user_has_defined_cadences++)
10979                      /* this is the first user-defined cadence; clear the default user cadences */
10980                      num_cadence = 0;
10981                   if ((num_cadence+1) >= NUM_CADENCE_MAX)
10982                      ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args);
10983                   else {
10984                      cadences[num_cadence] = new_cadence;
10985                      cidrings[num_cadence++] = cid_location;
10986                      if (option_verbose > 2)
10987                         ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args);
10988                   }
10989                }
10990             }
10991          } else if (!strcasecmp(v->name, "ringtimeout")) {
10992             ringt_base = (atoi(v->value) * 8) / READ_SIZE;
10993          } else if (!strcasecmp(v->name, "prewink")) {
10994             cur_prewink = atoi(v->value);
10995          } else if (!strcasecmp(v->name, "preflash")) {
10996             cur_preflash = atoi(v->value);
10997          } else if (!strcasecmp(v->name, "wink")) {
10998             cur_wink = atoi(v->value);
10999          } else if (!strcasecmp(v->name, "flash")) {
11000             cur_flash = atoi(v->value);
11001          } else if (!strcasecmp(v->name, "start")) {
11002             cur_start = atoi(v->value);
11003          } else if (!strcasecmp(v->name, "rxwink")) {
11004             cur_rxwink = atoi(v->value);
11005          } else if (!strcasecmp(v->name, "rxflash")) {
11006             cur_rxflash = atoi(v->value);
11007          } else if (!strcasecmp(v->name, "debounce")) {
11008             cur_debounce = atoi(v->value);
11009          } else if (!strcasecmp(v->name, "toneduration")) {
11010             int toneduration;
11011             int ctlfd;
11012             int res;
11013             struct zt_dialparams dps;
11014 
11015             ctlfd = open("/dev/zap/ctl", O_RDWR);
11016             if (ctlfd == -1) {
11017                ast_log(LOG_ERROR, "Unable to open /dev/zap/ctl to set toneduration\n");
11018                return -1;
11019             }
11020 
11021             toneduration = atoi(v->value);
11022             if (toneduration > -1) {
11023                dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration;
11024                res = ioctl(ctlfd, ZT_SET_DIALPARAMS, &dps);
11025                if (res < 0) {
11026                   ast_log(LOG_ERROR, "Invalid tone duration: %d ms\n", toneduration);
11027                   return -1;
11028                }
11029             }
11030             close(ctlfd);
11031          } else if (!strcasecmp(v->name, "polarityonanswerdelay")) {
11032             polarityonanswerdelay = atoi(v->value);
11033          } else if (!strcasecmp(v->name, "answeronpolarityswitch")) {
11034             answeronpolarityswitch = ast_true(v->value);
11035          } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) {
11036             hanguponpolarityswitch = ast_true(v->value);
11037          } else if (!strcasecmp(v->name, "sendcalleridafter")) {
11038             sendcalleridafter = atoi(v->value);
11039          } else if (!strcasecmp(v->name, "defaultcic")) {
11040             ast_copy_string(defaultcic, v->value, sizeof(defaultcic));
11041          } else if (!strcasecmp(v->name, "defaultozz")) {
11042             ast_copy_string(defaultozz, v->value, sizeof(defaultozz));
11043          } 
11044       } else 
11045          ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
11046       v = v->next;
11047    }
11048    if (!found_pseudo && reload == 0) {
11049    
11050       /* Make sure pseudo isn't a member of any groups if
11051          we're automatically making it. */   
11052       cur_group = 0;
11053       cur_callergroup = 0;
11054       cur_pickupgroup = 0;
11055    
11056       tmp = mkintf(CHAN_PSEUDO, cur_signalling, cur_radio, NULL, reload);
11057 
11058       if (tmp) {
11059          if (option_verbose > 2)
11060             ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n");
11061       } else {
11062          ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
11063       }
11064    }
11065    ast_mutex_unlock(&iflock);
11066    ast_config_destroy(cfg);
11067 #ifdef ZAPATA_PRI
11068    if (!reload) {
11069       for (x=0;x<NUM_SPANS;x++) {
11070          if (pris[x].pvts[0]) {
11071             if (start_pri(pris + x)) {
11072                ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
11073                return -1;
11074             } else if (option_verbose > 1)
11075                ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1);
11076          }
11077       }
11078    }
11079 #endif
11080    /* And start the monitor for the first time */
11081    restart_monitor();
11082    return 0;
11083 }
11084 
11085 int load_module(void)
11086 {
11087    int res;
11088 
11089 #ifdef ZAPATA_PRI
11090    int y,i;
11091    memset(pris, 0, sizeof(pris));
11092    for (y=0;y<NUM_SPANS;y++) {
11093       ast_mutex_init(&pris[y].lock);
11094       pris[y].offset = -1;
11095       pris[y].master = AST_PTHREADT_NULL;
11096       for (i=0;i<NUM_DCHANS;i++)
11097          pris[y].fds[i] = -1;
11098    }
11099    pri_set_error(zt_pri_error);
11100    pri_set_message(zt_pri_message);
11101 #endif
11102    res = setup_zap(0);
11103    /* Make sure we can register our Zap channel type */
11104    if(res) {
11105      return -1;
11106    }
11107    if (ast_channel_register(&zap_tech)) {
11108       ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
11109       __unload_module();
11110       return -1;
11111    }
11112 #ifdef ZAPATA_PRI
11113    ast_cli_register_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(zap_pri_cli[0]));
11114 #endif   
11115 #ifdef ZAPATA_R2
11116    ast_cli_register_multiple(zap_r2_cli, sizeof(zap_r2_cli) / sizeof(zap_r2_cli[0]));
11117 #endif   
11118    ast_cli_register_multiple(zap_cli, sizeof(zap_cli) / sizeof(zap_cli[0]));
11119    
11120    memset(round_robin, 0, sizeof(round_robin));
11121    ast_manager_register( "ZapTransfer", 0, action_transfer, "Transfer Zap Channel" );
11122    ast_manager_register( "ZapHangup", 0, action_transferhangup, "Hangup Zap Channel" );
11123    ast_manager_register( "ZapDialOffhook", 0, action_zapdialoffhook, "Dial over Zap channel while offhook" );
11124    ast_manager_register( "ZapDNDon", 0, action_zapdndon, "Toggle Zap channel Do Not Disturb status ON" );
11125    ast_manager_register( "ZapDNDoff", 0, action_zapdndoff, "Toggle Zap channel Do Not Disturb status OFF" );
11126    ast_manager_register("ZapShowChannels", 0, action_zapshowchannels, "Show status zapata channels");
11127 
11128    return res;
11129 }
11130 
11131 static int zt_sendtext(struct ast_channel *c, const char *text)
11132 {
11133 #define  END_SILENCE_LEN 400
11134 #define  HEADER_MS 50
11135 #define  TRAILER_MS 5
11136 #define  HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
11137 #define  ASCII_BYTES_PER_CHAR 80
11138 
11139    unsigned char *buf,*mybuf;
11140    struct zt_pvt *p = c->tech_pvt;
11141    struct pollfd fds[1];
11142    int size,res,fd,len,x;
11143    int bytes=0;
11144    /* Initial carrier (imaginary) */
11145    float cr = 1.0;
11146    float ci = 0.0;
11147    float scont = 0.0;
11148    int index;
11149 
11150    index = zt_get_index(c, p, 0);
11151    if (index < 0) {
11152       ast_log(LOG_WARNING, "Huh?  I don't exist?\n");
11153       return -1;
11154    }
11155    if (!text[0]) return(0); /* if nothing to send, dont */
11156    if ((!p->tdd) && (!p->mate)) return(0);  /* if not in TDD mode, just return */
11157    if (p->mate) 
11158       buf = malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
11159    else
11160       buf = malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
11161    if (!buf) {
11162       ast_log(LOG_ERROR, "MALLOC FAILED\n");
11163       return -1;
11164    }
11165    mybuf = buf;
11166    if (p->mate) {
11167       int codec = AST_LAW(p);
11168       for (x=0;x<HEADER_MS;x++) {   /* 50 ms of Mark */
11169          PUT_CLID_MARKMS;
11170          }
11171       /* Put actual message */
11172       for (x=0;text[x];x++)  {
11173          PUT_CLID(text[x]);
11174          }
11175       for (x=0;x<TRAILER_MS;x++) {  /* 5 ms of Mark */
11176          PUT_CLID_MARKMS;
11177          }
11178       len = bytes;
11179       buf = mybuf;
11180    }
11181    else {
11182       len = tdd_generate(p->tdd,buf,text);
11183       if (len < 1) {
11184          ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n",(int)strlen(text));
11185          free(mybuf);
11186          return -1;
11187       }
11188    }
11189    memset(buf + len,0x7f,END_SILENCE_LEN);
11190    len += END_SILENCE_LEN;
11191    fd = p->subs[index].zfd;
11192    while(len) {
11193       if (ast_check_hangup(c)) {
11194          free(mybuf);
11195          return -1;
11196       }
11197       size = len;
11198       if (size > READ_SIZE)
11199          size = READ_SIZE;
11200       fds[0].fd = fd;
11201       fds[0].events = POLLOUT | POLLPRI;
11202       fds[0].revents = 0;
11203       res = poll(fds, 1, -1);
11204       if (!res) {
11205          ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
11206          continue;
11207       }
11208         /* if got exception */
11209       if (fds[0].revents & POLLPRI) return -1;
11210       if (!(fds[0].revents & POLLOUT)) {
11211          ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
11212          continue;
11213       }
11214       res = write(fd, buf, size);
11215       if (res != size) {
11216          if (res == -1) {
11217             free(mybuf);
11218             return -1;
11219          }
11220          if (option_debug)
11221             ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
11222          break;
11223       }
11224       len -= size;
11225       buf += size;
11226    }
11227    free(mybuf);
11228    return(0);
11229 }
11230 
11231 
11232 int reload(void)
11233 {
11234    int res = 0;
11235 
11236    res = setup_zap(1);
11237    if (res) {
11238       ast_log(LOG_WARNING, "Reload of chan_zap.so is unsuccessful!\n");
11239       return -1;
11240    }
11241    return 0;
11242 }
11243 
11244 int usecount()
11245 {
11246    return usecnt;
11247 }
11248 
11249 char *description()
11250 {
11251    return (char *) desc;
11252 }
11253 
11254 char *key()
11255 {
11256    return ASTERISK_GPL_KEY;
11257 }
11258 

Generated on Thu May 24 14:21:09 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.7