Sat Nov 1 06:28:24 2008

Asterisk developer's documentation


chan_dahdi.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2006, 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 DAHDI Pseudo TDM interface 
00022  *
00023  * \author Mark Spencer <markster@digium.com>
00024  * 
00025  * Connects to the DAHDI telephony library as well as 
00026  * libpri. Libpri is optional and needed only if you are
00027  * going to use ISDN connections.
00028  *
00029  * You need to install libraries before you attempt to compile
00030  * and install the DAHDI channel.
00031  *
00032  * \par See also
00033  * \arg \ref Config_dahdi
00034  *
00035  * \ingroup channel_drivers
00036  *
00037  * \todo Deprecate the "musiconhold" configuration option post 1.4
00038  */
00039 
00040 /*** MODULEINFO
00041    <depend>res_smdi</depend>
00042    <depend>dahdi</depend>
00043    <depend>tonezone</depend>
00044    <depend>res_features</depend>
00045    <use>pri</use>
00046  ***/
00047 
00048 #include "asterisk.h"
00049 
00050 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 152286 $")
00051 
00052 #include <stdio.h>
00053 #include <string.h>
00054 #ifdef __NetBSD__
00055 #include <pthread.h>
00056 #include <signal.h>
00057 #else
00058 #include <sys/signal.h>
00059 #endif
00060 #include <errno.h>
00061 #include <stdlib.h>
00062 #if !defined(SOLARIS) && !defined(__FreeBSD__)
00063 #include <stdint.h>
00064 #endif
00065 #include <unistd.h>
00066 #include <sys/ioctl.h>
00067 #include <math.h>
00068 #include <ctype.h>
00069 
00070 #ifdef HAVE_PRI
00071 #include <libpri.h>
00072 #endif
00073 
00074 #include "asterisk/lock.h"
00075 #include "asterisk/channel.h"
00076 #include "asterisk/config.h"
00077 #include "asterisk/logger.h"
00078 #include "asterisk/module.h"
00079 #include "asterisk/pbx.h"
00080 #include "asterisk/options.h"
00081 #include "asterisk/file.h"
00082 #include "asterisk/ulaw.h"
00083 #include "asterisk/alaw.h"
00084 #include "asterisk/callerid.h"
00085 #include "asterisk/adsi.h"
00086 #include "asterisk/cli.h"
00087 #include "asterisk/cdr.h"
00088 #include "asterisk/features.h"
00089 #include "asterisk/musiconhold.h"
00090 #include "asterisk/say.h"
00091 #include "asterisk/tdd.h"
00092 #include "asterisk/app.h"
00093 #include "asterisk/dsp.h"
00094 #include "asterisk/astdb.h"
00095 #include "asterisk/manager.h"
00096 #include "asterisk/causes.h"
00097 #include "asterisk/term.h"
00098 #include "asterisk/utils.h"
00099 #include "asterisk/transcap.h"
00100 #include "asterisk/stringfields.h"
00101 #include "asterisk/abstract_jb.h"
00102 #include "asterisk/smdi.h"
00103 #include "asterisk/astobj.h"
00104 #define SMDI_MD_WAIT_TIMEOUT 1500 /* 1.5 seconds */
00105 
00106 #include "asterisk/dahdi_compat.h"
00107 #include "asterisk/tonezone_compat.h"
00108 
00109 /*! Global jitterbuffer configuration - by default, jb is disabled */
00110 static struct ast_jb_conf default_jbconf =
00111 {
00112    .flags = 0,
00113    .max_size = -1,
00114    .resync_threshold = -1,
00115    .impl = ""
00116 };
00117 static struct ast_jb_conf global_jbconf;
00118 
00119 #ifndef DAHDI_TONEDETECT
00120 /* Work around older code with no tone detect */
00121 #define DAHDI_EVENT_DTMFDOWN 0
00122 #define DAHDI_EVENT_DTMFUP 0
00123 #endif
00124 
00125 /* define this to send PRI user-user information elements */
00126 #undef SUPPORT_USERUSER
00127 
00128 /*! 
00129  * \note Define ZHONE_HACK to cause us to go off hook and then back on hook when
00130  * the user hangs up to reset the state machine so ring works properly.
00131  * This is used to be able to support kewlstart by putting the zhone in
00132  * groundstart mode since their forward disconnect supervision is entirely
00133  * broken even though their documentation says it isn't and their support
00134  * is entirely unwilling to provide any assistance with their channel banks
00135  * even though their web site says they support their products for life.
00136  */
00137 /* #define ZHONE_HACK */
00138 
00139 /*! \note
00140  * Define if you want to check the hook state for an FXO (FXS signalled) interface
00141  * before dialing on it.  Certain FXO interfaces always think they're out of
00142  * service with this method however.
00143  */
00144 /* #define DAHDI_CHECK_HOOKSTATE */
00145 
00146 /*! \brief Typically, how many rings before we should send Caller*ID */
00147 #define DEFAULT_CIDRINGS 1
00148 
00149 #define CHANNEL_PSEUDO -12
00150 
00151 #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
00152 
00153 /*! \brief Signaling types that need to use MF detection should be placed in this macro */
00154 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FGC_CAMA) || ((p)->sig == SIG_FGC_CAMAMF) || ((p)->sig == SIG_FEATB)) 
00155 
00156 static const char tdesc[] = "DAHDI Telephony Driver"
00157 #ifdef HAVE_PRI
00158                " w/PRI"
00159 #endif
00160 ;
00161 
00162 #define SIG_EM    DAHDI_SIG_EM
00163 #define SIG_EMWINK   (0x0100000 | DAHDI_SIG_EM)
00164 #define SIG_FEATD (0x0200000 | DAHDI_SIG_EM)
00165 #define  SIG_FEATDMF (0x0400000 | DAHDI_SIG_EM)
00166 #define  SIG_FEATB   (0x0800000 | DAHDI_SIG_EM)
00167 #define  SIG_E911 (0x1000000 | DAHDI_SIG_EM)
00168 #define  SIG_FEATDMF_TA (0x2000000 | DAHDI_SIG_EM)
00169 #define  SIG_FGC_CAMA   (0x4000000 | DAHDI_SIG_EM)
00170 #define  SIG_FGC_CAMAMF (0x8000000 | DAHDI_SIG_EM)
00171 #define SIG_FXSLS DAHDI_SIG_FXSLS
00172 #define SIG_FXSGS DAHDI_SIG_FXSGS
00173 #define SIG_FXSKS DAHDI_SIG_FXSKS
00174 #define SIG_FXOLS DAHDI_SIG_FXOLS
00175 #define SIG_FXOGS DAHDI_SIG_FXOGS
00176 #define SIG_FXOKS DAHDI_SIG_FXOKS
00177 #define SIG_PRI      DAHDI_SIG_CLEAR
00178 #define  SIG_SF      DAHDI_SIG_SF
00179 #define SIG_SFWINK   (0x0100000 | DAHDI_SIG_SF)
00180 #define SIG_SF_FEATD (0x0200000 | DAHDI_SIG_SF)
00181 #define  SIG_SF_FEATDMF (0x0400000 | DAHDI_SIG_SF)
00182 #define  SIG_SF_FEATB   (0x0800000 | DAHDI_SIG_SF)
00183 #define SIG_EM_E1 DAHDI_SIG_EM_E1
00184 #define SIG_GR303FXOKS  (0x0100000 | DAHDI_SIG_FXOKS)
00185 #define SIG_GR303FXSKS  (0x0100000 | DAHDI_SIG_FXSKS)
00186 
00187 #define NUM_SPANS       32
00188 #define NUM_DCHANS      4  /*!< No more than 4 d-channels */
00189 #define MAX_CHANNELS 672      /*!< No more than a DS3 per trunk group */
00190 
00191 #define CHAN_PSEUDO  -2
00192 
00193 #define DCHAN_PROVISIONED (1 << 0)
00194 #define DCHAN_NOTINALARM  (1 << 1)
00195 #define DCHAN_UP          (1 << 2)
00196 
00197 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
00198 
00199 static char defaultcic[64] = "";
00200 static char defaultozz[64] = "";
00201 
00202 static char progzone[10] = "";
00203 
00204 static int distinctiveringaftercid = 0;
00205 
00206 static int numbufs = 4;
00207 
00208 #ifdef HAVE_PRI
00209 static struct ast_channel inuse;
00210 #ifdef PRI_GETSET_TIMERS
00211 static int pritimers[PRI_MAX_TIMERS];
00212 #endif
00213 static int pridebugfd = -1;
00214 static char pridebugfilename[1024] = "";
00215 #endif
00216 
00217 /*! \brief Wait up to 16 seconds for first digit (FXO logic) */
00218 static int firstdigittimeout = 16000;
00219 
00220 /*! \brief How long to wait for following digits (FXO logic) */
00221 static int gendigittimeout = 8000;
00222 
00223 /*! \brief How long to wait for an extra digit, if there is an ambiguous match */
00224 static int matchdigittimeout = 3000;
00225 
00226 /*! \brief Protect the interface list (of dahdi_pvt's) */
00227 AST_MUTEX_DEFINE_STATIC(iflock);
00228 
00229 
00230 static int ifcount = 0;
00231 
00232 #ifdef HAVE_PRI
00233 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
00234 #endif
00235 
00236 /*! \brief Protect the monitoring thread, so only one process can kill or start it, and not
00237    when it's doing something critical. */
00238 AST_MUTEX_DEFINE_STATIC(monlock);
00239 
00240 /*! \brief This is the thread for the monitor which checks for input on the channels
00241    which are not currently in use. */
00242 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00243 static ast_cond_t ss_thread_complete;
00244 AST_MUTEX_DEFINE_STATIC(ss_thread_lock);
00245 AST_MUTEX_DEFINE_STATIC(restart_lock);
00246 static int ss_thread_count = 0;
00247 static int num_restart_pending = 0;
00248 
00249 static int restart_monitor(void);
00250 
00251 static enum ast_bridge_result dahdi_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
00252 
00253 static int dahdi_sendtext(struct ast_channel *c, const char *text);
00254 
00255 /*! \brief Avoid the silly dahdi_getevent which ignores a bunch of events */
00256 static inline int dahdi_get_event(int fd)
00257 {
00258    int j;
00259    if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
00260       return -1;
00261    return j;
00262 }
00263 
00264 /*! \brief Avoid the silly dahdi_waitevent which ignores a bunch of events */
00265 static inline int dahdi_wait_event(int fd)
00266 {
00267    int i, j = 0;
00268    i = DAHDI_IOMUX_SIGEVENT;
00269    if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
00270       return -1;
00271    if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
00272       return -1;
00273    return j;
00274 }
00275 
00276 /*! Chunk size to read -- we use 20ms chunks to make things happy. */
00277 #define READ_SIZE 160
00278 
00279 #define MASK_AVAIL      (1 << 0) /*!< Channel available for PRI use */
00280 #define MASK_INUSE      (1 << 1) /*!< Channel currently in use */
00281 
00282 #define CALLWAITING_SILENT_SAMPLES  ( (300 * 8) / READ_SIZE) /*!< 300 ms */
00283 #define CALLWAITING_REPEAT_SAMPLES  ( (10000 * 8) / READ_SIZE) /*!< 10,000 ms */
00284 #define CIDCW_EXPIRE_SAMPLES     ( (500 * 8) / READ_SIZE) /*!< 500 ms */
00285 #define MIN_MS_SINCE_FLASH       ( (2000) )  /*!< 2000 ms */
00286 #define DEFAULT_RINGT            ( (8000 * 8) / READ_SIZE) /*!< 8,000 ms */
00287 
00288 struct dahdi_pvt;
00289 
00290 static int ringt_base = DEFAULT_RINGT;
00291 
00292 #ifdef HAVE_PRI
00293 
00294 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
00295 #define PRI_CHANNEL(p) ((p) & 0xff)
00296 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
00297 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
00298 
00299 struct dahdi_pri {
00300    pthread_t master;                /*!< Thread of master */
00301    ast_mutex_t lock;                /*!< Mutex */
00302    char idleext[AST_MAX_EXTENSION];          /*!< Where to idle extra calls */
00303    char idlecontext[AST_MAX_CONTEXT];           /*!< What context to use for idle */
00304    char idledial[AST_MAX_EXTENSION];            /*!< What to dial before dumping */
00305    int minunused;                   /*!< Min # of channels to keep empty */
00306    int minidle;                     /*!< Min # of "idling" calls to keep active */
00307    int nodetype;                    /*!< Node type */
00308    int switchtype;                     /*!< Type of switch to emulate */
00309    int nsf;                   /*!< Network-Specific Facilities */
00310    int dialplan;                    /*!< Dialing plan */
00311    int localdialplan;                  /*!< Local dialing plan */
00312    char internationalprefix[10];             /*!< country access code ('00' for european dialplans) */
00313    char nationalprefix[10];               /*!< area access code ('0' for european dialplans) */
00314    char localprefix[20];                  /*!< area access code + area code ('0'+area code for european dialplans) */
00315    char privateprefix[20];                /*!< for private dialplans */
00316    char unknownprefix[20];                /*!< for unknown dialplans */
00317    int dchannels[NUM_DCHANS];             /*!< What channel are the dchannels on */
00318    int trunkgroup;                     /*!< What our trunkgroup is */
00319    int mastertrunkgroup;                  /*!< What trunk group is our master */
00320    int prilogicalspan;                 /*!< Logical span number within trunk group */
00321    int numchans;                    /*!< Num of channels we represent */
00322    int overlapdial;                 /*!< In overlap dialing mode */
00323    int facilityenable;                 /*!< Enable facility IEs */
00324    struct pri *dchans[NUM_DCHANS];              /*!< Actual d-channels */
00325    int dchanavail[NUM_DCHANS];               /*!< Whether each channel is available */
00326    struct pri *pri;                 /*!< Currently active D-channel */
00327    int debug;
00328    int fds[NUM_DCHANS];                /*!< FD's for d-channels */
00329    int offset;
00330    int span;
00331    int resetting;
00332    int resetpos;
00333 #ifdef HAVE_PRI_INBANDDISCONNECT
00334    unsigned int inbanddisconnect:1;          /*!< Should we support inband audio after receiving DISCONNECT? */
00335 #endif
00336    time_t lastreset;                /*!< time when unused channels were last reset */
00337    long resetinterval;                 /*!< Interval (in seconds) for resetting unused channels */
00338    struct dahdi_pvt *pvts[MAX_CHANNELS];           /*!< Member channel pvt structs */
00339    struct dahdi_pvt *crvs;                /*!< Member CRV structs */
00340    struct dahdi_pvt *crvend;                 /*!< Pointer to end of CRV structs */
00341 };
00342 
00343 
00344 static struct dahdi_pri pris[NUM_SPANS];
00345 
00346 #if 0
00347 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
00348 #else
00349 #define DEFAULT_PRI_DEBUG 0
00350 #endif
00351 
00352 static inline void pri_rel(struct dahdi_pri *pri)
00353 {
00354    ast_mutex_unlock(&pri->lock);
00355 }
00356 
00357 #else
00358 /*! Shut up the compiler */
00359 struct dahdi_pri;
00360 #endif
00361 
00362 #define SUB_REAL  0        /*!< Active call */
00363 #define SUB_CALLWAIT 1        /*!< Call-Waiting call on hold */
00364 #define SUB_THREEWAY 2        /*!< Three-way call */
00365 
00366 /* Polarity states */
00367 #define POLARITY_IDLE   0
00368 #define POLARITY_REV    1
00369 
00370 
00371 static struct dahdi_distRings drings;
00372 
00373 struct distRingData {
00374    int ring[3];
00375 };
00376 struct ringContextData {
00377    char contextData[AST_MAX_CONTEXT];
00378 };
00379 struct dahdi_distRings {
00380    struct distRingData ringnum[3];
00381    struct ringContextData ringContext[3];
00382 };
00383 
00384 static char *subnames[] = {
00385    "Real",
00386    "Callwait",
00387    "Threeway"
00388 };
00389 
00390 struct dahdi_subchannel {
00391    int dfd;
00392    struct ast_channel *owner;
00393    int chan;
00394    short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
00395    struct ast_frame f;     /*!< One frame for each channel.  How did this ever work before? */
00396    unsigned int needringing:1;
00397    unsigned int needbusy:1;
00398    unsigned int needcongestion:1;
00399    unsigned int needcallerid:1;
00400    unsigned int needanswer:1;
00401    unsigned int needflash:1;
00402    unsigned int needhold:1;
00403    unsigned int needunhold:1;
00404    unsigned int linear:1;
00405    unsigned int inthreeway:1;
00406    struct dahdi_confinfo curconf;
00407 };
00408 
00409 #define CONF_USER_REAL     (1 << 0)
00410 #define CONF_USER_THIRDCALL   (1 << 1)
00411 
00412 #define MAX_SLAVES   4
00413 
00414 static struct dahdi_pvt {
00415    ast_mutex_t lock;
00416    struct ast_channel *owner;       /*!< Our current active owner (if applicable) */
00417                      /*!< Up to three channels can be associated with this call */
00418       
00419    struct dahdi_subchannel sub_unused;    /*!< Just a safety precaution */
00420    struct dahdi_subchannel subs[3];       /*!< Sub-channels */
00421    struct dahdi_confinfo saveconf;        /*!< Saved conference info */
00422 
00423    struct dahdi_pvt *slaves[MAX_SLAVES];     /*!< Slave to us (follows our conferencing) */
00424    struct dahdi_pvt *master;           /*!< Master to us (we follow their conferencing) */
00425    int inconference;          /*!< If our real should be in the conference */
00426    
00427    int buf_no;             /*!< Number of buffers */
00428    int buf_policy;            /*!< Buffer policy */
00429    int sig;             /*!< Signalling style */
00430    int radio;              /*!< radio type */
00431    int outsigmod;             /*!< Outbound Signalling style (modifier) */
00432    int oprmode;               /*!< "Operator Services" mode */
00433    struct dahdi_pvt *oprpeer;          /*!< "Operator Services" peer tech_pvt ptr */
00434    float rxgain;
00435    float txgain;
00436    int tonezone;              /*!< tone zone for this chan, or -1 for default */
00437    struct dahdi_pvt *next;          /*!< Next channel in list */
00438    struct dahdi_pvt *prev;          /*!< Prev channel in list */
00439 
00440    /* flags */
00441    unsigned int adsi:1;
00442    unsigned int answeronpolarityswitch:1;
00443    unsigned int busydetect:1;
00444    unsigned int callreturn:1;
00445    unsigned int callwaiting:1;
00446    unsigned int callwaitingcallerid:1;
00447    unsigned int cancallforward:1;
00448    unsigned int canpark:1;
00449    unsigned int confirmanswer:1;       /*!< Wait for '#' to confirm answer */
00450    unsigned int destroy:1;
00451    unsigned int didtdd:1;           /*!< flag to say its done it once */
00452    unsigned int dialednone:1;
00453    unsigned int dialing:1;
00454    unsigned int digital:1;
00455    unsigned int dnd:1;
00456    unsigned int echobreak:1;
00457    unsigned int echocanbridged:1;
00458    unsigned int echocanon:1;
00459    unsigned int faxhandled:1;       /*!< Has a fax tone already been handled? */
00460    unsigned int firstradio:1;
00461    unsigned int hanguponpolarityswitch:1;
00462    unsigned int hardwaredtmf:1;
00463    unsigned int hidecallerid:1;
00464    unsigned int hidecalleridname:1;      /*!< Hide just the name not the number for legacy PBX use */
00465    unsigned int ignoredtmf:1;
00466    unsigned int immediate:1;        /*!< Answer before getting digits? */
00467    unsigned int inalarm:1;
00468    unsigned int unknown_alarm:1;
00469    unsigned int mate:1;          /*!< flag to say its in MATE mode */
00470    unsigned int outgoing:1;
00471    unsigned int overlapdial:1;
00472    unsigned int permcallwaiting:1;
00473    unsigned int permhidecallerid:1;    /*!< Whether to hide our outgoing caller ID or not */
00474    unsigned int priindication_oob:1;
00475    unsigned int priexclusive:1;
00476    unsigned int pulse:1;
00477    unsigned int pulsedial:1;        /*!< whether a pulse dial phone is detected */
00478    unsigned int restartpending:1;      /*!< flag to ensure counted only once for restart */
00479    unsigned int restrictcid:1;         /*!< Whether restrict the callerid -> only send ANI */
00480    unsigned int threewaycalling:1;
00481    unsigned int transfer:1;
00482    unsigned int use_callerid:1;        /*!< Whether or not to use caller id on this channel */
00483    unsigned int use_callingpres:1;        /*!< Whether to use the callingpres the calling switch sends */
00484    unsigned int usedistinctiveringdetection:1;
00485    unsigned int dahditrcallerid:1;        /*!< should we use the callerid from incoming call on dahdi transfer or not */
00486    unsigned int transfertobusy:1;         /*!< allow flash-transfers to busy channels */
00487 #if defined(HAVE_PRI)
00488    unsigned int alerting:1;
00489    unsigned int alreadyhungup:1;
00490    unsigned int isidlecall:1;
00491    unsigned int proceeding:1;
00492    unsigned int progress:1;
00493    unsigned int resetting:1;
00494    unsigned int setup_ack:1;
00495 #endif
00496    unsigned int use_smdi:1;      /* Whether to use SMDI on this channel */
00497    struct ast_smdi_interface *smdi_iface; /* The serial port to listen for SMDI data on */
00498 
00499    struct dahdi_distRings drings;
00500 
00501    char context[AST_MAX_CONTEXT];
00502    char defcontext[AST_MAX_CONTEXT];
00503    char exten[AST_MAX_EXTENSION];
00504    char language[MAX_LANGUAGE];
00505    char mohinterpret[MAX_MUSICCLASS];
00506    char mohsuggest[MAX_MUSICCLASS];
00507 #ifdef PRI_ANI
00508    char cid_ani[AST_MAX_EXTENSION];
00509 #endif
00510    char cid_num[AST_MAX_EXTENSION];
00511    int cid_ton;               /*!< Type Of Number (TON) */
00512    char cid_name[AST_MAX_EXTENSION];
00513    char lastcid_num[AST_MAX_EXTENSION];
00514    char lastcid_name[AST_MAX_EXTENSION];
00515    char *origcid_num;            /*!< malloced original callerid */
00516    char *origcid_name;           /*!< malloced original callerid */
00517    char callwait_num[AST_MAX_EXTENSION];
00518    char callwait_name[AST_MAX_EXTENSION];
00519    char rdnis[AST_MAX_EXTENSION];
00520    char dnid[AST_MAX_EXTENSION];
00521    ast_group_t group;
00522    int law;
00523    int confno;             /*!< Our conference */
00524    int confusers;             /*!< Who is using our conference */
00525    int propconfno;               /*!< Propagated conference number */
00526    ast_group_t callgroup;
00527    ast_group_t pickupgroup;
00528    int channel;               /*!< Channel Number or CRV */
00529    int span;               /*!< Span number */
00530    time_t guardtime;          /*!< Must wait this much time before using for new call */
00531    int cid_signalling;           /*!< CID signalling type bell202 or v23 */
00532    int cid_start;             /*!< CID start indicator, polarity or ring */
00533    int callingpres;           /*!< The value of callling presentation that we're going to use when placing a PRI call */
00534    int callwaitingrepeat;           /*!< How many samples to wait before repeating call waiting */
00535    int cidcwexpire;           /*!< When to expire our muting for CID/CW */
00536    unsigned char *cidspill;
00537    int cidpos;
00538    int cidlen;
00539    int ringt;
00540    int ringt_base;
00541    int stripmsd;
00542    int callwaitcas;
00543    int callwaitrings;
00544    int echocancel;
00545    int echotraining;
00546    char echorest[20];
00547    int busycount;
00548    int busy_tonelength;
00549    int busy_quietlength;
00550    int callprogress;
00551    struct timeval flashtime;        /*!< Last flash-hook time */
00552    struct ast_dsp *dsp;
00553    int cref;               /*!< Call reference number */
00554    struct dahdi_dialoperation dop;
00555    int whichwink;             /*!< SIG_FEATDMF_TA Which wink are we on? */
00556    char finaldial[64];
00557    char accountcode[AST_MAX_ACCOUNT_CODE];      /*!< Account code */
00558    int amaflags;              /*!< AMA Flags */
00559    struct tdd_state *tdd;           /*!< TDD flag */
00560    char call_forward[AST_MAX_EXTENSION];
00561    char mailbox[AST_MAX_EXTENSION];
00562    char dialdest[256];
00563    int onhooktime;
00564    int msgstate;
00565    int distinctivering;          /*!< Which distinctivering to use */
00566    int cidrings;              /*!< Which ring to deliver CID on */
00567    int dtmfrelax;             /*!< whether to run in relaxed DTMF mode */
00568    int fake_event;
00569    int polarityonanswerdelay;
00570    struct timeval polaritydelaytv;
00571    int sendcalleridafter;
00572 #ifdef HAVE_PRI
00573    struct dahdi_pri *pri;
00574    struct dahdi_pvt *bearer;
00575    struct dahdi_pvt *realcall;
00576    q931_call *call;
00577    int prioffset;
00578    int logicalspan;
00579 #endif   
00580    int polarity;
00581    int dsp_features;
00582    char begindigit;
00583 } *iflist = NULL, *ifend = NULL;
00584 
00585 /*! \brief Channel configuration from chan_dahdi.conf .
00586  * This struct is used for parsing the [channels] section of chan_dahdi.conf.
00587  * Generally there is a field here for every possible configuration item.
00588  *
00589  * The state of fields is saved along the parsing and whenever a 'channel'
00590  * statement is reached, the current dahdi_chan_conf is used to configure the 
00591  * channel (struct dahdi_pvt)
00592  *
00593  * @seealso dahdi_chan_init for the default values.
00594  */
00595 struct dahdi_chan_conf {
00596    struct dahdi_pvt chan;
00597 #ifdef HAVE_PRI
00598    struct dahdi_pri pri;
00599 #endif
00600    struct dahdi_params timing;
00601 
00602    char smdi_port[SMDI_MAX_FILENAME_LEN];
00603 };
00604 
00605 /** returns a new dahdi_chan_conf with default values (by-value) */
00606 static struct dahdi_chan_conf dahdi_chan_conf_default(void) {
00607    /* recall that if a field is not included here it is initialized
00608     * to 0 or equivalent
00609     */
00610    struct dahdi_chan_conf conf = {
00611 #ifdef HAVE_PRI
00612       .pri = {
00613          .nsf = PRI_NSF_NONE,
00614          .switchtype = PRI_SWITCH_NI2,
00615          .dialplan = PRI_NATIONAL_ISDN + 1,
00616          .localdialplan = PRI_NATIONAL_ISDN + 1,
00617          .nodetype = PRI_CPE,
00618 
00619          .minunused = 2,
00620          .idleext = "",
00621          .idledial = "",
00622          .internationalprefix = "",
00623          .nationalprefix = "",
00624          .localprefix = "",
00625          .privateprefix = "",
00626          .unknownprefix = "",
00627 
00628          .resetinterval = 3600
00629       },
00630 #endif
00631       .chan = {
00632          .context = "default",
00633          .cid_num = "",
00634          .cid_name = "",
00635          .mohinterpret = "default",
00636          .mohsuggest = "",
00637          .transfertobusy = 1,
00638 
00639          .cid_signalling = CID_SIG_BELL,
00640          .cid_start = CID_START_RING,
00641          .dahditrcallerid = 0,
00642          .use_callerid = 1,
00643          .sig = -1,
00644          .outsigmod = -1,
00645 
00646          .tonezone = -1,
00647 
00648          .echocancel = 1,
00649 
00650          .busycount = 3,
00651 
00652          .accountcode = "",
00653 
00654          .mailbox = "",
00655 
00656 
00657          .polarityonanswerdelay = 600,
00658 
00659          .sendcalleridafter = DEFAULT_CIDRINGS,
00660 
00661          .buf_policy = DAHDI_POLICY_IMMEDIATE,
00662          .buf_no = numbufs
00663       },
00664       .timing = {
00665          .prewinktime = -1,
00666          .preflashtime = -1,
00667          .winktime = -1,
00668          .flashtime = -1,
00669          .starttime = -1,
00670          .rxwinktime = -1,
00671          .rxflashtime = -1,
00672          .debouncetime = -1
00673       },
00674       .smdi_port = "/dev/ttyS0",
00675    };
00676 
00677    return conf;
00678 }
00679 
00680 
00681 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause);
00682 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
00683 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
00684 static int dahdi_sendtext(struct ast_channel *c, const char *text);
00685 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
00686 static int dahdi_hangup(struct ast_channel *ast);
00687 static int dahdi_answer(struct ast_channel *ast);
00688 static struct ast_frame *dahdi_read(struct ast_channel *ast);
00689 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
00690 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
00691 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
00692 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00693 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
00694 static int dahdi_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len); 
00695 
00696 static const struct ast_channel_tech dahdi_tech = {
00697    .type = "DAHDI",
00698    .description = tdesc,
00699    .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
00700    .requester = dahdi_request,
00701    .send_digit_begin = dahdi_digit_begin,
00702    .send_digit_end = dahdi_digit_end,
00703    .send_text = dahdi_sendtext,
00704    .call = dahdi_call,
00705    .hangup = dahdi_hangup,
00706    .answer = dahdi_answer,
00707    .read = dahdi_read,
00708    .write = dahdi_write,
00709    .bridge = dahdi_bridge,
00710    .exception = dahdi_exception,
00711    .indicate = dahdi_indicate,
00712    .fixup = dahdi_fixup,
00713    .setoption = dahdi_setoption,
00714    .func_channel_read = dahdi_func_read,
00715 };
00716 
00717 static const struct ast_channel_tech zap_tech = {
00718    .type = "Zap",
00719    .description = tdesc,
00720    .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
00721    .requester = dahdi_request,
00722    .send_digit_begin = dahdi_digit_begin,
00723    .send_digit_end = dahdi_digit_end,
00724    .send_text = dahdi_sendtext,
00725    .call = dahdi_call,
00726    .hangup = dahdi_hangup,
00727    .answer = dahdi_answer,
00728    .read = dahdi_read,
00729    .write = dahdi_write,
00730    .bridge = dahdi_bridge,
00731    .exception = dahdi_exception,
00732    .indicate = dahdi_indicate,
00733    .fixup = dahdi_fixup,
00734    .setoption = dahdi_setoption,
00735    .func_channel_read = dahdi_func_read,
00736 };
00737 
00738 static const struct ast_channel_tech *chan_tech;
00739 
00740 #ifdef HAVE_PRI
00741 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
00742 #else
00743 #define GET_CHANNEL(p) ((p)->channel)
00744 #endif
00745 
00746 struct dahdi_pvt *round_robin[32];
00747 
00748 #ifdef HAVE_PRI
00749 static inline int pri_grab(struct dahdi_pvt *pvt, struct dahdi_pri *pri)
00750 {
00751    int res;
00752    /* Grab the lock first */
00753    do {
00754       res = ast_mutex_trylock(&pri->lock);
00755       if (res) {
00756          DEADLOCK_AVOIDANCE(&pvt->lock);
00757       }
00758    } while (res);
00759    /* Then break the poll */
00760    if (pri->master != AST_PTHREADT_NULL)
00761       pthread_kill(pri->master, SIGURG);
00762    return 0;
00763 }
00764 #endif
00765 
00766 #define NUM_CADENCE_MAX 25
00767 static int num_cadence = 4;
00768 static int user_has_defined_cadences = 0;
00769 
00770 static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
00771    { { 125, 125, 2000, 4000 } },       /*!< Quick chirp followed by normal ring */
00772    { { 250, 250, 500, 1000, 250, 250, 500, 4000 } }, /*!< British style ring */
00773    { { 125, 125, 125, 125, 125, 4000 } }, /*!< Three short bursts */
00774    { { 1000, 500, 2500, 5000 } },   /*!< Long ring */
00775 };
00776 
00777 /*! \brief cidrings says in which pause to transmit the cid information, where the first pause