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: 8905 $")
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 alloc_sub(struct zt_pvt *p, int x)
00958 {
00959 ZT_BUFFERINFO bi;
00960 int res;
00961 if (p->subs[x].zfd < 0) {
00962 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
00963 if (p->subs[x].zfd > -1) {
00964 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
00965 if (!res) {
00966 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
00967 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
00968 bi.numbufs = numbufs;
00969 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
00970 if (res < 0) {
00971 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
00972 }
00973 } else
00974 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
00975 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
00976 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
00977 zt_close(p->subs[x].zfd);
00978 p->subs[x].zfd = -1;
00979 return -1;
00980 }
00981 if (option_debug)
00982 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
00983 return 0;
00984 } else
00985 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
00986 return -1;
00987 }
00988 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
00989 return -1;
00990 }
00991
00992 static int unalloc_sub(struct zt_pvt *p, int x)
00993 {
00994 if (!x) {
00995 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
00996 return -1;
00997 }
00998 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
00999 if (p->subs[x].zfd > -1) {
01000 zt_close(p->subs[x].zfd);
01001 }
01002 p->subs[x].zfd = -1;
01003 p->subs[x].linear = 0;
01004 p->subs[x].chan = 0;
01005 p->subs[x].owner = NULL;
01006 p->subs[x].inthreeway = 0;
01007 p->polarity = POLARITY_IDLE;
01008 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
01009 return 0;
01010 }
01011
01012 static int zt_digit(struct ast_channel *ast, char digit)
01013 {
01014 ZT_DIAL_OPERATION zo;
01015 struct zt_pvt *p;
01016 int res = 0;
01017 int index;
01018 p = ast->tech_pvt;
01019 ast_mutex_lock(&p->lock);
01020 index = zt_get_index(ast, p, 0);
01021 if ((index == SUB_REAL) && p->owner) {
01022 #ifdef ZAPATA_PRI
01023 if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
01024 if (p->setup_ack) {
01025 if (!pri_grab(p, p->pri)) {
01026 pri_information(p->pri->pri,p->call,digit);
01027 pri_rel(p->pri);
01028 } else
01029 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
01030 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
01031 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
01032 res = strlen(p->dialdest);
01033 p->dialdest[res++] = digit;
01034 p->dialdest[res] = '\0';
01035 }
01036 } else {
01037 #else
01038 {
01039 #endif
01040 zo.op = ZT_DIAL_OP_APPEND;
01041 zo.dialstr[0] = 'T';
01042 zo.dialstr[1] = digit;
01043 zo.dialstr[2] = 0;
01044 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
01045 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
01046 else
01047 p->dialing = 1;
01048 }
01049 }
01050 ast_mutex_unlock(&p->lock);
01051 return res;
01052 }
01053
01054 static char *events[] = {
01055 "No event",
01056 "On hook",
01057 "Ring/Answered",
01058 "Wink/Flash",
01059 "Alarm",
01060 "No more alarm",
01061 "HDLC Abort",
01062 "HDLC Overrun",
01063 "HDLC Bad FCS",
01064 "Dial Complete",
01065 "Ringer On",
01066 "Ringer Off",
01067 "Hook Transition Complete",
01068 "Bits Changed",
01069 "Pulse Start",
01070 "Timer Expired",
01071 "Timer Ping",
01072 "Polarity Reversal",
01073 "Ring Begin",
01074 };
01075
01076 static struct {
01077 int alarm;
01078 char *name;
01079 } alarms[] = {
01080 { ZT_ALARM_RED, "Red Alarm" },
01081 { ZT_ALARM_YELLOW, "Yellow Alarm" },
01082 { ZT_ALARM_BLUE, "Blue Alarm" },
01083 { ZT_ALARM_RECOVER, "Recovering" },
01084 { ZT_ALARM_LOOPBACK, "Loopback" },
01085 { ZT_ALARM_NOTOPEN, "Not Open" },
01086 { ZT_ALARM_NONE, "None" },
01087 };
01088
01089 static char *alarm2str(int alarm)
01090 {
01091 int x;
01092 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
01093 if (alarms[x].alarm & alarm)
01094 return alarms[x].name;
01095 }
01096 return alarm ? "Unknown Alarm" : "No Alarm";
01097 }
01098
01099 static char *event2str(int event)
01100 {
01101 static char buf[256];
01102 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
01103 return events[event];
01104 sprintf(buf, "Event %d", event);
01105 return buf;
01106 }
01107
01108 #ifdef ZAPATA_PRI
01109 static char *dialplan2str(int dialplan)
01110 {
01111 if (dialplan == -1) {
01112 return("Dynamically set dialplan in ISDN");
01113 }
01114 return(pri_plan2str(dialplan));
01115 }
01116 #endif
01117
01118 #ifdef ZAPATA_R2
01119 static int str2r2prot(char *swtype)
01120 {
01121 if (!strcasecmp(swtype, "ar"))
01122 return MFCR2_PROT_ARGENTINA;
01123
01124 if (!strcasecmp(swtype, "cn"))
01125 return MFCR2_PROT_CHINA;
01126
01127 if (!strcasecmp(swtype, "kr"))
01128 return MFCR2_PROT_KOREA;
01129
01130 return -1;
01131 }
01132 #endif
01133
01134 static char *zap_sig2str(int sig)
01135 {
01136 static char buf[256];
01137 switch(sig) {
01138 case SIG_EM:
01139 return "E & M Immediate";
01140 case SIG_EMWINK:
01141 return "E & M Wink";
01142 case SIG_EM_E1:
01143 return "E & M E1";
01144 case SIG_FEATD:
01145 return "Feature Group D (DTMF)";
01146 case SIG_FEATDMF:
01147 return "Feature Group D (MF)";
01148 case SIG_FEATDMF_TA:
01149 return "Feature Groud D (MF) Tandem Access";
01150 case SIG_FEATB:
01151 return "Feature Group B (MF)";
01152 case SIG_E911:
01153 return "E911 (MF)";
01154 case SIG_FXSLS:
01155 return "FXS Loopstart";
01156 case SIG_FXSGS:
01157 return "FXS Groundstart";
01158 case SIG_FXSKS:
01159 return "FXS Kewlstart";
01160 case SIG_FXOLS:
01161 return "FXO Loopstart";
01162 case SIG_FXOGS:
01163 return "FXO Groundstart";
01164 case SIG_FXOKS:
01165 return "FXO Kewlstart";
01166 case SIG_PRI:
01167 return "PRI Signalling";
01168 case SIG_R2:
01169 return "R2 Signalling";
01170 case SIG_SF:
01171 return "SF (Tone) Signalling Immediate";
01172 case SIG_SFWINK:
01173 return "SF (Tone) Signalling Wink";
01174 case SIG_SF_FEATD:
01175 return "SF (Tone) Signalling with Feature Group D (DTMF)";
01176 case SIG_SF_FEATDMF:
01177 return "SF (Tone) Signalling with Feature Group D (MF)";
01178 case SIG_SF_FEATB:
01179 return "SF (Tone) Signalling with Feature Group B (MF)";
01180 case SIG_GR303FXOKS:
01181 return "GR-303 Signalling with FXOKS";
01182 case SIG_GR303FXSKS:
01183 return "GR-303 Signalling with FXSKS";
01184 case 0:
01185 return "Pseudo Signalling";
01186 default:
01187 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
01188 return buf;
01189 }
01190 }
01191
01192 #define sig2str zap_sig2str
01193
01194 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
01195 {
01196
01197
01198 ZT_CONFINFO zi;
01199
01200 memset(&zi, 0, sizeof(zi));
01201 zi.chan = 0;
01202
01203 if (slavechannel > 0) {
01204
01205 zi.confmode = ZT_CONF_DIGITALMON;
01206 zi.confno = slavechannel;
01207 } else {
01208 if (!index) {
01209
01210 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
01211 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
01212 } else
01213 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
01214 zi.confno = p->confno;
01215 }
01216 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
01217 return 0;
01218 if (c->zfd < 0)
01219 return 0;
01220 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01221 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
01222 return -1;
01223 }
01224 if (slavechannel < 1) {
01225 p->confno = zi.confno;
01226 }
01227 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01228 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01229 return 0;
01230 }
01231
01232 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
01233 {
01234
01235 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
01236 return 1;
01237
01238 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
01239 return 1;
01240 return 0;
01241 }
01242
01243 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
01244 {
01245 ZT_CONFINFO zi;
01246 if (
01247 (c->zfd < 0) ||
01248
01249 !isourconf(p, c)
01250
01251 ) return 0;
01252 memset(&zi, 0, sizeof(zi));
01253 zi.chan = 0;
01254 zi.confno = 0;
01255 zi.confmode = 0;
01256 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01257 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01258 return -1;
01259 }
01260 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01261 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01262 return 0;
01263 }
01264
01265 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
01266 {
01267 int x;
01268 int useslavenative;
01269 struct zt_pvt *slave = NULL;
01270
01271 useslavenative = 1;
01272
01273 for (x=0;x<3;x++) {
01274
01275
01276 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
01277 useslavenative = 0;
01278 }
01279
01280
01281 if (useslavenative) {
01282 for (x=0;x<MAX_SLAVES;x++) {
01283 if (p->slaves[x]) {
01284 if (slave) {
01285
01286
01287 slave = NULL;
01288 useslavenative = 0;
01289 break;
01290 } else {
01291
01292 slave = p->slaves[x];
01293 }
01294 }
01295 }
01296 }
01297
01298 if (!slave)
01299 useslavenative = 0;
01300 else if (slave->law != p->law) {
01301 useslavenative = 0;
01302 slave = NULL;
01303 }
01304 if (out)
01305 *out = slave;
01306 return useslavenative;
01307 }
01308
01309 static int reset_conf(struct zt_pvt *p)
01310 {
01311 ZT_CONFINFO zi;
01312 memset(&zi, 0, sizeof(zi));
01313 p->confno = -1;
01314 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
01315 if (p->subs[SUB_REAL].zfd > -1) {
01316 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
01317 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
01318 }
01319 return 0;
01320 }
01321
01322 static int update_conf(struct zt_pvt *p)
01323 {
01324 int needconf = 0;
01325 int x;
01326 int useslavenative;
01327 struct zt_pvt *slave = NULL;
01328
01329 useslavenative = isslavenative(p, &slave);
01330
01331 for (x=0;x<3;x++) {
01332
01333 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
01334 conf_add(p, &p->subs[x], x, 0);
01335 needconf++;
01336 } else {
01337 conf_del(p, &p->subs[x], x);
01338 }
01339 }
01340
01341
01342 for (x=0;x<MAX_SLAVES;x++) {
01343 if (p->slaves[x]) {
01344 if (useslavenative)
01345 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
01346 else {
01347 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
01348 needconf++;
01349 }
01350 }
01351 }
01352
01353 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
01354 if (useslavenative)
01355 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
01356 else {
01357 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
01358 needconf++;
01359 }
01360 }
01361
01362 if (p->master) {
01363 if (isslavenative(p->master, NULL)) {
01364 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
01365 } else {
01366 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
01367 }
01368 }
01369 if (!needconf) {
01370
01371
01372 p->confno = -1;
01373 }
01374 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
01375 return 0;
01376 }
01377
01378 static void zt_enable_ec(struct zt_pvt *p)
01379 {
01380 int x;
01381 int res;
01382 if (!p)
01383 return;
01384 if (p->echocanon) {
01385 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
01386 return;
01387 }
01388 if (p->digital) {
01389 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
01390 return;
01391 }
01392 if (p->echocancel) {
01393 if (p->sig == SIG_PRI) {
01394 x = 1;
01395 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
01396 if (res)
01397 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01398 }
01399 x = p->echocancel;
01400 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01401 if (res)
01402 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01403 else {
01404 p->echocanon = 1;
01405 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
01406 }
01407 } else
01408 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
01409 }
01410
01411 static void zt_train_ec(struct zt_pvt *p)
01412 {
01413 int x;
01414 int res;
01415 if (p && p->echocancel && p->echotraining) {
01416 x = p->echotraining;
01417 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
01418 if (res)
01419 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
01420 else {
01421 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
01422 }
01423 } else
01424 ast_log(LOG_DEBUG, "No echo training requested\n");
01425 }
01426
01427 static void zt_disable_ec(struct zt_pvt *p)
01428 {
01429 int x;
01430 int res;
01431 if (p->echocancel) {
01432 x = 0;
01433 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01434 if (res)
01435 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
01436 else
01437 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
01438 }
01439 p->echocanon = 0;
01440 }
01441
01442 static void fill_txgain(struct zt_gains *g, float gain, int law)
01443 {
01444 int j;
01445 int k;
01446 float linear_gain = pow(10.0, gain / 20.0);
01447
01448 switch (law) {
01449 case ZT_LAW_ALAW:
01450 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01451 if (gain) {
01452 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01453 if (k > 32767) k = 32767;
01454 if (k < -32767) k = -32767;
01455 g->txgain[j] = AST_LIN2A(k);
01456 } else {
01457 g->txgain[j] = j;
01458 }
01459 }
01460 break;
01461 case ZT_LAW_MULAW:
01462 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01463 if (gain) {
01464 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01465 if (k > 32767) k = 32767;
01466 if (k < -32767) k = -32767;
01467 g->txgain[j] = AST_LIN2MU(k);
01468 } else {
01469 g->txgain[j] = j;
01470 }
01471 }
01472 break;
01473 }
01474 }
01475
01476 static void fill_rxgain(struct zt_gains *g, float gain, int law)
01477 {
01478 int j;
01479 int k;
01480 float linear_gain = pow(10.0, gain / 20.0);
01481
01482 switch (law) {
01483 case ZT_LAW_ALAW:
01484 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01485 if (gain) {
01486 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01487 if (k > 32767) k = 32767;
01488 if (k < -32767) k = -32767;
01489 g->rxgain[j] = AST_LIN2A(k);
01490 } else {
01491 g->rxgain[j] = j;
01492 }
01493 }
01494 break;
01495 case ZT_LAW_MULAW:
01496 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01497 if (gain) {
01498 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01499 if (k > 32767) k = 32767;
01500 if (k < -32767) k = -32767;
01501 g->rxgain[j] = AST_LIN2MU(k);
01502 } else {
01503 g->rxgain[j] = j;
01504 }
01505 }
01506 break;
01507 }
01508 }
01509
01510 int set_actual_txgain(int fd, int chan, float gain, int law)
01511 {
01512 struct zt_gains g;
01513 int res;
01514
01515 memset(&g, 0, sizeof(g));
01516 g.chan = chan;
01517 res = ioctl(fd, ZT_GETGAINS, &g);
01518 if (res) {
01519 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01520 return res;
01521 }
01522
01523 fill_txgain(&g, gain, law);
01524
01525 return ioctl(fd, ZT_SETGAINS, &g);
01526 }
01527
01528 int set_actual_rxgain(int fd, int chan, float gain, int law)
01529 {
01530 struct zt_gains g;
01531 int res;
01532
01533 memset(&g, 0, sizeof(g));
01534 g.chan = chan;
01535 res = ioctl(fd, ZT_GETGAINS, &g);
01536 if (res) {
01537 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01538 return res;
01539 }
01540
01541 fill_rxgain(&g, gain, law);
01542
01543 return ioctl(fd, ZT_SETGAINS, &g);
01544 }
01545
01546 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
01547 {
01548 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
01549 }
01550
01551 static int bump_gains(struct zt_pvt *p)
01552 {
01553 int res;
01554
01555
01556 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
01557 if (res) {
01558 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
01559 return -1;
01560 }
01561
01562 return 0;
01563 }
01564
01565 static int restore_gains(struct zt_pvt *p)
01566 {
01567 int res;
01568
01569 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01570 if (res) {
01571 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
01572 return -1;
01573 }
01574
01575 return 0;
01576 }
01577
01578 static inline int zt_set_hook(int fd, int hs)
01579 {
01580 int x, res;
01581 x = hs;
01582 res = ioctl(fd, ZT_HOOK, &x);
01583 if (res < 0)
01584 {
01585 if (errno == EINPROGRESS) return 0;
01586 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
01587 }
01588 return res;
01589 }
01590
01591 static inline int zt_confmute(struct zt_pvt *p, int muted)
01592 {
01593 int x, y, res;
01594 x = muted;
01595 if (p->sig == SIG_PRI) {
01596 y = 1;
01597 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
01598 if (res)
01599 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
01600 }
01601 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
01602 if (res < 0)
01603 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
01604 return res;
01605 }
01606
01607 static int save_conference(struct zt_pvt *p)
01608 {
01609 struct zt_confinfo c;
01610 int res;
01611 if (p->saveconf.confmode) {
01612 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
01613 return -1;
01614 }
01615 p->saveconf.chan = 0;
01616 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
01617 if (res) {
01618 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
01619 p->saveconf.confmode = 0;
01620 return -1;
01621 }
01622 c.chan = 0;
01623 c.confno = 0;
01624 c.confmode = ZT_CONF_NORMAL;
01625 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
01626 if (res) {
01627 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
01628 return -1;
01629 }
01630 if (option_debug)
01631 ast_log(LOG_DEBUG, "Disabled conferencing\n");
01632 return 0;
01633 }
01634
01635 static int restore_conference(struct zt_pvt *p)
01636 {
01637 int res;
01638 if (p->saveconf.confmode) {
01639 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
01640 p->saveconf.confmode = 0;
01641 if (res) {
01642 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
01643 return -1;
01644 }
01645 }
01646 if (option_debug)
01647 ast_log(LOG_DEBUG, "Restored conferencing\n");
01648 return 0;
01649 }
01650
01651 static int send_callerid(struct zt_pvt *p);
01652
01653 int send_cwcidspill(struct zt_pvt *p)
01654 {
01655 p->callwaitcas = 0;
01656 p->cidcwexpire = 0;
01657 p->cidspill = malloc(MAX_CALLERID_SIZE);
01658 if (p->cidspill) {
01659 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
01660 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
01661
01662 p->cidlen += READ_SIZE * 4;
01663 p->cidpos = 0;
01664 send_callerid(p);
01665 if (option_verbose > 2)
01666 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
01667 } else return -1;
01668 return 0;
01669 }
01670
01671 static int has_voicemail(struct zt_pvt *p)
01672 {
01673
01674 return ast_app_has_voicemail(p->mailbox, NULL);
01675 }
01676
01677 static int send_callerid(struct zt_pvt *p)
01678 {
01679
01680 int res;
01681
01682 if (p->subs[SUB_REAL].linear) {
01683 p->subs[SUB_REAL].linear = 0;
01684 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
01685 }
01686 while(p->cidpos < p->cidlen) {
01687 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
01688 if (res < 0) {
01689 if (errno == EAGAIN)
01690 return 0;
01691 else {
01692 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
01693 return -1;
01694 }
01695 }
01696 if (!res)
01697 return 0;
01698 p->cidpos += res;
01699 }
01700 free(p->cidspill);
01701 p->cidspill = NULL;
01702 if (p->callwaitcas) {
01703
01704 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
01705 } else
01706 restore_conference(p);
01707 return 0;
01708 }
01709
01710 static int zt_callwait(struct ast_channel *ast)
01711 {
01712 struct zt_pvt *p = ast->tech_pvt;
01713 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
01714 if (p->cidspill) {
01715 ast_log(LOG_WARNING, "Spill already exists?!?\n");
01716 free(p->cidspill);
01717 }
01718 p->cidspill = malloc(2400 + 680 + READ_SIZE * 4);
01719 if (p->cidspill) {
01720 save_conference(p);
01721
01722 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
01723 if (!p->callwaitrings && p->callwaitingcallerid) {
01724 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
01725 p->callwaitcas = 1;
01726 p->cidlen = 2400 + 680 + READ_SIZE * 4;
01727 } else {
01728 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
01729 p->callwaitcas = 0;
01730 p->cidlen = 2400 + READ_SIZE * 4;
01731 }
01732 p->cidpos = 0;
01733 send_callerid(p);
01734 } else {
01735 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
01736 return -1;
01737 }
01738 return 0;
01739 }
01740
01741 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
01742 {
01743 struct zt_pvt *p = ast->tech_pvt;
01744 int x, res, index;
01745 char *c, *n, *l;
01746 #ifdef ZAPATA_PRI
01747 char *s=NULL;
01748 #endif
01749 char dest[256];
01750 ast_mutex_lock(&p->lock);
01751 ast_copy_string(dest, rdest, sizeof(dest));
01752 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
01753 if ((ast->_state == AST_STATE_BUSY)) {
01754 p->subs[SUB_REAL].needbusy = 1;
01755 ast_mutex_unlock(&p->lock);
01756 return 0;
01757 }
01758 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
01759 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
01760 ast_mutex_unlock(&p->lock);
01761 return -1;
01762 }
01763 p->dialednone = 0;
01764 if (p->radio)
01765 {
01766
01767 ast_setstate(ast, AST_STATE_UP);
01768 ast_mutex_unlock(&p->lock);
01769 return 0;
01770 }
01771 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
01772 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
01773 if (res)
01774 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
01775 p->outgoing = 1;
01776
01777 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01778
01779 switch(p->sig) {
01780 case SIG_FXOLS:
01781 case SIG_FXOGS:
01782 case SIG_FXOKS:
01783 if (p->owner == ast) {
01784
01785
01786
01787 p->dialing = 1;
01788 if (p->use_callerid) {
01789
01790 if (p->cidspill) {
01791 ast_log(LOG_WARNING, "cidspill already exists??\n");
01792 free(p->cidspill);
01793 }
01794 p->cidspill = malloc(MAX_CALLERID_SIZE);
01795 p->callwaitcas = 0;
01796 if (p->cidspill) {
01797 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
01798 p->cidpos = 0;
01799 send_callerid(p);
01800 } else
01801 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
01802 }
01803
01804 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
01805 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
01806 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
01807 p->cidrings = cidrings[p->distinctivering - 1];
01808 } else {
01809 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
01810 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
01811 p->cidrings = p->sendcalleridafter;
01812 }
01813
01814
01815
01816 c = strchr(dest, '/');
01817 if (c)
01818 c++;
01819 if (c && (strlen(c) < p->stripmsd)) {
01820 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01821 c = NULL;
01822 }
01823 if (c) {
01824 p->dop.op = ZT_DIAL_OP_REPLACE;
01825 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
01826 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
01827 } else {
01828 p->dop.dialstr[0] = '\0';
01829 }
01830 x = ZT_RING;
01831 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
01832 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
01833 ast_mutex_unlock(&p->lock);
01834 return -1;
01835 }
01836 p->dialing = 1;
01837 } else {
01838
01839 p->callwaitrings = 0;
01840 if (ast->cid.cid_num)
01841 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
01842 else
01843 p->callwait_num[0] = '\0';
01844 if (ast->cid.cid_name)
01845 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
01846 else
01847 p->callwait_name[0] = '\0';
01848
01849 if (zt_callwait(ast)) {
01850 ast_mutex_unlock(&p->lock);
01851 return -1;
01852 }
01853
01854 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
01855 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
01856
01857 }
01858 n = ast->cid.cid_name;
01859 l = ast->cid.cid_num;
01860 if (l)
01861 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
01862 else
01863 p->lastcid_num[0] = '\0';
01864 if (n)
01865 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
01866 else
01867 p->lastcid_name[0] = '\0';
01868 ast_setstate(ast, AST_STATE_RINGING);
01869 index = zt_get_index(ast, p, 0);
01870 if (index > -1) {
01871 p->subs[index].needringing = 1;
01872 }
01873 break;
01874 case SIG_FXSLS:
01875 case SIG_FXSGS:
01876 case SIG_FXSKS:
01877 case SIG_EMWINK:
01878 case SIG_EM:
01879 case SIG_EM_E1:
01880 case SIG_FEATD:
01881 case SIG_FEATDMF:
01882 case SIG_E911:
01883 case SIG_FEATB:
01884 case SIG_SFWINK:
01885 case SIG_SF:
01886 case SIG_SF_FEATD:
01887 case SIG_SF_FEATDMF:
01888 case SIG_FEATDMF_TA:
01889 case SIG_SF_FEATB:
01890 c = strchr(dest, '/');
01891 if (c)
01892 c++;
01893 else
01894 c = "";
01895 if (strlen(c) < p->stripmsd) {
01896 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01897 ast_mutex_unlock(&p->lock);
01898 return -1;
01899 }
01900 #ifdef ZAPATA_PRI
01901
01902 if (!p->pri) {
01903 #endif
01904 x = ZT_START;
01905 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01906 if (res < 0) {
01907 if (errno != EINPROGRESS) {
01908 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
01909 ast_mutex_unlock(&p->lock);
01910 return -1;
01911 }
01912 }
01913 #ifdef ZAPATA_PRI
01914 }
01915 #endif
01916 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
01917 p->dop.op = ZT_DIAL_OP_REPLACE;
01918
01919 c += p->stripmsd;
01920
01921 switch (p->sig) {
01922 case SIG_FEATD:
01923 l = ast->cid.cid_num;
01924 if (l)
01925 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
01926 else
01927 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
01928 break;
01929 case SIG_FEATDMF:
01930 l = ast->cid.cid_num;
01931 if (l)
01932 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
01933 else
01934 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
01935 break;
01936 case SIG_FEATDMF_TA:
01937 {
01938 char *cic = NULL, *ozz = NULL;
01939
01940
01941 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
01942 if (!ozz)
01943 ozz = defaultozz;
01944 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
01945 if (!cic)
01946 cic = defaultcic;
01947 if (!ozz || !cic) {
01948 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
01949 ast_mutex_unlock(&p->lock);
01950 return -1;
01951 }
01952 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
01953 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
01954 p->whichwink = 0;
01955 }
01956 break;
01957 case SIG_E911:
01958 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
01959 break;
01960 case SIG_FEATB:
01961 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
01962 break;
01963 default:
01964 if (p->pulse)
01965 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
01966 else
01967 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
01968 break;
01969 }
01970
01971 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
01972 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
01973 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
01974 p->echorest[sizeof(p->echorest) - 1] = '\0';
01975 p->echobreak = 1;
01976 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
01977 } else
01978 p->echobreak = 0;
01979 if (!res) {
01980 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
01981 x = ZT_ONHOOK;
01982 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01983 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
01984 ast_mutex_unlock(&p->lock);
01985 return -1;
01986 }
01987 } else
01988 ast_log(LOG_DEBUG, "Deferring dialing...\n");
01989 p->dialing = 1;
01990 if (ast_strlen_zero(c))
01991 p->dialednone = 1;
01992 ast_setstate(ast, AST_STATE_DIALING);
01993 break;
01994 case 0:
01995
01996 ast_setstate(ast, AST_STATE_UP);
01997 break;
01998 case SIG_PRI:
01999
02000 p->dialdest[0] = '\0';
02001 break;
02002 default:
02003 ast_log(LOG_DEBUG, "not yet implemented\n");
02004 ast_mutex_unlock(&p->lock);
02005 return -1;
02006 }
02007 #ifdef ZAPATA_PRI
02008 if (p->pri) {
02009 struct pri_sr *sr;
02010 #ifdef SUPPORT_USERUSER
02011 char *useruser;
02012 #endif
02013 int pridialplan;
02014 int dp_strip;
02015 int prilocaldialplan;
02016 int ldp_strip;
02017 int exclusive;
02018
02019 c = strchr(dest, '/');
02020 if (c)
02021 c++;
02022 else
02023 c = dest;
02024 if (!p->hidecallerid) {
02025 l = ast->cid.cid_num;
02026 n = ast->cid.cid_name;
02027 } else {
02028 l = NULL;
02029 n = NULL;
02030 }
02031 if (strlen(c) < p->stripmsd) {
02032 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
02033 ast_mutex_unlock(&p->lock);
02034 return -1;
02035 }
02036 if (p->sig != SIG_FXSKS) {
02037 p->dop.op = ZT_DIAL_OP_REPLACE;
02038 s = strchr(c + p->stripmsd, 'w');
02039 if (s) {
02040 if (strlen(s) > 1)
02041 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
02042 else
02043 p->dop.dialstr[0] = '\0';
02044 *s = '\0';
02045 } else {
02046 p->dop.dialstr[0] = '\0';
02047 }
02048 }
02049 if (pri_grab(p, p->pri)) {
02050 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
02051 ast_mutex_unlock(&p->lock);
02052 return -1;
02053 }
02054 if (!(p->call = pri_new_call(p->pri->pri))) {
02055 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
02056 pri_rel(p->pri);
02057 ast_mutex_unlock(&p->lock);
02058 return -1;
02059 }
02060 if (!(sr = pri_sr_new())) {
02061 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
02062 pri_rel(p->pri);
02063 ast_mutex_unlock(&p->lock);
02064 }
02065 if (p->bearer || (p->sig == SIG_FXSKS)) {
02066 if (p->bearer) {
02067 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);
02068 p->bearer->call = p->call;
02069 } else
02070 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
02071 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
02072 }
02073 p->digital = IS_DIGITAL(ast->transfercapability);
02074
02075 if (p->priexclusive)
02076 exclusive = 1;
02077 else {
02078
02079 if (p->pri->nodetype == PRI_NETWORK)
02080 exclusive = 0;
02081 else
02082 exclusive = 1;
02083 }
02084
02085 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
02086 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
02087 (p->digital ? -1 :
02088 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
02089 if (p->pri->facilityenable)
02090 pri_facility_enable(p->pri->pri);
02091
02092 if (option_verbose > 2)
02093 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
02094 dp_strip = 0;
02095 pridialplan = p->pri->dialplan - 1;
02096 if (pridialplan == -2) {
02097 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02098 dp_strip = strlen(p->pri->internationalprefix);
02099 pridialplan = PRI_INTERNATIONAL_ISDN;
02100 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02101 dp_strip = strlen(p->pri->nationalprefix);
02102 pridialplan = PRI_NATIONAL_ISDN;
02103 } else {
02104 pridialplan = PRI_LOCAL_ISDN;
02105 }
02106 }
02107 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
02108
02109 ldp_strip = 0;
02110 prilocaldialplan = p->pri->localdialplan - 1;
02111 if ((l != NULL) && (prilocaldialplan == -2)) {
02112 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02113 ldp_strip = strlen(p->pri->internationalprefix);
02114 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
02115 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02116 ldp_strip = strlen(p->pri->nationalprefix);
02117 prilocaldialplan = PRI_NATIONAL_ISDN;
02118 } else {
02119 prilocaldialplan = PRI_LOCAL_ISDN;
02120 }
02121 }
02122 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
02123 l ? (p->use_callingpres ? ast->cid.cid_pres : PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN) :
02124 PRES_NUMBER_NOT_AVAILABLE);
02125 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
02126
02127 #ifdef SUPPORT_USERUSER
02128
02129 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
02130
02131 if (useruser)
02132 pri_sr_set_useruser(sr, useruser);
02133 #endif
02134
02135 if (pri_setup(p->pri->pri, p->call, sr)) {
02136 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
02137 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
02138 pri_rel(p->pri);
02139 ast_mutex_unlock(&p->lock);
02140 pri_sr_free(sr);
02141 return -1;
02142 }
02143 pri_sr_free(sr);
02144 ast_setstate(ast, AST_STATE_DIALING);
02145 pri_rel(p->pri);
02146 }
02147 #endif
02148 ast_mutex_unlock(&p->lock);
02149 return 0;
02150 }
02151
02152 static void destroy_zt_pvt(struct zt_pvt **pvt)
02153 {
02154 struct zt_pvt *p = *pvt;
02155
02156 if(p->prev)
02157 p->prev->next = p->next;
02158 if(p->next)
02159 p->next->prev = p->prev;
02160 ast_mutex_destroy(&p->lock);
02161 free(p);
02162 *pvt = NULL;
02163 }
02164
02165 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
02166 {
02167 int owned = 0;
02168 int i = 0;
02169
02170 if (!now) {
02171 if (cur->owner) {
02172 owned = 1;
02173 }
02174
02175 for (i = 0; i < 3; i++) {
02176 if (cur->subs[i].owner) {
02177 owned = 1;
02178 }
02179 }
02180 if (!owned) {
02181 if (prev) {
02182 prev->next = cur->next;
02183 if (prev->next)
02184 prev->next->prev = prev;
02185 else
02186 ifend = prev;
02187 } else {
02188 iflist = cur->next;
02189 if (iflist)
02190 iflist->prev = NULL;
02191 else
02192 ifend = NULL;
02193 }
02194 if (cur->subs[SUB_REAL].zfd > -1) {
02195 zt_close(cur->subs[SUB_REAL].zfd);
02196 }
02197 destroy_zt_pvt(&cur);
02198 }
02199 } else {
02200 if (prev) {
02201 prev->next = cur->next;
02202 if (prev->next)
02203 prev->next->prev = prev;
02204 else
02205 ifend = prev;
02206 } else {
02207 iflist = cur->next;
02208 if (iflist)
02209 iflist->prev = NULL;
02210 else
02211 ifend = NULL;
02212 }
02213 if (cur->subs[SUB_REAL].zfd > -1) {
02214 zt_close(cur->subs[SUB_REAL].zfd);
02215 }
02216 destroy_zt_pvt(&cur);
02217 }
02218 return 0;
02219 }
02220
02221 #ifdef ZAPATA_PRI
02222 int pri_is_up(struct zt_pri *pri)
02223 {
02224 int x;
02225 for (x=0;x<NUM_DCHANS;x++) {
02226 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
02227 return 1;
02228 }
02229 return 0;
02230 }
02231
02232 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
02233 {
02234 bearer->owner = &inuse;
02235 bearer->realcall = crv;
02236 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
02237 if (crv->subs[SUB_REAL].owner)
02238 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
02239 crv->bearer = bearer;
02240 crv->call = bearer->call;
02241 crv->pri = pri;
02242 return 0;
02243 }
02244
02245 static char *pri_order(int level)
02246 {
02247 switch(level) {
02248 case 0:
02249 return "Primary";
02250 case 1:
02251 return "Secondary";
02252 case 2:
02253 return "Tertiary";
02254 case 3:
02255 return "Quaternary";
02256 default:
02257 return "<Unknown>";
02258 }
02259 }
02260
02261
02262 int pri_active_dchan_fd(struct zt_pri *pri)
02263 {
02264 int x = -1;
02265
02266 for (x = 0; x < NUM_DCHANS; x++) {
02267 if ((pri->dchans[x] == pri->pri))
02268 break;
02269 }
02270
02271 return pri->fds[x];
02272 }
02273
02274 int pri_find_dchan(struct zt_pri *pri)
02275 {
02276 int oldslot = -1;
02277 struct pri *old;
02278 int newslot = -1;
02279 int x;
02280 old = pri->pri;
02281 for(x=0;x<NUM_DCHANS;x++) {
02282 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
02283 newslot = x;
02284 if (pri->dchans[x] == old) {
02285 oldslot = x;
02286 }
02287 }
02288 if (newslot < 0) {
02289 newslot = 0;
02290 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
02291 pri->dchannels[newslot]);
02292 }
02293 if (old && (oldslot != newslot))
02294 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
02295 pri->dchannels[oldslot], pri->dchannels[newslot]);
02296 pri->pri = pri->dchans[newslot];
02297 return 0;
02298 }
02299 #endif
02300
02301 static int zt_hangup(struct ast_channel *ast)
02302 {
02303 int res;
02304 int index,x, law;
02305
02306 struct zt_pvt *p = ast->tech_pvt;
02307 struct zt_pvt *tmp = NULL;
02308 struct zt_pvt *prev = NULL;
02309 ZT_PARAMS par;
02310
02311 if (option_debug)
02312 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
02313 if (!ast->tech_pvt) {
02314 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
02315 return 0;
02316 }
02317
02318 ast_mutex_lock(&p->lock);
02319
02320 index = zt_get_index(ast, p, 1);
02321
02322 if (p->sig == SIG_PRI) {
02323 x = 1;
02324 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02325 }
02326
02327 x = 0;
02328 zt_confmute(p, 0);
02329 restore_gains(p);
02330 if (p->origcid_num) {
02331 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
02332 free(p->origcid_num);
02333 p->origcid_num = NULL;
02334 }
02335 if (p->origcid_name) {
02336 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
02337 free(p->origcid_name);
02338 p->origcid_name = NULL;
02339 }
02340 if (p->dsp)
02341 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02342 if (p->exten)
02343 p->exten[0] = '\0';
02344
02345 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
02346 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
02347 p->ignoredtmf = 0;
02348
02349 if (index > -1) {
02350
02351 p->subs[index].owner = NULL;
02352 p->subs[index].needanswer = 0;
02353 p->subs[index].needflash = 0;
02354 p->subs[index].needringing = 0;
02355 p->subs[index].needbusy = 0;
02356 p->subs[index].needcongestion = 0;
02357 p->subs[index].linear = 0;
02358 p->subs[index].needcallerid = 0;
02359 p->polarity = POLARITY_IDLE;
02360 zt_setlinear(p->subs[index].zfd, 0);
02361 if (index == SUB_REAL) {
02362 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
02363 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
02364 if (p->subs[SUB_CALLWAIT].inthreeway) {
02365
02366 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
02367
02368 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02369 unalloc_sub(p, SUB_CALLWAIT);
02370 p->owner = NULL;
02371 } else {
02372
02373 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
02374 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02375 unalloc_sub(p, SUB_THREEWAY);
02376 if (p->subs[SUB_REAL].inthreeway) {
02377
02378
02379 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02380 p->owner = p->subs[SUB_REAL].owner;
02381 } else {
02382
02383 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02384 p->owner = NULL;
02385 }
02386 p->subs[SUB_REAL].inthreeway = 0;
02387 }
02388 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
02389
02390 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02391 unalloc_sub(p, SUB_CALLWAIT);
02392 p->owner = p->subs[SUB_REAL].owner;
02393 if (p->owner->_state != AST_STATE_UP)
02394 p->subs[SUB_REAL].needanswer = 1;
02395 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
02396 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
02397 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
02398 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02399 unalloc_sub(p, SUB_THREEWAY);
02400 if (p->subs[SUB_REAL].inthreeway) {
02401
02402
02403 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02404 p->owner = p->subs[SUB_REAL].owner;
02405 } else {
02406
02407 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02408 p->owner = NULL;
02409 }
02410 p->subs[SUB_REAL].inthreeway = 0;
02411 }
02412 } else if (index == SUB_CALLWAIT) {
02413
02414 if (p->subs[SUB_CALLWAIT].inthreeway) {
02415
02416
02417 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
02418 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
02419 p->subs[SUB_THREEWAY].inthreeway = 0;
02420
02421 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
02422 unalloc_sub(p, SUB_THREEWAY);
02423 } else
02424 unalloc_sub(p, SUB_CALLWAIT);
02425 } else if (index == SUB_THREEWAY) {
02426 if (p->subs[SUB_CALLWAIT].inthreeway) {
02427
02428
02429 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
02430 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
02431 p->subs[SUB_CALLWAIT].inthreeway = 0;
02432 }
02433 p->subs[SUB_REAL].inthreeway = 0;
02434
02435
02436 unalloc_sub(p, SUB_THREEWAY);
02437 } else {
02438
02439 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
02440 }
02441 }
02442
02443
02444 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
02445 p->owner = NULL;
02446 p->ringt = 0;
02447 p->distinctivering = 0;
02448 p->confirmanswer = 0;
02449 p->cidrings = 1;
02450 p->outgoing = 0;
02451 p->digital = 0;
02452 p->faxhandled = 0;
02453 p->pulsedial = 0;
02454 p->onhooktime = time(NULL);
02455 #ifdef ZAPATA_PRI
02456 p->proceeding = 0;
02457 p->progress = 0;
02458 p->alerting = 0;
02459 p->setup_ack = 0;
02460 #endif
02461 if (p->dsp) {
02462 ast_dsp_free(p->dsp);
02463 p->dsp = NULL;
02464 }
02465
02466 law = ZT_LAW_DEFAULT;
02467 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
02468 if (res < 0)
02469 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
02470
02471 #ifdef ZAPATA_PRI
02472 if (p->pri) {
02473 #ifdef SUPPORT_USERUSER
02474 char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
02475 #endif
02476
02477
02478 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
02479 if (!pri_grab(p, p->pri)) {
02480 if (p->alreadyhungup) {
02481 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
02482
02483 #ifdef SUPPORT_USERUSER
02484 pri_call_set_useruser(p->call, useruser);
02485 #endif
02486
02487 pri_hangup(p->pri->pri, p->call, -1);
02488 p->call = NULL;
02489 if (p->bearer)
02490 p->bearer->call = NULL;
02491 } else {
02492 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
02493 int icause = ast->hangupcause ? ast->hangupcause : -1;
02494 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
02495
02496 #ifdef SUPPORT_USERUSER
02497 pri_call_set_useruser(p->call, useruser);
02498 #endif
02499
02500 p->alreadyhungup = 1;
02501 if (p->bearer)
02502 p->bearer->alreadyhungup = 1;
02503 if (cause) {
02504 if (atoi(cause))
02505 icause = atoi(cause);
02506 }
02507 pri_hangup(p->pri->pri, p->call, icause);
02508 }
02509 if (res < 0)
02510 ast_log(LOG_WARNING, "pri_disconnect failed\n");
02511 pri_rel(p->pri);
02512 } else {
02513 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02514 res = -1;
02515 }
02516 } else {
02517 if (p->bearer)
02518 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
02519 p->call = NULL;
02520 res = 0;
02521 }
02522 }
02523 #endif
02524 #ifdef ZAPATA_R2
02525 if (p->sig == SIG_R2) {
02526 if (p->hasr2call) {
02527 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
02528 p->hasr2call = 0;
02529 res = 0;
02530 } else
02531 res = 0;
02532
02533 }
02534 #endif
02535 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
02536 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
02537 if (res < 0) {
02538 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
02539 }
02540 switch(p->sig) {
02541 case SIG_FXOGS:
02542 case SIG_FXOLS:
02543 case SIG_FXOKS:
02544 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
02545 if (!res) {
02546 #if 0
02547 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
02548 #endif
02549
02550 if ((par.rxisoffhook) && (!p->radio))
02551 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
02552 else
02553 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02554 }
02555 break;
02556 case SIG_FXSGS:
02557 case SIG_FXSLS:
02558 case SIG_FXSKS:
02559
02560
02561 if (ast->_state != AST_STATE_RESERVED) {
02562 time(&p->guardtime);
02563 p->guardtime += 2;
02564 }
02565 break;
02566 default:
02567 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02568 }
02569 if (p->cidspill)
02570 free(p->cidspill);
02571 if (p->sig)
02572 zt_disable_ec(p);
02573 x = 0;
02574 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
02575 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
02576 p->didtdd = 0;
02577 p->cidspill = NULL;
02578 p->callwaitcas = 0;
02579 p->callwaiting = p->permcallwaiting;
02580 p->hidecallerid = p->permhidecallerid;
02581 p->dialing = 0;
02582 p->rdnis[0] = '\0';
02583 update_conf(p);
02584 reset_conf(p);
02585
02586 if (p->sig == SIG_PRI) {
02587 x = 0;
02588 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02589 }
02590 #ifdef ZAPATA_PRI
02591 if (p->bearer) {
02592 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
02593
02594
02595 update_conf(p->bearer);
02596 reset_conf(p->bearer);
02597 p->bearer->owner = NULL;
02598 p->bearer->realcall = NULL;
02599 p->bearer = NULL;
02600 p->subs[SUB_REAL].zfd = -1;
02601 p->pri = NULL;
02602 }
02603 #endif
02604 restart_monitor();
02605 }
02606
02607
02608 p->callwaitingrepeat = 0;
02609 p->cidcwexpire = 0;
02610 ast->tech_pvt = NULL;
02611 ast_mutex_unlock(&p->lock);
02612 ast_mutex_lock(&usecnt_lock);
02613 usecnt--;
02614 if (usecnt < 0)
02615 ast_log(LOG_WARNING, "Usecnt < 0???\n");
02616 ast_mutex_unlock(&usecnt_lock);
02617 ast_update_use_count();
02618 if (option_verbose > 2)
02619 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
02620
02621 ast_mutex_lock(&iflock);
02622 tmp = iflist;
02623 prev = NULL;
02624 if (p->destroy) {
02625 while (tmp) {
02626 if (tmp == p) {
02627 destroy_channel(prev, tmp, 0);
02628 break;
02629 } else {
02630 prev = tmp;
02631 tmp = tmp->next;
02632 }
02633 }
02634 }
02635 ast_mutex_unlock(&iflock);
02636 return 0;
02637 }
02638
02639 static int zt_answer(struct ast_channel *ast)
02640 {
02641 struct zt_pvt *p = ast->tech_pvt;
02642 int res=0;
02643 int index;
02644 int oldstate = ast->_state;
02645 ast_setstate(ast, AST_STATE_UP);
02646 ast_mutex_lock(&p->lock);
02647 index = zt_get_index(ast, p, 0);
02648 if (index < 0)
02649 index = SUB_REAL;
02650
02651 if (p->radio) {
02652 ast_mutex_unlock(&p->lock);
02653 return 0;
02654 }
02655 switch(p->sig) {
02656 case SIG_FXSLS:
02657 case SIG_FXSGS:
02658 case SIG_FXSKS:
02659 p->ringt = 0;
02660
02661 case SIG_EM:
02662 case SIG_EM_E1:
02663 case SIG_EMWINK:
02664 case SIG_FEATD:
02665 case SIG_FEATDMF:
02666 case SIG_E911:
02667 case SIG_FEATB:
02668 case SIG_SF:
02669 case SIG_SFWINK:
02670 case SIG_SF_FEATD:
02671 case SIG_SF_FEATDMF:
02672 case SIG_SF_FEATB:
02673 case SIG_FXOLS:
02674 case SIG_FXOGS:
02675 case SIG_FXOKS:
02676
02677 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
02678 if(p->hanguponpolarityswitch) {
02679 gettimeofday(&p->polaritydelaytv, NULL);
02680 }
02681 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
02682 tone_zone_play_tone(p->subs[index].zfd, -1);
02683 p->dialing = 0;
02684 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
02685 if (oldstate == AST_STATE_RINGING) {
02686 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
02687 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
02688 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02689 p->owner = p->subs[SUB_REAL].owner;
02690 }
02691 }
02692 if (p->sig & __ZT_SIG_FXS) {
02693 zt_enable_ec(p);
02694 zt_train_ec(p);
02695 }
02696 break;
02697 #ifdef ZAPATA_PRI
02698 case SIG_PRI:
02699
02700 if (!pri_grab(p, p->pri)) {
02701 p->proceeding = 1;
02702 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
02703 pri_rel(p->pri);
02704 } else {
02705 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02706 res= -1;
02707 }
02708 break;
02709 #endif
02710 #ifdef ZAPATA_R2
02711 case SIG_R2:
02712 res = mfcr2_AnswerCall(p->r2, NULL);
02713 if (res)
02714 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
02715 break;
02716 #endif
02717 case 0:
02718 ast_mutex_unlock(&p->lock);
02719 return 0;
02720 default:
02721 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
02722 res = -1;
02723 }
02724 ast_mutex_unlock(&p->lock);
02725 return res;
02726 }
02727
02728 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
02729 {
02730 char *cp;
02731 signed char *scp;
02732 int x;
02733 int index;
02734 struct zt_pvt *p = chan->tech_pvt;
02735
02736
02737 if (!data || (datalen < 1)) {
02738 errno = EINVAL;
02739 return -1;
02740 }
02741
02742 switch(option) {
02743 case AST_OPTION_TXGAIN:
02744 scp = (signed char *) data;
02745 index = zt_get_index(chan, p, 0);
02746 if (index < 0) {
02747 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
02748 return -1;
02749 }
02750 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
02751 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
02752 case AST_OPTION_RXGAIN:
02753 scp = (signed char *) data;
02754 index = zt_get_index(chan, p, 0);
02755 if (index < 0) {
02756 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
02757 return -1;
02758 }
02759 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
02760 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
02761 case AST_OPTION_TONE_VERIFY:
02762 if (!p->dsp)
02763 break;
02764 cp = (char *) data;
02765 switch (*cp) {
02766 case 1:
02767 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
02768 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax);
02769 break;
02770 case 2:
02771 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
02772 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax);
02773 break;
02774 default:
02775 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
02776 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02777 break;
02778 }
02779 break;
02780 case AST_OPTION_TDD:
02781
02782 cp = (char *) data;
02783 p->mate = 0;
02784 if (!*cp) {
02785 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
02786 if (p->tdd) tdd_free(p->tdd);
02787 p->tdd = 0;
02788 break;
02789 }
02790 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
02791 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
02792 zt_disable_ec(p);
02793
02794 if (!p->didtdd) {
02795 unsigned char mybuf[41000],*buf;
02796 int size,res,fd,len;
02797 struct pollfd fds[1];
02798
02799 buf = mybuf;
02800 memset(buf, 0x7f, sizeof(mybuf));
02801 ast_tdd_gen_ecdisa(buf + 16000, 16000);
02802 len = 40000;
02803 index = zt_get_index(chan, p, 0);
02804 if (index < 0) {
02805 ast_log(LOG_WARNING, "No index in TDD?\n");
02806 return -1;
02807 }
02808 fd = p->subs[index].zfd;
02809 while(len) {
02810 if (ast_check_hangup(chan)) return -1;
02811 size = len;
02812 if (size > READ_SIZE)
02813 size = READ_SIZE;
02814 fds[0].fd = fd;
02815 fds[0].events = POLLPRI | POLLOUT;
02816 fds[0].revents = 0;
02817 res = poll(fds, 1, -1);
02818 if (!res) {
02819 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
02820 continue;
02821 }
02822
02823 if (fds[0].revents & POLLPRI) return -1;
02824 if (!(fds[0].revents & POLLOUT)) {
02825 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
02826 continue;
02827 }
02828 res = write(fd, buf, size);
02829 if (res != size) {
02830 if (res == -1) return -1;
02831 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
02832 break;
02833 }
02834 len -= size;
02835 buf += size;
02836 }
02837 p->didtdd = 1;
02838 }
02839 if (*cp == 2) {
02840 if (p->tdd) tdd_free(p->tdd);
02841 p->tdd = 0;
02842 p->mate = 1;
02843 break;
02844 }
02845 if (!p->tdd) {
02846 p->tdd = tdd_new();
02847 }
02848 break;
02849 case AST_OPTION_RELAXDTMF:
02850 if (!p->dsp)
02851 break;
02852 cp = (char *) data;
02853 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
02854 *cp ? "ON" : "OFF", (int) *cp, chan->name);
02855 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
02856 break;
02857 case AST_OPTION_AUDIO_MODE:
02858 cp = (char *) data;
02859 if (!*cp) {
02860 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
02861 x = 0;
02862 zt_disable_ec(p);
02863 } else {
02864 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
02865 x = 1;
02866 }
02867 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
02868 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
02869 break;
02870 }
02871 errno = 0;
02872
02873 return 0;
02874 }
02875
02876 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
02877 {
02878
02879 int x;
02880 int hasslaves;
02881 if (!master)
02882 return;
02883 if (needlock) {
02884 ast_mutex_lock(&master->lock);
02885 if (slave) {
02886 while(ast_mutex_trylock(&slave->lock)) {
02887 ast_mutex_unlock(&master->lock);
02888 usleep(1);
02889 ast_mutex_lock(&master->lock);
02890 }
02891 }
02892 }
02893 hasslaves = 0;
02894 for (x=0;x<MAX_SLAVES;x++) {
02895 if (master->slaves[x]) {
02896 if (!slave || (master->slaves[x] == slave)) {
02897
02898 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
02899 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
02900 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
02901 master->slaves[x]->master = NULL;
02902 master->slaves[x] = NULL;
02903 } else
02904 hasslaves = 1;
02905 }
02906 if (!hasslaves)
02907 master->inconference = 0;
02908 }
02909 if (!slave) {
02910 if (master->master) {
02911
02912 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
02913 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
02914 hasslaves = 0;
02915 for (x=0;x<MAX_SLAVES;x++) {
02916 if (master->master->slaves[x] == master)
02917 master->master->slaves[x] = NULL;
02918 else if (master->master->slaves[x])
02919 hasslaves = 1;
02920 }
02921 if (!hasslaves)
02922 master->master->inconference = 0;
02923 }
02924 master->master = NULL;
02925 }
02926 update_conf(master);
02927 if (needlock) {
02928 if (slave)
02929 ast_mutex_unlock(&slave->lock);
02930 ast_mutex_unlock(&master->lock);
02931 }
02932 }
02933
02934 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
02935 int x;
02936 if (!slave || !master) {
02937 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
02938 return;
02939 }
02940 for (x=0;x<MAX_SLAVES;x++) {
02941 if (!master->slaves[x]) {
02942 master->slaves[x] = slave;
02943 break;
02944 }
02945 }
02946 if (x >= MAX_SLAVES) {
02947 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
02948 master->slaves[MAX_SLAVES - 1] = slave;
02949 }
02950 if (slave->master)
02951 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
02952 slave->master = master;
02953
02954 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
02955 }
02956
02957 static void disable_dtmf_detect(struct zt_pvt *p)
02958 {
02959 #ifdef ZT_TONEDETECT
02960 int val;
02961 #endif
02962
02963 p->ignoredtmf = 1;
02964
02965 #ifdef ZT_TONEDETECT
02966 val = 0;
02967 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
02968 #endif
02969 if (!p->hardwaredtmf && p->dsp) {
02970 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
02971 ast_dsp_set_features(p->dsp, p->dsp_features);
02972 }
02973 }
02974
02975 static void enable_dtmf_detect(struct zt_pvt *p)
02976 {
02977 #ifdef ZT_TONEDETECT
02978 int val;
02979 #endif
02980
02981 if (p->channel == CHAN_PSEUDO)
02982 return;
02983
02984 p->ignoredtmf = 0;
02985
02986 #ifdef ZT_TONEDETECT
02987 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
02988 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
02989 #endif
02990 if (!p->hardwaredtmf && p->dsp) {
02991 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
02992 ast_dsp_set_features(p->dsp, p->dsp_features);
02993 }
02994 }
02995
02996 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)
02997 {
02998 struct ast_channel *who;
02999 struct zt_pvt *p0, *p1, *op0, *op1;
03000 struct zt_pvt *master = NULL, *slave = NULL;
03001 struct ast_frame *f;
03002 int inconf = 0;
03003 int nothingok = 1;
03004 int ofd0, ofd1;
03005 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
03006 int os0 = -1, os1 = -1;
03007 int priority = 0;
03008 struct ast_channel *oc0, *oc1;
03009 enum ast_bridge_result res;
03010
03011 #ifdef PRI_2BCT
03012 int triedtopribridge = 0;
03013 q931_call *q931c0 = NULL, *q931c1 = NULL;
03014 #endif
03015
03016
03017
03018
03019
03020
03021 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
03022 return AST_BRIDGE_FAILED_NOWARN;
03023
03024 ast_mutex_lock(&c0->lock);
03025 ast_mutex_lock(&c1->lock);
03026
03027 p0 = c0->tech_pvt;
03028 p1 = c1->tech_pvt;
03029
03030 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
03031 ast_mutex_unlock(&c0->lock);
03032 ast_mutex_unlock(&c1->lock);
03033 return AST_BRIDGE_FAILED_NOWARN;
03034 }
03035
03036 oi0 = zt_get_index(c0, p0, 0);
03037 oi1 = zt_get_index(c1, p1, 0);
03038 if ((oi0 < 0) || (oi1 < 0)) {
03039 ast_mutex_unlock(&c0->lock);
03040 ast_mutex_unlock(&c1->lock);
03041 return AST_BRIDGE_FAILED;
03042 }
03043
03044 op0 = p0 = c0->tech_pvt;
03045 op1 = p1 = c1->tech_pvt;
03046 ofd0 = c0->fds[0];
03047 ofd1 = c1->fds[0];
03048 oc0 = p0->owner;
03049 oc1 = p1->owner;
03050
03051 ast_mutex_lock(&p0->lock);
03052 if (ast_mutex_trylock(&p1->lock)) {
03053
03054 ast_mutex_unlock(&p0->lock);
03055 ast_mutex_unlock(&c0->lock);
03056 ast_mutex_unlock(&c1->lock);
03057 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03058 return AST_BRIDGE_RETRY;
03059 }
03060
03061 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03062 if (p0->owner && p1->owner) {
03063
03064 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
03065 master = p0;
03066 slave = p1;
03067 inconf = 1;
03068 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
03069 master = p1;
03070 slave = p0;
03071 inconf = 1;
03072 } else {
03073 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
03074 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
03075 p0->channel,
03076 oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03077 p0->subs[SUB_REAL].inthreeway, p0->channel,
03078 oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03079 p1->subs[SUB_REAL].inthreeway);
03080 }
03081 nothingok = 0;
03082 }
03083 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
03084 if (p1->subs[SUB_THREEWAY].inthreeway) {
03085 master = p1;
03086 slave = p0;
03087 nothingok = 0;
03088 }
03089 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
03090 if (p0->subs[SUB_THREEWAY].inthreeway) {
03091 master = p0;
03092 slave = p1;
03093 nothingok = 0;
03094 }
03095 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
03096
03097
03098 if (p1->subs[SUB_CALLWAIT].inthreeway) {
03099 master = p1;
03100 slave = p0;
03101 nothingok = 0;
03102 }
03103 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
03104
03105 if (p0->subs[SUB_CALLWAIT].inthreeway) {
03106 master = p0;
03107 slave = p1;
03108 nothingok = 0;
03109 }
03110 }
03111 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
03112 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
03113 if (master && slave) {
03114
03115
03116
03117 if ((oi1 == SUB_THREEWAY) &&
03118 p1->subs[SUB_THREEWAY].inthreeway &&
03119 p1->subs[SUB_REAL].owner &&
03120 p1->subs[SUB_REAL].inthreeway &&
03121 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03122 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
03123 tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE);
03124 os1 = p1->subs[SUB_REAL].owner->_state;
03125 } else {
03126 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
03127 tone_zone_play_tone(p0->subs[oi0].zfd, -1);
03128 }
03129 if ((oi0 == SUB_THREEWAY) &&
03130 p0->subs[SUB_THREEWAY].inthreeway &&
03131 p0->subs[SUB_REAL].owner &&
03132 p0->subs[SUB_REAL].inthreeway &&
03133 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03134 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
03135 tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE);
03136 os0 = p0->subs[SUB_REAL].owner->_state;
03137 } else {
03138 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
03139 tone_zone_play_tone(p1->subs[oi0].zfd, -1);
03140 }
03141 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03142 if (!p0->echocanbridged || !p1->echocanbridged) {
03143
03144 zt_disable_ec(p0);
03145 zt_disable_ec(p1);
03146 }
03147 }
03148 zt_link(slave, master);
03149 master->inconference = inconf;
03150 } else if (!nothingok)
03151 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
03152
03153 update_conf(p0);
03154 update_conf(p1);
03155 t0 = p0->subs[SUB_REAL].inthreeway;
03156 t1 = p1->subs[SUB_REAL].inthreeway;
03157
03158 ast_mutex_unlock(&p0->lock);
03159 ast_mutex_unlock(&p1->lock);
03160
03161 ast_mutex_unlock(&c0->lock);
03162 ast_mutex_unlock(&c1->lock);
03163
03164
03165 if ((!master || !slave) && !nothingok) {
03166 zt_enable_ec(p0);
03167 zt_enable_ec(p1);
03168 return AST_BRIDGE_FAILED;
03169 }
03170
03171 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03172 disable_dtmf_detect(op0);
03173
03174 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03175 disable_dtmf_detect(op1);
03176
03177 for (;;) {
03178 struct ast_channel *c0_priority[2] = {c0, c1};
03179 struct ast_channel *c1_priority[2] = {c1, c0};
03180
03181
03182
03183 ast_mutex_lock(&c0->lock);
03184 ast_mutex_lock(&c1->lock);
03185 p0 = c0->tech_pvt;
03186 p1 = c1->tech_pvt;
03187
03188 if (op0 == p0)
03189 i0 = zt_get_index(c0, p0, 1);
03190 if (op1 == p1)
03191 i1 = zt_get_index(c1, p1, 1);
03192 ast_mutex_unlock(&c0->lock);
03193 ast_mutex_unlock(&c1->lock);
03194
03195 if (!timeoutms ||
03196 (op0 != p0) ||
03197 (op1 != p1) ||
03198 (ofd0 != c0->fds[0]) ||
03199 (ofd1 != c1->fds[0]) ||
03200 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
03201 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
03202 (oc0 != p0->owner) ||
03203 (oc1 != p1->owner) ||
03204 (t0 != p0->subs[SUB_REAL].inthreeway) ||
03205 (t1 != p1->subs[SUB_REAL].inthreeway) ||
03206 (oi0 != i0) ||
03207 (oi1 != i1)) {
03208 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
03209 op0->channel, oi0, op1->channel, oi1);
03210 res = AST_BRIDGE_RETRY;
03211 goto return_from_bridge;
03212 }
03213
03214 #ifdef PRI_2BCT
03215 q931c0 = p0->call;
03216 q931c1 = p1->call;
03217 if (p0->transfer && p1->transfer
03218 && q931c0 && q931c1
03219 && !triedtopribridge) {
03220 pri_channel_bridge(q931c0, q931c1);
03221 triedtopribridge = 1;
03222 }
03223 #endif
03224
03225 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
03226 if (!who) {
03227 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
03228 continue;
03229 }
03230 f = ast_read(who);
03231 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
03232 *fo = f;
03233 *rc = who;
03234 res = AST_BRIDGE_COMPLETE;
03235 goto return_from_bridge;
03236 }
03237 if (f->frametype == AST_FRAME_DTMF) {
03238 if ((who == c0) && p0->pulsedial) {
03239 ast_write(c1, f);
03240 } else if ((who == c1) && p1->pulsedial) {
03241 ast_write(c0, f);
03242 } else {
03243 *fo = f;
03244 *rc = who;
03245 res = AST_BRIDGE_COMPLETE;
03246 goto return_from_bridge;
03247 }
03248 }
03249 ast_frfree(f);
03250
03251
03252 priority = !priority;
03253 }
03254
03255 return_from_bridge:
03256 if (op0 == p0)
03257 zt_enable_ec(p0);
03258
03259 if (op1 == p1)
03260 zt_enable_ec(p1);
03261
03262 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03263 enable_dtmf_detect(op0);
03264
03265 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03266 enable_dtmf_detect(op1);
03267
03268 zt_unlink(slave, master, 1);
03269
03270 return res;
03271 }
03272
03273 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03274 {
03275 struct zt_pvt *p = newchan->tech_pvt;
03276 int x;
03277 ast_mutex_lock(&p->lock);
03278 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
03279 if (p->owner == oldchan) {
03280 p->owner = newchan;
03281 }
03282 for (x=0;x<3;x++)
03283 if (p->subs[x].owner == oldchan) {
03284 if (!x)
03285 zt_unlink(NULL, p, 0);
03286 p->subs[x].owner = newchan;
03287 }
03288 if (newchan->_state == AST_STATE_RINGING)
03289 zt_indicate(newchan, AST_CONTROL_RINGING);
03290 update_conf(p);
03291 ast_mutex_unlock(&p->lock);
03292 return 0;
03293 }
03294
03295 static int zt_ring_phone(struct zt_pvt *p)
03296 {
03297 int x;
03298 int res;
03299
03300 x = 0;
03301 x = ZT_ONHOOK;
03302 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03303 do {
03304 x = ZT_RING;
03305 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03306 #if 0
03307 printf("Res: %d, error: %s\n", res, strerror(errno));
03308 #endif
03309 if (res) {
03310 switch(errno) {
03311 case EBUSY:
03312 case EINTR:
03313
03314 usleep(10000);
03315 continue;
03316 case EINPROGRESS:
03317 res = 0;
03318 break;
03319 default:
03320 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
03321 res = 0;
03322 }
03323 }
03324 } while (res);
03325 return res;
03326 }
03327
03328 static void *ss_thread(void *data);
03329
03330 static struct ast_channel *zt_new(struct zt_pvt *, int, int, int, int, int);
03331
03332 static int attempt_transfer(struct zt_pvt *p)
03333 {
03334
03335
03336
03337 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
03338
03339
03340 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
03341 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
03342 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
03343 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
03344 }
03345 if (p->subs[SUB_REAL].owner->cdr) {
03346
03347 p->subs[SUB_THREEWAY].owner->cdr =
03348 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, p->subs[SUB_REAL].owner->cdr);
03349 p->subs[SUB_REAL].owner->cdr = NULL;
03350 }
03351 if (ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr) {
03352
03353 p->subs[SUB_THREEWAY].owner->cdr =
03354 ast_cdr_append(p->subs[SUB_THREEWAY].owner->cdr, ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr);
03355 ast_bridged_channel(p->subs[SUB_REAL].owner)->cdr = NULL;
03356 }
03357 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
03358 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03359 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
03360 return -1;
03361 }
03362
03363 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03364 unalloc_sub(p, SUB_THREEWAY);
03365 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
03366 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
03367 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
03368 }
03369 ast_moh_stop(ast_bridged_channel(p->subs[SUB_THREEWAY].owner));
03370 if (p->subs[SUB_THREEWAY].owner->cdr) {
03371
03372 p->subs[SUB_REAL].owner->cdr =
03373 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, p->subs[SUB_THREEWAY].owner->cdr);
03374 p->subs[SUB_THREEWAY].owner->cdr = NULL;
03375 }
03376 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr) {
03377
03378 p->subs[SUB_REAL].owner->cdr =
03379 ast_cdr_append(p->subs[SUB_REAL].owner->cdr, ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr);
03380 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->cdr = NULL;
03381 }
03382 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
03383 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
03384 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
03385 return -1;
03386 }
03387
03388 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03389 ast_mutex_unlock(&p->subs[SUB_REAL].owner->lock);
03390 unalloc_sub(p, SUB_THREEWAY);
03391
03392 return 1;
03393 } else {
03394 ast_log(LOG_DEBUG, "Neither %s nor %s are in a bridge, nothing to transfer\n",
03395 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
03396 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03397 return -1;
03398 }
03399 return 0;
03400 }
03401
03402 #ifdef ZAPATA_R2
03403 static struct ast_frame *handle_r2_event(struct zt_pvt *p, mfcr2_event_t *e, int index)
03404 {
03405 struct ast_frame *f;
03406 f = &p->subs[index].f;
03407 if (!p->r2) {
03408 ast_log(LOG_WARNING, "Huh? No R2 structure :(\n");
03409 return NULL;
03410 }
03411 switch(e->e) {
03412 case MFCR2_EVENT_BLOCKED:
03413 if (option_verbose > 2)
03414 ast_verbose(VERBOSE_PREFIX_3 "Channel %d blocked\n", p->channel);
03415 break;
03416 case MFCR2_EVENT_UNBLOCKED:
03417 if (option_verbose > 2)
03418 ast_verbose(VERBOSE_PREFIX_3 "Channel %d unblocked\n", p->channel);
03419 break;
03420 case MFCR2_EVENT_CONFIG_ERR:
03421 if (option_verbose > 2)
03422 ast_verbose(VERBOSE_PREFIX_3 "Config error on channel %d\n", p->channel);
03423 break;
03424 case MFCR2_EVENT_RING:
03425 if (option_verbose > 2)
03426 ast_verbose(VERBOSE_PREFIX_3 "Ring on channel %d\n", p->channel);
03427 break;
03428 case MFCR2_EVENT_HANGUP:
03429 if (option_verbose > 2)
03430 ast_verbose(VERBOSE_PREFIX_3 "Hangup on channel %d\n", p->channel);
03431 break;
03432 case MFCR2_EVENT_RINGING:
03433 if (option_verbose > 2)
03434 ast_verbose(VERBOSE_PREFIX_3 "Ringing on channel %d\n", p->channel);
03435 break;
03436 case MFCR2_EVENT_ANSWER:
03437 if (option_verbose > 2)
03438 ast_verbose(VERBOSE_PREFIX_3 "Answer on channel %d\n", p->channel);
03439 break;
03440 case MFCR2_EVENT_HANGUP_ACK:
03441 if (option_verbose > 2)
03442 ast_verbose(VERBOSE_PREFIX_3 "Hangup ACK on channel %d\n", p->channel);
03443 break;
03444 case MFCR2_EVENT_IDLE:
03445 if (option_verbose > 2)
03446 ast_verbose(VERBOSE_PREFIX_3 "Idle on channel %d\n", p->channel);
03447 break;
03448 default:
03449 ast_log(LOG_WARNING, "Unknown MFC/R2 event %d\n", e->e);
03450 break;
03451 }
03452 return f;
03453 }
03454
03455 static mfcr2_event_t *r2_get_event_bits(struct zt_pvt *p)
03456 {
03457 int x;
03458 int res;
03459 mfcr2_event_t *e;
03460 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETRXBITS, &x);
03461 if (res) {
03462 ast_log(LOG_WARNING, "Unable to check received bits\n");
03463 return NULL;
03464 }
03465 if (!p->r2) {
03466 ast_log(LOG_WARNING, "Odd, no R2 structure on channel %d\n", p->channel);
03467 return NULL;
03468 }
03469 e = mfcr2_cas_signaling_event(p->r2, x);
03470 return e;
03471 }
03472 #endif
03473
03474 static int check_for_conference(struct zt_pvt *p)
03475 {
03476 ZT_CONFINFO ci;
03477
03478 if (p->master || (p->confno > -1))
03479 return 0;
03480 memset(&ci, 0, sizeof(ci));
03481 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
03482 ast_log(LOG_WARNING, "Failed to get conference info on channel %d\n", p->channel);
03483 return 0;
03484 }
03485
03486
03487
03488 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
03489 if (option_verbose > 2)
03490 ast_verbose(VERBOSE_PREFIX_3 "Avoiding 3-way call when in an external conference\n");
03491 return 1;
03492 }
03493 return 0;
03494 }
03495
03496 static int get_alarms(struct zt_pvt *p)
03497 {
03498 int res;
03499 ZT_SPANINFO zi;
03500 memset(&zi, 0, sizeof(zi));
03501 zi.spanno = p->span;
03502 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SPANSTAT, &zi);
03503 if (res < 0) {
03504 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
03505 return 0;
03506 }
03507 return zi.alarms;
03508 }
03509
03510 static struct ast_frame *zt_handle_event(struct ast_channel *ast)
03511 {
03512 int res,x;
03513 int index;
03514 char *c;
03515 struct zt_pvt *p = ast->tech_pvt;
03516 pthread_t threadid;
03517 pthread_attr_t attr;
03518 struct ast_channel *chan;
03519
03520 pthread_attr_init(&attr);
03521 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
03522
03523 index = zt_get_index(ast, p, 0);
03524 p->subs[index].f.frametype = AST_FRAME_NULL;
03525 p->subs[index].f.datalen = 0;
03526 p->subs[index].f.samples = 0;
03527 p->subs[index].f.mallocd = 0;
03528 p->subs[index].f.offset = 0;
03529 p->subs[index].f.src = "zt_handle_event";
03530 p->subs[index].f.data = NULL;
03531 if (index < 0)
03532 return &p->subs[index].f;
03533 if (p->fake_event) {
03534 res = p->fake_event;
03535 p->fake_event = 0;
03536 } else
03537 res = zt_get_event(p->subs[index].zfd);
03538
03539 ast_log(LOG_DEBUG, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, index);
03540
03541 if (res & (ZT_EVENT_PULSEDIGIT | ZT_EVENT_DTMFUP)) {
03542 if (res & ZT_EVENT_PULSEDIGIT)
03543 p->pulsedial = 1;
03544 else
03545 p->pulsedial = 0;
03546 ast_log(LOG_DEBUG, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse ": "", res & 0xff);
03547 #ifdef ZAPATA_PRI
03548 if (!p->proceeding && p->sig == SIG_PRI && p->pri && p->pri->overlapdial) {
03549 p->subs[index].f.frametype = AST_FRAME_NULL;
03550 p->subs[index].f.subclass = 0;
03551 } else {
03552 #endif
03553 p->subs[index].f.frametype = AST_FRAME_DTMF;
03554 p->subs[index].f.subclass = res & 0xff;
03555 #ifdef ZAPATA_PRI
03556 }
03557 #endif
03558
03559 zt_confmute(p, 0);
03560 return &p->subs[index].f;
03561 }
03562
03563 if (res & ZT_EVENT_DTMFDOWN) {
03564 ast_log(LOG_DEBUG, "DTMF Down '%c'\n", res & 0xff);
03565 p->subs[index].f.frametype = AST_FRAME_NULL;
03566 p->subs[index].f.subclass = 0;
03567 zt_confmute(p, 1);
03568
03569 return &p->subs[index].f;
03570 }
03571
03572 switch(res) {
03573 case ZT_EVENT_BITSCHANGED:
03574 if (p->sig == SIG_R2) {
03575 #ifdef ZAPATA_R2
03576 struct ast_frame *f = &p->subs[index].f;
03577 mfcr2_event_t *e;
03578 e = r2_get_event_bits(p);
03579 if (e)
03580 f = handle_r2_event(p, e, index);
03581 return f;
03582 #else
03583 break;
03584 #endif
03585 }
03586 ast_log(LOG_WARNING, "Recieved bits changed on %s signalling?\n", sig2str(p->sig));
03587 case ZT_EVENT_PULSE_START:
03588
03589 if (!ast->pbx)
03590 tone_zone_play_tone(p->subs[index].zfd, -1);
03591 break;
03592 case ZT_EVENT_DIALCOMPLETE:
03593 if (p->inalarm) break;
03594 if (p->radio) break;
03595 if (ioctl(p->subs[index].zfd,ZT_DIALING,&x) == -1) {
03596 ast_log(LOG_DEBUG, "ZT_DIALING ioctl failed on %s\n",ast->name);
03597 return NULL;
03598 }
03599 if (!x) {
03600 zt_enable_ec(p);
03601 if (p->echobreak) {
03602 zt_train_ec(p);
03603 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
03604 p->dop.op = ZT_DIAL_OP_REPLACE;
03605 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
03606 p->echobreak = 0;
03607 } else {
03608 p->dialing = 0;
03609 if (p->sig == SIG_E911) {
03610
03611 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
03612 ast_setstate(ast, AST_STATE_UP);
03613 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03614 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03615 break;
03616 } else {
03617
03618 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
03619 }
03620 }
03621 if (ast->_state == AST_STATE_DIALING) {
03622 if ((p->callprogress & 1) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
03623 ast_log(LOG_DEBUG, "Done dialing, but waiting for progress detection before doing more...\n");
03624 } 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)))) {
03625 ast_setstate(ast, AST_STATE_RINGING);
03626 } else if (!p->answeronpolarityswitch) {
03627 ast_setstate(ast, AST_STATE_UP);
03628 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03629 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03630 }
03631 }
03632 }
03633 }
03634 break;
03635 case ZT_EVENT_ALARM:
03636 #ifdef ZAPATA_PRI
03637 if (p->call) {
03638 if (p->pri && p->pri->pri) {
03639 if (!pri_grab(p, p->pri)) {
03640 pri_hangup(p->pri->pri, p->call, -1);
03641 pri_destroycall(p->pri->pri, p->call);
03642 p->call = NULL;
03643 pri_rel(p->pri);
03644 } else
03645 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
03646 } else
03647 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
03648 }
03649 if (p->owner)
03650 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
03651 if (p->bearer)
03652 p->bearer->inalarm = 1;
03653 else
03654 #endif
03655 p->inalarm = 1;
03656 res = get_alarms(p);
03657 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm2str(res));
03658 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
03659 "Alarm: %s\r\n"
03660 "Channel: %d\r\n",
03661 alarm2str(res), p->channel);
03662
03663 case ZT_EVENT_ONHOOK:
03664 if (p->radio)
03665 {
03666 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03667 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
03668 break;
03669 }
03670 switch(p->sig) {
03671 case SIG_FXOLS:
03672 case SIG_FXOGS:
03673 case SIG_FXOKS:
03674 p->onhooktime = time(NULL);
03675 p->msgstate = -1;
03676
03677 if (index == SUB_REAL) {
03678
03679 if (p->subs[SUB_CALLWAIT].owner) {
03680
03681 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
03682 if (option_verbose > 2)
03683 ast_verbose(VERBOSE_PREFIX_3 "Channel %d still has (callwait) call, ringing phone\n", p->channel);
03684 unalloc_sub(p, SUB_CALLWAIT);
03685 #if 0
03686 p->subs[index].needanswer = 0;
03687 p->subs[index].needringing = 0;
03688 #endif
03689 p->callwaitingrepeat = 0;
03690 p->cidcwexpire = 0;
03691 p->owner = NULL;
03692
03693 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
03694 p->dialing = 1;
03695 zt_ring_phone(p);
03696 } else if (p->subs[SUB_THREEWAY].owner) {
03697 unsigned int mssinceflash;
03698
03699
03700 while(p->subs[SUB_THREEWAY].owner && ast_mutex_trylock(&p->subs[SUB_THREEWAY].owner->lock)) {
03701
03702 ast_mutex_unlock(&p->lock);
03703 ast_mutex_unlock(&ast->lock);
03704 usleep(1);
03705
03706
03707
03708 ast_mutex_lock(&ast->lock);
03709 ast_mutex_lock(&p->lock);
03710 if (p->owner != ast) {
03711 ast_log(LOG_WARNING, "This isn't good...\n");
03712 return NULL;
03713 }
03714 }
03715 if (!p->subs[SUB_THREEWAY].owner) {
03716 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
03717 return NULL;
03718 }
03719 mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
03720 ast_log(LOG_DEBUG, "Last flash was %d ms ago\n", mssinceflash);
03721 if (mssinceflash < MIN_MS_SINCE_FLASH) {
03722
03723
03724 if (p->subs[SUB_THREEWAY].owner)
03725 ast_queue_hangup(p->subs[SUB_THREEWAY].owner);
03726 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03727 ast_log(LOG_DEBUG, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
03728 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03729 } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
03730 if (p->transfer) {
03731
03732 p->subs[SUB_REAL].inthreeway = 0;
03733 p->subs[SUB_THREEWAY].inthreeway = 0;
03734
03735 if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
03736 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03737
03738 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03739 p->owner = NULL;
03740
03741 zt_ring_phone(p);
03742 } else {
03743 if ((res = attempt_transfer(p)) < 0) {
03744 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03745 if (p->subs[SUB_THREEWAY].owner)
03746 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03747 } else if (res) {
03748
03749 if (p->subs[SUB_THREEWAY].owner)
03750 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03751 break;
03752 }
03753 }
03754 } else {
03755 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
03756 if (p->subs[SUB_THREEWAY].owner)
03757 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03758 }
03759 } else {
03760 ast_mutex_unlock(&p->subs[SUB_THREEWAY].owner->lock);
03761
03762 swap_subs(p, SUB_THREEWAY, SUB_REAL);
03763 p->owner = NULL;
03764
03765 zt_ring_phone(p);
03766 }
03767 }
03768 } else {
03769 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", index);
03770 }
03771
03772 default:
03773 zt_disable_ec(p);
03774 return NULL;
03775 }
03776 break;
03777 case ZT_EVENT_RINGOFFHOOK:
03778 if (p->inalarm) break;
03779 if (p->radio)
03780 {
03781 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03782 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
03783 break;
03784 }
03785
03786
03787 if ((p->sig == SIG_E911) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
03788 c = strchr(p->dialdest, '/');
03789 if (c)
03790 c++;
03791 else
03792 c = p->dialdest;
03793 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
03794 else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
03795 if (strlen(p->dop.dialstr) > 4) {
03796 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
03797 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
03798 p->echorest[sizeof(p->echorest) - 1] = '\0';
03799 p->echobreak = 1;
03800 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
03801 } else
03802 p->echobreak = 0;
03803 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
03804 x = ZT_ONHOOK;
03805 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03806 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
03807 return NULL;
03808 }
03809 p->dialing = 1;
03810 return &p->subs[index].f;
03811 }
03812 switch(p->sig) {
03813 case SIG_FXOLS:
03814 case SIG_FXOGS:
03815 case SIG_FXOKS:
03816 switch(ast->_state) {
03817 case AST_STATE_RINGING:
03818 zt_enable_ec(p);
03819 zt_train_ec(p);
03820 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03821 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03822
03823 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
03824 ast_log(LOG_DEBUG, "channel %d answered\n", p->channel);
03825 if (p->cidspill) {
03826
03827 free(p->cidspill);
03828 p->cidspill = NULL;
03829 }
03830 p->dialing = 0;
03831 p->callwaitcas = 0;
03832 if (p->confirmanswer) {
03833
03834 p->subs[index].f.frametype = AST_FRAME_NULL;
03835 p->subs[index].f.subclass = 0;
03836 } else if (!ast_strlen_zero(p->dop.dialstr)) {
03837
03838 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
03839 if (res < 0) {
03840 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
03841 p->dop.dialstr[0] = '\0';
03842 return NULL;
03843 } else {
03844 ast_log(LOG_DEBUG, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
03845 p->subs[index].f.frametype = AST_FRAME_NULL;
03846 p->subs[index].f.subclass = 0;
03847 p->dialing = 1;
03848 }
03849 p->dop.dialstr[0] = '\0';
03850 ast_setstate(ast, AST_STATE_DIALING);
03851 } else
03852 ast_setstate(ast, AST_STATE_UP);
03853 return &p->subs[index].f;
03854 case AST_STATE_DOWN:
03855 ast_setstate(ast, AST_STATE_RING);
03856 ast->rings = 1;
03857 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03858 p->subs[index].f.subclass = AST_CONTROL_OFFHOOK;
03859 ast_log(LOG_DEBUG, "channel %d picked up\n", p->channel);
03860 return &p->subs[index].f;
03861 case AST_STATE_UP:
03862
03863 zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
03864
03865 if (ast_bridged_channel(p->owner))
03866 ast_moh_stop(ast_bridged_channel(p->owner));
03867 break;
03868 case AST_STATE_RESERVED:
03869
03870 if (has_voicemail(p))
03871 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
03872 else
03873 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
03874 break;
03875 default:
03876 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
03877 }
03878 break;
03879 case SIG_FXSLS:
03880 case SIG_FXSGS:
03881 case SIG_FXSKS:
03882 if (ast->_state == AST_STATE_RING) {
03883 p->ringt = p->ringt_base;
03884 }
03885
03886
03887
03888 ast_log(LOG_DEBUG, "Setting IDLE polarity due "
03889 "to ring. Old polarity was %d\n",
03890 p->polarity);
03891 p->polarity = POLARITY_IDLE;
03892
03893
03894 case SIG_EM:
03895 case SIG_EM_E1:
03896 case SIG_EMWINK:
03897 case SIG_FEATD:
03898 case SIG_FEATDMF:
03899 case SIG_FEATDMF_TA:
03900 case SIG_E911:
03901 case SIG_FEATB:
03902 case SIG_SF:
03903 case SIG_SFWINK:
03904 case SIG_SF_FEATD:
03905 case SIG_SF_FEATDMF:
03906 case SIG_SF_FEATB:
03907 if (ast->_state == AST_STATE_PRERING)
03908 ast_setstate(ast, AST_STATE_RING);
03909 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
03910 if (option_debug)
03911 ast_log(LOG_DEBUG, "Ring detected\n");
03912 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03913 p->subs[index].f.subclass = AST_CONTROL_RING;
03914 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
03915 if (option_debug)
03916 ast_log(LOG_DEBUG, "Line answered\n");
03917 if (p->confirmanswer) {
03918 p->subs[index].f.frametype = AST_FRAME_NULL;
03919 p->subs[index].f.subclass = 0;
03920 } else {
03921 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03922 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
03923 ast_setstate(ast, AST_STATE_UP);
03924 }
03925 } else if (ast->_state != AST_STATE_RING)
03926 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
03927 break;
03928 default:
03929 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
03930 }
03931 break;
03932 #ifdef ZT_EVENT_RINGBEGIN
03933 case ZT_EVENT_RINGBEGIN:
03934 switch(p->sig) {
03935 case SIG_FXSLS:
03936 case SIG_FXSGS:
03937 case SIG_FXSKS:
03938 if (ast->_state == AST_STATE_RING) {
03939 p->ringt = p->ringt_base;
03940 }
03941 break;
03942 }
03943 break;
03944 #endif
03945 case ZT_EVENT_RINGEROFF:
03946 if (p->inalarm) break;
03947 if (p->radio) break;
03948 ast->rings++;
03949 if ((ast->rings > p->cidrings) && (p->cidspill)) {
03950 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
03951 free(p->cidspill);
03952 p->cidspill = NULL;
03953 p->callwaitcas = 0;
03954 }
03955 p->subs[index].f.frametype = AST_FRAME_CONTROL;
03956 p->subs[index].f.subclass = AST_CONTROL_RINGING;
03957 break;
03958 case ZT_EVENT_RINGERON:
03959 break;
03960 case ZT_EVENT_NOALARM:
03961 p->inalarm = 0;
03962 #ifdef ZAPATA_PRI
03963
03964 if (p->bearer)
03965 p->bearer->inalarm = 0;
03966 #endif
03967 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
03968 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
03969 "Channel: %d\r\n", p->channel);
03970 break;
03971 case ZT_EVENT_WINKFLASH:
03972 if (p->inalarm) break;
03973 if (p->radio) break;
03974
03975 gettimeofday(&p->flashtime, NULL);
03976 switch(p->sig) {
03977 case SIG_FXOLS:
03978 case SIG_FXOGS:
03979 case SIG_FXOKS:
03980 ast_log(LOG_DEBUG, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
03981 index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
03982 p->callwaitcas = 0;
03983
03984 if (index != SUB_REAL) {
03985 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", index, p->channel);
03986 goto winkflashdone;
03987 }
03988
03989 if (p->subs[SUB_CALLWAIT].owner) {
03990
03991 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
03992 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
03993 p->owner = p->subs[SUB_REAL].owner;
03994 ast_log(LOG_DEBUG, "Making %s the new owner\n", p->owner->name);
03995 if (p->owner->_state == AST_STATE_RINGING) {
03996 ast_setstate(p->owner, AST_STATE_UP);
03997 p->subs[SUB_REAL].needanswer = 1;
03998 }
03999 p->callwaitingrepeat = 0;
04000 p->cidcwexpire = 0;
04001
04002 if (!p->subs[SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
04003 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
04004 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
04005 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
04006 } else if (!p->subs[SUB_THREEWAY].owner) {
04007 char cid_num[256];
04008 char cid_name[256];
04009
04010 if (!p->threewaycalling) {
04011
04012 p->subs[SUB_REAL].needflash = 1;
04013 goto winkflashdone;
04014 } else if (!check_for_conference(p)) {
04015 if (p->zaptrcallerid && p->owner) {
04016 if (p->owner->cid.cid_num)
04017 ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
04018 if (p->owner->cid.cid_name)
04019 ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
04020 }
04021
04022
04023 if (!((ast->pbx) ||
04024 (ast->_state == AST_STATE_UP) ||
04025 (ast->_state == AST_STATE_RING))) {
04026 ast_log(LOG_DEBUG, "Flash when call not up or ringing\n");
04027 goto winkflashdone;
04028 }
04029 if (alloc_sub(p, SUB_THREEWAY)) {
04030 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
04031 goto winkflashdone;
04032 }
04033
04034 chan = zt_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
04035 if (p->zaptrcallerid) {
04036 if (!p->origcid_num)
04037 p->origcid_num = strdup(p->cid_num);
04038 if (!p->origcid_name)
04039 p->origcid_name = strdup(p->cid_name);
04040 ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
04041 ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
04042 }
04043
04044 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04045
04046 zt_disable_ec(p);
04047 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_DIALRECALL);
04048 if (res)
04049 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
04050 p->owner = chan;
04051 if (!chan) {
04052 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", p->channel);
04053 } else if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
04054 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
04055 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
04056 zt_enable_ec(p);
04057 ast_hangup(chan);
04058 } else {
04059 if (option_verbose > 2)
04060 ast_verbose(VERBOSE_PREFIX_3 "Started three way call on channel %d\n", p->channel);
04061
04062 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
04063 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
04064 }
04065 }
04066 } else {
04067
04068 if (p->subs[SUB_THREEWAY].inthreeway) {
04069
04070 if (option_debug)
04071 ast_log(LOG_DEBUG, "Got flash with three way call up, dropping last call on %d\n", p->channel);
04072
04073 if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
04074
04075 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04076 p->owner = p->subs[SUB_REAL].owner;
04077 }
04078
04079 if (option_verbose > 2)
04080 ast_verbose(VERBOSE_PREFIX_3 "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
04081 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04082 p->subs[SUB_REAL].inthreeway = 0;
04083 p->subs[SUB_THREEWAY].inthreeway = 0;
04084 } else {
04085
04086 if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
04087 (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
04088 int otherindex = SUB_THREEWAY;
04089
04090 if (option_verbose > 2)
04091 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);
04092
04093 p->subs[SUB_THREEWAY].inthreeway = 1;
04094 p->subs[SUB_REAL].inthreeway = 1;
04095 if (ast->_state == AST_STATE_UP) {
04096 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04097 otherindex = SUB_REAL;
04098 }
04099 if (p->subs[otherindex].owner && ast_bridged_channel(p->subs[otherindex].owner))
04100 ast_moh_stop(ast_bridged_channel(p->subs[otherindex].owner));
04101 p->owner = p->subs[SUB_REAL].owner;
04102 if (ast->_state == AST_STATE_RINGING) {
04103 ast_log(LOG_DEBUG, "Enabling ringtone on real and threeway\n");
04104 res = tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
04105 res = tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, ZT_TONE_RINGTONE);
04106 }
04107 } else {
04108 if (option_verbose > 2)
04109 ast_verbose(VERBOSE_PREFIX_3 "Dumping incomplete call on on %s\n", p->subs[SUB_THREEWAY].owner->name);
04110 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04111 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
04112 p->owner = p->subs[SUB_REAL].owner;
04113 if (p->subs[SUB_REAL].owner && ast_bridged_channel(p->subs[SUB_REAL].owner))
04114 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
04115 zt_enable_ec(p);
04116 }
04117
04118 }
04119 }
04120 winkflashdone:
04121 update_conf(p);
04122 break;
04123 case SIG_EM:
04124 case SIG_EM_E1:
04125 case SIG_EMWINK:
04126 case SIG_FEATD:
04127 case SIG_SF:
04128 case SIG_SFWINK:
04129 case SIG_SF_FEATD:
04130 case SIG_FXSLS:
04131 case SIG_FXSGS:
04132 if (p->dialing)
04133 ast_log(LOG_DEBUG, "Ignoring wink on channel %d\n", p->channel);
04134 else
04135 ast_log(LOG_DEBUG, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
04136 break;
04137 case SIG_FEATDMF_TA:
04138 switch (p->whichwink) {
04139 case 0:
04140 ast_log(LOG_DEBUG, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04141 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
04142 break;
04143 case 1:
04144 ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
04145 break;
04146 case 2:
04147 ast_log(LOG_WARNING, "Received unexpected wink on channel of type SIG_FEATDMF_TA\n");
04148 return NULL;
04149 }
04150 p->whichwink++;
04151
04152 case SIG_FEATDMF:
04153 case SIG_E911:
04154 case SIG_FEATB:
04155 case SIG_SF_FEATDMF:
04156 case SIG_SF_FEATB:
04157
04158 if (!ast_strlen_zero(p->dop.dialstr))
04159 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04160 else if (res < 0) {
04161 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04162 p->dop.dialstr[0] = '\0';
04163 return NULL;
04164 } else
04165 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04166 p->dop.dialstr[0] = '\0';
04167 break;
04168 default:
04169 ast_log(LOG_WARNING, "Don't know how to handle ring/off hoook for signalling %d\n", p->sig);
04170 }
04171 break;
04172 case ZT_EVENT_HOOKCOMPLETE:
04173 if (p->inalarm) break;
04174 if (p->radio) break;
04175 switch(p->sig) {
04176 case SIG_FXSLS:
04177 case SIG_FXSGS:
04178 case SIG_FXSKS:
04179 case SIG_EM:
04180 case SIG_EM_E1:
04181 case SIG_EMWINK:
04182 case SIG_FEATD:
04183 case SIG_SF:
04184 case SIG_SFWINK:
04185 case SIG_SF_FEATD:
04186 if (!ast_strlen_zero(p->dop.dialstr))
04187 res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop);
04188 else if (res < 0) {
04189 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
04190 p->dop.dialstr[0] = '\0';
04191 return NULL;
04192 } else
04193 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
04194 p->dop.dialstr[0] = '\0';
04195 p->dop.op = ZT_DIAL_OP_REPLACE;
04196 break;
04197 case SIG_FEATDMF:
04198 case SIG_E911:
04199 case SIG_FEATB:
04200 case SIG_SF_FEATDMF:
04201 case SIG_SF_FEATB:
04202 ast_log(LOG_DEBUG, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
04203 break;
04204 default:
04205 break;
04206 }
04207 break;
04208 case ZT_EVENT_POLARITY:
04209
04210
04211
04212
04213
04214
04215 if (p->polarity == POLARITY_IDLE) {
04216 p->polarity = POLARITY_REV;
04217 if (p->answeronpolarityswitch &&
04218 ((ast->_state == AST_STATE_DIALING) ||
04219 (ast->_state == AST_STATE_RINGING))) {
04220 ast_log(LOG_DEBUG, "Answering on polarity switch!\n");
04221 ast_setstate(p->owner, AST_STATE_UP);
04222 if(p->hanguponpolarityswitch) {
04223 gettimeofday(&p->polaritydelaytv, NULL);
04224 }
04225 break;
04226 } else
04227 ast_log(LOG_DEBUG, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, ast->_state);
04228 }
04229
04230
04231 if(p->hanguponpolarityswitch &&
04232 (p->polarityonanswerdelay > 0) &&
04233 (p->polarity == POLARITY_REV) &&
04234 ((ast->_state == AST_STATE_UP) || (ast->_state == AST_STATE_RING)) ) {
04235
04236 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) );
04237
04238 if(ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
04239 ast_log(LOG_DEBUG, "Polarity Reversal detected and now Hanging up on channel %d\n", p->channel);
04240 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
04241 p->polarity = POLARITY_IDLE;
04242 } else {
04243 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);
04244 }
04245 } else {
04246 p->polarity = POLARITY_IDLE;
04247 ast_log(LOG_DEBUG, "Ignoring Polarity switch to IDLE on channel %d, state %d\n", p->channel, ast->_state);
04248 }
04249
04250 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) );
04251 break;
04252 default:
04253 ast_log(LOG_DEBUG, "Dunno what to do with event %d on channel %d\n", res, p->channel);
04254 }
04255 return &p->subs[index].f;
04256 }
04257
04258 static struct ast_frame *__zt_exception(struct ast_channel *ast)
04259 {
04260 struct zt_pvt *p = ast->tech_pvt;
04261 int res;
04262 int usedindex=-1;
04263 int index;
04264 struct ast_frame *f;
04265
04266
04267 index = zt_get_index(ast, p, 1);
04268
04269 p->subs[index].f.frametype = AST_FRAME_NULL;
04270 p->subs[index].f.datalen = 0;
04271 p->subs[index].f.samples = 0;
04272 p->subs[index].f.mallocd = 0;
04273 p->subs[index].f.offset = 0;
04274 p->subs[index].f.subclass = 0;
04275 p->subs[index].f.delivery = ast_tv(0,0);
04276 p->subs[index].f.src = "zt_exception";
04277 p->subs[index].f.data = NULL;
04278
04279
04280 if ((!p->owner) && (!p->radio)) {
04281
04282
04283
04284
04285
04286 if (p->fake_event) {
04287 res = p->fake_event;
04288 p->fake_event = 0;
04289 } else
04290 res = zt_get_event(p->subs[SUB_REAL].zfd);
04291
04292 if ((res != ZT_EVENT_RINGEROFF) && (res != ZT_EVENT_RINGERON) &&
04293 (res != ZT_EVENT_HOOKCOMPLETE)) {
04294 ast_log(LOG_DEBUG, "Restoring owner of channel %d on event %d\n", p->channel, res);
04295 p->owner = p->subs[SUB_REAL].owner;
04296 if (p->owner && ast_bridged_channel(p->owner))
04297 ast_moh_stop(ast_bridged_channel(p->owner));
04298 }
04299 switch(res) {
04300 case ZT_EVENT_ONHOOK:
04301 zt_disable_ec(p);
04302 if (p->owner) {
04303 if (option_verbose > 2)
04304 ast_verbose(VERBOSE_PREFIX_3 "Channel %s still has call, ringing phone\n", p->owner->name);
04305 zt_ring_phone(p);
04306 p->callwaitingrepeat = 0;
04307 p->cidcwexpire = 0;
04308 } else
04309 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04310 update_conf(p);
04311 break;
04312 case ZT_EVENT_RINGOFFHOOK:
04313 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
04314 if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
04315 p->subs[SUB_REAL].needanswer = 1;
04316 p->dialing = 0;
04317 }
04318 break;
04319 case ZT_EVENT_HOOKCOMPLETE:
04320 case ZT_EVENT_RINGERON:
04321 case ZT_EVENT_RINGEROFF:
04322
04323 break;
04324 case ZT_EVENT_WINKFLASH:
04325 gettimeofday(&p->flashtime, NULL);
04326 if (p->owner) {
04327 if (option_verbose > 2)
04328 ast_verbose(VERBOSE_PREFIX_3 "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
04329 if (p->owner->_state != AST_STATE_UP) {
04330
04331 usedindex = zt_get_index(p->owner, p, 0);
04332 if (usedindex > -1) {
04333 p->subs[usedindex].needanswer = 1;
04334 }
04335 ast_setstate(p->owner, AST_STATE_UP);
04336 }
04337 p->callwaitingrepeat = 0;
04338 p->cidcwexpire = 0;
04339 if (ast_bridged_channel(p->owner))
04340 ast_moh_stop(ast_bridged_channel(p->owner));
04341 } else
04342 ast_log(LOG_WARNING, "Absorbed on hook, but nobody is left!?!?\n");
04343 update_conf(p);
04344 break;
04345 default:
04346 ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
04347 }
04348 f = &p->subs[index].f;
04349 return f;
04350 }
04351 if (!p->radio) ast_log(LOG_DEBUG, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
04352
04353 if (ast != p->owner) {
04354 ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
04355 f = &p->subs[index].f;
04356 return f;
04357 }
04358 f = zt_handle_event(ast);
04359 return f;
04360 }
04361
04362 struct ast_frame *zt_exception(struct ast_channel *ast)
04363 {
04364 struct zt_pvt *p = ast->tech_pvt;
04365 struct ast_frame *f;
04366 ast_mutex_lock(&p->lock);
04367 f = __zt_exception(ast);
04368 ast_mutex_unlock(&p->lock);
04369 return f;
04370 }
04371
04372 struct ast_frame *zt_read(struct ast_channel *ast)
04373 {
04374 struct zt_pvt *p = ast->tech_pvt;
04375 int res;
04376 int index;
04377 void *readbuf;
04378 struct ast_frame *f;
04379
04380
04381 ast_mutex_lock(&p->lock);
04382
04383 index = zt_get_index(ast, p, 0);
04384
04385
04386 if (index < 0) {
04387 ast_log(LOG_WARNING, "We dont exist?\n");
04388 ast_mutex_unlock(&p->lock);
04389 return NULL;
04390 }
04391
04392 if (p->radio && p->inalarm) return NULL;
04393
04394 p->subs[index].f.frametype = AST_FRAME_NULL;
04395 p->subs[index].f.datalen = 0;
04396 p->subs[index].f.samples = 0;
04397 p->subs[index].f.mallocd = 0;
04398 p->subs[index].f.offset = 0;
04399 p->subs[index].f.subclass = 0;
04400 p->subs[index].f.delivery = ast_tv(0,0);
04401 p->subs[index].f.src = "zt_read";
04402 p->subs[index].f.data = NULL;
04403
04404
04405 if (p->radio && (!p->firstradio))
04406 {
04407 ZT_PARAMS ps;
04408
04409 ps.channo = p->channel;
04410 if (ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
04411 ast_mutex_unlock(&p->lock);
04412 return NULL;
04413 }
04414 p->firstradio = 1;
04415 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04416 if (ps.rxisoffhook)
04417 {
04418 p->subs[index].f.subclass = AST_CONTROL_RADIO_KEY;
04419 }
04420 else
04421 {
04422 p->subs[index].f.subclass = AST_CONTROL_RADIO_UNKEY;
04423 }
04424 ast_mutex_unlock(&p->lock);
04425 return &p->subs[index].f;
04426 }
04427 if (p->ringt == 1) {
04428 ast_mutex_unlock(&p->lock);
04429 return NULL;
04430 }
04431 else if (p->ringt > 0)
04432 p->ringt--;
04433
04434 if (p->subs[index].needringing) {
04435
04436 p->subs[index].needringing = 0;
04437 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04438 p->subs[index].f.subclass = AST_CONTROL_RINGING;
04439 ast_setstate(ast, AST_STATE_RINGING);
04440 ast_mutex_unlock(&p->lock);
04441 return &p->subs[index].f;
04442 }
04443
04444 if (p->subs[index].needbusy) {
04445
04446 p->subs[index].needbusy = 0;
04447 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04448 p->subs[index].f.subclass = AST_CONTROL_BUSY;
04449 ast_mutex_unlock(&p->lock);
04450 return &p->subs[index].f;
04451 }
04452
04453 if (p->subs[index].needcongestion) {
04454
04455 p->subs[index].needcongestion = 0;
04456 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04457 p->subs[index].f.subclass = AST_CONTROL_CONGESTION;
04458 ast_mutex_unlock(&p->lock);
04459 return &p->subs[index].f;
04460 }
04461
04462 if (p->subs[index].needcallerid) {
04463 ast_set_callerid(ast, !ast_strlen_zero(p->lastcid_num) ? p->lastcid_num : NULL,
04464 !ast_strlen_zero(p->lastcid_name) ? p->lastcid_name : NULL,
04465 !ast_strlen_zero(p->lastcid_num) ? p->lastcid_num : NULL
04466 );
04467 p->subs[index].needcallerid = 0;
04468 }
04469
04470 if (p->subs[index].needanswer) {
04471
04472 p->subs[index].needanswer = 0;
04473 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04474 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04475 ast_mutex_unlock(&p->lock);
04476 return &p->subs[index].f;
04477 }
04478
04479 if (p->subs[index].needflash) {
04480
04481 p->subs[index].needflash = 0;
04482 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04483 p->subs[index].f.subclass = AST_CONTROL_FLASH;
04484 ast_mutex_unlock(&p->lock);
04485 return &p->subs[index].f;
04486 }
04487
04488 if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
04489 if (!p->subs[index].linear) {
04490 p->subs[index].linear = 1;
04491 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04492 if (res)
04493 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, index);
04494 }
04495 } else if ((ast->rawreadformat == AST_FORMAT_ULAW) ||
04496 (ast->rawreadformat == AST_FORMAT_ALAW)) {
04497 if (p->subs[index].linear) {
04498 p->subs[index].linear = 0;
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 companded mode.\n", p->channel, index);
04502 }
04503 } else {
04504 ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast->rawreadformat));
04505 ast_mutex_unlock(&p->lock);
04506 return NULL;
04507 }
04508 readbuf = ((unsigned char *)p->subs[index].buffer) + AST_FRIENDLY_OFFSET;
04509 CHECK_BLOCKING(ast);
04510 res = read(p->subs[index].zfd, readbuf, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04511 ast_clear_flag(ast, AST_FLAG_BLOCKING);
04512
04513 if (res < 0) {
04514 f = NULL;
04515 if (res == -1) {
04516 if (errno == EAGAIN) {
04517
04518 ast_mutex_unlock(&p->lock);
04519 return &p->subs[index].f;
04520 } else if (errno == ELAST) {
04521 f = __zt_exception(ast);
04522 } else
04523 ast_log(LOG_WARNING, "zt_rec: %s\n", strerror(errno));
04524 }
04525 ast_mutex_unlock(&p->lock);
04526 return f;
04527 }
04528 if (res != (p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE)) {
04529 ast_log(LOG_DEBUG, "Short read (%d/%d), must be an event...\n", res, p->subs[index].linear ? READ_SIZE * 2 : READ_SIZE);
04530 f = __zt_exception(ast);
04531 ast_mutex_unlock(&p->lock);
04532 return f;
04533 }
04534 if (p->tdd) {
04535 int c;
04536
04537 c = tdd_feed(p->tdd,readbuf,READ_SIZE);
04538 if (c < 0) {
04539 ast_log(LOG_DEBUG,"tdd_feed failed\n");
04540 ast_mutex_unlock(&p->lock);
04541 return NULL;
04542 }
04543 if (c) {
04544 p->subs[index].f.subclass = 0;
04545 p->subs[index].f.frametype = AST_FRAME_TEXT;
04546 p->subs[index].f.mallocd = 0;
04547 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04548 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET;
04549 p->subs[index].f.datalen = 1;
04550 *((char *) p->subs[index].f.data) = c;
04551 ast_mutex_unlock(&p->lock);
04552 return &p->subs[index].f;
04553 }
04554 }
04555 if (p->callwaitingrepeat)
04556 p->callwaitingrepeat--;
04557 if (p->cidcwexpire)
04558 p->cidcwexpire--;
04559
04560 if (p->callwaitingrepeat == 1) {
04561 p->callwaitrings++;
04562 zt_callwait(ast);
04563 }
04564
04565 if (p->cidcwexpire == 1) {
04566 if (option_verbose > 2)
04567 ast_verbose(VERBOSE_PREFIX_3 "CPE does not support Call Waiting Caller*ID.\n");
04568 restore_conference(p);
04569 }
04570 if (p->subs[index].linear) {
04571 p->subs[index].f.datalen = READ_SIZE * 2;
04572 } else
04573 p->subs[index].f.datalen = READ_SIZE;
04574
04575
04576 if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
04577 send_callerid(p);
04578 }
04579
04580 p->subs[index].f.frametype = AST_FRAME_VOICE;
04581 p->subs[index].f.subclass = ast->rawreadformat;
04582 p->subs[index].f.samples = READ_SIZE;
04583 p->subs[index].f.mallocd = 0;
04584 p->subs[index].f.offset = AST_FRIENDLY_OFFSET;
04585 p->subs[index].f.data = p->subs[index].buffer + AST_FRIENDLY_OFFSET/2;
04586 #if 0
04587 ast_log(LOG_DEBUG, "Read %d of voice on %s\n", p->subs[index].f.datalen, ast->name);
04588 #endif
04589 if (p->dialing ||
04590 (index && (ast->_state != AST_STATE_UP)) ||
04591 ((index == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway)
04592 ) {
04593
04594
04595 p->subs[index].f.frametype = AST_FRAME_NULL;
04596 p->subs[index].f.subclass = 0;
04597 p->subs[index].f.samples = 0;
04598 p->subs[index].f.mallocd = 0;
04599 p->subs[index].f.offset = 0;
04600 p->subs[index].f.data = NULL;
04601 p->subs[index].f.datalen= 0;
04602 }
04603 if (p->dsp && (!p->ignoredtmf || p->callwaitcas || p->busydetect || p->callprogress) && !index) {
04604
04605 f = ast_dsp_process(ast, p->dsp, &p->subs[index].f);
04606 if (f) {
04607 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
04608 if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
04609
04610
04611 f = NULL;
04612 }
04613 } else if (f->frametype == AST_FRAME_DTMF) {
04614 #ifdef ZAPATA_PRI
04615 if (!p->proceeding && p->sig==SIG_PRI && p->pri && p->pri->overlapdial) {
04616
04617 f->frametype = AST_FRAME_NULL;
04618 f->subclass = 0;
04619 }
04620 #endif
04621
04622 p->pulsedial = 0;
04623 }
04624 }
04625 } else
04626 f = &p->subs[index].f;
04627 if (f && (f->frametype == AST_FRAME_DTMF)) {
04628 ast_log(LOG_DEBUG, "DTMF digit: %c on %s\n", f->subclass, ast->name);
04629 if (p->confirmanswer) {
04630 ast_log(LOG_DEBUG, "Confirm answer on %s!\n", ast->name);
04631
04632
04633 p->subs[index].f.frametype = AST_FRAME_CONTROL;
04634 p->subs[index].f.subclass = AST_CONTROL_ANSWER;
04635 f = &p->subs[index].f;
04636
04637 p->confirmanswer = 0;
04638 } else if (p->callwaitcas) {
04639 if ((f->subclass == 'A') || (f->subclass == 'D')) {
04640 ast_log(LOG_DEBUG, "Got some DTMF, but it's for the CAS\n");
04641 if (p->cidspill)
04642 free(p->cidspill);
04643 send_cwcidspill(p);
04644 }
04645 if ((f->subclass != 'm') && (f->subclass != 'u'))
04646 p->callwaitcas = 0;
04647 p->subs[index].f.frametype = AST_FRAME_NULL;
04648 p->subs[index].f.subclass = 0;
04649 f = &p->subs[index].f;
04650 } else if (f->subclass == 'f') {
04651
04652 if (!p->faxhandled) {
04653 p->faxhandled++;
04654 if (strcmp(ast->exten, "fax")) {
04655 const char *target_context = ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext;
04656
04657 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
04658 if (option_verbose > 2)
04659 ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
04660
04661 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
04662 if (ast_async_goto(ast, target_context, "fax", 1))
04663 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
04664 } else
04665 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
04666 } else
04667 ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
04668 } else
04669 ast_log(LOG_DEBUG, "Fax already handled\n");
04670 zt_confmute(p, 0);
04671 p->subs[index].f.frametype = AST_FRAME_NULL;
04672 p->subs[index].f.subclass = 0;
04673 f = &p->subs[index].f;
04674 } else if (f->subclass == 'm') {
04675
04676 zt_confmute(p, 1);
04677 p->subs[index].f.frametype = AST_FRAME_NULL;
04678 p->subs[index].f.subclass = 0;
04679 f = &p->subs[index].f;
04680 } else if (f->subclass == 'u') {
04681
04682 zt_confmute(p, 0);
04683 p->subs[index].f.frametype = AST_FRAME_NULL;
04684 p->subs[index].f.subclass = 0;
04685 f = &p->subs[index].f;
04686 } else
04687 zt_confmute(p, 0);
04688 }
04689
04690
04691 if (p->fake_event)
04692 ast_set_flag(ast, AST_FLAG_EXCEPTION);
04693
04694 ast_mutex_unlock(&p->lock);
04695 return f;
04696 }
04697
04698 static int my_zt_write(struct zt_pvt *p, unsigned char *buf, int len, int index, int linear)
04699 {
04700 int sent=0;
04701 int size;
04702 int res;
04703 int fd;
04704 fd = p->subs[index].zfd;
04705 while(len) {
04706 size = len;
04707 if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
04708 size = (linear ? READ_SIZE * 2 : READ_SIZE);
04709 res = write(fd, buf, size);
04710 if (res != size) {
04711 if (option_debug)
04712 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
04713 return sent;
04714 }
04715 len -= size;
04716 buf += size;
04717 }
04718 return sent;
04719 }
04720
04721 static int zt_write(struct ast_channel *ast, struct ast_frame *frame)
04722 {
04723 struct zt_pvt *p = ast->tech_pvt;
04724 int res;
04725 unsigned char outbuf[4096];
04726 int index;
04727 index = zt_get_index(ast, p, 0);
04728 if (index < 0) {
04729 ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
04730 return -1;
04731 }
04732
04733 #if 0
04734 #ifdef ZAPATA_PRI
04735 ast_mutex_lock(&p->lock);
04736 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04737 if (p->pri->pri) {
04738 if (!pri_grab(p, p->pri)) {
04739 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04740 pri_rel(p->pri);
04741 } else
04742 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04743 }
04744 p->proceeding=1;
04745 }
04746 ast_mutex_unlock(&p->lock);
04747 #endif
04748 #endif
04749
04750 if (frame->frametype != AST_FRAME_VOICE) {
04751 if (frame->frametype != AST_FRAME_IMAGE)
04752 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
04753 return 0;
04754 }
04755 if ((frame->subclass != AST_FORMAT_SLINEAR) &&
04756 (frame->subclass != AST_FORMAT_ULAW) &&
04757 (frame->subclass != AST_FORMAT_ALAW)) {
04758 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
04759 return -1;
04760 }
04761 if (p->dialing) {
04762 if (option_debug)
04763 ast_log(LOG_DEBUG, "Dropping frame since I'm still dialing on %s...\n",ast->name);
04764 return 0;
04765 }
04766 if (!p->owner) {
04767 if (option_debug)
04768 ast_log(LOG_DEBUG, "Dropping frame since there is no active owner on %s...\n",ast->name);
04769 return 0;
04770 }
04771 if (p->cidspill) {
04772 if (option_debug)
04773 ast_log(LOG_DEBUG, "Dropping frame since I've still got a callerid spill\n");
04774 return 0;
04775 }
04776
04777 if (!frame->data || !frame->datalen)
04778 return 0;
04779 if (frame->datalen > sizeof(outbuf) * 2) {
04780 ast_log(LOG_WARNING, "Frame too large\n");
04781 return 0;
04782 }
04783
04784 if (frame->subclass == AST_FORMAT_SLINEAR) {
04785 if (!p->subs[index].linear) {
04786 p->subs[index].linear = 1;
04787 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04788 if (res)
04789 ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
04790 }
04791 res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 1);
04792 } else {
04793
04794 if (p->subs[index].linear) {
04795 p->subs[index].linear = 0;
04796 res = zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
04797 if (res)
04798 ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
04799 }
04800 res = my_zt_write(p, (unsigned char *)frame->data, frame->datalen, index, 0);
04801 }
04802 if (res < 0) {
04803 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
04804 return -1;
04805 }
04806 return 0;
04807 }
04808
04809 static int zt_indicate(struct ast_channel *chan, int condition)
04810 {
04811 struct zt_pvt *p = chan->tech_pvt;
04812 int res=-1;
04813 int index;
04814 int func = ZT_FLASH;
04815 ast_mutex_lock(&p->lock);
04816 index = zt_get_index(chan, p, 0);
04817 ast_log(LOG_DEBUG, "Requested indication %d on channel %s\n", condition, chan->name);
04818 if (index == SUB_REAL) {
04819 switch(condition) {
04820 case AST_CONTROL_BUSY:
04821 #ifdef ZAPATA_PRI
04822 if (p->priindication_oob && p->sig == SIG_PRI) {
04823 chan->hangupcause = AST_CAUSE_USER_BUSY;
04824 chan->_softhangup |= AST_SOFTHANGUP_DEV;
04825 res = 0;
04826 } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04827 if (p->pri->pri) {
04828 if (!pri_grab(p, p->pri)) {
04829 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04830 pri_rel(p->pri);
04831 }
04832 else
04833 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04834 }
04835 p->progress = 1;
04836 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
04837 } else
04838 #endif
04839 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_BUSY);
04840 break;
04841 case AST_CONTROL_RINGING:
04842 #ifdef ZAPATA_PRI
04843 if ((!p->alerting) && p->sig==SIG_PRI && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
04844 if (p->pri->pri) {
04845 if (!pri_grab(p, p->pri)) {
04846 pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04847 pri_rel(p->pri);
04848 }
04849 else
04850 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04851 }
04852 p->alerting = 1;
04853 }
04854 #endif
04855 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_RINGTONE);
04856 if (chan->_state != AST_STATE_UP) {
04857 if ((chan->_state != AST_STATE_RING) ||
04858 ((p->sig != SIG_FXSKS) &&
04859 (p->sig != SIG_FXSLS) &&
04860 (p->sig != SIG_FXSGS)))
04861 ast_setstate(chan, AST_STATE_RINGING);
04862 }
04863 break;
04864 case AST_CONTROL_PROCEEDING:
04865 ast_log(LOG_DEBUG,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
04866 #ifdef ZAPATA_PRI
04867 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04868 if (p->pri->pri) {
04869 if (!pri_grab(p, p->pri)) {
04870 pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
04871 pri_rel(p->pri);
04872 }
04873 else
04874 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04875 }
04876 p->proceeding = 1;
04877 }
04878 #endif
04879
04880 res = 0;
04881 break;
04882 case AST_CONTROL_PROGRESS:
04883 ast_log(LOG_DEBUG,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
04884 #ifdef ZAPATA_PRI
04885 p->digital = 0;
04886 if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04887 if (p->pri->pri) {
04888 if (!pri_grab(p, p->pri)) {
04889 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04890 pri_rel(p->pri);
04891 }
04892 else
04893 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04894 }
04895 p->progress = 1;
04896 }
04897 #endif
04898
04899 res = 0;
04900 break;
04901 case AST_CONTROL_CONGESTION:
04902 chan->hangupcause = AST_CAUSE_CONGESTION;
04903 #ifdef ZAPATA_PRI
04904 if (p->priindication_oob && p->sig == SIG_PRI) {
04905 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
04906 chan->_softhangup |= AST_SOFTHANGUP_DEV;
04907 res = 0;
04908 } else if (!p->progress && p->sig==SIG_PRI && p->pri && !p->outgoing) {
04909 if (p->pri) {
04910 if (!pri_grab(p, p->pri)) {
04911 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
04912 pri_rel(p->pri);
04913 } else
04914 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04915 }
04916 p->progress = 1;
04917 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
04918 } else
04919 #endif
04920 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
04921 break;
04922 #ifdef ZAPATA_PRI
04923 case AST_CONTROL_HOLD:
04924 if (p->pri) {
04925 if (!pri_grab(p, p->pri)) {
04926 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
04927 pri_rel(p->pri);
04928 } else
04929 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04930 }
04931 break;
04932 case AST_CONTROL_UNHOLD:
04933 if (p->pri) {
04934 if (!pri_grab(p, p->pri)) {
04935 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
04936 pri_rel(p->pri);
04937 } else
04938 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04939 }
04940 break;
04941 #endif
04942 case AST_CONTROL_RADIO_KEY:
04943 if (p->radio)
04944 res = zt_set_hook(p->subs[index].zfd, ZT_OFFHOOK);
04945 res = 0;
04946 break;
04947 case AST_CONTROL_RADIO_UNKEY:
04948 if (p->radio)
04949 res = zt_set_hook(p->subs[index].zfd, ZT_RINGOFF);
04950 res = 0;
04951 break;
04952 case AST_CONTROL_FLASH:
04953
04954 if (ISTRUNK(p) && (p->sig != SIG_PRI)) {
04955
04956 p->dop.dialstr[0] = '\0';
04957 if ((ioctl(p->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
04958 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
04959 chan->name, strerror(errno));
04960 } else
04961 res = 0;
04962 } else
04963 res = 0;
04964 break;
04965 case -1:
04966 res = tone_zone_play_tone(p->subs[index].zfd, -1);
04967 break;
04968 }
04969 } else
04970 res = 0;
04971 ast_mutex_unlock(&p->lock);
04972 return res;
04973 }
04974
04975 static struct ast_channel *zt_new(struct zt_pvt *i, int state, int startpbx, int index, int law, int transfercapability)
04976 {
04977 struct ast_channel *tmp;
04978 int deflaw;
04979 int res;
04980 int x,y;
04981 int features;
04982 ZT_PARAMS ps;
04983 if (i->subs[index].owner) {
04984 ast_log(LOG_WARNING, "Channel %d already has a %s call\n", i->channel,subnames[index]);
04985 return NULL;
04986 }
04987 tmp = ast_channel_alloc(0);
04988 if (tmp) {
04989 tmp->tech = &zap_tech;
04990 ps.channo = i->channel;
04991 res = ioctl(i->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps);
04992 if (res) {
04993 ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW\n");
04994 ps.curlaw = ZT_LAW_MULAW;
04995 }
04996 if (ps.curlaw == ZT_LAW_ALAW)
04997 deflaw = AST_FORMAT_ALAW;
04998 else
04999 deflaw = AST_FORMAT_ULAW;
05000 if (law) {
05001 if (law == ZT_LAW_ALAW)
05002 deflaw = AST_FORMAT_ALAW;
05003 else
05004 deflaw = AST_FORMAT_ULAW;
05005 }
05006 y = 1;
05007 do {
05008 #ifdef ZAPATA_PRI
05009 if (i->bearer || (i->pri && (i->sig == SIG_FXSKS)))
05010 snprintf(tmp->name, sizeof(tmp->name), "Zap/%d:%d-%d", i->pri->trunkgroup, i->channel, y);
05011 else
05012 #endif
05013 if (i->channel == CHAN_PSEUDO)
05014 snprintf(tmp->name, sizeof(tmp->name), "Zap/pseudo-%d", rand());
05015 else
05016 snprintf(tmp->name, sizeof(tmp->name), "Zap/%d-%d", i->channel, y);
05017 for (x=0;x<3;x++) {
05018 if ((index != x) && i->subs[x].owner && !strcasecmp(tmp->name, i->subs[x].owner->name))
05019 break;
05020 }
05021 y++;
05022 } while (x < 3);
05023 tmp->type = type;
05024 tmp->fds[0] = i->subs[index].zfd;
05025 tmp->nativeformats = AST_FORMAT_SLINEAR | deflaw;
05026
05027 tmp->rawreadformat = deflaw;
05028 tmp->readformat = deflaw;
05029 tmp->rawwriteformat = deflaw;
05030 tmp->writeformat = deflaw;
05031 i->subs[index].linear = 0;
05032 zt_setlinear(i->subs[index].zfd, i->subs[index].linear);
05033 features = 0;
05034 if (i->busydetect && CANBUSYDETECT(i)) {
05035 features |= DSP_FEATURE_BUSY_DETECT;
05036 }
05037 if ((i->callprogress & 1) && CANPROGRESSDETECT(i)) {
05038 features |= DSP_FEATURE_CALL_PROGRESS;
05039 }
05040 if ((!i->outgoing && (i->callprogress & 4)) ||
05041 (i->outgoing && (i->callprogress & 2))) {
05042 features |= DSP_FEATURE_FAX_DETECT;
05043 }
05044 #ifdef ZT_TONEDETECT
05045 x = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
05046 if (ioctl(i->subs[index].zfd, ZT_TONEDETECT, &x)) {
05047 #endif
05048 i->hardwaredtmf = 0;
05049 features |= DSP_FEATURE_DTMF_DETECT;
05050 #ifdef ZT_TONEDETECT
05051 } else if (NEED_MFDETECT(i)) {
05052 i->hardwaredtmf = 1;
05053 features |= DSP_FEATURE_DTMF_DETECT;
05054 }
05055 #endif
05056 if (features) {
05057 if (i->dsp) {
05058 ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", tmp->name);
05059 } else {
05060 if (i->channel != CHAN_PSEUDO)
05061 i->dsp = ast_dsp_new();
05062 else
05063 i->dsp = NULL;
05064 if (i->dsp) {
05065 i->dsp_features = features & ~DSP_PROGRESS_TALK;
05066 #ifdef ZAPATA_PRI
05067
05068 if (i->outgoing && (i->sig == SIG_PRI)) {
05069
05070
05071 features = 0;
05072 }
05073 #endif
05074 ast_dsp_set_features(i->dsp, features);
05075 ast_dsp_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax);
05076 if (!ast_strlen_zero(progzone))
05077 ast_dsp_set_call_progress_zone(i->dsp, progzone);
05078 if (i->busydetect && CANBUSYDETECT(i)) {
05079 ast_dsp_set_busy_count(i->dsp, i->busycount);
05080 ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength);
05081 }
05082 }
05083 }
05084 }
05085
05086 if (state == AST_STATE_RING)
05087 tmp->rings = 1;
05088 tmp->tech_pvt = i;
05089 if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) {
05090
05091 tmp->callgroup = i->callgroup;
05092 tmp->pickupgroup = i->pickupgroup;
05093 }
05094 if (!ast_strlen_zero(i->language))
05095 ast_copy_string(tmp->language, i->language, sizeof(tmp->language));
05096 if (!ast_strlen_zero(i->musicclass))
05097 ast_copy_string(tmp->musicclass, i->musicclass, sizeof(tmp->musicclass));
05098 if (!i->owner)
05099 i->owner = tmp;
05100 if (!ast_strlen_zero(i->accountcode))
05101 ast_copy_string(tmp->accountcode, i->accountcode, sizeof(tmp->accountcode));
05102 if (i->amaflags)
05103 tmp->amaflags = i->amaflags;
05104 i->subs[index].owner = tmp;
05105 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
05106
05107 ast_copy_string(tmp->call_forward, i->call_forward, sizeof(tmp->call_forward));
05108
05109 if (!i->adsi)
05110 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
05111 if (!ast_strlen_zero(i->exten))
05112 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
05113 if (!ast_strlen_zero(i->rdnis))
05114 tmp->cid.cid_rdnis = strdup(i->rdnis);
05115 if (!ast_strlen_zero(i->dnid))
05116 tmp->cid.cid_dnid = strdup(i->dnid);
05117
05118 #ifdef PRI_ANI
05119 ast_set_callerid(tmp, i->cid_num, i->cid_name, ast_strlen_zero(i->cid_ani) ? i->cid_num : i->cid_ani);
05120 #else
05121 ast_set_callerid(tmp, i->cid_num, i->cid_name, i->cid_num);
05122 #endif
05123 tmp->cid.cid_pres = i->callingpres;
05124 tmp->cid.cid_ton = i->cid_ton;
05125 #ifdef ZAPATA_PRI
05126 tmp->transfercapability = transfercapability;
05127 pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
05128 if (transfercapability & PRI_TRANS_CAP_DIGITAL) {
05129 i->digital = 1;
05130 }
05131
05132 i->isidlecall = 0;
05133 i->alreadyhungup = 0;
05134 #endif
05135
05136 i->fake_event = 0;
05137
05138 zt_confmute(i, 0);
05139 ast_setstate(tmp, state);
05140 ast_mutex_lock(&usecnt_lock);
05141 usecnt++;
05142 ast_mutex_unlock(&usecnt_lock);
05143 ast_update_use_count();
05144 if (startpbx) {
05145 if (ast_pbx_start(tmp)) {
05146 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
05147 ast_hangup(tmp);
05148 tmp = NULL;
05149 }
05150 }
05151 } else
05152 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
05153 return tmp;
05154 }
05155
05156
05157 static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
05158 {
05159 char c;
05160
05161 *str = 0;
05162 for (;;)
05163 {
05164
05165 c = ast_waitfordigit(chan, ms);
05166
05167 if (c < 1)
05168 return c;
05169 *str++ = c;
05170 *str = 0;
05171 if (strchr(term, c))
05172 return 1;
05173 }
05174 }
05175
05176 static int zt_wink(struct zt_pvt *p, int index)
05177 {
05178 int j;
05179 zt_set_hook(p->subs[index].zfd, ZT_WINK);
05180 for(;;)
05181 {
05182
05183 j = ZT_IOMUX_SIGEVENT;
05184
05185 if (ioctl(p->subs[index].zfd,ZT_IOMUX,&j) == -1) return(-1);
05186
05187 if (j & ZT_IOMUX_SIGEVENT) break;
05188 }
05189
05190 if (ioctl(p->subs[index].zfd,ZT_GETEVENT,&j) == -1) return(-1);
05191 return 0;
05192 }
05193
05194 static void *ss_thread(void *data)
05195 {
05196 struct ast_channel *chan = data;
05197 struct zt_pvt *p = chan->tech_pvt;
05198 char exten[AST_MAX_EXTENSION]="";
05199 char exten2[AST_MAX_EXTENSION]="";
05200 unsigned char buf[256];
05201 char dtmfcid[300];
05202 char dtmfbuf[300];
05203 struct callerid_state *cs;
05204 char *name=NULL, *number=NULL;
05205 int distMatches;
05206 int curRingData[3];
05207 int receivedRingT;
05208 int counter1;
05209 int counter;
05210 int samples = 0;
05211
05212 int flags;
05213 int i;
05214 int timeout;
05215 int getforward=0;
05216 char *s1, *s2;
05217 int len = 0;
05218 int res;
05219 int index;
05220 if (option_verbose > 2)
05221 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name);
05222 index = zt_get_index(chan, p, 1);
05223 if (index < 0) {
05224 ast_log(LOG_WARNING, "Huh?\n");
05225 ast_hangup(chan);
05226 return NULL;
05227 }
05228 if (p->dsp)
05229 ast_dsp_digitreset(p->dsp);
05230 switch(p->sig) {
05231 #ifdef ZAPATA_PRI
05232 case SIG_PRI:
05233
05234 ast_copy_string(exten, p->exten, sizeof(exten));
05235 len = strlen(exten);
05236 res = 0;
05237 while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05238 if (len && !ast_ignore_pattern(chan->context, exten))
05239 tone_zone_play_tone(p->subs[index].zfd, -1);
05240 else
05241 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05242 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
05243 timeout = matchdigittimeout;
05244 else
05245 timeout = gendigittimeout;
05246 res = ast_waitfordigit(chan, timeout);
05247 if (res < 0) {
05248 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05249 ast_hangup(chan);
05250 return NULL;
05251 } else if (res) {
05252 exten[len++] = res;
05253 exten[len] = '\0';
05254 } else
05255 break;
05256 }
05257
05258 if (ast_strlen_zero(exten)) {
05259 if (option_verbose > 2)
05260 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n");
05261 exten[0] = 's';
05262 exten[1] = '\0';
05263 }
05264 tone_zone_play_tone(p->subs[index].zfd, -1);
05265 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
05266
05267 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05268 if (p->dsp) ast_dsp_digitreset(p->dsp);
05269 zt_enable_ec(p);
05270 ast_setstate(chan, AST_STATE_RING);
05271 res = ast_pbx_run(chan);
05272 if (res) {
05273 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
05274 }
05275 } else {
05276 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
05277 chan->hangupcause = AST_CAUSE_UNALLOCATED;
05278 ast_hangup(chan);
05279 p->exten[0] = '\0';
05280
05281 p->call = NULL;
05282 }
05283 return NULL;
05284 break;
05285 #endif
05286 case SIG_FEATD:
05287 case SIG_FEATDMF:
05288 case SIG_E911:
05289 case SIG_FEATB:
05290 case SIG_EMWINK:
05291 case SIG_SF_FEATD:
05292 case SIG_SF_FEATDMF:
05293 case SIG_SF_FEATB:
05294 case SIG_SFWINK:
05295 if (zt_wink(p, index))
05296 return NULL;
05297
05298 case SIG_EM:
05299 case SIG_EM_E1:
05300 case SIG_SF:
05301 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05302 if (p->dsp)
05303 ast_dsp_digitreset(p->dsp);
05304
05305 if (p->dsp) {
05306 if (NEED_MFDETECT(p))
05307 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax);
05308 else
05309 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05310 }
05311 memset(dtmfbuf, 0, sizeof(dtmfbuf));
05312
05313 if (!p->immediate)
05314
05315 res = ast_waitfordigit(chan, 5000);
05316 else res = 0;
05317 if (res > 0) {
05318
05319 dtmfbuf[0] = res;
05320 switch(p->sig) {
05321 case SIG_FEATD:
05322 case SIG_SF_FEATD:
05323 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05324 if (res > 0)
05325 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05326 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05327 break;
05328 case SIG_FEATDMF:
05329 case SIG_E911:
05330 case SIG_SF_FEATDMF:
05331 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05332 if (res > 0) {
05333
05334 if (p->sig == SIG_E911)
05335 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
05336 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
05337 }
05338 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05339 break;
05340 case SIG_FEATB:
05341 case SIG_SF_FEATB:
05342 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05343 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05344 break;
05345 case SIG_EMWINK:
05346
05347
05348
05349
05350 if (res == '*') {
05351 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05352 if (res > 0)
05353 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05354 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05355 break;
05356 }
05357 default:
05358
05359 len = 1;
05360 dtmfbuf[len] = '\0';
05361 while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05362 if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05363 timeout = matchdigittimeout;
05364 } else {
05365 timeout = gendigittimeout;
05366 }
05367 res = ast_waitfordigit(chan, timeout);
05368 if (res < 0) {
05369 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05370 ast_hangup(chan);
05371 return NULL;
05372 } else if (res) {
05373 dtmfbuf[len++] = res;
05374 dtmfbuf[len] = '\0';
05375 } else {
05376 break;
05377 }
05378 }
05379 break;
05380 }
05381 }
05382 if (res == -1) {
05383 ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
05384 ast_hangup(chan);
05385 return NULL;
05386 } else if (res < 0) {
05387 ast_log(LOG_DEBUG, "Got hung up before digits finished\n");
05388 ast_hangup(chan);
05389 return NULL;
05390 }
05391 ast_copy_string(exten, dtmfbuf, sizeof(exten));
05392 if (ast_strlen_zero(exten))
05393 ast_copy_string(exten, "s", sizeof(exten));
05394 if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
05395
05396 if (exten[0] == '*') {
05397 char *stringp=NULL;
05398 ast_copy_string(exten2, exten, sizeof(exten2));
05399
05400 stringp=exten2 +1;
05401 s1 = strsep(&stringp, "*");
05402 s2 = strsep(&stringp, "*");
05403 if (s2) {
05404 if (!ast_strlen_zero(p->cid_num))
05405 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05406 else
05407 ast_set_callerid(chan, s1, NULL, s1);
05408 ast_copy_string(exten, s2, sizeof(exten));
05409 } else
05410 ast_copy_string(exten, s1, sizeof(exten));
05411 } else if (p->sig == SIG_FEATD)
05412 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05413 }
05414 if (p->sig == SIG_FEATDMF) {
05415 if (exten[0] == '*') {
05416 char *stringp=NULL;
05417 ast_copy_string(exten2, exten, sizeof(exten2));
05418
05419 stringp=exten2 +1;
05420 s1 = strsep(&stringp, "#");
05421 s2 = strsep(&stringp, "#");
05422 if (s2) {
05423 if (!ast_strlen_zero(p->cid_num))
05424 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05425 else
05426 if(*(s1 + 2))
05427 ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
05428 ast_copy_string(exten, s2 + 1, sizeof(exten));
05429 } else
05430 ast_copy_string(exten, s1 + 2, sizeof(exten));
05431 } else
05432 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05433 }
05434 if (p->sig == SIG_E911) {
05435 if (exten[0] == '*') {
05436 char *stringp=NULL;
05437 ast_copy_string(exten2, exten, sizeof(exten2));
05438
05439 stringp=exten2 +1;
05440 s1 = strsep(&stringp, "#");
05441 s2 = strsep(&stringp, "#");
05442 if (s2 && (*(s2 + 1) == '0')) {
05443 if(*(s2 + 2))
05444 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
05445 }
05446 if (s1) ast_copy_string(exten, s1, sizeof(exten));
05447 else ast_copy_string(exten, "911", sizeof(exten));
05448 printf("E911: exten: %s, ANI: %s\n",exten, chan->cid.cid_ani);
05449 } else
05450 ast_log(LOG_WARNING, "Got a non-E911 input on channel %d. Assuming E&M Wink instead\n", p->channel);
05451 }
05452 if (p->sig == SIG_FEATB) {
05453 if (exten[0] == '*') {
05454 char *stringp=NULL;
05455 ast_copy_string(exten2, exten, sizeof(exten2));
05456
05457 stringp=exten2 +1;
05458 s1 = strsep(&stringp, "#");
05459 ast_copy_string(exten, exten2 + 1, sizeof(exten));
05460 } else
05461 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
05462 }
05463 if (p->sig == SIG_FEATDMF) {
05464 zt_wink(p, index);
05465 }
05466 zt_enable_ec(p);
05467 if (NEED_MFDETECT(p)) {
05468 if (p->dsp) {
05469 if (!p->hardwaredtmf)
05470 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05471 else {
05472 ast_dsp_free(p->dsp);
05473 p->dsp = NULL;
05474 }
05475 }
05476 }
05477
05478 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
05479 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05480 if (p->dsp) ast_dsp_digitreset(p->dsp);
05481 res = ast_pbx_run(chan);
05482 if (res) {
05483 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05484 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05485 }
05486 return NULL;
05487 } else {
05488 if (option_verbose > 2)
05489 ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
05490 sleep(2);
05491 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_INFO);
05492 if (res < 0)
05493 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
05494 else
05495 sleep(1);
05496 res = ast_streamfile(chan, "ss-noservice", chan->language);
05497 if (res >= 0)
05498 ast_waitstream(chan, "");
05499 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05500 ast_hangup(chan);
05501 return NULL;
05502 }
05503 break;
05504 case SIG_FXOLS:
05505 case SIG_FXOGS:
05506 case SIG_FXOKS:
05507
05508 timeout = firstdigittimeout;
05509
05510
05511 if (p->subs[SUB_THREEWAY].owner)
05512 timeout = 999999;
05513 while(len < AST_MAX_EXTENSION-1) {
05514
05515
05516 if (p->immediate)
05517 res = 's';
05518 else
05519 res = ast_waitfordigit(chan, timeout);
05520 timeout = 0;
05521 if (res < 0) {
05522 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05523 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05524 ast_hangup(chan);
05525 return NULL;
05526 } else if (res) {
05527 exten[len++]=res;
05528 exten[len] = '\0';
05529 }
05530 if (!ast_ignore_pattern(chan->context, exten))
05531 tone_zone_play_tone(p->subs[index].zfd, -1);
05532 else
05533 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05534 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
05535 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05536 if (getforward) {
05537
05538 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
05539 if (option_verbose > 2)
05540 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
05541 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05542 if (res)
05543 break;
05544 usleep(500000);
05545 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05546 sleep(1);
05547 memset(exten, 0, sizeof(exten));
05548 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05549 len = 0;
05550 getforward = 0;
05551 } else {
05552 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05553 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05554 if (!ast_strlen_zero(p->cid_num)) {
05555 if (!p->hidecallerid)
05556 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05557 else
05558 ast_set_callerid(chan, NULL, NULL, p->cid_num);
05559 }
05560 if (!ast_strlen_zero(p->cid_name)) {
05561 if (!p->hidecallerid)
05562 ast_set_callerid(chan, NULL, p->cid_name, NULL);
05563 }
05564 ast_setstate(chan, AST_STATE_RING);
05565 zt_enable_ec(p);
05566 res = ast_pbx_run(chan);
05567 if (res) {
05568 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05569 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05570 }
05571 return NULL;
05572 }
05573 } else {
05574
05575
05576 timeout = matchdigittimeout;
05577 }
05578 } else if (res == 0) {
05579 ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n");
05580 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05581 zt_wait_event(p->subs[index].zfd);
05582 ast_hangup(chan);
05583 return NULL;
05584 } else if (p->callwaiting && !strcmp(exten, "*70")) {
05585 if (option_verbose > 2)
05586 ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
05587
05588 p->callwaiting = 0;
05589 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05590 if (res) {
05591 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05592 chan->name, strerror(errno));
05593 }
05594 len = 0;
05595 ioctl(p->subs[index].zfd,ZT_CONFDIAG,&len);
05596 memset(exten, 0, sizeof(exten));
05597 timeout = firstdigittimeout;
05598
05599 } else if (!strcmp(exten,ast_pickup_ext())) {
05600
05601
05602
05603
05604 if (index == SUB_REAL) {
05605
05606 if (p->subs[SUB_THREEWAY].owner) {
05607
05608
05609 alloc_sub(p, SUB_CALLWAIT);
05610 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
05611 unalloc_sub(p, SUB_THREEWAY);
05612 }
05613 zt_enable_ec(p);
05614 if (ast_pickup_call(chan)) {
05615 ast_log(LOG_DEBUG, "No call pickup possible...\n");
05616 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05617 zt_wait_event(p->subs[index].zfd);
05618 }
05619 ast_hangup(chan);
05620 return NULL;
05621 } else {
05622 ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
05623 ast_hangup(chan);
05624 return NULL;
05625 }
05626
05627 } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
05628 if (option_verbose > 2)
05629 ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
05630
05631 p->hidecallerid = 1;
05632 if (chan->cid.cid_num)
05633 free(chan->cid.cid_num);
05634 chan->cid.cid_num = NULL;
05635 if (chan->cid.cid_name)
05636 free(chan->cid.cid_name);
05637 chan->cid.cid_name = NULL;
05638 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05639 if (res) {
05640 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05641 chan->name, strerror(errno));
05642 }
05643 len = 0;
05644 memset(exten, 0, sizeof(exten));
05645 timeout = firstdigittimeout;
05646 } else if (p->callreturn && !strcmp(exten, "*69")) {
05647 res = 0;
05648 if (!ast_strlen_zero(p->lastcid_num)) {
05649 res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
05650 }
05651 if (!res)
05652 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05653 break;
05654 } else if (!strcmp(exten, "*78")) {
05655
05656 if (option_verbose > 2) {
05657 ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel);
05658 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05659 "Channel: Zap/%d\r\n"
05660 "Status: enabled\r\n", p->channel);
05661 }
05662 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05663 p->dnd = 1;
05664 getforward = 0;
05665 memset(exten, 0, sizeof(exten));
05666 len = 0;
05667 } else if (!strcmp(exten, "*79")) {
05668
05669 if (option_verbose > 2) {
05670 ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel);
05671 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05672 "Channel: Zap/%d\r\n"
05673 "Status: disabled\r\n", p->channel);
05674 }
05675 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05676 p->dnd = 0;
05677 getforward = 0;
05678 memset(exten, 0, sizeof(exten));
05679 len = 0;
05680 } else if (p->cancallforward && !strcmp(exten, "*72")) {
05681 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05682 getforward = 1;
05683 memset(exten, 0, sizeof(exten));
05684 len = 0;
05685 } else if (p->cancallforward && !strcmp(exten, "*73")) {
05686 if (option_verbose > 2)
05687 ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel);
05688 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05689 memset(p->call_forward, 0, sizeof(p->call_forward));
05690 getforward = 0;
05691 memset(exten, 0, sizeof(exten));
05692 len = 0;
05693 } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
05694 p->subs[SUB_THREEWAY].owner &&
05695 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
05696
05697
05698 ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL);
05699 if (option_verbose > 2)
05700 ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
05701 break;
05702 } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
05703 if (option_verbose > 2)
05704 ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num);
05705 res = ast_db_put("blacklist", p->lastcid_num, "1");
05706 if (!res) {
05707 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05708 memset(exten, 0, sizeof(exten));
05709 len = 0;
05710 }
05711 } else if (p->hidecallerid && !strcmp(exten, "*82")) {
05712 if (option_verbose > 2)
05713 ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
05714
05715 p->hidecallerid = 0;
05716 if (chan->cid.cid_num)
05717 free(chan->cid.cid_num);
05718 chan->cid.cid_num = NULL;
05719 if (chan->cid.cid_name)
05720 free(chan->cid.cid_name);
05721 chan->cid.cid_name = NULL;
05722 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
05723 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05724 if (res) {
05725 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05726 chan->name, strerror(errno));
05727 }
05728 len = 0;
05729 memset(exten, 0, sizeof(exten));
05730 timeout = firstdigittimeout;
05731 } else if (!strcmp(exten, "*0")) {
05732 struct ast_channel *nbridge =
05733 p->subs[SUB_THREEWAY].owner;
05734 struct zt_pvt *pbridge = NULL;
05735
05736 if (nbridge && ast_bridged_channel(nbridge))
05737 pbridge = ast_bridged_channel(nbridge)->tech_pvt;
05738 if (nbridge && pbridge &&
05739 (!strcmp(nbridge->type,"Zap")) &&
05740 (!strcmp(ast_bridged_channel(nbridge)->type, "Zap")) &&
05741 ISTRUNK(pbridge)) {
05742 int func = ZT_FLASH;
05743
05744 p->dop.dialstr[0] = '\0';
05745
05746 if ((ioctl(pbridge->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
05747 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
05748 nbridge->name, strerror(errno));
05749 }
05750 swap_subs(p, SUB_REAL, SUB_THREEWAY);
05751 unalloc_sub(p, SUB_THREEWAY);
05752 p->owner = p->subs[SUB_REAL].owner;
05753 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
05754 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
05755 ast_hangup(chan);
05756 return NULL;
05757 } else {
05758 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05759 zt_wait_event(p->subs[index].zfd);
05760 tone_zone_play_tone(p->subs[index].zfd, -1);
05761 swap_subs(p, SUB_REAL, SUB_THREEWAY);
05762 unalloc_sub(p, SUB_THREEWAY);
05763 p->owner = p->subs[SUB_REAL].owner;
05764 ast_hangup(chan);
05765 return NULL;
05766 }
05767 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
05768 ((exten[0] != '*') || (strlen(exten) > 2))) {
05769 if (option_debug)
05770 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);
05771 break;
05772 }
05773 if (!timeout)
05774 timeout = gendigittimeout;
05775 if (len && !ast_ignore_pattern(chan->context, exten))
05776 tone_zone_play_tone(p->subs[index].zfd, -1);
05777 }
05778 break;
05779 case SIG_FXSLS:
05780 case SIG_FXSGS:
05781 case SIG_FXSKS:
05782 #ifdef ZAPATA_PRI
05783 if (p->pri) {
05784
05785 struct ast_frame *f;
05786 int res;
05787 time_t start;
05788
05789 time(&start);
05790 ast_setstate(chan, AST_STATE_RING);
05791 while(time(NULL) < start + 3) {
05792 res = ast_waitfor(chan, 1000);
05793 if (res) {
05794 f = ast_read(chan);
05795 if (!f) {
05796 ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
05797 ast_hangup(chan);
05798 return NULL;
05799 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
05800 res = 1;
05801 } else
05802 res = 0;
05803 ast_frfree(f);
05804 if (res) {
05805 ast_log(LOG_DEBUG, "Got ring!\n");
05806 res = 0;
05807 break;
05808 }
05809 }
05810 }
05811 }
05812 #endif
05813
05814
05815
05816 if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) {
05817
05818 if (p->cid_signalling == CID_SIG_DTMF) {
05819 int i = 0;
05820 cs = NULL;
05821 ast_log(LOG_DEBUG, "Receiving DTMF cid on "
05822 "channel %s\n", chan->name);
05823 zt_setlinear(p->subs[index].zfd, 0);
05824 res = 2000;
05825 for (;;) {
05826 struct ast_frame *f;
05827 res = ast_waitfor(chan, res);
05828 if (res <= 0) {
05829 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
05830 "Exiting simple switch\n");
05831 ast_hangup(chan);
05832 return NULL;
05833 }
05834 f = ast_read(chan);
05835 if (f->frametype == AST_FRAME_DTMF) {
05836 dtmfbuf[i++] = f->subclass;
05837 ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
05838 res = 2000;
05839 }
05840 ast_frfree(f);
05841 if (chan->_state == AST_STATE_RING ||
05842 chan->_state == AST_STATE_RINGING)
05843 break;
05844 }
05845 dtmfbuf[i] = 0;
05846 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
05847
05848 ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf);
05849 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
05850 ast_log(LOG_DEBUG, "CID is '%s', flags %d\n",
05851 dtmfcid, flags);
05852
05853 if (dtmfcid[0])
05854 number = dtmfcid;
05855 else
05856 number = 0;
05857
05858 } else if (p->cid_signalling == CID_SIG_V23) {
05859 cs = callerid_new(cid_signalling);
05860 if (cs) {
05861 samples = 0;
05862 #if 1
05863 bump_gains(p);
05864 #endif
05865
05866 zt_setlinear(p->subs[index].zfd, 0);
05867
05868
05869 for(;;) {
05870 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
05871 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
05872 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
05873 callerid_free(cs);
05874 ast_hangup(chan);
05875 return NULL;
05876 }
05877 if (i & ZT_IOMUX_SIGEVENT) {
05878 res = zt_get_event(p->subs[index].zfd);
05879 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
05880 res = 0;
05881 break;
05882 } else if (i & ZT_IOMUX_READ) {
05883 res = read(p->subs[index].zfd, buf, sizeof(buf));
05884 if (res < 0) {
05885 if (errno != ELAST) {
05886 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
05887 callerid_free(cs);
05888 ast_hangup(chan);
05889 return NULL;
05890 }
05891 break;
05892 }
05893 samples += res;
05894 res = callerid_feed(cs, buf, res, AST_LAW(p));
05895 if (res < 0) {
05896 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
05897 break;
05898 } else if (res)
05899 break;
05900 else if (samples > (8000 * 10))
05901 break;
05902 }
05903 }
05904 if (res == 1) {
05905 callerid_get(cs, &name, &number, &flags);
05906 if (option_debug)
05907 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
05908 }
05909 if (res < 0) {
05910 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
05911 }
05912
05913
05914 res = 2000;
05915 for (;;) {
05916 struct ast_frame *f;
05917 res = ast_waitfor(chan, res);
05918 if (res <= 0) {
05919 ast_log(LOG_WARNING, "CID timed out waiting for ring. "
05920 "Exiting simple switch\n");
05921 ast_hangup(chan);
05922 return NULL;
05923 }
05924 f = ast_read(chan);
05925 ast_frfree(f);
05926 if (chan->_state == AST_STATE_RING ||
05927 chan->_state == AST_STATE_RINGING)
05928 break;
05929 }
05930
05931
05932
05933 if (p->usedistinctiveringdetection == 1) {
05934 len = 0;
05935 distMatches = 0;
05936
05937 for (receivedRingT=0; receivedRingT < 3; receivedRingT++) {
05938 curRingData[receivedRingT] = 0;
05939 }
05940 receivedRingT = 0;
05941 counter = 0;
05942 counter1 = 0;
05943
05944 if (strcmp(p->context,p->defcontext) != 0) {
05945 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
05946 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
05947 }
05948
05949 for(;;) {
05950 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
05951 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
05952 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
05953 callerid_free(cs);
05954 ast_hangup(chan);
05955 return NULL;
05956 }
05957 if (i & ZT_IOMUX_SIGEVENT) {
05958 res = zt_get_event(p->subs[index].zfd);
05959 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
05960 res = 0;
05961
05962
05963 curRingData[receivedRingT] = p->ringt;
05964
05965 if (p->ringt < p->ringt_base/2)
05966 break;
05967 ++receivedRingT;
05968
05969 } else if (i & ZT_IOMUX_READ) {
05970 res = read(p->subs[index].zfd, buf, sizeof(buf));
05971 if (res < 0) {
05972 if (errno != ELAST) {
05973 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
05974 callerid_free(cs);
05975 ast_hangup(chan);
05976 return NULL;
05977 }
05978 break;
05979 }
05980 if (p->ringt)
05981 p->ringt--;
05982 if (p->ringt == 1) {
05983 res = -1;
05984 break;
05985 }
05986 }
05987 }
05988 if(option_verbose > 2)
05989
05990 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
05991
05992 for (counter=0; counter < 3; counter++) {
05993
05994
05995 distMatches = 0;
05996 for (counter1=0; counter1 < 3; counter1++) {
05997 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
05998 (p->drings.ringnum[counter].ring[counter1]-10)) {
05999 distMatches++;
06000 }
06001 }
06002 if (distMatches == 3) {
06003
06004 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06005 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06006 if(option_verbose > 2)
06007 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06008 break;
06009 }
06010 }
06011 }
06012
06013 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06014 #if 1
06015 restore_gains(p);
06016 #endif
06017 } else
06018 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06019 } else {
06020 ast_log(LOG_WARNING, "Channel %s in prering "
06021 "state, but I have nothing to do. "
06022 "Terminating simple switch, should be "
06023 "restarted by the actual ring.\n",
06024 chan->name);
06025 ast_hangup(chan);
06026 return NULL;
06027 }
06028 } else if (p->use_callerid && p->cid_start == CID_START_RING) {
06029
06030 cs = callerid_new(cid_signalling);
06031 if (cs) {
06032 #if 1
06033 bump_gains(p);
06034 #endif
06035 samples = 0;
06036 len = 0;
06037 distMatches = 0;
06038
06039 for (receivedRingT=0; receivedRingT < 3; receivedRingT++) {
06040 curRingData[receivedRingT] = 0;
06041 }
06042 receivedRingT = 0;
06043 counter = 0;
06044 counter1 = 0;
06045
06046 if (strcmp(p->context,p->defcontext) != 0) {
06047 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06048 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06049 }
06050
06051
06052 zt_setlinear(p->subs[index].zfd, 0);
06053 for(;;) {
06054 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06055 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
06056 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06057 callerid_free(cs);
06058 ast_hangup(chan);
06059 return NULL;
06060 }
06061 if (i & ZT_IOMUX_SIGEVENT) {
06062 res = zt_get_event(p->subs[index].zfd);
06063 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06064 res = 0;
06065
06066
06067 curRingData[receivedRingT] = p->ringt;
06068
06069 if (p->ringt < p->ringt_base/2)
06070 break;
06071 ++receivedRingT;
06072
06073 } else if (i & ZT_IOMUX_READ) {
06074 res = read(p->subs[index].zfd, buf, sizeof(buf));
06075 if (res < 0) {
06076 if (errno != ELAST) {
06077 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06078 callerid_free(cs);
06079 ast_hangup(chan);
06080 return NULL;
06081 }
06082 break;
06083 }
06084 if (p->ringt)
06085 p->ringt--;
06086 if (p->ringt == 1) {
06087 res = -1;
06088 break;
06089 }
06090 samples += res;
06091 res = callerid_feed(cs, buf, res, AST_LAW(p));
06092 if (res < 0) {
06093 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06094 break;
06095 } else if (res)
06096 break;
06097 else if (samples > (8000 * 10))
06098 break;
06099 }
06100 }
06101 if (p->usedistinctiveringdetection == 1) {
06102 if(option_verbose > 2)
06103
06104 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06105
06106 for (counter=0; counter < 3; counter++) {
06107
06108
06109 distMatches = 0;
06110 for (counter1=0; counter1 < 3; counter1++) {
06111 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06112 (p->drings.ringnum[counter].ring[counter1]-10)) {
06113 distMatches++;
06114 }
06115 }
06116 if (distMatches == 3) {
06117
06118 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06119 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06120 if(option_verbose > 2)
06121 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06122 break;
06123 }
06124 }
06125 }
06126 if (res == 1) {
06127 callerid_get(cs, &name, &number, &flags);
06128 if (option_debug)
06129 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06130 }
06131
06132 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06133 #if 1
06134 restore_gains(p);
06135 #endif
06136 if (res < 0) {
06137 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06138 }
06139 } else
06140 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06141 }
06142 else
06143 cs = NULL;
06144 if (number || name) {
06145 if (chan->cid.cid_num) {
06146 free(chan->cid.cid_num);
06147 chan->cid.cid_num = NULL;
06148 }
06149 if (chan->cid.cid_name) {
06150 free(chan->cid.cid_name);
06151 chan->cid.cid_name = NULL;
06152 }
06153 }
06154 if (number)
06155 ast_shrink_phone_number(number);
06156
06157 ast_set_callerid(chan, number, name, number);
06158
06159 if (cs)
06160 callerid_free(cs);
06161 ast_setstate(chan, AST_STATE_RING);
06162 chan->rings = 1;
06163 p->ringt = p->ringt_base;
06164 res = ast_pbx_run(chan);
06165 if (res) {
06166 ast_hangup(chan);
06167 ast_log(LOG_WARNING, "PBX exited non-zero\n");
06168 }
06169 return NULL;
06170 default:
06171 ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
06172 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06173 if (res < 0)
06174 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06175 }
06176 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06177 if (res < 0)
06178 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06179 ast_hangup(chan);
06180 return NULL;
06181 }
06182
06183 #ifdef ZAPATA_R2
06184 static int handle_init_r2_event(struct zt_pvt *i, mfcr2_event_t *e)
06185 {
06186 struct ast_channel *chan;
06187
06188 switch(e->e) {
06189 case MFCR2_EVENT_UNBLOCKED:
06190 i->r2blocked = 0;
06191 if (option_verbose > 2)
06192 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
06193 break;
06194 case MFCR2_EVENT_BLOCKED:
06195 i->r2blocked = 1;
06196 if (option_verbose > 2)
06197 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
06198 break;
06199 case MFCR2_EVENT_IDLE:
06200 if (option_verbose > 2)
06201 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d idle\n", i->channel);
06202 break;
06203 case MFCR2_EVENT_RINGING:
06204
06205
06206
06207 i->hasr2call = 1;
06208 chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06209 if (!chan) {
06210 ast_log(LOG_WARNING, "Unable to create channel for channel %d\n", i->channel);
06211 mfcr2_DropCall(i->r2, NULL, UC_NETWORK_CONGESTION);
06212 i->hasr2call = 0;
06213 }
06214 if (ast_pbx_start(chan)) {
06215 ast_log(LOG_WARNING, "Unable to start PBX on channel %s\n", chan->name);
06216 ast_hangup(chan);
06217 }
06218 break;
06219 default:
06220 ast_log(LOG_WARNING, "Don't know how to handle initial R2 event %s on channel %d\n", mfcr2_event2str(e->e), i->channel);
06221 return -1;
06222 }
06223 return 0;
06224 }
06225 #endif
06226
06227 static int handle_init_event(struct zt_pvt *i, int event)
06228 {
06229 int res;
06230 pthread_t threadid;
06231 pthread_attr_t attr;
06232 struct ast_channel *chan;
06233 pthread_attr_init(&attr);
06234 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06235
06236 switch(event) {
06237 case ZT_EVENT_NONE:
06238 case ZT_EVENT_BITSCHANGED:
06239 if (i->radio) break;
06240 #ifdef ZAPATA_R2
06241 if (i->r2) {
06242 mfcr2_event_t *e;
06243 e = r2_get_event_bits(i);
06244 i->sigchecked = 1;
06245 if (e)
06246 handle_init_r2_event(i, e);
06247 }
06248 #endif
06249 break;
06250 case ZT_EVENT_WINKFLASH:
06251 case ZT_EVENT_RINGOFFHOOK:
06252 if (i->inalarm) break;
06253 if (i->radio) break;
06254
06255 switch(i->sig) {
06256 case SIG_FXOLS:
06257 case SIG_FXOGS:
06258 case SIG_FXOKS:
06259 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06260 if (i->cidspill) {
06261
06262 free(i->cidspill);
06263 i->cidspill = NULL;
06264 }
06265 if (i->immediate) {
06266 zt_enable_ec(i);
06267
06268 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
06269 chan = zt_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
06270 if (!chan) {
06271 ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
06272 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06273 if (res < 0)
06274 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06275 }
06276 } else {
06277
06278 chan = zt_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
06279 if (chan) {
06280 if (has_voicemail(i))
06281 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
06282 else
06283 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
06284 if (res < 0)
06285 ast_log(LOG_WARNING, "Unable to play dialtone on channel %d\n", i->channel);
06286 if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06287 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06288 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06289 if (res < 0)
06290 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06291 ast_hangup(chan);
06292 }
06293 } else
06294 ast_log(LOG_WARNING, "Unable to create channel\n");
06295 #if 0
06296 printf("Created thread %ld detached in switch\n", threadid);
06297 #endif
06298 }
06299 break;
06300 case SIG_FXSLS:
06301 case SIG_FXSGS:
06302 case SIG_FXSKS:
06303 i->ringt = i->ringt_base;
06304
06305 case SIG_EMWINK:
06306 case SIG_FEATD:
06307 case SIG_FEATDMF:
06308 case SIG_E911:
06309 case SIG_FEATB:
06310 case SIG_EM:
06311 case SIG_EM_E1:
06312 case SIG_SFWINK:
06313 case SIG_SF_FEATD:
06314 case SIG_SF_FEATDMF:
06315 case SIG_SF_FEATB:
06316 case SIG_SF:
06317
06318 chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06319 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06320 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06321 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06322 if (res < 0)
06323 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06324 ast_hangup(chan);
06325 } else if (!chan) {
06326 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
06327 }
06328 #if 0
06329 printf("Created thread %ld detached in switch(2)\n", threadid);
06330 #endif
06331 break;
06332 default:
06333 ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06334 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06335 if (res < 0)
06336 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06337 return -1;
06338 }
06339 break;
06340 case ZT_EVENT_NOALARM:
06341 i->inalarm = 0;
06342 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
06343 break;
06344 case ZT_EVENT_ALARM:
06345 i->inalarm = 1;
06346 res = get_alarms(i);
06347 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", i->channel, alarm2str(res));
06348
06349 case ZT_EVENT_ONHOOK:
06350 if (i->radio) break;
06351
06352 switch(i->sig) {
06353 case SIG_FXOLS:
06354 case SIG_FXOGS:
06355 case SIG_FEATD:
06356 case SIG_FEATDMF:
06357 case SIG_E911:
06358 case SIG_FEATB:
06359 case SIG_EM:
06360 case SIG_EM_E1:
06361 case SIG_EMWINK:
06362 case SIG_SF_FEATD:
06363 case SIG_SF_FEATDMF:
06364 case SIG_SF_FEATB:
06365 case SIG_SF:
06366 case SIG_SFWINK:
06367 case SIG_FXSLS:
06368 case SIG_FXSGS:
06369 case SIG_FXSKS:
06370 case SIG_GR303FXSKS:
06371 zt_disable_ec(i);
06372 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06373 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06374 break;
06375 case SIG_GR303FXOKS:
06376 case SIG_FXOKS:
06377 zt_disable_ec(i);
06378
06379 #ifdef ZHONE_HACK
06380 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06381 usleep(1);
06382 #endif
06383 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06384 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06385 break;
06386 case SIG_PRI:
06387 zt_disable_ec(i);
06388 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06389 break;
06390 default:
06391 ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06392 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06393 return -1;
06394 }
06395 break;
06396 case ZT_EVENT_POLARITY:
06397 switch(i->sig) {
06398 case SIG_FXSLS:
06399 case SIG_FXSKS:
06400 case SIG_FXSGS:
06401 if (i->cid_start == CID_START_POLARITY) {
06402 i->polarity = POLARITY_REV;
06403 ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
06404 "CID detection on channel %d\n",
06405 i->channel);
06406 chan = zt_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
06407 if (chan && 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 }
06410 }
06411 break;
06412 default:
06413 ast_log(LOG_WARNING, "handle_init_event detected "
06414 "polarity reversal on non-FXO (SIG_FXS) "
06415 "interface %d\n", i->channel);
06416 }
06417 }
06418 return 0;
06419 }
06420
06421 static void *do_monitor(void *data)
06422 {
06423 int count, res, res2, spoint, pollres=0;
06424 struct zt_pvt *i;
06425 struct zt_pvt *last = NULL;
06426 time_t thispass = 0, lastpass = 0;
06427 int found;
06428 char buf[1024];
06429 struct pollfd *pfds=NULL;
06430 int lastalloc = -1;
06431
06432
06433
06434 #if 0
06435 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
06436 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
06437 return NULL;
06438 }
06439 ast_log(LOG_DEBUG, "Monitor starting...\n");
06440 #endif
06441 for(;;) {
06442
06443 if (ast_mutex_lock(&iflock)) {
06444 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
06445 return NULL;
06446 }
06447 if (!pfds || (lastalloc != ifcount)) {
06448 if (pfds)
06449 free(pfds);
06450 if (ifcount) {
06451 pfds = malloc(ifcount * sizeof(struct pollfd));
06452 if (!pfds) {
06453 ast_log(LOG_WARNING, "Critical memory error. Zap dies.\n");
06454 ast_mutex_unlock(&iflock);
06455 return NULL;
06456 }
06457 }
06458 lastalloc = ifcount;
06459 }
06460
06461
06462 count = 0;
06463 i = iflist;
06464 while(i) {
06465 if ((i->subs[SUB_REAL].zfd > -1) && i->sig && (!i->radio)) {
06466 if (!i->owner && !i->subs[SUB_REAL].owner) {
06467
06468 pfds[count].fd = i->subs[SUB_REAL].zfd;
06469 pfds[count].events = POLLPRI;
06470 pfds[count].revents = 0;
06471
06472 #ifdef ZAPATA_R2
06473 if (i->cidspill || i->r2)
06474 #else
06475 if (i->cidspill)
06476 #endif
06477 pfds[count].events |= POLLIN;
06478 count++;
06479 }
06480 }
06481 i = i->next;
06482 }
06483
06484 ast_mutex_unlock(&iflock);
06485
06486 pthread_testcancel();
06487
06488 res = poll(pfds, count, 1000);
06489 pthread_testcancel();
06490
06491 if (res < 0) {
06492 if ((errno != EAGAIN) && (errno != EINTR))
06493 ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
06494 continue;
06495 }
06496
06497
06498 if (ast_mutex_lock(&iflock)) {
06499 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
06500 continue;
06501 }
06502 found = 0;
06503 spoint = 0;
06504 lastpass = thispass;
06505 thispass = time(NULL);
06506 i = iflist;
06507 while(i) {
06508 if (thispass != lastpass) {
06509 if (!found && ((i == last) || ((i == iflist) && !last))) {
06510 last = i;
06511 if (last) {
06512 #if 0
06513 printf("Checking channel %d\n", last->channel);
06514 #endif
06515 if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) &&
06516 (last->sig & __ZT_SIG_FXO)) {
06517 #if 0
06518 printf("Channel %d has mailbox %s\n", last->channel, last->mailbox);
06519 #endif
06520 res = ast_app_has_voicemail(last->mailbox, NULL);
06521 if (last->msgstate != res) {
06522 int x;
06523 ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel);
06524 x = ZT_FLUSH_BOTH;
06525 res2 = ioctl(last->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
06526 if (res2)
06527 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", last->channel);
06528 last->cidspill = malloc(MAX_CALLERID_SIZE);
06529 if (last->cidspill) {
06530
06531 x = 4000;
06532 ioctl(last->subs[SUB_REAL].zfd, ZT_ONHOOKTRANSFER, &x);
06533 last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last));
06534 last->cidpos = 0;
06535 #if 0
06536 printf("Made %d bytes of message waiting for %d\n", last->cidlen, res);
06537 #endif
06538 last->msgstate = res;
06539 last->onhooktime = thispass;
06540 }
06541 found ++;
06542 }
06543 }
06544 last = last->next;
06545 }
06546 }
06547 }
06548 if ((i->subs[SUB_REAL].zfd > -1) && i->sig) {
06549 if (i->radio && !i->owner)
06550 {
06551 res = zt_get_event(i->subs[SUB_REAL].zfd);
06552 if (res)
06553 {
06554 if (option_debug)
06555 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
06556
06557 ast_mutex_unlock(&iflock);
06558 handle_init_event(i, res);
06559 ast_mutex_lock(&iflock);
06560 }
06561 i = i->next;
06562 continue;
06563 }
06564 pollres = ast_fdisset(pfds, i->subs[SUB_REAL].zfd, count, &spoint);
06565 if (pollres & POLLIN) {
06566 if (i->owner || i->subs[SUB_REAL].owner) {
06567 #ifdef ZAPATA_PRI
06568 if (!i->pri)
06569 #endif
06570 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].zfd);
06571 i = i->next;
06572 continue;
06573 }
06574 #ifdef ZAPATA_R2
06575 if (i->r2) {
06576
06577 mfcr2_event_t *e;
06578 e = mfcr2_check_event(i->r2);
06579 if (e)
06580 handle_init_r2_event(i, e);
06581 else {
06582 e = mfcr2_schedule_run(i->r2);
06583 if (e)
06584 handle_init_r2_event(i, e);
06585 }
06586 i = i->next;
06587 continue;
06588 }
06589 #endif
06590 if (!i->cidspill) {
06591 ast_log(LOG_WARNING, "Whoa.... I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].zfd);
06592 i = i->next;
06593 continue;
06594 }
06595 res = read(i->subs[SUB_REAL].zfd, buf, sizeof(buf));
06596 if (res > 0) {
06597
06598 if (res > i->cidlen - i->cidpos)
06599 res = i->cidlen - i->cidpos;
06600 res2 = write(i->subs[SUB_REAL].zfd, i->cidspill + i->cidpos, res);
06601 if (res2 > 0) {
06602 i->cidpos += res2;
06603 if (i->cidpos >= i->cidlen) {
06604 free(i->cidspill);
06605 i->cidspill = 0;
06606 i->cidpos = 0;
06607 i->cidlen = 0;
06608 }
06609 } else {
06610 ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
06611 i->msgstate = -1;
06612 }
06613 } else {
06614 ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
06615 }
06616 if (option_debug)
06617 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06618
06619 ast_mutex_unlock(&iflock);
06620 handle_init_event(i, res);
06621 ast_mutex_lock(&iflock);
06622 }
06623 #ifdef ZAPATA_R2
06624 if ((pollres & POLLPRI) || (i->r2 && !i->sigchecked))
06625 #else
06626 if (pollres & POLLPRI)
06627 #endif
06628 {
06629 if (i->owner || i->subs[SUB_REAL].owner) {
06630 #ifdef ZAPATA_PRI
06631 if (!i->pri)
06632 #endif
06633 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].zfd);
06634 i = i->next;
06635 continue;
06636 }
06637 res = zt_get_event(i->subs[SUB_REAL].zfd);
06638 if (option_debug)
06639 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06640
06641 ast_mutex_unlock(&iflock);
06642 handle_init_event(i, res);
06643 ast_mutex_lock(&iflock);
06644 }
06645 }
06646 i=i->next;
06647 }
06648 ast_mutex_unlock(&iflock);
06649 }
06650
06651 return NULL;
06652
06653 }
06654
06655 static int restart_monitor(void)
06656 {
06657 pthread_attr_t attr;
06658 pthread_attr_init(&attr);
06659 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06660
06661 if (monitor_thread == AST_PTHREADT_STOP)
06662 return 0;
06663 if (ast_mutex_lock(&monlock)) {
06664 ast_log(LOG_WARNING, "Unable to lock monitor\n");
06665 return -1;
06666 }
06667 if (monitor_thread == pthread_self()) {
06668 ast_mutex_unlock(&monlock);
06669 ast_log(LOG_WARNING, "Cannot kill myself\n");
06670 return -1;
06671 }
06672 if (monitor_thread != AST_PTHREADT_NULL) {
06673
06674 #if 0
06675 pthread_cancel(monitor_thread);
06676 #endif
06677 pthread_kill(monitor_thread, SIGURG);
06678 #if 0
06679 pthread_join(monitor_thread, NULL);
06680 #endif
06681 } else {
06682
06683 if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
06684 ast_mutex_unlock(&monlock);
06685 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
06686 return -1;
06687 }
06688 }
06689 #if 0
06690 printf("Created thread %ld detached in restart monitor\n", monitor_thread);
06691 #endif
06692 ast_mutex_unlock(&monlock);
06693 return 0;
06694 }
06695
06696 #ifdef ZAPATA_PRI
06697 static int pri_resolve_span(int *span, int channel, int offset, struct zt_spaninfo *si)
06698 {
06699 int x;
06700 int trunkgroup;
06701
06702 trunkgroup = pris[*span].mastertrunkgroup;
06703 if (trunkgroup) {
06704
06705 for (x=0;x<NUM_SPANS;x++) {
06706 if (pris[x].trunkgroup == trunkgroup) {
06707 *span = x;
06708 return 0;
06709 }
06710 }
06711 ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup);
06712 *span = -1;
06713 } else {
06714 if (pris[*span].trunkgroup) {
06715 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup);
06716 *span = -1;
06717 } else if (pris[*span].mastertrunkgroup) {
06718 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup);
06719 *span = -1;
06720 } else {
06721 if (si->totalchans == 31) {
06722 pris[*span].dchannels[0] = 16 + offset;
06723 } else {
06724 pris[*span].dchannels[0] = 24 + offset;
06725 }
06726 pris[*span].dchanavail[0] |= DCHAN_PROVISIONED;
06727 pris[*span].offset = offset;
06728 pris[*span].span = *span + 1;
06729 }
06730 }
06731 return 0;
06732 }
06733
06734 static int pri_create_trunkgroup(int trunkgroup, int *channels)
06735 {
06736 struct zt_spaninfo si;
06737 ZT_PARAMS p;
06738 int fd;
06739 int span;
06740 int ospan=0;
06741 int x,y;
06742 for (x=0;x<NUM_SPANS;x++) {
06743 if (pris[x].trunkgroup == trunkgroup) {
06744 ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]);
06745 return -1;
06746 }
06747 }
06748 for (y=0;y<NUM_DCHANS;y++) {
06749 if (!channels[y])
06750 break;
06751 memset(&si, 0, sizeof(si));
06752 memset(&p, 0, sizeof(p));
06753 fd = open("/dev/zap/channel", O_RDWR);
06754 if (fd < 0) {
06755 ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno));
06756 return -1;
06757 }
06758 x = channels[y];
06759 if (ioctl(fd, ZT_SPECIFY, &x)) {
06760 ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
06761 zt_close(fd);
06762 return -1;
06763 }
06764 if (ioctl(fd, ZT_GET_PARAMS, &p)) {
06765 ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno));
06766 return -1;
06767 }
06768 if (ioctl(fd, ZT_SPANSTAT, &si)) {
06769 ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d)\n", channels[y], p.spanno);
06770 zt_close(fd);
06771 return -1;
06772 }
06773 span = p.spanno - 1;
06774 if (pris[span].trunkgroup) {
06775 ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
06776 zt_close(fd);
06777 return -1;
06778 }
06779 if (pris[span].pvts[0]) {
06780 ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
06781 zt_close(fd);
06782 return -1;
06783 }
06784 if (!y) {
06785 pris[span].trunkgroup = trunkgroup;
06786 pris[span].offset = channels[y] - p.chanpos;
06787 ospan = span;
06788 }
06789 pris[ospan].dchannels[y] = channels[y];
06790 pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
06791 pris[span].span = span + 1;
06792 zt_close(fd);
06793 }
06794 return 0;
06795 }
06796
06797 static int pri_create_spanmap(int span, int trunkgroup, int logicalspan)
06798 {
06799 if (pris[span].mastertrunkgroup) {
06800 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);
06801 return -1;
06802 }
06803 pris[span].mastertrunkgroup = trunkgroup;
06804 pris[span].prilogicalspan = logicalspan;
06805 return 0;
06806 }
06807
06808 #endif
06809
06810 static struct zt_pvt *mkintf(int channel, int signalling, int radio, struct zt_pri *pri, int reloading)
06811 {
06812
06813 struct zt_pvt *tmp = NULL, *tmp2, *prev = NULL;
06814 char fn[80];
06815 #if 1
06816 struct zt_bufferinfo bi;
06817 #endif
06818 struct zt_spaninfo si;
06819 int res;
06820 int span=0;
06821 int here = 0;
06822 int x;
06823 struct zt_pvt **wlist;
06824 struct zt_pvt **wend;
06825 ZT_PARAMS p;
06826
06827 wlist = &iflist;
06828 wend = &ifend;
06829
06830 #ifdef ZAPATA_PRI
06831 if (pri) {
06832 wlist = &pri->crvs;
06833 wend = &pri->crvend;
06834 }
06835 #endif
06836
06837 tmp2 = *wlist;
06838 prev = NULL;
06839
06840 while (tmp2) {
06841 if (!tmp2->destroy) {
06842 if (tmp2->channel == channel) {
06843 tmp = tmp2;
06844 here = 1;
06845 break;
06846 }
06847 if (tmp2->channel > channel) {
06848 break;
06849 }
06850 }
06851 prev = tmp2;
06852 tmp2 = tmp2->next;
06853 }
06854
06855 if (!here && !reloading) {
06856 tmp = (struct zt_pvt*)malloc(sizeof(struct zt_pvt));
06857 if (!tmp) {
06858 ast_log(LOG_ERROR, "MALLOC FAILED\n");
06859 destroy_zt_pvt(&tmp);
06860 return NULL;
06861 }
06862 memset(tmp, 0, sizeof(struct zt_pvt));
06863 ast_mutex_init(&tmp->lock);
06864 ifcount++;
06865 for (x=0;x<3;x++)
06866 tmp->subs[x].zfd = -1;
06867 tmp->channel = channel;
06868 }
06869
06870 if (tmp) {
06871 if (!here) {
06872 if ((channel != CHAN_PSEUDO) && !pri) {
06873 snprintf(fn, sizeof(fn), "%d", channel);
06874
06875 if (!here)
06876 tmp->subs[SUB_REAL].zfd = zt_open(fn);
06877
06878 if (tmp->subs[SUB_REAL].zfd < 0) {
06879 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);
06880 destroy_zt_pvt(&tmp);
06881 return NULL;
06882 }
06883 memset(&p, 0, sizeof(p));
06884 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
06885 if (res < 0) {
06886 ast_log(LOG_ERROR, "Unable to get parameters\n");
06887 destroy_zt_pvt(&tmp);
06888 return NULL;
06889 }
06890 if (p.sigtype != (signalling & 0x3ffff)) {
06891 ast_log(LOG_ERROR, "Signalling requested on channel %d is %s but line is in %s signalling\n", channel, sig2str(signalling), sig2str(p.sigtype));
06892 destroy_zt_pvt(&tmp);
06893 return tmp;
06894 }
06895 tmp->law = p.curlaw;
06896 tmp->span = p.spanno;
06897 span = p.spanno - 1;
06898 } else {
06899 if (channel == CHAN_PSEUDO)
06900 signalling = 0;
06901 else if ((signalling != SIG_FXOKS) && (signalling != SIG_FXSKS)) {
06902 ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n");
06903 return NULL;
06904 }
06905 }
06906 #ifdef ZAPATA_PRI
06907 if ((signalling == SIG_PRI) || (signalling == SIG_GR303FXOKS) || (signalling == SIG_GR303FXSKS)) {
06908 int offset;
06909 int myswitchtype;
06910 int matchesdchan;
06911 int x,y;
06912 offset = 0;
06913 if ((signalling == SIG_PRI) && ioctl(tmp->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &offset)) {
06914 ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
06915 destroy_zt_pvt(&tmp);
06916 return NULL;
06917 }
06918 if (span >= NUM_SPANS) {
06919 ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span);
06920 destroy_zt_pvt(&tmp);
06921 return NULL;
06922 } else {
06923 si.spanno = 0;
06924 if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
06925 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
06926 destroy_zt_pvt(&tmp);
06927 return NULL;
06928 }
06929
06930 tmp->logicalspan = pris[span].prilogicalspan;
06931 pri_resolve_span(&span, channel, (channel - p.chanpos), &si);
06932 if (span < 0) {
06933 ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel);
06934 destroy_zt_pvt(&tmp);
06935 return NULL;
06936 }
06937 if (signalling == SIG_PRI)
06938 myswitchtype = switchtype;
06939 else
06940 myswitchtype = PRI_SWITCH_GR303_TMC;
06941
06942 matchesdchan=0;
06943 for (x=0;x<NUM_SPANS;x++) {
06944 for (y=0;y<NUM_DCHANS;y++) {
06945 if (pris[x].dchannels[y] == tmp->channel) {
06946 matchesdchan = 1;
06947 break;
06948 }
06949 }
06950 }
06951 offset = p.chanpos;
06952 if (!matchesdchan) {
06953 if (pris[span].nodetype && (pris[span].nodetype != pritype)) {
06954 ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype));
06955 destroy_zt_pvt(&tmp);
06956 return NULL;
06957 }
06958 if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) {
06959 ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype));
06960 destroy_zt_pvt(&tmp);
06961 return NULL;
06962 }
06963 if ((pris[span].dialplan) && (pris[span].dialplan != dialplan)) {
06964 ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan));
06965 destroy_zt_pvt(&tmp);
06966 return NULL;
06967 }
06968 if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, idledial)) {
06969 ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, idledial);
06970 destroy_zt_pvt(&tmp);
06971 return NULL;
06972 }
06973 if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, idleext)) {
06974 ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, idleext);
06975 destroy_zt_pvt(&tmp);
06976 return NULL;
06977 }
06978 if (pris[span].minunused && (pris[span].minunused != minunused)) {
06979 ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, minunused);
06980 destroy_zt_pvt(&tmp);
06981 return NULL;
06982 }
06983 if (pris[span].minidle && (pris[span].minidle != minidle)) {
06984 ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, minidle);
06985 destroy_zt_pvt(&tmp);
06986 return NULL;
06987 }
06988 if (pris[span].numchans >= MAX_CHANNELS) {
06989 ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel,
06990 pris[span].trunkgroup);
06991 destroy_zt_pvt(&tmp);
06992 return NULL;
06993 }
06994 pris[span].nodetype = pritype;
06995 pris[span].switchtype = myswitchtype;
06996 pris[span].nsf = nsf;
06997 pris[span].dialplan = dialplan;
06998 pris[span].localdialplan = localdialplan;
06999 pris[span].pvts[pris[span].numchans++] = tmp;
07000 pris[span].minunused = minunused;
07001 pris[span].minidle = minidle;
07002 pris[span].overlapdial = overlapdial;
07003 pris[span].facilityenable = facilityenable;
07004 ast_copy_string(pris[span].idledial, idledial, sizeof(pris[span].idledial));
07005 ast_copy_string(pris[span].idleext, idleext, sizeof(pris[span].idleext));
07006 ast_copy_string(pris[span].internationalprefix, internationalprefix, sizeof(pris[span].internationalprefix));
07007 ast_copy_string(pris[span].nationalprefix, nationalprefix, sizeof(pris[span].nationalprefix));
07008 ast_copy_string(pris[span].localprefix, localprefix, sizeof(pris[span].localprefix));
07009 ast_copy_string(pris[span].privateprefix, privateprefix, sizeof(pris[span].privateprefix));
07010 ast_copy_string(pris[span].unknownprefix, unknownprefix, sizeof(pris[span].unknownprefix));
07011 pris[span].resetinterval = resetinterval;
07012
07013 tmp->pri = &pris[span];
07014 tmp->prioffset = offset;
07015 tmp->call = NULL;
07016 } else {
07017 ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset);
07018 destroy_zt_pvt(&tmp);
07019 return NULL;
07020 }
07021 }
07022 } else {
07023 tmp->prioffset = 0;
07024 }
07025 #endif
07026 #ifdef ZAPATA_R2
07027 if (signalling == SIG_R2) {
07028 if (r2prot < 0) {
07029 ast_log(LOG_WARNING, "R2 Country not specified for channel %d -- Assuming China\n", tmp->channel);
07030 tmp->r2prot = MFCR2_PROT_CHINA;
07031 } else
07032 tmp->r2prot = r2prot;
07033 tmp->r2 = mfcr2_new(tmp->subs[SUB_REAL].zfd, tmp->r2prot, 1);
07034 if (!tmp->r2) {
07035 ast_log(LOG_WARNING, "Unable to create r2 call :(\n");
07036 zt_close(tmp->subs[SUB_REAL].zfd);
07037 destroy_zt_pvt(&tmp);
07038 return NULL;
07039 }
07040 } else {
07041 if (tmp->r2)
07042 mfcr2_free(tmp->r2);
07043 tmp->r2 = NULL;
07044 }
07045 #endif
07046 } else {
07047 signalling = tmp->sig;
07048 radio = tmp->radio;
07049 memset(&p, 0, sizeof(p));
07050 if (tmp->subs[SUB_REAL].zfd > -1)
07051 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &p);
07052 }
07053
07054 if ((signalling == SIG_FXSKS) || (signalling == SIG_FXSLS) ||
07055 (signalling == SIG_EM) || (signalling == SIG_EM_E1) || (signalling == SIG_EMWINK) ||
07056 (signalling == SIG_FEATD) || (signalling == SIG_FEATDMF) || (signalling == SIG_FEATDMF_TA) ||
07057 (signalling == SIG_FEATB) || (signalling == SIG_E911) ||
07058 (signalling == SIG_SF) || (signalling == SIG_SFWINK) ||
07059 (signalling == SIG_SF_FEATD) || (signalling == SIG_SF_FEATDMF) ||
07060 (signalling == SIG_SF_FEATB)) {
07061 p.starttime = 250;
07062 }
07063 if (radio) {
07064
07065 p.channo = channel;
07066 p.rxwinktime = 1;
07067 p.rxflashtime = 1;
07068 p.starttime = 1;
07069 p.debouncetime = 5;
07070 }
07071 if (!radio) {
07072 p.channo = channel;
07073
07074 if (cur_prewink >= 0)
07075 p.prewinktime = cur_prewink;
07076 if (cur_preflash >= 0)
07077 p.preflashtime = cur_preflash;
07078 if (cur_wink >= 0)
07079 p.winktime = cur_wink;
07080 if (cur_flash >= 0)
07081 p.flashtime = cur_flash;
07082 if (cur_start >= 0)
07083 p.starttime = cur_start;
07084 if (cur_rxwink >= 0)
07085 p.rxwinktime = cur_rxwink;
07086 if (cur_rxflash >= 0)
07087 p.rxflashtime = cur_rxflash;
07088 if (cur_debounce >= 0)
07089 p.debouncetime = cur_debounce;
07090 }
07091
07092
07093 if (tmp->subs[SUB_REAL].zfd >= 0)
07094 {
07095 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_PARAMS, &p);
07096 if (res < 0) {
07097 ast_log(LOG_ERROR, "Unable to set parameters\n");
07098 destroy_zt_pvt(&tmp);
07099 return NULL;
07100 }
07101 }
07102 #if 1
07103 if (!here && (tmp->subs[SUB_REAL].zfd > -1)) {
07104 memset(&bi, 0, sizeof(bi));
07105 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
07106 if (!res) {
07107 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
07108 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
07109 bi.numbufs = numbufs;
07110 res = ioctl(tmp->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
07111 if (res < 0) {
07112 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", channel);
07113 }
07114 } else
07115 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", channel);
07116 }
07117 #endif
07118 tmp->immediate = immediate;
07119 tmp->transfertobusy = transfertobusy;
07120 tmp->sig = signalling;
07121 tmp->radio = radio;
07122 tmp->ringt_base = ringt_base;
07123 tmp->firstradio = 0;
07124 if ((signalling == SIG_FXOKS) || (signalling == SIG_FXOLS) || (signalling == SIG_FXOGS))
07125 tmp->permcallwaiting = callwaiting;
07126 else
07127 tmp->permcallwaiting = 0;
07128
07129 tmp->destroy = 0;
07130 tmp->drings = drings;
07131 tmp->usedistinctiveringdetection = usedistinctiveringdetection;
07132 tmp->callwaitingcallerid = callwaitingcallerid;
07133 tmp->threewaycalling = threewaycalling;
07134 tmp->adsi = adsi;
07135 tmp->permhidecallerid = hidecallerid;
07136 tmp->callreturn = callreturn;
07137 tmp->echocancel = echocancel;
07138 tmp->echotraining = echotraining;
07139 tmp->pulse = pulse;
07140 tmp->echocanbridged = echocanbridged;
07141 tmp->busydetect = busydetect;
07142 tmp->busycount = busycount;
07143 tmp->busy_tonelength = busy_tonelength;
07144 tmp->busy_quietlength = busy_quietlength;
07145 tmp->callprogress = callprogress;
07146 tmp->cancallforward = cancallforward;
07147 tmp->dtmfrelax = relaxdtmf;
07148 tmp->callwaiting = tmp->permcallwaiting;
07149 tmp->hidecallerid = tmp->permhidecallerid;
07150 tmp->channel = channel;
07151 tmp->stripmsd = stripmsd;
07152 tmp->use_callerid = use_callerid;
07153 tmp->cid_signalling = cid_signalling;
07154 tmp->cid_start = cid_start;
07155 tmp->zaptrcallerid = zaptrcallerid;
07156 tmp->restrictcid = restrictcid;
07157 tmp->use_callingpres = use_callingpres;
07158 tmp->priindication_oob = priindication_oob;
07159 tmp->priexclusive = cur_priexclusive;
07160 if (tmp->usedistinctiveringdetection) {
07161 if (!tmp->use_callerid) {
07162 ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n");
07163 tmp->use_callerid = 1;
07164 }
07165 }
07166
07167 ast_copy_string(tmp->accountcode, accountcode, sizeof(tmp->accountcode));
07168 tmp->amaflags = amaflags;
07169 if (!here) {
07170 tmp->confno = -1;
07171 tmp->propconfno = -1;
07172 }
07173 tmp->canpark = canpark;
07174 tmp->transfer = transfer;
07175 ast_copy_string(tmp->defcontext,context,sizeof(tmp->defcontext));
07176 ast_copy_string(tmp->language, language, sizeof(tmp->language));
07177 ast_copy_string(tmp->musicclass, musicclass, sizeof(tmp->musicclass));
07178 ast_copy_string(tmp->context, context, sizeof(tmp->context));
07179 ast_copy_string(tmp->cid_num, cid_num, sizeof(tmp->cid_num));
07180 tmp->cid_ton = 0;
07181 ast_copy_string(tmp->cid_name, cid_name, sizeof(tmp->cid_name));
07182 ast_copy_string(tmp->mailbox, mailbox, sizeof(tmp->mailbox));
07183 tmp->msgstate = -1;
07184 tmp->group = cur_group;
07185 tmp->callgroup=cur_callergroup;
07186 tmp->pickupgroup=cur_pickupgroup;
07187 tmp->rxgain = rxgain;
07188 tmp->txgain = txgain;
07189 tmp->tonezone = tonezone;
07190 tmp->onhooktime = time(NULL);
07191 if (tmp->subs[SUB_REAL].zfd > -1) {
07192 set_actual_gain(tmp->subs[SUB_REAL].zfd, 0, tmp->rxgain, tmp->txgain, tmp->law);
07193 if (tmp->dsp)
07194 ast_dsp_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax);
07195 update_conf(tmp);
07196 if (!here) {
07197 if ((signalling != SIG_PRI) && (signalling != SIG_R2))
07198
07199 zt_set_hook(tmp->subs[SUB_REAL].zfd, ZT_ONHOOK);
07200 }
07201 ioctl(tmp->subs[SUB_REAL].zfd,ZT_SETTONEZONE,&tmp->tonezone);
07202 #ifdef ZAPATA_PRI
07203
07204 if (tmp->pri && !pri_is_up(tmp->pri))
07205 tmp->inalarm = 1;
07206 else
07207 tmp->inalarm = 0;
07208 #endif
07209 memset(&si, 0, sizeof(si));
07210 if (ioctl(tmp->subs[SUB_REAL].zfd,ZT_SPANSTAT,&si) == -1) {
07211 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
07212 destroy_zt_pvt(&tmp);
07213 return NULL;
07214 }
07215 if (si.alarms) tmp->inalarm = 1;
07216 }
07217
07218 tmp->polarityonanswerdelay = polarityonanswerdelay;
07219 tmp->answeronpolarityswitch = answeronpolarityswitch;
07220 tmp->hanguponpolarityswitch = hanguponpolarityswitch;
07221 tmp->sendcalleridafter = sendcalleridafter;
07222
07223 }
07224 if (tmp && !here) {
07225
07226 if (!*wlist) {
07227 *wlist = tmp;
07228 tmp->prev = NULL;
07229 tmp->next = NULL;
07230 *wend = tmp;
07231 } else {
07232
07233 struct zt_pvt *working = *wlist;
07234
07235
07236 if (working->channel > tmp->channel) {
07237 tmp->next = *wlist;
07238 tmp->prev = NULL;
07239 (*wlist)->prev = tmp;
07240 *wlist = tmp;
07241 } else {
07242
07243 while (working) {
07244
07245 if (working->next) {
07246 if (working->channel < tmp->channel && working->next->channel > tmp->channel) {
07247 tmp->next = working->next;
07248 tmp->prev = working;
07249 working->next->prev = tmp;
07250 working->next = tmp;
07251 break;
07252 }
07253 } else {
07254
07255 if (working->channel < tmp->channel) {
07256 working->next = tmp;
07257 tmp->next = NULL;
07258 tmp->prev = working;
07259 *wend = tmp;
07260 break;
07261 }
07262 }
07263 working = working->next;
07264 }
07265 }
07266 }
07267 }
07268 return tmp;
07269 }
07270
07271 static inline int available(struct zt_pvt *p, int channelmatch, int groupmatch, int *busy, int *channelmatched, int *groupmatched)
07272 {
07273 int res;
07274 ZT_PARAMS par;
07275
07276
07277 if (groupmatch) {
07278 if ((p->group & groupmatch) != groupmatch)
07279 return 0;
07280 *groupmatched = 1;
07281 }
07282
07283 if (channelmatch != -1) {
07284 if (p->channel != channelmatch)
07285 return 0;
07286 *channelmatched = 1;
07287 }
07288
07289 if (busy) {
07290 if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS))
07291 *busy = 1;
07292 }
07293
07294 if (p->dnd)
07295 return 0;
07296
07297 if (p->guardtime && (time(NULL) < p->guardtime))
07298 return 0;
07299
07300
07301 if (!p->owner) {
07302 #ifdef ZAPATA_PRI
07303
07304 if (p->pri) {
07305 if (p->resetting || p->call)
07306 return 0;
07307 else
07308 return 1;
07309 }
07310 #endif
07311 #ifdef ZAPATA_R2
07312
07313 if (p->r2) {
07314 if (p->hasr2call || p->r2blocked)
07315 return 0;
07316 else
07317 return 1;
07318 }
07319 #endif
07320 if (!p->radio)
07321 {
07322 if (!p->sig || (p->sig == SIG_FXSLS))
07323 return 1;
07324
07325 if (p->subs[SUB_REAL].zfd > -1)
07326 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
07327 else {
07328
07329 res = 0;
07330 par.rxisoffhook = 0;
07331 }
07332 if (res) {
07333 ast_log(LOG_WARNING, "Unable to check hook state on channel %d\n", p->channel);
07334 } else if ((p->sig == SIG_FXSKS) || (p->sig == SIG_FXSGS)) {
07335
07336
07337
07338 if (par.rxbits > -1)
07339 return 1;
07340 if (par.rxisoffhook)
07341 return 1;
07342 else
07343 #ifdef ZAP_CHECK_HOOKSTATE
07344 return 0;
07345 #else
07346 return 1;
07347 #endif
07348 } else if (par.rxisoffhook) {
07349 ast_log(LOG_DEBUG, "Channel %d off hook, can't use\n", p->channel);
07350
07351 return 0;
07352 }
07353 }
07354 return 1;
07355 }
07356
07357
07358 if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS))
07359 return 0;
07360
07361 if (!p->callwaiting) {
07362
07363 return 0;
07364 }
07365
07366 if (p->subs[SUB_CALLWAIT].zfd > -1) {
07367
07368 return 0;
07369 }
07370
07371 if ((p->owner->_state != AST_STATE_UP) &&
07372 ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
07373
07374 return 0;
07375 }
07376 if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) {
07377
07378 return 0;
07379 }
07380
07381 return 1;
07382 }
07383
07384 static struct zt_pvt *chandup(struct zt_pvt *src)
07385 {
07386 struct zt_pvt *p;
07387 ZT_BUFFERINFO bi;
07388 int res;
07389 p = malloc(sizeof(struct zt_pvt));
07390 if (p) {
07391 memcpy(p, src, sizeof(struct zt_pvt));
07392 ast_mutex_init(&p->lock);
07393 p->subs[SUB_REAL].zfd = zt_open("/dev/zap/pseudo");
07394
07395 if (p->subs[SUB_REAL].zfd < 0) {
07396 ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno));
07397 destroy_zt_pvt(&p);
07398 return NULL;
07399 }
07400 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_BUFINFO, &bi);
07401 if (!res) {
07402 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
07403 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
07404 bi.numbufs = numbufs;
07405 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SET_BUFINFO, &bi);
07406 if (res < 0) {
07407 ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel\n");
07408 }
07409 } else
07410 ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel\n");
07411 }
07412 p->destroy = 1;
07413 p->next = iflist;
07414 iflist = p;
07415 return p;
07416 }
07417
07418
07419 #ifdef ZAPATA_PRI
07420 static int pri_find_empty_chan(struct zt_pri *pri, int backwards)
07421 {
07422 int x;
07423 if (backwards)
07424 x = pri->numchans;
07425 else
07426 x = 0;
07427 for (;;) {
07428 if (backwards && (x < 0))
07429 break;
07430 if (!backwards && (x >= pri->numchans))
07431 break;
07432 if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
07433 ast_log(LOG_DEBUG, "Found empty available channel %d/%d\n",
07434 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
07435 return x;
07436 }
07437 if (backwards)
07438 x--;
07439 else
07440 x++;
07441 }
07442 return -1;
07443 }
07444 #endif
07445
07446 static struct ast_channel *zt_request(const char *type, int format, void *data, int *cause)
07447 {
07448 int oldformat;
07449 int groupmatch = 0;
07450 int channelmatch = -1;
07451 int roundrobin = 0;
07452 int callwait = 0;
07453 int busy = 0;
07454 struct zt_pvt *p;
07455 struct ast_channel *tmp = NULL;
07456 char *dest=NULL;
07457 int x;
07458 char *s;
07459 char opt=0;
07460 int res=0, y=0;
07461 int backwards = 0;
07462 #ifdef ZAPATA_PRI
07463 int crv;
07464 int bearer = -1;
07465 int trunkgroup;
07466 struct zt_pri *pri=NULL;
07467 #endif
07468 struct zt_pvt *exit, *start, *end;
07469 ast_mutex_t *lock;
07470 int channelmatched = 0;
07471 int groupmatched = 0;
07472
07473
07474 lock = &iflock;
07475 start = iflist;
07476 end = ifend;
07477
07478 oldformat = format;
07479 format &= (AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
07480 if (!format) {
07481 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
07482 return NULL;
07483 }
07484 if (data) {
07485 dest = ast_strdupa((char *)data);
07486 } else {
07487 ast_log(LOG_WARNING, "Channel requested with no data\n");
07488 return NULL;
07489 }
07490 if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') {
07491
07492 char *stringp=NULL;
07493 stringp=dest + 1;
07494 s = strsep(&stringp, "/");
07495 if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07496 ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data);
07497 return NULL;
07498 }
07499 groupmatch = 1 << x;
07500 if (toupper(dest[0]) == 'G') {
07501 if (dest[0] == 'G') {
07502 backwards = 1;
07503 p = ifend;
07504 } else
07505 p = iflist;
07506 } else {
07507 if (dest[0] == 'R') {
07508 backwards = 1;
07509 p = round_robin[x]?round_robin[x]->prev:ifend;
07510 if (!p)
07511 p = ifend;
07512 } else {
07513 p = round_robin[x]?round_robin[x]->next:iflist;
07514 if (!p)
07515 p = iflist;
07516 }
07517 roundrobin = 1;
07518 }
07519 } else {
07520 char *stringp=NULL;
07521 stringp=dest;
07522 s = strsep(&stringp, "/");
07523 p = iflist;
07524 if (!strcasecmp(s, "pseudo")) {
07525
07526 x = CHAN_PSEUDO;
07527 channelmatch = x;
07528 }
07529 #ifdef ZAPATA_PRI
07530 else if ((res = sscanf(s, "%d:%d%c%d", &trunkgroup, &crv, &opt, &y)) > 1) {
07531 if ((trunkgroup < 1) || (crv < 1)) {
07532 ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data);
07533 return NULL;
07534 }
07535 res--;
07536 for (x=0;x<NUM_SPANS;x++) {
07537 if (pris[x].trunkgroup == trunkgroup) {
07538 pri = pris + x;
07539 lock = &pri->lock;
07540 start = pri->crvs;
07541 end = pri->crvend;
07542 break;
07543 }
07544 }
07545 if (!pri) {
07546 ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup);
07547 return NULL;
07548 }
07549 channelmatch = crv;
07550 p = pris[x].crvs;
07551 }
07552 #endif
07553 else if ((res = sscanf(s, "%d%c%d", &x, &opt, &y)) < 1) {
07554 ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data);
07555 return NULL;
07556 } else {
07557 channelmatch = x;
07558 }
07559 }
07560
07561 if (ast_mutex_lock(lock)) {
07562 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
07563 return NULL;
07564 }
07565 exit = p;
07566 while(p && !tmp) {
07567 if (roundrobin)
07568 round_robin[x] = p;
07569 #if 0
07570 ast_verbose("name = %s, %d, %d, %d\n",p->owner ? p->owner->name : "<none>", p->channel, channelmatch, groupmatch);
07571 #endif
07572
07573 if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) {
07574 if (option_debug)
07575 ast_log(LOG_DEBUG, "Using channel %d\n", p->channel);
07576 if (p->inalarm)
07577 goto next;
07578
07579 callwait = (p->owner != NULL);
07580 #ifdef ZAPATA_PRI
07581 if (pri && (p->subs[SUB_REAL].zfd < 0)) {
07582 if (p->sig != SIG_FXSKS) {
07583
07584
07585 bearer = pri_find_empty_chan(pri, 0);
07586 if (bearer < 0) {
07587 ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv);
07588 p = NULL;
07589 break;
07590 }
07591 pri_assign_bearer(p, pri, pri->pvts[bearer]);
07592 } else {
07593 if (alloc_sub(p, 0)) {
07594 ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n");
07595 p = NULL;
07596 break;
07597 } else
07598 ast_log(LOG_DEBUG, "Allocated placeholder pseudo channel\n");
07599 p->pri = pri;
07600 }
07601 }
07602 #endif
07603 if (p->channel == CHAN_PSEUDO) {
07604 p = chandup(p);
07605 if (!p) {
07606 break;
07607 }
07608 }
07609 if (p->owner) {
07610 if (alloc_sub(p, SUB_CALLWAIT)) {
07611 p = NULL;
07612 break;
07613 }
07614 }
07615 p->outgoing = 1;
07616 tmp = zt_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0);
07617 #ifdef ZAPATA_PRI
07618 if (p->bearer) {
07619
07620 p->bearer->owner = tmp;
07621 }
07622 #endif
07623
07624 if (res > 1) {
07625 if (opt == 'c') {
07626
07627 p->confirmanswer = 1;
07628 } else if (opt == 'r') {
07629
07630 if (res < 3)
07631 ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data);
07632 else
07633 p->distinctivering = y;
07634 } else if (opt == 'd') {
07635
07636 p->digital = 1;
07637 if (tmp)
07638 tmp->transfercapability = AST_TRANS_CAP_DIGITAL;
07639 } else {
07640 ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data);
07641 }
07642 }
07643
07644 if (tmp && callwait)
07645 tmp->cdrflags |= AST_CDR_CALLWAIT;
07646 break;
07647 }
07648 next:
07649 if (backwards) {
07650 p = p->prev;
07651 if (!p)
07652 p = end;
07653 } else {
07654 p = p->next;
07655 if (!p)
07656 p = start;
07657 }
07658
07659 if (p == exit)
07660 break;
07661 }
07662 ast_mutex_unlock(lock);
07663 restart_monitor();
07664 if (callwait)
07665 *cause = AST_CAUSE_BUSY;
07666 else if (!tmp) {
07667 if (channelmatched) {
07668 if (busy)
07669 *cause = AST_CAUSE_BUSY;
07670 } else if (groupmatched) {
07671 *cause = AST_CAUSE_CONGESTION;
07672 }
07673 }
07674
07675 return tmp;
07676 }
07677
07678
07679 #ifdef ZAPATA_PRI
07680 static struct zt_pvt *pri_find_crv(struct zt_pri *pri, int crv)
07681 {
07682 struct zt_pvt *p;
07683 p = pri->crvs;
07684 while(p) {
07685 if (p->channel == crv)
07686 return p;
07687 p = p->next;
07688 }
07689 return NULL;
07690 }
07691
07692
07693 static int pri_find_principle(struct zt_pri *pri, int channel)
07694 {
07695 int x;
07696 int span = PRI_SPAN(channel);
07697 int spanfd;
07698 ZT_PARAMS param;
07699 int principle = -1;
07700 int explicit = PRI_EXPLICIT(channel);
07701 span = PRI_SPAN(channel);
07702 channel = PRI_CHANNEL(channel);
07703
07704 if (!explicit) {
07705 spanfd = pri_active_dchan_fd(pri);
07706 if (ioctl(spanfd, ZT_GET_PARAMS, ¶m))
07707 return -1;
07708 span = pris[param.spanno - 1].prilogicalspan;
07709 }
07710
07711 for (x=0;x<pri->numchans;x++) {
07712 if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
07713 principle = x;
07714 break;
07715 }
07716 }
07717
07718 return principle;
07719 }
07720
07721 static int pri_fixup_principle(struct zt_pri *pri, int principle, q931_call *c)
07722 {
07723 int x;
07724 struct zt_pvt *crv;
07725 if (!c) {
07726 if (principle < 0)
07727 return -1;
07728 return principle;
07729 }
07730 if ((principle > -1) &&
07731 (principle < pri->numchans) &&
07732 (pri->pvts[principle]) &&
07733 (pri->pvts[principle]->call == c))
07734 return principle;
07735
07736 for (x=0;x<pri->numchans;x++) {
07737 if (!pri->pvts[x]) continue;
07738 if (pri->pvts[x]->call == c) {
07739
07740 if (principle != x) {
07741 if (option_verbose > 2)
07742 ast_verbose(VERBOSE_PREFIX_3 "Moving call from channel %d to channel %d\n",
07743 pri->pvts[x]->channel, pri->pvts[principle]->channel);
07744 if (pri->pvts[principle]->owner) {
07745 ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
07746 pri->pvts[x]->channel, pri->pvts[principle]->channel, pri->pvts[principle]->channel);
07747 return -1;
07748 }
07749
07750 pri->pvts[principle]->owner = pri->pvts[x]->owner;
07751 if (pri->pvts[principle]->owner) {
07752 snprintf(pri->pvts[principle]->owner->name, sizeof(pri->pvts[principle]->owner->name),
07753 "Zap/%d:%d-%d", pri->trunkgroup, pri->pvts[principle]->channel, 1);
07754 pri->pvts[principle]->owner->tech_pvt = pri->pvts[principle];
07755 pri->pvts[principle]->owner->fds[0] = pri->pvts[principle]->subs[SUB_REAL].zfd;
07756 pri->pvts[principle]->subs[SUB_REAL].owner = pri->pvts[x]->subs[SUB_REAL].owner;
07757 } else
07758 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);
07759 pri->pvts[principle]->call = pri->pvts[x]->call;
07760
07761 pri->pvts[x]->subs[SUB_REAL].owner = NULL;
07762 pri->pvts[x]->owner = NULL;
07763 pri->pvts[x]->call = NULL;
07764 }
07765 return principle;
07766 }
07767 }
07768
07769 crv = pri->crvs;
07770 while(crv) {
07771 if (crv->call == c) {
07772
07773 if (crv->bearer)
07774 ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n");
07775 else if (pri->pvts[principle]->owner)
07776 ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n");
07777 else {
07778
07779
07780 zt_close(crv->subs[SUB_REAL].zfd);
07781 pri->pvts[principle]->call = crv->call;
07782 pri_assign_bearer(crv, pri, pri->pvts[principle]);
07783 ast_log(LOG_DEBUG, "Assigning bearer %d/%d to CRV %d:%d\n",
07784 pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset,
07785 pri->trunkgroup, crv->channel);
07786 wakeup_sub(crv, SUB_REAL, pri);
07787 }
07788 return principle;
07789 }
07790 crv = crv->next;
07791 }
07792 ast_log(LOG_WARNING, "Call specified, but not found?\n");
07793 return -1;
07794 }
07795
07796 static void *do_idle_thread(void *vchan)
07797 {
07798 struct ast_channel *chan = vchan;
07799 struct zt_pvt *pvt = chan->tech_pvt;
07800 struct ast_frame *f;
07801 char ex[80];
07802
07803 int newms, ms = 30000;
07804 if (option_verbose > 2)
07805 ast_verbose(VERBOSE_PREFIX_3 "Initiating idle call on channel %s\n", chan->name);
07806 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
07807 if (ast_call(chan, ex, 0)) {
07808 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
07809 ast_hangup(chan);
07810 return NULL;
07811 }
07812 while((newms = ast_waitfor(chan, ms)) > 0) {
07813 f = ast_read(chan);
07814 if (!f) {
07815
07816 break;
07817 }
07818 if (f->frametype == AST_FRAME_CONTROL) {
07819 switch(f->subclass) {
07820 case AST_CONTROL_ANSWER:
07821
07822 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
07823 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
07824 chan->priority = 1;
07825 if (option_verbose > 3)
07826 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
07827 ast_pbx_run(chan);
07828
07829 return NULL;
07830 case AST_CONTROL_BUSY:
07831 if (option_verbose > 3)
07832 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' busy, waiting...\n", chan->name);
07833 break;
07834 case AST_CONTROL_CONGESTION:
07835 if (option_verbose > 3)
07836 ast_verbose(VERBOSE_PREFIX_3 "Idle channel '%s' congested, waiting...\n", chan->name);
07837 break;
07838 };
07839 }
07840 ast_frfree(f);
07841 ms = newms;
07842 }
07843 #if 0
07844 printf("Hanging up '%s'\n", chan->name);
07845 #endif
07846
07847 ast_hangup(chan);
07848 return NULL;
07849 }
07850
07851 #ifndef PRI_RESTART
07852 #error "Upgrade your libpri"
07853 #endif
07854 static void zt_pri_message(struct pri *pri, char *s)
07855 {
07856 int x, y;
07857 int dchan = -1, span = -1;
07858 int dchancount = 0;
07859
07860 if (pri) {
07861 for (x = 0; x < NUM_SPANS; x++) {
07862 for (y = 0; y < NUM_DCHANS; y++) {
07863 if (pris[x].dchans[y])
07864 dchancount++;
07865
07866 if (pris[x].dchans[y] == pri)
07867 dchan = y;
07868 }
07869 if (dchan >= 0) {
07870 span = x;
07871 break;
07872 }
07873 dchancount = 0;
07874 }
07875 if ((dchan >= 0) && (span >= 0)) {
07876 if (dchancount > 1)
07877 ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s);
07878 else
07879 ast_verbose("%s", s);
07880 } else
07881 ast_verbose("PRI debug error: could not find pri associated it with debug message output\n");
07882 } else
07883 ast_verbose("%s", s);
07884
07885 ast_mutex_lock(&pridebugfdlock);
07886
07887 if (pridebugfd >= 0)
07888 write(pridebugfd, s, strlen(s));
07889
07890 ast_mutex_unlock(&pridebugfdlock);
07891 }
07892
07893 static void zt_pri_error(struct pri *pri, char *s)
07894 {
07895 int x, y;
07896 int dchan = -1, span = -1;
07897 int dchancount = 0;
07898
07899 if (pri) {
07900 for (x = 0; x < NUM_SPANS; x++) {
07901 for (y = 0; y < NUM_DCHANS; y++) {
07902 if (pris[x].dchans[y])
07903 dchancount++;
07904
07905 if (pris[x].dchans[y] == pri)
07906 dchan = y;
07907 }
07908 if (dchan >= 0) {
07909 span = x;
07910 break;
07911 }
07912 dchancount = 0;
07913 }
07914 if ((dchan >= 0) && (span >= 0)) {
07915 if (dchancount > 1)
07916 ast_log(LOG_WARNING, "[Span %d D-Channel %d] PRI: %s", span, dchan, s);
07917 else
07918 ast_verbose("%s", s);
07919 } else
07920 ast_verbose("PRI debug error: could not find pri associated it with debug message output\n");
07921 } else
07922 ast_log(LOG_WARNING, "%s", s);
07923
07924 ast_mutex_lock(&pridebugfdlock);
07925
07926 if (pridebugfd >= 0)
07927 write(pridebugfd, s, strlen(s));
07928
07929 ast_mutex_unlock(&pridebugfdlock);
07930 }
07931
07932 static int pri_check_restart(struct zt_pri *pri)
07933 {
07934 do {
07935 pri->resetpos++;
07936 } while((pri->resetpos < pri->numchans) &&
07937 (!pri->pvts[pri->resetpos] ||
07938 pri->pvts[pri->resetpos]->call ||
07939 pri->pvts[pri->resetpos]->resetting));
07940 if (pri->resetpos < pri->numchans) {
07941
07942 pri->pvts[pri->resetpos]->resetting = 1;
07943 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
07944 } else {
07945 pri->resetting = 0;
07946 time(&pri->lastreset);
07947 }
07948 return 0;
07949 }
07950
07951 static int pri_hangup_all(struct zt_pvt *p, struct zt_pri *pri)
07952 {
07953 int x;
07954 int redo;
07955 ast_mutex_unlock(&pri->lock);
07956 ast_mutex_lock(&p->lock);
07957 do {
07958 redo = 0;
07959 for (x=0;x<3;x++) {
07960 while(p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) {
07961 redo++;
07962 ast_mutex_unlock(&p->lock);
07963 usleep(1);
07964 ast_mutex_lock(&p->lock);
07965 }
07966 if (p->subs[x].owner) {
07967 ast_queue_hangup(p->subs[x].owner);
07968 ast_mutex_unlock(&p->subs[x].owner->lock);
07969 }
07970 }
07971 } while (redo);
07972 ast_mutex_unlock(&p->lock);
07973 ast_mutex_lock(&pri->lock);
07974 return 0;
07975 }
07976 char * redirectingreason2str(int redirectingreason)
07977 {
07978 switch (redirectingreason) {
07979 case 0:
07980 return "UNKNOWN";
07981 case 1:
07982 return "BUSY";
07983 case 2:
07984 return "NO_REPLY";
07985 case 0xF:
07986 return "UNCONDITIONAL";
07987 default:
07988 return "NOREDIRECT";
07989 }
07990 }
07991
07992 static void apply_plan_to_number(char *buf, size_t size, const struct zt_pri *pri, const char *number, const int plan)
07993 {
07994 switch (plan) {
07995 case PRI_INTERNATIONAL_ISDN:
07996 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
07997 break;
07998 case PRI_NATIONAL_ISDN:
07999 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
08000 break;
08001 case PRI_LOCAL_ISDN:
08002 snprintf(buf, size, "%s%s", pri->localprefix, number);
08003 break;
08004 case PRI_PRIVATE:
08005 snprintf(buf, size, "%s%s", pri->privateprefix, number);
08006 break;
08007 case PRI_UNKNOWN:
08008 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
08009 break;
08010 default:
08011 snprintf(buf, size, "%s", number);
08012 break;
08013 }
08014 }
08015
08016 static void *pri_dchannel(void *vpri)
08017 {
08018 struct zt_pri *pri = vpri;
08019 pri_event *e;
08020 struct pollfd fds[NUM_DCHANS];
08021 int res;
08022 int chanpos = 0;
08023 int x;
08024 int haveidles;
08025 int activeidles;
08026 int nextidle = -1;
08027 struct ast_channel *c;
08028 struct timeval tv, lowest, *next;
08029 struct timeval lastidle = { 0, 0 };
08030 int doidling=0;
08031 char *cc;
08032 char idlen[80];
08033 struct ast_channel *idle;
08034 pthread_t p;
08035 time_t t;
08036 int i, which=-1;
08037 int numdchans;
08038 int cause=0;
08039 struct zt_pvt *crv;
08040 pthread_t threadid;
08041 pthread_attr_t attr;
08042 char ani2str[6];
08043 char plancallingnum[256];
08044 char plancallingani[256];
08045 char calledtonstr[10];
08046
08047 pthread_attr_init(&attr);
08048 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
08049
08050 gettimeofday(&lastidle, NULL);
08051 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
08052
08053 cc = strchr(pri->idleext, '@');
08054 if (cc) {
08055 *cc = '\0';
08056 cc++;
08057 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
08058 #if 0
08059
08060 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
08061 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
08062 else
08063 #endif
08064 doidling = 1;
08065 } else
08066 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
08067 }
08068 for(;;) {
08069 for (i=0;i<NUM_DCHANS;i++) {
08070 if (!pri->dchannels[i])
08071 break;
08072 fds[i].fd = pri->fds[i];
08073 fds[i].events = POLLIN | POLLPRI;
08074 fds[i].revents = 0;
08075 }
08076 numdchans = i;
08077 time(&t);
08078 ast_mutex_lock(&pri->lock);
08079 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->resetinterval > 0)) {
08080 if (pri->resetting && pri_is_up(pri)) {
08081 if (pri->resetpos < 0)
08082 pri_check_restart(pri);
08083 } else {
08084 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
08085 pri->resetting = 1;
08086 pri->resetpos = -1;
08087 }
08088 }
08089 }
08090
08091 if (doidling && pri_is_up(pri)) {
08092 nextidle = -1;
08093 haveidles = 0;
08094 activeidles = 0;
08095 for (x=pri->numchans;x>=0;x--) {
08096 if (pri->pvts[x] && !pri->pvts[x]->owner &&
08097 !pri->pvts[x]->call) {
08098 if (haveidles < pri->minunused) {
08099 haveidles++;
08100 } else if (!pri->pvts[x]->resetting) {
08101 nextidle = x;
08102 break;
08103 }
08104 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
08105 activeidles++;
08106 }
08107 #if 0
08108 printf("nextidle: %d, haveidles: %d, minunsed: %d\n",
08109 nextidle, haveidles, minunused);
08110 printf("nextidle: %d, haveidles: %d, ms: %ld, minunsed: %d\n",
08111 nextidle, haveidles, ast_tvdiff_ms(ast_tvnow(), lastidle), minunused);
08112 #endif
08113 if (nextidle > -1) {
08114 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
08115
08116 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
08117 idle = zt_request("Zap", AST_FORMAT_ULAW, idlen, &cause);
08118 if (idle) {
08119 pri->pvts[nextidle]->isidlecall = 1;
08120 if (ast_pthread_create(&p, NULL, do_idle_thread, idle)) {
08121 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
08122 zt_hangup(idle);
08123 }
08124 } else
08125 ast_log(LOG_WARNING, "Unable to request channel 'Zap/%s' for idle call\n", idlen);
08126 gettimeofday(&lastidle, NULL);
08127 }
08128 } else if ((haveidles < pri->minunused) &&
08129 (activeidles > pri->minidle)) {
08130
08131
08132 for (x=pri->numchans;x>=0;x--) {
08133
08134 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
08135 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08136 haveidles++;
08137
08138
08139 if ((haveidles >= pri->minunused) ||
08140 (activeidles <= pri->minidle))
08141 break;
08142 }
08143 }
08144 }
08145 }
08146
08147 lowest = ast_tv(60, 0);
08148 for (i=0; i<NUM_DCHANS; i++) {
08149
08150 if (!pri->dchannels[i])
08151 break;
08152 if ((next = pri_schedule_next(pri->dchans[i]))) {
08153
08154 tv = ast_tvsub(*next, ast_tvnow());
08155 if (tv.tv_sec < 0) {
08156 tv = ast_tv(0,0);
08157 }
08158 if (doidling || pri->resetting) {
08159 if (tv.tv_sec > 1) {
08160 tv = ast_tv(1, 0);
08161 }
08162 } else {
08163 if (tv.tv_sec > 60) {
08164 tv = ast_tv(60, 0);
08165 }
08166 }
08167 } else if (doidling || pri->resetting) {
08168
08169
08170 tv = ast_tv(1,0);
08171 } else {
08172
08173 tv = ast_tv(60, 0);
08174 }
08175 if (!i || ast_tvcmp(tv, lowest) < 0) {
08176 lowest = tv;
08177 }
08178 }
08179 ast_mutex_unlock(&pri->lock);
08180
08181 e = NULL;
08182 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
08183
08184 ast_mutex_lock(&pri->lock);
08185 if (!res) {
08186 for (which=0;which<NUM_DCHANS;which++) {
08187 if (!pri->dchans[which])
08188 break;
08189
08190 e = pri_schedule_run(pri->dchans[which]);
08191 if (e)
08192 break;
08193 }
08194 } else if (res > -1) {
08195 for (which=0;which<NUM_DCHANS;which++) {
08196 if (!pri->dchans[which])
08197 break;
08198 if (fds[which].revents & POLLPRI) {
08199
08200 x = 0;
08201 res = ioctl(pri->fds[which], ZT_GETEVENT, &x);
08202 if (x)
08203 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);
08204
08205 if (x == ZT_EVENT_ALARM) {
08206 pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
08207 pri_find_dchan(pri);
08208 } else if (x == ZT_EVENT_NOALARM) {
08209 pri->dchanavail[which] |= DCHAN_NOTINALARM;
08210 pri_restart(pri->dchans[which]);
08211 }
08212
08213 if (option_debug)
08214 ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
08215 } else if (fds[which].revents & POLLIN) {
08216 e = pri_check_event(pri->dchans[which]);
08217 }
08218 if (e)
08219 break;
08220 }
08221 } else if (errno != EINTR)
08222 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
08223
08224 if (e) {
08225 if (pri->debug)
08226 pri_dump_event(pri->dchans[which], e);
08227 if (e->e != PRI_EVENT_DCHAN_DOWN)
08228 pri->dchanavail[which] |= DCHAN_UP;
08229 switch(e->e) {
08230 case PRI_EVENT_DCHAN_UP:
08231 if (option_verbose > 1)
08232 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span);
08233 pri->dchanavail[which] |= DCHAN_UP;
08234 if (!pri->pri) pri_find_dchan(pri);
08235
08236
08237 time(&pri->lastreset);
08238
08239
08240 if (pri->resetinterval > -1) {
08241 pri->lastreset -= pri->resetinterval;
08242 pri->lastreset += 5;
08243 }
08244 pri->resetting = 0;
08245
08246 for (i=0; i<pri->numchans; i++)
08247 if (pri->pvts[i]) {
08248 pri->pvts[i]->inalarm = 0;
08249 }
08250 break;
08251 case PRI_EVENT_DCHAN_DOWN:
08252 if (option_verbose > 1)
08253 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span);
08254 pri->dchanavail[which] &= ~DCHAN_UP;
08255 pri_find_dchan(pri);
08256 if (!pri_is_up(pri)) {
08257 pri->resetting = 0;
08258
08259 for (i=0; i<pri->numchans; i++) {
08260 struct zt_pvt *p = pri->pvts[i];
08261 if (p) {
08262 if (p->call) {
08263 if (p->pri && p->pri->pri) {
08264 pri_hangup(p->pri->pri, p->call, -1);
08265 pri_destroycall(p->pri->pri, p->call);
08266 p->call = NULL;
08267 } else
08268 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
08269 }
08270 if (p->realcall) {
08271 pri_hangup_all(p->realcall, pri);
08272 } else if (p->owner)
08273 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08274 p->inalarm = 1;
08275 }
08276 }
08277 }
08278 break;
08279 case PRI_EVENT_RESTART:
08280 if (e->restart.channel > -1) {
08281 chanpos = pri_find_principle(pri, e->restart.channel);
08282 if (chanpos < 0)
08283 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
08284 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08285 else {
08286 if (option_verbose > 2)
08287 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n",
08288 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08289 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08290 if (pri->pvts[chanpos]->call) {
08291 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
08292 pri->pvts[chanpos]->call = NULL;
08293 }
08294
08295 if (pri->pvts[chanpos]->realcall)
08296 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08297 else if (pri->pvts[chanpos]->owner)
08298 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08299 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08300 }
08301 } else {
08302 if (option_verbose > 2)
08303 ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span);
08304 for (x=0;x < pri->numchans;x++)
08305 if (pri->pvts[x]) {
08306 ast_mutex_lock(&pri->pvts[x]->lock);
08307 if (pri->pvts[x]->call) {
08308 pri_destroycall(pri->pri, pri->pvts[x]->call);
08309 pri->pvts[x]->call = NULL;
08310 }
08311 if (pri->pvts[chanpos]->realcall)
08312 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08313 else if (pri->pvts[x]->owner)
08314 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08315 ast_mutex_unlock(&pri->pvts[x]->lock);
08316 }
08317 }
08318 break;
08319 case PRI_EVENT_KEYPAD_DIGIT:
08320 chanpos = pri_find_principle(pri, e->digit.channel);
08321 if (chanpos < 0) {
08322 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
08323 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
08324 } else {
08325 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
08326 if (chanpos > -1) {
08327 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08328
08329 if (pri->overlapdial && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
08330
08331 int digitlen = strlen(e->digit.digits);
08332 char digit;
08333 int i;
08334 for (i=0; i<digitlen; i++) {
08335 digit = e->digit.digits[i];
08336 {
08337 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08338 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08339 }
08340 }
08341 }
08342 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08343 }
08344 }
08345 break;
08346
08347 case PRI_EVENT_INFO_RECEIVED:
08348 chanpos = pri_find_principle(pri, e->ring.channel);
08349 if (chanpos < 0) {
08350 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
08351 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08352 } else {
08353 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
08354 if (chanpos > -1) {
08355 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08356
08357 if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
08358
08359 int digitlen = strlen(e->ring.callednum);
08360 char digit;
08361 int i;
08362 for (i=0; i<digitlen; i++) {
08363 digit = e->ring.callednum[i];
08364 {
08365 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08366 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08367 }
08368 }
08369 }
08370 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08371 }
08372 }
08373 break;
08374 case PRI_EVENT_RING:
08375 crv = NULL;
08376 if (e->ring.channel == -1)
08377 chanpos = pri_find_empty_chan(pri, 1);
08378 else
08379 chanpos = pri_find_principle(pri, e->ring.channel);
08380
08381 if (chanpos < 0) {
08382 ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
08383 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08384 } else {
08385 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08386 if (pri->pvts[chanpos]->owner) {
08387 if (pri->pvts[chanpos]->call == e->ring.call) {
08388 ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
08389 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08390 break;
08391 } else {
08392 ast_log(LOG_WARNING, "Ring requested on channel %d/%d already in use on span %d. Hanging up owner.\n",
08393 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08394 if (pri->pvts[chanpos]->realcall)
08395 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08396 else
08397 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08398 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08399 chanpos = -1;
08400 }
08401 }
08402 if (chanpos > -1)
08403 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08404 }
08405 if ((chanpos < 0) && (e->ring.flexible))
08406 chanpos = pri_find_empty_chan(pri, 1);
08407 if (chanpos > -1) {
08408 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08409 if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
08410
08411 crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
08412 if (crv)
08413 ast_mutex_lock(&crv->lock);
08414 if (!crv || crv->owner) {
08415 pri->pvts[chanpos]->call = NULL;
08416 if (crv) {
08417 if (crv->owner)
08418 crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08419 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);
08420 } else
08421 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);
08422 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
08423 if (crv)
08424 ast_mutex_unlock(&crv->lock);
08425 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08426 break;
08427 }
08428 }
08429 pri->pvts[chanpos]->call = e->ring.call;
08430 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
08431 if (pri->pvts[chanpos]->use_callerid) {
08432 ast_shrink_phone_number(plancallingnum);
08433 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
08434 #ifdef PRI_ANI
08435 if (!ast_strlen_zero(e->ring.callingani)) {
08436 apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
08437 ast_shrink_phone_number(plancallingani);
08438 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
08439 } else {
08440 pri->pvts[chanpos]->cid_ani[0] = '\0';
08441 }
08442 #endif
08443 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
08444 pri->pvts[chanpos]->cid_ton = e->ring.callingplan;
08445 } else {
08446 pri->pvts[chanpos]->cid_num[0] = '\0';
08447 pri->pvts[chanpos]->cid_ani[0] = '\0';
08448 pri->pvts[chanpos]->cid_name[0] = '\0';
08449 pri->pvts[chanpos]->cid_ton = 0;
08450 }
08451 apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
08452 e->ring.redirectingnum, e->ring.callingplanrdnis);
08453
08454 if (pri->pvts[chanpos]->immediate) {
08455 if (option_verbose > 2)
08456 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n");
08457 pri->pvts[chanpos]->exten[0] = 's';
08458 pri->pvts[chanpos]->exten[1] = '\0';
08459 }
08460
08461 else if (!ast_strlen_zero(e->ring.callednum)) {
08462 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
08463 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08464 } else
08465 pri->pvts[chanpos]->exten[0] = '\0';
08466
08467 if (!ast_strlen_zero(e->ring.callednum))
08468 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08469
08470 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
08471 if (option_verbose > 2)
08472 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n");
08473 pri->pvts[chanpos]->exten[0] = 's';
08474 pri->pvts[chanpos]->exten[1] = '\0';
08475 }
08476
08477 if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
08478 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08479
08480 int law;
08481 if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
08482
08483 law = 1;
08484 if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &law) == -1)
08485 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", pri->pvts[chanpos]->channel, law);
08486 }
08487 if (e->ring.layer1 == PRI_LAYER_1_ALAW)
08488 law = ZT_LAW_ALAW;
08489 else
08490 law = ZT_LAW_MULAW;
08491 res = zt_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].zfd, law);
08492 if (res < 0)
08493 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
08494 res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].zfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
08495 if (res < 0)
08496 ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
08497 if (e->ring.complete || !pri->overlapdial)
08498
08499 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
08500 else {
08501 if (pri->switchtype != PRI_SWITCH_GR303_TMC)
08502 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08503 else
08504 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08505 }
08506
08507 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
08508
08509
08510 if (pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08511
08512 ast_mutex_unlock(&pri->lock);
08513 if (crv) {
08514
08515 pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
08516 c = zt_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08517 pri->pvts[chanpos]->owner = &inuse;
08518 ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
08519 } else {
08520 c = zt_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08521 }
08522 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
08523 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
08524 }
08525 if(e->ring.ani2 >= 0) {
08526 snprintf(ani2str, 5, "%.2d", e->ring.ani2);
08527 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08528 }
08529
08530 #ifdef SUPPORT_USERUSER
08531 if (!ast_strlen_zero(e->ring.useruserinfo)) {
08532 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08533 }
08534 #endif
08535
08536 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08537 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08538 if (e->ring.redirectingreason >= 0)
08539 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08540
08541 ast_mutex_lock(&pri->lock);
08542 if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) {
08543 if (option_verbose > 2)
08544 ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
08545 plancallingnum, !ast_strlen_zero(pri->pvts[chanpos]->exten) ? pri->pvts[chanpos]->exten : "<unspecified>",
08546 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08547 } else {
08548 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
08549 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08550 if (c)
08551 ast_hangup(c);
08552 else {
08553 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08554 pri->pvts[chanpos]->call = NULL;
08555 }
08556 }
08557 } else {
08558 ast_mutex_unlock(&pri->lock);
08559
08560 c = zt_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype);
08561 ast_mutex_lock(&pri->lock);
08562 if (c) {
08563 char calledtonstr[10];
08564 if(e->ring.ani2 >= 0) {
08565 snprintf(ani2str, 5, "%d", e->ring.ani2);
08566 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08567 }
08568
08569 #ifdef SUPPORT_USERUSER
08570 if (!ast_strlen_zero(e->ring.useruserinfo)) {
08571 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08572 }
08573 #endif
08574
08575 if (e->ring.redirectingreason >= 0)
08576 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08577
08578 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08579 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08580 if (option_verbose > 2)
08581 ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
08582 plancallingnum, pri->pvts[chanpos]->exten,
08583 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08584 zt_enable_ec(pri->pvts[chanpos]);
08585 } else {
08586 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
08587 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08588 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08589 pri->pvts[chanpos]->call = NULL;
08590 }
08591 }
08592 } else {
08593 if (option_verbose > 2)
08594 ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
08595 pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
08596 pri->pvts[chanpos]->prioffset, pri->span);
08597 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
08598 pri->pvts[chanpos]->call = NULL;
08599 pri->pvts[chanpos]->exten[0] = '\0';
08600 }
08601 if (crv)
08602 ast_mutex_unlock(&crv->lock);
08603 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08604 } else
08605 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
08606 break;
08607 case PRI_EVENT_RINGING:
08608 chanpos = pri_find_principle(pri, e->ringing.channel);
08609 if (chanpos < 0) {
08610 ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
08611 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08612 chanpos = -1;
08613 }
08614 if (chanpos > -1) {
08615 chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
08616 if (chanpos < 0) {
08617 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
08618 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08619 chanpos = -1;
08620 } else {
08621 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08622 if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08623 zt_enable_ec(pri->pvts[chanpos]);
08624 pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1;
08625 pri->pvts[chanpos]->alerting = 1;
08626 } else
08627 ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n");
08628 #ifdef PRI_PROGRESS_MASK
08629 if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08630 #else
08631 if (e->ringing.progress == 8) {
08632 #endif
08633
08634 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08635
08636 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING);
08637 pri->pvts[chanpos]->dsp_features = 0;
08638 }
08639 }
08640
08641 #ifdef SUPPORT_USERUSER
08642 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
08643 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->ringing.useruserinfo);
08644 }
08645 #endif
08646
08647 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08648 }
08649 }
08650 break;
08651 case PRI_EVENT_PROGRESS:
08652
08653 chanpos = pri_find_principle(pri, e->proceeding.channel);
08654 if (chanpos > -1) {
08655 #ifdef PRI_PROGRESS_MASK
08656 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) {
08657 #else
08658 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) {
08659 #endif
08660 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
08661
08662 if (e->proceeding.cause > -1) {
08663 if (option_verbose > 2)
08664 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with cause code %d received\n", e->proceeding.cause);
08665
08666
08667 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
08668 if (pri->pvts[chanpos]->owner) {
08669 if (option_verbose > 2)
08670 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
08671
08672 pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
08673 f.subclass = AST_CONTROL_BUSY;
08674 }
08675 }
08676 }
08677
08678 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08679 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
08680 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08681 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08682 #ifdef PRI_PROGRESS_MASK
08683 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08684 #else
08685 if (e->proceeding.progress == 8) {
08686 #endif
08687
08688 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08689 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08690 pri->pvts[chanpos]->dsp_features = 0;
08691 }
08692 }
08693 pri->pvts[chanpos]->progress = 1;
08694 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08695 }
08696 }
08697 break;
08698 case PRI_EVENT_PROCEEDING:
08699 chanpos = pri_find_principle(pri, e->proceeding.channel);
08700 if (chanpos > -1) {
08701 if (!pri->pvts[chanpos]->proceeding) {
08702 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
08703
08704 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08705 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
08706 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08707 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08708 #ifdef PRI_PROGRESS_MASK
08709 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08710 #else
08711 if (e->proceeding.progress == 8) {
08712 #endif
08713
08714 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08715 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08716 pri->pvts[chanpos]->dsp_features = 0;
08717 }
08718
08719 f.subclass = AST_CONTROL_PROGRESS;
08720 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08721 }
08722 pri->pvts[chanpos]->proceeding = 1;
08723 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08724 }
08725 }
08726 break;
08727 case PRI_EVENT_FACNAME:
08728 chanpos = pri_find_principle(pri, e->facname.channel);
08729 if (chanpos < 0) {
08730 ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
08731 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08732 chanpos = -1;
08733 }
08734 if (chanpos > -1) {
08735 chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
08736 if (chanpos < 0) {
08737 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
08738 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08739 chanpos = -1;
08740 } else {
08741
08742 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08743 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
08744 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
08745 pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
08746 zt_enable_ec(pri->pvts[chanpos]);
08747 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08748 }
08749 }
08750 break;
08751 case PRI_EVENT_ANSWER:
08752 chanpos = pri_find_principle(pri, e->answer.channel);
08753 if (chanpos < 0) {
08754 ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
08755 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
08756 chanpos = -1;
08757 }
08758 if (chanpos > -1) {
08759 chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
08760 if (chanpos < 0) {
08761 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
08762 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
08763 chanpos = -1;
08764 } else {
08765 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08766
08767
08768
08769
08770
08771 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08772 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08773 pri->pvts[chanpos]->dsp_features = 0;
08774 }
08775 if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) {
08776 ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n");
08777 x = ZT_START;
08778 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_HOOK, &x);
08779 if (res < 0) {
08780 if (errno != EINPROGRESS) {
08781 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
08782 }
08783 }
08784 } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08785 pri->pvts[chanpos]->dialing = 1;
08786
08787 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_DIAL, &pri->pvts[chanpos]->dop);
08788 if (res < 0) {
08789 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", pri->pvts[chanpos]->channel);
08790 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
08791 } else
08792 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
08793 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
08794 } else if (pri->pvts[chanpos]->confirmanswer) {
08795 ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
08796 } else {
08797 pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
08798
08799 zt_enable_ec(pri->pvts[chanpos]);
08800 }
08801
08802 #ifdef SUPPORT_USERUSER
08803 if (!ast_strlen_zero(e->answer.useruserinfo)) {
08804 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->answer.useruserinfo);
08805 }
08806 #endif
08807
08808 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08809 }
08810 }
08811 break;
08812 case PRI_EVENT_HANGUP:
08813 chanpos = pri_find_principle(pri, e->hangup.channel);
08814 if (chanpos < 0) {
08815 ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
08816 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08817 chanpos = -1;
08818 }
08819 if (chanpos > -1) {
08820 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
08821 if (chanpos > -1) {
08822 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08823 if (!pri->pvts[chanpos]->alreadyhungup) {
08824
08825 pri->pvts[chanpos]->alreadyhungup = 1;
08826 if (pri->pvts[chanpos]->realcall)
08827 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08828 else if (pri->pvts[chanpos]->owner) {
08829
08830 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
08831 switch(e->hangup.cause) {
08832 case PRI_CAUSE_USER_BUSY:
08833 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
08834 break;
08835 case PRI_CAUSE_CALL_REJECTED:
08836 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
08837 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
08838 case PRI_CAUSE_SWITCH_CONGESTION:
08839 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
08840 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
08841 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
08842 break;
08843 default:
08844 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08845 }
08846 }
08847 if (option_verbose > 2)
08848 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup\n",
08849 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08850 } else {
08851 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
08852 pri->pvts[chanpos]->call = NULL;
08853 }
08854 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
08855 if (option_verbose > 2)
08856 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
08857 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08858 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
08859 pri->pvts[chanpos]->resetting = 1;
08860 }
08861 if (e->hangup.aoc_units > -1)
08862 if (option_verbose > 2)
08863 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
08864 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
08865
08866 #ifdef SUPPORT_USERUSER
08867 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
08868 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
08869 }
08870 #endif
08871
08872 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08873 } else {
08874 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
08875 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08876 }
08877 }
08878 break;
08879 #ifndef PRI_EVENT_HANGUP_REQ
08880 #error please update libpri
08881 #endif
08882 case PRI_EVENT_HANGUP_REQ:
08883 chanpos = pri_find_principle(pri, e->hangup.channel);
08884 if (chanpos < 0) {
08885 ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
08886 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08887 chanpos = -1;
08888 }
08889 if (chanpos > -1) {
08890 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
08891 if (chanpos > -1) {
08892 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08893 if (pri->pvts[chanpos]->realcall)
08894 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08895 else if (pri->pvts[chanpos]->owner) {
08896 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
08897 switch(e->hangup.cause) {
08898 case PRI_CAUSE_USER_BUSY:
08899 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
08900 break;
08901 case PRI_CAUSE_CALL_REJECTED:
08902 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
08903 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
08904 case PRI_CAUSE_SWITCH_CONGESTION:
08905 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
08906 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
08907 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
08908 break;
08909 default:
08910 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08911 }
08912 if (option_verbose > 2)
08913 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);
08914 if (e->hangup.aoc_units > -1)
08915 if (option_verbose > 2)
08916 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
08917 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
08918 } else {
08919 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
08920 pri->pvts[chanpos]->call = NULL;
08921 }
08922 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
08923 if (option_verbose > 2)
08924 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n",
08925 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08926 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
08927 pri->pvts[chanpos]->resetting = 1;
08928 }
08929
08930 #ifdef SUPPORT_USERUSER
08931 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
08932 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
08933 }
08934 #endif
08935
08936 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08937 } else {
08938 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);
08939 }
08940 }
08941 break;
08942 case PRI_EVENT_HANGUP_ACK:
08943 chanpos = pri_find_principle(pri, e->hangup.channel);
08944 if (chanpos < 0) {
08945 ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
08946 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08947 chanpos = -1;
08948 }
08949 if (chanpos > -1) {
08950 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
08951 if (chanpos > -1) {
08952 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08953 pri->pvts[chanpos]->call = NULL;
08954 pri->pvts[chanpos]->resetting = 0;
08955 if (pri->pvts[chanpos]->owner) {
08956 if (option_verbose > 2)
08957 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);
08958 }
08959
08960 #ifdef SUPPORT_USERUSER
08961 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
08962 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
08963 }
08964 #endif
08965
08966 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08967 }
08968 }
08969 break;
08970 case PRI_EVENT_CONFIG_ERR:
08971 ast_log(LOG_WARNING, "PRI Error: %s\n", e->err.err);
08972 break;
08973 case PRI_EVENT_RESTART_ACK:
08974 chanpos = pri_find_principle(pri, e->restartack.channel);
08975 if (chanpos < 0) {
08976
08977
08978
08979 for (x=0;x<pri->numchans;x++) {
08980 if (pri->pvts[x] && pri->pvts[x]->resetting) {
08981 chanpos = x;
08982 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08983 ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
08984 pri->pvts[chanpos]->prioffset, pri->span);
08985 if (pri->pvts[chanpos]->realcall)
08986 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08987 else if (pri->pvts[chanpos]->owner) {
08988 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
08989 pri->pvts[chanpos]->prioffset, pri->span);
08990 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08991 }
08992 pri->pvts[chanpos]->resetting = 0;
08993 if (option_verbose > 2)
08994 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
08995 pri->pvts[chanpos]->prioffset, pri->span);
08996 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08997 if (pri->resetting)
08998 pri_check_restart(pri);
08999 break;
09000 }
09001 }
09002 if (chanpos < 0) {
09003 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
09004 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09005 }
09006 chanpos = -1;
09007 }
09008 if (chanpos > -1) {
09009 if (pri->pvts[chanpos]) {
09010 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09011 if (pri->pvts[chanpos]->realcall)
09012 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09013 else if (pri->pvts[chanpos]->owner) {
09014 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
09015 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09016 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09017 }
09018 pri->pvts[chanpos]->resetting = 0;
09019 if (option_verbose > 2)
09020 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
09021 pri->pvts[chanpos]->prioffset, pri->span);
09022 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09023 if (pri->resetting)
09024 pri_check_restart(pri);
09025 }
09026 }
09027 break;
09028 case PRI_EVENT_SETUP_ACK:
09029 chanpos = pri_find_principle(pri, e->setup_ack.channel);
09030 if (chanpos < 0) {
09031 ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
09032 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
09033 } else {
09034 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09035 pri->pvts[chanpos]->setup_ack = 1;
09036
09037 for (x=0;x<strlen(pri->pvts[chanpos]->dialdest);x++) {
09038 ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
09039 pri_information(pri->pri, pri->pvts[chanpos]->call,
09040 pri->pvts[chanpos]->dialdest[x]);
09041 }
09042 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09043 }
09044 break;
09045 case PRI_EVENT_NOTIFY:
09046 chanpos = pri_find_principle(pri, e->notify.channel);
09047 if (chanpos < 0) {
09048 ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
09049 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
09050 } else {
09051 struct ast_frame f = { AST_FRAME_CONTROL, };
09052 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09053 switch(e->notify.info) {
09054 case PRI_NOTIFY_REMOTE_HOLD:
09055 f.subclass = AST_CONTROL_HOLD;
09056 zap_queue_frame(pri->pvts[chanpos], &f, pri);
09057 break;
09058 case PRI_NOTIFY_REMOTE_RETRIEVAL:
09059 f.subclass = AST_CONTROL_UNHOLD;
09060 zap_queue_frame(pri->pvts[chanpos], &f, pri);
09061 break;
09062 }
09063 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09064 }
09065 break;
09066 default:
09067 ast_log(LOG_DEBUG, "Event: %d\n", e->e);
09068 }
09069 }
09070 ast_mutex_unlock(&pri->lock);
09071 }
09072
09073 return NULL;
09074 }
09075
09076 static int start_pri(struct zt_pri *pri)
09077 {
09078 int res, x;
09079 ZT_PARAMS p;
09080 ZT_BUFFERINFO bi;
09081 struct zt_spaninfo si;
09082 int i;
09083
09084 for (i=0;i<NUM_DCHANS;i++) {
09085 if (!pri->dchannels[i])
09086 break;
09087 pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600);
09088 x = pri->dchannels[i];
09089 if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],ZT_SPECIFY,&x) == -1)) {
09090 ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
09091 return -1;
09092 }
09093 res = ioctl(pri->fds[i], ZT_GET_PARAMS, &p);
09094 if (res) {
09095 zt_close(pri->fds[i]);
09096 pri->fds[i] = -1;
09097 ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
09098 return -1;
09099 }
09100 if (p.sigtype != ZT_SIG_HDLCFCS) {
09101 zt_close(pri->fds[i]);
09102 pri->fds[i] = -1;
09103 ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode. See /etc/zaptel.conf\n", x);
09104 return -1;
09105 }
09106 memset(&si, 0, sizeof(si));
09107 res = ioctl(pri->fds[i], ZT_SPANSTAT, &si);
09108 if (res) {
09109 zt_close(pri->fds[i]);
09110 pri->fds[i] = -1;
09111 ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
09112 }
09113 if (!si.alarms)
09114 pri->dchanavail[i] |= DCHAN_NOTINALARM;
09115 else
09116 pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
09117 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
09118 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
09119 bi.numbufs = 32;
09120 bi.bufsize = 1024;
09121 if (ioctl(pri->fds[i], ZT_SET_BUFINFO, &bi)) {
09122 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d\n", x);
09123 zt_close(pri->fds[i]);
09124 pri->fds[i] = -1;
09125 return -1;
09126 }
09127 pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
09128
09129 if (pri->switchtype == PRI_SWITCH_GR303_TMC)
09130 pri->overlapdial = 1;
09131 pri_set_overlapdial(pri->dchans[i],pri->overlapdial);
09132
09133 if (i)
09134 pri_enslave(pri->dchans[0], pri->dchans[i]);
09135 if (!pri->dchans[i]) {
09136 zt_close(pri->fds[i]);
09137 pri->fds[i] = -1;
09138 ast_log(LOG_ERROR, "Unable to create PRI structure\n");
09139 return -1;
09140 }
09141 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
09142 pri_set_nsf(pri->dchans[i], pri->nsf);
09143 #ifdef PRI_GETSET_TIMERS
09144 for (x = 0; x < PRI_MAX_TIMERS; x++) {
09145 if (pritimers[x] != 0)
09146 pri_set_timer(pri->dchans[i], x, pritimers[x]);
09147 }
09148 #endif
09149 }
09150
09151 pri->pri = pri->dchans[0];
09152 pri->resetpos = -1;
09153 if (ast_pthread_create(&pri->master, NULL, pri_dchannel, pri)) {
09154 for (i=0;i<NUM_DCHANS;i++) {
09155 if (!pri->dchannels[i])
09156 break;
09157 zt_close(pri->fds[i]);
09158 pri->fds[i] = -1;
09159 }
09160 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
09161 return -1;
09162 }
09163 return 0;
09164 }
09165
09166 static char *complete_span_helper(char *line, char *word, int pos, int state, int rpos)
09167 {
09168 int span=1;
09169 char tmp[50];
09170 if (pos != rpos)
09171 return 0;
09172 while(span <= NUM_SPANS) {
09173 if (span > state && pris[span-1].pri)
09174 break;
09175 span++;
09176 }
09177 if (span <= NUM_SPANS) {
09178 snprintf(tmp, sizeof(tmp), "%d", span);
09179 return strdup(tmp);
09180 } else
09181 return NULL;
09182 }
09183
09184 static char *complete_span_4(char *line, char *word, int pos, int state)
09185 {
09186 return complete_span_helper(line,word,pos,state,3);
09187 }
09188
09189 static char *complete_span_5(char *line, char *word, int pos, int state)
09190 {
09191 return complete_span_helper(line,word,pos,state,4);
09192 }
09193
09194 static int handle_pri_set_debug_file(int fd, int argc, char **argv)
09195 {
09196 int myfd;
09197
09198 if (!strncasecmp(argv[1], "set", 3)) {
09199 if (argc < 5)
09200 return RESULT_SHOWUSAGE;
09201
09202 if (ast_strlen_zero(argv[4]))
09203 return RESULT_SHOWUSAGE;
09204
09205 myfd = open(argv[4], O_CREAT|O_WRONLY);
09206 if (myfd < 0) {
09207 ast_cli(fd, "Unable to open '%s' for writing\n", argv[4]);
09208 return RESULT_SUCCESS;
09209 }
09210
09211 ast_mutex_lock(&pridebugfdlock);
09212
09213 if (pridebugfd >= 0)
09214 close(pridebugfd);
09215
09216 pridebugfd = myfd;
09217 ast_copy_string(pridebugfilename,argv[4],sizeof(pridebugfilename));
09218
09219 ast_mutex_unlock(&pridebugfdlock);
09220
09221 ast_cli(fd, "PRI debug output will be sent to '%s'\n", argv[4]);
09222 } else {
09223
09224 ast_mutex_lock(&pridebugfdlock);
09225 close(pridebugfd);
09226 pridebugfd = -1;
09227 ast_cli(fd, "PRI debug output to file disabled\n");
09228 ast_mutex_unlock(&pridebugfdlock);
09229 }
09230
09231 return RESULT_SUCCESS;
09232 }
09233
09234 static int handle_pri_debug(int fd, int argc, char *argv[])
09235 {
09236 int span;
09237 int x;
09238 if (argc < 4) {
09239 return RESULT_SHOWUSAGE;
09240 }
09241 span = atoi(argv[3]);
09242 if ((span < 1) || (span > NUM_SPANS)) {
09243 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[3], 1, NUM_SPANS);
09244 return RESULT_SUCCESS;
09245 }
09246 if (!pris[span-1].pri) {
09247 ast_cli(fd, "No PRI running on span %d\n", span);
09248 return RESULT_SUCCESS;
09249 }
09250 for (x=0;x<NUM_DCHANS;x++) {
09251 if (pris[span-1].dchans[x])
09252 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE);
09253 }
09254 ast_cli(fd, "Enabled debugging on span %d\n", span);
09255 return RESULT_SUCCESS;
09256 }
09257
09258
09259
09260 static int handle_pri_no_debug(int fd, int argc, char *argv[])
09261 {
09262 int span;
09263 int x;
09264 if (argc < 5)
09265 return RESULT_SHOWUSAGE;
09266 span = atoi(argv[4]);
09267 if ((span < 1) || (span > NUM_SPANS)) {
09268 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09269 return RESULT_SUCCESS;
09270 }
09271 if (!pris[span-1].pri) {
09272 ast_cli(fd, "No PRI running on span %d\n", span);
09273 return RESULT_SUCCESS;
09274 }
09275 for (x=0;x<NUM_DCHANS;x++) {
09276 if (pris[span-1].dchans[x])
09277 pri_set_debug(pris[span-1].dchans[x], 0);
09278 }
09279 ast_cli(fd, "Disabled debugging on span %d\n", span);
09280 return RESULT_SUCCESS;
09281 }
09282
09283 static int handle_pri_really_debug(int fd, int argc, char *argv[])
09284 {
09285 int span;
09286 int x;
09287 if (argc < 5)
09288 return RESULT_SHOWUSAGE;
09289 span = atoi(argv[4]);
09290 if ((span < 1) || (span > NUM_SPANS)) {
09291 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09292 return RESULT_SUCCESS;
09293 }
09294 if (!pris[span-1].pri) {
09295 ast_cli(fd, "No PRI running on span %d\n", span);
09296 return RESULT_SUCCESS;
09297 }
09298 for (x=0;x<NUM_DCHANS;x++) {
09299 if (pris[span-1].dchans[x])
09300 pri_set_debug(pris[span-1].dchans[x], (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE));
09301 }
09302 ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span);
09303 return RESULT_SUCCESS;
09304 }
09305
09306 static void build_status(char *s, size_t len, int status, int active)
09307 {
09308 if (!s || len < 1) {
09309 return;
09310 }
09311 s[0] = '\0';
09312 if (status & DCHAN_PROVISIONED)
09313 strncat(s, "Provisioned, ", len - strlen(s) - 1);
09314 if (!(status & DCHAN_NOTINALARM))
09315 strncat(s, "In Alarm, ", len - strlen(s) - 1);
09316 if (status & DCHAN_UP)
09317 strncat(s, "Up", len - strlen(s) - 1);
09318 else
09319 strncat(s, "Down", len - strlen(s) - 1);
09320 if (active)
09321 strncat(s, ", Active", len - strlen(s) - 1);
09322 else
09323 strncat(s, ", Standby", len - strlen(s) - 1);
09324 s[len - 1] = '\0';
09325 }
09326
09327 static int handle_pri_show_span(int fd, int argc, char *argv[])
09328 {
09329 int span;
09330 int x;
09331 char status[256];
09332 if (argc < 4)
09333 return RESULT_SHOWUSAGE;
09334 span = atoi(argv[3]);
09335 if ((span < 1) || (span > NUM_SPANS)) {
09336 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09337 return RESULT_SUCCESS;
09338 }
09339 if (!pris[span-1].pri) {
09340 ast_cli(fd, "No PRI running on span %d\n", span);
09341 return RESULT_SUCCESS;
09342 }
09343 for(x=0;x<NUM_DCHANS;x++) {
09344 if (pris[span-1].dchannels[x]) {
09345 #ifdef PRI_DUMP_INFO_STR
09346 char *info_str = NULL;
09347 #endif
09348 ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
09349 build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
09350 ast_cli(fd, "Status: %s\n", status);
09351 #ifdef PRI_DUMP_INFO_STR
09352 info_str = pri_dump_info_str(pris[span-1].pri);
09353 if (info_str) {
09354 ast_cli(fd, "%s", info_str);
09355 free(info_str);
09356 }
09357 #else
09358 pri_dump_info(pris[span-1].pri);
09359 #endif
09360 ast_cli(fd, "\n");
09361 }
09362 }
09363 return RESULT_SUCCESS;
09364 }
09365
09366 static int handle_pri_show_debug(int fd, int argc, char *argv[])
09367 {
09368 int x;
09369 int span;
09370 int count=0;
09371 int debug=0;
09372
09373 for(span=0;span<NUM_SPANS;span++) {
09374 if (pris[span].pri) {
09375 for(x=0;x<NUM_DCHANS;x++) {
09376 debug=0;
09377 if (pris[span].dchans[x]) {
09378 debug = pri_get_debug(pris[span].dchans[x]);
09379 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" );
09380 count++;
09381 }
09382 }
09383 }
09384
09385 }
09386 ast_mutex_lock(&pridebugfdlock);
09387 if (pridebugfd >= 0)
09388 ast_cli(fd, "Logging PRI debug to file %s\n", pridebugfilename);
09389 ast_mutex_unlock(&pridebugfdlock);
09390
09391 if (!count)
09392 ast_cli(fd, "No debug set or no PRI running\n");
09393 return RESULT_SUCCESS;
09394 }
09395
09396 static char pri_debug_help[] =
09397 "Usage: pri debug span <span>\n"
09398 " Enables debugging on a given PRI span\n";
09399
09400 static char pri_no_debug_help[] =
09401 "Usage: pri no debug span <span>\n"
09402 " Disables debugging on a given PRI span\n";
09403
09404 static char pri_really_debug_help[] =
09405 "Usage: pri intensive debug span <span>\n"
09406 " Enables debugging down to the Q.921 level\n";
09407
09408 static char pri_show_span_help[] =
09409 "Usage: pri show span <span>\n"
09410 " Displays PRI Information\n";
09411
09412 static struct ast_cli_entry zap_pri_cli[] = {
09413 { { "pri", "debug", "span", NULL }, handle_pri_debug,
09414 "Enables PRI debugging on a span", pri_debug_help, complete_span_4 },
09415 { { "pri", "no", "debug", "span", NULL }, handle_pri_no_debug,
09416 "Disables PRI debugging on a span", pri_no_debug_help, complete_span_5 },
09417 { { "pri", "intense", "debug", "span", NULL }, handle_pri_really_debug,
09418 "Enables REALLY INTENSE PRI debugging", pri_really_debug_help, complete_span_5 },
09419 { { "pri", "show", "span", NULL }, handle_pri_show_span,
09420 "Displays PRI Information", pri_show_span_help, complete_span_4 },
09421 { { "pri", "show", "debug", NULL }, handle_pri_show_debug,
09422 "Displays current PRI debug settings" },
09423 { { "pri", "set", "debug", "file", NULL }, handle_pri_set_debug_file,
09424 "Sends PRI debug output to the specified file" },
09425 { { "pri", "unset", "debug", "file", NULL }, handle_pri_set_debug_file,
09426 "Ends PRI debug output to file" },
09427 };
09428
09429 #endif
09430
09431
09432 #ifdef ZAPATA_R2
09433 static int handle_r2_no_debug(int fd, int argc, char *argv[])
09434 {
09435 int chan;
09436 struct zt_pvt *tmp = NULL;;
09437 if (argc < 5)
09438 return RESULT_SHOWUSAGE;
09439 chan = atoi(argv[4]);
09440 if ((chan < 1) || (chan > NUM_SPANS)) {
09441 ast_cli(fd, "Invalid channel %s. Should be a number greater than 0\n", argv[4]);
09442 return RESULT_SUCCESS;
09443 }
09444 tmp = iflist;
09445 while(tmp) {
09446 if (tmp->channel == chan) {
09447 if (tmp->r2) {
09448 mfcr2_set_debug(tmp->r2, 0);
09449 ast_cli(fd, "Disabled R2 debugging on channel %d\n", chan);
09450 return RESULT_SUCCESS;
09451 }
09452 break;
09453 }
09454 tmp = tmp->next;
09455 }
09456 if (tmp)
09457 ast_cli(fd, "No R2 running on channel %d\n", chan);
09458 else
09459 ast_cli(fd, "No such zap channel %d\n", chan);
09460 return RESULT_SUCCESS;
09461 }
09462
09463 static int handle_r2_debug(int fd, int argc, char *argv[])
09464 {
09465 int chan;
09466 struct zt_pvt *tmp = NULL;;
09467 if (argc < 4) {
09468 return RESULT_SHOWUSAGE;
09469 }
09470 chan = atoi(argv[3]);
09471 if ((chan < 1) || (chan > NUM_SPANS)) {
09472 ast_cli(fd, "Invalid channel %s. Should be a number greater than 0\n", argv[3]);
09473 return RESULT_SUCCESS;
09474 }
09475 tmp = iflist;
09476 while(tmp) {
09477 if (tmp->channel == chan) {
09478 if (tmp->r2) {
09479 mfcr2_set_debug(tmp->r2, 0xFFFFFFFF);
09480 ast_cli(fd, "Enabled R2 debugging on channel %d\n", chan);
09481 return RESULT_SUCCESS;
09482 }
09483 break;
09484 }
09485 tmp = tmp->next;
09486 }
09487 if (tmp)
09488 ast_cli(fd, "No R2 running on channel %d\n", chan);
09489 else
09490 ast_cli(fd, "No such zap channel %d\n", chan);
09491 return RESULT_SUCCESS;
09492 }
09493 static char r2_debug_help[] =
09494 "Usage: r2 debug channel <channel>\n"
09495 " Enables R2 protocol level debugging on a given channel\n";
09496
09497 static char r2_no_debug_help[] =
09498 "Usage: r2 no debug channel <channel>\n"
09499 " Enables R2 protocol level debugging on a given channel\n";
09500
09501 static struct ast_cli_entry zap_r2_cli[] = {
09502 { { "r2", "debug", "channel", NULL }, handle_r2_debug,
09503 "Enables R2 debugging on a channel", r2_debug_help },
09504 { { "r2", "no", "debug", "channel", NULL }, handle_r2_no_debug,
09505 "Disables R2 debugging on a channel", r2_no_debug_help },
09506 };
09507
09508 #endif
09509
09510 static int zap_destroy_channel(int fd, int argc, char **argv)
09511 {
09512 int channel = 0;
09513 struct zt_pvt *tmp = NULL;
09514 struct zt_pvt *prev = NULL;
09515
09516 if (argc != 4) {
09517 return RESULT_SHOWUSAGE;
09518 }
09519 channel = atoi(argv[3]);
09520
09521 tmp = iflist;
09522 while (tmp) {
09523 if (tmp->channel == channel) {
09524 destroy_channel(prev, tmp, 1);
09525 return RESULT_SUCCESS;
09526 }
09527 prev = tmp;
09528 tmp = tmp->next;
09529 }
09530 return RESULT_FAILURE;
09531 }
09532
09533 static int zap_show_channels(int fd, int argc, char **argv)
09534 {
09535 #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09536 #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09537 struct zt_pvt *tmp = NULL;
09538 char tmps[20] = "";
09539 ast_mutex_t *lock;
09540 struct zt_pvt *start;
09541 #ifdef ZAPATA_PRI
09542 int trunkgroup;
09543 struct zt_pri *pri=NULL;
09544 int x;
09545 #endif
09546
09547 lock = &iflock;
09548 start = iflist;
09549
09550 #ifdef ZAPATA_PRI
09551 if (argc == 4) {
09552 if ((trunkgroup = atoi(argv[3])) < 1)
09553 return RESULT_SHOWUSAGE;
09554 for (x=0;x<NUM_SPANS;x++) {
09555 if (pris[x].trunkgroup == trunkgroup) {
09556 pri = pris + x;
09557 break;
09558 }
09559 }
09560 if (pri) {
09561 start = pri->crvs;
09562 lock = &pri->lock;
09563 } else {
09564 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09565 return RESULT_FAILURE;
09566 }
09567 } else
09568 #endif
09569 if (argc != 3)
09570 return RESULT_SHOWUSAGE;
09571
09572 ast_mutex_lock(lock);
09573 #ifdef ZAPATA_PRI
09574 ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MusicOnHold");
09575 #else
09576 ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MusicOnHold");
09577 #endif
09578
09579 tmp = start;
09580 while (tmp) {
09581 if (tmp->channel > 0) {
09582 snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
09583 } else
09584 ast_copy_string(tmps, "pseudo", sizeof(tmps));
09585 ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->musicclass);
09586 tmp = tmp->next;
09587 }
09588 ast_mutex_unlock(lock);
09589 return RESULT_SUCCESS;
09590 #undef FORMAT
09591 #undef FORMAT2
09592 }
09593
09594 static int zap_show_channel(int fd, int argc, char **argv)
09595 {
09596 int channel;
09597 struct zt_pvt *tmp = NULL;
09598 ZT_CONFINFO ci;
09599 ZT_PARAMS ps;
09600 int x;
09601 ast_mutex_t *lock;
09602 struct zt_pvt *start;
09603 #ifdef ZAPATA_PRI
09604 char *c;
09605 int trunkgroup;
09606 struct zt_pri *pri=NULL;
09607 #endif
09608
09609 lock = &iflock;
09610 start = iflist;
09611
09612 if (argc != 4)
09613 return RESULT_SHOWUSAGE;
09614 #ifdef ZAPATA_PRI
09615 if ((c = strchr(argv[3], ':'))) {
09616 if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2)
09617 return RESULT_SHOWUSAGE;
09618 if ((trunkgroup < 1) || (channel < 1))
09619 return RESULT_SHOWUSAGE;
09620 for (x=0;x<NUM_SPANS;x++) {
09621 if (pris[x].trunkgroup == trunkgroup) {
09622 pri = pris + x;
09623 break;
09624 }
09625 }
09626 if (pri) {
09627 start = pri->crvs;
09628 lock = &pri->lock;
09629 } else {
09630 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09631 return RESULT_FAILURE;
09632 }
09633 } else
09634 #endif
09635 channel = atoi(argv[3]);
09636
09637 ast_mutex_lock(lock);
09638 tmp = start;
09639 while (tmp) {
09640 if (tmp->channel == channel) {
09641 #ifdef ZAPATA_PRI
09642 if (pri)
09643 ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
09644 else
09645 #endif
09646 ast_cli(fd, "Channel: %d\n", tmp->channel);
09647 ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].zfd);
09648 ast_cli(fd, "Span: %d\n", tmp->span);
09649 ast_cli(fd, "Extension: %s\n", tmp->exten);
09650 ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
09651 ast_cli(fd, "Context: %s\n", tmp->context);
09652 ast_cli(fd, "Caller ID: %s\n", tmp->cid_num);
09653 ast_cli(fd, "Calling TON: %d\n", tmp->cid_ton);
09654 ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name);
09655 ast_cli(fd, "Destroy: %d\n", tmp->destroy);
09656 ast_cli(fd, "InAlarm: %d\n", tmp->inalarm);
09657 ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig));
09658 ast_cli(fd, "Radio: %d\n", tmp->radio);
09659 ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
09660 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)" : "");
09661 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)" : "");
09662 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)" : "");
09663 ast_cli(fd, "Confno: %d\n", tmp->confno);
09664 ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno);
09665 ast_cli(fd, "Real in conference: %d\n", tmp->inconference);
09666 ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
09667 ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
09668 ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
09669 ast_cli(fd, "Default law: %s\n", tmp->law == ZT_LAW_MULAW ? "ulaw" : tmp->law == ZT_LAW_ALAW ? "alaw" : "unknown");
09670 ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
09671 ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
09672 ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF");
09673 if (tmp->master)
09674 ast_cli(fd, "Master Channel: %d\n", tmp->master->channel);
09675 for (x=0;x<MAX_SLAVES;x++) {
09676 if (tmp->slaves[x])
09677 ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
09678 }
09679 #ifdef ZAPATA_PRI
09680 if (tmp->pri) {
09681 ast_cli(fd, "PRI Flags: ");
09682 if (tmp->resetting)
09683 ast_cli(fd, "Resetting ");
09684 if (tmp->call)
09685 ast_cli(fd, "Call ");
09686 if (tmp->bearer)
09687 ast_cli(fd, "Bearer ");
09688 ast_cli(fd, "\n");
09689 if (tmp->logicalspan)
09690 ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan);
09691 else
09692 ast_cli(fd, "PRI Logical Span: Implicit\n");
09693 }
09694
09695 #endif
09696 #ifdef ZAPATA_R2
09697 if (tmp->r2) {
09698 ast_cli(fd, "R2 Flags: ");
09699 if (tmp->r2blocked)
09700 ast_cli(fd, "Blocked ");
09701 if (tmp->hasr2call)
09702 ast_cli(fd, "Call ");
09703 ast_cli(fd, "\n");
09704 }
09705 #endif
09706 memset(&ci, 0, sizeof(ci));
09707 ps.channo = tmp->channel;
09708 if (tmp->subs[SUB_REAL].zfd > -1) {
09709 if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
09710 ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
09711 }
09712 #ifdef ZT_GETCONFMUTE
09713 if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONFMUTE, &x)) {
09714 ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
09715 }
09716 #endif
09717 if (ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
09718 ast_log(LOG_WARNING, "Failed to get parameters on channel %d\n", tmp->channel);
09719 } else {
09720 ast_cli(fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
09721 }
09722 }
09723 ast_mutex_unlock(lock);
09724 return RESULT_SUCCESS;
09725 }
09726 tmp = tmp->next;
09727 }
09728
09729 ast_cli(fd, "Unable to find given channel %d\n", channel);
09730 ast_mutex_unlock(lock);
09731 return RESULT_FAILURE;
09732 }
09733
09734 static char zap_show_cadences_help[] =
09735 "Usage: zap show cadences\n"
09736 " Shows all cadences currently defined\n";
09737
09738 static int handle_zap_show_cadences(int fd, int argc, char *argv[])
09739 {
09740 int i, j;
09741 for (i=0;i<num_cadence;i++) {
09742 char output[1024];
09743 char tmp[16], tmp2[64];
09744 snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
09745 term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
09746
09747 for (j=0;j<16;j++) {
09748 if (cadences[i].ringcadence[j] == 0)
09749 break;
09750 snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
09751 if (cidrings[i] * 2 - 1 == j)
09752 term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
09753 else
09754 term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
09755 if (j != 0)
09756 strncat(output, ",", sizeof(output) - strlen(output) - 1);
09757 strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
09758 }
09759 ast_cli(fd,"%s\n",output);
09760 }
09761 return 0;
09762 }
09763
09764
09765 static int zap_show_status(int fd, int argc, char *argv[]) {
09766 #define FORMAT "%-40.40s %-10.10s %-10d %-10d %-10d\n"
09767 #define FORMAT2 "%-40.40s %-10.10s %-10.10s %-10.10s %-10.10s\n"
09768
09769 int span;
09770 int res;
09771 char alarms[50];
09772
09773 int ctl;
09774 ZT_SPANINFO s;
09775
09776 ctl = open("/dev/zap/ctl", O_RDWR);
09777 if (ctl < 0) {
09778 fprintf(stderr, "Unable to open /dev/zap/ctl: %s\n", strerror(errno));
09779 ast_cli(fd, "No Zaptel interface found.\n");
09780 return RESULT_FAILURE;
09781 }
09782 ast_cli(fd,FORMAT2, "Description", "Alarms","IRQ","bpviol","CRC4");
09783
09784 for (span=1;span < ZT_MAX_SPANS;++span) {
09785 s.spanno = span;
09786 res = ioctl(ctl, ZT_SPANSTAT, &s);
09787 if (res) {
09788 continue;
09789 }
09790 alarms[0] = '\0';
09791 if (s.alarms > 0) {
09792 if (s.alarms & ZT_ALARM_BLUE)
09793 strcat(alarms,"BLU/");
09794 if (s.alarms & ZT_ALARM_YELLOW)
09795 strcat(alarms, "YEL/");
09796 if (s.alarms & ZT_ALARM_RED)
09797 strcat(alarms, "RED/");
09798 if (s.alarms & ZT_ALARM_LOOPBACK)
09799 strcat(alarms,"LB/");
09800 if (s.alarms & ZT_ALARM_RECOVER)
09801 strcat(alarms,"REC/");
09802 if (s.alarms & ZT_ALARM_NOTOPEN)
09803 strcat(alarms, "NOP/");
09804 if (!strlen(alarms))
09805 strcat(alarms, "UUU/");
09806 if (strlen(alarms)) {
09807
09808 alarms[strlen(alarms)-1]='\0';
09809 }
09810 } else {
09811 if (s.numchans)
09812 strcpy(alarms, "OK");
09813 else
09814 strcpy(alarms, "UNCONFIGURED");
09815 }
09816
09817 ast_cli(fd, FORMAT, s.desc, alarms, s.irqmisses, s.bpvcount, s.crc4count);
09818 }
09819 close(ctl);
09820
09821 return RESULT_SUCCESS;
09822 #undef FORMAT
09823 #undef FORMAT2
09824 }
09825
09826 static char show_channels_usage[] =
09827 "Usage: zap show channels\n"
09828 " Shows a list of available channels\n";
09829
09830 static char show_channel_usage[] =
09831 "Usage: zap show channel <chan num>\n"
09832 " Detailed information about a given channel\n";
09833
09834 static char zap_show_status_usage[] =
09835 "Usage: zap show status\n"
09836 " Shows a list of Zaptel cards with status\n";
09837
09838 static char destroy_channel_usage[] =
09839 "Usage: zap destroy channel <chan num>\n"
09840 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n";
09841
09842 static struct ast_cli_entry zap_cli[] = {
09843 { { "zap", "show", "cadences", NULL }, handle_zap_show_cadences,
09844 "List cadences", zap_show_cadences_help },
09845 { {"zap", "show", "channels", NULL}, zap_show_channels,
09846 "Show active zapata channels", show_channels_usage },
09847 { {"zap", "show", "channel", NULL}, zap_show_channel,
09848 "Show information on a channel", show_channel_usage },
09849 { {"zap", "destroy", "channel", NULL}, zap_destroy_channel,
09850 "Destroy a channel", destroy_channel_usage },
09851 { {"zap", "show", "status", NULL}, zap_show_status,
09852 "Show all Zaptel cards status", zap_show_status_usage },
09853 };
09854
09855 #define TRANSFER 0
09856 #define HANGUP 1
09857
09858 static int zap_fake_event(struct zt_pvt *p, int mode)
09859 {
09860 if (p) {
09861 switch(mode) {
09862 case TRANSFER:
09863 p->fake_event = ZT_EVENT_WINKFLASH;
09864 break;
09865 case HANGUP:
09866 p->fake_event = ZT_EVENT_ONHOOK;
09867 break;
09868 default:
09869 ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);
09870 }
09871 }
09872 return 0;
09873 }
09874 static struct zt_pvt *find_channel(int channel)
09875 {
09876 struct zt_pvt *p = iflist;
09877 while(p) {
09878 if (p->channel == channel) {
09879 break;
09880 }
09881 p = p->next;
09882 }
09883 return p;
09884 }
09885
09886 static int action_zapdndon(struct mansession *s, struct message *m)
09887 {
09888 struct zt_pvt *p = NULL;
09889 char *channel = astman_get_header(m, "ZapChannel");
09890 if (ast_strlen_zero(channel)) {
09891 astman_send_error(s, m, "No channel specified");
09892 return 0;
09893 }
09894 p = find_channel(atoi(channel));
09895 if (!p) {
09896 astman_send_error(s, m, "No such channel");
09897 return 0;
09898 }
09899 p->dnd = 1;
09900 astman_send_ack(s, m, "DND Enabled");
09901 return 0;
09902 }
09903
09904 static int action_zapdndoff(struct mansession *s, struct message *m)
09905 {
09906 struct zt_pvt *p = NULL;
09907 char *channel = astman_get_header(m, "ZapChannel");
09908 if (ast_strlen_zero(channel)) {
09909 astman_send_error(s, m, "No channel specified");
09910 return 0;
09911 }
09912 p = find_channel(atoi(channel));
09913 if (!p) {
09914 astman_send_error(s, m, "No such channel");
09915 return 0;
09916 }
09917 p->dnd = 0;
09918 astman_send_ack(s, m, "DND Disabled");
09919 return 0;
09920 }
09921
09922 static int action_transfer(struct mansession *s, struct message *m)
09923 {
09924 struct zt_pvt *p = NULL;
09925 char *channel = astman_get_header(m, "ZapChannel");
09926 if (ast_strlen_zero(channel)) {
09927 astman_send_error(s, m, "No channel specified");
09928 return 0;
09929 }
09930 p = find_channel(atoi(channel));
09931 if (!p) {
09932 astman_send_error(s, m, "No such channel");
09933 return 0;
09934 }
09935 zap_fake_event(p,TRANSFER);
09936 astman_send_ack(s, m, "ZapTransfer");
09937 return 0;
09938 }
09939
09940 static int action_transferhangup(struct mansession *s, struct message *m)
09941 {
09942 struct zt_pvt *p = NULL;
09943 char *channel = astman_get_header(m, "ZapChannel");
09944 if (ast_strlen_zero(channel)) {
09945 astman_send_error(s, m, "No channel specified");
09946 return 0;
09947 }
09948 p = find_channel(atoi(channel));
09949 if (!p) {
09950 astman_send_error(s, m, "No such channel");
09951 return 0;
09952 }
09953 zap_fake_event(p,HANGUP);
09954 astman_send_ack(s, m, "ZapHangup");
09955 return 0;
09956 }
09957
09958 static int action_zapdialoffhook(struct mansession *s, struct message *m)
09959 {
09960 struct zt_pvt *p = NULL;
09961 char *channel = astman_get_header(m, "ZapChannel");
09962 char *number = astman_get_header(m, "Number");
09963 int i;
09964 if (ast_strlen_zero(channel)) {
09965 astman_send_error(s, m, "No channel specified");
09966 return 0;
09967 }
09968 if (ast_strlen_zero(number)) {
09969 astman_send_error(s, m, "No number specified");
09970 return 0;
09971 }
09972 p = find_channel(atoi(channel));
09973 if (!p) {
09974 astman_send_error(s, m, "No such channel");
09975 return 0;
09976 }
09977 if (!p->owner) {
09978 astman_send_error(s, m, "Channel does not have it's owner");
09979 return 0;
09980 }
09981 for (i=0; i<strlen(number); i++) {
09982 struct ast_frame f = { AST_FRAME_DTMF, number[i] };
09983 zap_queue_frame(p, &f, NULL);
09984 }
09985 astman_send_ack(s, m, "ZapDialOffhook");
09986 return 0;
09987 }
09988
09989 static int action_zapshowchannels(struct mansession *s, struct message *m)
09990 {
09991 struct zt_pvt *tmp = NULL;
09992 char *id = astman_get_header(m, "ActionID");
09993 char idText[256] = "";
09994
09995 astman_send_ack(s, m, "Zapata channel status will follow");
09996 if (!ast_strlen_zero(id))
09997 snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id);
09998
09999 ast_mutex_lock(&iflock);
10000
10001 tmp = iflist;
10002 while (tmp) {
10003 if (tmp->channel > 0) {
10004 int alarm = get_alarms(tmp);
10005 ast_cli(s->fd,
10006 "Event: ZapShowChannels\r\n"
10007 "Channel: %d\r\n"
10008 "Signalling: %s\r\n"
10009 "Context: %s\r\n"
10010 "DND: %s\r\n"
10011 "Alarm: %s\r\n"
10012 "%s"
10013 "\r\n",
10014 tmp->channel, sig2str(tmp->sig), tmp->context,
10015 tmp->dnd ? "Enabled" : "Disabled",
10016 alarm2str(alarm), idText);
10017 }
10018
10019 tmp = tmp->next;
10020 }
10021
10022 ast_mutex_unlock(&iflock);
10023
10024 ast_cli(s->fd,
10025 "Event: ZapShowChannelsComplete\r\n"
10026 "%s"
10027 "\r\n",
10028 idText);
10029 return 0;
10030 }
10031
10032 static int __unload_module(void)
10033 {
10034 int x = 0;
10035 struct zt_pvt *p, *pl;
10036 #ifdef ZAPATA_PRI
10037 int i;
10038 for(i=0;i<NUM_SPANS;i++) {
10039 if (pris[i].master != AST_PTHREADT_NULL)
10040 pthread_cancel(pris[i].master);
10041 }
10042 ast_cli_unregister_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(zap_pri_cli[0]));
10043 #endif
10044 #ifdef ZAPATA_R2
10045 ast_cli_unregister_multiple(zap_r2_cli, sizeof(zap_r2_cli) / sizeof(zap_r2_cli[0]));
10046 #endif
10047 ast_cli_unregister_multiple(zap_cli, sizeof(zap_cli) / sizeof(zap_cli[0]));
10048 ast_manager_unregister( "ZapDialOffhook" );
10049 ast_manager_unregister( "ZapHangup" );
10050 ast_manager_unregister( "ZapTransfer" );
10051 ast_manager_unregister( "ZapDNDoff" );
10052 ast_manager_unregister( "ZapDNDon" );
10053 ast_manager_unregister("ZapShowChannels");
10054 ast_channel_unregister(&zap_tech);
10055 if (!ast_mutex_lock(&iflock)) {
10056
10057 p = iflist;
10058 while(p) {
10059 if (p->owner)
10060 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
10061 p = p->next;
10062 }
10063 ast_mutex_unlock(&iflock);
10064 } else {
10065 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10066 return -1;
10067 }
10068 if (!ast_mutex_lock(&monlock)) {
10069 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
10070 pthread_cancel(monitor_thread);
10071 pthread_kill(monitor_thread, SIGURG);
10072 pthread_join(monitor_thread, NULL);
10073 }
10074 monitor_thread = AST_PTHREADT_STOP;
10075 ast_mutex_unlock(&monlock);
10076 } else {
10077 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10078 return -1;
10079 }
10080
10081 if (!ast_mutex_lock(&iflock)) {
10082
10083 p = iflist;
10084 while(p) {
10085
10086 if (p->cidspill)
10087 free(p->cidspill);
10088
10089 if (p->subs[SUB_REAL].zfd > -1)
10090 zt_close(p->subs[SUB_REAL].zfd);
10091 pl = p;
10092 p = p->next;
10093 x++;
10094
10095 if(pl)
10096 destroy_zt_pvt(&pl);
10097 ast_verbose(VERBOSE_PREFIX_3 "Unregistered channel %d\n", x);
10098 }
10099 iflist = NULL;
10100 ifcount = 0;
10101 ast_mutex_unlock(&iflock);
10102 } else {
10103 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10104 return -1;
10105 }
10106 #ifdef ZAPATA_PRI
10107 for(i=0;i<NUM_SPANS;i++) {
10108 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
10109 pthread_join(pris[i].master, NULL);
10110 zt_close(pris[i].fds[i]);
10111 }
10112 #endif
10113 return 0;
10114 }
10115
10116 int unload_module()
10117 {
10118 #ifdef ZAPATA_PRI
10119 int y;
10120 for (y=0;y<NUM_SPANS;y++)
10121 ast_mutex_destroy(&pris[y].lock);
10122 #endif
10123 return __unload_module();
10124 }
10125
10126 static int setup_zap(int reload)
10127 {
10128 struct ast_config *cfg;
10129 struct ast_variable *v;
10130 struct zt_pvt *tmp;
10131 char *chan;
10132 char *c;
10133 char *ringc;
10134 int start, finish,x;
10135 int y;
10136 int found_pseudo = 0;
10137 int cur_radio = 0;
10138 #ifdef ZAPATA_PRI
10139 int spanno;
10140 int i;
10141 int logicalspan;
10142 int trunkgroup;
10143 int dchannels[NUM_DCHANS];
10144 struct zt_pri *pri;
10145 #endif
10146
10147 cfg = ast_config_load(config);
10148
10149
10150 if (!cfg) {
10151 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
10152 return -1;
10153 }
10154
10155
10156 if (ast_mutex_lock(&iflock)) {
10157
10158 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
10159 return -1;
10160 }
10161 #ifdef ZAPATA_PRI
10162 if (!reload) {
10163
10164 v = ast_variable_browse(cfg, "trunkgroups");
10165 while(v) {
10166 if (!strcasecmp(v->name, "trunkgroup")) {
10167 trunkgroup = atoi(v->value);
10168 if (trunkgroup > 0) {
10169 if ((c = strchr(v->value, ','))) {
10170 i = 0;
10171 memset(dchannels, 0, sizeof(dchannels));
10172 while(c && (i < NUM_DCHANS)) {
10173 dchannels[i] = atoi(c + 1);
10174 if (dchannels[i] < 0) {
10175 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);
10176 } else
10177 i++;
10178 c = strchr(c + 1, ',');
10179 }
10180 if (i) {
10181 if (pri_create_trunkgroup(trunkgroup, dchannels)) {
10182 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);
10183 } else if (option_verbose > 1)
10184 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");
10185 } else
10186 ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of zapata.conf\n", trunkgroup, v->lineno);
10187 } else
10188 ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of zapata.conf\n", trunkgroup, v->lineno);
10189 } else
10190 ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of zapata.conf\n", v->lineno);
10191 } else if (!strcasecmp(v->name, "spanmap")) {
10192 spanno = atoi(v->value);
10193 if (spanno > 0) {
10194 if ((c = strchr(v->value, ','))) {
10195 trunkgroup = atoi(c + 1);
10196 if (trunkgroup > 0) {
10197 if ((c = strchr(c + 1, ',')))
10198 logicalspan = atoi(c + 1);
10199 else
10200 logicalspan = 0;
10201 if (logicalspan >= 0) {
10202 if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
10203 ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
10204 } else if (option_verbose > 1)
10205 ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
10206 } else
10207 ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of zapata.conf\n", v->lineno);
10208 } else
10209 ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of zapata.conf\n", v->lineno);
10210 } else
10211 ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of zapata.conf\n", v->lineno);
10212 } else
10213 ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of zapata.conf\n", v->lineno);
10214 } else {
10215 ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
10216 }
10217 v = v->next;
10218 }
10219 }
10220 #endif
10221 v = ast_variable_browse(cfg, "channels");
10222 while(v) {
10223
10224 if (!strcasecmp(v->name, "channel")
10225 #ifdef ZAPATA_PRI
10226 || !strcasecmp(v->name, "crv")
10227 #endif
10228 ) {
10229 if (reload == 0) {
10230 if (cur_signalling < 0) {
10231 ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
10232 ast_config_destroy(cfg);
10233 ast_mutex_unlock(&iflock);
10234 return -1;
10235 }
10236 }
10237 c = v->value;
10238
10239 #ifdef ZAPATA_PRI
10240 pri = NULL;
10241 if (!strcasecmp(v->name, "crv")) {
10242 if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) {
10243 ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", v->lineno);
10244 ast_config_destroy(cfg);
10245 ast_mutex_unlock(&iflock);
10246 return -1;
10247 }
10248 if (trunkgroup < 1) {
10249 ast_log(LOG_WARNING, "CRV trunk group must be a postive number at line %d\n", v->lineno);
10250 ast_config_destroy(cfg);
10251 ast_mutex_unlock(&iflock);
10252 return -1;
10253 }
10254 c+=y;
10255 for (y=0;y<NUM_SPANS;y++) {
10256 if (pris[y].trunkgroup == trunkgroup) {
10257 pri = pris + y;
10258 break;
10259 }
10260 }
10261 if (!pri) {
10262 ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, v->lineno);
10263 ast_config_destroy(cfg);
10264 ast_mutex_unlock(&iflock);
10265 return -1;
10266 }
10267 }
10268 #endif
10269 chan = strsep(&c, ",");
10270 while(chan) {
10271 if (sscanf(chan, "%d-%d", &start, &finish) == 2) {
10272
10273 } else if (sscanf(chan, "%d", &start)) {
10274
10275 finish = start;
10276 } else if (!strcasecmp(chan, "pseudo")) {
10277 finish = start = CHAN_PSEUDO;
10278 found_pseudo = 1;
10279 } else {
10280 ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", v->value, chan);
10281 ast_config_destroy(cfg);
10282 ast_mutex_unlock(&iflock);
10283 return -1;
10284 }
10285 if (finish < start) {
10286 ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
10287 x = finish;
10288 finish = start;
10289 start = x;
10290 }
10291 for (x=start;x<=finish;x++) {
10292 #ifdef ZAPATA_PRI
10293 tmp = mkintf(x, cur_signalling, cur_radio, pri, reload);
10294 #else
10295 tmp = mkintf(x, cur_signalling, cur_radio, NULL, reload);
10296 #endif
10297
10298 if (tmp) {
10299 if (option_verbose > 2) {
10300 #ifdef ZAPATA_PRI
10301 if (pri)
10302 ast_verbose(VERBOSE_PREFIX_3 "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup,x, sig2str(tmp->sig));
10303 else
10304 #endif
10305 ast_verbose(VERBOSE_PREFIX_3 "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig));
10306 }
10307 } else {
10308 if (reload == 1)
10309 ast_log(LOG_ERROR, "Unable to reconfigure channel '%s'\n", v->value);
10310 else
10311 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
10312 ast_config_destroy(cfg);
10313 ast_mutex_unlock(&iflock);
10314 return -1;
10315 }
10316 }
10317 chan = strsep(&c, ",");
10318 }
10319 } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
10320 if (ast_true(v->value))
10321 usedistinctiveringdetection = 1;
10322 } else if (!strcasecmp(v->name, "dring1context")) {
10323 ast_copy_string(drings.ringContext[0].contextData,v->value,sizeof(drings.ringContext[0].contextData));
10324 } else if (!strcasecmp(v->name, "dring2context")) {
10325 ast_copy_string(drings.ringContext[1].contextData,v->value,sizeof(drings.ringContext[1].contextData));
10326 } else if (!strcasecmp(v->name, "dring3context")) {
10327 ast_copy_string(drings.ringContext[2].contextData,v->value,sizeof(drings.ringContext[2].contextData));
10328 } else if (!strcasecmp(v->name, "dring1")) {
10329 ringc = v->value;
10330 sscanf(ringc, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]);
10331 } else if (!strcasecmp(v->name, "dring2")) {
10332 ringc = v->value;
10333 sscanf(ringc,"%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]);
10334 } else if (!strcasecmp(v->name, "dring3")) {
10335 ringc = v->value;
10336 sscanf(ringc, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]);
10337 } else if (!strcasecmp(v->name, "usecallerid")) {
10338 use_callerid = ast_true(v->value);
10339 } else if (!strcasecmp(v->name, "cidsignalling")) {
10340 if (!strcasecmp(v->value, "bell"))
10341 cid_signalling = CID_SIG_BELL;
10342 else if (!strcasecmp(v->value, "v23"))
10343 cid_signalling = CID_SIG_V23;
10344 else if (!strcasecmp(v->value, "dtmf"))
10345 cid_signalling = CID_SIG_DTMF;
10346 else if (ast_true(v->value))
10347 cid_signalling = CID_SIG_BELL;
10348 } else if (!strcasecmp(v->name, "cidstart")) {
10349 if (!strcasecmp(v->value, "ring"))
10350 cid_start = CID_START_RING;
10351 else if (!strcasecmp(v->value, "polarity"))
10352 cid_start = CID_START_POLARITY;
10353 else if (ast_true(v->value))
10354 cid_start = CID_START_RING;
10355 } else if (!strcasecmp(v->name, "threewaycalling")) {
10356 threewaycalling = ast_true(v->value);
10357 } else if (!strcasecmp(v->name, "cancallforward")) {
10358 cancallforward = ast_true(v->value);
10359 } else if (!strcasecmp(v->name, "relaxdtmf")) {
10360 if (ast_true(v->value))
10361 relaxdtmf = DSP_DIGITMODE_RELAXDTMF;
10362 else
10363 relaxdtmf = 0;
10364 } else if (!strcasecmp(v->name, "mailbox")) {
10365 ast_copy_string(mailbox, v->value, sizeof(mailbox));
10366 } else if (!strcasecmp(v->name, "adsi")) {
10367 adsi = ast_true(v->value);
10368 } else if (!strcasecmp(v->name, "transfer")) {
10369 transfer = ast_true(v->value);
10370 } else if (!strcasecmp(v->name, "canpark")) {
10371 canpark = ast_true(v->value);
10372 } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
10373 echocanbridged = ast_true(v->value);
10374 } else if (!strcasecmp(v->name, "busydetect")) {
10375 busydetect = ast_true(v->value);
10376 } else if (!strcasecmp(v->name, "busycount")) {
10377 busycount = atoi(v->value);
10378 } else if (!strcasecmp(v->name, "busypattern")) {
10379 if (sscanf(v->value, "%d,%d", &busy_tonelength, &busy_quietlength) != 2) {
10380 ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength\n");
10381 }
10382 } else if (!strcasecmp(v->name, "callprogress")) {
10383 if (ast_true(v->value))
10384 callprogress |= 1;
10385 else
10386 callprogress &= ~1;
10387 } else if (!strcasecmp(v->name, "faxdetect")) {
10388 if (!strcasecmp(v->value, "incoming")) {
10389 callprogress |= 4;
10390 callprogress &= ~2;
10391 } else if (!strcasecmp(v->value, "outgoing")) {
10392 callprogress &= ~4;
10393 callprogress |= 2;
10394 } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
10395 callprogress |= 6;
10396 else
10397 callprogress &= ~6;
10398 } else if (!strcasecmp(v->name, "echocancel")) {
10399 if (!ast_strlen_zero(v->value)) {
10400 y = atoi(v->value);
10401 } else
10402 y = 0;
10403 if ((y == 32) || (y == 64) || (y == 128) || (y == 256))
10404 echocancel = y;
10405 else {
10406 echocancel = ast_true(v->value);
10407 if (echocancel)
10408 echocancel=128;
10409 }
10410 } else if (!strcasecmp(v->name, "echotraining")) {
10411 if (sscanf(v->value, "%d", &y) == 1) {
10412 if ((y < 10) || (y > 4000)) {
10413 ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 2000 ms at line %d\n", v->lineno);
10414 } else {
10415 echotraining = y;
10416 }
10417 } else if (ast_true(v->value)) {
10418 echotraining = 400;
10419 } else
10420 echotraining = 0;
10421 } else if (!strcasecmp(v->name, "hidecallerid")) {
10422 hidecallerid = ast_true(v->value);
10423 } else if (!strcasecmp(v->name, "pulsedial")) {
10424 pulse = ast_true(v->value);
10425 } else if (!strcasecmp(v->name, "callreturn")) {
10426 callreturn = ast_true(v->value);
10427 } else if (!strcasecmp(v->name, "callwaiting")) {
10428 callwaiting = ast_true(v->value);
10429 } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
10430 callwaitingcallerid = ast_true(v->value);
10431 } else if (!strcasecmp(v->name, "context")) {
10432 ast_copy_string(context, v->value, sizeof(context));
10433 } else if (!strcasecmp(v->name, "language")) {
10434 ast_copy_string(language, v->value, sizeof(language));
10435 } else if (!strcasecmp(v->name, "progzone")) {
10436 ast_copy_string(progzone, v->value, sizeof(progzone));
10437 } else if (!strcasecmp(v->name, "musiconhold")) {
10438 ast_copy_string(musicclass, v->value, sizeof(musicclass));
10439 } else if (!strcasecmp(v->name, "stripmsd")) {
10440 stripmsd = atoi(v->value);
10441 } else if (!strcasecmp(v->name, "jitterbuffers")) {
10442 numbufs = atoi(v->value);
10443 } else if (!strcasecmp(v->name, "group")) {
10444 cur_group = ast_get_group(v->value);
10445 } else if (!strcasecmp(v->name, "callgroup")) {
10446 cur_callergroup = ast_get_group(v->value);
10447 } else if (!strcasecmp(v->name, "pickupgroup")) {
10448 cur_pickupgroup = ast_get_group(v->value);
10449 } else if (!strcasecmp(v->name, "immediate")) {
10450 immediate = ast_true(v->value);
10451 } else if (!strcasecmp(v->name, "transfertobusy")) {
10452 transfertobusy = ast_true(v->value);
10453 } else if (!strcasecmp(v->name, "rxgain")) {
10454 if (sscanf(v->value, "%f", &rxgain) != 1) {
10455 ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value);
10456 }
10457 } else if (!strcasecmp(v->name, "txgain")) {
10458 if (sscanf(v->value, "%f", &txgain) != 1) {
10459 ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value);
10460 }
10461 } else if (!strcasecmp(v->name, "tonezone")) {
10462 if (sscanf(v->value, "%d", &tonezone) != 1) {
10463 ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
10464 }
10465 } else if (!strcasecmp(v->name, "callerid")) {
10466 if (!strcasecmp(v->value, "asreceived")) {
10467 cid_num[0] = '\0';
10468 cid_name[0] = '\0';
10469 } else {
10470 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
10471 }
10472 } else if (!strcasecmp(v->name, "useincomingcalleridonzaptransfer")) {
10473 zaptrcallerid = ast_true(v->value);
10474 } else if (!strcasecmp(v->name, "restrictcid")) {
10475 restrictcid = ast_true(v->value);
10476 } else if (!strcasecmp(v->name, "usecallingpres")) {
10477 use_callingpres = ast_true(v->value);
10478 } else if (!strcasecmp(v->name, "accountcode")) {
10479 ast_copy_string(accountcode, v->value, sizeof(accountcode));
10480 } else if (!strcasecmp(v->name, "amaflags")) {
10481 y = ast_cdr_amaflags2int(v->value);
10482 if (y < 0)
10483 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
10484 else
10485 amaflags = y;
10486 } else if(!reload){
10487 if (!strcasecmp(v->name, "signalling")) {
10488 if (!strcasecmp(v->value, "em")) {
10489 cur_signalling = SIG_EM;
10490 } else if (!strcasecmp(v->value, "em_e1")) {
10491 cur_signalling = SIG_EM_E1;
10492 } else if (!strcasecmp(v->value, "em_w")) {
10493 cur_signalling = SIG_EMWINK;
10494 cur_radio = 0;
10495 } else if (!strcasecmp(v->value, "fxs_ls")) {
10496 cur_signalling = SIG_FXSLS;
10497 cur_radio = 0;
10498 } else if (!strcasecmp(v->value, "fxs_gs")) {
10499 cur_signalling = SIG_FXSGS;
10500 cur_radio = 0;
10501 } else if (!strcasecmp(v->value, "fxs_ks")) {
10502 cur_signalling = SIG_FXSKS;
10503 cur_radio = 0;
10504 } else if (!strcasecmp(v->value, "fxo_ls")) {
10505 cur_signalling = SIG_FXOLS;
10506 cur_radio = 0;
10507 } else if (!strcasecmp(v->value, "fxo_gs")) {
10508 cur_signalling = SIG_FXOGS;
10509 cur_radio = 0;
10510 } else if (!strcasecmp(v->value, "fxo_ks")) {
10511 cur_signalling = SIG_FXOKS;
10512 cur_radio = 0;
10513 } else if (!strcasecmp(v->value, "fxs_rx")) {
10514 cur_signalling = SIG_FXSKS;
10515 cur_radio = 1;
10516 } else if (!strcasecmp(v->value, "fxo_rx")) {
10517 cur_signalling = SIG_FXOLS;
10518 cur_radio = 1;
10519 } else if (!strcasecmp(v->value, "fxs_tx")) {
10520 cur_signalling = SIG_FXSLS;
10521 cur_radio = 1;
10522 } else if (!strcasecmp(v->value, "fxo_tx")) {
10523 cur_signalling = SIG_FXOGS;
10524 cur_radio = 1;
10525 } else if (!strcasecmp(v->value, "em_rx")) {
10526 cur_signalling = SIG_EM;
10527 cur_radio = 1;
10528 } else if (!strcasecmp(v->value, "em_tx")) {
10529 cur_signalling = SIG_EM;
10530 cur_radio = 1;
10531 } else if (!strcasecmp(v->value, "em_rxtx")) {
10532 cur_signalling = SIG_EM;
10533 cur_radio = 2;
10534 } else if (!strcasecmp(v->value, "em_txrx")) {
10535 cur_signalling = SIG_EM;
10536 cur_radio = 2;
10537 } else if (!strcasecmp(v->value, "sf")) {
10538 cur_signalling = SIG_SF;
10539 cur_radio = 0;
10540 } else if (!strcasecmp(v->value, "sf_w")) {
10541 cur_signalling = SIG_SFWINK;
10542 cur_radio = 0;
10543 } else if (!strcasecmp(v->value, "sf_featd")) {
10544 cur_signalling = SIG_FEATD;
10545 cur_radio = 0;
10546 } else if (!strcasecmp(v->value, "sf_featdmf")) {
10547 cur_signalling = SIG_FEATDMF;
10548 cur_radio = 0;
10549 } else if (!strcasecmp(v->value, "sf_featb")) {
10550 cur_signalling = SIG_SF_FEATB;
10551 cur_radio = 0;
10552 } else if (!strcasecmp(v->value, "sf")) {
10553 cur_signalling = SIG_SF;
10554 cur_radio = 0;
10555 } else if (!strcasecmp(v->value, "sf_rx")) {
10556 cur_signalling = SIG_SF;
10557 cur_radio = 1;
10558 } else if (!strcasecmp(v->value, "sf_tx")) {
10559 cur_signalling = SIG_SF;
10560 cur_radio = 1;
10561 } else if (!strcasecmp(v->value, "sf_rxtx")) {
10562 cur_signalling = SIG_SF;
10563 cur_radio = 2;
10564 } else if (!strcasecmp(v->value, "sf_txrx")) {
10565 cur_signalling = SIG_SF;
10566 cur_radio = 2;
10567 } else if (!strcasecmp(v->value, "featd")) {
10568 cur_signalling = SIG_FEATD;
10569 cur_radio = 0;
10570 } else if (!strcasecmp(v->value, "featdmf")) {
10571 cur_signalling = SIG_FEATDMF;
10572 cur_radio = 0;
10573 } else if (!strcasecmp(v->value, "featdmf_ta")) {
10574 cur_signalling = SIG_FEATDMF_TA;
10575 cur_radio = 0;
10576 } else if (!strcasecmp(v->value, "e911")) {
10577 cur_signalling = SIG_E911;
10578 cur_radio = 0;
10579 } else if (!strcasecmp(v->value, "featb")) {
10580 cur_signalling = SIG_FEATB;
10581 cur_radio = 0;
10582 #ifdef ZAPATA_PRI
10583 } else if (!strcasecmp(v->value, "pri_net")) {
10584 cur_radio = 0;
10585 cur_signalling = SIG_PRI;
10586 pritype = PRI_NETWORK;
10587 } else if (!strcasecmp(v->value, "pri_cpe")) {
10588 cur_signalling = SIG_PRI;
10589 cur_radio = 0;
10590 pritype = PRI_CPE;
10591 } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
10592 cur_signalling = SIG_GR303FXOKS;
10593 cur_radio = 0;
10594 pritype = PRI_NETWORK;
10595 } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
10596 cur_signalling = SIG_GR303FXSKS;
10597 cur_radio = 0;
10598 pritype = PRI_CPE;
10599 #endif
10600 #ifdef ZAPATA_R2
10601 } else if (!strcasecmp(v->value, "r2")) {
10602 cur_signalling = SIG_R2;
10603 cur_radio = 0;
10604 #endif
10605 } else {
10606 ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
10607 }
10608 #ifdef ZAPATA_R2
10609 } else if (!strcasecmp(v->name, "r2country")) {
10610 r2prot = str2r2prot(v->value);
10611 if (r2prot < 0) {
10612 ast_log(LOG_WARNING, "Unknown R2 Country '%s' at line %d.\n", v->value, v->lineno);
10613 }
10614 #endif
10615 #ifdef ZAPATA_PRI
10616 } else if (!strcasecmp(v->name, "pridialplan")) {
10617 if (!strcasecmp(v->value, "national")) {
10618 dialplan = PRI_NATIONAL_ISDN + 1;
10619 } else if (!strcasecmp(v->value, "unknown")) {
10620 dialplan = PRI_UNKNOWN + 1;
10621 } else if (!strcasecmp(v->value, "private")) {
10622 dialplan = PRI_PRIVATE + 1;
10623 } else if (!strcasecmp(v->value, "international")) {
10624 dialplan = PRI_INTERNATIONAL_ISDN + 1;
10625 } else if (!strcasecmp(v->value, "local")) {
10626 dialplan = PRI_LOCAL_ISDN + 1;
10627 } else if (!strcasecmp(v->value, "dynamic")) {
10628 dialplan = -1;
10629 } else {
10630 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10631 }
10632 } else if (!strcasecmp(v->name, "prilocaldialplan")) {
10633 if (!strcasecmp(v->value, "national")) {
10634 localdialplan = PRI_NATIONAL_ISDN + 1;
10635 } else if (!strcasecmp(v->value, "unknown")) {
10636 localdialplan = PRI_UNKNOWN + 1;
10637 } else if (!strcasecmp(v->value, "private")) {
10638 localdialplan = PRI_PRIVATE + 1;
10639 } else if (!strcasecmp(v->value, "international")) {
10640 localdialplan = PRI_INTERNATIONAL_ISDN + 1;
10641 } else if (!strcasecmp(v->value, "local")) {
10642 localdialplan = PRI_LOCAL_ISDN + 1;
10643 } else if (!strcasecmp(v->value, "dynamic")) {
10644 localdialplan = -1;
10645 } else {
10646 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10647 }
10648 } else if (!strcasecmp(v->name, "switchtype")) {
10649 if (!strcasecmp(v->value, "national"))
10650 switchtype = PRI_SWITCH_NI2;
10651 else if (!strcasecmp(v->value, "ni1"))
10652 switchtype = PRI_SWITCH_NI1;
10653 else if (!strcasecmp(v->value, "dms100"))
10654 switchtype = PRI_SWITCH_DMS100;
10655 else if (!strcasecmp(v->value, "4ess"))
10656 switchtype = PRI_SWITCH_ATT4ESS;
10657 else if (!strcasecmp(v->value, "5ess"))
10658 switchtype = PRI_SWITCH_LUCENT5E;
10659 else if (!strcasecmp(v->value, "euroisdn"))
10660 switchtype = PRI_SWITCH_EUROISDN_E1;
10661 else if (!strcasecmp(v->value, "qsig"))
10662 switchtype = PRI_SWITCH_QSIG;
10663 else {
10664 ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value);
10665 ast_config_destroy(cfg);
10666 ast_mutex_unlock(&iflock);
10667 return -1;
10668 }
10669 } else if (!strcasecmp(v->name, "nsf")) {
10670 if (!strcasecmp(v->value, "sdn"))
10671 nsf = PRI_NSF_SDN;
10672 else if (!strcasecmp(v->value, "megacom"))
10673 nsf = PRI_NSF_MEGACOM;
10674 else if (!strcasecmp(v->value, "accunet"))
10675 nsf = PRI_NSF_ACCUNET;
10676 else if (!strcasecmp(v->value, "none"))
10677 nsf = PRI_NSF_NONE;
10678 else {
10679 ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value);
10680 nsf = PRI_NSF_NONE;
10681 }
10682 } else if (!strcasecmp(v->name, "priindication")) {
10683 if (!strcasecmp(v->value, "outofband"))
10684 priindication_oob = 1;
10685 else if (!strcasecmp(v->value, "inband"))
10686 priindication_oob = 0;
10687 else
10688 ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n",
10689 v->value, v->lineno);
10690 } else if (!strcasecmp(v->name, "priexclusive")) {
10691 cur_priexclusive = ast_true(v->value);
10692 } else if (!strcasecmp(v->name, "internationalprefix")) {
10693 ast_copy_string(internationalprefix, v->value, sizeof(internationalprefix));
10694 } else if (!strcasecmp(v->name, "nationalprefix")) {
10695 ast_copy_string(nationalprefix, v->value, sizeof(nationalprefix));
10696 } else if (!strcasecmp(v->name, "localprefix")) {
10697 ast_copy_string(localprefix, v->value, sizeof(localprefix));
10698 } else if (!strcasecmp(v->name, "privateprefix")) {
10699 ast_copy_string(privateprefix, v->value, sizeof(privateprefix));
10700 } else if (!strcasecmp(v->name, "unknownprefix")) {
10701 ast_copy_string(unknownprefix, v->value, sizeof(unknownprefix));
10702 } else if (!strcasecmp(v->name, "resetinterval")) {
10703 if (!strcasecmp(v->value, "never"))
10704 resetinterval = -1;
10705 else if( atoi(v->value) >= 60 )
10706 resetinterval = atoi(v->value);
10707 else
10708 ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d\n",
10709 v->value, v->lineno);
10710 } else if (!strcasecmp(v->name, "minunused")) {
10711 minunused = atoi(v->value);
10712 } else if (!strcasecmp(v->name, "idleext")) {
10713 ast_copy_string(idleext, v->value, sizeof(idleext));
10714 } else if (!strcasecmp(v->name, "idledial")) {
10715 ast_copy_string(idledial, v->value, sizeof(idledial));
10716 } else if (!strcasecmp(v->name, "overlapdial")) {
10717 overlapdial = ast_true(v->value);
10718 } else if (!strcasecmp(v->name, "pritimer")) {
10719 #ifdef PRI_GETSET_TIMERS
10720 char *timerc;
10721 int timer, timeridx;
10722 c = v->value;
10723 timerc = strsep(&c, ",");
10724 if (timerc) {
10725 timer = atoi(c);
10726 if (!timer)
10727 ast_log(LOG_WARNING, "'%s' is not a valid value for an ISDN timer\n", timerc);
10728 else {
10729 if ((timeridx = pri_timer2idx(timerc)) >= 0)
10730 pritimers[timeridx] = timer;
10731 else
10732 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer\n", timerc);
10733 }
10734 } else
10735 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer configuration string\n", v->value);
10736
10737 } else if (!strcasecmp(v->name, "facilityenable")) {
10738 facilityenable = ast_true(v->value);
10739 #endif
10740 #endif
10741 } else if (!strcasecmp(v->name, "cadence")) {
10742
10743 int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
10744 int i;
10745 struct zt_ring_cadence new_cadence;
10746 int cid_location = -1;
10747 int firstcadencepos = 0;
10748 char original_args[80];
10749 int cadence_is_ok = 1;
10750
10751 ast_copy_string(original_args, v->value, sizeof(original_args));
10752
10753 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]);
10754
10755
10756 if (element_count % 2 == 1) {
10757 ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args);
10758 cadence_is_ok = 0;
10759 }
10760
10761
10762 for (i=0;i<element_count;i++) {
10763 if (c[i] == 0) {
10764 ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s\n", original_args);
10765 cadence_is_ok = 0;
10766 break;
10767 } else if (c[i] < 0) {
10768 if (i % 2 == 1) {
10769
10770 if (cid_location == -1) {
10771 cid_location = i;
10772 c[i] *= -1;
10773 } else {
10774 ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args);
10775 cadence_is_ok = 0;
10776 break;
10777 }
10778 } else {
10779 if (firstcadencepos == 0) {
10780 firstcadencepos = i;
10781
10782 } else {
10783 ast_log(LOG_ERROR, "First cadence position specified twice: %s\n",original_args);
10784 cadence_is_ok = 0;
10785 break;
10786 }
10787 }
10788 }
10789 }
10790
10791
10792 for (i=0;i<16;i++) {
10793 new_cadence.ringcadence[i] = c[i];
10794 }
10795
10796 if (cadence_is_ok) {
10797
10798 if (element_count < 2) {
10799 ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args);
10800 } else {
10801 if (cid_location == -1) {
10802
10803 cid_location = 1;
10804 } else {
10805
10806 cid_location = (cid_location + 1) / 2;
10807 }
10808
10809 if (!user_has_defined_cadences++)
10810
10811 num_cadence = 0;
10812 if ((num_cadence+1) >= NUM_CADENCE_MAX)
10813 ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args);
10814 else {
10815 cadences[num_cadence] = new_cadence;
10816 cidrings[num_cadence++] = cid_location;
10817 if (option_verbose > 2)
10818 ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args);
10819 }
10820 }
10821 }
10822 } else if (!strcasecmp(v->name, "ringtimeout")) {
10823 ringt_base = (atoi(v->value) * 8) / READ_SIZE;
10824 } else if (!strcasecmp(v->name, "prewink")) {
10825 cur_prewink = atoi(v->value);
10826 } else if (!strcasecmp(v->name, "preflash")) {
10827 cur_preflash = atoi(v->value);
10828 } else if (!strcasecmp(v->name, "wink")) {
10829 cur_wink = atoi(v->value);
10830 } else if (!strcasecmp(v->name, "flash")) {
10831 cur_flash = atoi(v->value);
10832 } else if (!strcasecmp(v->name, "start")) {
10833 cur_start = atoi(v->value);
10834 } else if (!strcasecmp(v->name, "rxwink")) {
10835 cur_rxwink = atoi(v->value);
10836 } else if (!strcasecmp(v->name, "rxflash")) {
10837 cur_rxflash = atoi(v->value);
10838 } else if (!strcasecmp(v->name, "debounce")) {
10839 cur_debounce = atoi(v->value);
10840 } else if (!strcasecmp(v->name, "toneduration")) {
10841 int toneduration;
10842 int ctlfd;
10843 int res;
10844 struct zt_dialparams dps;
10845
10846 ctlfd = open("/dev/zap/ctl", O_RDWR);
10847 if (ctlfd == -1) {
10848 ast_log(LOG_ERROR, "Unable to open /dev/zap/ctl to set toneduration\n");
10849 return -1;
10850 }
10851
10852 toneduration = atoi(v->value);
10853 if (toneduration > -1) {
10854 dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration;
10855 res = ioctl(ctlfd, ZT_SET_DIALPARAMS, &dps);
10856 if (res < 0) {
10857 ast_log(LOG_ERROR, "Invalid tone duration: %d ms\n", toneduration);
10858 return -1;
10859 }
10860 }
10861 close(ctlfd);
10862 } else if (!strcasecmp(v->name, "polarityonanswerdelay")) {
10863 polarityonanswerdelay = atoi(v->value);
10864 } else if (!strcasecmp(v->name, "answeronpolarityswitch")) {
10865 answeronpolarityswitch = ast_true(v->value);
10866 } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) {
10867 hanguponpolarityswitch = ast_true(v->value);
10868 } else if (!strcasecmp(v->name, "sendcalleridafter")) {
10869 sendcalleridafter = atoi(v->value);
10870 } else if (!strcasecmp(v->name, "defaultcic")) {
10871 ast_copy_string(defaultcic, v->value, sizeof(defaultcic));
10872 } else if (!strcasecmp(v->name, "defaultozz")) {
10873 ast_copy_string(defaultozz, v->value, sizeof(defaultozz));
10874 }
10875 } else
10876 ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
10877 v = v->next;
10878 }
10879 if (!found_pseudo && reload == 0) {
10880
10881
10882
10883 cur_group = 0;
10884 cur_callergroup = 0;
10885 cur_pickupgroup = 0;
10886
10887 tmp = mkintf(CHAN_PSEUDO, cur_signalling, cur_radio, NULL, reload);
10888
10889 if (tmp) {
10890 if (option_verbose > 2)
10891 ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n");
10892 } else {
10893 ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
10894 }
10895 }
10896 ast_mutex_unlock(&iflock);
10897 ast_config_destroy(cfg);
10898 #ifdef ZAPATA_PRI
10899 if (!reload) {
10900 for (x=0;x<NUM_SPANS;x++) {
10901 if (pris[x].pvts[0]) {
10902 if (start_pri(pris + x)) {
10903 ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
10904 return -1;
10905 } else if (option_verbose > 1)
10906 ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1);
10907 }
10908 }
10909 }
10910 #endif
10911
10912 restart_monitor();
10913 return 0;
10914 }
10915
10916 int load_module(void)
10917 {
10918 int res;
10919
10920 #ifdef ZAPATA_PRI
10921 int y,i;
10922 memset(pris, 0, sizeof(pris));
10923 for (y=0;y<NUM_SPANS;y++) {
10924 ast_mutex_init(&pris[y].lock);
10925 pris[y].offset = -1;
10926 pris[y].master = AST_PTHREADT_NULL;
10927 for (i=0;i<NUM_DCHANS;i++)
10928 pris[y].fds[i] = -1;
10929 }
10930 pri_set_error(zt_pri_error);
10931 pri_set_message(zt_pri_message);
10932 #endif
10933 res = setup_zap(0);
10934
10935 if(res) {
10936 return -1;
10937 }
10938 if (ast_channel_register(&zap_tech)) {
10939 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
10940 __unload_module();
10941 return -1;
10942 }
10943 #ifdef ZAPATA_PRI
10944 ast_cli_register_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(zap_pri_cli[0]));
10945 #endif
10946 #ifdef ZAPATA_R2
10947 ast_cli_register_multiple(zap_r2_cli, sizeof(zap_r2_cli) / sizeof(zap_r2_cli[0]));
10948 #endif
10949 ast_cli_register_multiple(zap_cli, sizeof(zap_cli) / sizeof(zap_cli[0]));
10950
10951 memset(round_robin, 0, sizeof(round_robin));
10952 ast_manager_register( "ZapTransfer", 0, action_transfer, "Transfer Zap Channel" );
10953 ast_manager_register( "ZapHangup", 0, action_transferhangup, "Hangup Zap Channel" );
10954 ast_manager_register( "ZapDialOffhook", 0, action_zapdialoffhook, "Dial over Zap channel while offhook" );
10955 ast_manager_register( "ZapDNDon", 0, action_zapdndon, "Toggle Zap channel Do Not Disturb status ON" );
10956 ast_manager_register( "ZapDNDoff", 0, action_zapdndoff, "Toggle Zap channel Do Not Disturb status OFF" );
10957 ast_manager_register("ZapShowChannels", 0, action_zapshowchannels, "Show status zapata channels");
10958
10959 return res;
10960 }
10961
10962 static int zt_sendtext(struct ast_channel *c, const char *text)
10963 {
10964 #define END_SILENCE_LEN 400
10965 #define HEADER_MS 50
10966 #define TRAILER_MS 5
10967 #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
10968 #define ASCII_BYTES_PER_CHAR 80
10969
10970 unsigned char *buf,*mybuf;
10971 struct zt_pvt *p = c->tech_pvt;
10972 struct pollfd fds[1];
10973 int size,res,fd,len,x;
10974 int bytes=0;
10975
10976 float cr = 1.0;
10977 float ci = 0.0;
10978 float scont = 0.0;
10979 int index;
10980
10981 index = zt_get_index(c, p, 0);
10982 if (index < 0) {
10983 ast_log(LOG_WARNING, "Huh? I don't exist?\n");
10984 return -1;
10985 }
10986 if (!text[0]) return(0);
10987 if ((!p->tdd) && (!p->mate)) return(0);
10988 if (p->mate)
10989 buf = malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
10990 else
10991 buf = malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
10992 if (!buf) {
10993 ast_log(LOG_ERROR, "MALLOC FAILED\n");
10994 return -1;
10995 }
10996 mybuf = buf;
10997 if (p->mate) {
10998 int codec = AST_LAW(p);
10999 for (x=0;x<HEADER_MS;x++) {
11000 PUT_CLID_MARKMS;
11001 }
11002
11003 for (x=0;text[x];x++) {
11004 PUT_CLID(text[x]);
11005 }
11006 for (x=0;x<TRAILER_MS;x++) {
11007 PUT_CLID_MARKMS;
11008 }
11009 len = bytes;
11010 buf = mybuf;
11011 }
11012 else {
11013 len = tdd_generate(p->tdd,buf,text);
11014 if (len < 1) {
11015 ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n",(int)strlen(text));
11016 free(mybuf);
11017 return -1;
11018 }
11019 }
11020 memset(buf + len,0x7f,END_SILENCE_LEN);
11021 len += END_SILENCE_LEN;
11022 fd = p->subs[index].zfd;
11023 while(len) {
11024 if (ast_check_hangup(c)) {
11025 free(mybuf);
11026 return -1;
11027 }
11028 size = len;
11029 if (size > READ_SIZE)
11030 size = READ_SIZE;
11031 fds[0].fd = fd;
11032 fds[0].events = POLLOUT | POLLPRI;
11033 fds[0].revents = 0;
11034 res = poll(fds, 1, -1);
11035 if (!res) {
11036 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
11037 continue;
11038 }
11039
11040 if (fds[0].revents & POLLPRI) return -1;
11041 if (!(fds[0].revents & POLLOUT)) {
11042 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
11043 continue;
11044 }
11045 res = write(fd, buf, size);
11046 if (res != size) {
11047 if (res == -1) {
11048 free(mybuf);
11049 return -1;
11050 }
11051 if (option_debug)
11052 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
11053 break;
11054 }
11055 len -= size;
11056 buf += size;
11057 }
11058 free(mybuf);
11059 return(0);
11060 }
11061
11062
11063 int reload(void)
11064 {
11065 int res = 0;
11066
11067 res = setup_zap(1);
11068 if (res) {
11069 ast_log(LOG_WARNING, "Reload of chan_zap.so is unsuccessful!\n");
11070 return -1;
11071 }
11072 return 0;
11073 }
11074
11075 int usecount()
11076 {
11077 return usecnt;
11078 }
11079
11080 char *description()
11081 {
11082 return (char *) desc;
11083 }
11084
11085 char *key()
11086 {
11087 return ASTERISK_GPL_KEY;
11088 }
11089