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 <linux/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: 41069 $")
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
00764
00765
00766
00767 static int cidrings[NUM_CADENCE_MAX] = {
00768 2,
00769 4,
00770 3,
00771 2,
00772 };
00773
00774 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
00775 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
00776
00777 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
00778 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
00779
00780 static int zt_get_index(struct ast_channel *ast, struct zt_pvt *p, int nullok)
00781 {
00782 int res;
00783 if (p->subs[0].owner == ast)
00784 res = 0;
00785 else if (p->subs[1].owner == ast)
00786 res = 1;
00787 else if (p->subs[2].owner == ast)
00788 res = 2;
00789 else {
00790 res = -1;
00791 if (!nullok)
00792 ast_log(LOG_WARNING, "Unable to get index, and nullok is not asserted\n");
00793 }
00794 return res;
00795 }
00796
00797 #ifdef ZAPATA_PRI
00798 static void wakeup_sub(struct zt_pvt *p, int a, struct zt_pri *pri)
00799 #else
00800 static void wakeup_sub(struct zt_pvt *p, int a, void *pri)
00801 #endif
00802 {
00803 struct ast_frame null = { AST_FRAME_NULL, };
00804 #ifdef ZAPATA_PRI
00805 if (pri)
00806 ast_mutex_unlock(&pri->lock);
00807 #endif
00808 for (;;) {
00809 if (p->subs[a].owner) {
00810 if (ast_mutex_trylock(&p->subs[a].owner->lock)) {
00811 ast_mutex_unlock(&p->lock);
00812 usleep(1);
00813 ast_mutex_lock(&p->lock);
00814 } else {
00815 ast_queue_frame(p->subs[a].owner, &null);
00816 ast_mutex_unlock(&p->subs[a].owner->lock);
00817 break;
00818 }
00819 } else
00820 break;
00821 }
00822 #ifdef ZAPATA_PRI
00823 if (pri)
00824 ast_mutex_lock(&pri->lock);
00825 #endif
00826 }
00827
00828 #ifdef ZAPATA_PRI
00829 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, struct zt_pri *pri)
00830 #else
00831 static void zap_queue_frame(struct zt_pvt *p, struct ast_frame *f, void *pri)
00832 #endif
00833 {
00834
00835 #ifdef ZAPATA_PRI
00836 if (pri)
00837 ast_mutex_unlock(&pri->lock);
00838 #endif
00839 for (;;) {
00840 if (p->owner) {
00841 if (ast_mutex_trylock(&p->owner->lock)) {
00842 ast_mutex_unlock(&p->lock);
00843 usleep(1);
00844 ast_mutex_lock(&p->lock);
00845 } else {
00846 ast_queue_frame(p->owner, f);
00847 ast_mutex_unlock(&p->owner->lock);
00848 break;
00849 }
00850 } else
00851 break;
00852 }
00853 #ifdef ZAPATA_PRI
00854 if (pri)
00855 ast_mutex_lock(&pri->lock);
00856 #endif
00857 }
00858
00859 static int restore_gains(struct zt_pvt *p);
00860
00861 static void swap_subs(struct zt_pvt *p, int a, int b)
00862 {
00863 int tchan;
00864 int tinthreeway;
00865 struct ast_channel *towner;
00866
00867 ast_log(LOG_DEBUG, "Swapping %d and %d\n", a, b);
00868
00869 tchan = p->subs[a].chan;
00870 towner = p->subs[a].owner;
00871 tinthreeway = p->subs[a].inthreeway;
00872
00873 p->subs[a].chan = p->subs[b].chan;
00874 p->subs[a].owner = p->subs[b].owner;
00875 p->subs[a].inthreeway = p->subs[b].inthreeway;
00876
00877 p->subs[b].chan = tchan;
00878 p->subs[b].owner = towner;
00879 p->subs[b].inthreeway = tinthreeway;
00880
00881 if (p->subs[a].owner)
00882 p->subs[a].owner->fds[0] = p->subs[a].zfd;
00883 if (p->subs[b].owner)
00884 p->subs[b].owner->fds[0] = p->subs[b].zfd;
00885 wakeup_sub(p, a, NULL);
00886 wakeup_sub(p, b, NULL);
00887 }
00888
00889 static int zt_open(char *fn)
00890 {
00891 int fd;
00892 int isnum;
00893 int chan = 0;
00894 int bs;
00895 int x;
00896 isnum = 1;
00897 for (x=0;x<strlen(fn);x++) {
00898 if (!isdigit(fn[x])) {
00899 isnum = 0;
00900 break;
00901 }
00902 }
00903 if (isnum) {
00904 chan = atoi(fn);
00905 if (chan < 1) {
00906 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
00907 return -1;
00908 }
00909 fn = "/dev/zap/channel";
00910 }
00911 fd = open(fn, O_RDWR | O_NONBLOCK);
00912 if (fd < 0) {
00913 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
00914 return -1;
00915 }
00916 if (chan) {
00917 if (ioctl(fd, ZT_SPECIFY, &chan)) {
00918 x = errno;
00919 close(fd);
00920 errno = x;
00921 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
00922 return -1;
00923 }
00924 }
00925 bs = READ_SIZE;
00926 if (ioctl(fd, ZT_SET_BLOCKSIZE, &bs) == -1) return -1;
00927 return fd;
00928 }
00929
00930 static void zt_close(int fd)
00931 {
00932 if(fd > 0)
00933 close(fd);
00934 }
00935
00936 int zt_setlinear(int zfd, int linear)
00937 {
00938 int res;
00939 res = ioctl(zfd, ZT_SETLINEAR, &linear);
00940 if (res)
00941 return res;
00942 return 0;
00943 }
00944
00945
00946 int zt_setlaw(int zfd, int law)
00947 {
00948 int res;
00949 res = ioctl(zfd, ZT_SETLAW, &law);
00950 if (res)
00951 return res;
00952 return 0;
00953 }
00954
00955 static int alloc_sub(struct zt_pvt *p, int x)
00956 {
00957 ZT_BUFFERINFO bi;
00958 int res;
00959 if (p->subs[x].zfd < 0) {
00960 p->subs[x].zfd = zt_open("/dev/zap/pseudo");
00961 if (p->subs[x].zfd > -1) {
00962 res = ioctl(p->subs[x].zfd, ZT_GET_BUFINFO, &bi);
00963 if (!res) {
00964 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
00965 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
00966 bi.numbufs = numbufs;
00967 res = ioctl(p->subs[x].zfd, ZT_SET_BUFINFO, &bi);
00968 if (res < 0) {
00969 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d\n", x);
00970 }
00971 } else
00972 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d\n", x);
00973 if (ioctl(p->subs[x].zfd, ZT_CHANNO, &p->subs[x].chan) == 1) {
00974 ast_log(LOG_WARNING,"Unable to get channel number for pseudo channel on FD %d\n",p->subs[x].zfd);
00975 zt_close(p->subs[x].zfd);
00976 p->subs[x].zfd = -1;
00977 return -1;
00978 }
00979 if (option_debug)
00980 ast_log(LOG_DEBUG, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].zfd, p->subs[x].chan);
00981 return 0;
00982 } else
00983 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
00984 return -1;
00985 }
00986 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
00987 return -1;
00988 }
00989
00990 static int unalloc_sub(struct zt_pvt *p, int x)
00991 {
00992 if (!x) {
00993 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
00994 return -1;
00995 }
00996 ast_log(LOG_DEBUG, "Released sub %d of channel %d\n", x, p->channel);
00997 if (p->subs[x].zfd > -1) {
00998 zt_close(p->subs[x].zfd);
00999 }
01000 p->subs[x].zfd = -1;
01001 p->subs[x].linear = 0;
01002 p->subs[x].chan = 0;
01003 p->subs[x].owner = NULL;
01004 p->subs[x].inthreeway = 0;
01005 p->polarity = POLARITY_IDLE;
01006 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
01007 return 0;
01008 }
01009
01010 static int zt_digit(struct ast_channel *ast, char digit)
01011 {
01012 ZT_DIAL_OPERATION zo;
01013 struct zt_pvt *p;
01014 int res = 0;
01015 int index;
01016 p = ast->tech_pvt;
01017 ast_mutex_lock(&p->lock);
01018 index = zt_get_index(ast, p, 0);
01019 if ((index == SUB_REAL) && p->owner) {
01020 #ifdef ZAPATA_PRI
01021 if ((p->sig == SIG_PRI) && (ast->_state == AST_STATE_DIALING) && !p->proceeding) {
01022 if (p->setup_ack) {
01023 if (!pri_grab(p, p->pri)) {
01024 pri_information(p->pri->pri,p->call,digit);
01025 pri_rel(p->pri);
01026 } else
01027 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
01028 } else if (strlen(p->dialdest) < sizeof(p->dialdest) - 1) {
01029 ast_log(LOG_DEBUG, "Queueing digit '%c' since setup_ack not yet received\n", digit);
01030 res = strlen(p->dialdest);
01031 p->dialdest[res++] = digit;
01032 p->dialdest[res] = '\0';
01033 }
01034 } else {
01035 #else
01036 {
01037 #endif
01038 zo.op = ZT_DIAL_OP_APPEND;
01039 zo.dialstr[0] = 'T';
01040 zo.dialstr[1] = digit;
01041 zo.dialstr[2] = 0;
01042 if ((res = ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &zo)))
01043 ast_log(LOG_WARNING, "Couldn't dial digit %c\n", digit);
01044 else
01045 p->dialing = 1;
01046 }
01047 }
01048 ast_mutex_unlock(&p->lock);
01049 return res;
01050 }
01051
01052 static char *events[] = {
01053 "No event",
01054 "On hook",
01055 "Ring/Answered",
01056 "Wink/Flash",
01057 "Alarm",
01058 "No more alarm",
01059 "HDLC Abort",
01060 "HDLC Overrun",
01061 "HDLC Bad FCS",
01062 "Dial Complete",
01063 "Ringer On",
01064 "Ringer Off",
01065 "Hook Transition Complete",
01066 "Bits Changed",
01067 "Pulse Start",
01068 "Timer Expired",
01069 "Timer Ping",
01070 "Polarity Reversal",
01071 "Ring Begin",
01072 };
01073
01074 static struct {
01075 int alarm;
01076 char *name;
01077 } alarms[] = {
01078 { ZT_ALARM_RED, "Red Alarm" },
01079 { ZT_ALARM_YELLOW, "Yellow Alarm" },
01080 { ZT_ALARM_BLUE, "Blue Alarm" },
01081 { ZT_ALARM_RECOVER, "Recovering" },
01082 { ZT_ALARM_LOOPBACK, "Loopback" },
01083 { ZT_ALARM_NOTOPEN, "Not Open" },
01084 { ZT_ALARM_NONE, "None" },
01085 };
01086
01087 static char *alarm2str(int alarm)
01088 {
01089 int x;
01090 for (x=0;x<sizeof(alarms) / sizeof(alarms[0]); x++) {
01091 if (alarms[x].alarm & alarm)
01092 return alarms[x].name;
01093 }
01094 return alarm ? "Unknown Alarm" : "No Alarm";
01095 }
01096
01097 static char *event2str(int event)
01098 {
01099 static char buf[256];
01100 if ((event < (sizeof(events) / sizeof(events[0]))) && (event > -1))
01101 return events[event];
01102 sprintf(buf, "Event %d", event);
01103 return buf;
01104 }
01105
01106 #ifdef ZAPATA_PRI
01107 static char *dialplan2str(int dialplan)
01108 {
01109 if (dialplan == -1) {
01110 return("Dynamically set dialplan in ISDN");
01111 }
01112 return(pri_plan2str(dialplan));
01113 }
01114 #endif
01115
01116 #ifdef ZAPATA_R2
01117 static int str2r2prot(char *swtype)
01118 {
01119 if (!strcasecmp(swtype, "ar"))
01120 return MFCR2_PROT_ARGENTINA;
01121
01122 if (!strcasecmp(swtype, "cn"))
01123 return MFCR2_PROT_CHINA;
01124
01125 if (!strcasecmp(swtype, "kr"))
01126 return MFCR2_PROT_KOREA;
01127
01128 return -1;
01129 }
01130 #endif
01131
01132 static char *zap_sig2str(int sig)
01133 {
01134 static char buf[256];
01135 switch(sig) {
01136 case SIG_EM:
01137 return "E & M Immediate";
01138 case SIG_EMWINK:
01139 return "E & M Wink";
01140 case SIG_EM_E1:
01141 return "E & M E1";
01142 case SIG_FEATD:
01143 return "Feature Group D (DTMF)";
01144 case SIG_FEATDMF:
01145 return "Feature Group D (MF)";
01146 case SIG_FEATDMF_TA:
01147 return "Feature Groud D (MF) Tandem Access";
01148 case SIG_FEATB:
01149 return "Feature Group B (MF)";
01150 case SIG_E911:
01151 return "E911 (MF)";
01152 case SIG_FXSLS:
01153 return "FXS Loopstart";
01154 case SIG_FXSGS:
01155 return "FXS Groundstart";
01156 case SIG_FXSKS:
01157 return "FXS Kewlstart";
01158 case SIG_FXOLS:
01159 return "FXO Loopstart";
01160 case SIG_FXOGS:
01161 return "FXO Groundstart";
01162 case SIG_FXOKS:
01163 return "FXO Kewlstart";
01164 case SIG_PRI:
01165 return "PRI Signalling";
01166 case SIG_R2:
01167 return "R2 Signalling";
01168 case SIG_SF:
01169 return "SF (Tone) Signalling Immediate";
01170 case SIG_SFWINK:
01171 return "SF (Tone) Signalling Wink";
01172 case SIG_SF_FEATD:
01173 return "SF (Tone) Signalling with Feature Group D (DTMF)";
01174 case SIG_SF_FEATDMF:
01175 return "SF (Tone) Signalling with Feature Group D (MF)";
01176 case SIG_SF_FEATB:
01177 return "SF (Tone) Signalling with Feature Group B (MF)";
01178 case SIG_GR303FXOKS:
01179 return "GR-303 Signalling with FXOKS";
01180 case SIG_GR303FXSKS:
01181 return "GR-303 Signalling with FXSKS";
01182 case 0:
01183 return "Pseudo Signalling";
01184 default:
01185 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
01186 return buf;
01187 }
01188 }
01189
01190 #define sig2str zap_sig2str
01191
01192 static int conf_add(struct zt_pvt *p, struct zt_subchannel *c, int index, int slavechannel)
01193 {
01194
01195
01196 ZT_CONFINFO zi;
01197
01198 memset(&zi, 0, sizeof(zi));
01199 zi.chan = 0;
01200
01201 if (slavechannel > 0) {
01202
01203 zi.confmode = ZT_CONF_DIGITALMON;
01204 zi.confno = slavechannel;
01205 } else {
01206 if (!index) {
01207
01208 zi.confmode = ZT_CONF_REALANDPSEUDO | ZT_CONF_TALKER | ZT_CONF_LISTENER |
01209 ZT_CONF_PSEUDO_TALKER | ZT_CONF_PSEUDO_LISTENER;
01210 } else
01211 zi.confmode = ZT_CONF_CONF | ZT_CONF_TALKER | ZT_CONF_LISTENER;
01212 zi.confno = p->confno;
01213 }
01214 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
01215 return 0;
01216 if (c->zfd < 0)
01217 return 0;
01218 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01219 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d\n", c->zfd, zi.confmode, zi.confno);
01220 return -1;
01221 }
01222 if (slavechannel < 1) {
01223 p->confno = zi.confno;
01224 }
01225 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01226 ast_log(LOG_DEBUG, "Added %d to conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01227 return 0;
01228 }
01229
01230 static int isourconf(struct zt_pvt *p, struct zt_subchannel *c)
01231 {
01232
01233 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == ZT_CONF_DIGITALMON))
01234 return 1;
01235
01236 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & ZT_CONF_TALKER))
01237 return 1;
01238 return 0;
01239 }
01240
01241 static int conf_del(struct zt_pvt *p, struct zt_subchannel *c, int index)
01242 {
01243 ZT_CONFINFO zi;
01244 if (
01245 (c->zfd < 0) ||
01246
01247 !isourconf(p, c)
01248
01249 ) return 0;
01250 memset(&zi, 0, sizeof(zi));
01251 zi.chan = 0;
01252 zi.confno = 0;
01253 zi.confmode = 0;
01254 if (ioctl(c->zfd, ZT_SETCONF, &zi)) {
01255 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01256 return -1;
01257 }
01258 ast_log(LOG_DEBUG, "Removed %d from conference %d/%d\n", c->zfd, c->curconf.confmode, c->curconf.confno);
01259 memcpy(&c->curconf, &zi, sizeof(c->curconf));
01260 return 0;
01261 }
01262
01263 static int isslavenative(struct zt_pvt *p, struct zt_pvt **out)
01264 {
01265 int x;
01266 int useslavenative;
01267 struct zt_pvt *slave = NULL;
01268
01269 useslavenative = 1;
01270
01271 for (x=0;x<3;x++) {
01272
01273
01274 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway)
01275 useslavenative = 0;
01276 }
01277
01278
01279 if (useslavenative) {
01280 for (x=0;x<MAX_SLAVES;x++) {
01281 if (p->slaves[x]) {
01282 if (slave) {
01283
01284
01285 slave = NULL;
01286 useslavenative = 0;
01287 break;
01288 } else {
01289
01290 slave = p->slaves[x];
01291 }
01292 }
01293 }
01294 }
01295
01296 if (!slave)
01297 useslavenative = 0;
01298 else if (slave->law != p->law) {
01299 useslavenative = 0;
01300 slave = NULL;
01301 }
01302 if (out)
01303 *out = slave;
01304 return useslavenative;
01305 }
01306
01307 static int reset_conf(struct zt_pvt *p)
01308 {
01309 ZT_CONFINFO zi;
01310 memset(&zi, 0, sizeof(zi));
01311 p->confno = -1;
01312 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
01313 if (p->subs[SUB_REAL].zfd > -1) {
01314 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &zi))
01315 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d!\n", p->channel);
01316 }
01317 return 0;
01318 }
01319
01320 static int update_conf(struct zt_pvt *p)
01321 {
01322 int needconf = 0;
01323 int x;
01324 int useslavenative;
01325 struct zt_pvt *slave = NULL;
01326
01327 useslavenative = isslavenative(p, &slave);
01328
01329 for (x=0;x<3;x++) {
01330
01331 if ((p->subs[x].zfd > -1) && p->subs[x].inthreeway) {
01332 conf_add(p, &p->subs[x], x, 0);
01333 needconf++;
01334 } else {
01335 conf_del(p, &p->subs[x], x);
01336 }
01337 }
01338
01339
01340 for (x=0;x<MAX_SLAVES;x++) {
01341 if (p->slaves[x]) {
01342 if (useslavenative)
01343 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
01344 else {
01345 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
01346 needconf++;
01347 }
01348 }
01349 }
01350
01351 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
01352 if (useslavenative)
01353 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
01354 else {
01355 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
01356 needconf++;
01357 }
01358 }
01359
01360 if (p->master) {
01361 if (isslavenative(p->master, NULL)) {
01362 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
01363 } else {
01364 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
01365 }
01366 }
01367 if (!needconf) {
01368
01369
01370 p->confno = -1;
01371 }
01372 ast_log(LOG_DEBUG, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
01373 return 0;
01374 }
01375
01376 static void zt_enable_ec(struct zt_pvt *p)
01377 {
01378 int x;
01379 int res;
01380 if (!p)
01381 return;
01382 if (p->echocanon) {
01383 ast_log(LOG_DEBUG, "Echo cancellation already on\n");
01384 return;
01385 }
01386 if (p->digital) {
01387 ast_log(LOG_DEBUG, "Echo cancellation isn't required on digital connection\n");
01388 return;
01389 }
01390 if (p->echocancel) {
01391 if (p->sig == SIG_PRI) {
01392 x = 1;
01393 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x);
01394 if (res)
01395 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01396 }
01397 x = p->echocancel;
01398 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01399 if (res)
01400 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d\n", p->channel);
01401 else {
01402 p->echocanon = 1;
01403 ast_log(LOG_DEBUG, "Enabled echo cancellation on channel %d\n", p->channel);
01404 }
01405 } else
01406 ast_log(LOG_DEBUG, "No echo cancellation requested\n");
01407 }
01408
01409 static void zt_train_ec(struct zt_pvt *p)
01410 {
01411 int x;
01412 int res;
01413 if (p && p->echocancel && p->echotraining) {
01414 x = p->echotraining;
01415 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOTRAIN, &x);
01416 if (res)
01417 ast_log(LOG_WARNING, "Unable to request echo training on channel %d\n", p->channel);
01418 else {
01419 ast_log(LOG_DEBUG, "Engaged echo training on channel %d\n", p->channel);
01420 }
01421 } else
01422 ast_log(LOG_DEBUG, "No echo training requested\n");
01423 }
01424
01425 static void zt_disable_ec(struct zt_pvt *p)
01426 {
01427 int x;
01428 int res;
01429 if (p->echocancel) {
01430 x = 0;
01431 res = ioctl(p->subs[SUB_REAL].zfd, ZT_ECHOCANCEL, &x);
01432 if (res)
01433 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d\n", p->channel);
01434 else
01435 ast_log(LOG_DEBUG, "disabled echo cancellation on channel %d\n", p->channel);
01436 }
01437 p->echocanon = 0;
01438 }
01439
01440 static void fill_txgain(struct zt_gains *g, float gain, int law)
01441 {
01442 int j;
01443 int k;
01444 float linear_gain = pow(10.0, gain / 20.0);
01445
01446 switch (law) {
01447 case ZT_LAW_ALAW:
01448 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01449 if (gain) {
01450 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01451 if (k > 32767) k = 32767;
01452 if (k < -32767) k = -32767;
01453 g->txgain[j] = AST_LIN2A(k);
01454 } else {
01455 g->txgain[j] = j;
01456 }
01457 }
01458 break;
01459 case ZT_LAW_MULAW:
01460 for (j = 0; j < (sizeof(g->txgain) / sizeof(g->txgain[0])); j++) {
01461 if (gain) {
01462 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01463 if (k > 32767) k = 32767;
01464 if (k < -32767) k = -32767;
01465 g->txgain[j] = AST_LIN2MU(k);
01466 } else {
01467 g->txgain[j] = j;
01468 }
01469 }
01470 break;
01471 }
01472 }
01473
01474 static void fill_rxgain(struct zt_gains *g, float gain, int law)
01475 {
01476 int j;
01477 int k;
01478 float linear_gain = pow(10.0, gain / 20.0);
01479
01480 switch (law) {
01481 case ZT_LAW_ALAW:
01482 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01483 if (gain) {
01484 k = (int) (((float) AST_ALAW(j)) * linear_gain);
01485 if (k > 32767) k = 32767;
01486 if (k < -32767) k = -32767;
01487 g->rxgain[j] = AST_LIN2A(k);
01488 } else {
01489 g->rxgain[j] = j;
01490 }
01491 }
01492 break;
01493 case ZT_LAW_MULAW:
01494 for (j = 0; j < (sizeof(g->rxgain) / sizeof(g->rxgain[0])); j++) {
01495 if (gain) {
01496 k = (int) (((float) AST_MULAW(j)) * linear_gain);
01497 if (k > 32767) k = 32767;
01498 if (k < -32767) k = -32767;
01499 g->rxgain[j] = AST_LIN2MU(k);
01500 } else {
01501 g->rxgain[j] = j;
01502 }
01503 }
01504 break;
01505 }
01506 }
01507
01508 int set_actual_txgain(int fd, int chan, float gain, int law)
01509 {
01510 struct zt_gains g;
01511 int res;
01512
01513 memset(&g, 0, sizeof(g));
01514 g.chan = chan;
01515 res = ioctl(fd, ZT_GETGAINS, &g);
01516 if (res) {
01517 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01518 return res;
01519 }
01520
01521 fill_txgain(&g, gain, law);
01522
01523 return ioctl(fd, ZT_SETGAINS, &g);
01524 }
01525
01526 int set_actual_rxgain(int fd, int chan, float gain, int law)
01527 {
01528 struct zt_gains g;
01529 int res;
01530
01531 memset(&g, 0, sizeof(g));
01532 g.chan = chan;
01533 res = ioctl(fd, ZT_GETGAINS, &g);
01534 if (res) {
01535 ast_log(LOG_DEBUG, "Failed to read gains: %s\n", strerror(errno));
01536 return res;
01537 }
01538
01539 fill_rxgain(&g, gain, law);
01540
01541 return ioctl(fd, ZT_SETGAINS, &g);
01542 }
01543
01544 int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
01545 {
01546 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
01547 }
01548
01549 static int bump_gains(struct zt_pvt *p)
01550 {
01551 int res;
01552
01553
01554 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain + 5.0, p->txgain, p->law);
01555 if (res) {
01556 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
01557 return -1;
01558 }
01559
01560 return 0;
01561 }
01562
01563 static int restore_gains(struct zt_pvt *p)
01564 {
01565 int res;
01566
01567 res = set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01568 if (res) {
01569 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
01570 return -1;
01571 }
01572
01573 return 0;
01574 }
01575
01576 static inline int zt_set_hook(int fd, int hs)
01577 {
01578 int x, res;
01579 x = hs;
01580 res = ioctl(fd, ZT_HOOK, &x);
01581 if (res < 0)
01582 {
01583 if (errno == EINPROGRESS) return 0;
01584 ast_log(LOG_WARNING, "zt hook failed: %s\n", strerror(errno));
01585 }
01586 return res;
01587 }
01588
01589 static inline int zt_confmute(struct zt_pvt *p, int muted)
01590 {
01591 int x, y, res;
01592 x = muted;
01593 if (p->sig == SIG_PRI) {
01594 y = 1;
01595 res = ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &y);
01596 if (res)
01597 ast_log(LOG_WARNING, "Unable to set audio mode on '%d'\n", p->channel);
01598 }
01599 res = ioctl(p->subs[SUB_REAL].zfd, ZT_CONFMUTE, &x);
01600 if (res < 0)
01601 ast_log(LOG_WARNING, "zt confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
01602 return res;
01603 }
01604
01605 static int save_conference(struct zt_pvt *p)
01606 {
01607 struct zt_confinfo c;
01608 int res;
01609 if (p->saveconf.confmode) {
01610 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
01611 return -1;
01612 }
01613 p->saveconf.chan = 0;
01614 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GETCONF, &p->saveconf);
01615 if (res) {
01616 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
01617 p->saveconf.confmode = 0;
01618 return -1;
01619 }
01620 c.chan = 0;
01621 c.confno = 0;
01622 c.confmode = ZT_CONF_NORMAL;
01623 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &c);
01624 if (res) {
01625 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
01626 return -1;
01627 }
01628 if (option_debug)
01629 ast_log(LOG_DEBUG, "Disabled conferencing\n");
01630 return 0;
01631 }
01632
01633 static int restore_conference(struct zt_pvt *p)
01634 {
01635 int res;
01636 if (p->saveconf.confmode) {
01637 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETCONF, &p->saveconf);
01638 p->saveconf.confmode = 0;
01639 if (res) {
01640 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
01641 return -1;
01642 }
01643 }
01644 if (option_debug)
01645 ast_log(LOG_DEBUG, "Restored conferencing\n");
01646 return 0;
01647 }
01648
01649 static int send_callerid(struct zt_pvt *p);
01650
01651 int send_cwcidspill(struct zt_pvt *p)
01652 {
01653 p->callwaitcas = 0;
01654 p->cidcwexpire = 0;
01655 p->cidspill = malloc(MAX_CALLERID_SIZE);
01656 if (p->cidspill) {
01657 memset(p->cidspill, 0x7f, MAX_CALLERID_SIZE);
01658 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
01659
01660 p->cidlen += READ_SIZE * 4;
01661 p->cidpos = 0;
01662 send_callerid(p);
01663 if (option_verbose > 2)
01664 ast_verbose(VERBOSE_PREFIX_3 "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
01665 } else return -1;
01666 return 0;
01667 }
01668
01669 static int has_voicemail(struct zt_pvt *p)
01670 {
01671
01672 return ast_app_has_voicemail(p->mailbox, NULL);
01673 }
01674
01675 static int send_callerid(struct zt_pvt *p)
01676 {
01677
01678 int res;
01679
01680 if (p->subs[SUB_REAL].linear) {
01681 p->subs[SUB_REAL].linear = 0;
01682 zt_setlinear(p->subs[SUB_REAL].zfd, 0);
01683 }
01684 while(p->cidpos < p->cidlen) {
01685 res = write(p->subs[SUB_REAL].zfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
01686 if (res < 0) {
01687 if (errno == EAGAIN)
01688 return 0;
01689 else {
01690 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
01691 return -1;
01692 }
01693 }
01694 if (!res)
01695 return 0;
01696 p->cidpos += res;
01697 }
01698 free(p->cidspill);
01699 p->cidspill = NULL;
01700 if (p->callwaitcas) {
01701
01702 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
01703 } else
01704 restore_conference(p);
01705 return 0;
01706 }
01707
01708 static int zt_callwait(struct ast_channel *ast)
01709 {
01710 struct zt_pvt *p = ast->tech_pvt;
01711 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
01712 if (p->cidspill) {
01713 ast_log(LOG_WARNING, "Spill already exists?!?\n");
01714 free(p->cidspill);
01715 }
01716 p->cidspill = malloc(2400 + 680 + READ_SIZE * 4);
01717 if (p->cidspill) {
01718 save_conference(p);
01719
01720 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
01721 if (!p->callwaitrings && p->callwaitingcallerid) {
01722 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
01723 p->callwaitcas = 1;
01724 p->cidlen = 2400 + 680 + READ_SIZE * 4;
01725 } else {
01726 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
01727 p->callwaitcas = 0;
01728 p->cidlen = 2400 + READ_SIZE * 4;
01729 }
01730 p->cidpos = 0;
01731 send_callerid(p);
01732 } else {
01733 ast_log(LOG_WARNING, "Unable to create SAS/CAS spill\n");
01734 return -1;
01735 }
01736 return 0;
01737 }
01738
01739 static int zt_call(struct ast_channel *ast, char *rdest, int timeout)
01740 {
01741 struct zt_pvt *p = ast->tech_pvt;
01742 int x, res, index;
01743 char *c, *n, *l;
01744 #ifdef ZAPATA_PRI
01745 char *s=NULL;
01746 #endif
01747 char dest[256];
01748 ast_mutex_lock(&p->lock);
01749 ast_copy_string(dest, rdest, sizeof(dest));
01750 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
01751 if ((ast->_state == AST_STATE_BUSY)) {
01752 p->subs[SUB_REAL].needbusy = 1;
01753 ast_mutex_unlock(&p->lock);
01754 return 0;
01755 }
01756 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
01757 ast_log(LOG_WARNING, "zt_call called on %s, neither down nor reserved\n", ast->name);
01758 ast_mutex_unlock(&p->lock);
01759 return -1;
01760 }
01761 p->dialednone = 0;
01762 if (p->radio)
01763 {
01764
01765 ast_setstate(ast, AST_STATE_UP);
01766 ast_mutex_unlock(&p->lock);
01767 return 0;
01768 }
01769 x = ZT_FLUSH_READ | ZT_FLUSH_WRITE;
01770 res = ioctl(p->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
01771 if (res)
01772 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", p->channel);
01773 p->outgoing = 1;
01774
01775 set_actual_gain(p->subs[SUB_REAL].zfd, 0, p->rxgain, p->txgain, p->law);
01776
01777 switch(p->sig) {
01778 case SIG_FXOLS:
01779 case SIG_FXOGS:
01780 case SIG_FXOKS:
01781 if (p->owner == ast) {
01782
01783
01784
01785 p->dialing = 1;
01786 if (p->use_callerid) {
01787
01788 if (p->cidspill) {
01789 ast_log(LOG_WARNING, "cidspill already exists??\n");
01790 free(p->cidspill);
01791 }
01792 p->cidspill = malloc(MAX_CALLERID_SIZE);
01793 p->callwaitcas = 0;
01794 if (p->cidspill) {
01795 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
01796 p->cidpos = 0;
01797 send_callerid(p);
01798 } else
01799 ast_log(LOG_WARNING, "Unable to generate CallerID spill\n");
01800 }
01801
01802 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
01803 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, &cadences[p->distinctivering-1]))
01804 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s'\n", p->distinctivering, ast->name);
01805 p->cidrings = cidrings[p->distinctivering - 1];
01806 } else {
01807 if (ioctl(p->subs[SUB_REAL].zfd, ZT_SETCADENCE, NULL))
01808 ast_log(LOG_WARNING, "Unable to reset default ring on '%s'\n", ast->name);
01809 p->cidrings = p->sendcalleridafter;
01810 }
01811
01812
01813
01814 c = strchr(dest, '/');
01815 if (c)
01816 c++;
01817 if (c && (strlen(c) < p->stripmsd)) {
01818 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01819 c = NULL;
01820 }
01821 if (c) {
01822 p->dop.op = ZT_DIAL_OP_REPLACE;
01823 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
01824 ast_log(LOG_DEBUG, "FXO: setup deferred dialstring: %s\n", c);
01825 } else {
01826 p->dop.dialstr[0] = '\0';
01827 }
01828 x = ZT_RING;
01829 if (ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x) && (errno != EINPROGRESS)) {
01830 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
01831 ast_mutex_unlock(&p->lock);
01832 return -1;
01833 }
01834 p->dialing = 1;
01835 } else {
01836
01837 p->callwaitrings = 0;
01838 if (ast->cid.cid_num)
01839 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
01840 else
01841 p->callwait_num[0] = '\0';
01842 if (ast->cid.cid_name)
01843 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
01844 else
01845 p->callwait_name[0] = '\0';
01846
01847 if (zt_callwait(ast)) {
01848 ast_mutex_unlock(&p->lock);
01849 return -1;
01850 }
01851
01852 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].zfd, ZT_TONE_RINGTONE))
01853 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
01854
01855 }
01856 n = ast->cid.cid_name;
01857 l = ast->cid.cid_num;
01858 if (l)
01859 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
01860 else
01861 p->lastcid_num[0] = '\0';
01862 if (n)
01863 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
01864 else
01865 p->lastcid_name[0] = '\0';
01866 ast_setstate(ast, AST_STATE_RINGING);
01867 index = zt_get_index(ast, p, 0);
01868 if (index > -1) {
01869 p->subs[index].needringing = 1;
01870 }
01871 break;
01872 case SIG_FXSLS:
01873 case SIG_FXSGS:
01874 case SIG_FXSKS:
01875 case SIG_EMWINK:
01876 case SIG_EM:
01877 case SIG_EM_E1:
01878 case SIG_FEATD:
01879 case SIG_FEATDMF:
01880 case SIG_E911:
01881 case SIG_FEATB:
01882 case SIG_SFWINK:
01883 case SIG_SF:
01884 case SIG_SF_FEATD:
01885 case SIG_SF_FEATDMF:
01886 case SIG_FEATDMF_TA:
01887 case SIG_SF_FEATB:
01888 c = strchr(dest, '/');
01889 if (c)
01890 c++;
01891 else
01892 c = "";
01893 if (strlen(c) < p->stripmsd) {
01894 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
01895 ast_mutex_unlock(&p->lock);
01896 return -1;
01897 }
01898 #ifdef ZAPATA_PRI
01899
01900 if (!p->pri) {
01901 #endif
01902 x = ZT_START;
01903 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01904 if (res < 0) {
01905 if (errno != EINPROGRESS) {
01906 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
01907 ast_mutex_unlock(&p->lock);
01908 return -1;
01909 }
01910 }
01911 #ifdef ZAPATA_PRI
01912 }
01913 #endif
01914 ast_log(LOG_DEBUG, "Dialing '%s'\n", c);
01915 p->dop.op = ZT_DIAL_OP_REPLACE;
01916
01917 c += p->stripmsd;
01918
01919 switch (p->sig) {
01920 case SIG_FEATD:
01921 l = ast->cid.cid_num;
01922 if (l)
01923 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
01924 else
01925 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
01926 break;
01927 case SIG_FEATDMF:
01928 l = ast->cid.cid_num;
01929 if (l)
01930 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
01931 else
01932 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
01933 break;
01934 case SIG_FEATDMF_TA:
01935 {
01936 char *cic = NULL, *ozz = NULL;
01937
01938
01939 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
01940 if (!ozz)
01941 ozz = defaultozz;
01942 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
01943 if (!cic)
01944 cic = defaultcic;
01945 if (!ozz || !cic) {
01946 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
01947 ast_mutex_unlock(&p->lock);
01948 return -1;
01949 }
01950 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
01951 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
01952 p->whichwink = 0;
01953 }
01954 break;
01955 case SIG_E911:
01956 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
01957 break;
01958 case SIG_FEATB:
01959 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
01960 break;
01961 default:
01962 if (p->pulse)
01963 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
01964 else
01965 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
01966 break;
01967 }
01968
01969 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
01970 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
01971 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
01972 p->echorest[sizeof(p->echorest) - 1] = '\0';
01973 p->echobreak = 1;
01974 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
01975 } else
01976 p->echobreak = 0;
01977 if (!res) {
01978 if (ioctl(p->subs[SUB_REAL].zfd, ZT_DIAL, &p->dop)) {
01979 x = ZT_ONHOOK;
01980 ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
01981 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(errno));
01982 ast_mutex_unlock(&p->lock);
01983 return -1;
01984 }
01985 } else
01986 ast_log(LOG_DEBUG, "Deferring dialing...\n");
01987 p->dialing = 1;
01988 if (ast_strlen_zero(c))
01989 p->dialednone = 1;
01990 ast_setstate(ast, AST_STATE_DIALING);
01991 break;
01992 case 0:
01993
01994 ast_setstate(ast, AST_STATE_UP);
01995 break;
01996 case SIG_PRI:
01997
01998 p->dialdest[0] = '\0';
01999 break;
02000 default:
02001 ast_log(LOG_DEBUG, "not yet implemented\n");
02002 ast_mutex_unlock(&p->lock);
02003 return -1;
02004 }
02005 #ifdef ZAPATA_PRI
02006 if (p->pri) {
02007 struct pri_sr *sr;
02008 #ifdef SUPPORT_USERUSER
02009 char *useruser;
02010 #endif
02011 int pridialplan;
02012 int dp_strip;
02013 int prilocaldialplan;
02014 int ldp_strip;
02015 int exclusive;
02016
02017 c = strchr(dest, '/');
02018 if (c)
02019 c++;
02020 else
02021 c = dest;
02022 if (!p->hidecallerid) {
02023 l = ast->cid.cid_num;
02024 n = ast->cid.cid_name;
02025 } else {
02026 l = NULL;
02027 n = NULL;
02028 }
02029 if (strlen(c) < p->stripmsd) {
02030 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
02031 ast_mutex_unlock(&p->lock);
02032 return -1;
02033 }
02034 if (p->sig != SIG_FXSKS) {
02035 p->dop.op = ZT_DIAL_OP_REPLACE;
02036 s = strchr(c + p->stripmsd, 'w');
02037 if (s) {
02038 if (strlen(s) > 1)
02039 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
02040 else
02041 p->dop.dialstr[0] = '\0';
02042 *s = '\0';
02043 } else {
02044 p->dop.dialstr[0] = '\0';
02045 }
02046 }
02047 if (pri_grab(p, p->pri)) {
02048 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
02049 ast_mutex_unlock(&p->lock);
02050 return -1;
02051 }
02052 if (!(p->call = pri_new_call(p->pri->pri))) {
02053 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
02054 pri_rel(p->pri);
02055 ast_mutex_unlock(&p->lock);
02056 return -1;
02057 }
02058 if (!(sr = pri_sr_new())) {
02059 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
02060 pri_rel(p->pri);
02061 ast_mutex_unlock(&p->lock);
02062 }
02063 if (p->bearer || (p->sig == SIG_FXSKS)) {
02064 if (p->bearer) {
02065 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);
02066 p->bearer->call = p->call;
02067 } else
02068 ast_log(LOG_DEBUG, "I'm being setup with no bearer right now...\n");
02069 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
02070 }
02071 p->digital = IS_DIGITAL(ast->transfercapability);
02072
02073 if (p->priexclusive)
02074 exclusive = 1;
02075 else {
02076
02077 if (p->pri->nodetype == PRI_NETWORK)
02078 exclusive = 0;
02079 else
02080 exclusive = 1;
02081 }
02082
02083 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
02084 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
02085 (p->digital ? -1 :
02086 ((p->law == ZT_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
02087 if (p->pri->facilityenable)
02088 pri_facility_enable(p->pri->pri);
02089
02090 if (option_verbose > 2)
02091 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
02092 dp_strip = 0;
02093 pridialplan = p->pri->dialplan - 1;
02094 if (pridialplan == -2) {
02095 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02096 dp_strip = strlen(p->pri->internationalprefix);
02097 pridialplan = PRI_INTERNATIONAL_ISDN;
02098 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02099 dp_strip = strlen(p->pri->nationalprefix);
02100 pridialplan = PRI_NATIONAL_ISDN;
02101 } else {
02102 pridialplan = PRI_LOCAL_ISDN;
02103 }
02104 }
02105 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
02106
02107 ldp_strip = 0;
02108 prilocaldialplan = p->pri->localdialplan - 1;
02109 if ((l != NULL) && (prilocaldialplan == -2)) {
02110 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
02111 ldp_strip = strlen(p->pri->internationalprefix);
02112 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
02113 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
02114 ldp_strip = strlen(p->pri->nationalprefix);
02115 prilocaldialplan = PRI_NATIONAL_ISDN;
02116 } else {
02117 prilocaldialplan = PRI_LOCAL_ISDN;
02118 }
02119 }
02120 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
02121 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
02122 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, PRI_REDIR_UNCONDITIONAL);
02123
02124 #ifdef SUPPORT_USERUSER
02125
02126 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
02127
02128 if (useruser)
02129 pri_sr_set_useruser(sr, useruser);
02130 #endif
02131
02132 if (pri_setup(p->pri->pri, p->call, sr)) {
02133 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
02134 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
02135 pri_rel(p->pri);
02136 ast_mutex_unlock(&p->lock);
02137 pri_sr_free(sr);
02138 return -1;
02139 }
02140 pri_sr_free(sr);
02141 ast_setstate(ast, AST_STATE_DIALING);
02142 pri_rel(p->pri);
02143 }
02144 #endif
02145 ast_mutex_unlock(&p->lock);
02146 return 0;
02147 }
02148
02149 static void destroy_zt_pvt(struct zt_pvt **pvt)
02150 {
02151 struct zt_pvt *p = *pvt;
02152
02153 if(p->prev)
02154 p->prev->next = p->next;
02155 if(p->next)
02156 p->next->prev = p->prev;
02157 ast_mutex_destroy(&p->lock);
02158 free(p);
02159 *pvt = NULL;
02160 }
02161
02162 static int destroy_channel(struct zt_pvt *prev, struct zt_pvt *cur, int now)
02163 {
02164 int owned = 0;
02165 int i = 0;
02166
02167 if (!now) {
02168 if (cur->owner) {
02169 owned = 1;
02170 }
02171
02172 for (i = 0; i < 3; i++) {
02173 if (cur->subs[i].owner) {
02174 owned = 1;
02175 }
02176 }
02177 if (!owned) {
02178 if (prev) {
02179 prev->next = cur->next;
02180 if (prev->next)
02181 prev->next->prev = prev;
02182 else
02183 ifend = prev;
02184 } else {
02185 iflist = cur->next;
02186 if (iflist)
02187 iflist->prev = NULL;
02188 else
02189 ifend = NULL;
02190 }
02191 if (cur->subs[SUB_REAL].zfd > -1) {
02192 zt_close(cur->subs[SUB_REAL].zfd);
02193 }
02194 destroy_zt_pvt(&cur);
02195 }
02196 } else {
02197 if (prev) {
02198 prev->next = cur->next;
02199 if (prev->next)
02200 prev->next->prev = prev;
02201 else
02202 ifend = prev;
02203 } else {
02204 iflist = cur->next;
02205 if (iflist)
02206 iflist->prev = NULL;
02207 else
02208 ifend = NULL;
02209 }
02210 if (cur->subs[SUB_REAL].zfd > -1) {
02211 zt_close(cur->subs[SUB_REAL].zfd);
02212 }
02213 destroy_zt_pvt(&cur);
02214 }
02215 return 0;
02216 }
02217
02218 #ifdef ZAPATA_PRI
02219 int pri_is_up(struct zt_pri *pri)
02220 {
02221 int x;
02222 for (x=0;x<NUM_DCHANS;x++) {
02223 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
02224 return 1;
02225 }
02226 return 0;
02227 }
02228
02229 int pri_assign_bearer(struct zt_pvt *crv, struct zt_pri *pri, struct zt_pvt *bearer)
02230 {
02231 bearer->owner = &inuse;
02232 bearer->realcall = crv;
02233 crv->subs[SUB_REAL].zfd = bearer->subs[SUB_REAL].zfd;
02234 if (crv->subs[SUB_REAL].owner)
02235 crv->subs[SUB_REAL].owner->fds[0] = crv->subs[SUB_REAL].zfd;
02236 crv->bearer = bearer;
02237 crv->call = bearer->call;
02238 crv->pri = pri;
02239 return 0;
02240 }
02241
02242 static char *pri_order(int level)
02243 {
02244 switch(level) {
02245 case 0:
02246 return "Primary";
02247 case 1:
02248 return "Secondary";
02249 case 2:
02250 return "Tertiary";
02251 case 3:
02252 return "Quaternary";
02253 default:
02254 return "<Unknown>";
02255 }
02256 }
02257
02258
02259 int pri_active_dchan_fd(struct zt_pri *pri)
02260 {
02261 int x = -1;
02262
02263 for (x = 0; x < NUM_DCHANS; x++) {
02264 if ((pri->dchans[x] == pri->pri))
02265 break;
02266 }
02267
02268 return pri->fds[x];
02269 }
02270
02271 int pri_find_dchan(struct zt_pri *pri)
02272 {
02273 int oldslot = -1;
02274 struct pri *old;
02275 int newslot = -1;
02276 int x;
02277 old = pri->pri;
02278 for(x=0;x<NUM_DCHANS;x++) {
02279 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
02280 newslot = x;
02281 if (pri->dchans[x] == old) {
02282 oldslot = x;
02283 }
02284 }
02285 if (newslot < 0) {
02286 newslot = 0;
02287 ast_log(LOG_WARNING, "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
02288 pri->dchannels[newslot]);
02289 }
02290 if (old && (oldslot != newslot))
02291 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
02292 pri->dchannels[oldslot], pri->dchannels[newslot]);
02293 pri->pri = pri->dchans[newslot];
02294 return 0;
02295 }
02296 #endif
02297
02298 static int zt_hangup(struct ast_channel *ast)
02299 {
02300 int res;
02301 int index,x, law;
02302
02303 struct zt_pvt *p = ast->tech_pvt;
02304 struct zt_pvt *tmp = NULL;
02305 struct zt_pvt *prev = NULL;
02306 ZT_PARAMS par;
02307
02308 if (option_debug)
02309 ast_log(LOG_DEBUG, "zt_hangup(%s)\n", ast->name);
02310 if (!ast->tech_pvt) {
02311 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
02312 return 0;
02313 }
02314
02315 ast_mutex_lock(&p->lock);
02316
02317 index = zt_get_index(ast, p, 1);
02318
02319 if (p->sig == SIG_PRI) {
02320 x = 1;
02321 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02322 }
02323
02324 x = 0;
02325 zt_confmute(p, 0);
02326 restore_gains(p);
02327 if (p->origcid_num) {
02328 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
02329 free(p->origcid_num);
02330 p->origcid_num = NULL;
02331 }
02332 if (p->origcid_name) {
02333 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
02334 free(p->origcid_name);
02335 p->origcid_name = NULL;
02336 }
02337 if (p->dsp)
02338 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02339 if (p->exten)
02340 p->exten[0] = '\0';
02341
02342 ast_log(LOG_DEBUG, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
02343 p->channel, index, p->subs[SUB_REAL].zfd, p->subs[SUB_CALLWAIT].zfd, p->subs[SUB_THREEWAY].zfd);
02344 p->ignoredtmf = 0;
02345
02346 if (index > -1) {
02347
02348 p->subs[index].owner = NULL;
02349 p->subs[index].needanswer = 0;
02350 p->subs[index].needflash = 0;
02351 p->subs[index].needringing = 0;
02352 p->subs[index].needbusy = 0;
02353 p->subs[index].needcongestion = 0;
02354 p->subs[index].linear = 0;
02355 p->subs[index].needcallerid = 0;
02356 p->polarity = POLARITY_IDLE;
02357 zt_setlinear(p->subs[index].zfd, 0);
02358 if (index == SUB_REAL) {
02359 if ((p->subs[SUB_CALLWAIT].zfd > -1) && (p->subs[SUB_THREEWAY].zfd > -1)) {
02360 ast_log(LOG_DEBUG, "Normal call hung up with both three way call and a call waiting call in place?\n");
02361 if (p->subs[SUB_CALLWAIT].inthreeway) {
02362
02363 ast_log(LOG_DEBUG, "We were flipped over to the callwait, moving back and unowning.\n");
02364
02365 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02366 unalloc_sub(p, SUB_CALLWAIT);
02367 p->owner = NULL;
02368 } else {
02369
02370 ast_log(LOG_DEBUG, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
02371 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02372 unalloc_sub(p, SUB_THREEWAY);
02373 if (p->subs[SUB_REAL].inthreeway) {
02374
02375
02376 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02377 p->owner = p->subs[SUB_REAL].owner;
02378 } else {
02379
02380 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02381 p->owner = NULL;
02382 }
02383 p->subs[SUB_REAL].inthreeway = 0;
02384 }
02385 } else if (p->subs[SUB_CALLWAIT].zfd > -1) {
02386
02387 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
02388 unalloc_sub(p, SUB_CALLWAIT);
02389 p->owner = p->subs[SUB_REAL].owner;
02390 if (p->owner->_state != AST_STATE_UP)
02391 p->subs[SUB_REAL].needanswer = 1;
02392 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
02393 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
02394 } else if (p->subs[SUB_THREEWAY].zfd > -1) {
02395 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02396 unalloc_sub(p, SUB_THREEWAY);
02397 if (p->subs[SUB_REAL].inthreeway) {
02398
02399
02400 ast_log(LOG_DEBUG, "Call was complete, setting owner to former third call\n");
02401 p->owner = p->subs[SUB_REAL].owner;
02402 } else {
02403
02404 ast_log(LOG_DEBUG, "Call was incomplete, setting owner to NULL\n");
02405 p->owner = NULL;
02406 }
02407 p->subs[SUB_REAL].inthreeway = 0;
02408 }
02409 } else if (index == SUB_CALLWAIT) {
02410
02411 if (p->subs[SUB_CALLWAIT].inthreeway) {
02412
02413
02414 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
02415 ast_moh_start(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), NULL);
02416 p->subs[SUB_THREEWAY].inthreeway = 0;
02417
02418 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
02419 unalloc_sub(p, SUB_THREEWAY);
02420 } else
02421 unalloc_sub(p, SUB_CALLWAIT);
02422 } else if (index == SUB_THREEWAY) {
02423 if (p->subs[SUB_CALLWAIT].inthreeway) {
02424
02425
02426 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner))
02427 ast_moh_start(ast_bridged_channel(p->subs[SUB_CALLWAIT].owner), NULL);
02428 p->subs[SUB_CALLWAIT].inthreeway = 0;
02429 }
02430 p->subs[SUB_REAL].inthreeway = 0;
02431
02432
02433 unalloc_sub(p, SUB_THREEWAY);
02434 } else {
02435
02436 ast_log(LOG_WARNING, "Index found but not any type of call?\n");
02437 }
02438 }
02439
02440
02441 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
02442 p->owner = NULL;
02443 p->ringt = 0;
02444 p->distinctivering = 0;
02445 p->confirmanswer = 0;
02446 p->cidrings = 1;
02447 p->outgoing = 0;
02448 p->digital = 0;
02449 p->faxhandled = 0;
02450 p->pulsedial = 0;
02451 p->onhooktime = time(NULL);
02452 #ifdef ZAPATA_PRI
02453 p->proceeding = 0;
02454 p->progress = 0;
02455 p->alerting = 0;
02456 p->setup_ack = 0;
02457 #endif
02458 if (p->dsp) {
02459 ast_dsp_free(p->dsp);
02460 p->dsp = NULL;
02461 }
02462
02463 law = ZT_LAW_DEFAULT;
02464 res = ioctl(p->subs[SUB_REAL].zfd, ZT_SETLAW, &law);
02465 if (res < 0)
02466 ast_log(LOG_WARNING, "Unable to set law on channel %d to default\n", p->channel);
02467
02468 #ifdef ZAPATA_PRI
02469 if (p->pri) {
02470 #ifdef SUPPORT_USERUSER
02471 char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
02472 #endif
02473
02474
02475 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
02476 if (!pri_grab(p, p->pri)) {
02477 if (p->alreadyhungup) {
02478 ast_log(LOG_DEBUG, "Already hungup... Calling hangup once, and clearing call\n");
02479
02480 #ifdef SUPPORT_USERUSER
02481 pri_call_set_useruser(p->call, useruser);
02482 #endif
02483
02484 pri_hangup(p->pri->pri, p->call, -1);
02485 p->call = NULL;
02486 if (p->bearer)
02487 p->bearer->call = NULL;
02488 } else {
02489 char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
02490 int icause = ast->hangupcause ? ast->hangupcause : -1;
02491 ast_log(LOG_DEBUG, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
02492
02493 #ifdef SUPPORT_USERUSER
02494 pri_call_set_useruser(p->call, useruser);
02495 #endif
02496
02497 p->alreadyhungup = 1;
02498 if (p->bearer)
02499 p->bearer->alreadyhungup = 1;
02500 if (cause) {
02501 if (atoi(cause))
02502 icause = atoi(cause);
02503 }
02504 pri_hangup(p->pri->pri, p->call, icause);
02505 }
02506 if (res < 0)
02507 ast_log(LOG_WARNING, "pri_disconnect failed\n");
02508 pri_rel(p->pri);
02509 } else {
02510 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02511 res = -1;
02512 }
02513 } else {
02514 if (p->bearer)
02515 ast_log(LOG_DEBUG, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
02516 p->call = NULL;
02517 res = 0;
02518 }
02519 }
02520 #endif
02521 #ifdef ZAPATA_R2
02522 if (p->sig == SIG_R2) {
02523 if (p->hasr2call) {
02524 mfcr2_DropCall(p->r2, NULL, UC_NORMAL_CLEARING);
02525 p->hasr2call = 0;
02526 res = 0;
02527 } else
02528 res = 0;
02529
02530 }
02531 #endif
02532 if (p->sig && (p->sig != SIG_PRI) && (p->sig != SIG_R2))
02533 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_ONHOOK);
02534 if (res < 0) {
02535 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
02536 }
02537 switch(p->sig) {
02538 case SIG_FXOGS:
02539 case SIG_FXOLS:
02540 case SIG_FXOKS:
02541 res = ioctl(p->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &par);
02542 if (!res) {
02543 #if 0
02544 ast_log(LOG_DEBUG, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
02545 #endif
02546
02547 if ((par.rxisoffhook) && (!p->radio))
02548 tone_zone_play_tone(p->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
02549 else
02550 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02551 }
02552 break;
02553 case SIG_FXSGS:
02554 case SIG_FXSLS:
02555 case SIG_FXSKS:
02556
02557
02558 if (ast->_state != AST_STATE_RESERVED) {
02559 time(&p->guardtime);
02560 p->guardtime += 2;
02561 }
02562 break;
02563 default:
02564 tone_zone_play_tone(p->subs[SUB_REAL].zfd, -1);
02565 }
02566 if (p->cidspill)
02567 free(p->cidspill);
02568 if (p->sig)
02569 zt_disable_ec(p);
02570 x = 0;
02571 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
02572 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
02573 p->didtdd = 0;
02574 p->cidspill = NULL;
02575 p->callwaitcas = 0;
02576 p->callwaiting = p->permcallwaiting;
02577 p->hidecallerid = p->permhidecallerid;
02578 p->dialing = 0;
02579 p->rdnis[0] = '\0';
02580 update_conf(p);
02581 reset_conf(p);
02582
02583 if (p->sig == SIG_PRI) {
02584 x = 0;
02585 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
02586 }
02587 #ifdef ZAPATA_PRI
02588 if (p->bearer) {
02589 ast_log(LOG_DEBUG, "Freeing up bearer channel %d\n", p->bearer->channel);
02590
02591
02592 update_conf(p->bearer);
02593 reset_conf(p->bearer);
02594 p->bearer->owner = NULL;
02595 p->bearer->realcall = NULL;
02596 p->bearer = NULL;
02597 p->subs[SUB_REAL].zfd = -1;
02598 p->pri = NULL;
02599 }
02600 #endif
02601 restart_monitor();
02602 }
02603
02604
02605 p->callwaitingrepeat = 0;
02606 p->cidcwexpire = 0;
02607 ast->tech_pvt = NULL;
02608 ast_mutex_unlock(&p->lock);
02609 ast_mutex_lock(&usecnt_lock);
02610 usecnt--;
02611 if (usecnt < 0)
02612 ast_log(LOG_WARNING, "Usecnt < 0???\n");
02613 ast_mutex_unlock(&usecnt_lock);
02614 ast_update_use_count();
02615 if (option_verbose > 2)
02616 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
02617
02618 ast_mutex_lock(&iflock);
02619 tmp = iflist;
02620 prev = NULL;
02621 if (p->destroy) {
02622 while (tmp) {
02623 if (tmp == p) {
02624 destroy_channel(prev, tmp, 0);
02625 break;
02626 } else {
02627 prev = tmp;
02628 tmp = tmp->next;
02629 }
02630 }
02631 }
02632 ast_mutex_unlock(&iflock);
02633 return 0;
02634 }
02635
02636 static int zt_answer(struct ast_channel *ast)
02637 {
02638 struct zt_pvt *p = ast->tech_pvt;
02639 int res=0;
02640 int index;
02641 int oldstate = ast->_state;
02642 ast_setstate(ast, AST_STATE_UP);
02643 ast_mutex_lock(&p->lock);
02644 index = zt_get_index(ast, p, 0);
02645 if (index < 0)
02646 index = SUB_REAL;
02647
02648 if (p->radio) {
02649 ast_mutex_unlock(&p->lock);
02650 return 0;
02651 }
02652 switch(p->sig) {
02653 case SIG_FXSLS:
02654 case SIG_FXSGS:
02655 case SIG_FXSKS:
02656 p->ringt = 0;
02657
02658 case SIG_EM:
02659 case SIG_EM_E1:
02660 case SIG_EMWINK:
02661 case SIG_FEATD:
02662 case SIG_FEATDMF:
02663 case SIG_E911:
02664 case SIG_FEATB:
02665 case SIG_SF:
02666 case SIG_SFWINK:
02667 case SIG_SF_FEATD:
02668 case SIG_SF_FEATDMF:
02669 case SIG_SF_FEATB:
02670 case SIG_FXOLS:
02671 case SIG_FXOGS:
02672 case SIG_FXOKS:
02673
02674 ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
02675 if(p->hanguponpolarityswitch) {
02676 gettimeofday(&p->polaritydelaytv, NULL);
02677 }
02678 res = zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
02679 tone_zone_play_tone(p->subs[index].zfd, -1);
02680 p->dialing = 0;
02681 if ((index == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
02682 if (oldstate == AST_STATE_RINGING) {
02683 ast_log(LOG_DEBUG, "Finally swapping real and threeway\n");
02684 tone_zone_play_tone(p->subs[SUB_THREEWAY].zfd, -1);
02685 swap_subs(p, SUB_THREEWAY, SUB_REAL);
02686 p->owner = p->subs[SUB_REAL].owner;
02687 }
02688 }
02689 if (p->sig & __ZT_SIG_FXS) {
02690 zt_enable_ec(p);
02691 zt_train_ec(p);
02692 }
02693 break;
02694 #ifdef ZAPATA_PRI
02695 case SIG_PRI:
02696
02697 if (!pri_grab(p, p->pri)) {
02698 p->proceeding = 1;
02699 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
02700 pri_rel(p->pri);
02701 } else {
02702 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
02703 res= -1;
02704 }
02705 break;
02706 #endif
02707 #ifdef ZAPATA_R2
02708 case SIG_R2:
02709 res = mfcr2_AnswerCall(p->r2, NULL);
02710 if (res)
02711 ast_log(LOG_WARNING, "R2 Answer call failed :( on %s\n", ast->name);
02712 break;
02713 #endif
02714 case 0:
02715 ast_mutex_unlock(&p->lock);
02716 return 0;
02717 default:
02718 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
02719 res = -1;
02720 }
02721 ast_mutex_unlock(&p->lock);
02722 return res;
02723 }
02724
02725 static int zt_setoption(struct ast_channel *chan, int option, void *data, int datalen)
02726 {
02727 char *cp;
02728 signed char *scp;
02729 int x;
02730 int index;
02731 struct zt_pvt *p = chan->tech_pvt;
02732
02733
02734 if (!data || (datalen < 1)) {
02735 errno = EINVAL;
02736 return -1;
02737 }
02738
02739 switch(option) {
02740 case AST_OPTION_TXGAIN:
02741 scp = (signed char *) data;
02742 index = zt_get_index(chan, p, 0);
02743 if (index < 0) {
02744 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
02745 return -1;
02746 }
02747 ast_log(LOG_DEBUG, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
02748 return set_actual_txgain(p->subs[index].zfd, 0, p->txgain + (float) *scp, p->law);
02749 case AST_OPTION_RXGAIN:
02750 scp = (signed char *) data;
02751 index = zt_get_index(chan, p, 0);
02752 if (index < 0) {
02753 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
02754 return -1;
02755 }
02756 ast_log(LOG_DEBUG, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
02757 return set_actual_rxgain(p->subs[index].zfd, 0, p->rxgain + (float) *scp, p->law);
02758 case AST_OPTION_TONE_VERIFY:
02759 if (!p->dsp)
02760 break;
02761 cp = (char *) data;
02762 switch (*cp) {
02763 case 1:
02764 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
02765 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | p->dtmfrelax);
02766 break;
02767 case 2:
02768 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
02769 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax);
02770 break;
02771 default:
02772 ast_log(LOG_DEBUG, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
02773 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
02774 break;
02775 }
02776 break;
02777 case AST_OPTION_TDD:
02778
02779 cp = (char *) data;
02780 p->mate = 0;
02781 if (!*cp) {
02782 ast_log(LOG_DEBUG, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
02783 if (p->tdd) tdd_free(p->tdd);
02784 p->tdd = 0;
02785 break;
02786 }
02787 ast_log(LOG_DEBUG, "Set option TDD MODE, value: %s(%d) on %s\n",
02788 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
02789 zt_disable_ec(p);
02790
02791 if (!p->didtdd) {
02792 unsigned char mybuf[41000],*buf;
02793 int size,res,fd,len;
02794 struct pollfd fds[1];
02795
02796 buf = mybuf;
02797 memset(buf, 0x7f, sizeof(mybuf));
02798 ast_tdd_gen_ecdisa(buf + 16000, 16000);
02799 len = 40000;
02800 index = zt_get_index(chan, p, 0);
02801 if (index < 0) {
02802 ast_log(LOG_WARNING, "No index in TDD?\n");
02803 return -1;
02804 }
02805 fd = p->subs[index].zfd;
02806 while(len) {
02807 if (ast_check_hangup(chan)) return -1;
02808 size = len;
02809 if (size > READ_SIZE)
02810 size = READ_SIZE;
02811 fds[0].fd = fd;
02812 fds[0].events = POLLPRI | POLLOUT;
02813 fds[0].revents = 0;
02814 res = poll(fds, 1, -1);
02815 if (!res) {
02816 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
02817 continue;
02818 }
02819
02820 if (fds[0].revents & POLLPRI) return -1;
02821 if (!(fds[0].revents & POLLOUT)) {
02822 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
02823 continue;
02824 }
02825 res = write(fd, buf, size);
02826 if (res != size) {
02827 if (res == -1) return -1;
02828 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
02829 break;
02830 }
02831 len -= size;
02832 buf += size;
02833 }
02834 p->didtdd = 1;
02835 }
02836 if (*cp == 2) {
02837 if (p->tdd) tdd_free(p->tdd);
02838 p->tdd = 0;
02839 p->mate = 1;
02840 break;
02841 }
02842 if (!p->tdd) {
02843 p->tdd = tdd_new();
02844 }
02845 break;
02846 case AST_OPTION_RELAXDTMF:
02847 if (!p->dsp)
02848 break;
02849 cp = (char *) data;
02850 ast_log(LOG_DEBUG, "Set option RELAX DTMF, value: %s(%d) on %s\n",
02851 *cp ? "ON" : "OFF", (int) *cp, chan->name);
02852 ast_dsp_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
02853 break;
02854 case AST_OPTION_AUDIO_MODE:
02855 cp = (char *) data;
02856 if (!*cp) {
02857 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
02858 x = 0;
02859 zt_disable_ec(p);
02860 } else {
02861 ast_log(LOG_DEBUG, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
02862 x = 1;
02863 }
02864 if (ioctl(p->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &x) == -1)
02865 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", p->channel, x);
02866 break;
02867 }
02868 errno = 0;
02869
02870 return 0;
02871 }
02872
02873 static void zt_unlink(struct zt_pvt *slave, struct zt_pvt *master, int needlock)
02874 {
02875
02876 int x;
02877 int hasslaves;
02878 if (!master)
02879 return;
02880 if (needlock) {
02881 ast_mutex_lock(&master->lock);
02882 if (slave) {
02883 while(ast_mutex_trylock(&slave->lock)) {
02884 ast_mutex_unlock(&master->lock);
02885 usleep(1);
02886 ast_mutex_lock(&master->lock);
02887 }
02888 }
02889 }
02890 hasslaves = 0;
02891 for (x=0;x<MAX_SLAVES;x++) {
02892 if (master->slaves[x]) {
02893 if (!slave || (master->slaves[x] == slave)) {
02894
02895 ast_log(LOG_DEBUG, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
02896 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
02897 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
02898 master->slaves[x]->master = NULL;
02899 master->slaves[x] = NULL;
02900 } else
02901 hasslaves = 1;
02902 }
02903 if (!hasslaves)
02904 master->inconference = 0;
02905 }
02906 if (!slave) {
02907 if (master->master) {
02908
02909 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
02910 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
02911 hasslaves = 0;
02912 for (x=0;x<MAX_SLAVES;x++) {
02913 if (master->master->slaves[x] == master)
02914 master->master->slaves[x] = NULL;
02915 else if (master->master->slaves[x])
02916 hasslaves = 1;
02917 }
02918 if (!hasslaves)
02919 master->master->inconference = 0;
02920 }
02921 master->master = NULL;
02922 }
02923 update_conf(master);
02924 if (needlock) {
02925 if (slave)
02926 ast_mutex_unlock(&slave->lock);
02927 ast_mutex_unlock(&master->lock);
02928 }
02929 }
02930
02931 static void zt_link(struct zt_pvt *slave, struct zt_pvt *master) {
02932 int x;
02933 if (!slave || !master) {
02934 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
02935 return;
02936 }
02937 for (x=0;x<MAX_SLAVES;x++) {
02938 if (!master->slaves[x]) {
02939 master->slaves[x] = slave;
02940 break;
02941 }
02942 }
02943 if (x >= MAX_SLAVES) {
02944 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
02945 master->slaves[MAX_SLAVES - 1] = slave;
02946 }
02947 if (slave->master)
02948 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
02949 slave->master = master;
02950
02951 ast_log(LOG_DEBUG, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
02952 }
02953
02954 static void disable_dtmf_detect(struct zt_pvt *p)
02955 {
02956 #ifdef ZT_TONEDETECT
02957 int val;
02958 #endif
02959
02960 p->ignoredtmf = 1;
02961
02962 #ifdef ZT_TONEDETECT
02963 val = 0;
02964 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
02965 #endif
02966 if (!p->hardwaredtmf && p->dsp) {
02967 p->dsp_features &= ~DSP_FEATURE_DTMF_DETECT;
02968 ast_dsp_set_features(p->dsp, p->dsp_features);
02969 }
02970 }
02971
02972 static void enable_dtmf_detect(struct zt_pvt *p)
02973 {
02974 #ifdef ZT_TONEDETECT
02975 int val;
02976 #endif
02977
02978 if (p->channel == CHAN_PSEUDO)
02979 return;
02980
02981 p->ignoredtmf = 0;
02982
02983 #ifdef ZT_TONEDETECT
02984 val = ZT_TONEDETECT_ON | ZT_TONEDETECT_MUTE;
02985 ioctl(p->subs[SUB_REAL].zfd, ZT_TONEDETECT, &val);
02986 #endif
02987 if (!p->hardwaredtmf && p->dsp) {
02988 p->dsp_features |= DSP_FEATURE_DTMF_DETECT;
02989 ast_dsp_set_features(p->dsp, p->dsp_features);
02990 }
02991 }
02992
02993 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)
02994 {
02995 struct ast_channel *who;
02996 struct zt_pvt *p0, *p1, *op0, *op1;
02997 struct zt_pvt *master = NULL, *slave = NULL;
02998 struct ast_frame *f;
02999 int inconf = 0;
03000 int nothingok = 1;
03001 int ofd0, ofd1;
03002 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
03003 int os0 = -1, os1 = -1;
03004 int priority = 0;
03005 struct ast_channel *oc0, *oc1;
03006 enum ast_bridge_result res;
03007
03008 #ifdef PRI_2BCT
03009 int triedtopribridge = 0;
03010 q931_call *q931c0 = NULL, *q931c1 = NULL;
03011 #endif
03012
03013
03014
03015
03016
03017
03018 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
03019 return AST_BRIDGE_FAILED_NOWARN;
03020
03021 ast_mutex_lock(&c0->lock);
03022 ast_mutex_lock(&c1->lock);
03023
03024 p0 = c0->tech_pvt;
03025 p1 = c1->tech_pvt;
03026
03027 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
03028 ast_mutex_unlock(&c0->lock);
03029 ast_mutex_unlock(&c1->lock);
03030 return AST_BRIDGE_FAILED_NOWARN;
03031 }
03032
03033 oi0 = zt_get_index(c0, p0, 0);
03034 oi1 = zt_get_index(c1, p1, 0);
03035 if ((oi0 < 0) || (oi1 < 0)) {
03036 ast_mutex_unlock(&c0->lock);
03037 ast_mutex_unlock(&c1->lock);
03038 return AST_BRIDGE_FAILED;
03039 }
03040
03041 op0 = p0 = c0->tech_pvt;
03042 op1 = p1 = c1->tech_pvt;
03043 ofd0 = c0->fds[0];
03044 ofd1 = c1->fds[0];
03045 oc0 = p0->owner;
03046 oc1 = p1->owner;
03047
03048 if (ast_mutex_trylock(&p0->lock)) {
03049
03050 ast_mutex_unlock(&c0->lock);
03051 ast_mutex_unlock(&c1->lock);
03052 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03053 return AST_BRIDGE_RETRY;
03054 }
03055 if (ast_mutex_trylock(&p1->lock)) {
03056
03057 ast_mutex_unlock(&p0->lock);
03058 ast_mutex_unlock(&c0->lock);
03059 ast_mutex_unlock(&c1->lock);
03060 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
03061 return AST_BRIDGE_RETRY;
03062 }
03063
03064 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03065 if (p0->owner && p1->owner) {
03066
03067 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
03068 master = p0;
03069 slave = p1;
03070 inconf = 1;
03071 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
03072 master = p1;
03073 slave = p0;
03074 inconf = 1;
03075 } else {
03076 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
03077 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
03078 p0->channel,
03079 oi0, (p0->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03080 p0->subs[SUB_REAL].inthreeway, p0->channel,
03081 oi0, (p1->subs[SUB_CALLWAIT].zfd > -1) ? 1 : 0,
03082 p1->subs[SUB_REAL].inthreeway);
03083 }
03084 nothingok = 0;
03085 }
03086 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
03087 if (p1->subs[SUB_THREEWAY].inthreeway) {
03088 master = p1;
03089 slave = p0;
03090 nothingok = 0;
03091 }
03092 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
03093 if (p0->subs[SUB_THREEWAY].inthreeway) {
03094 master = p0;
03095 slave = p1;
03096 nothingok = 0;
03097 }
03098 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
03099
03100
03101 if (p1->subs[SUB_CALLWAIT].inthreeway) {
03102 master = p1;
03103 slave = p0;
03104 nothingok = 0;
03105 }
03106 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
03107
03108 if (p0->subs[SUB_CALLWAIT].inthreeway) {
03109 master = p0;
03110 slave = p1;
03111 nothingok = 0;
03112 }
03113 }
03114 ast_log(LOG_DEBUG, "master: %d, slave: %d, nothingok: %d\n",
03115 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
03116 if (master && slave) {
03117
03118
03119
03120 if ((oi1 == SUB_THREEWAY) &&
03121 p1->subs[SUB_THREEWAY].inthreeway &&
03122 p1->subs[SUB_REAL].owner &&
03123 p1->subs[SUB_REAL].inthreeway &&
03124 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03125 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c0->name, c1->name);
03126 tone_zone_play_tone(p0->subs[oi0].zfd, ZT_TONE_RINGTONE);
03127 os1 = p1->subs[SUB_REAL].owner->_state;
03128 } else {
03129 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p0->channel, oi0, p1->channel, oi1);
03130 tone_zone_play_tone(p0->subs[oi0].zfd, -1);
03131 }
03132 if ((oi0 == SUB_THREEWAY) &&
03133 p0->subs[SUB_THREEWAY].inthreeway &&
03134 p0->subs[SUB_REAL].owner &&
03135 p0->subs[SUB_REAL].inthreeway &&
03136 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
03137 ast_log(LOG_DEBUG, "Playing ringback on %s since %s is in a ringing three-way\n", c1->name, c0->name);
03138 tone_zone_play_tone(p1->subs[oi1].zfd, ZT_TONE_RINGTONE);
03139 os0 = p0->subs[SUB_REAL].owner->_state;
03140 } else {
03141 ast_log(LOG_DEBUG, "Stopping tones on %d/%d talking to %d/%d\n", p1->channel, oi1, p0->channel, oi0);
03142 tone_zone_play_tone(p1->subs[oi0].zfd, -1);
03143 }
03144 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
03145 if (!p0->echocanbridged || !p1->echocanbridged) {
03146
03147 zt_disable_ec(p0);
03148 zt_disable_ec(p1);
03149 }
03150 }
03151 zt_link(slave, master);
03152 master->inconference = inconf;
03153 } else if (!nothingok)
03154 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
03155
03156 update_conf(p0);
03157 update_conf(p1);
03158 t0 = p0->subs[SUB_REAL].inthreeway;
03159 t1 = p1->subs[SUB_REAL].inthreeway;
03160
03161 ast_mutex_unlock(&p0->lock);
03162 ast_mutex_unlock(&p1->lock);
03163
03164 ast_mutex_unlock(&c0->lock);
03165 ast_mutex_unlock(&c1->lock);
03166
03167
03168 if ((!master || !slave) && !nothingok) {
03169 zt_enable_ec(p0);
03170 zt_enable_ec(p1);
03171 return AST_BRIDGE_FAILED;
03172 }
03173
03174 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03175 disable_dtmf_detect(op0);
03176
03177 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03178 disable_dtmf_detect(op1);
03179
03180 for (;;) {
03181 struct ast_channel *c0_priority[2] = {c0, c1};
03182 struct ast_channel *c1_priority[2] = {c1, c0};
03183
03184
03185
03186 ast_mutex_lock(&c0->lock);
03187 ast_mutex_lock(&c1->lock);
03188 p0 = c0->tech_pvt;
03189 p1 = c1->tech_pvt;
03190
03191 if (op0 == p0)
03192 i0 = zt_get_index(c0, p0, 1);
03193 if (op1 == p1)
03194 i1 = zt_get_index(c1, p1, 1);
03195 ast_mutex_unlock(&c0->lock);
03196 ast_mutex_unlock(&c1->lock);
03197
03198 if (!timeoutms ||
03199 (op0 != p0) ||
03200 (op1 != p1) ||
03201 (ofd0 != c0->fds[0]) ||
03202 (ofd1 != c1->fds[0]) ||
03203 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
03204 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
03205 (oc0 != p0->owner) ||
03206 (oc1 != p1->owner) ||
03207 (t0 != p0->subs[SUB_REAL].inthreeway) ||
03208 (t1 != p1->subs[SUB_REAL].inthreeway) ||
03209 (oi0 != i0) ||
03210 (oi1 != i1)) {
03211 ast_log(LOG_DEBUG, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
03212 op0->channel, oi0, op1->channel, oi1);
03213 res = AST_BRIDGE_RETRY;
03214 goto return_from_bridge;
03215 }
03216
03217 #ifdef PRI_2BCT
03218 q931c0 = p0->call;
03219 q931c1 = p1->call;
03220 if (p0->transfer && p1->transfer
03221 && q931c0 && q931c1
03222 && !triedtopribridge) {
03223 pri_channel_bridge(q931c0, q931c1);
03224 triedtopribridge = 1;
03225 }
03226 #endif
03227
03228 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
03229 if (!who) {
03230 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
03231 continue;
03232 }
03233 f = ast_read(who);
03234 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
03235 *fo = f;
03236 *rc = who;
03237 res = AST_BRIDGE_COMPLETE;
03238 goto return_from_bridge;
03239 }
03240 if (f->frametype == AST_FRAME_DTMF) {
03241 if ((who == c0) && p0->pulsedial) {
03242 ast_write(c1, f);
03243 } else if ((who == c1) && p1->pulsedial) {
03244 ast_write(c0, f);
03245 } else {
03246 *fo = f;
03247 *rc = who;
03248 res = AST_BRIDGE_COMPLETE;
03249 goto return_from_bridge;
03250 }
03251 }
03252 ast_frfree(f);
03253
03254
03255 priority = !priority;
03256 }
03257
03258 return_from_bridge:
03259 if (op0 == p0)
03260 zt_enable_ec(p0);
03261
03262 if (op1 == p1)
03263 zt_enable_ec(p1);
03264
03265 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
03266 enable_dtmf_detect(op0);
03267
03268 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
03269 enable_dtmf_detect(op1);
03270
03271 zt_unlink(slave, master, 1);
03272
03273 return res;
03274 }
03275
03276 static int zt_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
03277 {
03278 struct zt_pvt *p = newchan->tech_pvt;
03279 int x;
03280 ast_mutex_lock(&p->lock);
03281 ast_log(LOG_DEBUG, "New owner for channel %d is %s\n", p->channel, newchan->name);
03282 if (p->owner == oldchan) {
03283 p->owner = newchan;
03284 }
03285 for (x=0;x<3;x++)
03286 if (p->subs[x].owner == oldchan) {
03287 if (!x)
03288 zt_unlink(NULL, p, 0);
03289 p->subs[x].owner = newchan;
03290 }
03291 if (newchan->_state == AST_STATE_RINGING)
03292 zt_indicate(newchan, AST_CONTROL_RINGING);
03293 update_conf(p);
03294 ast_mutex_unlock(&p->lock);
03295 return 0;
03296 }
03297
03298 static int zt_ring_phone(struct zt_pvt *p)
03299 {
03300 int x;
03301 int res;
03302
03303 x = 0;
03304 x = ZT_ONHOOK;
03305 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
03306 do {
03307 x = ZT_RING;
03308 res = ioctl(p->subs[SUB_REAL].zfd, ZT_HOOK, &x);
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 if (!ast_strlen_zero(i->cid_num))
05120 tmp->cid.cid_num = strdup(i->cid_num);
05121 if (!ast_strlen_zero(i->cid_name))
05122 tmp->cid.cid_name = strdup(i->cid_name);
05123 if (!ast_strlen_zero(i->cid_ani))
05124 tmp->cid.cid_ani = strdup(i->cid_num);
05125 else if (!ast_strlen_zero(i->cid_num))
05126 tmp->cid.cid_ani = strdup(i->cid_num);
05127 #else
05128 if (!ast_strlen_zero(i->cid_num)) {
05129 tmp->cid.cid_num = strdup(i->cid_num);
05130 tmp->cid.cid_ani = strdup(i->cid_num);
05131 }
05132 if (!ast_strlen_zero(i->cid_name))
05133 tmp->cid.cid_name = strdup(i->cid_name);
05134 #endif
05135 tmp->cid.cid_pres = i->callingpres;
05136 tmp->cid.cid_ton = i->cid_ton;
05137 #ifdef ZAPATA_PRI
05138 tmp->transfercapability = transfercapability;
05139 pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
05140 if (transfercapability & PRI_TRANS_CAP_DIGITAL) {
05141 i->digital = 1;
05142 }
05143
05144 i->isidlecall = 0;
05145 i->alreadyhungup = 0;
05146 #endif
05147
05148 i->fake_event = 0;
05149
05150 zt_confmute(i, 0);
05151 ast_setstate(tmp, state);
05152 ast_mutex_lock(&usecnt_lock);
05153 usecnt++;
05154 ast_mutex_unlock(&usecnt_lock);
05155 ast_update_use_count();
05156 if (startpbx) {
05157 if (ast_pbx_start(tmp)) {
05158 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
05159 ast_hangup(tmp);
05160 tmp = NULL;
05161 }
05162 }
05163 } else
05164 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
05165 return tmp;
05166 }
05167
05168
05169 static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
05170 {
05171 char c;
05172
05173 *str = 0;
05174 for (;;)
05175 {
05176
05177 c = ast_waitfordigit(chan, ms);
05178
05179 if (c < 1)
05180 return c;
05181 *str++ = c;
05182 *str = 0;
05183 if (strchr(term, c))
05184 return 1;
05185 }
05186 }
05187
05188 static int zt_wink(struct zt_pvt *p, int index)
05189 {
05190 int j;
05191 zt_set_hook(p->subs[index].zfd, ZT_WINK);
05192 for(;;)
05193 {
05194
05195 j = ZT_IOMUX_SIGEVENT;
05196
05197 if (ioctl(p->subs[index].zfd,ZT_IOMUX,&j) == -1) return(-1);
05198
05199 if (j & ZT_IOMUX_SIGEVENT) break;
05200 }
05201
05202 if (ioctl(p->subs[index].zfd,ZT_GETEVENT,&j) == -1) return(-1);
05203 return 0;
05204 }
05205
05206 static void *ss_thread(void *data)
05207 {
05208 struct ast_channel *chan = data;
05209 struct zt_pvt *p = chan->tech_pvt;
05210 char exten[AST_MAX_EXTENSION]="";
05211 char exten2[AST_MAX_EXTENSION]="";
05212 unsigned char buf[256];
05213 char dtmfcid[300];
05214 char dtmfbuf[300];
05215 struct callerid_state *cs;
05216 char *name=NULL, *number=NULL;
05217 int distMatches;
05218 int curRingData[3];
05219 int receivedRingT;
05220 int counter1;
05221 int counter;
05222 int samples = 0;
05223
05224 int flags;
05225 int i;
05226 int timeout;
05227 int getforward=0;
05228 char *s1, *s2;
05229 int len = 0;
05230 int res;
05231 int index;
05232 if (option_verbose > 2)
05233 ast_verbose( VERBOSE_PREFIX_3 "Starting simple switch on '%s'\n", chan->name);
05234 index = zt_get_index(chan, p, 1);
05235 if (index < 0) {
05236 ast_log(LOG_WARNING, "Huh?\n");
05237 ast_hangup(chan);
05238 return NULL;
05239 }
05240 if (p->dsp)
05241 ast_dsp_digitreset(p->dsp);
05242 switch(p->sig) {
05243 #ifdef ZAPATA_PRI
05244 case SIG_PRI:
05245
05246 ast_copy_string(exten, p->exten, sizeof(exten));
05247 len = strlen(exten);
05248 res = 0;
05249 while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05250 if (len && !ast_ignore_pattern(chan->context, exten))
05251 tone_zone_play_tone(p->subs[index].zfd, -1);
05252 else
05253 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05254 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
05255 timeout = matchdigittimeout;
05256 else
05257 timeout = gendigittimeout;
05258 res = ast_waitfordigit(chan, timeout);
05259 if (res < 0) {
05260 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05261 ast_hangup(chan);
05262 return NULL;
05263 } else if (res) {
05264 exten[len++] = res;
05265 exten[len] = '\0';
05266 } else
05267 break;
05268 }
05269
05270 if (ast_strlen_zero(exten)) {
05271 if (option_verbose > 2)
05272 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of empty extension received on overlap call\n");
05273 exten[0] = 's';
05274 exten[1] = '\0';
05275 }
05276 tone_zone_play_tone(p->subs[index].zfd, -1);
05277 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
05278
05279 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05280 if (p->dsp) ast_dsp_digitreset(p->dsp);
05281 zt_enable_ec(p);
05282 ast_setstate(chan, AST_STATE_RING);
05283 res = ast_pbx_run(chan);
05284 if (res) {
05285 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
05286 }
05287 } else {
05288 ast_log(LOG_DEBUG, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
05289 chan->hangupcause = AST_CAUSE_UNALLOCATED;
05290 ast_hangup(chan);
05291 p->exten[0] = '\0';
05292
05293 p->call = NULL;
05294 }
05295 return NULL;
05296 break;
05297 #endif
05298 case SIG_FEATD:
05299 case SIG_FEATDMF:
05300 case SIG_E911:
05301 case SIG_FEATB:
05302 case SIG_EMWINK:
05303 case SIG_SF_FEATD:
05304 case SIG_SF_FEATDMF:
05305 case SIG_SF_FEATB:
05306 case SIG_SFWINK:
05307 if (zt_wink(p, index))
05308 return NULL;
05309
05310 case SIG_EM:
05311 case SIG_EM_E1:
05312 case SIG_SF:
05313 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05314 if (p->dsp)
05315 ast_dsp_digitreset(p->dsp);
05316
05317 if (p->dsp) {
05318 if (NEED_MFDETECT(p))
05319 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_MF | p->dtmfrelax);
05320 else
05321 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05322 }
05323 memset(dtmfbuf, 0, sizeof(dtmfbuf));
05324
05325 if (!p->immediate)
05326
05327 res = ast_waitfordigit(chan, 5000);
05328 else res = 0;
05329 if (res > 0) {
05330
05331 dtmfbuf[0] = res;
05332 switch(p->sig) {
05333 case SIG_FEATD:
05334 case SIG_SF_FEATD:
05335 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05336 if (res > 0)
05337 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05338 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05339 break;
05340 case SIG_FEATDMF:
05341 case SIG_E911:
05342 case SIG_SF_FEATDMF:
05343 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05344 if (res > 0) {
05345
05346 if (p->sig == SIG_E911)
05347 zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
05348 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
05349 }
05350 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05351 break;
05352 case SIG_FEATB:
05353 case SIG_SF_FEATB:
05354 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
05355 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05356 break;
05357 case SIG_EMWINK:
05358
05359
05360
05361
05362 if (res == '*') {
05363 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
05364 if (res > 0)
05365 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
05366 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
05367 break;
05368 }
05369 default:
05370
05371 len = 1;
05372 dtmfbuf[len] = '\0';
05373 while((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05374 if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
05375 timeout = matchdigittimeout;
05376 } else {
05377 timeout = gendigittimeout;
05378 }
05379 res = ast_waitfordigit(chan, timeout);
05380 if (res < 0) {
05381 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05382 ast_hangup(chan);
05383 return NULL;
05384 } else if (res) {
05385 dtmfbuf[len++] = res;
05386 dtmfbuf[len] = '\0';
05387 } else {
05388 break;
05389 }
05390 }
05391 break;
05392 }
05393 }
05394 if (res == -1) {
05395 ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
05396 ast_hangup(chan);
05397 return NULL;
05398 } else if (res < 0) {
05399 ast_log(LOG_DEBUG, "Got hung up before digits finished\n");
05400 ast_hangup(chan);
05401 return NULL;
05402 }
05403 ast_copy_string(exten, dtmfbuf, sizeof(exten));
05404 if (ast_strlen_zero(exten))
05405 ast_copy_string(exten, "s", sizeof(exten));
05406 if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
05407
05408 if (exten[0] == '*') {
05409 char *stringp=NULL;
05410 ast_copy_string(exten2, exten, sizeof(exten2));
05411
05412 stringp=exten2 +1;
05413 s1 = strsep(&stringp, "*");
05414 s2 = strsep(&stringp, "*");
05415 if (s2) {
05416 if (!ast_strlen_zero(p->cid_num))
05417 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05418 else
05419 ast_set_callerid(chan, s1, NULL, s1);
05420 ast_copy_string(exten, s2, sizeof(exten));
05421 } else
05422 ast_copy_string(exten, s1, sizeof(exten));
05423 } else if (p->sig == SIG_FEATD)
05424 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05425 }
05426 if (p->sig == SIG_FEATDMF) {
05427 if (exten[0] == '*') {
05428 char *stringp=NULL;
05429 ast_copy_string(exten2, exten, sizeof(exten2));
05430
05431 stringp=exten2 +1;
05432 s1 = strsep(&stringp, "#");
05433 s2 = strsep(&stringp, "#");
05434 if (s2) {
05435 if (!ast_strlen_zero(p->cid_num))
05436 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05437 else
05438 if(*(s1 + 2))
05439 ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
05440 ast_copy_string(exten, s2 + 1, sizeof(exten));
05441 } else
05442 ast_copy_string(exten, s1 + 2, sizeof(exten));
05443 } else
05444 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
05445 }
05446 if (p->sig == SIG_E911) {
05447 if (exten[0] == '*') {
05448 char *stringp=NULL;
05449 ast_copy_string(exten2, exten, sizeof(exten2));
05450
05451 stringp=exten2 +1;
05452 s1 = strsep(&stringp, "#");
05453 s2 = strsep(&stringp, "#");
05454 if (s2 && (*(s2 + 1) == '0')) {
05455 if(*(s2 + 2))
05456 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
05457 }
05458 if (s1) ast_copy_string(exten, s1, sizeof(exten));
05459 else ast_copy_string(exten, "911", sizeof(exten));
05460 } else
05461 ast_log(LOG_WARNING, "Got a non-E911 input on channel %d. Assuming E&M Wink instead\n", p->channel);
05462 }
05463 if (p->sig == SIG_FEATB) {
05464 if (exten[0] == '*') {
05465 char *stringp=NULL;
05466 ast_copy_string(exten2, exten, sizeof(exten2));
05467
05468 stringp=exten2 +1;
05469 s1 = strsep(&stringp, "#");
05470 ast_copy_string(exten, exten2 + 1, sizeof(exten));
05471 } else
05472 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
05473 }
05474 if (p->sig == SIG_FEATDMF) {
05475 zt_wink(p, index);
05476 }
05477 zt_enable_ec(p);
05478 if (NEED_MFDETECT(p)) {
05479 if (p->dsp) {
05480 if (!p->hardwaredtmf)
05481 ast_dsp_digitmode(p->dsp,DSP_DIGITMODE_DTMF | p->dtmfrelax);
05482 else {
05483 ast_dsp_free(p->dsp);
05484 p->dsp = NULL;
05485 }
05486 }
05487 }
05488
05489 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
05490 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05491 if (p->dsp) ast_dsp_digitreset(p->dsp);
05492 res = ast_pbx_run(chan);
05493 if (res) {
05494 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05495 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05496 }
05497 return NULL;
05498 } else {
05499 if (option_verbose > 2)
05500 ast_verbose(VERBOSE_PREFIX_2 "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
05501 sleep(2);
05502 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_INFO);
05503 if (res < 0)
05504 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
05505 else
05506 sleep(1);
05507 res = ast_streamfile(chan, "ss-noservice", chan->language);
05508 if (res >= 0)
05509 ast_waitstream(chan, "");
05510 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05511 ast_hangup(chan);
05512 return NULL;
05513 }
05514 break;
05515 case SIG_FXOLS:
05516 case SIG_FXOGS:
05517 case SIG_FXOKS:
05518
05519 timeout = firstdigittimeout;
05520
05521
05522 if (p->subs[SUB_THREEWAY].owner)
05523 timeout = 999999;
05524 while(len < AST_MAX_EXTENSION-1) {
05525
05526
05527 if (p->immediate)
05528 res = 's';
05529 else
05530 res = ast_waitfordigit(chan, timeout);
05531 timeout = 0;
05532 if (res < 0) {
05533 ast_log(LOG_DEBUG, "waitfordigit returned < 0...\n");
05534 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05535 ast_hangup(chan);
05536 return NULL;
05537 } else if (res) {
05538 exten[len++]=res;
05539 exten[len] = '\0';
05540 }
05541 if (!ast_ignore_pattern(chan->context, exten))
05542 tone_zone_play_tone(p->subs[index].zfd, -1);
05543 else
05544 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05545 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
05546 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
05547 if (getforward) {
05548
05549 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
05550 if (option_verbose > 2)
05551 ast_verbose(VERBOSE_PREFIX_3 "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
05552 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05553 if (res)
05554 break;
05555 usleep(500000);
05556 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05557 sleep(1);
05558 memset(exten, 0, sizeof(exten));
05559 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALTONE);
05560 len = 0;
05561 getforward = 0;
05562 } else {
05563 res = tone_zone_play_tone(p->subs[index].zfd, -1);
05564 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
05565 if (!ast_strlen_zero(p->cid_num)) {
05566 if (!p->hidecallerid)
05567 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
05568 else
05569 ast_set_callerid(chan, NULL, NULL, p->cid_num);
05570 }
05571 if (!ast_strlen_zero(p->cid_name)) {
05572 if (!p->hidecallerid)
05573 ast_set_callerid(chan, NULL, p->cid_name, NULL);
05574 }
05575 ast_setstate(chan, AST_STATE_RING);
05576 zt_enable_ec(p);
05577 res = ast_pbx_run(chan);
05578 if (res) {
05579 ast_log(LOG_WARNING, "PBX exited non-zero\n");
05580 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05581 }
05582 return NULL;
05583 }
05584 } else {
05585
05586
05587 timeout = matchdigittimeout;
05588 }
05589 } else if (res == 0) {
05590 ast_log(LOG_DEBUG, "not enough digits (and no ambiguous match)...\n");
05591 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05592 zt_wait_event(p->subs[index].zfd);
05593 ast_hangup(chan);
05594 return NULL;
05595 } else if (p->callwaiting && !strcmp(exten, "*70")) {
05596 if (option_verbose > 2)
05597 ast_verbose(VERBOSE_PREFIX_3 "Disabling call waiting on %s\n", chan->name);
05598
05599 p->callwaiting = 0;
05600 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05601 if (res) {
05602 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05603 chan->name, strerror(errno));
05604 }
05605 len = 0;
05606 ioctl(p->subs[index].zfd,ZT_CONFDIAG,&len);
05607 memset(exten, 0, sizeof(exten));
05608 timeout = firstdigittimeout;
05609
05610 } else if (!strcmp(exten,ast_pickup_ext())) {
05611
05612
05613
05614
05615 if (index == SUB_REAL) {
05616
05617 if (p->subs[SUB_THREEWAY].owner) {
05618
05619
05620 alloc_sub(p, SUB_CALLWAIT);
05621 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
05622 unalloc_sub(p, SUB_THREEWAY);
05623 }
05624 zt_enable_ec(p);
05625 if (ast_pickup_call(chan)) {
05626 ast_log(LOG_DEBUG, "No call pickup possible...\n");
05627 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05628 zt_wait_event(p->subs[index].zfd);
05629 }
05630 ast_hangup(chan);
05631 return NULL;
05632 } else {
05633 ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
05634 ast_hangup(chan);
05635 return NULL;
05636 }
05637
05638 } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
05639 if (option_verbose > 2)
05640 ast_verbose(VERBOSE_PREFIX_3 "Disabling Caller*ID on %s\n", chan->name);
05641
05642 p->hidecallerid = 1;
05643 if (chan->cid.cid_num)
05644 free(chan->cid.cid_num);
05645 chan->cid.cid_num = NULL;
05646 if (chan->cid.cid_name)
05647 free(chan->cid.cid_name);
05648 chan->cid.cid_name = NULL;
05649 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05650 if (res) {
05651 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05652 chan->name, strerror(errno));
05653 }
05654 len = 0;
05655 memset(exten, 0, sizeof(exten));
05656 timeout = firstdigittimeout;
05657 } else if (p->callreturn && !strcmp(exten, "*69")) {
05658 res = 0;
05659 if (!ast_strlen_zero(p->lastcid_num)) {
05660 res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
05661 }
05662 if (!res)
05663 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05664 break;
05665 } else if (!strcmp(exten, "*78")) {
05666
05667 if (option_verbose > 2) {
05668 ast_verbose(VERBOSE_PREFIX_3 "Enabled DND on channel %d\n", p->channel);
05669 }
05670 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05671 "Channel: Zap/%d\r\n"
05672 "Status: enabled\r\n", p->channel);
05673 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05674 p->dnd = 1;
05675 getforward = 0;
05676 memset(exten, 0, sizeof(exten));
05677 len = 0;
05678 } else if (!strcmp(exten, "*79")) {
05679
05680 if (option_verbose > 2)
05681 ast_verbose(VERBOSE_PREFIX_3 "Disabled DND on channel %d\n", p->channel);
05682 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
05683 "Channel: Zap/%d\r\n"
05684 "Status: disabled\r\n", p->channel);
05685 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05686 p->dnd = 0;
05687 getforward = 0;
05688 memset(exten, 0, sizeof(exten));
05689 len = 0;
05690 } else if (p->cancallforward && !strcmp(exten, "*72")) {
05691 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05692 getforward = 1;
05693 memset(exten, 0, sizeof(exten));
05694 len = 0;
05695 } else if (p->cancallforward && !strcmp(exten, "*73")) {
05696 if (option_verbose > 2)
05697 ast_verbose(VERBOSE_PREFIX_3 "Cancelling call forwarding on channel %d\n", p->channel);
05698 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05699 memset(p->call_forward, 0, sizeof(p->call_forward));
05700 getforward = 0;
05701 memset(exten, 0, sizeof(exten));
05702 len = 0;
05703 } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
05704 p->subs[SUB_THREEWAY].owner &&
05705 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
05706
05707
05708 ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL);
05709 if (option_verbose > 2)
05710 ast_verbose(VERBOSE_PREFIX_3 "Parking call to '%s'\n", chan->name);
05711 break;
05712 } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
05713 if (option_verbose > 2)
05714 ast_verbose(VERBOSE_PREFIX_3 "Blacklisting number %s\n", p->lastcid_num);
05715 res = ast_db_put("blacklist", p->lastcid_num, "1");
05716 if (!res) {
05717 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05718 memset(exten, 0, sizeof(exten));
05719 len = 0;
05720 }
05721 } else if (p->hidecallerid && !strcmp(exten, "*82")) {
05722 if (option_verbose > 2)
05723 ast_verbose(VERBOSE_PREFIX_3 "Enabling Caller*ID on %s\n", chan->name);
05724
05725 p->hidecallerid = 0;
05726 if (chan->cid.cid_num)
05727 free(chan->cid.cid_num);
05728 chan->cid.cid_num = NULL;
05729 if (chan->cid.cid_name)
05730 free(chan->cid.cid_name);
05731 chan->cid.cid_name = NULL;
05732 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
05733 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_DIALRECALL);
05734 if (res) {
05735 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
05736 chan->name, strerror(errno));
05737 }
05738 len = 0;
05739 memset(exten, 0, sizeof(exten));
05740 timeout = firstdigittimeout;
05741 } else if (!strcmp(exten, "*0")) {
05742 struct ast_channel *nbridge =
05743 p->subs[SUB_THREEWAY].owner;
05744 struct zt_pvt *pbridge = NULL;
05745
05746 if (nbridge && ast_bridged_channel(nbridge))
05747 pbridge = ast_bridged_channel(nbridge)->tech_pvt;
05748 if (nbridge && pbridge &&
05749 (!strcmp(nbridge->type,"Zap")) &&
05750 (!strcmp(ast_bridged_channel(nbridge)->type, "Zap")) &&
05751 ISTRUNK(pbridge)) {
05752 int func = ZT_FLASH;
05753
05754 p->dop.dialstr[0] = '\0';
05755
05756 if ((ioctl(pbridge->subs[SUB_REAL].zfd,ZT_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
05757 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
05758 nbridge->name, strerror(errno));
05759 }
05760 swap_subs(p, SUB_REAL, SUB_THREEWAY);
05761 unalloc_sub(p, SUB_THREEWAY);
05762 p->owner = p->subs[SUB_REAL].owner;
05763 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
05764 ast_moh_stop(ast_bridged_channel(p->subs[SUB_REAL].owner));
05765 ast_hangup(chan);
05766 return NULL;
05767 } else {
05768 tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
05769 zt_wait_event(p->subs[index].zfd);
05770 tone_zone_play_tone(p->subs[index].zfd, -1);
05771 swap_subs(p, SUB_REAL, SUB_THREEWAY);
05772 unalloc_sub(p, SUB_THREEWAY);
05773 p->owner = p->subs[SUB_REAL].owner;
05774 ast_hangup(chan);
05775 return NULL;
05776 }
05777 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
05778 ((exten[0] != '*') || (strlen(exten) > 2))) {
05779 if (option_debug)
05780 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);
05781 break;
05782 }
05783 if (!timeout)
05784 timeout = gendigittimeout;
05785 if (len && !ast_ignore_pattern(chan->context, exten))
05786 tone_zone_play_tone(p->subs[index].zfd, -1);
05787 }
05788 break;
05789 case SIG_FXSLS:
05790 case SIG_FXSGS:
05791 case SIG_FXSKS:
05792 #ifdef ZAPATA_PRI
05793 if (p->pri) {
05794
05795 struct ast_frame *f;
05796 int res;
05797 time_t start;
05798
05799 time(&start);
05800 ast_setstate(chan, AST_STATE_RING);
05801 while(time(NULL) < start + 3) {
05802 res = ast_waitfor(chan, 1000);
05803 if (res) {
05804 f = ast_read(chan);
05805 if (!f) {
05806 ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
05807 ast_hangup(chan);
05808 return NULL;
05809 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
05810 res = 1;
05811 } else
05812 res = 0;
05813 ast_frfree(f);
05814 if (res) {
05815 ast_log(LOG_DEBUG, "Got ring!\n");
05816 res = 0;
05817 break;
05818 }
05819 }
05820 }
05821 }
05822 #endif
05823
05824
05825
05826 if (p->use_callerid && (chan->_state == AST_STATE_PRERING && p->cid_start == CID_START_POLARITY)) {
05827
05828 if (p->cid_signalling == CID_SIG_DTMF) {
05829 int i = 0;
05830 cs = NULL;
05831 ast_log(LOG_DEBUG, "Receiving DTMF cid on "
05832 "channel %s\n", chan->name);
05833 zt_setlinear(p->subs[index].zfd, 0);
05834 res = 2000;
05835 for (;;) {
05836 struct ast_frame *f;
05837 res = ast_waitfor(chan, res);
05838 if (res <= 0) {
05839 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
05840 "Exiting simple switch\n");
05841 ast_hangup(chan);
05842 return NULL;
05843 }
05844 f = ast_read(chan);
05845 if (f->frametype == AST_FRAME_DTMF) {
05846 dtmfbuf[i++] = f->subclass;
05847 ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
05848 res = 2000;
05849 }
05850 ast_frfree(f);
05851 if (chan->_state == AST_STATE_RING ||
05852 chan->_state == AST_STATE_RINGING)
05853 break;
05854 }
05855 dtmfbuf[i] = 0;
05856 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
05857
05858 ast_log(LOG_DEBUG, "CID got string '%s'\n", dtmfbuf);
05859 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
05860 ast_log(LOG_DEBUG, "CID is '%s', flags %d\n",
05861 dtmfcid, flags);
05862
05863 if (dtmfcid[0])
05864 number = dtmfcid;
05865 else
05866 number = 0;
05867
05868 } else if (p->cid_signalling == CID_SIG_V23) {
05869 cs = callerid_new(p->cid_signalling);
05870 if (cs) {
05871 samples = 0;
05872 #if 1
05873 bump_gains(p);
05874 #endif
05875
05876 zt_setlinear(p->subs[index].zfd, 0);
05877
05878
05879 for(;;) {
05880 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
05881 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
05882 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
05883 callerid_free(cs);
05884 ast_hangup(chan);
05885 return NULL;
05886 }
05887 if (i & ZT_IOMUX_SIGEVENT) {
05888 res = zt_get_event(p->subs[index].zfd);
05889 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
05890 res = 0;
05891 break;
05892 } else if (i & ZT_IOMUX_READ) {
05893 res = read(p->subs[index].zfd, buf, sizeof(buf));
05894 if (res < 0) {
05895 if (errno != ELAST) {
05896 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
05897 callerid_free(cs);
05898 ast_hangup(chan);
05899 return NULL;
05900 }
05901 break;
05902 }
05903 samples += res;
05904 res = callerid_feed(cs, buf, res, AST_LAW(p));
05905 if (res < 0) {
05906 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
05907 break;
05908 } else if (res)
05909 break;
05910 else if (samples > (8000 * 10))
05911 break;
05912 }
05913 }
05914 if (res == 1) {
05915 callerid_get(cs, &name, &number, &flags);
05916 if (option_debug)
05917 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
05918 }
05919 if (res < 0) {
05920 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
05921 }
05922
05923
05924 res = 2000;
05925 for (;;) {
05926 struct ast_frame *f;
05927 res = ast_waitfor(chan, res);
05928 if (res <= 0) {
05929 ast_log(LOG_WARNING, "CID timed out waiting for ring. "
05930 "Exiting simple switch\n");
05931 ast_hangup(chan);
05932 return NULL;
05933 }
05934 f = ast_read(chan);
05935 ast_frfree(f);
05936 if (chan->_state == AST_STATE_RING ||
05937 chan->_state == AST_STATE_RINGING)
05938 break;
05939 }
05940
05941
05942
05943 if (p->usedistinctiveringdetection == 1) {
05944 len = 0;
05945 distMatches = 0;
05946
05947 for (receivedRingT=0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
05948 curRingData[receivedRingT] = 0;
05949 receivedRingT = 0;
05950 counter = 0;
05951 counter1 = 0;
05952
05953 if (strcmp(p->context,p->defcontext) != 0) {
05954 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
05955 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
05956 }
05957
05958 for(;;) {
05959 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
05960 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
05961 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
05962 callerid_free(cs);
05963 ast_hangup(chan);
05964 return NULL;
05965 }
05966 if (i & ZT_IOMUX_SIGEVENT) {
05967 res = zt_get_event(p->subs[index].zfd);
05968 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
05969 res = 0;
05970
05971
05972 curRingData[receivedRingT] = p->ringt;
05973
05974 if (p->ringt < p->ringt_base/2)
05975 break;
05976
05977
05978 if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
05979 break;
05980 } else if (i & ZT_IOMUX_READ) {
05981 res = read(p->subs[index].zfd, buf, sizeof(buf));
05982 if (res < 0) {
05983 if (errno != ELAST) {
05984 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
05985 callerid_free(cs);
05986 ast_hangup(chan);
05987 return NULL;
05988 }
05989 break;
05990 }
05991 if (p->ringt)
05992 p->ringt--;
05993 if (p->ringt == 1) {
05994 res = -1;
05995 break;
05996 }
05997 }
05998 }
05999 if(option_verbose > 2)
06000
06001 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06002
06003 for (counter=0; counter < 3; counter++) {
06004
06005
06006 distMatches = 0;
06007 for (counter1=0; counter1 < 3; counter1++) {
06008 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06009 (p->drings.ringnum[counter].ring[counter1]-10)) {
06010 distMatches++;
06011 }
06012 }
06013 if (distMatches == 3) {
06014
06015 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06016 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06017 if(option_verbose > 2)
06018 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06019 break;
06020 }
06021 }
06022 }
06023
06024 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06025 #if 1
06026 restore_gains(p);
06027 #endif
06028 } else
06029 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06030 } else {
06031 ast_log(LOG_WARNING, "Channel %s in prering "
06032 "state, but I have nothing to do. "
06033 "Terminating simple switch, should be "
06034 "restarted by the actual ring.\n",
06035 chan->name);
06036 ast_hangup(chan);
06037 return NULL;
06038 }
06039 } else if (p->use_callerid && p->cid_start == CID_START_RING) {
06040
06041 cs = callerid_new(p->cid_signalling);
06042 if (cs) {
06043 #if 1
06044 bump_gains(p);
06045 #endif
06046 samples = 0;
06047 len = 0;
06048 distMatches = 0;
06049
06050 for (receivedRingT=0; receivedRingT < (sizeof(curRingData) / sizeof(curRingData[0])); receivedRingT++)
06051 curRingData[receivedRingT] = 0;
06052 receivedRingT = 0;
06053 counter = 0;
06054 counter1 = 0;
06055
06056 if (strcmp(p->context,p->defcontext) != 0) {
06057 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
06058 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
06059 }
06060
06061
06062 zt_setlinear(p->subs[index].zfd, 0);
06063 for(;;) {
06064 i = ZT_IOMUX_READ | ZT_IOMUX_SIGEVENT;
06065 if ((res = ioctl(p->subs[index].zfd, ZT_IOMUX, &i))) {
06066 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
06067 callerid_free(cs);
06068 ast_hangup(chan);
06069 return NULL;
06070 }
06071 if (i & ZT_IOMUX_SIGEVENT) {
06072 res = zt_get_event(p->subs[index].zfd);
06073 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
06074 res = 0;
06075
06076
06077 curRingData[receivedRingT] = p->ringt;
06078
06079 if (p->ringt < p->ringt_base/2)
06080 break;
06081
06082
06083 if (++receivedRingT == (sizeof(curRingData) / sizeof(curRingData[0])))
06084 break;
06085 } else if (i & ZT_IOMUX_READ) {
06086 res = read(p->subs[index].zfd, buf, sizeof(buf));
06087 if (res < 0) {
06088 if (errno != ELAST) {
06089 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
06090 callerid_free(cs);
06091 ast_hangup(chan);
06092 return NULL;
06093 }
06094 break;
06095 }
06096 if (p->ringt)
06097 p->ringt--;
06098 if (p->ringt == 1) {
06099 res = -1;
06100 break;
06101 }
06102 samples += res;
06103 res = callerid_feed(cs, buf, res, AST_LAW(p));
06104 if (res < 0) {
06105 ast_log(LOG_WARNING, "CallerID feed failed: %s\n", strerror(errno));
06106 break;
06107 } else if (res)
06108 break;
06109 else if (samples > (8000 * 10))
06110 break;
06111 }
06112 }
06113 if (p->usedistinctiveringdetection == 1) {
06114 if(option_verbose > 2)
06115
06116 ast_verbose( VERBOSE_PREFIX_3 "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
06117
06118 for (counter=0; counter < 3; counter++) {
06119
06120
06121 distMatches = 0;
06122 for (counter1=0; counter1 < 3; counter1++) {
06123 if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1]+10) && curRingData[counter1] >=
06124 (p->drings.ringnum[counter].ring[counter1]-10)) {
06125 distMatches++;
06126 }
06127 }
06128 if (distMatches == 3) {
06129
06130 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
06131 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
06132 if(option_verbose > 2)
06133 ast_verbose( VERBOSE_PREFIX_3 "Distinctive Ring matched context %s\n",p->context);
06134 break;
06135 }
06136 }
06137 }
06138 if (res == 1) {
06139 callerid_get(cs, &name, &number, &flags);
06140 if (option_debug)
06141 ast_log(LOG_DEBUG, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
06142 }
06143
06144 zt_setlinear(p->subs[index].zfd, p->subs[index].linear);
06145 #if 1
06146 restore_gains(p);
06147 #endif
06148 if (res < 0) {
06149 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
06150 }
06151 } else
06152 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
06153 }
06154 else
06155 cs = NULL;
06156 if (number || name) {
06157 if (chan->cid.cid_num) {
06158 free(chan->cid.cid_num);
06159 chan->cid.cid_num = NULL;
06160 }
06161 if (chan->cid.cid_name) {
06162 free(chan->cid.cid_name);
06163 chan->cid.cid_name = NULL;
06164 }
06165 }
06166 if (number)
06167 ast_shrink_phone_number(number);
06168
06169 ast_set_callerid(chan, number, name, number);
06170
06171 if (cs)
06172 callerid_free(cs);
06173 ast_setstate(chan, AST_STATE_RING);
06174 chan->rings = 1;
06175 p->ringt = p->ringt_base;
06176 res = ast_pbx_run(chan);
06177 if (res) {
06178 ast_hangup(chan);
06179 ast_log(LOG_WARNING, "PBX exited non-zero\n");
06180 }
06181 return NULL;
06182 default:
06183 ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
06184 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06185 if (res < 0)
06186 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06187 }
06188 res = tone_zone_play_tone(p->subs[index].zfd, ZT_TONE_CONGESTION);
06189 if (res < 0)
06190 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
06191 ast_hangup(chan);
06192 return NULL;
06193 }
06194
06195 #ifdef ZAPATA_R2
06196 static int handle_init_r2_event(struct zt_pvt *i, mfcr2_event_t *e)
06197 {
06198 struct ast_channel *chan;
06199
06200 switch(e->e) {
06201 case MFCR2_EVENT_UNBLOCKED:
06202 i->r2blocked = 0;
06203 if (option_verbose > 2)
06204 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
06205 break;
06206 case MFCR2_EVENT_BLOCKED:
06207 i->r2blocked = 1;
06208 if (option_verbose > 2)
06209 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d unblocked\n", i->channel);
06210 break;
06211 case MFCR2_EVENT_IDLE:
06212 if (option_verbose > 2)
06213 ast_verbose(VERBOSE_PREFIX_3 "R2 Channel %d idle\n", i->channel);
06214 break;
06215 case MFCR2_EVENT_RINGING:
06216
06217
06218
06219 i->hasr2call = 1;
06220 chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06221 if (!chan) {
06222 ast_log(LOG_WARNING, "Unable to create channel for channel %d\n", i->channel);
06223 mfcr2_DropCall(i->r2, NULL, UC_NETWORK_CONGESTION);
06224 i->hasr2call = 0;
06225 }
06226 if (ast_pbx_start(chan)) {
06227 ast_log(LOG_WARNING, "Unable to start PBX on channel %s\n", chan->name);
06228 ast_hangup(chan);
06229 }
06230 break;
06231 default:
06232 ast_log(LOG_WARNING, "Don't know how to handle initial R2 event %s on channel %d\n", mfcr2_event2str(e->e), i->channel);
06233 return -1;
06234 }
06235 return 0;
06236 }
06237 #endif
06238
06239 static int handle_init_event(struct zt_pvt *i, int event)
06240 {
06241 int res;
06242 pthread_t threadid;
06243 pthread_attr_t attr;
06244 struct ast_channel *chan;
06245 pthread_attr_init(&attr);
06246 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06247
06248 switch(event) {
06249 case ZT_EVENT_NONE:
06250 case ZT_EVENT_BITSCHANGED:
06251 if (i->radio) break;
06252 #ifdef ZAPATA_R2
06253 if (i->r2) {
06254 mfcr2_event_t *e;
06255 e = r2_get_event_bits(i);
06256 i->sigchecked = 1;
06257 if (e)
06258 handle_init_r2_event(i, e);
06259 }
06260 #endif
06261 break;
06262 case ZT_EVENT_WINKFLASH:
06263 case ZT_EVENT_RINGOFFHOOK:
06264 if (i->inalarm) break;
06265 if (i->radio) break;
06266
06267 switch(i->sig) {
06268 case SIG_FXOLS:
06269 case SIG_FXOGS:
06270 case SIG_FXOKS:
06271 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06272 if (i->cidspill) {
06273
06274 free(i->cidspill);
06275 i->cidspill = NULL;
06276 }
06277 if (i->immediate) {
06278 zt_enable_ec(i);
06279
06280 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_RINGTONE);
06281 chan = zt_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
06282 if (!chan) {
06283 ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
06284 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06285 if (res < 0)
06286 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06287 }
06288 } else {
06289
06290 chan = zt_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
06291 if (chan) {
06292 if (has_voicemail(i))
06293 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_STUTTER);
06294 else
06295 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_DIALTONE);
06296 if (res < 0)
06297 ast_log(LOG_WARNING, "Unable to play dialtone on channel %d\n", i->channel);
06298 if (ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06299 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06300 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06301 if (res < 0)
06302 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06303 ast_hangup(chan);
06304 }
06305 } else
06306 ast_log(LOG_WARNING, "Unable to create channel\n");
06307 }
06308 break;
06309 case SIG_FXSLS:
06310 case SIG_FXSGS:
06311 case SIG_FXSKS:
06312 i->ringt = i->ringt_base;
06313
06314 case SIG_EMWINK:
06315 case SIG_FEATD:
06316 case SIG_FEATDMF:
06317 case SIG_E911:
06318 case SIG_FEATB:
06319 case SIG_EM:
06320 case SIG_EM_E1:
06321 case SIG_SFWINK:
06322 case SIG_SF_FEATD:
06323 case SIG_SF_FEATDMF:
06324 case SIG_SF_FEATB:
06325 case SIG_SF:
06326
06327 chan = zt_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
06328 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06329 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06330 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06331 if (res < 0)
06332 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06333 ast_hangup(chan);
06334 } else if (!chan) {
06335 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
06336 }
06337 break;
06338 default:
06339 ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06340 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, ZT_TONE_CONGESTION);
06341 if (res < 0)
06342 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
06343 return -1;
06344 }
06345 break;
06346 case ZT_EVENT_NOALARM:
06347 i->inalarm = 0;
06348 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
06349 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
06350 "Channel: %d\r\n", i->channel);
06351 break;
06352 case ZT_EVENT_ALARM:
06353 i->inalarm = 1;
06354 res = get_alarms(i);
06355 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", i->channel, alarm2str(res));
06356 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
06357 "Alarm: %s\r\n"
06358 "Channel: %d\r\n",
06359 alarm2str(res), i->channel);
06360
06361 case ZT_EVENT_ONHOOK:
06362 if (i->radio) break;
06363
06364 switch(i->sig) {
06365 case SIG_FXOLS:
06366 case SIG_FXOGS:
06367 case SIG_FEATD:
06368 case SIG_FEATDMF:
06369 case SIG_E911:
06370 case SIG_FEATB:
06371 case SIG_EM:
06372 case SIG_EM_E1:
06373 case SIG_EMWINK:
06374 case SIG_SF_FEATD:
06375 case SIG_SF_FEATDMF:
06376 case SIG_SF_FEATB:
06377 case SIG_SF:
06378 case SIG_SFWINK:
06379 case SIG_FXSLS:
06380 case SIG_FXSGS:
06381 case SIG_FXSKS:
06382 case SIG_GR303FXSKS:
06383 zt_disable_ec(i);
06384 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06385 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06386 break;
06387 case SIG_GR303FXOKS:
06388 case SIG_FXOKS:
06389 zt_disable_ec(i);
06390
06391 #ifdef ZHONE_HACK
06392 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_OFFHOOK);
06393 usleep(1);
06394 #endif
06395 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06396 zt_set_hook(i->subs[SUB_REAL].zfd, ZT_ONHOOK);
06397 break;
06398 case SIG_PRI:
06399 zt_disable_ec(i);
06400 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06401 break;
06402 default:
06403 ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
06404 res = tone_zone_play_tone(i->subs[SUB_REAL].zfd, -1);
06405 return -1;
06406 }
06407 break;
06408 case ZT_EVENT_POLARITY:
06409 switch(i->sig) {
06410 case SIG_FXSLS:
06411 case SIG_FXSKS:
06412 case SIG_FXSGS:
06413 if (i->cid_start == CID_START_POLARITY) {
06414 i->polarity = POLARITY_REV;
06415 ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
06416 "CID detection on channel %d\n",
06417 i->channel);
06418 chan = zt_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
06419 if (chan && ast_pthread_create(&threadid, &attr, ss_thread, chan)) {
06420 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
06421 }
06422 }
06423 break;
06424 default:
06425 ast_log(LOG_WARNING, "handle_init_event detected "
06426 "polarity reversal on non-FXO (SIG_FXS) "
06427 "interface %d\n", i->channel);
06428 }
06429 }
06430 return 0;
06431 }
06432
06433 static void *do_monitor(void *data)
06434 {
06435 int count, res, res2, spoint, pollres=0;
06436 struct zt_pvt *i;
06437 struct zt_pvt *last = NULL;
06438 time_t thispass = 0, lastpass = 0;
06439 int found;
06440 char buf[1024];
06441 struct pollfd *pfds=NULL;
06442 int lastalloc = -1;
06443
06444
06445
06446 #if 0
06447 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
06448 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
06449 return NULL;
06450 }
06451 ast_log(LOG_DEBUG, "Monitor starting...\n");
06452 #endif
06453 for(;;) {
06454
06455 if (ast_mutex_lock(&iflock)) {
06456 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
06457 return NULL;
06458 }
06459 if (!pfds || (lastalloc != ifcount)) {
06460 if (pfds)
06461 free(pfds);
06462 if (ifcount) {
06463 pfds = malloc(ifcount * sizeof(struct pollfd));
06464 if (!pfds) {
06465 ast_log(LOG_WARNING, "Critical memory error. Zap dies.\n");
06466 ast_mutex_unlock(&iflock);
06467 return NULL;
06468 }
06469 }
06470 lastalloc = ifcount;
06471 }
06472
06473
06474 count = 0;
06475 i = iflist;
06476 while(i) {
06477 if ((i->subs[SUB_REAL].zfd > -1) && i->sig && (!i->radio)) {
06478 if (!i->owner && !i->subs[SUB_REAL].owner) {
06479
06480 pfds[count].fd = i->subs[SUB_REAL].zfd;
06481 pfds[count].events = POLLPRI;
06482 pfds[count].revents = 0;
06483
06484 #ifdef ZAPATA_R2
06485 if (i->cidspill || i->r2)
06486 #else
06487 if (i->cidspill)
06488 #endif
06489 pfds[count].events |= POLLIN;
06490 count++;
06491 }
06492 }
06493 i = i->next;
06494 }
06495
06496 ast_mutex_unlock(&iflock);
06497
06498 pthread_testcancel();
06499
06500 res = poll(pfds, count, 1000);
06501 pthread_testcancel();
06502
06503 if (res < 0) {
06504 if ((errno != EAGAIN) && (errno != EINTR))
06505 ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
06506 continue;
06507 }
06508
06509
06510 if (ast_mutex_lock(&iflock)) {
06511 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
06512 continue;
06513 }
06514 found = 0;
06515 spoint = 0;
06516 lastpass = thispass;
06517 thispass = time(NULL);
06518 i = iflist;
06519 while(i) {
06520 if (thispass != lastpass) {
06521 if (!found && ((i == last) || ((i == iflist) && !last))) {
06522 last = i;
06523 if (last) {
06524 if (!last->cidspill && !last->owner && !ast_strlen_zero(last->mailbox) && (thispass - last->onhooktime > 3) &&
06525 (last->sig & __ZT_SIG_FXO)) {
06526 res = ast_app_has_voicemail(last->mailbox, NULL);
06527 if (last->msgstate != res) {
06528 int x;
06529 ast_log(LOG_DEBUG, "Message status for %s changed from %d to %d on %d\n", last->mailbox, last->msgstate, res, last->channel);
06530 x = ZT_FLUSH_BOTH;
06531 res2 = ioctl(last->subs[SUB_REAL].zfd, ZT_FLUSH, &x);
06532 if (res2)
06533 ast_log(LOG_WARNING, "Unable to flush input on channel %d\n", last->channel);
06534 last->cidspill = malloc(MAX_CALLERID_SIZE);
06535 if (last->cidspill) {
06536
06537 x = 4000;
06538 ioctl(last->subs[SUB_REAL].zfd, ZT_ONHOOKTRANSFER, &x);
06539 last->cidlen = vmwi_generate(last->cidspill, res, 1, AST_LAW(last));
06540 last->cidpos = 0;
06541 last->msgstate = res;
06542 last->onhooktime = thispass;
06543 }
06544 found ++;
06545 }
06546 }
06547 last = last->next;
06548 }
06549 }
06550 }
06551 if ((i->subs[SUB_REAL].zfd > -1) && i->sig) {
06552 if (i->radio && !i->owner)
06553 {
06554 res = zt_get_event(i->subs[SUB_REAL].zfd);
06555 if (res)
06556 {
06557 if (option_debug)
06558 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
06559
06560 ast_mutex_unlock(&iflock);
06561 handle_init_event(i, res);
06562 ast_mutex_lock(&iflock);
06563 }
06564 i = i->next;
06565 continue;
06566 }
06567 pollres = ast_fdisset(pfds, i->subs[SUB_REAL].zfd, count, &spoint);
06568 if (pollres & POLLIN) {
06569 if (i->owner || i->subs[SUB_REAL].owner) {
06570 #ifdef ZAPATA_PRI
06571 if (!i->pri)
06572 #endif
06573 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].zfd);
06574 i = i->next;
06575 continue;
06576 }
06577 #ifdef ZAPATA_R2
06578 if (i->r2) {
06579
06580 mfcr2_event_t *e;
06581 e = mfcr2_check_event(i->r2);
06582 if (e)
06583 handle_init_r2_event(i, e);
06584 else {
06585 e = mfcr2_schedule_run(i->r2);
06586 if (e)
06587 handle_init_r2_event(i, e);
06588 }
06589 i = i->next;
06590 continue;
06591 }
06592 #endif
06593 if (!i->cidspill) {
06594 ast_log(LOG_WARNING, "Whoa.... I'm reading but have no cidspill (%d)...\n", i->subs[SUB_REAL].zfd);
06595 i = i->next;
06596 continue;
06597 }
06598 res = read(i->subs[SUB_REAL].zfd, buf, sizeof(buf));
06599 if (res > 0) {
06600
06601 if (res > i->cidlen - i->cidpos)
06602 res = i->cidlen - i->cidpos;
06603 res2 = write(i->subs[SUB_REAL].zfd, i->cidspill + i->cidpos, res);
06604 if (res2 > 0) {
06605 i->cidpos += res2;
06606 if (i->cidpos >= i->cidlen) {
06607 free(i->cidspill);
06608 i->cidspill = 0;
06609 i->cidpos = 0;
06610 i->cidlen = 0;
06611 }
06612 } else {
06613 ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
06614 i->msgstate = -1;
06615 }
06616 } else {
06617 ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
06618 }
06619 if (option_debug)
06620 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06621
06622 ast_mutex_unlock(&iflock);
06623 handle_init_event(i, res);
06624 ast_mutex_lock(&iflock);
06625 }
06626 #ifdef ZAPATA_R2
06627 if ((pollres & POLLPRI) || (i->r2 && !i->sigchecked))
06628 #else
06629 if (pollres & POLLPRI)
06630 #endif
06631 {
06632 if (i->owner || i->subs[SUB_REAL].owner) {
06633 #ifdef ZAPATA_PRI
06634 if (!i->pri)
06635 #endif
06636 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].zfd);
06637 i = i->next;
06638 continue;
06639 }
06640 res = zt_get_event(i->subs[SUB_REAL].zfd);
06641 if (option_debug)
06642 ast_log(LOG_DEBUG, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
06643
06644 ast_mutex_unlock(&iflock);
06645 handle_init_event(i, res);
06646 ast_mutex_lock(&iflock);
06647 }
06648 }
06649 i=i->next;
06650 }
06651 ast_mutex_unlock(&iflock);
06652 }
06653
06654 return NULL;
06655
06656 }
06657
06658 static int restart_monitor(void)
06659 {
06660 pthread_attr_t attr;
06661 pthread_attr_init(&attr);
06662 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
06663
06664 if (monitor_thread == AST_PTHREADT_STOP)
06665 return 0;
06666 if (ast_mutex_lock(&monlock)) {
06667 ast_log(LOG_WARNING, "Unable to lock monitor\n");
06668 return -1;
06669 }
06670 if (monitor_thread == pthread_self()) {
06671 ast_mutex_unlock(&monlock);
06672 ast_log(LOG_WARNING, "Cannot kill myself\n");
06673 return -1;
06674 }
06675 if (monitor_thread != AST_PTHREADT_NULL) {
06676
06677 #if 0
06678 pthread_cancel(monitor_thread);
06679 #endif
06680 pthread_kill(monitor_thread, SIGURG);
06681 #if 0
06682 pthread_join(monitor_thread, NULL);
06683 #endif
06684 } else {
06685
06686 if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
06687 ast_mutex_unlock(&monlock);
06688 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
06689 return -1;
06690 }
06691 }
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
07844 ast_hangup(chan);
07845 return NULL;
07846 }
07847
07848 #ifndef PRI_RESTART
07849 #error "Upgrade your libpri"
07850 #endif
07851 static void zt_pri_message(struct pri *pri, char *s)
07852 {
07853 int x, y;
07854 int dchan = -1, span = -1;
07855 int dchancount = 0;
07856
07857 if (pri) {
07858 for (x = 0; x < NUM_SPANS; x++) {
07859 for (y = 0; y < NUM_DCHANS; y++) {
07860 if (pris[x].dchans[y])
07861 dchancount++;
07862
07863 if (pris[x].dchans[y] == pri)
07864 dchan = y;
07865 }
07866 if (dchan >= 0) {
07867 span = x;
07868 break;
07869 }
07870 dchancount = 0;
07871 }
07872 if ((dchan >= 0) && (span >= 0)) {
07873 if (dchancount > 1)
07874 ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s);
07875 else
07876 ast_verbose("%s", s);
07877 } else
07878 ast_verbose("PRI debug error: could not find pri associated it with debug message output\n");
07879 } else
07880 ast_verbose("%s", s);
07881
07882 ast_mutex_lock(&pridebugfdlock);
07883
07884 if (pridebugfd >= 0)
07885 write(pridebugfd, s, strlen(s));
07886
07887 ast_mutex_unlock(&pridebugfdlock);
07888 }
07889
07890 static void zt_pri_error(struct pri *pri, char *s)
07891 {
07892 int x, y;
07893 int dchan = -1, span = -1;
07894 int dchancount = 0;
07895
07896 if (pri) {
07897 for (x = 0; x < NUM_SPANS; x++) {
07898 for (y = 0; y < NUM_DCHANS; y++) {
07899 if (pris[x].dchans[y])
07900 dchancount++;
07901
07902 if (pris[x].dchans[y] == pri)
07903 dchan = y;
07904 }
07905 if (dchan >= 0) {
07906 span = x;
07907 break;
07908 }
07909 dchancount = 0;
07910 }
07911 if ((dchan >= 0) && (span >= 0)) {
07912 if (dchancount > 1)
07913 ast_log(LOG_WARNING, "[Span %d D-Channel %d] PRI: %s", span, dchan, s);
07914 else
07915 ast_verbose("%s", s);
07916 } else
07917 ast_verbose("PRI debug error: could not find pri associated it with debug message output\n");
07918 } else
07919 ast_log(LOG_WARNING, "%s", s);
07920
07921 ast_mutex_lock(&pridebugfdlock);
07922
07923 if (pridebugfd >= 0)
07924 write(pridebugfd, s, strlen(s));
07925
07926 ast_mutex_unlock(&pridebugfdlock);
07927 }
07928
07929 static int pri_check_restart(struct zt_pri *pri)
07930 {
07931 do {
07932 pri->resetpos++;
07933 } while((pri->resetpos < pri->numchans) &&
07934 (!pri->pvts[pri->resetpos] ||
07935 pri->pvts[pri->resetpos]->call ||
07936 pri->pvts[pri->resetpos]->resetting));
07937 if (pri->resetpos < pri->numchans) {
07938
07939 pri->pvts[pri->resetpos]->resetting = 1;
07940 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
07941 } else {
07942 pri->resetting = 0;
07943 time(&pri->lastreset);
07944 }
07945 return 0;
07946 }
07947
07948 static int pri_hangup_all(struct zt_pvt *p, struct zt_pri *pri)
07949 {
07950 int x;
07951 int redo;
07952 ast_mutex_unlock(&pri->lock);
07953 ast_mutex_lock(&p->lock);
07954 do {
07955 redo = 0;
07956 for (x=0;x<3;x++) {
07957 while(p->subs[x].owner && ast_mutex_trylock(&p->subs[x].owner->lock)) {
07958 redo++;
07959 ast_mutex_unlock(&p->lock);
07960 usleep(1);
07961 ast_mutex_lock(&p->lock);
07962 }
07963 if (p->subs[x].owner) {
07964 ast_queue_hangup(p->subs[x].owner);
07965 ast_mutex_unlock(&p->subs[x].owner->lock);
07966 }
07967 }
07968 } while (redo);
07969 ast_mutex_unlock(&p->lock);
07970 ast_mutex_lock(&pri->lock);
07971 return 0;
07972 }
07973 char * redirectingreason2str(int redirectingreason)
07974 {
07975 switch (redirectingreason) {
07976 case 0:
07977 return "UNKNOWN";
07978 case 1:
07979 return "BUSY";
07980 case 2:
07981 return "NO_REPLY";
07982 case 0xF:
07983 return "UNCONDITIONAL";
07984 default:
07985 return "NOREDIRECT";
07986 }
07987 }
07988
07989 static void apply_plan_to_number(char *buf, size_t size, const struct zt_pri *pri, const char *number, const int plan)
07990 {
07991 switch (plan) {
07992 case PRI_INTERNATIONAL_ISDN:
07993 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
07994 break;
07995 case PRI_NATIONAL_ISDN:
07996 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
07997 break;
07998 case PRI_LOCAL_ISDN:
07999 snprintf(buf, size, "%s%s", pri->localprefix, number);
08000 break;
08001 case PRI_PRIVATE:
08002 snprintf(buf, size, "%s%s", pri->privateprefix, number);
08003 break;
08004 case PRI_UNKNOWN:
08005 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
08006 break;
08007 default:
08008 snprintf(buf, size, "%s", number);
08009 break;
08010 }
08011 }
08012
08013 static void *pri_dchannel(void *vpri)
08014 {
08015 struct zt_pri *pri = vpri;
08016 pri_event *e;
08017 struct pollfd fds[NUM_DCHANS];
08018 int res;
08019 int chanpos = 0;
08020 int x;
08021 int haveidles;
08022 int activeidles;
08023 int nextidle = -1;
08024 struct ast_channel *c;
08025 struct timeval tv, lowest, *next;
08026 struct timeval lastidle = { 0, 0 };
08027 int doidling=0;
08028 char *cc;
08029 char idlen[80];
08030 struct ast_channel *idle;
08031 pthread_t p;
08032 time_t t;
08033 int i, which=-1;
08034 int numdchans;
08035 int cause=0;
08036 struct zt_pvt *crv;
08037 pthread_t threadid;
08038 pthread_attr_t attr;
08039 char ani2str[6];
08040 char plancallingnum[256];
08041 char plancallingani[256];
08042 char calledtonstr[10];
08043
08044 pthread_attr_init(&attr);
08045 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
08046
08047 gettimeofday(&lastidle, NULL);
08048 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
08049
08050 cc = strchr(pri->idleext, '@');
08051 if (cc) {
08052 *cc = '\0';
08053 cc++;
08054 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
08055 #if 0
08056
08057 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
08058 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
08059 else
08060 #endif
08061 doidling = 1;
08062 } else
08063 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
08064 }
08065 for(;;) {
08066 for (i=0;i<NUM_DCHANS;i++) {
08067 if (!pri->dchannels[i])
08068 break;
08069 fds[i].fd = pri->fds[i];
08070 fds[i].events = POLLIN | POLLPRI;
08071 fds[i].revents = 0;
08072 }
08073 numdchans = i;
08074 time(&t);
08075 ast_mutex_lock(&pri->lock);
08076 if (pri->switchtype != PRI_SWITCH_GR303_TMC && (pri->resetinterval > 0)) {
08077 if (pri->resetting && pri_is_up(pri)) {
08078 if (pri->resetpos < 0)
08079 pri_check_restart(pri);
08080 } else {
08081 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
08082 pri->resetting = 1;
08083 pri->resetpos = -1;
08084 }
08085 }
08086 }
08087
08088 if (doidling && pri_is_up(pri)) {
08089 nextidle = -1;
08090 haveidles = 0;
08091 activeidles = 0;
08092 for (x=pri->numchans;x>=0;x--) {
08093 if (pri->pvts[x] && !pri->pvts[x]->owner &&
08094 !pri->pvts[x]->call) {
08095 if (haveidles < pri->minunused) {
08096 haveidles++;
08097 } else if (!pri->pvts[x]->resetting) {
08098 nextidle = x;
08099 break;
08100 }
08101 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
08102 activeidles++;
08103 }
08104 if (nextidle > -1) {
08105 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
08106
08107 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
08108 idle = zt_request("Zap", AST_FORMAT_ULAW, idlen, &cause);
08109 if (idle) {
08110 pri->pvts[nextidle]->isidlecall = 1;
08111 if (ast_pthread_create(&p, NULL, do_idle_thread, idle)) {
08112 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
08113 zt_hangup(idle);
08114 }
08115 } else
08116 ast_log(LOG_WARNING, "Unable to request channel 'Zap/%s' for idle call\n", idlen);
08117 gettimeofday(&lastidle, NULL);
08118 }
08119 } else if ((haveidles < pri->minunused) &&
08120 (activeidles > pri->minidle)) {
08121
08122
08123 for (x=pri->numchans;x>=0;x--) {
08124
08125 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
08126 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08127 haveidles++;
08128
08129
08130 if ((haveidles >= pri->minunused) ||
08131 (activeidles <= pri->minidle))
08132 break;
08133 }
08134 }
08135 }
08136 }
08137
08138 lowest = ast_tv(60, 0);
08139 for (i=0; i<NUM_DCHANS; i++) {
08140
08141 if (!pri->dchannels[i])
08142 break;
08143 if ((next = pri_schedule_next(pri->dchans[i]))) {
08144
08145 tv = ast_tvsub(*next, ast_tvnow());
08146 if (tv.tv_sec < 0) {
08147 tv = ast_tv(0,0);
08148 }
08149 if (doidling || pri->resetting) {
08150 if (tv.tv_sec > 1) {
08151 tv = ast_tv(1, 0);
08152 }
08153 } else {
08154 if (tv.tv_sec > 60) {
08155 tv = ast_tv(60, 0);
08156 }
08157 }
08158 } else if (doidling || pri->resetting) {
08159
08160
08161 tv = ast_tv(1,0);
08162 } else {
08163
08164 tv = ast_tv(60, 0);
08165 }
08166 if (!i || ast_tvcmp(tv, lowest) < 0) {
08167 lowest = tv;
08168 }
08169 }
08170 ast_mutex_unlock(&pri->lock);
08171
08172 e = NULL;
08173 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
08174
08175 ast_mutex_lock(&pri->lock);
08176 if (!res) {
08177 for (which=0;which<NUM_DCHANS;which++) {
08178 if (!pri->dchans[which])
08179 break;
08180
08181 e = pri_schedule_run(pri->dchans[which]);
08182 if (e)
08183 break;
08184 }
08185 } else if (res > -1) {
08186 for (which=0;which<NUM_DCHANS;which++) {
08187 if (!pri->dchans[which])
08188 break;
08189 if (fds[which].revents & POLLPRI) {
08190
08191 x = 0;
08192 res = ioctl(pri->fds[which], ZT_GETEVENT, &x);
08193 if (x)
08194 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);
08195
08196 if (x == ZT_EVENT_ALARM) {
08197 pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
08198 pri_find_dchan(pri);
08199 } else if (x == ZT_EVENT_NOALARM) {
08200 pri->dchanavail[which] |= DCHAN_NOTINALARM;
08201 pri_restart(pri->dchans[which]);
08202 }
08203
08204 if (option_debug)
08205 ast_log(LOG_DEBUG, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
08206 } else if (fds[which].revents & POLLIN) {
08207 e = pri_check_event(pri->dchans[which]);
08208 }
08209 if (e)
08210 break;
08211 }
08212 } else if (errno != EINTR)
08213 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
08214
08215 if (e) {
08216 if (pri->debug)
08217 pri_dump_event(pri->dchans[which], e);
08218 if (e->e != PRI_EVENT_DCHAN_DOWN)
08219 pri->dchanavail[which] |= DCHAN_UP;
08220 switch(e->e) {
08221 case PRI_EVENT_DCHAN_UP:
08222 if (option_verbose > 1)
08223 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d up\n", pri_order(which), pri->span);
08224 pri->dchanavail[which] |= DCHAN_UP;
08225 if (!pri->pri) pri_find_dchan(pri);
08226
08227
08228 time(&pri->lastreset);
08229
08230
08231 if (pri->resetinterval > -1) {
08232 pri->lastreset -= pri->resetinterval;
08233 pri->lastreset += 5;
08234 }
08235 pri->resetting = 0;
08236
08237 for (i=0; i<pri->numchans; i++)
08238 if (pri->pvts[i]) {
08239 pri->pvts[i]->inalarm = 0;
08240 }
08241 break;
08242 case PRI_EVENT_DCHAN_DOWN:
08243 if (option_verbose > 1)
08244 ast_verbose(VERBOSE_PREFIX_2 "%s D-Channel on span %d down\n", pri_order(which), pri->span);
08245 pri->dchanavail[which] &= ~DCHAN_UP;
08246 pri_find_dchan(pri);
08247 if (!pri_is_up(pri)) {
08248 pri->resetting = 0;
08249
08250 for (i=0; i<pri->numchans; i++) {
08251 struct zt_pvt *p = pri->pvts[i];
08252 if (p) {
08253 if (p->call) {
08254 if (p->pri && p->pri->pri) {
08255 pri_hangup(p->pri->pri, p->call, -1);
08256 pri_destroycall(p->pri->pri, p->call);
08257 p->call = NULL;
08258 } else
08259 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
08260 }
08261 if (p->realcall) {
08262 pri_hangup_all(p->realcall, pri);
08263 } else if (p->owner)
08264 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08265 p->inalarm = 1;
08266 }
08267 }
08268 }
08269 break;
08270 case PRI_EVENT_RESTART:
08271 if (e->restart.channel > -1) {
08272 chanpos = pri_find_principle(pri, e->restart.channel);
08273 if (chanpos < 0)
08274 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
08275 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08276 else {
08277 if (option_verbose > 2)
08278 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d restarted on span %d\n",
08279 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
08280 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08281 if (pri->pvts[chanpos]->call) {
08282 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
08283 pri->pvts[chanpos]->call = NULL;
08284 }
08285
08286 if (pri->pvts[chanpos]->realcall)
08287 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08288 else if (pri->pvts[chanpos]->owner)
08289 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08290 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08291 }
08292 } else {
08293 if (option_verbose > 2)
08294 ast_verbose(VERBOSE_PREFIX_2 "Restart on requested on entire span %d\n", pri->span);
08295 for (x=0;x < pri->numchans;x++)
08296 if (pri->pvts[x]) {
08297 ast_mutex_lock(&pri->pvts[x]->lock);
08298 if (pri->pvts[x]->call) {
08299 pri_destroycall(pri->pri, pri->pvts[x]->call);
08300 pri->pvts[x]->call = NULL;
08301 }
08302 if (pri->pvts[chanpos]->realcall)
08303 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08304 else if (pri->pvts[x]->owner)
08305 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08306 ast_mutex_unlock(&pri->pvts[x]->lock);
08307 }
08308 }
08309 break;
08310 case PRI_EVENT_KEYPAD_DIGIT:
08311 chanpos = pri_find_principle(pri, e->digit.channel);
08312 if (chanpos < 0) {
08313 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
08314 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
08315 } else {
08316 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
08317 if (chanpos > -1) {
08318 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08319
08320 if (pri->overlapdial && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
08321
08322 int digitlen = strlen(e->digit.digits);
08323 char digit;
08324 int i;
08325 for (i=0; i<digitlen; i++) {
08326 digit = e->digit.digits[i];
08327 {
08328 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08329 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08330 }
08331 }
08332 }
08333 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08334 }
08335 }
08336 break;
08337
08338 case PRI_EVENT_INFO_RECEIVED:
08339 chanpos = pri_find_principle(pri, e->ring.channel);
08340 if (chanpos < 0) {
08341 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
08342 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08343 } else {
08344 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
08345 if (chanpos > -1) {
08346 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08347
08348 if (pri->overlapdial && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
08349
08350 int digitlen = strlen(e->ring.callednum);
08351 char digit;
08352 int i;
08353 for (i=0; i<digitlen; i++) {
08354 digit = e->ring.callednum[i];
08355 {
08356 struct ast_frame f = { AST_FRAME_DTMF, digit, };
08357 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08358 }
08359 }
08360 }
08361 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08362 }
08363 }
08364 break;
08365 case PRI_EVENT_RING:
08366 crv = NULL;
08367 if (e->ring.channel == -1)
08368 chanpos = pri_find_empty_chan(pri, 1);
08369 else
08370 chanpos = pri_find_principle(pri, e->ring.channel);
08371
08372 if (chanpos < 0) {
08373 ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
08374 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08375 } else {
08376 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08377 if (pri->pvts[chanpos]->owner) {
08378 if (pri->pvts[chanpos]->call == e->ring.call) {
08379 ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
08380 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08381 break;
08382 } else {
08383 ast_log(LOG_WARNING, "Ring requested on channel %d/%d already in use on span %d. Hanging up owner.\n",
08384 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
08385 if (pri->pvts[chanpos]->realcall)
08386 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08387 else
08388 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08389 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08390 chanpos = -1;
08391 }
08392 }
08393 if (chanpos > -1)
08394 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08395 }
08396 if ((chanpos < 0) && (e->ring.flexible))
08397 chanpos = pri_find_empty_chan(pri, 1);
08398 if (chanpos > -1) {
08399 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08400 if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
08401
08402 crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
08403 if (crv)
08404 ast_mutex_lock(&crv->lock);
08405 if (!crv || crv->owner) {
08406 pri->pvts[chanpos]->call = NULL;
08407 if (crv) {
08408 if (crv->owner)
08409 crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08410 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);
08411 } else
08412 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);
08413 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
08414 if (crv)
08415 ast_mutex_unlock(&crv->lock);
08416 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08417 break;
08418 }
08419 }
08420 pri->pvts[chanpos]->call = e->ring.call;
08421 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
08422 if (pri->pvts[chanpos]->use_callerid) {
08423 ast_shrink_phone_number(plancallingnum);
08424 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
08425 #ifdef PRI_ANI
08426 if (!ast_strlen_zero(e->ring.callingani)) {
08427 apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
08428 ast_shrink_phone_number(plancallingani);
08429 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
08430 } else {
08431 pri->pvts[chanpos]->cid_ani[0] = '\0';
08432 }
08433 #endif
08434 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
08435 pri->pvts[chanpos]->cid_ton = e->ring.callingplan;
08436 } else {
08437 pri->pvts[chanpos]->cid_num[0] = '\0';
08438 pri->pvts[chanpos]->cid_ani[0] = '\0';
08439 pri->pvts[chanpos]->cid_name[0] = '\0';
08440 pri->pvts[chanpos]->cid_ton = 0;
08441 }
08442 apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
08443 e->ring.redirectingnum, e->ring.callingplanrdnis);
08444
08445 if (pri->pvts[chanpos]->immediate) {
08446 if (option_verbose > 2)
08447 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of immediate=yes\n");
08448 pri->pvts[chanpos]->exten[0] = 's';
08449 pri->pvts[chanpos]->exten[1] = '\0';
08450 }
08451
08452 else if (!ast_strlen_zero(e->ring.callednum)) {
08453 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
08454 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08455 } else
08456 pri->pvts[chanpos]->exten[0] = '\0';
08457
08458 if (!ast_strlen_zero(e->ring.callednum))
08459 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
08460
08461 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
08462 if (option_verbose > 2)
08463 ast_verbose(VERBOSE_PREFIX_3 "Going to extension s|1 because of Complete received\n");
08464 pri->pvts[chanpos]->exten[0] = 's';
08465 pri->pvts[chanpos]->exten[1] = '\0';
08466 }
08467
08468 if ((pri->overlapdial && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
08469 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08470
08471 int law;
08472 if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
08473
08474 law = 1;
08475 if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_AUDIOMODE, &law) == -1)
08476 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d\n", pri->pvts[chanpos]->channel, law);
08477 }
08478 if (e->ring.layer1 == PRI_LAYER_1_ALAW)
08479 law = ZT_LAW_ALAW;
08480 else
08481 law = ZT_LAW_MULAW;
08482 res = zt_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].zfd, law);
08483 if (res < 0)
08484 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
08485 res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].zfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
08486 if (res < 0)
08487 ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
08488 if (e->ring.complete || !pri->overlapdial) {
08489
08490 pri->pvts[chanpos]->proceeding = 1;
08491 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
08492 } else {
08493 if (pri->switchtype != PRI_SWITCH_GR303_TMC)
08494 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08495 else
08496 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
08497 }
08498
08499 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
08500
08501
08502 if (pri->overlapdial && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
08503
08504 ast_mutex_unlock(&pri->lock);
08505 if (crv) {
08506
08507 pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
08508 c = zt_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08509 pri->pvts[chanpos]->owner = &inuse;
08510 ast_log(LOG_DEBUG, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
08511 } else {
08512 c = zt_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
08513 }
08514 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
08515 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
08516 }
08517 if(e->ring.ani2 >= 0) {
08518 snprintf(ani2str, 5, "%.2d", e->ring.ani2);
08519 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08520 }
08521
08522 #ifdef SUPPORT_USERUSER
08523 if (!ast_strlen_zero(e->ring.useruserinfo)) {
08524 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08525 }
08526 #endif
08527
08528 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08529 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08530 if (e->ring.redirectingreason >= 0)
08531 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08532
08533 ast_mutex_lock(&pri->lock);
08534 if (c && !ast_pthread_create(&threadid, &attr, ss_thread, c)) {
08535 if (option_verbose > 2)
08536 ast_verbose(VERBOSE_PREFIX_3 "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
08537 plancallingnum, !ast_strlen_zero(pri->pvts[chanpos]->exten) ? pri->pvts[chanpos]->exten : "<unspecified>",
08538 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08539 } else {
08540 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
08541 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08542 if (c)
08543 ast_hangup(c);
08544 else {
08545 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08546 pri->pvts[chanpos]->call = NULL;
08547 }
08548 }
08549 } else {
08550 ast_mutex_unlock(&pri->lock);
08551
08552 c = zt_new(pri->pvts[chanpos], AST_STATE_RING, 1, SUB_REAL, law, e->ring.ctype);
08553 ast_mutex_lock(&pri->lock);
08554 if (c) {
08555 char calledtonstr[10];
08556 if(e->ring.ani2 >= 0) {
08557 snprintf(ani2str, 5, "%d", e->ring.ani2);
08558 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
08559 }
08560
08561 #ifdef SUPPORT_USERUSER
08562 if (!ast_strlen_zero(e->ring.useruserinfo)) {
08563 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
08564 }
08565 #endif
08566
08567 if (e->ring.redirectingreason >= 0)
08568 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
08569
08570 snprintf(calledtonstr, sizeof(calledtonstr)-1, "%d", e->ring.calledplan);
08571 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
08572 if (option_verbose > 2)
08573 ast_verbose(VERBOSE_PREFIX_3 "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
08574 plancallingnum, pri->pvts[chanpos]->exten,
08575 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08576 zt_enable_ec(pri->pvts[chanpos]);
08577 } else {
08578 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
08579 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08580 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
08581 pri->pvts[chanpos]->call = NULL;
08582 }
08583 }
08584 } else {
08585 if (option_verbose > 2)
08586 ast_verbose(VERBOSE_PREFIX_3 "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
08587 pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
08588 pri->pvts[chanpos]->prioffset, pri->span);
08589 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
08590 pri->pvts[chanpos]->call = NULL;
08591 pri->pvts[chanpos]->exten[0] = '\0';
08592 }
08593 if (crv)
08594 ast_mutex_unlock(&crv->lock);
08595 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08596 } else
08597 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
08598 break;
08599 case PRI_EVENT_RINGING:
08600 chanpos = pri_find_principle(pri, e->ringing.channel);
08601 if (chanpos < 0) {
08602 ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
08603 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08604 chanpos = -1;
08605 }
08606 if (chanpos > -1) {
08607 chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
08608 if (chanpos < 0) {
08609 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
08610 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
08611 chanpos = -1;
08612 } else {
08613 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08614 if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08615 zt_enable_ec(pri->pvts[chanpos]);
08616 pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1;
08617 pri->pvts[chanpos]->alerting = 1;
08618 } else
08619 ast_log(LOG_DEBUG, "Deferring ringing notification because of extra digits to dial...\n");
08620 #ifdef PRI_PROGRESS_MASK
08621 if (e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08622 #else
08623 if (e->ringing.progress == 8) {
08624 #endif
08625
08626 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08627
08628 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING);
08629 pri->pvts[chanpos]->dsp_features = 0;
08630 }
08631 }
08632
08633 #ifdef SUPPORT_USERUSER
08634 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
08635 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->ringing.useruserinfo);
08636 }
08637 #endif
08638
08639 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08640 }
08641 }
08642 break;
08643 case PRI_EVENT_PROGRESS:
08644
08645 chanpos = pri_find_principle(pri, e->proceeding.channel);
08646 if (chanpos > -1) {
08647 #ifdef PRI_PROGRESS_MASK
08648 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)) {
08649 #else
08650 if ((!pri->pvts[chanpos]->progress) || (e->proceeding.progress == 8)) {
08651 #endif
08652 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
08653
08654 if (e->proceeding.cause > -1) {
08655 if (option_verbose > 2)
08656 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with cause code %d received\n", e->proceeding.cause);
08657
08658
08659 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
08660 if (pri->pvts[chanpos]->owner) {
08661 if (option_verbose > 2)
08662 ast_verbose(VERBOSE_PREFIX_3 "PROGRESS with 'user busy' received, signaling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
08663
08664 pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
08665 f.subclass = AST_CONTROL_BUSY;
08666 }
08667 }
08668 }
08669
08670 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08671 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
08672 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08673 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08674 #ifdef PRI_PROGRESS_MASK
08675 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08676 #else
08677 if (e->proceeding.progress == 8) {
08678 #endif
08679
08680 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08681 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08682 pri->pvts[chanpos]->dsp_features = 0;
08683 }
08684 }
08685 pri->pvts[chanpos]->progress = 1;
08686 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08687 }
08688 }
08689 break;
08690 case PRI_EVENT_PROCEEDING:
08691 chanpos = pri_find_principle(pri, e->proceeding.channel);
08692 if (chanpos > -1) {
08693 if (!pri->pvts[chanpos]->proceeding) {
08694 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
08695
08696 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08697 ast_log(LOG_DEBUG, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
08698 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
08699 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08700 #ifdef PRI_PROGRESS_MASK
08701 if (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE) {
08702 #else
08703 if (e->proceeding.progress == 8) {
08704 #endif
08705
08706 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08707 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08708 pri->pvts[chanpos]->dsp_features = 0;
08709 }
08710
08711 f.subclass = AST_CONTROL_PROGRESS;
08712 zap_queue_frame(pri->pvts[chanpos], &f, pri);
08713 }
08714 pri->pvts[chanpos]->proceeding = 1;
08715 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08716 }
08717 }
08718 break;
08719 case PRI_EVENT_FACNAME:
08720 chanpos = pri_find_principle(pri, e->facname.channel);
08721 if (chanpos < 0) {
08722 ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
08723 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08724 chanpos = -1;
08725 }
08726 if (chanpos > -1) {
08727 chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
08728 if (chanpos < 0) {
08729 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
08730 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
08731 chanpos = -1;
08732 } else {
08733
08734 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08735 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
08736 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
08737 pri->pvts[chanpos]->subs[SUB_REAL].needcallerid =1;
08738 zt_enable_ec(pri->pvts[chanpos]);
08739 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08740 }
08741 }
08742 break;
08743 case PRI_EVENT_ANSWER:
08744 chanpos = pri_find_principle(pri, e->answer.channel);
08745 if (chanpos < 0) {
08746 ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
08747 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
08748 chanpos = -1;
08749 }
08750 if (chanpos > -1) {
08751 chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
08752 if (chanpos < 0) {
08753 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
08754 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
08755 chanpos = -1;
08756 } else {
08757 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08758
08759
08760
08761
08762
08763 if(pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
08764 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
08765 pri->pvts[chanpos]->dsp_features = 0;
08766 }
08767 if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) {
08768 ast_log(LOG_DEBUG, "Starting up GR-303 trunk now that we got CONNECT...\n");
08769 x = ZT_START;
08770 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_HOOK, &x);
08771 if (res < 0) {
08772 if (errno != EINPROGRESS) {
08773 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
08774 }
08775 }
08776 } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
08777 pri->pvts[chanpos]->dialing = 1;
08778
08779 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].zfd, ZT_DIAL, &pri->pvts[chanpos]->dop);
08780 if (res < 0) {
08781 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", pri->pvts[chanpos]->channel);
08782 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
08783 } else
08784 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
08785 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
08786 } else if (pri->pvts[chanpos]->confirmanswer) {
08787 ast_log(LOG_DEBUG, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
08788 } else {
08789 pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
08790
08791 zt_enable_ec(pri->pvts[chanpos]);
08792 }
08793
08794 #ifdef SUPPORT_USERUSER
08795 if (!ast_strlen_zero(e->answer.useruserinfo)) {
08796 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->answer.useruserinfo);
08797 }
08798 #endif
08799
08800 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08801 }
08802 }
08803 break;
08804 case PRI_EVENT_HANGUP:
08805 chanpos = pri_find_principle(pri, e->hangup.channel);
08806 if (chanpos < 0) {
08807 ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
08808 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08809 chanpos = -1;
08810 }
08811 if (chanpos > -1) {
08812 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
08813 if (chanpos > -1) {
08814 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08815 if (!pri->pvts[chanpos]->alreadyhungup) {
08816
08817 pri->pvts[chanpos]->alreadyhungup = 1;
08818 if (pri->pvts[chanpos]->realcall)
08819 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08820 else if (pri->pvts[chanpos]->owner) {
08821
08822 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
08823 switch(e->hangup.cause) {
08824 case PRI_CAUSE_USER_BUSY:
08825 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
08826 break;
08827 case PRI_CAUSE_CALL_REJECTED:
08828 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
08829 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
08830 case PRI_CAUSE_SWITCH_CONGESTION:
08831 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
08832 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
08833 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
08834 break;
08835 default:
08836 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08837 }
08838 }
08839 if (option_verbose > 2)
08840 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d got hangup\n",
08841 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
08842 } else {
08843 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
08844 pri->pvts[chanpos]->call = NULL;
08845 }
08846 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
08847 if (option_verbose > 2)
08848 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
08849 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08850 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
08851 pri->pvts[chanpos]->resetting = 1;
08852 }
08853 if (e->hangup.aoc_units > -1)
08854 if (option_verbose > 2)
08855 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
08856 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
08857
08858 #ifdef SUPPORT_USERUSER
08859 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
08860 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
08861 }
08862 #endif
08863
08864 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08865 } else {
08866 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
08867 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08868 }
08869 }
08870 break;
08871 #ifndef PRI_EVENT_HANGUP_REQ
08872 #error please update libpri
08873 #endif
08874 case PRI_EVENT_HANGUP_REQ:
08875 chanpos = pri_find_principle(pri, e->hangup.channel);
08876 if (chanpos < 0) {
08877 ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
08878 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08879 chanpos = -1;
08880 }
08881 if (chanpos > -1) {
08882 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
08883 if (chanpos > -1) {
08884 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08885 if (pri->pvts[chanpos]->realcall)
08886 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08887 else if (pri->pvts[chanpos]->owner) {
08888 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
08889 switch(e->hangup.cause) {
08890 case PRI_CAUSE_USER_BUSY:
08891 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
08892 break;
08893 case PRI_CAUSE_CALL_REJECTED:
08894 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
08895 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
08896 case PRI_CAUSE_SWITCH_CONGESTION:
08897 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
08898 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
08899 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
08900 break;
08901 default:
08902 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08903 }
08904 if (option_verbose > 2)
08905 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);
08906 if (e->hangup.aoc_units > -1)
08907 if (option_verbose > 2)
08908 ast_verbose(VERBOSE_PREFIX_3 "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
08909 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
08910 } else {
08911 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
08912 pri->pvts[chanpos]->call = NULL;
08913 }
08914 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
08915 if (option_verbose > 2)
08916 ast_verbose(VERBOSE_PREFIX_3 "Forcing restart of channel %d/%d span %d since channel reported in use\n",
08917 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08918 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
08919 pri->pvts[chanpos]->resetting = 1;
08920 }
08921
08922 #ifdef SUPPORT_USERUSER
08923 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
08924 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
08925 }
08926 #endif
08927
08928 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08929 } else {
08930 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);
08931 }
08932 }
08933 break;
08934 case PRI_EVENT_HANGUP_ACK:
08935 chanpos = pri_find_principle(pri, e->hangup.channel);
08936 if (chanpos < 0) {
08937 ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
08938 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
08939 chanpos = -1;
08940 }
08941 if (chanpos > -1) {
08942 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
08943 if (chanpos > -1) {
08944 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08945 pri->pvts[chanpos]->call = NULL;
08946 pri->pvts[chanpos]->resetting = 0;
08947 if (pri->pvts[chanpos]->owner) {
08948 if (option_verbose > 2)
08949 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);
08950 }
08951
08952 #ifdef SUPPORT_USERUSER
08953 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
08954 pbx_builtin_setvar_helper(pri->pvts[chanpos]->owner, "USERUSERINFO", e->hangup.useruserinfo);
08955 }
08956 #endif
08957
08958 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08959 }
08960 }
08961 break;
08962 case PRI_EVENT_CONFIG_ERR:
08963 ast_log(LOG_WARNING, "PRI Error: %s\n", e->err.err);
08964 break;
08965 case PRI_EVENT_RESTART_ACK:
08966 chanpos = pri_find_principle(pri, e->restartack.channel);
08967 if (chanpos < 0) {
08968
08969
08970
08971 for (x=0;x<pri->numchans;x++) {
08972 if (pri->pvts[x] && pri->pvts[x]->resetting) {
08973 chanpos = x;
08974 ast_mutex_lock(&pri->pvts[chanpos]->lock);
08975 ast_log(LOG_DEBUG, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
08976 pri->pvts[chanpos]->prioffset, pri->span);
08977 if (pri->pvts[chanpos]->realcall)
08978 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
08979 else if (pri->pvts[chanpos]->owner) {
08980 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
08981 pri->pvts[chanpos]->prioffset, pri->span);
08982 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
08983 }
08984 pri->pvts[chanpos]->resetting = 0;
08985 if (option_verbose > 2)
08986 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
08987 pri->pvts[chanpos]->prioffset, pri->span);
08988 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
08989 if (pri->resetting)
08990 pri_check_restart(pri);
08991 break;
08992 }
08993 }
08994 if (chanpos < 0) {
08995 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
08996 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
08997 }
08998 chanpos = -1;
08999 }
09000 if (chanpos > -1) {
09001 if (pri->pvts[chanpos]) {
09002 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09003 if (pri->pvts[chanpos]->realcall)
09004 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
09005 else if (pri->pvts[chanpos]->owner) {
09006 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
09007 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
09008 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
09009 }
09010 pri->pvts[chanpos]->resetting = 0;
09011 if (option_verbose > 2)
09012 ast_verbose(VERBOSE_PREFIX_3 "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
09013 pri->pvts[chanpos]->prioffset, pri->span);
09014 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09015 if (pri->resetting)
09016 pri_check_restart(pri);
09017 }
09018 }
09019 break;
09020 case PRI_EVENT_SETUP_ACK:
09021 chanpos = pri_find_principle(pri, e->setup_ack.channel);
09022 if (chanpos < 0) {
09023 ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
09024 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
09025 } else {
09026 chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
09027 if (chanpos > -1) {
09028 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09029 pri->pvts[chanpos]->setup_ack = 1;
09030
09031 for (x=0;x<strlen(pri->pvts[chanpos]->dialdest);x++) {
09032 ast_log(LOG_DEBUG, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
09033 pri_information(pri->pri, pri->pvts[chanpos]->call,
09034 pri->pvts[chanpos]->dialdest[x]);
09035 }
09036 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09037 } else
09038 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
09039 }
09040 break;
09041 case PRI_EVENT_NOTIFY:
09042 chanpos = pri_find_principle(pri, e->notify.channel);
09043 if (chanpos < 0) {
09044 ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
09045 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
09046 } else {
09047 struct ast_frame f = { AST_FRAME_CONTROL, };
09048 ast_mutex_lock(&pri->pvts[chanpos]->lock);
09049 switch(e->notify.info) {
09050 case PRI_NOTIFY_REMOTE_HOLD:
09051 f.subclass = AST_CONTROL_HOLD;
09052 zap_queue_frame(pri->pvts[chanpos], &f, pri);
09053 break;
09054 case PRI_NOTIFY_REMOTE_RETRIEVAL:
09055 f.subclass = AST_CONTROL_UNHOLD;
09056 zap_queue_frame(pri->pvts[chanpos], &f, pri);
09057 break;
09058 }
09059 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
09060 }
09061 break;
09062 default:
09063 ast_log(LOG_DEBUG, "Event: %d\n", e->e);
09064 }
09065 }
09066 ast_mutex_unlock(&pri->lock);
09067 }
09068
09069 return NULL;
09070 }
09071
09072 static int start_pri(struct zt_pri *pri)
09073 {
09074 int res, x;
09075 ZT_PARAMS p;
09076 ZT_BUFFERINFO bi;
09077 struct zt_spaninfo si;
09078 int i;
09079
09080 for (i=0;i<NUM_DCHANS;i++) {
09081 if (!pri->dchannels[i])
09082 break;
09083 pri->fds[i] = open("/dev/zap/channel", O_RDWR, 0600);
09084 x = pri->dchannels[i];
09085 if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],ZT_SPECIFY,&x) == -1)) {
09086 ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
09087 return -1;
09088 }
09089 res = ioctl(pri->fds[i], ZT_GET_PARAMS, &p);
09090 if (res) {
09091 zt_close(pri->fds[i]);
09092 pri->fds[i] = -1;
09093 ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
09094 return -1;
09095 }
09096 if (p.sigtype != ZT_SIG_HDLCFCS) {
09097 zt_close(pri->fds[i]);
09098 pri->fds[i] = -1;
09099 ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode. See /etc/zaptel.conf\n", x);
09100 return -1;
09101 }
09102 memset(&si, 0, sizeof(si));
09103 res = ioctl(pri->fds[i], ZT_SPANSTAT, &si);
09104 if (res) {
09105 zt_close(pri->fds[i]);
09106 pri->fds[i] = -1;
09107 ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
09108 }
09109 if (!si.alarms)
09110 pri->dchanavail[i] |= DCHAN_NOTINALARM;
09111 else
09112 pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
09113 bi.txbufpolicy = ZT_POLICY_IMMEDIATE;
09114 bi.rxbufpolicy = ZT_POLICY_IMMEDIATE;
09115 bi.numbufs = 32;
09116 bi.bufsize = 1024;
09117 if (ioctl(pri->fds[i], ZT_SET_BUFINFO, &bi)) {
09118 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d\n", x);
09119 zt_close(pri->fds[i]);
09120 pri->fds[i] = -1;
09121 return -1;
09122 }
09123 pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
09124
09125 if (pri->switchtype == PRI_SWITCH_GR303_TMC)
09126 pri->overlapdial = 1;
09127 pri_set_overlapdial(pri->dchans[i],pri->overlapdial);
09128
09129 if (i)
09130 pri_enslave(pri->dchans[0], pri->dchans[i]);
09131 if (!pri->dchans[i]) {
09132 zt_close(pri->fds[i]);
09133 pri->fds[i] = -1;
09134 ast_log(LOG_ERROR, "Unable to create PRI structure\n");
09135 return -1;
09136 }
09137 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
09138 pri_set_nsf(pri->dchans[i], pri->nsf);
09139 #ifdef PRI_GETSET_TIMERS
09140 for (x = 0; x < PRI_MAX_TIMERS; x++) {
09141 if (pritimers[x] != 0)
09142 pri_set_timer(pri->dchans[i], x, pritimers[x]);
09143 }
09144 #endif
09145 }
09146
09147 pri->pri = pri->dchans[0];
09148 pri->resetpos = -1;
09149 if (ast_pthread_create(&pri->master, NULL, pri_dchannel, pri)) {
09150 for (i=0;i<NUM_DCHANS;i++) {
09151 if (!pri->dchannels[i])
09152 break;
09153 zt_close(pri->fds[i]);
09154 pri->fds[i] = -1;
09155 }
09156 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
09157 return -1;
09158 }
09159 return 0;
09160 }
09161
09162 static char *complete_span_helper(char *line, char *word, int pos, int state, int rpos)
09163 {
09164 int span=1;
09165 char tmp[50];
09166 if (pos != rpos)
09167 return 0;
09168 while(span <= NUM_SPANS) {
09169 if (span > state && pris[span-1].pri)
09170 break;
09171 span++;
09172 }
09173 if (span <= NUM_SPANS) {
09174 snprintf(tmp, sizeof(tmp), "%d", span);
09175 return strdup(tmp);
09176 } else
09177 return NULL;
09178 }
09179
09180 static char *complete_span_4(char *line, char *word, int pos, int state)
09181 {
09182 return complete_span_helper(line,word,pos,state,3);
09183 }
09184
09185 static char *complete_span_5(char *line, char *word, int pos, int state)
09186 {
09187 return complete_span_helper(line,word,pos,state,4);
09188 }
09189
09190 static int handle_pri_set_debug_file(int fd, int argc, char **argv)
09191 {
09192 int myfd;
09193
09194 if (!strncasecmp(argv[1], "set", 3)) {
09195 if (argc < 5)
09196 return RESULT_SHOWUSAGE;
09197
09198 if (ast_strlen_zero(argv[4]))
09199 return RESULT_SHOWUSAGE;
09200
09201 myfd = open(argv[4], O_CREAT|O_WRONLY);
09202 if (myfd < 0) {
09203 ast_cli(fd, "Unable to open '%s' for writing\n", argv[4]);
09204 return RESULT_SUCCESS;
09205 }
09206
09207 ast_mutex_lock(&pridebugfdlock);
09208
09209 if (pridebugfd >= 0)
09210 close(pridebugfd);
09211
09212 pridebugfd = myfd;
09213 ast_copy_string(pridebugfilename,argv[4],sizeof(pridebugfilename));
09214
09215 ast_mutex_unlock(&pridebugfdlock);
09216
09217 ast_cli(fd, "PRI debug output will be sent to '%s'\n", argv[4]);
09218 } else {
09219
09220 ast_mutex_lock(&pridebugfdlock);
09221 close(pridebugfd);
09222 pridebugfd = -1;
09223 ast_cli(fd, "PRI debug output to file disabled\n");
09224 ast_mutex_unlock(&pridebugfdlock);
09225 }
09226
09227 return RESULT_SUCCESS;
09228 }
09229
09230 static int handle_pri_debug(int fd, int argc, char *argv[])
09231 {
09232 int span;
09233 int x;
09234 if (argc < 4) {
09235 return RESULT_SHOWUSAGE;
09236 }
09237 span = atoi(argv[3]);
09238 if ((span < 1) || (span > NUM_SPANS)) {
09239 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[3], 1, NUM_SPANS);
09240 return RESULT_SUCCESS;
09241 }
09242 if (!pris[span-1].pri) {
09243 ast_cli(fd, "No PRI running on span %d\n", span);
09244 return RESULT_SUCCESS;
09245 }
09246 for (x=0;x<NUM_DCHANS;x++) {
09247 if (pris[span-1].dchans[x])
09248 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE);
09249 }
09250 ast_cli(fd, "Enabled debugging on span %d\n", span);
09251 return RESULT_SUCCESS;
09252 }
09253
09254
09255
09256 static int handle_pri_no_debug(int fd, int argc, char *argv[])
09257 {
09258 int span;
09259 int x;
09260 if (argc < 5)
09261 return RESULT_SHOWUSAGE;
09262 span = atoi(argv[4]);
09263 if ((span < 1) || (span > NUM_SPANS)) {
09264 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09265 return RESULT_SUCCESS;
09266 }
09267 if (!pris[span-1].pri) {
09268 ast_cli(fd, "No PRI running on span %d\n", span);
09269 return RESULT_SUCCESS;
09270 }
09271 for (x=0;x<NUM_DCHANS;x++) {
09272 if (pris[span-1].dchans[x])
09273 pri_set_debug(pris[span-1].dchans[x], 0);
09274 }
09275 ast_cli(fd, "Disabled debugging on span %d\n", span);
09276 return RESULT_SUCCESS;
09277 }
09278
09279 static int handle_pri_really_debug(int fd, int argc, char *argv[])
09280 {
09281 int span;
09282 int x;
09283 if (argc < 5)
09284 return RESULT_SHOWUSAGE;
09285 span = atoi(argv[4]);
09286 if ((span < 1) || (span > NUM_SPANS)) {
09287 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09288 return RESULT_SUCCESS;
09289 }
09290 if (!pris[span-1].pri) {
09291 ast_cli(fd, "No PRI running on span %d\n", span);
09292 return RESULT_SUCCESS;
09293 }
09294 for (x=0;x<NUM_DCHANS;x++) {
09295 if (pris[span-1].dchans[x])
09296 pri_set_debug(pris[span-1].dchans[x], (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE));
09297 }
09298 ast_cli(fd, "Enabled EXTENSIVE debugging on span %d\n", span);
09299 return RESULT_SUCCESS;
09300 }
09301
09302 static void build_status(char *s, size_t len, int status, int active)
09303 {
09304 if (!s || len < 1) {
09305 return;
09306 }
09307 s[0] = '\0';
09308 if (status & DCHAN_PROVISIONED)
09309 strncat(s, "Provisioned, ", len - strlen(s) - 1);
09310 if (!(status & DCHAN_NOTINALARM))
09311 strncat(s, "In Alarm, ", len - strlen(s) - 1);
09312 if (status & DCHAN_UP)
09313 strncat(s, "Up", len - strlen(s) - 1);
09314 else
09315 strncat(s, "Down", len - strlen(s) - 1);
09316 if (active)
09317 strncat(s, ", Active", len - strlen(s) - 1);
09318 else
09319 strncat(s, ", Standby", len - strlen(s) - 1);
09320 s[len - 1] = '\0';
09321 }
09322
09323 static int handle_pri_show_span(int fd, int argc, char *argv[])
09324 {
09325 int span;
09326 int x;
09327 char status[256];
09328 if (argc < 4)
09329 return RESULT_SHOWUSAGE;
09330 span = atoi(argv[3]);
09331 if ((span < 1) || (span > NUM_SPANS)) {
09332 ast_cli(fd, "Invalid span %s. Should be a number %d to %d\n", argv[4], 1, NUM_SPANS);
09333 return RESULT_SUCCESS;
09334 }
09335 if (!pris[span-1].pri) {
09336 ast_cli(fd, "No PRI running on span %d\n", span);
09337 return RESULT_SUCCESS;
09338 }
09339 for(x=0;x<NUM_DCHANS;x++) {
09340 if (pris[span-1].dchannels[x]) {
09341 #ifdef PRI_DUMP_INFO_STR
09342 char *info_str = NULL;
09343 #endif
09344 ast_cli(fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
09345 build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
09346 ast_cli(fd, "Status: %s\n", status);
09347 #ifdef PRI_DUMP_INFO_STR
09348 info_str = pri_dump_info_str(pris[span-1].pri);
09349 if (info_str) {
09350 ast_cli(fd, "%s", info_str);
09351 free(info_str);
09352 }
09353 #else
09354 pri_dump_info(pris[span-1].pri);
09355 #endif
09356 ast_cli(fd, "\n");
09357 }
09358 }
09359 return RESULT_SUCCESS;
09360 }
09361
09362 static int handle_pri_show_debug(int fd, int argc, char *argv[])
09363 {
09364 int x;
09365 int span;
09366 int count=0;
09367 int debug=0;
09368
09369 for(span=0;span<NUM_SPANS;span++) {
09370 if (pris[span].pri) {
09371 for(x=0;x<NUM_DCHANS;x++) {
09372 debug=0;
09373 if (pris[span].dchans[x]) {
09374 debug = pri_get_debug(pris[span].dchans[x]);
09375 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" );
09376 count++;
09377 }
09378 }
09379 }
09380
09381 }
09382 ast_mutex_lock(&pridebugfdlock);
09383 if (pridebugfd >= 0)
09384 ast_cli(fd, "Logging PRI debug to file %s\n", pridebugfilename);
09385 ast_mutex_unlock(&pridebugfdlock);
09386
09387 if (!count)
09388 ast_cli(fd, "No debug set or no PRI running\n");
09389 return RESULT_SUCCESS;
09390 }
09391
09392 static char pri_debug_help[] =
09393 "Usage: pri debug span <span>\n"
09394 " Enables debugging on a given PRI span\n";
09395
09396 static char pri_no_debug_help[] =
09397 "Usage: pri no debug span <span>\n"
09398 " Disables debugging on a given PRI span\n";
09399
09400 static char pri_really_debug_help[] =
09401 "Usage: pri intensive debug span <span>\n"
09402 " Enables debugging down to the Q.921 level\n";
09403
09404 static char pri_show_span_help[] =
09405 "Usage: pri show span <span>\n"
09406 " Displays PRI Information\n";
09407
09408 static struct ast_cli_entry zap_pri_cli[] = {
09409 { { "pri", "debug", "span", NULL }, handle_pri_debug,
09410 "Enables PRI debugging on a span", pri_debug_help, complete_span_4 },
09411 { { "pri", "no", "debug", "span", NULL }, handle_pri_no_debug,
09412 "Disables PRI debugging on a span", pri_no_debug_help, complete_span_5 },
09413 { { "pri", "intense", "debug", "span", NULL }, handle_pri_really_debug,
09414 "Enables REALLY INTENSE PRI debugging", pri_really_debug_help, complete_span_5 },
09415 { { "pri", "show", "span", NULL }, handle_pri_show_span,
09416 "Displays PRI Information", pri_show_span_help, complete_span_4 },
09417 { { "pri", "show", "debug", NULL }, handle_pri_show_debug,
09418 "Displays current PRI debug settings" },
09419 { { "pri", "set", "debug", "file", NULL }, handle_pri_set_debug_file,
09420 "Sends PRI debug output to the specified file" },
09421 { { "pri", "unset", "debug", "file", NULL }, handle_pri_set_debug_file,
09422 "Ends PRI debug output to file" },
09423 };
09424
09425 #endif
09426
09427
09428 #ifdef ZAPATA_R2
09429 static int handle_r2_no_debug(int fd, int argc, char *argv[])
09430 {
09431 int chan;
09432 struct zt_pvt *tmp = NULL;;
09433 if (argc < 5)
09434 return RESULT_SHOWUSAGE;
09435 chan = atoi(argv[4]);
09436 if ((chan < 1) || (chan > NUM_SPANS)) {
09437 ast_cli(fd, "Invalid channel %s. Should be a number greater than 0\n", argv[4]);
09438 return RESULT_SUCCESS;
09439 }
09440 tmp = iflist;
09441 while(tmp) {
09442 if (tmp->channel == chan) {
09443 if (tmp->r2) {
09444 mfcr2_set_debug(tmp->r2, 0);
09445 ast_cli(fd, "Disabled R2 debugging on channel %d\n", chan);
09446 return RESULT_SUCCESS;
09447 }
09448 break;
09449 }
09450 tmp = tmp->next;
09451 }
09452 if (tmp)
09453 ast_cli(fd, "No R2 running on channel %d\n", chan);
09454 else
09455 ast_cli(fd, "No such zap channel %d\n", chan);
09456 return RESULT_SUCCESS;
09457 }
09458
09459 static int handle_r2_debug(int fd, int argc, char *argv[])
09460 {
09461 int chan;
09462 struct zt_pvt *tmp = NULL;;
09463 if (argc < 4) {
09464 return RESULT_SHOWUSAGE;
09465 }
09466 chan = atoi(argv[3]);
09467 if ((chan < 1) || (chan > NUM_SPANS)) {
09468 ast_cli(fd, "Invalid channel %s. Should be a number greater than 0\n", argv[3]);
09469 return RESULT_SUCCESS;
09470 }
09471 tmp = iflist;
09472 while(tmp) {
09473 if (tmp->channel == chan) {
09474 if (tmp->r2) {
09475 mfcr2_set_debug(tmp->r2, 0xFFFFFFFF);
09476 ast_cli(fd, "Enabled R2 debugging on channel %d\n", chan);
09477 return RESULT_SUCCESS;
09478 }
09479 break;
09480 }
09481 tmp = tmp->next;
09482 }
09483 if (tmp)
09484 ast_cli(fd, "No R2 running on channel %d\n", chan);
09485 else
09486 ast_cli(fd, "No such zap channel %d\n", chan);
09487 return RESULT_SUCCESS;
09488 }
09489 static char r2_debug_help[] =
09490 "Usage: r2 debug channel <channel>\n"
09491 " Enables R2 protocol level debugging on a given channel\n";
09492
09493 static char r2_no_debug_help[] =
09494 "Usage: r2 no debug channel <channel>\n"
09495 " Enables R2 protocol level debugging on a given channel\n";
09496
09497 static struct ast_cli_entry zap_r2_cli[] = {
09498 { { "r2", "debug", "channel", NULL }, handle_r2_debug,
09499 "Enables R2 debugging on a channel", r2_debug_help },
09500 { { "r2", "no", "debug", "channel", NULL }, handle_r2_no_debug,
09501 "Disables R2 debugging on a channel", r2_no_debug_help },
09502 };
09503
09504 #endif
09505
09506 static int zap_destroy_channel(int fd, int argc, char **argv)
09507 {
09508 int channel = 0;
09509 struct zt_pvt *tmp = NULL;
09510 struct zt_pvt *prev = NULL;
09511
09512 if (argc != 4) {
09513 return RESULT_SHOWUSAGE;
09514 }
09515 channel = atoi(argv[3]);
09516
09517 tmp = iflist;
09518 while (tmp) {
09519 if (tmp->channel == channel) {
09520 destroy_channel(prev, tmp, 1);
09521 return RESULT_SUCCESS;
09522 }
09523 prev = tmp;
09524 tmp = tmp->next;
09525 }
09526 return RESULT_FAILURE;
09527 }
09528
09529 static int zap_show_channels(int fd, int argc, char **argv)
09530 {
09531 #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09532 #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s\n"
09533 struct zt_pvt *tmp = NULL;
09534 char tmps[20] = "";
09535 ast_mutex_t *lock;
09536 struct zt_pvt *start;
09537 #ifdef ZAPATA_PRI
09538 int trunkgroup;
09539 struct zt_pri *pri=NULL;
09540 int x;
09541 #endif
09542
09543 lock = &iflock;
09544 start = iflist;
09545
09546 #ifdef ZAPATA_PRI
09547 if (argc == 4) {
09548 if ((trunkgroup = atoi(argv[3])) < 1)
09549 return RESULT_SHOWUSAGE;
09550 for (x=0;x<NUM_SPANS;x++) {
09551 if (pris[x].trunkgroup == trunkgroup) {
09552 pri = pris + x;
09553 break;
09554 }
09555 }
09556 if (pri) {
09557 start = pri->crvs;
09558 lock = &pri->lock;
09559 } else {
09560 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09561 return RESULT_FAILURE;
09562 }
09563 } else
09564 #endif
09565 if (argc != 3)
09566 return RESULT_SHOWUSAGE;
09567
09568 ast_mutex_lock(lock);
09569 #ifdef ZAPATA_PRI
09570 ast_cli(fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MusicOnHold");
09571 #else
09572 ast_cli(fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MusicOnHold");
09573 #endif
09574
09575 tmp = start;
09576 while (tmp) {
09577 if (tmp->channel > 0) {
09578 snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
09579 } else
09580 ast_copy_string(tmps, "pseudo", sizeof(tmps));
09581 ast_cli(fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->musicclass);
09582 tmp = tmp->next;
09583 }
09584 ast_mutex_unlock(lock);
09585 return RESULT_SUCCESS;
09586 #undef FORMAT
09587 #undef FORMAT2
09588 }
09589
09590 static int zap_show_channel(int fd, int argc, char **argv)
09591 {
09592 int channel;
09593 struct zt_pvt *tmp = NULL;
09594 ZT_CONFINFO ci;
09595 ZT_PARAMS ps;
09596 int x;
09597 ast_mutex_t *lock;
09598 struct zt_pvt *start;
09599 #ifdef ZAPATA_PRI
09600 char *c;
09601 int trunkgroup;
09602 struct zt_pri *pri=NULL;
09603 #endif
09604
09605 lock = &iflock;
09606 start = iflist;
09607
09608 if (argc != 4)
09609 return RESULT_SHOWUSAGE;
09610 #ifdef ZAPATA_PRI
09611 if ((c = strchr(argv[3], ':'))) {
09612 if (sscanf(argv[3], "%d:%d", &trunkgroup, &channel) != 2)
09613 return RESULT_SHOWUSAGE;
09614 if ((trunkgroup < 1) || (channel < 1))
09615 return RESULT_SHOWUSAGE;
09616 for (x=0;x<NUM_SPANS;x++) {
09617 if (pris[x].trunkgroup == trunkgroup) {
09618 pri = pris + x;
09619 break;
09620 }
09621 }
09622 if (pri) {
09623 start = pri->crvs;
09624 lock = &pri->lock;
09625 } else {
09626 ast_cli(fd, "No such trunk group %d\n", trunkgroup);
09627 return RESULT_FAILURE;
09628 }
09629 } else
09630 #endif
09631 channel = atoi(argv[3]);
09632
09633 ast_mutex_lock(lock);
09634 tmp = start;
09635 while (tmp) {
09636 if (tmp->channel == channel) {
09637 #ifdef ZAPATA_PRI
09638 if (pri)
09639 ast_cli(fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
09640 else
09641 #endif
09642 ast_cli(fd, "Channel: %d\n", tmp->channel);
09643 ast_cli(fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].zfd);
09644 ast_cli(fd, "Span: %d\n", tmp->span);
09645 ast_cli(fd, "Extension: %s\n", tmp->exten);
09646 ast_cli(fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
09647 ast_cli(fd, "Context: %s\n", tmp->context);
09648 ast_cli(fd, "Caller ID: %s\n", tmp->cid_num);
09649 ast_cli(fd, "Calling TON: %d\n", tmp->cid_ton);
09650 ast_cli(fd, "Caller ID name: %s\n", tmp->cid_name);
09651 ast_cli(fd, "Destroy: %d\n", tmp->destroy);
09652 ast_cli(fd, "InAlarm: %d\n", tmp->inalarm);
09653 ast_cli(fd, "Signalling Type: %s\n", sig2str(tmp->sig));
09654 ast_cli(fd, "Radio: %d\n", tmp->radio);
09655 ast_cli(fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
09656 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)" : "");
09657 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)" : "");
09658 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)" : "");
09659 ast_cli(fd, "Confno: %d\n", tmp->confno);
09660 ast_cli(fd, "Propagated Conference: %d\n", tmp->propconfno);
09661 ast_cli(fd, "Real in conference: %d\n", tmp->inconference);
09662 ast_cli(fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
09663 ast_cli(fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
09664 ast_cli(fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
09665 ast_cli(fd, "Default law: %s\n", tmp->law == ZT_LAW_MULAW ? "ulaw" : tmp->law == ZT_LAW_ALAW ? "alaw" : "unknown");
09666 ast_cli(fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
09667 ast_cli(fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
09668 ast_cli(fd, "Echo Cancellation: %d taps%s, currently %s\n", tmp->echocancel, tmp->echocanbridged ? "" : " unless TDM bridged", tmp->echocanon ? "ON" : "OFF");
09669 if (tmp->master)
09670 ast_cli(fd, "Master Channel: %d\n", tmp->master->channel);
09671 for (x=0;x<MAX_SLAVES;x++) {
09672 if (tmp->slaves[x])
09673 ast_cli(fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
09674 }
09675 #ifdef ZAPATA_PRI
09676 if (tmp->pri) {
09677 ast_cli(fd, "PRI Flags: ");
09678 if (tmp->resetting)
09679 ast_cli(fd, "Resetting ");
09680 if (tmp->call)
09681 ast_cli(fd, "Call ");
09682 if (tmp->bearer)
09683 ast_cli(fd, "Bearer ");
09684 ast_cli(fd, "\n");
09685 if (tmp->logicalspan)
09686 ast_cli(fd, "PRI Logical Span: %d\n", tmp->logicalspan);
09687 else
09688 ast_cli(fd, "PRI Logical Span: Implicit\n");
09689 }
09690
09691 #endif
09692 #ifdef ZAPATA_R2
09693 if (tmp->r2) {
09694 ast_cli(fd, "R2 Flags: ");
09695 if (tmp->r2blocked)
09696 ast_cli(fd, "Blocked ");
09697 if (tmp->hasr2call)
09698 ast_cli(fd, "Call ");
09699 ast_cli(fd, "\n");
09700 }
09701 #endif
09702 memset(&ci, 0, sizeof(ci));
09703 ps.channo = tmp->channel;
09704 if (tmp->subs[SUB_REAL].zfd > -1) {
09705 if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONF, &ci)) {
09706 ast_cli(fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
09707 }
09708 #ifdef ZT_GETCONFMUTE
09709 if (!ioctl(tmp->subs[SUB_REAL].zfd, ZT_GETCONFMUTE, &x)) {
09710 ast_cli(fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
09711 }
09712 #endif
09713 if (ioctl(tmp->subs[SUB_REAL].zfd, ZT_GET_PARAMS, &ps) < 0) {
09714 ast_log(LOG_WARNING, "Failed to get parameters on channel %d\n", tmp->channel);
09715 } else {
09716 ast_cli(fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
09717 }
09718 }
09719 ast_mutex_unlock(lock);
09720 return RESULT_SUCCESS;
09721 }
09722 tmp = tmp->next;
09723 }
09724
09725 ast_cli(fd, "Unable to find given channel %d\n", channel);
09726 ast_mutex_unlock(lock);
09727 return RESULT_FAILURE;
09728 }
09729
09730 static char zap_show_cadences_help[] =
09731 "Usage: zap show cadences\n"
09732 " Shows all cadences currently defined\n";
09733
09734 static int handle_zap_show_cadences(int fd, int argc, char *argv[])
09735 {
09736 int i, j;
09737 for (i=0;i<num_cadence;i++) {
09738 char output[1024];
09739 char tmp[16], tmp2[64];
09740 snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
09741 term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
09742
09743 for (j=0;j<16;j++) {
09744 if (cadences[i].ringcadence[j] == 0)
09745 break;
09746 snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
09747 if (cidrings[i] * 2 - 1 == j)
09748 term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
09749 else
09750 term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
09751 if (j != 0)
09752 strncat(output, ",", sizeof(output) - strlen(output) - 1);
09753 strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
09754 }
09755 ast_cli(fd,"%s\n",output);
09756 }
09757 return 0;
09758 }
09759
09760
09761 static int zap_show_status(int fd, int argc, char *argv[]) {
09762 #define FORMAT "%-40.40s %-10.10s %-10d %-10d %-10d\n"
09763 #define FORMAT2 "%-40.40s %-10.10s %-10.10s %-10.10s %-10.10s\n"
09764
09765 int span;
09766 int res;
09767 char alarms[50];
09768
09769 int ctl;
09770 ZT_SPANINFO s;
09771
09772 ctl = open("/dev/zap/ctl", O_RDWR);
09773 if (ctl < 0) {
09774 ast_log(LOG_WARNING, "Unable to open /dev/zap/ctl: %s\n", strerror(errno));
09775 ast_cli(fd, "No Zaptel interface found.\n");
09776 return RESULT_FAILURE;
09777 }
09778 ast_cli(fd,FORMAT2, "Description", "Alarms","IRQ","bpviol","CRC4");
09779
09780 for (span=1;span < ZT_MAX_SPANS;++span) {
09781 s.spanno = span;
09782 res = ioctl(ctl, ZT_SPANSTAT, &s);
09783 if (res) {
09784 continue;
09785 }
09786 alarms[0] = '\0';
09787 if (s.alarms > 0) {
09788 if (s.alarms & ZT_ALARM_BLUE)
09789 strcat(alarms,"BLU/");
09790 if (s.alarms & ZT_ALARM_YELLOW)
09791 strcat(alarms, "YEL/");
09792 if (s.alarms & ZT_ALARM_RED)
09793 strcat(alarms, "RED/");
09794 if (s.alarms & ZT_ALARM_LOOPBACK)
09795 strcat(alarms,"LB/");
09796 if (s.alarms & ZT_ALARM_RECOVER)
09797 strcat(alarms,"REC/");
09798 if (s.alarms & ZT_ALARM_NOTOPEN)
09799 strcat(alarms, "NOP/");
09800 if (!strlen(alarms))
09801 strcat(alarms, "UUU/");
09802 if (strlen(alarms)) {
09803
09804 alarms[strlen(alarms)-1]='\0';
09805 }
09806 } else {
09807 if (s.numchans)
09808 strcpy(alarms, "OK");
09809 else
09810 strcpy(alarms, "UNCONFIGURED");
09811 }
09812
09813 ast_cli(fd, FORMAT, s.desc, alarms, s.irqmisses, s.bpvcount, s.crc4count);
09814 }
09815 close(ctl);
09816
09817 return RESULT_SUCCESS;
09818 #undef FORMAT
09819 #undef FORMAT2
09820 }
09821
09822 static char show_channels_usage[] =
09823 "Usage: zap show channels\n"
09824 " Shows a list of available channels\n";
09825
09826 static char show_channel_usage[] =
09827 "Usage: zap show channel <chan num>\n"
09828 " Detailed information about a given channel\n";
09829
09830 static char zap_show_status_usage[] =
09831 "Usage: zap show status\n"
09832 " Shows a list of Zaptel cards with status\n";
09833
09834 static char destroy_channel_usage[] =
09835 "Usage: zap destroy channel <chan num>\n"
09836 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n";
09837
09838 static struct ast_cli_entry zap_cli[] = {
09839 { { "zap", "show", "cadences", NULL }, handle_zap_show_cadences,
09840 "List cadences", zap_show_cadences_help },
09841 { {"zap", "show", "channels", NULL}, zap_show_channels,
09842 "Show active zapata channels", show_channels_usage },
09843 { {"zap", "show", "channel", NULL}, zap_show_channel,
09844 "Show information on a channel", show_channel_usage },
09845 { {"zap", "destroy", "channel", NULL}, zap_destroy_channel,
09846 "Destroy a channel", destroy_channel_usage },
09847 { {"zap", "show", "status", NULL}, zap_show_status,
09848 "Show all Zaptel cards status", zap_show_status_usage },
09849 };
09850
09851 #define TRANSFER 0
09852 #define HANGUP 1
09853
09854 static int zap_fake_event(struct zt_pvt *p, int mode)
09855 {
09856 if (p) {
09857 switch(mode) {
09858 case TRANSFER:
09859 p->fake_event = ZT_EVENT_WINKFLASH;
09860 break;
09861 case HANGUP:
09862 p->fake_event = ZT_EVENT_ONHOOK;
09863 break;
09864 default:
09865 ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);
09866 }
09867 }
09868 return 0;
09869 }
09870 static struct zt_pvt *find_channel(int channel)
09871 {
09872 struct zt_pvt *p = iflist;
09873 while(p) {
09874 if (p->channel == channel) {
09875 break;
09876 }
09877 p = p->next;
09878 }
09879 return p;
09880 }
09881
09882 static int action_zapdndon(struct mansession *s, struct message *m)
09883 {
09884 struct zt_pvt *p = NULL;
09885 char *channel = astman_get_header(m, "ZapChannel");
09886 if (ast_strlen_zero(channel)) {
09887 astman_send_error(s, m, "No channel specified");
09888 return 0;
09889 }
09890 p = find_channel(atoi(channel));
09891 if (!p) {
09892 astman_send_error(s, m, "No such channel");
09893 return 0;
09894 }
09895 p->dnd = 1;
09896 astman_send_ack(s, m, "DND Enabled");
09897 return 0;
09898 }
09899
09900 static int action_zapdndoff(struct mansession *s, struct message *m)
09901 {
09902 struct zt_pvt *p = NULL;
09903 char *channel = astman_get_header(m, "ZapChannel");
09904 if (ast_strlen_zero(channel)) {
09905 astman_send_error(s, m, "No channel specified");
09906 return 0;
09907 }
09908 p = find_channel(atoi(channel));
09909 if (!p) {
09910 astman_send_error(s, m, "No such channel");
09911 return 0;
09912 }
09913 p->dnd = 0;
09914 astman_send_ack(s, m, "DND Disabled");
09915 return 0;
09916 }
09917
09918 static int action_transfer(struct mansession *s, struct message *m)
09919 {
09920 struct zt_pvt *p = NULL;
09921 char *channel = astman_get_header(m, "ZapChannel");
09922 if (ast_strlen_zero(channel)) {
09923 astman_send_error(s, m, "No channel specified");
09924 return 0;
09925 }
09926 p = find_channel(atoi(channel));
09927 if (!p) {
09928 astman_send_error(s, m, "No such channel");
09929 return 0;
09930 }
09931 zap_fake_event(p,TRANSFER);
09932 astman_send_ack(s, m, "ZapTransfer");
09933 return 0;
09934 }
09935
09936 static int action_transferhangup(struct mansession *s, struct message *m)
09937 {
09938 struct zt_pvt *p = NULL;
09939 char *channel = astman_get_header(m, "ZapChannel");
09940 if (ast_strlen_zero(channel)) {
09941 astman_send_error(s, m, "No channel specified");
09942 return 0;
09943 }
09944 p = find_channel(atoi(channel));
09945 if (!p) {
09946 astman_send_error(s, m, "No such channel");
09947 return 0;
09948 }
09949 zap_fake_event(p,HANGUP);
09950 astman_send_ack(s, m, "ZapHangup");
09951 return 0;
09952 }
09953
09954 static int action_zapdialoffhook(struct mansession *s, struct message *m)
09955 {
09956 struct zt_pvt *p = NULL;
09957 char *channel = astman_get_header(m, "ZapChannel");
09958 char *number = astman_get_header(m, "Number");
09959 int i;
09960 if (ast_strlen_zero(channel)) {
09961 astman_send_error(s, m, "No channel specified");
09962 return 0;
09963 }
09964 if (ast_strlen_zero(number)) {
09965 astman_send_error(s, m, "No number specified");
09966 return 0;
09967 }
09968 p = find_channel(atoi(channel));
09969 if (!p) {
09970 astman_send_error(s, m, "No such channel");
09971 return 0;
09972 }
09973 if (!p->owner) {
09974 astman_send_error(s, m, "Channel does not have it's owner");
09975 return 0;
09976 }
09977 for (i=0; i<strlen(number); i++) {
09978 struct ast_frame f = { AST_FRAME_DTMF, number[i] };
09979 zap_queue_frame(p, &f, NULL);
09980 }
09981 astman_send_ack(s, m, "ZapDialOffhook");
09982 return 0;
09983 }
09984
09985 static int action_zapshowchannels(struct mansession *s, struct message *m)
09986 {
09987 struct zt_pvt *tmp = NULL;
09988 char *id = astman_get_header(m, "ActionID");
09989 char idText[256] = "";
09990
09991 astman_send_ack(s, m, "Zapata channel status will follow");
09992 if (!ast_strlen_zero(id))
09993 snprintf(idText, sizeof(idText) - 1, "ActionID: %s\r\n", id);
09994
09995 ast_mutex_lock(&iflock);
09996
09997 tmp = iflist;
09998 while (tmp) {
09999 if (tmp->channel > 0) {
10000 int alarm = get_alarms(tmp);
10001 ast_cli(s->fd,
10002 "Event: ZapShowChannels\r\n"
10003 "Channel: %d\r\n"
10004 "Signalling: %s\r\n"
10005 "Context: %s\r\n"
10006 "DND: %s\r\n"
10007 "Alarm: %s\r\n"
10008 "%s"
10009 "\r\n",
10010 tmp->channel, sig2str(tmp->sig), tmp->context,
10011 tmp->dnd ? "Enabled" : "Disabled",
10012 alarm2str(alarm), idText);
10013 }
10014
10015 tmp = tmp->next;
10016 }
10017
10018 ast_mutex_unlock(&iflock);
10019
10020 ast_cli(s->fd,
10021 "Event: ZapShowChannelsComplete\r\n"
10022 "%s"
10023 "\r\n",
10024 idText);
10025 return 0;
10026 }
10027
10028 static int __unload_module(void)
10029 {
10030 int x = 0;
10031 struct zt_pvt *p, *pl;
10032 #ifdef ZAPATA_PRI
10033 int i;
10034 for(i=0;i<NUM_SPANS;i++) {
10035 if (pris[i].master != AST_PTHREADT_NULL)
10036 pthread_cancel(pris[i].master);
10037 }
10038 ast_cli_unregister_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(zap_pri_cli[0]));
10039 #endif
10040 #ifdef ZAPATA_R2
10041 ast_cli_unregister_multiple(zap_r2_cli, sizeof(zap_r2_cli) / sizeof(zap_r2_cli[0]));
10042 #endif
10043 ast_cli_unregister_multiple(zap_cli, sizeof(zap_cli) / sizeof(zap_cli[0]));
10044 ast_manager_unregister( "ZapDialOffhook" );
10045 ast_manager_unregister( "ZapHangup" );
10046 ast_manager_unregister( "ZapTransfer" );
10047 ast_manager_unregister( "ZapDNDoff" );
10048 ast_manager_unregister( "ZapDNDon" );
10049 ast_manager_unregister("ZapShowChannels");
10050 ast_channel_unregister(&zap_tech);
10051 if (!ast_mutex_lock(&iflock)) {
10052
10053 p = iflist;
10054 while(p) {
10055 if (p->owner)
10056 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
10057 p = p->next;
10058 }
10059 ast_mutex_unlock(&iflock);
10060 } else {
10061 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10062 return -1;
10063 }
10064 if (!ast_mutex_lock(&monlock)) {
10065 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
10066 pthread_cancel(monitor_thread);
10067 pthread_kill(monitor_thread, SIGURG);
10068 pthread_join(monitor_thread, NULL);
10069 }
10070 monitor_thread = AST_PTHREADT_STOP;
10071 ast_mutex_unlock(&monlock);
10072 } else {
10073 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10074 return -1;
10075 }
10076
10077 if (!ast_mutex_lock(&iflock)) {
10078
10079 p = iflist;
10080 while(p) {
10081
10082 if (p->cidspill)
10083 free(p->cidspill);
10084
10085 if (p->subs[SUB_REAL].zfd > -1)
10086 zt_close(p->subs[SUB_REAL].zfd);
10087 pl = p;
10088 p = p->next;
10089 x++;
10090
10091 if(pl)
10092 destroy_zt_pvt(&pl);
10093 ast_verbose(VERBOSE_PREFIX_3 "Unregistered channel %d\n", x);
10094 }
10095 iflist = NULL;
10096 ifcount = 0;
10097 ast_mutex_unlock(&iflock);
10098 } else {
10099 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
10100 return -1;
10101 }
10102 #ifdef ZAPATA_PRI
10103 for(i=0;i<NUM_SPANS;i++) {
10104 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
10105 pthread_join(pris[i].master, NULL);
10106 zt_close(pris[i].fds[i]);
10107 }
10108 #endif
10109 return 0;
10110 }
10111
10112 int unload_module()
10113 {
10114 #ifdef ZAPATA_PRI
10115 int y;
10116 for (y=0;y<NUM_SPANS;y++)
10117 ast_mutex_destroy(&pris[y].lock);
10118 #endif
10119 return __unload_module();
10120 }
10121
10122 static int setup_zap(int reload)
10123 {
10124 struct ast_config *cfg;
10125 struct ast_variable *v;
10126 struct zt_pvt *tmp;
10127 char *chan;
10128 char *c;
10129 char *ringc;
10130 int start, finish,x;
10131 int y;
10132 int found_pseudo = 0;
10133 int cur_radio = 0;
10134 #ifdef ZAPATA_PRI
10135 int spanno;
10136 int i;
10137 int logicalspan;
10138 int trunkgroup;
10139 int dchannels[NUM_DCHANS];
10140 struct zt_pri *pri;
10141 #endif
10142
10143 cfg = ast_config_load(config);
10144
10145
10146 if (!cfg) {
10147 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
10148 return -1;
10149 }
10150
10151
10152 if (ast_mutex_lock(&iflock)) {
10153
10154 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
10155 return -1;
10156 }
10157 #ifdef ZAPATA_PRI
10158 if (!reload) {
10159
10160 v = ast_variable_browse(cfg, "trunkgroups");
10161 while(v) {
10162 if (!strcasecmp(v->name, "trunkgroup")) {
10163 trunkgroup = atoi(v->value);
10164 if (trunkgroup > 0) {
10165 if ((c = strchr(v->value, ','))) {
10166 i = 0;
10167 memset(dchannels, 0, sizeof(dchannels));
10168 while(c && (i < NUM_DCHANS)) {
10169 dchannels[i] = atoi(c + 1);
10170 if (dchannels[i] < 0) {
10171 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);
10172 } else
10173 i++;
10174 c = strchr(c + 1, ',');
10175 }
10176 if (i) {
10177 if (pri_create_trunkgroup(trunkgroup, dchannels)) {
10178 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);
10179 } else if (option_verbose > 1)
10180 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");
10181 } else
10182 ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of zapata.conf\n", trunkgroup, v->lineno);
10183 } else
10184 ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of zapata.conf\n", trunkgroup, v->lineno);
10185 } else
10186 ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of zapata.conf\n", v->lineno);
10187 } else if (!strcasecmp(v->name, "spanmap")) {
10188 spanno = atoi(v->value);
10189 if (spanno > 0) {
10190 if ((c = strchr(v->value, ','))) {
10191 trunkgroup = atoi(c + 1);
10192 if (trunkgroup > 0) {
10193 if ((c = strchr(c + 1, ',')))
10194 logicalspan = atoi(c + 1);
10195 else
10196 logicalspan = 0;
10197 if (logicalspan >= 0) {
10198 if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
10199 ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
10200 } else if (option_verbose > 1)
10201 ast_verbose(VERBOSE_PREFIX_2 "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
10202 } else
10203 ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of zapata.conf\n", v->lineno);
10204 } else
10205 ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of zapata.conf\n", v->lineno);
10206 } else
10207 ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of zapata.conf\n", v->lineno);
10208 } else
10209 ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of zapata.conf\n", v->lineno);
10210 } else {
10211 ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
10212 }
10213 v = v->next;
10214 }
10215 }
10216 #endif
10217 v = ast_variable_browse(cfg, "channels");
10218 while(v) {
10219
10220 if (!strcasecmp(v->name, "channel")
10221 #ifdef ZAPATA_PRI
10222 || !strcasecmp(v->name, "crv")
10223 #endif
10224 ) {
10225 if (reload == 0) {
10226 if (cur_signalling < 0) {
10227 ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
10228 ast_config_destroy(cfg);
10229 ast_mutex_unlock(&iflock);
10230 return -1;
10231 }
10232 }
10233 c = v->value;
10234
10235 #ifdef ZAPATA_PRI
10236 pri = NULL;
10237 if (!strcasecmp(v->name, "crv")) {
10238 if (sscanf(c, "%d:%n", &trunkgroup, &y) != 1) {
10239 ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d\n", v->lineno);
10240 ast_config_destroy(cfg);
10241 ast_mutex_unlock(&iflock);
10242 return -1;
10243 }
10244 if (trunkgroup < 1) {
10245 ast_log(LOG_WARNING, "CRV trunk group must be a postive number at line %d\n", v->lineno);
10246 ast_config_destroy(cfg);
10247 ast_mutex_unlock(&iflock);
10248 return -1;
10249 }
10250 c+=y;
10251 for (y=0;y<NUM_SPANS;y++) {
10252 if (pris[y].trunkgroup == trunkgroup) {
10253 pri = pris + y;
10254 break;
10255 }
10256 }
10257 if (!pri) {
10258 ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d\n", trunkgroup, v->lineno);
10259 ast_config_destroy(cfg);
10260 ast_mutex_unlock(&iflock);
10261 return -1;
10262 }
10263 }
10264 #endif
10265 chan = strsep(&c, ",");
10266 while(chan) {
10267 if (sscanf(chan, "%d-%d", &start, &finish) == 2) {
10268
10269 } else if (sscanf(chan, "%d", &start)) {
10270
10271 finish = start;
10272 } else if (!strcasecmp(chan, "pseudo")) {
10273 finish = start = CHAN_PSEUDO;
10274 found_pseudo = 1;
10275 } else {
10276 ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", v->value, chan);
10277 ast_config_destroy(cfg);
10278 ast_mutex_unlock(&iflock);
10279 return -1;
10280 }
10281 if (finish < start) {
10282 ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
10283 x = finish;
10284 finish = start;
10285 start = x;
10286 }
10287 for (x=start;x<=finish;x++) {
10288 #ifdef ZAPATA_PRI
10289 tmp = mkintf(x, cur_signalling, cur_radio, pri, reload);
10290 #else
10291 tmp = mkintf(x, cur_signalling, cur_radio, NULL, reload);
10292 #endif
10293
10294 if (tmp) {
10295 if (option_verbose > 2) {
10296 #ifdef ZAPATA_PRI
10297 if (pri)
10298 ast_verbose(VERBOSE_PREFIX_3 "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup,x, sig2str(tmp->sig));
10299 else
10300 #endif
10301 ast_verbose(VERBOSE_PREFIX_3 "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig));
10302 }
10303 } else {
10304 if (reload == 1)
10305 ast_log(LOG_ERROR, "Unable to reconfigure channel '%s'\n", v->value);
10306 else
10307 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
10308 ast_config_destroy(cfg);
10309 ast_mutex_unlock(&iflock);
10310 return -1;
10311 }
10312 }
10313 chan = strsep(&c, ",");
10314 }
10315 } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
10316 if (ast_true(v->value))
10317 usedistinctiveringdetection = 1;
10318 } else if (!strcasecmp(v->name, "dring1context")) {
10319 ast_copy_string(drings.ringContext[0].contextData,v->value,sizeof(drings.ringContext[0].contextData));
10320 } else if (!strcasecmp(v->name, "dring2context")) {
10321 ast_copy_string(drings.ringContext[1].contextData,v->value,sizeof(drings.ringContext[1].contextData));
10322 } else if (!strcasecmp(v->name, "dring3context")) {
10323 ast_copy_string(drings.ringContext[2].contextData,v->value,sizeof(drings.ringContext[2].contextData));
10324 } else if (!strcasecmp(v->name, "dring1")) {
10325 ringc = v->value;
10326 sscanf(ringc, "%d,%d,%d", &drings.ringnum[0].ring[0], &drings.ringnum[0].ring[1], &drings.ringnum[0].ring[2]);
10327 } else if (!strcasecmp(v->name, "dring2")) {
10328 ringc = v->value;
10329 sscanf(ringc,"%d,%d,%d", &drings.ringnum[1].ring[0], &drings.ringnum[1].ring[1], &drings.ringnum[1].ring[2]);
10330 } else if (!strcasecmp(v->name, "dring3")) {
10331 ringc = v->value;
10332 sscanf(ringc, "%d,%d,%d", &drings.ringnum[2].ring[0], &drings.ringnum[2].ring[1], &drings.ringnum[2].ring[2]);
10333 } else if (!strcasecmp(v->name, "usecallerid")) {
10334 use_callerid = ast_true(v->value);
10335 } else if (!strcasecmp(v->name, "cidsignalling")) {
10336 if (!strcasecmp(v->value, "bell"))
10337 cid_signalling = CID_SIG_BELL;
10338 else if (!strcasecmp(v->value, "v23"))
10339 cid_signalling = CID_SIG_V23;
10340 else if (!strcasecmp(v->value, "dtmf"))
10341 cid_signalling = CID_SIG_DTMF;
10342 else if (ast_true(v->value))
10343 cid_signalling = CID_SIG_BELL;
10344 } else if (!strcasecmp(v->name, "cidstart")) {
10345 if (!strcasecmp(v->value, "ring"))
10346 cid_start = CID_START_RING;
10347 else if (!strcasecmp(v->value, "polarity"))
10348 cid_start = CID_START_POLARITY;
10349 else if (ast_true(v->value))
10350 cid_start = CID_START_RING;
10351 } else if (!strcasecmp(v->name, "threewaycalling")) {
10352 threewaycalling = ast_true(v->value);
10353 } else if (!strcasecmp(v->name, "cancallforward")) {
10354 cancallforward = ast_true(v->value);
10355 } else if (!strcasecmp(v->name, "relaxdtmf")) {
10356 if (ast_true(v->value))
10357 relaxdtmf = DSP_DIGITMODE_RELAXDTMF;
10358 else
10359 relaxdtmf = 0;
10360 } else if (!strcasecmp(v->name, "mailbox")) {
10361 ast_copy_string(mailbox, v->value, sizeof(mailbox));
10362 } else if (!strcasecmp(v->name, "adsi")) {
10363 adsi = ast_true(v->value);
10364 } else if (!strcasecmp(v->name, "transfer")) {
10365 transfer = ast_true(v->value);
10366 } else if (!strcasecmp(v->name, "canpark")) {
10367 canpark = ast_true(v->value);
10368 } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
10369 echocanbridged = ast_true(v->value);
10370 } else if (!strcasecmp(v->name, "busydetect")) {
10371 busydetect = ast_true(v->value);
10372 } else if (!strcasecmp(v->name, "busycount")) {
10373 busycount = atoi(v->value);
10374 } else if (!strcasecmp(v->name, "busypattern")) {
10375 if (sscanf(v->value, "%d,%d", &busy_tonelength, &busy_quietlength) != 2) {
10376 ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength\n");
10377 }
10378 } else if (!strcasecmp(v->name, "callprogress")) {
10379 if (ast_true(v->value))
10380 callprogress |= 1;
10381 else
10382 callprogress &= ~1;
10383 } else if (!strcasecmp(v->name, "faxdetect")) {
10384 if (!strcasecmp(v->value, "incoming")) {
10385 callprogress |= 4;
10386 callprogress &= ~2;
10387 } else if (!strcasecmp(v->value, "outgoing")) {
10388 callprogress &= ~4;
10389 callprogress |= 2;
10390 } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
10391 callprogress |= 6;
10392 else
10393 callprogress &= ~6;
10394 } else if (!strcasecmp(v->name, "echocancel")) {
10395 if (!ast_strlen_zero(v->value)) {
10396 y = atoi(v->value);
10397 } else
10398 y = 0;
10399 if ((y == 32) || (y == 64) || (y == 128) || (y == 256))
10400 echocancel = y;
10401 else {
10402 echocancel = ast_true(v->value);
10403 if (echocancel)
10404 echocancel=128;
10405 }
10406 } else if (!strcasecmp(v->name, "echotraining")) {
10407 if (sscanf(v->value, "%d", &y) == 1) {
10408 if ((y < 10) || (y > 4000)) {
10409 ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 2000 ms at line %d\n", v->lineno);
10410 } else {
10411 echotraining = y;
10412 }
10413 } else if (ast_true(v->value)) {
10414 echotraining = 400;
10415 } else
10416 echotraining = 0;
10417 } else if (!strcasecmp(v->name, "hidecallerid")) {
10418 hidecallerid = ast_true(v->value);
10419 } else if (!strcasecmp(v->name, "pulsedial")) {
10420 pulse = ast_true(v->value);
10421 } else if (!strcasecmp(v->name, "callreturn")) {
10422 callreturn = ast_true(v->value);
10423 } else if (!strcasecmp(v->name, "callwaiting")) {
10424 callwaiting = ast_true(v->value);
10425 } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
10426 callwaitingcallerid = ast_true(v->value);
10427 } else if (!strcasecmp(v->name, "context")) {
10428 ast_copy_string(context, v->value, sizeof(context));
10429 } else if (!strcasecmp(v->name, "language")) {
10430 ast_copy_string(language, v->value, sizeof(language));
10431 } else if (!strcasecmp(v->name, "progzone")) {
10432 ast_copy_string(progzone, v->value, sizeof(progzone));
10433 } else if (!strcasecmp(v->name, "musiconhold")) {
10434 ast_copy_string(musicclass, v->value, sizeof(musicclass));
10435 } else if (!strcasecmp(v->name, "stripmsd")) {
10436 stripmsd = atoi(v->value);
10437 } else if (!strcasecmp(v->name, "jitterbuffers")) {
10438 numbufs = atoi(v->value);
10439 } else if (!strcasecmp(v->name, "group")) {
10440 cur_group = ast_get_group(v->value);
10441 } else if (!strcasecmp(v->name, "callgroup")) {
10442 cur_callergroup = ast_get_group(v->value);
10443 } else if (!strcasecmp(v->name, "pickupgroup")) {
10444 cur_pickupgroup = ast_get_group(v->value);
10445 } else if (!strcasecmp(v->name, "immediate")) {
10446 immediate = ast_true(v->value);
10447 } else if (!strcasecmp(v->name, "transfertobusy")) {
10448 transfertobusy = ast_true(v->value);
10449 } else if (!strcasecmp(v->name, "rxgain")) {
10450 if (sscanf(v->value, "%f", &rxgain) != 1) {
10451 ast_log(LOG_WARNING, "Invalid rxgain: %s\n", v->value);
10452 }
10453 } else if (!strcasecmp(v->name, "txgain")) {
10454 if (sscanf(v->value, "%f", &txgain) != 1) {
10455 ast_log(LOG_WARNING, "Invalid txgain: %s\n", v->value);
10456 }
10457 } else if (!strcasecmp(v->name, "tonezone")) {
10458 if (sscanf(v->value, "%d", &tonezone) != 1) {
10459 ast_log(LOG_WARNING, "Invalid tonezone: %s\n", v->value);
10460 }
10461 } else if (!strcasecmp(v->name, "callerid")) {
10462 if (!strcasecmp(v->value, "asreceived")) {
10463 cid_num[0] = '\0';
10464 cid_name[0] = '\0';
10465 } else {
10466 ast_callerid_split(v->value, cid_name, sizeof(cid_name), cid_num, sizeof(cid_num));
10467 }
10468 } else if (!strcasecmp(v->name, "useincomingcalleridonzaptransfer")) {
10469 zaptrcallerid = ast_true(v->value);
10470 } else if (!strcasecmp(v->name, "restrictcid")) {
10471 restrictcid = ast_true(v->value);
10472 } else if (!strcasecmp(v->name, "usecallingpres")) {
10473 use_callingpres = ast_true(v->value);
10474 } else if (!strcasecmp(v->name, "accountcode")) {
10475 ast_copy_string(accountcode, v->value, sizeof(accountcode));
10476 } else if (!strcasecmp(v->name, "amaflags")) {
10477 y = ast_cdr_amaflags2int(v->value);
10478 if (y < 0)
10479 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d\n", v->value, v->lineno);
10480 else
10481 amaflags = y;
10482 } else if(!reload){
10483 if (!strcasecmp(v->name, "signalling")) {
10484 if (!strcasecmp(v->value, "em")) {
10485 cur_signalling = SIG_EM;
10486 } else if (!strcasecmp(v->value, "em_e1")) {
10487 cur_signalling = SIG_EM_E1;
10488 } else if (!strcasecmp(v->value, "em_w")) {
10489 cur_signalling = SIG_EMWINK;
10490 cur_radio = 0;
10491 } else if (!strcasecmp(v->value, "fxs_ls")) {
10492 cur_signalling = SIG_FXSLS;
10493 cur_radio = 0;
10494 } else if (!strcasecmp(v->value, "fxs_gs")) {
10495 cur_signalling = SIG_FXSGS;
10496 cur_radio = 0;
10497 } else if (!strcasecmp(v->value, "fxs_ks")) {
10498 cur_signalling = SIG_FXSKS;
10499 cur_radio = 0;
10500 } else if (!strcasecmp(v->value, "fxo_ls")) {
10501 cur_signalling = SIG_FXOLS;
10502 cur_radio = 0;
10503 } else if (!strcasecmp(v->value, "fxo_gs")) {
10504 cur_signalling = SIG_FXOGS;
10505 cur_radio = 0;
10506 } else if (!strcasecmp(v->value, "fxo_ks")) {
10507 cur_signalling = SIG_FXOKS;
10508 cur_radio = 0;
10509 } else if (!strcasecmp(v->value, "fxs_rx")) {
10510 cur_signalling = SIG_FXSKS;
10511 cur_radio = 1;
10512 } else if (!strcasecmp(v->value, "fxo_rx")) {
10513 cur_signalling = SIG_FXOLS;
10514 cur_radio = 1;
10515 } else if (!strcasecmp(v->value, "fxs_tx")) {
10516 cur_signalling = SIG_FXSLS;
10517 cur_radio = 1;
10518 } else if (!strcasecmp(v->value, "fxo_tx")) {
10519 cur_signalling = SIG_FXOGS;
10520 cur_radio = 1;
10521 } else if (!strcasecmp(v->value, "em_rx")) {
10522 cur_signalling = SIG_EM;
10523 cur_radio = 1;
10524 } else if (!strcasecmp(v->value, "em_tx")) {
10525 cur_signalling = SIG_EM;
10526 cur_radio = 1;
10527 } else if (!strcasecmp(v->value, "em_rxtx")) {
10528 cur_signalling = SIG_EM;
10529 cur_radio = 2;
10530 } else if (!strcasecmp(v->value, "em_txrx")) {
10531 cur_signalling = SIG_EM;
10532 cur_radio = 2;
10533 } else if (!strcasecmp(v->value, "sf")) {
10534 cur_signalling = SIG_SF;
10535 cur_radio = 0;
10536 } else if (!strcasecmp(v->value, "sf_w")) {
10537 cur_signalling = SIG_SFWINK;
10538 cur_radio = 0;
10539 } else if (!strcasecmp(v->value, "sf_featd")) {
10540 cur_signalling = SIG_FEATD;
10541 cur_radio = 0;
10542 } else if (!strcasecmp(v->value, "sf_featdmf")) {
10543 cur_signalling = SIG_FEATDMF;
10544 cur_radio = 0;
10545 } else if (!strcasecmp(v->value, "sf_featb")) {
10546 cur_signalling = SIG_SF_FEATB;
10547 cur_radio = 0;
10548 } else if (!strcasecmp(v->value, "sf")) {
10549 cur_signalling = SIG_SF;
10550 cur_radio = 0;
10551 } else if (!strcasecmp(v->value, "sf_rx")) {
10552 cur_signalling = SIG_SF;
10553 cur_radio = 1;
10554 } else if (!strcasecmp(v->value, "sf_tx")) {
10555 cur_signalling = SIG_SF;
10556 cur_radio = 1;
10557 } else if (!strcasecmp(v->value, "sf_rxtx")) {
10558 cur_signalling = SIG_SF;
10559 cur_radio = 2;
10560 } else if (!strcasecmp(v->value, "sf_txrx")) {
10561 cur_signalling = SIG_SF;
10562 cur_radio = 2;
10563 } else if (!strcasecmp(v->value, "featd")) {
10564 cur_signalling = SIG_FEATD;
10565 cur_radio = 0;
10566 } else if (!strcasecmp(v->value, "featdmf")) {
10567 cur_signalling = SIG_FEATDMF;
10568 cur_radio = 0;
10569 } else if (!strcasecmp(v->value, "featdmf_ta")) {
10570 cur_signalling = SIG_FEATDMF_TA;
10571 cur_radio = 0;
10572 } else if (!strcasecmp(v->value, "e911")) {
10573 cur_signalling = SIG_E911;
10574 cur_radio = 0;
10575 } else if (!strcasecmp(v->value, "featb")) {
10576 cur_signalling = SIG_FEATB;
10577 cur_radio = 0;
10578 #ifdef ZAPATA_PRI
10579 } else if (!strcasecmp(v->value, "pri_net")) {
10580 cur_radio = 0;
10581 cur_signalling = SIG_PRI;
10582 pritype = PRI_NETWORK;
10583 } else if (!strcasecmp(v->value, "pri_cpe")) {
10584 cur_signalling = SIG_PRI;
10585 cur_radio = 0;
10586 pritype = PRI_CPE;
10587 } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
10588 cur_signalling = SIG_GR303FXOKS;
10589 cur_radio = 0;
10590 pritype = PRI_NETWORK;
10591 } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
10592 cur_signalling = SIG_GR303FXSKS;
10593 cur_radio = 0;
10594 pritype = PRI_CPE;
10595 #endif
10596 #ifdef ZAPATA_R2
10597 } else if (!strcasecmp(v->value, "r2")) {
10598 cur_signalling = SIG_R2;
10599 cur_radio = 0;
10600 #endif
10601 } else {
10602 ast_log(LOG_ERROR, "Unknown signalling method '%s'\n", v->value);
10603 }
10604 #ifdef ZAPATA_R2
10605 } else if (!strcasecmp(v->name, "r2country")) {
10606 r2prot = str2r2prot(v->value);
10607 if (r2prot < 0) {
10608 ast_log(LOG_WARNING, "Unknown R2 Country '%s' at line %d.\n", v->value, v->lineno);
10609 }
10610 #endif
10611 #ifdef ZAPATA_PRI
10612 } else if (!strcasecmp(v->name, "pridialplan")) {
10613 if (!strcasecmp(v->value, "national")) {
10614 dialplan = PRI_NATIONAL_ISDN + 1;
10615 } else if (!strcasecmp(v->value, "unknown")) {
10616 dialplan = PRI_UNKNOWN + 1;
10617 } else if (!strcasecmp(v->value, "private")) {
10618 dialplan = PRI_PRIVATE + 1;
10619 } else if (!strcasecmp(v->value, "international")) {
10620 dialplan = PRI_INTERNATIONAL_ISDN + 1;
10621 } else if (!strcasecmp(v->value, "local")) {
10622 dialplan = PRI_LOCAL_ISDN + 1;
10623 } else if (!strcasecmp(v->value, "dynamic")) {
10624 dialplan = -1;
10625 } else {
10626 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10627 }
10628 } else if (!strcasecmp(v->name, "prilocaldialplan")) {
10629 if (!strcasecmp(v->value, "national")) {
10630 localdialplan = PRI_NATIONAL_ISDN + 1;
10631 } else if (!strcasecmp(v->value, "unknown")) {
10632 localdialplan = PRI_UNKNOWN + 1;
10633 } else if (!strcasecmp(v->value, "private")) {
10634 localdialplan = PRI_PRIVATE + 1;
10635 } else if (!strcasecmp(v->value, "international")) {
10636 localdialplan = PRI_INTERNATIONAL_ISDN + 1;
10637 } else if (!strcasecmp(v->value, "local")) {
10638 localdialplan = PRI_LOCAL_ISDN + 1;
10639 } else if (!strcasecmp(v->value, "dynamic")) {
10640 localdialplan = -1;
10641 } else {
10642 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
10643 }
10644 } else if (!strcasecmp(v->name, "switchtype")) {
10645 if (!strcasecmp(v->value, "national"))
10646 switchtype = PRI_SWITCH_NI2;
10647 else if (!strcasecmp(v->value, "ni1"))
10648 switchtype = PRI_SWITCH_NI1;
10649 else if (!strcasecmp(v->value, "dms100"))
10650 switchtype = PRI_SWITCH_DMS100;
10651 else if (!strcasecmp(v->value, "4ess"))
10652 switchtype = PRI_SWITCH_ATT4ESS;
10653 else if (!strcasecmp(v->value, "5ess"))
10654 switchtype = PRI_SWITCH_LUCENT5E;
10655 else if (!strcasecmp(v->value, "euroisdn"))
10656 switchtype = PRI_SWITCH_EUROISDN_E1;
10657 else if (!strcasecmp(v->value, "qsig"))
10658 switchtype = PRI_SWITCH_QSIG;
10659 else {
10660 ast_log(LOG_ERROR, "Unknown switchtype '%s'\n", v->value);
10661 ast_config_destroy(cfg);
10662 ast_mutex_unlock(&iflock);
10663 return -1;
10664 }
10665 } else if (!strcasecmp(v->name, "nsf")) {
10666 if (!strcasecmp(v->value, "sdn"))
10667 nsf = PRI_NSF_SDN;
10668 else if (!strcasecmp(v->value, "megacom"))
10669 nsf = PRI_NSF_MEGACOM;
10670 else if (!strcasecmp(v->value, "accunet"))
10671 nsf = PRI_NSF_ACCUNET;
10672 else if (!strcasecmp(v->value, "none"))
10673 nsf = PRI_NSF_NONE;
10674 else {
10675 ast_log(LOG_WARNING, "Unknown network-specific facility '%s'\n", v->value);
10676 nsf = PRI_NSF_NONE;
10677 }
10678 } else if (!strcasecmp(v->name, "priindication")) {
10679 if (!strcasecmp(v->value, "outofband"))
10680 priindication_oob = 1;
10681 else if (!strcasecmp(v->value, "inband"))
10682 priindication_oob = 0;
10683 else
10684 ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d\n",
10685 v->value, v->lineno);
10686 } else if (!strcasecmp(v->name, "priexclusive")) {
10687 cur_priexclusive = ast_true(v->value);
10688 } else if (!strcasecmp(v->name, "internationalprefix")) {
10689 ast_copy_string(internationalprefix, v->value, sizeof(internationalprefix));
10690 } else if (!strcasecmp(v->name, "nationalprefix")) {
10691 ast_copy_string(nationalprefix, v->value, sizeof(nationalprefix));
10692 } else if (!strcasecmp(v->name, "localprefix")) {
10693 ast_copy_string(localprefix, v->value, sizeof(localprefix));
10694 } else if (!strcasecmp(v->name, "privateprefix")) {
10695 ast_copy_string(privateprefix, v->value, sizeof(privateprefix));
10696 } else if (!strcasecmp(v->name, "unknownprefix")) {
10697 ast_copy_string(unknownprefix, v->value, sizeof(unknownprefix));
10698 } else if (!strcasecmp(v->name, "resetinterval")) {
10699 if (!strcasecmp(v->value, "never"))
10700 resetinterval = -1;
10701 else if( atoi(v->value) >= 60 )
10702 resetinterval = atoi(v->value);
10703 else
10704 ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d\n",
10705 v->value, v->lineno);
10706 } else if (!strcasecmp(v->name, "minunused")) {
10707 minunused = atoi(v->value);
10708 } else if (!strcasecmp(v->name, "minidle")) {
10709 minidle = atoi(v->value);
10710 } else if (!strcasecmp(v->name, "idleext")) {
10711 ast_copy_string(idleext, v->value, sizeof(idleext));
10712 } else if (!strcasecmp(v->name, "idledial")) {
10713 ast_copy_string(idledial, v->value, sizeof(idledial));
10714 } else if (!strcasecmp(v->name, "overlapdial")) {
10715 overlapdial = ast_true(v->value);
10716 } else if (!strcasecmp(v->name, "pritimer")) {
10717 #ifdef PRI_GETSET_TIMERS
10718 char *timerc;
10719 int timer, timeridx;
10720 c = v->value;
10721 timerc = strsep(&c, ",");
10722 if (timerc) {
10723 timer = atoi(c);
10724 if (!timer)
10725 ast_log(LOG_WARNING, "'%s' is not a valid value for an ISDN timer\n", timerc);
10726 else {
10727 if ((timeridx = pri_timer2idx(timerc)) >= 0)
10728 pritimers[timeridx] = timer;
10729 else
10730 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer\n", timerc);
10731 }
10732 } else
10733 ast_log(LOG_WARNING, "'%s' is not a valid ISDN timer configuration string\n", v->value);
10734
10735 } else if (!strcasecmp(v->name, "facilityenable")) {
10736 facilityenable = ast_true(v->value);
10737 #endif
10738 #endif
10739 } else if (!strcasecmp(v->name, "cadence")) {
10740
10741 int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
10742 int i;
10743 struct zt_ring_cadence new_cadence;
10744 int cid_location = -1;
10745 int firstcadencepos = 0;
10746 char original_args[80];
10747 int cadence_is_ok = 1;
10748
10749 ast_copy_string(original_args, v->value, sizeof(original_args));
10750
10751 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]);
10752
10753
10754 if (element_count % 2 == 1) {
10755 ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s\n",original_args);
10756 cadence_is_ok = 0;
10757 }
10758
10759
10760 for (i=0;i<element_count;i++) {
10761 if (c[i] == 0) {
10762 ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s\n", original_args);
10763 cadence_is_ok = 0;
10764 break;
10765 } else if (c[i] < 0) {
10766 if (i % 2 == 1) {
10767
10768 if (cid_location == -1) {
10769 cid_location = i;
10770 c[i] *= -1;
10771 } else {
10772 ast_log(LOG_ERROR, "CID location specified twice: %s\n",original_args);
10773 cadence_is_ok = 0;
10774 break;
10775 }
10776 } else {
10777 if (firstcadencepos == 0) {
10778 firstcadencepos = i;
10779
10780 } else {
10781 ast_log(LOG_ERROR, "First cadence position specified twice: %s\n",original_args);
10782 cadence_is_ok = 0;
10783 break;
10784 }
10785 }
10786 }
10787 }
10788
10789
10790 for (i=0;i<16;i++) {
10791 new_cadence.ringcadence[i] = c[i];
10792 }
10793
10794 if (cadence_is_ok) {
10795
10796 if (element_count < 2) {
10797 ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s\n", original_args);
10798 } else {
10799 if (cid_location == -1) {
10800
10801 cid_location = 1;
10802 } else {
10803
10804 cid_location = (cid_location + 1) / 2;
10805 }
10806
10807 if (!user_has_defined_cadences++)
10808
10809 num_cadence = 0;
10810 if ((num_cadence+1) >= NUM_CADENCE_MAX)
10811 ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s\n", NUM_CADENCE_MAX, original_args);
10812 else {
10813 cadences[num_cadence] = new_cadence;
10814 cidrings[num_cadence++] = cid_location;
10815 if (option_verbose > 2)
10816 ast_verbose(VERBOSE_PREFIX_3 "cadence 'r%d' added: %s\n",num_cadence,original_args);
10817 }
10818 }
10819 }
10820 } else if (!strcasecmp(v->name, "ringtimeout")) {
10821 ringt_base = (atoi(v->value) * 8) / READ_SIZE;
10822 } else if (!strcasecmp(v->name, "prewink")) {
10823 cur_prewink = atoi(v->value);
10824 } else if (!strcasecmp(v->name, "preflash")) {
10825 cur_preflash = atoi(v->value);
10826 } else if (!strcasecmp(v->name, "wink")) {
10827 cur_wink = atoi(v->value);
10828 } else if (!strcasecmp(v->name, "flash")) {
10829 cur_flash = atoi(v->value);
10830 } else if (!strcasecmp(v->name, "start")) {
10831 cur_start = atoi(v->value);
10832 } else if (!strcasecmp(v->name, "rxwink")) {
10833 cur_rxwink = atoi(v->value);
10834 } else if (!strcasecmp(v->name, "rxflash")) {
10835 cur_rxflash = atoi(v->value);
10836 } else if (!strcasecmp(v->name, "debounce")) {
10837 cur_debounce = atoi(v->value);
10838 } else if (!strcasecmp(v->name, "toneduration")) {
10839 int toneduration;
10840 int ctlfd;
10841 int res;
10842 struct zt_dialparams dps;
10843
10844 ctlfd = open("/dev/zap/ctl", O_RDWR);
10845 if (ctlfd == -1) {
10846 ast_log(LOG_ERROR, "Unable to open /dev/zap/ctl to set toneduration\n");
10847 return -1;
10848 }
10849
10850 toneduration = atoi(v->value);
10851 if (toneduration > -1) {
10852 dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration;
10853 res = ioctl(ctlfd, ZT_SET_DIALPARAMS, &dps);
10854 if (res < 0) {
10855 ast_log(LOG_ERROR, "Invalid tone duration: %d ms\n", toneduration);
10856 return -1;
10857 }
10858 }
10859 close(ctlfd);
10860 } else if (!strcasecmp(v->name, "polarityonanswerdelay")) {
10861 polarityonanswerdelay = atoi(v->value);
10862 } else if (!strcasecmp(v->name, "answeronpolarityswitch")) {
10863 answeronpolarityswitch = ast_true(v->value);
10864 } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) {
10865 hanguponpolarityswitch = ast_true(v->value);
10866 } else if (!strcasecmp(v->name, "sendcalleridafter")) {
10867 sendcalleridafter = atoi(v->value);
10868 } else if (!strcasecmp(v->name, "defaultcic")) {
10869 ast_copy_string(defaultcic, v->value, sizeof(defaultcic));
10870 } else if (!strcasecmp(v->name, "defaultozz")) {
10871 ast_copy_string(defaultozz, v->value, sizeof(defaultozz));
10872 }
10873 } else
10874 ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
10875 v = v->next;
10876 }
10877 if (!found_pseudo && reload == 0) {
10878
10879
10880
10881 cur_group = 0;
10882 cur_callergroup = 0;
10883 cur_pickupgroup = 0;
10884
10885 tmp = mkintf(CHAN_PSEUDO, cur_signalling, cur_radio, NULL, reload);
10886
10887 if (tmp) {
10888 if (option_verbose > 2)
10889 ast_verbose(VERBOSE_PREFIX_3 "Automatically generated pseudo channel\n");
10890 } else {
10891 ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
10892 }
10893 }
10894 ast_mutex_unlock(&iflock);
10895 ast_config_destroy(cfg);
10896 #ifdef ZAPATA_PRI
10897 if (!reload) {
10898 for (x=0;x<NUM_SPANS;x++) {
10899 if (pris[x].pvts[0]) {
10900 if (start_pri(pris + x)) {
10901 ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
10902 return -1;
10903 } else if (option_verbose > 1)
10904 ast_verbose(VERBOSE_PREFIX_2 "Starting D-Channel on span %d\n", x + 1);
10905 }
10906 }
10907 }
10908 #endif
10909
10910 restart_monitor();
10911 return 0;
10912 }
10913
10914 int load_module(void)
10915 {
10916 int res;
10917
10918 #ifdef ZAPATA_PRI
10919 int y,i;
10920 memset(pris, 0, sizeof(pris));
10921 for (y=0;y<NUM_SPANS;y++) {
10922 ast_mutex_init(&pris[y].lock);
10923 pris[y].offset = -1;
10924 pris[y].master = AST_PTHREADT_NULL;
10925 for (i=0;i<NUM_DCHANS;i++)
10926 pris[y].fds[i] = -1;
10927 }
10928 pri_set_error(zt_pri_error);
10929 pri_set_message(zt_pri_message);
10930 #endif
10931 res = setup_zap(0);
10932
10933 if(res) {
10934 return -1;
10935 }
10936 if (ast_channel_register(&zap_tech)) {
10937 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
10938 __unload_module();
10939 return -1;
10940 }
10941 #ifdef ZAPATA_PRI
10942 ast_cli_register_multiple(zap_pri_cli, sizeof(zap_pri_cli) / sizeof(zap_pri_cli[0]));
10943 #endif
10944 #ifdef ZAPATA_R2
10945 ast_cli_register_multiple(zap_r2_cli, sizeof(zap_r2_cli) / sizeof(zap_r2_cli[0]));
10946 #endif
10947 ast_cli_register_multiple(zap_cli, sizeof(zap_cli) / sizeof(zap_cli[0]));
10948
10949 memset(round_robin, 0, sizeof(round_robin));
10950 ast_manager_register( "ZapTransfer", 0, action_transfer, "Transfer Zap Channel" );
10951 ast_manager_register( "ZapHangup", 0, action_transferhangup, "Hangup Zap Channel" );
10952 ast_manager_register( "ZapDialOffhook", 0, action_zapdialoffhook, "Dial over Zap channel while offhook" );
10953 ast_manager_register( "ZapDNDon", 0, action_zapdndon, "Toggle Zap channel Do Not Disturb status ON" );
10954 ast_manager_register( "ZapDNDoff", 0, action_zapdndoff, "Toggle Zap channel Do Not Disturb status OFF" );
10955 ast_manager_register("ZapShowChannels", 0, action_zapshowchannels, "Show status zapata channels");
10956
10957 return res;
10958 }
10959
10960 static int zt_sendtext(struct ast_channel *c, const char *text)
10961 {
10962 #define END_SILENCE_LEN 400
10963 #define HEADER_MS 50
10964 #define TRAILER_MS 5
10965 #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
10966 #define ASCII_BYTES_PER_CHAR 80
10967
10968 unsigned char *buf,*mybuf;
10969 struct zt_pvt *p = c->tech_pvt;
10970 struct pollfd fds[1];
10971 int size,res,fd,len,x;
10972 int bytes=0;
10973
10974 float cr = 1.0;
10975 float ci = 0.0;
10976 float scont = 0.0;
10977 int index;
10978
10979 index = zt_get_index(c, p, 0);
10980 if (index < 0) {
10981 ast_log(LOG_WARNING, "Huh? I don't exist?\n");
10982 return -1;
10983 }
10984 if (!text[0]) return(0);
10985 if ((!p->tdd) && (!p->mate)) return(0);
10986 if (p->mate)
10987 buf = malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
10988 else
10989 buf = malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
10990 if (!buf) {
10991 ast_log(LOG_ERROR, "MALLOC FAILED\n");
10992 return -1;
10993 }
10994 mybuf = buf;
10995 if (p->mate) {
10996 int codec = AST_LAW(p);
10997 for (x=0;x<HEADER_MS;x++) {
10998 PUT_CLID_MARKMS;
10999 }
11000
11001 for (x=0;text[x];x++) {
11002 PUT_CLID(text[x]);
11003 }
11004 for (x=0;x<TRAILER_MS;x++) {
11005 PUT_CLID_MARKMS;
11006 }
11007 len = bytes;
11008 buf = mybuf;
11009 }
11010 else {
11011 len = tdd_generate(p->tdd,buf,text);
11012 if (len < 1) {
11013 ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n",(int)strlen(text));
11014 free(mybuf);
11015 return -1;
11016 }
11017 }
11018 memset(buf + len,0x7f,END_SILENCE_LEN);
11019 len += END_SILENCE_LEN;
11020 fd = p->subs[index].zfd;
11021 while(len) {
11022 if (ast_check_hangup(c)) {
11023 free(mybuf);
11024 return -1;
11025 }
11026 size = len;
11027 if (size > READ_SIZE)
11028 size = READ_SIZE;
11029 fds[0].fd = fd;
11030 fds[0].events = POLLOUT | POLLPRI;
11031 fds[0].revents = 0;
11032 res = poll(fds, 1, -1);
11033 if (!res) {
11034 ast_log(LOG_DEBUG, "poll (for write) ret. 0 on channel %d\n", p->channel);
11035 continue;
11036 }
11037
11038 if (fds[0].revents & POLLPRI) return -1;
11039 if (!(fds[0].revents & POLLOUT)) {
11040 ast_log(LOG_DEBUG, "write fd not ready on channel %d\n", p->channel);
11041 continue;
11042 }
11043 res = write(fd, buf, size);
11044 if (res != size) {
11045 if (res == -1) {
11046 free(mybuf);
11047 return -1;
11048 }
11049 if (option_debug)
11050 ast_log(LOG_DEBUG, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
11051 break;
11052 }
11053 len -= size;
11054 buf += size;
11055 }
11056 free(mybuf);
11057 return(0);
11058 }
11059
11060
11061 int reload(void)
11062 {
11063 int res = 0;
11064
11065 res = setup_zap(1);
11066 if (res) {
11067 ast_log(LOG_WARNING, "Reload of chan_zap.so is unsuccessful!\n");
11068 return -1;
11069 }
11070 return 0;
11071 }
11072
11073 int usecount()
11074 {
11075 return usecnt;
11076 }
11077
11078 char *description()
11079 {
11080 return (char *) desc;
11081 }
11082
11083 char *key()
11084 {
11085 return ASTERISK_GPL_KEY;
11086 }
11087