00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include "asterisk.h"
00049
00050 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
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
00105
00106 #include "asterisk/dahdi_compat.h"
00107 #include "asterisk/tonezone_compat.h"
00108
00109
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
00121 #define DAHDI_EVENT_DTMFDOWN 0
00122 #define DAHDI_EVENT_DTMFUP 0
00123 #endif
00124
00125
00126 #undef SUPPORT_USERUSER
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
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
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
00189 #define MAX_CHANNELS 672
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
00218 static int firstdigittimeout = 16000;
00219
00220
00221 static int gendigittimeout = 8000;
00222
00223
00224 static int matchdigittimeout = 3000;
00225
00226
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
00237
00238 AST_MUTEX_DEFINE_STATIC(monlock);
00239
00240
00241
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
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
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
00277 #define READ_SIZE 160
00278
00279 #define MASK_AVAIL (1 << 0)
00280 #define MASK_INUSE (1 << 1)
00281
00282 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE)
00283 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE)
00284 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE)
00285 #define MIN_MS_SINCE_FLASH ( (2000) )
00286 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
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;
00301 ast_mutex_t lock;
00302 char idleext[AST_MAX_EXTENSION];
00303 char idlecontext[AST_MAX_CONTEXT];
00304 char idledial[AST_MAX_EXTENSION];
00305 int minunused;
00306 int minidle;
00307 int nodetype;
00308 int switchtype;
00309 int nsf;
00310 int dialplan;
00311 int localdialplan;
00312 char internationalprefix[10];
00313 char nationalprefix[10];
00314 char localprefix[20];
00315 char privateprefix[20];
00316 char unknownprefix[20];
00317 int dchannels[NUM_DCHANS];
00318 int trunkgroup;
00319 int mastertrunkgroup;
00320 int prilogicalspan;
00321 int numchans;
00322 int overlapdial;
00323 int facilityenable;
00324 struct pri *dchans[NUM_DCHANS];
00325 int dchanavail[NUM_DCHANS];
00326 struct pri *pri;
00327 int debug;
00328 int fds[NUM_DCHANS];
00329 int offset;
00330 int span;
00331 int resetting;
00332 int resetpos;
00333 #ifdef HAVE_PRI_INBANDDISCONNECT
00334 unsigned int inbanddisconnect:1;
00335 #endif
00336 time_t lastreset;
00337 long resetinterval;
00338 struct dahdi_pvt *pvts[MAX_CHANNELS];
00339 struct dahdi_pvt *crvs;
00340 struct dahdi_pvt *crvend;
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
00359 struct dahdi_pri;
00360 #endif
00361
00362 #define SUB_REAL 0
00363 #define SUB_CALLWAIT 1
00364 #define SUB_THREEWAY 2
00365
00366
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;
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;
00417
00418
00419 struct dahdi_subchannel sub_unused;
00420 struct dahdi_subchannel subs[3];
00421 struct dahdi_confinfo saveconf;
00422
00423 struct dahdi_pvt *slaves[MAX_SLAVES];
00424 struct dahdi_pvt *master;
00425 int inconference;
00426
00427 int buf_no;
00428 int buf_policy;
00429 int sig;
00430 int radio;
00431 int outsigmod;
00432 int oprmode;
00433 struct dahdi_pvt *oprpeer;
00434 float rxgain;
00435 float txgain;
00436 int tonezone;
00437 struct dahdi_pvt *next;
00438 struct dahdi_pvt *prev;
00439
00440
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;
00450 unsigned int destroy:1;
00451 unsigned int didtdd:1;
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;
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;
00465 unsigned int ignoredtmf:1;
00466 unsigned int immediate:1;
00467 unsigned int inalarm:1;
00468 unsigned int unknown_alarm:1;
00469 unsigned int mate:1;
00470 unsigned int outgoing:1;
00471 unsigned int overlapdial:1;
00472 unsigned int permcallwaiting:1;
00473 unsigned int permhidecallerid:1;
00474 unsigned int priindication_oob:1;
00475 unsigned int priexclusive:1;
00476 unsigned int pulse:1;
00477 unsigned int pulsedial:1;
00478 unsigned int restartpending:1;
00479 unsigned int restrictcid:1;
00480 unsigned int threewaycalling:1;
00481 unsigned int transfer:1;
00482 unsigned int use_callerid:1;
00483 unsigned int use_callingpres:1;
00484 unsigned int usedistinctiveringdetection:1;
00485 unsigned int dahditrcallerid:1;
00486 unsigned int transfertobusy:1;
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;
00497 struct ast_smdi_interface *smdi_iface;
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;
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;
00516 char *origcid_name;
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;
00524 int confusers;
00525 int propconfno;
00526 ast_group_t callgroup;
00527 ast_group_t pickupgroup;
00528 int channel;
00529 int span;
00530 time_t guardtime;
00531 int cid_signalling;
00532 int cid_start;
00533 int callingpres;
00534 int callwaitingrepeat;
00535 int cidcwexpire;
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;
00552 struct ast_dsp *dsp;
00553 int cref;
00554 struct dahdi_dialoperation dop;
00555 int whichwink;
00556 char finaldial[64];
00557 char accountcode[AST_MAX_ACCOUNT_CODE];
00558 int amaflags;
00559 struct tdd_state *tdd;
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;
00566 int cidrings;
00567 int dtmfrelax;
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
00586
00587
00588
00589
00590
00591
00592
00593
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
00606 static struct dahdi_chan_conf dahdi_chan_conf_default(void) {
00607
00608
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
00753 do {
00754 res = ast_mutex_trylock(&pri->lock);
00755 if (res) {
00756 DEADLOCK_AVOIDANCE(&pvt->lock);
00757 }
00758 } while (res);
00759
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 } },
00772 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } },
00773 { { 125, 125, 125, 125, 125, 4000 } },
00774 { { 1000, 500, 2500, 5000 } },
00775 };
00776
00777
00778
00779
00780
00781 static int cidrings[NUM_CADENCE_MAX] = {
00782 2,
00783 4,
00784 3,
00785 2,
00786 };
00787
00788 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
00789 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
00790
00791 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
00792 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
00793
00794 static int dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok)
00795 {
00796 int res;
00797 if (p->subs[SUB_REAL].owner == ast)
00798 res = 0;
00799 else if (p->subs[SUB_CALLWAIT].owner == ast)
00800 res = 1;
00801 else if (p->subs[SUB_THREEWAY].owner == ast)
00802 res = 2;
00803 else {
00804 res = -1;
00805 if (!nullok)
00806 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
00807 }
00808 return res;
00809 }
00810
00811 #ifdef HAVE_PRI
00812 static void wakeup_sub(struct dahdi_pvt *p, int a, struct dahdi_pri *pri)
00813 #else
00814 static void wakeup_sub(struct dahdi_pvt *p, int a, void *pri)
00815 #endif
00816 {
00817 #ifdef HAVE_PRI
00818 if (pri)
00819 ast_mutex_unlock(&pri->lock);
00820 #endif
00821 for (;;) {
00822 if (p->subs[a].owner) {
00823 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
00824 DEADLOCK_AVOIDANCE(&p->lock);
00825 } else {
00826 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
00827 ast_mutex_unlock(&p->subs[a].owner->lock);
00828 break;
00829 }
00830 } else
00831 break;
00832 }
00833 #ifdef HAVE_PRI
00834 if (pri)
00835 ast_mutex_lock(&pri->lock);
00836 #endif
00837 }
00838
00839 #ifdef HAVE_PRI
00840 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, struct dahdi_pri *pri)
00841 #else
00842 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *pri)
00843 #endif
00844 {
00845
00846 #ifdef HAVE_PRI
00847 if (pri)
00848 ast_mutex_unlock(&pri->lock);
00849 #endif
00850 for (;;) {
00851 if (p->owner) {
00852 if (ast_mutex_trylock(&p->owner->lock)) {
00853 DEADLOCK_AVOIDANCE(&p->lock);
00854 } else {
00855 ast_queue_frame(p->owner, f);
00856 ast_mutex_unlock(&p->owner->lock);
00857 break;
00858 }
00859 } else
00860 break;
00861 }
00862 #ifdef HAVE_PRI
00863 if (pri)
00864 ast_mutex_lock(&pri->lock);
00865 #endif
00866 }
00867
00868 static int restore_gains(struct dahdi_pvt *p);
00869
00870 static void swap_subs(struct dahdi_pvt *p, int a, int b)
00871 {
00872 int tchan;
00873 int tinthreeway;
00874 struct ast_channel *towner;
00875
00876 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
00877
00878 tchan = p->subs[a].chan;
00879 towner = p->subs[a].owner;
00880 tinthreeway = p->subs[a].inthreeway;
00881
00882 p->subs[a].chan = p->subs[b].chan;
00883 p->subs[a].owner = p->subs[b].owner;
00884 p->subs[a].inthreeway = p->subs[b].inthreeway;
00885
00886 p->subs[b].chan = tchan;
00887 p->subs[b].owner = towner;
00888 p->subs[b].inthreeway = tinthreeway;
00889
00890 if (p->subs[a].owner)
00891 p->subs[a].owner->fds[0] = p->subs[a].dfd;
00892 if (p->subs[b].owner)
00893 p->subs[b].owner->fds[0] = p->subs[b].dfd;
00894 wakeup_sub(p, a, NULL);
00895 wakeup_sub(p, b, NULL);
00896 }
00897
00898 static int dahdi_open(char *fn)
00899 {
00900 int fd;
00901 int isnum;
00902 int chan = 0;
00903 int bs;
00904 int x;
00905 isnum = 1;
00906 for (x = 0; x < strlen(fn); x++) {
00907 if (!isdigit(fn[x])) {
00908 isnum = 0;
00909 break;
00910 }
00911 }
00912 if (isnum) {
00913 chan = atoi(fn);
00914 if (chan < 1) {
00915 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
00916 return -1;
00917 }
00918 #ifdef HAVE_ZAPTEL
00919 fn = "/dev/zap/channel";
00920 #else
00921 fn = "/dev/dahdi/channel";
00922 #endif
00923 }
00924 fd = open(fn, O_RDWR | O_NONBLOCK);
00925 if (fd < 0) {
00926 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
00927 return -1;
00928 }
00929 if (chan) {
00930 if (ioctl(fd, DAHDI_SPECIFY, &chan)) {
00931 x = errno;
00932 close(fd);
00933 errno = x;
00934 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
00935 return -1;
00936 }
00937 }
00938 bs = READ_SIZE;
00939 if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs) == -1) {
00940 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
00941 x = errno;
00942 close(fd);
00943 errno = x;
00944 return -1;
00945 }
00946 return fd;
00947 }
00948
00949 static void dahdi_close(int fd)
00950 {
00951 if (fd > 0)
00952 close(fd);
00953 }
00954
00955 static void dahdi_close_sub(struct dahdi_pvt *chan_pvt, int sub_num)
00956 {
00957 dahdi_close(chan_pvt->subs[sub_num].dfd);
00958 chan_pvt->subs[sub_num].dfd = -1;
00959 }
00960
00961 #ifdef HAVE_PRI
00962 static void dahdi_close_pri_fd(struct dahdi_pri *pri, int fd_num)
00963 {
00964 dahdi_close(pri->fds[fd_num]);
00965 pri->fds[fd_num] = -1;
00966 }
00967 #endif
00968
00969 static int dahdi_setlinear(int dfd, int linear)
00970 {
00971 int res;
00972 res = ioctl(dfd, DAHDI_SETLINEAR, &linear);
00973 if (res)
00974 return res;
00975 return 0;
00976 }
00977
00978
00979 static int alloc_sub(struct dahdi_pvt *p, int x)
00980 {
00981 struct dahdi_bufferinfo bi;
00982 int res;
00983 if (p->subs[x].dfd < 0) {
00984 #ifdef HAVE_ZAPTEL
00985 p->subs[x].dfd = dahdi_open("/dev/zap/pseudo");
00986 #else
00987 p->subs[x].dfd = dahdi_open("/dev/dahdi/pseudo");
00988 #endif
00989 if (p->subs[x].dfd > -1) {
00990 res = ioctl(p->subs[x].dfd, DAHDI_GET_BUFINFO, &bi);
00991 if (!res) {
00992 bi.txbufpolicy = p->buf_policy;
00993 bi.rxbufpolicy = p->buf_policy;
00994 bi.numbufs = p->buf_no;
00995 res = ioctl(p->subs[x].dfd, DAHDI_SET_BUFINFO, &bi);
00996 if (res < 0) {
00997 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", x, strerror(errno));
00998 }
00999 } else
01000 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
01001 if (ioctl(p->subs[x].dfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
01002 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].dfd, strerror(errno));
01003 dahdi_close_sub(p, x);
01004 return -1;
01005 }
01006 if (option_debug)
01007 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].dfd, p->subs[x].chan);
01008 return 0;
01009 } else
01010 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
01011 return -1;
01012 }
01013 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
01014 return -1;
01015 }
01016
01017 static int unalloc_sub(struct dahdi_pvt *p, int x)
01018 {
01019 if (!x) {
01020 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
01021 return -1;
01022 }
01023 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
01024 dahdi_close_sub(p, x);
01025 p->subs[x].linear = 0;
01026 p->subs[x].chan = 0;
01027 p->subs[x].owner = NULL;
01028 p->subs[x].inthreeway = 0;
01029 p->polarity = POLARITY_IDLE;
01030 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
01031 return 0;
01032 }
01033
01034 static int digit_to_dtmfindex(char digit)
01035 {
01036 if (isdigit(digit))
01037 return DAHDI_TONE_DTMF_BASE + (digit - '0');
01038 else if (digit >= 'A' && digit <= 'D')
01039 return DAHDI_TONE_DTMF_A + (digit - 'A');
01040 else if (digit >= 'a' && digit <= 'd')
01041 return DAHDI_TONE_DTMF_A + (digit - 'a');
01042 else if (digit == '*')
01043 return DAHDI_TONE_DTMF_s;
01044 else if (digit == '#')
01045 return DAHDI_TONE_DTMF_p;
01046 else
01047 return -1;
01048 }
01049
01050 static int dahdi_digit_begin(struct ast_channel *chan, char digit)
01051 {
01052 struct dahdi_pvt *pvt;
01053 int index;
01054 int dtmf = -1;
01055
01056 pvt = chan->tech_pvt;
01057
01058 ast_mutex_lock(&pvt->lock);
01059
01060 index = dahdi_get_index(chan, pvt, 0);
01061
01062 if ((index != SUB_REAL) || !pvt->owner)
01063 goto out;
01064
01065 #ifdef HAVE_PRI
01066 if ((pvt->sig == SIG_PRI) && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
01067 if (pvt->setup_ack) {
01068 if (!pri_grab(pvt, pvt->pri)) {
01069 pri_information(pvt->pri->pri, pvt->call, digit);
01070 pri_rel(pvt->pri);
01071 } else
01072 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
01073 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
01074 int res;
01075 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
01076 res = strlen(pvt->dialdest);
01077 pvt->dialdest[res++] = digit;
01078 pvt->dialdest[res] = '\0';
01079 }
01080 goto out;
01081 }
01082 #endif
01083 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
01084 goto out;
01085
01086 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &dtmf)) {
01087 int res;
01088 struct dahdi_dialoperation zo = {
01089 .op = DAHDI_DIAL_OP_APPEND,
01090 .dialstr[0] = 'T',
01091 .dialstr[1] = digit,
01092 .dialstr[2] = 0,
01093 };
01094 if ((res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_DIAL, &zo)))
01095 ast_log(LOG_WARNING, "Couldn't dial digit %c: %s\n", digit, strerror(errno));
01096 else
01097 pvt->dialing = 1;
01098 } else {
01099 ast_log(LOG_DEBUG, "Started VLDTMF digit '%c'\n", digit);
01100 pvt->dialing = 1;
01101 pvt->begindigit = digit;
01102 }
01103
01104 out:
01105 ast_mutex_unlock(&pvt->lock);
01106
01107 return 0;
01108 }
01109
01110 static int dahdi_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
01111 {
01112 struct dahdi_pvt *pvt;
01113 int res = 0;
01114 int index;
01115 int x;
01116
01117 pvt = chan->tech_pvt;
01118
01119 ast_mutex_lock(&pvt->lock);
01120
01121 index = dahdi_get_index(chan, pvt, 0);
01122
01123 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
01124 goto out;
01125
01126 #ifdef HAVE_PRI
01127
01128 if (pvt->sig == SIG_PRI && !pvt->begindigit)
01129 goto out;
01130 #endif
01131
01132 if (pvt->begindigit) {
01133 x = -1;
01134 ast_log(LOG_DEBUG, "Ending VLDTMF digit '%c'\n", digit);
01135 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &x);
01136 pvt->dialing = 0;
01137 pvt->begindigit = 0;
01138 }
01139
01140 out:
01141 ast_mutex_unlock(&pvt->lock);
01142
01143 return res;
01144 }
01145
01146 static char *events[] = {
01147 "No event",
01148 "On hook",
01149 "Ring/Answered",
01150 "Wink/Flash",
01151 "Alarm",
01152 "No more alarm",
01153 "HDLC Abort",
01154 "HDLC Overrun",
01155 "HDLC Bad FCS",
01156 "Dial Complete",
01157 "Ringer On",
01158 "Ringer Off",
01159 "Hook Transition Complete",
01160 "Bits Changed",
01161 "Pulse Start",
01162 "Timer Expired",
01163 "Timer Ping",
01164 "Polarity Reversal",
01165 "Ring Begin",
01166 };
01167
01168 static struct {
01169 int alarm;
01170 char *name;
01171 } alarms[] = {
01172 { DAHDI_ALARM_RED, "Red Alarm" },
01173 { DAHDI_ALARM_YELLOW, "Yellow Alarm" },
01174 { DAHDI_ALARM_BLUE, "Blue Alarm" },
01175 { DAHDI_ALARM_RECOVER, "Recovering" },
01176 { DAHDI_ALARM_LOOPBACK, "Loopback" },
01177 { DAHDI_ALARM_NOTOPEN, "Not Open" },
01178 { DAHDI_ALARM_NONE, "None" },
01179 };
01180
01181 static char *alarm2str(int alarm)
01182 {
01183 int x;
01184 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
01185 if (alarms[x].alarm & alarm)
01186 return alarms[x].name;
01187 }
01188 return alarm ? "Unknown Alarm" : "No Alarm";
01189 }
01190
01191 static char *event2str(int event)
01192 {
01193 static char buf[256];
01194 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
01195 return events[event];
01196 sprintf(buf, "Event %d", event);
01197 return buf;
01198 }
01199
01200 #ifdef HAVE_PRI
01201 static char *dialplan2str(int dialplan)
01202 {
01203 if (dialplan == -1) {
01204 return("Dynamically set dialplan in ISDN");
01205 }
01206 return (pri_plan2str(dialplan));
01207 }
01208 #endif
01209
01210 static char *dahdi_sig2str(int sig)
01211 {
01212 static char buf[256];
01213 switch (sig) {
01214 case SIG_EM:
01215 return "E & M Immediate";
01216 case SIG_EMWINK:
01217 return "E & M Wink";
01218 case SIG_EM_E1:
01219 return "E & M E1";
01220 case SIG_FEATD:
01221 return "Feature Group D (DTMF)";
01222 case SIG_FEATDMF:
01223 return "Feature Group D (MF)";
01224 case SIG_FEATDMF_TA:
01225 return "Feature Groud D (MF) Tandem Access";
01226 case SIG_FEATB:
01227 return "Feature Group B (MF)";
01228 case SIG_E911:
01229 return "E911 (MF)";
01230 case SIG_FGC_CAMA:
01231 return "FGC/CAMA (Dialpulse)";
01232 case SIG_FGC_CAMAMF:
01233 return "FGC/CAMA (MF)";
01234 case SIG_FXSLS:
01235 return "FXS Loopstart";
01236 case SIG_FXSGS:
01237 return "FXS Groundstart";
01238 case SIG_FXSKS:
01239 return "FXS Kewlstart";
01240 case SIG_FXOLS:
01241 return "FXO Loopstart";
01242 case SIG_FXOGS:
01243 return "FXO Groundstart";
01244 case SIG_FXOKS:
01245 return "FXO Kewlstart";
01246 case SIG_PRI:
01247 return "ISDN PRI";
01248 case SIG_SF:
01249 return "SF (Tone) Immediate";
01250 case SIG_SFWINK:
01251 return "SF (Tone) Wink";
01252 case SIG_SF_FEATD:
01253 return "SF (Tone) with Feature Group D (DTMF)";
01254 case SIG_SF_FEATDMF:
01255 return "SF (Tone) with Feature Group D (MF)";
01256 case SIG_SF_FEATB:
01257 return "SF (Tone) with Feature Group B (MF)";
01258 case SIG_GR303FXOKS:
01259 return "GR-303 with FXOKS";
01260 case SIG_GR303FXSKS:
01261 return "GR-303 with FXSKS";
01262 case 0:
01263 return "Pseudo";
01264 default:
01265 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
01266 return buf;
01267 }
01268 }
01269
01270 #define sig2str dahdi_sig2str
01271
01272 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index, int slavechannel)
01273 {
01274
01275
01276 struct dahdi_confinfo zi;
01277
01278 memset(&zi, 0, sizeof(zi));
01279 zi.chan = 0;
01280
01281 if (slavechannel > 0) {
01282
01283 zi.confmode = DAHDI_CONF_DIGITALMON;
01284 zi.confno = slavechannel;
01285 } else {
01286 if (!index) {
01287
01288 zi.confmode = DAHDI_CONF_REALANDPSEUDO | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER |
01289 DAHDI_CONF_PSEUDO_TALKER | DAHDI_CONF_PSEUDO_LISTENER;
01290 } else
01291 zi.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
01292 zi.confno = p->confno;
01293 }
01294 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
01295 return 0;
01296 if (c->dfd < 0)
01297 return 0;
01298 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
01299 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d: %s\n", c->dfd, zi.confmode, zi.confno, strerror(errno));
01300 return -1;
01301 }
01302 if (slavechannel < 1) {
01303 p->confno = zi.confno;
01304 }
01305 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01306 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
01307 return 0;
01308 }
01309
01310 static int isourconf(struct dahdi_pvt *p, struct dahdi_subchannel *c)
01311 {
01312
01313 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == DAHDI_CONF_DIGITALMON))
01314 return 1;
01315
01316 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & DAHDI_CONF_TALKER))
01317 return 1;
01318 return 0;
01319 }
01320
01321 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int index)
01322 {
01323 struct dahdi_confinfo zi;
01324 if (
01325 (c->dfd < 0) ||
01326
01327 !isourconf(p, c)
01328
01329 ) return 0;
01330 memset(&zi, 0, sizeof(zi));
01331 zi.chan = 0;
01332 zi.confno = 0;
01333 zi.confmode = 0;
01334 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
01335 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d: %s\n", c->dfd, c->curconf.confmode, c->curconf.confno, strerror(errno));
01336 return -1;
01337 }
01338 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
01339 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01340 return 0;
01341 }
01342
01343 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out)
01344 {
01345 int x;
01346 int useslavenative;
01347 struct dahdi_pvt *slave = NULL;
01348
01349 useslavenative = 1;
01350
01351 for (x = 0; x < 3; x++) {
01352
01353
01354 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway)
01355 useslavenative = 0;
01356 }
01357
01358
01359 if (useslavenative) {
01360 for (x = 0; x < MAX_SLAVES; x++) {
01361 if (p->slaves[x]) {
01362 if (slave) {
01363
01364
01365 slave = NULL;
01366 useslavenative = 0;
01367 break;
01368 } else {
01369
01370 slave = p->slaves[x];
01371 }
01372 }
01373 }
01374 }
01375
01376 if (!slave)
01377 useslavenative = 0;
01378 else if (slave->law != p->law) {
01379 useslavenative = 0;
01380 slave = NULL;
01381 }
01382 if (out)
01383 *out = slave;
01384 return useslavenative;
01385 }
01386
01387 static int reset_conf(struct dahdi_pvt *p)
01388 {
01389 struct dahdi_confinfo zi;
01390 memset(&zi, 0, sizeof(zi));
01391 p->confno = -1;
01392 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
01393 if (p->subs[SUB_REAL].dfd > -1) {
01394 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &zi))
01395 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d: %s\n", p->channel, strerror(errno));
01396 }
01397 return 0;
01398 }
01399
01400 static int update_conf(struct dahdi_pvt *p)
01401 {
01402 int needconf = 0;
01403 int x;
01404 int useslavenative;
01405 struct dahdi_pvt *slave = NULL;
01406
01407 useslavenative = isslavenative(p, &slave);
01408
01409 for (x = 0; x < 3; x++) {
01410
01411 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway) {
01412 conf_add(p, &p->subs[x], x, 0);
01413 needconf++;
01414 } else {
01415 conf_del(p, &p->subs[x], x);
01416 }
01417 }
01418
01419
01420 for (x = 0; x < MAX_SLAVES; x++) {
01421 if (p->slaves[x]) {
01422 if (useslavenative)
01423 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
01424 else {
01425 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
01426 needconf++;
01427 }
01428 }
01429 }
01430
01431 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
01432 if (useslavenative)
01433 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
01434 else {
01435 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
01436 needconf++;
01437 }
01438 }
01439
01440 if (p->master) {
01441 if (isslavenative(p->master, NULL)) {
01442 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
01443 } else {
01444 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
01445 }
01446 }
01447 if (!needconf) {
01448
01449
01450 p->confno = -1;
01451 }
01452 if (option_debug)
01453 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
01454 return 0;
01455 }
01456
01457 static void dahdi_enable_ec(struct dahdi_pvt *p)
01458 {
01459 int x;
01460 int res;
01461 if (!p)
01462 return;
01463 if (p->echocanon) {
01464 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
01465 return;
01466 }
01467 if (p->digital) {
01468 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
01469 return;
01470 }
01471 if (p->echocancel) {
01472 if (p->sig == SIG_PRI) {
01473 x = 1;
01474 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x);
01475 if (res)
01476 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n", p->channel, strerror(errno));
01477 }
01478 x = p->echocancel;
01479 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL, &x);
01480 if (res)
01481 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d (%s)\n", p->channel, strerror(errno));
01482 else {
01483 p->echocanon = 1;
01484 if (option_debug)
01485 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
01486 }
01487 } else if (option_debug)
01488 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
01489 }
01490
01491 static void dahdi_train_ec(struct dahdi_pvt *p)
01492 {
01493 int x;
01494 int res;
01495 if (p && p->echocancel && p->echotraining) {
01496 x = p->echotraining;
01497 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOTRAIN, &x);
01498 if (res)
01499 ast_log(LOG_WARNING, "Unable to request echo training on channel %d: %s\n", p->channel, strerror(errno));
01500 else {
01501 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
01502 }
01503 } else
01504 ast_log(LOG_DEBUG, "No echo training requested\n");
01505 }
01506
01507 static void dahdi_disable_ec(struct dahdi_pvt *p)
01508 {
01509 int x;
01510 int res;
01511 if (p->echocancel) {
01512 x = 0;
01513 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL, &x);
01514 if (res)
01515 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d: %s\n", p->channel, strerror(errno));
01516 else if (option_debug)
01517 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
01518 }
01519 p->echocanon = 0;
01520 }
01521
01522 static void fill_txgain(struct dahdi_gains *g, float gain, int law)
01523 {
01524 int j;
01525 int k;
01526 float linear_gain = pow(10.0, gain / 20.0);
01527
01528 switch (law) {
01529 case DAHDI_LAW_ALAW:
01530 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01531 if (gain) {
01532 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01533 if (k > 32767) k = 32767;
01534 if (k < -32767) k = -32767;
01535 g->txgain[j] = AST_LIN2A(k);
01536 } else {
01537 g->txgain[j] = j;
01538 }
01539 }
01540 break;
01541 case DAHDI_LAW_MULAW:
01542 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01543 if (gain) {
01544 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01545 if (k > 32767) k = 32767;
01546 if (k < -32767) k = -32767;
01547 g->txgain[j] = AST_LIN2MU(k);
01548 } else {
01549 g->txgain[j] = j;
01550 }
01551 }
01552 break;
01553 }
01554 }
01555
01556 static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
01557 {
01558 int j;
01559 int k;
01560 float linear_gain = pow(10.0, gain / 20.0);
01561
01562 switch (law) {
01563 case DAHDI_LAW_ALAW:
01564 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01565 if (gain) {
01566 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01567 if (k > 32767) k = 32767;
01568 if (k < -32767) k = -32767;
01569 g->rxgain[j] = AST_LIN2A(k);
01570 } else {
01571 g->rxgain[j] = j;
01572 }
01573 }
01574 break;
01575 case DAHDI_LAW_MULAW:
01576 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01577 if (gain) {
01578 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01579 if (k > 32767) k = 32767;
01580 if (k < -32767) k = -32767;
01581 g->rxgain[j] = AST_LIN2MU(k);
01582 } else {
01583 g->rxgain[j] = j;
01584 }
01585 }
01586 break;
01587 }
01588 }
01589
01590 static int set_actual_txgain(int fd, int chan, float gain, int law)
01591 {
01592 struct dahdi_gains g;
01593 int res;
01594
01595 memset(&g, 0, sizeof(g));
01596 g.chan = chan;
01597 res = ioctl(fd, DAHDI_GETGAINS, &g);
01598 if (res) {
01599 if (option_debug)
01600 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01601 return res;
01602 }
01603
01604 fill_txgain(&g, gain, law);
01605
01606 return ioctl(fd, DAHDI_SETGAINS, &g);
01607 }
01608
01609 static int set_actual_rxgain(int fd, int chan, float gain, int law)
01610 {
01611 struct dahdi_gains g;
01612 int res;
01613
01614 memset(&g, 0, sizeof(g));
01615 g.chan = chan;
01616 res = ioctl(fd, DAHDI_GETGAINS, &g);
01617 if (res) {
01618 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01619 return res;
01620 }
01621
01622 fill_rxgain(&g, gain, law);
01623
01624 return ioctl(fd, DAHDI_SETGAINS, &g);
01625 }
01626
01627 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
01628 {
01629 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
01630 }
01631
01632 static int bump_gains(struct dahdi_pvt *p)
01633 {
01634 int res;
01635
01636
01637 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain + 5.0, p->txgain, p->law);
01638 if (res) {
01639 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
01640 return -1;
01641 }
01642
01643 return 0;
01644 }
01645
01646 static int restore_gains(struct dahdi_pvt *p)
01647 {
01648 int res;
01649
01650 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
01651 if (res) {
01652 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
01653 return -1;
01654 }
01655
01656 return 0;
01657 }
01658
01659 static inline int dahdi_set_hook(int fd, int hs)
01660 {
01661 int x, res;
01662
01663 x = hs;
01664 res = ioctl(fd, DAHDI_HOOK, &x);
01665
01666 if (res < 0) {
01667 if (errno == EINPROGRESS)
01668 return 0;
01669 ast_log(LOG_WARNING, "DAHDI hook failed returned %d (trying %d): %s\n", res, hs, strerror(errno));
01670
01671 }
01672
01673 return res;
01674 }
01675
01676 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted)
01677 {
01678 int x, y, res;
01679 x = muted;
01680 if (p->sig == SIG_PRI) {
01681 y = 1;
01682 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &y);
01683 if (res)
01684 ast_log(LOG_WARNING, "Unable to set audio mode on %d: %s\n", p->channel, strerror(errno));
01685 }
01686 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_CONFMUTE, &x);
01687 if (res < 0)
01688 ast_log(LOG_WARNING, "dahdi confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
01689 return res;
01690 }
01691
01692 static int save_conference(struct dahdi_pvt *p)
01693 {
01694 struct dahdi_confinfo c;
01695 int res;
01696 if (p->saveconf.confmode) {
01697 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
01698 return -1;
01699 }
01700 p->saveconf.chan = 0;
01701 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &p->saveconf);
01702 if (res) {
01703 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
01704 p->saveconf.confmode = 0;
01705 return -1;
01706 }
01707 c.chan = 0;
01708 c.confno = 0;
01709 c.confmode = DAHDI_CONF_NORMAL;
01710 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &c);
01711 if (res) {
01712 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
01713 return -1;
01714 }
01715 if (option_debug)
01716 ast_log(LOG_DEBUG, "Disabled conferencing\n");
01717 return 0;
01718 }
01719
01720 static int restore_conference(struct dahdi_pvt *p)
01721 {
01722 int res;
01723 if (p->saveconf.confmode) {
01724 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &p->saveconf);
01725 p->saveconf.confmode = 0;
01726 if (res) {
01727 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
01728 return -1;
01729 }
01730 }
01731 if (option_debug)
01732 ast_log(LOG_DEBUG, "Restored conferencing\n");
01733 return 0;
01734 }
01735
01736 static int send_callerid(struct dahdi_pvt *p);
01737
01738 static int send_cwcidspill(struct dahdi_pvt *p)
01739 {
01740 p->callwaitcas = 0;
01741 p->cidcwexpire = 0;
01742 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
01743 return -1;
01744 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
01745
01746 p->cidlen += READ_SIZE * 4;
01747 p->cidpos = 0;
01748 send_callerid(p);
01749 if (option_verbose > 2)
01750 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
01751 return 0;
01752 }
01753
01754 static int has_voicemail(struct dahdi_pvt *p)
01755 {
01756
01757 return ast_app_has_voicemail(p->mailbox, NULL);
01758 }
01759
01760 static int send_callerid(struct dahdi_pvt *p)
01761 {
01762
01763 int res;
01764
01765 if (p->subs[SUB_REAL].linear) {
01766 p->subs[SUB_REAL].linear = 0;
01767 dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
01768 }
01769 while (p->cidpos < p->cidlen) {
01770 res = write(p->subs[SUB_REAL].dfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
01771 if (res < 0) {
01772 if (errno == EAGAIN)
01773 return 0;
01774 else {
01775 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
01776 return -1;
01777 }
01778 }
01779 if (!res)
01780 return 0;
01781 p->cidpos += res;
01782 }
01783 free(p->cidspill);
01784 p->cidspill = NULL;
01785 if (p->callwaitcas) {
01786
01787 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
01788 } else
01789 restore_conference(p);
01790 return 0;
01791 }
01792
01793 static int dahdi_callwait(struct ast_channel *ast)
01794 {
01795 struct dahdi_pvt *p = ast->tech_pvt;
01796 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
01797 if (p->cidspill) {
01798 ast_log(LOG_WARNING, "Spill already exists?!?\n");
01799 free(p->cidspill);
01800 }
01801 if (!(p->cidspill = ast_malloc(2400 + 680 + READ_SIZE * 4)))
01802 return -1;
01803 save_conference(p);
01804
01805 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
01806 if (!p->callwaitrings && p->callwaitingcallerid) {
01807 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
01808 p->callwaitcas = 1;
01809 p->cidlen = 2400 + 680 + READ_SIZE * 4;
01810 } else {
01811 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
01812 p->callwaitcas = 0;
01813 p->cidlen = 2400 + READ_SIZE * 4;
01814 }
01815 p->cidpos = 0;
01816 send_callerid(p);
01817
01818 return 0;
01819 }
01820
01821 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout)
01822 {
01823 struct dahdi_pvt *p = ast->tech_pvt;
01824 int x, res, index,mysig;
01825 char *c, *n, *l;
01826 #ifdef HAVE_PRI
01827 char *s = NULL;
01828 #endif
01829 char dest[256];
01830 ast_mutex_lock(&p->lock);
01831 ast_copy_string(dest, rdest, sizeof(dest));
01832 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
01833 if ((ast->_state == AST_STATE_BUSY)) {
01834 p->subs[SUB_REAL].needbusy = 1;
01835 ast_mutex_unlock(&p->lock);
01836 return 0;
01837 }
01838 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
01839 ast_log(LOG_WARNING, "dahdi_call called on %s, neither down nor reserved\n", ast->name);
01840 ast_mutex_unlock(&p->lock);
01841 return -1;
01842 }
01843 p->dialednone = 0;
01844 if ((p->radio || (p->oprmode < 0)))
01845 {
01846
01847 ast_setstate(ast, AST_STATE_UP);
01848 ast_mutex_unlock(&p->lock);
01849 return 0;
01850 }
01851 x = DAHDI_FLUSH_READ | DAHDI_FLUSH_WRITE;
01852 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
01853 if (res)
01854 ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", p->channel, strerror(errno));
01855 p->outgoing = 1;
01856
01857 set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
01858
01859 mysig = p->sig;
01860 if (p->outsigmod > -1)
01861 mysig = p->outsigmod;
01862
01863 switch (mysig) {
01864 case SIG_FXOLS:
01865 case SIG_FXOGS:
01866 case SIG_FXOKS:
01867 if (p->owner == ast) {
01868
01869
01870
01871 p->dialing = 1;
01872 if (p->use_callerid) {
01873
01874 if (p->cidspill) {
01875 ast_log(LOG_WARNING, "cidspill already exists??\n");
01876 free(p->cidspill);
01877 }
01878 p->callwaitcas = 0;
01879 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
01880 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
01881 p->cidpos = 0;
01882 send_callerid(p);
01883 }
01884 }
01885
01886 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
01887 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
01888 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
01889 p->cidrings = cidrings[p->distinctivering - 1];
01890 } else {
01891 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
01892 ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
01893 p->cidrings = p->sendcalleridafter;
01894 }
01895
01896
01897 c = strchr(dest, '/');
01898 if (c)
01899 c++;
01900 if (c && (strlen(c) < p->stripmsd)) {
01901 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01902 c = NULL;
01903 }
01904 if (c) {
01905 p->dop.op = DAHDI_DIAL_OP_REPLACE;
01906 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
01907 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
01908 } else {
01909 p->dop.dialstr[0] = '\0';
01910 }
01911 x = DAHDI_RING;
01912 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x) && (errno != EINPROGRESS)) {
01913 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
01914 ast_mutex_unlock(&p->lock);
01915 return -1;
01916 }
01917 p->dialing = 1;
01918 } else {
01919
01920 p->callwaitrings = 0;
01921 if (ast->cid.cid_num)
01922 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
01923 else
01924 p->callwait_num[0] = '\0';
01925 if (ast->cid.cid_name)
01926 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
01927 else
01928 p->callwait_name[0] = '\0';
01929
01930 if (dahdi_callwait(ast)) {
01931 ast_mutex_unlock(&p->lock);
01932 return -1;
01933 }
01934
01935 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].dfd, DAHDI_TONE_RINGTONE))
01936 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
01937
01938 }
01939 n = ast->cid.cid_name;
01940 l = ast->cid.cid_num;
01941 if (l)
01942 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
01943 else
01944 p->lastcid_num[0] = '\0';
01945 if (n)
01946 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
01947 else
01948 p->lastcid_name[0] = '\0';
01949 ast_setstate(ast, AST_STATE_RINGING);
01950 index = dahdi_get_index(ast, p, 0);
01951 if (index > -1) {
01952 p->subs[index].needringing = 1;
01953 }
01954 break;
01955 case SIG_FXSLS:
01956 case SIG_FXSGS:
01957 case SIG_FXSKS:
01958 case SIG_EMWINK:
01959 case SIG_EM:
01960 case SIG_EM_E1:
01961 case SIG_FEATD:
01962 case SIG_FEATDMF:
01963 case SIG_E911:
01964 case SIG_FGC_CAMA:
01965 case SIG_FGC_CAMAMF:
01966 case SIG_FEATB:
01967 case SIG_SFWINK:
01968 case SIG_SF:
01969 case SIG_SF_FEATD:
01970 case SIG_SF_FEATDMF:
01971 case SIG_FEATDMF_TA:
01972 case SIG_SF_FEATB:
01973 c = strchr(dest, '/');
01974 if (c)
01975 c++;
01976 else
01977 c = "";
01978 if (strlen(c) < p->stripmsd) {
01979 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01980 ast_mutex_unlock(&p->lock);
01981 return -1;
01982 }
01983 #ifdef HAVE_PRI
01984
01985 if (!p->pri) {
01986 #endif
01987 x = DAHDI_START;
01988 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
01989 if (res < 0) {
01990 if (errno != EINPROGRESS) {
01991 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
01992 ast_mutex_unlock(&p->lock);
01993 return -1;
01994 }
01995 }
01996 #ifdef HAVE_PRI
01997 }
01998 #endif
01999 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
02000 p->dop.op = DAHDI_DIAL_OP_REPLACE;
02001
02002 c += p->stripmsd;
02003
02004 switch (mysig) {
02005 case SIG_FEATD:
02006 l = ast->cid.cid_num;
02007 if (l)
02008 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
02009 else
02010 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
02011 break;
02012 case SIG_FEATDMF:
02013 l = ast->cid.cid_num;
02014 if (l)
02015 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
02016 else
02017 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
02018 break;
02019 case SIG_FEATDMF_TA:
02020 {
02021 const char *cic, *ozz;
02022
02023
02024 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
02025 if (!ozz)
02026 ozz = defaultozz;
02027 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
02028 if (!cic)
02029 cic = defaultcic;
02030 if (!ozz || !cic) {
02031 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
02032 ast_mutex_unlock(&p->lock);
02033 return -1;
02034 }
02035 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
02036 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
02037 p->whichwink = 0;
02038 }
02039 break;
02040 case SIG_E911:
02041 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
02042 break;
02043 case SIG_FGC_CAMA:
02044 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
02045 break;
02046 case SIG_FGC_CAMAMF:
02047 case SIG_FEATB:
02048 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
02049 break;
02050 default:
02051 if (p->pulse)
02052 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
02053 else
02054 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
02055 break;
02056 }
02057
02058 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
02059 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
02060 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
02061 p->echorest[sizeof(p->echorest) - 1] = '\0';
02062 p->echobreak = 1;
02063 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
02064 } else
02065 p->echobreak = 0;
02066 if (!res) {
02067 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
02068 int saveerr = errno;
02069
02070 x = DAHDI_ONHOOK;
02071 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
02072 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
02073 ast_mutex_unlock(&p->lock);
02074 return -1;
02075 }
02076 } else
02077 ast_log(LOG_DEBUG, "Deferring dialing...\n");
02078 p->dialing = 1;
02079 if (ast_strlen_zero(c))
02080 p->dialednone = 1;
02081 ast_setstate(ast, AST_STATE_DIALING);
02082 break;
02083 case 0:
02084
02085 ast_setstate(ast, AST_STATE_UP);
02086 break;
02087 case SIG_PRI:
02088
02089 p->dialdest[0] = '\0';
02090 break;
02091 default:
02092 ast_log(LOG_DEBUG, "not yet implemented\n");
02093 ast_mutex_unlock(&p->lock);
02094 return -1;
02095 }
02096 #ifdef HAVE_PRI
02097 if (p->pri) {
02098 struct pri_sr *sr;
02099 #ifdef SUPPORT_USERUSER
02100 const char *useruser;
02101 #endif
02102 int pridialplan;
02103 int dp_strip;
02104 int prilocaldialplan;
02105 int ldp_strip;
02106 int exclusive;
02107 const char *rr_str;
02108 int redirect_reason;
02109
02110 c = strchr(dest, '/');
02111 if (c)
02112 c++;
02113 else
02114 c = dest;
02115
02116 l = NULL;
02117 n = NULL;
02118
02119 if (!p->hidecallerid) {
02120 l = ast->cid.cid_num;
02121 if (!p->hidecalleridname) {
02122 n = ast->cid.cid_name;
02123 }
02124 }
02125
02126
02127 if (strlen(c) < p->stripmsd) {
02128 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
02129 ast_mutex_unlock(&p->lock);
02130 return -1;
02131 }
02132 if (mysig != SIG_FXSKS) {
02133 p->dop.op = DAHDI_DIAL_OP_REPLACE;
02134 s = strchr(c + p->stripmsd, 'w');
02135 if (s) {
02136 if (strlen(s) > 1)
02137 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
02138 else
02139 p->dop.dialstr[0] = '\0';
02140 *s = '\0';
02141 } else {
02142 p->dop.dialstr[0] = '\0';
02143 }
02144 }
02145 if (pri_grab(p, p->pri)) {
02146 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
02147 ast_mutex_unlock(&p->lock);
02148 return -1;
02149 }
02150 if (!(p->call = pri_new_call(p->pri->pri))) {
02151 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
02152 pri_rel(p->pri);
02153 ast_mutex_unlock(&p->lock);
02154 return -1;
02155 }
02156 if (!(sr = pri_sr_new())) {
02157 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
02158 pri_destroycall(p->pri->pri, p->call);
02159 p->call = NULL;
02160 pri_rel(p->pri);
02161 ast_mutex_unlock(&p->lock);
02162 return -1;
02163 }
02164 if (p->bearer || (mysig == SIG_FXSKS)) {
02165 if (p->bearer) {
02166 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);
02167 p->bearer->call = p->call;
02168 } else
02169 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
02170 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
02171 }
02172 p->digital = IS_DIGITAL(ast->transfercapability);
02173
02174 if (p->priexclusive)
02175 exclusive = 1;
02176 else {
02177
02178 if (p->pri->nodetype == PRI_NETWORK)
02179 exclusive = 0;
02180 else
02181 exclusive = 1;
02182 }
02183
02184 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
02185 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
02186 (p->digital ? -1 :
02187 ((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
02188 if (p->pri->facilityenable)
02189 pri_facility_enable(p->pri->pri);
02190
02191 if (option_verbose > 2)
02192 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
02193 dp_strip = 0;
02194 pridialplan = p->pri->dialplan - 1;
02195 if (pridialplan == -2) {
02196 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02197 dp_strip = strlen(p->pri->internationalprefix);
02198 pridialplan = PRI_INTERNATIONAL_ISDN;
02199 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02200 dp_strip = strlen(p->pri->nationalprefix);
02201 pridialplan = PRI_NATIONAL_ISDN;
02202 } else {
02203 pridialplan = PRI_LOCAL_ISDN;
02204 }
02205 }
02206 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
02207
02208 ldp_strip = 0;
02209 prilocaldialplan = p->pri->localdialplan - 1;
02210 if ((l != NULL) && (prilocaldialplan == -2)) {
02211 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02212 ldp_strip = strlen(p->pri->internationalprefix);
02213 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
02214 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02215 ldp_strip = strlen(p->pri->nationalprefix);
02216 prilocaldialplan = PRI_NATIONAL_ISDN;
02217 } else {
02218 prilocaldialplan = PRI_LOCAL_ISDN;
02219 }
02220 }
02221 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
02222 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
02223 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
02224 if (!strcasecmp(rr_str, "UNKNOWN"))
02225 redirect_reason = 0;
02226 else if (!strcasecmp(rr_str, "BUSY"))
02227 redirect_reason = 1;
02228 else if (!strcasecmp(rr_str, "NO_REPLY"))
02229 redirect_reason = 2;
02230 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
02231 redirect_reason = 15;
02232 else
02233 redirect_reason = PRI_REDIR_UNCONDITIONAL;
02234 } else
02235 redirect_reason = PRI_REDIR_UNCONDITIONAL;
02236 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
02237
02238 #ifdef SUPPORT_USERUSER
02239
02240 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
02241
02242 if (useruser)
02243 pri_sr_set_useruser(sr, useruser);
02244 #endif
02245
02246 if (pri_setup(p->pri->pri, p->call, sr)) {
02247 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
02248 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
02249 pri_rel(p->pri);
02250 ast_mutex_unlock(&p->lock);
02251 pri_sr_free(sr);
02252 return -1;
02253 }
02254 pri_sr_free(sr);
02255 ast_setstate(ast, AST_STATE_DIALING);
02256 pri_rel(p->pri);
02257 }
02258 #endif
02259 ast_mutex_unlock(&p->lock);
02260 return 0;
02261 }
02262
02263 static void destroy_dahdi_pvt(struct dahdi_pvt **pvt)
02264 {
02265 struct dahdi_pvt *p = *pvt;
02266
02267 if (p->prev)
02268 p->prev->next = p->next;
02269 if (p->next)
02270 p->next->prev = p->prev;
02271 if (p->use_smdi)
02272 ast_smdi_interface_unref(p->smdi_iface);
02273 ast_mutex_destroy(&p->lock);
02274 dahdi_close_sub(p, SUB_REAL);
02275 if (p->owner)
02276 p->owner->tech_pvt = NULL;
02277 free(p);
02278 *pvt = NULL;
02279 }
02280
02281 static int destroy_channel(struct dahdi_pvt *prev, struct dahdi_pvt *cur, int now)
02282 {
02283 int owned = 0;
02284 int i = 0;
02285
02286 if (!now) {
02287 if (cur->owner) {
02288 owned = 1;
02289 }
02290
02291 for (i = 0; i < 3; i++) {
02292 if (cur->subs[i].owner) {
02293 owned = 1;
02294 }
02295 }
02296 if (!owned) {
02297 if (prev) {
02298 prev->next = cur->next;
02299 if (prev->next)
02300 prev->next->prev = prev;
02301 else
02302 ifend = prev;
02303 } else {
02304 iflist = cur->next;
02305 if (iflist)
02306 iflist->prev = NULL;
02307 else
02308 ifend = NULL;
02309 }
02310 destroy_dahdi_pvt(&cur);
02311 }
02312 } else {
02313 if (prev) {
02314 prev->next = cur->next;
02315 if (prev->next)
02316 prev->next->prev = prev;
02317 else
02318 ifend = prev;
02319 } else {
02320 iflist = cur->next;
02321 if (iflist)
02322 iflist->prev = NULL;
02323 else
02324 ifend = NULL;
02325 }
02326 destroy_dahdi_pvt(&cur);
02327 }
02328 return 0;
02329 }
02330
02331 static void destroy_all_channels(void)
02332 {
02333 int x;
02334 struct dahdi_pvt *p, *pl;
02335
02336 while (num_restart_pending) {
02337 usleep(1);
02338 }
02339
02340 ast_mutex_lock(&iflock);
02341
02342 p = iflist;
02343 while (p) {
02344
02345 if (p->cidspill)
02346 ast_free(p->cidspill);
02347 pl = p;
02348 p = p->next;
02349 x = pl->channel;
02350
02351 if (pl)
02352 destroy_dahdi_pvt(&pl);
02353 if (option_verbose > 2)
02354 ast_verbose(VERBOSE_PREFIX_2 "Unregistered channel %d\n", x);
02355 }
02356 iflist = NULL;
02357 ifcount = 0;
02358 ast_mutex_unlock(&iflock);
02359 }
02360
02361 #ifdef HAVE_PRI
02362 static char *dahdi_send_keypad_facility_app = "DAHDISendKeypadFacility";
02363 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
02364
02365 static char *dahdi_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
02366 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
02367
02368 static char *dahdi_send_keypad_facility_descrip =
02369 " DAHDISendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
02370 " IE over the current channel.\n";
02371 static char *zap_send_keypad_facility_descrip =
02372 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
02373 " IE over the current channel.\n";
02374
02375 static int send_keypad_facility_exec(struct ast_channel *chan, void *data)
02376 {
02377
02378 struct dahdi_pvt *p;
02379 char *digits = (char *) data;
02380
02381 if (ast_strlen_zero(digits)) {
02382 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
02383 return -1;
02384 }
02385
02386 p = (struct dahdi_pvt *)chan->tech_pvt;
02387
02388 if (!p) {
02389 ast_log(LOG_DEBUG, "Unable to find technology private\n");
02390 return -1;
02391 }
02392
02393 ast_mutex_lock(&p->lock);
02394
02395 if (!p->pri || !p->call) {
02396 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
02397 ast_mutex_unlock(&p->lock);
02398 return -1;
02399 }
02400
02401 if (!pri_grab(p, p->pri)) {
02402 pri_keypad_facility(p->pri->pri, p->call, digits);
02403 pri_rel(p->pri);
02404 } else {
02405 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
02406 ast_mutex_unlock(&p->lock);
02407 return -1;
02408 }
02409
02410 ast_mutex_unlock(&p->lock);
02411
02412 return 0;
02413 }
02414
02415 static int dahdi_send_keypad_facility_exec(struct ast_channel *chan, void *data)
02416 {
02417 return send_keypad_facility_exec(chan, data);
02418 }
02419
02420 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
02421 {
02422 ast_log(LOG_WARNING, "Use of the command %s is deprecated, please use %s instead.\n", zap_send_keypad_facility_app, dahdi_send_keypad_facility_app);
02423 return send_keypad_facility_exec(chan, data);
02424 }
02425
02426 static int pri_is_up(struct dahdi_pri *pri)
02427 {
02428 int x;
02429 for (x = 0; x < NUM_DCHANS; x++) {
02430 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
02431 return 1;
02432 }
02433 return 0;
02434 }
02435
02436 static int pri_assign_bearer(struct dahdi_pvt *crv, struct dahdi_pri *pri, struct dahdi_pvt *bearer)
02437 {
02438 bearer->owner = &inuse;
02439 bearer->realcall = crv;
02440 crv->subs[SUB_REAL].dfd = bearer->subs[SUB_REAL].dfd;
02441 if (crv->subs[SUB_REAL].owner)
02442 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].dfd;
02443 crv->bearer = bearer;
02444 crv->call = bearer->call;
02445 crv->pri = pri;
02446 return 0;
02447 }
02448
02449 static char *pri_order(int level)
02450 {
02451 switch (level) {
02452 case 0:
02453 return "Primary";
02454 case 1:
02455 return "Secondary";
02456 case 2:
02457 return "Tertiary";
02458 case 3:
02459 return "Quaternary";
02460 default:
02461 return "<Unknown>";
02462 }
02463 }
02464
02465
02466 static int pri_active_dchan_fd(struct dahdi_pri *pri)
02467 {
02468 int x = -1;
02469
02470 for (x = 0; x < NUM_DCHANS; x++) {
02471 if ((pri->dchans[x] == pri->pri))
02472 break;
02473 }
02474
02475 return pri->fds[x];
02476 }
02477
02478 static int pri_find_dchan(struct dahdi_pri *pri)
02479 {
02480 int oldslot = -1;
02481 struct pri *old;
02482 int newslot = -1;
02483 int x;
02484 old = pri->pri;
02485 for (x = 0; x < NUM_DCHANS; x++) {
02486 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
02487 newslot = x;
02488 if (pri->dchans[x] == old) {
02489 oldslot = x;
02490 }
02491 }
02492 if (newslot < 0) {
02493 newslot = 0;
02494 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
02495 pri->dchannels[newslot]);
02496 }
02497 if (old && (oldslot != newslot))
02498 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
02499 pri->dchannels[oldslot], pri->dchannels[newslot]);
02500 pri->pri = pri->dchans[newslot];
02501 return 0;
02502 }
02503 #endif
02504
02505 static int dahdi_hangup(struct ast_channel *ast)
02506 {
02507 int res;
02508 int index,x, law;
02509
02510 struct dahdi_pvt *p = ast->tech_pvt;
02511 struct dahdi_pvt *tmp = NULL;
02512 struct dahdi_pvt *prev = NULL;
02513 struct dahdi_params par;
02514
02515 if (option_debug)
02516 ast_log(LOG_DEBUG, "dahdi_hangup(%s)\n", ast->name);
02517 if (!ast->tech_pvt) {
02518 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
02519 return 0;
02520 }
02521
02522 ast_mutex_lock(&p->lock);
02523
02524 index = dahdi_get_index(ast, p, 1);
02525
02526 if (p->sig == SIG_PRI) {
02527 x = 1;
02528 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02529 }
02530
02531 x = 0;
02532 dahdi_confmute(p, 0);
02533 restore_gains(p);
02534 if (p->origcid_num) {
02535 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
02536 free(p->origcid_num);
02537 p->origcid_num = NULL;
02538 }
02539 if (p->origcid_name) {
02540 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
02541 free(p->origcid_name);
02542 p->origcid_name = NULL;
02543 }
02544 if (p->dsp)
02545 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02546 if (p->exten)
02547 p->exten[0] = '\0';
02548
02549 if (option_debug)
02550 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
02551 p->channel, index, p->subs[SUB_REAL].dfd, p->subs[SUB_CALLWAIT].dfd, p->subs[SUB_THREEWAY].dfd);
02552 p->ignoredtmf = 0;
02553
02554 if (index > -1) {
02555
02556 p->subs[index].owner = NULL;
02557 p->subs[index].needanswer = 0;
02558 p->subs[index].needflash = 0;
02559 p->subs[index].needringing = 0;
02560 p->subs[index].needbusy = 0;
02561 p->subs[index].needcongestion = 0;
02562 p->subs[index].linear = 0;
02563 p->subs[index].needcallerid = 0;
02564 p->polarity = POLARITY_IDLE;
02565 dahdi_setlinear(p->subs[index].dfd, 0);
02566 if (index == SUB_REAL) {
02567 if ((p->subs[SUB_CALLWAIT].dfd > -1) && (p->subs[SUB_THREEWAY].dfd > -1)) {
02568 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
02569 if (p->subs[SUB_CALLWAIT].inthreeway) {
02570
02571 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
02572
02573 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02574 unalloc_sub(p, SUB_CALLWAIT);
02575 p->owner = NULL;
02576 } else {
02577
02578 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
02579 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02580 unalloc_sub(p, SUB_THREEWAY);
02581 if (p->subs[SUB_REAL].inthreeway) {
02582
02583
02584 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02585 p->owner = p->subs[SUB_REAL].owner;
02586 } else {
02587
02588 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02589 p->owner = NULL;
02590 }
02591 p->subs[SUB_REAL].inthreeway = 0;
02592 }
02593 } else if (p->subs[SUB_CALLWAIT].dfd > -1) {
02594
02595 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02596 unalloc_sub(p, SUB_CALLWAIT);
02597 p->owner = p->subs[SUB_REAL].owner;
02598 if (p->owner->_state != AST_STATE_UP)
02599 p->subs[SUB_REAL].needanswer = 1;
02600 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
02601 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
02602 } else if (p->subs[SUB_THREEWAY].dfd > -1) {
02603 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02604 unalloc_sub(p, SUB_THREEWAY);
02605 if (p->subs[SUB_REAL].inthreeway) {
02606
02607
02608 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02609 p->owner = p->subs[SUB_REAL].owner;
02610 } else {
02611
02612 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02613 p->owner = NULL;
02614 }
02615 p->subs[SUB_REAL].inthreeway = 0;
02616 }
02617 } else if (index == SUB_CALLWAIT) {
02618
02619 if (p->subs[SUB_CALLWAIT].inthreeway) {
02620
02621
02622 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
02623 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
02624 S_OR(p->mohsuggest, NULL),
02625 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
02626 }
02627 p->subs[SUB_THREEWAY].inthreeway = 0;
02628
02629 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
02630 unalloc_sub(p, SUB_THREEWAY);
02631 } else
02632 unalloc_sub(p, SUB_CALLWAIT);
02633 } else if (index == SUB_THREEWAY) {
02634 if (p->subs[SUB_CALLWAIT].inthreeway) {
02635
02636
02637 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
02638 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
02639 S_OR(p->mohsuggest, NULL),
02640 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
02641 }
02642 p->subs[SUB_CALLWAIT].inthreeway = 0;
02643 }
02644 p->subs[SUB_REAL].inthreeway = 0;
02645
02646
02647 unalloc_sub(p, SUB_THREEWAY);
02648 } else {
02649
02650 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
02651 }
02652 }
02653
02654 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
02655 p->owner = NULL;
02656 p->ringt = 0;
02657 p->distinctivering = 0;
02658 p->confirmanswer = 0;
02659 p->cidrings = 1;
02660 p->outgoing = 0;
02661 p->digital = 0;
02662 p->faxhandled = 0;
02663 p->pulsedial = 0;
02664 p->onhooktime = time(NULL);
02665 #ifdef HAVE_PRI
02666 p->proceeding = 0;
02667 p->progress = 0;
02668 p->alerting = 0;
02669 p->setup_ack = 0;
02670 #endif
02671 if (p->dsp) {
02672 ast_dsp_free(p->dsp);
02673 p->dsp = NULL;
02674 }
02675
02676 law = DAHDI_LAW_DEFAULT;
02677 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETLAW, &law);
02678 if (res < 0)
02679 ast_log(LOG_WARNING, "Unable to set law on channel %d to default: %s\n", p->channel, strerror(errno));
02680
02681 #ifdef HAVE_PRI
02682 if (p->pri) {
02683 #ifdef SUPPORT_USERUSER
02684 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
02685 #endif
02686
02687
02688 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
02689 if (!pri_grab(p, p->pri)) {
02690 if (p->alreadyhungup) {
02691 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
02692
02693 #ifdef SUPPORT_USERUSER
02694 pri_call_set_useruser(p->call, useruser);
02695 #endif
02696
02697 pri_hangup(p->pri->pri, p->call, -1);
02698 p->call = NULL;
02699 if (p->bearer)
02700 p->bearer->call = NULL;
02701 } else {
02702 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
02703 int icause = ast->hangupcause ? ast->hangupcause : -1;
02704 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
02705
02706 #ifdef SUPPORT_USERUSER
02707 pri_call_set_useruser(p->call, useruser);
02708 #endif
02709
02710 p->alreadyhungup = 1;
02711 if (p->bearer)
02712 p->bearer->alreadyhungup = 1;
02713 if (cause) {
02714 if (atoi(cause))
02715 icause = atoi(cause);
02716 }
02717 pri_hangup(p->pri->pri, p->call, icause);
02718 }
02719 if (res < 0)
02720 ast_log(LOG_WARNING, "pri_disconnect failed\n");
02721 pri_rel(p->pri);
02722 } else {
02723 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02724 res = -1;
02725 }
02726 } else {
02727 if (p->bearer)
02728 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
02729 p->call = NULL;
02730 res = 0;
02731 }
02732 }
02733 #endif
02734 if (p->sig && (p->sig != SIG_PRI))
02735 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
02736 if (res < 0) {
02737 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
02738 }
02739 switch (p->sig) {
02740 case SIG_FXOGS:
02741 case SIG_FXOLS:
02742 case SIG_FXOKS:
02743 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
02744 if (!res) {
02745 #if 0
02746 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
02747 #endif
02748
02749 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
02750 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
02751 else
02752 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
02753 }
02754 break;
02755 case SIG_FXSGS:
02756 case SIG_FXSLS:
02757 case SIG_FXSKS:
02758
02759
02760 if (ast->_state != AST_STATE_RESERVED) {
02761 time(&p->guardtime);
02762 p->guardtime += 2;
02763 }
02764 break;
02765 default:
02766 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
02767 }
02768 if (p->cidspill)
02769 free(p->cidspill);
02770 if (p->sig)
02771 dahdi_disable_ec(p);
02772 x = 0;
02773 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
02774 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
02775 p->didtdd = 0;
02776 p->cidspill = NULL;
02777 p->callwaitcas = 0;
02778 p->callwaiting = p->permcallwaiting;
02779 p->hidecallerid = p->permhidecallerid;
02780 p->dialing = 0;
02781 p->rdnis[0] = '\0';
02782 update_conf(p);
02783 reset_conf(p);
02784
02785 if (p->sig == SIG_PRI) {
02786 x = 0;
02787 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02788 }
02789 #ifdef HAVE_PRI
02790 if (p->bearer) {
02791 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
02792
02793
02794 update_conf(p->bearer);
02795 reset_conf(p->bearer);
02796 p->bearer->owner = NULL;
02797 p->bearer->realcall = NULL;
02798 p->bearer = NULL;
02799 p->subs[SUB_REAL].dfd = -1;
02800 p->pri = NULL;
02801 }
02802 #endif
02803 if (num_restart_pending == 0)
02804 restart_monitor();
02805 }
02806
02807 p->callwaitingrepeat = 0;
02808 p->cidcwexpire = 0;
02809 p->oprmode = 0;
02810 ast->tech_pvt = NULL;
02811 ast_mutex_unlock(&p->lock);
02812 ast_module_unref(ast_module_info->self);
02813 if (option_verbose > 2)
02814 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
02815
02816 ast_mutex_lock(&iflock);
02817
02818 if (p->restartpending) {
02819 num_restart_pending--;
02820 }
02821
02822 tmp = iflist;
02823 prev = NULL;
02824 if (p->destroy) {
02825 while (tmp) {
02826 if (tmp == p) {
02827 destroy_channel(prev, tmp, 0);
02828 break;
02829 } else {
02830 prev = tmp;
02831 tmp = tmp->next;
02832 }
02833 }
02834 }
02835 ast_mutex_unlock(&iflock);
02836 return 0;
02837 }
02838
02839 static int dahdi_answer(struct ast_channel *ast)
02840 {
02841 struct dahdi_pvt *p = ast->tech_pvt;
02842 int res = 0;
02843 int index;
02844 int oldstate = ast->_state;
02845 ast_setstate(ast, AST_STATE_UP);
02846 ast_mutex_lock(&p->lock);
02847 index = dahdi_get_index(ast, p, 0);
02848 if (index < 0)
02849 index = SUB_REAL;
02850
02851 if ((p->radio || (p->oprmode < 0))) {
02852 ast_mutex_unlock(&p->lock);
02853 return 0;
02854 }
02855 switch (p->sig) {
02856 case SIG_FXSLS:
02857 case SIG_FXSGS:
02858 case SIG_FXSKS:
02859 p->ringt = 0;
02860
02861 case SIG_EM:
02862 case SIG_EM_E1:
02863 case SIG_EMWINK:
02864 case SIG_FEATD:
02865 case SIG_FEATDMF:
02866 case SIG_FEATDMF_TA:
02867 case SIG_E911:
02868 case SIG_FGC_CAMA:
02869 case SIG_FGC_CAMAMF:
02870 case SIG_FEATB:
02871 case SIG_SF:
02872 case SIG_SFWINK:
02873 case SIG_SF_FEATD:
02874 case SIG_SF_FEATDMF:
02875 case SIG_SF_FEATB:
02876 case SIG_FXOLS:
02877 case SIG_FXOGS:
02878 case SIG_FXOKS:
02879
02880 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
02881 if (p->hanguponpolarityswitch) {
02882 gettimeofday(&p->polaritydelaytv, NULL);
02883 }
02884 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
02885 tone_zone_play_tone(p->subs[index].dfd, -1);
02886 p->dialing = 0;
02887 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
02888 if (oldstate == AST_STATE_RINGING) {
02889 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
02890 tone_zone_play_tone(p->subs[SUB_THREEWAY].dfd, -1);
02891 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02892 p->owner = p->subs[SUB_REAL].owner;
02893 }
02894 }
02895 if (p->sig & __DAHDI_SIG_FXS) {
02896 dahdi_enable_ec(p);
02897 dahdi_train_ec(p);
02898 }
02899 break;
02900 #ifdef HAVE_PRI
02901 case SIG_PRI:
02902
02903 if (!pri_grab(p, p->pri)) {
02904 p->proceeding = 1;
02905 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
02906 pri_rel(p->pri);
02907 } else {
02908 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02909 res = -1;
02910 }
02911 break;
02912 #endif
02913 case 0:
02914 ast_mutex_unlock(&p->lock);
02915 return 0;
02916 default:
02917 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
02918 res = -1;
02919 }
02920 ast_mutex_unlock(&p->lock);
02921 return res;
02922 }
02923
02924 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen)
02925 {
02926 char *cp;
02927 signed char *scp;
02928 int x;
02929 int index;
02930 struct dahdi_pvt *p = chan->tech_pvt, *pp;
02931 struct oprmode *oprmode;
02932
02933
02934
02935 if (!data || (datalen < 1)) {
02936 errno = EINVAL;
02937 return -1;
02938 }
02939
02940 switch (option) {
02941 case AST_OPTION_TXGAIN:
02942 scp = (signed char *) data;
02943 index = dahdi_get_index(chan, p, 0);
02944 if (index < 0) {
02945 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
02946 return -1;
02947 }
02948 if (option_debug)
02949 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
02950 return set_actual_txgain(p->subs[index].dfd, 0, p->txgain + (float) *scp, p->law);
02951 case AST_OPTION_RXGAIN:
02952 scp = (signed char *) data;
02953 index = dahdi_get_index(chan, p, 0);
02954 if (index < 0) {
02955 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
02956 return -1;
02957 }
02958 if (option_debug)
02959 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
02960 return set_actual_rxgain(p->subs[index].dfd, 0, p->rxgain + (float) *scp, p->law);
02961 case AST_OPTION_TONE_VERIFY:
02962 if (!p->dsp)
02963 break;
02964 cp = (char *) data;
02965 switch (*cp) {
02966 case 1:
02967 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
02968 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax);
02969 break;
02970 case 2:
02971 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
02972 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax);
02973 break;
02974 default:
02975 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
02976 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02977 break;
02978 }
02979 break;
02980 case AST_OPTION_TDD:
02981
02982 cp = (char *) data;
02983 p->mate = 0;
02984 if (!*cp) {
02985 if (option_debug)
02986 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
02987 if (p->tdd)
02988 tdd_free(p->tdd);
02989 p->tdd = 0;
02990 break;
02991 }
02992 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
02993 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
02994 dahdi_disable_ec(p);
02995
02996 if (!p->didtdd) {
02997 unsigned char mybuf[41000], *buf;
02998 int size, res, fd, len;
02999 struct pollfd fds[1];
03000
03001 buf = mybuf;
03002 memset(buf, 0x7f, sizeof(mybuf));
03003 ast_tdd_gen_ecdisa(buf + 16000, 16000);
03004 len = 40000;
03005 index = dahdi_get_index(chan, p, 0);
03006 if (index < 0) {
03007 ast_log(LOG_WARNING, "No index in TDD?\n");
03008 return -1;
03009 }
03010 fd = p->subs[index].dfd;
03011 while (len) {
03012 if (ast_check_hangup(chan))
03013 return -1;
03014 size = len;
03015 if (size > READ_SIZE)
03016 size = READ_SIZE;
03017 fds[0].fd = fd;
03018 fds[0].events = POLLPRI | POLLOUT;
03019 fds[0].revents = 0;
03020 res = poll(fds, 1, -1);
03021 if (!res) {
03022 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
03023 continue;
03024 }
03025
03026 if (fds[0].revents & POLLPRI)
03027 return -1;
03028 if (!(fds[0].revents & POLLOUT)) {
03029 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
03030 continue;
03031 }
03032 res = write(fd, buf, size);
03033 if (res != size) {
03034 if (res == -1) return -1;
03035 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
03036 break;
03037 }
03038 len -= size;
03039 buf += size;
03040 }
03041 p->didtdd = 1;
03042 }
03043 if (*cp == 2) {
03044 if (p->tdd)
03045 tdd_free(p->tdd);
03046 p->tdd = 0;
03047 p->mate = 1;
03048 break;
03049 }
03050 if (!p->tdd) {
03051 p->tdd = tdd_new();
03052 }
03053 break;
03054 case AST_OPTION_RELAXDTMF:
03055 if (!p->dsp)
03056 break;
03057 cp = (char *) data;
03058 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
03059 *cp ? "ON" : "OFF", (int) *cp, chan->name);
03060 p->dtmfrelax = 0;
03061 if (*cp) p->dtmfrelax = DSP_DIGITMODE_RELAXDTMF;
03062 ast_dsp_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
03063 break;
03064 case AST_OPTION_AUDIO_MODE:
03065 cp = (char *) data;
03066 if (!*cp) {
03067 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
03068 x = 0;
03069 dahdi_disable_ec(p);
03070 } else {
03071 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
03072 x = 1;
03073 }
03074 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x) == -1)
03075 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", p->channel, x, strerror(errno));
03076 break;
03077 case AST_OPTION_OPRMODE:
03078 oprmode = (struct oprmode *) data;
03079 pp = oprmode->peer->tech_pvt;
03080 p->oprmode = pp->oprmode = 0;
03081
03082 p->oprpeer = pp;
03083 pp->oprpeer = p;
03084
03085 if (oprmode->mode)
03086 {
03087 pp->oprmode = oprmode->mode;
03088 p->oprmode = -oprmode->mode;
03089 }
03090 ast_log(LOG_DEBUG, "Set Operator Services mode, value: %d on %s/%s\n",
03091 oprmode->mode, chan->name,oprmode->peer->name);;
03092 break;
03093 case AST_OPTION_ECHOCAN:
03094 cp = (char *) data;
03095 if (*cp) {
03096 ast_log(LOG_DEBUG, "Enabling echo cancelation on %s\n", chan->name);
03097 dahdi_enable_ec(p);
03098 } else {
03099 ast_log(LOG_DEBUG, "Disabling echo cancelation on %s\n", chan->name);
03100 dahdi_disable_ec(p);
03101 }
03102 break;
03103 }
03104 errno = 0;
03105
03106 return 0;
03107 }
03108
03109 static int dahdi_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
03110 {
03111 struct dahdi_pvt *p = chan->tech_pvt;
03112
03113 if (!strcasecmp(data, "rxgain")) {
03114 ast_mutex_lock(&p->lock);
03115 snprintf(buf, len, "%f", p->rxgain);
03116 ast_mutex_unlock(&p->lock);
03117 } else if (!strcasecmp(data, "txgain")) {
03118 ast_mutex_lock(&p->lock);
03119 snprintf(buf, len, "%f", p->txgain);
03120 ast_mutex_unlock(&p->lock);
03121 } else {
03122 ast_copy_string(buf, "", len);
03123 }
03124 return 0;
03125 }
03126
03127
03128 static void dahdi_unlink(struct dahdi_pvt *slave, struct dahdi_pvt *master, int needlock)
03129 {
03130
03131 int x;
03132 int hasslaves;
03133 if (!master)
03134 return;
03135 if (needlock) {
03136 ast_mutex_lock(&master->lock);
03137 if (slave) {
03138 while (ast_mutex_trylock(&slave->lock)) {
03139 DEADLOCK_AVOIDANCE(&master->lock);
03140 }
03141 }
03142 }
03143 hasslaves = 0;
03144 for (x = 0; x < MAX_SLAVES; x++) {
03145 if (master->slaves[x]) {
03146 if (!slave || (master->slaves[x] == slave)) {
03147
03148 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
03149 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
03150 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
03151 master->slaves[x]->master = NULL;
03152 master->slaves[x] = NULL;
03153 } else
03154 hasslaves = 1;
03155 }
03156 if (!hasslaves)
03157 master->inconference = 0;
03158 }
03159 if (!slave) {
03160 if (master->master) {
03161
03162 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
03163 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
03164 hasslaves = 0;
03165 for (x = 0; x < MAX_SLAVES; x++) {
03166 if (master->master->slaves[x] == master)
03167 master->master->slaves[x] = NULL;
03168 else if (master->master->slaves[x])
03169 hasslaves = 1;
03170 }
03171 if (!hasslaves)
03172 master->master->inconference = 0;
03173 }
03174 master->master = NULL;
03175 }
03176 update_conf(master);
03177 if (needlock) {
03178 if (slave)
03179 ast_mutex_unlock(&slave->lock);
03180 ast_mutex_unlock(&master->lock);
03181 }
03182 }
03183
03184 static void dahdi_link(struct dahdi_pvt *slave, struct dahdi_pvt *master) {
03185 int x;
03186 if (!slave || !master) {
03187 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
03188 return;
03189 }
03190 for (x = 0; x < MAX_SLAVES; x++) {
03191 if (!master->slaves[x]) {
03192 master->slaves[x] = slave;
03193 break;
03194 }
03195 }
03196 if (x >= MAX_SLAVES) {
03197 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
03198 master->slaves[MAX_SLAVES - 1] = slave;
03199 }
03200 if (slave->master)
03201 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
03202 slave->master = master;
03203
03204 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
03205 }
03206
03207 static void disable_dtmf_detect(struct dahdi_pvt *p)
03208 {
03209 #ifdef DAHDI_TONEDETECT
03210 int val;
03211 #endif
03212
03213 p->ignoredtmf = 1;
03214
03215 #ifdef DAHDI_TONEDETECT
03216 val = 0;
03217 ioctl(p->subs[SUB_REAL].dfd, DAHDI_TONEDETECT, &val);
03218 #endif
03219 if (!p->hardwaredtmf && p->dsp) {
03220 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
03221 ast_dsp_set_features(p->dsp, p->dsp_features);
03222 }
03223 }
03224
03225 static void enable_dtmf_detect(struct dahdi_pvt *p)
03226 {
03227 #ifdef DAHDI_TONEDETECT
03228 int val;
03229 #endif
03230
03231 if (p->channel == CHAN_PSEUDO)
03232 return;
03233
03234 p->ignoredtmf = 0;
03235
03236 #ifdef DAHDI_TONEDETECT
03237 val = DAHDI_TONEDETECT_ON | DAHDI_TONEDETECT_MUTE;
03238 ioctl(p->subs[SUB_REAL].dfd, DAHDI_TONEDETECT, &val);
03239 #endif
03240 if (!p->hardwaredtmf && p->dsp) {
03241 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
03242 ast_dsp_set_features(p->dsp, p->dsp_features);
03243 }
03244 }
03245
03246 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)
03247 {
03248 struct ast_channel *who;
03249 struct dahdi_pvt *p0, *p1, *op0, *op1;
03250 struct dahdi_pvt *master = NULL, *slave = NULL;
03251 struct ast_frame *f;
03252 int inconf = 0;
03253 int nothingok = 1;
03254 int ofd0, ofd1;
03255 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
03256 int os0 = -1, os1 = -1;
03257 int priority = 0;
03258 struct ast_channel *oc0, *oc1;
03259 enum ast_bridge_result res;
03260
03261 #ifdef PRI_2BCT
03262 int triedtopribridge = 0;
03263 q931_call *q931c0 = NULL, *q931c1 = NULL;
03264 #endif
03265
03266
03267
03268
03269
03270
03271 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
03272 return AST_BRIDGE_FAILED_NOWARN;
03273
03274 ast_mutex_lock(&c0->lock);
03275 while (ast_mutex_trylock(&c1->lock)) {
03276 DEADLOCK_AVOIDANCE(&c0->lock);
03277 }
03278
03279 p0 = c0->tech_pvt;
03280 p1 = c1->tech_pvt;
03281
03282 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
03283 ast_mutex_unlock(&c0->lock);
03284 ast_mutex_unlock(&c1->lock);
03285 return AST_BRIDGE_FAILED_NOWARN;
03286 }
03287
03288 oi0 = dahdi_get_index(c0, p0, 0);
03289 oi1 = dahdi_get_index(c1, p1, 0);
03290 if ((oi0 < 0) || (oi1 < 0)) {
03291 ast_mutex_unlock(&c0->lock);
03292 ast_mutex_unlock(&c1->lock);
03293 return AST_BRIDGE_FAILED;
03294 }
03295
03296 op0 = p0 = c0->tech_pvt;
03297 op1 = p1 = c1->tech_pvt;
03298 ofd0 = c0->fds[0];
03299 ofd1 = c1->fds[0];
03300 oc0 = p0->owner;
03301 oc1 = p1->owner;
03302
03303 if (ast_mutex_trylock(&p0->lock)) {
03304
03305 ast_mutex_unlock(&c0->lock);
03306 ast_mutex_unlock(&c1->lock);
03307 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03308 return AST_BRIDGE_RETRY;
03309 }
03310 if (ast_mutex_trylock(&p1->lock)) {
03311
03312 ast_mutex_unlock(&p0->lock);
03313 ast_mutex_unlock(&c0->lock);
03314 ast_mutex_unlock(&c1->lock);
03315 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03316 return AST_BRIDGE_RETRY;
03317 }
03318
03319 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03320 if (p0->owner && p1->owner) {
03321
03322 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
03323 master = p0;
03324 slave = p1;
03325 inconf = 1;
03326 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
03327 master = p1;
03328 slave = p0;
03329 inconf = 1;
03330 } else {
03331 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
03332 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
03333 p0->channel,
03334 oi0, (p0->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
03335 p0->subs[SUB_REAL].inthreeway, p0->channel,
03336 oi0, (p1->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
03337 p1->subs[SUB_REAL].inthreeway);
03338 }
03339 nothingok = 0;
03340 }
03341 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
03342 if (p1->subs[SUB_THREEWAY].inthreeway) {
03343 master = p1;
03344 slave = p0;
03345 nothingok = 0;
03346 }
03347 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
03348 if (p0->subs[SUB_THREEWAY].inthreeway) {
03349 master = p0;
03350 slave = p1;
03351 nothingok = 0;
03352 }
03353 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
03354
03355
03356 if (p1->subs[SUB_CALLWAIT].inthreeway) {
03357 master = p1;
03358 slave = p0;
03359 nothingok = 0;
03360 }
03361 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
03362
03363 if (p0->subs[SUB_CALLWAIT].inthreeway) {
03364 master = p0;
03365 slave = p1;
03366 nothingok = 0;
03367 }
03368 }
03369 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
03370 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
03371 if (master && slave) {
03372
03373
03374
03375 if ((oi1 == SUB_THREEWAY) &&
03376 p1->subs[SUB_THREEWAY].inthreeway &&
03377 p1->subs[SUB_REAL].owner &&
03378 p1->subs[SUB_REAL].inthreeway &&
03379 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03380 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
03381 tone_zone_play_tone(p0->subs[oi0].dfd, DAHDI_TONE_RINGTONE);
03382 os1 = p1->subs[SUB_REAL].owner->_state;
03383 } else {
03384 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
03385 tone_zone_play_tone(p0->subs[oi0].dfd, -1);
03386 }
03387 if ((oi0 == SUB_THREEWAY) &&
03388 p0->subs[SUB_THREEWAY].inthreeway &&
03389 p0->subs[SUB_REAL].owner &&
03390 p0->subs[SUB_REAL].inthreeway &&
03391 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03392 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
03393 tone_zone_play_tone(p1->subs[oi1].dfd, DAHDI_TONE_RINGTONE);
03394 os0 = p0->subs[SUB_REAL].owner->_state;
03395 } else {
03396 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
03397 tone_zone_play_tone(p1->subs[oi0].dfd, -1);
03398 }
03399 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03400 if (!p0->echocanbridged || !p1->echocanbridged) {
03401
03402 dahdi_disable_ec(p0);
03403 dahdi_disable_ec(p1);
03404 }
03405 }
03406 dahdi_link(slave, master);
03407 master->inconference = inconf;
03408 } else if (!nothingok)
03409 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
03410
03411 update_conf(p0);
03412 update_conf(p1);
03413 t0 = p0->subs[SUB_REAL].inthreeway;
03414 t1 = p1->subs[SUB_REAL].inthreeway;
03415
03416 ast_mutex_unlock(&p0->lock);
03417 ast_mutex_unlock(&p1->lock);
03418
03419 ast_mutex_unlock(&c0->lock);
03420 ast_mutex_unlock(&c1->lock);
03421
03422
03423 if ((!master || !slave) && !nothingok) {
03424 dahdi_enable_ec(p0);
03425 dahdi_enable_ec(p1);
03426 return AST_BRIDGE_FAILED;
03427 }
03428
03429 if (option_verbose > 2)
03430 ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
03431
03432 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03433 disable_dtmf_detect(op0);
03434
03435 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03436 disable_dtmf_detect(op1);
03437
03438 for (;;) {
03439 struct ast_channel *c0_priority[2] = {c0, c1};
03440 struct ast_channel *c1_priority[2] = {c1, c0};
03441
03442
03443
03444 ast_mutex_lock(&c0->lock);
03445 while (ast_mutex_trylock(&c1->lock)) {
03446 DEADLOCK_AVOIDANCE(&c0->lock);
03447 }
03448
03449 p0 = c0->tech_pvt;
03450 p1 = c1->tech_pvt;
03451
03452 if (op0 == p0)
03453 i0 = dahdi_get_index(c0, p0, 1);
03454 if (op1 == p1)
03455 i1 = dahdi_get_index(c1, p1, 1);
03456 ast_mutex_unlock(&c0->lock);
03457 ast_mutex_unlock(&c1->lock);
03458
03459 if (!timeoutms ||
03460 (op0 != p0) ||
03461 (op1 != p1) ||
03462 (ofd0 != c0->fds[0]) ||
03463 (ofd1 != c1->fds[0]) ||
03464 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
03465 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
03466 (oc0 != p0->owner) ||
03467 (oc1 != p1->owner) ||
03468 (t0 != p0->subs[SUB_REAL].inthreeway) ||
03469 (t1 != p1->subs[SUB_REAL].inthreeway) ||
03470 (oi0 != i0) ||
03471 (oi1 != i1)) {
03472 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
03473 op0->channel, oi0, op1->channel, oi1);
03474 res = AST_BRIDGE_RETRY;
03475 goto return_from_bridge;
03476 }
03477
03478 #ifdef PRI_2BCT
03479 q931c0 = p0->call;
03480 q931c1 = p1->call;
03481 if (p0->transfer && p1->transfer
03482 && q931c0 && q931c1
03483 && !triedtopribridge) {
03484 pri_channel_bridge(q931c0, q931c1);
03485 triedtopribridge = 1;
03486 }
03487 #endif
03488
03489 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
03490 if (!who) {
03491 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
03492 continue;
03493 }
03494 f = ast_read(who);
03495 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
03496 *fo = f;
03497 *rc = who;
03498 res = AST_BRIDGE_COMPLETE;
03499 goto return_from_bridge;
03500 }
03501 if (f->frametype == AST_FRAME_DTMF) {
03502 if ((who == c0) && p0->pulsedial) {
03503 ast_write(c1, f);
03504 } else if ((who == c1) && p1->pulsedial) {
03505 ast_write(c0, f);
03506 } else {
03507 *fo = f;
03508 *rc = who;
03509 res = AST_BRIDGE_COMPLETE;
03510 goto return_from_bridge;
03511 }
03512 }
03513 ast_frfree(f);
03514
03515
03516 priority = !priority;
03517 }
03518
03519 return_from_bridge:
03520 if (op0 == p0)
03521 dahdi_enable_ec(p0);
03522
03523 if (op1 == p1)
03524 dahdi_enable_ec(p1);
03525
03526 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03527 enable_dtmf_detect(op0);
03528
03529 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03530 enable_dtmf_detect(op1);
03531
03532 dahdi_unlink(slave, master, 1);
03533
03534 return res;
03535 }
03536
03537 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03538 {
03539 struct dahdi_pvt *p = newchan->tech_pvt;
03540 int x;
03541 ast_mutex_lock(&p->lock);
03542 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
03543 if (p->owner == oldchan) {
03544 p->owner = newchan;
03545 }
03546 for (x = 0; x < 3; x++)
03547 if (p->subs[x].owner == oldchan) {
03548 if (!x)
03549 dahdi_unlink(NULL, p, 0);
03550 p->subs[x].owner = newchan;
03551 }
03552 if (newchan->_state == AST_STATE_RINGING)
03553 dahdi_indicate(newchan, AST_CONTROL_RINGING, NULL, 0);
03554 update_conf(p);
03555 ast_mutex_unlock(&p->lock);
03556 return 0;
03557 }
03558
03559 static int dahdi_ring_phone(struct dahdi_pvt *p)
03560 {
03561 int x;
03562 int res;
03563
03564 x = 0;
03565 x = DAHDI_ONHOOK;
03566 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
03567 do {
03568 x = DAHDI_RING;
03569 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
03570 if (res) {
03571 switch (errno) {
03572 case EBUSY:
03573 case EINTR:
03574
03575 usleep(10000);
03576 continue;
03577 case EINPROGRESS:
03578 res = 0;
03579 break;
03580 default:
03581 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
03582 res = 0;
03583 }
03584 }
03585 } while (res);
03586 return res;
03587 }
03588
03589 static void *ss_thread(void *data);
03590
03591 static struct ast_channel *dahdi_new(struct dahdi_pvt *, int, int, int, int, int);
03592
03593 static int attempt_transfer(struct dahdi_pvt *p)
03594 {
03595
03596
03597
03598 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
03599
03600
03601 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
03602 ast_queue_control(p->subs[SUB_THREEWAY].owner, AST_CONTROL_UNHOLD);
03603 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
03604 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
03605 }
03606 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RING) {
03607 tone_zone_play_tone(p->subs[SUB_THREEWAY].dfd, DAHDI_TONE_RINGTONE);
03608 }
03609 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
03610 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03611 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
03612 return -1;
03613 }
03614
03615 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03616 unalloc_sub(p, SUB_THREEWAY);
03617 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
03618 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
03619 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
03620 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
03621 }
03622 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RING) {
03623 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
03624 }
03625 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
03626 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03627 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
03628 return -1;
03629 }
03630
03631 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03632 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
03633 unalloc_sub(p, SUB_THREEWAY);
03634
03635 return 1;
03636 } else {
03637 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
03638 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
03639 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03640 return -1;
03641 }
03642 return 0;
03643 }
03644
03645 static int check_for_conference(struct dahdi_pvt *p)
03646 {
03647 struct dahdi_confinfo ci;
03648
03649 if (p->master || (p->confno > -1))
03650 return 0;
03651 memset(&ci, 0, sizeof(ci));
03652 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &ci)) {
03653 ast_log(LOG_WARNING, "Failed to get conference info on channel %d: %s\n", p->channel, strerror(errno));
03654 return 0;
03655 }
03656
03657
03658
03659 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
03660 if (option_verbose > 2)
03661 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
03662 return 1;
03663 }
03664 return 0;
03665 }
03666
03667 static int get_alarms(struct dahdi_pvt *p)
03668 {
03669 int res;
03670 struct dahdi_spaninfo zi;
03671 #if !defined(HAVE_ZAPTEL) || defined(HAVE_ZAPTEL_CHANALARMS)
03672
03673
03674
03675
03676
03677 struct dahdi_params params;
03678 #endif
03679
03680 memset(&zi, 0, sizeof(zi));
03681 zi.spanno = p->span;
03682
03683
03684 if((res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SPANSTAT, &zi)) < 0) {
03685 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d: %s\n", p->channel, strerror(errno));
03686 return 0;
03687 }
03688 if (zi.alarms != DAHDI_ALARM_NONE)
03689 return zi.alarms;
03690 #if !defined(HAVE_ZAPTEL) || defined(HAVE_ZAPTEL_CHANALARMS)
03691
03692 if ((res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, ¶ms)) >= 0)
03693 return params.chan_alarms;
03694
03695 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
03696 #endif
03697 return DAHDI_ALARM_NONE;
03698 }
03699
03700 static void dahdi_handle_dtmfup(struct ast_channel *ast, int index, struct ast_frame **dest)
03701 {
03702 struct dahdi_pvt *p = ast->tech_pvt;
03703 struct ast_frame *f = *dest;
03704
03705 if (option_debug)
03706 ast_log(LOG_DEBUG, "DTMF digit: %c on %s\n", f->subclass, ast->name);
03707
03708 if (p->confirmanswer) {
03709 if (option_debug)
03710 ast_log(LOG_DEBUG, "Confirm answer on %s!\n", ast->name);
03711
03712
03713 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03714 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03715 *dest = &p->subs[index].f;
03716
03717 p->confirmanswer = 0;
03718 } else if (p->callwaitcas) {
03719 if ((f->subclass == 'A') || (f->subclass == 'D')) {
03720 if (option_debug)
03721 ast_log(LOG_DEBUG, "Got some DTMF, but it's for the CAS\n");
03722 if (p->cidspill)
03723 free(p->cidspill);
03724 send_cwcidspill(p);
03725 }
03726 if ((f->subclass != 'm') && (f->subclass != 'u'))
03727 p->callwaitcas = 0;
03728 p->subs[index].f.frametype = AST_FRAME_NULL;
03729 p->subs[index].f.subclass = 0;
03730 *dest = &p->subs[index].f;
03731 } else if (f->subclass == 'f') {
03732
03733 if ((p->callprogress & 0x6) && !p->faxhandled) {
03734 p->faxhandled++;
03735 if (strcmp(ast->exten, "fax")) {
03736 const char *target_context = S_OR(ast->macrocontext, ast->context);
03737
03738 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
03739 if (option_verbose > 2)
03740 ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
03741
03742 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
03743 if (ast_async_goto(ast, target_context, "fax", 1))
03744 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
03745 } else
03746 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
03747 } else if (option_debug)
03748 ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
03749 } else if (option_debug)
03750 ast_log(LOG_DEBUG, "Fax already handled\n");
03751 dahdi_confmute(p, 0);
03752 p->subs[index].f.frametype = AST_FRAME_NULL;
03753 p->subs[index].f.subclass = 0;
03754 *dest = &p->subs[index].f;
03755 } else if (f->subclass == 'm') {
03756
03757 dahdi_confmute(p, 1);
03758 p->subs[index].f.frametype = AST_FRAME_NULL;
03759 p->subs[index].f.subclass = 0;
03760 *dest = &p->subs[index].f;
03761 } else if (f->subclass == 'u') {
03762
03763 dahdi_confmute(p, 0);
03764 p->subs[index].f.frametype = AST_FRAME_NULL;
03765 p->subs[index].f.subclass = 0;
03766 *dest = &p->subs[index].f;
03767 } else
03768 dahdi_confmute(p, 0);
03769 }
03770
03771 static void handle_alarms(struct dahdi_pvt *p, int alarms)
03772 {
03773 const char *alarm_str = alarm2str(alarms);
03774
03775
03776
03777 if (!strcasecmp(alarm_str, "No Alarm") || !strcasecmp(alarm_str, "Unknown Alarm")) {
03778 p->unknown_alarm = 1;
03779 return;
03780 } else {
03781 p->unknown_alarm = 0;
03782 }
03783
03784 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm_str);
03785 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
03786 "Alarm: %s\r\n"
03787 "Channel: %d\r\n",
03788 alarm_str, p->channel);
03789 }
03790
03791 static struct ast_frame *dahdi_handle_event(struct ast_channel *ast)
03792 {
03793 int res, x;
03794 int index, mysig;
03795 char *c;
03796 struct dahdi_pvt *p = ast->tech_pvt;
03797 pthread_t threadid;
03798 pthread_attr_t attr;
03799 struct ast_channel *chan;
03800 struct ast_frame *f;
03801
03802 index = dahdi_get_index(ast, p, 0);
03803 mysig = p->sig;
03804 if (p->outsigmod > -1)
03805 mysig = p->outsigmod;
03806 p->subs[index].f.frametype = AST_FRAME_NULL;
03807 p->subs[index].f.subclass = 0;
03808 p->subs[index].f.datalen = 0;
03809 p->subs[index].f.samples = 0;
03810 p->subs[index].f.mallocd = 0;
03811 p->subs[index].f.offset = 0;
03812 p->subs[index].f.src = "dahdi_handle_event";
03813 p->subs[index].f.data = NULL;
03814 f = &p->subs[index].f;
03815
03816 if (index < 0)
03817 return &p->subs[index].f;
03818 if (p->fake_event) {
03819 res = p->fake_event;
03820 p->fake_event = 0;
03821 } else
03822 res = dahdi_get_event(p->subs[index].dfd);
03823
03824 if (option_debug)
03825 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
03826
03827 if (res & (DAHDI_EVENT_PULSEDIGIT | DAHDI_EVENT_DTMFUP)) {
03828 p->pulsedial = (res & DAHDI_EVENT_PULSEDIGIT) ? 1 : 0;
03829
03830 ast_log(LOG_DEBUG, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse ": "", res & 0xff);
03831 #ifdef HAVE_PRI
03832 if (!p->proceeding && p->sig == SIG_PRI && p->pri && p->pri->overlapdial) {
03833
03834 } else {
03835 #endif
03836 p->subs[index].f.frametype = AST_FRAME_DTMF_END;
03837 p->subs[index].f.subclass = res & 0xff;
03838 #ifdef HAVE_PRI
03839 }
03840 #endif
03841 dahdi_handle_dtmfup(ast, index, &f);
03842 return f;
03843 }
03844
03845 if (res & DAHDI_EVENT_DTMFDOWN) {
03846 if (option_debug)
03847 ast_log(LOG_DEBUG, "DTMF Down '%c'\n", res & 0xff);
03848
03849 dahdi_confmute(p, 1);
03850 p->subs[index].f.frametype = AST_FRAME_DTMF_BEGIN;
03851 p->subs[index].f.subclass = res & 0xff;
03852 return &p->subs[index].f;
03853 }
03854
03855 switch (res) {
03856 #ifdef DAHDI_EVENT_EC_DISABLED
03857 case DAHDI_EVENT_EC_DISABLED:
03858 if (option_verbose > 2)
03859 ast_verbose(VERBOSE_PREFIX_3 "Channel %d echo canceler disabled due to CED detection\n", p->channel);
03860 p->echocanon = 0;
03861 break;
03862 #endif
03863 case DAHDI_EVENT_BITSCHANGED:
03864 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
03865 case DAHDI_EVENT_PULSE_START:
03866
03867 if (!ast->pbx)
03868 tone_zone_play_tone(p->subs[index].dfd, -1);
03869 break;
03870 case DAHDI_EVENT_DIALCOMPLETE:
03871 if (p->inalarm) break;
03872 if ((p->radio || (p->oprmode < 0))) break;
03873 if (ioctl(p->subs[index].dfd,DAHDI_DIALING,&x) == -1) {
03874 ast_log(LOG_DEBUG, "DAHDI_DIALING ioctl failed on %s: %s\n",ast->name, strerror(errno));
03875 return NULL;
03876 }
03877 if (!x) {
03878 dahdi_enable_ec(p);
03879 if (p->echobreak) {
03880 dahdi_train_ec(p);
03881 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
03882 p->dop.op = DAHDI_DIAL_OP_REPLACE;
03883 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
03884 p->echobreak = 0;
03885 } else {
03886 p->dialing = 0;
03887 if ((mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF)) {
03888
03889 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
03890 ast_setstate(ast, AST_STATE_UP);
03891 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03892 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03893 break;
03894 } else {
03895
03896 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
03897 }
03898 }
03899 if (ast->_state == AST_STATE_DIALING) {
03900 if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
03901 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
03902 } else if (p->confirmanswer || (!p->dialednone && ((mysig == SIG_EM) || (mysig == SIG_EM_E1) || (mysig == SIG_EMWINK) || (mysig == SIG_FEATD) || (mysig == SIG_FEATDMF_TA) || (mysig == SIG_FEATDMF) || (mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF) || (mysig == SIG_FEATB) || (mysig == SIG_SF) || (mysig == SIG_SFWINK) || (mysig == SIG_SF_FEATD) || (mysig == SIG_SF_FEATDMF) || (mysig == SIG_SF_FEATB)))) {
03903 ast_setstate(ast, AST_STATE_RINGING);
03904 } else if (!p->answeronpolarityswitch) {
03905 ast_setstate(ast, AST_STATE_UP);
03906 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03907 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03908
03909 p->polarity = POLARITY_REV;
03910 } else {
03911
03912 p->polarity = POLARITY_IDLE;
03913 }
03914 }
03915 }
03916 }
03917 break;
03918 case DAHDI_EVENT_ALARM:
03919 #ifdef HAVE_PRI
03920 if (!p->pri || !p->pri->pri || (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0)) {
03921
03922 if (p->call) {
03923 if (p->pri && p->pri->pri) {
03924 if (!pri_grab(p, p->pri)) {
03925 pri_hangup(p->pri->pri, p->call, -1);
03926 pri_destroycall(p->pri->pri, p->call);
03927 p->call = NULL;
03928 pri_rel(p->pri);
03929 } else
03930 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
03931 } else
03932 ast_log(LOG_WARNING, "The PRI Call has not been destroyed\n");
03933 }
03934 if (p->owner)
03935 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
03936 }
03937 if (p->bearer)
03938 p->bearer->inalarm = 1;
03939 else
03940 #endif
03941 p->inalarm = 1;
03942 res = get_alarms(p);
03943 handle_alarms(p, res);
03944 #ifdef HAVE_LIBPRI
03945 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
03946
03947 } else {
03948 break;
03949 }
03950 #endif
03951 case DAHDI_EVENT_ONHOOK:
03952 if (p->radio) {
03953 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03954 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
03955 break;
03956 }
03957 if (p->oprmode < 0)
03958 {
03959 if (p->oprmode != -1) break;
03960 if ((p->sig == SIG_FXOLS) || (p->sig == SIG_FXOKS) || (p->sig == SIG_FXOGS))
03961 {
03962
03963 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
03964 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RING);
03965 save_conference(p->oprpeer);
03966 tone_zone_play_tone(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
03967 }
03968 break;
03969 }
03970 switch (p->sig) {
03971 case SIG_FXOLS:
03972 case SIG_FXOGS:
03973 case SIG_FXOKS:
03974 p->onhooktime = time(NULL);
03975 p->msgstate = -1;
03976
03977 if (index == SUB_REAL) {
03978
03979 if (p->subs[SUB_CALLWAIT].owner) {
03980
03981 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
03982 if (option_verbose > 2)
03983 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
03984 unalloc_sub(p, SUB_CALLWAIT);
03985 #if 0
03986 p->subs[index].needanswer = 0;
03987 p->subs[index].needringing = 0;
03988 #endif
03989 p->callwaitingrepeat = 0;
03990 p->cidcwexpire = 0;
03991 p->owner = NULL;
03992
03993 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
03994 p->dialing = 1;
03995 dahdi_ring_phone(p);
03996 } else if (p->subs[SUB_THREEWAY].owner) {
03997 unsigned int mssinceflash;
03998
03999
04000 while (p->subs[SUB_THREEWAY].owner && ast_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) {
04001
04002 ast_mutex_unlock(&p->lock);
04003 DEADLOCK_AVOIDANCE(&ast->lock);
04004
04005
04006
04007 ast_mutex_lock(&p->lock);
04008 if (p->owner != ast) {
04009 ast_log(LOG_WARNING, "This isn't good...\n");
04010 return NULL;
04011 }
04012 }
04013 if (!p->subs[SUB_THREEWAY].owner) {
04014 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
04015 return NULL;
04016 }
04017 mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
04018 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
04019 if (mssinceflash < MIN_MS_SINCE_FLASH) {
04020
04021
04022 if (p->subs[SUB_THREEWAY].owner)
04023 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
04024 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04025 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
04026 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
04027 } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
04028 if (p->transfer) {
04029
04030 p->subs[SUB_REAL].inthreeway = 0;
04031 p->subs[SUB_THREEWAY].inthreeway = 0;
04032
04033 if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
04034 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
04035
04036 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04037 p->owner = NULL;
04038
04039 dahdi_ring_phone(p);
04040 } else {
04041 if ((res = attempt_transfer(p)) < 0) {
04042 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04043 if (p->subs[SUB_THREEWAY].owner)
04044 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
04045 } else if (res) {
04046
04047 if (p->subs[SUB_THREEWAY].owner)
04048 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
04049 break;
04050 }
04051 }
04052 } else {
04053 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04054 if (p->subs[SUB_THREEWAY].owner)
04055 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
04056 }
04057 } else {
04058 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
04059
04060 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04061 p->owner = NULL;
04062
04063 dahdi_ring_phone(p);
04064 }
04065 }
04066 } else {
04067 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
04068 }
04069
04070 default:
04071 dahdi_disable_ec(p);
04072 return NULL;
04073 }
04074 break;
04075 case DAHDI_EVENT_RINGOFFHOOK:
04076 if (p->inalarm) break;
04077 if (p->oprmode < 0)
04078 {
04079 if ((p->sig == SIG_FXOLS) || (p->sig == SIG_FXOKS) || (p->sig == SIG_FXOGS))
04080 {
04081
04082 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
04083 tone_zone_play_tone(p->oprpeer->subs[SUB_REAL].dfd, -1);
04084 restore_conference(p->oprpeer);
04085 }
04086 break;
04087 }
04088 if (p->radio)
04089 {
04090 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04091 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
04092 break;
04093 }
04094
04095
04096 if (((mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF)) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
04097 c = strchr(p->dialdest, '/');
04098 if (c)
04099 c++;
04100 else
04101 c = p->dialdest;
04102 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
04103 else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
04104 if (strlen(p->dop.dialstr) > 4) {
04105 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
04106 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
04107 p->echorest[sizeof(p->echorest) - 1] = '\0';
04108 p->echobreak = 1;
04109 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
04110 } else
04111 p->echobreak = 0;
04112 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
04113 int saveerr = errno;
04114
04115 x = DAHDI_ONHOOK;
04116 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
04117 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
04118 return NULL;
04119 }
04120 p->dialing = 1;
04121 return &p->subs[index].f;
04122 }
04123 switch (p->sig) {
04124 case SIG_FXOLS:
04125 case SIG_FXOGS:
04126 case SIG_FXOKS:
04127 switch (ast->_state) {
04128 case AST_STATE_RINGING:
04129 dahdi_enable_ec(p);
04130 dahdi_train_ec(p);
04131 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04132 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04133
04134 dahdi_set_hook(p->subs[index].dfd, DAHDI_OFFHOOK);
04135 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
04136 if (p->cidspill) {
04137
04138 free(p->cidspill);
04139 p->cidspill = NULL;
04140 }
04141 p->dialing = 0;
04142 p->callwaitcas = 0;
04143 if (p->confirmanswer) {
04144
04145 p->subs[index].f.frametype = AST_FRAME_NULL;
04146 p->subs[index].f.subclass = 0;
04147 } else if (!ast_strlen_zero(p->dop.dialstr)) {
04148
04149 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
04150 if (res < 0) {
04151 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
04152 p->dop.dialstr[0] = '\0';
04153 return NULL;
04154 } else {
04155 ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
04156 p->subs[index].f.frametype = AST_FRAME_NULL;
04157 p->subs[index].f.subclass = 0;
04158 p->dialing = 1;
04159 }
04160 p->dop.dialstr[0] = '\0';
04161 ast_setstate(ast, AST_STATE_DIALING);
04162 } else
04163 ast_setstate(ast, AST_STATE_UP);
04164 return &p->subs[index].f;
04165 case AST_STATE_DOWN:
04166 ast_setstate(ast, AST_STATE_RING);
04167 ast->rings = 1;
04168 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04169 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
04170 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
04171 return &p->subs[index].f;
04172 case AST_STATE_UP:
04173
04174 dahdi_set_hook(p->subs[index].dfd, DAHDI_OFFHOOK);
04175
04176 if (ast_bridged_channel(p->owner))
04177 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
04178 p->subs[index].needunhold = 1;
04179 break;
04180 case AST_STATE_RESERVED:
04181
04182 if (has_voicemail(p))
04183 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_STUTTER);
04184 else
04185 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_DIALTONE);
04186 break;
04187 default:
04188 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
04189 }
04190 break;
04191 case SIG_FXSLS:
04192 case SIG_FXSGS:
04193 case SIG_FXSKS:
04194 if (ast->_state == AST_STATE_RING) {
04195 p->ringt = p->ringt_base;
04196 }
04197
04198
04199 case SIG_EM:
04200 case SIG_EM_E1:
04201 case SIG_EMWINK:
04202 case SIG_FEATD:
04203 case SIG_FEATDMF:
04204 case SIG_FEATDMF_TA:
04205 case SIG_E911:
04206 case SIG_FGC_CAMA:
04207 case SIG_FGC_CAMAMF:
04208 case SIG_FEATB:
04209 case SIG_SF:
04210 case SIG_SFWINK:
04211 case SIG_SF_FEATD:
04212 case SIG_SF_FEATDMF:
04213 case SIG_SF_FEATB:
04214 if (ast->_state == AST_STATE_PRERING)
04215 ast_setstate(ast, AST_STATE_RING);
04216 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
04217 if (option_debug)
04218 ast_log(LOG_DEBUG, "Ring detected\n");
04219 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04220 p->subs[index].f.subclass = AST_CONTROL_RING;
04221 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
04222 if (option_debug)
04223 ast_log(LOG_DEBUG, "Line answered\n");
04224 if (p->confirmanswer) {
04225 p->subs[index].f.frametype = AST_FRAME_NULL;
04226 p->subs[index].f.subclass = 0;
04227 } else {
04228 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04229 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04230 ast_setstate(ast, AST_STATE_UP);
04231 }
04232 } else if (ast->_state != AST_STATE_RING)
04233 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
04234 break;
04235 default:
04236 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
04237 }
04238 break;
04239 #ifdef DAHDI_EVENT_RINGBEGIN
04240 case DAHDI_EVENT_RINGBEGIN:
04241 switch (p->sig) {
04242 case SIG_FXSLS:
04243 case SIG_FXSGS:
04244 case SIG_FXSKS:
04245 if (ast->_state == AST_STATE_RING) {
04246 p->ringt = p->ringt_base;
04247 }
04248 break;
04249 }
04250 break;
04251 #endif
04252 case DAHDI_EVENT_RINGEROFF:
04253 if (p->inalarm) break;
04254 if ((p->radio || (p->oprmode < 0))) break;
04255 ast->rings++;
04256 if ((ast->rings > p->cidrings) && (p->cidspill)) {
04257 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
04258 free(p->cidspill);
04259 p->cidspill = NULL;
04260 p->callwaitcas = 0;
04261 }
04262 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04263 p->subs[index].f.subclass = AST_CONTROL_RINGING;
04264 break;
04265 case DAHDI_EVENT_RINGERON:
04266 break;
04267 case DAHDI_EVENT_NOALARM:
04268 p->inalarm = 0;
04269 #ifdef HAVE_PRI
04270
04271 if (p->bearer)
04272 p->bearer->inalarm = 0;
04273 #endif
04274 if (!p->unknown_alarm) {
04275 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
04276 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
04277 "Channel: %d\r\n", p->channel);
04278 } else {
04279 p->unknown_alarm = 0;
04280 }
04281 break;
04282 case DAHDI_EVENT_WINKFLASH:
04283 if (p->inalarm) break;
04284 if (p->radio) break;
04285 if (p->oprmode < 0) break;
04286 if (p->oprmode > 1)
04287 {
04288 struct dahdi_params par;
04289
04290 if (ioctl(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par) != -1)
04291 {
04292 if (!par.rxisoffhook)
04293 {
04294
04295 dahdi_set_hook(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
04296 dahdi_set_hook(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_RING);
04297 save_conference(p);
04298 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
04299 }
04300 }
04301 break;
04302 }
04303
04304 gettimeofday(&p->flashtime, NULL);
04305 switch (mysig) {
04306 case SIG_FXOLS:
04307 case SIG_FXOGS:
04308 case SIG_FXOKS:
04309 ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
04310 index, p->subs[SUB_REAL].dfd, p->subs[SUB_CALLWAIT].dfd, p->subs[SUB_THREEWAY].dfd);
04311 p->callwaitcas = 0;
04312
04313 if (index != SUB_REAL) {
04314 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
04315 goto winkflashdone;
04316 }
04317
04318 if (p->subs[SUB_CALLWAIT].owner) {
04319
04320 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
04321 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
04322 p->owner = p->subs[SUB_REAL].owner;
04323 ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
04324 if (p->owner->_state == AST_STATE_RINGING) {
04325 ast_setstate(p->owner, AST_STATE_UP);
04326 p->subs[SUB_REAL].needanswer = 1;
04327 }
04328 p->callwaitingrepeat = 0;
04329 p->cidcwexpire = 0;
04330
04331 if (!p->subs[SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
04332 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
04333 S_OR(p->mohsuggest, NULL),
04334 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04335 }
04336 p->subs[SUB_CALLWAIT].needhold = 1;
04337 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
04338 ast_queue_control_data(p->subs[SUB_REAL].owner, AST_CONTROL_HOLD,
04339 S_OR(p->mohsuggest, NULL),
04340 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04341 }
04342 p->subs[SUB_REAL].needunhold = 1;
04343 } else if (!p->subs[SUB_THREEWAY].owner) {
04344 char cid_num[256];
04345 char cid_name[256];
04346
04347 if (!p->threewaycalling) {
04348
04349 p->subs[SUB_REAL].needflash = 1;
04350 goto winkflashdone;
04351 } else if (!check_for_conference(p)) {
04352 if (p->dahditrcallerid && p->owner) {
04353 if (p->owner->cid.cid_num)
04354 ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
04355 if (p->owner->cid.cid_name)
04356 ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
04357 }
04358
04359
04360 if (!((ast->pbx) ||
04361 (ast->_state == AST_STATE_UP) ||
04362 (ast->_state == AST_STATE_RING))) {
04363 ast_log(LOG_DEBUG, "Flash when call not up or ringing\n");
04364 goto winkflashdone;
04365 }
04366 if (alloc_sub(p, SUB_THREEWAY)) {
04367 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
04368 goto winkflashdone;
04369 }
04370
04371 chan = dahdi_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
04372 if (p->dahditrcallerid) {
04373 if (!p->origcid_num)
04374 p->origcid_num = ast_strdup(p->cid_num);
04375 if (!p->origcid_name)
04376 p->origcid_name = ast_strdup(p->cid_name);
04377 ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
04378 ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
04379 }
04380
04381 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04382
04383 dahdi_disable_ec(p);
04384 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_DIALRECALL);
04385 if (res)
04386 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
04387 p->owner = chan;
04388 pthread_attr_init(&attr);
04389 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
04390 if (!chan) {
04391 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
04392 } else if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
04393 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
04394 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
04395 dahdi_enable_ec(p);
04396 ast_hangup(chan);
04397 } else {
04398 struct ast_channel *other = ast_bridged_channel(p->subs[SUB_THREEWAY].owner);
04399 int way3bridge = 0, cdr3way = 0;
04400
04401 if (!other) {
04402 other = ast_bridged_channel(p->subs[SUB_REAL].owner);
04403 } else
04404 way3bridge = 1;
04405
04406 if (p->subs[SUB_THREEWAY].owner->cdr)
04407 cdr3way = 1;
04408
04409 if (option_verbose > 2)
04410 ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);
04411
04412 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
04413 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
04414 S_OR(p->mohsuggest, NULL),
04415 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04416 }
04417 p->subs[SUB_THREEWAY].needhold = 1;
04418 }
04419 pthread_attr_destroy(&attr);
04420 }
04421 } else {
04422
04423 if (p->subs[SUB_THREEWAY].inthreeway) {
04424
04425 if (option_debug)
04426 ast_log(LOG_DEBUG, "Got flash with three way call up, dropping last call on %d\n", p->channel);
04427
04428 if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
04429
04430 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04431 p->owner = p->subs[SUB_REAL].owner;
04432 }
04433
04434 if (option_verbose > 2)
04435 ast_verbose(VERBOSE_PREFIX_3 "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
04436 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04437 p->subs[SUB_REAL].inthreeway = 0;
04438 p->subs[SUB_THREEWAY].inthreeway = 0;
04439 } else {
04440
04441 if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
04442 (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
04443 int otherindex = SUB_THREEWAY;
04444 struct ast_channel *other = ast_bridged_channel(p->subs[SUB_THREEWAY].owner);
04445 int way3bridge = 0, cdr3way = 0;
04446
04447 if (!other) {
04448 other = ast_bridged_channel(p->subs[SUB_REAL].owner);
04449 } else
04450 way3bridge = 1;
04451
04452 if (p->subs[SUB_THREEWAY].owner->cdr)
04453 cdr3way = 1;
04454
04455 if (option_verbose > 2)
04456 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);
04457
04458 p->subs[SUB_THREEWAY].inthreeway = 1;
04459 p->subs[SUB_REAL].inthreeway = 1;
04460 if (ast->_state == AST_STATE_UP) {
04461 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04462 otherindex = SUB_REAL;
04463 }
04464 if (p->subs[otherindex].owner && ast_bridged_channel(p->subs[otherindex].owner))
04465 ast_queue_control(p->subs[otherindex].owner, AST_CONTROL_UNHOLD);
04466 p->subs[otherindex].needunhold = 1;
04467 p->owner = p->subs[SUB_REAL].owner;
04468 if (ast->_state == AST_STATE_RINGING) {
04469 ast_log(LOG_DEBUG, "Enabling ringtone on real and threeway\n");
04470 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
04471 res = tone_zone_play_tone(p->subs[SUB_THREEWAY].dfd, DAHDI_TONE_RINGTONE);
04472 }
04473 } else {
04474 if (option_verbose > 2)
04475 ast_verbose(VERBOSE_PREFIX_3 "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name);
04476 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04477 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04478 p->owner = p->subs[SUB_REAL].owner;
04479 if (p->subs[SUB_REAL].owner && ast_bridged_channel(p->subs[SUB_REAL].owner))
04480 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
04481 p->subs[SUB_REAL].needunhold = 1;
04482 dahdi_enable_ec(p);
04483 }
04484
04485 }
04486 }
04487 winkflashdone:
04488 update_conf(p);
04489 break;
04490 case SIG_EM:
04491 case SIG_EM_E1:
04492 case SIG_EMWINK:
04493 case SIG_FEATD:
04494 case SIG_SF:
04495 case SIG_SFWINK:
04496 case SIG_SF_FEATD:
04497 case SIG_FXSLS:
04498 case SIG_FXSGS:
04499 if (p->dialing)
04500 ast_log(LOG_DEBUG, "Ignoring wink on channel %d\n", p->channel);
04501 else
04502 ast_log(LOG_DEBUG, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
04503 break;
04504 case SIG_FEATDMF_TA:
04505 switch (p->whichwink) {
04506 case 0:
04507 ast_log(LOG_DEBUG, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04508 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04509 break;
04510 case 1:
04511 ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
04512 break;
04513 case 2:
04514 ast_log(LOG_WARNING, "Received unexpected wink on channel of type SIG_FEATDMF_TA\n");
04515 return NULL;
04516 }
04517 p->whichwink++;
04518
04519 case SIG_FEATDMF:
04520 case SIG_E911:
04521 case SIG_FGC_CAMAMF:
04522 case SIG_FGC_CAMA:
04523 case SIG_FEATB:
04524 case SIG_SF_FEATDMF:
04525 case SIG_SF_FEATB:
04526
04527 if (!ast_strlen_zero(p->dop.dialstr)) {
04528 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
04529 if (res < 0) {
04530 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
04531 p->dop.dialstr[0] = '\0';
04532 return NULL;
04533 } else
04534 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04535 }
04536 p->dop.dialstr[0] = '\0';
04537 break;
04538 default:
04539 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
04540 }
04541 break;
04542 case DAHDI_EVENT_HOOKCOMPLETE:
04543 if (p->inalarm) break;
04544 if ((p->radio || (p->oprmode < 0))) break;
04545 switch (mysig) {
04546 case SIG_FXSLS:
04547 case SIG_FXSGS:
04548 case SIG_FXSKS:
04549 case SIG_EM:
04550 case SIG_EM_E1:
04551 case SIG_EMWINK:
04552 case SIG_FEATD:
04553 case SIG_SF:
04554 case SIG_SFWINK:
04555 case SIG_SF_FEATD:
04556 if (!ast_strlen_zero(p->dop.dialstr)) {
04557 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
04558 if (res < 0) {
04559 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
04560 p->dop.dialstr[0] = '\0';
04561 return NULL;
04562 } else
04563 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04564 }
04565 p->dop.dialstr[0] = '\0';
04566 p->dop.op = DAHDI_DIAL_OP_REPLACE;
04567 break;
04568 case SIG_FEATDMF:
04569 case SIG_FEATDMF_TA:
04570 case SIG_E911:
04571 case SIG_FGC_CAMA:
04572 case SIG_FGC_CAMAMF:
04573 case SIG_FEATB:
04574 case SIG_SF_FEATDMF:
04575 case SIG_SF_FEATB:
04576 ast_log(LOG_DEBUG, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
04577 break;
04578 default:
04579 break;
04580 }
04581 break;
04582 case DAHDI_EVENT_POLARITY:
04583
04584
04585
04586
04587
04588
04589 if (p->polarity == POLARITY_IDLE) {
04590 p->polarity = POLARITY_REV;
04591 if (p->answeronpolarityswitch &&
04592 ((ast->_state == AST_STATE_DIALING) ||
04593 (ast->_state == AST_STATE_RINGING))) {
04594 ast_log(LOG_DEBUG, "Answering on polarity switch!\n");
04595 ast_setstate(p->owner, AST_STATE_UP);
04596 if (p->hanguponpolarityswitch) {
04597 gettimeofday(&p->polaritydelaytv, NULL);
04598 }
04599 } else
04600 ast_log(LOG_DEBUG, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, ast->_state);
04601 }
04602
04603
04604 if (p->hanguponpolarityswitch &&
04605 (p->polarityonanswerdelay > 0) &&
04606 (p->polarity == POLARITY_REV) &&
04607 ((ast->_state == AST_STATE_UP) || (ast->_state == AST_STATE_RING)) ) {
04608
04609 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) );
04610
04611 if (ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
04612 ast_log(LOG_DEBUG, "Polarity Reversal detected and now Hanging up on channel %d\n", p->channel);
04613 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
04614 p->polarity = POLARITY_IDLE;
04615 } else {
04616 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);
04617 }
04618 } else {
04619 p->polarity = POLARITY_IDLE;
04620 ast_log(LOG_DEBUG, "Ignoring Polarity switch to IDLE on channel %d, state %d\n", p->channel, ast->_state);
04621 }
04622
04623 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) );
04624 break;
04625 default:
04626 ast_log(LOG_DEBUG, "Dunno what to do with event %d on channel %d\n", res, p->channel);
04627 }
04628 return &p->subs[index].f;
04629 }
04630
04631 static struct ast_frame *__dahdi_exception(struct ast_channel *ast)
04632 {
04633 struct dahdi_pvt *p = ast->tech_pvt;
04634 int res;
04635 int usedindex=-1;
04636 int index;
04637 struct ast_frame *f;
04638
04639
04640 index = dahdi_get_index(ast, p, 1);
04641
04642 p->subs[index].f.frametype = AST_FRAME_NULL;
04643 p->subs[index].f.datalen = 0;
04644 p->subs[index].f.samples = 0;
04645 p->subs[index].f.mallocd = 0;
04646 p->subs[index].f.offset = 0;
04647 p->subs[index].f.subclass = 0;
04648 p->subs[index].f.delivery = ast_tv(0,0);
04649 p->subs[index].f.src = "dahdi_exception";
04650 p->subs[index].f.data = NULL;
04651
04652
04653 if ((!p->owner) && (!(p->radio || (p->oprmode < 0)))) {
04654
04655
04656
04657
04658
04659 if (p->fake_event) {
04660 res = p->fake_event;
04661 p->fake_event = 0;
04662 } else
04663 res = dahdi_get_event(p->subs[SUB_REAL].dfd);
04664
04665 if ((res != DAHDI_EVENT_RINGEROFF) && (res != DAHDI_EVENT_RINGERON) &&
04666 (res != DAHDI_EVENT_HOOKCOMPLETE)) {
04667 ast_log(LOG_DEBUG, "Restoring owner of channel %d on event %d\n", p->channel, res);
04668 p->owner = p->subs[SUB_REAL].owner;
04669 if (p->owner && ast_bridged_channel(p->owner))
04670 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
04671 p->subs[SUB_REAL].needunhold = 1;
04672 }
04673 switch (res) {
04674 case DAHDI_EVENT_ONHOOK:
04675 dahdi_disable_ec(p);
04676 if (p->owner) {
04677 if (option_verbose > 2)
04678 ast_verbose(VERBOSE_PREFIX_3 "Channel %s still has call, ringing phone\n", p->owner->name);
04679 dahdi_ring_phone(p);
04680 p->callwaitingrepeat = 0;
04681 p->cidcwexpire = 0;
04682 } else
04683 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04684 update_conf(p);
04685 break;
04686 case DAHDI_EVENT_RINGOFFHOOK:
04687 dahdi_enable_ec(p);
04688 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
04689 if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
04690 p->subs[SUB_REAL].needanswer = 1;
04691 p->dialing = 0;
04692 }
04693 break;
04694 case DAHDI_EVENT_HOOKCOMPLETE:
04695 case DAHDI_EVENT_RINGERON:
04696 case DAHDI_EVENT_RINGEROFF:
04697
04698 break;
04699 case DAHDI_EVENT_WINKFLASH:
04700 gettimeofday(&p->flashtime, NULL);
04701 if (p->owner) {
04702 if (option_verbose > 2)
04703 ast_verbose(VERBOSE_PREFIX_3 "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
04704 if (p->owner->_state != AST_STATE_UP) {
04705
04706 usedindex = dahdi_get_index(p->owner, p, 0);
04707 if (usedindex > -1) {
04708 p->subs[usedindex].needanswer = 1;
04709 }
04710 ast_setstate(p->owner, AST_STATE_UP);
04711 }
04712 p->callwaitingrepeat = 0;
04713 p->cidcwexpire = 0;
04714 if (ast_bridged_channel(p->owner))
04715 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
04716 p->subs[SUB_REAL].needunhold = 1;
04717 } else
04718 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04719 update_conf(p);
04720 break;
04721 default:
04722 ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
04723 }
04724 f = &p->subs[index].f;
04725 return f;
04726 }
04727 if (!(p->radio || (p->oprmode < 0)) && option_debug)
04728 ast_log(LOG_DEBUG, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
04729
04730 if (ast != p->owner) {
04731 ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
04732 f = &p->subs[index].f;
04733 return f;
04734 }
04735 f = dahdi_handle_event(ast);
04736 return f;
04737 }
04738
04739 static struct ast_frame *dahdi_exception(struct ast_channel *ast)
04740 {
04741 struct dahdi_pvt *p = ast->tech_pvt;
04742 struct ast_frame *f;
04743 ast_mutex_lock(&p->lock);
04744 f = __dahdi_exception(ast);
04745 ast_mutex_unlock(&p->lock);
04746 return f;
04747 }
04748
04749 static struct ast_frame *dahdi_read(struct ast_channel *ast)
04750 {
04751 struct dahdi_pvt *p = ast->tech_pvt;
04752 int res;
04753 int index;
04754 void *readbuf;
04755 struct ast_frame *f;
04756
04757 while (ast_mutex_trylock(&p->lock)) {
04758 DEADLOCK_AVOIDANCE(&ast->lock);
04759 }
04760
04761 index = dahdi_get_index(ast, p, 0);
04762
04763
04764 if (index < 0) {
04765 ast_log(LOG_WARNING, "We dont exist?\n");
04766 ast_mutex_unlock(&p->lock);
04767 return NULL;
04768 }
04769
04770 if ((p->radio || (p->oprmode < 0)) && p->inalarm) return NULL;
04771
04772 p->subs[index].f.frametype = AST_FRAME_NULL;
04773 p->subs[index].f.datalen = 0;
04774 p->subs[index].f.samples = 0;
04775 p->subs[index].f.mallocd = 0;
04776 p->subs[index].f.offset = 0;
04777 p->subs[index].f.subclass = 0;
04778 p->subs[index].f.delivery = ast_tv(0,0);
04779 p->subs[index].f.src = "dahdi_read";
04780 p->subs[index].f.data = NULL;
04781
04782
04783 if ((p->radio || (p->oprmode < 0)) && (!p->firstradio))
04784 {
04785 struct dahdi_params ps;
04786
04787 ps.channo = p->channel;
04788 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps) < 0) {
04789 ast_mutex_unlock(&p->lock);
04790 return NULL;
04791 }
04792 p->firstradio = 1;
04793 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04794 if (ps.rxisoffhook)
04795 {
04796 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
04797 }
04798 else
04799 {
04800 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
04801 }
04802 ast_mutex_unlock(&p->lock);
04803 return &p->subs[index].f;
04804 }
04805 if (p->ringt == 1) {
04806 ast_mutex_unlock(&p->lock);
04807 return NULL;
04808 }
04809 else if (p->ringt > 0)
04810 p->ringt--;
04811
04812 if (p->subs[index].needringing) {
04813
04814 p->subs[index].needringing = 0;
04815 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04816 p->subs[index].f.subclass = AST_CONTROL_RINGING;
04817 ast_setstate(ast, AST_STATE_RINGING);
04818 ast_mutex_unlock(&p->lock);
04819 return &p->subs[index].f;
04820 }
04821
04822 if (p->subs[index].needbusy) {
04823
04824 p->subs[index].needbusy = 0;
04825 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04826 p->subs[index].f.subclass = AST_CONTROL_BUSY;
04827 ast_mutex_unlock(&p->lock);
04828 return &p->subs[index].f;
04829 }
04830
04831 if (p->subs[index].needcongestion) {
04832
04833 p->subs[index].needcongestion = 0;
04834 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04835 p->subs[index].f.subclass = AST_CONTROL_CONGESTION;
04836 ast_mutex_unlock(&p->lock);
04837 return &p->subs[index].f;
04838 }
04839
04840 if (p->subs[index].needcallerid) {
04841 ast_set_callerid(ast, S_OR(p->lastcid_num, NULL),
04842 S_OR(p->lastcid_name, NULL),
04843 S_OR(p->lastcid_num, NULL)
04844 );
04845 p->subs[index].needcallerid = 0;
04846 }
04847
04848 if (p->subs[index].needanswer) {
04849
04850 p->subs[index].needanswer = 0;
04851 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04852 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04853 ast_mutex_unlock(&p->lock);
04854 return &p->subs[index].f;
04855 }
04856
04857 if (p->subs[index].needflash) {
04858
04859 p->subs[index].needflash = 0;
04860 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04861 p->subs[index].f.subclass = AST_CONTROL_FLASH;
04862 ast_mutex_unlock(&p->lock);
04863 return &p->subs[index].f;
04864 }
04865
04866 if (p->subs[index].needhold) {
04867
04868 p->subs[index].needhold = 0;
04869 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04870 p->subs[index].f.subclass = AST_CONTROL_HOLD;
04871 ast_mutex_unlock(&p->lock);
04872 ast_log(LOG_DEBUG, "Sending hold on '%s'\n", ast->name);
04873 return &p->subs[index].f;
04874 }
04875
04876 if (p->subs[index].needunhold) {
04877
04878 p->subs[index].needunhold = 0;
04879 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04880 p->subs[index].f.subclass = AST_CONTROL_UNHOLD;
04881 ast_mutex_unlock(&p->lock);
04882 ast_log(LOG_DEBUG, "Sending unhold on '%s'\n", ast->name);
04883 return &p->subs[index].f;
04884 }
04885
04886 if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
04887 if (!p->subs[index].linear) {
04888 p->subs[index].linear = 1;
04889 res = dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
04890 if (res)
04891 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, index);
04892 }
04893 } else if ((ast->rawreadformat == AST_FORMAT_ULAW) ||
04894 (ast->rawreadformat == AST_FORMAT_ALAW)) {
04895 if (p->subs[index].linear) {
04896 p->subs[index].linear = 0;
04897 res = dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
04898 if (res)
04899 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to companded mode.\n", p->channel, index);
04900 }
04901 } else {
04902 ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast->rawreadformat));
04903 ast_mutex_unlock(&p->lock);
04904 return NULL;
04905 }
04906 readbuf = ((unsigned char *)p->subs[index].buffer) + AST_FRIENDLY_OFFSET;
04907 CHECK_BLOCKING(ast);
04908 res = read(p->subs[index].dfd, readbuf, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04909 ast_clear_flag(ast, AST_FLAG_BLOCKING);
04910
04911 if (res < 0) {
04912 f = NULL;
04913 if (res == -1) {
04914 if (errno == EAGAIN) {
04915
04916 ast_mutex_unlock(&p->lock);
04917 return &p->subs[index].f;
04918 } else if (errno == ELAST) {
04919 f = __dahdi_exception(ast);
04920 } else
04921 ast_log(LOG_WARNING, "dahdi_rec: %s\n", strerror(errno));
04922 }
04923 ast_mutex_unlock(&p->lock);
04924 return f;
04925 }
04926 if (res != (p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE)) {
04927 ast_log(LOG_DEBUG, "Short read (%d/%d), must be an event...\n", res, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04928 f = __dahdi_exception(ast);
04929 ast_mutex_unlock(&p->lock);
04930 return f;
04931 }
04932 if (p->tdd) {
04933 int c;
04934
04935 c = tdd_feed(p->tdd,readbuf,READ_SIZE);
04936 if (c < 0) {
04937 ast_log(LOG_DEBUG,"tdd_feed failed\n");
04938 ast_mutex_unlock(&p->lock);
04939 return NULL;
04940 }
04941 if (c) {
04942 p->subs[index].f.subclass = 0;
04943 p->subs[index].f.frametype = AST_FRAME_TEXT;
04944 p->subs[index].f.mallocd = 0;
04945 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04946 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET;
04947 p->subs[index].f.datalen = 1;
04948 *((char *) p->subs[index].f.data) = c;
04949 ast_mutex_unlock(&p->lock);
04950 return &p->subs[index].f;
04951 }
04952 }
04953
04954 if (p->callwaitingrepeat && dahdi_get_index(ast, p, 1) == SUB_REAL) {
04955 p->callwaitingrepeat--;
04956 }
04957 if (p->cidcwexpire)
04958 p->cidcwexpire--;
04959
04960 if (p->callwaitingrepeat == 1) {
04961 p->callwaitrings++;
04962 dahdi_callwait(ast);
04963 }
04964
04965 if (p->cidcwexpire == 1) {
04966 if (option_verbose > 2)
04967 ast_verbose(VERBOSE_PREFIX_3 "CPE does not support Call Waiting Caller*ID.\n");
04968 restore_conference(p);
04969 }
04970 if (p->subs[index].linear) {
04971 p->subs[index].f.datalen = READ_SIZE * 2;
04972 } else
04973 p->subs[index].f.datalen = READ_SIZE;
04974
04975
04976 if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
04977 send_callerid(p);
04978 }
04979
04980 p->subs[index].f.frametype = AST_FRAME_VOICE;
04981 p->subs[index].f.subclass = ast->rawreadformat;
04982 p->subs[index].f.samples = READ_SIZE;
04983 p->subs[index].f.mallocd = 0;
04984 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04985 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET / sizeof(p->subs[index].buffer[0]);
04986 #if 0
04987 ast_log(LOG_DEBUG, "Read %d of voice on %s\n", p->subs[index].f.datalen, ast->name);
04988 #endif
04989 if (p->dialing ||
04990 (index && (ast->_state != AST_STATE_UP)) ||
04991 ((index == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway)
04992 ) {
04993
04994
04995 p->subs[index].f.frametype = AST_FRAME_NULL;
04996 p->subs[index].f.subclass = 0;
04997 p->subs[index].f.samples = 0;
04998 p->subs[index].f.mallocd = 0;
04999 p->subs[index].f.offset = 0;
05000 p->subs[index].f.data = NULL;
05001 p->subs[index].f.datalen= 0;
05002 }
05003 if (p->dsp && (!p->ignoredtmf || p->callwaitcas || p->busydetect || p->callprogress) && !index) {
05004
05005 f = ast_dsp_process(ast, p->dsp, &p->subs[index].f);
05006 if (f) {
05007 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
05008 if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
05009
05010
05011 f = NULL;
05012 }
05013 } else if (f->frametype == AST_FRAME_DTMF) {
05014 #ifdef HAVE_PRI
05015 if (!p->proceeding && p->sig==SIG_PRI && p->pri && p->pri->overlapdial) {
05016
05017 f->frametype = AST_FRAME_NULL;
05018 f->subclass = 0;
05019 }
05020 #endif
05021
05022 p->pulsedial = 0;
05023 }
05024 }
05025 } else
05026 f = &p->subs[index].f;
05027
05028 if (f && (f->frametype == AST_FRAME_DTMF))
05029 dahdi_handle_dtmfup(ast, index, &f);
05030
05031
05032 if (p->fake_event)
05033 ast_set_flag(ast, AST_FLAG_EXCEPTION);
05034
05035 ast_mutex_unlock(&p->lock);
05036 return f;
05037 }
05038
05039 static int my_dahdi_write(struct dahdi_pvt *p, unsigned char *buf, int len, int index, int linear)
05040 {
05041 int sent=0;
05042 int size;
05043 int res;
05044 int fd;
05045 fd = p->subs[index].dfd;
05046 while (len) {
05047 size = len;
05048 if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
05049 size = (linear ? READ_SIZE * 2 : READ_SIZE);
05050 res = write(fd, buf, size);
05051 if (res != size) {
05052 if (option_debug)
05053 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
05054 return sent;
05055 }
05056 len -= size;
05057 buf += size;
05058 }
05059 return sent;
05060 }
05061
05062 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame)
05063 {
05064 struct dahdi_pvt *p = ast->tech_pvt;
05065 int res;
05066 int index;
05067 index = dahdi_get_index(ast, p, 0);
05068 if (index < 0) {
05069 ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
05070 return -1;
05071 }
05072
05073 #if 0
05074 #ifdef HAVE_PRI
05075 ast_mutex_lock(&p->lock);
05076 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05077 if (p->pri->pri) {
05078 if (!pri_grab(p, p->pri)) {
05079 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
05080 pri_rel(p->pri);
05081 } else
05082 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05083 }
05084 p->proceeding=1;
05085 }
05086 ast_mutex_unlock(&p->lock);
05087 #endif
05088 #endif
05089
05090 if (frame->frametype != AST_FRAME_VOICE) {
05091 if (frame->frametype != AST_FRAME_IMAGE)
05092 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
05093 return 0;
05094 }
05095 if ((frame->subclass != AST_FORMAT_SLINEAR) &&
05096 (frame->subclass != AST_FORMAT_ULAW) &&
05097 (frame->subclass != AST_FORMAT_ALAW)) {
05098 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
05099 return -1;
05100 }
05101 if (p->dialing) {
05102 if (option_debug)
05103 ast_log(LOG_DEBUG, "Dropping frame since I'm still dialing on %s...\n",ast->name);
05104 return 0;
05105 }
05106 if (!p->owner) {
05107 if (option_debug)
05108 ast_log(LOG_DEBUG, "Dropping frame since there is no active owner on %s...\n",ast->name);
05109 return 0;
05110 }
05111 if (p->cidspill) {
05112 if (option_debug)
05113 ast_log(LOG_DEBUG, "Dropping frame since I've still got a callerid spill\n");
05114 return 0;
05115 }
05116
05117 if (!frame->data || !frame->datalen)
05118 return 0;
05119
05120 if (frame->subclass == AST_FORMAT_SLINEAR) {
05121 if (!p->subs[index].linear) {
05122 p->subs[index].linear = 1;
05123 res = dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
05124 if (res)
05125 ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
05126 }
05127 res = my_dahdi_write(p, (unsigned char *)frame->data, frame->datalen, index, 1);
05128 } else {
05129
05130 if (p->subs[index].linear) {
05131 p->subs[index].linear = 0;
05132 res = dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
05133 if (res)
05134 ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
05135 }
05136 res = my_dahdi_write(p, (unsigned char *)frame->data, frame->datalen, index, 0);
05137 }
05138 if (res < 0) {
05139 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
05140 return -1;
05141 }
05142 return 0;
05143 }
05144
05145 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
05146 {
05147 struct dahdi_pvt *p = chan->tech_pvt;
05148 int res=-1;
05149 int index;
05150 int func = DAHDI_FLASH;
05151 ast_mutex_lock(&p->lock);
05152 index = dahdi_get_index(chan, p, 0);
05153 if (option_debug)
05154 ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
05155 if (index == SUB_REAL) {
05156 switch (condition) {
05157 case AST_CONTROL_BUSY:
05158 #ifdef HAVE_PRI
05159 if (p->priindication_oob && p->sig == SIG_PRI) {
05160 chan->hangupcause = AST_CAUSE_USER_BUSY;
05161 chan->_softhangup |= AST_SOFTHANGUP_DEV;
05162 res = 0;
05163 } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05164 if (p->pri->pri) {
05165 if (!pri_grab(p, p->pri)) {
05166 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
05167 pri_rel(p->pri);
05168 }
05169 else
05170 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05171 }
05172 p->progress = 1;
05173 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_BUSY);
05174 } else
05175 #endif
05176 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_BUSY);
05177 break;
05178 case AST_CONTROL_RINGING:
05179 #ifdef HAVE_PRI
05180 if ((!p->alerting) && p->sig==SIG_PRI && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
05181 if (p->pri->pri) {
05182 if (!pri_grab(p, p->pri)) {
05183 pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
05184 pri_rel(p->pri);
05185 }
05186 else
05187 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05188 }
05189 p->alerting = 1;
05190 }
05191 #endif
05192 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_RINGTONE);
05193 if (chan->_state != AST_STATE_UP) {
05194 if ((chan->_state != AST_STATE_RING) ||
05195 ((p->sig != SIG_FXSKS) &&
05196 (p->sig != SIG_FXSLS) &&
05197 (p->sig != SIG_FXSGS)))
05198 ast_setstate(chan, AST_STATE_RINGING);
05199 }
05200 break;
05201 case AST_CONTROL_PROCEEDING:
05202 ast_log(LOG_DEBUG,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
05203 #ifdef HAVE_PRI
05204 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05205 if (p->pri->pri) {
05206 if (!pri_grab(p, p->pri)) {
05207 pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
05208 pri_rel(p->pri);
05209 }
05210 else
05211 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05212 }
05213 p->proceeding = 1;
05214 }
05215 #endif
05216
05217 res = 0;
05218 break;
05219 case AST_CONTROL_PROGRESS:
05220 ast_log(LOG_DEBUG,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
05221 #ifdef HAVE_PRI
05222 p->digital = 0;
05223 if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05224 if (p->pri->pri) {
05225 if (!pri_grab(p, p->pri)) {
05226 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
05227 pri_rel(p->pri);
05228 }
05229 else
05230 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05231 }
05232 p->progress = 1;
05233 }
05234 #endif
05235
05236 res = 0;
05237 break;
05238 case AST_CONTROL_CONGESTION:
05239 chan->hangupcause = AST_CAUSE_CONGESTION;
05240 #ifdef HAVE_PRI
05241 if (p->priindication_oob && p->sig == SIG_PRI) {
05242 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
05243 chan->_softhangup |= AST_SOFTHANGUP_DEV;
05244 res = 0;
05245 } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05246 if (p->pri) {
05247 if (!pri_grab(p, p->pri)) {
05248 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
05249 pri_rel(p->pri);
05250 } else
05251 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05252 }
05253 p->progress = 1;
05254 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
05255 } else
05256 #endif
05257 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
05258 break;
05259 case AST_CONTROL_HOLD:
05260 #ifdef HAVE_PRI
05261 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
05262 if (!pri_grab(p, p->pri)) {
05263 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
05264 pri_rel(p->pri);
05265 } else
05266 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05267 } else
05268 #endif
05269 ast_moh_start(chan, data, p->mohinterpret);
05270 break;
05271 case AST_CONTROL_UNHOLD:
05272 #ifdef HAVE_PRI
05273 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
05274 if (!pri_grab(p, p->pri)) {
05275 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
05276 pri_rel(p->pri);
05277 } else
05278 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05279 } else
05280 #endif
05281 ast_moh_stop(chan);
05282 break;
05283 case AST_CONTROL_RADIO_KEY:
05284 if (p->radio)
05285 res = dahdi_set_hook(p->subs[index].dfd, DAHDI_OFFHOOK);
05286 res = 0;
05287 break;
05288 case AST_CONTROL_RADIO_UNKEY:
05289 if (p->radio)
05290 res = dahdi_set_hook(p->subs[index].dfd, DAHDI_RINGOFF);
05291 res = 0;
05292 break;
05293 case AST_CONTROL_FLASH:
05294
05295 if (ISTRUNK(p) && (p->sig != SIG_PRI)) {
05296
05297 p->dop.dialstr[0] = '\0';
05298 if ((ioctl(p->subs[SUB_REAL].dfd,DAHDI_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
05299 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
05300 chan->name, strerror(errno));
05301 } else
05302 res = 0;
05303 } else
05304 res = 0;
05305 break;
05306 case AST_CONTROL_SRCUPDATE:
05307 res = 0;
05308 break;
05309 case -1:
05310 res = tone_zone_play_tone(p->subs[index].dfd, -1);
05311 break;
05312 }
05313 } else
05314 res = 0;
05315 ast_mutex_unlock(&p->lock);
05316 return res;
05317 }
05318
05319 static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpbx, int index, int law, int transfercapability)
05320 {
05321 struct ast_channel *tmp;
05322 int deflaw;
05323 int res;
05324 int x,y;
05325 int features;
05326 char *b2 = NULL;
05327 struct dahdi_params ps;
05328 char chanprefix[*dahdi_chan_name_len + 4];
05329
05330 if (i->subs[index].owner) {
05331 ast_log(LOG_WARNING, "Channel %d already has a %s call\n", i->channel,subnames[index]);
05332 return NULL;
05333 }
05334 y = 1;
05335 do {
05336 if (b2)
05337 free(b2);
05338 #ifdef HAVE_PRI
05339 if (i->bearer || (i->pri && (i->sig == SIG_FXSKS)))
05340 b2 = ast_safe_string_alloc("%d:%d-%d", i->pri->trunkgroup, i->channel, y);
05341 else
05342 #endif
05343 if (i->channel == CHAN_PSEUDO)
05344 b2 = ast_safe_string_alloc("pseudo-%ld", ast_random());
05345 else
05346 b2 = ast_safe_string_alloc("%d-%d", i->channel, y);
05347 for (x = 0; x < 3; x++) {
05348 if ((index != x) && i->subs[x].owner && !strcasecmp(b2, i->subs[x].owner->name + (!strncmp(i->subs[x].owner->name, "Zap", 3) ? 4 : 6)))
05349 break;
05350 }
05351 y++;
05352 } while (x < 3);
05353 strcpy(chanprefix, dahdi_chan_name);
05354 strcat(chanprefix, "/%s");
05355 tmp = ast_channel_alloc(0, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, i->amaflags, chanprefix, b2);
05356 if (b2)
05357 free(b2);
05358 if (!tmp)
05359 return NULL;
05360 tmp->tech = chan_tech;
05361 ps.channo = i->channel;
05362 res = ioctl(i->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps);
05363 if (res) {
05364 ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW: %s\n", strerror(errno));
05365 ps.curlaw = DAHDI_LAW_MULAW;
05366 }
05367 if (ps.curlaw == DAHDI_LAW_ALAW)
05368 deflaw = AST_FORMAT_ALAW;
05369 else
05370 deflaw = AST_FORMAT_ULAW;
05371 if (law) {
05372 if (law == DAHDI_LAW_ALAW)
05373 deflaw = AST_FORMAT_ALAW;
05374 else
05375 deflaw = AST_FORMAT_ULAW;
05376 }
05377 tmp->fds[0] = i->subs[index].dfd;
05378 tmp->nativeformats = AST_FORMAT_SLINEAR | deflaw;
05379
05380 tmp->rawreadformat = deflaw;
05381 tmp->readformat = deflaw;
05382 tmp->rawwriteformat = deflaw;
05383 tmp->writeformat = deflaw;
05384 i->subs[index].linear = 0;
05385 dahdi_setlinear(i->subs[index].dfd, i->subs[index].linear);
05386 features = 0;
05387 if (index == SUB_REAL) {
05388 if (i->busydetect && CANBUSYDETECT(i))
05389 features |= DSP_FEATURE_BUSY_DETECT;
05390 if ((i->callprogress & 1) && CANPROGRESSDETECT(i))
05391 features |= DSP_FEATURE_CALL_PROGRESS;
05392 if ((!i->outgoing && (i->callprogress & 4)) ||
05393 (i->outgoing && (i->callprogress & 2))) {
05394 features |= DSP_FEATURE_FAX_DETECT;
05395 }
05396 #ifdef DAHDI_TONEDETECT
05397 x = DAHDI_TONEDETECT_ON | DAHDI_TONEDETECT_MUTE;
05398 if (ioctl(i->subs[index].dfd, DAHDI_TONEDETECT, &x)) {
05399 #endif
05400 i->hardwaredtmf = 0;
05401 features |= DSP_FEATURE_DTMF_DETECT;
05402 #ifdef DAHDI_TONEDETECT
05403 } else if (NEED_MFDETECT(i)) {
05404 i->hardwaredtmf = 1;
05405 features |= DSP_FEATURE_DTMF_DETECT;
05406 }
05407 #endif
05408 }
05409 if (features) {
05410 if (i->dsp) {
05411 ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", tmp->name);
05412 } else {
05413 if (i->channel != CHAN_PSEUDO)
05414 i->dsp = ast_dsp_new();
05415 else
05416 i->dsp = NULL;
05417 if (i->dsp) {
05418 i->dsp_features = features;
05419 #ifdef HAVE_PRI
05420
05421 if (i->outgoing && (i->sig == SIG_PRI)) {
05422
05423
05424 i->dsp_features = features & ~DSP_PROGRESS_TALK;
05425 features = 0;
05426 }
05427 #endif
05428 ast_dsp_set_features(i->dsp, features);
05429 ast_dsp_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax);
05430 if (!ast_strlen_zero(progzone))
05431 ast_dsp_set_call_progress_zone(i->dsp, progzone);
05432 if (i->busydetect && CANBUSYDETECT(i)) {
05433 ast_dsp_set_busy_count(i->dsp, i->busycount);
05434 ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength);
05435 }
05436 }
05437 }
05438 }
05439
05440 if (state == AST_STATE_RING)
05441 tmp->rings = 1;
05442 tmp->tech_pvt = i;
05443 if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) {
05444
05445 tmp->callgroup = i->callgroup;
05446 tmp->pickupgroup = i->pickupgroup;
05447 }
05448 if (!ast_strlen_zero(i->language))
05449 ast_string_field_set(tmp, language, i->language);
05450 if (!i->owner)
05451 i->owner = tmp;
05452 if (!ast_strlen_zero(i->accountcode))
05453 ast_string_field_set(tmp, accountcode, i->accountcode);
05454 if (i->amaflags)
05455 tmp->amaflags = i->amaflags;
05456 i->subs[index].owner = tmp;
05457 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
05458 ast_string_field_set(tmp, call_forward, i->call_forward);
05459
05460 if (!i->adsi)
05461 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
05462 if (!ast_strlen_zero(i->exten))
05463 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
05464 if (!ast_strlen_zero(i->rdnis))
05465 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
05466 if (!ast_strlen_zero(i->dnid))
05467 tmp->cid.cid_dnid = ast_strdup(i->dnid);
05468
05469
05470
05471 #ifdef PRI_ANI
05472 if (!ast_strlen_zero(i->cid_ani))
05473 tmp->cid.cid_ani = ast_strdup(i->cid_ani);
05474 else
05475 tmp->cid.cid_ani = ast_strdup(i->cid_num);
05476 #else
05477 tmp->cid.cid_ani = ast_strdup(i->cid_num);
05478 #endif
05479 tmp->cid.cid_pres = i->callingpres;
05480 tmp->cid.cid_ton = i->cid_ton;
05481 #ifdef HAVE_PRI
05482 tmp->transfercapability = transfercapability;
05483 pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
05484 if (transfercapability & PRI_TRANS_CAP_DIGITAL)
05485 i->digital = 1;
05486
05487 i->isidlecall = 0;
05488 i->alreadyhungup = 0;
05489 #endif
05490
05491 i->fake_event = 0;
05492
05493 dahdi_confmute(i, 0);
05494
05495 ast_jb_configure(tmp, &global_jbconf);
05496 if (startpbx) {
05497 if (ast_pbx_start(tmp)) {
05498 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
05499 ast_hangup(tmp);
05500 i->owner = NULL;
05501 return NULL;
05502 }
05503 }
05504
05505 ast_module_ref(ast_module_info->self);
05506
05507 return tmp;
05508 }
05509
05510
05511 static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
05512 {
05513 char c;
05514
05515 *str = 0;
05516 for (;;)
05517 {
05518
05519 c = ast_waitfordigit(chan, ms);
05520
05521 if (c < 1)
05522 return c;
05523 *str++ = c;
05524 *str = 0;
05525 if (strchr(term, c))
05526 return 1;
05527 }
05528 }
05529
05530 static int dahdi_wink(struct dahdi_pvt *p, int index)
05531 {
05532 int j;
05533 dahdi_set_hook(p->subs[index].dfd, DAHDI_WINK);
05534 for (;;)
05535 {
05536
05537 j = DAHDI_IOMUX_SIGEVENT;
05538
05539 if (ioctl(p->subs[index].dfd,DAHDI_IOMUX,&j) == -1) return(-1);
05540
05541 if (j & DAHDI_IOMUX_SIGEVENT) break;
05542 }
05543
05544 if (ioctl(p->subs[index].dfd,DAHDI_GETEVENT,&j) == -1) return(-1);
05545 return 0;
05546 }
05547
05548 static void *ss_thread(void *data)
05549 {
05550 struct ast_channel *chan = data;
05551 struct dahdi_pvt *p = chan->tech_pvt;
05552 char exten[AST_MAX_EXTENSION] = "";
05553 char exten2[AST_MAX_EXTENSION] = "";
05554 unsigned char buf[256];
05555 char dtmfcid[300];
05556 char dtmfbuf[300];
05557 struct callerid_state *cs = NULL;
05558 char *name = NULL, *number = NULL;
05559 int distMatches;
05560 int curRingData[3];
05561 int receivedRingT;
05562 int counter1;
05563 int counter;
05564 int samples = 0;
05565 struct ast_smdi_md_message *smdi_msg = NULL;
05566 int flags;
05567 int i;
05568 int timeout;
05569 int getforward = 0;
05570 char *s1, *s2;
05571 int len = 0;
05572 int res;
05573 int index;
05574
05575 ast_mutex_lock(&ss_thread_lock);
05576 ss_thread_count++;
05577 ast_mutex_unlock(&ss_thread_lock);
05578
05579
05580
05581 if (!p) {
05582 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
05583 ast_hangup(chan);
05584 goto quit;
05585 }
05586 if (option_verbose > 2)
05587 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name);
05588 index = dahdi_get_index(chan, p, 1);
05589 if (index < 0) {
05590 ast_log(LOG_WARNING, "Huh?\n");
05591 ast_hangup(chan);
05592 goto quit;
05593 }
05594 if (p->dsp)
05595 ast_dsp_digitreset(p->dsp);
05596 switch (p->sig) {
05597 #ifdef HAVE_PRI
05598 case SIG_PRI:
05599
05600 ast_copy_string(exten, p->exten, sizeof(exten));
05601 len = strlen(exten);
05602 res = 0;
05603 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05604 if (len && !ast_ignore_pattern(chan->context, exten))
05605 tone_zone_play_tone(p->subs[index].dfd, -1);
05606 else
05607 tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE);
05608 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
05609 timeout = matchdigittimeout;
05610 else
05611 timeout = gendigittimeout;
05612 res = ast_waitfordigit(chan, timeout);
05613 if (res < 0) {
05614 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05615 ast_hangup(chan);
05616 goto quit;
05617 } else if (res) {
05618 exten[len++] = res;
05619 exten[len] = '\0';
05620 } else
05621 break;
05622 }
05623
05624 if (ast_strlen_zero(exten)) {
05625 if (option_verbose > 2)
05626 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n");
05627 exten[0] = 's';
05628 exten[1] = '\0';
05629 }
05630 tone_zone_play_tone(p->subs[index].dfd, -1);
05631 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
05632
05633 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05634 if (p->dsp) ast_dsp_digitreset(p->dsp);
05635 dahdi_enable_ec(p);
05636 ast_setstate(chan, AST_STATE_RING);
05637 res = ast_pbx_run(chan);
05638 if (res) {
05639 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
05640 }
05641 } else {
05642 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
05643 chan->hangupcause = AST_CAUSE_UNALLOCATED;
05644 ast_hangup(chan);
05645 p->exten[0] = '\0';
05646
05647 p->call = NULL;
05648 }
05649 goto quit;
05650 break;
05651 #endif
05652 case SIG_FEATD:
05653 case SIG_FEATDMF:
05654 case SIG_FEATDMF_TA:
05655 case SIG_E911:
05656 case SIG_FGC_CAMAMF:
05657 case SIG_FEATB:
05658 case SIG_EMWINK:
05659 case SIG_SF_FEATD:
05660 case SIG_SF_FEATDMF:
05661 case SIG_SF_FEATB:
05662 case SIG_SFWINK:
05663 if (dahdi_wink(p, index))
05664 goto quit;
05665
05666 case SIG_EM:
05667 case SIG_EM_E1:
05668 case SIG_SF:
05669 case SIG_FGC_CAMA:
05670 res = tone_zone_play_tone(p->subs[index].dfd, -1);
05671 if (p->dsp)
05672 ast_dsp_digitreset(p->dsp);
05673
05674 if (p->dsp) {
05675 if (NEED_MFDETECT(p))
05676 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax);
05677 else
05678 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05679 }
05680 memset(dtmfbuf, 0, sizeof(dtmfbuf));
05681
05682 if (!p->immediate)
05683
05684 res = ast_waitfordigit(chan, 5000);
05685 else
05686 res = 0;
05687 if (res > 0) {
05688
05689 dtmfbuf[0] = res;
05690 switch (p->sig) {
05691 case SIG_FEATD:
05692 case SIG_SF_FEATD:
05693 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05694 if (res > 0)
05695 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05696 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05697 break;
05698 case SIG_FEATDMF_TA:
05699 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05700 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05701 if (dahdi_wink(p, index)) goto quit;
05702 dtmfbuf[0] = 0;
05703
05704 res = ast_waitfordigit(chan, 5000);
05705 if (res <= 0) break;
05706 dtmfbuf[0] = res;
05707
05708 case SIG_FEATDMF:
05709 case SIG_E911:
05710 case SIG_FGC_CAMAMF:
05711 case SIG_SF_FEATDMF:
05712 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05713
05714 if ((p->sig == SIG_FEATDMF) && (dtmfbuf[1] != '0') && (strlen(dtmfbuf) != 14))
05715 {
05716 if (dahdi_wink(p, index)) goto quit;
05717 dtmfbuf[0] = 0;
05718
05719 res = ast_waitfordigit(chan, 5000);
05720 if (res <= 0) break;
05721 dtmfbuf[0] = res;
05722 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05723 }
05724 if (res > 0) {
05725
05726 if (p->sig == SIG_E911)
05727 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
05728 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
05729 }
05730 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05731 break;
05732 case SIG_FEATB:
05733 case SIG_SF_FEATB:
05734 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05735 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05736 break;
05737 case SIG_EMWINK:
05738
05739
05740
05741
05742 if (res == '*') {
05743 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05744 if (res > 0)
05745 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05746 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05747 break;
05748 }
05749 default:
05750
05751 len = 1;
05752 dtmfbuf[len] = '\0';
05753 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05754 if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05755 timeout = matchdigittimeout;
05756 } else {
05757 timeout = gendigittimeout;
05758 }
05759 res = ast_waitfordigit(chan, timeout);
05760 if (res < 0) {
05761 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05762 ast_hangup(chan);
05763 goto quit;
05764 } else if (res) {
05765 dtmfbuf[len++] = res;
05766 dtmfbuf[len] = '\0';
05767 } else {
05768 break;
05769 }
05770 }
05771 break;
05772 }
05773 }
05774 if (res == -1) {
05775 ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
05776 ast_hangup(chan);
05777 goto quit;
05778 } else if (res < 0) {
05779 ast_log(LOG_DEBUG, "Got hung up before digits finished\n");
05780 ast_hangup(chan);
05781 goto quit;
05782 }
05783
05784 if (p->sig == SIG_FGC_CAMA) {
05785 char anibuf[100];
05786
05787 if (ast_safe_sleep(chan,1000) == -1) {
05788 ast_hangup(chan);
05789 goto quit;
05790 }
05791 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
05792 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax);
05793 res = my_getsigstr(chan, anibuf, "#", 10000);
05794 if ((res > 0) && (strlen(anibuf) > 2)) {
05795 if (anibuf[strlen(anibuf) - 1] == '#')
05796 anibuf[strlen(anibuf) - 1] = 0;
05797 ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
05798 }
05799 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05800 }
05801
05802 ast_copy_string(exten, dtmfbuf, sizeof(exten));
05803 if (ast_strlen_zero(exten))
05804 ast_copy_string(exten, "s", sizeof(exten));
05805 if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
05806
05807 if (exten[0] == '*') {
05808 char *stringp=NULL;
05809 ast_copy_string(exten2, exten, sizeof(exten2));
05810
05811 stringp=exten2 +1;
05812 s1 = strsep(&stringp, "*");
05813 s2 = strsep(&stringp, "*");
05814 if (s2) {
05815 if (!ast_strlen_zero(p->cid_num))
05816 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05817 else
05818 ast_set_callerid(chan, s1, NULL, s1);
05819 ast_copy_string(exten, s2, sizeof(exten));
05820 } else
05821 ast_copy_string(exten, s1, sizeof(exten));
05822 } else if (p->sig == SIG_FEATD)
05823 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05824 }
05825 if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) {
05826 if (exten[0] == '*') {
05827 char *stringp=NULL;
05828 ast_copy_string(exten2, exten, sizeof(exten2));
05829
05830 stringp=exten2 +1;
05831 s1 = strsep(&stringp, "#");
05832 s2 = strsep(&stringp, "#");
05833 if (s2) {
05834 if (!ast_strlen_zero(p->cid_num))
05835 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05836 else
05837 if (*(s1 + 2))
05838 ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
05839 ast_copy_string(exten, s2 + 1, sizeof(exten));
05840 } else
05841 ast_copy_string(exten, s1 + 2, sizeof(exten));
05842 } else
05843 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05844 }
05845 if ((p->sig == SIG_E911) || (p->sig == SIG_FGC_CAMAMF)) {
05846 if (exten[0] == '*') {
05847 char *stringp=NULL;
05848 ast_copy_string(exten2, exten, sizeof(exten2));
05849
05850 stringp=exten2 +1;
05851 s1 = strsep(&stringp, "#");
05852 s2 = strsep(&stringp, "#");
05853 if (s2 && (*(s2 + 1) == '0')) {
05854 if (*(s2 + 2))
05855 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
05856 }
05857 if (s1) ast_copy_string(exten, s1, sizeof(exten));
05858 else ast_copy_string(exten, "911", sizeof(exten));
05859 } else
05860 ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d. Assuming E&M Wink instead\n", p->channel);
05861 }
05862 if (p->sig == SIG_FEATB) {
05863 if (exten[0] == '*') {
05864 char *stringp=NULL;
05865 ast_copy_string(exten2, exten, sizeof(exten2));
05866
05867 stringp=exten2 +1;
05868 s1 = strsep(&stringp, "#");
05869 ast_copy_string(exten, exten2 + 1, sizeof(exten));
05870 } else
05871 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
05872 }
05873 if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) {
05874 dahdi_wink(p, index);
05875
05876
05877
05878 if (ast_safe_sleep(chan,100)) goto quit;
05879 }
05880 dahdi_enable_ec(p);
05881 if (NEED_MFDETECT(p)) {
05882 if (p->dsp) {
05883 if (!p->hardwaredtmf)
05884 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05885 else {
05886 ast_dsp_free(p->dsp);
05887 p->dsp = NULL;
05888 }
05889 }
05890 }
05891
05892 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
05893 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05894 if (p->dsp) ast_dsp_digitreset(p->dsp);
05895 res = ast_pbx_run(chan);
05896 if (res) {
05897 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05898 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
05899 }
05900 goto quit;
05901 } else {
05902 if (option_verbose > 2)
05903 ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
05904 sleep(2);
05905 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_INFO);
05906 if (res < 0)
05907 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
05908 else
05909 sleep(1);
05910 res = ast_streamfile(chan, "ss-noservice", chan->language);
05911 if (res >= 0)
05912 ast_waitstream(chan, "");
05913 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
05914 ast_hangup(chan);
05915 goto quit;
05916 }
05917 break;
05918 case SIG_FXOLS:
05919 case SIG_FXOGS:
05920 case SIG_FXOKS:
05921
05922 timeout = firstdigittimeout;
05923
05924
05925 if (p->subs[SUB_THREEWAY].owner)
05926 timeout = 999999;
05927 while (len < AST_MAX_EXTENSION-1) {
05928
05929
05930 if (p->immediate)
05931 res = 's';
05932 else
05933 res = ast_waitfordigit(chan, timeout);
05934 timeout = 0;
05935 if (res < 0) {
05936 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05937 res = tone_zone_play_tone(p->subs[index].dfd, -1);
05938 ast_hangup(chan);
05939 goto quit;
05940 } else if (res) {
05941 exten[len++]=res;
05942 exten[len] = '\0';
05943 }
05944 if (!ast_ignore_pattern(chan->context, exten))
05945 tone_zone_play_tone(p->subs[index].dfd, -1);
05946 else
05947 tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE);
05948 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
05949 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05950 if (getforward) {
05951
05952 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
05953 if (option_verbose > 2)
05954 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
05955 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
05956 if (res)
05957 break;
05958 usleep(500000);
05959 res = tone_zone_play_tone(p->subs[index].dfd, -1);
05960 sleep(1);
05961 memset(exten, 0, sizeof(exten));
05962 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALTONE);
05963 len = 0;
05964 getforward = 0;
05965 } else {
05966 res = tone_zone_play_tone(p->subs[index].dfd, -1);
05967 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05968 if (!ast_strlen_zero(p->cid_num)) {
05969 if (!p->hidecallerid)
05970 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05971 else
05972 ast_set_callerid(chan, NULL, NULL, p->cid_num);
05973 }
05974 if (!ast_strlen_zero(p->cid_name)) {
05975 if (!p->hidecallerid)
05976 ast_set_callerid(chan, NULL, p->cid_name, NULL);
05977 }
05978 ast_setstate(chan, AST_STATE_RING);
05979 dahdi_enable_ec(p);
05980 res = ast_pbx_run(chan);
05981 if (res) {
05982 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05983 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
05984 }
05985 goto quit;
05986 }
05987 } else {
05988
05989
05990 timeout = matchdigittimeout;
05991 }
05992 } else if (res == 0) {
05993 ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n");
05994 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
05995 dahdi_wait_event(p->subs[index].dfd);
05996 ast_hangup(chan);
05997 goto quit;
05998 } else if (p->callwaiting && !strcmp(exten, "*70")) {
05999 if (option_verbose > 2)
06000 ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
06001
06002 p->callwaiting = 0;
06003 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06004 if (res) {
06005 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
06006 chan->name, strerror(errno));
06007 }
06008 len = 0;
06009 ioctl(p->subs[index].dfd,DAHDI_CONFDIAG,&len);
06010 memset(exten, 0, sizeof(exten));
06011 timeout = firstdigittimeout;
06012
06013 } else if (!strcmp(exten,ast_pickup_ext())) {
06014
06015
06016
06017
06018 if (index == SUB_REAL) {
06019
06020 if (p->subs[SUB_THREEWAY].owner) {
06021
06022
06023 alloc_sub(p, SUB_CALLWAIT);
06024 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
06025 unalloc_sub(p, SUB_THREEWAY);
06026 }
06027 dahdi_enable_ec(p);
06028 if (ast_pickup_call(chan)) {
06029 ast_log(LOG_DEBUG, "No call pickup possible...\n");
06030 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
06031 dahdi_wait_event(p->subs[index].dfd);
06032 }
06033 ast_hangup(chan);
06034 goto quit;
06035 } else {
06036 ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
06037 ast_hangup(chan);
06038 goto quit;
06039 }
06040
06041 } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
06042 if (option_verbose > 2)
06043 ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
06044
06045 p->hidecallerid = 1;
06046 if (chan->cid.cid_num)
06047 free(chan->cid.cid_num);
06048 chan->cid.cid_num = NULL;
06049 if (chan->cid.cid_name)
06050 free(chan->cid.cid_name);
06051 chan->cid.cid_name = NULL;
06052 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06053 if (res) {
06054 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
06055 chan->name, strerror(errno));
06056 }
06057 len = 0;
06058 memset(exten, 0, sizeof(exten));
06059 timeout = firstdigittimeout;
06060 } else if (p->callreturn && !strcmp(exten, "*69")) {
06061 res = 0;
06062 if (!ast_strlen_zero(p->lastcid_num)) {
06063 res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
06064 }
06065 if (!res)
06066 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06067 break;
06068 } else if (!strcmp(exten, "*78")) {
06069
06070 if (option_verbose > 2)
06071 ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel);
06072 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
06073 "Channel: %s/%d\r\n"
06074 "Status: enabled\r\n", dahdi_chan_name, p->channel);
06075 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06076 p->dnd = 1;
06077 getforward = 0;
06078 memset(exten, 0, sizeof(exten));
06079 len = 0;
06080 } else if (!strcmp(exten, "*79")) {
06081
06082 if (option_verbose > 2)
06083 ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel);
06084 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
06085 "Channel: %s/%d\r\n"
06086 "Status: disabled\r\n", dahdi_chan_name, p->channel);
06087 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06088 p->dnd = 0;
06089 getforward = 0;
06090 memset(exten, 0, sizeof(exten));
06091 len = 0;
06092 } else if (p->cancallforward && !strcmp(exten, "*72")) {
06093 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06094 getforward = 1;
06095 memset(exten, 0, sizeof(exten));
06096 len = 0;
06097 } else if (p->cancallforward && !strcmp(exten, "*73")) {
06098 if (option_verbose > 2)
06099 ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel);
06100 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06101 memset(p->call_forward, 0, sizeof(p->call_forward));
06102 getforward = 0;
06103 memset(exten, 0, sizeof(exten));
06104 len = 0;
06105 } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
06106 p->subs[SUB_THREEWAY].owner &&
06107 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
06108
06109
06110 ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL);
06111 if (option_verbose > 2)
06112 ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
06113 break;
06114 } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
06115 if (option_verbose > 2)
06116 ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num);
06117 res = ast_db_put("blacklist", p->lastcid_num, "1");
06118 if (!res) {
06119 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06120 memset(exten, 0, sizeof(exten));
06121 len = 0;
06122 }
06123 } else if (p->hidecallerid && !strcmp(exten, "*82")) {
06124 if (option_verbose > 2)
06125 ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
06126
06127 p->hidecallerid = 0;
06128 if (chan->cid.cid_num)
06129 free(chan->cid.cid_num);
06130 chan->cid.cid_num = NULL;
06131 if (chan->cid.cid_name)
06132 free(chan->cid.cid_name);
06133 chan->cid.cid_name = NULL;
06134 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
06135 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_DIALRECALL);
06136 if (res) {
06137 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
06138 chan->name, strerror(errno));
06139 }
06140 len = 0;
06141 memset(exten, 0, sizeof(exten));
06142 timeout = firstdigittimeout;
06143 } else if (!strcmp(exten, "*0")) {
06144 struct ast_channel *nbridge =
06145 p->subs[SUB_THREEWAY].owner;
06146 struct dahdi_pvt *pbridge = NULL;
06147
06148 if (nbridge && ast_bridged_channel(nbridge))
06149 pbridge = ast_bridged_channel(nbridge)->tech_pvt;
06150 if (nbridge && pbridge &&
06151 (nbridge->tech == chan_tech) &&
06152 (ast_bridged_channel(nbridge)->tech == chan_tech) &&
06153 ISTRUNK(pbridge)) {
06154 int func = DAHDI_FLASH;
06155
06156 p->dop.dialstr[0] = '\0';
06157
06158 if ((ioctl(pbridge->subs[SUB_REAL].dfd,DAHDI_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
06159 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
06160 nbridge->name, strerror(errno));
06161 }
06162 swap_subs(p, SUB_REAL, SUB_THREEWAY);
06163 unalloc_sub(p, SUB_THREEWAY);
06164 p->owner = p->subs[SUB_REAL].owner;
06165 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
06166 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
06167 ast_hangup(chan);
06168 goto quit;
06169 } else {
06170 tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
06171 dahdi_wait_event(p->subs[index].dfd);
06172 tone_zone_play_tone(p->subs[index].dfd, -1);
06173 swap_subs(p, SUB_REAL, SUB_THREEWAY);
06174 unalloc_sub(p, SUB_THREEWAY);
06175 p->owner = p->subs[SUB_REAL].owner;
06176 ast_hangup(chan);
06177 goto quit;
06178 }
06179 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
06180 ((exten[0] != '*') || (strlen(exten) > 2))) {
06181 if (option_debug)
06182 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);
06183 break;
06184 }
06185 if (!timeout)
06186 timeout = gendigittimeout;
06187 if (len && !ast_ignore_pattern(chan->context, exten))
06188 tone_zone_play_tone(p->subs[index].dfd, -1);
06189 }
06190 break;
06191 case SIG_FXSLS:
06192 case SIG_FXSGS:
06193 case SIG_FXSKS:
06194 #ifdef HAVE_PRI
06195 if (p->pri) {
06196
06197 struct ast_frame *f;
06198 int res;
06199 time_t start;
06200
06201 time(&start);
06202 ast_setstate(chan, AST_STATE_RING);
06203 while (time(NULL) < start + 3) {
06204 res = ast_waitfor(chan, 1000);
06205 if (res) {
06206 f = ast_read(chan);
06207 if (!f) {
06208 ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
06209 ast_hangup(chan);
06210 goto quit;
06211 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
06212 res = 1;
06213 } else
06214 res = 0;
06215 ast_frfree(f);
06216 if (res) {
06217 ast_log(LOG_DEBUG, "Got ring!\n");
06218 res = 0;
06219 break;
06220 }
06221 }
06222 }
06223 }
06224 #endif
06225
06226 if (p->use_smdi && p->smdi_iface) {
06227 smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, SMDI_MD_WAIT_TIMEOUT);
06228
06229 if (smdi_msg != NULL) {
06230 ast_copy_string(chan->exten, smdi_msg->fwd_st, sizeof(chan->exten));
06231
06232 if (smdi_msg->type == 'B')
06233 pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
06234 else if (smdi_msg->type == 'N')
06235 pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
06236
06237 ast_log(LOG_DEBUG, "Recieved SMDI message on %s\n", chan->name);
06238 } else {
06239 ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
06240 }
06241 }
06242
06243 if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
06244 number = smdi_msg->calling_st;
06245
06246
06247
06248
06249 } else if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) {
06250
06251 if (p->cid_signalling == CID_SIG_DTMF) {
06252 int i = 0;
06253 cs = NULL;
06254 ast_log(LOG_DEBUG, "Receiving DTMF cid on "
06255 "channel %s\n", chan->name);
06256 dahdi_setlinear(p->subs[index].dfd, 0);
06257 res = 2000;
06258 for (;;) {
06259 struct ast_frame *f;
06260 res = ast_waitfor(chan, res);
06261 if (res <= 0) {
06262 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
06263 "Exiting simple switch\n");
06264 ast_hangup(chan);
06265 goto quit;
06266 }
06267 f = ast_read(chan);
06268 if (!f)
06269 break;
06270 if (f->frametype == AST_FRAME_DTMF) {
06271 dtmfbuf[i++] = f->subclass;
06272 ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
06273 res = 2000;
06274 }
06275 ast_frfree(f);
06276 if (chan->_state == AST_STATE_RING ||
06277 chan->_state == AST_STATE_RINGING)
06278 break;
06279 }
06280 dtmfbuf[i] = '\0';
06281 dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
06282
06283 ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf);
06284 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
06285 ast_log(LOG_DEBUG, "CID is '%s', flags %d\n",
06286 dtmfcid, flags);
06287
06288 if (!ast_strlen_zero(dtmfcid))
06289 number = dtmfcid;
06290 else
06291 number = NULL;
06292
06293 } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
06294 cs = callerid_new(p->cid_signalling);
06295 if (cs) {
06296 samples = 0;
06297 #if 1
06298 bump_gains(p);
06299 #endif
06300
06301 dahdi_setlinear(p->subs[index].dfd, 0);
06302
06303
06304 for (;;) {
06305 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
06306 if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) {
06307 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06308 callerid_free(cs);
06309 ast_hangup(chan);
06310 goto quit;
06311 }
06312 if (i & DAHDI_IOMUX_SIGEVENT) {
06313 res = dahdi_get_event(p->subs[index].dfd);
06314 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06315
06316 if (p->cid_signalling == CID_SIG_V23_JP) {
06317 #ifdef DAHDI_EVENT_RINGBEGIN
06318 if (res == DAHDI_EVENT_RINGBEGIN) {
06319 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
06320 usleep(1);
06321 }
06322 #endif
06323 } else {
06324 res = 0;
06325 break;
06326 }
06327 } else if (i & DAHDI_IOMUX_READ) {
06328 res = read(p->subs[index].dfd, buf, sizeof(buf));
06329 if (res < 0) {
06330 if (errno != ELAST) {
06331 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06332 callerid_free(cs);
06333 ast_hangup(chan);
06334 goto quit;
06335 }
06336 break;
06337 }
06338 samples += res;
06339
06340 if (p->cid_signalling == CID_SIG_V23_JP) {
06341 res = callerid_feed_jp(cs, buf, res, AST_LAW(p));
06342 } else {
06343 res = callerid_feed(cs, buf, res, AST_LAW(p));
06344 }
06345
06346 if (res < 0) {
06347 ast_log(LOG_WARNING, "CallerID feed failed on channel '%s'\n", chan->name);
06348 break;
06349 } else if (res)
06350 break;
06351 else if (samples > (8000 * 10))
06352 break;
06353 }
06354 }
06355 if (res == 1) {
06356 callerid_get(cs, &name, &number, &flags);
06357 ast_log(LOG_NOTICE, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06358 }
06359
06360 if (p->cid_signalling == CID_SIG_V23_JP) {
06361 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
06362 usleep(1);
06363 res = 4000;
06364 } else {
06365
06366
06367 res = 2000;
06368 }
06369
06370 for (;;) {
06371 struct ast_frame *f;
06372 res = ast_waitfor(chan, res);
06373 if (res <= 0) {
06374 ast_log(LOG_WARNING, "CID timed out waiting for ring. "
06375 "Exiting simple switch\n");
06376 ast_hangup(chan);
06377 goto quit;
06378 }
06379 if (!(f = ast_read(chan))) {
06380 ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
06381 ast_hangup(chan);
06382 goto quit;
06383 }
06384 ast_frfree(f);
06385 if (chan->_state == AST_STATE_RING ||
06386 chan->_state == AST_STATE_RINGING)
06387 break;
06388 }
06389
06390
06391
06392 if (p->usedistinctiveringdetection == 1) {
06393 len = 0;
06394 distMatches = 0;
06395
06396 for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
06397 curRingData[receivedRingT] = 0;
06398 receivedRingT = 0;
06399 counter = 0;
06400 counter1 = 0;
06401
06402 if (strcmp(p->context,p->defcontext) != 0) {
06403 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06404 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06405 }
06406
06407 for (;;) {
06408 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
06409 if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) {
06410 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06411 callerid_free(cs);
06412 ast_hangup(chan);
06413 goto quit;
06414 }
06415 if (i & DAHDI_IOMUX_SIGEVENT) {
06416 res = dahdi_get_event(p->subs[index].dfd);
06417 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06418 res = 0;
06419
06420
06421 curRingData[receivedRingT] = p->ringt;
06422
06423 if (p->ringt < p->ringt_base/2)
06424 break;
06425
06426
06427 if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06428 break;
06429 } else if (i & DAHDI_IOMUX_READ) {
06430 res = read(p->subs[index].dfd, buf, sizeof(buf));
06431 if (res < 0) {
06432 if (errno != ELAST) {
06433 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06434 callerid_free(cs);
06435 ast_hangup(chan);
06436 goto quit;
06437 }
06438 break;
06439 }
06440 if (p->ringt)
06441 p->ringt--;
06442 if (p->ringt == 1) {
06443 res = -1;
06444 break;
06445 }
06446 }
06447 }
06448 if (option_verbose > 2)
06449
06450 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06451
06452 for (counter = 0; counter < 3; counter++) {
06453
06454
06455 distMatches = 0;
06456 for (counter1 = 0; counter1 < 3; counter1++) {
06457 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06458 (p->drings.ringnum[counter].ring[counter1]-10)) {
06459 distMatches++;
06460 }
06461 }
06462 if (distMatches == 3) {
06463
06464 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06465 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06466 if (option_verbose > 2)
06467 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06468 break;
06469 }
06470 }
06471 }
06472
06473 dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
06474 #if 1
06475 restore_gains(p);
06476 #endif
06477 } else
06478 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06479 } else {
06480 ast_log(LOG_WARNING, "Channel %s in prering "
06481 "state, but I have nothing to do. "
06482 "Terminating simple switch, should be "
06483 "restarted by the actual ring.\n",
06484 chan->name);
06485 ast_hangup(chan);
06486 goto quit;
06487 }
06488 } else if (p->use_callerid && p->cid_start == CID_START_RING) {
06489
06490 cs = callerid_new(p->cid_signalling);
06491 if (cs) {
06492 #if 1
06493 bump_gains(p);
06494 #endif
06495 samples = 0;
06496 len = 0;
06497 distMatches = 0;
06498
06499 for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
06500 curRingData[receivedRingT] = 0;
06501 receivedRingT = 0;
06502 counter = 0;
06503 counter1 = 0;
06504
06505 if (strcmp(p->context,p->defcontext) != 0) {
06506 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06507 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06508 }
06509
06510
06511 dahdi_setlinear(p->subs[index].dfd, 0);
06512 for (;;) {
06513 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
06514 if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) {
06515 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06516 callerid_free(cs);
06517 ast_hangup(chan);
06518 goto quit;
06519 }
06520 if (i & DAHDI_IOMUX_SIGEVENT) {
06521 res = dahdi_get_event(p->subs[index].dfd);
06522 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06523
06524 if ( res == DAHDI_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) {
06525 ast_log(LOG_DEBUG, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel);
06526 p->polarity = POLARITY_IDLE;
06527 callerid_free(cs);
06528 ast_hangup(chan);
06529 goto quit;
06530 }
06531 res = 0;
06532
06533
06534 curRingData[receivedRingT] = p->ringt;
06535
06536 if (p->ringt < p->ringt_base/2)
06537 break;
06538
06539
06540 if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06541 break;
06542 } else if (i & DAHDI_IOMUX_READ) {
06543 res = read(p->subs[index].dfd, buf, sizeof(buf));
06544 if (res < 0) {
06545 if (errno != ELAST) {
06546 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06547 callerid_free(cs);
06548 ast_hangup(chan);
06549 goto quit;
06550 }
06551 break;
06552 }
06553 if (p->ringt)
06554 p->ringt--;
06555 if (p->ringt == 1) {
06556 res = -1;
06557 break;
06558 }
06559 samples += res;
06560 res = callerid_feed(cs, buf, res, AST_LAW(p));
06561 if (res < 0) {
06562 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06563 break;
06564 } else if (res)
06565 break;
06566 else if (samples > (8000 * 10))
06567 break;
06568 }
06569 }
06570 if (res == 1) {
06571 callerid_get(cs, &name, &number, &flags);
06572 if (option_debug)
06573 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06574 }
06575 if (distinctiveringaftercid == 1) {
06576
06577 for (receivedRingT = 0; receivedRingT < 3; receivedRingT++) {
06578 curRingData[receivedRingT] = 0;
06579 }
06580 receivedRingT = 0;
06581 if (option_verbose > 2)
06582 ast_verbose( VERBOSE_PREFIX_3 "Detecting post-CID distinctive ring\n");
06583 for (;;) {
06584 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
06585 if ((res = ioctl(p->subs[index].dfd, DAHDI_IOMUX, &i))) {
06586 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06587 callerid_free(cs);
06588 ast_hangup(chan);
06589 goto quit;
06590 }
06591 if (i & DAHDI_IOMUX_SIGEVENT) {
06592 res = dahdi_get_event(p->subs[index].dfd);
06593 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06594 res = 0;
06595
06596
06597 curRingData[receivedRingT] = p->ringt;
06598
06599 if (p->ringt < p->ringt_base/2)
06600 break;
06601
06602
06603 if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06604 break;
06605 } else if (i & DAHDI_IOMUX_READ) {
06606 res = read(p->subs[index].dfd, buf, sizeof(buf));
06607 if (res < 0) {
06608 if (errno != ELAST) {
06609 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06610 callerid_free(cs);
06611 ast_hangup(chan);
06612 goto quit;
06613 }
06614 break;
06615 }
06616 if (p->ringt)
06617 p->ringt--;
06618 if (p->ringt == 1) {
06619 res = -1;
06620 break;
06621 }
06622 }
06623 }
06624 }
06625 if (p->usedistinctiveringdetection == 1) {
06626 if (option_verbose > 2)
06627
06628 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06629
06630 for (counter = 0; counter < 3; counter++) {
06631
06632
06633 if (option_verbose > 2)
06634
06635 ast_verbose( VERBOSE_PREFIX_3 "Checking %d,%d,%d\n",
06636 p->drings.ringnum[counter].ring[0],
06637 p->drings.ringnum[counter].ring[1],
06638 p->drings.ringnum[counter].ring[2]);
06639 distMatches = 0;
06640 for (counter1 = 0; counter1 < 3; counter1++) {
06641 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06642 (p->drings.ringnum[counter].ring[counter1]-10)) {
06643 distMatches++;
06644 }
06645 }
06646 if (distMatches == 3) {
06647
06648 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06649 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06650 if (option_verbose > 2)
06651 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06652 break;
06653 }
06654 }
06655 }
06656
06657 dahdi_setlinear(p->subs[index].dfd, p->subs[index].linear);
06658 #if 1
06659 restore_gains(p);
06660 #endif
06661 if (res < 0) {
06662 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06663 }
06664 } else
06665 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06666 }
06667 else
06668 cs = NULL;
06669
06670 if (number)
06671 ast_shrink_phone_number(number);
06672 ast_set_callerid(chan, number, name, number);
06673
06674 if (smdi_msg)
06675 ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy);
06676
06677 if (cs)
06678 callerid_free(cs);
06679
06680 ast_setstate(chan, AST_STATE_RING);
06681 chan->rings = 1;
06682 p->ringt = p->ringt_base;
06683 res = ast_pbx_run(chan);
06684 if (res) {
06685 ast_hangup(chan);
06686 ast_log(LOG_WARNING, "PBX exited non-zero\n");
06687 }
06688 goto quit;
06689 default:
06690 ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
06691 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
06692 if (res < 0)
06693 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06694 }
06695 res = tone_zone_play_tone(p->subs[index].dfd, DAHDI_TONE_CONGESTION);
06696 if (res < 0)
06697 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06698 ast_hangup(chan);
06699 quit:
06700 ast_mutex_lock(&ss_thread_lock);
06701 ss_thread_count--;
06702 ast_cond_signal(&ss_thread_complete);
06703 ast_mutex_unlock(&ss_thread_lock);
06704 return NULL;
06705 }
06706
06707
06708 static int dahdi_destroy_channel_bynum(int channel)
06709 {
06710 struct dahdi_pvt *tmp = NULL;
06711 struct dahdi_pvt *prev = NULL;
06712
06713 tmp = iflist;
06714 while (tmp) {
06715 if (tmp->channel == channel) {
06716 int x = DAHDI_FLASH;
06717 ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
06718 destroy_channel(prev, tmp, 1);
06719 ast_module_unref(ast_module_info->self);
06720 return RESULT_SUCCESS;
06721 }
06722 prev = tmp;
06723 tmp = tmp->next;
06724 }
06725 return RESULT_FAILURE;
06726 }
06727
06728 static int handle_init_event(struct dahdi_pvt *i, int event)
06729 {
06730 int res;
06731 pthread_t threadid;
06732 pthread_attr_t attr;
06733 struct ast_channel *chan;
06734 pthread_attr_init(&attr);
06735 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06736
06737 switch (event) {
06738 case DAHDI_EVENT_NONE:
06739 case DAHDI_EVENT_BITSCHANGED:
06740 break;
06741 case DAHDI_EVENT_WINKFLASH:
06742 case DAHDI_EVENT_RINGOFFHOOK:
06743 if (i->inalarm) break;
06744 if (i->radio) break;
06745
06746 switch (i->sig) {
06747 case SIG_FXOLS:
06748 case SIG_FXOGS:
06749 case SIG_FXOKS:
06750 res = dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
06751 if (res && (errno == EBUSY))
06752 break;
06753 if (i->cidspill) {
06754
06755 free(i->cidspill);
06756 i->cidspill = NULL;
06757 }
06758 if (i->immediate) {
06759 dahdi_enable_ec(i);
06760
06761 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
06762 chan = dahdi_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
06763 if (!chan) {
06764 ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
06765 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
06766 if (res < 0)
06767 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06768 }
06769 } else {
06770
06771 chan = dahdi_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
06772 if (chan) {
06773 if (has_voicemail(i))
06774 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_STUTTER);
06775 else
06776 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_DIALTONE);
06777 if (res < 0)
06778 ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel);
06779 if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06780 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06781 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
06782 if (res < 0)
06783 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06784 ast_hangup(chan);
06785 }
06786 } else
06787 ast_log(LOG_WARNING, "Unable to create channel\n");
06788 }
06789 break;
06790 case SIG_FXSLS:
06791 case SIG_FXSGS:
06792 case SIG_FXSKS:
06793 i->ringt = i->ringt_base;
06794
06795 case SIG_EMWINK:
06796 case SIG_FEATD:
06797 case SIG_FEATDMF:
06798 case SIG_FEATDMF_TA:
06799 case SIG_E911:
06800 case SIG_FGC_CAMA:
06801 case SIG_FGC_CAMAMF:
06802 case SIG_FEATB:
06803 case SIG_EM:
06804 case SIG_EM_E1:
06805 case SIG_SFWINK:
06806 case SIG_SF_FEATD:
06807 case SIG_SF_FEATDMF:
06808 case SIG_SF_FEATB:
06809 case SIG_SF:
06810
06811 chan = dahdi_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06812 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06813 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06814 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
06815 if (res < 0)
06816 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06817 ast_hangup(chan);
06818 } else if (!chan) {
06819 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
06820 }
06821 break;
06822 default:
06823 ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06824 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
06825 if (res < 0)
06826 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06827 return -1;
06828 }
06829 break;
06830 case DAHDI_EVENT_NOALARM:
06831 i->inalarm = 0;
06832 if (!i->unknown_alarm) {
06833 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
06834 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
06835 "Channel: %d\r\n", i->channel);
06836 } else {
06837 i->unknown_alarm = 0;
06838 }
06839 break;
06840 case DAHDI_EVENT_ALARM:
06841 i->inalarm = 1;
06842 res = get_alarms(i);
06843 handle_alarms(i, res);
06844
06845 case DAHDI_EVENT_ONHOOK:
06846 if (i->radio)
06847 break;
06848
06849 switch (i->sig) {
06850 case SIG_FXOLS:
06851 case SIG_FXOGS:
06852 case SIG_FEATD:
06853 case SIG_FEATDMF:
06854 case SIG_FEATDMF_TA:
06855 case SIG_E911:
06856 case SIG_FGC_CAMA:
06857 case SIG_FGC_CAMAMF:
06858 case SIG_FEATB:
06859 case SIG_EM:
06860 case SIG_EM_E1:
06861 case SIG_EMWINK:
06862 case SIG_SF_FEATD:
06863 case SIG_SF_FEATDMF:
06864 case SIG_SF_FEATB:
06865 case SIG_SF:
06866 case SIG_SFWINK:
06867 case SIG_FXSLS:
06868 case SIG_FXSGS:
06869 case SIG_FXSKS:
06870 case SIG_GR303FXSKS:
06871 dahdi_disable_ec(i);
06872 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
06873 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
06874 break;
06875 case SIG_GR303FXOKS:
06876 case SIG_FXOKS:
06877 dahdi_disable_ec(i);
06878
06879 #ifdef ZHONE_HACK
06880 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
06881 usleep(1);
06882 #endif
06883 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
06884 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
06885 break;
06886 case SIG_PRI:
06887 dahdi_disable_ec(i);
06888 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
06889 break;
06890 default:
06891 ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06892 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
06893 return -1;
06894 }
06895 break;
06896 case DAHDI_EVENT_POLARITY:
06897 switch (i->sig) {
06898 case SIG_FXSLS:
06899 case SIG_FXSKS:
06900 case SIG_FXSGS:
06901
06902
06903
06904
06905 if (i->hanguponpolarityswitch)
06906 i->polarity = POLARITY_REV;
06907
06908 if (i->cid_start == CID_START_POLARITY) {
06909 i->polarity = POLARITY_REV;
06910 ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
06911 "CID detection on channel %d\n",
06912 i->channel);
06913 chan = dahdi_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
06914 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06915 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06916 }
06917 }
06918 break;
06919 default:
06920 ast_log(LOG_WARNING, "handle_init_event detected "
06921 "polarity reversal on non-FXO (SIG_FXS) "
06922 "interface %d\n", i->channel);
06923 }
06924 break;
06925 case DAHDI_EVENT_REMOVED:
06926 ast_log(LOG_NOTICE,
06927 "Got DAHDI_EVENT_REMOVED. Destroying channel %d\n",
06928 i->channel);
06929 dahdi_destroy_channel_bynum(i->channel);
06930 break;
06931 }
06932 pthread_attr_destroy(&attr);
06933 return 0;
06934 }
06935
06936 static void *do_monitor(void *data)
06937 {
06938 int count, res, res2, spoint, pollres=0;
06939 struct dahdi_pvt *i;
06940 struct dahdi_pvt *last = NULL;
06941 time_t thispass = 0, lastpass = 0;
06942 int found;
06943 char buf[1024];
06944 struct pollfd *pfds=NULL;
06945 int lastalloc = -1;
06946
06947
06948
06949 #if 0
06950 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
06951 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
06952 return NULL;
06953 }
06954 ast_log(LOG_DEBUG, "Monitor starting...\n");
06955 #endif
06956 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
06957
06958 for (;;) {
06959
06960 ast_mutex_lock(&iflock);
06961 if (!pfds || (lastalloc != ifcount)) {
06962 if (pfds) {
06963 free(pfds);
06964 pfds = NULL;
06965 }
06966 if (ifcount) {
06967 if (!(pfds = ast_calloc(1, ifcount * sizeof(*pfds)))) {
06968 ast_mutex_unlock(&iflock);
06969 return NULL;
06970 }
06971 }
06972 lastalloc = ifcount;
06973 }
06974
06975
06976 count = 0;
06977 i = iflist;
06978 while (i) {
06979 if ((i->subs[SUB_REAL].dfd > -1) && i->sig && (!i->radio)) {
06980 if (!i->owner && !i->subs[SUB_REAL].owner) {
06981
06982 pfds[count].fd = i->subs[SUB_REAL].dfd;
06983 pfds[count].events = POLLPRI;
06984 pfds[count].revents = 0;
06985
06986 if (i->cidspill)
06987 pfds[count].events |= POLLIN;
06988 count++;
06989 }
06990 }
06991 i = i->next;
06992 }
06993
06994 ast_mutex_unlock(&iflock);
06995
06996 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
06997 pthread_testcancel();
06998
06999 res = poll(pfds, count, 1000);
07000 pthread_testcancel();
07001 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
07002
07003
07004 if (res < 0) {
07005 if ((errno != EAGAIN) && (errno != EINTR))
07006 ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
07007 continue;
07008 }
07009
07010
07011 ast_mutex_lock(&iflock);
07012 found = 0;
07013 spoint = 0;
07014 lastpass = thispass;
07015 thispass = time(NULL);
07016 i = iflist;
07017 while (i) {
07018 if (thispass != lastpass) {
07019 if (!found && ((i == last) || ((i == iflist) && !last))) {
07020 last = i;
07021 if (last) {
07022 if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) &&
07023 (last->sig & __DAHDI_SIG_FXO)) {
07024 res = ast_app_has_voicemail(last->mailbox, NULL);
07025 if (last->msgstate != res) {
07026 int x;
07027 ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel);
07028 x = DAHDI_FLUSH_BOTH;
07029 res2 = ioctl(last->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
07030 if (res2)
07031 ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", last->channel, strerror(errno));
07032 if ((last->cidspill = ast_calloc(1, MAX_CALLERID_SIZE))) {
07033
07034 x = 4000;
07035 ioctl(last->subs[SUB_REAL].dfd, DAHDI_ONHOOKTRANSFER, &x);
07036 last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last));
07037 last->cidpos = 0;
07038 last->msgstate = res;
07039 last->onhooktime = thispass;
07040 }
07041 found ++;
07042 }
07043 }
07044 last = last->next;
07045 }
07046 }
07047 }
07048 if ((i->subs[SUB_REAL].dfd > -1) && i->sig) {
07049 if (i->radio && !i->owner)
07050 {
07051 res = dahdi_get_event(i->subs[SUB_REAL].dfd);
07052 if (res)
07053 {
07054 if (option_debug)
07055 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
07056
07057 ast_mutex_unlock(&iflock);
07058 handle_init_event(i, res);
07059 ast_mutex_lock(&iflock);
07060 }
07061 i = i->next;
07062 continue;
07063 }
07064 pollres = ast_fdisset(pfds, i->subs[SUB_REAL].dfd, count, &spoint);
07065 if (pollres & POLLIN) {
07066 if (i->owner || i->subs[SUB_REAL].owner) {
07067 #ifdef HAVE_PRI
07068 if (!i->pri)
07069 #endif
07070 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].dfd);
07071 i = i->next;
07072 continue;
07073 }
07074 if (!i->cidspill) {
07075 ast_log(LOG_WARNING, "Whoa.... I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].dfd);
07076 i = i->next;
07077 continue;
07078 }
07079 res = read(i->subs[SUB_REAL].dfd, buf, sizeof(buf));
07080 if (res > 0) {
07081
07082 if (res > i->cidlen - i->cidpos)
07083 res = i->cidlen - i->cidpos;
07084 res2 = write(i->subs[SUB_REAL].dfd, i->cidspill + i->cidpos, res);
07085 if (res2 > 0) {
07086 i->cidpos += res2;
07087 if (i->cidpos >= i->cidlen) {
07088 free(i->cidspill);
07089 i->cidspill = 0;
07090 i->cidpos = 0;
07091 i->cidlen = 0;
07092 }
07093 } else {
07094 ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
07095 i->msgstate = -1;
07096 }
07097 } else {
07098 ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
07099 }
07100 }
07101 if (pollres & POLLPRI) {
07102 if (i->owner || i->subs[SUB_REAL].owner) {
07103 #ifdef HAVE_PRI
07104 if (!i->pri)
07105 #endif
07106 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].dfd);
07107 i = i->next;
07108 continue;
07109 }
07110 res = dahdi_get_event(i->subs[SUB_REAL].dfd);
07111 if (option_debug)
07112 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
07113
07114 ast_mutex_unlock(&iflock);
07115 handle_init_event(i, res);
07116 ast_mutex_lock(&iflock);
07117 }
07118 }
07119 i=i->next;
07120 }
07121 ast_mutex_unlock(&iflock);
07122 }
07123
07124 return NULL;
07125
07126 }
07127
07128 static int restart_monitor(void)
07129 {
07130 pthread_attr_t attr;
07131 pthread_attr_init(&attr);
07132 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
07133
07134 if (monitor_thread == AST_PTHREADT_STOP)
07135 return 0;
07136 ast_mutex_lock(&monlock);
07137 if (monitor_thread == pthread_self()) {
07138 ast_mutex_unlock(&monlock);
07139 ast_log(LOG_WARNING, "Cannot kill myself\n");
07140 return -1;
07141 }
07142 if (monitor_thread != AST_PTHREADT_NULL) {
07143
07144 pthread_kill(monitor_thread, SIGURG);
07145 } else {
07146
07147 if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) {
07148 ast_mutex_unlock(&monlock);
07149 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
07150 pthread_attr_destroy(&attr);
07151 return -1;
07152 }
07153 }
07154 ast_mutex_unlock(&monlock);
07155 pthread_attr_destroy(&attr);
07156 return 0;
07157 }
07158
07159 #ifdef HAVE_PRI
07160 static int pri_resolve_span(int *span, int channel, int offset, struct dahdi_spaninfo *si)
07161 {
07162 int x;
07163 int trunkgroup;
07164
07165 trunkgroup = pris[*span].mastertrunkgroup;
07166 if (trunkgroup) {
07167
07168 for (x = 0; x < NUM_SPANS; x++) {
07169 if (pris[x].trunkgroup == trunkgroup) {
07170 *span = x;
07171 return 0;
07172 }
07173 }
07174 ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup);
07175 *span = -1;
07176 } else {
07177 if (pris[*span].trunkgroup) {
07178 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup);
07179 *span = -1;
07180 } else if (pris[*span].mastertrunkgroup) {
07181 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup);
07182 *span = -1;
07183 } else {
07184 if (si->totalchans == 31) {
07185
07186 pris[*span].dchannels[0] = 16 + offset;
07187 } else if (si->totalchans == 24) {
07188
07189 pris[*span].dchannels[0] = 24 + offset;
07190 } else if (si->totalchans == 3) {
07191
07192 pris[*span].dchannels[0] = 3 + offset;
07193 } else {
07194 ast_log(LOG_WARNING, "Unable to use span %d, since the D-channel cannot be located (unexpected span size of %d channels)\n", *span, si->totalchans);
07195 *span = -1;
07196 return 0;
07197 }
07198 pris[*span].dchanavail[0] |= DCHAN_PROVISIONED;
07199 pris[*span].offset = offset;
07200 pris[*span].span = *span + 1;
07201 }
07202 }
07203 return 0;
07204 }
07205
07206 static int pri_create_trunkgroup(int trunkgroup, int *channels)
07207 {
07208 struct dahdi_spaninfo si;
07209 struct dahdi_params p;
07210 int fd;
07211 int span;
07212 int ospan=0;
07213 int x,y;
07214 for (x = 0; x < NUM_SPANS; x++) {
07215 if (pris[x].trunkgroup == trunkgroup) {
07216 ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]);
07217 return -1;
07218 }
07219 }
07220 for (y = 0; y < NUM_DCHANS; y++) {
07221 if (!channels[y])
07222 break;
07223 memset(&si, 0, sizeof(si));
07224 memset(&p, 0, sizeof(p));
07225 #ifdef HAVE_ZAPTEL
07226 fd = open("/dev/zap/channel", O_RDWR);
07227 #else
07228 fd = open("/dev/dahdi/channel", O_RDWR);
07229 #endif
07230 if (fd < 0) {
07231 ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno));
07232 return -1;
07233 }
07234 x = channels[y];
07235 if (ioctl(fd, DAHDI_SPECIFY, &x)) {
07236 ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
07237 close(fd);
07238 return -1;
07239 }
07240 if (ioctl(fd, DAHDI_GET_PARAMS, &p)) {
07241 ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno));
07242 return -1;
07243 }
07244 if (ioctl(fd, DAHDI_SPANSTAT, &si)) {
07245 ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d): %s\n", channels[y], p.spanno, strerror(errno));
07246 close(fd);
07247 return -1;
07248 }
07249 span = p.spanno - 1;
07250 if (pris[span].trunkgroup) {
07251 ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
07252 close(fd);
07253 return -1;
07254 }
07255 if (pris[span].pvts[0]) {
07256 ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
07257 close(fd);
07258 return -1;
07259 }
07260 if (!y) {
07261 pris[span].trunkgroup = trunkgroup;
07262 pris[span].offset = channels[y] - p.chanpos;
07263 ospan = span;
07264 }
07265 pris[ospan].dchannels[y] = channels[y];
07266 pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
07267 pris[span].span = span + 1;
07268 close(fd);
07269 }
07270 return 0;
07271 }
07272
07273 static int pri_create_spanmap(int span, int trunkgroup, int logicalspan)
07274 {
07275 if (pris[span].mastertrunkgroup) {
07276 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);
07277 return -1;
07278 }
07279 pris[span].mastertrunkgroup = trunkgroup;
07280 pris[span].prilogicalspan = logicalspan;
07281 return 0;
07282 }
07283
07284 #endif
07285
07286 static struct dahdi_pvt *mkintf(int channel, const struct dahdi_chan_conf *conf, struct dahdi_pri *pri, int reloading)
07287 {
07288
07289 struct dahdi_pvt *tmp = NULL, *tmp2, *prev = NULL;
07290 char fn[80];
07291 #if 1
07292 struct dahdi_bufferinfo bi;
07293 #endif
07294 int res;
07295 int span=0;
07296 int here = 0;
07297 int x;
07298 struct dahdi_pvt **wlist;
07299 struct dahdi_pvt **wend;
07300 struct dahdi_params p;
07301
07302 wlist = &iflist;
07303 wend = &ifend;
07304
07305 #ifdef HAVE_PRI
07306 if (pri) {
07307 wlist = &pri->crvs;
07308 wend = &pri->crvend;
07309 }
07310 #endif
07311
07312 tmp2 = *wlist;
07313 prev = NULL;
07314
07315 while (tmp2) {
07316 if (!tmp2->destroy) {
07317 if (tmp2->channel == channel) {
07318 tmp = tmp2;
07319 here = 1;
07320 break;
07321 }
07322 if (tmp2->channel > channel) {
07323 break;
07324 }
07325 }
07326 prev = tmp2;
07327 tmp2 = tmp2->next;
07328 }
07329
07330 if (!here && reloading != 1) {
07331 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
07332 if (tmp)
07333 free(tmp);
07334 return NULL;
07335 }
07336 ast_mutex_init(&tmp->lock);
07337 ifcount++;
07338 for (x = 0; x < 3; x++)
07339 tmp->subs[x].dfd = -1;
07340 tmp->channel = channel;
07341 }
07342
07343 if (tmp) {
07344 int chan_sig = conf->chan.sig;
07345 if (!here) {
07346 if ((channel != CHAN_PSEUDO) && !pri) {
07347 int count = 0;
07348 snprintf(fn, sizeof(fn), "%d", channel);
07349
07350 tmp->subs[SUB_REAL].dfd = dahdi_open(fn);
07351 while (tmp->subs[SUB_REAL].dfd < 0 && reloading == 2 && count < 1000) {
07352 usleep(1);
07353 tmp->subs[SUB_REAL].dfd = dahdi_open(fn);
07354 count++;
07355 }
07356
07357 if (tmp->subs[SUB_REAL].dfd < 0) {
07358 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);
07359 destroy_dahdi_pvt(&tmp);
07360 return NULL;
07361 }
07362 memset(&p, 0, sizeof(p));
07363 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p);
07364 if (res < 0) {
07365 ast_log(LOG_ERROR, "Unable to get parameters: %s\n", strerror(errno));
07366 destroy_dahdi_pvt(&tmp);
07367 return NULL;
07368 }
07369 if (p.sigtype != (conf->chan.sig & 0x3ffff)) {
07370 ast_log(LOG_ERROR, "Signalling requested on channel %d is %s but line is in %s signalling\n", channel, sig2str(conf->chan.sig), sig2str(p.sigtype));
07371 destroy_dahdi_pvt(&tmp);
07372 return NULL;
07373 }
07374 tmp->law = p.curlaw;
07375 tmp->span = p.spanno;
07376 span = p.spanno - 1;
07377 } else {
07378 if (channel == CHAN_PSEUDO)
07379 chan_sig = 0;
07380 else if ((chan_sig != SIG_FXOKS) && (chan_sig != SIG_FXSKS)) {
07381 ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n");
07382 return NULL;
07383 }
07384 }
07385 #ifdef HAVE_PRI
07386 if ((chan_sig == SIG_PRI) || (chan_sig == SIG_GR303FXOKS) || (chan_sig == SIG_GR303FXSKS)) {
07387 int offset;
07388 int myswitchtype;
07389 int matchesdchan;
07390 int x,y;
07391 offset = 0;
07392 if ((chan_sig == SIG_PRI) && ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &offset)) {
07393 ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
07394 destroy_dahdi_pvt(&tmp);
07395 return NULL;
07396 }
07397 if (span >= NUM_SPANS) {
07398 ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span);
07399 destroy_dahdi_pvt(&tmp);
07400 return NULL;
07401 } else {
07402 struct dahdi_spaninfo si;
07403 si.spanno = 0;
07404 if (ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SPANSTAT,&si) == -1) {
07405 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
07406 destroy_dahdi_pvt(&tmp);
07407 return NULL;
07408 }
07409
07410 tmp->logicalspan = pris[span].prilogicalspan;
07411 pri_resolve_span(&span, channel, (channel - p.chanpos), &si);
07412 if (span < 0) {
07413 ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel);
07414 destroy_dahdi_pvt(&tmp);
07415 return NULL;
07416 }
07417 if (chan_sig == SIG_PRI)
07418 myswitchtype = conf->pri.switchtype;
07419 else
07420 myswitchtype = PRI_SWITCH_GR303_TMC;
07421
07422 matchesdchan=0;
07423 for (x = 0; x < NUM_SPANS; x++) {
07424 for (y = 0; y < NUM_DCHANS; y++) {
07425 if (pris[x].dchannels[y] == tmp->channel) {
07426 matchesdchan = 1;
07427 break;
07428 }
07429 }
07430 }
07431 offset = p.chanpos;
07432 if (!matchesdchan) {
07433 if (pris[span].nodetype && (pris[span].nodetype != conf->pri.nodetype)) {
07434 ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype));
07435 destroy_dahdi_pvt(&tmp);
07436 return NULL;
07437 }
07438 if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) {
07439 ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype));
07440 destroy_dahdi_pvt(&tmp);
07441 return NULL;
07442 }
07443 if ((pris[span].dialplan) && (pris[span].dialplan != conf->pri.dialplan)) {
07444 ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan));
07445 destroy_dahdi_pvt(&tmp);
07446 return NULL;
07447 }
07448 if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, conf->pri.idledial)) {
07449 ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, conf->pri.idledial);
07450 destroy_dahdi_pvt(&tmp);
07451 return NULL;
07452 }
07453 if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, conf->pri.idleext)) {
07454 ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, conf->pri.idleext);
07455 destroy_dahdi_pvt(&tmp);
07456 return NULL;
07457 }
07458 if (pris[span].minunused && (pris[span].minunused != conf->pri.minunused)) {
07459 ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, conf->pri.minunused);
07460 destroy_dahdi_pvt(&tmp);
07461 return NULL;
07462 }
07463 if (pris[span].minidle && (pris[span].minidle != conf->pri.minidle)) {
07464 ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, conf->pri.minidle);
07465 destroy_dahdi_pvt(&tmp);
07466 return NULL;
07467 }
07468 if (pris[span].numchans >= MAX_CHANNELS) {
07469 ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel,
07470 pris[span].trunkgroup);
07471 destroy_dahdi_pvt(&tmp);
07472 return NULL;
07473 }
07474 pris[span].nodetype = conf->pri.nodetype;
07475 pris[span].switchtype = myswitchtype;
07476 pris[span].nsf = conf->pri.nsf;
07477 pris[span].dialplan = conf->pri.dialplan;
07478 pris[span].localdialplan = conf->pri.localdialplan;
07479 pris[span].pvts[pris[span].numchans++] = tmp;
07480 pris[span].minunused = conf->pri.minunused;
07481 pris[span].minidle = conf->pri.minidle;
07482 pris[span].overlapdial = conf->pri.overlapdial;
07483 #ifdef HAVE_PRI_INBANDDISCONNECT
07484 pris[span].inbanddisconnect = conf->pri.inbanddisconnect;
07485 #endif
07486 pris[span].facilityenable = conf->pri.facilityenable;
07487 ast_copy_string(pris[span].idledial, conf->pri.idledial, sizeof(pris[span].idledial));
07488 ast_copy_string(pris[span].idleext, conf->pri.idleext, sizeof(pris[span].idleext));
07489 ast_copy_string(pris[span].internationalprefix, conf->pri.internationalprefix, sizeof(pris[span].internationalprefix));
07490 ast_copy_string(pris[span].nationalprefix, conf->pri.nationalprefix, sizeof(pris[span].nationalprefix));
07491 ast_copy_string(pris[span].localprefix, conf->pri.localprefix, sizeof(pris[span].localprefix));
07492 ast_copy_string(pris[span].privateprefix, conf->pri.privateprefix, sizeof(pris[span].privateprefix));
07493 ast_copy_string(pris[span].unknownprefix, conf->pri.unknownprefix, sizeof(pris[span].unknownprefix));
07494 pris[span].resetinterval = conf->pri.resetinterval;
07495
07496 tmp->pri = &pris[span];
07497 tmp->prioffset = offset;
07498 tmp->call = NULL;
07499 } else {
07500 ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset);
07501 destroy_dahdi_pvt(&tmp);
07502 return NULL;
07503 }
07504 }
07505 } else {
07506 tmp->prioffset = 0;
07507 }
07508 #endif
07509 } else {
07510 chan_sig = tmp->sig;
07511 memset(&p, 0, sizeof(p));
07512 if (tmp->subs[SUB_REAL].dfd > -1)
07513 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p);
07514 }
07515
07516 switch (chan_sig) {
07517 case SIG_FXSKS:
07518 case SIG_FXSLS:
07519 case SIG_EM:
07520 case SIG_EM_E1:
07521 case SIG_EMWINK:
07522 case SIG_FEATD:
07523 case SIG_FEATDMF:
07524 case SIG_FEATDMF_TA:
07525 case SIG_FEATB:
07526 case SIG_E911:
07527 case SIG_SF:
07528 case SIG_SFWINK:
07529 case SIG_FGC_CAMA:
07530 case SIG_FGC_CAMAMF:
07531 case SIG_SF_FEATD:
07532 case SIG_SF_FEATDMF:
07533 case SIG_SF_FEATB:
07534 p.starttime = 250;
07535 break;
07536 }
07537
07538 if (tmp->radio) {
07539
07540 p.channo = channel;
07541 p.rxwinktime = 1;
07542 p.rxflashtime = 1;
07543 p.starttime = 1;
07544 p.debouncetime = 5;
07545 }
07546 if (!tmp->radio) {
07547 p.channo = channel;
07548
07549 if (conf->timing.prewinktime >= 0)
07550 p.prewinktime = conf->timing.prewinktime;
07551 if (conf->timing.preflashtime >= 0)
07552 p.preflashtime = conf->timing.preflashtime;
07553 if (conf->timing.winktime >= 0)
07554 p.winktime = conf->timing.winktime;
07555 if (conf->timing.flashtime >= 0)
07556 p.flashtime = conf->timing.flashtime;
07557 if (conf->timing.starttime >= 0)
07558 p.starttime = conf->timing.starttime;
07559 if (conf->timing.rxwinktime >= 0)
07560 p.rxwinktime = conf->timing.rxwinktime;
07561 if (conf->timing.rxflashtime >= 0)
07562 p.rxflashtime = conf->timing.rxflashtime;
07563 if (conf->timing.debouncetime >= 0)
07564 p.debouncetime = conf->timing.debouncetime;
07565 }
07566
07567
07568 if (tmp->subs[SUB_REAL].dfd >= 0)
07569 {
07570 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_PARAMS, &p);
07571 if (res < 0) {
07572 ast_log(LOG_ERROR, "Unable to set parameters: %s\n", strerror(errno));
07573 destroy_dahdi_pvt(&tmp);
07574 return NULL;
07575 }
07576 }
07577 #if 1
07578 if (!here && (tmp->subs[SUB_REAL].dfd > -1)) {
07579 memset(&bi, 0, sizeof(bi));
07580 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi);
07581 if (!res) {
07582 bi.txbufpolicy = conf->chan.buf_policy;
07583 bi.rxbufpolicy = conf->chan.buf_policy;
07584 bi.numbufs = conf->chan.buf_no;
07585 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi);
07586 if (res < 0) {
07587 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", channel, strerror(errno));
07588 }
07589 } else
07590 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", channel, strerror(errno));
07591 }
07592 #endif
07593 tmp->immediate = conf->chan.immediate;
07594 tmp->transfertobusy = conf->chan.transfertobusy;
07595 tmp->sig = chan_sig;
07596 tmp->outsigmod = conf->chan.outsigmod;
07597 tmp->ringt_base = ringt_base;
07598 tmp->firstradio = 0;
07599 if ((chan_sig == SIG_FXOKS) || (chan_sig == SIG_FXOLS) || (chan_sig == SIG_FXOGS))
07600 tmp->permcallwaiting = conf->chan.callwaiting;
07601 else
07602 tmp->permcallwaiting = 0;
07603
07604 tmp->destroy = 0;
07605 tmp->drings = drings;
07606 tmp->usedistinctiveringdetection = conf->chan.usedistinctiveringdetection;
07607 tmp->callwaitingcallerid = conf->chan.callwaitingcallerid;
07608 tmp->threewaycalling = conf->chan.threewaycalling;
07609 tmp->adsi = conf->chan.adsi;
07610 tmp->use_smdi = conf->chan.use_smdi;
07611 tmp->permhidecallerid = conf->chan.hidecallerid;
07612 tmp->callreturn = conf->chan.callreturn;
07613 tmp->echocancel = conf->chan.echocancel;
07614 tmp->echotraining = conf->chan.echotraining;
07615 tmp->pulse = conf->chan.pulse;
07616 if (tmp->echocancel)
07617 tmp->echocanbridged = conf->chan.echocanbridged;
07618 else {
07619 if (conf->chan.echocanbridged)
07620 ast_log(LOG_NOTICE, "echocancelwhenbridged requires echocancel to be enabled; ignoring\n");
07621 tmp->echocanbridged = 0;
07622 }
07623 tmp->busydetect = conf->chan.busydetect;
07624 tmp->busycount = conf->chan.busycount;
07625 tmp->busy_tonelength = conf->chan.busy_tonelength;
07626 tmp->busy_quietlength = conf->chan.busy_quietlength;
07627 tmp->callprogress = conf->chan.callprogress;
07628 tmp->cancallforward = conf->chan.cancallforward;
07629 tmp->dtmfrelax = conf->chan.dtmfrelax;
07630 tmp->callwaiting = tmp->permcallwaiting;
07631 tmp->hidecallerid = tmp->permhidecallerid;
07632 tmp->channel = channel;
07633 tmp->stripmsd = conf->chan.stripmsd;
07634 tmp->use_callerid = conf->chan.use_callerid;
07635 tmp->cid_signalling = conf->chan.cid_signalling;
07636 tmp->cid_start = conf->chan.cid_start;
07637 tmp->dahditrcallerid = conf->chan.dahditrcallerid;
07638 tmp->restrictcid = conf->chan.restrictcid;
07639 tmp->use_callingpres = conf->chan.use_callingpres;
07640 tmp->priindication_oob = conf->chan.priindication_oob;
07641 tmp->priexclusive = conf->chan.priexclusive;
07642 if (tmp->usedistinctiveringdetection) {
07643 if (!tmp->use_callerid) {
07644 ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n");
07645 tmp->use_callerid = 1;
07646 }
07647 }
07648
07649 if (tmp->cid_signalling == CID_SIG_SMDI) {
07650 if (!tmp->use_smdi) {
07651 ast_log(LOG_WARNING, "SMDI callerid requires SMDI to be enabled, enabling...\n");
07652 tmp->use_smdi = 1;
07653 }
07654 }
07655 if (tmp->use_smdi) {
07656 tmp->smdi_iface = ast_smdi_interface_find(conf->smdi_port);
07657 if (!(tmp->smdi_iface)) {
07658 ast_log(LOG_ERROR, "Invalid SMDI port specfied, disabling SMDI support\n");
07659 tmp->use_smdi = 0;
07660 }
07661 }
07662
07663 ast_copy_string(tmp->accountcode, conf->chan.accountcode, sizeof(tmp->accountcode));
07664 tmp->amaflags = conf->chan.amaflags;
07665 if (!here) {
07666 tmp->confno = -1;
07667 tmp->propconfno = -1;
07668 }
07669 tmp->canpark = conf->chan.canpark;
07670 tmp->transfer = conf->chan.transfer;
07671 ast_copy_string(tmp->defcontext,conf->chan.context,sizeof(tmp->defcontext));
07672 ast_copy_string(tmp->language, conf->chan.language, sizeof(tmp->language));
07673 ast_copy_string(tmp->mohinterpret, conf->chan.mohinterpret, sizeof(tmp->mohinterpret));
07674 ast_copy_string(tmp->mohsuggest, conf->chan.mohsuggest, sizeof(tmp->mohsuggest));
07675 ast_copy_string(tmp->context, conf->chan.context, sizeof(tmp->context));
07676 ast_copy_string(tmp->cid_num, conf->chan.cid_num, sizeof(tmp->cid_num));
07677 tmp->cid_ton = 0;
07678 ast_copy_string(tmp->cid_name, conf->chan.cid_name, sizeof(tmp->cid_name));
07679 ast_copy_string(tmp->mailbox, conf->chan.mailbox, sizeof(tmp->mailbox));
07680 tmp->msgstate = -1;
07681 tmp->group = conf->chan.group;
07682 tmp->callgroup = conf->chan.callgroup;
07683 tmp->pickupgroup= conf->chan.pickupgroup;
07684 tmp->rxgain = conf->chan.rxgain;
07685 tmp->txgain = conf->chan.txgain;
07686 tmp->tonezone = conf->chan.tonezone;
07687 tmp->onhooktime = time(NULL);
07688 if (tmp->subs[SUB_REAL].dfd > -1) {
07689 set_actual_gain(tmp->subs[SUB_REAL].dfd, 0, tmp->rxgain, tmp->txgain, tmp->law);
07690 if (tmp->dsp)
07691 ast_dsp_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax);
07692 update_conf(tmp);
07693 if (!here) {
07694 if (chan_sig != SIG_PRI)
07695
07696 dahdi_set_hook(tmp->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
07697 }
07698 ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SETTONEZONE,&tmp->tonezone);
07699 #ifdef HAVE_PRI
07700
07701 if (tmp->pri && !pri_is_up(tmp->pri)) {
07702 tmp->inalarm = 1;
07703 }
07704 #endif
07705 if ((res = get_alarms(tmp)) != DAHDI_ALARM_NONE) {
07706 tmp->inalarm = 1;
07707 handle_alarms(tmp, res);
07708 } else {
07709
07710
07711
07712
07713
07714
07715
07716
07717
07718
07719 tmp->unknown_alarm = 1;
07720 }
07721 }
07722
07723 tmp->polarityonanswerdelay = conf->chan.polarityonanswerdelay;
07724 tmp->answeronpolarityswitch = conf->chan.answeronpolarityswitch;
07725 tmp->hanguponpolarityswitch = conf->chan.hanguponpolarityswitch;
07726 tmp->sendcalleridafter = conf->chan.sendcalleridafter;
07727
07728 }
07729 if (tmp && !here) {
07730
07731 if (!*wlist) {
07732 *wlist = tmp;
07733 tmp->prev = NULL;
07734 tmp->next = NULL;
07735 *wend = tmp;
07736 } else {
07737
07738 struct dahdi_pvt *working = *wlist;
07739
07740
07741 if (working->channel > tmp->channel) {
07742 tmp->next = *wlist;
07743 tmp->prev = NULL;
07744 (*wlist)->prev = tmp;
07745 *wlist = tmp;
07746 } else {
07747
07748 while (working) {
07749
07750 if (working->next) {
07751 if (working->channel < tmp->channel && working->next->channel > tmp->channel) {
07752 tmp->next = working->next;
07753 tmp->prev = working;
07754 working->next->prev = tmp;
07755 working->next = tmp;
07756 break;
07757 }
07758 } else {
07759
07760 if (working->channel < tmp->channel) {
07761 working->next = tmp;
07762 tmp->next = NULL;
07763 tmp->prev = working;
07764 *wend = tmp;
07765 break;
07766 }
07767 }
07768 working = working->next;
07769 }
07770 }
07771 }
07772 }
07773 return tmp;
07774 }
07775
07776 static inline int available(struct dahdi_pvt *p, int channelmatch, ast_group_t groupmatch, int *busy, int *channelmatched, int *groupmatched)
07777 {
07778 int res;
07779 struct dahdi_params par;
07780
07781
07782 if (groupmatch) {
07783 if ((p->group & groupmatch) != groupmatch)
07784 return 0;
07785 *groupmatched = 1;
07786 }
07787
07788 if (channelmatch != -1) {
07789 if (p->channel != channelmatch)
07790 return 0;
07791 *channelmatched = 1;
07792 }
07793
07794 if (busy) {
07795 if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS))
07796 *busy = 1;
07797 }
07798
07799 if (p->dnd)
07800 return 0;
07801
07802 if (p->guardtime && (time(NULL) < p->guardtime))
07803 return 0;
07804
07805
07806 if (!p->owner) {
07807 #ifdef HAVE_PRI
07808
07809 if (p->pri) {
07810 if (p->resetting || p->call)
07811 return 0;
07812 else
07813 return 1;
07814 }
07815 #endif
07816 if (!(p->radio || (p->oprmode < 0)))
07817 {
07818 if (!p->sig || (p->sig == SIG_FXSLS))
07819 return 1;
07820
07821 if (p->subs[SUB_REAL].dfd > -1)
07822 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
07823 else {
07824
07825 res = 0;
07826 par.rxisoffhook = 0;
07827 }
07828 if (res) {
07829 ast_log(LOG_WARNING, "Unable to check hook state on channel %d: %s\n", p->channel, strerror(errno));
07830 } else if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
07831
07832
07833
07834 if (par.rxbits > -1)
07835 return 1;
07836 if (par.rxisoffhook)
07837 return 1;
07838 else
07839 #ifdef DAHDI_CHECK_HOOKSTATE
07840 return 0;
07841 #else
07842 return 1;
07843 #endif
07844 } else if (par.rxisoffhook) {
07845 ast_log(LOG_DEBUG, "Channel %d off hook, can't use\n", p->channel);
07846
07847 return 0;
07848 }
07849 }
07850 return 1;
07851 }
07852
07853
07854 if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS))
07855 return 0;
07856
07857 if (!p->callwaiting) {
07858
07859 return 0;
07860 }
07861
07862 if (p->subs[SUB_CALLWAIT].dfd > -1) {
07863
07864 return 0;
07865 }
07866
07867 if ((p->owner->_state != AST_STATE_UP) &&
07868 ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
07869
07870 return 0;
07871 }
07872 if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) {
07873
07874 return 0;
07875 }
07876
07877 return 1;
07878 }
07879
07880 static struct dahdi_pvt *chandup(struct dahdi_pvt *src)
07881 {
07882 struct dahdi_pvt *p;
07883 struct dahdi_bufferinfo bi;
07884 int res;
07885
07886 if ((p = ast_malloc(sizeof(*p)))) {
07887 memcpy(p, src, sizeof(struct dahdi_pvt));
07888 ast_mutex_init(&p->lock);
07889 #ifdef HAVE_ZAPTEL
07890 p->subs[SUB_REAL].dfd = dahdi_open("/dev/zap/pseudo");
07891 #else
07892 p->subs[SUB_REAL].dfd = dahdi_open("/dev/dahdi/pseudo");
07893 #endif
07894
07895 if (p->subs[SUB_REAL].dfd < 0) {
07896 ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno));
07897 destroy_dahdi_pvt(&p);
07898 return NULL;
07899 }
07900 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi);
07901 if (!res) {
07902 bi.txbufpolicy = p->buf_policy;
07903 bi.rxbufpolicy = p->buf_policy;
07904 bi.numbufs = p->buf_no;
07905 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi);
07906 if (res < 0) {
07907 ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel: %s\n", strerror(errno));
07908 }
07909 } else
07910 ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel: %s\n", strerror(errno));
07911 }
07912 p->destroy = 1;
07913 p->next = iflist;
07914 p->prev = NULL;
07915 iflist = p;
07916 if (iflist->next)
07917 iflist->next->prev = p;
07918 return p;
07919 }
07920
07921
07922 #ifdef HAVE_PRI
07923 static int pri_find_empty_chan(struct dahdi_pri *pri, int backwards)
07924 {
07925 int x;
07926 if (backwards)
07927 x = pri->numchans;
07928 else
07929 x = 0;
07930 for (;;) {
07931 if (backwards && (x < 0))
07932 break;
07933 if (!backwards && (x >= pri->numchans))
07934 break;
07935 if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
07936 ast_log(LOG_DEBUG, "Found empty available channel %d/%d\n",
07937 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
07938 return x;
07939 }
07940 if (backwards)
07941 x--;
07942 else
07943 x++;
07944 }
07945 return -1;
07946 }
07947 #endif
07948
07949 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause)
07950 {
07951 ast_group_t groupmatch = 0;
07952 int channelmatch = -1;
07953 int roundrobin = 0;
07954 int callwait = 0;
07955 int busy = 0;
07956 struct dahdi_pvt *p;
07957 struct ast_channel *tmp = NULL;
07958 char *dest=NULL;
07959 int x;
07960 char *s;
07961 char opt=0;
07962 int res=0, y=0;
07963 int backwards = 0;
07964 #ifdef HAVE_PRI
07965 int crv;
07966 int bearer = -1;
07967 int trunkgroup;
07968 struct dahdi_pri *pri=NULL;
07969 #endif
07970 struct dahdi_pvt *exit, *start, *end;
07971 ast_mutex_t *lock;
07972 int channelmatched = 0;
07973 int groupmatched = 0;
07974
07975
07976 lock = &iflock;
07977 start = iflist;
07978 end = ifend;
07979 if (data) {
07980 dest = ast_strdupa((char *)data);
07981 } else {
07982 ast_log(LOG_WARNING, "Channel requested with no data\n");
07983 return NULL;
07984 }
07985 if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') {
07986
07987 char *stringp=NULL;
07988 stringp=dest + 1;
07989 s = strsep(&stringp, "/");
07990 if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07991 ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data);
07992 return NULL;
07993 }
07994 groupmatch = ((ast_group_t) 1 << x);
07995 if (toupper(dest[0]) == 'G') {
07996 if (dest[0] == 'G') {
07997 backwards = 1;
07998 p = ifend;
07999 } else
08000 p = iflist;
08001 } else {
08002 if (dest[0] == 'R') {
08003 backwards = 1;
08004 p = round_robin[x]?round_robin[x]->prev:ifend;
08005 if (!p)
08006 p = ifend;
08007 } else {
08008 p = round_robin[x]?round_robin[x]->next:iflist;
08009 if (!p)
08010 p = iflist;
08011 }
08012 roundrobin = 1;
08013 }
08014 } else {
08015 char *stringp=NULL;
08016 stringp=dest;
08017 s = strsep(&stringp, "/");
08018 p = iflist;
08019 if (!strcasecmp(s, "pseudo")) {
08020
08021 x = CHAN_PSEUDO;
08022 channelmatch = x;
08023 }
08024 #ifdef HAVE_PRI
08025 else if ((res = sscanf(s, "%d:%d%c%d", &trunkgroup, &crv, &opt, &y)) > 1) {
08026 if ((trunkgroup < 1) || (crv < 1)) {
08027 ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data);
08028 return NULL;
08029 }
08030 res--;
08031 for (x = 0; x < NUM_SPANS; x++) {
08032 if (pris[x].trunkgroup == trunkgroup) {
08033 pri = pris + x;
08034 lock = &pri->lock;
08035 start = pri->crvs;
08036 end = pri->crvend;
08037 break;
08038 }
08039 }
08040 if (!pri) {
08041 ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup);
08042 return NULL;
08043 }
08044 channelmatch = crv;
08045 p = pris[x].crvs;
08046 }
08047 #endif
08048 else if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
08049 ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data);
08050 return NULL;
08051 } else {
08052 channelmatch = x;
08053 }
08054 }
08055
08056 ast_mutex_lock(lock);
08057 exit = p;
08058 while (p && !tmp) {
08059 if (roundrobin)
08060 round_robin[x] = p;
08061 #if 0
08062 ast_verbose("name = %s, %d, %d, %d\n",p->owner ? p->owner->name : "<none>", p->channel, channelmatch, groupmatch);
08063 #endif
08064
08065 if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) {
08066 if (option_debug)
08067 ast_log(LOG_DEBUG, "Using channel %d\n", p->channel);
08068 if (p->inalarm)
08069 goto next;
08070
08071 callwait = (p->owner != NULL);
08072 #ifdef HAVE_PRI
08073 if (pri && (p->subs[SUB_REAL].dfd < 0)) {
08074 if (p->sig != SIG_FXSKS) {
08075
08076
08077 bearer = pri_find_empty_chan(pri, 0);
08078 if (bearer < 0) {
08079 ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv);
08080 p = NULL;
08081 break;
08082 }
08083 pri_assign_bearer(p, pri, pri->pvts[bearer]);
08084 } else {
08085 if (alloc_sub(p, 0)) {
08086 ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n");
08087 p = NULL;
08088 break;
08089 } else
08090 ast_log(LOG_DEBUG, "Allocated placeholder pseudo channel\n");
08091 p->pri = pri;
08092 }
08093 }
08094 #endif
08095 if (p->channel == CHAN_PSEUDO) {
08096 p = chandup(p);
08097 if (!p) {
08098 break;
08099 }
08100 }
08101 if (p->owner) {
08102 if (alloc_sub(p, SUB_CALLWAIT)) {
08103 p = NULL;
08104 break;
08105 }
08106 }
08107 p->outgoing = 1;
08108 tmp = dahdi_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0);
08109 #ifdef HAVE_PRI
08110 if (p->bearer) {
08111
08112 p->bearer->owner = tmp;
08113 }
08114 #endif
08115
08116 if (res > 1) {
08117 if (opt == 'c') {
08118
08119 p->confirmanswer = 1;
08120 } else if (opt == 'r') {
08121
08122 if (res < 3)
08123 ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data);
08124 else
08125 p->distinctivering = y;
08126 } else if (opt == 'd') {
08127
08128 p->digital = 1;
08129 if (tmp)
08130 tmp->transfercapability = AST_TRANS_CAP_DIGITAL;
08131 } else {
08132 ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data);
08133 }
08134 }
08135
08136 if (tmp && callwait)
08137 tmp->cdrflags |= AST_CDR_CALLWAIT;
08138 break;
08139 }
08140 next:
08141 if (backwards) {
08142 p = p->prev;
08143 if (!p)
08144 p = end;
08145 } else {
08146 p = p->next;
08147 if (!p)
08148 p = start;
08149 }
08150
08151 if (p == exit)
08152 break;
08153 }
08154 ast_mutex_unlock(lock);
08155 restart_monitor();
08156 if (callwait)
08157 *cause = AST_CAUSE_BUSY;
08158 else if (!tmp) {
08159 if (channelmatched) {
08160 if (busy)
08161 *cause = AST_CAUSE_BUSY;
08162 } else if (groupmatched) {
08163 *cause = AST_CAUSE_CONGESTION;
08164 }
08165 }
08166
08167 return tmp;
08168 }
08169
08170
08171 #ifdef HAVE_PRI
08172 static struct dahdi_pvt *pri_find_crv(struct dahdi_pri *pri, int crv)
08173 {
08174 struct dahdi_pvt *p;
08175 p = pri->crvs;
08176 while (p) {
08177 if (p->channel == crv)
08178 return p;
08179 p = p->next;
08180 }
08181 return NULL;
08182 }
08183
08184
08185 static int pri_find_principle(struct dahdi_pri *pri, int channel)
08186 {
08187 int x;
08188 int span = PRI_SPAN(channel);
08189 int spanfd;
08190 struct dahdi_params param;
08191 int principle = -1;
08192 int explicit = PRI_EXPLICIT(channel);
08193 channel = PRI_CHANNEL(channel);
08194
08195 if (!explicit) {
08196 spanfd = pri_active_dchan_fd(pri);
08197 if (ioctl(spanfd, DAHDI_GET_PARAMS, ¶m))
08198 return -1;
08199 span = pris[param.spanno - 1].prilogicalspan;
08200 }
08201
08202 for (x = 0; x < pri->numchans; x++) {
08203 if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
08204 principle = x;
08205 break;
08206 }
08207 }
08208
08209 return principle;
08210 }
08211
08212 static int pri_fixup_principle(struct dahdi_pri *pri, int principle, q931_call *c)
08213 {
08214 int x;
08215 struct dahdi_pvt *crv;
08216 if (!c) {
08217 if (principle < 0)
08218 return -1;
08219 return principle;
08220 }
08221 if ((principle > -1) &&
08222 (principle < pri->numchans) &&
08223 (pri->pvts[principle]) &&
08224 (pri->pvts[principle]->call == c))
08225 return principle;
08226
08227 for (x = 0; x < pri->numchans; x++) {
08228 if (!pri->pvts[x])
08229 continue;
08230 if (pri->pvts[x]->call == c) {
08231
08232 if (principle != x) {
08233 struct dahdi_pvt *new = pri->pvts[principle], *old = pri->pvts[x];
08234
08235 if (option_verbose > 2)
08236 ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n",
08237 old->channel, new->channel);
08238 if (new->owner) {
08239 ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
08240 old->channel, new->channel, new->channel);
08241 return -1;
08242 }
08243
08244 new->owner = old->owner;
08245 old->owner = NULL;
08246 if (new->owner) {
08247 ast_string_field_build(new->owner, name, "%s/%d:%d-%d", dahdi_chan_name, pri->trunkgroup, new->channel, 1);
08248 new->owner->tech_pvt = new;
08249 new->owner->fds[0] = new->subs[SUB_REAL].dfd;
08250 new->subs[SUB_REAL].owner = old->subs[SUB_REAL].owner;
08251 old->subs[SUB_REAL].owner = NULL;
08252 } else
08253 ast_log(LOG_WARNING, "Whoa, there's no owner, and we're having to fix up channel %d to channel %d\n", old->channel, new->channel);
08254 new->call = old->call;
08255 old->call = NULL;
08256
08257
08258 new->dsp = old->dsp;
08259 new->dsp_features = old->dsp_features;
08260 old->dsp = NULL;
08261 old->dsp_features = 0;
08262 }
08263 return principle;
08264 }
08265 }
08266
08267 crv = pri->crvs;
08268 while (crv) {
08269 if (crv->call == c) {
08270
08271 if (crv->bearer)
08272 ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n");
08273 else if (pri->pvts[principle]->owner)
08274 ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n");
08275 else {
08276
08277
08278 dahdi_close_sub(crv, SUB_REAL);
08279 pri->pvts[principle]->call = crv->call;
08280 pri_assign_bearer(crv, pri, pri->pvts[principle]);
08281 ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n",
08282 pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset,
08283 pri->trunkgroup, crv->channel);
08284 wakeup_sub(crv, SUB_REAL, pri);
08285 }
08286 return principle;
08287 }
08288 crv = crv->next;
08289 }
08290 ast_log(LOG_WARNING, "Call specified, but not found?\n");
08291 return -1;
08292 }
08293
08294 static void *do_idle_thread(void *vchan)
08295 {
08296 struct ast_channel *chan = vchan;
08297 struct dahdi_pvt *pvt = chan->tech_pvt;
08298 struct ast_frame *f;
08299 char ex[80];
08300
08301 int newms, ms = 30000;
08302 if (option_verbose > 2)
08303 ast_verbose(VERBOSE_PREFIX_3 "Initiating idle call on channel %s\n", chan->name);
08304 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
08305 if (ast_call(chan, ex, 0)) {
08306 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
08307 ast_hangup(chan);
08308 return NULL;
08309 }
08310 while ((newms = ast_waitfor(chan, ms)) > 0) {
08311 f = ast_read(chan);
08312 if (!f) {
08313
08314 break;
08315 }
08316 if (f->frametype == AST_FRAME_CONTROL) {
08317 switch (f->subclass) {
08318 case AST_CONTROL_ANSWER:
08319
08320 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
08321 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
08322 chan->priority = 1;
08323 if (option_verbose > 3)
08324 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
08325 ast_pbx_run(chan);
08326
08327 return NULL;
08328 case AST_CONTROL_BUSY:
08329 if (option_verbose > 3)
08330 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' busy, waiting...\n", chan->name);
08331 break;
08332 case AST_CONTROL_CONGESTION:
08333 if (option_verbose > 3)
08334 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' congested, waiting...\n", chan->name);
08335 break;
08336 };
08337 }
08338 ast_frfree(f);
08339 ms = newms;
08340 }
08341
08342 ast_hangup(chan);
08343 return NULL;
08344 }
08345
08346 #ifndef PRI_RESTART
08347 #error "Upgrade your libpri"
08348 #endif
08349 static void dahdi_pri_message(struct pri *pri, char *s)
08350 {
08351 int x, y;
08352 int dchan = -1, span = -1;
08353 int dchancount = 0;
08354
08355 if (pri) {
08356 for (x = 0; x < NUM_SPANS; x++) {
08357 for (y = 0; y < NUM_DCHANS; y++) {
08358 if (pris[x].dchans[y])
08359 dchancount++;
08360
08361 if (pris[x].dchans[y] == pri)
08362 dchan = y;
08363 }
08364 if (dchan >= 0) {
08365 span = x;
08366 break;
08367 }
08368 dchancount = 0;
08369 }
08370 if ((dchan >= 0) && (span >= 0)) {
08371 if (dchancount > 1)
08372 ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s);
08373 else
08374 ast_verbose("%s", s);
08375 } else
08376 ast_log(LOG_ERROR, "PRI debug error: could not find pri associated it with debug message output\n");
08377 } else
08378 ast_verbose("%s", s);
08379
08380 ast_mutex_lock(&pridebugfdlock);
08381
08382 if (pridebugfd >= 0) {
08383 if (write(pridebugfd, s, strlen(s)) < 0) {
08384 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
08385 }
08386 }
08387
08388 ast_mutex_unlock(&pridebugfdlock);
08389 }
08390
08391 static void dahdi_pri_error(struct pri *pri, char *s)
08392 {
08393 int x, y;
08394 int dchan = -1, span = -1;
08395 int dchancount = 0;
08396
08397 if (pri) {
08398 for (x = 0; x < NUM_SPANS; x++) {
08399 for (y = 0; y < NUM_DCHANS; y++) {
08400 if (pris[x].dchans[y])
08401 dchancount++;
08402
08403 if (pris[x].dchans[y] == pri)
08404 dchan = y;
08405 }
08406 if (dchan >= 0) {
08407 span = x;
08408 break;
08409 }
08410 dchancount = 0;
08411 }
08412 if ((dchan >= 0) && (span >= 0)) {
08413 if (dchancount > 1)
08414 ast_log(LOG_ERROR, "[Span %d D-Channel %d] PRI: %s", span, dchan, s);
08415 else
08416 ast_log(LOG_ERROR, "%s", s);
08417 } else
08418 ast_log(LOG_ERROR, "PRI debug error: could not find pri associated it with debug message output\n");
08419 } else
08420 ast_log(LOG_ERROR, "%s", s);
08421
08422 ast_mutex_lock(&pridebugfdlock);
08423
08424 if (pridebugfd >= 0) {
08425 if (write(pridebugfd, s, strlen(s)) < 0) {
08426 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
08427 }
08428 }
08429
08430 ast_mutex_unlock(&pridebugfdlock);
08431 }
08432
08433 static int pri_check_restart(struct dahdi_pri *pri)
08434 {
08435 do {
08436 pri->resetpos++;
08437 } while ((pri->resetpos < pri->numchans) &&
08438 (!pri->pvts[pri->resetpos] ||
08439 pri->pvts[pri->resetpos]->call ||
08440 pri->pvts[pri->resetpos]->resetting));
08441 if (pri->resetpos < pri->numchans) {
08442
08443 pri->pvts[pri->resetpos]->resetting = 1;
08444 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
08445 } else {
08446 pri->resetting = 0;
08447 time(&pri->lastreset);
08448 }
08449 return 0;
08450 }
08451
08452 static int pri_hangup_all(struct dahdi_pvt *p, struct dahdi_pri *pri)
08453 {
08454 int x;
08455 int redo;
08456 ast_mutex_unlock(&pri->lock);
08457 ast_mutex_lock(&p->lock);
08458 do {
08459 redo = 0;
08460 for (x = 0; x < 3; x++) {
08461 while (p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) {
08462 redo++;
08463 DEADLOCK_AVOIDANCE(&p->lock);
08464 }
08465 if (p->subs[x].owner) {
08466 ast_queue_hangup(p->subs[x].owner);
08467 ast_mutex_unlock(&p->subs[x].owner->lock);
08468 }
08469 }
08470 } while (redo);
08471 ast_mutex_unlock(&p->lock);
08472 ast_mutex_lock(&pri->lock);
08473 return 0;
08474 }
08475
08476 static char * redirectingreason2str(int redirectingreason)
08477 {
08478 switch (redirectingreason) {
08479 case 0:
08480 return "UNKNOWN";
08481 case 1:
08482 return "BUSY";
08483 case 2:
08484 return "NO_REPLY";
08485 case 0xF:
08486 return "UNCONDITIONAL";
08487 default:
08488 return "NOREDIRECT";
08489 }
08490 }
08491
08492 static void apply_plan_to_number(char *buf, size_t size, const struct dahdi_pri *pri, const char *number, const int plan)
08493 {
08494 switch (plan) {
08495 case PRI_INTERNATIONAL_ISDN:
08496 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
08497 break;
08498 case PRI_NATIONAL_ISDN:
08499 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
08500 break;
08501 case PRI_LOCAL_ISDN:
08502 snprintf(buf, size, "%s%s", pri->localprefix, number);
08503 break;
08504 case PRI_PRIVATE:
08505 snprintf(buf, size, "%s%s", pri->privateprefix, number);
08506 break;
08507 case PRI_UNKNOWN:
08508 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
08509 break;
08510 default:
08511 snprintf(buf, size, "%s", number);
08512 break;
08513 }
08514 }
08515
08516 static int dahdi_setlaw(int dfd, int law)
08517 {
08518 int res;
08519 res = ioctl(dfd, DAHDI_SETLAW, &law);
08520 if (res)
08521 return res;
08522 return 0;
08523 }
08524
08525 static void *pri_dchannel(void *vpri)
08526 {
08527 struct dahdi_pri *pri = vpri;
08528 pri_event *e;
08529 struct pollfd fds[NUM_DCHANS];
08530 int res;
08531 int chanpos = 0;
08532 int x;
08533 int haveidles;
08534 int activeidles;
08535 int nextidle = -1;
08536 struct ast_channel *c;
08537 struct timeval tv, lowest, *next;
08538 struct timeval lastidle = { 0, 0 };
08539 int doidling=0;
08540 char *cc;
08541 char idlen[80];
08542 struct ast_channel *idle;
08543 pthread_t p;
08544 time_t t;
08545 int i, which=-1;
08546 int numdchans;
08547 int cause=0;
08548 struct dahdi_pvt *crv;
08549 pthread_t threadid;
08550 pthread_attr_t attr;
08551 char ani2str[6];
08552 char plancallingnum[256];
08553 char plancallingani[256];
08554 char calledtonstr[10];
08555
08556 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
08557
08558 gettimeofday(&lastidle, NULL);
08559 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
08560
08561 cc = strchr(pri->idleext, '@');
08562 if (cc) {
08563 *cc = '\0';
08564 cc++;
08565 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
08566 #if 0
08567
08568 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
08569 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
08570 else
08571 #endif
08572 doidling = 1;
08573 } else
08574 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
08575 }
08576 for (;;) {
08577 for (i = 0; i < NUM_DCHANS; i++) {
08578 if (!pri->dchannels[i])
08579 break;
08580 fds[i].fd = pri->fds[i];
08581 fds[i].events = POLLIN | POLLPRI;
08582 fds[i].revents = 0;
08583 }
08584 numdchans = i;
08585 time(&t);
08586 ast_mutex_lock(&pri->lock);
08587 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->resetinterval > 0)) {
08588 if (pri->resetting && pri_is_up(pri)) {
08589 if (pri->resetpos < 0)
08590 pri_check_restart(pri);
08591 } else {
08592 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
08593 pri->resetting = 1;
08594 pri->resetpos = -1;
08595 }
08596 }
08597 }
08598
08599 if (doidling && pri_is_up(pri)) {
08600 nextidle = -1;
08601 haveidles = 0;
08602 activeidles = 0;
08603 for (x = pri->numchans; x >= 0; x--) {
08604 if (pri->pvts[x] && !pri->pvts[x]->owner &&
08605 !pri->pvts[x]->call) {
08606 if (haveidles < pri->minunused) {
08607 haveidles++;
08608 } else if (!pri->pvts[x]->resetting) {
08609 nextidle = x;
08610 break;
08611 }
08612 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
08613 activeidles++;
08614 }
08615 if (nextidle > -1) {
08616 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
08617
08618 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
08619 idle = dahdi_request(dahdi_chan_name, AST_FORMAT_ULAW, idlen, &cause);
08620 if (idle) {
08621 pri->pvts[nextidle]->isidlecall = 1;
08622 if (ast_pthread_create_background(&p, NULL, do_idle_thread, idle)) {
08623 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
08624 dahdi_hangup(idle);
08625 }
08626 } else
08627 ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
08628 gettimeofday(&lastidle, NULL);
08629 }
08630 } else if ((haveidles < pri->minunused) &&
08631 (activeidles > pri->minidle)) {
08632
08633
08634 for (x = pri->numchans; x >= 0; x--) {
08635
08636 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
08637 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08638 haveidles++;
08639
08640
08641 if ((haveidles >= pri->minunused) ||
08642 (activeidles <= pri->minidle))
08643 break;
08644 }
08645 }
08646 }
08647 }
08648
08649 lowest = ast_tv(60, 0);
08650 for (i = 0; i < NUM_DCHANS; i++) {
08651
08652 if (!pri->dchannels[i])
08653 break;
08654 if ((next = pri_schedule_next(pri->dchans[i]))) {
08655
08656 tv = ast_tvsub(*next, ast_tvnow());
08657 if (tv.tv_sec < 0) {
08658 tv = ast_tv(0,0);
08659 }
08660 if (doidling || pri->resetting) {
08661 if (tv.tv_sec > 1) {
08662 tv = ast_tv(1, 0);
08663 }
08664 } else {
08665 if (tv.tv_sec > 60) {
08666 tv = ast_tv(60, 0);
08667 }
08668 }
08669 } else if (doidling || pri->resetting) {
08670
08671
08672 tv = ast_tv(1,0);
08673 } else {
08674
08675 tv = ast_tv(60, 0);
08676 }
08677 if (!i || ast_tvcmp(tv, lowest) < 0) {
08678 lowest = tv;
08679 }
08680 }
08681 ast_mutex_unlock(&pri->lock);
08682
08683 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
08684 pthread_testcancel();
08685 e = NULL;
08686 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
08687 pthread_testcancel();
08688 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
08689
08690 ast_mutex_lock(&pri->lock);
08691 if (!res) {
08692 for (which = 0; which < NUM_DCHANS; which++) {
08693 if (!pri->dchans[which])
08694 break;
08695
08696 e = pri_schedule_run(pri->dchans[which]);
08697 if (e)
08698 break;
08699 }
08700 } else if (res > -1) {
08701 for (which = 0; which < NUM_DCHANS; which++) {
08702 if (!pri->dchans[which])
08703 break;
08704 if (fds[which].revents & POLLPRI) {
08705
08706 x = 0;
08707 res = ioctl(pri->fds[which], DAHDI_GETEVENT, &x);
08708 if (x)
08709 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);
08710
08711 if (x == DAHDI_EVENT_ALARM) {
08712 pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
08713 pri_find_dchan(pri);
08714 } else if (x == DAHDI_EVENT_NOALARM) {
08715 pri->dchanavail[which] |= DCHAN_NOTINALARM;
08716 pri_restart(pri->dchans[which]);
08717 }
08718
08719 if (option_debug)
08720 ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
08721 } else if (fds[which].revents & POLLIN) {
08722 e = pri_check_event(pri->dchans[which]);
08723 }
08724 if (e)
08725 break;
08726 }
08727 } else if (errno != EINTR)
08728 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
08729
08730 if (e) {
08731 if (pri->debug)
08732 pri_dump_event(pri->dchans[which], e);
08733
08734 if (e->e != PRI_EVENT_DCHAN_DOWN) {
08735 if (!(pri->dchanavail[which] & DCHAN_UP)) {
08736 if (option_verbose > 1)
08737 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span);
08738 }
08739 pri->dchanavail[which] |= DCHAN_UP;
08740 } else {
08741 if (pri->dchanavail[which] & DCHAN_UP) {
08742 if (option_verbose > 1)
08743 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span);
08744 }
08745 pri->dchanavail[which] &= ~DCHAN_UP;
08746 }
08747
08748 if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
08749
08750 pri->pri = pri->dchans[which];
08751
08752 switch (e->e) {
08753 case PRI_EVENT_DCHAN_UP:
08754 if (!pri->pri) pri_find_dchan(pri);
08755
08756
08757 time(&pri->lastreset);
08758
08759
08760 if (pri->resetinterval > -1) {
08761 pri->lastreset -= pri->resetinterval;
08762 pri->lastreset += 5;
08763 }
08764 pri->resetting = 0;
08765
08766 for (i = 0; i < pri->numchans; i++)
08767 if (pri->pvts[i]) {
08768 pri->pvts[i]->inalarm = 0;
08769 }
08770 break;
08771 case PRI_EVENT_DCHAN_DOWN:
08772 pri_find_dchan(pri);
08773 if (!pri_is_up(pri)) {
08774 pri->resetting = 0;
08775
08776 for (i = 0; i < pri->numchans; i++) {
08777 struct dahdi_pvt *p = pri->pvts[i];
08778 if (p) {
08779 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
08780
08781 if (p->call) {
08782 if (p->pri && p->pri->pri) {
08783 pri_hangup(p->pri->pri, p->call, -1);
08784 pri_destroycall(p->pri->pri, p->call);
08785 p->call = NULL;
08786 } else
08787 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
08788 }
08789 if (p->realcall) {
08790 pri_hangup_all(p->realcall, pri);
08791 } else if (p->owner)
08792 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08793 }
08794 p->inalarm = 1;
08795 }
08796 }
08797 }
08798 break;
08799 case PRI_EVENT_RESTART:
08800 if (e->restart.channel > -1) {
08801 chanpos = pri_find_principle(pri, e->restart.channel);
08802 if (chanpos < 0)
08803 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
08804 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08805 else {
08806 if (option_verbose > 2)
08807 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n",
08808 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08809 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08810 if (pri->pvts[chanpos]->call) {
08811 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
08812 pri->pvts[chanpos]->call = NULL;
08813 }
08814
08815 if (pri->pvts[chanpos]->realcall)
08816 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08817 else if (pri->pvts[chanpos]->owner)
08818 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08819 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08820 }
08821 } else {
08822 if (option_verbose > 2)
08823 ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span);
08824 for (x = 0; x < pri->numchans; x++)
08825 if (pri->pvts[x]) {
08826 ast_mutex_lock(&pri->pvts[x]->lock);
08827 if (pri->pvts[x]->call) {
08828 pri_destroycall(pri->pri, pri->pvts[x]->call);
08829 pri->pvts[x]->call = NULL;
08830 }
08831 if (pri->pvts[chanpos]->realcall)
08832 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08833 else if (pri->pvts[x]->owner)
08834 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08835 ast_mutex_unlock(&pri->pvts[x]->lock);
08836 }
08837 }
08838 break;
08839 case PRI_EVENT_KEYPAD_DIGIT:
08840 chanpos = pri_find_principle(pri, e->digit.channel);
08841 if (chanpos < 0) {
08842 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
08843 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
08844 } else {
08845 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
08846 if (chanpos > -1) {
08847 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08848
08849 if (pri->overlapdial && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
08850
08851 int digitlen = strlen(e->digit.digits);
08852 char digit;
08853 int i;
08854 for (i = 0; i < digitlen; i++) {
08855 digit = e->digit.digits[i];
08856 {
08857 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08858 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
08859 }
08860 }
08861 }
08862 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08863 }
08864 }
08865 break;
08866
08867 case PRI_EVENT_INFO_RECEIVED:
08868 chanpos = pri_find_principle(pri, e->ring.channel);
08869 if (chanpos < 0) {
08870 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
08871 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08872 } else {
08873 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
08874 if (chanpos > -1) {
08875 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08876
08877 if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
08878
08879 int digitlen = strlen(e->ring.callednum);
08880 char digit;
08881 int i;
08882 for (i = 0; i < digitlen; i++) {
08883 digit = e->ring.callednum[i];
08884 {
08885 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08886 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
08887 }
08888 }
08889 }
08890 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08891 }
08892 }
08893 break;
08894 case PRI_EVENT_RING:
08895 crv = NULL;
08896 if (e->ring.channel == -1)
08897 chanpos = pri_find_empty_chan(pri, 1);
08898 else
08899 chanpos = pri_find_principle(pri, e->ring.channel);
08900
08901 if (chanpos < 0) {
08902 ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
08903 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08904 } else {
08905 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08906 if (pri->pvts[chanpos]->owner) {
08907 if (pri->pvts[chanpos]->call == e->ring.call) {
08908 ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
08909 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08910 break;
08911 } else {
08912
08913 ast_log(LOG_DEBUG, "Ring requested on channel %d/%d already in use or previously requested on span %d. Attempting to renegotiating channel.\n",
08914 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08915 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08916 chanpos = -1;
08917 }
08918 }
08919 if (chanpos > -1)
08920 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08921 }
08922 if ((chanpos < 0) && (e->ring.flexible))
08923 chanpos = pri_find_empty_chan(pri, 1);
08924 if (chanpos > -1) {
08925 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08926 if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
08927
08928 crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
08929 if (crv)
08930 ast_mutex_lock(&crv->lock);
08931 if (!crv || crv->owner) {
08932 pri->pvts[chanpos]->call = NULL;
08933 if (crv) {
08934 if (crv->owner)
08935 crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08936 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);
08937 } else
08938 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);
08939 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
08940 if (crv)
08941 ast_mutex_unlock(&crv->lock);
08942 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08943 break;
08944 }
08945 }
08946 pri->pvts[chanpos]->call = e->ring.call;
08947 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
08948 if (pri->pvts[chanpos]->use_callerid) {
08949 ast_shrink_phone_number(plancallingnum);
08950 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
08951 #ifdef PRI_ANI
08952 if (!ast_strlen_zero(e->ring.callingani)) {
08953 apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
08954 ast_shrink_phone_number(plancallingani);
08955 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
08956 } else {
08957 pri->pvts[chanpos]->cid_ani[0] = '\0';
08958 }
08959 #endif
08960 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
08961 pri->pvts[chanpos]->cid_ton = e->ring.callingplan;
08962 } else {
08963 pri->pvts[chanpos]->cid_num[0] = '\0';
08964 pri->pvts[chanpos]->cid_ani[0] = '\0';
08965 pri->pvts[chanpos]->cid_name[0] = '\0';
08966 pri->pvts[chanpos]->cid_ton = 0;
08967 }
08968 apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
08969 e->ring.redirectingnum, e->ring.callingplanrdnis);
08970
08971 if (pri->pvts[chanpos]->immediate) {
08972 if (option_verbose > 2)
08973 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n");
08974 pri->pvts[chanpos]->exten[0] = 's';
08975 pri->pvts[chanpos]->exten[1] = '\0';
08976 }
08977
08978 else if (!ast_strlen_zero(e->ring.callednum)) {
08979 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
08980 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08981 } else if (pri->overlapdial)
08982 pri->pvts[chanpos]->exten[0] = '\0';
08983 else {
08984
08985 pri->pvts[chanpos]->exten[0] = 's';
08986 pri->pvts[chanpos]->exten[1] = '\0';
08987 }
08988
08989 if (!ast_strlen_zero(e->ring.callednum))
08990 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08991
08992 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
08993 if (option_verbose > 2)
08994 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n");
08995 pri->pvts[chanpos]->exten[0] = 's';
08996 pri->pvts[chanpos]->exten[1] = '\0';
08997 }
08998
08999 if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
09000 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
09001
09002 int law;
09003 if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
09004
09005 law = 1;
09006 if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &law) == -1)
09007 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", pri->pvts[chanpos]->channel, law, strerror(errno));
09008 }
09009 if (e->ring.layer1 == PRI_LAYER_1_ALAW)
09010 law = DAHDI_LAW_ALAW;
09011 else
09012 law = DAHDI_LAW_MULAW;
09013 res = dahdi_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].dfd, law);
09014 if (res < 0)
09015 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
09016 res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].dfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
09017 if (res < 0)
09018 ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
09019 if (e->ring.complete || !pri->overlapdial) {
09020
09021 pri->pvts[chanpos]->proceeding = 1;
09022 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
09023 } else {
09024 if (pri->switchtype != PRI_SWITCH_GR303_TMC)
09025 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
09026 else
09027 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
09028 }
09029
09030 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
09031
09032
09033 if (!e->ring.complete && pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
09034
09035 ast_mutex_unlock(&pri->lock);
09036 if (crv) {
09037
09038 pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
09039 c = dahdi_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
09040 pri->pvts[chanpos]->owner = &inuse;
09041 ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
09042 } else {
09043 c = dahdi_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
09044 }
09045
09046 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09047
09048 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
09049 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
09050 }
09051 if (e->ring.ani2 >= 0) {
09052 snprintf(ani2str, 5, "%.2d", e->ring.ani2);
09053 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
09054 }
09055
09056 #ifdef SUPPORT_USERUSER
09057 if (!ast_strlen_zero(e->ring.useruserinfo)) {
09058 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
09059 }
09060 #endif
09061
09062 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
09063 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
09064 if (e->ring.redirectingreason >= 0)
09065 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
09066
09067 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09068 ast_mutex_lock(&pri->lock);
09069
09070 pthread_attr_init(&attr);
09071 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
09072 if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) {
09073 if (option_verbose > 2)
09074 ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
09075 plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
09076 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
09077 } else {
09078 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
09079 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
09080 if (c)
09081 ast_hangup(c);
09082 else {
09083 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
09084 pri->pvts[chanpos]->call = NULL;
09085 }
09086 }
09087 pthread_attr_destroy(&attr);
09088 } else {
09089 ast_mutex_unlock(&pri->lock);
09090
09091 c = dahdi_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype);
09092 if (c) {
09093 char calledtonstr[10];
09094
09095 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09096
09097 if (e->ring.ani2 >= 0) {
09098 snprintf(ani2str, 5, "%d", e->ring.ani2);
09099 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
09100 }
09101
09102 #ifdef SUPPORT_USERUSER
09103 if (!ast_strlen_zero(e->ring.useruserinfo)) {
09104 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
09105 }
09106 #endif
09107
09108 if (e->ring.redirectingreason >= 0)
09109 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
09110
09111 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
09112 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
09113
09114 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09115 ast_mutex_lock(&pri->lock);
09116
09117 if (option_verbose > 2)
09118 ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
09119 plancallingnum, pri->pvts[chanpos]->exten,
09120 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
09121 dahdi_enable_ec(pri->pvts[chanpos]);
09122 } else {
09123
09124 ast_mutex_lock(&pri->lock);
09125
09126 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
09127 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
09128 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
09129 pri->pvts[chanpos]->call = NULL;
09130 }
09131 }
09132 } else {
09133 if (option_verbose > 2)
09134 ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
09135 pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
09136 pri->pvts[chanpos]->prioffset, pri->span);
09137 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
09138 pri->pvts[chanpos]->call = NULL;
09139 pri->pvts[chanpos]->exten[0] = '\0';
09140 }
09141 if (crv)
09142 ast_mutex_unlock(&crv->lock);
09143 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09144 } else {
09145 if (e->ring.flexible)
09146 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
09147 else
09148 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
09149 }
09150 break;
09151 case PRI_EVENT_RINGING:
09152 chanpos = pri_find_principle(pri, e->ringing.channel);
09153 if (chanpos < 0) {
09154 ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
09155 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
09156 } else {
09157 chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
09158 if (chanpos < 0) {
09159 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
09160 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
09161 } else {
09162 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09163 if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
09164 dahdi_enable_ec(pri->pvts[chanpos]);
09165 pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1;
09166 pri->pvts[chanpos]->alerting = 1;
09167 } else
09168 ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n");
09169 #ifdef PRI_PROGRESS_MASK
09170 if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) {
09171 #else
09172 if (e->ringing.progress == 8) {
09173 #endif
09174
09175 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
09176
09177 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING);
09178 pri->pvts[chanpos]->dsp_features = 0;
09179 }
09180 }
09181
09182 #ifdef SUPPORT_USERUSER
09183 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
09184 struct ast_channel *owner = pri->pvts[chanpos]->owner;
09185 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09186 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->ringing.useruserinfo);
09187 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09188 }
09189 #endif
09190
09191 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09192 }
09193 }
09194 break;
09195 case PRI_EVENT_PROGRESS:
09196
09197 chanpos = pri_find_principle(pri, e->proceeding.channel);
09198 if (chanpos > -1) {
09199 #ifdef PRI_PROGRESS_MASK
09200 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) {
09201 #else
09202 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) {
09203 #endif
09204 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
09205
09206 if (e->proceeding.cause > -1) {
09207 if (option_verbose > 2)
09208 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with cause code %d received\n", e->proceeding.cause);
09209
09210
09211 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
09212 if (pri->pvts[chanpos]->owner) {
09213 if (option_verbose > 2)
09214 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
09215
09216 pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
09217 f.subclass = AST_CONTROL_BUSY;
09218 }
09219 }
09220 }
09221
09222 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09223 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
09224 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
09225 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
09226 #ifdef PRI_PROGRESS_MASK
09227 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
09228 #else
09229 if (e->proceeding.progress == 8) {
09230 #endif
09231
09232 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
09233 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
09234 pri->pvts[chanpos]->dsp_features = 0;
09235 }
09236 }
09237 pri->pvts[chanpos]->progress = 1;
09238 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09239 }
09240 }
09241 break;
09242 case PRI_EVENT_PROCEEDING:
09243 chanpos = pri_find_principle(pri, e->proceeding.channel);
09244 if (chanpos > -1) {
09245 if (!pri->pvts[chanpos]->proceeding) {
09246 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
09247
09248 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09249 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
09250 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
09251 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
09252 #ifdef PRI_PROGRESS_MASK
09253 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
09254 #else
09255 if (e->proceeding.progress == 8) {
09256 #endif
09257
09258 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
09259 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
09260 pri->pvts[chanpos]->dsp_features = 0;
09261 }
09262
09263 f.subclass = AST_CONTROL_PROGRESS;
09264 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
09265 }
09266 pri->pvts[chanpos]->proceeding = 1;
09267 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09268 }
09269 }
09270 break;
09271 case PRI_EVENT_FACNAME:
09272 chanpos = pri_find_principle(pri, e->facname.channel);
09273 if (chanpos < 0) {
09274 ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
09275 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
09276 } else {
09277 chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
09278 if (chanpos < 0) {
09279 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
09280 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
09281 } else {
09282
09283 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09284 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
09285 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
09286 pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
09287 dahdi_enable_ec(pri->pvts[chanpos]);
09288 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09289 }
09290 }
09291 break;
09292 case PRI_EVENT_ANSWER:
09293 chanpos = pri_find_principle(pri, e->answer.channel);
09294 if (chanpos < 0) {
09295 ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
09296 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
09297 } else {
09298 chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
09299 if (chanpos < 0) {
09300 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
09301 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
09302 } else {
09303 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09304
09305
09306
09307
09308
09309 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
09310 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
09311 pri->pvts[chanpos]->dsp_features = 0;
09312 }
09313 if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) {
09314 ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n");
09315 x = DAHDI_START;
09316 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
09317 if (res < 0) {
09318 if (errno != EINPROGRESS) {
09319 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
09320 }
09321 }
09322 } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
09323 pri->pvts[chanpos]->dialing = 1;
09324
09325 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_DIAL, &pri->pvts[chanpos]->dop);
09326 if (res < 0) {
09327 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", pri->pvts[chanpos]->channel, strerror(errno));
09328 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
09329 } else
09330 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
09331 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
09332 } else if (pri->pvts[chanpos]->confirmanswer) {
09333 ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
09334 } else {
09335 pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
09336
09337 dahdi_enable_ec(pri->pvts[chanpos]);
09338 }
09339
09340 #ifdef SUPPORT_USERUSER
09341 if (!ast_strlen_zero(e->answer.useruserinfo)) {
09342 struct ast_channel *owner = pri->pvts[chanpos]->owner;
09343 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09344 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->answer.useruserinfo);
09345 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09346 }
09347 #endif
09348
09349 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09350 }
09351 }
09352 break;
09353 case PRI_EVENT_HANGUP:
09354 chanpos = pri_find_principle(pri, e->hangup.channel);
09355 if (chanpos < 0) {
09356 ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
09357 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09358 } else {
09359 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09360 if (chanpos > -1) {
09361 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09362 if (!pri->pvts[chanpos]->alreadyhungup) {
09363
09364 pri->pvts[chanpos]->alreadyhungup = 1;
09365 if (pri->pvts[chanpos]->realcall)
09366 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09367 else if (pri->pvts[chanpos]->owner) {
09368
09369 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
09370 if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
09371 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09372 else {
09373 switch (e->hangup.cause) {
09374 case PRI_CAUSE_USER_BUSY:
09375 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
09376 break;
09377 case PRI_CAUSE_CALL_REJECTED:
09378 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
09379 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
09380 case PRI_CAUSE_SWITCH_CONGESTION:
09381 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
09382 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
09383 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
09384 break;
09385 default:
09386 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09387 }
09388 }
09389 }
09390 if (option_verbose > 2)
09391 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup, cause %d\n",
09392 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
09393 } else {
09394 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
09395 pri->pvts[chanpos]->call = NULL;
09396 }
09397 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
09398 if (option_verbose > 2)
09399 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
09400 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09401 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
09402 pri->pvts[chanpos]->resetting = 1;
09403 }
09404 if (e->hangup.aoc_units > -1)
09405 if (option_verbose > 2)
09406 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
09407 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
09408
09409 #ifdef SUPPORT_USERUSER
09410 if (pri->pvts[chanpos]->owner && !ast_strlen_zero(e->hangup.useruserinfo)) {
09411 struct ast_channel *owner = pri->pvts[chanpos]->owner;
09412 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09413 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
09414 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09415 }
09416 #endif
09417
09418 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09419 } else {
09420 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
09421 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09422 }
09423 }
09424 break;
09425 #ifndef PRI_EVENT_HANGUP_REQ
09426 #error please update libpri
09427 #endif
09428 case PRI_EVENT_HANGUP_REQ:
09429 chanpos = pri_find_principle(pri, e->hangup.channel);
09430 if (chanpos < 0) {
09431 ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
09432 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09433 } else {
09434 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09435 if (chanpos > -1) {
09436 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09437 if (pri->pvts[chanpos]->realcall)
09438 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09439 else if (pri->pvts[chanpos]->owner) {
09440 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
09441 if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
09442 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09443 else {
09444 switch (e->hangup.cause) {
09445 case PRI_CAUSE_USER_BUSY:
09446 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
09447 break;
09448 case PRI_CAUSE_CALL_REJECTED:
09449 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
09450 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
09451 case PRI_CAUSE_SWITCH_CONGESTION:
09452 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
09453 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
09454 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
09455 break;
09456 default:
09457 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09458 }
09459 }
09460 if (option_verbose > 2)
09461 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup request, cause %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span, e->hangup.cause);
09462 if (e->hangup.aoc_units > -1)
09463 if (option_verbose > 2)
09464 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
09465 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
09466 } else {
09467 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
09468 pri->pvts[chanpos]->call = NULL;
09469 }
09470 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
09471 if (option_verbose > 2)
09472 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n",
09473 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09474 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
09475 pri->pvts[chanpos]->resetting = 1;
09476 }
09477
09478 #ifdef SUPPORT_USERUSER
09479 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09480 struct ast_channel *owner = pri->pvts[chanpos]->owner;
09481 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09482 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
09483 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09484 }
09485 #endif
09486
09487 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09488 } else {
09489 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);
09490 }
09491 }
09492 break;
09493 case PRI_EVENT_HANGUP_ACK:
09494 chanpos = pri_find_principle(pri, e->hangup.channel);
09495 if (chanpos < 0) {
09496 ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
09497 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09498 } else {
09499 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09500 if (chanpos > -1) {
09501 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09502 pri->pvts[chanpos]->call = NULL;
09503 pri->pvts[chanpos]->resetting = 0;
09504 if (pri->pvts[chanpos]->owner) {
09505 if (option_verbose > 2)
09506 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);
09507 }
09508
09509 #ifdef SUPPORT_USERUSER
09510 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09511 struct ast_channel *owner = pri->pvts[chanpos]->owner;
09512 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09513 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
09514 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09515 }
09516 #endif
09517
09518 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09519 }
09520 }
09521 break;
09522 case PRI_EVENT_CONFIG_ERR:
09523 ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->trunkgroup, e->err.err);
09524 break;
09525 case PRI_EVENT_RESTART_ACK:
09526 chanpos = pri_find_principle(pri, e->restartack.channel);
09527 if (chanpos < 0) {
09528
09529
09530
09531 for (x = 0; x < pri->numchans; x++) {
09532 if (pri->pvts[x] && pri->pvts[x]->resetting) {
09533 chanpos = x;
09534 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09535 ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
09536 pri->pvts[chanpos]->prioffset, pri->span);
09537 if (pri->pvts[chanpos]->realcall)
09538 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09539 else if (pri->pvts[chanpos]->owner) {
09540 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
09541 pri->pvts[chanpos]->prioffset, pri->span);
09542 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09543 }
09544 pri->pvts[chanpos]->resetting = 0;
09545 if (option_verbose > 2)
09546 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
09547 pri->pvts[chanpos]->prioffset, pri->span);
09548 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09549 if (pri->resetting)
09550 pri_check_restart(pri);
09551 break;
09552 }
09553 }
09554 if (chanpos < 0) {
09555 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
09556 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09557 }
09558 } else {
09559 if (pri->pvts[chanpos]) {
09560 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09561 if (pri->pvts[chanpos]->realcall)
09562 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09563 else if (pri->pvts[chanpos]->owner) {
09564 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
09565 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09566 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09567 }
09568 pri->pvts[chanpos]->resetting = 0;
09569 if (option_verbose > 2)
09570 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
09571 pri->pvts[chanpos]->prioffset, pri->span);
09572 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09573 if (pri->resetting)
09574 pri_check_restart(pri);
09575 }
09576 }
09577 break;
09578 case PRI_EVENT_SETUP_ACK:
09579 chanpos = pri_find_principle(pri, e->setup_ack.channel);
09580 if (chanpos < 0) {
09581 ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
09582 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
09583 } else {
09584 chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
09585 if (chanpos > -1) {
09586 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09587 pri->pvts[chanpos]->setup_ack = 1;
09588
09589 for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
09590 ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
09591 pri_information(pri->pri, pri->pvts[chanpos]->call,
09592 pri->pvts[chanpos]->dialdest[x]);
09593 }
09594 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09595 } else
09596 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
09597 }
09598 break;
09599 case PRI_EVENT_NOTIFY:
09600 chanpos = pri_find_principle(pri, e->notify.channel);
09601 if (chanpos < 0) {
09602 ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
09603 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
09604 } else {
09605 struct ast_frame f = { AST_FRAME_CONTROL, };
09606 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09607 switch (e->notify.info) {
09608 case PRI_NOTIFY_REMOTE_HOLD:
09609 f.subclass = AST_CONTROL_HOLD;
09610 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
09611 break;
09612 case PRI_NOTIFY_REMOTE_RETRIEVAL:
09613 f.subclass = AST_CONTROL_UNHOLD;
09614 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
09615 break;
09616 }
09617 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09618 }
09619 break;
09620 default:
09621 ast_log(LOG_DEBUG, "Event: %d\n", e->e);
09622 }
09623 }
09624 ast_mutex_unlock(&pri->lock);
09625 }
09626
09627 return NULL;
09628 }
09629
09630 static int start_pri(struct dahdi_pri *pri)
09631 {
09632 int res, x;
09633 struct dahdi_params p;
09634 struct dahdi_bufferinfo bi;
09635 struct dahdi_spaninfo si;
09636 int i;
09637
09638 for (i = 0; i < NUM_DCHANS; i++) {
09639 if (!pri->dchannels[i])
09640 break;
09641 #ifdef HAVE_ZAPTEL
09642 pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600);
09643 #else
09644 pri->fds[i] = open("/dev/dahdi/channel", O_RDWR, 0600);
09645 #endif
09646 x = pri->dchannels[i];
09647 if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],DAHDI_SPECIFY,&x) == -1)) {
09648 ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
09649 return -1;
09650 }
09651 res = ioctl(pri->fds[i], DAHDI_GET_PARAMS, &p);
09652 if (res) {
09653 dahdi_close_pri_fd(pri, i);
09654 ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
09655 return -1;
09656 }
09657 if ((p.sigtype != DAHDI_SIG_HDLCFCS) && (p.sigtype != DAHDI_SIG_HARDHDLC)) {
09658 dahdi_close_pri_fd(pri, i);
09659 ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode. See /etc/dahdi/system.conf\n", x);
09660 return -1;
09661 }
09662 memset(&si, 0, sizeof(si));
09663 res = ioctl(pri->fds[i], DAHDI_SPANSTAT, &si);
09664 if (res) {
09665 dahdi_close_pri_fd(pri, i);
09666 ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
09667 }
09668 if (!si.alarms)
09669 pri->dchanavail[i] |= DCHAN_NOTINALARM;
09670 else
09671 pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
09672 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
09673 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
09674 bi.numbufs = 32;
09675 bi.bufsize = 1024;
09676 if (ioctl(pri->fds[i], DAHDI_SET_BUFINFO, &bi)) {
09677 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d: %s\n", x, strerror(errno));
09678 dahdi_close_pri_fd(pri, i);
09679 return -1;
09680 }
09681 pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
09682
09683 if (pri->switchtype == PRI_SWITCH_GR303_TMC)
09684 pri->overlapdial = 1;
09685 pri_set_overlapdial(pri->dchans[i],pri->overlapdial);
09686 #ifdef HAVE_PRI_INBANDDISCONNECT
09687 pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
09688 #endif
09689
09690 if (i)
09691 pri_enslave(pri->dchans[0], pri->dchans[i]);
09692 if (!pri->dchans[i]) {
09693 dahdi_close_pri_fd(pri, i);
09694 ast_log(LOG_ERROR, "Unable to create PRI structure\n");
09695 return -1;
09696 }
09697 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
09698 pri_set_nsf(pri->dchans[i], pri->nsf);
09699 #ifdef PRI_GETSET_TIMERS
09700 for (x = 0; x < PRI_MAX_TIMERS; x++) {
09701 if (pritimers[x] != 0)
09702 pri_set_timer(pri->dchans[i], x, pritimers[x]);
09703 }
09704 #endif
09705 }
09706
09707 pri->pri = pri->dchans[0];
09708 pri->resetpos = -1;
09709 if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
09710 for (i = 0; i < NUM_DCHANS; i++) {
09711 if (!pri->dchannels[i])
09712 break;
09713 dahdi_close_pri_fd(pri, i);
09714 }
09715 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
09716 return -1;
09717 }
09718 return 0;
09719 }
09720
09721 static char *complete_span_helper(const char *line, const char *word, int pos, int state, int rpos)
09722 {
09723 int which, span;
09724 char *ret = NULL;
09725
09726 if (pos != rpos)
09727 return ret;
09728
09729 for (which = span = 0; span < NUM_SPANS; span++) {
09730 if (pris[span].pri && ++which > state) {
09731 if (asprintf(&ret, "%d", span + 1) < 0) {
09732 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
09733 }
09734 break;
09735 }
09736 }
09737 return ret;
09738 }
09739
09740 static char *complete_span_4(const char *line, const char *word, int pos, int state)
09741 {
09742 return complete_span_helper(line,word,pos,state,3);
09743 }
09744
09745 static char *complete_span_5(const char *line, const char *word, int pos, int state)
09746 {
09747 return complete_span_helper(line,word,pos,state,4);
09748 }
09749
09750 static int handle_pri_set_debug_file(int fd, int argc, char **argv)
09751 {
09752 int myfd;
09753
09754 if (!strncasecmp(argv[1], "set", 3)) {
09755 if (argc < 5)
09756 return RESULT_SHOWUSAGE;
09757
09758 if (ast_strlen_zero(argv[4]))
09759 return RESULT_SHOWUSAGE;
09760
09761 myfd = open(argv[4], O_CREAT|O_WRONLY, 0600);
09762 if (myfd < 0) {
09763 ast_cli(fd, "Unable to open '%s' for writing\n", argv[4]);
09764 return RESULT_SUCCESS;
09765 }
09766
09767 ast_mutex_lock(&pridebugfdlock);
09768
09769 if (pridebugfd >= 0)
09770 close(pridebugfd);
09771
09772 pridebugfd = myfd;
09773 ast_copy_string(pridebugfilename,argv[4],sizeof(pridebugfilename));
09774
09775 ast_mutex_unlock(&pridebugfdlock);
09776
09777 ast_cli(fd, "PRI debug output will be sent to '%s'\n", argv[4]);
09778 } else {
09779
09780 ast_mutex_lock(&pridebugfdlock);
09781 close(pridebugfd);
09782 pridebugfd = -1;
09783 ast_cli(fd, "PRI debug output to file disabled\n");
09784 ast_mutex_unlock(&pridebugfdlock);
09785 }
09786
09787 return RESULT_SUCCESS;
09788 }
09789
09790 #ifdef HAVE_PRI_VERSION
09791 static int handle_pri_version(int fd, int agc, char *argv[]) {
09792 ast_cli(fd, "libpri version: %s\n", pri_get_version());
09793 return RESULT_SUCCESS;
09794 }
09795 #endif
09796
09797 static int handle_pri_debug(int fd, int argc, char *argv[])
09798 {
09799 int span;
09800 int x;
09801 if (argc < 4) {
09802 return RESULT_SHOWUSAGE;
09803 }
09804 span = atoi(argv[3]);
09805 if ((span < 1) || (span > NUM_SPANS)) {
09806 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[3], 1, NUM_SPANS);
09807 return RESULT_SUCCESS;
09808 }
09809 if (!pris[span-1].pri) {
09810 ast_cli(fd, "No PRI running on span %d\n", span);
09811 return RESULT_SUCCESS;
09812 }
09813 for (x = 0; x < NUM_DCHANS; x++) {
09814 if (pris[span-1].dchans[x])
09815 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU |
09816 PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE |
09817 PRI_DEBUG_Q921_STATE);
09818 }
09819 ast_cli(fd, "Enabled debugging on span %d\n", span);
09820 return RESULT_SUCCESS;
09821 }
09822
09823
09824
09825 static int handle_pri_no_debug(int fd, int argc, char *argv[])
09826 {
09827 int span;
09828 int x;
09829 if (argc < 5)
09830 return RESULT_SHOWUSAGE;
09831 span = atoi(argv[4]);
09832 if ((span < 1) || (span > NUM_SPANS)) {
09833 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09834 return RESULT_SUCCESS;
09835 }
09836 if (!pris[span-1].pri) {
09837 ast_cli(fd, "No PRI running on span %d\n", span);
09838 return RESULT_SUCCESS;
09839 }
09840 for (x = 0; x < NUM_DCHANS; x++) {
09841 if (pris[span-1].dchans[x])
09842 pri_set_debug(pris[span-1].dchans[x], 0);
09843 }
09844 ast_cli(fd, "Disabled debugging on span %d\n", span);
09845 return RESULT_SUCCESS;
09846 }
09847
09848 static int handle_pri_really_debug(int fd, int argc, char *argv[])
09849 {
09850 int span;
09851 int x;
09852 if (argc < 5)
09853 return RESULT_SHOWUSAGE;
09854 span = atoi(argv[4]);
09855 if ((span < 1) || (span > NUM_SPANS)) {
09856 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09857 return RESULT_SUCCESS;
09858 }
09859 if (!pris[span-1].pri) {
09860 ast_cli(fd, "No PRI running on span %d\n", span);
09861 return RESULT_SUCCESS;
09862 }
09863 for (x = 0; x < NUM_DCHANS; x++) {
09864 if (pris[span-1].dchans[x])
09865 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU |
09866 PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE |
09867 PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_STATE);
09868 }
09869 ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span);
09870 return RESULT_SUCCESS;
09871 }
09872
09873 static void build_status(char *s, size_t len, int status, int active)
09874 {
09875 if (!s || len < 1) {
09876 return;
09877 }
09878 s[0] = '\0';
09879 if (status & DCHAN_PROVISIONED)
09880 strncat(s, "Provisioned, ", len - strlen(s) - 1);
09881 if (!(status & DCHAN_NOTINALARM))
09882 strncat(s, "In Alarm, ", len - strlen(s) - 1);
09883 if (status & DCHAN_UP)
09884 strncat(s, "Up", len - strlen(s) - 1);
09885 else
09886 strncat(s, "Down", len - strlen(s) - 1);
09887 if (active)
09888 strncat(s, ", Active", len - strlen(s) - 1);
09889 else
09890 strncat(s, ", Standby", len - strlen(s) - 1);
09891 s[len - 1] = '\0';
09892 }
09893
09894 static int handle_pri_show_spans(int fd, int argc, char *argv[])
09895 {
09896 int span;
09897 int x;
09898 char status[256];
09899 if (argc != 3)
09900 return RESULT_SHOWUSAGE;
09901
09902 for (span = 0; span < NUM_SPANS; span++) {
09903 if (pris[span].pri) {
09904 for (x = 0; x < NUM_DCHANS; x++) {
09905 if (pris[span].dchannels[x]) {
09906 build_status(status, sizeof(status), pris[span].dchanavail[x], pris[span].dchans[x] == pris[span].pri);
09907 ast_cli(fd, "PRI span %d/%d: %s\n", span + 1, x, status);
09908 }
09909 }
09910 }
09911 }
09912 return RESULT_SUCCESS;
09913 }
09914
09915 static int handle_pri_show_span(int fd, int argc, char *argv[])
09916 {
09917 int span;
09918 int x;
09919 char status[256];
09920 if (argc < 4)
09921 return RESULT_SHOWUSAGE;
09922 span = atoi(argv[3]);
09923 if ((span < 1) || (span > NUM_SPANS)) {
09924 ast_cli(fd, "Invalid span '%s'. Should be a number from %d to %d\n", argv[3], 1, NUM_SPANS);
09925 return RESULT_SUCCESS;
09926 }
09927 if (!pris[span-1].pri) {
09928 ast_cli(fd, "No PRI running on span %d\n", span);
09929 return RESULT_SUCCESS;
09930 }
09931 for (x = 0; x < NUM_DCHANS; x++) {
09932 if (pris[span-1].dchannels[x]) {
09933 #ifdef PRI_DUMP_INFO_STR
09934 char *info_str = NULL;
09935 #endif
09936 ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
09937 build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
09938 ast_cli(fd, "Status: %s\n", status);
09939 #ifdef PRI_DUMP_INFO_STR
09940 info_str = pri_dump_info_str(pris[span-1].pri);
09941 if (info_str) {
09942 ast_cli(fd, "%s", info_str);
09943 free(info_str);
09944 }
09945 #else
09946 pri_dump_info(pris[span-1].pri);
09947 #endif
09948 ast_cli(fd, "\n");
09949 }
09950 }
09951 return RESULT_SUCCESS;
09952 }
09953
09954 static int handle_pri_show_debug(int fd, int argc, char *argv[])
09955 {
09956 int x;
09957 int span;
09958 int count=0;
09959 int debug=0;
09960
09961 for (span = 0; span < NUM_SPANS; span++) {
09962 if (pris[span].pri) {
09963 for (x = 0; x < NUM_DCHANS; x++) {
09964 debug = 0;
09965 if (pris[span].dchans[x]) {
09966 debug = pri_get_debug(pris[span].dchans[x]);
09967 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" );
09968 count++;
09969 }
09970 }
09971 }
09972
09973 }
09974 ast_mutex_lock(&pridebugfdlock);
09975 if (pridebugfd >= 0)
09976 ast_cli(fd, "Logging PRI debug to file %s\n", pridebugfilename);
09977 ast_mutex_unlock(&pridebugfdlock);
09978
09979 if (!count)
09980 ast_cli(fd, "No debug set or no PRI running\n");
09981 return RESULT_SUCCESS;
09982 }
09983
09984 static const char pri_debug_help[] =
09985 "Usage: pri debug span <span>\n"
09986 " Enables debugging on a given PRI span\n";
09987
09988 static const char pri_no_debug_help[] =
09989 "Usage: pri no debug span <span>\n"
09990 " Disables debugging on a given PRI span\n";
09991
09992 static const char pri_really_debug_help[] =
09993 "Usage: pri intensive debug span <span>\n"
09994 " Enables debugging down to the Q.921 level\n";
09995
09996 static const char pri_show_span_help[] =
09997 "Usage: pri show span <span>\n"
09998 " Displays PRI Information on a given PRI span\n";
09999
10000 static const char pri_show_spans_help[] =
10001 "Usage: pri show spans\n"
10002 " Displays PRI Information\n";
10003
10004 static struct ast_cli_entry dahdi_pri_cli[] = {
10005 { { "pri", "debug", "span", NULL },
10006 handle_pri_debug, "Enables PRI debugging on a span",
10007 pri_debug_help, complete_span_4 },
10008
10009 { { "pri", "no", "debug", "span", NULL },
10010 handle_pri_no_debug, "Disables PRI debugging on a span",
10011 pri_no_debug_help, complete_span_5 },
10012
10013 { { "pri", "intense", "debug", "span", NULL },
10014 handle_pri_really_debug, "Enables REALLY INTENSE PRI debugging",
10015 pri_really_debug_help, complete_span_5 },
10016
10017 { { "pri", "show", "spans", NULL },
10018 handle_pri_show_spans, "Displays PRI Information",
10019 pri_show_spans_help },
10020
10021 { { "pri", "show", "span", NULL },
10022 handle_pri_show_span, "Displays PRI Information",
10023 pri_show_span_help, complete_span_4 },
10024
10025 { { "pri", "show", "debug", NULL },
10026 handle_pri_show_debug, "Displays current PRI debug settings" },
10027
10028 { { "pri", "set", "debug", "file", NULL },
10029 handle_pri_set_debug_file, "Sends PRI debug output to the specified file" },
10030
10031 { { "pri", "unset", "debug", "file", NULL },
10032 handle_pri_set_debug_file, "Ends PRI debug output to file" },
10033
10034 #ifdef HAVE_PRI_VERSION
10035 { { "pri", "show", "version", NULL },
10036 handle_pri_version, "Displays version of libpri" },
10037 #endif
10038 };
10039
10040 #endif
10041
10042 static int dahdi_destroy_channel(int fd, int argc, char **argv)
10043 {
10044 int channel;
10045
10046 if (argc != 4)
10047 return RESULT_SHOWUSAGE;
10048
10049 channel = atoi(argv[3]);
10050
10051 return dahdi_destroy_channel_bynum(channel);
10052 }
10053
10054 static void dahdi_softhangup_all(void)
10055 {
10056 struct dahdi_pvt *p;
10057 retry:
10058 ast_mutex_lock(&iflock);
10059 for (p = iflist; p; p = p->next) {
10060 ast_mutex_lock(&p->lock);
10061 if (p->owner && !p->restartpending) {
10062 if (ast_channel_trylock(p->owner)) {
10063 if (option_debug > 2)
10064 ast_verbose("Avoiding deadlock\n");
10065
10066 ast_mutex_unlock(&p->lock);
10067 ast_mutex_unlock(&iflock);
10068 goto retry;
10069 }
10070 if (option_debug > 2)
10071 ast_verbose("Softhanging up on %s\n", p->owner->name);
10072 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_EXPLICIT);
10073 p->restartpending = 1;
10074 num_restart_pending++;
10075 ast_channel_unlock(p->owner);
10076 }
10077 ast_mutex_unlock(&p->lock);
10078 }
10079 ast_mutex_unlock(&iflock);
10080 }
10081
10082 static int setup_dahdi(int reload);
10083 static int dahdi_restart(void)
10084 {
10085 #if defined(HAVE_PRI)
10086 int i, j;
10087 #endif
10088 int cancel_code;
10089 struct dahdi_pvt *p;
10090
10091 ast_mutex_lock(&restart_lock);
10092
10093 if (option_verbose)
10094 ast_verbose("Destroying channels and reloading DAHDI configuration.\n");
10095 dahdi_softhangup_all();
10096 if (option_verbose > 3)
10097 ast_verbose("Initial softhangup of all DAHDI channels complete.\n");
10098
10099 #if defined(HAVE_PRI)
10100 for (i = 0; i < NUM_SPANS; i++) {
10101 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL)) {
10102 cancel_code = pthread_cancel(pris[i].master);
10103 pthread_kill(pris[i].master, SIGURG);
10104 if (option_debug > 3)
10105 ast_verbose("Waiting to join thread of span %d with pid=%p, cancel_code=%d\n", i, (void *) pris[i].master, cancel_code);
10106 pthread_join(pris[i].master, NULL);
10107 if (option_debug > 3)
10108 ast_verbose("Joined thread of span %d\n", i);
10109 }
10110 }
10111 #endif
10112
10113 ast_mutex_lock(&monlock);
10114 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
10115 cancel_code = pthread_cancel(monitor_thread);
10116 pthread_kill(monitor_thread, SIGURG);
10117 if (option_debug > 3)
10118 ast_verbose("Waiting to join monitor thread with pid=%p, cancel_code=%d\n", (void *) monitor_thread, cancel_code);
10119 pthread_join(monitor_thread, NULL);
10120 if (option_debug > 3)
10121 ast_verbose("Joined monitor thread\n");
10122 }
10123 monitor_thread = AST_PTHREADT_NULL;
10124
10125 ast_mutex_lock(&ss_thread_lock);
10126 while (ss_thread_count > 0) {
10127 int x = DAHDI_FLASH;
10128 if (option_debug > 2)
10129 ast_verbose("Waiting on %d ss_thread(s) to finish\n", ss_thread_count);
10130
10131 for (p = iflist; p; p = p->next) {
10132 if (p->owner)
10133 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
10134 }
10135 ast_cond_wait(&ss_thread_complete, &ss_thread_lock);
10136 }
10137
10138
10139 dahdi_softhangup_all();
10140 if (option_verbose > 3)
10141 ast_verbose("Final softhangup of all DAHDI channels complete.\n");
10142 destroy_all_channels();
10143 if (option_debug)
10144 ast_verbose("Channels destroyed. Now re-reading config. %d active channels remaining.\n", ast_active_channels());
10145
10146 ast_mutex_unlock(&monlock);
10147
10148 #ifdef HAVE_PRI
10149 for (i = 0; i < NUM_SPANS; i++) {
10150 for (j = 0; j < NUM_DCHANS; j++)
10151 dahdi_close_pri_fd(&(pris[i]), j);
10152 }
10153
10154 memset(pris, 0, sizeof(pris));
10155 for (i = 0; i < NUM_SPANS; i++) {
10156 ast_mutex_init(&pris[i].lock);
10157 pris[i].offset = -1;
10158 pris[i].master = AST_PTHREADT_NULL;
10159 for (j = 0; j < NUM_DCHANS; j++)
10160 pris[i].fds[j] = -1;
10161 }
10162 pri_set_error(dahdi_pri_error);
10163 pri_set_message(dahdi_pri_message);
10164 #endif
10165
10166 if (setup_dahdi(2) != 0) {
10167 ast_log(LOG_WARNING, "Reload channels from dahdi config failed!\n");
10168 ast_mutex_unlock(&ss_thread_lock);
10169 return 1;
10170 }
10171 ast_mutex_unlock(&ss_thread_lock);
10172 ast_mutex_unlock(&restart_lock);
10173 return 0;
10174 }
10175
10176 static int dahdi_restart_cmd(int fd, int argc, char **argv)
10177 {
10178 if (argc != 2) {
10179 return RESULT_SHOWUSAGE;
10180 }
10181
10182 if (dahdi_restart() != 0)
10183 return RESULT_FAILURE;
10184 return RESULT_SUCCESS;
10185 }
10186
10187 static int dahdi_show_channels(int fd, int argc, char **argv)
10188 {
10189 #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
10190 #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
10191 struct dahdi_pvt *tmp = NULL;
10192 char tmps[20] = "";
10193 ast_mutex_t *lock;
10194 struct dahdi_pvt *start;
10195 #ifdef HAVE_PRI
10196 int trunkgroup;
10197 struct dahdi_pri *pri = NULL;
10198 int x;
10199 #endif
10200
10201 lock = &iflock;
10202 start = iflist;
10203
10204 #ifdef HAVE_PRI
10205 if (argc == 4) {
10206 if ((trunkgroup = atoi(argv[3])) < 1)
10207 return RESULT_SHOWUSAGE;
10208 for (x = 0; x < NUM_SPANS; x++) {
10209 if (pris[x].trunkgroup == trunkgroup) {
10210 pri = pris + x;
10211 break;
10212 }
10213 }
10214 if (pri) {
10215 start = pri->crvs;
10216 lock = &pri->lock;
10217 } else {
10218 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
10219 return RESULT_FAILURE;
10220 }
10221 } else
10222 #endif
10223 if (argc != 3)
10224 return RESULT_SHOWUSAGE;
10225
10226 ast_mutex_lock(lock);
10227 #ifdef HAVE_PRI
10228 ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MOH Interpret");
10229 #else
10230 ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MOH Interpret");
10231 #endif
10232
10233 tmp = start;
10234 while (tmp) {
10235 if (tmp->channel > 0) {
10236 snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
10237 } else
10238 ast_copy_string(tmps, "pseudo", sizeof(tmps));
10239 ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->mohinterpret);
10240 tmp = tmp->next;
10241 }
10242 ast_mutex_unlock(lock);
10243 return RESULT_SUCCESS;
10244 #undef FORMAT
10245 #undef FORMAT2
10246 }
10247
10248 static int dahdi_show_channel(int fd, int argc, char **argv)
10249 {
10250 int channel;
10251 struct dahdi_pvt *tmp = NULL;
10252 struct dahdi_confinfo ci;
10253 struct dahdi_params ps;
10254 int x;
10255 ast_mutex_t *lock;
10256 struct dahdi_pvt *start;
10257 #ifdef HAVE_PRI
10258 char *c;
10259 int trunkgroup;
10260 struct dahdi_pri *pri=NULL;
10261 #endif
10262
10263 lock = &iflock;
10264 start = iflist;
10265
10266 if (argc != 4)
10267 return RESULT_SHOWUSAGE;
10268 #ifdef HAVE_PRI
10269 if ((c = strchr(argv[3], ':'))) {
10270 if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2)
10271 return RESULT_SHOWUSAGE;
10272 if ((trunkgroup < 1) || (channel < 1))
10273 return RESULT_SHOWUSAGE;
10274 for (x = 0; x < NUM_SPANS; x++) {
10275 if (pris[x].trunkgroup == trunkgroup) {
10276 pri = pris + x;
10277 break;
10278 }
10279 }
10280 if (pri) {
10281 start = pri->crvs;
10282 lock = &pri->lock;
10283 } else {
10284 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
10285 return RESULT_FAILURE;
10286 }
10287 } else
10288 #endif
10289 channel = atoi(argv[3]);
10290
10291 ast_mutex_lock(lock);
10292 tmp = start;
10293 while (tmp) {
10294 if (tmp->channel == channel) {
10295 #ifdef HAVE_PRI
10296 if (pri)
10297 ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
10298 else
10299 #endif
10300 ast_cli(fd, "Channel: %d\n", tmp->channel);
10301 ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].dfd);
10302 ast_cli(fd, "Span: %d\n", tmp->span);
10303 ast_cli(fd, "Extension: %s\n", tmp->exten);
10304 ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
10305 ast_cli(fd, "Context: %s\n", tmp->context);
10306 ast_cli(fd, "Caller ID: %s\n", tmp->cid_num);
10307 ast_cli(fd, "Calling TON: %d\n", tmp->cid_ton);
10308 ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name);
10309 ast_cli(fd, "Destroy: %d\n", tmp->destroy);
10310 ast_cli(fd, "InAlarm: %d\n", tmp->inalarm);
10311 ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig));
10312 ast_cli(fd, "Radio: %d\n", tmp->radio);
10313 ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
10314 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)" : "");
10315 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)" : "");
10316 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)" : "");
10317 ast_cli(fd, "Confno: %d\n", tmp->confno);
10318 ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno);
10319 ast_cli(fd, "Real in conference: %d\n", tmp->inconference);
10320 ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
10321 ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
10322 ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
10323 ast_cli(fd, "Default law: %s\n", tmp->law == DAHDI_LAW_MULAW ? "ulaw" : tmp->law == DAHDI_LAW_ALAW ? "alaw" : "unknown");
10324 ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
10325 ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
10326 ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF");
10327 if (tmp->master)
10328 ast_cli(fd, "Master Channel: %d\n", tmp->master->channel);
10329 for (x = 0; x < MAX_SLAVES; x++) {
10330 if (tmp->slaves[x])
10331 ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
10332 }
10333 #ifdef HAVE_PRI
10334 if (tmp->pri) {
10335 ast_cli(fd, "PRI Flags: ");
10336 if (tmp->resetting)
10337 ast_cli(fd, "Resetting ");
10338 if (tmp->call)
10339 ast_cli(fd, "Call ");
10340 if (tmp->bearer)
10341 ast_cli(fd, "Bearer ");
10342 ast_cli(fd, "\n");
10343 if (tmp->logicalspan)
10344 ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan);
10345 else
10346 ast_cli(fd, "PRI Logical Span: Implicit\n");
10347 }
10348
10349 #endif
10350 memset(&ci, 0, sizeof(ci));
10351 ps.channo = tmp->channel;
10352 if (tmp->subs[SUB_REAL].dfd > -1) {
10353 if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONF, &ci)) {
10354 ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
10355 }
10356 #ifdef DAHDI_GETCONFMUTE
10357 if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONFMUTE, &x)) {
10358 ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
10359 }
10360 #endif
10361 if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps) < 0) {
10362 ast_log(LOG_WARNING, "Failed to get parameters on channel %d: %s\n", tmp->channel, strerror(errno));
10363 } else {
10364 ast_cli(fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
10365 }
10366 }
10367 ast_mutex_unlock(lock);
10368 return RESULT_SUCCESS;
10369 }
10370 tmp = tmp->next;
10371 }
10372
10373 ast_cli(fd, "Unable to find given channel %d\n", channel);
10374 ast_mutex_unlock(lock);
10375 return RESULT_FAILURE;
10376 }
10377
10378 static char dahdi_show_cadences_usage[] =
10379 "Usage: dahdi show cadences\n"
10380 " Shows all cadences currently defined\n";
10381
10382 static int handle_dahdi_show_cadences(int fd, int argc, char *argv[])
10383 {
10384 int i, j;
10385 for (i = 0; i < num_cadence; i++) {
10386 char output[1024];
10387 char tmp[16], tmp2[64];
10388 snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
10389 term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
10390
10391 for (j = 0; j < 16; j++) {
10392 if (cadences[i].ringcadence[j] == 0)
10393 break;
10394 snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
10395 if (cidrings[i] * 2 - 1 == j)
10396 term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
10397 else
10398 term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
10399 if (j != 0)
10400 strncat(output, ",", sizeof(output) - strlen(output) - 1);
10401 strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
10402 }
10403 ast_cli(fd,"%s\n",output);
10404 }
10405 return 0;
10406 }
10407
10408
10409 static int dahdi_show_status(int fd, int argc, char *argv[]) {
10410 #define FORMAT "%-40.40s %-10.10s %-10d %-10d %-10d\n"
10411 #define FORMAT2 "%-40.40s %-10.10s %-10.10s %-10.10s %-10.10s\n"
10412
10413 int span;
10414 int res;
10415 char alarms[50];
10416
10417 int ctl;
10418 struct dahdi_spaninfo s;
10419
10420 #ifdef HAVE_ZAPTEL
10421 if ((ctl = open("/dev/zap/ctl", O_RDWR)) < 0) {
10422 ast_log(LOG_WARNING, "Unable to open /dev/zap/ctl: %s\n", strerror(errno));
10423 ast_cli(fd, "No Zaptel interface found.\n");
10424 return RESULT_FAILURE;
10425 }
10426 #else
10427 if ((ctl = open("/dev/dahdi/ctl", O_RDWR)) < 0) {
10428 ast_log(LOG_WARNING, "Unable to open /dev/dahdi/ctl: %s\n", strerror(errno));
10429 ast_cli(fd, "No DAHDI interface found.\n");
10430 return RESULT_FAILURE;
10431 }
10432 #endif
10433 ast_cli(fd, FORMAT2, "Description", "Alarms", "IRQ", "bpviol", "CRC4");
10434
10435 for (span = 1; span < DAHDI_MAX_SPANS; ++span) {
10436 s.spanno = span;
10437 res = ioctl(ctl, DAHDI_SPANSTAT, &s);
10438 if (res) {
10439 continue;
10440 }
10441 alarms[0] = '\0';
10442 if (s.alarms > 0) {
10443 if (s.alarms & DAHDI_ALARM_BLUE)
10444 strcat(alarms, "BLU/");
10445 if (s.alarms & DAHDI_ALARM_YELLOW)
10446 strcat(alarms, "YEL/");
10447 if (s.alarms & DAHDI_ALARM_RED)
10448 strcat(alarms, "RED/");
10449 if (s.alarms & DAHDI_ALARM_LOOPBACK)
10450 strcat(alarms, "LB/");
10451 if (s.alarms & DAHDI_ALARM_RECOVER)
10452 strcat(alarms, "REC/");
10453 if (s.alarms & DAHDI_ALARM_NOTOPEN)
10454 strcat(alarms, "NOP/");
10455 if (!strlen(alarms))
10456 strcat(alarms, "UUU/");
10457 if (strlen(alarms)) {
10458
10459 alarms[strlen(alarms) - 1] = '\0';
10460 }
10461 } else {
10462 if (s.numchans)
10463 strcpy(alarms, "OK");
10464 else
10465 strcpy(alarms, "UNCONFIGURED");
10466 }
10467
10468 ast_cli(fd, FORMAT, s.desc, alarms, s.irqmisses, s.bpvcount, s.crc4count);
10469 }
10470 close(ctl);
10471
10472 return RESULT_SUCCESS;
10473 #undef FORMAT
10474 #undef FORMAT2
10475 }
10476
10477 static char show_channels_usage[] =
10478 "Usage: dahdi show channels\n"
10479 " Shows a list of available channels\n";
10480
10481 static char show_channel_usage[] =
10482 "Usage: dahdi show channel <chan num>\n"
10483 " Detailed information about a given channel\n";
10484
10485 static char dahdi_show_status_usage[] =
10486 "Usage: dahdi show status\n"
10487 " Shows a list of DAHDI cards with status\n";
10488
10489 static char destroy_channel_usage[] =
10490 "Usage: dahdi destroy channel <chan num>\n"
10491 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n";
10492
10493 static char dahdi_restart_usage[] =
10494 "Usage: dahdi restart\n"
10495 " Restarts the DAHDI channels: destroys them all and then\n"
10496 " re-reads them from chan_dahdi.conf.\n"
10497 " Note that this will STOP any running CALL on DAHDI channels.\n"
10498 "";
10499
10500 static struct ast_cli_entry cli_zap_show_cadences_deprecated = {
10501 { "zap", "show", "cadences", NULL },
10502 handle_dahdi_show_cadences, NULL,
10503 NULL };
10504
10505 static struct ast_cli_entry cli_zap_show_channels_deprecated = {
10506 { "zap", "show", "channels", NULL },
10507 dahdi_show_channels, NULL,
10508 NULL };
10509
10510 static struct ast_cli_entry cli_zap_show_channel_deprecated = {
10511 { "zap", "show", "channel", NULL },
10512 dahdi_show_channel, NULL,
10513 NULL };
10514
10515 static struct ast_cli_entry cli_zap_destroy_channel_deprecated = {
10516 { "zap", "destroy", "channel", NULL },
10517 dahdi_destroy_channel, NULL,
10518 NULL };
10519
10520 static struct ast_cli_entry cli_zap_restart_deprecated = {
10521 { "zap", "restart", NULL },
10522 dahdi_restart_cmd, NULL,
10523 NULL };
10524
10525 static struct ast_cli_entry cli_zap_show_status_deprecated = {
10526 { "zap", "show", "status", NULL },
10527 dahdi_show_status, NULL,
10528 NULL };
10529
10530 static struct ast_cli_entry dahdi_cli[] = {
10531 { { "dahdi", "show", "cadences", NULL },
10532 handle_dahdi_show_cadences, "List cadences",
10533 dahdi_show_cadences_usage, NULL, &cli_zap_show_cadences_deprecated },
10534
10535 { { "dahdi", "show", "channels", NULL},
10536 dahdi_show_channels, "Show active DAHDI channels",
10537 show_channels_usage, NULL, &cli_zap_show_channels_deprecated },
10538
10539 { { "dahdi", "show", "channel", NULL},
10540 dahdi_show_channel, "Show information on a channel",
10541 show_channel_usage, NULL, &cli_zap_show_channel_deprecated },
10542
10543 { { "dahdi", "destroy", "channel", NULL},
10544 dahdi_destroy_channel, "Destroy a channel",
10545 destroy_channel_usage, NULL, &cli_zap_destroy_channel_deprecated },
10546
10547 { { "dahdi", "restart", NULL},
10548 dahdi_restart_cmd, "Fully restart DAHDI channels",
10549 dahdi_restart_usage, NULL, &cli_zap_restart_deprecated },
10550
10551 { { "dahdi", "show", "status", NULL},
10552 dahdi_show_status, "Show all DAHDI cards status",
10553 dahdi_show_status_usage, NULL, &cli_zap_show_status_deprecated },
10554 };
10555
10556 #define TRANSFER 0
10557 #define HANGUP 1
10558
10559 static int dahdi_fake_event(struct dahdi_pvt *p, int mode)
10560 {
10561 if (p) {
10562 switch (mode) {
10563 case TRANSFER:
10564 p->fake_event = DAHDI_EVENT_WINKFLASH;
10565 break;
10566 case HANGUP:
10567 p->fake_event = DAHDI_EVENT_ONHOOK;
10568 break;
10569 default:
10570 ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);
10571 }
10572 }
10573 return 0;
10574 }
10575 static struct dahdi_pvt *find_channel(int channel)
10576 {
10577 struct dahdi_pvt *p = iflist;
10578 while (p) {
10579 if (p->channel == channel) {
10580 break;
10581 }
10582 p = p->next;
10583 }
10584 return p;
10585 }
10586
10587 #define local_astman_ack(s, m, msg, zap) do { if (!zap) astman_send_ack(s, m, "DAHDI" msg); else astman_send_ack(s, m, "Zap" msg); } while (0)
10588 #define local_astman_header(m, hdr, zap) astman_get_header(m, (!zap) ? "DAHDI" hdr : "Zap" hdr)
10589
10590 static int __action_dnd(struct mansession *s, const struct message *m, int zap_mode, int dnd)
10591 {
10592 struct dahdi_pvt *p = NULL;
10593 const char *channel = local_astman_header(m, "Channel", zap_mode);
10594
10595 if (ast_strlen_zero(channel)) {
10596 astman_send_error(s, m, "No channel specified");
10597 return 0;
10598 }
10599 if (!(p = find_channel(atoi(channel)))) {
10600 astman_send_error(s, m, "No such channel");
10601 return 0;
10602 }
10603 p->dnd = dnd;
10604 local_astman_ack(s, m, "DND", zap_mode);
10605
10606 return 0;
10607 }
10608
10609 static int zap_action_dndon(struct mansession *s, const struct message *m)
10610 {
10611 return __action_dnd(s, m, 1, 1);
10612 }
10613
10614 static int dahdi_action_dndon(struct mansession *s, const struct message *m)
10615 {
10616 return __action_dnd(s, m, 0, 1);
10617 }
10618
10619 static int zap_action_dndoff(struct mansession *s, const struct message *m)
10620 {
10621 return __action_dnd(s, m, 1, 0);
10622 }
10623
10624 static int dahdi_action_dndoff(struct mansession *s, const struct message *m)
10625 {
10626 return __action_dnd(s, m, 0, 0);
10627 }
10628
10629 static int __action_transfer(struct mansession *s, const struct message *m, int zap_mode)
10630 {
10631 struct dahdi_pvt *p = NULL;
10632 const char *channel = local_astman_header(m, "Channel", zap_mode);
10633
10634 if (ast_strlen_zero(channel)) {
10635 astman_send_error(s, m, "No channel specified");
10636 return 0;
10637 }
10638 if (!(p = find_channel(atoi(channel)))) {
10639 astman_send_error(s, m, "No such channel");
10640 return 0;
10641 }
10642 dahdi_fake_event(p,TRANSFER);
10643 local_astman_ack(s, m, "Transfer", zap_mode);
10644
10645 return 0;
10646 }
10647
10648 static int zap_action_transfer(struct mansession *s, const struct message *m)
10649 {
10650 return __action_transfer(s, m, 1);
10651 }
10652
10653 static int dahdi_action_transfer(struct mansession *s, const struct message *m)
10654 {
10655 return __action_transfer(s, m, 0);
10656 }
10657
10658 static int __action_transferhangup(struct mansession *s, const struct message *m, int zap_mode)
10659 {
10660 struct dahdi_pvt *p = NULL;
10661 const char *channel = local_astman_header(m, "Channel", zap_mode);
10662
10663 if (ast_strlen_zero(channel)) {
10664 astman_send_error(s, m, "No channel specified");
10665 return 0;
10666 }
10667 if (!(p = find_channel(atoi(channel)))) {
10668 astman_send_error(s, m, "No such channel");
10669 return 0;
10670 }
10671 dahdi_fake_event(p, HANGUP);
10672 local_astman_ack(s, m, "Hangup", zap_mode);
10673 return 0;
10674 }
10675
10676 static int zap_action_transferhangup(struct mansession *s, const struct message *m)
10677 {
10678 return __action_transferhangup(s, m, 1);
10679 }
10680
10681 static int dahdi_action_transferhangup(struct mansession *s, const struct message *m)
10682 {
10683 return __action_transferhangup(s, m, 0);
10684 }
10685
10686 static int __action_dialoffhook(struct mansession *s, const struct message *m, int zap_mode)
10687 {
10688 struct dahdi_pvt *p = NULL;
10689 const char *channel = local_astman_header(m, "Channel", zap_mode);
10690 const char *number = astman_get_header(m, "Number");
10691 int i;
10692
10693 if (ast_strlen_zero(channel)) {
10694 astman_send_error(s, m, "No channel specified");
10695 return 0;
10696 }
10697 if (ast_strlen_zero(number)) {
10698 astman_send_error(s, m, "No number specified");
10699 return 0;
10700 }
10701 if (!(p = find_channel(atoi(channel)))) {
10702 astman_send_error(s, m, "No such channel");
10703 return 0;
10704 }
10705 if (!p->owner) {
10706 astman_send_error(s, m, "Channel does not have an owner");
10707 return 0;
10708 }
10709 for (i = 0; i < strlen(number); i++) {
10710 struct ast_frame f = { AST_FRAME_DTMF, number[i] };
10711
10712 dahdi_queue_frame(p, &f, NULL);
10713 }
10714 local_astman_ack(s, m, "DialOffHook", zap_mode);
10715
10716 return 0;
10717 }
10718
10719 static int zap_action_dialoffhook(struct mansession *s, const struct message *m)
10720 {
10721 return __action_dialoffhook(s, m, 1);
10722 }
10723
10724 static int dahdi_action_dialoffhook(struct mansession *s, const struct message *m)
10725 {
10726 return __action_dialoffhook(s, m, 0);
10727 }
10728
10729 static int __action_showchannels(struct mansession *s, const struct message *m, int zap_mode)
10730 {
10731 struct dahdi_pvt *tmp = NULL;
10732 const char *id = astman_get_header(m, "ActionID");
10733 char idText[256] = "";
10734
10735 local_astman_ack(s, m, " channel status will follow", zap_mode);
10736 if (!ast_strlen_zero(id))
10737 snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id);
10738
10739 ast_mutex_lock(&iflock);
10740
10741 tmp = iflist;
10742 while (tmp) {
10743 if (tmp->channel > 0) {
10744 int alarm = get_alarms(tmp);
10745 astman_append(s,
10746 "Event: %sShowChannels\r\n"
10747 "Channel: %d\r\n"
10748 "Signalling: %s\r\n"
10749 "Context: %s\r\n"
10750 "DND: %s\r\n"
10751 "Alarm: %s\r\n"
10752 "%s"
10753 "\r\n",
10754 dahdi_chan_name,
10755 tmp->channel, sig2str(tmp->sig), tmp->context,
10756 tmp->dnd ? "Enabled" : "Disabled",
10757 alarm2str(alarm), idText);
10758 }
10759
10760 tmp = tmp->next;
10761 }
10762
10763 ast_mutex_unlock(&iflock);
10764
10765 astman_append(s,
10766 "Event: %sShowChannelsComplete\r\n"
10767 "%s"
10768 "\r\n",
10769 dahdi_chan_name,
10770 idText);
10771 return 0;
10772 }
10773
10774 static int zap_action_showchannels(struct mansession *s, const struct message *m)
10775 {
10776 return __action_showchannels(s, m, 1);
10777 }
10778
10779 static int dahdi_action_showchannels(struct mansession *s, const struct message *m)
10780 {
10781 return __action_showchannels(s, m, 0);
10782 }
10783
10784 static int __action_restart(struct mansession *s, const struct message *m, int zap_mode)
10785 {
10786 if (dahdi_restart() != 0) {
10787 if (zap_mode) {
10788 astman_send_error(s, m, "Failed to restart Zap");
10789 } else {
10790 astman_send_error(s, m, "Failed to restart DAHDI");
10791 }
10792 return 1;
10793 }
10794 local_astman_ack(s, m, "Restart: Success", zap_mode);
10795 return 0;
10796 }
10797
10798 static int zap_action_restart(struct mansession *s, const struct message *m)
10799 {
10800 return __action_restart(s, m, 1);
10801 }
10802
10803 static int dahdi_action_restart(struct mansession *s, const struct message *m)
10804 {
10805 return __action_restart(s, m, 0);
10806 }
10807
10808 #define local_astman_unregister(a) do { \
10809 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { \
10810 ast_manager_unregister("DAHDI" a); \
10811 } \
10812 ast_manager_unregister("Zap" a); \
10813 } while (0)
10814
10815 static int __unload_module(void)
10816 {
10817 struct dahdi_pvt *p;
10818
10819 #ifdef HAVE_PRI
10820 int i, j;
10821 for (i = 0; i < NUM_SPANS; i++) {
10822 if (pris[i].master != AST_PTHREADT_NULL)
10823 pthread_cancel(pris[i].master);
10824 }
10825 ast_cli_unregister_multiple(dahdi_pri_cli, sizeof(dahdi_pri_cli) / sizeof(struct ast_cli_entry));
10826
10827 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) {
10828 ast_unregister_application(dahdi_send_keypad_facility_app);
10829 }
10830 ast_unregister_application(zap_send_keypad_facility_app);
10831 #endif
10832 ast_cli_unregister_multiple(dahdi_cli, sizeof(dahdi_cli) / sizeof(struct ast_cli_entry));
10833 local_astman_unregister("DialOffHook");
10834 local_astman_unregister("Hangup");
10835 local_astman_unregister("Transfer");
10836 local_astman_unregister("DNDoff");
10837 local_astman_unregister("DNDon");
10838 local_astman_unregister("ShowChannels");
10839 local_astman_unregister("Restart");
10840 ast_channel_unregister(chan_tech);
10841 ast_mutex_lock(&iflock);
10842
10843 p = iflist;
10844 while (p) {
10845 if (p->owner)
10846 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
10847 p = p->next;
10848 }
10849 ast_mutex_unlock(&iflock);
10850 ast_mutex_lock(&monlock);
10851 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
10852 pthread_cancel(monitor_thread);
10853 pthread_kill(monitor_thread, SIGURG);
10854 pthread_join(monitor_thread, NULL);
10855 }
10856 monitor_thread = AST_PTHREADT_STOP;
10857 ast_mutex_unlock(&monlock);
10858
10859 destroy_all_channels();
10860 #ifdef HAVE_PRI
10861 for (i = 0; i < NUM_SPANS; i++) {
10862 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
10863 pthread_join(pris[i].master, NULL);
10864 for (j = 0; j < NUM_DCHANS; j++) {
10865 dahdi_close_pri_fd(&(pris[i]), j);
10866 }
10867 }
10868 #endif
10869 ast_cond_destroy(&ss_thread_complete);
10870 return 0;
10871 }
10872
10873 static int unload_module(void)
10874 {
10875 #ifdef HAVE_PRI
10876 int y;
10877 for (y = 0; y < NUM_SPANS; y++)
10878 ast_mutex_destroy(&pris[y].lock);
10879 #endif
10880 return __unload_module();
10881 }
10882
10883 static int build_channels(struct dahdi_chan_conf *conf, int iscrv, const char *value, int reload, int lineno, int *found_pseudo)
10884 {
10885 char *c, *chan;
10886 int x, start, finish;
10887 struct dahdi_pvt *tmp;
10888 #ifdef HAVE_PRI
10889 struct dahdi_pri *pri;
10890 int trunkgroup, y;
10891 #endif
10892
10893 if ((reload == 0) && (conf->chan.sig < 0)) {
10894 ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
10895 return -1;
10896 }
10897
10898 c = ast_strdupa(value);
10899
10900 #ifdef HAVE_PRI
10901 pri = NULL;
10902 if (iscrv) {
10903 if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) {
10904 ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", lineno);
10905 return -1;
10906 }
10907 if (trunkgroup < 1) {
10908 ast_log(LOG_WARNING, "CRV trunk group must be a positive number at line %d\n", lineno);
10909 return -1;
10910 }
10911 c += y;
10912 for (y = 0; y < NUM_SPANS; y++) {
10913 if (pris[y].trunkgroup == trunkgroup) {
10914 pri = pris + y;
10915 break;
10916 }
10917 }
10918 if (!pri) {
10919 ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, lineno);
10920 return -1;
10921 }
10922 }
10923 #endif
10924
10925 while ((chan = strsep(&c, ","))) {
10926 if (sscanf(chan, "%d-%d", &start, &finish) == 2) {
10927
10928 } else if (sscanf(chan, "%d", &start)) {
10929
10930 finish = start;
10931 } else if (!strcasecmp(chan, "pseudo")) {
10932 finish = start = CHAN_PSEUDO;
10933 if (found_pseudo)
10934 *found_pseudo = 1;
10935 } else {
10936 ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", value, chan);
10937 return -1;
10938 }
10939 if (finish < start) {
10940 ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
10941 x = finish;
10942 finish = start;
10943 start = x;
10944 }
10945
10946 for (x = start; x <= finish; x++) {
10947 #ifdef HAVE_PRI
10948 tmp = mkintf(x, conf, pri, reload);
10949 #else
10950 tmp = mkintf(x, conf, NULL, reload);
10951 #endif
10952
10953 if (tmp) {
10954 if (option_verbose > 2) {
10955 #ifdef HAVE_PRI
10956 if (pri)
10957 ast_verbose(VERBOSE_PREFIX_3 "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup, x, sig2str(tmp->sig));
10958 else
10959 #endif
10960 ast_verbose(VERBOSE_PREFIX_3 "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig));
10961 }
10962 } else {
10963 ast_log(LOG_ERROR, "Unable to %s channel '%s'\n",
10964 (reload == 1) ? "reconfigure" : "register", value);
10965 return -1;
10966 }
10967 }
10968 }
10969
10970 return 0;
10971 }
10972
10973
10974
10975 #define MAX_CHANLIST_LEN 80
10976 static int process_dahdi(struct dahdi_chan_conf *confp, const char *cat, struct ast_variable *v, int reload, int skipchannels)
10977 {
10978 struct dahdi_pvt *tmp;
10979 int y;
10980 int found_pseudo = 0;
10981 char dahdichan[MAX_CHANLIST_LEN] = {};
10982
10983 for (; v; v = v->next) {
10984 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
10985 continue;
10986
10987
10988 if (!strcasecmp(v->name, "channel")
10989 #ifdef HAVE_PRI
10990 || !strcasecmp(v->name, "crv")
10991 #endif
10992 ) {
10993 int iscrv;
10994 if (skipchannels)
10995 continue;
10996 iscrv = !strcasecmp(v->name, "crv");
10997 if (build_channels(confp, iscrv, v->value, reload, v->lineno, &found_pseudo))
10998 return -1;
10999 } else if (!strcasecmp(v->name, "buffers")) {
11000 int res;
11001 char policy[21] = "";
11002
11003 res = sscanf(v->value, "%d,%20s", &confp->chan.buf_no, policy);
11004 if (res != 2) {
11005 ast_log(LOG_WARNING, "Parsing buffers option data failed, using defaults.\n");
11006 confp->chan.buf_no = numbufs;
11007 continue;
11008 }
11009 if (confp->chan.buf_no < 0)
11010 confp->chan.buf_no = numbufs;
11011 if (!strcasecmp(policy, "full")) {
11012 confp->chan.buf_policy = DAHDI_POLICY_WHEN_FULL;
11013 } else if (!strcasecmp(policy, "immediate")) {
11014 confp->chan.buf_policy = DAHDI_POLICY_IMMEDIATE;
11015 } else {
11016 ast_log(LOG_WARNING, "Invalid policy name given (%s).\n", policy);
11017 }
11018 } else if (!strcasecmp(v->name, "zapchan") || !strcasecmp(v->name, "dahdichan")) {
11019 ast_copy_string(dahdichan, v->value, sizeof(dahdichan));
11020 if (v->name[0] == 'z' || v->name[0] == 'Z') {
11021 ast_log(LOG_WARNING, "Option zapchan has been deprecated in favor of dahdichan (found in [%s])\n", cat);
11022 }
11023 } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
11024 if (ast_true(v->value))
11025 confp->chan.usedistinctiveringdetection = 1;
11026 } else if (!strcasecmp(v->name, "distinctiveringaftercid")) {
11027 if (ast_true(v->value))
11028 distinctiveringaftercid = 1;
11029 } else if (!strcasecmp(v->name, "dring1context")) {
11030 ast_copy_string(drings.ringContext[0].contextData, v->value, sizeof(drings.ringContext[0].contextData));
11031 } else if (!strcasecmp(v->name, "dring2context")) {
11032 ast_copy_string(drings.ringContext[1].contextData, v->value, sizeof(drings.ringContext[1].contextData));
11033 } else if (!strcasecmp(v->name, "dring3context")) {
11034 ast_copy_string(drings.ringContext[2].contextData, v->value, sizeof(drings.ringContext[2].contextData));
11035 } else if (!strcasecmp(v->name, "dring1")) {
11036 sscanf(v->value, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]);
11037 } else if (!strcasecmp(v->name, "dring2")) {
11038 sscanf(v->value, "%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]);
11039 } else if (!strcasecmp(v->name, "dring3")) {
11040 sscanf(v->value, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]);
11041 } else if (!strcasecmp(v->name, "usecallerid")) {
11042 confp->chan.use_callerid = ast_true(v->value);
11043 } else if (!strcasecmp(v->name, "cidsignalling")) {
11044 if (!strcasecmp(v->value, "bell"))
11045 confp->chan.cid_signalling = CID_SIG_BELL;
11046 else if (!strcasecmp(v->value, "v23"))
11047 confp->chan.cid_signalling = CID_SIG_V23;
11048 else if (!strcasecmp(v->value, "dtmf"))
11049 confp->chan.cid_signalling = CID_SIG_DTMF;
11050 else if (!strcasecmp(v->value, "smdi"))
11051 confp->chan.cid_signalling = CID_SIG_SMDI;
11052 else if (!strcasecmp(v->value, "v23_jp"))
11053 confp->chan.cid_signalling = CID_SIG_V23_JP;
11054 else if (ast_true(v->value))
11055 confp->chan.cid_signalling = CID_SIG_BELL;
11056 } else if (!strcasecmp(v->name, "cidstart")) {
11057 if (!strcasecmp(v->value, "ring"))
11058 confp->chan.cid_start = CID_START_RING;
11059 else if (!strcasecmp(v->value, "polarity"))
11060 confp->chan.cid_start = CID_START_POLARITY;
11061 else if (ast_true(v->value))
11062 confp->chan.cid_start = CID_START_RING;
11063 } else if (!strcasecmp(v->name, "threewaycalling")) {
11064 confp->chan.threewaycalling = ast_true(v->value);
11065 } else if (!strcasecmp(v->name, "cancallforward")) {
11066 confp->chan.cancallforward = ast_true(v->value);
11067 } else if (!strcasecmp(v->name, "relaxdtmf")) {
11068 if (ast_true(v->value))
11069 confp->chan.dtmfrelax = DSP_DIGITMODE_RELAXDTMF;
11070 else
11071 confp->chan.dtmfrelax = 0;
11072 } else if (!strcasecmp(v->name, "mailbox")) {
11073 ast_copy_string(confp->chan.mailbox, v->value, sizeof(confp->chan.mailbox));
11074 } else if (!strcasecmp(v->name, "hasvoicemail")) {
11075 if (ast_true(v->value) && ast_strlen_zero(confp->chan.mailbox)) {
11076 ast_copy_string(confp->chan.mailbox, cat, sizeof(confp->chan.mailbox));
11077 }
11078 } else if (!strcasecmp(v->name, "adsi")) {
11079 confp->chan.adsi = ast_true(v->value);
11080 } else if (!strcasecmp(v->name, "usesmdi")) {
11081 confp->chan.use_smdi = ast_true(v->value);
11082 } else if (!strcasecmp(v->name, "smdiport")) {
11083 ast_copy_string(confp->smdi_port, v->value, sizeof(confp->smdi_port));
11084 } else if (!strcasecmp(v->name, "transfer")) {
11085 confp->chan.transfer = ast_true(v->value);
11086 } else if (!strcasecmp(v->name, "canpark")) {
11087 confp->chan.canpark = ast_true(v->value);
11088 } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
11089 confp->chan.echocanbridged = ast_true(v->value);
11090 } else if (!strcasecmp(v->name, "busydetect")) {
11091 confp->chan.busydetect = ast_true(v->value);
11092 } else if (!strcasecmp(v->name, "busycount")) {
11093 confp->chan.busycount = atoi(v->value);
11094 } else if (!strcasecmp(v->name, "busypattern")) {
11095 if (sscanf(v->value, "%d,%d", &confp->chan.busy_tonelength, &confp->chan.busy_quietlength) != 2) {
11096 ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength\n");
11097 }
11098 } else if (!strcasecmp(v->name, "callprogress")) {
11099 if (ast_true(v->value))
11100 confp->chan.callprogress |= 1;
11101 else
11102 confp->chan.callprogress &= ~1;
11103 } else if (!strcasecmp(v->name, "faxdetect")) {
11104 if (!strcasecmp(v->value, "incoming")) {
11105 confp->chan.callprogress |= 4;
11106 confp->chan.callprogress &= ~2;
11107 } else if (!strcasecmp(v->value, "outgoing")) {
11108 confp->chan.callprogress &= ~4;
11109 confp->chan.callprogress |= 2;
11110 } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
11111 confp->chan.callprogress |= 6;
11112 else
11113 confp->chan.callprogress &= ~6;
11114 } else if (!strcasecmp(v->name, "echocancel")) {
11115 if (!ast_strlen_zero(v->value)) {
11116 y = atoi(v->value);
11117 } else
11118 y = 0;
11119 if ((y == 32) || (y == 64) || (y == 128) || (y == 256) || (y == 512) || (y == 1024))
11120 confp->chan.echocancel = y;
11121 else {
11122 confp->chan.echocancel = ast_true(v->value);
11123 if (confp->chan.echocancel)
11124 confp->chan.echocancel=128;
11125 }
11126 } else if (!strcasecmp(v->name, "echotraining")) {
11127 if (sscanf(v->value, "%d", &y) == 1) {
11128 if ((y < 10) || (y > 4000)) {
11129 ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 4000 ms at line %d\n", v->lineno);
11130 } else {
11131 confp->chan.echotraining = y;
11132 }
11133 } else if (ast_true(v->value)) {
11134 confp->chan.echotraining = 400;
11135 } else
11136 confp->chan.echotraining = 0;
11137 } else if (!strcasecmp(v->name, "hidecallerid")) {
11138 confp->chan.hidecallerid = ast_true(v->value);
11139 } else if (!strcasecmp(v->name, "hidecalleridname")) {
11140 confp->chan.hidecalleridname = ast_true(v->value);
11141 } else if (!strcasecmp(v->name, "pulsedial")) {
11142 confp->chan.pulse = ast_true(v->value);
11143 } else if (!strcasecmp(v->name, "callreturn")) {
11144 confp->chan.callreturn = ast_true(v->value);
11145 } else if (!strcasecmp(v->name, "callwaiting")) {
11146 confp->chan.callwaiting = ast_true(v->value);
11147 } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
11148 confp->chan.callwaitingcallerid = ast_true(v->value);
11149 } else if (!strcasecmp(v->name, "context")) {
11150 ast_copy_string(confp->chan.context, v->value, sizeof(confp->chan.context));
11151 } else if (!strcasecmp(v->name, "language")) {
11152 ast_copy_string(confp->chan.language, v->value, sizeof(confp->chan.language));
11153 } else if (!strcasecmp(v->name, "progzone")) {
11154 ast_copy_string(progzone, v->value, sizeof(progzone));
11155 } else if (!strcasecmp(v->name, "mohinterpret")
11156 ||!strcasecmp(v->name, "musiconhold") || !strcasecmp(v->name, "musicclass")) {
11157 ast_copy_string(confp->chan.mohinterpret, v->value, sizeof(confp->chan.mohinterpret));
11158 } else if (!strcasecmp(v->name, "mohsuggest")) {
11159 ast_copy_string(confp->chan.mohsuggest, v->value, sizeof(confp->chan.mohsuggest));
11160 } else if (!strcasecmp(v->name, "stripmsd")) {
11161 confp->chan.stripmsd = atoi(v->value);
11162 } else if (!strcasecmp(v->name, "jitterbuffers")) {
11163 numbufs = atoi(v->value);
11164 } else if (!strcasecmp(v->name, "group")) {
11165 confp->chan.group = ast_get_group(v->value);
11166 } else if (!strcasecmp(v->name, "callgroup")) {
11167 confp->chan.callgroup = ast_get_group(v->value);
11168 } else if (!strcasecmp(v->name, "pickupgroup")) {
11169 confp->chan.pickupgroup = ast_get_group(v->value);
11170 } else if (!strcasecmp(v->name, "immediate")) {
11171 confp->chan.immediate = ast_true(v->value);
11172 } else if (!strcasecmp(v->name, "transfertobusy")) {
11173 confp->chan.transfertobusy = ast_true(v->value);
11174 } else if (!strcasecmp(v->name, "rxgain")) {
11175 if (sscanf(v->value, "%f", &confp->chan.rxgain) != 1) {
11176 ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value);
11177 }
11178 } else if (!strcasecmp(v->name, "txgain")) {
11179 if (sscanf(v->value, "%f", &confp->chan.txgain) != 1) {
11180 ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value);
11181 }
11182 } else if (!strcasecmp(v->name, "tonezone")) {
11183 if (sscanf(v->value, "%d", &confp->chan.tonezone) != 1) {
11184 ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
11185 }
11186 } else if (!strcasecmp(v->name, "callerid")) {
11187 if (!strcasecmp(v->value, "asreceived")) {
11188 confp->chan.cid_num[0] = '\0';
11189 confp->chan.cid_name[0] = '\0';
11190 } else {
11191 ast_callerid_split(v->value, confp->chan.cid_name, sizeof(confp->chan.cid_name), confp->chan.cid_num, sizeof(confp->chan.cid_num));
11192 }
11193 } else if (!strcasecmp(v->name, "fullname")) {
11194 ast_copy_string(confp->chan.cid_name, v->value, sizeof(confp->chan.cid_name));
11195 } else if (!strcasecmp(v->name, "cid_number")) {
11196 ast_copy_string(confp->chan.cid_num, v->value, sizeof(confp->chan.cid_num));
11197 } else if (!strcasecmp(v->name, "useincomingcalleridondahditransfer") || !strcasecmp(v->name, "useincomingcalleridonzaptransfer")) {
11198 confp->chan.dahditrcallerid = ast_true(v->value);
11199 if (strstr(v->name, "zap")) {
11200 ast_log(LOG_WARNING, "Option useincomingcalleridonzaptransfer has been deprecated in favor of useincomingcalleridondahditransfer (in [%s]).\n", cat);
11201 }
11202 } else if (!strcasecmp(v->name, "restrictcid")) {
11203 confp->chan.restrictcid = ast_true(v->value);
11204 } else if (!strcasecmp(v->name, "usecallingpres")) {
11205 confp->chan.use_callingpres = ast_true(v->value);
11206 } else if (!strcasecmp(v->name, "accountcode")) {
11207 ast_copy_string(confp->chan.accountcode, v->value, sizeof(confp->chan.accountcode));
11208 } else if (!strcasecmp(v->name, "amaflags")) {
11209 y = ast_cdr_amaflags2int(v->value);
11210 if (y < 0)
11211 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
11212 else
11213 confp->chan.amaflags = y;
11214 } else if (!strcasecmp(v->name, "polarityonanswerdelay")) {
11215 confp->chan.polarityonanswerdelay = atoi(v->value);
11216 } else if (!strcasecmp(v->name, "answeronpolarityswitch")) {
11217 confp->chan.answeronpolarityswitch = ast_true(v->value);
11218 } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) {
11219 confp->chan.hanguponpolarityswitch = ast_true(v->value);
11220 } else if (!strcasecmp(v->name, "sendcalleridafter")) {
11221 confp->chan.sendcalleridafter = atoi(v->value);
11222 } else if (reload != 1){
11223 if (!strcasecmp(v->name, "signalling")) {
11224 confp->chan.outsigmod = -1;
11225 if (!strcasecmp(v->value, "em")) {
11226 confp->chan.sig = SIG_EM;
11227 } else if (!strcasecmp(v->value, "em_e1")) {
11228 confp->chan.sig = SIG_EM_E1;
11229 } else if (!strcasecmp(v->value, "em_w")) {
11230 confp->chan.sig = SIG_EMWINK;
11231 confp->chan.radio = 0;
11232 } else if (!strcasecmp(v->value, "fxs_ls")) {
11233 confp->chan.sig = SIG_FXSLS;
11234 confp->chan.radio = 0;
11235 } else if (!strcasecmp(v->value, "fxs_gs")) {
11236 confp->chan.sig = SIG_FXSGS;
11237 confp->chan.radio = 0;
11238 } else if (!strcasecmp(v->value, "fxs_ks")) {
11239 confp->chan.sig = SIG_FXSKS;
11240 confp->chan.radio = 0;
11241 } else if (!strcasecmp(v->value, "fxo_ls")) {
11242 confp->chan.sig = SIG_FXOLS;
11243 confp->chan.radio = 0;
11244 } else if (!strcasecmp(v->value, "fxo_gs")) {
11245 confp->chan.sig = SIG_FXOGS;
11246 confp->chan.radio = 0;
11247 } else if (!strcasecmp(v->value, "fxo_ks")) {
11248 confp->chan.sig = SIG_FXOKS;
11249 confp->chan.radio = 0;
11250 } else if (!strcasecmp(v->value, "fxs_rx")) {
11251 confp->chan.sig = SIG_FXSKS;
11252 confp->chan.radio = 1;
11253 } else if (!strcasecmp(v->value, "fxo_rx")) {
11254 confp->chan.sig = SIG_FXOLS;
11255 confp->chan.radio = 1;
11256 } else if (!strcasecmp(v->value, "fxs_tx")) {
11257 confp->chan.sig = SIG_FXSLS;
11258 confp->chan.radio = 1;
11259 } else if (!strcasecmp(v->value, "fxo_tx")) {
11260 confp->chan.sig = SIG_FXOGS;
11261 confp->chan.radio = 1;
11262 } else if (!strcasecmp(v->value, "em_rx")) {
11263 confp->chan.sig = SIG_EM;
11264 confp->chan.radio = 1;
11265 } else if (!strcasecmp(v->value, "em_tx")) {
11266 confp->chan.sig = SIG_EM;
11267 confp->chan.radio = 1;
11268 } else if (!strcasecmp(v->value, "em_rxtx")) {
11269 confp->chan.sig = SIG_EM;
11270 confp->chan.radio = 2;
11271 } else if (!strcasecmp(v->value, "em_txrx")) {
11272 confp->chan.sig = SIG_EM;
11273 confp->chan.radio = 2;
11274 } else if (!strcasecmp(v->value, "sf")) {
11275 confp->chan.sig = SIG_SF;
11276 confp->chan.radio = 0;
11277 } else if (!strcasecmp(v->value, "sf_w")) {
11278 confp->chan.sig = SIG_SFWINK;
11279 confp->chan.radio = 0;
11280 } else if (!strcasecmp(v->value, "sf_featd")) {
11281 confp->chan.sig = SIG_FEATD;
11282 confp->chan.radio = 0;
11283 } else if (!strcasecmp(v->value, "sf_featdmf")) {
11284 confp->chan.sig = SIG_FEATDMF;
11285 confp->chan.radio = 0;
11286 } else if (!strcasecmp(v->value, "sf_featb")) {
11287 confp->chan.sig = SIG_SF_FEATB;
11288 confp->chan.radio = 0;
11289 } else if (!strcasecmp(v->value, "sf")) {
11290 confp->chan.sig = SIG_SF;
11291 confp->chan.radio = 0;
11292 } else if (!strcasecmp(v->value, "sf_rx")) {
11293 confp->chan.sig = SIG_SF;
11294 confp->chan.radio = 1;
11295 } else if (!strcasecmp(v->value, "sf_tx")) {
11296 confp->chan.sig = SIG_SF;
11297 confp->chan.radio = 1;
11298 } else if (!strcasecmp(v->value, "sf_rxtx")) {
11299 confp->chan.sig = SIG_SF;
11300 confp->chan.radio = 2;
11301 } else if (!strcasecmp(v->value, "sf_txrx")) {
11302 confp->chan.sig = SIG_SF;
11303 confp->chan.radio = 2;
11304 } else if (!strcasecmp(v->value, "featd")) {
11305 confp->chan.sig = SIG_FEATD;
11306 confp->chan.radio = 0;
11307 } else if (!strcasecmp(v->value, "featdmf")) {
11308 confp->chan.sig = SIG_FEATDMF;
11309 confp->chan.radio = 0;
11310 } else if (!strcasecmp(v->value, "featdmf_ta")) {
11311 confp->chan.sig = SIG_FEATDMF_TA;
11312 confp->chan.radio = 0;
11313 } else if (!strcasecmp(v->value, "e911")) {
11314 confp->chan.sig = SIG_E911;
11315 confp->chan.radio = 0;
11316 } else if (!strcasecmp(v->value, "fgccama")) {
11317 confp->chan.sig = SIG_FGC_CAMA;
11318 confp->chan.radio = 0;
11319 } else if (!strcasecmp(v->value, "fgccamamf")) {
11320 confp->chan.sig = SIG_FGC_CAMAMF;
11321 confp->chan.radio = 0;
11322 } else if (!strcasecmp(v->value, "featb")) {
11323 confp->chan.sig = SIG_FEATB;
11324 confp->chan.radio = 0;
11325 #ifdef HAVE_PRI
11326 } else if (!strcasecmp(v->value, "pri_net")) {
11327 confp->chan.radio = 0;
11328 confp->chan.sig = SIG_PRI;
11329 confp->pri.nodetype = PRI_NETWORK;
11330 } else if (!strcasecmp(v->value, "pri_cpe")) {
11331 confp->chan.sig = SIG_PRI;
11332 confp->chan.radio = 0;
11333 confp->pri.nodetype = PRI_CPE;
11334 } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
11335 confp->chan.sig = SIG_GR303FXOKS;
11336 confp->chan.radio = 0;
11337 confp->pri.nodetype = PRI_NETWORK;
11338 } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
11339 confp->chan.sig = SIG_GR303FXSKS;
11340 confp->chan.radio = 0;
11341 confp->pri.nodetype = PRI_CPE;
11342 #endif
11343 } else {
11344 ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
11345 }
11346 } else if (!strcasecmp(v->name, "outsignalling")) {
11347 if (!strcasecmp(v->value, "em")) {
11348 confp->chan.outsigmod = SIG_EM;
11349 } else if (!strcasecmp(v->value, "em_e1")) {
11350 confp->chan.outsigmod = SIG_EM_E1;
11351 } else if (!strcasecmp(v->value, "em_w")) {
11352 confp->chan.outsigmod = SIG_EMWINK;
11353 } else if (!strcasecmp(v->value, "sf")) {
11354 confp->chan.outsigmod = SIG_SF;
11355 } else if (!strcasecmp(v->value, "sf_w")) {
11356 confp->chan.outsigmod = SIG_SFWINK;
11357 } else if (!strcasecmp(v->value, "sf_featd")) {
11358 confp->chan.outsigmod = SIG_FEATD;
11359 } else if (!strcasecmp(v->value, "sf_featdmf")) {
11360 confp->chan.outsigmod = SIG_FEATDMF;
11361 } else if (!strcasecmp(v->value, "sf_featb")) {
11362 confp->chan.outsigmod = SIG_SF_FEATB;
11363 } else if (!strcasecmp(v->value, "sf")) {
11364 confp->chan.outsigmod = SIG_SF;
11365 } else if (!strcasecmp(v->value, "featd")) {
11366 confp->chan.outsigmod = SIG_FEATD;
11367 } else if (!strcasecmp(v->value, "featdmf")) {
11368 confp->chan.outsigmod = SIG_FEATDMF;
11369 } else if (!strcasecmp(v->value, "featdmf_ta")) {
11370 confp->chan.outsigmod = SIG_FEATDMF_TA;
11371 } else if (!strcasecmp(v->value, "e911")) {
11372 confp->chan.outsigmod = SIG_E911;
11373 } else if (!strcasecmp(v->value, "fgccama")) {
11374 confp->chan.outsigmod = SIG_FGC_CAMA;
11375 } else if (!strcasecmp(v->value, "fgccamamf")) {
11376 confp->chan.outsigmod = SIG_FGC_CAMAMF;
11377 } else if (!strcasecmp(v->value, "featb")) {
11378 confp->chan.outsigmod = SIG_FEATB;
11379 } else {
11380 ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
11381 }
11382 #ifdef HAVE_PRI
11383 } else if (!strcasecmp(v->name, "pridialplan")) {
11384 if (!strcasecmp(v->value, "national")) {
11385 confp->pri.dialplan = PRI_NATIONAL_ISDN + 1;
11386 } else if (!strcasecmp(v->value, "unknown")) {
11387 confp->pri.dialplan = PRI_UNKNOWN + 1;
11388 } else if (!strcasecmp(v->value, "private")) {
11389 confp->pri.dialplan = PRI_PRIVATE + 1;
11390 } else if (!strcasecmp(v->value, "international")) {
11391 confp->pri.dialplan = PRI_INTERNATIONAL_ISDN + 1;
11392 } else if (!strcasecmp(v->value, "local")) {
11393 confp->pri.dialplan = PRI_LOCAL_ISDN + 1;
11394 } else if (!strcasecmp(v->value, "dynamic")) {
11395 confp->pri.dialplan = -1;
11396 } else {
11397 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
11398 }
11399 } else if (!strcasecmp(v->name, "prilocaldialplan")) {
11400 if (!strcasecmp(v->value, "national")) {
11401 confp->pri.localdialplan = PRI_NATIONAL_ISDN + 1;
11402 } else if (!strcasecmp(v->value, "unknown")) {
11403 confp->pri.localdialplan = PRI_UNKNOWN + 1;
11404 } else if (!strcasecmp(v->value, "private")) {
11405 confp->pri.localdialplan = PRI_PRIVATE + 1;
11406 } else if (!strcasecmp(v->value, "international")) {
11407 confp->pri.localdialplan = PRI_INTERNATIONAL_ISDN + 1;
11408 } else if (!strcasecmp(v->value, "local")) {
11409 confp->pri.localdialplan = PRI_LOCAL_ISDN + 1;
11410 } else if (!strcasecmp(v->value, "dynamic")) {
11411 confp->pri.localdialplan = -1;
11412 } else {
11413 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
11414 }
11415 } else if (!strcasecmp(v->name, "switchtype")) {
11416 if (!strcasecmp(v->value, "national"))
11417 confp->pri.switchtype = PRI_SWITCH_NI2;
11418 else if (!strcasecmp(v->value, "ni1"))
11419 confp->pri.switchtype = PRI_SWITCH_NI1;
11420 else if (!strcasecmp(v->value, "dms100"))
11421 confp->pri.switchtype = PRI_SWITCH_DMS100;
11422 else if (!strcasecmp(v->value, "4ess"))
11423 confp->pri.switchtype = PRI_SWITCH_ATT4ESS;
11424 else if (!strcasecmp(v->value, "5ess"))
11425 confp->pri.switchtype = PRI_SWITCH_LUCENT5E;
11426 else if (!strcasecmp(v->value, "euroisdn"))
11427 confp->pri.switchtype = PRI_SWITCH_EUROISDN_E1;
11428 else if (!strcasecmp(v->value, "qsig"))
11429 confp->pri.switchtype = PRI_SWITCH_QSIG;
11430 else {
11431 ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value);
11432 return -1;
11433 }
11434 } else if (!strcasecmp(v->name, "nsf")) {
11435 if (!strcasecmp(v->value, "sdn"))
11436 confp->pri.nsf = PRI_NSF_SDN;
11437 else if (!strcasecmp(v->value, "megacom"))
11438 confp->pri.nsf = PRI_NSF_MEGACOM;
11439 else if (!strcasecmp(v->value, "tollfreemegacom"))
11440 confp->pri.nsf = PRI_NSF_TOLL_FREE_MEGACOM;
11441 else if (!strcasecmp(v->value, "accunet"))
11442 confp->pri.nsf = PRI_NSF_ACCUNET;
11443 else if (!strcasecmp(v->value, "none"))
11444 confp->pri.nsf = PRI_NSF_NONE;
11445 else {
11446 ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value);
11447 confp->pri.nsf = PRI_NSF_NONE;
11448 }
11449 } else if (!strcasecmp(v->name, "priindication")) {
11450 if (!strcasecmp(v->value, "outofband"))
11451 confp->chan.priindication_oob = 1;
11452 else if (!strcasecmp(v->value, "inband"))
11453 confp->chan.priindication_oob = 0;
11454 else
11455 ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n",
11456 v->value, v->lineno);
11457 } else if (!strcasecmp(v->name, "priexclusive")) {
11458 confp->chan.priexclusive = ast_true(v->value);
11459 } else if (!strcasecmp(v->name, "internationalprefix")) {
11460 ast_copy_string(confp->pri.internationalprefix, v->value, sizeof(confp->pri.internationalprefix));
11461 } else if (!strcasecmp(v->name, "nationalprefix")) {
11462 ast_copy_string(confp->pri.nationalprefix, v->value, sizeof(confp->pri.nationalprefix));
11463 } else if (!strcasecmp(v->name, "localprefix")) {
11464 ast_copy_string(confp->pri.localprefix, v->value, sizeof(confp->pri.localprefix));
11465 } else if (!strcasecmp(v->name, "privateprefix")) {
11466 ast_copy_string(confp->pri.privateprefix, v->value, sizeof(confp->pri.privateprefix));
11467 } else if (!strcasecmp(v->name, "unknownprefix")) {
11468 ast_copy_string(confp->pri.unknownprefix, v->value, sizeof(confp->pri.unknownprefix));
11469 } else if (!strcasecmp(v->name, "resetinterval")) {
11470 if (!strcasecmp(v->value, "never"))
11471 confp->pri.resetinterval = -1;
11472 else if (atoi(v->value) >= 60)
11473 confp->pri.resetinterval = atoi(v->value);
11474 else
11475 ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d\n",
11476 v->value, v->lineno);
11477 } else if (!strcasecmp(v->name, "minunused")) {
11478 confp->pri.minunused = atoi(v->value);
11479 } else if (!strcasecmp(v->name, "minidle")) {
11480 confp->pri.minidle = atoi(v->value);
11481 } else if (!strcasecmp(v->name, "idleext")) {
11482 ast_copy_string(confp->pri.idleext, v->value, sizeof(confp->pri.idleext));
11483 } else if (!strcasecmp(v->name, "idledial")) {
11484 ast_copy_string(confp->pri.idledial, v->value, sizeof(confp->pri.idledial));
11485 } else if (!strcasecmp(v->name, "overlapdial")) {
11486 confp->pri.overlapdial = ast_true(v->value);
11487 #ifdef HAVE_PRI_INBANDDISCONNECT
11488 } else if (!strcasecmp(v->name, "inbanddisconnect")) {
11489 confp->pri.inbanddisconnect = ast_true(v->value);
11490 #endif
11491 } else if (!strcasecmp(v->name, "pritimer")) {
11492 #ifdef PRI_GETSET_TIMERS
11493 char *timerc, *c;
11494 int timer, timeridx;
11495 c = v->value;
11496 timerc = strsep(&c, ",");
11497 if (timerc) {
11498 timer = atoi(c);
11499 if (!timer)
11500 ast_log(LOG_WARNING, "'%s' is not a valid value for an ISDN timer\n", timerc);
11501 else {
11502 if ((timeridx = pri_timer2idx(timerc)) >= 0)
11503 pritimers[timeridx] = timer;
11504 else
11505 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer\n", timerc);
11506 }
11507 } else
11508 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer configuration string\n", v->value);
11509
11510 } else if (!strcasecmp(v->name, "facilityenable")) {
11511 confp->pri.facilityenable = ast_true(v->value);
11512 #endif
11513 #endif
11514 } else if (!strcasecmp(v->name, "cadence")) {
11515
11516 int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
11517 int i;
11518 struct dahdi_ring_cadence new_cadence;
11519 int cid_location = -1;
11520 int firstcadencepos = 0;
11521 char original_args[80];
11522 int cadence_is_ok = 1;
11523
11524 ast_copy_string(original_args, v->value, sizeof(original_args));
11525
11526 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]);
11527
11528
11529 if (element_count % 2 == 1) {
11530 ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args);
11531 cadence_is_ok = 0;
11532 }
11533
11534
11535 for (i = 0; i < element_count; i++) {
11536 if (c[i] == 0) {
11537 ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s\n", original_args);
11538 cadence_is_ok = 0;
11539 break;
11540 } else if (c[i] < 0) {
11541 if (i % 2 == 1) {
11542
11543 if (cid_location == -1) {
11544 cid_location = i;
11545 c[i] *= -1;
11546 } else {
11547 ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args);
11548 cadence_is_ok = 0;
11549 break;
11550 }
11551 } else {
11552 if (firstcadencepos == 0) {
11553 firstcadencepos = i;
11554
11555 } else {
11556 ast_log(LOG_ERROR, "First cadence position specified twice: %s\n",original_args);
11557 cadence_is_ok = 0;
11558 break;
11559 }
11560 }
11561 }
11562 }
11563
11564
11565 for (i = 0; i < 16; i++) {
11566 new_cadence.ringcadence[i] = c[i];
11567 }
11568
11569 if (cadence_is_ok) {
11570
11571 if (element_count < 2) {
11572 ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args);
11573 } else {
11574 if (cid_location == -1) {
11575
11576 cid_location = 1;
11577 } else {
11578
11579 cid_location = (cid_location + 1) / 2;
11580 }
11581
11582 if (!user_has_defined_cadences++)
11583
11584 num_cadence = 0;
11585 if ((num_cadence+1) >= NUM_CADENCE_MAX)
11586 ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args);
11587 else {
11588 cadences[num_cadence] = new_cadence;
11589 cidrings[num_cadence++] = cid_location;
11590 if (option_verbose > 2)
11591 ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args);
11592 }
11593 }
11594 }
11595 } else if (!strcasecmp(v->name, "ringtimeout")) {
11596 ringt_base = (atoi(v->value) * 8) / READ_SIZE;
11597 } else if (!strcasecmp(v->name, "prewink")) {
11598 confp->timing.prewinktime = atoi(v->value);
11599 } else if (!strcasecmp(v->name, "preflash")) {
11600 confp->timing.preflashtime = atoi(v->value);
11601 } else if (!strcasecmp(v->name, "wink")) {
11602 confp->timing.winktime = atoi(v->value);
11603 } else if (!strcasecmp(v->name, "flash")) {
11604 confp->timing.flashtime = atoi(v->value);
11605 } else if (!strcasecmp(v->name, "start")) {
11606 confp->timing.starttime = atoi(v->value);
11607 } else if (!strcasecmp(v->name, "rxwink")) {
11608 confp->timing.rxwinktime = atoi(v->value);
11609 } else if (!strcasecmp(v->name, "rxflash")) {
11610 confp->timing.rxflashtime = atoi(v->value);
11611 } else if (!strcasecmp(v->name, "debounce")) {
11612 confp->timing.debouncetime = atoi(v->value);
11613 } else if (!strcasecmp(v->name, "toneduration")) {
11614 int toneduration;
11615 int ctlfd;
11616 int res;
11617 struct dahdi_dialparams dps;
11618
11619 #ifdef HAVE_ZAPTEL
11620 ctlfd = open("/dev/zap/ctl", O_RDWR);
11621 #else
11622 ctlfd = open("/dev/dahdi/ctl", O_RDWR);
11623 #endif
11624
11625 if (ctlfd == -1) {
11626 ast_log(LOG_ERROR, "Unable to open /dev/dahdi/ctl to set toneduration\n");
11627 return -1;
11628 }
11629
11630 toneduration = atoi(v->value);
11631 if (toneduration > -1) {
11632 memset(&dps, 0, sizeof(dps));
11633
11634 dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration;
11635 res = ioctl(ctlfd, DAHDI_SET_DIALPARAMS, &dps);
11636 if (res < 0) {
11637 ast_log(LOG_ERROR, "Invalid tone duration: %d ms: %s\n", toneduration, strerror(errno));
11638 return -1;
11639 }
11640 }
11641 close(ctlfd);
11642 } else if (!strcasecmp(v->name, "defaultcic")) {
11643 ast_copy_string(defaultcic, v->value, sizeof(defaultcic));
11644 } else if (!strcasecmp(v->name, "defaultozz")) {
11645 ast_copy_string(defaultozz, v->value, sizeof(defaultozz));
11646 }
11647 } else if (!skipchannels)
11648 ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
11649 }
11650 if (dahdichan[0]) {
11651
11652
11653 if (build_channels(confp, 0, dahdichan, reload, 0, &found_pseudo)) {
11654 return -1;
11655 }
11656 }
11657
11658
11659 if (!found_pseudo && reload == 0) {
11660
11661
11662
11663
11664 struct dahdi_chan_conf conf = dahdi_chan_conf_default();
11665
11666 tmp = mkintf(CHAN_PSEUDO, &conf, NULL, reload);
11667
11668 if (tmp) {
11669 if (option_verbose > 2)
11670 ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n");
11671 } else {
11672 ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
11673 }
11674 }
11675 return 0;
11676 }
11677
11678 static int setup_dahdi(int reload)
11679 {
11680 struct ast_config *cfg;
11681 struct ast_variable *v;
11682 struct dahdi_chan_conf conf = dahdi_chan_conf_default();
11683 int res;
11684
11685 #ifdef HAVE_PRI
11686 char *c;
11687 int spanno;
11688 int i, x;
11689 int logicalspan;
11690 int trunkgroup;
11691 int dchannels[NUM_DCHANS];
11692 #endif
11693
11694 #ifdef HAVE_ZAPTEL
11695 int load_from_zapata_conf = 1;
11696 #else
11697 int load_from_zapata_conf = (dahdi_chan_mode == CHAN_ZAP_MODE);
11698 #endif
11699
11700 if (load_from_zapata_conf) {
11701 if (!(cfg = ast_config_load("zapata.conf"))) {
11702 ast_log(LOG_ERROR, "Unable to load zapata.conf\n");
11703 return 0;
11704 }
11705 } else {
11706 if (!(cfg = ast_config_load("chan_dahdi.conf"))) {
11707 ast_log(LOG_ERROR, "Unable to load chan_dahdi.conf\n");
11708 return 0;
11709 }
11710 }
11711
11712
11713 ast_mutex_lock(&iflock);
11714 #ifdef HAVE_PRI
11715 if (reload != 1) {
11716
11717 v = ast_variable_browse(cfg, "trunkgroups");
11718 while (v) {
11719 if (!strcasecmp(v->name, "trunkgroup")) {
11720 trunkgroup = atoi(v->value);
11721 if (trunkgroup > 0) {
11722 if ((c = strchr(v->value, ','))) {
11723 i = 0;
11724 memset(dchannels, 0, sizeof(dchannels));
11725 while (c && (i < NUM_DCHANS)) {
11726 dchannels[i] = atoi(c + 1);
11727 if (dchannels[i] < 0) {
11728 ast_log(LOG_WARNING, "D-channel for trunk group %d must be a postiive number at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno);
11729 } else
11730 i++;
11731 c = strchr(c + 1, ',');
11732 }
11733 if (i) {
11734 if (pri_create_trunkgroup(trunkgroup, dchannels)) {
11735 ast_log(LOG_WARNING, "Unable to create trunk group %d with Primary D-channel %d at line %d of chan_dahdi.conf\n", trunkgroup, dchannels[0], v->lineno);
11736 } else if (option_verbose > 1)
11737 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");
11738 } else
11739 ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno);
11740 } else
11741 ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno);
11742 } else
11743 ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of chan_dahdi.conf\n", v->lineno);
11744 } else if (!strcasecmp(v->name, "spanmap")) {
11745 spanno = atoi(v->value);
11746 if (spanno > 0) {
11747 if ((c = strchr(v->value, ','))) {
11748 trunkgroup = atoi(c + 1);
11749 if (trunkgroup > 0) {
11750 if ((c = strchr(c + 1, ',')))
11751 logicalspan = atoi(c + 1);
11752 else
11753 logicalspan = 0;
11754 if (logicalspan >= 0) {
11755 if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
11756 ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
11757 } else if (option_verbose > 1)
11758 ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
11759 } else
11760 ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of chan_dahdi.conf\n", v->lineno);
11761 } else
11762 ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of chan_dahdi.conf\n", v->lineno);
11763 } else
11764 ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of chan_dahdi.conf\n", v->lineno);
11765 } else
11766 ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of chan_dahdi.conf\n", v->lineno);
11767 } else {
11768 ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
11769 }
11770 v = v->next;
11771 }
11772 }
11773 #endif
11774
11775
11776 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
11777
11778 v = ast_variable_browse(cfg, "channels");
11779 res = process_dahdi(&conf, "", v, reload, 0);
11780 ast_mutex_unlock(&iflock);
11781 ast_config_destroy(cfg);
11782 if (res)
11783 return res;
11784 cfg = ast_config_load("users.conf");
11785 if (cfg) {
11786 char *cat;
11787 const char *chans;
11788 process_dahdi(&conf, "", ast_variable_browse(cfg, "general"), 1, 1);
11789 for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
11790 if (!strcasecmp(cat, "general"))
11791 continue;
11792 chans = ast_variable_retrieve(cfg, cat, "dahdichan");
11793 if (!ast_strlen_zero(chans)) {
11794 struct dahdi_chan_conf sect_conf;
11795 memcpy(§_conf, &conf, sizeof(sect_conf));
11796
11797 process_dahdi(§_conf, cat, ast_variable_browse(cfg, cat), reload, 0);
11798 }
11799 }
11800 ast_config_destroy(cfg);
11801 }
11802 #ifdef HAVE_PRI
11803 if (reload != 1) {
11804 for (x = 0; x < NUM_SPANS; x++) {
11805 if (pris[x].pvts[0]) {
11806 if (start_pri(pris + x)) {
11807 ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
11808 return -1;
11809 } else if (option_verbose > 1)
11810 ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1);
11811 }
11812 }
11813 }
11814 #endif
11815
11816 restart_monitor();
11817 return 0;
11818 }
11819
11820 #define local_astman_register(a, b, c, d) do { \
11821 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) { \
11822 ast_manager_register("DAHDI" a, b, dahdi_ ## c, d); \
11823 } \
11824 ast_manager_register("Zap" a, b, zap_ ## c, d); \
11825 } while (0)
11826
11827 static int load_module(void)
11828 {
11829 int res;
11830
11831 #ifdef HAVE_PRI
11832 int y,i;
11833 memset(pris, 0, sizeof(pris));
11834 for (y = 0; y < NUM_SPANS; y++) {
11835 ast_mutex_init(&pris[y].lock);
11836 pris[y].offset = -1;
11837 pris[y].master = AST_PTHREADT_NULL;
11838 for (i = 0; i < NUM_DCHANS; i++)
11839 pris[y].fds[i] = -1;
11840 }
11841 pri_set_error(dahdi_pri_error);
11842 pri_set_message(dahdi_pri_message);
11843 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) {
11844 ast_register_application(dahdi_send_keypad_facility_app, dahdi_send_keypad_facility_exec,
11845 dahdi_send_keypad_facility_synopsis, dahdi_send_keypad_facility_descrip);
11846 }
11847 ast_register_application(zap_send_keypad_facility_app, zap_send_keypad_facility_exec,
11848 zap_send_keypad_facility_synopsis, zap_send_keypad_facility_descrip);
11849 #endif
11850 if ((res = setup_dahdi(0))) {
11851 return AST_MODULE_LOAD_DECLINE;
11852 }
11853 if (*dahdi_chan_mode == CHAN_DAHDI_PLUS_ZAP_MODE) {
11854 chan_tech = &dahdi_tech;
11855 } else {
11856 chan_tech = &zap_tech;
11857 }
11858 if (ast_channel_register(chan_tech)) {
11859 ast_log(LOG_ERROR, "Unable to register channel class '%s'\n", chan_tech->type);
11860 __unload_module();
11861 return -1;
11862 }
11863 #ifdef HAVE_PRI
11864 ast_string_field_init(&inuse, 16);
11865 ast_string_field_set(&inuse, name, "GR-303InUse");
11866 ast_cli_register_multiple(dahdi_pri_cli, sizeof(dahdi_pri_cli) / sizeof(struct ast_cli_entry));
11867 #endif
11868 ast_cli_register_multiple(dahdi_cli, sizeof(dahdi_cli) / sizeof(struct ast_cli_entry));
11869
11870 memset(round_robin, 0, sizeof(round_robin));
11871 local_astman_register("Transfer", 0, action_transfer, "Transfer Channel");
11872 local_astman_register("Hangup", 0, action_transferhangup, "Hangup Channel");
11873 local_astman_register("DialOffHook", 0, action_dialoffhook, "Dial over channel while offhook");
11874 local_astman_register("DNDon", 0, action_dndon, "Toggle channel Do Not Disturb status ON");
11875 local_astman_register("DNDoff", 0, action_dndoff, "Toggle channel Do Not Disturb status OFF");
11876 local_astman_register("ShowChannels", 0, action_showchannels, "Show status channels");
11877 local_astman_register("Restart", 0, action_restart, "Fully Restart channels (terminates calls)");
11878
11879 ast_cond_init(&ss_thread_complete, NULL);
11880
11881 return res;
11882 }
11883
11884 static int dahdi_sendtext(struct ast_channel *c, const char *text)
11885 {
11886 #define END_SILENCE_LEN 400
11887 #define HEADER_MS 50
11888 #define TRAILER_MS 5
11889 #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
11890 #define ASCII_BYTES_PER_CHAR 80
11891
11892 unsigned char *buf,*mybuf;
11893 struct dahdi_pvt *p = c->tech_pvt;
11894 struct pollfd fds[1];
11895 int size,res,fd,len,x;
11896 int bytes=0;
11897
11898 float cr = 1.0;
11899 float ci = 0.0;
11900 float scont = 0.0;
11901 int index;
11902
11903 index = dahdi_get_index(c, p, 0);
11904 if (index < 0) {
11905 ast_log(LOG_WARNING, "Huh? I don't exist?\n");
11906 return -1;
11907 }
11908 if (!text[0]) return(0);
11909 if ((!p->tdd) && (!p->mate)) return(0);
11910 if (p->mate)
11911 buf = ast_malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
11912 else
11913 buf = ast_malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
11914 if (!buf)
11915 return -1;
11916 mybuf = buf;
11917 if (p->mate) {
11918 int codec = AST_LAW(p);
11919 for (x = 0; x < HEADER_MS; x++) {
11920 PUT_CLID_MARKMS;
11921 }
11922
11923 for (x = 0; text[x]; x++) {
11924 PUT_CLID(text[x]);
11925 }
11926 for (x = 0; x < TRAILER_MS; x++) {
11927 PUT_CLID_MARKMS;
11928 }
11929 len = bytes;
11930 buf = mybuf;
11931 } else {
11932 len = tdd_generate(p->tdd, buf, text);
11933 if (len < 1) {
11934 ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n", (int)strlen(text));
11935 free(mybuf);
11936 return -1;
11937 }
11938 }
11939 memset(buf + len, 0x7f, END_SILENCE_LEN);
11940 len += END_SILENCE_LEN;
11941 fd = p->subs[index].dfd;
11942 while (len) {
11943 if (ast_check_hangup(c)) {
11944 free(mybuf);
11945 return -1;
11946 }
11947 size = len;
11948 if (size > READ_SIZE)
11949 size = READ_SIZE;
11950 fds[0].fd = fd;
11951 fds[0].events = POLLOUT | POLLPRI;
11952 fds[0].revents = 0;
11953 res = poll(fds, 1, -1);
11954 if (!res) {
11955 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
11956 continue;
11957 }
11958
11959 if (fds[0].revents & POLLPRI) {
11960 ast_free(mybuf);
11961 return -1;
11962 }
11963 if (!(fds[0].revents & POLLOUT)) {
11964 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
11965 continue;
11966 }
11967 res = write(fd, buf, size);
11968 if (res != size) {
11969 if (res == -1) {
11970 free(mybuf);
11971 return -1;
11972 }
11973 if (option_debug)
11974 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
11975 break;
11976 }
11977 len -= size;
11978 buf += size;
11979 }
11980 free(mybuf);
11981 return(0);
11982 }
11983
11984
11985 static int reload(void)
11986 {
11987 int res = 0;
11988
11989 res = setup_dahdi(1);
11990 if (res) {
11991 ast_log(LOG_WARNING, "Reload of chan_dahdi.so is unsuccessful!\n");
11992 return -1;
11993 }
11994 return 0;
11995 }
11996
11997
11998
11999
12000
12001 #ifdef HAVE_PRI
12002 #define tdesc "DAHDI Telephony w/PRI"
12003 #else
12004 #define tdesc "DAHDI Telephony"
12005 #endif
12006
12007 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, tdesc,
12008 .load = load_module,
12009 .unload = unload_module,
12010 .reload = reload,
12011 );
12012
12013