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 #include <stdio.h>
00037 #include <string.h>
00038 #ifdef __NetBSD__
00039 #include <pthread.h>
00040 #include <signal.h>
00041 #else
00042 #include <sys/signal.h>
00043 #endif
00044 #include <errno.h>
00045 #include <stdlib.h>
00046 #if !defined(SOLARIS) && !defined(__FreeBSD__)
00047 #include <stdint.h>
00048 #endif
00049 #include <unistd.h>
00050 #include <sys/ioctl.h>
00051 #ifdef __linux__
00052 #include <linux/zaptel.h>
00053 #else
00054 #include <zaptel.h>
00055 #endif
00056 #include <math.h>
00057 #include <tonezone.h>
00058 #include <ctype.h>
00059 #ifdef ZAPATA_PRI
00060 #include <libpri.h>
00061 #ifndef PRI_USER_USER_TX
00062 #error "You need newer libpri"
00063 #endif
00064 #endif
00065 #ifdef ZAPATA_R2
00066 #include <libmfcr2.h>
00067 #endif
00068
00069 #include "asterisk.h"
00070
00071 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 17367 $")
00072
00073 #include "asterisk/lock.h"
00074 #include "asterisk/channel.h"
00075 #include "asterisk/config.h"
00076 #include "asterisk/logger.h"
00077 #include "asterisk/module.h"
00078 #include "asterisk/pbx.h"
00079 #include "asterisk/options.h"
00080 #include "asterisk/file.h"
00081 #include "asterisk/ulaw.h"
00082 #include "asterisk/alaw.h"
00083 #include "asterisk/callerid.h"
00084 #include "asterisk/adsi.h"
00085 #include "asterisk/cli.h"
00086 #include "asterisk/cdr.h"
00087 #include "asterisk/features.h"
00088 #include "asterisk/musiconhold.h"
00089 #include "asterisk/say.h"
00090 #include "asterisk/tdd.h"
00091 #include "asterisk/app.h"
00092 #include "asterisk/dsp.h"
00093 #include "asterisk/astdb.h"
00094 #include "asterisk/manager.h"
00095 #include "asterisk/causes.h"
00096 #include "asterisk/term.h"
00097 #include "asterisk/utils.h"
00098 #include "asterisk/transcap.h"
00099
00100 #ifndef ZT_SIG_EM_E1
00101 #error "Your zaptel is too old. please cvs update"
00102 #endif
00103
00104 #ifndef ZT_TONEDETECT
00105
00106 #define ZT_EVENT_DTMFDOWN 0
00107 #define ZT_EVENT_DTMFUP 0
00108 #endif
00109
00110
00111 #undef SUPPORT_USERUSER
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 #define DEFAULT_CIDRINGS 1
00133
00134 #define CHANNEL_PSEUDO -12
00135
00136 #define AST_LAW(p) (((p)->law == ZT_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
00137
00138
00139 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FEATB))
00140
00141 static const char desc[] = "Zapata Telephony"
00142 #ifdef ZAPATA_PRI
00143 " w/PRI"
00144 #endif
00145 #ifdef ZAPATA_R2
00146 " w/R2"
00147 #endif
00148 ;
00149
00150 static const char tdesc[] = "Zapata Telephony Driver"
00151 #ifdef ZAPATA_PRI
00152 " w/PRI"
00153 #endif
00154 #ifdef ZAPATA_R2
00155 " w/R2"
00156 #endif
00157 ;
00158
00159 static const char type[] = "Zap";
00160 static const char config[] = "zapata.conf";
00161
00162 #define SIG_EM ZT_SIG_EM
00163 #define SIG_EMWINK (0x0100000 | ZT_SIG_EM)
00164 #define SIG_FEATD (0x0200000 | ZT_SIG_EM)
00165 #define SIG_FEATDMF (0x0400000 | ZT_SIG_EM)
00166 #define SIG_FEATB (0x0800000 | ZT_SIG_EM)
00167 #define SIG_E911 (0x1000000 | ZT_SIG_EM)
00168 #define SIG_FEATDMF_TA (0x2000000 | ZT_SIG_EM)
00169 #define SIG_FXSLS ZT_SIG_FXSLS
00170 #define SIG_FXSGS ZT_SIG_FXSGS
00171 #define SIG_FXSKS ZT_SIG_FXSKS
00172 #define SIG_FXOLS ZT_SIG_FXOLS
00173 #define SIG_FXOGS ZT_SIG_FXOGS
00174 #define SIG_FXOKS ZT_SIG_FXOKS
00175 #define SIG_PRI ZT_SIG_CLEAR
00176 #define SIG_R2 ZT_SIG_CAS
00177 #define SIG_SF ZT_SIG_SF
00178 #define SIG_SFWINK (0x0100000 | ZT_SIG_SF)
00179 #define SIG_SF_FEATD (0x0200000 | ZT_SIG_SF)
00180 #define SIG_SF_FEATDMF (0x0400000 | ZT_SIG_SF)
00181 #define SIG_SF_FEATB (0x0800000 | ZT_SIG_SF)
00182 #define SIG_EM_E1 ZT_SIG_EM_E1
00183 #define SIG_GR303FXOKS (0x0100000 | ZT_SIG_FXOKS)
00184 #define SIG_GR303FXSKS (0x0100000 | ZT_SIG_FXSKS)
00185
00186 #define NUM_SPANS 32
00187 #define NUM_DCHANS 4
00188 #define MAX_CHANNELS 672
00189
00190 #define CHAN_PSEUDO -2
00191
00192 #define DCHAN_PROVISIONED (1 << 0)
00193 #define DCHAN_NOTINALARM (1 << 1)
00194 #define DCHAN_UP (1 << 2)
00195
00196 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
00197
00198 static char context[AST_MAX_CONTEXT] = "default";
00199 static char cid_num[256] = "";
00200 static char cid_name[256] = "";
00201 static char defaultcic[64] = "";
00202 static char defaultozz[64] = "";
00203
00204 static char language[MAX_LANGUAGE] = "";
00205 static char musicclass[MAX_MUSICCLASS] = "";
00206 static char progzone[10]= "";
00207
00208 static int usedistinctiveringdetection = 0;
00209
00210 static int transfertobusy = 1;
00211
00212 static int use_callerid = 1;
00213 static int cid_signalling = CID_SIG_BELL;
00214 static int cid_start = CID_START_RING;
00215 static int zaptrcallerid = 0;
00216 static int cur_signalling = -1;
00217
00218 static ast_group_t cur_group = 0;
00219 static ast_group_t cur_callergroup = 0;
00220 static ast_group_t cur_pickupgroup = 0;
00221 static int relaxdtmf = 0;
00222
00223 static int immediate = 0;
00224
00225 static int stripmsd = 0;
00226
00227 static int callwaiting = 0;
00228
00229 static int callwaitingcallerid = 0;
00230
00231 static int hidecallerid = 0;
00232
00233 static int restrictcid = 0;
00234
00235 static int use_callingpres = 0;
00236
00237 static int callreturn = 0;
00238
00239 static int threewaycalling = 0;
00240
00241 static int transfer = 0;
00242
00243 static int canpark = 0;
00244
00245 static int cancallforward = 0;
00246
00247 static float rxgain = 0.0;
00248
00249 static float txgain = 0.0;
00250
00251 static int tonezone = -1;
00252
00253 static int echocancel;
00254
00255 static int echotraining;
00256
00257 static int pulse;
00258
00259 static int echocanbridged = 0;
00260
00261 static int busydetect = 0;
00262
00263 static int busycount = 3;
00264 static int busy_tonelength = 0;
00265 static int busy_quietlength = 0;
00266
00267 static int callprogress = 0;
00268
00269 static char accountcode[AST_MAX_ACCOUNT_CODE] = "";
00270
00271 static char mailbox[AST_MAX_EXTENSION];
00272
00273 static int amaflags = 0;
00274
00275 static int adsi = 0;
00276
00277 static int numbufs = 4;
00278
00279 static int cur_prewink = -1;
00280 static int cur_preflash = -1;
00281 static int cur_wink = -1;
00282 static int cur_flash = -1;
00283 static int cur_start = -1;
00284 static int cur_rxwink = -1;
00285 static int cur_rxflash = -1;
00286 static int cur_debounce = -1;
00287 static int cur_priexclusive = 0;
00288
00289 static int priindication_oob = 0;
00290
00291 #ifdef ZAPATA_PRI
00292 static int minunused = 2;
00293 static int minidle = 0;
00294 static char idleext[AST_MAX_EXTENSION];
00295 static char idledial[AST_MAX_EXTENSION];
00296 static int overlapdial = 0;
00297 static int facilityenable = 0;
00298 static char internationalprefix[10] = "";
00299 static char nationalprefix[10] = "";
00300 static char localprefix[20] = "";
00301 static char privateprefix[20] = "";
00302 static char unknownprefix[20] = "";
00303 static long resetinterval = 3600;
00304 static struct ast_channel inuse = { "GR-303InUse" };
00305 #ifdef PRI_GETSET_TIMERS
00306 static int pritimers[PRI_MAX_TIMERS];
00307 #endif
00308 static int pridebugfd = -1;
00309 static char pridebugfilename[1024]="";
00310 #endif
00311
00312
00313 static int firstdigittimeout = 16000;
00314
00315
00316 static int gendigittimeout = 8000;
00317
00318
00319 static int matchdigittimeout = 3000;
00320
00321 static int usecnt =0;
00322 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
00323
00324
00325 AST_MUTEX_DEFINE_STATIC(iflock);
00326
00327
00328 static int ifcount = 0;
00329
00330 #ifdef ZAPATA_PRI
00331 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
00332 #endif
00333
00334
00335 static int answeronpolarityswitch = 0;
00336
00337
00338 static int hanguponpolarityswitch = 0;
00339
00340
00341 static int polarityonanswerdelay = 600;
00342
00343
00344 static int sendcalleridafter = DEFAULT_CIDRINGS;
00345
00346
00347
00348 AST_MUTEX_DEFINE_STATIC(monlock);
00349
00350
00351
00352 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00353
00354 static int restart_monitor(void);
00355
00356 static enum ast_bridge_result zt_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
00357
00358 static int zt_sendtext(struct ast_channel *c, const char *text);
00359
00360
00361 static inline int zt_get_event(int fd)
00362 {
00363 int j;
00364 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
00365 return j;
00366 }
00367
00368
00369 static inline int zt_wait_event(int fd)
00370 {
00371 int i,j=0;
00372 i = ZT_IOMUX_SIGEVENT;
00373 if (ioctl(fd, ZT_IOMUX, &i) == -1) return -1;
00374 if (ioctl(fd, ZT_GETEVENT, &j) == -1) return -1;
00375 return j;
00376 }
00377
00378
00379 #define READ_SIZE 160
00380
00381 #define MASK_AVAIL (1 << 0)
00382 #define MASK_INUSE (1 << 1)
00383
00384 #define CALLWAITING_SILENT_SAMPLES ( (300 * 8) / READ_SIZE)
00385 #define CALLWAITING_REPEAT_SAMPLES ( (10000 * 8) / READ_SIZE)
00386 #define CIDCW_EXPIRE_SAMPLES ( (500 * 8) / READ_SIZE)
00387 #define MIN_MS_SINCE_FLASH ( (2000) )
00388 #define DEFAULT_RINGT ( (8000 * 8) / READ_SIZE)
00389
00390 struct zt_pvt;
00391
00392
00393 #ifdef ZAPATA_R2
00394 static int r2prot = -1;
00395 #endif
00396
00397 static int ringt_base = DEFAULT_RINGT;
00398
00399 #ifdef ZAPATA_PRI
00400
00401 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
00402 #define PRI_CHANNEL(p) ((p) & 0xff)
00403 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
00404 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
00405
00406 struct zt_pri {
00407 pthread_t master;
00408 ast_mutex_t lock;
00409 char idleext[AST_MAX_EXTENSION];
00410 char idlecontext[AST_MAX_CONTEXT];
00411 char idledial[AST_MAX_EXTENSION];
00412 int minunused;
00413 int minidle;
00414 int nodetype;
00415 int switchtype;
00416 int nsf;
00417 int dialplan;
00418 int localdialplan;
00419 char internationalprefix[10];
00420 char nationalprefix[10];
00421 char localprefix[20];
00422 char privateprefix[20];
00423 char unknownprefix[20];
00424 int dchannels[NUM_DCHANS];
00425 int trunkgroup;
00426 int mastertrunkgroup;
00427 int prilogicalspan;
00428 int numchans;
00429 int overlapdial;
00430 int facilityenable;
00431 struct pri *dchans[NUM_DCHANS];
00432 int dchanavail[NUM_DCHANS];
00433 struct pri *pri;
00434 int debug;
00435 int fds[NUM_DCHANS];
00436 int offset;
00437 int span;
00438 int resetting;
00439 int resetpos;
00440 time_t lastreset;
00441 long resetinterval;
00442 struct zt_pvt *pvts[MAX_CHANNELS];
00443 struct zt_pvt *crvs;
00444 struct zt_pvt *crvend;
00445 };
00446
00447
00448 static struct zt_pri pris[NUM_SPANS];
00449
00450 static int pritype = PRI_CPE;
00451
00452 #if 0
00453 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
00454 #else
00455 #define DEFAULT_PRI_DEBUG 0
00456 #endif
00457
00458 static inline void pri_rel(struct zt_pri *pri)
00459 {
00460 ast_mutex_unlock(&pri->lock);
00461 }
00462
00463 static int switchtype = PRI_SWITCH_NI2;
00464 static int nsf = PRI_NSF_NONE;
00465 static int dialplan = PRI_NATIONAL_ISDN + 1;
00466 static int localdialplan = PRI_NATIONAL_ISDN + 1;
00467
00468 #else
00469
00470 struct zt_pri;
00471 #endif
00472
00473 #define SUB_REAL 0
00474 #define SUB_CALLWAIT 1
00475 #define SUB_THREEWAY 2
00476
00477
00478 #define POLARITY_IDLE 0
00479 #define POLARITY_REV 1
00480
00481
00482 static struct zt_distRings drings;
00483
00484 struct distRingData {
00485 int ring[3];
00486 };
00487 struct ringContextData {
00488 char contextData[AST_MAX_CONTEXT];
00489 };
00490 struct zt_distRings {
00491 struct distRingData ringnum[3];
00492 struct ringContextData ringContext[3];
00493 };
00494
00495 static char *subnames[] = {
00496 "Real",
00497 "Callwait",
00498 "Threeway"
00499 };
00500
00501 struct zt_subchannel {
00502 int zfd;
00503 struct ast_channel *owner;
00504 int chan;
00505 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
00506 struct ast_frame f;
00507 unsigned int needringing:1;
00508 unsigned int needbusy:1;
00509 unsigned int needcongestion:1;
00510 unsigned int needcallerid:1;
00511 unsigned int needanswer:1;
00512 unsigned int needflash:1;
00513 unsigned int linear:1;
00514 unsigned int inthreeway:1;
00515 ZT_CONFINFO curconf;
00516 };
00517
00518 #define CONF_USER_REAL (1 << 0)
00519 #define CONF_USER_THIRDCALL (1 << 1)
00520
00521 #define MAX_SLAVES 4
00522
00523 static struct zt_pvt {
00524 ast_mutex_t lock;
00525 struct ast_channel *owner;
00526
00527
00528 struct zt_subchannel sub_unused;
00529 struct zt_subchannel subs[3];
00530 struct zt_confinfo saveconf;
00531
00532 struct zt_pvt *slaves[MAX_SLAVES];
00533 struct zt_pvt *master;
00534 int inconference;
00535
00536 int sig;
00537 int radio;
00538 float rxgain;
00539 float txgain;
00540 int tonezone;
00541 struct zt_pvt *next;
00542 struct zt_pvt *prev;
00543
00544
00545 unsigned int adsi:1;
00546 unsigned int answeronpolarityswitch:1;
00547 unsigned int busydetect:1;
00548 unsigned int callreturn:1;
00549 unsigned int callwaiting:1;
00550 unsigned int callwaitingcallerid:1;
00551 unsigned int cancallforward:1;
00552 unsigned int canpark:1;
00553 unsigned int confirmanswer:1;
00554 unsigned int destroy:1;
00555 unsigned int didtdd:1;
00556 unsigned int dialednone:1;
00557 unsigned int dialing:1;
00558 unsigned int digital:1;
00559 unsigned int dnd:1;
00560 unsigned int echobreak:1;
00561 unsigned int echocanbridged:1;
00562 unsigned int echocanon:1;
00563 unsigned int faxhandled:1;
00564 unsigned int firstradio:1;
00565 unsigned int hanguponpolarityswitch:1;
00566 unsigned int hardwaredtmf:1;
00567 unsigned int hidecallerid;
00568 unsigned int ignoredtmf:1;
00569 unsigned int immediate:1;
00570 unsigned int inalarm:1;
00571 unsigned int mate:1;
00572 unsigned int outgoing:1;
00573 unsigned int overlapdial:1;
00574 unsigned int permcallwaiting:1;
00575 unsigned int permhidecallerid:1;
00576 unsigned int priindication_oob:1;
00577 unsigned int priexclusive:1;
00578 unsigned int pulse:1;
00579 unsigned int pulsedial:1;
00580 unsigned int restrictcid:1;
00581 unsigned int threewaycalling:1;
00582 unsigned int transfer:1;
00583 unsigned int use_callerid:1;
00584 unsigned int use_callingpres:1;
00585 unsigned int usedistinctiveringdetection:1;
00586 unsigned int zaptrcallerid:1;
00587 unsigned int transfertobusy:1;
00588 #if defined(ZAPATA_PRI)
00589 unsigned int alerting:1;
00590 unsigned int alreadyhungup:1;
00591 unsigned int isidlecall:1;
00592 unsigned int proceeding:1;
00593 unsigned int progress:1;
00594 unsigned int resetting:1;
00595 unsigned int setup_ack:1;
00596 #endif
00597 #if defined(ZAPATA_R2)
00598 unsigned int hasr2call:1;
00599 unsigned int r2blocked:1;
00600 unsigned int sigchecked:1;
00601 #endif
00602
00603 struct zt_distRings drings;
00604
00605 char context[AST_MAX_CONTEXT];
00606 char defcontext[AST_MAX_CONTEXT];
00607 char exten[AST_MAX_EXTENSION];
00608 char language[MAX_LANGUAGE];
00609 char musicclass[MAX_MUSICCLASS];
00610 #ifdef PRI_ANI
00611 char cid_ani[AST_MAX_EXTENSION];
00612 #endif
00613 char cid_num[AST_MAX_EXTENSION];
00614 int cid_ton;
00615 char cid_name[AST_MAX_EXTENSION];
00616 char lastcid_num[AST_MAX_EXTENSION];
00617 char lastcid_name[AST_MAX_EXTENSION];
00618 char *origcid_num;
00619 char *origcid_name;
00620 char callwait_num[AST_MAX_EXTENSION];
00621 char callwait_name[AST_MAX_EXTENSION];
00622 char rdnis[AST_MAX_EXTENSION];
00623 char dnid[AST_MAX_EXTENSION];
00624 unsigned int group;
00625 int law;
00626 int confno;
00627 int confusers;
00628 int propconfno;
00629 ast_group_t callgroup;
00630 ast_group_t pickupgroup;
00631 int channel;
00632 int span;
00633 time_t guardtime;
00634 int cid_signalling;
00635 int cid_start;
00636 int callingpres;
00637 int callwaitingrepeat;
00638 int cidcwexpire;
00639 unsigned char *cidspill;
00640 int cidpos;
00641 int cidlen;
00642 int ringt;
00643 int ringt_base;
00644 int stripmsd;
00645 int callwaitcas;
00646 int callwaitrings;
00647 int echocancel;
00648 int echotraining;
00649 char echorest[20];
00650 int busycount;
00651 int busy_tonelength;
00652 int busy_quietlength;
00653 int callprogress;
00654 struct timeval flashtime;
00655 struct ast_dsp *dsp;
00656 int cref;
00657 ZT_DIAL_OPERATION dop;
00658 int whichwink;
00659 char finaldial[64];
00660 char accountcode[AST_MAX_ACCOUNT_CODE];
00661 int amaflags;
00662 struct tdd_state *tdd;
00663 char call_forward[AST_MAX_EXTENSION];
00664 char mailbox[AST_MAX_EXTENSION];
00665 char dialdest[256];
00666 int onhooktime;
00667 int msgstate;
00668 int distinctivering;
00669 int cidrings;
00670 int dtmfrelax;
00671 int fake_event;
00672 int polarityonanswerdelay;
00673 struct timeval polaritydelaytv;
00674 int sendcalleridafter;
00675 #ifdef ZAPATA_PRI
00676 struct zt_pri *pri;
00677 struct zt_pvt *bearer;
00678 struct zt_pvt *realcall;
00679 q931_call *call;
00680 int prioffset;
00681 int logicalspan;
00682 #endif
00683 #ifdef ZAPATA_R2
00684 int r2prot;
00685 mfcr2_t *r2;
00686 #endif
00687 int polarity;
00688 int dsp_features;
00689
00690 } *iflist = NULL, *ifend = NULL;
00691
00692 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause);
00693 static int zt_digit(struct ast_channel *ast, char digit);
00694 static int zt_sendtext(struct ast_channel *c, const char *text);
00695 static int zt_call(struct ast_channel *ast, char *rdest, int timeout);
00696 static int zt_hangup(struct ast_channel *ast);
00697 static int zt_answer(struct ast_channel *ast);
00698 struct ast_frame *zt_read(struct ast_channel *ast);
00699 static int zt_write(struct ast_channel *ast, struct ast_frame *frame);
00700 struct ast_frame *zt_exception(struct ast_channel *ast);
00701 static int zt_indicate(struct ast_channel *chan, int condition);
00702 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00703 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen);
00704
00705 static const struct ast_channel_tech zap_tech = {
00706 .type = type,
00707 .description = tdesc,
00708 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
00709 .requester = zt_request,
00710 .send_digit = zt_digit,
00711 .send_text = zt_sendtext,
00712 .call = zt_call,
00713 .hangup = zt_hangup,
00714 .answer = zt_answer,
00715 .read = zt_read,
00716 .write = zt_write,
00717 .bridge = zt_bridge,
00718 .exception = zt_exception,
00719 .indicate = zt_indicate,
00720 .fixup = zt_fixup,
00721 .setoption = zt_setoption,
00722 };
00723
00724 #ifdef ZAPATA_PRI
00725 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
00726 #else
00727 #define GET_CHANNEL(p) ((p)->channel)
00728 #endif
00729
00730 struct zt_pvt *round_robin[32];
00731
00732 #ifdef ZAPATA_PRI
00733 static inline int pri_grab(struct zt_pvt *pvt, struct zt_pri *pri)
00734 {
00735 int res;
00736
00737 do {
00738 res = ast_mutex_trylock(&pri->lock);
00739 if (res) {
00740 ast_mutex_unlock(&pvt->lock);
00741
00742 usleep(1);
00743 ast_mutex_lock(&pvt->lock);
00744 }
00745 } while(res);
00746
00747 pthread_kill(pri->master, SIGURG);
00748 return 0;
00749 }
00750 #endif
00751
00752 #define NUM_CADENCE_MAX 25
00753 static int num_cadence = 4;
00754 static int user_has_defined_cadences = 0;
00755
00756 static struct zt_ring_cadence cadences[NUM_CADENCE_MAX] = {
00757 { { 125, 125, 2000, 4000 } },
00758 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } },
00759 { { 125, 125, 125, 125, 125, 4000 } },
00760 { { 1000, 500, 2500, 5000 } },
00761 };
00762
00763 int receivedRingT;
00764
00765
00766
00767
00768
00769 static int cidrings[NUM_CADENCE_MAX] = {
00770 2,
00771 4,
00772 3,
00773 2,
00774 };
00775
00776 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
00777 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
00778
00779 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
00780 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
00781
00782 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
00783 {
00784 int res;
00785 if (p->subs[0].owner == ast)
00786 res = 0;
00787 else if (p->subs[1].owner == ast)
00788 res = 1;
00789 else if (p->subs[2].owner == ast)
00790 res = 2;
00791 else {
00792 res = -1;
00793 if (!nullok)
00794 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
00795 }
00796 return res;
00797 }
00798
00799 #ifdef ZAPATA_PRI
00800 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
00801 #else
00802 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
00803 #endif
00804 {
00805 struct ast_frame null = { AST_FRAME_NULL, };
00806 #ifdef ZAPATA_PRI
00807 if (pri)
00808 ast_mutex_unlock(&pri->lock);
00809 #endif
00810 for (;;) {
00811 if (p->subs[a].owner) {
00812 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
00813 ast_mutex_unlock(&p->lock);
00814 usleep(1);
00815 ast_mutex_lock(&p->lock);
00816 } else {
00817 ast_queue_frame(p->subs[a].owner, &null);
00818 ast_mutex_unlock(&p->subs[a].owner->lock);
00819 break;
00820 }
00821 } else
00822 break;
00823 }
00824 #ifdef ZAPATA_PRI
00825 if (pri)
00826 ast_mutex_lock(&pri->lock);
00827 #endif
00828 }
00829
00830 #ifdef ZAPATA_PRI
00831 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
00832 #else
00833 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
00834 #endif
00835 {
00836
00837 #ifdef ZAPATA_PRI
00838 if (pri)
00839 ast_mutex_unlock(&pri->lock);
00840 #endif
00841 for (;;) {
00842 if (p->owner) {
00843 if (ast_mutex_trylock(&p->owner->lock)) {
00844 ast_mutex_unlock(&p->lock);
00845 usleep(1);
00846 ast_mutex_lock(&p->lock);
00847 } else {
00848 ast_queue_frame(p->owner, f);
00849 ast_mutex_unlock(&p->owner->lock);
00850 break;
00851 }
00852 } else
00853 break;
00854 }
00855 #ifdef ZAPATA_PRI
00856 if (pri)
00857 ast_mutex_lock(&pri->lock);
00858 #endif
00859 }
00860
00861 static int restore_gains(struct zt_pvt *p);
00862
00863 static void swap_subs(struct zt_pvt *p, int a, int b)
00864 {
00865 int tchan;
00866 int tinthreeway;
00867 struct ast_channel *towner;
00868
00869 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
00870
00871 tchan = p->subs[a].chan;
00872 towner = p->subs[a].owner;
00873 tinthreeway = p->subs[a].inthreeway;
00874
00875 p->subs[a].chan = p->subs[b].chan;
00876 p->subs[a].owner = p->subs[b].owner;
00877 p->subs[a].inthreeway = p->subs[b].inthreeway;
00878
00879 p->subs[b].chan = tchan;
00880 p->subs[b].owner = towner;
00881 p->subs[b].inthreeway = tinthreeway;
00882
00883 if (p->subs[a].owner)
00884 p->subs[a].owner->fds[0] = p->subs[a].zfd;
00885 if (p->subs[b].owner)
00886 p->subs[b].owner->fds[0] = p->subs[b].zfd;
00887 wakeup_sub(p, a, NULL);
00888 wakeup_sub(p, b, NULL);
00889 }
00890
00891 static int zt_open(char *fn)
00892 {
00893 int fd;
00894 int isnum;
00895 int chan = 0;
00896 int bs;
00897 int x;
00898 isnum = 1;
00899 for (x=0;x<strlen(fn);x++) {
00900 if (!isdigit(fn[x])) {
00901 isnum = 0;
00902 break;
00903 }
00904 }
00905 if (isnum) {
00906 chan = atoi(fn);
00907 if (chan < 1) {
00908 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
00909 return -1;
00910 }
00911 fn = "/dev/zap/channel";
00912 }
00913 fd = open(fn, O_RDWR | O_NONBLOCK);
00914 if (fd < 0) {
00915 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
00916 return -1;
00917 }
00918 if (chan) {
00919 if (ioctl(fd, ZT_SPECIFY, &chan)) {
00920 x = errno;
00921 close(fd);
00922 errno = x;
00923 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
00924 return -1;
00925 }
00926 }
00927 bs = READ_SIZE;
00928 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
00929 return fd;
00930 }
00931
00932 static void zt_close(int fd)
00933 {
00934 if(fd > 0)
00935 close(fd);
00936 }
00937
00938 int zt_setlinear(int zfd, int linear)
00939 {
00940 int res;
00941 res = ioctl(zfd, ZT_SETLINEAR, &linear);
00942 if (res)
00943 return res;
00944 return 0;
00945 }
00946
00947
00948 int zt_setlaw(int zfd, int law)
00949 {
00950 int res;
00951 res = ioctl(zfd, ZT_SETLAW, &law);
00952 if (res)
00953 return res;
00954 return 0;
00955 }
00956
00957 static int zt_get_history(int fd, void *buf, int buf_size)
00958 {
00959 struct zt_history hist;
00960 hist.buf=buf;
00961 hist.len=buf_size;
00962 return ioctl(fd, ZT_GET_HISTORY, &hist);
00963 }
00964
00965 static int alloc_sub(struct zt_pvt *p, int x)
00966 {
00967 ZT_BUFFERINFO bi;
00968 int res;
00969 if (p->subs[x].zfd < 0) {
00970 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
00971 if (p->subs[x].zfd > -1) {
00972 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
00973 if (!res) {
00974 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
00975 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
00976 bi.numbufs = numbufs;
00977 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
00978 if (res < 0) {
00979 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
00980 }
00981 } else
00982 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
00983 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
00984 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
00985 zt_close(p->subs[x].zfd);
00986 p->subs[x].zfd = -1;
00987 return -1;
00988 }
00989 if (option_debug)
00990 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
00991 return 0;
00992 } else
00993 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
00994 return -1;
00995 }
00996 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
00997 return -1;
00998 }
00999
01000 static int unalloc_sub(struct zt_pvt *p, int x)
01001 {
01002 if (!x) {
01003 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
01004 return -1;
01005 }
01006 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
01007 if (p->subs[x].zfd > -1) {
01008 zt_close(p->subs[x].zfd);
01009 }
01010 p->subs[x].zfd = -1;
01011 p->subs[x].linear = 0;
01012 p->subs[x].chan = 0;
01013 p->subs[x].owner = NULL;
01014 p->subs[x].inthreeway = 0;
01015 p->polarity = POLARITY_IDLE;
01016 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
01017 return 0;
01018 }
01019
01020 static int zt_digit(struct ast_channel *ast, char digit)
01021 {
01022 ZT_DIAL_OPERATION zo;
01023 struct zt_pvt *p;
01024 int res = 0;
01025 int index;
01026 p = ast->tech_pvt;
01027 ast_mutex_lock(&p->lock);
01028 index = zt_get_index(ast, p, 0);
01029 if ((index == SUB_REAL) && p->owner) {
01030 #ifdef ZAPATA_PRI
01031 if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
01032 if (p->setup_ack) {
01033 if (!pri_grab(p, p->pri)) {
01034 pri_information(p->pri->pri,p->call,digit);
01035 pri_rel(p->pri);
01036 } else
01037 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
01038 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
01039 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
01040 res = strlen(p->dialdest);
01041 p->dialdest[res++] = digit;
01042 p->dialdest[res] = '\0';
01043 }
01044 } else {
01045 #else
01046 {
01047 #endif
01048 zo.op = ZT_DIAL_OP_APPEND;
01049 zo.dialstr[0] = 'T';
01050 zo.dialstr[1] = digit;
01051 zo.dialstr[2] = 0;
01052 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
01053 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
01054 else
01055 p->dialing = 1;
01056 }
01057 }
01058 ast_mutex_unlock(&p->lock);
01059 return res;
01060 }
01061
01062 static char *events[] = {
01063 "No event",
01064 "On hook",
01065 "Ring/Answered",
01066 "Wink/Flash",
01067 "Alarm",
01068 "No more alarm",
01069 "HDLC Abort",
01070 "HDLC Overrun",
01071 "HDLC Bad FCS",
01072 "Dial Complete",
01073 "Ringer On",
01074 "Ringer Off",
01075 "Hook Transition Complete",
01076 "Bits Changed",
01077 "Pulse Start",
01078 "Timer Expired",
01079 "Timer Ping",
01080 "Polarity Reversal",
01081 "Ring Begin",
01082 };
01083
01084 static struct {
01085 int alarm;
01086 char *name;
01087 } alarms[] = {
01088 { ZT_ALARM_RED, "Red Alarm" },
01089 { ZT_ALARM_YELLOW, "Yellow Alarm" },
01090 { ZT_ALARM_BLUE, "Blue Alarm" },
01091 { ZT_ALARM_RECOVER, "Recovering" },
01092 { ZT_ALARM_LOOPBACK, "Loopback" },
01093 { ZT_ALARM_NOTOPEN, "Not Open" },
01094 { ZT_ALARM_NONE, "None" },
01095 };
01096
01097 static char *alarm2str(int alarm)
01098 {
01099 int x;
01100 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
01101 if (alarms[x].alarm & alarm)
01102 return alarms[x].name;
01103 }
01104 return alarm ? "Unknown Alarm" : "No Alarm";
01105 }
01106
01107 static char *event2str(int event)
01108 {
01109 static char buf[256];
01110 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
01111 return events[event];
01112 sprintf(buf, "Event %d", event);
01113 return buf;
01114 }
01115
01116 #ifdef ZAPATA_PRI
01117 static char *dialplan2str(int dialplan)
01118 {
01119 if (dialplan == -1) {
01120 return("Dynamically set dialplan in ISDN");
01121 }
01122 return(pri_plan2str(dialplan));
01123 }
01124 #endif
01125
01126 #ifdef ZAPATA_R2
01127 static int str2r2prot(char *swtype)
01128 {
01129 if (!strcasecmp(swtype, "ar"))
01130 return MFCR2_PROT_ARGENTINA;
01131
01132 if (!strcasecmp(swtype, "cn"))
01133 return MFCR2_PROT_CHINA;
01134
01135 if (!strcasecmp(swtype, "kr"))
01136 return MFCR2_PROT_KOREA;
01137
01138 return -1;
01139 }
01140 #endif
01141
01142 static char *zap_sig2str(int sig)
01143 {
01144 static char buf[256];
01145 switch(sig) {
01146 case SIG_EM:
01147 return "E & M Immediate";
01148 case SIG_EMWINK:
01149 return "E & M Wink";
01150 case SIG_EM_E1:
01151 return "E & M E1";
01152 case SIG_FEATD:
01153 return "Feature Group D (DTMF)";
01154 case SIG_FEATDMF:
01155 return "Feature Group D (MF)";
01156 case SIG_FEATDMF_TA:
01157 return "Feature Groud D (MF) Tandem Access";
01158 case SIG_FEATB:
01159 return "Feature Group B (MF)";
01160 case SIG_E911:
01161 return "E911 (MF)";
01162 case SIG_FXSLS:
01163 return "FXS Loopstart";
01164 case SIG_FXSGS:
01165 return "FXS Groundstart";
01166 case SIG_FXSKS:
01167 return "FXS Kewlstart";
01168 case SIG_FXOLS:
01169 return "FXO Loopstart";
01170 case SIG_FXOGS:
01171 return "FXO Groundstart";
01172 case SIG_FXOKS:
01173 return "FXO Kewlstart";
01174 case SIG_PRI:
01175 return "PRI Signalling";
01176 case SIG_R2:
01177 return "R2 Signalling";
01178 case SIG_SF:
01179 return "SF (Tone) Signalling Immediate";
01180 case SIG_SFWINK:
01181 return "SF (Tone) Signalling Wink";
01182 case SIG_SF_FEATD:
01183 return "SF (Tone) Signalling with Feature Group D (DTMF)";
01184 case SIG_SF_FEATDMF:
01185 return "SF (Tone) Signalling with Feature Group D (MF)";
01186 case SIG_SF_FEATB:
01187 return "SF (Tone) Signalling with Feature Group B (MF)";
01188 case SIG_GR303FXOKS:
01189 return "GR-303 Signalling with FXOKS";
01190 case SIG_GR303FXSKS:
01191 return "GR-303 Signalling with FXSKS";
01192 case 0:
01193 return "Pseudo Signalling";
01194 default:
01195 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
01196 return buf;
01197 }
01198 }
01199
01200 #define sig2str zap_sig2str
01201
01202 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
01203 {
01204
01205
01206 ZT_CONFINFO zi;
01207
01208 memset(&zi, 0, sizeof(zi));
01209 zi.chan = 0;
01210
01211 if (slavechannel > 0) {
01212
01213 zi.confmode = ZT_CONF_DIGITALMON;
01214 zi.confno = slavechannel;
01215 } else {
01216 if (!index) {
01217
01218 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
01219 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
01220 } else
01221 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
01222 zi.confno = p->confno;
01223 }
01224 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
01225 return 0;
01226 if (c->zfd < 0)
01227 return 0;
01228 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01229 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
01230 return -1;
01231 }
01232 if (slavechannel < 1) {
01233 p->confno = zi.confno;
01234 }
01235 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01236 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01237 return 0;
01238 }
01239
01240 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
01241 {
01242
01243 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
01244 return 1;
01245
01246 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
01247 return 1;
01248 return 0;
01249 }
01250
01251 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
01252 {
01253 ZT_CONFINFO zi;
01254 if (
01255 (c->zfd < 0) ||
01256
01257 !isourconf(p, c)
01258
01259 ) return 0;
01260 memset(&zi, 0, sizeof(zi));
01261 zi.chan = 0;
01262 zi.confno = 0;
01263 zi.confmode = 0;
01264 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01265 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01266 return -1;
01267 }
01268 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01269 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01270 return 0;
01271 }
01272
01273 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
01274 {
01275 int x;
01276 int useslavenative;
01277 struct zt_pvt *slave = NULL;
01278
01279 useslavenative = 1;
01280
01281 for (x=0;x<3;x++) {
01282
01283
01284 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
01285 useslavenative = 0;
01286 }
01287
01288
01289 if (useslavenative) {
01290 for (x=0;x<MAX_SLAVES;x++) {
01291 if (p->slaves[x]) {
01292 if (slave) {
01293
01294
01295 slave = NULL;
01296 useslavenative = 0;
01297 break;
01298 } else {
01299
01300 slave = p->slaves[x];
01301 }
01302 }
01303 }
01304 }
01305
01306 if (!slave)
01307 useslavenative = 0;
01308 else if (slave->law != p->law) {
01309 useslavenative = 0;
01310 slave = NULL;
01311 }
01312 if (out)
01313 *out = slave;
01314 return useslavenative;
01315 }
01316
01317 static int reset_conf(struct zt_pvt *p)
01318 {
01319 ZT_CONFINFO zi;
01320 memset(&zi, 0, sizeof(zi));
01321 p->confno = -1;
01322 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
01323 if (p->subs[SUB_REAL].zfd > -1) {
01324 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
01325 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
01326 }
01327 return 0;
01328 }
01329
01330 static int update_conf(struct zt_pvt *p)
01331 {
01332 int needconf = 0;
01333 int x;
01334 int useslavenative;
01335 struct zt_pvt *slave = NULL;
01336
01337 useslavenative = isslavenative(p, &slave);
01338
01339 for (x=0;x<3;x++) {
01340
01341 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
01342 conf_add(p, &p->subs[x], x, 0);
01343 needconf++;
01344 } else {
01345 conf_del(p, &p->subs[x], x);
01346 }
01347 }
01348
01349
01350 for (x=0;x<MAX_SLAVES;x++) {
01351 if (p->slaves[x]) {
01352 if (useslavenative)
01353 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
01354 else {
01355 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
01356 needconf++;
01357 }
01358 }
01359 }
01360
01361 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
01362 if (useslavenative)
01363 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
01364 else {
01365 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
01366 needconf++;
01367 }
01368 }
01369
01370 if (p->master) {
01371 if (isslavenative(p->master, NULL)) {
01372 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
01373 } else {
01374 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
01375 }
01376 }
01377 if (!needconf) {
01378
01379
01380 p->confno = -1;
01381 }
01382 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
01383 return 0;
01384 }
01385
01386 static void zt_enable_ec(struct zt_pvt *p)
01387 {
01388 int x;
01389 int res;
01390 if (!p)
01391 return;
01392 if (p->echocanon) {
01393 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
01394 return;
01395 }
01396 if (p->digital) {
01397 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
01398 return;
01399 }
01400 if (p->echocancel) {
01401 if (p->sig == SIG_PRI) {
01402 x = 1;
01403 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
01404 if (res)
01405 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01406 }
01407 x = p->echocancel;
01408 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01409 if (res)
01410 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01411 else {
01412 p->echocanon = 1;
01413 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
01414 }
01415 } else
01416 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
01417 }
01418
01419 static void zt_train_ec(struct zt_pvt *p)
01420 {
01421 int x;
01422 int res;
01423 if (p && p->echocancel && p->echotraining) {
01424 x = p->echotraining;
01425 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
01426 if (res)
01427 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
01428 else {
01429 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
01430 }
01431 } else
01432 ast_log(LOG_DEBUG, "No echo training requested\n");
01433 }
01434
01435 static void zt_disable_ec(struct zt_pvt *p)
01436 {
01437 int x;
01438 int res;
01439 if (p->echocancel) {
01440 x = 0;
01441 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01442 if (res)
01443 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
01444 else
01445 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
01446 }
01447 p->echocanon = 0;
01448 }
01449
01450 static void fill_txgain(struct zt_gains *g, float gain, int law)
01451 {
01452 int j;
01453 int k;
01454 float linear_gain = pow(10.0, gain / 20.0);
01455
01456 switch (law) {
01457 case ZT_LAW_ALAW:
01458 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01459 if (gain) {
01460 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01461 if (k > 32767) k = 32767;
01462 if (k < -32767) k = -32767;
01463 g->txgain[j] = AST_LIN2A(k);
01464 } else {
01465 g->txgain[j] = j;
01466 }
01467 }
01468 break;
01469 case ZT_LAW_MULAW:
01470 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01471 if (gain) {
01472 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01473 if (k > 32767) k = 32767;
01474 if (k < -32767) k = -32767;
01475 g->txgain[j] = AST_LIN2MU(k);
01476 } else {
01477 g->txgain[j] = j;
01478 }
01479 }
01480 break;
01481 }
01482 }
01483
01484 static void fill_rxgain(struct zt_gains *g, float gain, int law)
01485 {
01486 int j;
01487 int k;
01488 float linear_gain = pow(10.0, gain / 20.0);
01489
01490 switch (law) {
01491 case ZT_LAW_ALAW:
01492 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01493 if (gain) {
01494 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01495 if (k > 32767) k = 32767;
01496 if (k < -32767) k = -32767;
01497 g->rxgain[j] = AST_LIN2A(k);
01498 } else {
01499 g->rxgain[j] = j;
01500 }
01501 }
01502 break;
01503 case ZT_LAW_MULAW:
01504 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01505 if (gain) {
01506 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01507 if (k > 32767) k = 32767;
01508 if (k < -32767) k = -32767;
01509 g->rxgain[j] = AST_LIN2MU(k);
01510 } else {
01511 g->rxgain[j] = j;
01512 }
01513 }
01514 break;
01515 }
01516 }
01517
01518 int set_actual_txgain(int fd, int chan, float gain, int law)
01519 {
01520 struct zt_gains g;
01521 int res;
01522
01523 memset(&g, 0, sizeof(g));
01524 g.chan = chan;
01525 res = ioctl(fd, ZT_GETGAINS, &g);
01526 if (res) {
01527 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01528 return res;
01529 }
01530
01531 fill_txgain(&g, gain, law);
01532
01533 return ioctl(fd, ZT_SETGAINS, &g);
01534 }
01535
01536 int set_actual_rxgain(int fd, int chan, float gain, int law)
01537 {
01538 struct zt_gains g;
01539 int res;
01540
01541 memset(&g, 0, sizeof(g));
01542 g.chan = chan;
01543 res = ioctl(fd, ZT_GETGAINS, &g);
01544 if (res) {
01545 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01546 return res;
01547 }
01548
01549 fill_rxgain(&g, gain, law);
01550
01551 return ioctl(fd, ZT_SETGAINS, &g);
01552 }
01553
01554 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
01555 {
01556 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
01557 }
01558
01559 static int bump_gains(struct zt_pvt *p)
01560 {
01561 int res;
01562
01563
01564 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
01565 if (res) {
01566 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
01567 return -1;
01568 }
01569
01570 return 0;
01571 }
01572
01573 static int restore_gains(struct zt_pvt *p)
01574 {
01575 int res;
01576
01577 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01578 if (res) {
01579 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
01580 return -1;
01581 }
01582
01583 return 0;
01584 }
01585
01586 static inline int zt_set_hook(int fd, int hs)
01587 {
01588 int x, res;
01589 x = hs;
01590 res = ioctl(fd, ZT_HOOK, &x);
01591 if (res < 0)
01592 {
01593 if (errno == EINPROGRESS) return 0;
01594 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
01595 }
01596 return res;
01597 }
01598
01599 static inline int zt_confmute(struct zt_pvt *p, int muted)
01600 {
01601 int x, y, res;
01602 x = muted;
01603 if (p->sig == SIG_PRI) {
01604 y = 1;
01605 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
01606 if (res)
01607 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
01608 }
01609 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
01610 if (res < 0)
01611 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
01612 return res;
01613 }
01614
01615 static int save_conference(struct zt_pvt *p)
01616 {
01617 struct zt_confinfo c;
01618 int res;
01619 if (p->saveconf.confmode) {
01620 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
01621 return -1;
01622 }
01623 p->saveconf.chan = 0;
01624 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
01625 if (res) {
01626 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
01627 p->saveconf.confmode = 0;
01628 return -1;
01629 }
01630 c.chan = 0;
01631 c.confno = 0;
01632 c.confmode = ZT_CONF_NORMAL;
01633 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
01634 if (res) {
01635 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
01636 return -1;
01637 }
01638 if (option_debug)
01639 ast_log(LOG_DEBUG, "Disabled conferencing\n");
01640 return 0;
01641 }
01642
01643 static int restore_conference(struct zt_pvt *p)
01644 {
01645 int res;
01646 if (p->saveconf.confmode) {
01647 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
01648 p->saveconf.confmode = 0;
01649 if (res) {
01650 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
01651 return -1;
01652 }
01653 }
01654 if (option_debug)
01655 ast_log(LOG_DEBUG, "Restored conferencing\n");
01656 return 0;
01657 }
01658
01659 static int send_callerid(struct zt_pvt *p);
01660
01661 int send_cwcidspill(struct zt_pvt *p)
01662 {
01663 p->callwaitcas = 0;
01664 p->cidcwexpire = 0;
01665 p->cidspill = malloc(MAX_CALLERID_SIZE);
01666 if (p->cidspill) {
01667 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
01668 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
01669
01670 p->cidlen += READ_SIZE * 4;
01671 p->cidpos = 0;
01672 send_callerid(p);
01673 if (option_verbose > 2)
01674 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
01675 } else return -1;
01676 return 0;
01677 }
01678
01679 static int has_voicemail(struct zt_pvt *p)
01680 {
01681
01682 return ast_app_has_voicemail(p->mailbox, NULL);
01683 }
01684
01685 static int send_callerid(struct zt_pvt *p)
01686 {
01687
01688 int res;
01689
01690 if (p->subs[SUB_REAL].linear) {
01691 p->subs[SUB_REAL].linear = 0;
01692 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
01693 }
01694 while(p->cidpos < p->cidlen) {
01695 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
01696 if (res < 0) {
01697 if (errno == EAGAIN)
01698 return 0;
01699 else {
01700 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
01701 return -1;
01702 }
01703 }
01704 if (!res)
01705 return 0;
01706 p->cidpos += res;
01707 }
01708 free(p->cidspill);
01709 p->cidspill = NULL;
01710 if (p->callwaitcas) {
01711
01712 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
01713 } else
01714 restore_conference(p);
01715 return 0;
01716 }
01717
01718 static int zt_callwait(struct ast_channel *ast)
01719 {
01720 struct zt_pvt *p = ast->tech_pvt;
01721 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
01722 if (p->cidspill) {
01723 ast_log(LOG_WARNING, "Spill already exists?!?\n");
01724 free(p->cidspill);
01725 }
01726 p->cidspill = malloc(2400 + 680 + READ_SIZE * 4);
01727 if (p->cidspill) {
01728 save_conference(p);
01729
01730 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
01731 if (!p->callwaitrings && p->callwaitingcallerid) {
01732 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
01733 p->callwaitcas = 1;
01734 p->cidlen = 2400 + 680 + READ_SIZE * 4;
01735 } else {
01736 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
01737 p->callwaitcas = 0;
01738 p->cidlen = 2400 + READ_SIZE * 4;
01739 }
01740 p->cidpos = 0;
01741 send_callerid(p);
01742 } else {
01743 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
01744 return -1;
01745 }
01746 return 0;
01747 }
01748
01749 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
01750 {
01751 struct zt_pvt *p = ast->tech_pvt;
01752 int x, res, index;
01753 char *c, *n, *l;
01754 #ifdef ZAPATA_PRI
01755 char *s=NULL;
01756 #endif
01757 char dest[256];
01758 ast_mutex_lock(&p->lock);
01759 ast_copy_string(dest, rdest, sizeof(dest));
01760 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
01761 if ((ast->_state == AST_STATE_BUSY)) {
01762 p->subs[SUB_REAL].needbusy = 1;
01763 ast_mutex_unlock(&p->lock);
01764 return 0;
01765 }
01766 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
01767 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
01768 ast_mutex_unlock(&p->lock);
01769 return -1;
01770 }
01771 p->dialednone = 0;
01772 if (p->radio)
01773 {
01774
01775 ast_setstate(ast, AST_STATE_UP);
01776 ast_mutex_unlock(&p->lock);
01777 return 0;
01778 }
01779 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
01780 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
01781 if (res)
01782 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
01783 p->outgoing = 1;
01784
01785 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01786
01787 switch(p->sig) {
01788 case SIG_FXOLS:
01789 case SIG_FXOGS:
01790 case SIG_FXOKS:
01791 if (p->owner == ast) {
01792
01793
01794
01795 p->dialing = 1;
01796 if (p->use_callerid) {
01797
01798 if (p->cidspill) {
01799 ast_log(LOG_WARNING, "cidspill already exists??\n");
01800 free(p->cidspill);
01801 }
01802 p->cidspill = malloc(MAX_CALLERID_SIZE);
01803 p->callwaitcas = 0;
01804 if (p->cidspill) {
01805 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
01806 p->cidpos = 0;
01807 send_callerid(p);
01808 } else
01809 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
01810 }
01811
01812 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
01813 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
01814 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
01815 p->cidrings = cidrings[p->distinctivering - 1];
01816 } else {
01817 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
01818 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
01819 p->cidrings = p->sendcalleridafter;
01820 }
01821
01822
01823
01824 c = strchr(dest, '/');
01825 if (c)
01826 c++;
01827 if (c && (strlen(c) < p->stripmsd)) {
01828 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01829 c = NULL;
01830 }
01831 if (c) {
01832 p->dop.op = ZT_DIAL_OP_REPLACE;
01833 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
01834 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
01835 } else {
01836 p->dop.dialstr[0] = '\0';
01837 }
01838 x = ZT_RING;
01839 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
01840 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
01841 ast_mutex_unlock(&p->lock);
01842 return -1;
01843 }
01844 p->dialing = 1;
01845 } else {
01846
01847 p->callwaitrings = 0;
01848 if (ast->cid.cid_num)
01849 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
01850 else
01851 p->callwait_num[0] = '\0';
01852 if (ast->cid.cid_name)
01853 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
01854 else
01855 p->callwait_name[0] = '\0';
01856
01857 if (zt_callwait(ast)) {
01858 ast_mutex_unlock(&p->lock);
01859 return -1;
01860 }
01861
01862 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
01863 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
01864
01865 }
01866 n = ast->cid.cid_name;
01867 l = ast->cid.cid_num;
01868 if (l)
01869 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
01870 else
01871 p->lastcid_num[0] = '\0';
01872 if (n)
01873 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
01874 else
01875 p->lastcid_name[0] = '\0';
01876 ast_setstate(ast, AST_STATE_RINGING);
01877 index = zt_get_index(ast, p, 0);
01878 if (index > -1) {
01879 p->subs[index].needringing = 1;
01880 }
01881 break;
01882 case SIG_FXSLS:
01883 case SIG_FXSGS:
01884 case SIG_FXSKS:
01885 case SIG_EMWINK:
01886 case SIG_EM:
01887 case SIG_EM_E1:
01888 case SIG_FEATD:
01889 case SIG_FEATDMF:
01890 case SIG_E911:
01891 case SIG_FEATB:
01892 case SIG_SFWINK:
01893 case SIG_SF:
01894 case SIG_SF_FEATD:
01895 case SIG_SF_FEATDMF:
01896 case SIG_FEATDMF_TA:
01897 case SIG_SF_FEATB:
01898 c = strchr(dest, '/');
01899 if (c)
01900 c++;
01901 else
01902 c = "";
01903 if (strlen(c) < p->stripmsd) {
01904 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01905 ast_mutex_unlock(&p->lock);
01906 return -1;
01907 }
01908 #ifdef ZAPATA_PRI
01909
01910 if (!p->pri) {
01911 #endif
01912 x = ZT_START;
01913 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01914 if (res < 0) {
01915 if (errno != EINPROGRESS) {
01916 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
01917 ast_mutex_unlock(&p->lock);
01918 return -1;
01919 }
01920 }
01921 #ifdef ZAPATA_PRI
01922 }
01923 #endif
01924 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
01925 p->dop.op = ZT_DIAL_OP_REPLACE;
01926
01927 c += p->stripmsd;
01928
01929 switch (p->sig) {
01930 case SIG_FEATD:
01931 l = ast->cid.cid_num;
01932 if (l)
01933 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
01934 else
01935 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
01936 break;
01937 case SIG_FEATDMF:
01938 l = ast->cid.cid_num;
01939 if (l)
01940 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
01941 else
01942 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
01943 break;
01944 case SIG_FEATDMF_TA:
01945 {
01946 char *cic = NULL, *ozz = NULL;
01947
01948
01949 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
01950 if (!ozz)
01951 ozz = defaultozz;
01952 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
01953 if (!cic)
01954 cic = defaultcic;
01955 if (!ozz || !cic) {
01956 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
01957 ast_mutex_unlock(&p->lock);
01958 return -1;
01959 }
01960 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
01961 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
01962 p->whichwink = 0;
01963 }
01964 break;
01965 case SIG_E911:
01966 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
01967 break;
01968 case SIG_FEATB:
01969 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
01970 break;
01971 default:
01972 if (p->pulse)
01973 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
01974 else
01975 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
01976 break;
01977 }
01978
01979 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
01980 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
01981 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
01982 p->echorest[sizeof(p->echorest) - 1] = '\0';
01983 p->echobreak = 1;
01984 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
01985 } else
01986 p->echobreak = 0;
01987 if (!res) {
01988 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
01989 x = ZT_ONHOOK;
01990 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01991 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
01992 ast_mutex_unlock(&p->lock);
01993 return -1;
01994 }
01995 } else
01996 ast_log(LOG_DEBUG, "Deferring dialing...\n");
01997 p->dialing = 1;
01998 if (ast_strlen_zero(c))
01999 p->dialednone = 1;
02000 ast_setstate(ast, AST_STATE_DIALING);
02001 break;
02002 case 0:
02003
02004 ast_setstate(ast, AST_STATE_UP);
02005 break;
02006 case SIG_PRI:
02007
02008 p->dialdest[0] = '\0';
02009 break;
02010 default:
02011 ast_log(LOG_DEBUG, "not yet implemented\n");
02012 ast_mutex_unlock(&p->lock);
02013 return -1;
02014 }
02015 #ifdef ZAPATA_PRI
02016 if (p->pri) {
02017 struct pri_sr *sr;
02018 #ifdef SUPPORT_USERUSER
02019 char *useruser;
02020 #endif
02021 int pridialplan;
02022 int dp_strip;
02023 int prilocaldialplan;
02024 int ldp_strip;
02025 int exclusive;
02026
02027 c = strchr(dest, '/');
02028 if (c)
02029 c++;
02030 else
02031 c = dest;
02032 if (!p->hidecallerid) {
02033 l = ast->cid.cid_num;
02034 n = ast->cid.cid_name;
02035 } else {
02036 l = NULL;
02037 n = NULL;
02038 }
02039 if (strlen(c) < p->stripmsd) {
02040 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
02041 ast_mutex_unlock(&p->lock);
02042 return -1;
02043 }
02044 if (p->sig != SIG_FXSKS) {
02045 p->dop.op = ZT_DIAL_OP_REPLACE;
02046 s = strchr(c + p->stripmsd, 'w');
02047 if (s) {
02048 if (strlen(s) > 1)
02049 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
02050 else
02051 p->dop.dialstr[0] = '\0';
02052 *s = '\0';
02053 } else {
02054 p->dop.dialstr[0] = '\0';
02055 }
02056 }
02057 if (pri_grab(p, p->pri)) {
02058 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
02059 ast_mutex_unlock(&p->lock);
02060 return -1;
02061 }
02062 if (!(p->call = pri_new_call(p->pri->pri))) {
02063 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
02064 pri_rel(p->pri);
02065 ast_mutex_unlock(&p->lock);
02066 return -1;
02067 }
02068 if (!(sr = pri_sr_new())) {
02069 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
02070 pri_rel(p->pri);
02071 ast_mutex_unlock(&p->lock);
02072 }
02073 if (p->bearer || (p->sig == SIG_FXSKS)) {
02074 if (p->bearer) {
02075 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);
02076 p->bearer->call = p->call;
02077 } else
02078 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
02079 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
02080 }
02081 p->digital = IS_DIGITAL(ast->transfercapability);
02082
02083 if (p->priexclusive)
02084 exclusive = 1;
02085 else {
02086
02087 if (p->pri->nodetype == PRI_NETWORK)
02088 exclusive = 0;
02089 else
02090 exclusive = 1;
02091 }
02092
02093 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
02094 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
02095 (p->digital ? -1 :
02096 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
02097 if (p->pri->facilityenable)
02098 pri_facility_enable(p->pri->pri);
02099
02100 if (option_verbose > 2)
02101 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
02102 dp_strip = 0;
02103 pridialplan = p->pri->dialplan - 1;
02104 if (pridialplan == -2) {
02105 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02106 dp_strip = strlen(p->pri->internationalprefix);
02107 pridialplan = PRI_INTERNATIONAL_ISDN;
02108 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02109 dp_strip = strlen(p->pri->nationalprefix);
02110 pridialplan = PRI_NATIONAL_ISDN;
02111 } else {
02112 pridialplan = PRI_LOCAL_ISDN;
02113 }
02114 }
02115 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
02116
02117 ldp_strip = 0;
02118 prilocaldialplan = p->pri->localdialplan - 1;
02119 if ((l != NULL) && (prilocaldialplan == -2)) {
02120 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02121 ldp_strip = strlen(p->pri->internationalprefix);
02122 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
02123 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02124 ldp_strip = strlen(p->pri->nationalprefix);
02125 prilocaldialplan = PRI_NATIONAL_ISDN;
02126 } else {
02127 prilocaldialplan = PRI_LOCAL_ISDN;
02128 }
02129 }
02130 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
02131 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
02132 PRES_NUMBER_NOT_AVAILABLE);
02133 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
02134
02135 #ifdef SUPPORT_USERUSER
02136
02137 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
02138
02139 if (useruser)
02140 pri_sr_set_useruser(sr, useruser);
02141 #endif
02142
02143 if (pri_setup(p->pri->pri, p->call, sr)) {
02144 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
02145 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
02146 pri_rel(p->pri);
02147 ast_mutex_unlock(&p->lock);
02148 pri_sr_free(sr);
02149 return -1;
02150 }
02151 pri_sr_free(sr);
02152 ast_setstate(ast, AST_STATE_DIALING);
02153 pri_rel(p->pri);
02154 }
02155 #endif
02156 ast_mutex_unlock(&p->lock);
02157 return 0;
02158 }
02159
02160 static void destroy_zt_pvt(struct zt_pvt **pvt)
02161 {
02162 struct zt_pvt *p = *pvt;
02163
02164 if(p->prev)
02165 p->prev->next = p->next;
02166 if(p->next)
02167 p->next->prev = p->prev;
02168 ast_mutex_destroy(&p->lock);
02169 free(p);
02170 *pvt = NULL;
02171 }
02172
02173 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
02174 {
02175 int owned = 0;
02176 int i = 0;
02177
02178 if (!now) {
02179 if (cur->owner) {
02180 owned = 1;
02181 }
02182
02183 for (i = 0; i < 3; i++) {
02184 if (cur->subs[i].owner) {
02185 owned = 1;
02186 }
02187 }
02188 if (!owned) {
02189 if (prev) {
02190 prev->next = cur->next;
02191 if (prev->next)
02192 prev->next->prev = prev;
02193 else
02194 ifend = prev;
02195 } else {
02196 iflist = cur->next;
02197 if (iflist)
02198 iflist->prev = NULL;
02199 else
02200 ifend = NULL;
02201 }
02202 if (cur->subs[SUB_REAL].zfd > -1) {
02203 zt_close(cur->subs[SUB_REAL].zfd);
02204 }
02205 destroy_zt_pvt(&cur);
02206 }
02207 } else {
02208 if (prev) {
02209 prev->next = cur->next;
02210 if (prev->next)
02211 prev->next->prev = prev;
02212 else
02213 ifend = prev;
02214 } else {
02215 iflist = cur->next;
02216 if (iflist)
02217 iflist->prev = NULL;
02218 else
02219 ifend = NULL;
02220 }
02221 if (cur->subs[SUB_REAL].zfd > -1) {
02222 zt_close(cur->subs[SUB_REAL].zfd);
02223 }
02224 destroy_zt_pvt(&cur);
02225 }
02226 return 0;
02227 }
02228
02229 #ifdef ZAPATA_PRI
02230 int pri_is_up(struct zt_pri *pri)
02231 {
02232 int x;
02233 for (x=0;x<NUM_DCHANS;x++) {
02234 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
02235 return 1;
02236 }
02237 return 0;
02238 }
02239
02240 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
02241 {
02242 bearer->owner = &inuse;
02243 bearer->realcall = crv;
02244 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
02245 if (crv->subs[SUB_REAL].owner)
02246 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
02247 crv->bearer = bearer;
02248 crv->call = bearer->call;
02249 crv->pri = pri;
02250 return 0;
02251 }
02252
02253 static char *pri_order(int level)
02254 {
02255 switch(level) {
02256 case 0:
02257 return "Primary";
02258 case 1:
02259 return "Secondary";
02260 case 2:
02261 return "Tertiary";
02262 case 3:
02263 return "Quaternary";
02264 default:
02265 return "<Unknown>";
02266 }
02267 }
02268
02269
02270 int pri_active_dchan_fd(struct zt_pri *pri)
02271 {
02272 int x = -1;
02273
02274 for (x = 0; x < NUM_DCHANS; x++) {
02275 if ((pri->dchans[x] == pri->pri))
02276 break;
02277 }
02278
02279 return pri->fds[x];
02280 }
02281
02282 int pri_find_dchan(struct zt_pri *pri)
02283 {
02284 int oldslot = -1;
02285 struct pri *old;
02286 int newslot = -1;
02287 int x;
02288 old = pri->pri;
02289 for(x=0;x<NUM_DCHANS;x++) {
02290 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
02291 newslot = x;
02292 if (pri->dchans[x] == old) {
02293 oldslot = x;
02294 }
02295 }
02296 if (newslot < 0) {
02297 newslot = 0;
02298 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
02299 pri->dchannels[newslot]);
02300 }
02301 if (old && (oldslot != newslot))
02302 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
02303 pri->dchannels[oldslot], pri->dchannels[newslot]);
02304 pri->pri = pri->dchans[newslot];
02305 return 0;
02306 }
02307 #endif
02308
02309 static int zt_hangup(struct ast_channel *ast)
02310 {
02311 int res;
02312 int index,x, law;
02313
02314 struct zt_pvt *p = ast->tech_pvt;
02315 struct zt_pvt *tmp = NULL;
02316 struct zt_pvt *prev = NULL;
02317 ZT_PARAMS par;
02318
02319 if (option_debug)
02320 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
02321 if (!ast->tech_pvt) {
02322 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
02323 return 0;
02324 }
02325
02326 ast_mutex_lock(&p->lock);
02327
02328 index = zt_get_index(ast, p, 1);
02329
02330 if (p->sig == SIG_PRI) {
02331 x = 1;
02332 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02333 }
02334
02335 x = 0;
02336 zt_confmute(p, 0);
02337 restore_gains(p);
02338 if (p->origcid_num) {
02339 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
02340 free(p->origcid_num);
02341 p->origcid_num = NULL;
02342 }
02343 if (p->origcid_name) {
02344 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
02345 free(p->origcid_name);
02346 p->origcid_name = NULL;
02347 }
02348 if (p->dsp)
02349 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02350 if (p->exten)
02351 p->exten[0] = '\0';
02352
02353 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
02354 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
02355 p->ignoredtmf = 0;
02356
02357 if (index > -1) {
02358
02359 p->subs[index].owner = NULL;
02360 p->subs[index].needanswer = 0;
02361 p->subs[index].needflash = 0;
02362 p->subs[index].needringing = 0;
02363 p->subs[index].needbusy = 0;
02364 p->subs[index].needcongestion = 0;
02365 p->subs[index].linear = 0;
02366 p->subs[index].needcallerid = 0;
02367 p->polarity = POLARITY_IDLE;
02368 zt_setlinear(p->subs[index].zfd, 0);
02369 if (index == SUB_REAL) {
02370 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
02371 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
02372 if (p->subs[SUB_CALLWAIT].inthreeway) {
02373
02374 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
02375
02376 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02377 unalloc_sub(p, SUB_CALLWAIT);
02378 p->owner = NULL;
02379 } else {
02380
02381 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
02382 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02383 unalloc_sub(p, SUB_THREEWAY);
02384 if (p->subs[SUB_REAL].inthreeway) {
02385
02386
02387 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02388 p->owner = p->subs[SUB_REAL].owner;
02389 } else {
02390
02391 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02392 p->owner = NULL;
02393 }
02394 p->subs[SUB_REAL].inthreeway = 0;
02395 }
02396 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
02397
02398 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02399 unalloc_sub(p, SUB_CALLWAIT);
02400 p->owner = p->subs[SUB_REAL].owner;
02401 if (p->owner->_state != AST_STATE_UP)
02402 p->subs[SUB_REAL].needanswer = 1;
02403 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
02404 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
02405 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
02406 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02407 unalloc_sub(p, SUB_THREEWAY);
02408 if (p->subs[SUB_REAL].inthreeway) {
02409
02410
02411 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02412 p->owner = p->subs[SUB_REAL].owner;
02413 } else {
02414
02415 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02416 p->owner = NULL;
02417 }
02418 p->subs[SUB_REAL].inthreeway = 0;
02419 }
02420 } else if (index == SUB_CALLWAIT) {
02421
02422 if (p->subs[SUB_CALLWAIT].inthreeway) {
02423
02424
02425 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
02426 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
02427 p->subs[SUB_THREEWAY].inthreeway = 0;
02428
02429 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
02430 unalloc_sub(p, SUB_THREEWAY);
02431 } else
02432 unalloc_sub(p, SUB_CALLWAIT);
02433 } else if (index == SUB_THREEWAY) {
02434 if (p->subs[SUB_CALLWAIT].inthreeway) {
02435
02436
02437 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
02438 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
02439 p->subs[SUB_CALLWAIT].inthreeway = 0;
02440 }
02441 p->subs[SUB_REAL].inthreeway = 0;
02442
02443
02444 unalloc_sub(p, SUB_THREEWAY);
02445 } else {
02446
02447 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
02448 }
02449 }
02450
02451
02452 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
02453 p->owner = NULL;
02454 p->ringt = 0;
02455 p->distinctivering = 0;
02456 p->confirmanswer = 0;
02457 p->cidrings = 1;
02458 p->outgoing = 0;
02459 p->digital = 0;
02460 p->faxhandled = 0;
02461 p->pulsedial = 0;
02462 p->onhooktime = time(NULL);
02463 #ifdef ZAPATA_PRI
02464 p->proceeding = 0;
02465 p->progress = 0;
02466 p->alerting = 0;
02467 p->setup_ack = 0;
02468 #endif
02469 if (p->dsp) {
02470 ast_dsp_free(p->dsp);
02471 p->dsp = NULL;
02472 }
02473
02474 law = ZT_LAW_DEFAULT;
02475 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
02476 if (res < 0)
02477 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
02478
02479 #ifdef ZAPATA_PRI
02480 if (p->pri) {
02481 #ifdef SUPPORT_USERUSER
02482 char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
02483 #endif
02484
02485
02486 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
02487 if (!pri_grab(p, p->pri)) {
02488 if (p->alreadyhungup) {
02489 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
02490
02491 #ifdef SUPPORT_USERUSER
02492 pri_call_set_useruser(p->call, useruser);
02493 #endif
02494
02495 pri_hangup(p->pri->pri, p->call, -1);
02496 p->call = NULL;
02497 if (p->bearer)
02498 p->bearer->call = NULL;
02499 } else {
02500 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
02501 int icause = ast->hangupcause ? ast->hangupcause : -1;
02502 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
02503
02504 #ifdef SUPPORT_USERUSER
02505 pri_call_set_useruser(p->call, useruser);
02506 #endif
02507
02508 p->alreadyhungup = 1;
02509 if (p->bearer)
02510 p->bearer->alreadyhungup = 1;
02511 if (cause) {
02512 if (atoi(cause))
02513 icause = atoi(cause);
02514 }
02515 pri_hangup(p->pri->pri, p->call, icause);
02516 }
02517 if (res < 0)
02518 ast_log(LOG_WARNING, "pri_disconnect failed\n");
02519 pri_rel(p->pri);
02520 } else {
02521 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02522 res = -1;
02523 }
02524 } else {
02525 if (p->bearer)
02526 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
02527 p->call = NULL;
02528 res = 0;
02529 }
02530 }
02531 #endif
02532 #ifdef ZAPATA_R2
02533 if (p->sig == SIG_R2) {
02534 if (p->hasr2call) {
02535 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
02536 p->hasr2call = 0;
02537 res = 0;
02538 } else
02539 res = 0;
02540
02541 }
02542 #endif
02543 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
02544 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
02545 if (res < 0) {
02546 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
02547 }
02548 switch(p->sig) {
02549 case SIG_FXOGS:
02550 case SIG_FXOLS:
02551 case SIG_FXOKS:
02552 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
02553 if (!res) {
02554 #if 0
02555 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
02556 #endif
02557
02558 if ((par.rxisoffhook) && (!p->radio))
02559 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
02560 else
02561 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02562 }
02563 break;
02564 case SIG_FXSGS:
02565 case SIG_FXSLS:
02566 case SIG_FXSKS:
02567
02568
02569 if (ast->_state != AST_STATE_RESERVED) {
02570 time(&p->guardtime);
02571 p->guardtime += 2;
02572 }
02573 break;
02574 default:
02575 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02576 }
02577 if (p->cidspill)
02578 free(p->cidspill);
02579 if (p->sig)
02580 zt_disable_ec(p);
02581 x = 0;
02582 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
02583 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
02584 p->didtdd = 0;
02585 p->cidspill = NULL;
02586 p->callwaitcas = 0;
02587 p->callwaiting = p->permcallwaiting;
02588 p->hidecallerid = p->permhidecallerid;
02589 p->dialing = 0;
02590 p->rdnis[0] = '\0';
02591 update_conf(p);
02592 reset_conf(p);
02593
02594 if (p->sig == SIG_PRI) {
02595 x = 0;
02596 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02597 }
02598 #ifdef ZAPATA_PRI
02599 if (p->bearer) {
02600 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
02601
02602
02603 update_conf(p->bearer);
02604 reset_conf(p->bearer);
02605 p->bearer->owner = NULL;
02606 p->bearer->realcall = NULL;
02607 p->bearer = NULL;
02608 p->subs[SUB_REAL].zfd = -1;
02609 p->pri = NULL;
02610 }
02611 #endif
02612 restart_monitor();
02613 }
02614
02615
02616 p->callwaitingrepeat = 0;
02617 p->cidcwexpire = 0;
02618 ast->tech_pvt = NULL;
02619 ast_mutex_unlock(&p->lock);
02620 ast_mutex_lock(&usecnt_lock);
02621 usecnt--;
02622 if (usecnt < 0)
02623 ast_log(LOG_WARNING, "Usecnt < 0???\n");
02624 ast_mutex_unlock(&usecnt_lock);
02625 ast_update_use_count();
02626 if (option_verbose > 2)
02627 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
02628
02629 ast_mutex_lock(&iflock);
02630 tmp = iflist;
02631 prev = NULL;
02632 if (p->destroy) {
02633 while (tmp) {
02634 if (tmp == p) {
02635 destroy_channel(prev, tmp, 0);
02636 break;
02637 } else {
02638 prev = tmp;
02639 tmp = tmp->next;
02640 }
02641 }
02642 }
02643 ast_mutex_unlock(&iflock);
02644 return 0;
02645 }
02646
02647 static int zt_answer(struct ast_channel *ast)
02648 {
02649 struct zt_pvt *p = ast->tech_pvt;
02650 int res=0;
02651 int index;
02652 int oldstate = ast->_state;
02653 ast_setstate(ast, AST_STATE_UP);
02654 ast_mutex_lock(&p->lock);
02655 index = zt_get_index(ast, p, 0);
02656 if (index < 0)
02657 index = SUB_REAL;
02658
02659 if (p->radio) {
02660 ast_mutex_unlock(&p->lock);
02661 return 0;
02662 }
02663 switch(p->sig) {
02664 case SIG_FXSLS:
02665 case SIG_FXSGS:
02666 case SIG_FXSKS:
02667 p->ringt = 0;
02668
02669 case SIG_EM:
02670 case SIG_EM_E1:
02671 case SIG_EMWINK:
02672 case SIG_FEATD:
02673 case SIG_FEATDMF:
02674 case SIG_E911:
02675 case SIG_FEATB:
02676 case SIG_SF:
02677 case SIG_SFWINK:
02678 case SIG_SF_FEATD:
02679 case SIG_SF_FEATDMF:
02680 case SIG_SF_FEATB:
02681 case SIG_FXOLS:
02682 case SIG_FXOGS:
02683 case SIG_FXOKS:
02684
02685 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
02686 if(p->hanguponpolarityswitch) {
02687 gettimeofday(&p->polaritydelaytv, NULL);
02688 }
02689 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
02690 tone_zone_play_tone(p->subs[index].zfd, -1);
02691 p->dialing = 0;
02692 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
02693 if (oldstate == AST_STATE_RINGING) {
02694 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
02695 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
02696 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02697 p->owner = p->subs[SUB_REAL].owner;
02698 }
02699 }
02700 if (p->sig & __ZT_SIG_FXS) {
02701 zt_enable_ec(p);
02702 zt_train_ec(p);
02703 }
02704 break;
02705 #ifdef ZAPATA_PRI
02706 case SIG_PRI:
02707
02708 if (!pri_grab(p, p->pri)) {
02709 p->proceeding = 1;
02710 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
02711 pri_rel(p->pri);
02712 } else {
02713 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02714 res= -1;
02715 }
02716 break;
02717 #endif
02718 #ifdef ZAPATA_R2
02719 case SIG_R2:
02720 res = mfcr2_AnswerCall(p->r2, NULL);
02721 if (res)
02722 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
02723 break;
02724 #endif
02725 case 0:
02726 ast_mutex_unlock(&p->lock);
02727 return 0;
02728 default:
02729 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
02730 res = -1;
02731 }
02732 ast_mutex_unlock(&p->lock);
02733 return res;
02734 }
02735
02736 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
02737 {
02738 char *cp;
02739 signed char *scp;
02740 int x;
02741 int index;
02742 struct zt_pvt *p = chan->tech_pvt;
02743
02744
02745 if (!data || (datalen < 1)) {
02746 errno = EINVAL;
02747 return -1;
02748 }
02749
02750 switch(option) {
02751 case AST_OPTION_TXGAIN:
02752 scp = (signed char *) data;
02753 index = zt_get_index(chan, p, 0);
02754 if (index < 0) {
02755 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
02756 return -1;
02757 }
02758 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
02759 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
02760 case AST_OPTION_RXGAIN:
02761 scp = (signed char *) data;
02762 index = zt_get_index(chan, p, 0);
02763 if (index < 0) {
02764 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
02765 return -1;
02766 }
02767 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
02768 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
02769 case AST_OPTION_TONE_VERIFY:
02770 if (!p->dsp)
02771 break;
02772 cp = (char *) data;
02773 switch (*cp) {
02774 case 1:
02775 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
02776 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax);
02777 break;
02778 case 2:
02779 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
02780 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax);
02781 break;
02782 default:
02783 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
02784 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02785 break;
02786 }
02787 break;
02788 case AST_OPTION_TDD:
02789
02790 cp = (char *) data;
02791 p->mate = 0;
02792 if (!*cp) {
02793 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
02794 if (p->tdd) tdd_free(p->tdd);
02795 p->tdd = 0;
02796 break;
02797 }
02798 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
02799 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
02800 zt_disable_ec(p);
02801
02802 if (!p->didtdd) {
02803 unsigned char mybuf[41000],*buf;
02804 int size,res,fd,len;
02805 struct pollfd fds[1];
02806
02807 buf = mybuf;
02808 memset(buf, 0x7f, sizeof(mybuf));
02809 ast_tdd_gen_ecdisa(buf + 16000, 16000);
02810 len = 40000;
02811 index = zt_get_index(chan, p, 0);
02812 if (index < 0) {
02813 ast_log(LOG_WARNING, "No index in TDD?\n");
02814 return -1;
02815 }
02816 fd = p->subs[index].zfd;
02817 while(len) {
02818 if (ast_check_hangup(chan)) return -1;
02819 size = len;
02820 if (size > READ_SIZE)
02821 size = READ_SIZE;
02822 fds[0].fd = fd;
02823 fds[0].events = POLLPRI | POLLOUT;
02824 fds[0].revents = 0;
02825 res = poll(fds, 1, -1);
02826 if (!res) {
02827 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
02828 continue;
02829 }
02830
02831 if (fds[0].revents & POLLPRI) return -1;
02832 if (!(fds[0].revents & POLLOUT)) {
02833 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
02834 continue;
02835 }
02836 res = write(fd, buf, size);
02837 if (res != size) {
02838 if (res == -1) return -1;
02839 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
02840 break;
02841 }
02842 len -= size;
02843 buf += size;
02844 }
02845 p->didtdd = 1;
02846 }
02847 if (*cp == 2) {
02848 if (p->tdd) tdd_free(p->tdd);
02849 p->tdd = 0;
02850 p->mate = 1;
02851 break;
02852 }
02853 if (!p->tdd) {
02854 p->tdd = tdd_new();
02855 }
02856 break;
02857 case AST_OPTION_RELAXDTMF:
02858 if (!p->dsp)
02859 break;
02860 cp = (char *) data;
02861 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
02862 *cp ? "ON" : "OFF", (int) *cp, chan->name);
02863 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
02864 break;
02865 case AST_OPTION_AUDIO_MODE:
02866 cp = (char *) data;
02867 if (!*cp) {
02868 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
02869 x = 0;
02870 zt_disable_ec(p);
02871 } else {
02872 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
02873 x = 1;
02874 }
02875 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
02876 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
02877 break;
02878 }
02879 errno = 0;
02880
02881 return 0;
02882 }
02883
02884 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
02885 {
02886
02887 int x;
02888 int hasslaves;
02889 if (!master)
02890 return;
02891 if (needlock) {
02892 ast_mutex_lock(&master->lock);
02893 if (slave) {
02894 while(ast_mutex_trylock(&slave->lock)) {
02895 ast_mutex_unlock(&master->lock);
02896 usleep(1);
02897 ast_mutex_lock(&master->lock);
02898 }
02899 }
02900 }
02901 hasslaves = 0;
02902 for (x=0;x<MAX_SLAVES;x++) {
02903 if (master->slaves[x]) {
02904 if (!slave || (master->slaves[x] == slave)) {
02905
02906 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
02907 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
02908 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
02909 master->slaves[x]->master = NULL;
02910 master->slaves[x] = NULL;
02911 } else
02912 hasslaves = 1;
02913 }
02914 if (!hasslaves)
02915 master->inconference = 0;
02916 }
02917 if (!slave) {
02918 if (master->master) {
02919
02920 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
02921 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
02922 hasslaves = 0;
02923 for (x=0;x<MAX_SLAVES;x++) {
02924 if (master->master->slaves[x] == master)
02925 master->master->slaves[x] = NULL;
02926 else if (master->master->slaves[x])
02927 hasslaves = 1;
02928 }
02929 if (!hasslaves)
02930 master->master->inconference = 0;
02931 }
02932 master->master = NULL;
02933 }
02934 update_conf(master);
02935 if (needlock) {
02936 if (slave)
02937 ast_mutex_unlock(&slave->lock);
02938 ast_mutex_unlock(&master->lock);
02939 }
02940 }
02941
02942 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
02943 int x;
02944 if (!slave || !master) {
02945 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
02946 return;
02947 }
02948 for (x=0;x<MAX_SLAVES;x++) {
02949 if (!master->slaves[x]) {
02950 master->slaves[x] = slave;
02951 break;
02952 }
02953 }
02954 if (x >= MAX_SLAVES) {
02955 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
02956 master->slaves[MAX_SLAVES - 1] = slave;
02957 }
02958 if (slave->master)
02959 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
02960 slave->master = master;
02961
02962 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
02963 }
02964
02965 static void disable_dtmf_detect(struct zt_pvt *p)
02966 {
02967 #ifdef ZT_TONEDETECT
02968 int val;
02969 #endif
02970
02971 p->ignoredtmf = 1;
02972
02973 #ifdef ZT_TONEDETECT
02974 val = 0;
02975 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
02976 #endif
02977 if (!p->hardwaredtmf && p->dsp) {
02978 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
02979 ast_dsp_set_features(p->dsp, p->dsp_features);
02980 }
02981 }
02982
02983 static void enable_dtmf_detect(struct zt_pvt *p)
02984 {
02985 #ifdef ZT_TONEDETECT
02986 int val;
02987 #endif
02988
02989 if (p->channel == CHAN_PSEUDO)
02990 return;
02991
02992 p->ignoredtmf = 0;
02993
02994 #ifdef ZT_TONEDETECT
02995 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
02996 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
02997 #endif
02998 if (!p->hardwaredtmf && p->dsp) {
02999 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
03000 ast_dsp_set_features(p->dsp, p->dsp_features);
03001 }
03002 }
03003
03004 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)
03005 {
03006 struct ast_channel *who;
03007 struct zt_pvt *p0, *p1, *op0, *op1;
03008 struct zt_pvt *master = NULL, *slave = NULL;
03009 struct ast_frame *f;
03010 int inconf = 0;
03011 int nothingok = 1;
03012 int ofd0, ofd1;
03013 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
03014 int os0 = -1, os1 = -1;
03015 int priority = 0;
03016 struct ast_channel *oc0, *oc1;
03017 enum ast_bridge_result res;
03018
03019 #ifdef PRI_2BCT
03020 int triedtopribridge = 0;
03021 q931_call *q931c0 = NULL, *q931c1 = NULL;
03022 #endif
03023
03024
03025
03026
03027
03028
03029 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
03030 return AST_BRIDGE_FAILED_NOWARN;
03031
03032 ast_mutex_lock(&c0->lock);
03033 ast_mutex_lock(&c1->lock);
03034
03035 p0 = c0->tech_pvt;
03036 p1 = c1->tech_pvt;
03037
03038 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
03039 ast_mutex_unlock(&c0->lock);
03040 ast_mutex_unlock(&c1->lock);
03041 return AST_BRIDGE_FAILED_NOWARN;
03042 }
03043
03044 oi0 = zt_get_index(c0, p0, 0);
03045 oi1 = zt_get_index(c1, p1, 0);
03046 if ((oi0 < 0) || (oi1 < 0)) {
03047 ast_mutex_unlock(&c0->lock);
03048 ast_mutex_unlock(&c1->lock);
03049 return AST_BRIDGE_FAILED;
03050 }
03051
03052 op0 = p0 = c0->tech_pvt;
03053 op1 = p1 = c1->tech_pvt;
03054 ofd0 = c0->fds[0];
03055 ofd1 = c1->fds[0];
03056 oc0 = p0->owner;
03057 oc1 = p1->owner;
03058
03059 ast_mutex_lock(&p0->lock);
03060 if (ast_mutex_trylock(&p1->lock)) {
03061
03062 ast_mutex_unlock(&p0->lock);
03063 ast_mutex_unlock(&c0->lock);
03064 ast_mutex_unlock(&c1->lock);
03065 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03066 return AST_BRIDGE_RETRY;
03067 }
03068
03069 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03070 if (p0->owner && p1->owner) {
03071
03072 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
03073 master = p0;
03074 slave = p1;
03075 inconf = 1;
03076 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
03077 master = p1;
03078 slave = p0;
03079 inconf = 1;
03080 } else {
03081 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
03082 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
03083 p0->channel,
03084 oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03085 p0->subs[SUB_REAL].inthreeway, p0->channel,
03086 oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03087 p1->subs[SUB_REAL].inthreeway);
03088 }
03089 nothingok = 0;
03090 }
03091 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
03092 if (p1->subs[SUB_THREEWAY].inthreeway) {
03093 master = p1;
03094 slave = p0;
03095 nothingok = 0;
03096 }
03097 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
03098 if (p0->subs[SUB_THREEWAY].inthreeway) {
03099 master = p0;
03100 slave = p1;
03101 nothingok = 0;
03102 }
03103 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
03104
03105
03106 if (p1->subs[SUB_CALLWAIT].inthreeway) {
03107 master = p1;
03108 slave = p0;
03109 nothingok = 0;
03110 }
03111 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
03112
03113 if (p0->subs[SUB_CALLWAIT].inthreeway) {
03114 master = p0;
03115 slave = p1;
03116 nothingok = 0;
03117 }
03118 }
03119 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
03120 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
03121 if (master && slave) {
03122
03123
03124
03125 if ((oi1 == SUB_THREEWAY) &&
03126 p1->subs[SUB_THREEWAY].inthreeway &&
03127 p1->subs[SUB_REAL].owner &&
03128 p1->subs[SUB_REAL].inthreeway &&
03129 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03130 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
03131 tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE);
03132 os1 = p1->subs[SUB_REAL].owner->_state;
03133 } else {
03134 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
03135 tone_zone_play_tone(p0->subs[oi0].zfd, -1);
03136 }
03137 if ((oi0 == SUB_THREEWAY) &&
03138 p0->subs[SUB_THREEWAY].inthreeway &&
03139 p0->subs[SUB_REAL].owner &&
03140 p0->subs[SUB_REAL].inthreeway &&
03141 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03142 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
03143 tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE);
03144 os0 = p0->subs[SUB_REAL].owner->_state;
03145 } else {
03146 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
03147 tone_zone_play_tone(p1->subs[oi0].zfd, -1);
03148 }
03149 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03150 if (!p0->echocanbridged || !p1->echocanbridged) {
03151
03152 zt_disable_ec(p0);
03153 zt_disable_ec(p1);
03154 }
03155 }
03156 zt_link(slave, master);
03157 master->inconference = inconf;
03158 } else if (!nothingok)
03159 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
03160
03161 update_conf(p0);
03162 update_conf(p1);
03163 t0 = p0->subs[SUB_REAL].inthreeway;
03164 t1 = p1->subs[SUB_REAL].inthreeway;
03165
03166 ast_mutex_unlock(&p0->lock);
03167 ast_mutex_unlock(&p1->lock);
03168
03169 ast_mutex_unlock(&c0->lock);
03170 ast_mutex_unlock(&c1->lock);
03171
03172
03173 if ((!master || !slave) && !nothingok) {
03174 zt_enable_ec(p0);
03175 zt_enable_ec(p1);
03176 return AST_BRIDGE_FAILED;
03177 }
03178
03179 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03180 disable_dtmf_detect(op0);
03181
03182 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03183 disable_dtmf_detect(op1);
03184
03185 for (;;) {
03186 struct ast_channel *c0_priority[2] = {c0, c1};
03187 struct ast_channel *c1_priority[2] = {c1, c0};
03188
03189
03190
03191 ast_mutex_lock(&c0->lock);
03192 ast_mutex_lock(&c1->lock);
03193 p0 = c0->tech_pvt;
03194 p1 = c1->tech_pvt;
03195
03196 if (op0 == p0)
03197 i0 = zt_get_index(c0, p0, 1);
03198 if (op1 == p1)
03199 i1 = zt_get_index(c1, p1, 1);
03200 ast_mutex_unlock(&c0->lock);
03201 ast_mutex_unlock(&c1->lock);
03202
03203 if (!timeoutms ||
03204 (op0 != p0) ||
03205 (op1 != p1) ||
03206 (ofd0 != c0->fds[0]) ||
03207 (ofd1 != c1->fds[0]) ||
03208 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
03209 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
03210 (oc0 != p0->owner) ||
03211 (oc1 != p1->owner) ||
03212 (t0 != p0->subs[SUB_REAL].inthreeway) ||
03213 (t1 != p1->subs[SUB_REAL].inthreeway) ||
03214 (oi0 != i0) ||
03215 (oi1 != i1)) {
03216 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
03217 op0->channel, oi0, op1->channel, oi1);
03218 res = AST_BRIDGE_RETRY;
03219 goto return_from_bridge;
03220 }
03221
03222 #ifdef PRI_2BCT
03223 q931c0 = p0->call;
03224 q931c1 = p1->call;
03225 if (p0->transfer && p1->transfer
03226 && q931c0 && q931c1
03227 && !triedtopribridge) {
03228 pri_channel_bridge(q931c0, q931c1);
03229 triedtopribridge = 1;
03230 }
03231 #endif
03232
03233 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
03234 if (!who) {
03235 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
03236 continue;
03237 }
03238 f = ast_read(who);
03239 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
03240 *fo = f;
03241 *rc = who;
03242 res = AST_BRIDGE_COMPLETE;
03243 goto return_from_bridge;
03244 }
03245 if (f->frametype == AST_FRAME_DTMF) {
03246 if ((who == c0) && p0->pulsedial) {
03247 ast_write(c1, f);
03248 } else if ((who == c1) && p1->pulsedial) {
03249 ast_write(c0, f);
03250 } else {
03251 *fo = f;
03252 *rc = who;
03253 res = AST_BRIDGE_COMPLETE;
03254 goto return_from_bridge;
03255 }
03256 }
03257 ast_frfree(f);
03258
03259
03260 priority = !priority;
03261 }
03262
03263 return_from_bridge:
03264 if (op0 == p0)
03265 zt_enable_ec(p0);
03266
03267 if (op1 == p1)
03268 zt_enable_ec(p1);
03269
03270 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03271 enable_dtmf_detect(op0);
03272
03273 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03274 enable_dtmf_detect(op1);
03275
03276 zt_unlink(slave, master, 1);
03277
03278 return res;
03279 }
03280
03281 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03282 {
03283 struct zt_pvt *p = newchan->tech_pvt;
03284 int x;
03285 ast_mutex_lock(&p->lock);
03286 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
03287 if (p->owner == oldchan) {
03288 p->owner = newchan;
03289 }
03290 for (x=0;x<3;x++)
03291 if (p->subs[x].owner == oldchan) {
03292 if (!x)
03293 zt_unlink(NULL, p, 0);
03294 p->subs[x].owner = newchan;
03295 }
03296 if (newchan->_state == AST_STATE_RINGING)
03297 zt_indicate(newchan, AST_CONTROL_RINGING);
03298 update_conf(p);
03299 ast_mutex_unlock(&p->lock);
03300 return 0;
03301 }
03302
03303 static int zt_ring_phone(struct zt_pvt *p)
03304 {
03305 int x;
03306 int res;
03307
03308 x = 0;
03309 x = ZT_ONHOOK;
03310 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03311 do {
03312 x = ZT_RING;
03313 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03314 #if 0
03315 printf("Res: %d, error: %s\n", res, strerror(errno));
03316 #endif
03317 if (res) {
03318 switch(errno) {
03319 case EBUSY:
03320 case EINTR:
03321
03322 usleep(10000);
03323 continue;
03324 case EINPROGRESS:
03325 res = 0;
03326 break;
03327 default:
03328 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
03329 res = 0;
03330 }
03331 }
03332 } while (res);
03333 return res;
03334 }
03335
03336 static void *ss_thread(void *data);
03337
03338 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
03339
03340 static int attempt_transfer(struct zt_pvt *p)
03341 {
03342
03343
03344
03345 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
03346
03347
03348 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
03349 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
03350 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
03351 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
03352 }
03353 if (p->subs[SUB_REAL].owner->cdr) {
03354
03355 p->subs[SUB_THREEWAY].owner->cdr =
03356 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
03357 p->subs[SUB_REAL].owner->cdr = NULL;
03358 }
03359 if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
03360
03361 p->subs[SUB_THREEWAY].owner->cdr =
03362 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
03363 ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
03364 }
03365 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
03366 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03367 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
03368 return -1;
03369 }
03370
03371 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03372 unalloc_sub(p, SUB_THREEWAY);
03373 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
03374 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
03375 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
03376 }
03377 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
03378 if (p->subs[SUB_THREEWAY].owner->cdr) {
03379
03380 p->subs[SUB_REAL].owner->cdr =
03381 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
03382 p->subs[SUB_THREEWAY].owner->cdr = NULL;
03383 }
03384 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr) {
03385
03386 p->subs[SUB_REAL].owner->cdr =
03387 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr);
03388 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr = NULL;
03389 }
03390 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
03391 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03392 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
03393 return -1;
03394 }
03395
03396 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03397 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
03398 unalloc_sub(p, SUB_THREEWAY);
03399
03400 return 1;
03401 } else {
03402 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
03403 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
03404 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03405 return -1;
03406 }
03407 return 0;
03408 }
03409
03410 #ifdef ZAPATA_R2
03411 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
03412 {
03413 struct ast_frame *f;
03414 f = &p->subs[index].f;
03415 if (!p->r2) {
03416 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
03417 return NULL;
03418 }
03419 switch(e->e) {
03420 case MFCR2_EVENT_BLOCKED:
03421 if (option_verbose > 2)
03422 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
03423 break;
03424 case MFCR2_EVENT_UNBLOCKED:
03425 if (option_verbose > 2)
03426 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
03427 break;
03428 case MFCR2_EVENT_CONFIG_ERR:
03429 if (option_verbose > 2)
03430 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
03431 break;
03432 case MFCR2_EVENT_RING:
03433 if (option_verbose > 2)
03434 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
03435 break;
03436 case MFCR2_EVENT_HANGUP:
03437 if (option_verbose > 2)
03438 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
03439 break;
03440 case MFCR2_EVENT_RINGING:
03441 if (option_verbose > 2)
03442 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
03443 break;
03444 case MFCR2_EVENT_ANSWER:
03445 if (option_verbose > 2)
03446 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
03447 break;
03448 case MFCR2_EVENT_HANGUP_ACK:
03449 if (option_verbose > 2)
03450 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
03451 break;
03452 case MFCR2_EVENT_IDLE:
03453 if (option_verbose > 2)
03454 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
03455 break;
03456 default:
03457 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
03458 break;
03459 }
03460 return f;
03461 }
03462
03463 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
03464 {
03465 int x;
03466 int res;
03467 mfcr2_event_t *e;
03468 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
03469 if (res) {
03470 ast_log(LOG_WARNING, "Unable to check received bits\n");
03471 return NULL;
03472 }
03473 if (!p->r2) {
03474 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
03475 return NULL;
03476 }
03477 e = mfcr2_cas_signaling_event(p->r2, x);
03478 return e;
03479 }
03480 #endif
03481
03482 static int check_for_conference(struct zt_pvt *p)
03483 {
03484 ZT_CONFINFO ci;
03485
03486 if (p->master || (p->confno > -1))
03487 return 0;
03488 memset(&ci, 0, sizeof(ci));
03489 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
03490 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
03491 return 0;
03492 }
03493
03494
03495
03496 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
03497 if (option_verbose > 2)
03498 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
03499 return 1;
03500 }
03501 return 0;
03502 }
03503
03504 static int get_alarms(struct zt_pvt *p)
03505 {
03506 int res;
03507 ZT_SPANINFO zi;
03508 memset(&zi, 0, sizeof(zi));
03509 zi.spanno = p->span;
03510 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
03511 if (res < 0) {
03512 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
03513 return 0;
03514 }
03515 return zi.alarms;
03516 }
03517
03518 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
03519 {
03520 int res,x;
03521 int index;
03522 char *c;
03523 struct zt_pvt *p = ast->tech_pvt;
03524 pthread_t threadid;
03525 pthread_attr_t attr;
03526 struct ast_channel *chan;
03527
03528 pthread_attr_init(&attr);
03529 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
03530
03531 index = zt_get_index(ast, p, 0);
03532 p->subs[index].f.frametype = AST_FRAME_NULL;
03533 p->subs[index].f.datalen = 0;
03534 p->subs[index].f.samples = 0;
03535 p->subs[index].f.mallocd = 0;
03536 p->subs[index].f.offset = 0;
03537 p->subs[index].f.src = "zt_handle_event";
03538 p->subs[index].f.data = NULL;
03539 if (index < 0)
03540 return &p->subs[index].f;
03541 if (p->fake_event) {
03542 res = p->fake_event;
03543 p->fake_event = 0;
03544 } else
03545 res = zt_get_event(p->subs[index].zfd);
03546
03547 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
03548
03549 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFUP)) {
03550 if (res & ZT_EVENT_PULSEDIGIT)
03551 p->pulsedial = 1;
03552 else
03553 p->pulsedial = 0;
03554 ast_log(LOG_DEBUG, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse ": "", res & 0xff);
03555 #ifdef ZAPATA_PRI
03556 if (!p->proceeding && p->sig == SIG_PRI && p->pri && p->pri->overlapdial) {
03557 p->subs[index].f.frametype = AST_FRAME_NULL;
03558 p->subs[index].f.subclass = 0;
03559 } else {
03560 #endif
03561 p->subs[index].f.frametype = AST_FRAME_DTMF;
03562 p->subs[index].f.subclass = res & 0xff;
03563 #ifdef ZAPATA_PRI
03564 }
03565 #endif
03566
03567 zt_confmute(p, 0);
03568 return &p->subs[index].f;
03569 }
03570
03571 if (res & ZT_EVENT_DTMFDOWN) {
03572 ast_log(LOG_DEBUG, "DTMF Down '%c'\n", res & 0xff);
03573 p->subs[index].f.frametype = AST_FRAME_NULL;
03574 p->subs[index].f.subclass = 0;
03575 zt_confmute(p, 1);
03576
03577 return &p->subs[index].f;
03578 }
03579
03580 switch(res) {
03581 case ZT_EVENT_BITSCHANGED:
03582 if (p->sig == SIG_R2) {
03583 #ifdef ZAPATA_R2
03584 struct ast_frame *f = &p->subs[index].f;
03585 mfcr2_event_t *e;
03586 e = r2_get_event_bits(p);
03587 if (e)
03588 f = handle_r2_event(p, e, index);
03589 return f;
03590 #else
03591 break;
03592 #endif
03593 }
03594 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
03595 case ZT_EVENT_PULSE_START:
03596
03597 if (!ast->pbx)
03598 tone_zone_play_tone(p->subs[index].zfd, -1);
03599 break;
03600 case ZT_EVENT_DIALCOMPLETE:
03601 if (p->inalarm) break;
03602 if (p->radio) break;
03603 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
03604 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
03605 return NULL;
03606 }
03607 if (!x) {
03608 zt_enable_ec(p);
03609 if (p->echobreak) {
03610 zt_train_ec(p);
03611 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
03612 p->dop.op = ZT_DIAL_OP_REPLACE;
03613 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
03614 p->echobreak = 0;
03615 } else {
03616 p->dialing = 0;
03617 if (p->sig == SIG_E911) {
03618
03619 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
03620 ast_setstate(ast, AST_STATE_UP);
03621 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03622 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03623 break;
03624 } else {
03625
03626 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
03627 }
03628 }
03629 if (ast->_state == AST_STATE_DIALING) {
03630 if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
03631 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
03632 } else if (p->confirmanswer || (!p->dialednone && ((p->sig == SIG_EM) || (p->sig == SIG_EM_E1) || (p->sig == SIG_EMWINK) || (p->sig == SIG_FEATD) || (p->sig == SIG_FEATDMF) || (p->sig == SIG_E911) || (p->sig == SIG_FEATB) || (p->sig == SIG_SF) || (p->sig == SIG_SFWINK) || (p->sig == SIG_SF_FEATD) || (p->sig == SIG_SF_FEATDMF) || (p->sig == SIG_SF_FEATB)))) {
03633 ast_setstate(ast, AST_STATE_RINGING);
03634 } else if (!p->answeronpolarityswitch) {
03635 ast_setstate(ast, AST_STATE_UP);
03636 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03637 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03638 }
03639 }
03640 }
03641 }
03642 break;
03643 case ZT_EVENT_ALARM:
03644 #ifdef ZAPATA_PRI
03645 if (p->call) {
03646 if (p->pri && p->pri->pri) {
03647 if (!pri_grab(p, p->pri)) {
03648 pri_hangup(p->pri->pri, p->call, -1);
03649 pri_destroycall(p->pri->pri, p->call);
03650 p->call = NULL;
03651 pri_rel(p->pri);
03652 } else
03653 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
03654 } else
03655 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
03656 }
03657 if (p->owner)
03658 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
03659 if (p->bearer)
03660 p->bearer->inalarm = 1;
03661 else
03662 #endif
03663 p->inalarm = 1;
03664 res = get_alarms(p);
03665 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
03666 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
03667 "Alarm: %s\r\n"
03668 "Channel: %d\r\n",
03669 alarm2str(res), p->channel);
03670
03671 case ZT_EVENT_ONHOOK:
03672 if (p->radio)
03673 {
03674 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03675 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
03676 break;
03677 }
03678 switch(p->sig) {
03679 case SIG_FXOLS:
03680 case SIG_FXOGS:
03681 case SIG_FXOKS:
03682 p->onhooktime = time(NULL);
03683 p->msgstate = -1;
03684
03685 if (index == SUB_REAL) {
03686
03687 if (p->subs[SUB_CALLWAIT].owner) {
03688
03689 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
03690 if (option_verbose > 2)
03691 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
03692 unalloc_sub(p, SUB_CALLWAIT);
03693 #if 0
03694 p->subs[index].needanswer = 0;
03695 p->subs[index].needringing = 0;
03696 #endif
03697 p->callwaitingrepeat = 0;
03698 p->cidcwexpire = 0;
03699 p->owner = NULL;
03700
03701 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
03702 p->dialing = 1;
03703 zt_ring_phone(p);
03704 } else if (p->subs[SUB_THREEWAY].owner) {
03705 unsigned int mssinceflash;
03706
03707
03708 while(p->subs[SUB_THREEWAY].owner && ast_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) {
03709
03710 ast_mutex_unlock(&p->lock);
03711 ast_mutex_unlock(&ast->lock);
03712 usleep(1);
03713
03714
03715
03716 ast_mutex_lock(&ast->lock);
03717 ast_mutex_lock(&p->lock);
03718 if (p->owner != ast) {
03719 ast_log(LOG_WARNING, "This isn't good...\n");
03720 return NULL;
03721 }
03722 }
03723 if (!p->subs[SUB_THREEWAY].owner) {
03724 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
03725 return NULL;
03726 }
03727 mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
03728 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
03729 if (mssinceflash < MIN_MS_SINCE_FLASH) {
03730
03731
03732 if (p->subs[SUB_THREEWAY].owner)
03733 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
03734 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03735 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
03736 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03737 } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
03738 if (p->transfer) {
03739
03740 p->subs[SUB_REAL].inthreeway = 0;
03741 p->subs[SUB_THREEWAY].inthreeway = 0;
03742
03743 if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
03744 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03745
03746 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03747 p->owner = NULL;
03748
03749 zt_ring_phone(p);
03750 } else {
03751 if ((res = attempt_transfer(p)) < 0) {
03752 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03753 if (p->subs[SUB_THREEWAY].owner)
03754 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03755 } else if (res) {
03756
03757 if (p->subs[SUB_THREEWAY].owner)
03758 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03759 break;
03760 }
03761 }
03762 } else {
03763 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03764 if (p->subs[SUB_THREEWAY].owner)
03765 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03766 }
03767 } else {
03768 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03769
03770 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03771 p->owner = NULL;
03772
03773 zt_ring_phone(p);
03774 }
03775 }
03776 } else {
03777 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
03778 }
03779
03780 default:
03781 zt_disable_ec(p);
03782 return NULL;
03783 }
03784 break;
03785 case ZT_EVENT_RINGOFFHOOK:
03786 if (p->inalarm) break;
03787 if (p->radio)
03788 {
03789 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03790 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
03791 break;
03792 }
03793
03794
03795 if ((p->sig == SIG_E911) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
03796 c = strchr(p->dialdest, '/');
03797 if (c)
03798 c++;
03799 else
03800 c = p->dialdest;
03801 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
03802 else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
03803 if (strlen(p->dop.dialstr) > 4) {
03804 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
03805 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
03806 p->echorest[sizeof(p->echorest) - 1] = '\0';
03807 p->echobreak = 1;
03808 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
03809 } else
03810 p->echobreak = 0;
03811 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
03812 x = ZT_ONHOOK;
03813 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03814 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
03815 return NULL;
03816 }
03817 p->dialing = 1;
03818 return &p->subs[index].f;
03819 }
03820 switch(p->sig) {
03821 case SIG_FXOLS:
03822 case SIG_FXOGS:
03823 case SIG_FXOKS:
03824 switch(ast->_state) {
03825 case AST_STATE_RINGING:
03826 zt_enable_ec(p);
03827 zt_train_ec(p);
03828 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03829 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03830
03831 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
03832 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
03833 if (p->cidspill) {
03834
03835 free(p->cidspill);
03836 p->cidspill = NULL;
03837 }
03838 p->dialing = 0;
03839 p->callwaitcas = 0;
03840 if (p->confirmanswer) {
03841
03842 p->subs[index].f.frametype = AST_FRAME_NULL;
03843 p->subs[index].f.subclass = 0;
03844 } else if (!ast_strlen_zero(p->dop.dialstr)) {
03845
03846 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
03847 if (res < 0) {
03848 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
03849 p->dop.dialstr[0] = '\0';
03850 return NULL;
03851 } else {
03852 ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
03853 p->subs[index].f.frametype = AST_FRAME_NULL;
03854 p->subs[index].f.subclass = 0;
03855 p->dialing = 1;
03856 }
03857 p->dop.dialstr[0] = '\0';
03858 ast_setstate(ast, AST_STATE_DIALING);
03859 } else
03860 ast_setstate(ast, AST_STATE_UP);
03861 return &p->subs[index].f;
03862 case AST_STATE_DOWN:
03863 ast_setstate(ast, AST_STATE_RING);
03864 ast->rings = 1;
03865 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03866 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
03867 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
03868 return &p->subs[index].f;
03869 case AST_STATE_UP:
03870
03871 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
03872
03873 if (ast_bridged_channel(p->owner))
03874 ast_moh_stop(ast_bridged_channel(p->owner));
03875 break;
03876 case AST_STATE_RESERVED:
03877
03878 if (has_voicemail(p))
03879 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
03880 else
03881 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
03882 break;
03883 default:
03884 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
03885 }
03886 break;
03887 case SIG_FXSLS:
03888 case SIG_FXSGS:
03889 case SIG_FXSKS:
03890 if (ast->_state == AST_STATE_RING) {
03891 p->ringt = p->ringt_base;
03892 }
03893
03894
03895
03896 ast_log(LOG_DEBUG, "Setting IDLE polarity due "
03897 "to ring. Old polarity was %d\n",
03898 p->polarity);
03899 p->polarity = POLARITY_IDLE;
03900
03901
03902 case SIG_EM:
03903 case SIG_EM_E1:
03904 case SIG_EMWINK:
03905 case SIG_FEATD:
03906 case SIG_FEATDMF:
03907 case SIG_FEATDMF_TA:
03908 case SIG_E911:
03909 case SIG_FEATB:
03910 case SIG_SF:
03911 case SIG_SFWINK:
03912 case SIG_SF_FEATD:
03913 case SIG_SF_FEATDMF:
03914 case SIG_SF_FEATB:
03915 if (ast->_state == AST_STATE_PRERING)
03916 ast_setstate(ast, AST_STATE_RING);
03917 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
03918 if (option_debug)
03919 ast_log(LOG_DEBUG, "Ring detected\n");
03920 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03921 p->subs[index].f.subclass = AST_CONTROL_RING;
03922 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
03923 if (option_debug)
03924 ast_log(LOG_DEBUG, "Line answered\n");
03925 if (p->confirmanswer) {
03926 p->subs[index].f.frametype = AST_FRAME_NULL;
03927 p->subs[index].f.subclass = 0;
03928 } else {
03929 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03930 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03931 ast_setstate(ast, AST_STATE_UP);
03932 }
03933 } else if (ast->_state != AST_STATE_RING)
03934 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
03935 break;
03936 default:
03937 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
03938 }
03939 break;
03940 #ifdef ZT_EVENT_RINGBEGIN
03941 case ZT_EVENT_RINGBEGIN:
03942 switch(p->sig) {
03943 case SIG_FXSLS:
03944 case SIG_FXSGS:
03945 case SIG_FXSKS:
03946 if (ast->_state == AST_STATE_RING) {
03947 p->ringt = p->ringt_base;
03948 }
03949 break;
03950 }
03951 break;
03952 #endif
03953 case ZT_EVENT_RINGEROFF:
03954 if (p->inalarm) break;
03955 if (p->radio) break;
03956 ast->rings++;
03957 if ((ast->rings > p->cidrings) && (p->cidspill)) {
03958 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
03959 free(p->cidspill);
03960 p->cidspill = NULL;
03961 p->callwaitcas = 0;
03962 }
03963 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03964 p->subs[index].f.subclass = AST_CONTROL_RINGING;
03965 break;
03966 case ZT_EVENT_RINGERON:
03967 break;
03968 case ZT_EVENT_NOALARM:
03969 p->inalarm = 0;
03970 #ifdef ZAPATA_PRI
03971
03972 if (p->bearer)
03973 p->bearer->inalarm = 0;
03974 #endif
03975 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
03976 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
03977 "Channel: %d\r\n", p->channel);
03978 break;
03979 case ZT_EVENT_WINKFLASH:
03980 if (p->inalarm) break;
03981 if (p->radio) break;
03982
03983 gettimeofday(&p->flashtime, NULL);
03984 switch(p->sig) {
03985 case SIG_FXOLS:
03986 case SIG_FXOGS:
03987 case SIG_FXOKS:
03988 ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
03989 index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
03990 p->callwaitcas = 0;
03991
03992 if (index != SUB_REAL) {
03993 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
03994 goto winkflashdone;
03995 }
03996
03997 if (p->subs[SUB_CALLWAIT].owner) {
03998
03999 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
04000 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
04001 p->owner = p->subs[SUB_REAL].owner;
04002 ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
04003 if (p->owner->_state == AST_STATE_RINGING) {
04004 ast_setstate(p->owner, AST_STATE_UP);
04005 p->subs[SUB_REAL].needanswer = 1;
04006 }
04007 p->callwaitingrepeat = 0;
04008 p->cidcwexpire = 0;
04009
04010 if (!p->subs[SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
04011 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
04012 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
04013 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
04014 } else if (!p->subs[SUB_THREEWAY].owner) {
04015 char cid_num[256];
04016 char cid_name[256];
04017
04018 if (!p->threewaycalling) {
04019
04020 p->subs[SUB_REAL].needflash = 1;
04021 goto winkflashdone;
04022 } else if (!check_for_conference(p)) {
04023 if (p->zaptrcallerid && p->owner) {
04024 if (p->owner->cid.cid_num)
04025 ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
04026 if (p->owner->cid.cid_name)
04027 ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
04028 }
04029
04030
04031 if (!((ast->pbx) ||
04032 (ast->_state == AST_STATE_UP) ||
04033 (ast->_state == AST_STATE_RING))) {
04034 ast_log(LOG_DEBUG, "Flash when call not up or ringing\n");
04035 goto winkflashdone;
04036 }
04037 if (alloc_sub(p, SUB_THREEWAY)) {
04038 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
04039 goto winkflashdone;
04040 }
04041
04042 chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
04043 if (p->zaptrcallerid) {
04044 if (!p->origcid_num)
04045 p->origcid_num = strdup(p->cid_num);
04046 if (!p->origcid_name)
04047 p->origcid_name = strdup(p->cid_name);
04048 ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
04049 ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
04050 }
04051
04052 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04053
04054 zt_disable_ec(p);
04055 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
04056 if (res)
04057 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
04058 p->owner = chan;
04059 if (!chan) {
04060 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
04061 } else if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
04062 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
04063 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
04064 zt_enable_ec(p);
04065 ast_hangup(chan);
04066 } else {
04067 if (option_verbose > 2)
04068 ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);
04069
04070 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
04071 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
04072 }
04073 }
04074 } else {
04075
04076 if (p->subs[SUB_THREEWAY].inthreeway) {
04077
04078 if (option_debug)
04079 ast_log(LOG_DEBUG, "Got flash with three way call up, dropping last call on %d\n", p->channel);
04080
04081 if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
04082
04083 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04084 p->owner = p->subs[SUB_REAL].owner;
04085 }
04086
04087 if (option_verbose > 2)
04088 ast_verbose(VERBOSE_PREFIX_3 "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
04089 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04090 p->subs[SUB_REAL].inthreeway = 0;
04091 p->subs[SUB_THREEWAY].inthreeway = 0;
04092 } else {
04093
04094 if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
04095 (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
04096 int otherindex = SUB_THREEWAY;
04097
04098 if (option_verbose > 2)
04099 ast_verbose(VERBOSE_PREFIX_3 "Building conference on call on %s and %s\n", p->subs[SUB_THREEWAY].owner->name, p->subs[SUB_REAL].owner->name);
04100
04101 p->subs[SUB_THREEWAY].inthreeway = 1;
04102 p->subs[SUB_REAL].inthreeway = 1;
04103 if (ast->_state == AST_STATE_UP) {
04104 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04105 otherindex = SUB_REAL;
04106 }
04107 if (p->subs[otherindex].owner && ast_bridged_channel(p->subs[otherindex].owner))
04108 ast_moh_stop(ast_bridged_channel(p->subs[otherindex].owner));
04109 p->owner = p->subs[SUB_REAL].owner;
04110 if (ast->_state == AST_STATE_RINGING) {
04111 ast_log(LOG_DEBUG, "Enabling ringtone on real and threeway\n");
04112 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
04113 res = tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, ZT_TONE_RINGTONE);
04114 }
04115 } else {
04116 if (option_verbose > 2)
04117 ast_verbose(VERBOSE_PREFIX_3 "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name);
04118 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04119 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04120 p->owner = p->subs[SUB_REAL].owner;
04121 if (p->subs[SUB_REAL].owner && ast_bridged_channel(p->subs[SUB_REAL].owner))
04122 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
04123 zt_enable_ec(p);
04124 }
04125
04126 }
04127 }
04128 winkflashdone:
04129 update_conf(p);
04130 break;
04131 case SIG_EM:
04132 case SIG_EM_E1:
04133 case SIG_EMWINK:
04134 case SIG_FEATD:
04135 case SIG_SF:
04136 case SIG_SFWINK:
04137 case SIG_SF_FEATD:
04138 case SIG_FXSLS:
04139 case SIG_FXSGS:
04140 if (p->dialing)
04141 ast_log(LOG_DEBUG, "Ignoring wink on channel %d\n", p->channel);
04142 else
04143 ast_log(LOG_DEBUG, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
04144 break;
04145 case SIG_FEATDMF_TA:
04146 switch (p->whichwink) {
04147 case 0:
04148 ast_log(LOG_DEBUG, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04149 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04150 break;
04151 case 1:
04152 ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
04153 break;
04154 case 2:
04155 ast_log(LOG_WARNING, "Received unexpected wink on channel of type SIG_FEATDMF_TA\n");
04156 return NULL;
04157 }
04158 p->whichwink++;
04159
04160 case SIG_FEATDMF:
04161 case SIG_E911:
04162 case SIG_FEATB:
04163 case SIG_SF_FEATDMF:
04164 case SIG_SF_FEATB:
04165
04166 if (!ast_strlen_zero(p->dop.dialstr))
04167 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04168 else if (res < 0) {
04169 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04170 p->dop.dialstr[0] = '\0';
04171 return NULL;
04172 } else
04173 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04174 p->dop.dialstr[0] = '\0';
04175 break;
04176 default:
04177 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
04178 }
04179 break;
04180 case ZT_EVENT_HOOKCOMPLETE:
04181 if (p->inalarm) break;
04182 if (p->radio) break;
04183 switch(p->sig) {
04184 case SIG_FXSLS:
04185 case SIG_FXSGS:
04186 case SIG_FXSKS:
04187 case SIG_EM:
04188 case SIG_EM_E1:
04189 case SIG_EMWINK:
04190 case SIG_FEATD:
04191 case SIG_SF:
04192 case SIG_SFWINK:
04193 case SIG_SF_FEATD:
04194 if (!ast_strlen_zero(p->dop.dialstr))
04195 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04196 else if (res < 0) {
04197 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04198 p->dop.dialstr[0] = '\0';
04199 return NULL;
04200 } else
04201 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04202 p->dop.dialstr[0] = '\0';
04203 p->dop.op = ZT_DIAL_OP_REPLACE;
04204 break;
04205 case SIG_FEATDMF:
04206 case SIG_E911:
04207 case SIG_FEATB:
04208 case SIG_SF_FEATDMF:
04209 case SIG_SF_FEATB:
04210 ast_log(LOG_DEBUG, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
04211 break;
04212 default:
04213 break;
04214 }
04215 break;
04216 case ZT_EVENT_POLARITY:
04217
04218
04219
04220
04221
04222
04223 if (p->polarity == POLARITY_IDLE) {
04224 p->polarity = POLARITY_REV;
04225 if (p->answeronpolarityswitch &&
04226 ((ast->_state == AST_STATE_DIALING) ||
04227 (ast->_state == AST_STATE_RINGING))) {
04228 ast_log(LOG_DEBUG, "Answering on polarity switch!\n");
04229 ast_setstate(p->owner, AST_STATE_UP);
04230 if(p->hanguponpolarityswitch) {
04231 gettimeofday(&p->polaritydelaytv, NULL);
04232 }
04233 break;
04234 } else
04235 ast_log(LOG_DEBUG, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, ast->_state);
04236 }
04237
04238
04239 if(p->hanguponpolarityswitch &&
04240 (p->polarityonanswerdelay > 0) &&
04241 (p->polarity == POLARITY_REV) &&
04242 ((ast->_state == AST_STATE_UP) || (ast->_state == AST_STATE_RING)) ) {
04243
04244 ast_log(LOG_DEBUG, "Polarity Reversal event occured - DEBUG 1: channel %d, state %d, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %d\n", p->channel, ast->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
04245
04246 if(ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
04247 ast_log(LOG_DEBUG, "Polarity Reversal detected and now Hanging up on channel %d\n", p->channel);
04248 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
04249 p->polarity = POLARITY_IDLE;
04250 } else {
04251 ast_log(LOG_DEBUG, "Polarity Reversal detected but NOT hanging up (too close to answer event) on channel %d, state %d\n", p->channel, ast->_state);
04252 }
04253 } else {
04254 p->polarity = POLARITY_IDLE;
04255 ast_log(LOG_DEBUG, "Ignoring Polarity switch to IDLE on channel %d, state %d\n", p->channel, ast->_state);
04256 }
04257
04258 ast_log(LOG_DEBUG, "Polarity Reversal event occured - DEBUG 2: channel %d, state %d, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %d\n", p->channel, ast->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
04259 break;
04260 default:
04261 ast_log(LOG_DEBUG, "Dunno what to do with event %d on channel %d\n", res, p->channel);
04262 }
04263 return &p->subs[index].f;
04264 }
04265
04266 static struct ast_frame *__zt_exception(struct ast_channel *ast)
04267 {
04268 struct zt_pvt *p = ast->tech_pvt;
04269 int res;
04270 int usedindex=-1;
04271 int index;
04272 struct ast_frame *f;
04273
04274
04275 index = zt_get_index(ast, p, 1);
04276
04277 p->subs[index].f.frametype = AST_FRAME_NULL;
04278 p->subs[index].f.datalen = 0;
04279 p->subs[index].f.samples = 0;
04280 p->subs[index].f.mallocd = 0;
04281 p->subs[index].f.offset = 0;
04282 p->subs[index].f.subclass = 0;
04283 p->subs[index].f.delivery = ast_tv(0,0);
04284 p->subs[index].f.src = "zt_exception";
04285 p->subs[index].f.data = NULL;
04286
04287
04288 if ((!p->owner) && (!p->radio)) {
04289
04290
04291
04292
04293
04294 if (p->fake_event) {
04295 res = p->fake_event;
04296 p->fake_event = 0;
04297 } else
04298 res = zt_get_event(p->subs[SUB_REAL].zfd);
04299
04300 if ((res != ZT_EVENT_RINGEROFF) && (res != ZT_EVENT_RINGERON) &&
04301 (res != ZT_EVENT_HOOKCOMPLETE)) {
04302 ast_log(LOG_DEBUG, "Restoring owner of channel %d on event %d\n", p->channel, res);
04303 p->owner = p->subs[SUB_REAL].owner;
04304 if (p->owner && ast_bridged_channel(p->owner))
04305 ast_moh_stop(ast_bridged_channel(p->owner));
04306 }
04307 switch(res) {
04308 case ZT_EVENT_ONHOOK:
04309 zt_disable_ec(p);
04310 if (p->owner) {
04311 if (option_verbose > 2)
04312 ast_verbose(VERBOSE_PREFIX_3 "Channel %s still has call, ringing phone\n", p->owner->name);
04313 zt_ring_phone(p);
04314 p->callwaitingrepeat = 0;
04315 p->cidcwexpire = 0;
04316 } else
04317 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04318 update_conf(p);
04319 break;
04320 case ZT_EVENT_RINGOFFHOOK:
04321 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
04322 if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
04323 p->subs[SUB_REAL].needanswer = 1;
04324 p->dialing = 0;
04325 }
04326 break;
04327 case ZT_EVENT_HOOKCOMPLETE:
04328 case ZT_EVENT_RINGERON:
04329 case ZT_EVENT_RINGEROFF:
04330
04331 break;
04332 case ZT_EVENT_WINKFLASH:
04333 gettimeofday(&p->flashtime, NULL);
04334 if (p->owner) {
04335 if (option_verbose > 2)
04336 ast_verbose(VERBOSE_PREFIX_3 "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
04337 if (p->owner->_state != AST_STATE_UP) {
04338
04339 usedindex = zt_get_index(p->owner, p, 0);
04340 if (usedindex > -1) {
04341 p->subs[usedindex].needanswer = 1;
04342 }
04343 ast_setstate(p->owner, AST_STATE_UP);
04344 }
04345 p->callwaitingrepeat = 0;
04346 p->cidcwexpire = 0;
04347 if (ast_bridged_channel(p->owner))
04348 ast_moh_stop(ast_bridged_channel(p->owner));
04349 } else
04350 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04351 update_conf(p);
04352 break;
04353 default:
04354 ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
04355 }
04356 f = &p->subs[index].f;
04357 return f;
04358 }
04359 if (!p->radio) ast_log(LOG_DEBUG, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
04360
04361 if (ast != p->owner) {
04362 ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
04363 f = &p->subs[index].f;
04364 return f;
04365 }
04366 f = zt_handle_event(ast);
04367 return f;
04368 }
04369
04370 struct ast_frame *zt_exception(struct ast_channel *ast)
04371 {
04372 struct zt_pvt *p = ast->tech_pvt;
04373 struct ast_frame *f;
04374 ast_mutex_lock(&p->lock);
04375 f = __zt_exception(ast);
04376 ast_mutex_unlock(&p->lock);
04377 return f;
04378 }
04379
04380 struct ast_frame *zt_read(struct ast_channel *ast)
04381 {
04382 struct zt_pvt *p = ast->tech_pvt;
04383 int res;
04384 int index;
04385 void *readbuf;
04386 struct ast_frame *f;
04387
04388
04389 ast_mutex_lock(&p->lock);
04390
04391 index = zt_get_index(ast, p, 0);
04392
04393
04394 if (index < 0) {
04395 ast_log(LOG_WARNING, "We dont exist?\n");
04396 ast_mutex_unlock(&p->lock);
04397 return NULL;
04398 }
04399
04400 if (p->radio && p->inalarm) return NULL;
04401
04402 p->subs[index].f.frametype = AST_FRAME_NULL;
04403 p->subs[index].f.datalen = 0;
04404 p->subs[index].f.samples = 0;
04405 p->subs[index].f.mallocd = 0;
04406 p->subs[index].f.offset = 0;
04407 p->subs[index].f.subclass = 0;
04408 p->subs[index].f.delivery = ast_tv(0,0);
04409 p->subs[index].f.src = "zt_read";
04410 p->subs[index].f.data = NULL;
04411
04412
04413 if (p->radio && (!p->firstradio))
04414 {
04415 ZT_PARAMS ps;
04416
04417 ps.channo = p->channel;
04418 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
04419 ast_mutex_unlock(&p->lock);
04420 return NULL;
04421 }
04422 p->firstradio = 1;
04423 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04424 if (ps.rxisoffhook)
04425 {
04426 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
04427 }
04428 else
04429 {
04430 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
04431 }
04432 ast_mutex_unlock(&p->lock);
04433 return &p->subs[index].f;
04434 }
04435 if (p->ringt == 1) {
04436 ast_mutex_unlock(&p->lock);
04437 return NULL;
04438 }
04439 else if (p->ringt > 0)
04440 p->ringt--;
04441
04442 if (p->subs[index].needringing) {
04443
04444 p->subs[index].needringing = 0;
04445 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04446 p->subs[index].f.subclass = AST_CONTROL_RINGING;
04447 ast_setstate(ast, AST_STATE_RINGING);
04448 ast_mutex_unlock(&p->lock);
04449 return &p->subs[index].f;
04450 }
04451
04452 if (p->subs[index].needbusy) {
04453
04454 p->subs[index].needbusy = 0;
04455 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04456 p->subs[index].f.subclass = AST_CONTROL_BUSY;
04457 ast_mutex_unlock(&p->lock);
04458 return &p->subs[index].f;
04459 }
04460
04461 if (p->subs[index].needcongestion) {
04462
04463 p->subs[index].needcongestion = 0;
04464 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04465 p->subs[index].f.subclass = AST_CONTROL_CONGESTION;
04466 ast_mutex_unlock(&p->lock);
04467 return &p->subs[index].f;
04468 }
04469
04470 if (p->subs[index].needcallerid) {
04471 ast_set_callerid(ast, !ast_strlen_zero(p->lastcid_num) ? p->lastcid_num : NULL,
04472 !ast_strlen_zero(p->lastcid_name) ? p->lastcid_name : NULL,
04473 !ast_strlen_zero(p->lastcid_num) ? p->lastcid_num : NULL
04474 );
04475 p->subs[index].needcallerid = 0;
04476 }
04477
04478 if (p->subs[index].needanswer) {
04479
04480 p->subs[index].needanswer = 0;
04481 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04482 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04483 ast_mutex_unlock(&p->lock);
04484 return &p->subs[index].f;
04485 }
04486
04487 if (p->subs[index].needflash) {
04488
04489 p->subs[index].needflash = 0;
04490 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04491 p->subs[index].f.subclass = AST_CONTROL_FLASH;
04492 ast_mutex_unlock(&p->lock);
04493 return &p->subs[index].f;
04494 }
04495
04496 if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
04497 if (!p->subs[index].linear) {
04498 p->subs[index].linear = 1;
04499 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04500 if (res)
04501 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, index);
04502 }
04503 } else if ((ast->rawreadformat == AST_FORMAT_ULAW) ||
04504 (ast->rawreadformat == AST_FORMAT_ALAW)) {
04505 if (p->subs[index].linear) {
04506 p->subs[index].linear = 0;
04507 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04508 if (res)
04509 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to companded mode.\n", p->channel, index);
04510 }
04511 } else {
04512 ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast->rawreadformat));
04513 ast_mutex_unlock(&p->lock);
04514 return NULL;
04515 }
04516 readbuf = ((unsigned char *)p->subs[index].buffer) + AST_FRIENDLY_OFFSET;
04517 CHECK_BLOCKING(ast);
04518 res = read(p->subs[index].zfd, readbuf, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04519 ast_clear_flag(ast, AST_FLAG_BLOCKING);
04520
04521 if (res < 0) {
04522 f = NULL;
04523 if (res == -1) {
04524 if (errno == EAGAIN) {
04525
04526 ast_mutex_unlock(&p->lock);
04527 return &p->subs[index].f;
04528 } else if (errno == ELAST) {
04529 f = __zt_exception(ast);
04530 } else
04531 ast_log(LOG_WARNING, "zt_rec: %s\n", strerror(errno));
04532 }
04533 ast_mutex_unlock(&p->lock);
04534 return f;
04535 }
04536 if (res != (p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE)) {
04537 ast_log(LOG_DEBUG, "Short read (%d/%d), must be an event...\n", res, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04538 f = __zt_exception(ast);
04539 ast_mutex_unlock(&p->lock);
04540 return f;
04541 }
04542 if (p->tdd) {
04543 int c;
04544
04545 c = tdd_feed(p->tdd,readbuf,READ_SIZE);
04546 if (c < 0) {
04547 ast_log(LOG_DEBUG,"tdd_feed failed\n");
04548 ast_mutex_unlock(&p->lock);
04549 return NULL;
04550 }
04551 if (c) {
04552 p->subs[index].f.subclass = 0;
04553 p->subs[index].f.frametype = AST_FRAME_TEXT;
04554 p->subs[index].f.mallocd = 0;
04555 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04556 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET;
04557 p->subs[index].f.datalen = 1;
04558 *((char *) p->subs[index].f.data) = c;
04559 ast_mutex_unlock(&p->lock);
04560 return &p->subs[index].f;
04561 }
04562 }
04563 if (p->callwaitingrepeat)
04564 p->callwaitingrepeat--;
04565 if (p->cidcwexpire)
04566 p->cidcwexpire--;
04567
04568 if (p->callwaitingrepeat == 1) {
04569 p->callwaitrings++;
04570 zt_callwait(ast);
04571 }
04572
04573 if (p->cidcwexpire == 1) {
04574 if (option_verbose > 2)
04575 ast_verbose(VERBOSE_PREFIX_3 "CPE does not support Call Waiting Caller*ID.\n");
04576 restore_conference(p);
04577 }
04578 if (p->subs[index].linear) {
04579 p->subs[index].f.datalen = READ_SIZE * 2;
04580 } else
04581 p->subs[index].f.datalen = READ_SIZE;
04582
04583
04584 if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
04585 send_callerid(p);
04586 }
04587
04588 p->subs[index].f.frametype = AST_FRAME_VOICE;
04589 p->subs[index].f.subclass = ast->rawreadformat;
04590 p->subs[index].f.samples = READ_SIZE;
04591 p->subs[index].f.mallocd = 0;
04592 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04593 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET/2;
04594 #if 0
04595 ast_log(LOG_DEBUG, "Read %d of voice on %s\n", p->subs[index].f.datalen, ast->name);
04596 #endif
04597 if (p->dialing ||
04598 (index && (ast->_state != AST_STATE_UP)) ||
04599 ((index == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway)
04600 ) {
04601
04602
04603 p->subs[index].f.frametype = AST_FRAME_NULL;
04604 p->subs[index].f.subclass = 0;
04605 p->subs[index].f.samples = 0;
04606 p->subs[index].f.mallocd = 0;
04607 p->subs[index].f.offset = 0;
04608 p->subs[index].f.data = NULL;
04609 p->subs[index].f.datalen= 0;
04610 }
04611 if (p->dsp && (!p->ignoredtmf || p->callwaitcas || p->busydetect || p->callprogress) && !index) {
04612
04613 f = ast_dsp_process(ast, p->dsp, &p->subs[index].f);
04614 if (f) {
04615 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
04616 if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
04617
04618
04619 f = NULL;
04620 }
04621 } else if (f->frametype == AST_FRAME_DTMF) {
04622 #ifdef ZAPATA_PRI
04623 if (!p->proceeding && p->sig==SIG_PRI && p->pri && p->pri->overlapdial) {
04624
04625 f->frametype = AST_FRAME_NULL;
04626 f->subclass = 0;
04627 }
04628 #endif
04629
04630 p->pulsedial = 0;
04631 }
04632 }
04633 } else
04634 f = &p->subs[index].f;
04635 if (f && (f->frametype == AST_FRAME_DTMF)) {
04636 ast_log(LOG_DEBUG, "DTMF digit: %c on %s\n", f->subclass, ast->name);
04637 if (p->confirmanswer) {
04638 ast_log(LOG_DEBUG, "Confirm answer on %s!\n", ast->name);
04639
04640
04641 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04642 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04643 f = &p->subs[index].f;
04644
04645 p->confirmanswer = 0;
04646 } else if (p->callwaitcas) {
04647 if ((f->subclass == 'A') || (f->subclass == 'D')) {
04648 ast_log(LOG_DEBUG, "Got some DTMF, but it's for the CAS\n");
04649 if (p->cidspill)
04650 free(p->cidspill);
04651 send_cwcidspill(p);
04652 }
04653 if ((f->subclass != 'm') && (f->subclass != 'u'))
04654 p->callwaitcas = 0;
04655 p->subs[index].f.frametype = AST_FRAME_NULL;
04656 p->subs[index].f.subclass = 0;
04657 f = &p->subs[index].f;
04658 } else if (f->subclass == 'f') {
04659
04660 if (!p->faxhandled) {
04661 p->faxhandled++;
04662 if (strcmp(ast->exten, "fax")) {
04663 const char *target_context = ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext;
04664
04665 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
04666 if (option_verbose > 2)
04667 ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
04668
04669 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
04670 if (ast_async_goto(ast, target_context, "fax", 1))
04671 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
04672 } else
04673 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
04674 } else
04675 ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
04676 } else
04677 ast_log(LOG_DEBUG, "Fax already handled\n");
04678 zt_confmute(p, 0);
04679 p->subs[index].f.frametype = AST_FRAME_NULL;
04680 p->subs[index].f.subclass = 0;
04681 f = &p->subs[index].f;
04682 } else if (f->subclass == 'm') {
04683
04684 zt_confmute(p, 1);
04685 p->subs[index].f.frametype = AST_FRAME_NULL;
04686 p->subs[index].f.subclass = 0;
04687 f = &p->subs[index].f;
04688 } else if (f->subclass == 'u') {
04689
04690 zt_confmute(p, 0);
04691 p->subs[index].f.frametype = AST_FRAME_NULL;
04692 p->subs[index].f.subclass = 0;
04693 f = &p->subs[index].f;
04694 } else
04695 zt_confmute(p, 0);
04696 }
04697
04698
04699 if (p->fake_event)
04700 ast_set_flag(ast, AST_FLAG_EXCEPTION);
04701
04702 ast_mutex_unlock(&p->lock);
04703 return f;
04704 }
04705
04706 static int my_zt_write(struct zt_pvt *p, unsigned char *buf, int len, int index, int linear)
04707 {
04708 int sent=0;
04709 int size;
04710 int res;
04711 int fd;
04712 fd = p->subs[index].zfd;
04713 while(len) {
04714 size = len;
04715 if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
04716 size = (linear ? READ_SIZE * 2 : READ_SIZE);
04717 res = write(fd, buf, size);
04718 if (res != size) {
04719 if (option_debug)
04720 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
04721 return sent;
04722 }
04723 len -= size;
04724 buf += size;
04725 }
04726 return sent;
04727 }
04728
04729 static int zt_write(struct ast_channel *ast, struct ast_frame *frame)
04730 {
04731 struct zt_pvt *p = ast->tech_pvt;
04732 int res;
04733 unsigned char outbuf[4096];
04734 int index;
04735 index = zt_get_index(ast, p, 0);
04736 if (index < 0) {
04737 ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
04738 return -1;
04739 }
04740
04741 #if 0
04742 #ifdef ZAPATA_PRI
04743 ast_mutex_lock(&p->lock);
04744 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04745 if (p->pri->pri) {
04746 if (!pri_grab(p, p->pri)) {
04747 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04748 pri_rel(p->pri);
04749 } else
04750 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04751 }
04752 p->proceeding=1;
04753 }
04754 ast_mutex_unlock(&p->lock);
04755 #endif
04756 #endif
04757
04758 if (frame->frametype != AST_FRAME_VOICE) {
04759 if (frame->frametype != AST_FRAME_IMAGE)
04760 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
04761 return 0;
04762 }
04763 if ((frame->subclass != AST_FORMAT_SLINEAR) &&
04764 (frame->subclass != AST_FORMAT_ULAW) &&
04765 (frame->subclass != AST_FORMAT_ALAW)) {
04766 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
04767 return -1;
04768 }
04769 if (p->dialing) {
04770 if (option_debug)
04771 ast_log(LOG_DEBUG, "Dropping frame since I'm still dialing on %s...\n",ast->name);
04772 return 0;
04773 }
04774 if (!p->owner) {
04775 if (option_debug)
04776 ast_log(LOG_DEBUG, "Dropping frame since there is no active owner on %s...\n",ast->name);
04777 return 0;
04778 }
04779 if (p->cidspill) {
04780 if (option_debug)
04781 ast_log(LOG_DEBUG, "Dropping frame since I've still got a callerid spill\n");
04782 return 0;
04783 }
04784
04785 if (!frame->data || !frame->datalen)
04786 return 0;
04787 if (frame->datalen > sizeof(outbuf) * 2) {
04788 ast_log(LOG_WARNING, "Frame too large\n");
04789 return 0;
04790 }
04791
04792 if (frame->subclass == AST_FORMAT_SLINEAR) {
04793 if (!p->subs[index].linear) {
04794 p->subs[index].linear = 1;
04795 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04796 if (res)
04797 ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
04798 }
04799 res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 1);
04800 } else {
04801
04802 if (p->subs[index].linear) {
04803 p->subs[index].linear = 0;
04804 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04805 if (res)
04806 ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
04807 }
04808 res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 0);
04809 }
04810 if (res < 0) {
04811 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
04812 return -1;
04813 }
04814 return 0;
04815 }
04816
04817 static int zt_indicate(struct ast_channel *chan, int condition)
04818 {
04819 struct zt_pvt *p = chan->tech_pvt;
04820 int res=-1;
04821 int index;
04822 int func = ZT_FLASH;
04823 ast_mutex_lock(&p->lock);
04824 index = zt_get_index(chan, p, 0);
04825 ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
04826 if (index == SUB_REAL) {
04827 switch(condition) {
04828 case AST_CONTROL_BUSY:
04829 #ifdef ZAPATA_PRI
04830 if (p->priindication_oob && p->sig == SIG_PRI) {
04831 chan->hangupcause = AST_CAUSE_USER_BUSY;
04832 chan->_softhangup |= AST_SOFTHANGUP_DEV;
04833 res = 0;
04834 } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04835 if (p->pri->pri) {
04836 if (!pri_grab(p, p->pri)) {
04837 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04838 pri_rel(p->pri);
04839 }
04840 else
04841 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04842 }
04843 p->progress = 1;
04844 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
04845 } else
04846 #endif
04847 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
04848 break;
04849 case AST_CONTROL_RINGING:
04850 #ifdef ZAPATA_PRI
04851 if ((!p->alerting) && p->sig==SIG_PRI && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
04852 if (p->pri->pri) {
04853 if (!pri_grab(p, p->pri)) {
04854 pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04855 pri_rel(p->pri);
04856 }
04857 else
04858 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04859 }
04860 p->alerting = 1;
04861 }
04862 #endif
04863 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_RINGTONE);
04864 if (chan->_state != AST_STATE_UP) {
04865 if ((chan->_state != AST_STATE_RING) ||
04866 ((p->sig != SIG_FXSKS) &&
04867 (p->sig != SIG_FXSLS) &&
04868 (p->sig != SIG_FXSGS)))
04869 ast_setstate(chan, AST_STATE_RINGING);
04870 }
04871 break;
04872 case AST_CONTROL_PROCEEDING:
04873 ast_log(LOG_DEBUG,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
04874 #ifdef ZAPATA_PRI
04875 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04876 if (p->pri->pri) {
04877 if (!pri_grab(p, p->pri)) {
04878 pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04879 pri_rel(p->pri);
04880 }
04881 else
04882 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04883 }
04884 p->proceeding = 1;
04885 }
04886 #endif
04887
04888 res = 0;
04889 break;
04890 case AST_CONTROL_PROGRESS:
04891 ast_log(LOG_DEBUG,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
04892 #ifdef ZAPATA_PRI
04893 p->digital = 0;
04894 if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04895 if (p->pri->pri) {
04896 if (!pri_grab(p, p->pri)) {
04897 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04898 pri_rel(p->pri);
04899 }
04900 else
04901 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04902 }
04903 p->progress = 1;
04904 }
04905 #endif
04906
04907 res = 0;
04908 break;
04909 case AST_CONTROL_CONGESTION:
04910 chan->hangupcause = AST_CAUSE_CONGESTION;
04911 #ifdef ZAPATA_PRI
04912 if (p->priindication_oob && p->sig == SIG_PRI) {
04913 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
04914 chan->_softhangup |= AST_SOFTHANGUP_DEV;
04915 res = 0;
04916 } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04917 if (p->pri) {
04918 if (!pri_grab(p, p->pri)) {
04919 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04920 pri_rel(p->pri);
04921 } else
04922 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04923 }
04924 p->progress = 1;
04925 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
04926 } else
04927 #endif
04928 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
04929 break;
04930 #ifdef ZAPATA_PRI
04931 case AST_CONTROL_HOLD:
04932 if (p->pri) {
04933 if (!pri_grab(p, p->pri)) {
04934 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
04935 pri_rel(p->pri);
04936 } else
04937 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04938 }
04939 break;
04940 case AST_CONTROL_UNHOLD:
04941 if (p->pri) {
04942 if (!pri_grab(p, p->pri)) {
04943 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
04944 pri_rel(p->pri);
04945 } else
04946 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04947 }
04948 break;
04949 #endif
04950 case AST_CONTROL_RADIO_KEY:
04951 if (p->radio)
04952 res = zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
04953 res = 0;
04954 break;
04955 case AST_CONTROL_RADIO_UNKEY:
04956 if (p->radio)
04957 res = zt_set_hook(p->subs[index].zfd, ZT_RINGOFF);
04958 res = 0;
04959 break;
04960 case AST_CONTROL_FLASH:
04961
04962 if (ISTRUNK(p) && (p->sig != SIG_PRI)) {
04963
04964 p->dop.dialstr[0] = '\0';
04965 if ((ioctl(p->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
04966 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
04967 chan->name, strerror(errno));
04968 } else
04969 res = 0;
04970 } else
04971 res = 0;
04972 break;
04973 case -1:
04974 res = tone_zone_play_tone(p->subs[index].zfd, -1);
04975 break;
04976 }
04977 } else
04978 res = 0;
04979 ast_mutex_unlock(&p->lock);
04980 return res;
04981 }
04982
04983 static struct ast_channel *zt_new(struct zt_pvt *i, int state, int startpbx, int index, int law, int transfercapability)
04984 {
04985 struct ast_channel *tmp;
04986 int deflaw;
04987 int res;
04988 int x,y;
04989 int features;
04990 ZT_PARAMS ps;
04991 if (i->subs[index].owner) {
04992 ast_log(LOG_WARNING, "Channel %d already has a %s call\n", i->channel,subnames[index]);
04993 return NULL;
04994 }
04995 tmp = ast_channel_alloc(0);
04996 if (tmp) {
04997 tmp->tech = &zap_tech;
04998 ps.channo = i->channel;
04999 res = ioctl(i->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps);
05000 if (res) {
05001 ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW\n");
05002 ps.curlaw = ZT_LAW_MULAW;
05003 }
05004 if (ps.curlaw == ZT_LAW_ALAW)
05005 deflaw = AST_FORMAT_ALAW;
05006 else
05007 deflaw = AST_FORMAT_ULAW;
05008 if (law) {
05009 if (law == ZT_LAW_ALAW)
05010 deflaw = AST_FORMAT_ALAW;
05011 else
05012 deflaw = AST_FORMAT_ULAW;
05013 }
05014 y = 1;
05015 do {
05016 #ifdef ZAPATA_PRI
05017 if (i->bearer || (i->pri && (i->sig == SIG_FXSKS)))
05018 snprintf(tmp->name, sizeof(tmp->name), "Zap/%d:%d-%d", i->pri->trunkgroup, i->channel, y);
05019 else
05020 #endif
05021 if (i->channel == CHAN_PSEUDO)
05022 snprintf(tmp->name, sizeof(tmp->name), "Zap/pseudo-%d", rand());
05023 else
05024 snprintf(tmp->name, sizeof(tmp->name), "Zap/%d-%d", i->channel, y);
05025 for (x=0;x<3;x++) {
05026 if ((index != x) && i->subs[x].owner && !strcasecmp(tmp->name, i->subs[x].owner->name))
05027 break;
05028 }
05029 y++;
05030 } while (x < 3);
05031 tmp->type = type;
05032 tmp->fds[0] = i->subs[index].zfd;
05033 tmp->nativeformats = AST_FORMAT_SLINEAR | deflaw;
05034
05035 tmp->rawreadformat = deflaw;
05036 tmp->readformat = deflaw;
05037 tmp->rawwriteformat = deflaw;
05038 tmp->writeformat = deflaw;
05039 i->subs[index].linear = 0;
05040 zt_setlinear(i->subs[index].zfd, i->subs[index].linear);
05041 features = 0;
05042 if (i->busydetect && CANBUSYDETECT(i)) {
05043 features |= DSP_FEATURE_BUSY_DETECT;
05044 }
05045 if ((i->callprogress & 1) && CANPROGRESSDETECT(i)) {
05046 features |= DSP_FEATURE_CALL_PROGRESS;
05047 }
05048 if ((!i->outgoing && (i->callprogress & 4)) ||
05049 (i->outgoing && (i->callprogress & 2))) {
05050 features |= DSP_FEATURE_FAX_DETECT;
05051 }
05052 #ifdef ZT_TONEDETECT
05053 x = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
05054 if (ioctl(i->subs[index].zfd, ZT_TONEDETECT, &x)) {
05055 #endif
05056 i->hardwaredtmf = 0;
05057 features |= DSP_FEATURE_DTMF_DETECT;
05058 #ifdef ZT_TONEDETECT
05059 } else if (NEED_MFDETECT(i)) {
05060 i->hardwaredtmf = 1;
05061 features |= DSP_FEATURE_DTMF_DETECT;
05062 }
05063 #endif
05064 if (features) {
05065 if (i->dsp) {
05066 ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", tmp->name);
05067 } else {
05068 if (i->channel != CHAN_PSEUDO)
05069 i->dsp = ast_dsp_new();
05070 else
05071 i->dsp = NULL;
05072 if (i->dsp) {
05073 i->dsp_features = features & ~DSP_PROGRESS_TALK;
05074 #ifdef ZAPATA_PRI
05075
05076 if (i->outgoing && (i->sig == SIG_PRI)) {
05077
05078
05079 features = 0;
05080 }
05081 #endif
05082 ast_dsp_set_features(i->dsp, features);
05083 ast_dsp_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax);
05084 if (!ast_strlen_zero(progzone))
05085 ast_dsp_set_call_progress_zone(i->dsp, progzone);
05086 if (i->busydetect && CANBUSYDETECT(i)) {
05087 ast_dsp_set_busy_count(i->dsp, i->busycount);
05088 ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength);
05089 }
05090 }
05091 }
05092 }
05093
05094 if (state == AST_STATE_RING)
05095 tmp->rings = 1;
05096 tmp->tech_pvt = i;
05097 if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) {
05098
05099 tmp->callgroup = i->callgroup;
05100 tmp->pickupgroup = i->pickupgroup;
05101 }
05102 if (!ast_strlen_zero(i->language))
05103 ast_copy_string(tmp->language, i->language, sizeof(tmp->language));
05104 if (!ast_strlen_zero(i->musicclass))
05105 ast_copy_string(tmp->musicclass, i->musicclass, sizeof(tmp->musicclass));
05106 if (!i->owner)
05107 i->owner = tmp;
05108 if (!ast_strlen_zero(i->accountcode))
05109 ast_copy_string(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode));
05110 if (i->amaflags)
05111 tmp->amaflags = i->amaflags;
05112 i->subs[index].owner = tmp;
05113 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
05114
05115 ast_copy_string(tmp->call_forward, i->call_forward, sizeof(tmp->call_forward));
05116
05117 if (!i->adsi)
05118 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
05119 if (!ast_strlen_zero(i->exten))
05120 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
05121 if (!ast_strlen_zero(i->rdnis))
05122 tmp->cid.cid_rdnis = strdup(i->rdnis);
05123 if (!ast_strlen_zero(i->dnid))
05124 tmp->cid.cid_dnid = strdup(i->dnid);
05125
05126 #ifdef PRI_ANI
05127 ast_set_callerid(tmp, i->cid_num, i->cid_name, ast_strlen_zero(i->cid_ani) ? i->cid_num : i->cid_ani);
05128 #else
05129 ast_set_callerid(tmp, i->cid_num, i->cid_name, i->cid_num);
05130 #endif
05131 tmp->cid.cid_pres = i->callingpres;
05132 tmp->cid.cid_ton = i->cid_ton;
05133 #ifdef ZAPATA_PRI
05134 tmp->transfercapability = transfercapability;
05135 pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
05136 if (transfercapability & PRI_TRANS_CAP_DIGITAL) {
05137 i->digital = 1;
05138 }
05139
05140 i->isidlecall = 0;
05141 i->alreadyhungup = 0;
05142 #endif
05143
05144 i->fake_event = 0;
05145
05146 zt_confmute(i, 0);
05147 ast_setstate(tmp, state);
05148 ast_mutex_lock(&usecnt_lock);
05149 usecnt++;
05150 ast_mutex_unlock(&usecnt_lock);
05151 ast_update_use_count();
05152 if (startpbx) {
05153 if (ast_pbx_start(tmp)) {
05154 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
05155 ast_hangup(tmp);
05156 tmp = NULL;
05157 }
05158 }
05159 } else
05160 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
05161 return tmp;
05162 }
05163
05164
05165 static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
05166 {
05167 char c;
05168
05169 *str = 0;
05170 for (;;)
05171 {
05172
05173 c = ast_waitfordigit(chan, ms);
05174
05175 if (c < 1)
05176 return c;
05177 *str++ = c;
05178 *str = 0;
05179 if (strchr(term, c))
05180 return 1;
05181 }
05182 }
05183
05184 static int zt_wink(struct zt_pvt *p, int index)
05185 {
05186 int j;
05187 zt_set_hook(p->subs[index].zfd, ZT_WINK);
05188 for(;;)
05189 {
05190
05191 j = ZT_IOMUX_SIGEVENT;
05192
05193 if (ioctl(p->subs[index].zfd,ZT_IOMUX,&j) == -1) return(-1);
05194
05195 if (j & ZT_IOMUX_SIGEVENT) break;
05196 }
05197
05198 if (ioctl(p->subs[index].zfd,ZT_GETEVENT,&j) == -1) return(-1);
05199 return 0;
05200 }
05201
05202 static void *ss_thread(void *data)
05203 {
05204 struct ast_channel *chan = data;
05205 struct zt_pvt *p = chan->tech_pvt;
05206 char exten[AST_MAX_EXTENSION]="";
05207 char exten2[AST_MAX_EXTENSION]="";
05208 unsigned char buf[256];
05209 char dtmfcid[300];
05210 char dtmfbuf[300];
05211 struct callerid_state *cs;
05212 char *name=NULL, *number=NULL;
05213 int distMatches;
05214 int curRingData[3];
05215 int receivedRingT;
05216 int counter1;
05217 int counter;
05218 int samples = 0;
05219
05220 int flags;
05221 int i;
05222 int timeout;
05223 int getforward=0;
05224 char *s1, *s2;
05225 int len = 0;
05226 int res;
05227 int index;
05228 if (option_verbose > 2)
05229 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name);
05230 index = zt_get_index(chan, p, 1);
05231 if (index < 0) {
05232 ast_log(LOG_WARNING, "Huh?\n");
05233 ast_hangup(chan);
05234 return NULL;
05235 }
05236 if (p->dsp)
05237 ast_dsp_digitreset(p->dsp);
05238 switch(p->sig) {
05239 #ifdef ZAPATA_PRI
05240 case SIG_PRI:
05241
05242 ast_copy_string(exten, p->exten, sizeof(exten));
05243 len = strlen(exten);
05244 res = 0;
05245 while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05246 if (len && !ast_ignore_pattern(chan->context, exten))
05247 tone_zone_play_tone(p->subs[index].zfd, -1);
05248 else
05249 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05250 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
05251 timeout = matchdigittimeout;
05252 else
05253 timeout = gendigittimeout;
05254 res = ast_waitfordigit(chan, timeout);
05255 if (res < 0) {
05256 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05257 ast_hangup(chan);
05258 return NULL;
05259 } else if (res) {
05260 exten[len++] = res;
05261 exten[len] = '\0';
05262 } else
05263 break;
05264 }
05265
05266 if (ast_strlen_zero(exten)) {
05267 if (option_verbose > 2)
05268 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n");
05269 exten[0] = 's';
05270 exten[1] = '\0';
05271 }
05272 tone_zone_play_tone(p->subs[index].zfd, -1);
05273 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
05274
05275 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05276 if (p->dsp) ast_dsp_digitreset(p->dsp);
05277 zt_enable_ec(p);
05278 ast_setstate(chan, AST_STATE_RING);
05279 res = ast_pbx_run(chan);
05280 if (res) {
05281 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
05282 }
05283 } else {
05284 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
05285 chan->hangupcause = AST_CAUSE_UNALLOCATED;
05286 ast_hangup(chan);
05287 p->exten[0] = '\0';
05288
05289 p->call = NULL;
05290 }
05291 return NULL;
05292 break;
05293 #endif
05294 case SIG_FEATD:
05295 case SIG_FEATDMF:
05296 case SIG_E911:
05297 case SIG_FEATB:
05298 case SIG_EMWINK:
05299 case SIG_SF_FEATD:
05300 case SIG_SF_FEATDMF:
05301 case SIG_SF_FEATB:
05302 case SIG_SFWINK:
05303 if (zt_wink(p, index))
05304 return NULL;
05305
05306 case SIG_EM:
05307 case SIG_EM_E1:
05308 case SIG_SF:
05309 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05310 if (p->dsp)
05311 ast_dsp_digitreset(p->dsp);
05312
05313 if (p->dsp) {
05314 if (NEED_MFDETECT(p))
05315 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax);
05316 else
05317 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05318 }
05319 memset(dtmfbuf, 0, sizeof(dtmfbuf));
05320
05321 if (!p->immediate)
05322
05323 res = ast_waitfordigit(chan, 5000);
05324 else res = 0;
05325 if (res > 0) {
05326
05327 dtmfbuf[0] = res;
05328 switch(p->sig) {
05329 case SIG_FEATD:
05330 case SIG_SF_FEATD:
05331 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05332 if (res > 0)
05333 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05334 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05335 break;
05336 case SIG_FEATDMF:
05337 case SIG_E911:
05338 case SIG_SF_FEATDMF:
05339 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05340 if (res > 0) {
05341
05342 if (p->sig == SIG_E911)
05343 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
05344 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
05345 }
05346 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05347 break;
05348 case SIG_FEATB:
05349 case SIG_SF_FEATB:
05350 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05351 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05352 break;
05353 case SIG_EMWINK:
05354
05355
05356
05357
05358 if (res == '*') {
05359 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05360 if (res > 0)
05361 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05362 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05363 break;
05364 }
05365 default:
05366
05367 len = 1;
05368 dtmfbuf[len] = '\0';
05369 while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05370 if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05371 timeout = matchdigittimeout;
05372 } else {
05373 timeout = gendigittimeout;
05374 }
05375 res = ast_waitfordigit(chan, timeout);
05376 if (res < 0) {
05377 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05378 ast_hangup(chan);
05379 return NULL;
05380 } else if (res) {
05381 dtmfbuf[len++] = res;
05382 dtmfbuf[len] = '\0';
05383 } else {
05384 break;
05385 }
05386 }
05387 break;
05388 }
05389 }
05390 if (res == -1) {
05391 ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
05392 ast_hangup(chan);
05393 return NULL;
05394 } else if (res < 0) {
05395 ast_log(LOG_DEBUG, "Got hung up before digits finished\n");
05396 ast_hangup(chan);
05397 return NULL;
05398 }
05399 ast_copy_string(exten, dtmfbuf, sizeof(exten));
05400 if (ast_strlen_zero(exten))
05401 ast_copy_string(exten, "s", sizeof(exten));
05402 if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
05403
05404 if (exten[0] == '*') {
05405 char *stringp=NULL;
05406 ast_copy_string(exten2, exten, sizeof(exten2));
05407
05408 stringp=exten2 +1;
05409 s1 = strsep(&stringp, "*");
05410 s2 = strsep(&stringp, "*");
05411 if (s2) {
05412 if (!ast_strlen_zero(p->cid_num))
05413 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05414 else
05415 ast_set_callerid(chan, s1, NULL, s1);
05416 ast_copy_string(exten, s2, sizeof(exten));
05417 } else
05418 ast_copy_string(exten, s1, sizeof(exten));
05419 } else if (p->sig == SIG_FEATD)
05420 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05421 }
05422 if (p->sig == SIG_FEATDMF) {
05423 if (exten[0] == '*') {
05424 char *stringp=NULL;
05425 ast_copy_string(exten2, exten, sizeof(exten2));
05426
05427 stringp=exten2 +1;
05428 s1 = strsep(&stringp, "#");
05429 s2 = strsep(&stringp, "#");
05430 if (s2) {
05431 if (!ast_strlen_zero(p->cid_num))
05432 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05433 else
05434 if(*(s1 + 2))
05435 ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
05436 ast_copy_string(exten, s2 + 1, sizeof(exten));
05437 } else
05438 ast_copy_string(exten, s1 + 2, sizeof(exten));
05439 } else
05440 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05441 }
05442 if (p->sig == SIG_E911) {
05443 if (exten[0] == '*') {
05444 char *stringp=NULL;
05445 ast_copy_string(exten2, exten, sizeof(exten2));
05446
05447 stringp=exten2 +1;
05448 s1 = strsep(&stringp, "#");
05449 s2 = strsep(&stringp, "#");
05450 if (s2 && (*(s2 + 1) == '0')) {
05451 if(*(s2 + 2))
05452 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
05453 }
05454 if (s1) ast_copy_string(exten, s1, sizeof(exten));
05455 else ast_copy_string(exten, "911", sizeof(exten));
05456 printf("E911: exten: %s, ANI: %s\n",exten, chan->cid.cid_ani);
05457 } else
05458 ast_log(LOG_WARNING, "Got a non-E911 input on channel %d. Assuming E&M Wink instead\n", p->channel);
05459 }
05460 if (p->sig == SIG_FEATB) {
05461 if (exten[0] == '*') {
05462 char *stringp=NULL;
05463 ast_copy_string(exten2, exten, sizeof(exten2));
05464
05465 stringp=exten2 +1;
05466 s1 = strsep(&stringp, "#");
05467 ast_copy_string(exten, exten2 + 1, sizeof(exten));
05468 } else
05469 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
05470 }
05471 if (p->sig == SIG_FEATDMF) {
05472 zt_wink(p, index);
05473 }
05474 zt_enable_ec(p);
05475 if (NEED_MFDETECT(p)) {
05476 if (p->dsp) {
05477 if (!p->hardwaredtmf)
05478 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05479 else {
05480 ast_dsp_free(p->dsp);
05481 p->dsp = NULL;
05482 }
05483 }
05484 }
05485
05486 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
05487 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05488 if (p->dsp) ast_dsp_digitreset(p->dsp);
05489 res = ast_pbx_run(chan);
05490 if (res) {
05491 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05492 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05493 }
05494 return NULL;
05495 } else {
05496 if (option_verbose > 2)
05497 ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
05498 sleep(2);
05499 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_INFO);
05500 if (res < 0)
05501 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
05502 else
05503 sleep(1);
05504 res = ast_streamfile(chan, "ss-noservice", chan->language);
05505 if (res >= 0)
05506 ast_waitstream(chan, "");
05507 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05508 ast_hangup(chan);
05509 return NULL;
05510 }
05511 break;
05512 case SIG_FXOLS:
05513 case SIG_FXOGS:
05514 case SIG_FXOKS:
05515
05516 timeout = firstdigittimeout;
05517
05518
05519 if (p->subs[SUB_THREEWAY].owner)
05520 timeout = 999999;
05521 while(len < AST_MAX_EXTENSION-1) {
05522
05523
05524 if (p->immediate)
05525 res = 's';
05526 else
05527 res = ast_waitfordigit(chan, timeout);
05528 timeout = 0;
05529 if (res < 0) {
05530 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05531 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05532 ast_hangup(chan);
05533 return NULL;
05534 } else if (res) {
05535 exten[len++]=res;
05536 exten[len] = '\0';
05537 }
05538 if (!ast_ignore_pattern(chan->context, exten))
05539 tone_zone_play_tone(p->subs[index].zfd, -1);
05540 else
05541 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05542 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
05543 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05544 if (getforward) {
05545
05546 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
05547 if (option_verbose > 2)
05548 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
05549 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05550 if (res)
05551 break;
05552 usleep(500000);
05553 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05554 sleep(1);
05555 memset(exten, 0, sizeof(exten));
05556 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05557 len = 0;
05558 getforward = 0;
05559 } else {
05560 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05561 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05562 if (!ast_strlen_zero(p->cid_num)) {
05563 if (!p->hidecallerid)
05564 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05565 else
05566 ast_set_callerid(chan, NULL, NULL, p->cid_num);
05567 }
05568 if (!ast_strlen_zero(p->cid_name)) {
05569 if (!p->hidecallerid)
05570 ast_set_callerid(chan, NULL, p->cid_name, NULL);
05571 }
05572 ast_setstate(chan, AST_STATE_RING);
05573 zt_enable_ec(p);
05574 res = ast_pbx_run(chan);
05575 if (res) {
05576 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05577 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05578 }
05579 return NULL;
05580 }
05581 } else {
05582
05583
05584 timeout = matchdigittimeout;
05585 }
05586 } else if (res == 0) {
05587 ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n");
05588 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05589 zt_wait_event(p->subs[index].zfd);
05590 ast_hangup(chan);
05591 return NULL;
05592 } else if (p->callwaiting && !strcmp(exten, "*70")) {
05593 if (option_verbose > 2)
05594 ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
05595
05596 p->callwaiting = 0;
05597 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05598 if (res) {
05599 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05600 chan->name, strerror(errno));
05601 }
05602 len = 0;
05603 ioctl(p->subs[index].zfd,ZT_CONFDIAG,&len);
05604 memset(exten, 0, sizeof(exten));
05605 timeout = firstdigittimeout;
05606
05607 } else if (!strcmp(exten,ast_pickup_ext())) {
05608
05609
05610
05611
05612 if (index == SUB_REAL) {
05613
05614 if (p->subs[SUB_THREEWAY].owner) {
05615
05616
05617 alloc_sub(p, SUB_CALLWAIT);
05618 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
05619 unalloc_sub(p, SUB_THREEWAY);
05620 }
05621 zt_enable_ec(p);
05622 if (ast_pickup_call(chan)) {
05623 ast_log(LOG_DEBUG, "No call pickup possible...\n");
05624 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05625 zt_wait_event(p->subs[index].zfd);
05626 }
05627 ast_hangup(chan);
05628 return NULL;
05629 } else {
05630 ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
05631 ast_hangup(chan);
05632 return NULL;
05633 }
05634
05635 } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
05636 if (option_verbose > 2)
05637 ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
05638
05639 p->hidecallerid = 1;
05640 if (chan->cid.cid_num)
05641 free(chan->cid.cid_num);
05642 chan->cid.cid_num = NULL;
05643 if (chan->cid.cid_name)
05644 free(chan->cid.cid_name);
05645 chan->cid.cid_name = NULL;
05646 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05647 if (res) {
05648 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05649 chan->name, strerror(errno));
05650 }
05651 len = 0;
05652 memset(exten, 0, sizeof(exten));
05653 timeout = firstdigittimeout;
05654 } else if (p->callreturn && !strcmp(exten, "*69")) {
05655 res = 0;
05656 if (!ast_strlen_zero(p->lastcid_num)) {
05657 res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
05658 }
05659 if (!res)
05660 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05661 break;
05662 } else if (!strcmp(exten, "*78")) {
05663
05664 if (option_verbose > 2) {
05665 ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel);
05666 }
05667 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05668 "Channel: Zap/%d\r\n"
05669 "Status: enabled\r\n", p->channel);
05670 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05671 p->dnd = 1;
05672 getforward = 0;
05673 memset(exten, 0, sizeof(exten));
05674 len = 0;
05675 } else if (!strcmp(exten, "*79")) {
05676
05677 if (option_verbose > 2)
05678 ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel);
05679 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05680 "Channel: Zap/%d\r\n"
05681 "Status: disabled\r\n", p->channel);
05682 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05683 p->dnd = 0;
05684 getforward = 0;
05685 memset(exten, 0, sizeof(exten));
05686 len = 0;
05687 } else if (p->cancallforward && !strcmp(exten, "*72")) {
05688 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05689 getforward = 1;
05690 memset(exten, 0, sizeof(exten));
05691 len = 0;
05692 } else if (p->cancallforward && !strcmp(exten, "*73")) {
05693 if (option_verbose > 2)
05694 ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel);
05695 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05696 memset(p->call_forward, 0, sizeof(p->call_forward));
05697 getforward = 0;
05698 memset(exten, 0, sizeof(exten));
05699 len = 0;
05700 } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
05701 p->subs[SUB_THREEWAY].owner &&
05702 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
05703
05704
05705 ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL);
05706 if (option_verbose > 2)
05707 ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
05708 break;
05709 } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
05710 if (option_verbose > 2)
05711 ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num);
05712 res = ast_db_put("blacklist", p->lastcid_num, "1");
05713 if (!res) {
05714 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05715 memset(exten, 0, sizeof(exten));
05716 len = 0;
05717 }
05718 } else if (p->hidecallerid && !strcmp(exten, "*82")) {
05719 if (option_verbose > 2)
05720 ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
05721
05722 p->hidecallerid = 0;
05723 if (chan->cid.cid_num)
05724 free(chan->cid.cid_num);
05725 chan->cid.cid_num = NULL;
05726 if (chan->cid.cid_name)
05727 free(chan->cid.cid_name);
05728 chan->cid.cid_name = NULL;
05729 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
05730 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05731 if (res) {
05732 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05733 chan->name, strerror(errno));
05734 }
05735 len = 0;
05736 memset(exten, 0, sizeof(exten));
05737 timeout = firstdigittimeout;
05738 } else if (!strcmp(exten, "*0")) {
05739 struct ast_channel *nbridge =
05740 p->subs[SUB_THREEWAY].owner;
05741 struct zt_pvt *pbridge = NULL;
05742
05743 if (nbridge && ast_bridged_channel(nbridge))
05744 pbridge = ast_bridged_channel(nbridge)->tech_pvt;
05745 if (nbridge && pbridge &&
05746 (!strcmp(nbridge->type,"Zap")) &&
05747 (!strcmp(ast_bridged_channel(nbridge)->type, "Zap")) &&
05748 ISTRUNK(pbridge)) {
05749 int func = ZT_FLASH;
05750
05751 p->dop.dialstr[0] = '\0';
05752
05753 if ((ioctl(pbridge->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
05754 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
05755 nbridge->name, strerror(errno));
05756 }
05757 swap_subs(p, SUB_REAL, SUB_THREEWAY);
05758 unalloc_sub(p, SUB_THREEWAY);
05759 p->owner = p->subs[SUB_REAL].owner;
05760 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
05761 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
05762 ast_hangup(chan);
05763 return NULL;
05764 } else {
05765 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05766 zt_wait_event(p->subs[index].zfd);
05767 tone_zone_play_tone(p->subs[index].zfd, -1);
05768 swap_subs(p, SUB_REAL, SUB_THREEWAY);
05769 unalloc_sub(p, SUB_THREEWAY);
05770 p->owner = p->subs[SUB_REAL].owner;
05771 ast_hangup(chan);
05772 return NULL;
05773 }
05774 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
05775 ((exten[0] != '*') || (strlen(exten) > 2))) {
05776 if (option_debug)
05777 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);
05778 break;
05779 }
05780 if (!timeout)
05781 timeout = gendigittimeout;
05782 if (len && !ast_ignore_pattern(chan->context, exten))
05783 tone_zone_play_tone(p->subs[index].zfd, -1);
05784 }
05785 break;
05786 case SIG_FXSLS:
05787 case SIG_FXSGS:
05788 case SIG_FXSKS:
05789 #ifdef ZAPATA_PRI
05790 if (p->pri) {
05791
05792 struct ast_frame *f;
05793 int res;
05794 time_t start;
05795
05796 time(&start);
05797 ast_setstate(chan, AST_STATE_RING);
05798 while(time(NULL) < start + 3) {
05799 res = ast_waitfor(chan, 1000);
05800 if (res) {
05801 f = ast_read(chan);
05802 if (!f) {
05803 ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
05804 ast_hangup(chan);
05805 return NULL;
05806 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
05807 res = 1;
05808 } else
05809 res = 0;
05810 ast_frfree(f);
05811 if (res) {
05812 ast_log(LOG_DEBUG, "Got ring!\n");
05813 res = 0;
05814 break;
05815 }
05816 }
05817 }
05818 }
05819 #endif
05820
05821
05822 if (p->use_callerid && p->cid_start == CID_START_USEHIST) {
05823 ast_log(LOG_DEBUG,"Using history buffer to extract UK caller ID\n");
05824 cs = callerid_new(cid_signalling);
05825 if (cs) {
05826 unsigned char cidbuf[16384];
05827 res=0;
05828
05829 res = zt_get_history(p->subs[index].zfd,cidbuf,sizeof(cidbuf));
05830 if(res<0) {
05831 ast_log(LOG_ERROR,"zt_get_history failed: %s\n", strerror(errno));
05832 } else {
05833 res=callerid_feed(cs,cidbuf,sizeof(cidbuf),AST_LAW(p));
05834 if (res < 0) {
05835 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
05836 }
05837 }
05838
05839 if(res==1) {
05840 callerid_get(cs, &name, &number, &flags);
05841 if (option_debug)
05842 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
05843 }
05844 }
05845 if (p->usedistinctiveringdetection == 1) {
05846 #if 1
05847 bump_gains(p);
05848 #endif
05849 len = 0;
05850 distMatches = 0;
05851
05852 for (receivedRingT=0; receivedRingT < 3; receivedRingT++) {
05853 curRingData[receivedRingT] = 0;
05854 }
05855 receivedRingT = 0;
05856 counter = 0;
05857 counter1 = 0;
05858
05859 if (strcmp(p->context,p->defcontext) != 0) {
05860 strncpy(p->context, p->defcontext, sizeof(p->context)-1);
05861 strncpy(chan->context,p->defcontext,sizeof(chan->context)-1);
05862 }
05863
05864 for(;;) {
05865 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
05866 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
05867 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
05868 callerid_free(cs);
05869 ast_hangup(chan);
05870 return NULL;
05871 }
05872 if (i & ZT_IOMUX_SIGEVENT) {
05873 res = zt_get_event(p->subs[index].zfd);
05874 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
05875 res = 0;
05876
05877
05878 curRingData[receivedRingT] = p->ringt;
05879
05880 if (p->ringt < ringt_base/2)
05881 break;
05882 ++receivedRingT;
05883
05884 } else if (i & ZT_IOMUX_READ) {
05885 res = read(p->subs[index].zfd, buf, sizeof(buf));
05886 if (res < 0) {
05887 if (errno != ELAST) {
05888 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
05889 callerid_free(cs);
05890 ast_hangup(chan);
05891 return NULL;
05892 }
05893 break;
05894 }
05895 if (p->ringt)
05896 p->ringt--;
05897 if (p->ringt == 1) {
05898 res = -1;
05899 break;
05900 }
05901 }
05902 }
05903 if(option_verbose > 2)
05904
05905 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
05906
05907 for (counter=0; counter < 3; counter++) {
05908
05909
05910 distMatches = 0;
05911 for (counter1=0; counter1 < 3; counter1++) {
05912 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
05913 (p->drings.ringnum[counter].ring[counter1]-10)) {
05914 distMatches++;
05915 }
05916 }
05917 if (distMatches == 3) {
05918
05919 strncpy(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context)-1);
05920 strncpy(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context)-1);
05921 if(option_verbose > 2)
05922 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
05923 break;
05924 }
05925 }
05926 }
05927
05928 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
05929 #if 1
05930 restore_gains(p);
05931 #endif
05932 }
05933
05934
05935
05936
05937 if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) {
05938
05939 if (p->cid_signalling == CID_SIG_DTMF) {
05940 int i = 0;
05941 cs = NULL;
05942 ast_log(LOG_DEBUG, "Receiving DTMF cid on "
05943 "channel %s\n", chan->name);
05944 zt_setlinear(p->subs[index].zfd, 0);
05945 res = 2000;
05946 for (;;) {
05947 struct ast_frame *f;
05948 res = ast_waitfor(chan, res);
05949 if (res <= 0) {
05950 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
05951 "Exiting simple switch\n");
05952 ast_hangup(chan);
05953 return NULL;
05954 }
05955 f = ast_read(chan);
05956 if (f->frametype == AST_FRAME_DTMF) {
05957 dtmfbuf[i++] = f->subclass;
05958 ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
05959 res = 2000;
05960 }
05961 ast_frfree(f);
05962 if (chan->_state == AST_STATE_RING ||
05963 chan->_state == AST_STATE_RINGING)
05964 break;
05965 }
05966 dtmfbuf[i] = 0;
05967 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
05968
05969 ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf);
05970 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
05971 ast_log(LOG_DEBUG, "CID is '%s', flags %d\n",
05972 dtmfcid, flags);
05973
05974 if (dtmfcid[0])
05975 number = dtmfcid;
05976 else
05977 number = 0;
05978
05979 } else if (p->cid_signalling == CID_SIG_V23) {
05980 cs = callerid_new(p->cid_signalling);
05981 if (cs) {
05982 samples = 0;
05983 #if 1
05984 bump_gains(p);
05985 #endif
05986
05987 zt_setlinear(p->subs[index].zfd, 0);
05988
05989
05990 for(;;) {
05991 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
05992 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
05993 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
05994 callerid_free(cs);
05995 ast_hangup(chan);
05996 return NULL;
05997 }
05998 if (i & ZT_IOMUX_SIGEVENT) {
05999 res = zt_get_event(p->subs[index].zfd);
06000 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06001 res = 0;
06002 break;
06003 } else if (i & ZT_IOMUX_READ) {
06004 res = read(p->subs[index].zfd, buf, sizeof(buf));
06005 if (res < 0) {
06006 if (errno != ELAST) {
06007 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06008 callerid_free(cs);
06009 ast_hangup(chan);
06010 return NULL;
06011 }
06012 break;
06013 }
06014 samples += res;
06015 res = callerid_feed(cs, buf, res, AST_LAW(p));
06016 if (res < 0) {
06017 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06018 break;
06019 } else if (res)
06020 break;
06021 else if (samples > (8000 * 10))
06022 break;
06023 }
06024 }
06025 if (res == 1) {
06026 callerid_get(cs, &name, &number, &flags);
06027 if (option_debug)
06028 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06029 }
06030 if (res < 0) {
06031 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06032 }
06033
06034
06035 res = 2000;
06036 for (;;) {
06037 struct ast_frame *f;
06038 res = ast_waitfor(chan, res);
06039 if (res <= 0) {
06040 ast_log(LOG_WARNING, "CID timed out waiting for ring. "
06041 "Exiting simple switch\n");
06042 ast_hangup(chan);
06043 return NULL;
06044 }
06045 f = ast_read(chan);
06046 ast_frfree(f);
06047 if (chan->_state == AST_STATE_RING ||
06048 chan->_state == AST_STATE_RINGING)
06049 break;
06050 }
06051
06052
06053
06054 if (p->usedistinctiveringdetection == 1) {
06055 len = 0;
06056 distMatches = 0;
06057
06058 for (receivedRingT=0; receivedRingT < 3; receivedRingT++) {
06059 curRingData[receivedRingT] = 0;
06060 }
06061 receivedRingT = 0;
06062 counter = 0;
06063 counter1 = 0;
06064
06065 if (strcmp(p->context,p->defcontext) != 0) {
06066 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06067 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06068 }
06069
06070 for(;;) {
06071 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06072 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
06073 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06074 callerid_free(cs);
06075 ast_hangup(chan);
06076 return NULL;
06077 }
06078 if (i & ZT_IOMUX_SIGEVENT) {
06079 res = zt_get_event(p->subs[index].zfd);
06080 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06081 res = 0;
06082
06083
06084 curRingData[receivedRingT] = p->ringt;
06085
06086 if (p->ringt < p->ringt_base/2)
06087 break;
06088 ++receivedRingT;
06089
06090 } else if (i & ZT_IOMUX_READ) {
06091 res = read(p->subs[index].zfd, buf, sizeof(buf));
06092 if (res < 0) {
06093 if (errno != ELAST) {
06094 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06095 callerid_free(cs);
06096 ast_hangup(chan);
06097 return NULL;
06098 }
06099 break;
06100 }
06101 if (p->ringt)
06102 p->ringt--;
06103 if (p->ringt == 1) {
06104 res = -1;
06105 break;
06106 }
06107 }
06108 }
06109 if(option_verbose > 2)
06110
06111 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06112
06113 for (counter=0; counter < 3; counter++) {
06114
06115
06116 distMatches = 0;
06117 for (counter1=0; counter1 < 3; counter1++) {
06118 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06119 (p->drings.ringnum[counter].ring[counter1]-10)) {
06120 distMatches++;
06121 }
06122 }
06123 if (distMatches == 3) {
06124
06125 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06126 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06127 if(option_verbose > 2)
06128 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06129 break;
06130 }
06131 }
06132 }
06133
06134 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06135 #if 1
06136 restore_gains(p);
06137 #endif
06138 } else
06139 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06140 } else {
06141 ast_log(LOG_WARNING, "Channel %s in prering "
06142 "state, but I have nothing to do. "
06143 "Terminating simple switch, should be "
06144 "restarted by the actual ring.\n",
06145 chan->name);
06146 ast_hangup(chan);
06147 return NULL;
06148 }
06149 } else if (p->use_callerid && p->cid_start == CID_START_RING) {
06150
06151 cs = callerid_new(p->cid_signalling);
06152 if (cs) {
06153 #if 1
06154 bump_gains(p);
06155 #endif
06156 samples = 0;
06157 len = 0;
06158 distMatches = 0;
06159
06160 for (receivedRingT=0; receivedRingT < 3; receivedRingT++) {
06161 curRingData[receivedRingT] = 0;
06162 }
06163 receivedRingT = 0;
06164 counter = 0;
06165 counter1 = 0;
06166
06167 if (strcmp(p->context,p->defcontext) != 0) {
06168 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06169 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06170 }
06171
06172
06173 zt_setlinear(p->subs[index].zfd, 0);
06174 for(;;) {
06175 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06176 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
06177 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06178 callerid_free(cs);
06179 ast_hangup(chan);
06180 return NULL;
06181 }
06182 if (i & ZT_IOMUX_SIGEVENT) {
06183 res = zt_get_event(p->subs[index].zfd);
06184 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06185 res = 0;
06186
06187
06188 curRingData[receivedRingT] = p->ringt;
06189
06190 if (p->ringt < p->ringt_base/2)
06191 break;
06192 ++receivedRingT;
06193
06194 } else if (i & ZT_IOMUX_READ) {
06195 res = read(p->subs[index].zfd, buf, sizeof(buf));
06196 if (res < 0) {
06197 if (errno != ELAST) {
06198 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06199 callerid_free(cs);
06200 ast_hangup(chan);
06201 return NULL;
06202 }
06203 break;
06204 }
06205 if (p->ringt)
06206 p->ringt--;
06207 if (p->ringt == 1) {
06208 res = -1;
06209 break;
06210 }
06211 samples += res;
06212 res = callerid_feed(cs, buf, res, AST_LAW(p));
06213 if (res < 0) {
06214 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06215 break;
06216 } else if (res)
06217 break;
06218 else if (samples > (8000 * 10))
06219 break;
06220 }
06221 }
06222 if (p->usedistinctiveringdetection == 1) {
06223 if(option_verbose > 2)
06224
06225 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06226
06227 for (counter=0; counter < 3; counter++) {
06228
06229
06230 distMatches = 0;
06231 for (counter1=0; counter1 < 3; counter1++) {
06232 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06233 (p->drings.ringnum[counter].ring[counter1]-10)) {
06234 distMatches++;
06235 }
06236 }
06237 if (distMatches == 3) {
06238
06239 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06240 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06241 if(option_verbose > 2)
06242 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06243 break;
06244 }
06245 }
06246 }
06247 if (res == 1) {
06248 callerid_get(cs, &name, &number, &flags);
06249 if (option_debug)
06250 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06251 }
06252
06253 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06254 #if 1
06255 restore_gains(p);
06256 #endif
06257 if (res < 0) {
06258 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06259 }
06260 } else
06261 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06262 }
06263 else
06264 cs = NULL;
06265 if (number || name) {
06266 if (chan->cid.cid_num) {
06267 free(chan->cid.cid_num);
06268 chan->cid.cid_num = NULL;
06269 }
06270 if (chan->cid.cid_name) {
06271 free(chan->cid.cid_name);
06272 chan->cid.cid_name = NULL;
06273 }
06274 }
06275 if (number)
06276 ast_shrink_phone_number(number);
06277
06278 ast_set_callerid(chan, number, name, number);
06279
06280 if (cs)
06281 callerid_free(cs);
06282 ast_setstate(chan, AST_STATE_RING);
06283 chan->rings = 1;
06284 p->ringt = p->ringt_base;
06285 res = ast_pbx_run(chan);
06286 if (res) {
06287 ast_hangup(chan);
06288 ast_log(LOG_WARNING, "PBX exited non-zero\n");
06289 }
06290 return NULL;
06291 default:
06292 ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
06293 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06294 if (res < 0)
06295 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06296 }
06297 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06298 if (res < 0)
06299 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06300 ast_hangup(chan);
06301 return NULL;
06302 }
06303
06304 #ifdef ZAPATA_R2
06305 static int handle_init_r2_event(struct zt_pvt *i, mfcr2_event_t *e)
06306 {
06307 struct ast_channel *chan;
06308
06309 switch(e->e) {
06310 case MFCR2_EVENT_UNBLOCKED:
06311 i->r2blocked = 0;
06312 if (option_verbose > 2)
06313 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
06314 break;
06315 case MFCR2_EVENT_BLOCKED:
06316 i->r2blocked = 1;
06317 if (option_verbose > 2)
06318 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
06319 break;
06320 case MFCR2_EVENT_IDLE:
06321 if (option_verbose > 2)
06322 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d idle\n", i->channel);
06323 break;
06324 case MFCR2_EVENT_RINGING:
06325
06326
06327
06328 i->hasr2call = 1;
06329 chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06330 if (!chan) {
06331 ast_log(LOG_WARNING, "Unable to create channel for channel %d\n", i->channel);
06332 mfcr2_DropCall(i->r2, NULL, UC_NETWORK_CONGESTION);
06333 i->hasr2call = 0;
06334 }
06335 if (ast_pbx_start(chan)) {
06336 ast_log(LOG_WARNING, "Unable to start PBX on channel %s\n", chan->name);
06337 ast_hangup(chan);
06338 }
06339 break;
06340 default:
06341 ast_log(LOG_WARNING, "Don't know how to handle initial R2 event %s on channel %d\n", mfcr2_event2str(e->e), i->channel);
06342 return -1;
06343 }
06344 return 0;
06345 }
06346 #endif
06347
06348 static int handle_init_event(struct zt_pvt *i, int event)
06349 {
06350 int res;
06351 pthread_t threadid;
06352 pthread_attr_t attr;
06353 struct ast_channel *chan;
06354 pthread_attr_init(&attr);
06355 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06356
06357 switch(event) {
06358 case ZT_EVENT_NONE:
06359 case ZT_EVENT_BITSCHANGED:
06360 if (i->radio) break;
06361 #ifdef ZAPATA_R2
06362 if (i->r2) {
06363 mfcr2_event_t *e;
06364 e = r2_get_event_bits(i);
06365 i->sigchecked = 1;
06366 if (e)
06367 handle_init_r2_event(i, e);
06368 }
06369 #endif
06370 break;
06371 case ZT_EVENT_WINKFLASH:
06372 case ZT_EVENT_RINGOFFHOOK:
06373 if (i->inalarm) break;
06374 if (i->radio) break;
06375
06376 switch(i->sig) {
06377 case SIG_FXOLS:
06378 case SIG_FXOGS:
06379 case SIG_FXOKS:
06380 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06381 if (i->cidspill) {
06382
06383 free(i->cidspill);
06384 i->cidspill = NULL;
06385 }
06386 if (i->immediate) {
06387 zt_enable_ec(i);
06388
06389 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
06390 chan = zt_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
06391 if (!chan) {
06392 ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
06393 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06394 if (res < 0)
06395 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06396 }
06397 } else {
06398
06399 chan = zt_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
06400 if (chan) {
06401 if (has_voicemail(i))
06402 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
06403 else
06404 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
06405 if (res < 0)
06406 ast_log(LOG_WARNING, "Unable to play dialtone on channel %d\n", i->channel);
06407 if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06408 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06409 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06410 if (res < 0)
06411 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06412 ast_hangup(chan);
06413 }
06414 } else
06415 ast_log(LOG_WARNING, "Unable to create channel\n");
06416 #if 0
06417 printf("Created thread %ld detached in switch\n", threadid);
06418 #endif
06419 }
06420 break;
06421 case SIG_FXSLS:
06422 case SIG_FXSGS:
06423 case SIG_FXSKS:
06424 i->ringt = i->ringt_base;
06425
06426 case SIG_EMWINK:
06427 case SIG_FEATD:
06428 case SIG_FEATDMF:
06429 case SIG_E911:
06430 case SIG_FEATB:
06431 case SIG_EM:
06432 case SIG_EM_E1:
06433 case SIG_SFWINK:
06434 case SIG_SF_FEATD:
06435 case SIG_SF_FEATDMF:
06436 case SIG_SF_FEATB:
06437 case SIG_SF:
06438
06439 chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06440 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06441 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06442 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06443 if (res < 0)
06444 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06445 ast_hangup(chan);
06446 } else if (!chan) {
06447 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
06448 }
06449 #if 0
06450 printf("Created thread %ld detached in switch(2)\n", threadid);
06451 #endif
06452 break;
06453 default:
06454 ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06455 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06456 if (res < 0)
06457 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06458 return -1;
06459 }
06460 break;
06461 case ZT_EVENT_NOALARM:
06462 i->inalarm = 0;
06463 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
06464 break;
06465 case ZT_EVENT_ALARM:
06466 i->inalarm = 1;
06467 res = get_alarms(i);
06468 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", i->channel, alarm2str(res));
06469
06470 case ZT_EVENT_ONHOOK:
06471 if (i->radio) break;
06472
06473 switch(i->sig) {
06474 case SIG_FXOLS:
06475 case SIG_FXOGS:
06476 case SIG_FEATD:
06477 case SIG_FEATDMF:
06478 case SIG_E911:
06479 case SIG_FEATB:
06480 case SIG_EM:
06481 case SIG_EM_E1:
06482 case SIG_EMWINK:
06483 case SIG_SF_FEATD:
06484 case SIG_SF_FEATDMF:
06485 case SIG_SF_FEATB:
06486 case SIG_SF:
06487 case SIG_SFWINK:
06488 case SIG_FXSLS:
06489 case SIG_FXSGS:
06490 case SIG_FXSKS:
06491 case SIG_GR303FXSKS:
06492 zt_disable_ec(i);
06493 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06494 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06495 break;
06496 case SIG_GR303FXOKS:
06497 case SIG_FXOKS:
06498 zt_disable_ec(i);
06499
06500 #ifdef ZHONE_HACK
06501 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06502 usleep(1);
06503 #endif
06504 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06505 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06506 break;
06507 case SIG_PRI:
06508 zt_disable_ec(i);
06509 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06510 break;
06511 default:
06512 ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06513 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06514 return -1;
06515 }
06516 break;
06517 case ZT_EVENT_POLARITY:
06518 switch(i->sig) {
06519 case SIG_FXSLS:
06520 case SIG_FXSKS:
06521 case SIG_FXSGS:
06522 if (i->cid_start == CID_START_POLARITY) {
06523 i->polarity = POLARITY_REV;
06524 ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
06525 "CID detection on channel %d\n",
06526 i->channel);
06527 chan = zt_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
06528 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06529 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06530 }
06531 }
06532 break;
06533 default:
06534 ast_log(LOG_WARNING, "handle_init_event detected "
06535 "polarity reversal on non-FXO (SIG_FXS) "
06536 "interface %d\n", i->channel);
06537 }
06538 }
06539 return 0;
06540 }
06541
06542 static void *do_monitor(void *data)
06543 {
06544 int count, res, res2, spoint, pollres=0;
06545 struct zt_pvt *i;
06546 struct zt_pvt *last = NULL;
06547 time_t thispass = 0, lastpass = 0;
06548 int found;
06549 char buf[1024];
06550 struct pollfd *pfds=NULL;
06551 int lastalloc = -1;
06552
06553
06554
06555 #if 0
06556 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
06557 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
06558 return NULL;
06559 }
06560 ast_log(LOG_DEBUG, "Monitor starting...\n");
06561 #endif
06562 for(;;) {
06563
06564 if (ast_mutex_lock(&iflock)) {
06565 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
06566 return NULL;
06567 }
06568 if (!pfds || (lastalloc != ifcount)) {
06569 if (pfds)
06570 free(pfds);
06571 if (ifcount) {
06572 pfds = malloc(ifcount * sizeof(struct pollfd));
06573 if (!pfds) {
06574 ast_log(LOG_WARNING, "Critical memory error. Zap dies.\n");
06575 ast_mutex_unlock(&iflock);
06576 return NULL;
06577 }
06578 }
06579 lastalloc = ifcount;
06580 }
06581
06582
06583 count = 0;
06584 i = iflist;
06585 while(i) {
06586 if ((i->subs[SUB_REAL].zfd > -1) && i->sig && (!i->radio)) {
06587 if (!i->owner && !i->subs[SUB_REAL].owner) {
06588
06589 pfds[count].fd = i->subs[SUB_REAL].zfd;
06590 pfds[count].events = POLLPRI;
06591 pfds[count].revents = 0;
06592
06593 #ifdef ZAPATA_R2
06594 if (i->cidspill || i->r2)
06595 #else
06596 if (i->cidspill)
06597 #endif
06598 pfds[count].events |= POLLIN;
06599 count++;
06600 }
06601 }
06602 i = i->next;
06603 }
06604
06605 ast_mutex_unlock(&iflock);
06606
06607 pthread_testcancel();
06608
06609 res = poll(pfds, count, 1000);
06610 pthread_testcancel();
06611
06612 if (res < 0) {
06613 if ((errno != EAGAIN) && (errno != EINTR))
06614 ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
06615 continue;
06616 }
06617
06618
06619 if (ast_mutex_lock(&iflock)) {
06620 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
06621 continue;
06622 }
06623 found = 0;
06624 spoint = 0;
06625 lastpass = thispass;
06626 thispass = time(NULL);
06627 i = iflist;
06628 while(i) {
06629 if (thispass != lastpass) {
06630 if (!found && ((i == last) || ((i == iflist) && !last))) {
06631 last = i;
06632 if (last) {
06633 #if 0
06634 printf("Checking channel %d\n", last->channel);
06635 #endif
06636 if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) &&
06637 (last->sig & __ZT_SIG_FXO)) {
06638 #if 0
06639 printf("Channel %d has mailbox %s\n", last->channel, last->mailbox);
06640 #endif
06641 res = ast_app_has_voicemail(last->mailbox, NULL);
06642 if (last->msgstate != res) {
06643 int x;
06644 ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel);
06645 x = ZT_FLUSH_BOTH;
06646 res2 = ioctl(last->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
06647 if (res2)
06648 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", last->channel);
06649 last->cidspill = malloc(MAX_CALLERID_SIZE);
06650 if (last->cidspill) {
06651
06652 x = 4000;
06653 ioctl(last->subs[SUB_REAL].zfd, ZT_ONHOOKTRANSFER, &x);
06654 last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last));
06655 last->cidpos = 0;
06656 #if 0
06657 printf("Made %d bytes of message waiting for %d\n", last->cidlen, res);
06658 #endif
06659 last->msgstate = res;
06660 last->onhooktime = thispass;
06661 }
06662 found ++;
06663 }
06664 }
06665 last = last->next;
06666 }
06667 }
06668 }
06669 if ((i->subs[SUB_REAL].zfd > -1) && i->sig) {
06670 if (i->radio && !i->owner)
06671 {
06672 res = zt_get_event(i->subs[SUB_REAL].zfd);
06673 if (res)
06674 {
06675 if (option_debug)
06676 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
06677
06678 ast_mutex_unlock(&iflock);
06679 handle_init_event(i, res);
06680 ast_mutex_lock(&iflock);
06681 }
06682 i = i->next;
06683 continue;
06684 }
06685 pollres = ast_fdisset(pfds, i->subs[SUB_REAL].zfd, count, &spoint);
06686 if (pollres & POLLIN) {
06687 if (i->owner || i->subs[SUB_REAL].owner) {
06688 #ifdef ZAPATA_PRI
06689 if (!i->pri)
06690 #endif
06691 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].zfd);
06692 i = i->next;
06693 continue;
06694 }
06695 #ifdef ZAPATA_R2
06696 if (i->r2) {
06697
06698 mfcr2_event_t *e;
06699 e = mfcr2_check_event(i->r2);
06700 if (e)
06701 handle_init_r2_event(i, e);
06702 else {
06703 e = mfcr2_schedule_run(i->r2);
06704 if (e)
06705 handle_init_r2_event(i, e);
06706 }
06707 i = i->next;
06708 continue;
06709 }
06710 #endif
06711 if (!i->cidspill) {
06712 ast_log(LOG_WARNING, "Whoa.... I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].zfd);
06713 i = i->next;
06714 continue;
06715 }
06716 res = read(i->subs[SUB_REAL].zfd, buf, sizeof(buf));
06717 if (res > 0) {
06718
06719 if (res > i->cidlen - i->cidpos)
06720 res = i->cidlen - i->cidpos;
06721 res2 = write(i->subs[SUB_REAL].zfd, i->cidspill + i->cidpos, res);
06722 if (res2 > 0) {
06723 i->cidpos += res2;
06724 if (i->cidpos >= i->cidlen) {
06725 free(i->cidspill);
06726 i->cidspill = 0;
06727 i->cidpos = 0;
06728 i->cidlen = 0;
06729 }
06730 } else {
06731 ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
06732 i->msgstate = -1;
06733 }
06734 } else {
06735 ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
06736 }
06737 if (option_debug)
06738 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06739
06740 ast_mutex_unlock(&iflock);
06741 handle_init_event(i, res);
06742 ast_mutex_lock(&iflock);
06743 }
06744 #ifdef ZAPATA_R2
06745 if ((pollres & POLLPRI) || (i->r2 && !i->sigchecked))
06746 #else
06747 if (pollres & POLLPRI)
06748 #endif
06749 {
06750 if (i->owner || i->subs[SUB_REAL].owner) {
06751 #ifdef ZAPATA_PRI
06752 if (!i->pri)
06753 #endif
06754 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].zfd);
06755 i = i->next;
06756 continue;
06757 }
06758 res = zt_get_event(i->subs[SUB_REAL].zfd);
06759 if (option_debug)
06760 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06761
06762 ast_mutex_unlock(&iflock);
06763 handle_init_event(i, res);
06764 ast_mutex_lock(&iflock);
06765 }
06766 }
06767 i=i->next;
06768 }
06769 ast_mutex_unlock(&iflock);
06770 }
06771
06772 return NULL;
06773
06774 }
06775
06776 static int restart_monitor(void)
06777 {
06778 pthread_attr_t attr;
06779 pthread_attr_init(&attr);
06780 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06781
06782 if (monitor_thread == AST_PTHREADT_STOP)
06783 return 0;
06784 if (ast_mutex_lock(&monlock)) {
06785 ast_log(LOG_WARNING, "Unable to lock monitor\n");
06786 return -1;
06787 }
06788 if (monitor_thread == pthread_self()) {
06789 ast_mutex_unlock(&monlock);
06790 ast_log(LOG_WARNING, "Cannot kill myself\n");
06791 return -1;
06792 }
06793 if (monitor_thread != AST_PTHREADT_NULL) {
06794
06795 #if 0
06796 pthread_cancel(monitor_thread);
06797 #endif
06798 pthread_kill(monitor_thread, SIGURG);
06799 #if 0
06800 pthread_join(monitor_thread, NULL);
06801 #endif
06802 } else {
06803
06804 if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
06805 ast_mutex_unlock(&monlock);
06806 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
06807 return -1;
06808 }
06809 }
06810 #if 0
06811 printf("Created thread %ld detached in restart monitor\n", monitor_thread);
06812 #endif
06813 ast_mutex_unlock(&monlock);
06814 return 0;
06815 }
06816
06817 #ifdef ZAPATA_PRI
06818 static int pri_resolve_span(int *span, int channel, int offset, struct zt_spaninfo *si)
06819 {
06820 int x;
06821 int trunkgroup;
06822
06823 trunkgroup = pris[*span].mastertrunkgroup;
06824 if (trunkgroup) {
06825
06826 for (x=0;x<NUM_SPANS;x++) {
06827 if (pris[x].trunkgroup == trunkgroup) {
06828 *span = x;
06829 return 0;
06830 }
06831 }
06832 ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup);
06833 *span = -1;
06834 } else {
06835 if (pris[*span].trunkgroup) {
06836 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup);
06837 *span = -1;
06838 } else if (pris[*span].mastertrunkgroup) {
06839 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup);
06840 *span = -1;
06841 } else {
06842 if (si->totalchans == 31) {
06843 pris[*span].dchannels[0] = 16 + offset;
06844 } else {
06845 pris[*span].dchannels[0] = 24 + offset;
06846 }
06847 pris[*span].dchanavail[0] |= DCHAN_PROVISIONED;
06848 pris[*span].offset = offset;
06849 pris[*span].span = *span + 1;
06850 }
06851 }
06852 return 0;
06853 }
06854
06855 static int pri_create_trunkgroup(int trunkgroup, int *channels)
06856 {
06857 struct zt_spaninfo si;
06858 ZT_PARAMS p;
06859 int fd;
06860 int span;
06861 int ospan=0;
06862 int x,y;
06863 for (x=0;x<NUM_SPANS;x++) {
06864 if (pris[x].trunkgroup == trunkgroup) {
06865 ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]);
06866 return -1;
06867 }
06868 }
06869 for (y=0;y<NUM_DCHANS;y++) {
06870 if (!channels[y])
06871 break;
06872 memset(&si, 0, sizeof(si));
06873 memset(&p, 0, sizeof(p));
06874 fd = open("/dev/zap/channel", O_RDWR);
06875 if (fd < 0) {
06876 ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno));
06877 return -1;
06878 }
06879 x = channels[y];
06880 if (ioctl(fd, ZT_SPECIFY, &x)) {
06881 ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
06882 zt_close(fd);
06883 return -1;
06884 }
06885 if (ioctl(fd, ZT_GET_PARAMS, &p)) {
06886 ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno));
06887 return -1;
06888 }
06889 if (ioctl(fd, ZT_SPANSTAT, &si)) {
06890 ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d)\n", channels[y], p.spanno);
06891 zt_close(fd);
06892 return -1;
06893 }
06894 span = p.spanno - 1;
06895 if (pris[span].trunkgroup) {
06896 ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
06897 zt_close(fd);
06898 return -1;
06899 }
06900 if (pris[span].pvts[0]) {
06901 ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
06902 zt_close(fd);
06903 return -1;
06904 }
06905 if (!y) {
06906 pris[span].trunkgroup = trunkgroup;
06907 pris[span].offset = channels[y] - p.chanpos;
06908 ospan = span;
06909 }
06910 pris[ospan].dchannels[y] = channels[y];
06911 pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
06912 pris[span].span = span + 1;
06913 zt_close(fd);
06914 }
06915 return 0;
06916 }
06917
06918 static int pri_create_spanmap(int span, int trunkgroup, int logicalspan)
06919 {
06920 if (pris[span].mastertrunkgroup) {
06921 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);
06922 return -1;
06923 }
06924 pris[span].mastertrunkgroup = trunkgroup;
06925 pris[span].prilogicalspan = logicalspan;
06926 return 0;
06927 }
06928
06929 #endif
06930
06931 static struct zt_pvt *mkintf(int channel, int signalling, int radio, struct zt_pri *pri, int reloading)
06932 {
06933
06934 struct zt_pvt *tmp = NULL, *tmp2, *prev = NULL;
06935 char fn[80];
06936 #if 1
06937 struct zt_bufferinfo bi;
06938 #endif
06939 struct zt_spaninfo si;
06940 int res;
06941 int span=0;
06942 int here = 0;
06943 int x;
06944 struct zt_pvt **wlist;
06945 struct zt_pvt **wend;
06946 ZT_PARAMS p;
06947
06948 wlist = &iflist;
06949 wend = &ifend;
06950
06951 #ifdef ZAPATA_PRI
06952 if (pri) {
06953 wlist = &pri->crvs;
06954 wend = &pri->crvend;
06955 }
06956 #endif
06957
06958 tmp2 = *wlist;
06959 prev = NULL;
06960
06961 while (tmp2) {
06962 if (!tmp2->destroy) {
06963 if (tmp2->channel == channel) {
06964 tmp = tmp2;
06965 here = 1;
06966 break;
06967 }
06968 if (tmp2->channel > channel) {
06969 break;
06970 }
06971 }
06972 prev = tmp2;
06973 tmp2 = tmp2->next;
06974 }
06975
06976 if (!here && !reloading) {
06977 tmp = (struct zt_pvt*)malloc(sizeof(struct zt_pvt));
06978 if (!tmp) {
06979 ast_log(LOG_ERROR, "MALLOC FAILED\n");
06980 destroy_zt_pvt(&tmp);
06981 return NULL;
06982 }
06983 memset(tmp, 0, sizeof(struct zt_pvt));
06984 ast_mutex_init(&tmp->lock);
06985 ifcount++;
06986 for (x=0;x<3;x++)
06987 tmp->subs[x].zfd = -1;
06988 tmp->channel = channel;
06989 }
06990
06991 if (tmp) {
06992 if (!here) {
06993 if ((channel != CHAN_PSEUDO) && !pri) {
06994 snprintf(fn, sizeof(fn), "%d", channel);
06995
06996 if (!here)
06997 tmp->subs[SUB_REAL].zfd = zt_open(fn);
06998
06999 if (tmp->subs[SUB_REAL].zfd < 0) {
07000 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);
07001 destroy_zt_pvt(&tmp);
07002 return NULL;
07003 }
07004 memset(&p, 0, sizeof(p));
07005 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
07006 if (res < 0) {
07007 ast_log(LOG_ERROR, "Unable to get parameters\n");
07008 destroy_zt_pvt(&tmp);
07009 return NULL;
07010 }
07011 if (p.sigtype != (signalling & 0x3ffff)) {
07012 ast_log(LOG_ERROR, "Signalling requested on channel %d is %s but line is in %s signalling\n", channel, sig2str(signalling), sig2str(p.sigtype));
07013 destroy_zt_pvt(&tmp);
07014 return tmp;
07015 }
07016 tmp->law = p.curlaw;
07017 tmp->span = p.spanno;
07018 span = p.spanno - 1;
07019 } else {
07020 if (channel == CHAN_PSEUDO)
07021 signalling = 0;
07022 else if ((signalling != SIG_FXOKS) && (signalling != SIG_FXSKS)) {
07023 ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n");
07024 return NULL;
07025 }
07026 }
07027 #ifdef ZAPATA_PRI
07028 if ((signalling == SIG_PRI) || (signalling == SIG_GR303FXOKS) || (signalling == SIG_GR303FXSKS)) {
07029 int offset;
07030 int myswitchtype;
07031 int matchesdchan;
07032 int x,y;
07033 offset = 0;
07034 if ((signalling == SIG_PRI) && ioctl(tmp->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &offset)) {
07035 ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
07036 destroy_zt_pvt(&tmp);
07037 return NULL;
07038 }
07039 if (span >= NUM_SPANS) {
07040 ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span);
07041 destroy_zt_pvt(&tmp);
07042 return NULL;
07043 } else {
07044 si.spanno = 0;
07045 if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
07046 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
07047 destroy_zt_pvt(&tmp);
07048 return NULL;
07049 }
07050
07051 tmp->logicalspan = pris[span].prilogicalspan;
07052 pri_resolve_span(&span, channel, (channel - p.chanpos), &si);
07053 if (span < 0) {
07054 ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel);
07055 destroy_zt_pvt(&tmp);
07056 return NULL;
07057 }
07058 if (signalling == SIG_PRI)
07059 myswitchtype = switchtype;
07060 else
07061 myswitchtype = PRI_SWITCH_GR303_TMC;
07062
07063 matchesdchan=0;
07064 for (x=0;x<NUM_SPANS;x++) {
07065 for (y=0;y<NUM_DCHANS;y++) {
07066 if (pris[x].dchannels[y] == tmp->channel) {
07067 matchesdchan = 1;
07068 break;
07069 }
07070 }
07071 }
07072 offset = p.chanpos;
07073 if (!matchesdchan) {
07074 if (pris[span].nodetype && (pris[span].nodetype != pritype)) {
07075 ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype));
07076 destroy_zt_pvt(&tmp);
07077 return NULL;
07078 }
07079 if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) {
07080 ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype));
07081 destroy_zt_pvt(&tmp);
07082 return NULL;
07083 }
07084 if ((pris[span].dialplan) && (pris[span].dialplan != dialplan)) {
07085 ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan));
07086 destroy_zt_pvt(&tmp);
07087 return NULL;
07088 }
07089 if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, idledial)) {
07090 ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, idledial);
07091 destroy_zt_pvt(&tmp);
07092 return NULL;
07093 }
07094 if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, idleext)) {
07095 ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, idleext);
07096 destroy_zt_pvt(&tmp);
07097 return NULL;
07098 }
07099 if (pris[span].minunused && (pris[span].minunused != minunused)) {
07100 ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, minunused);
07101 destroy_zt_pvt(&tmp);
07102 return NULL;
07103 }
07104 if (pris[span].minidle && (pris[span].minidle != minidle)) {
07105 ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, minidle);
07106 destroy_zt_pvt(&tmp);
07107 return NULL;
07108 }
07109 if (pris[span].numchans >= MAX_CHANNELS) {
07110 ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel,
07111 pris[span].trunkgroup);
07112 destroy_zt_pvt(&tmp);
07113 return NULL;
07114 }
07115 pris[span].nodetype = pritype;
07116 pris[span].switchtype = myswitchtype;
07117 pris[span].nsf = nsf;
07118 pris[span].dialplan = dialplan;
07119 pris[span].localdialplan = localdialplan;
07120 pris[span].pvts[pris[span].numchans++] = tmp;
07121 pris[span].minunused = minunused;
07122 pris[span].minidle = minidle;
07123 pris[span].overlapdial = overlapdial;
07124 pris[span].facilityenable = facilityenable;
07125 ast_copy_string(pris[span].idledial, idledial, sizeof(pris[span].idledial));
07126 ast_copy_string(pris[span].idleext, idleext, sizeof(pris[span].idleext));
07127 ast_copy_string(pris[span].internationalprefix, internationalprefix, sizeof(pris[span].internationalprefix));
07128 ast_copy_string(pris[span].nationalprefix, nationalprefix, sizeof(pris[span].nationalprefix));
07129 ast_copy_string(pris[span].localprefix, localprefix, sizeof(pris[span].localprefix));
07130 ast_copy_string(pris[span].privateprefix, privateprefix, sizeof(pris[span].privateprefix));
07131 ast_copy_string(pris[span].unknownprefix, unknownprefix, sizeof(pris[span].unknownprefix));
07132 pris[span].resetinterval = resetinterval;
07133
07134 tmp->pri = &pris[span];
07135 tmp->prioffset = offset;
07136 tmp->call = NULL;
07137 } else {
07138 ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset);
07139 destroy_zt_pvt(&tmp);
07140 return NULL;
07141 }
07142 }
07143 } else {
07144 tmp->prioffset = 0;
07145 }
07146 #endif
07147 #ifdef ZAPATA_R2
07148 if (signalling == SIG_R2) {
07149 if (r2prot < 0) {
07150 ast_log(LOG_WARNING, "R2 Country not specified for channel %d -- Assuming China\n", tmp->channel);
07151 tmp->r2prot = MFCR2_PROT_CHINA;
07152 } else
07153 tmp->r2prot = r2prot;
07154 tmp->r2 = mfcr2_new(tmp->subs[SUB_REAL].zfd, tmp->r2prot, 1);
07155 if (!tmp->r2) {
07156 ast_log(LOG_WARNING, "Unable to create r2 call :(\n");
07157 zt_close(tmp->subs[SUB_REAL].zfd);
07158 destroy_zt_pvt(&tmp);
07159 return NULL;
07160 }
07161 } else {
07162 if (tmp->r2)
07163 mfcr2_free(tmp->r2);
07164 tmp->r2 = NULL;
07165 }
07166 #endif
07167 } else {
07168 signalling = tmp->sig;
07169 radio = tmp->radio;
07170 memset(&p, 0, sizeof(p));
07171 if (tmp->subs[SUB_REAL].zfd > -1)
07172 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
07173 }
07174
07175 if ((signalling == SIG_FXSKS) || (signalling == SIG_FXSLS) ||
07176 (signalling == SIG_EM) || (signalling == SIG_EM_E1) || (signalling == SIG_EMWINK) ||
07177 (signalling == SIG_FEATD) || (signalling == SIG_FEATDMF) || (signalling == SIG_FEATDMF_TA) ||
07178 (signalling == SIG_FEATB) || (signalling == SIG_E911) ||
07179 (signalling == SIG_SF) || (signalling == SIG_SFWINK) ||
07180 (signalling == SIG_SF_FEATD) || (signalling == SIG_SF_FEATDMF) ||
07181 (signalling == SIG_SF_FEATB)) {
07182 p.starttime = 250;
07183 }
07184 if (radio) {
07185
07186 p.channo = channel;
07187 p.rxwinktime = 1;
07188 p.rxflashtime = 1;
07189 p.starttime = 1;
07190 p.debouncetime = 5;
07191 }
07192 if (!radio) {
07193 p.channo = channel;
07194
07195 if (cur_prewink >= 0)
07196 p.prewinktime = cur_prewink;
07197 if (cur_preflash >= 0)
07198 p.preflashtime = cur_preflash;
07199 if (cur_wink >= 0)
07200 p.winktime = cur_wink;
07201 if (cur_flash >= 0)
07202 p.flashtime = cur_flash;
07203 if (cur_start >= 0)
07204 p.starttime = cur_start;
07205 if (cur_rxwink >= 0)
07206 p.rxwinktime = cur_rxwink;
07207 if (cur_rxflash >= 0)
07208 p.rxflashtime = cur_rxflash;
07209 if (cur_debounce >= 0)
07210 p.debouncetime = cur_debounce;
07211 }
07212
07213
07214 if (tmp->subs[SUB_REAL].zfd >= 0)
07215 {
07216 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_PARAMS, &p);
07217 if (res < 0) {
07218 ast_log(LOG_ERROR, "Unable to set parameters\n");
07219 destroy_zt_pvt(&tmp);
07220 return NULL;
07221 }
07222 }
07223 #if 1
07224 if (!here && (tmp->subs[SUB_REAL].zfd > -1)) {
07225 memset(&bi, 0, sizeof(bi));
07226 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
07227 if (!res) {
07228 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
07229 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
07230 bi.numbufs = numbufs;
07231 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
07232 if (res < 0) {
07233 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", channel);
07234 }
07235 } else
07236 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", channel);
07237 }
07238 #endif
07239 tmp->immediate = immediate;
07240 tmp->transfertobusy = transfertobusy;
07241 tmp->sig = signalling;
07242 tmp->radio = radio;
07243 tmp->ringt_base = ringt_base;
07244 tmp->firstradio = 0;
07245 if ((signalling == SIG_FXOKS) || (signalling == SIG_FXOLS) || (signalling == SIG_FXOGS))
07246 tmp->permcallwaiting = callwaiting;
07247 else
07248 tmp->permcallwaiting = 0;
07249
07250 tmp->destroy = 0;
07251 tmp->drings = drings;
07252 tmp->usedistinctiveringdetection = usedistinctiveringdetection;
07253 tmp->callwaitingcallerid = callwaitingcallerid;
07254 tmp->threewaycalling = threewaycalling;
07255 tmp->adsi = adsi;
07256 tmp->permhidecallerid = hidecallerid;
07257 tmp->callreturn = callreturn;
07258 tmp->echocancel = echocancel;
07259 tmp->echotraining = echotraining;
07260 tmp->pulse = pulse;
07261 tmp->echocanbridged = echocanbridged;
07262 tmp->busydetect = busydetect;
07263 tmp->busycount = busycount;
07264 tmp->busy_tonelength = busy_tonelength;
07265 tmp->busy_quietlength = busy_quietlength;
07266 tmp->callprogress = callprogress;
07267 tmp->cancallforward = cancallforward;
07268 tmp->dtmfrelax = relaxdtmf;
07269 tmp->callwaiting = tmp->permcallwaiting;
07270 tmp->hidecallerid = tmp->permhidecallerid;
07271 tmp->channel = channel;
07272 tmp->stripmsd = stripmsd;
07273 tmp->use_callerid = use_callerid;
07274 tmp->cid_signalling = cid_signalling;
07275 tmp->cid_start = cid_start;
07276 tmp->zaptrcallerid = zaptrcallerid;
07277 tmp->restrictcid = restrictcid;
07278 tmp->use_callingpres = use_callingpres;
07279 tmp->priindication_oob = priindication_oob;
07280 tmp->priexclusive = cur_priexclusive;
07281 if (tmp->usedistinctiveringdetection) {
07282 if (!tmp->use_callerid) {
07283 ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n");
07284 tmp->use_callerid = 1;
07285 }
07286 }
07287
07288 ast_copy_string(tmp->accountcode, accountcode, sizeof(tmp->accountcode));
07289 tmp->amaflags = amaflags;
07290 if (!here) {
07291 tmp->confno = -1;
07292 tmp->propconfno = -1;
07293 }
07294 tmp->canpark = canpark;
07295 tmp->transfer = transfer;
07296 ast_copy_string(tmp->defcontext,context,sizeof(tmp->defcontext));
07297 ast_copy_string(tmp->language, language, sizeof(tmp->language));
07298 ast_copy_string(tmp->musicclass, musicclass, sizeof(tmp->musicclass));
07299 ast_copy_string(tmp->context, context, sizeof(tmp->context));
07300 ast_copy_string(tmp->cid_num, cid_num, sizeof(tmp->cid_num));
07301 tmp->cid_ton = 0;
07302 ast_copy_string(tmp->cid_name, cid_name, sizeof(tmp->cid_name));
07303 ast_copy_string(tmp->mailbox, mailbox, sizeof(tmp->mailbox));
07304 tmp->msgstate = -1;
07305 tmp->group = cur_group;
07306 tmp->callgroup=cur_callergroup;
07307 tmp->pickupgroup=cur_pickupgroup;
07308 tmp->rxgain = rxgain;
07309 tmp->txgain = txgain;
07310 tmp->tonezone = tonezone;
07311 tmp->onhooktime = time(NULL);
07312 if (tmp->subs[SUB_REAL].zfd > -1) {
07313 set_actual_gain(tmp->subs[SUB_REAL].zfd, 0, tmp->rxgain, tmp->txgain, tmp->law);
07314 if (tmp->dsp)
07315 ast_dsp_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax);
07316 update_conf(tmp);
07317 if (!here) {
07318 if ((signalling != SIG_PRI) && (signalling != SIG_R2))
07319
07320 zt_set_hook(tmp->subs[SUB_REAL].zfd, ZT_ONHOOK);
07321 }
07322 ioctl(tmp->subs[SUB_REAL].zfd,ZT_SETTONEZONE,&tmp->tonezone);
07323 #ifdef ZAPATA_PRI
07324
07325 if (tmp->pri && !pri_is_up(tmp->pri))
07326 tmp->inalarm = 1;
07327 else
07328 tmp->inalarm = 0;
07329 #endif
07330 memset(&si, 0, sizeof(si));
07331 if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
07332 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
07333 destroy_zt_pvt(&tmp);
07334 return NULL;
07335 }
07336 if (si.alarms) tmp->inalarm = 1;
07337 }
07338
07339 tmp->polarityonanswerdelay = polarityonanswerdelay;
07340 tmp->answeronpolarityswitch = answeronpolarityswitch;
07341 tmp->hanguponpolarityswitch = hanguponpolarityswitch;
07342 tmp->sendcalleridafter = sendcalleridafter;
07343
07344 }
07345 if (tmp && !here) {
07346
07347 if (!*wlist) {
07348 *wlist = tmp;
07349 tmp->prev = NULL;
07350 tmp->next = NULL;
07351 *wend = tmp;
07352 } else {
07353
07354 struct zt_pvt *working = *wlist;
07355
07356
07357 if (working->channel > tmp->channel) {
07358 tmp->next = *wlist;
07359 tmp->prev = NULL;
07360 (*wlist)->prev = tmp;
07361 *wlist = tmp;
07362 } else {
07363
07364 while (working) {
07365
07366 if (working->next) {
07367 if (working->channel < tmp->channel && working->next->channel > tmp->channel) {
07368 tmp->next = working->next;
07369 tmp->prev = working;
07370 working->next->prev = tmp;
07371 working->next = tmp;
07372 break;
07373 }
07374 } else {
07375
07376 if (working->channel < tmp->channel) {
07377 working->next = tmp;
07378 tmp->next = NULL;
07379 tmp->prev = working;
07380 *wend = tmp;
07381 break;
07382 }
07383 }
07384 working = working->next;
07385 }
07386 }
07387 }
07388 }
07389 return tmp;
07390 }
07391
07392 static inline int available(struct zt_pvt *p, int channelmatch, int groupmatch, int *busy, int *channelmatched, int *groupmatched)
07393 {
07394 int res;
07395 ZT_PARAMS par;
07396
07397
07398 if (groupmatch) {
07399 if ((p->group & groupmatch) != groupmatch)
07400 return 0;
07401 *groupmatched = 1;
07402 }
07403
07404 if (channelmatch != -1) {
07405 if (p->channel != channelmatch)
07406 return 0;
07407 *channelmatched = 1;
07408 }
07409
07410 if (busy) {
07411 if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS))
07412 *busy = 1;
07413 }
07414
07415 if (p->dnd)
07416 return 0;
07417
07418 if (p->guardtime && (time(NULL) < p->guardtime))
07419 return 0;
07420
07421
07422 if (!p->owner) {
07423 #ifdef ZAPATA_PRI
07424
07425 if (p->pri) {
07426 if (p->resetting || p->call)
07427 return 0;
07428 else
07429 return 1;
07430 }
07431 #endif
07432 #ifdef ZAPATA_R2
07433
07434 if (p->r2) {
07435 if (p->hasr2call || p->r2blocked)
07436 return 0;
07437 else
07438 return 1;
07439 }
07440 #endif
07441 if (!p->radio)
07442 {
07443 if (!p->sig || (p->sig == SIG_FXSLS))
07444 return 1;
07445
07446 if (p->subs[SUB_REAL].zfd > -1)
07447 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
07448 else {
07449
07450 res = 0;
07451 par.rxisoffhook = 0;
07452 }
07453 if (res) {
07454 ast_log(LOG_WARNING, "Unable to check hook state on channel %d\n", p->channel);
07455 } else if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
07456
07457
07458
07459 if (par.rxbits > -1)
07460 return 1;
07461 if (par.rxisoffhook)
07462 return 1;
07463 else
07464 #ifdef ZAP_CHECK_HOOKSTATE
07465 return 0;
07466 #else
07467 return 1;
07468 #endif
07469 } else if (par.rxisoffhook) {
07470 ast_log(LOG_DEBUG, "Channel %d off hook, can't use\n", p->channel);
07471
07472 return 0;
07473 }
07474 }
07475 return 1;
07476 }
07477
07478
07479 if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS))
07480 return 0;
07481
07482 if (!p->callwaiting) {
07483
07484 return 0;
07485 }
07486
07487 if (p->subs[SUB_CALLWAIT].zfd > -1) {
07488
07489 return 0;
07490 }
07491
07492 if ((p->owner->_state != AST_STATE_UP) &&
07493 ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
07494
07495 return 0;
07496 }
07497 if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) {
07498
07499 return 0;
07500 }
07501
07502 return 1;
07503 }
07504
07505 static struct zt_pvt *chandup(struct zt_pvt *src)
07506 {
07507 struct zt_pvt *p;
07508 ZT_BUFFERINFO bi;
07509 int res;
07510 p = malloc(sizeof(struct zt_pvt));
07511 if (p) {
07512 memcpy(p, src, sizeof(struct zt_pvt));
07513 ast_mutex_init(&p->lock);
07514 p->subs[SUB_REAL].zfd = zt_open("/dev/zap/pseudo");
07515
07516 if (p->subs[SUB_REAL].zfd < 0) {
07517 ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno));
07518 destroy_zt_pvt(&p);
07519 return NULL;
07520 }
07521 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
07522 if (!res) {
07523 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
07524 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
07525 bi.numbufs = numbufs;
07526 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
07527 if (res < 0) {
07528 ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel\n");
07529 }
07530 } else
07531 ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel\n");
07532 }
07533 p->destroy = 1;
07534 p->next = iflist;
07535 iflist = p;
07536 return p;
07537 }
07538
07539
07540 #ifdef ZAPATA_PRI
07541 static int pri_find_empty_chan(struct zt_pri *pri, int backwards)
07542 {
07543 int x;
07544 if (backwards)
07545 x = pri->numchans;
07546 else
07547 x = 0;
07548 for (;;) {
07549 if (backwards && (x < 0))
07550 break;
07551 if (!backwards && (x >= pri->numchans))
07552 break;
07553 if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
07554 ast_log(LOG_DEBUG, "Found empty available channel %d/%d\n",
07555 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
07556 return x;
07557 }
07558 if (backwards)
07559 x--;
07560 else
07561 x++;
07562 }
07563 return -1;
07564 }
07565 #endif
07566
07567 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause)
07568 {
07569 int oldformat;
07570 int groupmatch = 0;
07571 int channelmatch = -1;
07572 int roundrobin = 0;
07573 int callwait = 0;
07574 int busy = 0;
07575 struct zt_pvt *p;
07576 struct ast_channel *tmp = NULL;
07577 char *dest=NULL;
07578 int x;
07579 char *s;
07580 char opt=0;
07581 int res=0, y=0;
07582 int backwards = 0;
07583 #ifdef ZAPATA_PRI
07584 int crv;
07585 int bearer = -1;
07586 int trunkgroup;
07587 struct zt_pri *pri=NULL;
07588 #endif
07589 struct zt_pvt *exit, *start, *end;
07590 ast_mutex_t *lock;
07591 int channelmatched = 0;
07592 int groupmatched = 0;
07593
07594
07595 lock = &iflock;
07596 start = iflist;
07597 end = ifend;
07598
07599 oldformat = format;
07600 format &= (AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
07601 if (!format) {
07602 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
07603 return NULL;
07604 }
07605 if (data) {
07606 dest = ast_strdupa((char *)data);
07607 } else {
07608 ast_log(LOG_WARNING, "Channel requested with no data\n");
07609 return NULL;
07610 }
07611 if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') {
07612
07613 char *stringp=NULL;
07614 stringp=dest + 1;
07615 s = strsep(&stringp, "/");
07616 if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07617 ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data);
07618 return NULL;
07619 }
07620 groupmatch = 1 << x;
07621 if (toupper(dest[0]) == 'G') {
07622 if (dest[0] == 'G') {
07623 backwards = 1;
07624 p = ifend;
07625 } else
07626 p = iflist;
07627 } else {
07628 if (dest[0] == 'R') {
07629 backwards = 1;
07630 p = round_robin[x]?round_robin[x]->prev:ifend;
07631 if (!p)
07632 p = ifend;
07633 } else {
07634 p = round_robin[x]?round_robin[x]->next:iflist;
07635 if (!p)
07636 p = iflist;
07637 }
07638 roundrobin = 1;
07639 }
07640 } else {
07641 char *stringp=NULL;
07642 stringp=dest;
07643 s = strsep(&stringp, "/");
07644 p = iflist;
07645 if (!strcasecmp(s, "pseudo")) {
07646
07647 x = CHAN_PSEUDO;
07648 channelmatch = x;
07649 }
07650 #ifdef ZAPATA_PRI
07651 else if ((res = sscanf(s, "%d:%d%c%d", &trunkgroup, &crv, &opt, &y)) > 1) {
07652 if ((trunkgroup < 1) || (crv < 1)) {
07653 ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data);
07654 return NULL;
07655 }
07656 res--;
07657 for (x=0;x<NUM_SPANS;x++) {
07658 if (pris[x].trunkgroup == trunkgroup) {
07659 pri = pris + x;
07660 lock = &pri->lock;
07661 start = pri->crvs;
07662 end = pri->crvend;
07663 break;
07664 }
07665 }
07666 if (!pri) {
07667 ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup);
07668 return NULL;
07669 }
07670 channelmatch = crv;
07671 p = pris[x].crvs;
07672 }
07673 #endif
07674 else if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07675 ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data);
07676 return NULL;
07677 } else {
07678 channelmatch = x;
07679 }
07680 }
07681
07682 if (ast_mutex_lock(lock)) {
07683 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
07684 return NULL;
07685 }
07686 exit = p;
07687 while(p && !tmp) {
07688 if (roundrobin)
07689 round_robin[x] = p;
07690 #if 0
07691 ast_verbose("name = %s, %d, %d, %d\n",p->owner ? p->owner->name : "<none>", p->channel, channelmatch, groupmatch);
07692 #endif
07693
07694 if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) {
07695 if (option_debug)
07696 ast_log(LOG_DEBUG, "Using channel %d\n", p->channel);
07697 if (p->inalarm)
07698 goto next;
07699
07700 callwait = (p->owner != NULL);
07701 #ifdef ZAPATA_PRI
07702 if (pri && (p->subs[SUB_REAL].zfd < 0)) {
07703 if (p->sig != SIG_FXSKS) {
07704
07705
07706 bearer = pri_find_empty_chan(pri, 0);
07707 if (bearer < 0) {
07708 ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv);
07709 p = NULL;
07710 break;
07711 }
07712 pri_assign_bearer(p, pri, pri->pvts[bearer]);
07713 } else {
07714 if (alloc_sub(p, 0)) {
07715 ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n");
07716 p = NULL;
07717 break;
07718 } else
07719 ast_log(LOG_DEBUG, "Allocated placeholder pseudo channel\n");
07720 p->pri = pri;
07721 }
07722 }
07723 #endif
07724 if (p->channel == CHAN_PSEUDO) {
07725 p = chandup(p);
07726 if (!p) {
07727 break;
07728 }
07729 }
07730 if (p->owner) {
07731 if (alloc_sub(p, SUB_CALLWAIT)) {
07732 p = NULL;
07733 break;
07734 }
07735 }
07736 p->outgoing = 1;
07737 tmp = zt_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0);
07738 #ifdef ZAPATA_PRI
07739 if (p->bearer) {
07740
07741 p->bearer->owner = tmp;
07742 }
07743 #endif
07744
07745 if (res > 1) {
07746 if (opt == 'c') {
07747
07748 p->confirmanswer = 1;
07749 } else if (opt == 'r') {
07750
07751 if (res < 3)
07752 ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data);
07753 else
07754 p->distinctivering = y;
07755 } else if (opt == 'd') {
07756
07757 p->digital = 1;
07758 if (tmp)
07759 tmp->transfercapability = AST_TRANS_CAP_DIGITAL;
07760 } else {
07761 ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data);
07762 }
07763 }
07764
07765 if (tmp && callwait)
07766 tmp->cdrflags |= AST_CDR_CALLWAIT;
07767 break;
07768 }
07769 next:
07770 if (backwards) {
07771 p = p->prev;
07772 if (!p)
07773 p = end;
07774 } else {
07775 p = p->next;
07776 if (!p)
07777 p = start;
07778 }
07779
07780 if (p == exit)
07781 break;
07782 }
07783 ast_mutex_unlock(lock);
07784 restart_monitor();
07785 if (callwait)
07786 *cause = AST_CAUSE_BUSY;
07787 else if (!tmp) {
07788 if (channelmatched) {
07789 if (busy)
07790 *cause = AST_CAUSE_BUSY;
07791 } else if (groupmatched) {
07792 *cause = AST_CAUSE_CONGESTION;
07793 }
07794 }
07795
07796 return tmp;
07797 }
07798
07799
07800 #ifdef ZAPATA_PRI
07801 static struct zt_pvt *pri_find_crv(struct zt_pri *pri, int crv)
07802 {
07803 struct zt_pvt *p;
07804 p = pri->crvs;
07805 while(p) {
07806 if (p->channel == crv)
07807 return p;
07808 p = p->next;
07809 }
07810 return NULL;
07811 }
07812
07813
07814 static int pri_find_principle(struct zt_pri *pri, int channel)
07815 {
07816 int x;
07817 int span = PRI_SPAN(channel);
07818 int spanfd;
07819 ZT_PARAMS param;
07820 int principle = -1;
07821 int explicit = PRI_EXPLICIT(channel);
07822 span = PRI_SPAN(channel);
07823 channel = PRI_CHANNEL(channel);
07824
07825 if (!explicit) {
07826 spanfd = pri_active_dchan_fd(pri);
07827 if (ioctl(spanfd, ZT_GET_PARAMS, ¶m))
07828 return -1;
07829 span = pris[param.spanno - 1].prilogicalspan;
07830 }
07831
07832 for (x=0;x<pri->numchans;x++) {
07833 if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
07834 principle = x;
07835 break;
07836 }
07837 }
07838
07839 return principle;
07840 }
07841
07842 static int pri_fixup_principle(struct zt_pri *pri, int principle, q931_call *c)
07843 {
07844 int x;
07845 struct zt_pvt *crv;
07846 if (!c) {
07847 if (principle < 0)
07848 return -1;
07849 return principle;
07850 }
07851 if ((principle > -1) &&
07852 (principle < pri->numchans) &&
07853 (pri->pvts[principle]) &&
07854 (pri->pvts[principle]->call == c))
07855 return principle;
07856
07857 for (x=0;x<pri->numchans;x++) {
07858 if (!pri->pvts[x]) continue;
07859 if (pri->pvts[x]->call == c) {
07860
07861 if (principle != x) {
07862 if (option_verbose > 2)
07863 ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n",
07864 pri->pvts[x]->channel, pri->pvts[principle]->channel);
07865 if (pri->pvts[principle]->owner) {
07866 ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
07867 pri->pvts[x]->channel, pri->pvts[principle]->channel, pri->pvts[principle]->channel);
07868 return -1;
07869 }
07870
07871 pri->pvts[principle]->owner = pri->pvts[x]->owner;
07872 if (pri->pvts[principle]->owner) {
07873 snprintf(pri->pvts[principle]->owner->name, sizeof(pri->pvts[principle]->owner->name),
07874 "Zap/%d:%d-%d", pri->trunkgroup, pri->pvts[principle]->channel, 1);
07875 pri->pvts[principle]->owner->tech_pvt = pri->pvts[principle];
07876 pri->pvts[principle]->owner->fds[0] = pri->pvts[principle]->subs[SUB_REAL].zfd;
07877 pri->pvts[principle]->subs[SUB_REAL].owner = pri->pvts[x]->subs[SUB_REAL].owner;
07878 } else
07879 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);
07880 pri->pvts[principle]->call = pri->pvts[x]->call;
07881
07882 pri->pvts[x]->subs[SUB_REAL].owner = NULL;
07883 pri->pvts[x]->owner = NULL;
07884 pri->pvts[x]->call = NULL;
07885 }
07886 return principle;
07887 }
07888 }
07889
07890 crv = pri->crvs;
07891 while(crv) {
07892 if (crv->call == c) {
07893
07894 if (crv->bearer)
07895 ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n");
07896 else if (pri->pvts[principle]->owner)
07897 ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n");
07898 else {
07899
07900
07901 zt_close(crv->subs[SUB_REAL].zfd);
07902 pri->pvts[principle]->call = crv->call;
07903 pri_assign_bearer(crv, pri, pri->pvts[principle]);
07904 ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n",
07905 pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset,
07906 pri->trunkgroup, crv->channel);
07907 wakeup_sub(crv, SUB_REAL, pri);
07908 }
07909 return principle;
07910 }
07911 crv = crv->next;
07912 }
07913 ast_log(LOG_WARNING, "Call specified, but not found?\n");
07914 return -1;
07915 }
07916
07917 static void *do_idle_thread(void *vchan)
07918 {
07919 struct ast_channel *chan = vchan;
07920 struct zt_pvt *pvt = chan->tech_pvt;
07921 struct ast_frame *f;
07922 char ex[80];
07923
07924 int newms, ms = 30000;
07925 if (option_verbose > 2)
07926 ast_verbose(VERBOSE_PREFIX_3 "Initiating idle call on channel %s\n", chan->name);
07927 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
07928 if (ast_call(chan, ex, 0)) {
07929 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
07930 ast_hangup(chan);
07931 return NULL;
07932 }
07933 while((newms = ast_waitfor(chan, ms)) > 0) {
07934 f = ast_read(chan);
07935 if (!f) {
07936
07937 break;
07938 }
07939 if (f->frametype == AST_FRAME_CONTROL) {
07940 switch(f->subclass) {
07941 case AST_CONTROL_ANSWER:
07942
07943 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
07944 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
07945 chan->priority = 1;
07946 if (option_verbose > 3)
07947 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
07948 ast_pbx_run(chan);
07949
07950 return NULL;
07951 case AST_CONTROL_BUSY:
07952 if (option_verbose > 3)
07953 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' busy, waiting...\n", chan->name);
07954 break;
07955 case AST_CONTROL_CONGESTION:
07956 if (option_verbose > 3)
07957 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' congested, waiting...\n", chan->name);
07958 break;
07959 };
07960 }
07961 ast_frfree(f);
07962 ms = newms;
07963 }
07964 #if 0
07965 printf("Hanging up '%s'\n", chan->name);
07966 #endif
07967
07968 ast_hangup(chan);
07969 return NULL;
07970 }
07971
07972 #ifndef PRI_RESTART
07973 #error "Upgrade your libpri"
07974 #endif
07975 static void zt_pri_message(struct pri *pri, char *s)
07976 {
07977 int x, y;
07978 int dchan = -1, span = -1;
07979 int dchancount = 0;
07980
07981 if (pri) {
07982 for (x = 0; x < NUM_SPANS; x++) {
07983 for (y = 0; y < NUM_DCHANS; y++) {
07984 if (pris[x].dchans[y])
07985 dchancount++;
07986
07987 if (pris[x].dchans[y] == pri)
07988 dchan = y;
07989 }
07990 if (dchan >= 0) {
07991 span = x;
07992 break;
07993 }
07994 dchancount = 0;
07995 }
07996 if ((dchan >= 0) && (span >= 0)) {
07997 if (dchancount > 1)
07998 ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s);
07999 else
08000 ast_verbose("%s", s);
08001 } else
08002 ast_verbose("PRI debug error: could not find pri associated it with debug message output\n");
08003 } else
08004 ast_verbose("%s", s);
08005
08006 ast_mutex_lock(&pridebugfdlock);
08007
08008 if (pridebugfd >= 0)
08009 write(pridebugfd, s, strlen(s));
08010
08011 ast_mutex_unlock(&pridebugfdlock);
08012 }
08013
08014 static void zt_pri_error(struct pri *pri, char *s)
08015 {
08016 int x, y;
08017 int dchan = -1, span = -1;
08018 int dchancount = 0;
08019
08020 if (pri) {
08021 for (x = 0; x < NUM_SPANS; x++) {
08022 for (y = 0; y < NUM_DCHANS; y++) {
08023 if (pris[x].dchans[y])
08024 dchancount++;
08025
08026 if (pris[x].dchans[y] == pri)
08027 dchan = y;
08028 }
08029 if (dchan >= 0) {
08030 span = x;
08031 break;
08032 }
08033 dchancount = 0;
08034 }
08035 if ((dchan >= 0) && (span >= 0)) {
08036 if (dchancount > 1)
08037 ast_log(LOG_WARNING, "[Span %d D-Channel %d] PRI: %s", span, dchan, s);
08038 else
08039 ast_verbose("%s", s);
08040 } else
08041 ast_verbose("PRI debug error: could not find pri associated it with debug message output\n");
08042 } else
08043 ast_log(LOG_WARNING, "%s", s);
08044
08045 ast_mutex_lock(&pridebugfdlock);
08046
08047 if (pridebugfd >= 0)
08048 write(pridebugfd, s, strlen(s));
08049
08050 ast_mutex_unlock(&pridebugfdlock);
08051 }
08052
08053 static int pri_check_restart(struct zt_pri *pri)
08054 {
08055 do {
08056 pri->resetpos++;
08057 } while((pri->resetpos < pri->numchans) &&
08058 (!pri->pvts[pri->resetpos] ||
08059 pri->pvts[pri->resetpos]->call ||
08060 pri->pvts[pri->resetpos]->resetting));
08061 if (pri->resetpos < pri->numchans) {
08062
08063 pri->pvts[pri->resetpos]->resetting = 1;
08064 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
08065 } else {
08066 pri->resetting = 0;
08067 time(&pri->lastreset);
08068 }
08069 return 0;
08070 }
08071
08072 static int pri_hangup_all(struct zt_pvt *p, struct zt_pri *pri)
08073 {
08074 int x;
08075 int redo;
08076 ast_mutex_unlock(&pri->lock);
08077 ast_mutex_lock(&p->lock);
08078 do {
08079 redo = 0;
08080 for (x=0;x<3;x++) {
08081 while(p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) {
08082 redo++;
08083 ast_mutex_unlock(&p->lock);
08084 usleep(1);
08085 ast_mutex_lock(&p->lock);
08086 }
08087 if (p->subs[x].owner) {
08088 ast_queue_hangup(p->subs[x].owner);
08089 ast_mutex_unlock(&p->subs[x].owner->lock);
08090 }
08091 }
08092 } while (redo);
08093 ast_mutex_unlock(&p->lock);
08094 ast_mutex_lock(&pri->lock);
08095 return 0;
08096 }
08097 char * redirectingreason2str(int redirectingreason)
08098 {
08099 switch (redirectingreason) {
08100 case 0:
08101 return "UNKNOWN";
08102 case 1:
08103 return "BUSY";
08104 case 2:
08105 return "NO_REPLY";
08106 case 0xF:
08107 return "UNCONDITIONAL";
08108 default:
08109 return "NOREDIRECT";
08110 }
08111 }
08112
08113 static void apply_plan_to_number(char *buf, size_t size, const struct zt_pri *pri, const char *number, const int plan)
08114 {
08115 switch (plan) {
08116 case PRI_INTERNATIONAL_ISDN:
08117 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
08118 break;
08119 case PRI_NATIONAL_ISDN:
08120 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
08121 break;
08122 case PRI_LOCAL_ISDN:
08123 snprintf(buf, size, "%s%s", pri->localprefix, number);
08124 break;
08125 case PRI_PRIVATE:
08126 snprintf(buf, size, "%s%s", pri->privateprefix, number);
08127 break;
08128 case PRI_UNKNOWN:
08129 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
08130 break;
08131 default:
08132 snprintf(buf, size, "%s", number);
08133 break;
08134 }
08135 }
08136
08137 static void *pri_dchannel(void *vpri)
08138 {
08139 struct zt_pri *pri = vpri;
08140 pri_event *e;
08141 struct pollfd fds[NUM_DCHANS];
08142 int res;
08143 int chanpos = 0;
08144 int x;
08145 int haveidles;
08146 int activeidles;
08147 int nextidle = -1;
08148 struct ast_channel *c;
08149 struct timeval tv, lowest, *next;
08150 struct timeval lastidle = { 0, 0 };
08151 int doidling=0;
08152 char *cc;
08153 char idlen[80];
08154 struct ast_channel *idle;
08155 pthread_t p;
08156 time_t t;
08157 int i, which=-1;
08158 int numdchans;
08159 int cause=0;
08160 struct zt_pvt *crv;
08161 pthread_t threadid;
08162 pthread_attr_t attr;
08163 char ani2str[6];
08164 char plancallingnum[256];
08165 char plancallingani[256];
08166 char calledtonstr[10];
08167
08168 pthread_attr_init(&attr);
08169 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
08170
08171 gettimeofday(&lastidle, NULL);
08172 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
08173
08174 cc = strchr(pri->idleext, '@');
08175 if (cc) {
08176 *cc = '\0';
08177 cc++;
08178 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
08179 #if 0
08180
08181 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
08182 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
08183 else
08184 #endif
08185 doidling = 1;
08186 } else
08187 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
08188 }
08189 for(;;) {
08190 for (i=0;i<NUM_DCHANS;i++) {
08191 if (!pri->dchannels[i])
08192 break;
08193 fds[i].fd = pri->fds[i];
08194 fds[i].events = POLLIN | POLLPRI;
08195 fds[i].revents = 0;
08196 }
08197 numdchans = i;
08198 time(&t);
08199 ast_mutex_lock(&pri->lock);
08200 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->resetinterval > 0)) {
08201 if (pri->resetting && pri_is_up(pri)) {
08202 if (pri->resetpos < 0)
08203 pri_check_restart(pri);
08204 } else {
08205 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
08206 pri->resetting = 1;
08207 pri->resetpos = -1;
08208 }
08209 }
08210 }
08211
08212 if (doidling && pri_is_up(pri)) {
08213 nextidle = -1;
08214 haveidles = 0;
08215 activeidles = 0;
08216 for (x=pri->numchans;x>=0;x--) {
08217 if (pri->pvts[x] && !pri->pvts[x]->owner &&
08218 !pri->pvts[x]->call) {
08219 if (haveidles < pri->minunused) {
08220 haveidles++;
08221 } else if (!pri->pvts[x]->resetting) {
08222 nextidle = x;
08223 break;
08224 }
08225 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
08226 activeidles++;
08227 }
08228 #if 0
08229 printf("nextidle: %d, haveidles: %d, minunsed: %d\n",
08230 nextidle, haveidles, minunused);
08231 printf("nextidle: %d, haveidles: %d, ms: %ld, minunsed: %d\n",
08232 nextidle, haveidles, ast_tvdiff_ms(ast_tvnow(), lastidle), minunused);
08233 #endif
08234 if (nextidle > -1) {
08235 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
08236
08237 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
08238 idle = zt_request("Zap", AST_FORMAT_ULAW, idlen, &cause);
08239 if (idle) {
08240 pri->pvts[nextidle]->isidlecall = 1;
08241 if (ast_pthread_create(&p, NULL, do_idle_thread, idle)) {
08242 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
08243 zt_hangup(idle);
08244 }
08245 } else
08246 ast_log(LOG_WARNING, "Unable to request channel 'Zap/%s' for idle call\n", idlen);
08247 gettimeofday(&lastidle, NULL);
08248 }
08249 } else if ((haveidles < pri->minunused) &&
08250 (activeidles > pri->minidle)) {
08251
08252
08253 for (x=pri->numchans;x>=0;x--) {
08254
08255 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
08256 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08257 haveidles++;
08258
08259
08260 if ((haveidles >= pri->minunused) ||
08261 (activeidles <= pri->minidle))
08262 break;
08263 }
08264 }
08265 }
08266 }
08267
08268 lowest = ast_tv(60, 0);
08269 for (i=0; i<NUM_DCHANS; i++) {
08270
08271 if (!pri->dchannels[i])
08272 break;
08273 if ((next = pri_schedule_next(pri->dchans[i]))) {
08274
08275 tv = ast_tvsub(*next, ast_tvnow());
08276 if (tv.tv_sec < 0) {
08277 tv = ast_tv(0,0);
08278 }
08279 if (doidling || pri->resetting) {
08280 if (tv.tv_sec > 1) {
08281 tv = ast_tv(1, 0);
08282 }
08283 } else {
08284 if (tv.tv_sec > 60) {
08285 tv = ast_tv(60, 0);
08286 }
08287 }
08288 } else if (doidling || pri->resetting) {
08289
08290
08291 tv = ast_tv(1,0);
08292 } else {
08293
08294 tv = ast_tv(60, 0);
08295 }
08296 if (!i || ast_tvcmp(tv, lowest) < 0) {
08297 lowest = tv;
08298 }
08299 }
08300 ast_mutex_unlock(&pri->lock);
08301
08302 e = NULL;
08303 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
08304
08305 ast_mutex_lock(&pri->lock);
08306 if (!res) {
08307 for (which=0;which<NUM_DCHANS;which++) {
08308 if (!pri->dchans[which])
08309 break;
08310
08311 e = pri_schedule_run(pri->dchans[which]);
08312 if (e)
08313 break;
08314 }
08315 } else if (res > -1) {
08316 for (which=0;which<NUM_DCHANS;which++) {
08317 if (!pri->dchans[which])
08318 break;
08319 if (fds[which].revents & POLLPRI) {
08320
08321 x = 0;
08322 res = ioctl(pri->fds[which], ZT_GETEVENT, &x);
08323 if (x)
08324 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);
08325
08326 if (x == ZT_EVENT_ALARM) {
08327 pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
08328 pri_find_dchan(pri);
08329 } else if (x == ZT_EVENT_NOALARM) {
08330 pri->dchanavail[which] |= DCHAN_NOTINALARM;
08331 pri_restart(pri->dchans[which]);
08332 }
08333
08334 if (option_debug)
08335 ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
08336 } else if (fds[which].revents & POLLIN) {
08337 e = pri_check_event(pri->dchans[which]);
08338 }
08339 if (e)
08340 break;
08341 }
08342 } else if (errno != EINTR)
08343 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
08344
08345 if (e) {
08346 if (pri->debug)
08347 pri_dump_event(pri->dchans[which], e);
08348 if (e->e != PRI_EVENT_DCHAN_DOWN)
08349 pri->dchanavail[which] |= DCHAN_UP;
08350 switch(e->e) {
08351 case PRI_EVENT_DCHAN_UP:
08352 if (option_verbose > 1)
08353 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span);
08354 pri->dchanavail[which] |= DCHAN_UP;
08355 if (!pri->pri) pri_find_dchan(pri);
08356
08357
08358 time(&pri->lastreset);
08359
08360
08361 if (pri->resetinterval > -1) {
08362 pri->lastreset -= pri->resetinterval;
08363 pri->lastreset += 5;
08364 }
08365 pri->resetting = 0;
08366
08367 for (i=0; i<pri->numchans; i++)
08368 if (pri->pvts[i]) {
08369 pri->pvts[i]->inalarm = 0;
08370 }
08371 break;
08372 case PRI_EVENT_DCHAN_DOWN:
08373 if (option_verbose > 1)
08374 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span);
08375 pri->dchanavail[which] &= ~DCHAN_UP;
08376 pri_find_dchan(pri);
08377 if (!pri_is_up(pri)) {
08378 pri->resetting = 0;
08379
08380 for (i=0; i<pri->numchans; i++) {
08381 struct zt_pvt *p = pri->pvts[i];
08382 if (p) {
08383 if (p->call) {
08384 if (p->pri && p->pri->pri) {
08385 pri_hangup(p->pri->pri, p->call, -1);
08386 pri_destroycall(p->pri->pri, p->call);
08387 p->call = NULL;
08388 } else
08389 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
08390 }
08391 if (p->realcall) {
08392 pri_hangup_all(p->realcall, pri);
08393 } else if (p->owner)
08394 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08395 p->inalarm = 1;
08396 }
08397 }
08398 }
08399 break;
08400 case PRI_EVENT_RESTART:
08401 if (e->restart.channel > -1) {
08402 chanpos = pri_find_principle(pri, e->restart.channel);
08403 if (chanpos < 0)
08404 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
08405 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08406 else {
08407 if (option_verbose > 2)
08408 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n",
08409 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08410 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08411 if (pri->pvts[chanpos]->call) {
08412 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
08413 pri->pvts[chanpos]->call = NULL;
08414 }
08415
08416 if (pri->pvts[chanpos]->realcall)
08417 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08418 else if (pri->pvts[chanpos]->owner)
08419 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08420 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08421 }
08422 } else {
08423 if (option_verbose > 2)
08424 ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span);
08425 for (x=0;x < pri->numchans;x++)
08426 if (pri->pvts[x]) {
08427 ast_mutex_lock(&pri->pvts[x]->lock);
08428 if (pri->pvts[x]->call) {
08429 pri_destroycall(pri->pri, pri->pvts[x]->call);
08430 pri->pvts[x]->call = NULL;
08431 }
08432 if (pri->pvts[chanpos]->realcall)
08433 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08434 else if (pri->pvts[x]->owner)
08435 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08436 ast_mutex_unlock(&pri->pvts[x]->lock);
08437 }
08438 }
08439 break;
08440 case PRI_EVENT_KEYPAD_DIGIT:
08441 chanpos = pri_find_principle(pri, e->digit.channel);
08442 if (chanpos < 0) {
08443 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
08444 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
08445 } else {
08446 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
08447 if (chanpos > -1) {
08448 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08449
08450 if (pri->overlapdial && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
08451
08452 int digitlen = strlen(e->digit.digits);
08453 char digit;
08454 int i;
08455 for (i=0; i<digitlen; i++) {
08456 digit = e->digit.digits[i];
08457 {
08458 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08459 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08460 }
08461 }
08462 }
08463 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08464 }
08465 }
08466 break;
08467
08468 case PRI_EVENT_INFO_RECEIVED:
08469 chanpos = pri_find_principle(pri, e->ring.channel);
08470 if (chanpos < 0) {
08471 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
08472 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08473 } else {
08474 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
08475 if (chanpos > -1) {
08476 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08477
08478 if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
08479
08480 int digitlen = strlen(e->ring.callednum);
08481 char digit;
08482 int i;
08483 for (i=0; i<digitlen; i++) {
08484 digit = e->ring.callednum[i];
08485 {
08486 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08487 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08488 }
08489 }
08490 }
08491 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08492 }
08493 }
08494 break;
08495 case PRI_EVENT_RING:
08496 crv = NULL;
08497 if (e->ring.channel == -1)
08498 chanpos = pri_find_empty_chan(pri, 1);
08499 else
08500 chanpos = pri_find_principle(pri, e->ring.channel);
08501
08502 if (chanpos < 0) {
08503 ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
08504 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08505 } else {
08506 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08507 if (pri->pvts[chanpos]->owner) {
08508 if (pri->pvts[chanpos]->call == e->ring.call) {
08509 ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
08510 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08511 break;
08512 } else {
08513 ast_log(LOG_WARNING, "Ring requested on channel %d/%d already in use on span %d. Hanging up owner.\n",
08514 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08515 if (pri->pvts[chanpos]->realcall)
08516 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08517 else
08518 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08519 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08520 chanpos = -1;
08521 }
08522 }
08523 if (chanpos > -1)
08524 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08525 }
08526 if ((chanpos < 0) && (e->ring.flexible))
08527 chanpos = pri_find_empty_chan(pri, 1);
08528 if (chanpos > -1) {
08529 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08530 if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
08531
08532 crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
08533 if (crv)
08534 ast_mutex_lock(&crv->lock);
08535 if (!crv || crv->owner) {
08536 pri->pvts[chanpos]->call = NULL;
08537 if (crv) {
08538 if (crv->owner)
08539 crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08540 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);
08541 } else
08542 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);
08543 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
08544 if (crv)
08545 ast_mutex_unlock(&crv->lock);
08546 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08547 break;
08548 }
08549 }
08550 pri->pvts[chanpos]->call = e->ring.call;
08551 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
08552 if (pri->pvts[chanpos]->use_callerid) {
08553 ast_shrink_phone_number(plancallingnum);
08554 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
08555 #ifdef PRI_ANI
08556 if (!ast_strlen_zero(e->ring.callingani)) {
08557 apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
08558 ast_shrink_phone_number(plancallingani);
08559 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
08560 } else {
08561 pri->pvts[chanpos]->cid_ani[0] = '\0';
08562 }
08563 #endif
08564 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
08565 pri->pvts[chanpos]->cid_ton = e->ring.callingplan;
08566 } else {
08567 pri->pvts[chanpos]->cid_num[0] = '\0';
08568 pri->pvts[chanpos]->cid_ani[0] = '\0';
08569 pri->pvts[chanpos]->cid_name[0] = '\0';
08570 pri->pvts[chanpos]->cid_ton = 0;
08571 }
08572 apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
08573 e->ring.redirectingnum, e->ring.callingplanrdnis);
08574
08575 if (pri->pvts[chanpos]->immediate) {
08576 if (option_verbose > 2)
08577 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n");
08578 pri->pvts[chanpos]->exten[0] = 's';
08579 pri->pvts[chanpos]->exten[1] = '\0';
08580 }
08581
08582 else if (!ast_strlen_zero(e->ring.callednum)) {
08583 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
08584 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08585 } else
08586 pri->pvts[chanpos]->exten[0] = '\0';
08587
08588 if (!ast_strlen_zero(e->ring.callednum))
08589 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08590
08591 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
08592 if (option_verbose > 2)
08593 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n");
08594 pri->pvts[chanpos]->exten[0] = 's';
08595 pri->pvts[chanpos]->exten[1] = '\0';
08596 }
08597
08598 if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
08599 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08600
08601 int law;
08602 if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
08603
08604 law = 1;
08605 if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &law) == -1)
08606 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", pri->pvts[chanpos]->channel, law);
08607 }
08608 if (e->ring.layer1 == PRI_LAYER_1_ALAW)
08609 law = ZT_LAW_ALAW;
08610 else
08611 law = ZT_LAW_MULAW;
08612 res = zt_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].zfd, law);
08613 if (res < 0)
08614 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
08615 res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].zfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
08616 if (res < 0)
08617 ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
08618 if (e->ring.complete || !pri->overlapdial)
08619
08620 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
08621 else {
08622 if (pri->switchtype != PRI_SWITCH_GR303_TMC)
08623 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08624 else
08625 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08626 }
08627
08628 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
08629
08630
08631 if (pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08632
08633 ast_mutex_unlock(&pri->lock);
08634 if (crv) {
08635
08636 pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
08637 c = zt_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08638 pri->pvts[chanpos]->owner = &inuse;
08639 ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
08640 } else {
08641 c = zt_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08642 }
08643 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
08644 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
08645 }
08646 if(e->ring.ani2 >= 0) {
08647 snprintf(ani2str, 5, "%.2d", e->ring.ani2);
08648 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08649 }
08650
08651 #ifdef SUPPORT_USERUSER
08652 if (!ast_strlen_zero(e->ring.useruserinfo)) {
08653 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08654 }
08655 #endif
08656
08657 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08658 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08659 if (e->ring.redirectingreason >= 0)
08660 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08661
08662 ast_mutex_lock(&pri->lock);
08663 if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) {
08664 if (option_verbose > 2)
08665 ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
08666 plancallingnum, !ast_strlen_zero(pri->pvts[chanpos]->exten) ? pri->pvts[chanpos]->exten : "<unspecified>",
08667 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08668 } else {
08669 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
08670 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08671 if (c)
08672 ast_hangup(c);
08673 else {
08674 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08675 pri->pvts[chanpos]->call = NULL;
08676 }
08677 }
08678 } else {
08679 ast_mutex_unlock(&pri->lock);
08680
08681 c = zt_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype);
08682 ast_mutex_lock(&pri->lock);
08683 if (c) {
08684 char calledtonstr[10];
08685 if(e->ring.ani2 >= 0) {
08686 snprintf(ani2str, 5, "%d", e->ring.ani2);
08687 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08688 }
08689
08690 #ifdef SUPPORT_USERUSER
08691 if (!ast_strlen_zero(e->ring.useruserinfo)) {
08692 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08693 }
08694 #endif
08695
08696 if (e->ring.redirectingreason >= 0)
08697 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08698
08699 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08700 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08701 if (option_verbose > 2)
08702 ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
08703 plancallingnum, pri->pvts[chanpos]->exten,
08704 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08705 zt_enable_ec(pri->pvts[chanpos]);
08706 } else {
08707 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
08708 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08709 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08710 pri->pvts[chanpos]->call = NULL;
08711 }
08712 }
08713 } else {
08714 if (option_verbose > 2)
08715 ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
08716 pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
08717 pri->pvts[chanpos]->prioffset, pri->span);
08718 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
08719 pri->pvts[chanpos]->call = NULL;
08720 pri->pvts[chanpos]->exten[0] = '\0';
08721 }
08722 if (crv)
08723 ast_mutex_unlock(&crv->lock);
08724 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08725 } else
08726 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
08727 break;
08728 case PRI_EVENT_RINGING:
08729 chanpos = pri_find_principle(pri, e->ringing.channel);
08730 if (chanpos < 0) {
08731 ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
08732 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08733 chanpos = -1;
08734 }
08735 if (chanpos > -1) {
08736 chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
08737 if (chanpos < 0) {
08738 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
08739 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08740 chanpos = -1;
08741 } else {
08742 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08743 if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08744 zt_enable_ec(pri->pvts[chanpos]);
08745 pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1;
08746 pri->pvts[chanpos]->alerting = 1;
08747 } else
08748 ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n");
08749 #ifdef PRI_PROGRESS_MASK
08750 if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08751 #else
08752 if (e->ringing.progress == 8) {
08753 #endif
08754
08755 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08756
08757 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING);
08758 pri->pvts[chanpos]->dsp_features = 0;
08759 }
08760 }
08761
08762 #ifdef SUPPORT_USERUSER
08763 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
08764 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->ringing.useruserinfo);
08765 }
08766 #endif
08767
08768 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08769 }
08770 }
08771 break;
08772 case PRI_EVENT_PROGRESS:
08773
08774 chanpos = pri_find_principle(pri, e->proceeding.channel);
08775 if (chanpos > -1) {
08776 #ifdef PRI_PROGRESS_MASK
08777 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) {
08778 #else
08779 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) {
08780 #endif
08781 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
08782
08783 if (e->proceeding.cause > -1) {
08784 if (option_verbose > 2)
08785 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with cause code %d received\n", e->proceeding.cause);
08786
08787
08788 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
08789 if (pri->pvts[chanpos]->owner) {
08790 if (option_verbose > 2)
08791 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
08792
08793 pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
08794 f.subclass = AST_CONTROL_BUSY;
08795 }
08796 }
08797 }
08798
08799 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08800 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
08801 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08802 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08803 #ifdef PRI_PROGRESS_MASK
08804 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08805 #else
08806 if (e->proceeding.progress == 8) {
08807 #endif
08808
08809 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08810 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08811 pri->pvts[chanpos]->dsp_features = 0;
08812 }
08813 }
08814 pri->pvts[chanpos]->progress = 1;
08815 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08816 }
08817 }
08818 break;
08819 case PRI_EVENT_PROCEEDING:
08820 chanpos = pri_find_principle(pri, e->proceeding.channel);
08821 if (chanpos > -1) {
08822 if (!pri->pvts[chanpos]->proceeding) {
08823 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
08824
08825 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08826 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
08827 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08828 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08829 #ifdef PRI_PROGRESS_MASK
08830 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08831 #else
08832 if (e->proceeding.progress == 8) {
08833 #endif
08834
08835 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08836 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08837 pri->pvts[chanpos]->dsp_features = 0;
08838 }
08839
08840 f.subclass = AST_CONTROL_PROGRESS;
08841 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08842 }
08843 pri->pvts[chanpos]->proceeding = 1;
08844 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08845 }
08846 }
08847 break;
08848 case PRI_EVENT_FACNAME:
08849 chanpos = pri_find_principle(pri, e->facname.channel);
08850 if (chanpos < 0) {
08851 ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
08852 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08853 chanpos = -1;
08854 }
08855 if (chanpos > -1) {
08856 chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
08857 if (chanpos < 0) {
08858 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
08859 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08860 chanpos = -1;
08861 } else {
08862
08863 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08864 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
08865 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
08866 pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
08867 zt_enable_ec(pri->pvts[chanpos]);
08868 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08869 }
08870 }
08871 break;
08872 case PRI_EVENT_ANSWER:
08873 chanpos = pri_find_principle(pri, e->answer.channel);
08874 if (chanpos < 0) {
08875 ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
08876 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
08877 chanpos = -1;
08878 }
08879 if (chanpos > -1) {
08880 chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
08881 if (chanpos < 0) {
08882 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
08883 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
08884 chanpos = -1;
08885 } else {
08886 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08887
08888
08889
08890
08891
08892 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08893 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08894 pri->pvts[chanpos]->dsp_features = 0;
08895 }
08896 if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) {
08897 ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n");
08898 x = ZT_START;
08899 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_HOOK, &x);
08900 if (res < 0) {
08901 if (errno != EINPROGRESS) {
08902 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
08903 }
08904 }
08905 } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08906 pri->pvts[chanpos]->dialing = 1;
08907
08908 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_DIAL, &pri->pvts[chanpos]->dop);
08909 if (res < 0) {
08910 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", pri->pvts[chanpos]->channel);
08911 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
08912 } else
08913 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
08914 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
08915 } else if (pri->pvts[chanpos]->confirmanswer) {
08916 ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
08917 } else {
08918 pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
08919
08920 zt_enable_ec(pri->pvts[chanpos]);
08921 }
08922
08923 #ifdef SUPPORT_USERUSER
08924 if (!ast_strlen_zero(e->answer.useruserinfo)) {
08925 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->answer.useruserinfo);
08926 }
08927 #endif
08928
08929 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08930 }
08931 }
08932 break;
08933 case PRI_EVENT_HANGUP:
08934 chanpos = pri_find_principle(pri, e->hangup.channel);
08935 if (chanpos < 0) {
08936 ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
08937 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08938 chanpos = -1;
08939 }
08940 if (chanpos > -1) {
08941 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
08942 if (chanpos > -1) {
08943 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08944 if (!pri->pvts[chanpos]->alreadyhungup) {
08945
08946 pri->pvts[chanpos]->alreadyhungup = 1;
08947 if (pri->pvts[chanpos]->realcall)
08948 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08949 else if (pri->pvts[chanpos]->owner) {
08950
08951 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
08952 switch(e->hangup.cause) {
08953 case PRI_CAUSE_USER_BUSY:
08954 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
08955 break;
08956 case PRI_CAUSE_CALL_REJECTED:
08957 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
08958 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
08959 case PRI_CAUSE_SWITCH_CONGESTION:
08960 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
08961 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
08962 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
08963 break;
08964 default:
08965 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08966 }
08967 }
08968 if (option_verbose > 2)
08969 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup\n",
08970 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08971 } else {
08972 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
08973 pri->pvts[chanpos]->call = NULL;
08974 }
08975 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
08976 if (option_verbose > 2)
08977 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
08978 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08979 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
08980 pri->pvts[chanpos]->resetting = 1;
08981 }
08982 if (e->hangup.aoc_units > -1)
08983 if (option_verbose > 2)
08984 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
08985 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
08986
08987 #ifdef SUPPORT_USERUSER
08988 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
08989 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
08990 }
08991 #endif
08992
08993 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08994 } else {
08995 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
08996 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08997 }
08998 }
08999 break;
09000 #ifndef PRI_EVENT_HANGUP_REQ
09001 #error please update libpri
09002 #endif
09003 case PRI_EVENT_HANGUP_REQ:
09004 chanpos = pri_find_principle(pri, e->hangup.channel);
09005 if (chanpos < 0) {
09006 ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
09007 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09008 chanpos = -1;
09009 }
09010 if (chanpos > -1) {
09011 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09012 if (chanpos > -1) {
09013 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09014 if (pri->pvts[chanpos]->realcall)
09015 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09016 else if (pri->pvts[chanpos]->owner) {
09017 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
09018 switch(e->hangup.cause) {
09019 case PRI_CAUSE_USER_BUSY:
09020 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
09021 break;
09022 case PRI_CAUSE_CALL_REJECTED:
09023 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
09024 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
09025 case PRI_CAUSE_SWITCH_CONGESTION:
09026 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
09027 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
09028 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
09029 break;
09030 default:
09031 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09032 }
09033 if (option_verbose > 2)
09034 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup request\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09035 if (e->hangup.aoc_units > -1)
09036 if (option_verbose > 2)
09037 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
09038 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
09039 } else {
09040 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
09041 pri->pvts[chanpos]->call = NULL;
09042 }
09043 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
09044 if (option_verbose > 2)
09045 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n",
09046 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09047 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
09048 pri->pvts[chanpos]->resetting = 1;
09049 }
09050
09051 #ifdef SUPPORT_USERUSER
09052 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09053 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
09054 }
09055 #endif
09056
09057 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09058 } else {
09059 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);
09060 }
09061 }
09062 break;
09063 case PRI_EVENT_HANGUP_ACK:
09064 chanpos = pri_find_principle(pri, e->hangup.channel);
09065 if (chanpos < 0) {
09066 ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
09067 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
09068 chanpos = -1;
09069 }
09070 if (chanpos > -1) {
09071 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
09072 if (chanpos > -1) {
09073 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09074 pri->pvts[chanpos]->call = NULL;
09075 pri->pvts[chanpos]->resetting = 0;
09076 if (pri->pvts[chanpos]->owner) {
09077 if (option_verbose > 2)
09078 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);
09079 }
09080
09081 #ifdef SUPPORT_USERUSER
09082 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
09083 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
09084 }
09085 #endif
09086
09087 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09088 }
09089 }
09090 break;
09091 case PRI_EVENT_CONFIG_ERR:
09092 ast_log(LOG_WARNING, "PRI Error: %s\n", e->err.err);
09093 break;
09094 case PRI_EVENT_RESTART_ACK:
09095 chanpos = pri_find_principle(pri, e->restartack.channel);
09096 if (chanpos < 0) {
09097
09098
09099
09100 for (x=0;x<pri->numchans;x++) {
09101 if (pri->pvts[x] && pri->pvts[x]->resetting) {
09102 chanpos = x;
09103 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09104 ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
09105 pri->pvts[chanpos]->prioffset, pri->span);
09106 if (pri->pvts[chanpos]->realcall)
09107 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09108 else if (pri->pvts[chanpos]->owner) {
09109 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
09110 pri->pvts[chanpos]->prioffset, pri->span);
09111 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09112 }
09113 pri->pvts[chanpos]->resetting = 0;
09114 if (option_verbose > 2)
09115 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
09116 pri->pvts[chanpos]->prioffset, pri->span);
09117 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09118 if (pri->resetting)
09119 pri_check_restart(pri);
09120 break;
09121 }
09122 }
09123 if (chanpos < 0) {
09124 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
09125 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09126 }
09127 chanpos = -1;
09128 }
09129 if (chanpos > -1) {
09130 if (pri->pvts[chanpos]) {
09131 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09132 if (pri->pvts[chanpos]->realcall)
09133 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09134 else if (pri->pvts[chanpos]->owner) {
09135 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
09136 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09137 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09138 }
09139 pri->pvts[chanpos]->resetting = 0;
09140 if (option_verbose > 2)
09141 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
09142 pri->pvts[chanpos]->prioffset, pri->span);
09143 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09144 if (pri->resetting)
09145 pri_check_restart(pri);
09146 }
09147 }
09148 break;
09149 case PRI_EVENT_SETUP_ACK:
09150 chanpos = pri_find_principle(pri, e->setup_ack.channel);
09151 if (chanpos < 0) {
09152 ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
09153 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
09154 } else {
09155 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
09156 if (chanpos > -1) {
09157 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09158 pri->pvts[chanpos]->setup_ack = 1;
09159
09160 for (x=0;x<strlen(pri->pvts[chanpos]->dialdest);x++) {
09161 ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
09162 pri_information(pri->pri, pri->pvts[chanpos]->call,
09163 pri->pvts[chanpos]->dialdest[x]);
09164 }
09165 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09166 } else
09167 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
09168 }
09169 break;
09170 case PRI_EVENT_NOTIFY:
09171 chanpos = pri_find_principle(pri, e->notify.channel);
09172 if (chanpos < 0) {
09173 ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
09174 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
09175 } else {
09176 struct ast_frame f = { AST_FRAME_CONTROL, };
09177 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09178 switch(e->notify.info) {
09179 case PRI_NOTIFY_REMOTE_HOLD:
09180 f.subclass = AST_CONTROL_HOLD;
09181 zap_queue_frame(pri->pvts[chanpos], &f, pri);
09182 break;
09183 case PRI_NOTIFY_REMOTE_RETRIEVAL:
09184 f.subclass = AST_CONTROL_UNHOLD;
09185 zap_queue_frame(pri->pvts[chanpos], &f, pri);
09186 break;
09187 }
09188 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09189 }
09190 break;
09191 default:
09192 ast_log(LOG_DEBUG, "Event: %d\n", e->e);
09193 }
09194 }
09195 ast_mutex_unlock(&pri->lock);
09196 }
09197
09198 return NULL;
09199 }
09200
09201 static int start_pri(struct zt_pri *pri)
09202 {
09203 int res, x;
09204 ZT_PARAMS p;
09205 ZT_BUFFERINFO bi;
09206 struct zt_spaninfo si;
09207 int i;
09208
09209 for (i=0;i<NUM_DCHANS;i++) {
09210 if (!pri->dchannels[i])
09211 break;
09212 pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600);
09213 x = pri->dchannels[i];
09214 if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],ZT_SPECIFY,&x) == -1)) {
09215 ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
09216 return -1;
09217 }
09218 res = ioctl(pri->fds[i], ZT_GET_PARAMS, &p);
09219 if (res) {
09220 zt_close(pri->fds[i]);
09221 pri->fds[i] = -1;
09222 ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
09223 return -1;
09224 }
09225 if (p.sigtype != ZT_SIG_HDLCFCS) {
09226 zt_close(pri->fds[i]);
09227 pri->fds[i] = -1;
09228 ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode. See /etc/zaptel.conf\n", x);
09229 return -1;
09230 }
09231 memset(&si, 0, sizeof(si));
09232 res = ioctl(pri->fds[i], ZT_SPANSTAT, &si);
09233 if (res) {
09234 zt_close(pri->fds[i]);
09235 pri->fds[i] = -1;
09236 ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
09237 }
09238 if (!si.alarms)
09239 pri->dchanavail[i] |= DCHAN_NOTINALARM;
09240 else
09241 pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
09242 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
09243 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
09244 bi.numbufs = 32;
09245 bi.bufsize = 1024;
09246 if (ioctl(pri->fds[i], ZT_SET_BUFINFO, &bi)) {
09247 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d\n", x);
09248 zt_close(pri->fds[i]);
09249 pri->fds[i] = -1;
09250 return -1;
09251 }
09252 pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
09253
09254 if (pri->switchtype == PRI_SWITCH_GR303_TMC)
09255 pri->overlapdial = 1;
09256 pri_set_overlapdial(pri->dchans[i],pri->overlapdial);
09257
09258 if (i)
09259 pri_enslave(pri->dchans[0], pri->dchans[i]);
09260 if (!pri->dchans[i]) {
09261 zt_close(pri->fds[i]);
09262 pri->fds[i] = -1;
09263 ast_log(LOG_ERROR, "Unable to create PRI structure\n");
09264 return -1;
09265 }
09266 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
09267 pri_set_nsf(pri->dchans[i], pri->nsf);
09268 #ifdef PRI_GETSET_TIMERS
09269 for (x = 0; x < PRI_MAX_TIMERS; x++) {
09270 if (pritimers[x] != 0)
09271 pri_set_timer(pri->dchans[i], x, pritimers[x]);
09272 }
09273 #endif
09274 }
09275
09276 pri->pri = pri->dchans[0];
09277 pri->resetpos = -1;
09278 if (ast_pthread_create(&pri->master, NULL, pri_dchannel, pri)) {
09279 for (i=0;i<NUM_DCHANS;i++) {
09280 if (!pri->dchannels[i])
09281 break;
09282 zt_close(pri->fds[i]);
09283 pri->fds[i] = -1;
09284 }
09285 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
09286 return -1;
09287 }
09288 return 0;
09289 }
09290
09291 static char *complete_span_helper(char *line, char *word, int pos, int state, int rpos)
09292 {
09293 int span=1;
09294 char tmp[50];
09295 if (pos != rpos)
09296 return 0;
09297 while(span <= NUM_SPANS) {
09298 if (span > state && pris[span-1].pri)
09299 break;
09300 span++;
09301 }
09302 if (span <= NUM_SPANS) {
09303 snprintf(tmp, sizeof(tmp), "%d", span);
09304 return strdup(tmp);
09305 } else
09306 return NULL;
09307 }
09308
09309 static char *complete_span_4(char *line, char *word, int pos, int state)
09310 {
09311 return complete_span_helper(line,word,pos,state,3);
09312 }
09313
09314 static char *complete_span_5(char *line, char *word, int pos, int state)
09315 {
09316 return complete_span_helper(line,word,pos,state,4);
09317 }
09318
09319 static int handle_pri_set_debug_file(int fd, int argc, char **argv)
09320 {
09321 int myfd;
09322
09323 if (!strncasecmp(argv[1], "set", 3)) {
09324 if (argc < 5)
09325 return RESULT_SHOWUSAGE;
09326
09327 if (ast_strlen_zero(argv[4]))
09328 return RESULT_SHOWUSAGE;
09329
09330 myfd = open(argv[4], O_CREAT|O_WRONLY);
09331 if (myfd < 0) {
09332 ast_cli(fd, "Unable to open '%s' for writing\n", argv[4]);
09333 return RESULT_SUCCESS;
09334 }
09335
09336 ast_mutex_lock(&pridebugfdlock);
09337
09338 if (pridebugfd >= 0)
09339 close(pridebugfd);
09340
09341 pridebugfd = myfd;
09342 ast_copy_string(pridebugfilename,argv[4],sizeof(pridebugfilename));
09343
09344 ast_mutex_unlock(&pridebugfdlock);
09345
09346 ast_cli(fd, "PRI debug output will be sent to '%s'\n", argv[4]);
09347 } else {
09348
09349 ast_mutex_lock(&pridebugfdlock);
09350 close(pridebugfd);
09351 pridebugfd = -1;
09352 ast_cli(fd, "PRI debug output to file disabled\n");
09353 ast_mutex_unlock(&pridebugfdlock);
09354 }
09355
09356 return RESULT_SUCCESS;
09357 }
09358
09359 static int handle_pri_debug(int fd, int argc, char *argv[])
09360 {
09361 int span;
09362 int x;
09363 if (argc < 4) {
09364 return RESULT_SHOWUSAGE;
09365 }
09366 span = atoi(argv[3]);
09367 if ((span < 1) || (span > NUM_SPANS)) {
09368 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[3], 1, NUM_SPANS);
09369 return RESULT_SUCCESS;
09370 }
09371 if (!pris[span-1].pri) {
09372 ast_cli(fd, "No PRI running on span %d\n", span);
09373 return RESULT_SUCCESS;
09374 }
09375 for (x=0;x<NUM_DCHANS;x++) {
09376 if (pris[span-1].dchans[x])
09377 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE);
09378 }
09379 ast_cli(fd, "Enabled debugging on span %d\n", span);
09380 return RESULT_SUCCESS;
09381 }
09382
09383
09384
09385 static int handle_pri_no_debug(int fd, int argc, char *argv[])
09386 {
09387 int span;
09388 int x;
09389 if (argc < 5)
09390 return RESULT_SHOWUSAGE;
09391 span = atoi(argv[4]);
09392 if ((span < 1) || (span > NUM_SPANS)) {
09393 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09394 return RESULT_SUCCESS;
09395 }
09396 if (!pris[span-1].pri) {
09397 ast_cli(fd, "No PRI running on span %d\n", span);
09398 return RESULT_SUCCESS;
09399 }
09400 for (x=0;x<NUM_DCHANS;x++) {
09401 if (pris[span-1].dchans[x])
09402 pri_set_debug(pris[span-1].dchans[x], 0);
09403 }
09404 ast_cli(fd, "Disabled debugging on span %d\n", span);
09405 return RESULT_SUCCESS;
09406 }
09407
09408 static int handle_pri_really_debug(int fd, int argc, char *argv[])
09409 {
09410 int span;
09411 int x;
09412 if (argc < 5)
09413 return RESULT_SHOWUSAGE;
09414 span = atoi(argv[4]);
09415 if ((span < 1) || (span > NUM_SPANS)) {
09416 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09417 return RESULT_SUCCESS;
09418 }
09419 if (!pris[span-1].pri) {
09420 ast_cli(fd, "No PRI running on span %d\n", span);
09421 return RESULT_SUCCESS;
09422 }
09423 for (x=0;x<NUM_DCHANS;x++) {
09424 if (pris[span-1].dchans[x])
09425 pri_set_debug(pris[span-1].dchans[x], (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE));
09426 }
09427 ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span);
09428 return RESULT_SUCCESS;
09429 }
09430
09431 static void build_status(char *s, size_t len, int status, int active)
09432 {
09433 if (!s || len < 1) {
09434 return;
09435 }
09436 s[0] = '\0';
09437 if (status & DCHAN_PROVISIONED)
09438 strncat(s, "Provisioned, ", len - strlen(s) - 1);
09439 if (!(status & DCHAN_NOTINALARM))
09440 strncat(s, "In Alarm, ", len - strlen(s) - 1);
09441 if (status & DCHAN_UP)
09442 strncat(s, "Up", len - strlen(s) - 1);
09443 else
09444 strncat(s, "Down", len - strlen(s) - 1);
09445 if (active)
09446 strncat(s, ", Active", len - strlen(s) - 1);
09447 else
09448 strncat(s, ", Standby", len - strlen(s) - 1);
09449 s[len - 1] = '\0';
09450 }
09451
09452 static int handle_pri_show_span(int fd, int argc, char *argv[])
09453 {
09454 int span;
09455 int x;
09456 char status[256];
09457 if (argc < 4)
09458 return RESULT_SHOWUSAGE;
09459 span = atoi(argv[3]);
09460 if ((span < 1) || (span > NUM_SPANS)) {
09461 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09462 return RESULT_SUCCESS;
09463 }
09464 if (!pris[span-1].pri) {
09465 ast_cli(fd, "No PRI running on span %d\n", span);
09466 return RESULT_SUCCESS;
09467 }
09468 for(x=0;x<NUM_DCHANS;x++) {
09469 if (pris[span-1].dchannels[x]) {
09470 #ifdef PRI_DUMP_INFO_STR
09471 char *info_str = NULL;
09472 #endif
09473 ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
09474 build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
09475 ast_cli(fd, "Status: %s\n", status);
09476 #ifdef PRI_DUMP_INFO_STR
09477 info_str = pri_dump_info_str(pris[span-1].pri);
09478 if (info_str) {
09479 ast_cli(fd, "%s", info_str);
09480 free(info_str);
09481 }
09482 #else
09483 pri_dump_info(pris[span-1].pri);
09484 #endif
09485 ast_cli(fd, "\n");
09486 }
09487 }
09488 return RESULT_SUCCESS;
09489 }
09490
09491 static int handle_pri_show_debug(int fd, int argc, char *argv[])
09492 {
09493 int x;
09494 int span;
09495 int count=0;
09496 int debug=0;
09497
09498 for(span=0;span<NUM_SPANS;span++) {
09499 if (pris[span].pri) {
09500 for(x=0;x<NUM_DCHANS;x++) {
09501 debug=0;
09502 if (pris[span].dchans[x]) {
09503 debug = pri_get_debug(pris[span].dchans[x]);
09504 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" );
09505 count++;
09506 }
09507 }
09508 }
09509
09510 }
09511 ast_mutex_lock(&pridebugfdlock);
09512 if (pridebugfd >= 0)
09513 ast_cli(fd, "Logging PRI debug to file %s\n", pridebugfilename);
09514 ast_mutex_unlock(&pridebugfdlock);
09515
09516 if (!count)
09517 ast_cli(fd, "No debug set or no PRI running\n");
09518 return RESULT_SUCCESS;
09519 }
09520
09521 static char pri_debug_help[] =
09522 "Usage: pri debug span <span>\n"
09523 " Enables debugging on a given PRI span\n";
09524
09525 static char pri_no_debug_help[] =
09526 "Usage: pri no debug span <span>\n"
09527 " Disables debugging on a given PRI span\n";
09528
09529 static char pri_really_debug_help[] =
09530 "Usage: pri intensive debug span <span>\n"
09531 " Enables debugging down to the Q.921 level\n";
09532
09533 static char pri_show_span_help[] =
09534 "Usage: pri show span <span>\n"
09535 " Displays PRI Information\n";
09536
09537 static struct ast_cli_entry zap_pri_cli[] = {
09538 { { "pri", "debug", "span", NULL }, handle_pri_debug,
09539 "Enables PRI debugging on a span", pri_debug_help, complete_span_4 },
09540 { { "pri", "no", "debug", "span", NULL }, handle_pri_no_debug,
09541 "Disables PRI debugging on a span", pri_no_debug_help, complete_span_5 },
09542 { { "pri", "intense", "debug", "span", NULL }, handle_pri_really_debug,
09543 "Enables REALLY INTENSE PRI debugging", pri_really_debug_help, complete_span_5 },
09544 { { "pri", "show", "span", NULL }, handle_pri_show_span,
09545 "Displays PRI Information", pri_show_span_help, complete_span_4 },
09546 { { "pri", "show", "debug", NULL }, handle_pri_show_debug,
09547 "Displays current PRI debug settings" },
09548 { { "pri", "set", "debug", "file", NULL }, handle_pri_set_debug_file,
09549 "Sends PRI debug output to the specified file" },
09550 { { "pri", "unset", "debug", "file", NULL }, handle_pri_set_debug_file,
09551 "Ends PRI debug output to file" },
09552 };
09553
09554 #endif
09555
09556
09557 #ifdef ZAPATA_R2
09558 static int handle_r2_no_debug(int fd, int argc, char *argv[])
09559 {
09560 int chan;
09561 struct zt_pvt *tmp = NULL;;
09562 if (argc < 5)
09563 return RESULT_SHOWUSAGE;
09564 chan = atoi(argv[4]);
09565 if ((chan < 1) || (chan > NUM_SPANS)) {
09566 ast_cli(fd, "Invalid channel %s. Should be a number greater than 0\n", argv[4]);
09567 return RESULT_SUCCESS;
09568 }
09569 tmp = iflist;
09570 while(tmp) {
09571 if (tmp->channel == chan) {
09572 if (tmp->r2) {
09573 mfcr2_set_debug(tmp->r2, 0);
09574 ast_cli(fd, "Disabled R2 debugging on channel %d\n", chan);
09575 return RESULT_SUCCESS;
09576 }
09577 break;
09578 }
09579 tmp = tmp->next;
09580 }
09581 if (tmp)
09582 ast_cli(fd, "No R2 running on channel %d\n", chan);
09583 else
09584 ast_cli(fd, "No such zap channel %d\n", chan);
09585 return RESULT_SUCCESS;
09586 }
09587
09588 static int handle_r2_debug(int fd, int argc, char *argv[])
09589 {
09590 int chan;
09591 struct zt_pvt *tmp = NULL;;
09592 if (argc < 4) {
09593 return RESULT_SHOWUSAGE;
09594 }
09595 chan = atoi(argv[3]);
09596 if ((chan < 1) || (chan > NUM_SPANS)) {
09597 ast_cli(fd, "Invalid channel %s. Should be a number greater than 0\n", argv[3]);
09598 return RESULT_SUCCESS;
09599 }
09600 tmp = iflist;
09601 while(tmp) {
09602 if (tmp->channel == chan) {
09603 if (tmp->r2) {
09604 mfcr2_set_debug(tmp->r2, 0xFFFFFFFF);
09605 ast_cli(fd, "Enabled R2 debugging on channel %d\n", chan);
09606 return RESULT_SUCCESS;
09607 }
09608 break;
09609 }
09610 tmp = tmp->next;
09611 }
09612 if (tmp)
09613 ast_cli(fd, "No R2 running on channel %d\n", chan);
09614 else
09615 ast_cli(fd, "No such zap channel %d\n", chan);
09616 return RESULT_SUCCESS;
09617 }
09618 static char r2_debug_help[] =
09619 "Usage: r2 debug channel <channel>\n"
09620 " Enables R2 protocol level debugging on a given channel\n";
09621
09622 static char r2_no_debug_help[] =
09623 "Usage: r2 no debug channel <channel>\n"
09624 " Enables R2 protocol level debugging on a given channel\n";
09625
09626 static struct ast_cli_entry zap_r2_cli[] = {
09627 { { "r2", "debug", "channel", NULL }, handle_r2_debug,
09628 "Enables R2 debugging on a channel", r2_debug_help },
09629 { { "r2", "no", "debug", "channel", NULL }, handle_r2_no_debug,
09630 "Disables R2 debugging on a channel", r2_no_debug_help },
09631 };
09632
09633 #endif
09634
09635 static int zap_destroy_channel(int fd, int argc, char **argv)
09636 {
09637 int channel = 0;
09638 struct zt_pvt *tmp = NULL;
09639 struct zt_pvt *prev = NULL;
09640
09641 if (argc != 4) {
09642 return RESULT_SHOWUSAGE;
09643 }
09644 channel = atoi(argv[3]);
09645
09646 tmp = iflist;
09647 while (tmp) {
09648 if (tmp->channel == channel) {
09649 destroy_channel(prev, tmp, 1);
09650 return RESULT_SUCCESS;
09651 }
09652 prev = tmp;
09653 tmp = tmp->next;
09654 }
09655 return RESULT_FAILURE;
09656 }
09657
09658 static int setup_zap(int reload);
09659 static int zap_restart(void)
09660 {
09661 if (option_verbose > 0)
09662 ast_verbose(VERBOSE_PREFIX_1 "Destroying channels and reloading zaptel configuration.\n");
09663 while (iflist) {
09664 if (option_debug)
09665 ast_log(LOG_DEBUG, "Destroying zaptel channel no. %d\n", iflist->channel);
09666
09667 destroy_channel(NULL, iflist, 1);
09668 }
09669 if (option_debug)
09670 ast_log(LOG_DEBUG, "Channels destroyed. Now re-reading config.\n");
09671 if (setup_zap(0) != 0) {
09672 ast_log(LOG_WARNING, "Reload channels from zap config failed!\n");
09673 return 1;
09674 }
09675 return 0;
09676 }
09677
09678 static int zap_restart_cmd(int fd, int argc, char **argv)
09679 {
09680 if (argc != 2) {
09681 return RESULT_SHOWUSAGE;
09682 }
09683
09684 if (zap_restart() != 0)
09685 return RESULT_FAILURE;
09686 return RESULT_SUCCESS;
09687 }
09688
09689 static int action_zaprestart(struct mansession *s, struct message *m)
09690 {
09691 if (zap_restart() != 0) {
09692 astman_send_error(s, m, "Failed rereading zaptel configuration");
09693 return 1;
09694 }
09695 astman_send_ack(s, m, "ZapRestart: Success");
09696 return 0;
09697 }
09698
09699 static int zap_show_channels(int fd, int argc, char **argv)
09700 {
09701 #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09702 #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09703 struct zt_pvt *tmp = NULL;
09704 char tmps[20] = "";
09705 ast_mutex_t *lock;
09706 struct zt_pvt *start;
09707 #ifdef ZAPATA_PRI
09708 int trunkgroup;
09709 struct zt_pri *pri=NULL;
09710 int x;
09711 #endif
09712
09713 lock = &iflock;
09714 start = iflist;
09715
09716 #ifdef ZAPATA_PRI
09717 if (argc == 4) {
09718 if ((trunkgroup = atoi(argv[3])) < 1)
09719 return RESULT_SHOWUSAGE;
09720 for (x=0;x<NUM_SPANS;x++) {
09721 if (pris[x].trunkgroup == trunkgroup) {
09722 pri = pris + x;
09723 break;
09724 }
09725 }
09726 if (pri) {
09727 start = pri->crvs;
09728 lock = &pri->lock;
09729 } else {
09730 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09731 return RESULT_FAILURE;
09732 }
09733 } else
09734 #endif
09735 if (argc != 3)
09736 return RESULT_SHOWUSAGE;
09737
09738 ast_mutex_lock(lock);
09739 #ifdef ZAPATA_PRI
09740 ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MusicOnHold");
09741 #else
09742 ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MusicOnHold");
09743 #endif
09744
09745 tmp = start;
09746 while (tmp) {
09747 if (tmp->channel > 0) {
09748 snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
09749 } else
09750 ast_copy_string(tmps, "pseudo", sizeof(tmps));
09751 ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->musicclass);
09752 tmp = tmp->next;
09753 }
09754 ast_mutex_unlock(lock);
09755 return RESULT_SUCCESS;
09756 #undef FORMAT
09757 #undef FORMAT2
09758 }
09759
09760 static int zap_show_channel(int fd, int argc, char **argv)
09761 {
09762 int channel;
09763 struct zt_pvt *tmp = NULL;
09764 ZT_CONFINFO ci;
09765 ZT_PARAMS ps;
09766 int x;
09767 ast_mutex_t *lock;
09768 struct zt_pvt *start;
09769 #ifdef ZAPATA_PRI
09770 char *c;
09771 int trunkgroup;
09772 struct zt_pri *pri=NULL;
09773 #endif
09774
09775 lock = &iflock;
09776 start = iflist;
09777
09778 if (argc != 4)
09779 return RESULT_SHOWUSAGE;
09780 #ifdef ZAPATA_PRI
09781 if ((c = strchr(argv[3], ':'))) {
09782 if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2)
09783 return RESULT_SHOWUSAGE;
09784 if ((trunkgroup < 1) || (channel < 1))
09785 return RESULT_SHOWUSAGE;
09786 for (x=0;x<NUM_SPANS;x++) {
09787 if (pris[x].trunkgroup == trunkgroup) {
09788 pri = pris + x;
09789 break;
09790 }
09791 }
09792 if (pri) {
09793 start = pri->crvs;
09794 lock = &pri->lock;
09795 } else {
09796 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09797 return RESULT_FAILURE;
09798 }
09799 } else
09800 #endif
09801 channel = atoi(argv[3]);
09802
09803 ast_mutex_lock(lock);
09804 tmp = start;
09805 while (tmp) {
09806 if (tmp->channel == channel) {
09807 #ifdef ZAPATA_PRI
09808 if (pri)
09809 ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
09810 else
09811 #endif
09812 ast_cli(fd, "Channel: %d\n", tmp->channel);
09813 ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].zfd);
09814 ast_cli(fd, "Span: %d\n", tmp->span);
09815 ast_cli(fd, "Extension: %s\n", tmp->exten);
09816 ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
09817 ast_cli(fd, "Context: %s\n", tmp->context);
09818 ast_cli(fd, "Caller ID: %s\n", tmp->cid_num);
09819 ast_cli(fd, "Calling TON: %d\n", tmp->cid_ton);
09820 ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name);
09821 ast_cli(fd, "Destroy: %d\n", tmp->destroy);
09822 ast_cli(fd, "InAlarm: %d\n", tmp->inalarm);
09823 ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig));
09824 ast_cli(fd, "Radio: %d\n", tmp->radio);
09825 ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
09826 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)" : "");
09827 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)" : "");
09828 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)" : "");
09829 ast_cli(fd, "Confno: %d\n", tmp->confno);
09830 ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno);
09831 ast_cli(fd, "Real in conference: %d\n", tmp->inconference);
09832 ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
09833 ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
09834 ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
09835 ast_cli(fd, "Default law: %s\n", tmp->law == ZT_LAW_MULAW ? "ulaw" : tmp->law == ZT_LAW_ALAW ? "alaw" : "unknown");
09836 ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
09837 ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
09838 ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF");
09839 if (tmp->master)
09840 ast_cli(fd, "Master Channel: %d\n", tmp->master->channel);
09841 for (x=0;x<MAX_SLAVES;x++) {
09842 if (tmp->slaves[x])
09843 ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
09844 }
09845 #ifdef ZAPATA_PRI
09846 if (tmp->pri) {
09847 ast_cli(fd, "PRI Flags: ");
09848 if (tmp->resetting)
09849 ast_cli(fd, "Resetting ");
09850 if (tmp->call)
09851 ast_cli(fd, "Call ");
09852 if (tmp->bearer)
09853 ast_cli(fd, "Bearer ");
09854 ast_cli(fd, "\n");
09855 if (tmp->logicalspan)
09856 ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan);
09857 else
09858 ast_cli(fd, "PRI Logical Span: Implicit\n");
09859 }
09860
09861 #endif
09862 #ifdef ZAPATA_R2
09863 if (tmp->r2) {
09864 ast_cli(fd, "R2 Flags: ");
09865 if (tmp->r2blocked)
09866 ast_cli(fd, "Blocked ");
09867 if (tmp->hasr2call)
09868 ast_cli(fd, "Call ");
09869 ast_cli(fd, "\n");
09870 }
09871 #endif
09872 memset(&ci, 0, sizeof(ci));
09873 ps.channo = tmp->channel;
09874 if (tmp->subs[SUB_REAL].zfd > -1) {
09875 if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
09876 ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
09877 }
09878 #ifdef ZT_GETCONFMUTE
09879 if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONFMUTE, &x)) {
09880 ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
09881 }
09882 #endif
09883 if (ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
09884 ast_log(LOG_WARNING, "Failed to get parameters on channel %d\n", tmp->channel);
09885 } else {
09886 ast_cli(fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
09887 }
09888 }
09889 ast_mutex_unlock(lock);
09890 return RESULT_SUCCESS;
09891 }
09892 tmp = tmp->next;
09893 }
09894
09895 ast_cli(fd, "Unable to find given channel %d\n", channel);
09896 ast_mutex_unlock(lock);
09897 return RESULT_FAILURE;
09898 }
09899
09900 static char zap_show_cadences_help[] =
09901 "Usage: zap show cadences\n"
09902 " Shows all cadences currently defined\n";
09903
09904 static int handle_zap_show_cadences(int fd, int argc, char *argv[])
09905 {
09906 int i, j;
09907 for (i=0;i<num_cadence;i++) {
09908 char output[1024];
09909 char tmp[16], tmp2[64];
09910 snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
09911 term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
09912
09913 for (j=0;j<16;j++) {
09914 if (cadences[i].ringcadence[j] == 0)
09915 break;
09916 snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
09917 if (cidrings[i] * 2 - 1 == j)
09918 term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
09919 else
09920 term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
09921 if (j != 0)
09922 strncat(output, ",", sizeof(output) - strlen(output) - 1);
09923 strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
09924 }
09925 ast_cli(fd,"%s\n",output);
09926 }
09927 return 0;
09928 }
09929
09930
09931 static int zap_show_status(int fd, int argc, char *argv[]) {
09932 #define FORMAT "%-40.40s %-10.10s %-10d %-10d %-10d\n"
09933 #define FORMAT2 "%-40.40s %-10.10s %-10.10s %-10.10s %-10.10s\n"
09934
09935 int span;
09936 int res;
09937 char alarms[50];
09938
09939 int ctl;
09940 ZT_SPANINFO s;
09941
09942 ctl = open("/dev/zap/ctl", O_RDWR);
09943 if (ctl < 0) {
09944 fprintf(stderr, "Unable to open /dev/zap/ctl: %s\n", strerror(errno));
09945 ast_cli(fd, "No Zaptel interface found.\n");
09946 return RESULT_FAILURE;
09947 }
09948 ast_cli(fd,FORMAT2, "Description", "Alarms","IRQ","bpviol","CRC4");
09949
09950 for (span=1;span < ZT_MAX_SPANS;++span) {
09951 s.spanno = span;
09952 res = ioctl(ctl, ZT_SPANSTAT, &s);
09953 if (res) {
09954 continue;
09955 }
09956 alarms[0] = '\0';
09957 if (s.alarms > 0) {
09958 if (s.alarms & ZT_ALARM_BLUE)
09959 strcat(alarms,"BLU/");
09960 if (s.alarms & ZT_ALARM_YELLOW)
09961 strcat(alarms, "YEL/");
09962 if (s.alarms & ZT_ALARM_RED)
09963 strcat(alarms, "RED/");
09964 if (s.alarms & ZT_ALARM_LOOPBACK)
09965 strcat(alarms,"LB/");
09966 if (s.alarms & ZT_ALARM_RECOVER)
09967 strcat(alarms,"REC/");
09968 if (s.alarms & ZT_ALARM_NOTOPEN)
09969 strcat(alarms, "NOP/");
09970 if (!strlen(alarms))
09971 strcat(alarms, "UUU/");
09972 if (strlen(alarms)) {
09973
09974 alarms[strlen(alarms)-1]='\0';
09975 }
09976 } else {
09977 if (s.numchans)
09978 strcpy(alarms, "OK");
09979 else
09980 strcpy(alarms, "UNCONFIGURED");
09981 }
09982
09983 ast_cli(fd, FORMAT, s.desc, alarms, s.irqmisses, s.bpvcount, s.crc4count);
09984 }
09985 close(ctl);
09986
09987 return RESULT_SUCCESS;
09988 #undef FORMAT
09989 #undef FORMAT2
09990 }
09991
09992 static char show_channels_usage[] =
09993 "Usage: zap show channels\n"
09994 " Shows a list of available channels\n";
09995
09996 static char show_channel_usage[] =
09997 "Usage: zap show channel <chan num>\n"
09998 " Detailed information about a given channel\n";
09999
10000 static char zap_show_status_usage[] =
10001 "Usage: zap show status\n"
10002 " Shows a list of Zaptel cards with status\n";
10003
10004 static char destroy_channel_usage[] =
10005 "Usage: zap destroy channel <chan num>\n"
10006 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n";
10007
10008 static char zap_restart_usage[] =
10009 "Usage: zap restart\n"
10010 " fully restarts the zaptel channels: destroys them all and then re-reads from config.\n";
10011
10012 static struct ast_cli_entry zap_cli[] = {
10013 { { "zap", "show", "cadences", NULL }, handle_zap_show_cadences,
10014 "List cadences", zap_show_cadences_help },
10015 { {"zap", "show", "channels", NULL}, zap_show_channels,
10016 "Show active zapata channels", show_channels_usage },
10017 { {"zap", "show", "channel", NULL}, zap_show_channel,
10018 "Show information on a channel", show_channel_usage },
10019 { {"zap", "destroy", "channel", NULL}, zap_destroy_channel,
10020 "Destroy a channel", destroy_channel_usage },
10021 { {"zap", "restart", NULL}, zap_restart_cmd,
10022 "Fully restart zaptel channels", zap_restart_usage },
10023 { {"zap", "show", "status", NULL}, zap_show_status,
10024 "Show all Zaptel cards status", zap_show_status_usage },
10025 };
10026
10027 #define TRANSFER 0
10028 #define HANGUP 1
10029
10030 static int zap_fake_event(struct zt_pvt *p, int mode)
10031 {
10032 if (p) {
10033 switch(mode) {
10034 case TRANSFER:
10035 p->fake_event = ZT_EVENT_WINKFLASH;
10036 break;
10037 case HANGUP:
10038 p->fake_event = ZT_EVENT_ONHOOK;
10039 break;
10040 default:
10041 ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);
10042 }
10043 }
10044 return 0;
10045 }
10046 static struct zt_pvt *find_channel(int channel)
10047 {
10048 struct zt_pvt *p = iflist;
10049 while(p) {
10050 if (p->channel == channel) {
10051 break;
10052 }
10053 p = p->next;
10054 }
10055 return p;
10056 }
10057
10058 static int action_zapdndon(struct mansession *s, struct message *m)
10059 {
10060 struct zt_pvt *p = NULL;
10061 char *channel = astman_get_header(m, "ZapChannel");
10062 if (ast_strlen_zero(channel)) {
10063 astman_send_error(s, m, "No channel specified");
10064 return 0;
10065 }
10066 p = find_channel(atoi(channel));
10067 if (!p) {
10068 astman_send_error(s, m, "No such channel");
10069 return 0;
10070 }
10071 p->dnd = 1;
10072 astman_send_ack(s, m, "DND Enabled");
10073 return 0;
10074 }
10075
10076 static int action_zapdndoff(struct mansession *s, struct message *m)
10077 {
10078 struct zt_pvt *p = NULL;
10079 char *channel = astman_get_header(m, "ZapChannel");
10080 if (ast_strlen_zero(channel)) {
10081 astman_send_error(s, m, "No channel specified");
10082 return 0;
10083 }
10084 p = find_channel(atoi(channel));
10085 if (!p) {
10086 astman_send_error(s, m, "No such channel");
10087 return 0;
10088 }
10089 p->dnd = 0;
10090 astman_send_ack(s, m, "DND Disabled");
10091 return 0;
10092 }
10093
10094 static int action_transfer(struct mansession *s, struct message *m)
10095 {
10096 struct zt_pvt *p = NULL;
10097 char *channel = astman_get_header(m, "ZapChannel");
10098 if (ast_strlen_zero(channel)) {
10099 astman_send_error(s, m, "No channel specified");
10100 return 0;
10101 }
10102 p = find_channel(atoi(channel));
10103 if (!p) {
10104 astman_send_error(s, m, "No such channel");
10105 return 0;
10106 }
10107 zap_fake_event(p,TRANSFER);
10108 astman_send_ack(s, m, "ZapTransfer");
10109 return 0;
10110 }
10111
10112 static int action_transferhangup(struct mansession *s, struct message *m)
10113 {
10114 struct zt_pvt *p = NULL;
10115 char *channel = astman_get_header(m, "ZapChannel");
10116 if (ast_strlen_zero(channel)) {
10117 astman_send_error(s, m, "No channel specified");
10118 return 0;
10119 }
10120 p = find_channel(atoi(channel));
10121 if (!p) {
10122 astman_send_error(s, m, "No such channel");
10123 return 0;
10124 }
10125 zap_fake_event(p,HANGUP);
10126 astman_send_ack(s, m, "ZapHangup");
10127 return 0;
10128 }
10129
10130 static int action_zapdialoffhook(struct mansession *s, struct message *m)
10131 {
10132 struct zt_pvt *p = NULL;
10133 char *channel = astman_get_header(m, "ZapChannel");
10134 char *number = astman_get_header(m, "Number");
10135 int i;
10136 if (ast_strlen_zero(channel)) {
10137 astman_send_error(s, m, "No channel specified");
10138 return 0;
10139 }
10140 if (ast_strlen_zero(number)) {
10141 astman_send_error(s, m, "No number 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 if (!p->owner) {
10150 astman_send_error(s, m, "Channel does not have it's owner");
10151 return 0;
10152 }
10153 for (i=0; i<strlen(number); i++) {
10154 struct ast_frame f = { AST_FRAME_DTMF, number[i] };
10155 zap_queue_frame(p, &f, NULL);
10156 }
10157 astman_send_ack(s, m, "ZapDialOffhook");
10158 return 0;
10159 }
10160
10161 static int action_zapshowchannels(struct mansession *s, struct message *m)
10162 {
10163 struct zt_pvt *tmp = NULL;
10164 char *id = astman_get_header(m, "ActionID");
10165 char idText[256] = "";
10166
10167 astman_send_ack(s, m, "Zapata channel status will follow");
10168 if (!ast_strlen_zero(id))
10169 snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id);
10170
10171 ast_mutex_lock(&iflock);
10172
10173 tmp = iflist;
10174 while (tmp) {
10175 if (tmp->channel > 0) {
10176 int alarm = get_alarms(tmp);
10177 ast_cli(s->fd,
10178 "Event: ZapShowChannels\r\n"
10179 "Channel: %d\r\n"
10180 "Signalling: %s\r\n"
10181 "Context: %s\r\n"
10182 "DND: %s\r\n"
10183 "Alarm: %s\r\n"
10184 "%s"
10185 "\r\n",
10186 tmp->channel, sig2str(tmp->sig), tmp->context,
10187 tmp->dnd ? "Enabled" : "Disabled",
10188 alarm2str(alarm), idText);
10189 }
10190
10191 tmp = tmp->next;
10192 }
10193
10194 ast_mutex_unlock(&iflock);
10195
10196 ast_cli(s->fd,
10197 "Event: ZapShowChannelsComplete\r\n"
10198 "%s"
10199 "\r\n",
10200 idText);
10201 return 0;
10202 }
10203
10204 static int __unload_module(void)
10205 {
10206 int x = 0;
10207 struct zt_pvt *p, *pl;
10208 #ifdef ZAPATA_PRI
10209 int i;
10210 for(i=0;i<NUM_SPANS;i++) {
10211 if (pris[i].master != AST_PTHREADT_NULL)
10212 pthread_cancel(pris[i].master);
10213 }
10214 ast_cli_unregister_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(zap_pri_cli[0]));
10215 #endif
10216 #ifdef ZAPATA_R2
10217 ast_cli_unregister_multiple(zap_r2_cli, sizeof(zap_r2_cli) / sizeof(zap_r2_cli[0]));
10218 #endif
10219 ast_cli_unregister_multiple(zap_cli, sizeof(zap_cli) / sizeof(zap_cli[0]));
10220 ast_manager_unregister( "ZapDialOffhook" );
10221 ast_manager_unregister( "ZapHangup" );
10222 ast_manager_unregister( "ZapTransfer" );
10223 ast_manager_unregister( "ZapDNDoff" );
10224 ast_manager_unregister( "ZapDNDon" );
10225 ast_manager_unregister("ZapShowChannels");
10226 ast_channel_unregister(&zap_tech);
10227 if (!ast_mutex_lock(&iflock)) {
10228
10229 p = iflist;
10230 while(p) {
10231 if (p->owner)
10232 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
10233 p = p->next;
10234 }
10235 ast_mutex_unlock(&iflock);
10236 } else {
10237 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10238 return -1;
10239 }
10240 if (!ast_mutex_lock(&monlock)) {
10241 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
10242 pthread_cancel(monitor_thread);
10243 pthread_kill(monitor_thread, SIGURG);
10244 pthread_join(monitor_thread, NULL);
10245 }
10246 monitor_thread = AST_PTHREADT_STOP;
10247 ast_mutex_unlock(&monlock);
10248 } else {
10249 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10250 return -1;
10251 }
10252
10253 if (!ast_mutex_lock(&iflock)) {
10254
10255 p = iflist;
10256 while(p) {
10257
10258 if (p->cidspill)
10259 free(p->cidspill);
10260
10261 if (p->subs[SUB_REAL].zfd > -1)
10262 zt_close(p->subs[SUB_REAL].zfd);
10263 pl = p;
10264 p = p->next;
10265 x++;
10266
10267 if(pl)
10268 destroy_zt_pvt(&pl);
10269 ast_verbose(VERBOSE_PREFIX_3 "Unregistered channel %d\n", x);
10270 }
10271 iflist = NULL;
10272 ifcount = 0;
10273 ast_mutex_unlock(&iflock);
10274 } else {
10275 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10276 return -1;
10277 }
10278 #ifdef ZAPATA_PRI
10279 for(i=0;i<NUM_SPANS;i++) {
10280 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
10281 pthread_join(pris[i].master, NULL);
10282 zt_close(pris[i].fds[i]);
10283 }
10284 #endif
10285 return 0;
10286 }
10287
10288 int unload_module()
10289 {
10290 #ifdef ZAPATA_PRI
10291 int y;
10292 for (y=0;y<NUM_SPANS;y++)
10293 ast_mutex_destroy(&pris[y].lock);
10294 #endif
10295 return __unload_module();
10296 }
10297
10298 static int setup_zap(int reload)
10299 {
10300 struct ast_config *cfg;
10301 struct ast_variable *v;
10302 struct zt_pvt *tmp;
10303 char *chan;
10304 char *c;
10305 char *ringc;
10306 int start, finish,x;
10307 int y;
10308 int found_pseudo = 0;
10309 int cur_radio = 0;
10310 #ifdef ZAPATA_PRI
10311 int spanno;
10312 int i;
10313 int logicalspan;
10314 int trunkgroup;
10315 int dchannels[NUM_DCHANS];
10316 struct zt_pri *pri;
10317 #endif
10318
10319 cfg = ast_config_load(config);
10320
10321
10322 if (!cfg) {
10323 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
10324 return -1;
10325 }
10326
10327
10328 if (ast_mutex_lock(&iflock)) {
10329
10330 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
10331 return -1;
10332 }
10333 #ifdef ZAPATA_PRI
10334 if (!reload) {
10335
10336 v = ast_variable_browse(cfg, "trunkgroups");
10337 while(v) {
10338 if (!strcasecmp(v->name, "trunkgroup")) {
10339 trunkgroup = atoi(v->value);
10340 if (trunkgroup > 0) {
10341 if ((c = strchr(v->value, ','))) {
10342 i = 0;
10343 memset(dchannels, 0, sizeof(dchannels));
10344 while(c && (i < NUM_DCHANS)) {
10345 dchannels[i] = atoi(c + 1);
10346 if (dchannels[i] < 0) {
10347 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);
10348 } else
10349 i++;
10350 c = strchr(c + 1, ',');
10351 }
10352 if (i) {
10353 if (pri_create_trunkgroup(trunkgroup, dchannels)) {
10354 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);
10355 } else if (option_verbose > 1)
10356 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");
10357 } else
10358 ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of zapata.conf\n", trunkgroup, v->lineno);
10359 } else
10360 ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of zapata.conf\n", trunkgroup, v->lineno);
10361 } else
10362 ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of zapata.conf\n", v->lineno);
10363 } else if (!strcasecmp(v->name, "spanmap")) {
10364 spanno = atoi(v->value);
10365 if (spanno > 0) {
10366 if ((c = strchr(v->value, ','))) {
10367 trunkgroup = atoi(c + 1);
10368 if (trunkgroup > 0) {
10369 if ((c = strchr(c + 1, ',')))
10370 logicalspan = atoi(c + 1);
10371 else
10372 logicalspan = 0;
10373 if (logicalspan >= 0) {
10374 if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
10375 ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
10376 } else if (option_verbose > 1)
10377 ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
10378 } else
10379 ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of zapata.conf\n", v->lineno);
10380 } else
10381 ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of zapata.conf\n", v->lineno);
10382 } else
10383 ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of zapata.conf\n", v->lineno);
10384 } else
10385 ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of zapata.conf\n", v->lineno);
10386 } else {
10387 ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
10388 }
10389 v = v->next;
10390 }
10391 }
10392 #endif
10393 v = ast_variable_browse(cfg, "channels");
10394 while(v) {
10395
10396 if (!strcasecmp(v->name, "channel")
10397 #ifdef ZAPATA_PRI
10398 || !strcasecmp(v->name, "crv")
10399 #endif
10400 ) {
10401 if (reload == 0) {
10402 if (cur_signalling < 0) {
10403 ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
10404 ast_config_destroy(cfg);
10405 ast_mutex_unlock(&iflock);
10406 return -1;
10407 }
10408 }
10409 c = v->value;
10410
10411 #ifdef ZAPATA_PRI
10412 pri = NULL;
10413 if (!strcasecmp(v->name, "crv")) {
10414 if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) {
10415 ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", v->lineno);
10416 ast_config_destroy(cfg);
10417 ast_mutex_unlock(&iflock);
10418 return -1;
10419 }
10420 if (trunkgroup < 1) {
10421 ast_log(LOG_WARNING, "CRV trunk group must be a postive number at line %d\n", v->lineno);
10422 ast_config_destroy(cfg);
10423 ast_mutex_unlock(&iflock);
10424 return -1;
10425 }
10426 c+=y;
10427 for (y=0;y<NUM_SPANS;y++) {
10428 if (pris[y].trunkgroup == trunkgroup) {
10429 pri = pris + y;
10430 break;
10431 }
10432 }
10433 if (!pri) {
10434 ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, v->lineno);
10435 ast_config_destroy(cfg);
10436 ast_mutex_unlock(&iflock);
10437 return -1;
10438 }
10439 }
10440 #endif
10441 chan = strsep(&c, ",");
10442 while(chan) {
10443 if (sscanf(chan, "%d-%d", &start, &finish) == 2) {
10444
10445 } else if (sscanf(chan, "%d", &start)) {
10446
10447 finish = start;
10448 } else if (!strcasecmp(chan, "pseudo")) {
10449 finish = start = CHAN_PSEUDO;
10450 found_pseudo = 1;
10451 } else {
10452 ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", v->value, chan);
10453 ast_config_destroy(cfg);
10454 ast_mutex_unlock(&iflock);
10455 return -1;
10456 }
10457 if (finish < start) {
10458 ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
10459 x = finish;
10460 finish = start;
10461 start = x;
10462 }
10463 for (x=start;x<=finish;x++) {
10464 #ifdef ZAPATA_PRI
10465 tmp = mkintf(x, cur_signalling, cur_radio, pri, reload);
10466 #else
10467 tmp = mkintf(x, cur_signalling, cur_radio, NULL, reload);
10468 #endif
10469
10470 if (tmp) {
10471 if (option_verbose > 2) {
10472 #ifdef ZAPATA_PRI
10473 if (pri)
10474 ast_verbose(VERBOSE_PREFIX_3 "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup,x, sig2str(tmp->sig));
10475 else
10476 #endif
10477 ast_verbose(VERBOSE_PREFIX_3 "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig));
10478 }
10479 } else {
10480 if (reload == 1)
10481 ast_log(LOG_ERROR, "Unable to reconfigure channel '%s'\n", v->value);
10482 else
10483 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
10484 ast_config_destroy(cfg);
10485 ast_mutex_unlock(&iflock);
10486 return -1;
10487 }
10488 }
10489 chan = strsep(&c, ",");
10490 }
10491 } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
10492 if (ast_true(v->value))
10493 usedistinctiveringdetection = 1;
10494 } else if (!strcasecmp(v->name, "dring1context")) {
10495 ast_copy_string(drings.ringContext[0].contextData,v->value,sizeof(drings.ringContext[0].contextData));
10496 } else if (!strcasecmp(v->name, "dring2context")) {
10497 ast_copy_string(drings.ringContext[1].contextData,v->value,sizeof(drings.ringContext[1].contextData));
10498 } else if (!strcasecmp(v->name, "dring3context")) {
10499 ast_copy_string(drings.ringContext[2].contextData,v->value,sizeof(drings.ringContext[2].contextData));
10500 } else if (!strcasecmp(v->name, "dring1")) {
10501 ringc = v->value;
10502 sscanf(ringc, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]);
10503 } else if (!strcasecmp(v->name, "dring2")) {
10504 ringc = v->value;
10505 sscanf(ringc,"%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]);
10506 } else if (!strcasecmp(v->name, "dring3")) {
10507 ringc = v->value;
10508 sscanf(ringc, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]);
10509 } else if (!strcasecmp(v->name, "usecallerid")) {
10510 use_callerid = ast_true(v->value);
10511 } else if (!strcasecmp(v->name, "cidsignalling")) {
10512 if (!strcasecmp(v->value, "bell"))
10513 cid_signalling = CID_SIG_BELL;
10514 else if (!strcasecmp(v->value, "v23"))
10515 cid_signalling = CID_SIG_V23;
10516 else if (!strcasecmp(v->value, "dtmf"))
10517 cid_signalling = CID_SIG_DTMF;
10518 else if (ast_true(v->value))
10519 cid_signalling = CID_SIG_BELL;
10520 } else if (!strcasecmp(v->name, "cidstart")) {
10521 if (!strcasecmp(v->value, "ring"))
10522 cid_start = CID_START_RING;
10523 else if (!strcasecmp(v->value, "polarity"))
10524 cid_start = CID_START_POLARITY;
10525 else if (!strcasecmp(v->value, "usehist"))
10526 cid_start = CID_START_USEHIST;
10527 else if (ast_true(v->value))
10528 cid_start = CID_START_RING;
10529 } else if (!strcasecmp(v->name, "threewaycalling")) {
10530 threewaycalling = ast_true(v->value);
10531 } else if (!strcasecmp(v->name, "cancallforward")) {
10532 cancallforward = ast_true(v->value);
10533 } else if (!strcasecmp(v->name, "relaxdtmf")) {
10534 if (ast_true(v->value))
10535 relaxdtmf = DSP_DIGITMODE_RELAXDTMF;
10536 else
10537 relaxdtmf = 0;
10538 } else if (!strcasecmp(v->name, "mailbox")) {
10539 ast_copy_string(mailbox, v->value, sizeof(mailbox));
10540 } else if (!strcasecmp(v->name, "adsi")) {
10541 adsi = ast_true(v->value);
10542 } else if (!strcasecmp(v->name, "transfer")) {
10543 transfer = ast_true(v->value);
10544 } else if (!strcasecmp(v->name, "canpark")) {
10545 canpark = ast_true(v->value);
10546 } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
10547 echocanbridged = ast_true(v->value);
10548 } else if (!strcasecmp(v->name, "busydetect")) {
10549 busydetect = ast_true(v->value);
10550 } else if (!strcasecmp(v->name, "busycount")) {
10551 busycount = atoi(v->value);
10552 } else if (!strcasecmp(v->name, "busypattern")) {
10553 if (sscanf(v->value, "%d,%d", &busy_tonelength, &busy_quietlength) != 2) {
10554 ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength\n");
10555 }
10556 } else if (!strcasecmp(v->name, "callprogress")) {
10557 if (ast_true(v->value))
10558 callprogress |= 1;
10559 else
10560 callprogress &= ~1;
10561 } else if (!strcasecmp(v->name, "faxdetect")) {
10562 if (!strcasecmp(v->value, "incoming")) {
10563 callprogress |= 4;
10564 callprogress &= ~2;
10565 } else if (!strcasecmp(v->value, "outgoing")) {
10566 callprogress &= ~4;
10567 callprogress |= 2;
10568 } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
10569 callprogress |= 6;
10570 else
10571 callprogress &= ~6;
10572 } else if (!strcasecmp(v->name, "echocancel")) {
10573 if (!ast_strlen_zero(v->value)) {
10574 y = atoi(v->value);
10575 } else
10576 y = 0;
10577 if ((y == 32) || (y == 64) || (y == 128) || (y == 256))
10578 echocancel = y;
10579 else {
10580 echocancel = ast_true(v->value);
10581 if (echocancel)
10582 echocancel=128;
10583 }
10584 } else if (!strcasecmp(v->name, "echotraining")) {
10585 if (sscanf(v->value, "%d", &y) == 1) {
10586 if ((y < 10) || (y > 4000)) {
10587 ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 2000 ms at line %d\n", v->lineno);
10588 } else {
10589 echotraining = y;
10590 }
10591 } else if (ast_true(v->value)) {
10592 echotraining = 400;
10593 } else
10594 echotraining = 0;
10595 } else if (!strcasecmp(v->name, "hidecallerid")) {
10596 hidecallerid = ast_true(v->value);
10597 } else if (!strcasecmp(v->name, "pulsedial")) {
10598 pulse = ast_true(v->value);
10599 } else if (!strcasecmp(v->name, "callreturn")) {
10600 callreturn = ast_true(v->value);
10601 } else if (!strcasecmp(v->name, "callwaiting")) {
10602 callwaiting = ast_true(v->value);
10603 } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
10604 callwaitingcallerid = ast_true(v->value);
10605 } else if (!strcasecmp(v->name, "context")) {
10606 ast_copy_string(context, v->value, sizeof(context));
10607 } else if (!strcasecmp(v->name, "language")) {
10608 ast_copy_string(language, v->value, sizeof(language));
10609 } else if (!strcasecmp(v->name, "progzone")) {
10610 ast_copy_string(progzone, v->value, sizeof(progzone));
10611 } else if (!strcasecmp(v->name, "musiconhold")) {
10612 ast_copy_string(musicclass, v->value, sizeof(musicclass));
10613 } else if (!strcasecmp(v->name, "stripmsd")) {
10614 stripmsd = atoi(v->value);
10615 } else if (!strcasecmp(v->name, "jitterbuffers")) {
10616 numbufs = atoi(v->value);
10617 } else if (!strcasecmp(v->name, "group")) {
10618 cur_group = ast_get_group(v->value);
10619 } else if (!strcasecmp(v->name, "callgroup")) {
10620 cur_callergroup = ast_get_group(v->value);
10621 } else if (!strcasecmp(v->name, "pickupgroup")) {
10622 cur_pickupgroup = ast_get_group(v->value);
10623 } else if (!strcasecmp(v->name, "immediate")) {
10624 immediate = ast_true(v->value);
10625 } else if (!strcasecmp(v->name, "transfertobusy")) {
10626 transfertobusy = ast_true(v->value);
10627 } else if (!strcasecmp(v->name, "rxgain")) {
10628 if (sscanf(v->value, "%f", &rxgain) != 1) {
10629 ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value);
10630 }
10631 } else if (!strcasecmp(v->name, "txgain")) {
10632 if (sscanf(v->value, "%f", &txgain) != 1) {
10633 ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value);
10634 }
10635 } else if (!strcasecmp(v->name, "tonezone")) {
10636 if (sscanf(v->value, "%d", &tonezone) != 1) {
10637 ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
10638 }
10639 } else if (!strcasecmp(v->name, "callerid")) {
10640 if (!strcasecmp(v->value, "asreceived")) {
10641 cid_num[0] = '\0';
10642 cid_name[0] = '\0';
10643 } else {
10644 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
10645 }
10646 } else if (!strcasecmp(v->name, "useincomingcalleridonzaptransfer")) {
10647 zaptrcallerid = ast_true(v->value);
10648 } else if (!strcasecmp(v->name, "restrictcid")) {
10649 restrictcid = ast_true(v->value);
10650 } else if (!strcasecmp(v->name, "usecallingpres")) {
10651 use_callingpres = ast_true(v->value);
10652 } else if (!strcasecmp(v->name, "accountcode")) {
10653 ast_copy_string(accountcode, v->value, sizeof(accountcode));
10654 } else if (!strcasecmp(v->name, "amaflags")) {
10655 y = ast_cdr_amaflags2int(v->value);
10656 if (y < 0)
10657 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
10658 else
10659 amaflags = y;
10660 } else if(!reload){
10661 if (!strcasecmp(v->name, "signalling")) {
10662 if (!strcasecmp(v->value, "em")) {
10663 cur_signalling = SIG_EM;
10664 } else if (!strcasecmp(v->value, "em_e1")) {
10665 cur_signalling = SIG_EM_E1;
10666 } else if (!strcasecmp(v->value, "em_w")) {
10667 cur_signalling = SIG_EMWINK;
10668 cur_radio = 0;
10669 } else if (!strcasecmp(v->value, "fxs_ls")) {
10670 cur_signalling = SIG_FXSLS;
10671 cur_radio = 0;
10672 } else if (!strcasecmp(v->value, "fxs_gs")) {
10673 cur_signalling = SIG_FXSGS;
10674 cur_radio = 0;
10675 } else if (!strcasecmp(v->value, "fxs_ks")) {
10676 cur_signalling = SIG_FXSKS;
10677 cur_radio = 0;
10678 } else if (!strcasecmp(v->value, "fxo_ls")) {
10679 cur_signalling = SIG_FXOLS;
10680 cur_radio = 0;
10681 } else if (!strcasecmp(v->value, "fxo_gs")) {
10682 cur_signalling = SIG_FXOGS;
10683 cur_radio = 0;
10684 } else if (!strcasecmp(v->value, "fxo_ks")) {
10685 cur_signalling = SIG_FXOKS;
10686 cur_radio = 0;
10687 } else if (!strcasecmp(v->value, "fxs_rx")) {
10688 cur_signalling = SIG_FXSKS;
10689 cur_radio = 1;
10690 } else if (!strcasecmp(v->value, "fxo_rx")) {
10691 cur_signalling = SIG_FXOLS;
10692 cur_radio = 1;
10693 } else if (!strcasecmp(v->value, "fxs_tx")) {
10694 cur_signalling = SIG_FXSLS;
10695 cur_radio = 1;
10696 } else if (!strcasecmp(v->value, "fxo_tx")) {
10697 cur_signalling = SIG_FXOGS;
10698 cur_radio = 1;
10699 } else if (!strcasecmp(v->value, "em_rx")) {
10700 cur_signalling = SIG_EM;
10701 cur_radio = 1;
10702 } else if (!strcasecmp(v->value, "em_tx")) {
10703 cur_signalling = SIG_EM;
10704 cur_radio = 1;
10705 } else if (!strcasecmp(v->value, "em_rxtx")) {
10706 cur_signalling = SIG_EM;
10707 cur_radio = 2;
10708 } else if (!strcasecmp(v->value, "em_txrx")) {
10709 cur_signalling = SIG_EM;
10710 cur_radio = 2;
10711 } else if (!strcasecmp(v->value, "sf")) {
10712 cur_signalling = SIG_SF;
10713 cur_radio = 0;
10714 } else if (!strcasecmp(v->value, "sf_w")) {
10715 cur_signalling = SIG_SFWINK;
10716 cur_radio = 0;
10717 } else if (!strcasecmp(v->value, "sf_featd")) {
10718 cur_signalling = SIG_FEATD;
10719 cur_radio = 0;
10720 } else if (!strcasecmp(v->value, "sf_featdmf")) {
10721 cur_signalling = SIG_FEATDMF;
10722 cur_radio = 0;
10723 } else if (!strcasecmp(v->value, "sf_featb")) {
10724 cur_signalling = SIG_SF_FEATB;
10725 cur_radio = 0;
10726 } else if (!strcasecmp(v->value, "sf")) {
10727 cur_signalling = SIG_SF;
10728 cur_radio = 0;
10729 } else if (!strcasecmp(v->value, "sf_rx")) {
10730 cur_signalling = SIG_SF;
10731 cur_radio = 1;
10732 } else if (!strcasecmp(v->value, "sf_tx")) {
10733 cur_signalling = SIG_SF;
10734 cur_radio = 1;
10735 } else if (!strcasecmp(v->value, "sf_rxtx")) {
10736 cur_signalling = SIG_SF;
10737 cur_radio = 2;
10738 } else if (!strcasecmp(v->value, "sf_txrx")) {
10739 cur_signalling = SIG_SF;
10740 cur_radio = 2;
10741 } else if (!strcasecmp(v->value, "featd")) {
10742 cur_signalling = SIG_FEATD;
10743 cur_radio = 0;
10744 } else if (!strcasecmp(v->value, "featdmf")) {
10745 cur_signalling = SIG_FEATDMF;
10746 cur_radio = 0;
10747 } else if (!strcasecmp(v->value, "featdmf_ta")) {
10748 cur_signalling = SIG_FEATDMF_TA;
10749 cur_radio = 0;
10750 } else if (!strcasecmp(v->value, "e911")) {
10751 cur_signalling = SIG_E911;
10752 cur_radio = 0;
10753 } else if (!strcasecmp(v->value, "featb")) {
10754 cur_signalling = SIG_FEATB;
10755 cur_radio = 0;
10756 #ifdef ZAPATA_PRI
10757 } else if (!strcasecmp(v->value, "pri_net")) {
10758 cur_radio = 0;
10759 cur_signalling = SIG_PRI;
10760 pritype = PRI_NETWORK;
10761 } else if (!strcasecmp(v->value, "pri_cpe")) {
10762 cur_signalling = SIG_PRI;
10763 cur_radio = 0;
10764 pritype = PRI_CPE;
10765 } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
10766 cur_signalling = SIG_GR303FXOKS;
10767 cur_radio = 0;
10768 pritype = PRI_NETWORK;
10769 } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
10770 cur_signalling = SIG_GR303FXSKS;
10771 cur_radio = 0;
10772 pritype = PRI_CPE;
10773 #endif
10774 #ifdef ZAPATA_R2
10775 } else if (!strcasecmp(v->value, "r2")) {
10776 cur_signalling = SIG_R2;
10777 cur_radio = 0;
10778 #endif
10779 } else {
10780 ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
10781 }
10782 #ifdef ZAPATA_R2
10783 } else if (!strcasecmp(v->name, "r2country")) {
10784 r2prot = str2r2prot(v->value);
10785 if (r2prot < 0) {
10786 ast_log(LOG_WARNING, "Unknown R2 Country '%s' at line %d.\n", v->value, v->lineno);
10787 }
10788 #endif
10789 #ifdef ZAPATA_PRI
10790 } else if (!strcasecmp(v->name, "pridialplan")) {
10791 if (!strcasecmp(v->value, "national")) {
10792 dialplan = PRI_NATIONAL_ISDN + 1;
10793 } else if (!strcasecmp(v->value, "unknown")) {
10794 dialplan = PRI_UNKNOWN + 1;
10795 } else if (!strcasecmp(v->value, "private")) {
10796 dialplan = PRI_PRIVATE + 1;
10797 } else if (!strcasecmp(v->value, "international")) {
10798 dialplan = PRI_INTERNATIONAL_ISDN + 1;
10799 } else if (!strcasecmp(v->value, "local")) {
10800 dialplan = PRI_LOCAL_ISDN + 1;
10801 } else if (!strcasecmp(v->value, "dynamic")) {
10802 dialplan = -1;
10803 } else {
10804 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10805 }
10806 } else if (!strcasecmp(v->name, "prilocaldialplan")) {
10807 if (!strcasecmp(v->value, "national")) {
10808 localdialplan = PRI_NATIONAL_ISDN + 1;
10809 } else if (!strcasecmp(v->value, "unknown")) {
10810 localdialplan = PRI_UNKNOWN + 1;
10811 } else if (!strcasecmp(v->value, "private")) {
10812 localdialplan = PRI_PRIVATE + 1;
10813 } else if (!strcasecmp(v->value, "international")) {
10814 localdialplan = PRI_INTERNATIONAL_ISDN + 1;
10815 } else if (!strcasecmp(v->value, "local")) {
10816 localdialplan = PRI_LOCAL_ISDN + 1;
10817 } else if (!strcasecmp(v->value, "dynamic")) {
10818 localdialplan = -1;
10819 } else {
10820 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10821 }
10822 } else if (!strcasecmp(v->name, "switchtype")) {
10823 if (!strcasecmp(v->value, "national"))
10824 switchtype = PRI_SWITCH_NI2;
10825 else if (!strcasecmp(v->value, "ni1"))
10826 switchtype = PRI_SWITCH_NI1;
10827 else if (!strcasecmp(v->value, "dms100"))
10828 switchtype = PRI_SWITCH_DMS100;
10829 else if (!strcasecmp(v->value, "4ess"))
10830 switchtype = PRI_SWITCH_ATT4ESS;
10831 else if (!strcasecmp(v->value, "5ess"))
10832 switchtype = PRI_SWITCH_LUCENT5E;
10833 else if (!strcasecmp(v->value, "euroisdn"))
10834 switchtype = PRI_SWITCH_EUROISDN_E1;
10835 else if (!strcasecmp(v->value, "qsig"))
10836 switchtype = PRI_SWITCH_QSIG;
10837 else {
10838 ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value);
10839 ast_config_destroy(cfg);
10840 ast_mutex_unlock(&iflock);
10841 return -1;
10842 }
10843 } else if (!strcasecmp(v->name, "nsf")) {
10844 if (!strcasecmp(v->value, "sdn"))
10845 nsf = PRI_NSF_SDN;
10846 else if (!strcasecmp(v->value, "megacom"))
10847 nsf = PRI_NSF_MEGACOM;
10848 else if (!strcasecmp(v->value, "accunet"))
10849 nsf = PRI_NSF_ACCUNET;
10850 else if (!strcasecmp(v->value, "none"))
10851 nsf = PRI_NSF_NONE;
10852 else {
10853 ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value);
10854 nsf = PRI_NSF_NONE;
10855 }
10856 } else if (!strcasecmp(v->name, "priindication")) {
10857 if (!strcasecmp(v->value, "outofband"))
10858 priindication_oob = 1;
10859 else if (!strcasecmp(v->value, "inband"))
10860 priindication_oob = 0;
10861 else
10862 ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n",
10863 v->value, v->lineno);
10864 } else if (!strcasecmp(v->name, "priexclusive")) {
10865 cur_priexclusive = ast_true(v->value);
10866 } else if (!strcasecmp(v->name, "internationalprefix")) {
10867 ast_copy_string(internationalprefix, v->value, sizeof(internationalprefix));
10868 } else if (!strcasecmp(v->name, "nationalprefix")) {
10869 ast_copy_string(nationalprefix, v->value, sizeof(nationalprefix));
10870 } else if (!strcasecmp(v->name, "localprefix")) {
10871 ast_copy_string(localprefix, v->value, sizeof(localprefix));
10872 } else if (!strcasecmp(v->name, "privateprefix")) {
10873 ast_copy_string(privateprefix, v->value, sizeof(privateprefix));
10874 } else if (!strcasecmp(v->name, "unknownprefix")) {
10875 ast_copy_string(unknownprefix, v->value, sizeof(unknownprefix));
10876 } else if (!strcasecmp(v->name, "resetinterval")) {
10877 if (!strcasecmp(v->value, "never"))
10878 resetinterval = -1;
10879 else if( atoi(v->value) >= 60 )
10880 resetinterval = atoi(v->value);
10881 else
10882 ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d\n",
10883 v->value, v->lineno);
10884 } else if (!strcasecmp(v->name, "minunused")) {
10885 minunused = atoi(v->value);
10886 } else if (!strcasecmp(v->name, "idleext")) {
10887 ast_copy_string(idleext, v->value, sizeof(idleext));
10888 } else if (!strcasecmp(v->name, "idledial")) {
10889 ast_copy_string(idledial, v->value, sizeof(idledial));
10890 } else if (!strcasecmp(v->name, "overlapdial")) {
10891 overlapdial = ast_true(v->value);
10892 } else if (!strcasecmp(v->name, "pritimer")) {
10893 #ifdef PRI_GETSET_TIMERS
10894 char *timerc;
10895 int timer, timeridx;
10896 c = v->value;
10897 timerc = strsep(&c, ",");
10898 if (timerc) {
10899 timer = atoi(c);
10900 if (!timer)
10901 ast_log(LOG_WARNING, "'%s' is not a valid value for an ISDN timer\n", timerc);
10902 else {
10903 if ((timeridx = pri_timer2idx(timerc)) >= 0)
10904 pritimers[timeridx] = timer;
10905 else
10906 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer\n", timerc);
10907 }
10908 } else
10909 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer configuration string\n", v->value);
10910
10911 } else if (!strcasecmp(v->name, "facilityenable")) {
10912 facilityenable = ast_true(v->value);
10913 #endif
10914 #endif
10915 } else if (!strcasecmp(v->name, "cadence")) {
10916
10917 int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
10918 int i;
10919 struct zt_ring_cadence new_cadence;
10920 int cid_location = -1;
10921 int firstcadencepos = 0;
10922 char original_args[80];
10923 int cadence_is_ok = 1;
10924
10925 ast_copy_string(original_args, v->value, sizeof(original_args));
10926
10927 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]);
10928
10929
10930 if (element_count % 2 == 1) {
10931 ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args);
10932 cadence_is_ok = 0;
10933 }
10934
10935
10936 for (i=0;i<element_count;i++) {
10937 if (c[i] == 0) {
10938 ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s\n", original_args);
10939 cadence_is_ok = 0;
10940 break;
10941 } else if (c[i] < 0) {
10942 if (i % 2 == 1) {
10943
10944 if (cid_location == -1) {
10945 cid_location = i;
10946 c[i] *= -1;
10947 } else {
10948 ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args);
10949 cadence_is_ok = 0;
10950 break;
10951 }
10952 } else {
10953 if (firstcadencepos == 0) {
10954 firstcadencepos = i;
10955
10956 } else {
10957 ast_log(LOG_ERROR, "First cadence position specified twice: %s\n",original_args);
10958 cadence_is_ok = 0;
10959 break;
10960 }
10961 }
10962 }
10963 }
10964
10965
10966 for (i=0;i<16;i++) {
10967 new_cadence.ringcadence[i] = c[i];
10968 }
10969
10970 if (cadence_is_ok) {
10971
10972 if (element_count < 2) {
10973 ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args);
10974 } else {
10975 if (cid_location == -1) {
10976
10977 cid_location = 1;
10978 } else {
10979
10980 cid_location = (cid_location + 1) / 2;
10981 }
10982
10983 if (!user_has_defined_cadences++)
10984
10985 num_cadence = 0;
10986 if ((num_cadence+1) >= NUM_CADENCE_MAX)
10987 ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args);
10988 else {
10989 cadences[num_cadence] = new_cadence;
10990 cidrings[num_cadence++] = cid_location;
10991 if (option_verbose > 2)
10992 ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args);
10993 }
10994 }
10995 }
10996 } else if (!strcasecmp(v->name, "ringtimeout")) {
10997 ringt_base = (atoi(v->value) * 8) / READ_SIZE;
10998 } else if (!strcasecmp(v->name, "prewink")) {
10999 cur_prewink = atoi(v->value);
11000 } else if (!strcasecmp(v->name, "preflash")) {
11001 cur_preflash = atoi(v->value);
11002 } else if (!strcasecmp(v->name, "wink")) {
11003 cur_wink = atoi(v->value);
11004 } else if (!strcasecmp(v->name, "flash")) {
11005 cur_flash = atoi(v->value);
11006 } else if (!strcasecmp(v->name, "start")) {
11007 cur_start = atoi(v->value);
11008 } else if (!strcasecmp(v->name, "rxwink")) {
11009 cur_rxwink = atoi(v->value);
11010 } else if (!strcasecmp(v->name, "rxflash")) {
11011 cur_rxflash = atoi(v->value);
11012 } else if (!strcasecmp(v->name, "debounce")) {
11013 cur_debounce = atoi(v->value);
11014 } else if (!strcasecmp(v->name, "toneduration")) {
11015 int toneduration;
11016 int ctlfd;
11017 int res;
11018 struct zt_dialparams dps;
11019
11020 ctlfd = open("/dev/zap/ctl", O_RDWR);
11021 if (ctlfd == -1) {
11022 ast_log(LOG_ERROR, "Unable to open /dev/zap/ctl to set toneduration\n");
11023 return -1;
11024 }
11025
11026 toneduration = atoi(v->value);
11027 if (toneduration > -1) {
11028 dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration;
11029 res = ioctl(ctlfd, ZT_SET_DIALPARAMS, &dps);
11030 if (res < 0) {
11031 ast_log(LOG_ERROR, "Invalid tone duration: %d ms\n", toneduration);
11032 return -1;
11033 }
11034 }
11035 close(ctlfd);
11036 } else if (!strcasecmp(v->name, "polarityonanswerdelay")) {
11037 polarityonanswerdelay = atoi(v->value);
11038 } else if (!strcasecmp(v->name, "answeronpolarityswitch")) {
11039 answeronpolarityswitch = ast_true(v->value);
11040 } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) {
11041 hanguponpolarityswitch = ast_true(v->value);
11042 } else if (!strcasecmp(v->name, "sendcalleridafter")) {
11043 sendcalleridafter = atoi(v->value);
11044 } else if (!strcasecmp(v->name, "defaultcic")) {
11045 ast_copy_string(defaultcic, v->value, sizeof(defaultcic));
11046 } else if (!strcasecmp(v->name, "defaultozz")) {
11047 ast_copy_string(defaultozz, v->value, sizeof(defaultozz));
11048 }
11049 } else
11050 ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
11051 v = v->next;
11052 }
11053 if (!found_pseudo && reload == 0) {
11054
11055
11056
11057 cur_group = 0;
11058 cur_callergroup = 0;
11059 cur_pickupgroup = 0;
11060
11061 tmp = mkintf(CHAN_PSEUDO, cur_signalling, cur_radio, NULL, reload);
11062
11063 if (tmp) {
11064 if (option_verbose > 2)
11065 ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n");
11066 } else {
11067 ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
11068 }
11069 }
11070 ast_mutex_unlock(&iflock);
11071 ast_config_destroy(cfg);
11072 #ifdef ZAPATA_PRI
11073 if (!reload) {
11074 for (x=0;x<NUM_SPANS;x++) {
11075 if (pris[x].pvts[0]) {
11076 if (start_pri(pris + x)) {
11077 ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
11078 return -1;
11079 } else if (option_verbose > 1)
11080 ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1);
11081 }
11082 }
11083 }
11084 #endif
11085
11086 restart_monitor();
11087 return 0;
11088 }
11089
11090 int load_module(void)
11091 {
11092 int res;
11093
11094 #ifdef ZAPATA_PRI
11095 int y,i;
11096 memset(pris, 0, sizeof(pris));
11097 for (y=0;y<NUM_SPANS;y++) {
11098 ast_mutex_init(&pris[y].lock);
11099 pris[y].offset = -1;
11100 pris[y].master = AST_PTHREADT_NULL;
11101 for (i=0;i<NUM_DCHANS;i++)
11102 pris[y].fds[i] = -1;
11103 }
11104 pri_set_error(zt_pri_error);
11105 pri_set_message(zt_pri_message);
11106 #endif
11107 res = setup_zap(0);
11108
11109 if(res) {
11110 return -1;
11111 }
11112 if (ast_channel_register(&zap_tech)) {
11113 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
11114 __unload_module();
11115 return -1;
11116 }
11117 #ifdef ZAPATA_PRI
11118 ast_cli_register_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(zap_pri_cli[0]));
11119 #endif
11120 #ifdef ZAPATA_R2
11121 ast_cli_register_multiple(zap_r2_cli, sizeof(zap_r2_cli) / sizeof(zap_r2_cli[0]));
11122 #endif
11123 ast_cli_register_multiple(zap_cli, sizeof(zap_cli) / sizeof(zap_cli[0]));
11124
11125 memset(round_robin, 0, sizeof(round_robin));
11126 ast_manager_register( "ZapTransfer", 0, action_transfer, "Transfer Zap Channel" );
11127 ast_manager_register( "ZapHangup", 0, action_transferhangup, "Hangup Zap Channel" );
11128 ast_manager_register( "ZapDialOffhook", 0, action_zapdialoffhook, "Dial over Zap channel while offhook" );
11129 ast_manager_register( "ZapDNDon", 0, action_zapdndon, "Toggle Zap channel Do Not Disturb status ON" );
11130 ast_manager_register( "ZapDNDoff", 0, action_zapdndoff, "Toggle Zap channel Do Not Disturb status OFF" );
11131 ast_manager_register("ZapShowChannels", 0, action_zapshowchannels, "Show status zapata channels");
11132
11133 return res;
11134 }
11135
11136 static int zt_sendtext(struct ast_channel *c, const char *text)
11137 {
11138 #define END_SILENCE_LEN 400
11139 #define HEADER_MS 50
11140 #define TRAILER_MS 5
11141 #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
11142 #define ASCII_BYTES_PER_CHAR 80
11143
11144 unsigned char *buf,*mybuf;
11145 struct zt_pvt *p = c->tech_pvt;
11146 struct pollfd fds[1];
11147 int size,res,fd,len,x;
11148 int bytes=0;
11149
11150 float cr = 1.0;
11151 float ci = 0.0;
11152 float scont = 0.0;
11153 int index;
11154
11155 index = zt_get_index(c, p, 0);
11156 if (index < 0) {
11157 ast_log(LOG_WARNING, "Huh? I don't exist?\n");
11158 return -1;
11159 }
11160 if (!text[0]) return(0);
11161 if ((!p->tdd) && (!p->mate)) return(0);
11162 if (p->mate)
11163 buf = malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
11164 else
11165 buf = malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
11166 if (!buf) {
11167 ast_log(LOG_ERROR, "MALLOC FAILED\n");
11168 return -1;
11169 }
11170 mybuf = buf;
11171 if (p->mate) {
11172 int codec = AST_LAW(p);
11173 for (x=0;x<HEADER_MS;x++) {
11174 PUT_CLID_MARKMS;
11175 }
11176
11177 for (x=0;text[x];x++) {
11178 PUT_CLID(text[x]);
11179 }
11180 for (x=0;x<TRAILER_MS;x++) {
11181 PUT_CLID_MARKMS;
11182 }
11183 len = bytes;
11184 buf = mybuf;
11185 }
11186 else {
11187 len = tdd_generate(p->tdd,buf,text);
11188 if (len < 1) {
11189 ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n",(int)strlen(text));
11190 free(mybuf);
11191 return -1;
11192 }
11193 }
11194 memset(buf + len,0x7f,END_SILENCE_LEN);
11195 len += END_SILENCE_LEN;
11196 fd = p->subs[index].zfd;
11197 while(len) {
11198 if (ast_check_hangup(c)) {
11199 free(mybuf);
11200 return -1;
11201 }
11202 size = len;
11203 if (size > READ_SIZE)
11204 size = READ_SIZE;
11205 fds[0].fd = fd;
11206 fds[0].events = POLLOUT | POLLPRI;
11207 fds[0].revents = 0;
11208 res = poll(fds, 1, -1);
11209 if (!res) {
11210 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
11211 continue;
11212 }
11213
11214 if (fds[0].revents & POLLPRI) return -1;
11215 if (!(fds[0].revents & POLLOUT)) {
11216 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
11217 continue;
11218 }
11219 res = write(fd, buf, size);
11220 if (res != size) {
11221 if (res == -1) {
11222 free(mybuf);
11223 return -1;
11224 }
11225 if (option_debug)
11226 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
11227 break;
11228 }
11229 len -= size;
11230 buf += size;
11231 }
11232 free(mybuf);
11233 return(0);
11234 }
11235
11236
11237 int reload(void)
11238 {
11239 int res = 0;
11240
11241 res = setup_zap(1);
11242 if (res) {
11243 ast_log(LOG_WARNING, "Reload of chan_zap.so is unsuccessful!\n");
11244 return -1;
11245 }
11246 return 0;
11247 }
11248
11249 int usecount()
11250 {
11251 return usecnt;
11252 }
11253
11254 char *description()
11255 {
11256 return (char *) desc;
11257 }
11258
11259 char *key()
11260 {
11261 return ASTERISK_GPL_KEY;
11262 }
11263