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 #include "asterisk.h"
00048
00049 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00050
00051 #include <stdio.h>
00052 #include <string.h>
00053 #ifdef __NetBSD__
00054 #include <pthread.h>
00055 #include <signal.h>
00056 #else
00057 #include <sys/signal.h>
00058 #endif
00059 #include <errno.h>
00060 #include <stdlib.h>
00061 #if !defined(SOLARIS) && !defined(__FreeBSD__)
00062 #include <stdint.h>
00063 #endif
00064 #include <unistd.h>
00065 #include <sys/ioctl.h>
00066 #include <math.h>
00067 #include <ctype.h>
00068 #include <zaptel/zaptel.h>
00069 #include <zaptel/tonezone.h>
00070
00071 #ifdef HAVE_PRI
00072 #include <libpri.h>
00073 #endif
00074
00075 #include "asterisk/lock.h"
00076 #include "asterisk/channel.h"
00077 #include "asterisk/config.h"
00078 #include "asterisk/logger.h"
00079 #include "asterisk/module.h"
00080 #include "asterisk/pbx.h"
00081 #include "asterisk/options.h"
00082 #include "asterisk/file.h"
00083 #include "asterisk/ulaw.h"
00084 #include "asterisk/alaw.h"
00085 #include "asterisk/callerid.h"
00086 #include "asterisk/adsi.h"
00087 #include "asterisk/cli.h"
00088 #include "asterisk/cdr.h"
00089 #include "asterisk/features.h"
00090 #include "asterisk/musiconhold.h"
00091 #include "asterisk/say.h"
00092 #include "asterisk/tdd.h"
00093 #include "asterisk/app.h"
00094 #include "asterisk/dsp.h"
00095 #include "asterisk/astdb.h"
00096 #include "asterisk/manager.h"
00097 #include "asterisk/causes.h"
00098 #include "asterisk/term.h"
00099 #include "asterisk/utils.h"
00100 #include "asterisk/transcap.h"
00101 #include "asterisk/stringfields.h"
00102 #include "asterisk/abstract_jb.h"
00103 #include "asterisk/smdi.h"
00104 #include "asterisk/astobj.h"
00105 #define SMDI_MD_WAIT_TIMEOUT 1500
00106
00107
00108 static struct ast_jb_conf default_jbconf =
00109 {
00110 .flags = 0,
00111 .max_size = -1,
00112 .resync_threshold = -1,
00113 .impl = ""
00114 };
00115 static struct ast_jb_conf global_jbconf;
00116
00117 #if !defined(ZT_SIG_EM_E1) || (defined(HAVE_PRI) && !defined(ZT_SIG_HARDHDLC))
00118 #error "Your zaptel is too old. Please update"
00119 #endif
00120
00121 #ifndef ZT_TONEDETECT
00122
00123 #define ZT_EVENT_DTMFDOWN 0
00124 #define ZT_EVENT_DTMFUP 0
00125 #endif
00126
00127
00128 #undef SUPPORT_USERUSER
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 #define DEFAULT_CIDRINGS 1
00150
00151 #define CHANNEL_PSEUDO -12
00152
00153 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
00154
00155
00156 #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))
00157
00158 static const char tdesc[] = "Zapata Telephony Driver"
00159 #ifdef HAVE_PRI
00160 " w/PRI"
00161 #endif
00162 ;
00163
00164 static const char config[] = "zapata.conf";
00165
00166 #define SIG_EM ZT_SIG_EM
00167 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
00168 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
00169 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
00170 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
00171 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
00172 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
00173 #define SIG_FGC_CAMA (0x4000000 | ZT_SIG_EM)
00174 #define SIG_FGC_CAMAMF (0x8000000 | ZT_SIG_EM)
00175 #define SIG_FXSLS ZT_SIG_FXSLS
00176 #define SIG_FXSGS ZT_SIG_FXSGS
00177 #define SIG_FXSKS ZT_SIG_FXSKS
00178 #define SIG_FXOLS ZT_SIG_FXOLS
00179 #define SIG_FXOGS ZT_SIG_FXOGS
00180 #define SIG_FXOKS ZT_SIG_FXOKS
00181 #define SIG_PRI ZT_SIG_CLEAR
00182 #define SIG_SF ZT_SIG_SF
00183 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
00184 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
00185 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
00186 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
00187 #define SIG_EM_E1 ZT_SIG_EM_E1
00188 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
00189 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
00190
00191 #define NUM_SPANS 32
00192 #define NUM_DCHANS 4
00193 #define MAX_CHANNELS 672
00194
00195 #define CHAN_PSEUDO -2
00196
00197 #define DCHAN_PROVISIONED (1 << 0)
00198 #define DCHAN_NOTINALARM (1 << 1)
00199 #define DCHAN_UP (1 << 2)
00200
00201 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
00202
00203 static char defaultcic[64] = "";
00204 static char defaultozz[64] = "";
00205
00206 static char progzone[10] = "";
00207
00208 static int usedistinctiveringdetection = 0;
00209 static int distinctiveringaftercid = 0;
00210
00211 static int numbufs = 4;
00212
00213 #ifdef HAVE_PRI
00214 static struct ast_channel inuse;
00215 #ifdef PRI_GETSET_TIMERS
00216 static int pritimers[PRI_MAX_TIMERS];
00217 #endif
00218 static int pridebugfd = -1;
00219 static char pridebugfilename[1024] = "";
00220 #endif
00221
00222
00223 static int firstdigittimeout = 16000;
00224
00225
00226 static int gendigittimeout = 8000;
00227
00228
00229 static int matchdigittimeout = 3000;
00230
00231
00232 AST_MUTEX_DEFINE_STATIC(iflock);
00233
00234
00235 static int ifcount = 0;
00236
00237 #ifdef HAVE_PRI
00238 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
00239 #endif
00240
00241
00242
00243 AST_MUTEX_DEFINE_STATIC(monlock);
00244
00245
00246
00247 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00248
00249 static int restart_monitor(void);
00250
00251 static enum ast_bridge_result zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
00252
00253 static int zt_sendtext(struct ast_channel *c, const char *text);
00254
00255
00256 static inline int zt_get_event(int fd)
00257 {
00258 int j;
00259 if (ioctl(fd, ZT_GETEVENT, &j) == -1)
00260 return -1;
00261 return j;
00262 }
00263
00264
00265 static inline int zt_wait_event(int fd)
00266 {
00267 int i, j = 0;
00268 i = ZT_IOMUX_SIGEVENT;
00269 if (ioctl(fd, ZT_IOMUX, &i) == -1)
00270 return -1;
00271 if (ioctl(fd, ZT_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 zt_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 zt_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 time_t lastreset;
00334 long resetinterval;
00335 struct zt_pvt *pvts[MAX_CHANNELS];
00336 struct zt_pvt *crvs;
00337 struct zt_pvt *crvend;
00338 };
00339
00340
00341 static struct zt_pri pris[NUM_SPANS];
00342
00343 #if 0
00344 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
00345 #else
00346 #define DEFAULT_PRI_DEBUG 0
00347 #endif
00348
00349 static inline void pri_rel(struct zt_pri *pri)
00350 {
00351 ast_mutex_unlock(&pri->lock);
00352 }
00353
00354 #else
00355
00356 struct zt_pri;
00357 #endif
00358
00359 #define SUB_REAL 0
00360 #define SUB_CALLWAIT 1
00361 #define SUB_THREEWAY 2
00362
00363
00364 #define POLARITY_IDLE 0
00365 #define POLARITY_REV 1
00366
00367
00368 static struct zt_distRings drings;
00369
00370 struct distRingData {
00371 int ring[3];
00372 };
00373 struct ringContextData {
00374 char contextData[AST_MAX_CONTEXT];
00375 };
00376 struct zt_distRings {
00377 struct distRingData ringnum[3];
00378 struct ringContextData ringContext[3];
00379 };
00380
00381 static char *subnames[] = {
00382 "Real",
00383 "Callwait",
00384 "Threeway"
00385 };
00386
00387 struct zt_subchannel {
00388 int zfd;
00389 struct ast_channel *owner;
00390 int chan;
00391 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
00392 struct ast_frame f;
00393 unsigned int needringing:1;
00394 unsigned int needbusy:1;
00395 unsigned int needcongestion:1;
00396 unsigned int needcallerid:1;
00397 unsigned int needanswer:1;
00398 unsigned int needflash:1;
00399 unsigned int needhold:1;
00400 unsigned int needunhold:1;
00401 unsigned int linear:1;
00402 unsigned int inthreeway:1;
00403 ZT_CONFINFO curconf;
00404 };
00405
00406 #define CONF_USER_REAL (1 << 0)
00407 #define CONF_USER_THIRDCALL (1 << 1)
00408
00409 #define MAX_SLAVES 4
00410
00411 static struct zt_pvt {
00412 ast_mutex_t lock;
00413 struct ast_channel *owner;
00414
00415
00416 struct zt_subchannel sub_unused;
00417 struct zt_subchannel subs[3];
00418 struct zt_confinfo saveconf;
00419
00420 struct zt_pvt *slaves[MAX_SLAVES];
00421 struct zt_pvt *master;
00422 int inconference;
00423
00424 int sig;
00425 int radio;
00426 int outsigmod;
00427 int oprmode;
00428 struct zt_pvt *oprpeer;
00429 float rxgain;
00430 float txgain;
00431 int tonezone;
00432 struct zt_pvt *next;
00433 struct zt_pvt *prev;
00434
00435
00436 unsigned int adsi:1;
00437 unsigned int answeronpolarityswitch:1;
00438 unsigned int busydetect:1;
00439 unsigned int callreturn:1;
00440 unsigned int callwaiting:1;
00441 unsigned int callwaitingcallerid:1;
00442 unsigned int cancallforward:1;
00443 unsigned int canpark:1;
00444 unsigned int confirmanswer:1;
00445 unsigned int destroy:1;
00446 unsigned int didtdd:1;
00447 unsigned int dialednone:1;
00448 unsigned int dialing:1;
00449 unsigned int digital:1;
00450 unsigned int dnd:1;
00451 unsigned int echobreak:1;
00452 unsigned int echocanbridged:1;
00453 unsigned int echocanon:1;
00454 unsigned int faxhandled:1;
00455 unsigned int firstradio:1;
00456 unsigned int hanguponpolarityswitch:1;
00457 unsigned int hardwaredtmf:1;
00458 unsigned int hidecallerid:1;
00459 unsigned int hidecalleridname:1;
00460 unsigned int ignoredtmf:1;
00461 unsigned int immediate:1;
00462 unsigned int inalarm:1;
00463 unsigned int mate:1;
00464 unsigned int outgoing:1;
00465 unsigned int overlapdial:1;
00466 unsigned int permcallwaiting:1;
00467 unsigned int permhidecallerid:1;
00468 unsigned int priindication_oob:1;
00469 unsigned int priexclusive:1;
00470 unsigned int pulse:1;
00471 unsigned int pulsedial:1;
00472 unsigned int restrictcid:1;
00473 unsigned int threewaycalling:1;
00474 unsigned int transfer:1;
00475 unsigned int use_callerid:1;
00476 unsigned int use_callingpres:1;
00477 unsigned int usedistinctiveringdetection:1;
00478 unsigned int zaptrcallerid:1;
00479 unsigned int transfertobusy:1;
00480 #if defined(HAVE_PRI)
00481 unsigned int alerting:1;
00482 unsigned int alreadyhungup:1;
00483 unsigned int isidlecall:1;
00484 unsigned int proceeding:1;
00485 unsigned int progress:1;
00486 unsigned int resetting:1;
00487 unsigned int setup_ack:1;
00488 #endif
00489 unsigned int use_smdi:1;
00490 struct ast_smdi_interface *smdi_iface;
00491
00492 struct zt_distRings drings;
00493
00494 char context[AST_MAX_CONTEXT];
00495 char defcontext[AST_MAX_CONTEXT];
00496 char exten[AST_MAX_EXTENSION];
00497 char language[MAX_LANGUAGE];
00498 char mohinterpret[MAX_MUSICCLASS];
00499 char mohsuggest[MAX_MUSICCLASS];
00500 #ifdef PRI_ANI
00501 char cid_ani[AST_MAX_EXTENSION];
00502 #endif
00503 char cid_num[AST_MAX_EXTENSION];
00504 int cid_ton;
00505 char cid_name[AST_MAX_EXTENSION];
00506 char lastcid_num[AST_MAX_EXTENSION];
00507 char lastcid_name[AST_MAX_EXTENSION];
00508 char *origcid_num;
00509 char *origcid_name;
00510 char callwait_num[AST_MAX_EXTENSION];
00511 char callwait_name[AST_MAX_EXTENSION];
00512 char rdnis[AST_MAX_EXTENSION];
00513 char dnid[AST_MAX_EXTENSION];
00514 unsigned int group;
00515 int law;
00516 int confno;
00517 int confusers;
00518 int propconfno;
00519 ast_group_t callgroup;
00520 ast_group_t pickupgroup;
00521 int channel;
00522 int span;
00523 time_t guardtime;
00524 int cid_signalling;
00525 int cid_start;
00526 int callingpres;
00527 int callwaitingrepeat;
00528 int cidcwexpire;
00529 unsigned char *cidspill;
00530 int cidpos;
00531 int cidlen;
00532 int ringt;
00533 int ringt_base;
00534 int stripmsd;
00535 int callwaitcas;
00536 int callwaitrings;
00537 int echocancel;
00538 int echotraining;
00539 char echorest[20];
00540 int busycount;
00541 int busy_tonelength;
00542 int busy_quietlength;
00543 int callprogress;
00544 struct timeval flashtime;
00545 struct ast_dsp *dsp;
00546 int cref;
00547 ZT_DIAL_OPERATION dop;
00548 int whichwink;
00549 char finaldial[64];
00550 char accountcode[AST_MAX_ACCOUNT_CODE];
00551 int amaflags;
00552 struct tdd_state *tdd;
00553 char call_forward[AST_MAX_EXTENSION];
00554 char mailbox[AST_MAX_EXTENSION];
00555 char dialdest[256];
00556 int onhooktime;
00557 int msgstate;
00558 int distinctivering;
00559 int cidrings;
00560 int dtmfrelax;
00561 int fake_event;
00562 int polarityonanswerdelay;
00563 struct timeval polaritydelaytv;
00564 int sendcalleridafter;
00565 #ifdef HAVE_PRI
00566 struct zt_pri *pri;
00567 struct zt_pvt *bearer;
00568 struct zt_pvt *realcall;
00569 q931_call *call;
00570 int prioffset;
00571 int logicalspan;
00572 #endif
00573 int polarity;
00574 int dsp_features;
00575 char begindigit;
00576 } *iflist = NULL, *ifend = NULL;
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588 struct zt_chan_conf {
00589 struct zt_pvt chan;
00590 #ifdef HAVE_PRI
00591 struct zt_pri pri;
00592 #endif
00593 ZT_PARAMS timing;
00594
00595 char smdi_port[SMDI_MAX_FILENAME_LEN];
00596 };
00597
00598
00599 static struct zt_chan_conf zt_chan_conf_default(void) {
00600
00601
00602
00603 struct zt_chan_conf conf = {
00604 #ifdef HAVE_PRI
00605 .pri = {
00606 .nsf = PRI_NSF_NONE,
00607 .switchtype = PRI_SWITCH_NI2,
00608 .dialplan = PRI_NATIONAL_ISDN + 1,
00609 .localdialplan = PRI_NATIONAL_ISDN + 1,
00610 .nodetype = PRI_CPE,
00611
00612 .minunused = 2,
00613 .idleext = "",
00614 .idledial = "",
00615 .internationalprefix = "",
00616 .nationalprefix = "",
00617 .localprefix = "",
00618 .privateprefix = "",
00619 .unknownprefix = "",
00620
00621 .resetinterval = 3600
00622 },
00623 #endif
00624 .chan = {
00625 .context = "default",
00626 .cid_num = "",
00627 .cid_name = "",
00628 .mohinterpret = "default",
00629 .mohsuggest = "",
00630 .transfertobusy = 1,
00631
00632 .cid_signalling = CID_SIG_BELL,
00633 .cid_start = CID_START_RING,
00634 .zaptrcallerid = 0,
00635 .use_callerid = 1,
00636 .sig = -1,
00637 .outsigmod = -1,
00638
00639 .tonezone = -1,
00640
00641 .echocancel = 1,
00642
00643 .busycount = 3,
00644
00645 .accountcode = "",
00646
00647 .mailbox = "",
00648
00649
00650 .polarityonanswerdelay = 600,
00651
00652 .sendcalleridafter = DEFAULT_CIDRINGS
00653 },
00654 .timing = {
00655 .prewinktime = -1,
00656 .preflashtime = -1,
00657 .winktime = -1,
00658 .flashtime = -1,
00659 .starttime = -1,
00660 .rxwinktime = -1,
00661 .rxflashtime = -1,
00662 .debouncetime = -1
00663 },
00664 .smdi_port = "/dev/ttyS0",
00665 };
00666
00667 return conf;
00668 }
00669
00670
00671 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
00672 static int zt_digit_begin(struct ast_channel *ast, char digit);
00673 static int zt_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
00674 static int zt_sendtext(struct ast_channel *c, const char *text);
00675 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
00676 static int zt_hangup(struct ast_channel *ast);
00677 static int zt_answer(struct ast_channel *ast);
00678 static struct ast_frame *zt_read(struct ast_channel *ast);
00679 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
00680 static struct ast_frame *zt_exception(struct ast_channel *ast);
00681 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
00682 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00683 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
00684 static int zt_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len);
00685
00686 static const struct ast_channel_tech zap_tech = {
00687 .type = "Zap",
00688 .description = tdesc,
00689 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
00690 .requester = zt_request,
00691 .send_digit_begin = zt_digit_begin,
00692 .send_digit_end = zt_digit_end,
00693 .send_text = zt_sendtext,
00694 .call = zt_call,
00695 .hangup = zt_hangup,
00696 .answer = zt_answer,
00697 .read = zt_read,
00698 .write = zt_write,
00699 .bridge = zt_bridge,
00700 .exception = zt_exception,
00701 .indicate = zt_indicate,
00702 .fixup = zt_fixup,
00703 .setoption = zt_setoption,
00704 .func_channel_read = zt_func_read,
00705 };
00706
00707 #ifdef HAVE_PRI
00708 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
00709 #else
00710 #define GET_CHANNEL(p) ((p)->channel)
00711 #endif
00712
00713 struct zt_pvt *round_robin[32];
00714
00715 #ifdef HAVE_PRI
00716 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
00717 {
00718 int res;
00719
00720 do {
00721 res = ast_mutex_trylock(&pri->lock);
00722 if (res) {
00723 ast_mutex_unlock(&pvt->lock);
00724
00725 usleep(1);
00726 ast_mutex_lock(&pvt->lock);
00727 }
00728 } while (res);
00729
00730 pthread_kill(pri->master, SIGURG);
00731 return 0;
00732 }
00733 #endif
00734
00735 #define NUM_CADENCE_MAX 25
00736 static int num_cadence = 4;
00737 static int user_has_defined_cadences = 0;
00738
00739 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
00740 { { 125, 125, 2000, 4000 } },
00741 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } },
00742 { { 125, 125, 125, 125, 125, 4000 } },
00743 { { 1000, 500, 2500, 5000 } },
00744 };
00745
00746
00747
00748
00749
00750 static int cidrings[NUM_CADENCE_MAX] = {
00751 2,
00752 4,
00753 3,
00754 2,
00755 };
00756
00757 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
00758 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
00759
00760 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
00761 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
00762
00763 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
00764 {
00765 int res;
00766 if (p->subs[0].owner == ast)
00767 res = 0;
00768 else if (p->subs[1].owner == ast)
00769 res = 1;
00770 else if (p->subs[2].owner == ast)
00771 res = 2;
00772 else {
00773 res = -1;
00774 if (!nullok)
00775 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
00776 }
00777 return res;
00778 }
00779
00780 #ifdef HAVE_PRI
00781 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
00782 #else
00783 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
00784 #endif
00785 {
00786 #ifdef HAVE_PRI
00787 if (pri)
00788 ast_mutex_unlock(&pri->lock);
00789 #endif
00790 for (;;) {
00791 if (p->subs[a].owner) {
00792 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
00793 ast_mutex_unlock(&p->lock);
00794 usleep(1);
00795 ast_mutex_lock(&p->lock);
00796 } else {
00797 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
00798 ast_mutex_unlock(&p->subs[a].owner->lock);
00799 break;
00800 }
00801 } else
00802 break;
00803 }
00804 #ifdef HAVE_PRI
00805 if (pri)
00806 ast_mutex_lock(&pri->lock);
00807 #endif
00808 }
00809
00810 #ifdef HAVE_PRI
00811 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
00812 #else
00813 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
00814 #endif
00815 {
00816
00817 #ifdef HAVE_PRI
00818 if (pri)
00819 ast_mutex_unlock(&pri->lock);
00820 #endif
00821 for (;;) {
00822 if (p->owner) {
00823 if (ast_mutex_trylock(&p->owner->lock)) {
00824 ast_mutex_unlock(&p->lock);
00825 usleep(1);
00826 ast_mutex_lock(&p->lock);
00827 } else {
00828 ast_queue_frame(p->owner, f);
00829 ast_mutex_unlock(&p->owner->lock);
00830 break;
00831 }
00832 } else
00833 break;
00834 }
00835 #ifdef HAVE_PRI
00836 if (pri)
00837 ast_mutex_lock(&pri->lock);
00838 #endif
00839 }
00840
00841 static int restore_gains(struct zt_pvt *p);
00842
00843 static void swap_subs(struct zt_pvt *p, int a, int b)
00844 {
00845 int tchan;
00846 int tinthreeway;
00847 struct ast_channel *towner;
00848
00849 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
00850
00851 tchan = p->subs[a].chan;
00852 towner = p->subs[a].owner;
00853 tinthreeway = p->subs[a].inthreeway;
00854
00855 p->subs[a].chan = p->subs[b].chan;
00856 p->subs[a].owner = p->subs[b].owner;
00857 p->subs[a].inthreeway = p->subs[b].inthreeway;
00858
00859 p->subs[b].chan = tchan;
00860 p->subs[b].owner = towner;
00861 p->subs[b].inthreeway = tinthreeway;
00862
00863 if (p->subs[a].owner)
00864 p->subs[a].owner->fds[0] = p->subs[a].zfd;
00865 if (p->subs[b].owner)
00866 p->subs[b].owner->fds[0] = p->subs[b].zfd;
00867 wakeup_sub(p, a, NULL);
00868 wakeup_sub(p, b, NULL);
00869 }
00870
00871 static int zt_open(char *fn)
00872 {
00873 int fd;
00874 int isnum;
00875 int chan = 0;
00876 int bs;
00877 int x;
00878 isnum = 1;
00879 for (x = 0; x < strlen(fn); x++) {
00880 if (!isdigit(fn[x])) {
00881 isnum = 0;
00882 break;
00883 }
00884 }
00885 if (isnum) {
00886 chan = atoi(fn);
00887 if (chan < 1) {
00888 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
00889 return -1;
00890 }
00891 fn = "/dev/zap/channel";
00892 }
00893 fd = open(fn, O_RDWR | O_NONBLOCK);
00894 if (fd < 0) {
00895 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
00896 return -1;
00897 }
00898 if (chan) {
00899 if (ioctl(fd, ZT_SPECIFY, &chan)) {
00900 x = errno;
00901 close(fd);
00902 errno = x;
00903 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
00904 return -1;
00905 }
00906 }
00907 bs = READ_SIZE;
00908 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
00909 return fd;
00910 }
00911
00912 static void zt_close(int fd)
00913 {
00914 if (fd > 0)
00915 close(fd);
00916 }
00917
00918 static int zt_setlinear(int zfd, int linear)
00919 {
00920 int res;
00921 res = ioctl(zfd, ZT_SETLINEAR, &linear);
00922 if (res)
00923 return res;
00924 return 0;
00925 }
00926
00927
00928 static int alloc_sub(struct zt_pvt *p, int x)
00929 {
00930 ZT_BUFFERINFO bi;
00931 int res;
00932 if (p->subs[x].zfd < 0) {
00933 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
00934 if (p->subs[x].zfd > -1) {
00935 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
00936 if (!res) {
00937 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
00938 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
00939 bi.numbufs = numbufs;
00940 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
00941 if (res < 0) {
00942 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
00943 }
00944 } else
00945 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
00946 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
00947 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d\n", p->subs[x].zfd);
00948 zt_close(p->subs[x].zfd);
00949 p->subs[x].zfd = -1;
00950 return -1;
00951 }
00952 if (option_debug)
00953 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
00954 return 0;
00955 } else
00956 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
00957 return -1;
00958 }
00959 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
00960 return -1;
00961 }
00962
00963 static int unalloc_sub(struct zt_pvt *p, int x)
00964 {
00965 if (!x) {
00966 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
00967 return -1;
00968 }
00969 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
00970 if (p->subs[x].zfd > -1) {
00971 zt_close(p->subs[x].zfd);
00972 }
00973 p->subs[x].zfd = -1;
00974 p->subs[x].linear = 0;
00975 p->subs[x].chan = 0;
00976 p->subs[x].owner = NULL;
00977 p->subs[x].inthreeway = 0;
00978 p->polarity = POLARITY_IDLE;
00979 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
00980 return 0;
00981 }
00982
00983 static int digit_to_dtmfindex(char digit)
00984 {
00985 if (isdigit(digit))
00986 return ZT_TONE_DTMF_BASE + (digit - '0');
00987 else if (digit >= 'A' && digit <= 'D')
00988 return ZT_TONE_DTMF_A + (digit - 'A');
00989 else if (digit >= 'a' && digit <= 'd')
00990 return ZT_TONE_DTMF_A + (digit - 'a');
00991 else if (digit == '*')
00992 return ZT_TONE_DTMF_s;
00993 else if (digit == '#')
00994 return ZT_TONE_DTMF_p;
00995 else
00996 return -1;
00997 }
00998
00999 static int zt_digit_begin(struct ast_channel *chan, char digit)
01000 {
01001 struct zt_pvt *pvt;
01002 int index;
01003 int dtmf = -1;
01004
01005 pvt = chan->tech_pvt;
01006
01007 ast_mutex_lock(&pvt->lock);
01008
01009 index = zt_get_index(chan, pvt, 0);
01010
01011 if ((index != SUB_REAL) || !pvt->owner)
01012 goto out;
01013
01014 #ifdef HAVE_PRI
01015 if ((pvt->sig == SIG_PRI) && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
01016 if (pvt->setup_ack) {
01017 if (!pri_grab(pvt, pvt->pri)) {
01018 pri_information(pvt->pri->pri, pvt->call, digit);
01019 pri_rel(pvt->pri);
01020 } else
01021 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
01022 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
01023 int res;
01024 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
01025 res = strlen(pvt->dialdest);
01026 pvt->dialdest[res++] = digit;
01027 pvt->dialdest[res] = '\0';
01028 }
01029 goto out;
01030 }
01031 #endif
01032 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
01033 goto out;
01034
01035 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &dtmf)) {
01036 int res;
01037 ZT_DIAL_OPERATION zo = {
01038 .op = ZT_DIAL_OP_APPEND,
01039 .dialstr[0] = 'T',
01040 .dialstr[1] = digit,
01041 .dialstr[2] = 0,
01042 };
01043 if ((res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
01044 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
01045 else
01046 pvt->dialing = 1;
01047 } else {
01048 ast_log(LOG_DEBUG, "Started VLDTMF digit '%c'\n", digit);
01049 pvt->dialing = 1;
01050 pvt->begindigit = digit;
01051 }
01052
01053 out:
01054 ast_mutex_unlock(&pvt->lock);
01055
01056 return 0;
01057 }
01058
01059 static int zt_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
01060 {
01061 struct zt_pvt *pvt;
01062 int res = 0;
01063 int index;
01064 int x;
01065
01066 pvt = chan->tech_pvt;
01067
01068 ast_mutex_lock(&pvt->lock);
01069
01070 index = zt_get_index(chan, pvt, 0);
01071
01072 if ((index != SUB_REAL) || !pvt->owner || pvt->pulse)
01073 goto out;
01074
01075 #ifdef HAVE_PRI
01076
01077 if (pvt->sig == SIG_PRI && !pvt->begindigit)
01078 goto out;
01079 #endif
01080
01081 if (pvt->begindigit) {
01082 x = -1;
01083 ast_log(LOG_DEBUG, "Ending VLDTMF digit '%c'\n", digit);
01084 res = ioctl(pvt->subs[SUB_REAL].zfd, ZT_SENDTONE, &x);
01085 pvt->dialing = 0;
01086 pvt->begindigit = 0;
01087 }
01088
01089 out:
01090 ast_mutex_unlock(&pvt->lock);
01091
01092 return res;
01093 }
01094
01095 static char *events[] = {
01096 "No event",
01097 "On hook",
01098 "Ring/Answered",
01099 "Wink/Flash",
01100 "Alarm",
01101 "No more alarm",
01102 "HDLC Abort",
01103 "HDLC Overrun",
01104 "HDLC Bad FCS",
01105 "Dial Complete",
01106 "Ringer On",
01107 "Ringer Off",
01108 "Hook Transition Complete",
01109 "Bits Changed",
01110 "Pulse Start",
01111 "Timer Expired",
01112 "Timer Ping",
01113 "Polarity Reversal",
01114 "Ring Begin",
01115 };
01116
01117 static struct {
01118 int alarm;
01119 char *name;
01120 } alarms[] = {
01121 { ZT_ALARM_RED, "Red Alarm" },
01122 { ZT_ALARM_YELLOW, "Yellow Alarm" },
01123 { ZT_ALARM_BLUE, "Blue Alarm" },
01124 { ZT_ALARM_RECOVER, "Recovering" },
01125 { ZT_ALARM_LOOPBACK, "Loopback" },
01126 { ZT_ALARM_NOTOPEN, "Not Open" },
01127 { ZT_ALARM_NONE, "None" },
01128 };
01129
01130 static char *alarm2str(int alarm)
01131 {
01132 int x;
01133 for (x = 0; x < sizeof(alarms) / sizeof(alarms[0]); x++) {
01134 if (alarms[x].alarm & alarm)
01135 return alarms[x].name;
01136 }
01137 return alarm ? "Unknown Alarm" : "No Alarm";
01138 }
01139
01140 static char *event2str(int event)
01141 {
01142 static char buf[256];
01143 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
01144 return events[event];
01145 sprintf(buf, "Event %d", event);
01146 return buf;
01147 }
01148
01149 #ifdef HAVE_PRI
01150 static char *dialplan2str(int dialplan)
01151 {
01152 if (dialplan == -1) {
01153 return("Dynamically set dialplan in ISDN");
01154 }
01155 return (pri_plan2str(dialplan));
01156 }
01157 #endif
01158
01159 static char *zap_sig2str(int sig)
01160 {
01161 static char buf[256];
01162 switch (sig) {
01163 case SIG_EM:
01164 return "E & M Immediate";
01165 case SIG_EMWINK:
01166 return "E & M Wink";
01167 case SIG_EM_E1:
01168 return "E & M E1";
01169 case SIG_FEATD:
01170 return "Feature Group D (DTMF)";
01171 case SIG_FEATDMF:
01172 return "Feature Group D (MF)";
01173 case SIG_FEATDMF_TA:
01174 return "Feature Groud D (MF) Tandem Access";
01175 case SIG_FEATB:
01176 return "Feature Group B (MF)";
01177 case SIG_E911:
01178 return "E911 (MF)";
01179 case SIG_FGC_CAMA:
01180 return "FGC/CAMA (Dialpulse)";
01181 case SIG_FGC_CAMAMF:
01182 return "FGC/CAMA (MF)";
01183 case SIG_FXSLS:
01184 return "FXS Loopstart";
01185 case SIG_FXSGS:
01186 return "FXS Groundstart";
01187 case SIG_FXSKS:
01188 return "FXS Kewlstart";
01189 case SIG_FXOLS:
01190 return "FXO Loopstart";
01191 case SIG_FXOGS:
01192 return "FXO Groundstart";
01193 case SIG_FXOKS:
01194 return "FXO Kewlstart";
01195 case SIG_PRI:
01196 return "PRI Signalling";
01197 case SIG_SF:
01198 return "SF (Tone) Signalling Immediate";
01199 case SIG_SFWINK:
01200 return "SF (Tone) Signalling Wink";
01201 case SIG_SF_FEATD:
01202 return "SF (Tone) Signalling with Feature Group D (DTMF)";
01203 case SIG_SF_FEATDMF:
01204 return "SF (Tone) Signalling with Feature Group D (MF)";
01205 case SIG_SF_FEATB:
01206 return "SF (Tone) Signalling with Feature Group B (MF)";
01207 case SIG_GR303FXOKS:
01208 return "GR-303 Signalling with FXOKS";
01209 case SIG_GR303FXSKS:
01210 return "GR-303 Signalling with FXSKS";
01211 case 0:
01212 return "Pseudo Signalling";
01213 default:
01214 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
01215 return buf;
01216 }
01217 }
01218
01219 #define sig2str zap_sig2str
01220
01221 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
01222 {
01223
01224
01225 ZT_CONFINFO zi;
01226
01227 memset(&zi, 0, sizeof(zi));
01228 zi.chan = 0;
01229
01230 if (slavechannel > 0) {
01231
01232 zi.confmode = ZT_CONF_DIGITALMON;
01233 zi.confno = slavechannel;
01234 } else {
01235 if (!index) {
01236
01237 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
01238 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
01239 } else
01240 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
01241 zi.confno = p->confno;
01242 }
01243 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
01244 return 0;
01245 if (c->zfd < 0)
01246 return 0;
01247 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01248 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
01249 return -1;
01250 }
01251 if (slavechannel < 1) {
01252 p->confno = zi.confno;
01253 }
01254 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01255 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01256 return 0;
01257 }
01258
01259 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
01260 {
01261
01262 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
01263 return 1;
01264
01265 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
01266 return 1;
01267 return 0;
01268 }
01269
01270 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
01271 {
01272 ZT_CONFINFO zi;
01273 if (
01274 (c->zfd < 0) ||
01275
01276 !isourconf(p, c)
01277
01278 ) return 0;
01279 memset(&zi, 0, sizeof(zi));
01280 zi.chan = 0;
01281 zi.confno = 0;
01282 zi.confmode = 0;
01283 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01284 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01285 return -1;
01286 }
01287 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01288 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01289 return 0;
01290 }
01291
01292 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
01293 {
01294 int x;
01295 int useslavenative;
01296 struct zt_pvt *slave = NULL;
01297
01298 useslavenative = 1;
01299
01300 for (x = 0; x < 3; x++) {
01301
01302
01303 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
01304 useslavenative = 0;
01305 }
01306
01307
01308 if (useslavenative) {
01309 for (x = 0; x < MAX_SLAVES; x++) {
01310 if (p->slaves[x]) {
01311 if (slave) {
01312
01313
01314 slave = NULL;
01315 useslavenative = 0;
01316 break;
01317 } else {
01318
01319 slave = p->slaves[x];
01320 }
01321 }
01322 }
01323 }
01324
01325 if (!slave)
01326 useslavenative = 0;
01327 else if (slave->law != p->law) {
01328 useslavenative = 0;
01329 slave = NULL;
01330 }
01331 if (out)
01332 *out = slave;
01333 return useslavenative;
01334 }
01335
01336 static int reset_conf(struct zt_pvt *p)
01337 {
01338 ZT_CONFINFO zi;
01339 memset(&zi, 0, sizeof(zi));
01340 p->confno = -1;
01341 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
01342 if (p->subs[SUB_REAL].zfd > -1) {
01343 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
01344 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
01345 }
01346 return 0;
01347 }
01348
01349 static int update_conf(struct zt_pvt *p)
01350 {
01351 int needconf = 0;
01352 int x;
01353 int useslavenative;
01354 struct zt_pvt *slave = NULL;
01355
01356 useslavenative = isslavenative(p, &slave);
01357
01358 for (x = 0; x < 3; x++) {
01359
01360 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
01361 conf_add(p, &p->subs[x], x, 0);
01362 needconf++;
01363 } else {
01364 conf_del(p, &p->subs[x], x);
01365 }
01366 }
01367
01368
01369 for (x = 0; x < MAX_SLAVES; x++) {
01370 if (p->slaves[x]) {
01371 if (useslavenative)
01372 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
01373 else {
01374 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
01375 needconf++;
01376 }
01377 }
01378 }
01379
01380 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
01381 if (useslavenative)
01382 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
01383 else {
01384 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
01385 needconf++;
01386 }
01387 }
01388
01389 if (p->master) {
01390 if (isslavenative(p->master, NULL)) {
01391 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
01392 } else {
01393 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
01394 }
01395 }
01396 if (!needconf) {
01397
01398
01399 p->confno = -1;
01400 }
01401 if (option_debug)
01402 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
01403 return 0;
01404 }
01405
01406 static void zt_enable_ec(struct zt_pvt *p)
01407 {
01408 int x;
01409 int res;
01410 if (!p)
01411 return;
01412 if (p->echocanon) {
01413 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
01414 return;
01415 }
01416 if (p->digital) {
01417 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
01418 return;
01419 }
01420 if (p->echocancel) {
01421 if (p->sig == SIG_PRI) {
01422 x = 1;
01423 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
01424 if (res)
01425 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01426 }
01427 x = p->echocancel;
01428 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01429 if (res)
01430 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01431 else {
01432 p->echocanon = 1;
01433 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
01434 }
01435 } else
01436 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
01437 }
01438
01439 static void zt_train_ec(struct zt_pvt *p)
01440 {
01441 int x;
01442 int res;
01443 if (p && p->echocancel && p->echotraining) {
01444 x = p->echotraining;
01445 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
01446 if (res)
01447 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
01448 else {
01449 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
01450 }
01451 } else
01452 ast_log(LOG_DEBUG, "No echo training requested\n");
01453 }
01454
01455 static void zt_disable_ec(struct zt_pvt *p)
01456 {
01457 int x;
01458 int res;
01459 if (p->echocancel) {
01460 x = 0;
01461 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01462 if (res)
01463 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
01464 else
01465 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
01466 }
01467 p->echocanon = 0;
01468 }
01469
01470 static void fill_txgain(struct zt_gains *g, float gain, int law)
01471 {
01472 int j;
01473 int k;
01474 float linear_gain = pow(10.0, gain / 20.0);
01475
01476 switch (law) {
01477 case ZT_LAW_ALAW:
01478 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01479 if (gain) {
01480 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01481 if (k > 32767) k = 32767;
01482 if (k < -32767) k = -32767;
01483 g->txgain[j] = AST_LIN2A(k);
01484 } else {
01485 g->txgain[j] = j;
01486 }
01487 }
01488 break;
01489 case ZT_LAW_MULAW:
01490 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01491 if (gain) {
01492 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01493 if (k > 32767) k = 32767;
01494 if (k < -32767) k = -32767;
01495 g->txgain[j] = AST_LIN2MU(k);
01496 } else {
01497 g->txgain[j] = j;
01498 }
01499 }
01500 break;
01501 }
01502 }
01503
01504 static void fill_rxgain(struct zt_gains *g, float gain, int law)
01505 {
01506 int j;
01507 int k;
01508 float linear_gain = pow(10.0, gain / 20.0);
01509
01510 switch (law) {
01511 case ZT_LAW_ALAW:
01512 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01513 if (gain) {
01514 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01515 if (k > 32767) k = 32767;
01516 if (k < -32767) k = -32767;
01517 g->rxgain[j] = AST_LIN2A(k);
01518 } else {
01519 g->rxgain[j] = j;
01520 }
01521 }
01522 break;
01523 case ZT_LAW_MULAW:
01524 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01525 if (gain) {
01526 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01527 if (k > 32767) k = 32767;
01528 if (k < -32767) k = -32767;
01529 g->rxgain[j] = AST_LIN2MU(k);
01530 } else {
01531 g->rxgain[j] = j;
01532 }
01533 }
01534 break;
01535 }
01536 }
01537
01538 static int set_actual_txgain(int fd, int chan, float gain, int law)
01539 {
01540 struct zt_gains g;
01541 int res;
01542
01543 memset(&g, 0, sizeof(g));
01544 g.chan = chan;
01545 res = ioctl(fd, ZT_GETGAINS, &g);
01546 if (res) {
01547 if (option_debug)
01548 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01549 return res;
01550 }
01551
01552 fill_txgain(&g, gain, law);
01553
01554 return ioctl(fd, ZT_SETGAINS, &g);
01555 }
01556
01557 static int set_actual_rxgain(int fd, int chan, float gain, int law)
01558 {
01559 struct zt_gains g;
01560 int res;
01561
01562 memset(&g, 0, sizeof(g));
01563 g.chan = chan;
01564 res = ioctl(fd, ZT_GETGAINS, &g);
01565 if (res) {
01566 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01567 return res;
01568 }
01569
01570 fill_rxgain(&g, gain, law);
01571
01572 return ioctl(fd, ZT_SETGAINS, &g);
01573 }
01574
01575 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
01576 {
01577 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
01578 }
01579
01580 static int bump_gains(struct zt_pvt *p)
01581 {
01582 int res;
01583
01584
01585 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
01586 if (res) {
01587 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
01588 return -1;
01589 }
01590
01591 return 0;
01592 }
01593
01594 static int restore_gains(struct zt_pvt *p)
01595 {
01596 int res;
01597
01598 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01599 if (res) {
01600 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
01601 return -1;
01602 }
01603
01604 return 0;
01605 }
01606
01607 static inline int zt_set_hook(int fd, int hs)
01608 {
01609 int x, res;
01610 x = hs;
01611 res = ioctl(fd, ZT_HOOK, &x);
01612 if (res < 0)
01613 {
01614 if (errno == EINPROGRESS) return 0;
01615 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
01616 }
01617 return res;
01618 }
01619
01620 static inline int zt_confmute(struct zt_pvt *p, int muted)
01621 {
01622 int x, y, res;
01623 x = muted;
01624 if (p->sig == SIG_PRI) {
01625 y = 1;
01626 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
01627 if (res)
01628 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
01629 }
01630 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
01631 if (res < 0)
01632 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
01633 return res;
01634 }
01635
01636 static int save_conference(struct zt_pvt *p)
01637 {
01638 struct zt_confinfo c;
01639 int res;
01640 if (p->saveconf.confmode) {
01641 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
01642 return -1;
01643 }
01644 p->saveconf.chan = 0;
01645 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
01646 if (res) {
01647 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
01648 p->saveconf.confmode = 0;
01649 return -1;
01650 }
01651 c.chan = 0;
01652 c.confno = 0;
01653 c.confmode = ZT_CONF_NORMAL;
01654 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
01655 if (res) {
01656 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
01657 return -1;
01658 }
01659 if (option_debug)
01660 ast_log(LOG_DEBUG, "Disabled conferencing\n");
01661 return 0;
01662 }
01663
01664 static int restore_conference(struct zt_pvt *p)
01665 {
01666 int res;
01667 if (p->saveconf.confmode) {
01668 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
01669 p->saveconf.confmode = 0;
01670 if (res) {
01671 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
01672 return -1;
01673 }
01674 }
01675 if (option_debug)
01676 ast_log(LOG_DEBUG, "Restored conferencing\n");
01677 return 0;
01678 }
01679
01680 static int send_callerid(struct zt_pvt *p);
01681
01682 static int send_cwcidspill(struct zt_pvt *p)
01683 {
01684 p->callwaitcas = 0;
01685 p->cidcwexpire = 0;
01686 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
01687 return -1;
01688 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
01689
01690 p->cidlen += READ_SIZE * 4;
01691 p->cidpos = 0;
01692 send_callerid(p);
01693 if (option_verbose > 2)
01694 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
01695 return 0;
01696 }
01697
01698 static int has_voicemail(struct zt_pvt *p)
01699 {
01700
01701 return ast_app_has_voicemail(p->mailbox, NULL);
01702 }
01703
01704 static int send_callerid(struct zt_pvt *p)
01705 {
01706
01707 int res;
01708
01709 if (p->subs[SUB_REAL].linear) {
01710 p->subs[SUB_REAL].linear = 0;
01711 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
01712 }
01713 while (p->cidpos < p->cidlen) {
01714 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
01715 if (res < 0) {
01716 if (errno == EAGAIN)
01717 return 0;
01718 else {
01719 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
01720 return -1;
01721 }
01722 }
01723 if (!res)
01724 return 0;
01725 p->cidpos += res;
01726 }
01727 free(p->cidspill);
01728 p->cidspill = NULL;
01729 if (p->callwaitcas) {
01730
01731 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
01732 } else
01733 restore_conference(p);
01734 return 0;
01735 }
01736
01737 static int zt_callwait(struct ast_channel *ast)
01738 {
01739 struct zt_pvt *p = ast->tech_pvt;
01740 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
01741 if (p->cidspill) {
01742 ast_log(LOG_WARNING, "Spill already exists?!?\n");
01743 free(p->cidspill);
01744 }
01745 if (!(p->cidspill = ast_malloc(2400 + 680 + READ_SIZE * 4)))
01746 return -1;
01747 save_conference(p);
01748
01749 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
01750 if (!p->callwaitrings && p->callwaitingcallerid) {
01751 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
01752 p->callwaitcas = 1;
01753 p->cidlen = 2400 + 680 + READ_SIZE * 4;
01754 } else {
01755 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
01756 p->callwaitcas = 0;
01757 p->cidlen = 2400 + READ_SIZE * 4;
01758 }
01759 p->cidpos = 0;
01760 send_callerid(p);
01761
01762 return 0;
01763 }
01764
01765 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
01766 {
01767 struct zt_pvt *p = ast->tech_pvt;
01768 int x, res, index,mysig;
01769 char *c, *n, *l;
01770 #ifdef HAVE_PRI
01771 char *s = NULL;
01772 #endif
01773 char dest[256];
01774 ast_mutex_lock(&p->lock);
01775 ast_copy_string(dest, rdest, sizeof(dest));
01776 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
01777 if ((ast->_state == AST_STATE_BUSY)) {
01778 p->subs[SUB_REAL].needbusy = 1;
01779 ast_mutex_unlock(&p->lock);
01780 return 0;
01781 }
01782 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
01783 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
01784 ast_mutex_unlock(&p->lock);
01785 return -1;
01786 }
01787 p->dialednone = 0;
01788 if ((p->radio || (p->oprmode < 0)))
01789 {
01790
01791 ast_setstate(ast, AST_STATE_UP);
01792 ast_mutex_unlock(&p->lock);
01793 return 0;
01794 }
01795 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
01796 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
01797 if (res)
01798 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
01799 p->outgoing = 1;
01800
01801 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01802
01803 mysig = p->sig;
01804 if (p->outsigmod > -1)
01805 mysig = p->outsigmod;
01806
01807 switch (mysig) {
01808 case SIG_FXOLS:
01809 case SIG_FXOGS:
01810 case SIG_FXOKS:
01811 if (p->owner == ast) {
01812
01813
01814
01815 p->dialing = 1;
01816 if (p->use_callerid) {
01817
01818 if (p->cidspill) {
01819 ast_log(LOG_WARNING, "cidspill already exists??\n");
01820 free(p->cidspill);
01821 }
01822 p->callwaitcas = 0;
01823 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
01824 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
01825 p->cidpos = 0;
01826 send_callerid(p);
01827 }
01828 }
01829
01830 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
01831 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering - 1]))
01832 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
01833 p->cidrings = cidrings[p->distinctivering - 1];
01834 } else {
01835 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
01836 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
01837 p->cidrings = p->sendcalleridafter;
01838 }
01839
01840
01841 c = strchr(dest, '/');
01842 if (c)
01843 c++;
01844 if (c && (strlen(c) < p->stripmsd)) {
01845 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01846 c = NULL;
01847 }
01848 if (c) {
01849 p->dop.op = ZT_DIAL_OP_REPLACE;
01850 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
01851 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
01852 } else {
01853 p->dop.dialstr[0] = '\0';
01854 }
01855 x = ZT_RING;
01856 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
01857 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
01858 ast_mutex_unlock(&p->lock);
01859 return -1;
01860 }
01861 p->dialing = 1;
01862 } else {
01863
01864 p->callwaitrings = 0;
01865 if (ast->cid.cid_num)
01866 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
01867 else
01868 p->callwait_num[0] = '\0';
01869 if (ast->cid.cid_name)
01870 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
01871 else
01872 p->callwait_name[0] = '\0';
01873
01874 if (zt_callwait(ast)) {
01875 ast_mutex_unlock(&p->lock);
01876 return -1;
01877 }
01878
01879 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
01880 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
01881
01882 }
01883 n = ast->cid.cid_name;
01884 l = ast->cid.cid_num;
01885 if (l)
01886 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
01887 else
01888 p->lastcid_num[0] = '\0';
01889 if (n)
01890 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
01891 else
01892 p->lastcid_name[0] = '\0';
01893 ast_setstate(ast, AST_STATE_RINGING);
01894 index = zt_get_index(ast, p, 0);
01895 if (index > -1) {
01896 p->subs[index].needringing = 1;
01897 }
01898 break;
01899 case SIG_FXSLS:
01900 case SIG_FXSGS:
01901 case SIG_FXSKS:
01902 case SIG_EMWINK:
01903 case SIG_EM:
01904 case SIG_EM_E1:
01905 case SIG_FEATD:
01906 case SIG_FEATDMF:
01907 case SIG_E911:
01908 case SIG_FGC_CAMA:
01909 case SIG_FGC_CAMAMF:
01910 case SIG_FEATB:
01911 case SIG_SFWINK:
01912 case SIG_SF:
01913 case SIG_SF_FEATD:
01914 case SIG_SF_FEATDMF:
01915 case SIG_FEATDMF_TA:
01916 case SIG_SF_FEATB:
01917 c = strchr(dest, '/');
01918 if (c)
01919 c++;
01920 else
01921 c = "";
01922 if (strlen(c) < p->stripmsd) {
01923 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01924 ast_mutex_unlock(&p->lock);
01925 return -1;
01926 }
01927 #ifdef HAVE_PRI
01928
01929 if (!p->pri) {
01930 #endif
01931 x = ZT_START;
01932 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01933 if (res < 0) {
01934 if (errno != EINPROGRESS) {
01935 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
01936 ast_mutex_unlock(&p->lock);
01937 return -1;
01938 }
01939 }
01940 #ifdef HAVE_PRI
01941 }
01942 #endif
01943 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
01944 p->dop.op = ZT_DIAL_OP_REPLACE;
01945
01946 c += p->stripmsd;
01947
01948 switch (mysig) {
01949 case SIG_FEATD:
01950 l = ast->cid.cid_num;
01951 if (l)
01952 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
01953 else
01954 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
01955 break;
01956 case SIG_FEATDMF:
01957 l = ast->cid.cid_num;
01958 if (l)
01959 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
01960 else
01961 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
01962 break;
01963 case SIG_FEATDMF_TA:
01964 {
01965 const char *cic, *ozz;
01966
01967
01968 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
01969 if (!ozz)
01970 ozz = defaultozz;
01971 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
01972 if (!cic)
01973 cic = defaultcic;
01974 if (!ozz || !cic) {
01975 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
01976 ast_mutex_unlock(&p->lock);
01977 return -1;
01978 }
01979 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
01980 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
01981 p->whichwink = 0;
01982 }
01983 break;
01984 case SIG_E911:
01985 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
01986 break;
01987 case SIG_FGC_CAMA:
01988 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
01989 break;
01990 case SIG_FGC_CAMAMF:
01991 case SIG_FEATB:
01992 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
01993 break;
01994 default:
01995 if (p->pulse)
01996 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
01997 else
01998 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
01999 break;
02000 }
02001
02002 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
02003 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
02004 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
02005 p->echorest[sizeof(p->echorest) - 1] = '\0';
02006 p->echobreak = 1;
02007 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
02008 } else
02009 p->echobreak = 0;
02010 if (!res) {
02011 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
02012 x = ZT_ONHOOK;
02013 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
02014 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
02015 ast_mutex_unlock(&p->lock);
02016 return -1;
02017 }
02018 } else
02019 ast_log(LOG_DEBUG, "Deferring dialing...\n");
02020 p->dialing = 1;
02021 if (ast_strlen_zero(c))
02022 p->dialednone = 1;
02023 ast_setstate(ast, AST_STATE_DIALING);
02024 break;
02025 case 0:
02026
02027 ast_setstate(ast, AST_STATE_UP);
02028 break;
02029 case SIG_PRI:
02030
02031 p->dialdest[0] = '\0';
02032 break;
02033 default:
02034 ast_log(LOG_DEBUG, "not yet implemented\n");
02035 ast_mutex_unlock(&p->lock);
02036 return -1;
02037 }
02038 #ifdef HAVE_PRI
02039 if (p->pri) {
02040 struct pri_sr *sr;
02041 #ifdef SUPPORT_USERUSER
02042 const char *useruser;
02043 #endif
02044 int pridialplan;
02045 int dp_strip;
02046 int prilocaldialplan;
02047 int ldp_strip;
02048 int exclusive;
02049 const char *rr_str;
02050 int redirect_reason;
02051
02052 c = strchr(dest, '/');
02053 if (c)
02054 c++;
02055 else
02056 c = dest;
02057 if (!p->hidecalleridname)
02058 n = ast->cid.cid_name;
02059 else
02060 n = NULL;
02061 if (!p->hidecallerid) {
02062 l = ast->cid.cid_num;
02063 n = ast->cid.cid_name;
02064 } else {
02065 l = NULL;
02066 n = NULL;
02067 }
02068 if (strlen(c) < p->stripmsd) {
02069 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
02070 ast_mutex_unlock(&p->lock);
02071 return -1;
02072 }
02073 if (mysig != SIG_FXSKS) {
02074 p->dop.op = ZT_DIAL_OP_REPLACE;
02075 s = strchr(c + p->stripmsd, 'w');
02076 if (s) {
02077 if (strlen(s) > 1)
02078 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
02079 else
02080 p->dop.dialstr[0] = '\0';
02081 *s = '\0';
02082 } else {
02083 p->dop.dialstr[0] = '\0';
02084 }
02085 }
02086 if (pri_grab(p, p->pri)) {
02087 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
02088 ast_mutex_unlock(&p->lock);
02089 return -1;
02090 }
02091 if (!(p->call = pri_new_call(p->pri->pri))) {
02092 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
02093 pri_rel(p->pri);
02094 ast_mutex_unlock(&p->lock);
02095 return -1;
02096 }
02097 if (!(sr = pri_sr_new())) {
02098 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
02099 pri_rel(p->pri);
02100 ast_mutex_unlock(&p->lock);
02101 }
02102 if (p->bearer || (mysig == SIG_FXSKS)) {
02103 if (p->bearer) {
02104 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);
02105 p->bearer->call = p->call;
02106 } else
02107 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
02108 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
02109 }
02110 p->digital = IS_DIGITAL(ast->transfercapability);
02111
02112 if (p->priexclusive)
02113 exclusive = 1;
02114 else {
02115
02116 if (p->pri->nodetype == PRI_NETWORK)
02117 exclusive = 0;
02118 else
02119 exclusive = 1;
02120 }
02121
02122 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
02123 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
02124 (p->digital ? -1 :
02125 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
02126 if (p->pri->facilityenable)
02127 pri_facility_enable(p->pri->pri);
02128
02129 if (option_verbose > 2)
02130 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
02131 dp_strip = 0;
02132 pridialplan = p->pri->dialplan - 1;
02133 if (pridialplan == -2) {
02134 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02135 dp_strip = strlen(p->pri->internationalprefix);
02136 pridialplan = PRI_INTERNATIONAL_ISDN;
02137 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02138 dp_strip = strlen(p->pri->nationalprefix);
02139 pridialplan = PRI_NATIONAL_ISDN;
02140 } else {
02141 pridialplan = PRI_LOCAL_ISDN;
02142 }
02143 }
02144 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
02145
02146 ldp_strip = 0;
02147 prilocaldialplan = p->pri->localdialplan - 1;
02148 if ((l != NULL) && (prilocaldialplan == -2)) {
02149 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02150 ldp_strip = strlen(p->pri->internationalprefix);
02151 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
02152 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02153 ldp_strip = strlen(p->pri->nationalprefix);
02154 prilocaldialplan = PRI_NATIONAL_ISDN;
02155 } else {
02156 prilocaldialplan = PRI_LOCAL_ISDN;
02157 }
02158 }
02159 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
02160 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
02161 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
02162 if (!strcasecmp(rr_str, "UNKNOWN"))
02163 redirect_reason = 0;
02164 else if (!strcasecmp(rr_str, "BUSY"))
02165 redirect_reason = 1;
02166 else if (!strcasecmp(rr_str, "NO_REPLY"))
02167 redirect_reason = 2;
02168 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
02169 redirect_reason = 15;
02170 else
02171 redirect_reason = PRI_REDIR_UNCONDITIONAL;
02172 } else
02173 redirect_reason = PRI_REDIR_UNCONDITIONAL;
02174 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
02175
02176 #ifdef SUPPORT_USERUSER
02177
02178 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
02179
02180 if (useruser)
02181 pri_sr_set_useruser(sr, useruser);
02182 #endif
02183
02184 if (pri_setup(p->pri->pri, p->call, sr)) {
02185 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
02186 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
02187 pri_rel(p->pri);
02188 ast_mutex_unlock(&p->lock);
02189 pri_sr_free(sr);
02190 return -1;
02191 }
02192 pri_sr_free(sr);
02193 ast_setstate(ast, AST_STATE_DIALING);
02194 pri_rel(p->pri);
02195 }
02196 #endif
02197 ast_mutex_unlock(&p->lock);
02198 return 0;
02199 }
02200
02201 static void destroy_zt_pvt(struct zt_pvt **pvt)
02202 {
02203 struct zt_pvt *p = *pvt;
02204
02205 if (p->prev)
02206 p->prev->next = p->next;
02207 if (p->next)
02208 p->next->prev = p->prev;
02209 if (p->use_smdi)
02210 ASTOBJ_UNREF(p->smdi_iface, ast_smdi_interface_destroy);
02211 ast_mutex_destroy(&p->lock);
02212 free(p);
02213 *pvt = NULL;
02214 }
02215
02216 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
02217 {
02218 int owned = 0;
02219 int i = 0;
02220
02221 if (!now) {
02222 if (cur->owner) {
02223 owned = 1;
02224 }
02225
02226 for (i = 0; i < 3; i++) {
02227 if (cur->subs[i].owner) {
02228 owned = 1;
02229 }
02230 }
02231 if (!owned) {
02232 if (prev) {
02233 prev->next = cur->next;
02234 if (prev->next)
02235 prev->next->prev = prev;
02236 else
02237 ifend = prev;
02238 } else {
02239 iflist = cur->next;
02240 if (iflist)
02241 iflist->prev = NULL;
02242 else
02243 ifend = NULL;
02244 }
02245 if (cur->subs[SUB_REAL].zfd > -1) {
02246 zt_close(cur->subs[SUB_REAL].zfd);
02247 }
02248 destroy_zt_pvt(&cur);
02249 }
02250 } else {
02251 if (prev) {
02252 prev->next = cur->next;
02253 if (prev->next)
02254 prev->next->prev = prev;
02255 else
02256 ifend = prev;
02257 } else {
02258 iflist = cur->next;
02259 if (iflist)
02260 iflist->prev = NULL;
02261 else
02262 ifend = NULL;
02263 }
02264 if (cur->subs[SUB_REAL].zfd > -1) {
02265 zt_close(cur->subs[SUB_REAL].zfd);
02266 }
02267 destroy_zt_pvt(&cur);
02268 }
02269 return 0;
02270 }
02271
02272 #ifdef HAVE_PRI
02273 static char *zap_send_keypad_facility_app = "ZapSendKeypadFacility";
02274
02275 static char *zap_send_keypad_facility_synopsis = "Send digits out of band over a PRI";
02276
02277 static char *zap_send_keypad_facility_descrip =
02278 " ZapSendKeypadFacility(): This application will send the given string of digits in a Keypad Facility\n"
02279 " IE over the current channel.\n";
02280
02281 static int zap_send_keypad_facility_exec(struct ast_channel *chan, void *data)
02282 {
02283
02284 struct zt_pvt *p;
02285 char *digits = (char *) data;
02286
02287 if (ast_strlen_zero(digits)) {
02288 ast_log(LOG_DEBUG, "No digit string sent to application!\n");
02289 return -1;
02290 }
02291
02292 p = (struct zt_pvt *)chan->tech_pvt;
02293
02294 if (!p) {
02295 ast_log(LOG_DEBUG, "Unable to find technology private\n");
02296 return -1;
02297 }
02298
02299 ast_mutex_lock(&p->lock);
02300
02301 if (!p->pri || !p->call) {
02302 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
02303 ast_mutex_unlock(&p->lock);
02304 return -1;
02305 }
02306
02307 if (!pri_grab(p, p->pri)) {
02308 pri_keypad_facility(p->pri->pri, p->call, digits);
02309 pri_rel(p->pri);
02310 } else {
02311 ast_log(LOG_DEBUG, "Unable to grab pri to send keypad facility!\n");
02312 ast_mutex_unlock(&p->lock);
02313 return -1;
02314 }
02315
02316 ast_mutex_unlock(&p->lock);
02317
02318 return 0;
02319 }
02320
02321 static int pri_is_up(struct zt_pri *pri)
02322 {
02323 int x;
02324 for (x = 0; x < NUM_DCHANS; x++) {
02325 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
02326 return 1;
02327 }
02328 return 0;
02329 }
02330
02331 static int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
02332 {
02333 bearer->owner = &inuse;
02334 bearer->realcall = crv;
02335 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
02336 if (crv->subs[SUB_REAL].owner)
02337 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
02338 crv->bearer = bearer;
02339 crv->call = bearer->call;
02340 crv->pri = pri;
02341 return 0;
02342 }
02343
02344 static char *pri_order(int level)
02345 {
02346 switch (level) {
02347 case 0:
02348 return "Primary";
02349 case 1:
02350 return "Secondary";
02351 case 2:
02352 return "Tertiary";
02353 case 3:
02354 return "Quaternary";
02355 default:
02356 return "<Unknown>";
02357 }
02358 }
02359
02360
02361 static int pri_active_dchan_fd(struct zt_pri *pri)
02362 {
02363 int x = -1;
02364
02365 for (x = 0; x < NUM_DCHANS; x++) {
02366 if ((pri->dchans[x] == pri->pri))
02367 break;
02368 }
02369
02370 return pri->fds[x];
02371 }
02372
02373 static int pri_find_dchan(struct zt_pri *pri)
02374 {
02375 int oldslot = -1;
02376 struct pri *old;
02377 int newslot = -1;
02378 int x;
02379 old = pri->pri;
02380 for (x = 0; x < NUM_DCHANS; x++) {
02381 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
02382 newslot = x;
02383 if (pri->dchans[x] == old) {
02384 oldslot = x;
02385 }
02386 }
02387 if (newslot < 0) {
02388 newslot = 0;
02389 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
02390 pri->dchannels[newslot]);
02391 }
02392 if (old && (oldslot != newslot))
02393 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
02394 pri->dchannels[oldslot], pri->dchannels[newslot]);
02395 pri->pri = pri->dchans[newslot];
02396 return 0;
02397 }
02398 #endif
02399
02400 static int zt_hangup(struct ast_channel *ast)
02401 {
02402 int res;
02403 int index,x, law;
02404
02405 struct zt_pvt *p = ast->tech_pvt;
02406 struct zt_pvt *tmp = NULL;
02407 struct zt_pvt *prev = NULL;
02408 ZT_PARAMS par;
02409
02410 if (option_debug)
02411 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
02412 if (!ast->tech_pvt) {
02413 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
02414 return 0;
02415 }
02416
02417 ast_mutex_lock(&p->lock);
02418
02419 index = zt_get_index(ast, p, 1);
02420
02421 if (p->sig == SIG_PRI) {
02422 x = 1;
02423 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02424 }
02425
02426 x = 0;
02427 zt_confmute(p, 0);
02428 restore_gains(p);
02429 if (p->origcid_num) {
02430 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
02431 free(p->origcid_num);
02432 p->origcid_num = NULL;
02433 }
02434 if (p->origcid_name) {
02435 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
02436 free(p->origcid_name);
02437 p->origcid_name = NULL;
02438 }
02439 if (p->dsp)
02440 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02441 if (p->exten)
02442 p->exten[0] = '\0';
02443
02444 if (option_debug)
02445 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
02446 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
02447 p->ignoredtmf = 0;
02448
02449 if (index > -1) {
02450
02451 p->subs[index].owner = NULL;
02452 p->subs[index].needanswer = 0;
02453 p->subs[index].needflash = 0;
02454 p->subs[index].needringing = 0;
02455 p->subs[index].needbusy = 0;
02456 p->subs[index].needcongestion = 0;
02457 p->subs[index].linear = 0;
02458 p->subs[index].needcallerid = 0;
02459 p->polarity = POLARITY_IDLE;
02460 zt_setlinear(p->subs[index].zfd, 0);
02461 if (index == SUB_REAL) {
02462 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
02463 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
02464 if (p->subs[SUB_CALLWAIT].inthreeway) {
02465
02466 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
02467
02468 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02469 unalloc_sub(p, SUB_CALLWAIT);
02470 p->owner = NULL;
02471 } else {
02472
02473 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
02474 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02475 unalloc_sub(p, SUB_THREEWAY);
02476 if (p->subs[SUB_REAL].inthreeway) {
02477
02478
02479 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02480 p->owner = p->subs[SUB_REAL].owner;
02481 } else {
02482
02483 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02484 p->owner = NULL;
02485 }
02486 p->subs[SUB_REAL].inthreeway = 0;
02487 }
02488 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
02489
02490 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02491 unalloc_sub(p, SUB_CALLWAIT);
02492 p->owner = p->subs[SUB_REAL].owner;
02493 if (p->owner->_state != AST_STATE_UP)
02494 p->subs[SUB_REAL].needanswer = 1;
02495 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
02496 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
02497 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
02498 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02499 unalloc_sub(p, SUB_THREEWAY);
02500 if (p->subs[SUB_REAL].inthreeway) {
02501
02502
02503 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02504 p->owner = p->subs[SUB_REAL].owner;
02505 } else {
02506
02507 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02508 p->owner = NULL;
02509 }
02510 p->subs[SUB_REAL].inthreeway = 0;
02511 }
02512 } else if (index == SUB_CALLWAIT) {
02513
02514 if (p->subs[SUB_CALLWAIT].inthreeway) {
02515
02516
02517 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
02518 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
02519 S_OR(p->mohsuggest, NULL),
02520 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
02521 }
02522 p->subs[SUB_THREEWAY].inthreeway = 0;
02523
02524 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
02525 unalloc_sub(p, SUB_THREEWAY);
02526 } else
02527 unalloc_sub(p, SUB_CALLWAIT);
02528 } else if (index == SUB_THREEWAY) {
02529 if (p->subs[SUB_CALLWAIT].inthreeway) {
02530
02531
02532 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
02533 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
02534 S_OR(p->mohsuggest, NULL),
02535 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
02536 }
02537 p->subs[SUB_CALLWAIT].inthreeway = 0;
02538 }
02539 p->subs[SUB_REAL].inthreeway = 0;
02540
02541
02542 unalloc_sub(p, SUB_THREEWAY);
02543 } else {
02544
02545 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
02546 }
02547 }
02548
02549 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
02550 p->owner = NULL;
02551 p->ringt = 0;
02552 p->distinctivering = 0;
02553 p->confirmanswer = 0;
02554 p->cidrings = 1;
02555 p->outgoing = 0;
02556 p->digital = 0;
02557 p->faxhandled = 0;
02558 p->pulsedial = 0;
02559 p->onhooktime = time(NULL);
02560 #ifdef HAVE_PRI
02561 p->proceeding = 0;
02562 p->progress = 0;
02563 p->alerting = 0;
02564 p->setup_ack = 0;
02565 #endif
02566 if (p->dsp) {
02567 ast_dsp_free(p->dsp);
02568 p->dsp = NULL;
02569 }
02570
02571 law = ZT_LAW_DEFAULT;
02572 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
02573 if (res < 0)
02574 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
02575
02576 #ifdef HAVE_PRI
02577 if (p->pri) {
02578 #ifdef SUPPORT_USERUSER
02579 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
02580 #endif
02581
02582
02583 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
02584 if (!pri_grab(p, p->pri)) {
02585 if (p->alreadyhungup) {
02586 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
02587
02588 #ifdef SUPPORT_USERUSER
02589 pri_call_set_useruser(p->call, useruser);
02590 #endif
02591
02592 pri_hangup(p->pri->pri, p->call, -1);
02593 p->call = NULL;
02594 if (p->bearer)
02595 p->bearer->call = NULL;
02596 } else {
02597 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
02598 int icause = ast->hangupcause ? ast->hangupcause : -1;
02599 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
02600
02601 #ifdef SUPPORT_USERUSER
02602 pri_call_set_useruser(p->call, useruser);
02603 #endif
02604
02605 p->alreadyhungup = 1;
02606 if (p->bearer)
02607 p->bearer->alreadyhungup = 1;
02608 if (cause) {
02609 if (atoi(cause))
02610 icause = atoi(cause);
02611 }
02612 pri_hangup(p->pri->pri, p->call, icause);
02613 }
02614 if (res < 0)
02615 ast_log(LOG_WARNING, "pri_disconnect failed\n");
02616 pri_rel(p->pri);
02617 } else {
02618 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02619 res = -1;
02620 }
02621 } else {
02622 if (p->bearer)
02623 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
02624 p->call = NULL;
02625 res = 0;
02626 }
02627 }
02628 #endif
02629 if (p->sig && (p->sig != SIG_PRI))
02630 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
02631 if (res < 0) {
02632 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
02633 }
02634 switch (p->sig) {
02635 case SIG_FXOGS:
02636 case SIG_FXOLS:
02637 case SIG_FXOKS:
02638 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
02639 if (!res) {
02640 #if 0
02641 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
02642 #endif
02643
02644 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
02645 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
02646 else
02647 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02648 }
02649 break;
02650 case SIG_FXSGS:
02651 case SIG_FXSLS:
02652 case SIG_FXSKS:
02653
02654
02655 if (ast->_state != AST_STATE_RESERVED) {
02656 time(&p->guardtime);
02657 p->guardtime += 2;
02658 }
02659 break;
02660 default:
02661 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02662 }
02663 if (p->cidspill)
02664 free(p->cidspill);
02665 if (p->sig)
02666 zt_disable_ec(p);
02667 x = 0;
02668 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
02669 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
02670 p->didtdd = 0;
02671 p->cidspill = NULL;
02672 p->callwaitcas = 0;
02673 p->callwaiting = p->permcallwaiting;
02674 p->hidecallerid = p->permhidecallerid;
02675 p->dialing = 0;
02676 p->rdnis[0] = '\0';
02677 update_conf(p);
02678 reset_conf(p);
02679
02680 if (p->sig == SIG_PRI) {
02681 x = 0;
02682 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02683 }
02684 #ifdef HAVE_PRI
02685 if (p->bearer) {
02686 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
02687
02688
02689 update_conf(p->bearer);
02690 reset_conf(p->bearer);
02691 p->bearer->owner = NULL;
02692 p->bearer->realcall = NULL;
02693 p->bearer = NULL;
02694 p->subs[SUB_REAL].zfd = -1;
02695 p->pri = NULL;
02696 }
02697 #endif
02698 restart_monitor();
02699 }
02700
02701 p->callwaitingrepeat = 0;
02702 p->cidcwexpire = 0;
02703 p->oprmode = 0;
02704 ast->tech_pvt = NULL;
02705 ast_mutex_unlock(&p->lock);
02706 ast_module_unref(ast_module_info->self);
02707 if (option_verbose > 2)
02708 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
02709
02710 ast_mutex_lock(&iflock);
02711 tmp = iflist;
02712 prev = NULL;
02713 if (p->destroy) {
02714 while (tmp) {
02715 if (tmp == p) {
02716 destroy_channel(prev, tmp, 0);
02717 break;
02718 } else {
02719 prev = tmp;
02720 tmp = tmp->next;
02721 }
02722 }
02723 }
02724 ast_mutex_unlock(&iflock);
02725 return 0;
02726 }
02727
02728 static int zt_answer(struct ast_channel *ast)
02729 {
02730 struct zt_pvt *p = ast->tech_pvt;
02731 int res = 0;
02732 int index;
02733 int oldstate = ast->_state;
02734 ast_setstate(ast, AST_STATE_UP);
02735 ast_mutex_lock(&p->lock);
02736 index = zt_get_index(ast, p, 0);
02737 if (index < 0)
02738 index = SUB_REAL;
02739
02740 if ((p->radio || (p->oprmode < 0))) {
02741 ast_mutex_unlock(&p->lock);
02742 return 0;
02743 }
02744 switch (p->sig) {
02745 case SIG_FXSLS:
02746 case SIG_FXSGS:
02747 case SIG_FXSKS:
02748 p->ringt = 0;
02749
02750 case SIG_EM:
02751 case SIG_EM_E1:
02752 case SIG_EMWINK:
02753 case SIG_FEATD:
02754 case SIG_FEATDMF:
02755 case SIG_FEATDMF_TA:
02756 case SIG_E911:
02757 case SIG_FGC_CAMA:
02758 case SIG_FGC_CAMAMF:
02759 case SIG_FEATB:
02760 case SIG_SF:
02761 case SIG_SFWINK:
02762 case SIG_SF_FEATD:
02763 case SIG_SF_FEATDMF:
02764 case SIG_SF_FEATB:
02765 case SIG_FXOLS:
02766 case SIG_FXOGS:
02767 case SIG_FXOKS:
02768
02769 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
02770 if (p->hanguponpolarityswitch) {
02771 gettimeofday(&p->polaritydelaytv, NULL);
02772 }
02773 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
02774 tone_zone_play_tone(p->subs[index].zfd, -1);
02775 p->dialing = 0;
02776 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
02777 if (oldstate == AST_STATE_RINGING) {
02778 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
02779 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
02780 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02781 p->owner = p->subs[SUB_REAL].owner;
02782 }
02783 }
02784 if (p->sig & __ZT_SIG_FXS) {
02785 zt_enable_ec(p);
02786 zt_train_ec(p);
02787 }
02788 break;
02789 #ifdef HAVE_PRI
02790 case SIG_PRI:
02791
02792 if (!pri_grab(p, p->pri)) {
02793 p->proceeding = 1;
02794 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
02795 pri_rel(p->pri);
02796 } else {
02797 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02798 res = -1;
02799 }
02800 break;
02801 #endif
02802 case 0:
02803 ast_mutex_unlock(&p->lock);
02804 return 0;
02805 default:
02806 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
02807 res = -1;
02808 }
02809 ast_mutex_unlock(&p->lock);
02810 return res;
02811 }
02812
02813 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
02814 {
02815 char *cp;
02816 signed char *scp;
02817 int x;
02818 int index;
02819 struct zt_pvt *p = chan->tech_pvt, *pp;
02820 struct oprmode *oprmode;
02821
02822
02823
02824 if (!data || (datalen < 1)) {
02825 errno = EINVAL;
02826 return -1;
02827 }
02828
02829 switch (option) {
02830 case AST_OPTION_TXGAIN:
02831 scp = (signed char *) data;
02832 index = zt_get_index(chan, p, 0);
02833 if (index < 0) {
02834 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
02835 return -1;
02836 }
02837 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
02838 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
02839 case AST_OPTION_RXGAIN:
02840 scp = (signed char *) data;
02841 index = zt_get_index(chan, p, 0);
02842 if (index < 0) {
02843 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
02844 return -1;
02845 }
02846 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
02847 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
02848 case AST_OPTION_TONE_VERIFY:
02849 if (!p->dsp)
02850 break;
02851 cp = (char *) data;
02852 switch (*cp) {
02853 case 1:
02854 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
02855 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax);
02856 break;
02857 case 2:
02858 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
02859 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax);
02860 break;
02861 default:
02862 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
02863 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02864 break;
02865 }
02866 break;
02867 case AST_OPTION_TDD:
02868
02869 cp = (char *) data;
02870 p->mate = 0;
02871 if (!*cp) {
02872 if (option_debug)
02873 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
02874 if (p->tdd)
02875 tdd_free(p->tdd);
02876 p->tdd = 0;
02877 break;
02878 }
02879 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
02880 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
02881 zt_disable_ec(p);
02882
02883 if (!p->didtdd) {
02884 unsigned char mybuf[41000], *buf;
02885 int size, res, fd, len;
02886 struct pollfd fds[1];
02887
02888 buf = mybuf;
02889 memset(buf, 0x7f, sizeof(mybuf));
02890 ast_tdd_gen_ecdisa(buf + 16000, 16000);
02891 len = 40000;
02892 index = zt_get_index(chan, p, 0);
02893 if (index < 0) {
02894 ast_log(LOG_WARNING, "No index in TDD?\n");
02895 return -1;
02896 }
02897 fd = p->subs[index].zfd;
02898 while (len) {
02899 if (ast_check_hangup(chan))
02900 return -1;
02901 size = len;
02902 if (size > READ_SIZE)
02903 size = READ_SIZE;
02904 fds[0].fd = fd;
02905 fds[0].events = POLLPRI | POLLOUT;
02906 fds[0].revents = 0;
02907 res = poll(fds, 1, -1);
02908 if (!res) {
02909 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
02910 continue;
02911 }
02912
02913 if (fds[0].revents & POLLPRI)
02914 return -1;
02915 if (!(fds[0].revents & POLLOUT)) {
02916 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
02917 continue;
02918 }
02919 res = write(fd, buf, size);
02920 if (res != size) {
02921 if (res == -1) return -1;
02922 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
02923 break;
02924 }
02925 len -= size;
02926 buf += size;
02927 }
02928 p->didtdd = 1;
02929 }
02930 if (*cp == 2) {
02931 if (p->tdd)
02932 tdd_free(p->tdd);
02933 p->tdd = 0;
02934 p->mate = 1;
02935 break;
02936 }
02937 if (!p->tdd) {
02938 p->tdd = tdd_new();
02939 }
02940 break;
02941 case AST_OPTION_RELAXDTMF:
02942 if (!p->dsp)
02943 break;
02944 cp = (char *) data;
02945 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
02946 *cp ? "ON" : "OFF", (int) *cp, chan->name);
02947 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
02948 break;
02949 case AST_OPTION_AUDIO_MODE:
02950 cp = (char *) data;
02951 if (!*cp) {
02952 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
02953 x = 0;
02954 zt_disable_ec(p);
02955 } else {
02956 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
02957 x = 1;
02958 }
02959 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
02960 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
02961 break;
02962 case AST_OPTION_OPRMODE:
02963 oprmode = (struct oprmode *) data;
02964 pp = oprmode->peer->tech_pvt;
02965 p->oprmode = pp->oprmode = 0;
02966
02967 p->oprpeer = pp;
02968 pp->oprpeer = p;
02969
02970 if (oprmode->mode)
02971 {
02972 pp->oprmode = oprmode->mode;
02973 p->oprmode = -oprmode->mode;
02974 }
02975 ast_log(LOG_DEBUG, "Set Operator Services mode, value: %d on %s/%s\n",
02976 oprmode->mode, chan->name,oprmode->peer->name);;
02977 break;
02978 case AST_OPTION_ECHOCAN:
02979 cp = (char *) data;
02980 if (*cp) {
02981 ast_log(LOG_DEBUG, "Enabling echo cancelation on %s\n", chan->name);
02982 zt_enable_ec(p);
02983 } else {
02984 ast_log(LOG_DEBUG, "Disabling echo cancelation on %s\n", chan->name);
02985 zt_disable_ec(p);
02986 }
02987 break;
02988 }
02989 errno = 0;
02990
02991 return 0;
02992 }
02993
02994 static int zt_func_read(struct ast_channel *chan, char *function, char *data, char *buf, size_t len)
02995 {
02996 struct zt_pvt *p = chan->tech_pvt;
02997
02998 if (!strcasecmp(data, "rxgain")) {
02999 ast_mutex_lock(&p->lock);
03000 snprintf(buf, len, "%f", p->rxgain);
03001 ast_mutex_unlock(&p->lock);
03002 } else if (!strcasecmp(data, "txgain")) {
03003 ast_mutex_lock(&p->lock);
03004 snprintf(buf, len, "%f", p->txgain);
03005 ast_mutex_unlock(&p->lock);
03006 } else {
03007 ast_copy_string(buf, "", len);
03008 }
03009 return 0;
03010 }
03011
03012
03013 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
03014 {
03015
03016 int x;
03017 int hasslaves;
03018 if (!master)
03019 return;
03020 if (needlock) {
03021 ast_mutex_lock(&master->lock);
03022 if (slave) {
03023 while (ast_mutex_trylock(&slave->lock)) {
03024 ast_mutex_unlock(&master->lock);
03025 usleep(1);
03026 ast_mutex_lock(&master->lock);
03027 }
03028 }
03029 }
03030 hasslaves = 0;
03031 for (x = 0; x < MAX_SLAVES; x++) {
03032 if (master->slaves[x]) {
03033 if (!slave || (master->slaves[x] == slave)) {
03034
03035 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
03036 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
03037 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
03038 master->slaves[x]->master = NULL;
03039 master->slaves[x] = NULL;
03040 } else
03041 hasslaves = 1;
03042 }
03043 if (!hasslaves)
03044 master->inconference = 0;
03045 }
03046 if (!slave) {
03047 if (master->master) {
03048
03049 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
03050 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
03051 hasslaves = 0;
03052 for (x = 0; x < MAX_SLAVES; x++) {
03053 if (master->master->slaves[x] == master)
03054 master->master->slaves[x] = NULL;
03055 else if (master->master->slaves[x])
03056 hasslaves = 1;
03057 }
03058 if (!hasslaves)
03059 master->master->inconference = 0;
03060 }
03061 master->master = NULL;
03062 }
03063 update_conf(master);
03064 if (needlock) {
03065 if (slave)
03066 ast_mutex_unlock(&slave->lock);
03067 ast_mutex_unlock(&master->lock);
03068 }
03069 }
03070
03071 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
03072 int x;
03073 if (!slave || !master) {
03074 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
03075 return;
03076 }
03077 for (x = 0; x < MAX_SLAVES; x++) {
03078 if (!master->slaves[x]) {
03079 master->slaves[x] = slave;
03080 break;
03081 }
03082 }
03083 if (x >= MAX_SLAVES) {
03084 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
03085 master->slaves[MAX_SLAVES - 1] = slave;
03086 }
03087 if (slave->master)
03088 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
03089 slave->master = master;
03090
03091 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
03092 }
03093
03094 static void disable_dtmf_detect(struct zt_pvt *p)
03095 {
03096 #ifdef ZT_TONEDETECT
03097 int val;
03098 #endif
03099
03100 p->ignoredtmf = 1;
03101
03102 #ifdef ZT_TONEDETECT
03103 val = 0;
03104 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
03105 #endif
03106 if (!p->hardwaredtmf && p->dsp) {
03107 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
03108 ast_dsp_set_features(p->dsp, p->dsp_features);
03109 }
03110 }
03111
03112 static void enable_dtmf_detect(struct zt_pvt *p)
03113 {
03114 #ifdef ZT_TONEDETECT
03115 int val;
03116 #endif
03117
03118 if (p->channel == CHAN_PSEUDO)
03119 return;
03120
03121 p->ignoredtmf = 0;
03122
03123 #ifdef ZT_TONEDETECT
03124 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
03125 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
03126 #endif
03127 if (!p->hardwaredtmf && p->dsp) {
03128 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
03129 ast_dsp_set_features(p->dsp, p->dsp_features);
03130 }
03131 }
03132
03133 static enum ast_bridge_result zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
03134 {
03135 struct ast_channel *who;
03136 struct zt_pvt *p0, *p1, *op0, *op1;
03137 struct zt_pvt *master = NULL, *slave = NULL;
03138 struct ast_frame *f;
03139 int inconf = 0;
03140 int nothingok = 1;
03141 int ofd0, ofd1;
03142 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
03143 int os0 = -1, os1 = -1;
03144 int priority = 0;
03145 struct ast_channel *oc0, *oc1;
03146 enum ast_bridge_result res;
03147
03148 #ifdef PRI_2BCT
03149 int triedtopribridge = 0;
03150 q931_call *q931c0 = NULL, *q931c1 = NULL;
03151 #endif
03152
03153
03154
03155
03156
03157
03158 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
03159 return AST_BRIDGE_FAILED_NOWARN;
03160
03161 ast_mutex_lock(&c0->lock);
03162 ast_mutex_lock(&c1->lock);
03163
03164 p0 = c0->tech_pvt;
03165 p1 = c1->tech_pvt;
03166
03167 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
03168 ast_mutex_unlock(&c0->lock);
03169 ast_mutex_unlock(&c1->lock);
03170 return AST_BRIDGE_FAILED_NOWARN;
03171 }
03172
03173 oi0 = zt_get_index(c0, p0, 0);
03174 oi1 = zt_get_index(c1, p1, 0);
03175 if ((oi0 < 0) || (oi1 < 0)) {
03176 ast_mutex_unlock(&c0->lock);
03177 ast_mutex_unlock(&c1->lock);
03178 return AST_BRIDGE_FAILED;
03179 }
03180
03181 op0 = p0 = c0->tech_pvt;
03182 op1 = p1 = c1->tech_pvt;
03183 ofd0 = c0->fds[0];
03184 ofd1 = c1->fds[0];
03185 oc0 = p0->owner;
03186 oc1 = p1->owner;
03187
03188 if (ast_mutex_trylock(&p0->lock)) {
03189
03190 ast_mutex_unlock(&c0->lock);
03191 ast_mutex_unlock(&c1->lock);
03192 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03193 return AST_BRIDGE_RETRY;
03194 }
03195 if (ast_mutex_trylock(&p1->lock)) {
03196
03197 ast_mutex_unlock(&p0->lock);
03198 ast_mutex_unlock(&c0->lock);
03199 ast_mutex_unlock(&c1->lock);
03200 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03201 return AST_BRIDGE_RETRY;
03202 }
03203
03204 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03205 if (p0->owner && p1->owner) {
03206
03207 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
03208 master = p0;
03209 slave = p1;
03210 inconf = 1;
03211 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
03212 master = p1;
03213 slave = p0;
03214 inconf = 1;
03215 } else {
03216 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
03217 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
03218 p0->channel,
03219 oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03220 p0->subs[SUB_REAL].inthreeway, p0->channel,
03221 oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03222 p1->subs[SUB_REAL].inthreeway);
03223 }
03224 nothingok = 0;
03225 }
03226 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
03227 if (p1->subs[SUB_THREEWAY].inthreeway) {
03228 master = p1;
03229 slave = p0;
03230 nothingok = 0;
03231 }
03232 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
03233 if (p0->subs[SUB_THREEWAY].inthreeway) {
03234 master = p0;
03235 slave = p1;
03236 nothingok = 0;
03237 }
03238 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
03239
03240
03241 if (p1->subs[SUB_CALLWAIT].inthreeway) {
03242 master = p1;
03243 slave = p0;
03244 nothingok = 0;
03245 }
03246 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
03247
03248 if (p0->subs[SUB_CALLWAIT].inthreeway) {
03249 master = p0;
03250 slave = p1;
03251 nothingok = 0;
03252 }
03253 }
03254 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
03255 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
03256 if (master && slave) {
03257
03258
03259
03260 if ((oi1 == SUB_THREEWAY) &&
03261 p1->subs[SUB_THREEWAY].inthreeway &&
03262 p1->subs[SUB_REAL].owner &&
03263 p1->subs[SUB_REAL].inthreeway &&
03264 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03265 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
03266 tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE);
03267 os1 = p1->subs[SUB_REAL].owner->_state;
03268 } else {
03269 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
03270 tone_zone_play_tone(p0->subs[oi0].zfd, -1);
03271 }
03272 if ((oi0 == SUB_THREEWAY) &&
03273 p0->subs[SUB_THREEWAY].inthreeway &&
03274 p0->subs[SUB_REAL].owner &&
03275 p0->subs[SUB_REAL].inthreeway &&
03276 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03277 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
03278 tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE);
03279 os0 = p0->subs[SUB_REAL].owner->_state;
03280 } else {
03281 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
03282 tone_zone_play_tone(p1->subs[oi0].zfd, -1);
03283 }
03284 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03285 if (!p0->echocanbridged || !p1->echocanbridged) {
03286
03287 zt_disable_ec(p0);
03288 zt_disable_ec(p1);
03289 }
03290 }
03291 zt_link(slave, master);
03292 master->inconference = inconf;
03293 } else if (!nothingok)
03294 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
03295
03296 update_conf(p0);
03297 update_conf(p1);
03298 t0 = p0->subs[SUB_REAL].inthreeway;
03299 t1 = p1->subs[SUB_REAL].inthreeway;
03300
03301 ast_mutex_unlock(&p0->lock);
03302 ast_mutex_unlock(&p1->lock);
03303
03304 ast_mutex_unlock(&c0->lock);
03305 ast_mutex_unlock(&c1->lock);
03306
03307
03308 if ((!master || !slave) && !nothingok) {
03309 zt_enable_ec(p0);
03310 zt_enable_ec(p1);
03311 return AST_BRIDGE_FAILED;
03312 }
03313
03314 if (option_verbose > 2)
03315 ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
03316
03317 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03318 disable_dtmf_detect(op0);
03319
03320 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03321 disable_dtmf_detect(op1);
03322
03323 for (;;) {
03324 struct ast_channel *c0_priority[2] = {c0, c1};
03325 struct ast_channel *c1_priority[2] = {c1, c0};
03326
03327
03328
03329 ast_mutex_lock(&c0->lock);
03330 ast_mutex_lock(&c1->lock);
03331 p0 = c0->tech_pvt;
03332 p1 = c1->tech_pvt;
03333
03334 if (op0 == p0)
03335 i0 = zt_get_index(c0, p0, 1);
03336 if (op1 == p1)
03337 i1 = zt_get_index(c1, p1, 1);
03338 ast_mutex_unlock(&c0->lock);
03339 ast_mutex_unlock(&c1->lock);
03340
03341 if (!timeoutms ||
03342 (op0 != p0) ||
03343 (op1 != p1) ||
03344 (ofd0 != c0->fds[0]) ||
03345 (ofd1 != c1->fds[0]) ||
03346 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
03347 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
03348 (oc0 != p0->owner) ||
03349 (oc1 != p1->owner) ||
03350 (t0 != p0->subs[SUB_REAL].inthreeway) ||
03351 (t1 != p1->subs[SUB_REAL].inthreeway) ||
03352 (oi0 != i0) ||
03353 (oi1 != i1)) {
03354 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
03355 op0->channel, oi0, op1->channel, oi1);
03356 res = AST_BRIDGE_RETRY;
03357 goto return_from_bridge;
03358 }
03359
03360 #ifdef PRI_2BCT
03361 q931c0 = p0->call;
03362 q931c1 = p1->call;
03363 if (p0->transfer && p1->transfer
03364 && q931c0 && q931c1
03365 && !triedtopribridge) {
03366 pri_channel_bridge(q931c0, q931c1);
03367 triedtopribridge = 1;
03368 }
03369 #endif
03370
03371 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
03372 if (!who) {
03373 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
03374 continue;
03375 }
03376 f = ast_read(who);
03377 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
03378 *fo = f;
03379 *rc = who;
03380 res = AST_BRIDGE_COMPLETE;
03381 goto return_from_bridge;
03382 }
03383 if (f->frametype == AST_FRAME_DTMF) {
03384 if ((who == c0) && p0->pulsedial) {
03385 ast_write(c1, f);
03386 } else if ((who == c1) && p1->pulsedial) {
03387 ast_write(c0, f);
03388 } else {
03389 *fo = f;
03390 *rc = who;
03391 res = AST_BRIDGE_COMPLETE;
03392 goto return_from_bridge;
03393 }
03394 }
03395 ast_frfree(f);
03396
03397
03398 priority = !priority;
03399 }
03400
03401 return_from_bridge:
03402 if (op0 == p0)
03403 zt_enable_ec(p0);
03404
03405 if (op1 == p1)
03406 zt_enable_ec(p1);
03407
03408 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03409 enable_dtmf_detect(op0);
03410
03411 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03412 enable_dtmf_detect(op1);
03413
03414 zt_unlink(slave, master, 1);
03415
03416 return res;
03417 }
03418
03419 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03420 {
03421 struct zt_pvt *p = newchan->tech_pvt;
03422 int x;
03423 ast_mutex_lock(&p->lock);
03424 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
03425 if (p->owner == oldchan) {
03426 p->owner = newchan;
03427 }
03428 for (x = 0; x < 3; x++)
03429 if (p->subs[x].owner == oldchan) {
03430 if (!x)
03431 zt_unlink(NULL, p, 0);
03432 p->subs[x].owner = newchan;
03433 }
03434 if (newchan->_state == AST_STATE_RINGING)
03435 zt_indicate(newchan, AST_CONTROL_RINGING, NULL, 0);
03436 update_conf(p);
03437 ast_mutex_unlock(&p->lock);
03438 return 0;
03439 }
03440
03441 static int zt_ring_phone(struct zt_pvt *p)
03442 {
03443 int x;
03444 int res;
03445
03446 x = 0;
03447 x = ZT_ONHOOK;
03448 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03449 do {
03450 x = ZT_RING;
03451 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03452 if (res) {
03453 switch (errno) {
03454 case EBUSY:
03455 case EINTR:
03456
03457 usleep(10000);
03458 continue;
03459 case EINPROGRESS:
03460 res = 0;
03461 break;
03462 default:
03463 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
03464 res = 0;
03465 }
03466 }
03467 } while (res);
03468 return res;
03469 }
03470
03471 static void *ss_thread(void *data);
03472
03473 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
03474
03475 static int attempt_transfer(struct zt_pvt *p)
03476 {
03477
03478
03479
03480 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
03481
03482
03483 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
03484 ast_queue_control(p->subs[SUB_THREEWAY].owner, AST_CONTROL_UNHOLD);
03485 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
03486 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
03487 }
03488 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RING) {
03489 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, ZT_TONE_RINGTONE);
03490 }
03491 if (p->subs[SUB_REAL].owner->cdr) {
03492
03493 p->subs[SUB_THREEWAY].owner->cdr =
03494 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
03495 p->subs[SUB_REAL].owner->cdr = NULL;
03496 }
03497 if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
03498
03499 p->subs[SUB_THREEWAY].owner->cdr =
03500 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
03501 ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
03502 }
03503 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
03504 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03505 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
03506 return -1;
03507 }
03508
03509 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03510 unalloc_sub(p, SUB_THREEWAY);
03511 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
03512 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
03513 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
03514 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
03515 }
03516 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RING) {
03517 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
03518 }
03519 if (p->subs[SUB_THREEWAY].owner->cdr) {
03520
03521 p->subs[SUB_REAL].owner->cdr =
03522 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
03523 p->subs[SUB_THREEWAY].owner->cdr = NULL;
03524 }
03525 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr) {
03526
03527 p->subs[SUB_REAL].owner->cdr =
03528 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr);
03529 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr = NULL;
03530 }
03531 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
03532 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03533 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
03534 return -1;
03535 }
03536
03537 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03538 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
03539 unalloc_sub(p, SUB_THREEWAY);
03540
03541 return 1;
03542 } else {
03543 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
03544 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
03545 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03546 return -1;
03547 }
03548 return 0;
03549 }
03550
03551 static int check_for_conference(struct zt_pvt *p)
03552 {
03553 ZT_CONFINFO ci;
03554
03555 if (p->master || (p->confno > -1))
03556 return 0;
03557 memset(&ci, 0, sizeof(ci));
03558 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
03559 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
03560 return 0;
03561 }
03562
03563
03564
03565 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
03566 if (option_verbose > 2)
03567 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
03568 return 1;
03569 }
03570 return 0;
03571 }
03572
03573 static int get_alarms(struct zt_pvt *p)
03574 {
03575 int res;
03576 ZT_SPANINFO zi;
03577 memset(&zi, 0, sizeof(zi));
03578 zi.spanno = p->span;
03579 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
03580 if (res < 0) {
03581 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
03582 return 0;
03583 }
03584 return zi.alarms;
03585 }
03586
03587 static void zt_handle_dtmfup(struct ast_channel *ast, int index, struct ast_frame **dest)
03588 {
03589 struct zt_pvt *p = ast->tech_pvt;
03590 struct ast_frame *f = *dest;
03591
03592 ast_log(LOG_DEBUG, "DTMF digit: %c on %s\n", f->subclass, ast->name);
03593 if (p->confirmanswer) {
03594 ast_log(LOG_DEBUG, "Confirm answer on %s!\n", ast->name);
03595
03596
03597 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03598 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03599 *dest = &p->subs[index].f;
03600
03601 p->confirmanswer = 0;
03602 } else if (p->callwaitcas) {
03603 if ((f->subclass == 'A') || (f->subclass == 'D')) {
03604 ast_log(LOG_DEBUG, "Got some DTMF, but it's for the CAS\n");
03605 if (p->cidspill)
03606 free(p->cidspill);
03607 send_cwcidspill(p);
03608 }
03609 if ((f->subclass != 'm') && (f->subclass != 'u'))
03610 p->callwaitcas = 0;
03611 p->subs[index].f.frametype = AST_FRAME_NULL;
03612 p->subs[index].f.subclass = 0;
03613 *dest = &p->subs[index].f;
03614 } else if (f->subclass == 'f') {
03615
03616 if ((p->callprogress & 0x6) && !p->faxhandled) {
03617 p->faxhandled++;
03618 if (strcmp(ast->exten, "fax")) {
03619 const char *target_context = S_OR(ast->macrocontext, ast->context);
03620
03621 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
03622 if (option_verbose > 2)
03623 ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
03624
03625 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
03626 if (ast_async_goto(ast, target_context, "fax", 1))
03627 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
03628 } else
03629 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
03630 } else
03631 ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
03632 } else
03633 ast_log(LOG_DEBUG, "Fax already handled\n");
03634 zt_confmute(p, 0);
03635 p->subs[index].f.frametype = AST_FRAME_NULL;
03636 p->subs[index].f.subclass = 0;
03637 *dest = &p->subs[index].f;
03638 } else if (f->subclass == 'm') {
03639
03640 zt_confmute(p, 1);
03641 p->subs[index].f.frametype = AST_FRAME_NULL;
03642 p->subs[index].f.subclass = 0;
03643 *dest = &p->subs[index].f;
03644 } else if (f->subclass == 'u') {
03645
03646 zt_confmute(p, 0);
03647 p->subs[index].f.frametype = AST_FRAME_NULL;
03648 p->subs[index].f.subclass = 0;
03649 *dest = &p->subs[index].f;
03650 } else
03651 zt_confmute(p, 0);
03652 }
03653
03654 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
03655 {
03656 int res, x;
03657 int index, mysig;
03658 char *c;
03659 struct zt_pvt *p = ast->tech_pvt;
03660 pthread_t threadid;
03661 pthread_attr_t attr;
03662 struct ast_channel *chan;
03663 struct ast_frame *f;
03664
03665 index = zt_get_index(ast, p, 0);
03666 mysig = p->sig;
03667 if (p->outsigmod > -1)
03668 mysig = p->outsigmod;
03669 p->subs[index].f.frametype = AST_FRAME_NULL;
03670 p->subs[index].f.subclass = 0;
03671 p->subs[index].f.datalen = 0;
03672 p->subs[index].f.samples = 0;
03673 p->subs[index].f.mallocd = 0;
03674 p->subs[index].f.offset = 0;
03675 p->subs[index].f.src = "zt_handle_event";
03676 p->subs[index].f.data = NULL;
03677 f = &p->subs[index].f;
03678
03679 if (index < 0)
03680 return &p->subs[index].f;
03681 if (p->fake_event) {
03682 res = p->fake_event;
03683 p->fake_event = 0;
03684 } else
03685 res = zt_get_event(p->subs[index].zfd);
03686
03687 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
03688
03689 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFUP)) {
03690 p->pulsedial = (res & ZT_EVENT_PULSEDIGIT) ? 1 : 0;
03691
03692 ast_log(LOG_DEBUG, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse ": "", res & 0xff);
03693 #ifdef HAVE_PRI
03694 if (!p->proceeding && p->sig == SIG_PRI && p->pri && p->pri->overlapdial) {
03695
03696 } else {
03697 #endif
03698 p->subs[index].f.frametype = AST_FRAME_DTMF_END;
03699 p->subs[index].f.subclass = res & 0xff;
03700 #ifdef HAVE_PRI
03701 }
03702 #endif
03703 zt_handle_dtmfup(ast, index, &f);
03704 return f;
03705 }
03706
03707 if (res & ZT_EVENT_DTMFDOWN) {
03708 ast_log(LOG_DEBUG, "DTMF Down '%c'\n", res & 0xff);
03709
03710 zt_confmute(p, 1);
03711 p->subs[index].f.frametype = AST_FRAME_DTMF_BEGIN;
03712 p->subs[index].f.subclass = res & 0xff;
03713 return &p->subs[index].f;
03714 }
03715
03716 switch (res) {
03717 #ifdef ZT_EVENT_EC_DISABLED
03718 case ZT_EVENT_EC_DISABLED:
03719 if (option_verbose > 2)
03720 ast_verbose(VERBOSE_PREFIX_3 "Channel %d echo canceler disabled due to CED detection\n", p->channel);
03721 p->echocanon = 0;
03722 break;
03723 #endif
03724 case ZT_EVENT_BITSCHANGED:
03725 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
03726 case ZT_EVENT_PULSE_START:
03727
03728 if (!ast->pbx)
03729 tone_zone_play_tone(p->subs[index].zfd, -1);
03730 break;
03731 case ZT_EVENT_DIALCOMPLETE:
03732 if (p->inalarm) break;
03733 if ((p->radio || (p->oprmode < 0))) break;
03734 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
03735 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
03736 return NULL;
03737 }
03738 if (!x) {
03739 zt_enable_ec(p);
03740 if (p->echobreak) {
03741 zt_train_ec(p);
03742 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
03743 p->dop.op = ZT_DIAL_OP_REPLACE;
03744 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
03745 p->echobreak = 0;
03746 } else {
03747 p->dialing = 0;
03748 if ((mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF)) {
03749
03750 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
03751 ast_setstate(ast, AST_STATE_UP);
03752 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03753 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03754 break;
03755 } else {
03756
03757 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
03758 }
03759 }
03760 if (ast->_state == AST_STATE_DIALING) {
03761 if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
03762 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
03763 } 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)))) {
03764 ast_setstate(ast, AST_STATE_RINGING);
03765 } else if (!p->answeronpolarityswitch) {
03766 ast_setstate(ast, AST_STATE_UP);
03767 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03768 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03769 }
03770 }
03771 }
03772 }
03773 break;
03774 case ZT_EVENT_ALARM:
03775 #ifdef HAVE_PRI
03776 if (!p->pri || !p->pri->pri || (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0)) {
03777
03778 if (p->call) {
03779 if (p->pri && p->pri->pri) {
03780 if (!pri_grab(p, p->pri)) {
03781 pri_hangup(p->pri->pri, p->call, -1);
03782 pri_destroycall(p->pri->pri, p->call);
03783 p->call = NULL;
03784 pri_rel(p->pri);
03785 } else
03786 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
03787 } else
03788 ast_log(LOG_WARNING, "The PRI Call has not been destroyed\n");
03789 }
03790 if (p->owner)
03791 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
03792 }
03793 if (p->bearer)
03794 p->bearer->inalarm = 1;
03795 else
03796 #endif
03797 p->inalarm = 1;
03798 res = get_alarms(p);
03799 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
03800 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
03801 "Alarm: %s\r\n"
03802 "Channel: %d\r\n",
03803 alarm2str(res), p->channel);
03804 #ifdef HAVE_LIBPRI
03805 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
03806
03807 } else {
03808 break;
03809 }
03810 #endif
03811 case ZT_EVENT_ONHOOK:
03812 if (p->radio) {
03813 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03814 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
03815 break;
03816 }
03817 if (p->oprmode < 0)
03818 {
03819 if (p->oprmode != -1) break;
03820 if ((p->sig == SIG_FXOLS) || (p->sig == SIG_FXOKS) || (p->sig == SIG_FXOGS))
03821 {
03822
03823 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_RINGOFF);
03824 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_RING);
03825 save_conference(p->oprpeer);
03826 tone_zone_play_tone(p->oprpeer->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
03827 }
03828 break;
03829 }
03830 switch (p->sig) {
03831 case SIG_FXOLS:
03832 case SIG_FXOGS:
03833 case SIG_FXOKS:
03834 p->onhooktime = time(NULL);
03835 p->msgstate = -1;
03836
03837 if (index == SUB_REAL) {
03838
03839 if (p->subs[SUB_CALLWAIT].owner) {
03840
03841 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
03842 if (option_verbose > 2)
03843 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
03844 unalloc_sub(p, SUB_CALLWAIT);
03845 #if 0
03846 p->subs[index].needanswer = 0;
03847 p->subs[index].needringing = 0;
03848 #endif
03849 p->callwaitingrepeat = 0;
03850 p->cidcwexpire = 0;
03851 p->owner = NULL;
03852
03853 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
03854 p->dialing = 1;
03855 zt_ring_phone(p);
03856 } else if (p->subs[SUB_THREEWAY].owner) {
03857 unsigned int mssinceflash;
03858
03859
03860 while (p->subs[SUB_THREEWAY].owner && ast_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) {
03861
03862 ast_mutex_unlock(&p->lock);
03863 ast_mutex_unlock(&ast->lock);
03864 usleep(1);
03865
03866
03867
03868 ast_mutex_lock(&ast->lock);
03869 ast_mutex_lock(&p->lock);
03870 if (p->owner != ast) {
03871 ast_log(LOG_WARNING, "This isn't good...\n");
03872 return NULL;
03873 }
03874 }
03875 if (!p->subs[SUB_THREEWAY].owner) {
03876 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
03877 return NULL;
03878 }
03879 mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
03880 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
03881 if (mssinceflash < MIN_MS_SINCE_FLASH) {
03882
03883
03884 if (p->subs[SUB_THREEWAY].owner)
03885 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
03886 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03887 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
03888 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03889 } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
03890 if (p->transfer) {
03891
03892 p->subs[SUB_REAL].inthreeway = 0;
03893 p->subs[SUB_THREEWAY].inthreeway = 0;
03894
03895 if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
03896 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03897
03898 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03899 p->owner = NULL;
03900
03901 zt_ring_phone(p);
03902 } else {
03903 if ((res = attempt_transfer(p)) < 0) {
03904 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03905 if (p->subs[SUB_THREEWAY].owner)
03906 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03907 } else if (res) {
03908
03909 if (p->subs[SUB_THREEWAY].owner)
03910 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03911 break;
03912 }
03913 }
03914 } else {
03915 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03916 if (p->subs[SUB_THREEWAY].owner)
03917 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03918 }
03919 } else {
03920 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03921
03922 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03923 p->owner = NULL;
03924
03925 zt_ring_phone(p);
03926 }
03927 }
03928 } else {
03929 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
03930 }
03931
03932 default:
03933 zt_disable_ec(p);
03934 return NULL;
03935 }
03936 break;
03937 case ZT_EVENT_RINGOFFHOOK:
03938 if (p->inalarm) break;
03939 if (p->oprmode < 0)
03940 {
03941 if ((p->sig == SIG_FXOLS) || (p->sig == SIG_FXOKS) || (p->sig == SIG_FXOGS))
03942 {
03943
03944 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_RINGOFF);
03945 tone_zone_play_tone(p->oprpeer->subs[SUB_REAL].zfd, -1);
03946 restore_conference(p->oprpeer);
03947 }
03948 break;
03949 }
03950 if (p->radio)
03951 {
03952 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03953 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
03954 break;
03955 }
03956
03957
03958 if (((mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF)) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
03959 c = strchr(p->dialdest, '/');
03960 if (c)
03961 c++;
03962 else
03963 c = p->dialdest;
03964 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
03965 else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
03966 if (strlen(p->dop.dialstr) > 4) {
03967 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
03968 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
03969 p->echorest[sizeof(p->echorest) - 1] = '\0';
03970 p->echobreak = 1;
03971 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
03972 } else
03973 p->echobreak = 0;
03974 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
03975 x = ZT_ONHOOK;
03976 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03977 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
03978 return NULL;
03979 }
03980 p->dialing = 1;
03981 return &p->subs[index].f;
03982 }
03983 switch (p->sig) {
03984 case SIG_FXOLS:
03985 case SIG_FXOGS:
03986 case SIG_FXOKS:
03987 switch (ast->_state) {
03988 case AST_STATE_RINGING:
03989 zt_enable_ec(p);
03990 zt_train_ec(p);
03991 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03992 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03993
03994 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
03995 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
03996 if (p->cidspill) {
03997
03998 free(p->cidspill);
03999 p->cidspill = NULL;
04000 }
04001 p->dialing = 0;
04002 p->callwaitcas = 0;
04003 if (p->confirmanswer) {
04004
04005 p->subs[index].f.frametype = AST_FRAME_NULL;
04006 p->subs[index].f.subclass = 0;
04007 } else if (!ast_strlen_zero(p->dop.dialstr)) {
04008
04009 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04010 if (res < 0) {
04011 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04012 p->dop.dialstr[0] = '\0';
04013 return NULL;
04014 } else {
04015 ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
04016 p->subs[index].f.frametype = AST_FRAME_NULL;
04017 p->subs[index].f.subclass = 0;
04018 p->dialing = 1;
04019 }
04020 p->dop.dialstr[0] = '\0';
04021 ast_setstate(ast, AST_STATE_DIALING);
04022 } else
04023 ast_setstate(ast, AST_STATE_UP);
04024 return &p->subs[index].f;
04025 case AST_STATE_DOWN:
04026 ast_setstate(ast, AST_STATE_RING);
04027 ast->rings = 1;
04028 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04029 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
04030 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
04031 return &p->subs[index].f;
04032 case AST_STATE_UP:
04033
04034 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
04035
04036 if (ast_bridged_channel(p->owner))
04037 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
04038 p->subs[index].needunhold = 1;
04039 break;
04040 case AST_STATE_RESERVED:
04041
04042 if (has_voicemail(p))
04043 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
04044 else
04045 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
04046 break;
04047 default:
04048 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
04049 }
04050 break;
04051 case SIG_FXSLS:
04052 case SIG_FXSGS:
04053 case SIG_FXSKS:
04054 if (ast->_state == AST_STATE_RING) {
04055 p->ringt = p->ringt_base;
04056 }
04057
04058
04059
04060 ast_log(LOG_DEBUG, "Setting IDLE polarity due "
04061 "to ring. Old polarity was %d\n",
04062 p->polarity);
04063 p->polarity = POLARITY_IDLE;
04064
04065
04066 case SIG_EM:
04067 case SIG_EM_E1:
04068 case SIG_EMWINK:
04069 case SIG_FEATD:
04070 case SIG_FEATDMF:
04071 case SIG_FEATDMF_TA:
04072 case SIG_E911:
04073 case SIG_FGC_CAMA:
04074 case SIG_FGC_CAMAMF:
04075 case SIG_FEATB:
04076 case SIG_SF:
04077 case SIG_SFWINK:
04078 case SIG_SF_FEATD:
04079 case SIG_SF_FEATDMF:
04080 case SIG_SF_FEATB:
04081 if (ast->_state == AST_STATE_PRERING)
04082 ast_setstate(ast, AST_STATE_RING);
04083 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
04084 if (option_debug)
04085 ast_log(LOG_DEBUG, "Ring detected\n");
04086 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04087 p->subs[index].f.subclass = AST_CONTROL_RING;
04088 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
04089 if (option_debug)
04090 ast_log(LOG_DEBUG, "Line answered\n");
04091 if (p->confirmanswer) {
04092 p->subs[index].f.frametype = AST_FRAME_NULL;
04093 p->subs[index].f.subclass = 0;
04094 } else {
04095 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04096 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04097 ast_setstate(ast, AST_STATE_UP);
04098 }
04099 } else if (ast->_state != AST_STATE_RING)
04100 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
04101 break;
04102 default:
04103 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
04104 }
04105 break;
04106 #ifdef ZT_EVENT_RINGBEGIN
04107 case ZT_EVENT_RINGBEGIN:
04108 switch (p->sig) {
04109 case SIG_FXSLS:
04110 case SIG_FXSGS:
04111 case SIG_FXSKS:
04112 if (ast->_state == AST_STATE_RING) {
04113 p->ringt = p->ringt_base;
04114 }
04115 break;
04116 }
04117 break;
04118 #endif
04119 case ZT_EVENT_RINGEROFF:
04120 if (p->inalarm) break;
04121 if ((p->radio || (p->oprmode < 0))) break;
04122 ast->rings++;
04123 if ((ast->rings > p->cidrings) && (p->cidspill)) {
04124 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
04125 free(p->cidspill);
04126 p->cidspill = NULL;
04127 p->callwaitcas = 0;
04128 }
04129 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04130 p->subs[index].f.subclass = AST_CONTROL_RINGING;
04131 break;
04132 case ZT_EVENT_RINGERON:
04133 break;
04134 case ZT_EVENT_NOALARM:
04135 p->inalarm = 0;
04136 #ifdef HAVE_PRI
04137
04138 if (p->bearer)
04139 p->bearer->inalarm = 0;
04140 #endif
04141 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
04142 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
04143 "Channel: %d\r\n", p->channel);
04144 break;
04145 case ZT_EVENT_WINKFLASH:
04146 if (p->inalarm) break;
04147 if (p->radio) break;
04148 if (p->oprmode < 0) break;
04149 if (p->oprmode > 1)
04150 {
04151 struct zt_params par;
04152
04153 if (ioctl(p->oprpeer->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par) != -1)
04154 {
04155 if (!par.rxisoffhook)
04156 {
04157
04158 zt_set_hook(p->oprpeer->subs[SUB_REAL].zfd, ZT_RINGOFF);
04159 zt_set_hook(p->oprpeer->subs[SUB_REAL].zfd, ZT_RING);
04160 save_conference(p);
04161 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
04162 }
04163 }
04164 break;
04165 }
04166
04167 gettimeofday(&p->flashtime, NULL);
04168 switch (mysig) {
04169 case SIG_FXOLS:
04170 case SIG_FXOGS:
04171 case SIG_FXOKS:
04172 ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
04173 index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
04174 p->callwaitcas = 0;
04175
04176 if (index != SUB_REAL) {
04177 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
04178 goto winkflashdone;
04179 }
04180
04181 if (p->subs[SUB_CALLWAIT].owner) {
04182
04183 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
04184 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
04185 p->owner = p->subs[SUB_REAL].owner;
04186 ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
04187 if (p->owner->_state == AST_STATE_RINGING) {
04188 ast_setstate(p->owner, AST_STATE_UP);
04189 p->subs[SUB_REAL].needanswer = 1;
04190 }
04191 p->callwaitingrepeat = 0;
04192 p->cidcwexpire = 0;
04193
04194 if (!p->subs[SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
04195 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
04196 S_OR(p->mohsuggest, NULL),
04197 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04198 }
04199 p->subs[SUB_CALLWAIT].needhold = 1;
04200 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
04201 ast_queue_control_data(p->subs[SUB_REAL].owner, AST_CONTROL_HOLD,
04202 S_OR(p->mohsuggest, NULL),
04203 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04204 }
04205 p->subs[SUB_REAL].needunhold = 1;
04206 } else if (!p->subs[SUB_THREEWAY].owner) {
04207 char cid_num[256];
04208 char cid_name[256];
04209
04210 if (!p->threewaycalling) {
04211
04212 p->subs[SUB_REAL].needflash = 1;
04213 goto winkflashdone;
04214 } else if (!check_for_conference(p)) {
04215 if (p->zaptrcallerid && p->owner) {
04216 if (p->owner->cid.cid_num)
04217 ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
04218 if (p->owner->cid.cid_name)
04219 ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
04220 }
04221
04222
04223 if (!((ast->pbx) ||
04224 (ast->_state == AST_STATE_UP) ||
04225 (ast->_state == AST_STATE_RING))) {
04226 ast_log(LOG_DEBUG, "Flash when call not up or ringing\n");
04227 goto winkflashdone;
04228 }
04229 if (alloc_sub(p, SUB_THREEWAY)) {
04230 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
04231 goto winkflashdone;
04232 }
04233
04234 chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
04235 if (p->zaptrcallerid) {
04236 if (!p->origcid_num)
04237 p->origcid_num = ast_strdup(p->cid_num);
04238 if (!p->origcid_name)
04239 p->origcid_name = ast_strdup(p->cid_name);
04240 ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
04241 ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
04242 }
04243
04244 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04245
04246 zt_disable_ec(p);
04247 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
04248 if (res)
04249 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
04250 p->owner = chan;
04251 pthread_attr_init(&attr);
04252 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
04253 if (!chan) {
04254 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
04255 } else if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
04256 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
04257 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
04258 zt_enable_ec(p);
04259 ast_hangup(chan);
04260 } else {
04261 if (option_verbose > 2)
04262 ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);
04263
04264 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
04265 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
04266 S_OR(p->mohsuggest, NULL),
04267 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04268 }
04269 p->subs[SUB_THREEWAY].needhold = 1;
04270 }
04271 pthread_attr_destroy(&attr);
04272 }
04273 } else {
04274
04275 if (p->subs[SUB_THREEWAY].inthreeway) {
04276
04277 if (option_debug)
04278 ast_log(LOG_DEBUG, "Got flash with three way call up, dropping last call on %d\n", p->channel);
04279
04280 if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
04281
04282 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04283 p->owner = p->subs[SUB_REAL].owner;
04284 }
04285
04286 if (option_verbose > 2)
04287 ast_verbose(VERBOSE_PREFIX_3 "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
04288 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04289 p->subs[SUB_REAL].inthreeway = 0;
04290 p->subs[SUB_THREEWAY].inthreeway = 0;
04291 } else {
04292
04293 if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
04294 (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
04295 int otherindex = SUB_THREEWAY;
04296
04297 if (option_verbose > 2)
04298 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);
04299
04300 p->subs[SUB_THREEWAY].inthreeway = 1;
04301 p->subs[SUB_REAL].inthreeway = 1;
04302 if (ast->_state == AST_STATE_UP) {
04303 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04304 otherindex = SUB_REAL;
04305 }
04306 if (p->subs[otherindex].owner && ast_bridged_channel(p->subs[otherindex].owner))
04307 ast_queue_control(p->subs[otherindex].owner, AST_CONTROL_UNHOLD);
04308 p->subs[otherindex].needunhold = 1;
04309 p->owner = p->subs[SUB_REAL].owner;
04310 if (ast->_state == AST_STATE_RINGING) {
04311 ast_log(LOG_DEBUG, "Enabling ringtone on real and threeway\n");
04312 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
04313 res = tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, ZT_TONE_RINGTONE);
04314 }
04315 } else {
04316 if (option_verbose > 2)
04317 ast_verbose(VERBOSE_PREFIX_3 "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name);
04318 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04319 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04320 p->owner = p->subs[SUB_REAL].owner;
04321 if (p->subs[SUB_REAL].owner && ast_bridged_channel(p->subs[SUB_REAL].owner))
04322 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
04323 p->subs[SUB_REAL].needunhold = 1;
04324 zt_enable_ec(p);
04325 }
04326
04327 }
04328 }
04329 winkflashdone:
04330 update_conf(p);
04331 break;
04332 case SIG_EM:
04333 case SIG_EM_E1:
04334 case SIG_EMWINK:
04335 case SIG_FEATD:
04336 case SIG_SF:
04337 case SIG_SFWINK:
04338 case SIG_SF_FEATD:
04339 case SIG_FXSLS:
04340 case SIG_FXSGS:
04341 if (p->dialing)
04342 ast_log(LOG_DEBUG, "Ignoring wink on channel %d\n", p->channel);
04343 else
04344 ast_log(LOG_DEBUG, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
04345 break;
04346 case SIG_FEATDMF_TA:
04347 switch (p->whichwink) {
04348 case 0:
04349 ast_log(LOG_DEBUG, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04350 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04351 break;
04352 case 1:
04353 ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
04354 break;
04355 case 2:
04356 ast_log(LOG_WARNING, "Received unexpected wink on channel of type SIG_FEATDMF_TA\n");
04357 return NULL;
04358 }
04359 p->whichwink++;
04360
04361 case SIG_FEATDMF:
04362 case SIG_E911:
04363 case SIG_FGC_CAMAMF:
04364 case SIG_FGC_CAMA:
04365 case SIG_FEATB:
04366 case SIG_SF_FEATDMF:
04367 case SIG_SF_FEATB:
04368
04369 if (!ast_strlen_zero(p->dop.dialstr))
04370 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04371 else if (res < 0) {
04372 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04373 p->dop.dialstr[0] = '\0';
04374 return NULL;
04375 } else
04376 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04377 p->dop.dialstr[0] = '\0';
04378 break;
04379 default:
04380 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
04381 }
04382 break;
04383 case ZT_EVENT_HOOKCOMPLETE:
04384 if (p->inalarm) break;
04385 if ((p->radio || (p->oprmode < 0))) break;
04386 switch (mysig) {
04387 case SIG_FXSLS:
04388 case SIG_FXSGS:
04389 case SIG_FXSKS:
04390 case SIG_EM:
04391 case SIG_EM_E1:
04392 case SIG_EMWINK:
04393 case SIG_FEATD:
04394 case SIG_SF:
04395 case SIG_SFWINK:
04396 case SIG_SF_FEATD:
04397 if (!ast_strlen_zero(p->dop.dialstr))
04398 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04399 else if (res < 0) {
04400 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04401 p->dop.dialstr[0] = '\0';
04402 return NULL;
04403 } else
04404 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04405 p->dop.dialstr[0] = '\0';
04406 p->dop.op = ZT_DIAL_OP_REPLACE;
04407 break;
04408 case SIG_FEATDMF:
04409 case SIG_FEATDMF_TA:
04410 case SIG_E911:
04411 case SIG_FGC_CAMA:
04412 case SIG_FGC_CAMAMF:
04413 case SIG_FEATB:
04414 case SIG_SF_FEATDMF:
04415 case SIG_SF_FEATB:
04416 ast_log(LOG_DEBUG, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
04417 break;
04418 default:
04419 break;
04420 }
04421 break;
04422 case ZT_EVENT_POLARITY:
04423
04424
04425
04426
04427
04428
04429 if (p->polarity == POLARITY_IDLE) {
04430 p->polarity = POLARITY_REV;
04431 if (p->answeronpolarityswitch &&
04432 ((ast->_state == AST_STATE_DIALING) ||
04433 (ast->_state == AST_STATE_RINGING))) {
04434 ast_log(LOG_DEBUG, "Answering on polarity switch!\n");
04435 ast_setstate(p->owner, AST_STATE_UP);
04436 if (p->hanguponpolarityswitch) {
04437 gettimeofday(&p->polaritydelaytv, NULL);
04438 }
04439 } else
04440 ast_log(LOG_DEBUG, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, ast->_state);
04441 }
04442
04443
04444 if (p->hanguponpolarityswitch &&
04445 (p->polarityonanswerdelay > 0) &&
04446 (p->polarity == POLARITY_REV) &&
04447 ((ast->_state == AST_STATE_UP) || (ast->_state == AST_STATE_RING)) ) {
04448
04449 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) );
04450
04451 if (ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
04452 ast_log(LOG_DEBUG, "Polarity Reversal detected and now Hanging up on channel %d\n", p->channel);
04453 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
04454 p->polarity = POLARITY_IDLE;
04455 } else {
04456 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);
04457 }
04458 } else {
04459 p->polarity = POLARITY_IDLE;
04460 ast_log(LOG_DEBUG, "Ignoring Polarity switch to IDLE on channel %d, state %d\n", p->channel, ast->_state);
04461 }
04462
04463 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) );
04464 break;
04465 default:
04466 ast_log(LOG_DEBUG, "Dunno what to do with event %d on channel %d\n", res, p->channel);
04467 }
04468 return &p->subs[index].f;
04469 }
04470
04471 static struct ast_frame *__zt_exception(struct ast_channel *ast)
04472 {
04473 struct zt_pvt *p = ast->tech_pvt;
04474 int res;
04475 int usedindex=-1;
04476 int index;
04477 struct ast_frame *f;
04478
04479
04480 index = zt_get_index(ast, p, 1);
04481
04482 p->subs[index].f.frametype = AST_FRAME_NULL;
04483 p->subs[index].f.datalen = 0;
04484 p->subs[index].f.samples = 0;
04485 p->subs[index].f.mallocd = 0;
04486 p->subs[index].f.offset = 0;
04487 p->subs[index].f.subclass = 0;
04488 p->subs[index].f.delivery = ast_tv(0,0);
04489 p->subs[index].f.src = "zt_exception";
04490 p->subs[index].f.data = NULL;
04491
04492
04493 if ((!p->owner) && (!(p->radio || (p->oprmode < 0)))) {
04494
04495
04496
04497
04498
04499 if (p->fake_event) {
04500 res = p->fake_event;
04501 p->fake_event = 0;
04502 } else
04503 res = zt_get_event(p->subs[SUB_REAL].zfd);
04504
04505 if ((res != ZT_EVENT_RINGEROFF) && (res != ZT_EVENT_RINGERON) &&
04506 (res != ZT_EVENT_HOOKCOMPLETE)) {
04507 ast_log(LOG_DEBUG, "Restoring owner of channel %d on event %d\n", p->channel, res);
04508 p->owner = p->subs[SUB_REAL].owner;
04509 if (p->owner && ast_bridged_channel(p->owner))
04510 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
04511 p->subs[SUB_REAL].needunhold = 1;
04512 }
04513 switch (res) {
04514 case ZT_EVENT_ONHOOK:
04515 zt_disable_ec(p);
04516 if (p->owner) {
04517 if (option_verbose > 2)
04518 ast_verbose(VERBOSE_PREFIX_3 "Channel %s still has call, ringing phone\n", p->owner->name);
04519 zt_ring_phone(p);
04520 p->callwaitingrepeat = 0;
04521 p->cidcwexpire = 0;
04522 } else
04523 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04524 update_conf(p);
04525 break;
04526 case ZT_EVENT_RINGOFFHOOK:
04527 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
04528 if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
04529 p->subs[SUB_REAL].needanswer = 1;
04530 p->dialing = 0;
04531 }
04532 break;
04533 case ZT_EVENT_HOOKCOMPLETE:
04534 case ZT_EVENT_RINGERON:
04535 case ZT_EVENT_RINGEROFF:
04536
04537 break;
04538 case ZT_EVENT_WINKFLASH:
04539 gettimeofday(&p->flashtime, NULL);
04540 if (p->owner) {
04541 if (option_verbose > 2)
04542 ast_verbose(VERBOSE_PREFIX_3 "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
04543 if (p->owner->_state != AST_STATE_UP) {
04544
04545 usedindex = zt_get_index(p->owner, p, 0);
04546 if (usedindex > -1) {
04547 p->subs[usedindex].needanswer = 1;
04548 }
04549 ast_setstate(p->owner, AST_STATE_UP);
04550 }
04551 p->callwaitingrepeat = 0;
04552 p->cidcwexpire = 0;
04553 if (ast_bridged_channel(p->owner))
04554 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
04555 p->subs[SUB_REAL].needunhold = 1;
04556 } else
04557 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04558 update_conf(p);
04559 break;
04560 default:
04561 ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
04562 }
04563 f = &p->subs[index].f;
04564 return f;
04565 }
04566 if (!(p->radio || (p->oprmode < 0))) ast_log(LOG_DEBUG, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
04567
04568 if (ast != p->owner) {
04569 ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
04570 f = &p->subs[index].f;
04571 return f;
04572 }
04573 f = zt_handle_event(ast);
04574 return f;
04575 }
04576
04577 static struct ast_frame *zt_exception(struct ast_channel *ast)
04578 {
04579 struct zt_pvt *p = ast->tech_pvt;
04580 struct ast_frame *f;
04581 ast_mutex_lock(&p->lock);
04582 f = __zt_exception(ast);
04583 ast_mutex_unlock(&p->lock);
04584 return f;
04585 }
04586
04587 static struct ast_frame *zt_read(struct ast_channel *ast)
04588 {
04589 struct zt_pvt *p = ast->tech_pvt;
04590 int res;
04591 int index;
04592 void *readbuf;
04593 struct ast_frame *f;
04594
04595
04596 ast_mutex_lock(&p->lock);
04597
04598 index = zt_get_index(ast, p, 0);
04599
04600
04601 if (index < 0) {
04602 ast_log(LOG_WARNING, "We dont exist?\n");
04603 ast_mutex_unlock(&p->lock);
04604 return NULL;
04605 }
04606
04607 if ((p->radio || (p->oprmode < 0)) && p->inalarm) return NULL;
04608
04609 p->subs[index].f.frametype = AST_FRAME_NULL;
04610 p->subs[index].f.datalen = 0;
04611 p->subs[index].f.samples = 0;
04612 p->subs[index].f.mallocd = 0;
04613 p->subs[index].f.offset = 0;
04614 p->subs[index].f.subclass = 0;
04615 p->subs[index].f.delivery = ast_tv(0,0);
04616 p->subs[index].f.src = "zt_read";
04617 p->subs[index].f.data = NULL;
04618
04619
04620 if ((p->radio || (p->oprmode < 0)) && (!p->firstradio))
04621 {
04622 ZT_PARAMS ps;
04623
04624 ps.channo = p->channel;
04625 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
04626 ast_mutex_unlock(&p->lock);
04627 return NULL;
04628 }
04629 p->firstradio = 1;
04630 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04631 if (ps.rxisoffhook)
04632 {
04633 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
04634 }
04635 else
04636 {
04637 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
04638 }
04639 ast_mutex_unlock(&p->lock);
04640 return &p->subs[index].f;
04641 }
04642 if (p->ringt == 1) {
04643 ast_mutex_unlock(&p->lock);
04644 return NULL;
04645 }
04646 else if (p->ringt > 0)
04647 p->ringt--;
04648
04649 if (p->subs[index].needringing) {
04650
04651 p->subs[index].needringing = 0;
04652 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04653 p->subs[index].f.subclass = AST_CONTROL_RINGING;
04654 ast_setstate(ast, AST_STATE_RINGING);
04655 ast_mutex_unlock(&p->lock);
04656 return &p->subs[index].f;
04657 }
04658
04659 if (p->subs[index].needbusy) {
04660
04661 p->subs[index].needbusy = 0;
04662 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04663 p->subs[index].f.subclass = AST_CONTROL_BUSY;
04664 ast_mutex_unlock(&p->lock);
04665 return &p->subs[index].f;
04666 }
04667
04668 if (p->subs[index].needcongestion) {
04669
04670 p->subs[index].needcongestion = 0;
04671 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04672 p->subs[index].f.subclass = AST_CONTROL_CONGESTION;
04673 ast_mutex_unlock(&p->lock);
04674 return &p->subs[index].f;
04675 }
04676
04677 if (p->subs[index].needcallerid) {
04678 ast_set_callerid(ast, S_OR(p->lastcid_num, NULL),
04679 S_OR(p->lastcid_name, NULL),
04680 S_OR(p->lastcid_num, NULL)
04681 );
04682 p->subs[index].needcallerid = 0;
04683 }
04684
04685 if (p->subs[index].needanswer) {
04686
04687 p->subs[index].needanswer = 0;
04688 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04689 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04690 ast_mutex_unlock(&p->lock);
04691 return &p->subs[index].f;
04692 }
04693
04694 if (p->subs[index].needflash) {
04695
04696 p->subs[index].needflash = 0;
04697 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04698 p->subs[index].f.subclass = AST_CONTROL_FLASH;
04699 ast_mutex_unlock(&p->lock);
04700 return &p->subs[index].f;
04701 }
04702
04703 if (p->subs[index].needhold) {
04704
04705 p->subs[index].needhold = 0;
04706 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04707 p->subs[index].f.subclass = AST_CONTROL_HOLD;
04708 ast_mutex_unlock(&p->lock);
04709 ast_log(LOG_DEBUG, "Sending hold on '%s'\n", ast->name);
04710 return &p->subs[index].f;
04711 }
04712
04713 if (p->subs[index].needunhold) {
04714
04715 p->subs[index].needunhold = 0;
04716 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04717 p->subs[index].f.subclass = AST_CONTROL_UNHOLD;
04718 ast_mutex_unlock(&p->lock);
04719 ast_log(LOG_DEBUG, "Sending unhold on '%s'\n", ast->name);
04720 return &p->subs[index].f;
04721 }
04722
04723 if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
04724 if (!p->subs[index].linear) {
04725 p->subs[index].linear = 1;
04726 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04727 if (res)
04728 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, index);
04729 }
04730 } else if ((ast->rawreadformat == AST_FORMAT_ULAW) ||
04731 (ast->rawreadformat == AST_FORMAT_ALAW)) {
04732 if (p->subs[index].linear) {
04733 p->subs[index].linear = 0;
04734 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04735 if (res)
04736 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to companded mode.\n", p->channel, index);
04737 }
04738 } else {
04739 ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast->rawreadformat));
04740 ast_mutex_unlock(&p->lock);
04741 return NULL;
04742 }
04743 readbuf = ((unsigned char *)p->subs[index].buffer) + AST_FRIENDLY_OFFSET;
04744 CHECK_BLOCKING(ast);
04745 res = read(p->subs[index].zfd, readbuf, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04746 ast_clear_flag(ast, AST_FLAG_BLOCKING);
04747
04748 if (res < 0) {
04749 f = NULL;
04750 if (res == -1) {
04751 if (errno == EAGAIN) {
04752
04753 ast_mutex_unlock(&p->lock);
04754 return &p->subs[index].f;
04755 } else if (errno == ELAST) {
04756 f = __zt_exception(ast);
04757 } else
04758 ast_log(LOG_WARNING, "zt_rec: %s\n", strerror(errno));
04759 }
04760 ast_mutex_unlock(&p->lock);
04761 return f;
04762 }
04763 if (res != (p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE)) {
04764 ast_log(LOG_DEBUG, "Short read (%d/%d), must be an event...\n", res, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04765 f = __zt_exception(ast);
04766 ast_mutex_unlock(&p->lock);
04767 return f;
04768 }
04769 if (p->tdd) {
04770 int c;
04771
04772 c = tdd_feed(p->tdd,readbuf,READ_SIZE);
04773 if (c < 0) {
04774 ast_log(LOG_DEBUG,"tdd_feed failed\n");
04775 ast_mutex_unlock(&p->lock);
04776 return NULL;
04777 }
04778 if (c) {
04779 p->subs[index].f.subclass = 0;
04780 p->subs[index].f.frametype = AST_FRAME_TEXT;
04781 p->subs[index].f.mallocd = 0;
04782 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04783 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET;
04784 p->subs[index].f.datalen = 1;
04785 *((char *) p->subs[index].f.data) = c;
04786 ast_mutex_unlock(&p->lock);
04787 return &p->subs[index].f;
04788 }
04789 }
04790 if (p->callwaitingrepeat)
04791 p->callwaitingrepeat--;
04792 if (p->cidcwexpire)
04793 p->cidcwexpire--;
04794
04795 if (p->callwaitingrepeat == 1) {
04796 p->callwaitrings++;
04797 zt_callwait(ast);
04798 }
04799
04800 if (p->cidcwexpire == 1) {
04801 if (option_verbose > 2)
04802 ast_verbose(VERBOSE_PREFIX_3 "CPE does not support Call Waiting Caller*ID.\n");
04803 restore_conference(p);
04804 }
04805 if (p->subs[index].linear) {
04806 p->subs[index].f.datalen = READ_SIZE * 2;
04807 } else
04808 p->subs[index].f.datalen = READ_SIZE;
04809
04810
04811 if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
04812 send_callerid(p);
04813 }
04814
04815 p->subs[index].f.frametype = AST_FRAME_VOICE;
04816 p->subs[index].f.subclass = ast->rawreadformat;
04817 p->subs[index].f.samples = READ_SIZE;
04818 p->subs[index].f.mallocd = 0;
04819 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04820 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET / sizeof(p->subs[index].buffer[0]);
04821 #if 0
04822 ast_log(LOG_DEBUG, "Read %d of voice on %s\n", p->subs[index].f.datalen, ast->name);
04823 #endif
04824 if (p->dialing ||
04825 (index && (ast->_state != AST_STATE_UP)) ||
04826 ((index == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway)
04827 ) {
04828
04829
04830 p->subs[index].f.frametype = AST_FRAME_NULL;
04831 p->subs[index].f.subclass = 0;
04832 p->subs[index].f.samples = 0;
04833 p->subs[index].f.mallocd = 0;
04834 p->subs[index].f.offset = 0;
04835 p->subs[index].f.data = NULL;
04836 p->subs[index].f.datalen= 0;
04837 }
04838 if (p->dsp && (!p->ignoredtmf || p->callwaitcas || p->busydetect || p->callprogress) && !index) {
04839
04840 f = ast_dsp_process(ast, p->dsp, &p->subs[index].f);
04841 if (f) {
04842 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
04843 if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
04844
04845
04846 f = NULL;
04847 }
04848 } else if (f->frametype == AST_FRAME_DTMF) {
04849 #ifdef HAVE_PRI
04850 if (!p->proceeding && p->sig==SIG_PRI && p->pri && p->pri->overlapdial) {
04851
04852 f->frametype = AST_FRAME_NULL;
04853 f->subclass = 0;
04854 }
04855 #endif
04856
04857 p->pulsedial = 0;
04858 }
04859 }
04860 } else
04861 f = &p->subs[index].f;
04862
04863 if (f && (f->frametype == AST_FRAME_DTMF))
04864 zt_handle_dtmfup(ast, index, &f);
04865
04866
04867 if (p->fake_event)
04868 ast_set_flag(ast, AST_FLAG_EXCEPTION);
04869
04870 ast_mutex_unlock(&p->lock);
04871 return f;
04872 }
04873
04874 static int my_zt_write(struct zt_pvt *p, unsigned char *buf, int len, int index, int linear)
04875 {
04876 int sent=0;
04877 int size;
04878 int res;
04879 int fd;
04880 fd = p->subs[index].zfd;
04881 while (len) {
04882 size = len;
04883 if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
04884 size = (linear ? READ_SIZE * 2 : READ_SIZE);
04885 res = write(fd, buf, size);
04886 if (res != size) {
04887 if (option_debug)
04888 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
04889 return sent;
04890 }
04891 len -= size;
04892 buf += size;
04893 }
04894 return sent;
04895 }
04896
04897 static int zt_write(struct ast_channel *ast, struct ast_frame *frame)
04898 {
04899 struct zt_pvt *p = ast->tech_pvt;
04900 int res;
04901 unsigned char outbuf[4096];
04902 int index;
04903 index = zt_get_index(ast, p, 0);
04904 if (index < 0) {
04905 ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
04906 return -1;
04907 }
04908
04909 #if 0
04910 #ifdef HAVE_PRI
04911 ast_mutex_lock(&p->lock);
04912 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04913 if (p->pri->pri) {
04914 if (!pri_grab(p, p->pri)) {
04915 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04916 pri_rel(p->pri);
04917 } else
04918 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04919 }
04920 p->proceeding=1;
04921 }
04922 ast_mutex_unlock(&p->lock);
04923 #endif
04924 #endif
04925
04926 if (frame->frametype != AST_FRAME_VOICE) {
04927 if (frame->frametype != AST_FRAME_IMAGE)
04928 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
04929 return 0;
04930 }
04931 if ((frame->subclass != AST_FORMAT_SLINEAR) &&
04932 (frame->subclass != AST_FORMAT_ULAW) &&
04933 (frame->subclass != AST_FORMAT_ALAW)) {
04934 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
04935 return -1;
04936 }
04937 if (p->dialing) {
04938 if (option_debug)
04939 ast_log(LOG_DEBUG, "Dropping frame since I'm still dialing on %s...\n",ast->name);
04940 return 0;
04941 }
04942 if (!p->owner) {
04943 if (option_debug)
04944 ast_log(LOG_DEBUG, "Dropping frame since there is no active owner on %s...\n",ast->name);
04945 return 0;
04946 }
04947 if (p->cidspill) {
04948 if (option_debug)
04949 ast_log(LOG_DEBUG, "Dropping frame since I've still got a callerid spill\n");
04950 return 0;
04951 }
04952
04953 if (!frame->data || !frame->datalen)
04954 return 0;
04955 if (frame->datalen > sizeof(outbuf) * 2) {
04956 ast_log(LOG_WARNING, "Frame too large\n");
04957 return 0;
04958 }
04959
04960 if (frame->subclass == AST_FORMAT_SLINEAR) {
04961 if (!p->subs[index].linear) {
04962 p->subs[index].linear = 1;
04963 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04964 if (res)
04965 ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
04966 }
04967 res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 1);
04968 } else {
04969
04970 if (p->subs[index].linear) {
04971 p->subs[index].linear = 0;
04972 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04973 if (res)
04974 ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
04975 }
04976 res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 0);
04977 }
04978 if (res < 0) {
04979 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
04980 return -1;
04981 }
04982 return 0;
04983 }
04984
04985 static int zt_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
04986 {
04987 struct zt_pvt *p = chan->tech_pvt;
04988 int res=-1;
04989 int index;
04990 int func = ZT_FLASH;
04991 ast_mutex_lock(&p->lock);
04992 index = zt_get_index(chan, p, 0);
04993 ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
04994 if (index == SUB_REAL) {
04995 switch (condition) {
04996 case AST_CONTROL_BUSY:
04997 #ifdef HAVE_PRI
04998 if (p->priindication_oob && p->sig == SIG_PRI) {
04999 chan->hangupcause = AST_CAUSE_USER_BUSY;
05000 chan->_softhangup |= AST_SOFTHANGUP_DEV;
05001 res = 0;
05002 } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05003 if (p->pri->pri) {
05004 if (!pri_grab(p, p->pri)) {
05005 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
05006 pri_rel(p->pri);
05007 }
05008 else
05009 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05010 }
05011 p->progress = 1;
05012 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
05013 } else
05014 #endif
05015 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
05016 break;
05017 case AST_CONTROL_RINGING:
05018 #ifdef HAVE_PRI
05019 if ((!p->alerting) && p->sig==SIG_PRI && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
05020 if (p->pri->pri) {
05021 if (!pri_grab(p, p->pri)) {
05022 pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
05023 pri_rel(p->pri);
05024 }
05025 else
05026 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05027 }
05028 p->alerting = 1;
05029 }
05030 #endif
05031 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_RINGTONE);
05032 if (chan->_state != AST_STATE_UP) {
05033 if ((chan->_state != AST_STATE_RING) ||
05034 ((p->sig != SIG_FXSKS) &&
05035 (p->sig != SIG_FXSLS) &&
05036 (p->sig != SIG_FXSGS)))
05037 ast_setstate(chan, AST_STATE_RINGING);
05038 }
05039 break;
05040 case AST_CONTROL_PROCEEDING:
05041 ast_log(LOG_DEBUG,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
05042 #ifdef HAVE_PRI
05043 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05044 if (p->pri->pri) {
05045 if (!pri_grab(p, p->pri)) {
05046 pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
05047 pri_rel(p->pri);
05048 }
05049 else
05050 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05051 }
05052 p->proceeding = 1;
05053 }
05054 #endif
05055
05056 res = 0;
05057 break;
05058 case AST_CONTROL_PROGRESS:
05059 ast_log(LOG_DEBUG,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
05060 #ifdef HAVE_PRI
05061 p->digital = 0;
05062 if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05063 if (p->pri->pri) {
05064 if (!pri_grab(p, p->pri)) {
05065 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
05066 pri_rel(p->pri);
05067 }
05068 else
05069 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05070 }
05071 p->progress = 1;
05072 }
05073 #endif
05074
05075 res = 0;
05076 break;
05077 case AST_CONTROL_CONGESTION:
05078 chan->hangupcause = AST_CAUSE_CONGESTION;
05079 #ifdef HAVE_PRI
05080 if (p->priindication_oob && p->sig == SIG_PRI) {
05081 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
05082 chan->_softhangup |= AST_SOFTHANGUP_DEV;
05083 res = 0;
05084 } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
05085 if (p->pri) {
05086 if (!pri_grab(p, p->pri)) {
05087 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
05088 pri_rel(p->pri);
05089 } else
05090 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05091 }
05092 p->progress = 1;
05093 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05094 } else
05095 #endif
05096 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05097 break;
05098 case AST_CONTROL_HOLD:
05099 #ifdef HAVE_PRI
05100 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
05101 if (!pri_grab(p, p->pri)) {
05102 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
05103 pri_rel(p->pri);
05104 } else
05105 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05106 } else
05107 #endif
05108 ast_moh_start(chan, data, p->mohinterpret);
05109 break;
05110 case AST_CONTROL_UNHOLD:
05111 #ifdef HAVE_PRI
05112 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
05113 if (!pri_grab(p, p->pri)) {
05114 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
05115 pri_rel(p->pri);
05116 } else
05117 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
05118 } else
05119 #endif
05120 ast_moh_stop(chan);
05121 break;
05122 case AST_CONTROL_RADIO_KEY:
05123 if (p->radio)
05124 res = zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
05125 res = 0;
05126 break;
05127 case AST_CONTROL_RADIO_UNKEY:
05128 if (p->radio)
05129 res = zt_set_hook(p->subs[index].zfd, ZT_RINGOFF);
05130 res = 0;
05131 break;
05132 case AST_CONTROL_FLASH:
05133
05134 if (ISTRUNK(p) && (p->sig != SIG_PRI)) {
05135
05136 p->dop.dialstr[0] = '\0';
05137 if ((ioctl(p->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
05138 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
05139 chan->name, strerror(errno));
05140 } else
05141 res = 0;
05142 } else
05143 res = 0;
05144 break;
05145 case -1:
05146 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05147 break;
05148 }
05149 } else
05150 res = 0;
05151 ast_mutex_unlock(&p->lock);
05152 return res;
05153 }
05154
05155 static struct ast_channel *zt_new(struct zt_pvt *i, int state, int startpbx, int index, int law, int transfercapability)
05156 {
05157 struct ast_channel *tmp;
05158 int deflaw;
05159 int res;
05160 int x,y;
05161 int features;
05162 char *b2 = 0;
05163 ZT_PARAMS ps;
05164 if (i->subs[index].owner) {
05165 ast_log(LOG_WARNING, "Channel %d already has a %s call\n", i->channel,subnames[index]);
05166 return NULL;
05167 }
05168 y = 1;
05169 do {
05170 if (b2)
05171 free(b2);
05172 #ifdef HAVE_PRI
05173 if (i->bearer || (i->pri && (i->sig == SIG_FXSKS)))
05174 asprintf(&b2, "Zap/%d:%d-%d", i->pri->trunkgroup, i->channel, y);
05175 else
05176 #endif
05177 if (i->channel == CHAN_PSEUDO)
05178 asprintf(&b2, "Zap/pseudo-%ld", ast_random());
05179 else
05180 asprintf(&b2, "Zap/%d-%d", i->channel, y);
05181 for (x = 0; x < 3; x++) {
05182 if ((index != x) && i->subs[x].owner && !strcasecmp(b2, i->subs[x].owner->name))
05183 break;
05184 }
05185 y++;
05186 } while (x < 3);
05187 tmp = ast_channel_alloc(0, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, i->amaflags, b2);
05188 if (b2)
05189 free(b2);
05190 if (!tmp)
05191 return NULL;
05192 tmp->tech = &zap_tech;
05193 ps.channo = i->channel;
05194 res = ioctl(i->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps);
05195 if (res) {
05196 ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW\n");
05197 ps.curlaw = ZT_LAW_MULAW;
05198 }
05199 if (ps.curlaw == ZT_LAW_ALAW)
05200 deflaw = AST_FORMAT_ALAW;
05201 else
05202 deflaw = AST_FORMAT_ULAW;
05203 if (law) {
05204 if (law == ZT_LAW_ALAW)
05205 deflaw = AST_FORMAT_ALAW;
05206 else
05207 deflaw = AST_FORMAT_ULAW;
05208 }
05209 tmp->fds[0] = i->subs[index].zfd;
05210 tmp->nativeformats = AST_FORMAT_SLINEAR | deflaw;
05211
05212 tmp->rawreadformat = deflaw;
05213 tmp->readformat = deflaw;
05214 tmp->rawwriteformat = deflaw;
05215 tmp->writeformat = deflaw;
05216 i->subs[index].linear = 0;
05217 zt_setlinear(i->subs[index].zfd, i->subs[index].linear);
05218 features = 0;
05219 if (index == SUB_REAL) {
05220 if (i->busydetect && CANBUSYDETECT(i))
05221 features |= DSP_FEATURE_BUSY_DETECT;
05222 if ((i->callprogress & 1) && CANPROGRESSDETECT(i))
05223 features |= DSP_FEATURE_CALL_PROGRESS;
05224 if ((!i->outgoing && (i->callprogress & 4)) ||
05225 (i->outgoing && (i->callprogress & 2))) {
05226 features |= DSP_FEATURE_FAX_DETECT;
05227 }
05228 #ifdef ZT_TONEDETECT
05229 x = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
05230 if (ioctl(i->subs[index].zfd, ZT_TONEDETECT, &x)) {
05231 #endif
05232 i->hardwaredtmf = 0;
05233 features |= DSP_FEATURE_DTMF_DETECT;
05234 #ifdef ZT_TONEDETECT
05235 } else if (NEED_MFDETECT(i)) {
05236 i->hardwaredtmf = 1;
05237 features |= DSP_FEATURE_DTMF_DETECT;
05238 }
05239 #endif
05240 }
05241 if (features) {
05242 if (i->dsp) {
05243 ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", tmp->name);
05244 } else {
05245 if (i->channel != CHAN_PSEUDO)
05246 i->dsp = ast_dsp_new();
05247 else
05248 i->dsp = NULL;
05249 if (i->dsp) {
05250 i->dsp_features = features & ~DSP_PROGRESS_TALK;
05251 #ifdef HAVE_PRI
05252
05253 if (i->outgoing && (i->sig == SIG_PRI)) {
05254
05255
05256 features = 0;
05257 }
05258 #endif
05259 ast_dsp_set_features(i->dsp, features);
05260 ast_dsp_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax);
05261 if (!ast_strlen_zero(progzone))
05262 ast_dsp_set_call_progress_zone(i->dsp, progzone);
05263 if (i->busydetect && CANBUSYDETECT(i)) {
05264 ast_dsp_set_busy_count(i->dsp, i->busycount);
05265 ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength);
05266 }
05267 }
05268 }
05269 }
05270
05271 if (state == AST_STATE_RING)
05272 tmp->rings = 1;
05273 tmp->tech_pvt = i;
05274 if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) {
05275
05276 tmp->callgroup = i->callgroup;
05277 tmp->pickupgroup = i->pickupgroup;
05278 }
05279 if (!ast_strlen_zero(i->language))
05280 ast_string_field_set(tmp, language, i->language);
05281 if (!i->owner)
05282 i->owner = tmp;
05283 if (!ast_strlen_zero(i->accountcode))
05284 ast_string_field_set(tmp, accountcode, i->accountcode);
05285 if (i->amaflags)
05286 tmp->amaflags = i->amaflags;
05287 i->subs[index].owner = tmp;
05288 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
05289 ast_string_field_set(tmp, call_forward, i->call_forward);
05290
05291 if (!i->adsi)
05292 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
05293 if (!ast_strlen_zero(i->exten))
05294 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
05295 if (!ast_strlen_zero(i->rdnis))
05296 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
05297 if (!ast_strlen_zero(i->dnid))
05298 tmp->cid.cid_dnid = ast_strdup(i->dnid);
05299
05300
05301
05302 #ifdef PRI_ANI
05303 tmp->cid.cid_num = ast_strdup(i->cid_num);
05304 tmp->cid.cid_name = ast_strdup(i->cid_name);
05305 if (!ast_strlen_zero(i->cid_ani))
05306 tmp->cid.cid_ani = ast_strdup(i->cid_ani);
05307 else
05308 tmp->cid.cid_ani = ast_strdup(i->cid_num);
05309 #else
05310 tmp->cid.cid_num = ast_strdup(i->cid_num);
05311 tmp->cid.cid_ani = ast_strdup(i->cid_num);
05312 tmp->cid.cid_name = ast_strdup(i->cid_name);
05313 #endif
05314 tmp->cid.cid_pres = i->callingpres;
05315 tmp->cid.cid_ton = i->cid_ton;
05316 #ifdef HAVE_PRI
05317 tmp->transfercapability = transfercapability;
05318 pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
05319 if (transfercapability & PRI_TRANS_CAP_DIGITAL)
05320 i->digital = 1;
05321
05322 i->isidlecall = 0;
05323 i->alreadyhungup = 0;
05324 #endif
05325
05326 i->fake_event = 0;
05327
05328 zt_confmute(i, 0);
05329
05330 ast_jb_configure(tmp, &global_jbconf);
05331 if (startpbx) {
05332 if (ast_pbx_start(tmp)) {
05333 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
05334 ast_hangup(tmp);
05335 i->owner = NULL;
05336 return NULL;
05337 }
05338 }
05339
05340 ast_module_ref(ast_module_info->self);
05341
05342 return tmp;
05343 }
05344
05345
05346 static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
05347 {
05348 char c;
05349
05350 *str = 0;
05351 for (;;)
05352 {
05353
05354 c = ast_waitfordigit(chan, ms);
05355
05356 if (c < 1)
05357 return c;
05358 *str++ = c;
05359 *str = 0;
05360 if (strchr(term, c))
05361 return 1;
05362 }
05363 }
05364
05365 static int zt_wink(struct zt_pvt *p, int index)
05366 {
05367 int j;
05368 zt_set_hook(p->subs[index].zfd, ZT_WINK);
05369 for (;;)
05370 {
05371
05372 j = ZT_IOMUX_SIGEVENT;
05373
05374 if (ioctl(p->subs[index].zfd,ZT_IOMUX,&j) == -1) return(-1);
05375
05376 if (j & ZT_IOMUX_SIGEVENT) break;
05377 }
05378
05379 if (ioctl(p->subs[index].zfd,ZT_GETEVENT,&j) == -1) return(-1);
05380 return 0;
05381 }
05382
05383 static void *ss_thread(void *data)
05384 {
05385 struct ast_channel *chan = data;
05386 struct zt_pvt *p = chan->tech_pvt;
05387 char exten[AST_MAX_EXTENSION] = "";
05388 char exten2[AST_MAX_EXTENSION] = "";
05389 unsigned char buf[256];
05390 char dtmfcid[300];
05391 char dtmfbuf[300];
05392 struct callerid_state *cs = NULL;
05393 char *name = NULL, *number = NULL;
05394 int distMatches;
05395 int curRingData[3];
05396 int receivedRingT;
05397 int counter1;
05398 int counter;
05399 int samples = 0;
05400 struct ast_smdi_md_message *smdi_msg = NULL;
05401 int flags;
05402 int i;
05403 int timeout;
05404 int getforward = 0;
05405 char *s1, *s2;
05406 int len = 0;
05407 int res;
05408 int index;
05409
05410
05411
05412
05413 if (!p) {
05414 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
05415 ast_hangup(chan);
05416 return NULL;
05417 }
05418
05419 if (option_verbose > 2)
05420 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name);
05421 index = zt_get_index(chan, p, 1);
05422 if (index < 0) {
05423 ast_log(LOG_WARNING, "Huh?\n");
05424 ast_hangup(chan);
05425 return NULL;
05426 }
05427 if (p->dsp)
05428 ast_dsp_digitreset(p->dsp);
05429 switch (p->sig) {
05430 #ifdef HAVE_PRI
05431 case SIG_PRI:
05432
05433 ast_copy_string(exten, p->exten, sizeof(exten));
05434 len = strlen(exten);
05435 res = 0;
05436 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05437 if (len && !ast_ignore_pattern(chan->context, exten))
05438 tone_zone_play_tone(p->subs[index].zfd, -1);
05439 else
05440 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05441 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
05442 timeout = matchdigittimeout;
05443 else
05444 timeout = gendigittimeout;
05445 res = ast_waitfordigit(chan, timeout);
05446 if (res < 0) {
05447 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05448 ast_hangup(chan);
05449 return NULL;
05450 } else if (res) {
05451 exten[len++] = res;
05452 exten[len] = '\0';
05453 } else
05454 break;
05455 }
05456
05457 if (ast_strlen_zero(exten)) {
05458 if (option_verbose > 2)
05459 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n");
05460 exten[0] = 's';
05461 exten[1] = '\0';
05462 }
05463 tone_zone_play_tone(p->subs[index].zfd, -1);
05464 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
05465
05466 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05467 if (p->dsp) ast_dsp_digitreset(p->dsp);
05468 zt_enable_ec(p);
05469 ast_setstate(chan, AST_STATE_RING);
05470 res = ast_pbx_run(chan);
05471 if (res) {
05472 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
05473 }
05474 } else {
05475 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
05476 chan->hangupcause = AST_CAUSE_UNALLOCATED;
05477 ast_hangup(chan);
05478 p->exten[0] = '\0';
05479
05480 p->call = NULL;
05481 }
05482 return NULL;
05483 break;
05484 #endif
05485 case SIG_FEATD:
05486 case SIG_FEATDMF:
05487 case SIG_FEATDMF_TA:
05488 case SIG_E911:
05489 case SIG_FGC_CAMAMF:
05490 case SIG_FEATB:
05491 case SIG_EMWINK:
05492 case SIG_SF_FEATD:
05493 case SIG_SF_FEATDMF:
05494 case SIG_SF_FEATB:
05495 case SIG_SFWINK:
05496 if (zt_wink(p, index))
05497 return NULL;
05498
05499 case SIG_EM:
05500 case SIG_EM_E1:
05501 case SIG_SF:
05502 case SIG_FGC_CAMA:
05503 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05504 if (p->dsp)
05505 ast_dsp_digitreset(p->dsp);
05506
05507 if (p->dsp) {
05508 if (NEED_MFDETECT(p))
05509 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax);
05510 else
05511 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05512 }
05513 memset(dtmfbuf, 0, sizeof(dtmfbuf));
05514
05515 if (!p->immediate)
05516
05517 res = ast_waitfordigit(chan, 5000);
05518 else
05519 res = 0;
05520 if (res > 0) {
05521
05522 dtmfbuf[0] = res;
05523 switch (p->sig) {
05524 case SIG_FEATD:
05525 case SIG_SF_FEATD:
05526 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05527 if (res > 0)
05528 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05529 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05530 break;
05531 case SIG_FEATDMF_TA:
05532 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05533 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05534 if (zt_wink(p, index)) return NULL;
05535 dtmfbuf[0] = 0;
05536
05537 res = ast_waitfordigit(chan, 5000);
05538 if (res <= 0) break;
05539 dtmfbuf[0] = res;
05540
05541 case SIG_FEATDMF:
05542 case SIG_E911:
05543 case SIG_FGC_CAMAMF:
05544 case SIG_SF_FEATDMF:
05545 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05546
05547 if ((p->sig == SIG_FEATDMF) && (dtmfbuf[1] != '0') && (strlen(dtmfbuf) != 14))
05548 {
05549 if (zt_wink(p, index)) return NULL;
05550 dtmfbuf[0] = 0;
05551
05552 res = ast_waitfordigit(chan, 5000);
05553 if (res <= 0) break;
05554 dtmfbuf[0] = res;
05555 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05556 }
05557 if (res > 0) {
05558
05559 if (p->sig == SIG_E911)
05560 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
05561 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
05562 }
05563 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05564 break;
05565 case SIG_FEATB:
05566 case SIG_SF_FEATB:
05567 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05568 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05569 break;
05570 case SIG_EMWINK:
05571
05572
05573
05574
05575 if (res == '*') {
05576 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05577 if (res > 0)
05578 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05579 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05580 break;
05581 }
05582 default:
05583
05584 len = 1;
05585 dtmfbuf[len] = '\0';
05586 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05587 if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05588 timeout = matchdigittimeout;
05589 } else {
05590 timeout = gendigittimeout;
05591 }
05592 res = ast_waitfordigit(chan, timeout);
05593 if (res < 0) {
05594 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05595 ast_hangup(chan);
05596 return NULL;
05597 } else if (res) {
05598 dtmfbuf[len++] = res;
05599 dtmfbuf[len] = '\0';
05600 } else {
05601 break;
05602 }
05603 }
05604 break;
05605 }
05606 }
05607 if (res == -1) {
05608 ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
05609 ast_hangup(chan);
05610 return NULL;
05611 } else if (res < 0) {
05612 ast_log(LOG_DEBUG, "Got hung up before digits finished\n");
05613 ast_hangup(chan);
05614 return NULL;
05615 }
05616
05617 if (p->sig == SIG_FGC_CAMA) {
05618 char anibuf[100];
05619
05620 if (ast_safe_sleep(chan,1000) == -1) {
05621 ast_hangup(chan);
05622 return NULL;
05623 }
05624 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
05625 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax);
05626 res = my_getsigstr(chan, anibuf, "#", 10000);
05627 if ((res > 0) && (strlen(anibuf) > 2)) {
05628 if (anibuf[strlen(anibuf) - 1] == '#')
05629 anibuf[strlen(anibuf) - 1] = 0;
05630 ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
05631 }
05632 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05633 }
05634
05635 ast_copy_string(exten, dtmfbuf, sizeof(exten));
05636 if (ast_strlen_zero(exten))
05637 ast_copy_string(exten, "s", sizeof(exten));
05638 if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
05639
05640 if (exten[0] == '*') {
05641 char *stringp=NULL;
05642 ast_copy_string(exten2, exten, sizeof(exten2));
05643
05644 stringp=exten2 +1;
05645 s1 = strsep(&stringp, "*");
05646 s2 = strsep(&stringp, "*");
05647 if (s2) {
05648 if (!ast_strlen_zero(p->cid_num))
05649 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05650 else
05651 ast_set_callerid(chan, s1, NULL, s1);
05652 ast_copy_string(exten, s2, sizeof(exten));
05653 } else
05654 ast_copy_string(exten, s1, sizeof(exten));
05655 } else if (p->sig == SIG_FEATD)
05656 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05657 }
05658 if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) {
05659 if (exten[0] == '*') {
05660 char *stringp=NULL;
05661 ast_copy_string(exten2, exten, sizeof(exten2));
05662
05663 stringp=exten2 +1;
05664 s1 = strsep(&stringp, "#");
05665 s2 = strsep(&stringp, "#");
05666 if (s2) {
05667 if (!ast_strlen_zero(p->cid_num))
05668 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05669 else
05670 if (*(s1 + 2))
05671 ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
05672 ast_copy_string(exten, s2 + 1, sizeof(exten));
05673 } else
05674 ast_copy_string(exten, s1 + 2, sizeof(exten));
05675 } else
05676 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05677 }
05678 if ((p->sig == SIG_E911) || (p->sig == SIG_FGC_CAMAMF)) {
05679 if (exten[0] == '*') {
05680 char *stringp=NULL;
05681 ast_copy_string(exten2, exten, sizeof(exten2));
05682
05683 stringp=exten2 +1;
05684 s1 = strsep(&stringp, "#");
05685 s2 = strsep(&stringp, "#");
05686 if (s2 && (*(s2 + 1) == '0')) {
05687 if (*(s2 + 2))
05688 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
05689 }
05690 if (s1) ast_copy_string(exten, s1, sizeof(exten));
05691 else ast_copy_string(exten, "911", sizeof(exten));
05692 } else
05693 ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d. Assuming E&M Wink instead\n", p->channel);
05694 }
05695 if (p->sig == SIG_FEATB) {
05696 if (exten[0] == '*') {
05697 char *stringp=NULL;
05698 ast_copy_string(exten2, exten, sizeof(exten2));
05699
05700 stringp=exten2 +1;
05701 s1 = strsep(&stringp, "#");
05702 ast_copy_string(exten, exten2 + 1, sizeof(exten));
05703 } else
05704 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
05705 }
05706 if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) {
05707 zt_wink(p, index);
05708
05709
05710
05711 if (ast_safe_sleep(chan,100)) return NULL;
05712 }
05713 zt_enable_ec(p);
05714 if (NEED_MFDETECT(p)) {
05715 if (p->dsp) {
05716 if (!p->hardwaredtmf)
05717 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05718 else {
05719 ast_dsp_free(p->dsp);
05720 p->dsp = NULL;
05721 }
05722 }
05723 }
05724
05725 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
05726 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05727 if (p->dsp) ast_dsp_digitreset(p->dsp);
05728 res = ast_pbx_run(chan);
05729 if (res) {
05730 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05731 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05732 }
05733 return NULL;
05734 } else {
05735 if (option_verbose > 2)
05736 ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
05737 sleep(2);
05738 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_INFO);
05739 if (res < 0)
05740 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
05741 else
05742 sleep(1);
05743 res = ast_streamfile(chan, "ss-noservice", chan->language);
05744 if (res >= 0)
05745 ast_waitstream(chan, "");
05746 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05747 ast_hangup(chan);
05748 return NULL;
05749 }
05750 break;
05751 case SIG_FXOLS:
05752 case SIG_FXOGS:
05753 case SIG_FXOKS:
05754
05755 timeout = firstdigittimeout;
05756
05757
05758 if (p->subs[SUB_THREEWAY].owner)
05759 timeout = 999999;
05760 while (len < AST_MAX_EXTENSION-1) {
05761
05762
05763 if (p->immediate)
05764 res = 's';
05765 else
05766 res = ast_waitfordigit(chan, timeout);
05767 timeout = 0;
05768 if (res < 0) {
05769 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05770 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05771 ast_hangup(chan);
05772 return NULL;
05773 } else if (res) {
05774 exten[len++]=res;
05775 exten[len] = '\0';
05776 }
05777 if (!ast_ignore_pattern(chan->context, exten))
05778 tone_zone_play_tone(p->subs[index].zfd, -1);
05779 else
05780 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05781 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
05782 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05783 if (getforward) {
05784
05785 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
05786 if (option_verbose > 2)
05787 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
05788 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05789 if (res)
05790 break;
05791 usleep(500000);
05792 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05793 sleep(1);
05794 memset(exten, 0, sizeof(exten));
05795 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05796 len = 0;
05797 getforward = 0;
05798 } else {
05799 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05800 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05801 if (!ast_strlen_zero(p->cid_num)) {
05802 if (!p->hidecallerid)
05803 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05804 else
05805 ast_set_callerid(chan, NULL, NULL, p->cid_num);
05806 }
05807 if (!ast_strlen_zero(p->cid_name)) {
05808 if (!p->hidecallerid)
05809 ast_set_callerid(chan, NULL, p->cid_name, NULL);
05810 }
05811 ast_setstate(chan, AST_STATE_RING);
05812 zt_enable_ec(p);
05813 res = ast_pbx_run(chan);
05814 if (res) {
05815 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05816 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05817 }
05818 return NULL;
05819 }
05820 } else {
05821
05822
05823 timeout = matchdigittimeout;
05824 }
05825 } else if (res == 0) {
05826 ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n");
05827 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05828 zt_wait_event(p->subs[index].zfd);
05829 ast_hangup(chan);
05830 return NULL;
05831 } else if (p->callwaiting && !strcmp(exten, "*70")) {
05832 if (option_verbose > 2)
05833 ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
05834
05835 p->callwaiting = 0;
05836 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05837 if (res) {
05838 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05839 chan->name, strerror(errno));
05840 }
05841 len = 0;
05842 ioctl(p->subs[index].zfd,ZT_CONFDIAG,&len);
05843 memset(exten, 0, sizeof(exten));
05844 timeout = firstdigittimeout;
05845
05846 } else if (!strcmp(exten,ast_pickup_ext())) {
05847
05848
05849
05850
05851 if (index == SUB_REAL) {
05852
05853 if (p->subs[SUB_THREEWAY].owner) {
05854
05855
05856 alloc_sub(p, SUB_CALLWAIT);
05857 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
05858 unalloc_sub(p, SUB_THREEWAY);
05859 }
05860 zt_enable_ec(p);
05861 if (ast_pickup_call(chan)) {
05862 ast_log(LOG_DEBUG, "No call pickup possible...\n");
05863 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05864 zt_wait_event(p->subs[index].zfd);
05865 }
05866 ast_hangup(chan);
05867 return NULL;
05868 } else {
05869 ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
05870 ast_hangup(chan);
05871 return NULL;
05872 }
05873
05874 } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
05875 if (option_verbose > 2)
05876 ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
05877
05878 p->hidecallerid = 1;
05879 if (chan->cid.cid_num)
05880 free(chan->cid.cid_num);
05881 chan->cid.cid_num = NULL;
05882 if (chan->cid.cid_name)
05883 free(chan->cid.cid_name);
05884 chan->cid.cid_name = NULL;
05885 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05886 if (res) {
05887 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05888 chan->name, strerror(errno));
05889 }
05890 len = 0;
05891 memset(exten, 0, sizeof(exten));
05892 timeout = firstdigittimeout;
05893 } else if (p->callreturn && !strcmp(exten, "*69")) {
05894 res = 0;
05895 if (!ast_strlen_zero(p->lastcid_num)) {
05896 res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
05897 }
05898 if (!res)
05899 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05900 break;
05901 } else if (!strcmp(exten, "*78")) {
05902
05903 if (option_verbose > 2)
05904 ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel);
05905 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05906 "Channel: Zap/%d\r\n"
05907 "Status: enabled\r\n", p->channel);
05908 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05909 p->dnd = 1;
05910 getforward = 0;
05911 memset(exten, 0, sizeof(exten));
05912 len = 0;
05913 } else if (!strcmp(exten, "*79")) {
05914
05915 if (option_verbose > 2)
05916 ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel);
05917 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05918 "Channel: Zap/%d\r\n"
05919 "Status: disabled\r\n", p->channel);
05920 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05921 p->dnd = 0;
05922 getforward = 0;
05923 memset(exten, 0, sizeof(exten));
05924 len = 0;
05925 } else if (p->cancallforward && !strcmp(exten, "*72")) {
05926 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05927 getforward = 1;
05928 memset(exten, 0, sizeof(exten));
05929 len = 0;
05930 } else if (p->cancallforward && !strcmp(exten, "*73")) {
05931 if (option_verbose > 2)
05932 ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel);
05933 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05934 memset(p->call_forward, 0, sizeof(p->call_forward));
05935 getforward = 0;
05936 memset(exten, 0, sizeof(exten));
05937 len = 0;
05938 } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
05939 p->subs[SUB_THREEWAY].owner &&
05940 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
05941
05942
05943 ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL);
05944 if (option_verbose > 2)
05945 ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
05946 break;
05947 } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
05948 if (option_verbose > 2)
05949 ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num);
05950 res = ast_db_put("blacklist", p->lastcid_num, "1");
05951 if (!res) {
05952 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05953 memset(exten, 0, sizeof(exten));
05954 len = 0;
05955 }
05956 } else if (p->hidecallerid && !strcmp(exten, "*82")) {
05957 if (option_verbose > 2)
05958 ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
05959
05960 p->hidecallerid = 0;
05961 if (chan->cid.cid_num)
05962 free(chan->cid.cid_num);
05963 chan->cid.cid_num = NULL;
05964 if (chan->cid.cid_name)
05965 free(chan->cid.cid_name);
05966 chan->cid.cid_name = NULL;
05967 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
05968 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05969 if (res) {
05970 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05971 chan->name, strerror(errno));
05972 }
05973 len = 0;
05974 memset(exten, 0, sizeof(exten));
05975 timeout = firstdigittimeout;
05976 } else if (!strcmp(exten, "*0")) {
05977 struct ast_channel *nbridge =
05978 p->subs[SUB_THREEWAY].owner;
05979 struct zt_pvt *pbridge = NULL;
05980
05981 if (nbridge && ast_bridged_channel(nbridge))
05982 pbridge = ast_bridged_channel(nbridge)->tech_pvt;
05983 if (nbridge && pbridge &&
05984 (nbridge->tech == &zap_tech) &&
05985 (ast_bridged_channel(nbridge)->tech == &zap_tech) &&
05986 ISTRUNK(pbridge)) {
05987 int func = ZT_FLASH;
05988
05989 p->dop.dialstr[0] = '\0';
05990
05991 if ((ioctl(pbridge->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
05992 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
05993 nbridge->name, strerror(errno));
05994 }
05995 swap_subs(p, SUB_REAL, SUB_THREEWAY);
05996 unalloc_sub(p, SUB_THREEWAY);
05997 p->owner = p->subs[SUB_REAL].owner;
05998 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
05999 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
06000 ast_hangup(chan);
06001 return NULL;
06002 } else {
06003 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06004 zt_wait_event(p->subs[index].zfd);
06005 tone_zone_play_tone(p->subs[index].zfd, -1);
06006 swap_subs(p, SUB_REAL, SUB_THREEWAY);
06007 unalloc_sub(p, SUB_THREEWAY);
06008 p->owner = p->subs[SUB_REAL].owner;
06009 ast_hangup(chan);
06010 return NULL;
06011 }
06012 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
06013 ((exten[0] != '*') || (strlen(exten) > 2))) {
06014 if (option_debug)
06015 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);
06016 break;
06017 }
06018 if (!timeout)
06019 timeout = gendigittimeout;
06020 if (len && !ast_ignore_pattern(chan->context, exten))
06021 tone_zone_play_tone(p->subs[index].zfd, -1);
06022 }
06023 break;
06024 case SIG_FXSLS:
06025 case SIG_FXSGS:
06026 case SIG_FXSKS:
06027 #ifdef HAVE_PRI
06028 if (p->pri) {
06029
06030 struct ast_frame *f;
06031 int res;
06032 time_t start;
06033
06034 time(&start);
06035 ast_setstate(chan, AST_STATE_RING);
06036 while (time(NULL) < start + 3) {
06037 res = ast_waitfor(chan, 1000);
06038 if (res) {
06039 f = ast_read(chan);
06040 if (!f) {
06041 ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
06042 ast_hangup(chan);
06043 return NULL;
06044 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
06045 res = 1;
06046 } else
06047 res = 0;
06048 ast_frfree(f);
06049 if (res) {
06050 ast_log(LOG_DEBUG, "Got ring!\n");
06051 res = 0;
06052 break;
06053 }
06054 }
06055 }
06056 }
06057 #endif
06058
06059 if (p->use_smdi && p->smdi_iface) {
06060 smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, SMDI_MD_WAIT_TIMEOUT);
06061
06062 if (smdi_msg != NULL) {
06063 ast_copy_string(chan->exten, smdi_msg->fwd_st, sizeof(chan->exten));
06064
06065 if (smdi_msg->type == 'B')
06066 pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
06067 else if (smdi_msg->type == 'N')
06068 pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
06069
06070 ast_log(LOG_DEBUG, "Recieved SMDI message on %s\n", chan->name);
06071 } else {
06072 ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
06073 }
06074 }
06075
06076 if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
06077 number = smdi_msg->calling_st;
06078
06079
06080
06081
06082 } else if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) {
06083
06084 if (p->cid_signalling == CID_SIG_DTMF) {
06085 int i = 0;
06086 cs = NULL;
06087 ast_log(LOG_DEBUG, "Receiving DTMF cid on "
06088 "channel %s\n", chan->name);
06089 zt_setlinear(p->subs[index].zfd, 0);
06090 res = 2000;
06091 for (;;) {
06092 struct ast_frame *f;
06093 res = ast_waitfor(chan, res);
06094 if (res <= 0) {
06095 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
06096 "Exiting simple switch\n");
06097 ast_hangup(chan);
06098 return NULL;
06099 }
06100 f = ast_read(chan);
06101 if (f->frametype == AST_FRAME_DTMF) {
06102 dtmfbuf[i++] = f->subclass;
06103 ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
06104 res = 2000;
06105 }
06106 ast_frfree(f);
06107 if (chan->_state == AST_STATE_RING ||
06108 chan->_state == AST_STATE_RINGING)
06109 break;
06110 }
06111 dtmfbuf[i] = '\0';
06112 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06113
06114 ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf);
06115 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
06116 ast_log(LOG_DEBUG, "CID is '%s', flags %d\n",
06117 dtmfcid, flags);
06118
06119 if (!ast_strlen_zero(dtmfcid))
06120 number = dtmfcid;
06121 else
06122 number = NULL;
06123
06124 } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
06125 cs = callerid_new(p->cid_signalling);
06126 if (cs) {
06127 samples = 0;
06128 #if 1
06129 bump_gains(p);
06130 #endif
06131
06132 zt_setlinear(p->subs[index].zfd, 0);
06133
06134
06135 for (;;) {
06136 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06137 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
06138 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06139 callerid_free(cs);
06140 ast_hangup(chan);
06141 return NULL;
06142 }
06143 if (i & ZT_IOMUX_SIGEVENT) {
06144 res = zt_get_event(p->subs[index].zfd);
06145 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06146
06147 if (p->cid_signalling == CID_SIG_V23_JP) {
06148 #ifdef ZT_EVENT_RINGBEGIN
06149 if (res == ZT_EVENT_RINGBEGIN) {
06150 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06151 usleep(1);
06152 }
06153 #endif
06154 } else {
06155 res = 0;
06156 break;
06157 }
06158 } else if (i & ZT_IOMUX_READ) {
06159 res = read(p->subs[index].zfd, buf, sizeof(buf));
06160 if (res < 0) {
06161 if (errno != ELAST) {
06162 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06163 callerid_free(cs);
06164 ast_hangup(chan);
06165 return NULL;
06166 }
06167 break;
06168 }
06169 samples += res;
06170
06171 if (p->cid_signalling == CID_SIG_V23_JP) {
06172 res = callerid_feed_jp(cs, buf, res, AST_LAW(p));
06173 } else {
06174 res = callerid_feed(cs, buf, res, AST_LAW(p));
06175 }
06176
06177 if (res < 0) {
06178 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06179 break;
06180 } else if (res)
06181 break;
06182 else if (samples > (8000 * 10))
06183 break;
06184 }
06185 }
06186 if (res == 1) {
06187 callerid_get(cs, &name, &number, &flags);
06188 ast_log(LOG_NOTICE, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06189 }
06190 if (res < 0) {
06191 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06192 }
06193
06194 if (p->cid_signalling == CID_SIG_V23_JP) {
06195 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
06196 usleep(1);
06197 res = 4000;
06198 } else {
06199
06200
06201 res = 2000;
06202 }
06203
06204 for (;;) {
06205 struct ast_frame *f;
06206 res = ast_waitfor(chan, res);
06207 if (res <= 0) {
06208 ast_log(LOG_WARNING, "CID timed out waiting for ring. "
06209 "Exiting simple switch\n");
06210 ast_hangup(chan);
06211 return NULL;
06212 }
06213 f = ast_read(chan);
06214 ast_frfree(f);
06215 if (chan->_state == AST_STATE_RING ||
06216 chan->_state == AST_STATE_RINGING)
06217 break;
06218 }
06219
06220
06221
06222 if (p->usedistinctiveringdetection == 1) {
06223 len = 0;
06224 distMatches = 0;
06225
06226 for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
06227 curRingData[receivedRingT] = 0;
06228 receivedRingT = 0;
06229 counter = 0;
06230 counter1 = 0;
06231
06232 if (strcmp(p->context,p->defcontext) != 0) {
06233 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06234 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06235 }
06236
06237 for (;;) {
06238 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06239 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
06240 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06241 callerid_free(cs);
06242 ast_hangup(chan);
06243 return NULL;
06244 }
06245 if (i & ZT_IOMUX_SIGEVENT) {
06246 res = zt_get_event(p->subs[index].zfd);
06247 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06248 res = 0;
06249
06250
06251 curRingData[receivedRingT] = p->ringt;
06252
06253 if (p->ringt < p->ringt_base/2)
06254 break;
06255
06256
06257 if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06258 break;
06259 } else if (i & ZT_IOMUX_READ) {
06260 res = read(p->subs[index].zfd, buf, sizeof(buf));
06261 if (res < 0) {
06262 if (errno != ELAST) {
06263 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06264 callerid_free(cs);
06265 ast_hangup(chan);
06266 return NULL;
06267 }
06268 break;
06269 }
06270 if (p->ringt)
06271 p->ringt--;
06272 if (p->ringt == 1) {
06273 res = -1;
06274 break;
06275 }
06276 }
06277 }
06278 if (option_verbose > 2)
06279
06280 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06281
06282 for (counter = 0; counter < 3; counter++) {
06283
06284
06285 distMatches = 0;
06286 for (counter1 = 0; counter1 < 3; counter1++) {
06287 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06288 (p->drings.ringnum[counter].ring[counter1]-10)) {
06289 distMatches++;
06290 }
06291 }
06292 if (distMatches == 3) {
06293
06294 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06295 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06296 if (option_verbose > 2)
06297 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06298 break;
06299 }
06300 }
06301 }
06302
06303 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06304 #if 1
06305 restore_gains(p);
06306 #endif
06307 } else
06308 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06309 } else {
06310 ast_log(LOG_WARNING, "Channel %s in prering "
06311 "state, but I have nothing to do. "
06312 "Terminating simple switch, should be "
06313 "restarted by the actual ring.\n",
06314 chan->name);
06315 ast_hangup(chan);
06316 return NULL;
06317 }
06318 } else if (p->use_callerid && p->cid_start == CID_START_RING) {
06319
06320 cs = callerid_new(p->cid_signalling);
06321 if (cs) {
06322 #if 1
06323 bump_gains(p);
06324 #endif
06325 samples = 0;
06326 len = 0;
06327 distMatches = 0;
06328
06329 for (receivedRingT = 0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
06330 curRingData[receivedRingT] = 0;
06331 receivedRingT = 0;
06332 counter = 0;
06333 counter1 = 0;
06334
06335 if (strcmp(p->context,p->defcontext) != 0) {
06336 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06337 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06338 }
06339
06340
06341 zt_setlinear(p->subs[index].zfd, 0);
06342 for (;;) {
06343 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06344 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
06345 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06346 callerid_free(cs);
06347 ast_hangup(chan);
06348 return NULL;
06349 }
06350 if (i & ZT_IOMUX_SIGEVENT) {
06351 res = zt_get_event(p->subs[index].zfd);
06352 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06353 res = 0;
06354
06355
06356 curRingData[receivedRingT] = p->ringt;
06357
06358 if (p->ringt < p->ringt_base/2)
06359 break;
06360
06361
06362 if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06363 break;
06364 } else if (i & ZT_IOMUX_READ) {
06365 res = read(p->subs[index].zfd, buf, sizeof(buf));
06366 if (res < 0) {
06367 if (errno != ELAST) {
06368 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06369 callerid_free(cs);
06370 ast_hangup(chan);
06371 return NULL;
06372 }
06373 break;
06374 }
06375 if (p->ringt)
06376 p->ringt--;
06377 if (p->ringt == 1) {
06378 res = -1;
06379 break;
06380 }
06381 samples += res;
06382 res = callerid_feed(cs, buf, res, AST_LAW(p));
06383 if (res < 0) {
06384 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06385 break;
06386 } else if (res)
06387 break;
06388 else if (samples > (8000 * 10))
06389 break;
06390 }
06391 }
06392 if (res == 1) {
06393 callerid_get(cs, &name, &number, &flags);
06394 if (option_debug)
06395 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06396 }
06397 if (distinctiveringaftercid == 1) {
06398
06399 for (receivedRingT = 0; receivedRingT < 3; receivedRingT++) {
06400 curRingData[receivedRingT] = 0;
06401 }
06402 receivedRingT = 0;
06403 if (option_verbose > 2)
06404 ast_verbose( VERBOSE_PREFIX_3 "Detecting post-CID distinctive ring\n");
06405 for (;;) {
06406 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06407 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
06408 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06409 callerid_free(cs);
06410 ast_hangup(chan);
06411 return NULL;
06412 }
06413 if (i & ZT_IOMUX_SIGEVENT) {
06414 res = zt_get_event(p->subs[index].zfd);
06415 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06416 res = 0;
06417
06418
06419 curRingData[receivedRingT] = p->ringt;
06420
06421 if (p->ringt < p->ringt_base/2)
06422 break;
06423
06424
06425 if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06426 break;
06427 } else if (i & ZT_IOMUX_READ) {
06428 res = read(p->subs[index].zfd, buf, sizeof(buf));
06429 if (res < 0) {
06430 if (errno != ELAST) {
06431 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06432 callerid_free(cs);
06433 ast_hangup(chan);
06434 return NULL;
06435 }
06436 break;
06437 }
06438 if (p->ringt)
06439 p->ringt--;
06440 if (p->ringt == 1) {
06441 res = -1;
06442 break;
06443 }
06444 }
06445 }
06446 }
06447 if (p->usedistinctiveringdetection == 1) {
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 if (option_verbose > 2)
06456
06457 ast_verbose( VERBOSE_PREFIX_3 "Checking %d,%d,%d\n",
06458 p->drings.ringnum[counter].ring[0],
06459 p->drings.ringnum[counter].ring[1],
06460 p->drings.ringnum[counter].ring[2]);
06461 distMatches = 0;
06462 for (counter1 = 0; counter1 < 3; counter1++) {
06463 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06464 (p->drings.ringnum[counter].ring[counter1]-10)) {
06465 distMatches++;
06466 }
06467 }
06468 if (distMatches == 3) {
06469
06470 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06471 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06472 if (option_verbose > 2)
06473 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06474 break;
06475 }
06476 }
06477 }
06478
06479 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06480 #if 1
06481 restore_gains(p);
06482 #endif
06483 if (res < 0) {
06484 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06485 }
06486 } else
06487 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06488 }
06489 else
06490 cs = NULL;
06491
06492 if (number)
06493 ast_shrink_phone_number(number);
06494 ast_set_callerid(chan, number, name, number);
06495
06496 if (smdi_msg)
06497 ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy);
06498
06499 if (cs)
06500 callerid_free(cs);
06501
06502 ast_setstate(chan, AST_STATE_RING);
06503 chan->rings = 1;
06504 p->ringt = p->ringt_base;
06505 res = ast_pbx_run(chan);
06506 if (res) {
06507 ast_hangup(chan);
06508 ast_log(LOG_WARNING, "PBX exited non-zero\n");
06509 }
06510 return NULL;
06511 default:
06512 ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
06513 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06514 if (res < 0)
06515 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06516 }
06517 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06518 if (res < 0)
06519 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06520 ast_hangup(chan);
06521 return NULL;
06522 }
06523
06524
06525 static int zap_destroy_channel_bynum(int channel)
06526 {
06527 struct zt_pvt *tmp = NULL;
06528 struct zt_pvt *prev = NULL;
06529
06530 tmp = iflist;
06531 while (tmp) {
06532 if (tmp->channel == channel) {
06533 destroy_channel(prev, tmp, 1);
06534 return RESULT_SUCCESS;
06535 }
06536 prev = tmp;
06537 tmp = tmp->next;
06538 }
06539 return RESULT_FAILURE;
06540 }
06541
06542 static int handle_init_event(struct zt_pvt *i, int event)
06543 {
06544 int res;
06545 pthread_t threadid;
06546 pthread_attr_t attr;
06547 struct ast_channel *chan;
06548 pthread_attr_init(&attr);
06549 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06550
06551 switch (event) {
06552 case ZT_EVENT_NONE:
06553 case ZT_EVENT_BITSCHANGED:
06554 break;
06555 case ZT_EVENT_WINKFLASH:
06556 case ZT_EVENT_RINGOFFHOOK:
06557 if (i->inalarm) break;
06558 if (i->radio) break;
06559
06560 switch (i->sig) {
06561 case SIG_FXOLS:
06562 case SIG_FXOGS:
06563 case SIG_FXOKS:
06564 res = zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06565 if (res && (errno == EBUSY))
06566 break;
06567 if (i->cidspill) {
06568
06569 free(i->cidspill);
06570 i->cidspill = NULL;
06571 }
06572 if (i->immediate) {
06573 zt_enable_ec(i);
06574
06575 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
06576 chan = zt_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
06577 if (!chan) {
06578 ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
06579 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06580 if (res < 0)
06581 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06582 }
06583 } else {
06584
06585 chan = zt_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
06586 if (chan) {
06587 if (has_voicemail(i))
06588 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
06589 else
06590 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
06591 if (res < 0)
06592 ast_log(LOG_WARNING, "Unable to play dialtone on channel %d\n", i->channel);
06593 if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06594 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06595 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06596 if (res < 0)
06597 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06598 ast_hangup(chan);
06599 }
06600 } else
06601 ast_log(LOG_WARNING, "Unable to create channel\n");
06602 }
06603 break;
06604 case SIG_FXSLS:
06605 case SIG_FXSGS:
06606 case SIG_FXSKS:
06607 i->ringt = i->ringt_base;
06608
06609 case SIG_EMWINK:
06610 case SIG_FEATD:
06611 case SIG_FEATDMF:
06612 case SIG_FEATDMF_TA:
06613 case SIG_E911:
06614 case SIG_FGC_CAMA:
06615 case SIG_FGC_CAMAMF:
06616 case SIG_FEATB:
06617 case SIG_EM:
06618 case SIG_EM_E1:
06619 case SIG_SFWINK:
06620 case SIG_SF_FEATD:
06621 case SIG_SF_FEATDMF:
06622 case SIG_SF_FEATB:
06623 case SIG_SF:
06624
06625 chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06626 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06627 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06628 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06629 if (res < 0)
06630 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06631 ast_hangup(chan);
06632 } else if (!chan) {
06633 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
06634 }
06635 break;
06636 default:
06637 ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06638 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06639 if (res < 0)
06640 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06641 return -1;
06642 }
06643 break;
06644 case ZT_EVENT_NOALARM:
06645 i->inalarm = 0;
06646 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
06647 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
06648 "Channel: %d\r\n", i->channel);
06649 break;
06650 case ZT_EVENT_ALARM:
06651 i->inalarm = 1;
06652 res = get_alarms(i);
06653 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", i->channel, alarm2str(res));
06654 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
06655 "Alarm: %s\r\n"
06656 "Channel: %d\r\n",
06657 alarm2str(res), i->channel);
06658
06659 case ZT_EVENT_ONHOOK:
06660 if (i->radio)
06661 break;
06662
06663 switch (i->sig) {
06664 case SIG_FXOLS:
06665 case SIG_FXOGS:
06666 case SIG_FEATD:
06667 case SIG_FEATDMF:
06668 case SIG_FEATDMF_TA:
06669 case SIG_E911:
06670 case SIG_FGC_CAMA:
06671 case SIG_FGC_CAMAMF:
06672 case SIG_FEATB:
06673 case SIG_EM:
06674 case SIG_EM_E1:
06675 case SIG_EMWINK:
06676 case SIG_SF_FEATD:
06677 case SIG_SF_FEATDMF:
06678 case SIG_SF_FEATB:
06679 case SIG_SF:
06680 case SIG_SFWINK:
06681 case SIG_FXSLS:
06682 case SIG_FXSGS:
06683 case SIG_FXSKS:
06684 case SIG_GR303FXSKS:
06685 zt_disable_ec(i);
06686 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06687 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06688 break;
06689 case SIG_GR303FXOKS:
06690 case SIG_FXOKS:
06691 zt_disable_ec(i);
06692
06693 #ifdef ZHONE_HACK
06694 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06695 usleep(1);
06696 #endif
06697 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06698 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06699 break;
06700 case SIG_PRI:
06701 zt_disable_ec(i);
06702 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06703 break;
06704 default:
06705 ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06706 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06707 return -1;
06708 }
06709 break;
06710 case ZT_EVENT_POLARITY:
06711 switch (i->sig) {
06712 case SIG_FXSLS:
06713 case SIG_FXSKS:
06714 case SIG_FXSGS:
06715 if (i->cid_start == CID_START_POLARITY) {
06716 i->polarity = POLARITY_REV;
06717 ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
06718 "CID detection on channel %d\n",
06719 i->channel);
06720 chan = zt_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
06721 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06722 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06723 }
06724 }
06725 break;
06726 default:
06727 ast_log(LOG_WARNING, "handle_init_event detected "
06728 "polarity reversal on non-FXO (SIG_FXS) "
06729 "interface %d\n", i->channel);
06730 }
06731 break;
06732 case ZT_EVENT_REMOVED:
06733 ast_log(LOG_NOTICE,
06734 "Got ZT_EVENT_REMOVED. Destroying channel %d\n",
06735 i->channel);
06736 zap_destroy_channel_bynum(i->channel);
06737 break;
06738 }
06739 pthread_attr_destroy(&attr);
06740 return 0;
06741 }
06742
06743 static void *do_monitor(void *data)
06744 {
06745 int count, res, res2, spoint, pollres=0;
06746 struct zt_pvt *i;
06747 struct zt_pvt *last = NULL;
06748 time_t thispass = 0, lastpass = 0;
06749 int found;
06750 char buf[1024];
06751 struct pollfd *pfds=NULL;
06752 int lastalloc = -1;
06753
06754
06755
06756 #if 0
06757 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
06758 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
06759 return NULL;
06760 }
06761 ast_log(LOG_DEBUG, "Monitor starting...\n");
06762 #endif
06763 for (;;) {
06764
06765 ast_mutex_lock(&iflock);
06766 if (!pfds || (lastalloc != ifcount)) {
06767 if (pfds)
06768 free(pfds);
06769 if (ifcount) {
06770 if (!(pfds = ast_calloc(1, ifcount * sizeof(*pfds)))) {
06771 ast_mutex_unlock(&iflock);
06772 return NULL;
06773 }
06774 }
06775 lastalloc = ifcount;
06776 }
06777
06778
06779 count = 0;
06780 i = iflist;
06781 while (i) {
06782 if ((i->subs[SUB_REAL].zfd > -1) && i->sig && (!i->radio)) {
06783 if (!i->owner && !i->subs[SUB_REAL].owner) {
06784
06785 pfds[count].fd = i->subs[SUB_REAL].zfd;
06786 pfds[count].events = POLLPRI;
06787 pfds[count].revents = 0;
06788
06789 if (i->cidspill)
06790 pfds[count].events |= POLLIN;
06791 count++;
06792 }
06793 }
06794 i = i->next;
06795 }
06796
06797 ast_mutex_unlock(&iflock);
06798
06799 pthread_testcancel();
06800
06801 res = poll(pfds, count, 1000);
06802 pthread_testcancel();
06803
06804 if (res < 0) {
06805 if ((errno != EAGAIN) && (errno != EINTR))
06806 ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
06807 continue;
06808 }
06809
06810
06811 ast_mutex_lock(&iflock);
06812 found = 0;
06813 spoint = 0;
06814 lastpass = thispass;
06815 thispass = time(NULL);
06816 i = iflist;
06817 while (i) {
06818 if (thispass != lastpass) {
06819 if (!found && ((i == last) || ((i == iflist) && !last))) {
06820 last = i;
06821 if (last) {
06822 if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) &&
06823 (last->sig & __ZT_SIG_FXO)) {
06824 res = ast_app_has_voicemail(last->mailbox, NULL);
06825 if (last->msgstate != res) {
06826 int x;
06827 ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel);
06828 x = ZT_FLUSH_BOTH;
06829 res2 = ioctl(last->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
06830 if (res2)
06831 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", last->channel);
06832 if ((last->cidspill = ast_calloc(1, MAX_CALLERID_SIZE))) {
06833
06834 x = 4000;
06835 ioctl(last->subs[SUB_REAL].zfd, ZT_ONHOOKTRANSFER, &x);
06836 last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last));
06837 last->cidpos = 0;
06838 last->msgstate = res;
06839 last->onhooktime = thispass;
06840 }
06841 found ++;
06842 }
06843 }
06844 last = last->next;
06845 }
06846 }
06847 }
06848 if ((i->subs[SUB_REAL].zfd > -1) && i->sig) {
06849 if (i->radio && !i->owner)
06850 {
06851 res = zt_get_event(i->subs[SUB_REAL].zfd);
06852 if (res)
06853 {
06854 if (option_debug)
06855 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
06856
06857 ast_mutex_unlock(&iflock);
06858 handle_init_event(i, res);
06859 ast_mutex_lock(&iflock);
06860 }
06861 i = i->next;
06862 continue;
06863 }
06864 pollres = ast_fdisset(pfds, i->subs[SUB_REAL].zfd, count, &spoint);
06865 if (pollres & POLLIN) {
06866 if (i->owner || i->subs[SUB_REAL].owner) {
06867 #ifdef HAVE_PRI
06868 if (!i->pri)
06869 #endif
06870 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].zfd);
06871 i = i->next;
06872 continue;
06873 }
06874 if (!i->cidspill) {
06875 ast_log(LOG_WARNING, "Whoa.... I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].zfd);
06876 i = i->next;
06877 continue;
06878 }
06879 res = read(i->subs[SUB_REAL].zfd, buf, sizeof(buf));
06880 if (res > 0) {
06881
06882 if (res > i->cidlen - i->cidpos)
06883 res = i->cidlen - i->cidpos;
06884 res2 = write(i->subs[SUB_REAL].zfd, i->cidspill + i->cidpos, res);
06885 if (res2 > 0) {
06886 i->cidpos += res2;
06887 if (i->cidpos >= i->cidlen) {
06888 free(i->cidspill);
06889 i->cidspill = 0;
06890 i->cidpos = 0;
06891 i->cidlen = 0;
06892 }
06893 } else {
06894 ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
06895 i->msgstate = -1;
06896 }
06897 } else {
06898 ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
06899 }
06900 if (option_debug)
06901 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06902
06903 ast_mutex_unlock(&iflock);
06904 handle_init_event(i, res);
06905 ast_mutex_lock(&iflock);
06906 }
06907 if (pollres & POLLPRI) {
06908 if (i->owner || i->subs[SUB_REAL].owner) {
06909 #ifdef HAVE_PRI
06910 if (!i->pri)
06911 #endif
06912 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].zfd);
06913 i = i->next;
06914 continue;
06915 }
06916 res = zt_get_event(i->subs[SUB_REAL].zfd);
06917 if (option_debug)
06918 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06919
06920 ast_mutex_unlock(&iflock);
06921 handle_init_event(i, res);
06922 ast_mutex_lock(&iflock);
06923 }
06924 }
06925 i=i->next;
06926 }
06927 ast_mutex_unlock(&iflock);
06928 }
06929
06930 return NULL;
06931
06932 }
06933
06934 static int restart_monitor(void)
06935 {
06936 pthread_attr_t attr;
06937 pthread_attr_init(&attr);
06938 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06939
06940 if (monitor_thread == AST_PTHREADT_STOP)
06941 return 0;
06942 ast_mutex_lock(&monlock);
06943 if (monitor_thread == pthread_self()) {
06944 ast_mutex_unlock(&monlock);
06945 ast_log(LOG_WARNING, "Cannot kill myself\n");
06946 return -1;
06947 }
06948 if (monitor_thread != AST_PTHREADT_NULL) {
06949
06950 pthread_kill(monitor_thread, SIGURG);
06951 } else {
06952
06953 if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) {
06954 ast_mutex_unlock(&monlock);
06955 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
06956 pthread_attr_destroy(&attr);
06957 return -1;
06958 }
06959 }
06960 ast_mutex_unlock(&monlock);
06961 pthread_attr_destroy(&attr);
06962 return 0;
06963 }
06964
06965 #ifdef HAVE_PRI
06966 static int pri_resolve_span(int *span, int channel, int offset, struct zt_spaninfo *si)
06967 {
06968 int x;
06969 int trunkgroup;
06970
06971 trunkgroup = pris[*span].mastertrunkgroup;
06972 if (trunkgroup) {
06973
06974 for (x = 0; x < NUM_SPANS; x++) {
06975 if (pris[x].trunkgroup == trunkgroup) {
06976 *span = x;
06977 return 0;
06978 }
06979 }
06980 ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup);
06981 *span = -1;
06982 } else {
06983 if (pris[*span].trunkgroup) {
06984 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup);
06985 *span = -1;
06986 } else if (pris[*span].mastertrunkgroup) {
06987 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup);
06988 *span = -1;
06989 } else {
06990 if (si->totalchans == 31) {
06991 pris[*span].dchannels[0] = 16 + offset;
06992 } else {
06993 pris[*span].dchannels[0] = 24 + offset;
06994 }
06995 pris[*span].dchanavail[0] |= DCHAN_PROVISIONED;
06996 pris[*span].offset = offset;
06997 pris[*span].span = *span + 1;
06998 }
06999 }
07000 return 0;
07001 }
07002
07003 static int pri_create_trunkgroup(int trunkgroup, int *channels)
07004 {
07005 struct zt_spaninfo si;
07006 ZT_PARAMS p;
07007 int fd;
07008 int span;
07009 int ospan=0;
07010 int x,y;
07011 for (x = 0; x < NUM_SPANS; x++) {
07012 if (pris[x].trunkgroup == trunkgroup) {
07013 ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]);
07014 return -1;
07015 }
07016 }
07017 for (y = 0; y < NUM_DCHANS; y++) {
07018 if (!channels[y])
07019 break;
07020 memset(&si, 0, sizeof(si));
07021 memset(&p, 0, sizeof(p));
07022 fd = open("/dev/zap/channel", O_RDWR);
07023 if (fd < 0) {
07024 ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno));
07025 return -1;
07026 }
07027 x = channels[y];
07028 if (ioctl(fd, ZT_SPECIFY, &x)) {
07029 ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
07030 zt_close(fd);
07031 return -1;
07032 }
07033 if (ioctl(fd, ZT_GET_PARAMS, &p)) {
07034 ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno));
07035 return -1;
07036 }
07037 if (ioctl(fd, ZT_SPANSTAT, &si)) {
07038 ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d)\n", channels[y], p.spanno);
07039 zt_close(fd);
07040 return -1;
07041 }
07042 span = p.spanno - 1;
07043 if (pris[span].trunkgroup) {
07044 ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
07045 zt_close(fd);
07046 return -1;
07047 }
07048 if (pris[span].pvts[0]) {
07049 ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
07050 zt_close(fd);
07051 return -1;
07052 }
07053 if (!y) {
07054 pris[span].trunkgroup = trunkgroup;
07055 pris[span].offset = channels[y] - p.chanpos;
07056 ospan = span;
07057 }
07058 pris[ospan].dchannels[y] = channels[y];
07059 pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
07060 pris[span].span = span + 1;
07061 zt_close(fd);
07062 }
07063 return 0;
07064 }
07065
07066 static int pri_create_spanmap(int span, int trunkgroup, int logicalspan)
07067 {
07068 if (pris[span].mastertrunkgroup) {
07069 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);
07070 return -1;
07071 }
07072 pris[span].mastertrunkgroup = trunkgroup;
07073 pris[span].prilogicalspan = logicalspan;
07074 return 0;
07075 }
07076
07077 #endif
07078
07079 static struct zt_pvt *mkintf(int channel, struct zt_chan_conf conf, struct zt_pri *pri, int reloading)
07080 {
07081
07082 struct zt_pvt *tmp = NULL, *tmp2, *prev = NULL;
07083 char fn[80];
07084 #if 1
07085 struct zt_bufferinfo bi;
07086 #endif
07087 struct zt_spaninfo si;
07088 int res;
07089 int span=0;
07090 int here = 0;
07091 int x;
07092 struct zt_pvt **wlist;
07093 struct zt_pvt **wend;
07094 ZT_PARAMS p;
07095
07096 wlist = &iflist;
07097 wend = &ifend;
07098
07099 #ifdef HAVE_PRI
07100 if (pri) {
07101 wlist = &pri->crvs;
07102 wend = &pri->crvend;
07103 }
07104 #endif
07105
07106 tmp2 = *wlist;
07107 prev = NULL;
07108
07109 while (tmp2) {
07110 if (!tmp2->destroy) {
07111 if (tmp2->channel == channel) {
07112 tmp = tmp2;
07113 here = 1;
07114 break;
07115 }
07116 if (tmp2->channel > channel) {
07117 break;
07118 }
07119 }
07120 prev = tmp2;
07121 tmp2 = tmp2->next;
07122 }
07123
07124 if (!here && !reloading) {
07125 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
07126 destroy_zt_pvt(&tmp);
07127 return NULL;
07128 }
07129 ast_mutex_init(&tmp->lock);
07130 ifcount++;
07131 for (x = 0; x < 3; x++)
07132 tmp->subs[x].zfd = -1;
07133 tmp->channel = channel;
07134 }
07135
07136 if (tmp) {
07137 if (!here) {
07138 if ((channel != CHAN_PSEUDO) && !pri) {
07139 snprintf(fn, sizeof(fn), "%d", channel);
07140
07141 if (!here)
07142 tmp->subs[SUB_REAL].zfd = zt_open(fn);
07143
07144 if (tmp->subs[SUB_REAL].zfd < 0) {
07145 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);
07146 destroy_zt_pvt(&tmp);
07147 return NULL;
07148 }
07149 memset(&p, 0, sizeof(p));
07150 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
07151 if (res < 0) {
07152 ast_log(LOG_ERROR, "Unable to get parameters\n");
07153 destroy_zt_pvt(&tmp);
07154 return NULL;
07155 }
07156 if (p.sigtype != (conf.chan.sig & 0x3ffff)) {
07157 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));
07158 destroy_zt_pvt(&tmp);
07159 return NULL;
07160 }
07161 tmp->law = p.curlaw;
07162 tmp->span = p.spanno;
07163 span = p.spanno - 1;
07164 } else {
07165 if (channel == CHAN_PSEUDO)
07166 conf.chan.sig = 0;
07167 else if ((conf.chan.sig != SIG_FXOKS) && (conf.chan.sig != SIG_FXSKS)) {
07168 ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n");
07169 return NULL;
07170 }
07171 }
07172 #ifdef HAVE_PRI
07173 if ((conf.chan.sig == SIG_PRI) || (conf.chan.sig == SIG_GR303FXOKS) || (conf.chan.sig == SIG_GR303FXSKS)) {
07174 int offset;
07175 int myswitchtype;
07176 int matchesdchan;
07177 int x,y;
07178 offset = 0;
07179 if ((conf.chan.sig == SIG_PRI) && ioctl(tmp->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &offset)) {
07180 ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
07181 destroy_zt_pvt(&tmp);
07182 return NULL;
07183 }
07184 if (span >= NUM_SPANS) {
07185 ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span);
07186 destroy_zt_pvt(&tmp);
07187 return NULL;
07188 } else {
07189 si.spanno = 0;
07190 if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
07191 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
07192 destroy_zt_pvt(&tmp);
07193 return NULL;
07194 }
07195
07196 tmp->logicalspan = pris[span].prilogicalspan;
07197 pri_resolve_span(&span, channel, (channel - p.chanpos), &si);
07198 if (span < 0) {
07199 ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel);
07200 destroy_zt_pvt(&tmp);
07201 return NULL;
07202 }
07203 if (conf.chan.sig == SIG_PRI)
07204 myswitchtype = conf.pri.switchtype;
07205 else
07206 myswitchtype = PRI_SWITCH_GR303_TMC;
07207
07208 matchesdchan=0;
07209 for (x = 0; x < NUM_SPANS; x++) {
07210 for (y = 0; y < NUM_DCHANS; y++) {
07211 if (pris[x].dchannels[y] == tmp->channel) {
07212 matchesdchan = 1;
07213 break;
07214 }
07215 }
07216 }
07217 offset = p.chanpos;
07218 if (!matchesdchan) {
07219 if (pris[span].nodetype && (pris[span].nodetype != conf.pri.nodetype)) {
07220 ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype));
07221 destroy_zt_pvt(&tmp);
07222 return NULL;
07223 }
07224 if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) {
07225 ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype));
07226 destroy_zt_pvt(&tmp);
07227 return NULL;
07228 }
07229 if ((pris[span].dialplan) && (pris[span].dialplan != conf.pri.dialplan)) {
07230 ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan));
07231 destroy_zt_pvt(&tmp);
07232 return NULL;
07233 }
07234 if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, conf.pri.idledial)) {
07235 ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, conf.pri.idledial);
07236 destroy_zt_pvt(&tmp);
07237 return NULL;
07238 }
07239 if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, conf.pri.idleext)) {
07240 ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, conf.pri.idleext);
07241 destroy_zt_pvt(&tmp);
07242 return NULL;
07243 }
07244 if (pris[span].minunused && (pris[span].minunused != conf.pri.minunused)) {
07245 ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, conf.pri.minunused);
07246 destroy_zt_pvt(&tmp);
07247 return NULL;
07248 }
07249 if (pris[span].minidle && (pris[span].minidle != conf.pri.minidle)) {
07250 ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, conf.pri.minidle);
07251 destroy_zt_pvt(&tmp);
07252 return NULL;
07253 }
07254 if (pris[span].numchans >= MAX_CHANNELS) {
07255 ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel,
07256 pris[span].trunkgroup);
07257 destroy_zt_pvt(&tmp);
07258 return NULL;
07259 }
07260 pris[span].nodetype = conf.pri.nodetype;
07261 pris[span].switchtype = myswitchtype;
07262 pris[span].nsf = conf.pri.nsf;
07263 pris[span].dialplan = conf.pri.dialplan;
07264 pris[span].localdialplan = conf.pri.localdialplan;
07265 pris[span].pvts[pris[span].numchans++] = tmp;
07266 pris[span].minunused = conf.pri.minunused;
07267 pris[span].minidle = conf.pri.minidle;
07268 pris[span].overlapdial = conf.pri.overlapdial;
07269 pris[span].facilityenable = conf.pri.facilityenable;
07270 ast_copy_string(pris[span].idledial, conf.pri.idledial, sizeof(pris[span].idledial));
07271 ast_copy_string(pris[span].idleext, conf.pri.idleext, sizeof(pris[span].idleext));
07272 ast_copy_string(pris[span].internationalprefix, conf.pri.internationalprefix, sizeof(pris[span].internationalprefix));
07273 ast_copy_string(pris[span].nationalprefix, conf.pri.nationalprefix, sizeof(pris[span].nationalprefix));
07274 ast_copy_string(pris[span].localprefix, conf.pri.localprefix, sizeof(pris[span].localprefix));
07275 ast_copy_string(pris[span].privateprefix, conf.pri.privateprefix, sizeof(pris[span].privateprefix));
07276 ast_copy_string(pris[span].unknownprefix, conf.pri.unknownprefix, sizeof(pris[span].unknownprefix));
07277 pris[span].resetinterval = conf.pri.resetinterval;
07278
07279 tmp->pri = &pris[span];
07280 tmp->prioffset = offset;
07281 tmp->call = NULL;
07282 } else {
07283 ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset);
07284 destroy_zt_pvt(&tmp);
07285 return NULL;
07286 }
07287 }
07288 } else {
07289 tmp->prioffset = 0;
07290 }
07291 #endif
07292 } else {
07293 conf.chan.sig = tmp->sig;
07294 conf.chan.radio = tmp->radio;
07295 memset(&p, 0, sizeof(p));
07296 if (tmp->subs[SUB_REAL].zfd > -1)
07297 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
07298 }
07299
07300 if ((conf.chan.sig == SIG_FXSKS) || (conf.chan.sig == SIG_FXSLS) ||
07301 (conf.chan.sig == SIG_EM) || (conf.chan.sig == SIG_EM_E1) || (conf.chan.sig == SIG_EMWINK) ||
07302 (conf.chan.sig == SIG_FEATD) || (conf.chan.sig == SIG_FEATDMF) || (conf.chan.sig == SIG_FEATDMF_TA) ||
07303 (conf.chan.sig == SIG_FEATB) || (conf.chan.sig == SIG_E911) ||
07304 (conf.chan.sig == SIG_SF) || (conf.chan.sig == SIG_SFWINK) || (conf.chan.sig == SIG_FGC_CAMA) || (conf.chan.sig == SIG_FGC_CAMAMF) ||
07305 (conf.chan.sig == SIG_SF_FEATD) || (conf.chan.sig == SIG_SF_FEATDMF) ||
07306 (conf.chan.sig == SIG_SF_FEATB)) {
07307 p.starttime = 250;
07308 }
07309 if (conf.chan.radio) {
07310
07311 p.channo = channel;
07312 p.rxwinktime = 1;
07313 p.rxflashtime = 1;
07314 p.starttime = 1;
07315 p.debouncetime = 5;
07316 }
07317 if (!conf.chan.radio) {
07318 p.channo = channel;
07319
07320 if (conf.timing.prewinktime >= 0)
07321 p.prewinktime = conf.timing.prewinktime;
07322 if (conf.timing.preflashtime >= 0)
07323 p.preflashtime = conf.timing.preflashtime;
07324 if (conf.timing.winktime >= 0)
07325 p.winktime = conf.timing.winktime;
07326 if (conf.timing.flashtime >= 0)
07327 p.flashtime = conf.timing.flashtime;
07328 if (conf.timing.starttime >= 0)
07329 p.starttime = conf.timing.starttime;
07330 if (conf.timing.rxwinktime >= 0)
07331 p.rxwinktime = conf.timing.rxwinktime;
07332 if (conf.timing.rxflashtime >= 0)
07333 p.rxflashtime = conf.timing.rxflashtime;
07334 if (conf.timing.debouncetime >= 0)
07335 p.debouncetime = conf.timing.debouncetime;
07336 }
07337
07338
07339 if (tmp->subs[SUB_REAL].zfd >= 0)
07340 {
07341 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_PARAMS, &p);
07342 if (res < 0) {
07343 ast_log(LOG_ERROR, "Unable to set parameters\n");
07344 destroy_zt_pvt(&tmp);
07345 return NULL;
07346 }
07347 }
07348 #if 1
07349 if (!here && (tmp->subs[SUB_REAL].zfd > -1)) {
07350 memset(&bi, 0, sizeof(bi));
07351 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
07352 if (!res) {
07353 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
07354 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
07355 bi.numbufs = numbufs;
07356 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
07357 if (res < 0) {
07358 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", channel);
07359 }
07360 } else
07361 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", channel);
07362 }
07363 #endif
07364 tmp->immediate = conf.chan.immediate;
07365 tmp->transfertobusy = conf.chan.transfertobusy;
07366 tmp->sig = conf.chan.sig;
07367 tmp->outsigmod = conf.chan.outsigmod;
07368 tmp->radio = conf.chan.radio;
07369 tmp->ringt_base = ringt_base;
07370 tmp->firstradio = 0;
07371 if ((conf.chan.sig == SIG_FXOKS) || (conf.chan.sig == SIG_FXOLS) || (conf.chan.sig == SIG_FXOGS))
07372 tmp->permcallwaiting = conf.chan.callwaiting;
07373 else
07374 tmp->permcallwaiting = 0;
07375
07376 tmp->destroy = 0;
07377 tmp->drings = drings;
07378 tmp->usedistinctiveringdetection = usedistinctiveringdetection;
07379 tmp->callwaitingcallerid = conf.chan.callwaitingcallerid;
07380 tmp->threewaycalling = conf.chan.threewaycalling;
07381 tmp->adsi = conf.chan.adsi;
07382 tmp->use_smdi = conf.chan.use_smdi;
07383 tmp->permhidecallerid = conf.chan.hidecallerid;
07384 tmp->callreturn = conf.chan.callreturn;
07385 tmp->echocancel = conf.chan.echocancel;
07386 tmp->echotraining = conf.chan.echotraining;
07387 tmp->pulse = conf.chan.pulse;
07388 tmp->echocanbridged = conf.chan.echocanbridged;
07389 tmp->busydetect = conf.chan.busydetect;
07390 tmp->busycount = conf.chan.busycount;
07391 tmp->busy_tonelength = conf.chan.busy_tonelength;
07392 tmp->busy_quietlength = conf.chan.busy_quietlength;
07393 tmp->callprogress = conf.chan.callprogress;
07394 tmp->cancallforward = conf.chan.cancallforward;
07395 tmp->dtmfrelax = conf.chan.dtmfrelax;
07396 tmp->callwaiting = tmp->permcallwaiting;
07397 tmp->hidecallerid = tmp->permhidecallerid;
07398 tmp->channel = channel;
07399 tmp->stripmsd = conf.chan.stripmsd;
07400 tmp->use_callerid = conf.chan.use_callerid;
07401 tmp->cid_signalling = conf.chan.cid_signalling;
07402 tmp->cid_start = conf.chan.cid_start;
07403 tmp->zaptrcallerid = conf.chan.zaptrcallerid;
07404 tmp->restrictcid = conf.chan.restrictcid;
07405 tmp->use_callingpres = conf.chan.use_callingpres;
07406 tmp->priindication_oob = conf.chan.priindication_oob;
07407 tmp->priexclusive = conf.chan.priexclusive;
07408 if (tmp->usedistinctiveringdetection) {
07409 if (!tmp->use_callerid) {
07410 ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n");
07411 tmp->use_callerid = 1;
07412 }
07413 }
07414
07415 if (tmp->cid_signalling == CID_SIG_SMDI) {
07416 if (!tmp->use_smdi) {
07417 ast_log(LOG_WARNING, "SMDI callerid requires SMDI to be enabled, enabling...\n");
07418 tmp->use_smdi = 1;
07419 }
07420 }
07421 if (tmp->use_smdi) {
07422 tmp->smdi_iface = ast_smdi_interface_find(conf.smdi_port);
07423 if (!(tmp->smdi_iface)) {
07424 ast_log(LOG_ERROR, "Invalid SMDI port specfied, disabling SMDI support\n");
07425 tmp->use_smdi = 0;
07426 }
07427 }
07428
07429 ast_copy_string(tmp->accountcode, conf.chan.accountcode, sizeof(tmp->accountcode));
07430 tmp->amaflags = conf.chan.amaflags;
07431 if (!here) {
07432 tmp->confno = -1;
07433 tmp->propconfno = -1;
07434 }
07435 tmp->canpark = conf.chan.canpark;
07436 tmp->transfer = conf.chan.transfer;
07437 ast_copy_string(tmp->defcontext,conf.chan.context,sizeof(tmp->defcontext));
07438 ast_copy_string(tmp->language, conf.chan.language, sizeof(tmp->language));
07439 ast_copy_string(tmp->mohinterpret, conf.chan.mohinterpret, sizeof(tmp->mohinterpret));
07440 ast_copy_string(tmp->mohsuggest, conf.chan.mohsuggest, sizeof(tmp->mohsuggest));
07441 ast_copy_string(tmp->context, conf.chan.context, sizeof(tmp->context));
07442 ast_copy_string(tmp->cid_num, conf.chan.cid_num, sizeof(tmp->cid_num));
07443 tmp->cid_ton = 0;
07444 ast_copy_string(tmp->cid_name, conf.chan.cid_name, sizeof(tmp->cid_name));
07445 ast_copy_string(tmp->mailbox, conf.chan.mailbox, sizeof(tmp->mailbox));
07446 tmp->msgstate = -1;
07447 tmp->group = conf.chan.group;
07448 tmp->callgroup = conf.chan.callgroup;
07449 tmp->pickupgroup= conf.chan.pickupgroup;
07450 tmp->rxgain = conf.chan.rxgain;
07451 tmp->txgain = conf.chan.txgain;
07452 tmp->tonezone = conf.chan.tonezone;
07453 tmp->onhooktime = time(NULL);
07454 if (tmp->subs[SUB_REAL].zfd > -1) {
07455 set_actual_gain(tmp->subs[SUB_REAL].zfd, 0, tmp->rxgain, tmp->txgain, tmp->law);
07456 if (tmp->dsp)
07457 ast_dsp_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax);
07458 update_conf(tmp);
07459 if (!here) {
07460 if (conf.chan.sig != SIG_PRI)
07461
07462 zt_set_hook(tmp->subs[SUB_REAL].zfd, ZT_ONHOOK);
07463 }
07464 ioctl(tmp->subs[SUB_REAL].zfd,ZT_SETTONEZONE,&tmp->tonezone);
07465 #ifdef HAVE_PRI
07466
07467 if (tmp->pri && !pri_is_up(tmp->pri))
07468 tmp->inalarm = 1;
07469 else
07470 tmp->inalarm = 0;
07471 #endif
07472 memset(&si, 0, sizeof(si));
07473 if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
07474 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
07475 destroy_zt_pvt(&tmp);
07476 return NULL;
07477 }
07478 if (si.alarms) tmp->inalarm = 1;
07479 }
07480
07481 tmp->polarityonanswerdelay = conf.chan.polarityonanswerdelay;
07482 tmp->answeronpolarityswitch = conf.chan.answeronpolarityswitch;
07483 tmp->hanguponpolarityswitch = conf.chan.hanguponpolarityswitch;
07484 tmp->sendcalleridafter = conf.chan.sendcalleridafter;
07485
07486 }
07487 if (tmp && !here) {
07488
07489 if (!*wlist) {
07490 *wlist = tmp;
07491 tmp->prev = NULL;
07492 tmp->next = NULL;
07493 *wend = tmp;
07494 } else {
07495
07496 struct zt_pvt *working = *wlist;
07497
07498
07499 if (working->channel > tmp->channel) {
07500 tmp->next = *wlist;
07501 tmp->prev = NULL;
07502 (*wlist)->prev = tmp;
07503 *wlist = tmp;
07504 } else {
07505
07506 while (working) {
07507
07508 if (working->next) {
07509 if (working->channel < tmp->channel && working->next->channel > tmp->channel) {
07510 tmp->next = working->next;
07511 tmp->prev = working;
07512 working->next->prev = tmp;
07513 working->next = tmp;
07514 break;
07515 }
07516 } else {
07517
07518 if (working->channel < tmp->channel) {
07519 working->next = tmp;
07520 tmp->next = NULL;
07521 tmp->prev = working;
07522 *wend = tmp;
07523 break;
07524 }
07525 }
07526 working = working->next;
07527 }
07528 }
07529 }
07530 }
07531 return tmp;
07532 }
07533
07534 static inline int available(struct zt_pvt *p, int channelmatch, int groupmatch, int *busy, int *channelmatched, int *groupmatched)
07535 {
07536 int res;
07537 ZT_PARAMS par;
07538
07539
07540 if (groupmatch) {
07541 if ((p->group & groupmatch) != groupmatch)
07542 return 0;
07543 *groupmatched = 1;
07544 }
07545
07546 if (channelmatch != -1) {
07547 if (p->channel != channelmatch)
07548 return 0;
07549 *channelmatched = 1;
07550 }
07551
07552 if (busy) {
07553 if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS))
07554 *busy = 1;
07555 }
07556
07557 if (p->dnd)
07558 return 0;
07559
07560 if (p->guardtime && (time(NULL) < p->guardtime))
07561 return 0;
07562
07563
07564 if (!p->owner) {
07565 #ifdef HAVE_PRI
07566
07567 if (p->pri) {
07568 if (p->resetting || p->call)
07569 return 0;
07570 else
07571 return 1;
07572 }
07573 #endif
07574 if (!(p->radio || (p->oprmode < 0)))
07575 {
07576 if (!p->sig || (p->sig == SIG_FXSLS))
07577 return 1;
07578
07579 if (p->subs[SUB_REAL].zfd > -1)
07580 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
07581 else {
07582
07583 res = 0;
07584 par.rxisoffhook = 0;
07585 }
07586 if (res) {
07587 ast_log(LOG_WARNING, "Unable to check hook state on channel %d\n", p->channel);
07588 } else if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
07589
07590
07591
07592 if (par.rxbits > -1)
07593 return 1;
07594 if (par.rxisoffhook)
07595 return 1;
07596 else
07597 #ifdef ZAP_CHECK_HOOKSTATE
07598 return 0;
07599 #else
07600 return 1;
07601 #endif
07602 } else if (par.rxisoffhook) {
07603 ast_log(LOG_DEBUG, "Channel %d off hook, can't use\n", p->channel);
07604
07605 return 0;
07606 }
07607 }
07608 return 1;
07609 }
07610
07611
07612 if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS))
07613 return 0;
07614
07615 if (!p->callwaiting) {
07616
07617 return 0;
07618 }
07619
07620 if (p->subs[SUB_CALLWAIT].zfd > -1) {
07621
07622 return 0;
07623 }
07624
07625 if ((p->owner->_state != AST_STATE_UP) &&
07626 ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
07627
07628 return 0;
07629 }
07630 if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) {
07631
07632 return 0;
07633 }
07634
07635 return 1;
07636 }
07637
07638 static struct zt_pvt *chandup(struct zt_pvt *src)
07639 {
07640 struct zt_pvt *p;
07641 ZT_BUFFERINFO bi;
07642 int res;
07643
07644 if ((p = ast_malloc(sizeof(*p)))) {
07645 memcpy(p, src, sizeof(struct zt_pvt));
07646 ast_mutex_init(&p->lock);
07647 p->subs[SUB_REAL].zfd = zt_open("/dev/zap/pseudo");
07648
07649 if (p->subs[SUB_REAL].zfd < 0) {
07650 ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno));
07651 destroy_zt_pvt(&p);
07652 return NULL;
07653 }
07654 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
07655 if (!res) {
07656 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
07657 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
07658 bi.numbufs = numbufs;
07659 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
07660 if (res < 0) {
07661 ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel\n");
07662 }
07663 } else
07664 ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel\n");
07665 }
07666 p->destroy = 1;
07667 p->next = iflist;
07668 iflist = p;
07669 return p;
07670 }
07671
07672
07673 #ifdef HAVE_PRI
07674 static int pri_find_empty_chan(struct zt_pri *pri, int backwards)
07675 {
07676 int x;
07677 if (backwards)
07678 x = pri->numchans;
07679 else
07680 x = 0;
07681 for (;;) {
07682 if (backwards && (x < 0))
07683 break;
07684 if (!backwards && (x >= pri->numchans))
07685 break;
07686 if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
07687 ast_log(LOG_DEBUG, "Found empty available channel %d/%d\n",
07688 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
07689 return x;
07690 }
07691 if (backwards)
07692 x--;
07693 else
07694 x++;
07695 }
07696 return -1;
07697 }
07698 #endif
07699
07700 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause)
07701 {
07702 int groupmatch = 0;
07703 int channelmatch = -1;
07704 int roundrobin = 0;
07705 int callwait = 0;
07706 int busy = 0;
07707 struct zt_pvt *p;
07708 struct ast_channel *tmp = NULL;
07709 char *dest=NULL;
07710 int x;
07711 char *s;
07712 char opt=0;
07713 int res=0, y=0;
07714 int backwards = 0;
07715 #ifdef HAVE_PRI
07716 int crv;
07717 int bearer = -1;
07718 int trunkgroup;
07719 struct zt_pri *pri=NULL;
07720 #endif
07721 struct zt_pvt *exit, *start, *end;
07722 ast_mutex_t *lock;
07723 int channelmatched = 0;
07724 int groupmatched = 0;
07725
07726
07727 lock = &iflock;
07728 start = iflist;
07729 end = ifend;
07730 if (data) {
07731 dest = ast_strdupa((char *)data);
07732 } else {
07733 ast_log(LOG_WARNING, "Channel requested with no data\n");
07734 return NULL;
07735 }
07736 if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') {
07737
07738 char *stringp=NULL;
07739 stringp=dest + 1;
07740 s = strsep(&stringp, "/");
07741 if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07742 ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data);
07743 return NULL;
07744 }
07745 groupmatch = 1 << x;
07746 if (toupper(dest[0]) == 'G') {
07747 if (dest[0] == 'G') {
07748 backwards = 1;
07749 p = ifend;
07750 } else
07751 p = iflist;
07752 } else {
07753 if (dest[0] == 'R') {
07754 backwards = 1;
07755 p = round_robin[x]?round_robin[x]->prev:ifend;
07756 if (!p)
07757 p = ifend;
07758 } else {
07759 p = round_robin[x]?round_robin[x]->next:iflist;
07760 if (!p)
07761 p = iflist;
07762 }
07763 roundrobin = 1;
07764 }
07765 } else {
07766 char *stringp=NULL;
07767 stringp=dest;
07768 s = strsep(&stringp, "/");
07769 p = iflist;
07770 if (!strcasecmp(s, "pseudo")) {
07771
07772 x = CHAN_PSEUDO;
07773 channelmatch = x;
07774 }
07775 #ifdef HAVE_PRI
07776 else if ((res = sscanf(s, "%d:%d%c%d", &trunkgroup, &crv, &opt, &y)) > 1) {
07777 if ((trunkgroup < 1) || (crv < 1)) {
07778 ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data);
07779 return NULL;
07780 }
07781 res--;
07782 for (x = 0; x < NUM_SPANS; x++) {
07783 if (pris[x].trunkgroup == trunkgroup) {
07784 pri = pris + x;
07785 lock = &pri->lock;
07786 start = pri->crvs;
07787 end = pri->crvend;
07788 break;
07789 }
07790 }
07791 if (!pri) {
07792 ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup);
07793 return NULL;
07794 }
07795 channelmatch = crv;
07796 p = pris[x].crvs;
07797 }
07798 #endif
07799 else if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07800 ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data);
07801 return NULL;
07802 } else {
07803 channelmatch = x;
07804 }
07805 }
07806
07807 ast_mutex_lock(lock);
07808 exit = p;
07809 while (p && !tmp) {
07810 if (roundrobin)
07811 round_robin[x] = p;
07812 #if 0
07813 ast_verbose("name = %s, %d, %d, %d\n",p->owner ? p->owner->name : "<none>", p->channel, channelmatch, groupmatch);
07814 #endif
07815
07816 if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) {
07817 if (option_debug)
07818 ast_log(LOG_DEBUG, "Using channel %d\n", p->channel);
07819 if (p->inalarm)
07820 goto next;
07821
07822 callwait = (p->owner != NULL);
07823 #ifdef HAVE_PRI
07824 if (pri && (p->subs[SUB_REAL].zfd < 0)) {
07825 if (p->sig != SIG_FXSKS) {
07826
07827
07828 bearer = pri_find_empty_chan(pri, 0);
07829 if (bearer < 0) {
07830 ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv);
07831 p = NULL;
07832 break;
07833 }
07834 pri_assign_bearer(p, pri, pri->pvts[bearer]);
07835 } else {
07836 if (alloc_sub(p, 0)) {
07837 ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n");
07838 p = NULL;
07839 break;
07840 } else
07841 ast_log(LOG_DEBUG, "Allocated placeholder pseudo channel\n");
07842 p->pri = pri;
07843 }
07844 }
07845 #endif
07846 if (p->channel == CHAN_PSEUDO) {
07847 p = chandup(p);
07848 if (!p) {
07849 break;
07850 }
07851 }
07852 if (p->owner) {
07853 if (alloc_sub(p, SUB_CALLWAIT)) {
07854 p = NULL;
07855 break;
07856 }
07857 }
07858 p->outgoing = 1;
07859 tmp = zt_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0);
07860 #ifdef HAVE_PRI
07861 if (p->bearer) {
07862
07863 p->bearer->owner = tmp;
07864 }
07865 #endif
07866
07867 if (res > 1) {
07868 if (opt == 'c') {
07869
07870 p->confirmanswer = 1;
07871 } else if (opt == 'r') {
07872
07873 if (res < 3)
07874 ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data);
07875 else
07876 p->distinctivering = y;
07877 } else if (opt == 'd') {
07878
07879 p->digital = 1;
07880 if (tmp)
07881 tmp->transfercapability = AST_TRANS_CAP_DIGITAL;
07882 } else {
07883 ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data);
07884 }
07885 }
07886
07887 if (tmp && callwait)
07888 tmp->cdrflags |= AST_CDR_CALLWAIT;
07889 break;
07890 }
07891 next:
07892 if (backwards) {
07893 p = p->prev;
07894 if (!p)
07895 p = end;
07896 } else {
07897 p = p->next;
07898 if (!p)
07899 p = start;
07900 }
07901
07902 if (p == exit)
07903 break;
07904 }
07905 ast_mutex_unlock(lock);
07906 restart_monitor();
07907 if (callwait)
07908 *cause = AST_CAUSE_BUSY;
07909 else if (!tmp) {
07910 if (channelmatched) {
07911 if (busy)
07912 *cause = AST_CAUSE_BUSY;
07913 } else if (groupmatched) {
07914 *cause = AST_CAUSE_CONGESTION;
07915 }
07916 }
07917
07918 return tmp;
07919 }
07920
07921
07922 #ifdef HAVE_PRI
07923 static struct zt_pvt *pri_find_crv(struct zt_pri *pri, int crv)
07924 {
07925 struct zt_pvt *p;
07926 p = pri->crvs;
07927 while (p) {
07928 if (p->channel == crv)
07929 return p;
07930 p = p->next;
07931 }
07932 return NULL;
07933 }
07934
07935
07936 static int pri_find_principle(struct zt_pri *pri, int channel)
07937 {
07938 int x;
07939 int span = PRI_SPAN(channel);
07940 int spanfd;
07941 ZT_PARAMS param;
07942 int principle = -1;
07943 int explicit = PRI_EXPLICIT(channel);
07944 channel = PRI_CHANNEL(channel);
07945
07946 if (!explicit) {
07947 spanfd = pri_active_dchan_fd(pri);
07948 if (ioctl(spanfd, ZT_GET_PARAMS, ¶m))
07949 return -1;
07950 span = pris[param.spanno - 1].prilogicalspan;
07951 }
07952
07953 for (x = 0; x < pri->numchans; x++) {
07954 if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
07955 principle = x;
07956 break;
07957 }
07958 }
07959
07960 return principle;
07961 }
07962
07963 static int pri_fixup_principle(struct zt_pri *pri, int principle, q931_call *c)
07964 {
07965 int x;
07966 struct zt_pvt *crv;
07967 if (!c) {
07968 if (principle < 0)
07969 return -1;
07970 return principle;
07971 }
07972 if ((principle > -1) &&
07973 (principle < pri->numchans) &&
07974 (pri->pvts[principle]) &&
07975 (pri->pvts[principle]->call == c))
07976 return principle;
07977
07978 for (x = 0; x < pri->numchans; x++) {
07979 if (!pri->pvts[x])
07980 continue;
07981 if (pri->pvts[x]->call == c) {
07982
07983 if (principle != x) {
07984 if (option_verbose > 2)
07985 ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n",
07986 pri->pvts[x]->channel, pri->pvts[principle]->channel);
07987 if (pri->pvts[principle]->owner) {
07988 ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
07989 pri->pvts[x]->channel, pri->pvts[principle]->channel, pri->pvts[principle]->channel);
07990 return -1;
07991 }
07992
07993 pri->pvts[principle]->owner = pri->pvts[x]->owner;
07994 if (pri->pvts[principle]->owner) {
07995 ast_string_field_build(pri->pvts[principle]->owner, name,
07996 "Zap/%d:%d-%d", pri->trunkgroup,
07997 pri->pvts[principle]->channel, 1);
07998 pri->pvts[principle]->owner->tech_pvt = pri->pvts[principle];
07999 pri->pvts[principle]->owner->fds[0] = pri->pvts[principle]->subs[SUB_REAL].zfd;
08000 pri->pvts[principle]->subs[SUB_REAL].owner = pri->pvts[x]->subs[SUB_REAL].owner;
08001 } else
08002 ast_log(LOG_WARNING, "Whoa, there's no owner, and we're having to fix up channel %d to channel %d\n", pri->pvts[x]->channel, pri->pvts[principle]->channel);
08003 pri->pvts[principle]->call = pri->pvts[x]->call;
08004
08005 pri->pvts[x]->subs[SUB_REAL].owner = NULL;
08006 pri->pvts[x]->owner = NULL;
08007 pri->pvts[x]->call = NULL;
08008 }
08009 return principle;
08010 }
08011 }
08012
08013 crv = pri->crvs;
08014 while (crv) {
08015 if (crv->call == c) {
08016
08017 if (crv->bearer)
08018 ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n");
08019 else if (pri->pvts[principle]->owner)
08020 ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n");
08021 else {
08022
08023
08024 zt_close(crv->subs[SUB_REAL].zfd);
08025 pri->pvts[principle]->call = crv->call;
08026 pri_assign_bearer(crv, pri, pri->pvts[principle]);
08027 ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n",
08028 pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset,
08029 pri->trunkgroup, crv->channel);
08030 wakeup_sub(crv, SUB_REAL, pri);
08031 }
08032 return principle;
08033 }
08034 crv = crv->next;
08035 }
08036 ast_log(LOG_WARNING, "Call specified, but not found?\n");
08037 return -1;
08038 }
08039
08040 static void *do_idle_thread(void *vchan)
08041 {
08042 struct ast_channel *chan = vchan;
08043 struct zt_pvt *pvt = chan->tech_pvt;
08044 struct ast_frame *f;
08045 char ex[80];
08046
08047 int newms, ms = 30000;
08048 if (option_verbose > 2)
08049 ast_verbose(VERBOSE_PREFIX_3 "Initiating idle call on channel %s\n", chan->name);
08050 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
08051 if (ast_call(chan, ex, 0)) {
08052 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
08053 ast_hangup(chan);
08054 return NULL;
08055 }
08056 while ((newms = ast_waitfor(chan, ms)) > 0) {
08057 f = ast_read(chan);
08058 if (!f) {
08059
08060 break;
08061 }
08062 if (f->frametype == AST_FRAME_CONTROL) {
08063 switch (f->subclass) {
08064 case AST_CONTROL_ANSWER:
08065
08066 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
08067 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
08068 chan->priority = 1;
08069 if (option_verbose > 3)
08070 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
08071 ast_pbx_run(chan);
08072
08073 return NULL;
08074 case AST_CONTROL_BUSY:
08075 if (option_verbose > 3)
08076 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' busy, waiting...\n", chan->name);
08077 break;
08078 case AST_CONTROL_CONGESTION:
08079 if (option_verbose > 3)
08080 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' congested, waiting...\n", chan->name);
08081 break;
08082 };
08083 }
08084 ast_frfree(f);
08085 ms = newms;
08086 }
08087
08088 ast_hangup(chan);
08089 return NULL;
08090 }
08091
08092 #ifndef PRI_RESTART
08093 #error "Upgrade your libpri"
08094 #endif
08095 static void zt_pri_message(struct pri *pri, char *s)
08096 {
08097 int x, y;
08098 int dchan = -1, span = -1;
08099 int dchancount = 0;
08100
08101 if (pri) {
08102 for (x = 0; x < NUM_SPANS; x++) {
08103 for (y = 0; y < NUM_DCHANS; y++) {
08104 if (pris[x].dchans[y])
08105 dchancount++;
08106
08107 if (pris[x].dchans[y] == pri)
08108 dchan = y;
08109 }
08110 if (dchan >= 0) {
08111 span = x;
08112 break;
08113 }
08114 dchancount = 0;
08115 }
08116 if ((dchan >= 0) && (span >= 0)) {
08117 if (dchancount > 1)
08118 ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s);
08119 else
08120 ast_verbose("%s", s);
08121 } else
08122 ast_log(LOG_ERROR, "PRI debug error: could not find pri associated it with debug message output\n");
08123 } else
08124 ast_verbose("%s", s);
08125
08126 ast_mutex_lock(&pridebugfdlock);
08127
08128 if (pridebugfd >= 0)
08129 write(pridebugfd, s, strlen(s));
08130
08131 ast_mutex_unlock(&pridebugfdlock);
08132 }
08133
08134 static void zt_pri_error(struct pri *pri, char *s)
08135 {
08136 int x, y;
08137 int dchan = -1, span = -1;
08138 int dchancount = 0;
08139
08140 if (pri) {
08141 for (x = 0; x < NUM_SPANS; x++) {
08142 for (y = 0; y < NUM_DCHANS; y++) {
08143 if (pris[x].dchans[y])
08144 dchancount++;
08145
08146 if (pris[x].dchans[y] == pri)
08147 dchan = y;
08148 }
08149 if (dchan >= 0) {
08150 span = x;
08151 break;
08152 }
08153 dchancount = 0;
08154 }
08155 if ((dchan >= 0) && (span >= 0)) {
08156 if (dchancount > 1)
08157 ast_log(LOG_ERROR, "[Span %d D-Channel %d] PRI: %s", span, dchan, s);
08158 else
08159 ast_log(LOG_ERROR, "%s", s);
08160 } else
08161 ast_log(LOG_ERROR, "PRI debug error: could not find pri associated it with debug message output\n");
08162 } else
08163 ast_log(LOG_ERROR, "%s", s);
08164
08165 ast_mutex_lock(&pridebugfdlock);
08166
08167 if (pridebugfd >= 0)
08168 write(pridebugfd, s, strlen(s));
08169
08170 ast_mutex_unlock(&pridebugfdlock);
08171 }
08172
08173 static int pri_check_restart(struct zt_pri *pri)
08174 {
08175 do {
08176 pri->resetpos++;
08177 } while ((pri->resetpos < pri->numchans) &&
08178 (!pri->pvts[pri->resetpos] ||
08179 pri->pvts[pri->resetpos]->call ||
08180 pri->pvts[pri->resetpos]->resetting));
08181 if (pri->resetpos < pri->numchans) {
08182
08183 pri->pvts[pri->resetpos]->resetting = 1;
08184 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
08185 } else {
08186 pri->resetting = 0;
08187 time(&pri->lastreset);
08188 }
08189 return 0;
08190 }
08191
08192 static int pri_hangup_all(struct zt_pvt *p, struct zt_pri *pri)
08193 {
08194 int x;
08195 int redo;
08196 ast_mutex_unlock(&pri->lock);
08197 ast_mutex_lock(&p->lock);
08198 do {
08199 redo = 0;
08200 for (x = 0; x < 3; x++) {
08201 while (p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) {
08202 redo++;
08203 ast_mutex_unlock(&p->lock);
08204 usleep(1);
08205 ast_mutex_lock(&p->lock);
08206 }
08207 if (p->subs[x].owner) {
08208 ast_queue_hangup(p->subs[x].owner);
08209 ast_mutex_unlock(&p->subs[x].owner->lock);
08210 }
08211 }
08212 } while (redo);
08213 ast_mutex_unlock(&p->lock);
08214 ast_mutex_lock(&pri->lock);
08215 return 0;
08216 }
08217
08218 static char * redirectingreason2str(int redirectingreason)
08219 {
08220 switch (redirectingreason) {
08221 case 0:
08222 return "UNKNOWN";
08223 case 1:
08224 return "BUSY";
08225 case 2:
08226 return "NO_REPLY";
08227 case 0xF:
08228 return "UNCONDITIONAL";
08229 default:
08230 return "NOREDIRECT";
08231 }
08232 }
08233
08234 static void apply_plan_to_number(char *buf, size_t size, const struct zt_pri *pri, const char *number, const int plan)
08235 {
08236 switch (plan) {
08237 case PRI_INTERNATIONAL_ISDN:
08238 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
08239 break;
08240 case PRI_NATIONAL_ISDN:
08241 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
08242 break;
08243 case PRI_LOCAL_ISDN:
08244 snprintf(buf, size, "%s%s", pri->localprefix, number);
08245 break;
08246 case PRI_PRIVATE:
08247 snprintf(buf, size, "%s%s", pri->privateprefix, number);
08248 break;
08249 case PRI_UNKNOWN:
08250 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
08251 break;
08252 default:
08253 snprintf(buf, size, "%s", number);
08254 break;
08255 }
08256 }
08257
08258 static int zt_setlaw(int zfd, int law)
08259 {
08260 int res;
08261 res = ioctl(zfd, ZT_SETLAW, &law);
08262 if (res)
08263 return res;
08264 return 0;
08265 }
08266
08267 static void *pri_dchannel(void *vpri)
08268 {
08269 struct zt_pri *pri = vpri;
08270 pri_event *e;
08271 struct pollfd fds[NUM_DCHANS];
08272 int res;
08273 int chanpos = 0;
08274 int x;
08275 int haveidles;
08276 int activeidles;
08277 int nextidle = -1;
08278 struct ast_channel *c;
08279 struct timeval tv, lowest, *next;
08280 struct timeval lastidle = { 0, 0 };
08281 int doidling=0;
08282 char *cc;
08283 char idlen[80];
08284 struct ast_channel *idle;
08285 pthread_t p;
08286 time_t t;
08287 int i, which=-1;
08288 int numdchans;
08289 int cause=0;
08290 struct zt_pvt *crv;
08291 pthread_t threadid;
08292 pthread_attr_t attr;
08293 char ani2str[6];
08294 char plancallingnum[256];
08295 char plancallingani[256];
08296 char calledtonstr[10];
08297
08298 gettimeofday(&lastidle, NULL);
08299 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
08300
08301 cc = strchr(pri->idleext, '@');
08302 if (cc) {
08303 *cc = '\0';
08304 cc++;
08305 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
08306 #if 0
08307
08308 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
08309 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
08310 else
08311 #endif
08312 doidling = 1;
08313 } else
08314 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
08315 }
08316 for (;;) {
08317 for (i = 0; i < NUM_DCHANS; i++) {
08318 if (!pri->dchannels[i])
08319 break;
08320 fds[i].fd = pri->fds[i];
08321 fds[i].events = POLLIN | POLLPRI;
08322 fds[i].revents = 0;
08323 }
08324 numdchans = i;
08325 time(&t);
08326 ast_mutex_lock(&pri->lock);
08327 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->resetinterval > 0)) {
08328 if (pri->resetting && pri_is_up(pri)) {
08329 if (pri->resetpos < 0)
08330 pri_check_restart(pri);
08331 } else {
08332 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
08333 pri->resetting = 1;
08334 pri->resetpos = -1;
08335 }
08336 }
08337 }
08338
08339 if (doidling && pri_is_up(pri)) {
08340 nextidle = -1;
08341 haveidles = 0;
08342 activeidles = 0;
08343 for (x = pri->numchans; x >= 0; x--) {
08344 if (pri->pvts[x] && !pri->pvts[x]->owner &&
08345 !pri->pvts[x]->call) {
08346 if (haveidles < pri->minunused) {
08347 haveidles++;
08348 } else if (!pri->pvts[x]->resetting) {
08349 nextidle = x;
08350 break;
08351 }
08352 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
08353 activeidles++;
08354 }
08355 if (nextidle > -1) {
08356 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
08357
08358 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
08359 idle = zt_request("Zap", AST_FORMAT_ULAW, idlen, &cause);
08360 if (idle) {
08361 pri->pvts[nextidle]->isidlecall = 1;
08362 if (ast_pthread_create_background(&p, NULL, do_idle_thread, idle)) {
08363 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
08364 zt_hangup(idle);
08365 }
08366 } else
08367 ast_log(LOG_WARNING, "Unable to request channel 'Zap/%s' for idle call\n", idlen);
08368 gettimeofday(&lastidle, NULL);
08369 }
08370 } else if ((haveidles < pri->minunused) &&
08371 (activeidles > pri->minidle)) {
08372
08373
08374 for (x = pri->numchans; x >= 0; x--) {
08375
08376 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
08377 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08378 haveidles++;
08379
08380
08381 if ((haveidles >= pri->minunused) ||
08382 (activeidles <= pri->minidle))
08383 break;
08384 }
08385 }
08386 }
08387 }
08388
08389 lowest = ast_tv(60, 0);
08390 for (i = 0; i < NUM_DCHANS; i++) {
08391
08392 if (!pri->dchannels[i])
08393 break;
08394 if ((next = pri_schedule_next(pri->dchans[i]))) {
08395
08396 tv = ast_tvsub(*next, ast_tvnow());
08397 if (tv.tv_sec < 0) {
08398 tv = ast_tv(0,0);
08399 }
08400 if (doidling || pri->resetting) {
08401 if (tv.tv_sec > 1) {
08402 tv = ast_tv(1, 0);
08403 }
08404 } else {
08405 if (tv.tv_sec > 60) {
08406 tv = ast_tv(60, 0);
08407 }
08408 }
08409 } else if (doidling || pri->resetting) {
08410
08411
08412 tv = ast_tv(1,0);
08413 } else {
08414
08415 tv = ast_tv(60, 0);
08416 }
08417 if (!i || ast_tvcmp(tv, lowest) < 0) {
08418 lowest = tv;
08419 }
08420 }
08421 ast_mutex_unlock(&pri->lock);
08422
08423 e = NULL;
08424 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
08425
08426 ast_mutex_lock(&pri->lock);
08427 if (!res) {
08428 for (which = 0; which < NUM_DCHANS; which++) {
08429 if (!pri->dchans[which])
08430 break;
08431
08432 e = pri_schedule_run(pri->dchans[which]);
08433 if (e)
08434 break;
08435 }
08436 } else if (res > -1) {
08437 for (which = 0; which < NUM_DCHANS; which++) {
08438 if (!pri->dchans[which])
08439 break;
08440 if (fds[which].revents & POLLPRI) {
08441
08442 x = 0;
08443 res = ioctl(pri->fds[which], ZT_GETEVENT, &x);
08444 if (x)
08445 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);
08446
08447 if (x == ZT_EVENT_ALARM) {
08448 pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
08449 pri_find_dchan(pri);
08450 } else if (x == ZT_EVENT_NOALARM) {
08451 pri->dchanavail[which] |= DCHAN_NOTINALARM;
08452 pri_restart(pri->dchans[which]);
08453 }
08454
08455 if (option_debug)
08456 ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
08457 } else if (fds[which].revents & POLLIN) {
08458 e = pri_check_event(pri->dchans[which]);
08459 }
08460 if (e)
08461 break;
08462 }
08463 } else if (errno != EINTR)
08464 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
08465
08466 if (e) {
08467 if (pri->debug)
08468 pri_dump_event(pri->dchans[which], e);
08469 if (e->e != PRI_EVENT_DCHAN_DOWN)
08470 pri->dchanavail[which] |= DCHAN_UP;
08471
08472 if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
08473
08474 pri->pri = pri->dchans[which];
08475
08476 switch (e->e) {
08477 case PRI_EVENT_DCHAN_UP:
08478 if (option_verbose > 1)
08479 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span);
08480 pri->dchanavail[which] |= DCHAN_UP;
08481 if (!pri->pri) pri_find_dchan(pri);
08482
08483
08484 time(&pri->lastreset);
08485
08486
08487 if (pri->resetinterval > -1) {
08488 pri->lastreset -= pri->resetinterval;
08489 pri->lastreset += 5;
08490 }
08491 pri->resetting = 0;
08492
08493 for (i = 0; i < pri->numchans; i++)
08494 if (pri->pvts[i]) {
08495 pri->pvts[i]->inalarm = 0;
08496 }
08497 break;
08498 case PRI_EVENT_DCHAN_DOWN:
08499 if (option_verbose > 1)
08500 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span);
08501 pri->dchanavail[which] &= ~DCHAN_UP;
08502 pri_find_dchan(pri);
08503 if (!pri_is_up(pri)) {
08504 pri->resetting = 0;
08505
08506 for (i = 0; i < pri->numchans; i++) {
08507 struct zt_pvt *p = pri->pvts[i];
08508 if (p) {
08509 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
08510
08511 if (p->call) {
08512 if (p->pri && p->pri->pri) {
08513 pri_hangup(p->pri->pri, p->call, -1);
08514 pri_destroycall(p->pri->pri, p->call);
08515 p->call = NULL;
08516 } else
08517 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
08518 }
08519 if (p->realcall) {
08520 pri_hangup_all(p->realcall, pri);
08521 } else if (p->owner)
08522 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08523 }
08524 p->inalarm = 1;
08525 }
08526 }
08527 }
08528 break;
08529 case PRI_EVENT_RESTART:
08530 if (e->restart.channel > -1) {
08531 chanpos = pri_find_principle(pri, e->restart.channel);
08532 if (chanpos < 0)
08533 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
08534 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08535 else {
08536 if (option_verbose > 2)
08537 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n",
08538 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08539 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08540 if (pri->pvts[chanpos]->call) {
08541 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
08542 pri->pvts[chanpos]->call = NULL;
08543 }
08544
08545 if (pri->pvts[chanpos]->realcall)
08546 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08547 else if (pri->pvts[chanpos]->owner)
08548 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08549 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08550 }
08551 } else {
08552 if (option_verbose > 2)
08553 ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span);
08554 for (x = 0; x < pri->numchans; x++)
08555 if (pri->pvts[x]) {
08556 ast_mutex_lock(&pri->pvts[x]->lock);
08557 if (pri->pvts[x]->call) {
08558 pri_destroycall(pri->pri, pri->pvts[x]->call);
08559 pri->pvts[x]->call = NULL;
08560 }
08561 if (pri->pvts[chanpos]->realcall)
08562 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08563 else if (pri->pvts[x]->owner)
08564 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08565 ast_mutex_unlock(&pri->pvts[x]->lock);
08566 }
08567 }
08568 break;
08569 case PRI_EVENT_KEYPAD_DIGIT:
08570 chanpos = pri_find_principle(pri, e->digit.channel);
08571 if (chanpos < 0) {
08572 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
08573 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
08574 } else {
08575 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
08576 if (chanpos > -1) {
08577 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08578
08579 if (pri->overlapdial && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
08580
08581 int digitlen = strlen(e->digit.digits);
08582 char digit;
08583 int i;
08584 for (i = 0; i < digitlen; i++) {
08585 digit = e->digit.digits[i];
08586 {
08587 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08588 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08589 }
08590 }
08591 }
08592 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08593 }
08594 }
08595 break;
08596
08597 case PRI_EVENT_INFO_RECEIVED:
08598 chanpos = pri_find_principle(pri, e->ring.channel);
08599 if (chanpos < 0) {
08600 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
08601 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08602 } else {
08603 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
08604 if (chanpos > -1) {
08605 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08606
08607 if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
08608
08609 int digitlen = strlen(e->ring.callednum);
08610 char digit;
08611 int i;
08612 for (i = 0; i < digitlen; i++) {
08613 digit = e->ring.callednum[i];
08614 {
08615 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08616 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08617 }
08618 }
08619 }
08620 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08621 }
08622 }
08623 break;
08624 case PRI_EVENT_RING:
08625 crv = NULL;
08626 if (e->ring.channel == -1)
08627 chanpos = pri_find_empty_chan(pri, 1);
08628 else
08629 chanpos = pri_find_principle(pri, e->ring.channel);
08630
08631 if (chanpos < 0) {
08632 ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
08633 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08634 } else {
08635 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08636 if (pri->pvts[chanpos]->owner) {
08637 if (pri->pvts[chanpos]->call == e->ring.call) {
08638 ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
08639 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08640 break;
08641 } else {
08642
08643 ast_log(LOG_DEBUG, "Ring requested on channel %d/%d already in use or previously requested on span %d. Attempting to renegotiating channel.\n",
08644 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08645 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08646 chanpos = -1;
08647 }
08648 }
08649 if (chanpos > -1)
08650 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08651 }
08652 if ((chanpos < 0) && (e->ring.flexible))
08653 chanpos = pri_find_empty_chan(pri, 1);
08654 if (chanpos > -1) {
08655 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08656 if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
08657
08658 crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
08659 if (crv)
08660 ast_mutex_lock(&crv->lock);
08661 if (!crv || crv->owner) {
08662 pri->pvts[chanpos]->call = NULL;
08663 if (crv) {
08664 if (crv->owner)
08665 crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08666 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);
08667 } else
08668 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);
08669 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
08670 if (crv)
08671 ast_mutex_unlock(&crv->lock);
08672 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08673 break;
08674 }
08675 }
08676 pri->pvts[chanpos]->call = e->ring.call;
08677 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
08678 if (pri->pvts[chanpos]->use_callerid) {
08679 ast_shrink_phone_number(plancallingnum);
08680 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
08681 #ifdef PRI_ANI
08682 if (!ast_strlen_zero(e->ring.callingani)) {
08683 apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
08684 ast_shrink_phone_number(plancallingani);
08685 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
08686 } else {
08687 pri->pvts[chanpos]->cid_ani[0] = '\0';
08688 }
08689 #endif
08690 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
08691 pri->pvts[chanpos]->cid_ton = e->ring.callingplan;
08692 } else {
08693 pri->pvts[chanpos]->cid_num[0] = '\0';
08694 pri->pvts[chanpos]->cid_ani[0] = '\0';
08695 pri->pvts[chanpos]->cid_name[0] = '\0';
08696 pri->pvts[chanpos]->cid_ton = 0;
08697 }
08698 apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
08699 e->ring.redirectingnum, e->ring.callingplanrdnis);
08700
08701 if (pri->pvts[chanpos]->immediate) {
08702 if (option_verbose > 2)
08703 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n");
08704 pri->pvts[chanpos]->exten[0] = 's';
08705 pri->pvts[chanpos]->exten[1] = '\0';
08706 }
08707
08708 else if (!ast_strlen_zero(e->ring.callednum)) {
08709 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
08710 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08711 } else
08712 pri->pvts[chanpos]->exten[0] = '\0';
08713
08714 if (!ast_strlen_zero(e->ring.callednum))
08715 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08716
08717 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
08718 if (option_verbose > 2)
08719 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n");
08720 pri->pvts[chanpos]->exten[0] = 's';
08721 pri->pvts[chanpos]->exten[1] = '\0';
08722 }
08723
08724 if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
08725 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08726
08727 int law;
08728 if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
08729
08730 law = 1;
08731 if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &law) == -1)
08732 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", pri->pvts[chanpos]->channel, law);
08733 }
08734 if (e->ring.layer1 == PRI_LAYER_1_ALAW)
08735 law = ZT_LAW_ALAW;
08736 else
08737 law = ZT_LAW_MULAW;
08738 res = zt_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].zfd, law);
08739 if (res < 0)
08740 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
08741 res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].zfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
08742 if (res < 0)
08743 ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
08744 if (e->ring.complete || !pri->overlapdial) {
08745
08746 pri->pvts[chanpos]->proceeding = 1;
08747 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
08748 } else {
08749 if (pri->switchtype != PRI_SWITCH_GR303_TMC)
08750 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08751 else
08752 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08753 }
08754
08755 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
08756
08757
08758 if (pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08759
08760 ast_mutex_unlock(&pri->lock);
08761 if (crv) {
08762
08763 pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
08764 c = zt_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08765 pri->pvts[chanpos]->owner = &inuse;
08766 ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
08767 } else {
08768 c = zt_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08769 }
08770 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
08771 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
08772 }
08773 if (e->ring.ani2 >= 0) {
08774 snprintf(ani2str, 5, "%.2d", e->ring.ani2);
08775 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08776 }
08777
08778 #ifdef SUPPORT_USERUSER
08779 if (!ast_strlen_zero(e->ring.useruserinfo)) {
08780 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08781 }
08782 #endif
08783
08784 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08785 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08786 if (e->ring.redirectingreason >= 0)
08787 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08788
08789 ast_mutex_lock(&pri->lock);
08790 pthread_attr_init(&attr);
08791 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
08792 if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) {
08793 if (option_verbose > 2)
08794 ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
08795 plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
08796 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08797 } else {
08798 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
08799 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08800 if (c)
08801 ast_hangup(c);
08802 else {
08803 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08804 pri->pvts[chanpos]->call = NULL;
08805 }
08806 }
08807 pthread_attr_destroy(&attr);
08808 } else {
08809 ast_mutex_unlock(&pri->lock);
08810
08811 c = zt_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype);
08812 ast_mutex_lock(&pri->lock);
08813 if (c) {
08814 char calledtonstr[10];
08815 if (e->ring.ani2 >= 0) {
08816 snprintf(ani2str, 5, "%d", e->ring.ani2);
08817 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08818 }
08819
08820 #ifdef SUPPORT_USERUSER
08821 if (!ast_strlen_zero(e->ring.useruserinfo)) {
08822 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08823 }
08824 #endif
08825
08826 if (e->ring.redirectingreason >= 0)
08827 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08828
08829 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08830 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08831 if (option_verbose > 2)
08832 ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
08833 plancallingnum, pri->pvts[chanpos]->exten,
08834 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08835 zt_enable_ec(pri->pvts[chanpos]);
08836 } else {
08837 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
08838 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08839 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08840 pri->pvts[chanpos]->call = NULL;
08841 }
08842 }
08843 } else {
08844 if (option_verbose > 2)
08845 ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
08846 pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
08847 pri->pvts[chanpos]->prioffset, pri->span);
08848 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
08849 pri->pvts[chanpos]->call = NULL;
08850 pri->pvts[chanpos]->exten[0] = '\0';
08851 }
08852 if (crv)
08853 ast_mutex_unlock(&crv->lock);
08854 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08855 } else {
08856 if (e->ring.flexible)
08857 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
08858 else
08859 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
08860 }
08861 break;
08862 case PRI_EVENT_RINGING:
08863 chanpos = pri_find_principle(pri, e->ringing.channel);
08864 if (chanpos < 0) {
08865 ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
08866 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08867 } else {
08868 chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
08869 if (chanpos < 0) {
08870 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
08871 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08872 } else {
08873 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08874 if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08875 zt_enable_ec(pri->pvts[chanpos]);
08876 pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1;
08877 pri->pvts[chanpos]->alerting = 1;
08878 } else
08879 ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n");
08880 #ifdef PRI_PROGRESS_MASK
08881 if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08882 #else
08883 if (e->ringing.progress == 8) {
08884 #endif
08885
08886 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08887
08888 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING);
08889 pri->pvts[chanpos]->dsp_features = 0;
08890 }
08891 }
08892
08893 #ifdef SUPPORT_USERUSER
08894 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
08895 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->ringing.useruserinfo);
08896 }
08897 #endif
08898
08899 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08900 }
08901 }
08902 break;
08903 case PRI_EVENT_PROGRESS:
08904
08905 chanpos = pri_find_principle(pri, e->proceeding.channel);
08906 if (chanpos > -1) {
08907 #ifdef PRI_PROGRESS_MASK
08908 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) {
08909 #else
08910 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) {
08911 #endif
08912 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
08913
08914 if (e->proceeding.cause > -1) {
08915 if (option_verbose > 2)
08916 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with cause code %d received\n", e->proceeding.cause);
08917
08918
08919 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
08920 if (pri->pvts[chanpos]->owner) {
08921 if (option_verbose > 2)
08922 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
08923
08924 pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
08925 f.subclass = AST_CONTROL_BUSY;
08926 }
08927 }
08928 }
08929
08930 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08931 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
08932 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08933 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08934 #ifdef PRI_PROGRESS_MASK
08935 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08936 #else
08937 if (e->proceeding.progress == 8) {
08938 #endif
08939
08940 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08941 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08942 pri->pvts[chanpos]->dsp_features = 0;
08943 }
08944 }
08945 pri->pvts[chanpos]->progress = 1;
08946 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08947 }
08948 }
08949 break;
08950 case PRI_EVENT_PROCEEDING:
08951 chanpos = pri_find_principle(pri, e->proceeding.channel);
08952 if (chanpos > -1) {
08953 if (!pri->pvts[chanpos]->proceeding) {
08954 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
08955
08956 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08957 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
08958 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08959 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08960 #ifdef PRI_PROGRESS_MASK
08961 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08962 #else
08963 if (e->proceeding.progress == 8) {
08964 #endif
08965
08966 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08967 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08968 pri->pvts[chanpos]->dsp_features = 0;
08969 }
08970
08971 f.subclass = AST_CONTROL_PROGRESS;
08972 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08973 }
08974 pri->pvts[chanpos]->proceeding = 1;
08975 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08976 }
08977 }
08978 break;
08979 case PRI_EVENT_FACNAME:
08980 chanpos = pri_find_principle(pri, e->facname.channel);
08981 if (chanpos < 0) {
08982 ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
08983 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08984 } else {
08985 chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
08986 if (chanpos < 0) {
08987 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
08988 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08989 } else {
08990
08991 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08992 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
08993 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
08994 pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
08995 zt_enable_ec(pri->pvts[chanpos]);
08996 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08997 }
08998 }
08999 break;
09000 case PRI_EVENT_ANSWER:
09001 chanpos = pri_find_principle(pri, e->answer.channel);
09002 if (chanpos < 0) {
09003 ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
09004 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
09005 } else {
09006 chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
09007 if (chanpos < 0) {
09008 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
09009 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
09010 } else {
09011 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09012
09013
09014
09015
09016
09017 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
09018 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
09019 pri->pvts[chanpos]->dsp_features = 0;
09020 }
09021 if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) {
09022 ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n");
09023 x = ZT_START;
09024 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_HOOK, &x);
09025 if (res < 0) {
09026 if (errno != EINPROGRESS) {
09027 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
09028 }
09029 }
09030 } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
09031 pri->pvts[chanpos]->dialing = 1;
09032
09033 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_DIAL, &pri->pvts[chanpos]->dop);
09034 if (res < 0) {
09035 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", pri->pvts[chanpos]->channel);
09036 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
09037 } else
09038 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
09039 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
09040 } else if (pri->pvts[chanpos]->confirmanswer) {
09041 ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
09042 } else {
09043 pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
09044
09045 zt_enable_ec(pri->pvts[chanpos]);
09046 }
09047
09048 #ifdef SUPPORT_USERUSER
09049 if (!ast_strlen_zero(e->answer.useruserinfo)) {
09050 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->answer.useruserinfo);
09051 }
09052 #endif
09053
09054 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09055 }
09056 }
09057 break;
09058 case PRI_EVENT_HANGUP:
09059 chanpos = pri_find_principle(pri, e->hangup.channel);
09060 if (chanpos < 0) {
09061 ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
09062 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09063 } else {
09064 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09065 if (chanpos > -1) {
09066 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09067 if (!pri->pvts[chanpos]->alreadyhungup) {
09068
09069 pri->pvts[chanpos]->alreadyhungup = 1;
09070 if (pri->pvts[chanpos]->realcall)
09071 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09072 else if (pri->pvts[chanpos]->owner) {
09073
09074 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
09075
09076 if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
09077 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09078 else if (pri->pvts[chanpos]->owner->hangupcause == PRI_CAUSE_USER_BUSY)
09079 pri->pvts[chanpos]->subs[SUB_REAL].needbusy = 1;
09080 else
09081 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion = 1;
09082 }
09083 if (option_verbose > 2)
09084 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup, cause %d\n",
09085 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
09086 } else {
09087 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
09088 pri->pvts[chanpos]->call = NULL;
09089 }
09090 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
09091 if (option_verbose > 2)
09092 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
09093 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09094 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
09095 pri->pvts[chanpos]->resetting = 1;
09096 }
09097 if (e->hangup.aoc_units > -1)
09098 if (option_verbose > 2)
09099 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
09100 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
09101
09102 #ifdef SUPPORT_USERUSER
09103 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09104 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
09105 }
09106 #endif
09107
09108 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09109 } else {
09110 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
09111 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09112 }
09113 }
09114 break;
09115 #ifndef PRI_EVENT_HANGUP_REQ
09116 #error please update libpri
09117 #endif
09118 case PRI_EVENT_HANGUP_REQ:
09119 chanpos = pri_find_principle(pri, e->hangup.channel);
09120 if (chanpos < 0) {
09121 ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
09122 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09123 } else {
09124 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09125 if (chanpos > -1) {
09126 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09127 if (pri->pvts[chanpos]->realcall)
09128 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09129 else if (pri->pvts[chanpos]->owner) {
09130 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
09131
09132 if (pri->pvts[chanpos]->owner->_state == AST_STATE_UP)
09133 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09134 else if (pri->pvts[chanpos]->owner->hangupcause == PRI_CAUSE_USER_BUSY)
09135 pri->pvts[chanpos]->subs[SUB_REAL].needbusy = 1;
09136 else
09137 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion = 1;
09138
09139 if (option_verbose > 2)
09140 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);
09141 if (e->hangup.aoc_units > -1)
09142 if (option_verbose > 2)
09143 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
09144 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
09145 } else {
09146 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
09147 pri->pvts[chanpos]->call = NULL;
09148 }
09149 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
09150 if (option_verbose > 2)
09151 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n",
09152 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09153 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
09154 pri->pvts[chanpos]->resetting = 1;
09155 }
09156
09157 #ifdef SUPPORT_USERUSER
09158 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09159 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
09160 }
09161 #endif
09162
09163 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09164 } else {
09165 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);
09166 }
09167 }
09168 break;
09169 case PRI_EVENT_HANGUP_ACK:
09170 chanpos = pri_find_principle(pri, e->hangup.channel);
09171 if (chanpos < 0) {
09172 ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
09173 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09174 } else {
09175 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09176 if (chanpos > -1) {
09177 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09178 pri->pvts[chanpos]->call = NULL;
09179 pri->pvts[chanpos]->resetting = 0;
09180 if (pri->pvts[chanpos]->owner) {
09181 if (option_verbose > 2)
09182 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);
09183 }
09184
09185 #ifdef SUPPORT_USERUSER
09186 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09187 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
09188 }
09189 #endif
09190
09191 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09192 }
09193 }
09194 break;
09195 case PRI_EVENT_CONFIG_ERR:
09196 ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->trunkgroup, e->err.err);
09197 break;
09198 case PRI_EVENT_RESTART_ACK:
09199 chanpos = pri_find_principle(pri, e->restartack.channel);
09200 if (chanpos < 0) {
09201
09202
09203
09204 for (x = 0; x < pri->numchans; x++) {
09205 if (pri->pvts[x] && pri->pvts[x]->resetting) {
09206 chanpos = x;
09207 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09208 ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
09209 pri->pvts[chanpos]->prioffset, pri->span);
09210 if (pri->pvts[chanpos]->realcall)
09211 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09212 else if (pri->pvts[chanpos]->owner) {
09213 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
09214 pri->pvts[chanpos]->prioffset, pri->span);
09215 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09216 }
09217 pri->pvts[chanpos]->resetting = 0;
09218 if (option_verbose > 2)
09219 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
09220 pri->pvts[chanpos]->prioffset, pri->span);
09221 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09222 if (pri->resetting)
09223 pri_check_restart(pri);
09224 break;
09225 }
09226 }
09227 if (chanpos < 0) {
09228 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
09229 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09230 }
09231 } else {
09232 if (pri->pvts[chanpos]) {
09233 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09234 if (pri->pvts[chanpos]->realcall)
09235 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09236 else if (pri->pvts[chanpos]->owner) {
09237 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
09238 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09239 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09240 }
09241 pri->pvts[chanpos]->resetting = 0;
09242 if (option_verbose > 2)
09243 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
09244 pri->pvts[chanpos]->prioffset, pri->span);
09245 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09246 if (pri->resetting)
09247 pri_check_restart(pri);
09248 }
09249 }
09250 break;
09251 case PRI_EVENT_SETUP_ACK:
09252 chanpos = pri_find_principle(pri, e->setup_ack.channel);
09253 if (chanpos < 0) {
09254 ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
09255 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
09256 } else {
09257 chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
09258 if (chanpos > -1) {
09259 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09260 pri->pvts[chanpos]->setup_ack = 1;
09261
09262 for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
09263 ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
09264 pri_information(pri->pri, pri->pvts[chanpos]->call,
09265 pri->pvts[chanpos]->dialdest[x]);
09266 }
09267 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09268 } else
09269 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
09270 }
09271 break;
09272 case PRI_EVENT_NOTIFY:
09273 chanpos = pri_find_principle(pri, e->notify.channel);
09274 if (chanpos < 0) {
09275 ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
09276 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
09277 } else {
09278 struct ast_frame f = { AST_FRAME_CONTROL, };
09279 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09280 switch (e->notify.info) {
09281 case PRI_NOTIFY_REMOTE_HOLD:
09282 f.subclass = AST_CONTROL_HOLD;
09283 zap_queue_frame(pri->pvts[chanpos], &f, pri);
09284 break;
09285 case PRI_NOTIFY_REMOTE_RETRIEVAL:
09286 f.subclass = AST_CONTROL_UNHOLD;
09287 zap_queue_frame(pri->pvts[chanpos], &f, pri);
09288 break;
09289 }
09290 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09291 }
09292 break;
09293 default:
09294 ast_log(LOG_DEBUG, "Event: %d\n", e->e);
09295 }
09296 }
09297 ast_mutex_unlock(&pri->lock);
09298 }
09299
09300 return NULL;
09301 }
09302
09303 static int start_pri(struct zt_pri *pri)
09304 {
09305 int res, x;
09306 ZT_PARAMS p;
09307 ZT_BUFFERINFO bi;
09308 struct zt_spaninfo si;
09309 int i;
09310
09311 for (i = 0; i < NUM_DCHANS; i++) {
09312 if (!pri->dchannels[i])
09313 break;
09314 pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600);
09315 x = pri->dchannels[i];
09316 if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],ZT_SPECIFY,&x) == -1)) {
09317 ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
09318 return -1;
09319 }
09320 res = ioctl(pri->fds[i], ZT_GET_PARAMS, &p);
09321 if (res) {
09322 zt_close(pri->fds[i]);
09323 pri->fds[i] = -1;
09324 ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
09325 return -1;
09326 }
09327 if ((p.sigtype != ZT_SIG_HDLCFCS) && (p.sigtype != ZT_SIG_HARDHDLC)) {
09328 zt_close(pri->fds[i]);
09329 pri->fds[i] = -1;
09330 ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode. See /etc/zaptel.conf\n", x);
09331 return -1;
09332 }
09333 memset(&si, 0, sizeof(si));
09334 res = ioctl(pri->fds[i], ZT_SPANSTAT, &si);
09335 if (res) {
09336 zt_close(pri->fds[i]);
09337 pri->fds[i] = -1;
09338 ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
09339 }
09340 if (!si.alarms)
09341 pri->dchanavail[i] |= DCHAN_NOTINALARM;
09342 else
09343 pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
09344 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
09345 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
09346 bi.numbufs = 32;
09347 bi.bufsize = 1024;
09348 if (ioctl(pri->fds[i], ZT_SET_BUFINFO, &bi)) {
09349 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d\n", x);
09350 zt_close(pri->fds[i]);
09351 pri->fds[i] = -1;
09352 return -1;
09353 }
09354 pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
09355
09356 if (pri->switchtype == PRI_SWITCH_GR303_TMC)
09357 pri->overlapdial = 1;
09358 pri_set_overlapdial(pri->dchans[i],pri->overlapdial);
09359
09360 if (i)
09361 pri_enslave(pri->dchans[0], pri->dchans[i]);
09362 if (!pri->dchans[i]) {
09363 zt_close(pri->fds[i]);
09364 pri->fds[i] = -1;
09365 ast_log(LOG_ERROR, "Unable to create PRI structure\n");
09366 return -1;
09367 }
09368 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
09369 pri_set_nsf(pri->dchans[i], pri->nsf);
09370 #ifdef PRI_GETSET_TIMERS
09371 for (x = 0; x < PRI_MAX_TIMERS; x++) {
09372 if (pritimers[x] != 0)
09373 pri_set_timer(pri->dchans[i], x, pritimers[x]);
09374 }
09375 #endif
09376 }
09377
09378 pri->pri = pri->dchans[0];
09379 pri->resetpos = -1;
09380 if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
09381 for (i = 0; i < NUM_DCHANS; i++) {
09382 if (!pri->dchannels[i])
09383 break;
09384 zt_close(pri->fds[i]);
09385 pri->fds[i] = -1;
09386 }
09387 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
09388 return -1;
09389 }
09390 return 0;
09391 }
09392
09393 static char *complete_span_helper(const char *line, const char *word, int pos, int state, int rpos)
09394 {
09395 int which, span;
09396 char *ret = NULL;
09397
09398 if (pos != rpos)
09399 return ret;
09400
09401 for (which = span = 0; span < NUM_SPANS; span++) {
09402 if (pris[span].pri && ++which > state) {
09403 asprintf(&ret, "%d", span + 1);
09404 break;
09405 }
09406 }
09407 return ret;
09408 }
09409
09410 static char *complete_span_4(const char *line, const char *word, int pos, int state)
09411 {
09412 return complete_span_helper(line,word,pos,state,3);
09413 }
09414
09415 static char *complete_span_5(const char *line, const char *word, int pos, int state)
09416 {
09417 return complete_span_helper(line,word,pos,state,4);
09418 }
09419
09420 static int handle_pri_set_debug_file(int fd, int argc, char **argv)
09421 {
09422 int myfd;
09423
09424 if (!strncasecmp(argv[1], "set", 3)) {
09425 if (argc < 5)
09426 return RESULT_SHOWUSAGE;
09427
09428 if (ast_strlen_zero(argv[4]))
09429 return RESULT_SHOWUSAGE;
09430
09431 myfd = open(argv[4], O_CREAT|O_WRONLY);
09432 if (myfd < 0) {
09433 ast_cli(fd, "Unable to open '%s' for writing\n", argv[4]);
09434 return RESULT_SUCCESS;
09435 }
09436
09437 ast_mutex_lock(&pridebugfdlock);
09438
09439 if (pridebugfd >= 0)
09440 close(pridebugfd);
09441
09442 pridebugfd = myfd;
09443 ast_copy_string(pridebugfilename,argv[4],sizeof(pridebugfilename));
09444
09445 ast_mutex_unlock(&pridebugfdlock);
09446
09447 ast_cli(fd, "PRI debug output will be sent to '%s'\n", argv[4]);
09448 } else {
09449
09450 ast_mutex_lock(&pridebugfdlock);
09451 close(pridebugfd);
09452 pridebugfd = -1;
09453 ast_cli(fd, "PRI debug output to file disabled\n");
09454 ast_mutex_unlock(&pridebugfdlock);
09455 }
09456
09457 return RESULT_SUCCESS;
09458 }
09459
09460 static int handle_pri_debug(int fd, int argc, char *argv[])
09461 {
09462 int span;
09463 int x;
09464 if (argc < 4) {
09465 return RESULT_SHOWUSAGE;
09466 }
09467 span = atoi(argv[3]);
09468 if ((span < 1) || (span > NUM_SPANS)) {
09469 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[3], 1, NUM_SPANS);
09470 return RESULT_SUCCESS;
09471 }
09472 if (!pris[span-1].pri) {
09473 ast_cli(fd, "No PRI running on span %d\n", span);
09474 return RESULT_SUCCESS;
09475 }
09476 for (x = 0; x < NUM_DCHANS; x++) {
09477 if (pris[span-1].dchans[x])
09478 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU |
09479 PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE |
09480 PRI_DEBUG_Q921_STATE);
09481 }
09482 ast_cli(fd, "Enabled debugging on span %d\n", span);
09483 return RESULT_SUCCESS;
09484 }
09485
09486
09487
09488 static int handle_pri_no_debug(int fd, int argc, char *argv[])
09489 {
09490 int span;
09491 int x;
09492 if (argc < 5)
09493 return RESULT_SHOWUSAGE;
09494 span = atoi(argv[4]);
09495 if ((span < 1) || (span > NUM_SPANS)) {
09496 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09497 return RESULT_SUCCESS;
09498 }
09499 if (!pris[span-1].pri) {
09500 ast_cli(fd, "No PRI running on span %d\n", span);
09501 return RESULT_SUCCESS;
09502 }
09503 for (x = 0; x < NUM_DCHANS; x++) {
09504 if (pris[span-1].dchans[x])
09505 pri_set_debug(pris[span-1].dchans[x], 0);
09506 }
09507 ast_cli(fd, "Disabled debugging on span %d\n", span);
09508 return RESULT_SUCCESS;
09509 }
09510
09511 static int handle_pri_really_debug(int fd, int argc, char *argv[])
09512 {
09513 int span;
09514 int x;
09515 if (argc < 5)
09516 return RESULT_SHOWUSAGE;
09517 span = atoi(argv[4]);
09518 if ((span < 1) || (span > NUM_SPANS)) {
09519 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09520 return RESULT_SUCCESS;
09521 }
09522 if (!pris[span-1].pri) {
09523 ast_cli(fd, "No PRI running on span %d\n", span);
09524 return RESULT_SUCCESS;
09525 }
09526 for (x = 0; x < NUM_DCHANS; x++) {
09527 if (pris[span-1].dchans[x])
09528 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU |
09529 PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE |
09530 PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_STATE);
09531 }
09532 ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span);
09533 return RESULT_SUCCESS;
09534 }
09535
09536 static void build_status(char *s, size_t len, int status, int active)
09537 {
09538 if (!s || len < 1) {
09539 return;
09540 }
09541 s[0] = '\0';
09542 if (status & DCHAN_PROVISIONED)
09543 strncat(s, "Provisioned, ", len - strlen(s) - 1);
09544 if (!(status & DCHAN_NOTINALARM))
09545 strncat(s, "In Alarm, ", len - strlen(s) - 1);
09546 if (status & DCHAN_UP)
09547 strncat(s, "Up", len - strlen(s) - 1);
09548 else
09549 strncat(s, "Down", len - strlen(s) - 1);
09550 if (active)
09551 strncat(s, ", Active", len - strlen(s) - 1);
09552 else
09553 strncat(s, ", Standby", len - strlen(s) - 1);
09554 s[len - 1] = '\0';
09555 }
09556
09557 static int handle_pri_show_spans(int fd, int argc, char *argv[])
09558 {
09559 int span;
09560 int x;
09561 char status[256];
09562 if (argc != 3)
09563 return RESULT_SHOWUSAGE;
09564
09565 for (span = 0; span < NUM_SPANS; span++) {
09566 if (pris[span].pri) {
09567 for (x = 0; x < NUM_DCHANS; x++) {
09568 if (pris[span].dchannels[x]) {
09569 build_status(status, sizeof(status), pris[span].dchanavail[x], pris[span].dchans[x] == pris[span].pri);
09570 ast_cli(fd, "PRI span %d/%d: %s\n", span + 1, x, status);
09571 }
09572 }
09573 }
09574 }
09575 return RESULT_SUCCESS;
09576 }
09577
09578 static int handle_pri_show_span(int fd, int argc, char *argv[])
09579 {
09580 int span;
09581 int x;
09582 char status[256];
09583 if (argc < 4)
09584 return RESULT_SHOWUSAGE;
09585 span = atoi(argv[3]);
09586 if ((span < 1) || (span > NUM_SPANS)) {
09587 ast_cli(fd, "Invalid span '%s'. Should be a number from %d to %d\n", argv[3], 1, NUM_SPANS);
09588 return RESULT_SUCCESS;
09589 }
09590 if (!pris[span-1].pri) {
09591 ast_cli(fd, "No PRI running on span %d\n", span);
09592 return RESULT_SUCCESS;
09593 }
09594 for (x = 0; x < NUM_DCHANS; x++) {
09595 if (pris[span-1].dchannels[x]) {
09596 #ifdef PRI_DUMP_INFO_STR
09597 char *info_str = NULL;
09598 #endif
09599 ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
09600 build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
09601 ast_cli(fd, "Status: %s\n", status);
09602 #ifdef PRI_DUMP_INFO_STR
09603 info_str = pri_dump_info_str(pris[span-1].pri);
09604 if (info_str) {
09605 ast_cli(fd, "%s", info_str);
09606 free(info_str);
09607 }
09608 #else
09609 pri_dump_info(pris[span-1].pri);
09610 #endif
09611 ast_cli(fd, "\n");
09612 }
09613 }
09614 return RESULT_SUCCESS;
09615 }
09616
09617 static int handle_pri_show_debug(int fd, int argc, char *argv[])
09618 {
09619 int x;
09620 int span;
09621 int count=0;
09622 int debug=0;
09623
09624 for (span = 0; span < NUM_SPANS; span++) {
09625 if (pris[span].pri) {
09626 for (x = 0; x < NUM_DCHANS; x++) {
09627 debug = 0;
09628 if (pris[span].dchans[x]) {
09629 debug = pri_get_debug(pris[span].dchans[x]);
09630 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" );
09631 count++;
09632 }
09633 }
09634 }
09635
09636 }
09637 ast_mutex_lock(&pridebugfdlock);
09638 if (pridebugfd >= 0)
09639 ast_cli(fd, "Logging PRI debug to file %s\n", pridebugfilename);
09640 ast_mutex_unlock(&pridebugfdlock);
09641
09642 if (!count)
09643 ast_cli(fd, "No debug set or no PRI running\n");
09644 return RESULT_SUCCESS;
09645 }
09646
09647 static const char pri_debug_help[] =
09648 "Usage: pri debug span <span>\n"
09649 " Enables debugging on a given PRI span\n";
09650
09651 static const char pri_no_debug_help[] =
09652 "Usage: pri no debug span <span>\n"
09653 " Disables debugging on a given PRI span\n";
09654
09655 static const char pri_really_debug_help[] =
09656 "Usage: pri intensive debug span <span>\n"
09657 " Enables debugging down to the Q.921 level\n";
09658
09659 static const char pri_show_span_help[] =
09660 "Usage: pri show span <span>\n"
09661 " Displays PRI Information on a given PRI span\n";
09662
09663 static const char pri_show_spans_help[] =
09664 "Usage: pri show spans\n"
09665 " Displays PRI Information\n";
09666
09667 static struct ast_cli_entry zap_pri_cli[] = {
09668 { { "pri", "debug", "span", NULL },
09669 handle_pri_debug, "Enables PRI debugging on a span",
09670 pri_debug_help, complete_span_4 },
09671
09672 { { "pri", "no", "debug", "span", NULL },
09673 handle_pri_no_debug, "Disables PRI debugging on a span",
09674 pri_no_debug_help, complete_span_5 },
09675
09676 { { "pri", "intense", "debug", "span", NULL },
09677 handle_pri_really_debug, "Enables REALLY INTENSE PRI debugging",
09678 pri_really_debug_help, complete_span_5 },
09679
09680 { { "pri", "show", "spans", NULL },
09681 handle_pri_show_spans, "Displays PRI Information",
09682 pri_show_spans_help },
09683
09684 { { "pri", "show", "span", NULL },
09685 handle_pri_show_span, "Displays PRI Information",
09686 pri_show_span_help, complete_span_4 },
09687
09688 { { "pri", "show", "debug", NULL },
09689 handle_pri_show_debug, "Displays current PRI debug settings" },
09690
09691 { { "pri", "set", "debug", "file", NULL },
09692 handle_pri_set_debug_file, "Sends PRI debug output to the specified file" },
09693
09694 { { "pri", "unset", "debug", "file", NULL },
09695 handle_pri_set_debug_file, "Ends PRI debug output to file" },
09696 };
09697
09698 #endif
09699
09700 static int zap_destroy_channel(int fd, int argc, char **argv)
09701 {
09702 int channel;
09703
09704 if (argc != 4)
09705 return RESULT_SHOWUSAGE;
09706
09707 channel = atoi(argv[3]);
09708
09709 return zap_destroy_channel_bynum(channel);
09710 }
09711
09712 static int setup_zap(int reload);
09713 static int zap_restart(void)
09714 {
09715 if (option_verbose > 0)
09716 ast_verbose(VERBOSE_PREFIX_1 "Destroying channels and reloading zaptel configuration.\n");
09717 while (iflist) {
09718 if (option_debug)
09719 ast_log(LOG_DEBUG, "Destroying zaptel channel no. %d\n", iflist->channel);
09720
09721 destroy_channel(NULL, iflist, 1);
09722 }
09723 if (option_debug)
09724 ast_log(LOG_DEBUG, "Channels destroyed. Now re-reading config.\n");
09725 if (setup_zap(0) != 0) {
09726 ast_log(LOG_WARNING, "Reload channels from zap config failed!\n");
09727 return 1;
09728 }
09729 return 0;
09730 }
09731
09732 static int zap_restart_cmd(int fd, int argc, char **argv)
09733 {
09734 if (argc != 2) {
09735 return RESULT_SHOWUSAGE;
09736 }
09737
09738 if (zap_restart() != 0)
09739 return RESULT_FAILURE;
09740 return RESULT_SUCCESS;
09741 }
09742
09743 static int action_zaprestart(struct mansession *s, const struct message *m)
09744 {
09745 if (zap_restart() != 0) {
09746 astman_send_error(s, m, "Failed rereading zaptel configuration");
09747 return 1;
09748 }
09749 astman_send_ack(s, m, "ZapRestart: Success");
09750 return 0;
09751 }
09752
09753 static int zap_show_channels(int fd, int argc, char **argv)
09754 {
09755 #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09756 #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09757 struct zt_pvt *tmp = NULL;
09758 char tmps[20] = "";
09759 ast_mutex_t *lock;
09760 struct zt_pvt *start;
09761 #ifdef HAVE_PRI
09762 int trunkgroup;
09763 struct zt_pri *pri = NULL;
09764 int x;
09765 #endif
09766
09767 lock = &iflock;
09768 start = iflist;
09769
09770 #ifdef HAVE_PRI
09771 if (argc == 4) {
09772 if ((trunkgroup = atoi(argv[3])) < 1)
09773 return RESULT_SHOWUSAGE;
09774 for (x = 0; x < NUM_SPANS; x++) {
09775 if (pris[x].trunkgroup == trunkgroup) {
09776 pri = pris + x;
09777 break;
09778 }
09779 }
09780 if (pri) {
09781 start = pri->crvs;
09782 lock = &pri->lock;
09783 } else {
09784 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09785 return RESULT_FAILURE;
09786 }
09787 } else
09788 #endif
09789 if (argc != 3)
09790 return RESULT_SHOWUSAGE;
09791
09792 ast_mutex_lock(lock);
09793 #ifdef HAVE_PRI
09794 ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MOH Interpret");
09795 #else
09796 ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MOH Interpret");
09797 #endif
09798
09799 tmp = start;
09800 while (tmp) {
09801 if (tmp->channel > 0) {
09802 snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
09803 } else
09804 ast_copy_string(tmps, "pseudo", sizeof(tmps));
09805 ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->mohinterpret);
09806 tmp = tmp->next;
09807 }
09808 ast_mutex_unlock(lock);
09809 return RESULT_SUCCESS;
09810 #undef FORMAT
09811 #undef FORMAT2
09812 }
09813
09814 static int zap_show_channel(int fd, int argc, char **argv)
09815 {
09816 int channel;
09817 struct zt_pvt *tmp = NULL;
09818 ZT_CONFINFO ci;
09819 ZT_PARAMS ps;
09820 int x;
09821 ast_mutex_t *lock;
09822 struct zt_pvt *start;
09823 #ifdef HAVE_PRI
09824 char *c;
09825 int trunkgroup;
09826 struct zt_pri *pri=NULL;
09827 #endif
09828
09829 lock = &iflock;
09830 start = iflist;
09831
09832 if (argc != 4)
09833 return RESULT_SHOWUSAGE;
09834 #ifdef HAVE_PRI
09835 if ((c = strchr(argv[3], ':'))) {
09836 if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2)
09837 return RESULT_SHOWUSAGE;
09838 if ((trunkgroup < 1) || (channel < 1))
09839 return RESULT_SHOWUSAGE;
09840 for (x = 0; x < NUM_SPANS; x++) {
09841 if (pris[x].trunkgroup == trunkgroup) {
09842 pri = pris + x;
09843 break;
09844 }
09845 }
09846 if (pri) {
09847 start = pri->crvs;
09848 lock = &pri->lock;
09849 } else {
09850 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09851 return RESULT_FAILURE;
09852 }
09853 } else
09854 #endif
09855 channel = atoi(argv[3]);
09856
09857 ast_mutex_lock(lock);
09858 tmp = start;
09859 while (tmp) {
09860 if (tmp->channel == channel) {
09861 #ifdef HAVE_PRI
09862 if (pri)
09863 ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
09864 else
09865 #endif
09866 ast_cli(fd, "Channel: %d\n", tmp->channel);
09867 ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].zfd);
09868 ast_cli(fd, "Span: %d\n", tmp->span);
09869 ast_cli(fd, "Extension: %s\n", tmp->exten);
09870 ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
09871 ast_cli(fd, "Context: %s\n", tmp->context);
09872 ast_cli(fd, "Caller ID: %s\n", tmp->cid_num);
09873 ast_cli(fd, "Calling TON: %d\n", tmp->cid_ton);
09874 ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name);
09875 ast_cli(fd, "Destroy: %d\n", tmp->destroy);
09876 ast_cli(fd, "InAlarm: %d\n", tmp->inalarm);
09877 ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig));
09878 ast_cli(fd, "Radio: %d\n", tmp->radio);
09879 ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
09880 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)" : "");
09881 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)" : "");
09882 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)" : "");
09883 ast_cli(fd, "Confno: %d\n", tmp->confno);
09884 ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno);
09885 ast_cli(fd, "Real in conference: %d\n", tmp->inconference);
09886 ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
09887 ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
09888 ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
09889 ast_cli(fd, "Default law: %s\n", tmp->law == ZT_LAW_MULAW ? "ulaw" : tmp->law == ZT_LAW_ALAW ? "alaw" : "unknown");
09890 ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
09891 ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
09892 ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF");
09893 if (tmp->master)
09894 ast_cli(fd, "Master Channel: %d\n", tmp->master->channel);
09895 for (x = 0; x < MAX_SLAVES; x++) {
09896 if (tmp->slaves[x])
09897 ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
09898 }
09899 #ifdef HAVE_PRI
09900 if (tmp->pri) {
09901 ast_cli(fd, "PRI Flags: ");
09902 if (tmp->resetting)
09903 ast_cli(fd, "Resetting ");
09904 if (tmp->call)
09905 ast_cli(fd, "Call ");
09906 if (tmp->bearer)
09907 ast_cli(fd, "Bearer ");
09908 ast_cli(fd, "\n");
09909 if (tmp->logicalspan)
09910 ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan);
09911 else
09912 ast_cli(fd, "PRI Logical Span: Implicit\n");
09913 }
09914
09915 #endif
09916 memset(&ci, 0, sizeof(ci));
09917 ps.channo = tmp->channel;
09918 if (tmp->subs[SUB_REAL].zfd > -1) {
09919 if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
09920 ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
09921 }
09922 #ifdef ZT_GETCONFMUTE
09923 if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONFMUTE, &x)) {
09924 ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
09925 }
09926 #endif
09927 if (ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
09928 ast_log(LOG_WARNING, "Failed to get parameters on channel %d\n", tmp->channel);
09929 } else {
09930 ast_cli(fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
09931 }
09932 }
09933 ast_mutex_unlock(lock);
09934 return RESULT_SUCCESS;
09935 }
09936 tmp = tmp->next;
09937 }
09938
09939 ast_cli(fd, "Unable to find given channel %d\n", channel);
09940 ast_mutex_unlock(lock);
09941 return RESULT_FAILURE;
09942 }
09943
09944 static char zap_show_cadences_help[] =
09945 "Usage: zap show cadences\n"
09946 " Shows all cadences currently defined\n";
09947
09948 static int handle_zap_show_cadences(int fd, int argc, char *argv[])
09949 {
09950 int i, j;
09951 for (i = 0; i < num_cadence; i++) {
09952 char output[1024];
09953 char tmp[16], tmp2[64];
09954 snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
09955 term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
09956
09957 for (j = 0; j < 16; j++) {
09958 if (cadences[i].ringcadence[j] == 0)
09959 break;
09960 snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
09961 if (cidrings[i] * 2 - 1 == j)
09962 term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
09963 else
09964 term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
09965 if (j != 0)
09966 strncat(output, ",", sizeof(output) - strlen(output) - 1);
09967 strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
09968 }
09969 ast_cli(fd,"%s\n",output);
09970 }
09971 return 0;
09972 }
09973
09974
09975 static int zap_show_status(int fd, int argc, char *argv[]) {
09976 #define FORMAT "%-40.40s %-10.10s %-10d %-10d %-10d\n"
09977 #define FORMAT2 "%-40.40s %-10.10s %-10.10s %-10.10s %-10.10s\n"
09978
09979 int span;
09980 int res;
09981 char alarms[50];
09982
09983 int ctl;
09984 ZT_SPANINFO s;
09985
09986 ctl = open("/dev/zap/ctl", O_RDWR);
09987 if (ctl < 0) {
09988 ast_log(LOG_WARNING, "Unable to open /dev/zap/ctl: %s\n", strerror(errno));
09989 ast_cli(fd, "No Zaptel interface found.\n");
09990 return RESULT_FAILURE;
09991 }
09992 ast_cli(fd, FORMAT2, "Description", "Alarms", "IRQ", "bpviol", "CRC4");
09993
09994 for (span = 1; span < ZT_MAX_SPANS; ++span) {
09995 s.spanno = span;
09996 res = ioctl(ctl, ZT_SPANSTAT, &s);
09997 if (res) {
09998 continue;
09999 }
10000 alarms[0] = '\0';
10001 if (s.alarms > 0) {
10002 if (s.alarms & ZT_ALARM_BLUE)
10003 strcat(alarms, "BLU/");
10004 if (s.alarms & ZT_ALARM_YELLOW)
10005 strcat(alarms, "YEL/");
10006 if (s.alarms & ZT_ALARM_RED)
10007 strcat(alarms, "RED/");
10008 if (s.alarms & ZT_ALARM_LOOPBACK)
10009 strcat(alarms, "LB/");
10010 if (s.alarms & ZT_ALARM_RECOVER)
10011 strcat(alarms, "REC/");
10012 if (s.alarms & ZT_ALARM_NOTOPEN)
10013 strcat(alarms, "NOP/");
10014 if (!strlen(alarms))
10015 strcat(alarms, "UUU/");
10016 if (strlen(alarms)) {
10017
10018 alarms[strlen(alarms) - 1] = '\0';
10019 }
10020 } else {
10021 if (s.numchans)
10022 strcpy(alarms, "OK");
10023 else
10024 strcpy(alarms, "UNCONFIGURED");
10025 }
10026
10027 ast_cli(fd, FORMAT, s.desc, alarms, s.irqmisses, s.bpvcount, s.crc4count);
10028 }
10029 close(ctl);
10030
10031 return RESULT_SUCCESS;
10032 #undef FORMAT
10033 #undef FORMAT2
10034 }
10035
10036 static char show_channels_usage[] =
10037 "Usage: zap show channels\n"
10038 " Shows a list of available channels\n";
10039
10040 static char show_channel_usage[] =
10041 "Usage: zap show channel <chan num>\n"
10042 " Detailed information about a given channel\n";
10043
10044 static char zap_show_status_usage[] =
10045 "Usage: zap show status\n"
10046 " Shows a list of Zaptel cards with status\n";
10047
10048 static char destroy_channel_usage[] =
10049 "Usage: zap destroy channel <chan num>\n"
10050 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n";
10051
10052 static char zap_restart_usage[] =
10053 "Usage: zap restart\n"
10054 " Restarts the zaptel channels: destroys them all and then\n"
10055 " re-reads them from zapata.conf.\n"
10056 " Note that this will STOP any running CALL on zaptel channels.\n"
10057 "";
10058
10059 static struct ast_cli_entry zap_cli[] = {
10060 { { "zap", "show", "cadences", NULL },
10061 handle_zap_show_cadences, "List cadences",
10062 zap_show_cadences_help },
10063
10064 { { "zap", "show", "channels", NULL},
10065 zap_show_channels, "Show active zapata channels",
10066 show_channels_usage },
10067
10068 { { "zap", "show", "channel", NULL},
10069 zap_show_channel, "Show information on a channel",
10070 show_channel_usage },
10071
10072 { { "zap", "destroy", "channel", NULL},
10073 zap_destroy_channel, "Destroy a channel",
10074 destroy_channel_usage },
10075
10076 { { "zap", "restart", NULL},
10077 zap_restart_cmd, "Fully restart zaptel channels",
10078 zap_restart_usage },
10079
10080 { { "zap", "show", "status", NULL},
10081 zap_show_status, "Show all Zaptel cards status",
10082 zap_show_status_usage },
10083 };
10084
10085 #define TRANSFER 0
10086 #define HANGUP 1
10087
10088 static int zap_fake_event(struct zt_pvt *p, int mode)
10089 {
10090 if (p) {
10091 switch (mode) {
10092 case TRANSFER:
10093 p->fake_event = ZT_EVENT_WINKFLASH;
10094 break;
10095 case HANGUP:
10096 p->fake_event = ZT_EVENT_ONHOOK;
10097 break;
10098 default:
10099 ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);
10100 }
10101 }
10102 return 0;
10103 }
10104 static struct zt_pvt *find_channel(int channel)
10105 {
10106 struct zt_pvt *p = iflist;
10107 while (p) {
10108 if (p->channel == channel) {
10109 break;
10110 }
10111 p = p->next;
10112 }
10113 return p;
10114 }
10115
10116 static int action_zapdndon(struct mansession *s, const struct message *m)
10117 {
10118 struct zt_pvt *p = NULL;
10119 const char *channel = astman_get_header(m, "ZapChannel");
10120
10121 if (ast_strlen_zero(channel)) {
10122 astman_send_error(s, m, "No channel specified");
10123 return 0;
10124 }
10125 p = find_channel(atoi(channel));
10126 if (!p) {
10127 astman_send_error(s, m, "No such channel");
10128 return 0;
10129 }
10130 p->dnd = 1;
10131 astman_send_ack(s, m, "DND Enabled");
10132 return 0;
10133 }
10134
10135 static int action_zapdndoff(struct mansession *s, const struct message *m)
10136 {
10137 struct zt_pvt *p = NULL;
10138 const char *channel = astman_get_header(m, "ZapChannel");
10139
10140 if (ast_strlen_zero(channel)) {
10141 astman_send_error(s, m, "No channel specified");
10142 return 0;
10143 }
10144 p = find_channel(atoi(channel));
10145 if (!p) {
10146 astman_send_error(s, m, "No such channel");
10147 return 0;
10148 }
10149 p->dnd = 0;
10150 astman_send_ack(s, m, "DND Disabled");
10151 return 0;
10152 }
10153
10154 static int action_transfer(struct mansession *s, const struct message *m)
10155 {
10156 struct zt_pvt *p = NULL;
10157 const char *channel = astman_get_header(m, "ZapChannel");
10158
10159 if (ast_strlen_zero(channel)) {
10160 astman_send_error(s, m, "No channel specified");
10161 return 0;
10162 }
10163 p = find_channel(atoi(channel));
10164 if (!p) {
10165 astman_send_error(s, m, "No such channel");
10166 return 0;
10167 }
10168 zap_fake_event(p,TRANSFER);
10169 astman_send_ack(s, m, "ZapTransfer");
10170 return 0;
10171 }
10172
10173 static int action_transferhangup(struct mansession *s, const struct message *m)
10174 {
10175 struct zt_pvt *p = NULL;
10176 const char *channel = astman_get_header(m, "ZapChannel");
10177
10178 if (ast_strlen_zero(channel)) {
10179 astman_send_error(s, m, "No channel specified");
10180 return 0;
10181 }
10182 p = find_channel(atoi(channel));
10183 if (!p) {
10184 astman_send_error(s, m, "No such channel");
10185 return 0;
10186 }
10187 zap_fake_event(p,HANGUP);
10188 astman_send_ack(s, m, "ZapHangup");
10189 return 0;
10190 }
10191
10192 static int action_zapdialoffhook(struct mansession *s, const struct message *m)
10193 {
10194 struct zt_pvt *p = NULL;
10195 const char *channel = astman_get_header(m, "ZapChannel");
10196 const char *number = astman_get_header(m, "Number");
10197 int i;
10198
10199 if (ast_strlen_zero(channel)) {
10200 astman_send_error(s, m, "No channel specified");
10201 return 0;
10202 }
10203 if (ast_strlen_zero(number)) {
10204 astman_send_error(s, m, "No number specified");
10205 return 0;
10206 }
10207 p = find_channel(atoi(channel));
10208 if (!p) {
10209 astman_send_error(s, m, "No such channel");
10210 return 0;
10211 }
10212 if (!p->owner) {
10213 astman_send_error(s, m, "Channel does not have it's owner");
10214 return 0;
10215 }
10216 for (i = 0; i < strlen(number); i++) {
10217 struct ast_frame f = { AST_FRAME_DTMF, number[i] };
10218 zap_queue_frame(p, &f, NULL);
10219 }
10220 astman_send_ack(s, m, "ZapDialOffhook");
10221 return 0;
10222 }
10223
10224 static int action_zapshowchannels(struct mansession *s, const struct message *m)
10225 {
10226 struct zt_pvt *tmp = NULL;
10227 const char *id = astman_get_header(m, "ActionID");
10228 char idText[256] = "";
10229
10230 astman_send_ack(s, m, "Zapata channel status will follow");
10231 if (!ast_strlen_zero(id))
10232 snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id);
10233
10234 ast_mutex_lock(&iflock);
10235
10236 tmp = iflist;
10237 while (tmp) {
10238 if (tmp->channel > 0) {
10239 int alarm = get_alarms(tmp);
10240 astman_append(s,
10241 "Event: ZapShowChannels\r\n"
10242 "Channel: %d\r\n"
10243 "Signalling: %s\r\n"
10244 "Context: %s\r\n"
10245 "DND: %s\r\n"
10246 "Alarm: %s\r\n"
10247 "%s"
10248 "\r\n",
10249 tmp->channel, sig2str(tmp->sig), tmp->context,
10250 tmp->dnd ? "Enabled" : "Disabled",
10251 alarm2str(alarm), idText);
10252 }
10253
10254 tmp = tmp->next;
10255 }
10256
10257 ast_mutex_unlock(&iflock);
10258
10259 astman_append(s,
10260 "Event: ZapShowChannelsComplete\r\n"
10261 "%s"
10262 "\r\n",
10263 idText);
10264 return 0;
10265 }
10266
10267 static int __unload_module(void)
10268 {
10269 int x = 0;
10270 struct zt_pvt *p, *pl;
10271 #ifdef HAVE_PRI
10272 int i;
10273 for (i = 0; i < NUM_SPANS; i++) {
10274 if (pris[i].master != AST_PTHREADT_NULL)
10275 pthread_cancel(pris[i].master);
10276 }
10277 ast_cli_unregister_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(struct ast_cli_entry));
10278 ast_unregister_application(zap_send_keypad_facility_app);
10279 #endif
10280 ast_cli_unregister_multiple(zap_cli, sizeof(zap_cli) / sizeof(struct ast_cli_entry));
10281 ast_manager_unregister( "ZapDialOffhook" );
10282 ast_manager_unregister( "ZapHangup" );
10283 ast_manager_unregister( "ZapTransfer" );
10284 ast_manager_unregister( "ZapDNDoff" );
10285 ast_manager_unregister( "ZapDNDon" );
10286 ast_manager_unregister("ZapShowChannels");
10287 ast_manager_unregister("ZapRestart");
10288 ast_channel_unregister(&zap_tech);
10289 ast_mutex_lock(&iflock);
10290
10291 p = iflist;
10292 while (p) {
10293 if (p->owner)
10294 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
10295 p = p->next;
10296 }
10297 ast_mutex_unlock(&iflock);
10298 ast_mutex_lock(&monlock);
10299 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
10300 pthread_cancel(monitor_thread);
10301 pthread_kill(monitor_thread, SIGURG);
10302 pthread_join(monitor_thread, NULL);
10303 }
10304 monitor_thread = AST_PTHREADT_STOP;
10305 ast_mutex_unlock(&monlock);
10306
10307 ast_mutex_lock(&iflock);
10308
10309 p = iflist;
10310 while (p) {
10311
10312 if (p->cidspill)
10313 free(p->cidspill);
10314
10315 if (p->subs[SUB_REAL].zfd > -1)
10316 zt_close(p->subs[SUB_REAL].zfd);
10317 pl = p;
10318 p = p->next;
10319 x++;
10320
10321 if (pl)
10322 destroy_zt_pvt(&pl);
10323 ast_verbose(VERBOSE_PREFIX_3 "Unregistered channel %d\n", x);
10324 }
10325 iflist = NULL;
10326 ifcount = 0;
10327 ast_mutex_unlock(&iflock);
10328 #ifdef HAVE_PRI
10329 for (i = 0; i < NUM_SPANS; i++) {
10330 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
10331 pthread_join(pris[i].master, NULL);
10332 zt_close(pris[i].fds[i]);
10333 }
10334 #endif
10335 return 0;
10336 }
10337
10338 static int unload_module(void)
10339 {
10340 #ifdef HAVE_PRI
10341 int y;
10342 for (y = 0; y < NUM_SPANS; y++)
10343 ast_mutex_destroy(&pris[y].lock);
10344 #endif
10345 return __unload_module();
10346 }
10347
10348 static int build_channels(struct zt_chan_conf conf, int iscrv, const char *value, int reload, int lineno, int *found_pseudo)
10349 {
10350 char *c, *chan;
10351 int x, start, finish;
10352 struct zt_pvt *tmp;
10353 #ifdef HAVE_PRI
10354 struct zt_pri *pri;
10355 int trunkgroup, y;
10356 #endif
10357
10358 if ((reload == 0) && (conf.chan.sig < 0)) {
10359 ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
10360 return -1;
10361 }
10362
10363 c = ast_strdupa(value);
10364
10365 #ifdef HAVE_PRI
10366 pri = NULL;
10367 if (iscrv) {
10368 if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) {
10369 ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", lineno);
10370 return -1;
10371 }
10372 if (trunkgroup < 1) {
10373 ast_log(LOG_WARNING, "CRV trunk group must be a positive number at line %d\n", lineno);
10374 return -1;
10375 }
10376 c += y;
10377 for (y = 0; y < NUM_SPANS; y++) {
10378 if (pris[y].trunkgroup == trunkgroup) {
10379 pri = pris + y;
10380 break;
10381 }
10382 }
10383 if (!pri) {
10384 ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, lineno);
10385 return -1;
10386 }
10387 }
10388 #endif
10389
10390 while ((chan = strsep(&c, ","))) {
10391 if (sscanf(chan, "%d-%d", &start, &finish) == 2) {
10392
10393 } else if (sscanf(chan, "%d", &start)) {
10394
10395 finish = start;
10396 } else if (!strcasecmp(chan, "pseudo")) {
10397 finish = start = CHAN_PSEUDO;
10398 if (found_pseudo)
10399 *found_pseudo = 1;
10400 } else {
10401 ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", value, chan);
10402 return -1;
10403 }
10404 if (finish < start) {
10405 ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
10406 x = finish;
10407 finish = start;
10408 start = x;
10409 }
10410
10411 for (x = start; x <= finish; x++) {
10412 #ifdef HAVE_PRI
10413 tmp = mkintf(x, conf, pri, reload);
10414 #else
10415 tmp = mkintf(x, conf, NULL, reload);
10416 #endif
10417
10418 if (tmp) {
10419 if (option_verbose > 2) {
10420 #ifdef HAVE_PRI
10421 if (pri)
10422 ast_verbose(VERBOSE_PREFIX_3 "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup, x, sig2str(tmp->sig));
10423 else
10424 #endif
10425 ast_verbose(VERBOSE_PREFIX_3 "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig));
10426 }
10427 } else {
10428 ast_log(LOG_ERROR, "Unable to %s channel '%s'\n",
10429 (reload == 1) ? "reconfigure" : "register", value);
10430 return -1;
10431 }
10432 }
10433 }
10434
10435 return 0;
10436 }
10437
10438
10439
10440 #define MAX_CHANLIST_LEN 80
10441 static int process_zap(struct zt_chan_conf *confp, struct ast_variable *v, int reload, int skipchannels)
10442 {
10443 struct zt_pvt *tmp;
10444 char *ringc;
10445 int y;
10446 int found_pseudo = 0;
10447 char zapchan[MAX_CHANLIST_LEN] = {};
10448
10449 for (; v; v = v->next) {
10450 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
10451 continue;
10452
10453
10454 if (!strcasecmp(v->name, "channel")
10455 #ifdef HAVE_PRI
10456 || !strcasecmp(v->name, "crv")
10457 #endif
10458 ) {
10459 if (skipchannels)
10460 continue;
10461 int iscrv = !strcasecmp(v->name, "crv");
10462 if (build_channels(*confp, iscrv, v->value, reload, v->lineno, &found_pseudo))
10463 return -1;
10464 } else if (!strcasecmp(v->name, "zapchan")) {
10465 ast_copy_string(zapchan, v->value, sizeof(zapchan));
10466 } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
10467 if (ast_true(v->value))
10468 confp->chan.usedistinctiveringdetection = 1;
10469 } else if (!strcasecmp(v->name, "distinctiveringaftercid")) {
10470 if (ast_true(v->value))
10471 distinctiveringaftercid = 1;
10472 } else if (!strcasecmp(v->name, "dring1context")) {
10473 ast_copy_string(confp->chan.drings.ringContext[0].contextData,v->value,sizeof(confp->chan.drings.ringContext[0].contextData));
10474 } else if (!strcasecmp(v->name, "dring2context")) {
10475 ast_copy_string(confp->chan.drings.ringContext[1].contextData,v->value,sizeof(confp->chan.drings.ringContext[1].contextData));
10476 } else if (!strcasecmp(v->name, "dring3context")) {
10477 ast_copy_string(confp->chan.drings.ringContext[2].contextData,v->value,sizeof(confp->chan.drings.ringContext[2].contextData));
10478 } else if (!strcasecmp(v->name, "dring1")) {
10479 ringc = v->value;
10480 sscanf(ringc, "%d,%d,%d", &confp->chan.drings.ringnum[0].ring[0], &confp->chan.drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]);
10481 } else if (!strcasecmp(v->name, "dring2")) {
10482 ringc = v->value;
10483 sscanf(ringc,"%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]);
10484 } else if (!strcasecmp(v->name, "dring3")) {
10485 ringc = v->value;
10486 sscanf(ringc, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]);
10487 } else if (!strcasecmp(v->name, "usecallerid")) {
10488 confp->chan.use_callerid = ast_true(v->value);
10489 } else if (!strcasecmp(v->name, "cidsignalling")) {
10490 if (!strcasecmp(v->value, "bell"))
10491 confp->chan.cid_signalling = CID_SIG_BELL;
10492 else if (!strcasecmp(v->value, "v23"))
10493 confp->chan.cid_signalling = CID_SIG_V23;
10494 else if (!strcasecmp(v->value, "dtmf"))
10495 confp->chan.cid_signalling = CID_SIG_DTMF;
10496 else if (!strcasecmp(v->value, "smdi"))
10497 confp->chan.cid_signalling = CID_SIG_SMDI;
10498 else if (!strcasecmp(v->value, "v23_jp"))
10499 confp->chan.cid_signalling = CID_SIG_V23_JP;
10500 else if (ast_true(v->value))
10501 confp->chan.cid_signalling = CID_SIG_BELL;
10502 } else if (!strcasecmp(v->name, "cidstart")) {
10503 if (!strcasecmp(v->value, "ring"))
10504 confp->chan.cid_start = CID_START_RING;
10505 else if (!strcasecmp(v->value, "polarity"))
10506 confp->chan.cid_start = CID_START_POLARITY;
10507 else if (ast_true(v->value))
10508 confp->chan.cid_start = CID_START_RING;
10509 } else if (!strcasecmp(v->name, "threewaycalling")) {
10510 confp->chan.threewaycalling = ast_true(v->value);
10511 } else if (!strcasecmp(v->name, "cancallforward")) {
10512 confp->chan.cancallforward = ast_true(v->value);
10513 } else if (!strcasecmp(v->name, "relaxdtmf")) {
10514 if (ast_true(v->value))
10515 confp->chan.dtmfrelax = DSP_DIGITMODE_RELAXDTMF;
10516 else
10517 confp->chan.dtmfrelax = 0;
10518 } else if (!strcasecmp(v->name, "mailbox")) {
10519 ast_copy_string(confp->chan.mailbox, v->value, sizeof(confp->chan.mailbox));
10520 } else if (!strcasecmp(v->name, "adsi")) {
10521 confp->chan.adsi = ast_true(v->value);
10522 } else if (!strcasecmp(v->name, "usesmdi")) {
10523 confp->chan.use_smdi = ast_true(v->value);
10524 } else if (!strcasecmp(v->name, "smdiport")) {
10525 ast_copy_string(confp->smdi_port, v->value, sizeof(confp->smdi_port));
10526 } else if (!strcasecmp(v->name, "transfer")) {
10527 confp->chan.transfer = ast_true(v->value);
10528 } else if (!strcasecmp(v->name, "canpark")) {
10529 confp->chan.canpark = ast_true(v->value);
10530 } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
10531 confp->chan.echocanbridged = ast_true(v->value);
10532 } else if (!strcasecmp(v->name, "busydetect")) {
10533 confp->chan.busydetect = ast_true(v->value);
10534 } else if (!strcasecmp(v->name, "busycount")) {
10535 confp->chan.busycount = atoi(v->value);
10536 } else if (!strcasecmp(v->name, "busypattern")) {
10537 if (sscanf(v->value, "%d,%d", &confp->chan.busy_tonelength, &confp->chan.busy_quietlength) != 2) {
10538 ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength\n");
10539 }
10540 } else if (!strcasecmp(v->name, "callprogress")) {
10541 if (ast_true(v->value))
10542 confp->chan.callprogress |= 1;
10543 else
10544 confp->chan.callprogress &= ~1;
10545 } else if (!strcasecmp(v->name, "faxdetect")) {
10546 if (!strcasecmp(v->value, "incoming")) {
10547 confp->chan.callprogress |= 4;
10548 confp->chan.callprogress &= ~2;
10549 } else if (!strcasecmp(v->value, "outgoing")) {
10550 confp->chan.callprogress &= ~4;
10551 confp->chan.callprogress |= 2;
10552 } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
10553 confp->chan.callprogress |= 6;
10554 else
10555 confp->chan.callprogress &= ~6;
10556 } else if (!strcasecmp(v->name, "echocancel")) {
10557 if (!ast_strlen_zero(v->value)) {
10558 y = atoi(v->value);
10559 } else
10560 y = 0;
10561 if ((y == 32) || (y == 64) || (y == 128) || (y == 256) || (y == 512) || (y == 1024))
10562 confp->chan.echocancel = y;
10563 else {
10564 confp->chan.echocancel = ast_true(v->value);
10565 if (confp->chan.echocancel)
10566 confp->chan.echocancel=128;
10567 }
10568 } else if (!strcasecmp(v->name, "echotraining")) {
10569 if (sscanf(v->value, "%d", &y) == 1) {
10570 if ((y < 10) || (y > 4000)) {
10571 ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 4000 ms at line %d\n", v->lineno);
10572 } else {
10573 confp->chan.echotraining = y;
10574 }
10575 } else if (ast_true(v->value)) {
10576 confp->chan.echotraining = 400;
10577 } else
10578 confp->chan.echotraining = 0;
10579 } else if (!strcasecmp(v->name, "hidecallerid")) {
10580 confp->chan.hidecallerid = ast_true(v->value);
10581 } else if (!strcasecmp(v->name, "hidecalleridname")) {
10582 confp->chan.hidecalleridname = ast_true(v->value);
10583 } else if (!strcasecmp(v->name, "pulsedial")) {
10584 confp->chan.pulse = ast_true(v->value);
10585 } else if (!strcasecmp(v->name, "callreturn")) {
10586 confp->chan.callreturn = ast_true(v->value);
10587 } else if (!strcasecmp(v->name, "callwaiting")) {
10588 confp->chan.callwaiting = ast_true(v->value);
10589 } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
10590 confp->chan.callwaitingcallerid = ast_true(v->value);
10591 } else if (!strcasecmp(v->name, "context")) {
10592 ast_copy_string(confp->chan.context, v->value, sizeof(confp->chan.context));
10593 } else if (!strcasecmp(v->name, "language")) {
10594 ast_copy_string(confp->chan.language, v->value, sizeof(confp->chan.language));
10595 } else if (!strcasecmp(v->name, "progzone")) {
10596 ast_copy_string(progzone, v->value, sizeof(progzone));
10597 } else if (!strcasecmp(v->name, "mohinterpret")
10598 ||!strcasecmp(v->name, "musiconhold") || !strcasecmp(v->name, "musicclass")) {
10599 ast_copy_string(confp->chan.mohinterpret, v->value, sizeof(confp->chan.mohinterpret));
10600 } else if (!strcasecmp(v->name, "mohsuggest")) {
10601 ast_copy_string(confp->chan.mohsuggest, v->value, sizeof(confp->chan.mohsuggest));
10602 } else if (!strcasecmp(v->name, "stripmsd")) {
10603 confp->chan.stripmsd = atoi(v->value);
10604 } else if (!strcasecmp(v->name, "jitterbuffers")) {
10605 numbufs = atoi(v->value);
10606 } else if (!strcasecmp(v->name, "group")) {
10607 confp->chan.group = ast_get_group(v->value);
10608 } else if (!strcasecmp(v->name, "callgroup")) {
10609 confp->chan.callgroup = ast_get_group(v->value);
10610 } else if (!strcasecmp(v->name, "pickupgroup")) {
10611 confp->chan.pickupgroup = ast_get_group(v->value);
10612 } else if (!strcasecmp(v->name, "immediate")) {
10613 confp->chan.immediate = ast_true(v->value);
10614 } else if (!strcasecmp(v->name, "transfertobusy")) {
10615 confp->chan.transfertobusy = ast_true(v->value);
10616 } else if (!strcasecmp(v->name, "rxgain")) {
10617 if (sscanf(v->value, "%f", &confp->chan.rxgain) != 1) {
10618 ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value);
10619 }
10620 } else if (!strcasecmp(v->name, "txgain")) {
10621 if (sscanf(v->value, "%f", &confp->chan.txgain) != 1) {
10622 ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value);
10623 }
10624 } else if (!strcasecmp(v->name, "tonezone")) {
10625 if (sscanf(v->value, "%d", &confp->chan.tonezone) != 1) {
10626 ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
10627 }
10628 } else if (!strcasecmp(v->name, "callerid")) {
10629 if (!strcasecmp(v->value, "asreceived")) {
10630 confp->chan.cid_num[0] = '\0';
10631 confp->chan.cid_name[0] = '\0';
10632 } else {
10633 ast_callerid_split(v->value, confp->chan.cid_name, sizeof(confp->chan.cid_name), confp->chan.cid_num, sizeof(confp->chan.cid_num));
10634 }
10635 } else if (!strcasecmp(v->name, "fullname")) {
10636 ast_copy_string(confp->chan.cid_name, v->value, sizeof(confp->chan.cid_name));
10637 } else if (!strcasecmp(v->name, "cid_number")) {
10638 ast_copy_string(confp->chan.cid_num, v->value, sizeof(confp->chan.cid_num));
10639 } else if (!strcasecmp(v->name, "useincomingcalleridonzaptransfer")) {
10640 confp->chan.zaptrcallerid = ast_true(v->value);
10641 } else if (!strcasecmp(v->name, "restrictcid")) {
10642 confp->chan.restrictcid = ast_true(v->value);
10643 } else if (!strcasecmp(v->name, "usecallingpres")) {
10644 confp->chan.use_callingpres = ast_true(v->value);
10645 } else if (!strcasecmp(v->name, "accountcode")) {
10646 ast_copy_string(confp->chan.accountcode, v->value, sizeof(confp->chan.accountcode));
10647 } else if (!strcasecmp(v->name, "amaflags")) {
10648 y = ast_cdr_amaflags2int(v->value);
10649 if (y < 0)
10650 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
10651 else
10652 confp->chan.amaflags = y;
10653 } else if (!reload){
10654 if (!strcasecmp(v->name, "signalling")) {
10655 confp->chan.outsigmod = -1;
10656 if (!strcasecmp(v->value, "em")) {
10657 confp->chan.sig = SIG_EM;
10658 } else if (!strcasecmp(v->value, "em_e1")) {
10659 confp->chan.sig = SIG_EM_E1;
10660 } else if (!strcasecmp(v->value, "em_w")) {
10661 confp->chan.sig = SIG_EMWINK;
10662 confp->chan.radio = 0;
10663 } else if (!strcasecmp(v->value, "fxs_ls")) {
10664 confp->chan.sig = SIG_FXSLS;
10665 confp->chan.radio = 0;
10666 } else if (!strcasecmp(v->value, "fxs_gs")) {
10667 confp->chan.sig = SIG_FXSGS;
10668 confp->chan.radio = 0;
10669 } else if (!strcasecmp(v->value, "fxs_ks")) {
10670 confp->chan.sig = SIG_FXSKS;
10671 confp->chan.radio = 0;
10672 } else if (!strcasecmp(v->value, "fxo_ls")) {
10673 confp->chan.sig = SIG_FXOLS;
10674 confp->chan.radio = 0;
10675 } else if (!strcasecmp(v->value, "fxo_gs")) {
10676 confp->chan.sig = SIG_FXOGS;
10677 confp->chan.radio = 0;
10678 } else if (!strcasecmp(v->value, "fxo_ks")) {
10679 confp->chan.sig = SIG_FXOKS;
10680 confp->chan.radio = 0;
10681 } else if (!strcasecmp(v->value, "fxs_rx")) {
10682 confp->chan.sig = SIG_FXSKS;
10683 confp->chan.radio = 1;
10684 } else if (!strcasecmp(v->value, "fxo_rx")) {
10685 confp->chan.sig = SIG_FXOLS;
10686 confp->chan.radio = 1;
10687 } else if (!strcasecmp(v->value, "fxs_tx")) {
10688 confp->chan.sig = SIG_FXSLS;
10689 confp->chan.radio = 1;
10690 } else if (!strcasecmp(v->value, "fxo_tx")) {
10691 confp->chan.sig = SIG_FXOGS;
10692 confp->chan.radio = 1;
10693 } else if (!strcasecmp(v->value, "em_rx")) {
10694 confp->chan.sig = SIG_EM;
10695 confp->chan.radio = 1;
10696 } else if (!strcasecmp(v->value, "em_tx")) {
10697 confp->chan.sig = SIG_EM;
10698 confp->chan.radio = 1;
10699 } else if (!strcasecmp(v->value, "em_rxtx")) {
10700 confp->chan.sig = SIG_EM;
10701 confp->chan.radio = 2;
10702 } else if (!strcasecmp(v->value, "em_txrx")) {
10703 confp->chan.sig = SIG_EM;
10704 confp->chan.radio = 2;
10705 } else if (!strcasecmp(v->value, "sf")) {
10706 confp->chan.sig = SIG_SF;
10707 confp->chan.radio = 0;
10708 } else if (!strcasecmp(v->value, "sf_w")) {
10709 confp->chan.sig = SIG_SFWINK;
10710 confp->chan.radio = 0;
10711 } else if (!strcasecmp(v->value, "sf_featd")) {
10712 confp->chan.sig = SIG_FEATD;
10713 confp->chan.radio = 0;
10714 } else if (!strcasecmp(v->value, "sf_featdmf")) {
10715 confp->chan.sig = SIG_FEATDMF;
10716 confp->chan.radio = 0;
10717 } else if (!strcasecmp(v->value, "sf_featb")) {
10718 confp->chan.sig = SIG_SF_FEATB;
10719 confp->chan.radio = 0;
10720 } else if (!strcasecmp(v->value, "sf")) {
10721 confp->chan.sig = SIG_SF;
10722 confp->chan.radio = 0;
10723 } else if (!strcasecmp(v->value, "sf_rx")) {
10724 confp->chan.sig = SIG_SF;
10725 confp->chan.radio = 1;
10726 } else if (!strcasecmp(v->value, "sf_tx")) {
10727 confp->chan.sig = SIG_SF;
10728 confp->chan.radio = 1;
10729 } else if (!strcasecmp(v->value, "sf_rxtx")) {
10730 confp->chan.sig = SIG_SF;
10731 confp->chan.radio = 2;
10732 } else if (!strcasecmp(v->value, "sf_txrx")) {
10733 confp->chan.sig = SIG_SF;
10734 confp->chan.radio = 2;
10735 } else if (!strcasecmp(v->value, "featd")) {
10736 confp->chan.sig = SIG_FEATD;
10737 confp->chan.radio = 0;
10738 } else if (!strcasecmp(v->value, "featdmf")) {
10739 confp->chan.sig = SIG_FEATDMF;
10740 confp->chan.radio = 0;
10741 } else if (!strcasecmp(v->value, "featdmf_ta")) {
10742 confp->chan.sig = SIG_FEATDMF_TA;
10743 confp->chan.radio = 0;
10744 } else if (!strcasecmp(v->value, "e911")) {
10745 confp->chan.sig = SIG_E911;
10746 confp->chan.radio = 0;
10747 } else if (!strcasecmp(v->value, "fgccama")) {
10748 confp->chan.sig = SIG_FGC_CAMA;
10749 confp->chan.radio = 0;
10750 } else if (!strcasecmp(v->value, "fgccamamf")) {
10751 confp->chan.sig = SIG_FGC_CAMAMF;
10752 confp->chan.radio = 0;
10753 } else if (!strcasecmp(v->value, "featb")) {
10754 confp->chan.sig = SIG_FEATB;
10755 confp->chan.radio = 0;
10756 #ifdef HAVE_PRI
10757 } else if (!strcasecmp(v->value, "pri_net")) {
10758 confp->chan.radio = 0;
10759 confp->chan.sig = SIG_PRI;
10760 confp->pri.nodetype = PRI_NETWORK;
10761 } else if (!strcasecmp(v->value, "pri_cpe")) {
10762 confp->chan.sig = SIG_PRI;
10763 confp->chan.radio = 0;
10764 confp->pri.nodetype = PRI_CPE;
10765 } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
10766 confp->chan.sig = SIG_GR303FXOKS;
10767 confp->chan.radio = 0;
10768 confp->pri.nodetype = PRI_NETWORK;
10769 } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
10770 confp->chan.sig = SIG_GR303FXSKS;
10771 confp->chan.radio = 0;
10772 confp->pri.nodetype = PRI_CPE;
10773 #endif
10774 } else {
10775 ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
10776 }
10777 } else if (!strcasecmp(v->name, "outsignalling")) {
10778 if (!strcasecmp(v->value, "em")) {
10779 confp->chan.outsigmod = SIG_EM;
10780 } else if (!strcasecmp(v->value, "em_e1")) {
10781 confp->chan.outsigmod = SIG_EM_E1;
10782 } else if (!strcasecmp(v->value, "em_w")) {
10783 confp->chan.outsigmod = SIG_EMWINK;
10784 } else if (!strcasecmp(v->value, "sf")) {
10785 confp->chan.outsigmod = SIG_SF;
10786 } else if (!strcasecmp(v->value, "sf_w")) {
10787 confp->chan.outsigmod = SIG_SFWINK;
10788 } else if (!strcasecmp(v->value, "sf_featd")) {
10789 confp->chan.outsigmod = SIG_FEATD;
10790 } else if (!strcasecmp(v->value, "sf_featdmf")) {
10791 confp->chan.outsigmod = SIG_FEATDMF;
10792 } else if (!strcasecmp(v->value, "sf_featb")) {
10793 confp->chan.outsigmod = SIG_SF_FEATB;
10794 } else if (!strcasecmp(v->value, "sf")) {
10795 confp->chan.outsigmod = SIG_SF;
10796 } else if (!strcasecmp(v->value, "featd")) {
10797 confp->chan.outsigmod = SIG_FEATD;
10798 } else if (!strcasecmp(v->value, "featdmf")) {
10799 confp->chan.outsigmod = SIG_FEATDMF;
10800 } else if (!strcasecmp(v->value, "featdmf_ta")) {
10801 confp->chan.outsigmod = SIG_FEATDMF_TA;
10802 } else if (!strcasecmp(v->value, "e911")) {
10803 confp->chan.outsigmod = SIG_E911;
10804 } else if (!strcasecmp(v->value, "fgccama")) {
10805 confp->chan.outsigmod = SIG_FGC_CAMA;
10806 } else if (!strcasecmp(v->value, "fgccamamf")) {
10807 confp->chan.outsigmod = SIG_FGC_CAMAMF;
10808 } else if (!strcasecmp(v->value, "featb")) {
10809 confp->chan.outsigmod = SIG_FEATB;
10810 } else {
10811 ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
10812 }
10813 #ifdef HAVE_PRI
10814 } else if (!strcasecmp(v->name, "pridialplan")) {
10815 if (!strcasecmp(v->value, "national")) {
10816 confp->pri.dialplan = PRI_NATIONAL_ISDN + 1;
10817 } else if (!strcasecmp(v->value, "unknown")) {
10818 confp->pri.dialplan = PRI_UNKNOWN + 1;
10819 } else if (!strcasecmp(v->value, "private")) {
10820 confp->pri.dialplan = PRI_PRIVATE + 1;
10821 } else if (!strcasecmp(v->value, "international")) {
10822 confp->pri.dialplan = PRI_INTERNATIONAL_ISDN + 1;
10823 } else if (!strcasecmp(v->value, "local")) {
10824 confp->pri.dialplan = PRI_LOCAL_ISDN + 1;
10825 } else if (!strcasecmp(v->value, "dynamic")) {
10826 confp->pri.dialplan = -1;
10827 } else {
10828 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10829 }
10830 } else if (!strcasecmp(v->name, "prilocaldialplan")) {
10831 if (!strcasecmp(v->value, "national")) {
10832 confp->pri.localdialplan = PRI_NATIONAL_ISDN + 1;
10833 } else if (!strcasecmp(v->value, "unknown")) {
10834 confp->pri.localdialplan = PRI_UNKNOWN + 1;
10835 } else if (!strcasecmp(v->value, "private")) {
10836 confp->pri.localdialplan = PRI_PRIVATE + 1;
10837 } else if (!strcasecmp(v->value, "international")) {
10838 confp->pri.localdialplan = PRI_INTERNATIONAL_ISDN + 1;
10839 } else if (!strcasecmp(v->value, "local")) {
10840 confp->pri.localdialplan = PRI_LOCAL_ISDN + 1;
10841 } else if (!strcasecmp(v->value, "dynamic")) {
10842 confp->pri.localdialplan = -1;
10843 } else {
10844 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10845 }
10846 } else if (!strcasecmp(v->name, "switchtype")) {
10847 if (!strcasecmp(v->value, "national"))
10848 confp->pri.switchtype = PRI_SWITCH_NI2;
10849 else if (!strcasecmp(v->value, "ni1"))
10850 confp->pri.switchtype = PRI_SWITCH_NI1;
10851 else if (!strcasecmp(v->value, "dms100"))
10852 confp->pri.switchtype = PRI_SWITCH_DMS100;
10853 else if (!strcasecmp(v->value, "4ess"))
10854 confp->pri.switchtype = PRI_SWITCH_ATT4ESS;
10855 else if (!strcasecmp(v->value, "5ess"))
10856 confp->pri.switchtype = PRI_SWITCH_LUCENT5E;
10857 else if (!strcasecmp(v->value, "euroisdn"))
10858 confp->pri.switchtype = PRI_SWITCH_EUROISDN_E1;
10859 else if (!strcasecmp(v->value, "qsig"))
10860 confp->pri.switchtype = PRI_SWITCH_QSIG;
10861 else {
10862 ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value);
10863 return -1;
10864 }
10865 } else if (!strcasecmp(v->name, "nsf")) {
10866 if (!strcasecmp(v->value, "sdn"))
10867 confp->pri.nsf = PRI_NSF_SDN;
10868 else if (!strcasecmp(v->value, "megacom"))
10869 confp->pri.nsf = PRI_NSF_MEGACOM;
10870 else if (!strcasecmp(v->value, "tollfreemegacom"))
10871 confp->pri.nsf = PRI_NSF_TOLL_FREE_MEGACOM;
10872 else if (!strcasecmp(v->value, "accunet"))
10873 confp->pri.nsf = PRI_NSF_ACCUNET;
10874 else if (!strcasecmp(v->value, "none"))
10875 confp->pri.nsf = PRI_NSF_NONE;
10876 else {
10877 ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value);
10878 confp->pri.nsf = PRI_NSF_NONE;
10879 }
10880 } else if (!strcasecmp(v->name, "priindication")) {
10881 if (!strcasecmp(v->value, "outofband"))
10882 confp->chan.priindication_oob = 1;
10883 else if (!strcasecmp(v->value, "inband"))
10884 confp->chan.priindication_oob = 0;
10885 else
10886 ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n",
10887 v->value, v->lineno);
10888 } else if (!strcasecmp(v->name, "priexclusive")) {
10889 confp->chan.priexclusive = ast_true(v->value);
10890 } else if (!strcasecmp(v->name, "internationalprefix")) {
10891 ast_copy_string(confp->pri.internationalprefix, v->value, sizeof(confp->pri.internationalprefix));
10892 } else if (!strcasecmp(v->name, "nationalprefix")) {
10893 ast_copy_string(confp->pri.nationalprefix, v->value, sizeof(confp->pri.nationalprefix));
10894 } else if (!strcasecmp(v->name, "localprefix")) {
10895 ast_copy_string(confp->pri.localprefix, v->value, sizeof(confp->pri.localprefix));
10896 } else if (!strcasecmp(v->name, "privateprefix")) {
10897 ast_copy_string(confp->pri.privateprefix, v->value, sizeof(confp->pri.privateprefix));
10898 } else if (!strcasecmp(v->name, "unknownprefix")) {
10899 ast_copy_string(confp->pri.unknownprefix, v->value, sizeof(confp->pri.unknownprefix));
10900 } else if (!strcasecmp(v->name, "resetinterval")) {
10901 if (!strcasecmp(v->value, "never"))
10902 confp->pri.resetinterval = -1;
10903 else if (atoi(v->value) >= 60)
10904 confp->pri.resetinterval = atoi(v->value);
10905 else
10906 ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d\n",
10907 v->value, v->lineno);
10908 } else if (!strcasecmp(v->name, "minunused")) {
10909 confp->pri.minunused = atoi(v->value);
10910 } else if (!strcasecmp(v->name, "minidle")) {
10911 confp->pri.minidle = atoi(v->value);
10912 } else if (!strcasecmp(v->name, "idleext")) {
10913 ast_copy_string(confp->pri.idleext, v->value, sizeof(confp->pri.idleext));
10914 } else if (!strcasecmp(v->name, "idledial")) {
10915 ast_copy_string(confp->pri.idledial, v->value, sizeof(confp->pri.idledial));
10916 } else if (!strcasecmp(v->name, "overlapdial")) {
10917 confp->pri.overlapdial = ast_true(v->value);
10918 } else if (!strcasecmp(v->name, "pritimer")) {
10919 #ifdef PRI_GETSET_TIMERS
10920 char *timerc, *c;
10921 int timer, timeridx;
10922 c = v->value;
10923 timerc = strsep(&c, ",");
10924 if (timerc) {
10925 timer = atoi(c);
10926 if (!timer)
10927 ast_log(LOG_WARNING, "'%s' is not a valid value for an ISDN timer\n", timerc);
10928 else {
10929 if ((timeridx = pri_timer2idx(timerc)) >= 0)
10930 pritimers[timeridx] = timer;
10931 else
10932 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer\n", timerc);
10933 }
10934 } else
10935 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer configuration string\n", v->value);
10936
10937 } else if (!strcasecmp(v->name, "facilityenable")) {
10938 confp->pri.facilityenable = ast_true(v->value);
10939 #endif
10940 #endif
10941 } else if (!strcasecmp(v->name, "cadence")) {
10942
10943 int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
10944 int i;
10945 struct zt_ring_cadence new_cadence;
10946 int cid_location = -1;
10947 int firstcadencepos = 0;
10948 char original_args[80];
10949 int cadence_is_ok = 1;
10950
10951 ast_copy_string(original_args, v->value, sizeof(original_args));
10952
10953 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]);
10954
10955
10956 if (element_count % 2 == 1) {
10957 ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args);
10958 cadence_is_ok = 0;
10959 }
10960
10961
10962 for (i = 0; i < element_count; i++) {
10963 if (c[i] == 0) {
10964 ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s\n", original_args);
10965 cadence_is_ok = 0;
10966 break;
10967 } else if (c[i] < 0) {
10968 if (i % 2 == 1) {
10969
10970 if (cid_location == -1) {
10971 cid_location = i;
10972 c[i] *= -1;
10973 } else {
10974 ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args);
10975 cadence_is_ok = 0;
10976 break;
10977 }
10978 } else {
10979 if (firstcadencepos == 0) {
10980 firstcadencepos = i;
10981
10982 } else {
10983 ast_log(LOG_ERROR, "First cadence position specified twice: %s\n",original_args);
10984 cadence_is_ok = 0;
10985 break;
10986 }
10987 }
10988 }
10989 }
10990
10991
10992 for (i = 0; i < 16; i++) {
10993 new_cadence.ringcadence[i] = c[i];
10994 }
10995
10996 if (cadence_is_ok) {
10997
10998 if (element_count < 2) {
10999 ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args);
11000 } else {
11001 if (cid_location == -1) {
11002
11003 cid_location = 1;
11004 } else {
11005
11006 cid_location = (cid_location + 1) / 2;
11007 }
11008
11009 if (!user_has_defined_cadences++)
11010
11011 num_cadence = 0;
11012 if ((num_cadence+1) >= NUM_CADENCE_MAX)
11013 ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args);
11014 else {
11015 cadences[num_cadence] = new_cadence;
11016 cidrings[num_cadence++] = cid_location;
11017 if (option_verbose > 2)
11018 ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args);
11019 }
11020 }
11021 }
11022 } else if (!strcasecmp(v->name, "ringtimeout")) {
11023 ringt_base = (atoi(v->value) * 8) / READ_SIZE;
11024 } else if (!strcasecmp(v->name, "prewink")) {
11025 confp->timing.prewinktime = atoi(v->value);
11026 } else if (!strcasecmp(v->name, "preflash")) {
11027 confp->timing.preflashtime = atoi(v->value);
11028 } else if (!strcasecmp(v->name, "wink")) {
11029 confp->timing.winktime = atoi(v->value);
11030 } else if (!strcasecmp(v->name, "flash")) {
11031 confp->timing.flashtime = atoi(v->value);
11032 } else if (!strcasecmp(v->name, "start")) {
11033 confp->timing.starttime = atoi(v->value);
11034 } else if (!strcasecmp(v->name, "rxwink")) {
11035 confp->timing.rxwinktime = atoi(v->value);
11036 } else if (!strcasecmp(v->name, "rxflash")) {
11037 confp->timing.rxflashtime = atoi(v->value);
11038 } else if (!strcasecmp(v->name, "debounce")) {
11039 confp->timing.debouncetime = atoi(v->value);
11040 } else if (!strcasecmp(v->name, "toneduration")) {
11041 int toneduration;
11042 int ctlfd;
11043 int res;
11044 struct zt_dialparams dps;
11045
11046 ctlfd = open("/dev/zap/ctl", O_RDWR);
11047 if (ctlfd == -1) {
11048 ast_log(LOG_ERROR, "Unable to open /dev/zap/ctl to set toneduration\n");
11049 return -1;
11050 }
11051
11052 toneduration = atoi(v->value);
11053 if (toneduration > -1) {
11054 dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration;
11055 res = ioctl(ctlfd, ZT_SET_DIALPARAMS, &dps);
11056 if (res < 0) {
11057 ast_log(LOG_ERROR, "Invalid tone duration: %d ms\n", toneduration);
11058 return -1;
11059 }
11060 }
11061 close(ctlfd);
11062 } else if (!strcasecmp(v->name, "polarityonanswerdelay")) {
11063 confp->chan.polarityonanswerdelay = atoi(v->value);
11064 } else if (!strcasecmp(v->name, "answeronpolarityswitch")) {
11065 confp->chan.answeronpolarityswitch = ast_true(v->value);
11066 } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) {
11067 confp->chan.hanguponpolarityswitch = ast_true(v->value);
11068 } else if (!strcasecmp(v->name, "sendcalleridafter")) {
11069 confp->chan.sendcalleridafter = atoi(v->value);
11070 } else if (!strcasecmp(v->name, "defaultcic")) {
11071 ast_copy_string(defaultcic, v->value, sizeof(defaultcic));
11072 } else if (!strcasecmp(v->name, "defaultozz")) {
11073 ast_copy_string(defaultozz, v->value, sizeof(defaultozz));
11074 }
11075 } else if (!skipchannels)
11076 ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
11077 }
11078 if (zapchan[0]) {
11079
11080
11081 if (build_channels(*confp, 0, zapchan, reload, 0, &found_pseudo)) {
11082 return -1;
11083 }
11084 }
11085
11086
11087 if (!found_pseudo && reload == 0) {
11088
11089
11090
11091 confp->chan.group = 0;
11092 confp->chan.callgroup = 0;
11093 confp->chan.pickupgroup = 0;
11094
11095 tmp = mkintf(CHAN_PSEUDO, *confp, NULL, reload);
11096
11097 if (tmp) {
11098 if (option_verbose > 2)
11099 ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n");
11100 } else {
11101 ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
11102 }
11103 }
11104 return 0;
11105 }
11106
11107 static int setup_zap(int reload)
11108 {
11109 struct ast_config *cfg;
11110 struct ast_variable *v;
11111 struct zt_chan_conf conf = zt_chan_conf_default();
11112 int res;
11113
11114 #ifdef HAVE_PRI
11115 char *c;
11116 int spanno;
11117 int i, x;
11118 int logicalspan;
11119 int trunkgroup;
11120 int dchannels[NUM_DCHANS];
11121 #endif
11122
11123 cfg = ast_config_load(config);
11124
11125
11126 if (!cfg) {
11127 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
11128 return 0;
11129 }
11130
11131
11132 ast_mutex_lock(&iflock);
11133 #ifdef HAVE_PRI
11134 if (!reload) {
11135
11136 v = ast_variable_browse(cfg, "trunkgroups");
11137 while (v) {
11138 if (!strcasecmp(v->name, "trunkgroup")) {
11139 trunkgroup = atoi(v->value);
11140 if (trunkgroup > 0) {
11141 if ((c = strchr(v->value, ','))) {
11142 i = 0;
11143 memset(dchannels, 0, sizeof(dchannels));
11144 while (c && (i < NUM_DCHANS)) {
11145 dchannels[i] = atoi(c + 1);
11146 if (dchannels[i] < 0) {
11147 ast_log(LOG_WARNING, "D-channel for trunk group %d must be a postiive number at line %d of zapata.conf\n", trunkgroup, v->lineno);
11148 } else
11149 i++;
11150 c = strchr(c + 1, ',');
11151 }
11152 if (i) {
11153 if (pri_create_trunkgroup(trunkgroup, dchannels)) {
11154 ast_log(LOG_WARNING, "Unable to create trunk group %d with Primary D-channel %d at line %d of zapata.conf\n", trunkgroup, dchannels[0], v->lineno);
11155 } else if (option_verbose > 1)
11156 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");
11157 } else
11158 ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of zapata.conf\n", trunkgroup, v->lineno);
11159 } else
11160 ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of zapata.conf\n", trunkgroup, v->lineno);
11161 } else
11162 ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of zapata.conf\n", v->lineno);
11163 } else if (!strcasecmp(v->name, "spanmap")) {
11164 spanno = atoi(v->value);
11165 if (spanno > 0) {
11166 if ((c = strchr(v->value, ','))) {
11167 trunkgroup = atoi(c + 1);
11168 if (trunkgroup > 0) {
11169 if ((c = strchr(c + 1, ',')))
11170 logicalspan = atoi(c + 1);
11171 else
11172 logicalspan = 0;
11173 if (logicalspan >= 0) {
11174 if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
11175 ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
11176 } else if (option_verbose > 1)
11177 ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
11178 } else
11179 ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of zapata.conf\n", v->lineno);
11180 } else
11181 ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of zapata.conf\n", v->lineno);
11182 } else
11183 ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of zapata.conf\n", v->lineno);
11184 } else
11185 ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of zapata.conf\n", v->lineno);
11186 } else {
11187 ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
11188 }
11189 v = v->next;
11190 }
11191 }
11192 #endif
11193
11194
11195 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
11196
11197 v = ast_variable_browse(cfg, "channels");
11198 res = process_zap(&conf, v, reload, 0);
11199 ast_mutex_unlock(&iflock);
11200 ast_config_destroy(cfg);
11201 if (res)
11202 return res;
11203 cfg = ast_config_load("users.conf");
11204 if (cfg) {
11205 char *cat;
11206 const char *chans;
11207 process_zap(&conf, ast_variable_browse(cfg, "general"), 1, 1);
11208 for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
11209 if (!strcasecmp(cat, "general"))
11210 continue;
11211 chans = ast_variable_retrieve(cfg, cat, "zapchan");
11212 if (!ast_strlen_zero(chans)) {
11213
11214
11215
11216
11217 process_zap(&conf, ast_variable_browse(cfg, cat), reload, 0);
11218 }
11219 }
11220 ast_config_destroy(cfg);
11221 }
11222 #ifdef HAVE_PRI
11223 if (!reload) {
11224 for (x = 0; x < NUM_SPANS; x++) {
11225 if (pris[x].pvts[0]) {
11226 if (start_pri(pris + x)) {
11227 ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
11228 return -1;
11229 } else if (option_verbose > 1)
11230 ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1);
11231 }
11232 }
11233 }
11234 #endif
11235
11236 restart_monitor();
11237 return 0;
11238 }
11239
11240 static int load_module(void)
11241 {
11242 int res;
11243
11244 #ifdef HAVE_PRI
11245 int y,i;
11246 memset(pris, 0, sizeof(pris));
11247 for (y = 0; y < NUM_SPANS; y++) {
11248 ast_mutex_init(&pris[y].lock);
11249 pris[y].offset = -1;
11250 pris[y].master = AST_PTHREADT_NULL;
11251 for (i = 0; i < NUM_DCHANS; i++)
11252 pris[y].fds[i] = -1;
11253 }
11254 pri_set_error(zt_pri_error);
11255 pri_set_message(zt_pri_message);
11256 ast_register_application(zap_send_keypad_facility_app, zap_send_keypad_facility_exec,
11257 zap_send_keypad_facility_synopsis, zap_send_keypad_facility_descrip);
11258 #endif
11259 res = setup_zap(0);
11260
11261 if (res)
11262 return AST_MODULE_LOAD_DECLINE;
11263 if (ast_channel_register(&zap_tech)) {
11264 ast_log(LOG_ERROR, "Unable to register channel class 'Zap'\n");
11265 __unload_module();
11266 return -1;
11267 }
11268 #ifdef HAVE_PRI
11269 ast_string_field_init(&inuse, 16);
11270 ast_string_field_set(&inuse, name, "GR-303InUse");
11271 ast_cli_register_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(struct ast_cli_entry));
11272 #endif
11273 ast_cli_register_multiple(zap_cli, sizeof(zap_cli) / sizeof(struct ast_cli_entry));
11274
11275 memset(round_robin, 0, sizeof(round_robin));
11276 ast_manager_register( "ZapTransfer", 0, action_transfer, "Transfer Zap Channel" );
11277 ast_manager_register( "ZapHangup", 0, action_transferhangup, "Hangup Zap Channel" );
11278 ast_manager_register( "ZapDialOffhook", 0, action_zapdialoffhook, "Dial over Zap channel while offhook" );
11279 ast_manager_register( "ZapDNDon", 0, action_zapdndon, "Toggle Zap channel Do Not Disturb status ON" );
11280 ast_manager_register( "ZapDNDoff", 0, action_zapdndoff, "Toggle Zap channel Do Not Disturb status OFF" );
11281 ast_manager_register("ZapShowChannels", 0, action_zapshowchannels, "Show status zapata channels");
11282 ast_manager_register("ZapRestart", 0, action_zaprestart, "Fully Restart zaptel channels (terminates calls)");
11283
11284 return res;
11285 }
11286
11287 static int zt_sendtext(struct ast_channel *c, const char *text)
11288 {
11289 #define END_SILENCE_LEN 400
11290 #define HEADER_MS 50
11291 #define TRAILER_MS 5
11292 #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
11293 #define ASCII_BYTES_PER_CHAR 80
11294
11295 unsigned char *buf,*mybuf;
11296 struct zt_pvt *p = c->tech_pvt;
11297 struct pollfd fds[1];
11298 int size,res,fd,len,x;
11299 int bytes=0;
11300
11301 float cr = 1.0;
11302 float ci = 0.0;
11303 float scont = 0.0;
11304 int index;
11305
11306 index = zt_get_index(c, p, 0);
11307 if (index < 0) {
11308 ast_log(LOG_WARNING, "Huh? I don't exist?\n");
11309 return -1;
11310 }
11311 if (!text[0]) return(0);
11312 if ((!p->tdd) && (!p->mate)) return(0);
11313 if (p->mate)
11314 buf = ast_malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
11315 else
11316 buf = ast_malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
11317 if (!buf)
11318 return -1;
11319 mybuf = buf;
11320 if (p->mate) {
11321 int codec = AST_LAW(p);
11322 for (x = 0; x < HEADER_MS; x++) {
11323 PUT_CLID_MARKMS;
11324 }
11325
11326 for (x = 0; text[x]; x++) {
11327 PUT_CLID(text[x]);
11328 }
11329 for (x = 0; x < TRAILER_MS; x++) {
11330 PUT_CLID_MARKMS;
11331 }
11332 len = bytes;
11333 buf = mybuf;
11334 } else {
11335 len = tdd_generate(p->tdd, buf, text);
11336 if (len < 1) {
11337 ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n", (int)strlen(text));
11338 free(mybuf);
11339 return -1;
11340 }
11341 }
11342 memset(buf + len, 0x7f, END_SILENCE_LEN);
11343 len += END_SILENCE_LEN;
11344 fd = p->subs[index].zfd;
11345 while (len) {
11346 if (ast_check_hangup(c)) {
11347 free(mybuf);
11348 return -1;
11349 }
11350 size = len;
11351 if (size > READ_SIZE)
11352 size = READ_SIZE;
11353 fds[0].fd = fd;
11354 fds[0].events = POLLOUT | POLLPRI;
11355 fds[0].revents = 0;
11356 res = poll(fds, 1, -1);
11357 if (!res) {
11358 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
11359 continue;
11360 }
11361
11362 if (fds[0].revents & POLLPRI)
11363 return -1;
11364 if (!(fds[0].revents & POLLOUT)) {
11365 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
11366 continue;
11367 }
11368 res = write(fd, buf, size);
11369 if (res != size) {
11370 if (res == -1) {
11371 free(mybuf);
11372 return -1;
11373 }
11374 if (option_debug)
11375 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
11376 break;
11377 }
11378 len -= size;
11379 buf += size;
11380 }
11381 free(mybuf);
11382 return(0);
11383 }
11384
11385
11386 static int reload(void)
11387 {
11388 int res = 0;
11389
11390 res = setup_zap(1);
11391 if (res) {
11392 ast_log(LOG_WARNING, "Reload of chan_zap.so is unsuccessful!\n");
11393 return -1;
11394 }
11395 return 0;
11396 }
11397
11398
11399
11400
11401
11402 #ifdef ZAPATA_PRI
11403 #define tdesc "Zapata Telephony w/PRI"
11404 #else
11405 #define tdesc "Zapata Telephony"
11406 #endif
11407
11408 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, tdesc,
11409 .load = load_module,
11410 .unload = unload_module,
11411 .reload = reload,
11412 );
11413
11414