00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include "asterisk.h"
00050
00051 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 299533 $")
00052
00053 #if defined(__NetBSD__) || defined(__FreeBSD__)
00054 #include <pthread.h>
00055 #include <signal.h>
00056 #else
00057 #include <sys/signal.h>
00058 #endif
00059 #include <sys/ioctl.h>
00060 #include <math.h>
00061 #include <ctype.h>
00062
00063 #include <dahdi/user.h>
00064 #include <dahdi/tonezone.h>
00065
00066 #ifdef HAVE_PRI
00067 #include <libpri.h>
00068 #endif
00069
00070 #ifdef HAVE_SS7
00071 #include <libss7.h>
00072 #endif
00073
00074 #ifdef HAVE_OPENR2
00075 #include <openr2.h>
00076 #endif
00077
00078 #include "asterisk/lock.h"
00079 #include "asterisk/channel.h"
00080 #include "asterisk/config.h"
00081 #include "asterisk/module.h"
00082 #include "asterisk/pbx.h"
00083 #include "asterisk/file.h"
00084 #include "asterisk/ulaw.h"
00085 #include "asterisk/alaw.h"
00086 #include "asterisk/callerid.h"
00087 #include "asterisk/adsi.h"
00088 #include "asterisk/cli.h"
00089 #include "asterisk/cdr.h"
00090 #include "asterisk/features.h"
00091 #include "asterisk/musiconhold.h"
00092 #include "asterisk/say.h"
00093 #include "asterisk/tdd.h"
00094 #include "asterisk/app.h"
00095 #include "asterisk/dsp.h"
00096 #include "asterisk/astdb.h"
00097 #include "asterisk/manager.h"
00098 #include "asterisk/causes.h"
00099 #include "asterisk/term.h"
00100 #include "asterisk/utils.h"
00101 #include "asterisk/transcap.h"
00102 #include "asterisk/stringfields.h"
00103 #include "asterisk/abstract_jb.h"
00104 #include "asterisk/smdi.h"
00105 #include "asterisk/astobj.h"
00106 #include "asterisk/event.h"
00107 #include "asterisk/devicestate.h"
00108 #include "asterisk/paths.h"
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159 #define SMDI_MD_WAIT_TIMEOUT 1500
00160
00161 static const char *lbostr[] = {
00162 "0 db (CSU)/0-133 feet (DSX-1)",
00163 "133-266 feet (DSX-1)",
00164 "266-399 feet (DSX-1)",
00165 "399-533 feet (DSX-1)",
00166 "533-655 feet (DSX-1)",
00167 "-7.5db (CSU)",
00168 "-15db (CSU)",
00169 "-22.5db (CSU)"
00170 };
00171
00172
00173 static struct ast_jb_conf default_jbconf =
00174 {
00175 .flags = 0,
00176 .max_size = -1,
00177 .resync_threshold = -1,
00178 .impl = "",
00179 .target_extra = -1,
00180 };
00181 static struct ast_jb_conf global_jbconf;
00182
00183
00184 #undef SUPPORT_USERUSER
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 #define DEFAULT_CIDRINGS 1
00206
00207 #define CHANNEL_PSEUDO -12
00208
00209 #define AST_LAW(p) (((p)->law == DAHDI_LAW_ALAW) ? AST_FORMAT_ALAW : AST_FORMAT_ULAW)
00210
00211
00212
00213 #define NEED_MFDETECT(p) (((p)->sig == SIG_FEATDMF) || ((p)->sig == SIG_FEATDMF_TA) || ((p)->sig == SIG_E911) || ((p)->sig == SIG_FGC_CAMA) || ((p)->sig == SIG_FGC_CAMAMF) || ((p)->sig == SIG_FEATB))
00214
00215 static const char tdesc[] = "DAHDI Telephony Driver"
00216 #if defined(HAVE_PRI) || defined(HAVE_SS7) || defined(HAVE_OPENR2)
00217 " w/"
00218 #endif
00219 #ifdef HAVE_PRI
00220 "PRI"
00221 #endif
00222 #ifdef HAVE_SS7
00223 #ifdef HAVE_PRI
00224 " & SS7"
00225 #else
00226 "SS7"
00227 #endif
00228 #endif
00229 #ifdef HAVE_OPENR2
00230 #if defined(HAVE_PRI) || defined(HAVE_SS7)
00231 " & MFC/R2"
00232 #else
00233 "MFC/R2"
00234 #endif
00235 #endif
00236 ;
00237
00238 static const char config[] = "chan_dahdi.conf";
00239
00240 #define SIG_EM DAHDI_SIG_EM
00241 #define SIG_EMWINK (0x0100000 | DAHDI_SIG_EM)
00242 #define SIG_FEATD (0x0200000 | DAHDI_SIG_EM)
00243 #define SIG_FEATDMF (0x0400000 | DAHDI_SIG_EM)
00244 #define SIG_FEATB (0x0800000 | DAHDI_SIG_EM)
00245 #define SIG_E911 (0x1000000 | DAHDI_SIG_EM)
00246 #define SIG_FEATDMF_TA (0x2000000 | DAHDI_SIG_EM)
00247 #define SIG_FGC_CAMA (0x4000000 | DAHDI_SIG_EM)
00248 #define SIG_FGC_CAMAMF (0x8000000 | DAHDI_SIG_EM)
00249 #define SIG_FXSLS DAHDI_SIG_FXSLS
00250 #define SIG_FXSGS DAHDI_SIG_FXSGS
00251 #define SIG_FXSKS DAHDI_SIG_FXSKS
00252 #define SIG_FXOLS DAHDI_SIG_FXOLS
00253 #define SIG_FXOGS DAHDI_SIG_FXOGS
00254 #define SIG_FXOKS DAHDI_SIG_FXOKS
00255 #define SIG_PRI DAHDI_SIG_CLEAR
00256 #define SIG_BRI (0x2000000 | DAHDI_SIG_CLEAR)
00257 #define SIG_BRI_PTMP (0X4000000 | DAHDI_SIG_CLEAR)
00258 #define SIG_SS7 (0x1000000 | DAHDI_SIG_CLEAR)
00259 #define SIG_MFCR2 DAHDI_SIG_CAS
00260 #define SIG_SF DAHDI_SIG_SF
00261 #define SIG_SFWINK (0x0100000 | DAHDI_SIG_SF)
00262 #define SIG_SF_FEATD (0x0200000 | DAHDI_SIG_SF)
00263 #define SIG_SF_FEATDMF (0x0400000 | DAHDI_SIG_SF)
00264 #define SIG_SF_FEATB (0x0800000 | DAHDI_SIG_SF)
00265 #define SIG_EM_E1 DAHDI_SIG_EM_E1
00266 #define SIG_GR303FXOKS (0x0100000 | DAHDI_SIG_FXOKS)
00267 #define SIG_GR303FXSKS (0x0100000 | DAHDI_SIG_FXSKS)
00268
00269 #ifdef LOTS_OF_SPANS
00270 #define NUM_SPANS DAHDI_MAX_SPANS
00271 #else
00272 #define NUM_SPANS 32
00273 #endif
00274 #define NUM_DCHANS 4
00275 #define MAX_CHANNELS 672
00276
00277 #define CHAN_PSEUDO -2
00278
00279 #define DCHAN_PROVISIONED (1 << 0)
00280 #define DCHAN_NOTINALARM (1 << 1)
00281 #define DCHAN_UP (1 << 2)
00282
00283 #define DCHAN_AVAILABLE (DCHAN_PROVISIONED | DCHAN_NOTINALARM | DCHAN_UP)
00284
00285
00286 #define DAHDI_OVERLAPDIAL_NONE 0
00287 #define DAHDI_OVERLAPDIAL_OUTGOING 1
00288 #define DAHDI_OVERLAPDIAL_INCOMING 2
00289 #define DAHDI_OVERLAPDIAL_BOTH (DAHDI_OVERLAPDIAL_INCOMING|DAHDI_OVERLAPDIAL_OUTGOING)
00290
00291 #define CALLPROGRESS_PROGRESS 1
00292 #define CALLPROGRESS_FAX_OUTGOING 2
00293 #define CALLPROGRESS_FAX_INCOMING 4
00294 #define CALLPROGRESS_FAX (CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING)
00295
00296 static char defaultcic[64] = "";
00297 static char defaultozz[64] = "";
00298
00299 static char parkinglot[AST_MAX_EXTENSION] = "";
00300
00301
00302 static char mwimonitornotify[PATH_MAX] = "";
00303 #ifndef HAVE_DAHDI_LINEREVERSE_VMWI
00304 static int mwisend_rpas = 0;
00305 #endif
00306
00307 static char progzone[10] = "";
00308
00309 static int usedistinctiveringdetection = 0;
00310 static int distinctiveringaftercid = 0;
00311
00312 static int numbufs = 4;
00313
00314 static int mwilevel = 512;
00315
00316 #ifdef HAVE_PRI
00317 static struct ast_channel inuse;
00318 #ifdef PRI_GETSET_TIMERS
00319 static int pritimers[PRI_MAX_TIMERS];
00320 #endif
00321 static int pridebugfd = -1;
00322 static char pridebugfilename[1024] = "";
00323 #endif
00324
00325
00326 static int firstdigittimeout = 16000;
00327
00328
00329 static int gendigittimeout = 8000;
00330
00331
00332 static int matchdigittimeout = 3000;
00333
00334
00335 AST_MUTEX_DEFINE_STATIC(iflock);
00336
00337
00338 #define DAHDI_CHAN_MAPPING_PHYSICAL 0
00339 #define DAHDI_CHAN_MAPPING_LOGICAL 1
00340
00341
00342 static int ifcount = 0;
00343
00344 #ifdef HAVE_PRI
00345 AST_MUTEX_DEFINE_STATIC(pridebugfdlock);
00346 #endif
00347
00348
00349
00350 AST_MUTEX_DEFINE_STATIC(monlock);
00351
00352
00353
00354 static pthread_t monitor_thread = AST_PTHREADT_NULL;
00355 static ast_cond_t ss_thread_complete;
00356 AST_MUTEX_DEFINE_STATIC(ss_thread_lock);
00357 AST_MUTEX_DEFINE_STATIC(restart_lock);
00358 static int ss_thread_count = 0;
00359 static int num_restart_pending = 0;
00360
00361 static int restart_monitor(void);
00362
00363 static enum ast_bridge_result dahdi_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms);
00364
00365 static int dahdi_sendtext(struct ast_channel *c, const char *text);
00366
00367 static void mwi_event_cb(const struct ast_event *event, void *userdata)
00368 {
00369
00370
00371
00372
00373 }
00374
00375
00376 static inline int dahdi_get_event(int fd)
00377 {
00378 int j;
00379 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
00380 return -1;
00381 return j;
00382 }
00383
00384
00385 static inline int dahdi_wait_event(int fd)
00386 {
00387 int i, j = 0;
00388 i = DAHDI_IOMUX_SIGEVENT;
00389 if (ioctl(fd, DAHDI_IOMUX, &i) == -1)
00390 return -1;
00391 if (ioctl(fd, DAHDI_GETEVENT, &j) == -1)
00392 return -1;
00393 return j;
00394 }
00395
00396
00397 #define READ_SIZE 160
00398
00399 #define MASK_AVAIL (1 << 0)
00400 #define MASK_INUSE (1 << 1)
00401
00402 #define CALLWAITING_SILENT_SAMPLES ((300 * 8) / READ_SIZE)
00403 #define CALLWAITING_REPEAT_SAMPLES ((10000 * 8) / READ_SIZE)
00404 #define CALLWAITING_SUPPRESS_SAMPLES ((100 * 8) / READ_SIZE)
00405 #define CIDCW_EXPIRE_SAMPLES ((500 * 8) / READ_SIZE)
00406 #define MIN_MS_SINCE_FLASH ((2000) )
00407 #define DEFAULT_RINGT ((8000 * 8) / READ_SIZE)
00408
00409 struct dahdi_pvt;
00410
00411
00412
00413
00414
00415 static int ringt_base = DEFAULT_RINGT;
00416
00417 #ifdef HAVE_SS7
00418
00419 #define LINKSTATE_INALARM (1 << 0)
00420 #define LINKSTATE_STARTING (1 << 1)
00421 #define LINKSTATE_UP (1 << 2)
00422 #define LINKSTATE_DOWN (1 << 3)
00423
00424 #define SS7_NAI_DYNAMIC -1
00425
00426 #define LINKSET_FLAG_EXPLICITACM (1 << 0)
00427
00428 struct dahdi_ss7 {
00429 pthread_t master;
00430 ast_mutex_t lock;
00431 int fds[NUM_DCHANS];
00432 int numsigchans;
00433 int linkstate[NUM_DCHANS];
00434 int numchans;
00435 int type;
00436 enum {
00437 LINKSET_STATE_DOWN = 0,
00438 LINKSET_STATE_UP
00439 } state;
00440 char called_nai;
00441 char calling_nai;
00442 char internationalprefix[10];
00443 char nationalprefix[10];
00444 char subscriberprefix[20];
00445 char unknownprefix[20];
00446 struct ss7 *ss7;
00447 struct dahdi_pvt *pvts[MAX_CHANNELS];
00448 int flags;
00449 };
00450
00451 static struct dahdi_ss7 linksets[NUM_SPANS];
00452
00453 static int cur_ss7type = -1;
00454 static int cur_linkset = -1;
00455 static int cur_pointcode = -1;
00456 static int cur_cicbeginswith = -1;
00457 static int cur_adjpointcode = -1;
00458 static int cur_networkindicator = -1;
00459 static int cur_defaultdpc = -1;
00460 #endif
00461
00462 #ifdef HAVE_OPENR2
00463 struct dahdi_mfcr2 {
00464 pthread_t r2master;
00465 openr2_context_t *protocol_context;
00466 struct dahdi_pvt *pvts[MAX_CHANNELS];
00467 int numchans;
00468 int monitored_count;
00469 };
00470
00471 struct dahdi_mfcr2_conf {
00472 openr2_variant_t variant;
00473 int mfback_timeout;
00474 int metering_pulse_timeout;
00475 int max_ani;
00476 int max_dnis;
00477 signed int get_ani_first:2;
00478 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
00479 signed int skip_category_request:2;
00480 #endif
00481 unsigned int call_files:1;
00482 unsigned int allow_collect_calls:1;
00483 unsigned int charge_calls:1;
00484 unsigned int accept_on_offer:1;
00485 unsigned int forced_release:1;
00486 unsigned int double_answer:1;
00487 signed int immediate_accept:2;
00488 char logdir[OR2_MAX_PATH];
00489 char r2proto_file[OR2_MAX_PATH];
00490 openr2_log_level_t loglevel;
00491 openr2_calling_party_category_t category;
00492 };
00493
00494
00495 static struct dahdi_mfcr2 **r2links;
00496
00497 static int r2links_count = 0;
00498
00499 #endif
00500
00501 #ifdef HAVE_PRI
00502
00503 #define PVT_TO_CHANNEL(p) (((p)->prioffset) | ((p)->logicalspan << 8) | (p->pri->mastertrunkgroup ? 0x10000 : 0))
00504 #define PRI_CHANNEL(p) ((p) & 0xff)
00505 #define PRI_SPAN(p) (((p) >> 8) & 0xff)
00506 #define PRI_EXPLICIT(p) (((p) >> 16) & 0x01)
00507
00508 struct dahdi_pri {
00509 pthread_t master;
00510 ast_mutex_t lock;
00511 char idleext[AST_MAX_EXTENSION];
00512 char idlecontext[AST_MAX_CONTEXT];
00513 char idledial[AST_MAX_EXTENSION];
00514 int minunused;
00515 int minidle;
00516 int nodetype;
00517 int switchtype;
00518 int nsf;
00519 int dialplan;
00520 int localdialplan;
00521 char internationalprefix[10];
00522 char nationalprefix[10];
00523 char localprefix[20];
00524 char privateprefix[20];
00525 char unknownprefix[20];
00526 int dchannels[NUM_DCHANS];
00527 int trunkgroup;
00528 int mastertrunkgroup;
00529 int prilogicalspan;
00530 int numchans;
00531 int overlapdial;
00532 int qsigchannelmapping;
00533 int discardremoteholdretrieval;
00534 int facilityenable;
00535 struct pri *dchans[NUM_DCHANS];
00536 int dchanavail[NUM_DCHANS];
00537 struct pri *pri;
00538
00539 int debug;
00540 int fds[NUM_DCHANS];
00541
00542 int offset;
00543
00544 int span;
00545
00546 int resetting;
00547
00548 int resetpos;
00549 #ifdef HAVE_PRI_INBANDDISCONNECT
00550 unsigned int inbanddisconnect:1;
00551 #endif
00552
00553 unsigned int no_d_channels:1;
00554 time_t lastreset;
00555 long resetinterval;
00556
00557 int sig;
00558 struct dahdi_pvt *pvts[MAX_CHANNELS];
00559 struct dahdi_pvt *crvs;
00560 struct dahdi_pvt *crvend;
00561 };
00562
00563
00564 static struct dahdi_pri pris[NUM_SPANS];
00565
00566 #if 0
00567 #define DEFAULT_PRI_DEBUG (PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_STATE)
00568 #else
00569 #define DEFAULT_PRI_DEBUG 0
00570 #endif
00571
00572 static inline void pri_rel(struct dahdi_pri *pri)
00573 {
00574 ast_mutex_unlock(&pri->lock);
00575 }
00576
00577 #else
00578
00579 struct dahdi_pri;
00580 #endif
00581
00582 #define SUB_REAL 0
00583 #define SUB_CALLWAIT 1
00584 #define SUB_THREEWAY 2
00585
00586
00587 #define POLARITY_IDLE 0
00588 #define POLARITY_REV 1
00589
00590
00591 struct distRingData {
00592 int ring[3];
00593 int range;
00594 };
00595 struct ringContextData {
00596 char contextData[AST_MAX_CONTEXT];
00597 };
00598 struct dahdi_distRings {
00599 struct distRingData ringnum[3];
00600 struct ringContextData ringContext[3];
00601 };
00602
00603 static char *subnames[] = {
00604 "Real",
00605 "Callwait",
00606 "Threeway"
00607 };
00608
00609 struct dahdi_subchannel {
00610 int dfd;
00611 struct ast_channel *owner;
00612 int chan;
00613 short buffer[AST_FRIENDLY_OFFSET/2 + READ_SIZE];
00614 struct ast_frame f;
00615 unsigned int needringing:1;
00616 unsigned int needbusy:1;
00617 unsigned int needcongestion:1;
00618 unsigned int needcallerid:1;
00619 unsigned int needanswer:1;
00620 unsigned int needflash:1;
00621 unsigned int needhold:1;
00622 unsigned int needunhold:1;
00623 unsigned int linear:1;
00624 unsigned int inthreeway:1;
00625 struct dahdi_confinfo curconf;
00626 };
00627
00628 #define CONF_USER_REAL (1 << 0)
00629 #define CONF_USER_THIRDCALL (1 << 1)
00630
00631 #define MAX_SLAVES 4
00632
00633
00634
00635
00636 typedef enum {
00637 MWI_SEND_NULL = 0,
00638 MWI_SEND_SA,
00639 MWI_SEND_SA_WAIT,
00640 MWI_SEND_PAUSE,
00641 MWI_SEND_SPILL,
00642 MWI_SEND_CLEANUP,
00643 MWI_SEND_DONE,
00644 } mwisend_states;
00645
00646 struct mwisend_info {
00647 struct timeval pause;
00648 mwisend_states mwisend_current;
00649 };
00650
00651 static struct dahdi_pvt {
00652 ast_mutex_t lock;
00653 struct ast_channel *owner;
00654
00655
00656 struct dahdi_subchannel sub_unused;
00657 struct dahdi_subchannel subs[3];
00658 struct dahdi_confinfo saveconf;
00659
00660 struct dahdi_pvt *slaves[MAX_SLAVES];
00661 struct dahdi_pvt *master;
00662 int inconference;
00663
00664 int bufsize;
00665 int buf_no;
00666 int buf_policy;
00667 int faxbuf_no;
00668 int faxbuf_policy;
00669 int sig;
00670
00671
00672
00673
00674 int radio;
00675 int outsigmod;
00676 int oprmode;
00677 struct dahdi_pvt *oprpeer;
00678
00679 float cid_rxgain;
00680
00681 float rxgain;
00682
00683 float txgain;
00684 int tonezone;
00685 struct dahdi_pvt *next;
00686 struct dahdi_pvt *prev;
00687
00688
00689
00690
00691
00692
00693
00694 unsigned int adsi:1;
00695
00696
00697
00698
00699
00700 unsigned int answeronpolarityswitch:1;
00701
00702
00703
00704
00705
00706 unsigned int busydetect:1;
00707
00708
00709
00710
00711
00712 unsigned int callreturn:1;
00713
00714
00715
00716
00717
00718
00719 unsigned int callwaiting:1;
00720
00721
00722
00723
00724 unsigned int callwaitingcallerid:1;
00725
00726
00727
00728
00729
00730
00731 unsigned int cancallforward:1;
00732
00733
00734
00735
00736 unsigned int canpark:1;
00737
00738 unsigned int confirmanswer:1;
00739
00740
00741
00742
00743 unsigned int destroy:1;
00744 unsigned int didtdd:1;
00745
00746 unsigned int dialednone:1;
00747
00748 unsigned int dialing:1;
00749
00750 unsigned int digital:1;
00751
00752 unsigned int dnd:1;
00753
00754 unsigned int echobreak:1;
00755
00756
00757
00758
00759
00760 unsigned int echocanbridged:1;
00761
00762 unsigned int echocanon:1;
00763
00764 unsigned int faxhandled:1;
00765
00766 unsigned int usefaxbuffers:1;
00767
00768 unsigned int bufferoverrideinuse:1;
00769
00770 unsigned int firstradio:1;
00771
00772
00773
00774
00775 unsigned int hanguponpolarityswitch:1;
00776
00777 unsigned int hardwaredtmf:1;
00778
00779
00780
00781
00782
00783
00784 unsigned int hidecallerid:1;
00785
00786
00787
00788
00789
00790 unsigned int hidecalleridname:1;
00791
00792 unsigned int ignoredtmf:1;
00793
00794
00795
00796
00797
00798 unsigned int immediate:1;
00799
00800 unsigned int inalarm:1;
00801
00802 unsigned int mate:1;
00803
00804 unsigned int outgoing:1;
00805
00806
00807
00808
00809
00810
00811 unsigned int permcallwaiting:1;
00812
00813
00814
00815
00816 unsigned int permhidecallerid:1;
00817
00818
00819
00820
00821 unsigned int priindication_oob:1;
00822
00823
00824
00825
00826 unsigned int priexclusive:1;
00827
00828
00829
00830
00831 unsigned int pulse:1;
00832
00833 unsigned int pulsedial:1;
00834 unsigned int restartpending:1;
00835
00836
00837
00838
00839
00840 unsigned int restrictcid:1;
00841
00842
00843
00844
00845 unsigned int threewaycalling:1;
00846
00847
00848
00849
00850
00851
00852
00853
00854 unsigned int transfer:1;
00855
00856
00857
00858
00859
00860
00861
00862 unsigned int use_callerid:1;
00863
00864
00865
00866
00867
00868
00869 unsigned int use_callingpres:1;
00870
00871
00872
00873
00874
00875 unsigned int usedistinctiveringdetection:1;
00876
00877
00878
00879
00880 unsigned int dahditrcallerid:1;
00881
00882
00883
00884
00885 unsigned int transfertobusy:1;
00886
00887
00888
00889
00890 unsigned int mwimonitor_neon:1;
00891
00892
00893
00894
00895 unsigned int mwimonitor_fsk:1;
00896
00897
00898
00899
00900
00901 unsigned int mwimonitor_rpas:1;
00902
00903 unsigned int mwimonitoractive:1;
00904
00905 unsigned int mwisendactive:1;
00906
00907
00908
00909
00910 unsigned int inservice:1;
00911
00912
00913
00914
00915 unsigned int locallyblocked:1;
00916
00917
00918
00919
00920 unsigned int remotelyblocked:1;
00921 #if defined(HAVE_PRI) || defined(HAVE_SS7)
00922
00923
00924
00925
00926 unsigned int rlt:1;
00927
00928 unsigned int alerting:1;
00929
00930 unsigned int alreadyhungup:1;
00931
00932
00933
00934
00935 unsigned int isidlecall:1;
00936
00937
00938
00939
00940 unsigned int proceeding:1;
00941
00942 unsigned int progress:1;
00943
00944
00945
00946
00947 unsigned int resetting:1;
00948
00949
00950
00951
00952 unsigned int setup_ack:1;
00953 #endif
00954
00955
00956
00957
00958 unsigned int use_smdi:1;
00959 struct mwisend_info mwisend_data;
00960
00961 struct ast_smdi_interface *smdi_iface;
00962
00963
00964 struct dahdi_distRings drings;
00965
00966
00967
00968
00969
00970 char context[AST_MAX_CONTEXT];
00971
00972
00973
00974 char defcontext[AST_MAX_CONTEXT];
00975
00976 char exten[AST_MAX_EXTENSION];
00977
00978
00979
00980
00981 char language[MAX_LANGUAGE];
00982
00983
00984
00985
00986 char mohinterpret[MAX_MUSICCLASS];
00987
00988
00989
00990
00991 char mohsuggest[MAX_MUSICCLASS];
00992 char parkinglot[AST_MAX_EXTENSION];
00993 #if defined(PRI_ANI) || defined(HAVE_SS7)
00994
00995 char cid_ani[AST_MAX_EXTENSION];
00996 #endif
00997
00998 int cid_ani2;
00999
01000 char cid_num[AST_MAX_EXTENSION];
01001
01002 int cid_ton;
01003
01004 char cid_name[AST_MAX_EXTENSION];
01005
01006 char lastcid_num[AST_MAX_EXTENSION];
01007
01008 char lastcid_name[AST_MAX_EXTENSION];
01009 char *origcid_num;
01010 char *origcid_name;
01011
01012 char callwait_num[AST_MAX_EXTENSION];
01013
01014 char callwait_name[AST_MAX_EXTENSION];
01015
01016 char rdnis[AST_MAX_EXTENSION];
01017
01018 char dnid[AST_MAX_EXTENSION];
01019
01020
01021
01022
01023 ast_group_t group;
01024
01025 int law;
01026 int confno;
01027 int confusers;
01028 int propconfno;
01029
01030
01031
01032
01033 ast_group_t callgroup;
01034
01035
01036
01037
01038 ast_group_t pickupgroup;
01039
01040
01041
01042
01043 struct ast_variable *vars;
01044 int channel;
01045 int span;
01046 time_t guardtime;
01047 int cid_signalling;
01048 int cid_start;
01049 int callingpres;
01050 int callwaitingrepeat;
01051 int cidcwexpire;
01052 int cid_suppress_expire;
01053
01054 unsigned char *cidspill;
01055
01056 int cidpos;
01057
01058 int cidlen;
01059
01060 int ringt;
01061
01062
01063
01064
01065 int ringt_base;
01066
01067
01068
01069
01070
01071
01072 int stripmsd;
01073
01074
01075
01076
01077
01078
01079 int callwaitcas;
01080
01081 int callwaitrings;
01082
01083 struct {
01084 struct dahdi_echocanparams head;
01085 struct dahdi_echocanparam params[DAHDI_MAX_ECHOCANPARAMS];
01086 } echocancel;
01087
01088
01089
01090
01091 int echotraining;
01092
01093 char echorest[20];
01094
01095
01096
01097
01098 int busycount;
01099
01100
01101
01102
01103 int busy_tonelength;
01104
01105
01106
01107
01108 int busy_quietlength;
01109
01110
01111
01112
01113 int callprogress;
01114
01115
01116
01117
01118 int waitfordialtone;
01119 struct timeval waitingfordt;
01120 struct timeval flashtime;
01121
01122 struct ast_dsp *dsp;
01123
01124
01125 struct dahdi_dialoperation dop;
01126 int whichwink;
01127
01128 char finaldial[64];
01129 char accountcode[AST_MAX_ACCOUNT_CODE];
01130 int amaflags;
01131 struct tdd_state *tdd;
01132
01133 char call_forward[AST_MAX_EXTENSION];
01134
01135
01136
01137
01138 char mailbox[AST_MAX_EXTENSION];
01139
01140 struct ast_event_sub *mwi_event_sub;
01141
01142 char dialdest[256];
01143
01144 int onhooktime;
01145
01146 int fxsoffhookstate;
01147
01148 int msgstate;
01149 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
01150 struct dahdi_vmwi_info mwisend_setting;
01151 unsigned int mwisend_fsk: 1;
01152 unsigned int mwisend_rpas:1;
01153 #endif
01154 int distinctivering;
01155 int cidrings;
01156 int dtmfrelax;
01157
01158 int fake_event;
01159
01160
01161
01162
01163 int polarityonanswerdelay;
01164
01165 struct timeval polaritydelaytv;
01166
01167
01168
01169
01170 int sendcalleridafter;
01171 #ifdef HAVE_PRI
01172
01173 struct dahdi_pri *pri;
01174
01175 struct dahdi_pvt *bearer;
01176
01177 struct dahdi_pvt *realcall;
01178
01179 q931_call *call;
01180
01181 int prioffset;
01182
01183 int logicalspan;
01184 #endif
01185
01186 int polarity;
01187
01188 int dsp_features;
01189 #ifdef HAVE_SS7
01190
01191 struct dahdi_ss7 *ss7;
01192
01193 struct isup_call *ss7call;
01194 char charge_number[50];
01195 char gen_add_number[50];
01196 char gen_dig_number[50];
01197 char orig_called_num[50];
01198 char redirecting_num[50];
01199 char generic_name[50];
01200 unsigned char gen_add_num_plan;
01201 unsigned char gen_add_nai;
01202 unsigned char gen_add_pres_ind;
01203 unsigned char gen_add_type;
01204 unsigned char gen_dig_type;
01205 unsigned char gen_dig_scheme;
01206 char jip_number[50];
01207 unsigned char lspi_type;
01208 unsigned char lspi_scheme;
01209 unsigned char lspi_context;
01210 char lspi_ident[50];
01211 unsigned int call_ref_ident;
01212 unsigned int call_ref_pc;
01213 unsigned char calling_party_cat;
01214 int transcap;
01215 int cic;
01216 unsigned int dpc;
01217 unsigned int loopedback:1;
01218 #endif
01219 #ifdef HAVE_OPENR2
01220 struct dahdi_mfcr2 *mfcr2;
01221 openr2_chan_t *r2chan;
01222 openr2_calling_party_category_t mfcr2_recvd_category;
01223 openr2_calling_party_category_t mfcr2_category;
01224 int mfcr2_dnis_index;
01225 int mfcr2_ani_index;
01226 int mfcr2call:1;
01227 int mfcr2_answer_pending:1;
01228 int mfcr2_charge_calls:1;
01229 int mfcr2_allow_collect_calls:1;
01230 int mfcr2_forced_release:1;
01231 int mfcr2_dnis_matched:1;
01232 int mfcr2_call_accepted:1;
01233 int mfcr2_progress:1;
01234 int mfcr2_accept_on_offer:1;
01235 #endif
01236
01237 char begindigit;
01238
01239 int muting;
01240 } *iflist = NULL, *ifend = NULL;
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252 struct dahdi_chan_conf {
01253 struct dahdi_pvt chan;
01254 #ifdef HAVE_PRI
01255 struct dahdi_pri pri;
01256 #endif
01257
01258 #ifdef HAVE_SS7
01259 struct dahdi_ss7 ss7;
01260 #endif
01261
01262 #ifdef HAVE_OPENR2
01263 struct dahdi_mfcr2_conf mfcr2;
01264 #endif
01265 struct dahdi_params timing;
01266 int is_sig_auto;
01267
01268
01269
01270
01271
01272 char smdi_port[SMDI_MAX_FILENAME_LEN];
01273 };
01274
01275
01276 static struct dahdi_chan_conf dahdi_chan_conf_default(void)
01277 {
01278
01279
01280
01281 struct dahdi_chan_conf conf = {
01282 #ifdef HAVE_PRI
01283 .pri = {
01284 .nsf = PRI_NSF_NONE,
01285 .switchtype = PRI_SWITCH_NI2,
01286 .dialplan = PRI_UNKNOWN + 1,
01287 .localdialplan = PRI_NATIONAL_ISDN + 1,
01288 .nodetype = PRI_CPE,
01289 .qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL,
01290
01291 .minunused = 2,
01292 .idleext = "",
01293 .idledial = "",
01294 .internationalprefix = "",
01295 .nationalprefix = "",
01296 .localprefix = "",
01297 .privateprefix = "",
01298 .unknownprefix = "",
01299 .resetinterval = -1,
01300 },
01301 #endif
01302 #ifdef HAVE_SS7
01303 .ss7 = {
01304 .called_nai = SS7_NAI_NATIONAL,
01305 .calling_nai = SS7_NAI_NATIONAL,
01306 .internationalprefix = "",
01307 .nationalprefix = "",
01308 .subscriberprefix = "",
01309 .unknownprefix = ""
01310 },
01311 #endif
01312 #ifdef HAVE_OPENR2
01313 .mfcr2 = {
01314 .variant = OR2_VAR_ITU,
01315 .mfback_timeout = -1,
01316 .metering_pulse_timeout = -1,
01317 .max_ani = 10,
01318 .max_dnis = 4,
01319 .get_ani_first = -1,
01320 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
01321 .skip_category_request = -1,
01322 #endif
01323 .call_files = 0,
01324 .allow_collect_calls = 0,
01325 .charge_calls = 1,
01326 .accept_on_offer = 1,
01327 .forced_release = 0,
01328 .double_answer = 0,
01329 .immediate_accept = -1,
01330 .logdir = "",
01331 .r2proto_file = "",
01332 .loglevel = OR2_LOG_ERROR | OR2_LOG_WARNING,
01333 .category = OR2_CALLING_PARTY_CATEGORY_NATIONAL_SUBSCRIBER
01334 },
01335 #endif
01336 .chan = {
01337 .context = "default",
01338 .cid_num = "",
01339 .cid_name = "",
01340 .mohinterpret = "default",
01341 .mohsuggest = "",
01342 .parkinglot = "",
01343 .transfertobusy = 1,
01344
01345 .cid_signalling = CID_SIG_BELL,
01346 .cid_start = CID_START_RING,
01347 .dahditrcallerid = 0,
01348 .use_callerid = 1,
01349 .sig = -1,
01350 .outsigmod = -1,
01351
01352 .cid_rxgain = +5.0,
01353
01354 .tonezone = -1,
01355
01356 .echocancel.head.tap_length = 1,
01357
01358 .busycount = 3,
01359
01360 .accountcode = "",
01361
01362 .mailbox = "",
01363
01364 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
01365 .mwisend_fsk = 1,
01366 #endif
01367 .polarityonanswerdelay = 600,
01368
01369 .sendcalleridafter = DEFAULT_CIDRINGS,
01370
01371 .buf_policy = DAHDI_POLICY_IMMEDIATE,
01372 .buf_no = numbufs,
01373 .usefaxbuffers = 0,
01374 .faxbuf_policy = DAHDI_POLICY_IMMEDIATE,
01375 .faxbuf_no = numbufs,
01376 },
01377 .timing = {
01378 .prewinktime = -1,
01379 .preflashtime = -1,
01380 .winktime = -1,
01381 .flashtime = -1,
01382 .starttime = -1,
01383 .rxwinktime = -1,
01384 .rxflashtime = -1,
01385 .debouncetime = -1
01386 },
01387 .is_sig_auto = 1,
01388 .smdi_port = "/dev/ttyS0",
01389 };
01390
01391 return conf;
01392 }
01393
01394
01395 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause);
01396 static int dahdi_digit_begin(struct ast_channel *ast, char digit);
01397 static int dahdi_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
01398 static int dahdi_sendtext(struct ast_channel *c, const char *text);
01399 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout);
01400 static int dahdi_hangup(struct ast_channel *ast);
01401 static int dahdi_answer(struct ast_channel *ast);
01402 static struct ast_frame *dahdi_read(struct ast_channel *ast);
01403 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame);
01404 static struct ast_frame *dahdi_exception(struct ast_channel *ast);
01405 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen);
01406 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
01407 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen);
01408 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len);
01409 static int dahdi_func_write(struct ast_channel *chan, const char *function, char *data, const char *value);
01410 static struct dahdi_pvt *handle_init_event(struct dahdi_pvt *i, int event);
01411
01412 static const struct ast_channel_tech dahdi_tech = {
01413 .type = "DAHDI",
01414 .description = tdesc,
01415 .capabilities = AST_FORMAT_SLINEAR | AST_FORMAT_ULAW | AST_FORMAT_ALAW,
01416 .requester = dahdi_request,
01417 .send_digit_begin = dahdi_digit_begin,
01418 .send_digit_end = dahdi_digit_end,
01419 .send_text = dahdi_sendtext,
01420 .call = dahdi_call,
01421 .hangup = dahdi_hangup,
01422 .answer = dahdi_answer,
01423 .read = dahdi_read,
01424 .write = dahdi_write,
01425 .bridge = dahdi_bridge,
01426 .exception = dahdi_exception,
01427 .indicate = dahdi_indicate,
01428 .fixup = dahdi_fixup,
01429 .setoption = dahdi_setoption,
01430 .func_channel_read = dahdi_func_read,
01431 .func_channel_write = dahdi_func_write,
01432 };
01433
01434 #ifdef HAVE_PRI
01435 #define GET_CHANNEL(p) ((p)->bearer ? (p)->bearer->channel : p->channel)
01436 #else
01437 #define GET_CHANNEL(p) ((p)->channel)
01438 #endif
01439
01440 struct dahdi_pvt *round_robin[32];
01441
01442 #if defined(HAVE_PRI)
01443 static inline int pri_grab(struct dahdi_pvt *pvt, struct dahdi_pri *pri)
01444 {
01445 int res;
01446
01447 do {
01448 res = ast_mutex_trylock(&pri->lock);
01449 if (res) {
01450 DEADLOCK_AVOIDANCE(&pvt->lock);
01451 }
01452 } while (res);
01453
01454 if (pri->master != AST_PTHREADT_NULL)
01455 pthread_kill(pri->master, SIGURG);
01456 return 0;
01457 }
01458 #endif
01459
01460 #if defined(HAVE_SS7)
01461 static inline void ss7_rel(struct dahdi_ss7 *ss7)
01462 {
01463 ast_mutex_unlock(&ss7->lock);
01464 }
01465 #endif
01466
01467 #if defined(HAVE_SS7)
01468 static inline int ss7_grab(struct dahdi_pvt *pvt, struct dahdi_ss7 *pri)
01469 {
01470 int res;
01471
01472 do {
01473 res = ast_mutex_trylock(&pri->lock);
01474 if (res) {
01475 DEADLOCK_AVOIDANCE(&pvt->lock);
01476 }
01477 } while (res);
01478
01479 if (pri->master != AST_PTHREADT_NULL)
01480 pthread_kill(pri->master, SIGURG);
01481 return 0;
01482 }
01483 #endif
01484 #define NUM_CADENCE_MAX 25
01485 static int num_cadence = 4;
01486 static int user_has_defined_cadences = 0;
01487
01488 static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX] = {
01489 { { 125, 125, 2000, 4000 } },
01490 { { 250, 250, 500, 1000, 250, 250, 500, 4000 } },
01491 { { 125, 125, 125, 125, 125, 4000 } },
01492 { { 1000, 500, 2500, 5000 } },
01493 };
01494
01495
01496
01497
01498
01499 static int cidrings[NUM_CADENCE_MAX] = {
01500 2,
01501 4,
01502 3,
01503 2,
01504 };
01505
01506
01507 static struct dahdi_ring_cadence AS_RP_cadence = {{250, 10000}};
01508
01509 #define ISTRUNK(p) ((p->sig == SIG_FXSLS) || (p->sig == SIG_FXSKS) || \
01510 (p->sig == SIG_FXSGS) || (p->sig == SIG_PRI))
01511
01512 #define CANBUSYDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
01513 #define CANPROGRESSDETECT(p) (ISTRUNK(p) || (p->sig & (SIG_EM | SIG_EM_E1 | SIG_SF)) )
01514
01515 #define dahdi_get_index(ast, p, nullok) _dahdi_get_index(ast, p, nullok, __PRETTY_FUNCTION__, __LINE__)
01516 static int _dahdi_get_index(struct ast_channel *ast, struct dahdi_pvt *p, int nullok, const char *fname, unsigned long line)
01517 {
01518 int res;
01519 if (p->subs[SUB_REAL].owner == ast)
01520 res = 0;
01521 else if (p->subs[SUB_CALLWAIT].owner == ast)
01522 res = 1;
01523 else if (p->subs[SUB_THREEWAY].owner == ast)
01524 res = 2;
01525 else {
01526 res = -1;
01527 if (!nullok)
01528 ast_log(LOG_WARNING,
01529 "Unable to get index for '%s' on channel %d (%s(), line %lu)\n",
01530 ast ? ast->name : "", p->channel, fname, line);
01531 }
01532 return res;
01533 }
01534
01535
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548
01549
01550 static void dahdi_lock_sub_owner(struct dahdi_pvt *pvt, int sub_idx)
01551 {
01552 for (;;) {
01553 if (!pvt->subs[sub_idx].owner) {
01554
01555 break;
01556 }
01557 if (!ast_channel_trylock(pvt->subs[sub_idx].owner)) {
01558
01559 break;
01560 }
01561
01562 DEADLOCK_AVOIDANCE(&pvt->lock);
01563 }
01564 }
01565
01566 static void wakeup_sub(struct dahdi_pvt *p, int a, struct dahdi_pri *pri)
01567 {
01568 #ifdef HAVE_PRI
01569 if (pri)
01570 ast_mutex_unlock(&pri->lock);
01571 #endif
01572 dahdi_lock_sub_owner(p, a);
01573 if (p->subs[a].owner) {
01574 ast_queue_frame(p->subs[a].owner, &ast_null_frame);
01575 ast_channel_unlock(p->subs[a].owner);
01576 }
01577 #ifdef HAVE_PRI
01578 if (pri)
01579 ast_mutex_lock(&pri->lock);
01580 #endif
01581 }
01582
01583 static void dahdi_queue_frame(struct dahdi_pvt *p, struct ast_frame *f, void *data)
01584 {
01585 #ifdef HAVE_PRI
01586 struct dahdi_pri *pri = (struct dahdi_pri*) data;
01587 #endif
01588 #ifdef HAVE_SS7
01589 struct dahdi_ss7 *ss7 = (struct dahdi_ss7*) data;
01590 #endif
01591
01592 #if defined(HAVE_PRI) || defined(HAVE_SS7)
01593 if (data) {
01594 switch (p->sig) {
01595 #ifdef HAVE_PRI
01596 case SIG_BRI:
01597 case SIG_BRI_PTMP:
01598 case SIG_PRI:
01599 ast_mutex_unlock(&pri->lock);
01600 break;
01601 #endif
01602 #ifdef HAVE_SS7
01603 case SIG_SS7:
01604 ast_mutex_unlock(&ss7->lock);
01605 break;
01606 #endif
01607 default:
01608 break;
01609 }
01610 }
01611 #endif
01612 for (;;) {
01613 if (p->owner) {
01614 if (ast_channel_trylock(p->owner)) {
01615 DEADLOCK_AVOIDANCE(&p->lock);
01616 } else {
01617 ast_queue_frame(p->owner, f);
01618 ast_channel_unlock(p->owner);
01619 break;
01620 }
01621 } else
01622 break;
01623 }
01624 #if defined(HAVE_PRI) || defined(HAVE_SS7)
01625 if (data) {
01626 switch (p->sig) {
01627 #ifdef HAVE_PRI
01628 case SIG_BRI:
01629 case SIG_BRI_PTMP:
01630 case SIG_PRI:
01631 ast_mutex_lock(&pri->lock);
01632 break;
01633 #endif
01634 #ifdef HAVE_SS7
01635 case SIG_SS7:
01636 ast_mutex_lock(&ss7->lock);
01637 break;
01638 #endif
01639 default:
01640 break;
01641 }
01642 }
01643 #endif
01644 }
01645
01646 static struct ast_channel *dahdi_new(struct dahdi_pvt *, int, int, int, int, int);
01647 #ifdef HAVE_OPENR2
01648
01649 static int dahdi_r2_answer(struct dahdi_pvt *p)
01650 {
01651 int res = 0;
01652
01653
01654
01655 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
01656 const char *double_answer = pbx_builtin_getvar_helper(p->owner, "MFCR2_DOUBLE_ANSWER");
01657 int wants_double_answer = ast_true(double_answer) ? 1 : 0;
01658 if (!double_answer) {
01659
01660
01661 res = openr2_chan_answer_call(p->r2chan);
01662 } else if (wants_double_answer) {
01663 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_DOUBLE);
01664 } else {
01665 res = openr2_chan_answer_call_with_mode(p->r2chan, OR2_ANSWER_SIMPLE);
01666 }
01667 #else
01668 res = openr2_chan_answer_call(p->r2chan);
01669 #endif
01670 return res;
01671 }
01672
01673
01674
01675
01676 static openr2_calling_party_category_t dahdi_r2_get_channel_category(struct ast_channel *c)
01677 {
01678 openr2_calling_party_category_t cat;
01679 const char *catstr = pbx_builtin_getvar_helper(c, "MFCR2_CATEGORY");
01680 struct dahdi_pvt *p = c->tech_pvt;
01681 if (ast_strlen_zero(catstr)) {
01682 ast_debug(1, "No MFC/R2 category specified for chan %s, using default %s\n",
01683 c->name, openr2_proto_get_category_string(p->mfcr2_category));
01684 return p->mfcr2_category;
01685 }
01686 if ((cat = openr2_proto_get_category(catstr)) == OR2_CALLING_PARTY_CATEGORY_UNKNOWN) {
01687 ast_log(LOG_WARNING, "Invalid category specified '%s' for chan %s, using default %s\n",
01688 catstr, c->name, openr2_proto_get_category_string(p->mfcr2_category));
01689 return p->mfcr2_category;
01690 }
01691 ast_debug(1, "Using category %s\n", catstr);
01692 return cat;
01693 }
01694
01695 static void dahdi_r2_on_call_init(openr2_chan_t *r2chan)
01696 {
01697 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01698 ast_mutex_lock(&p->lock);
01699 if (p->mfcr2call) {
01700 ast_mutex_unlock(&p->lock);
01701
01702
01703
01704
01705
01706 ast_log(LOG_ERROR, "Collision of calls on chan %d detected!.\n", openr2_chan_get_number(r2chan));
01707 return;
01708 }
01709 p->mfcr2call = 1;
01710
01711 p->cid_name[0] = '\0';
01712 p->cid_num[0] = '\0';
01713 p->rdnis[0] = '\0';
01714 p->exten[0] = '\0';
01715 p->mfcr2_ani_index = '\0';
01716 p->mfcr2_dnis_index = '\0';
01717 p->mfcr2_dnis_matched = 0;
01718 p->mfcr2_answer_pending = 0;
01719 p->mfcr2_call_accepted = 0;
01720 ast_mutex_unlock(&p->lock);
01721 ast_verbose("New MFC/R2 call detected on chan %d.\n", openr2_chan_get_number(r2chan));
01722 }
01723
01724 static int get_alarms(struct dahdi_pvt *p);
01725 static void handle_alarms(struct dahdi_pvt *p, int alms);
01726 static void dahdi_r2_on_hardware_alarm(openr2_chan_t *r2chan, int alarm)
01727 {
01728 int res;
01729 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01730 ast_mutex_lock(&p->lock);
01731 p->inalarm = alarm ? 1 : 0;
01732 if (p->inalarm) {
01733 res = get_alarms(p);
01734 handle_alarms(p, res);
01735 } else {
01736 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
01737 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear", "Channel: %d\r\n", p->channel);
01738 }
01739 ast_mutex_unlock(&p->lock);
01740 }
01741
01742 static void dahdi_r2_on_os_error(openr2_chan_t *r2chan, int errorcode)
01743 {
01744 ast_log(LOG_ERROR, "OS error on chan %d: %s\n", openr2_chan_get_number(r2chan), strerror(errorcode));
01745 }
01746
01747 static void dahdi_r2_on_protocol_error(openr2_chan_t *r2chan, openr2_protocol_error_t reason)
01748 {
01749 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01750 ast_log(LOG_ERROR, "MFC/R2 protocol error on chan %d: %s\n", openr2_chan_get_number(r2chan), openr2_proto_get_error(reason));
01751 if (p->owner) {
01752 p->owner->hangupcause = AST_CAUSE_PROTOCOL_ERROR;
01753 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01754 }
01755 ast_mutex_lock(&p->lock);
01756 p->mfcr2call = 0;
01757 ast_mutex_unlock(&p->lock);
01758 }
01759
01760 static void dahdi_r2_disconnect_call(struct dahdi_pvt *p, openr2_call_disconnect_cause_t cause)
01761 {
01762 if (openr2_chan_disconnect_call(p->r2chan, cause)) {
01763 ast_log(LOG_NOTICE, "Bad! failed to disconnect call on channel %d with reason %s, hope for the best!\n",
01764 p->channel, openr2_proto_get_disconnect_string(cause));
01765
01766 openr2_chan_set_idle(p->r2chan);
01767 ast_mutex_lock(&p->lock);
01768 p->mfcr2call = 0;
01769 ast_mutex_unlock(&p->lock);
01770 }
01771 }
01772
01773 static void dahdi_r2_on_call_offered(openr2_chan_t *r2chan, const char *ani, const char *dnis, openr2_calling_party_category_t category)
01774 {
01775 struct dahdi_pvt *p;
01776 struct ast_channel *c;
01777 ast_verbose("MFC/R2 call offered on chan %d. ANI = %s, DNIS = %s, Category = %s\n",
01778 openr2_chan_get_number(r2chan), ani ? ani : "(restricted)", dnis,
01779 openr2_proto_get_category_string(category));
01780 p = openr2_chan_get_client_data(r2chan);
01781
01782 if (!p->mfcr2_allow_collect_calls && category == OR2_CALLING_PARTY_CATEGORY_COLLECT_CALL) {
01783 ast_log(LOG_NOTICE, "Rejecting MFC/R2 collect call\n");
01784 dahdi_r2_disconnect_call(p, OR2_CAUSE_COLLECT_CALL_REJECTED);
01785 return;
01786 }
01787 ast_mutex_lock(&p->lock);
01788 p->mfcr2_recvd_category = category;
01789
01790 if (!p->use_callerid) {
01791 ast_log(LOG_DEBUG, "No CID allowed in configuration, CID is being cleared!\n");
01792 p->cid_num[0] = 0;
01793 p->cid_name[0] = 0;
01794 }
01795
01796 if (p->immediate || !openr2_context_get_max_dnis(openr2_chan_get_context(r2chan))) {
01797 ast_log(LOG_DEBUG, "Setting exten => s because of immediate or 0 DNIS configured\n");
01798 p->exten[0] = 's';
01799 p->exten[1] = 0;
01800 }
01801 ast_mutex_unlock(&p->lock);
01802 if (!ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
01803 ast_log(LOG_NOTICE, "MFC/R2 call on channel %d requested non-existent extension '%s' in context '%s'. Rejecting call.\n",
01804 p->channel, p->exten, p->context);
01805 dahdi_r2_disconnect_call(p, OR2_CAUSE_UNALLOCATED_NUMBER);
01806 return;
01807 }
01808 if (!p->mfcr2_accept_on_offer) {
01809
01810 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0);
01811 if (c) {
01812
01813
01814
01815 return;
01816 }
01817 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
01818 dahdi_r2_disconnect_call(p, OR2_CAUSE_OUT_OF_ORDER);
01819 } else if (p->mfcr2_charge_calls) {
01820 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with charge on chan %d\n", p->channel);
01821 openr2_chan_accept_call(r2chan, OR2_CALL_WITH_CHARGE);
01822 } else {
01823 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with no charge on chan %d\n", p->channel);
01824 openr2_chan_accept_call(r2chan, OR2_CALL_NO_CHARGE);
01825 }
01826 }
01827
01828 static void dahdi_r2_on_call_end(openr2_chan_t *r2chan)
01829 {
01830 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01831 ast_verbose("MFC/R2 call end on channel %d\n", p->channel);
01832 ast_mutex_lock(&p->lock);
01833 p->mfcr2call = 0;
01834 ast_mutex_unlock(&p->lock);
01835 }
01836
01837 static void dahdi_enable_ec(struct dahdi_pvt *p);
01838 static void dahdi_r2_on_call_accepted(openr2_chan_t *r2chan, openr2_call_mode_t mode)
01839 {
01840 struct dahdi_pvt *p = NULL;
01841 struct ast_channel *c = NULL;
01842 p = openr2_chan_get_client_data(r2chan);
01843 dahdi_enable_ec(p);
01844 p->mfcr2_call_accepted = 1;
01845
01846 if (OR2_DIR_BACKWARD == openr2_chan_get_direction(r2chan)) {
01847 ast_verbose("MFC/R2 call has been accepted on backward channel %d\n", openr2_chan_get_number(r2chan));
01848
01849
01850
01851
01852 if (!p->mfcr2_accept_on_offer) {
01853 openr2_chan_disable_read(r2chan);
01854 if (p->mfcr2_answer_pending) {
01855 ast_log(LOG_DEBUG, "Answering MFC/R2 call after accepting it on chan %d\n", openr2_chan_get_number(r2chan));
01856 dahdi_r2_answer(p);
01857 }
01858 return;
01859 }
01860 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, DAHDI_LAW_ALAW, 0);
01861 if (c) {
01862
01863
01864 openr2_chan_disable_read(r2chan);
01865 return;
01866 }
01867 ast_log(LOG_WARNING, "Unable to create PBX channel in DAHDI channel %d\n", p->channel);
01868
01869 dahdi_r2_disconnect_call(p, OR2_CAUSE_OUT_OF_ORDER);
01870 return;
01871 }
01872
01873 ast_verbose("MFC/R2 call has been accepted on forward channel %d\n", p->channel);
01874 p->subs[SUB_REAL].needringing = 1;
01875 p->dialing = 0;
01876
01877 openr2_chan_disable_read(r2chan);
01878 }
01879
01880 static void dahdi_r2_on_call_answered(openr2_chan_t *r2chan)
01881 {
01882 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01883 ast_verbose("MFC/R2 call has been answered on channel %d\n", openr2_chan_get_number(r2chan));
01884 p->subs[SUB_REAL].needanswer = 1;
01885 }
01886
01887 static void dahdi_r2_on_call_read(openr2_chan_t *r2chan, const unsigned char *buf, int buflen)
01888 {
01889
01890 }
01891
01892 static int dahdi_r2_cause_to_ast_cause(openr2_call_disconnect_cause_t cause)
01893 {
01894 switch (cause) {
01895 case OR2_CAUSE_BUSY_NUMBER:
01896 return AST_CAUSE_BUSY;
01897 case OR2_CAUSE_NETWORK_CONGESTION:
01898 return AST_CAUSE_CONGESTION;
01899 case OR2_CAUSE_OUT_OF_ORDER:
01900 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
01901 case OR2_CAUSE_UNALLOCATED_NUMBER:
01902 return AST_CAUSE_UNREGISTERED;
01903 case OR2_CAUSE_NO_ANSWER:
01904 return AST_CAUSE_NO_ANSWER;
01905 case OR2_CAUSE_NORMAL_CLEARING:
01906 return AST_CAUSE_NORMAL_CLEARING;
01907 case OR2_CAUSE_UNSPECIFIED:
01908 default:
01909 return AST_CAUSE_NOTDEFINED;
01910 }
01911 }
01912
01913 static void dahdi_r2_on_call_disconnect(openr2_chan_t *r2chan, openr2_call_disconnect_cause_t cause)
01914 {
01915 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01916 ast_verbose("MFC/R2 call disconnected on channel %d\n", openr2_chan_get_number(r2chan));
01917 ast_mutex_lock(&p->lock);
01918 if (!p->owner) {
01919 ast_mutex_unlock(&p->lock);
01920
01921 dahdi_r2_disconnect_call(p, OR2_CAUSE_NORMAL_CLEARING);
01922 return;
01923 }
01924
01925
01926 if (p->owner->_state == AST_STATE_UP) {
01927 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01928 ast_mutex_unlock(&p->lock);
01929 } else if (openr2_chan_get_direction(r2chan) == OR2_DIR_FORWARD) {
01930
01931 switch (cause) {
01932 case OR2_CAUSE_BUSY_NUMBER:
01933 p->subs[SUB_REAL].needbusy = 1;
01934 break;
01935 case OR2_CAUSE_NETWORK_CONGESTION:
01936 case OR2_CAUSE_OUT_OF_ORDER:
01937 case OR2_CAUSE_UNALLOCATED_NUMBER:
01938 case OR2_CAUSE_NO_ANSWER:
01939 case OR2_CAUSE_UNSPECIFIED:
01940 case OR2_CAUSE_NORMAL_CLEARING:
01941 p->subs[SUB_REAL].needcongestion = 1;
01942 break;
01943 default:
01944 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
01945 }
01946 ast_mutex_unlock(&p->lock);
01947 } else {
01948 ast_mutex_unlock(&p->lock);
01949
01950
01951 ast_queue_hangup_with_cause(p->owner, dahdi_r2_cause_to_ast_cause(cause));
01952 }
01953 }
01954
01955 static void dahdi_r2_write_log(openr2_log_level_t level, char *logmessage)
01956 {
01957 switch (level) {
01958 case OR2_LOG_NOTICE:
01959 ast_verbose("%s", logmessage);
01960 break;
01961 case OR2_LOG_WARNING:
01962 ast_log(LOG_WARNING, "%s", logmessage);
01963 break;
01964 case OR2_LOG_ERROR:
01965 ast_log(LOG_ERROR, "%s", logmessage);
01966 break;
01967 case OR2_LOG_STACK_TRACE:
01968 case OR2_LOG_MF_TRACE:
01969 case OR2_LOG_CAS_TRACE:
01970 case OR2_LOG_DEBUG:
01971 case OR2_LOG_EX_DEBUG:
01972 ast_log(LOG_DEBUG, "%s", logmessage);
01973 break;
01974 default:
01975 ast_log(LOG_WARNING, "We should handle logging level %d here.\n", level);
01976 ast_log(LOG_DEBUG, "%s", logmessage);
01977 break;
01978 }
01979 }
01980
01981 static void dahdi_r2_on_line_blocked(openr2_chan_t *r2chan)
01982 {
01983 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01984 ast_mutex_lock(&p->lock);
01985 p->remotelyblocked = 1;
01986 ast_mutex_unlock(&p->lock);
01987 ast_log(LOG_NOTICE, "Far end blocked on chan %d\n", openr2_chan_get_number(r2chan));
01988 }
01989
01990 static void dahdi_r2_on_line_idle(openr2_chan_t *r2chan)
01991 {
01992 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
01993 ast_mutex_lock(&p->lock);
01994 p->remotelyblocked = 0;
01995 ast_mutex_unlock(&p->lock);
01996 ast_log(LOG_NOTICE, "Far end unblocked on chan %d\n", openr2_chan_get_number(r2chan));
01997 }
01998
01999 static void dahdi_r2_on_context_log(openr2_context_t *r2context, openr2_log_level_t level, const char *fmt, va_list ap)
02000 __attribute__((format (printf, 3, 0)));
02001 static void dahdi_r2_on_context_log(openr2_context_t *r2context, openr2_log_level_t level, const char *fmt, va_list ap)
02002 {
02003 #define CONTEXT_TAG "Context - "
02004 char logmsg[256];
02005 char completemsg[sizeof(logmsg) + sizeof(CONTEXT_TAG) - 1];
02006 vsnprintf(logmsg, sizeof(logmsg), fmt, ap);
02007 snprintf(completemsg, sizeof(completemsg), CONTEXT_TAG "%s", logmsg);
02008 dahdi_r2_write_log(level, completemsg);
02009 #undef CONTEXT_TAG
02010 }
02011
02012 static void dahdi_r2_on_chan_log(openr2_chan_t *r2chan, openr2_log_level_t level, const char *fmt, va_list ap)
02013 __attribute__((format (printf, 3, 0)));
02014 static void dahdi_r2_on_chan_log(openr2_chan_t *r2chan, openr2_log_level_t level, const char *fmt, va_list ap)
02015 {
02016 #define CHAN_TAG "Chan "
02017 char logmsg[256];
02018 char completemsg[sizeof(logmsg) + sizeof(CHAN_TAG) - 1];
02019 vsnprintf(logmsg, sizeof(logmsg), fmt, ap);
02020 snprintf(completemsg, sizeof(completemsg), CHAN_TAG "%d - %s", openr2_chan_get_number(r2chan), logmsg);
02021 dahdi_r2_write_log(level, completemsg);
02022 }
02023
02024 static int dahdi_r2_on_dnis_digit_received(openr2_chan_t *r2chan, char digit)
02025 {
02026 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
02027
02028 if (p->immediate) {
02029 return 0;
02030 }
02031 p->exten[p->mfcr2_dnis_index] = digit;
02032 p->rdnis[p->mfcr2_dnis_index] = digit;
02033 p->mfcr2_dnis_index++;
02034 p->exten[p->mfcr2_dnis_index] = 0;
02035 p->rdnis[p->mfcr2_dnis_index] = 0;
02036
02037 if ((p->mfcr2_dnis_matched ||
02038 (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num) && (p->mfcr2_dnis_matched = 1))) &&
02039 !ast_matchmore_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
02040 return 0;
02041 }
02042
02043 return 1;
02044 }
02045
02046 static void dahdi_r2_on_ani_digit_received(openr2_chan_t *r2chan, char digit)
02047 {
02048 struct dahdi_pvt *p = openr2_chan_get_client_data(r2chan);
02049 p->cid_num[p->mfcr2_ani_index] = digit;
02050 p->cid_name[p->mfcr2_ani_index] = digit;
02051 p->mfcr2_ani_index++;
02052 p->cid_num[p->mfcr2_ani_index] = 0;
02053 p->cid_name[p->mfcr2_ani_index] = 0;
02054 }
02055
02056 static void dahdi_r2_on_billing_pulse_received(openr2_chan_t *r2chan)
02057 {
02058 ast_verbose("MFC/R2 billing pulse received on channel %d\n", openr2_chan_get_number(r2chan));
02059 }
02060
02061 static openr2_event_interface_t dahdi_r2_event_iface = {
02062 .on_call_init = dahdi_r2_on_call_init,
02063 .on_call_offered = dahdi_r2_on_call_offered,
02064 .on_call_accepted = dahdi_r2_on_call_accepted,
02065 .on_call_answered = dahdi_r2_on_call_answered,
02066 .on_call_disconnect = dahdi_r2_on_call_disconnect,
02067 .on_call_end = dahdi_r2_on_call_end,
02068 .on_call_read = dahdi_r2_on_call_read,
02069 .on_hardware_alarm = dahdi_r2_on_hardware_alarm,
02070 .on_os_error = dahdi_r2_on_os_error,
02071 .on_protocol_error = dahdi_r2_on_protocol_error,
02072 .on_line_blocked = dahdi_r2_on_line_blocked,
02073 .on_line_idle = dahdi_r2_on_line_idle,
02074
02075 .on_context_log = (openr2_handle_context_logging_func)dahdi_r2_on_context_log,
02076 .on_dnis_digit_received = dahdi_r2_on_dnis_digit_received,
02077 .on_ani_digit_received = dahdi_r2_on_ani_digit_received,
02078
02079 .on_billing_pulse_received = dahdi_r2_on_billing_pulse_received
02080 };
02081
02082 static inline int16_t dahdi_r2_alaw_to_linear(uint8_t sample)
02083 {
02084 return AST_ALAW(sample);
02085 }
02086
02087 static inline uint8_t dahdi_r2_linear_to_alaw(int sample)
02088 {
02089 return AST_LIN2A(sample);
02090 }
02091
02092 static openr2_transcoder_interface_t dahdi_r2_transcode_iface = {
02093 dahdi_r2_alaw_to_linear,
02094 dahdi_r2_linear_to_alaw
02095 };
02096
02097 #endif
02098
02099 static int restore_gains(struct dahdi_pvt *p);
02100
02101 static void swap_subs(struct dahdi_pvt *p, int a, int b)
02102 {
02103 int tchan;
02104 int tinthreeway;
02105 struct ast_channel *towner;
02106
02107 ast_debug(1, "Swapping %d and %d\n", a, b);
02108
02109 tchan = p->subs[a].chan;
02110 towner = p->subs[a].owner;
02111 tinthreeway = p->subs[a].inthreeway;
02112
02113 p->subs[a].chan = p->subs[b].chan;
02114 p->subs[a].owner = p->subs[b].owner;
02115 p->subs[a].inthreeway = p->subs[b].inthreeway;
02116
02117 p->subs[b].chan = tchan;
02118 p->subs[b].owner = towner;
02119 p->subs[b].inthreeway = tinthreeway;
02120
02121 if (p->subs[a].owner)
02122 ast_channel_set_fd(p->subs[a].owner, 0, p->subs[a].dfd);
02123 if (p->subs[b].owner)
02124 ast_channel_set_fd(p->subs[b].owner, 0, p->subs[b].dfd);
02125 wakeup_sub(p, a, NULL);
02126 wakeup_sub(p, b, NULL);
02127 }
02128
02129 static int dahdi_open(char *fn)
02130 {
02131 int fd;
02132 int isnum;
02133 int chan = 0;
02134 int bs;
02135 int x;
02136 isnum = 1;
02137 for (x = 0; x < strlen(fn); x++) {
02138 if (!isdigit(fn[x])) {
02139 isnum = 0;
02140 break;
02141 }
02142 }
02143 if (isnum) {
02144 chan = atoi(fn);
02145 if (chan < 1) {
02146 ast_log(LOG_WARNING, "Invalid channel number '%s'\n", fn);
02147 return -1;
02148 }
02149 fn = "/dev/dahdi/channel";
02150 }
02151 fd = open(fn, O_RDWR | O_NONBLOCK);
02152 if (fd < 0) {
02153 ast_log(LOG_WARNING, "Unable to open '%s': %s\n", fn, strerror(errno));
02154 return -1;
02155 }
02156 if (chan) {
02157 if (ioctl(fd, DAHDI_SPECIFY, &chan)) {
02158 x = errno;
02159 close(fd);
02160 errno = x;
02161 ast_log(LOG_WARNING, "Unable to specify channel %d: %s\n", chan, strerror(errno));
02162 return -1;
02163 }
02164 }
02165 bs = READ_SIZE;
02166 if (ioctl(fd, DAHDI_SET_BLOCKSIZE, &bs) == -1) {
02167 ast_log(LOG_WARNING, "Unable to set blocksize '%d': %s\n", bs, strerror(errno));
02168 x = errno;
02169 close(fd);
02170 errno = x;
02171 return -1;
02172 }
02173 return fd;
02174 }
02175
02176 static void dahdi_close(int fd)
02177 {
02178 if (fd > 0)
02179 close(fd);
02180 }
02181
02182 static void dahdi_close_sub(struct dahdi_pvt *chan_pvt, int sub_num)
02183 {
02184 dahdi_close(chan_pvt->subs[sub_num].dfd);
02185 chan_pvt->subs[sub_num].dfd = -1;
02186 }
02187
02188 #if defined(HAVE_PRI)
02189 static void dahdi_close_pri_fd(struct dahdi_pri *pri, int fd_num)
02190 {
02191 dahdi_close(pri->fds[fd_num]);
02192 pri->fds[fd_num] = -1;
02193 }
02194 #endif
02195
02196 #if defined(HAVE_SS7)
02197 static void dahdi_close_ss7_fd(struct dahdi_ss7 *ss7, int fd_num)
02198 {
02199 dahdi_close(ss7->fds[fd_num]);
02200 ss7->fds[fd_num] = -1;
02201 }
02202 #endif
02203
02204 static int dahdi_setlinear(int dfd, int linear)
02205 {
02206 int res;
02207 res = ioctl(dfd, DAHDI_SETLINEAR, &linear);
02208 if (res)
02209 return res;
02210 return 0;
02211 }
02212
02213
02214 static int alloc_sub(struct dahdi_pvt *p, int x)
02215 {
02216 struct dahdi_bufferinfo bi;
02217 int res;
02218 if (p->subs[x].dfd >= 0) {
02219 ast_log(LOG_WARNING, "%s subchannel of %d already in use\n", subnames[x], p->channel);
02220 return -1;
02221 }
02222
02223 p->subs[x].dfd = dahdi_open("/dev/dahdi/pseudo");
02224 if (p->subs[x].dfd <= -1) {
02225 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
02226 return -1;
02227 }
02228
02229 res = ioctl(p->subs[x].dfd, DAHDI_GET_BUFINFO, &bi);
02230 if (!res) {
02231 bi.txbufpolicy = p->buf_policy;
02232 bi.rxbufpolicy = p->buf_policy;
02233 bi.numbufs = p->buf_no;
02234 res = ioctl(p->subs[x].dfd, DAHDI_SET_BUFINFO, &bi);
02235 if (res < 0) {
02236 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", x, strerror(errno));
02237 }
02238 } else
02239 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", x, strerror(errno));
02240
02241 if (ioctl(p->subs[x].dfd, DAHDI_CHANNO, &p->subs[x].chan) == 1) {
02242 ast_log(LOG_WARNING, "Unable to get channel number for pseudo channel on FD %d: %s\n", p->subs[x].dfd, strerror(errno));
02243 dahdi_close_sub(p, x);
02244 p->subs[x].dfd = -1;
02245 return -1;
02246 }
02247 ast_debug(1, "Allocated %s subchannel on FD %d channel %d\n", subnames[x], p->subs[x].dfd, p->subs[x].chan);
02248 return 0;
02249 }
02250
02251 static int unalloc_sub(struct dahdi_pvt *p, int x)
02252 {
02253 if (!x) {
02254 ast_log(LOG_WARNING, "Trying to unalloc the real channel %d?!?\n", p->channel);
02255 return -1;
02256 }
02257 ast_debug(1, "Released sub %d of channel %d\n", x, p->channel);
02258 dahdi_close_sub(p, x);
02259 p->subs[x].linear = 0;
02260 p->subs[x].chan = 0;
02261 p->subs[x].owner = NULL;
02262 p->subs[x].inthreeway = 0;
02263 p->polarity = POLARITY_IDLE;
02264 memset(&p->subs[x].curconf, 0, sizeof(p->subs[x].curconf));
02265 return 0;
02266 }
02267
02268 static int digit_to_dtmfindex(char digit)
02269 {
02270 if (isdigit(digit))
02271 return DAHDI_TONE_DTMF_BASE + (digit - '0');
02272 else if (digit >= 'A' && digit <= 'D')
02273 return DAHDI_TONE_DTMF_A + (digit - 'A');
02274 else if (digit >= 'a' && digit <= 'd')
02275 return DAHDI_TONE_DTMF_A + (digit - 'a');
02276 else if (digit == '*')
02277 return DAHDI_TONE_DTMF_s;
02278 else if (digit == '#')
02279 return DAHDI_TONE_DTMF_p;
02280 else
02281 return -1;
02282 }
02283
02284 static int dahdi_digit_begin(struct ast_channel *chan, char digit)
02285 {
02286 struct dahdi_pvt *pvt;
02287 int idx;
02288 int dtmf = -1;
02289
02290 pvt = chan->tech_pvt;
02291
02292 ast_mutex_lock(&pvt->lock);
02293
02294 idx = dahdi_get_index(chan, pvt, 0);
02295
02296 if ((idx != SUB_REAL) || !pvt->owner)
02297 goto out;
02298
02299 #ifdef HAVE_PRI
02300 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
02301 && (chan->_state == AST_STATE_DIALING) && !pvt->proceeding) {
02302 if (pvt->setup_ack) {
02303 if (!pri_grab(pvt, pvt->pri)) {
02304 pri_information(pvt->pri->pri, pvt->call, digit);
02305 pri_rel(pvt->pri);
02306 } else
02307 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", pvt->span);
02308 } else if (strlen(pvt->dialdest) < sizeof(pvt->dialdest) - 1) {
02309 int res;
02310 ast_debug(1, "Queueing digit '%c' since setup_ack not yet received\n", digit);
02311 res = strlen(pvt->dialdest);
02312 pvt->dialdest[res++] = digit;
02313 pvt->dialdest[res] = '\0';
02314 }
02315 goto out;
02316 }
02317 #endif
02318 if ((dtmf = digit_to_dtmfindex(digit)) == -1)
02319 goto out;
02320
02321 if (pvt->pulse || ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &dtmf)) {
02322 int res;
02323 struct dahdi_dialoperation zo = {
02324 .op = DAHDI_DIAL_OP_APPEND,
02325 };
02326
02327 zo.dialstr[0] = 'T';
02328 zo.dialstr[1] = digit;
02329 zo.dialstr[2] = '\0';
02330 if ((res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_DIAL, &zo)))
02331 ast_log(LOG_WARNING, "Couldn't dial digit %c: %s\n", digit, strerror(errno));
02332 else
02333 pvt->dialing = 1;
02334 } else {
02335 ast_debug(1, "Started VLDTMF digit '%c'\n", digit);
02336 pvt->dialing = 1;
02337 pvt->begindigit = digit;
02338 }
02339
02340 out:
02341 ast_mutex_unlock(&pvt->lock);
02342
02343 return 0;
02344 }
02345
02346 static int dahdi_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
02347 {
02348 struct dahdi_pvt *pvt;
02349 int res = 0;
02350 int idx;
02351 int x;
02352
02353 pvt = chan->tech_pvt;
02354
02355 ast_mutex_lock(&pvt->lock);
02356
02357 idx = dahdi_get_index(chan, pvt, 0);
02358
02359 if ((idx != SUB_REAL) || !pvt->owner || pvt->pulse)
02360 goto out;
02361
02362 #ifdef HAVE_PRI
02363
02364 if (((pvt->sig == SIG_PRI) || (pvt->sig == SIG_BRI) || (pvt->sig == SIG_BRI_PTMP))
02365 && !pvt->begindigit)
02366 goto out;
02367 #endif
02368
02369 if (pvt->begindigit) {
02370 x = -1;
02371 ast_debug(1, "Ending VLDTMF digit '%c'\n", digit);
02372 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SENDTONE, &x);
02373 pvt->dialing = 0;
02374 pvt->begindigit = 0;
02375 }
02376
02377 out:
02378 ast_mutex_unlock(&pvt->lock);
02379
02380 return res;
02381 }
02382
02383 static char *events[] = {
02384 "No event",
02385 "On hook",
02386 "Ring/Answered",
02387 "Wink/Flash",
02388 "Alarm",
02389 "No more alarm",
02390 "HDLC Abort",
02391 "HDLC Overrun",
02392 "HDLC Bad FCS",
02393 "Dial Complete",
02394 "Ringer On",
02395 "Ringer Off",
02396 "Hook Transition Complete",
02397 "Bits Changed",
02398 "Pulse Start",
02399 "Timer Expired",
02400 "Timer Ping",
02401 "Polarity Reversal",
02402 "Ring Begin",
02403 };
02404
02405 static struct {
02406 int alarm;
02407 char *name;
02408 } alarms[] = {
02409 { DAHDI_ALARM_RED, "Red Alarm" },
02410 { DAHDI_ALARM_YELLOW, "Yellow Alarm" },
02411 { DAHDI_ALARM_BLUE, "Blue Alarm" },
02412 { DAHDI_ALARM_RECOVER, "Recovering" },
02413 { DAHDI_ALARM_LOOPBACK, "Loopback" },
02414 { DAHDI_ALARM_NOTOPEN, "Not Open" },
02415 { DAHDI_ALARM_NONE, "None" },
02416 };
02417
02418 static char *alarm2str(int alm)
02419 {
02420 int x;
02421 for (x = 0; x < ARRAY_LEN(alarms); x++) {
02422 if (alarms[x].alarm & alm)
02423 return alarms[x].name;
02424 }
02425 return alm ? "Unknown Alarm" : "No Alarm";
02426 }
02427
02428 static char *event2str(int event)
02429 {
02430 static char buf[256];
02431 if ((event < (ARRAY_LEN(events))) && (event > -1))
02432 return events[event];
02433 sprintf(buf, "Event %d", event);
02434 return buf;
02435 }
02436
02437 #ifdef HAVE_PRI
02438 static char *dialplan2str(int dialplan)
02439 {
02440 if (dialplan == -1 || dialplan == -2) {
02441 return("Dynamically set dialplan in ISDN");
02442 }
02443 return (pri_plan2str(dialplan));
02444 }
02445 #endif
02446
02447 static char *dahdi_sig2str(int sig)
02448 {
02449 static char buf[256];
02450 switch (sig) {
02451 case SIG_EM:
02452 return "E & M Immediate";
02453 case SIG_EMWINK:
02454 return "E & M Wink";
02455 case SIG_EM_E1:
02456 return "E & M E1";
02457 case SIG_FEATD:
02458 return "Feature Group D (DTMF)";
02459 case SIG_FEATDMF:
02460 return "Feature Group D (MF)";
02461 case SIG_FEATDMF_TA:
02462 return "Feature Groud D (MF) Tandem Access";
02463 case SIG_FEATB:
02464 return "Feature Group B (MF)";
02465 case SIG_E911:
02466 return "E911 (MF)";
02467 case SIG_FGC_CAMA:
02468 return "FGC/CAMA (Dialpulse)";
02469 case SIG_FGC_CAMAMF:
02470 return "FGC/CAMA (MF)";
02471 case SIG_FXSLS:
02472 return "FXS Loopstart";
02473 case SIG_FXSGS:
02474 return "FXS Groundstart";
02475 case SIG_FXSKS:
02476 return "FXS Kewlstart";
02477 case SIG_FXOLS:
02478 return "FXO Loopstart";
02479 case SIG_FXOGS:
02480 return "FXO Groundstart";
02481 case SIG_FXOKS:
02482 return "FXO Kewlstart";
02483 case SIG_PRI:
02484 return "ISDN PRI";
02485 case SIG_BRI:
02486 return "ISDN BRI Point to Point";
02487 case SIG_BRI_PTMP:
02488 return "ISDN BRI Point to MultiPoint";
02489 case SIG_SS7:
02490 return "SS7";
02491 case SIG_MFCR2:
02492 return "MFC/R2";
02493 case SIG_SF:
02494 return "SF (Tone) Immediate";
02495 case SIG_SFWINK:
02496 return "SF (Tone) Wink";
02497 case SIG_SF_FEATD:
02498 return "SF (Tone) with Feature Group D (DTMF)";
02499 case SIG_SF_FEATDMF:
02500 return "SF (Tone) with Feature Group D (MF)";
02501 case SIG_SF_FEATB:
02502 return "SF (Tone) with Feature Group B (MF)";
02503 case SIG_GR303FXOKS:
02504 return "GR-303 with FXOKS";
02505 case SIG_GR303FXSKS:
02506 return "GR-303 with FXSKS";
02507 case 0:
02508 return "Pseudo";
02509 default:
02510 snprintf(buf, sizeof(buf), "Unknown signalling %d", sig);
02511 return buf;
02512 }
02513 }
02514
02515 #define sig2str dahdi_sig2str
02516
02517 static int conf_add(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx, int slavechannel)
02518 {
02519
02520
02521 struct dahdi_confinfo zi;
02522
02523 memset(&zi, 0, sizeof(zi));
02524 zi.chan = 0;
02525
02526 if (slavechannel > 0) {
02527
02528 zi.confmode = DAHDI_CONF_DIGITALMON;
02529 zi.confno = slavechannel;
02530 } else {
02531 if (!idx) {
02532
02533 zi.confmode = DAHDI_CONF_REALANDPSEUDO | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER |
02534 DAHDI_CONF_PSEUDO_TALKER | DAHDI_CONF_PSEUDO_LISTENER;
02535 } else
02536 zi.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
02537 zi.confno = p->confno;
02538 }
02539 if ((zi.confno == c->curconf.confno) && (zi.confmode == c->curconf.confmode))
02540 return 0;
02541 if (c->dfd < 0)
02542 return 0;
02543 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
02544 ast_log(LOG_WARNING, "Failed to add %d to conference %d/%d: %s\n", c->dfd, zi.confmode, zi.confno, strerror(errno));
02545 return -1;
02546 }
02547 if (slavechannel < 1) {
02548 p->confno = zi.confno;
02549 }
02550 memcpy(&c->curconf, &zi, sizeof(c->curconf));
02551 ast_debug(1, "Added %d to conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
02552 return 0;
02553 }
02554
02555 static int isourconf(struct dahdi_pvt *p, struct dahdi_subchannel *c)
02556 {
02557
02558 if ((p->channel == c->curconf.confno) && (c->curconf.confmode == DAHDI_CONF_DIGITALMON))
02559 return 1;
02560
02561 if ((p->confno > 0) && (p->confno == c->curconf.confno) && (c->curconf.confmode & DAHDI_CONF_TALKER))
02562 return 1;
02563 return 0;
02564 }
02565
02566 static int conf_del(struct dahdi_pvt *p, struct dahdi_subchannel *c, int idx)
02567 {
02568 struct dahdi_confinfo zi;
02569 if (
02570 (c->dfd < 0) ||
02571
02572 !isourconf(p, c)
02573
02574 ) return 0;
02575 memset(&zi, 0, sizeof(zi));
02576 if (ioctl(c->dfd, DAHDI_SETCONF, &zi)) {
02577 ast_log(LOG_WARNING, "Failed to drop %d from conference %d/%d: %s\n", c->dfd, c->curconf.confmode, c->curconf.confno, strerror(errno));
02578 return -1;
02579 }
02580 ast_debug(1, "Removed %d from conference %d/%d\n", c->dfd, c->curconf.confmode, c->curconf.confno);
02581 memcpy(&c->curconf, &zi, sizeof(c->curconf));
02582 return 0;
02583 }
02584
02585 static int isslavenative(struct dahdi_pvt *p, struct dahdi_pvt **out)
02586 {
02587 int x;
02588 int useslavenative;
02589 struct dahdi_pvt *slave = NULL;
02590
02591 useslavenative = 1;
02592
02593 for (x = 0; x < 3; x++) {
02594
02595
02596 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway)
02597 useslavenative = 0;
02598 }
02599
02600
02601 if (useslavenative) {
02602 for (x = 0; x < MAX_SLAVES; x++) {
02603 if (p->slaves[x]) {
02604 if (slave) {
02605
02606
02607 slave = NULL;
02608 useslavenative = 0;
02609 break;
02610 } else {
02611
02612 slave = p->slaves[x];
02613 }
02614 }
02615 }
02616 }
02617
02618 if (!slave)
02619 useslavenative = 0;
02620 else if (slave->law != p->law) {
02621 useslavenative = 0;
02622 slave = NULL;
02623 }
02624 if (out)
02625 *out = slave;
02626 return useslavenative;
02627 }
02628
02629 static int reset_conf(struct dahdi_pvt *p)
02630 {
02631 p->confno = -1;
02632 memset(&p->subs[SUB_REAL].curconf, 0, sizeof(p->subs[SUB_REAL].curconf));
02633 if (p->subs[SUB_REAL].dfd > -1) {
02634 struct dahdi_confinfo zi;
02635
02636 memset(&zi, 0, sizeof(zi));
02637 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &zi))
02638 ast_log(LOG_WARNING, "Failed to reset conferencing on channel %d: %s\n", p->channel, strerror(errno));
02639 }
02640 return 0;
02641 }
02642
02643 static int update_conf(struct dahdi_pvt *p)
02644 {
02645 int needconf = 0;
02646 int x;
02647 int useslavenative;
02648 struct dahdi_pvt *slave = NULL;
02649
02650 useslavenative = isslavenative(p, &slave);
02651
02652 for (x = 0; x < 3; x++) {
02653
02654 if ((p->subs[x].dfd > -1) && p->subs[x].inthreeway) {
02655 conf_add(p, &p->subs[x], x, 0);
02656 needconf++;
02657 } else {
02658 conf_del(p, &p->subs[x], x);
02659 }
02660 }
02661
02662
02663 for (x = 0; x < MAX_SLAVES; x++) {
02664 if (p->slaves[x]) {
02665 if (useslavenative)
02666 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p));
02667 else {
02668 conf_add(p, &p->slaves[x]->subs[SUB_REAL], SUB_REAL, 0);
02669 needconf++;
02670 }
02671 }
02672 }
02673
02674 if (p->inconference && !p->subs[SUB_REAL].inthreeway) {
02675 if (useslavenative)
02676 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(slave));
02677 else {
02678 conf_add(p, &p->subs[SUB_REAL], SUB_REAL, 0);
02679 needconf++;
02680 }
02681 }
02682
02683 if (p->master) {
02684 if (isslavenative(p->master, NULL)) {
02685 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, GET_CHANNEL(p->master));
02686 } else {
02687 conf_add(p->master, &p->subs[SUB_REAL], SUB_REAL, 0);
02688 }
02689 }
02690 if (!needconf) {
02691
02692
02693 p->confno = -1;
02694 }
02695 ast_debug(1, "Updated conferencing on %d, with %d conference users\n", p->channel, needconf);
02696 return 0;
02697 }
02698
02699 static void dahdi_enable_ec(struct dahdi_pvt *p)
02700 {
02701 int x;
02702 int res;
02703 if (!p)
02704 return;
02705 if (p->echocanon) {
02706 ast_debug(1, "Echo cancellation already on\n");
02707 return;
02708 }
02709 if (p->digital) {
02710 ast_debug(1, "Echo cancellation isn't required on digital connection\n");
02711 return;
02712 }
02713 if (p->echocancel.head.tap_length) {
02714 if ((p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP) || (p->sig == SIG_PRI) || (p->sig == SIG_SS7)) {
02715 x = 1;
02716 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x);
02717 if (res)
02718 ast_log(LOG_WARNING, "Unable to enable audio mode on channel %d (%s)\n", p->channel, strerror(errno));
02719 }
02720 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &p->echocancel);
02721 if (res) {
02722 ast_log(LOG_WARNING, "Unable to enable echo cancellation on channel %d (%s)\n", p->channel, strerror(errno));
02723 } else {
02724 p->echocanon = 1;
02725 ast_debug(1, "Enabled echo cancellation on channel %d\n", p->channel);
02726 }
02727 } else
02728 ast_debug(1, "No echo cancellation requested\n");
02729 }
02730
02731 static void dahdi_train_ec(struct dahdi_pvt *p)
02732 {
02733 int x;
02734 int res;
02735
02736 if (p && p->echocanon && p->echotraining) {
02737 x = p->echotraining;
02738 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOTRAIN, &x);
02739 if (res)
02740 ast_log(LOG_WARNING, "Unable to request echo training on channel %d: %s\n", p->channel, strerror(errno));
02741 else
02742 ast_debug(1, "Engaged echo training on channel %d\n", p->channel);
02743 } else {
02744 ast_debug(1, "No echo training requested\n");
02745 }
02746 }
02747
02748 static void dahdi_disable_ec(struct dahdi_pvt *p)
02749 {
02750 int res;
02751
02752 if (p->echocanon) {
02753 struct dahdi_echocanparams ecp = { .tap_length = 0 };
02754
02755 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_ECHOCANCEL_PARAMS, &ecp);
02756
02757 if (res)
02758 ast_log(LOG_WARNING, "Unable to disable echo cancellation on channel %d: %s\n", p->channel, strerror(errno));
02759 else
02760 ast_debug(1, "Disabled echo cancellation on channel %d\n", p->channel);
02761 }
02762
02763 p->echocanon = 0;
02764 }
02765
02766 static void fill_txgain(struct dahdi_gains *g, float gain, int law)
02767 {
02768 int j;
02769 int k;
02770 float linear_gain = pow(10.0, gain / 20.0);
02771
02772 switch (law) {
02773 case DAHDI_LAW_ALAW:
02774 for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
02775 if (gain) {
02776 k = (int) (((float) AST_ALAW(j)) * linear_gain);
02777 if (k > 32767) k = 32767;
02778 if (k < -32767) k = -32767;
02779 g->txgain[j] = AST_LIN2A(k);
02780 } else {
02781 g->txgain[j] = j;
02782 }
02783 }
02784 break;
02785 case DAHDI_LAW_MULAW:
02786 for (j = 0; j < ARRAY_LEN(g->txgain); j++) {
02787 if (gain) {
02788 k = (int) (((float) AST_MULAW(j)) * linear_gain);
02789 if (k > 32767) k = 32767;
02790 if (k < -32767) k = -32767;
02791 g->txgain[j] = AST_LIN2MU(k);
02792 } else {
02793 g->txgain[j] = j;
02794 }
02795 }
02796 break;
02797 }
02798 }
02799
02800 static void fill_rxgain(struct dahdi_gains *g, float gain, int law)
02801 {
02802 int j;
02803 int k;
02804 float linear_gain = pow(10.0, gain / 20.0);
02805
02806 switch (law) {
02807 case DAHDI_LAW_ALAW:
02808 for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
02809 if (gain) {
02810 k = (int) (((float) AST_ALAW(j)) * linear_gain);
02811 if (k > 32767) k = 32767;
02812 if (k < -32767) k = -32767;
02813 g->rxgain[j] = AST_LIN2A(k);
02814 } else {
02815 g->rxgain[j] = j;
02816 }
02817 }
02818 break;
02819 case DAHDI_LAW_MULAW:
02820 for (j = 0; j < ARRAY_LEN(g->rxgain); j++) {
02821 if (gain) {
02822 k = (int) (((float) AST_MULAW(j)) * linear_gain);
02823 if (k > 32767) k = 32767;
02824 if (k < -32767) k = -32767;
02825 g->rxgain[j] = AST_LIN2MU(k);
02826 } else {
02827 g->rxgain[j] = j;
02828 }
02829 }
02830 break;
02831 }
02832 }
02833
02834 static int set_actual_txgain(int fd, int chan, float gain, int law)
02835 {
02836 struct dahdi_gains g;
02837 int res;
02838
02839 memset(&g, 0, sizeof(g));
02840 g.chan = chan;
02841 res = ioctl(fd, DAHDI_GETGAINS, &g);
02842 if (res) {
02843 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
02844 return res;
02845 }
02846
02847 fill_txgain(&g, gain, law);
02848
02849 return ioctl(fd, DAHDI_SETGAINS, &g);
02850 }
02851
02852 static int set_actual_rxgain(int fd, int chan, float gain, int law)
02853 {
02854 struct dahdi_gains g;
02855 int res;
02856
02857 memset(&g, 0, sizeof(g));
02858 g.chan = chan;
02859 res = ioctl(fd, DAHDI_GETGAINS, &g);
02860 if (res) {
02861 ast_debug(1, "Failed to read gains: %s\n", strerror(errno));
02862 return res;
02863 }
02864
02865 fill_rxgain(&g, gain, law);
02866
02867 return ioctl(fd, DAHDI_SETGAINS, &g);
02868 }
02869
02870 static int set_actual_gain(int fd, int chan, float rxgain, float txgain, int law)
02871 {
02872 return set_actual_txgain(fd, chan, txgain, law) | set_actual_rxgain(fd, chan, rxgain, law);
02873 }
02874
02875 static int bump_gains(struct dahdi_pvt *p)
02876 {
02877 int res;
02878
02879
02880 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain + p->cid_rxgain, p->txgain, p->law);
02881 if (res) {
02882 ast_log(LOG_WARNING, "Unable to bump gain: %s\n", strerror(errno));
02883 return -1;
02884 }
02885
02886 return 0;
02887 }
02888
02889 static int restore_gains(struct dahdi_pvt *p)
02890 {
02891 int res;
02892
02893 res = set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
02894 if (res) {
02895 ast_log(LOG_WARNING, "Unable to restore gains: %s\n", strerror(errno));
02896 return -1;
02897 }
02898
02899 return 0;
02900 }
02901
02902 static inline int dahdi_set_hook(int fd, int hs)
02903 {
02904 int x, res;
02905
02906 x = hs;
02907 res = ioctl(fd, DAHDI_HOOK, &x);
02908
02909 if (res < 0) {
02910 if (errno == EINPROGRESS)
02911 return 0;
02912 ast_log(LOG_WARNING, "DAHDI hook failed returned %d (trying %d): %s\n", res, hs, strerror(errno));
02913
02914 }
02915
02916 return res;
02917 }
02918
02919 static inline int dahdi_confmute(struct dahdi_pvt *p, int muted)
02920 {
02921 int x, y, res;
02922 x = muted;
02923 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
02924 y = 1;
02925 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &y);
02926 if (res)
02927 ast_log(LOG_WARNING, "Unable to set audio mode on %d: %s\n", p->channel, strerror(errno));
02928 }
02929 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_CONFMUTE, &x);
02930 if (res < 0)
02931 ast_log(LOG_WARNING, "DAHDI confmute(%d) failed on channel %d: %s\n", muted, p->channel, strerror(errno));
02932 return res;
02933 }
02934
02935 static int save_conference(struct dahdi_pvt *p)
02936 {
02937 struct dahdi_confinfo c;
02938 int res;
02939 if (p->saveconf.confmode) {
02940 ast_log(LOG_WARNING, "Can't save conference -- already in use\n");
02941 return -1;
02942 }
02943 p->saveconf.chan = 0;
02944 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &p->saveconf);
02945 if (res) {
02946 ast_log(LOG_WARNING, "Unable to get conference info: %s\n", strerror(errno));
02947 p->saveconf.confmode = 0;
02948 return -1;
02949 }
02950 memset(&c, 0, sizeof(c));
02951 c.confmode = DAHDI_CONF_NORMAL;
02952 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &c);
02953 if (res) {
02954 ast_log(LOG_WARNING, "Unable to set conference info: %s\n", strerror(errno));
02955 return -1;
02956 }
02957 ast_debug(1, "Disabled conferencing\n");
02958 return 0;
02959 }
02960
02961
02962
02963
02964
02965
02966
02967
02968
02969
02970
02971
02972
02973
02974
02975
02976
02977
02978
02979 static void notify_message(char *mailbox_full, int thereornot)
02980 {
02981 char s[sizeof(mwimonitornotify) + 80];
02982 struct ast_event *event;
02983 char *mailbox, *context;
02984
02985
02986 context = mailbox = ast_strdupa(mailbox_full);
02987 strsep(&context, "@");
02988 if (ast_strlen_zero(context))
02989 context = "default";
02990
02991 if (!(event = ast_event_new(AST_EVENT_MWI,
02992 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
02993 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
02994 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
02995 AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, thereornot,
02996 AST_EVENT_IE_END))) {
02997 return;
02998 }
02999
03000 ast_event_queue_and_cache(event);
03001
03002 if (!ast_strlen_zero(mailbox) && !ast_strlen_zero(mwimonitornotify)) {
03003 snprintf(s, sizeof(s), "%s %s %d", mwimonitornotify, mailbox, thereornot);
03004 ast_safe_system(s);
03005 }
03006 }
03007
03008 static int restore_conference(struct dahdi_pvt *p)
03009 {
03010 int res;
03011 if (p->saveconf.confmode) {
03012 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCONF, &p->saveconf);
03013 p->saveconf.confmode = 0;
03014 if (res) {
03015 ast_log(LOG_WARNING, "Unable to restore conference info: %s\n", strerror(errno));
03016 return -1;
03017 }
03018 ast_debug(1, "Restored conferencing\n");
03019 }
03020 return 0;
03021 }
03022
03023 static int send_callerid(struct dahdi_pvt *p);
03024
03025 static int send_cwcidspill(struct dahdi_pvt *p)
03026 {
03027 p->callwaitcas = 0;
03028 p->cidcwexpire = 0;
03029 p->cid_suppress_expire = 0;
03030 if (!(p->cidspill = ast_malloc(MAX_CALLERID_SIZE)))
03031 return -1;
03032 p->cidlen = ast_callerid_callwaiting_generate(p->cidspill, p->callwait_name, p->callwait_num, AST_LAW(p));
03033
03034 p->cidlen += READ_SIZE * 4;
03035 p->cidpos = 0;
03036 send_callerid(p);
03037 ast_verb(3, "CPE supports Call Waiting Caller*ID. Sending '%s/%s'\n", p->callwait_name, p->callwait_num);
03038 return 0;
03039 }
03040
03041 static int has_voicemail(struct dahdi_pvt *p)
03042 {
03043 int new_msgs;
03044 struct ast_event *event;
03045 char *mailbox, *context;
03046
03047 mailbox = context = ast_strdupa(p->mailbox);
03048 strsep(&context, "@");
03049 if (ast_strlen_zero(context))
03050 context = "default";
03051
03052 event = ast_event_get_cached(AST_EVENT_MWI,
03053 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
03054 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
03055 AST_EVENT_IE_END);
03056
03057 if (event) {
03058 new_msgs = ast_event_get_ie_uint(event, AST_EVENT_IE_NEWMSGS);
03059 ast_event_destroy(event);
03060 } else
03061 new_msgs = ast_app_has_voicemail(p->mailbox, NULL);
03062
03063 return new_msgs;
03064 }
03065
03066 static int send_callerid(struct dahdi_pvt *p)
03067 {
03068
03069 int res;
03070
03071 if (p->subs[SUB_REAL].linear) {
03072 p->subs[SUB_REAL].linear = 0;
03073 dahdi_setlinear(p->subs[SUB_REAL].dfd, 0);
03074 }
03075 while (p->cidpos < p->cidlen) {
03076 res = write(p->subs[SUB_REAL].dfd, p->cidspill + p->cidpos, p->cidlen - p->cidpos);
03077 if (res < 0) {
03078 if (errno == EAGAIN)
03079 return 0;
03080 else {
03081 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
03082 return -1;
03083 }
03084 }
03085 if (!res)
03086 return 0;
03087 p->cidpos += res;
03088 }
03089 p->cid_suppress_expire = CALLWAITING_SUPPRESS_SAMPLES;
03090 ast_free(p->cidspill);
03091 p->cidspill = NULL;
03092 if (p->callwaitcas) {
03093
03094 p->cidcwexpire = CIDCW_EXPIRE_SAMPLES;
03095 p->cid_suppress_expire = p->cidcwexpire;
03096 } else
03097 restore_conference(p);
03098 return 0;
03099 }
03100
03101 static int dahdi_callwait(struct ast_channel *ast)
03102 {
03103 struct dahdi_pvt *p = ast->tech_pvt;
03104 p->callwaitingrepeat = CALLWAITING_REPEAT_SAMPLES;
03105 if (p->cidspill) {
03106 ast_log(LOG_WARNING, "Spill already exists?!?\n");
03107 ast_free(p->cidspill);
03108 }
03109
03110
03111
03112
03113
03114 if (!(p->cidspill = ast_malloc(2400 + 680 + READ_SIZE * 4)))
03115 return -1;
03116 save_conference(p);
03117
03118 memset(p->cidspill, 0x7f, 2400 + 600 + READ_SIZE * 4);
03119 if (!p->callwaitrings && p->callwaitingcallerid) {
03120 ast_gen_cas(p->cidspill, 1, 2400 + 680, AST_LAW(p));
03121 p->callwaitcas = 1;
03122 p->cidlen = 2400 + 680 + READ_SIZE * 4;
03123 } else {
03124 ast_gen_cas(p->cidspill, 1, 2400, AST_LAW(p));
03125 p->callwaitcas = 0;
03126 p->cidlen = 2400 + READ_SIZE * 4;
03127 }
03128 p->cidpos = 0;
03129 send_callerid(p);
03130
03131 return 0;
03132 }
03133
03134 #if defined(HAVE_SS7)
03135 static unsigned char cid_pres2ss7pres(int cid_pres)
03136 {
03137 return (cid_pres >> 5) & 0x03;
03138 }
03139 #endif
03140
03141 #if defined(HAVE_SS7)
03142 static unsigned char cid_pres2ss7screen(int cid_pres)
03143 {
03144 return cid_pres & 0x03;
03145 }
03146 #endif
03147
03148 static int dahdi_call(struct ast_channel *ast, char *rdest, int timeout)
03149 {
03150 struct dahdi_pvt *p = ast->tech_pvt;
03151 int x, res, idx,mysig;
03152 char *c, *n, *l;
03153 #ifdef HAVE_PRI
03154 char *s = NULL;
03155 #endif
03156 char dest[256];
03157 ast_mutex_lock(&p->lock);
03158 ast_copy_string(dest, rdest, sizeof(dest));
03159 ast_copy_string(p->dialdest, rdest, sizeof(p->dialdest));
03160 if ((ast->_state == AST_STATE_BUSY)) {
03161 p->subs[SUB_REAL].needbusy = 1;
03162 ast_mutex_unlock(&p->lock);
03163 return 0;
03164 }
03165 if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
03166 ast_log(LOG_WARNING, "dahdi_call called on %s, neither down nor reserved\n", ast->name);
03167 ast_mutex_unlock(&p->lock);
03168 return -1;
03169 }
03170 p->waitingfordt.tv_sec = 0;
03171 p->dialednone = 0;
03172 if ((p->radio || (p->oprmode < 0)))
03173 {
03174
03175 ast_setstate(ast, AST_STATE_UP);
03176 ast_mutex_unlock(&p->lock);
03177 return 0;
03178 }
03179 x = DAHDI_FLUSH_READ | DAHDI_FLUSH_WRITE;
03180 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
03181 if (res)
03182 ast_log(LOG_WARNING, "Unable to flush input on channel %d: %s\n", p->channel, strerror(errno));
03183 p->outgoing = 1;
03184
03185 if (IS_DIGITAL(ast->transfercapability)){
03186 set_actual_gain(p->subs[SUB_REAL].dfd, 0, 0, 0, p->law);
03187 } else {
03188 set_actual_gain(p->subs[SUB_REAL].dfd, 0, p->rxgain, p->txgain, p->law);
03189 }
03190
03191 mysig = p->sig;
03192 if (p->outsigmod > -1)
03193 mysig = p->outsigmod;
03194
03195 switch (mysig) {
03196 case SIG_FXOLS:
03197 case SIG_FXOGS:
03198 case SIG_FXOKS:
03199 if (p->owner == ast) {
03200
03201
03202
03203 p->dialing = 1;
03204 if (p->use_callerid) {
03205
03206 if (p->cidspill) {
03207 ast_log(LOG_WARNING, "cidspill already exists??\n");
03208 ast_free(p->cidspill);
03209 }
03210 p->callwaitcas = 0;
03211 if ((p->cidspill = ast_malloc(MAX_CALLERID_SIZE))) {
03212 p->cidlen = ast_callerid_generate(p->cidspill, ast->cid.cid_name, ast->cid.cid_num, AST_LAW(p));
03213 p->cidpos = 0;
03214 send_callerid(p);
03215 }
03216 }
03217
03218 if ((p->distinctivering > 0) && (p->distinctivering <= num_cadence)) {
03219 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &cadences[p->distinctivering - 1]))
03220 ast_log(LOG_WARNING, "Unable to set distinctive ring cadence %d on '%s': %s\n", p->distinctivering, ast->name, strerror(errno));
03221 p->cidrings = cidrings[p->distinctivering - 1];
03222 } else {
03223 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, NULL))
03224 ast_log(LOG_WARNING, "Unable to reset default ring on '%s': %s\n", ast->name, strerror(errno));
03225 p->cidrings = p->sendcalleridafter;
03226 }
03227
03228
03229 c = strchr(dest, '/');
03230 if (c)
03231 c++;
03232 if (c && (strlen(c) < p->stripmsd)) {
03233 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03234 c = NULL;
03235 }
03236 if (c) {
03237 p->dop.op = DAHDI_DIAL_OP_REPLACE;
03238 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "Tw%s", c);
03239 ast_debug(1, "FXO: setup deferred dialstring: %s\n", c);
03240 } else {
03241 p->dop.dialstr[0] = '\0';
03242 }
03243 x = DAHDI_RING;
03244 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x) && (errno != EINPROGRESS)) {
03245 ast_log(LOG_WARNING, "Unable to ring phone: %s\n", strerror(errno));
03246 ast_mutex_unlock(&p->lock);
03247 return -1;
03248 }
03249 p->dialing = 1;
03250 } else {
03251
03252 p->callwaitrings = 0;
03253 if (ast->cid.cid_num)
03254 ast_copy_string(p->callwait_num, ast->cid.cid_num, sizeof(p->callwait_num));
03255 else
03256 p->callwait_num[0] = '\0';
03257 if (ast->cid.cid_name)
03258 ast_copy_string(p->callwait_name, ast->cid.cid_name, sizeof(p->callwait_name));
03259 else
03260 p->callwait_name[0] = '\0';
03261
03262 if (dahdi_callwait(ast)) {
03263 ast_mutex_unlock(&p->lock);
03264 return -1;
03265 }
03266
03267 if (tone_zone_play_tone(p->subs[SUB_CALLWAIT].dfd, DAHDI_TONE_RINGTONE))
03268 ast_log(LOG_WARNING, "Unable to generate call-wait ring-back on channel %s\n", ast->name);
03269 }
03270 n = ast->cid.cid_name;
03271 l = ast->cid.cid_num;
03272 if (l)
03273 ast_copy_string(p->lastcid_num, l, sizeof(p->lastcid_num));
03274 else
03275 p->lastcid_num[0] = '\0';
03276 if (n)
03277 ast_copy_string(p->lastcid_name, n, sizeof(p->lastcid_name));
03278 else
03279 p->lastcid_name[0] = '\0';
03280 ast_setstate(ast, AST_STATE_RINGING);
03281 idx = dahdi_get_index(ast, p, 0);
03282 if (idx > -1) {
03283 p->subs[idx].needringing = 1;
03284 }
03285 break;
03286 case SIG_FXSLS:
03287 case SIG_FXSGS:
03288 case SIG_FXSKS:
03289 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
03290 ast_debug(1, "Ignore possible polarity reversal on line seizure\n");
03291 p->polaritydelaytv = ast_tvnow();
03292 }
03293
03294 case SIG_EMWINK:
03295 case SIG_EM:
03296 case SIG_EM_E1:
03297 case SIG_FEATD:
03298 case SIG_FEATDMF:
03299 case SIG_E911:
03300 case SIG_FGC_CAMA:
03301 case SIG_FGC_CAMAMF:
03302 case SIG_FEATB:
03303 case SIG_SFWINK:
03304 case SIG_SF:
03305 case SIG_SF_FEATD:
03306 case SIG_SF_FEATDMF:
03307 case SIG_FEATDMF_TA:
03308 case SIG_SF_FEATB:
03309 c = strchr(dest, '/');
03310 if (c)
03311 c++;
03312 else
03313 c = "";
03314 if (strlen(c) < p->stripmsd) {
03315 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03316 ast_mutex_unlock(&p->lock);
03317 return -1;
03318 }
03319 #ifdef HAVE_PRI
03320
03321 if (!p->pri) {
03322 #endif
03323 x = DAHDI_START;
03324 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
03325 if (res < 0) {
03326 if (errno != EINPROGRESS) {
03327 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
03328 ast_mutex_unlock(&p->lock);
03329 return -1;
03330 }
03331 }
03332 #ifdef HAVE_PRI
03333 }
03334 #endif
03335 ast_debug(1, "Dialing '%s'\n", c);
03336 p->dop.op = DAHDI_DIAL_OP_REPLACE;
03337
03338 c += p->stripmsd;
03339
03340 switch (mysig) {
03341 case SIG_FEATD:
03342 l = ast->cid.cid_num;
03343 if (l)
03344 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T*%s*%s*", l, c);
03345 else
03346 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T**%s*", c);
03347 break;
03348 case SIG_FEATDMF:
03349 l = ast->cid.cid_num;
03350 if (l)
03351 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*00%s#*%s#", l, c);
03352 else
03353 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*02#*%s#", c);
03354 break;
03355 case SIG_FEATDMF_TA:
03356 {
03357 const char *cic, *ozz;
03358
03359
03360 ozz = pbx_builtin_getvar_helper(p->owner, "FEATDMF_OZZ");
03361 if (!ozz)
03362 ozz = defaultozz;
03363 cic = pbx_builtin_getvar_helper(p->owner, "FEATDMF_CIC");
03364 if (!cic)
03365 cic = defaultcic;
03366 if (!ozz || !cic) {
03367 ast_log(LOG_WARNING, "Unable to dial channel of type feature group D MF tandem access without CIC or OZZ set\n");
03368 ast_mutex_unlock(&p->lock);
03369 return -1;
03370 }
03371 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s%s#", ozz, cic);
03372 snprintf(p->finaldial, sizeof(p->finaldial), "M*%s#", c);
03373 p->whichwink = 0;
03374 }
03375 break;
03376 case SIG_E911:
03377 ast_copy_string(p->dop.dialstr, "M*911#", sizeof(p->dop.dialstr));
03378 break;
03379 case SIG_FGC_CAMA:
03380 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%s", c);
03381 break;
03382 case SIG_FGC_CAMAMF:
03383 case SIG_FEATB:
03384 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%s#", c);
03385 break;
03386 default:
03387 if (p->pulse)
03388 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "P%sw", c);
03389 else
03390 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%sw", c);
03391 break;
03392 }
03393
03394 if (p->echotraining && (strlen(p->dop.dialstr) > 4)) {
03395 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
03396 strcpy(p->echorest + (p->echotraining / 400) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
03397 p->echorest[sizeof(p->echorest) - 1] = '\0';
03398 p->echobreak = 1;
03399 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
03400 } else
03401 p->echobreak = 0;
03402
03403
03404 #ifdef HAVE_PRI
03405 if (!p->pri) {
03406 #endif
03407 if( p->waitfordialtone && CANPROGRESSDETECT(p) && p->dsp ) {
03408 ast_log(LOG_DEBUG, "Defer dialling for %dms or dialtone\n", p->waitfordialtone);
03409 gettimeofday(&p->waitingfordt,NULL);
03410 ast_setstate(ast, AST_STATE_OFFHOOK);
03411 break;
03412 }
03413 #ifdef HAVE_PRI
03414 }
03415 #endif
03416 if (!res) {
03417 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
03418 int saveerr = errno;
03419
03420 x = DAHDI_ONHOOK;
03421 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
03422 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
03423 ast_mutex_unlock(&p->lock);
03424 return -1;
03425 }
03426 } else
03427 ast_debug(1, "Deferring dialing...\n");
03428
03429 p->dialing = 1;
03430 if (ast_strlen_zero(c))
03431 p->dialednone = 1;
03432 ast_setstate(ast, AST_STATE_DIALING);
03433 break;
03434 case 0:
03435
03436 ast_setstate(ast, AST_STATE_UP);
03437 break;
03438 case SIG_PRI:
03439 case SIG_BRI:
03440 case SIG_BRI_PTMP:
03441 case SIG_SS7:
03442 case SIG_MFCR2:
03443
03444 p->dialdest[0] = '\0';
03445 p->dialing = 1;
03446 break;
03447 default:
03448 ast_debug(1, "not yet implemented\n");
03449 ast_mutex_unlock(&p->lock);
03450 return -1;
03451 }
03452 #ifdef HAVE_SS7
03453 if (p->ss7) {
03454 char ss7_called_nai;
03455 int called_nai_strip;
03456 char ss7_calling_nai;
03457 int calling_nai_strip;
03458 const char *charge_str = NULL;
03459 const char *gen_address = NULL;
03460 const char *gen_digits = NULL;
03461 const char *gen_dig_type = NULL;
03462 const char *gen_dig_scheme = NULL;
03463 const char *gen_name = NULL;
03464 const char *jip_digits = NULL;
03465 const char *lspi_ident = NULL;
03466 const char *rlt_flag = NULL;
03467 const char *call_ref_id = NULL;
03468 const char *call_ref_pc = NULL;
03469 const char *send_far = NULL;
03470
03471 c = strchr(dest, '/');
03472 if (c) {
03473 c++;
03474 } else {
03475 c = "";
03476 }
03477 if (strlen(c) < p->stripmsd) {
03478 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03479 ast_mutex_unlock(&p->lock);
03480 return -1;
03481 }
03482
03483 if (!p->hidecallerid) {
03484 l = ast->cid.cid_num;
03485 } else {
03486 l = NULL;
03487 }
03488
03489 if (ss7_grab(p, p->ss7)) {
03490 ast_log(LOG_WARNING, "Failed to grab SS7!\n");
03491 ast_mutex_unlock(&p->lock);
03492 return -1;
03493 }
03494 p->digital = IS_DIGITAL(ast->transfercapability);
03495 p->ss7call = isup_new_call(p->ss7->ss7);
03496
03497 if (!p->ss7call) {
03498 ss7_rel(p->ss7);
03499 ast_mutex_unlock(&p->lock);
03500 ast_log(LOG_ERROR, "Unable to allocate new SS7 call!\n");
03501 return -1;
03502 }
03503
03504 called_nai_strip = 0;
03505 ss7_called_nai = p->ss7->called_nai;
03506 if (ss7_called_nai == SS7_NAI_DYNAMIC) {
03507 if (strncmp(c + p->stripmsd, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
03508 called_nai_strip = strlen(p->ss7->internationalprefix);
03509 ss7_called_nai = SS7_NAI_INTERNATIONAL;
03510 } else if (strncmp(c + p->stripmsd, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
03511 called_nai_strip = strlen(p->ss7->nationalprefix);
03512 ss7_called_nai = SS7_NAI_NATIONAL;
03513 } else {
03514 ss7_called_nai = SS7_NAI_SUBSCRIBER;
03515 }
03516 }
03517 isup_set_called(p->ss7call, c + p->stripmsd + called_nai_strip, ss7_called_nai, p->ss7->ss7);
03518
03519 calling_nai_strip = 0;
03520 ss7_calling_nai = p->ss7->calling_nai;
03521 if ((l != NULL) && (ss7_calling_nai == SS7_NAI_DYNAMIC)) {
03522 if (strncmp(l, p->ss7->internationalprefix, strlen(p->ss7->internationalprefix)) == 0) {
03523 calling_nai_strip = strlen(p->ss7->internationalprefix);
03524 ss7_calling_nai = SS7_NAI_INTERNATIONAL;
03525 } else if (strncmp(l, p->ss7->nationalprefix, strlen(p->ss7->nationalprefix)) == 0) {
03526 calling_nai_strip = strlen(p->ss7->nationalprefix);
03527 ss7_calling_nai = SS7_NAI_NATIONAL;
03528 } else {
03529 ss7_calling_nai = SS7_NAI_SUBSCRIBER;
03530 }
03531 }
03532 isup_set_calling(p->ss7call, l ? (l + calling_nai_strip) : NULL, ss7_calling_nai,
03533 p->use_callingpres ? cid_pres2ss7pres(ast->cid.cid_pres) : (l ? SS7_PRESENTATION_ALLOWED : SS7_PRESENTATION_RESTRICTED),
03534 p->use_callingpres ? cid_pres2ss7screen(ast->cid.cid_pres) : SS7_SCREENING_USER_PROVIDED );
03535
03536 isup_set_oli(p->ss7call, ast->cid.cid_ani2);
03537 isup_init_call(p->ss7->ss7, p->ss7call, p->cic, p->dpc);
03538
03539 ast_channel_lock(ast);
03540
03541 charge_str = pbx_builtin_getvar_helper(ast, "SS7_CHARGE_NUMBER");
03542 if (charge_str)
03543 isup_set_charge(p->ss7call, charge_str, SS7_ANI_CALLING_PARTY_SUB_NUMBER, 0x10);
03544
03545 gen_address = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_ADDRESS");
03546 if (gen_address)
03547 isup_set_gen_address(p->ss7call, gen_address, p->gen_add_nai,p->gen_add_pres_ind, p->gen_add_num_plan,p->gen_add_type);
03548
03549 gen_digits = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGITS");
03550 gen_dig_type = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGTYPE");
03551 gen_dig_scheme = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_DIGSCHEME");
03552 if (gen_digits)
03553 isup_set_gen_digits(p->ss7call, gen_digits, atoi(gen_dig_type), atoi(gen_dig_scheme));
03554
03555 gen_name = pbx_builtin_getvar_helper(ast, "SS7_GENERIC_NAME");
03556 if (gen_name)
03557 isup_set_generic_name(p->ss7call, gen_name, GEN_NAME_TYPE_CALLING_NAME, GEN_NAME_AVAIL_AVAILABLE, GEN_NAME_PRES_ALLOWED);
03558
03559 jip_digits = pbx_builtin_getvar_helper(ast, "SS7_JIP");
03560 if (jip_digits)
03561 isup_set_jip_digits(p->ss7call, jip_digits);
03562
03563 lspi_ident = pbx_builtin_getvar_helper(ast, "SS7_LSPI_IDENT");
03564 if (lspi_ident)
03565 isup_set_lspi(p->ss7call, lspi_ident, 0x18, 0x7, 0x00);
03566
03567 rlt_flag = pbx_builtin_getvar_helper(ast, "SS7_RLT_ON");
03568 if ((rlt_flag) && ((strncmp("NO", rlt_flag, strlen(rlt_flag))) != 0 )) {
03569 isup_set_lspi(p->ss7call, rlt_flag, 0x18, 0x7, 0x00);
03570 }
03571
03572 call_ref_id = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_IDENT");
03573 call_ref_pc = pbx_builtin_getvar_helper(ast, "SS7_CALLREF_PC");
03574 if (call_ref_id && call_ref_pc) {
03575 isup_set_callref(p->ss7call, atoi(call_ref_id),
03576 call_ref_pc ? atoi(call_ref_pc) : 0);
03577 }
03578
03579 send_far = pbx_builtin_getvar_helper(ast, "SS7_SEND_FAR");
03580 if ((send_far) && ((strncmp("NO", send_far, strlen(send_far))) != 0 ))
03581 (isup_far(p->ss7->ss7, p->ss7call));
03582
03583 ast_channel_unlock(ast);
03584
03585 isup_iam(p->ss7->ss7, p->ss7call);
03586 ast_setstate(ast, AST_STATE_DIALING);
03587 ss7_rel(p->ss7);
03588 }
03589 #endif
03590 #ifdef HAVE_OPENR2
03591 if (p->mfcr2) {
03592 openr2_calling_party_category_t chancat;
03593 int callres = 0;
03594 char *c, *l;
03595
03596 c = strchr(dest, '/');
03597 if (c) {
03598 c++;
03599 } else {
03600 c = "";
03601 }
03602 if (!p->hidecallerid) {
03603 l = ast->cid.cid_num;
03604 } else {
03605 l = NULL;
03606 }
03607 if (strlen(c) < p->stripmsd) {
03608 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03609 ast_mutex_unlock(&p->lock);
03610 return -1;
03611 }
03612 p->dialing = 1;
03613 ast_channel_lock(ast);
03614 chancat = dahdi_r2_get_channel_category(ast);
03615 ast_channel_unlock(ast);
03616 callres = openr2_chan_make_call(p->r2chan, l, (c + p->stripmsd), chancat);
03617 if (-1 == callres) {
03618 ast_mutex_unlock(&p->lock);
03619 ast_log(LOG_ERROR, "unable to make new MFC/R2 call!\n");
03620 return -1;
03621 }
03622 p->mfcr2_call_accepted = 0;
03623 p->mfcr2_progress = 0;
03624 ast_setstate(ast, AST_STATE_DIALING);
03625 }
03626 #endif
03627 #ifdef HAVE_PRI
03628 if (p->pri) {
03629 struct pri_sr *sr;
03630 #ifdef SUPPORT_USERUSER
03631 const char *useruser;
03632 #endif
03633 int pridialplan;
03634 int dp_strip;
03635 int prilocaldialplan;
03636 int ldp_strip;
03637 int exclusive;
03638 const char *rr_str;
03639 int redirect_reason;
03640
03641 c = strchr(dest, '/');
03642 if (c) {
03643 c++;
03644 } else {
03645 c = "";
03646 }
03647
03648 l = NULL;
03649 n = NULL;
03650 if (!p->hidecallerid) {
03651
03652
03653
03654
03655 for (l = ast->cid.cid_num; l && *l; l++) {
03656 if (strchr("0123456789", *l)) {
03657 l = ast->cid.cid_num;
03658 break;
03659 }
03660 }
03661 if (!p->hidecalleridname) {
03662 n = ast->cid.cid_name;
03663 }
03664 }
03665
03666 if (strlen(c) < p->stripmsd) {
03667 ast_log(LOG_WARNING, "Number '%s' is shorter than stripmsd (%d)\n", c, p->stripmsd);
03668 ast_mutex_unlock(&p->lock);
03669 return -1;
03670 }
03671 if (mysig != SIG_FXSKS) {
03672 p->dop.op = DAHDI_DIAL_OP_REPLACE;
03673 s = strchr(c + p->stripmsd, 'w');
03674 if (s) {
03675 if (strlen(s) > 1)
03676 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "T%s", s);
03677 else
03678 p->dop.dialstr[0] = '\0';
03679 *s = '\0';
03680 } else {
03681 p->dop.dialstr[0] = '\0';
03682 }
03683 }
03684 if (pri_grab(p, p->pri)) {
03685 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
03686 ast_mutex_unlock(&p->lock);
03687 return -1;
03688 }
03689 if (!(p->call = pri_new_call(p->pri->pri))) {
03690 ast_log(LOG_WARNING, "Unable to create call on channel %d\n", p->channel);
03691 pri_rel(p->pri);
03692 ast_mutex_unlock(&p->lock);
03693 return -1;
03694 }
03695 if (!(sr = pri_sr_new())) {
03696 ast_log(LOG_WARNING, "Failed to allocate setup request channel %d\n", p->channel);
03697 pri_rel(p->pri);
03698 ast_mutex_unlock(&p->lock);
03699 }
03700 if (p->bearer || (mysig == SIG_FXSKS)) {
03701 if (p->bearer) {
03702 ast_debug(1, "Oooh, I have a bearer on %d (%d:%d)\n", PVT_TO_CHANNEL(p->bearer), p->bearer->logicalspan, p->bearer->channel);
03703 p->bearer->call = p->call;
03704 } else
03705 ast_debug(1, "I'm being setup with no bearer right now...\n");
03706
03707 pri_set_crv(p->pri->pri, p->call, p->channel, 0);
03708 }
03709 p->digital = IS_DIGITAL(ast->transfercapability);
03710
03711
03712 if (p->priexclusive || p->pri->nodetype == PRI_NETWORK) {
03713 exclusive = 1;
03714 } else {
03715 exclusive = 0;
03716 }
03717
03718 pri_sr_set_channel(sr, p->bearer ? PVT_TO_CHANNEL(p->bearer) : PVT_TO_CHANNEL(p), exclusive, 1);
03719 pri_sr_set_bearer(sr, p->digital ? PRI_TRANS_CAP_DIGITAL : ast->transfercapability,
03720 (p->digital ? -1 :
03721 ((p->law == DAHDI_LAW_ALAW) ? PRI_LAYER_1_ALAW : PRI_LAYER_1_ULAW)));
03722 if (p->pri->facilityenable)
03723 pri_facility_enable(p->pri->pri);
03724
03725 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", ast->transfercapability, ast_transfercapability2str(ast->transfercapability));
03726
03727 dp_strip = 0;
03728 pridialplan = p->pri->dialplan - 1;
03729 if (pridialplan == -2 || pridialplan == -3) {
03730 if (strncmp(c + p->stripmsd, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
03731 if (pridialplan == -2) {
03732 dp_strip = strlen(p->pri->internationalprefix);
03733 }
03734 pridialplan = PRI_INTERNATIONAL_ISDN;
03735 } else if (strncmp(c + p->stripmsd, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
03736 if (pridialplan == -2) {
03737 dp_strip = strlen(p->pri->nationalprefix);
03738 }
03739 pridialplan = PRI_NATIONAL_ISDN;
03740 } else {
03741 pridialplan = PRI_LOCAL_ISDN;
03742 }
03743 }
03744 while (c[p->stripmsd] > '9' && c[p->stripmsd] != '*' && c[p->stripmsd] != '#') {
03745 switch (c[p->stripmsd]) {
03746 case 'U':
03747 pridialplan = (PRI_TON_UNKNOWN << 4) | (pridialplan & 0xf);
03748 break;
03749 case 'I':
03750 pridialplan = (PRI_TON_INTERNATIONAL << 4) | (pridialplan & 0xf);
03751 break;
03752 case 'N':
03753 pridialplan = (PRI_TON_NATIONAL << 4) | (pridialplan & 0xf);
03754 break;
03755 case 'L':
03756 pridialplan = (PRI_TON_NET_SPECIFIC << 4) | (pridialplan & 0xf);
03757 break;
03758 case 'S':
03759 pridialplan = (PRI_TON_SUBSCRIBER << 4) | (pridialplan & 0xf);
03760 break;
03761 case 'V':
03762 pridialplan = (PRI_TON_ABBREVIATED << 4) | (pridialplan & 0xf);
03763 break;
03764 case 'R':
03765 pridialplan = (PRI_TON_RESERVED << 4) | (pridialplan & 0xf);
03766 break;
03767 case 'u':
03768 pridialplan = PRI_NPI_UNKNOWN | (pridialplan & 0xf0);
03769 break;
03770 case 'e':
03771 pridialplan = PRI_NPI_E163_E164 | (pridialplan & 0xf0);
03772 break;
03773 case 'x':
03774 pridialplan = PRI_NPI_X121 | (pridialplan & 0xf0);
03775 break;
03776 case 'f':
03777 pridialplan = PRI_NPI_F69 | (pridialplan & 0xf0);
03778 break;
03779 case 'n':
03780 pridialplan = PRI_NPI_NATIONAL | (pridialplan & 0xf0);
03781 break;
03782 case 'p':
03783 pridialplan = PRI_NPI_PRIVATE | (pridialplan & 0xf0);
03784 break;
03785 case 'r':
03786 pridialplan = PRI_NPI_RESERVED | (pridialplan & 0xf0);
03787 break;
03788 default:
03789 if (isalpha(c[p->stripmsd])) {
03790 ast_log(LOG_WARNING, "Unrecognized pridialplan %s modifier: %c\n",
03791 c[p->stripmsd] > 'Z' ? "NPI" : "TON", c[p->stripmsd]);
03792 }
03793 break;
03794 }
03795 c++;
03796 }
03797 pri_sr_set_called(sr, c + p->stripmsd + dp_strip, pridialplan, s ? 1 : 0);
03798
03799 ldp_strip = 0;
03800 prilocaldialplan = p->pri->localdialplan - 1;
03801 if ((l != NULL) && (prilocaldialplan == -2 || prilocaldialplan == -3)) {
03802 if (strncmp(l, p->pri->internationalprefix, strlen(p->pri->internationalprefix)) == 0) {
03803 if (prilocaldialplan == -2) {
03804 ldp_strip = strlen(p->pri->internationalprefix);
03805 }
03806 prilocaldialplan = PRI_INTERNATIONAL_ISDN;
03807 } else if (strncmp(l, p->pri->nationalprefix, strlen(p->pri->nationalprefix)) == 0) {
03808 if (prilocaldialplan == -2) {
03809 ldp_strip = strlen(p->pri->nationalprefix);
03810 }
03811 prilocaldialplan = PRI_NATIONAL_ISDN;
03812 } else {
03813 prilocaldialplan = PRI_LOCAL_ISDN;
03814 }
03815 }
03816 if (l != NULL) {
03817 while (*l > '9' && *l != '*' && *l != '#') {
03818 switch (*l) {
03819 case 'U':
03820 prilocaldialplan = (PRI_TON_UNKNOWN << 4) | (prilocaldialplan & 0xf);
03821 break;
03822 case 'I':
03823 prilocaldialplan = (PRI_TON_INTERNATIONAL << 4) | (prilocaldialplan & 0xf);
03824 break;
03825 case 'N':
03826 prilocaldialplan = (PRI_TON_NATIONAL << 4) | (prilocaldialplan & 0xf);
03827 break;
03828 case 'L':
03829 prilocaldialplan = (PRI_TON_NET_SPECIFIC << 4) | (prilocaldialplan & 0xf);
03830 break;
03831 case 'S':
03832 prilocaldialplan = (PRI_TON_SUBSCRIBER << 4) | (prilocaldialplan & 0xf);
03833 break;
03834 case 'V':
03835 prilocaldialplan = (PRI_TON_ABBREVIATED << 4) | (prilocaldialplan & 0xf);
03836 break;
03837 case 'R':
03838 prilocaldialplan = (PRI_TON_RESERVED << 4) | (prilocaldialplan & 0xf);
03839 break;
03840 case 'u':
03841 prilocaldialplan = PRI_NPI_UNKNOWN | (prilocaldialplan & 0xf0);
03842 break;
03843 case 'e':
03844 prilocaldialplan = PRI_NPI_E163_E164 | (prilocaldialplan & 0xf0);
03845 break;
03846 case 'x':
03847 prilocaldialplan = PRI_NPI_X121 | (prilocaldialplan & 0xf0);
03848 break;
03849 case 'f':
03850 prilocaldialplan = PRI_NPI_F69 | (prilocaldialplan & 0xf0);
03851 break;
03852 case 'n':
03853 prilocaldialplan = PRI_NPI_NATIONAL | (prilocaldialplan & 0xf0);
03854 break;
03855 case 'p':
03856 prilocaldialplan = PRI_NPI_PRIVATE | (prilocaldialplan & 0xf0);
03857 break;
03858 case 'r':
03859 prilocaldialplan = PRI_NPI_RESERVED | (prilocaldialplan & 0xf0);
03860 break;
03861 default:
03862 if (isalpha(*l)) {
03863 ast_log(LOG_WARNING,
03864 "Unrecognized prilocaldialplan %s modifier: %c\n",
03865 *l > 'Z' ? "NPI" : "TON", *l);
03866 }
03867 break;
03868 }
03869 l++;
03870 }
03871 }
03872 pri_sr_set_caller(sr, l ? (l + ldp_strip) : NULL, n, prilocaldialplan,
03873 p->use_callingpres ? ast->cid.cid_pres : (l ? PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN : PRES_NUMBER_NOT_AVAILABLE));
03874 if ((rr_str = pbx_builtin_getvar_helper(ast, "PRIREDIRECTREASON"))) {
03875 if (!strcasecmp(rr_str, "UNKNOWN"))
03876 redirect_reason = 0;
03877 else if (!strcasecmp(rr_str, "BUSY"))
03878 redirect_reason = 1;
03879 else if (!strcasecmp(rr_str, "NO_REPLY") || !strcasecmp(rr_str, "NOANSWER"))
03880
03881 redirect_reason = 2;
03882 else if (!strcasecmp(rr_str, "UNCONDITIONAL"))
03883 redirect_reason = 15;
03884 else
03885 redirect_reason = PRI_REDIR_UNCONDITIONAL;
03886 } else
03887 redirect_reason = PRI_REDIR_UNCONDITIONAL;
03888 pri_sr_set_redirecting(sr, ast->cid.cid_rdnis, p->pri->localdialplan - 1, PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, redirect_reason);
03889
03890 #ifdef SUPPORT_USERUSER
03891
03892 useruser = pbx_builtin_getvar_helper(p->owner, "USERUSERINFO");
03893 if (useruser)
03894 pri_sr_set_useruser(sr, useruser);
03895 #endif
03896
03897 if (pri_setup(p->pri->pri, p->call, sr)) {
03898 ast_log(LOG_WARNING, "Unable to setup call to %s (using %s)\n",
03899 c + p->stripmsd + dp_strip, dialplan2str(p->pri->dialplan));
03900 pri_rel(p->pri);
03901 ast_mutex_unlock(&p->lock);
03902 pri_sr_free(sr);
03903 return -1;
03904 }
03905 pri_sr_free(sr);
03906 ast_setstate(ast, AST_STATE_DIALING);
03907 pri_rel(p->pri);
03908 }
03909 #endif
03910 ast_mutex_unlock(&p->lock);
03911 return 0;
03912 }
03913
03914 static void destroy_dahdi_pvt(struct dahdi_pvt **pvt)
03915 {
03916 struct dahdi_pvt *p = *pvt;
03917
03918 if (p->prev)
03919 p->prev->next = p->next;
03920 if (p->next)
03921 p->next->prev = p->prev;
03922
03923 ast_free(p->cidspill);
03924 if (p->use_smdi)
03925 ast_smdi_interface_unref(p->smdi_iface);
03926 if (p->mwi_event_sub)
03927 ast_event_unsubscribe(p->mwi_event_sub);
03928 if (p->vars) {
03929 ast_variables_destroy(p->vars);
03930 }
03931 ast_mutex_destroy(&p->lock);
03932 dahdi_close_sub(p, SUB_REAL);
03933 if (p->owner)
03934 p->owner->tech_pvt = NULL;
03935 free(p);
03936 *pvt = NULL;
03937 }
03938
03939 static int destroy_channel(struct dahdi_pvt *prev, struct dahdi_pvt *cur, int now)
03940 {
03941 int owned = 0;
03942 int i = 0;
03943
03944 if (!now) {
03945 if (cur->owner) {
03946 owned = 1;
03947 }
03948
03949 for (i = 0; i < 3; i++) {
03950 if (cur->subs[i].owner) {
03951 owned = 1;
03952 }
03953 }
03954 if (!owned) {
03955 if (prev) {
03956 prev->next = cur->next;
03957 if (prev->next)
03958 prev->next->prev = prev;
03959 else
03960 ifend = prev;
03961 } else {
03962 iflist = cur->next;
03963 if (iflist)
03964 iflist->prev = NULL;
03965 else
03966 ifend = NULL;
03967 }
03968 destroy_dahdi_pvt(&cur);
03969 }
03970 } else {
03971 if (prev) {
03972 prev->next = cur->next;
03973 if (prev->next)
03974 prev->next->prev = prev;
03975 else
03976 ifend = prev;
03977 } else {
03978 iflist = cur->next;
03979 if (iflist)
03980 iflist->prev = NULL;
03981 else
03982 ifend = NULL;
03983 }
03984 destroy_dahdi_pvt(&cur);
03985 }
03986 return 0;
03987 }
03988
03989 static void destroy_all_channels(void)
03990 {
03991 int x;
03992 struct dahdi_pvt *p, *pl;
03993
03994 while (num_restart_pending) {
03995 usleep(1);
03996 }
03997
03998 ast_mutex_lock(&iflock);
03999
04000 p = iflist;
04001 while (p) {
04002 pl = p;
04003 p = p->next;
04004 x = pl->channel;
04005
04006 destroy_dahdi_pvt(&pl);
04007 if (option_verbose > 2)
04008 ast_verbose(VERBOSE_PREFIX_2 "Unregistered channel %d\n", x);
04009 }
04010 iflist = NULL;
04011 ifcount = 0;
04012 ast_mutex_unlock(&iflock);
04013 }
04014
04015 #if defined(HAVE_PRI)
04016 static char *dahdi_send_keypad_facility_app = "DAHDISendKeypadFacility";
04017
04018 static int dahdi_send_keypad_facility_exec(struct ast_channel *chan, void *data)
04019 {
04020
04021 struct dahdi_pvt *p;
04022 char *digits = (char *) data;
04023
04024 if (ast_strlen_zero(digits)) {
04025 ast_debug(1, "No digit string sent to application!\n");
04026 return -1;
04027 }
04028
04029 p = (struct dahdi_pvt *)chan->tech_pvt;
04030
04031 if (!p) {
04032 ast_debug(1, "Unable to find technology private\n");
04033 return -1;
04034 }
04035
04036 ast_mutex_lock(&p->lock);
04037
04038 if (!p->pri || !p->call) {
04039 ast_debug(1, "Unable to find pri or call on channel!\n");
04040 ast_mutex_unlock(&p->lock);
04041 return -1;
04042 }
04043
04044 if (!pri_grab(p, p->pri)) {
04045 pri_keypad_facility(p->pri->pri, p->call, digits);
04046 pri_rel(p->pri);
04047 } else {
04048 ast_debug(1, "Unable to grab pri to send keypad facility!\n");
04049 ast_mutex_unlock(&p->lock);
04050 return -1;
04051 }
04052
04053 ast_mutex_unlock(&p->lock);
04054
04055 return 0;
04056 }
04057 #endif
04058
04059 #if defined(HAVE_PRI)
04060 #if defined(HAVE_PRI_PROG_W_CAUSE)
04061 static char *dahdi_send_callrerouting_facility_app = "DAHDISendCallreroutingFacility";
04062
04063 static int dahdi_send_callrerouting_facility_exec(struct ast_channel *chan, void *data)
04064 {
04065
04066 struct dahdi_pvt *p;
04067 char *parse;
04068 int res = -1;
04069 AST_DECLARE_APP_ARGS(args,
04070 AST_APP_ARG(destination);
04071 AST_APP_ARG(original);
04072 AST_APP_ARG(reason);
04073 );
04074
04075 if (ast_strlen_zero(data)) {
04076 ast_log(LOG_DEBUG, "No data sent to application!\n");
04077 return -1;
04078 }
04079
04080 p = (struct dahdi_pvt *)chan->tech_pvt;
04081
04082 if (!p) {
04083 ast_log(LOG_DEBUG, "Unable to find technology private\n");
04084 return -1;
04085 }
04086
04087 parse = ast_strdupa(data);
04088 AST_STANDARD_APP_ARGS(args, parse);
04089
04090 if (ast_strlen_zero(args.destination)) {
04091 ast_log(LOG_WARNING, "callrerouting facility requires at least destination number argument\n");
04092 return -1;
04093 }
04094
04095 if (ast_strlen_zero(args.original)) {
04096 ast_log(LOG_WARNING, "Callrerouting Facility without original called number argument\n");
04097 args.original = NULL;
04098 }
04099
04100 if (ast_strlen_zero(args.reason)) {
04101 ast_log(LOG_NOTICE, "Callrerouting Facility without diversion reason argument, defaulting to unknown\n");
04102 args.reason = NULL;
04103 }
04104
04105 ast_mutex_lock(&p->lock);
04106
04107 if (!p->pri || !p->call) {
04108 ast_log(LOG_DEBUG, "Unable to find pri or call on channel!\n");
04109 ast_mutex_unlock(&p->lock);
04110 return -1;
04111 }
04112
04113 switch (p->sig) {
04114 case SIG_PRI:
04115 if (!pri_grab(p, p->pri)) {
04116 if (chan->_state == AST_STATE_RING) {
04117 res = pri_callrerouting_facility(p->pri->pri, p->call, args.destination, args.original, args.reason);
04118 }
04119 pri_rel(p->pri);
04120 } else {
04121 ast_log(LOG_DEBUG, "Unable to grab pri to send callrerouting facility on span %d!\n", p->span);
04122 ast_mutex_unlock(&p->lock);
04123 return -1;
04124 }
04125 break;
04126 }
04127
04128 ast_mutex_unlock(&p->lock);
04129
04130 return res;
04131 }
04132 #endif
04133 #endif
04134
04135 #if defined(HAVE_PRI)
04136 static int pri_is_up(struct dahdi_pri *pri)
04137 {
04138 int x;
04139 for (x = 0; x < NUM_DCHANS; x++) {
04140 if (pri->dchanavail[x] == DCHAN_AVAILABLE)
04141 return 1;
04142 }
04143 return 0;
04144 }
04145 #endif
04146
04147 #if defined(HAVE_PRI)
04148 static int pri_assign_bearer(struct dahdi_pvt *crv, struct dahdi_pri *pri, struct dahdi_pvt *bearer)
04149 {
04150 bearer->owner = &inuse;
04151 bearer->realcall = crv;
04152 crv->subs[SUB_REAL].dfd = bearer->subs[SUB_REAL].dfd;
04153 if (crv->subs[SUB_REAL].owner)
04154 ast_channel_set_fd(crv->subs[SUB_REAL].owner, 0, crv->subs[SUB_REAL].dfd);
04155 crv->bearer = bearer;
04156 crv->call = bearer->call;
04157 crv->pri = pri;
04158 return 0;
04159 }
04160 #endif
04161
04162 #if defined(HAVE_PRI)
04163 static char *pri_order(int level)
04164 {
04165 switch (level) {
04166 case 0:
04167 return "Primary";
04168 case 1:
04169 return "Secondary";
04170 case 2:
04171 return "Tertiary";
04172 case 3:
04173 return "Quaternary";
04174 default:
04175 return "<Unknown>";
04176 }
04177 }
04178 #endif
04179
04180 #if defined(HAVE_PRI)
04181
04182 static int pri_active_dchan_fd(struct dahdi_pri *pri)
04183 {
04184 int x = -1;
04185
04186 for (x = 0; x < NUM_DCHANS; x++) {
04187 if ((pri->dchans[x] == pri->pri))
04188 break;
04189 }
04190
04191 return pri->fds[x];
04192 }
04193 #endif
04194
04195 #if defined(HAVE_PRI)
04196 static int pri_find_dchan(struct dahdi_pri *pri)
04197 {
04198 int oldslot = -1;
04199 struct pri *old;
04200 int newslot = -1;
04201 int x;
04202 old = pri->pri;
04203 for (x = 0; x < NUM_DCHANS; x++) {
04204 if ((pri->dchanavail[x] == DCHAN_AVAILABLE) && (newslot < 0))
04205 newslot = x;
04206 if (pri->dchans[x] == old) {
04207 oldslot = x;
04208 }
04209 }
04210 if (newslot < 0) {
04211 newslot = 0;
04212
04213 if (pri->sig != SIG_BRI_PTMP && !pri->no_d_channels) {
04214 pri->no_d_channels = 1;
04215 ast_log(LOG_WARNING,
04216 "No D-channels available! Using Primary channel %d as D-channel anyway!\n",
04217 pri->dchannels[newslot]);
04218 }
04219 } else {
04220 pri->no_d_channels = 0;
04221 }
04222 if (old && (oldslot != newslot))
04223 ast_log(LOG_NOTICE, "Switching from from d-channel %d to channel %d!\n",
04224 pri->dchannels[oldslot], pri->dchannels[newslot]);
04225 pri->pri = pri->dchans[newslot];
04226 return 0;
04227 }
04228 #endif
04229
04230 #if defined(HAVE_OPENR2)
04231 static const char *dahdi_accept_r2_call_app = "DAHDIAcceptR2Call";
04232
04233 static int dahdi_accept_r2_call_exec(struct ast_channel *chan, void *data)
04234 {
04235
04236 openr2_call_mode_t accept_mode;
04237 int res, timeout, maxloops;
04238 struct ast_frame *f;
04239 struct dahdi_pvt *p;
04240 char *parse;
04241 AST_DECLARE_APP_ARGS(args,
04242 AST_APP_ARG(charge);
04243 );
04244
04245 if (ast_strlen_zero(data)) {
04246 ast_log(LOG_DEBUG, "No data sent to application!\n");
04247 return -1;
04248 }
04249
04250 if (chan->tech != &dahdi_tech) {
04251 ast_log(LOG_DEBUG, "Only DAHDI technology accepted!\n");
04252 return -1;
04253 }
04254
04255 p = (struct dahdi_pvt *)chan->tech_pvt;
04256 if (!p) {
04257 ast_log(LOG_DEBUG, "Unable to find technology private!\n");
04258 return -1;
04259 }
04260
04261 parse = ast_strdupa(data);
04262 AST_STANDARD_APP_ARGS(args, parse);
04263
04264 if (ast_strlen_zero(args.charge)) {
04265 ast_log(LOG_WARNING, "DAHDIAcceptR2Call requires 'yes' or 'no' for the charge parameter\n");
04266 return -1;
04267 }
04268
04269 ast_mutex_lock(&p->lock);
04270 if (!p->mfcr2 || !p->mfcr2call) {
04271 ast_mutex_unlock(&p->lock);
04272 ast_log(LOG_DEBUG, "Channel %s does not seems to be an R2 active channel!\n", chan->name);
04273 return -1;
04274 }
04275
04276 if (p->mfcr2_call_accepted) {
04277 ast_mutex_unlock(&p->lock);
04278 ast_log(LOG_DEBUG, "MFC/R2 call already accepted on channel %s!\n", chan->name);
04279 return 0;
04280 }
04281 accept_mode = ast_true(args.charge) ? OR2_CALL_WITH_CHARGE : OR2_CALL_NO_CHARGE;
04282 if (openr2_chan_accept_call(p->r2chan, accept_mode)) {
04283 ast_mutex_unlock(&p->lock);
04284 ast_log(LOG_WARNING, "Failed to accept MFC/R2 call!\n");
04285 return -1;
04286 }
04287 ast_mutex_unlock(&p->lock);
04288
04289 res = 0;
04290 timeout = 100;
04291 maxloops = 50;
04292
04293 while (maxloops > 0) {
04294 maxloops--;
04295 if (ast_check_hangup(chan)) {
04296 break;
04297 }
04298 res = ast_waitfor(chan, timeout);
04299 if (res < 0) {
04300 ast_log(LOG_DEBUG, "ast_waitfor failed on channel %s, going out ...\n", chan->name);
04301 res = -1;
04302 break;
04303 }
04304 if (res == 0) {
04305 continue;
04306 }
04307 f = ast_read(chan);
04308 if (!f) {
04309 ast_log(LOG_DEBUG, "No frame read on channel %s, going out ...\n", chan->name);
04310 res = -1;
04311 break;
04312 }
04313 if (f->frametype == AST_FRAME_CONTROL && f->subclass == AST_CONTROL_HANGUP) {
04314 ast_log(LOG_DEBUG, "Got HANGUP frame on channel %s, going out ...\n", chan->name);
04315 ast_frfree(f);
04316 res = -1;
04317 break;
04318 }
04319 ast_frfree(f);
04320 ast_mutex_lock(&p->lock);
04321 if (p->mfcr2_call_accepted) {
04322 ast_mutex_unlock(&p->lock);
04323 ast_log(LOG_DEBUG, "Accepted MFC/R2 call!\n");
04324 break;
04325 }
04326 ast_mutex_unlock(&p->lock);
04327 }
04328 if (res == -1) {
04329 ast_log(LOG_WARNING, "Failed to accept MFC/R2 call!\n");
04330 }
04331 return res;
04332 }
04333
04334 static openr2_call_disconnect_cause_t dahdi_ast_cause_to_r2_cause(int cause)
04335 {
04336 openr2_call_disconnect_cause_t r2cause = OR2_CAUSE_NORMAL_CLEARING;
04337 switch (cause) {
04338 case AST_CAUSE_USER_BUSY:
04339 case AST_CAUSE_CALL_REJECTED:
04340 case AST_CAUSE_INTERWORKING:
04341 r2cause = OR2_CAUSE_BUSY_NUMBER;
04342 break;
04343
04344 case AST_CAUSE_NORMAL_CIRCUIT_CONGESTION:
04345 case AST_CAUSE_SWITCH_CONGESTION:
04346 r2cause = OR2_CAUSE_NETWORK_CONGESTION;
04347 break;
04348
04349 case AST_CAUSE_UNALLOCATED:
04350 r2cause = OR2_CAUSE_UNALLOCATED_NUMBER;
04351 break;
04352
04353 case AST_CAUSE_NETWORK_OUT_OF_ORDER:
04354 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
04355 r2cause = OR2_CAUSE_OUT_OF_ORDER;
04356 break;
04357
04358 case AST_CAUSE_NO_ANSWER:
04359 case AST_CAUSE_NO_USER_RESPONSE:
04360 r2cause = OR2_CAUSE_NO_ANSWER;
04361 break;
04362
04363 default:
04364 r2cause = OR2_CAUSE_NORMAL_CLEARING;
04365 break;
04366 }
04367 ast_log(LOG_DEBUG, "ast cause %d resulted in openr2 cause %d/%s\n",
04368 cause, r2cause, openr2_proto_get_disconnect_string(r2cause));
04369 return r2cause;
04370 }
04371 #endif
04372
04373 static int dahdi_hangup(struct ast_channel *ast)
04374 {
04375 int res;
04376 int idx,x, law;
04377
04378 struct dahdi_pvt *p = ast->tech_pvt;
04379 struct dahdi_pvt *tmp = NULL;
04380 struct dahdi_pvt *prev = NULL;
04381 struct dahdi_params par;
04382
04383 ast_debug(1, "dahdi_hangup(%s)\n", ast->name);
04384 if (!ast->tech_pvt) {
04385 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
04386 return 0;
04387 }
04388
04389 ast_mutex_lock(&p->lock);
04390
04391 idx = dahdi_get_index(ast, p, 1);
04392
04393 switch (p->sig) {
04394 case SIG_PRI:
04395 case SIG_BRI:
04396 case SIG_BRI_PTMP:
04397 case SIG_SS7:
04398 x = 1;
04399 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
04400
04401 case SIG_MFCR2:
04402 p->cid_num[0] = '\0';
04403 p->cid_name[0] = '\0';
04404 break;
04405 default:
04406 break;
04407 }
04408
04409 x = 0;
04410 dahdi_confmute(p, 0);
04411 p->muting = 0;
04412 restore_gains(p);
04413 if (p->origcid_num) {
04414 ast_copy_string(p->cid_num, p->origcid_num, sizeof(p->cid_num));
04415 ast_free(p->origcid_num);
04416 p->origcid_num = NULL;
04417 }
04418 if (p->origcid_name) {
04419 ast_copy_string(p->cid_name, p->origcid_name, sizeof(p->cid_name));
04420 ast_free(p->origcid_name);
04421 p->origcid_name = NULL;
04422 }
04423 if (p->dsp)
04424 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
04425 p->exten[0] = '\0';
04426
04427 ast_debug(1, "Hangup: channel: %d index = %d, normal = %d, callwait = %d, thirdcall = %d\n",
04428 p->channel, idx, p->subs[SUB_REAL].dfd, p->subs[SUB_CALLWAIT].dfd, p->subs[SUB_THREEWAY].dfd);
04429 p->ignoredtmf = 0;
04430
04431 if (idx > -1) {
04432
04433 p->subs[idx].owner = NULL;
04434 p->subs[idx].needanswer = 0;
04435 p->subs[idx].needflash = 0;
04436 p->subs[idx].needringing = 0;
04437 p->subs[idx].needbusy = 0;
04438 p->subs[idx].needcongestion = 0;
04439 p->subs[idx].linear = 0;
04440 p->subs[idx].needcallerid = 0;
04441 p->polarity = POLARITY_IDLE;
04442 dahdi_setlinear(p->subs[idx].dfd, 0);
04443 switch (idx) {
04444 case SUB_REAL:
04445 if ((p->subs[SUB_CALLWAIT].dfd > -1) && (p->subs[SUB_THREEWAY].dfd > -1)) {
04446 ast_debug(1, "Normal call hung up with both three way call and a call waiting call in place?\n");
04447 if (p->subs[SUB_CALLWAIT].inthreeway) {
04448
04449 ast_debug(1, "We were flipped over to the callwait, moving back and unowning.\n");
04450
04451 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
04452 unalloc_sub(p, SUB_CALLWAIT);
04453 p->owner = NULL;
04454 } else {
04455
04456 ast_debug(1, "We were in the threeway and have a callwait still. Ditching the threeway.\n");
04457 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04458 unalloc_sub(p, SUB_THREEWAY);
04459 if (p->subs[SUB_REAL].inthreeway) {
04460
04461
04462 ast_debug(1, "Call was complete, setting owner to former third call\n");
04463 p->subs[SUB_REAL].inthreeway = 0;
04464 p->owner = p->subs[SUB_REAL].owner;
04465 } else {
04466
04467 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
04468 p->owner = NULL;
04469 }
04470 }
04471 } else if (p->subs[SUB_CALLWAIT].dfd > -1) {
04472
04473 dahdi_lock_sub_owner(p, SUB_CALLWAIT);
04474 if (!p->subs[SUB_CALLWAIT].owner) {
04475
04476 p->owner = NULL;
04477 break;
04478 }
04479
04480
04481 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
04482 unalloc_sub(p, SUB_CALLWAIT);
04483 p->owner = p->subs[SUB_REAL].owner;
04484 if (p->owner->_state != AST_STATE_UP)
04485 p->subs[SUB_REAL].needanswer = 1;
04486 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
04487 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
04488
04489 ast_channel_unlock(p->subs[SUB_REAL].owner);
04490 } else if (p->subs[SUB_THREEWAY].dfd > -1) {
04491 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04492 unalloc_sub(p, SUB_THREEWAY);
04493 if (p->subs[SUB_REAL].inthreeway) {
04494
04495
04496 ast_debug(1, "Call was complete, setting owner to former third call\n");
04497 p->subs[SUB_REAL].inthreeway = 0;
04498 p->owner = p->subs[SUB_REAL].owner;
04499 } else {
04500
04501 ast_debug(1, "Call was incomplete, setting owner to NULL\n");
04502 p->owner = NULL;
04503 }
04504 }
04505 break;
04506 case SUB_CALLWAIT:
04507
04508 if (p->subs[SUB_CALLWAIT].inthreeway) {
04509
04510 dahdi_lock_sub_owner(p, SUB_THREEWAY);
04511
04512
04513
04514 if (p->subs[SUB_THREEWAY].owner && ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
04515 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
04516 S_OR(p->mohsuggest, NULL),
04517 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04518 }
04519 p->subs[SUB_THREEWAY].inthreeway = 0;
04520
04521 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
04522 unalloc_sub(p, SUB_THREEWAY);
04523 if (p->subs[SUB_CALLWAIT].owner) {
04524
04525 ast_channel_unlock(p->subs[SUB_CALLWAIT].owner);
04526 }
04527 } else
04528 unalloc_sub(p, SUB_CALLWAIT);
04529 break;
04530 case SUB_THREEWAY:
04531
04532 dahdi_lock_sub_owner(p, SUB_CALLWAIT);
04533 if (p->subs[SUB_CALLWAIT].inthreeway) {
04534
04535
04536 p->subs[SUB_CALLWAIT].inthreeway = 0;
04537 if (p->subs[SUB_CALLWAIT].owner && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
04538 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
04539 S_OR(p->mohsuggest, NULL),
04540 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
04541 }
04542 }
04543 if (p->subs[SUB_CALLWAIT].owner) {
04544 ast_channel_unlock(p->subs[SUB_CALLWAIT].owner);
04545 }
04546 p->subs[SUB_REAL].inthreeway = 0;
04547
04548
04549 unalloc_sub(p, SUB_THREEWAY);
04550 break;
04551 default:
04552
04553
04554
04555
04556 ast_log(LOG_ERROR, "Index found but not any type of call?\n");
04557 break;
04558 }
04559 }
04560
04561 if (!p->subs[SUB_REAL].owner && !p->subs[SUB_CALLWAIT].owner && !p->subs[SUB_THREEWAY].owner) {
04562 p->owner = NULL;
04563 p->ringt = 0;
04564 p->distinctivering = 0;
04565 p->confirmanswer = 0;
04566 p->cidrings = 1;
04567 p->outgoing = 0;
04568 p->digital = 0;
04569 p->faxhandled = 0;
04570 p->pulsedial = 0;
04571 p->onhooktime = time(NULL);
04572 #if defined(HAVE_PRI) || defined(HAVE_SS7)
04573 p->proceeding = 0;
04574 p->dialing = 0;
04575 p->progress = 0;
04576 p->alerting = 0;
04577 p->setup_ack = 0;
04578 p->rlt = 0;
04579 #endif
04580 if (p->dsp) {
04581 ast_dsp_free(p->dsp);
04582 p->dsp = NULL;
04583 }
04584
04585 if (p->bufferoverrideinuse) {
04586
04587 struct dahdi_bufferinfo bi = {
04588 .txbufpolicy = p->buf_policy,
04589 .rxbufpolicy = p->buf_policy,
04590 .bufsize = p->bufsize,
04591 .numbufs = p->buf_no
04592 };
04593 int bpres;
04594
04595 if ((bpres = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
04596 ast_log(LOG_WARNING, "Channel '%s' unable to revert faxbuffer policy: %s\n", ast->name, strerror(errno));
04597 }
04598 p->bufferoverrideinuse = 0;
04599 }
04600
04601 law = DAHDI_LAW_DEFAULT;
04602 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SETLAW, &law);
04603 if (res < 0)
04604 ast_log(LOG_WARNING, "Unable to set law on channel %d to default: %s\n", p->channel, strerror(errno));
04605
04606 #ifdef HAVE_SS7
04607 if (p->ss7) {
04608 if (p->ss7call) {
04609 if (!ss7_grab(p, p->ss7)) {
04610 if (!p->alreadyhungup) {
04611 const char *cause = pbx_builtin_getvar_helper(ast,"SS7_CAUSE");
04612 int icause = ast->hangupcause ? ast->hangupcause : -1;
04613
04614 if (cause) {
04615 if (atoi(cause))
04616 icause = atoi(cause);
04617 }
04618 isup_rel(p->ss7->ss7, p->ss7call, icause);
04619 ss7_rel(p->ss7);
04620 p->alreadyhungup = 1;
04621 } else
04622 ast_log(LOG_WARNING, "Trying to hangup twice!\n");
04623 } else {
04624 ast_log(LOG_WARNING, "Unable to grab SS7 on CIC %d\n", p->cic);
04625 res = -1;
04626 }
04627 }
04628 }
04629 #endif
04630 #ifdef HAVE_OPENR2
04631 if (p->mfcr2 && p->mfcr2call && openr2_chan_get_direction(p->r2chan) != OR2_DIR_STOPPED) {
04632 ast_log(LOG_DEBUG, "disconnecting MFC/R2 call on chan %d\n", p->channel);
04633
04634 if (openr2_chan_get_direction(p->r2chan) == OR2_DIR_BACKWARD && p->mfcr2_forced_release) {
04635 dahdi_r2_disconnect_call(p, OR2_CAUSE_FORCED_RELEASE);
04636 } else {
04637 const char *r2causestr = pbx_builtin_getvar_helper(ast, "MFCR2_CAUSE");
04638 int r2cause_user = r2causestr ? atoi(r2causestr) : 0;
04639 openr2_call_disconnect_cause_t r2cause = r2cause_user ? dahdi_ast_cause_to_r2_cause(r2cause_user)
04640 : dahdi_ast_cause_to_r2_cause(ast->hangupcause);
04641 dahdi_r2_disconnect_call(p, r2cause);
04642 }
04643 } else if (p->mfcr2call) {
04644 ast_log(LOG_DEBUG, "Clearing call request on channel %d\n", p->channel);
04645
04646
04647
04648
04649 p->mfcr2call = 0;
04650 }
04651 #endif
04652 #ifdef HAVE_PRI
04653 if (p->pri) {
04654 #ifdef SUPPORT_USERUSER
04655 const char *useruser = pbx_builtin_getvar_helper(ast,"USERUSERINFO");
04656 #endif
04657
04658
04659 if (p->call && (!p->bearer || (p->bearer->call == p->call))) {
04660 if (!pri_grab(p, p->pri)) {
04661 if (p->alreadyhungup) {
04662 ast_debug(1, "Already hungup... Calling hangup once, and clearing call\n");
04663
04664 #ifdef SUPPORT_USERUSER
04665 pri_call_set_useruser(p->call, useruser);
04666 #endif
04667
04668 pri_hangup(p->pri->pri, p->call, -1);
04669 p->call = NULL;
04670 if (p->bearer)
04671 p->bearer->call = NULL;
04672 } else {
04673 const char *cause = pbx_builtin_getvar_helper(ast,"PRI_CAUSE");
04674 int icause = ast->hangupcause ? ast->hangupcause : -1;
04675 ast_debug(1, "Not yet hungup... Calling hangup once with icause, and clearing call\n");
04676
04677 #ifdef SUPPORT_USERUSER
04678 pri_call_set_useruser(p->call, useruser);
04679 #endif
04680
04681 p->alreadyhungup = 1;
04682 if (p->bearer)
04683 p->bearer->alreadyhungup = 1;
04684 if (cause) {
04685 if (atoi(cause))
04686 icause = atoi(cause);
04687 }
04688 pri_hangup(p->pri->pri, p->call, icause);
04689 }
04690 if (res < 0)
04691 ast_log(LOG_WARNING, "pri_disconnect failed\n");
04692 pri_rel(p->pri);
04693 } else {
04694 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04695 res = -1;
04696 }
04697 } else {
04698 if (p->bearer)
04699 ast_debug(1, "Bearer call is %p, while ours is still %p\n", p->bearer->call, p->call);
04700 p->call = NULL;
04701 res = 0;
04702 }
04703 }
04704 #endif
04705 if (p->sig && ((p->sig != SIG_PRI) && (p->sig != SIG_SS7)
04706 && (p->sig != SIG_BRI)
04707 && (p->sig != SIG_BRI_PTMP))
04708 && (p->sig != SIG_MFCR2))
04709 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
04710 if (res < 0) {
04711 ast_log(LOG_WARNING, "Unable to hangup line %s\n", ast->name);
04712 }
04713 switch (p->sig) {
04714 case SIG_FXOGS:
04715 case SIG_FXOLS:
04716 case SIG_FXOKS:
04717 memset(&par, 0, sizeof(par));
04718 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
04719 if (!res) {
04720 #if 0
04721 ast_debug(1, "Hanging up channel %d, offhook = %d\n", p->channel, par.rxisoffhook);
04722 #endif
04723
04724 if ((par.rxisoffhook) && (!(p->radio || (p->oprmode < 0))))
04725 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
04726 else
04727 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
04728 p->fxsoffhookstate = par.rxisoffhook;
04729 }
04730 break;
04731 case SIG_FXSGS:
04732 case SIG_FXSLS:
04733 case SIG_FXSKS:
04734
04735
04736 if (ast->_state != AST_STATE_RESERVED) {
04737 time(&p->guardtime);
04738 p->guardtime += 2;
04739 }
04740 break;
04741 default:
04742 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
04743 }
04744 ast_free(p->cidspill);
04745 p->cidspill = NULL;
04746 if (p->sig)
04747 dahdi_disable_ec(p);
04748 x = 0;
04749 ast_channel_setoption(ast,AST_OPTION_TONE_VERIFY,&x,sizeof(char),0);
04750 ast_channel_setoption(ast,AST_OPTION_TDD,&x,sizeof(char),0);
04751 p->didtdd = 0;
04752 p->callwaitcas = 0;
04753 p->callwaiting = p->permcallwaiting;
04754 p->hidecallerid = p->permhidecallerid;
04755 p->waitingfordt.tv_sec = 0;
04756 p->dialing = 0;
04757 p->rdnis[0] = '\0';
04758 update_conf(p);
04759 reset_conf(p);
04760
04761 if ((p->sig == SIG_PRI) || (p->sig == SIG_SS7) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
04762 x = 0;
04763 ast_channel_setoption(ast,AST_OPTION_AUDIO_MODE,&x,sizeof(char),0);
04764 }
04765 #ifdef HAVE_PRI
04766 if (p->bearer) {
04767 ast_debug(1, "Freeing up bearer channel %d\n", p->bearer->channel);
04768
04769
04770 update_conf(p->bearer);
04771 reset_conf(p->bearer);
04772 p->bearer->owner = NULL;
04773 p->bearer->realcall = NULL;
04774 p->bearer = NULL;
04775 p->subs[SUB_REAL].dfd = -1;
04776 p->pri = NULL;
04777 }
04778 #endif
04779 if (num_restart_pending == 0)
04780 restart_monitor();
04781 }
04782
04783 p->callwaitingrepeat = 0;
04784 p->cidcwexpire = 0;
04785 p->cid_suppress_expire = 0;
04786 p->oprmode = 0;
04787 ast->tech_pvt = NULL;
04788 ast_mutex_unlock(&p->lock);
04789 ast_module_unref(ast_module_info->self);
04790 ast_verb(3, "Hungup '%s'\n", ast->name);
04791
04792 ast_mutex_lock(&iflock);
04793
04794 if (p->restartpending) {
04795 num_restart_pending--;
04796 }
04797
04798 tmp = iflist;
04799 prev = NULL;
04800 if (p->destroy) {
04801 while (tmp) {
04802 if (tmp == p) {
04803 destroy_channel(prev, tmp, 0);
04804 break;
04805 } else {
04806 prev = tmp;
04807 tmp = tmp->next;
04808 }
04809 }
04810 }
04811 ast_mutex_unlock(&iflock);
04812 return 0;
04813 }
04814
04815 static int dahdi_answer(struct ast_channel *ast)
04816 {
04817 struct dahdi_pvt *p = ast->tech_pvt;
04818 int res = 0;
04819 int idx;
04820 int oldstate = ast->_state;
04821 ast_setstate(ast, AST_STATE_UP);
04822 ast_mutex_lock(&p->lock);
04823 idx = dahdi_get_index(ast, p, 0);
04824 if (idx < 0)
04825 idx = SUB_REAL;
04826
04827 if ((p->radio || (p->oprmode < 0))) {
04828 ast_mutex_unlock(&p->lock);
04829 return 0;
04830 }
04831 switch (p->sig) {
04832 case SIG_FXSLS:
04833 case SIG_FXSGS:
04834 case SIG_FXSKS:
04835 p->ringt = 0;
04836
04837 case SIG_EM:
04838 case SIG_EM_E1:
04839 case SIG_EMWINK:
04840 case SIG_FEATD:
04841 case SIG_FEATDMF:
04842 case SIG_FEATDMF_TA:
04843 case SIG_E911:
04844 case SIG_FGC_CAMA:
04845 case SIG_FGC_CAMAMF:
04846 case SIG_FEATB:
04847 case SIG_SF:
04848 case SIG_SFWINK:
04849 case SIG_SF_FEATD:
04850 case SIG_SF_FEATDMF:
04851 case SIG_SF_FEATB:
04852 case SIG_FXOLS:
04853 case SIG_FXOGS:
04854 case SIG_FXOKS:
04855
04856 ast_debug(1, "Took %s off hook\n", ast->name);
04857 if (p->hanguponpolarityswitch) {
04858 p->polaritydelaytv = ast_tvnow();
04859 }
04860 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
04861 tone_zone_play_tone(p->subs[idx].dfd, -1);
04862 p->dialing = 0;
04863 if ((idx == SUB_REAL) && p->subs[SUB_THREEWAY].inthreeway) {
04864 if (oldstate == AST_STATE_RINGING) {
04865 ast_debug(1, "Finally swapping real and threeway\n");
04866 tone_zone_play_tone(p->subs[SUB_THREEWAY].dfd, -1);
04867 swap_subs(p, SUB_THREEWAY, SUB_REAL);
04868 p->owner = p->subs[SUB_REAL].owner;
04869 }
04870 }
04871 if (p->sig & __DAHDI_SIG_FXS) {
04872 dahdi_enable_ec(p);
04873 dahdi_train_ec(p);
04874 }
04875 break;
04876 #ifdef HAVE_PRI
04877 case SIG_BRI:
04878 case SIG_BRI_PTMP:
04879 case SIG_PRI:
04880
04881 if (!pri_grab(p, p->pri)) {
04882 p->proceeding = 1;
04883 p->dialing = 0;
04884 res = pri_answer(p->pri->pri, p->call, 0, !p->digital);
04885 pri_rel(p->pri);
04886 } else {
04887 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
04888 res = -1;
04889 }
04890 break;
04891 #endif
04892 #ifdef HAVE_SS7
04893 case SIG_SS7:
04894 if (!ss7_grab(p, p->ss7)) {
04895 p->proceeding = 1;
04896 res = isup_anm(p->ss7->ss7, p->ss7call);
04897 ss7_rel(p->ss7);
04898 } else {
04899 ast_log(LOG_WARNING, "Unable to grab SS7 on span %d\n", p->span);
04900 res = -1;
04901 }
04902 break;
04903 #endif
04904 #ifdef HAVE_OPENR2
04905 case SIG_MFCR2:
04906 if (!p->mfcr2_call_accepted) {
04907
04908
04909 p->mfcr2_answer_pending = 1;
04910 if (p->mfcr2_charge_calls) {
04911 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with charge before answering on chan %d\n", p->channel);
04912 openr2_chan_accept_call(p->r2chan, OR2_CALL_WITH_CHARGE);
04913 } else {
04914 ast_log(LOG_DEBUG, "Accepting MFC/R2 call with no charge before answering on chan %d\n", p->channel);
04915 openr2_chan_accept_call(p->r2chan, OR2_CALL_NO_CHARGE);
04916 }
04917 } else {
04918 ast_log(LOG_DEBUG, "Answering MFC/R2 call on chan %d\n", p->channel);
04919 dahdi_r2_answer(p);
04920 }
04921 break;
04922 #endif
04923 case 0:
04924 ast_mutex_unlock(&p->lock);
04925 return 0;
04926 default:
04927 ast_log(LOG_WARNING, "Don't know how to answer signalling %d (channel %d)\n", p->sig, p->channel);
04928 res = -1;
04929 }
04930 ast_mutex_unlock(&p->lock);
04931 return res;
04932 }
04933
04934 static int dahdi_setoption(struct ast_channel *chan, int option, void *data, int datalen)
04935 {
04936 char *cp;
04937 signed char *scp;
04938 int x;
04939 int idx;
04940 struct dahdi_pvt *p = chan->tech_pvt, *pp;
04941 struct oprmode *oprmode;
04942
04943
04944
04945 if (!data || (datalen < 1)) {
04946 errno = EINVAL;
04947 return -1;
04948 }
04949
04950 switch (option) {
04951 case AST_OPTION_TXGAIN:
04952 scp = (signed char *) data;
04953 idx = dahdi_get_index(chan, p, 0);
04954 if (idx < 0) {
04955 ast_log(LOG_WARNING, "No index in TXGAIN?\n");
04956 return -1;
04957 }
04958 ast_debug(1, "Setting actual tx gain on %s to %f\n", chan->name, p->txgain + (float) *scp);
04959 return set_actual_txgain(p->subs[idx].dfd, 0, p->txgain + (float) *scp, p->law);
04960 case AST_OPTION_RXGAIN:
04961 scp = (signed char *) data;
04962 idx = dahdi_get_index(chan, p, 0);
04963 if (idx < 0) {
04964 ast_log(LOG_WARNING, "No index in RXGAIN?\n");
04965 return -1;
04966 }
04967 ast_debug(1, "Setting actual rx gain on %s to %f\n", chan->name, p->rxgain + (float) *scp);
04968 return set_actual_rxgain(p->subs[idx].dfd, 0, p->rxgain + (float) *scp, p->law);
04969 case AST_OPTION_TONE_VERIFY:
04970 if (!p->dsp)
04971 break;
04972 cp = (char *) data;
04973 switch (*cp) {
04974 case 1:
04975 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF(1) on %s\n",chan->name);
04976 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MUTECONF | p->dtmfrelax);
04977 break;
04978 case 2:
04979 ast_debug(1, "Set option TONE VERIFY, mode: MUTECONF/MAX(2) on %s\n",chan->name);
04980 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX | p->dtmfrelax);
04981 break;
04982 default:
04983 ast_debug(1, "Set option TONE VERIFY, mode: OFF(0) on %s\n",chan->name);
04984 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
04985 break;
04986 }
04987 break;
04988 case AST_OPTION_TDD:
04989
04990 cp = (char *) data;
04991 p->mate = 0;
04992 if (!*cp) {
04993 ast_debug(1, "Set option TDD MODE, value: OFF(0) on %s\n",chan->name);
04994 if (p->tdd)
04995 tdd_free(p->tdd);
04996 p->tdd = 0;
04997 break;
04998 }
04999 ast_debug(1, "Set option TDD MODE, value: %s(%d) on %s\n",
05000 (*cp == 2) ? "MATE" : "ON", (int) *cp, chan->name);
05001 dahdi_disable_ec(p);
05002
05003 if (!p->didtdd) {
05004 unsigned char mybuf[41000];
05005 unsigned char *buf;
05006 int size, res, fd, len;
05007 struct pollfd fds[1];
05008
05009 buf = mybuf;
05010 memset(buf, 0x7f, sizeof(mybuf));
05011 ast_tdd_gen_ecdisa(buf + 16000, 16000);
05012 len = 40000;
05013 idx = dahdi_get_index(chan, p, 0);
05014 if (idx < 0) {
05015 ast_log(LOG_WARNING, "No index in TDD?\n");
05016 return -1;
05017 }
05018 fd = p->subs[idx].dfd;
05019 while (len) {
05020 if (ast_check_hangup(chan))
05021 return -1;
05022 size = len;
05023 if (size > READ_SIZE)
05024 size = READ_SIZE;
05025 fds[0].fd = fd;
05026 fds[0].events = POLLPRI | POLLOUT;
05027 fds[0].revents = 0;
05028 res = poll(fds, 1, -1);
05029 if (!res) {
05030 ast_debug(1, "poll (for write) ret. 0 on channel %d\n", p->channel);
05031 continue;
05032 }
05033
05034 if (fds[0].revents & POLLPRI)
05035 return -1;
05036 if (!(fds[0].revents & POLLOUT)) {
05037 ast_debug(1, "write fd not ready on channel %d\n", p->channel);
05038 continue;
05039 }
05040 res = write(fd, buf, size);
05041 if (res != size) {
05042 if (res == -1) return -1;
05043 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
05044 break;
05045 }
05046 len -= size;
05047 buf += size;
05048 }
05049 p->didtdd = 1;
05050 }
05051 if (*cp == 2) {
05052 if (p->tdd)
05053 tdd_free(p->tdd);
05054 p->tdd = 0;
05055 p->mate = 1;
05056 break;
05057 }
05058 if (!p->tdd) {
05059 p->tdd = tdd_new();
05060 }
05061 break;
05062 case AST_OPTION_RELAXDTMF:
05063 if (!p->dsp)
05064 break;
05065 cp = (char *) data;
05066 ast_debug(1, "Set option RELAX DTMF, value: %s(%d) on %s\n",
05067 *cp ? "ON" : "OFF", (int) *cp, chan->name);
05068 ast_dsp_set_digitmode(p->dsp, ((*cp) ? DSP_DIGITMODE_RELAXDTMF : DSP_DIGITMODE_DTMF) | p->dtmfrelax);
05069 break;
05070 case AST_OPTION_AUDIO_MODE:
05071 cp = (char *) data;
05072 if (!*cp) {
05073 ast_debug(1, "Set option AUDIO MODE, value: OFF(0) on %s\n", chan->name);
05074 x = 0;
05075 dahdi_disable_ec(p);
05076 } else {
05077 ast_debug(1, "Set option AUDIO MODE, value: ON(1) on %s\n", chan->name);
05078 x = 1;
05079 }
05080 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &x) == -1)
05081 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", p->channel, x, strerror(errno));
05082 break;
05083 case AST_OPTION_OPRMODE:
05084 oprmode = (struct oprmode *) data;
05085
05086 if (strcasecmp(chan->tech->type, oprmode->peer->tech->type)) {
05087 ast_log(LOG_NOTICE, "Operator mode not supported on %s to %s calls.\n",
05088 chan->tech->type, oprmode->peer->tech->type);
05089 errno = EINVAL;
05090 return -1;
05091 }
05092 pp = oprmode->peer->tech_pvt;
05093 p->oprmode = pp->oprmode = 0;
05094
05095 p->oprpeer = pp;
05096 pp->oprpeer = p;
05097
05098 if (oprmode->mode)
05099 {
05100 pp->oprmode = oprmode->mode;
05101 p->oprmode = -oprmode->mode;
05102 }
05103 ast_debug(1, "Set Operator Services mode, value: %d on %s/%s\n",
05104 oprmode->mode, chan->name,oprmode->peer->name);
05105 break;
05106 case AST_OPTION_ECHOCAN:
05107 cp = (char *) data;
05108 if (*cp) {
05109 ast_debug(1, "Enabling echo cancellation on %s\n", chan->name);
05110 dahdi_enable_ec(p);
05111 } else {
05112 ast_debug(1, "Disabling echo cancellation on %s\n", chan->name);
05113 dahdi_disable_ec(p);
05114 }
05115 break;
05116 }
05117 errno = 0;
05118
05119 return 0;
05120 }
05121
05122 static int dahdi_func_read(struct ast_channel *chan, const char *function, char *data, char *buf, size_t len)
05123 {
05124 struct dahdi_pvt *p = chan->tech_pvt;
05125 int res = 0;
05126
05127 if (!strcasecmp(data, "rxgain")) {
05128 ast_mutex_lock(&p->lock);
05129 snprintf(buf, len, "%f", p->rxgain);
05130 ast_mutex_unlock(&p->lock);
05131 } else if (!strcasecmp(data, "txgain")) {
05132 ast_mutex_lock(&p->lock);
05133 snprintf(buf, len, "%f", p->txgain);
05134 ast_mutex_unlock(&p->lock);
05135 } else {
05136 ast_copy_string(buf, "", len);
05137 res = -1;
05138 }
05139
05140 return res;
05141 }
05142
05143
05144 static int parse_buffers_policy(const char *parse, int *num_buffers, int *policy)
05145 {
05146 int res;
05147 char policy_str[21] = "";
05148
05149 if (((res = sscanf(parse, "%d,%20s", num_buffers, policy_str)) != 2) &&
05150 ((res = sscanf(parse, "%d|%20s", num_buffers, policy_str)) != 2)) {
05151 ast_log(LOG_WARNING, "Parsing buffer string '%s' failed.\n", parse);
05152 return 1;
05153 }
05154 if (*num_buffers < 0) {
05155 ast_log(LOG_WARNING, "Invalid buffer count given '%d'.\n", *num_buffers);
05156 return -1;
05157 }
05158 if (!strcasecmp(policy_str, "full")) {
05159 *policy = DAHDI_POLICY_WHEN_FULL;
05160 } else if (!strcasecmp(policy_str, "immediate")) {
05161 *policy = DAHDI_POLICY_IMMEDIATE;
05162 #ifdef DAHDI_POLICY_HALF_FULL
05163 } else if (!strcasecmp(policy_str, "half")) {
05164 *policy = DAHDI_POLICY_HALF_FULL;
05165 #endif
05166 } else {
05167 ast_log(LOG_WARNING, "Invalid policy name given '%s'.\n", policy_str);
05168 return -1;
05169 }
05170
05171 return 0;
05172 }
05173
05174 static int dahdi_func_write(struct ast_channel *chan, const char *function, char *data, const char *value)
05175 {
05176 struct dahdi_pvt *p = chan->tech_pvt;
05177 int res = 0;
05178
05179 if (!strcasecmp(data, "buffers")) {
05180 int num_bufs, policy;
05181
05182 if (!(parse_buffers_policy(value, &num_bufs, &policy))) {
05183 struct dahdi_bufferinfo bi = {
05184 .txbufpolicy = policy,
05185 .rxbufpolicy = policy,
05186 .bufsize = p->bufsize,
05187 .numbufs = num_bufs,
05188 };
05189 int bpres;
05190
05191 if ((bpres = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
05192 ast_log(LOG_WARNING, "Channel '%d' unable to override buffer policy: %s\n", p->channel, strerror(errno));
05193 } else {
05194 p->bufferoverrideinuse = 1;
05195 }
05196 } else {
05197 res = -1;
05198 }
05199 } else {
05200 res = -1;
05201 }
05202
05203 return res;
05204 }
05205
05206 static void dahdi_unlink(struct dahdi_pvt *slave, struct dahdi_pvt *master, int needlock)
05207 {
05208
05209 int x;
05210 int hasslaves;
05211 if (!master)
05212 return;
05213 if (needlock) {
05214 ast_mutex_lock(&master->lock);
05215 if (slave) {
05216 while (ast_mutex_trylock(&slave->lock)) {
05217 DEADLOCK_AVOIDANCE(&master->lock);
05218 }
05219 }
05220 }
05221 hasslaves = 0;
05222 for (x = 0; x < MAX_SLAVES; x++) {
05223 if (master->slaves[x]) {
05224 if (!slave || (master->slaves[x] == slave)) {
05225
05226 ast_debug(1, "Unlinking slave %d from %d\n", master->slaves[x]->channel, master->channel);
05227 conf_del(master, &master->slaves[x]->subs[SUB_REAL], SUB_REAL);
05228 conf_del(master->slaves[x], &master->subs[SUB_REAL], SUB_REAL);
05229 master->slaves[x]->master = NULL;
05230 master->slaves[x] = NULL;
05231 } else
05232 hasslaves = 1;
05233 }
05234 if (!hasslaves)
05235 master->inconference = 0;
05236 }
05237 if (!slave) {
05238 if (master->master) {
05239
05240 conf_del(master->master, &master->subs[SUB_REAL], SUB_REAL);
05241 conf_del(master, &master->master->subs[SUB_REAL], SUB_REAL);
05242 hasslaves = 0;
05243 for (x = 0; x < MAX_SLAVES; x++) {
05244 if (master->master->slaves[x] == master)
05245 master->master->slaves[x] = NULL;
05246 else if (master->master->slaves[x])
05247 hasslaves = 1;
05248 }
05249 if (!hasslaves)
05250 master->master->inconference = 0;
05251 }
05252 master->master = NULL;
05253 }
05254 update_conf(master);
05255 if (needlock) {
05256 if (slave)
05257 ast_mutex_unlock(&slave->lock);
05258 ast_mutex_unlock(&master->lock);
05259 }
05260 }
05261
05262 static void dahdi_link(struct dahdi_pvt *slave, struct dahdi_pvt *master) {
05263 int x;
05264 if (!slave || !master) {
05265 ast_log(LOG_WARNING, "Tried to link to/from NULL??\n");
05266 return;
05267 }
05268 for (x = 0; x < MAX_SLAVES; x++) {
05269 if (!master->slaves[x]) {
05270 master->slaves[x] = slave;
05271 break;
05272 }
05273 }
05274 if (x >= MAX_SLAVES) {
05275 ast_log(LOG_WARNING, "Replacing slave %d with new slave, %d\n", master->slaves[MAX_SLAVES - 1]->channel, slave->channel);
05276 master->slaves[MAX_SLAVES - 1] = slave;
05277 }
05278 if (slave->master)
05279 ast_log(LOG_WARNING, "Replacing master %d with new master, %d\n", slave->master->channel, master->channel);
05280 slave->master = master;
05281
05282 ast_debug(1, "Making %d slave to master %d at %d\n", slave->channel, master->channel, x);
05283 }
05284
05285 static void disable_dtmf_detect(struct dahdi_pvt *p)
05286 {
05287 int val;
05288
05289 p->ignoredtmf = 1;
05290
05291 val = 0;
05292 ioctl(p->subs[SUB_REAL].dfd, DAHDI_TONEDETECT, &val);
05293
05294 if (!p->hardwaredtmf && p->dsp) {
05295 p->dsp_features &= ~DSP_FEATURE_DIGIT_DETECT;
05296 ast_dsp_set_features(p->dsp, p->dsp_features);
05297 }
05298 }
05299
05300 static void enable_dtmf_detect(struct dahdi_pvt *p)
05301 {
05302 int val;
05303
05304 if (p->channel == CHAN_PSEUDO)
05305 return;
05306
05307 p->ignoredtmf = 0;
05308
05309 val = DAHDI_TONEDETECT_ON | DAHDI_TONEDETECT_MUTE;
05310 ioctl(p->subs[SUB_REAL].dfd, DAHDI_TONEDETECT, &val);
05311
05312 if (!p->hardwaredtmf && p->dsp) {
05313 p->dsp_features |= DSP_FEATURE_DIGIT_DETECT;
05314 ast_dsp_set_features(p->dsp, p->dsp_features);
05315 }
05316 }
05317
05318 static enum ast_bridge_result dahdi_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
05319 {
05320 struct ast_channel *who;
05321 struct dahdi_pvt *p0, *p1, *op0, *op1;
05322 struct dahdi_pvt *master = NULL, *slave = NULL;
05323 struct ast_frame *f;
05324 int inconf = 0;
05325 int nothingok = 1;
05326 int ofd0, ofd1;
05327 int oi0, oi1, i0 = -1, i1 = -1, t0, t1;
05328 int os0 = -1, os1 = -1;
05329 int priority = 0;
05330 struct ast_channel *oc0, *oc1;
05331 enum ast_bridge_result res;
05332
05333 #ifdef PRI_2BCT
05334 int triedtopribridge = 0;
05335 #endif
05336
05337
05338
05339
05340
05341
05342 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
05343 return AST_BRIDGE_FAILED_NOWARN;
05344
05345 ast_channel_lock(c0);
05346 while (ast_channel_trylock(c1)) {
05347 CHANNEL_DEADLOCK_AVOIDANCE(c0);
05348 }
05349
05350 p0 = c0->tech_pvt;
05351 p1 = c1->tech_pvt;
05352
05353 if (!p0 || (!p0->sig) || !p1 || (!p1->sig)) {
05354 ast_channel_unlock(c0);
05355 ast_channel_unlock(c1);
05356 return AST_BRIDGE_FAILED_NOWARN;
05357 }
05358
05359 oi0 = dahdi_get_index(c0, p0, 0);
05360 oi1 = dahdi_get_index(c1, p1, 0);
05361 if ((oi0 < 0) || (oi1 < 0)) {
05362 ast_channel_unlock(c0);
05363 ast_channel_unlock(c1);
05364 return AST_BRIDGE_FAILED;
05365 }
05366
05367 op0 = p0 = c0->tech_pvt;
05368 op1 = p1 = c1->tech_pvt;
05369 ofd0 = c0->fds[0];
05370 ofd1 = c1->fds[0];
05371 oc0 = p0->owner;
05372 oc1 = p1->owner;
05373
05374 if (ast_mutex_trylock(&p0->lock)) {
05375
05376 ast_channel_unlock(c0);
05377 ast_channel_unlock(c1);
05378 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
05379 return AST_BRIDGE_RETRY;
05380 }
05381 if (ast_mutex_trylock(&p1->lock)) {
05382
05383 ast_mutex_unlock(&p0->lock);
05384 ast_channel_unlock(c0);
05385 ast_channel_unlock(c1);
05386 ast_log(LOG_NOTICE, "Avoiding deadlock...\n");
05387 return AST_BRIDGE_RETRY;
05388 }
05389
05390 if ((p0->callwaiting && p0->callwaitingcallerid)
05391 || (p1->callwaiting && p1->callwaitingcallerid)) {
05392
05393
05394
05395
05396
05397
05398
05399
05400
05401 ast_mutex_unlock(&p0->lock);
05402 ast_mutex_unlock(&p1->lock);
05403 ast_channel_unlock(c0);
05404 ast_channel_unlock(c1);
05405 return AST_BRIDGE_FAILED_NOWARN;
05406 }
05407
05408 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
05409 if (p0->owner && p1->owner) {
05410
05411 if (!p0->subs[SUB_CALLWAIT].inthreeway && !p1->subs[SUB_REAL].inthreeway) {
05412 master = p0;
05413 slave = p1;
05414 inconf = 1;
05415 } else if (!p1->subs[SUB_CALLWAIT].inthreeway && !p0->subs[SUB_REAL].inthreeway) {
05416 master = p1;
05417 slave = p0;
05418 inconf = 1;
05419 } else {
05420 ast_log(LOG_WARNING, "Huh? Both calls are callwaits or 3-ways? That's clever...?\n");
05421 ast_log(LOG_WARNING, "p0: chan %d/%d/CW%d/3W%d, p1: chan %d/%d/CW%d/3W%d\n",
05422 p0->channel,
05423 oi0, (p0->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
05424 p0->subs[SUB_REAL].inthreeway, p0->channel,
05425 oi0, (p1->subs[SUB_CALLWAIT].dfd > -1) ? 1 : 0,
05426 p1->subs[SUB_REAL].inthreeway);
05427 }
05428 nothingok = 0;
05429 }
05430 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_THREEWAY)) {
05431 if (p1->subs[SUB_THREEWAY].inthreeway) {
05432 master = p1;
05433 slave = p0;
05434 nothingok = 0;
05435 }
05436 } else if ((oi0 == SUB_THREEWAY) && (oi1 == SUB_REAL)) {
05437 if (p0->subs[SUB_THREEWAY].inthreeway) {
05438 master = p0;
05439 slave = p1;
05440 nothingok = 0;
05441 }
05442 } else if ((oi0 == SUB_REAL) && (oi1 == SUB_CALLWAIT)) {
05443
05444
05445 if (p1->subs[SUB_CALLWAIT].inthreeway) {
05446 master = p1;
05447 slave = p0;
05448 nothingok = 0;
05449 }
05450 } else if ((oi0 == SUB_CALLWAIT) && (oi1 == SUB_REAL)) {
05451
05452 if (p0->subs[SUB_CALLWAIT].inthreeway) {
05453 master = p0;
05454 slave = p1;
05455 nothingok = 0;
05456 }
05457 }
05458 ast_debug(1, "master: %d, slave: %d, nothingok: %d\n",
05459 master ? master->channel : 0, slave ? slave->channel : 0, nothingok);
05460 if (master && slave) {
05461
05462
05463
05464 if ((oi1 == SUB_THREEWAY) &&
05465 p1->subs[SUB_THREEWAY].inthreeway &&
05466 p1->subs[SUB_REAL].owner &&
05467 p1->subs[SUB_REAL].inthreeway &&
05468 (p1->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
05469 ast_debug(1,
05470 "Playing ringback on %d/%d(%s) since %d/%d(%s) is in a ringing three-way\n",
05471 p0->channel, oi0, c0->name, p1->channel, oi1, c1->name);
05472 tone_zone_play_tone(p0->subs[oi0].dfd, DAHDI_TONE_RINGTONE);
05473 os1 = p1->subs[SUB_REAL].owner->_state;
05474 } else {
05475 ast_debug(1, "Stopping tones on %d/%d(%s) talking to %d/%d(%s)\n",
05476 p0->channel, oi0, c0->name, p1->channel, oi1, c1->name);
05477 tone_zone_play_tone(p0->subs[oi0].dfd, -1);
05478 }
05479 if ((oi0 == SUB_THREEWAY) &&
05480 p0->subs[SUB_THREEWAY].inthreeway &&
05481 p0->subs[SUB_REAL].owner &&
05482 p0->subs[SUB_REAL].inthreeway &&
05483 (p0->subs[SUB_REAL].owner->_state == AST_STATE_RINGING)) {
05484 ast_debug(1,
05485 "Playing ringback on %d/%d(%s) since %d/%d(%s) is in a ringing three-way\n",
05486 p1->channel, oi1, c1->name, p0->channel, oi0, c0->name);
05487 tone_zone_play_tone(p1->subs[oi1].dfd, DAHDI_TONE_RINGTONE);
05488 os0 = p0->subs[SUB_REAL].owner->_state;
05489 } else {
05490 ast_debug(1, "Stopping tones on %d/%d(%s) talking to %d/%d(%s)\n",
05491 p1->channel, oi1, c1->name, p0->channel, oi0, c0->name);
05492 tone_zone_play_tone(p1->subs[oi1].dfd, -1);
05493 }
05494 if ((oi0 == SUB_REAL) && (oi1 == SUB_REAL)) {
05495 if (!p0->echocanbridged || !p1->echocanbridged) {
05496
05497 dahdi_disable_ec(p0);
05498 dahdi_disable_ec(p1);
05499 }
05500 }
05501 dahdi_link(slave, master);
05502 master->inconference = inconf;
05503 } else if (!nothingok)
05504 ast_log(LOG_WARNING, "Can't link %d/%s with %d/%s\n", p0->channel, subnames[oi0], p1->channel, subnames[oi1]);
05505
05506 update_conf(p0);
05507 update_conf(p1);
05508 t0 = p0->subs[SUB_REAL].inthreeway;
05509 t1 = p1->subs[SUB_REAL].inthreeway;
05510
05511 ast_mutex_unlock(&p0->lock);
05512 ast_mutex_unlock(&p1->lock);
05513
05514 ast_channel_unlock(c0);
05515 ast_channel_unlock(c1);
05516
05517
05518 if ((!master || !slave) && !nothingok) {
05519 dahdi_enable_ec(p0);
05520 dahdi_enable_ec(p1);
05521 return AST_BRIDGE_FAILED;
05522 }
05523
05524 ast_verb(3, "Native bridging %s and %s\n", c0->name, c1->name);
05525
05526 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
05527 disable_dtmf_detect(op0);
05528
05529 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
05530 disable_dtmf_detect(op1);
05531
05532 for (;;) {
05533 struct ast_channel *c0_priority[2] = {c0, c1};
05534 struct ast_channel *c1_priority[2] = {c1, c0};
05535
05536
05537
05538
05539 ast_channel_lock(c0);
05540 while (ast_channel_trylock(c1)) {
05541 CHANNEL_DEADLOCK_AVOIDANCE(c0);
05542 }
05543
05544 p0 = c0->tech_pvt;
05545 p1 = c1->tech_pvt;
05546
05547 if (op0 == p0)
05548 i0 = dahdi_get_index(c0, p0, 1);
05549 if (op1 == p1)
05550 i1 = dahdi_get_index(c1, p1, 1);
05551
05552 ast_channel_unlock(c0);
05553 ast_channel_unlock(c1);
05554
05555 if (!timeoutms ||
05556 (op0 != p0) ||
05557 (op1 != p1) ||
05558 (ofd0 != c0->fds[0]) ||
05559 (ofd1 != c1->fds[0]) ||
05560 (p0->subs[SUB_REAL].owner && (os0 > -1) && (os0 != p0->subs[SUB_REAL].owner->_state)) ||
05561 (p1->subs[SUB_REAL].owner && (os1 > -1) && (os1 != p1->subs[SUB_REAL].owner->_state)) ||
05562 (oc0 != p0->owner) ||
05563 (oc1 != p1->owner) ||
05564 (t0 != p0->subs[SUB_REAL].inthreeway) ||
05565 (t1 != p1->subs[SUB_REAL].inthreeway) ||
05566 (oi0 != i0) ||
05567 (oi1 != i1)) {
05568 ast_debug(1, "Something changed out on %d/%d to %d/%d, returning -3 to restart\n",
05569 op0->channel, oi0, op1->channel, oi1);
05570 res = AST_BRIDGE_RETRY;
05571 goto return_from_bridge;
05572 }
05573
05574 #ifdef PRI_2BCT
05575 if (!triedtopribridge) {
05576 triedtopribridge = 1;
05577 if (p0->pri && p0->pri == p1->pri && p0->transfer && p1->transfer) {
05578 ast_mutex_lock(&p0->pri->lock);
05579 if (p0->call && p1->call) {
05580 pri_channel_bridge(p0->call, p1->call);
05581 }
05582 ast_mutex_unlock(&p0->pri->lock);
05583 }
05584 }
05585 #endif
05586
05587 who = ast_waitfor_n(priority ? c0_priority : c1_priority, 2, &timeoutms);
05588 if (!who) {
05589 ast_debug(1, "Ooh, empty read...\n");
05590 continue;
05591 }
05592 f = ast_read(who);
05593 if (!f || (f->frametype == AST_FRAME_CONTROL)) {
05594 *fo = f;
05595 *rc = who;
05596 res = AST_BRIDGE_COMPLETE;
05597 goto return_from_bridge;
05598 }
05599 if (f->frametype == AST_FRAME_DTMF) {
05600 if ((who == c0) && p0->pulsedial) {
05601 ast_write(c1, f);
05602 } else if ((who == c1) && p1->pulsedial) {
05603 ast_write(c0, f);
05604 } else {
05605 *fo = f;
05606 *rc = who;
05607 res = AST_BRIDGE_COMPLETE;
05608 goto return_from_bridge;
05609 }
05610 }
05611 ast_frfree(f);
05612
05613
05614 priority = !priority;
05615 }
05616
05617 return_from_bridge:
05618 if (op0 == p0)
05619 dahdi_enable_ec(p0);
05620
05621 if (op1 == p1)
05622 dahdi_enable_ec(p1);
05623
05624 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_0) && (oi0 == SUB_REAL))
05625 enable_dtmf_detect(op0);
05626
05627 if (!(flags & AST_BRIDGE_DTMF_CHANNEL_1) && (oi1 == SUB_REAL))
05628 enable_dtmf_detect(op1);
05629
05630 dahdi_unlink(slave, master, 1);
05631
05632 return res;
05633 }
05634
05635 static int dahdi_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
05636 {
05637 struct dahdi_pvt *p = newchan->tech_pvt;
05638 int x;
05639 ast_mutex_lock(&p->lock);
05640 ast_debug(1, "New owner for channel %d is %s\n", p->channel, newchan->name);
05641 if (p->owner == oldchan) {
05642 p->owner = newchan;
05643 }
05644 for (x = 0; x < 3; x++)
05645 if (p->subs[x].owner == oldchan) {
05646 if (!x)
05647 dahdi_unlink(NULL, p, 0);
05648 p->subs[x].owner = newchan;
05649 }
05650 update_conf(p);
05651 ast_mutex_unlock(&p->lock);
05652 if (newchan->_state == AST_STATE_RINGING) {
05653 dahdi_indicate(newchan, AST_CONTROL_RINGING, NULL, 0);
05654 }
05655 return 0;
05656 }
05657
05658 static int dahdi_ring_phone(struct dahdi_pvt *p)
05659 {
05660 int x;
05661 int res;
05662
05663 x = 0;
05664 x = DAHDI_ONHOOK;
05665 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
05666 do {
05667 x = DAHDI_RING;
05668 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
05669 if (res) {
05670 switch (errno) {
05671 case EBUSY:
05672 case EINTR:
05673
05674 usleep(10000);
05675 continue;
05676 case EINPROGRESS:
05677 res = 0;
05678 break;
05679 default:
05680 ast_log(LOG_WARNING, "Couldn't ring the phone: %s\n", strerror(errno));
05681 res = 0;
05682 }
05683 }
05684 } while (res);
05685 return res;
05686 }
05687
05688 static void *ss_thread(void *data);
05689
05690
05691
05692
05693
05694
05695
05696
05697
05698
05699
05700
05701
05702
05703
05704 static int attempt_transfer(struct dahdi_pvt *p)
05705 {
05706
05707
05708
05709 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
05710
05711
05712 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner))
05713 ast_queue_control(p->subs[SUB_THREEWAY].owner, AST_CONTROL_UNHOLD);
05714 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RINGING) {
05715
05716
05717
05718
05719
05720
05721
05722 ast_indicate(ast_bridged_channel(p->subs[SUB_REAL].owner), AST_CONTROL_RINGING);
05723 }
05724 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RING) {
05725 tone_zone_play_tone(p->subs[SUB_THREEWAY].dfd, DAHDI_TONE_RINGTONE);
05726 }
05727 if (ast_channel_masquerade(p->subs[SUB_THREEWAY].owner, ast_bridged_channel(p->subs[SUB_REAL].owner))) {
05728 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
05729 ast_bridged_channel(p->subs[SUB_REAL].owner)->name, p->subs[SUB_THREEWAY].owner->name);
05730 return -1;
05731 }
05732
05733 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
05734 unalloc_sub(p, SUB_THREEWAY);
05735 } else if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
05736 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
05737 if (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_RINGING) {
05738
05739
05740
05741
05742
05743
05744
05745 ast_indicate(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), AST_CONTROL_RINGING);
05746 }
05747 if (p->subs[SUB_REAL].owner->_state == AST_STATE_RING) {
05748 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
05749 }
05750 if (ast_channel_masquerade(p->subs[SUB_REAL].owner, ast_bridged_channel(p->subs[SUB_THREEWAY].owner))) {
05751 ast_log(LOG_WARNING, "Unable to masquerade %s as %s\n",
05752 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)->name, p->subs[SUB_REAL].owner->name);
05753 return -1;
05754 }
05755
05756 swap_subs(p, SUB_THREEWAY, SUB_REAL);
05757 ast_channel_unlock(p->subs[SUB_REAL].owner);
05758 unalloc_sub(p, SUB_THREEWAY);
05759
05760 return 1;
05761 } else {
05762 ast_debug(1, "Neither %s nor %s are in a bridge, nothing to transfer\n",
05763 p->subs[SUB_REAL].owner->name, p->subs[SUB_THREEWAY].owner->name);
05764 return -1;
05765 }
05766 return 0;
05767 }
05768
05769 static int check_for_conference(struct dahdi_pvt *p)
05770 {
05771 struct dahdi_confinfo ci;
05772
05773 if (p->master || (p->confno > -1))
05774 return 0;
05775 memset(&ci, 0, sizeof(ci));
05776 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_GETCONF, &ci)) {
05777 ast_log(LOG_WARNING, "Failed to get conference info on channel %d: %s\n", p->channel, strerror(errno));
05778 return 0;
05779 }
05780
05781
05782
05783 if ((p->subs[SUB_REAL].curconf.confno != ci.confno) || (p->subs[SUB_REAL].curconf.confmode != ci.confmode)) {
05784 ast_verb(3, "Avoiding 3-way call when in an external conference\n");
05785 return 1;
05786 }
05787 return 0;
05788 }
05789
05790
05791
05792
05793
05794
05795 static int get_alarms(struct dahdi_pvt *p)
05796 {
05797 int res;
05798 struct dahdi_spaninfo zi;
05799 struct dahdi_params params;
05800
05801 memset(&zi, 0, sizeof(zi));
05802 zi.spanno = p->span;
05803
05804 if ((res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SPANSTAT, &zi)) >= 0) {
05805 if (zi.alarms != DAHDI_ALARM_NONE)
05806 return zi.alarms;
05807 } else {
05808 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d: %s\n", p->channel, strerror(errno));
05809 return 0;
05810 }
05811
05812
05813 memset(¶ms, 0, sizeof(params));
05814 if ((res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, ¶ms)) >= 0)
05815 return params.chan_alarms;
05816
05817 ast_log(LOG_WARNING, "Unable to determine alarm on channel %d\n", p->channel);
05818
05819 return DAHDI_ALARM_NONE;
05820 }
05821
05822 static void dahdi_handle_dtmf(struct ast_channel *ast, int idx, struct ast_frame **dest)
05823 {
05824 struct dahdi_pvt *p = ast->tech_pvt;
05825 struct ast_frame *f = *dest;
05826
05827 ast_debug(1, "%s DTMF digit: 0x%02X '%c' on %s\n",
05828 f->frametype == AST_FRAME_DTMF_BEGIN ? "Begin" : "End",
05829 f->subclass, f->subclass, ast->name);
05830
05831 if (p->confirmanswer) {
05832 if (f->frametype == AST_FRAME_DTMF_END) {
05833 ast_debug(1, "Confirm answer on %s!\n", ast->name);
05834
05835
05836 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
05837 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
05838
05839 p->confirmanswer = 0;
05840 } else {
05841 p->subs[idx].f.frametype = AST_FRAME_NULL;
05842 p->subs[idx].f.subclass = 0;
05843 }
05844 *dest = &p->subs[idx].f;
05845 } else if (p->callwaitcas) {
05846 if (f->frametype == AST_FRAME_DTMF_END) {
05847 if ((f->subclass == 'A') || (f->subclass == 'D')) {
05848 ast_debug(1, "Got some DTMF, but it's for the CAS\n");
05849 ast_free(p->cidspill);
05850 p->cidspill = NULL;
05851 send_cwcidspill(p);
05852 }
05853 p->callwaitcas = 0;
05854 }
05855 p->subs[idx].f.frametype = AST_FRAME_NULL;
05856 p->subs[idx].f.subclass = 0;
05857 *dest = &p->subs[idx].f;
05858 } else if (f->subclass == 'f') {
05859 if (f->frametype == AST_FRAME_DTMF_END) {
05860
05861 if ((p->callprogress & CALLPROGRESS_FAX) && !p->faxhandled) {
05862
05863 if (p->usefaxbuffers && !p->bufferoverrideinuse) {
05864 struct dahdi_bufferinfo bi = {
05865 .txbufpolicy = p->faxbuf_policy,
05866 .bufsize = p->bufsize,
05867 .numbufs = p->faxbuf_no
05868 };
05869 int res;
05870
05871 if ((res = ioctl(p->subs[idx].dfd, DAHDI_SET_BUFINFO, &bi)) < 0) {
05872 ast_log(LOG_WARNING, "Channel '%s' unable to set faxbuffer policy, reason: %s\n", ast->name, strerror(errno));
05873 } else {
05874 p->bufferoverrideinuse = 1;
05875 }
05876 }
05877 p->faxhandled = 1;
05878 if (strcmp(ast->exten, "fax")) {
05879 const char *target_context = S_OR(ast->macrocontext, ast->context);
05880
05881
05882
05883
05884
05885 ast_mutex_unlock(&p->lock);
05886 ast_channel_unlock(ast);
05887 if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
05888 ast_channel_lock(ast);
05889 ast_mutex_lock(&p->lock);
05890 ast_verb(3, "Redirecting %s to fax extension\n", ast->name);
05891
05892 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
05893 if (ast_async_goto(ast, target_context, "fax", 1))
05894 ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
05895 } else {
05896 ast_channel_lock(ast);
05897 ast_mutex_lock(&p->lock);
05898 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
05899 }
05900 } else {
05901 ast_debug(1, "Already in a fax extension, not redirecting\n");
05902 }
05903 } else {
05904 ast_debug(1, "Fax already handled\n");
05905 }
05906 dahdi_confmute(p, 0);
05907 }
05908 p->subs[idx].f.frametype = AST_FRAME_NULL;
05909 p->subs[idx].f.subclass = 0;
05910 *dest = &p->subs[idx].f;
05911 }
05912 }
05913
05914 static void handle_alarms(struct dahdi_pvt *p, int alms)
05915 {
05916 const char *alarm_str = alarm2str(alms);
05917
05918 ast_log(LOG_WARNING, "Detected alarm on channel %d: %s\n", p->channel, alarm_str);
05919 manager_event(EVENT_FLAG_SYSTEM, "Alarm",
05920 "Alarm: %s\r\n"
05921 "Channel: %d\r\n",
05922 alarm_str, p->channel);
05923 }
05924
05925 static struct ast_frame *dahdi_handle_event(struct ast_channel *ast)
05926 {
05927 int res, x;
05928 int idx, mysig;
05929 char *c;
05930 struct dahdi_pvt *p = ast->tech_pvt;
05931 pthread_t threadid;
05932 struct ast_channel *chan;
05933 struct ast_frame *f;
05934
05935 idx = dahdi_get_index(ast, p, 0);
05936 if (idx < 0) {
05937 return &ast_null_frame;
05938 }
05939 if (idx != SUB_REAL) {
05940 ast_log(LOG_ERROR, "We got an event on a non real sub. Fix it!\n");
05941 }
05942
05943 mysig = p->sig;
05944 if (p->outsigmod > -1)
05945 mysig = p->outsigmod;
05946
05947 p->subs[idx].f.frametype = AST_FRAME_NULL;
05948 p->subs[idx].f.subclass = 0;
05949 p->subs[idx].f.datalen = 0;
05950 p->subs[idx].f.samples = 0;
05951 p->subs[idx].f.mallocd = 0;
05952 p->subs[idx].f.offset = 0;
05953 p->subs[idx].f.src = "dahdi_handle_event";
05954 p->subs[idx].f.data.ptr = NULL;
05955 f = &p->subs[idx].f;
05956
05957 if (p->fake_event) {
05958 res = p->fake_event;
05959 p->fake_event = 0;
05960 } else
05961 res = dahdi_get_event(p->subs[idx].dfd);
05962
05963 ast_debug(1, "Got event %s(%d) on channel %d (index %d)\n", event2str(res), res, p->channel, idx);
05964
05965 if (res & (DAHDI_EVENT_PULSEDIGIT | DAHDI_EVENT_DTMFUP)) {
05966 p->pulsedial = (res & DAHDI_EVENT_PULSEDIGIT) ? 1 : 0;
05967 ast_debug(1, "Detected %sdigit '%c'\n", p->pulsedial ? "pulse ": "", res & 0xff);
05968 #if defined(HAVE_PRI)
05969 if (!p->proceeding && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) && p->pri && (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
05970
05971 } else
05972 #endif
05973 {
05974
05975 dahdi_confmute(p, 0);
05976 p->subs[idx].f.frametype = AST_FRAME_DTMF_END;
05977 p->subs[idx].f.subclass = res & 0xff;
05978 dahdi_handle_dtmf(ast, idx, &f);
05979 }
05980 return f;
05981 }
05982
05983 if (res & DAHDI_EVENT_DTMFDOWN) {
05984 ast_debug(1, "DTMF Down '%c'\n", res & 0xff);
05985 #if defined(HAVE_PRI)
05986 if (!p->proceeding && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) && p->pri && (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
05987
05988 } else
05989 #endif
05990 {
05991
05992 dahdi_confmute(p, 1);
05993 p->subs[idx].f.frametype = AST_FRAME_DTMF_BEGIN;
05994 p->subs[idx].f.subclass = res & 0xff;
05995 dahdi_handle_dtmf(ast, idx, &f);
05996 }
05997 return &p->subs[idx].f;
05998 }
05999
06000 switch (res) {
06001 case DAHDI_EVENT_EC_DISABLED:
06002 ast_verb(3, "Channel %d echo canceler disabled due to CED detection\n", p->channel);
06003 p->echocanon = 0;
06004 break;
06005 case DAHDI_EVENT_BITSCHANGED:
06006 #ifdef HAVE_OPENR2
06007 if (p->sig != SIG_MFCR2) {
06008 ast_log(LOG_WARNING, "Received bits changed on %s signalling?\n", sig2str(p->sig));
06009 } else {
06010 ast_log(LOG_DEBUG, "bits changed in chan %d\n", p->channel);
06011 openr2_chan_handle_cas(p->r2chan);
06012 }
06013 #else
06014 ast_log(LOG_WARNING, "Received bits changed on %s signalling?\n", sig2str(p->sig));
06015 #endif
06016 case DAHDI_EVENT_PULSE_START:
06017
06018 if (!ast->pbx)
06019 tone_zone_play_tone(p->subs[idx].dfd, -1);
06020 break;
06021 case DAHDI_EVENT_DIALCOMPLETE:
06022 #ifdef HAVE_OPENR2
06023 if ((p->sig & SIG_MFCR2) && p->r2chan && ast->_state != AST_STATE_UP) {
06024
06025
06026 break;
06027 }
06028 #endif
06029 if (p->inalarm) break;
06030 if ((p->radio || (p->oprmode < 0))) break;
06031 if (ioctl(p->subs[idx].dfd,DAHDI_DIALING,&x) == -1) {
06032 ast_log(LOG_DEBUG, "DAHDI_DIALING ioctl failed on %s: %s\n",ast->name, strerror(errno));
06033 return NULL;
06034 }
06035 if (!x) {
06036 dahdi_enable_ec(p);
06037 if (p->echobreak) {
06038 dahdi_train_ec(p);
06039 ast_copy_string(p->dop.dialstr, p->echorest, sizeof(p->dop.dialstr));
06040 p->dop.op = DAHDI_DIAL_OP_REPLACE;
06041 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
06042 p->echobreak = 0;
06043 } else {
06044 p->dialing = 0;
06045 if ((mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF)) {
06046
06047 if (ast->_state == AST_STATE_DIALING_OFFHOOK) {
06048 ast_setstate(ast, AST_STATE_UP);
06049 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06050 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
06051 break;
06052 } else {
06053
06054 ast_setstate(ast,AST_STATE_DIALING_OFFHOOK);
06055 }
06056 }
06057 if (ast->_state == AST_STATE_DIALING) {
06058 if ((p->callprogress & CALLPROGRESS_PROGRESS) && CANPROGRESSDETECT(p) && p->dsp && p->outgoing) {
06059 ast_debug(1, "Done dialing, but waiting for progress detection before doing more...\n");
06060 } else if (p->confirmanswer || (!p->dialednone
06061 && ((mysig == SIG_EM) || (mysig == SIG_EM_E1)
06062 || (mysig == SIG_EMWINK) || (mysig == SIG_FEATD)
06063 || (mysig == SIG_FEATDMF_TA) || (mysig == SIG_FEATDMF)
06064 || (mysig == SIG_E911) || (mysig == SIG_FGC_CAMA)
06065 || (mysig == SIG_FGC_CAMAMF) || (mysig == SIG_FEATB)
06066 || (mysig == SIG_SF) || (mysig == SIG_SFWINK)
06067 || (mysig == SIG_SF_FEATD) || (mysig == SIG_SF_FEATDMF)
06068 || (mysig == SIG_SF_FEATB)))) {
06069 ast_setstate(ast, AST_STATE_RINGING);
06070 } else if (!p->answeronpolarityswitch) {
06071 ast_setstate(ast, AST_STATE_UP);
06072 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06073 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
06074
06075 p->polarity = POLARITY_REV;
06076 } else {
06077
06078 p->polarity = POLARITY_IDLE;
06079 }
06080 }
06081 }
06082 }
06083 break;
06084 case DAHDI_EVENT_ALARM:
06085 #ifdef HAVE_PRI
06086 if ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) {
06087 if (!p->pri || !p->pri->pri || (pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0)) {
06088
06089 if (p->call) {
06090 if (p->pri && p->pri->pri) {
06091 if (!pri_grab(p, p->pri)) {
06092 pri_hangup(p->pri->pri, p->call, -1);
06093 pri_destroycall(p->pri->pri, p->call);
06094 p->call = NULL;
06095 pri_rel(p->pri);
06096 } else
06097 ast_log(LOG_WARNING, "Failed to grab PRI!\n");
06098 } else
06099 ast_log(LOG_WARNING, "The PRI Call has not been destroyed\n");
06100 }
06101 if (p->owner)
06102 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
06103 }
06104 }
06105 if (p->bearer)
06106 p->bearer->inalarm = 1;
06107 else
06108 #endif
06109 p->inalarm = 1;
06110 res = get_alarms(p);
06111 handle_alarms(p, res);
06112 #ifdef HAVE_PRI
06113 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
06114
06115 } else {
06116 break;
06117 }
06118 #endif
06119 #ifdef HAVE_SS7
06120 if (p->sig == SIG_SS7)
06121 break;
06122 #endif
06123 #ifdef HAVE_OPENR2
06124 if (p->sig == SIG_MFCR2)
06125 break;
06126 #endif
06127 case DAHDI_EVENT_ONHOOK:
06128 if (p->radio) {
06129 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06130 p->subs[idx].f.subclass = AST_CONTROL_RADIO_UNKEY;
06131 break;
06132 }
06133 if (p->oprmode < 0)
06134 {
06135 if (p->oprmode != -1) break;
06136 if ((p->sig == SIG_FXOLS) || (p->sig == SIG_FXOKS) || (p->sig == SIG_FXOGS))
06137 {
06138
06139 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
06140 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RING);
06141 save_conference(p->oprpeer);
06142 tone_zone_play_tone(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
06143 }
06144 break;
06145 }
06146 switch (p->sig) {
06147 case SIG_FXOLS:
06148 case SIG_FXOGS:
06149 case SIG_FXOKS:
06150 p->onhooktime = time(NULL);
06151 p->fxsoffhookstate = 0;
06152 p->msgstate = -1;
06153
06154 if (idx == SUB_REAL) {
06155
06156 if (p->subs[SUB_CALLWAIT].owner) {
06157
06158 dahdi_lock_sub_owner(p, SUB_CALLWAIT);
06159 if (!p->subs[SUB_CALLWAIT].owner) {
06160
06161
06162
06163
06164 dahdi_disable_ec(p);
06165 return NULL;
06166 }
06167
06168
06169 swap_subs(p, SUB_CALLWAIT, SUB_REAL);
06170 ast_verb(3, "Channel %d still has (callwait) call, ringing phone\n", p->channel);
06171 unalloc_sub(p, SUB_CALLWAIT);
06172 #if 0
06173 p->subs[idx].needanswer = 0;
06174 p->subs[idx].needringing = 0;
06175 #endif
06176 p->callwaitingrepeat = 0;
06177 p->cidcwexpire = 0;
06178 p->cid_suppress_expire = 0;
06179 p->owner = NULL;
06180
06181 if (p->subs[SUB_REAL].owner->_state != AST_STATE_UP)
06182 p->dialing = 1;
06183
06184 ast_channel_unlock(p->subs[SUB_REAL].owner);
06185 dahdi_ring_phone(p);
06186 } else if (p->subs[SUB_THREEWAY].owner) {
06187 unsigned int mssinceflash;
06188
06189
06190 dahdi_lock_sub_owner(p, SUB_THREEWAY);
06191 if (!p->subs[SUB_THREEWAY].owner) {
06192 ast_log(LOG_NOTICE, "Whoa, threeway disappeared kinda randomly.\n");
06193
06194 return NULL;
06195 }
06196 if (p->owner != ast) {
06197 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06198 ast_log(LOG_WARNING, "This isn't good...\n");
06199
06200 return NULL;
06201 }
06202
06203 mssinceflash = ast_tvdiff_ms(ast_tvnow(), p->flashtime);
06204 ast_debug(1, "Last flash was %d ms ago\n", mssinceflash);
06205 if (mssinceflash < MIN_MS_SINCE_FLASH) {
06206
06207
06208 ast_debug(1, "Looks like a bounced flash, hanging up both calls on %d\n", p->channel);
06209 ast_queue_hangup_with_cause(p->subs[SUB_THREEWAY].owner, AST_CAUSE_NO_ANSWER);
06210 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06211 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06212 } else if ((ast->pbx) || (ast->_state == AST_STATE_UP)) {
06213 if (p->transfer) {
06214
06215 p->subs[SUB_REAL].inthreeway = 0;
06216 p->subs[SUB_THREEWAY].inthreeway = 0;
06217
06218 if (!p->transfertobusy && ast->_state == AST_STATE_BUSY) {
06219
06220 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06221
06222 ast_channel_unlock(p->subs[SUB_REAL].owner);
06223 p->owner = NULL;
06224
06225 dahdi_ring_phone(p);
06226 } else {
06227 res = attempt_transfer(p);
06228 if (res < 0) {
06229
06230 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06231 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06232 } else if (res) {
06233
06234 break;
06235 }
06236 }
06237 } else {
06238 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06239 ast_channel_unlock(p->subs[SUB_THREEWAY].owner);
06240 }
06241 } else {
06242
06243 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06244
06245 ast_channel_unlock(p->subs[SUB_REAL].owner);
06246 p->owner = NULL;
06247
06248 dahdi_ring_phone(p);
06249 }
06250 }
06251 } else {
06252 ast_log(LOG_WARNING, "Got a hangup and my index is %d?\n", idx);
06253 }
06254
06255 default:
06256 dahdi_disable_ec(p);
06257 return NULL;
06258 }
06259 break;
06260 case DAHDI_EVENT_RINGOFFHOOK:
06261 if (p->inalarm) break;
06262 if (p->oprmode < 0)
06263 {
06264 if ((p->sig == SIG_FXOLS) || (p->sig == SIG_FXOKS) || (p->sig == SIG_FXOGS))
06265 {
06266
06267 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
06268 tone_zone_play_tone(p->oprpeer->subs[SUB_REAL].dfd, -1);
06269 restore_conference(p->oprpeer);
06270 }
06271 break;
06272 }
06273 if (p->radio)
06274 {
06275 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06276 p->subs[idx].f.subclass = AST_CONTROL_RADIO_KEY;
06277 break;
06278 }
06279
06280
06281 if (((mysig == SIG_E911) || (mysig == SIG_FGC_CAMA) || (mysig == SIG_FGC_CAMAMF)) && (ast->_state == AST_STATE_DIALING_OFFHOOK)) {
06282 c = strchr(p->dialdest, '/');
06283 if (c)
06284 c++;
06285 else
06286 c = p->dialdest;
06287 if (*c) snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*0%s#", c);
06288 else ast_copy_string(p->dop.dialstr,"M*2#", sizeof(p->dop.dialstr));
06289 if (strlen(p->dop.dialstr) > 4) {
06290 memset(p->echorest, 'w', sizeof(p->echorest) - 1);
06291 strcpy(p->echorest + (p->echotraining / 401) + 1, p->dop.dialstr + strlen(p->dop.dialstr) - 2);
06292 p->echorest[sizeof(p->echorest) - 1] = '\0';
06293 p->echobreak = 1;
06294 p->dop.dialstr[strlen(p->dop.dialstr)-2] = '\0';
06295 } else
06296 p->echobreak = 0;
06297 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop)) {
06298 int saveerr = errno;
06299
06300 x = DAHDI_ONHOOK;
06301 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
06302 ast_log(LOG_WARNING, "Dialing failed on channel %d: %s\n", p->channel, strerror(saveerr));
06303 return NULL;
06304 }
06305 p->dialing = 1;
06306 return &p->subs[idx].f;
06307 }
06308 switch (p->sig) {
06309 case SIG_FXOLS:
06310 case SIG_FXOGS:
06311 case SIG_FXOKS:
06312 p->fxsoffhookstate = 1;
06313 switch (ast->_state) {
06314 case AST_STATE_RINGING:
06315 dahdi_enable_ec(p);
06316 dahdi_train_ec(p);
06317 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06318 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
06319
06320 p->subs[SUB_REAL].needringing = 0;
06321 dahdi_set_hook(p->subs[idx].dfd, DAHDI_OFFHOOK);
06322 ast_debug(1, "channel %d answered\n", p->channel);
06323
06324
06325 ast_free(p->cidspill);
06326 p->cidspill = NULL;
06327 restore_conference(p);
06328
06329 p->dialing = 0;
06330 p->callwaitcas = 0;
06331 if (p->confirmanswer) {
06332
06333 p->subs[idx].f.frametype = AST_FRAME_NULL;
06334 p->subs[idx].f.subclass = 0;
06335 } else if (!ast_strlen_zero(p->dop.dialstr)) {
06336
06337 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
06338 if (res < 0) {
06339 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
06340 p->dop.dialstr[0] = '\0';
06341 return NULL;
06342 } else {
06343 ast_debug(1, "Sent FXO deferred digit string: %s\n", p->dop.dialstr);
06344 p->subs[idx].f.frametype = AST_FRAME_NULL;
06345 p->subs[idx].f.subclass = 0;
06346 p->dialing = 1;
06347 }
06348 p->dop.dialstr[0] = '\0';
06349 ast_setstate(ast, AST_STATE_DIALING);
06350 } else
06351 ast_setstate(ast, AST_STATE_UP);
06352 return &p->subs[idx].f;
06353 case AST_STATE_DOWN:
06354 ast_setstate(ast, AST_STATE_RING);
06355 ast->rings = 1;
06356 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06357 p->subs[idx].f.subclass = AST_CONTROL_OFFHOOK;
06358 ast_debug(1, "channel %d picked up\n", p->channel);
06359 return &p->subs[idx].f;
06360 case AST_STATE_UP:
06361
06362 dahdi_set_hook(p->subs[idx].dfd, DAHDI_OFFHOOK);
06363
06364 if (ast_bridged_channel(p->owner))
06365 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
06366 p->subs[idx].needunhold = 1;
06367 break;
06368 case AST_STATE_RESERVED:
06369
06370 if (has_voicemail(p))
06371 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_STUTTER);
06372 else
06373 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_DIALTONE);
06374 break;
06375 default:
06376 ast_log(LOG_WARNING, "FXO phone off hook in weird state %d??\n", ast->_state);
06377 }
06378 break;
06379 case SIG_FXSLS:
06380 case SIG_FXSGS:
06381 case SIG_FXSKS:
06382 if (ast->_state == AST_STATE_RING) {
06383 p->ringt = p->ringt_base;
06384 }
06385
06386
06387
06388 ast_debug(1, "Setting IDLE polarity due "
06389 "to ring. Old polarity was %d\n",
06390 p->polarity);
06391 p->polarity = POLARITY_IDLE;
06392
06393
06394 case SIG_EM:
06395 case SIG_EM_E1:
06396 case SIG_EMWINK:
06397 case SIG_FEATD:
06398 case SIG_FEATDMF:
06399 case SIG_FEATDMF_TA:
06400 case SIG_E911:
06401 case SIG_FGC_CAMA:
06402 case SIG_FGC_CAMAMF:
06403 case SIG_FEATB:
06404 case SIG_SF:
06405 case SIG_SFWINK:
06406 case SIG_SF_FEATD:
06407 case SIG_SF_FEATDMF:
06408 case SIG_SF_FEATB:
06409 if (ast->_state == AST_STATE_PRERING)
06410 ast_setstate(ast, AST_STATE_RING);
06411 if ((ast->_state == AST_STATE_DOWN) || (ast->_state == AST_STATE_RING)) {
06412 ast_debug(1, "Ring detected\n");
06413 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06414 p->subs[idx].f.subclass = AST_CONTROL_RING;
06415 } else if (p->outgoing && ((ast->_state == AST_STATE_RINGING) || (ast->_state == AST_STATE_DIALING))) {
06416 ast_debug(1, "Line answered\n");
06417 if (p->confirmanswer) {
06418 p->subs[idx].f.frametype = AST_FRAME_NULL;
06419 p->subs[idx].f.subclass = 0;
06420 } else {
06421 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06422 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
06423 ast_setstate(ast, AST_STATE_UP);
06424 }
06425 } else if (ast->_state != AST_STATE_RING)
06426 ast_log(LOG_WARNING, "Ring/Off-hook in strange state %d on channel %d\n", ast->_state, p->channel);
06427 break;
06428 default:
06429 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
06430 }
06431 break;
06432 case DAHDI_EVENT_RINGBEGIN:
06433 switch (p->sig) {
06434 case SIG_FXSLS:
06435 case SIG_FXSGS:
06436 case SIG_FXSKS:
06437 if (ast->_state == AST_STATE_RING) {
06438 p->ringt = p->ringt_base;
06439 }
06440 break;
06441 }
06442 break;
06443 case DAHDI_EVENT_RINGEROFF:
06444 if (p->inalarm) break;
06445 if ((p->radio || (p->oprmode < 0))) break;
06446 ast->rings++;
06447 if ((ast->rings > p->cidrings) && (p->cidspill)) {
06448 ast_log(LOG_WARNING, "Didn't finish Caller-ID spill. Cancelling.\n");
06449 ast_free(p->cidspill);
06450 p->cidspill = NULL;
06451 p->callwaitcas = 0;
06452 }
06453 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
06454 p->subs[idx].f.subclass = AST_CONTROL_RINGING;
06455 break;
06456 case DAHDI_EVENT_RINGERON:
06457 break;
06458 case DAHDI_EVENT_NOALARM:
06459 p->inalarm = 0;
06460 #ifdef HAVE_PRI
06461
06462 if (p->bearer)
06463 p->bearer->inalarm = 0;
06464 #endif
06465 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", p->channel);
06466 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
06467 "Channel: %d\r\n", p->channel);
06468 break;
06469 case DAHDI_EVENT_WINKFLASH:
06470 if (p->inalarm) break;
06471 if (p->radio) break;
06472 if (p->oprmode < 0) break;
06473 if (p->oprmode > 1)
06474 {
06475 struct dahdi_params par;
06476
06477 memset(&par, 0, sizeof(par));
06478 if (ioctl(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par) != -1)
06479 {
06480 if (!par.rxisoffhook)
06481 {
06482
06483 dahdi_set_hook(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_RINGOFF);
06484 dahdi_set_hook(p->oprpeer->subs[SUB_REAL].dfd, DAHDI_RING);
06485 save_conference(p);
06486 tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
06487 }
06488 }
06489 break;
06490 }
06491
06492 p->flashtime = ast_tvnow();
06493 switch (mysig) {
06494 case SIG_FXOLS:
06495 case SIG_FXOGS:
06496 case SIG_FXOKS:
06497 ast_debug(1, "Winkflash, index: %d, normal: %d, callwait: %d, thirdcall: %d\n",
06498 idx, p->subs[SUB_REAL].dfd, p->subs[SUB_CALLWAIT].dfd, p->subs[SUB_THREEWAY].dfd);
06499
06500
06501 ast_free(p->cidspill);
06502 p->cidspill = NULL;
06503 restore_conference(p);
06504 p->callwaitcas = 0;
06505
06506 if (idx != SUB_REAL) {
06507 ast_log(LOG_WARNING, "Got flash hook with index %d on channel %d?!?\n", idx, p->channel);
06508 goto winkflashdone;
06509 }
06510
06511 if (p->subs[SUB_CALLWAIT].owner) {
06512
06513 dahdi_lock_sub_owner(p, SUB_CALLWAIT);
06514 if (!p->subs[SUB_CALLWAIT].owner) {
06515
06516
06517
06518
06519 ast_log(LOG_NOTICE, "Whoa, the call-waiting call disappeared.\n");
06520 goto winkflashdone;
06521 }
06522
06523
06524 swap_subs(p, SUB_REAL, SUB_CALLWAIT);
06525 tone_zone_play_tone(p->subs[SUB_REAL].dfd, -1);
06526 p->owner = p->subs[SUB_REAL].owner;
06527 ast_debug(1, "Making %s the new owner\n", p->owner->name);
06528 if (p->owner->_state == AST_STATE_RINGING) {
06529 ast_setstate(p->owner, AST_STATE_UP);
06530 p->subs[SUB_REAL].needanswer = 1;
06531 }
06532 p->callwaitingrepeat = 0;
06533 p->cidcwexpire = 0;
06534 p->cid_suppress_expire = 0;
06535
06536
06537 if (!p->subs[SUB_CALLWAIT].inthreeway && ast_bridged_channel(p->subs[SUB_CALLWAIT].owner)) {
06538 ast_queue_control_data(p->subs[SUB_CALLWAIT].owner, AST_CONTROL_HOLD,
06539 S_OR(p->mohsuggest, NULL),
06540 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
06541 }
06542 p->subs[SUB_CALLWAIT].needhold = 1;
06543 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
06544 ast_queue_control_data(p->subs[SUB_REAL].owner, AST_CONTROL_HOLD,
06545 S_OR(p->mohsuggest, NULL),
06546 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
06547 }
06548 p->subs[SUB_REAL].needunhold = 1;
06549
06550
06551 ast_channel_unlock(p->subs[SUB_REAL].owner);
06552 } else if (!p->subs[SUB_THREEWAY].owner) {
06553 if (!p->threewaycalling) {
06554
06555 p->subs[SUB_REAL].needflash = 1;
06556 goto winkflashdone;
06557 } else if (!check_for_conference(p)) {
06558 char cid_num[256];
06559 char cid_name[256];
06560
06561 cid_num[0] = 0;
06562 cid_name[0] = 0;
06563 if (p->dahditrcallerid && p->owner) {
06564 if (p->owner->cid.cid_num)
06565 ast_copy_string(cid_num, p->owner->cid.cid_num, sizeof(cid_num));
06566 if (p->owner->cid.cid_name)
06567 ast_copy_string(cid_name, p->owner->cid.cid_name, sizeof(cid_name));
06568 }
06569
06570
06571 if (!((ast->pbx) ||
06572 (ast->_state == AST_STATE_UP) ||
06573 (ast->_state == AST_STATE_RING))) {
06574 ast_debug(1, "Flash when call not up or ringing\n");
06575 goto winkflashdone;
06576 }
06577 if (alloc_sub(p, SUB_THREEWAY)) {
06578 ast_log(LOG_WARNING, "Unable to allocate three-way subchannel\n");
06579 goto winkflashdone;
06580 }
06581
06582 chan = dahdi_new(p, AST_STATE_RESERVED, 0, SUB_THREEWAY, 0, 0);
06583 if (!chan) {
06584 ast_log(LOG_WARNING,
06585 "Cannot allocate new call structure on channel %d\n",
06586 p->channel);
06587 unalloc_sub(p, SUB_THREEWAY);
06588 goto winkflashdone;
06589 }
06590 if (p->dahditrcallerid) {
06591 if (!p->origcid_num)
06592 p->origcid_num = ast_strdup(p->cid_num);
06593 if (!p->origcid_name)
06594 p->origcid_name = ast_strdup(p->cid_name);
06595 ast_copy_string(p->cid_num, cid_num, sizeof(p->cid_num));
06596 ast_copy_string(p->cid_name, cid_name, sizeof(p->cid_name));
06597 }
06598
06599 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06600
06601 dahdi_disable_ec(p);
06602 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_DIALRECALL);
06603 if (res)
06604 ast_log(LOG_WARNING, "Unable to start dial recall tone on channel %d\n", p->channel);
06605 p->owner = chan;
06606 if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
06607 ast_log(LOG_WARNING, "Unable to start simple switch on channel %d\n", p->channel);
06608 res = tone_zone_play_tone(p->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
06609 dahdi_enable_ec(p);
06610 ast_hangup(chan);
06611 } else {
06612 ast_verb(3, "Started three way call on channel %d\n", p->channel);
06613
06614
06615 if (ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
06616 ast_queue_control_data(p->subs[SUB_THREEWAY].owner, AST_CONTROL_HOLD,
06617 S_OR(p->mohsuggest, NULL),
06618 !ast_strlen_zero(p->mohsuggest) ? strlen(p->mohsuggest) + 1 : 0);
06619 }
06620 p->subs[SUB_THREEWAY].needhold = 1;
06621 }
06622 }
06623 } else {
06624
06625 int orig_3way_sub;
06626
06627
06628 dahdi_lock_sub_owner(p, SUB_THREEWAY);
06629 if (!p->subs[SUB_THREEWAY].owner) {
06630
06631
06632
06633
06634 ast_log(LOG_NOTICE, "Whoa, the 3-way call disappeared.\n");
06635 goto winkflashdone;
06636 }
06637 orig_3way_sub = SUB_THREEWAY;
06638
06639 if (p->subs[SUB_THREEWAY].inthreeway) {
06640
06641 ast_debug(1, "Got flash with three way call up, dropping last call on %d\n", p->channel);
06642
06643 if ((p->subs[SUB_REAL].owner->_state != AST_STATE_UP) && (p->subs[SUB_THREEWAY].owner->_state == AST_STATE_UP)) {
06644
06645 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06646 orig_3way_sub = SUB_REAL;
06647 p->owner = p->subs[SUB_REAL].owner;
06648 }
06649
06650 ast_verb(3, "Dropping three-way call on %s\n", p->subs[SUB_THREEWAY].owner->name);
06651 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06652 p->subs[SUB_REAL].inthreeway = 0;
06653 p->subs[SUB_THREEWAY].inthreeway = 0;
06654 } else {
06655
06656 if (((ast->pbx) || (ast->_state == AST_STATE_UP)) &&
06657 (p->transfertobusy || (ast->_state != AST_STATE_BUSY))) {
06658 ast_verb(3, "Building conference call with %s and %s\n",
06659 p->subs[SUB_THREEWAY].owner->name,
06660 p->subs[SUB_REAL].owner->name);
06661
06662 p->subs[SUB_THREEWAY].inthreeway = 1;
06663 p->subs[SUB_REAL].inthreeway = 1;
06664 if (ast->_state == AST_STATE_UP) {
06665 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06666 orig_3way_sub = SUB_REAL;
06667 }
06668 if (ast_bridged_channel(p->subs[orig_3way_sub].owner)) {
06669 ast_queue_control(p->subs[orig_3way_sub].owner, AST_CONTROL_UNHOLD);
06670 }
06671 p->subs[orig_3way_sub].needunhold = 1;
06672 p->owner = p->subs[SUB_REAL].owner;
06673 } else {
06674 ast_verb(3, "Dumping incomplete call on %s\n", p->subs[SUB_THREEWAY].owner->name);
06675 swap_subs(p, SUB_THREEWAY, SUB_REAL);
06676 orig_3way_sub = SUB_REAL;
06677 p->subs[SUB_THREEWAY].owner->_softhangup |= AST_SOFTHANGUP_DEV;
06678 p->owner = p->subs[SUB_REAL].owner;
06679 if (ast_bridged_channel(p->subs[SUB_REAL].owner)) {
06680 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
06681 }
06682 p->subs[SUB_REAL].needunhold = 1;
06683 dahdi_enable_ec(p);
06684 }
06685 }
06686 ast_channel_unlock(p->subs[orig_3way_sub].owner);
06687 }
06688 winkflashdone:
06689 update_conf(p);
06690 break;
06691 case SIG_EM:
06692 case SIG_EM_E1:
06693 case SIG_FEATD:
06694 case SIG_SF:
06695 case SIG_SFWINK:
06696 case SIG_SF_FEATD:
06697 case SIG_FXSLS:
06698 case SIG_FXSGS:
06699 if (option_debug) {
06700 if (p->dialing)
06701 ast_debug(1, "Ignoring wink on channel %d\n", p->channel);
06702 else
06703 ast_debug(1, "Got wink in weird state %d on channel %d\n", ast->_state, p->channel);
06704 }
06705 break;
06706 case SIG_FEATDMF_TA:
06707 switch (p->whichwink) {
06708 case 0:
06709 ast_debug(1, "ANI2 set to '%d' and ANI is '%s'\n", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
06710 snprintf(p->dop.dialstr, sizeof(p->dop.dialstr), "M*%d%s#", p->owner->cid.cid_ani2, p->owner->cid.cid_ani);
06711 break;
06712 case 1:
06713 ast_copy_string(p->dop.dialstr, p->finaldial, sizeof(p->dop.dialstr));
06714 break;
06715 case 2:
06716 ast_log(LOG_WARNING, "Received unexpected wink on channel of type SIG_FEATDMF_TA\n");
06717 return NULL;
06718 }
06719 p->whichwink++;
06720
06721 case SIG_FEATDMF:
06722 case SIG_E911:
06723 case SIG_FGC_CAMAMF:
06724 case SIG_FGC_CAMA:
06725 case SIG_FEATB:
06726 case SIG_SF_FEATDMF:
06727 case SIG_SF_FEATB:
06728 case SIG_EMWINK:
06729
06730 if (!ast_strlen_zero(p->dop.dialstr)) {
06731 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
06732 if (res < 0) {
06733 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
06734 p->dop.dialstr[0] = '\0';
06735 return NULL;
06736 } else
06737 ast_debug(1, "Sent deferred digit string: %s\n", p->dop.dialstr);
06738 }
06739 p->dop.dialstr[0] = '\0';
06740 break;
06741 default:
06742 ast_log(LOG_WARNING, "Don't know how to handle ring/off hook for signalling %d\n", p->sig);
06743 }
06744 break;
06745 case DAHDI_EVENT_HOOKCOMPLETE:
06746 if (p->inalarm) break;
06747 if ((p->radio || (p->oprmode < 0))) break;
06748 if (p->waitingfordt.tv_sec) break;
06749 switch (mysig) {
06750 case SIG_FXSLS:
06751 case SIG_FXSGS:
06752 case SIG_FXSKS:
06753 case SIG_EM:
06754 case SIG_EM_E1:
06755 case SIG_EMWINK:
06756 case SIG_FEATD:
06757 case SIG_SF:
06758 case SIG_SFWINK:
06759 case SIG_SF_FEATD:
06760 if (!ast_strlen_zero(p->dop.dialstr)) {
06761 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
06762 if (res < 0) {
06763 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", p->channel, strerror(errno));
06764 p->dop.dialstr[0] = '\0';
06765 return NULL;
06766 } else
06767 ast_debug(1, "Sent deferred digit string: %s\n", p->dop.dialstr);
06768 }
06769 p->dop.dialstr[0] = '\0';
06770 p->dop.op = DAHDI_DIAL_OP_REPLACE;
06771 break;
06772 case SIG_FEATDMF:
06773 case SIG_FEATDMF_TA:
06774 case SIG_E911:
06775 case SIG_FGC_CAMA:
06776 case SIG_FGC_CAMAMF:
06777 case SIG_FEATB:
06778 case SIG_SF_FEATDMF:
06779 case SIG_SF_FEATB:
06780 ast_debug(1, "Got hook complete in MF FGD, waiting for wink now on channel %d\n",p->channel);
06781 break;
06782 default:
06783 break;
06784 }
06785 break;
06786 case DAHDI_EVENT_POLARITY:
06787
06788
06789
06790
06791
06792
06793
06794
06795
06796 if (p->polarityonanswerdelay > 0) {
06797
06798 if (ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
06799 switch (ast->_state) {
06800 case AST_STATE_DIALING:
06801 case AST_STATE_RINGING:
06802 if (p->answeronpolarityswitch) {
06803 ast_debug(1, "Answering on polarity switch! channel %d\n", p->channel);
06804 ast_setstate(p->owner, AST_STATE_UP);
06805 p->polarity = POLARITY_REV;
06806 if (p->hanguponpolarityswitch) {
06807 p->polaritydelaytv = ast_tvnow();
06808 }
06809 } else {
06810 ast_debug(1, "Ignore Answer on polarity switch, channel %d\n", p->channel);
06811 }
06812 break;
06813
06814 case AST_STATE_UP:
06815 case AST_STATE_RING:
06816 if (p->hanguponpolarityswitch) {
06817 ast_debug(1, "HangingUp on polarity switch! channel %d\n", p->channel);
06818 ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
06819 p->polarity = POLARITY_IDLE;
06820 } else {
06821 ast_debug(1, "Ignore Hangup on polarity switch, channel %d\n", p->channel);
06822 }
06823 break;
06824
06825 case AST_STATE_DOWN:
06826 case AST_STATE_RESERVED:
06827 case AST_STATE_OFFHOOK:
06828 case AST_STATE_BUSY:
06829 case AST_STATE_DIALING_OFFHOOK:
06830 case AST_STATE_PRERING:
06831 default:
06832 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
06833 ast_debug(1, "Ignoring Polarity switch on channel %d, state %d\n", p->channel, ast->_state);
06834 }
06835 }
06836
06837 } else {
06838
06839 switch (ast->_state) {
06840 case AST_STATE_DIALING:
06841 case AST_STATE_RINGING:
06842 if (p->answeronpolarityswitch) {
06843 ast_debug(1, "Polarity switch detected but NOT answering (too close to OffHook event) on channel %d, state %d\n", p->channel, ast->_state);
06844 }
06845 break;
06846
06847 case AST_STATE_UP:
06848 case AST_STATE_RING:
06849 if (p->hanguponpolarityswitch) {
06850 ast_debug(1, "Polarity switch detected but NOT hanging up (too close to Answer event) on channel %d, state %d\n", p->channel, ast->_state);
06851 }
06852 break;
06853
06854 default:
06855 if (p->answeronpolarityswitch || p->hanguponpolarityswitch) {
06856 ast_debug(1, "Polarity switch detected (too close to previous event) on channel %d, state %d\n", p->channel, ast->_state);
06857 }
06858 }
06859 }
06860 }
06861
06862
06863 ast_debug(1, "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) );
06864 break;
06865 default:
06866 ast_debug(1, "Dunno what to do with event %d on channel %d\n", res, p->channel);
06867 }
06868 return &p->subs[idx].f;
06869 }
06870
06871 static struct ast_frame *__dahdi_exception(struct ast_channel *ast)
06872 {
06873 struct dahdi_pvt *p = ast->tech_pvt;
06874 int res;
06875 int idx;
06876 struct ast_frame *f;
06877
06878
06879 idx = dahdi_get_index(ast, p, 1);
06880 if (idx < 0) {
06881 idx = SUB_REAL;
06882 }
06883
06884 p->subs[idx].f.frametype = AST_FRAME_NULL;
06885 p->subs[idx].f.datalen = 0;
06886 p->subs[idx].f.samples = 0;
06887 p->subs[idx].f.mallocd = 0;
06888 p->subs[idx].f.offset = 0;
06889 p->subs[idx].f.subclass = 0;
06890 p->subs[idx].f.delivery = ast_tv(0,0);
06891 p->subs[idx].f.src = "dahdi_exception";
06892 p->subs[idx].f.data.ptr = NULL;
06893
06894 if ((!p->owner) && (!(p->radio || (p->oprmode < 0)))) {
06895
06896
06897
06898
06899
06900 if (p->fake_event) {
06901 res = p->fake_event;
06902 p->fake_event = 0;
06903 } else
06904 res = dahdi_get_event(p->subs[SUB_REAL].dfd);
06905
06906 if ((res != DAHDI_EVENT_RINGEROFF) && (res != DAHDI_EVENT_RINGERON) &&
06907 (res != DAHDI_EVENT_HOOKCOMPLETE)) {
06908 ast_debug(1, "Restoring owner of channel %d on event %d\n", p->channel, res);
06909 p->owner = p->subs[SUB_REAL].owner;
06910 if (p->owner && ast != p->owner) {
06911
06912
06913
06914
06915 ast_log(LOG_WARNING, "Event %s on %s is not restored owner %s\n",
06916 event2str(res), ast->name, p->owner->name);
06917 }
06918 if (p->owner && ast_bridged_channel(p->owner))
06919 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
06920 p->subs[SUB_REAL].needunhold = 1;
06921 }
06922 switch (res) {
06923 case DAHDI_EVENT_ONHOOK:
06924 dahdi_disable_ec(p);
06925 if (p->owner) {
06926 ast_verb(3, "Channel %s still has call, ringing phone\n", p->owner->name);
06927 dahdi_ring_phone(p);
06928 p->callwaitingrepeat = 0;
06929 p->cidcwexpire = 0;
06930 p->cid_suppress_expire = 0;
06931 } else {
06932 ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
06933 event2str(res));
06934 }
06935 update_conf(p);
06936 break;
06937 case DAHDI_EVENT_RINGOFFHOOK:
06938 dahdi_enable_ec(p);
06939 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
06940 if (p->owner && (p->owner->_state == AST_STATE_RINGING)) {
06941 p->subs[SUB_REAL].needanswer = 1;
06942 p->dialing = 0;
06943 }
06944 break;
06945 case DAHDI_EVENT_HOOKCOMPLETE:
06946 case DAHDI_EVENT_RINGERON:
06947 case DAHDI_EVENT_RINGEROFF:
06948
06949 break;
06950 case DAHDI_EVENT_WINKFLASH:
06951 p->flashtime = ast_tvnow();
06952 if (p->owner) {
06953 ast_verb(3, "Channel %d flashed to other channel %s\n", p->channel, p->owner->name);
06954 if (p->owner->_state != AST_STATE_UP) {
06955
06956 p->subs[SUB_REAL].needanswer = 1;
06957 ast_setstate(p->owner, AST_STATE_UP);
06958 }
06959 p->callwaitingrepeat = 0;
06960 p->cidcwexpire = 0;
06961 p->cid_suppress_expire = 0;
06962 if (ast_bridged_channel(p->owner))
06963 ast_queue_control(p->owner, AST_CONTROL_UNHOLD);
06964 p->subs[SUB_REAL].needunhold = 1;
06965 } else {
06966 ast_log(LOG_WARNING, "Absorbed %s, but nobody is left!?!?\n",
06967 event2str(res));
06968 }
06969 update_conf(p);
06970 break;
06971 default:
06972 ast_log(LOG_WARNING, "Don't know how to absorb event %s\n", event2str(res));
06973 break;
06974 }
06975 f = &p->subs[idx].f;
06976 return f;
06977 }
06978 if (!(p->radio || (p->oprmode < 0)))
06979 ast_debug(1, "Exception on %d, channel %d\n", ast->fds[0],p->channel);
06980
06981 if (ast != p->owner) {
06982 ast_log(LOG_WARNING, "We're %s, not %s\n", ast->name, p->owner->name);
06983 f = &p->subs[idx].f;
06984 return f;
06985 }
06986 f = dahdi_handle_event(ast);
06987 return f;
06988 }
06989
06990 static struct ast_frame *dahdi_exception(struct ast_channel *ast)
06991 {
06992 struct dahdi_pvt *p = ast->tech_pvt;
06993 struct ast_frame *f;
06994 ast_mutex_lock(&p->lock);
06995 f = __dahdi_exception(ast);
06996 ast_mutex_unlock(&p->lock);
06997 return f;
06998 }
06999
07000 static struct ast_frame *dahdi_read(struct ast_channel *ast)
07001 {
07002 struct dahdi_pvt *p = ast->tech_pvt;
07003 int res;
07004 int idx;
07005 void *readbuf;
07006 struct ast_frame *f;
07007
07008 while (ast_mutex_trylock(&p->lock)) {
07009 CHANNEL_DEADLOCK_AVOIDANCE(ast);
07010 }
07011
07012 idx = dahdi_get_index(ast, p, 0);
07013
07014
07015 if (idx < 0) {
07016 ast_log(LOG_WARNING, "We don't exist?\n");
07017 ast_mutex_unlock(&p->lock);
07018 return NULL;
07019 }
07020
07021 if ((p->radio || (p->oprmode < 0)) && p->inalarm) {
07022 ast_mutex_unlock(&p->lock);
07023 return NULL;
07024 }
07025
07026 p->subs[idx].f.frametype = AST_FRAME_NULL;
07027 p->subs[idx].f.datalen = 0;
07028 p->subs[idx].f.samples = 0;
07029 p->subs[idx].f.mallocd = 0;
07030 p->subs[idx].f.offset = 0;
07031 p->subs[idx].f.subclass = 0;
07032 p->subs[idx].f.delivery = ast_tv(0,0);
07033 p->subs[idx].f.src = "dahdi_read";
07034 p->subs[idx].f.data.ptr = NULL;
07035
07036
07037 if ((p->radio || (p->oprmode < 0)) && (!p->firstradio))
07038 {
07039 struct dahdi_params ps;
07040
07041 memset(&ps, 0, sizeof(ps));
07042 ps.channo = p->channel;
07043 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps) < 0) {
07044 ast_mutex_unlock(&p->lock);
07045 return NULL;
07046 }
07047 p->firstradio = 1;
07048 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
07049 if (ps.rxisoffhook)
07050 {
07051 p->subs[idx].f.subclass = AST_CONTROL_RADIO_KEY;
07052 }
07053 else
07054 {
07055 p->subs[idx].f.subclass = AST_CONTROL_RADIO_UNKEY;
07056 }
07057 ast_mutex_unlock(&p->lock);
07058 return &p->subs[idx].f;
07059 }
07060 if (p->ringt == 1) {
07061 ast_mutex_unlock(&p->lock);
07062 return NULL;
07063 }
07064 else if (p->ringt > 0)
07065 p->ringt--;
07066
07067 #ifdef HAVE_OPENR2
07068 if (p->mfcr2) {
07069 openr2_chan_process_event(p->r2chan);
07070 if (OR2_DIR_FORWARD == openr2_chan_get_direction(p->r2chan)) {
07071 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
07072
07073
07074 if (p->mfcr2_call_accepted &&
07075 !p->mfcr2_progress &&
07076 ast->_state == AST_STATE_RINGING) {
07077 ast_log(LOG_DEBUG, "Enqueuing progress frame after R2 accept in chan %d\n", p->channel);
07078 ast_queue_frame(p->owner, &f);
07079 p->mfcr2_progress = 1;
07080 }
07081 }
07082 }
07083 #endif
07084
07085 if (p->subs[idx].needringing) {
07086
07087 p->subs[idx].needringing = 0;
07088 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
07089 p->subs[idx].f.subclass = AST_CONTROL_RINGING;
07090 ast_setstate(ast, AST_STATE_RINGING);
07091 ast_mutex_unlock(&p->lock);
07092 return &p->subs[idx].f;
07093 }
07094
07095 if (p->subs[idx].needbusy) {
07096
07097 p->subs[idx].needbusy = 0;
07098 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
07099 p->subs[idx].f.subclass = AST_CONTROL_BUSY;
07100 ast_mutex_unlock(&p->lock);
07101 return &p->subs[idx].f;
07102 }
07103
07104 if (p->subs[idx].needcongestion) {
07105
07106 p->subs[idx].needcongestion = 0;
07107 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
07108 p->subs[idx].f.subclass = AST_CONTROL_CONGESTION;
07109 ast_mutex_unlock(&p->lock);
07110 return &p->subs[idx].f;
07111 }
07112
07113 if (p->subs[idx].needcallerid && !ast->cid.cid_tns) {
07114 ast_set_callerid(ast, S_OR(p->lastcid_num, NULL),
07115 S_OR(p->lastcid_name, NULL),
07116 S_OR(p->lastcid_num, NULL)
07117 );
07118 p->subs[idx].needcallerid = 0;
07119 }
07120
07121 if (p->subs[idx].needanswer) {
07122
07123 p->subs[idx].needanswer = 0;
07124 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
07125 p->subs[idx].f.subclass = AST_CONTROL_ANSWER;
07126 ast_mutex_unlock(&p->lock);
07127 return &p->subs[idx].f;
07128 }
07129 #ifdef HAVE_OPENR2
07130 if (p->mfcr2 && openr2_chan_get_read_enabled(p->r2chan)) {
07131
07132
07133
07134
07135 ast_mutex_unlock(&p->lock);
07136 return &ast_null_frame;
07137 }
07138 #endif
07139
07140 if (p->subs[idx].needflash) {
07141
07142 p->subs[idx].needflash = 0;
07143 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
07144 p->subs[idx].f.subclass = AST_CONTROL_FLASH;
07145 ast_mutex_unlock(&p->lock);
07146 return &p->subs[idx].f;
07147 }
07148
07149 if (p->subs[idx].needhold) {
07150
07151 p->subs[idx].needhold = 0;
07152 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
07153 p->subs[idx].f.subclass = AST_CONTROL_HOLD;
07154 ast_mutex_unlock(&p->lock);
07155 ast_debug(1, "Sending hold on '%s'\n", ast->name);
07156 return &p->subs[idx].f;
07157 }
07158
07159 if (p->subs[idx].needunhold) {
07160
07161 p->subs[idx].needunhold = 0;
07162 p->subs[idx].f.frametype = AST_FRAME_CONTROL;
07163 p->subs[idx].f.subclass = AST_CONTROL_UNHOLD;
07164 ast_mutex_unlock(&p->lock);
07165 ast_debug(1, "Sending unhold on '%s'\n", ast->name);
07166 return &p->subs[idx].f;
07167 }
07168
07169 if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
07170 if (!p->subs[idx].linear) {
07171 p->subs[idx].linear = 1;
07172 res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
07173 if (res)
07174 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to linear mode.\n", p->channel, idx);
07175 }
07176 } else if ((ast->rawreadformat == AST_FORMAT_ULAW) ||
07177 (ast->rawreadformat == AST_FORMAT_ALAW)) {
07178 if (p->subs[idx].linear) {
07179 p->subs[idx].linear = 0;
07180 res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
07181 if (res)
07182 ast_log(LOG_WARNING, "Unable to set channel %d (index %d) to companded mode.\n", p->channel, idx);
07183 }
07184 } else {
07185 ast_log(LOG_WARNING, "Don't know how to read frames in format %s\n", ast_getformatname(ast->rawreadformat));
07186 ast_mutex_unlock(&p->lock);
07187 return NULL;
07188 }
07189 readbuf = ((unsigned char *)p->subs[idx].buffer) + AST_FRIENDLY_OFFSET;
07190 CHECK_BLOCKING(ast);
07191 res = read(p->subs[idx].dfd, readbuf, p->subs[idx].linear ? READ_SIZE * 2 : READ_SIZE);
07192 ast_clear_flag(ast, AST_FLAG_BLOCKING);
07193
07194 if (res < 0) {
07195 f = NULL;
07196 if (res == -1) {
07197 if (errno == EAGAIN) {
07198
07199 ast_mutex_unlock(&p->lock);
07200 return &p->subs[idx].f;
07201 } else if (errno == ELAST) {
07202 f = __dahdi_exception(ast);
07203 } else
07204 ast_log(LOG_WARNING, "dahdi_rec: %s\n", strerror(errno));
07205 }
07206 ast_mutex_unlock(&p->lock);
07207 return f;
07208 }
07209 if (res != (p->subs[idx].linear ? READ_SIZE * 2 : READ_SIZE)) {
07210 ast_debug(1, "Short read (%d/%d), must be an event...\n", res, p->subs[idx].linear ? READ_SIZE * 2 : READ_SIZE);
07211 f = __dahdi_exception(ast);
07212 ast_mutex_unlock(&p->lock);
07213 return f;
07214 }
07215 if (p->tdd) {
07216 int c;
07217
07218 c = tdd_feed(p->tdd,readbuf,READ_SIZE);
07219 if (c < 0) {
07220 ast_debug(1,"tdd_feed failed\n");
07221 ast_mutex_unlock(&p->lock);
07222 return NULL;
07223 }
07224 if (c) {
07225 p->subs[idx].f.subclass = 0;
07226 p->subs[idx].f.frametype = AST_FRAME_TEXT;
07227 p->subs[idx].f.mallocd = 0;
07228 p->subs[idx].f.offset = AST_FRIENDLY_OFFSET;
07229 p->subs[idx].f.data.ptr = p->subs[idx].buffer + AST_FRIENDLY_OFFSET;
07230 p->subs[idx].f.datalen = 1;
07231 *((char *) p->subs[idx].f.data.ptr) = c;
07232 ast_mutex_unlock(&p->lock);
07233 return &p->subs[idx].f;
07234 }
07235 }
07236 if (idx == SUB_REAL) {
07237
07238 if (p->cidcwexpire) {
07239 if (!--p->cidcwexpire) {
07240
07241 ast_verb(3, "CPE does not support Call Waiting Caller*ID.\n");
07242 restore_conference(p);
07243 }
07244 }
07245 if (p->cid_suppress_expire) {
07246 --p->cid_suppress_expire;
07247 }
07248 if (p->callwaitingrepeat) {
07249 if (!--p->callwaitingrepeat) {
07250
07251 ++p->callwaitrings;
07252 dahdi_callwait(ast);
07253 }
07254 }
07255 }
07256 if (p->subs[idx].linear) {
07257 p->subs[idx].f.datalen = READ_SIZE * 2;
07258 } else
07259 p->subs[idx].f.datalen = READ_SIZE;
07260
07261
07262 if ((p->owner == ast) && p->cidspill &&((ast->_state == AST_STATE_UP) || (ast->rings == p->cidrings))) {
07263 send_callerid(p);
07264 }
07265
07266 p->subs[idx].f.frametype = AST_FRAME_VOICE;
07267 p->subs[idx].f.subclass = ast->rawreadformat;
07268 p->subs[idx].f.samples = READ_SIZE;
07269 p->subs[idx].f.mallocd = 0;
07270 p->subs[idx].f.offset = AST_FRIENDLY_OFFSET;
07271 p->subs[idx].f.data.ptr = p->subs[idx].buffer + AST_FRIENDLY_OFFSET / sizeof(p->subs[idx].buffer[0]);
07272 #if 0
07273 ast_debug(1, "Read %d of voice on %s\n", p->subs[idx].f.datalen, ast->name);
07274 #endif
07275 if (p->dialing ||
07276 (idx && (ast->_state != AST_STATE_UP)) ||
07277 ((idx == SUB_CALLWAIT) && !p->subs[SUB_CALLWAIT].inthreeway)
07278 ) {
07279
07280
07281 p->subs[idx].f.frametype = AST_FRAME_NULL;
07282 p->subs[idx].f.subclass = 0;
07283 p->subs[idx].f.samples = 0;
07284 p->subs[idx].f.mallocd = 0;
07285 p->subs[idx].f.offset = 0;
07286 p->subs[idx].f.data.ptr = NULL;
07287 p->subs[idx].f.datalen= 0;
07288 }
07289 if (p->dsp && (!p->ignoredtmf || p->callwaitcas || p->busydetect || p->callprogress || p->waitingfordt.tv_sec) && !idx) {
07290
07291 int mute;
07292
07293 f = ast_dsp_process(ast, p->dsp, &p->subs[idx].f);
07294
07295
07296 mute = ast_dsp_was_muted(p->dsp);
07297 if (p->muting != mute) {
07298 p->muting = mute;
07299 dahdi_confmute(p, mute);
07300 }
07301
07302 if (f) {
07303 if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_BUSY)) {
07304 if ((ast->_state == AST_STATE_UP) && !p->outgoing) {
07305
07306
07307 f = NULL;
07308 }
07309 } else if (f->frametype == AST_FRAME_DTMF_BEGIN
07310 || f->frametype == AST_FRAME_DTMF_END) {
07311 #ifdef HAVE_PRI
07312 if (!p->proceeding && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP)) && p->pri &&
07313 ((!p->outgoing && (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) ||
07314 (p->outgoing && (p->pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING)))) {
07315
07316 ast_debug(1, "Absorbing inband %s DTMF digit: 0x%02X '%c' on %s\n",
07317 f->frametype == AST_FRAME_DTMF_BEGIN ? "begin" : "end",
07318 f->subclass, f->subclass, ast->name);
07319
07320 f->frametype = AST_FRAME_NULL;
07321 f->subclass = 0;
07322 }
07323 #endif
07324
07325 p->pulsedial = 0;
07326 } else if (p->waitingfordt.tv_sec) {
07327 if (ast_tvdiff_ms(ast_tvnow(), p->waitingfordt) >= p->waitfordialtone ) {
07328 p->waitingfordt.tv_sec = 0;
07329 ast_log(LOG_WARNING, "Never saw dialtone on channel %d\n", p->channel);
07330 f=NULL;
07331 } else if (f->frametype == AST_FRAME_VOICE) {
07332 f->frametype = AST_FRAME_NULL;
07333 f->subclass = 0;
07334 if ((ast_dsp_get_tstate(p->dsp) == DSP_TONE_STATE_DIALTONE || ast_dsp_get_tstate(p->dsp) == DSP_TONE_STATE_RINGING) && ast_dsp_get_tcount(p->dsp) > 9) {
07335 p->waitingfordt.tv_sec = 0;
07336 p->dsp_features &= ~DSP_FEATURE_WAITDIALTONE;
07337 ast_dsp_set_features(p->dsp, p->dsp_features);
07338 ast_log(LOG_DEBUG, "Got 10 samples of dialtone!\n");
07339 if (!ast_strlen_zero(p->dop.dialstr)) {
07340 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_DIAL, &p->dop);
07341 if (res < 0) {
07342 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d\n", p->channel);
07343 p->dop.dialstr[0] = '\0';
07344 ast_mutex_unlock(&p->lock);
07345 return NULL;
07346 } else {
07347 ast_log(LOG_DEBUG, "Sent deferred digit string: %s\n", p->dop.dialstr);
07348 p->dialing = 1;
07349 p->dop.dialstr[0] = '\0';
07350 p->dop.op = DAHDI_DIAL_OP_REPLACE;
07351 ast_setstate(ast, AST_STATE_DIALING);
07352 }
07353 }
07354 }
07355 }
07356 }
07357 }
07358 } else
07359 f = &p->subs[idx].f;
07360
07361 if (f) {
07362 switch (f->frametype) {
07363 case AST_FRAME_DTMF_BEGIN:
07364 case AST_FRAME_DTMF_END:
07365 dahdi_handle_dtmf(ast, idx, &f);
07366 break;
07367 case AST_FRAME_VOICE:
07368 if (p->cidspill || p->cid_suppress_expire) {
07369
07370 p->subs[idx].f.frametype = AST_FRAME_NULL;
07371 p->subs[idx].f.subclass = 0;
07372 p->subs[idx].f.samples = 0;
07373 p->subs[idx].f.mallocd = 0;
07374 p->subs[idx].f.offset = 0;
07375 p->subs[idx].f.data.ptr = NULL;
07376 p->subs[idx].f.datalen= 0;
07377 }
07378 break;
07379 default:
07380 break;
07381 }
07382 }
07383
07384
07385 if (p->fake_event)
07386 ast_set_flag(ast, AST_FLAG_EXCEPTION);
07387
07388 ast_mutex_unlock(&p->lock);
07389 return f;
07390 }
07391
07392 static int my_dahdi_write(struct dahdi_pvt *p, unsigned char *buf, int len, int idx, int linear)
07393 {
07394 int sent=0;
07395 int size;
07396 int res;
07397 int fd;
07398 fd = p->subs[idx].dfd;
07399 while (len) {
07400 size = len;
07401 if (size > (linear ? READ_SIZE * 2 : READ_SIZE))
07402 size = (linear ? READ_SIZE * 2 : READ_SIZE);
07403 res = write(fd, buf, size);
07404 if (res != size) {
07405 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
07406 return sent;
07407 }
07408 len -= size;
07409 buf += size;
07410 }
07411 return sent;
07412 }
07413
07414 static int dahdi_write(struct ast_channel *ast, struct ast_frame *frame)
07415 {
07416 struct dahdi_pvt *p = ast->tech_pvt;
07417 int res;
07418 int idx;
07419 idx = dahdi_get_index(ast, p, 0);
07420 if (idx < 0) {
07421 ast_log(LOG_WARNING, "%s doesn't really exist?\n", ast->name);
07422 return -1;
07423 }
07424
07425 #if 0
07426 #ifdef HAVE_PRI
07427 ast_mutex_lock(&p->lock);
07428 if (!p->proceeding && p->sig==SIG_PRI && p->pri && !p->outgoing) {
07429 if (p->pri->pri) {
07430 if (!pri_grab(p, p->pri)) {
07431 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
07432 pri_rel(p->pri);
07433 } else
07434 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07435 }
07436 p->proceeding=1;
07437 }
07438 ast_mutex_unlock(&p->lock);
07439 #endif
07440 #endif
07441
07442 if (frame->frametype != AST_FRAME_VOICE) {
07443 if (frame->frametype != AST_FRAME_IMAGE)
07444 ast_log(LOG_WARNING, "Don't know what to do with frame type '%d'\n", frame->frametype);
07445 return 0;
07446 }
07447 if ((frame->subclass != AST_FORMAT_SLINEAR) &&
07448 (frame->subclass != AST_FORMAT_ULAW) &&
07449 (frame->subclass != AST_FORMAT_ALAW)) {
07450 ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
07451 return -1;
07452 }
07453 if (p->dialing) {
07454 ast_debug(1, "Dropping frame since I'm still dialing on %s...\n",ast->name);
07455 return 0;
07456 }
07457 if (!p->owner) {
07458 ast_debug(1, "Dropping frame since there is no active owner on %s...\n",ast->name);
07459 return 0;
07460 }
07461 if (p->cidspill) {
07462 ast_debug(1, "Dropping frame since I've still got a callerid spill on %s...\n",
07463 ast->name);
07464 return 0;
07465 }
07466
07467 if (!frame->data.ptr || !frame->datalen)
07468 return 0;
07469
07470 if (frame->subclass == AST_FORMAT_SLINEAR) {
07471 if (!p->subs[idx].linear) {
07472 p->subs[idx].linear = 1;
07473 res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
07474 if (res)
07475 ast_log(LOG_WARNING, "Unable to set linear mode on channel %d\n", p->channel);
07476 }
07477 res = my_dahdi_write(p, (unsigned char *)frame->data.ptr, frame->datalen, idx, 1);
07478 } else {
07479
07480 if (p->subs[idx].linear) {
07481 p->subs[idx].linear = 0;
07482 res = dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
07483 if (res)
07484 ast_log(LOG_WARNING, "Unable to set companded mode on channel %d\n", p->channel);
07485 }
07486 res = my_dahdi_write(p, (unsigned char *)frame->data.ptr, frame->datalen, idx, 0);
07487 }
07488 if (res < 0) {
07489 ast_log(LOG_WARNING, "write failed: %s\n", strerror(errno));
07490 return -1;
07491 }
07492 return 0;
07493 }
07494
07495 static int dahdi_indicate(struct ast_channel *chan, int condition, const void *data, size_t datalen)
07496 {
07497 struct dahdi_pvt *p = chan->tech_pvt;
07498 int res=-1;
07499 int idx;
07500 int func = DAHDI_FLASH;
07501 ast_mutex_lock(&p->lock);
07502 idx = dahdi_get_index(chan, p, 0);
07503 ast_debug(1, "Requested indication %d on channel %s\n", condition, chan->name);
07504 #ifdef HAVE_OPENR2
07505 if (p->mfcr2 && !p->mfcr2_call_accepted) {
07506 ast_mutex_unlock(&p->lock);
07507
07508
07509 return 0;
07510 }
07511 #endif
07512 if (idx == SUB_REAL) {
07513 switch (condition) {
07514 case AST_CONTROL_BUSY:
07515 #ifdef HAVE_PRI
07516 if (p->priindication_oob && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))) {
07517 chan->hangupcause = AST_CAUSE_USER_BUSY;
07518 chan->_softhangup |= AST_SOFTHANGUP_DEV;
07519 res = 0;
07520 } else if (!p->progress &&
07521 ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07522 && p->pri && !p->outgoing) {
07523 if (p->pri->pri) {
07524 if (!pri_grab(p, p->pri)) {
07525 #ifdef HAVE_PRI_PROG_W_CAUSE
07526 pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_USER_BUSY);
07527 #else
07528 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
07529 #endif
07530 pri_rel(p->pri);
07531 }
07532 else
07533 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07534 }
07535 p->progress = 1;
07536 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_BUSY);
07537 } else
07538 #endif
07539 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_BUSY);
07540 break;
07541 case AST_CONTROL_RINGING:
07542 #ifdef HAVE_PRI
07543 if ((!p->alerting) && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07544 && p->pri && !p->outgoing && (chan->_state != AST_STATE_UP)) {
07545 if (p->pri->pri) {
07546 if (!pri_grab(p, p->pri)) {
07547 pri_acknowledge(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
07548 pri_rel(p->pri);
07549 }
07550 else
07551 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07552 }
07553 p->alerting = 1;
07554 }
07555
07556 #endif
07557 #ifdef HAVE_SS7
07558 if ((!p->alerting) && (p->sig == SIG_SS7) && p->ss7 && !p->outgoing && (chan->_state != AST_STATE_UP)) {
07559 if (p->ss7->ss7) {
07560 ss7_grab(p, p->ss7);
07561
07562 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
07563 p->rlt = 1;
07564 if (p->rlt != 1)
07565 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_ALERTING);
07566 p->alerting = 1;
07567 ss7_rel(p->ss7);
07568 }
07569 }
07570 #endif
07571
07572 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_RINGTONE);
07573
07574 if (chan->_state != AST_STATE_UP) {
07575 if ((chan->_state != AST_STATE_RING) ||
07576 ((p->sig != SIG_FXSKS) &&
07577 (p->sig != SIG_FXSLS) &&
07578 (p->sig != SIG_FXSGS)))
07579 ast_setstate(chan, AST_STATE_RINGING);
07580 }
07581 break;
07582 case AST_CONTROL_PROCEEDING:
07583 ast_debug(1,"Received AST_CONTROL_PROCEEDING on %s\n",chan->name);
07584 #ifdef HAVE_PRI
07585 if (!p->proceeding && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07586 && p->pri && !p->outgoing) {
07587 if (p->pri->pri) {
07588 if (!pri_grab(p, p->pri)) {
07589 pri_proceeding(p->pri->pri,p->call, PVT_TO_CHANNEL(p), !p->digital);
07590 pri_rel(p->pri);
07591 }
07592 else
07593 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07594 }
07595 p->proceeding = 1;
07596 p->dialing = 0;
07597 }
07598 #endif
07599 #ifdef HAVE_SS7
07600
07601 if (chan->_state == AST_STATE_UP && (p->rlt != 1) && (p->sig == SIG_SS7)){
07602 if ((isup_far(p->ss7->ss7, p->ss7call)) != -1)
07603 p->rlt = 1;
07604 }
07605
07606 if (!p->proceeding && p->sig == SIG_SS7 && p->ss7 && !p->outgoing) {
07607 if (p->ss7->ss7) {
07608 ss7_grab(p, p->ss7);
07609 isup_acm(p->ss7->ss7, p->ss7call);
07610 p->proceeding = 1;
07611 ss7_rel(p->ss7);
07612
07613 }
07614 }
07615 #endif
07616
07617 res = 0;
07618 break;
07619 case AST_CONTROL_PROGRESS:
07620 ast_debug(1,"Received AST_CONTROL_PROGRESS on %s\n",chan->name);
07621 #ifdef HAVE_PRI
07622 p->digital = 0;
07623 if (!p->progress && !p->alerting && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07624 && p->pri && !p->outgoing) {
07625 if (p->pri->pri) {
07626 if (!pri_grab(p, p->pri)) {
07627 #ifdef HAVE_PRI_PROG_W_CAUSE
07628 pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, -1);
07629 #else
07630 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
07631 #endif
07632 pri_rel(p->pri);
07633 }
07634 else
07635 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07636 }
07637 p->progress = 1;
07638 }
07639 #endif
07640 #ifdef HAVE_SS7
07641 if (!p->progress && p->sig==SIG_SS7 && p->ss7 && !p->outgoing) {
07642 if (p->ss7->ss7) {
07643 ss7_grab(p, p->ss7);
07644 isup_cpg(p->ss7->ss7, p->ss7call, CPG_EVENT_INBANDINFO);
07645 p->progress = 1;
07646 ss7_rel(p->ss7);
07647
07648 dahdi_enable_ec(p);
07649
07650 }
07651 }
07652 #endif
07653
07654 res = 0;
07655 break;
07656 case AST_CONTROL_CONGESTION:
07657 chan->hangupcause = AST_CAUSE_CONGESTION;
07658 #ifdef HAVE_PRI
07659 if (p->priindication_oob && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))) {
07660 chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
07661 chan->_softhangup |= AST_SOFTHANGUP_DEV;
07662 res = 0;
07663 } else if (!p->progress && ((p->sig == SIG_PRI) || (p->sig == SIG_BRI) || (p->sig == SIG_BRI_PTMP))
07664 && p->pri && !p->outgoing) {
07665 if (p->pri) {
07666 if (!pri_grab(p, p->pri)) {
07667 #ifdef HAVE_PRI_PROG_W_CAUSE
07668 pri_progress_with_cause(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1, PRI_CAUSE_SWITCH_CONGESTION);
07669 #else
07670 pri_progress(p->pri->pri,p->call, PVT_TO_CHANNEL(p), 1);
07671 #endif
07672 pri_rel(p->pri);
07673 } else
07674 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07675 }
07676 p->progress = 1;
07677 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
07678 } else
07679 #endif
07680 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
07681 break;
07682 case AST_CONTROL_HOLD:
07683 #ifdef HAVE_PRI
07684 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
07685 if (!pri_grab(p, p->pri)) {
07686 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_HOLD);
07687 pri_rel(p->pri);
07688 } else
07689 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07690 } else
07691 #endif
07692 ast_moh_start(chan, data, p->mohinterpret);
07693 break;
07694 case AST_CONTROL_UNHOLD:
07695 #ifdef HAVE_PRI
07696 if (p->pri && !strcasecmp(p->mohinterpret, "passthrough")) {
07697 if (!pri_grab(p, p->pri)) {
07698 res = pri_notify(p->pri->pri, p->call, p->prioffset, PRI_NOTIFY_REMOTE_RETRIEVAL);
07699 pri_rel(p->pri);
07700 } else
07701 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
07702 } else
07703 #endif
07704 ast_moh_stop(chan);
07705 break;
07706 case AST_CONTROL_RADIO_KEY:
07707 if (p->radio)
07708 res = dahdi_set_hook(p->subs[idx].dfd, DAHDI_OFFHOOK);
07709 res = 0;
07710 break;
07711 case AST_CONTROL_RADIO_UNKEY:
07712 if (p->radio)
07713 res = dahdi_set_hook(p->subs[idx].dfd, DAHDI_RINGOFF);
07714 res = 0;
07715 break;
07716 case AST_CONTROL_FLASH:
07717
07718 if (ISTRUNK(p) && (p->sig != SIG_PRI)) {
07719
07720 p->dop.dialstr[0] = '\0';
07721 if ((ioctl(p->subs[SUB_REAL].dfd,DAHDI_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
07722 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
07723 chan->name, strerror(errno));
07724 } else
07725 res = 0;
07726 } else
07727 res = 0;
07728 break;
07729 case AST_CONTROL_SRCUPDATE:
07730 res = 0;
07731 break;
07732 case -1:
07733 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
07734 break;
07735 }
07736 } else
07737 res = 0;
07738 ast_mutex_unlock(&p->lock);
07739 return res;
07740 }
07741
07742 static struct ast_channel *dahdi_new(struct dahdi_pvt *i, int state, int startpbx, int idx, int law, int transfercapability)
07743 {
07744 struct ast_channel *tmp;
07745 int deflaw;
07746 int res;
07747 int x,y;
07748 int features;
07749 struct ast_str *chan_name;
07750 struct ast_variable *v;
07751 struct dahdi_params ps;
07752 if (i->subs[idx].owner) {
07753 ast_log(LOG_WARNING, "Channel %d already has a %s call\n", i->channel,subnames[idx]);
07754 return NULL;
07755 }
07756 y = 1;
07757 chan_name = ast_str_alloca(32);
07758 do {
07759 #ifdef HAVE_PRI
07760 if (i->bearer || (i->pri && (i->sig == SIG_FXSKS)))
07761 ast_str_set(&chan_name, 0, "%d:%d-%d", i->pri->trunkgroup, i->channel, y);
07762 else
07763 #endif
07764 if (i->channel == CHAN_PSEUDO)
07765 ast_str_set(&chan_name, 0, "pseudo-%ld", ast_random());
07766 else
07767 ast_str_set(&chan_name, 0, "%d-%d", i->channel, y);
07768 for (x = 0; x < 3; x++) {
07769 if ((idx != x) && i->subs[x].owner && !strcasecmp(ast_str_buffer(chan_name), i->subs[x].owner->name + 6))
07770 break;
07771 }
07772 y++;
07773 } while (x < 3);
07774 tmp = ast_channel_alloc(0, state, i->cid_num, i->cid_name, i->accountcode, i->exten, i->context, i->amaflags, "DAHDI/%s", ast_str_buffer(chan_name));
07775 if (!tmp)
07776 return NULL;
07777 tmp->tech = &dahdi_tech;
07778 memset(&ps, 0, sizeof(ps));
07779 ps.channo = i->channel;
07780 res = ioctl(i->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps);
07781 if (res) {
07782 ast_log(LOG_WARNING, "Unable to get parameters, assuming MULAW: %s\n", strerror(errno));
07783 ps.curlaw = DAHDI_LAW_MULAW;
07784 }
07785 if (ps.curlaw == DAHDI_LAW_ALAW)
07786 deflaw = AST_FORMAT_ALAW;
07787 else
07788 deflaw = AST_FORMAT_ULAW;
07789 if (law) {
07790 if (law == DAHDI_LAW_ALAW)
07791 deflaw = AST_FORMAT_ALAW;
07792 else
07793 deflaw = AST_FORMAT_ULAW;
07794 }
07795 ast_channel_set_fd(tmp, 0, i->subs[idx].dfd);
07796 tmp->nativeformats = deflaw;
07797
07798 tmp->rawreadformat = deflaw;
07799 tmp->readformat = deflaw;
07800 tmp->rawwriteformat = deflaw;
07801 tmp->writeformat = deflaw;
07802 i->subs[idx].linear = 0;
07803 dahdi_setlinear(i->subs[idx].dfd, i->subs[idx].linear);
07804 features = 0;
07805 if (idx == SUB_REAL) {
07806 if (i->busydetect && CANBUSYDETECT(i))
07807 features |= DSP_FEATURE_BUSY_DETECT;
07808 if ((i->callprogress & CALLPROGRESS_PROGRESS) && CANPROGRESSDETECT(i))
07809 features |= DSP_FEATURE_CALL_PROGRESS;
07810 if ((i->waitfordialtone) && CANPROGRESSDETECT(i))
07811 features |= DSP_FEATURE_WAITDIALTONE;
07812 if ((!i->outgoing && (i->callprogress & CALLPROGRESS_FAX_INCOMING)) ||
07813 (i->outgoing && (i->callprogress & CALLPROGRESS_FAX_OUTGOING))) {
07814 features |= DSP_FEATURE_FAX_DETECT;
07815 }
07816 x = DAHDI_TONEDETECT_ON | DAHDI_TONEDETECT_MUTE;
07817 if (ioctl(i->subs[idx].dfd, DAHDI_TONEDETECT, &x)) {
07818 i->hardwaredtmf = 0;
07819 features |= DSP_FEATURE_DIGIT_DETECT;
07820 } else if (NEED_MFDETECT(i)) {
07821 i->hardwaredtmf = 1;
07822 features |= DSP_FEATURE_DIGIT_DETECT;
07823 }
07824 }
07825 if (features) {
07826 if (i->dsp) {
07827 ast_debug(1, "Already have a dsp on %s?\n", tmp->name);
07828 } else {
07829 if (i->channel != CHAN_PSEUDO)
07830 i->dsp = ast_dsp_new();
07831 else
07832 i->dsp = NULL;
07833 if (i->dsp) {
07834 i->dsp_features = features;
07835 #if defined(HAVE_PRI) || defined(HAVE_SS7)
07836
07837 if (i->outgoing && ((i->sig == SIG_PRI) || (i->sig == SIG_BRI) || (i->sig == SIG_BRI_PTMP) || (i->sig == SIG_SS7))) {
07838
07839
07840 i->dsp_features = features & ~DSP_PROGRESS_TALK;
07841 features = 0;
07842 }
07843 #endif
07844 ast_dsp_set_features(i->dsp, features);
07845 ast_dsp_set_digitmode(i->dsp, DSP_DIGITMODE_DTMF | i->dtmfrelax);
07846 if (!ast_strlen_zero(progzone))
07847 ast_dsp_set_call_progress_zone(i->dsp, progzone);
07848 if (i->busydetect && CANBUSYDETECT(i)) {
07849 ast_dsp_set_busy_count(i->dsp, i->busycount);
07850 ast_dsp_set_busy_pattern(i->dsp, i->busy_tonelength, i->busy_quietlength);
07851 }
07852 }
07853 }
07854 }
07855
07856 if (state == AST_STATE_RING)
07857 tmp->rings = 1;
07858 tmp->tech_pvt = i;
07859 if ((i->sig == SIG_FXOKS) || (i->sig == SIG_FXOGS) || (i->sig == SIG_FXOLS)) {
07860
07861 tmp->callgroup = i->callgroup;
07862 tmp->pickupgroup = i->pickupgroup;
07863 }
07864 if (!ast_strlen_zero(i->parkinglot))
07865 ast_string_field_set(tmp, parkinglot, i->parkinglot);
07866 if (!ast_strlen_zero(i->language))
07867 ast_string_field_set(tmp, language, i->language);
07868 if (!i->owner)
07869 i->owner = tmp;
07870 if (!ast_strlen_zero(i->accountcode))
07871 ast_string_field_set(tmp, accountcode, i->accountcode);
07872 if (i->amaflags)
07873 tmp->amaflags = i->amaflags;
07874 i->subs[idx].owner = tmp;
07875 ast_copy_string(tmp->context, i->context, sizeof(tmp->context));
07876 ast_string_field_set(tmp, call_forward, i->call_forward);
07877
07878 if (!i->adsi)
07879 tmp->adsicpe = AST_ADSI_UNAVAILABLE;
07880 if (!ast_strlen_zero(i->exten))
07881 ast_copy_string(tmp->exten, i->exten, sizeof(tmp->exten));
07882 if (!ast_strlen_zero(i->rdnis))
07883 tmp->cid.cid_rdnis = ast_strdup(i->rdnis);
07884 if (!ast_strlen_zero(i->dnid))
07885 tmp->cid.cid_dnid = ast_strdup(i->dnid);
07886
07887
07888
07889 #ifdef PRI_ANI
07890 if (!ast_strlen_zero(i->cid_ani))
07891 tmp->cid.cid_ani = ast_strdup(i->cid_ani);
07892 else
07893 tmp->cid.cid_ani = ast_strdup(i->cid_num);
07894 #else
07895 tmp->cid.cid_ani = ast_strdup(i->cid_num);
07896 #endif
07897 tmp->cid.cid_pres = i->callingpres;
07898 tmp->cid.cid_ton = i->cid_ton;
07899 tmp->cid.cid_ani2 = i->cid_ani2;
07900 #if defined(HAVE_PRI) || defined(HAVE_SS7)
07901 tmp->transfercapability = transfercapability;
07902 pbx_builtin_setvar_helper(tmp, "TRANSFERCAPABILITY", ast_transfercapability2str(transfercapability));
07903 if (transfercapability & AST_TRANS_CAP_DIGITAL)
07904 i->digital = 1;
07905
07906 i->isidlecall = 0;
07907 i->alreadyhungup = 0;
07908 #endif
07909
07910 i->fake_event = 0;
07911
07912 dahdi_confmute(i, 0);
07913 i->muting = 0;
07914
07915 ast_jb_configure(tmp, &global_jbconf);
07916
07917 ast_devstate_changed_literal(ast_state_chan2dev(state), tmp->name);
07918
07919 for (v = i->vars ; v ; v = v->next)
07920 pbx_builtin_setvar_helper(tmp, v->name, v->value);
07921
07922 if (startpbx) {
07923 #ifdef HAVE_OPENR2
07924 if (i->mfcr2call) {
07925 pbx_builtin_setvar_helper(tmp, "MFCR2_CATEGORY", openr2_proto_get_category_string(i->mfcr2_recvd_category));
07926 }
07927 #endif
07928 if (ast_pbx_start(tmp)) {
07929 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
07930 ast_hangup(tmp);
07931 i->owner = NULL;
07932 return NULL;
07933 }
07934 }
07935
07936 ast_module_ref(ast_module_info->self);
07937 return tmp;
07938 }
07939
07940
07941 static int my_getsigstr(struct ast_channel *chan, char *str, const char *term, int ms)
07942 {
07943 char c;
07944
07945 *str = 0;
07946 for (;;)
07947 {
07948
07949 c = ast_waitfordigit(chan, ms);
07950
07951 if (c < 1)
07952 return c;
07953 *str++ = c;
07954 *str = 0;
07955 if (strchr(term, c))
07956 return 1;
07957 }
07958 }
07959
07960 static int dahdi_wink(struct dahdi_pvt *p, int idx)
07961 {
07962 int j;
07963 dahdi_set_hook(p->subs[idx].dfd, DAHDI_WINK);
07964 for (;;)
07965 {
07966
07967 j = DAHDI_IOMUX_SIGEVENT;
07968
07969 if (ioctl(p->subs[idx].dfd,DAHDI_IOMUX,&j) == -1) return(-1);
07970
07971 if (j & DAHDI_IOMUX_SIGEVENT) break;
07972 }
07973
07974 if (ioctl(p->subs[idx].dfd,DAHDI_GETEVENT,&j) == -1) return(-1);
07975 return 0;
07976 }
07977
07978
07979
07980
07981
07982
07983
07984
07985
07986
07987 static void dahdi_dnd(struct dahdi_pvt *dahdichan, int on)
07988 {
07989
07990 dahdichan->dnd = on;
07991 ast_verb(3, "%s DND on channel %d\n",
07992 on? "Enabled" : "Disabled",
07993 dahdichan->channel);
07994 manager_event(EVENT_FLAG_SYSTEM, "DNDState",
07995 "Channel: DAHDI/%d\r\n"
07996 "Status: %s\r\n", dahdichan->channel,
07997 on? "enabled" : "disabled");
07998 }
07999
08000 static void *ss_thread(void *data)
08001 {
08002 struct ast_channel *chan = data;
08003 struct dahdi_pvt *p = chan->tech_pvt;
08004 char exten[AST_MAX_EXTENSION] = "";
08005 char exten2[AST_MAX_EXTENSION] = "";
08006 unsigned char buf[256];
08007 char dtmfcid[300];
08008 char dtmfbuf[300];
08009 struct callerid_state *cs = NULL;
08010 char *name = NULL, *number = NULL;
08011 int distMatches;
08012 int curRingData[3];
08013 int receivedRingT;
08014 int counter1;
08015 int counter;
08016 int samples = 0;
08017 struct ast_smdi_md_message *smdi_msg = NULL;
08018 int flags = 0;
08019 int i;
08020 int timeout;
08021 int getforward = 0;
08022 char *s1, *s2;
08023 int len = 0;
08024 int res;
08025 int idx;
08026
08027 ast_mutex_lock(&ss_thread_lock);
08028 ss_thread_count++;
08029 ast_mutex_unlock(&ss_thread_lock);
08030
08031
08032
08033 if (!p) {
08034 ast_log(LOG_WARNING, "Channel became a zombie before simple switch could be started (%s)\n", chan->name);
08035 ast_hangup(chan);
08036 goto quit;
08037 }
08038 ast_verb(3, "Starting simple switch on '%s'\n", chan->name);
08039 idx = dahdi_get_index(chan, p, 0);
08040 if (idx < 0) {
08041 ast_hangup(chan);
08042 goto quit;
08043 }
08044 if (p->dsp)
08045 ast_dsp_digitreset(p->dsp);
08046 switch (p->sig) {
08047 #ifdef HAVE_PRI
08048 case SIG_PRI:
08049 case SIG_BRI:
08050 case SIG_BRI_PTMP:
08051
08052 ast_copy_string(exten, p->exten, sizeof(exten));
08053 len = strlen(exten);
08054 res = 0;
08055 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
08056 if (len && !ast_ignore_pattern(chan->context, exten))
08057 tone_zone_play_tone(p->subs[idx].dfd, -1);
08058 else
08059 tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALTONE);
08060 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num))
08061 timeout = matchdigittimeout;
08062 else
08063 timeout = gendigittimeout;
08064 res = ast_waitfordigit(chan, timeout);
08065 if (res < 0) {
08066 ast_debug(1, "waitfordigit returned < 0...\n");
08067 ast_hangup(chan);
08068 goto quit;
08069 } else if (res) {
08070 exten[len++] = res;
08071 exten[len] = '\0';
08072 } else
08073 break;
08074 }
08075
08076 if (ast_strlen_zero(exten)) {
08077 ast_verb(3, "Going to extension s|1 because of empty extension received on overlap call\n");
08078 exten[0] = 's';
08079 exten[1] = '\0';
08080 }
08081 tone_zone_play_tone(p->subs[idx].dfd, -1);
08082 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num)) {
08083
08084 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
08085 if (p->dsp) {
08086 ast_dsp_digitreset(p->dsp);
08087 }
08088 if (p->pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) {
08089 ast_mutex_lock(&p->lock);
08090 if (p->pri->pri) {
08091 if (!pri_grab(p, p->pri)) {
08092 pri_proceeding(p->pri->pri, p->call, PVT_TO_CHANNEL(p), 0);
08093 p->proceeding = 1;
08094 pri_rel(p->pri);
08095 } else {
08096 ast_log(LOG_WARNING, "Unable to grab PRI on span %d\n", p->span);
08097 }
08098 }
08099 ast_mutex_unlock(&p->lock);
08100 }
08101 dahdi_enable_ec(p);
08102 ast_setstate(chan, AST_STATE_RING);
08103 res = ast_pbx_run(chan);
08104 if (res) {
08105 ast_log(LOG_WARNING, "PBX exited non-zero!\n");
08106 }
08107 } else {
08108 ast_debug(1, "No such possible extension '%s' in context '%s'\n", exten, chan->context);
08109 chan->hangupcause = AST_CAUSE_UNALLOCATED;
08110 ast_hangup(chan);
08111 p->exten[0] = '\0';
08112
08113 p->call = NULL;
08114 }
08115 goto quit;
08116 break;
08117 #endif
08118 case SIG_FEATD:
08119 case SIG_FEATDMF:
08120 case SIG_FEATDMF_TA:
08121 case SIG_E911:
08122 case SIG_FGC_CAMAMF:
08123 case SIG_FEATB:
08124 case SIG_EMWINK:
08125 case SIG_SF_FEATD:
08126 case SIG_SF_FEATDMF:
08127 case SIG_SF_FEATB:
08128 case SIG_SFWINK:
08129 if (dahdi_wink(p, idx))
08130 goto quit;
08131
08132 case SIG_EM:
08133 case SIG_EM_E1:
08134 case SIG_SF:
08135 case SIG_FGC_CAMA:
08136 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
08137 if (p->dsp)
08138 ast_dsp_digitreset(p->dsp);
08139
08140 if (p->dsp) {
08141 if (NEED_MFDETECT(p))
08142 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MF | p->dtmfrelax);
08143 else
08144 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
08145 }
08146 memset(dtmfbuf, 0, sizeof(dtmfbuf));
08147
08148 if (!p->immediate)
08149
08150 res = ast_waitfordigit(chan, 5000);
08151 else
08152 res = 0;
08153 if (res > 0) {
08154
08155 dtmfbuf[0] = res;
08156 switch (p->sig) {
08157 case SIG_FEATD:
08158 case SIG_SF_FEATD:
08159 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
08160 if (res > 0)
08161 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
08162 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
08163 break;
08164 case SIG_FEATDMF_TA:
08165 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
08166 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
08167 if (dahdi_wink(p, idx)) goto quit;
08168 dtmfbuf[0] = 0;
08169
08170 res = ast_waitfordigit(chan, 5000);
08171 if (res <= 0) break;
08172 dtmfbuf[0] = res;
08173
08174 case SIG_FEATDMF:
08175 case SIG_E911:
08176 case SIG_FGC_CAMAMF:
08177 case SIG_SF_FEATDMF:
08178 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
08179
08180 if ((p->sig == SIG_FEATDMF) && (dtmfbuf[1] != '0') && (strlen(dtmfbuf) != 14))
08181 {
08182 if (dahdi_wink(p, idx)) goto quit;
08183 dtmfbuf[0] = 0;
08184
08185 res = ast_waitfordigit(chan, 5000);
08186 if (res <= 0) break;
08187 dtmfbuf[0] = res;
08188 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
08189 }
08190 if (res > 0) {
08191
08192 if (p->sig == SIG_E911)
08193 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
08194 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "#", 3000);
08195 }
08196 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
08197 break;
08198 case SIG_FEATB:
08199 case SIG_SF_FEATB:
08200 res = my_getsigstr(chan, dtmfbuf + 1, "#", 3000);
08201 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
08202 break;
08203 case SIG_EMWINK:
08204
08205
08206
08207
08208 if (res == '*') {
08209 res = my_getsigstr(chan, dtmfbuf + 1, "*", 3000);
08210 if (res > 0)
08211 res = my_getsigstr(chan, dtmfbuf + strlen(dtmfbuf), "*", 3000);
08212 if ((res < 1) && (p->dsp)) ast_dsp_digitreset(p->dsp);
08213 break;
08214 }
08215 default:
08216
08217 len = 1;
08218 dtmfbuf[len] = '\0';
08219 while ((len < AST_MAX_EXTENSION-1) && ast_matchmore_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
08220 if (ast_exists_extension(chan, chan->context, dtmfbuf, 1, p->cid_num)) {
08221 timeout = matchdigittimeout;
08222 } else {
08223 timeout = gendigittimeout;
08224 }
08225 res = ast_waitfordigit(chan, timeout);
08226 if (res < 0) {
08227 ast_debug(1, "waitfordigit returned < 0...\n");
08228 ast_hangup(chan);
08229 goto quit;
08230 } else if (res) {
08231 dtmfbuf[len++] = res;
08232 dtmfbuf[len] = '\0';
08233 } else {
08234 break;
08235 }
08236 }
08237 break;
08238 }
08239 }
08240 if (res == -1) {
08241 ast_log(LOG_WARNING, "getdtmf on channel %d: %s\n", p->channel, strerror(errno));
08242 ast_hangup(chan);
08243 goto quit;
08244 } else if (res < 0) {
08245 ast_debug(1, "Got hung up before digits finished\n");
08246 ast_hangup(chan);
08247 goto quit;
08248 }
08249
08250 if (p->sig == SIG_FGC_CAMA) {
08251 char anibuf[100];
08252
08253 if (ast_safe_sleep(chan,1000) == -1) {
08254 ast_hangup(chan);
08255 goto quit;
08256 }
08257 dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
08258 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_MF | p->dtmfrelax);
08259 res = my_getsigstr(chan, anibuf, "#", 10000);
08260 if ((res > 0) && (strlen(anibuf) > 2)) {
08261 if (anibuf[strlen(anibuf) - 1] == '#')
08262 anibuf[strlen(anibuf) - 1] = 0;
08263 ast_set_callerid(chan, anibuf + 2, NULL, anibuf + 2);
08264 }
08265 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
08266 }
08267
08268 ast_copy_string(exten, dtmfbuf, sizeof(exten));
08269 if (ast_strlen_zero(exten))
08270 ast_copy_string(exten, "s", sizeof(exten));
08271 if (p->sig == SIG_FEATD || p->sig == SIG_EMWINK) {
08272
08273 if (exten[0] == '*') {
08274 char *stringp=NULL;
08275 ast_copy_string(exten2, exten, sizeof(exten2));
08276
08277 stringp=exten2 +1;
08278 s1 = strsep(&stringp, "*");
08279 s2 = strsep(&stringp, "*");
08280 if (s2) {
08281 if (!ast_strlen_zero(p->cid_num))
08282 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
08283 else
08284 ast_set_callerid(chan, s1, NULL, s1);
08285 ast_copy_string(exten, s2, sizeof(exten));
08286 } else
08287 ast_copy_string(exten, s1, sizeof(exten));
08288 } else if (p->sig == SIG_FEATD)
08289 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
08290 }
08291 if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) {
08292 if (exten[0] == '*') {
08293 char *stringp=NULL;
08294 ast_copy_string(exten2, exten, sizeof(exten2));
08295
08296 stringp=exten2 +1;
08297 s1 = strsep(&stringp, "#");
08298 s2 = strsep(&stringp, "#");
08299 if (s2) {
08300 if (!ast_strlen_zero(p->cid_num))
08301 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
08302 else
08303 if (*(s1 + 2))
08304 ast_set_callerid(chan, s1 + 2, NULL, s1 + 2);
08305 ast_copy_string(exten, s2 + 1, sizeof(exten));
08306 } else
08307 ast_copy_string(exten, s1 + 2, sizeof(exten));
08308 } else
08309 ast_log(LOG_WARNING, "Got a non-Feature Group D input on channel %d. Assuming E&M Wink instead\n", p->channel);
08310 }
08311 if ((p->sig == SIG_E911) || (p->sig == SIG_FGC_CAMAMF)) {
08312 if (exten[0] == '*') {
08313 char *stringp=NULL;
08314 ast_copy_string(exten2, exten, sizeof(exten2));
08315
08316 stringp=exten2 +1;
08317 s1 = strsep(&stringp, "#");
08318 s2 = strsep(&stringp, "#");
08319 if (s2 && (*(s2 + 1) == '0')) {
08320 if (*(s2 + 2))
08321 ast_set_callerid(chan, s2 + 2, NULL, s2 + 2);
08322 }
08323 if (s1) ast_copy_string(exten, s1, sizeof(exten));
08324 else ast_copy_string(exten, "911", sizeof(exten));
08325 } else
08326 ast_log(LOG_WARNING, "Got a non-E911/FGC CAMA input on channel %d. Assuming E&M Wink instead\n", p->channel);
08327 }
08328 if (p->sig == SIG_FEATB) {
08329 if (exten[0] == '*') {
08330 char *stringp=NULL;
08331 ast_copy_string(exten2, exten, sizeof(exten2));
08332
08333 stringp=exten2 +1;
08334 s1 = strsep(&stringp, "#");
08335 ast_copy_string(exten, exten2 + 1, sizeof(exten));
08336 } else
08337 ast_log(LOG_WARNING, "Got a non-Feature Group B input on channel %d. Assuming E&M Wink instead\n", p->channel);
08338 }
08339 if ((p->sig == SIG_FEATDMF) || (p->sig == SIG_FEATDMF_TA)) {
08340 dahdi_wink(p, idx);
08341
08342
08343
08344 if (ast_safe_sleep(chan,100)) goto quit;
08345 }
08346 dahdi_enable_ec(p);
08347 if (NEED_MFDETECT(p)) {
08348 if (p->dsp) {
08349 if (!p->hardwaredtmf)
08350 ast_dsp_set_digitmode(p->dsp, DSP_DIGITMODE_DTMF | p->dtmfrelax);
08351 else {
08352 ast_dsp_free(p->dsp);
08353 p->dsp = NULL;
08354 }
08355 }
08356 }
08357
08358 if (ast_exists_extension(chan, chan->context, exten, 1, chan->cid.cid_num)) {
08359 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
08360 if (p->dsp) ast_dsp_digitreset(p->dsp);
08361 res = ast_pbx_run(chan);
08362 if (res) {
08363 ast_log(LOG_WARNING, "PBX exited non-zero\n");
08364 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08365 }
08366 goto quit;
08367 } else {
08368 ast_verb(2, "Unknown extension '%s' in context '%s' requested\n", exten, chan->context);
08369 sleep(2);
08370 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_INFO);
08371 if (res < 0)
08372 ast_log(LOG_WARNING, "Unable to start special tone on %d\n", p->channel);
08373 else
08374 sleep(1);
08375 res = ast_streamfile(chan, "ss-noservice", chan->language);
08376 if (res >= 0)
08377 ast_waitstream(chan, "");
08378 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08379 ast_hangup(chan);
08380 goto quit;
08381 }
08382 break;
08383 case SIG_FXOLS:
08384 case SIG_FXOGS:
08385 case SIG_FXOKS:
08386
08387 timeout = firstdigittimeout;
08388
08389
08390 if (p->subs[SUB_THREEWAY].owner)
08391 timeout = 999999;
08392 while (len < AST_MAX_EXTENSION-1) {
08393
08394
08395 if (p->immediate)
08396 res = 's';
08397 else
08398 res = ast_waitfordigit(chan, timeout);
08399 timeout = 0;
08400 if (res < 0) {
08401 ast_debug(1, "waitfordigit returned < 0...\n");
08402 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
08403 ast_hangup(chan);
08404 goto quit;
08405 } else if (res) {
08406 ast_debug(1,"waitfordigit returned '%c' (%d), timeout = %d\n", res, res, timeout);
08407 exten[len++]=res;
08408 exten[len] = '\0';
08409 }
08410 if (!ast_ignore_pattern(chan->context, exten))
08411 tone_zone_play_tone(p->subs[idx].dfd, -1);
08412 else
08413 tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALTONE);
08414 if (ast_exists_extension(chan, chan->context, exten, 1, p->cid_num) && strcmp(exten, ast_parking_ext())) {
08415 if (!res || !ast_matchmore_extension(chan, chan->context, exten, 1, p->cid_num)) {
08416 if (getforward) {
08417
08418 ast_copy_string(p->call_forward, exten, sizeof(p->call_forward));
08419 ast_verb(3, "Setting call forward to '%s' on channel %d\n", p->call_forward, p->channel);
08420 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08421 if (res)
08422 break;
08423 usleep(500000);
08424 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
08425 sleep(1);
08426 memset(exten, 0, sizeof(exten));
08427 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALTONE);
08428 len = 0;
08429 getforward = 0;
08430 } else {
08431 res = tone_zone_play_tone(p->subs[idx].dfd, -1);
08432 ast_copy_string(chan->exten, exten, sizeof(chan->exten));
08433 if (!ast_strlen_zero(p->cid_num)) {
08434 if (!p->hidecallerid)
08435 ast_set_callerid(chan, p->cid_num, NULL, p->cid_num);
08436 else
08437 ast_set_callerid(chan, NULL, NULL, p->cid_num);
08438 }
08439 if (!ast_strlen_zero(p->cid_name)) {
08440 if (!p->hidecallerid)
08441 ast_set_callerid(chan, NULL, p->cid_name, NULL);
08442 }
08443 ast_setstate(chan, AST_STATE_RING);
08444 dahdi_enable_ec(p);
08445 res = ast_pbx_run(chan);
08446 if (res) {
08447 ast_log(LOG_WARNING, "PBX exited non-zero\n");
08448 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08449 }
08450 goto quit;
08451 }
08452 } else {
08453
08454
08455 timeout = matchdigittimeout;
08456 }
08457 } else if (res == 0) {
08458 ast_debug(1, "not enough digits (and no ambiguous match)...\n");
08459 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08460 dahdi_wait_event(p->subs[idx].dfd);
08461 ast_hangup(chan);
08462 goto quit;
08463 } else if (p->callwaiting && !strcmp(exten, "*70")) {
08464 ast_verb(3, "Disabling call waiting on %s\n", chan->name);
08465
08466 p->callwaiting = 0;
08467 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08468 if (res) {
08469 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
08470 chan->name, strerror(errno));
08471 }
08472 len = 0;
08473 ioctl(p->subs[idx].dfd,DAHDI_CONFDIAG,&len);
08474 memset(exten, 0, sizeof(exten));
08475 timeout = firstdigittimeout;
08476
08477 } else if (!strcmp(exten,ast_pickup_ext())) {
08478
08479
08480
08481
08482 if (idx == SUB_REAL) {
08483
08484 if (p->subs[SUB_THREEWAY].owner) {
08485
08486
08487 alloc_sub(p, SUB_CALLWAIT);
08488 swap_subs(p, SUB_CALLWAIT, SUB_THREEWAY);
08489 unalloc_sub(p, SUB_THREEWAY);
08490 }
08491 dahdi_enable_ec(p);
08492 if (ast_pickup_call(chan)) {
08493 ast_debug(1, "No call pickup possible...\n");
08494 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08495 dahdi_wait_event(p->subs[idx].dfd);
08496 }
08497 ast_hangup(chan);
08498 goto quit;
08499 } else {
08500 ast_log(LOG_WARNING, "Huh? Got *8# on call not on real\n");
08501 ast_hangup(chan);
08502 goto quit;
08503 }
08504
08505 } else if (!p->hidecallerid && !strcmp(exten, "*67")) {
08506 ast_verb(3, "Disabling Caller*ID on %s\n", chan->name);
08507
08508 p->hidecallerid = 1;
08509 if (chan->cid.cid_num)
08510 ast_free(chan->cid.cid_num);
08511 chan->cid.cid_num = NULL;
08512 if (chan->cid.cid_name)
08513 ast_free(chan->cid.cid_name);
08514 chan->cid.cid_name = NULL;
08515 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08516 if (res) {
08517 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
08518 chan->name, strerror(errno));
08519 }
08520 len = 0;
08521 memset(exten, 0, sizeof(exten));
08522 timeout = firstdigittimeout;
08523 } else if (p->callreturn && !strcmp(exten, "*69")) {
08524 res = 0;
08525 if (!ast_strlen_zero(p->lastcid_num)) {
08526 res = ast_say_digit_str(chan, p->lastcid_num, "", chan->language);
08527 }
08528 if (!res)
08529 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08530 break;
08531 } else if (!strcmp(exten, "*78")) {
08532 dahdi_dnd(p, 1);
08533
08534 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08535 getforward = 0;
08536 memset(exten, 0, sizeof(exten));
08537 len = 0;
08538 } else if (!strcmp(exten, "*79")) {
08539 dahdi_dnd(p, 0);
08540
08541 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08542 getforward = 0;
08543 memset(exten, 0, sizeof(exten));
08544 len = 0;
08545 } else if (p->cancallforward && !strcmp(exten, "*72")) {
08546 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08547 getforward = 1;
08548 memset(exten, 0, sizeof(exten));
08549 len = 0;
08550 } else if (p->cancallforward && !strcmp(exten, "*73")) {
08551 ast_verb(3, "Cancelling call forwarding on channel %d\n", p->channel);
08552 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08553 memset(p->call_forward, 0, sizeof(p->call_forward));
08554 getforward = 0;
08555 memset(exten, 0, sizeof(exten));
08556 len = 0;
08557 } else if ((p->transfer || p->canpark) && !strcmp(exten, ast_parking_ext()) &&
08558 p->subs[SUB_THREEWAY].owner &&
08559 ast_bridged_channel(p->subs[SUB_THREEWAY].owner)) {
08560
08561
08562 ast_masq_park_call(ast_bridged_channel(p->subs[SUB_THREEWAY].owner), chan, 0, NULL);
08563 ast_verb(3, "Parking call to '%s'\n", chan->name);
08564 break;
08565 } else if (!ast_strlen_zero(p->lastcid_num) && !strcmp(exten, "*60")) {
08566 ast_verb(3, "Blacklisting number %s\n", p->lastcid_num);
08567 res = ast_db_put("blacklist", p->lastcid_num, "1");
08568 if (!res) {
08569 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08570 memset(exten, 0, sizeof(exten));
08571 len = 0;
08572 }
08573 } else if (p->hidecallerid && !strcmp(exten, "*82")) {
08574 ast_verb(3, "Enabling Caller*ID on %s\n", chan->name);
08575
08576 p->hidecallerid = 0;
08577 if (chan->cid.cid_num)
08578 ast_free(chan->cid.cid_num);
08579 chan->cid.cid_num = NULL;
08580 if (chan->cid.cid_name)
08581 ast_free(chan->cid.cid_name);
08582 chan->cid.cid_name = NULL;
08583 ast_set_callerid(chan, p->cid_num, p->cid_name, NULL);
08584 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_DIALRECALL);
08585 if (res) {
08586 ast_log(LOG_WARNING, "Unable to do dial recall on channel %s: %s\n",
08587 chan->name, strerror(errno));
08588 }
08589 len = 0;
08590 memset(exten, 0, sizeof(exten));
08591 timeout = firstdigittimeout;
08592 } else if (!strcmp(exten, "*0")) {
08593 struct ast_channel *nbridge =
08594 p->subs[SUB_THREEWAY].owner;
08595 struct dahdi_pvt *pbridge = NULL;
08596
08597 if (nbridge && ast_bridged_channel(nbridge))
08598 pbridge = ast_bridged_channel(nbridge)->tech_pvt;
08599 if (nbridge && pbridge &&
08600 (nbridge->tech == &dahdi_tech) &&
08601 (ast_bridged_channel(nbridge)->tech == &dahdi_tech) &&
08602 ISTRUNK(pbridge)) {
08603 int func = DAHDI_FLASH;
08604
08605 p->dop.dialstr[0] = '\0';
08606
08607 if ((ioctl(pbridge->subs[SUB_REAL].dfd,DAHDI_HOOK,&func) == -1) && (errno != EINPROGRESS)) {
08608 ast_log(LOG_WARNING, "Unable to flash external trunk on channel %s: %s\n",
08609 nbridge->name, strerror(errno));
08610 }
08611 swap_subs(p, SUB_REAL, SUB_THREEWAY);
08612 unalloc_sub(p, SUB_THREEWAY);
08613 p->owner = p->subs[SUB_REAL].owner;
08614 if (ast_bridged_channel(p->subs[SUB_REAL].owner))
08615 ast_queue_control(p->subs[SUB_REAL].owner, AST_CONTROL_UNHOLD);
08616 ast_hangup(chan);
08617 goto quit;
08618 } else {
08619 tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
08620 dahdi_wait_event(p->subs[idx].dfd);
08621 tone_zone_play_tone(p->subs[idx].dfd, -1);
08622 swap_subs(p, SUB_REAL, SUB_THREEWAY);
08623 unalloc_sub(p, SUB_THREEWAY);
08624 p->owner = p->subs[SUB_REAL].owner;
08625 ast_hangup(chan);
08626 goto quit;
08627 }
08628 } else if (!ast_canmatch_extension(chan, chan->context, exten, 1, chan->cid.cid_num) &&
08629 ((exten[0] != '*') || (strlen(exten) > 2))) {
08630 ast_debug(1, "Can't match %s from '%s' in context %s\n", exten, chan->cid.cid_num ? chan->cid.cid_num : "<Unknown Caller>", chan->context);
08631 break;
08632 }
08633 if (!timeout)
08634 timeout = gendigittimeout;
08635 if (len && !ast_ignore_pattern(chan->context, exten))
08636 tone_zone_play_tone(p->subs[idx].dfd, -1);
08637 }
08638 break;
08639 case SIG_FXSLS:
08640 case SIG_FXSGS:
08641 case SIG_FXSKS:
08642 #ifdef HAVE_PRI
08643 if (p->pri) {
08644
08645 struct ast_frame *f;
08646 int res;
08647 time_t start;
08648
08649 time(&start);
08650 ast_setstate(chan, AST_STATE_RING);
08651 while (time(NULL) < start + 3) {
08652 res = ast_waitfor(chan, 1000);
08653 if (res) {
08654 f = ast_read(chan);
08655 if (!f) {
08656 ast_log(LOG_WARNING, "Whoa, hangup while waiting for first ring!\n");
08657 ast_hangup(chan);
08658 goto quit;
08659 } else if ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_RING)) {
08660 res = 1;
08661 } else
08662 res = 0;
08663 ast_frfree(f);
08664 if (res) {
08665 ast_debug(1, "Got ring!\n");
08666 res = 0;
08667 break;
08668 }
08669 }
08670 }
08671 }
08672 #endif
08673
08674 if (p->use_smdi && p->smdi_iface) {
08675 smdi_msg = ast_smdi_md_message_wait(p->smdi_iface, SMDI_MD_WAIT_TIMEOUT);
08676
08677 if (smdi_msg != NULL) {
08678 ast_copy_string(chan->exten, smdi_msg->fwd_st, sizeof(chan->exten));
08679
08680 if (smdi_msg->type == 'B')
08681 pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "b");
08682 else if (smdi_msg->type == 'N')
08683 pbx_builtin_setvar_helper(chan, "_SMDI_VM_TYPE", "u");
08684
08685 ast_debug(1, "Received SMDI message on %s\n", chan->name);
08686 } else {
08687 ast_log(LOG_WARNING, "SMDI enabled but no SMDI message present\n");
08688 }
08689 }
08690
08691 if (p->use_callerid && (p->cid_signalling == CID_SIG_SMDI && smdi_msg)) {
08692 number = smdi_msg->calling_st;
08693
08694
08695
08696
08697 } else if (p->use_callerid && (chan->_state == AST_STATE_PRERING && (p->cid_start == CID_START_POLARITY || p->cid_start == CID_START_POLARITY_IN))) {
08698
08699 if (p->cid_signalling == CID_SIG_DTMF) {
08700 int k = 0;
08701 cs = NULL;
08702 ast_debug(1, "Receiving DTMF cid on channel %s\n", chan->name);
08703 dahdi_setlinear(p->subs[idx].dfd, 0);
08704
08705
08706
08707
08708
08709
08710 ast_set_flag(chan, AST_FLAG_END_DTMF_ONLY);
08711 res = 4000;
08712 for (;;) {
08713 struct ast_frame *f;
08714 res = ast_waitfor(chan, res);
08715 if (res <= 0) {
08716
08717
08718
08719
08720
08721 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
08722 "Exiting simple switch\n");
08723 ast_hangup(chan);
08724 goto quit;
08725 }
08726 f = ast_read(chan);
08727 if (!f)
08728 break;
08729 if (f->frametype == AST_FRAME_DTMF) {
08730 if (k < ARRAY_LEN(dtmfbuf) - 1) {
08731 dtmfbuf[k++] = f->subclass;
08732 }
08733 ast_debug(1, "CID got digit '%c'\n", f->subclass);
08734 res = 4000;
08735 }
08736 ast_frfree(f);
08737 if (chan->_state == AST_STATE_RING ||
08738 chan->_state == AST_STATE_RINGING)
08739 break;
08740 }
08741 ast_clear_flag(chan, AST_FLAG_END_DTMF_ONLY);
08742 dtmfbuf[k] = '\0';
08743 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
08744
08745 ast_debug(1, "CID got string '%s'\n", dtmfbuf);
08746 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
08747 ast_debug(1, "CID is '%s', flags %d\n", dtmfcid, flags);
08748
08749 if (!ast_strlen_zero(dtmfcid))
08750 number = dtmfcid;
08751 else
08752 number = NULL;
08753
08754 } else if ((p->cid_signalling == CID_SIG_V23) || (p->cid_signalling == CID_SIG_V23_JP)) {
08755 cs = callerid_new(p->cid_signalling);
08756 if (cs) {
08757 samples = 0;
08758 #if 1
08759 bump_gains(p);
08760 #endif
08761
08762 dahdi_setlinear(p->subs[idx].dfd, 0);
08763
08764
08765 for (;;) {
08766 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
08767 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
08768 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
08769 callerid_free(cs);
08770 ast_hangup(chan);
08771 goto quit;
08772 }
08773 if (i & DAHDI_IOMUX_SIGEVENT) {
08774 res = dahdi_get_event(p->subs[idx].dfd);
08775 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
08776 if (res == DAHDI_EVENT_NOALARM) {
08777 p->inalarm = 0;
08778 }
08779
08780 if (p->cid_signalling == CID_SIG_V23_JP) {
08781 if (res == DAHDI_EVENT_RINGBEGIN) {
08782 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
08783 usleep(1);
08784 }
08785 } else {
08786 res = 0;
08787 break;
08788 }
08789 } else if (i & DAHDI_IOMUX_READ) {
08790 res = read(p->subs[idx].dfd, buf, sizeof(buf));
08791 if (res < 0) {
08792 if (errno != ELAST) {
08793 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
08794 callerid_free(cs);
08795 ast_hangup(chan);
08796 goto quit;
08797 }
08798 break;
08799 }
08800 samples += res;
08801
08802 if (p->cid_signalling == CID_SIG_V23_JP) {
08803 res = callerid_feed_jp(cs, buf, res, AST_LAW(p));
08804 } else {
08805 res = callerid_feed(cs, buf, res, AST_LAW(p));
08806 }
08807 if (res < 0) {
08808
08809
08810
08811
08812 ast_log(LOG_WARNING,
08813 "Failed to decode CallerID on channel '%s'\n",
08814 chan->name);
08815 break;
08816 } else if (res)
08817 break;
08818 else if (samples > (8000 * 10))
08819 break;
08820 }
08821 }
08822 if (res == 1) {
08823 callerid_get(cs, &name, &number, &flags);
08824 ast_log(LOG_NOTICE, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
08825 }
08826
08827 if (p->cid_signalling == CID_SIG_V23_JP) {
08828 res = dahdi_set_hook(p->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
08829 usleep(1);
08830 }
08831
08832
08833 res = 4000;
08834 for (;;) {
08835 struct ast_frame *f;
08836 res = ast_waitfor(chan, res);
08837 if (res <= 0) {
08838 ast_log(LOG_WARNING, "CID timed out waiting for ring. "
08839 "Exiting simple switch\n");
08840 ast_hangup(chan);
08841 goto quit;
08842 }
08843 if (!(f = ast_read(chan))) {
08844 ast_log(LOG_WARNING, "Hangup received waiting for ring. Exiting simple switch\n");
08845 ast_hangup(chan);
08846 goto quit;
08847 }
08848 ast_frfree(f);
08849 if (chan->_state == AST_STATE_RING ||
08850 chan->_state == AST_STATE_RINGING)
08851 break;
08852 }
08853
08854
08855
08856 if (p->usedistinctiveringdetection) {
08857 len = 0;
08858 distMatches = 0;
08859
08860 for (receivedRingT = 0; receivedRingT < ARRAY_LEN(curRingData); receivedRingT++)
08861 curRingData[receivedRingT] = 0;
08862 receivedRingT = 0;
08863 counter = 0;
08864 counter1 = 0;
08865
08866 if (strcmp(p->context,p->defcontext) != 0) {
08867 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
08868 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
08869 }
08870
08871 for (;;) {
08872 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
08873 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
08874 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
08875 callerid_free(cs);
08876 ast_hangup(chan);
08877 goto quit;
08878 }
08879 if (i & DAHDI_IOMUX_SIGEVENT) {
08880 res = dahdi_get_event(p->subs[idx].dfd);
08881 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
08882 if (res == DAHDI_EVENT_NOALARM) {
08883 p->inalarm = 0;
08884 }
08885 res = 0;
08886
08887
08888 curRingData[receivedRingT] = p->ringt;
08889
08890 if (p->ringt < p->ringt_base/2)
08891 break;
08892
08893
08894 if (++receivedRingT == ARRAY_LEN(curRingData))
08895 break;
08896 } else if (i & DAHDI_IOMUX_READ) {
08897 res = read(p->subs[idx].dfd, buf, sizeof(buf));
08898 if (res < 0) {
08899 if (errno != ELAST) {
08900 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
08901 callerid_free(cs);
08902 ast_hangup(chan);
08903 goto quit;
08904 }
08905 break;
08906 }
08907 if (p->ringt)
08908 p->ringt--;
08909 if (p->ringt == 1) {
08910 res = -1;
08911 break;
08912 }
08913 }
08914 }
08915
08916 ast_verb(3, "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
08917 for (counter = 0; counter < 3; counter++) {
08918
08919
08920 distMatches = 0;
08921 for (counter1 = 0; counter1 < 3; counter1++) {
08922 ast_verb(3, "Ring pattern check range: %d\n", p->drings.ringnum[counter].range);
08923 if (p->drings.ringnum[counter].ring[counter1] == -1) {
08924 ast_verb(3, "Pattern ignore (-1) detected, so matching pattern %d regardless.\n",
08925 curRingData[counter1]);
08926 distMatches++;
08927 } else if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range) &&
08928 curRingData[counter1] >= (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range)) {
08929 ast_verb(3, "Ring pattern matched in range: %d to %d\n",
08930 (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range),
08931 (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range));
08932 distMatches++;
08933 }
08934 }
08935
08936 if (distMatches == 3) {
08937
08938 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
08939 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
08940 ast_verb(3, "Distinctive Ring matched context %s\n",p->context);
08941 break;
08942 }
08943 }
08944 }
08945
08946 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
08947 #if 1
08948 restore_gains(p);
08949 #endif
08950 } else
08951 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
08952 } else {
08953 ast_log(LOG_WARNING, "Channel %s in prering "
08954 "state, but I have nothing to do. "
08955 "Terminating simple switch, should be "
08956 "restarted by the actual ring.\n",
08957 chan->name);
08958 ast_hangup(chan);
08959 goto quit;
08960 }
08961 } else if (p->use_callerid && p->cid_start == CID_START_RING) {
08962 if (p->cid_signalling == CID_SIG_DTMF) {
08963 int k = 0;
08964 cs = NULL;
08965 dahdi_setlinear(p->subs[idx].dfd, 0);
08966 res = 2000;
08967 for (;;) {
08968 struct ast_frame *f;
08969 res = ast_waitfor(chan, res);
08970 if (res <= 0) {
08971 ast_log(LOG_WARNING, "DTMFCID timed out waiting for ring. "
08972 "Exiting simple switch\n");
08973 ast_hangup(chan);
08974 return NULL;
08975 }
08976 f = ast_read(chan);
08977 if (f->frametype == AST_FRAME_DTMF) {
08978 dtmfbuf[k++] = f->subclass;
08979 ast_log(LOG_DEBUG, "CID got digit '%c'\n", f->subclass);
08980 res = 2000;
08981 }
08982 ast_frfree(f);
08983
08984 if (p->ringt_base == p->ringt)
08985 break;
08986 }
08987 dtmfbuf[k] = '\0';
08988 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
08989
08990 callerid_get_dtmf(dtmfbuf, dtmfcid, &flags);
08991 ast_log(LOG_DEBUG, "CID is '%s', flags %d\n",
08992 dtmfcid, flags);
08993
08994 if (!ast_strlen_zero(dtmfcid))
08995 number = dtmfcid;
08996 else
08997 number = NULL;
08998
08999 } else {
09000
09001 cs = callerid_new(p->cid_signalling);
09002 if (cs) {
09003 #if 1
09004 bump_gains(p);
09005 #endif
09006 samples = 0;
09007 len = 0;
09008 distMatches = 0;
09009
09010 for (receivedRingT = 0; receivedRingT < ARRAY_LEN(curRingData); receivedRingT++)
09011 curRingData[receivedRingT] = 0;
09012 receivedRingT = 0;
09013 counter = 0;
09014 counter1 = 0;
09015
09016 if (strcmp(p->context,p->defcontext) != 0) {
09017 ast_copy_string(p->context, p->defcontext, sizeof(p->context));
09018 ast_copy_string(chan->context,p->defcontext,sizeof(chan->context));
09019 }
09020
09021
09022 dahdi_setlinear(p->subs[idx].dfd, 0);
09023 for (;;) {
09024 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
09025 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
09026 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
09027 callerid_free(cs);
09028 ast_hangup(chan);
09029 goto quit;
09030 }
09031 if (i & DAHDI_IOMUX_SIGEVENT) {
09032 res = dahdi_get_event(p->subs[idx].dfd);
09033 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
09034 if (res == DAHDI_EVENT_NOALARM) {
09035 p->inalarm = 0;
09036 }
09037
09038 if ( res == DAHDI_EVENT_POLARITY && p->hanguponpolarityswitch && p->polarity == POLARITY_REV) {
09039 ast_log(LOG_DEBUG, "Hanging up due to polarity reversal on channel %d while detecting callerid\n", p->channel);
09040 p->polarity = POLARITY_IDLE;
09041 callerid_free(cs);
09042 ast_hangup(chan);
09043 goto quit;
09044 }
09045 res = 0;
09046
09047
09048 curRingData[receivedRingT] = p->ringt;
09049
09050 if (p->ringt < p->ringt_base/2)
09051 break;
09052
09053
09054 if (++receivedRingT == ARRAY_LEN(curRingData))
09055 break;
09056 } else if (i & DAHDI_IOMUX_READ) {
09057 res = read(p->subs[idx].dfd, buf, sizeof(buf));
09058 if (res < 0) {
09059 if (errno != ELAST) {
09060 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
09061 callerid_free(cs);
09062 ast_hangup(chan);
09063 goto quit;
09064 }
09065 break;
09066 }
09067 if (p->ringt)
09068 p->ringt--;
09069 if (p->ringt == 1) {
09070 res = -1;
09071 break;
09072 }
09073 samples += res;
09074 res = callerid_feed(cs, buf, res, AST_LAW(p));
09075 if (res < 0) {
09076
09077
09078
09079
09080 ast_log(LOG_WARNING,
09081 "Failed to decode CallerID on channel '%s'\n",
09082 chan->name);
09083 break;
09084 } else if (res)
09085 break;
09086 else if (samples > (8000 * 10))
09087 break;
09088 }
09089 }
09090 if (res == 1) {
09091 callerid_get(cs, &name, &number, &flags);
09092 ast_debug(1, "CallerID number: %s, name: %s, flags=%d\n", number, name, flags);
09093 }
09094 if (distinctiveringaftercid == 1) {
09095
09096 for (receivedRingT = 0; receivedRingT < 3; receivedRingT++) {
09097 curRingData[receivedRingT] = 0;
09098 }
09099 receivedRingT = 0;
09100 ast_verb(3, "Detecting post-CID distinctive ring\n");
09101 for (;;) {
09102 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
09103 if ((res = ioctl(p->subs[idx].dfd, DAHDI_IOMUX, &i))) {
09104 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
09105 callerid_free(cs);
09106 ast_hangup(chan);
09107 goto quit;
09108 }
09109 if (i & DAHDI_IOMUX_SIGEVENT) {
09110 res = dahdi_get_event(p->subs[idx].dfd);
09111 ast_log(LOG_NOTICE, "Got event %d (%s)...\n", res, event2str(res));
09112 if (res == DAHDI_EVENT_NOALARM) {
09113 p->inalarm = 0;
09114 }
09115 res = 0;
09116
09117
09118 curRingData[receivedRingT] = p->ringt;
09119
09120 if (p->ringt < p->ringt_base/2)
09121 break;
09122
09123
09124 if (++receivedRingT == ARRAY_LEN(curRingData))
09125 break;
09126 } else if (i & DAHDI_IOMUX_READ) {
09127 res = read(p->subs[idx].dfd, buf, sizeof(buf));
09128 if (res < 0) {
09129 if (errno != ELAST) {
09130 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
09131 callerid_free(cs);
09132 ast_hangup(chan);
09133 goto quit;
09134 }
09135 break;
09136 }
09137 if (p->ringt)
09138 p->ringt--;
09139 if (p->ringt == 1) {
09140 res = -1;
09141 break;
09142 }
09143 }
09144 }
09145 }
09146 if (p->usedistinctiveringdetection) {
09147
09148 ast_verb(3, "Detected ring pattern: %d,%d,%d\n",curRingData[0],curRingData[1],curRingData[2]);
09149
09150 for (counter = 0; counter < 3; counter++) {
09151
09152
09153
09154 ast_verb(3, "Checking %d,%d,%d\n",
09155 p->drings.ringnum[counter].ring[0],
09156 p->drings.ringnum[counter].ring[1],
09157 p->drings.ringnum[counter].ring[2]);
09158 distMatches = 0;
09159 for (counter1 = 0; counter1 < 3; counter1++) {
09160 ast_verb(3, "Ring pattern check range: %d\n", p->drings.ringnum[counter].range);
09161 if (p->drings.ringnum[counter].ring[counter1] == -1) {
09162 ast_verb(3, "Pattern ignore (-1) detected, so matching pattern %d regardless.\n",
09163 curRingData[counter1]);
09164 distMatches++;
09165 }
09166 else if (curRingData[counter1] <= (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range) &&
09167 curRingData[counter1] >= (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range)) {
09168 ast_verb(3, "Ring pattern matched in range: %d to %d\n",
09169 (p->drings.ringnum[counter].ring[counter1] - p->drings.ringnum[counter].range),
09170 (p->drings.ringnum[counter].ring[counter1] + p->drings.ringnum[counter].range));
09171 distMatches++;
09172 }
09173 }
09174 if (distMatches == 3) {
09175
09176 ast_copy_string(p->context, p->drings.ringContext[counter].contextData, sizeof(p->context));
09177 ast_copy_string(chan->context, p->drings.ringContext[counter].contextData, sizeof(chan->context));
09178 ast_verb(3, "Distinctive Ring matched context %s\n",p->context);
09179 break;
09180 }
09181 }
09182 }
09183
09184 dahdi_setlinear(p->subs[idx].dfd, p->subs[idx].linear);
09185 #if 1
09186 restore_gains(p);
09187 #endif
09188 if (res < 0) {
09189 ast_log(LOG_WARNING, "CallerID returned with error on channel '%s'\n", chan->name);
09190 }
09191 } else
09192 ast_log(LOG_WARNING, "Unable to get caller ID space\n");
09193 }
09194 } else
09195 cs = NULL;
09196
09197 if (number)
09198 ast_shrink_phone_number(number);
09199 ast_set_callerid(chan, number, name, number);
09200
09201 if (smdi_msg)
09202 ASTOBJ_UNREF(smdi_msg, ast_smdi_md_message_destroy);
09203
09204 if (cs)
09205 callerid_free(cs);
09206
09207 if (flags & CID_MSGWAITING) {
09208 ast_log(LOG_NOTICE, "MWI: Channel %d message waiting!\n", p->channel);
09209 notify_message(p->mailbox, 1);
09210
09211 if (p->mwimonitor_rpas) {
09212 ast_hangup(chan);
09213 return NULL;
09214 }
09215 } else if (flags & CID_NOMSGWAITING) {
09216 ast_log(LOG_NOTICE, "MWI: Channel %d no message waiting!\n", p->channel);
09217 notify_message(p->mailbox, 0);
09218
09219 if (p->mwimonitor_rpas) {
09220 ast_hangup(chan);
09221 return NULL;
09222 }
09223 }
09224
09225 ast_setstate(chan, AST_STATE_RING);
09226 chan->rings = 1;
09227 p->ringt = p->ringt_base;
09228 res = ast_pbx_run(chan);
09229 if (res) {
09230 ast_hangup(chan);
09231 ast_log(LOG_WARNING, "PBX exited non-zero\n");
09232 }
09233 goto quit;
09234 default:
09235 ast_log(LOG_WARNING, "Don't know how to handle simple switch with signalling %s on channel %d\n", sig2str(p->sig), p->channel);
09236 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
09237 if (res < 0)
09238 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
09239 }
09240 res = tone_zone_play_tone(p->subs[idx].dfd, DAHDI_TONE_CONGESTION);
09241 if (res < 0)
09242 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", p->channel);
09243 ast_hangup(chan);
09244 quit:
09245 ast_mutex_lock(&ss_thread_lock);
09246 ss_thread_count--;
09247 ast_cond_signal(&ss_thread_complete);
09248 ast_mutex_unlock(&ss_thread_lock);
09249 return NULL;
09250 }
09251
09252 struct mwi_thread_data {
09253 struct dahdi_pvt *pvt;
09254 unsigned char buf[READ_SIZE];
09255 size_t len;
09256 };
09257
09258 static int calc_energy(const unsigned char *buf, int len, int law)
09259 {
09260 int x;
09261 int sum = 0;
09262
09263 if (!len)
09264 return 0;
09265
09266 for (x = 0; x < len; x++)
09267 sum += abs(law == AST_FORMAT_ULAW ? AST_MULAW(buf[x]) : AST_ALAW(buf[x]));
09268
09269 return sum / len;
09270 }
09271
09272 static void *mwi_thread(void *data)
09273 {
09274 struct mwi_thread_data *mtd = data;
09275 struct callerid_state *cs;
09276 pthread_t threadid;
09277 int samples = 0;
09278 char *name, *number;
09279 int flags;
09280 int i, res;
09281 unsigned int spill_done = 0;
09282 int spill_result = -1;
09283
09284 if (!(cs = callerid_new(mtd->pvt->cid_signalling))) {
09285 mtd->pvt->mwimonitoractive = 0;
09286
09287 return NULL;
09288 }
09289
09290 callerid_feed(cs, mtd->buf, mtd->len, AST_LAW(mtd->pvt));
09291
09292 bump_gains(mtd->pvt);
09293
09294 for (;;) {
09295 i = DAHDI_IOMUX_READ | DAHDI_IOMUX_SIGEVENT;
09296 if ((res = ioctl(mtd->pvt->subs[SUB_REAL].dfd, DAHDI_IOMUX, &i))) {
09297 ast_log(LOG_WARNING, "I/O MUX failed: %s\n", strerror(errno));
09298 goto quit;
09299 }
09300
09301 if (i & DAHDI_IOMUX_SIGEVENT) {
09302 struct ast_channel *chan;
09303
09304
09305
09306
09307 res = dahdi_get_event(mtd->pvt->subs[SUB_REAL].dfd);
09308
09309 switch (res) {
09310 case DAHDI_EVENT_NEONMWI_ACTIVE:
09311 case DAHDI_EVENT_NEONMWI_INACTIVE:
09312 case DAHDI_EVENT_NONE:
09313 case DAHDI_EVENT_BITSCHANGED:
09314 break;
09315 case DAHDI_EVENT_NOALARM:
09316 mtd->pvt->inalarm = 0;
09317 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", mtd->pvt->channel);
09318 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
09319 "Channel: %d\r\n", mtd->pvt->channel);
09320 break;
09321 case DAHDI_EVENT_ALARM:
09322 mtd->pvt->inalarm = 1;
09323 res = get_alarms(mtd->pvt);
09324 handle_alarms(mtd->pvt, res);
09325 break;
09326 default:
09327 ast_log(LOG_NOTICE, "Got event %d (%s)... Passing along to ss_thread\n", res, event2str(res));
09328 callerid_free(cs);
09329
09330 restore_gains(mtd->pvt);
09331 mtd->pvt->ringt = mtd->pvt->ringt_base;
09332
09333 if ((chan = dahdi_new(mtd->pvt, AST_STATE_RING, 0, SUB_REAL, 0, 0))) {
09334 if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
09335 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", mtd->pvt->channel);
09336 res = tone_zone_play_tone(mtd->pvt->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09337 if (res < 0)
09338 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", mtd->pvt->channel);
09339 ast_hangup(chan);
09340 goto quit;
09341 }
09342 goto quit_no_clean;
09343
09344 } else {
09345 ast_log(LOG_WARNING, "Could not create channel to handle call\n");
09346 }
09347 }
09348 } else if (i & DAHDI_IOMUX_READ) {
09349 if ((res = read(mtd->pvt->subs[SUB_REAL].dfd, mtd->buf, sizeof(mtd->buf))) < 0) {
09350 if (errno != ELAST) {
09351 ast_log(LOG_WARNING, "read returned error: %s\n", strerror(errno));
09352 goto quit;
09353 }
09354 break;
09355 }
09356 samples += res;
09357 if (!spill_done) {
09358 if ((spill_result = callerid_feed(cs, mtd->buf, res, AST_LAW(mtd->pvt))) < 0) {
09359
09360
09361
09362
09363 ast_log(LOG_WARNING, "Failed to decode CallerID\n");
09364 break;
09365 } else if (spill_result) {
09366 spill_done = 1;
09367 }
09368 } else {
09369
09370
09371
09372 if (calc_energy(mtd->buf, res, AST_LAW(mtd->pvt)) <= mwilevel)
09373 break;
09374 }
09375 if (samples > (8000 * 4))
09376 break;
09377 }
09378 }
09379
09380 if (spill_result == 1) {
09381 callerid_get(cs, &name, &number, &flags);
09382 if (flags & CID_MSGWAITING) {
09383 ast_log(LOG_NOTICE, "mwi: Have Messages on channel %d\n", mtd->pvt->channel);
09384 notify_message(mtd->pvt->mailbox, 1);
09385 } else if (flags & CID_NOMSGWAITING) {
09386 ast_log(LOG_NOTICE, "mwi: No Messages on channel %d\n", mtd->pvt->channel);
09387 notify_message(mtd->pvt->mailbox, 0);
09388 } else {
09389 ast_log(LOG_NOTICE, "mwi: Status unknown on channel %d\n", mtd->pvt->channel);
09390 }
09391 }
09392
09393
09394 quit:
09395 callerid_free(cs);
09396
09397 restore_gains(mtd->pvt);
09398
09399 quit_no_clean:
09400 mtd->pvt->mwimonitoractive = 0;
09401
09402 ast_free(mtd);
09403
09404 return NULL;
09405 }
09406
09407
09408
09409
09410
09411
09412
09413
09414 static int mwi_send_init(struct dahdi_pvt * pvt)
09415 {
09416 int x, res;
09417
09418 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09419
09420 if (pvt->mwisend_rpas) {
09421 pvt->mwisend_data.mwisend_current = MWI_SEND_SA;
09422 pvt->mwisendactive = 1;
09423 } else if (pvt->mwisend_fsk) {
09424 pvt->mwisend_data.mwisend_current = MWI_SEND_SPILL;
09425 pvt->mwisendactive = 1;
09426 } else {
09427 pvt->mwisendactive = 0;
09428 return 0;
09429 }
09430 #else
09431 if (mwisend_rpas) {
09432 pvt->mwisend_data.mwisend_current = MWI_SEND_SA;
09433 } else {
09434 pvt->mwisend_data.mwisend_current = MWI_SEND_SPILL;
09435 }
09436 pvt->mwisendactive = 1;
09437 #endif
09438
09439 if (pvt->cidspill) {
09440 ast_log(LOG_WARNING, "cidspill already exists when trying to send FSK MWI\n");
09441 ast_free(pvt->cidspill);
09442 pvt->cidspill = NULL;
09443 pvt->cidpos = 0;
09444 pvt->cidlen = 0;
09445 }
09446 pvt->cidspill = ast_calloc(1, MAX_CALLERID_SIZE);
09447 if (!pvt->cidspill) {
09448 pvt->mwisendactive = 0;
09449 return -1;
09450 }
09451 x = DAHDI_FLUSH_BOTH;
09452 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_FLUSH, &x);
09453 x = 3000;
09454 ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_ONHOOKTRANSFER, &x);
09455 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09456 if (pvt->mwisend_fsk) {
09457 #endif
09458 pvt->cidlen = vmwi_generate(pvt->cidspill, has_voicemail(pvt), CID_MWI_TYPE_MDMF_FULL,
09459 AST_LAW(pvt), pvt->cid_name, pvt->cid_num, 0);
09460 pvt->cidpos = 0;
09461 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09462 }
09463 #endif
09464 return 0;
09465 }
09466
09467 static int mwi_send_process_buffer(struct dahdi_pvt * pvt, int num_read)
09468 {
09469 struct timeval now;
09470 int res;
09471
09472
09473
09474
09475 if (MWI_SEND_DONE != pvt->mwisend_data.mwisend_current && !pvt->cidspill) {
09476 pvt->mwisend_data.mwisend_current = MWI_SEND_DONE;
09477 } else if (MWI_SEND_DONE != pvt->mwisend_data.mwisend_current) {
09478
09479 switch ( pvt->mwisend_data.mwisend_current) {
09480 case MWI_SEND_SA:
09481
09482 res = ioctl(pvt->subs[SUB_REAL].dfd, DAHDI_SETCADENCE, &AS_RP_cadence);
09483 if (res) {
09484 ast_log(LOG_WARNING, "Unable to set RP-AS ring cadence: %s\n", strerror(errno));
09485 goto quit;
09486 }
09487 res = dahdi_set_hook(pvt->subs[SUB_REAL].dfd, DAHDI_RING);
09488 pvt->mwisend_data.mwisend_current = MWI_SEND_SA_WAIT;
09489 break;
09490 case MWI_SEND_SA_WAIT:
09491 break;
09492 case MWI_SEND_PAUSE:
09493 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09494 if (pvt->mwisend_fsk) {
09495 #endif
09496 gettimeofday(&now, NULL);
09497 if ((int)(now.tv_sec - pvt->mwisend_data.pause.tv_sec) * 1000000 + (int)now.tv_usec - (int)pvt->mwisend_data.pause.tv_usec > 500000) {
09498 pvt->mwisend_data.mwisend_current = MWI_SEND_SPILL;
09499 }
09500 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
09501 } else {
09502 pvt->mwisend_data.mwisend_current = MWI_SEND_CLEANUP;
09503 }
09504 #endif
09505 break;
09506 case MWI_SEND_SPILL:
09507
09508 if(0 < num_read) {
09509 if (num_read > pvt->cidlen - pvt->cidpos)
09510 num_read = pvt->cidlen - pvt->cidpos;
09511 res = write(pvt->subs[SUB_REAL].dfd, pvt->cidspill + pvt->cidpos, num_read);
09512 if (res > 0) {
09513 pvt->cidpos += res;
09514 if (pvt->cidpos >= pvt->cidlen) {
09515 pvt->mwisend_data.mwisend_current = MWI_SEND_CLEANUP;
09516 }
09517 } else {
09518 ast_log(LOG_WARNING, "MWI FSK Send Write failed: %s\n", strerror(errno));
09519 goto quit;
09520 }
09521 }
09522 break;
09523 case MWI_SEND_CLEANUP:
09524
09525 pvt->mwisend_data.mwisend_current = MWI_SEND_DONE;
09526 break;
09527 default:
09528
09529 goto quit;
09530 }
09531 }
09532
09533 if (MWI_SEND_DONE == pvt->mwisend_data.mwisend_current) {
09534 if (pvt->cidspill) {
09535 ast_free(pvt->cidspill);
09536 pvt->cidspill = NULL;
09537 pvt->cidpos = 0;
09538 pvt->cidlen = 0;
09539 }
09540 pvt->mwisendactive = 0;
09541 }
09542 return 0;
09543 quit:
09544 if (pvt->cidspill) {
09545 ast_free(pvt->cidspill);
09546 pvt->cidspill = NULL;
09547 pvt->cidpos = 0;
09548 pvt->cidlen = 0;
09549 }
09550 pvt->mwisendactive = 0;
09551 return -1;
09552 }
09553
09554 static int mwi_send_process_event(struct dahdi_pvt * pvt, int event)
09555 {
09556 int handled = 0;
09557
09558 if (MWI_SEND_DONE != pvt->mwisend_data.mwisend_current) {
09559 switch (event) {
09560 case DAHDI_EVENT_RINGEROFF:
09561 if(pvt->mwisend_data.mwisend_current == MWI_SEND_SA_WAIT) {
09562 handled = 1;
09563
09564 if (dahdi_set_hook(pvt->subs[SUB_REAL].dfd, DAHDI_RINGOFF) ) {
09565 ast_log(LOG_WARNING, "Unable to finish RP-AS: %s mwi send aborted\n", strerror(errno));
09566 ast_free(pvt->cidspill);
09567 pvt->cidspill = NULL;
09568 pvt->mwisend_data.mwisend_current = MWI_SEND_DONE;
09569 pvt->mwisendactive = 0;
09570 } else {
09571 pvt->mwisend_data.mwisend_current = MWI_SEND_PAUSE;
09572 gettimeofday(&pvt->mwisend_data.pause, NULL);
09573 }
09574 }
09575 break;
09576
09577 case DAHDI_EVENT_RINGOFFHOOK:
09578 if (pvt->cidspill) {
09579 ast_free(pvt->cidspill);
09580 pvt->cidspill = NULL;
09581 pvt->cidpos = 0;
09582 pvt->cidlen = 0;
09583 }
09584 pvt->mwisend_data.mwisend_current = MWI_SEND_DONE;
09585 pvt->mwisendactive = 0;
09586 break;
09587 case DAHDI_EVENT_RINGERON:
09588 case DAHDI_EVENT_HOOKCOMPLETE:
09589 break;
09590 default:
09591 break;
09592 }
09593 }
09594 return handled;
09595 }
09596
09597
09598 static int dahdi_destroy_channel_bynum(int channel)
09599 {
09600 struct dahdi_pvt *tmp = NULL;
09601 struct dahdi_pvt *prev = NULL;
09602
09603 tmp = iflist;
09604 while (tmp) {
09605 if (tmp->channel == channel) {
09606 int x = DAHDI_FLASH;
09607 ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
09608 destroy_channel(prev, tmp, 1);
09609 ast_module_unref(ast_module_info->self);
09610 return RESULT_SUCCESS;
09611 }
09612 prev = tmp;
09613 tmp = tmp->next;
09614 }
09615 return RESULT_FAILURE;
09616 }
09617
09618 static struct dahdi_pvt *handle_init_event(struct dahdi_pvt *i, int event)
09619 {
09620 int res;
09621 pthread_t threadid;
09622 struct ast_channel *chan;
09623
09624
09625
09626 switch (event) {
09627 case DAHDI_EVENT_NONE:
09628 case DAHDI_EVENT_BITSCHANGED:
09629 break;
09630 case DAHDI_EVENT_WINKFLASH:
09631 case DAHDI_EVENT_RINGOFFHOOK:
09632 if (i->inalarm) break;
09633 if (i->radio) break;
09634
09635 switch (i->sig) {
09636 case SIG_FXOLS:
09637 case SIG_FXOGS:
09638 case SIG_FXOKS:
09639 res = dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
09640 i->fxsoffhookstate = 1;
09641 if (res && (errno == EBUSY))
09642 break;
09643
09644
09645 ast_free(i->cidspill);
09646 i->cidspill = NULL;
09647 restore_conference(i);
09648
09649 if (i->immediate) {
09650 dahdi_enable_ec(i);
09651
09652 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_RINGTONE);
09653 chan = dahdi_new(i, AST_STATE_RING, 1, SUB_REAL, 0, 0);
09654 if (!chan) {
09655 ast_log(LOG_WARNING, "Unable to start PBX on channel %d\n", i->channel);
09656 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09657 if (res < 0)
09658 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
09659 }
09660 } else {
09661
09662 chan = dahdi_new(i, AST_STATE_RESERVED, 0, SUB_REAL, 0, 0);
09663 if (chan) {
09664 if (has_voicemail(i))
09665 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_STUTTER);
09666 else
09667 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_DIALTONE);
09668 if (res < 0)
09669 ast_log(LOG_WARNING, "Unable to play dialtone on channel %d, do you have defaultzone and loadzone defined?\n", i->channel);
09670 if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
09671 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
09672 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09673 if (res < 0)
09674 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
09675 ast_hangup(chan);
09676 }
09677 } else
09678 ast_log(LOG_WARNING, "Unable to create channel\n");
09679 }
09680 break;
09681 case SIG_FXSLS:
09682 case SIG_FXSGS:
09683 case SIG_FXSKS:
09684 i->ringt = i->ringt_base;
09685
09686 case SIG_EMWINK:
09687 case SIG_FEATD:
09688 case SIG_FEATDMF:
09689 case SIG_FEATDMF_TA:
09690 case SIG_E911:
09691 case SIG_FGC_CAMA:
09692 case SIG_FGC_CAMAMF:
09693 case SIG_FEATB:
09694 case SIG_EM:
09695 case SIG_EM_E1:
09696 case SIG_SFWINK:
09697 case SIG_SF_FEATD:
09698 case SIG_SF_FEATDMF:
09699 case SIG_SF_FEATB:
09700 case SIG_SF:
09701
09702 if (i->cid_start == CID_START_POLARITY_IN) {
09703 chan = dahdi_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
09704 } else {
09705 chan = dahdi_new(i, AST_STATE_RING, 0, SUB_REAL, 0, 0);
09706 }
09707
09708 if (!chan) {
09709 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
09710 } else if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
09711 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
09712 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09713 if (res < 0) {
09714 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
09715 }
09716 ast_hangup(chan);
09717 }
09718 break;
09719 default:
09720 ast_log(LOG_WARNING, "Don't know how to handle ring/answer with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
09721 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, DAHDI_TONE_CONGESTION);
09722 if (res < 0)
09723 ast_log(LOG_WARNING, "Unable to play congestion tone on channel %d\n", i->channel);
09724 return NULL;
09725 }
09726 break;
09727 case DAHDI_EVENT_NOALARM:
09728 i->inalarm = 0;
09729 ast_log(LOG_NOTICE, "Alarm cleared on channel %d\n", i->channel);
09730 manager_event(EVENT_FLAG_SYSTEM, "AlarmClear",
09731 "Channel: %d\r\n", i->channel);
09732 break;
09733 case DAHDI_EVENT_ALARM:
09734 i->inalarm = 1;
09735 res = get_alarms(i);
09736 handle_alarms(i, res);
09737
09738 case DAHDI_EVENT_ONHOOK:
09739 if (i->radio)
09740 break;
09741
09742 switch (i->sig) {
09743 case SIG_FXOLS:
09744 case SIG_FXOGS:
09745 case SIG_FEATD:
09746 case SIG_FEATDMF:
09747 case SIG_FEATDMF_TA:
09748 case SIG_E911:
09749 case SIG_FGC_CAMA:
09750 case SIG_FGC_CAMAMF:
09751 case SIG_FEATB:
09752 case SIG_EM:
09753 case SIG_EM_E1:
09754 case SIG_EMWINK:
09755 case SIG_SF_FEATD:
09756 case SIG_SF_FEATDMF:
09757 case SIG_SF_FEATB:
09758 case SIG_SF:
09759 case SIG_SFWINK:
09760 case SIG_FXSLS:
09761 case SIG_FXSGS:
09762 case SIG_FXSKS:
09763 case SIG_GR303FXSKS:
09764 dahdi_disable_ec(i);
09765 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
09766 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
09767 break;
09768 case SIG_GR303FXOKS:
09769 case SIG_FXOKS:
09770 dahdi_disable_ec(i);
09771
09772 #ifdef ZHONE_HACK
09773 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_OFFHOOK);
09774 usleep(1);
09775 #endif
09776 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
09777 dahdi_set_hook(i->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
09778 break;
09779 case SIG_PRI:
09780 case SIG_SS7:
09781 case SIG_BRI:
09782 case SIG_BRI_PTMP:
09783 dahdi_disable_ec(i);
09784 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
09785 break;
09786 default:
09787 ast_log(LOG_WARNING, "Don't know how to handle on hook with signalling %s on channel %d\n", sig2str(i->sig), i->channel);
09788 res = tone_zone_play_tone(i->subs[SUB_REAL].dfd, -1);
09789 return NULL;
09790 }
09791 if (i->sig & __DAHDI_SIG_FXO) {
09792 i->fxsoffhookstate = 0;
09793 }
09794 break;
09795 case DAHDI_EVENT_POLARITY:
09796 switch (i->sig) {
09797 case SIG_FXSLS:
09798 case SIG_FXSKS:
09799 case SIG_FXSGS:
09800
09801
09802
09803
09804 if (i->hanguponpolarityswitch)
09805 i->polarity = POLARITY_REV;
09806 if (i->cid_start == CID_START_POLARITY || i->cid_start == CID_START_POLARITY_IN) {
09807 i->polarity = POLARITY_REV;
09808 ast_verb(2, "Starting post polarity "
09809 "CID detection on channel %d\n",
09810 i->channel);
09811 chan = dahdi_new(i, AST_STATE_PRERING, 0, SUB_REAL, 0, 0);
09812 if (!chan) {
09813 ast_log(LOG_WARNING, "Cannot allocate new structure on channel %d\n", i->channel);
09814 } else if (ast_pthread_create_detached(&threadid, NULL, ss_thread, chan)) {
09815 ast_log(LOG_WARNING, "Unable to start simple switch thread on channel %d\n", i->channel);
09816 }
09817 }
09818 break;
09819 default:
09820 ast_log(LOG_WARNING, "handle_init_event detected "
09821 "polarity reversal on non-FXO (SIG_FXS) "
09822 "interface %d\n", i->channel);
09823 }
09824 break;
09825 case DAHDI_EVENT_REMOVED:
09826 ast_log(LOG_NOTICE,
09827 "Got DAHDI_EVENT_REMOVED. Destroying channel %d\n",
09828 i->channel);
09829 return i;
09830 case DAHDI_EVENT_NEONMWI_ACTIVE:
09831 if (i->mwimonitor_neon) {
09832 notify_message(i->mailbox, 1);
09833 ast_log(LOG_NOTICE, "NEON MWI set for channel %d, mailbox %s \n", i->channel, i->mailbox);
09834 }
09835 break;
09836 case DAHDI_EVENT_NEONMWI_INACTIVE:
09837 if (i->mwimonitor_neon) {
09838 notify_message(i->mailbox, 0);
09839 ast_log(LOG_NOTICE, "NEON MWI cleared for channel %d, mailbox %s\n", i->channel, i->mailbox);
09840 }
09841 break;
09842 }
09843 return NULL;
09844 }
09845
09846 static void *do_monitor(void *data)
09847 {
09848 int count, res, res2, spoint, pollres=0;
09849 struct dahdi_pvt *i;
09850 struct dahdi_pvt *last = NULL;
09851 struct dahdi_pvt *doomed;
09852 time_t thispass = 0, lastpass = 0;
09853 int found;
09854 char buf[1024];
09855 struct pollfd *pfds=NULL;
09856 int lastalloc = -1;
09857
09858
09859
09860 #if 0
09861 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
09862 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
09863 return NULL;
09864 }
09865 ast_debug(1, "Monitor starting...\n");
09866 #endif
09867 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
09868
09869 for (;;) {
09870
09871 ast_mutex_lock(&iflock);
09872 if (!pfds || (lastalloc != ifcount)) {
09873 if (pfds) {
09874 ast_free(pfds);
09875 pfds = NULL;
09876 }
09877 if (ifcount) {
09878 if (!(pfds = ast_calloc(1, ifcount * sizeof(*pfds)))) {
09879 ast_mutex_unlock(&iflock);
09880 return NULL;
09881 }
09882 }
09883 lastalloc = ifcount;
09884 }
09885
09886
09887 count = 0;
09888 i = iflist;
09889 while (i) {
09890 if ((i->subs[SUB_REAL].dfd > -1) && i->sig && (!i->radio) && !(i->sig & SIG_MFCR2)) {
09891 if (!i->owner && !i->subs[SUB_REAL].owner && !i->mwimonitoractive ) {
09892
09893 pfds[count].fd = i->subs[SUB_REAL].dfd;
09894 pfds[count].events = POLLPRI;
09895 pfds[count].revents = 0;
09896
09897
09898 if (i->cidspill || i->mwisendactive || i->mwimonitor_fsk)
09899 pfds[count].events |= POLLIN;
09900 count++;
09901 }
09902 }
09903 i = i->next;
09904 }
09905
09906 ast_mutex_unlock(&iflock);
09907
09908 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
09909 pthread_testcancel();
09910
09911 res = poll(pfds, count, 1000);
09912 pthread_testcancel();
09913 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
09914
09915
09916 if (res < 0) {
09917 if ((errno != EAGAIN) && (errno != EINTR))
09918 ast_log(LOG_WARNING, "poll return %d: %s\n", res, strerror(errno));
09919 continue;
09920 }
09921
09922
09923 ast_mutex_lock(&iflock);
09924 found = 0;
09925 spoint = 0;
09926 lastpass = thispass;
09927 thispass = time(NULL);
09928 i = iflist;
09929 doomed = NULL;
09930 for (i = iflist;; i = i->next) {
09931 if (doomed) {
09932 int res;
09933 res = dahdi_destroy_channel_bynum(doomed->channel);
09934 if (!res) {
09935 ast_log(LOG_WARNING, "Couldn't find channel to destroy, hopefully another destroy operation just happened.\n");
09936 }
09937 doomed = NULL;
09938 }
09939 if (!i) {
09940 break;
09941 }
09942
09943 if (thispass != lastpass) {
09944 if (!found && ((i == last) || ((i == iflist) && !last))) {
09945 last = i;
09946 if (last) {
09947
09948 if (!last->mwisendactive
09949 && (last->sig & __DAHDI_SIG_FXO)
09950 && !last->fxsoffhookstate
09951 && !last->owner
09952 && !ast_strlen_zero(last->mailbox)
09953 && (thispass - last->onhooktime > 3)) {
09954 res = has_voicemail(last);
09955 if (last->msgstate != res) {
09956
09957 res2 = ioctl(last->subs[SUB_REAL].dfd, DAHDI_VMWI, &res);
09958 if (res2) {
09959
09960 ast_debug(3, "Unable to control message waiting led on channel %d: %s\n", last->channel, strerror(errno));
09961 }
09962
09963 if (mwi_send_init(last)) {
09964 ast_log(LOG_WARNING, "Unable to initiate mwi send sequence on channel %d\n", last->channel);
09965 }
09966 last->msgstate = res;
09967 found ++;
09968 }
09969 }
09970 last = last->next;
09971 }
09972 }
09973 }
09974 if ((i->subs[SUB_REAL].dfd > -1) && i->sig) {
09975 if (i->radio && !i->owner)
09976 {
09977 res = dahdi_get_event(i->subs[SUB_REAL].dfd);
09978 if (res)
09979 {
09980 ast_debug(1, "Monitor doohicky got event %s on radio channel %d\n", event2str(res), i->channel);
09981
09982 ast_mutex_unlock(&iflock);
09983 doomed = handle_init_event(i, res);
09984 ast_mutex_lock(&iflock);
09985 }
09986 continue;
09987 }
09988 pollres = ast_fdisset(pfds, i->subs[SUB_REAL].dfd, count, &spoint);
09989 if (pollres & POLLIN) {
09990 if (i->owner || i->subs[SUB_REAL].owner) {
09991 #ifdef HAVE_PRI
09992 if (!i->pri)
09993 #endif
09994 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d) in read...\n", i->subs[SUB_REAL].dfd);
09995 continue;
09996 }
09997 if (!i->mwimonitor_fsk && !i->mwisendactive) {
09998 ast_log(LOG_WARNING, "Whoa.... I'm not looking for MWI or sending MWI but am reading (%d)...\n", i->subs[SUB_REAL].dfd);
09999 continue;
10000 }
10001 res = read(i->subs[SUB_REAL].dfd, buf, sizeof(buf));
10002 if (res > 0) {
10003 if (i->mwimonitor_fsk) {
10004 if (calc_energy((unsigned char *) buf, res, AST_LAW(i)) > mwilevel) {
10005 pthread_attr_t attr;
10006 pthread_t threadid;
10007 struct mwi_thread_data *mtd;
10008
10009 pthread_attr_init(&attr);
10010 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
10011
10012 ast_log(LOG_DEBUG, "Maybe some MWI on port %d!\n", i->channel);
10013 if ((mtd = ast_calloc(1, sizeof(*mtd)))) {
10014 mtd->pvt = i;
10015 memcpy(mtd->buf, buf, res);
10016 mtd->len = res;
10017 if (ast_pthread_create_background(&threadid, &attr, mwi_thread, mtd)) {
10018 ast_log(LOG_WARNING, "Unable to start mwi thread on channel %d\n", i->channel);
10019 ast_free(mtd);
10020 }
10021 i->mwimonitoractive = 1;
10022 }
10023 }
10024 }
10025 if (i->mwisendactive) {
10026 mwi_send_process_buffer(i, res);
10027 }
10028 } else {
10029 ast_log(LOG_WARNING, "Read failed with %d: %s\n", res, strerror(errno));
10030 }
10031 }
10032 if (pollres & POLLPRI) {
10033 if (i->owner || i->subs[SUB_REAL].owner) {
10034 #ifdef HAVE_PRI
10035 if (!i->pri)
10036 #endif
10037 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d)...\n", i->subs[SUB_REAL].dfd);
10038 continue;
10039 }
10040 res = dahdi_get_event(i->subs[SUB_REAL].dfd);
10041 ast_debug(1, "Monitor doohicky got event %s on channel %d\n", event2str(res), i->channel);
10042
10043 ast_mutex_unlock(&iflock);
10044 if (0 == i->mwisendactive || 0 == mwi_send_process_event(i, res)) {
10045 doomed = handle_init_event(i, res);
10046 }
10047 ast_mutex_lock(&iflock);
10048 }
10049 }
10050 }
10051 ast_mutex_unlock(&iflock);
10052 }
10053
10054 return NULL;
10055
10056 }
10057
10058 static int restart_monitor(void)
10059 {
10060
10061 if (monitor_thread == AST_PTHREADT_STOP)
10062 return 0;
10063 ast_mutex_lock(&monlock);
10064 if (monitor_thread == pthread_self()) {
10065 ast_mutex_unlock(&monlock);
10066 ast_log(LOG_WARNING, "Cannot kill myself\n");
10067 return -1;
10068 }
10069 if (monitor_thread != AST_PTHREADT_NULL) {
10070
10071 pthread_kill(monitor_thread, SIGURG);
10072 } else {
10073
10074 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
10075 ast_mutex_unlock(&monlock);
10076 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
10077 return -1;
10078 }
10079 }
10080 ast_mutex_unlock(&monlock);
10081 return 0;
10082 }
10083
10084 #if defined(HAVE_PRI)
10085 static int pri_resolve_span(int *span, int channel, int offset, struct dahdi_spaninfo *si)
10086 {
10087 int x;
10088 int trunkgroup;
10089
10090 trunkgroup = pris[*span].mastertrunkgroup;
10091 if (trunkgroup) {
10092
10093 for (x = 0; x < NUM_SPANS; x++) {
10094 if (pris[x].trunkgroup == trunkgroup) {
10095 *span = x;
10096 return 0;
10097 }
10098 }
10099 ast_log(LOG_WARNING, "Channel %d on span %d configured to use nonexistent trunk group %d\n", channel, *span, trunkgroup);
10100 *span = -1;
10101 } else {
10102 if (pris[*span].trunkgroup) {
10103 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is trunk group %d (please use spanmap)\n", *span, pris[*span].trunkgroup);
10104 *span = -1;
10105 } else if (pris[*span].mastertrunkgroup) {
10106 ast_log(LOG_WARNING, "Unable to use span %d implicitly since it is already part of trunk group %d\n", *span, pris[*span].mastertrunkgroup);
10107 *span = -1;
10108 } else {
10109 if (si->totalchans == 31) {
10110
10111 pris[*span].dchannels[0] = 16 + offset;
10112 } else if (si->totalchans == 24) {
10113
10114 pris[*span].dchannels[0] = 24 + offset;
10115 } else if (si->totalchans == 3) {
10116
10117 pris[*span].dchannels[0] = 3 + offset;
10118 } else {
10119 ast_log(LOG_WARNING, "Unable to use span %d, since the D-channel cannot be located (unexpected span size of %d channels)\n", *span, si->totalchans);
10120 *span = -1;
10121 return 0;
10122 }
10123 pris[*span].dchanavail[0] |= DCHAN_PROVISIONED;
10124 pris[*span].offset = offset;
10125 pris[*span].span = *span + 1;
10126 }
10127 }
10128 return 0;
10129 }
10130 #endif
10131
10132 #if defined(HAVE_PRI)
10133 static int pri_create_trunkgroup(int trunkgroup, int *channels)
10134 {
10135 struct dahdi_spaninfo si;
10136 struct dahdi_params p;
10137 int fd;
10138 int span;
10139 int ospan=0;
10140 int x,y;
10141 for (x = 0; x < NUM_SPANS; x++) {
10142 if (pris[x].trunkgroup == trunkgroup) {
10143 ast_log(LOG_WARNING, "Trunk group %d already exists on span %d, Primary d-channel %d\n", trunkgroup, x + 1, pris[x].dchannels[0]);
10144 return -1;
10145 }
10146 }
10147 for (y = 0; y < NUM_DCHANS; y++) {
10148 if (!channels[y])
10149 break;
10150 memset(&si, 0, sizeof(si));
10151 memset(&p, 0, sizeof(p));
10152 fd = open("/dev/dahdi/channel", O_RDWR);
10153 if (fd < 0) {
10154 ast_log(LOG_WARNING, "Failed to open channel: %s\n", strerror(errno));
10155 return -1;
10156 }
10157 x = channels[y];
10158 if (ioctl(fd, DAHDI_SPECIFY, &x)) {
10159 ast_log(LOG_WARNING, "Failed to specify channel %d: %s\n", channels[y], strerror(errno));
10160 close(fd);
10161 return -1;
10162 }
10163 if (ioctl(fd, DAHDI_GET_PARAMS, &p)) {
10164 ast_log(LOG_WARNING, "Failed to get channel parameters for channel %d: %s\n", channels[y], strerror(errno));
10165 return -1;
10166 }
10167 if (ioctl(fd, DAHDI_SPANSTAT, &si)) {
10168 ast_log(LOG_WARNING, "Failed go get span information on channel %d (span %d): %s\n", channels[y], p.spanno, strerror(errno));
10169 close(fd);
10170 return -1;
10171 }
10172 span = p.spanno - 1;
10173 if (pris[span].trunkgroup) {
10174 ast_log(LOG_WARNING, "Span %d is already provisioned for trunk group %d\n", span + 1, pris[span].trunkgroup);
10175 close(fd);
10176 return -1;
10177 }
10178 if (pris[span].pvts[0]) {
10179 ast_log(LOG_WARNING, "Span %d is already provisioned with channels (implicit PRI maybe?)\n", span + 1);
10180 close(fd);
10181 return -1;
10182 }
10183 if (!y) {
10184 pris[span].trunkgroup = trunkgroup;
10185 pris[span].offset = channels[y] - p.chanpos;
10186 ospan = span;
10187 }
10188 pris[ospan].dchannels[y] = channels[y];
10189 pris[ospan].dchanavail[y] |= DCHAN_PROVISIONED;
10190 pris[span].span = span + 1;
10191 close(fd);
10192 }
10193 return 0;
10194 }
10195 #endif
10196
10197 #if defined(HAVE_PRI)
10198 static int pri_create_spanmap(int span, int trunkgroup, int logicalspan)
10199 {
10200 if (pris[span].mastertrunkgroup) {
10201 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);
10202 return -1;
10203 }
10204 pris[span].mastertrunkgroup = trunkgroup;
10205 pris[span].prilogicalspan = logicalspan;
10206 return 0;
10207 }
10208 #endif
10209
10210 #if defined(HAVE_SS7)
10211 static unsigned int parse_pointcode(const char *pcstring)
10212 {
10213 unsigned int code1, code2, code3;
10214 int numvals;
10215
10216 numvals = sscanf(pcstring, "%30d-%30d-%30d", &code1, &code2, &code3);
10217 if (numvals == 1)
10218 return code1;
10219 if (numvals == 3)
10220 return (code1 << 16) | (code2 << 8) | code3;
10221
10222 return 0;
10223 }
10224 #endif
10225
10226 #if defined(HAVE_SS7)
10227 static struct dahdi_ss7 * ss7_resolve_linkset(int linkset)
10228 {
10229 if ((linkset < 0) || (linkset >= NUM_SPANS))
10230 return NULL;
10231 else
10232 return &linksets[linkset - 1];
10233 }
10234 #endif
10235
10236 #ifdef HAVE_OPENR2
10237 static void dahdi_r2_destroy_links(void)
10238 {
10239 int i = 0;
10240 if (!r2links) {
10241 return;
10242 }
10243 for (; i < r2links_count; i++) {
10244 if (r2links[i]->r2master != AST_PTHREADT_NULL) {
10245 pthread_cancel(r2links[i]->r2master);
10246 pthread_join(r2links[i]->r2master, NULL);
10247 openr2_context_delete(r2links[i]->protocol_context);
10248 }
10249 ast_free(r2links[i]);
10250 }
10251 ast_free(r2links);
10252 r2links = NULL;
10253 r2links_count = 0;
10254 }
10255
10256 #define R2_LINK_CAPACITY 10
10257 static struct dahdi_mfcr2 *dahdi_r2_get_link(void)
10258 {
10259 struct dahdi_mfcr2 *new_r2link = NULL;
10260 struct dahdi_mfcr2 **new_r2links = NULL;
10261
10262
10263 if (!r2links_count || (r2links[r2links_count - 1]->monitored_count == R2_LINK_CAPACITY)) {
10264 new_r2link = ast_calloc(1, sizeof(**r2links));
10265 if (!new_r2link) {
10266 ast_log(LOG_ERROR, "Cannot allocate R2 link!\n");
10267 return NULL;
10268 }
10269 new_r2links = ast_realloc(r2links, ((r2links_count + 1) * sizeof(*r2links)));
10270 if (!new_r2links) {
10271 ast_log(LOG_ERROR, "Cannot allocate R2 link!\n");
10272 ast_free(new_r2link);
10273 return NULL;
10274 }
10275 r2links = new_r2links;
10276 new_r2link->r2master = AST_PTHREADT_NULL;
10277 r2links[r2links_count] = new_r2link;
10278 r2links_count++;
10279 ast_log(LOG_DEBUG, "Created new R2 link!\n");
10280 }
10281 return r2links[r2links_count - 1];
10282 }
10283
10284 static int dahdi_r2_set_context(struct dahdi_mfcr2 *r2_link, const struct dahdi_chan_conf *conf)
10285 {
10286 char tmplogdir[] = "/tmp";
10287 char logdir[OR2_MAX_PATH];
10288 int threshold = 0;
10289 int snres = 0;
10290 r2_link->protocol_context = openr2_context_new(NULL, &dahdi_r2_event_iface,
10291 &dahdi_r2_transcode_iface, conf->mfcr2.variant, conf->mfcr2.max_ani,
10292 conf->mfcr2.max_dnis);
10293 if (!r2_link->protocol_context) {
10294 return -1;
10295 }
10296 openr2_context_set_log_level(r2_link->protocol_context, conf->mfcr2.loglevel);
10297 openr2_context_set_ani_first(r2_link->protocol_context, conf->mfcr2.get_ani_first);
10298 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
10299 openr2_context_set_skip_category_request(r2_link->protocol_context, conf->mfcr2.skip_category_request);
10300 #endif
10301 openr2_context_set_mf_threshold(r2_link->protocol_context, threshold);
10302 openr2_context_set_mf_back_timeout(r2_link->protocol_context, conf->mfcr2.mfback_timeout);
10303 openr2_context_set_metering_pulse_timeout(r2_link->protocol_context, conf->mfcr2.metering_pulse_timeout);
10304 openr2_context_set_double_answer(r2_link->protocol_context, conf->mfcr2.double_answer);
10305 openr2_context_set_immediate_accept(r2_link->protocol_context, conf->mfcr2.immediate_accept);
10306 if (ast_strlen_zero(conf->mfcr2.logdir)) {
10307 if (openr2_context_set_log_directory(r2_link->protocol_context, tmplogdir)) {
10308 ast_log(LOG_ERROR, "Failed setting default MFC/R2 log directory %s\n", tmplogdir);
10309 }
10310 } else {
10311 snres = snprintf(logdir, sizeof(logdir), "%s/%s/%s", ast_config_AST_LOG_DIR, "mfcr2", conf->mfcr2.logdir);
10312 if (snres >= sizeof(logdir)) {
10313 ast_log(LOG_ERROR, "MFC/R2 logging directory truncated, using %s\n", tmplogdir);
10314 if (openr2_context_set_log_directory(r2_link->protocol_context, tmplogdir)) {
10315 ast_log(LOG_ERROR, "Failed setting default MFC/R2 log directory %s\n", tmplogdir);
10316 }
10317 } else {
10318 if (openr2_context_set_log_directory(r2_link->protocol_context, logdir)) {
10319 ast_log(LOG_ERROR, "Failed setting MFC/R2 log directory %s\n", logdir);
10320 }
10321 }
10322 }
10323 if (!ast_strlen_zero(conf->mfcr2.r2proto_file)) {
10324 if (openr2_context_configure_from_advanced_file(r2_link->protocol_context, conf->mfcr2.r2proto_file)) {
10325 ast_log(LOG_ERROR, "Failed to configure r2context from advanced configuration file %s\n", conf->mfcr2.r2proto_file);
10326 }
10327 }
10328 r2_link->monitored_count = 0;
10329 return 0;
10330 }
10331 #endif
10332
10333
10334
10335
10336
10337
10338 static int sigtype_to_signalling(int sigtype)
10339 {
10340 return sigtype;
10341 }
10342
10343 static struct dahdi_pvt *mkintf(int channel, const struct dahdi_chan_conf *conf, struct dahdi_pri *pri, int reloading)
10344 {
10345
10346 struct dahdi_pvt *tmp = NULL, *tmp2, *prev = NULL;
10347 char fn[80];
10348 struct dahdi_bufferinfo bi;
10349
10350 int res;
10351 int span = 0;
10352 int here = 0;
10353 int x;
10354 struct dahdi_pvt **wlist;
10355 struct dahdi_pvt **wend;
10356 struct dahdi_params p;
10357
10358 wlist = &iflist;
10359 wend = &ifend;
10360
10361 #ifdef HAVE_PRI
10362 if (pri) {
10363 wlist = &pri->crvs;
10364 wend = &pri->crvend;
10365 }
10366 #endif
10367
10368 tmp2 = *wlist;
10369 prev = NULL;
10370
10371 while (tmp2) {
10372 if (!tmp2->destroy) {
10373 if (tmp2->channel == channel) {
10374 tmp = tmp2;
10375 here = 1;
10376 break;
10377 }
10378 if (tmp2->channel > channel) {
10379 break;
10380 }
10381 }
10382 prev = tmp2;
10383 tmp2 = tmp2->next;
10384 }
10385
10386 if (!here && reloading != 1) {
10387 if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
10388 if (tmp)
10389 free(tmp);
10390 return NULL;
10391 }
10392 ast_mutex_init(&tmp->lock);
10393 ifcount++;
10394 for (x = 0; x < 3; x++)
10395 tmp->subs[x].dfd = -1;
10396 tmp->channel = channel;
10397 tmp->priindication_oob = conf->chan.priindication_oob;
10398 }
10399
10400 if (tmp) {
10401 int chan_sig = conf->chan.sig;
10402 if (!here) {
10403 if ((channel != CHAN_PSEUDO) && !pri) {
10404 int count = 0;
10405 snprintf(fn, sizeof(fn), "%d", channel);
10406
10407 tmp->subs[SUB_REAL].dfd = dahdi_open(fn);
10408 while (tmp->subs[SUB_REAL].dfd < 0 && reloading == 2 && count < 1000) {
10409 usleep(1);
10410 tmp->subs[SUB_REAL].dfd = dahdi_open(fn);
10411 count++;
10412 }
10413
10414 if (tmp->subs[SUB_REAL].dfd < 0) {
10415 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);
10416 destroy_dahdi_pvt(&tmp);
10417 return NULL;
10418 }
10419 memset(&p, 0, sizeof(p));
10420 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p);
10421 if (res < 0) {
10422 ast_log(LOG_ERROR, "Unable to get parameters: %s\n", strerror(errno));
10423 destroy_dahdi_pvt(&tmp);
10424 return NULL;
10425 }
10426 if (conf->is_sig_auto)
10427 chan_sig = sigtype_to_signalling(p.sigtype);
10428 if (p.sigtype != (chan_sig & 0x3ffff)) {
10429 ast_log(LOG_ERROR, "Signalling requested on channel %d is %s but line is in %s signalling\n", channel, sig2str(chan_sig), sig2str(p.sigtype));
10430 destroy_dahdi_pvt(&tmp);
10431 return NULL;
10432 }
10433 tmp->law = p.curlaw;
10434 tmp->span = p.spanno;
10435 span = p.spanno - 1;
10436 } else {
10437 if (channel == CHAN_PSEUDO)
10438 chan_sig = 0;
10439 else if ((chan_sig != SIG_FXOKS) && (chan_sig != SIG_FXSKS)) {
10440 ast_log(LOG_ERROR, "CRV's must use FXO/FXS Kewl Start (fxo_ks/fxs_ks) signalling only.\n");
10441 return NULL;
10442 }
10443 }
10444 tmp->outsigmod = conf->chan.outsigmod;
10445
10446 #ifdef HAVE_SS7
10447 if (chan_sig == SIG_SS7) {
10448 struct dahdi_ss7 *ss7;
10449 int clear = 0;
10450 if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &clear)) {
10451 ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
10452 destroy_dahdi_pvt(&tmp);
10453 return NULL;
10454 }
10455
10456 ss7 = ss7_resolve_linkset(cur_linkset);
10457 if (!ss7) {
10458 ast_log(LOG_ERROR, "Unable to find linkset %d\n", cur_linkset);
10459 destroy_dahdi_pvt(&tmp);
10460 return NULL;
10461 }
10462 if (cur_cicbeginswith < 0) {
10463 ast_log(LOG_ERROR, "Need to set cicbeginswith for the channels!\n");
10464 destroy_dahdi_pvt(&tmp);
10465 return NULL;
10466 }
10467
10468 tmp->cic = cur_cicbeginswith++;
10469
10470
10471 tmp->dpc = cur_defaultdpc;
10472
10473 tmp->ss7 = ss7;
10474 tmp->ss7call = NULL;
10475 ss7->pvts[ss7->numchans++] = tmp;
10476
10477 ast_copy_string(ss7->internationalprefix, conf->ss7.internationalprefix, sizeof(ss7->internationalprefix));
10478 ast_copy_string(ss7->nationalprefix, conf->ss7.nationalprefix, sizeof(ss7->nationalprefix));
10479 ast_copy_string(ss7->subscriberprefix, conf->ss7.subscriberprefix, sizeof(ss7->subscriberprefix));
10480 ast_copy_string(ss7->unknownprefix, conf->ss7.unknownprefix, sizeof(ss7->unknownprefix));
10481
10482 ss7->called_nai = conf->ss7.called_nai;
10483 ss7->calling_nai = conf->ss7.calling_nai;
10484 }
10485 #endif
10486 #ifdef HAVE_OPENR2
10487 if (chan_sig == SIG_MFCR2 && reloading != 1) {
10488 struct dahdi_mfcr2 *r2_link;
10489 r2_link = dahdi_r2_get_link();
10490 if (!r2_link) {
10491 ast_log(LOG_WARNING, "Cannot get another R2 DAHDI context!\n");
10492 destroy_dahdi_pvt(&tmp);
10493 return NULL;
10494 }
10495 if (!r2_link->protocol_context && dahdi_r2_set_context(r2_link, conf)) {
10496 ast_log(LOG_ERROR, "Cannot create OpenR2 protocol context.\n");
10497 destroy_dahdi_pvt(&tmp);
10498 return NULL;
10499 }
10500 if (r2_link->numchans == (sizeof(r2_link->pvts)/sizeof(r2_link->pvts[0]))) {
10501 ast_log(LOG_ERROR, "Cannot add more channels to this link!\n");
10502 destroy_dahdi_pvt(&tmp);
10503 return NULL;
10504 }
10505 r2_link->pvts[r2_link->numchans++] = tmp;
10506 tmp->r2chan = openr2_chan_new_from_fd(r2_link->protocol_context,
10507 tmp->subs[SUB_REAL].dfd,
10508 NULL, NULL);
10509 if (!tmp->r2chan) {
10510 openr2_liberr_t err = openr2_context_get_last_error(r2_link->protocol_context);
10511 ast_log(LOG_ERROR, "Cannot create OpenR2 channel: %s\n", openr2_context_error_string(err));
10512 destroy_dahdi_pvt(&tmp);
10513 return NULL;
10514 }
10515 tmp->mfcr2 = r2_link;
10516 if (conf->mfcr2.call_files) {
10517 openr2_chan_enable_call_files(tmp->r2chan);
10518 }
10519 openr2_chan_set_client_data(tmp->r2chan, tmp);
10520
10521 openr2_chan_set_logging_func(tmp->r2chan, (openr2_logging_func_t)dahdi_r2_on_chan_log);
10522 openr2_chan_set_log_level(tmp->r2chan, conf->mfcr2.loglevel);
10523 tmp->mfcr2_category = conf->mfcr2.category;
10524 tmp->mfcr2_charge_calls = conf->mfcr2.charge_calls;
10525 tmp->mfcr2_allow_collect_calls = conf->mfcr2.allow_collect_calls;
10526 tmp->mfcr2_forced_release = conf->mfcr2.forced_release;
10527 tmp->mfcr2_accept_on_offer = conf->mfcr2.accept_on_offer;
10528 tmp->mfcr2call = 0;
10529 tmp->mfcr2_dnis_index = 0;
10530 tmp->mfcr2_ani_index = 0;
10531 r2_link->monitored_count++;
10532 }
10533 #endif
10534 #ifdef HAVE_PRI
10535 if ((chan_sig == SIG_PRI) || (chan_sig == SIG_BRI) || (chan_sig == SIG_BRI_PTMP) || (chan_sig == SIG_GR303FXOKS) || (chan_sig == SIG_GR303FXSKS)) {
10536 int offset;
10537 int myswitchtype;
10538 int matchesdchan;
10539 int x,y;
10540 offset = 0;
10541 if (((chan_sig == SIG_PRI) || (chan_sig == SIG_BRI) || (chan_sig == SIG_BRI_PTMP))
10542 && ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &offset)) {
10543 ast_log(LOG_ERROR, "Unable to set clear mode on clear channel %d of span %d: %s\n", channel, p.spanno, strerror(errno));
10544 destroy_dahdi_pvt(&tmp);
10545 return NULL;
10546 }
10547 if (span >= NUM_SPANS) {
10548 ast_log(LOG_ERROR, "Channel %d does not lie on a span I know of (%d)\n", channel, span);
10549 destroy_dahdi_pvt(&tmp);
10550 return NULL;
10551 } else {
10552 struct dahdi_spaninfo si;
10553 si.spanno = 0;
10554 if (ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SPANSTAT,&si) == -1) {
10555 ast_log(LOG_ERROR, "Unable to get span status: %s\n", strerror(errno));
10556 destroy_dahdi_pvt(&tmp);
10557 return NULL;
10558 }
10559
10560 tmp->logicalspan = pris[span].prilogicalspan;
10561 pri_resolve_span(&span, channel, (channel - p.chanpos), &si);
10562 if (span < 0) {
10563 ast_log(LOG_WARNING, "Channel %d: Unable to find locate channel/trunk group!\n", channel);
10564 destroy_dahdi_pvt(&tmp);
10565 return NULL;
10566 }
10567 if ((chan_sig == SIG_PRI) ||
10568 (chan_sig == SIG_BRI) ||
10569 (chan_sig == SIG_BRI_PTMP))
10570 myswitchtype = conf->pri.switchtype;
10571 else
10572 myswitchtype = PRI_SWITCH_GR303_TMC;
10573
10574 matchesdchan=0;
10575 for (x = 0; x < NUM_SPANS; x++) {
10576 for (y = 0; y < NUM_DCHANS; y++) {
10577 if (pris[x].dchannels[y] == tmp->channel) {
10578 matchesdchan = 1;
10579 break;
10580 }
10581 }
10582 }
10583 offset = p.chanpos;
10584 if (!matchesdchan) {
10585 if (pris[span].nodetype && (pris[span].nodetype != conf->pri.nodetype)) {
10586 ast_log(LOG_ERROR, "Span %d is already a %s node\n", span + 1, pri_node2str(pris[span].nodetype));
10587 destroy_dahdi_pvt(&tmp);
10588 return NULL;
10589 }
10590 if (pris[span].switchtype && (pris[span].switchtype != myswitchtype)) {
10591 ast_log(LOG_ERROR, "Span %d is already a %s switch\n", span + 1, pri_switch2str(pris[span].switchtype));
10592 destroy_dahdi_pvt(&tmp);
10593 return NULL;
10594 }
10595 if ((pris[span].dialplan) && (pris[span].dialplan != conf->pri.dialplan)) {
10596 ast_log(LOG_ERROR, "Span %d is already a %s dialing plan\n", span + 1, dialplan2str(pris[span].dialplan));
10597 destroy_dahdi_pvt(&tmp);
10598 return NULL;
10599 }
10600 if (!ast_strlen_zero(pris[span].idledial) && strcmp(pris[span].idledial, conf->pri.idledial)) {
10601 ast_log(LOG_ERROR, "Span %d already has idledial '%s'.\n", span + 1, conf->pri.idledial);
10602 destroy_dahdi_pvt(&tmp);
10603 return NULL;
10604 }
10605 if (!ast_strlen_zero(pris[span].idleext) && strcmp(pris[span].idleext, conf->pri.idleext)) {
10606 ast_log(LOG_ERROR, "Span %d already has idleext '%s'.\n", span + 1, conf->pri.idleext);
10607 destroy_dahdi_pvt(&tmp);
10608 return NULL;
10609 }
10610 if (pris[span].minunused && (pris[span].minunused != conf->pri.minunused)) {
10611 ast_log(LOG_ERROR, "Span %d already has minunused of %d.\n", span + 1, conf->pri.minunused);
10612 destroy_dahdi_pvt(&tmp);
10613 return NULL;
10614 }
10615 if (pris[span].minidle && (pris[span].minidle != conf->pri.minidle)) {
10616 ast_log(LOG_ERROR, "Span %d already has minidle of %d.\n", span + 1, conf->pri.minidle);
10617 destroy_dahdi_pvt(&tmp);
10618 return NULL;
10619 }
10620 if (pris[span].numchans >= MAX_CHANNELS) {
10621 ast_log(LOG_ERROR, "Unable to add channel %d: Too many channels in trunk group %d!\n", channel,
10622 pris[span].trunkgroup);
10623 destroy_dahdi_pvt(&tmp);
10624 return NULL;
10625 }
10626
10627 pris[span].sig = chan_sig;
10628 pris[span].nodetype = conf->pri.nodetype;
10629 pris[span].switchtype = myswitchtype;
10630 pris[span].nsf = conf->pri.nsf;
10631 pris[span].dialplan = conf->pri.dialplan;
10632 pris[span].localdialplan = conf->pri.localdialplan;
10633 pris[span].pvts[pris[span].numchans++] = tmp;
10634 pris[span].minunused = conf->pri.minunused;
10635 pris[span].minidle = conf->pri.minidle;
10636 pris[span].overlapdial = conf->pri.overlapdial;
10637 pris[span].qsigchannelmapping = conf->pri.qsigchannelmapping;
10638 pris[span].discardremoteholdretrieval = conf->pri.discardremoteholdretrieval;
10639 #ifdef HAVE_PRI_INBANDDISCONNECT
10640 pris[span].inbanddisconnect = conf->pri.inbanddisconnect;
10641 #endif
10642 pris[span].facilityenable = conf->pri.facilityenable;
10643 ast_copy_string(pris[span].idledial, conf->pri.idledial, sizeof(pris[span].idledial));
10644 ast_copy_string(pris[span].idleext, conf->pri.idleext, sizeof(pris[span].idleext));
10645 ast_copy_string(pris[span].internationalprefix, conf->pri.internationalprefix, sizeof(pris[span].internationalprefix));
10646 ast_copy_string(pris[span].nationalprefix, conf->pri.nationalprefix, sizeof(pris[span].nationalprefix));
10647 ast_copy_string(pris[span].localprefix, conf->pri.localprefix, sizeof(pris[span].localprefix));
10648 ast_copy_string(pris[span].privateprefix, conf->pri.privateprefix, sizeof(pris[span].privateprefix));
10649 ast_copy_string(pris[span].unknownprefix, conf->pri.unknownprefix, sizeof(pris[span].unknownprefix));
10650 pris[span].resetinterval = conf->pri.resetinterval;
10651
10652 tmp->pri = &pris[span];
10653 tmp->prioffset = offset;
10654 tmp->call = NULL;
10655
10656 tmp->priexclusive = conf->chan.priexclusive;
10657 } else {
10658 ast_log(LOG_ERROR, "Channel %d is reserved for D-channel.\n", offset);
10659 destroy_dahdi_pvt(&tmp);
10660 return NULL;
10661 }
10662 }
10663 } else {
10664 tmp->prioffset = 0;
10665 }
10666 #endif
10667 } else {
10668 chan_sig = tmp->sig;
10669 if (tmp->subs[SUB_REAL].dfd > -1) {
10670 memset(&p, 0, sizeof(p));
10671 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p);
10672 }
10673 }
10674
10675 switch (chan_sig) {
10676 case SIG_FXSKS:
10677 case SIG_FXSLS:
10678 case SIG_EM:
10679 case SIG_EM_E1:
10680 case SIG_EMWINK:
10681 case SIG_FEATD:
10682 case SIG_FEATDMF:
10683 case SIG_FEATDMF_TA:
10684 case SIG_FEATB:
10685 case SIG_E911:
10686 case SIG_SF:
10687 case SIG_SFWINK:
10688 case SIG_FGC_CAMA:
10689 case SIG_FGC_CAMAMF:
10690 case SIG_SF_FEATD:
10691 case SIG_SF_FEATDMF:
10692 case SIG_SF_FEATB:
10693 p.starttime = 250;
10694 break;
10695 }
10696
10697 if (tmp->radio) {
10698
10699 p.channo = channel;
10700 p.rxwinktime = 1;
10701 p.rxflashtime = 1;
10702 p.starttime = 1;
10703 p.debouncetime = 5;
10704 }
10705 if (!tmp->radio) {
10706 p.channo = channel;
10707
10708 if (conf->timing.prewinktime >= 0)
10709 p.prewinktime = conf->timing.prewinktime;
10710 if (conf->timing.preflashtime >= 0)
10711 p.preflashtime = conf->timing.preflashtime;
10712 if (conf->timing.winktime >= 0)
10713 p.winktime = conf->timing.winktime;
10714 if (conf->timing.flashtime >= 0)
10715 p.flashtime = conf->timing.flashtime;
10716 if (conf->timing.starttime >= 0)
10717 p.starttime = conf->timing.starttime;
10718 if (conf->timing.rxwinktime >= 0)
10719 p.rxwinktime = conf->timing.rxwinktime;
10720 if (conf->timing.rxflashtime >= 0)
10721 p.rxflashtime = conf->timing.rxflashtime;
10722 if (conf->timing.debouncetime >= 0)
10723 p.debouncetime = conf->timing.debouncetime;
10724 }
10725
10726
10727 if (tmp->subs[SUB_REAL].dfd >= 0)
10728 {
10729 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_PARAMS, &p);
10730 if (res < 0) {
10731 ast_log(LOG_ERROR, "Unable to set parameters: %s\n", strerror(errno));
10732 destroy_dahdi_pvt(&tmp);
10733 return NULL;
10734 }
10735 }
10736 #if 1
10737 if (!here && (tmp->subs[SUB_REAL].dfd > -1)) {
10738 memset(&bi, 0, sizeof(bi));
10739 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi);
10740 if (!res) {
10741 bi.txbufpolicy = conf->chan.buf_policy;
10742 bi.rxbufpolicy = conf->chan.buf_policy;
10743 bi.numbufs = conf->chan.buf_no;
10744 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi);
10745 if (res < 0) {
10746 ast_log(LOG_WARNING, "Unable to set buffer policy on channel %d: %s\n", channel, strerror(errno));
10747 }
10748 } else {
10749 ast_log(LOG_WARNING, "Unable to check buffer policy on channel %d: %s\n", channel, strerror(errno));
10750 }
10751 tmp->buf_policy = conf->chan.buf_policy;
10752 tmp->buf_no = conf->chan.buf_no;
10753 tmp->usefaxbuffers = conf->chan.usefaxbuffers;
10754 tmp->faxbuf_policy = conf->chan.faxbuf_policy;
10755 tmp->faxbuf_no = conf->chan.faxbuf_no;
10756
10757
10758
10759
10760 tmp->bufsize = bi.bufsize;
10761 }
10762 #endif
10763 tmp->immediate = conf->chan.immediate;
10764 tmp->transfertobusy = conf->chan.transfertobusy;
10765 if (chan_sig & __DAHDI_SIG_FXS) {
10766 tmp->mwimonitor_fsk = conf->chan.mwimonitor_fsk;
10767 tmp->mwimonitor_neon = conf->chan.mwimonitor_neon;
10768 tmp->mwimonitor_rpas = conf->chan.mwimonitor_rpas;
10769 }
10770 tmp->sig = chan_sig;
10771 tmp->ringt_base = ringt_base;
10772 tmp->firstradio = 0;
10773 if ((chan_sig == SIG_FXOKS) || (chan_sig == SIG_FXOLS) || (chan_sig == SIG_FXOGS))
10774 tmp->permcallwaiting = conf->chan.callwaiting;
10775 else
10776 tmp->permcallwaiting = 0;
10777
10778 tmp->destroy = 0;
10779 tmp->drings = conf->chan.drings;
10780
10781
10782 if (tmp->drings.ringnum[0].range == 0)
10783 tmp->drings.ringnum[0].range = 10;
10784 if (tmp->drings.ringnum[1].range == 0)
10785 tmp->drings.ringnum[1].range = 10;
10786 if (tmp->drings.ringnum[2].range == 0)
10787 tmp->drings.ringnum[2].range = 10;
10788
10789 tmp->usedistinctiveringdetection = usedistinctiveringdetection;
10790 tmp->callwaitingcallerid = conf->chan.callwaitingcallerid;
10791 tmp->threewaycalling = conf->chan.threewaycalling;
10792 tmp->adsi = conf->chan.adsi;
10793 tmp->use_smdi = conf->chan.use_smdi;
10794 tmp->permhidecallerid = conf->chan.hidecallerid;
10795 tmp->hidecalleridname = conf->chan.hidecalleridname;
10796 tmp->callreturn = conf->chan.callreturn;
10797 tmp->echocancel = conf->chan.echocancel;
10798 tmp->echotraining = conf->chan.echotraining;
10799 tmp->pulse = conf->chan.pulse;
10800 if (tmp->echocancel.head.tap_length) {
10801 tmp->echocanbridged = conf->chan.echocanbridged;
10802 } else {
10803 if (conf->chan.echocanbridged)
10804 ast_log(LOG_NOTICE, "echocancelwhenbridged requires echocancel to be enabled; ignoring\n");
10805 tmp->echocanbridged = 0;
10806 }
10807 tmp->busydetect = conf->chan.busydetect;
10808 tmp->busycount = conf->chan.busycount;
10809 tmp->busy_tonelength = conf->chan.busy_tonelength;
10810 tmp->busy_quietlength = conf->chan.busy_quietlength;
10811 tmp->callprogress = conf->chan.callprogress;
10812 tmp->waitfordialtone = conf->chan.waitfordialtone;
10813 tmp->cancallforward = conf->chan.cancallforward;
10814 tmp->dtmfrelax = conf->chan.dtmfrelax;
10815 tmp->callwaiting = tmp->permcallwaiting;
10816 tmp->hidecallerid = tmp->permhidecallerid;
10817 tmp->channel = channel;
10818 tmp->stripmsd = conf->chan.stripmsd;
10819 tmp->use_callerid = conf->chan.use_callerid;
10820 tmp->cid_signalling = conf->chan.cid_signalling;
10821 tmp->cid_start = conf->chan.cid_start;
10822 tmp->dahditrcallerid = conf->chan.dahditrcallerid;
10823 tmp->restrictcid = conf->chan.restrictcid;
10824 tmp->use_callingpres = conf->chan.use_callingpres;
10825 if (tmp->usedistinctiveringdetection) {
10826 if (!tmp->use_callerid) {
10827 ast_log(LOG_NOTICE, "Distinctive Ring detect requires 'usecallerid' be on\n");
10828 tmp->use_callerid = 1;
10829 }
10830 }
10831
10832 if (tmp->cid_signalling == CID_SIG_SMDI) {
10833 if (!tmp->use_smdi) {
10834 ast_log(LOG_WARNING, "SMDI callerid requires SMDI to be enabled, enabling...\n");
10835 tmp->use_smdi = 1;
10836 }
10837 }
10838 if (tmp->use_smdi) {
10839 tmp->smdi_iface = ast_smdi_interface_find(conf->smdi_port);
10840 if (!(tmp->smdi_iface)) {
10841 ast_log(LOG_ERROR, "Invalid SMDI port specfied, disabling SMDI support\n");
10842 tmp->use_smdi = 0;
10843 }
10844 }
10845
10846 ast_copy_string(tmp->accountcode, conf->chan.accountcode, sizeof(tmp->accountcode));
10847 tmp->amaflags = conf->chan.amaflags;
10848 if (!here) {
10849 tmp->confno = -1;
10850 tmp->propconfno = -1;
10851 }
10852 tmp->canpark = conf->chan.canpark;
10853 tmp->transfer = conf->chan.transfer;
10854 ast_copy_string(tmp->defcontext,conf->chan.context,sizeof(tmp->defcontext));
10855 ast_copy_string(tmp->language, conf->chan.language, sizeof(tmp->language));
10856 ast_copy_string(tmp->mohinterpret, conf->chan.mohinterpret, sizeof(tmp->mohinterpret));
10857 ast_copy_string(tmp->mohsuggest, conf->chan.mohsuggest, sizeof(tmp->mohsuggest));
10858 ast_copy_string(tmp->context, conf->chan.context, sizeof(tmp->context));
10859 ast_copy_string(tmp->parkinglot, conf->chan.parkinglot, sizeof(tmp->parkinglot));
10860 tmp->cid_ton = 0;
10861 switch (tmp->sig) {
10862 case SIG_PRI:
10863 case SIG_BRI:
10864 case SIG_BRI_PTMP:
10865 case SIG_SS7:
10866 case SIG_MFCR2:
10867 tmp->cid_num[0] = '\0';
10868 tmp->cid_name[0] = '\0';
10869 break;
10870 default:
10871 ast_copy_string(tmp->cid_num, conf->chan.cid_num, sizeof(tmp->cid_num));
10872 ast_copy_string(tmp->cid_name, conf->chan.cid_name, sizeof(tmp->cid_name));
10873 break;
10874 }
10875 ast_copy_string(tmp->mailbox, conf->chan.mailbox, sizeof(tmp->mailbox));
10876 if (channel != CHAN_PSEUDO && !ast_strlen_zero(tmp->mailbox)) {
10877 char *mailbox, *context;
10878 mailbox = context = ast_strdupa(tmp->mailbox);
10879 strsep(&context, "@");
10880 if (ast_strlen_zero(context))
10881 context = "default";
10882 tmp->mwi_event_sub = ast_event_subscribe(AST_EVENT_MWI, mwi_event_cb, NULL,
10883 AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, mailbox,
10884 AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, context,
10885 AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_EXISTS,
10886 AST_EVENT_IE_END);
10887 }
10888 tmp->msgstate = -1;
10889 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
10890 tmp->mwisend_setting = conf->chan.mwisend_setting;
10891 tmp->mwisend_fsk = conf->chan.mwisend_fsk;
10892 tmp->mwisend_rpas = conf->chan.mwisend_rpas;
10893 #endif
10894 if (chan_sig & __DAHDI_SIG_FXO) {
10895 memset(&p, 0, sizeof(p));
10896 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &p);
10897 if (!res) {
10898 tmp->fxsoffhookstate = p.rxisoffhook;
10899 }
10900 #ifdef HAVE_DAHDI_LINEREVERSE_VMWI
10901 res = ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_VMWI_CONFIG, &tmp->mwisend_setting);
10902 #endif
10903 }
10904 tmp->onhooktime = time(NULL);
10905 tmp->group = conf->chan.group;
10906 tmp->callgroup = conf->chan.callgroup;
10907 tmp->pickupgroup= conf->chan.pickupgroup;
10908 if (conf->chan.vars) {
10909 struct ast_variable *v, *tmpvar;
10910 for (v = conf->chan.vars ; v ; v = v->next) {
10911 if ((tmpvar = ast_variable_new(v->name, v->value, v->file))) {
10912 tmpvar->next = tmp->vars;
10913 tmp->vars = tmpvar;
10914 }
10915 }
10916 }
10917 tmp->cid_rxgain = conf->chan.cid_rxgain;
10918 tmp->rxgain = conf->chan.rxgain;
10919 tmp->txgain = conf->chan.txgain;
10920 tmp->tonezone = conf->chan.tonezone;
10921 if (tmp->subs[SUB_REAL].dfd > -1) {
10922 set_actual_gain(tmp->subs[SUB_REAL].dfd, 0, tmp->rxgain, tmp->txgain, tmp->law);
10923 if (tmp->dsp)
10924 ast_dsp_set_digitmode(tmp->dsp, DSP_DIGITMODE_DTMF | tmp->dtmfrelax);
10925 update_conf(tmp);
10926 if (!here) {
10927 if ((chan_sig != SIG_BRI) && (chan_sig != SIG_BRI_PTMP) && (chan_sig != SIG_PRI)
10928 && (chan_sig != SIG_SS7) && (chan_sig != SIG_MFCR2))
10929
10930 dahdi_set_hook(tmp->subs[SUB_REAL].dfd, DAHDI_ONHOOK);
10931 }
10932 ioctl(tmp->subs[SUB_REAL].dfd,DAHDI_SETTONEZONE,&tmp->tonezone);
10933 #ifdef HAVE_PRI
10934
10935 if (tmp->pri && !pri_is_up(tmp->pri))
10936 tmp->inalarm = 1;
10937 #endif
10938 if ((res = get_alarms(tmp)) != DAHDI_ALARM_NONE) {
10939 tmp->inalarm = 1;
10940 handle_alarms(tmp, res);
10941 }
10942 }
10943
10944 tmp->polarityonanswerdelay = conf->chan.polarityonanswerdelay;
10945 tmp->answeronpolarityswitch = conf->chan.answeronpolarityswitch;
10946 tmp->hanguponpolarityswitch = conf->chan.hanguponpolarityswitch;
10947 tmp->sendcalleridafter = conf->chan.sendcalleridafter;
10948 if (!here) {
10949 tmp->locallyblocked = tmp->remotelyblocked = 0;
10950 if ((chan_sig == SIG_PRI) || (chan_sig == SIG_BRI) || (chan_sig == SIG_BRI_PTMP) || (chan_sig == SIG_SS7))
10951 tmp->inservice = 0;
10952 else
10953 tmp->inservice = 1;
10954 }
10955 }
10956 if (tmp && !here) {
10957
10958 if (!*wlist) {
10959 *wlist = tmp;
10960 tmp->prev = NULL;
10961 tmp->next = NULL;
10962 *wend = tmp;
10963 } else {
10964
10965 struct dahdi_pvt *working = *wlist;
10966
10967
10968 if (working->channel > tmp->channel) {
10969 tmp->next = *wlist;
10970 tmp->prev = NULL;
10971 (*wlist)->prev = tmp;
10972 *wlist = tmp;
10973 } else {
10974
10975 while (working) {
10976
10977 if (working->next) {
10978 if (working->channel < tmp->channel && working->next->channel > tmp->channel) {
10979 tmp->next = working->next;
10980 tmp->prev = working;
10981 working->next->prev = tmp;
10982 working->next = tmp;
10983 break;
10984 }
10985 } else {
10986
10987 if (working->channel < tmp->channel) {
10988 working->next = tmp;
10989 tmp->next = NULL;
10990 tmp->prev = working;
10991 *wend = tmp;
10992 break;
10993 }
10994 }
10995 working = working->next;
10996 }
10997 }
10998 }
10999 }
11000 return tmp;
11001 }
11002
11003 static inline int available(struct dahdi_pvt *p, int channelmatch, ast_group_t groupmatch, int *busy, int *channelmatched, int *groupmatched)
11004 {
11005 int res;
11006 struct dahdi_params par;
11007
11008
11009 if (groupmatch) {
11010 if ((p->group & groupmatch) != groupmatch)
11011 return 0;
11012 *groupmatched = 1;
11013 }
11014
11015 if (channelmatch != -1) {
11016 if (p->channel != channelmatch)
11017 return 0;
11018 *channelmatched = 1;
11019 }
11020
11021 if (busy) {
11022 if ((p->sig == SIG_FXOKS) || (p->sig == SIG_FXOLS) || (p->sig == SIG_FXOGS))
11023 *busy = 1;
11024 }
11025
11026 if (p->dnd)
11027 return 0;
11028
11029 if (p->guardtime && (time(NULL) < p->guardtime))
11030 return 0;
11031
11032 if (p->locallyblocked || p->remotelyblocked)
11033 return 0;
11034
11035
11036 if (!p->owner) {
11037 #ifdef HAVE_PRI
11038
11039 if (p->pri) {
11040 if (p->resetting || p->call)
11041 return 0;
11042 else
11043 return 1;
11044 }
11045 #endif
11046 #ifdef HAVE_SS7
11047
11048 if (p->ss7) {
11049 if (p->ss7call)
11050 return 0;
11051 else
11052 return 1;
11053 }
11054 #endif
11055 #ifdef HAVE_OPENR2
11056
11057 if (p->mfcr2) {
11058 if (p->mfcr2call)
11059 return 0;
11060 else
11061 return 1;
11062 }
11063 #endif
11064
11065
11066 if (p->sig && !(p->radio || (p->oprmode < 0)))
11067 {
11068
11069 if (p->subs[SUB_REAL].dfd > -1) {
11070 memset(&par, 0, sizeof(par));
11071 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &par);
11072 } else {
11073
11074 res = 0;
11075 par.rxisoffhook = 0;
11076 }
11077
11078 if (res) {
11079 ast_log(LOG_WARNING, "Unable to check hook state on channel %d: %s\n", p->channel, strerror(errno));
11080 }
11081 else if ((p->sig != SIG_FXSKS) && (p->sig != SIG_FXSGS) && (p->sig != SIG_FXSLS)) {
11082 if (par.rxisoffhook) {
11083 ast_debug(1, "Channel %d off hook, can't use\n", p->channel);
11084
11085 return 0;
11086 }
11087 }
11088 #ifdef DAHDI_CHECK_HOOKSTATE
11089 } else {
11090
11091 if (par.rxbits > -1)
11092 return 1;
11093
11094 if (par.rxisoffhook)
11095 return 1;
11096 else
11097 return 0;
11098 #endif
11099 }
11100
11101 return 1;
11102 }
11103
11104
11105 if ((p->sig != SIG_FXOKS) && (p->sig != SIG_FXOLS) && (p->sig != SIG_FXOGS))
11106 return 0;
11107
11108 if (!p->callwaiting) {
11109
11110 return 0;
11111 }
11112
11113 if (p->subs[SUB_CALLWAIT].dfd > -1) {
11114
11115 return 0;
11116 }
11117
11118 if ((p->owner->_state != AST_STATE_UP) &&
11119 ((p->owner->_state != AST_STATE_RINGING) || p->outgoing)) {
11120
11121 return 0;
11122 }
11123 if ((p->subs[SUB_THREEWAY].owner) && (!p->subs[SUB_THREEWAY].inthreeway)) {
11124
11125 return 0;
11126 }
11127
11128 return 1;
11129 }
11130
11131
11132
11133
11134
11135
11136 static struct dahdi_pvt *duplicate_pseudo(struct dahdi_pvt *src)
11137 {
11138 struct dahdi_pvt *p;
11139 struct dahdi_bufferinfo bi;
11140 int res;
11141
11142 if ((p = ast_malloc(sizeof(*p)))) {
11143 memcpy(p, src, sizeof(struct dahdi_pvt));
11144 ast_mutex_init(&p->lock);
11145 p->subs[SUB_REAL].dfd = dahdi_open("/dev/dahdi/pseudo");
11146 if (p->subs[SUB_REAL].dfd < 0) {
11147 ast_log(LOG_ERROR, "Unable to dup channel: %s\n", strerror(errno));
11148 destroy_dahdi_pvt(&p);
11149 return NULL;
11150 }
11151 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_GET_BUFINFO, &bi);
11152 if (!res) {
11153 bi.txbufpolicy = src->buf_policy;
11154 bi.rxbufpolicy = src->buf_policy;
11155 bi.numbufs = src->buf_no;
11156 res = ioctl(p->subs[SUB_REAL].dfd, DAHDI_SET_BUFINFO, &bi);
11157 if (res < 0) {
11158 ast_log(LOG_WARNING, "Unable to set buffer policy on dup channel: %s\n", strerror(errno));
11159 }
11160 } else
11161 ast_log(LOG_WARNING, "Unable to check buffer policy on dup channel: %s\n", strerror(errno));
11162 }
11163 p->destroy = 1;
11164 p->next = iflist;
11165 p->prev = NULL;
11166 iflist = p;
11167 if (iflist->next)
11168 iflist->next->prev = p;
11169 return p;
11170 }
11171
11172 #if defined(HAVE_PRI)
11173 static int pri_find_empty_chan(struct dahdi_pri *pri, int backwards)
11174 {
11175 int x;
11176 if (backwards)
11177 x = pri->numchans;
11178 else
11179 x = 0;
11180 for (;;) {
11181 if (backwards && (x < 0))
11182 break;
11183 if (!backwards && (x >= pri->numchans))
11184 break;
11185 if (pri->pvts[x] && !pri->pvts[x]->inalarm && !pri->pvts[x]->owner) {
11186 ast_debug(1, "Found empty available channel %d/%d\n",
11187 pri->pvts[x]->logicalspan, pri->pvts[x]->prioffset);
11188 return x;
11189 }
11190 if (backwards)
11191 x--;
11192 else
11193 x++;
11194 }
11195 return -1;
11196 }
11197 #endif
11198
11199 static struct ast_channel *dahdi_request(const char *type, int format, void *data, int *cause)
11200 {
11201 ast_group_t groupmatch = 0;
11202 int channelmatch = -1;
11203 int roundrobin = 0;
11204 int callwait = 0;
11205 int busy = 0;
11206 struct dahdi_pvt *p;
11207 struct ast_channel *tmp = NULL;
11208 char *dest=NULL;
11209 int x;
11210 char *s;
11211 char opt=0;
11212 int res=0, y=0;
11213 int backwards = 0;
11214 #ifdef HAVE_PRI
11215 int crv;
11216 int bearer = -1;
11217 int trunkgroup;
11218 struct dahdi_pri *pri=NULL;
11219 #endif
11220 struct dahdi_pvt *exitpvt, *start, *end;
11221 ast_mutex_t *lock;
11222 int channelmatched = 0;
11223 int groupmatched = 0;
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243 lock = &iflock;
11244 start = iflist;
11245 end = ifend;
11246 if (data) {
11247 dest = ast_strdupa((char *)data);
11248 } else {
11249 ast_log(LOG_WARNING, "Channel requested with no data\n");
11250 return NULL;
11251 }
11252 if (toupper(dest[0]) == 'G' || toupper(dest[0])=='R') {
11253
11254 char *stringp;
11255
11256 stringp = dest + 1;
11257 s = strsep(&stringp, "/");
11258 if ((res = sscanf(s, "%30d%1c%30d", &x, &opt, &y)) < 1) {
11259 ast_log(LOG_WARNING, "Unable to determine group for data %s\n", (char *)data);
11260 return NULL;
11261 }
11262 groupmatch = ((ast_group_t) 1 << x);
11263 if (toupper(dest[0]) == 'G') {
11264 if (dest[0] == 'G') {
11265 backwards = 1;
11266 p = ifend;
11267 } else
11268 p = iflist;
11269 } else {
11270 if (dest[0] == 'R') {
11271 backwards = 1;
11272 p = round_robin[x]?round_robin[x]->prev:ifend;
11273 if (!p)
11274 p = ifend;
11275 } else {
11276 p = round_robin[x]?round_robin[x]->next:iflist;
11277 if (!p)
11278 p = iflist;
11279 }
11280 roundrobin = 1;
11281 }
11282 } else {
11283 char *stringp;
11284
11285 stringp = dest;
11286 s = strsep(&stringp, "/");
11287 p = iflist;
11288 if (!strcasecmp(s, "pseudo")) {
11289
11290 x = CHAN_PSEUDO;
11291 channelmatch = x;
11292 }
11293 #ifdef HAVE_PRI
11294 else if ((res = sscanf(s, "%30d:%30d%1c%30d", &trunkgroup, &crv, &opt, &y)) > 1) {
11295 if ((trunkgroup < 1) || (crv < 1)) {
11296 ast_log(LOG_WARNING, "Unable to determine trunk group and CRV for data %s\n", (char *)data);
11297 return NULL;
11298 }
11299 res--;
11300 for (x = 0; x < NUM_SPANS; x++) {
11301 if (pris[x].trunkgroup == trunkgroup) {
11302 pri = pris + x;
11303 lock = &pri->lock;
11304 start = pri->crvs;
11305 end = pri->crvend;
11306 break;
11307 }
11308 }
11309 if (!pri) {
11310 ast_log(LOG_WARNING, "Unable to find trunk group %d\n", trunkgroup);
11311 return NULL;
11312 }
11313 channelmatch = crv;
11314 p = pris[x].crvs;
11315 }
11316 #endif
11317 else if ((res = sscanf(s, "%30d%1c%30d", &x, &opt, &y)) < 1) {
11318 ast_log(LOG_WARNING, "Unable to determine channel for data %s\n", (char *)data);
11319 return NULL;
11320 } else {
11321 channelmatch = x;
11322 }
11323 }
11324
11325 ast_mutex_lock(lock);
11326 exitpvt = p;
11327 while (p && !tmp) {
11328 if (roundrobin)
11329 round_robin[x] = p;
11330 #if 0
11331 ast_verbose("name = %s, %d, %d, %llu\n",p->owner ? p->owner->name : "<none>", p->channel, channelmatch, groupmatch);
11332 #endif
11333
11334 if (p && available(p, channelmatch, groupmatch, &busy, &channelmatched, &groupmatched)) {
11335 ast_debug(1, "Using channel %d\n", p->channel);
11336 if (p->inalarm)
11337 goto next;
11338
11339 callwait = (p->owner != NULL);
11340 #ifdef HAVE_PRI
11341 if (pri && (p->subs[SUB_REAL].dfd < 0)) {
11342 if (p->sig != SIG_FXSKS) {
11343
11344
11345 bearer = pri_find_empty_chan(pri, 0);
11346 if (bearer < 0) {
11347 ast_log(LOG_NOTICE, "Out of bearer channels on span %d for call to CRV %d:%d\n", pri->span, trunkgroup, crv);
11348 p = NULL;
11349 break;
11350 }
11351 pri_assign_bearer(p, pri, pri->pvts[bearer]);
11352 } else {
11353 if (alloc_sub(p, 0)) {
11354 ast_log(LOG_NOTICE, "Failed to allocate place holder pseudo channel!\n");
11355 p = NULL;
11356 break;
11357 } else
11358 ast_debug(1, "Allocated placeholder pseudo channel\n");
11359
11360 p->pri = pri;
11361 }
11362 }
11363 #endif
11364 #ifdef HAVE_OPENR2
11365 if (p->mfcr2) {
11366 ast_mutex_lock(&p->lock);
11367 if (p->mfcr2call) {
11368 ast_mutex_unlock(&p->lock);
11369 ast_log(LOG_DEBUG, "Yay!, someone just beat us in the race for channel %d.\n", p->channel);
11370 goto next;
11371 }
11372 p->mfcr2call = 1;
11373 ast_mutex_unlock(&p->lock);
11374 }
11375 #endif
11376 if (p->channel == CHAN_PSEUDO) {
11377 p = duplicate_pseudo(p);
11378 if (!p) {
11379 break;
11380 }
11381 }
11382 if (p->owner) {
11383 if (alloc_sub(p, SUB_CALLWAIT)) {
11384 p = NULL;
11385 break;
11386 }
11387 }
11388 p->outgoing = 1;
11389 tmp = dahdi_new(p, AST_STATE_RESERVED, 0, p->owner ? SUB_CALLWAIT : SUB_REAL, 0, 0);
11390 if (!tmp) {
11391 p->outgoing = 0;
11392 }
11393 #ifdef HAVE_PRI
11394 if (p->bearer) {
11395
11396 p->bearer->owner = tmp;
11397 }
11398 #endif
11399
11400 if (res > 1) {
11401 if (opt == 'c') {
11402
11403 p->confirmanswer = 1;
11404 } else if (opt == 'r') {
11405
11406 if (res < 3)
11407 ast_log(LOG_WARNING, "Distinctive ring missing identifier in '%s'\n", (char *)data);
11408 else
11409 p->distinctivering = y;
11410 } else if (opt == 'd') {
11411
11412 p->digital = 1;
11413 if (tmp)
11414 tmp->transfercapability = AST_TRANS_CAP_DIGITAL;
11415 } else {
11416 ast_log(LOG_WARNING, "Unknown option '%c' in '%s'\n", opt, (char *)data);
11417 }
11418 }
11419
11420 if (tmp && callwait)
11421 tmp->cdrflags |= AST_CDR_CALLWAIT;
11422 break;
11423 }
11424 next:
11425 if (backwards) {
11426 p = p->prev;
11427 if (!p)
11428 p = end;
11429 } else {
11430 p = p->next;
11431 if (!p)
11432 p = start;
11433 }
11434
11435 if (p == exitpvt)
11436 break;
11437 }
11438 ast_mutex_unlock(lock);
11439 restart_monitor();
11440 if (callwait)
11441 *cause = AST_CAUSE_BUSY;
11442 else if (!tmp) {
11443 if (channelmatched) {
11444 if (busy)
11445 *cause = AST_CAUSE_BUSY;
11446 } else if (groupmatched) {
11447 *cause = AST_CAUSE_CONGESTION;
11448 }
11449 }
11450
11451 return tmp;
11452 }
11453
11454 #if defined(HAVE_PRI) || defined(HAVE_SS7)
11455 static int dahdi_setlaw(int dfd, int law)
11456 {
11457 return ioctl(dfd, DAHDI_SETLAW, &law);
11458 }
11459 #endif
11460
11461 #if defined(HAVE_SS7)
11462 static int ss7_find_cic(struct dahdi_ss7 *linkset, int cic, unsigned int dpc)
11463 {
11464 int i;
11465 int winner = -1;
11466 for (i = 0; i < linkset->numchans; i++) {
11467 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && linkset->pvts[i]->cic == cic)) {
11468 winner = i;
11469 break;
11470 }
11471 }
11472 return winner;
11473 }
11474 #endif
11475
11476 #if defined(HAVE_SS7)
11477 static void ss7_handle_cqm(struct dahdi_ss7 *linkset, int startcic, int endcic, unsigned int dpc)
11478 {
11479 unsigned char status[32];
11480 struct dahdi_pvt *p = NULL;
11481 int i, offset;
11482
11483 for (i = 0; i < linkset->numchans; i++) {
11484 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
11485 p = linkset->pvts[i];
11486 offset = p->cic - startcic;
11487 status[offset] = 0;
11488 if (p->locallyblocked)
11489 status[offset] |= (1 << 0) | (1 << 4);
11490 if (p->remotelyblocked)
11491 status[offset] |= (1 << 1) | (1 << 5);
11492 if (p->ss7call) {
11493 if (p->outgoing)
11494 status[offset] |= (1 << 3);
11495 else
11496 status[offset] |= (1 << 2);
11497 } else
11498 status[offset] |= 0x3 << 2;
11499 }
11500 }
11501
11502 if (p)
11503 isup_cqr(linkset->ss7, startcic, endcic, dpc, status);
11504 else
11505 ast_log(LOG_WARNING, "Could not find any equipped circuits within CQM CICs\n");
11506
11507 }
11508 #endif
11509
11510 #if defined(HAVE_SS7)
11511 static inline void ss7_hangup_cics(struct dahdi_ss7 *linkset, int startcic, int endcic, unsigned int dpc)
11512 {
11513 int i;
11514
11515 for (i = 0; i < linkset->numchans; i++) {
11516 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
11517 ast_mutex_lock(&linkset->pvts[i]->lock);
11518 if (linkset->pvts[i]->owner)
11519 linkset->pvts[i]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
11520 ast_mutex_unlock(&linkset->pvts[i]->lock);
11521 }
11522 }
11523 }
11524 #endif
11525
11526 #if defined(HAVE_SS7)
11527 static inline void ss7_block_cics(struct dahdi_ss7 *linkset, int startcic, int endcic, unsigned int dpc, unsigned char state[], int block)
11528 {
11529 int i;
11530
11531 for (i = 0; i < linkset->numchans; i++) {
11532 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic)))) {
11533 if (state) {
11534 if (state[i])
11535 linkset->pvts[i]->remotelyblocked = block;
11536 } else
11537 linkset->pvts[i]->remotelyblocked = block;
11538 }
11539 }
11540 }
11541 #endif
11542
11543 #if defined(HAVE_SS7)
11544 static void ss7_inservice(struct dahdi_ss7 *linkset, int startcic, int endcic, unsigned int dpc)
11545 {
11546 int i;
11547
11548 for (i = 0; i < linkset->numchans; i++) {
11549 if (linkset->pvts[i] && (linkset->pvts[i]->dpc == dpc && ((linkset->pvts[i]->cic >= startcic) && (linkset->pvts[i]->cic <= endcic))))
11550 linkset->pvts[i]->inservice = 1;
11551 }
11552 }
11553 #endif
11554
11555 #if defined(HAVE_SS7)
11556 static void ss7_reset_linkset(struct dahdi_ss7 *linkset)
11557 {
11558 int i, startcic = -1, endcic, dpc;
11559
11560 if (linkset->numchans <= 0)
11561 return;
11562
11563 startcic = linkset->pvts[0]->cic;
11564
11565 dpc = linkset->pvts[0]->dpc;
11566
11567 for (i = 0; i < linkset->numchans; i++) {
11568 if (linkset->pvts[i+1] && linkset->pvts[i+1]->dpc == dpc && ((linkset->pvts[i+1]->cic - linkset->pvts[i]->cic) == 1) && (linkset->pvts[i]->cic - startcic < 31)) {
11569 continue;
11570 } else {
11571 endcic = linkset->pvts[i]->cic;
11572 ast_verbose("Resetting CICs %d to %d\n", startcic, endcic);
11573 isup_grs(linkset->ss7, startcic, endcic, dpc);
11574
11575
11576 if (linkset->pvts[i+1]) {
11577 startcic = linkset->pvts[i+1]->cic;
11578 dpc = linkset->pvts[i+1]->dpc;
11579 }
11580 }
11581 }
11582 }
11583 #endif
11584
11585 #if defined(HAVE_SS7)
11586 static void dahdi_loopback(struct dahdi_pvt *p, int enable)
11587 {
11588 if (p->loopedback != enable) {
11589 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_LOOPBACK, &enable)) {
11590 ast_log(LOG_WARNING, "Unable to set loopback on channel %d: %s\n", p->channel, strerror(errno));
11591 return;
11592 }
11593 p->loopedback = enable;
11594 }
11595 }
11596 #endif
11597
11598 #if defined(HAVE_SS7)
11599
11600 static void ss7_start_call(struct dahdi_pvt *p, struct dahdi_ss7 *linkset)
11601 {
11602 struct ss7 *ss7 = linkset->ss7;
11603 int res;
11604 int law = 1;
11605 struct ast_channel *c;
11606 char tmp[256];
11607
11608 if (ioctl(p->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &law) == -1)
11609 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", p->channel, law, strerror(errno));
11610
11611 if (linkset->type == SS7_ITU)
11612 law = DAHDI_LAW_ALAW;
11613 else
11614 law = DAHDI_LAW_MULAW;
11615
11616 res = dahdi_setlaw(p->subs[SUB_REAL].dfd, law);
11617 if (res < 0)
11618 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", p->channel);
11619
11620 if (!(linkset->flags & LINKSET_FLAG_EXPLICITACM)) {
11621 p->proceeding = 1;
11622 isup_acm(ss7, p->ss7call);
11623 }
11624
11625 ast_mutex_unlock(&linkset->lock);
11626 c = dahdi_new(p, AST_STATE_RING, 1, SUB_REAL, law, 0);
11627
11628 if (!c) {
11629 ast_log(LOG_WARNING, "Unable to start PBX on CIC %d\n", p->cic);
11630
11631 ast_mutex_lock(&linkset->lock);
11632 return;
11633 } else
11634 ast_verb(3, "Accepting call to '%s' on CIC %d\n", p->exten, p->cic);
11635
11636 dahdi_enable_ec(p);
11637
11638
11639
11640
11641
11642 ast_mutex_unlock(&p->lock);
11643
11644 if (!ast_strlen_zero(p->charge_number)) {
11645 pbx_builtin_setvar_helper(c, "SS7_CHARGE_NUMBER", p->charge_number);
11646
11647 p->charge_number[0] = 0;
11648 }
11649 if (!ast_strlen_zero(p->gen_add_number)) {
11650 pbx_builtin_setvar_helper(c, "SS7_GENERIC_ADDRESS", p->gen_add_number);
11651
11652 p->gen_add_number[0] = 0;
11653 }
11654 if (!ast_strlen_zero(p->jip_number)) {
11655 pbx_builtin_setvar_helper(c, "SS7_JIP", p->jip_number);
11656
11657 p->jip_number[0] = 0;
11658 }
11659 if (!ast_strlen_zero(p->gen_dig_number)) {
11660 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGITS", p->gen_dig_number);
11661
11662 p->gen_dig_number[0] = 0;
11663 }
11664 if (!ast_strlen_zero(p->orig_called_num)) {
11665 pbx_builtin_setvar_helper(c, "SS7_ORIG_CALLED_NUM", p->orig_called_num);
11666
11667 p->orig_called_num[0] = 0;
11668 }
11669
11670 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_type);
11671 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGTYPE", tmp);
11672
11673 p->gen_dig_type = 0;
11674
11675 snprintf(tmp, sizeof(tmp), "%d", p->gen_dig_scheme);
11676 pbx_builtin_setvar_helper(c, "SS7_GENERIC_DIGSCHEME", tmp);
11677
11678 p->gen_dig_scheme = 0;
11679
11680 if (!ast_strlen_zero(p->lspi_ident)) {
11681 pbx_builtin_setvar_helper(c, "SS7_LSPI_IDENT", p->lspi_ident);
11682
11683 p->lspi_ident[0] = 0;
11684 }
11685
11686 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_ident);
11687 pbx_builtin_setvar_helper(c, "SS7_CALLREF_IDENT", tmp);
11688
11689 p->call_ref_ident = 0;
11690
11691 snprintf(tmp, sizeof(tmp), "%d", p->call_ref_pc);
11692 pbx_builtin_setvar_helper(c, "SS7_CALLREF_PC", tmp);
11693
11694 p->call_ref_pc = 0;
11695
11696 snprintf(tmp, sizeof(tmp), "%d", p->calling_party_cat);
11697 pbx_builtin_setvar_helper(c, "SS7_CALLING_PARTY_CATEGORY", tmp);
11698
11699 p->calling_party_cat = 0;
11700
11701 if (!ast_strlen_zero(p->redirecting_num)) {
11702 pbx_builtin_setvar_helper(c, "SS7_REDIRECTING_NUMBER", p->redirecting_num);
11703
11704 p->redirecting_num[0] = 0;
11705 }
11706 if (!ast_strlen_zero(p->generic_name)) {
11707 pbx_builtin_setvar_helper(c, "SS7_GENERIC_NAME", p->generic_name);
11708
11709 p->generic_name[0] = 0;
11710 }
11711
11712 ast_mutex_lock(&p->lock);
11713 ast_mutex_lock(&linkset->lock);
11714 }
11715 #endif
11716
11717 #if defined(HAVE_SS7)
11718 static void ss7_apply_plan_to_number(char *buf, size_t size, const struct dahdi_ss7 *ss7, const char *number, const unsigned nai)
11719 {
11720 if (ast_strlen_zero(number)) {
11721 if (size) {
11722 *buf = '\0';
11723 }
11724 return;
11725 }
11726 switch (nai) {
11727 case SS7_NAI_INTERNATIONAL:
11728 snprintf(buf, size, "%s%s", ss7->internationalprefix, number);
11729 break;
11730 case SS7_NAI_NATIONAL:
11731 snprintf(buf, size, "%s%s", ss7->nationalprefix, number);
11732 break;
11733 case SS7_NAI_SUBSCRIBER:
11734 snprintf(buf, size, "%s%s", ss7->subscriberprefix, number);
11735 break;
11736 case SS7_NAI_UNKNOWN:
11737 snprintf(buf, size, "%s%s", ss7->unknownprefix, number);
11738 break;
11739 default:
11740 snprintf(buf, size, "%s", number);
11741 break;
11742 }
11743 }
11744 #endif
11745
11746 #if defined(HAVE_SS7)
11747 static int ss7_pres_scr2cid_pres(char presentation_ind, char screening_ind)
11748 {
11749 return ((presentation_ind & 0x3) << 5) | (screening_ind & 0x3);
11750 }
11751 #endif
11752
11753 #if defined(HAVE_SS7)
11754 static void *ss7_linkset(void *data)
11755 {
11756 int res, i;
11757 struct timeval *next = NULL, tv;
11758 struct dahdi_ss7 *linkset = (struct dahdi_ss7 *) data;
11759 struct ss7 *ss7 = linkset->ss7;
11760 ss7_event *e = NULL;
11761 struct dahdi_pvt *p;
11762 int chanpos;
11763 struct pollfd pollers[NUM_DCHANS];
11764 int cic;
11765 unsigned int dpc;
11766 int nextms = 0;
11767
11768 ss7_start(ss7);
11769
11770 while(1) {
11771 ast_mutex_lock(&linkset->lock);
11772 if ((next = ss7_schedule_next(ss7))) {
11773 tv = ast_tvnow();
11774 tv.tv_sec = next->tv_sec - tv.tv_sec;
11775 tv.tv_usec = next->tv_usec - tv.tv_usec;
11776 if (tv.tv_usec < 0) {
11777 tv.tv_usec += 1000000;
11778 tv.tv_sec -= 1;
11779 }
11780 if (tv.tv_sec < 0) {
11781 tv.tv_sec = 0;
11782 tv.tv_usec = 0;
11783 }
11784 nextms = tv.tv_sec * 1000;
11785 nextms += tv.tv_usec / 1000;
11786 }
11787 ast_mutex_unlock(&linkset->lock);
11788
11789 for (i = 0; i < linkset->numsigchans; i++) {
11790 pollers[i].fd = linkset->fds[i];
11791 pollers[i].events = ss7_pollflags(ss7, linkset->fds[i]);
11792 pollers[i].revents = 0;
11793 }
11794
11795 res = poll(pollers, linkset->numsigchans, nextms);
11796 if ((res < 0) && (errno != EINTR)) {
11797 ast_log(LOG_ERROR, "poll(%s)\n", strerror(errno));
11798 } else if (!res) {
11799 ast_mutex_lock(&linkset->lock);
11800 ss7_schedule_run(ss7);
11801 ast_mutex_unlock(&linkset->lock);
11802 continue;
11803 }
11804
11805 ast_mutex_lock(&linkset->lock);
11806 for (i = 0; i < linkset->numsigchans; i++) {
11807 if (pollers[i].revents & POLLPRI) {
11808 int x;
11809 if (ioctl(pollers[i].fd, DAHDI_GETEVENT, &x)) {
11810 ast_log(LOG_ERROR, "Error in exception retrieval!\n");
11811 }
11812 switch (x) {
11813 case DAHDI_EVENT_OVERRUN:
11814 ast_debug(1, "Overrun detected!\n");
11815 break;
11816 case DAHDI_EVENT_BADFCS:
11817 ast_debug(1, "Bad FCS\n");
11818 break;
11819 case DAHDI_EVENT_ABORT:
11820 ast_debug(1, "HDLC Abort\n");
11821 break;
11822 case DAHDI_EVENT_ALARM:
11823 ast_log(LOG_ERROR, "Alarm on link!\n");
11824 linkset->linkstate[i] |= (LINKSTATE_DOWN | LINKSTATE_INALARM);
11825 linkset->linkstate[i] &= ~LINKSTATE_UP;
11826 ss7_link_alarm(ss7, pollers[i].fd);
11827 break;
11828 case DAHDI_EVENT_NOALARM:
11829 ast_log(LOG_ERROR, "Alarm cleared on link\n");
11830 linkset->linkstate[i] &= ~(LINKSTATE_INALARM | LINKSTATE_DOWN);
11831 linkset->linkstate[i] |= LINKSTATE_STARTING;
11832 ss7_link_noalarm(ss7, pollers[i].fd);
11833 break;
11834 default:
11835 ast_log(LOG_ERROR, "Got exception %d!\n", x);
11836 break;
11837 }
11838 }
11839
11840 if (pollers[i].revents & POLLIN) {
11841 res = ss7_read(ss7, pollers[i].fd);
11842 }
11843
11844 if (pollers[i].revents & POLLOUT) {
11845 res = ss7_write(ss7, pollers[i].fd);
11846 if (res < 0) {
11847 ast_debug(1, "Error in write %s\n", strerror(errno));
11848 }
11849 }
11850 }
11851
11852 while ((e = ss7_check_event(ss7))) {
11853 switch (e->e) {
11854 case SS7_EVENT_UP:
11855 if (linkset->state != LINKSET_STATE_UP) {
11856 ast_verbose("--- SS7 Up ---\n");
11857 ss7_reset_linkset(linkset);
11858 }
11859 linkset->state = LINKSET_STATE_UP;
11860 break;
11861 case SS7_EVENT_DOWN:
11862 ast_verbose("--- SS7 Down ---\n");
11863 linkset->state = LINKSET_STATE_DOWN;
11864 for (i = 0; i < linkset->numchans; i++) {
11865 struct dahdi_pvt *p = linkset->pvts[i];
11866 if (p)
11867 p->inalarm = 1;
11868 }
11869 break;
11870 case MTP2_LINK_UP:
11871 ast_verbose("MTP2 link up (SLC %d)\n", e->gen.data);
11872 break;
11873 case MTP2_LINK_DOWN:
11874 ast_log(LOG_WARNING, "MTP2 link down (SLC %d)\n", e->gen.data);
11875 break;
11876 case ISUP_EVENT_CPG:
11877 chanpos = ss7_find_cic(linkset, e->cpg.cic, e->cpg.opc);
11878 if (chanpos < 0) {
11879 ast_log(LOG_WARNING, "CPG on unconfigured CIC %d\n", e->cpg.cic);
11880 break;
11881 }
11882 p = linkset->pvts[chanpos];
11883 ast_mutex_lock(&p->lock);
11884 switch (e->cpg.event) {
11885 case CPG_EVENT_ALERTING:
11886 p->alerting = 1;
11887 p->subs[SUB_REAL].needringing = 1;
11888 break;
11889 case CPG_EVENT_PROGRESS:
11890 case CPG_EVENT_INBANDINFO:
11891 {
11892 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
11893 ast_debug(1, "Queuing frame PROGRESS on CIC %d\n", p->cic);
11894 dahdi_queue_frame(p, &f, linkset);
11895 p->progress = 1;
11896 p->dialing = 0;
11897 if (p->dsp && p->dsp_features) {
11898 ast_dsp_set_features(p->dsp, p->dsp_features);
11899 p->dsp_features = 0;
11900 }
11901 }
11902 break;
11903 default:
11904 ast_debug(1, "Do not handle CPG with event type 0x%x\n", e->cpg.event);
11905 }
11906
11907 ast_mutex_unlock(&p->lock);
11908 break;
11909 case ISUP_EVENT_RSC:
11910 ast_verbose("Resetting CIC %d\n", e->rsc.cic);
11911 chanpos = ss7_find_cic(linkset, e->rsc.cic, e->rsc.opc);
11912 if (chanpos < 0) {
11913 ast_log(LOG_WARNING, "RSC on unconfigured CIC %d\n", e->rsc.cic);
11914 break;
11915 }
11916 p = linkset->pvts[chanpos];
11917 ast_mutex_lock(&p->lock);
11918 p->inservice = 1;
11919 p->remotelyblocked = 0;
11920 dpc = p->dpc;
11921 isup_set_call_dpc(e->rsc.call, dpc);
11922 if (p->ss7call)
11923 p->ss7call = NULL;
11924 if (p->owner)
11925 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
11926 ast_mutex_unlock(&p->lock);
11927 isup_rlc(ss7, e->rsc.call);
11928 break;
11929 case ISUP_EVENT_GRS:
11930 ast_debug(1, "Got Reset for CICs %d to %d: Acknowledging\n", e->grs.startcic, e->grs.endcic);
11931 chanpos = ss7_find_cic(linkset, e->grs.startcic, e->grs.opc);
11932 if (chanpos < 0) {
11933 ast_log(LOG_WARNING, "GRS on unconfigured CIC %d\n", e->grs.startcic);
11934 break;
11935 }
11936 p = linkset->pvts[chanpos];
11937 isup_gra(ss7, e->grs.startcic, e->grs.endcic, e->grs.opc);
11938 ss7_block_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc, NULL, 0);
11939 ss7_hangup_cics(linkset, e->grs.startcic, e->grs.endcic, e->grs.opc);
11940 break;
11941 case ISUP_EVENT_CQM:
11942 ast_debug(1, "Got Circuit group query message from CICs %d to %d\n", e->cqm.startcic, e->cqm.endcic);
11943 ss7_handle_cqm(linkset, e->cqm.startcic, e->cqm.endcic, e->cqm.opc);
11944 break;
11945 case ISUP_EVENT_GRA:
11946 ast_verbose("Got reset acknowledgement from CIC %d to %d.\n", e->gra.startcic, e->gra.endcic);
11947 ss7_inservice(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc);
11948 ss7_block_cics(linkset, e->gra.startcic, e->gra.endcic, e->gra.opc, e->gra.status, 1);
11949 break;
11950 case ISUP_EVENT_IAM:
11951 ast_debug(1, "Got IAM for CIC %d and called number %s, calling number %s\n", e->iam.cic, e->iam.called_party_num, e->iam.calling_party_num);
11952 chanpos = ss7_find_cic(linkset, e->iam.cic, e->iam.opc);
11953 if (chanpos < 0) {
11954 ast_log(LOG_WARNING, "IAM on unconfigured CIC %d\n", e->iam.cic);
11955 isup_rel(ss7, e->iam.call, -1);
11956 break;
11957 }
11958 p = linkset->pvts[chanpos];
11959 ast_mutex_lock(&p->lock);
11960 if (p->owner) {
11961 if (p->ss7call == e->iam.call) {
11962 ast_mutex_unlock(&p->lock);
11963 ast_log(LOG_WARNING, "Duplicate IAM requested on CIC %d\n", e->iam.cic);
11964 break;
11965 } else {
11966 ast_mutex_unlock(&p->lock);
11967 ast_log(LOG_WARNING, "Ring requested on CIC %d already in use!\n", e->iam.cic);
11968 break;
11969 }
11970 }
11971
11972 dpc = p->dpc;
11973 p->ss7call = e->iam.call;
11974 isup_set_call_dpc(p->ss7call, dpc);
11975
11976 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.calling_party_num))) {
11977 ss7_apply_plan_to_number(p->cid_num, sizeof(p->cid_num), linkset, e->iam.calling_party_num, e->iam.calling_nai);
11978 p->callingpres = ss7_pres_scr2cid_pres(e->iam.presentation_ind, e->iam.screening_ind);
11979 } else
11980 p->cid_num[0] = 0;
11981
11982 if (p->immediate) {
11983 p->exten[0] = 's';
11984 p->exten[1] = '\0';
11985 } else if (!ast_strlen_zero(e->iam.called_party_num)) {
11986 char *st;
11987 ss7_apply_plan_to_number(p->exten, sizeof(p->exten), linkset, e->iam.called_party_num, e->iam.called_nai);
11988 st = strchr(p->exten, '#');
11989 if (st)
11990 *st = '\0';
11991 } else
11992 p->exten[0] = '\0';
11993
11994 p->cid_ani[0] = '\0';
11995 if ((p->use_callerid) && (!ast_strlen_zero(e->iam.generic_name)))
11996 ast_copy_string(p->cid_name, e->iam.generic_name, sizeof(p->cid_name));
11997 else
11998 p->cid_name[0] = '\0';
11999
12000 p->cid_ani2 = e->iam.oli_ani2;
12001 p->cid_ton = 0;
12002 ast_copy_string(p->charge_number, e->iam.charge_number, sizeof(p->charge_number));
12003 ast_copy_string(p->gen_add_number, e->iam.gen_add_number, sizeof(p->gen_add_number));
12004 p->gen_add_type = e->iam.gen_add_type;
12005 p->gen_add_nai = e->iam.gen_add_nai;
12006 p->gen_add_pres_ind = e->iam.gen_add_pres_ind;
12007 p->gen_add_num_plan = e->iam.gen_add_num_plan;
12008 ast_copy_string(p->gen_dig_number, e->iam.gen_dig_number, sizeof(p->gen_dig_number));
12009 p->gen_dig_type = e->iam.gen_dig_type;
12010 p->gen_dig_scheme = e->iam.gen_dig_scheme;
12011 ast_copy_string(p->jip_number, e->iam.jip_number, sizeof(p->jip_number));
12012 ast_copy_string(p->orig_called_num, e->iam.orig_called_num, sizeof(p->orig_called_num));
12013 ast_copy_string(p->redirecting_num, e->iam.redirecting_num, sizeof(p->redirecting_num));
12014 ast_copy_string(p->generic_name, e->iam.generic_name, sizeof(p->generic_name));
12015 p->calling_party_cat = e->iam.calling_party_cat;
12016
12017
12018 if (!ast_strlen_zero(e->iam.called_party_num))
12019 ss7_apply_plan_to_number(p->dnid, sizeof(p->dnid), linkset, e->iam.called_party_num, e->iam.called_nai);
12020
12021 if (ast_exists_extension(NULL, p->context, p->exten, 1, p->cid_num)) {
12022
12023 if (e->iam.cot_check_required) {
12024 dahdi_loopback(p, 1);
12025 } else
12026 ss7_start_call(p, linkset);
12027 } else {
12028 ast_debug(1, "Call on CIC for unconfigured extension %s\n", p->exten);
12029 p->alreadyhungup = 1;
12030 isup_rel(ss7, e->iam.call, AST_CAUSE_UNALLOCATED);
12031 }
12032 ast_mutex_unlock(&p->lock);
12033 break;
12034 case ISUP_EVENT_COT:
12035 chanpos = ss7_find_cic(linkset, e->cot.cic, e->cot.opc);
12036 if (chanpos < 0) {
12037 ast_log(LOG_WARNING, "COT on unconfigured CIC %d\n", e->cot.cic);
12038 isup_rel(ss7, e->cot.call, -1);
12039 break;
12040 }
12041 p = linkset->pvts[chanpos];
12042
12043 ast_mutex_lock(&p->lock);
12044
12045 if (p->loopedback) {
12046 dahdi_loopback(p, 0);
12047 ss7_start_call(p, linkset);
12048 }
12049
12050 ast_mutex_unlock(&p->lock);
12051
12052 break;
12053 case ISUP_EVENT_CCR:
12054 ast_debug(1, "Got CCR request on CIC %d\n", e->ccr.cic);
12055 chanpos = ss7_find_cic(linkset, e->ccr.cic, e->ccr.opc);
12056 if (chanpos < 0) {
12057 ast_log(LOG_WARNING, "CCR on unconfigured CIC %d\n", e->ccr.cic);
12058 break;
12059 }
12060
12061 p = linkset->pvts[chanpos];
12062
12063 ast_mutex_lock(&p->lock);
12064 dahdi_loopback(p, 1);
12065 ast_mutex_unlock(&p->lock);
12066
12067 isup_lpa(linkset->ss7, e->ccr.cic, p->dpc);
12068 break;
12069 case ISUP_EVENT_CVT:
12070 ast_debug(1, "Got CVT request on CIC %d\n", e->cvt.cic);
12071 chanpos = ss7_find_cic(linkset, e->cvt.cic, e->cvt.opc);
12072 if (chanpos < 0) {
12073 ast_log(LOG_WARNING, "CVT on unconfigured CIC %d\n", e->cvt.cic);
12074 break;
12075 }
12076
12077 p = linkset->pvts[chanpos];
12078
12079 ast_mutex_lock(&p->lock);
12080 dahdi_loopback(p, 1);
12081 ast_mutex_unlock(&p->lock);
12082
12083 isup_cvr(linkset->ss7, e->cvt.cic, p->dpc);
12084 break;
12085 case ISUP_EVENT_REL:
12086 chanpos = ss7_find_cic(linkset, e->rel.cic, e->rel.opc);
12087 if (chanpos < 0) {
12088 ast_log(LOG_WARNING, "REL on unconfigured CIC %d\n", e->rel.cic);
12089 break;
12090 }
12091 p = linkset->pvts[chanpos];
12092 ast_mutex_lock(&p->lock);
12093 if (p->owner) {
12094 p->owner->hangupcause = e->rel.cause;
12095 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
12096 } else if (!p->restartpending)
12097 ast_log(LOG_WARNING, "REL on channel (CIC %d) without owner!\n", p->cic);
12098
12099
12100 dahdi_loopback(p, 0);
12101
12102 isup_rlc(ss7, e->rel.call);
12103 p->ss7call = NULL;
12104
12105 ast_mutex_unlock(&p->lock);
12106 break;
12107 case ISUP_EVENT_ACM:
12108 chanpos = ss7_find_cic(linkset, e->acm.cic, e->acm.opc);
12109 if (chanpos < 0) {
12110 ast_log(LOG_WARNING, "ACM on unconfigured CIC %d\n", e->acm.cic);
12111 isup_rel(ss7, e->acm.call, -1);
12112 break;
12113 } else {
12114 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
12115
12116 p = linkset->pvts[chanpos];
12117
12118 ast_debug(1, "Queueing frame from SS7_EVENT_ACM on CIC %d\n", p->cic);
12119
12120 if (e->acm.call_ref_ident > 0) {
12121 p->rlt = 1;
12122 }
12123
12124 ast_mutex_lock(&p->lock);
12125 dahdi_queue_frame(p, &f, linkset);
12126 p->proceeding = 1;
12127 p->dialing = 0;
12128
12129 if (e->acm.called_party_status_ind == 1) {
12130 p->alerting = 1;
12131 p->subs[SUB_REAL].needringing = 1;
12132 }
12133 ast_mutex_unlock(&p->lock);
12134 }
12135 break;
12136 case ISUP_EVENT_CGB:
12137 chanpos = ss7_find_cic(linkset, e->cgb.startcic, e->cgb.opc);
12138 if (chanpos < 0) {
12139 ast_log(LOG_WARNING, "CGB on unconfigured CIC %d\n", e->cgb.startcic);
12140 break;
12141 }
12142 p = linkset->pvts[chanpos];
12143 ss7_block_cics(linkset, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, 1);
12144 isup_cgba(linkset->ss7, e->cgb.startcic, e->cgb.endcic, e->cgb.opc, e->cgb.status, e->cgb.type);
12145 break;
12146 case ISUP_EVENT_CGU:
12147 chanpos = ss7_find_cic(linkset, e->cgu.startcic, e->cgu.opc);
12148 if (chanpos < 0) {
12149 ast_log(LOG_WARNING, "CGU on unconfigured CIC %d\n", e->cgu.startcic);
12150 break;
12151 }
12152 p = linkset->pvts[chanpos];
12153 ss7_block_cics(linkset, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, 0);
12154 isup_cgua(linkset->ss7, e->cgu.startcic, e->cgu.endcic, e->cgu.opc, e->cgu.status, e->cgu.type);
12155 break;
12156 case ISUP_EVENT_UCIC:
12157 chanpos = ss7_find_cic(linkset, e->ucic.cic, e->ucic.opc);
12158 if (chanpos < 0) {
12159 ast_log(LOG_WARNING, "UCIC on unconfigured CIC %d\n", e->ucic.cic);
12160 break;
12161 }
12162 p = linkset->pvts[chanpos];
12163 ast_debug(1, "Unequiped Circuit Id Code on CIC %d\n", e->ucic.cic);
12164 ast_mutex_lock(&p->lock);
12165 p->remotelyblocked = 1;
12166 p->inservice = 0;
12167 ast_mutex_unlock(&p->lock);
12168 break;
12169 case ISUP_EVENT_BLO:
12170 chanpos = ss7_find_cic(linkset, e->blo.cic, e->blo.opc);
12171 if (chanpos < 0) {
12172 ast_log(LOG_WARNING, "BLO on unconfigured CIC %d\n", e->blo.cic);
12173 break;
12174 }
12175 p = linkset->pvts[chanpos];
12176 ast_debug(1, "Blocking CIC %d\n", e->blo.cic);
12177 ast_mutex_lock(&p->lock);
12178 p->remotelyblocked = 1;
12179 ast_mutex_unlock(&p->lock);
12180 isup_bla(linkset->ss7, e->blo.cic, p->dpc);
12181 break;
12182 case ISUP_EVENT_BLA:
12183 chanpos = ss7_find_cic(linkset, e->bla.cic, e->bla.opc);
12184 if (chanpos < 0) {
12185 ast_log(LOG_WARNING, "BLA on unconfigured CIC %d\n", e->bla.cic);
12186 break;
12187 }
12188 ast_debug(1, "Blocking CIC %d\n", e->bla.cic);
12189 p = linkset->pvts[chanpos];
12190 ast_mutex_lock(&p->lock);
12191 p->locallyblocked = 1;
12192 ast_mutex_unlock(&p->lock);
12193 break;
12194 case ISUP_EVENT_UBL:
12195 chanpos = ss7_find_cic(linkset, e->ubl.cic, e->ubl.opc);
12196 if (chanpos < 0) {
12197 ast_log(LOG_WARNING, "UBL on unconfigured CIC %d\n", e->ubl.cic);
12198 break;
12199 }
12200 p = linkset->pvts[chanpos];
12201 ast_debug(1, "Unblocking CIC %d\n", e->ubl.cic);
12202 ast_mutex_lock(&p->lock);
12203 p->remotelyblocked = 0;
12204 ast_mutex_unlock(&p->lock);
12205 isup_uba(linkset->ss7, e->ubl.cic, p->dpc);
12206 break;
12207 case ISUP_EVENT_UBA:
12208 chanpos = ss7_find_cic(linkset, e->uba.cic, e->uba.opc);
12209 if (chanpos < 0) {
12210 ast_log(LOG_WARNING, "UBA on unconfigured CIC %d\n", e->uba.cic);
12211 break;
12212 }
12213 p = linkset->pvts[chanpos];
12214 ast_debug(1, "Unblocking CIC %d\n", e->uba.cic);
12215 ast_mutex_lock(&p->lock);
12216 p->locallyblocked = 0;
12217 ast_mutex_unlock(&p->lock);
12218 break;
12219 case ISUP_EVENT_CON:
12220 case ISUP_EVENT_ANM:
12221 if (e->e == ISUP_EVENT_CON)
12222 cic = e->con.cic;
12223 else
12224 cic = e->anm.cic;
12225
12226 chanpos = ss7_find_cic(linkset, cic, (e->e == ISUP_EVENT_ANM) ? e->anm.opc : e->con.opc);
12227 if (chanpos < 0) {
12228 ast_log(LOG_WARNING, "ANM/CON on unconfigured CIC %d\n", cic);
12229 isup_rel(ss7, (e->e == ISUP_EVENT_ANM) ? e->anm.call : e->con.call, -1);
12230 break;
12231 } else {
12232 p = linkset->pvts[chanpos];
12233 ast_mutex_lock(&p->lock);
12234 p->subs[SUB_REAL].needanswer = 1;
12235 if (p->dsp && p->dsp_features) {
12236 ast_dsp_set_features(p->dsp, p->dsp_features);
12237 p->dsp_features = 0;
12238 }
12239 dahdi_enable_ec(p);
12240 ast_mutex_unlock(&p->lock);
12241 }
12242 break;
12243 case ISUP_EVENT_RLC:
12244 chanpos = ss7_find_cic(linkset, e->rlc.cic, e->rlc.opc);
12245 if (chanpos < 0) {
12246 ast_log(LOG_WARNING, "RLC on unconfigured CIC %d\n", e->rlc.cic);
12247 break;
12248 } else {
12249 p = linkset->pvts[chanpos];
12250 ast_mutex_lock(&p->lock);
12251 if (p->alreadyhungup)
12252 p->ss7call = NULL;
12253 else
12254 ast_log(LOG_NOTICE, "Received RLC out and we haven't sent REL. Ignoring.\n");
12255 ast_mutex_unlock(&p->lock);
12256 }
12257 break;
12258 case ISUP_EVENT_FAA:
12259 chanpos = ss7_find_cic(linkset, e->faa.cic, e->faa.opc);
12260 if (chanpos < 0) {
12261 ast_log(LOG_WARNING, "FAA on unconfigured CIC %d\n", e->faa.cic);
12262 break;
12263 } else {
12264 p = linkset->pvts[chanpos];
12265 ast_debug(1, "FAA received on CIC %d\n", e->faa.cic);
12266 ast_mutex_lock(&p->lock);
12267 if (p->alreadyhungup){
12268 p->ss7call = NULL;
12269 ast_log(LOG_NOTICE, "Received FAA and we haven't sent FAR. Ignoring.\n");
12270 }
12271 ast_mutex_unlock(&p->lock);
12272 }
12273 break;
12274 default:
12275 ast_debug(1, "Unknown event %s\n", ss7_event2str(e->e));
12276 break;
12277 }
12278 }
12279 ast_mutex_unlock(&linkset->lock);
12280 }
12281
12282 return 0;
12283 }
12284 #endif
12285
12286 #if defined(HAVE_SS7)
12287 static void dahdi_ss7_message(struct ss7 *ss7, char *s)
12288 {
12289 #if 0
12290 int i;
12291
12292 for (i = 0; i < NUM_SPANS; i++)
12293 if (linksets[i].ss7 == ss7)
12294 break;
12295
12296 ast_verbose("[%d] %s", i+1, s);
12297 #else
12298 ast_verbose("%s", s);
12299 #endif
12300 }
12301 #endif
12302
12303 #if defined(HAVE_SS7)
12304 static void dahdi_ss7_error(struct ss7 *ss7, char *s)
12305 {
12306 #if 0
12307 int i;
12308
12309 for (i = 0; i < NUM_SPANS; i++)
12310 if (linksets[i].ss7 == ss7)
12311 break;
12312
12313 #else
12314 ast_log(LOG_ERROR, "%s", s);
12315 #endif
12316 }
12317 #endif
12318
12319 #if defined(HAVE_OPENR2)
12320 static void *mfcr2_monitor(void *data)
12321 {
12322 struct dahdi_mfcr2 *mfcr2 = data;
12323
12324
12325
12326
12327
12328 struct pollfd pollers[sizeof(mfcr2->pvts)];
12329 int res = 0;
12330 int i = 0;
12331 int oldstate = 0;
12332 int quit_loop = 0;
12333 int maxsleep = 20;
12334 int was_idle = 0;
12335 int pollsize = 0;
12336
12337
12338 for (i = 0; i < mfcr2->numchans; i++) {
12339 openr2_chan_set_idle(mfcr2->pvts[i]->r2chan);
12340 openr2_chan_handle_cas(mfcr2->pvts[i]->r2chan);
12341 }
12342 while (1) {
12343
12344
12345 pollsize = 0;
12346 for (i = 0; i < mfcr2->numchans; i++) {
12347 pollers[i].revents = 0;
12348 pollers[i].events = 0;
12349 if (mfcr2->pvts[i]->owner) {
12350 continue;
12351 }
12352 if (!mfcr2->pvts[i]->r2chan) {
12353 ast_log(LOG_DEBUG, "Wow, no r2chan on channel %d\n", mfcr2->pvts[i]->channel);
12354 quit_loop = 1;
12355 break;
12356 }
12357 openr2_chan_enable_read(mfcr2->pvts[i]->r2chan);
12358 pollers[i].events = POLLIN | POLLPRI;
12359 pollers[i].fd = mfcr2->pvts[i]->subs[SUB_REAL].dfd;
12360 pollsize++;
12361 }
12362 if (quit_loop) {
12363 break;
12364 }
12365 if (pollsize == 0) {
12366 if (!was_idle) {
12367 ast_log(LOG_DEBUG, "Monitor thread going idle since everybody has an owner\n");
12368 was_idle = 1;
12369 }
12370 poll(NULL, 0, maxsleep);
12371 continue;
12372 }
12373 was_idle = 0;
12374
12375
12376 pthread_testcancel();
12377 res = poll(pollers, mfcr2->numchans, maxsleep);
12378 pthread_testcancel();
12379 if ((res < 0) && (errno != EINTR)) {
12380 ast_log(LOG_ERROR, "going out, poll failed: %s\n", strerror(errno));
12381 break;
12382 }
12383
12384 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
12385 for (i = 0; i < mfcr2->numchans; i++) {
12386 if (pollers[i].revents & POLLPRI || pollers[i].revents & POLLIN) {
12387 openr2_chan_process_event(mfcr2->pvts[i]->r2chan);
12388 }
12389 }
12390 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
12391 }
12392 ast_log(LOG_NOTICE, "Quitting MFC/R2 monitor thread\n");
12393 return 0;
12394 }
12395 #endif
12396
12397 #if defined(HAVE_PRI)
12398 static struct dahdi_pvt *pri_find_crv(struct dahdi_pri *pri, int crv)
12399 {
12400 struct dahdi_pvt *p;
12401 p = pri->crvs;
12402 while (p) {
12403 if (p->channel == crv)
12404 return p;
12405 p = p->next;
12406 }
12407 return NULL;
12408 }
12409 #endif
12410
12411 #if defined(HAVE_PRI)
12412 static int pri_find_principle(struct dahdi_pri *pri, int channel)
12413 {
12414 int x;
12415 int span = PRI_SPAN(channel);
12416 int spanfd;
12417 struct dahdi_params param;
12418 int principle = -1;
12419 int explicit = PRI_EXPLICIT(channel);
12420 channel = PRI_CHANNEL(channel);
12421
12422 if (!explicit) {
12423 spanfd = pri_active_dchan_fd(pri);
12424 memset(¶m, 0, sizeof(param));
12425 if (ioctl(spanfd, DAHDI_GET_PARAMS, ¶m))
12426 return -1;
12427 span = pris[param.spanno - 1].prilogicalspan;
12428 }
12429
12430 for (x = 0; x < pri->numchans; x++) {
12431 if (pri->pvts[x] && (pri->pvts[x]->prioffset == channel) && (pri->pvts[x]->logicalspan == span)) {
12432 principle = x;
12433 break;
12434 }
12435 }
12436
12437 return principle;
12438 }
12439 #endif
12440
12441 #if defined(HAVE_PRI)
12442 static int pri_fixup_principle(struct dahdi_pri *pri, int principle, q931_call *c)
12443 {
12444 int x;
12445 struct dahdi_pvt *crv;
12446 if (!c) {
12447 if (principle < 0)
12448 return -1;
12449 return principle;
12450 }
12451 if ((principle > -1) &&
12452 (principle < pri->numchans) &&
12453 (pri->pvts[principle]) &&
12454 (pri->pvts[principle]->call == c))
12455 return principle;
12456
12457 for (x = 0; x < pri->numchans; x++) {
12458 if (!pri->pvts[x])
12459 continue;
12460 if (pri->pvts[x]->call == c) {
12461
12462 if (principle != x) {
12463 struct dahdi_pvt *new = pri->pvts[principle], *old = pri->pvts[x];
12464
12465 ast_verb(3, "Moving call from channel %d to channel %d\n",
12466 old->channel, new->channel);
12467 if (new->owner) {
12468 ast_log(LOG_WARNING, "Can't fix up channel from %d to %d because %d is already in use\n",
12469 old->channel, new->channel, new->channel);
12470 return -1;
12471 }
12472
12473 new->owner = old->owner;
12474 old->owner = NULL;
12475 if (new->owner) {
12476 ast_string_field_build(new->owner, name,
12477 "DAHDI/%d:%d-%d", pri->trunkgroup,
12478 new->channel, 1);
12479 new->owner->tech_pvt = new;
12480 ast_channel_set_fd(new->owner, 0, new->subs[SUB_REAL].dfd);
12481 new->subs[SUB_REAL].owner = old->subs[SUB_REAL].owner;
12482 old->subs[SUB_REAL].owner = NULL;
12483 } else
12484 ast_log(LOG_WARNING, "Whoa, there's no owner, and we're having to fix up channel %d to channel %d\n", old->channel, new->channel);
12485 new->call = old->call;
12486 old->call = NULL;
12487
12488
12489 new->dsp = old->dsp;
12490 new->dsp_features = old->dsp_features;
12491 old->dsp = NULL;
12492 old->dsp_features = 0;
12493 }
12494 return principle;
12495 }
12496 }
12497
12498 crv = pri->crvs;
12499 while (crv) {
12500 if (crv->call == c) {
12501
12502 if (crv->bearer)
12503 ast_log(LOG_WARNING, "Trying to fix up call which already has a bearer which isn't the one we think it is\n");
12504 else if (pri->pvts[principle]->owner)
12505 ast_log(LOG_WARNING, "Tring to fix up a call to a bearer which already has an owner!\n");
12506 else {
12507
12508
12509 dahdi_close_sub(crv, SUB_REAL);
12510 pri->pvts[principle]->call = crv->call;
12511 pri_assign_bearer(crv, pri, pri->pvts[principle]);
12512 ast_debug(1, "Assigning bearer %d/%d to CRV %d:%d\n",
12513 pri->pvts[principle]->logicalspan, pri->pvts[principle]->prioffset,
12514 pri->trunkgroup, crv->channel);
12515 wakeup_sub(crv, SUB_REAL, pri);
12516 }
12517 return principle;
12518 }
12519 crv = crv->next;
12520 }
12521 ast_log(LOG_WARNING, "Call specified, but not found?\n");
12522 return -1;
12523 }
12524 #endif
12525
12526 #if defined(HAVE_PRI)
12527 static void *do_idle_thread(void *vchan)
12528 {
12529 struct ast_channel *chan = vchan;
12530 struct dahdi_pvt *pvt = chan->tech_pvt;
12531 struct ast_frame *f;
12532 char ex[80];
12533
12534 int newms, ms = 30000;
12535 ast_verb(3, "Initiating idle call on channel %s\n", chan->name);
12536 snprintf(ex, sizeof(ex), "%d/%s", pvt->channel, pvt->pri->idledial);
12537 if (ast_call(chan, ex, 0)) {
12538 ast_log(LOG_WARNING, "Idle dial failed on '%s' to '%s'\n", chan->name, ex);
12539 ast_hangup(chan);
12540 return NULL;
12541 }
12542 while ((newms = ast_waitfor(chan, ms)) > 0) {
12543 f = ast_read(chan);
12544 if (!f) {
12545
12546 break;
12547 }
12548 if (f->frametype == AST_FRAME_CONTROL) {
12549 switch (f->subclass) {
12550 case AST_CONTROL_ANSWER:
12551
12552 ast_copy_string(chan->exten, pvt->pri->idleext, sizeof(chan->exten));
12553 ast_copy_string(chan->context, pvt->pri->idlecontext, sizeof(chan->context));
12554 chan->priority = 1;
12555 ast_verb(4, "Idle channel '%s' answered, sending to %s@%s\n", chan->name, chan->exten, chan->context);
12556 ast_pbx_run(chan);
12557
12558 return NULL;
12559 case AST_CONTROL_BUSY:
12560 ast_verb(4, "Idle channel '%s' busy, waiting...\n", chan->name);
12561 break;
12562 case AST_CONTROL_CONGESTION:
12563 ast_verb(4, "Idle channel '%s' congested, waiting...\n", chan->name);
12564 break;
12565 };
12566 }
12567 ast_frfree(f);
12568 ms = newms;
12569 }
12570
12571 ast_hangup(chan);
12572 return NULL;
12573 }
12574 #endif
12575
12576 #if defined(HAVE_PRI)
12577 #ifndef PRI_RESTART
12578 #error "Upgrade your libpri"
12579 #endif
12580 static void dahdi_pri_message(struct pri *pri, char *s)
12581 {
12582 int x, y;
12583 int dchan = -1, span = -1;
12584 int dchancount = 0;
12585
12586 if (pri) {
12587 for (x = 0; x < NUM_SPANS; x++) {
12588 for (y = 0; y < NUM_DCHANS; y++) {
12589 if (pris[x].dchans[y])
12590 dchancount++;
12591
12592 if (pris[x].dchans[y] == pri)
12593 dchan = y;
12594 }
12595 if (dchan >= 0) {
12596 span = x;
12597 break;
12598 }
12599 dchancount = 0;
12600 }
12601 if (dchancount > 1 && (span > -1))
12602 ast_verbose("[Span %d D-Channel %d]%s", span, dchan, s);
12603 else
12604 ast_verbose("%s", s);
12605 } else
12606 ast_verbose("%s", s);
12607
12608 ast_mutex_lock(&pridebugfdlock);
12609
12610 if (pridebugfd >= 0) {
12611 if (write(pridebugfd, s, strlen(s)) < 0) {
12612 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
12613 }
12614 }
12615
12616 ast_mutex_unlock(&pridebugfdlock);
12617 }
12618 #endif
12619
12620 #if defined(HAVE_PRI)
12621 static void dahdi_pri_error(struct pri *pri, char *s)
12622 {
12623 int x, y;
12624 int dchan = -1, span = -1;
12625 int dchancount = 0;
12626
12627 if (pri) {
12628 for (x = 0; x < NUM_SPANS; x++) {
12629 for (y = 0; y < NUM_DCHANS; y++) {
12630 if (pris[x].dchans[y])
12631 dchancount++;
12632
12633 if (pris[x].dchans[y] == pri)
12634 dchan = y;
12635 }
12636 if (dchan >= 0) {
12637 span = x;
12638 break;
12639 }
12640 dchancount = 0;
12641 }
12642 if ((dchancount > 1) && (span > -1))
12643 ast_log(LOG_ERROR, "[Span %d D-Channel %d] PRI: %s", span, dchan, s);
12644 else
12645 ast_log(LOG_ERROR, "%s", s);
12646 } else
12647 ast_log(LOG_ERROR, "%s", s);
12648
12649 ast_mutex_lock(&pridebugfdlock);
12650
12651 if (pridebugfd >= 0) {
12652 if (write(pridebugfd, s, strlen(s)) < 0) {
12653 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
12654 }
12655 }
12656
12657 ast_mutex_unlock(&pridebugfdlock);
12658 }
12659 #endif
12660
12661 #if defined(HAVE_PRI)
12662 static int pri_check_restart(struct dahdi_pri *pri)
12663 {
12664 do {
12665 pri->resetpos++;
12666 } while ((pri->resetpos < pri->numchans) &&
12667 (!pri->pvts[pri->resetpos] ||
12668 pri->pvts[pri->resetpos]->call ||
12669 pri->pvts[pri->resetpos]->resetting));
12670 if (pri->resetpos < pri->numchans) {
12671
12672 pri->pvts[pri->resetpos]->resetting = 1;
12673 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[pri->resetpos]));
12674 } else {
12675 pri->resetting = 0;
12676 time(&pri->lastreset);
12677 }
12678 return 0;
12679 }
12680 #endif
12681
12682 #if defined(HAVE_PRI)
12683 static int pri_hangup_all(struct dahdi_pvt *p, struct dahdi_pri *pri)
12684 {
12685 int x;
12686 int redo;
12687 ast_mutex_unlock(&pri->lock);
12688 ast_mutex_lock(&p->lock);
12689 do {
12690 redo = 0;
12691 for (x = 0; x < 3; x++) {
12692 while (p->subs[x].owner && ast_channel_trylock(p->subs[x].owner)) {
12693 redo++;
12694 DEADLOCK_AVOIDANCE(&p->lock);
12695 }
12696 if (p->subs[x].owner) {
12697 ast_queue_hangup_with_cause(p->subs[x].owner, AST_CAUSE_PRE_EMPTED);
12698 ast_channel_unlock(p->subs[x].owner);
12699 }
12700 }
12701 } while (redo);
12702 ast_mutex_unlock(&p->lock);
12703 ast_mutex_lock(&pri->lock);
12704 return 0;
12705 }
12706 #endif
12707
12708 #if defined(HAVE_PRI)
12709 static char * redirectingreason2str(int redirectingreason)
12710 {
12711 switch (redirectingreason) {
12712 case 0:
12713 return "UNKNOWN";
12714 case 1:
12715 return "BUSY";
12716 case 2:
12717 return "NO_REPLY";
12718 case 0xF:
12719 return "UNCONDITIONAL";
12720 default:
12721 return "NOREDIRECT";
12722 }
12723 }
12724 #endif
12725
12726 #if defined(HAVE_PRI)
12727 static void apply_plan_to_number(char *buf, size_t size, const struct dahdi_pri *pri, const char *number, const int plan)
12728 {
12729 if (pri->dialplan == -2) {
12730 snprintf(buf, size, "%s", number);
12731 return;
12732 }
12733 if (ast_strlen_zero(number)) {
12734 if (size) {
12735 *buf = '\0';
12736 }
12737 return;
12738 }
12739 switch (plan) {
12740 case PRI_INTERNATIONAL_ISDN:
12741 snprintf(buf, size, "%s%s", pri->internationalprefix, number);
12742 break;
12743 case PRI_NATIONAL_ISDN:
12744 snprintf(buf, size, "%s%s", pri->nationalprefix, number);
12745 break;
12746 case PRI_LOCAL_ISDN:
12747 snprintf(buf, size, "%s%s", pri->localprefix, number);
12748 break;
12749 case PRI_PRIVATE:
12750 snprintf(buf, size, "%s%s", pri->privateprefix, number);
12751 break;
12752 case PRI_UNKNOWN:
12753 snprintf(buf, size, "%s%s", pri->unknownprefix, number);
12754 break;
12755 default:
12756 snprintf(buf, size, "%s", number);
12757 break;
12758 }
12759 }
12760 #endif
12761
12762 #if defined(HAVE_PRI)
12763 static void *pri_dchannel(void *vpri)
12764 {
12765 struct dahdi_pri *pri = vpri;
12766 pri_event *e;
12767 struct pollfd fds[NUM_DCHANS];
12768 int res;
12769 int chanpos = 0;
12770 int x;
12771 int haveidles;
12772 int activeidles;
12773 int nextidle = -1;
12774 struct ast_channel *c;
12775 struct timeval tv, lowest, *next;
12776 struct timeval lastidle = ast_tvnow();
12777 int doidling=0;
12778 char *cc;
12779 char idlen[80];
12780 struct ast_channel *idle;
12781 pthread_t p;
12782 time_t t;
12783 int i, which=-1;
12784 int numdchans;
12785 int cause=0;
12786 struct dahdi_pvt *crv;
12787 pthread_t threadid;
12788 char ani2str[6];
12789 char plancallingnum[256];
12790 char plancallingani[256];
12791 char calledtonstr[10];
12792
12793 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
12794
12795 gettimeofday(&lastidle, NULL);
12796 if (!ast_strlen_zero(pri->idledial) && !ast_strlen_zero(pri->idleext)) {
12797
12798 cc = strchr(pri->idleext, '@');
12799 if (cc) {
12800 *cc = '\0';
12801 cc++;
12802 ast_copy_string(pri->idlecontext, cc, sizeof(pri->idlecontext));
12803 #if 0
12804
12805 if (!ast_exists_extension(NULL, pri->idlecontext, pri->idleext, 1, NULL))
12806 ast_log(LOG_WARNING, "Extension '%s @ %s' does not exist\n", pri->idleext, pri->idlecontext);
12807 else
12808 #endif
12809 doidling = 1;
12810 } else
12811 ast_log(LOG_WARNING, "Idle dial string '%s' lacks '@context'\n", pri->idleext);
12812 }
12813 for (;;) {
12814 for (i = 0; i < NUM_DCHANS; i++) {
12815 if (!pri->dchannels[i])
12816 break;
12817 fds[i].fd = pri->fds[i];
12818 fds[i].events = POLLIN | POLLPRI;
12819 fds[i].revents = 0;
12820 }
12821 numdchans = i;
12822 time(&t);
12823 ast_mutex_lock(&pri->lock);
12824 if ((pri->switchtype != PRI_SWITCH_GR303_TMC) && (pri->sig != SIG_BRI_PTMP) && (pri->resetinterval > 0)) {
12825 if (pri->resetting && pri_is_up(pri)) {
12826 if (pri->resetpos < 0)
12827 pri_check_restart(pri);
12828 } else {
12829 if (!pri->resetting && (t - pri->lastreset) >= pri->resetinterval) {
12830 pri->resetting = 1;
12831 pri->resetpos = -1;
12832 }
12833 }
12834 }
12835
12836 if (doidling && pri_is_up(pri)) {
12837 nextidle = -1;
12838 haveidles = 0;
12839 activeidles = 0;
12840 for (x = pri->numchans; x >= 0; x--) {
12841 if (pri->pvts[x] && !pri->pvts[x]->owner &&
12842 !pri->pvts[x]->call) {
12843 if (haveidles < pri->minunused) {
12844 haveidles++;
12845 } else if (!pri->pvts[x]->resetting) {
12846 nextidle = x;
12847 break;
12848 }
12849 } else if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall)
12850 activeidles++;
12851 }
12852 if (nextidle > -1) {
12853 if (ast_tvdiff_ms(ast_tvnow(), lastidle) > 1000) {
12854
12855 snprintf(idlen, sizeof(idlen), "%d/%s", pri->pvts[nextidle]->channel, pri->idledial);
12856 idle = dahdi_request("DAHDI", AST_FORMAT_ULAW, idlen, &cause);
12857 if (idle) {
12858 pri->pvts[nextidle]->isidlecall = 1;
12859 if (ast_pthread_create_background(&p, NULL, do_idle_thread, idle)) {
12860 ast_log(LOG_WARNING, "Unable to start new thread for idle channel '%s'\n", idle->name);
12861 dahdi_hangup(idle);
12862 }
12863 } else
12864 ast_log(LOG_WARNING, "Unable to request channel 'DAHDI/%s' for idle call\n", idlen);
12865 lastidle = ast_tvnow();
12866 }
12867 } else if ((haveidles < pri->minunused) &&
12868 (activeidles > pri->minidle)) {
12869
12870
12871 for (x = pri->numchans; x >= 0; x--) {
12872
12873 if (pri->pvts[x] && pri->pvts[x]->owner && pri->pvts[x]->isidlecall) {
12874 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
12875 haveidles++;
12876
12877
12878 if ((haveidles >= pri->minunused) ||
12879 (activeidles <= pri->minidle))
12880 break;
12881 }
12882 }
12883 }
12884 }
12885
12886 lowest = ast_tv(60, 0);
12887 for (i = 0; i < NUM_DCHANS; i++) {
12888
12889 if (!pri->dchannels[i])
12890 break;
12891 if ((next = pri_schedule_next(pri->dchans[i]))) {
12892
12893 tv = ast_tvsub(*next, ast_tvnow());
12894 if (tv.tv_sec < 0) {
12895 tv = ast_tv(0,0);
12896 }
12897 if (doidling || pri->resetting) {
12898 if (tv.tv_sec > 1) {
12899 tv = ast_tv(1, 0);
12900 }
12901 } else {
12902 if (tv.tv_sec > 60) {
12903 tv = ast_tv(60, 0);
12904 }
12905 }
12906 } else if (doidling || pri->resetting) {
12907
12908
12909 tv = ast_tv(1,0);
12910 } else {
12911
12912 tv = ast_tv(60, 0);
12913 }
12914 if (!i || ast_tvcmp(tv, lowest) < 0) {
12915 lowest = tv;
12916 }
12917 }
12918 ast_mutex_unlock(&pri->lock);
12919
12920 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
12921 pthread_testcancel();
12922 e = NULL;
12923 res = poll(fds, numdchans, lowest.tv_sec * 1000 + lowest.tv_usec / 1000);
12924 pthread_testcancel();
12925 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
12926
12927 ast_mutex_lock(&pri->lock);
12928 if (!res) {
12929 for (which = 0; which < NUM_DCHANS; which++) {
12930 if (!pri->dchans[which])
12931 break;
12932
12933 e = pri_schedule_run(pri->dchans[which]);
12934 if (e)
12935 break;
12936 }
12937 } else if (res > -1) {
12938 for (which = 0; which < NUM_DCHANS; which++) {
12939 if (!pri->dchans[which])
12940 break;
12941 if (fds[which].revents & POLLPRI) {
12942
12943 x = 0;
12944 res = ioctl(pri->fds[which], DAHDI_GETEVENT, &x);
12945 if (x) {
12946 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);
12947 manager_event(EVENT_FLAG_SYSTEM, "PRIEvent",
12948 "PRIEvent: %s\r\n"
12949 "PRIEventCode: %d\r\n"
12950 "D-channel: %s\r\n"
12951 "Span: %d\r\n",
12952 event2str(x),
12953 x,
12954 pri_order(which),
12955 pri->span
12956 );
12957 }
12958
12959 if (x == DAHDI_EVENT_ALARM) {
12960 pri->dchanavail[which] &= ~(DCHAN_NOTINALARM | DCHAN_UP);
12961 pri_find_dchan(pri);
12962 } else if (x == DAHDI_EVENT_NOALARM) {
12963 pri->dchanavail[which] |= DCHAN_NOTINALARM;
12964 pri_restart(pri->dchans[which]);
12965 }
12966
12967 ast_debug(1, "Got event %s (%d) on D-channel for span %d\n", event2str(x), x, pri->span);
12968 } else if (fds[which].revents & POLLIN) {
12969 e = pri_check_event(pri->dchans[which]);
12970 }
12971 if (e)
12972 break;
12973 }
12974 } else if (errno != EINTR)
12975 ast_log(LOG_WARNING, "pri_event returned error %d (%s)\n", errno, strerror(errno));
12976
12977 if (e) {
12978 if (pri->debug)
12979 pri_dump_event(pri->dchans[which], e);
12980
12981 if (e->e != PRI_EVENT_DCHAN_DOWN) {
12982 if (!(pri->dchanavail[which] & DCHAN_UP)) {
12983 ast_verb(2, "%s D-Channel on span %d up\n", pri_order(which), pri->span);
12984 }
12985 pri->dchanavail[which] |= DCHAN_UP;
12986 } else if (pri->sig != SIG_BRI_PTMP) {
12987 if (pri->dchanavail[which] & DCHAN_UP) {
12988 ast_verb(2, "%s D-Channel on span %d down\n", pri_order(which), pri->span);
12989 }
12990 pri->dchanavail[which] &= ~DCHAN_UP;
12991 }
12992
12993 if ((e->e != PRI_EVENT_DCHAN_UP) && (e->e != PRI_EVENT_DCHAN_DOWN) && (pri->pri != pri->dchans[which]))
12994
12995 pri->pri = pri->dchans[which];
12996
12997 switch (e->e) {
12998 case PRI_EVENT_DCHAN_UP:
12999 pri->no_d_channels = 0;
13000 if (!pri->pri) pri_find_dchan(pri);
13001
13002
13003 time(&pri->lastreset);
13004
13005
13006 if (pri->resetinterval > -1) {
13007 pri->lastreset -= pri->resetinterval;
13008 pri->lastreset += 5;
13009 }
13010 pri->resetting = 0;
13011
13012 for (i = 0; i < pri->numchans; i++)
13013 if (pri->pvts[i]) {
13014 pri->pvts[i]->inalarm = 0;
13015 }
13016 break;
13017 case PRI_EVENT_DCHAN_DOWN:
13018 pri_find_dchan(pri);
13019 if (!pri_is_up(pri)) {
13020 pri->resetting = 0;
13021
13022 for (i = 0; i < pri->numchans; i++) {
13023 struct dahdi_pvt *p = pri->pvts[i];
13024 if (p) {
13025 if (!p->pri || !p->pri->pri || pri_get_timer(p->pri->pri, PRI_TIMER_T309) < 0) {
13026
13027 if (p->call) {
13028 if (p->pri && p->pri->pri) {
13029 pri_hangup(p->pri->pri, p->call, -1);
13030 pri_destroycall(p->pri->pri, p->call);
13031 p->call = NULL;
13032 } else
13033 ast_log(LOG_WARNING, "The PRI Call have not been destroyed\n");
13034 }
13035 if (p->realcall) {
13036 pri_hangup_all(p->realcall, pri);
13037 } else if (p->owner)
13038 p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13039 }
13040
13041
13042 if (p->sig != SIG_BRI_PTMP) {
13043 p->inalarm = 1;
13044 }
13045 }
13046 }
13047 }
13048 break;
13049 case PRI_EVENT_RESTART:
13050 if (e->restart.channel > -1) {
13051 chanpos = pri_find_principle(pri, e->restart.channel);
13052 if (chanpos < 0)
13053 ast_log(LOG_WARNING, "Restart requested on odd/unavailable channel number %d/%d on span %d\n",
13054 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
13055 else {
13056 ast_verb(3, "B-channel %d/%d restarted on span %d\n",
13057 PRI_SPAN(e->restart.channel), PRI_CHANNEL(e->restart.channel), pri->span);
13058 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13059 if (pri->pvts[chanpos]->call) {
13060 pri_destroycall(pri->pri, pri->pvts[chanpos]->call);
13061 pri->pvts[chanpos]->call = NULL;
13062 }
13063
13064 if (pri->pvts[chanpos]->realcall)
13065 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13066 else if (pri->pvts[chanpos]->owner)
13067 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13068 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13069 }
13070 } else {
13071 ast_verb(3, "Restart on requested on entire span %d\n", pri->span);
13072 for (x = 0; x < pri->numchans; x++)
13073 if (pri->pvts[x]) {
13074 ast_mutex_lock(&pri->pvts[x]->lock);
13075 if (pri->pvts[x]->call) {
13076 pri_destroycall(pri->pri, pri->pvts[x]->call);
13077 pri->pvts[x]->call = NULL;
13078 }
13079 if (pri->pvts[x]->realcall)
13080 pri_hangup_all(pri->pvts[x]->realcall, pri);
13081 else if (pri->pvts[x]->owner)
13082 pri->pvts[x]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13083 ast_mutex_unlock(&pri->pvts[x]->lock);
13084 }
13085 }
13086 break;
13087 case PRI_EVENT_KEYPAD_DIGIT:
13088 chanpos = pri_find_principle(pri, e->digit.channel);
13089 if (chanpos < 0) {
13090 ast_log(LOG_WARNING, "KEYPAD_DIGITs received on unconfigured channel %d/%d span %d\n",
13091 PRI_SPAN(e->digit.channel), PRI_CHANNEL(e->digit.channel), pri->span);
13092 } else {
13093 chanpos = pri_fixup_principle(pri, chanpos, e->digit.call);
13094 if (chanpos > -1) {
13095 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13096
13097 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && pri->pvts[chanpos]->call==e->digit.call && pri->pvts[chanpos]->owner) {
13098
13099 int digitlen = strlen(e->digit.digits);
13100 char digit;
13101 int i;
13102 for (i = 0; i < digitlen; i++) {
13103 digit = e->digit.digits[i];
13104 {
13105 struct ast_frame f = { AST_FRAME_DTMF, digit, };
13106 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13107 }
13108 }
13109 }
13110 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13111 }
13112 }
13113 break;
13114
13115 case PRI_EVENT_INFO_RECEIVED:
13116 chanpos = pri_find_principle(pri, e->ring.channel);
13117 if (chanpos < 0) {
13118 ast_log(LOG_WARNING, "INFO received on unconfigured channel %d/%d span %d\n",
13119 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
13120 } else {
13121 chanpos = pri_fixup_principle(pri, chanpos, e->ring.call);
13122 if (chanpos > -1) {
13123 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13124
13125 if ((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && pri->pvts[chanpos]->call==e->ring.call && pri->pvts[chanpos]->owner) {
13126
13127 int digitlen = strlen(e->ring.callednum);
13128 char digit;
13129 int i;
13130 for (i = 0; i < digitlen; i++) {
13131 digit = e->ring.callednum[i];
13132 {
13133 struct ast_frame f = { AST_FRAME_DTMF, digit, };
13134 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13135 }
13136 }
13137 }
13138 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13139 }
13140 }
13141 break;
13142 case PRI_EVENT_RING:
13143 crv = NULL;
13144 if (e->ring.channel == -1)
13145 chanpos = pri_find_empty_chan(pri, 1);
13146 else
13147 chanpos = pri_find_principle(pri, e->ring.channel);
13148
13149 if (chanpos < 0) {
13150 ast_log(LOG_WARNING, "Ring requested on unconfigured channel %d/%d span %d\n",
13151 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
13152 } else {
13153 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13154 if (pri->pvts[chanpos]->owner) {
13155 if (pri->pvts[chanpos]->call == e->ring.call) {
13156 ast_log(LOG_WARNING, "Duplicate setup requested on channel %d/%d already in use on span %d\n",
13157 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
13158 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13159 break;
13160 } else {
13161
13162 ast_debug(1, "Ring requested on channel %d/%d already in use or previously requested on span %d. Attempting to renegotiate channel.\n",
13163 PRI_SPAN(e->ring.channel), PRI_CHANNEL(e->ring.channel), pri->span);
13164 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13165 chanpos = -1;
13166 }
13167 }
13168 if (chanpos > -1)
13169 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13170 }
13171 if ((chanpos < 0) && (e->ring.flexible))
13172 chanpos = pri_find_empty_chan(pri, 1);
13173 if (chanpos > -1) {
13174 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13175 if (pri->switchtype == PRI_SWITCH_GR303_TMC) {
13176
13177 crv = pri_find_crv(pri, pri_get_crv(pri->pri, e->ring.call, NULL));
13178 if (crv)
13179 ast_mutex_lock(&crv->lock);
13180 if (!crv || crv->owner) {
13181 pri->pvts[chanpos]->call = NULL;
13182 if (crv) {
13183 if (crv->owner)
13184 crv->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13185 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);
13186 } else
13187 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);
13188 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_INVALID_CALL_REFERENCE);
13189 if (crv)
13190 ast_mutex_unlock(&crv->lock);
13191 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13192 break;
13193 }
13194 }
13195 pri->pvts[chanpos]->call = e->ring.call;
13196 apply_plan_to_number(plancallingnum, sizeof(plancallingnum), pri, e->ring.callingnum, e->ring.callingplan);
13197 if (pri->pvts[chanpos]->use_callerid) {
13198 ast_shrink_phone_number(plancallingnum);
13199 ast_copy_string(pri->pvts[chanpos]->cid_num, plancallingnum, sizeof(pri->pvts[chanpos]->cid_num));
13200 #ifdef PRI_ANI
13201 if (!ast_strlen_zero(e->ring.callingani)) {
13202 apply_plan_to_number(plancallingani, sizeof(plancallingani), pri, e->ring.callingani, e->ring.callingplanani);
13203 ast_shrink_phone_number(plancallingani);
13204 ast_copy_string(pri->pvts[chanpos]->cid_ani, plancallingani, sizeof(pri->pvts[chanpos]->cid_ani));
13205 } else {
13206 pri->pvts[chanpos]->cid_ani[0] = '\0';
13207 }
13208 #endif
13209 ast_copy_string(pri->pvts[chanpos]->cid_name, e->ring.callingname, sizeof(pri->pvts[chanpos]->cid_name));
13210 pri->pvts[chanpos]->cid_ton = e->ring.callingplan;
13211 } else {
13212 pri->pvts[chanpos]->cid_num[0] = '\0';
13213 pri->pvts[chanpos]->cid_ani[0] = '\0';
13214 pri->pvts[chanpos]->cid_name[0] = '\0';
13215 pri->pvts[chanpos]->cid_ton = 0;
13216 }
13217 apply_plan_to_number(pri->pvts[chanpos]->rdnis, sizeof(pri->pvts[chanpos]->rdnis), pri,
13218 e->ring.redirectingnum, e->ring.callingplanrdnis);
13219
13220
13221 ast_copy_string(pri->pvts[chanpos]->dnid, e->ring.callednum, sizeof(pri->pvts[chanpos]->dnid));
13222
13223
13224 if (pri->pvts[chanpos]->immediate) {
13225 ast_verb(3, "Going to extension s|1 because of immediate=yes\n");
13226 pri->pvts[chanpos]->exten[0] = 's';
13227 pri->pvts[chanpos]->exten[1] = '\0';
13228 }
13229
13230 else if (!ast_strlen_zero(e->ring.callednum)) {
13231 ast_copy_string(pri->pvts[chanpos]->exten, e->ring.callednum, sizeof(pri->pvts[chanpos]->exten));
13232 } else if (pri->overlapdial)
13233 pri->pvts[chanpos]->exten[0] = '\0';
13234 else {
13235
13236 pri->pvts[chanpos]->exten[0] = 's';
13237 pri->pvts[chanpos]->exten[1] = '\0';
13238 }
13239
13240 if (e->ring.complete && (ast_strlen_zero(e->ring.callednum))) {
13241 ast_verb(3, "Going to extension s|1 because of Complete received\n");
13242 pri->pvts[chanpos]->exten[0] = 's';
13243 pri->pvts[chanpos]->exten[1] = '\0';
13244 }
13245
13246
13247 if (((pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING) && ast_canmatch_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) ||
13248 ast_exists_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
13249
13250 int law;
13251 if (pri->switchtype != PRI_SWITCH_GR303_TMC) {
13252
13253 law = 1;
13254 if (ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_AUDIOMODE, &law) == -1)
13255 ast_log(LOG_WARNING, "Unable to set audio mode on channel %d to %d: %s\n", pri->pvts[chanpos]->channel, law, strerror(errno));
13256 }
13257 if (e->ring.layer1 == PRI_LAYER_1_ALAW)
13258 law = DAHDI_LAW_ALAW;
13259 else
13260 law = DAHDI_LAW_MULAW;
13261 res = dahdi_setlaw(pri->pvts[chanpos]->subs[SUB_REAL].dfd, law);
13262 if (res < 0)
13263 ast_log(LOG_WARNING, "Unable to set law on channel %d\n", pri->pvts[chanpos]->channel);
13264 res = set_actual_gain(pri->pvts[chanpos]->subs[SUB_REAL].dfd, 0, pri->pvts[chanpos]->rxgain, pri->pvts[chanpos]->txgain, law);
13265 if (res < 0)
13266 ast_log(LOG_WARNING, "Unable to set gains on channel %d\n", pri->pvts[chanpos]->channel);
13267 if (e->ring.complete || !(pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)) {
13268
13269 pri->pvts[chanpos]->proceeding = 1;
13270 pri_proceeding(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 0);
13271 } else {
13272 if (pri->switchtype != PRI_SWITCH_GR303_TMC)
13273 pri_need_more_info(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
13274 else
13275 pri_answer(pri->pri, e->ring.call, PVT_TO_CHANNEL(pri->pvts[chanpos]), 1);
13276 }
13277
13278 pri->pvts[chanpos]->callingpres = e->ring.callingpres;
13279
13280
13281 if (!e->ring.complete
13282 && (pri->overlapdial & DAHDI_OVERLAPDIAL_INCOMING)
13283 && ast_matchmore_extension(NULL, pri->pvts[chanpos]->context, pri->pvts[chanpos]->exten, 1, pri->pvts[chanpos]->cid_num)) {
13284
13285
13286
13287
13288 ast_mutex_unlock(&pri->lock);
13289 if (crv) {
13290
13291 pri_assign_bearer(crv, pri, pri->pvts[chanpos]);
13292 c = dahdi_new(crv, AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
13293 pri->pvts[chanpos]->owner = &inuse;
13294 ast_debug(1, "Started up crv %d:%d on bearer channel %d\n", pri->trunkgroup, crv->channel, crv->bearer->channel);
13295 } else {
13296 c = dahdi_new(pri->pvts[chanpos], AST_STATE_RESERVED, 0, SUB_REAL, law, e->ring.ctype);
13297 }
13298 ast_mutex_lock(&pri->lock);
13299 if (c) {
13300 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
13301 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
13302 }
13303 if (e->ring.ani2 >= 0) {
13304 snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
13305 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
13306 pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
13307 }
13308
13309 #ifdef SUPPORT_USERUSER
13310 if (!ast_strlen_zero(e->ring.useruserinfo)) {
13311 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
13312 }
13313 #endif
13314
13315 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
13316 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
13317 if (e->ring.redirectingreason >= 0)
13318 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
13319 }
13320 if (c && !ast_pthread_create_detached(&threadid, NULL, ss_thread, c)) {
13321 ast_verb(3, "Accepting overlap call from '%s' to '%s' on channel %d/%d, span %d\n",
13322 plancallingnum, S_OR(pri->pvts[chanpos]->exten, "<unspecified>"),
13323 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
13324 } else {
13325 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
13326 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
13327 if (c)
13328 ast_hangup(c);
13329 else {
13330 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
13331 pri->pvts[chanpos]->call = NULL;
13332 }
13333 }
13334 } else {
13335
13336
13337
13338
13339 ast_mutex_unlock(&pri->lock);
13340 c = dahdi_new(pri->pvts[chanpos], AST_STATE_RING, 0, SUB_REAL, law, e->ring.ctype);
13341 ast_mutex_lock(&pri->lock);
13342 if (c) {
13343
13344
13345
13346
13347
13348
13349
13350
13351 if (!ast_strlen_zero(e->ring.callingsubaddr)) {
13352 pbx_builtin_setvar_helper(c, "CALLINGSUBADDR", e->ring.callingsubaddr);
13353 }
13354 if (e->ring.ani2 >= 0) {
13355 snprintf(ani2str, sizeof(ani2str), "%d", e->ring.ani2);
13356 pbx_builtin_setvar_helper(c, "ANI2", ani2str);
13357 pri->pvts[chanpos]->cid_ani2 = e->ring.ani2;
13358 }
13359
13360 #ifdef SUPPORT_USERUSER
13361 if (!ast_strlen_zero(e->ring.useruserinfo)) {
13362 pbx_builtin_setvar_helper(c, "USERUSERINFO", e->ring.useruserinfo);
13363 }
13364 #endif
13365
13366 if (e->ring.redirectingreason >= 0)
13367 pbx_builtin_setvar_helper(c, "PRIREDIRECTREASON", redirectingreason2str(e->ring.redirectingreason));
13368
13369 snprintf(calledtonstr, sizeof(calledtonstr), "%d", e->ring.calledplan);
13370 pbx_builtin_setvar_helper(c, "CALLEDTON", calledtonstr);
13371 }
13372 if (c && !ast_pbx_start(c)) {
13373 ast_verb(3, "Accepting call from '%s' to '%s' on channel %d/%d, span %d\n",
13374 plancallingnum, pri->pvts[chanpos]->exten,
13375 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
13376
13377 dahdi_enable_ec(pri->pvts[chanpos]);
13378 } else {
13379 ast_log(LOG_WARNING, "Unable to start PBX on channel %d/%d, span %d\n",
13380 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span);
13381 if (c) {
13382 ast_hangup(c);
13383 } else {
13384 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_SWITCH_CONGESTION);
13385 pri->pvts[chanpos]->call = NULL;
13386 }
13387 }
13388 }
13389 } else {
13390 ast_verb(3, "Extension '%s' in context '%s' from '%s' does not exist. Rejecting call on channel %d/%d, span %d\n",
13391 pri->pvts[chanpos]->exten, pri->pvts[chanpos]->context, pri->pvts[chanpos]->cid_num, pri->pvts[chanpos]->logicalspan,
13392 pri->pvts[chanpos]->prioffset, pri->span);
13393 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_UNALLOCATED);
13394 pri->pvts[chanpos]->call = NULL;
13395 pri->pvts[chanpos]->exten[0] = '\0';
13396 }
13397 if (crv)
13398 ast_mutex_unlock(&crv->lock);
13399 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13400 } else {
13401 if (e->ring.flexible)
13402 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION);
13403 else
13404 pri_hangup(pri->pri, e->ring.call, PRI_CAUSE_REQUESTED_CHAN_UNAVAIL);
13405 }
13406 break;
13407 case PRI_EVENT_RINGING:
13408 chanpos = pri_find_principle(pri, e->ringing.channel);
13409 if (chanpos < 0) {
13410 ast_log(LOG_WARNING, "Ringing requested on unconfigured channel %d/%d span %d\n",
13411 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
13412 } else {
13413 chanpos = pri_fixup_principle(pri, chanpos, e->ringing.call);
13414 if (chanpos < 0) {
13415 ast_log(LOG_WARNING, "Ringing requested on channel %d/%d not in use on span %d\n",
13416 PRI_SPAN(e->ringing.channel), PRI_CHANNEL(e->ringing.channel), pri->span);
13417 } else {
13418 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13419 if (ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
13420 dahdi_enable_ec(pri->pvts[chanpos]);
13421 pri->pvts[chanpos]->subs[SUB_REAL].needringing = 1;
13422 pri->pvts[chanpos]->alerting = 1;
13423 } else
13424 ast_debug(1, "Deferring ringing notification because of extra digits to dial...\n");
13425
13426 if (
13427 #ifdef PRI_PROGRESS_MASK
13428 e->ringing.progressmask & PRI_PROG_INBAND_AVAILABLE
13429 #else
13430 e->ringing.progress == 8
13431 #endif
13432 ) {
13433
13434 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
13435
13436 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features & ~DSP_PROGRESS_RINGING);
13437 pri->pvts[chanpos]->dsp_features = 0;
13438 }
13439 }
13440
13441 #ifdef SUPPORT_USERUSER
13442 if (!ast_strlen_zero(e->ringing.useruserinfo)) {
13443 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13444 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13445 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->ringing.useruserinfo);
13446 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13447 }
13448 #endif
13449
13450 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13451 }
13452 }
13453 break;
13454 case PRI_EVENT_PROGRESS:
13455
13456 chanpos = pri_find_principle(pri, e->proceeding.channel);
13457 if (chanpos > -1) {
13458 if ((!pri->pvts[chanpos]->progress)
13459 #ifdef PRI_PROGRESS_MASK
13460 || (e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE)
13461 #else
13462 || (e->proceeding.progress == 8)
13463 #endif
13464 ) {
13465 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROGRESS, };
13466
13467 if (e->proceeding.cause > -1) {
13468 ast_verb(3, "PROGRESS with cause code %d received\n", e->proceeding.cause);
13469
13470
13471 if (e->proceeding.cause == AST_CAUSE_USER_BUSY) {
13472 if (pri->pvts[chanpos]->owner) {
13473 ast_verb(3, "PROGRESS with 'user busy' received, signalling AST_CONTROL_BUSY instead of AST_CONTROL_PROGRESS\n");
13474
13475 pri->pvts[chanpos]->owner->hangupcause = e->proceeding.cause;
13476 f.subclass = AST_CONTROL_BUSY;
13477 }
13478 }
13479 }
13480
13481 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13482 ast_debug(1, "Queuing frame from PRI_EVENT_PROGRESS on channel %d/%d span %d\n",
13483 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
13484 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13485 if (
13486 #ifdef PRI_PROGRESS_MASK
13487 e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
13488 #else
13489 e->proceeding.progress == 8
13490 #endif
13491 ) {
13492
13493 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
13494 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
13495 pri->pvts[chanpos]->dsp_features = 0;
13496 }
13497
13498 f.subclass = AST_CONTROL_PROGRESS;
13499 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13500 }
13501 pri->pvts[chanpos]->progress = 1;
13502 pri->pvts[chanpos]->dialing = 0;
13503 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13504 }
13505 }
13506 break;
13507 case PRI_EVENT_PROCEEDING:
13508 chanpos = pri_find_principle(pri, e->proceeding.channel);
13509 if (chanpos > -1) {
13510 if (!pri->pvts[chanpos]->proceeding) {
13511 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_PROCEEDING, };
13512
13513 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13514 ast_debug(1, "Queuing frame from PRI_EVENT_PROCEEDING on channel %d/%d span %d\n",
13515 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset,pri->span);
13516 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13517 if (
13518 #ifdef PRI_PROGRESS_MASK
13519 e->proceeding.progressmask & PRI_PROG_INBAND_AVAILABLE
13520 #else
13521 e->proceeding.progress == 8
13522 #endif
13523 ) {
13524
13525 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
13526 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
13527 pri->pvts[chanpos]->dsp_features = 0;
13528 }
13529
13530 f.subclass = AST_CONTROL_PROGRESS;
13531 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13532 }
13533 pri->pvts[chanpos]->proceeding = 1;
13534 pri->pvts[chanpos]->dialing = 0;
13535 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13536 }
13537 }
13538 break;
13539 case PRI_EVENT_FACNAME:
13540 chanpos = pri_find_principle(pri, e->facname.channel);
13541 if (chanpos < 0) {
13542 ast_log(LOG_WARNING, "Facility Name requested on unconfigured channel %d/%d span %d\n",
13543 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
13544 } else {
13545 chanpos = pri_fixup_principle(pri, chanpos, e->facname.call);
13546 if (chanpos < 0) {
13547 ast_log(LOG_WARNING, "Facility Name requested on channel %d/%d not in use on span %d\n",
13548 PRI_SPAN(e->facname.channel), PRI_CHANNEL(e->facname.channel), pri->span);
13549 } else if (pri->pvts[chanpos]->use_callerid) {
13550
13551 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13552 ast_copy_string(pri->pvts[chanpos]->lastcid_num, e->facname.callingnum, sizeof(pri->pvts[chanpos]->lastcid_num));
13553 ast_copy_string(pri->pvts[chanpos]->lastcid_name, e->facname.callingname, sizeof(pri->pvts[chanpos]->lastcid_name));
13554 pri->pvts[chanpos]->subs[SUB_REAL].needcallerid = 1;
13555 dahdi_enable_ec(pri->pvts[chanpos]);
13556 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13557 }
13558 }
13559 break;
13560 case PRI_EVENT_ANSWER:
13561 chanpos = pri_find_principle(pri, e->answer.channel);
13562 if (chanpos < 0) {
13563 ast_log(LOG_WARNING, "Answer on unconfigured channel %d/%d span %d\n",
13564 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
13565 } else {
13566 chanpos = pri_fixup_principle(pri, chanpos, e->answer.call);
13567 if (chanpos < 0) {
13568 ast_log(LOG_WARNING, "Answer requested on channel %d/%d not in use on span %d\n",
13569 PRI_SPAN(e->answer.channel), PRI_CHANNEL(e->answer.channel), pri->span);
13570 } else {
13571 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13572
13573
13574
13575
13576
13577 if (pri->pvts[chanpos]->dsp && pri->pvts[chanpos]->dsp_features) {
13578 ast_dsp_set_features(pri->pvts[chanpos]->dsp, pri->pvts[chanpos]->dsp_features);
13579 pri->pvts[chanpos]->dsp_features = 0;
13580 }
13581 if (pri->pvts[chanpos]->realcall && (pri->pvts[chanpos]->realcall->sig == SIG_FXSKS)) {
13582 ast_debug(1, "Starting up GR-303 trunk now that we got CONNECT...\n");
13583 x = DAHDI_START;
13584 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
13585 if (res < 0) {
13586 if (errno != EINPROGRESS) {
13587 ast_log(LOG_WARNING, "Unable to start channel: %s\n", strerror(errno));
13588 }
13589 }
13590 } else if (!ast_strlen_zero(pri->pvts[chanpos]->dop.dialstr)) {
13591 pri->pvts[chanpos]->dialing = 1;
13592
13593 res = ioctl(pri->pvts[chanpos]->subs[SUB_REAL].dfd, DAHDI_DIAL, &pri->pvts[chanpos]->dop);
13594 if (res < 0) {
13595 ast_log(LOG_WARNING, "Unable to initiate dialing on trunk channel %d: %s\n", pri->pvts[chanpos]->channel, strerror(errno));
13596 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
13597 } else
13598 ast_debug(1, "Sent deferred digit string: %s\n", pri->pvts[chanpos]->dop.dialstr);
13599
13600 pri->pvts[chanpos]->dop.dialstr[0] = '\0';
13601 } else if (pri->pvts[chanpos]->confirmanswer) {
13602 ast_debug(1, "Waiting on answer confirmation on channel %d!\n", pri->pvts[chanpos]->channel);
13603 } else {
13604 pri->pvts[chanpos]->dialing = 0;
13605 pri->pvts[chanpos]->proceeding = 1;
13606 pri->pvts[chanpos]->subs[SUB_REAL].needanswer =1;
13607
13608 dahdi_enable_ec(pri->pvts[chanpos]);
13609 }
13610
13611 #ifdef SUPPORT_USERUSER
13612 if (!ast_strlen_zero(e->answer.useruserinfo)) {
13613 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13614 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13615 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->answer.useruserinfo);
13616 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13617 }
13618 #endif
13619
13620 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13621 }
13622 }
13623 break;
13624 case PRI_EVENT_HANGUP:
13625 chanpos = pri_find_principle(pri, e->hangup.channel);
13626 if (chanpos < 0) {
13627 ast_log(LOG_WARNING, "Hangup requested on unconfigured channel %d/%d span %d\n",
13628 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13629 } else {
13630 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
13631 if (chanpos > -1) {
13632 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13633 if (!pri->pvts[chanpos]->alreadyhungup) {
13634
13635 pri->pvts[chanpos]->alreadyhungup = 1;
13636 if (pri->pvts[chanpos]->realcall)
13637 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13638 else if (pri->pvts[chanpos]->owner) {
13639
13640 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
13641 switch (pri->pvts[chanpos]->owner->_state) {
13642 case AST_STATE_BUSY:
13643 case AST_STATE_UP:
13644 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13645 break;
13646 default:
13647 if (!pri->pvts[chanpos]->outgoing) {
13648
13649
13650
13651
13652 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13653 break;
13654 }
13655 switch (e->hangup.cause) {
13656 case PRI_CAUSE_USER_BUSY:
13657 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
13658 break;
13659 case PRI_CAUSE_CALL_REJECTED:
13660 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
13661 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
13662 case PRI_CAUSE_SWITCH_CONGESTION:
13663 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
13664 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
13665 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
13666 break;
13667 default:
13668 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13669 }
13670 break;
13671 }
13672 }
13673 ast_verb(3, "Channel %d/%d, span %d got hangup, cause %d\n",
13674 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, e->hangup.cause);
13675 } else {
13676 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
13677 pri->pvts[chanpos]->call = NULL;
13678 }
13679 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
13680 ast_verb(3, "Forcing restart of channel %d/%d on span %d since channel reported in use\n",
13681 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13682 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
13683 pri->pvts[chanpos]->resetting = 1;
13684 }
13685 if (e->hangup.aoc_units > -1)
13686 ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
13687 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
13688
13689 #ifdef SUPPORT_USERUSER
13690 if (pri->pvts[chanpos]->owner && !ast_strlen_zero(e->hangup.useruserinfo)) {
13691 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13692 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13693 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
13694 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13695 }
13696 #endif
13697
13698 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13699 } else {
13700 ast_log(LOG_WARNING, "Hangup on bad channel %d/%d on span %d\n",
13701 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13702 }
13703 }
13704 break;
13705 #ifndef PRI_EVENT_HANGUP_REQ
13706 #error please update libpri
13707 #endif
13708 case PRI_EVENT_HANGUP_REQ:
13709 chanpos = pri_find_principle(pri, e->hangup.channel);
13710 if (chanpos < 0) {
13711 ast_log(LOG_WARNING, "Hangup REQ requested on unconfigured channel %d/%d span %d\n",
13712 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13713 } else {
13714 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
13715 if (chanpos > -1) {
13716 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13717 if (pri->pvts[chanpos]->realcall)
13718 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13719 else if (pri->pvts[chanpos]->owner) {
13720 pri->pvts[chanpos]->owner->hangupcause = e->hangup.cause;
13721 switch (pri->pvts[chanpos]->owner->_state) {
13722 case AST_STATE_BUSY:
13723 case AST_STATE_UP:
13724 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13725 break;
13726 default:
13727 if (!pri->pvts[chanpos]->outgoing) {
13728
13729
13730
13731
13732 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13733 break;
13734 }
13735 switch (e->hangup.cause) {
13736 case PRI_CAUSE_USER_BUSY:
13737 pri->pvts[chanpos]->subs[SUB_REAL].needbusy =1;
13738 break;
13739 case PRI_CAUSE_CALL_REJECTED:
13740 case PRI_CAUSE_NETWORK_OUT_OF_ORDER:
13741 case PRI_CAUSE_NORMAL_CIRCUIT_CONGESTION:
13742 case PRI_CAUSE_SWITCH_CONGESTION:
13743 case PRI_CAUSE_DESTINATION_OUT_OF_ORDER:
13744 case PRI_CAUSE_NORMAL_TEMPORARY_FAILURE:
13745 pri->pvts[chanpos]->subs[SUB_REAL].needcongestion =1;
13746 break;
13747 default:
13748 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13749 break;
13750 }
13751 break;
13752 }
13753 ast_verb(3, "Channel %d/%d, span %d got hangup request, cause %d\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span, e->hangup.cause);
13754 if (e->hangup.aoc_units > -1)
13755 ast_verb(3, "Channel %d/%d, span %d received AOC-E charging %d unit%s\n",
13756 pri->pvts[chanpos]->logicalspan, pri->pvts[chanpos]->prioffset, pri->span, (int)e->hangup.aoc_units, (e->hangup.aoc_units == 1) ? "" : "s");
13757 } else {
13758 pri_hangup(pri->pri, pri->pvts[chanpos]->call, e->hangup.cause);
13759 pri->pvts[chanpos]->call = NULL;
13760 }
13761 if (e->hangup.cause == PRI_CAUSE_REQUESTED_CHAN_UNAVAIL) {
13762 ast_verb(3, "Forcing restart of channel %d/%d span %d since channel reported in use\n",
13763 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13764 pri_reset(pri->pri, PVT_TO_CHANNEL(pri->pvts[chanpos]));
13765 pri->pvts[chanpos]->resetting = 1;
13766 }
13767
13768 #ifdef SUPPORT_USERUSER
13769 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
13770 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13771 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13772 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
13773 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13774 }
13775 #endif
13776
13777 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13778 } else {
13779 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);
13780 }
13781 }
13782 break;
13783 case PRI_EVENT_HANGUP_ACK:
13784 chanpos = pri_find_principle(pri, e->hangup.channel);
13785 if (chanpos < 0) {
13786 ast_log(LOG_WARNING, "Hangup ACK requested on unconfigured channel number %d/%d span %d\n",
13787 PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13788 } else {
13789 chanpos = pri_fixup_principle(pri, chanpos, e->hangup.call);
13790 if (chanpos > -1) {
13791 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13792 pri->pvts[chanpos]->call = NULL;
13793 pri->pvts[chanpos]->resetting = 0;
13794 if (pri->pvts[chanpos]->owner) {
13795 ast_verb(3, "Channel %d/%d, span %d got hangup ACK\n", PRI_SPAN(e->hangup.channel), PRI_CHANNEL(e->hangup.channel), pri->span);
13796 }
13797
13798 #ifdef SUPPORT_USERUSER
13799 if (!ast_strlen_zero(e->hangup.useruserinfo)) {
13800 struct ast_channel *owner = pri->pvts[chanpos]->owner;
13801 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13802 pbx_builtin_setvar_helper(owner, "USERUSERINFO", e->hangup.useruserinfo);
13803 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13804 }
13805 #endif
13806
13807 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13808 }
13809 }
13810 break;
13811 case PRI_EVENT_CONFIG_ERR:
13812 ast_log(LOG_WARNING, "PRI Error on span %d: %s\n", pri->span, e->err.err);
13813 break;
13814 case PRI_EVENT_RESTART_ACK:
13815 chanpos = pri_find_principle(pri, e->restartack.channel);
13816 if (chanpos < 0) {
13817
13818
13819
13820 for (x = 0; x < pri->numchans; x++) {
13821 if (pri->pvts[x] && pri->pvts[x]->resetting) {
13822 chanpos = x;
13823 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13824 ast_debug(1, "Assuming restart ack is really for channel %d/%d span %d\n", pri->pvts[chanpos]->logicalspan,
13825 pri->pvts[chanpos]->prioffset, pri->span);
13826 if (pri->pvts[chanpos]->realcall)
13827 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13828 else if (pri->pvts[chanpos]->owner) {
13829 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d with owner on span %d\n", pri->pvts[chanpos]->logicalspan,
13830 pri->pvts[chanpos]->prioffset, pri->span);
13831 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13832 }
13833 pri->pvts[chanpos]->resetting = 0;
13834 ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
13835 pri->pvts[chanpos]->prioffset, pri->span);
13836 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13837 if (pri->resetting)
13838 pri_check_restart(pri);
13839 break;
13840 }
13841 }
13842 if (chanpos < 0) {
13843 ast_log(LOG_WARNING, "Restart ACK requested on strange channel %d/%d span %d\n",
13844 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
13845 }
13846 } else {
13847 if (pri->pvts[chanpos]) {
13848 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13849 if (pri->pvts[chanpos]->realcall)
13850 pri_hangup_all(pri->pvts[chanpos]->realcall, pri);
13851 else if (pri->pvts[chanpos]->owner) {
13852 ast_log(LOG_WARNING, "Got restart ack on channel %d/%d span %d with owner\n",
13853 PRI_SPAN(e->restartack.channel), PRI_CHANNEL(e->restartack.channel), pri->span);
13854 pri->pvts[chanpos]->owner->_softhangup |= AST_SOFTHANGUP_DEV;
13855 }
13856 pri->pvts[chanpos]->resetting = 0;
13857 pri->pvts[chanpos]->inservice = 1;
13858 ast_verb(3, "B-channel %d/%d successfully restarted on span %d\n", pri->pvts[chanpos]->logicalspan,
13859 pri->pvts[chanpos]->prioffset, pri->span);
13860 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13861 if (pri->resetting)
13862 pri_check_restart(pri);
13863 }
13864 }
13865 break;
13866 case PRI_EVENT_SETUP_ACK:
13867 chanpos = pri_find_principle(pri, e->setup_ack.channel);
13868 if (chanpos < 0) {
13869 ast_log(LOG_WARNING, "Received SETUP_ACKNOWLEDGE on unconfigured channel %d/%d span %d\n",
13870 PRI_SPAN(e->setup_ack.channel), PRI_CHANNEL(e->setup_ack.channel), pri->span);
13871 } else {
13872 chanpos = pri_fixup_principle(pri, chanpos, e->setup_ack.call);
13873 if (chanpos > -1) {
13874 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13875 pri->pvts[chanpos]->setup_ack = 1;
13876
13877 for (x = 0;x < strlen(pri->pvts[chanpos]->dialdest); x++) {
13878 ast_debug(1, "Sending pending digit '%c'\n", pri->pvts[chanpos]->dialdest[x]);
13879 pri_information(pri->pri, pri->pvts[chanpos]->call,
13880 pri->pvts[chanpos]->dialdest[x]);
13881 }
13882 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13883 } else
13884 ast_log(LOG_WARNING, "Unable to move channel %d!\n", e->setup_ack.channel);
13885 }
13886 break;
13887 case PRI_EVENT_NOTIFY:
13888 chanpos = pri_find_principle(pri, e->notify.channel);
13889 if (chanpos < 0) {
13890 ast_log(LOG_WARNING, "Received NOTIFY on unconfigured channel %d/%d span %d\n",
13891 PRI_SPAN(e->notify.channel), PRI_CHANNEL(e->notify.channel), pri->span);
13892 } else if (!pri->discardremoteholdretrieval) {
13893 struct ast_frame f = { AST_FRAME_CONTROL, };
13894 ast_mutex_lock(&pri->pvts[chanpos]->lock);
13895 switch (e->notify.info) {
13896 case PRI_NOTIFY_REMOTE_HOLD:
13897 f.subclass = AST_CONTROL_HOLD;
13898 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13899 break;
13900 case PRI_NOTIFY_REMOTE_RETRIEVAL:
13901 f.subclass = AST_CONTROL_UNHOLD;
13902 dahdi_queue_frame(pri->pvts[chanpos], &f, pri);
13903 break;
13904 }
13905 ast_mutex_unlock(&pri->pvts[chanpos]->lock);
13906 }
13907 break;
13908 default:
13909 ast_debug(1, "Event: %d\n", e->e);
13910 }
13911 }
13912 ast_mutex_unlock(&pri->lock);
13913 }
13914
13915 return NULL;
13916 }
13917 #endif
13918
13919 #if defined(HAVE_PRI)
13920 static int start_pri(struct dahdi_pri *pri)
13921 {
13922 int res, x;
13923 struct dahdi_params p;
13924 struct dahdi_bufferinfo bi;
13925 struct dahdi_spaninfo si;
13926 int i;
13927
13928 for (i = 0; i < NUM_DCHANS; i++) {
13929 if (!pri->dchannels[i])
13930 break;
13931 pri->fds[i] = open("/dev/dahdi/channel", O_RDWR);
13932 x = pri->dchannels[i];
13933 if ((pri->fds[i] < 0) || (ioctl(pri->fds[i],DAHDI_SPECIFY,&x) == -1)) {
13934 ast_log(LOG_ERROR, "Unable to open D-channel %d (%s)\n", x, strerror(errno));
13935 return -1;
13936 }
13937 memset(&p, 0, sizeof(p));
13938 res = ioctl(pri->fds[i], DAHDI_GET_PARAMS, &p);
13939 if (res) {
13940 dahdi_close_pri_fd(pri, i);
13941 ast_log(LOG_ERROR, "Unable to get parameters for D-channel %d (%s)\n", x, strerror(errno));
13942 return -1;
13943 }
13944 if ((p.sigtype != DAHDI_SIG_HDLCFCS) && (p.sigtype != DAHDI_SIG_HARDHDLC)) {
13945 dahdi_close_pri_fd(pri, i);
13946 ast_log(LOG_ERROR, "D-channel %d is not in HDLC/FCS mode.\n", x);
13947 return -1;
13948 }
13949 memset(&si, 0, sizeof(si));
13950 res = ioctl(pri->fds[i], DAHDI_SPANSTAT, &si);
13951 if (res) {
13952 dahdi_close_pri_fd(pri, i);
13953 ast_log(LOG_ERROR, "Unable to get span state for D-channel %d (%s)\n", x, strerror(errno));
13954 }
13955 if (!si.alarms)
13956 pri->dchanavail[i] |= DCHAN_NOTINALARM;
13957 else
13958 pri->dchanavail[i] &= ~DCHAN_NOTINALARM;
13959 memset(&bi, 0, sizeof(bi));
13960 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
13961 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
13962 bi.numbufs = 32;
13963 bi.bufsize = 1024;
13964 if (ioctl(pri->fds[i], DAHDI_SET_BUFINFO, &bi)) {
13965 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d: %s\n", x, strerror(errno));
13966 dahdi_close_pri_fd(pri, i);
13967 return -1;
13968 }
13969 switch (pri->sig) {
13970 case SIG_BRI:
13971 pri->dchans[i] = pri_new_bri(pri->fds[i], 1, pri->nodetype, pri->switchtype);
13972 break;
13973 case SIG_BRI_PTMP:
13974 pri->dchans[i] = pri_new_bri(pri->fds[i], 0, pri->nodetype, pri->switchtype);
13975 break;
13976 default:
13977 pri->dchans[i] = pri_new(pri->fds[i], pri->nodetype, pri->switchtype);
13978 break;
13979 }
13980
13981 if (pri->switchtype == PRI_SWITCH_GR303_TMC)
13982 pri->overlapdial |= DAHDI_OVERLAPDIAL_BOTH;
13983 pri_set_overlapdial(pri->dchans[i],(pri->overlapdial & DAHDI_OVERLAPDIAL_OUTGOING)?1:0);
13984 #ifdef HAVE_PRI_PROG_W_CAUSE
13985 pri_set_chan_mapping_logical(pri->dchans[i], pri->qsigchannelmapping == DAHDI_CHAN_MAPPING_LOGICAL);
13986 #endif
13987 #ifdef HAVE_PRI_INBANDDISCONNECT
13988 pri_set_inbanddisconnect(pri->dchans[i], pri->inbanddisconnect);
13989 #endif
13990
13991 if (i)
13992 pri_enslave(pri->dchans[0], pri->dchans[i]);
13993 if (!pri->dchans[i]) {
13994 dahdi_close_pri_fd(pri, i);
13995 ast_log(LOG_ERROR, "Unable to create PRI structure\n");
13996 return -1;
13997 }
13998 pri_set_debug(pri->dchans[i], DEFAULT_PRI_DEBUG);
13999 pri_set_nsf(pri->dchans[i], pri->nsf);
14000 #ifdef PRI_GETSET_TIMERS
14001 for (x = 0; x < PRI_MAX_TIMERS; x++) {
14002 if (pritimers[x] != 0)
14003 pri_set_timer(pri->dchans[i], x, pritimers[x]);
14004 }
14005 #endif
14006 }
14007
14008 pri->pri = pri->dchans[0];
14009 pri->resetpos = -1;
14010 if (ast_pthread_create_background(&pri->master, NULL, pri_dchannel, pri)) {
14011 for (i = 0; i < NUM_DCHANS; i++) {
14012 if (!pri->dchannels[i])
14013 break;
14014 dahdi_close_pri_fd(pri, i);
14015 }
14016 ast_log(LOG_ERROR, "Unable to spawn D-channel: %s\n", strerror(errno));
14017 return -1;
14018 }
14019 return 0;
14020 }
14021 #endif
14022
14023 #if defined(HAVE_PRI)
14024 static char *complete_span_helper(const char *line, const char *word, int pos, int state, int rpos)
14025 {
14026 int which, span;
14027 char *ret = NULL;
14028
14029 if (pos != rpos)
14030 return ret;
14031
14032 for (which = span = 0; span < NUM_SPANS; span++) {
14033 if (pris[span].pri && ++which > state) {
14034 if (asprintf(&ret, "%d", span + 1) < 0) {
14035 ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
14036 }
14037 break;
14038 }
14039 }
14040 return ret;
14041 }
14042 #endif
14043
14044 #if defined(HAVE_PRI)
14045 static char *complete_span_4(const char *line, const char *word, int pos, int state)
14046 {
14047 return complete_span_helper(line,word,pos,state,3);
14048 }
14049 #endif
14050
14051 #if defined(HAVE_PRI)
14052 static char *handle_pri_set_debug_file(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14053 {
14054 int myfd;
14055 switch (cmd) {
14056 case CLI_INIT:
14057 e->command = "pri set debug file";
14058 e->usage = "Usage: pri set debug file [output-file]\n"
14059 " Sends PRI debug output to the specified output file\n";
14060 return NULL;
14061 case CLI_GENERATE:
14062 return NULL;
14063 }
14064 if (a->argc < 5)
14065 return CLI_SHOWUSAGE;
14066
14067 if (ast_strlen_zero(a->argv[4]))
14068 return CLI_SHOWUSAGE;
14069
14070 myfd = open(a->argv[4], O_CREAT|O_WRONLY, AST_FILE_MODE);
14071 if (myfd < 0) {
14072 ast_cli(a->fd, "Unable to open '%s' for writing\n", a->argv[4]);
14073 return CLI_SUCCESS;
14074 }
14075
14076 ast_mutex_lock(&pridebugfdlock);
14077
14078 if (pridebugfd >= 0)
14079 close(pridebugfd);
14080
14081 pridebugfd = myfd;
14082 ast_copy_string(pridebugfilename,a->argv[4],sizeof(pridebugfilename));
14083 ast_mutex_unlock(&pridebugfdlock);
14084 ast_cli(a->fd, "PRI debug output will be sent to '%s'\n", a->argv[4]);
14085 return CLI_SUCCESS;
14086 }
14087 #endif
14088
14089 #if defined(HAVE_PRI)
14090 static char *handle_pri_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14091 {
14092 int span;
14093 int x;
14094 int level = 0;
14095 switch (cmd) {
14096 case CLI_INIT:
14097 e->command = "pri set debug {on|off|0|1|2} span";
14098 e->usage =
14099 "Usage: pri set debug {<level>|on|off} span <span>\n"
14100 " Enables debugging on a given PRI span\n";
14101 return NULL;
14102 case CLI_GENERATE:
14103 return complete_span_4(a->line, a->word, a->pos, a->n);
14104 }
14105 if (a->argc < 6) {
14106 return CLI_SHOWUSAGE;
14107 }
14108
14109 if (!strcasecmp(a->argv[3], "on")) {
14110 level = 1;
14111 } else if (!strcasecmp(a->argv[3], "off")) {
14112 level = 0;
14113 } else {
14114 level = atoi(a->argv[3]);
14115 }
14116 span = atoi(a->argv[5]);
14117 if ((span < 1) || (span > NUM_SPANS)) {
14118 ast_cli(a->fd, "Invalid span %s. Should be a number %d to %d\n", a->argv[5], 1, NUM_SPANS);
14119 return CLI_SUCCESS;
14120 }
14121 if (!pris[span-1].pri) {
14122 ast_cli(a->fd, "No PRI running on span %d\n", span);
14123 return CLI_SUCCESS;
14124 }
14125 for (x = 0; x < NUM_DCHANS; x++) {
14126 if (pris[span-1].dchans[x]) {
14127 if (level == 1) {
14128 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU |
14129 PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE |
14130 PRI_DEBUG_Q921_STATE);
14131 ast_cli(a->fd, "Enabled debugging on span %d\n", span);
14132 } else if (level == 0) {
14133 pri_set_debug(pris[span-1].dchans[x], 0);
14134
14135 ast_mutex_lock(&pridebugfdlock);
14136 close(pridebugfd);
14137 pridebugfd = -1;
14138 ast_cli(a->fd, "PRI debug output to file disabled\n");
14139 ast_mutex_unlock(&pridebugfdlock);
14140 } else {
14141 pri_set_debug(pris[span-1].dchans[x], PRI_DEBUG_APDU |
14142 PRI_DEBUG_Q931_DUMP | PRI_DEBUG_Q931_STATE |
14143 PRI_DEBUG_Q921_RAW | PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_STATE);
14144 ast_cli(a->fd, "Enabled debugging on span %d\n", span);
14145 }
14146 }
14147 }
14148 return CLI_SUCCESS;
14149 }
14150 #endif
14151
14152 #if defined(HAVE_PRI)
14153 static void build_status(char *s, size_t len, int status, int active)
14154 {
14155 if (!s || len < 1) {
14156 return;
14157 }
14158 s[0] = '\0';
14159 if (status & DCHAN_PROVISIONED)
14160 strncat(s, "Provisioned, ", len - strlen(s) - 1);
14161 if (!(status & DCHAN_NOTINALARM))
14162 strncat(s, "In Alarm, ", len - strlen(s) - 1);
14163 if (status & DCHAN_UP)
14164 strncat(s, "Up", len - strlen(s) - 1);
14165 else
14166 strncat(s, "Down", len - strlen(s) - 1);
14167 if (active)
14168 strncat(s, ", Active", len - strlen(s) - 1);
14169 else
14170 strncat(s, ", Standby", len - strlen(s) - 1);
14171 s[len - 1] = '\0';
14172 }
14173 #endif
14174
14175 #if defined(HAVE_PRI)
14176 static char *handle_pri_show_spans(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14177 {
14178 int span;
14179 int x;
14180 char status[256];
14181
14182 switch (cmd) {
14183 case CLI_INIT:
14184 e->command = "pri show spans";
14185 e->usage =
14186 "Usage: pri show spans\n"
14187 " Displays PRI Information\n";
14188 return NULL;
14189 case CLI_GENERATE:
14190 return NULL;
14191 }
14192
14193 if (a->argc != 3)
14194 return CLI_SHOWUSAGE;
14195
14196 for (span = 0; span < NUM_SPANS; span++) {
14197 if (pris[span].pri) {
14198 for (x = 0; x < NUM_DCHANS; x++) {
14199 if (pris[span].dchannels[x]) {
14200 build_status(status, sizeof(status), pris[span].dchanavail[x], pris[span].dchans[x] == pris[span].pri);
14201 ast_cli(a->fd, "PRI span %d/%d: %s\n", span + 1, x, status);
14202 }
14203 }
14204 }
14205 }
14206 return CLI_SUCCESS;
14207 }
14208 #endif
14209
14210 #if defined(HAVE_PRI)
14211 static char *handle_pri_show_span(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14212 {
14213 int span;
14214 int x;
14215 char status[256];
14216 switch (cmd) {
14217 case CLI_INIT:
14218 e->command = "pri show span";
14219 e->usage =
14220 "Usage: pri show span <span>\n"
14221 " Displays PRI Information on a given PRI span\n";
14222 return NULL;
14223 case CLI_GENERATE:
14224 return complete_span_4(a->line, a->word, a->pos, a->n);
14225 }
14226
14227 if (a->argc < 4)
14228 return CLI_SHOWUSAGE;
14229 span = atoi(a->argv[3]);
14230 if ((span < 1) || (span > NUM_SPANS)) {
14231 ast_cli(a->fd, "Invalid span '%s'. Should be a number from %d to %d\n", a->argv[3], 1, NUM_SPANS);
14232 return CLI_SUCCESS;
14233 }
14234 if (!pris[span-1].pri) {
14235 ast_cli(a->fd, "No PRI running on span %d\n", span);
14236 return CLI_SUCCESS;
14237 }
14238 for (x = 0; x < NUM_DCHANS; x++) {
14239 if (pris[span-1].dchannels[x]) {
14240 #ifdef PRI_DUMP_INFO_STR
14241 char *info_str = NULL;
14242 #endif
14243 ast_cli(a->fd, "%s D-channel: %d\n", pri_order(x), pris[span-1].dchannels[x]);
14244 build_status(status, sizeof(status), pris[span-1].dchanavail[x], pris[span-1].dchans[x] == pris[span-1].pri);
14245 ast_cli(a->fd, "Status: %s\n", status);
14246 #ifdef PRI_DUMP_INFO_STR
14247 info_str = pri_dump_info_str(pris[span-1].pri);
14248 if (info_str) {
14249 ast_cli(a->fd, "%s", info_str);
14250 ast_free(info_str);
14251 }
14252 #else
14253 pri_dump_info(pris[span-1].pri);
14254 #endif
14255 ast_cli(a->fd, "Overlap Recv: %s\n\n", (pris[span-1].overlapdial & DAHDI_OVERLAPDIAL_INCOMING)?"Yes":"No");
14256 }
14257 }
14258 return CLI_SUCCESS;
14259 }
14260 #endif
14261
14262 #if defined(HAVE_PRI)
14263 static char *handle_pri_show_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14264 {
14265 int x;
14266 int span;
14267 int count=0;
14268 int debug=0;
14269
14270 switch (cmd) {
14271 case CLI_INIT:
14272 e->command = "pri show debug";
14273 e->usage =
14274 "Usage: pri show debug\n"
14275 " Show the debug state of pri spans\n";
14276 return NULL;
14277 case CLI_GENERATE:
14278 return NULL;
14279 }
14280
14281 for (span = 0; span < NUM_SPANS; span++) {
14282 if (pris[span].pri) {
14283 for (x = 0; x < NUM_DCHANS; x++) {
14284 debug = 0;
14285 if (pris[span].dchans[x]) {
14286 debug = pri_get_debug(pris[span].dchans[x]);
14287 ast_cli(a->fd, "Span %d: Debug: %s\tIntense: %s\n", span+1, (debug&PRI_DEBUG_Q931_STATE)? "Yes" : "No" ,(debug&PRI_DEBUG_Q921_RAW)? "Yes" : "No" );
14288 count++;
14289 }
14290 }
14291 }
14292
14293 }
14294 ast_mutex_lock(&pridebugfdlock);
14295 if (pridebugfd >= 0)
14296 ast_cli(a->fd, "Logging PRI debug to file %s\n", pridebugfilename);
14297 ast_mutex_unlock(&pridebugfdlock);
14298
14299 if (!count)
14300 ast_cli(a->fd, "No debug set or no PRI running\n");
14301 return CLI_SUCCESS;
14302 }
14303 #endif
14304
14305 #if defined(HAVE_PRI)
14306 static char *handle_pri_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14307 {
14308 switch (cmd) {
14309 case CLI_INIT:
14310 e->command = "pri show version";
14311 e->usage =
14312 "Usage: pri show version\n"
14313 "Show libpri version information\n";
14314 return NULL;
14315 case CLI_GENERATE:
14316 return NULL;
14317 }
14318
14319 ast_cli(a->fd, "libpri version: %s\n", pri_get_version());
14320
14321 return CLI_SUCCESS;
14322 }
14323 #endif
14324
14325 #if defined(HAVE_PRI)
14326 static struct ast_cli_entry dahdi_pri_cli[] = {
14327 AST_CLI_DEFINE(handle_pri_debug, "Enables PRI debugging on a span"),
14328 AST_CLI_DEFINE(handle_pri_show_spans, "Displays PRI Information"),
14329 AST_CLI_DEFINE(handle_pri_show_span, "Displays PRI Information"),
14330 AST_CLI_DEFINE(handle_pri_show_debug, "Displays current PRI debug settings"),
14331 AST_CLI_DEFINE(handle_pri_set_debug_file, "Sends PRI debug output to the specified file"),
14332 AST_CLI_DEFINE(handle_pri_version, "Displays libpri version"),
14333 };
14334 #endif
14335
14336 #ifdef HAVE_OPENR2
14337
14338 static char *handle_mfcr2_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14339 {
14340 switch (cmd) {
14341 case CLI_INIT:
14342 e->command = "mfcr2 show version";
14343 e->usage =
14344 "Usage: mfcr2 show version\n"
14345 " Shows the version of the OpenR2 library being used.\n";
14346 return NULL;
14347 case CLI_GENERATE:
14348 return NULL;
14349 }
14350 ast_cli(a->fd, "OpenR2 version: %s, revision: %s\n", openr2_get_version(), openr2_get_revision());
14351 return CLI_SUCCESS;
14352 }
14353
14354 static char *handle_mfcr2_show_variants(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14355 {
14356 #define FORMAT "%4s %40s\n"
14357 int i = 0;
14358 int numvariants = 0;
14359 const openr2_variant_entry_t *variants;
14360 switch (cmd) {
14361 case CLI_INIT:
14362 e->command = "mfcr2 show variants";
14363 e->usage =
14364 "Usage: mfcr2 show variants\n"
14365 " Shows the list of MFC/R2 variants supported.\n";
14366 return NULL;
14367 case CLI_GENERATE:
14368 return NULL;
14369 }
14370 if (!(variants = openr2_proto_get_variant_list(&numvariants))) {
14371 ast_cli(a->fd, "Failed to get list of variants.\n");
14372 return CLI_FAILURE;
14373 }
14374 ast_cli(a->fd, FORMAT, "Variant Code", "Country");
14375 for (i = 0; i < numvariants; i++) {
14376 ast_cli(a->fd, FORMAT, variants[i].name, variants[i].country);
14377 }
14378 return CLI_SUCCESS;
14379 #undef FORMAT
14380 }
14381
14382 static char *handle_mfcr2_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14383 {
14384 #define FORMAT "%4s %-7.7s %-7.7s %-8.8s %-9.9s %-16.16s %-8.8s %-8.8s\n"
14385 int filtertype = 0;
14386 int targetnum = 0;
14387 char channo[5];
14388 char anino[5];
14389 char dnisno[5];
14390 struct dahdi_pvt *p;
14391 openr2_context_t *r2context;
14392 openr2_variant_t r2variant;
14393 switch (cmd) {
14394 case CLI_INIT:
14395 e->command = "mfcr2 show channels [group|context]";
14396 e->usage =
14397 "Usage: mfcr2 show channels [group <group> | context <context>]\n"
14398 " Shows the DAHDI channels configured with MFC/R2 signaling.\n";
14399 return NULL;
14400 case CLI_GENERATE:
14401 return NULL;
14402 }
14403 if (!((a->argc == 3) || (a->argc == 5))) {
14404 return CLI_SHOWUSAGE;
14405 }
14406 if (a->argc == 5) {
14407 if (!strcasecmp(a->argv[3], "group")) {
14408 targetnum = atoi(a->argv[4]);
14409 if ((targetnum < 0) || (targetnum > 63))
14410 return CLI_SHOWUSAGE;
14411 targetnum = 1 << targetnum;
14412 filtertype = 1;
14413 } else if (!strcasecmp(a->argv[3], "context")) {
14414 filtertype = 2;
14415 } else {
14416 return CLI_SHOWUSAGE;
14417 }
14418 }
14419 ast_cli(a->fd, FORMAT, "Chan", "Variant", "Max ANI", "Max DNIS", "ANI First", "Immediate Accept", "Tx CAS", "Rx CAS");
14420 ast_mutex_lock(&iflock);
14421 p = iflist;
14422 for (p = iflist; p; p = p->next) {
14423 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14424 continue;
14425 }
14426 if (filtertype) {
14427 switch(filtertype) {
14428 case 1:
14429 if (p->group != targetnum) {
14430 continue;
14431 }
14432 break;
14433 case 2:
14434 if (strcasecmp(p->context, a->argv[4])) {
14435 continue;
14436 }
14437 break;
14438 default:
14439 ;
14440 }
14441 }
14442 r2context = openr2_chan_get_context(p->r2chan);
14443 r2variant = openr2_context_get_variant(r2context);
14444 snprintf(channo, sizeof(channo), "%d", p->channel);
14445 snprintf(anino, sizeof(anino), "%d", openr2_context_get_max_ani(r2context));
14446 snprintf(dnisno, sizeof(dnisno), "%d", openr2_context_get_max_dnis(r2context));
14447 ast_cli(a->fd, FORMAT, channo, openr2_proto_get_variant_string(r2variant),
14448 anino, dnisno, openr2_context_get_ani_first(r2context) ? "Yes" : "No",
14449 openr2_context_get_immediate_accept(r2context) ? "Yes" : "No",
14450 openr2_chan_get_tx_cas_string(p->r2chan), openr2_chan_get_rx_cas_string(p->r2chan));
14451 }
14452 ast_mutex_unlock(&iflock);
14453 return CLI_SUCCESS;
14454 #undef FORMAT
14455 }
14456
14457 static char *handle_mfcr2_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14458 {
14459 struct dahdi_pvt *p = NULL;
14460 int channo = 0;
14461 char *toklevel = NULL;
14462 char *saveptr = NULL;
14463 char *logval = NULL;
14464 openr2_log_level_t loglevel = OR2_LOG_NOTHING;
14465 openr2_log_level_t tmplevel = OR2_LOG_NOTHING;
14466 switch (cmd) {
14467 case CLI_INIT:
14468 e->command = "mfcr2 set debug";
14469 e->usage =
14470 "Usage: mfcr2 set debug <loglevel> <channel>\n"
14471 " Set a new logging level for the specified channel.\n"
14472 " If no channel is specified the logging level will be applied to all channels.\n";
14473 return NULL;
14474 case CLI_GENERATE:
14475 return NULL;
14476 }
14477 if (a->argc < 4) {
14478 return CLI_SHOWUSAGE;
14479 }
14480 channo = (a->argc == 5) ? atoi(a->argv[4]) : -1;
14481 logval = ast_strdupa(a->argv[3]);
14482 toklevel = strtok_r(logval, ",", &saveptr);
14483 if (-1 == (tmplevel = openr2_log_get_level(toklevel))) {
14484 ast_cli(a->fd, "Invalid MFC/R2 logging level '%s'.\n", a->argv[3]);
14485 return CLI_FAILURE;
14486 } else if (OR2_LOG_NOTHING == tmplevel) {
14487 loglevel = tmplevel;
14488 } else {
14489 loglevel |= tmplevel;
14490 while ((toklevel = strtok_r(NULL, ",", &saveptr))) {
14491 if (-1 == (tmplevel = openr2_log_get_level(toklevel))) {
14492 ast_cli(a->fd, "Ignoring invalid logging level: '%s'.\n", toklevel);
14493 continue;
14494 }
14495 loglevel |= tmplevel;
14496 }
14497 }
14498 ast_mutex_lock(&iflock);
14499 for (p = iflist; p; p = p->next) {
14500 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14501 continue;
14502 }
14503 if ((channo != -1) && (p->channel != channo )) {
14504 continue;
14505 }
14506 openr2_chan_set_log_level(p->r2chan, loglevel);
14507 if (channo != -1) {
14508 ast_cli(a->fd, "MFC/R2 debugging set to '%s' for channel %d.\n", a->argv[3], p->channel);
14509 break;
14510 }
14511 }
14512 if ((channo != -1) && !p) {
14513 ast_cli(a->fd, "MFC/R2 channel %d not found.\n", channo);
14514 }
14515 if (channo == -1) {
14516 ast_cli(a->fd, "MFC/R2 debugging set to '%s' for all channels.\n", a->argv[3]);
14517 }
14518 ast_mutex_unlock(&iflock);
14519 return CLI_SUCCESS;
14520 }
14521
14522 static char *handle_mfcr2_call_files(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14523 {
14524 struct dahdi_pvt *p = NULL;
14525 int channo = 0;
14526 switch (cmd) {
14527 case CLI_INIT:
14528 e->command = "mfcr2 call files [on|off]";
14529 e->usage =
14530 "Usage: mfcr2 call files [on|off] <channel>\n"
14531 " Enable call files creation on the specified channel.\n"
14532 " If no channel is specified call files creation policy will be applied to all channels.\n";
14533 return NULL;
14534 case CLI_GENERATE:
14535 return NULL;
14536 }
14537 if (a->argc < 4) {
14538 return CLI_SHOWUSAGE;
14539 }
14540 channo = (a->argc == 5) ? atoi(a->argv[4]) : -1;
14541 ast_mutex_lock(&iflock);
14542 for (p = iflist; p; p = p->next) {
14543 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14544 continue;
14545 }
14546 if ((channo != -1) && (p->channel != channo )) {
14547 continue;
14548 }
14549 if (ast_true(a->argv[3])) {
14550 openr2_chan_enable_call_files(p->r2chan);
14551 } else {
14552 openr2_chan_disable_call_files(p->r2chan);
14553 }
14554 if (channo != -1) {
14555 if (ast_true(a->argv[3])) {
14556 ast_cli(a->fd, "MFC/R2 call files enabled for channel %d.\n", p->channel);
14557 } else {
14558 ast_cli(a->fd, "MFC/R2 call files disabled for channel %d.\n", p->channel);
14559 }
14560 break;
14561 }
14562 }
14563 if ((channo != -1) && !p) {
14564 ast_cli(a->fd, "MFC/R2 channel %d not found.\n", channo);
14565 }
14566 if (channo == -1) {
14567 if (ast_true(a->argv[3])) {
14568 ast_cli(a->fd, "MFC/R2 Call files enabled for all channels.\n");
14569 } else {
14570 ast_cli(a->fd, "MFC/R2 Call files disabled for all channels.\n");
14571 }
14572 }
14573 ast_mutex_unlock(&iflock);
14574 return CLI_SUCCESS;
14575 }
14576
14577 static char *handle_mfcr2_set_idle(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14578 {
14579 struct dahdi_pvt *p = NULL;
14580 int channo = 0;
14581 switch (cmd) {
14582 case CLI_INIT:
14583 e->command = "mfcr2 set idle";
14584 e->usage =
14585 "Usage: mfcr2 set idle <channel>\n"
14586 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.\n"
14587 " Force the given channel into IDLE state.\n"
14588 " If no channel is specified, all channels will be set to IDLE.\n";
14589 return NULL;
14590 case CLI_GENERATE:
14591 return NULL;
14592 }
14593 channo = (a->argc == 4) ? atoi(a->argv[3]) : -1;
14594 ast_mutex_lock(&iflock);
14595 for (p = iflist; p; p = p->next) {
14596 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14597 continue;
14598 }
14599 if ((channo != -1) && (p->channel != channo )) {
14600 continue;
14601 }
14602 openr2_chan_set_idle(p->r2chan);
14603 ast_mutex_lock(&p->lock);
14604 p->locallyblocked = 0;
14605 p->mfcr2call = 0;
14606 ast_mutex_unlock(&p->lock);
14607 if (channo != -1) {
14608 break;
14609 }
14610 }
14611 if ((channo != -1) && !p) {
14612 ast_cli(a->fd, "MFC/R2 channel %d not found.\n", channo);
14613 }
14614 ast_mutex_unlock(&iflock);
14615 return CLI_SUCCESS;
14616 }
14617
14618 static char *handle_mfcr2_set_blocked(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14619 {
14620 struct dahdi_pvt *p = NULL;
14621 int channo = 0;
14622 switch (cmd) {
14623 case CLI_INIT:
14624 e->command = "mfcr2 set blocked";
14625 e->usage =
14626 "Usage: mfcr2 set blocked <channel>\n"
14627 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING.\n"
14628 " Force the given channel into BLOCKED state.\n"
14629 " If no channel is specified, all channels will be set to BLOCKED.\n";
14630 return NULL;
14631 case CLI_GENERATE:
14632 return NULL;
14633 }
14634 channo = (a->argc == 4) ? atoi(a->argv[3]) : -1;
14635 ast_mutex_lock(&iflock);
14636 for (p = iflist; p; p = p->next) {
14637 if (!(p->sig & SIG_MFCR2) || !p->r2chan) {
14638 continue;
14639 }
14640 if ((channo != -1) && (p->channel != channo )) {
14641 continue;
14642 }
14643 openr2_chan_set_blocked(p->r2chan);
14644 ast_mutex_lock(&p->lock);
14645 p->locallyblocked = 1;
14646 ast_mutex_unlock(&p->lock);
14647 if (channo != -1) {
14648 break;
14649 }
14650 }
14651 if ((channo != -1) && !p) {
14652 ast_cli(a->fd, "MFC/R2 channel %d not found.\n", channo);
14653 }
14654 ast_mutex_unlock(&iflock);
14655 return CLI_SUCCESS;
14656 }
14657
14658 static struct ast_cli_entry dahdi_mfcr2_cli[] = {
14659 AST_CLI_DEFINE(handle_mfcr2_version, "Show OpenR2 library version"),
14660 AST_CLI_DEFINE(handle_mfcr2_show_variants, "Show supported MFC/R2 variants"),
14661 AST_CLI_DEFINE(handle_mfcr2_show_channels, "Show MFC/R2 channels"),
14662 AST_CLI_DEFINE(handle_mfcr2_set_debug, "Set MFC/R2 channel logging level"),
14663 AST_CLI_DEFINE(handle_mfcr2_call_files, "Enable/Disable MFC/R2 call files"),
14664 AST_CLI_DEFINE(handle_mfcr2_set_idle, "Reset MFC/R2 channel forcing it to IDLE"),
14665 AST_CLI_DEFINE(handle_mfcr2_set_blocked, "Reset MFC/R2 channel forcing it to BLOCKED"),
14666 };
14667
14668 #endif
14669
14670 static char *dahdi_destroy_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14671 {
14672 int channel;
14673 int ret;
14674 switch (cmd) {
14675 case CLI_INIT:
14676 e->command = "dahdi destroy channel";
14677 e->usage =
14678 "Usage: dahdi destroy channel <chan num>\n"
14679 " DON'T USE THIS UNLESS YOU KNOW WHAT YOU ARE DOING. Immediately removes a given channel, whether it is in use or not\n";
14680 return NULL;
14681 case CLI_GENERATE:
14682 return NULL;
14683 }
14684 if (a->argc != 4)
14685 return CLI_SHOWUSAGE;
14686
14687 channel = atoi(a->argv[3]);
14688 ret = dahdi_destroy_channel_bynum(channel);
14689 return ( RESULT_SUCCESS == ret ) ? CLI_SUCCESS : CLI_FAILURE;
14690 }
14691
14692 static void dahdi_softhangup_all(void)
14693 {
14694 struct dahdi_pvt *p;
14695 retry:
14696 ast_mutex_lock(&iflock);
14697 for (p = iflist; p; p = p->next) {
14698 ast_mutex_lock(&p->lock);
14699 if (p->owner && !p->restartpending) {
14700 if (ast_channel_trylock(p->owner)) {
14701 if (option_debug > 2)
14702 ast_verbose("Avoiding deadlock\n");
14703
14704 ast_mutex_unlock(&p->lock);
14705 ast_mutex_unlock(&iflock);
14706 goto retry;
14707 }
14708 if (option_debug > 2)
14709 ast_verbose("Softhanging up on %s\n", p->owner->name);
14710 ast_softhangup_nolock(p->owner, AST_SOFTHANGUP_EXPLICIT);
14711 p->restartpending = 1;
14712 num_restart_pending++;
14713 ast_channel_unlock(p->owner);
14714 }
14715 ast_mutex_unlock(&p->lock);
14716 }
14717 ast_mutex_unlock(&iflock);
14718 }
14719
14720 static int setup_dahdi(int reload);
14721 static int dahdi_restart(void)
14722 {
14723 #if defined(HAVE_PRI) || defined(HAVE_SS7)
14724 int i, j;
14725 #endif
14726 int cancel_code;
14727 struct dahdi_pvt *p;
14728
14729 ast_mutex_lock(&restart_lock);
14730 ast_verb(1, "Destroying channels and reloading DAHDI configuration.\n");
14731 dahdi_softhangup_all();
14732 ast_verb(4, "Initial softhangup of all DAHDI channels complete.\n");
14733 #ifdef HAVE_OPENR2
14734 dahdi_r2_destroy_links();
14735 #endif
14736
14737 #if defined(HAVE_PRI)
14738 for (i = 0; i < NUM_SPANS; i++) {
14739 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL)) {
14740 cancel_code = pthread_cancel(pris[i].master);
14741 pthread_kill(pris[i].master, SIGURG);
14742 ast_debug(4, "Waiting to join thread of span %d with pid=%p, cancel_code=%d\n", i, (void *) pris[i].master, cancel_code);
14743 pthread_join(pris[i].master, NULL);
14744 ast_debug(4, "Joined thread of span %d\n", i);
14745 }
14746 }
14747 #endif
14748
14749 #if defined(HAVE_SS7)
14750 for (i = 0; i < NUM_SPANS; i++) {
14751 if (linksets[i].master && (linksets[i].master != AST_PTHREADT_NULL)) {
14752 cancel_code = pthread_cancel(linksets[i].master);
14753 pthread_kill(linksets[i].master, SIGURG);
14754 ast_debug(4, "Waiting to join thread of span %d with pid=%p, cancel_code=%d\n", i, (void *) linksets[i].master, cancel_code);
14755 pthread_join(linksets[i].master, NULL);
14756 ast_debug(4, "Joined thread of span %d\n", i);
14757 }
14758 }
14759 #endif
14760
14761 ast_mutex_lock(&monlock);
14762 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
14763 cancel_code = pthread_cancel(monitor_thread);
14764 pthread_kill(monitor_thread, SIGURG);
14765 ast_debug(4, "Waiting to join monitor thread with pid=%p, cancel_code=%d\n", (void *) monitor_thread, cancel_code);
14766 pthread_join(monitor_thread, NULL);
14767 ast_debug(4, "Joined monitor thread\n");
14768 }
14769 monitor_thread = AST_PTHREADT_NULL;
14770
14771 ast_mutex_lock(&ss_thread_lock);
14772 while (ss_thread_count > 0) {
14773 int x = DAHDI_FLASH;
14774 ast_debug(3, "Waiting on %d ss_thread(s) to finish\n", ss_thread_count);
14775
14776 for (p = iflist; p; p = p->next) {
14777 if (p->owner)
14778 ioctl(p->subs[SUB_REAL].dfd, DAHDI_HOOK, &x);
14779 }
14780 ast_cond_wait(&ss_thread_complete, &ss_thread_lock);
14781 }
14782
14783
14784 dahdi_softhangup_all();
14785 ast_verb(4, "Final softhangup of all DAHDI channels complete.\n");
14786 destroy_all_channels();
14787 ast_debug(1, "Channels destroyed. Now re-reading config. %d active channels remaining.\n", ast_active_channels());
14788
14789 ast_mutex_unlock(&monlock);
14790
14791 #ifdef HAVE_PRI
14792 for (i = 0; i < NUM_SPANS; i++) {
14793 for (j = 0; j < NUM_DCHANS; j++)
14794 dahdi_close_pri_fd(&(pris[i]), j);
14795 }
14796
14797 memset(pris, 0, sizeof(pris));
14798 for (i = 0; i < NUM_SPANS; i++) {
14799 ast_mutex_init(&pris[i].lock);
14800 pris[i].offset = -1;
14801 pris[i].master = AST_PTHREADT_NULL;
14802 for (j = 0; j < NUM_DCHANS; j++)
14803 pris[i].fds[j] = -1;
14804 }
14805 pri_set_error(dahdi_pri_error);
14806 pri_set_message(dahdi_pri_message);
14807 #endif
14808 #ifdef HAVE_SS7
14809 for (i = 0; i < NUM_SPANS; i++) {
14810 for (j = 0; j < NUM_DCHANS; j++)
14811 dahdi_close_ss7_fd(&(linksets[i]), j);
14812 }
14813
14814 memset(linksets, 0, sizeof(linksets));
14815 for (i = 0; i < NUM_SPANS; i++) {
14816 ast_mutex_init(&linksets[i].lock);
14817 linksets[i].master = AST_PTHREADT_NULL;
14818 for (j = 0; j < NUM_DCHANS; j++)
14819 linksets[i].fds[j] = -1;
14820 }
14821 ss7_set_error(dahdi_ss7_error);
14822 ss7_set_message(dahdi_ss7_message);
14823 #endif
14824
14825 if (setup_dahdi(2) != 0) {
14826 ast_log(LOG_WARNING, "Reload channels from dahdi config failed!\n");
14827 ast_mutex_unlock(&ss_thread_lock);
14828 return 1;
14829 }
14830 ast_mutex_unlock(&ss_thread_lock);
14831 ast_mutex_unlock(&restart_lock);
14832 return 0;
14833 }
14834
14835 static char *dahdi_restart_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14836 {
14837 switch (cmd) {
14838 case CLI_INIT:
14839 e->command = "dahdi restart";
14840 e->usage =
14841 "Usage: dahdi restart\n"
14842 " Restarts the DAHDI channels: destroys them all and then\n"
14843 " re-reads them from chan_dahdi.conf.\n"
14844 " Note that this will STOP any running CALL on DAHDI channels.\n"
14845 "";
14846 return NULL;
14847 case CLI_GENERATE:
14848 return NULL;
14849 }
14850 if (a->argc != 2)
14851 return CLI_SHOWUSAGE;
14852
14853 if (dahdi_restart() != 0)
14854 return CLI_FAILURE;
14855 return CLI_SUCCESS;
14856 }
14857
14858 static int action_dahdirestart(struct mansession *s, const struct message *m)
14859 {
14860 if (dahdi_restart() != 0) {
14861 astman_send_error(s, m, "Failed rereading DAHDI configuration");
14862 return 1;
14863 }
14864 astman_send_ack(s, m, "DAHDIRestart: Success");
14865 return 0;
14866 }
14867
14868 static char *dahdi_show_channels(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14869 {
14870 #define FORMAT "%7s %-10.10s %-15.15s %-10.10s %-20.20s %-10.10s %-10.10s\n"
14871 #define FORMAT2 "%7s %-10.10s %-15.15s %-10.10s %-20.20s %-10.10s %-10.10s\n"
14872 unsigned int targetnum = 0;
14873 int filtertype = 0;
14874 struct dahdi_pvt *tmp = NULL;
14875 char tmps[20] = "";
14876 char statestr[20] = "";
14877 char blockstr[20] = "";
14878 ast_mutex_t *lock;
14879 struct dahdi_pvt *start;
14880 #ifdef HAVE_PRI
14881 int trunkgroup;
14882 struct dahdi_pri *pri = NULL;
14883 int x;
14884 #endif
14885 switch (cmd) {
14886 case CLI_INIT:
14887 e->command = "dahdi show channels [trunkgroup|group|context]";
14888 e->usage =
14889 "Usage: dahdi show channels [ trunkgroup <trunkgroup> | group <group> | context <context> ]\n"
14890 " Shows a list of available channels with optional filtering\n"
14891 " <group> must be a number between 0 and 63\n";
14892 return NULL;
14893 case CLI_GENERATE:
14894 return NULL;
14895 }
14896
14897 lock = &iflock;
14898 start = iflist;
14899
14900
14901
14902 if (!((a->argc == 3) || (a->argc == 5)))
14903 return CLI_SHOWUSAGE;
14904
14905 if (a->argc == 5) {
14906 #ifdef HAVE_PRI
14907 if (!strcasecmp(a->argv[3], "trunkgroup")) {
14908
14909 if ((trunkgroup = atoi(a->argv[4])) < 1)
14910 return CLI_SHOWUSAGE;
14911 for (x = 0; x < NUM_SPANS; x++) {
14912 if (pris[x].trunkgroup == trunkgroup) {
14913 pri = pris + x;
14914 break;
14915 }
14916 }
14917 if (pri) {
14918 start = pri->crvs;
14919 lock = &pri->lock;
14920 } else {
14921 ast_cli(a->fd, "No such trunk group %d\n", trunkgroup);
14922 return CLI_FAILURE;
14923 }
14924 } else
14925 #endif
14926 if (!strcasecmp(a->argv[3], "group")) {
14927 targetnum = atoi(a->argv[4]);
14928 if ((targetnum < 0) || (targetnum > 63))
14929 return CLI_SHOWUSAGE;
14930 targetnum = 1 << targetnum;
14931 filtertype = 1;
14932 } else if (!strcasecmp(a->argv[3], "context")) {
14933 filtertype = 2;
14934 }
14935 }
14936
14937 ast_mutex_lock(lock);
14938 #ifdef HAVE_PRI
14939 ast_cli(a->fd, FORMAT2, pri ? "CRV" : "Chan", "Extension", "Context", "Language", "MOH Interpret", "Blocked", "State");
14940 #else
14941 ast_cli(a->fd, FORMAT2, "Chan", "Extension", "Context", "Language", "MOH Interpret", "Blocked", "State");
14942 #endif
14943
14944 tmp = start;
14945 while (tmp) {
14946 if (filtertype) {
14947 switch(filtertype) {
14948 case 1:
14949 if (!(tmp->group & targetnum)) {
14950 tmp = tmp->next;
14951 continue;
14952 }
14953 break;
14954 case 2:
14955 if (strcasecmp(tmp->context, a->argv[4])) {
14956 tmp = tmp->next;
14957 continue;
14958 }
14959 break;
14960 default:
14961 ;
14962 }
14963 }
14964 if (tmp->channel > 0) {
14965 snprintf(tmps, sizeof(tmps), "%d", tmp->channel);
14966 } else
14967 ast_copy_string(tmps, "pseudo", sizeof(tmps));
14968
14969 if (tmp->locallyblocked)
14970 blockstr[0] = 'L';
14971 else
14972 blockstr[0] = ' ';
14973
14974 if (tmp->remotelyblocked)
14975 blockstr[1] = 'R';
14976 else
14977 blockstr[1] = ' ';
14978
14979 blockstr[2] = '\0';
14980
14981 snprintf(statestr, sizeof(statestr), "%s", "In Service");
14982
14983 ast_cli(a->fd, FORMAT, tmps, tmp->exten, tmp->context, tmp->language, tmp->mohinterpret, blockstr, statestr);
14984 tmp = tmp->next;
14985 }
14986 ast_mutex_unlock(lock);
14987 return CLI_SUCCESS;
14988 #undef FORMAT
14989 #undef FORMAT2
14990 }
14991
14992 static char *dahdi_show_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
14993 {
14994 int channel;
14995 struct dahdi_pvt *tmp = NULL;
14996 struct dahdi_confinfo ci;
14997 struct dahdi_params ps;
14998 int x;
14999 ast_mutex_t *lock;
15000 struct dahdi_pvt *start;
15001 #ifdef HAVE_PRI
15002 char *c;
15003 int trunkgroup;
15004 struct dahdi_pri *pri=NULL;
15005 #endif
15006 switch (cmd) {
15007 case CLI_INIT:
15008 e->command = "dahdi show channel";
15009 e->usage =
15010 "Usage: dahdi show channel <chan num>\n"
15011 " Detailed information about a given channel\n";
15012 return NULL;
15013 case CLI_GENERATE:
15014 return NULL;
15015 }
15016
15017 lock = &iflock;
15018 start = iflist;
15019
15020 if (a->argc != 4)
15021 return CLI_SHOWUSAGE;
15022 #ifdef HAVE_PRI
15023 if ((c = strchr(a->argv[3], ':'))) {
15024 if (sscanf(a->argv[3], "%30d:%30d", &trunkgroup, &channel) != 2)
15025 return CLI_SHOWUSAGE;
15026 if ((trunkgroup < 1) || (channel < 1))
15027 return CLI_SHOWUSAGE;
15028 for (x = 0; x < NUM_SPANS; x++) {
15029 if (pris[x].trunkgroup == trunkgroup) {
15030 pri = pris + x;
15031 break;
15032 }
15033 }
15034 if (pri) {
15035 start = pri->crvs;
15036 lock = &pri->lock;
15037 } else {
15038 ast_cli(a->fd, "No such trunk group %d\n", trunkgroup);
15039 return CLI_FAILURE;
15040 }
15041 } else
15042 #endif
15043 channel = atoi(a->argv[3]);
15044
15045 ast_mutex_lock(lock);
15046 tmp = start;
15047 while (tmp) {
15048 if (tmp->channel == channel) {
15049 #ifdef HAVE_PRI
15050 if (pri)
15051 ast_cli(a->fd, "Trunk/CRV: %d/%d\n", trunkgroup, tmp->channel);
15052 else
15053 #endif
15054 ast_cli(a->fd, "Channel: %d\n", tmp->channel);
15055 ast_cli(a->fd, "File Descriptor: %d\n", tmp->subs[SUB_REAL].dfd);
15056 ast_cli(a->fd, "Span: %d\n", tmp->span);
15057 ast_cli(a->fd, "Extension: %s\n", tmp->exten);
15058 ast_cli(a->fd, "Dialing: %s\n", tmp->dialing ? "yes" : "no");
15059 ast_cli(a->fd, "Context: %s\n", tmp->context);
15060 ast_cli(a->fd, "Caller ID: %s\n", tmp->cid_num);
15061 ast_cli(a->fd, "Calling TON: %d\n", tmp->cid_ton);
15062 ast_cli(a->fd, "Caller ID name: %s\n", tmp->cid_name);
15063 ast_cli(a->fd, "Mailbox: %s\n", S_OR(tmp->mailbox, "none"));
15064 if (tmp->vars) {
15065 struct ast_variable *v;
15066 ast_cli(a->fd, "Variables:\n");
15067 for (v = tmp->vars ; v ; v = v->next)
15068 ast_cli(a->fd, " %s = %s\n", v->name, v->value);
15069 }
15070 ast_cli(a->fd, "Destroy: %d\n", tmp->destroy);
15071 ast_cli(a->fd, "InAlarm: %d\n", tmp->inalarm);
15072 ast_cli(a->fd, "Signalling Type: %s\n", sig2str(tmp->sig));
15073 ast_cli(a->fd, "Radio: %d\n", tmp->radio);
15074 ast_cli(a->fd, "Owner: %s\n", tmp->owner ? tmp->owner->name : "<None>");
15075 ast_cli(a->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)" : "");
15076 ast_cli(a->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)" : "");
15077 ast_cli(a->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)" : "");
15078 ast_cli(a->fd, "Confno: %d\n", tmp->confno);
15079 ast_cli(a->fd, "Propagated Conference: %d\n", tmp->propconfno);
15080 ast_cli(a->fd, "Real in conference: %d\n", tmp->inconference);
15081 ast_cli(a->fd, "DSP: %s\n", tmp->dsp ? "yes" : "no");
15082 ast_cli(a->fd, "Busy Detection: %s\n", tmp->busydetect ? "yes" : "no");
15083 if (tmp->busydetect) {
15084 #if defined(BUSYDETECT_TONEONLY)
15085 ast_cli(a->fd, " Busy Detector Helper: BUSYDETECT_TONEONLY\n");
15086 #elif defined(BUSYDETECT_COMPARE_TONE_AND_SILENCE)
15087 ast_cli(a->fd, " Busy Detector Helper: BUSYDETECT_COMPARE_TONE_AND_SILENCE\n");
15088 #endif
15089 #ifdef BUSYDETECT_DEBUG
15090 ast_cli(a->fd, " Busy Detector Debug: Enabled\n");
15091 #endif
15092 ast_cli(a->fd, " Busy Count: %d\n", tmp->busycount);
15093 ast_cli(a->fd, " Busy Pattern: %d,%d\n", tmp->busy_tonelength, tmp->busy_quietlength);
15094 }
15095 ast_cli(a->fd, "TDD: %s\n", tmp->tdd ? "yes" : "no");
15096 ast_cli(a->fd, "Relax DTMF: %s\n", tmp->dtmfrelax ? "yes" : "no");
15097 ast_cli(a->fd, "Dialing/CallwaitCAS: %d/%d\n", tmp->dialing, tmp->callwaitcas);
15098 ast_cli(a->fd, "Default law: %s\n", tmp->law == DAHDI_LAW_MULAW ? "ulaw" : tmp->law == DAHDI_LAW_ALAW ? "alaw" : "unknown");
15099 ast_cli(a->fd, "Fax Handled: %s\n", tmp->faxhandled ? "yes" : "no");
15100 ast_cli(a->fd, "Pulse phone: %s\n", tmp->pulsedial ? "yes" : "no");
15101 ast_cli(a->fd, "DND: %s\n", tmp->dnd ? "yes" : "no");
15102 ast_cli(a->fd, "Echo Cancellation:\n");
15103
15104 if (tmp->echocancel.head.tap_length) {
15105 ast_cli(a->fd, "\t%d taps\n", tmp->echocancel.head.tap_length);
15106 for (x = 0; x < tmp->echocancel.head.param_count; x++) {
15107 ast_cli(a->fd, "\t\t%s: %ud\n", tmp->echocancel.params[x].name, tmp->echocancel.params[x].value);
15108 }
15109 ast_cli(a->fd, "\t%scurrently %s\n", tmp->echocanbridged ? "" : "(unless TDM bridged) ", tmp->echocanon ? "ON" : "OFF");
15110 } else {
15111 ast_cli(a->fd, "\tnone\n");
15112 }
15113 ast_cli(a->fd, "Wait for dialtone: %dms\n", tmp->waitfordialtone);
15114 if (tmp->master)
15115 ast_cli(a->fd, "Master Channel: %d\n", tmp->master->channel);
15116 for (x = 0; x < MAX_SLAVES; x++) {
15117 if (tmp->slaves[x])
15118 ast_cli(a->fd, "Slave Channel: %d\n", tmp->slaves[x]->channel);
15119 }
15120 #ifdef HAVE_OPENR2
15121 if (tmp->mfcr2) {
15122 char calldir[OR2_MAX_PATH];
15123 openr2_context_t *r2context = openr2_chan_get_context(tmp->r2chan);
15124 openr2_variant_t r2variant = openr2_context_get_variant(r2context);
15125 ast_cli(a->fd, "MFC/R2 MF State: %s\n", openr2_chan_get_mf_state_string(tmp->r2chan));
15126 ast_cli(a->fd, "MFC/R2 MF Group: %s\n", openr2_chan_get_mf_group_string(tmp->r2chan));
15127 ast_cli(a->fd, "MFC/R2 State: %s\n", openr2_chan_get_r2_state_string(tmp->r2chan));
15128 ast_cli(a->fd, "MFC/R2 Call State: %s\n", openr2_chan_get_call_state_string(tmp->r2chan));
15129 ast_cli(a->fd, "MFC/R2 Call Files Enabled: %s\n", openr2_chan_get_call_files_enabled(tmp->r2chan) ? "Yes" : "No");
15130 ast_cli(a->fd, "MFC/R2 Variant: %s\n", openr2_proto_get_variant_string(r2variant));
15131 ast_cli(a->fd, "MFC/R2 Max ANI: %d\n", openr2_context_get_max_ani(r2context));
15132 ast_cli(a->fd, "MFC/R2 Max DNIS: %d\n", openr2_context_get_max_dnis(r2context));
15133 ast_cli(a->fd, "MFC/R2 Get ANI First: %s\n", openr2_context_get_ani_first(r2context) ? "Yes" : "No");
15134 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
15135 ast_cli(a->fd, "MFC/R2 Skip Category Request: %s\n", openr2_context_get_skip_category_request(r2context) ? "Yes" : "No");
15136 #endif
15137 ast_cli(a->fd, "MFC/R2 Immediate Accept: %s\n", openr2_context_get_immediate_accept(r2context) ? "Yes" : "No");
15138 ast_cli(a->fd, "MFC/R2 Accept on Offer: %s\n", tmp->mfcr2_accept_on_offer ? "Yes" : "No");
15139 ast_cli(a->fd, "MFC/R2 Charge Calls: %s\n", tmp->mfcr2_charge_calls ? "Yes" : "No");
15140 ast_cli(a->fd, "MFC/R2 Allow Collect Calls: %s\n", tmp->mfcr2_allow_collect_calls ? "Yes" : "No");
15141 ast_cli(a->fd, "MFC/R2 Forced Release: %s\n", tmp->mfcr2_forced_release ? "Yes" : "No");
15142 ast_cli(a->fd, "MFC/R2 MF Back Timeout: %dms\n", openr2_context_get_mf_back_timeout(r2context));
15143 ast_cli(a->fd, "MFC/R2 R2 Metering Pulse Timeout: %dms\n", openr2_context_get_metering_pulse_timeout(r2context));
15144 ast_cli(a->fd, "MFC/R2 Rx CAS: %s\n", openr2_chan_get_rx_cas_string(tmp->r2chan));
15145 ast_cli(a->fd, "MFC/R2 Tx CAS: %s\n", openr2_chan_get_tx_cas_string(tmp->r2chan));
15146 ast_cli(a->fd, "MFC/R2 MF Tx Signal: %d\n", openr2_chan_get_tx_mf_signal(tmp->r2chan));
15147 ast_cli(a->fd, "MFC/R2 MF Rx Signal: %d\n", openr2_chan_get_rx_mf_signal(tmp->r2chan));
15148 ast_cli(a->fd, "MFC/R2 Call Files Directory: %s\n", openr2_context_get_log_directory(r2context, calldir, sizeof(calldir)));
15149 }
15150 #endif
15151 #ifdef HAVE_SS7
15152 if (tmp->ss7) {
15153 ast_cli(a->fd, "CIC: %d\n", tmp->cic);
15154 }
15155 #endif
15156 #ifdef HAVE_PRI
15157 if (tmp->pri) {
15158 ast_cli(a->fd, "PRI Flags: ");
15159 if (tmp->resetting)
15160 ast_cli(a->fd, "Resetting ");
15161 if (tmp->call)
15162 ast_cli(a->fd, "Call ");
15163 if (tmp->bearer)
15164 ast_cli(a->fd, "Bearer ");
15165 ast_cli(a->fd, "\n");
15166 if (tmp->logicalspan)
15167 ast_cli(a->fd, "PRI Logical Span: %d\n", tmp->logicalspan);
15168 else
15169 ast_cli(a->fd, "PRI Logical Span: Implicit\n");
15170 }
15171 #endif
15172 memset(&ci, 0, sizeof(ci));
15173 ps.channo = tmp->channel;
15174 if (tmp->subs[SUB_REAL].dfd > -1) {
15175 memset(&ci, 0, sizeof(ci));
15176 if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONF, &ci)) {
15177 ast_cli(a->fd, "Actual Confinfo: Num/%d, Mode/0x%04x\n", ci.confno, ci.confmode);
15178 }
15179 if (!ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GETCONFMUTE, &x)) {
15180 ast_cli(a->fd, "Actual Confmute: %s\n", x ? "Yes" : "No");
15181 }
15182 memset(&ps, 0, sizeof(ps));
15183 if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_GET_PARAMS, &ps) < 0) {
15184 ast_log(LOG_WARNING, "Failed to get parameters on channel %d: %s\n", tmp->channel, strerror(errno));
15185 } else {
15186 ast_cli(a->fd, "Hookstate (FXS only): %s\n", ps.rxisoffhook ? "Offhook" : "Onhook");
15187 }
15188 }
15189 ast_mutex_unlock(lock);
15190 return CLI_SUCCESS;
15191 }
15192 tmp = tmp->next;
15193 }
15194
15195 ast_cli(a->fd, "Unable to find given channel %d\n", channel);
15196 ast_mutex_unlock(lock);
15197 return CLI_FAILURE;
15198 }
15199
15200 static char *handle_dahdi_show_cadences(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15201 {
15202 int i, j;
15203 switch (cmd) {
15204 case CLI_INIT:
15205 e->command = "dahdi show cadences";
15206 e->usage =
15207 "Usage: dahdi show cadences\n"
15208 " Shows all cadences currently defined\n";
15209 return NULL;
15210 case CLI_GENERATE:
15211 return NULL;
15212 }
15213 for (i = 0; i < num_cadence; i++) {
15214 char output[1024];
15215 char tmp[16], tmp2[64];
15216 snprintf(tmp, sizeof(tmp), "r%d: ", i + 1);
15217 term_color(output, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(output));
15218
15219 for (j = 0; j < 16; j++) {
15220 if (cadences[i].ringcadence[j] == 0)
15221 break;
15222 snprintf(tmp, sizeof(tmp), "%d", cadences[i].ringcadence[j]);
15223 if (cidrings[i] * 2 - 1 == j)
15224 term_color(tmp2, tmp, COLOR_MAGENTA, COLOR_BLACK, sizeof(tmp2) - 1);
15225 else
15226 term_color(tmp2, tmp, COLOR_GREEN, COLOR_BLACK, sizeof(tmp2) - 1);
15227 if (j != 0)
15228 strncat(output, ",", sizeof(output) - strlen(output) - 1);
15229 strncat(output, tmp2, sizeof(output) - strlen(output) - 1);
15230 }
15231 ast_cli(a->fd,"%s\n",output);
15232 }
15233 return CLI_SUCCESS;
15234 }
15235
15236
15237 static char *dahdi_show_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15238 {
15239 #define FORMAT "%-40.40s %-7.7s %-6d %-6d %-6d %-3.3s %-4.4s %-8.8s %s\n"
15240 #define FORMAT2 "%-40.40s %-7.7s %-6.6s %-6.6s %-6.6s %-3.3s %-4.4s %-8.8s %s\n"
15241 int span;
15242 int res;
15243 char alarmstr[50];
15244
15245 int ctl;
15246 struct dahdi_spaninfo s;
15247
15248 switch (cmd) {
15249 case CLI_INIT:
15250 e->command = "dahdi show status";
15251 e->usage =
15252 "Usage: dahdi show status\n"
15253 " Shows a list of DAHDI cards with status\n";
15254 return NULL;
15255 case CLI_GENERATE:
15256 return NULL;
15257 }
15258 ctl = open("/dev/dahdi/ctl", O_RDWR);
15259 if (ctl < 0) {
15260 ast_cli(a->fd, "No DAHDI found. Unable to open /dev/dahdi/ctl: %s\n", strerror(errno));
15261 return CLI_FAILURE;
15262 }
15263 ast_cli(a->fd, FORMAT2, "Description", "Alarms", "IRQ", "bpviol", "CRC4", "Framing", "Coding", "Options", "LBO");
15264
15265 for (span = 1; span < DAHDI_MAX_SPANS; ++span) {
15266 s.spanno = span;
15267 res = ioctl(ctl, DAHDI_SPANSTAT, &s);
15268 if (res) {
15269 continue;
15270 }
15271 alarmstr[0] = '\0';
15272 if (s.alarms > 0) {
15273 if (s.alarms & DAHDI_ALARM_BLUE)
15274 strcat(alarmstr, "BLU/");
15275 if (s.alarms & DAHDI_ALARM_YELLOW)
15276 strcat(alarmstr, "YEL/");
15277 if (s.alarms & DAHDI_ALARM_RED)
15278 strcat(alarmstr, "RED/");
15279 if (s.alarms & DAHDI_ALARM_LOOPBACK)
15280 strcat(alarmstr, "LB/");
15281 if (s.alarms & DAHDI_ALARM_RECOVER)
15282 strcat(alarmstr, "REC/");
15283 if (s.alarms & DAHDI_ALARM_NOTOPEN)
15284 strcat(alarmstr, "NOP/");
15285 if (!strlen(alarmstr))
15286 strcat(alarmstr, "UUU/");
15287 if (strlen(alarmstr)) {
15288
15289 alarmstr[strlen(alarmstr) - 1] = '\0';
15290 }
15291 } else {
15292 if (s.numchans)
15293 strcpy(alarmstr, "OK");
15294 else
15295 strcpy(alarmstr, "UNCONFIGURED");
15296 }
15297
15298 ast_cli(a->fd, FORMAT, s.desc, alarmstr, s.irqmisses, s.bpvcount, s.crc4count,
15299 s.lineconfig & DAHDI_CONFIG_D4 ? "D4" :
15300 s.lineconfig & DAHDI_CONFIG_ESF ? "ESF" :
15301 s.lineconfig & DAHDI_CONFIG_CCS ? "CCS" :
15302 "CAS",
15303 s.lineconfig & DAHDI_CONFIG_B8ZS ? "B8ZS" :
15304 s.lineconfig & DAHDI_CONFIG_HDB3 ? "HDB3" :
15305 s.lineconfig & DAHDI_CONFIG_AMI ? "AMI" :
15306 "Unk",
15307 s.lineconfig & DAHDI_CONFIG_CRC4 ?
15308 s.lineconfig & DAHDI_CONFIG_NOTOPEN ? "CRC4/YEL" : "CRC4" :
15309 s.lineconfig & DAHDI_CONFIG_NOTOPEN ? "YEL" : "",
15310 lbostr[s.lbo]
15311 );
15312 }
15313 close(ctl);
15314
15315 return CLI_SUCCESS;
15316 #undef FORMAT
15317 #undef FORMAT2
15318 }
15319
15320 static char *dahdi_show_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15321 {
15322 int pseudo_fd = -1;
15323 struct dahdi_versioninfo vi;
15324
15325 switch (cmd) {
15326 case CLI_INIT:
15327 e->command = "dahdi show version";
15328 e->usage =
15329 "Usage: dahdi show version\n"
15330 " Shows the DAHDI version in use\n";
15331 return NULL;
15332 case CLI_GENERATE:
15333 return NULL;
15334 }
15335 if ((pseudo_fd = open("/dev/dahdi/ctl", O_RDONLY)) < 0) {
15336 ast_cli(a->fd, "Failed to open control file to get version.\n");
15337 return CLI_SUCCESS;
15338 }
15339
15340 strcpy(vi.version, "Unknown");
15341 strcpy(vi.echo_canceller, "Unknown");
15342
15343 if (ioctl(pseudo_fd, DAHDI_GETVERSION, &vi))
15344 ast_cli(a->fd, "Failed to get DAHDI version: %s\n", strerror(errno));
15345 else
15346 ast_cli(a->fd, "DAHDI Version: %s Echo Canceller: %s\n", vi.version, vi.echo_canceller);
15347
15348 close(pseudo_fd);
15349
15350 return CLI_SUCCESS;
15351 }
15352
15353 static char *dahdi_set_hwgain(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15354 {
15355 int channel;
15356 int gain;
15357 int tx;
15358 struct dahdi_hwgain hwgain;
15359 struct dahdi_pvt *tmp = NULL;
15360
15361 switch (cmd) {
15362 case CLI_INIT:
15363 e->command = "dahdi set hwgain";
15364 e->usage =
15365 "Usage: dahdi set hwgain <rx|tx> <chan#> <gain>\n"
15366 " Sets the hardware gain on a a given channel, overriding the\n"
15367 " value provided at module loadtime, whether the channel is in\n"
15368 " use or not. Changes take effect immediately.\n"
15369 " <rx|tx> which direction do you want to change (relative to our module)\n"
15370 " <chan num> is the channel number relative to the device\n"
15371 " <gain> is the gain in dB (e.g. -3.5 for -3.5dB)\n";
15372 return NULL;
15373 case CLI_GENERATE:
15374 return NULL;
15375 }
15376
15377 if (a->argc != 6)
15378 return CLI_SHOWUSAGE;
15379
15380 if (!strcasecmp("rx", a->argv[3]))
15381 tx = 0;
15382 else if (!strcasecmp("tx", a->argv[3]))
15383 tx = 1;
15384 else
15385 return CLI_SHOWUSAGE;
15386
15387 channel = atoi(a->argv[4]);
15388 gain = atof(a->argv[5])*10.0;
15389
15390 ast_mutex_lock(&iflock);
15391
15392 for (tmp = iflist; tmp; tmp = tmp->next) {
15393
15394 if (tmp->channel != channel)
15395 continue;
15396
15397 if (tmp->subs[SUB_REAL].dfd == -1)
15398 break;
15399
15400 hwgain.newgain = gain;
15401 hwgain.tx = tx;
15402 if (ioctl(tmp->subs[SUB_REAL].dfd, DAHDI_SET_HWGAIN, &hwgain) < 0) {
15403 ast_cli(a->fd, "Unable to set the hardware gain for channel %d: %s\n", channel, strerror(errno));
15404 ast_mutex_unlock(&iflock);
15405 return CLI_FAILURE;
15406 }
15407 ast_cli(a->fd, "hardware %s gain set to %d (%.1f dB) on channel %d\n",
15408 tx ? "tx" : "rx", gain, (float)gain/10.0, channel);
15409 break;
15410 }
15411
15412 ast_mutex_unlock(&iflock);
15413
15414 if (tmp)
15415 return CLI_SUCCESS;
15416
15417 ast_cli(a->fd, "Unable to find given channel %d\n", channel);
15418 return CLI_FAILURE;
15419
15420 }
15421
15422 static char *dahdi_set_swgain(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15423 {
15424 int channel;
15425 float gain;
15426 int tx;
15427 int res;
15428 ast_mutex_t *lock;
15429 struct dahdi_pvt *tmp = NULL;
15430
15431 switch (cmd) {
15432 case CLI_INIT:
15433 e->command = "dahdi set swgain";
15434 e->usage =
15435 "Usage: dahdi set swgain <rx|tx> <chan#> <gain>\n"
15436 " Sets the software gain on a a given channel, overriding the\n"
15437 " value provided at module loadtime, whether the channel is in\n"
15438 " use or not. Changes take effect immediately.\n"
15439 " <rx|tx> which direction do you want to change (relative to our module)\n"
15440 " <chan num> is the channel number relative to the device\n"
15441 " <gain> is the gain in dB (e.g. -3.5 for -3.5dB)\n";
15442 return NULL;
15443 case CLI_GENERATE:
15444 return NULL;
15445 }
15446
15447 lock = &iflock;
15448
15449 if (a->argc != 6)
15450 return CLI_SHOWUSAGE;
15451
15452 if (!strcasecmp("rx", a->argv[3]))
15453 tx = 0;
15454 else if (!strcasecmp("tx", a->argv[3]))
15455 tx = 1;
15456 else
15457 return CLI_SHOWUSAGE;
15458
15459 channel = atoi(a->argv[4]);
15460 gain = atof(a->argv[5]);
15461
15462 ast_mutex_lock(lock);
15463 for (tmp = iflist; tmp; tmp = tmp->next) {
15464
15465 if (tmp->channel != channel)
15466 continue;
15467
15468 if (tmp->subs[SUB_REAL].dfd == -1)
15469 break;
15470
15471 if (tx)
15472 res = set_actual_txgain(tmp->subs[SUB_REAL].dfd, channel, gain, tmp->law);
15473 else
15474 res = set_actual_rxgain(tmp->subs[SUB_REAL].dfd, channel, gain, tmp->law);
15475
15476 if (res) {
15477 ast_cli(a->fd, "Unable to set the software gain for channel %d\n", channel);
15478 ast_mutex_unlock(lock);
15479 return CLI_FAILURE;
15480 }
15481
15482 ast_cli(a->fd, "software %s gain set to %.1f on channel %d\n",
15483 tx ? "tx" : "rx", gain, channel);
15484 break;
15485 }
15486 ast_mutex_unlock(lock);
15487
15488 if (tmp)
15489 return CLI_SUCCESS;
15490
15491 ast_cli(a->fd, "Unable to find given channel %d\n", channel);
15492 return CLI_FAILURE;
15493
15494 }
15495
15496 static char *dahdi_set_dnd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15497 {
15498 int channel;
15499 int on;
15500 struct dahdi_pvt *dahdi_chan = NULL;
15501
15502 switch (cmd) {
15503 case CLI_INIT:
15504 e->command = "dahdi set dnd";
15505 e->usage =
15506 "Usage: dahdi set dnd <chan#> <on|off>\n"
15507 " Sets/resets DND (Do Not Disturb) mode on a channel.\n"
15508 " Changes take effect immediately.\n"
15509 " <chan num> is the channel number\n"
15510 " <on|off> Enable or disable DND mode?\n"
15511 ;
15512 return NULL;
15513 case CLI_GENERATE:
15514 return NULL;
15515 }
15516
15517 if (a->argc != 5)
15518 return CLI_SHOWUSAGE;
15519
15520 if ((channel = atoi(a->argv[3])) <= 0) {
15521 ast_cli(a->fd, "Expected channel number, got '%s'\n", a->argv[3]);
15522 return CLI_SHOWUSAGE;
15523 }
15524
15525 if (ast_true(a->argv[4]))
15526 on = 1;
15527 else if (ast_false(a->argv[4]))
15528 on = 0;
15529 else {
15530 ast_cli(a->fd, "Expected 'on' or 'off', got '%s'\n", a->argv[4]);
15531 return CLI_SHOWUSAGE;
15532 }
15533
15534 ast_mutex_lock(&iflock);
15535 for (dahdi_chan = iflist; dahdi_chan; dahdi_chan = dahdi_chan->next) {
15536 if (dahdi_chan->channel != channel)
15537 continue;
15538
15539
15540 dahdi_dnd(dahdi_chan, on);
15541 break;
15542 }
15543 ast_mutex_unlock(&iflock);
15544
15545 if (!dahdi_chan) {
15546 ast_cli(a->fd, "Unable to find given channel %d\n", channel);
15547 return CLI_FAILURE;
15548 }
15549
15550 return CLI_SUCCESS;
15551 }
15552
15553 static struct ast_cli_entry dahdi_cli[] = {
15554 AST_CLI_DEFINE(handle_dahdi_show_cadences, "List cadences"),
15555 AST_CLI_DEFINE(dahdi_show_channels, "Show active DAHDI channels"),
15556 AST_CLI_DEFINE(dahdi_show_channel, "Show information on a channel"),
15557 AST_CLI_DEFINE(dahdi_destroy_channel, "Destroy a channel"),
15558 AST_CLI_DEFINE(dahdi_restart_cmd, "Fully restart DAHDI channels"),
15559 AST_CLI_DEFINE(dahdi_show_status, "Show all DAHDI cards status"),
15560 AST_CLI_DEFINE(dahdi_show_version, "Show the DAHDI version in use"),
15561 AST_CLI_DEFINE(dahdi_set_hwgain, "Set hardware gain on a channel"),
15562 AST_CLI_DEFINE(dahdi_set_swgain, "Set software gain on a channel"),
15563 AST_CLI_DEFINE(dahdi_set_dnd, "Sets/resets DND (Do Not Disturb) mode on a channel"),
15564 };
15565
15566 #define TRANSFER 0
15567 #define HANGUP 1
15568
15569 static int dahdi_fake_event(struct dahdi_pvt *p, int mode)
15570 {
15571 if (p) {
15572 switch (mode) {
15573 case TRANSFER:
15574 p->fake_event = DAHDI_EVENT_WINKFLASH;
15575 break;
15576 case HANGUP:
15577 p->fake_event = DAHDI_EVENT_ONHOOK;
15578 break;
15579 default:
15580 ast_log(LOG_WARNING, "I don't know how to handle transfer event with this: %d on channel %s\n",mode, p->owner->name);
15581 }
15582 }
15583 return 0;
15584 }
15585 static struct dahdi_pvt *find_channel(int channel)
15586 {
15587 struct dahdi_pvt *p = iflist;
15588 while (p) {
15589 if (p->channel == channel) {
15590 break;
15591 }
15592 p = p->next;
15593 }
15594 return p;
15595 }
15596
15597 static int action_dahdidndon(struct mansession *s, const struct message *m)
15598 {
15599 struct dahdi_pvt *p = NULL;
15600 const char *channel = astman_get_header(m, "DAHDIChannel");
15601
15602 if (ast_strlen_zero(channel)) {
15603 astman_send_error(s, m, "No channel specified");
15604 return 0;
15605 }
15606 p = find_channel(atoi(channel));
15607 if (!p) {
15608 astman_send_error(s, m, "No such channel");
15609 return 0;
15610 }
15611 p->dnd = 1;
15612 astman_send_ack(s, m, "DND Enabled");
15613 return 0;
15614 }
15615
15616 static int action_dahdidndoff(struct mansession *s, const struct message *m)
15617 {
15618 struct dahdi_pvt *p = NULL;
15619 const char *channel = astman_get_header(m, "DAHDIChannel");
15620
15621 if (ast_strlen_zero(channel)) {
15622 astman_send_error(s, m, "No channel specified");
15623 return 0;
15624 }
15625 p = find_channel(atoi(channel));
15626 if (!p) {
15627 astman_send_error(s, m, "No such channel");
15628 return 0;
15629 }
15630 p->dnd = 0;
15631 astman_send_ack(s, m, "DND Disabled");
15632 return 0;
15633 }
15634
15635 static int action_transfer(struct mansession *s, const struct message *m)
15636 {
15637 struct dahdi_pvt *p = NULL;
15638 const char *channel = astman_get_header(m, "DAHDIChannel");
15639
15640 if (ast_strlen_zero(channel)) {
15641 astman_send_error(s, m, "No channel specified");
15642 return 0;
15643 }
15644 p = find_channel(atoi(channel));
15645 if (!p) {
15646 astman_send_error(s, m, "No such channel");
15647 return 0;
15648 }
15649 dahdi_fake_event(p,TRANSFER);
15650 astman_send_ack(s, m, "DAHDITransfer");
15651 return 0;
15652 }
15653
15654 static int action_transferhangup(struct mansession *s, const struct message *m)
15655 {
15656 struct dahdi_pvt *p = NULL;
15657 const char *channel = astman_get_header(m, "DAHDIChannel");
15658
15659 if (ast_strlen_zero(channel)) {
15660 astman_send_error(s, m, "No channel specified");
15661 return 0;
15662 }
15663 p = find_channel(atoi(channel));
15664 if (!p) {
15665 astman_send_error(s, m, "No such channel");
15666 return 0;
15667 }
15668 dahdi_fake_event(p,HANGUP);
15669 astman_send_ack(s, m, "DAHDIHangup");
15670 return 0;
15671 }
15672
15673 static int action_dahdidialoffhook(struct mansession *s, const struct message *m)
15674 {
15675 struct dahdi_pvt *p = NULL;
15676 const char *channel = astman_get_header(m, "DAHDIChannel");
15677 const char *number = astman_get_header(m, "Number");
15678 int i;
15679
15680 if (ast_strlen_zero(channel)) {
15681 astman_send_error(s, m, "No channel specified");
15682 return 0;
15683 }
15684 if (ast_strlen_zero(number)) {
15685 astman_send_error(s, m, "No number specified");
15686 return 0;
15687 }
15688 p = find_channel(atoi(channel));
15689 if (!p) {
15690 astman_send_error(s, m, "No such channel");
15691 return 0;
15692 }
15693 if (!p->owner) {
15694 astman_send_error(s, m, "Channel does not have it's owner");
15695 return 0;
15696 }
15697 for (i = 0; i < strlen(number); i++) {
15698 struct ast_frame f = { AST_FRAME_DTMF, number[i] };
15699 dahdi_queue_frame(p, &f, NULL);
15700 }
15701 astman_send_ack(s, m, "DAHDIDialOffhook");
15702 return 0;
15703 }
15704
15705 static int action_dahdishowchannels(struct mansession *s, const struct message *m)
15706 {
15707 struct dahdi_pvt *tmp = NULL;
15708 const char *id = astman_get_header(m, "ActionID");
15709 const char *dahdichannel = astman_get_header(m, "DAHDIChannel");
15710 char idText[256] = "";
15711 int channels = 0;
15712 int dahdichanquery = -1;
15713 if (!ast_strlen_zero(dahdichannel)) {
15714 dahdichanquery = atoi(dahdichannel);
15715 }
15716
15717 astman_send_ack(s, m, "DAHDI channel status will follow");
15718 if (!ast_strlen_zero(id))
15719 snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
15720
15721 ast_mutex_lock(&iflock);
15722
15723 tmp = iflist;
15724 while (tmp) {
15725 if (tmp->channel > 0) {
15726 int alm = get_alarms(tmp);
15727
15728
15729 if (dahdichanquery > 0 && tmp->channel != dahdichanquery)
15730 continue;
15731
15732 channels++;
15733 if (tmp->owner) {
15734
15735 astman_append(s,
15736 "Event: DAHDIShowChannels\r\n"
15737 "DAHDIChannel: %d\r\n"
15738 "Channel: %s\r\n"
15739 "Uniqueid: %s\r\n"
15740 "AccountCode: %s\r\n"
15741 "Signalling: %s\r\n"
15742 "SignallingCode: %d\r\n"
15743 "Context: %s\r\n"
15744 "DND: %s\r\n"
15745 "Alarm: %s\r\n"
15746 "%s"
15747 "\r\n",
15748 tmp->channel,
15749 tmp->owner->name,
15750 tmp->owner->uniqueid,
15751 tmp->owner->accountcode,
15752 sig2str(tmp->sig),
15753 tmp->sig,
15754 tmp->context,
15755 tmp->dnd ? "Enabled" : "Disabled",
15756 alarm2str(alm), idText);
15757 } else {
15758 astman_append(s,
15759 "Event: DAHDIShowChannels\r\n"
15760 "DAHDIChannel: %d\r\n"
15761 "Signalling: %s\r\n"
15762 "SignallingCode: %d\r\n"
15763 "Context: %s\r\n"
15764 "DND: %s\r\n"
15765 "Alarm: %s\r\n"
15766 "%s"
15767 "\r\n",
15768 tmp->channel, sig2str(tmp->sig), tmp->sig,
15769 tmp->context,
15770 tmp->dnd ? "Enabled" : "Disabled",
15771 alarm2str(alm), idText);
15772 }
15773 }
15774
15775 tmp = tmp->next;
15776 }
15777
15778 ast_mutex_unlock(&iflock);
15779
15780 astman_append(s,
15781 "Event: DAHDIShowChannelsComplete\r\n"
15782 "%s"
15783 "Items: %d\r\n"
15784 "\r\n",
15785 idText,
15786 channels);
15787 return 0;
15788 }
15789
15790 #if defined(HAVE_SS7)
15791 static int linkset_addsigchan(int sigchan)
15792 {
15793 struct dahdi_ss7 *link;
15794 int res;
15795 int curfd;
15796 struct dahdi_params p;
15797 struct dahdi_bufferinfo bi;
15798 struct dahdi_spaninfo si;
15799
15800
15801 link = ss7_resolve_linkset(cur_linkset);
15802 if (!link) {
15803 ast_log(LOG_ERROR, "Invalid linkset number. Must be between 1 and %d\n", NUM_SPANS + 1);
15804 return -1;
15805 }
15806
15807 if (cur_ss7type < 0) {
15808 ast_log(LOG_ERROR, "Unspecified or invalid ss7type\n");
15809 return -1;
15810 }
15811
15812 if (!link->ss7)
15813 link->ss7 = ss7_new(cur_ss7type);
15814
15815 if (!link->ss7) {
15816 ast_log(LOG_ERROR, "Can't create new SS7!\n");
15817 return -1;
15818 }
15819
15820 link->type = cur_ss7type;
15821
15822 if (cur_pointcode < 0) {
15823 ast_log(LOG_ERROR, "Unspecified pointcode!\n");
15824 return -1;
15825 } else
15826 ss7_set_pc(link->ss7, cur_pointcode);
15827
15828 if (sigchan < 0) {
15829 ast_log(LOG_ERROR, "Invalid sigchan!\n");
15830 return -1;
15831 } else {
15832 if (link->numsigchans >= NUM_DCHANS) {
15833 ast_log(LOG_ERROR, "Too many sigchans on linkset %d\n", cur_linkset);
15834 return -1;
15835 }
15836 curfd = link->numsigchans;
15837
15838 link->fds[curfd] = open("/dev/dahdi/channel", O_RDWR, 0600);
15839 if ((link->fds[curfd] < 0) || (ioctl(link->fds[curfd],DAHDI_SPECIFY,&sigchan) == -1)) {
15840 ast_log(LOG_ERROR, "Unable to open SS7 sigchan %d (%s)\n", sigchan, strerror(errno));
15841 return -1;
15842 }
15843 memset(&p, 0, sizeof(p));
15844 res = ioctl(link->fds[curfd], DAHDI_GET_PARAMS, &p);
15845 if (res) {
15846 dahdi_close_ss7_fd(link, curfd);
15847 ast_log(LOG_ERROR, "Unable to get parameters for sigchan %d (%s)\n", sigchan, strerror(errno));
15848 return -1;
15849 }
15850 if ((p.sigtype != DAHDI_SIG_HDLCFCS) && (p.sigtype != DAHDI_SIG_HARDHDLC) && (p.sigtype != DAHDI_SIG_MTP2)) {
15851 dahdi_close_ss7_fd(link, curfd);
15852 ast_log(LOG_ERROR, "sigchan %d is not in HDLC/FCS mode.\n", sigchan);
15853 return -1;
15854 }
15855
15856 memset(&bi, 0, sizeof(bi));
15857 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
15858 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
15859 bi.numbufs = 32;
15860 bi.bufsize = 512;
15861
15862 if (ioctl(link->fds[curfd], DAHDI_SET_BUFINFO, &bi)) {
15863 ast_log(LOG_ERROR, "Unable to set appropriate buffering on channel %d: %s\n", sigchan, strerror(errno));
15864 dahdi_close_ss7_fd(link, curfd);
15865 return -1;
15866 }
15867
15868 if (p.sigtype == DAHDI_SIG_MTP2)
15869 ss7_add_link(link->ss7, SS7_TRANSPORT_DAHDIMTP2, link->fds[curfd]);
15870 else
15871 ss7_add_link(link->ss7, SS7_TRANSPORT_DAHDIDCHAN, link->fds[curfd]);
15872
15873 link->numsigchans++;
15874
15875 memset(&si, 0, sizeof(si));
15876 res = ioctl(link->fds[curfd], DAHDI_SPANSTAT, &si);
15877 if (res) {
15878 dahdi_close_ss7_fd(link, curfd);
15879 ast_log(LOG_ERROR, "Unable to get span state for sigchan %d (%s)\n", sigchan, strerror(errno));
15880 }
15881
15882 if (!si.alarms) {
15883 link->linkstate[curfd] = LINKSTATE_DOWN;
15884 ss7_link_noalarm(link->ss7, link->fds[curfd]);
15885 } else {
15886 link->linkstate[curfd] = LINKSTATE_DOWN | LINKSTATE_INALARM;
15887 ss7_link_alarm(link->ss7, link->fds[curfd]);
15888 }
15889 }
15890
15891 if (cur_adjpointcode < 0) {
15892 ast_log(LOG_ERROR, "Unspecified adjpointcode!\n");
15893 return -1;
15894 } else {
15895 ss7_set_adjpc(link->ss7, link->fds[curfd], cur_adjpointcode);
15896 }
15897
15898 if (cur_defaultdpc < 0) {
15899 ast_log(LOG_ERROR, "Unspecified defaultdpc!\n");
15900 return -1;
15901 }
15902
15903 if (cur_networkindicator < 0) {
15904 ast_log(LOG_ERROR, "Invalid networkindicator!\n");
15905 return -1;
15906 } else
15907 ss7_set_network_ind(link->ss7, cur_networkindicator);
15908
15909 return 0;
15910 }
15911 #endif
15912
15913 #if defined(HAVE_SS7)
15914 static char *handle_ss7_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15915 {
15916 int span;
15917 switch (cmd) {
15918 case CLI_INIT:
15919 e->command = "ss7 set debug {on|off} linkset";
15920 e->usage =
15921 "Usage: ss7 set debug {on|off} linkset <linkset>\n"
15922 " Enables debugging on a given SS7 linkset\n";
15923 return NULL;
15924 case CLI_GENERATE:
15925 return NULL;
15926 }
15927 if (a->argc < 6)
15928 return CLI_SHOWUSAGE;
15929 span = atoi(a->argv[5]);
15930 if ((span < 1) || (span > NUM_SPANS)) {
15931 ast_cli(a->fd, "Invalid linkset %s. Should be a number from %d to %d\n", a->argv[5], 1, NUM_SPANS);
15932 return CLI_SUCCESS;
15933 }
15934 if (!linksets[span-1].ss7) {
15935 ast_cli(a->fd, "No SS7 running on linkset %d\n", span);
15936 return CLI_SUCCESS;
15937 }
15938 if (linksets[span-1].ss7) {
15939 if (!strcasecmp(a->argv[3], "on")) {
15940 ss7_set_debug(linksets[span-1].ss7, SS7_DEBUG_MTP2 | SS7_DEBUG_MTP3 | SS7_DEBUG_ISUP);
15941 ast_cli(a->fd, "Enabled debugging on linkset %d\n", span);
15942 } else {
15943 ss7_set_debug(linksets[span-1].ss7, 0);
15944 ast_cli(a->fd, "Disabled debugging on linkset %d\n", span);
15945 }
15946 }
15947
15948 return CLI_SUCCESS;
15949 }
15950 #endif
15951
15952 #if defined(HAVE_SS7)
15953 static char *handle_ss7_block_cic(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
15954 {
15955 int linkset, cic;
15956 int blocked = -1, i;
15957 switch (cmd) {
15958 case CLI_INIT:
15959 e->command = "ss7 block cic";
15960 e->usage =
15961 "Usage: ss7 block cic <linkset> <CIC>\n"
15962 " Sends a remote blocking request for the given CIC on the specified linkset\n";
15963 return NULL;
15964 case CLI_GENERATE:
15965 return NULL;
15966 }
15967 if (a->argc == 5)
15968 linkset = atoi(a->argv[3]);
15969 else
15970 return CLI_SHOWUSAGE;
15971
15972 if ((linkset < 1) || (linkset > NUM_SPANS)) {
15973 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
15974 return CLI_SUCCESS;
15975 }
15976
15977 if (!linksets[linkset-1].ss7) {
15978 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
15979 return CLI_SUCCESS;
15980 }
15981
15982 cic = atoi(a->argv[4]);
15983
15984 if (cic < 1) {
15985 ast_cli(a->fd, "Invalid CIC specified!\n");
15986 return CLI_SUCCESS;
15987 }
15988
15989 for (i = 0; i < linksets[linkset-1].numchans; i++) {
15990 if (linksets[linkset-1].pvts[i]->cic == cic) {
15991 blocked = linksets[linkset-1].pvts[i]->locallyblocked;
15992 if (!blocked) {
15993 ast_mutex_lock(&linksets[linkset-1].lock);
15994 isup_blo(linksets[linkset-1].ss7, cic, linksets[linkset-1].pvts[i]->dpc);
15995 ast_mutex_unlock(&linksets[linkset-1].lock);
15996 }
15997 }
15998 }
15999
16000 if (blocked < 0) {
16001 ast_cli(a->fd, "Invalid CIC specified!\n");
16002 return CLI_SUCCESS;
16003 }
16004
16005 if (!blocked)
16006 ast_cli(a->fd, "Sent blocking request for linkset %d on CIC %d\n", linkset, cic);
16007 else
16008 ast_cli(a->fd, "CIC %d already locally blocked\n", cic);
16009
16010
16011 pthread_kill(linksets[linkset-1].master, SIGURG);
16012
16013 return CLI_SUCCESS;
16014 }
16015 #endif
16016
16017 #if defined(HAVE_SS7)
16018 static char *handle_ss7_block_linkset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16019 {
16020 int linkset;
16021 int i;
16022 switch (cmd) {
16023 case CLI_INIT:
16024 e->command = "ss7 block linkset";
16025 e->usage =
16026 "Usage: ss7 block linkset <linkset number>\n"
16027 " Sends a remote blocking request for all CICs on the given linkset\n";
16028 return NULL;
16029 case CLI_GENERATE:
16030 return NULL;
16031 }
16032 if (a->argc == 4)
16033 linkset = atoi(a->argv[3]);
16034 else
16035 return CLI_SHOWUSAGE;
16036
16037 if ((linkset < 1) || (linkset > NUM_SPANS)) {
16038 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
16039 return CLI_SUCCESS;
16040 }
16041
16042 if (!linksets[linkset-1].ss7) {
16043 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
16044 return CLI_SUCCESS;
16045 }
16046
16047 for (i = 0; i < linksets[linkset-1].numchans; i++) {
16048 ast_cli(a->fd, "Sending remote blocking request on CIC %d\n", linksets[linkset-1].pvts[i]->cic);
16049 ast_mutex_lock(&linksets[linkset-1].lock);
16050 isup_blo(linksets[linkset-1].ss7, linksets[linkset-1].pvts[i]->cic, linksets[linkset-1].pvts[i]->dpc);
16051 ast_mutex_unlock(&linksets[linkset-1].lock);
16052 }
16053
16054
16055 pthread_kill(linksets[linkset-1].master, SIGURG);
16056
16057 return CLI_SUCCESS;
16058 }
16059 #endif
16060
16061 #if defined(HAVE_SS7)
16062 static char *handle_ss7_unblock_cic(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16063 {
16064 int linkset, cic;
16065 int i, blocked = -1;
16066 switch (cmd) {
16067 case CLI_INIT:
16068 e->command = "ss7 unblock cic";
16069 e->usage =
16070 "Usage: ss7 unblock cic <linkset> <CIC>\n"
16071 " Sends a remote unblocking request for the given CIC on the specified linkset\n";
16072 return NULL;
16073 case CLI_GENERATE:
16074 return NULL;
16075 }
16076
16077 if (a->argc == 5)
16078 linkset = atoi(a->argv[3]);
16079 else
16080 return CLI_SHOWUSAGE;
16081
16082 if ((linkset < 1) || (linkset > NUM_SPANS)) {
16083 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
16084 return CLI_SUCCESS;
16085 }
16086
16087 if (!linksets[linkset-1].ss7) {
16088 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
16089 return CLI_SUCCESS;
16090 }
16091
16092 cic = atoi(a->argv[4]);
16093
16094 if (cic < 1) {
16095 ast_cli(a->fd, "Invalid CIC specified!\n");
16096 return CLI_SUCCESS;
16097 }
16098
16099 for (i = 0; i < linksets[linkset-1].numchans; i++) {
16100 if (linksets[linkset-1].pvts[i]->cic == cic) {
16101 blocked = linksets[linkset-1].pvts[i]->locallyblocked;
16102 if (blocked) {
16103 ast_mutex_lock(&linksets[linkset-1].lock);
16104 isup_ubl(linksets[linkset-1].ss7, cic, linksets[linkset-1].pvts[i]->dpc);
16105 ast_mutex_unlock(&linksets[linkset-1].lock);
16106 }
16107 }
16108 }
16109
16110 if (blocked > 0)
16111 ast_cli(a->fd, "Sent unblocking request for linkset %d on CIC %d\n", linkset, cic);
16112
16113
16114 pthread_kill(linksets[linkset-1].master, SIGURG);
16115
16116 return CLI_SUCCESS;
16117 }
16118 #endif
16119
16120 #if defined(HAVE_SS7)
16121 static char *handle_ss7_unblock_linkset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16122 {
16123 int linkset;
16124 int i;
16125 switch (cmd) {
16126 case CLI_INIT:
16127 e->command = "ss7 unblock linkset";
16128 e->usage =
16129 "Usage: ss7 unblock linkset <linkset number>\n"
16130 " Sends a remote unblocking request for all CICs on the specified linkset\n";
16131 return NULL;
16132 case CLI_GENERATE:
16133 return NULL;
16134 }
16135
16136 if (a->argc == 4)
16137 linkset = atoi(a->argv[3]);
16138 else
16139 return CLI_SHOWUSAGE;
16140
16141 if ((linkset < 1) || (linkset > NUM_SPANS)) {
16142 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
16143 return CLI_SUCCESS;
16144 }
16145
16146 if (!linksets[linkset-1].ss7) {
16147 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
16148 return CLI_SUCCESS;
16149 }
16150
16151 for (i = 0; i < linksets[linkset-1].numchans; i++) {
16152 ast_cli(a->fd, "Sending remote unblock request on CIC %d\n", linksets[linkset-1].pvts[i]->cic);
16153 ast_mutex_lock(&linksets[linkset-1].lock);
16154 isup_ubl(linksets[linkset-1].ss7, linksets[linkset-1].pvts[i]->cic, linksets[linkset-1].pvts[i]->dpc);
16155 ast_mutex_unlock(&linksets[linkset-1].lock);
16156 }
16157
16158
16159 pthread_kill(linksets[linkset-1].master, SIGURG);
16160
16161 return CLI_SUCCESS;
16162 }
16163 #endif
16164
16165 #if defined(HAVE_SS7)
16166 static char *handle_ss7_show_linkset(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16167 {
16168 int linkset;
16169 struct dahdi_ss7 *ss7;
16170 switch (cmd) {
16171 case CLI_INIT:
16172 e->command = "ss7 show linkset";
16173 e->usage =
16174 "Usage: ss7 show linkset <span>\n"
16175 " Shows the status of an SS7 linkset.\n";
16176 return NULL;
16177 case CLI_GENERATE:
16178 return NULL;
16179 }
16180
16181 if (a->argc < 4)
16182 return CLI_SHOWUSAGE;
16183 linkset = atoi(a->argv[3]);
16184 if ((linkset < 1) || (linkset > NUM_SPANS)) {
16185 ast_cli(a->fd, "Invalid linkset %s. Should be a number %d to %d\n", a->argv[3], 1, NUM_SPANS);
16186 return CLI_SUCCESS;
16187 }
16188 if (!linksets[linkset-1].ss7) {
16189 ast_cli(a->fd, "No SS7 running on linkset %d\n", linkset);
16190 return CLI_SUCCESS;
16191 }
16192 if (linksets[linkset-1].ss7)
16193 ss7 = &linksets[linkset-1];
16194
16195 ast_cli(a->fd, "SS7 linkset %d status: %s\n", linkset, (ss7->state == LINKSET_STATE_UP) ? "Up" : "Down");
16196
16197 return CLI_SUCCESS;
16198 }
16199 #endif
16200
16201 #if defined(HAVE_SS7)
16202 static char *handle_ss7_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
16203 {
16204 switch (cmd) {
16205 case CLI_INIT:
16206 e->command = "ss7 show version";
16207 e->usage =
16208 "Usage: ss7 show version\n"
16209 " Show the libss7 version\n";
16210 return NULL;
16211 case CLI_GENERATE:
16212 return NULL;
16213 }
16214
16215 ast_cli(a->fd, "libss7 version: %s\n", ss7_get_version());
16216
16217 return CLI_SUCCESS;
16218 }
16219 #endif
16220
16221 #if defined(HAVE_SS7)
16222 static struct ast_cli_entry dahdi_ss7_cli[] = {
16223 AST_CLI_DEFINE(handle_ss7_debug, "Enables SS7 debugging on a linkset"),
16224 AST_CLI_DEFINE(handle_ss7_block_cic, "Blocks the given CIC"),
16225 AST_CLI_DEFINE(handle_ss7_unblock_cic, "Unblocks the given CIC"),
16226 AST_CLI_DEFINE(handle_ss7_block_linkset, "Blocks all CICs on a linkset"),
16227 AST_CLI_DEFINE(handle_ss7_unblock_linkset, "Unblocks all CICs on a linkset"),
16228 AST_CLI_DEFINE(handle_ss7_show_linkset, "Shows the status of a linkset"),
16229 AST_CLI_DEFINE(handle_ss7_version, "Displays libss7 version"),
16230 };
16231 #endif
16232
16233 static int __unload_module(void)
16234 {
16235 struct dahdi_pvt *p;
16236 #if defined(HAVE_PRI) || defined(HAVE_SS7)
16237 int i, j;
16238 #endif
16239
16240 #ifdef HAVE_PRI
16241 for (i = 0; i < NUM_SPANS; i++) {
16242 if (pris[i].master != AST_PTHREADT_NULL)
16243 pthread_cancel(pris[i].master);
16244 }
16245 ast_cli_unregister_multiple(dahdi_pri_cli, ARRAY_LEN(dahdi_pri_cli));
16246 ast_unregister_application(dahdi_send_keypad_facility_app);
16247 #ifdef HAVE_PRI_PROG_W_CAUSE
16248 ast_unregister_application(dahdi_send_callrerouting_facility_app);
16249 #endif
16250 #endif
16251 #if defined(HAVE_SS7)
16252 for (i = 0; i < NUM_SPANS; i++) {
16253 if (linksets[i].master != AST_PTHREADT_NULL)
16254 pthread_cancel(linksets[i].master);
16255 }
16256 ast_cli_unregister_multiple(dahdi_ss7_cli, ARRAY_LEN(dahdi_ss7_cli));
16257 #endif
16258 #if defined(HAVE_OPENR2)
16259 dahdi_r2_destroy_links();
16260 ast_cli_unregister_multiple(dahdi_mfcr2_cli, ARRAY_LEN(dahdi_mfcr2_cli));
16261 ast_unregister_application(dahdi_accept_r2_call_app);
16262 #endif
16263
16264 ast_cli_unregister_multiple(dahdi_cli, ARRAY_LEN(dahdi_cli));
16265 ast_manager_unregister( "DAHDIDialOffhook" );
16266 ast_manager_unregister( "DAHDIHangup" );
16267 ast_manager_unregister( "DAHDITransfer" );
16268 ast_manager_unregister( "DAHDIDNDoff" );
16269 ast_manager_unregister( "DAHDIDNDon" );
16270 ast_manager_unregister("DAHDIShowChannels");
16271 ast_manager_unregister("DAHDIRestart");
16272 ast_channel_unregister(&dahdi_tech);
16273 ast_mutex_lock(&iflock);
16274
16275 p = iflist;
16276 while (p) {
16277 if (p->owner)
16278 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
16279 p = p->next;
16280 }
16281 ast_mutex_unlock(&iflock);
16282 ast_mutex_lock(&monlock);
16283 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
16284 pthread_cancel(monitor_thread);
16285 pthread_kill(monitor_thread, SIGURG);
16286 pthread_join(monitor_thread, NULL);
16287 }
16288 monitor_thread = AST_PTHREADT_STOP;
16289 ast_mutex_unlock(&monlock);
16290
16291 destroy_all_channels();
16292
16293 #if defined(HAVE_PRI)
16294 for (i = 0; i < NUM_SPANS; i++) {
16295 if (pris[i].master && (pris[i].master != AST_PTHREADT_NULL))
16296 pthread_join(pris[i].master, NULL);
16297 for (j = 0; j < NUM_DCHANS; j++) {
16298 dahdi_close_pri_fd(&(pris[i]), j);
16299 }
16300 }
16301 #endif
16302
16303 #if defined(HAVE_SS7)
16304 for (i = 0; i < NUM_SPANS; i++) {
16305 if (linksets[i].master && (linksets[i].master != AST_PTHREADT_NULL))
16306 pthread_join(linksets[i].master, NULL);
16307 for (j = 0; j < NUM_DCHANS; j++) {
16308 dahdi_close_ss7_fd(&(linksets[i]), j);
16309 }
16310 }
16311 #endif
16312 ast_cond_destroy(&ss_thread_complete);
16313 return 0;
16314 }
16315
16316 static int unload_module(void)
16317 {
16318 #if defined(HAVE_PRI) || defined(HAVE_SS7)
16319 int y;
16320 #endif
16321 #ifdef HAVE_PRI
16322 for (y = 0; y < NUM_SPANS; y++)
16323 ast_mutex_destroy(&pris[y].lock);
16324 #endif
16325 #ifdef HAVE_SS7
16326 for (y = 0; y < NUM_SPANS; y++)
16327 ast_mutex_destroy(&linksets[y].lock);
16328 #endif
16329 return __unload_module();
16330 }
16331
16332 static int build_channels(struct dahdi_chan_conf *conf, int iscrv, const char *value, int reload, int lineno, int *found_pseudo)
16333 {
16334 char *c, *chan;
16335 int x, start, finish;
16336 struct dahdi_pvt *tmp;
16337 #ifdef HAVE_PRI
16338 struct dahdi_pri *pri;
16339 int trunkgroup, y;
16340 #endif
16341
16342 if ((reload == 0) && (conf->chan.sig < 0) && !conf->is_sig_auto) {
16343 ast_log(LOG_ERROR, "Signalling must be specified before any channels are.\n");
16344 return -1;
16345 }
16346
16347 c = ast_strdupa(value);
16348
16349 #ifdef HAVE_PRI
16350 pri = NULL;
16351 if (iscrv) {
16352 if (sscanf(c, "%30d:%n", &trunkgroup, &y) != 1) {
16353 ast_log(LOG_WARNING, "CRV must begin with trunkgroup followed by a colon at line %d.\n", lineno);
16354 return -1;
16355 }
16356 if (trunkgroup < 1) {
16357 ast_log(LOG_WARNING, "CRV trunk group must be a positive number at line %d.\n", lineno);
16358 return -1;
16359 }
16360 c += y;
16361 for (y = 0; y < NUM_SPANS; y++) {
16362 if (pris[y].trunkgroup == trunkgroup) {
16363 pri = pris + y;
16364 break;
16365 }
16366 }
16367 if (!pri) {
16368 ast_log(LOG_WARNING, "No such trunk group %d at CRV declaration at line %d.\n", trunkgroup, lineno);
16369 return -1;
16370 }
16371 }
16372 #endif
16373
16374 while ((chan = strsep(&c, ","))) {
16375 if (sscanf(chan, "%30d-%30d", &start, &finish) == 2) {
16376
16377 } else if (sscanf(chan, "%30d", &start)) {
16378
16379 finish = start;
16380 } else if (!strcasecmp(chan, "pseudo")) {
16381 finish = start = CHAN_PSEUDO;
16382 if (found_pseudo)
16383 *found_pseudo = 1;
16384 } else {
16385 ast_log(LOG_ERROR, "Syntax error parsing '%s' at '%s'\n", value, chan);
16386 return -1;
16387 }
16388 if (finish < start) {
16389 ast_log(LOG_WARNING, "Sillyness: %d < %d\n", start, finish);
16390 x = finish;
16391 finish = start;
16392 start = x;
16393 }
16394
16395 for (x = start; x <= finish; x++) {
16396 #ifdef HAVE_PRI
16397 tmp = mkintf(x, conf, pri, reload);
16398 #else
16399 tmp = mkintf(x, conf, NULL, reload);
16400 #endif
16401
16402 if (tmp) {
16403 #ifdef HAVE_PRI
16404 if (pri)
16405 ast_verb(3, "%s CRV %d:%d, %s signalling\n", reload ? "Reconfigured" : "Registered", trunkgroup, x, sig2str(tmp->sig));
16406 else
16407 #endif
16408 ast_verb(3, "%s channel %d, %s signalling\n", reload ? "Reconfigured" : "Registered", x, sig2str(tmp->sig));
16409 } else {
16410 ast_log(LOG_ERROR, "Unable to %s channel '%s'\n",
16411 (reload == 1) ? "reconfigure" : "register", value);
16412 return -1;
16413 }
16414 }
16415 }
16416
16417 return 0;
16418 }
16419
16420
16421
16422 #define MAX_CHANLIST_LEN 80
16423
16424 static void process_echocancel(struct dahdi_chan_conf *confp, const char *data, unsigned int line)
16425 {
16426 char *parse = ast_strdupa(data);
16427 char *params[DAHDI_MAX_ECHOCANPARAMS + 1];
16428 unsigned int param_count;
16429 unsigned int x;
16430
16431 if (!(param_count = ast_app_separate_args(parse, ',', params, ARRAY_LEN(params))))
16432 return;
16433
16434 memset(&confp->chan.echocancel, 0, sizeof(confp->chan.echocancel));
16435
16436
16437
16438 x = ast_strlen_zero(params[0]) ? 0 : atoi(params[0]);
16439
16440 if ((x == 32) || (x == 64) || (x == 128) || (x == 256) || (x == 512) || (x == 1024))
16441 confp->chan.echocancel.head.tap_length = x;
16442 else if ((confp->chan.echocancel.head.tap_length = ast_true(params[0])))
16443 confp->chan.echocancel.head.tap_length = 128;
16444
16445
16446
16447 for (x = 1; x < param_count; x++) {
16448 struct {
16449 char *name;
16450 char *value;
16451 } param;
16452
16453 if (ast_app_separate_args(params[x], '=', (char **) ¶m, 2) < 1) {
16454 ast_log(LOG_WARNING, "Invalid echocancel parameter supplied at line %d: '%s'\n", line, params[x]);
16455 continue;
16456 }
16457
16458 if (ast_strlen_zero(param.name) || (strlen(param.name) > sizeof(confp->chan.echocancel.params[0].name)-1)) {
16459 ast_log(LOG_WARNING, "Invalid echocancel parameter supplied at line %d: '%s'\n", line, param.name);
16460 continue;
16461 }
16462
16463 strcpy(confp->chan.echocancel.params[confp->chan.echocancel.head.param_count].name, param.name);
16464
16465 if (param.value) {
16466 if (sscanf(param.value, "%30d", &confp->chan.echocancel.params[confp->chan.echocancel.head.param_count].value) != 1) {
16467 ast_log(LOG_WARNING, "Invalid echocancel parameter value supplied at line %d: '%s'\n", line, param.value);
16468 continue;
16469 }
16470 }
16471 confp->chan.echocancel.head.param_count++;
16472 }
16473 }
16474
16475
16476 #define PROC_DAHDI_OPT_NOCHAN (1 << 0)
16477
16478 #define PROC_DAHDI_OPT_NOWARN (1 << 1)
16479
16480 static int process_dahdi(struct dahdi_chan_conf *confp, const char *cat, struct ast_variable *v, int reload, int options)
16481 {
16482 struct dahdi_pvt *tmp;
16483 int y;
16484 int found_pseudo = 0;
16485 char dahdichan[MAX_CHANLIST_LEN] = {};
16486
16487 for (; v; v = v->next) {
16488 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
16489 continue;
16490
16491
16492 if (!strcasecmp(v->name, "parkinglot")) {
16493 ast_copy_string(confp->chan.parkinglot, v->value, sizeof(confp->chan.parkinglot));
16494 }
16495
16496
16497 if (!strcasecmp(v->name, "channel")
16498 #ifdef HAVE_PRI
16499 || !strcasecmp(v->name, "crv")
16500 #endif
16501 ) {
16502 int iscrv;
16503 if (options & PROC_DAHDI_OPT_NOCHAN) {
16504 ast_log(LOG_WARNING, "Channel '%s' ignored.\n", v->value);
16505 continue;
16506 }
16507 iscrv = !strcasecmp(v->name, "crv");
16508 if (build_channels(confp, iscrv, v->value, reload, v->lineno, &found_pseudo))
16509 return -1;
16510 ast_log(LOG_DEBUG, "Channel '%s' configured.\n", v->value);
16511 } else if (!strcasecmp(v->name, "buffers")) {
16512 if (parse_buffers_policy(v->value, &confp->chan.buf_no, &confp->chan.buf_policy)) {
16513 ast_log(LOG_WARNING, "Using default buffer policy.\n");
16514 confp->chan.buf_no = numbufs;
16515 confp->chan.buf_policy = DAHDI_POLICY_IMMEDIATE;
16516 }
16517 } else if (!strcasecmp(v->name, "faxbuffers")) {
16518 if (!parse_buffers_policy(v->value, &confp->chan.faxbuf_no, &confp->chan.faxbuf_policy)) {
16519 confp->chan.usefaxbuffers = 1;
16520 }
16521 } else if (!strcasecmp(v->name, "dahdichan")) {
16522 ast_copy_string(dahdichan, v->value, sizeof(dahdichan));
16523 } else if (!strcasecmp(v->name, "usedistinctiveringdetection")) {
16524 usedistinctiveringdetection = ast_true(v->value);
16525 } else if (!strcasecmp(v->name, "distinctiveringaftercid")) {
16526 distinctiveringaftercid = ast_true(v->value);
16527 } else if (!strcasecmp(v->name, "dring1context")) {
16528 ast_copy_string(confp->chan.drings.ringContext[0].contextData,v->value,sizeof(confp->chan.drings.ringContext[0].contextData));
16529 } else if (!strcasecmp(v->name, "dring2context")) {
16530 ast_copy_string(confp->chan.drings.ringContext[1].contextData,v->value,sizeof(confp->chan.drings.ringContext[1].contextData));
16531 } else if (!strcasecmp(v->name, "dring3context")) {
16532 ast_copy_string(confp->chan.drings.ringContext[2].contextData,v->value,sizeof(confp->chan.drings.ringContext[2].contextData));
16533 } else if (!strcasecmp(v->name, "dring1range")) {
16534 confp->chan.drings.ringnum[0].range = atoi(v->value);
16535 } else if (!strcasecmp(v->name, "dring2range")) {
16536 confp->chan.drings.ringnum[1].range = atoi(v->value);
16537 } else if (!strcasecmp(v->name, "dring3range")) {
16538 confp->chan.drings.ringnum[2].range = atoi(v->value);
16539 } else if (!strcasecmp(v->name, "dring1")) {
16540 sscanf(v->value, "%30d,%30d,%30d", &confp->chan.drings.ringnum[0].ring[0], &confp->chan.drings.ringnum[0].ring[1], &confp->chan.drings.ringnum[0].ring[2]);
16541 } else if (!strcasecmp(v->name, "dring2")) {
16542 sscanf(v->value, "%30d,%30d,%30d", &confp->chan.drings.ringnum[1].ring[0], &confp->chan.drings.ringnum[1].ring[1], &confp->chan.drings.ringnum[1].ring[2]);
16543 } else if (!strcasecmp(v->name, "dring3")) {
16544 sscanf(v->value, "%30d,%30d,%30d", &confp->chan.drings.ringnum[2].ring[0], &confp->chan.drings.ringnum[2].ring[1], &confp->chan.drings.ringnum[2].ring[2]);
16545 } else if (!strcasecmp(v->name, "usecallerid")) {
16546 confp->chan.use_callerid = ast_true(v->value);
16547 } else if (!strcasecmp(v->name, "cidsignalling")) {
16548 if (!strcasecmp(v->value, "bell"))
16549 confp->chan.cid_signalling = CID_SIG_BELL;
16550 else if (!strcasecmp(v->value, "v23"))
16551 confp->chan.cid_signalling = CID_SIG_V23;
16552 else if (!strcasecmp(v->value, "dtmf"))
16553 confp->chan.cid_signalling = CID_SIG_DTMF;
16554 else if (!strcasecmp(v->value, "smdi"))
16555 confp->chan.cid_signalling = CID_SIG_SMDI;
16556 else if (!strcasecmp(v->value, "v23_jp"))
16557 confp->chan.cid_signalling = CID_SIG_V23_JP;
16558 else if (ast_true(v->value))
16559 confp->chan.cid_signalling = CID_SIG_BELL;
16560 } else if (!strcasecmp(v->name, "cidstart")) {
16561 if (!strcasecmp(v->value, "ring"))
16562 confp->chan.cid_start = CID_START_RING;
16563 else if (!strcasecmp(v->value, "polarity_in"))
16564 confp->chan.cid_start = CID_START_POLARITY_IN;
16565 else if (!strcasecmp(v->value, "polarity"))
16566 confp->chan.cid_start = CID_START_POLARITY;
16567 else if (ast_true(v->value))
16568 confp->chan.cid_start = CID_START_RING;
16569 } else if (!strcasecmp(v->name, "threewaycalling")) {
16570 confp->chan.threewaycalling = ast_true(v->value);
16571 } else if (!strcasecmp(v->name, "cancallforward")) {
16572 confp->chan.cancallforward = ast_true(v->value);
16573 } else if (!strcasecmp(v->name, "relaxdtmf")) {
16574 if (ast_true(v->value))
16575 confp->chan.dtmfrelax = DSP_DIGITMODE_RELAXDTMF;
16576 else
16577 confp->chan.dtmfrelax = 0;
16578 } else if (!strcasecmp(v->name, "mailbox")) {
16579 ast_copy_string(confp->chan.mailbox, v->value, sizeof(confp->chan.mailbox));
16580 } else if (!strcasecmp(v->name, "hasvoicemail")) {
16581 if (ast_true(v->value) && ast_strlen_zero(confp->chan.mailbox)) {
16582 ast_copy_string(confp->chan.mailbox, cat, sizeof(confp->chan.mailbox));
16583 }
16584 } else if (!strcasecmp(v->name, "adsi")) {
16585 confp->chan.adsi = ast_true(v->value);
16586 } else if (!strcasecmp(v->name, "usesmdi")) {
16587 confp->chan.use_smdi = ast_true(v->value);
16588 } else if (!strcasecmp(v->name, "smdiport")) {
16589 ast_copy_string(confp->smdi_port, v->value, sizeof(confp->smdi_port));
16590 } else if (!strcasecmp(v->name, "transfer")) {
16591 confp->chan.transfer = ast_true(v->value);
16592 } else if (!strcasecmp(v->name, "canpark")) {
16593 confp->chan.canpark = ast_true(v->value);
16594 } else if (!strcasecmp(v->name, "echocancelwhenbridged")) {
16595 confp->chan.echocanbridged = ast_true(v->value);
16596 } else if (!strcasecmp(v->name, "busydetect")) {
16597 confp->chan.busydetect = ast_true(v->value);
16598 } else if (!strcasecmp(v->name, "busycount")) {
16599 confp->chan.busycount = atoi(v->value);
16600 } else if (!strcasecmp(v->name, "busypattern")) {
16601 if (sscanf(v->value, "%30d,%30d", &confp->chan.busy_tonelength, &confp->chan.busy_quietlength) != 2) {
16602 ast_log(LOG_ERROR, "busypattern= expects busypattern=tonelength,quietlength at line %d.\n", v->lineno);
16603 }
16604 } else if (!strcasecmp(v->name, "callprogress")) {
16605 confp->chan.callprogress &= ~CALLPROGRESS_PROGRESS;
16606 if (ast_true(v->value))
16607 confp->chan.callprogress |= CALLPROGRESS_PROGRESS;
16608 } else if (!strcasecmp(v->name, "waitfordialtone")) {
16609 confp->chan.waitfordialtone = atoi(v->value);
16610 } else if (!strcasecmp(v->name, "faxdetect")) {
16611 confp->chan.callprogress &= ~CALLPROGRESS_FAX;
16612 if (!strcasecmp(v->value, "incoming")) {
16613 confp->chan.callprogress |= CALLPROGRESS_FAX_INCOMING;
16614 } else if (!strcasecmp(v->value, "outgoing")) {
16615 confp->chan.callprogress |= CALLPROGRESS_FAX_OUTGOING;
16616 } else if (!strcasecmp(v->value, "both") || ast_true(v->value))
16617 confp->chan.callprogress |= CALLPROGRESS_FAX_INCOMING | CALLPROGRESS_FAX_OUTGOING;
16618 } else if (!strcasecmp(v->name, "echocancel")) {
16619 process_echocancel(confp, v->value, v->lineno);
16620 } else if (!strcasecmp(v->name, "echotraining")) {
16621 if (sscanf(v->value, "%30d", &y) == 1) {
16622 if ((y < 10) || (y > 4000)) {
16623 ast_log(LOG_WARNING, "Echo training time must be within the range of 10 to 4000 ms at line %d.\n", v->lineno);
16624 } else {
16625 confp->chan.echotraining = y;
16626 }
16627 } else if (ast_true(v->value)) {
16628 confp->chan.echotraining = 400;
16629 } else
16630 confp->chan.echotraining = 0;
16631 } else if (!strcasecmp(v->name, "hidecallerid")) {
16632 confp->chan.hidecallerid = ast_true(v->value);
16633 } else if (!strcasecmp(v->name, "hidecalleridname")) {
16634 confp->chan.hidecalleridname = ast_true(v->value);
16635 } else if (!strcasecmp(v->name, "pulsedial")) {
16636 confp->chan.pulse = ast_true(v->value);
16637 } else if (!strcasecmp(v->name, "callreturn")) {
16638 confp->chan.callreturn = ast_true(v->value);
16639 } else if (!strcasecmp(v->name, "callwaiting")) {
16640 confp->chan.callwaiting = ast_true(v->value);
16641 } else if (!strcasecmp(v->name, "callwaitingcallerid")) {
16642 confp->chan.callwaitingcallerid = ast_true(v->value);
16643 } else if (!strcasecmp(v->name, "context")) {
16644 ast_copy_string(confp->chan.context, v->value, sizeof(confp->chan.context));
16645 } else if (!strcasecmp(v->name, "language")) {
16646 ast_copy_string(confp->chan.language, v->value, sizeof(confp->chan.language));
16647 } else if (!strcasecmp(v->name, "progzone")) {
16648 ast_copy_string(progzone, v->value, sizeof(progzone));
16649 } else if (!strcasecmp(v->name, "mohinterpret")
16650 ||!strcasecmp(v->name, "musiconhold") || !strcasecmp(v->name, "musicclass")) {
16651 ast_copy_string(confp->chan.mohinterpret, v->value, sizeof(confp->chan.mohinterpret));
16652 } else if (!strcasecmp(v->name, "mohsuggest")) {
16653 ast_copy_string(confp->chan.mohsuggest, v->value, sizeof(confp->chan.mohsuggest));
16654 } else if (!strcasecmp(v->name, "parkinglot")) {
16655 ast_copy_string(parkinglot, v->value, sizeof(parkinglot));
16656 } else if (!strcasecmp(v->name, "stripmsd")) {
16657 ast_log(LOG_NOTICE, "Configuration option \"%s\" has been deprecated. Please use dialplan instead\n", v->name);
16658 confp->chan.stripmsd = atoi(v->value);
16659 } else if (!strcasecmp(v->name, "jitterbuffers")) {
16660 numbufs = atoi(v->value);
16661 } else if (!strcasecmp(v->name, "group")) {
16662 confp->chan.group = ast_get_group(v->value);
16663 } else if (!strcasecmp(v->name, "callgroup")) {
16664 if (!strcasecmp(v->value, "none"))
16665 confp->chan.callgroup = 0;
16666 else
16667 confp->chan.callgroup = ast_get_group(v->value);
16668 } else if (!strcasecmp(v->name, "pickupgroup")) {
16669 if (!strcasecmp(v->value, "none"))
16670 confp->chan.pickupgroup = 0;
16671 else
16672 confp->chan.pickupgroup = ast_get_group(v->value);
16673 } else if (!strcasecmp(v->name, "setvar")) {
16674 char *varname = ast_strdupa(v->value), *varval = NULL;
16675 struct ast_variable *tmpvar;
16676 if (varname && (varval = strchr(varname, '='))) {
16677 *varval++ = '\0';
16678 if ((tmpvar = ast_variable_new(varname, varval, ""))) {
16679 tmpvar->next = confp->chan.vars;
16680 confp->chan.vars = tmpvar;
16681 }
16682 }
16683 } else if (!strcasecmp(v->name, "immediate")) {
16684 confp->chan.immediate = ast_true(v->value);
16685 } else if (!strcasecmp(v->name, "transfertobusy")) {
16686 confp->chan.transfertobusy = ast_true(v->value);
16687 } else if (!strcasecmp(v->name, "mwimonitor")) {
16688 confp->chan.mwimonitor_neon = 0;
16689 confp->chan.mwimonitor_fsk = 0;
16690 confp->chan.mwimonitor_rpas = 0;
16691 if (strcasestr(v->value, "fsk")) {
16692 confp->chan.mwimonitor_fsk = 1;
16693 }
16694 if (strcasestr(v->value, "rpas")) {
16695 confp->chan.mwimonitor_rpas = 1;
16696 }
16697 if (strcasestr(v->value, "neon")) {
16698 confp->chan.mwimonitor_neon = 1;
16699 }
16700
16701 if (ast_true(v->value)) {
16702 confp->chan.mwimonitor_fsk = 1;
16703 }
16704 } else if (!strcasecmp(v->name, "cid_rxgain")) {
16705 if (sscanf(v->value, "%30f", &confp->chan.cid_rxgain) != 1) {
16706 ast_log(LOG_WARNING, "Invalid cid_rxgain: %s at line %d.\n", v->value, v->lineno);
16707 }
16708 } else if (!strcasecmp(v->name, "rxgain")) {
16709 if (sscanf(v->value, "%30f", &confp->chan.rxgain) != 1) {
16710 ast_log(LOG_WARNING, "Invalid rxgain: %s at line %d.\n", v->value, v->lineno);
16711 }
16712 } else if (!strcasecmp(v->name, "txgain")) {
16713 if (sscanf(v->value, "%30f", &confp->chan.txgain) != 1) {
16714 ast_log(LOG_WARNING, "Invalid txgain: %s at line %d.\n", v->value, v->lineno);
16715 }
16716 } else if (!strcasecmp(v->name, "tonezone")) {
16717 if (sscanf(v->value, "%30d", &confp->chan.tonezone) != 1) {
16718 ast_log(LOG_WARNING, "Invalid tonezone: %s at line %d.\n", v->value, v->lineno);
16719 }
16720 } else if (!strcasecmp(v->name, "callerid")) {
16721 if (!strcasecmp(v->value, "asreceived")) {
16722 confp->chan.cid_num[0] = '\0';
16723 confp->chan.cid_name[0] = '\0';
16724 } else {
16725 ast_callerid_split(v->value, confp->chan.cid_name, sizeof(confp->chan.cid_name), confp->chan.cid_num, sizeof(confp->chan.cid_num));
16726 }
16727 } else if (!strcasecmp(v->name, "fullname")) {
16728 ast_copy_string(confp->chan.cid_name, v->value, sizeof(confp->chan.cid_name));
16729 } else if (!strcasecmp(v->name, "cid_number")) {
16730 ast_copy_string(confp->chan.cid_num, v->value, sizeof(confp->chan.cid_num));
16731 } else if (!strcasecmp(v->name, "useincomingcalleridondahditransfer")) {
16732 confp->chan.dahditrcallerid = ast_true(v->value);
16733 } else if (!strcasecmp(v->name, "restrictcid")) {
16734 confp->chan.restrictcid = ast_true(v->value);
16735 } else if (!strcasecmp(v->name, "usecallingpres")) {
16736 confp->chan.use_callingpres = ast_true(v->value);
16737 } else if (!strcasecmp(v->name, "accountcode")) {
16738 ast_copy_string(confp->chan.accountcode, v->value, sizeof(confp->chan.accountcode));
16739 } else if (!strcasecmp(v->name, "amaflags")) {
16740 y = ast_cdr_amaflags2int(v->value);
16741 if (y < 0)
16742 ast_log(LOG_WARNING, "Invalid AMA flags: %s at line %d.\n", v->value, v->lineno);
16743 else
16744 confp->chan.amaflags = y;
16745 } else if (!strcasecmp(v->name, "polarityonanswerdelay")) {
16746 confp->chan.polarityonanswerdelay = atoi(v->value);
16747 } else if (!strcasecmp(v->name, "answeronpolarityswitch")) {
16748 confp->chan.answeronpolarityswitch = ast_true(v->value);
16749 } else if (!strcasecmp(v->name, "hanguponpolarityswitch")) {
16750 confp->chan.hanguponpolarityswitch = ast_true(v->value);
16751 } else if (!strcasecmp(v->name, "sendcalleridafter")) {
16752 confp->chan.sendcalleridafter = atoi(v->value);
16753 } else if (!strcasecmp(v->name, "mwimonitornotify")) {
16754 ast_copy_string(mwimonitornotify, v->value, sizeof(mwimonitornotify));
16755 } else if (!strcasecmp(v->name, "mwisendtype")) {
16756 #ifndef HAVE_DAHDI_LINEREVERSE_VMWI
16757 if (!strcasecmp(v->value, "rpas")) {
16758 mwisend_rpas = 1;
16759 } else {
16760 mwisend_rpas = 0;
16761 }
16762 #else
16763
16764 memset(&confp->chan.mwisend_setting, 0, sizeof(confp->chan.mwisend_setting));
16765 if (strcasestr(v->value, "nofsk")) {
16766 confp->chan.mwisend_fsk = 0;
16767 } else {
16768 confp->chan.mwisend_fsk = 1;
16769 }
16770 if (strcasestr(v->value, "rpas")) {
16771 confp->chan.mwisend_rpas = 1;
16772 } else {
16773 confp->chan.mwisend_rpas = 0;
16774 }
16775 if (strcasestr(v->value, "lrev")) {
16776 confp->chan.mwisend_setting.vmwi_type |= DAHDI_VMWI_LREV;
16777 }
16778 if (strcasestr(v->value, "hvdc")) {
16779 confp->chan.mwisend_setting.vmwi_type |= DAHDI_VMWI_HVDC;
16780 }
16781 if ( (strcasestr(v->value, "neon")) || (strcasestr(v->value, "hvac")) ){
16782 confp->chan.mwisend_setting.vmwi_type |= DAHDI_VMWI_HVAC;
16783 }
16784 #endif
16785 } else if (reload != 1) {
16786 if (!strcasecmp(v->name, "signalling") || !strcasecmp(v->name, "signaling")) {
16787 int orig_radio = confp->chan.radio;
16788 int orig_outsigmod = confp->chan.outsigmod;
16789 int orig_auto = confp->is_sig_auto;
16790
16791 confp->chan.radio = 0;
16792 confp->chan.outsigmod = -1;
16793 confp->is_sig_auto = 0;
16794 if (!strcasecmp(v->value, "em")) {
16795 confp->chan.sig = SIG_EM;
16796 } else if (!strcasecmp(v->value, "em_e1")) {
16797 confp->chan.sig = SIG_EM_E1;
16798 } else if (!strcasecmp(v->value, "em_w")) {
16799 confp->chan.sig = SIG_EMWINK;
16800 } else if (!strcasecmp(v->value, "fxs_ls")) {
16801 confp->chan.sig = SIG_FXSLS;
16802 } else if (!strcasecmp(v->value, "fxs_gs")) {
16803 confp->chan.sig = SIG_FXSGS;
16804 } else if (!strcasecmp(v->value, "fxs_ks")) {
16805 confp->chan.sig = SIG_FXSKS;
16806 } else if (!strcasecmp(v->value, "fxo_ls")) {
16807 confp->chan.sig = SIG_FXOLS;
16808 } else if (!strcasecmp(v->value, "fxo_gs")) {
16809 confp->chan.sig = SIG_FXOGS;
16810 } else if (!strcasecmp(v->value, "fxo_ks")) {
16811 confp->chan.sig = SIG_FXOKS;
16812 } else if (!strcasecmp(v->value, "fxs_rx")) {
16813 confp->chan.sig = SIG_FXSKS;
16814 confp->chan.radio = 1;
16815 } else if (!strcasecmp(v->value, "fxo_rx")) {
16816 confp->chan.sig = SIG_FXOLS;
16817 confp->chan.radio = 1;
16818 } else if (!strcasecmp(v->value, "fxs_tx")) {
16819 confp->chan.sig = SIG_FXSLS;
16820 confp->chan.radio = 1;
16821 } else if (!strcasecmp(v->value, "fxo_tx")) {
16822 confp->chan.sig = SIG_FXOGS;
16823 confp->chan.radio = 1;
16824 } else if (!strcasecmp(v->value, "em_rx")) {
16825 confp->chan.sig = SIG_EM;
16826 confp->chan.radio = 1;
16827 } else if (!strcasecmp(v->value, "em_tx")) {
16828 confp->chan.sig = SIG_EM;
16829 confp->chan.radio = 1;
16830 } else if (!strcasecmp(v->value, "em_rxtx")) {
16831 confp->chan.sig = SIG_EM;
16832 confp->chan.radio = 2;
16833 } else if (!strcasecmp(v->value, "em_txrx")) {
16834 confp->chan.sig = SIG_EM;
16835 confp->chan.radio = 2;
16836 } else if (!strcasecmp(v->value, "sf")) {
16837 confp->chan.sig = SIG_SF;
16838 } else if (!strcasecmp(v->value, "sf_w")) {
16839 confp->chan.sig = SIG_SFWINK;
16840 } else if (!strcasecmp(v->value, "sf_featd")) {
16841 confp->chan.sig = SIG_FEATD;
16842 } else if (!strcasecmp(v->value, "sf_featdmf")) {
16843 confp->chan.sig = SIG_FEATDMF;
16844 } else if (!strcasecmp(v->value, "sf_featb")) {
16845 confp->chan.sig = SIG_SF_FEATB;
16846 } else if (!strcasecmp(v->value, "sf")) {
16847 confp->chan.sig = SIG_SF;
16848 } else if (!strcasecmp(v->value, "sf_rx")) {
16849 confp->chan.sig = SIG_SF;
16850 confp->chan.radio = 1;
16851 } else if (!strcasecmp(v->value, "sf_tx")) {
16852 confp->chan.sig = SIG_SF;
16853 confp->chan.radio = 1;
16854 } else if (!strcasecmp(v->value, "sf_rxtx")) {
16855 confp->chan.sig = SIG_SF;
16856 confp->chan.radio = 2;
16857 } else if (!strcasecmp(v->value, "sf_txrx")) {
16858 confp->chan.sig = SIG_SF;
16859 confp->chan.radio = 2;
16860 } else if (!strcasecmp(v->value, "featd")) {
16861 confp->chan.sig = SIG_FEATD;
16862 } else if (!strcasecmp(v->value, "featdmf")) {
16863 confp->chan.sig = SIG_FEATDMF;
16864 } else if (!strcasecmp(v->value, "featdmf_ta")) {
16865 confp->chan.sig = SIG_FEATDMF_TA;
16866 } else if (!strcasecmp(v->value, "e911")) {
16867 confp->chan.sig = SIG_E911;
16868 } else if (!strcasecmp(v->value, "fgccama")) {
16869 confp->chan.sig = SIG_FGC_CAMA;
16870 } else if (!strcasecmp(v->value, "fgccamamf")) {
16871 confp->chan.sig = SIG_FGC_CAMAMF;
16872 } else if (!strcasecmp(v->value, "featb")) {
16873 confp->chan.sig = SIG_FEATB;
16874 #ifdef HAVE_PRI
16875 } else if (!strcasecmp(v->value, "pri_net")) {
16876 confp->chan.sig = SIG_PRI;
16877 confp->pri.nodetype = PRI_NETWORK;
16878 } else if (!strcasecmp(v->value, "pri_cpe")) {
16879 confp->chan.sig = SIG_PRI;
16880 confp->pri.nodetype = PRI_CPE;
16881 } else if (!strcasecmp(v->value, "bri_cpe")) {
16882 confp->chan.sig = SIG_BRI;
16883 confp->pri.nodetype = PRI_CPE;
16884 } else if (!strcasecmp(v->value, "bri_net")) {
16885 confp->chan.sig = SIG_BRI;
16886 confp->pri.nodetype = PRI_NETWORK;
16887 } else if (!strcasecmp(v->value, "bri_cpe_ptmp")) {
16888 confp->chan.sig = SIG_BRI_PTMP;
16889 confp->pri.nodetype = PRI_CPE;
16890 } else if (!strcasecmp(v->value, "bri_net_ptmp")) {
16891 ast_log(LOG_WARNING, "How cool would it be if someone implemented this mode! For now, sucks for you. (line %d)\n", v->lineno);
16892 } else if (!strcasecmp(v->value, "gr303fxoks_net")) {
16893 confp->chan.sig = SIG_GR303FXOKS;
16894 confp->pri.nodetype = PRI_NETWORK;
16895 } else if (!strcasecmp(v->value, "gr303fxsks_cpe")) {
16896 confp->chan.sig = SIG_GR303FXSKS;
16897 confp->pri.nodetype = PRI_CPE;
16898 #endif
16899 #ifdef HAVE_SS7
16900 } else if (!strcasecmp(v->value, "ss7")) {
16901 confp->chan.sig = SIG_SS7;
16902 #endif
16903 #ifdef HAVE_OPENR2
16904 } else if (!strcasecmp(v->value, "mfcr2")) {
16905 confp->chan.sig = SIG_MFCR2;
16906 #endif
16907 } else if (!strcasecmp(v->value, "auto")) {
16908 confp->is_sig_auto = 1;
16909 } else {
16910 confp->chan.outsigmod = orig_outsigmod;
16911 confp->chan.radio = orig_radio;
16912 confp->is_sig_auto = orig_auto;
16913 ast_log(LOG_ERROR, "Unknown signalling method '%s' at line %d.\n", v->value, v->lineno);
16914 }
16915 } else if (!strcasecmp(v->name, "outsignalling") || !strcasecmp(v->name, "outsignaling")) {
16916 if (!strcasecmp(v->value, "em")) {
16917 confp->chan.outsigmod = SIG_EM;
16918 } else if (!strcasecmp(v->value, "em_e1")) {
16919 confp->chan.outsigmod = SIG_EM_E1;
16920 } else if (!strcasecmp(v->value, "em_w")) {
16921 confp->chan.outsigmod = SIG_EMWINK;
16922 } else if (!strcasecmp(v->value, "sf")) {
16923 confp->chan.outsigmod = SIG_SF;
16924 } else if (!strcasecmp(v->value, "sf_w")) {
16925 confp->chan.outsigmod = SIG_SFWINK;
16926 } else if (!strcasecmp(v->value, "sf_featd")) {
16927 confp->chan.outsigmod = SIG_FEATD;
16928 } else if (!strcasecmp(v->value, "sf_featdmf")) {
16929 confp->chan.outsigmod = SIG_FEATDMF;
16930 } else if (!strcasecmp(v->value, "sf_featb")) {
16931 confp->chan.outsigmod = SIG_SF_FEATB;
16932 } else if (!strcasecmp(v->value, "sf")) {
16933 confp->chan.outsigmod = SIG_SF;
16934 } else if (!strcasecmp(v->value, "featd")) {
16935 confp->chan.outsigmod = SIG_FEATD;
16936 } else if (!strcasecmp(v->value, "featdmf")) {
16937 confp->chan.outsigmod = SIG_FEATDMF;
16938 } else if (!strcasecmp(v->value, "featdmf_ta")) {
16939 confp->chan.outsigmod = SIG_FEATDMF_TA;
16940 } else if (!strcasecmp(v->value, "e911")) {
16941 confp->chan.outsigmod = SIG_E911;
16942 } else if (!strcasecmp(v->value, "fgccama")) {
16943 confp->chan.outsigmod = SIG_FGC_CAMA;
16944 } else if (!strcasecmp(v->value, "fgccamamf")) {
16945 confp->chan.outsigmod = SIG_FGC_CAMAMF;
16946 } else if (!strcasecmp(v->value, "featb")) {
16947 confp->chan.outsigmod = SIG_FEATB;
16948 } else {
16949 ast_log(LOG_ERROR, "Unknown signalling method '%s' at line %d.\n", v->value, v->lineno);
16950 }
16951 #ifdef HAVE_PRI
16952 } else if (!strcasecmp(v->name, "pridialplan")) {
16953 if (!strcasecmp(v->value, "national")) {
16954 confp->pri.dialplan = PRI_NATIONAL_ISDN + 1;
16955 } else if (!strcasecmp(v->value, "unknown")) {
16956 confp->pri.dialplan = PRI_UNKNOWN + 1;
16957 } else if (!strcasecmp(v->value, "private")) {
16958 confp->pri.dialplan = PRI_PRIVATE + 1;
16959 } else if (!strcasecmp(v->value, "international")) {
16960 confp->pri.dialplan = PRI_INTERNATIONAL_ISDN + 1;
16961 } else if (!strcasecmp(v->value, "local")) {
16962 confp->pri.dialplan = PRI_LOCAL_ISDN + 1;
16963 } else if (!strcasecmp(v->value, "dynamic")) {
16964 confp->pri.dialplan = -1;
16965 } else if (!strcasecmp(v->value, "redundant")) {
16966 confp->pri.dialplan = -2;
16967 } else {
16968 ast_log(LOG_WARNING, "Unknown PRI dialplan '%s' at line %d.\n", v->value, v->lineno);
16969 }
16970 } else if (!strcasecmp(v->name, "prilocaldialplan")) {
16971 if (!strcasecmp(v->value, "national")) {
16972 confp->pri.localdialplan = PRI_NATIONAL_ISDN + 1;
16973 } else if (!strcasecmp(v->value, "unknown")) {
16974 confp->pri.localdialplan = PRI_UNKNOWN + 1;
16975 } else if (!strcasecmp(v->value, "private")) {
16976 confp->pri.localdialplan = PRI_PRIVATE + 1;
16977 } else if (!strcasecmp(v->value, "international")) {
16978 confp->pri.localdialplan = PRI_INTERNATIONAL_ISDN + 1;
16979 } else if (!strcasecmp(v->value, "local")) {
16980 confp->pri.localdialplan = PRI_LOCAL_ISDN + 1;
16981 } else if (!strcasecmp(v->value, "dynamic")) {
16982 confp->pri.localdialplan = -1;
16983 } else if (!strcasecmp(v->value, "redundant")) {
16984 confp->pri.localdialplan = -2;
16985 } else {
16986 ast_log(LOG_WARNING, "Unknown PRI localdialplan '%s' at line %d.\n", v->value, v->lineno);
16987 }
16988 } else if (!strcasecmp(v->name, "switchtype")) {
16989 if (!strcasecmp(v->value, "national"))
16990 confp->pri.switchtype = PRI_SWITCH_NI2;
16991 else if (!strcasecmp(v->value, "ni1"))
16992 confp->pri.switchtype = PRI_SWITCH_NI1;
16993 else if (!strcasecmp(v->value, "dms100"))
16994 confp->pri.switchtype = PRI_SWITCH_DMS100;
16995 else if (!strcasecmp(v->value, "4ess"))
16996 confp->pri.switchtype = PRI_SWITCH_ATT4ESS;
16997 else if (!strcasecmp(v->value, "5ess"))
16998 confp->pri.switchtype = PRI_SWITCH_LUCENT5E;
16999 else if (!strcasecmp(v->value, "euroisdn"))
17000 confp->pri.switchtype = PRI_SWITCH_EUROISDN_E1;
17001 else if (!strcasecmp(v->value, "qsig"))
17002 confp->pri.switchtype = PRI_SWITCH_QSIG;
17003 else {
17004 ast_log(LOG_ERROR, "Unknown switchtype '%s' at line %d.\n", v->value, v->lineno);
17005 return -1;
17006 }
17007 } else if (!strcasecmp(v->name, "nsf")) {
17008 if (!strcasecmp(v->value, "sdn"))
17009 confp->pri.nsf = PRI_NSF_SDN;
17010 else if (!strcasecmp(v->value, "megacom"))
17011 confp->pri.nsf = PRI_NSF_MEGACOM;
17012 else if (!strcasecmp(v->value, "tollfreemegacom"))
17013 confp->pri.nsf = PRI_NSF_TOLL_FREE_MEGACOM;
17014 else if (!strcasecmp(v->value, "accunet"))
17015 confp->pri.nsf = PRI_NSF_ACCUNET;
17016 else if (!strcasecmp(v->value, "none"))
17017 confp->pri.nsf = PRI_NSF_NONE;
17018 else {
17019 ast_log(LOG_WARNING, "Unknown network-specific facility '%s' at line %d.\n", v->value, v->lineno);
17020 confp->pri.nsf = PRI_NSF_NONE;
17021 }
17022 } else if (!strcasecmp(v->name, "priindication")) {
17023 if (!strcasecmp(v->value, "outofband"))
17024 confp->chan.priindication_oob = 1;
17025 else if (!strcasecmp(v->value, "inband"))
17026 confp->chan.priindication_oob = 0;
17027 else
17028 ast_log(LOG_WARNING, "'%s' is not a valid pri indication value, should be 'inband' or 'outofband' at line %d.\n",
17029 v->value, v->lineno);
17030 } else if (!strcasecmp(v->name, "priexclusive")) {
17031 confp->chan.priexclusive = ast_true(v->value);
17032 } else if (!strcasecmp(v->name, "internationalprefix")) {
17033 ast_copy_string(confp->pri.internationalprefix, v->value, sizeof(confp->pri.internationalprefix));
17034 } else if (!strcasecmp(v->name, "nationalprefix")) {
17035 ast_copy_string(confp->pri.nationalprefix, v->value, sizeof(confp->pri.nationalprefix));
17036 } else if (!strcasecmp(v->name, "localprefix")) {
17037 ast_copy_string(confp->pri.localprefix, v->value, sizeof(confp->pri.localprefix));
17038 } else if (!strcasecmp(v->name, "privateprefix")) {
17039 ast_copy_string(confp->pri.privateprefix, v->value, sizeof(confp->pri.privateprefix));
17040 } else if (!strcasecmp(v->name, "unknownprefix")) {
17041 ast_copy_string(confp->pri.unknownprefix, v->value, sizeof(confp->pri.unknownprefix));
17042 } else if (!strcasecmp(v->name, "resetinterval")) {
17043 if (!strcasecmp(v->value, "never"))
17044 confp->pri.resetinterval = -1;
17045 else if (atoi(v->value) >= 60)
17046 confp->pri.resetinterval = atoi(v->value);
17047 else
17048 ast_log(LOG_WARNING, "'%s' is not a valid reset interval, should be >= 60 seconds or 'never' at line %d.\n",
17049 v->value, v->lineno);
17050 } else if (!strcasecmp(v->name, "minunused")) {
17051 confp->pri.minunused = atoi(v->value);
17052 } else if (!strcasecmp(v->name, "minidle")) {
17053 confp->pri.minidle = atoi(v->value);
17054 } else if (!strcasecmp(v->name, "idleext")) {
17055 ast_copy_string(confp->pri.idleext, v->value, sizeof(confp->pri.idleext));
17056 } else if (!strcasecmp(v->name, "idledial")) {
17057 ast_copy_string(confp->pri.idledial, v->value, sizeof(confp->pri.idledial));
17058 } else if (!strcasecmp(v->name, "overlapdial")) {
17059 if (ast_true(v->value)) {
17060 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_BOTH;
17061 } else if (!strcasecmp(v->value, "incoming")) {
17062 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_INCOMING;
17063 } else if (!strcasecmp(v->value, "outgoing")) {
17064 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_OUTGOING;
17065 } else if (!strcasecmp(v->value, "both") || ast_true(v->value)) {
17066 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_BOTH;
17067 } else {
17068 confp->pri.overlapdial = DAHDI_OVERLAPDIAL_NONE;
17069 }
17070 #ifdef HAVE_PRI_PROG_W_CAUSE
17071 } else if (!strcasecmp(v->name, "qsigchannelmapping")) {
17072 if (!strcasecmp(v->value, "logical")) {
17073 confp->pri.qsigchannelmapping = DAHDI_CHAN_MAPPING_LOGICAL;
17074 } else if (!strcasecmp(v->value, "physical")) {
17075 confp->pri.qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL;
17076 } else {
17077 confp->pri.qsigchannelmapping = DAHDI_CHAN_MAPPING_PHYSICAL;
17078 }
17079 #endif
17080 } else if (!strcasecmp(v->name, "discardremoteholdretrieval")) {
17081 confp->pri.discardremoteholdretrieval = ast_true(v->value);
17082 #ifdef HAVE_PRI_INBANDDISCONNECT
17083 } else if (!strcasecmp(v->name, "inbanddisconnect")) {
17084 confp->pri.inbanddisconnect = ast_true(v->value);
17085 #endif
17086 } else if (!strcasecmp(v->name, "pritimer")) {
17087 #ifdef PRI_GETSET_TIMERS
17088 char tmp[20];
17089 char *timerc;
17090 char *c;
17091 int timer;
17092 int timeridx;
17093
17094 ast_copy_string(tmp, v->value, sizeof(tmp));
17095 c = tmp;
17096 timerc = strsep(&c, ",");
17097 if (!ast_strlen_zero(timerc) && !ast_strlen_zero(c)) {
17098 timeridx = pri_timer2idx(timerc);
17099 timer = atoi(c);
17100 if (timeridx < 0 || PRI_MAX_TIMERS <= timeridx) {
17101 ast_log(LOG_WARNING,
17102 "'%s' is not a valid ISDN timer at line %d.\n", timerc,
17103 v->lineno);
17104 } else if (!timer) {
17105 ast_log(LOG_WARNING,
17106 "'%s' is not a valid value for ISDN timer '%s' at line %d.\n",
17107 c, timerc, v->lineno);
17108 } else {
17109 pritimers[timeridx] = timer;
17110 }
17111 } else {
17112 ast_log(LOG_WARNING,
17113 "'%s' is not a valid ISDN timer configuration string at line %d.\n",
17114 v->value, v->lineno);
17115 }
17116
17117 } else if (!strcasecmp(v->name, "facilityenable")) {
17118 confp->pri.facilityenable = ast_true(v->value);
17119 #endif
17120 #endif
17121 #ifdef HAVE_SS7
17122 } else if (!strcasecmp(v->name, "ss7type")) {
17123 if (!strcasecmp(v->value, "itu")) {
17124 cur_ss7type = SS7_ITU;
17125 } else if (!strcasecmp(v->value, "ansi")) {
17126 cur_ss7type = SS7_ANSI;
17127 } else
17128 ast_log(LOG_WARNING, "'%s' is an unknown ss7 switch type at line %d.!\n", v->value, v->lineno);
17129 } else if (!strcasecmp(v->name, "linkset")) {
17130 cur_linkset = atoi(v->value);
17131 } else if (!strcasecmp(v->name, "pointcode")) {
17132 cur_pointcode = parse_pointcode(v->value);
17133 } else if (!strcasecmp(v->name, "adjpointcode")) {
17134 cur_adjpointcode = parse_pointcode(v->value);
17135 } else if (!strcasecmp(v->name, "defaultdpc")) {
17136 cur_defaultdpc = parse_pointcode(v->value);
17137 } else if (!strcasecmp(v->name, "cicbeginswith")) {
17138 cur_cicbeginswith = atoi(v->value);
17139 } else if (!strcasecmp(v->name, "networkindicator")) {
17140 if (!strcasecmp(v->value, "national"))
17141 cur_networkindicator = SS7_NI_NAT;
17142 else if (!strcasecmp(v->value, "national_spare"))
17143 cur_networkindicator = SS7_NI_NAT_SPARE;
17144 else if (!strcasecmp(v->value, "international"))
17145 cur_networkindicator = SS7_NI_INT;
17146 else if (!strcasecmp(v->value, "international_spare"))
17147 cur_networkindicator = SS7_NI_INT_SPARE;
17148 else
17149 cur_networkindicator = -1;
17150 } else if (!strcasecmp(v->name, "ss7_internationalprefix")) {
17151 ast_copy_string(confp->ss7.internationalprefix, v->value, sizeof(confp->ss7.internationalprefix));
17152 } else if (!strcasecmp(v->name, "ss7_nationalprefix")) {
17153 ast_copy_string(confp->ss7.nationalprefix, v->value, sizeof(confp->ss7.nationalprefix));
17154 } else if (!strcasecmp(v->name, "ss7_subscriberprefix")) {
17155 ast_copy_string(confp->ss7.subscriberprefix, v->value, sizeof(confp->ss7.subscriberprefix));
17156 } else if (!strcasecmp(v->name, "ss7_unknownprefix")) {
17157 ast_copy_string(confp->ss7.unknownprefix, v->value, sizeof(confp->ss7.unknownprefix));
17158 } else if (!strcasecmp(v->name, "ss7_called_nai")) {
17159 if (!strcasecmp(v->value, "national")) {
17160 confp->ss7.called_nai = SS7_NAI_NATIONAL;
17161 } else if (!strcasecmp(v->value, "international")) {
17162 confp->ss7.called_nai = SS7_NAI_INTERNATIONAL;
17163 } else if (!strcasecmp(v->value, "subscriber")) {
17164 confp->ss7.called_nai = SS7_NAI_SUBSCRIBER;
17165 } else if (!strcasecmp(v->value, "unknown")) {
17166 confp->ss7.called_nai = SS7_NAI_UNKNOWN;
17167 } else if (!strcasecmp(v->value, "dynamic")) {
17168 confp->ss7.called_nai = SS7_NAI_DYNAMIC;
17169 } else {
17170 ast_log(LOG_WARNING, "Unknown SS7 called_nai '%s' at line %d.\n", v->value, v->lineno);
17171 }
17172 } else if (!strcasecmp(v->name, "ss7_calling_nai")) {
17173 if (!strcasecmp(v->value, "national")) {
17174 confp->ss7.calling_nai = SS7_NAI_NATIONAL;
17175 } else if (!strcasecmp(v->value, "international")) {
17176 confp->ss7.calling_nai = SS7_NAI_INTERNATIONAL;
17177 } else if (!strcasecmp(v->value, "subscriber")) {
17178 confp->ss7.calling_nai = SS7_NAI_SUBSCRIBER;
17179 } else if (!strcasecmp(v->value, "unknown")) {
17180 confp->ss7.calling_nai = SS7_NAI_UNKNOWN;
17181 } else if (!strcasecmp(v->value, "dynamic")) {
17182 confp->ss7.calling_nai = SS7_NAI_DYNAMIC;
17183 } else {
17184 ast_log(LOG_WARNING, "Unknown SS7 calling_nai '%s' at line %d.\n", v->value, v->lineno);
17185 }
17186 } else if (!strcasecmp(v->name, "sigchan")) {
17187 int sigchan, res;
17188 sigchan = atoi(v->value);
17189 res = linkset_addsigchan(sigchan);
17190 if (res < 0)
17191 return -1;
17192
17193 } else if (!strcasecmp(v->name, "ss7_explicitacm")) {
17194 struct dahdi_ss7 *link;
17195 link = ss7_resolve_linkset(cur_linkset);
17196 if (!link) {
17197 ast_log(LOG_ERROR, "Invalid linkset number. Must be between 1 and %d\n", NUM_SPANS + 1);
17198 return -1;
17199 }
17200 if (ast_true(v->value))
17201 link->flags |= LINKSET_FLAG_EXPLICITACM;
17202 #endif
17203 #ifdef HAVE_OPENR2
17204 } else if (!strcasecmp(v->name, "mfcr2_advanced_protocol_file")) {
17205 ast_copy_string(confp->mfcr2.r2proto_file, v->value, sizeof(confp->mfcr2.r2proto_file));
17206 ast_log(LOG_WARNING, "MFC/R2 Protocol file '%s' will be used, you only should use this if you *REALLY KNOW WHAT YOU ARE DOING*.\n", confp->mfcr2.r2proto_file);
17207 } else if (!strcasecmp(v->name, "mfcr2_logdir")) {
17208 ast_copy_string(confp->mfcr2.logdir, v->value, sizeof(confp->mfcr2.logdir));
17209 } else if (!strcasecmp(v->name, "mfcr2_variant")) {
17210 confp->mfcr2.variant = openr2_proto_get_variant(v->value);
17211 if (OR2_VAR_UNKNOWN == confp->mfcr2.variant) {
17212 ast_log(LOG_WARNING, "Unknown MFC/R2 variant '%s' at line %d, defaulting to ITU.\n", v->value, v->lineno);
17213 confp->mfcr2.variant = OR2_VAR_ITU;
17214 }
17215 } else if (!strcasecmp(v->name, "mfcr2_mfback_timeout")) {
17216 confp->mfcr2.mfback_timeout = atoi(v->value);
17217 if (!confp->mfcr2.mfback_timeout) {
17218 ast_log(LOG_WARNING, "MF timeout of 0? hum, I will protect you from your ignorance. Setting default.\n");
17219 confp->mfcr2.mfback_timeout = -1;
17220 } else if (confp->mfcr2.mfback_timeout > 0 && confp->mfcr2.mfback_timeout < 500) {
17221 ast_log(LOG_WARNING, "MF timeout less than 500ms is not recommended, you have been warned!\n");
17222 }
17223 } else if (!strcasecmp(v->name, "mfcr2_metering_pulse_timeout")) {
17224 confp->mfcr2.metering_pulse_timeout = atoi(v->value);
17225 if (confp->mfcr2.metering_pulse_timeout > 500) {
17226 ast_log(LOG_WARNING, "Metering pulse timeout greater than 500ms is not recommended, you have been warned!\n");
17227 }
17228 } else if (!strcasecmp(v->name, "mfcr2_get_ani_first")) {
17229 confp->mfcr2.get_ani_first = ast_true(v->value) ? 1 : 0;
17230 } else if (!strcasecmp(v->name, "mfcr2_double_answer")) {
17231 confp->mfcr2.double_answer = ast_true(v->value) ? 1 : 0;
17232 } else if (!strcasecmp(v->name, "mfcr2_charge_calls")) {
17233 confp->mfcr2.charge_calls = ast_true(v->value) ? 1 : 0;
17234 } else if (!strcasecmp(v->name, "mfcr2_accept_on_offer")) {
17235 confp->mfcr2.accept_on_offer = ast_true(v->value) ? 1 : 0;
17236 } else if (!strcasecmp(v->name, "mfcr2_allow_collect_calls")) {
17237 confp->mfcr2.allow_collect_calls = ast_true(v->value) ? 1 : 0;
17238 } else if (!strcasecmp(v->name, "mfcr2_forced_release")) {
17239 confp->mfcr2.forced_release = ast_true(v->value) ? 1 : 0;
17240 } else if (!strcasecmp(v->name, "mfcr2_immediate_accept")) {
17241 confp->mfcr2.immediate_accept = ast_true(v->value) ? 1 : 0;
17242 #if defined(OR2_LIB_INTERFACE) && OR2_LIB_INTERFACE > 1
17243 } else if (!strcasecmp(v->name, "mfcr2_skip_category")) {
17244 confp->mfcr2.skip_category_request = ast_true(v->value) ? 1 : 0;
17245 #endif
17246 } else if (!strcasecmp(v->name, "mfcr2_call_files")) {
17247 confp->mfcr2.call_files = ast_true(v->value) ? 1 : 0;
17248 } else if (!strcasecmp(v->name, "mfcr2_max_ani")) {
17249 confp->mfcr2.max_ani = atoi(v->value);
17250 if (confp->mfcr2.max_ani >= AST_MAX_EXTENSION){
17251 confp->mfcr2.max_ani = AST_MAX_EXTENSION - 1;
17252 }
17253 } else if (!strcasecmp(v->name, "mfcr2_max_dnis")) {
17254 confp->mfcr2.max_dnis = atoi(v->value);
17255 if (confp->mfcr2.max_dnis >= AST_MAX_EXTENSION){
17256 confp->mfcr2.max_dnis = AST_MAX_EXTENSION - 1;
17257 }
17258 } else if (!strcasecmp(v->name, "mfcr2_category")) {
17259 confp->mfcr2.category = openr2_proto_get_category(v->value);
17260 if (OR2_CALLING_PARTY_CATEGORY_UNKNOWN == confp->mfcr2.category) {
17261 confp->mfcr2.category = OR2_CALLING_PARTY_CATEGORY_NATIONAL_SUBSCRIBER;
17262 ast_log(LOG_WARNING, "Invalid MFC/R2 caller category '%s' at line %d. Using national subscriber as default.\n",
17263 v->value, v->lineno);
17264 }
17265 } else if (!strcasecmp(v->name, "mfcr2_logging")) {
17266 openr2_log_level_t tmplevel;
17267 char *clevel;
17268 char *logval = ast_strdupa(v->value);
17269 while (logval) {
17270 clevel = strsep(&logval,",");
17271 if (-1 == (tmplevel = openr2_log_get_level(clevel))) {
17272 ast_log(LOG_WARNING, "Ignoring invalid logging level: '%s' at line %d.\n", clevel, v->lineno);
17273 continue;
17274 }
17275 confp->mfcr2.loglevel |= tmplevel;
17276 }
17277 #endif
17278 } else if (!strcasecmp(v->name, "cadence")) {
17279
17280 int element_count, c[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
17281 int i;
17282 struct dahdi_ring_cadence new_cadence;
17283 int cid_location = -1;
17284 int firstcadencepos = 0;
17285 char original_args[80];
17286 int cadence_is_ok = 1;
17287
17288 ast_copy_string(original_args, v->value, sizeof(original_args));
17289
17290 element_count = sscanf(v->value, "%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d,%30d", &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]);
17291
17292
17293 if (element_count % 2 == 1) {
17294 ast_log(LOG_ERROR, "Must be a silence duration for each ring duration: %s at line %d.\n", original_args, v->lineno);
17295 cadence_is_ok = 0;
17296 }
17297
17298
17299 for (i = 0; i < element_count; i++) {
17300 if (c[i] == 0) {
17301 ast_log(LOG_ERROR, "Ring or silence duration cannot be zero: %s at line %d.\n", original_args, v->lineno);
17302 cadence_is_ok = 0;
17303 break;
17304 } else if (c[i] < 0) {
17305 if (i % 2 == 1) {
17306
17307 if (cid_location == -1) {
17308 cid_location = i;
17309 c[i] *= -1;
17310 } else {
17311 ast_log(LOG_ERROR, "CID location specified twice: %s at line %d.\n", original_args, v->lineno);
17312 cadence_is_ok = 0;
17313 break;
17314 }
17315 } else {
17316 if (firstcadencepos == 0) {
17317 firstcadencepos = i;
17318
17319 } else {
17320 ast_log(LOG_ERROR, "First cadence position specified twice: %s at line %d.\n", original_args, v->lineno);
17321 cadence_is_ok = 0;
17322 break;
17323 }
17324 }
17325 }
17326 }
17327
17328
17329 for (i = 0; i < 16; i++) {
17330 new_cadence.ringcadence[i] = c[i];
17331 }
17332
17333 if (cadence_is_ok) {
17334
17335 if (element_count < 2) {
17336 ast_log(LOG_ERROR, "Minimum cadence is ring,pause: %s at line %d.\n", original_args, v->lineno);
17337 } else {
17338 if (cid_location == -1) {
17339
17340 cid_location = 1;
17341 } else {
17342
17343 cid_location = (cid_location + 1) / 2;
17344 }
17345
17346 if (!user_has_defined_cadences++)
17347
17348 num_cadence = 0;
17349 if ((num_cadence+1) >= NUM_CADENCE_MAX)
17350 ast_log(LOG_ERROR, "Already %d cadences; can't add another: %s at line %d.\n", NUM_CADENCE_MAX, original_args, v->lineno);
17351 else {
17352 cadences[num_cadence] = new_cadence;
17353 cidrings[num_cadence++] = cid_location;
17354 ast_verb(3, "cadence 'r%d' added: %s\n",num_cadence,original_args);
17355 }
17356 }
17357 }
17358 } else if (!strcasecmp(v->name, "ringtimeout")) {
17359 ringt_base = (atoi(v->value) * 8) / READ_SIZE;
17360 } else if (!strcasecmp(v->name, "prewink")) {
17361 confp->timing.prewinktime = atoi(v->value);
17362 } else if (!strcasecmp(v->name, "preflash")) {
17363 confp->timing.preflashtime = atoi(v->value);
17364 } else if (!strcasecmp(v->name, "wink")) {
17365 confp->timing.winktime = atoi(v->value);
17366 } else if (!strcasecmp(v->name, "flash")) {
17367 confp->timing.flashtime = atoi(v->value);
17368 } else if (!strcasecmp(v->name, "start")) {
17369 confp->timing.starttime = atoi(v->value);
17370 } else if (!strcasecmp(v->name, "rxwink")) {
17371 confp->timing.rxwinktime = atoi(v->value);
17372 } else if (!strcasecmp(v->name, "rxflash")) {
17373 confp->timing.rxflashtime = atoi(v->value);
17374 } else if (!strcasecmp(v->name, "debounce")) {
17375 confp->timing.debouncetime = atoi(v->value);
17376 } else if (!strcasecmp(v->name, "toneduration")) {
17377 int toneduration;
17378 int ctlfd;
17379 int res;
17380 struct dahdi_dialparams dps;
17381
17382 ctlfd = open("/dev/dahdi/ctl", O_RDWR);
17383 if (ctlfd == -1) {
17384 ast_log(LOG_ERROR, "Unable to open /dev/dahdi/ctl to set toneduration at line %d.\n", v->lineno);
17385 return -1;
17386 }
17387
17388 toneduration = atoi(v->value);
17389 if (toneduration > -1) {
17390 memset(&dps, 0, sizeof(dps));
17391
17392 dps.dtmf_tonelen = dps.mfv1_tonelen = toneduration;
17393 res = ioctl(ctlfd, DAHDI_SET_DIALPARAMS, &dps);
17394 if (res < 0) {
17395 ast_log(LOG_ERROR, "Invalid tone duration: %d ms at line %d: %s\n", toneduration, v->lineno, strerror(errno));
17396 close(ctlfd);
17397 return -1;
17398 }
17399 }
17400 close(ctlfd);
17401 } else if (!strcasecmp(v->name, "defaultcic")) {
17402 ast_copy_string(defaultcic, v->value, sizeof(defaultcic));
17403 } else if (!strcasecmp(v->name, "defaultozz")) {
17404 ast_copy_string(defaultozz, v->value, sizeof(defaultozz));
17405 } else if (!strcasecmp(v->name, "mwilevel")) {
17406 mwilevel = atoi(v->value);
17407 }
17408 } else if (!(options & PROC_DAHDI_OPT_NOWARN) )
17409 ast_log(LOG_WARNING, "Ignoring any changes to '%s' (on reload) at line %d.\n", v->name, v->lineno);
17410 }
17411 if (dahdichan[0]) {
17412
17413
17414 if (build_channels(confp, 0, dahdichan, reload, 0, &found_pseudo)) {
17415 return -1;
17416 }
17417 }
17418
17419
17420 if (!found_pseudo && reload != 1) {
17421
17422
17423
17424
17425 struct dahdi_chan_conf conf = dahdi_chan_conf_default();
17426
17427 tmp = mkintf(CHAN_PSEUDO, &conf, NULL, reload);
17428
17429 if (tmp) {
17430 ast_verb(3, "Automatically generated pseudo channel\n");
17431 } else {
17432 ast_log(LOG_WARNING, "Unable to register pseudo channel!\n");
17433 }
17434 }
17435 return 0;
17436 }
17437
17438 static int setup_dahdi(int reload)
17439 {
17440 struct ast_config *cfg, *ucfg;
17441 struct ast_variable *v;
17442 struct dahdi_chan_conf base_conf = dahdi_chan_conf_default();
17443 struct dahdi_chan_conf conf;
17444 struct ast_flags config_flags = { reload == 1 ? CONFIG_FLAG_FILEUNCHANGED : 0 };
17445 const char *cat;
17446 int res;
17447
17448 #ifdef HAVE_PRI
17449 char *c;
17450 int spanno;
17451 int i;
17452 int logicalspan;
17453 int trunkgroup;
17454 int dchannels[NUM_DCHANS];
17455 #endif
17456
17457 cfg = ast_config_load(config, config_flags);
17458
17459
17460 if (!cfg) {
17461 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
17462 return 0;
17463 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
17464 ucfg = ast_config_load("users.conf", config_flags);
17465 if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
17466 return 0;
17467 } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
17468 ast_log(LOG_ERROR, "File users.conf cannot be parsed. Aborting.\n");
17469 return 0;
17470 }
17471 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
17472 if ((cfg = ast_config_load(config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
17473 ast_log(LOG_ERROR, "File %s cannot be parsed. Aborting.\n", config);
17474 ast_config_destroy(ucfg);
17475 return 0;
17476 }
17477 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
17478 ast_log(LOG_ERROR, "File %s cannot be parsed. Aborting.\n", config);
17479 return 0;
17480 } else {
17481 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
17482 if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
17483 ast_log(LOG_ERROR, "File users.conf cannot be parsed. Aborting.\n");
17484 ast_config_destroy(cfg);
17485 return 0;
17486 }
17487 }
17488
17489
17490 ast_mutex_lock(&iflock);
17491 #ifdef HAVE_PRI
17492 if (reload != 1) {
17493
17494 v = ast_variable_browse(cfg, "trunkgroups");
17495 while (v) {
17496 if (!strcasecmp(v->name, "trunkgroup")) {
17497 trunkgroup = atoi(v->value);
17498 if (trunkgroup > 0) {
17499 if ((c = strchr(v->value, ','))) {
17500 i = 0;
17501 memset(dchannels, 0, sizeof(dchannels));
17502 while (c && (i < NUM_DCHANS)) {
17503 dchannels[i] = atoi(c + 1);
17504 if (dchannels[i] < 0) {
17505 ast_log(LOG_WARNING, "D-channel for trunk group %d must be a postiive number at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno);
17506 } else
17507 i++;
17508 c = strchr(c + 1, ',');
17509 }
17510 if (i) {
17511 if (pri_create_trunkgroup(trunkgroup, dchannels)) {
17512 ast_log(LOG_WARNING, "Unable to create trunk group %d with Primary D-channel %d at line %d of chan_dahdi.conf\n", trunkgroup, dchannels[0], v->lineno);
17513 } else
17514 ast_verb(2, "Created trunk group %d with Primary D-channel %d and %d backup%s\n", trunkgroup, dchannels[0], i - 1, (i == 1) ? "" : "s");
17515 } else
17516 ast_log(LOG_WARNING, "Trunk group %d lacks any valid D-channels at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno);
17517 } else
17518 ast_log(LOG_WARNING, "Trunk group %d lacks a primary D-channel at line %d of chan_dahdi.conf\n", trunkgroup, v->lineno);
17519 } else
17520 ast_log(LOG_WARNING, "Trunk group identifier must be a positive integer at line %d of chan_dahdi.conf\n", v->lineno);
17521 } else if (!strcasecmp(v->name, "spanmap")) {
17522 spanno = atoi(v->value);
17523 if (spanno > 0) {
17524 if ((c = strchr(v->value, ','))) {
17525 trunkgroup = atoi(c + 1);
17526 if (trunkgroup > 0) {
17527 if ((c = strchr(c + 1, ',')))
17528 logicalspan = atoi(c + 1);
17529 else
17530 logicalspan = 0;
17531 if (logicalspan >= 0) {
17532 if (pri_create_spanmap(spanno - 1, trunkgroup, logicalspan)) {
17533 ast_log(LOG_WARNING, "Failed to map span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
17534 } else
17535 ast_verb(2, "Mapped span %d to trunk group %d (logical span %d)\n", spanno, trunkgroup, logicalspan);
17536 } else
17537 ast_log(LOG_WARNING, "Logical span must be a postive number, or '0' (for unspecified) at line %d of chan_dahdi.conf\n", v->lineno);
17538 } else
17539 ast_log(LOG_WARNING, "Trunk group must be a postive number at line %d of chan_dahdi.conf\n", v->lineno);
17540 } else
17541 ast_log(LOG_WARNING, "Missing trunk group for span map at line %d of chan_dahdi.conf\n", v->lineno);
17542 } else
17543 ast_log(LOG_WARNING, "Span number must be a postive integer at line %d of chan_dahdi.conf\n", v->lineno);
17544 } else {
17545 ast_log(LOG_NOTICE, "Ignoring unknown keyword '%s' in trunkgroups\n", v->name);
17546 }
17547 v = v->next;
17548 }
17549 }
17550 #endif
17551
17552
17553 memcpy(&global_jbconf, &default_jbconf, sizeof(global_jbconf));
17554
17555 mwimonitornotify[0] = '\0';
17556
17557 v = ast_variable_browse(cfg, "channels");
17558 if ((res = process_dahdi(&base_conf, "", v, reload, 0))) {
17559 ast_mutex_unlock(&iflock);
17560 ast_config_destroy(cfg);
17561 if (ucfg) {
17562 ast_config_destroy(ucfg);
17563 }
17564 return res;
17565 }
17566
17567
17568 for (cat = ast_category_browse(cfg, NULL); cat ; cat = ast_category_browse(cfg, cat)) {
17569
17570
17571
17572 if (!strcasecmp(cat, "general") ||
17573 !strcasecmp(cat, "trunkgroups") ||
17574 !strcasecmp(cat, "globals") ||
17575 !strcasecmp(cat, "channels")) {
17576 continue;
17577 }
17578
17579 memcpy(&conf, &base_conf, sizeof(conf));
17580
17581 if ((res = process_dahdi(&conf, cat, ast_variable_browse(cfg, cat), reload, PROC_DAHDI_OPT_NOCHAN))) {
17582 ast_mutex_unlock(&iflock);
17583 ast_config_destroy(cfg);
17584 if (ucfg) {
17585 ast_config_destroy(ucfg);
17586 }
17587 return res;
17588 }
17589 }
17590
17591 ast_config_destroy(cfg);
17592
17593 if (ucfg) {
17594 const char *chans;
17595
17596 process_dahdi(&base_conf, "", ast_variable_browse(ucfg, "general"), 1, 0);
17597
17598 for (cat = ast_category_browse(ucfg, NULL); cat ; cat = ast_category_browse(ucfg, cat)) {
17599 if (!strcasecmp(cat, "general")) {
17600 continue;
17601 }
17602
17603 chans = ast_variable_retrieve(ucfg, cat, "dahdichan");
17604
17605 if (ast_strlen_zero(chans)) {
17606 continue;
17607 }
17608
17609 memcpy(&conf, &base_conf, sizeof(conf));
17610
17611 if ((res = process_dahdi(&conf, cat, ast_variable_browse(ucfg, cat), reload, PROC_DAHDI_OPT_NOCHAN | PROC_DAHDI_OPT_NOWARN))) {
17612 ast_config_destroy(ucfg);
17613 ast_mutex_unlock(&iflock);
17614 return res;
17615 }
17616 }
17617 ast_config_destroy(ucfg);
17618 }
17619 ast_mutex_unlock(&iflock);
17620
17621 #ifdef HAVE_PRI
17622 if (reload != 1) {
17623 int x;
17624 for (x = 0; x < NUM_SPANS; x++) {
17625 if (pris[x].pvts[0]) {
17626 if (start_pri(pris + x)) {
17627 ast_log(LOG_ERROR, "Unable to start D-channel on span %d\n", x + 1);
17628 return -1;
17629 } else
17630 ast_verb(2, "Starting D-Channel on span %d\n", x + 1);
17631 }
17632 }
17633 }
17634 #endif
17635 #ifdef HAVE_SS7
17636 if (reload != 1) {
17637 int x;
17638 for (x = 0; x < NUM_SPANS; x++) {
17639 if (linksets[x].ss7) {
17640 if (ast_pthread_create(&linksets[x].master, NULL, ss7_linkset, &linksets[x])) {
17641 ast_log(LOG_ERROR, "Unable to start SS7 linkset on span %d\n", x + 1);
17642 return -1;
17643 } else
17644 ast_verb(2, "Starting SS7 linkset on span %d\n", x + 1);
17645 }
17646 }
17647 }
17648 #endif
17649 #ifdef HAVE_OPENR2
17650 if (reload != 1) {
17651 int x;
17652 for (x = 0; x < r2links_count; x++) {
17653 if (ast_pthread_create(&r2links[x]->r2master, NULL, mfcr2_monitor, r2links[x])) {
17654 ast_log(LOG_ERROR, "Unable to start R2 monitor on channel group %d\n", x + 1);
17655 return -1;
17656 } else {
17657 ast_verb(2, "Starting R2 monitor on channel group %d\n", x + 1);
17658 }
17659 }
17660 }
17661 #endif
17662
17663 restart_monitor();
17664 return 0;
17665 }
17666
17667 static int load_module(void)
17668 {
17669 int res;
17670 #if defined(HAVE_PRI) || defined(HAVE_SS7)
17671 int y, i;
17672 #endif
17673
17674 #ifdef HAVE_PRI
17675 memset(pris, 0, sizeof(pris));
17676 for (y = 0; y < NUM_SPANS; y++) {
17677 ast_mutex_init(&pris[y].lock);
17678 pris[y].offset = -1;
17679 pris[y].master = AST_PTHREADT_NULL;
17680 for (i = 0; i < NUM_DCHANS; i++)
17681 pris[y].fds[i] = -1;
17682 }
17683 pri_set_error(dahdi_pri_error);
17684 pri_set_message(dahdi_pri_message);
17685 ast_register_application_xml(dahdi_send_keypad_facility_app, dahdi_send_keypad_facility_exec);
17686 #ifdef HAVE_PRI_PROG_W_CAUSE
17687 ast_register_application_xml(dahdi_send_callrerouting_facility_app, dahdi_send_callrerouting_facility_exec);
17688 #endif
17689 #endif
17690 #ifdef HAVE_SS7
17691 memset(linksets, 0, sizeof(linksets));
17692 for (y = 0; y < NUM_SPANS; y++) {
17693 ast_mutex_init(&linksets[y].lock);
17694 linksets[y].master = AST_PTHREADT_NULL;
17695 for (i = 0; i < NUM_DCHANS; i++)
17696 linksets[y].fds[i] = -1;
17697 }
17698 ss7_set_error(dahdi_ss7_error);
17699 ss7_set_message(dahdi_ss7_message);
17700 #endif
17701 res = setup_dahdi(0);
17702
17703 if (res)
17704 return AST_MODULE_LOAD_DECLINE;
17705 if (ast_channel_register(&dahdi_tech)) {
17706 ast_log(LOG_ERROR, "Unable to register channel class 'DAHDI'\n");
17707 __unload_module();
17708 return AST_MODULE_LOAD_FAILURE;
17709 }
17710 #ifdef HAVE_PRI
17711 ast_string_field_init(&inuse, 16);
17712 ast_string_field_set(&inuse, name, "GR-303InUse");
17713 ast_cli_register_multiple(dahdi_pri_cli, ARRAY_LEN(dahdi_pri_cli));
17714 #endif
17715 #ifdef HAVE_SS7
17716 ast_cli_register_multiple(dahdi_ss7_cli, ARRAY_LEN(dahdi_ss7_cli));
17717 #endif
17718 #ifdef HAVE_OPENR2
17719 ast_cli_register_multiple(dahdi_mfcr2_cli, sizeof(dahdi_mfcr2_cli)/sizeof(dahdi_mfcr2_cli[0]));
17720 ast_register_application_xml(dahdi_accept_r2_call_app, dahdi_accept_r2_call_exec);
17721 #endif
17722
17723 ast_cli_register_multiple(dahdi_cli, ARRAY_LEN(dahdi_cli));
17724
17725 memset(round_robin, 0, sizeof(round_robin));
17726 ast_manager_register( "DAHDITransfer", 0, action_transfer, "Transfer DAHDI Channel" );
17727 ast_manager_register( "DAHDIHangup", 0, action_transferhangup, "Hangup DAHDI Channel" );
17728 ast_manager_register( "DAHDIDialOffhook", 0, action_dahdidialoffhook, "Dial over DAHDI channel while offhook" );
17729 ast_manager_register( "DAHDIDNDon", 0, action_dahdidndon, "Toggle DAHDI channel Do Not Disturb status ON" );
17730 ast_manager_register( "DAHDIDNDoff", 0, action_dahdidndoff, "Toggle DAHDI channel Do Not Disturb status OFF" );
17731 ast_manager_register("DAHDIShowChannels", 0, action_dahdishowchannels, "Show status DAHDI channels");
17732 ast_manager_register("DAHDIRestart", 0, action_dahdirestart, "Fully Restart DAHDI channels (terminates calls)");
17733
17734 ast_cond_init(&ss_thread_complete, NULL);
17735
17736 return res;
17737 }
17738
17739 static int dahdi_sendtext(struct ast_channel *c, const char *text)
17740 {
17741 #define END_SILENCE_LEN 400
17742 #define HEADER_MS 50
17743 #define TRAILER_MS 5
17744 #define HEADER_LEN ((HEADER_MS + TRAILER_MS) * 8)
17745 #define ASCII_BYTES_PER_CHAR 80
17746
17747 unsigned char *buf,*mybuf;
17748 struct dahdi_pvt *p = c->tech_pvt;
17749 struct pollfd fds[1];
17750 int size,res,fd,len,x;
17751 int bytes=0;
17752
17753 float cr = 1.0;
17754 float ci = 0.0;
17755 float scont = 0.0;
17756 int idx;
17757
17758 idx = dahdi_get_index(c, p, 0);
17759 if (idx < 0) {
17760 ast_log(LOG_WARNING, "Huh? I don't exist?\n");
17761 return -1;
17762 }
17763 if (!text[0]) return(0);
17764 if ((!p->tdd) && (!p->mate)) return(0);
17765 if (p->mate)
17766 buf = ast_malloc(((strlen(text) + 1) * ASCII_BYTES_PER_CHAR) + END_SILENCE_LEN + HEADER_LEN);
17767 else
17768 buf = ast_malloc(((strlen(text) + 1) * TDD_BYTES_PER_CHAR) + END_SILENCE_LEN);
17769 if (!buf)
17770 return -1;
17771 mybuf = buf;
17772 if (p->mate) {
17773 int codec = AST_LAW(p);
17774 for (x = 0; x < HEADER_MS; x++) {
17775 PUT_CLID_MARKMS;
17776 }
17777
17778 for (x = 0; text[x]; x++) {
17779 PUT_CLID(text[x]);
17780 }
17781 for (x = 0; x < TRAILER_MS; x++) {
17782 PUT_CLID_MARKMS;
17783 }
17784 len = bytes;
17785 buf = mybuf;
17786 } else {
17787 len = tdd_generate(p->tdd, buf, text);
17788 if (len < 1) {
17789 ast_log(LOG_ERROR, "TDD generate (len %d) failed!!\n", (int)strlen(text));
17790 ast_free(mybuf);
17791 return -1;
17792 }
17793 }
17794 memset(buf + len, 0x7f, END_SILENCE_LEN);
17795 len += END_SILENCE_LEN;
17796 fd = p->subs[idx].dfd;
17797 while (len) {
17798 if (ast_check_hangup(c)) {
17799 ast_free(mybuf);
17800 return -1;
17801 }
17802 size = len;
17803 if (size > READ_SIZE)
17804 size = READ_SIZE;
17805 fds[0].fd = fd;
17806 fds[0].events = POLLOUT | POLLPRI;
17807 fds[0].revents = 0;
17808 res = poll(fds, 1, -1);
17809 if (!res) {
17810 ast_debug(1, "poll (for write) ret. 0 on channel %d\n", p->channel);
17811 continue;
17812 }
17813
17814 if (fds[0].revents & POLLPRI) {
17815 ast_free(mybuf);
17816 return -1;
17817 }
17818 if (!(fds[0].revents & POLLOUT)) {
17819 ast_debug(1, "write fd not ready on channel %d\n", p->channel);
17820 continue;
17821 }
17822 res = write(fd, buf, size);
17823 if (res != size) {
17824 if (res == -1) {
17825 ast_free(mybuf);
17826 return -1;
17827 }
17828 ast_debug(1, "Write returned %d (%s) on channel %d\n", res, strerror(errno), p->channel);
17829 break;
17830 }
17831 len -= size;
17832 buf += size;
17833 }
17834 ast_free(mybuf);
17835 return(0);
17836 }
17837
17838
17839 static int reload(void)
17840 {
17841 int res = 0;
17842
17843 res = setup_dahdi(1);
17844 if (res) {
17845 ast_log(LOG_WARNING, "Reload of chan_dahdi.so is unsuccessful!\n");
17846 return -1;
17847 }
17848 return 0;
17849 }
17850
17851
17852
17853
17854
17855 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, tdesc,
17856 .load = load_module,
17857 .unload = unload_module,
17858 .reload = reload,
17859 );